forked from RefPerSys/RefPerSys
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrefpersys.hh
More file actions
6058 lines (5490 loc) · 221 KB
/
refpersys.hh
File metadata and controls
6058 lines (5490 loc) · 221 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 refpersys.hh
* SPDX-License-Identifier: GPL-3.0-or-later
*
* Description:
* This file is part of the Reflective Persistent System.
* It is almost its only public C++ header file.
*
* Author(s):
* Basile Starynkevitch, France <basile@starynkevitch.net>
* Abhishek Chakravarti, India <abhishek@taranjali.org>
* Nimesh Neema, India <nimeshneema@gmail.com>
*
* © Copyright (C) 2019 - 2025 The Reflective Persistent System Team
* team@refpersys.org & http://refpersys.org/
*
* You can consider RefPerSys as either GPLv3+ or LGPLv3+ licensed (at
* your choice)
*
* License: GPLv3+ (file COPYING-GPLv3)
* This software 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.
*
* Alternative license: LGPLv3+ (file COPYING-LGPLv3)
* This software is is free software: you can
* redistribute it and/or modify it under the terms of the GNU
* Lesser 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 or the Lesser
* General Public License.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
#ifndef REFPERSYS_INCLUDED
#define REFPERSYS_INCLUDED
// A name containing `unsafe` refers to something which should be used
// with great caution.
#ifndef _GNU_SOURCE
#define _GNU_SOURCE 1
#endif /*_GNU_SOURCE*/
#if __cplusplus < 201412L
#error expecting C++17 standard
#endif
#include <set>
#include <map>
#include <deque>
#include <variant>
#include <unordered_map>
#include <unordered_set>
#include <new>
#include <random>
#include <istream>
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <limits>
#include <initializer_list>
#include <algorithm>
#include <mutex>
#include <shared_mutex>
#include <thread>
#include <condition_variable>
#include <atomic>
#include <stdexcept>
#include <functional>
#include <typeinfo>
#include <locale>
#include <cassert>
#include <cstring>
#include <cmath>
#include <cstdio>
#include <cstdarg>
#include <clocale>
#include <filesystem>
///
#include <argp.h>
#include <ctype.h>
#include <sys/mman.h>
#include <sys/resource.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/syscall.h>
#include <sys/syslog.h>
#include <sys/utsname.h>
#include <unistd.h>
#include <signal.h>
#include <errno.h>
#include <time.h>
#include <setjmp.h>
#include <dlfcn.h>
#include <dirent.h>
#include <pthread.h>
#include <limits.h>
#include <locale.h>
#include <libintl.h> //// gettext(3) and friends
#include <stdlib.h>
#include <sys/poll.h>
#include <sys/personality.h>
#include <sys/signalfd.h>
#include <sys/timerfd.h>
#include <wordexp.h>
#include <glob.h>
/// libssh2-1-dev package on Debian
#include <libssh2.h>
/// libgmp-dev package on Debian (GNU multiprecision library, and its
/// C++ wrapper)
#include <gmpxx.h>
#define RPS_WITH_FLTK 1 /* could be 1 if using fltk.org graphical toolkit */
//// the generated/rpsdata.h contain only preprocessor #define-s and #undef
//// it may undef RPS_WITH_FLTK. It has a pragma message
//// it is simpler to not use it... (but needed in some files)
#ifdef RPS_WITH_DATA
#include "generated/rpsdata.h"
#endif //RPS_WITH_DATA
extern "C" const char* rps_locale(void);
/// keep the debug options in alphabetical order
#define RPS_DEBUG_OPTIONS(dbgmacro) \
dbgmacro(CMD) \
dbgmacro(CODEGEN) \
dbgmacro(COMPL_REPL) \
dbgmacro(DUMP) \
dbgmacro(EVENT_LOOP) \
dbgmacro(GARBAGE_COLLECTOR) \
dbgmacro(GUI) \
dbgmacro(LOAD) \
dbgmacro(LOWREP) \
dbgmacro(LOW_REPL) \
dbgmacro(MISC) \
dbgmacro(MSGSEND) \
dbgmacro(PARSE) \
dbgmacro(PARSE_STRING) \
dbgmacro(PROGARG) \
dbgmacro(REPL) \
/*end RPS_DEBUG_OPTIONS*/
#define RPS_DEBUG_OPTION_DEFINE(dbgopt) RPS_DEBUG_##dbgopt,
/// passed to rps_fltk_show_debug_message as the debug level for
/// warning, inform, fatal
constexpr int RPS_INFORM_MSG_LEVEL= -1;
constexpr int RPS_WARNING_MSG_LEVEL= -2;
constexpr int RPS_FATAL_MSG_LEVEL= -3;
enum Rps_Debug
{
RPS_DEBUG__FATAL_MSG_LEVEL= RPS_FATAL_MSG_LEVEL /*ie -3*/,
RPS_DEBUG__WARNING_MSG_LEVEL= RPS_WARNING_MSG_LEVEL /*ie -2*/,
RPS_DEBUG__INFORM_MSG_LEVEL= RPS_INFORM_MSG_LEVEL /*ie -1*/,
RPS_DEBUG__NONE= 0,
// expands to RPS_DEBUG_CMD, RPS_DEBUG_DUMP, RPS_DEBUG_GARBAGE_COLLECTOR...
RPS_DEBUG_OPTIONS(RPS_DEBUG_OPTION_DEFINE)
RPS_DEBUG__LAST,
RPS_DEBUG__EVERYTHING=0xffff,
};
// forward declaration
class Rps_ProtoCallFrame;
typedef Rps_ProtoCallFrame Rps_CallFrame;
typedef void Rps_EventHandler_sigt(Rps_CallFrame*, int /*fd*/, void* /*data*/);
#if RPS_WITH_FLTK
extern "C" int rps_fltk_get_abi_version (void);
extern "C" int rps_fltk_get_api_version (void);
extern "C" void rps_fltk_initialize (int argc, char**argv);
extern "C" void rps_fltk_progoption(char*arg, struct argp_state*, bool side_effect);
extern "C" bool rps_fltk_enabled (void);
extern "C" void rps_fltk_run (void);
extern "C" void rps_fltk_stop (void);
extern "C" void rps_fltk_flush (void);
extern "C" void rps_fltk_show_debug_message(const char*file, int line, const char*funcname,
Rps_Debug dbgopt, long dbgcount,
const char*msg);
extern "C" void rps_fltk_printf_inform_message(const char*file, int line, const char*funcname, long dbgcount,
const char*fmt, ...)
__attribute__ ((format (printf, 5, 6)));
/* add an input file descriptor event handler to FLTK event loop */
extern "C" void rps_fltk_add_input_fd(int fd,
Rps_EventHandler_sigt* f,
const char* explanation,
int ix);
/* add an output file descriptor event handler to FLTK event loop */
extern "C" void rps_fltk_add_output_fd(int fd,
Rps_EventHandler_sigt* f,
const char* explanation,
int ix);
/* remove an input file descriptor event handler from FLTK event loop */
extern "C" void rps_fltk_remove_input_fd(int fd);
/* remove an output file descriptor event handler from FLTK event loop */
extern "C" void rps_fltk_remove_output_fd(int fd);
/* emit the size and align */
extern "C" void rps_fltk_emit_sizes(std::ostream&out);
#else /*not RPS_WITH_FLTK*/
#define rps_fltk_get_abi_version() 0
#define rps_fltk_get_api_version() 0
#define rps_fltk_initialize() do {}while(0)
#define rps_fltk_progoption(Arg,State,SidEff) do {}while(0)
#define rps_fltk_enabled() false
#define rps_fltk_add_input_fd(Fd,Fun,Expl,Ix) do {}while(0)
#define rps_fltk_add_output_fd(Fd,Fun,Expl,Ix) do {}while(0)
#define rps_fltk_remove_input_fd(Fd) do{}while(0)
#define rps_fltk_remove_output_fd(Fd) do{}while(0)
#define rps_fltk_stop() do{}while(0)
#define rps_fltk_run() do{}while(0)
#define rps_fltk_flush() do{}while(0)
#define rps_fltk_emit_sizes(Out) do{}while(0)
#define rps_fltk_printf_inform_message(File,Lin,Funcname,Count,Fmt,...) do{}while(0)
#endif
class Rps_QuasiZone; // GC-managed piece of memory
class Rps_ZoneValue; // memory for values
class Rps_ObjectZone; // memory for objects
class Rps_JsonZone; // memory for Json values
class Rps_LexTokenZone; /// memory for reified lexical tokens, mostly in repl_rps.cc
class Rps_DequVal;
class Rps_GarbageCollector;
class Rps_Payload;
class Rps_PayloadSymbol;
class Rps_PayloadClassInfo;
class Rps_PayloadStrBuf;
class Rps_PayloadWebPi;
class Rps_PayloadAgenda;
class Rps_PayloadTasklet;
class Rps_PayloadUnixProcess; // transient payload for forked processes
class Rps_PayloadPopenedFile; // transient payload for popened command
class Rps_PayloadCppStream; // transient payload for C++ streams
class Rps_PayloadGccJit; // payload for libgccjit
// code generation
#if RPS_WITH_FLTK
class Rps_PayloadFltkThing;
class Rps_PayloadFltkWidget;
class Rps_PayloadFltkWindow;
//TODO: add perhaps Rps_PayloadFltkWindow?
#endif
class Rps_Loader;
class Rps_Dumper;
class Rps_ProtoCallFrame;
class Rps_TokenSource;
class Rps_Value;
class Rps_Id;
typedef uint32_t Rps_HashInt;
typedef Rps_ProtoCallFrame Rps_CallFrame;
constexpr unsigned rps_path_byte_size = 384;
extern "C" char rps_bufpath_homedir[rps_path_byte_size];
extern "C" char rps_loaded_directory[rps_path_byte_size];
extern "C" char rps_debug_path[rps_path_byte_size];
extern "C" int rps_get_major_version(void);
extern "C" int rps_get_minor_version(void);
#define RPS_MAJOR_VERSION_NUM 0
#define RPS_MINOR_VERSION_NUM 6
extern "C" std::map<std::string,std::string> rps_pluginargs_map;
extern "C" std::string rps_cpluspluseditor_str;
extern "C" std::string rps_cplusplusflags_str;
extern "C" std::string rps_dumpdir_str;
extern "C" std::vector<std::string> rps_command_vec;
extern "C" std::string rps_test_repl_string;
extern "C" std::string rps_publisher_url_str;
extern "C" bool rps_without_quick_tests;
/// Given some SHORTPATH like "foo123.xyz" return a temporary unique
/// full path in the dump directory which would be renamed at end of
/// dump to the SHORTPATH...
extern "C" std::string rps_dumper_temporary_path(Rps_Dumper*du, std::string shortpath);
extern "C" char* rps_run_command_after_load;
extern "C" char* rps_debugflags_after_load;
extern "C" const char* rps_get_proc_version(void);
extern "C" std::string rps_run_name;
extern "C" std::string rps_stringprintf(const char*fmt, ...)
__attribute__((format (printf, 1, 2))); // in utilities_rps.cc
extern "C" {
#define RPS_USE_CURL 0 // temporary, see https://bugs.gentoo.org/939581 Invalid conversion from int to CURLoption
#if RPS_USE_CURL
// https://curl.se/libcurl/ is a web client library
#include "curl/curl.h"
#endif /*RPS_USE_CURL*/
// before November 2024 we used GNU lightning from https://www.gnu.org/software/lightning
};
// http://man7.org/linux/man-pages/man3/gnu_get_libc_version.3.html
#include <gnu/libc-version.h>
// for programmatic C++ name demangling, see also
// https://github.com/gcc-mirror/gcc/blob/master/libstdc%2B%2B-v3/libsupc%2B%2B/cxxabi.h
#include <cxxabi.h>
// comment for our do-scan-refpersys-pkgconfig.c utility
//@@PKGCONFIG jsoncpp
// JsonCPP https://github.com/open-source-parsers/jsoncpp
#include "json/json.h"
// GNU libunistring https://www.gnu.org/software/libunistring/
// we use UTF-8 strings
#include "unistr.h"
#include "backtrace.h"
// mark unlikely conditions to help optimization
#ifdef __GNUC__
#define RPS_UNLIKELY(P) __builtin_expect(!!(P),0)
#define RPS_LIKELY(P) !__builtin_expect(!(P),0)
#define RPS_UNUSED __attribute__((unused))
#else
#define RPS_UNLIKELY(P) (P)
#define RPS_LIKELY(P) (P)
#define RPS_UNUSED
#endif
#define RPS_FRIEND_CLASS(Suffix) friend class Rps_##Suffix
// generated in __timestamp.c by rps-generate-timestamp.sh utility script
extern "C" const char rps_timestamp[];
extern "C" unsigned long rps_timelong;
extern "C" const char rps_topdirectory[];
extern "C" const char rps_gitid[];
extern "C" const char rps_shortgitid[];
extern "C" const char rps_gitbranch[];
extern "C" const char rps_lastgittag[];
extern "C" const char rps_lastgitcommit[];
extern "C" const char rps_md5sum[];
extern "C" const char*const rps_files[];
/// the GNU make utility see www.gnu.org/software/make/
extern "C" const char rps_gnumakefile[];
extern "C" const char rps_gnu_make[];
extern "C" const char rps_gnu_make_version[];
extern "C" const char rps_gnu_make_features[];
/// the GNU bison parser generator see www.gnu.org/software/bison/
extern "C" const char rps_gnu_bison[];
extern "C" const char rps_gnu_bison_version[];
/// the Carburetta parser generator see carburetta.com & github.com/kingletbv/carburetta
extern "C" const char rps_carburetta[];
extern "C" const char rps_carburetta_version[];
/// subdirectories
extern "C" const char*const rps_subdirectories[];
/// realpath and version and flags of the GNU C++ compiler see gcc.gnu.org
extern "C" const char rps_cxx_compiler_realpath[];
extern "C" const char rps_cxx_compiler_version[];
extern "C" const char rps_cxx_compiler_flags[];
/// the GPP generic preprocessor from logological.org/gpp
/// we may in 2025 not need to use it
extern "C" const char rps_gpp_preprocessor_command[];
extern "C" const char rps_gpp_preprocessor_realpath[];
extern "C" const char rps_gpp_preprocessor_version[];
extern "C" const char rps_plugin_builder[];
/// In commit 92c6e6b70d2 of Feb, 8, 2024 we used to mention GNU bison and GPP
/// GNU bison is a parser generator, see www.gnu.org/software/bison/
/// GPP is a general purpose preprocessor, see logological.org/gpp
/// we did had some constants in __timestamp.c related to them.
///// a process running the GUI communicating using JSONRPC
extern "C" const char rps_gui_script_executable[];
// the Linux user compiling RefPerSys
extern "C" const char rps_building_user_name[];
extern "C" const char rps_building_user_email[];
/// the computer on which this RefPerSys is built
extern "C" const char rps_building_host[];
extern "C" const char rps_building_operating_system[];
extern "C" const char rps_building_opersysname[]; /// with only letters, digits, underscores
extern "C" const char rps_building_machine[];
extern "C" const char rps_building_machname[]; /// with only letters, digits, underscores
/// current utsname
extern "C" struct utsname rps_utsname;
////////////////////////////////////////////////////////////////
//// from RefPerSys program arguments
extern "C" bool rps_daemonized; /// --daemon option
extern "C" bool rps_syslog_enabled; /// --syslog option
///////////////////////////////////////////////////////////////////////////////
/// Provides miscellaneous runtime information for RefPerSys.
///
extern "C" pid_t rps_gui_pid;
/// a pair of Unix file descriptors, JSONRPC....
struct rps_fifo_fdpair_st
{
int fifo_ui_wcmd; // the commands written to the GUI process
int fifo_ui_rout; // the outputs read from the GUI process
};
extern "C" void rps_put_fifo_prefix(const char*pref);
//// postpone, by popening /bin/at, the remove of a file 5 minutes
//// after RefPerSys normally exited.
extern "C" void rps_postponed_remove_file(const std::string& path);
/// this is scheduling their removal, called by atexit
extern "C" void rps_schedule_files_postponed_removal(void);
extern "C" std::string rps_get_fifo_prefix(void);
extern "C" void rps_do_create_fifos(std::string prefix);
extern "C" struct rps_fifo_fdpair_st rps_get_gui_fifo_fds(void);
extern "C" pid_t rps_get_gui_pid(void);
extern "C" bool rps_is_fifo(std::string path); // in eventloop_rps.cc
extern "C" void rps_parse_program_arguments(int& argc, char**argv);
extern "C" void rps_run_test_repl_lexer(const std::string&); // defined in file lexer_rps.cc
/// actually, in function main we have something like asm volatile ("rps_end_of_main: nop");
extern "C" void rps_end_of_main(void);
extern "C" void rps_edit_run_cplusplus_code (Rps_CallFrame*callerframe);
extern "C" void rps_small_quick_tests_after_load (void);
extern "C" void rps_check_mtime_files (void);
// when set, no GUI is running
extern "C" bool rps_batch;
/// https://en.wikipedia.org/wiki/Address_space_layout_randomization
extern "C" bool rps_disable_aslr;
extern "C" bool rps_run_repl;
extern "C" void rps_jsonrpc_initialize(void);
extern "C" void rps_gccjit_initialize(void);
extern "C" void rps_gccjit_finalize(void); // passed to atexit(3)
/// Our event loop can call C++ closures before the poll(2) system
/// call in the event loop. This C++ closure (or std::function) could
/// add additional sources to the event loop. This
/// rps_register_event_loop_prepoller function returns some index for
/// the unregistering function.
extern "C" int rps_register_event_loop_prepoller(std::function<void (struct pollfd*, int& npoll, Rps_CallFrame*)> fun);
extern "C" void rps_unregister_event_loop_prepoller(int rank);
extern "C" bool rps_is_active_event_loop(void);
/// register C function input handler
extern "C" void rps_event_loop_add_input_fd_handler
(int fd,
Rps_EventHandler_sigt*cfun,
const char* explanation = nullptr,
void*data = nullptr);
extern "C" void rps_event_loop_add_output_fd_handler
(int fd,
Rps_EventHandler_sigt*cfun,
const char* explanation = nullptr,
void*data = nullptr);
extern "C" void rps_event_loop_remove_input_fd_handler(int fd);
extern "C" void rps_event_loop_remove_output_fd_handler(int fd);
extern "C" void rps_initialize_event_loop(void);
extern "C" void rps_event_loop(void); // run the event loop
extern "C" void rps_run_after_event_loop(void); // run after some event loop
extern "C" void rps_register_after_event_loop(std::function<void(void)>f); // run after some event loop
extern "C" void rps_do_stop_event_loop(void);
// in eventloop_rps.cc, tell if the event loop is running.
extern "C" bool rps_event_loop_is_running(void);
/* return true, and fill the information, about entry#ix in event loop
internal data, or else return false */
extern "C" bool rps_event_loop_get_entry(int ix,
Rps_EventHandler_sigt** pfun, struct pollfd*po, const char**pexpl, void**pdata);
// in eventloop_rps.cc, give the counter for the loop, or -1 if it is
// not running.
extern "C" long rps_event_loop_counter(void);
/// backtrace support
extern "C" struct backtrace_state* rps_backtrace_common_state;
/// the program name
extern "C" const char* rps_progname;
/// the program executable (readlink /proc/self/exe)
extern "C" char rps_progexe[];
/// the program arguments
extern "C" int rps_argc;
extern "C" char** rps_argv;
/// the program invocation
extern "C" char* rps_program_invocation;
extern "C" void rps_compute_program_invocation(void);
/// the load directory
extern "C" std::string rps_my_load_dir;
/// the initial copy-right year of RefPerSys
#define RPS_INITIAL_COPYRIGHT_YEAR 2019
// the number of jobs, that is of threads, to run in parallel
extern "C" int rps_nbjobs;
#define RPS_NBJOBS_MIN 3
#define RPS_NBJOBS_MAX 24
extern "C" bool rps_stdout_istty;
extern "C" bool rps_stderr_istty;
/// is the current thread the main thread?
extern "C" bool rps_is_main_thread(void);
/// the refpersys homedir, e.g. $REFPERSYS_HOME or $HOME or given with
/// --refpersys-home <dir>
extern "C" const char* rps_homedir(void);
/// the refpersys load directory
extern "C" const std::string& rps_get_loaddir(void);
extern "C" 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="");
#define rps_emit_gplv3_copyright_notice(Out,...) rps_emit_gplv3_copyright_notice_AT(Out,__FILE__,__LINE__,__PRETTY_FUNCTION__,##__VA_ARGS__)
extern "C" 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="");
#define rps_emit_lgplv3_copyright_notice(Out,...) rps_emit_gplv3_copyright_notice_AT(Out,__FILE__,__LINE__,__PRETTY_FUNCTION__,##__VA_ARGS__)
extern "C" FILE*rps_debug_file;
//////////////// fatal error - aborting
extern "C" void rps_fatal_stop_at (const char *, int) __attribute__((noreturn));
#define RPS_FATAL_AT_BIS(Fil,Lin,Fmt,...) do { \
if (rps_syslog_enabled) { \
syslog(LOG_ALERT, "*+* RefPerSys FATAL:%s:%d: {%s} " Fmt, \
Fil, Lin, __PRETTY_FUNCTION__, \
##__VA_ARGS__); \
} else { \
bool ontty = rps_stderr_istty; \
fprintf(stderr, "\n\n" \
"%s*** RefPerSys FATAL:%s%s:%d: {%s}\n " Fmt "\n\n", \
(ontty?RPS_TERMINAL_BOLD_ESCAPE:""), \
(ontty?RPS_TERMINAL_NORMAL_ESCAPE:""), \
Fil, Lin, __PRETTY_FUNCTION__, \
##__VA_ARGS__); \
}; \
if (rps_fltk_enabled()) \
rps_fltk_printf_inform_message(Fil, Lin, __PRETTY_FUNCTION__, \
rps_incremented_debug_counter(), \
"FATAL:" Fmt, ##__VA_ARGS__); \
if (rps_debug_file && rps_debug_file != stderr) \
fprintf(rps_debug_file, \
"\n\n*°* RefPerSys °FATAL° %s:%d:%s " Fmt "*°*\n", \
Fil, Lin, __PRETTY_FUNCTION__, \
##__VA_ARGS__); \
rps_fatal_stop_at (Fil,Lin); } while(0)
#define RPS_FATAL_AT(Fil,Lin,Fmt,...) RPS_FATAL_AT_BIS(Fil,Lin,Fmt,##__VA_ARGS__)
#define RPS_FATAL(Fmt,...) RPS_FATAL_AT(__FILE__,__LINE__,Fmt,##__VA_ARGS__)
#define RPS_FATALOUT_AT_BIS(Fil,Lin,...) do { \
std::ostringstream outl##Lin; \
outl##Lin << __VA_ARGS__ << std::flush; \
outl##Lin.flush(); \
if (rps_syslog_enabled) { \
syslog(LOG_ALERT, \
"*+* RefPerSys FATAL:%s:%d: {%s} %s", \
Fil, Lin, __PRETTY_FUNCTION__, \
outl##Lin.str().c_str()); \
} else { \
bool ontty = rps_stderr_istty; \
fprintf(stderr, "\n\n" \
"%s*** RefPerSys FATAL:%s%s:%d: {%s}\n %s\n\n", \
(ontty?RPS_TERMINAL_BOLD_ESCAPE:""), \
(ontty?RPS_TERMINAL_NORMAL_ESCAPE:""), \
Fil, Lin, __PRETTY_FUNCTION__, \
outl##Lin.str().c_str()); \
}; \
if (rps_debug_file && rps_debug_file != stderr) \
fprintf(rps_debug_file, \
"\n\n*°* RefPerSys °FATAL° %s:%d:%s %s*°*\n", \
Fil, Lin, __PRETTY_FUNCTION__, \
outl##Lin.str().c_str()); \
rps_fatal_stop_at (Fil,Lin); } while(0)
#define RPS_FATALOUT_AT(Fil,Lin,...) RPS_FATALOUT_AT_BIS(Fil,Lin,##__VA_ARGS__)
// typical usage would be RPS_FATALOUT("x=" << x)
#define RPS_FATALOUT(...) RPS_FATALOUT_AT(__FILE__,__LINE__,##__VA_ARGS__)
//////////////// warnings
extern "C" long rps_incremented_debug_counter(void);
extern "C" long rps_debug_counter(void);
extern "C" void rps_debug_warn_at(const char*file, int line);
#define RPS_WARN_AT_BIS(Fil,Lin,Fmt,...) do { \
if (rps_syslog_enabled) { \
syslog(LOG_WARNING, "RefPerSys WARN:%s:%d: {%s} " Fmt, \
Fil, Lin, __PRETTY_FUNCTION__, ##__VA_ARGS__); \
} else { \
bool ontty##Lin = rps_stderr_istty; \
fprintf(stderr, "\n\n" \
"%s*** RefPerSys WARN:%s%s:%d: {%s}\n " Fmt "\n\n", \
ontty##Lin?RPS_TERMINAL_BOLD_ESCAPE:"", \
ontty##Lin?RPS_TERMINAL_NORMAL_ESCAPE:"", \
Fil, Lin, __PRETTY_FUNCTION__, ##__VA_ARGS__); \
rps_debug_warn_at(Fil,Lin); \
fflush(stderr); } } while(0)
#define RPS_WARN_AT(Fil,Lin,Fmt,...) RPS_WARN_AT_BIS(Fil,Lin,Fmt,##__VA_ARGS__)
// typical usage could be RPS_WARN("something bad x=%d", x)
#define RPS_WARN(Fmt,...) RPS_WARN_AT(__FILE__,__LINE__,Fmt,##__VA_ARGS__)
#define RPS_WARNOUT_AT_BIS(Fil,Lin,...) do { \
if (rps_syslog_enabled) { \
std::ostringstream outl##Lin; \
outl##Lin << __VA_ARGS__ << std::flush; \
outl##Lin.flush(); \
syslog(LOG_WARNING, "RefPerSys WARN:%s:%d: {%s} %s", \
Fil, Lin, __PRETTY_FUNCTION__, \
outl##Lin.str().c_str()); \
} \
else { \
bool ontty##Lin = rps_stderr_istty; \
std::clog << (ontty##Lin?RPS_TERMINAL_BOLD_ESCAPE:"") \
<< "** RefPerSys WARN!" \
<< (ontty##Lin?RPS_TERMINAL_NORMAL_ESCAPE:"") \
<< " " << (Fil) << ":" << Lin << ":: " \
<< __VA_ARGS__ << std::endl; \
}; \
rps_debug_warn_at(Fil,Lin); } while(0)
#define RPS_WARNOUT_AT(Fil,Lin,...) RPS_WARNOUT_AT_BIS(Fil,Lin,##__VA_ARGS__)
// typical usage would be RPS_WARNOUT("annoying x=" << x)
#define RPS_WARNOUT(...) RPS_WARNOUT_AT(__FILE__,__LINE__,##__VA_ARGS__)
static inline pid_t rps_thread_id(void)
{
return syscall(SYS_gettid, 0L);
} // end rps_thread_id
// see https://en.wikipedia.org/wiki/ANSI_escape_code
extern "C" bool rps_without_terminal_escape;
// adapted from https://github.com/bstarynk
#define RPS_TERMINAL_NORMAL_ESCAPE \
(rps_without_terminal_escape?"":"\033[0m")
#define RPS_TERMINAL_BOLD_ESCAPE \
(rps_without_terminal_escape?"":"\033[1m")
#define RPS_TERMINAL_FAINT_ESCAPE \
(rps_without_terminal_escape?"":"\033[2m")
#define RPS_TERMINAL_ITALICS_ESCAPE \
(rps_without_terminal_escape?"":"\033[3m")
#define RPS_TERMINAL_UNDERLINE_ESCAPE \
(rps_without_terminal_escape?"":"\033[4m")
#define RPS_TERMINAL_BLINK_ESCAPE \
(rps_without_terminal_escape?"":"\033[5m")
extern "C" void rps_set_exit_code(std::uint8_t); // in main_rps.cc
/////////////////////////////////////////////////////// PLUGINS AFTER LOAD
struct Rps_Plugin
{
std::string plugin_name;
void* plugin_dlh;
Rps_Plugin (const char*name, void*dlh)
: plugin_name(name), plugin_dlh(dlh)
{
int plnamlen = plugin_name.length();
if (plnamlen > 4
&& plugin_name.substr(plnamlen-3) == ".so")
plugin_name.erase(plnamlen-3);
};
~Rps_Plugin ()
{
plugin_name.erase();
plugin_dlh = nullptr;
}
};
#define RPS_PLUGIN_INIT_NAME "rps_do_plugin"
typedef void rps_plugin_init_sig_t(const Rps_Plugin*curplugin);
extern "C" rps_plugin_init_sig_t rps_do_plugin;
// if plugin not found or without argument, return the nullptr
extern "C" const char*rps_get_plugin_cstr_argument(const Rps_Plugin*plugin);
extern "C" std::vector<Rps_Plugin> rps_plugins_vector;
////////////////////////////////////////////////////////////////
struct Rps_Status
{
// sizes in megabytes
long prog_sizemb_stat;
long rss_sizemb_stat;
long shared_sizemb_stat;
// time in seconds;
float cputime_stat;
float elapsedtime_stat;
Rps_Status() : prog_sizemb_stat(0), rss_sizemb_stat(0),
shared_sizemb_stat(0), cputime_stat(0.0), elapsedtime_stat(0.0) {};
static const Rps_Status get(void);
void output(std::ostream&out) const;
}; // end struct Rps_Status
inline std::ostream& operator << (std::ostream&out, const Rps_Status& rst)
{
rst.output(out);
return out;
}; // end operator << for Rps_Status
#if RPS_USE_CURL
//// a function to interact with some web service, usually on
//// http://refpersys.org/ to transmit information about this
//// RefPerSys process.. This is related to the --publish-me <URL>
//// program option in main_rps.cc
extern "C" void rps_curl_publish_me(const char*url);
#endif /*RPS_USE_CURL*/
///////////////////////////////////////////////////////////////////////////////
// DEBUGGING MACROS
// Adapted from MELT Monitor project
// https://github.com/bstarynk/melt-monitor/blob/master/meltmoni.hh#L278
///////////////////////////////////////////////////////////////////////////////
extern "C" bool rps_is_set_debug(const std::string &deblev);
extern "C" Rps_Debug rps_debug_of_string(const std::string &deblev);
extern "C" const char*rps_cstr_of_debug(Rps_Debug); /* gives null for
non-debug options */
// output a set of debug flags, or rps_debug_flags if flag is zero...
extern "C" void rps_output_debug_flags(std::ostream&out, unsigned flags=0);
/// add or remove a comma or space separated list of debug flags
extern "C" void rps_add_debug_cstr(const char*);
extern "C" void rps_remove_debug_cstr(const char*);
////////////////////////////////////////////////////////////////
///// parsing program options
enum rps_progoption_en
{
RPSPROGOPT__NONE=0,
/// keep these options in numerical order!
RPSPROGOPT_DEBUG_AFTER_LOAD='A',
RPSPROGOPT_BATCH='B',
RPSPROGOPT_DUMP='D',
RPSPROGOPT_FLTK='F',
RPSPROGOPT_JSONRPC='J', // no direct GUI, but use JSONRPC
RPSPROGOPT_LOADDIR='L',
RPSPROGOPT_COMMAND='c',
RPSPROGOPT_DEBUG='d',
RPSPROGOPT_INTERFACEFIFO='i',
/// see also github.com/bstarynk/misc-basile/blob/master/mini-edit-JSONRPC.md
RPSPROGOPT_JOBS='j',
RPSPROGOPT_USER_PREFERENCES='U',
RPSPROGOPT_HOMEDIR=1000,
RPSPROGOPT_CHDIR_BEFORE_LOAD,
RPSPROGOPT_CHDIR_AFTER_LOAD,
RPSPROGOPT_RANDOMOID,
RPSPROGOPT_TYPEINFO,
RPSPROGOPT_SYSLOG,
RPSPROGOPT_DAEMON,
RPSPROGOPT_FULL_GIT,
RPSPROGOPT_SHORT_GIT,
RPSPROGOPT_PID_FILE,
RPSPROGOPT_NO_TERMINAL,
RPSPROGOPT_NO_ASLR,
RPSPROGOPT_NO_QUICK_TESTS,
RPSPROGOPT_TEST_REPL_LEXER,
RPSPROGOPT_RUN_DELAY,
RPSPROGOPT_RUN_AFTER_LOAD,
RPSPROGOPT_PLUGIN_AFTER_LOAD,
RPSPROGOPT_PLUGIN_ARG,
RPSPROGOPT_CPLUSPLUSEDITOR_AFTER_LOAD,
RPSPROGOPT_CPLUSPLUSFLAGS_AFTER_LOAD,
RPSPROGOPT_DEBUG_PATH,
RPSPROGOPT_EXTRA_ARG,
RPSPROGOPT_RUN_NAME,
RPSPROGOPT_ECHO,
RPSPROGOPT_VERSION,
RPSPROGOPT_PREFERENCES_HELP,
RPSPROGOPT_PUBLISH_ME,
};
extern "C" std::string rps_user_preferences_path(void);
extern "C" bool rps_want_user_preferences_help(void);
#define REFPERSYS_DEFAULT_PREFERENCE_PATH ".refpersysrc" /*in $HOME*/
/// if state is RPS_EMPTYSLOT no serious side-effect happens
extern "C" error_t rps_parse1opt (int key, char *arg, struct argp_state *state);
extern "C" struct argp_option rps_progoptions[];
/// The program arguments can contain --extra NAME=VALUE
/// if the NAME is unknown return nullptr...
extern "C" const char*rps_get_extra_arg(const char*name);
extern "C" void rps_do_create_fifos_from_prefix(void);
extern "C" void rps_extend_env(void);
extern "C" unsigned long rps_run_delay; // in seconds
////////////////////////////////////////////////////////////////
extern "C" std::atomic<unsigned> rps_debug_flags;
void rps_set_debug_output_path(const char*filepath);
#define RPS_DEBUG_LOG_LEVEL LOG_DEBUG // syslog debug log level
// so we could code RPS_DEBUG_PRINTF(NEVER, ....)
#define RPS_DEBUG_NEVER RPS_DEBUG__NONE
#define RPS_DEBUG_ENABLED(dbgopt) (rps_debug_flags & (1 << RPS_DEBUG_##dbgopt))
/// debug print to stderr or syslog or to the file given to
/// rps_set_debug_output_path ....; if fline is negative, print a
/// newline before....
void
rps_debug_printf_at(const char *fname, int fline, const char*funcname, Rps_Debug dbgopt,
const char *fmt, ...)
__attribute__ ((format (printf, 5, 6)));
#warning we may want to define a std::ostream subclass for the debugging stream (with a singleton)
#define RPS_DEBUG_PRINTF_AT(fname, fline, dbgopt, fmt, ...) \
do \
{ \
if (RPS_DEBUG_ENABLED(dbgopt)) \
rps_debug_printf_at(fname, fline,__FUNCTION__, \
RPS_DEBUG_##dbgopt, fmt, \
##__VA_ARGS__); \
} \
while (0)
#define RPS_DEBUG_PRINTF_AT_BIS(fname, fline, dbgopt, fmt, ...) \
RPS_DEBUG_PRINTF_AT(fname, fline, dbgopt, fmt, ##__VA_ARGS__)
#define RPS_DEBUG_PRINTF(dbgopt, fmt, ...) \
RPS_DEBUG_PRINTF_AT_BIS(__FILE__, __LINE__, dbgopt, fmt, ##__VA_ARGS__)
#define RPS_DEBUG_NLPRINTF(dbgopt, fmt, ...) \
RPS_DEBUG_PRINTF_AT_BIS(__FILE__, -__LINE__, dbgopt, fmt, ##__VA_ARGS__)
#define RPS_DEBUG_LOG_AT(fname, fline, dbgopt, logmsg) do \
{ \
if (RPS_DEBUG_ENABLED(dbgopt)) \
{ \
std::ostringstream _logstream_##fline; \
_logstream_##fline << logmsg << std::flush; \
rps_debug_printf_at(fname, fline, __FUNCTION__, \
RPS_DEBUG_##dbgopt, \
"%s", \
_logstream_##fline.str().c_str()); \
} \
} \
while (0)
#define RPS_DEBUG_LOG_AT_BIS(fname, fline, dbgopt, logmsg) \
RPS_DEBUG_LOG_AT(fname, fline, dbgopt, logmsg)
/// example of usage: RPS_DEBUG_LOG(MISC, "x=" << x) related to RPS_DEBUG_MISC
#define RPS_DEBUG_LOG(dbgopt, logmsg) \
RPS_DEBUG_LOG_AT_BIS(__FILE__, __LINE__, dbgopt, logmsg)
#define RPS_DEBUGNL_LOG_AT(fname, fline, dbgopt, logmsg) \
do { /*in RPS_DEBUGNL_LOG_AT*/ \
if (RPS_DEBUG_ENABLED(dbgopt)) \
{ \
std::ostringstream _logstream_##fline; \
_logstream_##fline << logmsg << std::flush; \
rps_debug_printf_at(fname, -fline, __FUNCTION__, \
RPS_DEBUG_##dbgopt, \
"%s", \
_logstream_##fline.str().c_str()); \
} \
} \
while (0)
#define RPS_DEBUGNL_LOG_AT_BIS(fname, fline, dbgopt, logmsg) \
RPS_DEBUGNL_LOG_AT(fname, fline, dbgopt, logmsg)
/// example of usage: RPS_DEBUG_LOG(MISC, "x=" << x) related to RPS_DEBUG_MISC
#define RPS_DEBUGNL_LOG(dbgopt, logmsg) \
RPS_DEBUGNL_LOG_AT_BIS(__FILE__, __LINE__, dbgopt, logmsg)
//////////////// inform
#define RPS_INFORM_AT_BIS(Fil,Lin,Fmt,...) do { \
bool ontty = rps_stdout_istty; \
if (rps_syslog_enabled) { \
syslog(LOG_INFO, "RefPerSys INFORM %s:%d: %s " Fmt "\n", \
Fil, Lin, __PRETTY_FUNCTION__, ##__VA_ARGS__); \
} else { \
fprintf(stdout, "\n\n" \
"%s*** RefPerSys INFORM:%s %s:%d: %s<%s>%s\n " \
Fmt "\n\n", \
ontty?RPS_TERMINAL_BOLD_ESCAPE:"", \
ontty?RPS_TERMINAL_NORMAL_ESCAPE:"", \
Fil, Lin, \
ontty?RPS_TERMINAL_ITALICS_ESCAPE:"", \
__PRETTY_FUNCTION__, \
ontty?RPS_TERMINAL_NORMAL_ESCAPE:"", \
##__VA_ARGS__); \
fflush(stdout); }; \
if (rps_fltk_enabled()) \
rps_fltk_printf_inform_message \
(Fil,Lin, \
__PRETTY_FUNCTION__, \
rps_incremented_debug_counter(), \
Fmt, \
##__VA_ARGS__); \
} while(0)
#define RPS_INFORM_AT(Fil,Lin,Fmt,...) RPS_INFORM_AT_BIS(Fil,Lin,Fmt,##__VA_ARGS__)
// typical usage could be RPS_INFORM("something bad x=%d", x)
#define RPS_INFORM(Fmt,...) RPS_INFORM_AT(__FILE__,__LINE__,Fmt,##__VA_ARGS__)
#define RPS_INFORMOUT_AT_BIS(Fil,Lin,...) do { \
std::ostringstream outs_##Lin; \
if (rps_syslog_enabled) { \
outs_##Lin << __VA_ARGS__ << std::flush; \
syslog(LOG_INFO, "%s:%d:%s %s\n", \
(Fil), (Lin), __PRETTY_FUNCTION__, \
outs_##Lin.str().c_str()); \
} else { \
bool ontty = rps_stdout_istty; \
outs_##Lin \
<< (ontty?RPS_TERMINAL_BOLD_ESCAPE:"") \
<< "** RefPerSys INFORM!" \
<< (ontty?RPS_TERMINAL_NORMAL_ESCAPE:"") << " " \
<< (ontty?RPS_TERMINAL_ITALICS_ESCAPE:"") \
<< (Fil) << ":" << Lin << ": " \
<< __PRETTY_FUNCTION__ \
<< (ontty?RPS_TERMINAL_NORMAL_ESCAPE:"") \
<< ' ' << __VA_ARGS__ << std::flush; \
fputs(outs_##Lin.str().c_str(), stdout); \