Skip to content

Commit 5dcfe93

Browse files
anjiahao1xiaoxiang781216
authored andcommitted
nsh:support $! get last pid use nsh run
test: nsh> hello & hello [4:100] nsh> Hello, World!! nsh> echo $! 4 nsh> echo $! 3 nsh> ps PID GROUP PRI POLICY TYPE NPX STATE EVENT SIGMASK STACK COMMAND 0 0 0 FIFO Kthread - Ready 0000000000000000 0069616 Idle_Task 1 1 224 FIFO Kthread - Waiting Signal 0000000000000000 0067536 loop_task 2 2 224 FIFO Kthread - Waiting Semaphore 0000000000000000 0067504 hpwork 0x45ae80 0x45aea8 3 3 100 FIFO Task - Running 0000000000000000 0067536 nsh_main nsh> Signed-off-by: anjiahao <[email protected]>
1 parent 55df301 commit 5dcfe93

File tree

5 files changed

+14
-0
lines changed

5 files changed

+14
-0
lines changed

nshlib/nsh.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -659,6 +659,11 @@ struct nsh_parser_s
659659
bool np_redir_out; /* true: Output from the last command was re-directed */
660660
bool np_redir_in; /* true: Input from the last command was re-directed */
661661
bool np_fail; /* true: The last command failed */
662+
pid_t np_lastpid; /* Pid of the last command executed */
663+
#ifdef NSH_HAVE_VARS
664+
char np_pids[32]; /* String representation of the last pid */
665+
#endif
666+
662667
#ifndef CONFIG_NSH_DISABLESCRIPT
663668
uint8_t np_flags; /* See nsh_npflags_e above */
664669
#endif

nshlib/nsh_builtin.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ int nsh_builtin(FAR struct nsh_vtbl_s *vtbl, FAR const char *cmd,
121121
* foreground
122122
*/
123123

124+
vtbl->np.np_lastpid = ret;
124125
#ifdef CONFIG_SCHED_WAITPID
125126

126127
/* CONFIG_SCHED_WAITPID is selected, so we may run the command in

nshlib/nsh_command.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1256,6 +1256,7 @@ int nsh_command(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char *argv[])
12561256
}
12571257

12581258
ret = handler(vtbl, argc, argv);
1259+
vtbl->np.np_lastpid = getpid();
12591260
return ret;
12601261
}
12611262

nshlib/nsh_fileapps.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ int nsh_fileapp(FAR struct nsh_vtbl_s *vtbl, FAR const char *cmd,
202202
* foreground
203203
*/
204204

205+
vtbl->np.np_lastpid = pid;
205206
#ifdef CONFIG_SCHED_WAITPID
206207
/* CONFIG_SCHED_WAITPID is selected, so we may run the command in
207208
* foreground unless we were specifically requested to run the command

nshlib/nsh_parse.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,7 @@ static const char g_redirect_out2[] = ">>";
281281
static const char g_redirect_in1[] = "<";
282282
#ifdef NSH_HAVE_VARS
283283
static const char g_exitstatus[] = "?";
284+
static const char g_lastpid[] = "!";
284285
static const char g_success[] = "0";
285286
static const char g_failure[] = "1";
286287
#endif
@@ -1262,6 +1263,11 @@ static FAR char *nsh_envexpand(FAR struct nsh_vtbl_s *vtbl,
12621263
return (FAR char *)g_success;
12631264
}
12641265
}
1266+
else if (strcmp(varname, g_lastpid) == 0)
1267+
{
1268+
itoa(vtbl->np.np_lastpid, vtbl->np.np_pids, 10);
1269+
return vtbl->np.np_pids;
1270+
}
12651271
else
12661272
{
12671273
FAR char *value;

0 commit comments

Comments
 (0)