Annotation of OpenXM/src/kan96xx/Kan/stackmachine.c, Revision 1.27
1.27 ! takayama 1: /* $OpenXM: OpenXM/src/kan96xx/Kan/stackmachine.c,v 1.26 2004/09/16 02:22:03 takayama Exp $ */
1.1 maekawa 2: /* stackmachin.c */
3:
4: #include <stdio.h>
5: #include "datatype.h"
6: #include "stackm.h"
7: #include "extern.h"
8: #include "gradedset.h"
9: #include "kclass.h"
10: #include <signal.h>
11: #include <sys/types.h>
12:
13:
14: /* #define OPERAND_STACK_SIZE 2000 */
15: #define OPERAND_STACK_SIZE 30000
16: #define SYSTEM_DICTIONARY_SIZE 200
1.8 takayama 17: /* #define USER_DICTIONARY_SIZE 1223, 3581, 27449 */
18: #define USER_DICTIONARY_SIZE 59359
1.1 maekawa 19: /* The value of USER_DICTIONARY_SIZE must be prime number, because of hashing
20: method */
21: #define ARGV_WORK_MAX (AGLIMIT+100)
22: #define EMPTY (char *)NULL
23:
24:
25: /* global variables */
26: struct object StandardStackA[OPERAND_STACK_SIZE];
27: int StandardStackP = 0;
28: int StandardStackMax = OPERAND_STACK_SIZE;
29: struct operandStack StandardStack;
30: /* Initialization of operandStack will be done in initSystemDictionary(). */
31: #define ERROR_STACK_SIZE 100
32: struct object ErrorStackA[ERROR_STACK_SIZE];
33: int ErrorStackP = 0;
34: int ErrorStackMax = ERROR_STACK_SIZE;
35: struct operandStack ErrorStack;
36: /* Initialization of ErrorStack will be done in initSystemDictionary(). */
37:
38: struct operandStack *CurrentOperandStack = &StandardStack;
39: struct object *OperandStack = StandardStackA;
40: int Osp = 0; /* OperandStack pointer */
41: int OspMax = OPERAND_STACK_SIZE;
42:
43: struct dictionary SystemDictionary[SYSTEM_DICTIONARY_SIZE];
44: int Sdp = 0; /* SystemDictionary pointer */
45: struct dictionary UserDictionary[USER_DICTIONARY_SIZE];
46:
47: struct context StandardContext ;
48: /* Initialization of StructContext will be done in initSystemDictionary(). */
49: /* hashInitialize is done in global.c (initStackmachine()) */
50: struct context *StandardContextp = &StandardContext;
51: struct context *CurrentContextp = &StandardContext;
52: struct context *PrimitiveContextp = &StandardContext;
53:
54:
55: static struct object ObjTmp; /* for poor compiler */
56:
1.16 takayama 57: int Calling_ctrlC_hook = 0;
58:
1.1 maekawa 59: int StandardMacros = 1;
60: int StartAFile = 0;
61: char *StartFile;
62:
63: int StartAString = 0;
64: char *StartString;
65:
66: char *GotoLabel = (char *)NULL;
67: int GotoP = 0;
68:
69: static char *SMacros =
70: #include "smacro.h"
71:
72: static isInteger(char *);
73: static strToInteger(char *);
74: static power(int s,int i);
75: static void pstack(void);
76: static struct object executableStringToExecutableArray(char *str);
77:
78: extern int SerialCurrent;
1.13 takayama 79: extern int QuoteMode;
1.1 maekawa 80:
81: int SGClock = 0;
82: int UserCtrlC = 0;
83: int OXlock = 0;
84: int OXlockSaved = 0;
85:
1.19 takayama 86: char *UD_str;
87: int UD_attr;
88:
1.1 maekawa 89: struct object * newObject()
90: {
91: struct object *r;
92: r = (struct object *)sGC_malloc(sizeof(struct object));
93: if (r == (struct object *)NULL) errorStackmachine("No memory\n");
94: r->tag = 0;
95: (r->lc).ival = 0;
96: (r->rc).ival = 0;
97: return(r);
98: }
99:
100: struct object newObjectArray(size)
1.7 takayama 101: int size;
1.1 maekawa 102: {
103: struct object rob;
104: struct object *op;
105: if (size < 0) return(NullObject);
106: if (size > 0) {
107: op = (struct object *)sGC_malloc(size*sizeof(struct object));
108: if (op == (struct object *)NULL) errorStackmachine("No memory\n");
109: }else{
110: op = (struct object *)NULL;
111: }
112: rob.tag = Sarray;
113: rob.lc.ival = size;
114: rob.rc.op = op;
115: return(rob);
116: }
117:
118: isNullObject(obj)
1.7 takayama 119: struct object obj;
1.1 maekawa 120: {
121: if (obj.tag == 0) return(1);
122: else return(0);
123: }
124:
125: int putSystemDictionary(str,ob)
1.7 takayama 126: char *str; /* key */
127: struct object ob; /* value */
1.1 maekawa 128: {
129: int i;
130: int j;
131: int flag = 0;
132:
133: for (i = Sdp-1; i>=0; i--) {
134: /*printf("Add %d %s\n",i,str);*/
135: if (strcmp(str,(SystemDictionary[i]).key) > 0) {
136: for (j=Sdp-1; j>=i+1; j--) {
1.7 takayama 137: (SystemDictionary[j+1]).key = (SystemDictionary[j]).key;
138: (SystemDictionary[j+1]).obj = (SystemDictionary[j]).obj;
1.1 maekawa 139: }
140: (SystemDictionary[i+1]).key = str;
141: (SystemDictionary[i+1]).obj = ob;
142: flag = 1;
143: break;
144: }
145: }
146: if (!flag) { /* str is the minimum element */
147: for (j=Sdp-1; j>=0; j--) {
148: (SystemDictionary[j+1]).key = (SystemDictionary[j]).key;
149: (SystemDictionary[j+1]).obj = (SystemDictionary[j]).obj;
150: }
151: (SystemDictionary[0]).key = str;
152: (SystemDictionary[0]).obj = ob;
153: }
154: Sdp++;
155: if (Sdp >= SYSTEM_DICTIONARY_SIZE) {
156: warningStackmachine("No space for system dictionary area.\n");
157: Sdp--;
158: return(-1);
159: }
160: return(Sdp-1);
161: }
162:
163: int findSystemDictionary(str)
164: /* only used for primitive functions */
165: /* returns 0, if there is no item. */
166: /* This function assumes that the dictionary is sorted by strcmp() */
167: char *str; /* key */
168: {
169: int first,last,rr,middle;
170:
171: /* binary search */
172: first = 0; last = Sdp-1;
173: while (1) {
174: if (first > last) {
175: return(0);
176: } else if (first == last) {
177: if (strcmp(str,(SystemDictionary[first]).key) == 0) {
1.7 takayama 178: return((SystemDictionary[first]).obj.lc.ival);
1.1 maekawa 179: }else {
1.7 takayama 180: return(0);
1.1 maekawa 181: }
182: } else if (last - first == 1) { /* This case is necessary */
183: if (strcmp(str,(SystemDictionary[first]).key) == 0) {
1.7 takayama 184: return((SystemDictionary[first]).obj.lc.ival);
1.1 maekawa 185: }else if (strcmp(str,(SystemDictionary[last]).key) == 0) {
1.7 takayama 186: return((SystemDictionary[last]).obj.lc.ival);
1.1 maekawa 187: }else return(0);
188: }
189:
190: middle = (first + last)/2;
191: rr = strcmp(str,(SystemDictionary[middle]).key);
192: if (rr < 0) { /* str < middle */
193: last = middle;
194: }else if (rr == 0) {
195: return((SystemDictionary[middle]).obj.lc.ival);
196: }else { /* str > middle */
197: first = middle;
198: }
199: }
200: }
201:
202: int putUserDictionary(str,h0,h1,ob,dic)
1.7 takayama 203: char *str; /* key */
204: int h0,h1; /* Hash values of the key */
205: struct object ob; /* value */
206: struct dictionary *dic;
1.1 maekawa 207: {
208: int x,r;
209: extern int Strict2;
210: x = h0;
211: if (str[0] == '\0') {
212: errorKan1("%s\n","putUserDictionary(): You are defining a value with the null key.");
213: }
214: while (1) {
215: if ((dic[x]).key == EMPTY) break;
216: if (strcmp((dic[x]).key,str) == 0) break;
217: x = (x+h1) % USER_DICTIONARY_SIZE;
218: if (x == h0) {
219: errorStackmachine("User dictionary is full. loop hashing.\n");
220: }
221: }
222: r = x;
223: if (Strict2) {
1.20 takayama 224: switch(((dic[x]).attr) & (PROTECT | ABSOLUTE_PROTECT)) {
1.1 maekawa 225: case PROTECT:
226: r = -PROTECT; /* Protected, but we rewrite it. */
227: break;
228: case ABSOLUTE_PROTECT:
229: r = -ABSOLUTE_PROTECT; /* Protected and we do not rewrite it. */
230: return(r);
231: default:
1.20 takayama 232: /* (dic[x]).attr = 0; */ /* It is not necesarry, I think. */
1.1 maekawa 233: break;
234: }
235: }
236: (dic[x]).key = str;
237: (dic[x]).obj = ob;
238: (dic[x]).h0 = h0;
239: (dic[x]).h1 = h1;
240: return(r);
241: }
242:
243: struct object KputUserDictionary(char *str,struct object ob)
244: {
245: int r;
246: r = putUserDictionary(str,hash0(str),hash1(str),ob,CurrentContextp->userDictionary);
247: return(KpoInteger(r));
248: }
249:
250: struct object findUserDictionary(str,h0,h1,cp)
1.7 takayama 251: /* returns NoObject, if there is no item. */
252: char *str; /* key */
253: int h0,h1; /* The hashing values of the key. */
254: struct context *cp;
1.19 takayama 255: /* Set char *UD_str, int UD_attr (attributes) */
1.1 maekawa 256: {
257: int x;
258: struct dictionary *dic;
1.19 takayama 259: extern char *UD_str;
260: extern int UD_attr;
261: UD_str = NULL; UD_attr = -1;
1.1 maekawa 262: dic = cp->userDictionary;
263: x = h0;
264: while (1) {
265: if ((dic[x]).key == EMPTY) { break; }
266: if (strcmp((dic[x]).key,str) == 0) {
1.19 takayama 267: UD_str = (dic[x]).key; UD_attr = (dic[x]).attr;
1.1 maekawa 268: return( (dic[x]).obj );
269: }
270: x = (x+h1) % USER_DICTIONARY_SIZE;
271: if (x == h0) {
272: errorStackmachine("User dictionary is full. loop hashing in findUserDictionary.\n");
273: }
274: }
275: if (cp->super == (struct context *)NULL) return(NoObject);
276: else return(findUserDictionary(str,h0,h1,cp->super));
277:
278: }
279:
280: struct object KfindUserDictionary(char *str) {
281: return(findUserDictionary(str,hash0(str),hash1(str),CurrentContextp));
282: }
283:
284: int putUserDictionary2(str,h0,h1,attr,dic)
1.7 takayama 285: char *str; /* key */
286: int h0,h1; /* Hash values of the key */
287: int attr; /* attribute field */
288: struct dictionary *dic;
1.1 maekawa 289: {
290: int x;
291: int i;
292: if (SET_ATTR_FOR_ALL_WORDS & attr) {
293: for (i=0; i<USER_DICTIONARY_SIZE; i++) {
294: if ((dic[i]).key !=EMPTY) (dic[i]).attr = attr&(~SET_ATTR_FOR_ALL_WORDS);
1.27 ! takayama 295: }
! 296: return(0);
! 297: }
! 298: if (OR_ATTR_FOR_ALL_WORDS & attr) {
! 299: for (i=0; i<USER_DICTIONARY_SIZE; i++) {
! 300: if ((dic[i]).key !=EMPTY) (dic[i]).attr |= attr&(~OR_ATTR_FOR_ALL_WORDS);
1.1 maekawa 301: }
302: return(0);
303: }
304: x = h0;
305: if (str[0] == '\0') {
306: errorKan1("%s\n","putUserDictionary2(): You are defining a value with the null key.");
307: }
308: while (1) {
309: if ((dic[x]).key == EMPTY) return(-1);
310: if (strcmp((dic[x]).key,str) == 0) break;
311: x = (x+h1) % USER_DICTIONARY_SIZE;
312: if (x == h0) {
313: errorStackmachine("User dictionary is full. loop hashing.\n");
314: }
315: }
316: (dic[x]).attr = attr;
317: return(x);
318: }
319:
320:
321: int putPrimitiveFunction(str,number)
1.7 takayama 322: char *str;
323: int number;
1.1 maekawa 324: {
325: struct object ob;
326: ob.tag = Soperator;
327: ob.lc.ival = number;
328: return(putSystemDictionary(str,ob));
329: }
330:
331: struct tokens lookupTokens(t)
1.7 takayama 332: struct tokens t;
1.1 maekawa 333: {
334: struct object *left;
335: struct object *right;
336: t.object.tag = Slist;
337: left = t.object.lc.op = newObject();
338: right = t.object.rc.op = newObject();
339: left->tag = Sinteger;
340: (left->lc).ival = hash0(t.token);
341: (left->rc).ival = hash1(t.token);
342: right->tag = Sinteger;
343: (right->lc).ival = findSystemDictionary(t.token);
344: return(t);
345: }
346:
347: struct object lookupLiteralString(s)
1.7 takayama 348: char *s; /* s must be a literal string */
1.1 maekawa 349: {
350: struct object ob;
351: ob.tag = Slist;
352: ob.lc.op = newObject();
353: ob.rc.op = (struct object *)NULL;
354: ob.lc.op->tag = Sinteger;
355: (ob.lc.op->lc).ival = hash0(&(s[1]));
356: (ob.lc.op->rc).ival = hash1(&(s[1]));
357: return(ob);
358: }
359:
360:
361: int hash0(str)
1.7 takayama 362: char *str;
1.1 maekawa 363: {
364: int h=0;
365: while (*str != '\0') {
1.17 takayama 366: h = ((h*128)+((unsigned char)(*str))) % USER_DICTIONARY_SIZE;
1.1 maekawa 367: str++;
368: }
369: return(h);
370: }
371:
372: int hash1(str)
1.7 takayama 373: char *str;
1.1 maekawa 374: {
1.17 takayama 375: return(8-((unsigned char)(str[0])%8));
1.1 maekawa 376: }
377:
378: void hashInitialize(struct dictionary *dic)
379: {
380: int i;
381: for (i=0; i<USER_DICTIONARY_SIZE; i++) {
382: (dic[i]).key = EMPTY; (dic[i]).attr = 0;
383: }
384: }
385:
386: static isInteger(str)
1.7 takayama 387: char *str;
1.1 maekawa 388: {
389: int i;
390: int n;
391: int start;
392:
393: n = strlen(str);
394: if ((str[0] == '+') || (str[0] == '-'))
395: start = 1;
396: else
397: start = 0;
398: if (start >= n) return(0);
399:
400: for (i=start; i<n; i++) {
401: if (('0' <= str[i]) && (str[i] <= '9')) ;
402: else return(0);
403: }
404: return(1);
405: }
406:
407: static strToInteger(str)
1.7 takayama 408: char *str;
1.1 maekawa 409: {
410: int i;
411: int n;
412: int r;
413: int start;
414:
415: if ((str[0] == '+') || (str[0] == '-'))
416: start = 1;
417: else
418: start = 0;
419: n = strlen(str);
420: r = 0;
421: for (i=n-1; i>=start ; i--) {
422: r += (int)(str[i]-'0') *power(10,n-1-i);
423: }
424: if (str[0] == '-') r = -r;
425: return(r);
426: }
427:
428: static power(s,i)
1.7 takayama 429: int s;
430: int i;
1.1 maekawa 431: {
432: if (i == 0) return 1;
433: else return( s*power(s,i-1) );
434: }
435:
436: int Kpush(ob)
1.7 takayama 437: struct object ob;
1.1 maekawa 438: {
439: OperandStack[Osp++] = ob;
440: if (Osp >= OspMax) {
441: warningStackmachine("Operand stack overflow. \n");
442: Osp--;
443: return(-1);
444: }
445: return(0);
446: }
447:
448: struct object Kpop()
449: {
450: if (Osp <= 0) {
451: return( NullObject );
452: }else{
453: return( OperandStack[--Osp]);
454: }
455: }
456:
457: struct object peek(k)
1.7 takayama 458: int k;
1.1 maekawa 459: {
460: if ((Osp-k-1) < 0) {
461: return( NullObject );
462: }else{
463: return( OperandStack[Osp-k-1]);
464: }
465: }
466:
467:
468: struct object newOperandStack(int size)
469: {
470: struct operandStack *os ;
471: struct object ob;
472: os = (struct operandStack *)sGC_malloc(sizeof(struct operandStack));
473: if (os == (void *)NULL) errorStackmachine("No more memory.");
474: if (size <= 0) errorStackmachine("Size of stack must be more than 1.");
475: os->size = size;
476: os->sp = 0;
477: os->ostack = (struct object *)sGC_malloc(sizeof(struct object)*(size+1));
478: if (os->ostack == (void *)NULL) errorStackmachine("No more memory.");
479: ob.tag = Sclass;
480: ob.lc.ival = CLASSNAME_OPERANDSTACK;
481: ob.rc.voidp = os;
482: return(ob);
483: }
484:
485: void setOperandStack(struct object ob) {
486: if (ob.tag != Sclass) errorStackmachine("The argument must be class.");
487: if (ob.lc.ival != CLASSNAME_OPERANDSTACK)
488: errorStackmachine("The argument must be class.OperandStack.");
489: CurrentOperandStack->ostack = OperandStack;
490: CurrentOperandStack->sp = Osp;
491: CurrentOperandStack->size = OspMax;
492: OperandStack = ((struct operandStack *)(ob.rc.voidp))->ostack;
493: Osp = ((struct operandStack *)(ob.rc.voidp))->sp;
494: OspMax = ((struct operandStack *)(ob.rc.voidp))->size;
495: CurrentOperandStack = ob.rc.voidp;
496: }
497:
498: void stdOperandStack(void) {
499: CurrentOperandStack->ostack = OperandStack;
500: CurrentOperandStack->sp = Osp;
501: CurrentOperandStack->size = OspMax;
502:
503: CurrentOperandStack = &StandardStack;
504: OperandStack = CurrentOperandStack->ostack;
505: Osp = CurrentOperandStack->sp;
506: OspMax = CurrentOperandStack->size;
507: }
508:
509: /* functions to handle contexts. */
510: void fprintContext(FILE *fp,struct context *cp) {
511: if (cp == (struct context *)NULL) {
512: fprintf(fp," Context=NIL \n");
513: return;
514: }
515: fprintf(fp," ContextName = %s, ",cp->contextName);
516: fprintf(fp,"Super = ");
517: if (cp->super == (struct context *)NULL) fprintf(fp,"NIL");
518: else {
519: fprintf(fp,"%s",cp->super->contextName);
520: }
521: fprintf(fp,"\n");
522: }
523:
524: struct context *newContext0(struct context *super,char *name) {
525: struct context *cp;
526: cp = sGC_malloc(sizeof(struct context));
527: if (cp == (struct context *)NULL) errorStackmachine("No memory (newContext0)");
528: cp->userDictionary=sGC_malloc(sizeof(struct dictionary)*USER_DICTIONARY_SIZE);
529: if (cp->userDictionary==(struct dictionary *)NULL)
530: errorStackmachine("No memory (newContext0)");
531: hashInitialize(cp->userDictionary);
532: cp->contextName = name;
533: cp->super = super;
534: return(cp);
535: }
536:
537: void KsetContext(struct object contextObj) {
538: if (contextObj.tag != Sclass) {
539: errorStackmachine("Usage:setcontext");
540: }
541: if (contextObj.lc.ival != CLASSNAME_CONTEXT) {
542: errorStackmachine("Usage:setcontext");
543: }
544: if (contextObj.rc.voidp == NULL) {
545: errorStackmachine("You cannot set NullContext to the CurrentContext.");
546: }
547: CurrentContextp = (struct context *)(contextObj.rc.voidp);
548: }
549:
550:
551: struct object getSuperContext(struct object contextObj) {
552: struct object rob;
553: struct context *cp;
554: if (contextObj.tag != Sclass) {
555: errorStackmachine("Usage:supercontext");
556: }
557: if (contextObj.lc.ival != CLASSNAME_CONTEXT) {
558: errorStackmachine("Usage:supercontext");
559: }
560: cp = (struct context *)(contextObj.rc.voidp);
561: if (cp->super == (struct context *)NULL) {
562: return(NullObject);
563: }else{
564: rob.tag = Sclass;
565: rob.lc.ival = CLASSNAME_CONTEXT;
566: rob.rc.voidp = cp->super;
567: }
568: return(rob);
569: }
570:
571: #define CSTACK_SIZE 1000
572: void contextControl(actionOfContextControl ctl) {
573: static struct context *cstack[CSTACK_SIZE];
574: static int cstackp = 0;
575: switch(ctl) {
576: case CCRESTORE:
577: if (cstackp == 0) return;
578: else {
579: CurrentContextp = cstack[0];
580: cstackp = 0;
581: }
582: break;
583: case CCPUSH:
584: if (cstackp < CSTACK_SIZE) {
585: cstack[cstackp] = CurrentContextp;
586: cstackp++;
587: }else{
588: contextControl(CCRESTORE);
589: errorStackmachine("Context stack (cstack) is overflow. CurrentContext is restored.\n");
590: }
591: break;
592: case CCPOP:
593: if (cstackp > 0) {
594: cstackp--;
595: CurrentContextp = cstack[cstackp];
596: }
597: break;
598: default:
599: break;
600: }
601: return;
602: }
603:
604:
605:
606: int isLiteral(str)
1.7 takayama 607: char *str;
1.1 maekawa 608: {
609: if (strlen(str) <2) return(0);
610: else {
611: if ((str[0] == '/') && (str[1] != '/')) return(1);
612: else return(0);
613: }
614: }
615:
616: void printOperandStack() {
617: int i;
618: struct object ob;
619: int vs;
620: vs = VerboseStack; VerboseStack = 2;
621: for (i=Osp-1; i>=0; i--) {
622: fprintf(Fstack,"[%d] ",i);
623: ob = OperandStack[i];
624: printObject(ob,1,Fstack);
625: }
626: VerboseStack = vs;
627: }
628:
629:
630:
631: static initSystemDictionary()
1.7 takayama 632: {
1.1 maekawa 633: StandardStack.ostack = StandardStackA;
634: StandardStack.sp = StandardStackP;
635: StandardStack.size = OPERAND_STACK_SIZE;
636:
637: ErrorStack.ostack = ErrorStackA;
638: ErrorStack.sp = ErrorStackP;
639: ErrorStack.size = ErrorStackMax;
640:
641: StandardContext.userDictionary = UserDictionary;
642: StandardContext.contextName = "StandardContext";
643: StandardContext.super = (struct context *)NULL;
644:
645: KdefinePrimitiveFunctions();
646:
1.7 takayama 647: }
1.1 maekawa 648:
649: struct object showSystemDictionary(int f) {
650: int i;
651: int maxl;
652: char format[1000];
653: int nl;
654: struct object rob;
655: rob = NullObject;
656: if (f != 0) {
657: rob = newObjectArray(Sdp);
658: for (i=0; i<Sdp; i++) {
659: putoa(rob,i,KpoString((SystemDictionary[i]).key));
660: }
661: return(rob);
662: }
663: maxl = 1;
664: for (i=0; i<Sdp; i++) {
665: if (strlen((SystemDictionary[i]).key) >maxl)
666: maxl = strlen((SystemDictionary[i]).key);
667: }
668: maxl += 3;
669: nl = 80/maxl;
670: if (nl < 2) nl = 2;
671: sprintf(format,"%%-%ds",maxl);
672: for (i=0; i<Sdp; i++) {
673: fprintf(Fstack,format,(SystemDictionary[i]).key);
674: if (i % nl == nl-1) fprintf(Fstack,"\n");
675: }
676: fprintf(Fstack,"\n");
677: return(rob);
678: }
679:
680: int showUserDictionary()
681: {
682: int i,j;
683: int maxl;
684: char format[1000];
685: int nl;
686: struct dictionary *dic;
687: dic = CurrentContextp->userDictionary;
688: fprintf(Fstack,"DictionaryName=%s, super= ",CurrentContextp->contextName);
689: if (CurrentContextp->super == (struct context *)NULL) {
690: fprintf(Fstack,"NIL\n");
691: }else{
692: fprintf(Fstack,"%s\n",CurrentContextp->super->contextName);
693: }
694: maxl = 1;
695: for (i=0; i<USER_DICTIONARY_SIZE; i++) {
696: if ((dic[i]).key != EMPTY) {
697: if (strlen((dic[i]).key) >maxl)
1.7 takayama 698: maxl = strlen((dic[i]).key);
1.1 maekawa 699: }
700: }
701: maxl += 3;
702: nl = 80/maxl;
703: if (nl < 2) nl = 2;
704: sprintf(format,"%%-%ds",maxl);
705: for (i=0,j=0; i<USER_DICTIONARY_SIZE; i++) {
706: if ((dic[i]).key != EMPTY) {
707: fprintf(Fstack,format,(dic[i]).key);
708: /*{ char *sss; int ii,h0,h1;
1.7 takayama 709: sss = dic[i].key;
710: h0 = dic[i].h0;
711: h1 = dic[i].h1;
712: for (ii=0; ii<strlen(sss); ii++) fprintf(Fstack,"%x ",sss[ii]);
713: fprintf(Fstack,": h0=%d, h1=%d, %d\n",h0,h1,i);
714: }*/
1.1 maekawa 715: if (j % nl == nl-1) fprintf(Fstack,"\n");
716: j++;
717: }
718: }
719: fprintf(Fstack,"\n");
720: }
721:
722:
723: static struct object executableStringToExecutableArray(s)
1.7 takayama 724: char *s;
1.1 maekawa 725: {
726: struct tokens *tokenArray;
727: struct object ob;
728: int i;
729: int size;
730: tokenArray = decomposeToTokens(s,&size);
731: ob.tag = SexecutableArray;
732: ob.lc.tokenArray = tokenArray;
733: ob.rc.ival = size;
734: for (i=0; i<size; i++) {
735: if ( ((ob.lc.tokenArray)[i]).kind == EXECUTABLE_STRING) {
736: ((ob.lc.tokenArray)[i]).kind = EXECUTABLE_ARRAY;
737: ((ob.lc.tokenArray)[i]).object =
1.7 takayama 738: executableStringToExecutableArray(((ob.lc.tokenArray)[i]).token);
1.1 maekawa 739: }
740: }
741: return(ob);
742: }
743: /**************** stack machine **************************/
744: void scanner() {
745: struct tokens token;
746: struct object ob;
747: extern int Quiet;
748: extern void ctrlC();
1.22 takayama 749: int tmp, status;
1.1 maekawa 750: char *tmp2;
751: extern int ErrorMessageMode;
752: int jval;
1.14 takayama 753: extern int InSendmsg2;
1.22 takayama 754: int infixOn = 0;
755: struct tokens infixToken;
1.1 maekawa 756: getokenSM(INIT);
757: initSystemDictionary();
758:
1.9 takayama 759: #if defined(__CYGWIN__)
760: if (sigsetjmp(EnvOfStackMachine,1)) {
761: #else
1.1 maekawa 762: if (setjmp(EnvOfStackMachine)) {
1.9 takayama 763: #endif
1.1 maekawa 764: /* do nothing in the case of error */
765: fprintf(stderr,"An error or interrupt in reading macros, files and command strings.\n");
766: exit(10);
767: } else { }
768: if (signal(SIGINT,SIG_IGN) != SIG_IGN) {
769: signal(SIGINT,ctrlC);
770: }
771:
772: /* setup quiet mode or not */
1.21 takayama 773: token.kind = EXECUTABLE_STRING; token.tflag = 0;
1.1 maekawa 774: if (Quiet) {
775: token.token = " /@@@.quiet 1 def ";
776: }else {
777: token.token = " /@@@.quiet 0 def ";
778: }
779: executeToken(token); /* execute startup commands */
1.21 takayama 780: token.kind = ID; token.tflag = 0;
1.1 maekawa 781: token.token = "exec";
782: token = lookupTokens(token); /* set hashing values */
783: tmp = findSystemDictionary(token.token);
784: ob.tag = Soperator;
785: ob.lc.ival = tmp;
786: executePrimitive(ob); /* exec */
787:
788:
789: KSdefineMacros();
790:
791: if (StartAFile) {
792: tmp2 = StartFile;
793: StartFile = (char *)sGC_malloc(sizeof(char)*(strlen(StartFile)+
1.7 takayama 794: 40));
1.1 maekawa 795: sprintf(StartFile,"$%s$ run\n",tmp2);
1.21 takayama 796: token.kind = EXECUTABLE_STRING; token.tflag = 0;
1.1 maekawa 797: token.token = StartFile;
1.7 takayama 798: executeToken(token); /* execute startup commands */
1.21 takayama 799: token.kind = ID; token.tflag = 0;
1.1 maekawa 800: token.token = "exec";
801: token = lookupTokens(token); /* set hashing values */
802: tmp = findSystemDictionary(token.token);
803: ob.tag = Soperator;
804: ob.lc.ival = tmp;
1.7 takayama 805: executePrimitive(ob); /* exec */
1.1 maekawa 806: }
807:
808: if (StartAString) {
1.21 takayama 809: token.kind = EXECUTABLE_STRING; token.tflag = 0;
1.1 maekawa 810: token.token = StartString;
1.7 takayama 811: executeToken(token); /* execute startup commands */
1.21 takayama 812: token.kind = ID; token.tflag = 0;
1.1 maekawa 813: token.token = "exec";
814: token = lookupTokens(token); /* set hashing values */
815: tmp = findSystemDictionary(token.token);
816: ob.tag = Soperator;
817: ob.lc.ival = tmp;
1.7 takayama 818: executePrimitive(ob); /* exec */
1.1 maekawa 819: }
820:
821:
822: for (;;) {
1.9 takayama 823: #if defined(__CYGWIN__)
824: if (jval=sigsetjmp(EnvOfStackMachine,1)) {
825: #else
1.1 maekawa 826: if (jval=setjmp(EnvOfStackMachine)) {
1.9 takayama 827: #endif
1.1 maekawa 828: /* *** The following does not work properly. ****
1.7 takayama 829: if (jval == 2) {
830: if (ErrorMessageMode == 1 || ErrorMessageMode == 2) {
831: pushErrorStack(KnewErrorPacket(SerialCurrent,-1,"User interrupt by ctrl-C."));
832: }
833: }
834: **** */
1.1 maekawa 835: if (DebugStack >= 1) {
1.7 takayama 836: fprintf(Fstack,"\nscanner> ");
1.1 maekawa 837: }
1.16 takayama 838: if (!Calling_ctrlC_hook) { /* to avoid recursive call of ctrlC-hook. */
839: Calling_ctrlC_hook = 1;
840: KSexecuteString(" ctrlC-hook "); /* Execute User Defined functions. */
841: }
842: Calling_ctrlC_hook = 0;
1.12 takayama 843: KSexecuteString(" (Computation is interrupted.) "); /* move to ctrlC-hook? */
1.14 takayama 844: InSendmsg2 = 0;
1.22 takayama 845: infixOn = 0;
1.7 takayama 846: continue ;
1.1 maekawa 847: } else { }
848: if (DebugStack >= 1) { printOperandStack(); }
1.22 takayama 849: token = getokenSM(GET);
850: if ((status=executeToken(token)) < 0) break;
851: /***if (status == 1) fprintf(stderr," --- exit --- \n");*/
852: /* fprintf(stderr,"token.token=%s, status=%d, infixOn=%d\n",token.token,status,infixOn); */
853: if (status & STATUS_INFIX) {
854: infixOn = 1; infixToken = token; infixToken.tflag |= NO_DELAY;
855: }else if (infixOn) {
856: infixOn = 0;
857: if ((status=executeToken(infixToken)) < 0) break;
858: }
1.1 maekawa 859: }
860: }
861:
862:
863: void ctrlC(sig)
1.7 takayama 864: int sig;
1.1 maekawa 865: {
866: extern void ctrlC();
867: extern int ErrorMessageMode;
868: extern int SGClock;
869: extern int UserCtrlC;
870: extern int OXlock;
1.14 takayama 871:
1.1 maekawa 872: signal(sig,SIG_IGN);
873: /* see 133p */
1.10 takayama 874: cancelAlarm();
875: if (sig == SIGALRM) {
876: fprintf(stderr,"ctrlC by SIGALRM\n");
877: }
1.1 maekawa 878:
879: if (SGClock) {
880: UserCtrlC = 1;
881: fprintf(stderr,"ctrl-c is locked because of gc.\n");
1.10 takayama 882: signal(sig,ctrlC); if (sig == SIGALRM) alarm((unsigned int)10);
1.1 maekawa 883: return;
884: }
885: if (OXlock) {
886: if (UserCtrlC > 0) UserCtrlC++;
887: else UserCtrlC = 1;
888: if (UserCtrlC > 3) {
889: fprintf(stderr,"OK. You are eager to cancel the computation.\n");
890: fprintf(stderr,"You should close the ox communication cannel.\n");
891: signal(SIGINT,ctrlC);
892: unlockCtrlCForOx();
893: }
894: fprintf(stderr,"ctrl-c is locked because of ox lock %d.\n",UserCtrlC);
1.10 takayama 895: signal(sig,ctrlC); if (sig == SIGALRM) alarm((unsigned int)10);
1.1 maekawa 896: return;
897: }
898: if (ErrorMessageMode != 1) {
1.16 takayama 899: (void *) traceShowStack();
1.1 maekawa 900: fprintf(Fstack,"User interruption by ctrl-C. We are in the top-level.\n");
901: fprintf(Fstack,"Type in quit in order to exit sm1.\n");
902: }
1.16 takayama 903: traceClearStack();
1.1 maekawa 904: if (GotoP) {
905: fprintf(Fstack,"The interpreter was looking for the label <<%s>>. It is also aborted.\n",GotoLabel);
906: GotoP = 0;
907: }
908: stdOperandStack(); contextControl(CCRESTORE);
909: /*fprintf(Fstack,"Warning! The handler of ctrl-C has a bug, so you might have a core-dump.\n");*/
910: /*
911: $(x0+1)^50$ $x1 x0 + x1^20$ 2 groebner_n
912: ctrl-C
913: $(x0+1)^50$ $x1 x0 + x1^20$ 2 groebner_n
914: It SOMETIMES makes core dump.
915: */
916: getokenSM(INIT); /* It might fix the bug above. 1992/11/14 */
917: signal(SIGINT,ctrlC);
1.9 takayama 918: #if defined(__CYGWIN__)
919: siglongjmp(EnvOfStackMachine,2);
920: #else
1.1 maekawa 921: longjmp(EnvOfStackMachine,2); /* returns 2 for ctrl-C */
1.9 takayama 922: #endif
1.1 maekawa 923: }
924:
925: int executeToken(token)
1.7 takayama 926: struct tokens token;
1.1 maekawa 927: {
928: struct object ob;
929: int primitive;
930: int size;
931: int status;
932: int i,h0,h1;
933: extern int WarningMessageMode;
934: extern int Strict;
1.14 takayama 935: extern int InSendmsg2;
1.1 maekawa 936:
937: if (GotoP) { /* for goto */
938: if (token.kind == ID && isLiteral(token.token)) {
939: if (strcmp(&((token.token)[1]),GotoLabel) == 0) {
1.7 takayama 940: GotoP = 0;
941: return(0); /* normal exit */
1.1 maekawa 942: }
943: }
944: return(0); /* normal exit */
945: }
946: if (token.kind == DOLLAR) {
947: ob.tag = Sdollar;
948: ob.lc.str = token.token;
949: Kpush(ob);
950: } else if (token.kind == ID) { /* ID */
951:
952: if (strcmp(token.token,"exit") == 0) return(1);
953: /* "exit" is not primitive here. */
954:
955: if (isLiteral(token.token)) {
956: /* literal object */
957: ob.tag = Sstring;
958: ob.lc.str = (char *)sGC_malloc((strlen(token.token)+1)*sizeof(char));
959: if (ob.lc.str == (char *)NULL) errorStackmachine("No space.");
960: strcpy(ob.lc.str, &((token.token)[1]));
961:
962: if (token.object.tag != Slist) {
1.7 takayama 963: fprintf(Fstack,"\n%%Warning: The hashing values for the <<%s>> are not set.\n",token.token);
964: token.object = lookupLiteralString(token.token);
1.1 maekawa 965: }
966: ob.rc.op = token.object.lc.op;
967: Kpush(ob);
968: } else if (isInteger(token.token)) {
969: /* integer object */
970: ob.tag = Sinteger ;
971: ob.lc.ival = strToInteger(token.token);
972: Kpush(ob);
973: } else {
974: if (token.object.tag != Slist) {
1.7 takayama 975: fprintf(Fstack,"\n%%Warning: The hashing values for the <<%s>> are not set.\n",token.token);
976: token = lookupTokens(token);
1.1 maekawa 977: }
978: h0 = ((token.object.lc.op)->lc).ival;
979: h1 = ((token.object.lc.op)->rc).ival;
980: ob=findUserDictionary(token.token,h0,h1,CurrentContextp);
981: primitive = ((token.object.rc.op)->lc).ival;
1.22 takayama 982: if (!(token.tflag & NO_DELAY)) {
983: if ((ob.tag >= 0) && (UD_attr & ATTR_INFIX)) {
1.24 takayama 984: return STATUS_INFIX;
1.22 takayama 985: }
986: }
1.1 maekawa 987: if (ob.tag >= 0) {
1.7 takayama 988: /* there is a definition in the user dictionary */
989: if (ob.tag == SexecutableArray) {
1.25 takayama 990: status = executeExecutableArray(ob,token.token,0);
1.24 takayama 991: if ((status & STATUS_BREAK) || (status < 0)) return status;
1.7 takayama 992: }else {
993: Kpush(ob);
994: }
1.1 maekawa 995: } else if (primitive) {
1.15 takayama 996: tracePushName(token.token);
1.7 takayama 997: /* system operator */
998: ob.tag = Soperator;
999: ob.lc.ival = primitive;
1.15 takayama 1000: status = executePrimitive(ob);
1.18 takayama 1001: tracePopName();
1.15 takayama 1002: return(status);
1.1 maekawa 1003: } else {
1.14 takayama 1004: if (QuoteMode) {
1005: if (InSendmsg2) return(DO_QUOTE);
1006: else {
1007: Kpush(KpoString(token.token));
1008: return(0); /* normal exit.*/
1009: }
1.13 takayama 1010: }
1.7 takayama 1011: if (WarningMessageMode == 1 || WarningMessageMode == 2) {
1012: char tmpc[1024];
1013: if (strlen(token.token) < 900) {
1014: sprintf(tmpc,"\n%%Warning: The identifier <<%s>> is not in the system dictionary\n%% nor in the user dictionaries. Push NullObject.\n",token.token);
1015: }else {strcpy(tmpc,"Warning: identifier is not in the dictionaries.");}
1016: pushErrorStack(KnewErrorPacket(SerialCurrent,-1,tmpc));
1017: }
1018: if (WarningMessageMode != 1) {
1019: fprintf(Fstack,"\n%%Warning: The identifier <<%s>> is not in the system dictionary\n%% nor in the user dictionaries. Push NullObject.\n",token.token);
1020: /*fprintf(Fstack,"(%d,%d)\n",h0,h1);*/
1021: }
1022: if (Strict) {
1023: errorStackmachine("Warning: identifier is not in the dictionaries");
1024: }
1025: Kpush(NullObject);
1.1 maekawa 1026: }
1027: }
1028: } else if (token.kind == EXECUTABLE_STRING) {
1029: Kpush(executableStringToExecutableArray(token.token));
1030: } else if (token.kind == EXECUTABLE_ARRAY) {
1031: Kpush(token.object);
1032: } else if ((token.kind == -1) || (token.kind == -2)) { /* eof token */
1033: return(-1);
1034: } else {
1035: /*fprintf(Fstack,"\n%%Error: Unknown token type\n");***/
1036: fprintf(stderr,"\nUnknown token type = %d\n",token.kind);
1037: fprintf(stderr,"\ntype in ctrl-\\ if you like to make core-dump.\n");
1038: fprintf(stderr,"If you like to continue, type in RETURN key.\n");
1039: fprintf(stderr,"Note that you cannot input null string.\n");
1040: getchar();
1041: errorStackmachine("Error: Unknown token type.\n");
1042: /* return(-2); /* exit */
1043: }
1044: return(0); /* normal exit */
1045: }
1046:
1047:
1048:
1049:
1050: errorStackmachine(str)
1.7 takayama 1051: char *str;
1.1 maekawa 1052: {
1053: int i,j,k;
1054: static char *u="Usage:";
1055: char message0[1024];
1056: char *message;
1057: extern int ErrorMessageMode;
1.10 takayama 1058: cancelAlarm();
1.1 maekawa 1059: if (ErrorMessageMode == 1 || ErrorMessageMode == 2) {
1060: pushErrorStack(KnewErrorPacket(SerialCurrent,-1,str));
1061: }
1062: if (ErrorMessageMode != 1) {
1063: message = message0;
1064: i = 0;
1065: while (i<6 && str[i]!='0') {
1066: if (str[i] != u[i]) break;
1067: i++;
1068: }
1069: if (i==6) {
1070: fprintf(stderr,"ERROR(sm): \n");
1071: while (str[i] != '\0' && str[i] != ' ') {
1.7 takayama 1072: i++;
1.1 maekawa 1073: }
1074: if (str[i] == ' ') {
1.7 takayama 1075: fprintf(stderr," %s\n",&(str[i+1]));
1076: k = 0;
1077: if (i-6 > 1022) message = (char *)sGC_malloc(sizeof(char)*i);
1078: for (j=6; j<i ; j++) {
1079: message[k] = str[j];
1080: message[k+1] = '\0';
1081: k++;
1082: }
1083: Kusage2(stderr,message);
1.1 maekawa 1084: }else{
1.7 takayama 1085: Kusage2(stderr,&(str[6]));
1.1 maekawa 1086: }
1087: }else {
1088: fprintf(stderr,"ERROR(sm): ");
1089: fprintf(stderr,str);
1090: }
1091: fprintf(stderr,"\n");
1.16 takayama 1092: (void) traceShowStack();
1.1 maekawa 1093: }
1.16 takayama 1094: traceClearStack();
1.1 maekawa 1095: if (GotoP) {
1096: fprintf(Fstack,"The interpreter was looking for the label <<%s>>. It is also aborted.\n",GotoLabel);
1097: GotoP = 0;
1098: }
1099: stdOperandStack(); contextControl(CCRESTORE);
1100: getokenSM(INIT); /* It might fix the bug. 1996/3/10 */
1101: /* fprintf(stderr,"Now, Long jump!\n"); */
1102: longjmp(EnvOfStackMachine,1);
1103: }
1104:
1105: warningStackmachine(str)
1.7 takayama 1106: char *str;
1.1 maekawa 1107: {
1108: extern int WarningMessageMode;
1109: extern int Strict;
1110: if (WarningMessageMode == 1 || WarningMessageMode == 2) {
1111: pushErrorStack(KnewErrorPacket(SerialCurrent,-1,str));
1112: }
1113: if (WarningMessageMode != 1) {
1114: fprintf(stderr,"WARNING(sm): ");
1115: fprintf(stderr,str);
1116: }
1117: if (Strict) errorStackmachine(" ");
1118: return(0);
1119: }
1120:
1121:
1122: /* exports */
1123: /* NOTE: If you call this function and an error occured,
1124: you have to reset the jump buffer by setjmp(EnvOfStackMachine).
1125: cf. kxx/memo1.txt, kxx/stdserver00.c 1998, 2/6 */
1126: KSexecuteString(s)
1.7 takayama 1127: char *s;
1.1 maekawa 1128: {
1129: struct tokens token;
1130: struct object ob;
1131: int tmp;
1132: extern int CatchCtrlC;
1133: int jval;
1134: static int recursive = 0;
1135: extern int ErrorMessageMode;
1136: extern int KSPushEnvMode;
1137: jmp_buf saved_EnvOfStackMachine;
1138: void (*sigfunc)();
1139: int localCatchCtrlC ;
1140:
1141: localCatchCtrlC = CatchCtrlC;
1142: /* If CatchCtrlC is rewrited in this program,
1143: we crash. So, we use localCatchCtrlC. */
1144:
1145: if (localCatchCtrlC) {
1146: sigfunc = signal(SIGINT,SIG_IGN);
1147: signal(SIGINT,ctrlC);
1148: }
1149:
1150: if (KSPushEnvMode) {
1151: *saved_EnvOfStackMachine = *EnvOfStackMachine;
1.9 takayama 1152: #if defined(__CYGWIN__)
1153: if (jval = sigsetjmp(EnvOfStackMachine,1)) {
1154: #else
1.1 maekawa 1155: if (jval = setjmp(EnvOfStackMachine)) {
1.9 takayama 1156: #endif
1.1 maekawa 1157: *EnvOfStackMachine = *saved_EnvOfStackMachine;
1158: if (jval == 2) {
1.7 takayama 1159: if (ErrorMessageMode == 1 || ErrorMessageMode == 2) {
1160: pushErrorStack(KnewErrorPacket(SerialCurrent,-1,"User interrupt by ctrl-C."));
1161: }
1.1 maekawa 1162: }
1163: recursive--;
1164: if (localCatchCtrlC) { signal(SIGINT, sigfunc); }
1.16 takayama 1165: if (!Calling_ctrlC_hook) {
1166: Calling_ctrlC_hook = 1;
1167: KSexecuteString(" ctrlC-hook "); /* Execute User Defined functions. */
1168: }
1169: Calling_ctrlC_hook = 0;
1.12 takayama 1170: KSexecuteString(" (Computation is interrupted.) "); /* move to ctrlC-hook?*/
1.1 maekawa 1171: return(-1);
1172: }else{ }
1173: }else{
1174: if (recursive == 0) {
1.9 takayama 1175: #if defined(__CYGWIN__)
1176: if (jval=sigsetjmp(EnvOfStackMachine,1)) {
1177: #else
1.1 maekawa 1178: if (jval=setjmp(EnvOfStackMachine)) {
1.9 takayama 1179: #endif
1.7 takayama 1180: if (jval == 2) {
1181: if (ErrorMessageMode == 1 || ErrorMessageMode == 2) {
1182: pushErrorStack(KnewErrorPacket(SerialCurrent,-1,"User interrupt by ctrl-C."));
1183: }
1184: }
1185: recursive = 0;
1186: if (localCatchCtrlC) { signal(SIGINT, sigfunc); }
1.16 takayama 1187: if (!Calling_ctrlC_hook) {
1188: Calling_ctrlC_hook = 1;
1189: KSexecuteString(" ctrlC-hook "); /* Execute User Defined functions. */
1190: }
1191: Calling_ctrlC_hook = 0;
1192: Calling_ctrlC_hook = 0;
1.11 takayama 1193: KSexecuteString(" (Computation is interrupted.) ");
1.7 takayama 1194: return(-1);
1.1 maekawa 1195: }else { }
1196: }
1197: }
1198:
1199: recursive++;
1200: token.token = s;
1.21 takayama 1201: token.kind = EXECUTABLE_STRING; token.tflag = 0;
1.1 maekawa 1202: executeToken(token);
1.21 takayama 1203: token.kind = ID; token.tflag = 0;
1.1 maekawa 1204: token.token = "exec";
1205: token = lookupTokens(token); /* no use */
1206: tmp = findSystemDictionary(token.token);
1207: ob.tag = Soperator;
1208: ob.lc.ival = tmp;
1209: executePrimitive(ob);
1210: recursive--;
1211: if (KSPushEnvMode) *EnvOfStackMachine = *saved_EnvOfStackMachine;
1212: if (localCatchCtrlC) { signal(SIGINT, sigfunc); }
1213: return(0);
1214: }
1215:
1216: KSdefineMacros() {
1217: struct tokens token;
1218: int tmp;
1219: struct object ob;
1220:
1221: if (StandardMacros && (strlen(SMacros))) {
1.21 takayama 1222: token.kind = EXECUTABLE_STRING; token.tflag = 0;
1.1 maekawa 1223: token.token = SMacros;
1.7 takayama 1224: executeToken(token); /* execute startup commands */
1.21 takayama 1225: token.kind = ID; token.tflag = 0;
1.1 maekawa 1226: token.token = "exec";
1227: token = lookupTokens(token); /* no use */
1228: tmp = findSystemDictionary(token.token);
1229: ob.tag = Soperator;
1230: ob.lc.ival = tmp;
1.7 takayama 1231: executePrimitive(ob); /* exec */
1.1 maekawa 1232: }
1233: return(0);
1234:
1235: }
1236:
1237: void KSstart() {
1238: struct tokens token;
1239: int tmp;
1240: struct object ob;
1241: extern int Quiet;
1242:
1243: stackmachine_init(); KinitKan();
1244: getokenSM(INIT); initSystemDictionary();
1245:
1246: /* The following line may cause a core dump, if you do not setjmp properly
1247: after calling KSstart().*/
1248: /*
1.7 takayama 1249: if (setjmp(EnvOfStackMachine)) {
1.1 maekawa 1250: fprintf(stderr,"KSstart(): An error or interrupt in reading macros, files and command strings.\n");
1251: exit(10);
1.7 takayama 1252: } else { } */
1.1 maekawa 1253:
1254: /* setup quiet mode or not */
1.21 takayama 1255: token.kind = EXECUTABLE_STRING; token.tflag = 0;
1.1 maekawa 1256: if (Quiet) {
1257: token.token = " /@@@.quiet 1 def ";
1258: }else {
1259: token.token = " /@@@.quiet 0 def ";
1260: }
1261: executeToken(token); /* execute startup commands */
1.21 takayama 1262: token.kind = ID; token.tflag = 0;
1.1 maekawa 1263: token.token = "exec";
1264: token = lookupTokens(token); /* set hashing values */
1265: tmp = findSystemDictionary(token.token);
1266: ob.tag = Soperator;
1267: ob.lc.ival = tmp;
1268: executePrimitive(ob); /* exec */
1269:
1270: KSdefineMacros();
1271: }
1272:
1273: void KSstop() {
1274: Kclose(); stackmachine_close();
1275: }
1276:
1277:
1278: struct object KSpop() {
1279: return(Kpop());
1280: }
1281:
1282: void KSpush(ob)
1.7 takayama 1283: struct object ob;
1.1 maekawa 1284: {
1285: Kpush(ob);
1.4 takayama 1286: }
1287:
1288: struct object KSpeek(k) {
1289: return(peek(k));
1.1 maekawa 1290: }
1291:
1292: char *KSstringPop() {
1293: /* pop a string */
1294: struct object rob;
1295: rob = Kpop();
1296: if (rob.tag == Sdollar) {
1297: return(rob.lc.str);
1298: }else{
1299: return((char *)NULL);
1300: }
1301: }
1302:
1303: char *KSpopString() {
1304: return(KSstringPop());
1305: }
1306:
1307: int KSset(char *name) {
1308: char *tmp2;
1309: char tmp[1024];
1310: tmp2 = tmp;
1311: if (strlen(name) < 1000) {
1312: sprintf(tmp2," /%s set ",name);
1313: }else{
1314: tmp2 = sGC_malloc(sizeof(char)*(strlen(name)+20));
1315: if (tmp2 == (char *)NULL) errorStackmachine("Out of memory.");
1316: sprintf(tmp2," /%s set ",name);
1317: }
1318: return( KSexecuteString(tmp2) );
1319: }
1320:
1321: int KSpushBinary(int size,char *data) {
1322: /* struct object KbinaryToObject(int size, char *data); */
1323: errorStackmachine("KSpushBinary is not implemented.\n");
1324: return(-1);
1325: }
1326:
1327: char *KSpopBinary(int *size) {
1328: /* char *KobjectToBinary(struct object ob,int *size); */
1329: errorStackmachine("KSpopBinary is not implemented.\n");
1330: *size = 0;
1331: return((char *)NULL);
1332: }
1333:
1334: int pushErrorStack(struct object obj)
1335: {
1336: if (CurrentOperandStack == &ErrorStack) {
1337: fprintf(stderr,"You cannot call pushErrorStack when ErrorStack is the CurrentOperandStack. \n");
1338: return(-1);
1339: }
1340: (ErrorStack.ostack)[(ErrorStack.sp)++] = obj;
1341: /* printf("ErrorStack.sp = %d\n",ErrorStack.sp); */
1342: if ((ErrorStack.sp) >= (ErrorStack.size)) {
1343: ErrorStack.sp = 0;
1344: fprintf(stderr,"pushErrorStack():ErrorStack overflow. It is reset.\n");
1345: /* Note that it avoids recursive call.*/
1346: return(-1);
1347: }
1348: return(0);
1349: }
1350:
1351: struct object popErrorStack(void) {
1352: if (CurrentOperandStack == &ErrorStack) {
1353: fprintf(stderr,"You cannot call popErrorStack when ErrorStack is the CurrentOperandStack. \n");
1354: return(NullObject);
1355: }
1356: if ((ErrorStack.sp) <= 0) {
1357: return( NullObject );
1358: }else{
1359: return( (ErrorStack.ostack)[--(ErrorStack.sp)]);
1360: }
1361: }
1362:
1363: char *popErrorStackByString(void) {
1364: struct object obj;
1365: struct object eobj;
1366: eobj = popErrorStack();
1367: if (ectag(eobj) != CLASSNAME_ERROR_PACKET) {
1368: return(NULL);
1369: }else{
1370: obj = *(KopErrorPacket(eobj));
1371: }
1372: if (obj.tag != Sarray || getoaSize(obj) != 3) {
1373: fprintf(stderr,"errorPacket format error.\n");
1374: printObject(eobj,0,stderr); fflush(stderr);
1375: return("class errorPacket format error. Bug of sm1.");
1376: }
1377: obj = getoa(obj,2);
1378: if (obj.tag != Sdollar) {
1379: fprintf(stderr,"errorPacket format error at position 2..\n");
1380: printObject(eobj,0,stderr); fflush(stderr);
1381: return("class errorPacket format error at the position 2. Bug of sm1.");
1382: }
1383: return(KopString(obj));
1384: }
1385:
1386:
1387: int KScheckErrorStack(void)
1388: {
1389: return(ErrorStack.sp);
1390: }
1391:
1392: struct object KnewErrorPacket(int serial,int no,char *message)
1393: {
1394: struct object obj;
1395: struct object *myop;
1396: char *s;
1397: /* Set extended tag. */
1398: obj.tag = Sclass; obj.lc.ival = CLASSNAME_ERROR_PACKET ;
1399: myop = (struct object *)sGC_malloc(sizeof(struct object));
1400: if (myop == (struct object *)NULL) errorStackmachine("No memory\n");
1401: *myop = newObjectArray(3);
1402: /*fprintf(stderr,"newErrorPacket() in stackmachine.c: [%d, %d, %s] \n",serial,no,message); **kxx:CMO_ERROR */
1403: putoa((*myop),0,KpoInteger(serial));
1404: putoa((*myop),1,KpoInteger(no));
1405: s = (char *)sGC_malloc(sizeof(char)*(strlen(message)+2));
1406: if (s == (char *)NULL) errorStackmachine("No memory\n");
1407: strcpy(s,message);
1408: putoa((*myop),2,KpoString(s));
1409: obj.rc.op = myop;
1410: return(obj);
1411: }
1412:
1413:
1414: struct object KnewErrorPacketObj(struct object ob1)
1415: {
1416: struct object obj;
1417: struct object *myop;
1418: char *s;
1419: /* Set extended tag. */
1420: obj.tag = Sclass; obj.lc.ival = CLASSNAME_ERROR_PACKET ;
1421: myop = (struct object *)sGC_malloc(sizeof(struct object));
1422: if (myop == (struct object *)NULL) errorStackmachine("No memory\n");
1423: *myop = ob1;
1424: obj.rc.op = myop;
1425: return(obj);
1426: }
1427:
1428: void *sGC_malloc(size_t n) { /* synchronized function */
1429: void *c;
1430: int id;
1431: extern int SGClock, UserCtrlC;
1432:
1433: SGClock = 1;
1434: c = GC_malloc(n);
1435: SGClock = 0;
1436: if (UserCtrlC) {
1437: UserCtrlC = 0;
1438: id = getpid();
1439: kill(id,SIGINT);
1440: return(c);
1441: }else{
1442: return(c);
1443: }
1444: }
1445:
1446: void *sGC_realloc(void *p,size_t new) { /* synchronized function */
1447: void *c;
1448: int id;
1449: extern int SGClock, UserCtrlC;
1450:
1451: SGClock = 1;
1452: c = GC_realloc(p,new);
1453: SGClock = 0;
1454: if (UserCtrlC) {
1455: UserCtrlC = 0;
1456: id = getpid();
1457: kill(id,SIGINT);
1458: return(c);
1459: }else{
1460: return(c);
1461: }
1462: }
1463:
1464: void sGC_free(void *c) { /* synchronized function */
1465: int id;
1466: extern int SGClock, UserCtrlC;
1467:
1468: SGClock = 1;
1469: GC_free(c);
1470: SGClock = 0;
1471: if (UserCtrlC) {
1472: UserCtrlC = 0;
1473: id = getpid();
1474: kill(id,SIGINT);
1475: return;
1476: }else{
1477: return;
1478: }
1479: }
1480:
1481: void lockCtrlCForOx() {
1482: extern int OXlock;
1483: extern int OXlockSaved;
1484: OXlockSaved = OXlock;
1485: OXlock = 1;
1486: }
1487:
1488: void unlockCtrlCForOx() {
1489: int id;
1490: extern int OXlock, UserCtrlC;
1491: extern int OXlockSaved;
1492: OXlockSaved = OXlock;
1493: OXlock = 0;
1494: if (UserCtrlC) {
1495: UserCtrlC = 0;
1496: id = getpid();
1497: kill(id,SIGINT);
1498: return;
1499: }else{
1500: return;
1501: }
1502: }
1503:
1504: void restoreLockCtrlCForOx() {
1505: extern int OXlock;
1506: extern int OXlockSaved;
1507: OXlock = OXlockSaved;
1508: }
1509:
1510: int KSstackPointer() {
1511: return(Osp);
1512: }
1513:
1514: struct object KSdupErrors() {
1515: struct object rob;
1516: struct object ob;
1517: int i;
1518: int n;
1519: int m;
1520:
1521: n = KSstackPointer();
1522: m = 0;
1523: for (i=0; i<n; i++) {
1524: ob = peek(i);
1525: if (ob.tag == Sclass && ectag(ob) == CLASSNAME_ERROR_PACKET) {
1526: m++;
1527: }
1528: }
1529: rob = newObjectArray(m);
1530: m = 0;
1531: for (i=0; i<n; i++) {
1532: ob = peek(i);
1533: if (ob.tag == Sclass && ectag(ob) == CLASSNAME_ERROR_PACKET) {
1534: putoa(rob, m, ob);
1535: m++;
1536: }
1537: }
1538: return(rob);
1539: }
1.10 takayama 1540:
1541: void cancelAlarm() {
1542: alarm((unsigned int) 0);
1543: signal(SIGALRM,SIG_DFL);
1.15 takayama 1544: }
1545:
1546: /* back-trace */
1547: #define TraceNameStackSize 3000
1548: char *TraceNameStack[TraceNameStackSize];
1549: int TraceNameStackp = 0;
1550: void tracePushName(char *s) {
1551: char *t;
1552: /*
1553: t = (char *)sGC_malloc(strlen(s)+1);
1554: if (t == NULL) {
1555: fprintf(stderr,"No more memory.\n"); return;
1556: }
1557: strcpy(t,s);
1558: */
1559: t = s;
1560: TraceNameStack[TraceNameStackp++] = t;
1561: if (TraceNameStackp >= TraceNameStackSize) {
1562: fprintf(stderr,"Warning: TraceNameStack overflow. Clearing the stack.\n");
1563: TraceNameStackp = 0;
1564: }
1565: }
1566: void traceClearStack(void) {
1567: TraceNameStackp = 0;
1568: }
1569: char *tracePopName(void) {
1570: if (TraceNameStackp <= 0) return (char *) NULL;
1571: return TraceNameStack[--TraceNameStackp];
1572: }
1573: #define TRACE_MSG_SIZE 320
1574: char *traceShowStack(void) {
1575: char *s;
1576: char *t;
1577: int p;
1578: s = (char *) sGC_malloc(TRACE_MSG_SIZE);
1579: if (s == NULL) {
1580: fprintf(stderr,"No more memory.\n"); return NULL;
1581: }
1582: sprintf(s,"Trace: ");
1583: p = strlen(s);
1584: do {
1585: t = tracePopName();
1586: if (t == NULL) {
1587: s[p] = ';'; s[p+1] = 0;
1588: break;
1.26 takayama 1589: }else if ((strlen(t) + p) > (TRACE_MSG_SIZE-10)) {
1590: /* fprintf(stderr,"p=%d, TraceNameStackp=%d, strlen(t)=%d, t=%s\n",p,TraceNameStackp,strlen(t),t); */
1.15 takayama 1591: strcpy(&(s[p])," ...");
1592: break;
1593: }
1594: strcpy(&(s[p]),t); p += strlen(t);
1595: strcpy(&(s[p]),"<-"); p += 2;
1596: } while (t != (char *)NULL);
1597: fprintf(stderr,"%s\n",s);
1598: return s;
1.24 takayama 1599: }
1600:
1601: /*
1602: if (fname != NULL) fname is pushed to the trace stack.
1603: */
1.25 takayama 1604: int executeExecutableArray(struct object ob,char *fname,int withGotoP) {
1.24 takayama 1605: struct tokens *tokenArray;
1606: int size,i;
1607: int status;
1608: int infixOn;
1609: struct tokens infixToken;
1610: extern int GotoP;
1611:
1612: infixOn = 0;
1613: if (ob.tag != SexecutableArray) errorStackmachine("Error (executeTokenArray): the argument is not a token array.");
1614:
1615: if (fname != NULL) tracePushName(fname);
1616: tokenArray = ob.lc.tokenArray;
1617: size = ob.rc.ival;
1618: for (i=0; i<size; i++) {
1619: status = executeToken(tokenArray[i]);
1.25 takayama 1620: if ((status & STATUS_BREAK) || (status < 0) || (withGotoP && GotoP)) {
1.24 takayama 1621: if (fname != NULL) tracePopName();
1622: return(status);
1623: }
1624:
1625: if (status & STATUS_INFIX) {
1626: if (i == size-1) errorStackmachine("Infix operator at the end of an executable array.");
1627: infixOn = 1; infixToken = tokenArray[i];
1628: infixToken.tflag |= NO_DELAY;
1629: continue;
1630: }else if (infixOn) {
1631: infixOn = 0;
1632: status = executeToken(infixToken);
1.25 takayama 1633: if ((status & STATUS_BREAK) || (status < 0) || (withGotoP && GotoP)) {
1.24 takayama 1634: if (fname != NULL) tracePopName();
1635: return(status);
1636: }
1637: }
1638: }
1639: if (fname != NULL) tracePopName();
1640: return(0); /* normal exit */
1.10 takayama 1641: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>