2 * Copyright (c) 1992, 1993
3 * The Regents of the University of California. All rights reserved.
5 * %sccs.include.redist.c%
9 static char sccsid
[] = "$Id: ex_script.c,v 8.8 1993/12/19 19:36:59 bostic Exp $ (Berkeley) $Date: 1993/12/19 19:36:59 $";
12 #include <sys/types.h>
13 #include <sys/ioctl.h>
28 int openpty
__P((int *, int *, char *, struct termios
*, struct winsize
*));
30 static int sscr_getprompt
__P((SCR
*, EXF
*));
31 static int sscr_init
__P((SCR
*, EXF
*));
32 static int sscr_matchprompt
__P((SCR
*, char *, size_t, size_t *));
33 static int sscr_setprompt
__P((SCR
*, char *, size_t));
36 * ex_script -- : sc[ript][!] [file]
38 * Switch to script mode.
41 ex_script(sp
, ep
, cmdp
)
46 /* Vi only command. */
47 if (!IN_VI_MODE(sp
)) {
49 "The script command is only available in vi mode.");
53 /* Switch to the new file. */
54 if (cmdp
->argc
!= 0 && ex_edit(sp
, ep
, cmdp
))
58 * Create the shell, figure out the prompt.
61 * The files just switched, use sp->ep.
63 if (sscr_init(sp
, sp
->ep
))
71 * Create a pty setup for a shell.
81 MALLOC_RET(sp
, sc
, SCRIPT
*, sizeof(SCRIPT
));
84 sc
->sh_prompt_len
= 0;
87 * There are two different processes running through this code.
88 * They are the shell and the parent.
90 sc
->sh_master
= sc
->sh_slave
= -1;
92 if (tcgetattr(STDIN_FILENO
, &sc
->sh_term
) == -1) {
93 msgq(sp
, M_SYSERR
, "tcgetattr");
98 * Turn off output postprocessing and echo.
100 sc
->sh_term
.c_oflag
&= ~OPOST
;
101 sc
->sh_term
.c_cflag
&= ~(ECHO
|ECHOE
|ECHONL
|ECHOK
);
103 if (ioctl(STDIN_FILENO
, TIOCGWINSZ
, &sc
->sh_win
) == -1) {
104 msgq(sp
, M_SYSERR
, "tcgetattr");
108 if (openpty(&sc
->sh_master
,
109 &sc
->sh_slave
, sc
->sh_name
, &sc
->sh_term
, &sc
->sh_win
) == -1) {
110 msgq(sp
, M_SYSERR
, "openpty");
115 * Don't use vfork() here, because the signal semantics
116 * differ from implementation to implementation.
118 switch (sc
->sh_pid
= fork()) {
119 case -1: /* Error. */
120 msgq(sp
, M_SYSERR
, "fork");
121 err
: if (sc
->sh_master
!= -1)
122 (void)close(sc
->sh_master
);
123 if (sc
->sh_slave
!= -1)
124 (void)close(sc
->sh_slave
);
126 case 0: /* Utility. */
128 * The utility has default signal behavior. Don't bother
129 * using sigaction(2) 'cause we want the default behavior.
131 (void)signal(SIGINT
, SIG_DFL
);
132 (void)signal(SIGQUIT
, SIG_DFL
);
136 * So that shells that do command line editing turn it off.
138 (void)putenv("TERM=emacs");
139 (void)putenv("TERMCAP=emacs:");
140 (void)putenv("EMACS=t");
143 (void)ioctl(sc
->sh_slave
, TIOCSCTTY
, 0);
144 (void)close(sc
->sh_master
);
145 (void)dup2(sc
->sh_slave
, STDIN_FILENO
);
146 (void)dup2(sc
->sh_slave
, STDOUT_FILENO
);
147 (void)dup2(sc
->sh_slave
, STDERR_FILENO
);
148 (void)close(sc
->sh_slave
);
150 /* Assumes that all shells have -i. */
151 sh_path
= O_STR(sp
, O_SHELL
);
152 if ((sh
= strrchr(sh_path
, '/')) == NULL
)
156 execl(sh_path
, sh
, "-i", NULL
);
158 "Error: execl: %s: %s", sh_path
, strerror(errno
));
164 if (sscr_getprompt(sp
, ep
))
167 F_SET(sp
, S_REDRAW
| S_SCRIPT
);
174 * Eat lines printed by the shell until a line with no trailing
175 * carriage return comes; set the prompt from that line.
178 sscr_getprompt(sp
, ep
)
189 char *endp
, *p
, *t
, buf
[1024];
195 /* Wait up to a second for characters to read. */
199 FD_SET(sc
->sh_master
, &fdset
);
200 switch (select(sc
->sh_master
+ 1, &fdset
, NULL
, NULL
, &tv
)) {
201 case -1: /* Error or interrupt. */
202 msgq(sp
, M_SYSERR
, "select");
204 case 0: /* Timeout */
205 msgq(sp
, M_ERR
, "Error: timed out.");
207 case 1: /* Characters to read. */
211 /* Read the characters. */
212 more
: len
= sizeof(buf
) - (endp
- buf
);
213 switch (nr
= read(sc
->sh_master
, endp
, len
)) {
215 msgq(sp
, M_ERR
, "Error: shell: EOF");
217 case -1: /* Error or interrupt. */
218 msgq(sp
, M_SYSERR
, "shell");
225 /* If any complete lines, push them into the file. */
226 for (p
= t
= buf
; t
< endp
; ++t
) {
227 value
= term_key_val(sp
, *t
);
228 if (value
== K_CR
|| value
== K_NL
) {
229 if (file_lline(sp
, ep
, &lline
) ||
230 file_aline(sp
, ep
, 0, lline
, buf
, t
- p
))
236 memmove(buf
, p
, endp
- p
);
237 endp
= buf
+ (endp
- p
);
242 /* Wait up 1/10 of a second to make sure that we got it all. */
245 switch (select(sc
->sh_master
+ 1, &fdset
, NULL
, NULL
, &tv
)) {
246 case -1: /* Error or interrupt. */
247 msgq(sp
, M_SYSERR
, "select");
249 case 0: /* Timeout */
251 case 1: /* Characters to read. */
255 /* Timed out, so theoretically we have a prompt. */
259 /* Append the line into the file. */
260 if (file_lline(sp
, ep
, &lline
) ||
261 file_aline(sp
, ep
, 0, lline
, buf
, llen
)) {
262 prompterr
: sscr_end(sp
);
266 return (sscr_setprompt(sp
, buf
, llen
));
271 * Take a line and hand it off to the shell.
274 sscr_exec(sp
, ep
, lno
)
281 size_t blen
, len
, last_len
, tlen
;
282 int matchprompt
, nw
, rval
;
285 /* If there's a prompt on the last line, append the command. */
286 if (file_lline(sp
, ep
, &last_lno
))
288 if ((p
= file_gline(sp
, ep
, last_lno
, &last_len
)) == NULL
) {
289 GETLINE_ERR(sp
, last_lno
);
292 if (sscr_matchprompt(sp
, p
, last_len
, &tlen
) && tlen
== 0) {
294 GET_SPACE_RET(sp
, bp
, blen
, last_len
+ 128);
295 memmove(bp
, p
, last_len
);
299 /* Get something to execute. */
300 if ((p
= file_gline(sp
, ep
, lno
, &len
)) == NULL
) {
301 if (file_lline(sp
, ep
, &lno
))
306 GETLINE_ERR(sp
, lno
);
310 /* Empty lines aren't interesting. */
314 /* Delete any prompt. */
315 if (sscr_matchprompt(sp
, p
, len
, &tlen
)) {
317 empty
: msgq(sp
, M_BERR
, "Nothing to execute.");
324 /* Push the line to the shell. */
326 if ((nw
= write(sc
->sh_master
, p
, len
)) != len
)
329 if (write(sc
->sh_master
, "\n", 1) != 1) {
332 msgq(sp
, M_SYSERR
, "shell");
337 ADD_SPACE_RET(sp
, bp
, blen
, last_len
+ len
);
338 memmove(bp
+ last_len
, p
, len
);
339 if (file_sline(sp
, ep
, last_lno
, bp
, last_len
+ len
))
343 FREE_SPACE(sp
, bp
, blen
);
349 * Take a line from the shell and insert it into the file.
359 size_t blen
, len
, tlen
;
362 char *bp
, *endp
, *p
, *t
;
364 /* Find out where the end of the file is. */
366 if (file_lline(sp
, ep
, &lno
))
370 GET_SPACE_RET(sp
, bp
, blen
, MINREAD
);
373 /* Read the characters. */
376 more
: switch (nr
= read(sc
->sh_master
, endp
, MINREAD
)) {
377 case 0: /* EOF; shell just exited. */
382 case -1: /* Error or interrupt. */
383 msgq(sp
, M_SYSERR
, "shell");
390 /* Append the lines into the file. */
391 for (p
= t
= bp
; p
< endp
; ++p
) {
392 value
= term_key_val(sp
, *p
);
393 if (value
== K_CR
|| value
== K_NL
) {
395 if (file_aline(sp
, ep
, 1, lno
++, t
, len
))
403 * If the last thing from the shell isn't another prompt, wait
404 * up to 1/10 of a second for more stuff to show up, so that
405 * we don't break the output into two separate lines. Don't
406 * want to hang indefinitely because some program is hanging,
407 * confused the shell, or whatever.
409 if (!sscr_matchprompt(sp
, t
, len
, &tlen
) || tlen
!= 0) {
412 FD_SET(sc
->sh_master
, &sp
->rdfd
);
413 FD_CLR(STDIN_FILENO
, &sp
->rdfd
);
414 if (select(sc
->sh_master
+ 1,
415 &sp
->rdfd
, NULL
, NULL
, &tv
) == 1) {
421 if (sscr_setprompt(sp
, t
, len
))
423 if (file_aline(sp
, ep
, 1, lno
++, t
, len
))
427 /* The cursor moves to EOF. */
429 sp
->cno
= len
? len
- 1 : 0;
430 rval
= sp
->s_refresh(sp
, ep
);
432 ret
: FREE_SPACE(sp
, bp
, blen
);
439 * Set the prompt to the last line we got from the shell.
443 sscr_setprompt(sp
, buf
, len
)
452 FREE(sc
->sh_prompt
, sc
->sh_prompt_len
);
453 MALLOC(sp
, sc
->sh_prompt
, char *, len
+ 1);
454 if (sc
->sh_prompt
== NULL
) {
458 memmove(sc
->sh_prompt
, buf
, len
);
459 sc
->sh_prompt_len
= len
;
460 sc
->sh_prompt
[len
] = '\0';
465 * sscr_matchprompt --
466 * Check to see if a line matches the prompt. Nul's indicate
467 * parts that can change, in both content and size.
470 sscr_matchprompt(sp
, lp
, line_len
, lenp
)
473 size_t line_len
, *lenp
;
480 if (line_len
< (prompt_len
= sc
->sh_prompt_len
))
483 for (pp
= sc
->sh_prompt
;
484 prompt_len
&& line_len
; --prompt_len
, --line_len
) {
486 for (; prompt_len
&& *pp
== '\0'; --prompt_len
, ++pp
);
489 for (; line_len
&& *lp
!= *pp
; --line_len
, ++lp
);
506 * End the pipe to a shell.
515 if ((sc
= sp
->script
) == NULL
)
518 /* Turn off the script flag. */
521 /* Close down the parent's file descriptors. */
522 if (sc
->sh_master
!= -1)
523 (void)close(sc
->sh_master
);
524 if (sc
->sh_slave
!= -1)
525 (void)close(sc
->sh_slave
);
527 /* This should have killed the child. */
528 rval
= proc_wait(sp
, (long)sc
->sh_pid
, "script-shell", 0);
531 FREE(sc
->sh_prompt
, sc
->sh_prompt_len
);
532 FREE(sc
, sizeof(SCRIPT
));