-
Notifications
You must be signed in to change notification settings - Fork 2
/
progfltk.cc
630 lines (609 loc) · 24.3 KB
/
progfltk.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
/**** file guifltk-refpersys/progfltk.cc ******
**** SPDX-License-Identifier: MIT ******
*
* © Copyright 2023 The Reflective Persistent System Team
* [email protected] & http://refpersys.org/
*
* contributors: Basile Starynkevitch <[email protected]>
*
**********************************************/
#include "fltkrps.hh"
enum my_long_option_en
{
LONGOPT__FIRST= 1000,
LONGOPT_START,
LONGOPT__LAST
};
bool do_start_refpersys=false;
const char*progname;
char myhostname[80];
std::string my_window_title="GUI-Fltk RefPerSys";
std::string fifo_prefix;
int cmdfifofd= -1, outfifofd= -1;
std::vector<std::string> rest_prog_args;
int preferred_height=333, preferred_width=444;
float screen_scale= 1.0;
Fl_Window* main_window;
struct plugin_st
{
std::string plugin_name;
std::string plugin_base;
void* plugin_dlh;
int plugin_rank;
};
std::vector<plugin_st> vector_plugins;
static const struct option long_options[] =
{
//// --version | -V
{
.name=(char*)"version", .has_arg=no_argument, .flag=(int*)nullptr,
.val=(char)'V'
},
/// --help | -h
{
.name=(char*)"help", .has_arg=no_argument, .flag=(int*)nullptr,
.val=(char)'h'
},
/// --dimension | -D widthxheight, e.g. --dimension=600x500
{
.name=(char*)"dimension", .has_arg=required_argument, .flag=(int*)nullptr,
.val=(char)'D'
},
/// --refpersys | -r dir, e.g. --refpersys=/home/john/RefPerSys
{
.name=(char*)"refpersys", .has_arg=required_argument, .flag=(int*)nullptr,
.val=(char)'r'
},
/// --scale | -S scale, e.g. --scale=1.6
{
.name=(char*)"scale", .has_arg=required_argument, .flag=(int*)nullptr,
.val=(char)'S'
},
/// --hashstr | -H string, e.g. --hashstr='abcde'
{
.name=(char*)"hashstr", .has_arg=required_argument, .flag=(int*)nullptr,
.val=(char)'H'
},
/// --title | -T title-string, e.g. --title='Fltk RefPerSys for John'
{
.name=(char*)"title", .has_arg=required_argument, .flag=(int*)nullptr,
.val=(char)'T'
},
/// --fifo | -F <fifo-prefix>, e.g. --fifo=/tmp/rps
{
.name=(char*)"fifo", .has_arg=required_argument, .flag=(int*)nullptr,
.val=(char)'F'
},
/// --start
{
.name=(char*)"start", .has_arg=no_argument, .flag=(int*)nullptr,
.val=LONGOPT_START
},
/// --plugin | -P plugin, e.g. --plugin=foo/bar to dlopen
/// the plugin foo/bar.so and dlsym in it fltkrps_bar_start, a nullary
/// function return true on success...
{
.name=(char*)"plugin", .has_arg=required_argument, .flag=(int*)nullptr,
.val=(char)'P'
},
/// final sentinel
{ .name=(char*)nullptr, .has_arg=0, .flag=(int*)nullptr, .val=0 }
};
bool load_plugin(const char*plugname)
{
char buf[256];
memset (buf, 0, sizeof(buf));
char basebuf[256];
memset(basebuf, 0, sizeof(basebuf));
if (!plugname||!plugname[0])
return false;
if (strlen(plugname)>=sizeof(buf)-16)
return false;
strcpy(buf, plugname);
const char*plugbase = basename(buf);
for (const char*p = plugbase; *p; p++)
if (!isalnum(*p) && *p != '_')
return false;
strncpy(basebuf, plugbase, sizeof(basebuf));
if (strlen(basebuf)==0 || strlen(basebuf) + 4 >= sizeof(basebuf)) return false;
strcpy(buf, plugname);
strcat(buf, ".so");
void* dlh = dlopen(buf, RTLD_NOW | RTLD_GLOBAL);
if (!dlh)
{
std::clog << progname << " failed to load plugin " << buf << ":" << dlerror() << std::endl;
return false;
}
{
typedef bool initrout_t(void);
char inibuf[sizeof(buf)+16];
memset (inibuf, 0, sizeof(inibuf));
snprintf(inibuf, sizeof(inibuf), "fltkrps_%s_start", basebuf);
void* ad = dlsym(dlh, inibuf);
if (!ad)
{
std::clog << progname << " failed to dlsym " << inibuf << " in plugin " << buf << ":" << dlerror() << std::endl;
dlclose(dlh);
return false;
}
initrout_t*ini = (initrout_t*) ad;
bool ok= (*ini)();
if (!ok)
{
std::clog << progname << " failed to initialize plugin " << buf << " using " << inibuf << std::endl;
dlclose(dlh);
return false;
}
}
plugin_st p;
memset(&p, 0, sizeof(p));
p.plugin_name.assign(buf) ;
p.plugin_base.assign(basebuf);
p.plugin_dlh = dlh;
p.plugin_rank = vector_plugins.size();
vector_plugins.push_back(p);
std::clog << progname << " loaded plugin#" << p.plugin_rank << ": "<< p.plugin_name << std::endl;
return true;
} // end load_plugin
static void
show_usage(void)
{
std::clog << progname << " usage:" << std::endl
<< "\t --version | -V "
<< "\t\t# show version" << std::endl
<< "\t --dimension= | -D <width>x<height> "
<< "\t\t# preferred window dimension" << std::endl
<< "\t --refpersys= | -r <directory> " << std::endl
<< "\t\t# RefPerSys directory, containing its source, executable, persistore" << std::endl
<< "\t --scale= | -S<scale-factor> "
<< "\t\t# preferred scale factor" << std::endl
<< "\t --plugin= | -P<plugin-file> "
<< "\t\t# plugin (with .so suffix)" << std::endl
<< "\t --fifo= | -F<fifo-prefix> "
<< "\t\t# FIFO *.{cmd,out} used to communicate with RefPerSys" << std::endl
<< "\t --title= | -T<title> "
<< "\t\t# title of the window" << std::endl
<< "\t --hashstr | -H<string> "
<< "\t\t# compute and give on stdout the hash of the string"
<< std::endl
<< "\t --start "
<< "\t\t# really start RefPerSys, using FIFO if provided, and other arguments..."
<< std::endl
<< "\t --help | -h "
<< "\t\t# give this help" << std::endl
<< "### see also refpersys.org and github.com/RefPerSys/RefPerSys" << std::endl
;
} // end show_usage
static void
parse_program_options (int argc, char*const*argv)
{
int op= -1;
int ix;
while ((ix= -1), //
(op = getopt_long(argc, argv,
"VhD:P:S:r:T:F:", //short option string
long_options,
&ix)),
(op>=0))
{
asm volatile ("nop;nop;nop;nop;nop;nop;nop;nop"); // for a GDB breakpoint
switch (op)
{
case 'V': // --version
{
int sc= 0;
std::clog << progname << " version:" << std::endl
<< "\t built:" << __DATE__ "@" << __TIME__ << " on " << BUILD_HOST
<< " git: " << SHORTGIT_ID << std::endl
<< "\t FLTK:" << Fl::api_version() << " ABI " << FL_ABI_VERSION << std::endl
<< "\t screen count:" << Fl::screen_count();
sc = Fl::screen_scaling_supported();
if (sc==0)
std::clog << " no scaling";
else if (sc==1)
std::clog << " shared scaling factor";
else if (sc==2)
std::clog<< " scalable independently";
std::clog<<std::endl;
};
break;
case 'h': /// --help
show_usage();
break;
case 'H':
{
std::clog << progname << " hashing string:" << std::endl
<< optarg << std::endl;
int64_t ht[2] = {0,0};
if (rps_compute_cstr_two_64bits_hash(ht, optarg, -1))
{
char buf[128];
memset(buf, 0, sizeof(buf));
snprintf(buf, sizeof(buf),
"h0=%ld=%#lx h1=%ld=%#lx",
ht[0], ht[0], ht[1], ht[1]);
std::clog << buf << std::endl;
}
else
{
std::cerr << progname << " failed to rps_compute_cstr_two_64bits_hash on " << optarg << std::endl;
exit (EXIT_FAILURE);
};
std::string optstr(optarg);
std::clog << std::hash<std::string> {}(optarg) << std::endl;
}
break;
case 'r': /// --refpersys <directory>
{
if (!set_refpersys_path(optarg))
{
std::cerr << progname << " invalid RefPerSys path: " << optarg << std::endl;
exit (EXIT_FAILURE);
}
}
break;
case 'D': /// --dimension=<width>x<height> # e.g. --geometry 400x333
{
int w= -1, h= -1;
errno = 0;
int sdim= sscanf(optarg, "%d[xX]%d", &w, &h);
if (sdim < 2)
{
std::cerr << progname << " bad dimension " << optarg
<< " expecting <width>x<height> e.g. 400x333"
<< "(sdim=" << sdim
<< " " << strerror(errno) << ")" << std::endl;
exit(EXIT_FAILURE);
};
if (w<100) w=100;
if (h<64) h=64;
if (w>2048) w=2048;
if (h>2048) h=2048;
preferred_width = w;
preferred_height = h;
std::clog << progname << ": preferred window dimension:"
<< std::endl << "\t width=" << preferred_width
<< ", height=" << preferred_height
<< " ..." << std::endl;
}
break;
case 'S': //// --scale=<float> #e.g. --scale=1.5
{
float s=1.0;
errno = 0;
if (sscanf(optarg, "%f", &s) < 1)
{
std::cerr << progname << "bad scale " << optarg
<< " " << strerror(errno) << std::endl;
exit(EXIT_FAILURE);
};
if (s<0.05) s = 0.05;
if (s>20.0) s = 20.0;
screen_scale = s;
std::clog << progname << ": preferred scale:" << screen_scale << std::endl;
}
break;
case 'T': //// --title=<string> #e.g. --title='RefPerSys for John'
{
my_window_title.assign(optarg);
};
break;
case 'F': //// --fifo=<fifo-prefix> for {.cmd,.out} to RefPerSys
{
fifo_prefix.assign(optarg);
}
break;
case 'P': //// --plugin=<basepath> #e.g --plugin=$HOME/lib/myplug
if (!load_plugin(optarg))
{
std::clog << progname << " failed to load plugin " << optarg << std::endl;
exit(EXIT_FAILURE);
};
break;
case LONGOPT_START:
{
do_start_refpersys= true;
};
break;
default:
std::clog << progname << ": with unexpected argument: " << optarg << std::endl;
exit(EXIT_FAILURE);
};
};
asm volatile ("nop;nop;nop;nop;nop;nop;nop;nop"); /// for a GDB breakpoint
if (optind < argc)
{
rest_prog_args.reserve(argc-optind);
for (int i=optind; i<argc; i++)
rest_prog_args.push_back(std::string{argv[i]});
}
} // end parse_program_options
void
create_main_window(void)
{
main_window = new Fl_Window(preferred_height, preferred_width);
main_window->label(my_window_title.c_str());
} // end create_main_window
bool
set_refpersys_path(const char*path)
{
/// in comments path is supposed to be ~/RefPerSys/
static bool alreadycalled;
if (alreadycalled)
{
std::cerr << progname << " cannot set RefPerSys path twice, here to " << path << std::endl;
return false;
};
alreadycalled = true;
struct stat srefp;
memset(&srefp, 0, sizeof(srefp));
if (stat(path, &srefp))
{
std::cerr << progname << " fails to stat RefPerSys path " << path << " : " << strerror(errno) << "." << std::endl;
return false;
};
if (!srefp.st_mode & S_IFMT != S_IFDIR)
{
std::cerr << progname << " given RefPerSys path " << path << " is not a directory." << std::endl;
return false;
};
std::string pathstr(path);
/// check presence of ~/RefPerSys/refpersys executable
{
std::string exepath=pathstr + "/refpersys";
memset(&srefp, 0, sizeof(srefp));
if (stat(exepath.c_str(), &srefp))
{
std::cerr << progname << " RefPerSys path " << path << " without program " << exepath << ":" << strerror(errno) << "." << std::endl;
return false;
};
if (!srefp.st_mode & S_IFMT != S_IFREG)
{
std::cerr << progname << " given RefPerSys path " << path << " with non-file " << exepath << " ..." << std::endl;
return false;
};
if (!srefp.st_mode & S_IEXEC)
{
std::cerr << progname << " given RefPerSys path " << path << " with non-executable " << exepath << " ..." << std::endl;
return false;
};
/// executable ~/RefPerSys/refpersys should be an ELF binary
{
FILE* fexe = fopen(exepath.c_str(), "rb");
if (!fexe)
{
std::cerr << progname << " given RefPerSys path " << path << " has unreadable executable " << exepath << ":" << strerror(errno) << "." << std::endl;
return false;
};
Elf64_Ehdr elfhead;
memset (&elfhead, 0, sizeof(elfhead));
if (fread(&elfhead, sizeof(elfhead), 1, fexe) != 1)
{
std::cerr << progname << " given RefPerSys path "
<< path << " has unreadable ELF executable "
<< exepath << ":" << strerror(errno) << "." << std::endl;
fclose(fexe);
return false;
};
if (elfhead.e_ident[EI_MAG0] != ELFMAG0
|| elfhead.e_ident[EI_MAG1] != ELFMAG1
|| elfhead.e_ident[EI_MAG2] != ELFMAG2
|| elfhead.e_ident[EI_MAG3] != ELFMAG3
|| elfhead.e_ident[EI_CLASS] != ELFCLASS64
|| (elfhead.e_type != ET_EXEC && elfhead.e_type != ET_DYN))
{
std::cerr << progname << " given RefPerSys path " << path
<< " has bad ELF executable " << exepath
<< "." << std::endl;
fclose(fexe);
return false;
}
}
}
/// check presence of ~/RefPerSys/refpersys.hh header
{
std::string headerpath=pathstr + "/refpersys.hh";
FILE*fhead = fopen(headerpath.c_str(), "r");
if (!fhead)
{
std::cerr << progname << " RefPerSys path " << path << " without header " << headerpath << ":" << strerror(errno) << "." << std::endl;
return false;
};
char linbuf[64];
memset(linbuf, 0, sizeof(linbuf));
if (!fgets(linbuf, sizeof(linbuf), fhead))
{
std::cerr << progname << " RefPerSys path " << path << " with unreadable header " << headerpath << ":" << strerror(errno) << "." << std::endl;
fclose(fhead);
return false;
};
if (linbuf[0] != '/' || linbuf[1] != '*' || linbuf[2] != '*' || linbuf[3] != '*' || linbuf[4] != '*')
{
std::cerr << progname << " RefPerSys path " << path << " with bad header first line " << linbuf << std::endl;
fclose(fhead);
return false;
};
fclose(fhead);
}
//// check presence of ~/RefPerSys/LICENSE file
{
std::string licpath=pathstr + "/LICENSE";
FILE*flic = fopen(licpath.c_str(), "r");
if (!flic)
{
std::cerr << progname << " RefPerSys path "
<< path << " without LICENSE file "
<< licpath << ":"
<< strerror(errno) << "." << std::endl;
return false;
};
char linbuf[80];
memset(linbuf, 0, sizeof(linbuf));
int nbl=0;
constexpr int liclinelimit=64;
bool gplmentioned=false;
while (nbl<liclinelimit && !feof(flic))
{
nbl++;
memset(linbuf, 0, sizeof(linbuf));
if (!fgets(linbuf, sizeof(linbuf)-4, flic))
{
if (feof(flic))
break;
std::cerr << progname << " cannot read line#" << nbl
<< " of license file " << licpath
<< " :" << strerror(errno) << std::endl;
fclose(flic);
return false;
};
if (!gplmentioned)
gplmentioned = strstr(linbuf, "www.gnu.org/licenses");
}
fclose(flic), flic=nullptr;
if (!gplmentioned)
{
std::cerr << progname << " RefPerSys path "
<< path << " with incorrect LICENSE file "
<< licpath << "." << std::endl;
}
}
//// check presence of ~/RefPerSys/rps_manifest.json file
{
std::string manifpath=pathstr + "/rps_manifest.json";
FILE*manif = fopen(manifpath.c_str(), "r");
if (!manif)
{
std::cerr << progname << " RefPerSys path "
<< path << " without manifest file "
<< manifpath << ":"
<< strerror(errno) << "." << std::endl;
return false;
};
char linbuf[80];
memset(linbuf, 0, sizeof(linbuf));
if (!fgets(linbuf, sizeof(linbuf)-4, manif))
{
std::cerr << progname << " cannot read first line"
<< " of manifest file " << manifpath
<< " :" << strerror(errno) << std::endl;
fclose(manif);
return false;
};
constexpr const char firstmanif[]="//!! GENERATED file rps_manifest.json / DO NOT EDIT!";
if (strncmp(linbuf, firstmanif, sizeof(firstmanif)-1))
{
std::cerr << progname << " bad first line"
<< " of manifest file " << manifpath << ":" << std::endl
<< linbuf << std::endl;
fclose(manif);
return false;
};
}
//// check presence of ~/RefPerSys/persistore/ directory and that it contains some *json file.
{
std::string persistpath= pathstr+"/persistore";
DIR* persidir= opendir(persistpath.c_str());
if (!persidir)
{
std::cerr << progname << " cannot open RefPerSys persistent store directory " << persistpath
<< " :" << strerror(errno) << std::endl;
return false;
};
struct dirent* ent=nullptr;
int nbjsonfiles=0;
int nbent=0;
while ((ent=readdir(persidir)) != nullptr)
{
if (ent->d_type == DT_REG /*regular file*/
&& isalnum(ent->d_name[0]))
{
nbent++;
int nlen = strlen(ent->d_name);
if (nlen>10 && !strcmp(ent->d_name+nlen-sizeof(".json")+1, ".json"))
nbjsonfiles++;
}
}
closedir(persidir);
if (nbjsonfiles==0)
{
std::cerr << progname
<< " did not found JSON files in persistent store directory "
<< persistpath << " with " << nbent << " entries." << std::endl;
return false;
}
};
std::cout << progname << " using RefPerSys from " << pathstr << " on " << myhostname << " pid " << (int)getpid() << std::endl;
return true;
} // end set_refpersys_path
void
do_create_fifos(std::string prefix)
{
/// see https://stackoverflow.com/q/7931811/841108
std::string cmdfifo= prefix + ".cmd";
std::string outfifo= prefix + ".out";
errno = 0;
/// the command FIFO is written by RefPerSys, and so is read by this GUI interface
if (access(cmdfifo.c_str(), R_OK) && errno == ENOENT)
{
if (mkfifo(cmdfifo.c_str(), 0660)<0)
{
std::clog << progname << " failed to create command FIFO " << cmdfifo << " :" << strerror(errno) << std::endl;
exit(EXIT_FAILURE);
}
std::cout << progname << " created command FIFO " << cmdfifo << std::endl;
}
/// the output FIFO is read by RefPerSys so is written by this GUI interface
if (access(outfifo.c_str(), W_OK) && errno == ENOENT)
{
if (mkfifo(outfifo.c_str(), 0660)<0)
{
std::clog << progname << " failed to create output FIFO " << outfifo << " :" << strerror(errno) << std::endl;
exit(EXIT_FAILURE);
}
std::cout << progname << " created output FIFO " << outfifo << std::endl;
}
} // end do_create_fifos
int
main(int argc, char**argv)
{
progname = argv[0];
memset(myhostname, 0, sizeof(myhostname));
gethostname(myhostname, sizeof(myhostname)-4);
parse_program_options(argc, argv);
fl_open_display();
if (!fifo_prefix.empty())
{
do_create_fifos(fifo_prefix);
std::string cmdfifo= fifo_prefix + ".cmd";
std::string outfifo= fifo_prefix + ".out";
cmdfifofd = open(cmdfifo.c_str(), 0440| R_OK);
if (cmdfifofd < 0)
{
int e = errno;
std::clog << progname << " pid " << (int)getpid() << " git " << SHORTGIT_ID
<< " failed to open command FIFO " << cmdfifo << " for read : " << strerror(e) << std::endl;
exit(EXIT_FAILURE);
};
Fl::add_fd(outfifofd, FL_READ, out_fd_handler);
outfifofd = open(outfifo.c_str(), 0660| W_OK);
if (outfifofd < 0)
{
int e = errno;
std::clog << progname << " pid " << (int)getpid() << " git " << SHORTGIT_ID
<< " failed to open output FIFO " << outfifo << " for write : " << strerror(e) << std::endl;
exit(EXIT_FAILURE);
};
Fl::add_fd(cmdfifofd, FL_WRITE, cmd_fd_handler);
};
create_main_window();
#warning do_start_refpersys should be used
main_window->show(argc, argv);
std::cout << progname << " running pid " << (int)getpid()
<< " on " << myhostname << " FLTK:" << Fl::abi_version()
<< ", git "
<< SHORTGIT_ID << std::endl
<< ".... built " << __DATE__ "," __TIME__
<< " on " << BUILD_HOST << std::endl;
return Fl::run();
} // end main
/// enf of file progfltk.cc