/[tivodecode]/t2sami/trunk/getopt_long.c
ViewVC logotype

Contents of /t2sami/trunk/getopt_long.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 198 - (show annotations) (download)
Sun Apr 1 04:46:08 2007 UTC (15 years, 4 months ago) by jeremyd2019
File MIME type: text/plain
File size: 4763 byte(s)
merge autoconf branch back to head.  AUTOCONF_CONVERSION_BRANCH is now closed

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

cvs@jdrake.com
ViewVC Help
Powered by ViewVC 1.1.13