1 /******************************************************************************
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2016, 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 an optional argument
51 * "f^" - Option has optional single-char sub-options
52 * "f|" - Option has required single-char sub-options
55 #include <acpi/acpi.h>
59 #define ACPI_OPTION_ERROR(msg, badchar) \
60 if (acpi_gbl_opterr) {fprintf (stderr, "%s%c\n", msg, badchar);}
62 int acpi_gbl_opterr = 1;
63 int acpi_gbl_optind = 1;
64 int acpi_gbl_sub_opt_char = 0;
65 char *acpi_gbl_optarg;
67 static int current_char_ptr = 1;
69 /*******************************************************************************
71 * FUNCTION: acpi_getopt_argument
73 * PARAMETERS: argc, argv - from main
75 * RETURN: 0 if an argument was found, -1 otherwise. Sets acpi_gbl_Optarg
76 * to point to the next argument.
78 * DESCRIPTION: Get the next argument. Used to obtain arguments for the
79 * two-character options after the original call to acpi_getopt.
80 * Note: Either the argument starts at the next character after
81 * the option, or it is pointed to by the next argv entry.
82 * (After call to acpi_getopt, we need to backup to the previous
85 ******************************************************************************/
87 int acpi_getopt_argument(int argc, char **argv)
93 if (argv[acpi_gbl_optind][(int)(current_char_ptr + 1)] != '\0') {
95 &argv[acpi_gbl_optind++][(int)(current_char_ptr + 1)];
96 } else if (++acpi_gbl_optind >= argc) {
97 ACPI_OPTION_ERROR("\nOption requires an argument", 0);
102 acpi_gbl_optarg = argv[acpi_gbl_optind++];
105 current_char_ptr = 1;
109 /*******************************************************************************
111 * FUNCTION: acpi_getopt
113 * PARAMETERS: argc, argv - from main
114 * opts - options info list
116 * RETURN: Option character or ACPI_OPT_END
118 * DESCRIPTION: Get the next option
120 ******************************************************************************/
122 int acpi_getopt(int argc, char **argv, char *opts)
127 if (current_char_ptr == 1) {
128 if (acpi_gbl_optind >= argc ||
129 argv[acpi_gbl_optind][0] != '-' ||
130 argv[acpi_gbl_optind][1] == '\0') {
131 return (ACPI_OPT_END);
132 } else if (strcmp(argv[acpi_gbl_optind], "--") == 0) {
134 return (ACPI_OPT_END);
140 current_char = argv[acpi_gbl_optind][current_char_ptr];
142 /* Make sure that the option is legal */
144 if (current_char == ':' ||
145 (opts_ptr = strchr(opts, current_char)) == NULL) {
146 ACPI_OPTION_ERROR("Illegal option: -", current_char);
148 if (argv[acpi_gbl_optind][++current_char_ptr] == '\0') {
150 current_char_ptr = 1;
156 /* Option requires an argument? */
158 if (*++opts_ptr == ':') {
159 if (argv[acpi_gbl_optind][(int)(current_char_ptr + 1)] != '\0') {
161 &argv[acpi_gbl_optind++][(int)
162 (current_char_ptr + 1)];
163 } else if (++acpi_gbl_optind >= argc) {
164 ACPI_OPTION_ERROR("Option requires an argument: -",
167 current_char_ptr = 1;
170 acpi_gbl_optarg = argv[acpi_gbl_optind++];
173 current_char_ptr = 1;
176 /* Option has an optional argument? */
178 else if (*opts_ptr == '+') {
179 if (argv[acpi_gbl_optind][(int)(current_char_ptr + 1)] != '\0') {
181 &argv[acpi_gbl_optind++][(int)
182 (current_char_ptr + 1)];
183 } else if (++acpi_gbl_optind >= argc) {
184 acpi_gbl_optarg = NULL;
186 acpi_gbl_optarg = argv[acpi_gbl_optind++];
189 current_char_ptr = 1;
192 /* Option has optional single-char arguments? */
194 else if (*opts_ptr == '^') {
195 if (argv[acpi_gbl_optind][(int)(current_char_ptr + 1)] != '\0') {
197 &argv[acpi_gbl_optind][(int)(current_char_ptr + 1)];
199 acpi_gbl_optarg = "^";
202 acpi_gbl_sub_opt_char = acpi_gbl_optarg[0];
204 current_char_ptr = 1;
207 /* Option has a required single-char argument? */
209 else if (*opts_ptr == '|') {
210 if (argv[acpi_gbl_optind][(int)(current_char_ptr + 1)] != '\0') {
212 &argv[acpi_gbl_optind][(int)(current_char_ptr + 1)];
215 ("Option requires a single-character suboption: -",
218 current_char_ptr = 1;
222 acpi_gbl_sub_opt_char = acpi_gbl_optarg[0];
224 current_char_ptr = 1;
227 /* Option with no arguments */
230 if (argv[acpi_gbl_optind][++current_char_ptr] == '\0') {
231 current_char_ptr = 1;
235 acpi_gbl_optarg = NULL;
238 return (current_char);