1 /* $NetBSD: ex_script.c,v 1.4 2014/01/26 21:43:45 christos Exp $ */
3 * Copyright (c) 1992, 1993, 1994
4 * The Regents of the University of California. All rights reserved.
5 * Copyright (c) 1992, 1993, 1994, 1995, 1996
6 * Keith Bostic. All rights reserved.
8 * This code is derived from software contributed to Berkeley by
11 * See the LICENSE file for redistribution information.
16 #include <sys/cdefs.h>
19 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 ";
22 __RCSID("$NetBSD: ex_script.c,v 1.4 2014/01/26 21:43:45 christos Exp $");
25 #include <sys/types.h>
26 #include <sys/ioctl.h>
27 #include <sys/queue.h>
28 #ifdef HAVE_SYS_SELECT_H
29 #include <sys/select.h>
32 #if defined(HAVE_SYS5_PTY) && !defined(__NetBSD__)
33 #include <sys/stropts.h>
38 #include <bitstring.h>
41 #include <stdio.h> /* XXX: OSF/1 bug: include before <grp.h> */
49 #include "../common/common.h"
52 #include "pathnames.h"
54 static void sscr_check
__P((SCR
*));
55 static int sscr_getprompt
__P((SCR
*));
56 static int sscr_init
__P((SCR
*));
57 static int sscr_insert
__P((SCR
*));
58 static int sscr_matchprompt
__P((SCR
*, CHAR_T
*, size_t, size_t *));
59 static int sscr_pty
__P((int *, int *, char *, struct termios
*, void *));
60 static int sscr_setprompt
__P((SCR
*, CHAR_T
*, size_t));
63 * ex_script -- : sc[ript][!] [file]
64 * Switch to script mode.
66 * PUBLIC: int ex_script __P((SCR *, EXCMD *));
69 ex_script(SCR
*sp
, EXCMD
*cmdp
)
71 /* Vi only command. */
72 if (!F_ISSET(sp
, SC_VI
)) {
74 "150|The script command is only available in vi mode");
78 /* Switch to the new file. */
79 if (cmdp
->argc
!= 0 && ex_edit(sp
, cmdp
))
82 /* Create the shell, figure out the prompt. */
91 * Create a pty setup for a shell.
97 const char *sh
, *sh_path
;
99 /* We're going to need a shell. */
100 if (opts_empty(sp
, O_SHELL
, 0))
103 MALLOC_RET(sp
, sc
, SCRIPT
*, sizeof(SCRIPT
));
105 sc
->sh_prompt
= NULL
;
106 sc
->sh_prompt_len
= 0;
109 * There are two different processes running through this code.
110 * They are the shell and the parent.
112 sc
->sh_master
= sc
->sh_slave
= -1;
114 if (tcgetattr(STDIN_FILENO
, &sc
->sh_term
) == -1) {
115 msgq(sp
, M_SYSERR
, "tcgetattr");
120 * Turn off output postprocessing and echo.
122 sc
->sh_term
.c_oflag
&= ~OPOST
;
123 sc
->sh_term
.c_cflag
&= ~(ECHO
|ECHOE
|ECHONL
|ECHOK
);
126 if (ioctl(STDIN_FILENO
, TIOCGWINSZ
, &sc
->sh_win
) == -1) {
127 msgq(sp
, M_SYSERR
, "tcgetattr");
131 if (sscr_pty(&sc
->sh_master
,
132 &sc
->sh_slave
, sc
->sh_name
, &sc
->sh_term
, &sc
->sh_win
) == -1) {
133 msgq(sp
, M_SYSERR
, "pty");
137 if (sscr_pty(&sc
->sh_master
,
138 &sc
->sh_slave
, sc
->sh_name
, &sc
->sh_term
, NULL
) == -1) {
139 msgq(sp
, M_SYSERR
, "pty");
146 * Don't use vfork() here, because the signal semantics differ from
147 * implementation to implementation.
149 switch (sc
->sh_pid
= fork()) {
150 case -1: /* Error. */
151 msgq(sp
, M_SYSERR
, "fork");
152 err
: if (sc
->sh_master
!= -1)
153 (void)close(sc
->sh_master
);
154 if (sc
->sh_slave
!= -1)
155 (void)close(sc
->sh_slave
);
157 case 0: /* Utility. */
160 * So that shells that do command line editing turn it off.
162 (void)setenv("TERM", "emacs", 1);
163 (void)setenv("TERMCAP", "emacs:", 1);
164 (void)setenv("EMACS", "t", 1);
169 * 4.4BSD allocates a controlling terminal using the TIOCSCTTY
170 * ioctl, not by opening a terminal device file. POSIX 1003.1
171 * doesn't define a portable way to do this. If TIOCSCTTY is
172 * not available, hope that the open does it.
174 (void)ioctl(sc
->sh_slave
, TIOCSCTTY
, 0);
176 (void)close(sc
->sh_master
);
177 (void)dup2(sc
->sh_slave
, STDIN_FILENO
);
178 (void)dup2(sc
->sh_slave
, STDOUT_FILENO
);
179 (void)dup2(sc
->sh_slave
, STDERR_FILENO
);
180 (void)close(sc
->sh_slave
);
182 /* Assumes that all shells have -i. */
183 sh_path
= O_STR(sp
, O_SHELL
);
184 if ((sh
= strrchr(sh_path
, '/')) == NULL
)
188 execl(sh_path
, sh
, "-i", NULL
);
189 msgq_str(sp
, M_SYSERR
, sh_path
, "execl: %s");
191 default: /* Parent. */
195 if (sscr_getprompt(sp
))
198 F_SET(sp
, SC_SCRIPT
);
199 F_SET(sp
->gp
, G_SCRWIN
);
205 * Eat lines printed by the shell until a line with no trailing
206 * carriage return comes; set the prompt from that line.
209 sscr_getprompt(SCR
*sp
)
212 CHAR_T
*endp
, *p
, *t
, buf
[1024];
224 /* Wait up to a second for characters to read. */
228 FD_SET(sc
->sh_master
, &fdset
);
229 switch (select(sc
->sh_master
+ 1, &fdset
, NULL
, NULL
, &tv
)) {
230 case -1: /* Error or interrupt. */
231 msgq(sp
, M_SYSERR
, "select");
233 case 0: /* Timeout */
234 msgq(sp
, M_ERR
, "Error: timed out");
236 case 1: /* Characters to read. */
240 /* Read the characters. */
241 more
: len
= sizeof(buf
) - (endp
- buf
);
242 switch (nr
= read(sc
->sh_master
, endp
, len
)) {
244 msgq(sp
, M_ERR
, "Error: shell: EOF");
246 case -1: /* Error or interrupt. */
247 msgq(sp
, M_SYSERR
, "shell");
254 /* If any complete lines, push them into the file. */
255 for (p
= t
= buf
; p
< endp
; ++p
) {
256 value
= KEY_VAL(sp
, *p
);
257 if (value
== K_CR
|| value
== K_NL
) {
258 if (db_last(sp
, &lline
) ||
259 db_append(sp
, 0, lline
, t
, p
- t
))
265 MEMMOVE(buf
, t
, endp
- t
);
266 endp
= buf
+ (endp
- t
);
271 /* Wait up 1/10 of a second to make sure that we got it all. */
274 switch (select(sc
->sh_master
+ 1, &fdset
, NULL
, NULL
, &tv
)) {
275 case -1: /* Error or interrupt. */
276 msgq(sp
, M_SYSERR
, "select");
278 case 0: /* Timeout */
280 case 1: /* Characters to read. */
284 /* Timed out, so theoretically we have a prompt. */
288 /* Append the line into the file. */
289 if (db_last(sp
, &lline
) || db_append(sp
, 0, lline
, buf
, llen
)) {
290 prompterr
: sscr_end(sp
);
294 return (sscr_setprompt(sp
, buf
, llen
));
299 * Take a line and hand it off to the shell.
301 * PUBLIC: int sscr_exec __P((SCR *, db_recno_t));
304 sscr_exec(SCR
*sp
, db_recno_t lno
)
308 size_t blen
, len
, last_len
, tlen
;
309 int isempty
, matchprompt
, rval
;
314 /* If there's a prompt on the last line, append the command. */
315 if (db_last(sp
, &last_lno
))
317 if (db_get(sp
, last_lno
, DBG_FATAL
, &p
, &last_len
))
319 if (sscr_matchprompt(sp
, p
, last_len
, &tlen
) && tlen
== 0) {
321 GET_SPACE_RETW(sp
, bp
, blen
, last_len
+ 128);
322 MEMMOVEW(bp
, p
, last_len
);
326 /* Get something to execute. */
327 if (db_eget(sp
, lno
, &p
, &len
, &isempty
)) {
333 /* Empty lines aren't interesting. */
337 /* Delete any prompt. */
338 if (sscr_matchprompt(sp
, p
, len
, &tlen
)) {
340 empty
: msgq(sp
, M_BERR
, "151|No command to execute");
347 /* Push the line to the shell. */
349 if ((size_t)(nw
= write(sc
->sh_master
, p
, len
)) != len
)
352 if (write(sc
->sh_master
, "\n", 1) != 1) {
355 msgq(sp
, M_SYSERR
, "shell");
360 ADD_SPACE_RETW(sp
, bp
, blen
, last_len
+ len
);
361 MEMMOVEW(bp
+ last_len
, p
, len
);
362 if (db_set(sp
, last_lno
, bp
, last_len
+ len
))
366 FREE_SPACEW(sp
, bp
, blen
);
372 * Check whether any input from shell or passed set.
374 * PUBLIC: int sscr_check_input __P((SCR *sp, fd_set *rdfd, int maxfd));
377 sscr_check_input(SCR
*sp
, fd_set
*fdset
, int maxfd
)
385 loop
: memcpy(&rdfd
, fdset
, sizeof(fd_set
));
387 TAILQ_FOREACH(tsp
, &wp
->scrq
, q
)
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 TAILQ_FOREACH(tsp
, &wp
->scrq
, q
)
402 if (F_ISSET(sp
, SC_SCRIPT
) &&
403 FD_ISSET(sp
->script
->sh_master
, &rdfd
)) {
413 * Read any waiting shell input.
415 * PUBLIC: int sscr_input __P((SCR *));
432 /* Set up the input mask. */
433 TAILQ_FOREACH(sp
, &wp
->scrq
, q
)
434 if (F_ISSET(sp
, SC_SCRIPT
)) {
435 FD_SET(sp
->script
->sh_master
, &rdfd
);
436 if (sp
->script
->sh_master
> maxfd
)
437 maxfd
= sp
->script
->sh_master
;
440 /* Check for input. */
441 switch (select(maxfd
+ 1, &rdfd
, NULL
, NULL
, &poll
)) {
443 msgq(sp
, M_SYSERR
, "select");
451 /* Read the input. */
452 TAILQ_FOREACH(sp
, &wp
->scrq
, q
)
453 if (F_ISSET(sp
, SC_SCRIPT
) &&
454 FD_ISSET(sp
->script
->sh_master
, &rdfd
) &&
462 * Take a line from the shell and insert it into the file.
468 CHAR_T
*endp
, *p
, *t
;
472 size_t blen
, len
= 0, tlen
;
477 /* Find out where the end of the file is. */
478 if (db_last(sp
, &lno
))
482 GET_SPACE_RETW(sp
, bp
, blen
, MINREAD
);
485 /* Read the characters. */
488 more
: switch (nr
= read(sc
->sh_master
, endp
, MINREAD
)) {
489 case 0: /* EOF; shell just exited. */
493 case -1: /* Error or interrupt. */
494 msgq(sp
, M_SYSERR
, "shell");
501 /* Append the lines into the file. */
502 for (p
= t
= bp
; p
< endp
; ++p
) {
503 value
= KEY_VAL(sp
, *p
);
504 if (value
== K_CR
|| value
== K_NL
) {
506 if (db_append(sp
, 1, lno
++, t
, len
))
514 * If the last thing from the shell isn't another prompt, wait
515 * up to 1/10 of a second for more stuff to show up, so that
516 * we don't break the output into two separate lines. Don't
517 * want to hang indefinitely because some program is hanging,
518 * confused the shell, or whatever.
520 if (!sscr_matchprompt(sp
, t
, len
, &tlen
) || tlen
!= 0) {
524 FD_SET(sc
->sh_master
, &rdfd
);
525 if (select(sc
->sh_master
+ 1,
526 &rdfd
, NULL
, NULL
, &tv
) == 1) {
532 if (sscr_setprompt(sp
, t
, len
))
534 if (db_append(sp
, 1, lno
++, t
, len
))
538 /* The cursor moves to EOF. */
540 sp
->cno
= len
? len
- 1 : 0;
541 rval
= vs_refresh(sp
, 1);
543 ret
: FREE_SPACEW(sp
, bp
, blen
);
550 * Set the prompt to the last line we got from the shell.
554 sscr_setprompt(SCR
*sp
, CHAR_T
*buf
, size_t len
)
563 MALLOC(sp
, sc
->sh_prompt
, char *, len
+ 1);
564 if (sc
->sh_prompt
== NULL
) {
568 INT2CHAR(sp
, buf
, len
, np
, nlen
);
569 memmove(sc
->sh_prompt
, np
, nlen
);
570 sc
->sh_prompt_len
= len
;
571 sc
->sh_prompt
[len
] = '\0';
576 * sscr_matchprompt --
577 * Check to see if a line matches the prompt. Nul's indicate
578 * parts that can change, in both content and size.
581 sscr_matchprompt(SCR
*sp
, CHAR_T
*lp
, size_t line_len
, size_t *lenp
)
588 if (line_len
< (prompt_len
= sc
->sh_prompt_len
))
591 for (pp
= sc
->sh_prompt
;
592 prompt_len
&& line_len
; --prompt_len
, --line_len
) {
594 for (; prompt_len
&& *pp
== '\0'; --prompt_len
, ++pp
);
597 for (; line_len
&& *lp
!= *pp
; --line_len
, ++lp
);
614 * End the pipe to a shell.
616 * PUBLIC: int sscr_end __P((SCR *));
623 if ((sc
= sp
->script
) == NULL
)
626 /* Turn off the script flags. */
627 F_CLR(sp
, SC_SCRIPT
);
630 /* Close down the parent's file descriptors. */
631 if (sc
->sh_master
!= -1)
632 (void)close(sc
->sh_master
);
633 if (sc
->sh_slave
!= -1)
634 (void)close(sc
->sh_slave
);
636 /* This should have killed the child. */
637 (void)proc_wait(sp
, (long)sc
->sh_pid
, "script-shell", 0, 0);
649 * Set/clear the global scripting bit.
659 TAILQ_FOREACH(sp
, &wp
->scrq
, q
)
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
)
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
)
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) {
754 if (ioctl(fds
, I_PUSH
, "ptem") < 0) {
760 if (ioctl(fds
, I_PUSH
, "ldterm") < 0) {
766 if (ioctl(fds
, I_PUSH
, "ttcompat") < 0) {
776 #else /* !HAVE_SYS5_PTY */
779 sscr_pty(amaster
, aslave
, name
, termp
, winp
)
780 int *amaster
, *aslave
;
782 struct termios
*termp
;
785 static char line
[] = "/dev/ptyXX";
786 const char *cp1
, *cp2
;
787 int master
, slave
, ttygid
;
790 if ((gr
= getgrnam("tty")) != NULL
)
795 for (cp1
= "pqrs"; *cp1
; cp1
++) {
797 for (cp2
= "0123456789abcdef"; *cp2
; cp2
++) {
800 if ((master
= open(line
, O_RDWR
, 0)) == -1) {
802 return (-1); /* out of ptys */
805 (void) chown(line
, getuid(), ttygid
);
806 (void) chmod(line
, S_IRUSR
|S_IWUSR
|S_IWGRP
);
810 if ((slave
= open(line
, O_RDWR
, 0)) != -1) {
816 (void) tcsetattr(slave
,
820 (void) ioctl(slave
, TIOCSWINSZ
,
825 (void) close(master
);
829 errno
= ENOENT
; /* out of ptys */
832 #endif /* HAVE_SYS5_PTY */