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., 675 Mass Ave, Cambridge, MA 02139, USA.
27 #include <gpxe/tables.h>
28 #include <gpxe/command.h>
36 static struct command commands[0] __table_start ( commands );
37 static struct command commands_end[0] __table_end ( commands );
42 * @v command Command name
43 * @v argv Argument list
44 * @ret rc Command exit status
46 * Execute the named command. Unlike a traditional POSIX execv(),
47 * this function returns the exit status of the command.
49 int execv ( const char *command, char * const argv[] ) {
53 /* Count number of arguments */
54 for ( argc = 0 ; argv[argc] ; argc++ ) {}
58 DBG ( "No command\n" );
62 DBG ( "%s: empty argument list\n", command );
66 /* Reset getopt() library ready for use by the command. This
67 * is an artefact of the POSIX getopt() API within the context
68 * of Etherboot; see the documentation for reset_getopt() for
73 /* Hand off to command implementation */
74 for ( cmd = commands ; cmd < commands_end ; cmd++ ) {
75 if ( strcmp ( command, cmd->name ) == 0 )
76 return cmd->exec ( argc, ( char ** ) argv );
79 printf ( "%s: command not found\n", command );
84 * Split command line into argv array
86 * @v args Command line
87 * @v argv Argument array to populate, or NULL
88 * @ret argc Argument count
90 * Splits the command line into whitespace-delimited arguments. If @c
91 * argv is non-NULL, any whitespace in the command line will be
94 static int split_args ( char *args, char * argv[] ) {
98 /* Skip over any whitespace / convert to NUL */
99 while ( *args == ' ' ) {
104 /* Check for end of line */
107 /* We have found the start of the next argument */
111 /* Skip to start of next whitespace, if any */
112 while ( *args && ( *args != ' ' ) ) {
120 * Execute command line
122 * @v command Command line
123 * @ret rc Command exit status
125 * Execute the named command and arguments.
127 int system ( const char *command ) {
132 /* Obtain temporary modifiable copy of command line */
133 args = strdup ( command );
137 /* Count arguments */
138 argc = split_args ( args, NULL );
140 /* Create argv array and execute command */
142 char * argv[argc + 1];
144 split_args ( args, argv );
146 rc = execv ( argv[0], argv );