1 /* $NetBSD: ex_script.c,v 1.3 2008/12/05 22:51:42 christos Exp $ */
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
12 * See the LICENSE file for redistribution information.
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";
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>
28 #if defined(HAVE_SYS5_PTY) && !defined(__NetBSD__)
29 #include <sys/stropts.h>
34 #include <bitstring.h>
37 #include <stdio.h> /* XXX: OSF/1 bug: include before <grp.h> */
45 #include "../common/common.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 *));
65 ex_script(SCR
*sp
, EXCMD
*cmdp
)
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.
93 const char *sh
, *sh_path
;
95 /* We're going to need a shell. */
96 if (opts_empty(sp
, O_SHELL
, 0))
99 MALLOC_RET(sp
, sc
, SCRIPT
*, sizeof(SCRIPT
));
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");
116 * Turn off output postprocessing and echo.
118 sc
->sh_term
.c_oflag
&= ~OPOST
;
119 sc
->sh_term
.c_cflag
&= ~(ECHO
|ECHOE
|ECHONL
|ECHOK
);
122 if (ioctl(STDIN_FILENO
, TIOCGWINSZ
, &sc
->sh_win
) == -1) {
123 msgq(sp
, M_SYSERR
, "tcgetattr");
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");
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");
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
);
153 case 0: /* Utility. */
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);
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);
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
)
184 execl(sh_path
, sh
, "-i", NULL
);
185 msgq_str(sp
, M_SYSERR
, sh_path
, "execl: %s");
187 default: /* Parent. */
191 if (sscr_getprompt(sp
))
194 F_SET(sp
, SC_SCRIPT
);
195 F_SET(sp
->gp
, G_SCRWIN
);
201 * Eat lines printed by the shell until a line with no trailing
202 * carriage return comes; set the prompt from that line.
205 sscr_getprompt(SCR
*sp
)
208 CHAR_T
*endp
, *p
, *t
, buf
[1024];
220 /* Wait up to a second for characters to read. */
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");
229 case 0: /* Timeout */
230 msgq(sp
, M_ERR
, "Error: timed out");
232 case 1: /* Characters to read. */
236 /* Read the characters. */
237 more
: len
= sizeof(buf
) - (endp
- buf
);
238 switch (nr
= read(sc
->sh_master
, endp
, len
)) {
240 msgq(sp
, M_ERR
, "Error: shell: EOF");
242 case -1: /* Error or interrupt. */
243 msgq(sp
, M_SYSERR
, "shell");
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
))
261 MEMMOVE(buf
, t
, endp
- t
);
262 endp
= buf
+ (endp
- t
);
267 /* Wait up 1/10 of a second to make sure that we got it all. */
270 switch (select(sc
->sh_master
+ 1, &fdset
, NULL
, NULL
, &tv
)) {
271 case -1: /* Error or interrupt. */
272 msgq(sp
, M_SYSERR
, "select");
274 case 0: /* Timeout */
276 case 1: /* Characters to read. */
280 /* Timed out, so theoretically we have a prompt. */
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
);
290 return (sscr_setprompt(sp
, buf
, llen
));
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
)
304 size_t blen
, len
, last_len
, tlen
;
305 int isempty
, matchprompt
, rval
;
310 /* If there's a prompt on the last line, append the command. */
311 if (db_last(sp
, &last_lno
))
313 if (db_get(sp
, last_lno
, DBG_FATAL
, &p
, &last_len
))
315 if (sscr_matchprompt(sp
, p
, last_len
, &tlen
) && tlen
== 0) {
317 GET_SPACE_RETW(sp
, bp
, blen
, last_len
+ 128);
318 MEMMOVEW(bp
, p
, last_len
);
322 /* Get something to execute. */
323 if (db_eget(sp
, lno
, &p
, &len
, &isempty
)) {
329 /* Empty lines aren't interesting. */
333 /* Delete any prompt. */
334 if (sscr_matchprompt(sp
, p
, len
, &tlen
)) {
336 empty
: msgq(sp
, M_BERR
, "151|No command to execute");
343 /* Push the line to the shell. */
345 if ((size_t)(nw
= write(sc
->sh_master
, p
, len
)) != len
)
348 if (write(sc
->sh_master
, "\n", 1) != 1) {
351 msgq(sp
, M_SYSERR
, "shell");
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
))
362 FREE_SPACEW(sp
, bp
, blen
);
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
)
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
)) {
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
)) {
411 * Read any waiting shell input.
413 * PUBLIC: int sscr_input __P((SCR *));
432 /* Set up the input mask. */
433 for (sp
= wp
->scrq
.cqh_first
; sp
!= (void *)&wp
->scrq
;
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
)) {
444 msgq(sp
, M_SYSERR
, "select");
452 /* Read the input. */
453 for (sp
= wp
->scrq
.cqh_first
; sp
!= (void *)&wp
->scrq
;
455 if (F_ISSET(sp
, SC_SCRIPT
) &&
456 FD_ISSET(sp
->script
->sh_master
, &rdfd
) &&
464 * Take a line from the shell and insert it into the file.
470 CHAR_T
*endp
, *p
, *t
;
474 size_t blen
, len
= 0, tlen
;
479 /* Find out where the end of the file is. */
480 if (db_last(sp
, &lno
))
484 GET_SPACE_RETW(sp
, bp
, blen
, MINREAD
);
487 /* Read the characters. */
490 more
: switch (nr
= read(sc
->sh_master
, endp
, MINREAD
)) {
491 case 0: /* EOF; shell just exited. */
495 case -1: /* Error or interrupt. */
496 msgq(sp
, M_SYSERR
, "shell");
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
) {
508 if (db_append(sp
, 1, lno
++, t
, len
))
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) {
526 FD_SET(sc
->sh_master
, &rdfd
);
527 if (select(sc
->sh_master
+ 1,
528 &rdfd
, NULL
, NULL
, &tv
) == 1) {
534 if (sscr_setprompt(sp
, t
, len
))
536 if (db_append(sp
, 1, lno
++, t
, len
))
540 /* The cursor moves to EOF. */
542 sp
->cno
= len
? len
- 1 : 0;
543 rval
= vs_refresh(sp
, 1);
545 ret
: FREE_SPACEW(sp
, bp
, blen
);
552 * Set the prompt to the last line we got from the shell.
556 sscr_setprompt(SCR
*sp
, CHAR_T
*buf
, size_t len
)
565 MALLOC(sp
, sc
->sh_prompt
, char *, len
+ 1);
566 if (sc
->sh_prompt
== NULL
) {
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';
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.
583 sscr_matchprompt(SCR
*sp
, CHAR_T
*lp
, size_t line_len
, size_t *lenp
)
590 if (line_len
< (prompt_len
= sc
->sh_prompt_len
))
593 for (pp
= sc
->sh_prompt
;
594 prompt_len
&& line_len
; --prompt_len
, --line_len
) {
596 for (; prompt_len
&& *pp
== '\0'; --prompt_len
, ++pp
);
599 for (; line_len
&& *lp
!= *pp
; --line_len
, ++lp
);
616 * End the pipe to a shell.
618 * PUBLIC: int sscr_end __P((SCR *));
625 if ((sc
= sp
->script
) == NULL
)
628 /* Turn off the script flags. */
629 F_CLR(sp
, SC_SCRIPT
);
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);
651 * Set/clear the global scripting bit.
661 for (sp
= wp
->scrq
.cqh_first
; sp
!= (void *)&wp
->scrq
;
663 if (F_ISSET(sp
, SC_SCRIPT
)) {
671 static int ptys_open
__P((int, char *));
672 static int ptym_open
__P((char *));
675 sscr_pty(int *amaster
, int *aslave
, char *name
, struct termios
*termp
, void *winp
)
679 /* open master terminal */
680 if ((master
= ptym_open(name
)) < 0) {
681 errno
= ENOENT
; /* out of ptys */
685 /* open slave terminal */
686 if ((slave
= ptys_open(master
, name
)) >= 0) {
690 errno
= ENOENT
; /* out of ptys */
695 (void) tcsetattr(slave
, TCSAFLUSH
, termp
);
698 (void) ioctl(slave
, TIOCSWINSZ
, (struct winsize
*)winp
);
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.
709 ptym_open(char *pts_name
)
714 strcpy(pts_name
, _PATH_SYSV_PTY
);
715 if ((fdm
= open(pts_name
, O_RDWR
)) < 0 )
718 if (grantpt(fdm
) < 0) {
723 if (unlockpt(fdm
) < 0) {
728 if (unlockpt(fdm
) < 0) {
733 /* get slave's name */
734 if ((ptr
= ptsname(fdm
)) == NULL
) {
738 strcpy(pts_name
, ptr
);
744 * This function opens the slave pty.
747 ptys_open(int fdm
, char *pts_name
)
751 if ((fds
= open(pts_name
, O_RDWR
)) < 0) {
757 if (ioctl(fds
, I_PUSH
, "ptem") < 0) {
763 if (ioctl(fds
, I_PUSH
, "ldterm") < 0) {
769 if (ioctl(fds
, I_PUSH
, "ttcompat") < 0) {
779 #else /* !HAVE_SYS5_PTY */
782 sscr_pty(amaster
, aslave
, name
, termp
, winp
)
783 int *amaster
, *aslave
;
785 struct termios
*termp
;
788 static char line
[] = "/dev/ptyXX";
789 register char *cp1
, *cp2
;
790 register int master
, slave
, ttygid
;
793 if ((gr
= getgrnam("tty")) != NULL
)
798 for (cp1
= "pqrs"; *cp1
; cp1
++) {
800 for (cp2
= "0123456789abcdef"; *cp2
; cp2
++) {
803 if ((master
= open(line
, O_RDWR
, 0)) == -1) {
805 return (-1); /* out of ptys */
808 (void) chown(line
, getuid(), ttygid
);
809 (void) chmod(line
, S_IRUSR
|S_IWUSR
|S_IWGRP
);
813 if ((slave
= open(line
, O_RDWR
, 0)) != -1) {
819 (void) tcsetattr(slave
,
823 (void) ioctl(slave
, TIOCSWINSZ
,
828 (void) close(master
);
832 errno
= ENOENT
; /* out of ptys */
835 #endif /* HAVE_SYS5_PTY */