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

« back to all changes in this revision

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