xref: /freebsd/contrib/nvi/ex/ex_shell.c (revision fe815331bb40604ba31312acf7e4619674631777)
1 /*-
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.
6  *
7  * See the LICENSE file for redistribution information.
8  */
9 
10 #include "config.h"
11 
12 #include <sys/queue.h>
13 #include <sys/time.h>
14 #include <sys/wait.h>
15 
16 #include <bitstring.h>
17 #include <ctype.h>
18 #include <errno.h>
19 #include <limits.h>
20 #include <signal.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
25 
26 #include "../common/common.h"
27 
28 static const char *sigmsg(int);
29 
30 /*
31  * ex_shell -- :sh[ell]
32  *	Invoke the program named in the SHELL environment variable
33  *	with the argument -i.
34  *
35  * PUBLIC: int ex_shell(SCR *, EXCMD *);
36  */
37 int
38 ex_shell(SCR *sp, EXCMD *cmdp)
39 {
40 	int rval;
41 	char *buf;
42 
43 	/* We'll need a shell. */
44 	if (opts_empty(sp, O_SHELL, 0))
45 		return (1);
46 
47 	/*
48 	 * XXX
49 	 * Assumes all shells use -i.
50 	 */
51 	(void)asprintf(&buf, "%s -i", O_STR(sp, O_SHELL));
52 	if (buf == NULL) {
53 		msgq(sp, M_SYSERR, NULL);
54 		return (1);
55 	}
56 
57 	/* Restore the window name. */
58 	(void)sp->gp->scr_rename(sp, NULL, 0);
59 
60 	/* If we're still in a vi screen, move out explicitly. */
61 	rval = ex_exec_proc(sp, cmdp, buf, NULL, !F_ISSET(sp, SC_SCR_EXWROTE));
62 	free(buf);
63 
64 	/* Set the window name. */
65 	(void)sp->gp->scr_rename(sp, sp->frp->name, 1);
66 
67 	/*
68 	 * !!!
69 	 * Historically, vi didn't require a continue message after the
70 	 * return of the shell.  Match it.
71 	 */
72 	F_SET(sp, SC_EX_WAIT_NO);
73 
74 	return (rval);
75 }
76 
77 /*
78  * ex_exec_proc --
79  *	Run a separate process.
80  *
81  * PUBLIC: int ex_exec_proc(SCR *, EXCMD *, char *, const char *, int);
82  */
83 int
84 ex_exec_proc(SCR *sp, EXCMD *cmdp, char *cmd, const char *msg, int need_newline)
85 {
86 	GS *gp;
87 	const char *name;
88 	pid_t pid;
89 
90 	gp = sp->gp;
91 
92 	/* We'll need a shell. */
93 	if (opts_empty(sp, O_SHELL, 0))
94 		return (1);
95 
96 	/* Enter ex mode. */
97 	if (F_ISSET(sp, SC_VI)) {
98 		if (gp->scr_screen(sp, SC_EX)) {
99 			ex_wemsg(sp, cmdp->cmd->name, EXM_NOCANON);
100 			return (1);
101 		}
102 		(void)gp->scr_attr(sp, SA_ALTERNATE, 0);
103 		F_SET(sp, SC_SCR_EX | SC_SCR_EXWROTE);
104 	}
105 
106 	/* Put out additional newline, message. */
107 	if (need_newline)
108 		(void)ex_puts(sp, "\n");
109 	if (msg != NULL) {
110 		(void)ex_puts(sp, msg);
111 		(void)ex_puts(sp, "\n");
112 	}
113 	(void)ex_fflush(sp);
114 
115 	switch (pid = vfork()) {
116 	case -1:			/* Error. */
117 		msgq(sp, M_SYSERR, "vfork");
118 		return (1);
119 	case 0:				/* Utility. */
120 		if (gp->scr_child)
121 			gp->scr_child(sp);
122 		if ((name = strrchr(O_STR(sp, O_SHELL), '/')) == NULL)
123 			name = O_STR(sp, O_SHELL);
124 		else
125 			++name;
126 		execl(O_STR(sp, O_SHELL), name, "-c", cmd, (char *)NULL);
127 		msgq_str(sp, M_SYSERR, O_STR(sp, O_SHELL), "execl: %s");
128 		_exit(127);
129 		/* NOTREACHED */
130 	default:			/* Parent. */
131 		return (proc_wait(sp, (long)pid, cmd, 0, 0));
132 	}
133 	/* NOTREACHED */
134 }
135 
136 /*
137  * proc_wait --
138  *	Wait for one of the processes.
139  *
140  * !!!
141  * The pid_t type varies in size from a short to a long depending on the
142  * system.  It has to be cast into something or the standard promotion
143  * rules get you.  I'm using a long based on the belief that nobody is
144  * going to make it unsigned and it's unlikely to be a quad.
145  *
146  * PUBLIC: int proc_wait(SCR *, long, const char *, int, int);
147  */
148 int
149 proc_wait(SCR *sp, long int pid, const char *cmd, int silent, int okpipe)
150 {
151 	size_t len;
152 	int nf, pstat;
153 	char *p;
154 
155 	/* Wait for the utility, ignoring interruptions. */
156 	for (;;) {
157 		errno = 0;
158 		if (waitpid((pid_t)pid, &pstat, 0) != -1)
159 			break;
160 		if (errno != EINTR) {
161 			msgq(sp, M_SYSERR, "waitpid");
162 			return (1);
163 		}
164 	}
165 
166 	/*
167 	 * Display the utility's exit status.  Ignore SIGPIPE from the
168 	 * parent-writer, as that only means that the utility chose to
169 	 * exit before reading all of its input.
170 	 */
171 	if (WIFSIGNALED(pstat) && (!okpipe || WTERMSIG(pstat) != SIGPIPE)) {
172 		for (; cmdskip(*cmd); ++cmd);
173 		p = msg_print(sp, cmd, &nf);
174 		len = strlen(p);
175 		msgq(sp, M_ERR, "%.*s%s: received signal: %s%s",
176 		    (int)MIN(len, 20), p, len > 20 ? " ..." : "",
177 		    sigmsg(WTERMSIG(pstat)),
178 		    WCOREDUMP(pstat) ? "; core dumped" : "");
179 		if (nf)
180 			FREE_SPACE(sp, p, 0);
181 		return (1);
182 	}
183 
184 	if (WIFEXITED(pstat) && WEXITSTATUS(pstat)) {
185 		/*
186 		 * Remain silent for "normal" errors when doing shell file
187 		 * name expansions, they almost certainly indicate nothing
188 		 * more than a failure to match.
189 		 *
190 		 * Remain silent for vi read filter errors.  It's historic
191 		 * practice.
192 		 */
193 		if (!silent) {
194 			for (; cmdskip(*cmd); ++cmd);
195 			p = msg_print(sp, cmd, &nf);
196 			len = strlen(p);
197 			msgq(sp, M_ERR, "%.*s%s: exited with status %d",
198 			    (int)MIN(len, 20), p, len > 20 ? " ..." : "",
199 			    WEXITSTATUS(pstat));
200 			if (nf)
201 				FREE_SPACE(sp, p, 0);
202 		}
203 		return (1);
204 	}
205 	return (0);
206 }
207 
208 /*
209  * sigmsg --
210  * 	Return a pointer to a message describing a signal.
211  */
212 static const char *
213 sigmsg(int signo)
214 {
215 	static char buf[40];
216 	char *message;
217 
218 	/* POSIX.1-2008 leaves strsignal(3)'s return value unspecified. */
219 	if ((message = strsignal(signo)) != NULL)
220 		return message;
221 	(void)snprintf(buf, sizeof(buf), "Unknown signal: %d", signo);
222 	return (buf);
223 }
224