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