Skip to content

Commit e861ea8

Browse files
anchaoxiaoxiang781216
authored andcommitted
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>
1 parent d7ed692 commit e861ea8

File tree

6 files changed

+43
-34
lines changed

6 files changed

+43
-34
lines changed

nshlib/nsh.h

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -867,10 +867,10 @@ int nsh_fileapp(FAR struct nsh_vtbl_s *vtbl, FAR const char *cmd,
867867
FAR char **argv, FAR const struct nsh_param_s *param);
868868
#endif
869869

870-
#ifndef CONFIG_DISABLE_ENVIRON
871870
/* Working directory support */
872871

873-
FAR const char *nsh_getcwd(void);
872+
FAR const char *nsh_getcwd(FAR struct nsh_vtbl_s *vtbl);
873+
#ifndef CONFIG_DISABLE_ENVIRON
874874
FAR char *nsh_getfullpath(FAR struct nsh_vtbl_s *vtbl,
875875
FAR const char *relpath);
876876
void nsh_freefullpath(FAR char *fullpath);
@@ -1071,14 +1071,12 @@ int cmd_irqinfo(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv);
10711071
# endif
10721072
#endif /* !CONFIG_DISABLE_MOUNTPOINT */
10731073

1074-
#if !defined(CONFIG_DISABLE_ENVIRON)
1075-
# ifndef CONFIG_NSH_DISABLE_CD
1074+
#ifndef CONFIG_NSH_DISABLE_CD
10761075
int cmd_cd(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv);
1077-
# endif
1078-
# ifndef CONFIG_NSH_DISABLE_PWD
1076+
#endif
1077+
#ifndef CONFIG_NSH_DISABLE_PWD
10791078
int cmd_pwd(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv);
1080-
# endif
1081-
#endif /* !CONFIG_DISABLE_MOUNTPOINT */
1079+
#endif
10821080

10831081
#ifndef CONFIG_NSH_DISABLE_ENV
10841082
int cmd_env(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv);

nshlib/nsh_command.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -162,10 +162,8 @@ static const struct cmdmap_s g_cmdmap[] =
162162
"[<path> [<path> [<path> ...]]]"),
163163
#endif
164164

165-
#ifndef CONFIG_DISABLE_ENVIRON
166-
# ifndef CONFIG_NSH_DISABLE_CD
165+
#ifndef CONFIG_NSH_DISABLE_CD
167166
CMD_MAP("cd", cmd_cd, 1, 2, "[<dir-path>|-|~|..]"),
168-
# endif
169167
#endif
170168

171169
#ifndef CONFIG_NSH_DISABLE_CP
@@ -485,10 +483,8 @@ static const struct cmdmap_s g_cmdmap[] =
485483
# endif
486484
#endif
487485

488-
#ifndef CONFIG_DISABLE_ENVIRON
489-
# ifndef CONFIG_NSH_DISABLE_PWD
486+
#ifndef CONFIG_NSH_DISABLE_PWD
490487
CMD_MAP("pwd", cmd_pwd, 1, 1, NULL),
491-
# endif
492488
#endif
493489

494490
#if !defined(CONFIG_NSH_DISABLE_READLINK) && defined(CONFIG_PSEUDOFS_SOFTLINKS)

nshlib/nsh_console.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,13 @@ FAR struct console_stdio_s *nsh_newconsole(bool isctty)
442442
/* Initialize the input stream */
443443

444444
INFD(pstate) = STDIN_FILENO;
445+
446+
/* Initialize current working directory */
447+
448+
#ifdef CONFIG_DISABLE_ENVIRON
449+
strlcpy(pstate->cn_vtbl.cwd, CONFIG_LIBC_HOMEDIR,
450+
sizeof(pstate->cn_vtbl.cwd));
451+
#endif
445452
}
446453

447454
return pstate;

nshlib/nsh_console.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,12 @@ struct nsh_vtbl_s
150150
/* Ctrl tty or not */
151151

152152
bool isctty;
153+
154+
/* Current working directory */
155+
156+
#ifdef CONFIG_DISABLE_ENVIRON
157+
char cwd[PATH_MAX];
158+
#endif
153159
};
154160

155161
/* This structure describes a console front-end that is based on stdin and

nshlib/nsh_envcmds.c

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,12 @@
4949

5050
#ifndef CONFIG_DISABLE_ENVIRON
5151
static const char g_pwd[] = "PWD";
52-
#ifndef CONFIG_NSH_DISABLE_CD
52+
# ifndef CONFIG_NSH_DISABLE_CD
5353
static const char g_oldpwd[] = "OLDPWD";
54+
# endif
5455
#endif
56+
57+
#if !defined(CONFIG_NSH_DISABLE_CD) || !defined(CONFIG_DISABLE_ENVIRON)
5558
static const char g_home[] = CONFIG_LIBC_HOMEDIR;
5659
#endif
5760

@@ -169,12 +172,14 @@ static int nsh_dumpvar(FAR struct nsh_vtbl_s *vtbl, FAR void *arg,
169172
* Name: nsh_getwd
170173
****************************************************************************/
171174

172-
#ifndef CONFIG_DISABLE_ENVIRON
173-
FAR const char *nsh_getcwd(void)
175+
FAR const char *nsh_getcwd(FAR struct nsh_vtbl_s *vtbl)
174176
{
177+
#ifndef CONFIG_DISABLE_ENVIRON
175178
return nsh_getwd(g_pwd);
176-
}
179+
#else
180+
return vtbl->cwd;
177181
#endif
182+
}
178183

179184
/****************************************************************************
180185
* Name: nsh_getfullpath
@@ -201,7 +206,7 @@ FAR char *nsh_getfullpath(FAR struct nsh_vtbl_s *vtbl,
201206

202207
/* Get the path to the current working directory */
203208

204-
wd = nsh_getcwd();
209+
wd = nsh_getcwd(vtbl);
205210

206211
/* Fake the '.' directory */
207212

@@ -214,27 +219,24 @@ FAR char *nsh_getfullpath(FAR struct nsh_vtbl_s *vtbl,
214219

215220
return nsh_getdirpath(vtbl, wd, relpath);
216221
}
217-
#endif
218222

219223
/****************************************************************************
220224
* Name: nsh_freefullpath
221225
****************************************************************************/
222226

223-
#ifndef CONFIG_DISABLE_ENVIRON
224227
void nsh_freefullpath(FAR char *fullpath)
225228
{
226229
if (fullpath)
227230
{
228231
free(fullpath);
229232
}
230233
}
231-
#endif
234+
#endif /* CONFIG_DISABLE_ENVIRON */
232235

233236
/****************************************************************************
234237
* Name: cmd_cd
235238
****************************************************************************/
236239

237-
#ifndef CONFIG_DISABLE_ENVIRON
238240
#ifndef CONFIG_NSH_DISABLE_CD
239241
int cmd_cd(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
240242
{
@@ -249,14 +251,16 @@ int cmd_cd(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
249251
{
250252
path = g_home;
251253
}
254+
#ifndef CONFIG_DISABLE_ENVIRON
252255
else if (strcmp(path, "-") == 0)
253256
{
254257
alloc = strdup(nsh_getwd(g_oldpwd));
255258
path = alloc;
256259
}
260+
#endif
257261
else if (strcmp(path, "..") == 0)
258262
{
259-
alloc = strdup(nsh_getcwd());
263+
alloc = strdup(nsh_getcwd(vtbl));
260264
path = dirname(alloc);
261265
}
262266
else
@@ -273,6 +277,12 @@ int cmd_cd(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
273277
nsh_error(vtbl, g_fmtcmdfailed, argv[0], "chdir", NSH_ERRNO);
274278
ret = ERROR;
275279
}
280+
#ifdef CONFIG_DISABLE_ENVIRON
281+
else
282+
{
283+
strlcpy(vtbl->cwd, path, sizeof(vtbl->cwd));
284+
}
285+
#endif
276286

277287
/* Free any memory that was allocated */
278288

@@ -289,7 +299,6 @@ int cmd_cd(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
289299
return ret;
290300
}
291301
#endif
292-
#endif
293302

294303
/****************************************************************************
295304
* Name: cmd_echo
@@ -394,18 +403,16 @@ int cmd_env(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
394403
* Name: cmd_pwd
395404
****************************************************************************/
396405

397-
#ifndef CONFIG_DISABLE_ENVIRON
398406
#ifndef CONFIG_NSH_DISABLE_PWD
399407
int cmd_pwd(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
400408
{
401409
UNUSED(argc);
402410
UNUSED(argv);
403411

404-
nsh_output(vtbl, "%s\n", nsh_getcwd());
412+
nsh_output(vtbl, "%s\n", nsh_getcwd(vtbl));
405413
return OK;
406414
}
407415
#endif
408-
#endif
409416

410417
/****************************************************************************
411418
* Name: cmd_set

nshlib/nsh_fscmds.c

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1578,12 +1578,7 @@ int cmd_ls(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv)
15781578
}
15791579
else if (optind >= argc)
15801580
{
1581-
#ifndef CONFIG_DISABLE_ENVIRON
1582-
relpath = nsh_getcwd();
1583-
#else
1584-
nsh_error(vtbl, g_fmtargrequired, argv[0]);
1585-
return ERROR;
1586-
#endif
1581+
relpath = nsh_getcwd(vtbl);
15871582
}
15881583
else
15891584
{

0 commit comments

Comments
 (0)