2 * Copyright (c) 1992, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 * Copyright (c) 1992, 1993, 1994, 1995, 1996
5 * Keith Bostic. All rights reserved.
7 * See the LICENSE file for redistribution information.
12 #include <sys/queue.h>
16 #include <bitstring.h>
26 #include "../common/common.h"
28 static const char *sigmsg(int);
31 * ex_shell -- :sh[ell]
32 * Invoke the program named in the SHELL environment variable
33 * with the argument -i.
35 * PUBLIC: int ex_shell(SCR *, EXCMD *);
38 ex_shell(SCR *sp, EXCMD *cmdp)
43 /* We'll need a shell. */
44 if (opts_empty(sp, O_SHELL, 0))
49 * Assumes all shells use -i.
51 if (asprintf(&buf, "%s -i", O_STR(sp, O_SHELL)) == -1) {
52 msgq(sp, M_SYSERR, NULL);
56 /* Restore the window name. */
57 (void)sp->gp->scr_rename(sp, NULL, 0);
59 /* If we're still in a vi screen, move out explicitly. */
60 rval = ex_exec_proc(sp, cmdp, buf, NULL, !F_ISSET(sp, SC_SCR_EXWROTE));
63 /* Set the window name. */
64 (void)sp->gp->scr_rename(sp, sp->frp->name, 1);
68 * Historically, vi didn't require a continue message after the
69 * return of the shell. Match it.
71 F_SET(sp, SC_EX_WAIT_NO);
78 * Run a separate process.
80 * PUBLIC: int ex_exec_proc(SCR *, EXCMD *, char *, const char *, int);
83 ex_exec_proc(SCR *sp, EXCMD *cmdp, char *cmd, const char *msg, int need_newline)
91 /* We'll need a shell. */
92 if (opts_empty(sp, O_SHELL, 0))
96 if (F_ISSET(sp, SC_VI)) {
97 if (gp->scr_screen(sp, SC_EX)) {
98 ex_wemsg(sp, cmdp->cmd->name, EXM_NOCANON);
101 (void)gp->scr_attr(sp, SA_ALTERNATE, 0);
102 F_SET(sp, SC_SCR_EX | SC_SCR_EXWROTE);
105 /* Put out additional newline, message. */
107 (void)ex_puts(sp, "\n");
109 (void)ex_puts(sp, msg);
110 (void)ex_puts(sp, "\n");
114 switch (pid = vfork()) {
115 case -1: /* Error. */
116 msgq(sp, M_SYSERR, "vfork");
118 case 0: /* Utility. */
121 if ((name = strrchr(O_STR(sp, O_SHELL), '/')) == NULL)
122 name = O_STR(sp, O_SHELL);
125 execl(O_STR(sp, O_SHELL), name, "-c", cmd, (char *)NULL);
126 msgq_str(sp, M_SYSERR, O_STR(sp, O_SHELL), "execl: %s");
129 default: /* Parent. */
130 return (proc_wait(sp, (long)pid, cmd, 0, 0));
137 * Wait for one of the processes.
140 * The pid_t type varies in size from a short to a long depending on the
141 * system. It has to be cast into something or the standard promotion
142 * rules get you. I'm using a long based on the belief that nobody is
143 * going to make it unsigned and it's unlikely to be a quad.
145 * PUBLIC: int proc_wait(SCR *, long, const char *, int, int);
148 proc_wait(SCR *sp, long int pid, const char *cmd, int silent, int okpipe)
154 /* Wait for the utility, ignoring interruptions. */
157 if (waitpid((pid_t)pid, &pstat, 0) != -1)
159 if (errno != EINTR) {
160 msgq(sp, M_SYSERR, "waitpid");
166 * Display the utility's exit status. Ignore SIGPIPE from the
167 * parent-writer, as that only means that the utility chose to
168 * exit before reading all of its input.
170 if (WIFSIGNALED(pstat) && (!okpipe || WTERMSIG(pstat) != SIGPIPE)) {
171 for (; cmdskip(*cmd); ++cmd);
172 p = msg_print(sp, cmd, &nf);
174 msgq(sp, M_ERR, "%.*s%s: received signal: %s%s",
175 (int)MIN(len, 20), p, len > 20 ? " ..." : "",
176 sigmsg(WTERMSIG(pstat)),
177 WCOREDUMP(pstat) ? "; core dumped" : "");
179 FREE_SPACE(sp, p, 0);
183 if (WIFEXITED(pstat) && WEXITSTATUS(pstat)) {
185 * Remain silent for "normal" errors when doing shell file
186 * name expansions, they almost certainly indicate nothing
187 * more than a failure to match.
189 * Remain silent for vi read filter errors. It's historic
193 for (; cmdskip(*cmd); ++cmd);
194 p = msg_print(sp, cmd, &nf);
196 msgq(sp, M_ERR, "%.*s%s: exited with status %d",
197 (int)MIN(len, 20), p, len > 20 ? " ..." : "",
200 FREE_SPACE(sp, p, 0);
209 * Return a pointer to a message describing a signal.
217 /* POSIX.1-2008 leaves strsignal(3)'s return value unspecified. */
218 if ((message = strsignal(signo)) != NULL)
220 (void)snprintf(buf, sizeof(buf), "Unknown signal: %d", signo);