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