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
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
34 * src/port/getopt_long.c
39 #include "getopt_long.h"
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 before
57 getopt_long(int argc, char *const argv[],
58 const char *optstring,
59 const struct option * longopts, int *longindex)
61 static char *place = EMSG; /* option letter processing */
62 char *oli; /* option letter list index */
65 { /* update scanning pointer */
82 if (place[0] && place[0] == '-' && place[1] == '\0')
89 if (place[0] && place[0] == '-' && place[1])
97 namelen = strcspn(place, "=");
98 for (i = 0; longopts[i].name != NULL; i++)
100 if (strlen(longopts[i].name) == namelen
101 && strncmp(place, longopts[i].name, namelen) == 0)
103 int has_arg = longopts[i].has_arg;
105 if (has_arg != no_argument)
107 if (place[namelen] == '=')
108 optarg = place + namelen + 1;
109 else if (optind < argc - 1 &&
110 has_arg == required_argument)
113 optarg = argv[optind];
117 if (optstring[0] == ':')
120 if (opterr && has_arg == required_argument)
122 "%s: option requires an argument -- %s\n",
128 if (has_arg == required_argument)
136 if (place[namelen] != 0)
149 if (longopts[i].flag == NULL)
150 return longopts[i].val;
153 *longopts[i].flag = longopts[i].val;
159 if (opterr && optstring[0] != ':')
161 "%s: illegal option -- %s\n", argv[0], place);
169 optopt = (int) *place++;
171 oli = strchr(optstring, optopt);
176 if (opterr && *optstring != ':')
178 "%s: illegal option -- %c\n", argv[0], optopt);
183 { /* don't need argument */
189 { /* need an argument */
190 if (*place) /* no white space */
192 else if (argc <= ++optind)
195 if (*optstring == ':')
199 "%s: option requires an argument -- %c\n",
205 optarg = argv[optind];