]> cloudbase.mooo.com Git - z180-stamp.git/blob - avr/cli.c
clean up includes
[z180-stamp.git] / avr / cli.c
1 /*
2 * (C) Copyright 2014-2016 Leo C. <erbl259-lmu@yahoo.de>
3 *
4 * (C) Copyright 2000
5 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
6 *
7 * Add to readline cmdline-editing by
8 * (C) Copyright 2005
9 * JinHua Luo, GuangDong Linux Center, <luo.jinhua@gd-linux.com>
10 *
11 * SPDX-License-Identifier: GPL-2.0
12 */
13
14 #include "cli.h"
15 #include "command.h"
16
17 #include "config.h"
18 #include "debug.h"
19 #include "env.h"
20 #include "cli_readline.h"
21 #include "con-utils.h"
22
23
24 /* FIXME: Quoting problems */
25
26 #define DEBUG_PARSER 0 /* set to 1 to debug */
27
28 #define debug_parser(fmt, args...) \
29 debug_cond(DEBUG_PARSER, fmt, ##args)
30
31
32 static int_least8_t exec_flags;
33 #define OPT_XTRACE 1
34 #define OPT_VERBOSE 2
35
36 static int_least8_t command_level;
37
38 static void cli_trace_cmd(int_fast8_t level, int argc, char *argv[])
39 {
40 while (level-- > 0)
41 putchar('+');
42 for (int_fast8_t i = 0; i < argc; i++)
43 printf_P(PSTR(" %s"), argv[i]);
44 putchar('\n');
45 }
46
47
48
49
50 static int cli_parse_line(char *line, char *argv[])
51 {
52 uint_fast8_t state = 0;
53 uint_fast8_t nargs = 0;
54 char *inp, *outp;
55 char c, quote;
56
57 debug_parser("%s: \"%s\"\n", __func__, line);
58
59 for (outp = inp = line, quote = '\0'; (c = *inp) != '\0'; inp++) {
60
61 switch (state) {
62 case 0: /* before arg string, waiting for arg start */
63 if (isblank(c))
64 continue;
65
66 argv[nargs++] = inp; /* begin of argument string */
67 outp = inp;
68 state = 1;
69 /* fall thru */
70
71 case 1: /* in arg string, waiting for end of arg string */
72 if (c == '\\') {
73 ++state;
74 continue;
75 }
76 if (c == '\"' || c == '\'') {
77 quote = c;
78 state = 3;
79 continue;
80 }
81 if (isblank(c)) {
82 c = '\0';
83 state = 0;
84 }
85 break;
86
87 case 3: /* in quote */
88 if (c == '\\' && quote == '\"') {
89 ++state;
90 continue;
91 }
92 if (c == quote) {
93 state = 1;
94 continue;
95 }
96 break;
97
98 case 2: /* waiting for next char */
99 case 4:
100 --state;
101 break;
102
103 }
104
105 if (nargs > CONFIG_SYS_MAXARGS) {
106 --nargs;
107 break;
108 }
109 *outp++ = c;
110 }
111
112 if (*inp != '\0')
113 printf_P(PSTR("** Too many args (max. %d) **\n"), CONFIG_SYS_MAXARGS);
114
115 *outp = '\0';
116 argv[nargs] = NULL;
117 debug_parser("%s: nargs=%d\n", __func__, nargs);
118 #if 0
119 for (int i = 0; i < nargs; i++)
120 debug_parser("%s: arg %d: >%s<\n", __func__, i, argv[i]);
121 #endif
122 return nargs;
123
124 }
125
126 static
127 void append_char(uint_fast8_t pass, char **p, char c)
128 {
129
130 if (pass) {
131 **p = c;
132 }
133 ++(*p);
134 }
135
136 static
137 char *process_macros(char *input)
138 {
139 char c, prev, *inp;
140 char *output = NULL;
141 char *outp = NULL;
142 const char *varname = NULL;
143
144 debug_parser("[PROCESS_MACROS] INPUT len %d: \"%s\"\n",
145 strlen(input), input);
146
147 for(uint_fast8_t pass = 0; pass < 2; pass++)
148 {
149 uint_fast8_t state = 0;
150 /* 0 = waiting for '$' */
151 /* 1 = waiting for '{' */
152 /* 2 = waiting for '}' */
153 /* 3 = waiting for ''' */
154
155 if (pass > 0) {
156 size_t outputlen = outp - (char *) NULL;
157 output = (char *) malloc(outputlen);
158 if (output == NULL) {
159 printf_P(PSTR("** Can't process command: Out of memory! **\n"));
160 return NULL;
161 }
162 outp = output;
163 }
164
165 inp = input;
166
167 for (prev = '\0'; (c = *inp++) != '\0'; prev = c) {
168
169
170
171 switch (state) {
172 case 0: /* Waiting for (unescaped) $ */
173 if ((c == '\'') && (prev != '\\')) {
174 state = 3;
175 break;
176 }
177 if ((c == '$') && (prev != '\\')) {
178 state++;
179 continue;
180 }
181 break;
182 case 1: /* Waiting for { */
183 if (c == '{') {
184 state++;
185 varname = inp;
186 continue;
187 } else {
188 state = 0;
189 append_char(pass, &outp, '$');
190 }
191 break;
192 case 2: /* Waiting for } */
193 if (c == '}') {
194 /* Terminate variable name */
195 *(inp-1) = '\0';
196 const char *envval = getenv_str(varname);
197 *(inp-1) = '}';
198 /* Copy into the line if it exists */
199 if (envval != NULL)
200 while (*envval)
201 append_char(pass, &outp, *(envval++));
202 /* Look for another '$' */
203 state = 0;
204 }
205 continue;
206 case 3: /* Waiting for ' */
207 if (c == '\'')
208 state = 0;
209 break;
210 }
211 append_char(pass, &outp, c);
212 }
213
214 append_char(pass, &outp, 0);
215 }
216
217 debug_parser("[PROCESS_MACROS] OUTPUT len %d: \"%s\"\n",
218 strlen(output), output);
219
220 return output;
221 }
222
223 /**
224 *
225 *
226 * WARNING:
227 *
228 * We must create a temporary copy of the command since the command we get
229 * may be the result from getenv_str(), which returns a pointer directly to
230 * the environment data, which may change magicly when the command we run
231 * creates or modifies environment variables (like "bootp" does).
232 *
233 *
234 * @param cmd
235 * @param flag
236 * @returns
237 *
238 */
239 static int cli_run_command(const char *cmd, uint_fast8_t flag)
240 {
241 char cmdbuf[strlen(cmd) + 1]; /* working copy of cmd */
242 char *token; /* start of token in cmdbuf */
243 char *sep; /* end of token (separator) in cmdbuf */
244 char *finaltoken = NULL; /* token after macro expansion */
245 char *str;
246 char *argv[CONFIG_SYS_MAXARGS + 1]; /* NULL terminated */
247 int argc;
248 uint_fast8_t inquotes, repeatable = 1;
249 int rc = 0;
250
251 exec_flags = 0;
252 char *optenv = getenv_str(PSTR("cli"));
253 if (optenv) {
254 if (strstr_P(optenv, PSTR("verbose")) != NULL)
255 exec_flags |= OPT_VERBOSE;
256 if (strstr_P(optenv, PSTR("xtrace")) != NULL)
257 exec_flags |= OPT_XTRACE;
258 }
259
260 debug_parser("[RUN_COMMAND] cmd[%p]=\"%s\"\n",
261 cmd, cmd ? cmd : "NULL");
262
263 if (exec_flags & OPT_VERBOSE)
264 printf_P(PSTR("%s\n"), cmd, cmd ? cmd : "");
265
266 if (!cmd || !*cmd)
267 return -1; /* empty command */
268
269 clear_ctrlc(); /* forget any previous Control C */
270
271 str = strcpy(cmdbuf, cmd);
272 //str = cmdbuf;
273 ++command_level;
274
275 /* Process separators and check for invalid
276 * repeatable commands
277 */
278
279 debug_parser("[PROCESS_SEPARATORS] \"%s\"\n", cmd);
280 while (*str) {
281 /*
282 * Find separator, or string end
283 * Allow simple escape of ';' by writing "\;"
284 */
285 for (inquotes = 0, sep = str; *sep; sep++) {
286 if ((*sep == '\'') &&
287 (sep != str) && /* past string start */
288 (*(sep - 1) != '\\')) /* and NOT escaped */
289 inquotes = !inquotes;
290
291 if (!inquotes &&
292 (*sep == ';' || *sep == '\n' /* separator */
293 || *sep == '#') && /* or start of comment */
294 ((sep == str) || /* string start */
295 (*(sep - 1) != '\\'))) /* or NOT escaped */
296 break;
297 }
298
299 /* no more commands after unescaped '#' token */
300 if (*sep == '#')
301 *sep = '\0';
302
303 /* Limit the token to data between separators */
304 token = str;
305 if (*sep) {
306 str = sep + 1; /* start of command for next pass */
307 *sep = '\0';
308 } else {
309 str = sep; /* no more commands for next pass */
310 }
311 while (isblank(*token))
312 ++token;
313 if (*token == '\0') /* empty (comment only) */
314 continue;
315 debug_parser("token: \"%s\"\n", token);
316
317 /* find macros in this token and replace them */
318 finaltoken = process_macros(token);
319 if (finaltoken == NULL) {
320 rc = -1; /* no command at all */
321 continue;
322 }
323
324 /* Extract arguments */
325 argc = cli_parse_line(finaltoken, argv);
326 if (argc == 0) {
327 rc = -1; /* no command at all */
328 free(finaltoken);
329 continue;
330 }
331
332 if (exec_flags & OPT_XTRACE)
333 cli_trace_cmd(command_level, argc, argv);
334
335 rc = cmd_process(flag, argc, argv, &repeatable);
336 free(finaltoken);
337 if (rc != CMD_RET_SUCCESS) {
338 if (exec_flags & OPT_VERBOSE)
339 printf_P(PSTR("Command failed, result=%d\n"), rc);
340 rc = -1;
341 }
342
343 /* Did the user stop this? */
344 if (had_ctrlc()) {
345 rc = -1; /* if stopped then not repeatable */
346 break;
347 }
348 }
349
350 --command_level;
351
352 return rc ? rc : repeatable;
353 }
354
355 static int cli_run_command_list(const char *cmd)
356 {
357 return (cli_run_command(cmd, 0) < 0);
358 }
359
360 /******************************************************************************/
361
362
363 /*
364 * Run a command.
365 *
366 * @param cmd Command to run
367 * @param flag Execution flags (CMD_FLAG_...)
368 * @return 0 on success, or != 0 on error.
369 */
370 int run_command(const char *cmd, uint_fast8_t flag)
371 {
372 /*
373 * cli_run_command can return 0 or 1 for success, so clean up
374 * its result.
375 */
376 if (cli_run_command(cmd, flag) == -1)
377 return 1;
378
379 return 0;
380 }
381
382 /*
383 * Run a command using the selected parser, and check if it is repeatable.
384 *
385 * @param cmd Command to run
386 * @param flag Execution flags (CMD_FLAG_...)
387 * @return 0 (not repeatable) or 1 (repeatable) on success, -1 on error.
388 */
389 static int run_command_repeatable(const char *cmd, uint_fast8_t flag)
390 {
391 return cli_run_command(cmd, flag);
392 }
393
394 int run_command_list(const char *cmd, int len)
395 {
396 (void) len;
397
398 return cli_run_command_list(cmd);
399 }
400
401 /****************************************************************************/
402
403
404 void cli_loop(void)
405 {
406 char *lastcommand = NULL;
407 int len;
408 uint_fast8_t flag;
409 int rc = 1;
410
411 for (;;) {
412 len = cli_readline(lastcommand ? PSTR(CONFIG_SYS_PROMPT_REPEAT) : PSTR(CONFIG_SYS_PROMPT), 1);
413
414 flag = 0; /* assume no special flags for now */
415 if (len > 0) {
416 free (lastcommand);
417 lastcommand = strdup(console_buffer);
418 } else if (len == 0)
419 flag |= CMD_FLAG_REPEAT;
420
421 if (len == -1) {
422 if (exec_flags & OPT_VERBOSE)
423 my_puts_P(PSTR("<INTERRUPT>\n"));
424 } else
425 rc = run_command_repeatable(lastcommand, flag);
426
427 if (rc <= 0) {
428 /* invalid command or not repeatable, forget it */
429 free(lastcommand);
430 lastcommand = NULL;
431 }
432 }
433 }