首页 > 其他 > 详细

run_command函数分析

时间:2014-02-20 02:45:47      阅读:346      评论:0      收藏:0      [点我收藏+]

/****************************************************************************
* 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 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;

#ifdef DEBUG_PARSER
printf ("[RUN_COMMAND] cmd[%p]=\"", cmd);
puts (cmd ? cmd : "NULL"); /* use puts - string may be loooong */
puts ("\"\n");
#endif

clear_ctrlc(); /* forget any previous Control C */

if (!cmd || !*cmd) { //先是对命令的有效性进行检测
return -1; /* empty command */
}

if (strlen(cmd) >= CFG_CBSIZE) {//判断命令的长度是否在允许的范围内
puts ("## Command too long!\n");
return -1;
}

strcpy (cmdbuf, cmd);//备份命令

/* Process separators and check for invalid
* repeatable commands
*/

#ifdef DEBUG_PARSER
printf ("[PROCESS_SEPARATORS] %s\n", cmd);
#endif
while (*str) { // 输入的字符串是否为空

/*
* Find separator, or string end
* Allow simple escape of ‘;‘ by writing "\;"
*/
//下边的for循环是寻找输入命令的分割符或者结尾,因为uboot允许一次输入多个命令
//每次处理一个命令
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;//token指向命令的开头
if (*sep) {
//如果是分隔符的话,将分隔符替换成空字符
str = sep + 1; /* start of command for next pass */
//str指向下一句的开头
*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);
//将命令中的宏替换掉,例如$(kernelsize)替换成内核的大小
/* Extract arguments */ //提取参数,将每一个参数用‘\0‘分开,argv中的每一个指针指向一个参数
//的起始地址,返回值为参数的个数
if ((argc = parse_line (finaltoken, argv)) == 0) {
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;//设置命令重复执行标志,也就是按enter直接执行上次命令

/* Did the user stop this? */
//检查是否有control+c按下,如果有就结束当前命令
if (had_ctrlc ())
return 0; /* if stopped then not repeatable */
}

return rc ? rc : repeatable;
}

 

参考:run_command分析

run_command函数分析

原文:http://www.cnblogs.com/amanlikethis/p/3556128.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!