123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 |
- #include "libbb.h"
- #include "common_bufsiz.h"
- static void xget1(int fd, struct termios *t, struct termios *oldt)
- {
- tcgetattr(fd, oldt);
- *t = *oldt;
- cfmakeraw(t);
- }
- static int xset1(int fd, struct termios *tio, const char *device)
- {
- int ret = tcsetattr(fd, TCSAFLUSH, tio);
- if (ret) {
- bb_perror_msg("can't tcsetattr for %s", device);
- }
- return ret;
- }
- int microcom_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
- int microcom_main(int argc UNUSED_PARAM, char **argv)
- {
- int sfd;
- int nfd;
- struct pollfd pfd[2];
- struct termios tio0, tiosfd, tio;
- char *device_lock_file;
- enum {
- OPT_X = 1 << 0,
- OPT_s = 1 << 1,
- OPT_d = 1 << 2,
- OPT_t = 1 << 3,
- };
- speed_t speed = 9600;
- int delay = -1;
- int timeout = -1;
- unsigned opts;
-
- opts = getopt32(argv, "Xs:+d:+t:+", &speed, &delay, &timeout);
- argv += optind;
-
- device_lock_file = (char *)bb_basename(argv[0]);
- device_lock_file = xasprintf("/var/lock/LCK..%s", device_lock_file);
- sfd = open(device_lock_file, O_CREAT | O_WRONLY | O_TRUNC | O_EXCL, 0644);
- if (sfd < 0) {
-
- if (errno == EEXIST)
- bb_perror_msg_and_die("can't create '%s'", device_lock_file);
-
- if (ENABLE_FEATURE_CLEAN_UP)
- free(device_lock_file);
- device_lock_file = NULL;
- } else {
-
-
-
- fdprintf(sfd, "%4d\n", getpid());
- close(sfd);
- }
-
- bb_signals(0
- + (1 << SIGHUP)
- + (1 << SIGINT)
- + (1 << SIGTERM)
- + (1 << SIGPIPE)
- , record_signo);
-
- bb_got_signal = 1;
-
- sfd = open_or_warn(argv[0], O_RDWR | O_NOCTTY | O_NONBLOCK);
- if (sfd < 0)
- goto done;
- fcntl(sfd, F_SETFL, O_RDWR);
-
- xget1(sfd, &tio, &tiosfd);
-
- cfsetspeed(&tio, tty_value_to_baud(speed));
- if (xset1(sfd, &tio, argv[0]))
- goto done;
-
-
- if (isatty(STDIN_FILENO)) {
- xget1(STDIN_FILENO, &tio, &tio0);
- if (xset1(STDIN_FILENO, &tio, "stdin"))
- goto done;
- }
-
- pfd[0].fd = sfd;
- pfd[0].events = POLLIN;
- pfd[1].fd = STDIN_FILENO;
- pfd[1].events = POLLIN;
- bb_got_signal = 0;
- nfd = 2;
-
- while (!bb_got_signal && poll(pfd, nfd, timeout) > 0) {
- if (nfd > 1 && pfd[1].revents) {
- char c;
-
- if (safe_read(STDIN_FILENO, &c, 1) < 1) {
-
- nfd--;
- goto skip_write;
- }
-
- if (!(opts & OPT_X)) {
-
- if (VINTR == c) {
- tcsendbreak(sfd, 0);
- goto skip_write;
- }
-
- if (24 == c)
- break;
- }
- write(sfd, &c, 1);
- if (delay >= 0)
- safe_poll(pfd, 1, delay);
- skip_write: ;
- }
- if (pfd[0].revents) {
- ssize_t len;
- #define iobuf bb_common_bufsiz1
- setup_common_bufsiz();
-
- len = safe_read(sfd, iobuf, COMMON_BUFSIZE);
- if (len > 0)
- full_write(STDOUT_FILENO, iobuf, len);
- else {
-
- bb_got_signal = SIGHUP;
- break;
- }
- }
- }
-
- tcsetattr(sfd, TCSAFLUSH, &tiosfd);
- if (isatty(STDIN_FILENO))
- tcsetattr(STDIN_FILENO, TCSAFLUSH, &tio0);
- done:
- if (device_lock_file)
- unlink(device_lock_file);
- return bb_got_signal;
- }
|