📄 int_parser.c
字号:
char ParseProcLevel_(LEVEL level, char *arg, LWPROC defVal, LWPROC *p){ char *type; LWFLOAT resFlt; VALUE resVC; if (arg == NULL) { *p = defVal; if (defVal != NULL) { AddRefValue( defVal); TempValue( defVal); } SetErrorf("ParseProcLevel_() : NULL string cannot be converted to a proc"); return(NO); } resVC = NULL; type = TTEvalExpressionLevel_(level,arg,&resFlt,&resVC,OtherType,NO,YES,AnyType,YES); if (type == NULL) { *p = defVal; if (defVal != NULL) { AddRefValue( defVal); TempValue( defVal); } return(NO); } if (type != procType) { *p = defVal; if (defVal != NULL) { AddRefValue( defVal); TempValue( defVal); } SetErrorf("ParseProcLevel_() : expect a '&proc' type (instead of '%s')",type); return(NO); } *p = (LWPROC) resVC; return(YES);}char ParseProc_(char *arg, LWPROC defVal, LWPROC *p){ return(ParseProcLevel_(levelCur,arg,defVal,p));}/* Same as above but generate an error (no default value) */void ParseProcLevel(LEVEL level, char *arg, LWPROC *p){ if (ParseProcLevel_(level, arg,NULL,p) == NO) Errorf1("");}void ParseProc(char *arg, LWPROC *p){ if (ParseProc_(arg,NULL,p) == NO) Errorf1("");}/***************************************************************** * * * * Parsing of a script * * * *****************************************************************//* * Function for skipping newlines spaces and comments at the begining of a script * or after each new scriptline. */ static void SkipComments (char **pscript) { while (1) { while (**pscript == ' ' || **pscript == '\t' || **pscript == '\n' || **pscript == '\r' || (**pscript == '\\' && ((*pscript)[1] == '\n' || (*pscript)[1] == '\r'))) (*pscript)++; if (**pscript == '#') { (*pscript)++; while (1) { if (**pscript == '\\') (*pscript) += 2; else if (**pscript != '\n' && **pscript != '\r' && **pscript != '\0') (*pscript)++; else break; } } else break; if (**pscript != '\0') (*pscript)++; else break; }}/* * 'theLine' starts with a { or " and it returns the corresponding } or " * It returns NULL if none */char * LookForBracketScript(char *theLine, char *line){ static char *brackets[maxNImbricatedBrackets]; int nBrackets; char flagStop; nBrackets = 0; brackets[nBrackets++] = line++; flagStop = NO; while (!flagStop && *line && nBrackets != 0) { switch(*line) { case '\\' : if (*(++line) != '\0') line++; break; case '{': if (*(brackets[nBrackets-1]) == '"' || *(brackets[nBrackets-1]) == '\'') {line++;break;} if (nBrackets >= maxNImbricatedBrackets) { SetErrorf("Too many imbricated brackets"); return(NULL); } brackets[nBrackets++] = line++; break; case '}': if (*(brackets[nBrackets-1]) == '"' || *(brackets[nBrackets-1]) == '\'') {line++;break;} if (*(brackets[nBrackets-1]) != '{') flagStop = YES; else { line++; nBrackets--; } break; case '[': if (*(brackets[nBrackets-1]) == '"' || *(brackets[nBrackets-1]) == '\'') {line++;break;} if (*(brackets[nBrackets-1]) == '{') line++; else brackets[nBrackets++] = line++; break; case ']': if (*(brackets[nBrackets-1]) == '"' || *(brackets[nBrackets-1]) == '\'') {line++;break;} if (*(brackets[nBrackets-1]) == '{') line++; else if (*(brackets[nBrackets-1]) == '[') { line++; nBrackets--; } else flagStop = YES; break; case '(': if (*(brackets[nBrackets-1]) == '"' || *(brackets[nBrackets-1]) == '\'') {line++;break;} if (*(brackets[nBrackets-1]) == '{') line++; else brackets[nBrackets++] = line++; break; case ')': if (*(brackets[nBrackets-1]) == '"' || *(brackets[nBrackets-1]) == '\'') {line++;break;} if (*(brackets[nBrackets-1]) == '{') line++; else if (*(brackets[nBrackets-1]) == '(') { line++; nBrackets--; } else flagStop = YES; break; case '`': if (*(brackets[nBrackets-1]) == '"' || *(brackets[nBrackets-1]) == '\'') {line++;break;} if (*(brackets[nBrackets-1]) != '`') { if (nBrackets >= maxNImbricatedBrackets) { SetErrorf("Too many imbricated brackets"); return(NULL); } brackets[nBrackets++] = line++; } else { line++; nBrackets--; } break; case '"': if (*(brackets[nBrackets-1]) == '\'') {line++;break;} if (*(brackets[nBrackets-1]) != '"') { if (nBrackets >= maxNImbricatedBrackets) { SetErrorf("Too many imbricated brackets"); return(NULL); } brackets[nBrackets++] = line++; } else { line++; nBrackets--; } break; case '\'': if (*(brackets[nBrackets-1]) == '"') {line++;break;} if (*(brackets[nBrackets-1]) != '\'') { if (nBrackets >= maxNImbricatedBrackets) { SetErrorf("Too many imbricated brackets"); return(NULL); } brackets[nBrackets++] = line++; } else { line++; nBrackets--; } break; case '\n' : case '\r' : if (line[0] == '\n' && line[1] == '\r' || line[0] == '\r' && line[1] == '\n') line+=2; else line++; while (*line == ' ' || *line == '\t') line++; if (*line == '#') SkipComments(&line); break; default: line++; } } if (nBrackets != 0) { if (*(brackets[nBrackets-1]) == '`') SyntaxError("Missing the corresponding `",theLine,brackets[nBrackets-1]); else if (*(brackets[nBrackets-1]) == '(') SyntaxError("Missing the corresponding )",theLine,brackets[nBrackets-1]); else if (*(brackets[nBrackets-1]) == '"') SyntaxError("Missing the corresponding \"",theLine,brackets[nBrackets-1]); else if (*(brackets[nBrackets-1]) == '\'') SyntaxError("Missing the corresponding '",theLine,brackets[nBrackets-1]); else if (*(brackets[nBrackets-1]) == '{') SyntaxError("Missing the corresponding }",theLine,brackets[nBrackets-1]); else if (*(brackets[nBrackets-1]) == '[') SyntaxError("Missing the corresponding ]",theLine,brackets[nBrackets-1]); return(NULL); } else return(line-1);} /* * Get the corresponding character to an escape sequence in a list */int GetCharFromEscapeScript(char c){ switch (c) { case '#' : return('#'); case 't' : return('\t'); case 'r' : return('\r'); case 'n' : return('\n'); default : return(-1); }} /* * * The Main function for parsing a scriptline (No allocation is made) * * 'theLine' = the total line (used in case of error) * 'beg' and 'end' = will be filled wit the begining/end of each found words * both array are NULL terminated * 'endScriptLine' = will be pointing towards the end of the detected script line * 'endScriptLine1' = will be pointing towards the end of the last word of the script line * 'ptrs' ('nPtrs') = array (its size) pointing towards either each $ char that were found * (in case of $[], one pointer is pointing towards the $ and the next towards the the ]) * or if none were found towards the [] * (in which case one pointer is pointing towards the [ and the next towards the the ]) * 'flagEndIsBracket' = if YES, it means that the script line is expected to end with a ] * 'flagSetv' = if YES, it means that we dealt with a 'i=3' type of command (third word is the expression, second word is the '=' or '+=' ... sign) * 'redirectWord' = if not -1 it is the word number (first is index 0) the redirection :: starts at * 'flagSubst' = if NO then no substitution will be performed * * It returns the number of words found or -1 if the script is not complete and -2 if there is an error * */ #define MaxNPtrs 30 int ParseScriptLineBegEnd(char *theLine, char ***beg, char ***end, char **endScriptLine, char **endScriptLine1, char ***ptrs, int *nPtrs, char flagEndIsBracket, char *braceEnd,char *flagSetv, int *redirectWord, char flagSubst){ static char *theBegWords[MaxNumWords+1]; static char *theEndWords[MaxNumWords+1]; static char *thePtrs[MaxNPtrs+1]; static char theNewLine[MaxLengthList+1],*newLine; static char *theEndScriptLine; static char *bracketExt[maxNImbricatedBrackets]; static char *par[maxNImbricatedBrackets]; int nWord,nPtr,theRedirectWord; char *line,*bracket,*lineError,flagStop,*bquote,flagRedirect,*redirectChar,*quote,*start; int nPar; int nBracketExt; int nNewLine; char flagEscape; int i; char flagDollars; char flagSetv1; char flagCreateWord; /* If line is NULL then error */ if (theLine == NULL) return(-2); /* Set the result variables */ if (beg != NULL && end != NULL) { *beg = theBegWords; *end = theEndWords; } if (ptrs != NULL) *ptrs = thePtrs; /* * Some inits concerning the line */ line = theLine; while (*line == ' ' || *line == '\n' || *line == '\t' || *line == '\r' || (*line == '\\' && (line[1] == '\n' || line[1] == '\r'))) { if (*line == '\\') { if (line[1] == '\n' && line[2] == '\r' || line[1] == '\r' && line[2] == '\n') line+=3; else line+=2; } else line++; } start = line; if (endScriptLine != NULL) *endScriptLine1 = *endScriptLine = line; /* If empty line */ if (*line == '\0' || (*line == ']' && flagEndIsBracket) || (line == braceEnd)) { theBegWords[0] = theEndWords[0] = NULL; *nPtrs = 0; if (endScriptLine != NULL) *endScriptLine1 = *endScriptLine = line; return(0); } /* * Some inits concerning the processed line */ *theNewLine = '\0'; newLine = theNewLine; nNewLine = 0; /* * Other inits */ nWord = nPtr = 0; flagEscape = 0; flagDollars = NO; flagSetv1 = NO; theRedirectWord = -1; nPar = 0; nBracketExt = 0; flagStop = NO; bquote = NULL; quote = NULL; flagRedirect = NO; redirectChar = NULL; flagCreateWord = NO; theBegWords[nWord] = NULL; theEndWords[nWord] = NULL; /* * Let's loop on the characters */ while(1) { if (braceEnd != *endScriptLine1) *endScriptLine1 = line; /* * If we were asked to create a new word just do it */ if (flagCreateWord) { /* Let's end the word */ theEndWords[nWord] = (nNewLine != 0 ? newLine-1 : newLine); nWord++; /* And init the next one */ theBegWords[nWord] = theEndWords[nWord] = NULL; /* Skip spaces and tabs or escaped new lines */ while (*line == ' ' || *line == '\t' || (*line == '\\' && (line[1] == '\n' || line[1] == '\r'))) { if (*line == '\\') { if (line[1] == '\n' && line[2] == '\r' || line[1] == '\r' && line[2] == '\n') line+=3; else line+=2; } else line++; } /* If there is a new line --> we are done */ if (*line == '\n' || *line == '\r') { flagStop = YES; } /* If there is a ;; */ else if (*line == ';' && line[1] == ';') { line+=2; flagStop = YES; } if (flagRedirect) flagRedirect--; flagCreateWord = NO; } /* * Case we must exit the loop : * - end of the line or * - flagStop */ if (*line == '\0' || flagStop) { /* Do we expect to have stream names specified ? */ if (flagRedirect == 2) { SyntaxError("You need to specify the stream names for redirection",theLine,redirectChar); return(-2); } /* We expected an escaped characters */ if (flagEscape) { SyntaxError("You cannot escape a null character",theLine,line); return(-1); } /* We must end the last word if necessary */ if (theBegWords[nWord] != NULL) { theEndWords[nWord] = newLine-1; nWord++; } /* Skip (escaped) newlines, tabs and spaces */ while (1) { if (*line == ' ' || *line == '\n' || *line == '\t' || *line == '\r') line++; else if (*line == '\\' && (*line == '\n' || *line == '\t')) line+=2; break; } theEndScriptLine = line; break; } /* * We must init the new word if any */ if (theBegWords[nWord]==NULL) { if (nWord > MaxNumWords-1) { SetErrorf("Script line is too long"); return(-2); } theBegWords[nWord] = newLine; theEndWords[nWord] = NULL; } /* * 'lineError' points toward the current character ('line' will move in the switch). * It will be used in case of errors. */ lineError = line; /* * * Main switch * */ switch(*line) { /*************************************** * [] Signs ***************************************/ case '[' :
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -