From ff3af0a9df08c9b8966a9e1ad6f6565816951876 Mon Sep 17 00:00:00 2001 From: joerg Date: Sun, 22 Mar 2009 22:33:13 +0000 Subject: libnbcompat-20090322: Don't use getopt_long as replacement for getopt. It behaves different and doesn't work well in combination with optreset. Instead use getopt(3) from NetBSD if requested. Sync getopt_long.c with NetBSD while here. Addresses issues with pkg_admin on Solaris reported by Tim Zingelman. --- pkgtools/libnbcompat/Makefile | 4 +- pkgtools/libnbcompat/files/configure | 28 +++--- pkgtools/libnbcompat/files/configure.ac | 18 +--- pkgtools/libnbcompat/files/getopt.c | 149 +++++++++++++++++++++++++++++++ pkgtools/libnbcompat/files/getopt_long.c | 96 +++++++++----------- 5 files changed, 210 insertions(+), 85 deletions(-) create mode 100644 pkgtools/libnbcompat/files/getopt.c (limited to 'pkgtools') diff --git a/pkgtools/libnbcompat/Makefile b/pkgtools/libnbcompat/Makefile index bff3eda6c22..b0085b00b66 100644 --- a/pkgtools/libnbcompat/Makefile +++ b/pkgtools/libnbcompat/Makefile @@ -1,11 +1,11 @@ -# $NetBSD: Makefile,v 1.69 2009/03/02 13:54:55 joerg Exp $ +# $NetBSD: Makefile,v 1.70 2009/03/22 22:33:13 joerg Exp $ # # NOTE: If you update this package, it is *mandatory* that you update # pkgsrc/pkgtools/libnbcompat/files/README to reflect the actual # list of tested and supported platforms. # -DISTNAME= libnbcompat-20090302 +DISTNAME= libnbcompat-20090322 CATEGORIES= pkgtools devel MASTER_SITES= # empty DISTFILES= # empty diff --git a/pkgtools/libnbcompat/files/configure b/pkgtools/libnbcompat/files/configure index 71f3cc781a7..78f85c32715 100755 --- a/pkgtools/libnbcompat/files/configure +++ b/pkgtools/libnbcompat/files/configure @@ -5910,7 +5910,6 @@ fi -pkg_use_nbcompat_getopt_long=yes for ac_header in getopt.h do @@ -6112,9 +6111,14 @@ fi { $as_echo "$as_me:$LINENO: result: $pkg_cv_have_struct_option" >&5 $as_echo "$pkg_cv_have_struct_option" >&6; } if test "x$pkg_cv_have_struct_option" = "xyes"; then - pkg_use_nbcompat_getopt_long=no + : else - pkg_use_nbcompat_getopt_long=yes + case " $LIBOBJS " in + *" getopt_long.$ac_objext "* ) ;; + *) LIBOBJS="$LIBOBJS getopt_long.$ac_objext" + ;; +esac + : fi @@ -6126,23 +6130,13 @@ done if test "$enable_bsd_getopt" = yes; then if test "$ac_cv_have_decl_optreset" = no; then - pkg_use_nbcompat_getopt_long=yes - fi -fi - -if test $pkg_use_nbcompat_getopt_long = yes; then - cat >>confdefs.h <<\_ACEOF -#define HAVE_NBCOMPAT_GETOPT_LONG 1 -_ACEOF - - - - case " $LIBOBJS " in - *" getopt_long.$ac_objext "* ) ;; - *) LIBOBJS="$LIBOBJS getopt_long.$ac_objext" + case " $LIBOBJS " in + *" getopt.$ac_objext "* ) ;; + *) LIBOBJS="$LIBOBJS getopt.$ac_objext" ;; esac + fi fi pkg_use_nbcompat_vis=yes diff --git a/pkgtools/libnbcompat/files/configure.ac b/pkgtools/libnbcompat/files/configure.ac index b90025d491f..f6ddf8dfe1b 100644 --- a/pkgtools/libnbcompat/files/configure.ac +++ b/pkgtools/libnbcompat/files/configure.ac @@ -1,4 +1,4 @@ -dnl $NetBSD: configure.ac,v 1.71 2009/02/09 20:35:21 joerg Exp $ +dnl $NetBSD: configure.ac,v 1.72 2009/03/22 22:33:13 joerg Exp $ dnl Process this file with autoconf to produce a configure script. AC_PREREQ(2.52) @@ -148,29 +148,19 @@ AC_CHECK_DECLS([optind, optreset], [:], [:], [ ]) -pkg_use_nbcompat_getopt_long=yes AC_CHECK_HEADERS([getopt.h], [ AC_MSG_TRY_COMPILE([for struct option], pkg_cv_have_struct_option, [ #include ], [ struct option X; ], - [pkg_use_nbcompat_getopt_long=no], - [pkg_use_nbcompat_getopt_long=yes]) + [:], + [AC_LIBOBJ(getopt_long)]) ]) if test "$enable_bsd_getopt" = yes; then if test "$ac_cv_have_decl_optreset" = no; then - pkg_use_nbcompat_getopt_long=yes + AC_LIBOBJ(getopt) fi fi -if test $pkg_use_nbcompat_getopt_long = yes; then - AC_DEFINE(HAVE_NBCOMPAT_GETOPT_LONG) - AH_TEMPLATE([HAVE_NBCOMPAT_GETOPT_LONG], [ - Define to 1 if the `getopt_long' function is - built into the library. - ]) - AC_LIBOBJ(getopt_long) -fi - dnl Check that this vis.h has *vis()-related functions because Solaris' dnl vis.h doesn't. Also, only consider the header found if it defines dnl all of the functions that we need. diff --git a/pkgtools/libnbcompat/files/getopt.c b/pkgtools/libnbcompat/files/getopt.c new file mode 100644 index 00000000000..2d06b8eabb8 --- /dev/null +++ b/pkgtools/libnbcompat/files/getopt.c @@ -0,0 +1,149 @@ +/* $NetBSD: getopt.c,v 1.1 2009/03/22 22:33:13 joerg Exp $ */ + +/* + * 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. + */ + +#if HAVE_NBTOOL_CONFIG_H +#include "nbtool_config.h" +#endif + +#include +#include + +__RCSID("$NetBSD: getopt.c,v 1.1 2009/03/22 22:33:13 joerg Exp $"); + +#if 0 +#include "namespace.h" +#endif + +#include +#include +#include +#include +#include +#include + +#undef __UNCONST +#define __UNCONST(a) ((void *)(size_t)(const void *)(a)) + +#if 0 +#ifdef __weak_alias +__weak_alias(getopt,_getopt) +#endif +#endif + +int opterr = 1, /* if error message should be printed */ + optind = 1, /* index into parent argv vector */ + optopt, /* character checked for validity */ + optreset; /* reset getopt */ +char *optarg; /* argument associated with option */ + +#define BADCH (int)'?' +#define BADARG (int)':' +#define EMSG "" + +/* + * getopt -- + * Parse argc/argv argument vector. + */ +int +getopt(int nargc, char * const nargv[], const char *ostr) +{ + static const char *place = EMSG; /* option letter processing */ + char *oli; /* option letter list index */ + + _DIAGASSERT(nargv != NULL); + _DIAGASSERT(ostr != NULL); + + if (optreset || *place == 0) { /* update scanning pointer */ + optreset = 0; + place = nargv[optind]; + if (optind >= nargc || *place++ != '-') { + /* Argument is absent or is not an option */ + place = EMSG; + return (-1); + } + optopt = *place++; + if (optopt == '-' && *place == 0) { + /* "--" => end of options */ + ++optind; + place = EMSG; + return (-1); + } + if (optopt == 0) { + /* Solitary '-', treat as a '-' option + if the program (eg su) is looking for it. */ + place = EMSG; + if (strchr(ostr, '-') == NULL) + return -1; + optopt = '-'; + } + } else + optopt = *place++; + + /* See if option letter is one the caller wanted... */ + if (optopt == ':' || (oli = strchr(ostr, optopt)) == NULL) { + if (*place == 0) + ++optind; + if (opterr && *ostr != ':') + (void)fprintf(stderr, + "%s: unknown option -- %c\n", getprogname(), + optopt); + return (BADCH); + } + + /* Does this option need an argument? */ + if (oli[1] != ':') { + /* don't need argument */ + optarg = NULL; + if (*place == 0) + ++optind; + } else { + /* Option-argument is either the rest of this argument or the + entire next argument. */ + if (*place) + optarg = __UNCONST(place); + else if (nargc > ++optind) + optarg = nargv[optind]; + else { + /* option-argument absent */ + place = EMSG; + if (*ostr == ':') + return (BADARG); + if (opterr) + (void)fprintf(stderr, + "%s: option requires an argument -- %c\n", + getprogname(), optopt); + return (BADCH); + } + place = EMSG; + ++optind; + } + return (optopt); /* return option letter */ +} diff --git a/pkgtools/libnbcompat/files/getopt_long.c b/pkgtools/libnbcompat/files/getopt_long.c index cd4a6173d12..84fe1b3b739 100644 --- a/pkgtools/libnbcompat/files/getopt_long.c +++ b/pkgtools/libnbcompat/files/getopt_long.c @@ -1,4 +1,4 @@ -/* $NetBSD: getopt_long.c,v 1.9 2008/04/29 05:46:08 martin Exp $ */ +/* $NetBSD: getopt_long.c,v 1.10 2009/03/22 22:33:13 joerg Exp $ */ /*- * Copyright (c) 2000 The NetBSD Foundation, Inc. @@ -35,9 +35,7 @@ #include #include -#if defined(LIBC_SCCS) && !defined(lint) -__RCSID("$NetBSD: getopt_long.c,v 1.9 2008/04/29 05:46:08 martin Exp $"); -#endif /* LIBC_SCCS and not lint */ +__RCSID("$NetBSD: getopt_long.c,v 1.10 2009/03/22 22:33:13 joerg Exp $"); #if 0 #include "namespace.h" @@ -45,9 +43,7 @@ __RCSID("$NetBSD: getopt_long.c,v 1.9 2008/04/29 05:46:08 martin Exp $"); #include #include -#if HAVE_ERRNO_H #include -#endif #if HAVE_NBTOOL_CONFIG_H #include "compat_getopt.h" #else @@ -56,23 +52,24 @@ __RCSID("$NetBSD: getopt_long.c,v 1.9 2008/04/29 05:46:08 martin Exp $"); #include #include -#if (HAVE_NBTOOL_CONFIG_H && !HAVE_GETOPT_LONG && !HAVE_DECL_OPTIND) || 1 +#undef __UNCONST +#define __UNCONST(a) ((void *)(size_t)(const void *)(a)) + +#if HAVE_NBTOOL_CONFIG_H && !HAVE_GETOPT_LONG && !HAVE_DECL_OPTIND #define REPLACE_GETOPT #endif #ifdef REPLACE_GETOPT -#if 0 #ifdef __weak_alias __weak_alias(getopt,_getopt) #endif -#endif int opterr = 1; /* if error message should be printed */ int optind = 1; /* index into parent argv vector */ int optopt = '?'; /* character checked for validity */ int optreset; /* reset getopt */ char *optarg; /* argument associated with option */ #elif HAVE_NBTOOL_CONFIG_H && !HAVE_DECL_OPTRESET -static int optreset; +int optreset; #endif #if 0 @@ -81,7 +78,6 @@ __weak_alias(getopt_long,_getopt_long) #endif #endif -#if !HAVE_GETOPT_LONG #define IGNORE_FIRST (*options == '-' || *options == '+') #define PRINT_ERROR ((opterr) && ((*options != ':') \ || (IGNORE_FIRST && options[1] != ':'))) @@ -98,11 +94,11 @@ __weak_alias(getopt_long,_getopt_long) #define EMSG "" -static int getopt_internal __P((int, char * const *, const char *)); -static int gcd __P((int, int)); -static void permute_args __P((int, int, int, char * const *)); +static int getopt_internal(int, char **, const char *); +static int gcd(int, int); +static void permute_args(int, int, int, char **); -static char *place = EMSG; /* option letter processing */ +static const char *place = EMSG; /* option letter processing */ /* XXX: set optreset to 1 rather than these two */ static int nonopt_start = -1; /* first non option argument (for permute) */ @@ -121,9 +117,7 @@ static const char illoptstring[] = "unknown option -- %s"; * Compute the greatest common divisor of a and b. */ static int -gcd(a, b) - int a; - int b; +gcd(int a, int b) { int c; @@ -143,11 +137,7 @@ gcd(a, b) * in each block). */ static void -permute_args(panonopt_start, panonopt_end, opt_end, nargv) - int panonopt_start; - int panonopt_end; - int opt_end; - char * const *nargv; +permute_args(int panonopt_start, int panonopt_end, int opt_end, char **nargv) { int cstart, cyclelen, i, j, ncycle, nnonopts, nopts, pos; char *swap; @@ -171,10 +161,8 @@ permute_args(panonopt_start, panonopt_end, opt_end, nargv) else pos += nopts; swap = nargv[pos]; - /* LINTED const cast */ - ((char **) nargv)[pos] = nargv[cstart]; - /* LINTED const cast */ - ((char **)nargv)[cstart] = swap; + nargv[pos] = nargv[cstart]; + nargv[cstart] = swap; } } } @@ -185,10 +173,7 @@ permute_args(panonopt_start, panonopt_end, opt_end, nargv) * Returns -2 if -- is found (can be long option or end of options marker). */ static int -getopt_internal(nargc, nargv, options) - int nargc; - char * const *nargv; - const char *options; +getopt_internal(int nargc, char **nargv, const char *options) { char *oli; /* option letter list index */ int optchar; @@ -303,7 +288,7 @@ start: } else { /* takes (optional) argument */ optarg = NULL; if (*place) /* no white space */ - optarg = place; + optarg = __UNCONST(place); /* XXX: disable test for :: if PC? (GNU doesn't) */ else if (oli[1] != ':') { /* arg not optional */ if (++optind >= nargc) { /* no arg */ @@ -340,7 +325,8 @@ getopt(nargc, nargv, options) _DIAGASSERT(nargv != NULL); _DIAGASSERT(options != NULL); - if ((retval = getopt_internal(nargc, nargv, options)) == -2) { + retval = getopt_internal(nargc, __UNCONST(nargv), options); + if (retval == -2) { ++optind; /* * We found an option (--), so if we skipped non-options, @@ -363,27 +349,30 @@ getopt(nargc, nargv, options) * Parse argc/argv argument vector. */ int -getopt_long(nargc, nargv, options, long_options, idx) - int nargc; - char * const *nargv; - const char *options; - const struct option *long_options; - int *idx; +getopt_long(int nargc, char * const *nargv, const char *options, + const struct option *long_options, int *idx) { int retval; +#define IDENTICAL_INTERPRETATION(_x, _y) \ + (long_options[(_x)].has_arg == long_options[(_y)].has_arg && \ + long_options[(_x)].flag == long_options[(_y)].flag && \ + long_options[(_x)].val == long_options[(_y)].val) + _DIAGASSERT(nargv != NULL); _DIAGASSERT(options != NULL); _DIAGASSERT(long_options != NULL); /* idx may be NULL */ - if ((retval = getopt_internal(nargc, nargv, options)) == -2) { + retval = getopt_internal(nargc, __UNCONST(nargv), options); + if (retval == -2) { char *current_argv, *has_equal; size_t current_argv_len; - int i, match; + int i, ambiguous, match; - current_argv = place; + current_argv = __UNCONST(place); match = -1; + ambiguous = 0; optind++; place = EMSG; @@ -395,7 +384,7 @@ getopt_long(nargc, nargv, options, long_options, idx) */ if (nonopt_end != -1) { permute_args(nonopt_start, nonopt_end, - optind, nargv); + optind, __UNCONST(nargv)); optind -= nonopt_end - nonopt_start; } nonopt_start = nonopt_end = -1; @@ -418,18 +407,21 @@ getopt_long(nargc, nargv, options, long_options, idx) (unsigned)current_argv_len) { /* exact match */ match = i; + ambiguous = 0; break; } if (match == -1) /* partial match */ match = i; - else { - /* ambiguous abbreviation */ - if (PRINT_ERROR) - warnx(ambig, (int)current_argv_len, - current_argv); - optopt = 0; - return BADCH; - } + else if (!IDENTICAL_INTERPRETATION(i, match)) + ambiguous = 1; + } + if (ambiguous) { + /* ambiguous abbreviation */ + if (PRINT_ERROR) + warnx(ambig, (int)current_argv_len, + current_argv); + optopt = 0; + return BADCH; } if (match != -1) { /* option found */ if (long_options[match].has_arg == no_argument @@ -494,5 +486,5 @@ getopt_long(nargc, nargv, options, long_options, idx) *idx = match; } return retval; +#undef IDENTICAL_INTERPRETATION } -#endif /* !GETOPT_LONG */ -- cgit v1.2.3