forked from gap-system/gap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgap.c
More file actions
3234 lines (2814 loc) · 94 KB
/
gap.c
File metadata and controls
3234 lines (2814 loc) · 94 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
/****************************************************************************
**
*W gap.c GAP source Frank Celler
*W & Martin Schönert
**
**
*Y Copyright (C) 1996, Lehrstuhl D für Mathematik, RWTH Aachen, Germany
*Y (C) 1998 School Math and Comp. Sci., University of St Andrews, Scotland
*Y Copyright (C) 2002 The GAP Group
**
** This file contains the various read-eval-print loops and related stuff.
*/
#include <src/gap.h>
#include <src/ariths.h>
#include <src/bool.h>
#include <src/calls.h>
#include <src/code.h>
#include <src/compiler.h>
#include <src/compstat.h>
#include <src/exprs.h>
#include <src/funcs.h>
#include <src/gapstate.h>
#include <src/gvars.h>
#include <src/integer.h>
#include <src/io.h>
#include <src/lists.h>
#include <src/opers.h>
#include <src/plist.h>
#include <src/precord.h>
#include <src/records.h>
#include <src/read.h>
#include <src/saveload.h>
#include <src/stats.h>
#include <src/streams.h>
#include <src/stringobj.h>
#include <src/sysfiles.h>
#include <src/vars.h>
#ifdef HPCGAP
#include <src/intrprtr.h>
#include <src/hpc/misc.h>
#include <src/hpc/thread.h>
#include <src/hpc/threadapi.h>
#endif
static Obj Error;
static Obj ErrorInner;
static UInt SystemErrorCode;
/****************************************************************************
**
*V Last . . . . . . . . . . . . . . . . . . . . . . global variable 'last'
**
** 'Last', 'Last2', and 'Last3' are the global variables 'last', 'last2',
** and 'last3', which are automatically assigned the result values in the
** main read-eval-print loop.
*/
UInt Last;
/****************************************************************************
**
*V Last2 . . . . . . . . . . . . . . . . . . . . . . global variable 'last2'
*/
UInt Last2;
/****************************************************************************
**
*V Last3 . . . . . . . . . . . . . . . . . . . . . . global variable 'last3'
*/
UInt Last3;
/****************************************************************************
**
*V Time . . . . . . . . . . . . . . . . . . . . . . global variable 'time'
**
** 'Time' is the global variable 'time', which is automatically assigned the
** time the last command took.
*/
UInt Time;
/****************************************************************************
**
*V MemoryAllocated . . . . . . . . . . . global variable 'memory_allocated'
**
** 'MemoryAllocated' is the global variable 'memory_allocated',
** which is automatically assigned the amount of memory allocated while
** executing the last command.
*/
UInt MemoryAllocated;
/****************************************************************************
**
*F ViewObjHandler . . . . . . . . . handler to view object and catch errors
**
** This is the function actually called in Read-Eval-View loops.
** We might be in trouble if the library has not (yet) loaded and so ViewObj
** is not yet defined, or the fallback methods not yet installed. To avoid
** this problem, we check, and use PrintObj if there is a problem
**
** This function also supplies the \n after viewing.
*/
UInt ViewObjGVar;
void ViewObjHandler ( Obj obj )
{
volatile Obj func;
syJmp_buf readJmpError;
/* get the functions */
func = ValAutoGVar(ViewObjGVar);
/* if non-zero use this function, otherwise use `PrintObj' */
memcpy( readJmpError, STATE(ReadJmpError), sizeof(syJmp_buf) );
TRY_READ {
if ( func != 0 && TNUM_OBJ(func) == T_FUNCTION ) {
ViewObj(obj);
}
else {
PrintObj( obj );
}
Pr( "\n", 0L, 0L );
}
memcpy( STATE(ReadJmpError), readJmpError, sizeof(syJmp_buf) );
}
/****************************************************************************
**
*F main( <argc>, <argv> ) . . . . . . . main program, read-eval-print loop
*/
UInt QUITTINGGVar;
typedef struct {
const Char * name;
Obj * address;
} StructImportedGVars;
#ifndef MAX_IMPORTED_GVARS
#define MAX_IMPORTED_GVARS 1024
#endif
static StructImportedGVars ImportedGVars[MAX_IMPORTED_GVARS];
static Int NrImportedGVars;
static StructImportedGVars ImportedFuncs[MAX_IMPORTED_GVARS];
static Int NrImportedFuncs;
static char **sysenviron;
/*
TL: Obj ShellContext = 0;
TL: Obj BaseShellContext = 0;
*/
Obj Shell ( Obj context,
UInt canReturnVoid,
UInt canReturnObj,
UInt lastDepth,
UInt setTime,
Char *prompt,
Obj preCommandHook,
UInt catchQUIT,
Char *inFile,
Char *outFile)
{
UInt time = 0;
UInt8 mem = 0;
UInt status;
Obj evalResult;
UInt dualSemicolon;
UInt oldPrintDepth;
Obj res;
Obj oldShellContext;
Obj oldBaseShellContext;
Int oldRecursionDepth;
oldShellContext = STATE(ShellContext);
STATE(ShellContext) = context;
oldBaseShellContext = STATE(BaseShellContext);
STATE(BaseShellContext) = context;
Int oldErrorLLevel = STATE(ErrorLLevel);
STATE(ErrorLLevel) = 0;
oldRecursionDepth = GetRecursionDepth();
/* read-eval-print loop */
if (!OpenOutput(outFile))
ErrorQuit("SHELL: can't open outfile %s",(Int)outFile,0);
if(!OpenInput(inFile))
{
CloseOutput();
ErrorQuit("SHELL: can't open infile %s",(Int)inFile,0);
}
oldPrintDepth = STATE(PrintObjDepth);
STATE(PrintObjDepth) = 0;
while ( 1 ) {
/* start the stopwatch */
if (setTime) {
time = SyTime();
mem = SizeAllBags;
}
/* read and evaluate one command */
STATE(Prompt) = prompt;
ClearError();
STATE(PrintObjDepth) = 0;
ResetOutputIndent();
SetRecursionDepth(0);
/* here is a hook: */
if (preCommandHook) {
if (!IS_FUNC(preCommandHook))
{
Pr("#E CommandHook was non-function, ignoring\n",0L,0L);
}
else
{
Call0ArgsInNewReader(preCommandHook);
/* Recover from a potential break loop: */
STATE(Prompt) = prompt;
ClearError();
}
}
/* now read and evaluate and view one command */
status = ReadEvalCommand(STATE(ShellContext), &evalResult, &dualSemicolon);
if (STATE(UserHasQUIT))
break;
/* handle ordinary command */
if ( status == STATUS_END && evalResult != 0 ) {
/* remember the value in 'last' */
if (lastDepth >= 3)
AssGVar( Last3, ValGVarTL( Last2 ) );
if (lastDepth >= 2)
AssGVar( Last2, ValGVarTL( Last ) );
if (lastDepth >= 1)
AssGVar( Last, evalResult );
/* print the result */
if ( ! dualSemicolon ) {
ViewObjHandler( evalResult );
}
}
/* handle return-value or return-void command */
else if (status & STATUS_RETURN_VAL)
if(canReturnObj)
break;
else
Pr( "'return <object>' cannot be used in this read-eval-print loop\n",
0L, 0L );
else if (status & STATUS_RETURN_VOID)
if(canReturnVoid )
break;
else
Pr( "'return' cannot be used in this read-eval-print loop\n",
0L, 0L );
/* handle quit command or <end-of-file> */
else if ( status & (STATUS_EOF | STATUS_QUIT ) ) {
SetRecursionDepth(0);
STATE(UserHasQuit) = 1;
break;
}
/* handle QUIT */
else if (status & (STATUS_QQUIT)) {
STATE(UserHasQUIT) = 1;
break;
}
/* stop the stopwatch */
if (setTime) {
AssGVar( Time, INTOBJ_INT( SyTime() - time ) );
AssGVar(MemoryAllocated, ObjInt_Int8(SizeAllBags - mem));
}
if (STATE(UserHasQuit))
{
FlushRestOfInputLine();
STATE(UserHasQuit) = 0; /* quit has done its job if we are here */
}
}
STATE(PrintObjDepth) = oldPrintDepth;
CloseInput();
CloseOutput();
STATE(BaseShellContext) = oldBaseShellContext;
STATE(ShellContext) = oldShellContext;
STATE(ErrorLLevel) = oldErrorLLevel;
SetRecursionDepth(oldRecursionDepth);
if (STATE(UserHasQUIT))
{
if (catchQUIT)
{
STATE(UserHasQUIT) = 0;
MakeReadWriteGVar(QUITTINGGVar);
AssGVar(QUITTINGGVar, True);
MakeReadOnlyGVar(QUITTINGGVar);
return Fail;
}
else
ReadEvalError();
}
if (status & (STATUS_EOF | STATUS_QUIT | STATUS_QQUIT))
{
return Fail;
}
if (status & STATUS_RETURN_VOID)
{
res = NEW_PLIST(T_PLIST_EMPTY,0);
SET_LEN_PLIST(res,0);
return res;
}
if (status & STATUS_RETURN_VAL)
{
res = NEW_PLIST(T_PLIST_HOM,1);
SET_LEN_PLIST(res,1);
SET_ELM_PLIST(res,1,evalResult);
return res;
}
assert(0);
return (Obj) 0;
}
Obj FuncSHELL (Obj self, Obj args)
{
Obj context = 0;
UInt canReturnVoid = 0;
UInt canReturnObj = 0;
Int lastDepth = 0;
UInt setTime = 0;
Obj prompt = 0;
Obj preCommandHook = 0;
Obj infile;
Obj outfile;
Obj res;
Char promptBuffer[81];
UInt catchQUIT = 0;
if (!IS_PLIST(args) || LEN_PLIST(args) != 10)
ErrorMayQuit("SHELL takes 10 arguments",0,0);
context = ELM_PLIST(args,1);
if (!IS_LVARS_OR_HVARS(context))
ErrorMayQuit("SHELL: 1st argument should be a local variables bag",0,0);
if (ELM_PLIST(args,2) == True)
canReturnVoid = 1;
else if (ELM_PLIST(args,2) == False)
canReturnVoid = 0;
else
ErrorMayQuit("SHELL: 2nd argument (can return void) should be true or false",0,0);
if (ELM_PLIST(args,3) == True)
canReturnObj = 1;
else if (ELM_PLIST(args,3) == False)
canReturnObj = 0;
else
ErrorMayQuit("SHELL: 3rd argument (can return object) should be true or false",0,0);
if (!IS_INTOBJ(ELM_PLIST(args,4)))
ErrorMayQuit("SHELL: 4th argument (last depth) should be a small integer",0,0);
lastDepth = INT_INTOBJ(ELM_PLIST(args,4));
if (lastDepth < 0 )
{
Pr("#W SHELL: negative last depth treated as zero",0,0);
lastDepth = 0;
}
else if (lastDepth > 3 )
{
Pr("#W SHELL: last depth greater than 3 treated as 3",0,0);
lastDepth = 3;
}
if (ELM_PLIST(args,5) == True)
setTime = 1;
else if (ELM_PLIST(args,5) == False)
setTime = 0;
else
ErrorMayQuit("SHELL: 5th argument (set time) should be true or false",0,0);
prompt = ELM_PLIST(args,6);
if (!IsStringConv(prompt) || GET_LEN_STRING(prompt) > 80)
ErrorMayQuit("SHELL: 6th argument (prompt) must be a string of length at most 80 characters",0,0);
promptBuffer[0] = '\0';
strlcat(promptBuffer, CSTR_STRING(prompt), sizeof(promptBuffer));
preCommandHook = ELM_PLIST(args,7);
if (preCommandHook == False)
preCommandHook = 0;
else if (!IS_FUNC(preCommandHook))
ErrorMayQuit("SHELL: 7th argument (preCommandHook) must be function or false",0,0);
infile = ELM_PLIST(args,8);
if (!IsStringConv(infile))
ErrorMayQuit("SHELL: 8th argument (infile) must be a string",0,0);
outfile = ELM_PLIST(args,9);
if (!IsStringConv(infile))
ErrorMayQuit("SHELL: 9th argument (outfile) must be a string",0,0);
if (ELM_PLIST(args,10) == True)
catchQUIT = 1;
else if (ELM_PLIST(args,10) == False)
catchQUIT = 0;
else
ErrorMayQuit("SHELL: 10th argument (catch QUIT) should be true or false",0,0);
res = Shell(context, canReturnVoid, canReturnObj, lastDepth, setTime, promptBuffer, preCommandHook, catchQUIT,
CSTR_STRING(infile), CSTR_STRING(outfile));
STATE(UserHasQuit) = 0;
return res;
}
int realmain( int argc, char * argv[], char * environ[] )
{
UInt type; /* result of compile */
Obj func; /* function (compiler) */
Int4 crc; /* crc of file to compile */
SetupGAPLocation(argc, argv);
/* initialize everything and read init.g which runs the GAP session */
InitializeGap( &argc, argv, environ );
if (!STATE(UserHasQUIT)) { /* maybe the user QUIT from the initial
read of init.g somehow*/
/* maybe compile in which case init.g got skipped */
if ( SyCompilePlease ) {
if ( ! OpenInput(SyCompileInput) ) {
SyExit(1);
}
func = READ_AS_FUNC();
crc = SyGAPCRC(SyCompileInput);
type = CompileFunc(
SyCompileOutput,
func,
SyCompileName,
crc,
SyCompileMagic1 );
if ( type == 0 )
SyExit( 1 );
SyExit( 0 );
}
}
SyExit(SystemErrorCode);
return 0;
}
#if !defined(COMPILECYGWINDLL)
int main ( int argc, char * argv[], char * environ[] )
{
#if defined(HAVE_BACKTRACE) && defined(PRINT_BACKTRACE)
InstallBacktraceHandlers();
#endif
#ifdef HPCGAP
RunThreadedMain(realmain, argc, argv, environ);
return 0;
#else
return realmain(argc, argv, environ);
#endif
}
#endif
/****************************************************************************
**
*F FuncID_FUNC( <self>, <val1> ) . . . . . . . . . . . . . . . return <val1>
*/
Obj FuncID_FUNC (
Obj self,
Obj val1 )
{
return val1;
}
/****************************************************************************
**
*F FuncRETURN_FIRST( <self>, <args> ) . . . . . . . . Return first argument
*/
Obj FuncRETURN_FIRST (
Obj self,
Obj args )
{
if (!IS_PLIST(args) || LEN_PLIST(args) < 1)
ErrorMayQuit("RETURN_FIRST requires one or more arguments",0,0);
return ELM_PLIST(args, 1);
}
/****************************************************************************
**
*F FuncRETURN_NOTHING( <self>, <arg> ) . . . . . . . . . . . Return nothing
*/
Obj FuncRETURN_NOTHING (
Obj self,
Obj arg )
{
return 0;
}
/****************************************************************************
**
*F FuncRuntime( <self> ) . . . . . . . . . . . . internal function 'Runtime'
**
** 'FuncRuntime' implements the internal function 'Runtime'.
**
** 'Runtime()'
**
** 'Runtime' returns the time spent since the start of GAP in milliseconds.
** How much time execution of statements take is of course system dependent.
** The accuracy of this number is also system dependent.
*/
Obj FuncRuntime (
Obj self )
{
return INTOBJ_INT( SyTime() );
}
Obj FuncRUNTIMES( Obj self)
{
Obj res;
res = NEW_PLIST(T_PLIST, 4);
SET_LEN_PLIST(res, 4);
SET_ELM_PLIST(res, 1, INTOBJ_INT( SyTime() ));
SET_ELM_PLIST(res, 2, INTOBJ_INT( SyTimeSys() ));
SET_ELM_PLIST(res, 3, INTOBJ_INT( SyTimeChildren() ));
SET_ELM_PLIST(res, 4, INTOBJ_INT( SyTimeChildrenSys() ));
return res;
}
/****************************************************************************
**
*F FuncNanosecondsSinceEpoch( <self> )
**
** 'FuncNanosecondsSinceEpoch' returns an integer which represents the
** number of nanoseconds since some unspecified starting point. This
** function wraps SyNanosecondsSinceEpoch.
**
*/
Obj FuncNanosecondsSinceEpoch(Obj self)
{
Int8 val = SyNanosecondsSinceEpoch();
if(val == -1) {
return Fail;
}
else {
return ObjInt_Int8(val);
}
}
/****************************************************************************
**
*F FuncNanosecondsSinceEpochInfo( <self> )
**
** 'FuncNanosecondsSinceEpochInformation' returns a plain record
** contains information about the timers used for FuncNanosecondsSinceEpoch
**
*/
Obj FuncNanosecondsSinceEpochInfo(Obj self)
{
Obj res, tmp;
Int8 resolution;
res = NEW_PREC(4);
/* Note this has to be "DYN" since we're not passing a
literal but a const char * */
tmp = MakeImmString(SyNanosecondsSinceEpochMethod);
AssPRec(res, RNamName("Method"), tmp);
AssPRec(res, RNamName("Monotonic"),
SyNanosecondsSinceEpochMonotonic ? True : False);
resolution = SyNanosecondsSinceEpochResolution();
if (resolution > 0) {
AssPRec(res, RNamName("Resolution"), ObjInt_Int8(resolution));
AssPRec(res, RNamName("Reliable"), True);
} else if (resolution <= 0) {
AssPRec(res, RNamName("Resolution"), ObjInt_Int8(-resolution));
AssPRec(res, RNamName("Reliable"), False);
}
return res;
}
/****************************************************************************
**
*F FuncSizeScreen( <self>, <args> ) . . . . internal function 'SizeScreen'
**
** 'FuncSizeScreen' implements the internal function 'SizeScreen' to get
** or set the actual screen size.
**
** 'SizeScreen()'
**
** In this form 'SizeScreen' returns the size of the screen as a list with
** two entries. The first is the length of each line, the second is the
** number of lines.
**
** 'SizeScreen( [ <x>, <y> ] )'
**
** In this form 'SizeScreen' sets the size of the screen. <x> is the length
** of each line, <y> is the number of lines. Either value may be missing,
** to leave this value unaffected. Note that those parameters can also be
** set with the command line options '-x <x>' and '-y <y>'.
*/
Obj FuncSizeScreen (
Obj self,
Obj args )
{
Obj size; /* argument and result list */
Obj elm; /* one entry from size */
UInt len; /* length of lines on the screen */
UInt nr; /* number of lines on the screen */
/* check the arguments */
while ( ! IS_SMALL_LIST(args) || 1 < LEN_LIST(args) ) {
args = ErrorReturnObj(
"Function: number of arguments must be 0 or 1 (not %d)",
LEN_LIST(args), 0L,
"you can replace the argument list <args> via 'return <args>;'" );
}
/* get the arguments */
if ( LEN_LIST(args) == 0 ) {
size = NEW_PLIST( T_PLIST, 0 );
SET_LEN_PLIST( size, 0 );
}
/* otherwise check the argument */
else {
size = ELM_LIST( args, 1 );
while ( ! IS_SMALL_LIST(size) || 2 < LEN_LIST(size) ) {
size = ErrorReturnObj(
"SizeScreen: <size> must be a list of length 2",
0L, 0L,
"you can replace <size> via 'return <size>;'" );
}
}
/* extract the length */
if ( LEN_LIST(size) < 1 || ELM0_LIST(size,1) == 0 ) {
len = 0;
}
else {
elm = ELMW_LIST(size,1);
while ( !IS_INTOBJ(elm) ) {
elm = ErrorReturnObj(
"SizeScreen: <x> must be an integer",
0L, 0L,
"you can replace <x> via 'return <x>;'" );
}
len = INT_INTOBJ( elm );
if ( len < 20 ) len = 20;
if ( MAXLENOUTPUTLINE < len ) len = MAXLENOUTPUTLINE;
}
/* extract the number */
if ( LEN_LIST(size) < 2 || ELM0_LIST(size,2) == 0 ) {
nr = 0;
}
else {
elm = ELMW_LIST(size,2);
while ( !IS_INTOBJ(elm) ) {
elm = ErrorReturnObj(
"SizeScreen: <y> must be an integer",
0L, 0L,
"you can replace <y> via 'return <y>;'" );
}
nr = INT_INTOBJ( elm );
if ( nr < 10 ) nr = 10;
}
/* set length and number */
if (len != 0)
{
SyNrCols = len;
SyNrColsLocked = 1;
}
if (nr != 0)
{
SyNrRows = nr;
SyNrRowsLocked = 1;
}
/* make and return the size of the screen */
size = NEW_PLIST( T_PLIST, 2 );
SET_LEN_PLIST( size, 2 );
SET_ELM_PLIST( size, 1, INTOBJ_INT(SyNrCols) );
SET_ELM_PLIST( size, 2, INTOBJ_INT(SyNrRows) );
return size;
}
/****************************************************************************
**
*F FuncWindowCmd( <self>, <args> ) . . . . . . . . execute a window command
*/
static Obj WindowCmdString;
Obj FuncWindowCmd (
Obj self,
Obj args )
{
Obj tmp;
Obj list;
Int len;
Int n, m;
Int i;
Char * ptr;
const Char * inptr;
Char * qtr;
/* check arguments */
while ( ! IS_SMALL_LIST(args) ) {
args = ErrorReturnObj( "argument list must be a list (not a %s)",
(Int)TNAM_OBJ(args), 0L,
"you can replace the argument list <args> via 'return <args>;'" );
}
tmp = ELM_LIST(args,1);
while ( ! IsStringConv(tmp) || 3 != LEN_LIST(tmp) ) {
while ( ! IsStringConv(tmp) ) {
tmp = ErrorReturnObj( "<cmd> must be a string (not a %s)",
(Int)TNAM_OBJ(tmp), 0L,
"you can replace <cmd> via 'return <cmd>;'" );
}
if ( 3 != LEN_LIST(tmp) ) {
tmp = ErrorReturnObj( "<cmd> must be a string of length 3",
0L, 0L,
"you can replace <cmd> via 'return <cmd>;'" );
}
}
/* compute size needed to store argument string */
len = 13;
for ( i = 2; i <= LEN_LIST(args); i++ )
{
tmp = ELM_LIST( args, i );
while ( !IS_INTOBJ(tmp) && ! IsStringConv(tmp) ) {
tmp = ErrorReturnObj(
"%d. argument must be a string or integer (not a %s)",
i, (Int)TNAM_OBJ(tmp),
"you can replace the argument <arg> via 'return <arg>;'" );
SET_ELM_PLIST( args, i, tmp );
}
if ( IS_INTOBJ(tmp) )
len += 12;
else
len += 12 + LEN_LIST(tmp);
}
if ( SIZE_OBJ(WindowCmdString) <= len ) {
ResizeBag( WindowCmdString, 2*len+1 );
}
/* convert <args> into an argument string */
ptr = (Char*) CSTR_STRING(WindowCmdString);
/* first the command name */
memcpy( ptr, CSTR_STRING( ELM_LIST(args,1) ), 3 + 1 );
ptr += 3;
/* and now the arguments */
for ( i = 2; i <= LEN_LIST(args); i++ )
{
tmp = ELM_LIST(args,i);
if ( IS_INTOBJ(tmp) ) {
*ptr++ = 'I';
m = INT_INTOBJ(tmp);
for ( m = (m<0)?-m:m; 0 < m; m /= 10 )
*ptr++ = (m%10) + '0';
if ( INT_INTOBJ(tmp) < 0 )
*ptr++ = '-';
else
*ptr++ = '+';
}
else {
*ptr++ = 'S';
m = LEN_LIST(tmp);
for ( ; 0 < m; m/= 10 )
*ptr++ = (m%10) + '0';
*ptr++ = '+';
qtr = CSTR_STRING(tmp);
for ( m = LEN_LIST(tmp); 0 < m; m-- )
*ptr++ = *qtr++;
}
}
*ptr = 0;
/* now call the window front end with the argument string */
qtr = CSTR_STRING(WindowCmdString);
inptr = SyWinCmd( qtr, strlen(qtr) );
len = strlen(inptr);
/* now convert result back into a list */
list = NEW_PLIST( T_PLIST, 11 );
SET_LEN_PLIST( list, 0 );
i = 1;
while ( 0 < len ) {
if ( *inptr == 'I' ) {
inptr++;
for ( n=0,m=1; '0' <= *inptr && *inptr <= '9'; inptr++,m *= 10,len-- )
n += (*inptr-'0') * m;
if ( *inptr++ == '-' )
n *= -1;
len -= 2;
AssPlist( list, i, INTOBJ_INT(n) );
}
else if ( *inptr == 'S' ) {
inptr++;
for ( n=0,m=1; '0' <= *inptr && *inptr <= '9'; inptr++,m *= 10,len-- )
n += (*inptr-'0') * m;
inptr++; /* ignore the '+' */
C_NEW_STRING(tmp, n, inptr);
inptr += n;
len -= n+2;
AssPlist( list, i, tmp );
}
else {
ErrorQuit( "unknown return value '%s'", (Int)inptr, 0 );
return 0;
}
i++;
}
/* if the first entry is one signal an error */
if ( ELM_LIST(list,1) == INTOBJ_INT(1) ) {
tmp = MakeString("window system: ");
SET_ELM_PLIST(list, 1, tmp);
SET_LEN_PLIST(list, i - 1);
return CALL_XARGS(Error, list);
}
else {
for ( m = 1; m <= i-2; m++ )
SET_ELM_PLIST( list, m, ELM_PLIST(list,m+1) );
SET_LEN_PLIST( list, i-2 );
return list;
}
}
/****************************************************************************
**
*F * * * * * * * * * * * * * * error functions * * * * * * * * * * * * * * *
*/
/****************************************************************************
**
*F FuncDownEnv( <self>, <level> ) . . . . . . . . . change the environment
*/
void DownEnvInner( Int depth )
{
/* if we are asked to go up ... */
if ( depth < 0 ) {
/* ... we determine which level we are supposed to end up on ... */
depth = STATE(ErrorLLevel) + depth;
if (depth < 0) {
depth = 0;
}
/* ... then go back to the top, and later go down to the appropriate level. */
STATE(ErrorLVars) = STATE(BaseShellContext);
STATE(ErrorLLevel) = 0;
STATE(ShellContext) = STATE(BaseShellContext);
}
/* now go down */
while ( 0 < depth
&& STATE(ErrorLVars) != STATE(BottomLVars)
&& PARENT_LVARS(STATE(ErrorLVars)) != STATE(BottomLVars) ) {
STATE(ErrorLVars) = PARENT_LVARS(STATE(ErrorLVars));
STATE(ErrorLLevel)++;
STATE(ShellContext) = PARENT_LVARS(STATE(ShellContext));
depth--;
}
}
Obj FuncDownEnv (
Obj self,
Obj args )
{
Int depth;
if ( LEN_LIST(args) == 0 ) {
depth = 1;
}
else if ( LEN_LIST(args) == 1 && IS_INTOBJ( ELM_PLIST(args,1) ) ) {
depth = INT_INTOBJ( ELM_PLIST( args, 1 ) );
}
else {
ErrorQuit( "usage: DownEnv( [ <depth> ] )", 0L, 0L );
return (Obj)0;
}
if ( STATE(ErrorLVars) == STATE(BottomLVars) ) {
Pr( "not in any function\n", 0L, 0L );
return (Obj)0;
}
DownEnvInner( depth);
return (Obj)0;
}
Obj FuncUpEnv (
Obj self,
Obj args )
{
Int depth;
if ( LEN_LIST(args) == 0 ) {
depth = 1;
}
else if ( LEN_LIST(args) == 1 && IS_INTOBJ( ELM_PLIST(args,1) ) ) {
depth = INT_INTOBJ( ELM_PLIST( args, 1 ) );
}
else {
ErrorQuit( "usage: UpEnv( [ <depth> ] )", 0L, 0L );
return (Obj)0;
}
if ( STATE(ErrorLVars) == STATE(BottomLVars) ) {
Pr( "not in any function\n", 0L, 0L );
return (Obj)0;
}
DownEnvInner(-depth);
return (Obj)0;
}
Obj FuncCURRENT_STATEMENT_LOCATION(Obj self, Obj context)
{
if (context == STATE(BottomLVars))
return Fail;
Obj func = FUNC_LVARS(context);
GAP_ASSERT(func);
Stat call = STAT_LVARS(context);
if (IsKernelFunction(func)) {
return Fail;
}
Obj body = BODY_FUNC(func);
if (call < OFFSET_FIRST_STAT || call > SIZE_BAG(body) - sizeof(StatHeader)) {
return Fail;
}
Obj currLVars = STATE(CurrLVars);
SWITCH_TO_OLD_LVARS(context);
GAP_ASSERT(call == BRK_CALL_TO());
Obj retlist = Fail;
Int type = TNUM_STAT(call);
if ((FIRST_STAT_TNUM <= type && type <= LAST_STAT_TNUM) ||
(FIRST_EXPR_TNUM <= type && type <= LAST_EXPR_TNUM)) {
Int line = LINE_STAT(call);
Obj filename = GET_FILENAME_BODY(body);
retlist = NEW_PLIST(T_PLIST, 2);
SET_LEN_PLIST(retlist, 2);
SET_ELM_PLIST(retlist, 1, filename);
SET_ELM_PLIST(retlist, 2, INTOBJ_INT(line));
CHANGED_BAG(retlist);
}
SWITCH_TO_OLD_LVARS(currLVars);
return retlist;
}
Obj FuncPRINT_CURRENT_STATEMENT(Obj self, Obj context)
{
if (context == STATE(BottomLVars))
return 0;
Obj func = FUNC_LVARS(context);
GAP_ASSERT(func);