-
Notifications
You must be signed in to change notification settings - Fork 333
Expand file tree
/
Copy pathbuildincmd.cpp
More file actions
295 lines (270 loc) · 6.73 KB
/
Copy pathbuildincmd.cpp
File metadata and controls
295 lines (270 loc) · 6.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/*
* Copyright 2018 NXP.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice, this
* list of conditions and the following disclaimer in the documentation and/or
* other materials provided with the distribution.
*
* Neither the name of the NXP Semiconductor nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*/
#include "buildincmd.h"
#include <locale>
using namespace std;
constexpr BuildCmd::BuildCmd(const char * const cmd, const char * const buildcmd,
const char * const desc) :
m_cmd{cmd},
m_buildcmd{buildcmd},
m_desc{desc}
{
}
static constexpr std::array<const BuildCmd, 8> g_buildin_cmd
{
BuildCmd{
"emmc",
#include "emmc_burn_loader.clst"
,"burn boot loader to eMMC boot partition"
},
BuildCmd{
"emmc_all",
#include "emmc_burn_all.clst"
,"burn whole image to eMMC"
},
BuildCmd{
"fat_write",
#include "fat_write.clst"
,"update one file in fat partition, require uboot fastboot running in board"
},
BuildCmd{
"nand",
#include "nand_burn_loader.clst"
,"burn boot loader to NAND flash"
},
BuildCmd{
"qspi",
#include "qspi_burn_loader.clst"
,"burn boot loader to qspi nor flash"
},
BuildCmd{
"sd",
#include "sd_burn_loader.clst"
,"burn boot loader to sd card"
},
BuildCmd{
"sd_all",
#include "sd_burn_all.clst"
,"burn whole image to sd card"
},
BuildCmd{
"spl",
#include "spl_boot.clst"
,"boot spl and uboot"
}
};
BuildInScriptVector g_BuildScripts(g_buildin_cmd);
int Arg::parser(const string &option)
{
size_t pos;
pos = option.find('[');
if (pos == std::string::npos)
return 0;
m_options = option.substr(pos + 1, option.find(']') - pos - 1);
m_flags = ARG_OPTION | ARG_OPTION_KEY;
return 0;
}
BuildInScript::BuildInScript(const BuildCmd &p) :
m_script{p.m_buildcmd}
{
if (p.m_desc)
{
m_desc = p.m_desc;
}
if (p.m_cmd)
{
m_cmd = p.m_cmd;
}
for (size_t i = 1; i < m_script.size(); i++)
{
string param;
if (m_script[i] == '_'
&& (m_script[i - 1] == '@' || m_script[i - 1] == ' '))
{
size_t off = m_script.find(' ', i);
size_t off_tab = m_script.find('\t', i);
size_t ofn = m_script.find('\n', i);
if (off_tab < off)
off = off_tab;
if (ofn < off)
off = ofn;
if (off == std::string::npos)
off = m_script.size() + 1;
param = m_script.substr(i, off - i);
if (!find_args(param))
{
Arg a{param};
a.m_flags = Arg::ARG_MUST;
m_args.push_back(a);
}
}
}
for (size_t i = 0; i < m_args.size(); i++)
{
size_t pos = 0;
std::string str;
str += "@";
str += m_args[i].get_arg();
pos = m_script.find(str);
if (pos != std::string::npos) {
std::string def;
size_t start_descript;
start_descript = m_script.find('|', pos);
if (start_descript != std::string::npos)
{
m_args[i].m_desc = m_script.substr(start_descript + 1,
m_script.find('\n', start_descript) - start_descript - 1);
def = m_script.substr(pos, start_descript - pos);
m_args[i].parser(def);
}
}
}
}
bool BuildInScript::find_args(const string &arg) const
{
for (const auto &arg_ref : m_args)
{
if (arg_ref.get_arg() == arg)
{
return true;
}
}
return false;
}
string BuildInScript::replace_script_args(vector<string> args)
{
string script{m_script};
for (size_t i = 0; i < args.size() && i < m_args.size(); i++)
{
script = replace_str(script, m_args[i].get_arg(), args[i]);
}
//handle option args;
for (size_t i = args.size(); i < m_args.size(); i++)
{
if (m_args[i].m_flags & Arg::ARG_OPTION_KEY)
{
for (size_t j = 0; j < args.size(); j++)
{
if (m_args[j].get_arg() == m_args[i].get_options())
{
script = replace_str(script, m_args[i].get_arg(), args[j]);
break;
}
}
}
}
return script;
}
string BuildInScript::replace_str(string str, const string &key, string replace)
{
if (replace.size() > 4)
{
if (str_to_upper(replace.substr(replace.size() - 4)) == ".BZ2")
{
replace += "/*";
}
}
for (size_t j = 0; (j = str.find(key, j)) != std::string::npos;)
{
str.replace(j, key.size(), replace);
j += key.size();
}
return str;
}
void BuildInScript::show_cmd() const
{
printf("\t%s%s%s\t%s\n", g_vt_boldwhite, m_cmd.c_str(), g_vt_default, m_desc.c_str());
for (size_t i = 0; i < m_args.size(); i++)
{
string desc{m_args[i].get_arg()};
if (m_args[i].m_flags & Arg::ARG_OPTION)
{
desc += g_vt_boldwhite;
desc += "[Optional]";
desc += g_vt_default;
}
desc += " ";
desc += m_args[i].m_desc;
printf("\t\targ%d: %s\n", static_cast<int>(i), desc.c_str());
}
}
string BuildInScript::str_to_upper(const string &str)
{
const std::locale loc;
std::string s;
s.reserve(str.size());
for (size_t i = 0; i < str.size(); i++)
{
s.push_back(std::toupper(str[i], loc));
}
return s;
}
BuildInScriptVector::BuildInScriptVector(const array<const BuildCmd, 8> &build_cmds)
{
for (const auto &build_cmd : build_cmds) {
BuildInScript one{build_cmd};
(*this)[one.get_cmd()] = one;
}
}
void BuildInScriptVector::PrintAutoComplete(const string &match, const char *space)
{
for (const auto &str_n_script : *this)
{
if(str_n_script.first.substr(0, match.size()) == match)
{
printf("%s%s\n", str_n_script.first.c_str(), space);
}
}
}
void BuildInScriptVector::ShowAll() const
{
for (const auto &str_n_script : *this)
{
str_n_script.second.show_cmd();
}
}
void BuildInScriptVector::ShowCmds() const
{
printf("<");
for (auto iCol = cbegin(); iCol != cend(); ++iCol)
{
// Print the current command's name
printf("%s%s%s", g_vt_boldwhite, iCol->first.c_str(), g_vt_default);
// Check if it's the last command and if not print a separating bar
auto i = iCol;
++i;
if(i != cend())
{
printf("|");
}
}
printf(">");
}