This repository has been archived by the owner on Dec 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tty.c
887 lines (774 loc) · 25.7 KB
/
tty.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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
/*
* tio - a simple TTY terminal I/O application
*
* Copyright (c) 2014-2017 Martin Lund
*
* 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 "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <limits.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/param.h>
#include <sys/file.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <termios.h>
#include <stdbool.h>
#include <errno.h>
#include <time.h>
#include "config.h"
#include "tio/tty.h"
#include "tio/print.h"
#include "tio/options.h"
#include "tio/time.h"
#include "tio/log.h"
#include "tio/error.h"
#include "tio/upgrades.h"
#ifdef HAVE_TERMIOS2
extern int setspeed2(int tty_fd, int baudrate);
#endif
struct termios tio, tio_old, stdout_new; //Edited by Evandro. Now not static
struct termios stdout_old, stdin_new, stdin_old; //Edited by Evandro. Now not static
unsigned long rx_total = 0, tx_total = 0; //Edited by Evandro. Now not static
static bool connected = false;
static bool tainted = false;
static bool print_mode = NORMAL;
static bool standard_baudrate = true;
void (*print)(char c); //Edited by Evandro. Now not static
int tty_fd; //Edited by Evandro. Now not static
static bool map_i_nl_crnl = false;
bool map_o_cr_nl = false; //Edited by Evandro. Now not static
bool map_o_nl_crnl = false; //Edited by Evandro. Now not static
static bool map_o_del_bs = false;
static bool display_parallel_keyboard;
#define tio_printf(format, args...) \
{ \
if (tainted) putchar('\n'); \
color_printf("[%s] " format, current_time(), ## args); \
tainted = false; \
}
static void print_hex(char c)
{
if ((c == '\n') || (c == '\r'))
printf("%c", c);
else
printf("%02x ", (unsigned char) c);
fflush(stdout);
}
void print_normal(char c) //Edited by Evandro
{
putchar(c);
fflush(stdout);
}
static void toggle_line(const char *line_name, int mask)
{
int state;
if (ioctl(tty_fd, TIOCMGET, &state) < 0)
{
error_printf("Could not get line state: %s", strerror(errno));
}
else
{
if (state & mask)
{
state &= ~mask;
tio_printf("set %s to LOW", line_name);
}
else
{
state |= mask;
tio_printf("set %s to HIGH", line_name);
}
if (ioctl(tty_fd, TIOCMSET, &state) < 0)
error_printf("Could not set line state: %s", strerror(errno));
}
}
void handle_command_sequence(char input_char, char previous_char, char *output_char, bool *forward)
{
char unused_char;
bool unused_bool;
int state;
/* Ignore unused arguments */
if (output_char == NULL)
output_char = &unused_char;
if (forward == NULL)
forward = &unused_bool;
/* Handle escape key commands */
if (previous_char == KEY_CTRL_T)
{
/* Do not forward input char to output by default */
*forward = false;
switch (input_char)
{
case KEY_QUESTION:
tio_printf("Key commands:");
tio_printf(" ctrl-t ? List available key commands");
tio_printf(" ctrl-t b Send break");
tio_printf(" ctrl-t c Show configuration");
tio_printf(" ctrl-t e Toggle local echo mode");
tio_printf(" ctrl-t h Toggle hexadecimal mode");
tio_printf(" ctrl-t f File send (Plain Text Only)");
tio_printf(" ctrl-t k Show parallel keyboard maping");
tio_printf(" ctrl-t l Clear screen");
tio_printf(" ctrl-t q Quit");
tio_printf(" ctrl-t s Show statistics");
tio_printf(" ctrl-t t Send ctrl-t key code");
tio_printf(" ctrl-t T Toggle timestamps");
tio_printf(" ctrl-t L Show lines");
tio_printf(" ctrl-t d Toggle DTR");
tio_printf(" ctrl-t r Toggle RTS");
tio_printf(" ctrl-t v Show version");
break;
case KEY_SHIFT_L:
if (ioctl(tty_fd, TIOCMGET, &state) < 0)
{
error_printf("Could not get line state: %s", strerror(errno));
break;
}
tio_printf("Lines state:");
tio_printf(" DTR: %s", (state & TIOCM_DTR) ? "HIGH" : "LOW");
tio_printf(" RTS: %s", (state & TIOCM_RTS) ? "HIGH" : "LOW");
tio_printf(" CTS: %s", (state & TIOCM_CTS) ? "HIGH" : "LOW");
tio_printf(" DSR: %s", (state & TIOCM_DSR) ? "HIGH" : "LOW");
tio_printf(" DCD: %s", (state & TIOCM_CD) ? "HIGH" : "LOW");
tio_printf(" RI : %s", (state & TIOCM_RI) ? "HIGH" : "LOW");
break;
case KEY_D:
toggle_line("DTR", TIOCM_DTR);
break;
case KEY_R:
toggle_line("RTS", TIOCM_RTS);
break;
case KEY_B:
tcsendbreak(tty_fd, 0);
break;
case KEY_C:
tio_printf("Configuration:");
tio_printf(" TTY device: %s", option.tty_device);
tio_printf(" Baudrate: %u", option.baudrate);
tio_printf(" Databits: %d", option.databits);
tio_printf(" Flow: %s", option.flow);
tio_printf(" Stopbits: %d", option.stopbits);
tio_printf(" Parity: %s", option.parity);
tio_printf(" Local Echo: %s", option.local_echo ? "yes":"no");
tio_printf(" Timestamps: %s", option.timestamp ? "yes" : "no");
tio_printf(" Output delay: %d", option.output_delay);
if (option.map[0] != 0)
tio_printf(" Map flags: %s", option.map);
if (option.log)
tio_printf(" Log file: %s", option.log_filename);
break;
case KEY_E:
option.local_echo = !option.local_echo;
break;
case KEY_F:
file_send();
break;
case KEY_H:
/* Toggle hexadecimal printing mode */
if (print_mode == NORMAL)
{
print = print_hex;
print_mode = HEX;
tio_printf("Switched to hexadecimal mode");
}
else
{
print = print_normal;
print_mode = NORMAL;
tio_printf("Switched to normal mode");
}
break;
case KEY_K:
/* Initializes screen according with msx.keyboard file */
if(load_key_matrix())
display_parallel_keyboard = true;
break;
case KEY_L:
/* Clear screen using ANSI/VT100 escape code */
printf("\033c");
fflush(stdout);
break;
case KEY_Q:
/* Exit upon ctrl-t q sequence */
exit(EXIT_SUCCESS);
case KEY_S:
/* Show tx/rx statistics upon ctrl-t s sequence */
tio_printf("Statistics:");
tio_printf(" Sent %lu bytes, received %lu bytes", tx_total, rx_total);
break;
case KEY_T:
/* Send ctrl-t key code upon ctrl-t t sequence */
*output_char = KEY_CTRL_T;
*forward = true;
break;
case KEY_SHIFT_T:
option.timestamp = !option.timestamp;
break;
case KEY_V:
tio_printf("tio v%s", VERSION);
break;
default:
/* Ignore unknown ctrl-t escaped keys */
break;
}
}
}
void stdin_configure(void)
{
int status;
/* Save current stdin settings */
if (tcgetattr(STDIN_FILENO, &stdin_old) < 0)
{
error_printf("Saving current stdin settings failed");
exit(EXIT_FAILURE);
}
/* Prepare new stdin settings */
memcpy(&stdin_new, &stdin_old, sizeof(stdin_old));
/* Reconfigure stdin (RAW configuration) */
stdin_new.c_iflag &= ~(ICRNL); // Do not translate CR -> NL on input
stdin_new.c_oflag &= ~(OPOST);
stdin_new.c_lflag &= ~(ECHO|ICANON|ISIG|ECHOE|ECHOK|ECHONL);
/* Control characters */
stdin_new.c_cc[VTIME] = 0; /* Inter-character timer unused */
stdin_new.c_cc[VMIN] = 1; /* Blocking read until 1 character received */
/* Activate new stdin settings */
status = tcsetattr(STDIN_FILENO, TCSANOW, &stdin_new);
if (status == -1)
{
error_printf("Could not apply new stdin settings (%s)", strerror(errno));
exit(EXIT_FAILURE);
}
/* Make sure we restore old stdin settings on exit */
atexit(&stdin_restore);
}
void stdin_restore(void)
{
tcsetattr(STDIN_FILENO, TCSANOW, &stdin_old);
}
void stdout_configure(void)
{
int status;
/* Disable line buffering in stdout. This is necessary if we
* want things like local echo to work correctly. */
setbuf(stdout, NULL);
/* Save current stdout settings */
if (tcgetattr(STDOUT_FILENO, &stdout_old) < 0)
{
error_printf("Saving current stdio settings failed");
exit(EXIT_FAILURE);
}
/* Prepare new stdout settings */
memcpy(&stdout_new, &stdout_old, sizeof(stdout_old));
/* Reconfigure stdout (RAW configuration) */
stdout_new.c_oflag &= ~(OPOST);
stdout_new.c_lflag &= ~(ECHO|ICANON|ISIG|ECHOE|ECHOK|ECHONL);
/* Control characters */
stdout_new.c_cc[VTIME] = 0; /* Inter-character timer unused */
//stdout_new.c_cc[VMIN] = 1; /* Blocking read until 1 character received */
stdout_new.c_cc[VMIN] = 0; /* From Evandro: Non blocking read */
/* Activate new stdout settings */
status = tcsetattr(STDOUT_FILENO, TCSANOW, &stdout_new);
if (status == -1)
{
error_printf("Could not apply new stdout settings (%s)", strerror(errno));
exit(EXIT_FAILURE);
}
/* Print launch hints */
tio_printf("tio v%s", VERSION);
tio_printf("Press ctrl-t q to quit");
/* At start use normal print function */
print = print_normal;
/* Make sure we restore old stdout settings on exit */
atexit(&stdout_restore);
}
void stdout_restore(void)
{
tcsetattr(STDOUT_FILENO, TCSANOW, &stdout_old);
}
void tty_configure(void)
{
bool token_found = true;
char *token = NULL;
char *buffer;
int status;
speed_t baudrate;
memset(&tio, 0, sizeof(tio));
/* Set speed */
switch (option.baudrate)
{
/* The macro below expands into switch cases autogenerated by the
* configure script. Each switch case verifies and configures the baud
* rate and is of the form:
*
* case $baudrate: baudrate = B$baudrate; break;
*
* Only switch cases for baud rates detected supported by the host
* system are inserted.
*
* To see which baud rates are being probed see configure.ac
*/
AUTOCONF_BAUDRATE_CASES
default:
#ifdef HAVE_TERMIOS2
standard_baudrate = false;
break;
#else
error_printf("Invalid baud rate");
exit(EXIT_FAILURE);
#endif
}
if (standard_baudrate)
{
// Set input speed
status = cfsetispeed(&tio, baudrate);
if (status == -1)
{
error_printf("Could not configure input speed (%s)", strerror(errno));
exit(EXIT_FAILURE);
}
// Set output speed
status = cfsetospeed(&tio, baudrate);
if (status == -1)
{
error_printf("Could not configure output speed (%s)", strerror(errno));
exit(EXIT_FAILURE);
}
}
/* Set databits */
tio.c_cflag &= ~CSIZE;
switch (option.databits)
{
case 5:
tio.c_cflag |= CS5;
break;
case 6:
tio.c_cflag |= CS6;
break;
case 7:
tio.c_cflag |= CS7;
break;
case 8:
tio.c_cflag |= CS8;
break;
default:
error_printf("Invalid data bits");
exit(EXIT_FAILURE);
}
/* Set flow control */
if (strcmp("hard", option.flow) == 0)
{
tio.c_cflag |= CRTSCTS;
tio.c_iflag &= ~(IXON | IXOFF | IXANY);
}
else if (strcmp("soft", option.flow) == 0)
{
tio.c_cflag &= ~CRTSCTS;
tio.c_iflag |= IXON | IXOFF;
}
else if (strcmp("none", option.flow) == 0)
{
tio.c_cflag &= ~CRTSCTS;
tio.c_iflag &= ~(IXON | IXOFF | IXANY);
}
else
{
error_printf("Invalid flow control");
exit(EXIT_FAILURE);
}
/* Set stopbits */
switch (option.stopbits)
{
case 1:
tio.c_cflag &= ~CSTOPB;
break;
case 2:
tio.c_cflag |= CSTOPB;
break;
default:
error_printf("Invalid stop bits");
exit(EXIT_FAILURE);
}
/* Set parity */
if (strcmp("odd", option.parity) == 0)
{
tio.c_cflag |= PARENB;
tio.c_cflag |= PARODD;
}
else if (strcmp("even", option.parity) == 0)
{
tio.c_cflag |= PARENB;
tio.c_cflag &= ~PARODD;
}
else if (strcmp("none", option.parity) == 0)
tio.c_cflag &= ~PARENB;
else
{
error_printf("Invalid parity");
exit(EXIT_FAILURE);
}
/* Control, input, output, local modes for tty device */
tio.c_cflag |= CLOCAL | CREAD;
tio.c_oflag = 0;
//Canonical mode is disabled, echo, erasure, new-line echo, disable interpretation of INTR, QUIT and SUSP
tio.c_lflag = 0;
/* Control characters */
tio.c_cc[VTIME] = 0; // Inter-character timer unused
//tio.c_cc[VMIN] = 1; // Blocking read until 1 character received
tio.c_cc[VMIN] = 0; // From Evandro: No blocking read
/* Configure any specified input or output mappings */
buffer = strdup(option.map);
while (token_found == true)
{
if (token == NULL)
token = strtok(buffer,",");
else
token = strtok(NULL, ",");
if (token != NULL)
{
if (strcmp(token,"INLCR") == 0)
tio.c_iflag |= INLCR;
else if (strcmp(token,"IGNCR") == 0)
tio.c_iflag |= IGNCR;
else if (strcmp(token,"ICRNL") == 0)
tio.c_iflag |= ICRNL;
else if (strcmp(token,"OCRNL") == 0)
map_o_cr_nl = true;
else if (strcmp(token,"ODELBS") == 0)
map_o_del_bs = true;
else if (strcmp(token,"INLCRNL") == 0)
map_i_nl_crnl = true;
else if (strcmp(token, "ONLCRNL") == 0)
map_o_nl_crnl = true;
else
{
printf("Error: Unknown mapping flag %s\n", token);
exit(EXIT_FAILURE);
}
}
else
token_found = false;
}
free(buffer);
}
void tty_wait_for_device(void)
{
fd_set rdfs;
int status;
struct timeval tv;
static char input_char, previous_char = 0;
static bool first = true;
static int last_errno = 0;
/* Loop until device pops up */
while (true)
{
if (first)
{
/* Don't wait first time */
tv.tv_sec = 0;
tv.tv_usec = 1;
first = false;
} else
{
/* Wait up to 1 second */
tv.tv_sec = 1;
tv.tv_usec = 0;
}
FD_ZERO(&rdfs);
FD_SET(STDIN_FILENO, &rdfs);
/* Block until input becomes available or timeout */
status = select(STDIN_FILENO + 1, &rdfs, NULL, NULL, &tv);
if (status > 0)
{
/* Input from stdin ready */
/* Read one character */
status = read(STDIN_FILENO, &input_char, 1);
//if (status <= 0)
if (status < 0)
{
error_printf("Could not read from stdin");
exit(EXIT_FAILURE);
}
if (status > 0) //From Evandro: New, as altertion to non blocking read
{
/* Handle commands */
handle_command_sequence(input_char, previous_char, NULL, NULL);
previous_char = input_char;
}
} else if (status == -1)
{
error_printf("select() failed (%s)", strerror(errno));
exit(EXIT_FAILURE);
}
/* Test for accessible device file */
int rc = access(option.tty_device, R_OK);
if (rc == 0) {
last_errno = 0;
return;
}
else if (last_errno != errno) {
tio_printf("%s: %s. Waiting...", option.tty_device, strerror(errno));
last_errno = errno;
}
}
}
void tty_restore(void)
{
tcsetattr(tty_fd, TCSANOW, &tio_old);
if (connected)
tty_disconnect();
}
static void optional_local_echo(char c)
{
if (!option.local_echo)
return;
print(c);
fflush(stdout);
if (option.log)
log_write(c);
}
void tty_disconnect(void)
{
if (connected)
{
tio_printf("Disconnected");
flock(tty_fd, LOCK_UN);
close(tty_fd);
connected = false;
}
}
int tty_connect(void)
{
fd_set rdfs; /* Read file descriptor set */
int maxfd; /* Maximum file descriptor used */
char input_char, output_char;
static char previous_char = 0;
static bool first = true;
int status;
time_t next_timestamp = 0;
char* now = NULL;
char mount_string[16]; //Evandro. This variable has to be known inside this context, updated by check_input_char
/* Open tty device */
#ifdef __APPLE__
tty_fd = open(option.tty_device, O_RDWR | O_NOCTTY | O_NONBLOCK );
#else
tty_fd = open(option.tty_device, O_RDWR | O_NOCTTY);
#endif
if (tty_fd < 0)
{
error_printf_silent("Could not open tty device (%s)", strerror(errno));
goto error_open;
}
/* Make sure device is of tty type */
if (!isatty(tty_fd))
{
error_printf("Not a tty device");
exit(EXIT_FAILURE);;
}
/* Lock device file */
status = flock(tty_fd, LOCK_EX | LOCK_NB);
if ((status == -1) && (errno == EWOULDBLOCK))
{
error_printf("Device file is locked by another process");
exit(EXIT_FAILURE);
}
/* Flush stale I/O data (if any) */
tcflush(tty_fd, TCIOFLUSH);
/* Print connect status */
tio_printf("Connected");
connected = true;
tainted = false;
if (option.timestamp)
next_timestamp = time(NULL);
/* Save current port settings */
if (tcgetattr(tty_fd, &tio_old) < 0)
goto error_tcgetattr;
/* Make sure we restore tty settings on exit */
if (first)
{
atexit(&tty_restore);
first = false;
}
/* Activate new port settings */
status = tcsetattr(tty_fd, TCSANOW, &tio);
if (status == -1)
{
error_printf_silent("Could not apply port settings (%s)", strerror(errno));
goto error_tcsetattr;
}
#ifdef HAVE_TERMIOS2
if (!standard_baudrate)
{
if (setspeed2(tty_fd, option.baudrate) != 0)
{
error_printf_silent("Could not set baudrate speed (%s)", strerror(errno));
goto error_setspeed2;
}
}
#endif
maxfd = MAX(tty_fd, STDIN_FILENO) + 1; /* Maximum bit entry (tty_fd) to test */
display_parallel_keyboard = false; //Evandro. The default is work as standard terminal
mount_string[0] = 0; //Evandro.
/* Input loop */
while (true)
{
FD_ZERO(&rdfs);
FD_SET(tty_fd, &rdfs);
FD_SET(STDIN_FILENO, &rdfs);
/* Block until input becomes available */
status = select(maxfd, &rdfs, NULL, NULL, NULL);
if (status > 0)
{
if (FD_ISSET(tty_fd, &rdfs))
{
/* Input from tty device ready */
if (read(tty_fd, &input_char, 1) > 0)
{
/* Update receive statistics */
rx_total++;
/* From Evandro => Start Display pressed keys from MSX Keyboard Emulator readings */
if(display_parallel_keyboard)
{
/* Parse reception to present on key maping */
check_input_char(input_char, mount_string);
}
else //if(display_parallel_keyboard)
/* From Evandro => End Display pressed keys from MSX Keyboard Emulator readings */
{
/* Print timestamp on new line, if desired. */
if (next_timestamp && input_char != '\n' && input_char != '\r')
{
now = current_time();
fprintf(stdout, ANSI_COLOR_GRAY "[%s] " ANSI_COLOR_RESET, now);
if (option.log)
{
log_write('[');
while (*now != '\0')
{
log_write(*now);
++now;
}
log_write(']');
log_write(' ');
}
next_timestamp = 0;
}
/* Map input character */
if ((input_char == '\n') && (map_i_nl_crnl))
{
print('\r');
print('\n');
if (option.timestamp)
next_timestamp = time(NULL);
} else
{
/* Print received tty character to stdout */
print(input_char);
}
//fflush(stdout);
/* Write to log */
if (option.log)
log_write(input_char);
tainted = true;
if (input_char == '\n' && option.timestamp)
next_timestamp = time(NULL);
} //else if(display_parallel_keyboard)
} else //if (read(tty_fd, &input_char, 1) > 0)
{
/* Error reading - device is likely unplugged */
error_printf_silent("Could not read from tty device");
goto error_read;
} // else if (read(tty_fd, &input_char, 1) > 0)
}
if (FD_ISSET(STDIN_FILENO, &rdfs))
{
bool forward = true;
/* Input from stdin ready */
status = read(STDIN_FILENO, &input_char, 1);
if (status <= 0)
{
error_printf_silent("Could not read from stdin");
goto error_read;
}
/* Forward input to output except ctrl-t key */
output_char = input_char;
if (input_char == KEY_CTRL_T)
forward = false;
/* Check abort <Esc> */
if ( display_parallel_keyboard && (input_char == TIO_ABORT) )
{
//Stops keyboard scan presentation and moves cursor to the last line of the screen
display_parallel_keyboard = false;
forward = false;
printf("\x1B[%d;%dH\r\n",23, 0);
}
/* Handle commands */
handle_command_sequence(input_char, previous_char, &output_char, &forward);
if (forward)
{
/* Map output character */
if ((output_char == 127) && (map_o_del_bs))
output_char = '\b';
if ((output_char == '\r') && (map_o_cr_nl))
output_char = '\n';
/* Map newline character */
if ((output_char == '\n') && (map_o_nl_crnl)) {
char r = '\r';
optional_local_echo(r);
status = write(tty_fd, &r, 1);
if (status < 0)
warning_printf("Could not write to tty device");
tx_total++;
delay(option.output_delay);
}
/* Send output to tty device */
optional_local_echo(output_char);
status = write(tty_fd, &output_char, 1);
if (status < 0)
warning_printf("Could not write to tty device");
fsync(tty_fd);
/* Update transmit statistics */
tx_total++;
/* Insert output delay */
delay(option.output_delay);
}
/* Save previous key */
previous_char = input_char;
} // if (FD_ISSET(STDIN_FILENO, &rdfs))
} //if (status > 0) /* Block until input becomes available */
else if (status == -1)
{
error_printf("Error: select() failed (%s)", strerror(errno));
exit(EXIT_FAILURE);
} //else if (status > 0) /* Block until input becomes available */
} /* Input loop */
return TIO_SUCCESS;
#ifdef HAVE_TERMIOS2
error_setspeed2:
#endif
error_tcsetattr:
error_tcgetattr:
error_read:
tty_disconnect();
error_open:
return TIO_ERROR;
}