2 * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 FILE_LICENCE ( GPL2_OR_LATER );
31 #include <ipxe/tables.h>
32 #include <ipxe/command.h>
33 #include <ipxe/parseopt.h>
34 #include <ipxe/settings.h>
35 #include <ipxe/console.h>
36 #include <ipxe/keys.h>
37 #include <ipxe/process.h>
39 #include <ipxe/shell.h>
47 /** Shell stop state */
48 static int stop_state;
53 * @v command Command name
54 * @v argv Argument list
55 * @ret rc Return status code
57 * Execute the named command. Unlike a traditional POSIX execv(),
58 * this function returns the exit status of the command.
60 int execv ( const char *command, char * const argv[] ) {
65 /* Count number of arguments */
66 for ( argc = 0 ; argv[argc] ; argc++ ) {}
68 /* An empty command is deemed to do nothing, successfully */
69 if ( command == NULL ) {
76 DBG ( "%s: empty argument list\n", command );
81 /* Reset getopt() library ready for use by the command. This
82 * is an artefact of the POSIX getopt() API within the context
83 * of Etherboot; see the documentation for reset_getopt() for
88 /* Hand off to command implementation */
89 for_each_table_entry ( cmd, COMMANDS ) {
90 if ( strcmp ( command, cmd->name ) == 0 ) {
91 rc = cmd->exec ( argc, ( char ** ) argv );
96 printf ( "%s: command not found\n", command );
100 /* Store error number, if an error occurred */
111 * Split command line into tokens
113 * @v command Command line
114 * @v tokens Token list to populate, or NULL
115 * @ret count Number of tokens
117 * Splits the command line into whitespace-delimited tokens. If @c
118 * tokens is non-NULL, any whitespace in the command line will be
119 * replaced with NULs.
121 static int split_command ( char *command, char **tokens ) {
125 /* Skip over any whitespace / convert to NUL */
126 while ( isspace ( *command ) ) {
131 /* Check for end of line */
134 /* We have found the start of the next argument */
136 tokens[count] = command;
138 /* Skip to start of next whitespace, if any */
139 while ( *command && ! isspace ( *command ) ) {
147 * Process next command only if previous command succeeded
149 * @v rc Status of previous command
150 * @ret process Process next command
152 static int process_on_success ( int rc ) {
157 * Process next command only if previous command failed
159 * @v rc Status of previous command
160 * @ret process Process next command
162 static int process_on_failure ( int rc ) {
167 * Process next command regardless of status from previous command
169 * @v rc Status of previous command
170 * @ret process Process next command
172 static int process_always ( int rc __unused ) {
177 * Find command terminator
179 * @v tokens Token list
180 * @ret process_next "Should next command be processed?" function
181 * @ret argc Argument count
183 static int command_terminator ( char **tokens,
184 int ( **process_next ) ( int rc ) ) {
187 /* Find first terminating token */
188 for ( i = 0 ; tokens[i] ; i++ ) {
189 if ( tokens[i][0] == '#' ) {
190 /* Start of a comment */
192 } else if ( strcmp ( tokens[i], "||" ) == 0 ) {
193 /* Short-circuit logical OR */
194 *process_next = process_on_failure;
196 } else if ( strcmp ( tokens[i], "&&" ) == 0 ) {
197 /* Short-circuit logical AND */
198 *process_next = process_on_success;
200 } else if ( strcmp ( tokens[i], ";" ) == 0 ) {
201 /* Process next command unconditionally */
202 *process_next = process_always;
207 /* End of token list */
208 *process_next = NULL;
213 * Set shell stop state
215 * @v stop Shell stop state
217 void shell_stop ( int stop ) {
222 * Test and consume shell stop state
224 * @v stop Shell stop state to consume
225 * @v stopped Shell had been stopped
227 int shell_stopped ( int stop ) {
230 /* Test to see if we need to stop */
231 stopped = ( stop_state >= stop );
233 /* Consume stop state */
234 if ( stop_state <= stop )
241 * Expand settings within a token list
243 * @v argc Argument count
244 * @v tokens Token list
245 * @v argv Argument list to fill in
246 * @ret rc Return status code
248 static int expand_tokens ( int argc, char **tokens, char **argv ) {
251 /* Expand each token in turn */
252 for ( i = 0 ; i < argc ; i++ ) {
253 argv[i] = expand_settings ( tokens[i] );
255 goto err_expand_settings;
261 assert ( argv[i] == NULL );
262 for ( ; i >= 0 ; i-- )
268 * Free an expanded token list
270 * @v argv Argument list
272 static void free_tokens ( char **argv ) {
274 /* Free each expanded argument */
280 * Execute command line
282 * @v command Command line
283 * @ret rc Return status code
285 * Execute the named command and arguments.
287 int system ( const char *command ) {
288 int count = split_command ( ( char * ) command, NULL );
289 char *all_tokens[ count + 1 ];
290 int ( * process_next ) ( int rc );
297 /* Create modifiable copy of command */
298 command_copy = strdup ( command );
299 if ( ! command_copy )
302 /* Split command into tokens */
303 split_command ( command_copy, all_tokens );
304 all_tokens[count] = NULL;
306 /* Process individual commands */
308 for ( tokens = all_tokens ; ; tokens += ( argc + 1 ) ) {
310 /* Find command terminator */
311 argc = command_terminator ( tokens, &process_next );
313 /* Expand tokens and execute command */
315 char *argv[ argc + 1 ];
318 if ( ( rc = expand_tokens ( argc, tokens, argv ) ) != 0)
322 /* Execute command */
323 rc = execv ( argv[0], argv );
326 free_tokens ( argv );
329 /* Stop processing, if applicable */
330 if ( shell_stopped ( SHELL_STOP_COMMAND ) )
333 /* Stop processing if we have reached the end of the
336 if ( ! process_next )
339 /* Determine whether or not to process next command */
340 process = process_next ( rc );
343 /* Free modified copy of command */
344 free ( command_copy );
350 * Concatenate arguments
352 * @v args Argument list (NULL-terminated)
353 * @ret string Concatenated arguments
355 * The returned string is allocated with malloc(). The caller is
356 * responsible for eventually free()ing this string.
358 char * concat_args ( char **args ) {
364 /* Calculate total string length */
366 for ( arg = args ; *arg ; arg++ )
367 len += ( 1 /* possible space */ + strlen ( *arg ) );
369 /* Allocate string */
370 string = zalloc ( len );
374 /* Populate string */
376 for ( arg = args ; *arg ; arg++ ) {
377 ptr += sprintf ( ptr, "%s%s",
378 ( ( arg == args ) ? "" : " " ), *arg );
380 assert ( ptr < ( string + len ) );
385 /** "echo" options */
386 struct echo_options {
387 /** Do not print trailing newline */
391 /** "echo" option list */
392 static struct option_descriptor echo_opts[] = {
393 OPTION_DESC ( "n", 'n', no_argument,
394 struct echo_options, no_newline, parse_flag ),
397 /** "echo" command descriptor */
398 static struct command_descriptor echo_cmd =
399 COMMAND_DESC ( struct echo_options, echo_opts, 0, MAX_ARGUMENTS,
405 * @v argc Argument count
406 * @v argv Argument list
407 * @ret rc Return status code
409 static int echo_exec ( int argc, char **argv ) {
410 struct echo_options opts;
415 if ( ( rc = parse_options ( argc, argv, &echo_cmd, &opts ) ) != 0 )
419 text = concat_args ( &argv[optind] );
424 printf ( "%s%s", text, ( opts.no_newline ? "" : "\n" ) );
430 /** "echo" command */
431 struct command echo_command __command = {
436 /** "exit" options */
437 struct exit_options {};
439 /** "exit" option list */
440 static struct option_descriptor exit_opts[] = {};
442 /** "exit" command descriptor */
443 static struct command_descriptor exit_cmd =
444 COMMAND_DESC ( struct exit_options, exit_opts, 0, 1, "[<status>]" );
449 * @v argc Argument count
450 * @v argv Argument list
451 * @ret rc Return status code
453 static int exit_exec ( int argc, char **argv ) {
454 struct exit_options opts;
455 unsigned int exit_code = 0;
459 if ( ( rc = parse_options ( argc, argv, &exit_cmd, &opts ) ) != 0 )
462 /* Parse exit status, if present */
463 if ( optind != argc ) {
464 if ( ( rc = parse_integer ( argv[optind], &exit_code ) ) != 0 )
468 /* Stop shell processing */
469 shell_stop ( SHELL_STOP_COMMAND_SEQUENCE );
474 /** "exit" command */
475 struct command exit_command __command = {
480 /** "isset" options */
481 struct isset_options {};
483 /** "isset" option list */
484 static struct option_descriptor isset_opts[] = {};
486 /** "isset" command descriptor */
487 static struct command_descriptor isset_cmd =
488 COMMAND_DESC ( struct isset_options, isset_opts, 1, 1, "<value>" );
493 * @v argc Argument count
494 * @v argv Argument list
495 * @ret rc Return status code
497 static int isset_exec ( int argc, char **argv ) {
498 struct isset_options opts;
502 if ( ( rc = parse_options ( argc, argv, &isset_cmd, &opts ) ) != 0 )
505 /* Return success iff argument is non-empty */
506 return ( argv[optind][0] ? 0 : -ENOENT );
509 /** "isset" command */
510 struct command isset_command __command = {
515 /** "iseq" options */
516 struct iseq_options {};
518 /** "iseq" option list */
519 static struct option_descriptor iseq_opts[] = {};
521 /** "iseq" command descriptor */
522 static struct command_descriptor iseq_cmd =
523 COMMAND_DESC ( struct iseq_options, iseq_opts, 2, 2,
524 "<value1> <value2>" );
529 * @v argc Argument count
530 * @v argv Argument list
531 * @ret rc Return status code
533 static int iseq_exec ( int argc, char **argv ) {
534 struct iseq_options opts;
538 if ( ( rc = parse_options ( argc, argv, &iseq_cmd, &opts ) ) != 0 )
541 /* Return success iff arguments are equal */
542 return ( ( strcmp ( argv[optind], argv[ optind + 1 ] ) == 0 ) ?
546 /** "iseq" command */
547 struct command iseq_command __command = {
552 /** "sleep" options */
553 struct sleep_options {};
555 /** "sleep" option list */
556 static struct option_descriptor sleep_opts[] = {};
558 /** "sleep" command descriptor */
559 static struct command_descriptor sleep_cmd =
560 COMMAND_DESC ( struct sleep_options, sleep_opts, 1, 1, "<seconds>" );
565 * @v argc Argument count
566 * @v argv Argument list
567 * @ret rc Return status code
569 static int sleep_exec ( int argc, char **argv ) {
570 struct sleep_options opts;
571 unsigned int seconds;
577 if ( ( rc = parse_options ( argc, argv, &sleep_cmd, &opts ) ) != 0 )
580 /* Parse number of seconds */
581 if ( ( rc = parse_integer ( argv[optind], &seconds ) ) != 0 )
584 /* Delay for specified number of seconds */
586 delay = ( seconds * TICKS_PER_SEC );
587 while ( ( currticks() - start ) <= delay ) {
589 if ( iskey() && ( getchar() == CTRL_C ) )
597 /** "sleep" command */
598 struct command sleep_command __command = {