diff options
author | Florian Obser <florian@cvs.openbsd.org> | 2020-02-04 19:06:55 +0000 |
---|---|---|
committer | Florian Obser <florian@cvs.openbsd.org> | 2020-02-04 19:06:55 +0000 |
commit | cc6dda2855ffffb42db439a27ea3adbcfe1cb911 (patch) | |
tree | 3a398084417bf02fb8133e9e3dcd4b12e3c6ccd4 | |
parent | b2d4020cbbe227274c101346ab2400b6083007b8 (diff) |
Get rid of getopt(3) compat code.
-rw-r--r-- | usr.sbin/bind/bin/dig/host.c | 58 | ||||
-rw-r--r-- | usr.sbin/bind/bin/dig/nslookup.c | 1 | ||||
-rw-r--r-- | usr.sbin/bind/lib/isc/Makefile.inc | 4 | ||||
-rw-r--r-- | usr.sbin/bind/lib/isc/commandline.c | 213 | ||||
-rw-r--r-- | usr.sbin/bind/lib/isc/include/isc/commandline.h | 49 |
5 files changed, 30 insertions, 295 deletions
diff --git a/usr.sbin/bind/bin/dig/host.c b/usr.sbin/bind/bin/dig/host.c index 32ad3e30226..227bf11d168 100644 --- a/usr.sbin/bind/bin/dig/host.c +++ b/usr.sbin/bind/bin/dig/host.c @@ -24,7 +24,6 @@ #include <locale.h> #include <isc/app.h> -#include <isc/commandline.h> #include <isc/netaddr.h> #include <string.h> @@ -573,7 +572,7 @@ static void pre_parse_args(int argc, char **argv) { int c; - while ((c = isc_commandline_parse(argc, argv, optstring)) != -1) { + while ((c = getopt(argc, argv, optstring)) != -1) { switch (c) { case '4': if (ipv6only) @@ -614,12 +613,12 @@ pre_parse_args(int argc, char **argv) { show_usage(); } } - isc_commandline_reset = ISC_TRUE; - isc_commandline_index = 1; + optind = 1; + optreset = 1; } static void -parse_args(isc_boolean_t is_batchfile, int argc, char **argv) { +parse_args(int argc, char **argv) { char hostname[MXNAME]; dig_lookup_t *lookup; int c; @@ -630,14 +629,12 @@ parse_args(isc_boolean_t is_batchfile, int argc, char **argv) { dns_rdataclass_t rdclass; uint32_t serial = 0; - UNUSED(is_batchfile); - lookup = make_empty_lookup(); lookup->servfail_stops = ISC_FALSE; lookup->comments = ISC_FALSE; - while ((c = isc_commandline_parse(argc, argv, optstring)) != -1) { + while ((c = getopt(argc, argv, optstring)) != -1) { switch (c) { case 'l': lookup->tcp_mode = ISC_TRUE; @@ -653,24 +650,22 @@ parse_args(isc_boolean_t is_batchfile, int argc, char **argv) { lookup->recurse = ISC_FALSE; break; case 't': - if (strncasecmp(isc_commandline_argument, - "ixfr=", 5) == 0) { + if (strncasecmp(optarg, "ixfr=", 5) == 0) { rdtype = dns_rdatatype_ixfr; /* XXXMPA add error checking */ - serial = strtoul(isc_commandline_argument + 5, + serial = strtoul(optarg + 5, NULL, 10); result = ISC_R_SUCCESS; } else { - tr.base = isc_commandline_argument; - tr.length = strlen(isc_commandline_argument); + tr.base = optarg; + tr.length = strlen(optarg); result = dns_rdatatype_fromtext(&rdtype, (isc_textregion_t *)&tr); } if (result != ISC_R_SUCCESS) { fatalexit = 2; - fatal("invalid type: %s\n", - isc_commandline_argument); + fatal("invalid type: %s\n", optarg); } if (!lookup->rdtypeset || lookup->rdtype != dns_rdatatype_axfr) @@ -691,15 +686,14 @@ parse_args(isc_boolean_t is_batchfile, int argc, char **argv) { default_lookups = ISC_FALSE; break; case 'c': - tr.base = isc_commandline_argument; - tr.length = strlen(isc_commandline_argument); + tr.base = optarg; + tr.length = strlen(optarg); result = dns_rdataclass_fromtext(&rdclass, (isc_textregion_t *)&tr); if (result != ISC_R_SUCCESS) { fatalexit = 2; - fatal("invalid class: %s\n", - isc_commandline_argument); + fatal("invalid class: %s\n", optarg); } else { lookup->rdclass = rdclass; lookup->rdclassset = ISC_TRUE; @@ -733,12 +727,12 @@ parse_args(isc_boolean_t is_batchfile, int argc, char **argv) { timeout = INT_MAX; break; case 'W': - timeout = atoi(isc_commandline_argument); + timeout = atoi(optarg); if (timeout < 1) timeout = 1; break; case 'R': - tries = atoi(isc_commandline_argument) + 1; + tries = atoi(optarg) + 1; if (tries < 2) tries = 2; break; @@ -757,9 +751,8 @@ parse_args(isc_boolean_t is_batchfile, int argc, char **argv) { default_lookups = ISC_FALSE; break; case 'N': - debug("setting NDOTS to %s", - isc_commandline_argument); - ndots = atoi(isc_commandline_argument); + debug("setting NDOTS to %s", optarg); + ndots = atoi(optarg); break; case 'D': /* Handled by pre_parse_args(). */ @@ -773,19 +766,24 @@ parse_args(isc_boolean_t is_batchfile, int argc, char **argv) { case 's': lookup->servfail_stops = ISC_TRUE; break; + default: + show_usage(); } } lookup->retries = tries; - if (isc_commandline_index >= argc) + argc -= optind; + argv += optind; + + if (argc == 0) show_usage(); - strlcpy(hostname, argv[isc_commandline_index], sizeof(hostname)); + strlcpy(hostname, *argv, sizeof(hostname)); - if (argc > isc_commandline_index + 1) { - set_nameserver(argv[isc_commandline_index+1]); - debug("server is %s", argv[isc_commandline_index+1]); + if (argc == 2) { + set_nameserver(*argv + 1); + debug("server is %s", *argv + 1); listed_server = ISC_TRUE; } else check_ra = ISC_TRUE; @@ -841,7 +839,7 @@ host_main(int argc, char **argv) { exit(1); } - parse_args(ISC_FALSE, argc, argv); + parse_args(argc, argv); setup_system(ipv4only, ipv6only); result = isc_app_onrun(global_task, onrun_callback, NULL); check_result(result, "isc_app_onrun"); diff --git a/usr.sbin/bind/bin/dig/nslookup.c b/usr.sbin/bind/bin/dig/nslookup.c index 1430450c163..c25df607002 100644 --- a/usr.sbin/bind/bin/dig/nslookup.c +++ b/usr.sbin/bind/bin/dig/nslookup.c @@ -21,7 +21,6 @@ #include <isc/app.h> #include <isc/buffer.h> -#include <isc/commandline.h> #include <isc/event.h> #include <isc/parseint.h> diff --git a/usr.sbin/bind/lib/isc/Makefile.inc b/usr.sbin/bind/lib/isc/Makefile.inc index 040563828c6..d43cbb4a4b7 100644 --- a/usr.sbin/bind/lib/isc/Makefile.inc +++ b/usr.sbin/bind/lib/isc/Makefile.inc @@ -1,9 +1,9 @@ -# $OpenBSD: Makefile.inc,v 1.2 2020/02/04 18:02:31 florian Exp $ +# $OpenBSD: Makefile.inc,v 1.3 2020/02/04 19:06:54 florian Exp $ .PATH: ${.CURDIR}/lib/isc SRCS+= assertions.c base32.c base64.c netaddr.c buffer.c bufferlist.c -SRCS+= commandline.c error.c event.c hash.c heap.c hex.c hmacmd5.c hmacsha.c +SRCS+= error.c event.c hash.c heap.c hex.c hmacmd5.c hmacsha.c SRCS+= inet_aton.c iterated_hash.c lex.c log.c md5.c regex.c sockaddr.c SRCS+= task.c result.c netscope.c parseint.c refcount.c region.c timer.c SRCS+= safe.c serial.c sha1.c sha2.c symtab.c diff --git a/usr.sbin/bind/lib/isc/commandline.c b/usr.sbin/bind/lib/isc/commandline.c deleted file mode 100644 index d7ae27990f3..00000000000 --- a/usr.sbin/bind/lib/isc/commandline.c +++ /dev/null @@ -1,213 +0,0 @@ -/* - * Portions Copyright (C) Internet Systems Consortium, Inc. ("ISC") - * - * Permission to use, copy, modify, and/or distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH - * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY - * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, - * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM - * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE - * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR - * PERFORMANCE OF THIS SOFTWARE. - */ - -/* - * Copyright (c) 1987, 1993, 1994 - * The Regents of the University of California. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the University nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -/* $Id: commandline.c,v 1.12 2020/01/22 13:02:09 florian Exp $ */ - -/*! \file - * This file was adapted from the NetBSD project's source tree, RCS ID: - * NetBSD: getopt.c,v 1.15 1999/09/20 04:39:37 lukem Exp - * - * The primary change has been to rename items to the ISC namespace - * and format in the ISC coding style. - */ - -/* - * \author Principal Authors: Computer Systems Research Group at UC Berkeley - * \author Principal ISC caretaker: DCL - */ - - - -#include <stdio.h> - -#include <isc/commandline.h> -#include <isc/msgs.h> - -#include <string.h> -#include <isc/util.h> - -/*% Index into parent argv vector. */ -int isc_commandline_index = 1; -/*% Character checked for validity. */ -int isc_commandline_option; -/*% Argument associated with option. */ -char *isc_commandline_argument; -/*% For printing error messages. */ -char *isc_commandline_progname; -/*% Print error messages. */ -isc_boolean_t isc_commandline_errprint = ISC_TRUE; -/*% Reset processing. */ -isc_boolean_t isc_commandline_reset = ISC_TRUE; - -static char endopt = '\0'; - -#define BADOPT '?' -#define BADARG ':' -#define ENDOPT &endopt - -/*! - * getopt -- - * Parse argc/argv argument vector. - */ -int -isc_commandline_parse(int argc, char * const *argv, const char *options) { - static char *place = ENDOPT; - const char *option; /* Index into *options of option. */ - - REQUIRE(argc >= 0 && argv != NULL && options != NULL); - - /* - * Update scanning pointer, either because a reset was requested or - * the previous argv was finished. - */ - if (isc_commandline_reset || *place == '\0') { - if (isc_commandline_reset) { - isc_commandline_index = 1; - isc_commandline_reset = ISC_FALSE; - } - - if (isc_commandline_progname == NULL) - isc_commandline_progname = argv[0]; - - if (isc_commandline_index >= argc || - *(place = argv[isc_commandline_index]) != '-') { - /* - * Index out of range or points to non-option. - */ - place = ENDOPT; - return (-1); - } - - if (place[1] != '\0' && *++place == '-' && place[1] == '\0') { - /* - * Found '--' to signal end of options. Advance - * index to next argv, the first non-option. - */ - isc_commandline_index++; - place = ENDOPT; - return (-1); - } - } - - isc_commandline_option = *place++; - option = strchr(options, isc_commandline_option); - - /* - * Ensure valid option has been passed as specified by options string. - * '-:' is never a valid command line option because it could not - * distinguish ':' from the argument specifier in the options string. - */ - if (isc_commandline_option == ':' || option == NULL) { - if (*place == '\0') - isc_commandline_index++; - - if (isc_commandline_errprint && *options != ':') - fprintf(stderr, "%s: %s -- %c\n", - isc_commandline_progname, "illegal option", - isc_commandline_option); - - return (BADOPT); - } - - if (*++option != ':') { - /* - * Option does not take an argument. - */ - isc_commandline_argument = NULL; - - /* - * Skip to next argv if at the end of the current argv. - */ - if (*place == '\0') - ++isc_commandline_index; - - } else { - /* - * Option needs an argument. - */ - if (*place != '\0') - /* - * Option is in this argv, -D1 style. - */ - isc_commandline_argument = place; - - else if (argc > ++isc_commandline_index) - /* - * Option is next argv, -D 1 style. - */ - isc_commandline_argument = argv[isc_commandline_index]; - - else { - /* - * Argument needed, but no more argv. - */ - place = ENDOPT; - - /* - * Silent failure with "missing argument" return - * when ':' starts options string, per historical spec. - */ - if (*options == ':') - return (BADARG); - - if (isc_commandline_errprint) - fprintf(stderr, "%s: %s -- %c\n", - isc_commandline_progname, - "option requires an argument", - isc_commandline_option); - - return (BADOPT); - } - - place = ENDOPT; - - /* - * Point to argv that follows argument. - */ - isc_commandline_index++; - } - - return (isc_commandline_option); -} diff --git a/usr.sbin/bind/lib/isc/include/isc/commandline.h b/usr.sbin/bind/lib/isc/include/isc/commandline.h deleted file mode 100644 index 4a2fba016a7..00000000000 --- a/usr.sbin/bind/lib/isc/include/isc/commandline.h +++ /dev/null @@ -1,49 +0,0 @@ -/* - * Copyright (C) Internet Systems Consortium, Inc. ("ISC") - * - * Permission to use, copy, modify, and/or distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH - * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY - * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, - * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM - * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE - * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR - * PERFORMANCE OF THIS SOFTWARE. - */ - -/* $Id: commandline.h,v 1.5 2020/01/22 13:02:10 florian Exp $ */ - -#ifndef ISC_COMMANDLINE_H -#define ISC_COMMANDLINE_H 1 - -/*! \file isc/commandline.h */ - -#include <isc/boolean.h> -#include <isc/lang.h> - - -/*% Index into parent argv vector. */ -extern int isc_commandline_index; -/*% Character checked for validity. */ -extern int isc_commandline_option; -/*% Argument associated with option. */ -extern char *isc_commandline_argument; -/*% For printing error messages. */ -extern char *isc_commandline_progname; -/*% Print error message. */ -extern isc_boolean_t isc_commandline_errprint; -/*% Reset getopt. */ -extern isc_boolean_t isc_commandline_reset; - -ISC_LANG_BEGINDECLS - -/*% parse command line */ -int -isc_commandline_parse(int argc, char * const *argv, const char *options); - -ISC_LANG_ENDDECLS - -#endif /* ISC_COMMANDLINE_H */ |