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