mirror of
https://github.com/apache/nuttx-apps.git
synced 2025-07-04 19:07:16 +08:00
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 <anchao@lixiang.com>
This commit is contained in:
parent
d7ed69200f
commit
e861ea8b53
14
nshlib/nsh.h
14
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);
|
||||
|
@ -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)
|
||||
|
@ -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;
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
{
|
||||
|
Loading…
x
Reference in New Issue
Block a user