[contrib] Allow Network Protocol header to display in rom-o-matic
[gpxe.git] / src / core / exec.c
blob6c16aa4450d282829741e6f5761e108d0af3c284
1 /*
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 );
21 #include <stdint.h>
22 #include <string.h>
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <ctype.h>
26 #include <unistd.h>
27 #include <getopt.h>
28 #include <errno.h>
29 #include <assert.h>
30 #include <gpxe/tables.h>
31 #include <gpxe/command.h>
32 #include <gpxe/settings.h>
34 /** @file
36 * Command execution
40 /* Avoid dragging in getopt.o unless a command really uses it */
41 int optind;
42 int nextchar;
44 /**
45 * Execute command
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[] ) {
55 struct command *cmd;
56 int argc;
58 /* Count number of arguments */
59 for ( argc = 0 ; argv[argc] ; argc++ ) {}
61 /* Sanity checks */
62 if ( ! command ) {
63 DBG ( "No command\n" );
64 return -EINVAL;
66 if ( ! argc ) {
67 DBG ( "%s: empty argument list\n", command );
68 return -EINVAL;
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
74 * details.
76 reset_getopt();
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 );
85 return -ENOEXEC;
88 /**
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 ) {
98 char *expcmd;
99 char *start;
100 char *end;
101 char *head;
102 char *name;
103 char *tail;
104 int setting_len;
105 int new_len;
106 char *tmp;
108 /* Obtain temporary modifiable copy of command line */
109 expcmd = strdup ( command );
110 if ( ! expcmd )
111 return NULL;
113 /* Expand while expansions remain */
114 while ( 1 ) {
116 head = expcmd;
118 /* Locate opener */
119 start = strstr ( expcmd, "${" );
120 if ( ! start )
121 break;
122 *start = '\0';
123 name = ( start + 2 );
125 /* Locate closer */
126 end = strstr ( name, "}" );
127 if ( ! end )
128 break;
129 *end = '\0';
130 tail = ( end + 1 );
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 */
146 tmp = expcmd;
147 new_len = asprintf ( &expcmd, "%s%s%s",
148 head, setting_buf, tail );
149 free ( tmp );
150 if ( new_len < 0 )
151 return NULL;
155 return expcmd;
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[] ) {
170 int argc = 0;
172 while ( 1 ) {
173 /* Skip over any whitespace / convert to NUL */
174 while ( isspace ( *args ) ) {
175 if ( argv )
176 *args = '\0';
177 args++;
179 /* Check for end of line */
180 if ( ! *args )
181 break;
182 /* We have found the start of the next argument */
183 if ( argv )
184 argv[argc] = args;
185 argc++;
186 /* Skip to start of next whitespace, if any */
187 while ( *args && ! isspace ( *args ) ) {
188 args++;
191 return argc;
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 ) {
203 char *args;
204 int argc;
205 int rc = 0;
207 /* Perform variable expansion */
208 args = expand_command ( command );
209 if ( ! args )
210 return -ENOMEM;
212 /* Count arguments */
213 argc = split_args ( args, NULL );
215 /* Create argv array and execute command */
216 if ( argc ) {
217 char * argv[argc + 1];
219 split_args ( args, argv );
220 argv[argc] = NULL;
222 if ( argv[0][0] != '#' )
223 rc = execv ( argv[0], argv );
226 free ( args );
227 return rc;
231 * The "echo" command
233 * @v argc Argument count
234 * @v argv Argument list
235 * @ret rc Exit code
237 static int echo_exec ( int argc, char **argv ) {
238 int i;
240 for ( i = 1 ; i < argc ; i++ ) {
241 printf ( "%s%s", ( ( i == 1 ) ? "" : " " ), argv[i] );
243 printf ( "\n" );
244 return 0;
247 /** "echo" command */
248 struct command echo_command __command = {
249 .name = "echo",
250 .exec = echo_exec,