getopt.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /******************************************************************************
  2. *
  3. * Module Name: getopt
  4. *
  5. *****************************************************************************/
  6. /*
  7. * Copyright (C) 2000 - 2014, 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 <stdio.h>
  52. #include <string.h>
  53. #include <acpi/acpi.h>
  54. #include "accommon.h"
  55. #include "acapps.h"
  56. #define ACPI_OPTION_ERROR(msg, badchar) \
  57. if (acpi_gbl_opterr) {fprintf (stderr, "%s%c\n", msg, badchar);}
  58. int acpi_gbl_opterr = 1;
  59. int acpi_gbl_optind = 1;
  60. int acpi_gbl_sub_opt_char = 0;
  61. char *acpi_gbl_optarg;
  62. static int current_char_ptr = 1;
  63. /*******************************************************************************
  64. *
  65. * FUNCTION: acpi_getopt_argument
  66. *
  67. * PARAMETERS: argc, argv - from main
  68. *
  69. * RETURN: 0 if an argument was found, -1 otherwise. Sets acpi_gbl_Optarg
  70. * to point to the next argument.
  71. *
  72. * DESCRIPTION: Get the next argument. Used to obtain arguments for the
  73. * two-character options after the original call to acpi_getopt.
  74. * Note: Either the argument starts at the next character after
  75. * the option, or it is pointed to by the next argv entry.
  76. * (After call to acpi_getopt, we need to backup to the previous
  77. * argv entry).
  78. *
  79. ******************************************************************************/
  80. int acpi_getopt_argument(int argc, char **argv)
  81. {
  82. acpi_gbl_optind--;
  83. current_char_ptr++;
  84. if (argv[acpi_gbl_optind][(int)(current_char_ptr + 1)] != '\0') {
  85. acpi_gbl_optarg =
  86. &argv[acpi_gbl_optind++][(int)(current_char_ptr + 1)];
  87. } else if (++acpi_gbl_optind >= argc) {
  88. ACPI_OPTION_ERROR("Option requires an argument: -", 'v');
  89. current_char_ptr = 1;
  90. return (-1);
  91. } else {
  92. acpi_gbl_optarg = argv[acpi_gbl_optind++];
  93. }
  94. current_char_ptr = 1;
  95. return (0);
  96. }
  97. /*******************************************************************************
  98. *
  99. * FUNCTION: acpi_getopt
  100. *
  101. * PARAMETERS: argc, argv - from main
  102. * opts - options info list
  103. *
  104. * RETURN: Option character or EOF
  105. *
  106. * DESCRIPTION: Get the next option
  107. *
  108. ******************************************************************************/
  109. int acpi_getopt(int argc, char **argv, char *opts)
  110. {
  111. int current_char;
  112. char *opts_ptr;
  113. if (current_char_ptr == 1) {
  114. if (acpi_gbl_optind >= argc ||
  115. argv[acpi_gbl_optind][0] != '-' ||
  116. argv[acpi_gbl_optind][1] == '\0') {
  117. return (EOF);
  118. } else if (strcmp(argv[acpi_gbl_optind], "--") == 0) {
  119. acpi_gbl_optind++;
  120. return (EOF);
  121. }
  122. }
  123. /* Get the option */
  124. current_char = argv[acpi_gbl_optind][current_char_ptr];
  125. /* Make sure that the option is legal */
  126. if (current_char == ':' ||
  127. (opts_ptr = strchr(opts, current_char)) == NULL) {
  128. ACPI_OPTION_ERROR("Illegal option: -", current_char);
  129. if (argv[acpi_gbl_optind][++current_char_ptr] == '\0') {
  130. acpi_gbl_optind++;
  131. current_char_ptr = 1;
  132. }
  133. return ('?');
  134. }
  135. /* Option requires an argument? */
  136. if (*++opts_ptr == ':') {
  137. if (argv[acpi_gbl_optind][(int)(current_char_ptr + 1)] != '\0') {
  138. acpi_gbl_optarg =
  139. &argv[acpi_gbl_optind++][(int)
  140. (current_char_ptr + 1)];
  141. } else if (++acpi_gbl_optind >= argc) {
  142. ACPI_OPTION_ERROR("Option requires an argument: -",
  143. current_char);
  144. current_char_ptr = 1;
  145. return ('?');
  146. } else {
  147. acpi_gbl_optarg = argv[acpi_gbl_optind++];
  148. }
  149. current_char_ptr = 1;
  150. }
  151. /* Option has an optional argument? */
  152. else if (*opts_ptr == '+') {
  153. if (argv[acpi_gbl_optind][(int)(current_char_ptr + 1)] != '\0') {
  154. acpi_gbl_optarg =
  155. &argv[acpi_gbl_optind++][(int)
  156. (current_char_ptr + 1)];
  157. } else if (++acpi_gbl_optind >= argc) {
  158. acpi_gbl_optarg = NULL;
  159. } else {
  160. acpi_gbl_optarg = argv[acpi_gbl_optind++];
  161. }
  162. current_char_ptr = 1;
  163. }
  164. /* Option has optional single-char arguments? */
  165. else if (*opts_ptr == '^') {
  166. if (argv[acpi_gbl_optind][(int)(current_char_ptr + 1)] != '\0') {
  167. acpi_gbl_optarg =
  168. &argv[acpi_gbl_optind][(int)(current_char_ptr + 1)];
  169. } else {
  170. acpi_gbl_optarg = "^";
  171. }
  172. acpi_gbl_sub_opt_char = acpi_gbl_optarg[0];
  173. acpi_gbl_optind++;
  174. current_char_ptr = 1;
  175. }
  176. /* Option has a required single-char argument? */
  177. else if (*opts_ptr == '|') {
  178. if (argv[acpi_gbl_optind][(int)(current_char_ptr + 1)] != '\0') {
  179. acpi_gbl_optarg =
  180. &argv[acpi_gbl_optind][(int)(current_char_ptr + 1)];
  181. } else {
  182. ACPI_OPTION_ERROR
  183. ("Option requires a single-character suboption: -",
  184. current_char);
  185. current_char_ptr = 1;
  186. return ('?');
  187. }
  188. acpi_gbl_sub_opt_char = acpi_gbl_optarg[0];
  189. acpi_gbl_optind++;
  190. current_char_ptr = 1;
  191. }
  192. /* Option with no arguments */
  193. else {
  194. if (argv[acpi_gbl_optind][++current_char_ptr] == '\0') {
  195. current_char_ptr = 1;
  196. acpi_gbl_optind++;
  197. }
  198. acpi_gbl_optarg = NULL;
  199. }
  200. return (current_char);
  201. }