]> cloudbase.mooo.com Git - z180-stamp.git/blame - avr/cli.c
commandtable, flags: int --> uint8_t/uint_fast8_t. Macro UNUSED for Parameters/Variables
[z180-stamp.git] / avr / cli.c
CommitLineData
35edb766 1/*
04b3ea0e 2 * (C) Copyright 2014-2016 Leo C. <erbl259-lmu@yahoo.de>
35edb766
L
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 *
8ed66016 11 * SPDX-License-Identifier: GPL-2.0
35edb766
L
12 */
13
8ed66016 14#include "cli.h"
d684c216
L
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"
d684c216
L
24#include "debug.h"
25#include "env.h"
26#include "cli_readline.h"
27#include "con-utils.h"
d684c216 28
bbd45c46
L
29
30/* FIXME: Quoting problems */
31
d684c216
L
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
13e88ed5 37
78035a8d
L
38static int_least8_t exec_flags;
39#define OPT_XTRACE 1
40#define OPT_VERBOSE 2
41
13e88ed5
L
42static int_least8_t command_level;
43
44static 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
d684c216
L
56static int cli_parse_line(char *line, char *argv[])
57{
b0e4f7e5 58 uint_fast8_t state = 0;
d684c216 59 uint_fast8_t nargs = 0;
b0e4f7e5
L
60 char *inp, *outp;
61 char c, quote;
62
63 debug_parser("%s: \"%s\"\n", __func__, line);
64
aca998c3 65 for (outp = inp = line, quote = '\0'; (c = *inp) != '\0'; inp++) {
b0e4f7e5
L
66
67 switch (state) {
bbd45c46 68 case 0: /* before arg string, waiting for arg start */
b0e4f7e5
L
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
bbd45c46 77 case 1: /* in arg string, waiting for end of arg string */
b0e4f7e5
L
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
bbd45c46 93 case 3: /* in quote */
b0e4f7e5
L
94 if (c == '\\' && quote == '\"') {
95 ++state;
96 continue;
97 }
98 if (c == quote) {
99 state = 1;
100 continue;
101 }
102 break;
d684c216 103
bbd45c46 104 case 2: /* waiting for next char */
b0e4f7e5
L
105 case 4:
106 --state;
107 break;
d684c216 108
b0e4f7e5 109 }
aca998c3
L
110
111 if (nargs > CONFIG_SYS_MAXARGS) {
112 --nargs;
113 break;
114 }
b0e4f7e5 115 *outp++ = c;
d684c216
L
116 }
117
b0e4f7e5 118 if (*inp != '\0')
d684c216
L
119 printf_P(PSTR("** Too many args (max. %d) **\n"), CONFIG_SYS_MAXARGS);
120
b0e4f7e5 121 *outp = '\0';
d684c216 122 argv[nargs] = NULL;
b0e4f7e5
L
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
d684c216 128 return nargs;
b0e4f7e5 129
d684c216
L
130}
131
507d25e2
L
132static
133void append_char(uint_fast8_t pass, char **p, char c)
d684c216 134{
507d25e2
L
135
136 if (pass) {
137 **p = c;
138 }
139 ++(*p);
140}
141
142static
78035a8d 143char *process_macros(char *input)
507d25e2 144{
78035a8d
L
145 char c, prev, *inp;
146 char *output = NULL;
147 char *outp = NULL;
507d25e2
L
148 const char *varname = NULL;
149
78035a8d
L
150 debug_parser("[PROCESS_MACROS] INPUT len %d: \"%s\"\n",
151 strlen(input), input);
152
507d25e2
L
153 for(uint_fast8_t pass = 0; pass < 2; pass++)
154 {
b0e4f7e5
L
155 uint_fast8_t state = 0;
156 /* 0 = waiting for '$' */
507d25e2
L
157 /* 1 = waiting for '{' */
158 /* 2 = waiting for '}' */
159 /* 3 = waiting for ''' */
160
78035a8d
L
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 }
507d25e2 168 outp = output;
d684c216
L
169 }
170
507d25e2 171 inp = input;
507d25e2 172
b0e4f7e5 173 for (prev = '\0'; (c = *inp++) != '\0'; prev = c) {
507d25e2 174
507d25e2 175
507d25e2
L
176
177 switch (state) {
178 case 0: /* Waiting for (unescaped) $ */
179 if ((c == '\'') && (prev != '\\')) {
180 state = 3;
181 break;
182 }
b0e4f7e5 183 if ((c == '$') && (prev != '\\')) {
507d25e2 184 state++;
b0e4f7e5
L
185 continue;
186 }
507d25e2
L
187 break;
188 case 1: /* Waiting for { */
189 if (c == '{') {
190 state++;
191 varname = inp;
b0e4f7e5 192 continue;
507d25e2
L
193 } else {
194 state = 0;
195 append_char(pass, &outp, '$');
507d25e2
L
196 }
197 break;
198 case 2: /* Waiting for } */
199 if (c == '}') {
200 /* Terminate variable name */
201 *(inp-1) = '\0';
bddc7e77 202 const char *envval = getenv_str(varname);
507d25e2
L
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 }
b0e4f7e5 211 continue;
507d25e2 212 case 3: /* Waiting for ' */
b0e4f7e5 213 if (c == '\'')
507d25e2 214 state = 0;
507d25e2 215 break;
d684c216 216 }
b0e4f7e5 217 append_char(pass, &outp, c);
d684c216 218 }
d684c216 219
507d25e2
L
220 append_char(pass, &outp, 0);
221 }
d684c216
L
222
223 debug_parser("[PROCESS_MACROS] OUTPUT len %d: \"%s\"\n",
507d25e2
L
224 strlen(output), output);
225
226 return output;
d684c216
L
227}
228
f76ca346
L
229/**
230 *
231 *
d684c216
L
232 * WARNING:
233 *
234 * We must create a temporary copy of the command since the command we get
bddc7e77 235 * may be the result from getenv_str(), which returns a pointer directly to
d684c216
L
236 * the environment data, which may change magicly when the command we run
237 * creates or modifies environment variables (like "bootp" does).
f76ca346
L
238 *
239 *
240 * @param cmd
241 * @param flag
242 * @returns
243 *
d684c216 244 */
fcf1d5b3 245static int cli_run_command(const char *cmd, uint_fast8_t flag)
d684c216 246{
78035a8d 247 char cmdbuf[strlen(cmd) + 1]; /* working copy of cmd */
d684c216
L
248 char *token; /* start of token in cmdbuf */
249 char *sep; /* end of token (separator) in cmdbuf */
507d25e2 250 char *finaltoken = NULL; /* token after macro expansion */
d684c216
L
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
78035a8d 257 exec_flags = 0;
13e88ed5
L
258 char *optenv = getenv_str(PSTR("cli"));
259 if (optenv) {
78035a8d
L
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;
13e88ed5
L
264 }
265
f76ca346 266 debug_parser("[RUN_COMMAND] cmd[%p]=\"%s\"\n",
d684c216
L
267 cmd, cmd ? cmd : "NULL");
268
78035a8d 269 if (exec_flags & OPT_VERBOSE)
13e88ed5
L
270 printf_P(PSTR("%s\n"), cmd, cmd ? cmd : "");
271
d684c216
L
272 if (!cmd || !*cmd)
273 return -1; /* empty command */
274
78035a8d 275 clear_ctrlc(); /* forget any previous Control C */
d684c216 276
78035a8d
L
277 str = strcpy(cmdbuf, cmd);
278 //str = cmdbuf;
13e88ed5 279 ++command_level;
d684c216
L
280
281 /* Process separators and check for invalid
282 * repeatable commands
283 */
284
78035a8d 285 debug_parser("[PROCESS_SEPARATORS] \"%s\"\n", cmd);
d684c216
L
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 == '\'') &&
01c1907d
L
293 (sep != str) && /* past string start */
294 (*(sep - 1) != '\\')) /* and NOT escaped */
d684c216
L
295 inquotes = !inquotes;
296
297 if (!inquotes &&
01c1907d
L
298 (*sep == ';' || *sep == '\n' /* separator */
299 || *sep == '#') && /* or start of comment */
300 ((sep == str) || /* string start */
301 (*(sep - 1) != '\\'))) /* or NOT escaped */
d684c216
L
302 break;
303 }
304
01c1907d
L
305 /* no more commands after unescaped '#' token */
306 if (*sep == '#')
307 *sep = '\0';
308
309 /* Limit the token to data between separators */
d684c216
L
310 token = str;
311 if (*sep) {
01c1907d 312 str = sep + 1; /* start of command for next pass */
d684c216
L
313 *sep = '\0';
314 } else {
01c1907d 315 str = sep; /* no more commands for next pass */
d684c216
L
316 }
317 debug_parser("token: \"%s\"\n", token);
318
319 /* find macros in this token and replace them */
78035a8d
L
320 finaltoken = process_macros(token);
321 if (finaltoken == NULL) {
322 rc = -1; /* no command at all */
323 continue;
324 }
d684c216
L
325
326 /* Extract arguments */
327 argc = cli_parse_line(finaltoken, argv);
328 if (argc == 0) {
78035a8d
L
329 rc = -1; /* no command at all */
330 free(finaltoken);
331 continue;
d684c216
L
332 }
333
78035a8d 334 if (exec_flags & OPT_XTRACE)
13e88ed5
L
335 cli_trace_cmd(command_level, argc, argv);
336
337 rc = cmd_process(flag, argc, argv, &repeatable);
78035a8d 338 free(finaltoken);
13e88ed5 339 if (rc != CMD_RET_SUCCESS) {
78035a8d 340 if (exec_flags & OPT_VERBOSE)
13e88ed5 341 printf_P(PSTR("Command failed, result=%d\n"), rc);
d684c216 342 rc = -1;
13e88ed5 343 }
d684c216
L
344
345 /* Did the user stop this? */
346 if (had_ctrlc()) {
347 rc = -1; /* if stopped then not repeatable */
348 break;
349 }
350 }
351
13e88ed5 352 --command_level;
d684c216
L
353
354 return rc ? rc : repeatable;
355}
356
357static int cli_run_command_list(const char *cmd)
358{
359 return (cli_run_command(cmd, 0) < 0);
360}
361
362/******************************************************************************/
363
364
365/*
b08e079d 366 * Run a command.
d684c216
L
367 *
368 * @param cmd Command to run
369 * @param flag Execution flags (CMD_FLAG_...)
370 * @return 0 on success, or != 0 on error.
371 */
fcf1d5b3 372int run_command(const char *cmd, uint_fast8_t flag)
d684c216
L
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 */
fcf1d5b3 391static int run_command_repeatable(const char *cmd, uint_fast8_t flag)
d684c216
L
392{
393 return cli_run_command(cmd, flag);
394}
395
396int run_command_list(const char *cmd, int len)
397{
398 (void) len;
f76ca346 399
d684c216
L
400 return cli_run_command_list(cmd);
401}
402
403/****************************************************************************/
404
d684c216
L
405
406void cli_loop(void)
407{
d684c216 408 char *lastcommand = NULL;
d684c216 409 int len;
fcf1d5b3 410 uint_fast8_t flag;
d684c216
L
411 int rc = 1;
412
413 for (;;) {
66e23a9d 414 len = cli_readline(lastcommand ? PSTR(CONFIG_SYS_PROMPT_REPEAT) : PSTR(CONFIG_SYS_PROMPT), 1);
d684c216
L
415
416 flag = 0; /* assume no special flags for now */
417 if (len > 0) {
69988dc1
L
418 free (lastcommand);
419 lastcommand = strdup(console_buffer);
d684c216
L
420 } else if (len == 0)
421 flag |= CMD_FLAG_REPEAT;
422
13e88ed5 423 if (len == -1) {
78035a8d 424 if (exec_flags & OPT_VERBOSE)
13e88ed5
L
425 my_puts_P(PSTR("<INTERRUPT>\n"));
426 } else
d684c216
L
427 rc = run_command_repeatable(lastcommand, flag);
428
429 if (rc <= 0) {
430 /* invalid command or not repeatable, forget it */
d684c216
L
431 free(lastcommand);
432 lastcommand = NULL;
d684c216
L
433 }
434 }
435}