getopt.c 4.3 KB

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