📄 commline.c
字号:
if (inchar == EOS) { return(pos); } pos++; inchar = str[pos]; } return(pos); }/**************************************************************//* DoWhiteSpace: Skips over white space consisting of spaces, *//* tabs, and form feeds that is contained within a string. *//**************************************************************/static int DoWhiteSpace(str,pos) char *str; int pos; { int inchar; inchar = str[pos]; while ((inchar == ' ') || (inchar == '\f') || (inchar == '\t')) { pos++; inchar = str[pos]; } return(pos); }/********************************************************************//* CommandLoop: Endless loop which waits for user commands and then *//* executes them. The command loop will bypass the EventFunction *//* if there is an active batch file. *//********************************************************************/globle VOID CommandLoop() { int inchar; PrintCLIPS(WCLIPS,VersionString); SetHaltExecution(CLIPS_FALSE); SetEvaluationError(CLIPS_FALSE); PeriodicCleanup(CLIPS_TRUE,CLIPS_FALSE); PrintPrompt(); CLIPSInputCount = 0; while (CLIPS_TRUE) { /*===================================================*/ /* If a batch file is active, grab the command input */ /* directly from the batch file, otherwise call the */ /* event function. */ /*===================================================*/ if (BatchActive() == CLIPS_TRUE) { inchar = LLGetcBatch("stdin",CLIPS_TRUE); if (inchar == EOF) { (*EventFunction)(); } else { ExpandCommandString((char) inchar); } } else { (*EventFunction)(); } /*=================================================*/ /* If execution was halted, then remove everything */ /* from the command buffer. */ /*=================================================*/ if (GetHaltExecution() == CLIPS_TRUE) { SetHaltExecution(CLIPS_FALSE); SetEvaluationError(CLIPS_FALSE); FlushCommandString();#if ! WINDOW_INTERFACE fflush(stdin);#endif PrintCLIPS(WCLIPS,"\n"); PrintPrompt(); } /*=========================================*/ /* If a complete command is in the command */ /* buffer, then execute it. */ /*=========================================*/ if ((CompleteCommand(CommandString) != 0) && (CLIPSInputCount > 0)) { FlushPPBuffer(); SetPPBufferStatus(OFF); CLIPSInputCount = -1; RouteCommand(CommandString,CLIPS_TRUE); FlushPPBuffer(); SetHaltExecution(CLIPS_FALSE); SetEvaluationError(CLIPS_FALSE); FlushCommandString(); FlushBindList(); PeriodicCleanup(CLIPS_TRUE,CLIPS_FALSE); PrintPrompt(); } } }/*************************************************//* PrintPrompt: Prints the CLIPS command prompt. *//*************************************************/globle VOID PrintPrompt() { PrintCLIPS(WCLIPS,"CLIPS> "); if (MemoryStatusFunction != NULL) { (*MemoryStatusFunction)(); } }/********************************************************************************//* SetMemoryStatusFunction: Replaces the current value of MemoryStatusFunction. *//********************************************************************************/globle VOID SetMemoryStatusFunction(funptr) int (*funptr)(VOID_ARG); { MemoryStatusFunction = funptr; }/************************************************//* RouteCommand: Processes a completed command. *//************************************************/globle BOOLEAN RouteCommand(command,printResult) char *command; int printResult; { DATA_OBJECT result; struct expr *top; char *commandName; struct token theToken; if (command == NULL) { return(0); } /*========================================*/ /* Open a string input source and get the */ /* first token from that source. */ /*========================================*/ OpenStringSource("command",command,0); GetToken("command",&theToken); /*=====================*/ /* Evaluate constants. */ /*=====================*/ if ((theToken.type == SYMBOL) || (theToken.type == STRING) || (theToken.type == FLOAT) || (theToken.type == INTEGER) || (theToken.type == INSTANCE_NAME)) { CloseStringSource("command"); if (printResult) { PrintAtom("stdout",theToken.type,theToken.value); PrintCLIPS("stdout","\n"); } return(1); } /*============================*/ /* Evaluate global variables. */ /*============================*/ if (theToken.type == GBL_VARIABLE) { CloseStringSource("command"); top = GenConstant(theToken.type,theToken.value); EvaluateExpression(top,&result); rtn_struct(expr,top); if (printResult) { PrintDataObject("stdout",&result); PrintCLIPS("stdout","\n"); } return(1); } /*========================================================*/ /* If the next token isn't the beginning left parenthesis */ /* of a command or construct, then whatever was entered */ /* cannot be evaluated at the command prompt. */ /*========================================================*/ if (theToken.type != LPAREN) { PrintErrorID("COMMLINE",1,CLIPS_FALSE); PrintCLIPS(WERROR,"Expected a '(', constant, or global variable\n"); CloseStringSource("command"); return(0); } /*===========================================================*/ /* The next token must be a function name or construct type. */ /*===========================================================*/ GetToken("command",&theToken); if (theToken.type != SYMBOL) { PrintErrorID("COMMLINE",2,CLIPS_FALSE); PrintCLIPS(WERROR,"Expected a command.\n"); CloseStringSource("command"); return(0); } commandName = ValueToString(theToken.value); /*======================*/ /* Evaluate constructs. */ /*======================*/#if (! RUN_TIME) && (! BLOAD_ONLY) { int errorFlag; errorFlag = ParseConstruct(commandName,"command"); if (errorFlag != -1) { CloseStringSource("command"); if (errorFlag == 1) { PrintCLIPS(WERROR,"\nERROR:\n"); PrintInChunks(WERROR,GetPPBuffer()); PrintCLIPS(WERROR,"\n"); } DestroyPPBuffer(); return(errorFlag); } }#endif /*========================*/ /* Parse a function call. */ /*========================*/ ParsingTopLevelCommand = CLIPS_TRUE; top = Function2Parse("command",commandName); ParsingTopLevelCommand = CLIPS_FALSE; ClearParsedBindNames(); /*================================*/ /* Close the string input source. */ /*================================*/ CloseStringSource("command"); /*=========================*/ /* Evaluate function call. */ /*=========================*/ if (top == NULL) return(0); EvaluatingTopLevelCommand = CLIPS_TRUE; ExpressionInstall(top); EvaluateExpression(top,&result); ExpressionDeinstall(top); EvaluatingTopLevelCommand = CLIPS_FALSE; ReturnExpression(top); if ((result.type != RVOID) && printResult) { PrintDataObject("stdout",&result); PrintCLIPS("stdout","\n"); } return(1); }/*****************************************************************//* DefaultGetNextEvent: Default event-handling function. Handles *//* only keyboard events by first calling GetcCLIPS to get a *//* character and then calling ExpandCommandString to add the *//* character to the CommandString. *//*****************************************************************/static VOID DefaultGetNextEvent() { int inchar; inchar = GetcCLIPS("stdin"); if (inchar == EOF) inchar = '\n'; ExpandCommandString((char) inchar); }/*************************************//* SetEventFunction: Replaces the *//* current value of EventFunction. *//*************************************/#if ! MAC_MCWgloble int (*SetEventFunction(theFunction))(VOID_ARG) int (*theFunction)(VOID_ARG);#elsegloble int (*SetEventFunction(theFunction))() int (*theFunction)(VOID_ARG);#endif { int (*tmp_ptr)(VOID_ARG); tmp_ptr = EventFunction; EventFunction = theFunction; return(tmp_ptr); }/****************************************//* TopLevelCommand: Indicates whether a *//* top-level command is being parsed. *//****************************************/globle BOOLEAN TopLevelCommand() { return(ParsingTopLevelCommand); } /***********************************************************//* GetCommandCompletionString: Returns the last token in a *//* string if it is a valid token for command completion. *//***********************************************************/globle char *GetCommandCompletionString(theString,maxPosition) char *theString; int maxPosition; { struct token lastToken; struct token theToken; char lastChar; char *rs; int length; /*=========================*/ /* Get the command string. */ /*=========================*/ if (theString == NULL) return(""); /*=========================================================================*/ /* If the last character in the command string is a space, character */ /* return, or quotation mark, then the command completion can be anything. */ /*=========================================================================*/ lastChar = theString[maxPosition - 1]; if ((lastChar == ' ') || (lastChar == '"') || (lastChar == '\t') || (lastChar == '\f') || (lastChar == '\n') || (lastChar == '\r')) { return(""); } /*============================================*/ /* Find the last token in the command string. */ /*============================================*/ OpenTextSource("CommandCompletion",theString,0,maxPosition); IgnoreCompletionErrors = TRUE; GetToken("CommandCompletion",&theToken); CopyToken(&lastToken,&theToken); while (theToken.type != STOP) { CopyToken(&lastToken,&theToken); GetToken("CommandCompletion",&theToken); } CloseStringSource("CommandCompletion"); IgnoreCompletionErrors = FALSE; /*===============================================*/ /* Determine if the last token can be completed. */ /*===============================================*/ if (lastToken.type == SYMBOL) { rs = ValueToString(lastToken.value); if (rs[0] == '[') return (&rs[1]); return(ValueToString(lastToken.value)); } else if (lastToken.type == SF_VARIABLE) { return(ValueToString(lastToken.value)); } else if (lastToken.type == MF_VARIABLE) { return(ValueToString(lastToken.value)); } else if ((lastToken.type == GBL_VARIABLE) || (lastToken.type == MF_GBL_VARIABLE) || (lastToken.type == INSTANCE_NAME)) { return(NULL); } else if (lastToken.type == STRING) { length = strlen(ValueToString(lastToken.value)); return(GetCommandCompletionString(ValueToString(lastToken.value),length)); } else if ((lastToken.type == FLOAT) || (lastToken.type == INTEGER)) { return(NULL); } return(""); }#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -