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