Imported Upstream version 3.7.1
[platform/upstream/ccache.git] / src / getopt_long.c
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
35 #include "config.h"
36
37 #ifdef HAVE_GETOPT_LONG
38
39 typedef int do_not_warn_about_empty_compilation_unit;
40
41 #else
42
43 #include "getopt_long.h"
44
45 #include <stdio.h>
46 #include <string.h>
47 #include <unistd.h>
48
49 #define BADCH  '?'
50 #define BADARG ':'
51 #define EMSG   ""
52
53 int
54 getopt_long(int argc, char *const argv[],
55             const char *optstring,
56             const struct option * longopts, int *longindex)
57 {
58         static char *place = EMSG; /* option letter processing */
59         char        *oli;          /* option letter list index */
60
61         if (!*place)
62         { /* update scanning pointer */
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[1] == '\0')
80                 { /* found "--" */
81                         ++optind;
82                         place = EMSG;
83                         return -1;
84                 }
85
86                 if (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 }
200
201 #endif /* HAVE_GETOPT_LONG */