You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

170 lines
3.8 KiB

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