2012-11-10 17:06:01 +01:00
|
|
|
/****************************************************************************
|
2018-05-29 21:21:26 +02:00
|
|
|
* libs/libc/unistd/lib_getopt.c
|
2012-11-10 17:06:01 +01:00
|
|
|
*
|
2021-03-02 15:54:21 +01:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2012-11-10 17:06:01 +01:00
|
|
|
*
|
2021-03-02 15:54:21 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2012-11-10 17:06:01 +01:00
|
|
|
*
|
2021-03-02 15:54:21 +01:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2012-11-10 17:06:01 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <stdbool.h>
|
2020-11-17 18:27:01 +01:00
|
|
|
#include <getopt.h>
|
2012-11-10 17:06:01 +01:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2015-10-03 00:30:35 +02:00
|
|
|
* Public Data
|
2012-11-10 17:06:01 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
FAR char *optarg; /* Optional argument following option */
|
2020-11-17 18:27:01 +01:00
|
|
|
int opterr = 0; /* Print error message */
|
2012-11-10 17:06:01 +01:00
|
|
|
int optind = 1; /* Index into argv */
|
|
|
|
int optopt = '?'; /* unrecognized option character */
|
|
|
|
|
|
|
|
/****************************************************************************
|
2016-02-22 01:08:58 +01:00
|
|
|
* Private Data
|
2012-11-10 17:06:01 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
2020-05-06 11:24:42 +02:00
|
|
|
static FAR char *g_optptr = NULL;
|
|
|
|
static FAR char * const *g_argv = NULL;
|
|
|
|
static int g_argc = 0;
|
|
|
|
static bool g_binitialized = false;
|
2012-11-10 17:06:01 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
2015-10-03 00:30:35 +02:00
|
|
|
* Public Functions
|
2012-11-10 17:06:01 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: getopt
|
|
|
|
*
|
2019-02-05 17:30:59 +01:00
|
|
|
* Description:
|
|
|
|
* getopt() parses command-line arguments. Its arguments argc and argv
|
|
|
|
* are the argument count and array as passed to the main() function on
|
|
|
|
* program invocation. An element of argv that starts with '-' is an
|
|
|
|
* option element. The characters of this element (aside from the initial
|
|
|
|
* '-') are option characters. If getopt() is called repeatedly, it
|
|
|
|
* returns successively each of the option characters from each of the
|
2012-11-10 17:06:01 +01:00
|
|
|
* option elements.
|
|
|
|
*
|
|
|
|
* If getopt() finds another option character, it returns that character,
|
|
|
|
* updating the external variable optind and a static variable nextchar so
|
|
|
|
* that the next call to getopt() can resume the scan with the following
|
|
|
|
* option character or argv-element.
|
|
|
|
*
|
|
|
|
* If there are no more option characters, getopt() returns -1. Then optind
|
|
|
|
* is the index in argv of the first argv-element that is not an option.
|
|
|
|
*
|
|
|
|
* The 'optstring' argument is a string containing the legitimate option
|
|
|
|
* characters. If such a character is followed by a colon, this indicates
|
|
|
|
* that the option requires an argument. If an argument is required for an
|
|
|
|
* option so getopt() places a pointer to the following text in the same
|
|
|
|
* argv-element, or the text of the following argv-element, in optarg.
|
|
|
|
*
|
|
|
|
* NOTES:
|
|
|
|
* 1. opterr is not supported and this implementation of getopt() never
|
|
|
|
* printfs error messages.
|
|
|
|
* 2. getopt is NOT threadsafe!
|
|
|
|
* 3. This version of getopt() does not reset global variables until
|
|
|
|
* -1 is returned. As a result, your command line parsing loops
|
|
|
|
* must call getopt() repeatedly and continue to parse if other
|
|
|
|
* errors are returned ('?' or ':') until getopt() finally returns -1.
|
|
|
|
* (You can also set optind to -1 to force a reset).
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Returned Value:
|
2018-02-01 17:00:02 +01:00
|
|
|
* If an option was successfully found, then getopt() returns the option
|
2020-02-07 03:06:03 +01:00
|
|
|
* character. If all command-line options have been parsed, then getopt()
|
2018-02-01 17:00:02 +01:00
|
|
|
* returns -1. If getopt() encounters an option character that was not
|
|
|
|
* in optstring, then '?' is returned. If getopt() encounters an option
|
|
|
|
* with a missing argument, then the return value depends on the first
|
|
|
|
* character in optstring: if it is ':', then ':' is returned; otherwise
|
|
|
|
* '?' is returned.
|
2012-11-10 17:06:01 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-05-06 11:24:42 +02:00
|
|
|
int getopt(int argc, FAR char * const argv[], FAR const char *optstring)
|
2012-11-10 17:06:01 +01:00
|
|
|
{
|
2019-02-05 17:30:59 +01:00
|
|
|
/* Were new argc or argv passed in? This detects misuse of getopt() by
|
2020-02-14 15:50:45 +01:00
|
|
|
* applications that break out of the getopt() loop before getop() returns
|
2019-02-05 17:30:59 +01:00
|
|
|
* -1.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (argc != g_argc || argv != g_argv)
|
|
|
|
{
|
|
|
|
/* Yes, clear the internal state */
|
|
|
|
|
|
|
|
g_binitialized = false;
|
|
|
|
g_argc = argc;
|
|
|
|
g_argv = argv;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Verify input parameters. */
|
|
|
|
|
2020-12-01 04:20:54 +01:00
|
|
|
if (argv != NULL && optstring != NULL)
|
2012-11-10 17:06:01 +01:00
|
|
|
{
|
2019-02-05 17:30:59 +01:00
|
|
|
FAR char *optchar;
|
2012-11-10 17:06:01 +01:00
|
|
|
int noarg_ret = '?';
|
|
|
|
|
2020-05-06 11:24:42 +02:00
|
|
|
/* The initial value of optind is 1. If getopt() is called again in
|
|
|
|
* the program, optind must be reset to some value <= 1.
|
2012-11-10 17:06:01 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
if (optind < 1 || !g_binitialized)
|
|
|
|
{
|
2019-02-05 17:30:59 +01:00
|
|
|
optarg = NULL;
|
2012-11-10 17:06:01 +01:00
|
|
|
optind = 1; /* Skip over the program name */
|
2019-02-05 17:30:59 +01:00
|
|
|
optopt = '?';
|
2012-11-10 17:06:01 +01:00
|
|
|
g_optptr = NULL; /* Start at the beginning of the first argument */
|
|
|
|
g_binitialized = true; /* Now we are initialized */
|
|
|
|
}
|
|
|
|
|
2020-05-06 11:24:42 +02:00
|
|
|
/* If the first character of opstring s ':', then ':' is in the event
|
|
|
|
* of a missing argument. Otherwise '?' is returned.
|
2012-11-10 17:06:01 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
if (*optstring == ':')
|
|
|
|
{
|
|
|
|
noarg_ret = ':';
|
|
|
|
optstring++;
|
|
|
|
}
|
|
|
|
|
2020-05-06 11:24:42 +02:00
|
|
|
/* Are we resuming in the middle, or at the end of a string of
|
|
|
|
* arguments? g_optptr == NULL means that we are started at the
|
|
|
|
* beginning of argv[optind]; *g_optptr == \0 means that we are
|
|
|
|
* starting at the beginning of optind+1
|
2012-11-10 17:06:01 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
while (!g_optptr || !*g_optptr)
|
|
|
|
{
|
2020-05-06 11:24:42 +02:00
|
|
|
/* We need to start at the beginning of the next argv. Check if we
|
|
|
|
* need to increment optind
|
2012-11-10 17:06:01 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
if (g_optptr)
|
|
|
|
{
|
2020-05-06 11:24:42 +02:00
|
|
|
/* Yes.. Increment it and check for the case where where we
|
|
|
|
* have processed everything in the argv[] array.
|
2012-11-10 17:06:01 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
optind++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check for the end of the argument list */
|
|
|
|
|
|
|
|
g_optptr = argv[optind];
|
|
|
|
if (!g_optptr)
|
|
|
|
{
|
|
|
|
/* There are no more arguments, we are finished */
|
|
|
|
|
|
|
|
g_binitialized = false;
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
2020-05-06 11:24:42 +02:00
|
|
|
/* We are starting at the beginning of argv[optind]. In this case,
|
|
|
|
* the first character must be '-'
|
2012-11-10 17:06:01 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
if (*g_optptr != '-')
|
|
|
|
{
|
|
|
|
/* The argument does not start with '-', we are finished */
|
|
|
|
|
|
|
|
g_binitialized = false;
|
|
|
|
return ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Skip over the '-' */
|
|
|
|
|
|
|
|
g_optptr++;
|
|
|
|
}
|
|
|
|
|
2015-10-12 15:45:02 +02:00
|
|
|
/* Special case handling of "-" and "-:" */
|
|
|
|
|
|
|
|
if (!*g_optptr)
|
|
|
|
{
|
2020-05-06 11:24:42 +02:00
|
|
|
optopt = '\0'; /* We'll fix up g_optptr the next time we are called */
|
|
|
|
return '?';
|
2015-10-12 15:45:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Handle the case of "-:" */
|
|
|
|
|
|
|
|
if (*g_optptr == ':')
|
|
|
|
{
|
|
|
|
optopt = ':';
|
|
|
|
g_optptr++;
|
|
|
|
return '?';
|
|
|
|
}
|
|
|
|
|
2020-05-06 11:24:42 +02:00
|
|
|
/* g_optptr now points at the next option and it is not something
|
|
|
|
* crazy. check if the option is in the list of valid options.
|
2015-10-12 15:45:02 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
optchar = strchr(optstring, *g_optptr);
|
|
|
|
if (!optchar)
|
|
|
|
{
|
|
|
|
/* No this character is not in the list of valid options */
|
2012-11-10 17:06:01 +01:00
|
|
|
|
2015-10-12 15:45:02 +02:00
|
|
|
optopt = *g_optptr;
|
|
|
|
g_optptr++;
|
|
|
|
return '?';
|
|
|
|
}
|
2012-11-10 17:06:01 +01:00
|
|
|
|
2015-10-12 15:45:02 +02:00
|
|
|
/* Yes, the character is in the list of valid options. Does it have an
|
|
|
|
* required argument?
|
|
|
|
*/
|
2012-11-10 17:06:01 +01:00
|
|
|
|
2015-10-12 15:45:02 +02:00
|
|
|
if (optchar[1] != ':')
|
|
|
|
{
|
|
|
|
/* No, no arguments. Just return the character that we found */
|
|
|
|
|
|
|
|
g_optptr++;
|
|
|
|
return *optchar;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Yes, it has a required argument. Is the required argument
|
|
|
|
* immediately after the command in this same argument?
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (g_optptr[1] != '\0')
|
|
|
|
{
|
|
|
|
/* Yes, return a pointer into the current argument */
|
|
|
|
|
|
|
|
optarg = &g_optptr[1];
|
|
|
|
optind++;
|
|
|
|
g_optptr = NULL;
|
|
|
|
return *optchar;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* No.. is the optional argument the next argument in argv[] ? */
|
|
|
|
|
2020-05-06 11:24:42 +02:00
|
|
|
if (argv[optind + 1] && *argv[optind + 1] != '-')
|
2015-10-12 15:45:02 +02:00
|
|
|
{
|
|
|
|
/* Yes.. return that */
|
|
|
|
|
2020-05-06 11:24:42 +02:00
|
|
|
optarg = argv[optind + 1];
|
2015-10-12 15:45:02 +02:00
|
|
|
optind += 2;
|
|
|
|
g_optptr = NULL;
|
|
|
|
return *optchar;
|
|
|
|
}
|
2012-11-10 17:06:01 +01:00
|
|
|
|
2015-10-12 15:45:02 +02:00
|
|
|
/* No argument was supplied */
|
2012-11-10 17:06:01 +01:00
|
|
|
|
2015-10-12 15:45:02 +02:00
|
|
|
g_optptr = NULL;
|
2019-02-05 17:30:59 +01:00
|
|
|
optarg = NULL;
|
|
|
|
optopt = *optchar;
|
2015-10-12 15:45:02 +02:00
|
|
|
optind++;
|
|
|
|
return noarg_ret;
|
2012-11-10 17:06:01 +01:00
|
|
|
}
|
|
|
|
|
2014-01-20 20:13:36 +01:00
|
|
|
/* Restore the initial, uninitialized state */
|
|
|
|
|
2012-11-10 17:06:01 +01:00
|
|
|
g_binitialized = false;
|
|
|
|
return ERROR;
|
|
|
|
}
|
2020-11-17 18:27:01 +01:00
|
|
|
|
|
|
|
int getopt_long(int argc, FAR char *const argv[],
|
|
|
|
FAR const char *shortopts,
|
|
|
|
FAR const struct option *longopts,
|
|
|
|
FAR int *longind)
|
|
|
|
{
|
|
|
|
return getopt(argc, argv, shortopts);
|
|
|
|
}
|