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