Skip to content

Commit

Permalink
nshlib/[cd|ls|pwd]: add support for local CWD(Current working directory)
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
anchao committed Dec 11, 2024
1 parent 1f8b9aa commit a67f544
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 46 deletions.
24 changes: 6 additions & 18 deletions nshlib/nsh.h
Original file line number Diff line number Diff line change
Expand Up @@ -406,13 +406,6 @@
# define NSH_HAVE_VARS
#endif

/* Stubs used when working directory is not supported */

#ifdef CONFIG_DISABLE_ENVIRON
# define nsh_getfullpath(v,p) ((FAR char*)(p))
# define nsh_freefullpath(p)
#endif

/* The size of the I/O buffer may be specified in the
* boards/<arch>/<chip>/<board>/configs/<config>defconfig file -- provided
* that it is at least as large as PATH_MAX.
Expand All @@ -425,8 +418,7 @@
*/

#if defined(CONFIG_NSH_DISABLE_LS) && defined(CONFIG_NSH_DISABLE_CP) && \
defined(CONFIG_NSH_DISABLE_PS) && !defined(CONFIG_NSH_PLATFORM_MOTD) && \
defined(CONFIG_DISABLE_ENVIRON)
defined(CONFIG_NSH_DISABLE_PS) && !defined(CONFIG_NSH_PLATFORM_MOTD)
# undef NSH_HAVE_IOBUFFER
#endif

Expand Down Expand Up @@ -867,14 +859,12 @@ 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);
FAR char *nsh_getfullpath(FAR struct nsh_vtbl_s *vtbl,
FAR const char *relpath);
void nsh_freefullpath(FAR char *fullpath);
#endif

/* Debug */

Expand Down Expand Up @@ -1071,14 +1061,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);
Expand Down
8 changes: 2 additions & 6 deletions nshlib/nsh_command.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions nshlib/nsh_console.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 6 additions & 0 deletions nshlib/nsh_console.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
35 changes: 19 additions & 16 deletions nshlib/nsh_envcmds.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +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

static const char g_home[] = CONFIG_LIBC_HOMEDIR;
#endif

/****************************************************************************
* Private Functions
Expand Down Expand Up @@ -169,18 +170,19 @@ 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
****************************************************************************/

#ifndef CONFIG_DISABLE_ENVIRON
FAR char *nsh_getfullpath(FAR struct nsh_vtbl_s *vtbl,
FAR const char *relpath)
{
Expand All @@ -201,7 +203,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 */

Expand All @@ -214,27 +216,23 @@ 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)
{
free(fullpath);
}
}
#endif

/****************************************************************************
* 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)
{
Expand All @@ -249,14 +247,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
Expand All @@ -273,6 +273,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 */

Expand All @@ -289,7 +295,6 @@ int cmd_cd(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
return ret;
}
#endif
#endif

/****************************************************************************
* Name: cmd_echo
Expand Down Expand Up @@ -394,18 +399,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
Expand Down
7 changes: 1 addition & 6 deletions nshlib/nsh_fscmds.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down

0 comments on commit a67f544

Please sign in to comment.