123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240 |
- #include "libbb.h"
- #include "common_bufsiz.h"
- int script_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
- int script_main(int argc UNUSED_PARAM, char **argv)
- {
- int opt;
- int mode;
- int child_pid;
- int attr_ok;
- int winsz_ok;
- int pty;
- char pty_line[GETPTY_BUFSIZE];
- struct termios tt, rtt;
- struct winsize win;
- FILE *timing_fp;
- const char *str_t = NULL;
- const char *fname = "typescript";
- const char *shell;
- char shell_opt[] = "-i";
- char *shell_arg = NULL;
- enum {
- OPT_a = (1 << 0),
- OPT_c = (1 << 1),
- OPT_f = (1 << 2),
- OPT_q = (1 << 3),
- OPT_t = (1 << 4),
- };
- #if ENABLE_LONG_OPTS
- static const char script_longopts[] ALIGN1 =
- "append\0" No_argument "a"
- "command\0" Required_argument "c"
- "flush\0" No_argument "f"
- "quiet\0" No_argument "q"
- "timing\0" Optional_argument "t"
- ;
- #endif
- opt = getopt32long(argv, "^" "ac:fqt::" "\0" "?1",
- script_longopts,
- &shell_arg, &str_t
- );
-
- argv += optind;
- if (argv[0]) {
- fname = argv[0];
- }
- mode = O_CREAT|O_TRUNC|O_WRONLY;
- if (opt & OPT_a) {
- mode = O_CREAT|O_APPEND|O_WRONLY;
- }
- if (opt & OPT_c) {
- shell_opt[1] = 'c';
- }
- if (!(opt & OPT_q)) {
- printf("Script started, file is %s\n", fname);
- }
- timing_fp = stderr;
- if (str_t) {
- timing_fp = xfopen_for_write(str_t);
- }
- shell = get_shell_name();
-
- bb_sanitize_stdio();
- pty = xgetpty(pty_line);
-
- attr_ok = tcgetattr(0, &tt);
- winsz_ok = ioctl(0, TIOCGWINSZ, (char *)&win);
- rtt = tt;
- cfmakeraw(&rtt);
- rtt.c_lflag &= ~ECHO;
- tcsetattr(0, TCSAFLUSH, &rtt);
-
- signal(SIGCHLD, record_signo);
-
- child_pid = xvfork();
- if (child_pid) {
-
- struct pollfd pfd[2];
- int outfd, count, loop;
- double oldtime = time(NULL);
- smallint fd_count = 2;
- #define buf bb_common_bufsiz1
- setup_common_bufsiz();
- outfd = xopen(fname, mode);
- pfd[0].fd = pty;
- pfd[0].events = POLLIN;
- pfd[1].fd = STDIN_FILENO;
- pfd[1].events = POLLIN;
- ndelay_on(pty);
-
-
-
- while (fd_count && !bb_got_signal) {
-
- if (poll(pfd, fd_count, -1) < 0 && errno != EINTR) {
-
- break;
- }
- if (pfd[0].revents) {
- errno = 0;
- count = safe_read(pty, buf, COMMON_BUFSIZE);
- if (count <= 0 && errno != EAGAIN) {
-
- goto restore;
- }
- if (count > 0) {
- if (opt & OPT_t) {
- struct timeval tv;
- double newtime;
- gettimeofday(&tv, NULL);
- newtime = tv.tv_sec + (double) tv.tv_usec / 1000000;
- fprintf(timing_fp, "%f %u\n", newtime - oldtime, count);
- oldtime = newtime;
- }
- full_write(STDOUT_FILENO, buf, count);
- full_write(outfd, buf, count);
-
-
-
-
- }
- }
- if (pfd[1].revents) {
- count = safe_read(STDIN_FILENO, buf, COMMON_BUFSIZE);
- if (count <= 0) {
-
- pfd[1].revents = 0;
- fd_count--;
- } else {
- full_write(pty, buf, count);
- }
- }
- }
-
- loop = 999;
-
- while (--loop && (count = safe_read(pty, buf, COMMON_BUFSIZE)) > 0) {
- full_write(STDOUT_FILENO, buf, count);
- full_write(outfd, buf, count);
- }
- restore:
- if (attr_ok == 0)
- tcsetattr(0, TCSAFLUSH, &tt);
- if (!(opt & OPT_q))
- printf("Script done, file is %s\n", fname);
- return EXIT_SUCCESS;
- }
-
- close(pty);
-
- close(0);
- xopen(pty_line, O_RDWR);
- xdup2(0, 1);
- xdup2(0, 2);
-
- if (attr_ok == 0)
- tcsetattr(0, TCSAFLUSH, &tt);
- if (winsz_ok == 0)
- ioctl(0, TIOCSWINSZ, (char *)&win);
-
- setsid();
- ioctl(0, TIOCSCTTY, 0 );
-
-
- execl(shell, shell, shell_opt, shell_arg, (char *) NULL);
- bb_simple_perror_msg_and_die(shell);
- }
|