1 /* $NetBSD: ip_run.c,v 1.6 2013/11/27 18:06:28 christos Exp $ */
4 * Rob Zimmermann. All rights reserved.
6 * Keith Bostic. All rights reserved.
8 * See the LICENSE file for redistribution information.
14 static const char sccsid
[] = "Id: ip_run.c,v 8.17 2000/07/04 21:48:54 skimo Exp (Berkeley) Date: 2000/07/04 21:48:54 ";
17 #include <sys/types.h>
18 #include <sys/queue.h>
21 #include <bitstring.h>
29 #include <sys/socket.h>
31 #include "../common/common.h"
33 #include "pathnames.h"
35 static void arg_format
__P((char *, int *, char **[], int, int));
36 static void fatal
__P((void)) __dead
;
38 static void attach
__P((void));
40 static int channel(int rpipe
[2], int wpipe
[2]);
42 const char *vi_progname
= "vi"; /* Global: program name. */
48 * PUBLIC: int vi_run __P((IPVI *, int, char *[]));
51 vi_run(ipvi
, argc
, argv
)
57 int pflag
, rpipe
[2], wpipe
[2];
58 char *execp
, **p_av
, **t_av
;
61 execp
= __UNCONST(vi_progname
);
63 /* Strip out any arguments that vi isn't going to understand. */
64 for (p_av
= t_av
= argv
;;) {
69 if (!strcmp(*t_av
, "--")) {
70 while ((*p_av
++ = *++t_av
) != NULL
);
74 if (!memcmp(*t_av
, "-D", sizeof("-D") - 1)) {
83 if (!memcmp(*t_av
, "-T", sizeof("-T") - 1)) {
84 char *p
= &t_av
[0][sizeof("-T") - 1];
95 if (!memcmp(*t_av
, "-P", sizeof("-P") - 1)) {
96 if (t_av
[0][2] != '\0') {
103 if (t_av
[1] != NULL
) {
115 * Open the communications channels. The pipes are named from the
116 * parent's viewpoint, meaning the screen reads from rpipe[0] and
117 * writes to wpipe[1]. The vi process reads from wpipe[0], and it
118 * writes to rpipe[1].
120 if (channel(rpipe
, wpipe
) == -1)
122 ipvi
->ifd
= rpipe
[0];
123 ipvi
->ofd
= wpipe
[1];
126 * Reformat our arguments, adding a -I to the list. The first file
127 * descriptor for the -I argument is vi's input, and the second is
130 arg_format(execp
, &argc
, &argv
, wpipe
[0], rpipe
[1]);
133 switch (ipvi
->pid
= fork()) {
134 case -1: /* Error. */
137 case 0: /* Child: Vi. */
138 (void)close(rpipe
[0]);
139 (void)close(wpipe
[1]);
142 * If the user didn't override the path and there's a local
143 * (debugging) nvi, run it, otherwise run the user's path,
144 * if specified, else run the compiled in path.
146 /* coverity[+toctou] */
147 if (!pflag
&& stat("vi-ipc", &sb
) == 0)
148 execvp("vi-ipc", argv
);
150 (void)fprintf(stderr
,
151 "%s: %s %s\n", vi_progname
, execp
, strerror(errno
));
152 (void)fprintf(stderr
,
154 "usage: %s [-D] [-P vi_program] [-T trace] [vi arguments]\n",
156 "usage: %s [-P vi_program] [vi arguments]\n",
160 default: /* Parent: Screen. */
161 (void)close(rpipe
[1]);
162 (void)close(wpipe
[0]);
177 (void)fprintf(stderr
, "%s: %s\n", vi_progname
, strerror(errno
));
182 channel(int rpipe
[2], int wpipe
[2])
185 if ((x
= pipe(rpipe
) == -1) || pipe(wpipe
) == -1) {
193 if (socketpair(AF_LOCAL
, SOCK_STREAM
, 0, sockets
) == -1)
196 rpipe
[0] = sockets
[0];
197 wpipe
[0] = sockets
[1];
198 if (((rpipe
[1] = dup(sockets
[1])) == -1) ||
199 ((wpipe
[1] = dup(sockets
[0])) == -1)) {
213 * Reformat our arguments to add the -I argument for vi.
216 arg_format(char *execp
, int *argcp
, char **argvp
[], int i_fd
, int o_fd
)
218 char *iarg
, **largv
= NULL
/* XXX gcc */, *p
, **p_av
, **t_av
;
220 /* Get space for the argument array and the -I argument. */
221 if ((iarg
= malloc(64)) == NULL
||
222 (largv
= malloc((*argcp
+ 3) * sizeof(char *))) == NULL
)
224 memcpy(largv
+ 2, *argvp
, *argcp
* sizeof(char *) + 1);
226 /* Reset argv[0] to be the exec'd program. */
227 if ((p
= strrchr(execp
, '/')) == NULL
)
232 /* Create the -I argument. */
233 (void)sprintf(iarg
, "-I%d%s%d", i_fd
, ".", o_fd
);
236 /* Copy any remaining arguments into the array. */
237 for (p_av
= (*argvp
) + 1, t_av
= largv
+ 2;;)
238 if ((*t_av
++ = *p_av
++) == NULL
)
241 /* Reset the argument array. */
248 * Pause and let the user attach a debugger.
256 (void)printf("process %lu waiting, enter <CR> to continue: ",
258 (void)fflush(stdout
);
260 if ((fd
= open(_PATH_TTY
, O_RDONLY
, 0)) < 0) {
261 (void)fprintf(stderr
,
262 "%s: %s, %s\n", vi_progname
, _PATH_TTY
, strerror(errno
));
266 if (read(fd
, &ch
, 1) != 1) {
270 } while (ch
!= '\n' && ch
!= '\r');