1 /******************************************************************************
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2014, Intel Corp.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions, and the following disclaimer,
16 * without modification.
17 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18 * substantially similar to the "NO WARRANTY" disclaimer below
19 * ("Disclaimer") and any redistribution must be conditioned upon
20 * including a substantially similar Disclaimer requirement for further
21 * binary redistribution.
22 * 3. Neither the names of the above-listed copyright holders nor the names
23 * of any contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
26 * Alternatively, this software may be distributed under the terms of the
27 * GNU General Public License ("GPL") version 2 as published by the Free
28 * Software Foundation.
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 * POSSIBILITY OF SUCH DAMAGES.
45 * ACPICA getopt() implementation
48 * "f" - Option has no arguments
49 * "f:" - Option requires an argument
50 * "f^" - Option has optional single-char sub-options
51 * "f|" - Option has required single-char sub-options
58 #define ACPI_OPTION_ERROR(msg, badchar) \
59 if (AcpiGbl_Opterr) {AcpiLogError ("%s%c\n", msg, badchar);}
62 int AcpiGbl_Opterr = 1;
63 int AcpiGbl_Optind = 1;
64 int AcpiGbl_SubOptChar = 0;
67 static int CurrentCharPtr = 1;
70 /*******************************************************************************
72 * FUNCTION: AcpiGetoptArgument
74 * PARAMETERS: argc, argv - from main
76 * RETURN: 0 if an argument was found, -1 otherwise. Sets AcpiGbl_Optarg
77 * to point to the next argument.
79 * DESCRIPTION: Get the next argument. Used to obtain arguments for the
80 * two-character options after the original call to AcpiGetopt.
81 * Note: Either the argument starts at the next character after
82 * the option, or it is pointed to by the next argv entry.
83 * (After call to AcpiGetopt, we need to backup to the previous
86 ******************************************************************************/
96 if (argv[AcpiGbl_Optind][(int) (CurrentCharPtr+1)] != '\0')
98 AcpiGbl_Optarg = &argv[AcpiGbl_Optind++][(int) (CurrentCharPtr+1)];
100 else if (++AcpiGbl_Optind >= argc)
102 ACPI_OPTION_ERROR ("Option requires an argument: -", 'v');
109 AcpiGbl_Optarg = argv[AcpiGbl_Optind++];
117 /*******************************************************************************
119 * FUNCTION: AcpiGetopt
121 * PARAMETERS: argc, argv - from main
122 * opts - options info list
124 * RETURN: Option character or ACPI_OPT_END
126 * DESCRIPTION: Get the next option
128 ******************************************************************************/
140 if (CurrentCharPtr == 1)
142 if (AcpiGbl_Optind >= argc ||
143 argv[AcpiGbl_Optind][0] != '-' ||
144 argv[AcpiGbl_Optind][1] == '\0')
146 return (ACPI_OPT_END);
148 else if (ACPI_STRCMP (argv[AcpiGbl_Optind], "--") == 0)
151 return (ACPI_OPT_END);
157 CurrentChar = argv[AcpiGbl_Optind][CurrentCharPtr];
159 /* Make sure that the option is legal */
161 if (CurrentChar == ':' ||
162 (OptsPtr = ACPI_STRCHR (opts, CurrentChar)) == NULL)
164 ACPI_OPTION_ERROR ("Illegal option: -", CurrentChar);
166 if (argv[AcpiGbl_Optind][++CurrentCharPtr] == '\0')
175 /* Option requires an argument? */
177 if (*++OptsPtr == ':')
179 if (argv[AcpiGbl_Optind][(int) (CurrentCharPtr+1)] != '\0')
181 AcpiGbl_Optarg = &argv[AcpiGbl_Optind++][(int) (CurrentCharPtr+1)];
183 else if (++AcpiGbl_Optind >= argc)
185 ACPI_OPTION_ERROR ("Option requires an argument: -", CurrentChar);
192 AcpiGbl_Optarg = argv[AcpiGbl_Optind++];
198 /* Option has an optional argument? */
200 else if (*OptsPtr == '+')
202 if (argv[AcpiGbl_Optind][(int) (CurrentCharPtr+1)] != '\0')
204 AcpiGbl_Optarg = &argv[AcpiGbl_Optind++][(int) (CurrentCharPtr+1)];
206 else if (++AcpiGbl_Optind >= argc)
208 AcpiGbl_Optarg = NULL;
212 AcpiGbl_Optarg = argv[AcpiGbl_Optind++];
218 /* Option has optional single-char arguments? */
220 else if (*OptsPtr == '^')
222 if (argv[AcpiGbl_Optind][(int) (CurrentCharPtr+1)] != '\0')
224 AcpiGbl_Optarg = &argv[AcpiGbl_Optind][(int) (CurrentCharPtr+1)];
228 AcpiGbl_Optarg = "^";
231 AcpiGbl_SubOptChar = AcpiGbl_Optarg[0];
236 /* Option has a required single-char argument? */
238 else if (*OptsPtr == '|')
240 if (argv[AcpiGbl_Optind][(int) (CurrentCharPtr+1)] != '\0')
242 AcpiGbl_Optarg = &argv[AcpiGbl_Optind][(int) (CurrentCharPtr+1)];
246 ACPI_OPTION_ERROR ("Option requires a single-character suboption: -", CurrentChar);
252 AcpiGbl_SubOptChar = AcpiGbl_Optarg[0];
257 /* Option with no arguments */
261 if (argv[AcpiGbl_Optind][++CurrentCharPtr] == '\0')
267 AcpiGbl_Optarg = NULL;
270 return (CurrentChar);