2007-07-05 16:01:39 +00:00
|
|
|
/*
|
|
|
|
* getopt.c - a simple getopt(3) implementation. See getopt.h for explanation.
|
|
|
|
*
|
|
|
|
* Copyright (c) 2007 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License version 2
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <common.h>
|
2007-10-01 08:26:45 +00:00
|
|
|
#include <module.h>
|
2007-07-05 16:01:39 +00:00
|
|
|
#include <getopt.h>
|
|
|
|
|
|
|
|
int opterr = 1;
|
|
|
|
int optind = 1;
|
|
|
|
int optopt;
|
|
|
|
char *optarg;
|
|
|
|
|
2007-10-01 08:26:45 +00:00
|
|
|
EXPORT_SYMBOL(optind);
|
|
|
|
EXPORT_SYMBOL(opterr);
|
|
|
|
EXPORT_SYMBOL(optopt);
|
|
|
|
EXPORT_SYMBOL(optarg);
|
|
|
|
|
2007-07-05 16:01:39 +00:00
|
|
|
static int optindex = 1; /* option index in the current argv[] element */
|
|
|
|
static int nonopts = 0; /* number of nonopts found */
|
|
|
|
|
2012-02-25 16:10:57 +00:00
|
|
|
void getopt_context_store(struct getopt_context *gc)
|
2007-07-05 16:01:39 +00:00
|
|
|
{
|
2012-02-25 16:10:57 +00:00
|
|
|
gc->optind = optind;
|
|
|
|
gc->opterr = opterr;
|
|
|
|
gc->optopt = optopt;
|
|
|
|
gc->optarg = optarg;
|
|
|
|
gc->nonopts = nonopts;
|
|
|
|
gc->optindex = optindex;
|
|
|
|
|
2007-07-05 16:01:39 +00:00
|
|
|
optind = opterr = optindex = 1;
|
|
|
|
nonopts = 0;
|
|
|
|
}
|
2012-02-25 16:10:57 +00:00
|
|
|
EXPORT_SYMBOL(getopt_context_store);
|
|
|
|
|
|
|
|
void getopt_context_restore(struct getopt_context *gc)
|
|
|
|
{
|
|
|
|
optind = gc->optind;
|
|
|
|
opterr = gc->opterr;
|
|
|
|
optopt = gc->optopt;
|
|
|
|
optarg = gc->optarg;
|
|
|
|
nonopts = gc->nonopts;
|
|
|
|
optindex = gc->optindex;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(getopt_context_restore);
|
2007-07-05 16:01:39 +00:00
|
|
|
|
2013-02-07 21:31:39 +00:00
|
|
|
int getopt(int argc, char *argv[], const char *optstring)
|
2007-07-05 16:01:39 +00:00
|
|
|
{
|
|
|
|
char curopt; /* current option character */
|
2013-02-07 21:31:39 +00:00
|
|
|
const char *curoptp; /* pointer to the current option in optstring */
|
2007-07-05 16:01:39 +00:00
|
|
|
|
|
|
|
while(1) {
|
2012-02-25 15:56:43 +00:00
|
|
|
debug("optindex: %d nonopts: %d optind: %d\n", optindex, nonopts, optind);
|
2007-07-05 16:01:39 +00:00
|
|
|
|
|
|
|
/* first put nonopts to the end */
|
|
|
|
while (optind + nonopts < argc && *argv[optind] != '-') {
|
|
|
|
int i;
|
|
|
|
char *tmp;
|
|
|
|
|
|
|
|
nonopts++;
|
|
|
|
tmp = argv[optind];
|
|
|
|
for (i = optind; i + 1 < argc; i++)
|
|
|
|
argv[i] = argv[i + 1];
|
|
|
|
argv[argc - 1] = tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (optind + nonopts >= argc)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* We have found an option */
|
|
|
|
curopt = argv[optind][optindex];
|
|
|
|
if (curopt)
|
|
|
|
break;
|
|
|
|
/* no more options in current argv[] element. Start
|
|
|
|
* over with looking for nonopts
|
|
|
|
*/
|
|
|
|
optind++;
|
|
|
|
optindex = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* look up current option in optstring */
|
|
|
|
curoptp = strchr(optstring, curopt);
|
|
|
|
|
|
|
|
if (!curoptp) {
|
|
|
|
if (opterr)
|
|
|
|
printf("%s: invalid option -- %c\n", argv[0], curopt);
|
|
|
|
optopt = curopt;
|
|
|
|
optindex++;
|
|
|
|
return '?';
|
|
|
|
}
|
|
|
|
|
2007-07-05 16:01:40 +00:00
|
|
|
if (*(curoptp + 1) != ':') {
|
2007-07-05 16:01:39 +00:00
|
|
|
/* option with no argument. Just return it */
|
|
|
|
optarg = NULL;
|
|
|
|
optindex++;
|
|
|
|
return curopt;
|
|
|
|
}
|
|
|
|
|
2007-07-05 16:01:40 +00:00
|
|
|
if (*(curoptp + 1) && *(curoptp + 2) == ':') {
|
2007-07-05 16:01:39 +00:00
|
|
|
/* optional argument */
|
|
|
|
if (argv[optind][optindex + 1]) {
|
|
|
|
/* optional argument with directly following optarg */
|
|
|
|
optarg = argv[optind] + optindex + 1;
|
|
|
|
optindex = 1;
|
|
|
|
optind++;
|
|
|
|
return curopt;
|
|
|
|
}
|
|
|
|
if (optind + nonopts + 1 == argc) {
|
|
|
|
/* We are at the last argv[] element */
|
|
|
|
optarg = NULL;
|
|
|
|
optind++;
|
|
|
|
return curopt;
|
|
|
|
}
|
|
|
|
if (*argv[optind + 1] != '-') {
|
|
|
|
/* optional argument with optarg in next argv[] element
|
|
|
|
*/
|
|
|
|
optind++;
|
|
|
|
optarg = argv[optind];
|
|
|
|
optind++;
|
|
|
|
optindex = 1;
|
|
|
|
return curopt;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* no optional argument found */
|
|
|
|
optarg = NULL;
|
|
|
|
optindex = 1;
|
|
|
|
optind++;
|
|
|
|
return curopt;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (argv[optind][optindex + 1]) {
|
|
|
|
/* required argument with directly following optarg */
|
|
|
|
optarg = argv[optind] + optindex + 1;
|
|
|
|
optind++;
|
|
|
|
optindex = 1;
|
|
|
|
return curopt;
|
|
|
|
}
|
|
|
|
|
|
|
|
optind++;
|
|
|
|
optindex = 1;
|
|
|
|
|
|
|
|
if (optind + nonopts >= argc || argv[optind][0] == '-') {
|
|
|
|
if (opterr)
|
|
|
|
printf("option requires an argument -- %c\n",
|
|
|
|
curopt);
|
|
|
|
optopt = curopt;
|
|
|
|
return ':';
|
|
|
|
}
|
|
|
|
|
|
|
|
optarg = argv[optind];
|
|
|
|
optind++;
|
|
|
|
return curopt;
|
|
|
|
}
|
2007-10-01 08:26:45 +00:00
|
|
|
EXPORT_SYMBOL(getopt);
|