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]
37 __table_start ( struct command
, commands
);
38 static struct command commands_end
[0]
39 __table_end ( struct command
, commands
);
41 /* Avoid dragging in getopt.o unless a command really uses it */
48 * @v command Command name
49 * @v argv Argument list
50 * @ret rc Command exit status
52 * Execute the named command. Unlike a traditional POSIX execv(),
53 * this function returns the exit status of the command.
55 int execv ( const char *command
, char * const argv
[] ) {
59 /* Count number of arguments */
60 for ( argc
= 0 ; argv
[argc
] ; argc
++ ) {}
64 DBG ( "No command\n" );
68 DBG ( "%s: empty argument list\n", command
);
72 /* Reset getopt() library ready for use by the command. This
73 * is an artefact of the POSIX getopt() API within the context
74 * of Etherboot; see the documentation for reset_getopt() for
79 /* Hand off to command implementation */
80 for ( cmd
= commands
; cmd
< commands_end
; cmd
++ ) {
81 if ( strcmp ( command
, cmd
->name
) == 0 )
82 return cmd
->exec ( argc
, ( char ** ) argv
);
85 printf ( "%s: command not found\n", command
);
90 * Split command line into argv array
92 * @v args Command line
93 * @v argv Argument array to populate, or NULL
94 * @ret argc Argument count
96 * Splits the command line into whitespace-delimited arguments. If @c
97 * argv is non-NULL, any whitespace in the command line will be
100 static int split_args ( char *args
, char * argv
[] ) {
104 /* Skip over any whitespace / convert to NUL */
105 while ( *args
== ' ' ) {
110 /* Check for end of line */
113 /* We have found the start of the next argument */
117 /* Skip to start of next whitespace, if any */
118 while ( *args
&& ( *args
!= ' ' ) ) {
126 * Execute command line
128 * @v command Command line
129 * @ret rc Command exit status
131 * Execute the named command and arguments.
133 int system ( const char *command
) {
138 /* Obtain temporary modifiable copy of command line */
139 args
= strdup ( command
);
143 /* Count arguments */
144 argc
= split_args ( args
, NULL
);
146 /* Create argv array and execute command */
148 char * argv
[argc
+ 1];
150 split_args ( args
, argv
);
153 if ( argv
[0][0] != '#' )
154 rc
= execv ( argv
[0], argv
);