[process] Include process name in debug messages
[ipxe.git] / src / core / getopt.c
1 /*
2 * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
3 *
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.
8 *
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.
13 *
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., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA.
18 *
19 * You can also choose to distribute this program under the terms of
20 * the Unmodified Binary Distribution Licence (as given in the file
21 * COPYING.UBDL), provided that you have satisfied its requirements.
22 */
23
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25
26 #include <stdint.h>
27 #include <string.h>
28 #include <stdio.h>
29 #include <getopt.h>
30
31 /** @file
32 *
33 * Parse command-line options
34 *
35 */
36
37 /**
38 * Option argument
39 *
40 * This will point to the argument for the most recently returned
41 * option, if applicable.
42 */
43 char *optarg;
44
45 /**
46 * Current option index
47 *
48 * This is an index into the argv[] array. When getopt() returns -1,
49 * @c optind is the index to the first element that is not an option.
50 */
51 int optind;
52
53 /**
54 * Current option character index
55 *
56 * This is an index into the current element of argv[].
57 */
58 int nextchar;
59
60 /**
61 * Unrecognised option
62 *
63 * When an unrecognised option is encountered, the actual option
64 * character is stored in @c optopt.
65 */
66 int optopt;
67
68 /**
69 * Get option argument from argv[] array
70 *
71 * @v argc Argument count
72 * @v argv Argument list
73 * @ret argument Option argument, or NULL
74 *
75 * Grab the next element of argv[], if it exists and is not an option.
76 */
77 static const char * get_argv_argument ( int argc, char * const argv[] ) {
78 char *arg;
79
80 /* Don't overrun argv[] */
81 if ( optind >= argc )
82 return NULL;
83 arg = argv[optind];
84
85 /* If next argv element is an option, then it's not usable as
86 * an argument.
87 */
88 if ( *arg == '-' )
89 return NULL;
90
91 /** Consume this argv element, and return it */
92 optind++;
93 return arg;
94 }
95
96 /**
97 * Match long option
98 *
99 * @v argc Argument count
100 * @v argv Argument list
101 * @v opttext Option text within current argv[] element
102 * @v longopt Long option specification
103 * @ret option Option to return from getopt()
104 * @ret matched Found a match for this long option
105 */
106 static int match_long_option ( int argc, char * const argv[],
107 const char *opttext,
108 const struct option *longopt, int *option ) {
109 size_t optlen;
110 const char *argument = NULL;
111
112 /* Compare option name */
113 optlen = strlen ( longopt->name );
114 if ( strncmp ( opttext, longopt->name, optlen ) != 0 )
115 return 0;
116
117 /* Check for inline argument */
118 if ( opttext[optlen] == '=' ) {
119 argument = &opttext[ optlen + 1 ];
120 } else if ( opttext[optlen] ) {
121 /* Long option with trailing garbage - no match */
122 return 0;
123 }
124
125 /* Consume this argv element */
126 optind++;
127
128 /* If we want an argument but don't have one yet, try to grab
129 * the next argv element
130 */
131 if ( ( longopt->has_arg != no_argument ) && ( ! argument ) )
132 argument = get_argv_argument ( argc, argv );
133
134 /* If we need an argument but don't have one, sulk */
135 if ( ( longopt->has_arg == required_argument ) && ( ! argument ) ) {
136 printf ( "Option \"%s\" requires an argument\n",
137 longopt->name );
138 *option = ':';
139 return 1;
140 }
141
142 /* If we have an argument where we shouldn't have one, sulk */
143 if ( ( longopt->has_arg == no_argument ) && argument ) {
144 printf ( "Option \"%s\" takes no argument\n", longopt->name );
145 *option = ':';
146 return 1;
147 }
148
149 /* Store values and return success */
150 optarg = ( char * ) argument;
151 if ( longopt->flag ) {
152 *(longopt->flag) = longopt->val;
153 *option = 0;
154 } else {
155 *option = longopt->val;
156 }
157 return 1;
158 }
159
160 /**
161 * Match short option
162 *
163 * @v argc Argument count
164 * @v argv Argument list
165 * @v opttext Option text within current argv[] element
166 * @v shortopt Option character from option specification
167 * @ret option Option to return from getopt()
168 * @ret matched Found a match for this short option
169 */
170 static int match_short_option ( int argc, char * const argv[],
171 const char *opttext, int shortopt,
172 enum getopt_argument_requirement has_arg,
173 int *option ) {
174 const char *argument = NULL;
175
176 /* Compare option character */
177 if ( *opttext != shortopt )
178 return 0;
179
180 /* Consume option character */
181 opttext++;
182 nextchar++;
183 if ( *opttext ) {
184 if ( has_arg != no_argument ) {
185 /* Consume remainder of element as inline argument */
186 argument = opttext;
187 optind++;
188 nextchar = 0;
189 }
190 } else {
191 /* Reached end of argv element */
192 optind++;
193 nextchar = 0;
194 }
195
196 /* If we want an argument but don't have one yet, try to grab
197 * the next argv element
198 */
199 if ( ( has_arg != no_argument ) && ( ! argument ) )
200 argument = get_argv_argument ( argc, argv );
201
202 /* If we need an argument but don't have one, sulk */
203 if ( ( has_arg == required_argument ) && ( ! argument ) ) {
204 printf ( "Option \"%c\" requires an argument\n", shortopt );
205 *option = ':';
206 return 1;
207 }
208
209 /* Store values and return success */
210 optarg = ( char * ) argument;
211 *option = shortopt;
212 return 1;
213 }
214
215 /**
216 * Parse command-line options
217 *
218 * @v argc Argument count
219 * @v argv Argument list
220 * @v optstring Option specification string
221 * @v longopts Long option specification table
222 * @ret longindex Index of long option (or NULL)
223 * @ret option Option found, or -1 for no more options
224 *
225 * Note that the caller must arrange for reset_getopt() to be called
226 * before each set of calls to getopt_long(). In Etherboot, this is
227 * done automatically by execv().
228 */
229 int getopt_long ( int argc, char * const argv[], const char *optstring,
230 const struct option *longopts, int *longindex ) {
231 const char *opttext = argv[optind];
232 const struct option *longopt;
233 int shortopt;
234 enum getopt_argument_requirement has_arg;
235 int option;
236
237 /* Check for end of argv array */
238 if ( optind >= argc )
239 return -1;
240
241 /* Check for end of options */
242 if ( *(opttext++) != '-' )
243 return -1;
244
245 /* Check for long options */
246 if ( *(opttext++) == '-' ) {
247 /* "--" indicates end of options */
248 if ( *opttext == '\0' ) {
249 optind++;
250 return -1;
251 }
252 for ( longopt = longopts ; longopt->name ; longopt++ ) {
253 if ( ! match_long_option ( argc, argv, opttext,
254 longopt, &option ) )
255 continue;
256 if ( longindex )
257 *longindex = ( longopt - longopts );
258 return option;
259 }
260 optopt = '?';
261 printf ( "Unrecognised option \"--%s\"\n", opttext );
262 return '?';
263 }
264
265 /* Check for short options */
266 if ( nextchar < 1 )
267 nextchar = 1;
268 opttext = ( argv[optind] + nextchar );
269 while ( ( shortopt = *(optstring++) ) ) {
270 has_arg = no_argument;
271 while ( *optstring == ':' ) {
272 has_arg++;
273 optstring++;
274 }
275 if ( match_short_option ( argc, argv, opttext, shortopt,
276 has_arg, &option ) ) {
277 return option;
278 }
279 }
280 optopt = *opttext;
281 printf ( "Unrecognised option \"-%c\"\n", optopt );
282 return '?';
283 }