]> cloudbase.mooo.com Git - z180-stamp.git/blame - avr/cli.c
Add CP/M 3 file i/o (initial version)
[z180-stamp.git] / avr / cli.c
CommitLineData
35edb766
L
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
d684c216
L
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
35static int cli_parse_line(char *line, char *argv[])
36{
b0e4f7e5 37 uint_fast8_t state = 0;
d684c216 38 uint_fast8_t nargs = 0;
b0e4f7e5
L
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;
d684c216 84
b0e4f7e5
L
85 case 2:
86 case 4:
87 --state;
88 break;
d684c216 89
b0e4f7e5
L
90 }
91 *outp++ = c;
d684c216
L
92 }
93
b0e4f7e5 94 if (*inp != '\0')
d684c216
L
95 printf_P(PSTR("** Too many args (max. %d) **\n"), CONFIG_SYS_MAXARGS);
96
b0e4f7e5 97 *outp = '\0';
d684c216 98 argv[nargs] = NULL;
b0e4f7e5
L
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
d684c216 104 return nargs;
b0e4f7e5 105
d684c216
L
106}
107
507d25e2
L
108static
109void append_char(uint_fast8_t pass, char **p, char c)
d684c216 110{
507d25e2
L
111
112 if (pass) {
113 **p = c;
114 }
115 ++(*p);
116}
117
118static
119char *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 {
b0e4f7e5
L
126 uint_fast8_t state = 0;
127 /* 0 = waiting for '$' */
507d25e2
L
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;
d684c216
L
138 }
139
507d25e2 140 inp = input;
507d25e2 141
b0e4f7e5
L
142 debug_parser("[PROCESS_MACROS] INPUT len %d: \"%s\"\n",
143 strlen(inp), inp);
144
145 for (prev = '\0'; (c = *inp++) != '\0'; prev = c) {
507d25e2 146
507d25e2 147
507d25e2
L
148
149 switch (state) {
150 case 0: /* Waiting for (unescaped) $ */
151 if ((c == '\'') && (prev != '\\')) {
152 state = 3;
153 break;
154 }
b0e4f7e5 155 if ((c == '$') && (prev != '\\')) {
507d25e2 156 state++;
b0e4f7e5
L
157 continue;
158 }
507d25e2
L
159 break;
160 case 1: /* Waiting for { */
161 if (c == '{') {
162 state++;
163 varname = inp;
b0e4f7e5 164 continue;
507d25e2
L
165 } else {
166 state = 0;
167 append_char(pass, &outp, '$');
507d25e2
L
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 }
b0e4f7e5 183 continue;
507d25e2 184 case 3: /* Waiting for ' */
b0e4f7e5 185 if (c == '\'')
507d25e2 186 state = 0;
507d25e2 187 break;
d684c216 188 }
b0e4f7e5 189 append_char(pass, &outp, c);
d684c216 190 }
d684c216 191
507d25e2
L
192 append_char(pass, &outp, 0);
193 }
d684c216
L
194
195 debug_parser("[PROCESS_MACROS] OUTPUT len %d: \"%s\"\n",
507d25e2
L
196 strlen(output), output);
197
198 return output;
d684c216
L
199}
200
f76ca346
L
201/**
202 *
203 *
d684c216
L
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).
f76ca346
L
210 *
211 *
212 * @param cmd
213 * @param flag
214 * @returns
215 *
d684c216
L
216 */
217static 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 */
507d25e2 222 char *finaltoken = NULL; /* token after macro expansion */
d684c216
L
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
f76ca346 229 debug_parser("[RUN_COMMAND] cmd[%p]=\"%s\"\n",
d684c216
L
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
69988dc1 237 cmdbuf = strdup(cmd);
507d25e2 238 if (!cmdbuf)
69988dc1 239 return -1; /* not enough memory */
d684c216 240
d684c216
L
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 */
507d25e2 278 finaltoken = process_macros(token, finaltoken);
d684c216
L
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
d0581f88 287 if (cmd_process(flag, argc, argv, &repeatable) != CMD_RET_SUCCESS)
d684c216
L
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
303static 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 */
318int 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 */
337static int run_command_repeatable(const char *cmd, int flag)
338{
339 return cli_run_command(cmd, flag);
340}
341
342int run_command_list(const char *cmd, int len)
343{
344 (void) len;
f76ca346 345
d684c216
L
346 return cli_run_command_list(cmd);
347}
348
349/****************************************************************************/
350
d684c216
L
351
352void cli_loop(void)
353{
d684c216 354 char *lastcommand = NULL;
d684c216
L
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) {
69988dc1
L
364 free (lastcommand);
365 lastcommand = strdup(console_buffer);
d684c216
L
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 */
d684c216
L
376 free(lastcommand);
377 lastcommand = NULL;
d684c216
L
378 }
379 }
380}
381
382
d0581f88 383command_ret_t do_run(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
d684c216
L
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]);
d0581f88 397 return CMD_RET_FAILURE;
d684c216
L
398 }
399
400 if (run_command(arg, flag) != 0)
d0581f88 401 return CMD_RET_FAILURE;
d684c216 402 }
d0581f88 403 return CMD_RET_SUCCESS;
d684c216 404}