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.

154 lines
4.1 KiB

27 years ago
27 years ago
27 years ago
27 years ago
27 years ago
27 years ago
27 years ago
27 years ago
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 5 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2006 The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 3.01 of the PHP license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | http://www.php.net/license/3_01.txt |
  11. | If you did not receive a copy of the PHP license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@php.net so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Author: Marcus Boerger <helly@php.net> |
  16. +----------------------------------------------------------------------+
  17. */
  18. #include <stdio.h>
  19. #include <string.h>
  20. #include <assert.h>
  21. #include <stdlib.h>
  22. #include "php_getopt.h"
  23. #define OPTERRCOLON (1)
  24. #define OPTERRNF (2)
  25. #define OPTERRARG (3)
  26. static int php_opt_error(int argc, char * const *argv, int oint, int optchr, int err, int show_err)
  27. {
  28. if (show_err)
  29. {
  30. fprintf(stderr, "Error in argument %d, char %d: ", oint, optchr+1);
  31. switch(err)
  32. {
  33. case OPTERRCOLON:
  34. fprintf(stderr, ": in flags\n");
  35. break;
  36. case OPTERRNF:
  37. fprintf(stderr, "option not found %c\n", argv[oint][optchr]);
  38. break;
  39. case OPTERRARG:
  40. fprintf(stderr, "no argument for option %c\n", argv[oint][optchr]);
  41. break;
  42. default:
  43. fprintf(stderr, "unknown\n");
  44. break;
  45. }
  46. }
  47. return('?');
  48. }
  49. int php_getopt(int argc, char* const *argv, const opt_struct opts[], char **optarg, int *optind, int show_err)
  50. {
  51. static int optchr = 0;
  52. static int dash = 0; /* have already seen the - */
  53. int arg_start = 2;
  54. int opts_idx = -1;
  55. if (*optind >= argc) {
  56. return(EOF);
  57. }
  58. if (!dash) {
  59. if ((argv[*optind][0] != '-')) {
  60. return(EOF);
  61. } else {
  62. if (!argv[*optind][1])
  63. {
  64. /*
  65. * use to specify stdin. Need to let pgm process this and
  66. * the following args
  67. */
  68. return(EOF);
  69. }
  70. }
  71. }
  72. if ((argv[*optind][0] == '-') && (argv[*optind][1] == '-')) {
  73. /* '--' indicates end of args if not followed by a known long option name */
  74. while (1) {
  75. opts_idx++;
  76. if (opts[opts_idx].opt_char == '-') {
  77. (*optind)++;
  78. return(EOF);
  79. } else if (opts[opts_idx].opt_name && !strcmp(&argv[*optind][2], opts[opts_idx].opt_name)) {
  80. break;
  81. }
  82. }
  83. optchr = 0;
  84. dash = 1;
  85. arg_start = 2 + strlen(opts[opts_idx].opt_name);
  86. }
  87. if (!dash) {
  88. dash = 1;
  89. optchr = 1;
  90. }
  91. /* Check if the guy tries to do a -: kind of flag */
  92. if (argv[*optind][optchr] == ':') {
  93. dash = 0;
  94. (*optind)++;
  95. return (php_opt_error(argc, argv, *optind-1, optchr, OPTERRCOLON, show_err));
  96. }
  97. if (opts_idx < 0) {
  98. while (1) {
  99. opts_idx++;
  100. if (opts[opts_idx].opt_char == '-') {
  101. int errind = *optind;
  102. int errchr = optchr;
  103. if (!argv[*optind][optchr+1]) {
  104. dash = 0;
  105. (*optind)++;
  106. } else {
  107. optchr++;
  108. }
  109. return(php_opt_error(argc, argv, errind, errchr, OPTERRNF, show_err));
  110. } else if (argv[*optind][optchr] == opts[opts_idx].opt_char) {
  111. break;
  112. }
  113. }
  114. }
  115. if (opts[opts_idx].need_param) {
  116. /* Check for cases where the value of the argument
  117. is in the form -<arg> <val> or in the form -<arg><val> */
  118. dash = 0;
  119. if(!argv[*optind][arg_start]) {
  120. (*optind)++;
  121. if (*optind == argc) {
  122. return(php_opt_error(argc, argv, *optind-1, optchr, OPTERRARG, show_err));
  123. }
  124. *optarg = argv[(*optind)++];
  125. } else {
  126. *optarg = &argv[*optind][arg_start];
  127. (*optind)++;
  128. }
  129. return opts[opts_idx].opt_char;
  130. } else {
  131. if (arg_start == 2) {
  132. if (!argv[*optind][optchr+1])
  133. {
  134. dash = 0;
  135. (*optind)++;
  136. } else {
  137. optchr++;
  138. }
  139. } else {
  140. (*optind)++;
  141. }
  142. return opts[opts_idx].opt_char;
  143. }
  144. assert(0);
  145. return(0); /* never reached */
  146. }