uboot中命令行解析
Posted zhuangquan
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了uboot中命令行解析相关的知识,希望对你有一定的参考价值。
1.命令行解析
命令行解析函数:run_command
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 "\;" */
//将多命令拆解(比如:printenv ;saveenv) 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 printf ("token: \"%s\"\n", token); #endif /* find macros in this token and replace them */
//处理字符串中的转义字符 process_macros (token, finaltoken); /* Extract arguments */
//解析命令(比如md.w 0这个字符串就会解析成argv[0]="md.w" argv=[1]="0") if ((argc = parse_line (finaltoken, argv)) == 0) rc = -1; /* no command at all */ continue; /* Look up command in command table */
//根据argv[0],然后进行对比。找出相对应的命令,执行相对应得函数 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;
以上是关于uboot中命令行解析的主要内容,如果未能解决你的问题,请参考以下文章