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
19 * You can also choose to distribute this program under the terms of
20 * the Unmodified Binary Distribution Licence (as given in the file
21 * COPYING.UBDL), provided that you have satisfied its requirements.
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL
);
35 #include <ipxe/tables.h>
36 #include <ipxe/command.h>
37 #include <ipxe/parseopt.h>
38 #include <ipxe/settings.h>
39 #include <ipxe/console.h>
40 #include <ipxe/keys.h>
41 #include <ipxe/process.h>
43 #include <ipxe/shell.h>
51 /** Shell stop state */
52 static int stop_state
;
57 * @v command Command name
58 * @v argv Argument list
59 * @ret rc Return status code
61 * Execute the named command. Unlike a traditional POSIX execv(),
62 * this function returns the exit status of the command.
64 int execv ( const char *command
, char * const argv
[] ) {
69 /* Count number of arguments */
70 for ( argc
= 0 ; argv
[argc
] ; argc
++ ) {}
72 /* An empty command is deemed to do nothing, successfully */
73 if ( command
== NULL
) {
80 DBG ( "%s: empty argument list\n", command
);
85 /* Reset getopt() library ready for use by the command. This
86 * is an artefact of the POSIX getopt() API within the context
87 * of Etherboot; see the documentation for reset_getopt() for
92 /* Hand off to command implementation */
93 for_each_table_entry ( cmd
, COMMANDS
) {
94 if ( strcmp ( command
, cmd
->name
) == 0 ) {
95 rc
= cmd
->exec ( argc
, ( char ** ) argv
);
100 printf ( "%s: command not found\n", command
);
104 /* Store error number, if an error occurred */
115 * Split command line into tokens
117 * @v command Command line
118 * @v tokens Token list to populate, or NULL
119 * @ret count Number of tokens
121 * Splits the command line into whitespace-delimited tokens. If @c
122 * tokens is non-NULL, any whitespace in the command line will be
123 * replaced with NULs.
125 static int split_command ( char *command
, char **tokens
) {
129 /* Skip over any whitespace / convert to NUL */
130 while ( isspace ( *command
) ) {
135 /* Check for end of line */
138 /* We have found the start of the next argument */
140 tokens
[count
] = command
;
142 /* Skip to start of next whitespace, if any */
143 while ( *command
&& ! isspace ( *command
) ) {
151 * Process next command only if previous command succeeded
153 * @v rc Status of previous command
154 * @ret process Process next command
156 static int process_on_success ( int rc
) {
161 * Process next command only if previous command failed
163 * @v rc Status of previous command
164 * @ret process Process next command
166 static int process_on_failure ( int rc
) {
171 * Process next command regardless of status from previous command
173 * @v rc Status of previous command
174 * @ret process Process next command
176 static int process_always ( int rc __unused
) {
181 * Find command terminator
183 * @v tokens Token list
184 * @ret process_next "Should next command be processed?" function
185 * @ret argc Argument count
187 static int command_terminator ( char **tokens
,
188 int ( **process_next
) ( int rc
) ) {
191 /* Find first terminating token */
192 for ( i
= 0 ; tokens
[i
] ; i
++ ) {
193 if ( tokens
[i
][0] == '#' ) {
194 /* Start of a comment */
196 } else if ( strcmp ( tokens
[i
], "||" ) == 0 ) {
197 /* Short-circuit logical OR */
198 *process_next
= process_on_failure
;
200 } else if ( strcmp ( tokens
[i
], "&&" ) == 0 ) {
201 /* Short-circuit logical AND */
202 *process_next
= process_on_success
;
204 } else if ( strcmp ( tokens
[i
], ";" ) == 0 ) {
205 /* Process next command unconditionally */
206 *process_next
= process_always
;
211 /* End of token list */
212 *process_next
= NULL
;
217 * Set shell stop state
219 * @v stop Shell stop state
221 void shell_stop ( int stop
) {
226 * Test and consume shell stop state
228 * @v stop Shell stop state to consume
229 * @v stopped Shell had been stopped
231 int shell_stopped ( int stop
) {
234 /* Test to see if we need to stop */
235 stopped
= ( stop_state
>= stop
);
237 /* Consume stop state */
238 if ( stop_state
<= stop
)
245 * Expand settings within a token list
247 * @v argc Argument count
248 * @v tokens Token list
249 * @v argv Argument list to fill in
250 * @ret rc Return status code
252 static int expand_tokens ( int argc
, char **tokens
, char **argv
) {
255 /* Expand each token in turn */
256 for ( i
= 0 ; i
< argc
; i
++ ) {
257 argv
[i
] = expand_settings ( tokens
[i
] );
259 goto err_expand_settings
;
265 assert ( argv
[i
] == NULL
);
266 for ( ; i
>= 0 ; i
-- )
272 * Free an expanded token list
274 * @v argv Argument list
276 static void free_tokens ( char **argv
) {
278 /* Free each expanded argument */
284 * Execute command line
286 * @v command Command line
287 * @ret rc Return status code
289 * Execute the named command and arguments.
291 int system ( const char *command
) {
292 int count
= split_command ( ( char * ) command
, NULL
);
293 char *all_tokens
[ count
+ 1 ];
294 int ( * process_next
) ( int rc
);
301 /* Create modifiable copy of command */
302 command_copy
= strdup ( command
);
303 if ( ! command_copy
)
306 /* Split command into tokens */
307 split_command ( command_copy
, all_tokens
);
308 all_tokens
[count
] = NULL
;
310 /* Process individual commands */
312 for ( tokens
= all_tokens
; ; tokens
+= ( argc
+ 1 ) ) {
314 /* Find command terminator */
315 argc
= command_terminator ( tokens
, &process_next
);
317 /* Expand tokens and execute command */
319 char *argv
[ argc
+ 1 ];
322 if ( ( rc
= expand_tokens ( argc
, tokens
, argv
) ) != 0)
326 /* Execute command */
327 rc
= execv ( argv
[0], argv
);
330 free_tokens ( argv
);
333 /* Stop processing, if applicable */
334 if ( shell_stopped ( SHELL_STOP_COMMAND
) )
337 /* Stop processing if we have reached the end of the
340 if ( ! process_next
)
343 /* Determine whether or not to process next command */
344 process
= process_next ( rc
);
347 /* Free modified copy of command */
348 free ( command_copy
);
354 * Concatenate arguments
356 * @v args Argument list (NULL-terminated)
357 * @ret string Concatenated arguments
359 * The returned string is allocated with malloc(). The caller is
360 * responsible for eventually free()ing this string.
362 char * concat_args ( char **args
) {
368 /* Calculate total string length */
370 for ( arg
= args
; *arg
; arg
++ )
371 len
+= ( 1 /* possible space */ + strlen ( *arg
) );
373 /* Allocate string */
374 string
= zalloc ( len
);
378 /* Populate string */
380 for ( arg
= args
; *arg
; arg
++ ) {
381 ptr
+= sprintf ( ptr
, "%s%s",
382 ( ( arg
== args
) ?
"" : " " ), *arg
);
384 assert ( ptr
< ( string
+ len
) );
389 /** "echo" options */
390 struct echo_options
{
391 /** Do not print trailing newline */
395 /** "echo" option list */
396 static struct option_descriptor echo_opts
[] = {
397 OPTION_DESC ( "n", 'n', no_argument
,
398 struct echo_options
, no_newline
, parse_flag
),
401 /** "echo" command descriptor */
402 static struct command_descriptor echo_cmd
=
403 COMMAND_DESC ( struct echo_options
, echo_opts
, 0, MAX_ARGUMENTS
,
409 * @v argc Argument count
410 * @v argv Argument list
411 * @ret rc Return status code
413 static int echo_exec ( int argc
, char **argv
) {
414 struct echo_options opts
;
419 if ( ( rc
= parse_options ( argc
, argv
, &echo_cmd
, &opts
) ) != 0 )
423 text
= concat_args ( &argv
[optind
] );
428 printf ( "%s%s", text
, ( opts
.no_newline ?
"" : "\n" ) );
434 /** "echo" command */
435 struct command echo_command __command
= {
440 /** "exit" options */
441 struct exit_options
{};
443 /** "exit" option list */
444 static struct option_descriptor exit_opts
[] = {};
446 /** "exit" command descriptor */
447 static struct command_descriptor exit_cmd
=
448 COMMAND_DESC ( struct exit_options
, exit_opts
, 0, 1, "[<status>]" );
453 * @v argc Argument count
454 * @v argv Argument list
455 * @ret rc Return status code
457 static int exit_exec ( int argc
, char **argv
) {
458 struct exit_options opts
;
459 unsigned int exit_code
= 0;
463 if ( ( rc
= parse_options ( argc
, argv
, &exit_cmd
, &opts
) ) != 0 )
466 /* Parse exit status, if present */
467 if ( optind
!= argc
) {
468 if ( ( rc
= parse_integer ( argv
[optind
], &exit_code
) ) != 0 )
472 /* Stop shell processing */
473 shell_stop ( SHELL_STOP_COMMAND_SEQUENCE
);
478 /** "exit" command */
479 struct command exit_command __command
= {
484 /** "isset" options */
485 struct isset_options
{};
487 /** "isset" option list */
488 static struct option_descriptor isset_opts
[] = {};
490 /** "isset" command descriptor */
491 static struct command_descriptor isset_cmd
=
492 COMMAND_DESC ( struct isset_options
, isset_opts
, 1, 1, "<value>" );
497 * @v argc Argument count
498 * @v argv Argument list
499 * @ret rc Return status code
501 static int isset_exec ( int argc
, char **argv
) {
502 struct isset_options opts
;
506 if ( ( rc
= parse_options ( argc
, argv
, &isset_cmd
, &opts
) ) != 0 )
509 /* Return success iff argument is non-empty */
510 return ( argv
[optind
][0] ?
0 : -ENOENT
);
513 /** "isset" command */
514 struct command isset_command __command
= {
519 /** "iseq" options */
520 struct iseq_options
{};
522 /** "iseq" option list */
523 static struct option_descriptor iseq_opts
[] = {};
525 /** "iseq" command descriptor */
526 static struct command_descriptor iseq_cmd
=
527 COMMAND_DESC ( struct iseq_options
, iseq_opts
, 2, 2,
528 "<value1> <value2>" );
533 * @v argc Argument count
534 * @v argv Argument list
535 * @ret rc Return status code
537 static int iseq_exec ( int argc
, char **argv
) {
538 struct iseq_options opts
;
542 if ( ( rc
= parse_options ( argc
, argv
, &iseq_cmd
, &opts
) ) != 0 )
545 /* Return success iff arguments are equal */
546 return ( ( strcmp ( argv
[optind
], argv
[ optind
+ 1 ] ) == 0 ) ?
550 /** "iseq" command */
551 struct command iseq_command __command
= {
556 /** "sleep" options */
557 struct sleep_options
{};
559 /** "sleep" option list */
560 static struct option_descriptor sleep_opts
[] = {};
562 /** "sleep" command descriptor */
563 static struct command_descriptor sleep_cmd
=
564 COMMAND_DESC ( struct sleep_options
, sleep_opts
, 1, 1, "<seconds>" );
569 * @v argc Argument count
570 * @v argv Argument list
571 * @ret rc Return status code
573 static int sleep_exec ( int argc
, char **argv
) {
574 struct sleep_options opts
;
575 unsigned int seconds
;
581 if ( ( rc
= parse_options ( argc
, argv
, &sleep_cmd
, &opts
) ) != 0 )
584 /* Parse number of seconds */
585 if ( ( rc
= parse_integer ( argv
[optind
], &seconds
) ) != 0 )
588 /* Delay for specified number of seconds */
590 delay
= ( seconds
* TICKS_PER_SEC
);
591 while ( ( currticks() - start
) <= delay
) {
593 if ( iskey() && ( getchar() == CTRL_C
) )
601 /** "sleep" command */
602 struct command sleep_command __command
= {