📄 picocom.c
字号:
char *newflow_str, *newparity_str; char fname[128]; int r, n; unsigned char c; tty_q.len = 0; state = ST_TRANSPARENT; dtr_up = 0; for (;;) { FD_ZERO(&rdset); FD_ZERO(&wrset); FD_SET(STI, &rdset); FD_SET(tty_fd, &rdset); if ( tty_q.len ) FD_SET(tty_fd, &wrset); if (select(FD_SETSIZE, &rdset, &wrset, NULL, NULL) < 0) fatal("select failed: %d : %s", errno, strerror(errno)); if ( FD_ISSET(STI, &rdset) ) { /* read from terminal */ do { n = read(STI, &c, 1); } while (n < 0 && errno == EINTR); if (n == 0) fatal("stdin closed"); else if (n < 0) fatal("read from stdin failed: %s", strerror(errno)); switch (state) { case ST_COMMAND: if ( c == opts.escape ) { state = ST_TRANSPARENT; /* pass the escape character down */ if (tty_q.len <= TTY_Q_SZ) tty_q.buff[tty_q.len++] = c; else fd_printf(STO, "\x07"); break; } state = ST_TRANSPARENT; switch (c) { case KEY_EXIT: return; case KEY_QUIT: term_set_hupcl(tty_fd, 0); term_flush(tty_fd); term_apply(tty_fd); term_erase(tty_fd); return; case KEY_STATUS: fd_printf(STO, "\r\n"); fd_printf(STO, "*** baud: %d\r\n", opts.baud); fd_printf(STO, "*** flow: %s\r\n", opts.flow_str); fd_printf(STO, "*** parity: %s\r\n", opts.parity_str); fd_printf(STO, "*** databits: %d\r\n", opts.databits); fd_printf(STO, "*** dtr: %s\r\n", dtr_up ? "up" : "down"); break; case KEY_PULSE: fd_printf(STO, "\r\n*** pulse DTR ***\r\n"); if ( term_pulse_dtr(tty_fd) < 0 ) fd_printf(STO, "*** FAILED\r\n"); break; case KEY_TOGGLE: if ( dtr_up ) r = term_lower_dtr(tty_fd); else r = term_raise_dtr(tty_fd); if ( r >= 0 ) dtr_up = ! dtr_up; fd_printf(STO, "\r\n*** DTR: %s ***\r\n", dtr_up ? "up" : "down"); break; case KEY_BAUD_UP: newbaud = baud_up(opts.baud); term_set_baudrate(tty_fd, newbaud); tty_q.len = 0; term_flush(tty_fd); if ( term_apply(tty_fd) >= 0 ) opts.baud = newbaud; fd_printf(STO, "\r\n*** baud: %d ***\r\n", opts.baud); break; case KEY_BAUD_DN: newbaud = baud_down(opts.baud); term_set_baudrate(tty_fd, newbaud); tty_q.len = 0; term_flush(tty_fd); if ( term_apply(tty_fd) >= 0 ) opts.baud = newbaud; fd_printf(STO, "\r\n*** baud: %d ***\r\n", opts.baud); break; case KEY_FLOW: newflow = flow_next(opts.flow, &newflow_str); term_set_flowcntrl(tty_fd, newflow); tty_q.len = 0; term_flush(tty_fd); if ( term_apply(tty_fd) >= 0 ) { opts.flow = newflow; opts.flow_str = newflow_str; } fd_printf(STO, "\r\n*** flow: %s ***\r\n", opts.flow_str); break; case KEY_PARITY: newparity = parity_next(opts.parity, &newparity_str); term_set_parity(tty_fd, newparity); tty_q.len = 0; term_flush(tty_fd); if ( term_apply(tty_fd) >= 0 ) { opts.parity = newparity; opts.parity_str = newparity_str; } fd_printf(STO, "\r\n*** parity: %s ***\r\n", opts.parity_str); break; case KEY_BITS: newbits = bits_next(opts.databits); term_set_databits(tty_fd, newbits); tty_q.len = 0; term_flush(tty_fd); if ( term_apply(tty_fd) >= 0 ) opts.databits = newbits; fd_printf(STO, "\r\n*** databits: %d ***\r\n", opts.databits); break; case KEY_SEND: fd_printf(STO, "\r\n*** file: "); r = fd_readline(STI, STO, fname, sizeof(fname)); fd_printf(STO, "\r\n"); if ( r < -1 && errno == EINTR ) break; if ( r <= -1 ) fatal("cannot read filename: %s", strerror(errno)); run_cmd(tty_fd, opts.send_cmd, fname, NULL); break; case KEY_RECEIVE: fd_printf(STO, "*** file: "); r = fd_readline(STI, STO, fname, sizeof(fname)); fd_printf(STO, "\r\n"); if ( r < -1 && errno == EINTR ) break; if ( r <= -1 ) fatal("cannot read filename: %s", strerror(errno)); if ( fname[0] ) run_cmd(tty_fd, opts.send_cmd, fname, NULL); else run_cmd(tty_fd, opts.receive_cmd, NULL); break; case KEY_BREAK: term_break(tty_fd); fd_printf(STO, "\r\n*** break sent ***\r\n"); break; default: break; } break; case ST_TRANSPARENT: if ( c == opts.escape ) { state = ST_COMMAND; } else { if (tty_q.len <= TTY_Q_SZ) tty_q.buff[tty_q.len++] = c; else fd_printf(STO, "\x07"); } break; default: assert(0); break; } } if ( FD_ISSET(tty_fd, &rdset) ) { /* read from port */ do { n = read(tty_fd, &c, 1); } while (n < 0 && errno == EINTR); if (n == 0) fatal("term closed"); else if ( n < 0 ) fatal("read from term failed: %s", strerror(errno)); do { n = write(STO, &c, 1); } while ( errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR ); if ( n <= 0 ) fatal("write to stdout failed: %s", strerror(errno)); } if ( FD_ISSET(tty_fd, &wrset) ) { /* write to port */ do { n = write(tty_fd, tty_q.buff, tty_q.len); } while ( n < 0 && errno == EINTR ); if ( n <= 0 ) fatal("write to term failed: %s", strerror(errno)); memcpy(tty_q.buff, tty_q.buff + n, tty_q.len - n); tty_q.len -= n; } }}/**********************************************************************/voiddeadly_handler(int signum){ kill(0, SIGTERM); sleep(1);#ifdef UUCP_LOCK_DIR uucp_unlock();#endif exit(EXIT_FAILURE);}voidestablish_signal_handlers (void){ struct sigaction exit_action, ign_action; /* Set up the structure to specify the exit action. */ exit_action.sa_handler = deadly_handler; sigemptyset (&exit_action.sa_mask); exit_action.sa_flags = 0; /* Set up the structure to specify the ignore action. */ ign_action.sa_handler = SIG_IGN; sigemptyset (&ign_action.sa_mask); ign_action.sa_flags = 0; sigaction (SIGTERM, &exit_action, NULL); sigaction (SIGINT, &ign_action, NULL); sigaction (SIGHUP, &ign_action, NULL); sigaction (SIGALRM, &ign_action, NULL); sigaction (SIGUSR1, &ign_action, NULL); sigaction (SIGUSR2, &ign_action, NULL); sigaction (SIGPIPE, &ign_action, NULL);}/**********************************************************************/voidshow_usage(char *name){ char *s; s = strrchr(name, '/'); s = s ? s+1 : name; printf("picocom v%s\n", VERSION_STR); printf("Usage is: %s [options] <tty device>\n", s); printf("Options are:\n"); printf(" --<b>aud <baudrate>\n"); printf(" --<f>low s (=soft) | h (=hard) | n (=none)\n"); printf(" --<p>arity o (=odd) | e (=even) | n (=none)\n"); printf(" --<d>atabits 5 | 6 | 7 | 8\n"); printf(" --<e>scape <char>\n"); printf(" --no<i>nit\n"); printf(" --no<r>eset\n"); printf(" --no<l>ock\n"); printf(" --<s>end-cmd <command>\n"); printf(" --recei<v>e-cmd <command>\n"); printf(" --<h>elp\n"); printf("<?> indicates the equivalent short option.\n"); printf("Short options are prefixed by \"-\" instead of by \"--\".\n");}/**********************************************************************/voidparse_args(int argc, char *argv[]){ static struct option longOptions[] = { {"receive-cmd", required_argument, 0, 'v'}, {"send-cmd", required_argument, 0, 's'}, {"escape", required_argument, 0, 'e'}, {"noinit", no_argument, 0, 'i'}, {"noreset", no_argument, 0, 'r'}, {"nolock", no_argument, 0, 'l'}, {"flow", required_argument, 0, 'f'}, {"baud", required_argument, 0, 'b'}, {"parity", required_argument, 0, 'p'}, {"databits", required_argument, 0, 'd'}, {"help", no_argument, 0, 'h'}, {0, 0, 0, 0} }; while (1) { int optionIndex = 0; int c; /* no default error messages printed. */ opterr = 0; c = getopt_long(argc, argv, "hirls:r:e:f:b:p:d:", longOptions, &optionIndex); if (c < 0) break; switch (c) { case 's': strncpy(opts.send_cmd, optarg, sizeof(opts.send_cmd)); opts.send_cmd[sizeof(opts.send_cmd) - 1] = '\0'; break; case 'v': strncpy(opts.receive_cmd, optarg, sizeof(opts.receive_cmd)); opts.receive_cmd[sizeof(opts.receive_cmd) - 1] = '\0'; break; case 'i': opts.noinit = 1; break; case 'r': opts.noreset = 1; break; case 'l': opts.nolock = 1; break; case 'e': if ( isupper(optarg[0]) ) opts.escape = optarg[0] - 'A' + 1; else opts.escape = optarg[0] - 'a' + 1; break; case 'f': switch (optarg[0]) { case 'X': case 'x': opts.flow_str = "xon/xoff"; opts.flow = FC_XONXOFF; break; case 'H': case 'h': opts.flow_str = "RTS/CTS"; opts.flow = FC_RTSCTS; break; case 'N': case 'n': opts.flow_str = "none"; opts.flow = FC_NONE; break; default: fprintf(stderr, "--flow '%c' ignored.\n", optarg[0]); fprintf(stderr, "--flow can be one off: 'x', 'h', or 'n'\n"); break; } break; case 'b': opts.baud = atoi(optarg); break; case 'p': switch (optarg[0]) { case 'e': opts.parity_str = "even"; opts.parity = P_EVEN; break; case 'o': opts.parity_str = "odd"; opts.parity = P_ODD; break; case 'n': opts.parity_str = "none"; opts.parity = P_NONE; break; default: fprintf(stderr, "--parity '%c' ignored.\n", optarg[0]); fprintf(stderr, "--parity can be one off: 'o', 'e', or 'n'\n"); break; } break; case 'd': switch (optarg[0]) { case '5': opts.databits = 5; break; case '6': opts.databits = 6; break; case '7': opts.databits = 7; break; case '8': opts.databits = 8; break; default: fprintf(stderr, "--databits '%c' ignored.\n", optarg[0]); fprintf(stderr, "--databits can be one off: 5, 6, 7 or 8\n"); break; } break; case 'h': show_usage(argv[0]); exit(EXIT_SUCCESS); case '?': default: fprintf(stderr, "Unrecognized option.\n"); fprintf(stderr, "Run with '--help'.\n"); exit(EXIT_FAILURE); } } /* while */ if ( (argc - optind) < 1) { fprintf(stderr, "No port given\n"); exit(EXIT_FAILURE); } strncpy(opts.port, argv[optind], sizeof(opts.port) - 1); opts.port[sizeof(opts.port) - 1] = '\0'; printf("picocom v%s\n", VERSION_STR); printf("\n"); printf("port is : %s\n", opts.port); printf("flowcontrol : %s\n", opts.flow_str); printf("baudrate is : %d\n", opts.baud); printf("parity is : %s\n", opts.parity_str); printf("databits are : %d\n", opts.databits); printf("escape is : C-%c\n", 'a' + opts.escape - 1); printf("noinit is : %s\n", opts.noinit ? "yes" : "no"); printf("noreset is : %s\n", opts.noreset ? "yes" : "no"); printf("nolock is : %s\n", opts.nolock ? "yes" : "no"); printf("send_cmd is : %s\n", opts.send_cmd); printf("receive_cmd is : %s\n", opts.receive_cmd); printf("\n");}/**********************************************************************/intmain(int argc, char *argv[]){ int r; parse_args(argc, argv); establish_signal_handlers(); r = term_lib_init(); if ( r < 0 ) fatal("term_init failed: %s", term_strerror(term_errno, errno));#ifdef UUCP_LOCK_DIR if ( ! opts.nolock ) uucp_lockname(UUCP_LOCK_DIR, opts.port); if ( uucp_lock() < 0 ) fatal("cannot lock %s: %s", opts.port, strerror(errno));#endif tty_fd = open(opts.port, O_RDWR | O_NONBLOCK); if (tty_fd < 0) fatal("cannot open %s: %s", opts.port, strerror(errno)); if ( opts.noinit ) { r = term_add(tty_fd); } else { r = term_set(tty_fd, 1, /* raw mode. */ opts.baud, /* baud rate. */ opts.parity, /* parity. */ opts.databits, /* data bits. */ opts.flow, /* flow control. */ 1, /* local or modem */ !opts.noreset); /* hup-on-close. */ } if ( r < 0 ) fatal("failed to add device %s: %s", opts.port, term_strerror(term_errno, errno)); r = term_apply(tty_fd); if ( r < 0 ) fatal("failed to config device %s: %s", opts.port, term_strerror(term_errno, errno)); r = term_add(STI); if ( r < 0 ) fatal("failed to add I/O device: %s", term_strerror(term_errno, errno)); term_set_raw(STI); r = term_apply(STI); if ( r < 0 ) fatal("failed to set I/O device to raw mode: %s", term_strerror(term_errno, errno)); fd_printf(STO, "Terminal ready\r\n"); loop(); fd_printf(STO, "\r\n"); if ( opts.noreset ) { fd_printf(STO, "Skipping tty reset...\r\n"); term_erase(tty_fd); } fd_printf(STO, "Thanks for using picocom\r\n"); /* wait a bit for output to drain */ sleep(1);#ifdef UUCP_LOCK_DIR uucp_unlock();#endif return EXIT_SUCCESS;}/**********************************************************************//* * Local Variables: * mode:c * tab-width: 4 * c-basic-offset: 4 * End: */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -