forked from rmyorston/busybox-w32
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stty.c
1530 lines (1417 loc) · 42 KB
/
stty.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
/* vi: set sw=4 ts=4: */
/* stty -- change and print terminal line settings
Copyright (C) 1990-1999 Free Software Foundation, Inc.
Licensed under GPLv2 or later, see file LICENSE in this source tree.
*/
/* Usage: stty [-ag] [-F device] [setting...]
Options:
-a Write all current settings to stdout in human-readable form.
-g Write all current settings to stdout in stty-readable form.
-F Open and use the specified device instead of stdin
If no args are given, write to stdout the baud rate and settings that
have been changed from their defaults. Mode reading and changes
are done on the specified device, or stdin if none was specified.
David MacKenzie <[email protected]>
Special for busybox ported by Vladimir Oleynik <[email protected]> 2001
*/
#include "libbb.h"
#ifndef _POSIX_VDISABLE
# define _POSIX_VDISABLE ((unsigned char) 0)
#endif
#define Control(c) ((c) & 0x1f)
/* Canonical values for control characters */
#ifndef CINTR
# define CINTR Control('c')
#endif
#ifndef CQUIT
# define CQUIT 28
#endif
#ifndef CERASE
# define CERASE 127
#endif
#ifndef CKILL
# define CKILL Control('u')
#endif
#ifndef CEOF
# define CEOF Control('d')
#endif
#ifndef CEOL
# define CEOL _POSIX_VDISABLE
#endif
#ifndef CSTART
# define CSTART Control('q')
#endif
#ifndef CSTOP
# define CSTOP Control('s')
#endif
#ifndef CSUSP
# define CSUSP Control('z')
#endif
#if defined(VEOL2) && !defined(CEOL2)
# define CEOL2 _POSIX_VDISABLE
#endif
/* ISC renamed swtch to susp for termios, but we'll accept either name */
#if defined(VSUSP) && !defined(VSWTCH)
# define VSWTCH VSUSP
# define CSWTCH CSUSP
#endif
#if defined(VSWTCH) && !defined(CSWTCH)
# define CSWTCH _POSIX_VDISABLE
#endif
/* SunOS 5.3 loses (^Z doesn't work) if 'swtch' is the same as 'susp'.
So the default is to disable 'swtch.' */
#if defined(__sparc__) && defined(__svr4__)
# undef CSWTCH
# define CSWTCH _POSIX_VDISABLE
#endif
#if defined(VWERSE) && !defined(VWERASE) /* AIX-3.2.5 */
# define VWERASE VWERSE
#endif
#if defined(VDSUSP) && !defined(CDSUSP)
# define CDSUSP Control('y')
#endif
#if !defined(VREPRINT) && defined(VRPRNT) /* Irix 4.0.5 */
# define VREPRINT VRPRNT
#endif
#if defined(VREPRINT) && !defined(CRPRNT)
# define CRPRNT Control('r')
#endif
#if defined(VWERASE) && !defined(CWERASE)
# define CWERASE Control('w')
#endif
#if defined(VLNEXT) && !defined(CLNEXT)
# define CLNEXT Control('v')
#endif
#if defined(VDISCARD) && !defined(VFLUSHO)
# define VFLUSHO VDISCARD
#endif
#if defined(VFLUSH) && !defined(VFLUSHO) /* Ultrix 4.2 */
# define VFLUSHO VFLUSH
#endif
#if defined(CTLECH) && !defined(ECHOCTL) /* Ultrix 4.3 */
# define ECHOCTL CTLECH
#endif
#if defined(TCTLECH) && !defined(ECHOCTL) /* Ultrix 4.2 */
# define ECHOCTL TCTLECH
#endif
#if defined(CRTKIL) && !defined(ECHOKE) /* Ultrix 4.2 and 4.3 */
# define ECHOKE CRTKIL
#endif
#if defined(VFLUSHO) && !defined(CFLUSHO)
# define CFLUSHO Control('o')
#endif
#if defined(VSTATUS) && !defined(CSTATUS)
# define CSTATUS Control('t')
#endif
/* Save us from #ifdef forest plague */
#ifndef BSDLY
# define BSDLY 0
#endif
#ifndef CIBAUD
# define CIBAUD 0
#endif
#ifndef CRDLY
# define CRDLY 0
#endif
#ifndef CRTSCTS
# define CRTSCTS 0
#endif
#ifndef ECHOCTL
# define ECHOCTL 0
#endif
#ifndef ECHOKE
# define ECHOKE 0
#endif
#ifndef ECHOPRT
# define ECHOPRT 0
#endif
#ifndef FFDLY
# define FFDLY 0
#endif
#ifndef IEXTEN
# define IEXTEN 0
#endif
#ifndef IMAXBEL
# define IMAXBEL 0
#endif
#ifndef IUCLC
# define IUCLC 0
#endif
#ifndef IXANY
# define IXANY 0
#endif
#ifndef NLDLY
# define NLDLY 0
#endif
#ifndef OCRNL
# define OCRNL 0
#endif
#ifndef OFDEL
# define OFDEL 0
#endif
#ifndef OFILL
# define OFILL 0
#endif
#ifndef OLCUC
# define OLCUC 0
#endif
#ifndef ONLCR
# define ONLCR 0
#endif
#ifndef ONLRET
# define ONLRET 0
#endif
#ifndef ONOCR
# define ONOCR 0
#endif
#ifndef OXTABS
# define OXTABS 0
#endif
#ifndef TABDLY
# define TABDLY 0
#endif
#ifndef TAB1
# define TAB1 0
#endif
#ifndef TAB2
# define TAB2 0
#endif
#ifndef TOSTOP
# define TOSTOP 0
#endif
#ifndef VDSUSP
# define VDSUSP 0
#endif
#ifndef VEOL2
# define VEOL2 0
#endif
#ifndef VFLUSHO
# define VFLUSHO 0
#endif
#ifndef VLNEXT
# define VLNEXT 0
#endif
#ifndef VREPRINT
# define VREPRINT 0
#endif
#ifndef VSTATUS
# define VSTATUS 0
#endif
#ifndef VSWTCH
# define VSWTCH 0
#endif
#ifndef VTDLY
# define VTDLY 0
#endif
#ifndef VWERASE
# define VWERASE 0
#endif
#ifndef XCASE
# define XCASE 0
#endif
/* Which speeds to set */
enum speed_setting {
input_speed, output_speed, both_speeds
};
/* Which member(s) of 'struct termios' a mode uses */
enum {
/* Do NOT change the order or values, as mode_type_flag()
* depends on them */
control, input, output, local, combination
};
/* Flags for 'struct mode_info' */
#define SANE_SET 1 /* Set in 'sane' mode */
#define SANE_UNSET 2 /* Unset in 'sane' mode */
#define REV 4 /* Can be turned off by prepending '-' */
#define OMIT 8 /* Don't display value */
/* Each mode.
* This structure should be kept as small as humanly possible.
*/
struct mode_info {
const uint8_t type; /* Which structure element to change */
const uint8_t flags; /* Setting and display options */
/* only these values are ever used, so... */
#if (CSIZE | NLDLY | CRDLY | TABDLY | BSDLY | VTDLY | FFDLY) < 0x100
const uint8_t mask;
#elif (CSIZE | NLDLY | CRDLY | TABDLY | BSDLY | VTDLY | FFDLY) < 0x10000
const uint16_t mask;
#else
const tcflag_t mask; /* Other bits to turn off for this mode */
#endif
/* was using short here, but ppc32 was unhappy */
const tcflag_t bits; /* Bits to set for this mode */
};
enum {
/* Must match mode_name[] and mode_info[] order! */
IDX_evenp = 0,
IDX_parity,
IDX_oddp,
IDX_nl,
IDX_ek,
IDX_sane,
IDX_cooked,
IDX_raw,
IDX_pass8,
IDX_litout,
IDX_cbreak,
IDX_crt,
IDX_dec,
#if IXANY
IDX_decctlq,
#endif
#if TABDLY || OXTABS
IDX_tabs,
#endif
#if XCASE && IUCLC && OLCUC
IDX_lcase,
IDX_LCASE,
#endif
};
#define MI_ENTRY(N,T,F,B,M) N "\0"
/* Mode names given on command line */
static const char mode_name[] =
MI_ENTRY("evenp", combination, REV | OMIT, 0, 0 )
MI_ENTRY("parity", combination, REV | OMIT, 0, 0 )
MI_ENTRY("oddp", combination, REV | OMIT, 0, 0 )
MI_ENTRY("nl", combination, REV | OMIT, 0, 0 )
MI_ENTRY("ek", combination, OMIT, 0, 0 )
MI_ENTRY("sane", combination, OMIT, 0, 0 )
MI_ENTRY("cooked", combination, REV | OMIT, 0, 0 )
MI_ENTRY("raw", combination, REV | OMIT, 0, 0 )
MI_ENTRY("pass8", combination, REV | OMIT, 0, 0 )
MI_ENTRY("litout", combination, REV | OMIT, 0, 0 )
MI_ENTRY("cbreak", combination, REV | OMIT, 0, 0 )
MI_ENTRY("crt", combination, OMIT, 0, 0 )
MI_ENTRY("dec", combination, OMIT, 0, 0 )
#if IXANY
MI_ENTRY("decctlq", combination, REV | OMIT, 0, 0 )
#endif
#if TABDLY || OXTABS
MI_ENTRY("tabs", combination, REV | OMIT, 0, 0 )
#endif
#if XCASE && IUCLC && OLCUC
MI_ENTRY("lcase", combination, REV | OMIT, 0, 0 )
MI_ENTRY("LCASE", combination, REV | OMIT, 0, 0 )
#endif
MI_ENTRY("parenb", control, REV, PARENB, 0 )
MI_ENTRY("parodd", control, REV, PARODD, 0 )
MI_ENTRY("cs5", control, 0, CS5, CSIZE)
MI_ENTRY("cs6", control, 0, CS6, CSIZE)
MI_ENTRY("cs7", control, 0, CS7, CSIZE)
MI_ENTRY("cs8", control, 0, CS8, CSIZE)
MI_ENTRY("hupcl", control, REV, HUPCL, 0 )
MI_ENTRY("hup", control, REV | OMIT, HUPCL, 0 )
MI_ENTRY("cstopb", control, REV, CSTOPB, 0 )
MI_ENTRY("cread", control, SANE_SET | REV, CREAD, 0 )
MI_ENTRY("clocal", control, REV, CLOCAL, 0 )
#if CRTSCTS
MI_ENTRY("crtscts", control, REV, CRTSCTS, 0 )
#endif
MI_ENTRY("ignbrk", input, SANE_UNSET | REV, IGNBRK, 0 )
MI_ENTRY("brkint", input, SANE_SET | REV, BRKINT, 0 )
MI_ENTRY("ignpar", input, REV, IGNPAR, 0 )
MI_ENTRY("parmrk", input, REV, PARMRK, 0 )
MI_ENTRY("inpck", input, REV, INPCK, 0 )
MI_ENTRY("istrip", input, REV, ISTRIP, 0 )
MI_ENTRY("inlcr", input, SANE_UNSET | REV, INLCR, 0 )
MI_ENTRY("igncr", input, SANE_UNSET | REV, IGNCR, 0 )
MI_ENTRY("icrnl", input, SANE_SET | REV, ICRNL, 0 )
MI_ENTRY("ixon", input, REV, IXON, 0 )
MI_ENTRY("ixoff", input, SANE_UNSET | REV, IXOFF, 0 )
MI_ENTRY("tandem", input, REV | OMIT, IXOFF, 0 )
#if IUCLC
MI_ENTRY("iuclc", input, SANE_UNSET | REV, IUCLC, 0 )
#endif
#if IXANY
MI_ENTRY("ixany", input, SANE_UNSET | REV, IXANY, 0 )
#endif
#if IMAXBEL
MI_ENTRY("imaxbel", input, SANE_SET | REV, IMAXBEL, 0 )
#endif
MI_ENTRY("opost", output, SANE_SET | REV, OPOST, 0 )
#if OLCUC
MI_ENTRY("olcuc", output, SANE_UNSET | REV, OLCUC, 0 )
#endif
#if OCRNL
MI_ENTRY("ocrnl", output, SANE_UNSET | REV, OCRNL, 0 )
#endif
#if ONLCR
MI_ENTRY("onlcr", output, SANE_SET | REV, ONLCR, 0 )
#endif
#if ONOCR
MI_ENTRY("onocr", output, SANE_UNSET | REV, ONOCR, 0 )
#endif
#if ONLRET
MI_ENTRY("onlret", output, SANE_UNSET | REV, ONLRET, 0 )
#endif
#if OFILL
MI_ENTRY("ofill", output, SANE_UNSET | REV, OFILL, 0 )
#endif
#if OFDEL
MI_ENTRY("ofdel", output, SANE_UNSET | REV, OFDEL, 0 )
#endif
#if NLDLY
MI_ENTRY("nl1", output, SANE_UNSET, NL1, NLDLY)
MI_ENTRY("nl0", output, SANE_SET, NL0, NLDLY)
#endif
#if CRDLY
MI_ENTRY("cr3", output, SANE_UNSET, CR3, CRDLY)
MI_ENTRY("cr2", output, SANE_UNSET, CR2, CRDLY)
MI_ENTRY("cr1", output, SANE_UNSET, CR1, CRDLY)
MI_ENTRY("cr0", output, SANE_SET, CR0, CRDLY)
#endif
#if TABDLY
MI_ENTRY("tab3", output, SANE_UNSET, TAB3, TABDLY)
# if TAB2
MI_ENTRY("tab2", output, SANE_UNSET, TAB2, TABDLY)
# endif
# if TAB1
MI_ENTRY("tab1", output, SANE_UNSET, TAB1, TABDLY)
# endif
MI_ENTRY("tab0", output, SANE_SET, TAB0, TABDLY)
#else
# if OXTABS
MI_ENTRY("tab3", output, SANE_UNSET, OXTABS, 0 )
# endif
#endif
#if BSDLY
MI_ENTRY("bs1", output, SANE_UNSET, BS1, BSDLY)
MI_ENTRY("bs0", output, SANE_SET, BS0, BSDLY)
#endif
#if VTDLY
MI_ENTRY("vt1", output, SANE_UNSET, VT1, VTDLY)
MI_ENTRY("vt0", output, SANE_SET, VT0, VTDLY)
#endif
#if FFDLY
MI_ENTRY("ff1", output, SANE_UNSET, FF1, FFDLY)
MI_ENTRY("ff0", output, SANE_SET, FF0, FFDLY)
#endif
MI_ENTRY("isig", local, SANE_SET | REV, ISIG, 0 )
MI_ENTRY("icanon", local, SANE_SET | REV, ICANON, 0 )
#if IEXTEN
MI_ENTRY("iexten", local, SANE_SET | REV, IEXTEN, 0 )
#endif
MI_ENTRY("echo", local, SANE_SET | REV, ECHO, 0 )
MI_ENTRY("echoe", local, SANE_SET | REV, ECHOE, 0 )
MI_ENTRY("crterase", local, REV | OMIT, ECHOE, 0 )
MI_ENTRY("echok", local, SANE_SET | REV, ECHOK, 0 )
MI_ENTRY("echonl", local, SANE_UNSET | REV, ECHONL, 0 )
MI_ENTRY("noflsh", local, SANE_UNSET | REV, NOFLSH, 0 )
#if XCASE
MI_ENTRY("xcase", local, SANE_UNSET | REV, XCASE, 0 )
#endif
#if TOSTOP
MI_ENTRY("tostop", local, SANE_UNSET | REV, TOSTOP, 0 )
#endif
#if ECHOPRT
MI_ENTRY("echoprt", local, SANE_UNSET | REV, ECHOPRT, 0 )
MI_ENTRY("prterase", local, REV | OMIT, ECHOPRT, 0 )
#endif
#if ECHOCTL
MI_ENTRY("echoctl", local, SANE_SET | REV, ECHOCTL, 0 )
MI_ENTRY("ctlecho", local, REV | OMIT, ECHOCTL, 0 )
#endif
#if ECHOKE
MI_ENTRY("echoke", local, SANE_SET | REV, ECHOKE, 0 )
MI_ENTRY("crtkill", local, REV | OMIT, ECHOKE, 0 )
#endif
;
#undef MI_ENTRY
#define MI_ENTRY(N,T,F,B,M) { T, F, M, B },
static const struct mode_info mode_info[] = {
/* This should be verbatim cut-n-paste copy of the above MI_ENTRYs */
MI_ENTRY("evenp", combination, REV | OMIT, 0, 0 )
MI_ENTRY("parity", combination, REV | OMIT, 0, 0 )
MI_ENTRY("oddp", combination, REV | OMIT, 0, 0 )
MI_ENTRY("nl", combination, REV | OMIT, 0, 0 )
MI_ENTRY("ek", combination, OMIT, 0, 0 )
MI_ENTRY("sane", combination, OMIT, 0, 0 )
MI_ENTRY("cooked", combination, REV | OMIT, 0, 0 )
MI_ENTRY("raw", combination, REV | OMIT, 0, 0 )
MI_ENTRY("pass8", combination, REV | OMIT, 0, 0 )
MI_ENTRY("litout", combination, REV | OMIT, 0, 0 )
MI_ENTRY("cbreak", combination, REV | OMIT, 0, 0 )
MI_ENTRY("crt", combination, OMIT, 0, 0 )
MI_ENTRY("dec", combination, OMIT, 0, 0 )
#if IXANY
MI_ENTRY("decctlq", combination, REV | OMIT, 0, 0 )
#endif
#if TABDLY || OXTABS
MI_ENTRY("tabs", combination, REV | OMIT, 0, 0 )
#endif
#if XCASE && IUCLC && OLCUC
MI_ENTRY("lcase", combination, REV | OMIT, 0, 0 )
MI_ENTRY("LCASE", combination, REV | OMIT, 0, 0 )
#endif
MI_ENTRY("parenb", control, REV, PARENB, 0 )
MI_ENTRY("parodd", control, REV, PARODD, 0 )
MI_ENTRY("cs5", control, 0, CS5, CSIZE)
MI_ENTRY("cs6", control, 0, CS6, CSIZE)
MI_ENTRY("cs7", control, 0, CS7, CSIZE)
MI_ENTRY("cs8", control, 0, CS8, CSIZE)
MI_ENTRY("hupcl", control, REV, HUPCL, 0 )
MI_ENTRY("hup", control, REV | OMIT, HUPCL, 0 )
MI_ENTRY("cstopb", control, REV, CSTOPB, 0 )
MI_ENTRY("cread", control, SANE_SET | REV, CREAD, 0 )
MI_ENTRY("clocal", control, REV, CLOCAL, 0 )
#if CRTSCTS
MI_ENTRY("crtscts", control, REV, CRTSCTS, 0 )
#endif
MI_ENTRY("ignbrk", input, SANE_UNSET | REV, IGNBRK, 0 )
MI_ENTRY("brkint", input, SANE_SET | REV, BRKINT, 0 )
MI_ENTRY("ignpar", input, REV, IGNPAR, 0 )
MI_ENTRY("parmrk", input, REV, PARMRK, 0 )
MI_ENTRY("inpck", input, REV, INPCK, 0 )
MI_ENTRY("istrip", input, REV, ISTRIP, 0 )
MI_ENTRY("inlcr", input, SANE_UNSET | REV, INLCR, 0 )
MI_ENTRY("igncr", input, SANE_UNSET | REV, IGNCR, 0 )
MI_ENTRY("icrnl", input, SANE_SET | REV, ICRNL, 0 )
MI_ENTRY("ixon", input, REV, IXON, 0 )
MI_ENTRY("ixoff", input, SANE_UNSET | REV, IXOFF, 0 )
MI_ENTRY("tandem", input, REV | OMIT, IXOFF, 0 )
#if IUCLC
MI_ENTRY("iuclc", input, SANE_UNSET | REV, IUCLC, 0 )
#endif
#if IXANY
MI_ENTRY("ixany", input, SANE_UNSET | REV, IXANY, 0 )
#endif
#if IMAXBEL
MI_ENTRY("imaxbel", input, SANE_SET | REV, IMAXBEL, 0 )
#endif
MI_ENTRY("opost", output, SANE_SET | REV, OPOST, 0 )
#if OLCUC
MI_ENTRY("olcuc", output, SANE_UNSET | REV, OLCUC, 0 )
#endif
#if OCRNL
MI_ENTRY("ocrnl", output, SANE_UNSET | REV, OCRNL, 0 )
#endif
#if ONLCR
MI_ENTRY("onlcr", output, SANE_SET | REV, ONLCR, 0 )
#endif
#if ONOCR
MI_ENTRY("onocr", output, SANE_UNSET | REV, ONOCR, 0 )
#endif
#if ONLRET
MI_ENTRY("onlret", output, SANE_UNSET | REV, ONLRET, 0 )
#endif
#if OFILL
MI_ENTRY("ofill", output, SANE_UNSET | REV, OFILL, 0 )
#endif
#if OFDEL
MI_ENTRY("ofdel", output, SANE_UNSET | REV, OFDEL, 0 )
#endif
#if NLDLY
MI_ENTRY("nl1", output, SANE_UNSET, NL1, NLDLY)
MI_ENTRY("nl0", output, SANE_SET, NL0, NLDLY)
#endif
#if CRDLY
MI_ENTRY("cr3", output, SANE_UNSET, CR3, CRDLY)
MI_ENTRY("cr2", output, SANE_UNSET, CR2, CRDLY)
MI_ENTRY("cr1", output, SANE_UNSET, CR1, CRDLY)
MI_ENTRY("cr0", output, SANE_SET, CR0, CRDLY)
#endif
#if TABDLY
MI_ENTRY("tab3", output, SANE_UNSET, TAB3, TABDLY)
# if TAB2
MI_ENTRY("tab2", output, SANE_UNSET, TAB2, TABDLY)
# endif
# if TAB1
MI_ENTRY("tab1", output, SANE_UNSET, TAB1, TABDLY)
# endif
MI_ENTRY("tab0", output, SANE_SET, TAB0, TABDLY)
#else
# if OXTABS
MI_ENTRY("tab3", output, SANE_UNSET, OXTABS, 0 )
# endif
#endif
#if BSDLY
MI_ENTRY("bs1", output, SANE_UNSET, BS1, BSDLY)
MI_ENTRY("bs0", output, SANE_SET, BS0, BSDLY)
#endif
#if VTDLY
MI_ENTRY("vt1", output, SANE_UNSET, VT1, VTDLY)
MI_ENTRY("vt0", output, SANE_SET, VT0, VTDLY)
#endif
#if FFDLY
MI_ENTRY("ff1", output, SANE_UNSET, FF1, FFDLY)
MI_ENTRY("ff0", output, SANE_SET, FF0, FFDLY)
#endif
MI_ENTRY("isig", local, SANE_SET | REV, ISIG, 0 )
MI_ENTRY("icanon", local, SANE_SET | REV, ICANON, 0 )
#if IEXTEN
MI_ENTRY("iexten", local, SANE_SET | REV, IEXTEN, 0 )
#endif
MI_ENTRY("echo", local, SANE_SET | REV, ECHO, 0 )
MI_ENTRY("echoe", local, SANE_SET | REV, ECHOE, 0 )
MI_ENTRY("crterase", local, REV | OMIT, ECHOE, 0 )
MI_ENTRY("echok", local, SANE_SET | REV, ECHOK, 0 )
MI_ENTRY("echonl", local, SANE_UNSET | REV, ECHONL, 0 )
MI_ENTRY("noflsh", local, SANE_UNSET | REV, NOFLSH, 0 )
#if XCASE
MI_ENTRY("xcase", local, SANE_UNSET | REV, XCASE, 0 )
#endif
#if TOSTOP
MI_ENTRY("tostop", local, SANE_UNSET | REV, TOSTOP, 0 )
#endif
#if ECHOPRT
MI_ENTRY("echoprt", local, SANE_UNSET | REV, ECHOPRT, 0 )
MI_ENTRY("prterase", local, REV | OMIT, ECHOPRT, 0 )
#endif
#if ECHOCTL
MI_ENTRY("echoctl", local, SANE_SET | REV, ECHOCTL, 0 )
MI_ENTRY("ctlecho", local, REV | OMIT, ECHOCTL, 0 )
#endif
#if ECHOKE
MI_ENTRY("echoke", local, SANE_SET | REV, ECHOKE, 0 )
MI_ENTRY("crtkill", local, REV | OMIT, ECHOKE, 0 )
#endif
};
enum {
NUM_mode_info = ARRAY_SIZE(mode_info)
};
/* Control characters */
struct control_info {
const uint8_t saneval; /* Value to set for 'stty sane' */
const uint8_t offset; /* Offset in c_cc */
};
enum {
/* Must match control_name[] and control_info[] order! */
CIDX_intr = 0,
CIDX_quit,
CIDX_erase,
CIDX_kill,
CIDX_eof,
CIDX_eol,
#if VEOL2
CIDX_eol2,
#endif
#if VSWTCH
CIDX_swtch,
#endif
CIDX_start,
CIDX_stop,
CIDX_susp,
#if VDSUSP
CIDX_dsusp,
#endif
#if VREPRINT
CIDX_rprnt,
#endif
#if VWERASE
CIDX_werase,
#endif
#if VLNEXT
CIDX_lnext,
#endif
#if VFLUSHO
CIDX_flush,
#endif
#if VSTATUS
CIDX_status,
#endif
CIDX_min,
CIDX_time,
};
#define CI_ENTRY(n,s,o) n "\0"
/* Name given on command line */
static const char control_name[] =
CI_ENTRY("intr", CINTR, VINTR )
CI_ENTRY("quit", CQUIT, VQUIT )
CI_ENTRY("erase", CERASE, VERASE )
CI_ENTRY("kill", CKILL, VKILL )
CI_ENTRY("eof", CEOF, VEOF )
CI_ENTRY("eol", CEOL, VEOL )
#if VEOL2
CI_ENTRY("eol2", CEOL2, VEOL2 )
#endif
#if VSWTCH
CI_ENTRY("swtch", CSWTCH, VSWTCH )
#endif
CI_ENTRY("start", CSTART, VSTART )
CI_ENTRY("stop", CSTOP, VSTOP )
CI_ENTRY("susp", CSUSP, VSUSP )
#if VDSUSP
CI_ENTRY("dsusp", CDSUSP, VDSUSP )
#endif
#if VREPRINT
CI_ENTRY("rprnt", CRPRNT, VREPRINT)
#endif
#if VWERASE
CI_ENTRY("werase", CWERASE, VWERASE )
#endif
#if VLNEXT
CI_ENTRY("lnext", CLNEXT, VLNEXT )
#endif
#if VFLUSHO
CI_ENTRY("flush", CFLUSHO, VFLUSHO )
#endif
#if VSTATUS
CI_ENTRY("status", CSTATUS, VSTATUS )
#endif
/* These must be last because of the display routines */
CI_ENTRY("min", 1, VMIN )
CI_ENTRY("time", 0, VTIME )
;
#undef CI_ENTRY
#define CI_ENTRY(n,s,o) { s, o },
static const struct control_info control_info[] = {
/* This should be verbatim cut-n-paste copy of the above CI_ENTRYs */
CI_ENTRY("intr", CINTR, VINTR )
CI_ENTRY("quit", CQUIT, VQUIT )
CI_ENTRY("erase", CERASE, VERASE )
CI_ENTRY("kill", CKILL, VKILL )
CI_ENTRY("eof", CEOF, VEOF )
CI_ENTRY("eol", CEOL, VEOL )
#if VEOL2
CI_ENTRY("eol2", CEOL2, VEOL2 )
#endif
#if VSWTCH
CI_ENTRY("swtch", CSWTCH, VSWTCH )
#endif
CI_ENTRY("start", CSTART, VSTART )
CI_ENTRY("stop", CSTOP, VSTOP )
CI_ENTRY("susp", CSUSP, VSUSP )
#if VDSUSP
CI_ENTRY("dsusp", CDSUSP, VDSUSP )
#endif
#if VREPRINT
CI_ENTRY("rprnt", CRPRNT, VREPRINT)
#endif
#if VWERASE
CI_ENTRY("werase", CWERASE, VWERASE )
#endif
#if VLNEXT
CI_ENTRY("lnext", CLNEXT, VLNEXT )
#endif
#if VFLUSHO
CI_ENTRY("flush", CFLUSHO, VFLUSHO )
#endif
#if VSTATUS
CI_ENTRY("status", CSTATUS, VSTATUS )
#endif
/* These must be last because of the display routines */
CI_ENTRY("min", 1, VMIN )
CI_ENTRY("time", 0, VTIME )
};
enum {
NUM_control_info = ARRAY_SIZE(control_info)
};
struct globals {
const char *device_name;
/* The width of the screen, for output wrapping */
unsigned max_col;
/* Current position, to know when to wrap */
unsigned current_col;
char buf[10];
} FIX_ALIASING;
#define G (*(struct globals*)&bb_common_bufsiz1)
#define INIT_G() do { \
G.device_name = bb_msg_standard_input; \
G.max_col = 80; \
} while (0)
/* Return a string that is the printable representation of character CH */
/* Adapted from 'cat' by Torbjorn Granlund */
static const char *visible(unsigned ch)
{
char *bpout = G.buf;
if (ch == _POSIX_VDISABLE)
return "<undef>";
if (ch >= 128) {
ch -= 128;
*bpout++ = 'M';
*bpout++ = '-';
}
if (ch < 32) {
*bpout++ = '^';
*bpout++ = ch + 64;
} else if (ch < 127) {
*bpout++ = ch;
} else {
*bpout++ = '^';
*bpout++ = '?';
}
*bpout = '\0';
return G.buf;
}
static tcflag_t *mode_type_flag(unsigned type, const struct termios *mode)
{
static const uint8_t tcflag_offsets[] ALIGN1 = {
offsetof(struct termios, c_cflag), /* control */
offsetof(struct termios, c_iflag), /* input */
offsetof(struct termios, c_oflag), /* output */
offsetof(struct termios, c_lflag) /* local */
};
if (type <= local) {
return (tcflag_t*) (((char*)mode) + tcflag_offsets[type]);
}
return NULL;
}
static void set_speed_or_die(enum speed_setting type, const char *arg,
struct termios *mode)
{
speed_t baud;
baud = tty_value_to_baud(xatou(arg));
if (type != output_speed) { /* either input or both */
cfsetispeed(mode, baud);
}
if (type != input_speed) { /* either output or both */
cfsetospeed(mode, baud);
}
}
static NORETURN void perror_on_device_and_die(const char *fmt)
{
bb_perror_msg_and_die(fmt, G.device_name);
}
static void perror_on_device(const char *fmt)
{
bb_perror_msg(fmt, G.device_name);
}
/* Print format string MESSAGE and optional args.
Wrap to next line first if it won't fit.
Print a space first unless MESSAGE will start a new line */
static void wrapf(const char *message, ...)
{
char buf[128];
va_list args;
unsigned buflen;
va_start(args, message);
buflen = vsnprintf(buf, sizeof(buf), message, args);
va_end(args);
/* We seem to be called only with suitable lengths, but check if
somebody failed to adhere to this assumption just to be sure. */
if (!buflen || buflen >= sizeof(buf)) return;
if (G.current_col > 0) {
G.current_col++;
if (buf[0] != '\n') {
if (G.current_col + buflen >= G.max_col) {
bb_putchar('\n');
G.current_col = 0;
} else
bb_putchar(' ');
}
}
fputs(buf, stdout);
G.current_col += buflen;
if (buf[buflen-1] == '\n')
G.current_col = 0;
}
static void newline(void)
{
if (G.current_col != 0)
wrapf("\n");
}
#ifdef TIOCGWINSZ
static void set_window_size(int rows, int cols)
{
struct winsize win = { 0, 0, 0, 0 };
if (ioctl(STDIN_FILENO, TIOCGWINSZ, &win)) {
if (errno != EINVAL) {
goto bail;
}
memset(&win, 0, sizeof(win));
}
if (rows >= 0)
win.ws_row = rows;
if (cols >= 0)
win.ws_col = cols;
if (ioctl(STDIN_FILENO, TIOCSWINSZ, (char *) &win))
bail:
perror_on_device("%s");
}
#endif
static void display_window_size(int fancy)
{
const char *fmt_str = "%s\0%s: no size information for this device";
unsigned width, height;
if (get_terminal_width_height(STDIN_FILENO, &width, &height)) {
if ((errno != EINVAL) || ((fmt_str += 2), !fancy)) {
perror_on_device(fmt_str);
}
} else {
wrapf(fancy ? "rows %u; columns %u;" : "%u %u\n",
height, width);
}
}
static const struct suffix_mult stty_suffixes[] = {
{ "b", 512 },
{ "k", 1024 },
{ "B", 1024 },
{ "", 0 }
};
static const struct mode_info *find_mode(const char *name)
{
int i = index_in_strings(mode_name, name);
return i >= 0 ? &mode_info[i] : NULL;
}
static const struct control_info *find_control(const char *name)
{
int i = index_in_strings(control_name, name);
return i >= 0 ? &control_info[i] : NULL;
}
enum {
param_need_arg = 0x80,
param_line = 1 | 0x80,
param_rows = 2 | 0x80,
param_cols = 3 | 0x80,
param_columns = 4 | 0x80,
param_size = 5,
param_speed = 6,
param_ispeed = 7 | 0x80,
param_ospeed = 8 | 0x80,
};
static int find_param(const char *name)
{
static const char params[] ALIGN1 =
"line\0" /* 1 */
"rows\0" /* 2 */
"cols\0" /* 3 */
"columns\0" /* 4 */
"size\0" /* 5 */
"speed\0" /* 6 */
"ispeed\0"
"ospeed\0";
int i = index_in_strings(params, name) + 1;
if (i == 0)
return 0;
if (i != 5 && i != 6)
i |= 0x80;
return i;
}
static int recover_mode(const char *arg, struct termios *mode)
{
int i, n;
unsigned chr;
unsigned long iflag, oflag, cflag, lflag;
/* Scan into temporaries since it is too much trouble to figure out
the right format for 'tcflag_t' */
if (sscanf(arg, "%lx:%lx:%lx:%lx%n",
&iflag, &oflag, &cflag, &lflag, &n) != 4)
return 0;
mode->c_iflag = iflag;
mode->c_oflag = oflag;
mode->c_cflag = cflag;
mode->c_lflag = lflag;
arg += n;
for (i = 0; i < NCCS; ++i) {
if (sscanf(arg, ":%x%n", &chr, &n) != 1)
return 0;
mode->c_cc[i] = chr;
arg += n;
}
/* Fail if there are too many fields */
if (*arg != '\0')
return 0;
return 1;
}
static void display_recoverable(const struct termios *mode,
int UNUSED_PARAM dummy)
{
int i;
printf("%lx:%lx:%lx:%lx",
(unsigned long) mode->c_iflag, (unsigned long) mode->c_oflag,
(unsigned long) mode->c_cflag, (unsigned long) mode->c_lflag);
for (i = 0; i < NCCS; ++i)
printf(":%x", (unsigned int) mode->c_cc[i]);
bb_putchar('\n');
}
static void display_speed(const struct termios *mode, int fancy)
{
//____________________ 01234567 8 9
const char *fmt_str = "%lu %lu\n\0ispeed %lu baud; ospeed %lu baud;";
unsigned long ispeed, ospeed;
ospeed = ispeed = cfgetispeed(mode);
if (ispeed == 0 || ispeed == (ospeed = cfgetospeed(mode))) {
ispeed = ospeed; /* in case ispeed was 0 */
//________ 0123 4 5 6 7 8 9
fmt_str = "%lu\n\0\0\0\0\0speed %lu baud;";
}
if (fancy) fmt_str += 9;