Sync usage with man page.
[netbsd-mini2440.git] / dist / nvi / ex / ex_script.c
blobb7e036c389e882fe6d2159362c1e3deef1a7a41c
1 /* $NetBSD: ex_script.c,v 1.3 2008/12/05 22:51:42 christos Exp $ */
3 /*-
4 * Copyright (c) 1992, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 * Copyright (c) 1992, 1993, 1994, 1995, 1996
7 * Keith Bostic. All rights reserved.
9 * This code is derived from software contributed to Berkeley by
10 * Brian Hirt.
12 * See the LICENSE file for redistribution information.
15 #include "config.h"
17 #ifndef lint
18 static const char sccsid[] = "Id: ex_script.c,v 10.38 2001/06/25 15:19:19 skimo Exp (Berkeley) Date: 2001/06/25 15:19:19";
19 #endif /* not lint */
21 #include <sys/types.h>
22 #include <sys/ioctl.h>
23 #include <sys/queue.h>
24 #ifdef HAVE_SYS_SELECT_H
25 #include <sys/select.h>
26 #endif
27 #include <sys/stat.h>
28 #if defined(HAVE_SYS5_PTY) && !defined(__NetBSD__)
29 #include <sys/stropts.h>
30 #endif
31 #include <sys/time.h>
32 #include <sys/wait.h>
34 #include <bitstring.h>
35 #include <errno.h>
36 #include <fcntl.h>
37 #include <stdio.h> /* XXX: OSF/1 bug: include before <grp.h> */
38 #include <grp.h>
39 #include <limits.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <termios.h>
43 #include <unistd.h>
45 #include "../common/common.h"
46 #include "../vi/vi.h"
47 #include "script.h"
48 #include "pathnames.h"
50 static void sscr_check __P((SCR *));
51 static int sscr_getprompt __P((SCR *));
52 static int sscr_init __P((SCR *));
53 static int sscr_insert __P((SCR *));
54 static int sscr_matchprompt __P((SCR *, CHAR_T *, size_t, size_t *));
55 static int sscr_pty __P((int *, int *, char *, struct termios *, void *));
56 static int sscr_setprompt __P((SCR *, CHAR_T *, size_t));
59 * ex_script -- : sc[ript][!] [file]
60 * Switch to script mode.
62 * PUBLIC: int ex_script __P((SCR *, EXCMD *));
64 int
65 ex_script(SCR *sp, 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(SCR *sp)
92 SCRIPT *sc;
93 const char *sh, *sh_path;
95 /* We're going to need a shell. */
96 if (opts_empty(sp, O_SHELL, 0))
97 return (1);
99 MALLOC_RET(sp, sc, SCRIPT *, sizeof(SCRIPT));
100 sp->script = sc;
101 sc->sh_prompt = NULL;
102 sc->sh_prompt_len = 0;
105 * There are two different processes running through this code.
106 * They are the shell and the parent.
108 sc->sh_master = sc->sh_slave = -1;
110 if (tcgetattr(STDIN_FILENO, &sc->sh_term) == -1) {
111 msgq(sp, M_SYSERR, "tcgetattr");
112 goto err;
116 * Turn off output postprocessing and echo.
118 sc->sh_term.c_oflag &= ~OPOST;
119 sc->sh_term.c_cflag &= ~(ECHO|ECHOE|ECHONL|ECHOK);
121 #ifdef TIOCGWINSZ
122 if (ioctl(STDIN_FILENO, TIOCGWINSZ, &sc->sh_win) == -1) {
123 msgq(sp, M_SYSERR, "tcgetattr");
124 goto err;
127 if (sscr_pty(&sc->sh_master,
128 &sc->sh_slave, sc->sh_name, &sc->sh_term, &sc->sh_win) == -1) {
129 msgq(sp, M_SYSERR, "pty");
130 goto err;
132 #else
133 if (sscr_pty(&sc->sh_master,
134 &sc->sh_slave, sc->sh_name, &sc->sh_term, NULL) == -1) {
135 msgq(sp, M_SYSERR, "pty");
136 goto err;
138 #endif
141 * __TK__ huh?
142 * Don't use vfork() here, because the signal semantics differ from
143 * implementation to implementation.
145 switch (sc->sh_pid = fork()) {
146 case -1: /* Error. */
147 msgq(sp, M_SYSERR, "fork");
148 err: if (sc->sh_master != -1)
149 (void)close(sc->sh_master);
150 if (sc->sh_slave != -1)
151 (void)close(sc->sh_slave);
152 return (1);
153 case 0: /* Utility. */
155 * XXX
156 * So that shells that do command line editing turn it off.
158 (void)setenv("TERM", "emacs", 1);
159 (void)setenv("TERMCAP", "emacs:", 1);
160 (void)setenv("EMACS", "t", 1);
162 (void)setsid();
163 #ifdef TIOCSCTTY
165 * 4.4BSD allocates a controlling terminal using the TIOCSCTTY
166 * ioctl, not by opening a terminal device file. POSIX 1003.1
167 * doesn't define a portable way to do this. If TIOCSCTTY is
168 * not available, hope that the open does it.
170 (void)ioctl(sc->sh_slave, TIOCSCTTY, 0);
171 #endif
172 (void)close(sc->sh_master);
173 (void)dup2(sc->sh_slave, STDIN_FILENO);
174 (void)dup2(sc->sh_slave, STDOUT_FILENO);
175 (void)dup2(sc->sh_slave, STDERR_FILENO);
176 (void)close(sc->sh_slave);
178 /* Assumes that all shells have -i. */
179 sh_path = O_STR(sp, O_SHELL);
180 if ((sh = strrchr(sh_path, '/')) == NULL)
181 sh = sh_path;
182 else
183 ++sh;
184 execl(sh_path, sh, "-i", NULL);
185 msgq_str(sp, M_SYSERR, sh_path, "execl: %s");
186 _exit(127);
187 default: /* Parent. */
188 break;
191 if (sscr_getprompt(sp))
192 return (1);
194 F_SET(sp, SC_SCRIPT);
195 F_SET(sp->gp, G_SCRWIN);
196 return (0);
200 * sscr_getprompt --
201 * Eat lines printed by the shell until a line with no trailing
202 * carriage return comes; set the prompt from that line.
204 static int
205 sscr_getprompt(SCR *sp)
207 struct timeval tv;
208 CHAR_T *endp, *p, *t, buf[1024];
209 SCRIPT *sc;
210 fd_set fdset;
211 db_recno_t lline;
212 size_t llen, len;
213 u_int value;
214 int nr;
216 FD_ZERO(&fdset);
217 endp = buf;
218 len = sizeof(buf);
220 /* Wait up to a second for characters to read. */
221 tv.tv_sec = 5;
222 tv.tv_usec = 0;
223 sc = sp->script;
224 FD_SET(sc->sh_master, &fdset);
225 switch (select(sc->sh_master + 1, &fdset, NULL, NULL, &tv)) {
226 case -1: /* Error or interrupt. */
227 msgq(sp, M_SYSERR, "select");
228 goto prompterr;
229 case 0: /* Timeout */
230 msgq(sp, M_ERR, "Error: timed out");
231 goto prompterr;
232 case 1: /* Characters to read. */
233 break;
236 /* Read the characters. */
237 more: len = sizeof(buf) - (endp - buf);
238 switch (nr = read(sc->sh_master, endp, len)) {
239 case 0: /* EOF. */
240 msgq(sp, M_ERR, "Error: shell: EOF");
241 goto prompterr;
242 case -1: /* Error or interrupt. */
243 msgq(sp, M_SYSERR, "shell");
244 goto prompterr;
245 default:
246 endp += nr;
247 break;
250 /* If any complete lines, push them into the file. */
251 for (p = t = buf; p < endp; ++p) {
252 value = KEY_VAL(sp, *p);
253 if (value == K_CR || value == K_NL) {
254 if (db_last(sp, &lline) ||
255 db_append(sp, 0, lline, t, p - t))
256 goto prompterr;
257 t = p + 1;
260 if (p > buf) {
261 MEMMOVE(buf, t, endp - t);
262 endp = buf + (endp - t);
264 if (endp == buf)
265 goto more;
267 /* Wait up 1/10 of a second to make sure that we got it all. */
268 tv.tv_sec = 0;
269 tv.tv_usec = 100000;
270 switch (select(sc->sh_master + 1, &fdset, NULL, NULL, &tv)) {
271 case -1: /* Error or interrupt. */
272 msgq(sp, M_SYSERR, "select");
273 goto prompterr;
274 case 0: /* Timeout */
275 break;
276 case 1: /* Characters to read. */
277 goto more;
280 /* Timed out, so theoretically we have a prompt. */
281 llen = endp - buf;
282 endp = buf;
284 /* Append the line into the file. */
285 if (db_last(sp, &lline) || db_append(sp, 0, lline, buf, llen)) {
286 prompterr: sscr_end(sp);
287 return (1);
290 return (sscr_setprompt(sp, buf, llen));
294 * sscr_exec --
295 * Take a line and hand it off to the shell.
297 * PUBLIC: int sscr_exec __P((SCR *, db_recno_t));
300 sscr_exec(SCR *sp, db_recno_t lno)
302 SCRIPT *sc;
303 db_recno_t last_lno;
304 size_t blen, len, last_len, tlen;
305 int isempty, matchprompt, rval;
306 ssize_t nw;
307 CHAR_T *bp = NULL;
308 CHAR_T *p;
310 /* If there's a prompt on the last line, append the command. */
311 if (db_last(sp, &last_lno))
312 return (1);
313 if (db_get(sp, last_lno, DBG_FATAL, &p, &last_len))
314 return (1);
315 if (sscr_matchprompt(sp, p, last_len, &tlen) && tlen == 0) {
316 matchprompt = 1;
317 GET_SPACE_RETW(sp, bp, blen, last_len + 128);
318 MEMMOVEW(bp, p, last_len);
319 } else
320 matchprompt = 0;
322 /* Get something to execute. */
323 if (db_eget(sp, lno, &p, &len, &isempty)) {
324 if (isempty)
325 goto empty;
326 goto err1;
329 /* Empty lines aren't interesting. */
330 if (len == 0)
331 goto empty;
333 /* Delete any prompt. */
334 if (sscr_matchprompt(sp, p, len, &tlen)) {
335 if (tlen == len) {
336 empty: msgq(sp, M_BERR, "151|No command to execute");
337 goto err1;
339 p += (len - tlen);
340 len = tlen;
343 /* Push the line to the shell. */
344 sc = sp->script;
345 if ((size_t)(nw = write(sc->sh_master, p, len)) != len)
346 goto err2;
347 rval = 0;
348 if (write(sc->sh_master, "\n", 1) != 1) {
349 err2: if (nw == 0)
350 errno = EIO;
351 msgq(sp, M_SYSERR, "shell");
352 goto err1;
355 if (matchprompt) {
356 ADD_SPACE_RETW(sp, bp, blen, last_len + len);
357 MEMMOVEW(bp + last_len, p, len);
358 if (db_set(sp, last_lno, bp, last_len + len))
359 err1: rval = 1;
361 if (matchprompt)
362 FREE_SPACEW(sp, bp, blen);
363 return (rval);
367 * sscr_check_input -
368 * Check whether any input from shell or passed set.
370 * PUBLIC: int sscr_check_input __P((SCR *sp, fd_set *rdfd, int maxfd));
373 sscr_check_input(SCR *sp, fd_set *fdset, int maxfd)
375 fd_set rdfd;
376 SCR *tsp;
377 WIN *wp;
379 wp = sp->wp;
381 loop: memcpy(&rdfd, fdset, sizeof(fd_set));
383 for (tsp = wp->scrq.cqh_first;
384 tsp != (void *)&wp->scrq; tsp = tsp->q.cqe_next)
385 if (F_ISSET(sp, SC_SCRIPT)) {
386 FD_SET(sp->script->sh_master, &rdfd);
387 if (sp->script->sh_master > maxfd)
388 maxfd = sp->script->sh_master;
390 switch (select(maxfd + 1, &rdfd, NULL, NULL, NULL)) {
391 case 0:
392 abort();
393 case -1:
394 return 1;
395 default:
396 break;
398 for (tsp = wp->scrq.cqh_first;
399 tsp != (void *)&wp->scrq; tsp = tsp->q.cqe_next)
400 if (F_ISSET(sp, SC_SCRIPT) &&
401 FD_ISSET(sp->script->sh_master, &rdfd)) {
402 if (sscr_input(sp))
403 return 1;
404 goto loop;
406 return 0;
410 * sscr_input --
411 * Read any waiting shell input.
413 * PUBLIC: int sscr_input __P((SCR *));
416 sscr_input(SCR *sp)
418 GS *gp;
419 WIN *wp;
420 struct timeval poll;
421 fd_set rdfd;
422 int maxfd;
424 gp = sp->gp;
425 wp = sp->wp;
427 loop: maxfd = 0;
428 FD_ZERO(&rdfd);
429 poll.tv_sec = 0;
430 poll.tv_usec = 0;
432 /* Set up the input mask. */
433 for (sp = wp->scrq.cqh_first; sp != (void *)&wp->scrq;
434 sp = sp->q.cqe_next)
435 if (F_ISSET(sp, SC_SCRIPT)) {
436 FD_SET(sp->script->sh_master, &rdfd);
437 if (sp->script->sh_master > maxfd)
438 maxfd = sp->script->sh_master;
441 /* Check for input. */
442 switch (select(maxfd + 1, &rdfd, NULL, NULL, &poll)) {
443 case -1:
444 msgq(sp, M_SYSERR, "select");
445 return (1);
446 case 0:
447 return (0);
448 default:
449 break;
452 /* Read the input. */
453 for (sp = wp->scrq.cqh_first; sp != (void *)&wp->scrq;
454 sp = sp->q.cqe_next)
455 if (F_ISSET(sp, SC_SCRIPT) &&
456 FD_ISSET(sp->script->sh_master, &rdfd) &&
457 sscr_insert(sp))
458 return (1);
459 goto loop;
463 * sscr_insert --
464 * Take a line from the shell and insert it into the file.
466 static int
467 sscr_insert(SCR *sp)
469 struct timeval tv;
470 CHAR_T *endp, *p, *t;
471 SCRIPT *sc;
472 fd_set rdfd;
473 db_recno_t lno;
474 size_t blen, len = 0, tlen;
475 u_int value;
476 int nr, rval;
477 CHAR_T *bp;
479 /* Find out where the end of the file is. */
480 if (db_last(sp, &lno))
481 return (1);
483 #define MINREAD 1024
484 GET_SPACE_RETW(sp, bp, blen, MINREAD);
485 endp = bp;
487 /* Read the characters. */
488 rval = 1;
489 sc = sp->script;
490 more: switch (nr = read(sc->sh_master, endp, MINREAD)) {
491 case 0: /* EOF; shell just exited. */
492 sscr_end(sp);
493 rval = 0;
494 goto ret;
495 case -1: /* Error or interrupt. */
496 msgq(sp, M_SYSERR, "shell");
497 goto ret;
498 default:
499 endp += nr;
500 break;
503 /* Append the lines into the file. */
504 for (p = t = bp; p < endp; ++p) {
505 value = KEY_VAL(sp, *p);
506 if (value == K_CR || value == K_NL) {
507 len = p - t;
508 if (db_append(sp, 1, lno++, t, len))
509 goto ret;
510 t = p + 1;
513 if (p > t) {
514 len = p - t;
516 * If the last thing from the shell isn't another prompt, wait
517 * up to 1/10 of a second for more stuff to show up, so that
518 * we don't break the output into two separate lines. Don't
519 * want to hang indefinitely because some program is hanging,
520 * confused the shell, or whatever.
522 if (!sscr_matchprompt(sp, t, len, &tlen) || tlen != 0) {
523 tv.tv_sec = 0;
524 tv.tv_usec = 100000;
525 FD_ZERO(&rdfd);
526 FD_SET(sc->sh_master, &rdfd);
527 if (select(sc->sh_master + 1,
528 &rdfd, NULL, NULL, &tv) == 1) {
529 MEMMOVE(bp, t, len);
530 endp = bp + len;
531 goto more;
534 if (sscr_setprompt(sp, t, len))
535 return (1);
536 if (db_append(sp, 1, lno++, t, len))
537 goto ret;
540 /* The cursor moves to EOF. */
541 sp->lno = lno;
542 sp->cno = len ? len - 1 : 0;
543 rval = vs_refresh(sp, 1);
545 ret: FREE_SPACEW(sp, bp, blen);
546 return (rval);
550 * sscr_setprompt --
552 * Set the prompt to the last line we got from the shell.
555 static int
556 sscr_setprompt(SCR *sp, CHAR_T *buf, size_t len)
558 SCRIPT *sc;
559 const char *np;
560 size_t nlen;
562 sc = sp->script;
563 if (sc->sh_prompt)
564 free(sc->sh_prompt);
565 MALLOC(sp, sc->sh_prompt, char *, len + 1);
566 if (sc->sh_prompt == NULL) {
567 sscr_end(sp);
568 return (1);
570 INT2CHAR(sp, buf, len, np, nlen);
571 memmove(sc->sh_prompt, np, nlen);
572 sc->sh_prompt_len = len;
573 sc->sh_prompt[len] = '\0';
574 return (0);
578 * sscr_matchprompt --
579 * Check to see if a line matches the prompt. Nul's indicate
580 * parts that can change, in both content and size.
582 static int
583 sscr_matchprompt(SCR *sp, CHAR_T *lp, size_t line_len, size_t *lenp)
585 SCRIPT *sc;
586 size_t prompt_len;
587 char *pp;
589 sc = sp->script;
590 if (line_len < (prompt_len = sc->sh_prompt_len))
591 return (0);
593 for (pp = sc->sh_prompt;
594 prompt_len && line_len; --prompt_len, --line_len) {
595 if (*pp == '\0') {
596 for (; prompt_len && *pp == '\0'; --prompt_len, ++pp);
597 if (!prompt_len)
598 return (0);
599 for (; line_len && *lp != *pp; --line_len, ++lp);
600 if (!line_len)
601 return (0);
603 if (*pp++ != *lp++)
604 break;
607 if (prompt_len)
608 return (0);
609 if (lenp != NULL)
610 *lenp = line_len;
611 return (1);
615 * sscr_end --
616 * End the pipe to a shell.
618 * PUBLIC: int sscr_end __P((SCR *));
621 sscr_end(SCR *sp)
623 SCRIPT *sc;
625 if ((sc = sp->script) == NULL)
626 return (0);
628 /* Turn off the script flags. */
629 F_CLR(sp, SC_SCRIPT);
630 sscr_check(sp);
632 /* Close down the parent's file descriptors. */
633 if (sc->sh_master != -1)
634 (void)close(sc->sh_master);
635 if (sc->sh_slave != -1)
636 (void)close(sc->sh_slave);
638 /* This should have killed the child. */
639 (void)proc_wait(sp, (long)sc->sh_pid, "script-shell", 0, 0);
641 /* Free memory. */
642 free(sc->sh_prompt);
643 free(sc);
644 sp->script = NULL;
646 return (0);
650 * sscr_check --
651 * Set/clear the global scripting bit.
653 static void
654 sscr_check(SCR *sp)
656 GS *gp;
657 WIN *wp;
659 gp = sp->gp;
660 wp = sp->wp;
661 for (sp = wp->scrq.cqh_first; sp != (void *)&wp->scrq;
662 sp = sp->q.cqe_next)
663 if (F_ISSET(sp, SC_SCRIPT)) {
664 F_SET(gp, G_SCRWIN);
665 return;
667 F_CLR(gp, G_SCRWIN);
670 #ifdef HAVE_SYS5_PTY
671 static int ptys_open __P((int, char *));
672 static int ptym_open __P((char *));
674 static int
675 sscr_pty(int *amaster, int *aslave, char *name, struct termios *termp, void *winp)
677 int master, slave;
679 /* open master terminal */
680 if ((master = ptym_open(name)) < 0) {
681 errno = ENOENT; /* out of ptys */
682 return (-1);
685 /* open slave terminal */
686 if ((slave = ptys_open(master, name)) >= 0) {
687 *amaster = master;
688 *aslave = slave;
689 } else {
690 errno = ENOENT; /* out of ptys */
691 return (-1);
694 if (termp)
695 (void) tcsetattr(slave, TCSAFLUSH, termp);
696 #ifdef TIOCSWINSZ
697 if (winp != NULL)
698 (void) ioctl(slave, TIOCSWINSZ, (struct winsize *)winp);
699 #endif
700 return (0);
704 * ptym_open --
705 * This function opens a master pty and returns the file descriptor
706 * to it. pts_name is also returned which is the name of the slave.
708 static int
709 ptym_open(char *pts_name)
711 int fdm;
712 char *ptr;
714 strcpy(pts_name, _PATH_SYSV_PTY);
715 if ((fdm = open(pts_name, O_RDWR)) < 0 )
716 return (-1);
718 if (grantpt(fdm) < 0) {
719 close(fdm);
720 return (-2);
723 if (unlockpt(fdm) < 0) {
724 close(fdm);
725 return (-3);
728 if (unlockpt(fdm) < 0) {
729 close(fdm);
730 return (-3);
733 /* get slave's name */
734 if ((ptr = ptsname(fdm)) == NULL) {
735 close(fdm);
736 return (-3);
738 strcpy(pts_name, ptr);
739 return (fdm);
743 * ptys_open --
744 * This function opens the slave pty.
746 static int
747 ptys_open(int fdm, char *pts_name)
749 int fds;
751 if ((fds = open(pts_name, O_RDWR)) < 0) {
752 close(fdm);
753 return (-5);
756 #ifdef I_PUSH
757 if (ioctl(fds, I_PUSH, "ptem") < 0) {
758 close(fds);
759 close(fdm);
760 return (-6);
763 if (ioctl(fds, I_PUSH, "ldterm") < 0) {
764 close(fds);
765 close(fdm);
766 return (-7);
769 if (ioctl(fds, I_PUSH, "ttcompat") < 0) {
770 close(fds);
771 close(fdm);
772 return (-8);
774 #endif /* I_PUSH */
776 return (fds);
779 #else /* !HAVE_SYS5_PTY */
781 static int
782 sscr_pty(amaster, aslave, name, termp, winp)
783 int *amaster, *aslave;
784 char *name;
785 struct termios *termp;
786 void *winp;
788 static char line[] = "/dev/ptyXX";
789 register char *cp1, *cp2;
790 register int master, slave, ttygid;
791 struct group *gr;
793 if ((gr = getgrnam("tty")) != NULL)
794 ttygid = gr->gr_gid;
795 else
796 ttygid = -1;
798 for (cp1 = "pqrs"; *cp1; cp1++) {
799 line[8] = *cp1;
800 for (cp2 = "0123456789abcdef"; *cp2; cp2++) {
801 line[5] = 'p';
802 line[9] = *cp2;
803 if ((master = open(line, O_RDWR, 0)) == -1) {
804 if (errno == ENOENT)
805 return (-1); /* out of ptys */
806 } else {
807 line[5] = 't';
808 (void) chown(line, getuid(), ttygid);
809 (void) chmod(line, S_IRUSR|S_IWUSR|S_IWGRP);
810 #ifdef HAVE_REVOKE
811 (void) revoke(line);
812 #endif
813 if ((slave = open(line, O_RDWR, 0)) != -1) {
814 *amaster = master;
815 *aslave = slave;
816 if (name)
817 strcpy(name, line);
818 if (termp)
819 (void) tcsetattr(slave,
820 TCSAFLUSH, termp);
821 #ifdef TIOCSWINSZ
822 if (winp)
823 (void) ioctl(slave, TIOCSWINSZ,
824 (char *)winp);
825 #endif
826 return (0);
828 (void) close(master);
832 errno = ENOENT; /* out of ptys */
833 return (-1);
835 #endif /* HAVE_SYS5_PTY */