~ubuntu-branches/ubuntu/hardy/php5/hardy-updates

« back to all changes in this revision

Viewing changes to sapi/cgi/getopt.c

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-10-09 03:14:32 UTC
  • Revision ID: james.westby@ubuntu.com-20051009031432-kspik3lobxstafv9
Tags: upstream-5.0.5
ImportĀ upstreamĀ versionĀ 5.0.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
   +----------------------------------------------------------------------+
 
3
   | PHP Version 5                                                        |
 
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
   +----------------------------------------------------------------------+
 
17
*/
 
18
 
 
19
#include <stdio.h>
 
20
#include <string.h>
 
21
#include <assert.h>
 
22
#include <stdlib.h>
 
23
#include "php_getopt.h"
 
24
#define OPTERRCOLON (1)
 
25
#define OPTERRNF (2)
 
26
#define OPTERRARG (3)
 
27
 
 
28
 
 
29
static int php_opt_error(int argc, char * const *argv, int oint, int optchr, int err, int show_err)
 
30
{
 
31
        if (show_err)
 
32
        {
 
33
                fprintf(stderr, "Error in argument %d, char %d: ", oint, optchr+1);
 
34
                switch(err)
 
35
                {
 
36
                case OPTERRCOLON:
 
37
                        fprintf(stderr, ": in flags\n");
 
38
                        break;
 
39
                case OPTERRNF:
 
40
                        fprintf(stderr, "option not found %c\n", argv[oint][optchr]);
 
41
                        break;
 
42
                case OPTERRARG:
 
43
                        fprintf(stderr, "no argument for option %c\n", argv[oint][optchr]);
 
44
                        break;
 
45
                default:
 
46
                        fprintf(stderr, "unknown\n");
 
47
                        break;
 
48
                }
 
49
        }
 
50
        return('?');
 
51
}
 
52
 
 
53
int php_getopt(int argc, char* const *argv, const opt_struct opts[], char **optarg, int *optind, int show_err)
 
54
{
 
55
        static int optchr = 0;
 
56
        static int dash = 0; /* have already seen the - */
 
57
        int arg_start = 2;
 
58
 
 
59
        int opts_idx = -1;
 
60
 
 
61
        if (*optind >= argc) {
 
62
                return(EOF);
 
63
        }
 
64
        if (!dash) {
 
65
                if ((argv[*optind][0] !=  '-')) {
 
66
                        return(EOF);
 
67
                } else {
 
68
                        if (!argv[*optind][1])
 
69
                        {
 
70
                                /*
 
71
                                * use to specify stdin. Need to let pgm process this and
 
72
                                * the following args
 
73
                                */
 
74
                                return(EOF);
 
75
                        }
 
76
                }
 
77
        }
 
78
        if ((argv[*optind][0] == '-') && (argv[*optind][1] == '-')) {
 
79
                /* '--' indicates end of args if not followed by a known long option name */
 
80
                while (1) {
 
81
                        opts_idx++;
 
82
                        if (opts[opts_idx].opt_char == '-') {
 
83
                                (*optind)++;
 
84
                                return(EOF);
 
85
                        } else if (opts[opts_idx].opt_name && !strcmp(&argv[*optind][2], opts[opts_idx].opt_name)) {
 
86
                                break;
 
87
                        }
 
88
                }
 
89
                optchr = 0;
 
90
                dash = 1;
 
91
                arg_start = 2 + strlen(opts[opts_idx].opt_name);
 
92
        }
 
93
        if (!dash) {
 
94
                dash = 1;
 
95
                optchr = 1;
 
96
        }
 
97
 
 
98
        /* Check if the guy tries to do a -: kind of flag */
 
99
        if (argv[*optind][optchr] == ':') {
 
100
                dash = 0;
 
101
                (*optind)++;
 
102
                return (php_opt_error(argc, argv, *optind-1, optchr, OPTERRCOLON, show_err));
 
103
        }
 
104
        if (opts_idx < 0) {
 
105
                while (1) {
 
106
                        opts_idx++;
 
107
                        if (opts[opts_idx].opt_char == '-') {
 
108
                                int errind = *optind;
 
109
                                int errchr = optchr;
 
110
                
 
111
                                if (!argv[*optind][optchr+1]) {
 
112
                                        dash = 0;
 
113
                                        (*optind)++;
 
114
                                } else {
 
115
                                        optchr++;
 
116
                                }
 
117
                                return(php_opt_error(argc, argv, errind, errchr, OPTERRNF, show_err));
 
118
                        } else if (argv[*optind][optchr] == opts[opts_idx].opt_char) {
 
119
                                break;
 
120
                        }
 
121
                }
 
122
        }
 
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> */
 
126
                dash = 0;
 
127
                if(!argv[*optind][arg_start]) {
 
128
                        (*optind)++;
 
129
                        if (*optind == argc) {
 
130
                                return(php_opt_error(argc, argv, *optind-1, optchr, OPTERRARG, show_err));
 
131
                        }
 
132
                        *optarg = argv[(*optind)++];
 
133
                } else {
 
134
                        *optarg = &argv[*optind][arg_start];
 
135
                        (*optind)++;
 
136
                }
 
137
                return opts[opts_idx].opt_char;
 
138
        } else {
 
139
                if (arg_start == 2) {
 
140
                        if (!argv[*optind][optchr+1])
 
141
                        {
 
142
                                dash = 0;
 
143
                                (*optind)++;
 
144
                        } else {
 
145
                                optchr++;
 
146
                        }
 
147
                } else {
 
148
                        (*optind)++;
 
149
                }
 
150
                return opts[opts_idx].opt_char;
 
151
        }
 
152
        assert(0);
 
153
        return(0);      /* never reached */
 
154
}