📄 int_eval.c
字号:
str[m] = GetStringExpr(levelCur,cs->pos.dollar[i],&(left[m]),nw); if (str[m] == NULL) Errorf1(""); l+= strlen(str[m]); } if (str[m] == NULL) Errorf1(""); } l+= strlen(left[m-1])+1; /* Alloc the corresponding word */ list[nw] = TMalloc(sizeof(char)*l); /* And set it */ l = 0; str1 = list[nw]; for (m=0,i=0; cs->pos.dollar[i] != NULL; i++,m++) { /* Case the $ have already been taken into account by the last evaluation */ if (m != 0 && left[m-1] > cs->pos.dollar[i]) { if (*(cs->pos.dollar[i]+1) == '[') i++; continue; } if (i == 0) { l= cs->pos.dollar[0]-word; strncpy(str1,word,l); } else { l = cs->pos.dollar[i]-left[m-1]; strncpy(str1,left[m-1],l); } str1 += l; if (*(cs->pos.dollar[i]+1) == '[') i++; l = strlen(str[m]); strncpy(str1,str[m],l); str1+=l; } strcpy(str1,left[m-1]); } list[nw] = NULL; return(list);} /* * Basic function that executes a command that is described as a 'list'. * * It either returns (execution was successful) or an error occurs * It is executed within a TempAlloc */static void _EvalScriptLine(SCRIPTLINE sl){ char **list1,**list,**argv; LWPROC sc; LWPROC cc; char *name; char flagTrace; int i; char flag; SCRIPT script; char **oldCmdList; SCRIPTLINE oldSL; VALUE val; LEVEL theLevel; if (sl == NULL) Errorf("_EvalScriptLine() : weird bug"); list = sl->words; /* If the line is empty just returns */ if (list[0] == NULL) return; SetTempAlloc(); /* Init the error message and the result */ InitError(); InitResult(); /* Save/Set the cmdList and scriptline */ theLevel = levelCur; oldCmdList = theLevel->cmdList; oldSL = theLevel->scriptline; theLevel->cmdList = sl->words; theLevel->scriptline = sl; /* Make $ substitution if needed */ if ((sl->flags & SLDollarFlag) && sl->cs != NULL) { list = SLSubstitution(sl); theLevel->cmdList = list; } /* Make redirections of streams */ if (sl->redirect != NULL) SetCurStreams(sl->redirect-sl->words+list+1); else SetCurStreams(NULL); /* Set the argv */ if (sl->flags & SLSetvFlag) argv = list; else argv = list+1; /* * Read the command */ sc = NULL; cc = NULL; /* Get the name */ if (sl->flags & SLSetvFlag) name = "setv"; else name = list[0]; if (!strcmp(name,"stop")) { InitError(); InitResult(); } if (!strcmp(name,"!")) name = "shell"; /* Case the command name has already been processed */ if (sl->proc != NULL && (sl->proc->flagSP == NO || sl->cs == NULL || sl->cs[0] == NULL || sl->cs[0]->pos.dollar == NULL)) { /* If it is not the right version then delete it */ if (!sl->proc->flagStillExist && (sl->proc->flagSP == YES || name[0] != '\\')) { DeleteProc(sl->proc); sl->proc = NULL; } /* Otherwise we set it */ else { if (sl->proc->flagSP) sc = sl->proc; else cc = sl->proc; } } /* case we have to process the command name */ if (cc == NULL && sc == NULL) { /* If the command name starts with a \ then it must be a CProc */ if (name[0] == '\\') { name++; cc = GetCProc(name); if (cc == NULL) Errorf("Unknown command: %s",list[0]); sl->proc = cc; cc->nRef++; } /* Otherwise just process it */ else { /* get the command */ cc = GetProc(name); if (cc != NULL) { sl->proc = cc; cc->nRef++; if (cc->flagSP) { sc = cc; cc = NULL; } } } } /****************** * * Eval a scommand * ******************/ if (sc != NULL) { /* Open a new environment associated to this command */ flagTrace = theLevel->flagTrace; OpenLevel(sc); /* Should we trace this command ?*/ if (sc->flagTrace || flagTrace) { for (i=toplevelCur->nLevel-1;i>1;i--) Printf(" "); Printf(">>> "); for (list1 = list; *list1!= NULL; list1++) Printf("%s ",*list1); Printf("\n"); levelCur->flagTrace = sc->flagTrace==2; } /* Set the local variables */ SetVarsFromList(GetLevel(-1),sc->p.sp->varTypeList,sc->p.sp->varList,sc->p.sp->varDefList,argv); /* This flag is used to track whether a scommand returns a value */ toplevelCur->flagReturn = NO; /* Eval the script corresponding to this scommand */ script = sc->p.sp->script; for (i=0; i< script->nsl; i++) { if (i == script->nsl-1) flag = toplevelCur->flagStoreResult; else flag = NO; EvalScriptLine(script->sl[i],NO,flag); if (IsReturnFlag()) break; } ClearStopFlag(); /* Close the local environment */ CloseLevel(); /* Should we trace this command ?*/ if (sc->flagTrace || theLevel->flagTrace) { for (i=toplevelCur->nLevel;i>1;i--) Printf(" "); Printf("<<< "); for (list1 = list; *list1!= NULL; list1++) Printf("%s ",*list1); Printf("\n"); } } /****************** * Eval a ccommand ******************/ else if (cc != NULL) { /* Should we trace this command ?*/ if (theLevel->flagTrace || cc->flagTrace) { for (i=toplevelCur->nLevel-1;i>0;i--) Printf(" "); Printf("C>> "); for (list1 = list; *list1!= NULL; list1++) { if (sl->flags & SLSetvFlag && list1 == list+1) Printf("= "); if (strchr(*list1,'\n')) { Printf("..."); break; } Printf("%s ",*list1); } Printf("\n"); } (cc->p.cp->function)(argv); } /******************** * If we are in terminal --> try to evaluate ********************/ else if (toplevelCur->nLevel == 1 && (list[1] == NULL || !(IsValidSymbolChar1(*list[0])))) { if (sl->nWords == 1) ParseVal(sl->words[0],&val); /* ???? idiot : il faut concatener les words */ else ParseVal(sl->line,&val); SetResultValue(val); } /******************** * Command not found ********************/ else Errorf("Unknown command: %s",list[0]); /* Restore the commands that are currently executed */ levelCur = theLevel; theLevel->cmdList = oldCmdList; theLevel->scriptline = oldSL; SetCurStreams(NULL); /* Erase the temporary memory zone */ ClearTempAlloc();}/* * This is the function to call from a C function in order to execute a command line * flagHistory == YES ==> it records the command in the history * flagStoreResult == YES ==> the result of the evaluation will be used */ void EvalList(char **list, int nWords, char flagStoreResult){ struct scriptline _sl; SCRIPTLINE sl = &_sl; char oldFlag; sl->line = NULL; sl->nWords = nWords; sl->words = list; sl->cs = NULL; oldFlag = toplevelCur->flagStoreResult; toplevelCur->flagStoreResult = flagStoreResult; _EvalScriptLine(sl); toplevelCur->flagStoreResult = oldFlag;} /* * The corresponding command */void C_Eval(char **argv){extern void EvalScriptLevel(LEVEL level, SCRIPT script,char flagStoreResult); SCRIPT script; LEVEL level; argv = ParseArgv(argv,tLEVEL_,levelCur,&level,tSCRIPT,&script,0); while (level != level->levelVar) level = level->levelVar; EvalScriptLevel(level,script,toplevelCur->flagStoreResult);} /* * This is the function to call in order to execute a command * which you already have as a list. * flagHistory == YES ==> it records the command in the history * flagStoreResult == YES ==> the result of the evaluation will be used */void EvalScriptLine(SCRIPTLINE sl, char flagHistory,char flagStoreResult){ char oldFlag; /* Init the error message */ InitError(); /* Record the command line in the history */ if (flagHistory) RecordHistory(sl->line); oldFlag = toplevelCur->flagStoreResult; toplevelCur->flagStoreResult = flagStoreResult; _EvalScriptLine(sl); toplevelCur->flagStoreResult = oldFlag;} /*********************************************************************************** * * Evaluating a script .... (i.e., multiple commands) * ***********************************************************************************//* * Basic function for executing a script which is represented by a list of commands */void EvalScriptLevel(LEVEL level, SCRIPT script,char flagStoreResult){ char flag; int i; LEVEL level1; InitError(); if (script == NULL) Errorf("EvalScriptLevel() : weired bug"); if (script->nsl == 0) return; level1 = levelCur->levelVar; levelCur->levelVar = level; flag = NO; for (i=0;i<script->nsl;i++) { if (flagStoreResult == YES && i==script->nsl-1) flag = YES; EvalScriptLine(script->sl[i],NO,flag); if (IsStopFlag()) return; } levelCur->levelVar = level1;} void EvalScript(SCRIPT script,char flagStoreResult){ EvalScriptLevel(levelCur,script,flagStoreResult);}/* * Function for executing a script which is represented by a line * If the script is not complete it returns NO and does not perform anything. * otherwise it returns YES. * */char EvalScriptStringIfComplete(char *str,char flagHistory,char flagStoreResult){ SCRIPT script; if (str == NULL) Errorf("EvalScriptStringIfComplete() : Weired bug"); InitError(); SetTempAlloc(); if (ParseCompleteScript(&str,&script,NO) == NO) { ClearTempAlloc(); return(NO); } if (script->nsl == 0) { ClearTempAlloc(); return(YES); } if (flagHistory) RecordHistory(str); EvalScript(script,flagStoreResult); ClearTempAlloc(); return(YES);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -