int CompleteCommand( const char *mstring) { int i; char inchar; int depth = 0; bool moreThanZero = false; bool complete; bool error = false; if (mstring == NULL) return(0); /*===================================================*/ /* Loop through each character of the command string */ /* to determine if there is a complete command. */ /*===================================================*/ i = 0; while ((inchar = mstring[i++]) != EOS) { switch(inchar) { /*======================================================*/ /* If a carriage return or line feed is found, there is */ /* at least one completed token in the command buffer, */ /* and parentheses are balanced, then a complete */ /* command has been found. Otherwise, remove all white */ /* space beginning with the current character. */ /*======================================================*/ case '\n' : case '\r' : if (error) return(-1); if (moreThanZero && (depth == 0)) return(1); i = DoWhiteSpace(mstring,i); break; /*=====================*/ /* Remove white space. */ /*=====================*/ case ' ' : case '\f' : case '\t' : i = DoWhiteSpace(mstring,i); break; /*======================================================*/ /* If the opening quotation of a string is encountered, */ /* determine if the closing quotation of the string is */ /* in the command buffer. Until the closing quotation */ /* is found, a complete command can not be made. */ /*======================================================*/ case '"' : i = DoString(mstring,i,&complete); if ((depth == 0) && complete) moreThanZero = true; break; /*====================*/ /* Process a comment. */ /*====================*/ case ';' : i = DoComment(mstring,i); if (moreThanZero && (depth == 0) && (mstring[i] != EOS)) { if (error) return(-1); else return(1); } else if (mstring[i] != EOS) i++; break; /*====================================================*/ /* A left parenthesis increases the nesting depth of */ /* the current command by 1. Don't bother to increase */ /* the depth if the first token encountered was not */ /* a parenthesis (e.g. for the command string */ /* "red (+ 3 4", the symbol red already forms a */ /* complete command, so the next carriage return will */ /* cause evaluation of red--the closing parenthesis */ /* for "(+ 3 4" does not have to be found). */ /*====================================================*/ case '(' : if ((depth > 0) || (moreThanZero == false)) { depth++; moreThanZero = true; } break; /*====================================================*/ /* A right parenthesis decreases the nesting depth of */ /* the current command by 1. If the parenthesis is */ /* the first token of the command, then an error is */ /* generated. */ /*====================================================*/ case ')' : if (depth > 0) depth--; else if (moreThanZero == false) error = true; break; /*=====================================================*/ /* If the command begins with any other character and */ /* an opening parenthesis hasn't yet been found, then */ /* skip all characters on the same line. If a carriage */ /* return or line feed is found, then a complete */ /* command exists. */ /*=====================================================*/ default: if (depth == 0) { if (isprint(inchar) || IsUTF8MultiByteStart(inchar)) { while ((inchar = mstring[i++]) != EOS) { if ((inchar == '\n') || (inchar == '\r')) { if (error) return(-1); else return(1); } } return(0); } } break; } } /*====================================================*/ /* Return 0 because a complete command was not found. */ /*====================================================*/ return(0); }
globle void GetToken( void *theEnv, const char *logicalName, struct token *theToken) { int inchar; unsigned short type; /*=======================================*/ /* Set Unknown default values for token. */ /*=======================================*/ theToken->type = UNKNOWN_VALUE; theToken->value = NULL; theToken->printForm = "unknown"; ScannerData(theEnv)->GlobalPos = 0; ScannerData(theEnv)->GlobalMax = 0; /*==============================================*/ /* Remove all white space before processing the */ /* GetToken() request. */ /*==============================================*/ inchar = EnvGetcRouter(theEnv,logicalName); while ((inchar == ' ') || (inchar == '\n') || (inchar == '\f') || (inchar == '\r') || (inchar == ';') || (inchar == '\t')) { /*=======================*/ /* Remove comment lines. */ /*=======================*/ if (inchar == ';') { inchar = EnvGetcRouter(theEnv,logicalName); while ((inchar != '\n') && (inchar != '\r') && (inchar != EOF) ) { inchar = EnvGetcRouter(theEnv,logicalName); } } inchar = EnvGetcRouter(theEnv,logicalName); } /*==========================*/ /* Process Symbolic Tokens. */ /*==========================*/ if (isalpha(inchar) || IsUTF8MultiByteStart(inchar)) { theToken->type = SYMBOL; EnvUngetcRouter(theEnv,inchar,logicalName); theToken->value = (void *) ScanSymbol(theEnv,logicalName,0,&type); theToken->printForm = ValueToString(theToken->value); } /*===============================================*/ /* Process Number Tokens beginning with a digit. */ /*===============================================*/ else if (isdigit(inchar)) { EnvUngetcRouter(theEnv,inchar,logicalName); ScanNumber(theEnv,logicalName,theToken); } else switch (inchar) { /*========================*/ /* Process String Tokens. */ /*========================*/ case '"': theToken->value = (void *) ScanString(theEnv,logicalName); theToken->type = STRING; theToken->printForm = StringPrintForm(theEnv,ValueToString(theToken->value)); break; /*=======================================*/ /* Process Tokens that might be numbers. */ /*=======================================*/ case '-': case '.': case '+': EnvUngetcRouter(theEnv,inchar,logicalName); ScanNumber(theEnv,logicalName,theToken); break; /*===================================*/ /* Process ? and ?<variable> Tokens. */ /*===================================*/ case '?': inchar = EnvGetcRouter(theEnv,logicalName); if (isalpha(inchar) || IsUTF8MultiByteStart(inchar) #if DEFGLOBAL_CONSTRUCT || (inchar == '*')) #else ) #endif { EnvUngetcRouter(theEnv,inchar,logicalName); theToken->value = (void *) ScanSymbol(theEnv,logicalName,0,&type); theToken->type = SF_VARIABLE; #if DEFGLOBAL_CONSTRUCT if ((ValueToString(theToken->value)[0] == '*') && (((int) strlen(ValueToString(theToken->value))) > 1) && (ValueToString(theToken->value)[strlen(ValueToString(theToken->value)) - 1] == '*')) { size_t count; theToken->type = GBL_VARIABLE; theToken->printForm = AppendStrings(theEnv,"?",ValueToString(theToken->value)); count = strlen(ScannerData(theEnv)->GlobalString); ScannerData(theEnv)->GlobalString[count-1] = EOS; theToken->value = EnvAddSymbol(theEnv,ScannerData(theEnv)->GlobalString+1); ScannerData(theEnv)->GlobalString[count-1] = (char) inchar; } else #endif theToken->printForm = AppendStrings(theEnv,"?",ValueToString(theToken->value)); } else { theToken->type = SF_WILDCARD; theToken->value = (void *) EnvAddSymbol(theEnv,"?"); EnvUngetcRouter(theEnv,inchar,logicalName); theToken->printForm = "?"; } break; /*=====================================*/ /* Process $? and $?<variable> Tokens. */ /*=====================================*/ case '$': if ((inchar = EnvGetcRouter(theEnv,logicalName)) == '?') { inchar = EnvGetcRouter(theEnv,logicalName); if (isalpha(inchar) || IsUTF8MultiByteStart(inchar) #if DEFGLOBAL_CONSTRUCT || (inchar == '*')) #else ) #endif { EnvUngetcRouter(theEnv,inchar,logicalName); theToken->value = (void *) ScanSymbol(theEnv,logicalName,0,&type); theToken->type = MF_VARIABLE; #if DEFGLOBAL_CONSTRUCT if ((ValueToString(theToken->value)[0] == '*') && ((int) (strlen(ValueToString(theToken->value))) > 1) && (ValueToString(theToken->value)[strlen(ValueToString(theToken->value)) - 1] == '*')) { size_t count; theToken->type = MF_GBL_VARIABLE; theToken->printForm = AppendStrings(theEnv,"$?",ValueToString(theToken->value)); count = strlen(ScannerData(theEnv)->GlobalString); ScannerData(theEnv)->GlobalString[count-1] = EOS; theToken->value = EnvAddSymbol(theEnv,ScannerData(theEnv)->GlobalString+1); ScannerData(theEnv)->GlobalString[count-1] = (char) inchar; } else #endif theToken->printForm = AppendStrings(theEnv,"$?",ValueToString(theToken->value)); } else {