aboutsummaryrefslogtreecommitdiff
path: root/toys/net/microcom.c
blob: 1ee9204eb1f5724ddd02bb45a76e521120132db7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/* microcom.c - Simple serial console.
 *
 * Copyright 2017 The Android Open Source Project.

USE_MICROCOM(NEWTOY(microcom, "<1>1s#=115200X", TOYFLAG_USR|TOYFLAG_BIN|TOYFLAG_NOBUF))

config MICROCOM
  bool "microcom"
  default y
  help
    usage: microcom [-s SPEED] [-X] DEVICE

    Simple serial console. Hit CTRL-] for menu.

    -s	Set baud rate to SPEED (default 115200)
    -X	Ignore ^] menu escape
*/

#define FOR_microcom
#include "toys.h"

GLOBALS(
  long s;

  int fd, stok;
  struct termios old_stdin, old_fd;
)

// TODO: tty_sigreset outputs ansi escape sequences, how to disable?
static void restore_states(int i)
{
  if (TT.stok) tcsetattr(0, TCSAFLUSH, &TT.old_stdin);
  tcsetattr(TT.fd, TCSAFLUSH, &TT.old_fd);
}

static void handle_esc(void)
{
  char input;

  xputsn("\r\n[b]reak, [p]aste file, [q]uit: ");
  if (read(0, &input, 1)<1 || input == CTRL('D') || input == 'q') {
    xputs("exit\r");
    xexit();
  }
  if (input == 'b') tcsendbreak(TT.fd, 0);
  else if (input == 'p') {
    long long written = 0, size;
    char* filename;
    int len = 0, fd;

    // TODO: share code with hexedit's prompt() and vi's ex mode.
    // TODO: tab completion!
    memset(toybuf, 0, sizeof(toybuf));
    while (1) {
      xprintf("\r\e[2K\e[1mFilename: \e[0m%s", toybuf);
      if (read(0, &input, 1) <= 0 || input == CTRL('[')) {
        return;
      }
      if (input == '\r') break;
      if (input == 0x7f && len > 0) toybuf[--len] = 0;
      else if (input == CTRL('U')) while (len > 0) toybuf[--len] = 0;
      else if (input >= ' ' && input <= 0x7f && len < sizeof(toybuf))
        toybuf[len++] = input;
    }
    toybuf[len] = 0;
    if (!len) return;
    filename = xstrdup(toybuf);
    fd = xopen(filename, O_RDONLY);
    size = fdlength(fd);
    // The alternative would be to just feed this fd into the usual loop,
    // so we're reading back these characters if they're being echoed, but
    // for my specific use case of pasting into `base64 -d -i > foo`, this
    // is a much more convenient UI.
    while ((len = read(fd, toybuf, sizeof(toybuf))) > 0) {
      written += len;
      xprintf("\r\e[2KPasting '%s' %lld/%lld (%lld%%)...", filename, written,
        size, written*100/size);
      xwrite(TT.fd, toybuf, len);
    }
    free(filename);
    close(fd);
  } else xprintf("Ignoring unknown command.");

  xprintf("\r\n");
}

void microcom_main(void)
{
  struct termios tio;
  struct pollfd fds[2];
  int i;

  // Open with O_NDELAY, but switch back to blocking for reads.
  TT.fd = xopen(*toys.optargs, O_RDWR | O_NOCTTY | O_NDELAY);
  if (-1==(i = fcntl(TT.fd, F_GETFL, 0)) || fcntl(TT.fd, F_SETFL, i&~O_NDELAY)
      || tcgetattr(TT.fd, &TT.old_fd))
    perror_exit_raw(*toys.optargs);

  // Set both input and output to raw mode.
  memcpy(&tio, &TT.old_fd, sizeof(struct termios));
  cfmakeraw(&tio);
  xsetspeed(&tio, TT.s);
  if (tcsetattr(TT.fd, TCSAFLUSH, &tio)) perror_exit("set speed");
  if (!set_terminal(0, 1, 0, &TT.old_stdin)) TT.stok++;
  // ...and arrange to restore things, however we may exit.
  sigatexit(restore_states);

  fds[0].fd = TT.fd;
  fds[1].fd = 0;
  fds[0].events = fds[1].events = POLLIN;

  if (!FLAG(X)) xputs("Escape character is '^]'.\r");
  while (poll(fds, 2, -1) > 0) {

    // Read from connection, write to stdout.
    if (fds[0].revents) {
      if (0 < (i = read(TT.fd, toybuf, sizeof(toybuf)))) xwrite(0, toybuf, i);
      else break;
    }

    // Read from stdin, write to connection.
    if (fds[1].revents) {
      if (read(0, toybuf, 1) != 1) break;
      if (!FLAG(X) && *toybuf == CTRL(']')) handle_esc();
      else xwrite(TT.fd, toybuf, 1);
    }
  }
}