Add dry run option to check if script is correct
[mfgtools.git] / libuuu / cmd.h
blob4c296228201db832f4df8e231dd7db7b8d73e53d
1 /*
2 * Copyright 2018 NXP.
4 * Redistribution and use in source and binary forms, with or without modification,
5 * are permitted provided that the following conditions are met:
7 * Redistributions of source code must retain the above copyright notice, this
8 * list of conditions and the following disclaimer.
10 * Redistributions in binary form must reproduce the above copyright notice, this
11 * list of conditions and the following disclaimer in the documentation and/or
12 * other materials provided with the distribution.
14 * Neither the name of the NXP Semiconductor nor the names of its
15 * contributors may be used to endorse or promote products derived from this
16 * software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
32 #pragma once
34 #include <string>
35 #include <vector>
36 #include <map>
37 #include <memory>
39 #include "liberror.h"
40 #include "libcomm.h"
41 #include "config.h"
43 using namespace std;
45 string get_next_param(string &cmd, size_t &pos, char sperate = ' ');
46 string remove_square_brackets(string &cmd);
47 int get_string_in_square_brackets(string &cmd, string &context);
48 uint32_t str_to_uint(string &str);
50 class CmdCtx
52 public:
53 CmdCtx() { m_config_item = NULL; m_dev = NULL; };
54 virtual ~CmdCtx() {};
55 ConfigItem *m_config_item;
56 void *m_dev;
59 class CmdUsbCtx : public CmdCtx
61 public:
62 CmdUsbCtx() :CmdCtx() {};
63 ~CmdUsbCtx();
64 int look_for_match_device(const char * procotol);
67 struct Param
69 enum Param_Type
71 e_uint32,
72 e_bool,
73 e_string,
74 e_null,
75 e_string_filename,
78 const char * key;
79 void *pData;
80 int type;
81 bool ignore_case;
82 Param(const char *ky, void *pD, Param_Type tp, bool ignore=true)
84 key = ky; pData = pD; type = tp; ignore_case = ignore;
88 class CmdBase
90 public:
91 vector<Param> m_param;
92 uint64_t m_timeout;
93 bool m_lastcmd;
94 std::string m_cmd;
96 void CmdBaseInit() { m_timeout = 2000; m_lastcmd = false;}
97 CmdBase() { CmdBaseInit(); };
98 CmdBase(char *p) { CmdBaseInit(); if (p) m_cmd = p; }
100 void insert_param_info(const char *key, void *pD, Param::Param_Type tp, bool ignore_case = true)
102 m_param.push_back(Param(key, pD, tp, ignore_case));
105 virtual int parser_protocal(char *p, size_t &pos)
107 if (p)
108 m_cmd = *p;
110 string prot = get_next_param(m_cmd, pos, ':');
111 string param;
112 if (get_string_in_square_brackets(prot, param))
113 return -1;
115 if (!param.empty())
117 size_t param_pos = 0;
118 string s = get_next_param(param, param_pos);
120 if (s == "-t")
122 string timeout;
123 timeout = get_next_param(param, param_pos);
124 m_timeout = str_to_uint(timeout);
126 else
128 string err;
129 err = "Unknown option: ";
130 err += s;
131 err += " for protocol: ";
132 err += remove_square_brackets(prot);
133 set_last_err_string(err);
134 return -1;
137 return 0;
139 virtual int parser(char *p = NULL);
140 virtual int run(CmdCtx *p)=0;
141 virtual int dump();
144 typedef shared_ptr<CmdBase> (*CreateCmdObj) (char *);
146 class CmdObjCreateMap:public map<string, CreateCmdObj>
148 public:
149 CmdObjCreateMap();
152 class CmdDone :public CmdBase
154 public:
155 CmdDone(char *p) :CmdBase(p) { m_lastcmd = true; };
156 int run(CmdCtx *p);
159 class CmdDelay :public CmdBase
161 public:
162 int m_ms;
163 virtual int parser(char *p = NULL);
164 CmdDelay(char *p) :CmdBase(p) { m_ms = 0; };
165 int run(CmdCtx *p);
168 class CmdShell : public CmdBase
170 public:
171 string m_shellcmd;
172 string m_protocal;
173 bool m_dyn;
175 CmdShell(char *p) : CmdBase(p) { m_dyn = false; };
176 virtual int parser(char *p = NULL);
177 int run(CmdCtx *p);
180 class CmdList : public std::vector<shared_ptr<CmdBase>>
182 public:
183 int run_all(CmdCtx *p, bool dry_run = false);
186 class CmdMap : public std::map<std::string, shared_ptr<CmdList>>
188 public:
189 int run_all(std::string protocal, CmdCtx *p, bool dry_run = false)
191 if (find(protocal) == end())
193 set_last_err_id(-1);
194 std::string err;
195 err.append("Unknown Protocal:");
196 err.append(protocal);
197 set_last_err_string(err);
198 return -1;
200 return at(protocal)->run_all(p, dry_run);
204 class CfgCmd :public CmdBase
206 public:
207 int parser(char * /*p*/) { return 0; };
208 CfgCmd(char *cmd) :CmdBase(cmd) {};
209 int run(CmdCtx *p);
212 int run_cmds(const char *procotal, CmdCtx *p);