]>
cloudbase.mooo.com Git - z180-stamp.git/blob - avr/cli.c
13 #include "cli_readline.h"
14 #include "con-utils.h"
17 #define DEBUG_PARSER 0 /* set to 1 to debug */
19 #define debug_parser(fmt, args...) \
20 debug_cond(DEBUG_PARSER, fmt, ##args)
22 static int cli_parse_line(char *line
, char *argv
[])
24 uint_fast8_t state
= 0;
25 uint_fast8_t nargs
= 0;
29 debug_parser("%s: \"%s\"\n", __func__
, line
);
31 for (outp
= inp
= line
, quote
= '\0' ;
32 nargs
< CONFIG_SYS_MAXARGS
&& (c
= *inp
) != '\0';
40 argv
[nargs
++] = inp
; /* begin of argument string */
50 if (c
== '\"' || c
== '\'') {
62 if (c
== '\\' && quote
== '\"') {
82 printf_P(PSTR("** Too many args (max. %d) **\n"), CONFIG_SYS_MAXARGS
);
86 debug_parser("%s: nargs=%d\n", __func__
, nargs
);
88 for (int i
= 0; i
< nargs
; i
++)
89 debug_parser("%s: arg %d: >%s<\n", __func__
, i
, argv
[i
]);
96 void append_char(uint_fast8_t pass
, char **p
, char c
)
106 char *process_macros(char *input
, char *output
)
108 char c
, prev
, *inp
, *outp
;
109 const char *varname
= NULL
;
111 for(uint_fast8_t pass
= 0; pass
< 2; pass
++)
113 uint_fast8_t state
= 0;
114 /* 0 = waiting for '$' */
115 /* 1 = waiting for '{' */
116 /* 2 = waiting for '}' */
117 /* 3 = waiting for ''' */
122 int outputlen
= outp
- output
;
123 outp
= xrealloc(output
, outputlen
);
129 debug_parser("[PROCESS_MACROS] INPUT len %d: \"%s\"\n",
132 for (prev
= '\0'; (c
= *inp
++) != '\0'; prev
= c
) {
137 case 0: /* Waiting for (unescaped) $ */
138 if ((c
== '\'') && (prev
!= '\\')) {
142 if ((c
== '$') && (prev
!= '\\')) {
147 case 1: /* Waiting for { */
154 append_char(pass
, &outp
, '$');
157 case 2: /* Waiting for } */
159 /* Terminate variable name */
161 const char *envval
= getenv(varname
);
163 /* Copy into the line if it exists */
166 append_char(pass
, &outp
, *(envval
++));
167 /* Look for another '$' */
171 case 3: /* Waiting for ' */
176 append_char(pass
, &outp
, c
);
179 append_char(pass
, &outp
, 0);
182 debug_parser("[PROCESS_MACROS] OUTPUT len %d: \"%s\"\n",
183 strlen(output
), output
);
193 * We must create a temporary copy of the command since the command we get
194 * may be the result from getenv(), which returns a pointer directly to
195 * the environment data, which may change magicly when the command we run
196 * creates or modifies environment variables (like "bootp" does).
204 static int cli_run_command(const char *cmd
, int flag
)
206 char *cmdbuf
; /* working copy of cmd */
207 char *token
; /* start of token in cmdbuf */
208 char *sep
; /* end of token (separator) in cmdbuf */
209 char *finaltoken
= NULL
; /* token after macro expansion */
211 char *argv
[CONFIG_SYS_MAXARGS
+ 1]; /* NULL terminated */
213 uint_fast8_t inquotes
, repeatable
= 1;
216 debug_parser("[RUN_COMMAND] cmd[%p]=\"%s\"\n",
217 cmd
, cmd
? cmd
: "NULL");
219 clear_ctrlc(); /* forget any previous Control C */
222 return -1; /* empty command */
224 cmdbuf
= strdup(cmd
);
226 return -1; /* not enough memory */
230 /* Process separators and check for invalid
231 * repeatable commands
234 debug_parser("[PROCESS_SEPARATORS] %s\n", cmd
);
237 * Find separator, or string end
238 * Allow simple escape of ';' by writing "\;"
240 for (inquotes
= 0, sep
= str
; *sep
; sep
++) {
241 if ((*sep
== '\'') &&
242 (*(sep
- 1) != '\\'))
243 inquotes
= !inquotes
;
246 (*sep
== ';' || *sep
== '\n') && /* separator */
247 (sep
!= str
) && /* past string start */
248 (*(sep
- 1) != '\\')) /* and NOT escaped */
253 * Limit the token to data between separators
257 str
= sep
+ 1; /* start of command for next pass */
260 str
= sep
; /* no more commands for next pass */
262 debug_parser("token: \"%s\"\n", token
);
264 /* find macros in this token and replace them */
265 finaltoken
= process_macros(token
, finaltoken
);
267 /* Extract arguments */
268 argc
= cli_parse_line(finaltoken
, argv
);
270 rc
= -1; /* no command at all */
274 if (cmd_process(flag
, argc
, argv
, &repeatable
) != CMD_RET_SUCCESS
)
277 /* Did the user stop this? */
279 rc
= -1; /* if stopped then not repeatable */
287 return rc
? rc
: repeatable
;
290 static int cli_run_command_list(const char *cmd
)
292 return (cli_run_command(cmd
, 0) < 0);
295 /******************************************************************************/
299 * Run a command using the selected parser.
301 * @param cmd Command to run
302 * @param flag Execution flags (CMD_FLAG_...)
303 * @return 0 on success, or != 0 on error.
305 int run_command(const char *cmd
, int flag
)
308 * cli_run_command can return 0 or 1 for success, so clean up
311 if (cli_run_command(cmd
, flag
) == -1)
318 * Run a command using the selected parser, and check if it is repeatable.
320 * @param cmd Command to run
321 * @param flag Execution flags (CMD_FLAG_...)
322 * @return 0 (not repeatable) or 1 (repeatable) on success, -1 on error.
324 static int run_command_repeatable(const char *cmd
, int flag
)
326 return cli_run_command(cmd
, flag
);
329 int run_command_list(const char *cmd
, int len
)
333 return cli_run_command_list(cmd
);
336 /****************************************************************************/
341 char *lastcommand
= NULL
;
347 len
= cli_readline(PSTR(CONFIG_SYS_PROMPT
));
349 flag
= 0; /* assume no special flags for now */
352 lastcommand
= strdup(console_buffer
);
354 flag
|= CMD_FLAG_REPEAT
;
357 my_puts_P(PSTR("<INTERRUPT>\n"));
359 rc
= run_command_repeatable(lastcommand
, flag
);
362 /* invalid command or not repeatable, forget it */
370 command_ret_t
do_run(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
376 return CMD_RET_USAGE
;
378 for (i
= 1; i
< argc
; ++i
) {
381 arg
= getenv(argv
[i
]);
383 printf_P(PSTR("## Error: \"%s\" not defined\n"), argv
[i
]);
384 return CMD_RET_FAILURE
;
387 if (run_command(arg
, flag
) != 0)
388 return CMD_RET_FAILURE
;
390 return CMD_RET_SUCCESS
;