mirror of
https://github.com/apache/nuttx-apps.git
synced 2025-07-05 03:13:47 +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);
|
FAR char **argv, FAR const struct nsh_param_s *param);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifndef CONFIG_DISABLE_ENVIRON
|
|
||||||
/* Working directory support */
|
/* 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 char *nsh_getfullpath(FAR struct nsh_vtbl_s *vtbl,
|
||||||
FAR const char *relpath);
|
FAR const char *relpath);
|
||||||
void nsh_freefullpath(FAR char *fullpath);
|
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
|
||||||
#endif /* !CONFIG_DISABLE_MOUNTPOINT */
|
#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);
|
int cmd_cd(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv);
|
||||||
# endif
|
#endif
|
||||||
# ifndef CONFIG_NSH_DISABLE_PWD
|
#ifndef CONFIG_NSH_DISABLE_PWD
|
||||||
int cmd_pwd(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv);
|
int cmd_pwd(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv);
|
||||||
# endif
|
#endif
|
||||||
#endif /* !CONFIG_DISABLE_MOUNTPOINT */
|
|
||||||
|
|
||||||
#ifndef CONFIG_NSH_DISABLE_ENV
|
#ifndef CONFIG_NSH_DISABLE_ENV
|
||||||
int cmd_env(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv);
|
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> ...]]]"),
|
"[<path> [<path> [<path> ...]]]"),
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifndef CONFIG_DISABLE_ENVIRON
|
#ifndef CONFIG_NSH_DISABLE_CD
|
||||||
# ifndef CONFIG_NSH_DISABLE_CD
|
|
||||||
CMD_MAP("cd", cmd_cd, 1, 2, "[<dir-path>|-|~|..]"),
|
CMD_MAP("cd", cmd_cd, 1, 2, "[<dir-path>|-|~|..]"),
|
||||||
# endif
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifndef CONFIG_NSH_DISABLE_CP
|
#ifndef CONFIG_NSH_DISABLE_CP
|
||||||
@ -485,10 +483,8 @@ static const struct cmdmap_s g_cmdmap[] =
|
|||||||
# endif
|
# endif
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifndef CONFIG_DISABLE_ENVIRON
|
#ifndef CONFIG_NSH_DISABLE_PWD
|
||||||
# ifndef CONFIG_NSH_DISABLE_PWD
|
|
||||||
CMD_MAP("pwd", cmd_pwd, 1, 1, NULL),
|
CMD_MAP("pwd", cmd_pwd, 1, 1, NULL),
|
||||||
# endif
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if !defined(CONFIG_NSH_DISABLE_READLINK) && defined(CONFIG_PSEUDOFS_SOFTLINKS)
|
#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 */
|
/* Initialize the input stream */
|
||||||
|
|
||||||
INFD(pstate) = STDIN_FILENO;
|
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;
|
return pstate;
|
||||||
|
@ -150,6 +150,12 @@ struct nsh_vtbl_s
|
|||||||
/* Ctrl tty or not */
|
/* Ctrl tty or not */
|
||||||
|
|
||||||
bool isctty;
|
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
|
/* This structure describes a console front-end that is based on stdin and
|
||||||
|
@ -49,9 +49,12 @@
|
|||||||
|
|
||||||
#ifndef CONFIG_DISABLE_ENVIRON
|
#ifndef CONFIG_DISABLE_ENVIRON
|
||||||
static const char g_pwd[] = "PWD";
|
static const char g_pwd[] = "PWD";
|
||||||
#ifndef CONFIG_NSH_DISABLE_CD
|
# ifndef CONFIG_NSH_DISABLE_CD
|
||||||
static const char g_oldpwd[] = "OLDPWD";
|
static const char g_oldpwd[] = "OLDPWD";
|
||||||
|
# endif
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#if !defined(CONFIG_NSH_DISABLE_CD) || !defined(CONFIG_DISABLE_ENVIRON)
|
||||||
static const char g_home[] = CONFIG_LIBC_HOMEDIR;
|
static const char g_home[] = CONFIG_LIBC_HOMEDIR;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@ -169,12 +172,14 @@ static int nsh_dumpvar(FAR struct nsh_vtbl_s *vtbl, FAR void *arg,
|
|||||||
* Name: nsh_getwd
|
* Name: nsh_getwd
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
#ifndef CONFIG_DISABLE_ENVIRON
|
FAR const char *nsh_getcwd(FAR struct nsh_vtbl_s *vtbl)
|
||||||
FAR const char *nsh_getcwd(void)
|
|
||||||
{
|
{
|
||||||
|
#ifndef CONFIG_DISABLE_ENVIRON
|
||||||
return nsh_getwd(g_pwd);
|
return nsh_getwd(g_pwd);
|
||||||
}
|
#else
|
||||||
|
return vtbl->cwd;
|
||||||
#endif
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
* Name: nsh_getfullpath
|
* 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 */
|
/* Get the path to the current working directory */
|
||||||
|
|
||||||
wd = nsh_getcwd();
|
wd = nsh_getcwd(vtbl);
|
||||||
|
|
||||||
/* Fake the '.' directory */
|
/* Fake the '.' directory */
|
||||||
|
|
||||||
@ -214,13 +219,11 @@ FAR char *nsh_getfullpath(FAR struct nsh_vtbl_s *vtbl,
|
|||||||
|
|
||||||
return nsh_getdirpath(vtbl, wd, relpath);
|
return nsh_getdirpath(vtbl, wd, relpath);
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
* Name: nsh_freefullpath
|
* Name: nsh_freefullpath
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
#ifndef CONFIG_DISABLE_ENVIRON
|
|
||||||
void nsh_freefullpath(FAR char *fullpath)
|
void nsh_freefullpath(FAR char *fullpath)
|
||||||
{
|
{
|
||||||
if (fullpath)
|
if (fullpath)
|
||||||
@ -228,13 +231,12 @@ void nsh_freefullpath(FAR char *fullpath)
|
|||||||
free(fullpath);
|
free(fullpath);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
#endif /* CONFIG_DISABLE_ENVIRON */
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
* Name: cmd_cd
|
* Name: cmd_cd
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
#ifndef 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)
|
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;
|
path = g_home;
|
||||||
}
|
}
|
||||||
|
#ifndef CONFIG_DISABLE_ENVIRON
|
||||||
else if (strcmp(path, "-") == 0)
|
else if (strcmp(path, "-") == 0)
|
||||||
{
|
{
|
||||||
alloc = strdup(nsh_getwd(g_oldpwd));
|
alloc = strdup(nsh_getwd(g_oldpwd));
|
||||||
path = alloc;
|
path = alloc;
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
else if (strcmp(path, "..") == 0)
|
else if (strcmp(path, "..") == 0)
|
||||||
{
|
{
|
||||||
alloc = strdup(nsh_getcwd());
|
alloc = strdup(nsh_getcwd(vtbl));
|
||||||
path = dirname(alloc);
|
path = dirname(alloc);
|
||||||
}
|
}
|
||||||
else
|
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);
|
nsh_error(vtbl, g_fmtcmdfailed, argv[0], "chdir", NSH_ERRNO);
|
||||||
ret = ERROR;
|
ret = ERROR;
|
||||||
}
|
}
|
||||||
|
#ifdef CONFIG_DISABLE_ENVIRON
|
||||||
|
else
|
||||||
|
{
|
||||||
|
strlcpy(vtbl->cwd, path, sizeof(vtbl->cwd));
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
/* Free any memory that was allocated */
|
/* 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;
|
return ret;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
#endif
|
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
* Name: cmd_echo
|
* Name: cmd_echo
|
||||||
@ -394,18 +403,16 @@ int cmd_env(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
|
|||||||
* Name: cmd_pwd
|
* Name: cmd_pwd
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
#ifndef CONFIG_DISABLE_ENVIRON
|
|
||||||
#ifndef CONFIG_NSH_DISABLE_PWD
|
#ifndef CONFIG_NSH_DISABLE_PWD
|
||||||
int cmd_pwd(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
|
int cmd_pwd(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
|
||||||
{
|
{
|
||||||
UNUSED(argc);
|
UNUSED(argc);
|
||||||
UNUSED(argv);
|
UNUSED(argv);
|
||||||
|
|
||||||
nsh_output(vtbl, "%s\n", nsh_getcwd());
|
nsh_output(vtbl, "%s\n", nsh_getcwd(vtbl));
|
||||||
return OK;
|
return OK;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
#endif
|
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
* Name: cmd_set
|
* 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)
|
else if (optind >= argc)
|
||||||
{
|
{
|
||||||
#ifndef CONFIG_DISABLE_ENVIRON
|
relpath = nsh_getcwd(vtbl);
|
||||||
relpath = nsh_getcwd();
|
|
||||||
#else
|
|
||||||
nsh_error(vtbl, g_fmtargrequired, argv[0]);
|
|
||||||
return ERROR;
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
Loading…
x
Reference in New Issue
Block a user