2
+----------------------------------------------------------------------+
4
+----------------------------------------------------------------------+
5
| Copyright (c) 1997-2004 The PHP Group |
6
+----------------------------------------------------------------------+
7
| This source file is subject to version 3.0 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_0.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
+----------------------------------------------------------------------+
23
#include "php_getopt.h"
24
#define OPTERRCOLON (1)
29
static int php_opt_error(int argc, char * const *argv, int oint, int optchr, int err, int show_err)
33
fprintf(stderr, "Error in argument %d, char %d: ", oint, optchr+1);
37
fprintf(stderr, ": in flags\n");
40
fprintf(stderr, "option not found %c\n", argv[oint][optchr]);
43
fprintf(stderr, "no argument for option %c\n", argv[oint][optchr]);
46
fprintf(stderr, "unknown\n");
53
int php_getopt(int argc, char* const *argv, const opt_struct opts[], char **optarg, int *optind, int show_err)
55
static int optchr = 0;
56
static int dash = 0; /* have already seen the - */
61
if (*optind >= argc) {
65
if ((argv[*optind][0] != '-')) {
68
if (!argv[*optind][1])
71
* use to specify stdin. Need to let pgm process this and
78
if ((argv[*optind][0] == '-') && (argv[*optind][1] == '-')) {
79
/* '--' indicates end of args if not followed by a known long option name */
82
if (opts[opts_idx].opt_char == '-') {
85
} else if (opts[opts_idx].opt_name && !strcmp(&argv[*optind][2], opts[opts_idx].opt_name)) {
91
arg_start = 2 + strlen(opts[opts_idx].opt_name);
98
/* Check if the guy tries to do a -: kind of flag */
99
if (argv[*optind][optchr] == ':') {
102
return (php_opt_error(argc, argv, *optind-1, optchr, OPTERRCOLON, show_err));
107
if (opts[opts_idx].opt_char == '-') {
108
int errind = *optind;
111
if (!argv[*optind][optchr+1]) {
117
return(php_opt_error(argc, argv, errind, errchr, OPTERRNF, show_err));
118
} else if (argv[*optind][optchr] == opts[opts_idx].opt_char) {
123
if (opts[opts_idx].need_param) {
124
/* Check for cases where the value of the argument
125
is in the form -<arg> <val> or in the form -<arg><val> */
127
if(!argv[*optind][arg_start]) {
129
if (*optind == argc) {
130
return(php_opt_error(argc, argv, *optind-1, optchr, OPTERRARG, show_err));
132
*optarg = argv[(*optind)++];
134
*optarg = &argv[*optind][arg_start];
137
return opts[opts_idx].opt_char;
139
if (arg_start == 2) {
140
if (!argv[*optind][optchr+1])
150
return opts[opts_idx].opt_char;
153
return(0); /* never reached */