1 /* vi: set sw=4 ts=4: */
3 * script implementation for busybox
5 * pascal.bellard@ads-lu.com
7 * Based on code from util-linux v 2.12r
9 * The Regents of the University of California. All rights reserved.
11 * Licensed under GPLv2 or later, see file License in this tarball for details.
16 static smallint fd_count = 2;
18 static void handle_sigchld(int sig ATTRIBUTE_UNUSED)
23 int script_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
24 int script_main(int argc ATTRIBUTE_UNUSED, char **argv)
29 int attr_ok; /* NB: 0: ok */
32 char pty_line[GETPTY_BUFSIZE];
33 struct termios tt, rtt;
35 const char *fname = "typescript";
37 char shell_opt[] = "-i";
38 char *shell_arg = NULL;
40 #if ENABLE_GETOPT_LONG
41 static const char getopt_longopts[] ALIGN1 =
42 "append\0" No_argument "a"
43 "command\0" Required_argument "c"
44 "flush\0" No_argument "f"
45 "quiet\0" No_argument "q"
48 applet_long_options = getopt_longopts;
50 opt_complementary = "?1"; /* max one arg */
51 opt = getopt32(argv, "ac:fq", &shell_arg);
57 mode = O_CREAT|O_TRUNC|O_WRONLY;
59 mode = O_CREAT|O_APPEND|O_WRONLY;
64 if (!(opt & 8)) { /* not -q */
65 printf("Script started, file is %s\n", fname);
67 shell = getenv("SHELL");
69 shell = DEFAULT_SHELL;
72 pty = getpty(pty_line);
74 bb_perror_msg_and_die("can't get pty");
77 /* get current stdin's tty params */
78 attr_ok = tcgetattr(0, &tt);
79 winsz_ok = ioctl(0, TIOCGWINSZ, (char *)&win);
84 tcsetattr(0, TCSAFLUSH, &rtt);
86 /* "script" from util-linux exits when child exits,
87 * we wouldn't wait for EOF from slave pty
88 * (output may be produced by grandchildren of child) */
89 signal(SIGCHLD, handle_sigchld);
91 /* TODO: SIGWINCH? pass window size changes down to slave? */
95 bb_perror_msg_and_die("vfork");
100 #define buf bb_common_bufsiz1
101 struct pollfd pfd[2];
102 struct pollfd *ppfd = pfd;
103 int outfd, count, loop;
105 outfd = xopen(fname, mode);
107 pfd[0].events = POLLIN;
109 pfd[1].events = POLLIN;
110 ndelay_on(pty); /* this descriptor is not shared, can do this */
111 /* ndelay_on(0); - NO, stdin can be shared! Pity :( */
113 /* copy stdin to pty master input,
114 * copy pty master output to stdout and file */
115 /* TODO: don't use full_write's, use proper write buffering */
117 /* not safe_poll! we want SIGCHLD to EINTR poll */
118 poll(ppfd, fd_count, -1);
119 if (pfd[0].revents) {
120 count = safe_read(0, buf, sizeof(buf));
122 /* err/eof: don't read anymore */
127 full_write(pty, buf, count);
130 if (pfd[1].revents) {
132 count = safe_read(pty, buf, sizeof(buf));
133 if (count <= 0 && errno != EAGAIN) {
134 /* err/eof: don't read anymore */
139 full_write(1, buf, count);
140 full_write(outfd, buf, count);
141 if (opt & 4) { /* -f */
147 /* If loop was exited because SIGCHLD handler set fd_count to 0,
148 * there still can be some buffered output. But not loop forever:
149 * we won't pump orphaned grandchildren's output indefinitely.
150 * Testcase: running this in script:
151 * exec dd if=/dev/zero bs=1M count=1
152 * must have "1+0 records in, 1+0 records out" captured too.
153 * (util-linux's script doesn't do this. buggy :) */
155 /* pty is in O_NONBLOCK mode, we exit as soon as buffer is empty */
156 while (--loop && (count = safe_read(pty, buf, sizeof(buf))) > 0) {
157 full_write(1, buf, count);
158 full_write(outfd, buf, count);
162 tcsetattr(0, TCSAFLUSH, &tt);
163 if (!(opt & 8)) /* not -q */
164 printf("Script done, file is %s\n", fname);
168 /* child: make pty slave to be input, output, error; run shell */
169 close(pty); /* close pty master */
170 /* open pty slave to fd 0,1,2 */
172 xopen(pty_line, O_RDWR); /* uses fd 0 */
175 /* copy our original stdin tty's parameters to pty */
177 tcsetattr(0, TCSAFLUSH, &tt);
179 ioctl(0, TIOCSWINSZ, (char *)&win);
180 /* set pty as a controlling tty */
182 ioctl(0, TIOCSCTTY, 0 /* 0: don't forcibly steal */);
184 /* signal(SIGCHLD, SIG_DFL); - exec does this for us */
185 execl(shell, shell, shell_opt, shell_arg, NULL);
186 bb_simple_perror_msg_and_die(shell);