2021-07-01 17:00:15 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2021 Nordic Semiconductor ASA
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
includes: prefer <zephyr/kernel.h> over <zephyr/zephyr.h>
As of today <zephyr/zephyr.h> is 100% equivalent to <zephyr/kernel.h>.
This patch proposes to then include <zephyr/kernel.h> instead of
<zephyr/zephyr.h> since it is more clear that you are including the
Kernel APIs and (probably) nothing else. <zephyr/zephyr.h> sounds like a
catch-all header that may be confusing. Most applications need to
include a bunch of other things to compile, e.g. driver headers or
subsystem headers like BT, logging, etc.
The idea of a catch-all header in Zephyr is probably not feasible
anyway. Reason is that Zephyr is not a library, like it could be for
example `libpython`. Zephyr provides many utilities nowadays: a kernel,
drivers, subsystems, etc and things will likely grow. A catch-all header
would be massive, difficult to keep up-to-date. It is also likely that
an application will only build a small subset. Note that subsystem-level
headers may use a catch-all approach to make things easier, though.
NOTE: This patch is **NOT** removing the header, just removing its usage
in-tree. I'd advocate for its deprecation (add a #warning on it), but I
understand many people will have concerns.
Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2022-08-25 15:58:46 +08:00
|
|
|
#include <zephyr/kernel.h>
|
2022-05-06 17:23:05 +08:00
|
|
|
#include <zephyr/shell/shell.h>
|
2021-07-01 17:00:15 +08:00
|
|
|
#include "getopt.h"
|
|
|
|
|
|
|
|
/* Referring below variables is not thread safe. They reflects getopt state
|
|
|
|
* only when 1 thread is using getopt.
|
|
|
|
* When more threads are using getopt please call getopt_state_get to know
|
|
|
|
* getopt state for the current thread.
|
|
|
|
*/
|
|
|
|
int opterr = 1; /* if error message should be printed */
|
|
|
|
int optind = 1; /* index into parent argv vector */
|
|
|
|
int optopt; /* character checked for validity */
|
|
|
|
int optreset; /* reset getopt */
|
|
|
|
char *optarg; /* argument associated with option */
|
|
|
|
|
|
|
|
/* Common state for all threads that did not have own getopt state. */
|
|
|
|
static struct getopt_state m_getopt_common_state = {
|
|
|
|
.opterr = 1,
|
|
|
|
.optind = 1,
|
|
|
|
.optopt = 0,
|
|
|
|
.optreset = 0,
|
|
|
|
.optarg = NULL,
|
|
|
|
|
|
|
|
.place = "", /* EMSG */
|
|
|
|
|
|
|
|
#if CONFIG_GETOPT_LONG
|
|
|
|
.nonopt_start = -1, /* first non option argument (for permute) */
|
|
|
|
.nonopt_end = -1, /* first option after non options (for permute) */
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
/* This function is not thread safe. All threads using getopt are calling
|
|
|
|
* this function.
|
|
|
|
*/
|
|
|
|
void z_getopt_global_state_update(struct getopt_state *state)
|
|
|
|
{
|
|
|
|
opterr = state->opterr;
|
|
|
|
optind = state->optind;
|
|
|
|
optopt = state->optopt;
|
|
|
|
optreset = state->optreset;
|
|
|
|
optarg = state->optarg;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* It is internal getopt API function, it shall not be called by the user. */
|
|
|
|
struct getopt_state *getopt_state_get(void)
|
|
|
|
{
|
|
|
|
#if CONFIG_SHELL_GETOPT
|
|
|
|
k_tid_t tid;
|
|
|
|
|
|
|
|
tid = k_current_get();
|
|
|
|
STRUCT_SECTION_FOREACH(shell, sh) {
|
|
|
|
if (tid == sh->ctx->tid) {
|
|
|
|
return &sh->ctx->getopt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
/* If not a shell thread return a common pointer */
|
|
|
|
return &m_getopt_common_state;
|
|
|
|
}
|