getopt.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /* Borrowed from Apache NT Port */
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <assert.h>
  5. #include <stdlib.h>
  6. #include "getopt.h"
  7. #define OPTERRCOLON (1)
  8. #define OPTERRNF (2)
  9. #define OPTERRARG (3)
  10. char *ap_optarg;
  11. int ap_optind = 1;
  12. static int ap_opterr = 1;
  13. static int ap_optopt;
  14. static int
  15. ap_optiserr(int argc, char * const *argv, int oint, const char *optstr,
  16. int optchr, int err)
  17. {
  18. if (ap_opterr)
  19. {
  20. fprintf(stderr, "Error in argument %d, char %d: ", oint, optchr+1);
  21. switch(err)
  22. {
  23. case OPTERRCOLON:
  24. fprintf(stderr, ": in flags\n");
  25. break;
  26. case OPTERRNF:
  27. fprintf(stderr, "option not found %c\n", argv[oint][optchr]);
  28. break;
  29. case OPTERRARG:
  30. fprintf(stderr, "no argument for option %c\n", argv[oint][optchr]);
  31. break;
  32. default:
  33. fprintf(stderr, "unknown\n");
  34. break;
  35. }
  36. }
  37. ap_optopt = argv[oint][optchr];
  38. return('?');
  39. }
  40. int ap_getopt(int argc, char* const *argv, const char *optstr)
  41. {
  42. static int optchr = 0;
  43. static int dash = 0; /* have already seen the - */
  44. char *cp;
  45. if (ap_optind >= argc)
  46. return(EOF);
  47. if (!dash && (argv[ap_optind][0] != '-'))
  48. return(EOF);
  49. if (!dash && (argv[ap_optind][0] == '-') && !argv[ap_optind][1])
  50. {
  51. /*
  52. * use to specify stdin. Need to let pgm process this and
  53. * the following args
  54. */
  55. return(EOF);
  56. }
  57. if ((argv[ap_optind][0] == '-') && (argv[ap_optind][1] == '-'))
  58. {
  59. /* -- indicates end of args */
  60. ap_optind++;
  61. return(EOF);
  62. }
  63. if (!dash)
  64. {
  65. assert((argv[ap_optind][0] == '-') && argv[ap_optind][1]);
  66. dash = 1;
  67. optchr = 1;
  68. }
  69. /* Check if the guy tries to do a -: kind of flag */
  70. assert(dash);
  71. if (argv[ap_optind][optchr] == ':')
  72. {
  73. dash = 0;
  74. ap_optind++;
  75. return(ap_optiserr(argc, argv, ap_optind-1, optstr, optchr, OPTERRCOLON));
  76. }
  77. if (!(cp = strchr(optstr, argv[ap_optind][optchr])))
  78. {
  79. int errind = ap_optind;
  80. int errchr = optchr;
  81. if (!argv[ap_optind][optchr+1])
  82. {
  83. dash = 0;
  84. ap_optind++;
  85. }
  86. else
  87. optchr++;
  88. return(ap_optiserr(argc, argv, errind, optstr, errchr, OPTERRNF));
  89. }
  90. if (cp[1] == ':')
  91. {
  92. /* Check for cases where the value of the argument
  93. is in the form -<arg> <val> or in the form -<arg><val> */
  94. dash = 0;
  95. if(!argv[ap_optind][2]) {
  96. ap_optind++;
  97. if (ap_optind == argc)
  98. return(ap_optiserr(argc, argv, ap_optind-1, optstr, optchr, OPTERRARG));
  99. ap_optarg = argv[ap_optind++];
  100. }
  101. else
  102. {
  103. ap_optarg = &argv[ap_optind][2];
  104. ap_optind++;
  105. }
  106. return(*cp);
  107. }
  108. else
  109. {
  110. if (!argv[ap_optind][optchr+1])
  111. {
  112. dash = 0;
  113. ap_optind++;
  114. }
  115. else
  116. optchr++;
  117. return(*cp);
  118. }
  119. assert(0);
  120. return(0);
  121. }
  122. #ifdef TESTGETOPT
  123. int
  124. main (int argc, char **argv)
  125. {
  126. int c;
  127. extern char *ap_optarg;
  128. extern int ap_optind;
  129. int aflg = 0;
  130. int bflg = 0;
  131. int errflg = 0;
  132. char *ofile = NULL;
  133. while ((c = ap_getopt(argc, argv, "abo:")) != EOF)
  134. switch (c) {
  135. case 'a':
  136. if (bflg)
  137. errflg++;
  138. else
  139. aflg++;
  140. break;
  141. case 'b':
  142. if (aflg)
  143. errflg++;
  144. else
  145. bflg++;
  146. break;
  147. case 'o':
  148. ofile = ap_optarg;
  149. (void)printf("ofile = %s\n", ofile);
  150. break;
  151. case '?':
  152. errflg++;
  153. }
  154. if (errflg) {
  155. (void)fprintf(stderr,
  156. "usage: cmd [-a|-b] [-o <filename>] files...\n");
  157. exit (2);
  158. }
  159. for ( ; ap_optind < argc; ap_optind++)
  160. (void)printf("%s\n", argv[ap_optind]);
  161. return 0;
  162. }
  163. #endif /* TESTGETOPT */