diff --git a/nshlib/nsh.h b/nshlib/nsh.h index 9220502f3..af6cdb058 100644 --- a/nshlib/nsh.h +++ b/nshlib/nsh.h @@ -659,6 +659,11 @@ struct nsh_parser_s bool np_redir_out; /* true: Output from the last command was re-directed */ bool np_redir_in; /* true: Input from the last command was re-directed */ bool np_fail; /* true: The last command failed */ + pid_t np_lastpid; /* Pid of the last command executed */ +#ifdef NSH_HAVE_VARS + char np_pids[32]; /* String representation of the last pid */ +#endif + #ifndef CONFIG_NSH_DISABLESCRIPT uint8_t np_flags; /* See nsh_npflags_e above */ #endif diff --git a/nshlib/nsh_builtin.c b/nshlib/nsh_builtin.c index ea80c0cc7..7cdfd9b61 100644 --- a/nshlib/nsh_builtin.c +++ b/nshlib/nsh_builtin.c @@ -121,6 +121,7 @@ int nsh_builtin(FAR struct nsh_vtbl_s *vtbl, FAR const char *cmd, * foreground */ + vtbl->np.np_lastpid = ret; #ifdef CONFIG_SCHED_WAITPID /* CONFIG_SCHED_WAITPID is selected, so we may run the command in diff --git a/nshlib/nsh_command.c b/nshlib/nsh_command.c index 64932e419..af5a09933 100644 --- a/nshlib/nsh_command.c +++ b/nshlib/nsh_command.c @@ -1256,6 +1256,7 @@ int nsh_command(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char *argv[]) } ret = handler(vtbl, argc, argv); + vtbl->np.np_lastpid = getpid(); return ret; } diff --git a/nshlib/nsh_fileapps.c b/nshlib/nsh_fileapps.c index 89094b284..a5ae19dd5 100644 --- a/nshlib/nsh_fileapps.c +++ b/nshlib/nsh_fileapps.c @@ -202,6 +202,7 @@ int nsh_fileapp(FAR struct nsh_vtbl_s *vtbl, FAR const char *cmd, * foreground */ + vtbl->np.np_lastpid = pid; #ifdef CONFIG_SCHED_WAITPID /* CONFIG_SCHED_WAITPID is selected, so we may run the command in * foreground unless we were specifically requested to run the command diff --git a/nshlib/nsh_parse.c b/nshlib/nsh_parse.c index 14cf1e879..2119d4b47 100644 --- a/nshlib/nsh_parse.c +++ b/nshlib/nsh_parse.c @@ -281,6 +281,7 @@ static const char g_redirect_out2[] = ">>"; static const char g_redirect_in1[] = "<"; #ifdef NSH_HAVE_VARS static const char g_exitstatus[] = "?"; +static const char g_lastpid[] = "!"; static const char g_success[] = "0"; static const char g_failure[] = "1"; #endif @@ -1262,6 +1263,11 @@ static FAR char *nsh_envexpand(FAR struct nsh_vtbl_s *vtbl, return (FAR char *)g_success; } } + else if (strcmp(varname, g_lastpid) == 0) + { + itoa(vtbl->np.np_lastpid, vtbl->np.np_pids, 10); + return vtbl->np.np_pids; + } else { FAR char *value;