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