Merge tag 'block-5.11-2021-01-16' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / tools / power / acpi / common / getopt.c
blobc3708f30ab3a3aa19ebc4b67f2e5f32de9e4e295
1 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
2 /******************************************************************************
4 * Module Name: getopt
6 * Copyright (C) 2000 - 2020, Intel Corp.
8 *****************************************************************************/
11 * ACPICA getopt() implementation
13 * Option strings:
14 * "f" - Option has no arguments
15 * "f:" - Option requires an argument
16 * "f+" - Option has an optional argument
17 * "f^" - Option has optional single-char sub-options
18 * "f|" - Option has required single-char sub-options
21 #include <acpi/acpi.h>
22 #include "accommon.h"
23 #include "acapps.h"
25 #define ACPI_OPTION_ERROR(msg, badchar) \
26 if (acpi_gbl_opterr) {fprintf (stderr, "%s%c\n", msg, badchar);}
28 int acpi_gbl_opterr = 1;
29 int acpi_gbl_optind = 1;
30 int acpi_gbl_sub_opt_char = 0;
31 char *acpi_gbl_optarg;
33 static int current_char_ptr = 1;
35 /*******************************************************************************
37 * FUNCTION: acpi_getopt_argument
39 * PARAMETERS: argc, argv - from main
41 * RETURN: 0 if an argument was found, -1 otherwise. Sets acpi_gbl_Optarg
42 * to point to the next argument.
44 * DESCRIPTION: Get the next argument. Used to obtain arguments for the
45 * two-character options after the original call to acpi_getopt.
46 * Note: Either the argument starts at the next character after
47 * the option, or it is pointed to by the next argv entry.
48 * (After call to acpi_getopt, we need to backup to the previous
49 * argv entry).
51 ******************************************************************************/
53 int acpi_getopt_argument(int argc, char **argv)
56 acpi_gbl_optind--;
57 current_char_ptr++;
59 if (argv[acpi_gbl_optind][(int)(current_char_ptr + 1)] != '\0') {
60 acpi_gbl_optarg =
61 &argv[acpi_gbl_optind++][(int)(current_char_ptr + 1)];
62 } else if (++acpi_gbl_optind >= argc) {
63 ACPI_OPTION_ERROR("\nOption requires an argument", 0);
65 current_char_ptr = 1;
66 return (-1);
67 } else {
68 acpi_gbl_optarg = argv[acpi_gbl_optind++];
71 current_char_ptr = 1;
72 return (0);
75 /*******************************************************************************
77 * FUNCTION: acpi_getopt
79 * PARAMETERS: argc, argv - from main
80 * opts - options info list
82 * RETURN: Option character or ACPI_OPT_END
84 * DESCRIPTION: Get the next option
86 ******************************************************************************/
88 int acpi_getopt(int argc, char **argv, char *opts)
90 int current_char;
91 char *opts_ptr;
93 if (current_char_ptr == 1) {
94 if (acpi_gbl_optind >= argc ||
95 argv[acpi_gbl_optind][0] != '-' ||
96 argv[acpi_gbl_optind][1] == '\0') {
97 return (ACPI_OPT_END);
98 } else if (strcmp(argv[acpi_gbl_optind], "--") == 0) {
99 acpi_gbl_optind++;
100 return (ACPI_OPT_END);
104 /* Get the option */
106 current_char = argv[acpi_gbl_optind][current_char_ptr];
108 /* Make sure that the option is legal */
110 if (current_char == ':' ||
111 (opts_ptr = strchr(opts, current_char)) == NULL) {
112 ACPI_OPTION_ERROR("Illegal option: -", current_char);
114 if (argv[acpi_gbl_optind][++current_char_ptr] == '\0') {
115 acpi_gbl_optind++;
116 current_char_ptr = 1;
119 return ('?');
122 /* Option requires an argument? */
124 if (*++opts_ptr == ':') {
125 if (argv[acpi_gbl_optind][(int)(current_char_ptr + 1)] != '\0') {
126 acpi_gbl_optarg =
127 &argv[acpi_gbl_optind++][(int)
128 (current_char_ptr + 1)];
129 } else if (++acpi_gbl_optind >= argc) {
130 ACPI_OPTION_ERROR("Option requires an argument: -",
131 current_char);
133 current_char_ptr = 1;
134 return ('?');
135 } else {
136 acpi_gbl_optarg = argv[acpi_gbl_optind++];
139 current_char_ptr = 1;
142 /* Option has an optional argument? */
144 else if (*opts_ptr == '+') {
145 if (argv[acpi_gbl_optind][(int)(current_char_ptr + 1)] != '\0') {
146 acpi_gbl_optarg =
147 &argv[acpi_gbl_optind++][(int)
148 (current_char_ptr + 1)];
149 } else if (++acpi_gbl_optind >= argc) {
150 acpi_gbl_optarg = NULL;
151 } else {
152 acpi_gbl_optarg = argv[acpi_gbl_optind++];
155 current_char_ptr = 1;
158 /* Option has optional single-char arguments? */
160 else if (*opts_ptr == '^') {
161 if (argv[acpi_gbl_optind][(int)(current_char_ptr + 1)] != '\0') {
162 acpi_gbl_optarg =
163 &argv[acpi_gbl_optind][(int)(current_char_ptr + 1)];
164 } else {
165 acpi_gbl_optarg = "^";
168 acpi_gbl_sub_opt_char = acpi_gbl_optarg[0];
169 acpi_gbl_optind++;
170 current_char_ptr = 1;
173 /* Option has a required single-char argument? */
175 else if (*opts_ptr == '|') {
176 if (argv[acpi_gbl_optind][(int)(current_char_ptr + 1)] != '\0') {
177 acpi_gbl_optarg =
178 &argv[acpi_gbl_optind][(int)(current_char_ptr + 1)];
179 } else {
180 ACPI_OPTION_ERROR
181 ("Option requires a single-character suboption: -",
182 current_char);
184 current_char_ptr = 1;
185 return ('?');
188 acpi_gbl_sub_opt_char = acpi_gbl_optarg[0];
189 acpi_gbl_optind++;
190 current_char_ptr = 1;
193 /* Option with no arguments */
195 else {
196 if (argv[acpi_gbl_optind][++current_char_ptr] == '\0') {
197 current_char_ptr = 1;
198 acpi_gbl_optind++;
201 acpi_gbl_optarg = NULL;
204 return (current_char);