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.
19 FILE_LICENCE ( GPL2_OR_LATER
);
30 #include <gpxe/tables.h>
31 #include <gpxe/command.h>
32 #include <gpxe/settings.h>
40 /* Avoid dragging in getopt.o unless a command really uses it */
47 * @v command Command name
48 * @v argv Argument list
49 * @ret rc Command exit status
51 * Execute the named command. Unlike a traditional POSIX execv(),
52 * this function returns the exit status of the command.
54 int execv ( const char *command
, char * const argv
[] ) {
58 /* Count number of arguments */
59 for ( argc
= 0 ; argv
[argc
] ; argc
++ ) {}
63 DBG ( "No command\n" );
67 DBG ( "%s: empty argument list\n", command
);
71 /* Reset getopt() library ready for use by the command. This
72 * is an artefact of the POSIX getopt() API within the context
73 * of Etherboot; see the documentation for reset_getopt() for
78 /* Hand off to command implementation */
79 for_each_table_entry ( cmd
, COMMANDS
) {
80 if ( strcmp ( command
, cmd
->name
) == 0 )
81 return cmd
->exec ( argc
, ( char ** ) argv
);
84 printf ( "%s: command not found\n", command
);
89 * Expand variables within command line
91 * @v command Command line
92 * @ret expcmd Expanded command line
94 * The expanded command line is allocated with malloc() and the caller
95 * must eventually free() it.
97 static char * expand_command ( const char *command
) {
108 /* Obtain temporary modifiable copy of command line */
109 expcmd
= strdup ( command
);
113 /* Expand while expansions remain */
119 start
= strstr ( expcmd
, "${" );
123 name
= ( start
+ 2 );
126 end
= strstr ( name
, "}" );
132 /* Determine setting length */
133 setting_len
= fetchf_named_setting ( name
, NULL
, 0 );
134 if ( setting_len
< 0 )
135 setting_len
= 0; /* Treat error as empty setting */
137 /* Read setting into temporary buffer */
139 char setting_buf
[ setting_len
+ 1 ];
141 setting_buf
[0] = '\0';
142 fetchf_named_setting ( name
, setting_buf
,
143 sizeof ( setting_buf
) );
145 /* Construct expanded string and discard old string */
147 new_len
= asprintf ( &expcmd
, "%s%s%s",
148 head
, setting_buf
, tail
);
159 * Split command line into argv array
161 * @v args Command line
162 * @v argv Argument array to populate, or NULL
163 * @ret argc Argument count
165 * Splits the command line into whitespace-delimited arguments. If @c
166 * argv is non-NULL, any whitespace in the command line will be
167 * replaced with NULs.
169 static int split_args ( char *args
, char * argv
[] ) {
173 /* Skip over any whitespace / convert to NUL */
174 while ( isspace ( *args
) ) {
179 /* Check for end of line */
182 /* We have found the start of the next argument */
186 /* Skip to start of next whitespace, if any */
187 while ( *args
&& ! isspace ( *args
) ) {
195 * Execute command line
197 * @v command Command line
198 * @ret rc Command exit status
200 * Execute the named command and arguments.
202 int system ( const char *command
) {
207 /* Perform variable expansion */
208 args
= expand_command ( command
);
212 /* Count arguments */
213 argc
= split_args ( args
, NULL
);
215 /* Create argv array and execute command */
217 char * argv
[argc
+ 1];
219 split_args ( args
, argv
);
222 if ( argv
[0][0] != '#' )
223 rc
= execv ( argv
[0], argv
);
233 * @v argc Argument count
234 * @v argv Argument list
237 static int echo_exec ( int argc
, char **argv
) {
240 for ( i
= 1 ; i
< argc
; i
++ ) {
241 printf ( "%s%s", ( ( i
== 1 ) ? "" : " " ), argv
[i
] );
247 /** "echo" command */
248 struct command echo_command __command
= {