void tty_stop_tty(struct tty *tty) { struct winsize ws; /* * Be flexible about error handling and try not kill the server just * because the fd is invalid. Things like ssh -t can easily leave us * with a dead tty. */ if (ioctl(tty->fd, TIOCGWINSZ, &ws) == -1) return; if (tcsetattr(tty->fd, TCSANOW, &tty->tio) == -1) return; tty_raw(tty, tty_term_string2(tty->term, TTYC_CSR, 0, ws.ws_row - 1)); tty_raw(tty, tty_term_string(tty->term, TTYC_RMACS)); tty_raw(tty, tty_term_string(tty->term, TTYC_SGR0)); tty_raw(tty, tty_term_string(tty->term, TTYC_RMKX)); tty_raw(tty, tty_term_string(tty->term, TTYC_RMCUP)); tty_raw(tty, tty_term_string(tty->term, TTYC_CLEAR)); tty_raw(tty, tty_term_string(tty->term, TTYC_CNORM)); if (tty_term_has(tty->term, TTYC_KMOUS)) tty_raw(tty, "\033[?1000l"); }
int debutTerminalSansR() { if (tty_raw() != 0) return 0; atexit(tty_reset); return 1; }
void init_tty(void) /* Get terminal capabilities and set the tty to "editor" mode. */ { char *term; static char termbuf[1024]; char *tp; if ((term= getenv("TERM")) == nil || tgetent(termbuf, term) != 1) { fprintf(stderr, "part: Can't get terminal capabilities\n"); exit(1); } if (tgetstr("cd", (tp= t_cd, &tp)) == nil || tgetstr("cm", (tp= t_cm, &tp)) == nil) { fprintf(stderr, "part: This terminal is too dumb\n"); exit(1); } t_li= tgetnum("li"); t_co= tgetnum("co"); (void) tgetstr("so", (tp= t_so, &tp)); (void) tgetstr("se", (tp= t_se, &tp)); (void) tgetstr("md", (tp= t_md, &tp)); (void) tgetstr("me", (tp= t_me, &tp)); save_ttyflags(); tty_raw(); }
/* * Read a line from the user. Call our buf_args() function to * break it into an argv-style array, and call userfunc() to * process the arguments. */ int prompt_read(char *prompt, int (*userfunc)(int, char **)) { int n; char c, *ptr; tty_reset(STDIN_FILENO); /* allow user's editing chars */ n = strlen(prompt); if (write(STDOUT_FILENO, prompt, n) != n) err_sys("write error"); ptr = cmdargs; for ( ; ; ) { if ((n = read(STDIN_FILENO, &c, 1)) < 0) err_sys("read error"); else if (n == 0) break; if (c == '\n') break; if (ptr < &cmdargs[MAXLINE-2]) *ptr++ = c; } *ptr = 0; /* null terminate */ tty_raw(STDIN_FILENO); /* reset tty mode to raw */ /* * Return whatever userfunc() returns. */ return(buf_args(cmdargs, userfunc)); }
/* ** funkey_keys(test_list, status, ch) ** ** Test function keys */ static void funkey_keys( TestList * t, int *state, int *ch) { char keybuf[256]; if (keypad_xmit) { tc_putp(keypad_xmit); } keys_tested(1, 1, hex_out); /* also clears screen */ keybuf[0] = '\0'; end_state = 0; if (scan_mode) { fkmax = scan_max; } tty_raw(0, char_mask); while (end_state != 'd') { read_key(keybuf, sizeof(keybuf)); fresh_line(); if (found_match(keybuf, hex_out, 0)) { continue; } if (found_exit(keybuf, hex_out, 0)) { break; } } if (keypad_local) { tc_putp(keypad_local); } keys_tested(0, 0, hex_out); ptext("Function key test "); generic_done_message(t, state, ch); }
/* Called when window is resized. */ static void sigwinch() { tty_restore(); tty_raw(); action(NULL); }
static int resizetty(void) { /* * \e7 Save current state (cursor coordinates, attributes, * character sets pointed at by G0, G1). * \e[r Set scrolling region; parameters are top and bottom row. * \e[32766E Move cursor down 32766 (INT16_MAX - 1) rows. * \e[32766C Move cursor right 32766 columns. * \e[6n Report cursor position. * \e8 Restore state most recently saved by \e7. */ static const char *getpos = "\e7\e[r\e[32766E\e[32766C\e[6n\e8"; char retstr[32]; int row, col; size_t pos; ssize_t rc; struct winsize ws; struct termios saved_attributes; int saved_fl; if (!isatty(STDIN_FILENO)) errx(EXIT_FAILURE, _("stdin does not refer to a terminal")); tty_raw(&saved_attributes, &saved_fl); if (write_all(STDIN_FILENO, getpos, strlen(getpos)) < 0) { warn(_("write failed")); tty_restore(&saved_attributes, &saved_fl); return 1; } for (pos = 0; pos < sizeof(retstr) - 1;) { if (0 == select_wait()) break; if ((rc = read(STDIN_FILENO, retstr + pos, sizeof(retstr) - 1 - pos)) < 0) { if (errno == EINTR) continue; warn(_("read failed")); tty_restore(&saved_attributes, &saved_fl); return 1; } pos += rc; if (retstr[pos - 1] == 'R') break; } retstr[pos] = 0; tty_restore(&saved_attributes, &saved_fl); rc = sscanf(retstr, "\033[%d;%dR", &row, &col); if (rc != 2) { warnx(_("invalid cursor position: %s"), retstr); return 1; } memset(&ws, 0, sizeof(struct winsize)); ioctl(STDIN_FILENO, TIOCGWINSZ, &ws); ws.ws_row = row; ws.ws_col = col; ioctl(STDIN_FILENO, TIOCSWINSZ, &ws); return 0; }
/* Called when window is resized. */ static void sigwinch(int dummy) { UNUSED(dummy); tty_restore(); tty_raw(); action(NULL); }
int main(int argc, char *argv[]) { int c, remfd, debug; char args[MAXLINE]; args[0] = 0; /* build arg list for conn server here */ opterr = 0; /* don't want getopt() writing to stderr */ while ( (c = getopt(argc, argv, "des:o")) != EOF) { switch (c) { case 'd': /* debug */ debug = 1; strcat(args, "-d "); break; case 'e': /* even parity */ strcat(args, "-e "); break; case 'o': /* odd parity */ strcat(args, "-o "); break; case 's': /* speed */ strcat(args, "-s "); strcat(args, optarg); strcat(args, " "); break; case '?': usage("unrecognized option"); } } if (optind < argc) strcat(args, argv[optind]); /* name of host to call */ else usage("missing <hostname> to call"); if ( (remfd = call(args)) < 0) /* place the call */ exit(1); /* call() prints reason for failure */ printf("Connected\n"); if (tty_raw(STDIN_FILENO) < 0) /* user's tty to raw mode */ err_sys("tty_raw error"); if (atexit(tty_atexit) < 0) /* reset user's tty on exit */ err_sys("atexit error"); loop(remfd); /* and do it */ printf("Disconnected\n\r"); exit(0); }
/* ** funkey_meta(test_list, status, ch) ** ** Test meta key (km) (smm) (rmm) */ static void funkey_meta( TestList * t, int *state, int *ch) { if (has_meta_key) { int i, j, k, len; char outbuf[256]; put_crlf(); if (char_mask != ALLOW_PARITY) { if (tty_meta_prep()) { ptext("\nHit any key to continue > "); (void) wait_here(); put_crlf(); } } ptext("Begin meta key test. (km) (smm) (rmm) Hit any key"); ptext(" with the meta key. The character will be"); ptext(" displayed in hex. If the meta key is working"); ptext(" then the most significant bit will be set. Type"); ptextln(" 'end' to exit."); tty_raw(1, ALLOW_PARITY); tc_putp(meta_on); for (i = j = k = len = 0; i != 'e' || j != 'n' || k != 'd';) { i = j; j = k; k = getchp(ALLOW_PARITY); if (k == EOF) { break; } if ((len += 3) >= columns) { put_crlf(); len = 3; } sprintf(outbuf, "%02X ", k); put_str(outbuf); k &= STRIP_PARITY; } tc_putp(meta_off); put_crlf(); tty_set(); put_crlf(); } else { ptext("(km) Has-meta-key is not set. "); } generic_done_message(t, state, ch); }
int recinput(char *arqinput, char *argv[]) { int fdm, c; pid_t pid; struct termios orig_termios; struct winsize size; if (! isatty(0) || ! isatty(1)) { msg("stdin and stdout must be a terminal"); return ERRO; } input = criarq("", arqinput, ""); if (input == NULL) return ERRO; if (tty_save(STDIN_FILENO) == ERRO) return ERRO; pid = pty_fork2(&fdm, slave_name); if (pid < 0) { return ERRO; } else if (pid == 0) { /* child */ if (execv(argv[0], argv) < 0) { msg("can't execv: %s"); return ERRO; } } if (tty_raw(STDIN_FILENO) < 0) /* user's tty to raw mode */ { msg("tty_raw error"); return ERRO; } c = loop1(fdm); /* copies stdin -> ptym, ptym -> stdout */ waitpid(pid, NULL, 0); fclose(input); close(fdm); tty_copy(STDIN_FILENO); return c; }
int main() { /* check that input is from a tty */ if (! isatty(ttyfd)) fatal("not on a tty"); /* store current tty settings in orig_termios */ if (tcgetattr(ttyfd,&orig_termios) < 0) fatal("can't get tty settings"); /* register the tty reset with the exit handler */ if (atexit(tty_atexit) != 0) fatal("atexit: can't register tty reset"); tty_raw(); /* put tty in raw mode */ screenio(); /* run application code */ return 0; /* tty_atexit will restore terminal */ }
static void *watch_tty(struct watch_tty_data *wtd) { struct tcp_spec ts; char buf[256]; int nr; if (wtd->input_mode == INPUT_MODE_RAW) tty_raw(0, 1, 0); while ((nr = read(0, buf, sizeof(buf)))) { if (buf[0] == 29) /* ^] */ break; if (wtd->input_mode == INPUT_MODE_LINEECHO || wtd->input_mode == INPUT_MODE_LINEECHOR) { if (nr >= 3 && buf[0] == '^' && buf[1] == ']' && buf[2] == '\n') break; if (wtd->input_mode == INPUT_MODE_LINEECHOR && nr < sizeof(buf) && buf[nr - 1] == '\n') { buf[nr - 1] = '\r'; buf[nr++] = '\n'; } } memset(&ts, 0, sizeof(ts)); ts.saddr = wtd->ci->src_addr; ts.daddr = wtd->ci->dst_addr; ts.sport = wtd->ci->src_port; ts.dport = wtd->ci->dst_port; ts.src_mac = wtd->src_fake_mac; ts.dst_mac = wtd->ci->dst.src_mac; ts.seq = wtd->ci->dst.next_d_seq; ts.ack_seq = wtd->ci->dst.next_seq; ts.window = wtd->ci->src.window ? wtd->ci->src.window : htons(242); ts.id = htons(ntohs(wtd->ci->src.id) + 1); ts.ack = 1; ts.psh = 1; ts.rst = 0; ts.data = buf; ts.data_len = nr; send_tcp_packet(&ts); } if (wtd->input_mode == INPUT_MODE_RAW) tty_reset(0); list_produce_done(&l_hijack_conn); return NULL; }
int main(int argc, char ** argv){ int r; char c; printf("Type [s,n,p,e]: "); fflush(stdout); if(!isatty(ttyfd)) fatal("not on a tty"); if(tcgetattr(ttyfd, &orig_termios) < 0) fatal("can't get tty settings"); if(atexit(tty_reset) != 0) fatal("atexit: can't register tty reset"); tty_raw(); if(argc < 2) fatal("Missing fifo file path"); mplayer_init(argv[1]); while(1){ r = read(0, &c, 1); switch(c){ case 's': mplayer_send(MPLAYER_START); break; case 'p': mplayer_send(MPLAYER_PREV); break; case 'n': mplayer_send(MPLAYER_NEXT); break; case 'e': exit(0); default: printf("Unknown command"); break; } } return 0; }
void tty_stop_tty(struct tty *tty) { struct winsize ws; int mode; if (!(tty->flags & TTY_STARTED)) return; tty->flags &= ~TTY_STARTED; bufferevent_disable(tty->event, EV_READ|EV_WRITE); /* * Be flexible about error handling and try not kill the server just * because the fd is invalid. Things like ssh -t can easily leave us * with a dead tty. */ if (ioctl(tty->fd, TIOCGWINSZ, &ws) == -1) return; if (tcsetattr(tty->fd, TCSANOW, &tty->tio) == -1) return; tty_raw(tty, tty_term_string2(tty->term, TTYC_CSR, 0, ws.ws_row - 1)); tty_raw(tty, tty_term_string(tty->term, TTYC_RMACS)); tty_raw(tty, tty_term_string(tty->term, TTYC_SGR0)); tty_raw(tty, tty_term_string(tty->term, TTYC_RMKX)); tty_raw(tty, tty_term_string(tty->term, TTYC_CLEAR)); tty_raw(tty, tty_term_string(tty->term, TTYC_CNORM)); if (tty_term_has(tty->term, TTYC_KMOUS)) tty_raw(tty, "\033[?1000l"); tty_raw(tty, tty_term_string(tty->term, TTYC_RMCUP)); if ((mode = fcntl(tty->fd, F_GETFL)) != -1) fcntl(tty->fd, F_SETFL, mode & ~O_NONBLOCK); }
int main(void) { int i; char c; if (signal(SIGINT, sig_catch) == SIG_ERR) /* catch signals */ err_sys("signal(SIGINT) error"); if (signal(SIGQUIT, sig_catch) == SIG_ERR) err_sys("signal(SIGQUIT) error"); if (signal(SIGTERM, sig_catch) == SIG_ERR) err_sys("signal(SIGTERM) error"); if (tty_raw(STDIN_FILENO) < 0) err_sys("tty_raw error"); printf("Enter raw mode characters, terminate with DELETE\n"); while ((i = read(STDIN_FILENO, &c, 1)) == 1) { if ((c &= 255) == 0177) /* 0177 = ASCII DELETE */ break; printf("%o\n", c); } if (tty_reset(STDIN_FILENO) < 0) err_sys("tty_reset error"); if (i <= 0) err_sys("read error"); if (tty_cbreak(STDIN_FILENO) < 0) err_sys("tty_cbreak error"); printf("\nEnter cbreak mode characters, terminate with SIGINT\n"); while ((i = read(STDIN_FILENO, &c, 1)) == 1) { c &= 255; printf("%o\n", c); } if (tty_reset(STDIN_FILENO) < 0) err_sys("tty_reset error"); if (i <= 0) err_sys("read error"); exit(0); }
int main(int argc, char *argv[]) { atexit(tty_atexit); int i; char c; Signal(SIGINT, sig_catch); Signal(SIGQUIT, sig_catch); Signal(SIGTERM, sig_catch); if(tty_raw(STDIN_FILENO) < 0) err_sys("tty_raw error"); printf("Enter raw mode characters, terminate with DELETE\n"); while ((i = read(STDIN_FILENO, &c, 1)) == 1) { if((c &= 255) == 0177) /* 0177 = ASCII DELETE*/ break; printf("%o\n", c ); } if(tty_reset(STDIN_FILENO) < 0) err_sys("tty_reset error"); if(i <= 0) err_sys("read error"); if(tty_cbreak(STDIN_FILENO) < 0) err_sys("tty_raw error"); printf("\n Enter cbreak mode characters, terminate with SIGINT\n"); while ( (i = read(STDIN_FILENO, &c, 1)) == 1) { c &= 255; printf("%o\n", c); } tty_reset(STDIN_FILENO); if( i < 0) err_sys("read error");; return 0; }
int main (void) { char *term = getenv ("TERM"); int status; setupterm (term, 1, &status); printf ("has_meta_key: "); puts (has_meta_key == TRUE ? "true" : "false"); printf ("gnu_has_meta_key: "); puts (gnu_has_meta_key == TRUE ? "true" : "false"); tty_raw (STDIN_FILENO); puts ("Normal"); read_char (); new_line (); putp (meta_on); puts ("Meta mode"); read_char (); new_line (); tty_reset (STDIN_FILENO); return 0; }
/* ** subtest_xenl(test_list, status, ch) ** ** (xenl) eat newline glitch */ static void subtest_xenl( struct test_list *t, int *state, int *ch) { int i, j, k; if (over_strike) { /* test (xenl) on overstrike terminals */ if (!can_go_home || !can_clear_screen) { ptextln("(xenl) Newline-glitch not tested, can't home cursor and clear."); generic_done_message(t, state, ch); return; } put_clear(); /* this test must be done in raw mode. Otherwise UNIX will translate CR to CRLF. */ if (stty_query(TTY_OUT_TRANS)) tty_raw(1, char_mask); ptext("\nreset (xenl). Does "); i = char_count; put_str("not ignore CR, does "); k = char_count; put_str("not ignore LF"); go_home(); for (j = 0; j < columns; j++) put_this(' '); put_cr(); for (j = 0; j < i; j++) putchp(' '); put_str("@@@\n@@"); go_home(); for (j = 0; j < columns; j++) put_this(' '); put_lf(); for (j = 0; j < k; j++) putchp(' '); put_str("@@@\r@@"); tty_set(); go_home(); put_newlines(4); sprintf(temp, "(xenl) Newline-glitch is %s in the data base", eat_newline_glitch ? "true" : "false"); ptextln(temp); } else { /* test (xenl) when (os) is reset */ if (!can_go_home) { ptextln("(xenl) Newline-glitch not tested, can't home cursor"); generic_done_message(t, state, ch); return; } /* (xenl) test */ put_clear(); /* this test must be done in raw mode. Otherwise UNIX will translate CR to CRLF. */ if (stty_query(TTY_OUT_TRANS)) tty_raw(1, char_mask); for (j = 0; j < columns; j++) put_this(' '); put_cr(); ptext("(xenl) should be set. Does not ignore CR"); go_home(); put_crlf(); for (j = 0; j < columns; j++) put_this(' '); put_lf(); /* test (cud1) */ ptext("(xenl) should be set. Ignores (cud1)"); go_home(); put_newlines(3); if (scroll_forward && cursor_down && strcmp(scroll_forward, cursor_down)) { for (j = 0; j < columns; j++) put_this(' '); put_ind(); /* test (ind) */ ptext("(xenl) should be set. Ignores (ind)"); go_home(); put_newlines(5); } tty_set(); ptextln("If you don't see text above telling you to set it, (xenl) should be false"); sprintf(temp, "(xenl) Newline-glitch is %s in the data base", eat_newline_glitch ? "true" : "false"); ptextln(temp); } generic_done_message(t, state, ch); }
int main(int argc, char *argv[]) { int fdm, c, ignoreeof, interactive, noecho, verbose; pid_t pid; char *driver, slave_name[20]; struct termios orig_termios; struct winsize size; interactive = isatty(STDIN_FILENO); ignoreeof = 0; noecho = 0; verbose = 0; driver = NULL; opterr = 0; /* don't want getopt() writing to stderr */ while ( (c = getopt(argc, argv, "d:einv")) != EOF) { switch (c) { case 'd': /* driver for stdin/stdout */ driver = optarg; break; case 'e': /* noecho for slave pty's line discipline */ noecho = 1; break; case 'i': /* ignore EOF on standard input */ ignoreeof = 1; break; case 'n': /* not interactive */ interactive = 0; break; case 'v': /* verbose */ verbose = 1; break; case '?': err_quit("unrecognized option: -%c", optopt); } } if (optind >= argc) err_quit("usage: pty [ -d driver -einv ] program [ arg ... ]"); if (interactive) { /* fetch current termios and window size */ if (tcgetattr(STDIN_FILENO, &orig_termios) < 0) err_sys("tcgetattr error on stdin"); if (ioctl(STDIN_FILENO, TIOCGWINSZ, (char *) &size) < 0) err_sys("TIOCGWINSZ error"); pid = pty_fork(&fdm, slave_name, &orig_termios, &size); } else pid = pty_fork(&fdm, slave_name, NULL, NULL); if (pid < 0) err_sys("fork error"); else if (pid == 0) { /* child */ if (noecho) set_noecho(STDIN_FILENO); /* stdin is slave pty */ if (execvp(argv[optind], &argv[optind]) < 0) err_sys("can't execute: %s", argv[optind]); } if (verbose) { fprintf(stderr, "slave name = %s\n", slave_name); if (driver != NULL) fprintf(stderr, "driver = %s\n", driver); } if (interactive && driver == NULL) { if (tty_raw(STDIN_FILENO) < 0) /* user's tty to raw mode */ err_sys("tty_raw error"); if (atexit(tty_atexit) < 0) /* reset user's tty on exit */ err_sys("atexit error"); } if (driver) do_driver(driver); /* changes our stdin/stdout */ loop(fdm, ignoreeof); /* copies stdin -> ptym, ptym -> stdout */ exit(0); }
int main() { int i; int c; struct sigaction sigact; sigact.sa_handler = sig_catch; sigact.sa_flags = 0; sigemptyset(&sigact.sa_mask); if (-1 == sigaction(SIGINT, &sigact, NULL)) { perror("sigaction"); return -1; } if (-1 == sigaction(SIGQUIT, &sigact, NULL)) { perror("sigaction"); return -1; } if (-1 == sigaction(SIGTERM, &sigact, NULL)) { perror("sigaction"); return -1; } if (0 > tty_raw(STDIN_FILENO)) { perror("tty_raw"); return -1; } printf("Enter raw mode characters, terminate with DELETE\n"); while (1 == (i = read(STDIN_FILENO, &c, 1))) { if ((c &= 255) == 0177) { break; } printf("%o\n", c); } if (0 > tty_reset(STDIN_FILENO)) { perror("tty_reset"); return -1; } if (i <= 0) { fprintf(stderr, "read error\n"); return -1; } if (0 > tty_cbreak(STDIN_FILENO)) { perror("tty_cbreak"); return -1; } printf("\nEnter cbreak mode characters, terminate with SIGINT\n"); while (1 == (i = read(STDIN_FILENO, &c, 1))) { c &= 255; printf("%o\n", c); } if (0 > tty_reset(STDIN_FILENO)) { perror("tty_reset"); return -1; } if (0 >= i) { fprintf(stderr, "read error\n"); } exit(0); }
int main(void) { if (tty_raw(STDIN_FILENO) < 0) err_sys("tty_raw() error"); exit(0); }
void tty_stop_tty(struct tty *tty) { struct winsize ws; if (!(tty->flags & TTY_STARTED)) return; tty->flags &= ~TTY_STARTED; bufferevent_disable(tty->event, EV_READ|EV_WRITE); /* * Be flexible about error handling and try not kill the server just * because the fd is invalid. Things like ssh -t can easily leave us * with a dead tty. */ if (ioctl(tty->fd, TIOCGWINSZ, &ws) == -1) return; if (tcsetattr(tty->fd, TCSANOW, &tty->tio) == -1) return; setblocking(tty->fd, 1); tty_raw(tty, tty_term_string2(tty->term, TTYC_CSR, 0, ws.ws_row - 1)); if (tty_use_acs(tty)) tty_raw(tty, tty_term_string(tty->term, TTYC_RMACS)); tty_raw(tty, tty_term_string(tty->term, TTYC_SGR0)); tty_raw(tty, tty_term_string(tty->term, TTYC_RMKX)); tty_raw(tty, tty_term_string(tty->term, TTYC_CLEAR)); if (tty_term_has(tty->term, TTYC_CS1) && tty->cstyle != 0) { if (tty_term_has(tty->term, TTYC_CSR1)) tty_raw(tty, tty_term_string(tty->term, TTYC_CSR1)); else tty_raw(tty, tty_term_string1(tty->term, TTYC_CS1, 0)); } tty_raw(tty, tty_term_string(tty->term, TTYC_CR)); tty_raw(tty, tty_term_string(tty->term, TTYC_CNORM)); if (tty_term_has(tty->term, TTYC_KMOUS)) tty_raw(tty, "\033[?1000l"); tty_raw(tty, tty_term_string(tty->term, TTYC_RMCUP)); }
int main(int argc, char *argv[]) { int c, nostop=0; char *h; int i_rc = 0; cd_operation_t cd_op = NO_OP; /* operation to do in non-interactive mode */ psz_program = strrchr(argv[0],'/'); psz_program = psz_program ? psz_program+1 : argv[0]; memset(&cddb_opts, 0, sizeof(cddb_opts)); cdio_loglevel_default = CDIO_LOG_WARN; /* parse options */ while ( 1 ) { if (-1 == (c = getopt(argc, argv, "acCdehkplL:sSt:vx"))) break; switch (c) { case 'v': b_verbose = true; if (cdio_loglevel_default > CDIO_LOG_INFO) cdio_loglevel_default = CDIO_LOG_INFO; break; case 'd': debug = 1; if (cdio_loglevel_default > CDIO_LOG_DEBUG) cdio_loglevel_default = CDIO_LOG_DEBUG; break; case 'a': auto_mode = 1; break; case 'L': i_volume_level = atoi(optarg); cd_op = SET_VOLUME; b_interactive = false; break; case 't': if (NULL != (h = strchr(optarg,'-'))) { *h = 0; start_track = atoi(optarg); stop_track = atoi(h+1)+1; if (0 == start_track) start_track = 1; if (1 == stop_track) stop_track = CDIO_CDROM_LEADOUT_TRACK; } else { start_track = atoi(optarg); stop_track = start_track+1; one_track = 1; } b_interactive = false; cd_op = PLAY_TRACK; break; case 'p': b_interactive = false; cd_op = PLAY_CD; break; case 'l': b_interactive = false; cd_op = LIST_TRACKS; break; case 'C': b_interactive = false; cd_op = CLOSE_CD; break; case 'c': b_interactive = false; cd_op = PS_LIST_TRACKS; break; case 's': b_interactive = false; cd_op = STOP_PLAYING; break; case 'S': b_interactive = false; cd_op = LIST_SUBCHANNEL; break; case 'e': b_interactive = false; cd_op = EJECT_CD; break; case 'k': print_keys(); exit(1); case 'h': usage(psz_program); exit(1); default: usage(psz_program); exit(1); } } if (argc > optind) { psz_device = strdup(argv[optind]); } else { char **ppsz_cdda_drives=NULL; char **ppsz_all_cd_drives = cdio_get_devices_ret(&driver_id); if (!ppsz_all_cd_drives) { fprintf(stderr, "Can't find a CD-ROM drive\n"); exit(2); } ppsz_cdda_drives = cdio_get_devices_with_cap(ppsz_all_cd_drives, CDIO_FS_AUDIO, false); if (!ppsz_cdda_drives || !ppsz_cdda_drives[0]) { fprintf(stderr, "Can't find a CD-ROM drive with a CD-DA in it\n"); exit(3); } psz_device = strdup(ppsz_cdda_drives[0]); cdio_free_device_list(ppsz_all_cd_drives); cdio_free_device_list(ppsz_cdda_drives); } if (!b_interactive) { b_sig = true; nostop=1; } tty_raw(); signal(SIGINT,ctrlc); signal(SIGQUIT,ctrlc); signal(SIGTERM,ctrlc); signal(SIGHUP,ctrlc); signal(SIGWINCH, sigwinch); if (CLOSE_CD != cd_op) { /* open device */ if (b_verbose) fprintf(stderr, "open %s... ", psz_device); p_cdio = cdio_open (psz_device, driver_id); if (!p_cdio && cd_op != EJECT_CD) { cd_close(psz_device); p_cdio = cdio_open (psz_device, driver_id); } if (p_cdio && b_verbose) fprintf(stderr,"ok\n"); } if (b_interactive) { #ifdef HAVE_CDDB cddb_log_set_handler (cddb_log_handler); #else ; #endif } else { b_sig = true; nostop=1; if (EJECT_CD == cd_op) { i_rc = cd_eject() ? 0 : 1; } else { switch (cd_op) { case PS_LIST_TRACKS: case LIST_TRACKS: case PLAY_TRACK: read_toc(p_cdio); default: break; } if (p_cdio) switch (cd_op) { case STOP_PLAYING: b_cd = true; i_rc = cd_stop(p_cdio) ? 0 : 1; break; case EJECT_CD: /* Should have been handled above. */ cd_eject(); break; case LIST_TRACKS: list_tracks(); break; case PS_LIST_TRACKS: ps_list_tracks(); break; case PLAY_TRACK: /* play just this one track */ if (b_record) { printf("%s / %s\n", artist, title); if (one_track) printf("%s\n", cd_info[start_track].title); } i_rc = play_track(start_track, stop_track) ? 0 : 1; break; case PLAY_CD: if (b_record) printf("%s / %s\n", artist, title); play_track(1,CDIO_CDROM_LEADOUT_TRACK); break; case SET_VOLUME: i_rc = set_volume_level(p_cdio, i_volume_level); break; case LIST_SUBCHANNEL: if (read_subchannel(p_cdio)) { if (sub.audio_status == CDIO_MMC_READ_SUB_ST_PAUSED || sub.audio_status == CDIO_MMC_READ_SUB_ST_PLAY) { { printf("track %2d - %02x:%02x (%02x:%02x abs) ", sub.track, sub.rel_addr.m, sub.rel_addr.s, sub.abs_addr.m, sub.abs_addr.s); } } printf("drive state: %s\n", mmc_audio_state2str(sub.audio_status)); } else { i_rc = 1; } break; case CLOSE_CD: /* Handled below */ case LIST_KEYS: case TOGGLE_PAUSE: case EXIT_PROGRAM: case NO_OP: break; } else if (CLOSE_CD == cd_op) { i_rc = (DRIVER_OP_SUCCESS == cdio_close_tray(psz_device, NULL)) ? 0 : 1; } else { fprintf(stderr,"no CD in drive (%s)\n", psz_device); } } } /* Play all tracks *unless* we have a play or paused status already. */ read_subchannel(p_cdio); if (sub.audio_status != CDIO_MMC_READ_SUB_ST_PAUSED && sub.audio_status != CDIO_MMC_READ_SUB_ST_PLAY) play_track(1, CDIO_CDROM_LEADOUT_TRACK); while ( !b_sig ) { int key; if (!b_cd) read_toc(p_cdio); read_subchannel(p_cdio); display_status(false); if (1 == select_wait(b_cd ? 1 : 5)) { switch (key = getch()) { case '-': decrease_volume_level(p_cdio); break; case '+': increase_volume_level(p_cdio); break; case 'A': case 'a': auto_mode = !auto_mode; break; case 'X': case 'x': nostop=1; /* fall through */ case 'Q': case 'q': b_sig = true; break; case 'E': case 'e': cd_eject(); break; case 's': cd_stop(p_cdio); break; case 'C': case 'c': cd_close(psz_device); break; case 'L': case 'l': b_all_tracks = !b_all_tracks; if (b_all_tracks) display_tracks(); else { i_last_display_track = CDIO_INVALID_TRACK; display_cdinfo(p_cdio, i_tracks, i_first_track); } break; case 'K': case 'k': case 'h': case 'H': case '?': list_keys(); break; case ' ': case 'P': case 'p': toggle_pause(); break; case KEY_RIGHT: if (b_cd && (sub.audio_status == CDIO_MMC_READ_SUB_ST_PAUSED || sub.audio_status == CDIO_MMC_READ_SUB_ST_PLAY)) play_track(sub.track+1, CDIO_CDROM_LEADOUT_TRACK); else play_track(1,CDIO_CDROM_LEADOUT_TRACK); break; case KEY_LEFT: if (b_cd && (sub.audio_status == CDIO_MMC_READ_SUB_ST_PAUSED || sub.audio_status == CDIO_MMC_READ_SUB_ST_PLAY)) play_track(sub.track-1,CDIO_CDROM_LEADOUT_TRACK); break; case KEY_UP: if (b_cd && sub.audio_status == CDIO_MMC_READ_SUB_ST_PLAY) skip(10); break; case KEY_DOWN: if (b_cd && sub.audio_status == CDIO_MMC_READ_SUB_ST_PLAY) skip(-10); break; case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': play_track(key - '0', CDIO_CDROM_LEADOUT_TRACK); break; case '0': play_track(10, CDIO_CDROM_LEADOUT_TRACK); break; case KEY_F(1): case KEY_F(2): case KEY_F(3): case KEY_F(4): case KEY_F(5): case KEY_F(6): case KEY_F(7): case KEY_F(8): case KEY_F(9): case KEY_F(10): case KEY_F(11): case KEY_F(12): case KEY_F(13): case KEY_F(14): case KEY_F(15): case KEY_F(16): case KEY_F(17): case KEY_F(18): case KEY_F(19): case KEY_F(20): play_track(key - KEY_F(1) + 11, CDIO_CDROM_LEADOUT_TRACK); break; } } } if (!nostop) cd_stop(p_cdio); tty_restore(); finish("bye", i_rc); return 0; /* keep compiler happy */ }
static int found_exit(char *keybuf, int hx, int cc) { /* return true if the user wants to exit */ int j; char *s; if (scan_mode) { if (*keybuf == '\0') { return TRUE; } } else { int k; /* break is a special case */ if (*keybuf == '\0') { fresh_line(); tty_set(); ptext("Hit X to exit: "); if (wait_here() == 'X') { return TRUE; } keys_tested(0, 1, hx); tty_raw(cc, char_mask); return FALSE; } /* is this the end? */ for (k = 0; (j = (keybuf[k] & STRIP_PARITY)); k++) { if (end_funky(j)) { return TRUE; } } j = TRUE; /* does he need an updated list? */ for (k = 0; keybuf[k]; k++) { j &= (keybuf[k] & STRIP_PARITY) == '?'; } if (j || end_state == '?') { keys_tested(0, 1, hx); tty_raw(cc, char_mask); end_state = 0; return FALSE; } } put_cr(); if (hx) { s = hex_expand_to(keybuf, 10); } else { s = expand_to(keybuf, 10); } sprintf(temp, "%s Unknown", s); put_str(temp); for (j = 0; j < MAX_FK_UNK; j++) { if (j == funk) { fk_length[funk] = expand_chars; if ((fk_unknown[funk] = (char *) malloc(strlen(s) + 1))) { strcpy(fk_unknown[funk++], s); } break; } if (fk_length[j] == expand_chars) { if (!strcmp(fk_unknown[j], s)) { break; } } } return FALSE; }
static int found_match(char *s, int hx, int cc) { /* return true if this string is a match */ int j, f; alloc_strings(); if (!*s) { return 0; } if (scan_mode) { for (j = f = 0; scan_down[j]; j++) { if (scan_length[j] == 0) { continue; } if (!strncmp(s, scan_down[j], scan_length[j])) { if (!f) { /* first match */ put_cr(); if (hx) { put_str(hex_expand_to(s, 10)); } else { put_str(expand_to(s, 10)); } f = 1; } (void) end_funky(scan_name[j][0]); put_str(" "); put_str(scan_name[j]); scan_tested[j] = 1; s += scan_length[j]; if (strncmp(s, scan_up[j], scan_length[j])) { put_str(" scan down"); } else { s += scan_length[j]; } if (!*s) { break; } j = -1; } if (!strncmp(s, scan_up[j], scan_length[j])) { if (!f) { /* first match */ put_cr(); if (hx) { put_str(hex_expand_to(s, 10)); } else { put_str(expand_to(s, 10)); } f = 1; } put_str(" "); put_str(scan_name[j]); put_str(" scan up"); s += scan_length[j]; if (!*s) { break; } j = -1; } } } else { for (j = f = 0; j < key_count; j++) { if (fkval[j] && !strcmp(s, fkval[j])) { char outbuf[256]; if (!f) { /* first match */ put_cr(); if (hx) { put_str(hex_expand_to(s, 10)); } else { put_str(expand_to(s, 10)); } f = 1; } sprintf(outbuf, " (%s)", fk_name[j]); put_str(outbuf); if (fk_label[j]) { sprintf(outbuf, " <%s>", fk_label[j]); put_str(outbuf); } fk_tested[j] = 1; } } } if (end_state == '?') { keys_tested(0, 1, hx); tty_raw(cc, char_mask); end_state = 0; } return f; }
int main (int argc, char **argv) { int master_fd; int c; int ignore_eof; int interactive; int quiet; int err_flag; pid_t pid; char *driver; struct termios old_termios; struct winsize tty_winsize; ignore_eof = 0; interactive = isatty (STDIN_FILENO); quiet = 0; driver = NULL; err_flag = 0; opterr = 0; while ((c = getopt (argc, argv, "d:eiq")) != EOF) { switch (c) { case 'd': driver = optarg; break; case 'e': ignore_eof++; break; case 'i': interactive = 0; break; case 'q': quiet++; break; default: err_flag++; break; } } if ((err_flag) || (optind >= argc)) err_quit ("Usage: pty [-d driver] [eiq] command [arg ...]"); if (interactive) { if (tcgetattr (STDIN_FILENO, &old_termios) == -1) err_msg ("tcgetattr failed"); if (ioctl (STDIN_FILENO, TIOCGWINSZ, &tty_winsize) == -1) err_msg ("TIOCGWINSZ failed"); pid = pty_fork (&master_fd, &old_termios, &tty_winsize); } else pid = pty_fork (&master_fd, NULL, NULL); if (pid == -1) err_msg ("pty_fork failed"); if (pid == 0) { if (quiet) disable_echo (STDIN_FILENO); if (execvp (argv [optind], &argv [optind]) == -1) err_msg ("pty: %s", argv [optind]); } if ((interactive) && (driver == NULL)) { if (tty_raw (STDIN_FILENO) == -1) err_msg ("tty_raw failed"); if (atexit (tty_atexit) == -1) err_msg ("tty_atexit failed"); } if (driver) run_driver (driver); loop (master_fd, ignore_eof); return (0); }
int main(int argc, char* argv[]) { int fdm, c, ignoreeof, interactive, noecho, verbose; pid_t pid; char* config; char slave_name[20]; struct termios orig_termios; struct winsize size; int flags; interactive = isatty(STDIN_FILENO); ignoreeof = 0; noecho = 0; verbose = 0; config = NULL; while ((c = getopt_long(argc, argv, "hVs:einv", long_options, NULL)) != EOF) { switch (c) { case 'h': printf("Usage: %s [options] config_file -- program [args ...]\n", argv[0]); printf("\t -h --help \t\tdisplay usage summary\n"); printf("\t -V --version \t\tdisplay version\n"); printf("\t -e --no-echo \t\tdisable echo\n"); printf("\t -i --ignore-eof \tignore EOF\n"); printf("\t -n --non-interactive \tforce non-interactive mode\n"); printf("\t -v --verbose \t\tverbose mode\n"); return EXIT_SUCCESS; case 'V': printf("irpty %s\n", VERSION); return EXIT_SUCCESS; case 'e': noecho = 1; break; case 'i': ignoreeof = 1; break; case 'n': interactive = 0; break; case 'v': verbose = 1; break; case '?': die("unrecognized option: -%c\n", optopt); } } if (optind + 1 >= argc) die("usage: irpty [ -s server -einv ] cfg program [ arg ... ]\n"); config = argv[optind++]; if ((lsock = lirc_init("irpty", 1)) == -1) exit(EXIT_FAILURE); flags = fcntl(lsock, F_GETFL, 0); if (flags != -1) fcntl(lsock, F_SETFL, flags | FASYNC | O_NONBLOCK); if (lirc_readconfig(config, &lconfig, NULL) != 0) exit(EXIT_FAILURE); if (interactive) { if (tcgetattr(STDIN_FILENO, &orig_termios) < 0) die("tcgetattr error on stdin\n"); if (ioctl(STDIN_FILENO, TIOCGWINSZ, (char*)&size) < 0) die("TIOCGWINSZ error\n"); pid = pty_fork(&fdm, slave_name, &orig_termios, &size); } else { pid = pty_fork(&fdm, slave_name, NULL, NULL); } if (pid < 0) { die("fork error\n"); } else if (!pid) { /* child */ if (noecho) set_noecho(STDIN_FILENO); /* stdin is slave pty */ if (execvp(argv[optind], &argv[optind]) < 0) die("can't execute: %s\n", argv[optind]); } if (verbose) { fprintf(stderr, "slave name = %s\n", slave_name); if (config) fprintf(stderr, "config file = %s\n", config); } if (interactive) { if (tty_raw(STDIN_FILENO) < 0) /* user's tty to raw mode */ die("tty_raw error"); if (atexit(tty_atexit) < 0) /* reset user's tty on exit */ die("atexit error"); } copy_loop(fdm, ignoreeof); exit(0); }
int main() { tty_raw(STDIN_FILENO); return EXIT_SUCCESS; }