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
10 * See the LICENSE file for redistribution information.
16 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 #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>
27 #include <sys/stropts.h>
32 #include <bitstring.h>
35 #include <stdio.h> /* XXX: OSF/1 bug: include before <grp.h> */
43 #include "../common/common.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 *));
63 ex_script(SCR
*sp
, EXCMD
*cmdp
)
65 /* Vi only command. */
66 if (!F_ISSET(sp
, SC_VI
)) {
68 "150|The script command is only available in vi mode");
72 /* Switch to the new file. */
73 if (cmdp
->argc
!= 0 && ex_edit(sp
, cmdp
))
76 /* Create the shell, figure out the prompt. */
85 * Create a pty setup for a shell.
93 /* We're going to need a shell. */
94 if (opts_empty(sp
, O_SHELL
, 0))
97 MALLOC_RET(sp
, sc
, SCRIPT
*, sizeof(SCRIPT
));
100 sc
->sh_prompt_len
= 0;
103 * There are two different processes running through this code.
104 * They are the shell and the parent.
106 sc
->sh_master
= sc
->sh_slave
= -1;
108 if (tcgetattr(STDIN_FILENO
, &sc
->sh_term
) == -1) {
109 msgq(sp
, M_SYSERR
, "tcgetattr");
114 * Turn off output postprocessing and echo.
116 sc
->sh_term
.c_oflag
&= ~OPOST
;
117 sc
->sh_term
.c_cflag
&= ~(ECHO
|ECHOE
|ECHONL
|ECHOK
);
120 if (ioctl(STDIN_FILENO
, TIOCGWINSZ
, &sc
->sh_win
) == -1) {
121 msgq(sp
, M_SYSERR
, "tcgetattr");
125 if (sscr_pty(&sc
->sh_master
,
126 &sc
->sh_slave
, sc
->sh_name
, &sc
->sh_term
, &sc
->sh_win
) == -1) {
127 msgq(sp
, M_SYSERR
, "pty");
131 if (sscr_pty(&sc
->sh_master
,
132 &sc
->sh_slave
, sc
->sh_name
, &sc
->sh_term
, NULL
) == -1) {
133 msgq(sp
, M_SYSERR
, "pty");
140 * Don't use vfork() here, because the signal semantics differ from
141 * implementation to implementation.
143 switch (sc
->sh_pid
= fork()) {
144 case -1: /* Error. */
145 msgq(sp
, M_SYSERR
, "fork");
146 err
: if (sc
->sh_master
!= -1)
147 (void)close(sc
->sh_master
);
148 if (sc
->sh_slave
!= -1)
149 (void)close(sc
->sh_slave
);
151 case 0: /* Utility. */
154 * So that shells that do command line editing turn it off.
156 (void)setenv("TERM", "emacs", 1);
157 (void)setenv("TERMCAP", "emacs:", 1);
158 (void)setenv("EMACS", "t", 1);
163 * 4.4BSD allocates a controlling terminal using the TIOCSCTTY
164 * ioctl, not by opening a terminal device file. POSIX 1003.1
165 * doesn't define a portable way to do this. If TIOCSCTTY is
166 * not available, hope that the open does it.
168 (void)ioctl(sc
->sh_slave
, TIOCSCTTY
, 0);
170 (void)close(sc
->sh_master
);
171 (void)dup2(sc
->sh_slave
, STDIN_FILENO
);
172 (void)dup2(sc
->sh_slave
, STDOUT_FILENO
);
173 (void)dup2(sc
->sh_slave
, STDERR_FILENO
);
174 (void)close(sc
->sh_slave
);
176 /* Assumes that all shells have -i. */
177 sh_path
= O_STR(sp
, O_SHELL
);
178 if ((sh
= strrchr(sh_path
, '/')) == NULL
)
182 execl(sh_path
, sh
, "-i", NULL
);
183 msgq_str(sp
, M_SYSERR
, sh_path
, "execl: %s");
185 default: /* Parent. */
189 if (sscr_getprompt(sp
))
192 F_SET(sp
, SC_SCRIPT
);
193 F_SET(sp
->gp
, G_SCRWIN
);
199 * Eat lines printed by the shell until a line with no trailing
200 * carriage return comes; set the prompt from that line.
203 sscr_getprompt(SCR
*sp
)
206 CHAR_T
*endp
, *p
, *t
, buf
[1024];
218 /* Wait up to a second for characters to read. */
222 FD_SET(sc
->sh_master
, &fdset
);
223 switch (select(sc
->sh_master
+ 1, &fdset
, NULL
, NULL
, &tv
)) {
224 case -1: /* Error or interrupt. */
225 msgq(sp
, M_SYSERR
, "select");
227 case 0: /* Timeout */
228 msgq(sp
, M_ERR
, "Error: timed out");
230 case 1: /* Characters to read. */
234 /* Read the characters. */
235 more
: len
= sizeof(buf
) - (endp
- buf
);
236 switch (nr
= read(sc
->sh_master
, endp
, len
)) {
238 msgq(sp
, M_ERR
, "Error: shell: EOF");
240 case -1: /* Error or interrupt. */
241 msgq(sp
, M_SYSERR
, "shell");
248 /* If any complete lines, push them into the file. */
249 for (p
= t
= buf
; p
< endp
; ++p
) {
250 value
= KEY_VAL(sp
, *p
);
251 if (value
== K_CR
|| value
== K_NL
) {
252 if (db_last(sp
, &lline
) ||
253 db_append(sp
, 0, lline
, t
, p
- t
))
259 MEMMOVE(buf
, t
, endp
- t
);
260 endp
= buf
+ (endp
- t
);
265 /* Wait up 1/10 of a second to make sure that we got it all. */
268 switch (select(sc
->sh_master
+ 1, &fdset
, NULL
, NULL
, &tv
)) {
269 case -1: /* Error or interrupt. */
270 msgq(sp
, M_SYSERR
, "select");
272 case 0: /* Timeout */
274 case 1: /* Characters to read. */
278 /* Timed out, so theoretically we have a prompt. */
282 /* Append the line into the file. */
283 if (db_last(sp
, &lline
) || db_append(sp
, 0, lline
, buf
, llen
)) {
284 prompterr
: sscr_end(sp
);
288 return (sscr_setprompt(sp
, buf
, llen
));
293 * Take a line and hand it off to the shell.
295 * PUBLIC: int sscr_exec __P((SCR *, db_recno_t));
298 sscr_exec(SCR
*sp
, db_recno_t lno
)
302 size_t blen
, len
, last_len
, tlen
;
303 int isempty
, matchprompt
, nw
, rval
;
307 /* If there's a prompt on the last line, append the command. */
308 if (db_last(sp
, &last_lno
))
310 if (db_get(sp
, last_lno
, DBG_FATAL
, &p
, &last_len
))
312 if (sscr_matchprompt(sp
, p
, last_len
, &tlen
) && tlen
== 0) {
314 GET_SPACE_RETW(sp
, bp
, blen
, last_len
+ 128);
315 MEMMOVEW(bp
, p
, last_len
);
319 /* Get something to execute. */
320 if (db_eget(sp
, lno
, &p
, &len
, &isempty
)) {
326 /* Empty lines aren't interesting. */
330 /* Delete any prompt. */
331 if (sscr_matchprompt(sp
, p
, len
, &tlen
)) {
333 empty
: msgq(sp
, M_BERR
, "151|No command to execute");
340 /* Push the line to the shell. */
342 if ((nw
= write(sc
->sh_master
, p
, len
)) != len
)
345 if (write(sc
->sh_master
, "\n", 1) != 1) {
348 msgq(sp
, M_SYSERR
, "shell");
353 ADD_SPACE_RETW(sp
, bp
, blen
, last_len
+ len
);
354 MEMMOVEW(bp
+ last_len
, p
, len
);
355 if (db_set(sp
, last_lno
, bp
, last_len
+ len
))
359 FREE_SPACEW(sp
, bp
, blen
);
365 * Check whether any input from shell or passed set.
367 * PUBLIC: int sscr_check_input __P((SCR *sp, fd_set *rdfd, int maxfd));
370 sscr_check_input(SCR
*sp
, fd_set
*fdset
, int maxfd
)
378 loop
: memcpy(&rdfd
, fdset
, sizeof(fd_set
));
380 for (tsp
= wp
->scrq
.cqh_first
;
381 tsp
!= (void *)&wp
->scrq
; tsp
= tsp
->q
.cqe_next
)
382 if (F_ISSET(sp
, SC_SCRIPT
)) {
383 FD_SET(sp
->script
->sh_master
, &rdfd
);
384 if (sp
->script
->sh_master
> maxfd
)
385 maxfd
= sp
->script
->sh_master
;
387 switch (select(maxfd
+ 1, &rdfd
, NULL
, NULL
, NULL
)) {
395 for (tsp
= wp
->scrq
.cqh_first
;
396 tsp
!= (void *)&wp
->scrq
; tsp
= tsp
->q
.cqe_next
)
397 if (F_ISSET(sp
, SC_SCRIPT
) &&
398 FD_ISSET(sp
->script
->sh_master
, &rdfd
)) {
408 * Read any waiting shell input.
410 * PUBLIC: int sscr_input __P((SCR *));
429 /* Set up the input mask. */
430 for (sp
= wp
->scrq
.cqh_first
; sp
!= (void *)&wp
->scrq
;
432 if (F_ISSET(sp
, SC_SCRIPT
)) {
433 FD_SET(sp
->script
->sh_master
, &rdfd
);
434 if (sp
->script
->sh_master
> maxfd
)
435 maxfd
= sp
->script
->sh_master
;
438 /* Check for input. */
439 switch (select(maxfd
+ 1, &rdfd
, NULL
, NULL
, &poll
)) {
441 msgq(sp
, M_SYSERR
, "select");
449 /* Read the input. */
450 for (sp
= wp
->scrq
.cqh_first
; sp
!= (void *)&wp
->scrq
;
452 if (F_ISSET(sp
, SC_SCRIPT
) &&
453 FD_ISSET(sp
->script
->sh_master
, &rdfd
) &&
461 * Take a line from the shell and insert it into the file.
467 CHAR_T
*endp
, *p
, *t
;
471 size_t blen
, len
, tlen
;
476 /* Find out where the end of the file is. */
477 if (db_last(sp
, &lno
))
481 GET_SPACE_RETW(sp
, bp
, blen
, MINREAD
);
484 /* Read the characters. */
487 more
: switch (nr
= read(sc
->sh_master
, endp
, MINREAD
)) {
488 case 0: /* EOF; shell just exited. */
492 case -1: /* Error or interrupt. */
493 msgq(sp
, M_SYSERR
, "shell");
500 /* Append the lines into the file. */
501 for (p
= t
= bp
; p
< endp
; ++p
) {
502 value
= KEY_VAL(sp
, *p
);
503 if (value
== K_CR
|| value
== K_NL
) {
505 if (db_append(sp
, 1, lno
++, t
, len
))
513 * If the last thing from the shell isn't another prompt, wait
514 * up to 1/10 of a second for more stuff to show up, so that
515 * we don't break the output into two separate lines. Don't
516 * want to hang indefinitely because some program is hanging,
517 * confused the shell, or whatever.
519 if (!sscr_matchprompt(sp
, t
, len
, &tlen
) || tlen
!= 0) {
523 FD_SET(sc
->sh_master
, &rdfd
);
524 if (select(sc
->sh_master
+ 1,
525 &rdfd
, NULL
, NULL
, &tv
) == 1) {
531 if (sscr_setprompt(sp
, t
, len
))
533 if (db_append(sp
, 1, lno
++, t
, len
))
537 /* The cursor moves to EOF. */
539 sp
->cno
= len
? len
- 1 : 0;
540 rval
= vs_refresh(sp
, 1);
542 ret
: FREE_SPACEW(sp
, bp
, blen
);
549 * Set the prompt to the last line we got from the shell.
553 sscr_setprompt(SCR
*sp
, CHAR_T
*buf
, size_t len
)
562 MALLOC(sp
, sc
->sh_prompt
, char *, len
+ 1);
563 if (sc
->sh_prompt
== NULL
) {
567 INT2CHAR(sp
, buf
, len
, np
, nlen
);
568 memmove(sc
->sh_prompt
, np
, nlen
);
569 sc
->sh_prompt_len
= len
;
570 sc
->sh_prompt
[len
] = '\0';
575 * sscr_matchprompt --
576 * Check to see if a line matches the prompt. Nul's indicate
577 * parts that can change, in both content and size.
580 sscr_matchprompt(SCR
*sp
, CHAR_T
*lp
, size_t line_len
, size_t *lenp
)
587 if (line_len
< (prompt_len
= sc
->sh_prompt_len
))
590 for (pp
= sc
->sh_prompt
;
591 prompt_len
&& line_len
; --prompt_len
, --line_len
) {
593 for (; prompt_len
&& *pp
== '\0'; --prompt_len
, ++pp
);
596 for (; line_len
&& *lp
!= *pp
; --line_len
, ++lp
);
613 * End the pipe to a shell.
615 * PUBLIC: int sscr_end __P((SCR *));
622 if ((sc
= sp
->script
) == NULL
)
625 /* Turn off the script flags. */
626 F_CLR(sp
, SC_SCRIPT
);
629 /* Close down the parent's file descriptors. */
630 if (sc
->sh_master
!= -1)
631 (void)close(sc
->sh_master
);
632 if (sc
->sh_slave
!= -1)
633 (void)close(sc
->sh_slave
);
635 /* This should have killed the child. */
636 (void)proc_wait(sp
, (long)sc
->sh_pid
, "script-shell", 0, 0);
648 * Set/clear the global scripting bit.
658 for (sp
= wp
->scrq
.cqh_first
; sp
!= (void *)&wp
->scrq
;
660 if (F_ISSET(sp
, SC_SCRIPT
)) {
668 static int ptys_open
__P((int, char *));
669 static int ptym_open
__P((char *));
672 sscr_pty(int *amaster
, int *aslave
, char *name
, struct termios
*termp
, void *winp
)
674 int master
, slave
, ttygid
;
676 /* open master terminal */
677 if ((master
= ptym_open(name
)) < 0) {
678 errno
= ENOENT
; /* out of ptys */
682 /* open slave terminal */
683 if ((slave
= ptys_open(master
, name
)) >= 0) {
687 errno
= ENOENT
; /* out of ptys */
692 (void) tcsetattr(slave
, TCSAFLUSH
, termp
);
695 (void) ioctl(slave
, TIOCSWINSZ
, (struct winsize
*)winp
);
702 * This function opens a master pty and returns the file descriptor
703 * to it. pts_name is also returned which is the name of the slave.
706 ptym_open(char *pts_name
)
709 char *ptr
, *ptsname();
711 strcpy(pts_name
, _PATH_SYSV_PTY
);
712 if ((fdm
= open(pts_name
, O_RDWR
)) < 0 )
715 if (grantpt(fdm
) < 0) {
720 if (unlockpt(fdm
) < 0) {
725 if (unlockpt(fdm
) < 0) {
730 /* get slave's name */
731 if ((ptr
= ptsname(fdm
)) == NULL
) {
735 strcpy(pts_name
, ptr
);
741 * This function opens the slave pty.
744 ptys_open(int fdm
, char *pts_name
)
748 if ((fds
= open(pts_name
, O_RDWR
)) < 0) {
753 if (ioctl(fds
, I_PUSH
, "ptem") < 0) {
759 if (ioctl(fds
, I_PUSH
, "ldterm") < 0) {
765 if (ioctl(fds
, I_PUSH
, "ttcompat") < 0) {
774 #else /* !HAVE_SYS5_PTY */
777 sscr_pty(amaster
, aslave
, name
, termp
, winp
)
778 int *amaster
, *aslave
;
780 struct termios
*termp
;
783 static char line
[] = "/dev/ptyXX";
784 register char *cp1
, *cp2
;
785 register int master
, slave
, ttygid
;
788 if ((gr
= getgrnam("tty")) != NULL
)
793 for (cp1
= "pqrs"; *cp1
; cp1
++) {
795 for (cp2
= "0123456789abcdef"; *cp2
; cp2
++) {
798 if ((master
= open(line
, O_RDWR
, 0)) == -1) {
800 return (-1); /* out of ptys */
803 (void) chown(line
, getuid(), ttygid
);
804 (void) chmod(line
, S_IRUSR
|S_IWUSR
|S_IWGRP
);
808 if ((slave
= open(line
, O_RDWR
, 0)) != -1) {
814 (void) tcsetattr(slave
,
818 (void) ioctl(slave
, TIOCSWINSZ
,
823 (void) close(master
);
827 errno
= ENOENT
; /* out of ptys */
830 #endif /* HAVE_SYS5_PTY */