Rename e1000_main.c to e1000.c to so we can type 'make bin/e1000.dsk' instead of...
[gpxe.git] / src / core / exec.c
bloba1c073e3a58a3254d17a759a08d4a5c360cf1a6b
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 #include <stdint.h>
20 #include <string.h>
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <unistd.h>
24 #include <getopt.h>
25 #include <errno.h>
26 #include <assert.h>
27 #include <gpxe/tables.h>
28 #include <gpxe/command.h>
30 /** @file
32 * Command execution
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 */
42 int optind;
43 int nextchar;
45 /**
46 * Execute command
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[] ) {
56 struct command *cmd;
57 int argc;
59 /* Count number of arguments */
60 for ( argc = 0 ; argv[argc] ; argc++ ) {}
62 /* Sanity checks */
63 if ( ! command ) {
64 DBG ( "No command\n" );
65 return -EINVAL;
67 if ( ! argc ) {
68 DBG ( "%s: empty argument list\n", command );
69 return -EINVAL;
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
75 * details.
77 reset_getopt();
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 );
86 return -ENOEXEC;
89 /**
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
98 * replaced with NULs.
100 static int split_args ( char *args, char * argv[] ) {
101 int argc = 0;
103 while ( 1 ) {
104 /* Skip over any whitespace / convert to NUL */
105 while ( *args == ' ' ) {
106 if ( argv )
107 *args = '\0';
108 args++;
110 /* Check for end of line */
111 if ( ! *args )
112 break;
113 /* We have found the start of the next argument */
114 if ( argv )
115 argv[argc] = args;
116 argc++;
117 /* Skip to start of next whitespace, if any */
118 while ( *args && ( *args != ' ' ) ) {
119 args++;
122 return argc;
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 ) {
134 char *args;
135 int argc;
136 int rc = 0;
138 /* Obtain temporary modifiable copy of command line */
139 args = strdup ( command );
140 if ( ! args )
141 return -ENOMEM;
143 /* Count arguments */
144 argc = split_args ( args, NULL );
146 /* Create argv array and execute command */
147 if ( argc ) {
148 char * argv[argc + 1];
150 split_args ( args, argv );
151 argv[argc] = NULL;
153 if ( argv[0][0] != '#' )
154 rc = execv ( argv[0], argv );
157 free ( args );
158 return rc;