This is an automated email from the ASF dual-hosted git repository.
xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx-apps.git
The following commit(s) were added to refs/heads/master by this push:
new e861ea8b5 nshlib/[cd|ls|pwd]: add support for local CWD(Current
working directory)
e861ea8b5 is described below
commit e861ea8b53e6c86b28274e3651036761d17d88ea
Author: chao an <[email protected]>
AuthorDate: Tue Dec 10 21:06:08 2024 +0800
nshlib/[cd|ls|pwd]: add support for local CWD(Current working directory)
This PR will still allow basic shell operations such as cd/ls/pwd to be
used even when the environment is disabled.
Signed-off-by: chao an <[email protected]>
---
nshlib/nsh.h | 14 ++++++--------
nshlib/nsh_command.c | 8 ++------
nshlib/nsh_console.c | 7 +++++++
nshlib/nsh_console.h | 6 ++++++
nshlib/nsh_envcmds.c | 35 +++++++++++++++++++++--------------
nshlib/nsh_fscmds.c | 7 +------
6 files changed, 43 insertions(+), 34 deletions(-)
diff --git a/nshlib/nsh.h b/nshlib/nsh.h
index 75c84f856..42b7167c2 100644
--- a/nshlib/nsh.h
+++ b/nshlib/nsh.h
@@ -867,10 +867,10 @@ int nsh_fileapp(FAR struct nsh_vtbl_s *vtbl, FAR const
char *cmd,
FAR char **argv, FAR const struct nsh_param_s *param);
#endif
-#ifndef CONFIG_DISABLE_ENVIRON
/* Working directory support */
-FAR const char *nsh_getcwd(void);
+FAR const char *nsh_getcwd(FAR struct nsh_vtbl_s *vtbl);
+#ifndef CONFIG_DISABLE_ENVIRON
FAR char *nsh_getfullpath(FAR struct nsh_vtbl_s *vtbl,
FAR const char *relpath);
void nsh_freefullpath(FAR char *fullpath);
@@ -1071,14 +1071,12 @@ int cmd_irqinfo(FAR struct nsh_vtbl_s *vtbl, int argc,
FAR char **argv);
# endif
#endif /* !CONFIG_DISABLE_MOUNTPOINT */
-#if !defined(CONFIG_DISABLE_ENVIRON)
-# ifndef CONFIG_NSH_DISABLE_CD
+#ifndef CONFIG_NSH_DISABLE_CD
int cmd_cd(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv);
-# endif
-# ifndef CONFIG_NSH_DISABLE_PWD
+#endif
+#ifndef CONFIG_NSH_DISABLE_PWD
int cmd_pwd(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv);
-# endif
-#endif /* !CONFIG_DISABLE_MOUNTPOINT */
+#endif
#ifndef CONFIG_NSH_DISABLE_ENV
int cmd_env(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv);
diff --git a/nshlib/nsh_command.c b/nshlib/nsh_command.c
index c31a89b86..69fd54a92 100644
--- a/nshlib/nsh_command.c
+++ b/nshlib/nsh_command.c
@@ -162,10 +162,8 @@ static const struct cmdmap_s g_cmdmap[] =
"[<path> [<path> [<path> ...]]]"),
#endif
-#ifndef CONFIG_DISABLE_ENVIRON
-# ifndef CONFIG_NSH_DISABLE_CD
+#ifndef CONFIG_NSH_DISABLE_CD
CMD_MAP("cd", cmd_cd, 1, 2, "[<dir-path>|-|~|..]"),
-# endif
#endif
#ifndef CONFIG_NSH_DISABLE_CP
@@ -485,10 +483,8 @@ static const struct cmdmap_s g_cmdmap[] =
# endif
#endif
-#ifndef CONFIG_DISABLE_ENVIRON
-# ifndef CONFIG_NSH_DISABLE_PWD
+#ifndef CONFIG_NSH_DISABLE_PWD
CMD_MAP("pwd", cmd_pwd, 1, 1, NULL),
-# endif
#endif
#if !defined(CONFIG_NSH_DISABLE_READLINK) && defined(CONFIG_PSEUDOFS_SOFTLINKS)
diff --git a/nshlib/nsh_console.c b/nshlib/nsh_console.c
index ad1191df7..ffa058640 100644
--- a/nshlib/nsh_console.c
+++ b/nshlib/nsh_console.c
@@ -442,6 +442,13 @@ FAR struct console_stdio_s *nsh_newconsole(bool isctty)
/* Initialize the input stream */
INFD(pstate) = STDIN_FILENO;
+
+ /* Initialize current working directory */
+
+#ifdef CONFIG_DISABLE_ENVIRON
+ strlcpy(pstate->cn_vtbl.cwd, CONFIG_LIBC_HOMEDIR,
+ sizeof(pstate->cn_vtbl.cwd));
+#endif
}
return pstate;
diff --git a/nshlib/nsh_console.h b/nshlib/nsh_console.h
index 31edf27e6..9d305be01 100644
--- a/nshlib/nsh_console.h
+++ b/nshlib/nsh_console.h
@@ -150,6 +150,12 @@ struct nsh_vtbl_s
/* Ctrl tty or not */
bool isctty;
+
+ /* Current working directory */
+
+#ifdef CONFIG_DISABLE_ENVIRON
+ char cwd[PATH_MAX];
+#endif
};
/* This structure describes a console front-end that is based on stdin and
diff --git a/nshlib/nsh_envcmds.c b/nshlib/nsh_envcmds.c
index 7f06e48b3..331af3e9f 100644
--- a/nshlib/nsh_envcmds.c
+++ b/nshlib/nsh_envcmds.c
@@ -49,9 +49,12 @@
#ifndef CONFIG_DISABLE_ENVIRON
static const char g_pwd[] = "PWD";
-#ifndef CONFIG_NSH_DISABLE_CD
+# ifndef CONFIG_NSH_DISABLE_CD
static const char g_oldpwd[] = "OLDPWD";
+# endif
#endif
+
+#if !defined(CONFIG_NSH_DISABLE_CD) || !defined(CONFIG_DISABLE_ENVIRON)
static const char g_home[] = CONFIG_LIBC_HOMEDIR;
#endif
@@ -169,12 +172,14 @@ static int nsh_dumpvar(FAR struct nsh_vtbl_s *vtbl, FAR
void *arg,
* Name: nsh_getwd
****************************************************************************/
-#ifndef CONFIG_DISABLE_ENVIRON
-FAR const char *nsh_getcwd(void)
+FAR const char *nsh_getcwd(FAR struct nsh_vtbl_s *vtbl)
{
+#ifndef CONFIG_DISABLE_ENVIRON
return nsh_getwd(g_pwd);
-}
+#else
+ return vtbl->cwd;
#endif
+}
/****************************************************************************
* Name: nsh_getfullpath
@@ -201,7 +206,7 @@ FAR char *nsh_getfullpath(FAR struct nsh_vtbl_s *vtbl,
/* Get the path to the current working directory */
- wd = nsh_getcwd();
+ wd = nsh_getcwd(vtbl);
/* Fake the '.' directory */
@@ -214,13 +219,11 @@ FAR char *nsh_getfullpath(FAR struct nsh_vtbl_s *vtbl,
return nsh_getdirpath(vtbl, wd, relpath);
}
-#endif
/****************************************************************************
* Name: nsh_freefullpath
****************************************************************************/
-#ifndef CONFIG_DISABLE_ENVIRON
void nsh_freefullpath(FAR char *fullpath)
{
if (fullpath)
@@ -228,13 +231,12 @@ void nsh_freefullpath(FAR char *fullpath)
free(fullpath);
}
}
-#endif
+#endif /* CONFIG_DISABLE_ENVIRON */
/****************************************************************************
* Name: cmd_cd
****************************************************************************/
-#ifndef CONFIG_DISABLE_ENVIRON
#ifndef CONFIG_NSH_DISABLE_CD
int cmd_cd(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
{
@@ -249,14 +251,16 @@ int cmd_cd(FAR struct nsh_vtbl_s *vtbl, int argc, FAR
char **argv)
{
path = g_home;
}
+#ifndef CONFIG_DISABLE_ENVIRON
else if (strcmp(path, "-") == 0)
{
alloc = strdup(nsh_getwd(g_oldpwd));
path = alloc;
}
+#endif
else if (strcmp(path, "..") == 0)
{
- alloc = strdup(nsh_getcwd());
+ alloc = strdup(nsh_getcwd(vtbl));
path = dirname(alloc);
}
else
@@ -273,6 +277,12 @@ int cmd_cd(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char
**argv)
nsh_error(vtbl, g_fmtcmdfailed, argv[0], "chdir", NSH_ERRNO);
ret = ERROR;
}
+#ifdef CONFIG_DISABLE_ENVIRON
+ else
+ {
+ strlcpy(vtbl->cwd, path, sizeof(vtbl->cwd));
+ }
+#endif
/* Free any memory that was allocated */
@@ -289,7 +299,6 @@ int cmd_cd(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char
**argv)
return ret;
}
#endif
-#endif
/****************************************************************************
* Name: cmd_echo
@@ -394,18 +403,16 @@ int cmd_env(FAR struct nsh_vtbl_s *vtbl, int argc, FAR
char **argv)
* Name: cmd_pwd
****************************************************************************/
-#ifndef CONFIG_DISABLE_ENVIRON
#ifndef CONFIG_NSH_DISABLE_PWD
int cmd_pwd(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
{
UNUSED(argc);
UNUSED(argv);
- nsh_output(vtbl, "%s\n", nsh_getcwd());
+ nsh_output(vtbl, "%s\n", nsh_getcwd(vtbl));
return OK;
}
#endif
-#endif
/****************************************************************************
* Name: cmd_set
diff --git a/nshlib/nsh_fscmds.c b/nshlib/nsh_fscmds.c
index 88f76d1ee..aef6fd72f 100644
--- a/nshlib/nsh_fscmds.c
+++ b/nshlib/nsh_fscmds.c
@@ -1578,12 +1578,7 @@ int cmd_ls(FAR struct nsh_vtbl_s *vtbl, int argc, FAR
char **argv)
}
else if (optind >= argc)
{
-#ifndef CONFIG_DISABLE_ENVIRON
- relpath = nsh_getcwd();
-#else
- nsh_error(vtbl, g_fmtargrequired, argv[0]);
- return ERROR;
-#endif
+ relpath = nsh_getcwd(vtbl);
}
else
{