From: Denys Vlasenko <vda.linux@googlemail.com>
To: musl <musl@lists.openwall.com>, Rich Felker <dalias@libc.org>
Subject: Re: getopt() not exposing __optpos - shell needs it
Date: Tue, 29 Aug 2017 13:32:01 +0200 [thread overview]
Message-ID: <CAK1hOcOC9+5DJ8zaFt1tAXCEKxOCLV2rDHhovq=jWdBZJROnKg@mail.gmail.com> (raw)
In-Reply-To: <20170828152844.GY1627@brightrain.aerifal.cx>
On Mon, Aug 28, 2017 at 5:28 PM, Rich Felker <dalias@libc.org> wrote:
> On Mon, Aug 28, 2017 at 12:18:57PM +0200, Denys Vlasenko wrote:
>> I am using getopt() in busybox hush shell.
>> "unset" builtin, for example: it takes -v and -f options.
>> This works fine.
>>
>> However, POSIX requires that shells has a "getopts" builtin:
>> http://pubs.opengroup.org/onlinepubs/9699919799/utilities/getopts.html
>>
>> It is basically an API binding to access getopt() in the shell code:
>> it uses OPTIND and (in bash) OPTERR on entry, returns a single-char
>> variable on return and updates OPTIND and OPTARG. Sounds familiar, right?
>>
>> When I try to do that (use getopt() to implement "getopts"), it hits a snag.
>> Unlike normal getopt() usage in C programs, where it is called in a loop
>> with the same argv[] array until parsing is finished,
>> when it is used from "getopts", each successive call will (usually) have
>> the same argv[] CONTENTS, but not the ADDRESSES.
>> (The reason is in how shell works: it re-creates command arguments just before
>> running a command, since there can be variable substitution, globbing, etc).
>
> First, some background out of the spec to establish what is supposed
> to work and what's not:
>
> If the application sets OPTIND to the value 1, a new set of
> parameters can be used: either the current positional parameters
> or new arg values. Any other attempt to invoke getopts multiple
> times in a single shell execution environment with parameters
> (positional parameters or arg operands) that are not the same in
> all invocations, or with an OPTIND value modified to be a value
> other than 1, produces unspecified results.
>
> What this means is that, when you use getopts(1), you need to either
> use the exact same arguments (as you said, *string contents*, not
> likely to be the same argv[] pointers) or reset it with OPTIND=1.
>
> It seems to me that the easiest, fully-portable fix is just the
> obvious quadratic-time solution: on each run of getopts(1), reset
> getopt(3) to the start and call it ++N times.
This has several problems:
It prints multiple messages "invalid option -q"
when there are options which are not in optstring.
It mangles optarg if an option without argument follows
an option with an argument.
next prev parent reply other threads:[~2017-08-29 11:32 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-08-28 10:18 Denys Vlasenko
2017-08-28 15:17 ` Denys Vlasenko
2017-08-28 15:28 ` Rich Felker
2017-08-29 11:32 ` Denys Vlasenko [this message]
2017-08-29 12:20 ` Rich Felker
2017-08-29 12:47 ` Denys Vlasenko
2017-08-29 13:07 ` Rich Felker
2017-08-29 16:47 ` Denys Vlasenko
2017-08-29 17:38 ` Rich Felker
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to='CAK1hOcOC9+5DJ8zaFt1tAXCEKxOCLV2rDHhovq=jWdBZJROnKg@mail.gmail.com' \
--to=vda.linux@googlemail.com \
--cc=dalias@libc.org \
--cc=musl@lists.openwall.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://git.vuxu.org/mirror/musl/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).