-
Notifications
You must be signed in to change notification settings - Fork 0
/
tty.c
1544 lines (1381 loc) · 42.3 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
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* tio - a simple serial terminal I/O tool
*
* Copyright (c) 2014-2022 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 <ctype.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 <sys/socket.h>
#include <sys/un.h>
#include <fcntl.h>
#include <termios.h>
#include <stdbool.h>
#include <errno.h>
#include <time.h>
#include <dirent.h>
#include <pthread.h>
#include "config.h"
#include "configfile.h"
#include "tty.h"
#include "print.h"
#include "options.h"
#include "misc.h"
#include "log.h"
#include "error.h"
#include "socket.h"
#include "setspeed.h"
#include "rs485.h"
#include "alert.h"
#include "timestamp.h"
#include "misc.h"
#if defined(__APPLE__)
#define PATH_SERIAL_DEVICES "/dev/"
#define PREFIX_TTY_DEVICES "tty."
#elif defined(__CYGWIN__)
#define PATH_SERIAL_DEVICES "/dev/"
#define PREFIX_TTY_DEVICES "ttyS"
#elif defined(__HAIKU__)
#define PATH_SERIAL_DEVICES "/dev/ports/"
#define PREFIX_TTY_DEVICES ""
#else
#define PATH_SERIAL_DEVICES "/dev/serial/by-id/"
#define PREFIX_TTY_DEVICES ""
#endif
#ifndef CMSPAR
#define CMSPAR 010000000000
#endif
#define KEY_0 0x30
#define KEY_1 0x31
#define KEY_2 0x32
#define KEY_3 0x33
#define KEY_4 0x34
#define KEY_5 0x35
#define KEY_QUESTION 0x3f
#define KEY_B 0x62
#define KEY_C 0x63
#define KEY_E 0x65
#define KEY_F 0x66
#define KEY_SHIFT_F 0x46
#define KEY_G 0x67
#define KEY_H 0x68
#define KEY_L 0x6C
#define KEY_SHIFT_L 0x4C
#define KEY_M 0x6D
#define KEY_P 0x70
#define KEY_Q 0x71
#define KEY_S 0x73
#define KEY_T 0x74
#define KEY_U 0x55
#define KEY_V 0x76
#define KEY_Z 0x7a
enum line_mode_t
{
LINE_OFF,
LINE_TOGGLE,
LINE_PULSE
};
const char random_array[] =
{
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x28, 0x20, 0x28, 0x0A, 0x20,
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x29, 0x20, 0x29, 0x0A, 0x20,
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E, 0x2E,
0x2E, 0x0A, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x7C, 0x20, 0x20, 0x20,
0x20, 0x20, 0x20, 0x7C, 0x5D, 0x0A, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
0x5C, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x2F, 0x0A, 0x20, 0x20, 0x20, 0x20,
0x20, 0x20, 0x20, 0x20, 0x60, 0x2D, 0x2D, 0x2D, 0x2D, 0x27, 0x0A, 0x0A, 0x54,
0x69, 0x6D, 0x65, 0x20, 0x66, 0x6F, 0x72, 0x20, 0x61, 0x20, 0x63, 0x6F, 0x66,
0x66, 0x65, 0x65, 0x20, 0x62, 0x72, 0x65, 0x61, 0x6B, 0x21, 0x0A, 0x20, 0x0A,
0x00
};
bool interactive_mode = true;
static struct termios tio, tio_old, stdout_new, stdout_old, stdin_new, stdin_old;
static unsigned long rx_total = 0, tx_total = 0;
static bool connected = false;
static bool standard_baudrate = true;
static void (*print)(char c);
static int fd;
static bool map_i_nl_crnl = false;
static bool map_o_cr_nl = false;
static bool map_o_nl_crnl = false;
static bool map_o_del_bs = false;
static bool map_o_ltu = false;
static bool map_o_msblsb = false;
static char hex_chars[2];
static unsigned char hex_char_index = 0;
static char tty_buffer[BUFSIZ*2];
static size_t tty_buffer_count = 0;
static char *tty_buffer_write_ptr = tty_buffer;
static pthread_t thread;
static int pipefd[2];
static pthread_mutex_t mutex_input_ready = PTHREAD_MUTEX_INITIALIZER;
static void optional_local_echo(char c)
{
if (!option.local_echo)
{
return;
}
print(c);
if (option.log)
{
log_putc(c);
}
}
inline static bool is_valid_hex(char c)
{
return ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'));
}
inline static unsigned char char_to_nibble(char c)
{
if(c >= '0' && c <= '9')
{
return c - '0';
}
else if (c >= 'a' && c <= 'f')
{
return c - 'a' + 10;
}
else if (c >= 'A' && c <= 'F')
{
return c - 'A' + 10;
}
else
{
return 0;
}
}
void tty_sync(int fd)
{
ssize_t count;
while (tty_buffer_count > 0)
{
count = write(fd, tty_buffer, tty_buffer_count);
if (count < 0)
{
// Error
tio_debug_printf("Write error while flushing tty buffer (%s)", strerror(errno));
break;
}
tty_buffer_count -= count;
fsync(fd);
tcdrain(fd);
}
// Reset
tty_buffer_write_ptr = tty_buffer;
tty_buffer_count = 0;
}
ssize_t tty_write(int fd, const void *buffer, size_t count)
{
ssize_t retval = 0, bytes_written = 0;
size_t i;
if (map_o_ltu)
{
// Convert lower case to upper case
for (i = 0; i<count; i++)
{
*((unsigned char*)buffer+i) = toupper(*((unsigned char*)buffer+i));
}
}
if (option.output_delay || option.output_line_delay)
{
// Write byte by byte with output delay
for (i=0; i<count; i++)
{
retval = write(fd, buffer, 1);
if (retval < 0)
{
// Error
tio_debug_printf("Write error (%s)", strerror(errno));
break;
}
bytes_written += retval;
if (option.output_line_delay && *(unsigned char*)buffer == '\n')
{
delay(option.output_line_delay);
}
fsync(fd);
tcdrain(fd);
if (option.output_delay)
{
delay(option.output_delay);
}
}
}
else
{
// Force write of tty buffer if too full
if ((tty_buffer_count + count) > BUFSIZ)
{
tty_sync(fd);
}
// Copy bytes to tty write buffer
memcpy(tty_buffer_write_ptr, buffer, count);
tty_buffer_write_ptr += count;
tty_buffer_count += count;
bytes_written = count;
}
return bytes_written;
}
void *tty_stdin_input_thread(void *arg)
{
UNUSED(arg);
char input_buffer[BUFSIZ];
ssize_t byte_count;
ssize_t bytes_written;
// Create FIFO pipe
if (pipe(pipefd) == -1)
{
tio_error_printf("Failed to create pipe");
exit(EXIT_FAILURE);
}
// Signal that input pipe is ready
pthread_mutex_unlock(&mutex_input_ready);
// Input loop for stdin
while (1)
{
/* Input from stdin ready */
byte_count = read(STDIN_FILENO, input_buffer, BUFSIZ);
if (byte_count < 0)
{
tio_warning_printf("Could not read from stdin (%s)", strerror(errno));
}
else if (byte_count == 0)
{
// Close write end to signal EOF in read end
close(pipefd[1]);
pthread_exit(0);
}
if (interactive_mode)
{
static char previous_char = 0;
char input_char;
// Process quit and flush key command
for (int i = 0; i<byte_count; i++)
{
input_char = input_buffer[i];
if (previous_char == option.prefix_code)
{
if (input_char == option.prefix_code)
{
previous_char = 0;
continue;
}
switch (input_char)
{
case KEY_Q:
exit(EXIT_SUCCESS);
break;
case KEY_SHIFT_F:
tio_printf("Flushed data I/O channels")
tcflush(fd, TCIOFLUSH);
break;
default:
break;
}
}
previous_char = input_char;
}
}
// Write all bytes read to pipe
while (byte_count)
{
bytes_written = write(pipefd[1], input_buffer, byte_count);
if (bytes_written < 0)
{
tio_warning_printf("Could not write to pipe (%s)", strerror(errno));
break;
}
byte_count -= bytes_written;
}
}
pthread_exit(0);
}
void tty_input_thread_create(void)
{
pthread_mutex_lock(&mutex_input_ready);
if (pthread_create(&thread, NULL, tty_stdin_input_thread, NULL) != 0) {
tio_error_printf("pthread_create() error");
exit(1);
}
}
void tty_input_thread_wait_ready(void)
{
pthread_mutex_lock(&mutex_input_ready);
}
static void output_hex(char c)
{
hex_chars[hex_char_index++] = c;
printf("%c", c);
if (hex_char_index == 2)
{
usleep(100*1000);
printf("\b \b");
printf("\b \b");
unsigned char hex_value = char_to_nibble(hex_chars[0]) << 4 | (char_to_nibble(hex_chars[1]) & 0x0F);
hex_char_index = 0;
optional_local_echo(hex_value);
ssize_t status = tty_write(fd, &hex_value, 1);
if (status < 0)
{
tio_warning_printf("Could not write to tty device");
}
else
{
tx_total++;
}
}
}
static void toggle_line(const char *line_name, int mask, enum line_mode_t line_mode)
{
int state;
if (line_mode == LINE_TOGGLE)
{
// Toggle line
if (ioctl(fd, TIOCMGET, &state) < 0)
{
tio_warning_printf("Could not get line state (%s)", strerror(errno));
}
else
{
if (state & mask)
{
state &= ~mask;
tio_printf("Setting %s to LOW", line_name);
}
else
{
state |= mask;
tio_printf("Setting %s to HIGH", line_name);
}
if (ioctl(fd, TIOCMSET, &state) < 0)
tio_warning_printf("Could not set line state (%s)", strerror(errno));
}
} else if (line_mode == LINE_PULSE)
{
int duration = 0;
// Pulse line
toggle_line(line_name, mask, LINE_TOGGLE);
switch (mask)
{
case TIOCM_DTR:
duration = option.dtr_pulse_duration;
break;
case TIOCM_RTS:
duration = option.rts_pulse_duration;
break;
case TIOCM_CTS:
duration = option.cts_pulse_duration;
break;
case TIOCM_DSR:
duration = option.dsr_pulse_duration;
break;
case TIOCM_CD:
duration = option.dcd_pulse_duration;
break;
case TIOCM_RI:
duration = option.ri_pulse_duration;
break;
default:
duration = 0;
break;
}
if (duration > 0)
{
tio_printf("Waiting %d ms", duration);
delay(duration);
}
toggle_line(line_name, mask, LINE_TOGGLE);
}
}
void handle_command_sequence(char input_char, char *output_char, bool *forward)
{
char unused_char;
bool unused_bool;
int state;
static enum line_mode_t line_mode = LINE_OFF;
static char previous_char = 0;
/* Ignore unused arguments */
if (output_char == NULL)
{
output_char = &unused_char;
}
if (forward == NULL)
{
forward = &unused_bool;
}
if (line_mode)
{
// Handle line toggle number action
*forward = false;
switch (input_char)
{
case KEY_0:
toggle_line("DTR", TIOCM_DTR, line_mode);
break;
case KEY_1:
toggle_line("RTS", TIOCM_RTS, line_mode);
break;
case KEY_2:
toggle_line("CTS", TIOCM_CTS, line_mode);
break;
case KEY_3:
toggle_line("DSR", TIOCM_DSR, line_mode);
break;
case KEY_4:
toggle_line("DCD", TIOCM_CD, line_mode);
break;
case KEY_5:
toggle_line("RI", TIOCM_RI, line_mode);
break;
default:
tio_warning_printf("Invalid line number");
break;
}
line_mode = LINE_OFF;
return;
}
/* Handle escape key commands */
if (previous_char == option.prefix_code)
{
/* Do not forward input char to output by default */
*forward = false;
/* Handle special double prefix key input case */
if (input_char == option.prefix_code)
{
/* Forward prefix character to tty */
*output_char = option.prefix_code;
*forward = true;
previous_char = 0;
return;
}
switch (input_char)
{
case KEY_QUESTION:
tio_printf("Key commands:");
tio_printf(" ctrl-%c ? List available key commands", option.prefix_key);
tio_printf(" ctrl-%c b Send break", option.prefix_key);
tio_printf(" ctrl-%c c Show configuration", option.prefix_key);
tio_printf(" ctrl-%c e Toggle local echo mode", option.prefix_key);
tio_printf(" ctrl-%c f Toggle log to file", option.prefix_key);
tio_printf(" ctrl-%c F Flush data I/O buffers", option.prefix_key);
tio_printf(" ctrl-%c g Toggle serial port line", option.prefix_key);
tio_printf(" ctrl-%c h Toggle hexadecimal mode", option.prefix_key);
tio_printf(" ctrl-%c l Clear screen", option.prefix_key);
tio_printf(" ctrl-%c L Show line states", option.prefix_key);
tio_printf(" ctrl-%c m Toggle MSB to LSB bit order", option.prefix_key);
tio_printf(" ctrl-%c p Pulse serial port line", option.prefix_key);
tio_printf(" ctrl-%c q Quit", option.prefix_key);
tio_printf(" ctrl-%c s Show statistics", option.prefix_key);
tio_printf(" ctrl-%c t Toggle line timestamp mode", option.prefix_key);
tio_printf(" ctrl-%c U Toggle conversion to uppercase on output", option.prefix_key);
tio_printf(" ctrl-%c v Show version", option.prefix_key);
tio_printf(" ctrl-%c ctrl-%c Send ctrl-%c character", option.prefix_key, option.prefix_key, option.prefix_key);
break;
case KEY_SHIFT_L:
if (ioctl(fd, TIOCMGET, &state) < 0)
{
tio_warning_printf("Could not get line state (%s)", strerror(errno));
break;
}
tio_printf("Line states:");
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_F:
if (option.log)
{
log_close();
option.log = false;
}
else
{
if (log_open(option.log_filename) == 0)
{
option.log = true;
}
}
tio_printf("Switched log to file %s", option.log ? "on" : "off");
break;
case KEY_SHIFT_F:
break;
case KEY_G:
tio_printf("Please enter which serial line number to toggle:");
tio_printf(" DTR (0)");
tio_printf(" RTS (1)");
tio_printf(" CTS (2)");
tio_printf(" DSR (3)");
tio_printf(" DCD (4)");
tio_printf(" RI (5)");
// Process next input character as part of the line toggle step
line_mode = LINE_TOGGLE;
break;
case KEY_P:
tio_printf("Please enter which serial line number to pulse:");
tio_printf(" DTR (0)");
tio_printf(" RTS (1)");
tio_printf(" CTS (2)");
tio_printf(" DSR (3)");
tio_printf(" DCD (4)");
tio_printf(" RI (5)");
// Process next input character as part of the line pulse step
line_mode = LINE_PULSE;
break;
case KEY_B:
tcsendbreak(fd, 0);
break;
case KEY_C:
tio_printf("Configuration:");
options_print();
config_file_print();
if (option.rs485)
{
rs485_print_config();
}
break;
case KEY_E:
option.local_echo = !option.local_echo;
tio_printf("Switched local echo %s", option.local_echo ? "on" : "off");
break;
case KEY_H:
/* Toggle hexadecimal printing mode */
if (!option.hex_mode)
{
print = print_hex;
option.hex_mode = true;
tio_printf("Switched to hexadecimal mode");
}
else
{
print = print_normal;
option.hex_mode = false;
tio_printf("Switched to normal mode");
}
break;
case KEY_L:
/* Clear screen using ANSI/VT100 escape code */
printf("\033c");
break;
case KEY_M:
/* Toggle bit order */
if (!map_o_msblsb)
{
map_o_msblsb = true;
tio_printf("Switched to reverse bit order");
}
else
{
map_o_msblsb = false;
tio_printf("Switched to normal bit order");
}
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", tx_total);
tio_printf(" Received %lu bytes", rx_total);
break;
case KEY_T:
option.timestamp += 1;
switch (option.timestamp)
{
case TIMESTAMP_NONE:
break;
case TIMESTAMP_24HOUR:
tio_printf("Switched to 24hour timestamp mode");
break;
case TIMESTAMP_24HOUR_START:
tio_printf("Switched to 24hour-start timestamp mode");
break;
case TIMESTAMP_24HOUR_DELTA:
tio_printf("Switched to 24hour-delta timestamp mode");
break;
case TIMESTAMP_ISO8601:
tio_printf("Switched to iso8601 timestamp mode");
break;
case TIMESTAMP_END:
option.timestamp = TIMESTAMP_NONE;
tio_printf("Switched timestamp off");
break;
}
break;
case KEY_U:
map_o_ltu = !map_o_ltu;
break;
case KEY_V:
tio_printf("tio v%s", VERSION);
break;
case KEY_Z:
tio_printf_array(random_array);
break;
default:
/* Ignore unknown ctrl-t escaped keys */
break;
}
}
previous_char = input_char;
}
void stdin_restore(void)
{
tcsetattr(STDIN_FILENO, TCSANOW, &stdin_old);
}
void stdin_configure(void)
{
int status;
/* Save current stdin settings */
if (tcgetattr(STDIN_FILENO, &stdin_old) < 0)
{
tio_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) */
cfmakeraw(&stdin_new);
/* 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)
{
tio_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 stdout_restore(void)
{
tcsetattr(STDOUT_FILENO, TCSANOW, &stdout_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. */
setvbuf(stdout, NULL, _IONBF, 0);
/* Save current stdout settings */
if (tcgetattr(STDOUT_FILENO, &stdout_old) < 0)
{
tio_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) */
cfmakeraw(&stdout_new);
/* Allow ^C / SIGINT (to allow termination when piping to tio) */
if (!interactive_mode)
{
stdout_new.c_lflag |= ISIG;
}
/* Control characters */
stdout_new.c_cc[VTIME] = 0; /* Inter-character timer unused */
stdout_new.c_cc[VMIN] = 1; /* Blocking read until 1 character received */
/* Activate new stdout settings */
status = tcsetattr(STDOUT_FILENO, TCSANOW, &stdout_new);
if (status == -1)
{
tio_error_printf("Could not apply new stdout settings (%s)", strerror(errno));
exit(EXIT_FAILURE);
}
/* At start use normal print function */
print = print_normal;
/* Make sure we restore old stdout settings on exit */
atexit(&stdout_restore);
}
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 meson
* configure. 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 meson.build
*/
BAUDRATE_CASES
default:
#if defined (HAVE_TERMIOS2) || defined (HAVE_IOSSIOSPEED)
standard_baudrate = false;
break;
#else
tio_error_printf("Invalid baud rate");
exit(EXIT_FAILURE);
#endif
}
if (standard_baudrate)
{
// Set input speed
status = cfsetispeed(&tio, baudrate);
if (status == -1)
{
tio_error_printf("Could not configure input speed (%s)", strerror(errno));
exit(EXIT_FAILURE);
}
// Set output speed
status = cfsetospeed(&tio, baudrate);
if (status == -1)
{
tio_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:
tio_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
{
tio_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:
tio_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 if ( strcmp("mark", option.parity) == 0)
{
tio.c_cflag |= PARENB;
tio.c_cflag |= PARODD;
tio.c_cflag |= CMSPAR;
}
else if ( strcmp("space", option.parity) == 0)
{
tio.c_cflag |= PARENB;
tio.c_cflag &= ~PARODD;
tio.c_cflag |= CMSPAR;
}
else
{
tio_error_printf("Invalid parity");
exit(EXIT_FAILURE);
}
/* Control, input, output, local modes for tty device */
tio.c_cflag |= CLOCAL | CREAD;
tio.c_oflag = 0;
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
/* 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)
{