getopt_long.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. * Copyright (c) 1987, 1993, 1994, 1996
  3. * The Regents of the University of California. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. All advertising materials mentioning features or use of this software
  14. * must display the following acknowledgement:
  15. * This product includes software developed by the University of
  16. * California, Berkeley and its contributors.
  17. * 4. Neither the name of the University nor the names of its contributors
  18. * may be used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31. * SUCH DAMAGE.
  32. */
  33. #include <assert.h>
  34. #include <errno.h>
  35. #include <stdio.h>
  36. #include <stdlib.h>
  37. #include <string.h>
  38. #include "getopt.h"
  39. #define lws_ptr_diff(head, tail) \
  40. ((int)((char *)(head) - (char *)(tail)))
  41. extern int opterr; /* if error message should be printed */
  42. extern int optind; /* index into parent argv vector */
  43. extern int optopt; /* character checked for validity */
  44. extern int optreset; /* reset getopt */
  45. extern char *optarg; /* argument associated with option */
  46. #define __P(x) x
  47. #define _DIAGASSERT(x) assert(x)
  48. static char * __progname __P((char *));
  49. int getopt_internal __P((int, char * const *, const char *));
  50. static char *
  51. __progname(nargv0)
  52. char * nargv0;
  53. {
  54. char * tmp;
  55. _DIAGASSERT(nargv0 != NULL);
  56. tmp = strrchr(nargv0, '/');
  57. if (tmp)
  58. tmp++;
  59. else
  60. tmp = nargv0;
  61. return(tmp);
  62. }
  63. #define BADCH (int)'?'
  64. #define BADARG (int)':'
  65. #define EMSG ""
  66. /*
  67. * getopt --
  68. * Parse argc/argv argument vector.
  69. */
  70. int
  71. getopt_internal(nargc, nargv, ostr)
  72. int nargc;
  73. char * const *nargv;
  74. const char *ostr;
  75. {
  76. static char *place = EMSG; /* option letter processing */
  77. char *oli; /* option letter list index */
  78. _DIAGASSERT(nargv != NULL);
  79. _DIAGASSERT(ostr != NULL);
  80. if (optreset || !*place) { /* update scanning pointer */
  81. optreset = 0;
  82. if (optind >= nargc || *(place = nargv[optind]) != '-') {
  83. place = EMSG;
  84. return (-1);
  85. }
  86. if (place[1] && *++place == '-') { /* found "--" */
  87. /* ++optind; */
  88. place = EMSG;
  89. return (-2);
  90. }
  91. } /* option letter okay? */
  92. if ((optopt = (int)*place++) == (int)':' ||
  93. !(oli = strchr(ostr, optopt))) {
  94. /*
  95. * if the user didn't specify '-' as an option,
  96. * assume it means -1.
  97. */
  98. if (optopt == (int)'-')
  99. return (-1);
  100. if (!*place)
  101. ++optind;
  102. if (opterr && *ostr != ':')
  103. (void)fprintf(stderr,
  104. "%s: illegal option -- %c\n", __progname(nargv[0]), optopt);
  105. return (BADCH);
  106. }
  107. if (*++oli != ':') { /* don't need argument */
  108. optarg = NULL;
  109. if (!*place)
  110. ++optind;
  111. } else { /* need an argument */
  112. if (*place) /* no white space */
  113. optarg = place;
  114. else if (nargc <= ++optind) { /* no arg */
  115. place = EMSG;
  116. if ((opterr) && (*ostr != ':'))
  117. (void)fprintf(stderr,
  118. "%s: option requires an argument -- %c\n",
  119. __progname(nargv[0]), optopt);
  120. return (BADARG);
  121. } else /* white space */
  122. optarg = nargv[optind];
  123. place = EMSG;
  124. ++optind;
  125. }
  126. return (optopt); /* dump back option letter */
  127. }
  128. #if 0
  129. /*
  130. * getopt --
  131. * Parse argc/argv argument vector.
  132. */
  133. int
  134. getopt2(nargc, nargv, ostr)
  135. int nargc;
  136. char * const *nargv;
  137. const char *ostr;
  138. {
  139. int retval;
  140. if ((retval = getopt_internal(nargc, nargv, ostr)) == -2) {
  141. retval = -1;
  142. ++optind;
  143. }
  144. return(retval);
  145. }
  146. #endif
  147. /*
  148. * getopt_long --
  149. * Parse argc/argv argument vector.
  150. */
  151. int
  152. getopt_long(nargc, nargv, options, long_options, index)
  153. int nargc;
  154. char ** nargv;
  155. char * options;
  156. struct option * long_options;
  157. int * index;
  158. {
  159. int retval;
  160. _DIAGASSERT(nargv != NULL);
  161. _DIAGASSERT(options != NULL);
  162. _DIAGASSERT(long_options != NULL);
  163. /* index may be NULL */
  164. if ((retval = getopt_internal(nargc, nargv, options)) == -2) {
  165. char *current_argv = nargv[optind++] + 2, *has_equal;
  166. int i, current_argv_len, match = -1;
  167. if (*current_argv == '\0') {
  168. return(-1);
  169. }
  170. if ((has_equal = strchr(current_argv, '=')) != NULL) {
  171. current_argv_len = lws_ptr_diff(has_equal, current_argv);
  172. has_equal++;
  173. } else
  174. current_argv_len = (int)strlen(current_argv);
  175. for (i = 0; long_options[i].name; i++) {
  176. if (strncmp(current_argv, long_options[i].name, current_argv_len))
  177. continue;
  178. if (strlen(long_options[i].name) == (unsigned)current_argv_len) {
  179. match = i;
  180. break;
  181. }
  182. if (match == -1)
  183. match = i;
  184. }
  185. if (match != -1) {
  186. if (long_options[match].has_arg == required_argument ||
  187. long_options[match].has_arg == optional_argument) {
  188. if (has_equal)
  189. optarg = has_equal;
  190. else
  191. optarg = nargv[optind++];
  192. }
  193. if ((long_options[match].has_arg == required_argument)
  194. && (optarg == NULL)) {
  195. /*
  196. * Missing argument, leading :
  197. * indicates no error should be generated
  198. */
  199. if ((opterr) && (*options != ':'))
  200. (void)fprintf(stderr,
  201. "%s: option requires an argument -- %s\n",
  202. __progname(nargv[0]), current_argv);
  203. return (BADARG);
  204. }
  205. } else { /* No matching argument */
  206. if ((opterr) && (*options != ':'))
  207. (void)fprintf(stderr,
  208. "%s: illegal option -- %s\n", __progname(nargv[0]), current_argv);
  209. return (BADCH);
  210. }
  211. if (long_options[match].flag) {
  212. *long_options[match].flag = long_options[match].val;
  213. retval = 0;
  214. } else
  215. retval = long_options[match].val;
  216. if (index)
  217. *index = match;
  218. }
  219. return(retval);
  220. }