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 <anjiahao@xiaomi.com>
This commit is contained in:
anjiahao
2023-12-25 20:34:34 +08:00
committed by Xiang Xiao
parent 55df3019f9
commit 5dcfe93a95
5 changed files with 14 additions and 0 deletions

View File

@@ -659,6 +659,11 @@ struct nsh_parser_s
bool np_redir_out; /* true: Output from the last command was re-directed */ 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_redir_in; /* true: Input from the last command was re-directed */
bool np_fail; /* true: The last command failed */ 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 #ifndef CONFIG_NSH_DISABLESCRIPT
uint8_t np_flags; /* See nsh_npflags_e above */ uint8_t np_flags; /* See nsh_npflags_e above */
#endif #endif

View File

@@ -121,6 +121,7 @@ int nsh_builtin(FAR struct nsh_vtbl_s *vtbl, FAR const char *cmd,
* foreground * foreground
*/ */
vtbl->np.np_lastpid = ret;
#ifdef CONFIG_SCHED_WAITPID #ifdef CONFIG_SCHED_WAITPID
/* CONFIG_SCHED_WAITPID is selected, so we may run the command in /* CONFIG_SCHED_WAITPID is selected, so we may run the command in

View File

@@ -1256,6 +1256,7 @@ int nsh_command(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char *argv[])
} }
ret = handler(vtbl, argc, argv); ret = handler(vtbl, argc, argv);
vtbl->np.np_lastpid = getpid();
return ret; return ret;
} }

View File

@@ -202,6 +202,7 @@ int nsh_fileapp(FAR struct nsh_vtbl_s *vtbl, FAR const char *cmd,
* foreground * foreground
*/ */
vtbl->np.np_lastpid = pid;
#ifdef CONFIG_SCHED_WAITPID #ifdef CONFIG_SCHED_WAITPID
/* CONFIG_SCHED_WAITPID is selected, so we may run the command in /* CONFIG_SCHED_WAITPID is selected, so we may run the command in
* foreground unless we were specifically requested to run the command * foreground unless we were specifically requested to run the command

View File

@@ -281,6 +281,7 @@ static const char g_redirect_out2[] = ">>";
static const char g_redirect_in1[] = "<"; static const char g_redirect_in1[] = "<";
#ifdef NSH_HAVE_VARS #ifdef NSH_HAVE_VARS
static const char g_exitstatus[] = "?"; static const char g_exitstatus[] = "?";
static const char g_lastpid[] = "!";
static const char g_success[] = "0"; static const char g_success[] = "0";
static const char g_failure[] = "1"; static const char g_failure[] = "1";
#endif #endif
@@ -1262,6 +1263,11 @@ static FAR char *nsh_envexpand(FAR struct nsh_vtbl_s *vtbl,
return (FAR char *)g_success; 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 else
{ {
FAR char *value; FAR char *value;