=================================================================== RCS file: /home/cvs/OpenXM/src/kan96xx/Kan/stackmachine.c,v retrieving revision 1.2 retrieving revision 1.18 diff -u -p -r1.2 -r1.18 --- OpenXM/src/kan96xx/Kan/stackmachine.c 2000/01/16 07:55:41 1.2 +++ OpenXM/src/kan96xx/Kan/stackmachine.c 2004/09/05 08:08:41 1.18 @@ -1,4 +1,4 @@ -/* $OpenXM$ */ +/* $OpenXM: OpenXM/src/kan96xx/Kan/stackmachine.c,v 1.17 2004/09/05 01:15:47 takayama Exp $ */ /* stackmachin.c */ #include @@ -14,7 +14,8 @@ /* #define OPERAND_STACK_SIZE 2000 */ #define OPERAND_STACK_SIZE 30000 #define SYSTEM_DICTIONARY_SIZE 200 -#define USER_DICTIONARY_SIZE 1223 +/* #define USER_DICTIONARY_SIZE 1223, 3581, 27449 */ +#define USER_DICTIONARY_SIZE 59359 /* The value of USER_DICTIONARY_SIZE must be prime number, because of hashing method */ #define ARGV_WORK_MAX (AGLIMIT+100) @@ -53,6 +54,8 @@ struct context *PrimitiveContextp = &StandardContext; static struct object ObjTmp; /* for poor compiler */ +int Calling_ctrlC_hook = 0; + int StandardMacros = 1; int StartAFile = 0; char *StartFile; @@ -73,6 +76,7 @@ static void pstack(void); static struct object executableStringToExecutableArray(char *str); extern int SerialCurrent; +extern int QuoteMode; int SGClock = 0; int UserCtrlC = 0; @@ -91,7 +95,7 @@ struct object * newObject() } struct object newObjectArray(size) -int size; + int size; { struct object rob; struct object *op; @@ -109,15 +113,15 @@ int size; } isNullObject(obj) -struct object obj; + struct object obj; { if (obj.tag == 0) return(1); else return(0); } int putSystemDictionary(str,ob) -char *str; /* key */ -struct object ob; /* value */ + char *str; /* key */ + struct object ob; /* value */ { int i; int j; @@ -127,8 +131,8 @@ struct object ob; /* value */ /*printf("Add %d %s\n",i,str);*/ if (strcmp(str,(SystemDictionary[i]).key) > 0) { for (j=Sdp-1; j>=i+1; j--) { - (SystemDictionary[j+1]).key = (SystemDictionary[j]).key; - (SystemDictionary[j+1]).obj = (SystemDictionary[j]).obj; + (SystemDictionary[j+1]).key = (SystemDictionary[j]).key; + (SystemDictionary[j+1]).obj = (SystemDictionary[j]).obj; } (SystemDictionary[i+1]).key = str; (SystemDictionary[i+1]).obj = ob; @@ -168,15 +172,15 @@ int findSystemDictionary(str) return(0); } else if (first == last) { if (strcmp(str,(SystemDictionary[first]).key) == 0) { - return((SystemDictionary[first]).obj.lc.ival); + return((SystemDictionary[first]).obj.lc.ival); }else { - return(0); + return(0); } } else if (last - first == 1) { /* This case is necessary */ if (strcmp(str,(SystemDictionary[first]).key) == 0) { - return((SystemDictionary[first]).obj.lc.ival); + return((SystemDictionary[first]).obj.lc.ival); }else if (strcmp(str,(SystemDictionary[last]).key) == 0) { - return((SystemDictionary[last]).obj.lc.ival); + return((SystemDictionary[last]).obj.lc.ival); }else return(0); } @@ -193,10 +197,10 @@ int findSystemDictionary(str) } int putUserDictionary(str,h0,h1,ob,dic) -char *str; /* key */ -int h0,h1; /* Hash values of the key */ -struct object ob; /* value */ -struct dictionary *dic; + char *str; /* key */ + int h0,h1; /* Hash values of the key */ + struct object ob; /* value */ + struct dictionary *dic; { int x,r; extern int Strict2; @@ -241,10 +245,10 @@ struct object KputUserDictionary(char *str,struct obje } struct object findUserDictionary(str,h0,h1,cp) -/* returns NoObject, if there is no item. */ -char *str; /* key */ -int h0,h1; /* The hashing values of the key. */ -struct context *cp; + /* returns NoObject, if there is no item. */ + char *str; /* key */ + int h0,h1; /* The hashing values of the key. */ + struct context *cp; { int x; struct dictionary *dic; @@ -270,10 +274,10 @@ struct object KfindUserDictionary(char *str) { } int putUserDictionary2(str,h0,h1,attr,dic) -char *str; /* key */ -int h0,h1; /* Hash values of the key */ -int attr; /* attribute field */ -struct dictionary *dic; + char *str; /* key */ + int h0,h1; /* Hash values of the key */ + int attr; /* attribute field */ + struct dictionary *dic; { int x; int i; @@ -301,8 +305,8 @@ struct dictionary *dic; int putPrimitiveFunction(str,number) -char *str; -int number; + char *str; + int number; { struct object ob; ob.tag = Soperator; @@ -311,7 +315,7 @@ int number; } struct tokens lookupTokens(t) -struct tokens t; + struct tokens t; { struct object *left; struct object *right; @@ -327,7 +331,7 @@ struct tokens t; } struct object lookupLiteralString(s) -char *s; /* s must be a literal string */ + char *s; /* s must be a literal string */ { struct object ob; ob.tag = Slist; @@ -341,20 +345,20 @@ char *s; /* s must be a literal string */ int hash0(str) -char *str; + char *str; { int h=0; while (*str != '\0') { - h = ((h*128)+(*str)) % USER_DICTIONARY_SIZE; + h = ((h*128)+((unsigned char)(*str))) % USER_DICTIONARY_SIZE; str++; } return(h); } int hash1(str) -char *str; + char *str; { - return(8-(str[0]%8)); + return(8-((unsigned char)(str[0])%8)); } void hashInitialize(struct dictionary *dic) @@ -366,7 +370,7 @@ void hashInitialize(struct dictionary *dic) } static isInteger(str) -char *str; + char *str; { int i; int n; @@ -387,7 +391,7 @@ char *str; } static strToInteger(str) -char *str; + char *str; { int i; int n; @@ -408,15 +412,15 @@ char *str; } static power(s,i) -int s; -int i; + int s; + int i; { if (i == 0) return 1; else return( s*power(s,i-1) ); } int Kpush(ob) -struct object ob; + struct object ob; { OperandStack[Osp++] = ob; if (Osp >= OspMax) { @@ -437,7 +441,7 @@ struct object Kpop() } struct object peek(k) -int k; + int k; { if ((Osp-k-1) < 0) { return( NullObject ); @@ -586,7 +590,7 @@ void contextControl(actionOfContextControl ctl) { int isLiteral(str) -char *str; + char *str; { if (strlen(str) <2) return(0); else { @@ -611,7 +615,7 @@ void printOperandStack() { static initSystemDictionary() - { +{ StandardStack.ostack = StandardStackA; StandardStack.sp = StandardStackP; StandardStack.size = OPERAND_STACK_SIZE; @@ -626,7 +630,7 @@ static initSystemDictionary() KdefinePrimitiveFunctions(); - } +} struct object showSystemDictionary(int f) { int i; @@ -677,7 +681,7 @@ int showUserDictionary() for (i=0; imaxl) - maxl = strlen((dic[i]).key); + maxl = strlen((dic[i]).key); } } maxl += 3; @@ -688,12 +692,12 @@ int showUserDictionary() if ((dic[i]).key != EMPTY) { fprintf(Fstack,format,(dic[i]).key); /*{ char *sss; int ii,h0,h1; - sss = dic[i].key; - h0 = dic[i].h0; - h1 = dic[i].h1; - for (ii=0; ii= 1) { - fprintf(Fstack,"\nscanner> "); + fprintf(Fstack,"\nscanner> "); } - KSexecuteString(" ctrlC-hook "); /* Execute User Defined functions. */ + if (!Calling_ctrlC_hook) { /* to avoid recursive call of ctrlC-hook. */ + Calling_ctrlC_hook = 1; + KSexecuteString(" ctrlC-hook "); /* Execute User Defined functions. */ + } + Calling_ctrlC_hook = 0; + KSexecuteString(" (Computation is interrupted.) "); /* move to ctrlC-hook? */ + InSendmsg2 = 0; + continue ; } else { } if (DebugStack >= 1) { printOperandStack(); } token = getokenSM(GET); @@ -817,21 +837,25 @@ void scanner() { void ctrlC(sig) -int sig; + int sig; { extern void ctrlC(); extern int ErrorMessageMode; extern int SGClock; extern int UserCtrlC; extern int OXlock; - + signal(sig,SIG_IGN); /* see 133p */ + cancelAlarm(); + if (sig == SIGALRM) { + fprintf(stderr,"ctrlC by SIGALRM\n"); + } if (SGClock) { UserCtrlC = 1; fprintf(stderr,"ctrl-c is locked because of gc.\n"); - signal(SIGINT,ctrlC); + signal(sig,ctrlC); if (sig == SIGALRM) alarm((unsigned int)10); return; } if (OXlock) { @@ -844,13 +868,15 @@ int sig; unlockCtrlCForOx(); } fprintf(stderr,"ctrl-c is locked because of ox lock %d.\n",UserCtrlC); - signal(SIGINT,ctrlC); + signal(sig,ctrlC); if (sig == SIGALRM) alarm((unsigned int)10); return; } if (ErrorMessageMode != 1) { + (void *) traceShowStack(); fprintf(Fstack,"User interruption by ctrl-C. We are in the top-level.\n"); fprintf(Fstack,"Type in quit in order to exit sm1.\n"); } + traceClearStack(); if (GotoP) { fprintf(Fstack,"The interpreter was looking for the label <<%s>>. It is also aborted.\n",GotoLabel); GotoP = 0; @@ -865,11 +891,15 @@ int sig; */ getokenSM(INIT); /* It might fix the bug above. 1992/11/14 */ signal(SIGINT,ctrlC); +#if defined(__CYGWIN__) + siglongjmp(EnvOfStackMachine,2); +#else longjmp(EnvOfStackMachine,2); /* returns 2 for ctrl-C */ +#endif } int executeToken(token) -struct tokens token; + struct tokens token; { struct object ob; int primitive; @@ -879,12 +909,13 @@ struct tokens token; int i,h0,h1; extern int WarningMessageMode; extern int Strict; + extern int InSendmsg2; if (GotoP) { /* for goto */ if (token.kind == ID && isLiteral(token.token)) { if (strcmp(&((token.token)[1]),GotoLabel) == 0) { - GotoP = 0; - return(0); /* normal exit */ + GotoP = 0; + return(0); /* normal exit */ } } return(0); /* normal exit */ @@ -906,8 +937,8 @@ struct tokens token; strcpy(ob.lc.str, &((token.token)[1])); if (token.object.tag != Slist) { - fprintf(Fstack,"\n%%Warning: The hashing values for the <<%s>> are not set.\n",token.token); - token.object = lookupLiteralString(token.token); + fprintf(Fstack,"\n%%Warning: The hashing values for the <<%s>> are not set.\n",token.token); + token.object = lookupLiteralString(token.token); } ob.rc.op = token.object.lc.op; Kpush(ob); @@ -918,46 +949,60 @@ struct tokens token; Kpush(ob); } else { if (token.object.tag != Slist) { - fprintf(Fstack,"\n%%Warning: The hashing values for the <<%s>> are not set.\n",token.token); - token = lookupTokens(token); + fprintf(Fstack,"\n%%Warning: The hashing values for the <<%s>> are not set.\n",token.token); + token = lookupTokens(token); } h0 = ((token.object.lc.op)->lc).ival; h1 = ((token.object.lc.op)->rc).ival; ob=findUserDictionary(token.token,h0,h1,CurrentContextp); primitive = ((token.object.rc.op)->lc).ival; if (ob.tag >= 0) { - /* there is a definition in the user dictionary */ - if (ob.tag == SexecutableArray) { - tokenArray = ob.lc.tokenArray; - size = ob.rc.ival; - for (i=0; i> is not in the system dictionary\n%% nor in the user dictionaries. Push NullObject.\n",token.token); - }else {strcpy(tmpc,"Warning: identifier is not in the dictionaries.");} - pushErrorStack(KnewErrorPacket(SerialCurrent,-1,tmpc)); - } - if (WarningMessageMode != 1) { - fprintf(Fstack,"\n%%Warning: The identifier <<%s>> is not in the system dictionary\n%% nor in the user dictionaries. Push NullObject.\n",token.token); - /*fprintf(Fstack,"(%d,%d)\n",h0,h1);*/ - } - if (Strict) { - errorStackmachine("Warning: identifier is not in the dictionaries"); - } - Kpush(NullObject); + if (QuoteMode) { + if (InSendmsg2) return(DO_QUOTE); + else { + Kpush(KpoString(token.token)); + return(0); /* normal exit.*/ + } + } + if (WarningMessageMode == 1 || WarningMessageMode == 2) { + char tmpc[1024]; + if (strlen(token.token) < 900) { + sprintf(tmpc,"\n%%Warning: The identifier <<%s>> is not in the system dictionary\n%% nor in the user dictionaries. Push NullObject.\n",token.token); + }else {strcpy(tmpc,"Warning: identifier is not in the dictionaries.");} + pushErrorStack(KnewErrorPacket(SerialCurrent,-1,tmpc)); + } + if (WarningMessageMode != 1) { + fprintf(Fstack,"\n%%Warning: The identifier <<%s>> is not in the system dictionary\n%% nor in the user dictionaries. Push NullObject.\n",token.token); + /*fprintf(Fstack,"(%d,%d)\n",h0,h1);*/ + } + if (Strict) { + errorStackmachine("Warning: identifier is not in the dictionaries"); + } + Kpush(NullObject); } } } else if (token.kind == EXECUTABLE_STRING) { @@ -983,13 +1028,14 @@ struct tokens token; errorStackmachine(str) -char *str; + char *str; { int i,j,k; static char *u="Usage:"; char message0[1024]; char *message; extern int ErrorMessageMode; + cancelAlarm(); if (ErrorMessageMode == 1 || ErrorMessageMode == 2) { pushErrorStack(KnewErrorPacket(SerialCurrent,-1,str)); } @@ -1003,27 +1049,29 @@ char *str; if (i==6) { fprintf(stderr,"ERROR(sm): \n"); while (str[i] != '\0' && str[i] != ' ') { - i++; + i++; } if (str[i] == ' ') { - fprintf(stderr," %s\n",&(str[i+1])); - k = 0; - if (i-6 > 1022) message = (char *)sGC_malloc(sizeof(char)*i); - for (j=6; j 1022) message = (char *)sGC_malloc(sizeof(char)*i); + for (j=6; j>. It is also aborted.\n",GotoLabel); GotoP = 0; @@ -1035,7 +1083,7 @@ char *str; } warningStackmachine(str) -char *str; + char *str; { extern int WarningMessageMode; extern int Strict; @@ -1056,7 +1104,7 @@ char *str; you have to reset the jump buffer by setjmp(EnvOfStackMachine). cf. kxx/memo1.txt, kxx/stdserver00.c 1998, 2/6 */ KSexecuteString(s) -char *s; + char *s; { struct tokens token; struct object ob; @@ -1081,28 +1129,49 @@ char *s; if (KSPushEnvMode) { *saved_EnvOfStackMachine = *EnvOfStackMachine; +#if defined(__CYGWIN__) + if (jval = sigsetjmp(EnvOfStackMachine,1)) { +#else if (jval = setjmp(EnvOfStackMachine)) { +#endif *EnvOfStackMachine = *saved_EnvOfStackMachine; if (jval == 2) { - if (ErrorMessageMode == 1 || ErrorMessageMode == 2) { - pushErrorStack(KnewErrorPacket(SerialCurrent,-1,"User interrupt by ctrl-C.")); - } + if (ErrorMessageMode == 1 || ErrorMessageMode == 2) { + pushErrorStack(KnewErrorPacket(SerialCurrent,-1,"User interrupt by ctrl-C.")); + } } recursive--; if (localCatchCtrlC) { signal(SIGINT, sigfunc); } + if (!Calling_ctrlC_hook) { + Calling_ctrlC_hook = 1; + KSexecuteString(" ctrlC-hook "); /* Execute User Defined functions. */ + } + Calling_ctrlC_hook = 0; + KSexecuteString(" (Computation is interrupted.) "); /* move to ctrlC-hook?*/ return(-1); }else{ } }else{ if (recursive == 0) { +#if defined(__CYGWIN__) + if (jval=sigsetjmp(EnvOfStackMachine,1)) { +#else if (jval=setjmp(EnvOfStackMachine)) { - if (jval == 2) { - if (ErrorMessageMode == 1 || ErrorMessageMode == 2) { - pushErrorStack(KnewErrorPacket(SerialCurrent,-1,"User interrupt by ctrl-C.")); - } - } - recursive = 0; - if (localCatchCtrlC) { signal(SIGINT, sigfunc); } - return(-1); +#endif + if (jval == 2) { + if (ErrorMessageMode == 1 || ErrorMessageMode == 2) { + pushErrorStack(KnewErrorPacket(SerialCurrent,-1,"User interrupt by ctrl-C.")); + } + } + recursive = 0; + if (localCatchCtrlC) { signal(SIGINT, sigfunc); } + if (!Calling_ctrlC_hook) { + Calling_ctrlC_hook = 1; + KSexecuteString(" ctrlC-hook "); /* Execute User Defined functions. */ + } + Calling_ctrlC_hook = 0; + Calling_ctrlC_hook = 0; + KSexecuteString(" (Computation is interrupted.) "); + return(-1); }else { } } } @@ -1132,14 +1201,14 @@ KSdefineMacros() { if (StandardMacros && (strlen(SMacros))) { token.kind = EXECUTABLE_STRING; token.token = SMacros; - executeToken(token); /* execute startup commands */ + executeToken(token); /* execute startup commands */ token.kind = ID; token.token = "exec"; token = lookupTokens(token); /* no use */ tmp = findSystemDictionary(token.token); ob.tag = Soperator; ob.lc.ival = tmp; - executePrimitive(ob); /* exec */ + executePrimitive(ob); /* exec */ } return(0); @@ -1157,10 +1226,10 @@ void KSstart() { /* The following line may cause a core dump, if you do not setjmp properly after calling KSstart().*/ /* - if (setjmp(EnvOfStackMachine)) { + if (setjmp(EnvOfStackMachine)) { fprintf(stderr,"KSstart(): An error or interrupt in reading macros, files and command strings.\n"); exit(10); - } else { } */ + } else { } */ /* setup quiet mode or not */ token.kind = EXECUTABLE_STRING; @@ -1191,11 +1260,15 @@ struct object KSpop() { } void KSpush(ob) -struct object ob; + struct object ob; { Kpush(ob); } +struct object KSpeek(k) { + return(peek(k)); +} + char *KSstringPop() { /* pop a string */ struct object rob; @@ -1444,4 +1517,63 @@ struct object KSdupErrors() { } return(rob); } - + +void cancelAlarm() { + alarm((unsigned int) 0); + signal(SIGALRM,SIG_DFL); +} + +/* back-trace */ +#define TraceNameStackSize 3000 +char *TraceNameStack[TraceNameStackSize]; +int TraceNameStackp = 0; +void tracePushName(char *s) { + char *t; + /* + t = (char *)sGC_malloc(strlen(s)+1); + if (t == NULL) { + fprintf(stderr,"No more memory.\n"); return; + } + strcpy(t,s); + */ + t = s; + TraceNameStack[TraceNameStackp++] = t; + if (TraceNameStackp >= TraceNameStackSize) { + fprintf(stderr,"Warning: TraceNameStack overflow. Clearing the stack.\n"); + TraceNameStackp = 0; + } +} +void traceClearStack(void) { + TraceNameStackp = 0; +} +char *tracePopName(void) { + if (TraceNameStackp <= 0) return (char *) NULL; + return TraceNameStack[--TraceNameStackp]; +} +#define TRACE_MSG_SIZE 320 +char *traceShowStack(void) { + char *s; + char *t; + int p; + s = (char *) sGC_malloc(TRACE_MSG_SIZE); + if (s == NULL) { + fprintf(stderr,"No more memory.\n"); return NULL; + } + sprintf(s,"Trace: "); + p = strlen(s); + do { + t = tracePopName(); + if (t == NULL) { + s[p] = ';'; s[p+1] = 0; + break; + }else if ((strlen(t) + p -10) > TRACE_MSG_SIZE) { + /* fprintf(stderr,"p=%d, TraceNameStackp=%d, strlen(t)=%d, t=%s\n",p,TraceNameStackp,strlen(t),t); */ + strcpy(&(s[p])," ..."); + break; + } + strcpy(&(s[p]),t); p += strlen(t); + strcpy(&(s[p]),"<-"); p += 2; + } while (t != (char *)NULL); + fprintf(stderr,"%s\n",s); + return s; +}