Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shell add getopt long nuttx #2

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions doc/reference/shell/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ interaction is required. This module is a Unix-like shell with these features:
* Built-in handler to display help for the commands.
* Support for wildcards: ``*`` and ``?``.
* Support for meta keys.
* Support for getopt.
* Support for getopt and getopt_long.
* Kconfig configuration to optimize memory usage.

.. note::
Expand Down Expand Up @@ -486,9 +486,9 @@ Getopt Feature

Some shell users apart from subcommands might need to use options as well.
the arguments string, looking for supported options. Typically, this task
is accomplished by the ``getopt`` function.
is accomplished by the ``getopt`` familly functions.

For this purpose shell supports the getopt library available
For this purpose shell supports the getopt and getopt_long libraries available
in the FreeBSD project. I was modified so that it can be used
by all instances of the shell at the same time, hence its call requires
one more parameter.
Expand Down
69 changes: 60 additions & 9 deletions include/shell/shell.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ extern "C" {
* @{
*/

struct getopt_state;
struct z_option;
struct shell_static_entry;

/**
Expand Down Expand Up @@ -665,7 +665,7 @@ struct shell_ctx {

#if defined CONFIG_SHELL_GETOPT
/*!< getopt context for a shell backend. */
struct getopt_state getopt_state;
struct getopt_s getopt_vars;
#endif

uint16_t cmd_buff_len; /*!< Command length.*/
Expand Down Expand Up @@ -978,7 +978,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.
*
Expand All @@ -987,11 +986,11 @@ void shell_help(const struct shell *shell);
* @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.
* @param[in] options 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
* @return If options have been detected that is not in @p options
* function will return '?'.
* If function encounters an option with a missing
* argument, then the return value depends on the first
Expand All @@ -1000,17 +999,69 @@ void shell_help(const struct shell *shell);
* @return -1 If all options have been parsed.
*/
int shell_getopt(const struct shell *shell, int argc, char *const argv[],
const char *ostr);
const char *options);
/**
* @brief Parses the command-line arguments.
*
* The shell_getopt_long() function works like @ref shell_getopt() except
* it also accepts long options, started with two dashes.
*
* @note This function is based on FreeBSD implementation but it does not
* support environment variable: POSIXLY_CORRECT.
*
* @param[in] shell Pointer to the shell instance.
* @param[in] argc Arguments count.
* @param[in] argv Arguments.
* @param[in] options String containing the legitimate option characters.
* @param[in] long_options Pointer to the first element of an array of
* @a struct z_option.
* @param[in] long_idx If long_idx is not NULL, it points to a variable
* which is set to the index of the long option relative
* to @p long_options.
*
* @return If an option was successfully found, function returns
* the option character.
*/
int shell_getopt_long(const struct shell *shell, int argc, char *const argv[],
const char *options, const struct z_option *long_options,
int *long_idx);

/**
* @brief Parses the command-line arguments.
*
* The shell_getopt_long_only() function works like @ref shell_getopt_long(),
* but '-' as well as "--" can indicate a long option. If an option that starts
* with '-' (not "--") doesn't match a long option, but does match a short
* option, it is parsed as a short option instead.
*
* @note This function is based on FreeBSD implementation but it does not
* support environment variable: POSIXLY_CORRECT.
*
* @param[in] shell Pointer to the shell instance.
* @param[in] argc Arguments count.
* @param[in] argv Arguments.
* @param[in] options String containing the legitimate option characters.
* @param[in] long_options Pointer to the first element of an array of
* @a struct z_option.
* @param[in] long_idx If long_idx is not NULL, it points to a variable
* which is set to the index of the long option relative
* to @p long_options.
*
* @return If an option was successfully found, function returns
* the option character.
*/
int shell_getopt_long_only(const struct shell *shell, int argc,
char *const argv[], const char *options,
const struct z_option *long_options, int *long_idx);

/**
* @brief Returns shell_getopt state.
*
* @param[in] shell Pointer to the shell instance.
*
* @return Pointer to struct getopt_state.
* @return Pointer to @p struct getopt_s.
*/
struct getopt_state *shell_getopt_state_get(const struct shell *shell);
#endif /* CONFIG_SHELL_GETOPT */
struct getopt_s *shell_getopt_state_get(const struct shell *shell);

/** @brief Execute command.
*
Expand Down
2 changes: 1 addition & 1 deletion include/shell/shell_getopt.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ extern "C" {
*
* @param[in] shell Pointer to the shell instance.
*/
void z_shell_getopt_init(struct getopt_state *state);
void z_shell_getopt_init(struct getopt_s *state);

#ifdef __cplusplus
}
Expand Down
3 changes: 3 additions & 0 deletions lib/util/getopt/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,7 @@ zephyr_include_directories_ifdef(
zephyr_sources_ifdef(
CONFIG_GETOPT
getopt.c
getopt_long.c
lib_getopt_common.c
lib_getoptvars.c
)
17 changes: 15 additions & 2 deletions lib/util/getopt/Kconfig
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
# Copyright (c) 2021 Nordic Semiconductor
# SPDX-License-Identifier: Apache-2.0


config GETOPT
bool "GetOpt Support"
bool "Geopt library support"
help
This option enables the getopt library
This option adds support of the multi-instance getopt.
Different shell backends use their own instance of getopt state
structure.
There are two options if one wants to use getopt functions from
outside the shell thread. If getopt is called from only one thread
it can be done straight forward. The getopt functions will use
a single getopt state structure created in the lib_getoptvars.c
file.
However, if getopt is expected to be called from multiple threads,
you must modify the struct getopt_s *getoptvars(void) function
in the lib_getoptvars.c file so that it returns a separate getopt
state structure for each thread.

58 changes: 0 additions & 58 deletions lib/util/getopt/README

This file was deleted.

Loading