~vcs-imports/mammoth-replicator/trunk

« back to all changes in this revision

Viewing changes to src/port/getopt_long.c

  • Committer: alvherre
  • Date: 2005-12-16 21:24:52 UTC
  • Revision ID: svn-v4:db760fc0-0f08-0410-9d63-cc6633f64896:trunk:1
Initial import of the REL8_0_3 sources from the Pgsql CVS repository.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * getopt_long() -- long options parser
 
3
 *
 
4
 * Portions Copyright (c) 1987, 1993, 1994
 
5
 * The Regents of the University of California.  All rights reserved.
 
6
 *
 
7
 * Portions Copyright (c) 2003
 
8
 * PostgreSQL Global Development Group
 
9
 *
 
10
 * Redistribution and use in source and binary forms, with or without
 
11
 * modification, are permitted provided that the following conditions
 
12
 * are met:
 
13
 * 1. Redistributions of source code must retain the above copyright
 
14
 *        notice, this list of conditions and the following disclaimer.
 
15
 * 2. Redistributions in binary form must reproduce the above copyright
 
16
 *        notice, this list of conditions and the following disclaimer in the
 
17
 *        documentation and/or other materials provided with the distribution.
 
18
 * 3. All advertising materials mentioning features or use of this software
 
19
 *        must display the following acknowledgement:
 
20
 *      This product includes software developed by the University of
 
21
 *      California, Berkeley and its contributors.
 
22
 * 4. Neither the name of the University nor the names of its contributors
 
23
 *        may be used to endorse or promote products derived from this software
 
24
 *        without specific prior written permission.
 
25
 *
 
26
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 
27
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 
28
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 
29
 * ARE DISCLAIMED.      IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 
30
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 
31
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 
32
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 
33
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 
34
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 
35
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 
36
 * SUCH DAMAGE.
 
37
 *
 
38
 * $PostgreSQL: pgsql/src/port/getopt_long.c,v 1.3 2003-11-29 19:52:13 pgsql Exp $
 
39
 */
 
40
 
 
41
#include <stdio.h>
 
42
#include <stdlib.h>
 
43
#include <string.h>
 
44
 
 
45
#include "getopt_long.h"
 
46
 
 
47
#define BADCH   '?'
 
48
#define BADARG  ':'
 
49
#define EMSG    ""
 
50
 
 
51
int
 
52
getopt_long(int argc, char *const argv[],
 
53
                        const char *optstring,
 
54
                        const struct option * longopts, int *longindex)
 
55
{
 
56
        static char *place = EMSG;      /* option letter processing */
 
57
        char       *oli;                        /* option letter list index */
 
58
 
 
59
        if (optreset || !*place)
 
60
        {                                                       /* update scanning pointer */
 
61
                optreset = 0;
 
62
 
 
63
                if (optind >= argc)
 
64
                {
 
65
                        place = EMSG;
 
66
                        return -1;
 
67
                }
 
68
 
 
69
                place = argv[optind];
 
70
 
 
71
                if (place[0] != '-')
 
72
                {
 
73
                        place = EMSG;
 
74
                        return -1;
 
75
                }
 
76
 
 
77
                place++;
 
78
 
 
79
                if (place[0] && place[0] == '-' && place[1] == '\0')
 
80
                {                                               /* found "--" */
 
81
                        ++optind;
 
82
                        place = EMSG;
 
83
                        return -1;
 
84
                }
 
85
 
 
86
                if (place[0] && place[0] == '-' && place[1])
 
87
                {
 
88
                        /* long option */
 
89
                        size_t          namelen;
 
90
                        int                     i;
 
91
 
 
92
                        place++;
 
93
 
 
94
                        namelen = strcspn(place, "=");
 
95
                        for (i = 0; longopts[i].name != NULL; i++)
 
96
                        {
 
97
                                if (strlen(longopts[i].name) == namelen
 
98
                                        && strncmp(place, longopts[i].name, namelen) == 0)
 
99
                                {
 
100
                                        if (longopts[i].has_arg)
 
101
                                        {
 
102
                                                if (place[namelen] == '=')
 
103
                                                        optarg = place + namelen + 1;
 
104
                                                else if (optind < argc - 1)
 
105
                                                {
 
106
                                                        optind++;
 
107
                                                        optarg = argv[optind];
 
108
                                                }
 
109
                                                else
 
110
                                                {
 
111
                                                        if (optstring[0] == ':')
 
112
                                                                return BADARG;
 
113
                                                        if (opterr)
 
114
                                                                fprintf(stderr,
 
115
                                                                "%s: option requires an argument -- %s\n",
 
116
                                                                                argv[0], place);
 
117
                                                        place = EMSG;
 
118
                                                        optind++;
 
119
                                                        return BADCH;
 
120
                                                }
 
121
                                        }
 
122
                                        else
 
123
                                        {
 
124
                                                optarg = NULL;
 
125
                                                if (place[namelen] != 0)
 
126
                                                {
 
127
                                                        /* XXX error? */
 
128
                                                }
 
129
                                        }
 
130
 
 
131
                                        optind++;
 
132
 
 
133
                                        if (longindex)
 
134
                                                *longindex = i;
 
135
 
 
136
                                        place = EMSG;
 
137
 
 
138
                                        if (longopts[i].flag == NULL)
 
139
                                                return longopts[i].val;
 
140
                                        else
 
141
                                        {
 
142
                                                *longopts[i].flag = longopts[i].val;
 
143
                                                return 0;
 
144
                                        }
 
145
                                }
 
146
                        }
 
147
 
 
148
                        if (opterr && optstring[0] != ':')
 
149
                                fprintf(stderr,
 
150
                                                "%s: illegal option -- %s\n", argv[0], place);
 
151
                        place = EMSG;
 
152
                        optind++;
 
153
                        return BADCH;
 
154
                }
 
155
        }
 
156
 
 
157
        /* short option */
 
158
        optopt = (int) *place++;
 
159
 
 
160
        oli = strchr(optstring, optopt);
 
161
        if (!oli)
 
162
        {
 
163
                if (!*place)
 
164
                        ++optind;
 
165
                if (opterr && *optstring != ':')
 
166
                        fprintf(stderr,
 
167
                                        "%s: illegal option -- %c\n", argv[0], optopt);
 
168
                return BADCH;
 
169
        }
 
170
 
 
171
        if (oli[1] != ':')
 
172
        {                                                       /* don't need argument */
 
173
                optarg = NULL;
 
174
                if (!*place)
 
175
                        ++optind;
 
176
        }
 
177
        else
 
178
        {                                                       /* need an argument */
 
179
                if (*place)                             /* no white space */
 
180
                        optarg = place;
 
181
                else if (argc <= ++optind)
 
182
                {                                               /* no arg */
 
183
                        place = EMSG;
 
184
                        if (*optstring == ':')
 
185
                                return BADARG;
 
186
                        if (opterr)
 
187
                                fprintf(stderr,
 
188
                                                "%s: option requires an argument -- %c\n",
 
189
                                                argv[0], optopt);
 
190
                        return BADCH;
 
191
                }
 
192
                else
 
193
                        /* white space */
 
194
                        optarg = argv[optind];
 
195
                place = EMSG;
 
196
                ++optind;
 
197
        }
 
198
        return optopt;
 
199
}