1*0a6a1f1dSLionel Sambuc /* $NetBSD: ip_run.c,v 1.7 2014/01/26 21:43:45 christos Exp $ */
284d9c625SLionel Sambuc /*-
384d9c625SLionel Sambuc * Copyright (c) 1996
484d9c625SLionel Sambuc * Rob Zimmermann. All rights reserved.
584d9c625SLionel Sambuc * Copyright (c) 1996
684d9c625SLionel Sambuc * Keith Bostic. All rights reserved.
784d9c625SLionel Sambuc *
884d9c625SLionel Sambuc * See the LICENSE file for redistribution information.
984d9c625SLionel Sambuc */
1084d9c625SLionel Sambuc
1184d9c625SLionel Sambuc #include "config.h"
1284d9c625SLionel Sambuc
13*0a6a1f1dSLionel Sambuc #include <sys/cdefs.h>
14*0a6a1f1dSLionel Sambuc #if 0
1584d9c625SLionel Sambuc #ifndef lint
1684d9c625SLionel Sambuc 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 ";
1784d9c625SLionel Sambuc #endif /* not lint */
18*0a6a1f1dSLionel Sambuc #else
19*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: ip_run.c,v 1.7 2014/01/26 21:43:45 christos Exp $");
20*0a6a1f1dSLionel Sambuc #endif
2184d9c625SLionel Sambuc
2284d9c625SLionel Sambuc #include <sys/types.h>
2384d9c625SLionel Sambuc #include <sys/queue.h>
2484d9c625SLionel Sambuc #include <sys/stat.h>
2584d9c625SLionel Sambuc
2684d9c625SLionel Sambuc #include <bitstring.h>
2784d9c625SLionel Sambuc #include <errno.h>
2884d9c625SLionel Sambuc #include <fcntl.h>
2984d9c625SLionel Sambuc #include <stdio.h>
3084d9c625SLionel Sambuc #include <stdlib.h>
3184d9c625SLionel Sambuc #include <string.h>
3284d9c625SLionel Sambuc #include <unistd.h>
3384d9c625SLionel Sambuc
3484d9c625SLionel Sambuc #include <sys/socket.h>
3584d9c625SLionel Sambuc
3684d9c625SLionel Sambuc #include "../common/common.h"
3784d9c625SLionel Sambuc #include "ip.h"
3884d9c625SLionel Sambuc #include "pathnames.h"
3984d9c625SLionel Sambuc
4084d9c625SLionel Sambuc static void arg_format __P((char *, int *, char **[], int, int));
4184d9c625SLionel Sambuc static void fatal __P((void)) __dead;
4284d9c625SLionel Sambuc #ifdef DEBUG
4384d9c625SLionel Sambuc static void attach __P((void));
4484d9c625SLionel Sambuc #endif
4584d9c625SLionel Sambuc static int channel(int rpipe[2], int wpipe[2]);
4684d9c625SLionel Sambuc
4784d9c625SLionel Sambuc const char *vi_progname = "vi"; /* Global: program name. */
4884d9c625SLionel Sambuc
4984d9c625SLionel Sambuc /*
5084d9c625SLionel Sambuc * vi_run --
5184d9c625SLionel Sambuc * Run the vi program.
5284d9c625SLionel Sambuc *
5384d9c625SLionel Sambuc * PUBLIC: int vi_run __P((IPVI *, int, char *[]));
5484d9c625SLionel Sambuc */
5584d9c625SLionel Sambuc int
vi_run(ipvi,argc,argv)5684d9c625SLionel Sambuc vi_run(ipvi, argc, argv)
5784d9c625SLionel Sambuc IPVI *ipvi;
5884d9c625SLionel Sambuc int argc;
5984d9c625SLionel Sambuc char *argv[];
6084d9c625SLionel Sambuc {
6184d9c625SLionel Sambuc struct stat sb;
6284d9c625SLionel Sambuc int pflag, rpipe[2], wpipe[2];
6384d9c625SLionel Sambuc char *execp, **p_av, **t_av;
6484d9c625SLionel Sambuc
6584d9c625SLionel Sambuc pflag = 0;
6684d9c625SLionel Sambuc execp = __UNCONST(vi_progname);
6784d9c625SLionel Sambuc
6884d9c625SLionel Sambuc /* Strip out any arguments that vi isn't going to understand. */
6984d9c625SLionel Sambuc for (p_av = t_av = argv;;) {
7084d9c625SLionel Sambuc if (*t_av == NULL) {
7184d9c625SLionel Sambuc *p_av = NULL;
7284d9c625SLionel Sambuc break;
7384d9c625SLionel Sambuc }
7484d9c625SLionel Sambuc if (!strcmp(*t_av, "--")) {
7584d9c625SLionel Sambuc while ((*p_av++ = *++t_av) != NULL);
7684d9c625SLionel Sambuc break;
7784d9c625SLionel Sambuc }
7884d9c625SLionel Sambuc #ifdef DEBUG
7984d9c625SLionel Sambuc if (!memcmp(*t_av, "-D", sizeof("-D") - 1)) {
8084d9c625SLionel Sambuc attach();
8184d9c625SLionel Sambuc
8284d9c625SLionel Sambuc ++t_av;
8384d9c625SLionel Sambuc --argc;
8484d9c625SLionel Sambuc continue;
8584d9c625SLionel Sambuc }
8684d9c625SLionel Sambuc #endif
8784d9c625SLionel Sambuc #ifdef TRACE
8884d9c625SLionel Sambuc if (!memcmp(*t_av, "-T", sizeof("-T") - 1)) {
8984d9c625SLionel Sambuc char *p = &t_av[0][sizeof("-T") - 1];
9084d9c625SLionel Sambuc if (*p == '\0') {
9184d9c625SLionel Sambuc --argc;
9284d9c625SLionel Sambuc p = *++t_av;
9384d9c625SLionel Sambuc }
9484d9c625SLionel Sambuc vtrace_init(p);
9584d9c625SLionel Sambuc ++t_av;
9684d9c625SLionel Sambuc --argc;
9784d9c625SLionel Sambuc continue;
9884d9c625SLionel Sambuc }
9984d9c625SLionel Sambuc #endif
10084d9c625SLionel Sambuc if (!memcmp(*t_av, "-P", sizeof("-P") - 1)) {
10184d9c625SLionel Sambuc if (t_av[0][2] != '\0') {
10284d9c625SLionel Sambuc pflag = 1;
10384d9c625SLionel Sambuc execp = t_av[0] + 2;
10484d9c625SLionel Sambuc ++t_av;
10584d9c625SLionel Sambuc --argc;
10684d9c625SLionel Sambuc continue;
10784d9c625SLionel Sambuc }
10884d9c625SLionel Sambuc if (t_av[1] != NULL) {
10984d9c625SLionel Sambuc pflag = 1;
11084d9c625SLionel Sambuc execp = t_av[1];
11184d9c625SLionel Sambuc t_av += 2;
11284d9c625SLionel Sambuc argc -= 2;
11384d9c625SLionel Sambuc continue;
11484d9c625SLionel Sambuc }
11584d9c625SLionel Sambuc }
11684d9c625SLionel Sambuc *p_av++ = *t_av++;
11784d9c625SLionel Sambuc }
11884d9c625SLionel Sambuc
11984d9c625SLionel Sambuc /*
12084d9c625SLionel Sambuc * Open the communications channels. The pipes are named from the
12184d9c625SLionel Sambuc * parent's viewpoint, meaning the screen reads from rpipe[0] and
12284d9c625SLionel Sambuc * writes to wpipe[1]. The vi process reads from wpipe[0], and it
12384d9c625SLionel Sambuc * writes to rpipe[1].
12484d9c625SLionel Sambuc */
12584d9c625SLionel Sambuc if (channel(rpipe, wpipe) == -1)
12684d9c625SLionel Sambuc fatal();
12784d9c625SLionel Sambuc ipvi->ifd = rpipe[0];
12884d9c625SLionel Sambuc ipvi->ofd = wpipe[1];
12984d9c625SLionel Sambuc
13084d9c625SLionel Sambuc /*
13184d9c625SLionel Sambuc * Reformat our arguments, adding a -I to the list. The first file
13284d9c625SLionel Sambuc * descriptor for the -I argument is vi's input, and the second is
13384d9c625SLionel Sambuc * vi's output.
13484d9c625SLionel Sambuc */
13584d9c625SLionel Sambuc arg_format(execp, &argc, &argv, wpipe[0], rpipe[1]);
13684d9c625SLionel Sambuc
13784d9c625SLionel Sambuc /* Run vi. */
13884d9c625SLionel Sambuc switch (ipvi->pid = fork()) {
13984d9c625SLionel Sambuc case -1: /* Error. */
14084d9c625SLionel Sambuc fatal();
14184d9c625SLionel Sambuc /* NOTREACHED */
14284d9c625SLionel Sambuc case 0: /* Child: Vi. */
14384d9c625SLionel Sambuc (void)close(rpipe[0]);
14484d9c625SLionel Sambuc (void)close(wpipe[1]);
14584d9c625SLionel Sambuc
14684d9c625SLionel Sambuc /*
14784d9c625SLionel Sambuc * If the user didn't override the path and there's a local
14884d9c625SLionel Sambuc * (debugging) nvi, run it, otherwise run the user's path,
14984d9c625SLionel Sambuc * if specified, else run the compiled in path.
15084d9c625SLionel Sambuc */
15184d9c625SLionel Sambuc /* coverity[+toctou] */
15284d9c625SLionel Sambuc if (!pflag && stat("vi-ipc", &sb) == 0)
15384d9c625SLionel Sambuc execvp("vi-ipc", argv);
15484d9c625SLionel Sambuc execvp(execp, argv);
15584d9c625SLionel Sambuc (void)fprintf(stderr,
15684d9c625SLionel Sambuc "%s: %s %s\n", vi_progname, execp, strerror(errno));
15784d9c625SLionel Sambuc (void)fprintf(stderr,
15884d9c625SLionel Sambuc #ifdef DEBUG
15984d9c625SLionel Sambuc "usage: %s [-D] [-P vi_program] [-T trace] [vi arguments]\n",
16084d9c625SLionel Sambuc #else
16184d9c625SLionel Sambuc "usage: %s [-P vi_program] [vi arguments]\n",
16284d9c625SLionel Sambuc #endif
16384d9c625SLionel Sambuc vi_progname);
16484d9c625SLionel Sambuc _exit (1);
16584d9c625SLionel Sambuc default: /* Parent: Screen. */
16684d9c625SLionel Sambuc (void)close(rpipe[1]);
16784d9c625SLionel Sambuc (void)close(wpipe[0]);
16884d9c625SLionel Sambuc break;
16984d9c625SLionel Sambuc }
17084d9c625SLionel Sambuc free(argv[1]);
17184d9c625SLionel Sambuc free(argv);
17284d9c625SLionel Sambuc return (0);
17384d9c625SLionel Sambuc }
17484d9c625SLionel Sambuc
17584d9c625SLionel Sambuc /*
17684d9c625SLionel Sambuc * fatal --
17784d9c625SLionel Sambuc * Fatal error.
17884d9c625SLionel Sambuc */
17984d9c625SLionel Sambuc static void
fatal()18084d9c625SLionel Sambuc fatal()
18184d9c625SLionel Sambuc {
18284d9c625SLionel Sambuc (void)fprintf(stderr, "%s: %s\n", vi_progname, strerror(errno));
18384d9c625SLionel Sambuc exit (1);
18484d9c625SLionel Sambuc }
18584d9c625SLionel Sambuc
18684d9c625SLionel Sambuc static int
channel(int rpipe[2],int wpipe[2])18784d9c625SLionel Sambuc channel(int rpipe[2], int wpipe[2])
18884d9c625SLionel Sambuc {
18984d9c625SLionel Sambuc int x;
19084d9c625SLionel Sambuc if ((x = pipe(rpipe) == -1) || pipe(wpipe) == -1) {
19184d9c625SLionel Sambuc int sockets[2];
19284d9c625SLionel Sambuc
19384d9c625SLionel Sambuc if (x != -1) {
19484d9c625SLionel Sambuc close(rpipe[0]);
19584d9c625SLionel Sambuc close(rpipe[1]);
19684d9c625SLionel Sambuc }
19784d9c625SLionel Sambuc
19884d9c625SLionel Sambuc if (socketpair(AF_LOCAL, SOCK_STREAM, 0, sockets) == -1)
19984d9c625SLionel Sambuc return -1;
20084d9c625SLionel Sambuc
20184d9c625SLionel Sambuc rpipe[0] = sockets[0];
20284d9c625SLionel Sambuc wpipe[0] = sockets[1];
20384d9c625SLionel Sambuc if (((rpipe[1] = dup(sockets[1])) == -1) ||
20484d9c625SLionel Sambuc ((wpipe[1] = dup(sockets[0])) == -1)) {
20584d9c625SLionel Sambuc close(sockets[0]);
20684d9c625SLionel Sambuc close(sockets[1]);
20784d9c625SLionel Sambuc if (rpipe[1] != -1)
20884d9c625SLionel Sambuc close(rpipe[1]);
20984d9c625SLionel Sambuc return -1;
21084d9c625SLionel Sambuc }
21184d9c625SLionel Sambuc
21284d9c625SLionel Sambuc }
21384d9c625SLionel Sambuc return 0;
21484d9c625SLionel Sambuc }
21584d9c625SLionel Sambuc
21684d9c625SLionel Sambuc /*
21784d9c625SLionel Sambuc * arg_format --
21884d9c625SLionel Sambuc * Reformat our arguments to add the -I argument for vi.
21984d9c625SLionel Sambuc */
22084d9c625SLionel Sambuc static void
arg_format(char * execp,int * argcp,char ** argvp[],int i_fd,int o_fd)22184d9c625SLionel Sambuc arg_format(char *execp, int *argcp, char **argvp[], int i_fd, int o_fd)
22284d9c625SLionel Sambuc {
22384d9c625SLionel Sambuc char *iarg, **largv = NULL /* XXX gcc */, *p, **p_av, **t_av;
22484d9c625SLionel Sambuc
22584d9c625SLionel Sambuc /* Get space for the argument array and the -I argument. */
22684d9c625SLionel Sambuc if ((iarg = malloc(64)) == NULL ||
22784d9c625SLionel Sambuc (largv = malloc((*argcp + 3) * sizeof(char *))) == NULL)
22884d9c625SLionel Sambuc fatal();
22984d9c625SLionel Sambuc memcpy(largv + 2, *argvp, *argcp * sizeof(char *) + 1);
23084d9c625SLionel Sambuc
23184d9c625SLionel Sambuc /* Reset argv[0] to be the exec'd program. */
23284d9c625SLionel Sambuc if ((p = strrchr(execp, '/')) == NULL)
23384d9c625SLionel Sambuc largv[0] = execp;
23484d9c625SLionel Sambuc else
23584d9c625SLionel Sambuc largv[0] = p + 1;
23684d9c625SLionel Sambuc
23784d9c625SLionel Sambuc /* Create the -I argument. */
23884d9c625SLionel Sambuc (void)sprintf(iarg, "-I%d%s%d", i_fd, ".", o_fd);
23984d9c625SLionel Sambuc largv[1] = iarg;
24084d9c625SLionel Sambuc
24184d9c625SLionel Sambuc /* Copy any remaining arguments into the array. */
24284d9c625SLionel Sambuc for (p_av = (*argvp) + 1, t_av = largv + 2;;)
24384d9c625SLionel Sambuc if ((*t_av++ = *p_av++) == NULL)
24484d9c625SLionel Sambuc break;
24584d9c625SLionel Sambuc
24684d9c625SLionel Sambuc /* Reset the argument array. */
24784d9c625SLionel Sambuc *argvp = largv;
24884d9c625SLionel Sambuc }
24984d9c625SLionel Sambuc
25084d9c625SLionel Sambuc #ifdef DEBUG
25184d9c625SLionel Sambuc /*
25284d9c625SLionel Sambuc * attach --
25384d9c625SLionel Sambuc * Pause and let the user attach a debugger.
25484d9c625SLionel Sambuc */
25584d9c625SLionel Sambuc static void
attach()25684d9c625SLionel Sambuc attach()
25784d9c625SLionel Sambuc {
25884d9c625SLionel Sambuc int fd;
25984d9c625SLionel Sambuc char ch;
26084d9c625SLionel Sambuc
26184d9c625SLionel Sambuc (void)printf("process %lu waiting, enter <CR> to continue: ",
26284d9c625SLionel Sambuc (u_long)getpid());
26384d9c625SLionel Sambuc (void)fflush(stdout);
26484d9c625SLionel Sambuc
26584d9c625SLionel Sambuc if ((fd = open(_PATH_TTY, O_RDONLY, 0)) < 0) {
26684d9c625SLionel Sambuc (void)fprintf(stderr,
26784d9c625SLionel Sambuc "%s: %s, %s\n", vi_progname, _PATH_TTY, strerror(errno));
26884d9c625SLionel Sambuc exit (1);;
26984d9c625SLionel Sambuc }
27084d9c625SLionel Sambuc do {
27184d9c625SLionel Sambuc if (read(fd, &ch, 1) != 1) {
27284d9c625SLionel Sambuc (void)close(fd);
27384d9c625SLionel Sambuc return;
27484d9c625SLionel Sambuc }
27584d9c625SLionel Sambuc } while (ch != '\n' && ch != '\r');
27684d9c625SLionel Sambuc (void)close(fd);
27784d9c625SLionel Sambuc }
27884d9c625SLionel Sambuc #endif
279