一、概述

位置: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分析

run_command函数分析的更多相关文章

  1. 第1阶段——uboot分析之查找命令run_command函数和命令定义过程(6)

    本节主要学习,run_command函数命令查找过程,命令生成过程 1.run_command函数命令查找过程分析:在u-boot界面中(main_loop();位于u-boot-1.1.6/comm ...

  2. uboot main_loop函数分析

    一.概述    main_loop()函数做的都是与具体平台无关的工作.主要包括的工作如下: (1)初始化启动次数限制机制 (2)Modem功能 (3)设置软件版本号 (4)启动延迟 (5)读取命令, ...

  3. uboot之run_command简单分析

    本文档简单分析了uboot中命令的实现.run_command函数的实现以及从uboot命令行接收并处理命令的过程. 作者: 彭东林 邮箱: pengdonglin137@163.com http:/ ...

  4. split(),preg_split()与explode()函数分析与介

    split(),preg_split()与explode()函数分析与介 发布时间:2013-06-01 18:32:45   来源:尔玉毕业设计   评论:0 点击:965 split()函数可以实 ...

  5. string函数分析

    string函数分析string函数包含在string.c文件中,经常被C文件使用.1. strcpy函数原型: char* strcpy(char* str1,char* str2);函数功能: 把 ...

  6. start_amboot()函数分析

    一.整体流程 start_amboot()函数是执行完start.S汇编文件后第一个C语言函数,完成的功能自然还是初始化的工作 . 1.全局变量指针r8设定,以及全局变量区清零 2.执行一些类初始化函 ...

  7. uboot的jumptable_init函数分析

    一.函数说明 函数功能:安装系统函数指针 函数位置:common/exports.c 二.函数分析 void jumptable_init (void) { int i; gd->jt = (v ...

  8. Linux-0.11内核源代码分析系列:内存管理get_free_page()函数分析

    Linux-0.11内存管理模块是源码中比較难以理解的部分,如今把笔者个人的理解发表 先发Linux-0.11内核内存管理get_free_page()函数分析 有时间再写其它函数或者文件的:) /* ...

  9. 31.QPainter-rotate()函数分析-文字旋转不倾斜,图片旋转实现等待

    在上章和上上上章: 28.QT-QPainter介绍 30.QT-渐变之QLinearGradient. QConicalGradient.QRadialGradient 学习了QPainter基础绘 ...

随机推荐

  1. matches()方法

    java.lang包中的String类和java.util.regex包中的Pattern,Matcher类中都有matches()方法,都与正则表达式有关.下面我分别举例:(字符串:"ab ...

  2. java小数点的两种处理方法

    1. java.text.DecimalFormat;        //此方法为四舍五入 例如:DecimalFormat df = new DecimalFormat("#.0" ...

  3. java中main函数解析(转载)

    从写java至今,写的最多的可能就是主函数 public static void main(String[] args) {} 但是以前一直都没有问自己,为什么要这么写,因为在c语言中就没有这样子的要 ...

  4. HDU2699+Easy

    简单题. /* */ #include<stdio.h> #include<string.h> #include<stdlib.h> #include<alg ...

  5. 廖雪锋笔记2:list,tuble

    list:元素值不固定,元素类型不固定 apend(xx) insert(INDEX,xx) pop(index) 索引元素: [0] [1] [2] [-1] [-2] LIST,TUBLE变量值是 ...

  6. Qt 学习之路:QStringListModel

    上一章我们已经了解到有关 list.table 和 tree 三个最常用的视图类的便捷类的使用.前面也提到过,由于这些类仅仅是提供方便,功能.实现自然不如真正的 model/view 强大.从本章起, ...

  7. XML解析技术研究(一)

      摘要:XML作为过去十年中出现的最流行的技术之一,得到了广泛的应用,而其中XML解析技术是XML应用的关键.本文介绍了XML解析技术的研究动向,分析和比较了4种XML解析技术的优劣,并归纳总结了应 ...

  8. css兼容性问题

    其实做网页最大的问题还是兼容性吧,要调试IE的各种浏览器. DIV+CSS设计IE6.IE7.FF 兼容性  DIV+CSS网页布局这是一种趋势,我也开始顺应这股趋势了,不过在使用DIV+CSS网站设 ...

  9. O2O的理解

    O2O 就是把线上和线下的优势完美结合,网上优惠价格,线下享受贴身服务!O2O 还可以实现不同商家的联盟 两年前的微博营销,现在没落了,那么微信营销呢,只能说来的太快了.线上来筛选服务和产品,所有的交 ...

  10. 解决win service 2003 IIS发布Gis网站后,访问地图服务出错,无法正常打开而且 事件查看器出现错误提示。

    错误详情: 应用程序-特定 权限设置未将 COM 服务器应用程序(CLSID 为{379376DB-AEA6-40D1-9491-9345E61EF6BE})的 本地 激活 权限授予用户 NT AUT ...