static bool ParseCmdBuffer(SYS_CMD_DEVICE_NODE* pCmdIO) { int argc = 0; char *argv[MAX_CMD_ARGS + 1] = {}; static char saveCmd[SYS_CMD_MAX_LENGTH+1]; const void* cmdIoParam = pCmdIO->cmdIoParam; int ix, grp_ix; const SYS_CMD_DESCRIPTOR* pDcpt; strncpy(saveCmd, pCmdIO->cmdBuff, sizeof(saveCmd)); // make a copy of the command // parse a command string to *argv[] argc = StringToArgs(saveCmd, argv); if(argc != 0) { // ok, there's smth here // add it to the history list cmdNode* pN = CmdRemoveTail(&_cmdList); strncpy(pN->cmdBuff, pCmdIO->cmdBuff, sizeof(saveCmd)); // Need save non-parsed string CmdAddHead(&_cmdList, pN); _pCurrCmdN = 0; // try built-in commands first for(ix = 0, pDcpt = _builtinCmdTbl; ix < sizeof(_builtinCmdTbl)/sizeof(*_builtinCmdTbl); ix++, pDcpt++) { if(!strcmp(argv[0], pDcpt->cmdStr)) { // command found return (*pDcpt->cmdFnc)(pCmdIO, argc, argv); // call command handler } } // search user commands for (grp_ix=0; grp_ix<MAX_CMD_GROUP; grp_ix++) { if (_usrCmdTbl[grp_ix].pCmd == 0) { continue; } for(ix = 0, pDcpt = _usrCmdTbl[grp_ix].pCmd; ix < _usrCmdTbl[grp_ix].nCmds; ix++, pDcpt++) { if(!strcmp(argv[0], pDcpt->cmdStr)) { // command found return !(*pDcpt->cmdFnc)(pCmdIO, argc, argv); } } } // command not found (*pCmdIO->pCmdApi->msg)(cmdIoParam, " *** Command Processor: unknown command. ***\r\n"); } else { (*pCmdIO->pCmdApi->msg)(cmdIoParam, " *** Command Processor: Please type in a command***" LINE_TERM); } return false; }
bool _SYS_COMMAND_INIT(void) { int ix; CommandCleanup(); // just in case we have to deallocate previous data // construct the command history list for(ix = 0; ix<COMMAND_HISTORY_DEPTH; ix++) { cmdNode* pN; pN = (cmdNode*)SystemMalloc(sizeof(*pN)); if(!pN) { return false; } pN->cmdBuff[0] = '\0'; CmdAddHead(&_cmdList, pN); } _pCurrCmdN = 0; // Console will be added automatically #if defined(SYS_CONSOLE_ENABLE) // the console handle should be needed here but there's only one console for now _SYS_CMDIO_ADD(&sysConsoleApi, 0); #endif //SYS_CONSOLE_MESSAGE(_promptStr); _cmdAlive = true; return true; }