void add_command(user_command_t *cmd)//将命令加入链表
{
if (head_cmd == NULL) {
head_cmd = tail_cmd = cmd;//很普通的链表操作。
} else {
tail_cmd->next_cmd = cmd;
tail_cmd = cmd;
}
/*printk("Registered '%s' command\n", cmd->name);*/
}
/* find command */
user_command_t *find_cmd(const char *cmdname)
{
user_command_t *curr;
/* do da string compare for the first offset character of cmdstr
against each number of the cmdlist */
curr = head_cmd;//看见strncmp了吗? 自己实现的。
while(curr != NULL) {
if (strncmp(curr->name, cmdname, strlen(cmdname)) == 0)
return curr;
curr = curr->next_cmd;
}
return NULL;
}
/* execute a function */
void execcmd(int argc, const char **argv)
{
user_command_t *cmd = find_cmd(argv[0]);
if (cmd == NULL) {
printk("Could not found '%s' command\n", argv[0]);
printk("If you want to konw available commands, type 'help'\n");
return;
}
/*printk("execcmd: cmd=%s, argc=%d\n", argv[0], argc);*/
cmd->cmdfunc(argc, argv);
}
/* parse and execute a string */
void exec_string(char *buf)
{
int argc;
char *argv[128];
char *resid;
while (*buf) {
memset(argv, 0, sizeof(argv));
parseargs(buf, &argc, argv, &resid);
if (argc > 0)
execcmd(argc, (const char **)argv);
buf = resid;
}
}
/*
* For sub-commands
*/
void execsubcmd(user_subcommand_t *cmds, int argc, const char **argv)
{
while (cmds->name != NULL) {
if (strncmp(argv[0], cmds->name, strlen(argv[0])) == 0) {
/*printk("subexeccmd: cmd=%s, argc=%d\n", argv[0], argc);*/
cmds->cmdfunc(argc, argv);
return;
}
cmds++;
}
printk("Could not found '%s' sub-command\n", argv[0]);
}
void print_usage(char *strhead, user_subcommand_t *cmds)
{
printk("Usage:\n");
while (cmds->name != NULL) {
if (strhead)
printk("%s ", strhead);
if (*cmds->helpstr)
printk("%s\n", cmds->helpstr);
cmds++;
}
}
void invalid_cmd(const char *cmd_name, user_subcommand_t *cmds)
{
printk("invalid '%s' command: wrong argumets\n", cmd_name);
print_usage(" ", cmds);
}
/*
* Define (basic) built-in commands
*/
#if 0
"help [{cmds}] -- Help about help?"
"boot [{cmds}] - Booting linux kernel"
"call <addr> <args> -- Execute binaries"
"dump <addr> <length> -- Display (hex dump) a range of memory."
"flash [{cmds}]-- Manage Flash memory"
"info -- Display vivi information"
"load [{cmds}] -- Load a file"
"mem -- Show info about memory"
"reset -- Reset the System"
"param [eval|show|save [-n]|reset]"
"part [ help | add | delete | show | reset ] -- MTD partition"
"test -- Test items"
#endif
/* help command */
void command_help(int argc, const char **argv)
{
user_command_t *curr;
/* help <command>. invoke <command> with 'help' as an argument */
if (argc == 2) {
if (strncmp(argv[1], "help", strlen(argv[1])) == 0) {
printk("Are you kidding?\n");
return;
}
argv[0] = argv[1];
argv[1] = "help";
execcmd(argc, argv);
return;
}
printk("Usage:\n");
curr = head_cmd;
while(curr != NULL) {
printk(" %s\n", curr->helpstr);
curr = curr->next_cmd;
}
}
|