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.
15 #include <sys/types.h>
16 #include <sys/ioctl.h>
17 #include <sys/queue.h>
18 #include <sys/select.h>
22 #include <bitstring.h>
35 #elif defined HAVE_PTY_H
41 #include "../common/common.h"
44 #include "pathnames.h"
46 static void sscr_check(SCR *);
47 static int sscr_getprompt(SCR *);
48 static int sscr_init(SCR *);
49 static int sscr_insert(SCR *);
50 static int sscr_matchprompt(SCR *, char *, size_t, size_t *);
51 static int sscr_setprompt(SCR *, char *, size_t);
54 * ex_script -- : sc[ript][!] [file]
55 * Switch to script mode.
57 * PUBLIC: int ex_script(SCR *, EXCMD *);
60 ex_script(SCR *sp, EXCMD *cmdp)
62 /* Vi only command. */
63 if (!F_ISSET(sp, SC_VI)) {
65 "150|The script command is only available in vi mode");
69 /* Switch to the new file. */
70 if (cmdp->argc != 0 && ex_edit(sp, cmdp))
73 /* Create the shell, figure out the prompt. */
82 * Create a pty setup for a shell.
90 /* We're going to need a shell. */
91 if (opts_empty(sp, O_SHELL, 0))
94 MALLOC_RET(sp, sc, sizeof(SCRIPT));
97 sc->sh_prompt_len = 0;
100 * There are two different processes running through this code.
101 * They are the shell and the parent.
103 sc->sh_master = sc->sh_slave = -1;
105 if (tcgetattr(STDIN_FILENO, &sc->sh_term) == -1) {
106 msgq(sp, M_SYSERR, "tcgetattr");
111 * Turn off output postprocessing and echo.
113 sc->sh_term.c_oflag &= ~OPOST;
114 sc->sh_term.c_cflag &= ~(ECHO|ECHOE|ECHONL|ECHOK);
116 if (ioctl(STDIN_FILENO, TIOCGWINSZ, &sc->sh_win) == -1) {
117 msgq(sp, M_SYSERR, "tcgetattr");
121 if (openpty(&sc->sh_master,
122 &sc->sh_slave, sc->sh_name, &sc->sh_term, &sc->sh_win) == -1) {
123 msgq(sp, M_SYSERR, "openpty");
129 * Don't use vfork() here, because the signal semantics differ from
130 * implementation to implementation.
132 switch (sc->sh_pid = fork()) {
133 case -1: /* Error. */
134 msgq(sp, M_SYSERR, "fork");
135 err: if (sc->sh_master != -1)
136 (void)close(sc->sh_master);
137 if (sc->sh_slave != -1)
138 (void)close(sc->sh_slave);
140 case 0: /* Utility. */
143 * So that shells that do command line editing turn it off.
145 (void)setenv("TERM", "emacs", 1);
146 (void)setenv("TERMCAP", "emacs:", 1);
147 (void)setenv("EMACS", "t", 1);
152 * 4.4BSD allocates a controlling terminal using the TIOCSCTTY
153 * ioctl, not by opening a terminal device file. POSIX 1003.1
154 * doesn't define a portable way to do this. If TIOCSCTTY is
155 * not available, hope that the open does it.
157 (void)ioctl(sc->sh_slave, TIOCSCTTY, 0);
159 (void)close(sc->sh_master);
160 (void)dup2(sc->sh_slave, STDIN_FILENO);
161 (void)dup2(sc->sh_slave, STDOUT_FILENO);
162 (void)dup2(sc->sh_slave, STDERR_FILENO);
163 (void)close(sc->sh_slave);
165 /* Assumes that all shells have -i. */
166 sh_path = O_STR(sp, O_SHELL);
167 if ((sh = strrchr(sh_path, '/')) == NULL)
171 execl(sh_path, sh, "-i", NULL);
172 msgq_str(sp, M_SYSERR, sh_path, "execl: %s");
174 default: /* Parent. */
178 if (sscr_getprompt(sp))
181 F_SET(sp, SC_SCRIPT);
182 F_SET(sp->gp, G_SCRWIN);
188 * Eat lines printed by the shell until a line with no trailing
189 * carriage return comes; set the prompt from that line.
192 sscr_getprompt(SCR *sp)
196 char *endp, *p, *t, buf[1024];
211 /* Wait up to a second for characters to read. */
215 FD_SET(sc->sh_master, &fdset);
216 switch (select(sc->sh_master + 1, &fdset, NULL, NULL, &tv)) {
217 case -1: /* Error or interrupt. */
218 msgq(sp, M_SYSERR, "select");
220 case 0: /* Timeout */
221 msgq(sp, M_ERR, "Error: timed out");
223 case 1: /* Characters to read. */
227 /* Read the characters. */
228 more: len = sizeof(buf) - (endp - buf);
229 switch (nr = read(sc->sh_master, endp, len)) {
231 msgq(sp, M_ERR, "Error: shell: EOF");
233 case -1: /* Error or interrupt. */
234 msgq(sp, M_SYSERR, "shell");
241 /* If any complete lines, push them into the file. */
242 for (p = t = buf; p < endp; ++p) {
243 if (*p == '\r' || *p == '\n') {
244 if (CHAR2INT5(sp, exp->ibcw, t, p - t, wp, wlen))
246 if (db_last(sp, &lline) ||
247 db_append(sp, 0, lline, wp, wlen))
253 memmove(buf, t, endp - t);
254 endp = buf + (endp - t);
259 /* Wait up 1/10 of a second to make sure that we got it all. */
262 switch (select(sc->sh_master + 1, &fdset, NULL, NULL, &tv)) {
263 case -1: /* Error or interrupt. */
264 msgq(sp, M_SYSERR, "select");
266 case 0: /* Timeout */
268 case 1: /* Characters to read. */
272 /* Timed out, so theoretically we have a prompt. */
276 /* Append the line into the file. */
277 if (CHAR2INT5(sp, exp->ibcw, buf, llen, wp, wlen))
279 if (db_last(sp, &lline) || db_append(sp, 0, lline, wp, wlen)) {
281 conv_err: msgq(sp, M_ERR, "323|Invalid input. Truncated.");
282 prompterr: sscr_end(sp);
286 return (sscr_setprompt(sp, buf, llen));
291 * Take a line and hand it off to the shell.
293 * PUBLIC: int sscr_exec(SCR *, recno_t);
296 sscr_exec(SCR *sp, recno_t lno)
300 size_t blen, len, last_len, tlen;
301 int isempty, matchprompt, nw, rval;
306 /* If there's a prompt on the last line, append the command. */
307 if (db_last(sp, &last_lno))
309 if (db_get(sp, last_lno, DBG_FATAL, &wp, &wlen))
311 INT2CHAR(sp, wp, wlen, p, last_len);
312 if (sscr_matchprompt(sp, p, last_len, &tlen) && tlen == 0) {
314 GET_SPACE_RETC(sp, bp, blen, last_len + 128);
315 memmove(bp, p, last_len);
319 /* Get something to execute. */
320 if (db_eget(sp, lno, &wp, &wlen, &isempty)) {
326 /* Empty lines aren't interesting. */
329 INT2CHAR(sp, wp, wlen, p, len);
331 /* Delete any prompt. */
332 if (sscr_matchprompt(sp, p, len, &tlen)) {
334 empty: msgq(sp, M_BERR, "151|No command to execute");
341 /* Push the line to the shell. */
343 if ((nw = write(sc->sh_master, p, len)) != len)
346 if (write(sc->sh_master, "\n", 1) != 1) {
349 msgq(sp, M_SYSERR, "shell");
354 ADD_SPACE_RETC(sp, bp, blen, last_len + len);
355 memmove(bp + last_len, p, len);
356 CHAR2INT(sp, bp, last_len + len, wp, wlen);
357 if (db_set(sp, last_lno, wp, wlen))
361 FREE_SPACE(sp, bp, blen);
367 * Read any waiting shell input.
369 * PUBLIC: int sscr_input(SCR *);
386 /* Set up the input mask. */
387 TAILQ_FOREACH(sp, gp->dq, 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;
394 /* Check for input. */
395 switch (select(maxfd + 1, &rdfd, NULL, NULL, &poll)) {
397 msgq(sp, M_SYSERR, "select");
405 /* Read the input. */
406 TAILQ_FOREACH(sp, gp->dq, q)
407 if (F_ISSET(sp, SC_SCRIPT) &&
408 FD_ISSET(sp->script->sh_master, &rdfd) &&
416 * Take a line from the shell and insert it into the file.
427 size_t blen, len, tlen;
436 /* Find out where the end of the file is. */
437 if (db_last(sp, &lno))
441 GET_SPACE_RETC(sp, bp, blen, MINREAD);
444 /* Read the characters. */
447 more: switch (nr = read(sc->sh_master, endp, MINREAD)) {
448 case 0: /* EOF; shell just exited. */
452 case -1: /* Error or interrupt. */
453 msgq(sp, M_SYSERR, "shell");
460 /* Append the lines into the file. */
461 for (p = t = bp; p < endp; ++p) {
462 if (*p == '\r' || *p == '\n') {
464 if (CHAR2INT5(sp, exp->ibcw, t, len, wp, wlen))
466 if (db_append(sp, 1, lno++, wp, wlen))
474 * If the last thing from the shell isn't another prompt, wait
475 * up to 1/10 of a second for more stuff to show up, so that
476 * we don't break the output into two separate lines. Don't
477 * want to hang indefinitely because some program is hanging,
478 * confused the shell, or whatever.
480 if (!sscr_matchprompt(sp, t, len, &tlen) || tlen != 0) {
484 FD_SET(sc->sh_master, &rdfd);
485 if (select(sc->sh_master + 1,
486 &rdfd, NULL, NULL, &tv) == 1) {
492 if (sscr_setprompt(sp, t, len))
494 if (CHAR2INT5(sp, exp->ibcw, t, len, wp, wlen))
496 if (db_append(sp, 1, lno++, wp, wlen))
500 /* The cursor moves to EOF. */
502 sp->cno = wlen ? wlen - 1 : 0;
503 rval = vs_refresh(sp, 1);
506 conv_err: msgq(sp, M_ERR, "323|Invalid input. Truncated.");
508 ret: FREE_SPACE(sp, bp, blen);
515 * Set the prompt to the last line we got from the shell.
519 sscr_setprompt(SCR *sp, char *buf, size_t len)
525 MALLOC(sp, sc->sh_prompt, len + 1);
526 if (sc->sh_prompt == NULL) {
530 memmove(sc->sh_prompt, buf, len);
531 sc->sh_prompt_len = len;
532 sc->sh_prompt[len] = '\0';
537 * sscr_matchprompt --
538 * Check to see if a line matches the prompt. Nul's indicate
539 * parts that can change, in both content and size.
542 sscr_matchprompt(SCR *sp, char *lp, size_t line_len, size_t *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(SCR *);
584 if ((sc = sp->script) == NULL)
587 /* Turn off the script flags. */
588 F_CLR(sp, SC_SCRIPT);
591 /* Close down the parent's file descriptors. */
592 if (sc->sh_master != -1)
593 (void)close(sc->sh_master);
594 if (sc->sh_slave != -1)
595 (void)close(sc->sh_slave);
597 /* This should have killed the child. */
598 (void)proc_wait(sp, (long)sc->sh_pid, "script-shell", 0, 0);
610 * Set/clear the global scripting bit.
618 TAILQ_FOREACH(sp, gp->dq, q)
619 if (F_ISSET(sp, SC_SCRIPT)) {