diff options
author | Timo Teräs <timo.teras@iki.fi> | 2014-12-12 11:07:45 +0200 |
---|---|---|
committer | Timo Teräs <timo.teras@iki.fi> | 2014-12-12 11:08:23 +0200 |
commit | 9be3c06726e81ee7ca393d2232bdb1f04070b40d (patch) | |
tree | cdc6f31b80d81d2bbb6a6ab3025a83aa7e09359b /main/musl | |
parent | 25967063659719b2c6979fb5fb774382873d2dba (diff) | |
download | aports-9be3c06726e81ee7ca393d2232bdb1f04070b40d.tar.bz2 aports-9be3c06726e81ee7ca393d2232bdb1f04070b40d.tar.xz |
Revert "main/musl: use upstream getopt_long"
This reverts commit 21d10d3c2c3687b1475a4616ec9f341e3931f05b.
This is broken still, and was pushed by accident.
Diffstat (limited to 'main/musl')
-rw-r--r-- | main/musl/0019-support-options-after-non-option-arguments-in-getopt.patch | 71 | ||||
-rw-r--r-- | main/musl/0020-support-abbreviated-options-in-getopt_long.patch | 58 | ||||
-rw-r--r-- | main/musl/0021-fix-getopt-handling-of-initial-in-optstring.patch | 29 | ||||
-rw-r--r-- | main/musl/0022-accept-null-longopts-pointer-in-getopt_long.patch | 27 | ||||
-rw-r--r-- | main/musl/APKBUILD | 26 | ||||
-rw-r--r-- | main/musl/getopt_long.c | 618 |
6 files changed, 627 insertions, 202 deletions
diff --git a/main/musl/0019-support-options-after-non-option-arguments-in-getopt.patch b/main/musl/0019-support-options-after-non-option-arguments-in-getopt.patch deleted file mode 100644 index 395165fba1..0000000000 --- a/main/musl/0019-support-options-after-non-option-arguments-in-getopt.patch +++ /dev/null @@ -1,71 +0,0 @@ -From 59f6232f85240f448da0d1fc77b3e09fe36eeed9 Mon Sep 17 00:00:00 2001 -From: Rich Felker <dalias@aerifal.cx> -Date: Wed, 10 Dec 2014 20:25:32 -0500 -Subject: [PATCH] support options after non-option arguments in getopt_long - (argv permutation) - ---- - src/misc/getopt_long.c | 39 +++++++++++++++++++++++++++++++++++++++ - 1 file changed, 39 insertions(+) - -diff --git a/src/misc/getopt_long.c b/src/misc/getopt_long.c -index 3d318ce..643e598 100644 ---- a/src/misc/getopt_long.c -+++ b/src/misc/getopt_long.c -@@ -5,14 +5,53 @@ - - extern int __optpos, __optreset; - -+static void permute(char *const *argv, int dest, int src) -+{ -+ char **av = (char **)argv; -+ char *tmp = av[src]; -+ int i; -+ for (i=src; i>dest; i--) -+ av[i] = av[i-1]; -+ av[dest] = tmp; -+} -+ -+static int __getopt_long_core(int argc, char *const *argv, const char *optstring, const struct option *longopts, int *idx, int longonly); -+ - static int __getopt_long(int argc, char *const *argv, const char *optstring, const struct option *longopts, int *idx, int longonly) - { -+ int ret, skipped, resumed; - if (!optind || __optreset) { - __optreset = 0; - __optpos = 0; - optind = 1; - } - if (optind >= argc || !argv[optind]) return -1; -+ skipped = optind; -+ if (optstring[0] != '+' && optstring[0] != '-') { -+ int i; -+ for (i=optind; ; i++) { -+ if (i >= argc || !argv[i]) return -1; -+ if (argv[i][0] != '-') continue; -+ if (!argv[i][1]) continue; -+ if (argv[i][1] == '-' && !argv[i][2]) return -1; -+ break; -+ } -+ optind = i; -+ } -+ resumed = optind; -+ ret = __getopt_long_core(argc, argv, optstring, longopts, idx, longonly); -+ if (resumed > skipped) { -+ int i, cnt = optind-resumed; -+ for (i=0; i<cnt; i++) -+ permute(argv, skipped, optind-1); -+ optind = skipped + cnt; -+ } -+ return ret; -+} -+ -+static int __getopt_long_core(int argc, char *const *argv, const char *optstring, const struct option *longopts, int *idx, int longonly) -+{ -+ - if (argv[optind][0] == '-' && - ((longonly && argv[optind][1]) || - (argv[optind][1] == '-' && argv[optind][2]))) --- -2.2.0 - diff --git a/main/musl/0020-support-abbreviated-options-in-getopt_long.patch b/main/musl/0020-support-abbreviated-options-in-getopt_long.patch deleted file mode 100644 index 6cc3d9893b..0000000000 --- a/main/musl/0020-support-abbreviated-options-in-getopt_long.patch +++ /dev/null @@ -1,58 +0,0 @@ -From cfd7b4acd55420deedd41e0614c9b614c73c743e Mon Sep 17 00:00:00 2001 -From: Rich Felker <dalias@aerifal.cx> -Date: Wed, 10 Dec 2014 21:26:49 -0500 -Subject: [PATCH] support abbreviated options in getopt_long - ---- - src/misc/getopt_long.c | 25 ++++++++++++++++++------- - 1 file changed, 18 insertions(+), 7 deletions(-) - -diff --git a/src/misc/getopt_long.c b/src/misc/getopt_long.c -index 643e598..469ee92 100644 ---- a/src/misc/getopt_long.c -+++ b/src/misc/getopt_long.c -@@ -56,23 +56,34 @@ static int __getopt_long_core(int argc, char *const *argv, const char *optstring - ((longonly && argv[optind][1]) || - (argv[optind][1] == '-' && argv[optind][2]))) - { -- int i; -- for (i=0; longopts[i].name; i++) { -+ int i, cnt, match; -+ char *opt; -+ for (cnt=i=0; longopts[i].name; i++) { - const char *name = longopts[i].name; -- char *opt = argv[optind]+1; -+ opt = argv[optind]+1; - if (*opt == '-') opt++; - for (; *name && *name == *opt; name++, opt++); -- if (*name || (*opt && *opt != '=')) continue; -+ if (*opt && *opt != '=') continue; -+ match = i; -+ if (!*name) { -+ cnt = 1; -+ break; -+ } -+ cnt++; -+ } -+ if (cnt==1) { -+ i = match; -+ optind++; - if (*opt == '=') { -- if (!longopts[i].has_arg) continue; -+ if (!longopts[i].has_arg) return '?'; - optarg = opt+1; - } else { - if (longopts[i].has_arg == required_argument) { -- if (!(optarg = argv[++optind])) -+ if (!(optarg = argv[optind])) - return ':'; -+ optind++; - } else optarg = NULL; - } -- optind++; - if (idx) *idx = i; - if (longopts[i].flag) { - *longopts[i].flag = longopts[i].val; --- -2.2.0 - diff --git a/main/musl/0021-fix-getopt-handling-of-initial-in-optstring.patch b/main/musl/0021-fix-getopt-handling-of-initial-in-optstring.patch deleted file mode 100644 index b0b0ac1360..0000000000 --- a/main/musl/0021-fix-getopt-handling-of-initial-in-optstring.patch +++ /dev/null @@ -1,29 +0,0 @@ -From d4f7d9c46f0e8f19d70871efb66b0f482935642d Mon Sep 17 00:00:00 2001 -From: Rich Felker <dalias@aerifal.cx> -Date: Wed, 10 Dec 2014 21:29:01 -0500 -Subject: [PATCH] fix getopt handling of initial '+' in optstring - -in the case where an initial '+' was passed in optstring (a -getopt_long feature to suppress argv permutation), getopt would fail -to see a possible subsequent ':', resulting in incorrect handling of -missing arguments. ---- - src/misc/getopt.c | 2 +- - 1 file changed, 1 insertion(+), 1 deletion(-) - -diff --git a/src/misc/getopt.c b/src/misc/getopt.c -index 52aa7a3..e77e460 100644 ---- a/src/misc/getopt.c -+++ b/src/misc/getopt.c -@@ -55,7 +55,7 @@ int getopt(int argc, char * const argv[], const char *optstring) - optpos = 0; - } - -- if (optstring[0] == '-') -+ if (optstring[0] == '-' || optstring[0] == '+') - optstring++; - - i = 0; --- -2.2.0 - diff --git a/main/musl/0022-accept-null-longopts-pointer-in-getopt_long.patch b/main/musl/0022-accept-null-longopts-pointer-in-getopt_long.patch deleted file mode 100644 index 7ac5486e6c..0000000000 --- a/main/musl/0022-accept-null-longopts-pointer-in-getopt_long.patch +++ /dev/null @@ -1,27 +0,0 @@ -From b4ef1830b7863bfba1da4bdad56a20ef398672a8 Mon Sep 17 00:00:00 2001 -From: Rich Felker <dalias@aerifal.cx> -Date: Thu, 11 Dec 2014 01:07:02 -0500 -Subject: [PATCH] accept null longopts pointer in getopt_long - -this is an undocumented feature of GNU getopt_long that the BSD -version also mimics, and is reportedly needed by some programs. ---- - src/misc/getopt_long.c | 2 +- - 1 file changed, 1 insertion(+), 1 deletion(-) - -diff --git a/src/misc/getopt_long.c b/src/misc/getopt_long.c -index 469ee92..c3cf7f9 100644 ---- a/src/misc/getopt_long.c -+++ b/src/misc/getopt_long.c -@@ -52,7 +52,7 @@ static int __getopt_long(int argc, char *const *argv, const char *optstring, con - static int __getopt_long_core(int argc, char *const *argv, const char *optstring, const struct option *longopts, int *idx, int longonly) - { - -- if (argv[optind][0] == '-' && -+ if (longopts && argv[optind][0] == '-' && - ((longonly && argv[optind][1]) || - (argv[optind][1] == '-' && argv[optind][2]))) - { --- -2.2.0 - diff --git a/main/musl/APKBUILD b/main/musl/APKBUILD index d5a86a7eba..d1001181e3 100644 --- a/main/musl/APKBUILD +++ b/main/musl/APKBUILD @@ -2,7 +2,7 @@ # Maintainer: Timo Teräs <timo.teras@iki.fi> pkgname=musl pkgver=1.1.5 -pkgrel=3 +pkgrel=4 pkgdesc="the musl c library (libc) implementation" url="http://www.musl-libc.org/" arch="all" @@ -31,15 +31,12 @@ source="http://www.musl-libc.org/releases/musl-$pkgver.tar.gz 0016-don-t-fail-posix_spawn-on-failed-close.patch 0017-use-direct-syscall-rather-than-write-function-in-pos.patch 0018-don-t-shadow-functions-with-macros-in-C.patch - 0019-support-options-after-non-option-arguments-in-getopt.patch - 0020-support-abbreviated-options-in-getopt_long.patch - 0021-fix-getopt-handling-of-initial-in-optstring.patch - 0022-accept-null-longopts-pointer-in-getopt_long.patch 1001-add-basic-dns-record-parsing-functions.patch 1002-implement-FNM_CASEFOLD-gnu-extension.patch 1003-remove-ulimit-fiddling-from-setxid.patch ldconfig + getopt_long.c __stack_chk_fail_local.c getconf.c getent.c @@ -55,6 +52,10 @@ prepare() { *.patch) msg $i; patch -p1 -i "$srcdir"/$i || return 1;; esac done + + # use GNU compatible getopt() from BSD + rm -f src/misc/getopt*.c + cp "$srcdir"/getopt_long.c src/misc/ } install_sysroot_headers() { @@ -154,14 +155,11 @@ d9b4d921348fa86e57c44bf4c95714bd 0012-fix-uninitialized-output-from-sched_getaf 346d4db3e125b0679a4e5fedcbd99644 0016-don-t-fail-posix_spawn-on-failed-close.patch b3e9f774ca938dcd1833dbe4af44fea8 0017-use-direct-syscall-rather-than-write-function-in-pos.patch c59cc913f8ce2daffe4ceecc136ff81d 0018-don-t-shadow-functions-with-macros-in-C.patch -d73e635f22a12a66d9c49c49b29ddfff 0019-support-options-after-non-option-arguments-in-getopt.patch -15f097770f55d7a501dacc92deb8510d 0020-support-abbreviated-options-in-getopt_long.patch -159a4a48e4e772050b7ed3f08f95954a 0021-fix-getopt-handling-of-initial-in-optstring.patch -7d03bbaec20dd9c8faa3bef613a93da7 0022-accept-null-longopts-pointer-in-getopt_long.patch 2371eb1ce057fcb709a0e6a81f0d356c 1001-add-basic-dns-record-parsing-functions.patch c4c2f5b48af0aef958d191659b9e5192 1002-implement-FNM_CASEFOLD-gnu-extension.patch 71b2a4dcc39c436a6b89173943424043 1003-remove-ulimit-fiddling-from-setxid.patch 830d01f7821b978df770b06db3790921 ldconfig +61c6c1e84ed1df82abbe6d75e90cf21c getopt_long.c 0df687757221bbb0fc1aa67f1bd646f9 __stack_chk_fail_local.c 57ef2c63b9ec6a2041694ace97d4ffa2 getconf.c 2b941c4251cac44988a4abfc50e21267 getent.c @@ -185,14 +183,11 @@ fa2306adeda1a2ee733fcbc64fc8e0bb377b3c0dd6870ea15322722f08d135e1 0014-add-arm-p 598856ea334962a36b3ff99d1909306deb7dfe235b0ab8c5e20e531027fe4cf4 0016-don-t-fail-posix_spawn-on-failed-close.patch 0f7a2a055c87c2a2616b106fcf5d9f35c8a710c12383e652744f7429e0bfae96 0017-use-direct-syscall-rather-than-write-function-in-pos.patch da4d97570b552e332859f66c1a6fe8f4dee66b809e1e9f39fb1a3fc5a6e3574d 0018-don-t-shadow-functions-with-macros-in-C.patch -ea6a42962f7c4d1155f4d0b96fa3368a4724713da6baaaea5425a1e4f14926dd 0019-support-options-after-non-option-arguments-in-getopt.patch -2906e9f7bcdc52f8aa2799ce8a8058ac9a603e5a73185f16c616d736d8ac06b2 0020-support-abbreviated-options-in-getopt_long.patch -d5810bc44bcad4c71aa734ce8ee03101496efb95af2037d0701b849906cc297d 0021-fix-getopt-handling-of-initial-in-optstring.patch -e17e05b8818fef9cbe65f7e7a2e15e0db1aae61067599ae7c9a50b889e7adedd 0022-accept-null-longopts-pointer-in-getopt_long.patch 75053a31f6b84a64846d92c0ec631c76d7f747a9c0dc92a6dc1aa1bddfe2ea76 1001-add-basic-dns-record-parsing-functions.patch 57cb534a603dce32efa48be04ce7b1ca3f287fd8cc72258589fd529e86c1dd47 1002-implement-FNM_CASEFOLD-gnu-extension.patch fb542c2bd5081ff2f601c519edb3dac8f54ca5c888f44bc6cfb84e6565472025 1003-remove-ulimit-fiddling-from-setxid.patch b4a2c06db38742e8c42c3c9838b285a7d8cdac6c091ff3df5ff9a15f1e41b9c7 ldconfig +d9b644ec20bc33e81a7c52b9fcf7973d835923a69faf50f03db45534b811bd96 getopt_long.c 299a7d75a09de3e2e11e7fb4acc3182e4a14e868093d2f30938fce9bfcff13da __stack_chk_fail_local.c d87d0cbb3690ae2c5d8cc218349fd8278b93855dd625deaf7ae50e320aad247c getconf.c 68373a55e89ce85c562d941ccf588337d6cc6c9c17689d695f65cd7607134bbe getent.c @@ -216,14 +211,11 @@ fcd04e66c812d108712d2ad937f117500d73311a0d1390bd6834d2093d844751bb0644f8b4ea820a 54a747fb14b38ffdd0abcd6b202a01e3e778cf853152a4554f7941a2e2544ab1c4c498e2646f421b421180e934ff0ff3372bbd8e08b59b800378cfb2db179940 0016-don-t-fail-posix_spawn-on-failed-close.patch 59060226956ddef64ade61b98a46a6faa5896061186abfa5a8adf1786584beff79acfa6197e2dc7ee1104ad300eda979ad18b6d5466a5dc98ae8957576a5b09d 0017-use-direct-syscall-rather-than-write-function-in-pos.patch a627055d150087fae4d484c60ac7be8cccae0c1b8ddb88684d7b03255c72c1306dc55b7a1ee08ef691691a6b79bfd335609ea5271363c9f4824e48043dd5b037 0018-don-t-shadow-functions-with-macros-in-C.patch -4ff213f1e54def3adb75a8817f5cc6708055b1a557d253493903629fb02750d66666137358eafe30cc8a1f81812379ffe838fe102c02c7b04dead52c77a7e95a 0019-support-options-after-non-option-arguments-in-getopt.patch -36ce03cc336ed2b38d61489ccd65683e3a8d761a1aa155727e9c87e5f7a9c74ee7be0399778a6235cae7aff76c42d34f5005ad066eb5b7ca5859ed8397b910fe 0020-support-abbreviated-options-in-getopt_long.patch -3f721c8aa7d49cc4c51f9bb33ee5efcabc75e702fc1e7fd7aacabb7b26a7548429983903ed2ddba679b29802fe3a205f9f63c2f5cfeb5d930f2d9ef2beb482f3 0021-fix-getopt-handling-of-initial-in-optstring.patch -bebc872ae1797c3d38dd2ae4d1ea44bfbb351d820d9e69dde4a8d6165e52014e31e39c4c3bea2c17bb018a4f84165be149330efb1a82095c3affd13232dfd7ae 0022-accept-null-longopts-pointer-in-getopt_long.patch 5b8ffa0a50419581adbf6ce2dae5797774022551c6331fa5aa2ff13635eb72b74eedd8a92cb478d45d73e1956af2f588669681ac414f3a255abd4d8ba8579448 1001-add-basic-dns-record-parsing-functions.patch f379f2308a8ed04560d08db90935acf56617d1b650f6227212e9ad530b53318aaecfea326ad4727439112b602a810db6857d1fe72d6e3ddeec519c99cd3608d2 1002-implement-FNM_CASEFOLD-gnu-extension.patch dae010b45419fcab64410568466f659cdc874e63113025e2cbc2fbab047b470fec23851ecbef08886505924482a069caf37c16b483b6922535fbd31832f1c4a3 1003-remove-ulimit-fiddling-from-setxid.patch 8d3a2d5315fc56fee7da9abb8b89bb38c6046c33d154c10d168fb35bfde6b0cf9f13042a3bceee34daf091bc409d699223735dcf19f382eeee1f6be34154f26f ldconfig +140f3f20d30bd95ebce8c41b8cc7f616c6cbedf4ea06c729c21014e74f6043796825cc40ebc5180620ea38173afdba23f09ebf6d8b11fa05440b14d23764fca9 getopt_long.c 062bb49fa54839010acd4af113e20f7263dde1c8a2ca359b5fb2661ef9ed9d84a0f7c3bc10c25dcfa10bb3c5a4874588dff636ac43d5dbb3d748d75400756d0b __stack_chk_fail_local.c 0d80f37b34a35e3d14b012257c50862dfeb9d2c81139ea2dfa101d981d093b009b9fa450ba27a708ac59377a48626971dfc58e20a3799084a65777a0c32cbc7d getconf.c b35de9847353b273516162ed4828a810c6130fc5b7de44ee4433003b3f99647b25792d9b1c40dfc67069add11f3fb850e5c35d4f1912dccac108059bbbdfd5a2 getent.c diff --git a/main/musl/getopt_long.c b/main/musl/getopt_long.c new file mode 100644 index 0000000000..65e19940d3 --- /dev/null +++ b/main/musl/getopt_long.c @@ -0,0 +1,618 @@ +/* + * Copyright (c) 2002 Todd C. Miller <Todd.Miller@courtesan.com> + * + * Permission to use, copy, modify, and 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 THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR 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. + * + * Sponsored in part by the Defense Advanced Research Projects + * Agency (DARPA) and Air Force Research Laboratory, Air Force + * Materiel Command, USAF, under agreement number F39502-99-1-0512. + */ +/*- + * Copyright (c) 2000 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Dieter Baron and Thomas Klausner. + * + * 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. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the NetBSD + * Foundation, Inc. and its contributors. + * 4. Neither the name of The NetBSD Foundation 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 NETBSD FOUNDATION, INC. 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 FOUNDATION 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. + */ + +#define _GNU_SOURCE +#include <errno.h> +#include <stdio.h> +#include <getopt.h> +#include <stdlib.h> +#include <string.h> + +#define GNU_COMPATIBLE /* Be more compatible, configure's use us! */ + +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 */ + +#define PRINT_ERROR ((opterr) && (*options != ':')) + +#define FLAG_PERMUTE 0x01 /* permute non-options to the end of argv */ +#define FLAG_ALLARGS 0x02 /* treat non-options as args to option "-1" */ +#define FLAG_LONGONLY 0x04 /* operate as getopt_long_only */ + +/* return values */ +#define BADCH (int)'?' +#define BADARG ((*options == ':') ? (int)':' : (int)'?') +#define INORDER (int)1 + +#define EMSG "" + +#ifdef GNU_COMPATIBLE +#define NO_PREFIX (-1) +#define D_PREFIX 0 +#define DD_PREFIX 1 +#define W_PREFIX 2 +#endif + +static int getopt_internal(int, char * const *, const char *, + const struct option *, int *, int); +static int parse_long_options(char * const *, const char *, + const struct option *, int *, int, int); +static int gcd(int, int); +static void permute_args(int, int, int, char * const *); + +static 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) */ +static int nonopt_end = -1; /* first option after non options (for permute) */ + +/* Error messages */ +static const char recargchar[] = "option requires an argument -- %c\n"; +static const char illoptchar[] = "illegal option -- %c\n"; /* From P1003.2 */ +#ifdef GNU_COMPATIBLE +static int dash_prefix = NO_PREFIX; +static const char gnuoptchar[] = "invalid option -- %c\n"; + +static const char recargstring[] = "option `%s%s' requires an argument\n"; +static const char ambig[] = "option `%s%.*s' is ambiguous\n"; +static const char noarg[] = "option `%s%.*s' doesn't allow an argument\n"; +static const char illoptstring[] = "unrecognized option `%s%s'\n"; +#else +static const char recargstring[] = "option requires an argument -- %s\n"; +static const char ambig[] = "ambiguous option -- %.*s\n"; +static const char noarg[] = "option doesn't take an argument -- %.*s\n"; +static const char illoptstring[] = "unknown option -- %s\n"; +#endif + +/* + * Compute the greatest common divisor of a and b. + */ +static int +gcd(int a, int b) +{ + int c; + + c = a % b; + while (c != 0) { + a = b; + b = c; + c = a % b; + } + + return (b); +} + +/* + * Exchange the block from nonopt_start to nonopt_end with the block + * from nonopt_end to opt_end (keeping the same order of arguments + * in each block). + */ +static void +permute_args(int panonopt_start, int panonopt_end, int opt_end, + char * const *nargv) +{ + int cstart, cyclelen, i, j, ncycle, nnonopts, nopts, pos; + char *swap; + + /* + * compute lengths of blocks and number and size of cycles + */ + nnonopts = panonopt_end - panonopt_start; + nopts = opt_end - panonopt_end; + ncycle = gcd(nnonopts, nopts); + cyclelen = (opt_end - panonopt_start) / ncycle; + + for (i = 0; i < ncycle; i++) { + cstart = panonopt_end+i; + pos = cstart; + for (j = 0; j < cyclelen; j++) { + if (pos >= panonopt_end) + pos -= nnonopts; + else + pos += nopts; + swap = nargv[pos]; + /* LINTED const cast */ + ((char **) nargv)[pos] = nargv[cstart]; + /* LINTED const cast */ + ((char **)nargv)[cstart] = swap; + } + } +} + +/* + * parse_long_options -- + * Parse long options in argc/argv argument vector. + * Returns -1 if short_too is set and the option does not match long_options. + */ +static int +parse_long_options(char * const *nargv, const char *options, + const struct option *long_options, int *idx, int short_too, int flags) +{ + char *current_argv, *has_equal; +#ifdef GNU_COMPATIBLE + char *current_dash; +#endif + size_t current_argv_len; + int i, match, exact_match, second_partial_match; + + current_argv = place; +#ifdef GNU_COMPATIBLE + switch (dash_prefix) { + case D_PREFIX: + current_dash = "-"; + break; + case DD_PREFIX: + current_dash = "--"; + break; + case W_PREFIX: + current_dash = "-W "; + break; + default: + current_dash = ""; + break; + } +#endif + match = -1; + exact_match = 0; + second_partial_match = 0; + + optind++; + + if ((has_equal = strchr(current_argv, '=')) != NULL) { + /* argument found (--option=arg) */ + current_argv_len = has_equal - current_argv; + has_equal++; + } else + current_argv_len = strlen(current_argv); + + for (i = 0; long_options[i].name; i++) { + /* find matching long option */ + if (strncmp(current_argv, long_options[i].name, + current_argv_len)) + continue; + + if (strlen(long_options[i].name) == current_argv_len) { + /* exact match */ + match = i; + exact_match = 1; + break; + } + /* + * If this is a known short option, don't allow + * a partial match of a single character. + */ + if (short_too && current_argv_len == 1) + continue; + + if (match == -1) /* first partial match */ + match = i; + else if ((flags & FLAG_LONGONLY) || + long_options[i].has_arg != + long_options[match].has_arg || + long_options[i].flag != long_options[match].flag || + long_options[i].val != long_options[match].val) + second_partial_match = 1; + } + if (!exact_match && second_partial_match) { + /* ambiguous abbreviation */ + if (PRINT_ERROR) { + fprintf(stderr, ambig, +#ifdef GNU_COMPATIBLE + current_dash, +#endif + (int)current_argv_len, + current_argv); + } + optopt = 0; + return (BADCH); + } + if (match != -1) { /* option found */ + if (long_options[match].has_arg == no_argument + && has_equal) { + if (PRINT_ERROR) { + fprintf(stderr, noarg, +#ifdef GNU_COMPATIBLE + current_dash, +#endif + (int)current_argv_len, + current_argv); + } + /* + * XXX: GNU sets optopt to val regardless of flag + */ + if (long_options[match].flag == NULL) + optopt = long_options[match].val; + else + optopt = 0; +#ifdef GNU_COMPATIBLE + return (BADCH); +#else + return (BADARG); +#endif + } + if (long_options[match].has_arg == required_argument || + long_options[match].has_arg == optional_argument) { + if (has_equal) + optarg = has_equal; + else if (long_options[match].has_arg == + required_argument) { + /* + * optional argument doesn't use next nargv + */ + optarg = nargv[optind++]; + } + } + if ((long_options[match].has_arg == required_argument) + && (optarg == NULL)) { + /* + * Missing argument; leading ':' indicates no error + * should be generated. + */ + if (PRINT_ERROR) { + fprintf(stderr, recargstring, +#ifdef GNU_COMPATIBLE + current_dash, +#endif + current_argv); + } + /* + * XXX: GNU sets optopt to val regardless of flag + */ + if (long_options[match].flag == NULL) + optopt = long_options[match].val; + else + optopt = 0; + --optind; + return (BADARG); + } + } else { /* unknown option */ + if (short_too) { + --optind; + return (-1); + } + if (PRINT_ERROR) { + fprintf(stderr, illoptstring, +#ifdef GNU_COMPATIBLE + current_dash, +#endif + current_argv); + } + optopt = 0; + return (BADCH); + } + if (idx) + *idx = match; + if (long_options[match].flag) { + *long_options[match].flag = long_options[match].val; + return (0); + } else + return (long_options[match].val); +} + +/* + * getopt_internal -- + * Parse argc/argv argument vector. Called by user level routines. + */ +static int +getopt_internal(int nargc, char * const *nargv, const char *options, + const struct option *long_options, int *idx, int flags) +{ + char *oli; /* option letter list index */ + int optchar, short_too; + int posixly_correct; /* no static, can be changed on the fly */ + + if (options == NULL) + return (-1); + + /* + * Disable GNU extensions if POSIXLY_CORRECT is set or options + * string begins with a '+'. + */ + posixly_correct = (getenv("POSIXLY_CORRECT") != NULL); +#ifdef GNU_COMPATIBLE + if (*options == '-') + flags |= FLAG_ALLARGS; + else if (posixly_correct || *options == '+') + flags &= ~FLAG_PERMUTE; +#else + if (posixly_correct || *options == '+') + flags &= ~FLAG_PERMUTE; + else if (*options == '-') + flags |= FLAG_ALLARGS; +#endif +#if HAVE_STRICT_MODE >= 1 + flags &= ~FLAG_PERMUTE; +#endif + if (*options == '+' || *options == '-') + options++; + + /* + * XXX Some GNU programs (like cvs) set optind to 0 instead of + * XXX using optreset. Work around this braindamage. + */ + if (optind == 0) + optind = optreset = 1; + + optarg = NULL; + if (optreset) + nonopt_start = nonopt_end = -1; +start: + if (optreset || !*place) { /* update scanning pointer */ + optreset = 0; + if (optind >= nargc) { /* end of argument vector */ + place = EMSG; + if (nonopt_end != -1) { + /* do permutation, if we have to */ + permute_args(nonopt_start, nonopt_end, + optind, nargv); + optind -= nonopt_end - nonopt_start; + } + else if (nonopt_start != -1) { + /* + * If we skipped non-options, set optind + * to the first of them. + */ + optind = nonopt_start; + } + nonopt_start = nonopt_end = -1; + return (-1); + } + if (*(place = nargv[optind]) != '-' || +#ifdef GNU_COMPATIBLE + place[1] == '\0') { +#else + (place[1] == '\0' && strchr(options, '-') == NULL)) { +#endif + place = EMSG; /* found non-option */ + if (flags & FLAG_ALLARGS) { + /* + * GNU extension: + * return non-option as argument to option 1 + */ + optarg = nargv[optind++]; + return (INORDER); + } + if (!(flags & FLAG_PERMUTE)) { + /* + * If no permutation wanted, stop parsing + * at first non-option. + */ + return (-1); + } + /* do permutation */ + if (nonopt_start == -1) + nonopt_start = optind; + else if (nonopt_end != -1) { + permute_args(nonopt_start, nonopt_end, + optind, nargv); + nonopt_start = optind - + (nonopt_end - nonopt_start); + nonopt_end = -1; + } + optind++; + /* process next argument */ + goto start; + } + if (nonopt_start != -1 && nonopt_end == -1) + nonopt_end = optind; + + /* + * If we have "-" do nothing, if "--" we are done. + */ + if (place[1] != '\0' && *++place == '-' && place[1] == '\0') { + optind++; + place = EMSG; + /* + * We found an option (--), so if we skipped + * non-options, we have to permute. + */ + if (nonopt_end != -1) { + permute_args(nonopt_start, nonopt_end, + optind, nargv); + optind -= nonopt_end - nonopt_start; + } + nonopt_start = nonopt_end = -1; + return (-1); + } + } + + /* + * Check long options if: + * 1) we were passed some + * 2) the arg is not just "-" + * 3) either the arg starts with -- we are getopt_long_only() + */ + if (long_options != NULL && place != nargv[optind] && + (*place == '-' || (flags & FLAG_LONGONLY))) { + short_too = 0; +#ifdef GNU_COMPATIBLE + dash_prefix = D_PREFIX; +#endif + if (*place == '-') { + place++; /* --foo long option */ +#ifdef GNU_COMPATIBLE + dash_prefix = DD_PREFIX; +#endif + } else if (*place != ':' && strchr(options, *place) != NULL) + short_too = 1; /* could be short option too */ + + optchar = parse_long_options(nargv, options, long_options, + idx, short_too, flags); + if (optchar != -1) { + place = EMSG; + return (optchar); + } + } + + if ((optchar = (int)*place++) == (int)':' || + (optchar == (int)'-' && *place != '\0') || + (oli = strchr(options, optchar)) == NULL) { + /* + * If the user specified "-" and '-' isn't listed in + * options, return -1 (non-option) as per POSIX. + * Otherwise, it is an unknown option character (or ':'). + */ + if (optchar == (int)'-' && *place == '\0') + return (-1); + if (!*place) + ++optind; +#ifdef GNU_COMPATIBLE + if (PRINT_ERROR) { + fprintf(stderr, posixly_correct ? illoptchar : gnuoptchar, + optchar); + } +#else + if (PRINT_ERROR) { + fprintf(stderr, illoptchar, optchar); + } +#endif + optopt = optchar; + return (BADCH); + } + if (long_options != NULL && optchar == 'W' && oli[1] == ';') { + /* -W long-option */ + if (*place) /* no space */ + /* NOTHING */; + else if (++optind >= nargc) { /* no arg */ + place = EMSG; + if (PRINT_ERROR) { + fprintf(stderr, recargchar, optchar); + } + optopt = optchar; + return (BADARG); + } else /* white space */ + place = nargv[optind]; +#ifdef GNU_COMPATIBLE + dash_prefix = W_PREFIX; +#endif + optchar = parse_long_options(nargv, options, long_options, + idx, 0, flags); + place = EMSG; + return (optchar); + } + if (*++oli != ':') { /* doesn't take argument */ + if (!*place) + ++optind; + } else { /* takes (optional) argument */ + optarg = NULL; + if (*place) /* no white space */ + optarg = place; + else if (oli[1] != ':') { /* arg not optional */ + if (++optind >= nargc) { /* no arg */ + place = EMSG; + if (PRINT_ERROR) { + fprintf(stderr, recargchar, optchar); + } + optopt = optchar; + return (BADARG); + } else + optarg = nargv[optind]; + } + place = EMSG; + ++optind; + } + /* dump back option letter */ + return (optchar); +} + +/* + * getopt -- + * Parse argc/argv argument vector. + * + * [eventually this will replace the BSD getopt] + */ +int +getopt(int nargc, char * const *nargv, const char *options) +{ + + /* + * We don't pass FLAG_PERMUTE to getopt_internal() since + * the BSD getopt(3) (unlike GNU) has never done this. + * + * Furthermore, since many privileged programs call getopt() + * before dropping privileges it makes sense to keep things + * as simple (and bug-free) as possible. + */ + return (getopt_internal(nargc, nargv, options, NULL, NULL, 0)); +} + +/* + * getopt_long -- + * Parse argc/argv argument vector. + */ +int +getopt_long(int nargc, char * const *nargv, const char *options, + const struct option *long_options, int *idx) +{ + + return (getopt_internal(nargc, nargv, options, long_options, idx, + FLAG_PERMUTE)); +} + +/* + * getopt_long_only -- + * Parse argc/argv argument vector. + */ +int +getopt_long_only(int nargc, char * const *nargv, const char *options, + const struct option *long_options, int *idx) +{ + + return (getopt_internal(nargc, nargv, options, long_options, idx, + FLAG_PERMUTE|FLAG_LONGONLY)); +} |