1 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
2 /******************************************************************************
6 * Copyright (C) 2000 - 2020, Intel Corp.
8 *****************************************************************************/
11 * ACPICA getopt() implementation
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>
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
51 ******************************************************************************/
53 int acpi_getopt_argument(int argc
, char **argv
)
59 if (argv
[acpi_gbl_optind
][(int)(current_char_ptr
+ 1)] != '\0') {
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);
68 acpi_gbl_optarg
= argv
[acpi_gbl_optind
++];
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
)
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) {
100 return (ACPI_OPT_END
);
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') {
116 current_char_ptr
= 1;
122 /* Option requires an argument? */
124 if (*++opts_ptr
== ':') {
125 if (argv
[acpi_gbl_optind
][(int)(current_char_ptr
+ 1)] != '\0') {
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: -",
133 current_char_ptr
= 1;
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') {
147 &argv
[acpi_gbl_optind
++][(int)
148 (current_char_ptr
+ 1)];
149 } else if (++acpi_gbl_optind
>= argc
) {
150 acpi_gbl_optarg
= NULL
;
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') {
163 &argv
[acpi_gbl_optind
][(int)(current_char_ptr
+ 1)];
165 acpi_gbl_optarg
= "^";
168 acpi_gbl_sub_opt_char
= acpi_gbl_optarg
[0];
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') {
178 &argv
[acpi_gbl_optind
][(int)(current_char_ptr
+ 1)];
181 ("Option requires a single-character suboption: -",
184 current_char_ptr
= 1;
188 acpi_gbl_sub_opt_char
= acpi_gbl_optarg
[0];
190 current_char_ptr
= 1;
193 /* Option with no arguments */
196 if (argv
[acpi_gbl_optind
][++current_char_ptr
] == '\0') {
197 current_char_ptr
= 1;
201 acpi_gbl_optarg
= NULL
;
204 return (current_char
);