3 * Rob Zimmermann. All rights reserved.
5 * Keith Bostic. All rights reserved.
7 * See the LICENSE file for redistribution information.
13 static const char sccsid
[] = "$Id: ip_run.c,v 8.7 1996/12/11 20:57:03 bostic Exp $ (Berkeley) $Date: 1996/12/11 20:57:03 $";
16 #include <sys/types.h>
17 #include <sys/queue.h>
20 #include <bitstring.h>
28 #include "../common/common.h"
30 #include "ipc_extern.h"
31 #include "pathnames.h"
33 static void arg_format
__P((char *, int *, char **[], int, int));
34 static void fatal
__P((void));
36 static void attach
__P((void));
39 int vi_ofd
= -1; /* Global: output fd. */
40 char *vi_progname
= "vi"; /* Global: program name. */
46 * PUBLIC: int vi_run __P((int, char *[], int *, int *, pid_t *));
49 vi_run(argc
, argv
, ip
, op
, pidp
)
55 int pflag
, rpipe
[2], wpipe
[2];
56 char *execp
, *p
, **p_av
, **t_av
;
61 /* Strip out any arguments that vi isn't going to understand. */
62 for (p_av
= t_av
= argv
;;) {
67 if (!strcmp(*t_av
, "--")) {
68 while ((*p_av
++ = *t_av
++) != NULL
);
72 if (!memcmp(*t_av
, "-D", sizeof("-D") - 1)) {
79 if (!memcmp(*t_av
, "-T", sizeof("-T") - 1)) {
80 p
= &t_av
[0][sizeof("-T") - 1];
91 if (!memcmp(*t_av
, "-P", sizeof("-P") - 1)) {
92 if (t_av
[0][2] != '\0') {
99 if (t_av
[1] != NULL
) {
111 * Open the communications channels. The pipes are named from the
112 * parent's viewpoint, meaning the screen reads from rpipe[0] and
113 * writes to wpipe[1]. The vi process reads from wpipe[0], and it
114 * writes to rpipe[1].
116 if (pipe(rpipe
) == -1 || pipe(wpipe
) == -1)
122 * Reformat our arguments, adding a -I to the list. The first file
123 * descriptor for the -I argument is vi's input, and the second is
126 arg_format(execp
, &argc
, &argv
, wpipe
[0], rpipe
[1]);
129 switch (*pidp
= fork()) {
130 case -1: /* Error. */
133 case 0: /* Child: Vi. */
134 (void)close(rpipe
[0]);
135 (void)close(wpipe
[1]);
138 * If the user didn't override the path and there's a local
139 * (debugging) nvi, run it, otherwise run the user's path,
140 * if specified, else run the compiled in path.
142 if (!pflag
&& stat("vi", &sb
) == 0)
145 (void)fprintf(stderr
,
146 "%s: %s %s\n", vi_progname
, execp
, strerror(errno
));
147 (void)fprintf(stderr
,
149 "usage: %s [-D] [-P vi_program] [-T trace] [vi arguments]\n",
151 "usage: %s [-P vi_program] [vi arguments]\n",
155 default: /* Parent: Screen. */
156 (void)close(rpipe
[1]);
157 (void)close(wpipe
[0]);
170 (void)fprintf(stderr
, "%s: %s\n", vi_progname
, strerror(errno
));
176 * Reformat our arguments to add the -I argument for vi.
179 arg_format(execp
, argcp
, argvp
, i_fd
, o_fd
)
180 char *execp
, **argvp
[];
181 int *argcp
, i_fd
, o_fd
;
183 char *iarg
, **largv
, *p
, **p_av
, **t_av
;
185 /* Get space for the argument array and the -I argument. */
186 if ((iarg
= malloc(64)) == NULL
||
187 (largv
= malloc((*argcp
+ 3) * sizeof(char *))) == NULL
)
189 memcpy(largv
+ 2, *argvp
, *argcp
* sizeof(char *) + 1);
191 /* Reset argv[0] to be the exec'd program. */
192 if ((p
= strrchr(execp
, '/')) == NULL
)
197 /* Create the -I argument. */
198 (void)sprintf(iarg
, "-I%d%s%d", i_fd
, ".", o_fd
);
201 /* Copy any remaining arguments into the array. */
202 for (p_av
= (*argvp
) + 1, t_av
= largv
+ 2;;)
203 if ((*t_av
++ = *p_av
++) == NULL
)
206 /* Reset the argument array. */
213 * Pause and let the user attach a debugger.
221 (void)printf("process %lu waiting, enter <CR> to continue: ",
223 (void)fflush(stdout
);
225 if ((fd
= open(_PATH_TTY
, O_RDONLY
, 0)) < 0) {
226 (void)fprintf(stderr
,
227 "%s: %s, %s\n", vi_progname
, _PATH_TTY
, strerror(errno
));
231 if (read(fd
, &ch
, 1) != 1) {
235 } while (ch
!= '\n' && ch
!= '\r');