forked from RefPerSys/RefPerSys
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutilities_rps.cc
More file actions
2482 lines (2345 loc) · 90.2 KB
/
Copy pathutilities_rps.cc
File metadata and controls
2482 lines (2345 loc) · 90.2 KB
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
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/****************************************************************
* file utilities_rps.cc
* SPDX-License-Identifier: GPL-3.0-or-later
*
* Description:
* This file is part of the Reflective Persistent System.
*
* It has some utility functions.
*
* Author(s):
* Basile Starynkevitch (France) <basile@starynkevitch.net>
* Abhishek Chakravarti <abhishek@taranjali.org>
* Nimesh Neema <nimeshneema@gmail.com>
*
* © Copyright (C) 2019 - 2025 The Reflective Persistent System Team
* team@refpersys.org & http://refpersys.org/
*
* License:
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
******************************************************************************/
#include "refpersys.hh"
// comment for our do-scan-refpersys-pkgconfig.c utility
//@@PKGCONFIG gmp
//@@PKGCONFIG gmpxx
//@@PKGCONFIG glib-2.0
//@@PKGCONFIG cairo
#if RPS_WITH_FLTK
#include <FL/Fl.H>
#include <FL/platform.H>
#include <FL/Fl_Window.H>
#include <FL/Fl_Menu_Bar.H>
#include <FL/Fl_Multi_Label.H>
#include <FL/Fl_Widget.H>
#if FLTK_API_VERSION >= 10400
#include <FL/Fl_Pack.H>
#include <FL/Fl_Flex.H>
#endif
#include <FL/Fl_Text_Buffer.H>
#include <FL/Fl_Text_Editor.H>
#include <FL/Fl_Box.H>
#endif // RPS_WITH_FLTK
#include "glib.h"
#include "libgccjit.h"
extern "C" const char rps_utilities_gitid[];
const char rps_utilities_gitid[]= RPS_GITID;
extern "C" const char rps_utilities_date[];
const char rps_utilities_date[]= __DATE__;
extern "C" const char rps_utilities_shortgitid[];
const char rps_utilities_shortgitid[]= RPS_SHORTGITID;
extern "C" void rps_set_user_preferences(const char*path);
extern "C" char*rps_chdir_path_after_load;
static bool rps_flag_pref_help;
std::string rps_run_name;
/// https://lists.gnu.org/archive/html/lightning/2023-08/msg00004.html
/// see also file lightgen_rps.cc
static void rps_compute_program_invocation(int argc, char**argv);
// we may have a pair of FIFO to communicate with some external
// process (for graphical user interface), perhaps mini-edit-fltk on
// https://github.com/bstarynk/misc-basile/ ... The FIFO prefix is
// $FIFOPREFIX. The messages from the GUI user interface to RefPerSys
// are on $FIFOPREFIX.out; the messages from RefPerSys to that GUI
// user interface are on $FIFOPREFIX.cmd
static std::string rps_fifo_prefix;
char rps_bufpath_homedir[rps_path_byte_size];
char rps_debug_path[rps_path_byte_size];
static struct rps_fifo_fdpair_st rps_fifo_pair;
static std::vector<std::string> rps_postponed_removed_files_vector;
static std::mutex rps_postponed_lock;
std::string rps_publisher_url_str;
static pthread_t rps_main_thread_handle;
extern "C" char*rps_pidfile_path;
int
rps_get_major_version(void)
{
return RPS_MAJOR_VERSION_NUM;
} // end rps_get_major_version
int
rps_get_minor_version(void)
{
return RPS_MINOR_VERSION_NUM;
} // end rps_get_minor_version
bool
rps_is_main_thread(void)
{
return pthread_self() == rps_main_thread_handle;
} // end rps_is_main_thread
bool
rps_want_user_preferences_help(void)
{
return rps_flag_pref_help;
} // end rps_want_user_preferences_help
static std::map<std::string,std::string> rps_dict_extra_arg;
void
rps_put_fifo_prefix(const char*pref)
{
RPS_ASSERT(rps_is_main_thread());
if (!rps_fifo_prefix.empty())
return;
if (pref && pref[0])
rps_fifo_prefix = std::string(pref);
} // end rps_put_fifo_prefix
std::string
rps_get_fifo_prefix(void)
{
return rps_fifo_prefix;
} // end rps_get_fifo_prefix
struct rps_fifo_fdpair_st
rps_get_gui_fifo_fds(void)
{
if (rps_gui_pid)
return rps_fifo_pair;
else return {-1, -1};
} // end rps_get_gui_fifo_fd
pid_t
rps_get_gui_pid(void)
{
return rps_gui_pid;
} // end rps_get_gui_pid
static void
rps_remove_fifos(void)
{
std::string cmdfifo = rps_fifo_prefix+".cmd";
if (rps_is_fifo(cmdfifo))
remove(cmdfifo.c_str());
std::string outfifo = rps_fifo_prefix+".out";
if (rps_is_fifo(outfifo))
remove(outfifo.c_str());
} // end rps_remove_fifos
void
rps_do_create_fifos_from_prefix(void)
{
int cmdfd= -1;
int outfd= -1;
bool rmatex = false;
char cwdbuf[rps_path_byte_size+4];
memset(cwdbuf, 0, sizeof(cwdbuf));
RPS_ASSERT(rps_is_main_thread());
if (!getcwd(cwdbuf, rps_path_byte_size))
strcpy(cwdbuf, "./");
std::string cmdfifo = rps_fifo_prefix+".cmd";
std::string outfifo = rps_fifo_prefix+".out";
RPS_DEBUG_LOG(REPL, "rps_do_create_fifos_from_prefix " << rps_fifo_prefix
<< " in " << cwdbuf << std::endl
<< RPS_FULL_BACKTRACE_HERE(1, "rps_do_create_fifos_from_prefix"));
if (!rps_is_fifo(cmdfifo))
{
if (mkfifo(cmdfifo.c_str(), 0660)<0)
RPS_FATALOUT("failed to create command FIFO " << cmdfifo << ":" << strerror(errno));
RPS_INFORMOUT("created command FIFO " << cmdfifo
<< " cwd:" << cwdbuf << " pid "
<< (int)getpid() << " on " << rps_hostname()
<< " git " << rps_shortgitid);
rmatex = true;
}
RPS_DEBUG_LOG(REPL, "rps_do_create_fifos_from_prefix opening cmdfifo " << cmdfifo);
cmdfd = open(cmdfifo.c_str(), 0660 | O_CLOEXEC | O_NONBLOCK);
if (cmdfd<0)
RPS_FATALOUT("failed to open command FIFO " << cmdfifo << ":" << strerror(errno));
RPS_DEBUG_LOG(REPL, "rps_do_create_fifos_from_prefix cmdfd#" << cmdfd
<< " cmdfifo:" << cmdfifo);
if (!rps_is_fifo(outfifo))
{
if (mkfifo(outfifo.c_str(), 0660)<0)
RPS_FATALOUT("failed to create output FIFO " << outfifo << ":" << strerror(errno));
RPS_INFORMOUT("created output FIFO " << outfifo
<< " cwd:" << cwdbuf << " pid "
<< (int)getpid() << " on " << rps_hostname()
<< " git " << rps_shortgitid);
rmatex = true;
}
RPS_DEBUG_LOG(REPL, "rps_do_create_fifos_from_prefix opening outfifo " << outfifo);
outfd = open(outfifo.c_str(), 0440 | O_CLOEXEC | O_NONBLOCK);
if (outfd<0)
RPS_FATALOUT("failed to open output FIFO " << outfifo << ":" << strerror(errno));
RPS_DEBUG_LOG(REPL, "rps_do_create_fifos_from_prefix outfd#" << outfd
<< " outfifo:" << outfifo);
if (rmatex)
atexit(rps_remove_fifos);
rps_fifo_pair.fifo_ui_wcmd = cmdfd;
rps_fifo_pair.fifo_ui_rout = outfd;
RPS_INFORMOUT("RefPerSys did create (in pid " << (int)getpid()
<< ") command and output FIFOs to communicate with GUI" << std::endl
<< "… using for writing commands to GUI " << cmdfifo << " fd#" << cmdfd
<< std::endl
<< "… and for reading JSON output from GUI " << outfifo << " fd#" << outfd
<< "… git " << rps_shortgitid);
RPS_POSSIBLE_BREAKPOINT();
} // end rps_do_create_fifos_from_prefix
const char*
rps_homedir(void)
{
static std::mutex homedirmtx;
std::lock_guard<std::mutex> gu(homedirmtx);
if (RPS_UNLIKELY(rps_bufpath_homedir[0] == (char)0))
{
const char*rpshome = getenv("REFPERSYS_HOME");
const char*home = getenv("HOME");
const char*path = rpshome?rpshome:home;
if (!path)
RPS_FATAL("no RefPerSys home ($REFPERSYS_HOME or $HOME)");
char* rp = realpath(path, nullptr);
if (!rp)
RPS_FATAL("realpath failed on RefPerSys home %s - %m",
path);
if (strlen(rp) >= sizeof(rps_bufpath_homedir) -1)
RPS_FATAL("too long realpath %s on RefPerSys home %s", rp, path);
strncpy(rps_bufpath_homedir, rp, sizeof(rps_bufpath_homedir) -1);
}
return rps_bufpath_homedir;
} // end rps_homedir
const std::string&
rps_get_loaddir(void)
{
return rps_my_load_dir;
} // end rps_get_loaddir
const char*
rps_hostname(void)
{
static char hnambuf[80];
if (RPS_UNLIKELY(!hnambuf[0]))
gethostname(hnambuf, sizeof(hnambuf)-1);
return hnambuf;
} // end rps_hostname
const char*
rps_get_extra_arg(const char*name)
{
if (!name) return nullptr;
bool is_good_name=isalpha(name[0]);
for (const char*pc = name; is_good_name && *pc; pc++)
is_good_name = isalnum(*pc) || *pc == '_';
if (RPS_UNLIKELY(!is_good_name))
return nullptr;
std::string goodstr{name};
auto it = rps_dict_extra_arg.find(goodstr);
if (it == rps_dict_extra_arg.end())
return nullptr;
return it->second.c_str();
} // end rps_get_extra_arg
void
rps_emit_gplv3_copyright_notice_AT(std::ostream&outs, //
const char*fil, int lin, const char*fromfunc,//
std::string path, std::string linprefix, std::string linsuffix, std::string owner, std::string reason)
{
outs << linprefix << "SPDX-License-Identifier: GPL-3.0-or-later"
<< linsuffix << std::endl;
outs << linprefix
<< "GENERATED [GPLv3+] file " << path << " / DO NOT EDIT!"
<< linsuffix << std::endl;
outs << linprefix << "generating-git " << rps_shortgitid << linsuffix << std::endl;
if (reason.length()>0)
{
outs << linprefix << "~" << reason << linsuffix << std::endl;
};
outs << linprefix
<< "This " << path << " file is generated by ..."
<< linsuffix << std::endl << linprefix << " the RefPerSys "
<< rps_get_major_version() << "." << rps_get_minor_version()
<< linsuffix << std::endl;
outs << linprefix << "open source software. See refpersys.org and" << linsuffix << std::endl;
outs << linprefix << "contact team@refpersys.org"
<< linsuffix << std::endl;
outs << linprefix
<< "This file is part of the Reflective Persistent System."
<< linsuffix << std::endl;
{
time_t nowtime = time(nullptr);
struct tm nowtm = {};
localtime_r(&nowtime, &nowtm);
outs << linprefix
<< " © " "Copyright " "(C) "
<< RPS_INITIAL_COPYRIGHT_YEAR
<< " - "
<< (nowtm.tm_year + 1900) << " "
<< ((owner.empty()) ? "The Reflective Persistent Team" : owner.c_str())
<< linsuffix << std::endl;
outs << linprefix
<< " see refpersys.org and contact team@refpersys.org for more."
<< linsuffix << std::endl;
}
outs << linprefix << "_"
<< linsuffix << std::endl;
outs << linprefix << "This program is free software: you can redistribute it and/or modify"
<< linsuffix << std::endl;
outs << linprefix << "it under the terms of the GNU General Public License as published by"
<< linsuffix << std::endl;
outs << linprefix << "the Free Software Foundation, either version 3 of the License, or"
<< linsuffix << std::endl;
outs << linprefix << "(at your option) any later version."
<< linsuffix << std::endl;
outs << linprefix << "_"
<< linsuffix << std::endl;
outs << linprefix << "This program is distributed in the hope that it will be useful,"
<< linsuffix << std::endl;
outs << linprefix << "but WITHOUT ANY WARRANTY; without even the implied warranty of"
<< linsuffix << std::endl;
outs << linprefix << "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the"
<< linsuffix << std::endl;
outs << linprefix << "GNU General Public License for more details."
<< linsuffix << std::endl;
outs << linprefix << "_"
<< linsuffix << std::endl;
outs << linprefix << "You should have received a copy of the GNU "
"General Public License"
<< linsuffix << std::endl;
outs << linprefix << "along with this program. If not, see <http://www.gnu.org/licenses/>."
<< linsuffix << std::endl;
outs << linprefix << "generated from git " << rps_shortgitid
<< " branch " << rps_gitbranch << linsuffix << std::endl;
if (fil && lin>0 && fromfunc)
{
outs << linprefix << " emitted from " << fil << ":" << lin << linsuffix << std::endl;
outs << linprefix << " by " << fromfunc << linsuffix << std::endl;
}
} // end rps_emit_gplv3_copyright_notice_AT
void
rps_emit_lgplv3_copyright_notice_AT(std::ostream&outs,//
const char*fil, int lin, const char*fromfunc, //
std::string path, std::string linprefix, std::string linsuffix, std::string owner, std::string reason)
{
outs << linprefix << "SPDX-License-Identifier: LGPL-3.0-or-later"
<< linsuffix << std::endl;
outs << linprefix
<< "GENERATED [LGPLv3+] file " << path << " / DO NOT EDIT!"
<< linsuffix << std::endl;
outs << linprefix << "generating-git " << rps_shortgitid << linsuffix << std::endl;
if (reason.length()>0)
{
outs << linprefix << "~" << reason << linsuffix << std::endl;
};
outs << linprefix
<< "This " << path << " file is generated by ..." << linsuffix << std::endl
<< linprefix << " the RefPerSys "
<< rps_get_major_version() << "." << rps_get_minor_version()
<< linsuffix << std::endl;
outs << linprefix << "open source software. See refpersys.org and contact"
<< linsuffix << std::endl;
outs << linprefix << " team@refpersys.org ..."
<< linsuffix << std::endl;
{
time_t nowtime = time(nullptr);
struct tm nowtm = {};
localtime_r(&nowtime, &nowtm);
outs << linprefix
<< "© " "Copyright" " (C) "
<< RPS_INITIAL_COPYRIGHT_YEAR
<< " - "
<< (nowtm.tm_year + 1900)
<< ((owner.empty()) ? "The Reflective Persistent Team" : owner.c_str());
outs << linsuffix << std::endl;
}
outs << linprefix << "_"
<< linsuffix << std::endl;
outs << linprefix << "This file is free software: you can redistribute it and/or modify"
<< linsuffix << std::endl;
outs << linprefix << "it under the terms of the GNU Lesser General Public License as"
<< linsuffix << std::endl;
outs << linprefix << "published by the Free Software Foundation, either version 3 of the"
<< linsuffix << std::endl;
outs << linprefix << "License, or (at your option) any later version."
<< linsuffix << std::endl;
outs << linprefix << "_"
<< linsuffix << std::endl;
outs << linprefix << "This file is distributed in the hope that it will be useful,"
<< linsuffix << std::endl;
outs << linprefix << "but WITHOUT ANY WARRANTY; without even the implied warranty of"
<< linsuffix << std::endl;
outs << linprefix << "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the"
<< linsuffix << std::endl;
outs << linprefix << "GNU Lesser General Public License for more details."
<< linsuffix << std::endl;
outs << linprefix << "_"
<< linsuffix << std::endl;
outs << linprefix << "You should have received a copy of the GNU "
"Lesser General Public License"
<< linsuffix << std::endl;
outs << linprefix << "along with this program. If not, see <http://www.gnu.org/licenses/>."
<< linsuffix << std::endl;
outs << linprefix << "generated from git " << rps_shortgitid
<< " branch " << rps_gitbranch << linsuffix << std::endl;
if (fil && lin>0 && fromfunc)
{
outs << linprefix
<< " emitted from " << fil << ":" << lin
<< linsuffix << std::endl;
outs << linprefix
<< " by " << fromfunc
<< linsuffix << std::endl;
};
} // end rps_emit_lgplv3_copyright_notice_AT
////////////////
void
rps_print_types_info(void)
{
#define TYPEFMT_rps "%-62s:"
printf(TYPEFMT_rps " size align (bytes)\n", "**TYPE**");
/////
#define EXPLAIN_TYPE(Ty) printf(TYPEFMT_rps " %5d %5d\n", #Ty, \
(int)sizeof(Ty), (int)alignof(Ty))
/////
#define EXPLAIN_TYPE2(Ty1,Ty2) printf(TYPEFMT_rps " %5d %5d\n", \
#Ty1 "," #Ty2, \
(int)sizeof(Ty1,Ty2), \
(int)alignof(Ty1,Ty2))
/////
#define EXPLAIN_TYPE3(Ty1,Ty2,Ty3) \
printf(TYPEFMT_rps " %5d %5d\n", \
#Ty1 "," #Ty2 ",\n" \
" "#Ty3, \
(int)sizeof(Ty1,Ty2,Ty3), \
(int)alignof(Ty1,Ty2,Ty3))
/////
#define EXPLAIN_TYPE4(Ty1,Ty2,Ty3,Ty4) \
printf(TYPEFMT_rps " %5d %5d\n", \
#Ty1 "," #Ty2 ",\n " #Ty3 \
"," #Ty4, \
(int)sizeof(Ty1,Ty2,Ty3,Ty4), \
(int)alignof(Ty1,Ty2,Ty3,Ty4))
#define EXPLAIN_TYPE_ABSTRACT(Ty,Siz,Ali) printf(TYPEFMT_rps " %5d %5d\n", #Ty, \
(int)(Siz), (int)(Ali))
/////
EXPLAIN_TYPE(int);
EXPLAIN_TYPE(double);
EXPLAIN_TYPE(char);
EXPLAIN_TYPE(bool);
EXPLAIN_TYPE(void*);
EXPLAIN_TYPE(time_t);
EXPLAIN_TYPE(pid_t);
//opaque in glib: EXPLAIN_TYPE(GMainLoop);
//opaque in glib: EXPLAIN_TYPE(GMainContext);
EXPLAIN_TYPE(std::mutex);
EXPLAIN_TYPE(std::shared_mutex);
EXPLAIN_TYPE(std::recursive_mutex);
EXPLAIN_TYPE(std::atomic<void*>);
EXPLAIN_TYPE(std::lock_guard<std::shared_mutex>);
EXPLAIN_TYPE(std::lock_guard<std::recursive_mutex>);
EXPLAIN_TYPE(std::lock_guard<std::shared_mutex>);
EXPLAIN_TYPE(std::string);
EXPLAIN_TYPE(std::ostream);
EXPLAIN_TYPE(std::ostringstream);
EXPLAIN_TYPE(FILE);
EXPLAIN_TYPE(std::vector<std::string>);
EXPLAIN_TYPE(std::set<std::string>);
EXPLAIN_TYPE2(std::map<Rps_ObjectRef, Rps_Value>);
EXPLAIN_TYPE2(std::unordered_map<std::string, Rps_ObjectRef*>);
printf("########################## %s:%d\n", __FILE__, __LINE__);
EXPLAIN_TYPE3(std::unordered_map<Rps_Id,Rps_ObjectZone*,Rps_Id::Hasher>);
EXPLAIN_TYPE3(std::variant<unsigned, std::function<Rps_Value(void*)>,
std::function<int(void*,Rps_ObjectRef)>>);
EXPLAIN_TYPE(Rps_Backtracer);
EXPLAIN_TYPE(Rps_ClosureValue);
EXPLAIN_TYPE(Rps_ClosureZone);
EXPLAIN_TYPE(Rps_Double);
EXPLAIN_TYPE(Rps_DoubleValue);
EXPLAIN_TYPE(Rps_GarbageCollector);
EXPLAIN_TYPE(Rps_HashInt);
EXPLAIN_TYPE(Rps_Id);
EXPLAIN_TYPE(Rps_ObjectRef);
EXPLAIN_TYPE(Rps_ObjectValue);
EXPLAIN_TYPE(Rps_ObjectZone);
EXPLAIN_TYPE(Rps_Payload);
EXPLAIN_TYPE(Rps_PayloadClassInfo);
EXPLAIN_TYPE(Rps_PayloadSetOb);
EXPLAIN_TYPE(Rps_PayloadVectOb);
EXPLAIN_TYPE(Rps_QuasiZone);
EXPLAIN_TYPE(Rps_SetOb);
EXPLAIN_TYPE(Rps_SetValue);
EXPLAIN_TYPE(Rps_String);
EXPLAIN_TYPE(Rps_StringValue);
EXPLAIN_TYPE(Rps_TupleOb);
EXPLAIN_TYPE(Rps_TupleValue);
EXPLAIN_TYPE(Rps_Type);
EXPLAIN_TYPE(Rps_Value);
EXPLAIN_TYPE(Rps_ZoneValue);
EXPLAIN_TYPE_ABSTRACT(rpscarbrepl_stack,rpscarbrepl_stack_size,rpscarbrepl_stack_align);
////
#if RPS_WITH_FLTK
printf("\n\n===== FLTK widgets from %s:%d ====\n", __FILE__, __LINE__);
EXPLAIN_TYPE(Fl_Box);
#if FLTK_API_VERSION >= 10400
EXPLAIN_TYPE(Fl_Flex);
EXPLAIN_TYPE(Fl_Pack);
#endif
EXPLAIN_TYPE(Fl_Menu_Bar);
EXPLAIN_TYPE(Fl_Text_Buffer);
EXPLAIN_TYPE(Fl_Text_Editor);
EXPLAIN_TYPE(Fl_Widget);
EXPLAIN_TYPE(Fl_Window);
#endif /*RPS_WITH_FLTK*/
////
#undef EXPLAIN_TYPE4
#undef EXPLAIN_TYPE3
#undef EXPLAIN_TYPE
#undef TYPEFMT_rps
putchar('\n');
fflush(nullptr);
std::cout << "@@°°@@ The tagged integer one hundred is "
<< Rps_Value::make_tagged_int(100)
<< std::endl
<< "… and the tagged integer minus one billion is "
<< Rps_Value::make_tagged_int(-1000000000)
<< " !!! " << std::endl;
} // end rps_print_types_info
////////////////////////////////////////////////////////////////
extern "C" void rps_show_version_handwritten_source_files(void);
static void rps_show_version_one_source_file(const char*curfile, int curfilno, char curbase[], char cursuffix[], int& nbshownfiles, bool&nl);
void
rps_show_version_handwritten_source_files(void)
{
RPS_POSSIBLE_BREAKPOINT();
int nbsourcefiles =0;
int nbshownfiles =0;
bool nl= false;
for (const char*const*curfileptr = rps_files;
curfileptr && *curfileptr; curfileptr++)
nbsourcefiles++;
RPS_INFORMOUT("showing versions of " << nbsourcefiles
<< " handwritten source files (git " << rps_utilities_shortgitid
<< ")");
RPS_DEBUG_LOG(PROGARG, "starting " << std::endl
<< RPS_FULL_BACKTRACE_HERE(1, "rps_show_version_handwritten_source_files/start"));
//// show gitid and date of individual handwritten *cc files, using dlsym
//// since every file like utilities_rps.cc has rps_utilities_gitid and rps_utilities_date
int curfilno=0;
for (const char*const*curfileptr = rps_files;
curfileptr && *curfileptr; curfileptr++)
{
char curbase[64];
memset (curbase, 0, sizeof(curbase));
int endpos = -1;
const char*curfile = *curfileptr;
if (!curfile)
break;
RPS_POSSIBLE_BREAKPOINT();
curfilno++;
char cursuffix[16];
memset (cursuffix, 0, sizeof(cursuffix));
RPS_DEBUG_LOG(PROGARG, "curfile#" << curfilno << " =" << Rps_Cjson_String(curfile));
if (!isalpha(curfile[0]))
continue;
if (strchr(curfile, '/'))
continue;
if ((sscanf(curfile, "%60[a-zA-Z_].%10[a-z]%n", curbase, cursuffix, &endpos))<1
|| endpos<2 || curfile[endpos]!=(char)0)
continue;
rps_show_version_one_source_file(curfile, curfilno, curbase, cursuffix, nbshownfiles, nl);
}; // end major loop of rps_show_version_handwritten_source_files
////
////
if (!nl)
std::cout << std::endl;
} // end rps_show_version_handwritten_source_files
void
rps_show_version_one_source_file(const char*curfile, int curfilno, char curbase[], char cursuffix[], int &nbshownfiles, bool&nl)
{
RPS_DEBUG_LOG(PROGARG, "curfile#" << curfilno << " =" << Rps_Cjson_String(curfile)
<< " curbase=" << Rps_Cjson_String(curbase));
int lencurbase=strlen(curbase);
/// Human written source files (not scripts) are *_rps.* and dont start with underscores.
if (curbase[0]=='_' || lencurbase<6)
{
/// by convention basenames starting with an underscore are generated
RPS_DEBUG_LOG(PROGARG, "curfile#" << curfilno<< " =" << Rps_Cjson_String(curfile)
<< " skipping curbase=" << Rps_Cjson_String(curbase));
return;
}
RPS_POSSIBLE_BREAKPOINT();
// Human written source files are *_rps.* (except for refperys.hh)
if (!strcmp(curbase+lencurbase-4, "_rps"))
{
curbase[lencurbase-4]=(char)0;
RPS_DEBUG_LOG(PROGARG, "curfile#" << curfilno << " =" << Rps_Cjson_String(curfile)
<< " shrinked curbase=" << Rps_Cjson_String(curbase));
RPS_POSSIBLE_BREAKPOINT();
}
RPS_DEBUG_LOG(PROGARG, "curfile#" << curfilno << " =" << Rps_Cjson_String(curfile)
<< " curbase=" << Rps_Cjson_String(curbase)
<< " testing cursuffix=" << Rps_Cjson_String(cursuffix));
if (!strcmp(cursuffix, "so") || !strcmp(cursuffix, "o") || !strcmp(cursuffix, "a")
|| !strcmp(cursuffix, "la") || !strcmp(cursuffix, "status"))
{
RPS_POSSIBLE_BREAKPOINT();
RPS_DEBUG_LOG(PROGARG, "curfile=" << Rps_Cjson_String(curfile)
<< " skipped cursuffix=" << Rps_Cjson_String(cursuffix));
return;
};
RPS_POSSIBLE_BREAKPOINT();
////
RPS_DEBUG_LOG(PROGARG, "before µdlsyming curfile=" << Rps_Cjson_String(curfile)
<< " curbase=" << Rps_Cjson_String(curbase));
const char* symgit = nullptr;
const char* symdat = nullptr;
const char* symshortgit = nullptr;
{
char cursymgit[80];
char cursymdat[80];
char cursymshortgit[80];
memset (cursymgit, 0, sizeof(cursymgit));
memset (cursymdat, 0, sizeof(cursymdat));
memset (cursymshortgit, 0, sizeof(cursymshortgit));
snprintf (cursymgit, sizeof(cursymgit), "rps_%s_gitid", curbase);
snprintf (cursymshortgit, sizeof(cursymgit), "rps_%s_shortgitid", curbase);
snprintf (cursymdat, sizeof(cursymdat), "rps_%s_date", curbase);
RPS_DEBUG_LOG(PROGARG, "before µdlsym cursymgit=" << cursymgit);
symgit = (const char*)dlsym(rps_proghdl, cursymgit);
if (!symgit)
{
RPS_WARNOUT("µdlsym cursymgit=" << cursymgit << " failed "
<< dlerror());
return;
}
RPS_DEBUG_LOG(PROGARG, "µdlsym cursymgit=" << cursymgit
<< " gives symgit=" << Rps_Cjson_String(symgit));
if (!symgit || !isalnum(symgit[0]))
{
return;
}
symshortgit = (const char*)dlsym(rps_proghdl, cursymshortgit);
if (!symshortgit || !isalnum(symshortgit[0]))
return;
symdat = (const char*)dlsym(rps_proghdl, cursymdat);
if (!symdat || !isalnum(symdat[0]))
return;
if (symgit && symshortgit
&& strncmp(symgit, symshortgit, sizeof(rps_utilities_shortgitid)-2))
{
/// this should not happen and is likely a bug in C++ files or build procedure
RPS_POSSIBLE_BREAKPOINT();
RPS_WARNOUT("perhaps corrupted " << curfile << " in topdir " << rps_topdirectory
<< " with " << cursymgit << "=" << symgit
<< " and " << cursymshortgit << "=" << symshortgit);
}
};
if (symgit && isalnum(symgit[0]) && symdat && isalnum(symdat[0]))
{
char msgbuf[80];
memset (msgbuf, 0, sizeof(msgbuf));
nbshownfiles++;
if (snprintf(msgbuf, sizeof(msgbuf)-1, "%-16s git %.12s built %s",
curfile, symgit, symdat)>0)
std::cout << "#" << msgbuf << std::flush;
if (nbshownfiles % 2 == 0)
{
std::cout << std::endl;
nl= true;
};
};
} // end rps_show_version_one_source_file
void
rps_show_version(void)
{
int nbfiles=0;
int nbsubdirs=0;
for (const char*const*pfiles=rps_files; *pfiles; pfiles++)
nbfiles++;
for (auto psubdirs=rps_subdirectories; *psubdirs; psubdirs++)
nbsubdirs++;
char exepath[256];
memset (exepath, 0, sizeof(exepath));
static char realexepath[PATH_MAX];
memset (realexepath, 0, sizeof(realexepath));
{
ssize_t sz = readlink("/proc/self/exe", exepath, sizeof(exepath));
RPS_ASSERT(sz>0 && exepath[0]);
}
{
char*rp= realpath(exepath, realexepath);
RPS_ASSERT(rp != nullptr);
}
std::cout << "RefPerSys "<< rps_get_major_version() << "."
<< rps_get_minor_version() //
<< ", an open source Artificial Intelligence system" << std::endl;
std::cout << "\t symbolic inference engine - work in progress..." << std::endl;
std::cout << "version information:\n"
<< " major version: " << RPS_MAJOR_VERSION_NUM << std::endl
<< " minor version: " << RPS_MINOR_VERSION_NUM << std::endl
<< " program name: " << rps_progname << std::endl
<< " build time: " << rps_timestamp << std::endl
<< " top directory: " << rps_topdirectory << std::endl
<< " gitid: " << rps_gitid << std::endl
<< " short-gitid: " << rps_shortgitid << std::endl
<< " gitbranch: " << rps_gitbranch << std::endl
<< " last git tag: " << rps_lastgittag << std::endl
<< " last git commit: " << rps_lastgitcommit << std::endl
<< " md5sum of " << nbfiles << " source files: " << rps_md5sum << std::endl
<< " with " << nbsubdirs << " subdirectories." << std::endl
<< " GNU glibc: " << gnu_get_libc_version() << std::endl
<< " Glib: " << glib_major_version
<< "." << glib_minor_version
<< "." << glib_micro_version << std::endl
<< " executable: " << exepath;
if (strcmp(exepath, realexepath))
std::cout << " really " << realexepath;
std::cout << std::endl;
#if RPS_WITH_FLTK
std::cout << " FLTK (see fltk.org) ABI version:" << rps_fltk_get_abi_version()
<< std::endl;
std::cout << " FLTK API version:" << rps_fltk_get_api_version()
<< std::endl;
#endif
std::cout << " GCCJIT version:" << gcc_jit_version_major()
<< "." << gcc_jit_version_minor() << "." << gcc_jit_version_patchlevel() << std::endl;
std::cout << std::endl
/* TODO: near commit 191d55e1b31c, march 2023; decide
which parser generator to really use... and drop the
other one. Non technical considerations,
e.g. licensing, is important to some partners... */
<< " Gnu multi-precision library version: " << gmp_version
<< std::endl
<< " default GUI script: " << rps_gui_script_executable << std::endl
<< " Read Eval Print Loop: " << rps_repl_version() << std::endl
#if RPS_USE_CURL
<< " libCURL for web client: " << rps_curl_version() << std::endl
#endif /*RPS_USE_CURL*/
;
////
RPS_POSSIBLE_BREAKPOINT();
////
std::cout << " JSONCPP: " << JSONCPP_VERSION_STRING << std::endl
<< " GPP preprocessor command: " << rps_gpp_preprocessor_command << std::endl
<< " GPP preprocessor path: " << rps_gpp_preprocessor_realpath << std::endl
<< " GPP preprocessor version: " << rps_gpp_preprocessor_version << std::endl
<< " made with: " << rps_gnumakefile << std::endl
<< " running on: " << rps_hostname() << std::endl
<< " /proc/version:" << std::endl
<< " " << rps_get_proc_version() << std::endl
<< "This executable was built by "
<< rps_building_user_name
<< " of email " << rps_building_user_email << std::endl
<< "See refpersys.org and code on github.com/RefPerSys/RefPerSys"
<< std::endl;
std::cout << "Compiled by " << rps_cxx_compiler_version << " as " << rps_cxx_compiler_realpath
<< std::endl
<< "with " << rps_cxx_compiler_flags
<< std::endl;
/////
rps_show_version_handwritten_source_files();
/////
{
char cwdbuf[rps_path_byte_size+4];
memset (cwdbuf, 0, sizeof(cwdbuf));
if (getcwd(cwdbuf, rps_path_byte_size))
std::cout << " in: " << cwdbuf;
};
std::cout << std::endl << " C++ compiler: " << rps_cxx_compiler_version << std::endl
<< " free software license: GPLv3+, see https://gnu.org/licenses/gpl.html" << std::endl
<< "+++++ there is no WARRANTY, to the extent permitted by law ++++" << std::endl
<< "***** see also refpersys.org *****" << std::endl
<< "and github.com/RefPerSys/RefPerSys commit "
<< rps_shortgitid
<< std::endl << std::endl;
} // end rps_show_version
/// In a format string passed to strftime, replace .__ with the
/// centisecond fractional part of the time. See of course
/// http://man7.org/linux/man-pages/man3/strftime.3.html etc... Notice
/// that debugging facilities use that function, e.g. it gets called
/// from rps_debug_printf_at used by RPS_DEBUG_LOG and RPS_DEBUG_PRINTF
/// macros.
char *
rps_strftime_centiseconds(char *bfr, size_t len, const char *fmt, double tm)
{
if (!bfr || !fmt || len<4)
return nullptr;
//
memset (bfr, 0, len);
//
struct tm tmstruct;
memset(&tmstruct, 0, sizeof (tmstruct));
//
time_t time = static_cast<time_t>(tm);
strftime(bfr, len, fmt, localtime_r(&time, &tmstruct));
//
char *dotdunder = strstr(bfr, ".__");
if (dotdunder)
{
double intpart = 0.0;
double fraction = modf(tm, &intpart);
char minibuf[16];
memset(minibuf, 0, sizeof (minibuf));
assert(fraction >= 0.0 && fraction < 1.0);
snprintf(minibuf, sizeof (minibuf), "%.02f", fraction);
minibuf[4] = (char)0;
const char* dotminib = strchr(minibuf, '.');
if (dotminib && dotminib<minibuf+sizeof(minibuf)-4)
{
strncpy(dotdunder, dotminib, 3);
}
}
return bfr;
} // end rps_strftime_centiseconds
/// This rps_extend_env is called early from main. It is extending
/// the Unix environment.
/// Try running ./refpersys "--run-after-load=env|grep REFPERSYS" --batch
void
rps_extend_env(void)
{
static std::atomic<bool> extended;
if (extended) return;
RPS_ASSERT(rps_is_main_thread());
extended = true;
static char pidenv[64];
snprintf(pidenv, sizeof(pidenv), "REFPERSYS_PID=%d", (int)getpid());
putenv(pidenv); // e.g. REFPERSYS_PID=2345
static char shortgitenv[64];
snprintf(shortgitenv, sizeof(shortgitenv), "REFPERSYS_SHORTGITID=%s", rps_shortgitid);
putenv(shortgitenv); // e.g. REFPERSYS_SHORTGITID=49466057bf7d+
static char gitenv[128];
snprintf(gitenv, sizeof(gitenv), "REFPERSYS_GITID=%s", rps_gitid);
putenv(gitenv); // e.g. REFPERSYS_GITID=494...90+ with 40 hexdigit
static char topdirenv[384];
snprintf(topdirenv, sizeof(topdirenv), "REFPERSYS_TOPDIR=%s", rps_topdirectory);
putenv(topdirenv); // e.g. REFPERSYS_TOPDIR=$HOME/work/RefPerSys/
if (!rps_fifo_prefix.empty())
{
static char fifoenv[256];
snprintf(fifoenv, sizeof(fifoenv), "REFPERSYS_FIFO_PREFIX=%s", rps_fifo_prefix.c_str());
putenv(fifoenv); // e.g. REFPERSYS_FIFO_PREFIX=$HOME/tmp/rpsfifo
};
if (!rps_run_name.empty())
{
static char runamenv[256];
snprintf(runamenv, sizeof(runamenv), "REFPERSYS_RUN_NAME=%s", rps_run_name.c_str());
putenv(runamenv); // e.g. REFPERSYS_RUN_NAME=testflk3.x
};
} // end rps_extend_env
void
rps_check_mtime_files(void)
{
struct stat selfstat = {};
if (stat("/proc/self/exe", &selfstat))
RPS_FATAL("stat /proc/self/exe: %m");
char exebuf[128];
memset (exebuf, 0, sizeof(exebuf));
if (readlink("/proc/self/exe", exebuf, sizeof(exebuf)-1)<0)
RPS_FATAL("readlink /proc/self/exe: %m");
for (const char*const*curpath = rps_files; *curpath; curpath++)
{
int lencurpath = strlen(*curpath);
if (lencurpath < 6 || strstr(*curpath, "attic/"))
continue;
std::string curpathstr(*curpath);
/// Files under webroot could be sent to browser, so we don't
/// care about them being newer than executable....
auto wrp = curpathstr.find("webroot/");
if (wrp < curpathstr.size())
continue;
std::string curfullpathstr= std::string{rps_topdirectory} + "/" + curpathstr;
struct stat curstat = {};
if (stat(curfullpathstr.c_str(), &curstat))
{
RPS_WARNOUT("rps_check_mtime_files: stat " << curfullpathstr << " failed: " << strerror(errno));
continue;
};
if (curstat.st_mtime > (time_t) rps_timelong)
RPS_WARNOUT("rps_check_mtime_files: " << curfullpathstr.c_str()
<< " is younger by "
<< (curstat.st_mtime - (time_t) rps_timelong)
<< " seconds than current executable " << exebuf
<< ", so consider rebuilding with make");
}
//// run a make -t command to check that objects are up to date
{
char tempmakefileout[128];
memset (tempmakefileout, 0, sizeof(tempmakefileout));
snprintf(tempmakefileout, sizeof(tempmakefileout),
"/var/tmp/rpsmkchkmtim-%s-r%u-p%u",
rps_shortgitid,
(unsigned) Rps_Random::random_32u(),
(unsigned) getpid());
RPS_ASSERT(strlen(tempmakefileout) < sizeof(tempmakefileout)-4);
{
FILE* ftemp = fopen(tempmakefileout, "w");
if (!ftemp)
RPS_FATALOUT("failed to open temporary make output " << tempmakefileout);
fprintf(ftemp, "# postponed temporary make output %s for...\n"
"#... refpersys run %s from %s:%d\n",
tempmakefileout, rps_run_name.c_str(), __FILE__, __LINE__);
rps_postponed_remove_file(std::string{tempmakefileout});
{
char cwdbuf[256];
memset (cwdbuf, 0, sizeof(cwdbuf));
char*pwd = getcwd(cwdbuf, sizeof(cwdbuf));
if (pwd)
fprintf (ftemp, "# running in %s\n", pwd);
}
char makecmd [256];
memset (makecmd, 0, sizeof(makecmd));
if (snprintf(makecmd, sizeof(makecmd),
"%s -C %s -q objects 2>&1 >> %s",
rps_gnu_make, rps_topdirectory,
tempmakefileout)
< (int)sizeof(makecmd)-1)
{
int bad = system(makecmd);
if (bad)
RPS_WARNOUT("rps_check_mtime_files: " << makecmd
<< " failed with status# " << bad);
else
RPS_INFORMOUT("rps_check_mtime_files: did " << std::string(makecmd) << " successfully");
}
else // makecmd too big
RPS_FATAL("rps_check_mtime_files failed to construct makecmd in %s: %m",
rps_topdirectory);
fprintf (ftemp, "successful %s\n", makecmd);
fprintf (ftemp, "#end of %s from %s:%d (%s)\n",
tempmakefileout, __FILE__, __LINE__, __FUNCTION__);
fclose (ftemp);
}
} // end running make -t command
} // end rps_check_mtime_files
////////////////////////////////////////////////////////////////
static double rps_start_monotonic_time;
static double rps_start_wallclock_real_time;
/// rps_early_initialization is called by rps_parse_program_arguments which is called early from main.
static void