一、概述

位置:common/main.c

功能:根据传入参数(命令),在命令存储区(.u_boot_cmd)中查找对应的命令,找到命令并调用对应的函数执行

流程:

二、分析

1、函数说明信息

/****************************************************************************
* 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).
*/

2、具体内容分析

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分析

最新文章

  1. 使用pyinstaller打包Python应用,生成EXE执行文件
  2. 作业六—图书管理系统(SPEC)系统性能评估测试
  3. 系统广播 android.intent.action.KILL_BACKGROUND_SERVICE
  4. tornado初步 ppt分享
  5. C++实现水波纹、火焰和血浆效果
  6. LR实现http协议性能测试脚本
  7. C++中的随机数函数(
  8. PHP-语法(www.w3school.com.cn/php)
  9. 翻译器DIY它———算在英文文本中的单词数,字符和行数
  10. spring mvc 入门示例
  11. mysql数据库定时备份
  12. 解决ajax的parsererror错误的终极办法(后台传给前台的数据json问题)
  13. [BBS]搭建开源论坛之Jforum搭配开源CKEDITOR
  14. Linux上删除大量文件几种方式对比
  15. C# ComboBox绑定值问题
  16. windows下 mysql 5.6.40 卸载 安装 修改密码
  17. 好的LCT板子和一句话
  18. 《Zero MQ》
  19. win10 激活(亲测可用)
  20. MySQL中正则表达式

热门文章

  1. HTTP 返回时间 概念 TTFB..
  2. 【Android - MD】之TabLayout的使用
  3. 如何优化cocos2d程序的内存使用和程序大小:第一部分_(转)
  4. FreeMarker 自己定义指令(三)
  5. 数据存储(一)--SharedPreferences之你不知道的事
  6. android 40 Io编程
  7. ios中从相册:相机中获取图片信息
  8. [转] 三步将你的 React Native 项目运行在 Web 浏览器上面
  9. 基于Html5的兼容所有主流浏览器的在线视频播放器videoJs
  10. python 学习笔记(二)两种方式实现第一个python程序