getopt.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /******************************************************************************
  2. *
  3. * Module Name: getopt
  4. *
  5. *****************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2015, Intel Corp.
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions, and the following disclaimer,
  15. * without modification.
  16. * 2. Redistributions in binary form must reproduce at minimum a disclaimer
  17. * substantially similar to the "NO WARRANTY" disclaimer below
  18. * ("Disclaimer") and any redistribution must be conditioned upon
  19. * including a substantially similar Disclaimer requirement for further
  20. * binary redistribution.
  21. * 3. Neither the names of the above-listed copyright holders nor the names
  22. * of any contributors may be used to endorse or promote products derived
  23. * from this software without specific prior written permission.
  24. *
  25. * Alternatively, this software may be distributed under the terms of the
  26. * GNU General Public License ("GPL") version 2 as published by the Free
  27. * Software Foundation.
  28. *
  29. * NO WARRANTY
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  31. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  32. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
  33. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  34. * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  35. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  36. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  37. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  38. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  39. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  40. * POSSIBILITY OF SUCH DAMAGES.
  41. */
  42. /*
  43. * ACPICA getopt() implementation
  44. *
  45. * Option strings:
  46. * "f" - Option has no arguments
  47. * "f:" - Option requires an argument
  48. * "f^" - Option has optional single-char sub-options
  49. * "f|" - Option has required single-char sub-options
  50. */
  51. #include <acpi/acpi.h>
  52. #include "accommon.h"
  53. #include "acapps.h"
  54. #define ACPI_OPTION_ERROR(msg, badchar) \
  55. if (acpi_gbl_opterr) {acpi_log_error ("%s%c\n", msg, badchar);}
  56. int acpi_gbl_opterr = 1;
  57. int acpi_gbl_optind = 1;
  58. int acpi_gbl_sub_opt_char = 0;
  59. char *acpi_gbl_optarg;
  60. static int current_char_ptr = 1;
  61. /*******************************************************************************
  62. *
  63. * FUNCTION: acpi_getopt_argument
  64. *
  65. * PARAMETERS: argc, argv - from main
  66. *
  67. * RETURN: 0 if an argument was found, -1 otherwise. Sets acpi_gbl_Optarg
  68. * to point to the next argument.
  69. *
  70. * DESCRIPTION: Get the next argument. Used to obtain arguments for the
  71. * two-character options after the original call to acpi_getopt.
  72. * Note: Either the argument starts at the next character after
  73. * the option, or it is pointed to by the next argv entry.
  74. * (After call to acpi_getopt, we need to backup to the previous
  75. * argv entry).
  76. *
  77. ******************************************************************************/
  78. int acpi_getopt_argument(int argc, char **argv)
  79. {
  80. acpi_gbl_optind--;
  81. current_char_ptr++;
  82. if (argv[acpi_gbl_optind][(int)(current_char_ptr + 1)] != '\0') {
  83. acpi_gbl_optarg =
  84. &argv[acpi_gbl_optind++][(int)(current_char_ptr + 1)];
  85. } else if (++acpi_gbl_optind >= argc) {
  86. ACPI_OPTION_ERROR("Option requires an argument: -", 'v');
  87. current_char_ptr = 1;
  88. return (-1);
  89. } else {
  90. acpi_gbl_optarg = argv[acpi_gbl_optind++];
  91. }
  92. current_char_ptr = 1;
  93. return (0);
  94. }
  95. /*******************************************************************************
  96. *
  97. * FUNCTION: acpi_getopt
  98. *
  99. * PARAMETERS: argc, argv - from main
  100. * opts - options info list
  101. *
  102. * RETURN: Option character or ACPI_OPT_END
  103. *
  104. * DESCRIPTION: Get the next option
  105. *
  106. ******************************************************************************/
  107. int acpi_getopt(int argc, char **argv, char *opts)
  108. {
  109. int current_char;
  110. char *opts_ptr;
  111. if (current_char_ptr == 1) {
  112. if (acpi_gbl_optind >= argc ||
  113. argv[acpi_gbl_optind][0] != '-' ||
  114. argv[acpi_gbl_optind][1] == '\0') {
  115. return (ACPI_OPT_END);
  116. } else if (strcmp(argv[acpi_gbl_optind], "--") == 0) {
  117. acpi_gbl_optind++;
  118. return (ACPI_OPT_END);
  119. }
  120. }
  121. /* Get the option */
  122. current_char = argv[acpi_gbl_optind][current_char_ptr];
  123. /* Make sure that the option is legal */
  124. if (current_char == ':' ||
  125. (opts_ptr = strchr(opts, current_char)) == NULL) {
  126. ACPI_OPTION_ERROR("Illegal option: -", current_char);
  127. if (argv[acpi_gbl_optind][++current_char_ptr] == '\0') {
  128. acpi_gbl_optind++;
  129. current_char_ptr = 1;
  130. }
  131. return ('?');
  132. }
  133. /* Option requires an argument? */
  134. if (*++opts_ptr == ':') {
  135. if (argv[acpi_gbl_optind][(int)(current_char_ptr + 1)] != '\0') {
  136. acpi_gbl_optarg =
  137. &argv[acpi_gbl_optind++][(int)
  138. (current_char_ptr + 1)];
  139. } else if (++acpi_gbl_optind >= argc) {
  140. ACPI_OPTION_ERROR("Option requires an argument: -",
  141. current_char);
  142. current_char_ptr = 1;
  143. return ('?');
  144. } else {
  145. acpi_gbl_optarg = argv[acpi_gbl_optind++];
  146. }
  147. current_char_ptr = 1;
  148. }
  149. /* Option has an optional argument? */
  150. else if (*opts_ptr == '+') {
  151. if (argv[acpi_gbl_optind][(int)(current_char_ptr + 1)] != '\0') {
  152. acpi_gbl_optarg =
  153. &argv[acpi_gbl_optind++][(int)
  154. (current_char_ptr + 1)];
  155. } else if (++acpi_gbl_optind >= argc) {
  156. acpi_gbl_optarg = NULL;
  157. } else {
  158. acpi_gbl_optarg = argv[acpi_gbl_optind++];
  159. }
  160. current_char_ptr = 1;
  161. }
  162. /* Option has optional single-char arguments? */
  163. else if (*opts_ptr == '^') {
  164. if (argv[acpi_gbl_optind][(int)(current_char_ptr + 1)] != '\0') {
  165. acpi_gbl_optarg =
  166. &argv[acpi_gbl_optind][(int)(current_char_ptr + 1)];
  167. } else {
  168. acpi_gbl_optarg = "^";
  169. }
  170. acpi_gbl_sub_opt_char = acpi_gbl_optarg[0];
  171. acpi_gbl_optind++;
  172. current_char_ptr = 1;
  173. }
  174. /* 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);
  183. current_char_ptr = 1;
  184. return ('?');
  185. }
  186. acpi_gbl_sub_opt_char = acpi_gbl_optarg[0];
  187. acpi_gbl_optind++;
  188. current_char_ptr = 1;
  189. }
  190. /* Option with no arguments */
  191. else {
  192. if (argv[acpi_gbl_optind][++current_char_ptr] == '\0') {
  193. current_char_ptr = 1;
  194. acpi_gbl_optind++;
  195. }
  196. acpi_gbl_optarg = NULL;
  197. }
  198. return (current_char);
  199. }