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