getopt.c revision 1.1.1.5 1 1.1 jruoho /******************************************************************************
2 1.1 jruoho *
3 1.1 jruoho * Module Name: getopt
4 1.1 jruoho *
5 1.1 jruoho *****************************************************************************/
6 1.1 jruoho
7 1.1.1.2 jruoho /*
8 1.1.1.5 christos * Copyright (C) 2000 - 2015, Intel Corp.
9 1.1 jruoho * All rights reserved.
10 1.1 jruoho *
11 1.1.1.2 jruoho * Redistribution and use in source and binary forms, with or without
12 1.1.1.2 jruoho * modification, are permitted provided that the following conditions
13 1.1.1.2 jruoho * are met:
14 1.1.1.2 jruoho * 1. Redistributions of source code must retain the above copyright
15 1.1.1.2 jruoho * notice, this list of conditions, and the following disclaimer,
16 1.1.1.2 jruoho * without modification.
17 1.1.1.2 jruoho * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18 1.1.1.2 jruoho * substantially similar to the "NO WARRANTY" disclaimer below
19 1.1.1.2 jruoho * ("Disclaimer") and any redistribution must be conditioned upon
20 1.1.1.2 jruoho * including a substantially similar Disclaimer requirement for further
21 1.1.1.2 jruoho * binary redistribution.
22 1.1.1.2 jruoho * 3. Neither the names of the above-listed copyright holders nor the names
23 1.1.1.2 jruoho * of any contributors may be used to endorse or promote products derived
24 1.1.1.2 jruoho * from this software without specific prior written permission.
25 1.1.1.2 jruoho *
26 1.1.1.2 jruoho * Alternatively, this software may be distributed under the terms of the
27 1.1.1.2 jruoho * GNU General Public License ("GPL") version 2 as published by the Free
28 1.1.1.2 jruoho * Software Foundation.
29 1.1.1.2 jruoho *
30 1.1.1.2 jruoho * NO WARRANTY
31 1.1.1.2 jruoho * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 1.1.1.2 jruoho * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 1.1.1.2 jruoho * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34 1.1.1.2 jruoho * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 1.1.1.2 jruoho * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 1.1.1.2 jruoho * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 1.1.1.2 jruoho * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 1.1.1.2 jruoho * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 1.1.1.2 jruoho * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40 1.1.1.2 jruoho * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 1.1.1.2 jruoho * POSSIBILITY OF SUCH DAMAGES.
42 1.1.1.2 jruoho */
43 1.1 jruoho
44 1.1.1.3 christos /*
45 1.1.1.3 christos * ACPICA getopt() implementation
46 1.1.1.3 christos *
47 1.1.1.3 christos * Option strings:
48 1.1.1.3 christos * "f" - Option has no arguments
49 1.1.1.3 christos * "f:" - Option requires an argument
50 1.1.1.3 christos * "f^" - Option has optional single-char sub-options
51 1.1.1.3 christos * "f|" - Option has required single-char sub-options
52 1.1.1.3 christos */
53 1.1 jruoho
54 1.1 jruoho #include "acpi.h"
55 1.1 jruoho #include "accommon.h"
56 1.1 jruoho #include "acapps.h"
57 1.1 jruoho
58 1.1.1.3 christos #define ACPI_OPTION_ERROR(msg, badchar) \
59 1.1.1.4 christos if (AcpiGbl_Opterr) {AcpiLogError ("%s%c\n", msg, badchar);}
60 1.1.1.3 christos
61 1.1 jruoho
62 1.1.1.3 christos int AcpiGbl_Opterr = 1;
63 1.1.1.3 christos int AcpiGbl_Optind = 1;
64 1.1.1.3 christos int AcpiGbl_SubOptChar = 0;
65 1.1.1.3 christos char *AcpiGbl_Optarg;
66 1.1 jruoho
67 1.1.1.3 christos static int CurrentCharPtr = 1;
68 1.1.1.3 christos
69 1.1.1.3 christos
70 1.1.1.3 christos /*******************************************************************************
71 1.1.1.3 christos *
72 1.1.1.3 christos * FUNCTION: AcpiGetoptArgument
73 1.1.1.3 christos *
74 1.1.1.3 christos * PARAMETERS: argc, argv - from main
75 1.1.1.3 christos *
76 1.1.1.3 christos * RETURN: 0 if an argument was found, -1 otherwise. Sets AcpiGbl_Optarg
77 1.1.1.3 christos * to point to the next argument.
78 1.1.1.3 christos *
79 1.1.1.3 christos * DESCRIPTION: Get the next argument. Used to obtain arguments for the
80 1.1.1.3 christos * two-character options after the original call to AcpiGetopt.
81 1.1.1.3 christos * Note: Either the argument starts at the next character after
82 1.1.1.3 christos * the option, or it is pointed to by the next argv entry.
83 1.1.1.3 christos * (After call to AcpiGetopt, we need to backup to the previous
84 1.1.1.3 christos * argv entry).
85 1.1.1.3 christos *
86 1.1.1.3 christos ******************************************************************************/
87 1.1.1.3 christos
88 1.1.1.3 christos int
89 1.1.1.3 christos AcpiGetoptArgument (
90 1.1.1.3 christos int argc,
91 1.1.1.3 christos char **argv)
92 1.1.1.3 christos {
93 1.1.1.3 christos AcpiGbl_Optind--;
94 1.1.1.3 christos CurrentCharPtr++;
95 1.1.1.3 christos
96 1.1.1.3 christos if (argv[AcpiGbl_Optind][(int) (CurrentCharPtr+1)] != '\0')
97 1.1.1.3 christos {
98 1.1.1.3 christos AcpiGbl_Optarg = &argv[AcpiGbl_Optind++][(int) (CurrentCharPtr+1)];
99 1.1.1.3 christos }
100 1.1.1.3 christos else if (++AcpiGbl_Optind >= argc)
101 1.1.1.3 christos {
102 1.1.1.3 christos ACPI_OPTION_ERROR ("Option requires an argument: -", 'v');
103 1.1.1.3 christos
104 1.1.1.3 christos CurrentCharPtr = 1;
105 1.1.1.3 christos return (-1);
106 1.1.1.3 christos }
107 1.1.1.3 christos else
108 1.1.1.3 christos {
109 1.1.1.3 christos AcpiGbl_Optarg = argv[AcpiGbl_Optind++];
110 1.1.1.3 christos }
111 1.1.1.3 christos
112 1.1.1.3 christos CurrentCharPtr = 1;
113 1.1.1.3 christos return (0);
114 1.1.1.3 christos }
115 1.1 jruoho
116 1.1 jruoho
117 1.1 jruoho /*******************************************************************************
118 1.1 jruoho *
119 1.1 jruoho * FUNCTION: AcpiGetopt
120 1.1 jruoho *
121 1.1 jruoho * PARAMETERS: argc, argv - from main
122 1.1 jruoho * opts - options info list
123 1.1 jruoho *
124 1.1.1.4 christos * RETURN: Option character or ACPI_OPT_END
125 1.1 jruoho *
126 1.1 jruoho * DESCRIPTION: Get the next option
127 1.1 jruoho *
128 1.1 jruoho ******************************************************************************/
129 1.1 jruoho
130 1.1 jruoho int
131 1.1 jruoho AcpiGetopt(
132 1.1 jruoho int argc,
133 1.1 jruoho char **argv,
134 1.1 jruoho char *opts)
135 1.1 jruoho {
136 1.1 jruoho int CurrentChar;
137 1.1 jruoho char *OptsPtr;
138 1.1 jruoho
139 1.1 jruoho
140 1.1 jruoho if (CurrentCharPtr == 1)
141 1.1 jruoho {
142 1.1 jruoho if (AcpiGbl_Optind >= argc ||
143 1.1 jruoho argv[AcpiGbl_Optind][0] != '-' ||
144 1.1 jruoho argv[AcpiGbl_Optind][1] == '\0')
145 1.1 jruoho {
146 1.1.1.4 christos return (ACPI_OPT_END);
147 1.1 jruoho }
148 1.1.1.4 christos else if (ACPI_STRCMP (argv[AcpiGbl_Optind], "--") == 0)
149 1.1 jruoho {
150 1.1 jruoho AcpiGbl_Optind++;
151 1.1.1.4 christos return (ACPI_OPT_END);
152 1.1 jruoho }
153 1.1 jruoho }
154 1.1 jruoho
155 1.1 jruoho /* Get the option */
156 1.1 jruoho
157 1.1.1.2 jruoho CurrentChar = argv[AcpiGbl_Optind][CurrentCharPtr];
158 1.1 jruoho
159 1.1 jruoho /* Make sure that the option is legal */
160 1.1 jruoho
161 1.1 jruoho if (CurrentChar == ':' ||
162 1.1.1.4 christos (OptsPtr = ACPI_STRCHR (opts, CurrentChar)) == NULL)
163 1.1 jruoho {
164 1.1.1.3 christos ACPI_OPTION_ERROR ("Illegal option: -", CurrentChar);
165 1.1 jruoho
166 1.1 jruoho if (argv[AcpiGbl_Optind][++CurrentCharPtr] == '\0')
167 1.1 jruoho {
168 1.1 jruoho AcpiGbl_Optind++;
169 1.1 jruoho CurrentCharPtr = 1;
170 1.1 jruoho }
171 1.1 jruoho
172 1.1 jruoho return ('?');
173 1.1 jruoho }
174 1.1 jruoho
175 1.1 jruoho /* Option requires an argument? */
176 1.1 jruoho
177 1.1 jruoho if (*++OptsPtr == ':')
178 1.1 jruoho {
179 1.1 jruoho if (argv[AcpiGbl_Optind][(int) (CurrentCharPtr+1)] != '\0')
180 1.1 jruoho {
181 1.1 jruoho AcpiGbl_Optarg = &argv[AcpiGbl_Optind++][(int) (CurrentCharPtr+1)];
182 1.1 jruoho }
183 1.1 jruoho else if (++AcpiGbl_Optind >= argc)
184 1.1 jruoho {
185 1.1.1.3 christos ACPI_OPTION_ERROR ("Option requires an argument: -", CurrentChar);
186 1.1 jruoho
187 1.1 jruoho CurrentCharPtr = 1;
188 1.1 jruoho return ('?');
189 1.1 jruoho }
190 1.1 jruoho else
191 1.1 jruoho {
192 1.1 jruoho AcpiGbl_Optarg = argv[AcpiGbl_Optind++];
193 1.1 jruoho }
194 1.1 jruoho
195 1.1 jruoho CurrentCharPtr = 1;
196 1.1 jruoho }
197 1.1 jruoho
198 1.1.1.3 christos /* Option has an optional argument? */
199 1.1.1.3 christos
200 1.1.1.3 christos else if (*OptsPtr == '+')
201 1.1.1.3 christos {
202 1.1.1.3 christos if (argv[AcpiGbl_Optind][(int) (CurrentCharPtr+1)] != '\0')
203 1.1.1.3 christos {
204 1.1.1.3 christos AcpiGbl_Optarg = &argv[AcpiGbl_Optind++][(int) (CurrentCharPtr+1)];
205 1.1.1.3 christos }
206 1.1.1.3 christos else if (++AcpiGbl_Optind >= argc)
207 1.1.1.3 christos {
208 1.1.1.3 christos AcpiGbl_Optarg = NULL;
209 1.1.1.3 christos }
210 1.1.1.3 christos else
211 1.1.1.3 christos {
212 1.1.1.3 christos AcpiGbl_Optarg = argv[AcpiGbl_Optind++];
213 1.1.1.3 christos }
214 1.1.1.3 christos
215 1.1.1.3 christos CurrentCharPtr = 1;
216 1.1.1.3 christos }
217 1.1.1.3 christos
218 1.1 jruoho /* Option has optional single-char arguments? */
219 1.1 jruoho
220 1.1 jruoho else if (*OptsPtr == '^')
221 1.1 jruoho {
222 1.1 jruoho if (argv[AcpiGbl_Optind][(int) (CurrentCharPtr+1)] != '\0')
223 1.1 jruoho {
224 1.1 jruoho AcpiGbl_Optarg = &argv[AcpiGbl_Optind][(int) (CurrentCharPtr+1)];
225 1.1 jruoho }
226 1.1 jruoho else
227 1.1 jruoho {
228 1.1 jruoho AcpiGbl_Optarg = "^";
229 1.1 jruoho }
230 1.1 jruoho
231 1.1.1.3 christos AcpiGbl_SubOptChar = AcpiGbl_Optarg[0];
232 1.1.1.3 christos AcpiGbl_Optind++;
233 1.1.1.3 christos CurrentCharPtr = 1;
234 1.1.1.3 christos }
235 1.1.1.3 christos
236 1.1.1.3 christos /* Option has a required single-char argument? */
237 1.1.1.3 christos
238 1.1.1.3 christos else if (*OptsPtr == '|')
239 1.1.1.3 christos {
240 1.1.1.3 christos if (argv[AcpiGbl_Optind][(int) (CurrentCharPtr+1)] != '\0')
241 1.1.1.3 christos {
242 1.1.1.3 christos AcpiGbl_Optarg = &argv[AcpiGbl_Optind][(int) (CurrentCharPtr+1)];
243 1.1.1.3 christos }
244 1.1.1.3 christos else
245 1.1.1.3 christos {
246 1.1.1.3 christos ACPI_OPTION_ERROR ("Option requires a single-character suboption: -", CurrentChar);
247 1.1.1.3 christos
248 1.1.1.3 christos CurrentCharPtr = 1;
249 1.1.1.3 christos return ('?');
250 1.1.1.3 christos }
251 1.1.1.3 christos
252 1.1.1.3 christos AcpiGbl_SubOptChar = AcpiGbl_Optarg[0];
253 1.1 jruoho AcpiGbl_Optind++;
254 1.1 jruoho CurrentCharPtr = 1;
255 1.1 jruoho }
256 1.1 jruoho
257 1.1 jruoho /* Option with no arguments */
258 1.1 jruoho
259 1.1 jruoho else
260 1.1 jruoho {
261 1.1 jruoho if (argv[AcpiGbl_Optind][++CurrentCharPtr] == '\0')
262 1.1 jruoho {
263 1.1 jruoho CurrentCharPtr = 1;
264 1.1 jruoho AcpiGbl_Optind++;
265 1.1 jruoho }
266 1.1 jruoho
267 1.1 jruoho AcpiGbl_Optarg = NULL;
268 1.1 jruoho }
269 1.1 jruoho
270 1.1 jruoho return (CurrentChar);
271 1.1 jruoho }
272