/
system.c
1293 lines (1105 loc) · 37.8 KB
/
system.c
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
/****************************************************************************
**
** This file is part of GAP, a system for computational discrete algebra.
**
** Copyright of GAP belongs to its developers, whose names are too numerous
** to list here. Please refer to the COPYRIGHT file for details.
**
** SPDX-License-Identifier: GPL-2.0-or-later
**
** The files "system.c" and "sysfiles.c" contains all operating system
** dependent functions. This file contains all system dependent functions
** except file and stream operations, which are implemented in "sysfiles.c".
** The following labels determine which operating system is actually used.
*/
#include "system.h"
#include "gaputils.h"
#ifdef GAP_MEM_CHECK
#include "gasman.h"
#endif
#include "profile.h"
#include "sysfiles.h"
#include "sysmem.h"
#include "sysopt.h"
#ifdef HPCGAP
#include "hpc/misc.h"
#endif
#ifdef USE_JULIA_GC
#include "julia.h"
#endif
#include <assert.h>
#include <fcntl.h>
#include <stdarg.h>
#include <unistd.h>
#include <sys/stat.h>
#ifdef HAVE_LIBREADLINE
#include <readline/readline.h>
#endif
#include <sys/time.h> /* definition of 'struct timeval' */
#include <sys/types.h>
#ifdef HAVE_SYS_RESOURCE_H
#include <sys/resource.h> /* definition of 'struct rusage' */
#endif
#ifdef SYS_IS_DARWIN
#include <mach/mach_time.h>
#endif
/****************************************************************************
**
*V SyKernelVersion . . . . . . . . . . . . . . . hard coded kernel version
** do not edit the following line. Occurrences of `4.dev' and `today'
** will be replaced by string matching by distribution wrapping scripts.
*/
const Char * SyKernelVersion = "4.dev";
/****************************************************************************
**
*F * * * * * * * * * * * command line settable options * * * * * * * * * * *
*/
/****************************************************************************
**
*V SyArchitecture . . . . . . . . . . . . . . . . name of the architecture
*/
const Char * SyArchitecture = SYS_ARCH;
/****************************************************************************
**
*V SyCTRD . . . . . . . . . . . . . . . . . . . true if '<ctr>-D' is <eof>
*/
UInt SyCTRD;
/****************************************************************************
**
*V SyCompileInput . . . . . . . . . . . . . . . . . . from this input file
*/
Char SyCompileInput[GAP_PATH_MAX];
/****************************************************************************
**
*V SyCompileMagic1 . . . . . . . . . . . . . . . . . . and this magic string
*/
Char * SyCompileMagic1;
/****************************************************************************
**
*V SyCompileName . . . . . . . . . . . . . . . . . . . . . . with this name
*/
Char SyCompileName[256];
/****************************************************************************
**
*V SyCompileOutput . . . . . . . . . . . . . . . . . . into this output file
*/
Char SyCompileOutput[GAP_PATH_MAX];
/****************************************************************************
**
*V SyCompilePlease . . . . . . . . . . . . . . . tell GAP to compile a file
*/
Int SyCompilePlease;
/****************************************************************************
**
*V SyDebugLoading . . . . . . . . . output messages about loading of files
*/
Int SyDebugLoading;
/****************************************************************************
**
*V SyGapRootPaths . . . . . . . . . . . . . . . . . . . array of root paths
**
*/
Char SyGapRootPaths[MAX_GAP_DIRS][GAP_PATH_MAX];
Char DotGapPath[GAP_PATH_MAX];
/****************************************************************************
**
*V IgnoreGapRC . . . . . . . . . . . . . . . . . . . -r option for kernel
**
*/
Int IgnoreGapRC;
/****************************************************************************
**
*V SyLineEdit . . . . . . . . . . . . . . . . . . . . support line editing
**
** 0: no line editing
** 1: line editing if terminal
** 2: always line editing (EMACS)
*/
UInt SyLineEdit;
/****************************************************************************
**
*V SyUseReadline . . . . . . . . . . . . . . . . . . support line editing
**
** Switch for not using readline although GAP is compiled with libreadline
*/
UInt SyUseReadline;
/****************************************************************************
**
*V SyMsgsFlagBags . . . . . . . . . . . . . . . . . enable gasman messages
**
** 'SyMsgsFlagBags' determines whether garbage collections are reported or
** not.
**
** Per default it is false, i.e. Gasman is silent about garbage collections.
** It can be changed by using the '-g' option on the GAP command line.
**
** This is used in the function 'SyMsgsBags' below.
**
** Put in this package because the command line processing takes place here.
*/
UInt SyMsgsFlagBags;
/****************************************************************************
**
*V SyNrCols . . . . . . . . . . . . . . . . . . length of the output lines
**
** 'SyNrCols' is the length of the lines on the standard output device.
**
** Per default this is 80 characters which is the usual width of terminals.
** It can be changed by the '-x' options for larger terminals or printers.
**
** 'Pr' uses this to decide where to insert a <newline> on the output lines.
** 'SyRead' uses it to decide when to start scrolling the echoed input line.
**
** See also getwindowsize() below.
**
** Put in this package because the command line processing takes place here.
*/
UInt SyNrCols;
UInt SyNrColsLocked;
/****************************************************************************
**
*V SyNrRows . . . . . . . . . . . . . . . . . number of lines on the screen
**
** 'SyNrRows' is the number of lines on the standard output device.
**
** Per default this is 24, which is the usual size of terminal screens.
** It can be changed with the '-y' option for larger terminals or printers.
**
** 'SyHelp' uses this to decide where to stop with '-- <space> for more --'.
**
** See also getwindowsize() below.
*/
UInt SyNrRows;
UInt SyNrRowsLocked;
/****************************************************************************
**
*V SyQuiet . . . . . . . . . . . . . . . . . . . . . . . . . suppress prompt
**
** 'SyQuiet' determines whether GAP should print the prompt and the banner.
**
** Per default its false, i.e. GAP prints the prompt and the nice banner.
** It can be changed by the '-q' option to have GAP operate in silent mode.
**
** It is used by the functions in 'gap.c' to suppress printing the prompts.
**
** Put in this package because the command line processing takes place here.
*/
UInt SyQuiet;
/****************************************************************************
**
*V SyQuitOnBreak . . . . . . . . . . exit GAP instead of entering break loop
*/
UInt SyQuitOnBreak;
/****************************************************************************
**
*V SyRestoring . . . . . . . . . . . . . . . . . . . . restoring a workspace
**
** `SyRestoring' determines whether GAP is restoring a workspace or not. If
** it is zero no restoring should take place otherwise it holds the filename
** of a workspace to restore.
**
*/
Char * SyRestoring;
/****************************************************************************
**
*V SyInitializing set to 1 during library init
**
** `SyInitializing' is set to 1 during the library initialization phase of
** startup. It suppresses some behaviours that may not be possible so early
** such as homogeneity tests in the plist code.
*/
UInt SyInitializing;
/****************************************************************************
**
*V SyLoadSystemInitFile . . . . . . should GAP load 'lib/init.g' at startup
*/
Int SyLoadSystemInitFile = 1;
/****************************************************************************
**
*V SyUseModule . . . . . . . . . check for static modules in 'READ_GAP_ROOT'
*/
int SyUseModule;
/****************************************************************************
**
*V SyWindow . . . . . . . . . . . . . . . . running under a window handler
**
** 'SyWindow' is 1 if GAP is running under a window handler front end such
** as 'xgap', and 0 otherwise.
**
** If running under a window handler front end, GAP adds various commands
** starting with '@' to the output to let 'xgap' know what is going on.
*/
UInt SyWindow;
/****************************************************************************
**
*F * * * * * * * * * * * * * time related functions * * * * * * * * * * * * *
*/
/****************************************************************************
**
*F SyTime() . . . . . . . . . . . . . . . return time spent in milliseconds
**
** 'SyTime' returns the number of milliseconds spent by GAP so far.
**
** Should be as accurate as possible, because it is used for profiling.
*/
UInt SyTime ( void )
{
struct rusage buf;
if ( getrusage( RUSAGE_SELF, &buf ) ) {
Panic("'SyTime' could not get time");
}
return buf.ru_utime.tv_sec*1000 + buf.ru_utime.tv_usec/1000;
}
UInt SyTimeSys ( void )
{
struct rusage buf;
if ( getrusage( RUSAGE_SELF, &buf ) ) {
Panic("'SyTimeSys' could not get time");
}
return buf.ru_stime.tv_sec*1000 + buf.ru_stime.tv_usec/1000;
}
UInt SyTimeChildren ( void )
{
struct rusage buf;
if ( getrusage( RUSAGE_CHILDREN, &buf ) ) {
Panic("'SyTimeChildren' could not get time");
}
return buf.ru_utime.tv_sec*1000 + buf.ru_utime.tv_usec/1000;
}
UInt SyTimeChildrenSys ( void )
{
struct rusage buf;
if ( getrusage( RUSAGE_CHILDREN, &buf ) ) {
Panic("'SyTimeChildrenSys' could not get time");
}
return buf.ru_stime.tv_sec*1000 + buf.ru_stime.tv_usec/1000;
}
/****************************************************************************
**
*F * * * * * * * * * * * * * * * string functions * * * * * * * * * * * * * *
*/
#ifndef HAVE_STRLCPY
size_t strlcpy (
char *dst,
const char *src,
size_t len)
{
/* Keep a copy of the original src. */
const char * const orig_src = src;
/* If a non-empty len was specified, we can actually copy some data. */
if (len > 0) {
/* Copy up to len-1 bytes (reserve one for the terminating zero). */
while (--len > 0) {
/* Copy from src to dst; if we reach the string end, we are
done and can simply return the total source string length */
if ((*dst++ = *src++) == 0) {
/* return length of source string without the zero byte */
return src - orig_src - 1;
}
}
/* If we got here, then we used up the whole buffer and len is zero.
We must make sure to terminate the destination string. */
*dst = 0;
}
/* in the end, we must return the length of the source string, no
matter whether we completely copied or not; so advance src
till its terminator is reached */
while (*src++)
;
/* return length of source string without the zero byte */
return src - orig_src - 1;
}
#endif /* !HAVE_STRLCPY */
#ifndef HAVE_STRLCAT
size_t strlcat (
char *dst,
const char *src,
size_t len)
{
/* Keep a copy of the original dst. */
const char * const orig_dst = dst;
/* Find the end of the dst string, so that we can append after it. */
while (*dst != 0 && len > 0) {
dst++;
len--;
}
/* We can only append anything if there is free space left in the
destination buffer. */
if (len > 0) {
/* One byte goes away for the terminating zero. */
len--;
/* Do the actual work and append from src to dst, until we either
appended everything, or reached the dst buffer's end. */
while (*src != 0 && len > 0) {
*dst++ = *src++;
len--;
}
/* Terminate, terminate, terminate! */
*dst = 0;
}
/* Compute the final result. */
return (dst - orig_dst) + strlen(src);
}
#endif /* !HAVE_STRLCAT */
size_t strxcpy (
char *dst,
const char *src,
size_t len)
{
size_t res = strlcpy(dst, src, len);
assert(res < len);
return res;
}
size_t strxcat (
char *dst,
const char *src,
size_t len)
{
size_t res = strlcat(dst, src, len);
assert(res < len);
return res;
}
/****************************************************************************
**
*F SySleep( <secs> ) . . . . . . . . . . . . . .sleep GAP for <secs> seconds
**
** NB Various OS events (like signals) might wake us up
**
*/
void SySleep ( UInt secs )
{
sleep( (unsigned int) secs );
}
/****************************************************************************
**
*F SyUSleep( <msecs> ) . . . . . . . . . .sleep GAP for <msecs> microseconds
**
** NB Various OS events (like signals) might wake us up
**
*/
void SyUSleep ( UInt msecs )
{
usleep( (unsigned int) msecs );
}
/****************************************************************************
**
*F * * * * * * * * * * * * * initialize module * * * * * * * * * * * * * * *
*/
/****************************************************************************
**
*F SyExit( <ret> ) . . . . . . . . . . . . . exit GAP with return code <ret>
**
** 'SyExit' is the official way to exit GAP, bus errors are the unofficial.
** The function 'SyExit' must perform all the necessary cleanup operations.
** If ret is 0 'SyExit' should signal to a calling process that all is ok.
** If ret is 1 'SyExit' should signal a failure to the calling process.
**
** If the user calls 'QUIT_GAP' with a value, then the global variable
** 'UserHasQUIT' will be set, and their requested return value will be
** in 'SystemErrorCode'. If the return value would be 0, we check
** this value and use it instead.
*/
void SyExit (
UInt ret )
{
#ifdef USE_JULIA_GC
jl_atexit_hook(ret);
#endif
exit( (int)ret );
}
/****************************************************************************
**
*F Panic( <msg> )
*/
void Panic_(const char * file, int line, const char * fmt, ...)
{
fprintf(stderr, "Panic in %s:%d: ", file, line);
va_list args;
va_start(args, fmt);
vfprintf(stderr, fmt, args);
va_end (args);
fputs("\n", stderr);
SyExit(1);
}
/****************************************************************************
**
*F SyNanosecondsSinceEpoch()
**
** 'SyNanosecondsSinceEpoch' returns a 64-bit integer which represents the
** number of nanoseconds since some unspecified starting point. This means
** that the number returned by this function is not in itself meaningful,
** but the difference between the values returned by two consecutive calls
** can be used to measure wallclock time.
**
** The accuracy of this is system dependent. For systems that implement
** clock_getres, we could get the promised accuracy.
**
** Note that gettimeofday has been marked obsolete in the POSIX standard.
** We are using it because it is implemented in most systems still.
**
** If we are using gettimeofday we cannot guarantee the values that
** are returned by SyNanosecondsSinceEpoch to be monotonic.
**
** Returns -1 to represent failure
**
*/
Int8 SyNanosecondsSinceEpoch(void)
{
Int8 res;
#if defined(SYS_IS_DARWIN)
static mach_timebase_info_data_t timeinfo;
if ( timeinfo.denom == 0 ) {
(void) mach_timebase_info(&timeinfo);
}
res = mach_absolute_time();
res *= timeinfo.numer;
res /= timeinfo.denom;
#elif defined(HAVE_CLOCK_GETTIME) && defined(CLOCK_MONOTONIC)
struct timespec ts;
if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) {
res = ts.tv_sec;
res *= 1000000000L;
res += ts.tv_nsec;
} else {
res = -1;
}
#elif defined(HAVE_GETTIMEOFDAY)
struct timeval tv;
if (gettimeofday(&tv, NULL) == 0) {
res = tv.tv_sec;
res *= 1000000L;
res += tv.tv_usec;
res *= 1000;
} else {
res = -1;
};
#else
res = -1;
#endif
return res;
}
/****************************************************************************
**
*V SyNanosecondsSinceEpochMethod
*V SyNanosecondsSinceEpochMonotonic
**
** These constants give information about the method used to obtain
** NanosecondsSinceEpoch, and whether the values returned are guaranteed
** to be monotonic.
*/
#if defined(SYS_IS_DARWIN)
const char * const SyNanosecondsSinceEpochMethod = "mach_absolute_time";
const Int SyNanosecondsSinceEpochMonotonic = 1;
#elif defined(HAVE_CLOCK_GETTIME) && defined(CLOCK_MONOTONIC)
const char * const SyNanosecondsSinceEpochMethod = "clock_gettime";
const Int SyNanosecondsSinceEpochMonotonic = 1;
#elif defined(HAVE_GETTIMEOFDAY)
const char * const SyNanosecondsSinceEpochMethod = "gettimeofday";
const Int SyNanosecondsSinceEpochMonotonic = 0;
#else
const char * const SyNanosecondsSinceEpochMethod = "unsupported";
const Int SyNanosecondsSinceEpochMonotonic = 0;
#endif
/****************************************************************************
**
*F SyNanosecondsSinceEpochResolution()
**
** 'SyNanosecondsSinceEpochResolution' returns a 64-bit integer which
** represents the resolution in nanoseconds of the timer used for
** SyNanosecondsSinceEpoch.
**
** If the return value is positive then the value has been returned
** by the operating system can can probably be relied on. If the
** return value is negative it is just an estimate (as in the case
** of gettimeofday we have no way to get the exact resolution so we
** just pretend that the resolution is 1000 nanoseconds).
**
** A result of 0 signifies inability to obtain any sensible value.
*/
Int8 SyNanosecondsSinceEpochResolution(void)
{
Int8 res;
#if defined(SYS_IS_DARWIN)
static mach_timebase_info_data_t timeinfo;
if ( timeinfo.denom == 0 ) {
(void) mach_timebase_info(&timeinfo);
}
res = timeinfo.numer;
res /= timeinfo.denom;
#elif defined(HAVE_CLOCK_GETTIME) && defined(CLOCK_MONOTONIC)
struct timespec ts;
if (clock_getres(CLOCK_MONOTONIC, &ts) == 0) {
res = ts.tv_sec;
res *= 1000000000L;
res += ts.tv_nsec;
} else {
res = 0;
}
#elif defined(HAVE_GETTIMEOFDAY)
res = -1000;
#else
res = 0;
#endif
return res;
}
/****************************************************************************
**
*F SySetGapRootPath( <string> ) . . . . . . . . . set the root directories
**
** 'SySetGapRootPath' takes a string and modifies a list of root directories
** in 'SyGapRootPaths'.
**
** A leading semicolon in <string> means that the list of directories in
** <string> is appended to the existing list of root paths. A trailing
** semicolon means they are prepended. If there is no leading or trailing
** semicolon, then the root paths are overwritten.
*/
/****************************************************************************
**
*f SySetGapRootPath( <string> )
**
** This function assumes that the system uses '/' as path separator.
** Currently, we support nothing else. For Windows (or rather: Cygwin), we
** rely on a small hack which converts the path separator '\' used there
** on '/' on the fly. Put differently: Systems that use completely different
** path separators, or none at all, are currently not supported.
*/
static void SySetGapRootPath( const Char * string )
{
const Char * p;
Char * q;
Int i;
Int n;
/* set string to a default value if unset */
if ( string == 0 || *string == 0 ) {
string = "./";
}
/*
** check if we append, prepend or overwrite.
*/
if( string[0] == ';' ) {
/* Count the number of root directories already present. */
n = 0; while( SyGapRootPaths[n][0] != '\0' ) n++;
/* Skip leading semicolon. */
string++;
}
else if( string[ strlen(string) - 1 ] == ';' ) {
/* Count the number of directories in 'string'. */
n = 0; p = string; while( *p ) if( *p++ == ';' ) n++;
/* Find last root path. */
for( i = 0; i < MAX_GAP_DIRS; i++ )
if( SyGapRootPaths[i][0] == '\0' ) break;
i--;
#ifdef HPCGAP
n *= 2; // for each root <ROOT> we also add <ROOT/hpcgap> as a root
#endif
/* Move existing root paths to the back */
if( i + n >= MAX_GAP_DIRS ) return;
while( i >= 0 ) {
memcpy( SyGapRootPaths[i+n], SyGapRootPaths[i], sizeof(SyGapRootPaths[i+n]) );
i--;
}
n = 0;
}
else {
/* Make sure to wipe out all possibly existing root paths */
for( i = 0; i < MAX_GAP_DIRS; i++ ) SyGapRootPaths[i][0] = '\0';
n = 0;
}
/* unpack the argument */
p = string;
while ( *p ) {
if( n >= MAX_GAP_DIRS ) return;
q = SyGapRootPaths[n];
while ( *p && *p != ';' ) {
*q = *p++;
#ifdef SYS_IS_CYGWIN32
/* fix up for DOS */
if (*q == '\\')
*q = '/';
#endif
q++;
}
if ( q == SyGapRootPaths[n] ) {
strxcpy( SyGapRootPaths[n], "./", sizeof(SyGapRootPaths[n]) );
}
else if ( q[-1] != '/' ) {
*q++ = '/';
*q = '\0';
}
else {
*q = '\0';
}
if ( *p ) {
p++;
}
n++;
#ifdef HPCGAP
// or each root <ROOT> we also add <ROOT/hpcgap> as a root (and first)
if( n < MAX_GAP_DIRS ) {
strlcpy( SyGapRootPaths[n], SyGapRootPaths[n-1], sizeof(SyGapRootPaths[n]) );
}
strxcat( SyGapRootPaths[n-1], "hpcgap/", sizeof(SyGapRootPaths[n-1]) );
n++;
#endif
}
}
/****************************************************************************
**
*F SySetInitialGapRootPaths( <string> ) . . . . . set the root directories
**
** Set up GAP's initial root paths, based on the location of the
** GAP executable.
*/
static void SySetInitialGapRootPaths(void)
{
if (GAPExecLocation[0] != 0) {
// GAPExecLocation might be a subdirectory of GAP root,
// so we will go and search for the true GAP root.
// We try stepping back up to two levels.
char pathbuf[GAP_PATH_MAX];
char initgbuf[GAP_PATH_MAX];
strxcpy(pathbuf, GAPExecLocation, sizeof(pathbuf));
for (Int i = 0; i < 3; ++i) {
strxcpy(initgbuf, pathbuf, sizeof(initgbuf));
strxcat(initgbuf, "lib/init.g", sizeof(initgbuf));
if (SyIsReadableFile(initgbuf) == 0) {
SySetGapRootPath(pathbuf);
// escape from loop
return;
}
// try up a directory level
strxcat(pathbuf, "../", sizeof(pathbuf));
}
}
// Set GAP root path to current directory, if we have no other
// idea, and for backwards compatibility.
// Note that GAPExecLocation must always end with a slash.
SySetGapRootPath("./");
}
/****************************************************************************
**
*F syLongjmp( <jump buffer>, <value>)
** Perform a long jump
**
*F RegisterSyLongjmpObserver( <func> )
** Register a function to be called before longjmp is called.
** returns 1 on success, 0 if the table of functions is already full.
** This function is idempotent -- if a function is passed multiple times
** it is still only registered once.
*/
enum { signalSyLongjmpFuncsLen = 16 };
static voidfunc signalSyLongjmpFuncs[signalSyLongjmpFuncsLen];
Int RegisterSyLongjmpObserver(voidfunc func)
{
Int i;
for (i = 0; i < signalSyLongjmpFuncsLen; ++i) {
if (signalSyLongjmpFuncs[i] == func) {
return 1;
}
if (signalSyLongjmpFuncs[i] == 0) {
signalSyLongjmpFuncs[i] = func;
return 1;
}
}
return 0;
}
void syLongjmp(syJmp_buf* buf, int val)
{
Int i;
for (i = 0; i < signalSyLongjmpFuncsLen && signalSyLongjmpFuncs[i]; ++i)
(signalSyLongjmpFuncs[i])();
syLongjmpInternal(*buf, val);
}
/****************************************************************************
**
*F InitSystem( <argc>, <argv> ) . . . . . . . . . initialize system package
**
** 'InitSystem' is called very early during the initialization from 'main'.
** It is passed the command line array <argc>, <argv> to look for options.
**
** For UNIX it initializes the default files 'stdin', 'stdout' and 'stderr',
** installs the handler 'syAnswerIntr' to answer the user interrupts
** '<ctr>-C', scans the command line for options, sets up the GAP root paths,
** locates the '.gaprc' file (if any), and more.
*/
typedef struct { Char symbol; UInt value; } sizeMultiplier;
sizeMultiplier memoryUnits[]= {
{'k', 1024},
{'K', 1024},
{'m', 1024*1024},
{'M', 1024*1024},
{'g', 1024*1024*1024},
{'G', 1024*1024*1024},
#ifdef SYS_IS_64_BIT
{'t', 1024UL*1024*1024*1024},
{'T', 1024UL*1024*1024*1024},
{'p', 1024UL*1024*1024*1024*1024}, /* you never know */
{'P', 1024UL*1024*1024*1024*1024},
#endif
};
static UInt ParseMemory( Char * s)
{
UInt size = atol(s);
Char symbol = s[strlen(s)-1];
UInt i;
UInt maxmem;
#ifdef SYS_IS_64_BIT
maxmem = 15000000000000000000UL;
#else
maxmem = 4000000000UL;
#endif
for (i = 0; i < ARRAY_SIZE(memoryUnits); i++) {
if (symbol == memoryUnits[i].symbol) {
UInt value = memoryUnits[i].value;
if (size > maxmem/value)
size = maxmem;
else
size *= value;
return size;
}
}
if (!IsDigit(symbol))
fputs("Unrecognised memory unit ignored", stderr);
return size;
}
struct optInfo {
Char shortkey;
Char longkey[50];
Int (*handler)(Char **, void *);
void *otherArg;
UInt minargs;
};
static Int toggle( Char ** argv, void *Variable )
{
UInt * variable = (UInt *) Variable;
*variable = !*variable;
return 0;
}
#ifdef HPCGAP
static Int storePosInteger( Char **argv, void *Where )
{
UInt *where = (UInt *)Where;
UInt n;
Char *p = argv[0];
n = 0;
while (IsDigit(*p)) {
n = n * 10 + (*p-'0');
p++;
}
if (p == argv[0] || *p || n == 0)
fputs("Argument not a positive integer", stderr);
*where = n;
return 1;
}
#endif
static Int storeString( Char **argv, void *Where )
{
Char **where = (Char **)Where;
*where = argv[0];
return 1;
}
static Int storeMemory( Char **argv, void *Where )
{
UInt *where = (UInt *)Where;
*where = ParseMemory(argv[0]);
return 1;
}
static Int storeMemory2( Char **argv, void *Where )
{
UInt *where = (UInt *)Where;
*where = ParseMemory(argv[0])/1024;
return 1;
}
static Int processCompilerArgs( Char **argv, void * dummy)
{
SyCompilePlease = 1;
strxcat( SyCompileOutput, argv[0], sizeof(SyCompileOutput) );
strxcat( SyCompileInput, argv[1], sizeof(SyCompileInput) );
strxcat( SyCompileName, argv[2], sizeof(SyCompileName) );
SyCompileMagic1 = argv[3];
return 4;
}
static Int unsetString( Char **argv, void *Where)
{
*(Char **)Where = (Char *)0;
return 0;
}
static Int forceLineEditing( Char **argv,void *Level)
{
UInt level = (UInt)Level;
SyLineEdit = level;
return 0;
}
static Int setGapRootPath( Char **argv, void *Dummy)
{
SySetGapRootPath( argv[0] );
return 1;
}
#ifndef GAP_MEM_CHECK
// Provide stub with helpful error message
static Int enableMemCheck(Char ** argv, void * dummy)
{
SyFputs( "# Error: --enableMemCheck not supported by this copy of GAP\n", 3);
SyFputs( " pass --enable-memory-checking to ./configure\n", 3 );
SyExit(2);
}
#endif
static Int preAllocAmount;
/* These are just the options that need kernel processing. Additional options will be
recognised and handled in the library */
/* These options must be kept in sync with those in system.g, so the help output
is correct */