getopt_long.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. extern int opterr; /* if error message should be printed */
  40. extern int optind; /* index into parent argv vector */
  41. extern int optopt; /* character checked for validity */
  42. extern int optreset; /* reset getopt */
  43. extern char *optarg; /* argument associated with option */
  44. #define __P(x) x
  45. #define _DIAGASSERT(x) assert(x)
  46. static char * __progname __P((char *));
  47. int getopt_internal __P((int, char * const *, const char *));
  48. static char *
  49. __progname(nargv0)
  50. char * nargv0;
  51. {
  52. char * tmp;
  53. _DIAGASSERT(nargv0 != NULL);
  54. tmp = strrchr(nargv0, '/');
  55. if (tmp)
  56. tmp++;
  57. else
  58. tmp = nargv0;
  59. return(tmp);
  60. }
  61. #define BADCH (int)'?'
  62. #define BADARG (int)':'
  63. #define EMSG ""
  64. /*
  65. * getopt --
  66. * Parse argc/argv argument vector.
  67. */
  68. int
  69. getopt_internal(nargc, nargv, ostr)
  70. int nargc;
  71. char * const *nargv;
  72. const char *ostr;
  73. {
  74. static char *place = EMSG; /* option letter processing */
  75. char *oli; /* option letter list index */
  76. _DIAGASSERT(nargv != NULL);
  77. _DIAGASSERT(ostr != NULL);
  78. if (optreset || !*place) { /* update scanning pointer */
  79. optreset = 0;
  80. if (optind >= nargc || *(place = nargv[optind]) != '-') {
  81. place = EMSG;
  82. return (-1);
  83. }
  84. if (place[1] && *++place == '-') { /* found "--" */
  85. /* ++optind; */
  86. place = EMSG;
  87. return (-2);
  88. }
  89. } /* option letter okay? */
  90. if ((optopt = (int)*place++) == (int)':' ||
  91. !(oli = strchr(ostr, optopt))) {
  92. /*
  93. * if the user didn't specify '-' as an option,
  94. * assume it means -1.
  95. */
  96. if (optopt == (int)'-')
  97. return (-1);
  98. if (!*place)
  99. ++optind;
  100. if (opterr && *ostr != ':')
  101. (void)fprintf(stderr,
  102. "%s: illegal option -- %c\n", __progname(nargv[0]), optopt);
  103. return (BADCH);
  104. }
  105. if (*++oli != ':') { /* don't need argument */
  106. optarg = NULL;
  107. if (!*place)
  108. ++optind;
  109. } else { /* need an argument */
  110. if (*place) /* no white space */
  111. optarg = place;
  112. else if (nargc <= ++optind) { /* no arg */
  113. place = EMSG;
  114. if ((opterr) && (*ostr != ':'))
  115. (void)fprintf(stderr,
  116. "%s: option requires an argument -- %c\n",
  117. __progname(nargv[0]), optopt);
  118. return (BADARG);
  119. } else /* white space */
  120. optarg = nargv[optind];
  121. place = EMSG;
  122. ++optind;
  123. }
  124. return (optopt); /* dump back option letter */
  125. }
  126. #if 0
  127. /*
  128. * getopt --
  129. * Parse argc/argv argument vector.
  130. */
  131. int
  132. getopt2(nargc, nargv, ostr)
  133. int nargc;
  134. char * const *nargv;
  135. const char *ostr;
  136. {
  137. int retval;
  138. if ((retval = getopt_internal(nargc, nargv, ostr)) == -2) {
  139. retval = -1;
  140. ++optind;
  141. }
  142. return(retval);
  143. }
  144. #endif
  145. /*
  146. * getopt_long --
  147. * Parse argc/argv argument vector.
  148. */
  149. int
  150. getopt_long(nargc, nargv, options, long_options, index)
  151. int nargc;
  152. char ** nargv;
  153. char * options;
  154. struct option * long_options;
  155. int * index;
  156. {
  157. int retval;
  158. _DIAGASSERT(nargv != NULL);
  159. _DIAGASSERT(options != NULL);
  160. _DIAGASSERT(long_options != NULL);
  161. /* index may be NULL */
  162. if ((retval = getopt_internal(nargc, nargv, options)) == -2) {
  163. char *current_argv = nargv[optind++] + 2, *has_equal;
  164. int i, current_argv_len, match = -1;
  165. if (*current_argv == '\0') {
  166. return(-1);
  167. }
  168. if ((has_equal = strchr(current_argv, '=')) != NULL) {
  169. current_argv_len = has_equal - current_argv;
  170. has_equal++;
  171. } else
  172. current_argv_len = strlen(current_argv);
  173. for (i = 0; long_options[i].name; i++) {
  174. if (strncmp(current_argv, long_options[i].name, current_argv_len))
  175. continue;
  176. if (strlen(long_options[i].name) == (unsigned)current_argv_len) {
  177. match = i;
  178. break;
  179. }
  180. if (match == -1)
  181. match = i;
  182. }
  183. if (match != -1) {
  184. if (long_options[match].has_arg == required_argument ||
  185. long_options[match].has_arg == optional_argument) {
  186. if (has_equal)
  187. optarg = has_equal;
  188. else
  189. optarg = nargv[optind++];
  190. }
  191. if ((long_options[match].has_arg == required_argument)
  192. && (optarg == NULL)) {
  193. /*
  194. * Missing argument, leading :
  195. * indicates no error should be generated
  196. */
  197. if ((opterr) && (*options != ':'))
  198. (void)fprintf(stderr,
  199. "%s: option requires an argument -- %s\n",
  200. __progname(nargv[0]), current_argv);
  201. return (BADARG);
  202. }
  203. } else { /* No matching argument */
  204. if ((opterr) && (*options != ':'))
  205. (void)fprintf(stderr,
  206. "%s: illegal option -- %s\n", __progname(nargv[0]), current_argv);
  207. return (BADCH);
  208. }
  209. if (long_options[match].flag) {
  210. *long_options[match].flag = long_options[match].val;
  211. retval = 0;
  212. } else
  213. retval = long_options[match].val;
  214. if (index)
  215. *index = match;
  216. }
  217. return(retval);
  218. }