From b8da38ae9a0e6a572da13a361aaee56ebbda0298 Mon Sep 17 00:00:00 2001 From: Frank Li Date: Fri, 28 Jun 2019 11:13:05 -0500 Subject: [PATCH] Add dry run option to check if script is correct libuuu API changed, new -dry argument added at some functions Signed-off-by: Frank Li --- appveyor.yml | 2 +- libuuu/cmd.cpp | 91 +++++++++++++++++++++++++++++++++++++-------------------- libuuu/cmd.h | 2 +- libuuu/libuuu.h | 6 ++-- uuu/uuu.cpp | 26 ++++++++++++++--- 5 files changed, 86 insertions(+), 41 deletions(-) diff --git a/appveyor.yml b/appveyor.yml index 4896923..77868ee 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -1,4 +1,4 @@ -version: 1.2.{build} +version: 1.3.{build} image: - Visual Studio 2017 - Ubuntu1804 diff --git a/libuuu/cmd.cpp b/libuuu/cmd.cpp index d3cc228..2f0e0ea 100644 --- a/libuuu/cmd.cpp +++ b/libuuu/cmd.cpp @@ -129,7 +129,20 @@ int CmdBase::parser(char *p) return 0; } -int CmdList::run_all(CmdCtx *p, bool dry_run) +int CmdBase::dump() +{ + uuu_notify nt; + nt.type = uuu_notify::NOTIFY_CMD_INFO; + + string str = m_cmd; + str += "\n"; + nt.str = (char*)str.c_str(); + call_notify(nt); + + return 0; +} + +int CmdList::run_all(CmdCtx *p, bool dry) { CmdList::iterator it; int ret; @@ -143,33 +156,29 @@ int CmdList::run_all(CmdCtx *p, bool dry_run) for (it = begin(); it != end(); it++, i++) { - if (dry_run) - { - (*it)->dump(); - } - else - { - uuu_notify nt; + uuu_notify nt; - nt.type = uuu_notify::NOTIFY_CMD_INDEX; - nt.index = i; - call_notify(nt); + nt.type = uuu_notify::NOTIFY_CMD_INDEX; + nt.index = i; + call_notify(nt); - nt.type = uuu_notify::NOTIFY_CMD_START; - nt.str = (char *)(*it)->m_cmd.c_str(); - call_notify(nt); + nt.type = uuu_notify::NOTIFY_CMD_START; + nt.str = (char *)(*it)->m_cmd.c_str(); + call_notify(nt); + if (dry) + ret = (*it)->dump(); + else ret = (*it)->run(p); - nt.type = uuu_notify::NOTIFY_CMD_END; - nt.status = ret; - call_notify(nt); - if (ret) - return ret; + nt.type = uuu_notify::NOTIFY_CMD_END; + nt.status = ret; + call_notify(nt); + if (ret) + return ret; - if ((*it)->m_lastcmd) + if ((*it)->m_lastcmd) break; - } } return ret; } @@ -340,7 +349,7 @@ shared_ptr create_cmd_obj(string cmd) return NULL; } -int uuu_run_cmd(const char * cmd) +int uuu_run_cmd(const char * cmd, int dry) { shared_ptr p; p = create_cmd_obj(cmd); @@ -362,7 +371,7 @@ int uuu_run_cmd(const char * cmd) { size_t pos = 0; string c = cmd; - + string pro = get_next_param(c, pos, ':'); pro = remove_square_brackets(pro); pro += ":"; @@ -371,17 +380,23 @@ int uuu_run_cmd(const char * cmd) ret = -1; else { - CmdUsbCtx ctx; - ret = ctx.look_for_match_device(pro.c_str()); - if (ret) - return ret; + if (dry) + { + ret = p->dump(); + }else + { + CmdUsbCtx ctx; + ret = ctx.look_for_match_device(pro.c_str()); + if (ret) + return ret; - ret = p->run(&ctx); + ret = p->run(&ctx); + } } } else { - return ret =p->run(NULL); + return ret = dry? p->dump() : p->run(NULL); } nt.type = uuu_notify::NOTIFY_CMD_END; @@ -481,7 +496,7 @@ int CmdShell::run(CmdCtx*) if (pos != string::npos) cmd = cmd.substr(0, pos); - return uuu_run_cmd(cmd.c_str()); + return uuu_run_cmd(cmd.c_str(), 0); } uuu_notify nt; nt.type = uuu_notify::NOTIFY_CMD_INFO; @@ -643,7 +658,7 @@ int check_version(string str) return 0; } -int uuu_run_cmd_script(const char * buff) +int uuu_run_cmd_script(const char * buff, int dry) { shared_ptr p(new FileBuffer); p->m_data.resize(strlen(buff)); @@ -746,10 +761,22 @@ int notify_done(uuu_notify nt, void *p) return 0; } -int uuu_wait_uuu_finish(int deamon) +int uuu_wait_uuu_finish(int deamon, int dry) { std::atomic exit; exit = 0; + + if(dry) { + for(auto it=g_cmd_map.begin(); it != g_cmd_map.end(); it++) + { + for(auto cmd = it->second->begin(); cmd != it->second->end(); cmd++) + { + (*cmd)->dump(); + } + } + return 0; + } + if (!deamon) uuu_register_notify_callback(notify_done, &exit); diff --git a/libuuu/cmd.h b/libuuu/cmd.h index 60b5cbb..4c29622 100644 --- a/libuuu/cmd.h +++ b/libuuu/cmd.h @@ -138,7 +138,7 @@ public: } virtual int parser(char *p = NULL); virtual int run(CmdCtx *p)=0; - virtual void dump() { /*dbg(m_cmd.c_str());*/ }; + virtual int dump(); }; typedef shared_ptr (*CreateCmdObj) (char *); diff --git a/libuuu/libuuu.h b/libuuu/libuuu.h index a8e5410..1a83aae 100644 --- a/libuuu/libuuu.h +++ b/libuuu/libuuu.h @@ -114,11 +114,11 @@ int uuu_for_each_cfg(uuu_show_cfg fn, void *p); typedef int(*uuu_ls_file)(const char *path, void *p); int uuu_for_each_ls_file(uuu_ls_file fn, const char *path, void *p); -int uuu_run_cmd(const char * cmd); -int uuu_run_cmd_script(const char *script); +int uuu_run_cmd(const char * cmd, int dry); +int uuu_run_cmd_script(const char *script, int dry); int uuu_auto_detect_file(const char * filename); -int uuu_wait_uuu_finish(int deamon); +int uuu_wait_uuu_finish(int deamon, int dry); int uuu_add_usbpath_filter(const char *path); /* diff --git a/uuu/uuu.cpp b/uuu/uuu.cpp index 9f60c07..a9957fe 100644 --- a/uuu/uuu.cpp +++ b/uuu/uuu.cpp @@ -129,6 +129,7 @@ void print_help(bool detail = false) " example: SDPS: boot -f flash.bin\n" " -d Daemon mode, wait for forever.\n" " -v -V verbose mode, -V enable libusb error\\warning info\n" + " -dry Dry run mode, check if script or cmd correct \n" " -m USBPATH Only monitor these paths.\n" " -m 1:2 -m 1:3\n\n" "uuu -s Enter shell mode. uuu.inputlog record all input commands\n" @@ -674,7 +675,7 @@ int runshell(int shell) if (uboot_cmd) cmd = "fb: ucmd " + cmd; - int ret = uuu_run_cmd(cmd.c_str()); + int ret = uuu_run_cmd(cmd.c_str(), 0); if (ret) cout << uuu_get_last_err_string() << endl; else @@ -732,6 +733,7 @@ int main(int argc, char **argv) string filename; string cmd; int ret; + int dryrun = 0; string cmd_script; @@ -756,6 +758,10 @@ int main(int argc, char **argv) { g_verbose = 1; uuu_set_debug_level(2); + }else if (s == "-dry") + { + dryrun = 1; + g_verbose = 1; } else if (s == "-h") { @@ -849,6 +855,18 @@ int main(int argc, char **argv) return -1; } + if (deamon && dryrun) + { + printf("Error: -d -dry Can't apply at the same time\n"); + return -1; + } + + if (shell && dryrun) + { + printf("Error: -dry -s Can't apply at the same time\n"); + return -1; + } + if (g_verbose) { printf("%sBuild in config:%s\n", g_vt_boldwhite, g_vt_default); @@ -881,7 +899,7 @@ int main(int argc, char **argv) if (!cmd.empty()) { - ret = uuu_run_cmd(cmd.c_str()); + ret = uuu_run_cmd(cmd.c_str(), dryrun); for (size_t i = 0; i < g_map_path_nt.size()+3; i++) printf("\n"); @@ -895,7 +913,7 @@ int main(int argc, char **argv) } if (!cmd_script.empty()) - ret = uuu_run_cmd_script(cmd_script.c_str()); + ret = uuu_run_cmd_script(cmd_script.c_str(), dryrun); else ret = uuu_auto_detect_file(filename.c_str()); @@ -907,7 +925,7 @@ int main(int argc, char **argv) return ret; } - uuu_wait_uuu_finish(deamon); + uuu_wait_uuu_finish(deamon, dryrun); runshell(shell); -- 2.11.4.GIT