Home | History | Annotate | Line # | Download | only in isc
commandline.c revision 1.2.2.3
      1 /*	$NetBSD: commandline.c,v 1.2.2.3 2019/01/18 08:49:57 pgoyette Exp $	*/
      2 
      3 /*
      4  * Portions Copyright (C) Internet Systems Consortium, Inc. ("ISC")
      5  *
      6  * This Source Code Form is subject to the terms of the Mozilla Public
      7  * License, v. 2.0. If a copy of the MPL was not distributed with this
      8  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
      9  *
     10  * See the COPYRIGHT file distributed with this work for additional
     11  * information regarding copyright ownership.
     12  */
     13 
     14 /*
     15  * Copyright (c) 1987, 1993, 1994
     16  *	The Regents of the University of California.  All rights reserved.
     17  *
     18  * Redistribution and use in source and binary forms, with or without
     19  * modification, are permitted provided that the following conditions
     20  * are met:
     21  * 1. Redistributions of source code must retain the above copyright
     22  *    notice, this list of conditions and the following disclaimer.
     23  * 2. Redistributions in binary form must reproduce the above copyright
     24  *    notice, this list of conditions and the following disclaimer in the
     25  *    documentation and/or other materials provided with the distribution.
     26  * 3. Neither the name of the University nor the names of its contributors
     27  *    may be used to endorse or promote products derived from this software
     28  *    without specific prior written permission.
     29  *
     30  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     31  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     32  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     33  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     34  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     35  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     36  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     37  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     38  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     39  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     40  * SUCH DAMAGE.
     41  */
     42 
     43 
     44 /*! \file
     45  * This file was adapted from the NetBSD project's source tree, RCS ID:
     46  *    NetBSD: getopt.c,v 1.15 1999/09/20 04:39:37 lukem Exp
     47  *
     48  * The primary change has been to rename items to the ISC namespace
     49  * and format in the ISC coding style.
     50  */
     51 
     52 #include <config.h>
     53 
     54 #include <stdbool.h>
     55 #include <stdio.h>
     56 
     57 #include <isc/commandline.h>
     58 #include <isc/mem.h>
     59 #include <isc/msgs.h>
     60 #include <isc/print.h>
     61 #include <isc/string.h>
     62 #include <isc/util.h>
     63 
     64 /*% Index into parent argv vector. */
     65 LIBISC_EXTERNAL_DATA int isc_commandline_index = 1;
     66 /*% Character checked for validity. */
     67 LIBISC_EXTERNAL_DATA int isc_commandline_option;
     68 /*% Argument associated with option. */
     69 LIBISC_EXTERNAL_DATA char *isc_commandline_argument;
     70 /*% For printing error messages. */
     71 LIBISC_EXTERNAL_DATA char *isc_commandline_progname;
     72 /*% Print error messages. */
     73 LIBISC_EXTERNAL_DATA bool isc_commandline_errprint = true;
     74 /*% Reset processing. */
     75 LIBISC_EXTERNAL_DATA bool isc_commandline_reset = true;
     76 
     77 static char endopt = '\0';
     78 
     79 #define	BADOPT	'?'
     80 #define	BADARG	':'
     81 #define ENDOPT  &endopt
     82 
     83 /*!
     84  * getopt --
     85  *	Parse argc/argv argument vector.
     86  */
     87 int
     88 isc_commandline_parse(int argc, char * const *argv, const char *options) {
     89 	static char *place = ENDOPT;
     90 	const char *option;		/* Index into *options of option. */
     91 
     92 	REQUIRE(argc >= 0 && argv != NULL && options != NULL);
     93 
     94 	/*
     95 	 * Update scanning pointer, either because a reset was requested or
     96 	 * the previous argv was finished.
     97 	 */
     98 	if (isc_commandline_reset || *place == '\0') {
     99 		if (isc_commandline_reset) {
    100 			isc_commandline_index = 1;
    101 			isc_commandline_reset = false;
    102 		}
    103 
    104 		if (isc_commandline_progname == NULL)
    105 			isc_commandline_progname = argv[0];
    106 
    107 		if (isc_commandline_index >= argc ||
    108 		    *(place = argv[isc_commandline_index]) != '-') {
    109 			/*
    110 			 * Index out of range or points to non-option.
    111 			 */
    112 			place = ENDOPT;
    113 			return (-1);
    114 		}
    115 
    116 		if (place[1] != '\0' && *++place == '-' && place[1] == '\0') {
    117 			/*
    118 			 * Found '--' to signal end of options.  Advance
    119 			 * index to next argv, the first non-option.
    120 			 */
    121 			isc_commandline_index++;
    122 			place = ENDOPT;
    123 			return (-1);
    124 		}
    125 	}
    126 
    127 	isc_commandline_option = *place++;
    128 	option = strchr(options, isc_commandline_option);
    129 
    130 	/*
    131 	 * Ensure valid option has been passed as specified by options string.
    132 	 * '-:' is never a valid command line option because it could not
    133 	 * distinguish ':' from the argument specifier in the options string.
    134 	 */
    135 	if (isc_commandline_option == ':' || option == NULL) {
    136 		if (*place == '\0')
    137 			isc_commandline_index++;
    138 
    139 		if (isc_commandline_errprint && *options != ':')
    140 			fprintf(stderr, "%s: %s -- %c\n",
    141 				isc_commandline_progname,
    142 				isc_msgcat_get(isc_msgcat,
    143 					       ISC_MSGSET_COMMANDLINE,
    144 					       ISC_MSG_ILLEGALOPT,
    145 					       "illegal option"),
    146 				isc_commandline_option);
    147 
    148 		return (BADOPT);
    149 	}
    150 
    151 	if (*++option != ':') {
    152 		/*
    153 		 * Option does not take an argument.
    154 		 */
    155 		isc_commandline_argument = NULL;
    156 
    157 		/*
    158 		 * Skip to next argv if at the end of the current argv.
    159 		 */
    160 		if (*place == '\0')
    161 			++isc_commandline_index;
    162 
    163 	} else {
    164 		/*
    165 		 * Option needs an argument.
    166 		 */
    167 		if (*place != '\0')
    168 			/*
    169 			 * Option is in this argv, -D1 style.
    170 			 */
    171 			isc_commandline_argument = place;
    172 
    173 		else if (argc > ++isc_commandline_index)
    174 			/*
    175 			 * Option is next argv, -D 1 style.
    176 			 */
    177 			isc_commandline_argument = argv[isc_commandline_index];
    178 
    179 		else {
    180 			/*
    181 			 * Argument needed, but no more argv.
    182 			 */
    183 			place = ENDOPT;
    184 
    185 			/*
    186 			 * Silent failure with "missing argument" return
    187 			 * when ':' starts options string, per historical spec.
    188 			 */
    189 			if (*options == ':')
    190 				return (BADARG);
    191 
    192 			if (isc_commandline_errprint)
    193 				fprintf(stderr, "%s: %s -- %c\n",
    194 					isc_commandline_progname,
    195 					isc_msgcat_get(isc_msgcat,
    196 						       ISC_MSGSET_COMMANDLINE,
    197 						       ISC_MSG_OPTNEEDARG,
    198 						       "option requires "
    199 						       "an argument"),
    200 					isc_commandline_option);
    201 
    202 			return (BADOPT);
    203 		}
    204 
    205 		place = ENDOPT;
    206 
    207 		/*
    208 		 * Point to argv that follows argument.
    209 		 */
    210 		isc_commandline_index++;
    211 	}
    212 
    213 	return (isc_commandline_option);
    214 }
    215 
    216 isc_result_t
    217 isc_commandline_strtoargv(isc_mem_t *mctx, char *s, unsigned int *argcp,
    218 			  char ***argvp, unsigned int n)
    219 {
    220 	isc_result_t result;
    221 
    222  restart:
    223 	/* Discard leading whitespace. */
    224 	while (*s == ' ' || *s == '\t')
    225 		s++;
    226 
    227 	if (*s == '\0') {
    228 		/* We have reached the end of the string. */
    229 		*argcp = n;
    230 		*argvp = isc_mem_get(mctx, n * sizeof(char *));
    231 		if (*argvp == NULL)
    232 			return (ISC_R_NOMEMORY);
    233 	} else {
    234 		char *p = s;
    235 		while (*p != ' ' && *p != '\t' && *p != '\0' && *p != '{') {
    236 			if (*p == '\n') {
    237 				*p = ' ';
    238 				goto restart;
    239 			}
    240 			p++;
    241 		}
    242 
    243 		/* do "grouping", items between { and } are one arg */
    244 		if (*p == '{') {
    245 			char *t = p;
    246 			/*
    247 			 * shift all characters to left by 1 to get rid of '{'
    248 			 */
    249 			while (*t != '\0') {
    250 				t++;
    251 				*(t-1) = *t;
    252 			}
    253 			while (*p != '\0' && *p != '}') {
    254 				p++;
    255 			}
    256 			/* get rid of '}' character */
    257 			if (*p == '}') {
    258 				*p = '\0';
    259 				p++;
    260 			}
    261 			/* normal case, no "grouping" */
    262 		} else if (*p != '\0')
    263 			*p++ = '\0';
    264 
    265 		result = isc_commandline_strtoargv(mctx, p,
    266 						   argcp, argvp, n + 1);
    267 		if (result != ISC_R_SUCCESS)
    268 			return (result);
    269 		(*argvp)[n] = s;
    270 	}
    271 
    272 	return (ISC_R_SUCCESS);
    273 }
    274