1 /******************************************************************************
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2015, 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
54 #include <acpi/acpi.h>
58 #define ACPI_OPTION_ERROR(msg, badchar) \
59 if (acpi_gbl_opterr) {acpi_log_error ("%s%c\n", msg, badchar);}
61 int acpi_gbl_opterr = 1;
62 int acpi_gbl_optind = 1;
63 int acpi_gbl_sub_opt_char = 0;
64 char *acpi_gbl_optarg;
66 static int current_char_ptr = 1;
68 /*******************************************************************************
70 * FUNCTION: acpi_getopt_argument
72 * PARAMETERS: argc, argv - from main
74 * RETURN: 0 if an argument was found, -1 otherwise. Sets acpi_gbl_Optarg
75 * to point to the next argument.
77 * DESCRIPTION: Get the next argument. Used to obtain arguments for the
78 * two-character options after the original call to acpi_getopt.
79 * Note: Either the argument starts at the next character after
80 * the option, or it is pointed to by the next argv entry.
81 * (After call to acpi_getopt, we need to backup to the previous
84 ******************************************************************************/
86 int acpi_getopt_argument(int argc, char **argv)
92 if (argv[acpi_gbl_optind][(int)(current_char_ptr + 1)] != '\0') {
94 &argv[acpi_gbl_optind++][(int)(current_char_ptr + 1)];
95 } else if (++acpi_gbl_optind >= argc) {
96 ACPI_OPTION_ERROR("Option requires an argument: -", 'v');
101 acpi_gbl_optarg = argv[acpi_gbl_optind++];
104 current_char_ptr = 1;
108 /*******************************************************************************
110 * FUNCTION: acpi_getopt
112 * PARAMETERS: argc, argv - from main
113 * opts - options info list
115 * RETURN: Option character or ACPI_OPT_END
117 * DESCRIPTION: Get the next option
119 ******************************************************************************/
121 int acpi_getopt(int argc, char **argv, char *opts)
126 if (current_char_ptr == 1) {
127 if (acpi_gbl_optind >= argc ||
128 argv[acpi_gbl_optind][0] != '-' ||
129 argv[acpi_gbl_optind][1] == '\0') {
130 return (ACPI_OPT_END);
131 } else if (strcmp(argv[acpi_gbl_optind], "--") == 0) {
133 return (ACPI_OPT_END);
139 current_char = argv[acpi_gbl_optind][current_char_ptr];
141 /* Make sure that the option is legal */
143 if (current_char == ':' ||
144 (opts_ptr = strchr(opts, current_char)) == NULL) {
145 ACPI_OPTION_ERROR("Illegal option: -", current_char);
147 if (argv[acpi_gbl_optind][++current_char_ptr] == '\0') {
149 current_char_ptr = 1;
155 /* Option requires an argument? */
157 if (*++opts_ptr == ':') {
158 if (argv[acpi_gbl_optind][(int)(current_char_ptr + 1)] != '\0') {
160 &argv[acpi_gbl_optind++][(int)
161 (current_char_ptr + 1)];
162 } else if (++acpi_gbl_optind >= argc) {
163 ACPI_OPTION_ERROR("Option requires an argument: -",
166 current_char_ptr = 1;
169 acpi_gbl_optarg = argv[acpi_gbl_optind++];
172 current_char_ptr = 1;
175 /* Option has an optional argument? */
177 else if (*opts_ptr == '+') {
178 if (argv[acpi_gbl_optind][(int)(current_char_ptr + 1)] != '\0') {
180 &argv[acpi_gbl_optind++][(int)
181 (current_char_ptr + 1)];
182 } else if (++acpi_gbl_optind >= argc) {
183 acpi_gbl_optarg = NULL;
185 acpi_gbl_optarg = argv[acpi_gbl_optind++];
188 current_char_ptr = 1;
191 /* Option has optional single-char arguments? */
193 else if (*opts_ptr == '^') {
194 if (argv[acpi_gbl_optind][(int)(current_char_ptr + 1)] != '\0') {
196 &argv[acpi_gbl_optind][(int)(current_char_ptr + 1)];
198 acpi_gbl_optarg = "^";
201 acpi_gbl_sub_opt_char = acpi_gbl_optarg[0];
203 current_char_ptr = 1;
206 /* Option has a required single-char argument? */
208 else if (*opts_ptr == '|') {
209 if (argv[acpi_gbl_optind][(int)(current_char_ptr + 1)] != '\0') {
211 &argv[acpi_gbl_optind][(int)(current_char_ptr + 1)];
214 ("Option requires a single-character suboption: -",
217 current_char_ptr = 1;
221 acpi_gbl_sub_opt_char = acpi_gbl_optarg[0];
223 current_char_ptr = 1;
226 /* Option with no arguments */
229 if (argv[acpi_gbl_optind][++current_char_ptr] == '\0') {
230 current_char_ptr = 1;
234 acpi_gbl_optarg = NULL;
237 return (current_char);