Add num_done counter to the pg_stat_checkpointer view.
[pgsql.git] / src / port / getopt_long.c
blobf83de0dff97894647fff582753d71cb12ab1157d
1 /*
2 * getopt_long() -- long options parser
4 * Portions Copyright (c) 1987, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
7 * Portions Copyright (c) 2003
8 * PostgreSQL Global Development Group
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.
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.
34 * src/port/getopt_long.c
37 #include "c.h"
39 #include "getopt_long.h"
41 #define BADCH '?'
42 #define BADARG ':'
43 #define EMSG ""
47 * getopt_long
48 * Parse argc/argv argument vector, with long options.
50 * This implementation does not use optreset. Instead, we guarantee that
51 * it can be restarted on a new argv array after a previous call returned -1,
52 * if the caller resets optind to 1 before the first call of the new series.
53 * (Internally, this means we must be sure to reset "place" to EMSG,
54 * "nonopt_start" to -1, and "force_nonopt" to false before returning -1.)
56 * Note that this routine reorders the pointers in argv (despite the const
57 * qualifier) so that all non-options will be at the end when -1 is returned.
59 int
60 getopt_long(int argc, char *const argv[],
61 const char *optstring,
62 const struct option *longopts, int *longindex)
64 static char *place = EMSG; /* option letter processing */
65 char *oli; /* option letter list index */
66 static int nonopt_start = -1;
67 static bool force_nonopt = false;
69 if (!*place)
70 { /* update scanning pointer */
71 char **args = (char **) argv;
73 retry:
76 * If we are out of arguments or only non-options remain, return -1.
78 if (optind >= argc || optind == nonopt_start)
80 place = EMSG;
81 nonopt_start = -1;
82 force_nonopt = false;
83 return -1;
86 place = argv[optind];
89 * An argument is a non-option if it meets any of the following
90 * criteria: it follows an argument that is equivalent to the string
91 * "--", it does not start with '-', or it is equivalent to the string
92 * "-". When we encounter a non-option, we move it to the end of argv
93 * (after shifting all remaining arguments over to make room), and
94 * then we try again with the next argument.
96 if (force_nonopt || place[0] != '-' || place[1] == '\0')
98 for (int i = optind; i < argc - 1; i++)
99 args[i] = args[i + 1];
100 args[argc - 1] = place;
102 if (nonopt_start == -1)
103 nonopt_start = argc - 1;
104 else
105 nonopt_start--;
107 goto retry;
110 place++;
112 if (place[0] == '-' && place[1] == '\0')
114 /* found "--", treat it as end of options */
115 ++optind;
116 force_nonopt = true;
117 goto retry;
120 if (place[0] == '-' && place[1])
122 /* long option */
123 size_t namelen;
124 int i;
126 place++;
128 namelen = strcspn(place, "=");
129 for (i = 0; longopts[i].name != NULL; i++)
131 if (strlen(longopts[i].name) == namelen
132 && strncmp(place, longopts[i].name, namelen) == 0)
134 int has_arg = longopts[i].has_arg;
136 if (has_arg != no_argument)
138 if (place[namelen] == '=')
139 optarg = place + namelen + 1;
140 else if (optind < argc - 1 &&
141 has_arg == required_argument)
143 optind++;
144 optarg = argv[optind];
146 else
148 if (optstring[0] == ':')
149 return BADARG;
151 if (opterr && has_arg == required_argument)
152 fprintf(stderr,
153 "%s: option requires an argument -- %s\n",
154 argv[0], place);
156 place = EMSG;
157 optind++;
159 if (has_arg == required_argument)
160 return BADCH;
161 optarg = NULL;
164 else
166 optarg = NULL;
167 if (place[namelen] != 0)
169 /* XXX error? */
173 optind++;
175 if (longindex)
176 *longindex = i;
178 place = EMSG;
180 if (longopts[i].flag == NULL)
181 return longopts[i].val;
182 else
184 *longopts[i].flag = longopts[i].val;
185 return 0;
190 if (opterr && optstring[0] != ':')
191 fprintf(stderr,
192 "%s: illegal option -- %s\n", argv[0], place);
193 place = EMSG;
194 optind++;
195 return BADCH;
199 /* short option */
200 optopt = (int) *place++;
202 oli = strchr(optstring, optopt);
203 if (!oli)
205 if (!*place)
206 ++optind;
207 if (opterr && *optstring != ':')
208 fprintf(stderr,
209 "%s: illegal option -- %c\n", argv[0], optopt);
210 return BADCH;
213 if (oli[1] != ':')
214 { /* don't need argument */
215 optarg = NULL;
216 if (!*place)
217 ++optind;
219 else
220 { /* need an argument */
221 if (*place) /* no white space */
222 optarg = place;
223 else if (argc <= ++optind)
224 { /* no arg */
225 place = EMSG;
226 if (*optstring == ':')
227 return BADARG;
228 if (opterr)
229 fprintf(stderr,
230 "%s: option requires an argument -- %c\n",
231 argv[0], optopt);
232 return BADCH;
234 else
235 /* white space */
236 optarg = argv[optind];
237 place = EMSG;
238 ++optind;
240 return optopt;