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