]> CyberLeo.Net >> Repos - FreeBSD/stable/8.git/blob - contrib/bind9/lib/isc/commandline.c
MFC: r253983-253984
[FreeBSD/stable/8.git] / contrib / bind9 / lib / isc / commandline.c
1 /*
2  * Portions Copyright (C) 2004, 2005, 2007, 2008, 2014  Internet Systems Consortium, Inc. ("ISC")
3  * Portions Copyright (C) 1999-2001  Internet Software Consortium.
4  *
5  * Permission to use, copy, modify, and/or distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15  * PERFORMANCE OF THIS SOFTWARE.
16  */
17
18 /*
19  * Copyright (c) 1987, 1993, 1994
20  *      The Regents of the University of California.  All rights reserved.
21  *
22  * Redistribution and use in source and binary forms, with or without
23  * modification, are permitted provided that the following conditions
24  * are met:
25  * 1. Redistributions of source code must retain the above copyright
26  *    notice, this list of conditions and the following disclaimer.
27  * 2. Redistributions in binary form must reproduce the above copyright
28  *    notice, this list of conditions and the following disclaimer in the
29  *    documentation and/or other materials provided with the distribution.
30  * 3. Neither the name of the University nor the names of its contributors
31  *    may be used to endorse or promote products derived from this software
32  *    without specific prior written permission.
33  *
34  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
35  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
36  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
37  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
38  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
39  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
40  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
41  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
42  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
43  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
44  * SUCH DAMAGE.
45  */
46
47 /* $Id: commandline.c,v 1.22 2008/09/25 04:02:39 tbox Exp $ */
48
49 /*! \file
50  * This file was adapted from the NetBSD project's source tree, RCS ID:
51  *    NetBSD: getopt.c,v 1.15 1999/09/20 04:39:37 lukem Exp
52  *
53  * The primary change has been to rename items to the ISC namespace
54  * and format in the ISC coding style.
55  */
56
57 /*
58  * \author Principal Authors: Computer Systems Research Group at UC Berkeley
59  * \author Principal ISC caretaker: DCL
60  */
61
62 #include <config.h>
63
64 #include <stdio.h>
65
66 #include <isc/commandline.h>
67 #include <isc/msgs.h>
68 #include <isc/string.h>
69 #include <isc/util.h>
70
71 /*% Index into parent argv vector. */
72 LIBISC_EXTERNAL_DATA int isc_commandline_index = 1;
73 /*% Character checked for validity. */
74 LIBISC_EXTERNAL_DATA int isc_commandline_option;
75 /*% Argument associated with option. */
76 LIBISC_EXTERNAL_DATA char *isc_commandline_argument;
77 /*% For printing error messages. */
78 LIBISC_EXTERNAL_DATA char *isc_commandline_progname;
79 /*% Print error messages. */
80 LIBISC_EXTERNAL_DATA isc_boolean_t isc_commandline_errprint = ISC_TRUE;
81 /*% Reset processing. */
82 LIBISC_EXTERNAL_DATA isc_boolean_t isc_commandline_reset = ISC_TRUE;
83
84 static char endopt = '\0';
85
86 #define BADOPT  '?'
87 #define BADARG  ':'
88 #define ENDOPT  &endopt
89
90 /*!
91  * getopt --
92  *      Parse argc/argv argument vector.
93  */
94 int
95 isc_commandline_parse(int argc, char * const *argv, const char *options) {
96         static char *place = ENDOPT;
97         char *option;                   /* Index into *options of option. */
98
99         REQUIRE(argc >= 0 && argv != NULL && options != NULL);
100
101         /*
102          * Update scanning pointer, either because a reset was requested or
103          * the previous argv was finished.
104          */
105         if (isc_commandline_reset || *place == '\0') {
106                 if (isc_commandline_reset) {
107                         isc_commandline_index = 1;
108                         isc_commandline_reset = ISC_FALSE;
109                 }
110
111                 if (isc_commandline_progname == NULL)
112                         isc_commandline_progname = argv[0];
113
114                 if (isc_commandline_index >= argc ||
115                     *(place = argv[isc_commandline_index]) != '-') {
116                         /*
117                          * Index out of range or points to non-option.
118                          */
119                         place = ENDOPT;
120                         return (-1);
121                 }
122
123                 if (place[1] != '\0' && *++place == '-' && place[1] == '\0') {
124                         /*
125                          * Found '--' to signal end of options.  Advance
126                          * index to next argv, the first non-option.
127                          */
128                         isc_commandline_index++;
129                         place = ENDOPT;
130                         return (-1);
131                 }
132         }
133
134         isc_commandline_option = *place++;
135         option = strchr(options, isc_commandline_option);
136
137         /*
138          * Ensure valid option has been passed as specified by options string.
139          * '-:' is never a valid command line option because it could not
140          * distinguish ':' from the argument specifier in the options string.
141          */
142         if (isc_commandline_option == ':' || option == NULL) {
143                 if (*place == '\0')
144                         isc_commandline_index++;
145
146                 if (isc_commandline_errprint && *options != ':')
147                         fprintf(stderr, "%s: %s -- %c\n",
148                                 isc_commandline_progname,
149                                 isc_msgcat_get(isc_msgcat,
150                                                ISC_MSGSET_COMMANDLINE,
151                                                ISC_MSG_ILLEGALOPT,
152                                                "illegal option"),
153                                 isc_commandline_option);
154
155                 return (BADOPT);
156         }
157
158         if (*++option != ':') {
159                 /*
160                  * Option does not take an argument.
161                  */
162                 isc_commandline_argument = NULL;
163
164                 /*
165                  * Skip to next argv if at the end of the current argv.
166                  */
167                 if (*place == '\0')
168                         ++isc_commandline_index;
169
170         } else {
171                 /*
172                  * Option needs an argument.
173                  */
174                 if (*place != '\0')
175                         /*
176                          * Option is in this argv, -D1 style.
177                          */
178                         isc_commandline_argument = place;
179
180                 else if (argc > ++isc_commandline_index)
181                         /*
182                          * Option is next argv, -D 1 style.
183                          */
184                         isc_commandline_argument = argv[isc_commandline_index];
185
186                 else {
187                         /*
188                          * Argument needed, but no more argv.
189                          */
190                         place = ENDOPT;
191
192                         /*
193                          * Silent failure with "missing argument" return
194                          * when ':' starts options string, per historical spec.
195                          */
196                         if (*options == ':')
197                                 return (BADARG);
198
199                         if (isc_commandline_errprint)
200                                 fprintf(stderr, "%s: %s -- %c\n",
201                                         isc_commandline_progname,
202                                         isc_msgcat_get(isc_msgcat,
203                                                        ISC_MSGSET_COMMANDLINE,
204                                                        ISC_MSG_OPTNEEDARG,
205                                                        "option requires "
206                                                        "an argument"),
207                                         isc_commandline_option);
208
209                         return (BADOPT);
210                 }
211
212                 place = ENDOPT;
213
214                 /*
215                  * Point to argv that follows argument.
216                  */
217                 isc_commandline_index++;
218         }
219
220         return (isc_commandline_option);
221 }