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.
15 int script_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
16 int script_main(int argc UNUSED_PARAM, char **argv)
21 int attr_ok; /* NB: 0: ok */
24 char pty_line[GETPTY_BUFSIZE];
25 struct termios tt, rtt;
27 const char *fname = "typescript";
29 char shell_opt[] = "-i";
30 char *shell_arg = NULL;
40 static const char getopt_longopts[] ALIGN1 =
41 "append\0" No_argument "a"
42 "command\0" Required_argument "c"
43 "flush\0" No_argument "f"
44 "quiet\0" No_argument "q"
45 IF_SCRIPTREPLAY("timing\0" No_argument "t")
48 applet_long_options = getopt_longopts;
51 opt_complementary = "?1"; /* max one arg */
52 opt = getopt32(argv, "ac:fq" IF_SCRIPTREPLAY("t") , &shell_arg);
58 mode = O_CREAT|O_TRUNC|O_WRONLY;
60 mode = O_CREAT|O_APPEND|O_WRONLY;
66 printf("Script started, file is %s\n", fname);
68 shell = getenv("SHELL");
70 shell = DEFAULT_SHELL;
73 pty = xgetpty(pty_line);
75 /* get current stdin's tty params */
76 attr_ok = tcgetattr(0, &tt);
77 winsz_ok = ioctl(0, TIOCGWINSZ, (char *)&win);
82 tcsetattr(0, TCSAFLUSH, &rtt);
84 /* "script" from util-linux exits when child exits,
85 * we wouldn't wait for EOF from slave pty
86 * (output may be produced by grandchildren of child) */
87 signal(SIGCHLD, record_signo);
89 /* TODO: SIGWINCH? pass window size changes down to slave? */
95 #define buf bb_common_bufsiz1
97 int outfd, count, loop;
98 double oldtime = ENABLE_SCRIPTREPLAY ? time(NULL) : 0;
99 smallint fd_count = 2;
101 outfd = xopen(fname, mode);
103 pfd[0].events = POLLIN;
104 pfd[1].fd = STDIN_FILENO;
105 pfd[1].events = POLLIN;
106 ndelay_on(pty); /* this descriptor is not shared, can do this */
107 /* ndelay_on(STDIN_FILENO); - NO, stdin can be shared! Pity :( */
109 /* copy stdin to pty master input,
110 * copy pty master output to stdout and file */
111 /* TODO: don't use full_write's, use proper write buffering */
112 while (fd_count && !bb_got_signal) {
113 /* not safe_poll! we want SIGCHLD to EINTR poll */
114 if (poll(pfd, fd_count, -1) < 0 && errno != EINTR) {
115 /* If child exits too quickly, we may get EIO:
116 * for example, try "script -c true" */
119 if (pfd[0].revents) {
121 count = safe_read(pty, buf, sizeof(buf));
122 if (count <= 0 && errno != EAGAIN) {
123 /* err/eof from pty: exit */
127 if (ENABLE_SCRIPTREPLAY && (opt & OPT_t)) {
131 gettimeofday(&tv, NULL);
132 newtime = tv.tv_sec + (double) tv.tv_usec / 1000000;
133 fprintf(stderr, "%f %u\n", newtime - oldtime, count);
136 full_write(STDOUT_FILENO, buf, count);
137 full_write(outfd, buf, count);
143 if (pfd[1].revents) {
144 count = safe_read(STDIN_FILENO, buf, sizeof(buf));
146 /* err/eof from stdin: don't read stdin anymore */
150 full_write(pty, buf, count);
154 /* If loop was exited because SIGCHLD handler set bb_got_signal,
155 * there still can be some buffered output. But dont loop forever:
156 * we won't pump orphaned grandchildren's output indefinitely.
157 * Testcase: running this in script:
158 * exec dd if=/dev/zero bs=1M count=1
159 * must have "1+0 records in, 1+0 records out" captured too.
160 * (util-linux's script doesn't do this. buggy :) */
162 /* pty is in O_NONBLOCK mode, we exit as soon as buffer is empty */
163 while (--loop && (count = safe_read(pty, buf, sizeof(buf))) > 0) {
164 full_write(STDOUT_FILENO, buf, count);
165 full_write(outfd, buf, count);
169 tcsetattr(0, TCSAFLUSH, &tt);
171 printf("Script done, file is %s\n", fname);
175 /* child: make pty slave to be input, output, error; run shell */
176 close(pty); /* close pty master */
177 /* open pty slave to fd 0,1,2 */
179 xopen(pty_line, O_RDWR); /* uses fd 0 */
182 /* copy our original stdin tty's parameters to pty */
184 tcsetattr(0, TCSAFLUSH, &tt);
186 ioctl(0, TIOCSWINSZ, (char *)&win);
187 /* set pty as a controlling tty */
189 ioctl(0, TIOCSCTTY, 0 /* 0: don't forcibly steal */);
191 /* Non-ignored signals revert to SIG_DFL on exec anyway */
192 /*signal(SIGCHLD, SIG_DFL);*/
193 execl(shell, shell, shell_opt, shell_arg, (char *) NULL);
194 bb_simple_perror_msg_and_die(shell);