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
[] = "@(#)ex_script.c 10.30 (Berkeley) 9/24/96";
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 *, 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 *));
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 *, recno_t));
308 size_t blen
, len
, last_len
, tlen
;
309 int isempty
, matchprompt
, nw
, rval
;
312 /* If there's a prompt on the last line, append the command. */
313 if (db_last(sp
, &last_lno
))
315 if (db_get(sp
, last_lno
, DBG_FATAL
, &p
, &last_len
))
317 if (sscr_matchprompt(sp
, p
, last_len
, &tlen
) && tlen
== 0) {
319 GET_SPACE_RET(sp
, bp
, blen
, last_len
+ 128);
320 memmove(bp
, p
, last_len
);
324 /* Get something to execute. */
325 if (db_eget(sp
, lno
, &p
, &len
, &isempty
)) {
331 /* Empty lines aren't interesting. */
335 /* Delete any prompt. */
336 if (sscr_matchprompt(sp
, p
, len
, &tlen
)) {
338 empty
: msgq(sp
, M_BERR
, "151|No command to execute");
345 /* Push the line to the shell. */
347 if ((nw
= write(sc
->sh_master
, p
, len
)) != len
)
350 if (write(sc
->sh_master
, "\n", 1) != 1) {
353 msgq(sp
, M_SYSERR
, "shell");
358 ADD_SPACE_RET(sp
, bp
, blen
, last_len
+ len
);
359 memmove(bp
+ last_len
, p
, len
);
360 if (db_set(sp
, last_lno
, bp
, last_len
+ len
))
364 FREE_SPACE(sp
, bp
, blen
);
370 * Read any waiting shell input.
372 * PUBLIC: int sscr_input __P((SCR *));
390 /* Set up the input mask. */
391 for (sp
= gp
->dq
.cqh_first
; sp
!= (void *)&gp
->dq
; sp
= sp
->q
.cqe_next
)
392 if (F_ISSET(sp
, SC_SCRIPT
)) {
393 FD_SET(sp
->script
->sh_master
, &rdfd
);
394 if (sp
->script
->sh_master
> maxfd
)
395 maxfd
= sp
->script
->sh_master
;
398 /* Check for input. */
399 switch (select(maxfd
+ 1, &rdfd
, NULL
, NULL
, &poll
)) {
401 msgq(sp
, M_SYSERR
, "select");
409 /* Read the input. */
410 for (sp
= gp
->dq
.cqh_first
; sp
!= (void *)&gp
->dq
; sp
= sp
->q
.cqe_next
)
411 if (F_ISSET(sp
, SC_SCRIPT
) &&
412 FD_ISSET(sp
->script
->sh_master
, &rdfd
) && sscr_insert(sp
))
419 * Take a line from the shell and insert it into the file.
426 CHAR_T
*endp
, *p
, *t
;
430 size_t blen
, len
, tlen
;
435 /* Find out where the end of the file is. */
436 if (db_last(sp
, &lno
))
440 GET_SPACE_RET(sp
, bp
, blen
, MINREAD
);
443 /* Read the characters. */
446 more
: switch (nr
= read(sc
->sh_master
, endp
, MINREAD
)) {
447 case 0: /* EOF; shell just exited. */
451 case -1: /* Error or interrupt. */
452 msgq(sp
, M_SYSERR
, "shell");
459 /* Append the lines into the file. */
460 for (p
= t
= bp
; p
< endp
; ++p
) {
461 value
= KEY_VAL(sp
, *p
);
462 if (value
== K_CR
|| value
== K_NL
) {
464 if (db_append(sp
, 1, lno
++, t
, len
))
472 * If the last thing from the shell isn't another prompt, wait
473 * up to 1/10 of a second for more stuff to show up, so that
474 * we don't break the output into two separate lines. Don't
475 * want to hang indefinitely because some program is hanging,
476 * confused the shell, or whatever.
478 if (!sscr_matchprompt(sp
, t
, len
, &tlen
) || tlen
!= 0) {
482 FD_SET(sc
->sh_master
, &rdfd
);
483 if (select(sc
->sh_master
+ 1,
484 &rdfd
, NULL
, NULL
, &tv
) == 1) {
490 if (sscr_setprompt(sp
, t
, len
))
492 if (db_append(sp
, 1, lno
++, t
, len
))
496 /* The cursor moves to EOF. */
498 sp
->cno
= len
? len
- 1 : 0;
499 rval
= vs_refresh(sp
, 1);
501 ret
: FREE_SPACE(sp
, bp
, blen
);
508 * Set the prompt to the last line we got from the shell.
512 sscr_setprompt(sp
, buf
, len
)
522 MALLOC(sp
, sc
->sh_prompt
, char *, len
+ 1);
523 if (sc
->sh_prompt
== NULL
) {
527 memmove(sc
->sh_prompt
, buf
, len
);
528 sc
->sh_prompt_len
= len
;
529 sc
->sh_prompt
[len
] = '\0';
534 * sscr_matchprompt --
535 * Check to see if a line matches the prompt. Nul's indicate
536 * parts that can change, in both content and size.
539 sscr_matchprompt(sp
, lp
, line_len
, lenp
)
542 size_t line_len
, *lenp
;
549 if (line_len
< (prompt_len
= sc
->sh_prompt_len
))
552 for (pp
= sc
->sh_prompt
;
553 prompt_len
&& line_len
; --prompt_len
, --line_len
) {
555 for (; prompt_len
&& *pp
== '\0'; --prompt_len
, ++pp
);
558 for (; line_len
&& *lp
!= *pp
; --line_len
, ++lp
);
575 * End the pipe to a shell.
577 * PUBLIC: int sscr_end __P((SCR *));
585 if ((sc
= sp
->script
) == NULL
)
588 /* Turn off the script flags. */
589 F_CLR(sp
, SC_SCRIPT
);
592 /* Close down the parent's file descriptors. */
593 if (sc
->sh_master
!= -1)
594 (void)close(sc
->sh_master
);
595 if (sc
->sh_slave
!= -1)
596 (void)close(sc
->sh_slave
);
598 /* This should have killed the child. */
599 (void)proc_wait(sp
, (long)sc
->sh_pid
, "script-shell", 0, 0);
611 * Set/clear the global scripting bit.
620 for (sp
= gp
->dq
.cqh_first
; sp
!= (void *)&gp
->dq
; sp
= sp
->q
.cqe_next
)
621 if (F_ISSET(sp
, SC_SCRIPT
)) {
629 static int ptys_open
__P((int, char *));
630 static int ptym_open
__P((char *));
633 sscr_pty(amaster
, aslave
, name
, termp
, winp
)
634 int *amaster
, *aslave
;
636 struct termios
*termp
;
639 int master
, slave
, ttygid
;
641 /* open master terminal */
642 if ((master
= ptym_open(name
)) < 0) {
643 errno
= ENOENT
; /* out of ptys */
647 /* open slave terminal */
648 if ((slave
= ptys_open(master
, name
)) >= 0) {
652 errno
= ENOENT
; /* out of ptys */
657 (void) tcsetattr(slave
, TCSAFLUSH
, termp
);
660 (void) ioctl(slave
, TIOCSWINSZ
, (struct winsize
*)winp
);
667 * This function opens a master pty and returns the file descriptor
668 * to it. pts_name is also returned which is the name of the slave.
675 char *ptr
, *ptsname();
677 strcpy(pts_name
, _PATH_SYSV_PTY
);
678 if ((fdm
= open(pts_name
, O_RDWR
)) < 0 )
681 if (grantpt(fdm
) < 0) {
686 if (unlockpt(fdm
) < 0) {
691 if (unlockpt(fdm
) < 0) {
696 /* get slave's name */
697 if ((ptr
= ptsname(fdm
)) == NULL
) {
701 strcpy(pts_name
, ptr
);
707 * This function opens the slave pty.
710 ptys_open(fdm
, pts_name
)
716 if ((fds
= open(pts_name
, O_RDWR
)) < 0) {
721 if (ioctl(fds
, I_PUSH
, "ptem") < 0) {
727 if (ioctl(fds
, I_PUSH
, "ldterm") < 0) {
733 if (ioctl(fds
, I_PUSH
, "ttcompat") < 0) {
742 #else /* !HAVE_SYS5_PTY */
745 sscr_pty(amaster
, aslave
, name
, termp
, winp
)
746 int *amaster
, *aslave
;
748 struct termios
*termp
;
751 static char line
[] = "/dev/ptyXX";
752 register char *cp1
, *cp2
;
753 register int master
, slave
, ttygid
;
756 if ((gr
= getgrnam("tty")) != NULL
)
761 for (cp1
= "pqrs"; *cp1
; cp1
++) {
763 for (cp2
= "0123456789abcdef"; *cp2
; cp2
++) {
766 if ((master
= open(line
, O_RDWR
, 0)) == -1) {
768 return (-1); /* out of ptys */
771 (void) chown(line
, getuid(), ttygid
);
772 (void) chmod(line
, S_IRUSR
|S_IWUSR
|S_IWGRP
);
776 if ((slave
= open(line
, O_RDWR
, 0)) != -1) {
782 (void) tcsetattr(slave
,
786 (void) ioctl(slave
, TIOCSWINSZ
,
791 (void) close(master
);
795 errno
= ENOENT
; /* out of ptys */
798 #endif /* HAVE_SYS5_PTY */