u-boot/lib/getopt.c
Sean Anderson 72eda5074b lib: Add getopt
Some commands can get very unweildy if they have too many positional
arguments. Adding options makes them easier to read, remember, and
understand.

This implementation of getopt has been taken from barebox, which has had
option support for quite a while. I have made a few modifications to their
version, such as the removal of opterr in favor of a separate getopt_silent
function. In addition, I have moved all global variables into struct
getopt_context.

The getopt from barebox also re-orders the arguments passed to it so that
non-options are placed last. This allows users to specify options anywhere.
For example, `ls -l foo/ -R` would be re-ordered to `ls -l -R foo/` as
getopt parsed the options. However, this feature conflicts with the const
argv in cmd_tbl->cmd. This was originally added in 54841ab50c ("Make sure
that argv[] argument pointers are not modified."). The reason stated in
that commit is that hush requires argv to stay unmodified. Has this
situation changed? Barebox also uses hush, and does not have this problem.
Perhaps we could use their fix?

I have assigned maintenance of getopt to Simon Glass, as it is currently
only used by the log command. I would also be fine maintaining it.

Signed-off-by: Sean Anderson <seanga2@gmail.com>
2020-10-30 10:56:11 -04:00

125 lines
2.7 KiB
C

// SPDX-License-Identifier: GPL-2.0-only
/*
* getopt.c - a simple getopt(3) implementation. See getopt.h for explanation.
*
* Copyright (C) 2020 Sean Anderson <seanga2@gmail.com>
* Copyright (c) 2007 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix
*/
#define LOG_CATEGORY LOGC_CORE
#include <common.h>
#include <getopt.h>
#include <log.h>
void getopt_init_state(struct getopt_state *gs)
{
gs->index = 1;
gs->arg_index = 1;
}
int __getopt(struct getopt_state *gs, int argc, char *const argv[],
const char *optstring, bool silent)
{
char curopt; /* current option character */
const char *curoptp; /* pointer to the current option in optstring */
while (1) {
log_debug("arg_index: %d index: %d\n", gs->arg_index,
gs->index);
/* `--` indicates the end of options */
if (gs->arg_index == 1 && argv[gs->index] &&
!strcmp(argv[gs->index], "--")) {
gs->index++;
return -1;
}
/* Out of arguments */
if (gs->index >= argc)
return -1;
/* Can't parse non-options */
if (*argv[gs->index] != '-')
return -1;
/* We have found an option */
curopt = argv[gs->index][gs->arg_index];
if (curopt)
break;
/*
* no more options in current argv[] element; try the next one
*/
gs->index++;
gs->arg_index = 1;
}
/* look up current option in optstring */
curoptp = strchr(optstring, curopt);
if (!curoptp) {
if (!silent)
printf("%s: invalid option -- %c\n", argv[0], curopt);
gs->opt = curopt;
gs->arg_index++;
return '?';
}
if (*(curoptp + 1) != ':') {
/* option with no argument. Just return it */
gs->arg = NULL;
gs->arg_index++;
return curopt;
}
if (*(curoptp + 1) && *(curoptp + 2) == ':') {
/* optional argument */
if (argv[gs->index][gs->arg_index + 1]) {
/* optional argument with directly following arg */
gs->arg = argv[gs->index++] + gs->arg_index + 1;
gs->arg_index = 1;
return curopt;
}
if (gs->index + 1 == argc) {
/* We are at the last argv[] element */
gs->arg = NULL;
gs->index++;
return curopt;
}
if (*argv[gs->index + 1] != '-') {
/*
* optional argument with arg in next argv[] element
*/
gs->index++;
gs->arg = argv[gs->index++];
gs->arg_index = 1;
return curopt;
}
/* no optional argument found */
gs->arg = NULL;
gs->arg_index = 1;
gs->index++;
return curopt;
}
if (argv[gs->index][gs->arg_index + 1]) {
/* required argument with directly following arg */
gs->arg = argv[gs->index++] + gs->arg_index + 1;
gs->arg_index = 1;
return curopt;
}
gs->index++;
gs->arg_index = 1;
if (gs->index >= argc || argv[gs->index][0] == '-') {
if (!silent)
printf("option requires an argument -- %c\n", curopt);
gs->opt = curopt;
return ':';
}
gs->arg = argv[gs->index++];
return curopt;
}