****************************************************************************
* returns:
* 1 - command executed, repeatable
* 0 - command executed but not repeatable, interrupted commands are
* always considered not repeatable
* -1 - not executed (unrecognized, bootd recursion or too many args)
* (If cmd is NULL or "" or longer than CFG_CBSIZE-1 it is
* considered unrecognized)
*
* WARNING:
*
* We must create a temporary copy of the command since the command we get
* may be the result from getenv(), which returns a pointer directly to
* the environment data, which may change magicly when the command we run
* creates or modifies environment variables (like "bootp" does).
*/
// 先看返回值, 返回值为int,1 ----可重复执行的命令 0- 不可重复执行的命令 ,例如 中断, 比较明显常见的就是 exit, quit,ctrl+C
-1 --------不可执行,非法命令。
int run_command (const char *cmd, int flag)
{
cmd_tbl_t *cmdtp;
char cmdbuf[CFG_CBSIZE];/* working copy of cmd*/
char *token; /* start of token in cmdbuf */
char *sep; /* end of token (separator) in cmdbuf */
char finaltoken[CFG_CBSIZE];
char *str = cmdbuf;
char *argv[CFG_MAXARGS + 1];/* NULL terminated*/
int argc, inquotes;
int repeatable = 1;
int rc = 0;
先看看cmd_tbl_t 是什么。
--------------------------------------------------------------------------------------------------------------------------------------------
struct cmd_tbl_s {
char *name; /* Command Name */
int maxargs; /* maximum number of arguments */
int repeatable; /* autorepeat allowed? */
/* Implementation function */
int (*cmd)(struct cmd_tbl_s *, int, int, char *[]);
char *usage; /* Usage message (short) */
#ifdef CFG_LONGHELP
char *help; /* Help message (long) */
#endif
#ifdef CONFIG_AUTO_COMPLETE
/* do auto completion on the arguments */
int (*complete)(int argc, char *argv[], char last_char, int maxv, char *cmdv[]);
#endif
};
typedef struct cmd_tbl_s cmd_tbl_t;
cmd_tbl_t 是一个结构体类型,定义了一个command结构体。
---------------------------------------------------------------------------------------------------------------------------------------------
clear_ctrlc();/* forget any previous Control C */ //为什么forget Control C ?
if (!cmd || !*cmd) {
return -1; /* empty command */
}
if (strlen(cmd) >= CFG_CBSIZE) {
puts ("## Command too long!\n");
return -1;
}
strcpy (cmdbuf, cmd); //copy cmd to cmdbuf
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; // 这个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 printf ("token: \"%s\"\n", token); #endif
/* find macros in this token and replace them */ process_macros (token, finaltoken); //在命令中可能包含一些环境变量,函数process_macros将获取这些环境变量将命令展开存入finaltoken中。 /* Extract arguments */ /* 将命令行中的关键词取出放入argv中, 注意, 命令行被分解到argv数组中, Extract arguments */
比如我们输入的命令 bootdelay=3; 转换以后我们得到argv[0]="bootdelay" argv[1]="3"
if ((argc = parse_line (finaltoken, argv)) == 0) {
/* Look up command in command table */rc = -1; /* no command at all */ continue; } /* 查找命令, 我们认为命令行中的第一个关键词就是命令, Look up command in command table */
if ((cmdtp = find_cmd(argv[0])) == NULL) {
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) {
printf ("Usage:\n%s\n", cmdtp->usage);
rc = -1;
continue;
}
#if (CONFIG_COMMANDS & CFG_CMD_BOOTD)
/* avoid "bootd" recursion */
if (cmdtp->cmd == do_bootd) {
#ifdef DEBUG_PARSER
printf ("[%s]\n", finaltoken);
#endif
if (flag & CMD_FLAG_BOOTD) {
puts ("'bootd' recursion detected\n");
rc = -1;
continue;
} else {
flag |= CMD_FLAG_BOOTD;
}
}
#endif /* CFG_CMD_BOOTD */
/* OK - call function to do the command */
if ((cmdtp->cmd) (cmdtp, flag, argc, argv) != 0) {
rc = -1;
}
repeatable &= cmdtp->repeatable;
/* Did the user stop this? */
if (had_ctrlc ())
return 0; /* if stopped then not repeatable */
}
return rc ? rc : repeatable;
/****************************************************************************/