📄 command.cc
字号:
break; case SY_STRING: if (!parm->as_string()) return(DD_FALSE); break; case SY_DATALIST: if (!set_data_list(parm, &iparam)) return(DD_FALSE); break; default: return(DD_FALSE); } p++; iparam++; if (iparam < params->count) parm= (class cl_cmd_arg *)(params->at(iparam)); else parm= 0; } if (!*p && !parm) { matched_syntax= syntax; return(DD_TRUE); } return(DD_FALSE);}boolcl_cmdline::set_data_list(class cl_cmd_arg *parm, int *iparm){ class cl_cmd_arg *next_parm; int len, i, j; t_mem *array; len= 0; array= 0; for (i= *iparm, next_parm= param(i); next_parm; i++, next_parm= param(i)) { if (next_parm->is_string()) { int l; char *s; //s= proc_escape(next_parm->get_svalue(), &l); if (!next_parm->as_string()) continue; s= next_parm->value.string.string; l= next_parm->value.string.len; if (!array) array= (t_mem*)malloc(sizeof(t_mem)*l); else array= (t_mem*)realloc(array, sizeof(t_mem)*(l+len)); for (j= 0; j < l; j++) { array[len]= s[j]; len++; } //if (s) //free(s); } else { if (!next_parm->as_data()) { if (array) free(array); return(DD_FALSE); } if (!array) array= (t_mem*)malloc(sizeof(t_mem)); else array= (t_mem*)realloc(array, sizeof(t_mem)*(1+len)); array[len]= next_parm->value.data; len++; } } *iparm= i; parm->value.data_list.array= array; parm->value.data_list.len= len; return(DD_TRUE);}/* * Command *____________________________________________________________________________ */cl_cmd::cl_cmd(enum cmd_operate_on op_on, char *aname, int can_rep, char *short_hlp, char *long_hlp): cl_base(){ operate_on= op_on; names= new cl_strings(1, 1); names->add(aname?strdup(aname):strdup("unknown")); can_repeat= can_rep; short_help= short_hlp?strdup(short_hlp):NULL; long_help= long_hlp?strdup(long_hlp):NULL;}/*cl_cmd::cl_cmd(class cl_sim *asim): cl_base(){ sim= asim; name= short_help= long_help= 0; can_repeat= 0;}*/cl_cmd::~cl_cmd(void){ delete names; if (short_help) free(short_help); if (long_help) free(long_help);}voidcl_cmd::add_name(char *nam){ if (nam) names->add(strdup(nam));}intcl_cmd::name_match(char *aname, int strict){ int i; if (names->count == 0 && !aname) return(1); if (!aname) return(0); if (strict) { for (i= 0; i < names->count; i++) { char *n= (char*)(names->at(i)); if (strcmp(aname, n) == 0) return(1); } } else { for (i= 0; i < names->count; i++) { char *n= (char*)(names->at(i)); if (strstr(n, aname) == n) return(1); } } return(0);}intcl_cmd::name_match(class cl_cmdline *cmdline, int strict){ return(name_match(cmdline->get_name(), strict));}intcl_cmd::syntax_ok(class cl_cmdline *cmdline){ return(1);}intcl_cmd::work(class cl_app *app, class cl_cmdline *cmdline, class cl_console *con){ if (!syntax_ok(cmdline)) return(0); class cl_sim *sim= app->get_sim(); class cl_uc *uc= 0; if (sim) uc= sim->uc; switch (operate_on) { case operate_on_app: if (!app) { con->dd_printf("There is no application to work on!\n"); return(DD_TRUE); } return(do_work(app, cmdline, con)); case operate_on_sim: if (!sim) { con->dd_printf("There is no simulator to work on!\n"); return(DD_TRUE); } return(do_work(sim, cmdline, con)); case operate_on_uc: if (!sim) { con->dd_printf("There is no microcontroller to work on!\n"); return(DD_TRUE); } return(do_work(uc, cmdline, con)); default: return(do_work(cmdline, con)); }}intcl_cmd::do_work(class cl_cmdline *cmdline, class cl_console *con){ con->dd_printf("Command \"%s\" does nothing.\n", (char*)(names->at(0))); return(0);}intcl_cmd::do_work(class cl_app *app, class cl_cmdline *cmdline, class cl_console *con){ con->dd_printf("Command \"%s\" does nothing on application.\n", (char*)(names->at(0))); return(0);}intcl_cmd::do_work(class cl_sim *sim, class cl_cmdline *cmdline, class cl_console *con){ con->dd_printf("Command \"%s\" does nothing on simulator.\n", (char*)(names->at(0))); return(0);}intcl_cmd::do_work(class cl_uc *uc, class cl_cmdline *cmdline, class cl_console *con){ con->dd_printf("Command \"%s\" does nothing on microcontroller.\n", (char*)(names->at(0))); return(0);}/* * Set of commands *____________________________________________________________________________ */cl_cmdset::cl_cmdset(void): cl_list(5, 5){ //sim= 0; last_command= 0;}/*cl_cmdset::cl_cmdset(class cl_sim *asim): cl_list(5, 5){ sim= asim; last_command= 0;}*/class cl_cmd *cl_cmdset::get_cmd(class cl_cmdline *cmdline){ int i; if (cmdline->repeat()) { if (last_command) return(last_command); else return(0); } // exact match for (i= 0; i < count; i++) { class cl_cmd *c= (class cl_cmd *)at(i); if (c->name_match(cmdline, 1)) return(c); } // not exact match class cl_cmd *c_matched= 0; for (i= 0; i < count; i++) { class cl_cmd *c= (class cl_cmd *)at(i); if (c->name_match(cmdline, 0)) { if (!c_matched) c_matched= c; else return(0); } } return(c_matched); //return(0);}class cl_cmd *cl_cmdset::get_cmd(char *cmd_name){ int i; for (i= 0; i < count; i++) { class cl_cmd *c= (class cl_cmd *)at(i); if (c->name_match(cmd_name, 1)) return(c); } return(0);}voidcl_cmdset::del(char *nam){ int i; if (!nam) return; for (i= 0; i < count; i++) { class cl_cmd *cmd= (class cl_cmd *)(at(i)); if (cmd->name_match(nam, 1)) free_at(i); }}voidcl_cmdset::replace(char *nam, class cl_cmd *cmd){ int i; if (!nam) return; for (i= 0; i < count; i++) { class cl_cmd *c= (class cl_cmd *)(at(i)); if (c->name_match(nam, 1)) { delete c; put_at(i, cmd); } }}/* * Composed command: subset of commands *____________________________________________________________________________ */cl_super_cmd::cl_super_cmd(char *aname, int can_rep, char *short_hlp, char *long_hlp, class cl_cmdset *acommands): cl_cmd(operate_on_none, aname, can_rep, short_hlp, long_hlp){ commands= acommands;}cl_super_cmd::~cl_super_cmd(void){ if (commands) delete commands;}intcl_super_cmd::work(class cl_app *app, class cl_cmdline *cmdline, class cl_console *con){ class cl_cmd *cmd= 0; if (!commands) return(0); if (!cmdline->shift()) { if ((cmd= commands->get_cmd("_no_parameters_")) != 0) return(cmd->work(app, cmdline, con)); int i; con->dd_printf("\"%s\" must be followed by the name of a subcommand\n" "List of subcommands:\n", (char*)(names->at(0))); for (i= 0; i < commands->count; i++) { cmd= (class cl_cmd *)(commands->at(i)); con->dd_printf("%s\n", cmd->short_help); } return(0); } if ((cmd= commands->get_cmd(cmdline)) == NULL) { con->dd_printf("Undefined subcommand: \"%s\". Try \"help %s\".\n", cmdline->get_name(), (char*)(names->at(0))); return(0); } return(cmd->work(app, cmdline, con));}/* End of cmd.src/command.cc */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -