ip_run.c revision 1.1 1 1.1 christos /*-
2 1.1 christos * Copyright (c) 1996
3 1.1 christos * Rob Zimmermann. All rights reserved.
4 1.1 christos * Copyright (c) 1996
5 1.1 christos * Keith Bostic. All rights reserved.
6 1.1 christos *
7 1.1 christos * See the LICENSE file for redistribution information.
8 1.1 christos */
9 1.1 christos
10 1.1 christos #include "config.h"
11 1.1 christos
12 1.1 christos #ifndef lint
13 1.1 christos 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 ";
14 1.1 christos #endif /* not lint */
15 1.1 christos
16 1.1 christos #include <sys/types.h>
17 1.1 christos #include <sys/queue.h>
18 1.1 christos #include <sys/stat.h>
19 1.1 christos
20 1.1 christos #include <bitstring.h>
21 1.1 christos #include <errno.h>
22 1.1 christos #include <fcntl.h>
23 1.1 christos #include <stdio.h>
24 1.1 christos #include <stdlib.h>
25 1.1 christos #include <string.h>
26 1.1 christos #include <unistd.h>
27 1.1 christos
28 1.1 christos #include <sys/socket.h>
29 1.1 christos
30 1.1 christos #include "../common/common.h"
31 1.1 christos #include "ip.h"
32 1.1 christos #include "pathnames.h"
33 1.1 christos
34 1.1 christos static void arg_format __P((char *, int *, char **[], int, int));
35 1.1 christos static void fatal __P((void));
36 1.1 christos #ifdef DEBUG
37 1.1 christos static void attach __P((void));
38 1.1 christos #endif
39 1.1 christos static int channel(int rpipe[2], int wpipe[2]);
40 1.1 christos
41 1.1 christos char *vi_progname = "vi"; /* Global: program name. */
42 1.1 christos
43 1.1 christos /*
44 1.1 christos * vi_run --
45 1.1 christos * Run the vi program.
46 1.1 christos *
47 1.1 christos * PUBLIC: int vi_run __P((IPVI *, int, char *[]));
48 1.1 christos */
49 1.1 christos int
50 1.1 christos vi_run(ipvi, argc, argv)
51 1.1 christos IPVI *ipvi;
52 1.1 christos int argc;
53 1.1 christos char *argv[];
54 1.1 christos {
55 1.1 christos struct stat sb;
56 1.1 christos int pflag, rpipe[2], wpipe[2];
57 1.1 christos char *execp, **p_av, **t_av;
58 1.1 christos
59 1.1 christos pflag = 0;
60 1.1 christos execp = VI;
61 1.1 christos
62 1.1 christos /* Strip out any arguments that vi isn't going to understand. */
63 1.1 christos for (p_av = t_av = argv;;) {
64 1.1 christos if (*t_av == NULL) {
65 1.1 christos *p_av = NULL;
66 1.1 christos break;
67 1.1 christos }
68 1.1 christos if (!strcmp(*t_av, "--")) {
69 1.1 christos while ((*p_av++ = *++t_av) != NULL);
70 1.1 christos break;
71 1.1 christos }
72 1.1 christos #ifdef DEBUG
73 1.1 christos if (!memcmp(*t_av, "-D", sizeof("-D") - 1)) {
74 1.1 christos attach();
75 1.1 christos
76 1.1 christos ++t_av;
77 1.1 christos --argc;
78 1.1 christos continue;
79 1.1 christos }
80 1.1 christos #endif
81 1.1 christos #ifdef TRACE
82 1.1 christos if (!memcmp(*t_av, "-T", sizeof("-T") - 1)) {
83 1.1 christos char *p = &t_av[0][sizeof("-T") - 1];
84 1.1 christos if (*p == '\0') {
85 1.1 christos --argc;
86 1.1 christos p = *++t_av;
87 1.1 christos }
88 1.1 christos vtrace_init(p);
89 1.1 christos ++t_av;
90 1.1 christos --argc;
91 1.1 christos continue;
92 1.1 christos }
93 1.1 christos #endif
94 1.1 christos if (!memcmp(*t_av, "-P", sizeof("-P") - 1)) {
95 1.1 christos if (t_av[0][2] != '\0') {
96 1.1 christos pflag = 1;
97 1.1 christos execp = t_av[0] + 2;
98 1.1 christos ++t_av;
99 1.1 christos --argc;
100 1.1 christos continue;
101 1.1 christos }
102 1.1 christos if (t_av[1] != NULL) {
103 1.1 christos pflag = 1;
104 1.1 christos execp = t_av[1];
105 1.1 christos t_av += 2;
106 1.1 christos argc -= 2;
107 1.1 christos continue;
108 1.1 christos }
109 1.1 christos }
110 1.1 christos *p_av++ = *t_av++;
111 1.1 christos }
112 1.1 christos
113 1.1 christos /*
114 1.1 christos * Open the communications channels. The pipes are named from the
115 1.1 christos * parent's viewpoint, meaning the screen reads from rpipe[0] and
116 1.1 christos * writes to wpipe[1]. The vi process reads from wpipe[0], and it
117 1.1 christos * writes to rpipe[1].
118 1.1 christos */
119 1.1 christos if (channel(rpipe, wpipe) == -1)
120 1.1 christos fatal();
121 1.1 christos ipvi->ifd = rpipe[0];
122 1.1 christos ipvi->ofd = wpipe[1];
123 1.1 christos
124 1.1 christos /*
125 1.1 christos * Reformat our arguments, adding a -I to the list. The first file
126 1.1 christos * descriptor for the -I argument is vi's input, and the second is
127 1.1 christos * vi's output.
128 1.1 christos */
129 1.1 christos arg_format(execp, &argc, &argv, wpipe[0], rpipe[1]);
130 1.1 christos
131 1.1 christos /* Run vi. */
132 1.1 christos switch (ipvi->pid = fork()) {
133 1.1 christos case -1: /* Error. */
134 1.1 christos fatal();
135 1.1 christos /* NOTREACHED */
136 1.1 christos case 0: /* Child: Vi. */
137 1.1 christos (void)close(rpipe[0]);
138 1.1 christos (void)close(wpipe[1]);
139 1.1 christos
140 1.1 christos /*
141 1.1 christos * If the user didn't override the path and there's a local
142 1.1 christos * (debugging) nvi, run it, otherwise run the user's path,
143 1.1 christos * if specified, else run the compiled in path.
144 1.1 christos */
145 1.1 christos if (!pflag && stat("vi-ipc", &sb) == 0)
146 1.1 christos execv("vi-ipc", argv);
147 1.1 christos execv(execp, argv);
148 1.1 christos (void)fprintf(stderr,
149 1.1 christos "%s: %s %s\n", vi_progname, execp, strerror(errno));
150 1.1 christos (void)fprintf(stderr,
151 1.1 christos #ifdef DEBUG
152 1.1 christos "usage: %s [-D] [-P vi_program] [-T trace] [vi arguments]\n",
153 1.1 christos #else
154 1.1 christos "usage: %s [-P vi_program] [vi arguments]\n",
155 1.1 christos #endif
156 1.1 christos vi_progname);
157 1.1 christos _exit (1);
158 1.1 christos default: /* Parent: Screen. */
159 1.1 christos (void)close(rpipe[1]);
160 1.1 christos (void)close(wpipe[0]);
161 1.1 christos break;
162 1.1 christos }
163 1.1 christos return (0);
164 1.1 christos }
165 1.1 christos
166 1.1 christos /*
167 1.1 christos * fatal --
168 1.1 christos * Fatal error.
169 1.1 christos */
170 1.1 christos static void
171 1.1 christos fatal()
172 1.1 christos {
173 1.1 christos (void)fprintf(stderr, "%s: %s\n", vi_progname, strerror(errno));
174 1.1 christos exit (1);
175 1.1 christos }
176 1.1 christos
177 1.1 christos static
178 1.1 christos int channel(int rpipe[2], int wpipe[2])
179 1.1 christos {
180 1.1 christos if (0) {
181 1.1 christos
182 1.1 christos if (pipe(rpipe) == -1 || pipe(wpipe) == -1)
183 1.1 christos return -1;
184 1.1 christos
185 1.1 christos } else {
186 1.1 christos
187 1.1 christos int sockets[2];
188 1.1 christos
189 1.1 christos if (socketpair(AF_LOCAL, SOCK_STREAM, 0, sockets) == -1)
190 1.1 christos return -1;
191 1.1 christos
192 1.1 christos rpipe[0] = sockets[0];
193 1.1 christos wpipe[0] = sockets[1];
194 1.1 christos if (((rpipe[1] = dup(sockets[1])) == -1) ||
195 1.1 christos ((wpipe[1] = dup(sockets[0])) == -1))
196 1.1 christos return -1;
197 1.1 christos
198 1.1 christos }
199 1.1 christos }
200 1.1 christos
201 1.1 christos /*
202 1.1 christos * arg_format --
203 1.1 christos * Reformat our arguments to add the -I argument for vi.
204 1.1 christos */
205 1.1 christos static void
206 1.1 christos arg_format(execp, argcp, argvp, i_fd, o_fd)
207 1.1 christos char *execp, **argvp[];
208 1.1 christos int *argcp, i_fd, o_fd;
209 1.1 christos {
210 1.1 christos char *iarg, **largv, *p, **p_av, **t_av;
211 1.1 christos
212 1.1 christos /* Get space for the argument array and the -I argument. */
213 1.1 christos if ((iarg = malloc(64)) == NULL ||
214 1.1 christos (largv = malloc((*argcp + 3) * sizeof(char *))) == NULL)
215 1.1 christos fatal();
216 1.1 christos memcpy(largv + 2, *argvp, *argcp * sizeof(char *) + 1);
217 1.1 christos
218 1.1 christos /* Reset argv[0] to be the exec'd program. */
219 1.1 christos if ((p = strrchr(execp, '/')) == NULL)
220 1.1 christos largv[0] = execp;
221 1.1 christos else
222 1.1 christos largv[0] = p + 1;
223 1.1 christos
224 1.1 christos /* Create the -I argument. */
225 1.1 christos (void)sprintf(iarg, "-I%d%s%d", i_fd, ".", o_fd);
226 1.1 christos largv[1] = iarg;
227 1.1 christos
228 1.1 christos /* Copy any remaining arguments into the array. */
229 1.1 christos for (p_av = (*argvp) + 1, t_av = largv + 2;;)
230 1.1 christos if ((*t_av++ = *p_av++) == NULL)
231 1.1 christos break;
232 1.1 christos
233 1.1 christos /* Reset the argument array. */
234 1.1 christos *argvp = largv;
235 1.1 christos }
236 1.1 christos
237 1.1 christos #ifdef DEBUG
238 1.1 christos /*
239 1.1 christos * attach --
240 1.1 christos * Pause and let the user attach a debugger.
241 1.1 christos */
242 1.1 christos static void
243 1.1 christos attach()
244 1.1 christos {
245 1.1 christos int fd;
246 1.1 christos char ch;
247 1.1 christos
248 1.1 christos (void)printf("process %lu waiting, enter <CR> to continue: ",
249 1.1 christos (u_long)getpid());
250 1.1 christos (void)fflush(stdout);
251 1.1 christos
252 1.1 christos if ((fd = open(_PATH_TTY, O_RDONLY, 0)) < 0) {
253 1.1 christos (void)fprintf(stderr,
254 1.1 christos "%s: %s, %s\n", vi_progname, _PATH_TTY, strerror(errno));
255 1.1 christos exit (1);;
256 1.1 christos }
257 1.1 christos do {
258 1.1 christos if (read(fd, &ch, 1) != 1) {
259 1.1 christos (void)close(fd);
260 1.1 christos return;
261 1.1 christos }
262 1.1 christos } while (ch != '\n' && ch != '\r');
263 1.1 christos (void)close(fd);
264 1.1 christos }
265 1.1 christos #endif
266