only clean out gs when last window goes
[nvi.git] / ex / ex_script.c
blobc5cb951de38450103390fea982c76e189f170440
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.
7 * This code is derived from software contributed to Berkeley by
8 * Brian Hirt.
10 * See the LICENSE file for redistribution information.
13 #include "config.h"
15 #ifndef lint
16 static const char sccsid[] = "$Id: ex_script.c,v 10.33 2000/06/27 17:19:06 skimo Exp $ (Berkeley) $Date: 2000/06/27 17:19:06 $";
17 #endif /* not lint */
19 #include <sys/types.h>
20 #include <sys/ioctl.h>
21 #include <sys/queue.h>
22 #ifdef HAVE_SYS_SELECT_H
23 #include <sys/select.h>
24 #endif
25 #include <sys/stat.h>
26 #ifdef HAVE_SYS5_PTY
27 #include <sys/stropts.h>
28 #endif
29 #include <sys/time.h>
30 #include <sys/wait.h>
32 #include <bitstring.h>
33 #include <errno.h>
34 #include <fcntl.h>
35 #include <stdio.h> /* XXX: OSF/1 bug: include before <grp.h> */
36 #include <grp.h>
37 #include <limits.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <termios.h>
41 #include <unistd.h>
43 #include "../common/common.h"
44 #include "../vi/vi.h"
45 #include "script.h"
46 #include "pathnames.h"
48 static void sscr_check __P((SCR *));
49 static int sscr_getprompt __P((SCR *));
50 static int sscr_init __P((SCR *));
51 static int sscr_insert __P((SCR *));
52 static int sscr_matchprompt __P((SCR *, char *, size_t, size_t *));
53 static int sscr_pty __P((int *, int *, char *, struct termios *, void *));
54 static int sscr_setprompt __P((SCR *, char *, size_t));
57 * ex_script -- : sc[ript][!] [file]
58 * Switch to script mode.
60 * PUBLIC: int ex_script __P((SCR *, EXCMD *));
62 int
63 ex_script(sp, cmdp)
64 SCR *sp;
65 EXCMD *cmdp;
67 /* Vi only command. */
68 if (!F_ISSET(sp, SC_VI)) {
69 msgq(sp, M_ERR,
70 "150|The script command is only available in vi mode");
71 return (1);
74 /* Switch to the new file. */
75 if (cmdp->argc != 0 && ex_edit(sp, cmdp))
76 return (1);
78 /* Create the shell, figure out the prompt. */
79 if (sscr_init(sp))
80 return (1);
82 return (0);
86 * sscr_init --
87 * Create a pty setup for a shell.
89 static int
90 sscr_init(sp)
91 SCR *sp;
93 SCRIPT *sc;
94 char *sh, *sh_path;
96 /* We're going to need a shell. */
97 if (opts_empty(sp, O_SHELL, 0))
98 return (1);
100 MALLOC_RET(sp, sc, SCRIPT *, sizeof(SCRIPT));
101 sp->script = sc;
102 sc->sh_prompt = NULL;
103 sc->sh_prompt_len = 0;
106 * There are two different processes running through this code.
107 * They are the shell and the parent.
109 sc->sh_master = sc->sh_slave = -1;
111 if (tcgetattr(STDIN_FILENO, &sc->sh_term) == -1) {
112 msgq(sp, M_SYSERR, "tcgetattr");
113 goto err;
117 * Turn off output postprocessing and echo.
119 sc->sh_term.c_oflag &= ~OPOST;
120 sc->sh_term.c_cflag &= ~(ECHO|ECHOE|ECHONL|ECHOK);
122 #ifdef TIOCGWINSZ
123 if (ioctl(STDIN_FILENO, TIOCGWINSZ, &sc->sh_win) == -1) {
124 msgq(sp, M_SYSERR, "tcgetattr");
125 goto err;
128 if (sscr_pty(&sc->sh_master,
129 &sc->sh_slave, sc->sh_name, &sc->sh_term, &sc->sh_win) == -1) {
130 msgq(sp, M_SYSERR, "pty");
131 goto err;
133 #else
134 if (sscr_pty(&sc->sh_master,
135 &sc->sh_slave, sc->sh_name, &sc->sh_term, NULL) == -1) {
136 msgq(sp, M_SYSERR, "pty");
137 goto err;
139 #endif
142 * __TK__ huh?
143 * Don't use vfork() here, because the signal semantics differ from
144 * implementation to implementation.
146 switch (sc->sh_pid = fork()) {
147 case -1: /* Error. */
148 msgq(sp, M_SYSERR, "fork");
149 err: if (sc->sh_master != -1)
150 (void)close(sc->sh_master);
151 if (sc->sh_slave != -1)
152 (void)close(sc->sh_slave);
153 return (1);
154 case 0: /* Utility. */
156 * XXX
157 * So that shells that do command line editing turn it off.
159 (void)setenv("TERM", "emacs", 1);
160 (void)setenv("TERMCAP", "emacs:", 1);
161 (void)setenv("EMACS", "t", 1);
163 (void)setsid();
164 #ifdef TIOCSCTTY
166 * 4.4BSD allocates a controlling terminal using the TIOCSCTTY
167 * ioctl, not by opening a terminal device file. POSIX 1003.1
168 * doesn't define a portable way to do this. If TIOCSCTTY is
169 * not available, hope that the open does it.
171 (void)ioctl(sc->sh_slave, TIOCSCTTY, 0);
172 #endif
173 (void)close(sc->sh_master);
174 (void)dup2(sc->sh_slave, STDIN_FILENO);
175 (void)dup2(sc->sh_slave, STDOUT_FILENO);
176 (void)dup2(sc->sh_slave, STDERR_FILENO);
177 (void)close(sc->sh_slave);
179 /* Assumes that all shells have -i. */
180 sh_path = O_STR(sp, O_SHELL);
181 if ((sh = strrchr(sh_path, '/')) == NULL)
182 sh = sh_path;
183 else
184 ++sh;
185 execl(sh_path, sh, "-i", NULL);
186 msgq_str(sp, M_SYSERR, sh_path, "execl: %s");
187 _exit(127);
188 default: /* Parent. */
189 break;
192 if (sscr_getprompt(sp))
193 return (1);
195 F_SET(sp, SC_SCRIPT);
196 F_SET(sp->gp, G_SCRWIN);
197 return (0);
201 * sscr_getprompt --
202 * Eat lines printed by the shell until a line with no trailing
203 * carriage return comes; set the prompt from that line.
205 static int
206 sscr_getprompt(sp)
207 SCR *sp;
209 struct timeval tv;
210 CHAR_T *endp, *p, *t, buf[1024];
211 SCRIPT *sc;
212 fd_set fdset;
213 db_recno_t lline;
214 size_t llen, len;
215 u_int value;
216 int nr;
218 FD_ZERO(&fdset);
219 endp = buf;
220 len = sizeof(buf);
222 /* Wait up to a second for characters to read. */
223 tv.tv_sec = 5;
224 tv.tv_usec = 0;
225 sc = sp->script;
226 FD_SET(sc->sh_master, &fdset);
227 switch (select(sc->sh_master + 1, &fdset, NULL, NULL, &tv)) {
228 case -1: /* Error or interrupt. */
229 msgq(sp, M_SYSERR, "select");
230 goto prompterr;
231 case 0: /* Timeout */
232 msgq(sp, M_ERR, "Error: timed out");
233 goto prompterr;
234 case 1: /* Characters to read. */
235 break;
238 /* Read the characters. */
239 more: len = sizeof(buf) - (endp - buf);
240 switch (nr = read(sc->sh_master, endp, len)) {
241 case 0: /* EOF. */
242 msgq(sp, M_ERR, "Error: shell: EOF");
243 goto prompterr;
244 case -1: /* Error or interrupt. */
245 msgq(sp, M_SYSERR, "shell");
246 goto prompterr;
247 default:
248 endp += nr;
249 break;
252 /* If any complete lines, push them into the file. */
253 for (p = t = buf; p < endp; ++p) {
254 value = KEY_VAL(sp, *p);
255 if (value == K_CR || value == K_NL) {
256 if (db_last(sp, &lline) ||
257 db_append(sp, 0, lline, t, p - t))
258 goto prompterr;
259 t = p + 1;
262 if (p > buf) {
263 memmove(buf, t, endp - t);
264 endp = buf + (endp - t);
266 if (endp == buf)
267 goto more;
269 /* Wait up 1/10 of a second to make sure that we got it all. */
270 tv.tv_sec = 0;
271 tv.tv_usec = 100000;
272 switch (select(sc->sh_master + 1, &fdset, NULL, NULL, &tv)) {
273 case -1: /* Error or interrupt. */
274 msgq(sp, M_SYSERR, "select");
275 goto prompterr;
276 case 0: /* Timeout */
277 break;
278 case 1: /* Characters to read. */
279 goto more;
282 /* Timed out, so theoretically we have a prompt. */
283 llen = endp - buf;
284 endp = buf;
286 /* Append the line into the file. */
287 if (db_last(sp, &lline) || db_append(sp, 0, lline, buf, llen)) {
288 prompterr: sscr_end(sp);
289 return (1);
292 return (sscr_setprompt(sp, buf, llen));
296 * sscr_exec --
297 * Take a line and hand it off to the shell.
299 * PUBLIC: int sscr_exec __P((SCR *, db_recno_t));
302 sscr_exec(sp, lno)
303 SCR *sp;
304 db_recno_t lno;
306 SCRIPT *sc;
307 db_recno_t last_lno;
308 size_t blen, len, last_len, tlen;
309 int isempty, matchprompt, nw, rval;
310 char *bp;
311 CHAR_T *p;
313 /* If there's a prompt on the last line, append the command. */
314 if (db_last(sp, &last_lno))
315 return (1);
316 if (db_get(sp, last_lno, DBG_FATAL, &p, &last_len))
317 return (1);
318 if (sscr_matchprompt(sp, p, last_len, &tlen) && tlen == 0) {
319 matchprompt = 1;
320 GET_SPACE_RET(sp, bp, blen, last_len + 128);
321 memmove(bp, p, last_len);
322 } else
323 matchprompt = 0;
325 /* Get something to execute. */
326 if (db_eget(sp, lno, &p, &len, &isempty)) {
327 if (isempty)
328 goto empty;
329 goto err1;
332 /* Empty lines aren't interesting. */
333 if (len == 0)
334 goto empty;
336 /* Delete any prompt. */
337 if (sscr_matchprompt(sp, p, len, &tlen)) {
338 if (tlen == len) {
339 empty: msgq(sp, M_BERR, "151|No command to execute");
340 goto err1;
342 p += (len - tlen);
343 len = tlen;
346 /* Push the line to the shell. */
347 sc = sp->script;
348 if ((nw = write(sc->sh_master, p, len)) != len)
349 goto err2;
350 rval = 0;
351 if (write(sc->sh_master, "\n", 1) != 1) {
352 err2: if (nw == 0)
353 errno = EIO;
354 msgq(sp, M_SYSERR, "shell");
355 goto err1;
358 if (matchprompt) {
359 ADD_SPACE_RET(sp, bp, blen, last_len + len);
360 memmove(bp + last_len, p, len);
361 if (db_set(sp, last_lno, bp, last_len + len))
362 err1: rval = 1;
364 if (matchprompt)
365 FREE_SPACE(sp, bp, blen);
366 return (rval);
370 * sscr_input --
371 * Read any waiting shell input.
373 * PUBLIC: int sscr_input __P((SCR *));
376 sscr_input(sp)
377 SCR *sp;
379 GS *gp;
380 WIN *wp;
381 struct timeval poll;
382 fd_set rdfd;
383 int maxfd;
385 gp = sp->gp;
386 wp = sp->wp;
388 loop: maxfd = 0;
389 FD_ZERO(&rdfd);
390 poll.tv_sec = 0;
391 poll.tv_usec = 0;
393 /* Set up the input mask. */
394 for (sp = wp->scrq.cqh_first; sp != (void *)&wp->scrq;
395 sp = sp->q.cqe_next)
396 if (F_ISSET(sp, SC_SCRIPT)) {
397 FD_SET(sp->script->sh_master, &rdfd);
398 if (sp->script->sh_master > maxfd)
399 maxfd = sp->script->sh_master;
402 /* Check for input. */
403 switch (select(maxfd + 1, &rdfd, NULL, NULL, &poll)) {
404 case -1:
405 msgq(sp, M_SYSERR, "select");
406 return (1);
407 case 0:
408 return (0);
409 default:
410 break;
413 /* Read the input. */
414 for (sp = wp->scrq.cqh_first; sp != (void *)&wp->scrq;
415 sp = sp->q.cqe_next)
416 if (F_ISSET(sp, SC_SCRIPT) &&
417 FD_ISSET(sp->script->sh_master, &rdfd) &&
418 sscr_insert(sp))
419 return (1);
420 goto loop;
424 * sscr_insert --
425 * Take a line from the shell and insert it into the file.
427 static int
428 sscr_insert(sp)
429 SCR *sp;
431 struct timeval tv;
432 CHAR_T *endp, *p, *t;
433 SCRIPT *sc;
434 fd_set rdfd;
435 db_recno_t lno;
436 size_t blen, len, tlen;
437 u_int value;
438 int nr, rval;
439 char *bp;
441 /* Find out where the end of the file is. */
442 if (db_last(sp, &lno))
443 return (1);
445 #define MINREAD 1024
446 GET_SPACE_RET(sp, bp, blen, MINREAD);
447 endp = bp;
449 /* Read the characters. */
450 rval = 1;
451 sc = sp->script;
452 more: switch (nr = read(sc->sh_master, endp, MINREAD)) {
453 case 0: /* EOF; shell just exited. */
454 sscr_end(sp);
455 rval = 0;
456 goto ret;
457 case -1: /* Error or interrupt. */
458 msgq(sp, M_SYSERR, "shell");
459 goto ret;
460 default:
461 endp += nr;
462 break;
465 /* Append the lines into the file. */
466 for (p = t = bp; p < endp; ++p) {
467 value = KEY_VAL(sp, *p);
468 if (value == K_CR || value == K_NL) {
469 len = p - t;
470 if (db_append(sp, 1, lno++, t, len))
471 goto ret;
472 t = p + 1;
475 if (p > t) {
476 len = p - t;
478 * If the last thing from the shell isn't another prompt, wait
479 * up to 1/10 of a second for more stuff to show up, so that
480 * we don't break the output into two separate lines. Don't
481 * want to hang indefinitely because some program is hanging,
482 * confused the shell, or whatever.
484 if (!sscr_matchprompt(sp, t, len, &tlen) || tlen != 0) {
485 tv.tv_sec = 0;
486 tv.tv_usec = 100000;
487 FD_ZERO(&rdfd);
488 FD_SET(sc->sh_master, &rdfd);
489 if (select(sc->sh_master + 1,
490 &rdfd, NULL, NULL, &tv) == 1) {
491 memmove(bp, t, len);
492 endp = bp + len;
493 goto more;
496 if (sscr_setprompt(sp, t, len))
497 return (1);
498 if (db_append(sp, 1, lno++, t, len))
499 goto ret;
502 /* The cursor moves to EOF. */
503 sp->lno = lno;
504 sp->cno = len ? len - 1 : 0;
505 rval = vs_refresh(sp, 1);
507 ret: FREE_SPACE(sp, bp, blen);
508 return (rval);
512 * sscr_setprompt --
514 * Set the prompt to the last line we got from the shell.
517 static int
518 sscr_setprompt(sp, buf, len)
519 SCR *sp;
520 char *buf;
521 size_t len;
523 SCRIPT *sc;
525 sc = sp->script;
526 if (sc->sh_prompt)
527 free(sc->sh_prompt);
528 MALLOC(sp, sc->sh_prompt, char *, len + 1);
529 if (sc->sh_prompt == NULL) {
530 sscr_end(sp);
531 return (1);
533 memmove(sc->sh_prompt, buf, len);
534 sc->sh_prompt_len = len;
535 sc->sh_prompt[len] = '\0';
536 return (0);
540 * sscr_matchprompt --
541 * Check to see if a line matches the prompt. Nul's indicate
542 * parts that can change, in both content and size.
544 static int
545 sscr_matchprompt(sp, lp, line_len, lenp)
546 SCR *sp;
547 char *lp;
548 size_t line_len, *lenp;
550 SCRIPT *sc;
551 size_t prompt_len;
552 char *pp;
554 sc = sp->script;
555 if (line_len < (prompt_len = sc->sh_prompt_len))
556 return (0);
558 for (pp = sc->sh_prompt;
559 prompt_len && line_len; --prompt_len, --line_len) {
560 if (*pp == '\0') {
561 for (; prompt_len && *pp == '\0'; --prompt_len, ++pp);
562 if (!prompt_len)
563 return (0);
564 for (; line_len && *lp != *pp; --line_len, ++lp);
565 if (!line_len)
566 return (0);
568 if (*pp++ != *lp++)
569 break;
572 if (prompt_len)
573 return (0);
574 if (lenp != NULL)
575 *lenp = line_len;
576 return (1);
580 * sscr_end --
581 * End the pipe to a shell.
583 * PUBLIC: int sscr_end __P((SCR *));
586 sscr_end(sp)
587 SCR *sp;
589 SCRIPT *sc;
591 if ((sc = sp->script) == NULL)
592 return (0);
594 /* Turn off the script flags. */
595 F_CLR(sp, SC_SCRIPT);
596 sscr_check(sp);
598 /* Close down the parent's file descriptors. */
599 if (sc->sh_master != -1)
600 (void)close(sc->sh_master);
601 if (sc->sh_slave != -1)
602 (void)close(sc->sh_slave);
604 /* This should have killed the child. */
605 (void)proc_wait(sp, (long)sc->sh_pid, "script-shell", 0, 0);
607 /* Free memory. */
608 free(sc->sh_prompt);
609 free(sc);
610 sp->script = NULL;
612 return (0);
616 * sscr_check --
617 * Set/clear the global scripting bit.
619 static void
620 sscr_check(sp)
621 SCR *sp;
623 GS *gp;
624 WIN *wp;
626 gp = sp->gp;
627 wp = sp->wp;
628 for (sp = wp->scrq.cqh_first; sp != (void *)&wp->scrq;
629 sp = sp->q.cqe_next)
630 if (F_ISSET(sp, SC_SCRIPT)) {
631 F_SET(gp, G_SCRWIN);
632 return;
634 F_CLR(gp, G_SCRWIN);
637 #ifdef HAVE_SYS5_PTY
638 static int ptys_open __P((int, char *));
639 static int ptym_open __P((char *));
641 static int
642 sscr_pty(amaster, aslave, name, termp, winp)
643 int *amaster, *aslave;
644 char *name;
645 struct termios *termp;
646 void *winp;
648 int master, slave, ttygid;
650 /* open master terminal */
651 if ((master = ptym_open(name)) < 0) {
652 errno = ENOENT; /* out of ptys */
653 return (-1);
656 /* open slave terminal */
657 if ((slave = ptys_open(master, name)) >= 0) {
658 *amaster = master;
659 *aslave = slave;
660 } else {
661 errno = ENOENT; /* out of ptys */
662 return (-1);
665 if (termp)
666 (void) tcsetattr(slave, TCSAFLUSH, termp);
667 #ifdef TIOCSWINSZ
668 if (winp != NULL)
669 (void) ioctl(slave, TIOCSWINSZ, (struct winsize *)winp);
670 #endif
671 return (0);
675 * ptym_open --
676 * This function opens a master pty and returns the file descriptor
677 * to it. pts_name is also returned which is the name of the slave.
679 static int
680 ptym_open(pts_name)
681 char *pts_name;
683 int fdm;
684 char *ptr, *ptsname();
686 strcpy(pts_name, _PATH_SYSV_PTY);
687 if ((fdm = open(pts_name, O_RDWR)) < 0 )
688 return (-1);
690 if (grantpt(fdm) < 0) {
691 close(fdm);
692 return (-2);
695 if (unlockpt(fdm) < 0) {
696 close(fdm);
697 return (-3);
700 if (unlockpt(fdm) < 0) {
701 close(fdm);
702 return (-3);
705 /* get slave's name */
706 if ((ptr = ptsname(fdm)) == NULL) {
707 close(fdm);
708 return (-3);
710 strcpy(pts_name, ptr);
711 return (fdm);
715 * ptys_open --
716 * This function opens the slave pty.
718 static int
719 ptys_open(fdm, pts_name)
720 int fdm;
721 char *pts_name;
723 int fds;
725 if ((fds = open(pts_name, O_RDWR)) < 0) {
726 close(fdm);
727 return (-5);
730 if (ioctl(fds, I_PUSH, "ptem") < 0) {
731 close(fds);
732 close(fdm);
733 return (-6);
736 if (ioctl(fds, I_PUSH, "ldterm") < 0) {
737 close(fds);
738 close(fdm);
739 return (-7);
742 if (ioctl(fds, I_PUSH, "ttcompat") < 0) {
743 close(fds);
744 close(fdm);
745 return (-8);
748 return (fds);
751 #else /* !HAVE_SYS5_PTY */
753 static int
754 sscr_pty(amaster, aslave, name, termp, winp)
755 int *amaster, *aslave;
756 char *name;
757 struct termios *termp;
758 void *winp;
760 static char line[] = "/dev/ptyXX";
761 register char *cp1, *cp2;
762 register int master, slave, ttygid;
763 struct group *gr;
765 if ((gr = getgrnam("tty")) != NULL)
766 ttygid = gr->gr_gid;
767 else
768 ttygid = -1;
770 for (cp1 = "pqrs"; *cp1; cp1++) {
771 line[8] = *cp1;
772 for (cp2 = "0123456789abcdef"; *cp2; cp2++) {
773 line[5] = 'p';
774 line[9] = *cp2;
775 if ((master = open(line, O_RDWR, 0)) == -1) {
776 if (errno == ENOENT)
777 return (-1); /* out of ptys */
778 } else {
779 line[5] = 't';
780 (void) chown(line, getuid(), ttygid);
781 (void) chmod(line, S_IRUSR|S_IWUSR|S_IWGRP);
782 #ifdef HAVE_REVOKE
783 (void) revoke(line);
784 #endif
785 if ((slave = open(line, O_RDWR, 0)) != -1) {
786 *amaster = master;
787 *aslave = slave;
788 if (name)
789 strcpy(name, line);
790 if (termp)
791 (void) tcsetattr(slave,
792 TCSAFLUSH, termp);
793 #ifdef TIOCSWINSZ
794 if (winp)
795 (void) ioctl(slave, TIOCSWINSZ,
796 (char *)winp);
797 #endif
798 return (0);
800 (void) close(master);
804 errno = ENOENT; /* out of ptys */
805 return (-1);
807 #endif /* HAVE_SYS5_PTY */