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.37 2000/07/16 20:49:32 skimo Exp $ (Berkeley) $Date: 2000/07/16 20:49:32 $";
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 *));
67 /* Vi only command. */
68 if (!F_ISSET(sp
, SC_VI
)) {
70 "150|The script command is only available in vi mode");
74 /* Switch to the new file. */
75 if (cmdp
->argc
!= 0 && ex_edit(sp
, cmdp
))
78 /* Create the shell, figure out the prompt. */
87 * Create a pty setup for a shell.
96 /* We're going to need a shell. */
97 if (opts_empty(sp
, O_SHELL
, 0))
100 MALLOC_RET(sp
, sc
, SCRIPT
*, sizeof(SCRIPT
));
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");
117 * Turn off output postprocessing and echo.
119 sc
->sh_term
.c_oflag
&= ~OPOST
;
120 sc
->sh_term
.c_cflag
&= ~(ECHO
|ECHOE
|ECHONL
|ECHOK
);
123 if (ioctl(STDIN_FILENO
, TIOCGWINSZ
, &sc
->sh_win
) == -1) {
124 msgq(sp
, M_SYSERR
, "tcgetattr");
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");
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");
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
);
154 case 0: /* Utility. */
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);
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);
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
)
185 execl(sh_path
, sh
, "-i", NULL
);
186 msgq_str(sp
, M_SYSERR
, sh_path
, "execl: %s");
188 default: /* Parent. */
192 if (sscr_getprompt(sp
))
195 F_SET(sp
, SC_SCRIPT
);
196 F_SET(sp
->gp
, G_SCRWIN
);
202 * Eat lines printed by the shell until a line with no trailing
203 * carriage return comes; set the prompt from that line.
210 CHAR_T
*endp
, *p
, *t
, buf
[1024];
222 /* Wait up to a second for characters to read. */
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");
231 case 0: /* Timeout */
232 msgq(sp
, M_ERR
, "Error: timed out");
234 case 1: /* Characters to read. */
238 /* Read the characters. */
239 more
: len
= sizeof(buf
) - (endp
- buf
);
240 switch (nr
= read(sc
->sh_master
, endp
, len
)) {
242 msgq(sp
, M_ERR
, "Error: shell: EOF");
244 case -1: /* Error or interrupt. */
245 msgq(sp
, M_SYSERR
, "shell");
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
))
263 MEMMOVE(buf
, t
, endp
- t
);
264 endp
= buf
+ (endp
- t
);
269 /* Wait up 1/10 of a second to make sure that we got it all. */
272 switch (select(sc
->sh_master
+ 1, &fdset
, NULL
, NULL
, &tv
)) {
273 case -1: /* Error or interrupt. */
274 msgq(sp
, M_SYSERR
, "select");
276 case 0: /* Timeout */
278 case 1: /* Characters to read. */
282 /* Timed out, so theoretically we have a prompt. */
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
);
292 return (sscr_setprompt(sp
, buf
, llen
));
297 * Take a line and hand it off to the shell.
299 * PUBLIC: int sscr_exec __P((SCR *, db_recno_t));
308 size_t blen
, len
, last_len
, tlen
;
309 int isempty
, matchprompt
, nw
, rval
;
313 /* If there's a prompt on the last line, append the command. */
314 if (db_last(sp
, &last_lno
))
316 if (db_get(sp
, last_lno
, DBG_FATAL
, &p
, &last_len
))
318 if (sscr_matchprompt(sp
, p
, last_len
, &tlen
) && tlen
== 0) {
320 GET_SPACE_RETW(sp
, bp
, blen
, last_len
+ 128);
321 MEMMOVEW(bp
, p
, last_len
);
325 /* Get something to execute. */
326 if (db_eget(sp
, lno
, &p
, &len
, &isempty
)) {
332 /* Empty lines aren't interesting. */
336 /* Delete any prompt. */
337 if (sscr_matchprompt(sp
, p
, len
, &tlen
)) {
339 empty
: msgq(sp
, M_BERR
, "151|No command to execute");
346 /* Push the line to the shell. */
348 if ((nw
= write(sc
->sh_master
, p
, len
)) != len
)
351 if (write(sc
->sh_master
, "\n", 1) != 1) {
354 msgq(sp
, M_SYSERR
, "shell");
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
))
365 FREE_SPACEW(sp
, bp
, blen
);
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
)
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
)) {
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
)) {
414 * Read any waiting shell input.
416 * PUBLIC: int sscr_input __P((SCR *));
436 /* Set up the input mask. */
437 for (sp
= wp
->scrq
.cqh_first
; sp
!= (void *)&wp
->scrq
;
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
)) {
448 msgq(sp
, M_SYSERR
, "select");
456 /* Read the input. */
457 for (sp
= wp
->scrq
.cqh_first
; sp
!= (void *)&wp
->scrq
;
459 if (F_ISSET(sp
, SC_SCRIPT
) &&
460 FD_ISSET(sp
->script
->sh_master
, &rdfd
) &&
468 * Take a line from the shell and insert it into the file.
475 CHAR_T
*endp
, *p
, *t
;
479 size_t blen
, len
, tlen
;
484 /* Find out where the end of the file is. */
485 if (db_last(sp
, &lno
))
489 GET_SPACE_RETW(sp
, bp
, blen
, MINREAD
);
492 /* Read the characters. */
495 more
: switch (nr
= read(sc
->sh_master
, endp
, MINREAD
)) {
496 case 0: /* EOF; shell just exited. */
500 case -1: /* Error or interrupt. */
501 msgq(sp
, M_SYSERR
, "shell");
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
) {
513 if (db_append(sp
, 1, lno
++, t
, len
))
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) {
531 FD_SET(sc
->sh_master
, &rdfd
);
532 if (select(sc
->sh_master
+ 1,
533 &rdfd
, NULL
, NULL
, &tv
) == 1) {
539 if (sscr_setprompt(sp
, t
, len
))
541 if (db_append(sp
, 1, lno
++, t
, len
))
545 /* The cursor moves to EOF. */
547 sp
->cno
= len
? len
- 1 : 0;
548 rval
= vs_refresh(sp
, 1);
550 ret
: FREE_SPACEW(sp
, bp
, blen
);
557 * Set the prompt to the last line we got from the shell.
561 sscr_setprompt(sp
, buf
, len
)
573 MALLOC(sp
, sc
->sh_prompt
, char *, len
+ 1);
574 if (sc
->sh_prompt
== NULL
) {
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';
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.
591 sscr_matchprompt(sp
, lp
, line_len
, lenp
)
594 size_t line_len
, *lenp
;
601 if (line_len
< (prompt_len
= sc
->sh_prompt_len
))
604 for (pp
= sc
->sh_prompt
;
605 prompt_len
&& line_len
; --prompt_len
, --line_len
) {
607 for (; prompt_len
&& *pp
== '\0'; --prompt_len
, ++pp
);
610 for (; line_len
&& *lp
!= *pp
; --line_len
, ++lp
);
627 * End the pipe to a shell.
629 * PUBLIC: int sscr_end __P((SCR *));
637 if ((sc
= sp
->script
) == NULL
)
640 /* Turn off the script flags. */
641 F_CLR(sp
, SC_SCRIPT
);
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);
663 * Set/clear the global scripting bit.
674 for (sp
= wp
->scrq
.cqh_first
; sp
!= (void *)&wp
->scrq
;
676 if (F_ISSET(sp
, SC_SCRIPT
)) {
684 static int ptys_open
__P((int, char *));
685 static int ptym_open
__P((char *));
688 sscr_pty(amaster
, aslave
, name
, termp
, winp
)
689 int *amaster
, *aslave
;
691 struct termios
*termp
;
694 int master
, slave
, ttygid
;
696 /* open master terminal */
697 if ((master
= ptym_open(name
)) < 0) {
698 errno
= ENOENT
; /* out of ptys */
702 /* open slave terminal */
703 if ((slave
= ptys_open(master
, name
)) >= 0) {
707 errno
= ENOENT
; /* out of ptys */
712 (void) tcsetattr(slave
, TCSAFLUSH
, termp
);
715 (void) ioctl(slave
, TIOCSWINSZ
, (struct winsize
*)winp
);
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.
730 char *ptr
, *ptsname();
732 strcpy(pts_name
, _PATH_SYSV_PTY
);
733 if ((fdm
= open(pts_name
, O_RDWR
)) < 0 )
736 if (grantpt(fdm
) < 0) {
741 if (unlockpt(fdm
) < 0) {
746 if (unlockpt(fdm
) < 0) {
751 /* get slave's name */
752 if ((ptr
= ptsname(fdm
)) == NULL
) {
756 strcpy(pts_name
, ptr
);
762 * This function opens the slave pty.
765 ptys_open(fdm
, pts_name
)
771 if ((fds
= open(pts_name
, O_RDWR
)) < 0) {
776 if (ioctl(fds
, I_PUSH
, "ptem") < 0) {
782 if (ioctl(fds
, I_PUSH
, "ldterm") < 0) {
788 if (ioctl(fds
, I_PUSH
, "ttcompat") < 0) {
797 #else /* !HAVE_SYS5_PTY */
800 sscr_pty(amaster
, aslave
, name
, termp
, winp
)
801 int *amaster
, *aslave
;
803 struct termios
*termp
;
806 static char line
[] = "/dev/ptyXX";
807 register char *cp1
, *cp2
;
808 register int master
, slave
, ttygid
;
811 if ((gr
= getgrnam("tty")) != NULL
)
816 for (cp1
= "pqrs"; *cp1
; cp1
++) {
818 for (cp2
= "0123456789abcdef"; *cp2
; cp2
++) {
821 if ((master
= open(line
, O_RDWR
, 0)) == -1) {
823 return (-1); /* out of ptys */
826 (void) chown(line
, getuid(), ttygid
);
827 (void) chmod(line
, S_IRUSR
|S_IWUSR
|S_IWGRP
);
831 if ((slave
= open(line
, O_RDWR
, 0)) != -1) {
837 (void) tcsetattr(slave
,
841 (void) ioctl(slave
, TIOCSWINSZ
,
846 (void) close(master
);
850 errno
= ENOENT
; /* out of ptys */
853 #endif /* HAVE_SYS5_PTY */