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 source tree.
14 //usage:#define script_trivial_usage
15 //usage: "[-afq" IF_SCRIPTREPLAY("t") "] [-c PROG] [OUTFILE]"
16 //usage:#define script_full_usage "\n\n"
17 //usage: " -a Append output"
18 //usage: "\n -c PROG Run PROG, not shell"
19 //usage: "\n -f Flush output after each write"
20 //usage: "\n -q Quiet"
21 //usage: IF_SCRIPTREPLAY(
22 //usage: "\n -t Send timing to stderr"
27 int script_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
28 int script_main(int argc UNUSED_PARAM, char **argv)
33 int attr_ok; /* NB: 0: ok */
36 char pty_line[GETPTY_BUFSIZE];
37 struct termios tt, rtt;
39 const char *fname = "typescript";
41 char shell_opt[] = "-i";
42 char *shell_arg = NULL;
52 static const char getopt_longopts[] ALIGN1 =
53 "append\0" No_argument "a"
54 "command\0" Required_argument "c"
55 "flush\0" No_argument "f"
56 "quiet\0" No_argument "q"
57 IF_SCRIPTREPLAY("timing\0" No_argument "t")
60 applet_long_options = getopt_longopts;
63 opt_complementary = "?1"; /* max one arg */
64 opt = getopt32(argv, "ac:fq" IF_SCRIPTREPLAY("t") , &shell_arg);
70 mode = O_CREAT|O_TRUNC|O_WRONLY;
72 mode = O_CREAT|O_APPEND|O_WRONLY;
78 printf("Script started, file is %s\n", fname);
81 shell = get_shell_name();
83 /* Some people run "script ... 0>&-".
84 * Our code assumes that STDIN_FILENO != pty.
85 * Ensure STDIN_FILENO is not closed:
89 pty = xgetpty(pty_line);
91 /* get current stdin's tty params */
92 attr_ok = tcgetattr(0, &tt);
93 winsz_ok = ioctl(0, TIOCGWINSZ, (char *)&win);
98 tcsetattr(0, TCSAFLUSH, &rtt);
100 /* "script" from util-linux exits when child exits,
101 * we wouldn't wait for EOF from slave pty
102 * (output may be produced by grandchildren of child) */
103 signal(SIGCHLD, record_signo);
105 /* TODO: SIGWINCH? pass window size changes down to slave? */
107 child_pid = xvfork();
111 #define buf bb_common_bufsiz1
112 struct pollfd pfd[2];
113 int outfd, count, loop;
114 double oldtime = ENABLE_SCRIPTREPLAY ? time(NULL) : 0;
115 smallint fd_count = 2;
117 outfd = xopen(fname, mode);
119 pfd[0].events = POLLIN;
120 pfd[1].fd = STDIN_FILENO;
121 pfd[1].events = POLLIN;
122 ndelay_on(pty); /* this descriptor is not shared, can do this */
123 /* ndelay_on(STDIN_FILENO); - NO, stdin can be shared! Pity :( */
125 /* copy stdin to pty master input,
126 * copy pty master output to stdout and file */
127 /* TODO: don't use full_write's, use proper write buffering */
128 while (fd_count && !bb_got_signal) {
129 /* not safe_poll! we want SIGCHLD to EINTR poll */
130 if (poll(pfd, fd_count, -1) < 0 && errno != EINTR) {
131 /* If child exits too quickly, we may get EIO:
132 * for example, try "script -c true" */
135 if (pfd[0].revents) {
137 count = safe_read(pty, buf, sizeof(buf));
138 if (count <= 0 && errno != EAGAIN) {
139 /* err/eof from pty: exit */
143 if (ENABLE_SCRIPTREPLAY && (opt & OPT_t)) {
147 gettimeofday(&tv, NULL);
148 newtime = tv.tv_sec + (double) tv.tv_usec / 1000000;
149 fprintf(stderr, "%f %u\n", newtime - oldtime, count);
152 full_write(STDOUT_FILENO, buf, count);
153 full_write(outfd, buf, count);
159 if (pfd[1].revents) {
160 count = safe_read(STDIN_FILENO, buf, sizeof(buf));
162 /* err/eof from stdin: don't read stdin anymore */
166 full_write(pty, buf, count);
170 /* If loop was exited because SIGCHLD handler set bb_got_signal,
171 * there still can be some buffered output. But dont loop forever:
172 * we won't pump orphaned grandchildren's output indefinitely.
173 * Testcase: running this in script:
174 * exec dd if=/dev/zero bs=1M count=1
175 * must have "1+0 records in, 1+0 records out" captured too.
176 * (util-linux's script doesn't do this. buggy :) */
178 /* pty is in O_NONBLOCK mode, we exit as soon as buffer is empty */
179 while (--loop && (count = safe_read(pty, buf, sizeof(buf))) > 0) {
180 full_write(STDOUT_FILENO, buf, count);
181 full_write(outfd, buf, count);
185 tcsetattr(0, TCSAFLUSH, &tt);
187 printf("Script done, file is %s\n", fname);
191 /* child: make pty slave to be input, output, error; run shell */
192 close(pty); /* close pty master */
193 /* open pty slave to fd 0,1,2 */
195 xopen(pty_line, O_RDWR); /* uses fd 0 */
198 /* copy our original stdin tty's parameters to pty */
200 tcsetattr(0, TCSAFLUSH, &tt);
202 ioctl(0, TIOCSWINSZ, (char *)&win);
203 /* set pty as a controlling tty */
205 ioctl(0, TIOCSCTTY, 0 /* 0: don't forcibly steal */);
207 /* Non-ignored signals revert to SIG_DFL on exec anyway */
208 /*signal(SIGCHLD, SIG_DFL);*/
209 execl(shell, shell, shell_opt, shell_arg, (char *) NULL);
210 bb_simple_perror_msg_and_die(shell);