void CommandControl::parseCmd(byte *rxData, byte rxLen){ //copy new rx data to rx buffer, and increase the rx len in buffer if((rxLenInBuffer + rxLen) <= RX_BUFFER_LEN){ memcpy(rxBuffer+rxLenInBuffer,rxData,rxLen); rxLenInBuffer += rxLen; } else{//copy new rx data to the beginning of rx buffer, as it is overflowing memcpy(rxBuffer,rxData,rxLen); rxLenInBuffer = rxLen; } //find the start flag of the command to be processed int startPos = 0; while(1){ //Serial.println("here1"); if((startPos = findCmd()) >= 0){ //Serial.println(startPos); processCmd((byte)startPos); } else{ break; } } }
/* ******************************************************************************* * * FUNCTION: ExecCommandline * * DESCRIPTION: This function is used to process an input string, split the * input string into command and parameter strings, find a command * structure in the command list array and execute the command. * * INPUT PARAMETERS: cmdListP is a pointer to the command list array. * cmdStringP is a pointer to the input string. * * RETURNS: none. ******************************************************************************* */ void ExecCommandline (CommandListEntry_T * cmdListP, char * inputStringP) { CommandListEntry_T * cmdListEntryP; int argc; char *argv[16]; char *resid; while (*inputStringP) { memset(argv, 0, sizeof(argv)); parseargs(inputStringP, &argc, argv, &resid); if (argc > 0) { if ((cmdListEntryP=findCmd(cmdListP, argv[0])) == NULL) { SerialPutString("Command is not found\r\n"); } else { cmdListEntryP->func(argc, (const char **)argv); } } inputStringP = resid; } }
void ShellClass::update(){ int index; String line; ShellCommand cmd; if (serialPort->available()){ while (serialPort->available() && rxBuffer.getSize() < rxBuffer.getCapacity()){ rxBuffer.append(serialPort->read()); } index = rxBuffer.indexOf('\n'); if (index != -1){ line = rxBuffer.toString(0, index); line.trim(); if (findCmd(line, &cmd) != -1){ (this->*cmd.cmdFun)(line); //Log.e(SHELL_TAG) << ("Cmd executed") << Endl; } else{ Log.e(SHELL_TAG) << F("Command not found") << Endl; } rxBuffer.clear(); } } }
static int runCommand(void) { char *cmdBuffer = consoleBuffer; int len = strlen(cmdBuffer); char *argv[SYS_MAX_ARGS]; int argc; CmdTableT *cmdtp; if (len > SYS_CBSIZE) { puts("## Command too long!"); return -1; } if ((argc = parseLine(cmdBuffer, argv)) == 0) { puts("## No command at all"); return -1; } if ((cmdtp = findCmd(argv[0])) == NULL) { printf("Unknown command '%s' - try 'help'\n", argv[0]); return -1; } if (argc > cmdtp->maxargs) { cmdUsage(cmdtp); return -1; } return (cmdtp->cmd)(cmdtp, argc, argv); }
void CmdLine::parseCmdLine(const vector<string>& mArgs) { deque<string> args{begin(mArgs), end(mArgs)}; if(mArgs.size() == 0) { log("no command specified!"); return; } Cmd& cmd(findCmd(args.front())); args.pop_front(); // Find all flags, put them in cFlags, remove them from mArgs vector<string> cFlags; for(const auto& s : args) { if(startsWith(s, flagPrefixShort) || startsWith(s, flagPrefixLong)) { cFlags.push_back(s); if(cFlags.size() > cmd.getFlagCount()) throw runtime_error("Incorrect number of flags for command " + cmd.getNamesString() + " , correct number is '" + toStr(cmd.getFlagCount()) + "'"); } } for(const auto& f : cFlags) eraseRemove(args, f); // Find args, put them in cArgs vector<string> cArgs; for(unsigned int i{cmd.getArgCount()}; i > 0; --i) { if(args.empty()) throw runtime_error("Incorrect number of args for command " + cmd.getNamesString() + " , correct number is '" + toStr(cmd.getArgCount()) + "'"); cArgs.push_back(args.front()); args.pop_front(); } // Remaining string in args must be optargs vector<string> cOptArgs; while(!args.empty()) { cOptArgs.push_back(args.front()); if(cOptArgs.size() > cmd.getOptArgCount()) throw runtime_error("Incorrect number of optargs for command " + cmd.getNamesString() + " , correct number is '" + toStr(cmd.getOptArgCount()) + "'"); args.pop_front(); } for(unsigned int i{0}; i < cArgs.size(); ++i) cmd.setArgValue(i, cArgs[i]); for(unsigned int i{0}; i < cOptArgs.size(); ++i) cmd.setOptArgValue(i, cOptArgs[i]); for(const auto& f : cFlags) cmd.activateFlag(f); cmd(); }
int do_help(CmdTableT *cmdtp, int argc, char *argv[]) { int i; if (argc == 1) { for (i = 0; i < CMD_ITEMS; i++) { printf("%*s- %s\n", 8, cmdTable[i].name, cmdTable[i].usage); } } else { for (i = 1; i < argc; i++) { if ((cmdtp = findCmd(argv[i])) != NULL) { cmdUsage(cmdtp); } else { printf("Unknown comand '%s' - try help;" " without arguments for list of all" " known commands\n\n", argv[i]); } } } return 0; }
void main(int argc, char *argv[]) { int i = 0, cmd = -1, fd = 0; char line[128], cname[64], temp_pathname[124], pathname[124] = "NULL", basename[124] = "NULL", dirname[124] = "NULL", parameter[124] = "NULL", *device; //GETS DISK NAME TO MOUNT /* printf("Enter the name of your disk image\n"); fgets(device, 128, stdin); device[strlen(device)-1] = 0; // kill the \n char at end */ device = "mydisk"; mount_root(device, &fd); while(1) { //printf("P%d running: ", running.uid); printf("nick's@linux:"); pwd(P0.cwd); printf("$ "); fgets(line, 128, stdin); line[strlen(line)-1] = 0; // kill the \n char at end if (line[0]==0) continue; sscanf(line, "%s %s %s", cname, pathname, parameter); if(strcmp(pathname,"NULL") != 0) strcpy(PATHNAME, pathname); if(strcmp(parameter,"NULL") != 0) strcpy(PARAMETER, parameter); cmd = findCmd(cname); // map cname to an index switch(cmd) { case 0 : menu(); break; case 1 : pwd(P0.cwd);printf("\n"); break; case 2 : ls(); break; case 3 : cd(); break; case 4 : make_dir(); break; case 5 : rmdir(); break; case 6 : creat_file(); break; case 7 : link(); break; case 8 : unlink(); break; case 9 : symlink(); break; case 10: rm_file(); break; case 11: chmod_file(); break; case 12: chown_file(); break; case 13: stat_file(); break; case 14: touch_file(); break; case 20: open_file(); break; //LEVEL 2 case 21: close_file((int)PATHNAME); break; case 22: pfd(); break; case 23: lseek_file(); break; case 24: //access_file(); break; break; case 25: //read_file(); break; case 26: write_file(); break; case 27: cat_file(); break; case 28: cp_file(); break; case 29: mv_file(); break; case 30: mount(); break; //LEVLEL 3 case 31: //umount(); break; case 32: //cs(); break; case 33: //do_fork(); break; case 34: //do_ps(); break; case 40: //sync(); break; case 41: quit(); break; case 42 : system("clear"); break; default: printf("invalid command\n"); break; } //RESET NAMES strcpy(parameter, "NULL"); strcpy(pathname, "NULL"); strcpy(PATHNAME, "NULL"); } }
int main(int argc, char* argv[]) { int (*p[27]) (char pathname[], char *parameter); int i = 0, cmd; char line[128]; if (argc > 1) { device = argv[1]; printf("Device = %s\n", device); } init(); // initialize function table p[0] = menu; p[1] = make_dir; p[2] = cd; p[3] = pwd; p[4] = ls; p[5] = remove_dir; p[6] = creat_file; p[7] = linker; p[8] = ulink; p[9] = sylink; p[10] = rm_file; p[11] = chmod_file; p[12] = chown_file; p[13] = stater; p[14] = touch_file; p[15] = open_file; p[16] = close_file; p[17] = read_file; p[18] = write_file; p[19] = lseek_file; p[20] = cat_file; p[21] = cp_file; p[22] = mv_file; p[23] = mount; p[24] = umount; p[25] = quit; p[26] = wrongcmd; while(1) { char *name_of_dir = NULL; i = 0; while(minode[i]->refCount != 0) { printf("minode[%d]->refCount: %d\n", i, minode[i]->refCount); i++; } printf("Running: P%d\n", running->pid); printf("> "); fgets(line, 128, stdin); line[strlen(line)-1] = 0; sscanf(line, "%s %s %s", command, pathname, parameter); cmd = findCmd(); (*p[cmd])(pathname, parameter); memset(path, 0, 64); memset(command, 0, 64); memset(pathname, 0, 64); memset(parameter, 0, 64); startfromroot = 0; pathlen = 0; } return 0; }