lib: getopt: rework and extend getopt library
Getopt has been rework in this way that calling it does not require extra state parameter and its execution is thread safe. Global parameters describing the state of the getopt function have been made available to ensure full API compatibility in using this library. However, referencing these global variables directly is not thread safe. In order to get the state of the getopt function for the thread that is currently using it, call: getopt_state_get(); Extended the library with getopt_long and getopt_long_only functions. Moved getopt libary from utils to posix. Signed-off-by: Jakub Rzeszutko <jakub.rzeszutko@nordicsemi.no>
This commit is contained in:
parent
7f1f1229e1
commit
d4559f53fa
22 changed files with 978 additions and 179 deletions
|
@ -17,7 +17,7 @@
|
|||
#include <sys/util.h>
|
||||
|
||||
#if defined CONFIG_SHELL_GETOPT
|
||||
#include <shell/shell_getopt.h>
|
||||
#include <getopt.h>
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
@ -71,7 +71,6 @@ extern "C" {
|
|||
* @{
|
||||
*/
|
||||
|
||||
struct getopt_state;
|
||||
struct shell_static_entry;
|
||||
|
||||
/**
|
||||
|
@ -707,7 +706,7 @@ struct shell_ctx {
|
|||
|
||||
#if defined CONFIG_SHELL_GETOPT
|
||||
/*!< getopt context for a shell backend. */
|
||||
struct getopt_state getopt_state;
|
||||
struct getopt_state getopt;
|
||||
#endif
|
||||
|
||||
uint16_t cmd_buff_len; /*!< Command length.*/
|
||||
|
@ -1028,40 +1027,6 @@ void shell_help(const struct shell *shell);
|
|||
/* @brief Command's help has been printed */
|
||||
#define SHELL_CMD_HELP_PRINTED (1)
|
||||
|
||||
#if defined CONFIG_SHELL_GETOPT
|
||||
/**
|
||||
* @brief Parses the command-line arguments.
|
||||
*
|
||||
* It is based on FreeBSD implementation.
|
||||
*
|
||||
* @param[in] shell Pointer to the shell instance.
|
||||
* @param[in] argc Arguments count.
|
||||
* @param[in] argv Arguments.
|
||||
* @param[in] ostr String containing the legitimate option characters.
|
||||
*
|
||||
* @return If an option was successfully found, function returns
|
||||
* the option character.
|
||||
* @return If options have been detected that is not in @p ostr
|
||||
* function will return '?'.
|
||||
* If function 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.
|
||||
* @return -1 If all options have been parsed.
|
||||
*/
|
||||
int shell_getopt(const struct shell *shell, int argc, char *const argv[],
|
||||
const char *ostr);
|
||||
|
||||
/**
|
||||
* @brief Returns shell_getopt state.
|
||||
*
|
||||
* @param[in] shell Pointer to the shell instance.
|
||||
*
|
||||
* @return Pointer to struct getopt_state.
|
||||
*/
|
||||
struct getopt_state *shell_getopt_state_get(const struct shell *shell);
|
||||
#endif /* CONFIG_SHELL_GETOPT */
|
||||
|
||||
/** @brief Execute command.
|
||||
*
|
||||
* Pass command line to shell to execute.
|
||||
|
|
|
@ -1,27 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2021 Nordic Semiconductor ASA
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#ifndef SHELL_GETOPT_H__
|
||||
#define SHELL_GETOPT_H__
|
||||
|
||||
#include <getopt.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
|
||||
/* Initializing shell getopt module.
|
||||
*
|
||||
* @param[in] shell Pointer to the shell instance.
|
||||
*/
|
||||
void z_shell_getopt_init(struct getopt_state *state);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* SHELL_GETOPT_H__ */
|
Loading…
Add table
Add a link
Reference in a new issue