struct cmd_tbl_s { char *name; /* Command Name */ int maxargs; /* maximum number of arguments */ cmd_func cmd; /* Implementation function */ char *usage; /* Usage message (short) */ #ifdef CONFIG_SYS_LONGHELP char *help; /* Help message (long) */ #endif #ifdef CONFIG_AUTO_COMPLETE /* do auto completion on the arguments */ int (*complete)(int argc, char * const argv[], char last_char, int maxv, char *cmdv[]); #endif };
typedef struct cmd_tbl_s cmd_tbl_t;
/* someone should implement the table */ extern cmd_tbl_t cmd_table[];
if (strlen(cmd) >= CB_SIZE) { cmd_puts ("## Command too long!\n"); return -1; }
strcpy (cmdbuf, cmd);
/* Process separators and check for invalid * repeatable commands */
#ifdef DEBUG_PARSER cmd_printf ("[PROCESS_SEPARATORS] %s\n", cmd); #endif while (*str) {
/* * Find separator, or string end * Allow simple escape of ';' by writing "\;" */ for (inquotes = 0, sep = str; *sep; sep++) { if ((*sep=='\'') && (*(sep-1) != '\\')) inquotes=!inquotes;
if (!inquotes && (*sep == ';') && /* separator */ ( sep != str) && /* past string start */ (*(sep-1) != '\\')) /* and NOT escaped */ break; }
/* * Limit the token to data between separators */ token = str; if (*sep) { str = sep + 1; /* start of command for next pass */ *sep = '\0'; } else str = sep; /* no more commands for next pass */ #ifdef DEBUG_PARSER cmd_printf ("token: \"%s\"\n", token); #endif
/* Extract arguments */ if ((argc = parse_line (token, argv)) == 0) { rc = -1; /* no command at all */ continue; }
/* Look up command in command table */ if ((cmdtp = find_cmd(argv[0])) == NULL) { cmd_printf ("Unknown command '%s' - try 'help'\n", argv[0]); rc = -1; /* give up after bad command */ continue; }
/* found - check max args */ if (argc > cmdtp->maxargs) { cmd_usage(cmdtp); rc = -1; continue; }
/* OK - call function to do the command */ if ((cmdtp->cmd) (argc, argv) != 0) { rc = -1; } }