[BACK]Return to stackmachine.c CVS log [TXT][DIR] Up to [local] / OpenXM / src / kan96xx / Kan

Annotation of OpenXM/src/kan96xx/Kan/stackmachine.c, Revision 1.13

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

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>