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