-
Notifications
You must be signed in to change notification settings - Fork 2
/
neocon.c
412 lines (363 loc) · 7.86 KB
/
neocon.c
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/*
* neocon.c - An interface for changing tty devices
*
* Copyright (C) 2007, 2008 by OpenMoko, Inc.
* Written by Werner Almesberger <[email protected]>
* All Rights Reserved
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <stdlib.h>
#include <stdio.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <termios.h>
#include <fcntl.h>
#include <assert.h>
#include <sys/time.h>
#include <sys/select.h>
#ifdef __APPLE__
#include <sys/ioctl.h>
#include <sys/errno.h>
#include <IOKit/serial/ioss.h>
#endif
#define MAX_BUF 2048
static char *const *ttys;
static int num_ttys;
static int curr_tty = -1; /* start with first tty */
static speed_t speed = B115200;
static struct termios console, tty;
static FILE *log = NULL;
static int timestamp = 0;
static char escape = '~';
static struct bps {
speed_t speed;
int bps;
} bps_tab[] = {
{ B300, 300 },
{ B1200, 1200 },
{ B2400, 2400 },
{ B9600, 9600 },
{ B19200, 19200 },
{ B38400, 38400 },
{ B57600, 57600 },
{ B115200, 115200 },
#ifdef B460800
{ B460800, 460800 },
#endif
#ifdef B921600
{ B921600, 921600 },
#endif
{ 0, 0 }
};
static speed_t bps_to_speed(int bps)
{
#ifdef __APPLE__
return bps;
#endif
const struct bps *p;
for (p = bps_tab; p->bps; p++)
if (p->bps == bps)
return p->speed;
fprintf(stderr, "no such speed: %d bps\n", bps);
exit(1);
}
static void make_raw(int fd, struct termios *old)
{
struct termios t;
long flags;
if (tcgetattr(fd, &t) < 0) {
perror("tcgetattr");
exit(1);
}
if (old)
*old = t;
cfmakeraw(&t);
if (fd) {
t.c_iflag &= ~(IXON | IXOFF);
t.c_cflag |= CLOCAL;
t.c_cflag &= ~CRTSCTS;
#ifdef __APPLE__
if (ioctl(fd, IOSSIOSPEED, &speed) == -1) {
printf("Error %d calling ioctl( ..., IOSSIOSPEED, ... )\n", errno);
}
#endif
if (cfsetispeed(&t, speed) < 0) {
perror("cfsetispeed");
exit(1);
}
if (cfsetospeed(&t, speed) < 0) {
perror("cfsetospeed");
exit(1);
}
}
if (tcsetattr(fd, TCSANOW, &t) < 0) {
perror("tcsetattr");
exit(1);
}
flags = fcntl(fd,F_GETFL);
if (flags < 0) {
perror("fcntl F_GETFL");
exit(1);
}
if (fcntl(fd,F_SETFL,flags & ~O_NONBLOCK) < 0) {
perror("fcntl F_GETFL");
exit(1);
}
}
static int open_next_tty(void)
{
int i, fd = -1;
for (i = 0; i != num_ttys; i++) {
curr_tty = (curr_tty+1) % num_ttys;
fd = open(ttys[curr_tty], O_RDWR | O_NDELAY);
if (fd >= 0)
break;
}
if (fd >= 0)
make_raw(fd, &tty);
return fd;
}
/*
* Return 1 if the user manually forces a device change.
*/
static int scan(const char *s, size_t len)
{
static int state = 0;
const char *p;
int res = 0;
for (p = s; p != s+len; p++)
switch (state) {
case 0:
if (*p == escape)
state++;
else
state = 0;
break;
case 1:
if (*p == '.')
exit(0);
if (*p == 'n')
res = 1;
state = 0;
break;
}
return res;
}
static int write_log(const char *buf, ssize_t len)
{
size_t wrote;
wrote = fwrite(buf, 1, len, log);
if (wrote == len)
return 1;
fprintf(stderr, "write failed. closing log file.\n");
fclose(log);
log = NULL;
return 0;
}
static int add_timestamp(void)
{
struct timeval tv;
char buf[40]; /* be generous */
int len;
if (gettimeofday(&tv, NULL) < 0) {
perror("gettimeofday");
exit(1);
}
len = sprintf(buf, "%lu.%06lu ",
(unsigned long) tv.tv_sec, (unsigned long) tv.tv_usec);
return write_log(buf, len);
}
static void do_log(const char *buf, ssize_t len)
{
static int nl = 1; /* we're at the beginning of a new line */
char tmp[MAX_BUF];
const char *from;
char *to;
assert(len <= MAX_BUF);
from = buf;
to = tmp;
while (from != buf+len) {
if (*from == '\r') {
from++;
continue;
}
if (nl && timestamp)
if (!add_timestamp())
return;
nl = 0;
if (*from == '\n') {
*to++ = *from++;
if (!write_log(tmp, to-tmp))
return;
to = tmp;
nl = 1;
continue;
}
*to++ = *from < ' ' || *from > '~' ? '#' : *from;
from++;
}
write_log(tmp, to-tmp);
}
static int copy(int in, int out, int from_user, int single)
{
char buffer[MAX_BUF];
ssize_t got, wrote, pos;
got = read(in, buffer, single ? 1 : sizeof(buffer));
if (got <= 0)
return 0;
if (from_user) {
if (scan(buffer, got))
return 0;
}
else {
if (log)
do_log(buffer, got);
}
for (pos = 0; pos != got; pos += wrote) {
wrote = write(out, buffer+pos, got-pos);
if (wrote < 0)
return 0;
}
return 1;
}
static void write_string(const char *s)
{
int len = strlen(s);
while (len) {
ssize_t wrote;
wrote = write(1, s, len);
if (wrote < 0) {
perror("write");
exit(1);
}
s += wrote;
len -= wrote;
}
}
static void cleanup(void)
{
if (tcsetattr(0, TCSANOW, &console) < 0)
perror("tcsetattr");
write(1, "\n", 1);
}
static void usage(const char *name)
{
fprintf(stderr,
"usage: %s [-b bps] [-e escape] [-l logfile [-a] [-T]] [-t delay_ms] tty ...\n\n"
" -a append to the log file if it already exists\n"
" -b bps set the TTY to the specified bit rate\n"
" -e escape set the escape character (default: ~)\n"
" -l logfile log all output to the specified file\n"
" -t delay_ms wait the specified amount of time between input characters\n"
" -T add timestamps to the log file\n"
, name);
exit(1);
}
int main(int argc, char *const *argv)
{
char *end;
int c, bps;
int fd = -1;
int append = 0;
const char *logfile = NULL;
int throttle_us = 0;
int throttle = 0;
while ((c = getopt(argc, argv, "ab:e:l:t:T")) != EOF)
switch (c) {
case 'a':
append = 1;
break;
case 'b':
bps = strtoul(optarg, &end, 0);
if (*end)
usage(*argv);
speed = bps_to_speed(bps);
break;
case 'e':
if (strlen(optarg) != 1)
usage(*argv);
escape = *optarg;
break;
case 'l':
logfile = optarg;
break;
case 't':
throttle_us = strtoul(optarg, &end, 0)*1000;
if (*end)
usage(*argv);
break;
case 'T':
timestamp = 1;
break;
default:
usage(*argv);
}
num_ttys = argc-optind;
ttys = argv+optind;
if (logfile) {
log = fopen(logfile, append ? "a" : "w");
if (!log) {
perror(logfile);
exit(1);
}
setlinebuf(log);
}
make_raw(0, &console);
atexit(cleanup);
while (1) {
struct timeval tv;
fd_set set;
int res;
if (fd < 0) {
fd = open_next_tty();
if (fd > 0) {
char buf[1024]; /* enough :-) */
sprintf(buf, "\r\r[Open %s]\r\n", ttys[curr_tty]);
write_string(buf);
}
}
FD_ZERO(&set);
if (!throttle)
FD_SET(0, &set);
if (fd >= 0)
FD_SET(fd, &set);
tv.tv_sec = 0;
tv.tv_usec = throttle ? throttle_us : 100000;
res = select(fd < 0 ? 1 : fd+1, &set, NULL, NULL, &tv);
if (res < 0) {
perror("select");
return 1;
}
if (!res)
throttle = 0;
if (FD_ISSET(0, &set)) {
if (throttle_us)
throttle = 1;
if (!copy(0, fd, 1, throttle_us != 0))
goto failed;
}
if (fd >= 0 && FD_ISSET(fd, &set))
if (!copy(fd, 1, 0, 0))
goto failed;
continue;
failed:
write_string("\r\n[Closed]\r\n");
(void) close(fd);
fd = -1;
}
return 0;
}