//This method should be called from the main sketch in loop(); void AsiMS2000::checkSerial() { int inByte = 0; static int bufferPos = 0; static char commandBuffer [BUFFERSIZE]; if(Serial1.available() > 0) { inByte = Serial1.read(); if(bufferPos > BUFFERSIZE) { bufferPos = 0; bufferOverunError(commandBuffer); return; } //check for <CR> or | since arduino env can't send CR. if(inByte == 13 || inByte == 124) { commandBuffer[bufferPos] = '\0'; inputPrintln(commandBuffer); interpretCommand(commandBuffer); bufferPos =0; } else if(inByte == 10 || inByte == 27)//backspace or escape { clearCommandBuffer(commandBuffer); bufferPos = 0; } else if(inByte > 31)//ignore control characters { commandBuffer[bufferPos++] = inByte; } } }
/** * @brief bossTask * @param argument */ void StartBossTask(void const * argument) { // Start receiving data via USB UART cHAL_UART_TermReceive_IT(&huart1, 512); cHAL_UART_TermReceive_IT(&huart2, 512); msg_genericMessage_t rxMessage; // If in AUTO mode, set off a WIFI startup // if (globalFlags.states.commState == COMM_STATE_AUTO) { // sendCommand(msgQBoss, MSG_SRC_BOSS_TASK, MSG_CMD_WIFI_INIT, // osWaitForever); // } /* Infinite loop */ for (;;) { fetchMessage(msgQBoss, &rxMessage, 0); switch (rxMessage.messageType) { case MSG_TYPE_NO_MESSAGE: break; case MSG_TYPE_COMMAND: interpretCommand(decodeCommand(&rxMessage)); break; default: break; } } }
void commandInputCall() { char *command; command = XmTextGetString(commandInput); interpretCommand(command); XmTextSetString(commandInput,""); XtFree(command); }
void pConsole::executeCommand( const QString& command, bool writeCommand, bool showPrompt ) { const QStringList clearCommands = QStringList( "clear" ) << "cls"; if ( clearCommands.contains( command, Qt::CaseInsensitive ) ) { clear(); if ( showPrompt ) { displayPrompt(); } return; } // write command to execute if ( writeCommand ) { if ( !currentCommand().isEmpty() ) { displayPrompt(); } insertPlainText( command ); } // execute command int res; QString strRes = interpretCommand( command, &res ); // write output in different colors if needed if ( res == 0 ) { useColor( ctOutput ); } else { useColor( ctError ); } if ( !strRes.isEmpty() ) { appendPlainText( strRes ); } useColor( ctCommand ); // display the prompt again if needed if ( showPrompt ) { displayPrompt(); } }
/******************************************************************************** * Function name : struct command_s *interpretCommand(char *commandLine) * returns : pointer to a command_s structure containing all command * data, ready to be passed to executeCommand() * commandLine : command line input to tokenise and return in command * structure * * Created by : James Johns * Date created : 10/12/2011 * Description : tokenises commandLine by whitespace. first token is command * utility, followed by arguments * * returns tokens in command_s structure which needs * deallocating when no longer needed. * * NOTES : BUG - arguments of created command may have artifacts at the * end of their string ********************************************************************************/ struct command_s *interpretCommand(char *commandLine) { struct command_s *toRet = malloc(sizeof(struct command_s)); /* temporary argument storage. ready to copy into a perfect sized array later on */ char *tempStore[MAXARGUMENTCOUNT]; unsigned int startOfToken = 0; char *redirectedFileName; if (toRet == NULL) { perror("interpretCommand"); } else { toRet->next = NULL; toRet->backgroundTask = 0; toRet->inputFD = toRet->outputFD = -1; toRet->argc = 0; toRet->argv = malloc(sizeof(char *)*2); if (toRet->argv == NULL) { perror("interpretCommand: malloc"); free(toRet); return NULL; } int i = 0; /* ignore leading whitespace */ while ((i < strlen(commandLine)) && ((commandLine[i] == ' ') || (commandLine[i] == '\n'))) i++; if (i >= strlen(commandLine)) { /* no command to parse, so ignore it and return NULL */ fprintf(stderr, "Empty command detected\n"); free(toRet); toRet = NULL; return NULL; } startOfToken = i; /* could characters in command name, ready to allocate required space. */ for (; i < strlen(commandLine) && commandLine[i] != ' ' && commandLine[i] != '\n'; i++); /* count through characters until we reach the end * of the string or a tokenising character */ toRet->argv[0] = malloc(sizeof(char)*((i-startOfToken)+1)); toRet->argc++; strncpy(toRet->argv[0], &commandLine[startOfToken], i-(startOfToken)); /* startOfToken is an index. add one for calculating length properly */ toRet->argv[0][i-startOfToken] = NULL; toRet->argv[1] = NULL; /* make sure even for single commands, argv[] ends in a NULL pointer */ i++; startOfToken = i; /* loop until we reach the end of the string or we hit a new line character * tokenising the string at each space character */ while (i <= strlen(commandLine)) { switch (commandLine[i]) { case ' ': case '\n': case '\0': /* if 2 separators are found next to eachother, ignore the first separator and * use the second as the startOfToken point. */ if (i == startOfToken) { i++; startOfToken = i; break; } /* create a token from string */ tempStore[toRet->argc] = malloc(sizeof(char)*(i-startOfToken)+1); strncpy(tempStore[toRet->argc],commandLine+startOfToken,i-startOfToken); tempStore[toRet->argc][i-startOfToken] = '\0'; toRet->argc++; /* set records to beginning of the next possible token */ startOfToken = i; break; case '|': toRet->next = interpretCommand(commandLine+i+1); i = strlen(commandLine); break; case '&': /* send child process to background */ toRet->backgroundTask = 1; i++; break; case '>': /* next argument is the file to redirect into. */; i++; while (commandLine[i] == ' ' || commandLine[i] == '\t') i++; startOfToken = i; for (; i < strlen(commandLine) && commandLine != '\0' && commandLine[i] != '\n' && commandLine[i] != ' '; i++); if (i != startOfToken) { int filenameLength = (i-startOfToken); redirectedFileName = malloc(sizeof(char)*filenameLength); strncpy(redirectedFileName, commandLine, filenameLength); redirectedFileName[filenameLength] = '\0'; FILE *outputFile = fopen(redirectedFileName, "w"); if (outputFile == NULL) { perror("fopen"); } else { toRet->outputFD = fileno(outputFile); } } break; default: i++; break; } } /* copy arguments into command structure */ if (toRet->argc > 1) { toRet->argv = realloc(toRet->argv, sizeof(char *)*(toRet->argc+1)); /* loop through tempStore, copying all strings across to argv */ for (int j = 1; j < toRet->argc; j++) { toRet->argv[j] = tempStore[j]; } toRet->argv[toRet->argc] = NULL; } /* check I/O file descriptors and assign pipes as necessary */ if (toRet->outputFD == -1 && toRet->next != NULL) { /* create a pipe, assign to outputFD and next->outputFD */ int newpipe[2]; if (pipe(newpipe) == -1) { perror("interpretCommand: pipe"); destroyCommand(toRet); return NULL; } toRet->outputFD = newpipe[1]; toRet->next->inputFD = newpipe[0]; } else if (toRet->outputFD == -1 && toRet->next == NULL) { /* end of command list, output must be to stdout unless being rediected already. */ toRet->outputFD = fileno(stdout); } if (toRet->inputFD == -1) { /* if input has not been assigned yet, assign standard input to inputFD */ toRet->inputFD = fileno(stdin); } } return toRet; }