First test with an external encoding.
[nvi.git] / ex / ex_script.c
blobb5674a6d9b7f7f87b56db041f8a714750bb9ecae
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.37 2000/07/16 20:49:32 skimo Exp $ (Berkeley) $Date: 2000/07/16 20:49:32 $";
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_T *, size_t, size_t *));
53 static int sscr_pty __P((int *, int *, char *, struct termios *, void *));
54 static int sscr_setprompt __P((SCR *, CHAR_T *, 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_T *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_RETW(sp, bp, blen, last_len + 128);
321 MEMMOVEW(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_RETW(sp, bp, blen, last_len + len);
360 MEMMOVEW(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_SPACEW(sp, bp, blen);
366 return (rval);
370 * sscr_check_input -
371 * Check whether any input from shell or passed set.
373 * PUBLIC: int sscr_check_input __P((SCR *sp, fd_set *rdfd, int maxfd));
376 sscr_check_input(SCR *sp, fd_set *fdset, int maxfd)
378 fd_set rdfd;
379 SCR *tsp;
380 WIN *wp;
382 wp = sp->wp;
384 loop: memcpy(&rdfd, fdset, sizeof(fd_set));
386 for (tsp = wp->scrq.cqh_first;
387 tsp != (void *)&wp->scrq; tsp = tsp->q.cqe_next)
388 if (F_ISSET(sp, SC_SCRIPT)) {
389 FD_SET(sp->script->sh_master, &rdfd);
390 if (sp->script->sh_master > maxfd)
391 maxfd = sp->script->sh_master;
393 switch (select(maxfd + 1, &rdfd, NULL, NULL, NULL)) {
394 case 0:
395 abort();
396 case -1:
397 return 1;
398 default:
399 break;
401 for (tsp = wp->scrq.cqh_first;
402 tsp != (void *)&wp->scrq; tsp = tsp->q.cqe_next)
403 if (F_ISSET(sp, SC_SCRIPT) &&
404 FD_ISSET(sp->script->sh_master, &rdfd)) {
405 if (sscr_input(sp))
406 return 1;
407 goto loop;
409 return 0;
413 * sscr_input --
414 * Read any waiting shell input.
416 * PUBLIC: int sscr_input __P((SCR *));
419 sscr_input(sp)
420 SCR *sp;
422 GS *gp;
423 WIN *wp;
424 struct timeval poll;
425 fd_set rdfd;
426 int maxfd;
428 gp = sp->gp;
429 wp = sp->wp;
431 loop: maxfd = 0;
432 FD_ZERO(&rdfd);
433 poll.tv_sec = 0;
434 poll.tv_usec = 0;
436 /* Set up the input mask. */
437 for (sp = wp->scrq.cqh_first; sp != (void *)&wp->scrq;
438 sp = sp->q.cqe_next)
439 if (F_ISSET(sp, SC_SCRIPT)) {
440 FD_SET(sp->script->sh_master, &rdfd);
441 if (sp->script->sh_master > maxfd)
442 maxfd = sp->script->sh_master;
445 /* Check for input. */
446 switch (select(maxfd + 1, &rdfd, NULL, NULL, &poll)) {
447 case -1:
448 msgq(sp, M_SYSERR, "select");
449 return (1);
450 case 0:
451 return (0);
452 default:
453 break;
456 /* Read the input. */
457 for (sp = wp->scrq.cqh_first; sp != (void *)&wp->scrq;
458 sp = sp->q.cqe_next)
459 if (F_ISSET(sp, SC_SCRIPT) &&
460 FD_ISSET(sp->script->sh_master, &rdfd) &&
461 sscr_insert(sp))
462 return (1);
463 goto loop;
467 * sscr_insert --
468 * Take a line from the shell and insert it into the file.
470 static int
471 sscr_insert(sp)
472 SCR *sp;
474 struct timeval tv;
475 CHAR_T *endp, *p, *t;
476 SCRIPT *sc;
477 fd_set rdfd;
478 db_recno_t lno;
479 size_t blen, len, tlen;
480 u_int value;
481 int nr, rval;
482 CHAR_T *bp;
484 /* Find out where the end of the file is. */
485 if (db_last(sp, &lno))
486 return (1);
488 #define MINREAD 1024
489 GET_SPACE_RETW(sp, bp, blen, MINREAD);
490 endp = bp;
492 /* Read the characters. */
493 rval = 1;
494 sc = sp->script;
495 more: switch (nr = read(sc->sh_master, endp, MINREAD)) {
496 case 0: /* EOF; shell just exited. */
497 sscr_end(sp);
498 rval = 0;
499 goto ret;
500 case -1: /* Error or interrupt. */
501 msgq(sp, M_SYSERR, "shell");
502 goto ret;
503 default:
504 endp += nr;
505 break;
508 /* Append the lines into the file. */
509 for (p = t = bp; p < endp; ++p) {
510 value = KEY_VAL(sp, *p);
511 if (value == K_CR || value == K_NL) {
512 len = p - t;
513 if (db_append(sp, 1, lno++, t, len))
514 goto ret;
515 t = p + 1;
518 if (p > t) {
519 len = p - t;
521 * If the last thing from the shell isn't another prompt, wait
522 * up to 1/10 of a second for more stuff to show up, so that
523 * we don't break the output into two separate lines. Don't
524 * want to hang indefinitely because some program is hanging,
525 * confused the shell, or whatever.
527 if (!sscr_matchprompt(sp, t, len, &tlen) || tlen != 0) {
528 tv.tv_sec = 0;
529 tv.tv_usec = 100000;
530 FD_ZERO(&rdfd);
531 FD_SET(sc->sh_master, &rdfd);
532 if (select(sc->sh_master + 1,
533 &rdfd, NULL, NULL, &tv) == 1) {
534 MEMMOVE(bp, t, len);
535 endp = bp + len;
536 goto more;
539 if (sscr_setprompt(sp, t, len))
540 return (1);
541 if (db_append(sp, 1, lno++, t, len))
542 goto ret;
545 /* The cursor moves to EOF. */
546 sp->lno = lno;
547 sp->cno = len ? len - 1 : 0;
548 rval = vs_refresh(sp, 1);
550 ret: FREE_SPACEW(sp, bp, blen);
551 return (rval);
555 * sscr_setprompt --
557 * Set the prompt to the last line we got from the shell.
560 static int
561 sscr_setprompt(sp, buf, len)
562 SCR *sp;
563 CHAR_T *buf;
564 size_t len;
566 SCRIPT *sc;
567 char *np;
568 size_t nlen;
570 sc = sp->script;
571 if (sc->sh_prompt)
572 free(sc->sh_prompt);
573 MALLOC(sp, sc->sh_prompt, char *, len + 1);
574 if (sc->sh_prompt == NULL) {
575 sscr_end(sp);
576 return (1);
578 INT2CHAR(sp, buf, len, np, nlen);
579 memmove(sc->sh_prompt, np, nlen);
580 sc->sh_prompt_len = len;
581 sc->sh_prompt[len] = '\0';
582 return (0);
586 * sscr_matchprompt --
587 * Check to see if a line matches the prompt. Nul's indicate
588 * parts that can change, in both content and size.
590 static int
591 sscr_matchprompt(sp, lp, line_len, lenp)
592 SCR *sp;
593 CHAR_T *lp;
594 size_t line_len, *lenp;
596 SCRIPT *sc;
597 size_t prompt_len;
598 char *pp;
600 sc = sp->script;
601 if (line_len < (prompt_len = sc->sh_prompt_len))
602 return (0);
604 for (pp = sc->sh_prompt;
605 prompt_len && line_len; --prompt_len, --line_len) {
606 if (*pp == '\0') {
607 for (; prompt_len && *pp == '\0'; --prompt_len, ++pp);
608 if (!prompt_len)
609 return (0);
610 for (; line_len && *lp != *pp; --line_len, ++lp);
611 if (!line_len)
612 return (0);
614 if (*pp++ != *lp++)
615 break;
618 if (prompt_len)
619 return (0);
620 if (lenp != NULL)
621 *lenp = line_len;
622 return (1);
626 * sscr_end --
627 * End the pipe to a shell.
629 * PUBLIC: int sscr_end __P((SCR *));
632 sscr_end(sp)
633 SCR *sp;
635 SCRIPT *sc;
637 if ((sc = sp->script) == NULL)
638 return (0);
640 /* Turn off the script flags. */
641 F_CLR(sp, SC_SCRIPT);
642 sscr_check(sp);
644 /* Close down the parent's file descriptors. */
645 if (sc->sh_master != -1)
646 (void)close(sc->sh_master);
647 if (sc->sh_slave != -1)
648 (void)close(sc->sh_slave);
650 /* This should have killed the child. */
651 (void)proc_wait(sp, (long)sc->sh_pid, "script-shell", 0, 0);
653 /* Free memory. */
654 free(sc->sh_prompt);
655 free(sc);
656 sp->script = NULL;
658 return (0);
662 * sscr_check --
663 * Set/clear the global scripting bit.
665 static void
666 sscr_check(sp)
667 SCR *sp;
669 GS *gp;
670 WIN *wp;
672 gp = sp->gp;
673 wp = sp->wp;
674 for (sp = wp->scrq.cqh_first; sp != (void *)&wp->scrq;
675 sp = sp->q.cqe_next)
676 if (F_ISSET(sp, SC_SCRIPT)) {
677 F_SET(gp, G_SCRWIN);
678 return;
680 F_CLR(gp, G_SCRWIN);
683 #ifdef HAVE_SYS5_PTY
684 static int ptys_open __P((int, char *));
685 static int ptym_open __P((char *));
687 static int
688 sscr_pty(amaster, aslave, name, termp, winp)
689 int *amaster, *aslave;
690 char *name;
691 struct termios *termp;
692 void *winp;
694 int master, slave, ttygid;
696 /* open master terminal */
697 if ((master = ptym_open(name)) < 0) {
698 errno = ENOENT; /* out of ptys */
699 return (-1);
702 /* open slave terminal */
703 if ((slave = ptys_open(master, name)) >= 0) {
704 *amaster = master;
705 *aslave = slave;
706 } else {
707 errno = ENOENT; /* out of ptys */
708 return (-1);
711 if (termp)
712 (void) tcsetattr(slave, TCSAFLUSH, termp);
713 #ifdef TIOCSWINSZ
714 if (winp != NULL)
715 (void) ioctl(slave, TIOCSWINSZ, (struct winsize *)winp);
716 #endif
717 return (0);
721 * ptym_open --
722 * This function opens a master pty and returns the file descriptor
723 * to it. pts_name is also returned which is the name of the slave.
725 static int
726 ptym_open(pts_name)
727 char *pts_name;
729 int fdm;
730 char *ptr, *ptsname();
732 strcpy(pts_name, _PATH_SYSV_PTY);
733 if ((fdm = open(pts_name, O_RDWR)) < 0 )
734 return (-1);
736 if (grantpt(fdm) < 0) {
737 close(fdm);
738 return (-2);
741 if (unlockpt(fdm) < 0) {
742 close(fdm);
743 return (-3);
746 if (unlockpt(fdm) < 0) {
747 close(fdm);
748 return (-3);
751 /* get slave's name */
752 if ((ptr = ptsname(fdm)) == NULL) {
753 close(fdm);
754 return (-3);
756 strcpy(pts_name, ptr);
757 return (fdm);
761 * ptys_open --
762 * This function opens the slave pty.
764 static int
765 ptys_open(fdm, pts_name)
766 int fdm;
767 char *pts_name;
769 int fds;
771 if ((fds = open(pts_name, O_RDWR)) < 0) {
772 close(fdm);
773 return (-5);
776 if (ioctl(fds, I_PUSH, "ptem") < 0) {
777 close(fds);
778 close(fdm);
779 return (-6);
782 if (ioctl(fds, I_PUSH, "ldterm") < 0) {
783 close(fds);
784 close(fdm);
785 return (-7);
788 if (ioctl(fds, I_PUSH, "ttcompat") < 0) {
789 close(fds);
790 close(fdm);
791 return (-8);
794 return (fds);
797 #else /* !HAVE_SYS5_PTY */
799 static int
800 sscr_pty(amaster, aslave, name, termp, winp)
801 int *amaster, *aslave;
802 char *name;
803 struct termios *termp;
804 void *winp;
806 static char line[] = "/dev/ptyXX";
807 register char *cp1, *cp2;
808 register int master, slave, ttygid;
809 struct group *gr;
811 if ((gr = getgrnam("tty")) != NULL)
812 ttygid = gr->gr_gid;
813 else
814 ttygid = -1;
816 for (cp1 = "pqrs"; *cp1; cp1++) {
817 line[8] = *cp1;
818 for (cp2 = "0123456789abcdef"; *cp2; cp2++) {
819 line[5] = 'p';
820 line[9] = *cp2;
821 if ((master = open(line, O_RDWR, 0)) == -1) {
822 if (errno == ENOENT)
823 return (-1); /* out of ptys */
824 } else {
825 line[5] = 't';
826 (void) chown(line, getuid(), ttygid);
827 (void) chmod(line, S_IRUSR|S_IWUSR|S_IWGRP);
828 #ifdef HAVE_REVOKE
829 (void) revoke(line);
830 #endif
831 if ((slave = open(line, O_RDWR, 0)) != -1) {
832 *amaster = master;
833 *aslave = slave;
834 if (name)
835 strcpy(name, line);
836 if (termp)
837 (void) tcsetattr(slave,
838 TCSAFLUSH, termp);
839 #ifdef TIOCSWINSZ
840 if (winp)
841 (void) ioctl(slave, TIOCSWINSZ,
842 (char *)winp);
843 #endif
844 return (0);
846 (void) close(master);
850 errno = ENOENT; /* out of ptys */
851 return (-1);
853 #endif /* HAVE_SYS5_PTY */