aboutsummaryrefslogtreecommitdiff
path: root/coreutils/stty.c
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2006-09-19 14:24:23 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2006-09-19 14:24:23 +0000
commit20b253d2d8e87587a3b9913ac76a49cdafa002ad (patch)
treee7fac013e5dcc49652f90841a2660c2bbe44a167 /coreutils/stty.c
parent8971cdaeca82c8f8f2dc9257fc10c558d2cc77ff (diff)
downloadbusybox-w32-20b253d2d8e87587a3b9913ac76a49cdafa002ad.tar.gz
busybox-w32-20b253d2d8e87587a3b9913ac76a49cdafa002ad.tar.bz2
busybox-w32-20b253d2d8e87587a3b9913ac76a49cdafa002ad.zip
stty: convert "enum mode_type" into unnamed enum
(reduces code obfuscation); deindent set_mode; add _or_die suffixes to few functions
Diffstat (limited to 'coreutils/stty.c')
-rw-r--r--coreutils/stty.c326
1 files changed, 169 insertions, 157 deletions
diff --git a/coreutils/stty.c b/coreutils/stty.c
index e60c4f589..7fa5feb0d 100644
--- a/coreutils/stty.c
+++ b/coreutils/stty.c
@@ -121,7 +121,7 @@ enum speed_setting {
121}; 121};
122 122
123/* Which member(s) of `struct termios' a mode uses */ 123/* Which member(s) of `struct termios' a mode uses */
124enum mode_type { 124enum {
125 /* Do NOT change the order or values, as mode_type_flag() 125 /* Do NOT change the order or values, as mode_type_flag()
126 * depends on them */ 126 * depends on them */
127 control, input, output, local, combination 127 control, input, output, local, combination
@@ -159,13 +159,11 @@ static const char stty_dec [] = "dec";
159/* Each mode */ 159/* Each mode */
160struct mode_info { 160struct mode_info {
161 const char *name; /* Name given on command line */ 161 const char *name; /* Name given on command line */
162 /* enum mode_type type; */
163 char type; /* Which structure element to change */ 162 char type; /* Which structure element to change */
164 char flags; /* Setting and display options */ 163 char flags; /* Setting and display options */
165 unsigned short mask; /* Other bits to turn off for this mode */ 164 unsigned short mask; /* Other bits to turn off for this mode */
166 unsigned long bits; /* Bits to set for this mode */ 165 unsigned long bits; /* Bits to set for this mode */
167}; 166};
168#define EMT(t) ((enum mode_type)(t))
169 167
170#define MI_ENTRY(N,T,F,B,M) { N, T, F, M, B } 168#define MI_ENTRY(N,T,F,B,M) { N, T, F, M, B }
171 169
@@ -383,14 +381,14 @@ static int screen_columns(void);
383static void set_mode(const struct mode_info *info, 381static void set_mode(const struct mode_info *info,
384 int reversed, struct termios *mode); 382 int reversed, struct termios *mode);
385static speed_t string_to_baud(const char *arg); 383static speed_t string_to_baud(const char *arg);
386static tcflag_t* mode_type_flag(enum mode_type type, const struct termios *mode); 384static tcflag_t* mode_type_flag(unsigned type, const struct termios *mode);
387static void display_all(const struct termios *mode); 385static void display_all(const struct termios *mode);
388static void display_changed(const struct termios *mode); 386static void display_changed(const struct termios *mode);
389static void display_recoverable(const struct termios *mode); 387static void display_recoverable(const struct termios *mode);
390static void display_speed(const struct termios *mode, int fancy); 388static void display_speed(const struct termios *mode, int fancy);
391static void display_window_size(int fancy); 389static void display_window_size(int fancy);
392static void sane_mode(struct termios *mode); 390static void sane_mode(struct termios *mode);
393static void set_control_char(const struct control_info *info, 391static void set_control_char_or_die(const struct control_info *info,
394 const char *arg, struct termios *mode); 392 const char *arg, struct termios *mode);
395static void set_speed(enum speed_setting type, 393static void set_speed(enum speed_setting type,
396 const char *arg, struct termios *mode); 394 const char *arg, struct termios *mode);
@@ -398,11 +396,16 @@ static void set_window_size(int rows, int cols);
398 396
399static const char *device_name = bb_msg_standard_input; 397static const char *device_name = bb_msg_standard_input;
400 398
401static ATTRIBUTE_NORETURN void perror_on_device(const char *fmt) 399static ATTRIBUTE_NORETURN void perror_on_device_and_die(const char *fmt)
402{ 400{
403 bb_perror_msg_and_die(fmt, device_name); 401 bb_perror_msg_and_die(fmt, device_name);
404} 402}
405 403
404static void perror_on_device(const char *fmt)
405{
406 bb_perror_msg(fmt, device_name);
407}
408
406/* No, inline won't be as efficient (gcc 3.4.3) */ 409/* No, inline won't be as efficient (gcc 3.4.3) */
407#define streq(a,b) (!strcmp((a),(b))) 410#define streq(a,b) (!strcmp((a),(b)))
408 411
@@ -570,6 +573,8 @@ end_option:
570 if (cp) { 573 if (cp) {
571 if (!argnext) 574 if (!argnext)
572 bb_error_msg_and_die(bb_msg_requires_arg, arg); 575 bb_error_msg_and_die(bb_msg_requires_arg, arg);
576 /* called for the side effect of xfunc death only */
577 set_control_char_or_die(cp, argnext, &mode);
573 noargs = 0; 578 noargs = 0;
574 ++k; 579 ++k;
575 continue; 580 continue;
@@ -627,14 +632,14 @@ end_option:
627 } 632 }
628 fdflags = fcntl(STDIN_FILENO, F_GETFL); 633 fdflags = fcntl(STDIN_FILENO, F_GETFL);
629 if (fdflags == -1 || fcntl(STDIN_FILENO, F_SETFL, fdflags & ~O_NONBLOCK) < 0) 634 if (fdflags == -1 || fcntl(STDIN_FILENO, F_SETFL, fdflags & ~O_NONBLOCK) < 0)
630 perror_on_device("%s: couldn't reset non-blocking mode"); 635 perror_on_device_and_die("%s: couldn't reset non-blocking mode");
631 } 636 }
632 637
633 /* Initialize to all zeroes so there is no risk memcmp will report a 638 /* Initialize to all zeroes so there is no risk memcmp will report a
634 spurious difference in an uninitialized portion of the structure */ 639 spurious difference in an uninitialized portion of the structure */
635 memset(&mode, 0, sizeof(mode)); 640 memset(&mode, 0, sizeof(mode));
636 if (tcgetattr(STDIN_FILENO, &mode)) 641 if (tcgetattr(STDIN_FILENO, &mode))
637 perror_on_device("%s"); 642 perror_on_device_and_die("%s");
638 643
639 if (verbose_output || recoverable_output || noargs) { 644 if (verbose_output || recoverable_output || noargs) {
640 max_col = screen_columns(); 645 max_col = screen_columns();
@@ -670,7 +675,7 @@ end_option:
670 cp = find_control(arg); 675 cp = find_control(arg);
671 if (cp) { 676 if (cp) {
672 ++k; 677 ++k;
673 set_control_char(cp, argnext, &mode); 678 set_control_char_or_die(cp, argnext, &mode);
674 continue; 679 continue;
675 } 680 }
676 681
@@ -729,7 +734,7 @@ end_option:
729 struct termios new_mode; 734 struct termios new_mode;
730 735
731 if (tcsetattr(STDIN_FILENO, TCSADRAIN, &mode)) 736 if (tcsetattr(STDIN_FILENO, TCSADRAIN, &mode))
732 perror_on_device("%s"); 737 perror_on_device_and_die("%s");
733 738
734 /* POSIX (according to Zlotnick's book) tcsetattr returns zero if 739 /* POSIX (according to Zlotnick's book) tcsetattr returns zero if
735 it performs *any* of the requested operations. This means it 740 it performs *any* of the requested operations. This means it
@@ -742,7 +747,7 @@ end_option:
742 spurious difference in an uninitialized portion of the structure */ 747 spurious difference in an uninitialized portion of the structure */
743 memset(&new_mode, 0, sizeof(new_mode)); 748 memset(&new_mode, 0, sizeof(new_mode));
744 if (tcgetattr(STDIN_FILENO, &new_mode)) 749 if (tcgetattr(STDIN_FILENO, &new_mode))
745 perror_on_device("%s"); 750 perror_on_device_and_die("%s");
746 751
747 if (memcmp(&mode, &new_mode, sizeof(mode)) != 0) { 752 if (memcmp(&mode, &new_mode, sizeof(mode)) != 0) {
748#ifdef CIBAUD 753#ifdef CIBAUD
@@ -758,7 +763,7 @@ end_option:
758 new_mode.c_cflag &= (~CIBAUD); 763 new_mode.c_cflag &= (~CIBAUD);
759 if (speed_was_set || memcmp(&mode, &new_mode, sizeof(mode)) != 0) 764 if (speed_was_set || memcmp(&mode, &new_mode, sizeof(mode)) != 0)
760#endif 765#endif
761 perror_on_device ("%s: unable to perform all requested operations"); 766 perror_on_device_and_die ("%s: unable to perform all requested operations");
762 } 767 }
763 } 768 }
764 769
@@ -805,127 +810,130 @@ set_mode(const struct mode_info *info, int reversed, struct termios *mode)
805{ 810{
806 tcflag_t *bitsp; 811 tcflag_t *bitsp;
807 812
808 bitsp = mode_type_flag(EMT(info->type), mode); 813 bitsp = mode_type_flag(info->type, mode);
809 814
810 if (bitsp == NULL) { 815 if (bitsp) {
811 /* Combination mode */ 816 if (reversed)
812 if (info->name == evenp || info->name == parity) { 817 *bitsp = *bitsp & ~((unsigned long)info->mask) & ~info->bits;
813 if (reversed) 818 else
814 mode->c_cflag = (mode->c_cflag & ~PARENB & ~CSIZE) | CS8; 819 *bitsp = (*bitsp & ~((unsigned long)info->mask)) | info->bits;
815 else 820 return;
816 mode->c_cflag = 821 }
817 (mode->c_cflag & ~PARODD & ~CSIZE) | PARENB | CS7; 822
818 } else if (info->name == stty_oddp) { 823 /* Combination mode */
819 if (reversed) 824 if (info->name == evenp || info->name == parity) {
820 mode->c_cflag = (mode->c_cflag & ~PARENB & ~CSIZE) | CS8; 825 if (reversed)
821 else 826 mode->c_cflag = (mode->c_cflag & ~PARENB & ~CSIZE) | CS8;
822 mode->c_cflag = 827 else
823 (mode->c_cflag & ~CSIZE) | CS7 | PARODD | PARENB; 828 mode->c_cflag = (mode->c_cflag & ~PARODD & ~CSIZE) | PARENB | CS7;
824 } else if (info->name == stty_nl) { 829 } else if (info->name == stty_oddp) {
825 if (reversed) { 830 if (reversed)
826 mode->c_iflag = (mode->c_iflag | ICRNL) & ~INLCR & ~IGNCR; 831 mode->c_cflag = (mode->c_cflag & ~PARENB & ~CSIZE) | CS8;
827 mode->c_oflag = (mode->c_oflag | ONLCR) & ~OCRNL & ~ONLRET; 832 else
828 } else { 833 mode->c_cflag = (mode->c_cflag & ~CSIZE) | CS7 | PARODD | PARENB;
829 mode->c_iflag = mode->c_iflag & ~ICRNL; 834 } else if (info->name == stty_nl) {
830 if (ONLCR) mode->c_oflag = mode->c_oflag & ~ONLCR; 835 if (reversed) {
831 } 836 mode->c_iflag = (mode->c_iflag | ICRNL) & ~INLCR & ~IGNCR;
832 } else if (info->name == stty_ek) { 837 mode->c_oflag = (mode->c_oflag | ONLCR) & ~OCRNL & ~ONLRET;
833 mode->c_cc[VERASE] = CERASE; 838 } else {
834 mode->c_cc[VKILL] = CKILL; 839 mode->c_iflag = mode->c_iflag & ~ICRNL;
835 } else if (info->name == stty_sane) 840 if (ONLCR) mode->c_oflag = mode->c_oflag & ~ONLCR;
836 sane_mode(mode);
837 else if (info->name == cbreak) {
838 if (reversed)
839 mode->c_lflag |= ICANON;
840 else
841 mode->c_lflag &= ~ICANON;
842 } else if (info->name == stty_pass8) {
843 if (reversed) {
844 mode->c_cflag = (mode->c_cflag & ~CSIZE) | CS7 | PARENB;
845 mode->c_iflag |= ISTRIP;
846 } else {
847 mode->c_cflag = (mode->c_cflag & ~PARENB & ~CSIZE) | CS8;
848 mode->c_iflag &= ~ISTRIP;
849 }
850 } else if (info->name == litout) {
851 if (reversed) {
852 mode->c_cflag = (mode->c_cflag & ~CSIZE) | CS7 | PARENB;
853 mode->c_iflag |= ISTRIP;
854 mode->c_oflag |= OPOST;
855 } else {
856 mode->c_cflag = (mode->c_cflag & ~PARENB & ~CSIZE) | CS8;
857 mode->c_iflag &= ~ISTRIP;
858 mode->c_oflag &= ~OPOST;
859 }
860 } else if (info->name == raw || info->name == cooked) {
861 if ((info->name[0] == 'r' && reversed)
862 || (info->name[0] == 'c' && !reversed)) {
863 /* Cooked mode */
864 mode->c_iflag |= BRKINT | IGNPAR | ISTRIP | ICRNL | IXON;
865 mode->c_oflag |= OPOST;
866 mode->c_lflag |= ISIG | ICANON;
867#if VMIN == VEOF
868 mode->c_cc[VEOF] = CEOF;
869#endif
870#if VTIME == VEOL
871 mode->c_cc[VEOL] = CEOL;
872#endif
873 } else {
874 /* Raw mode */
875 mode->c_iflag = 0;
876 mode->c_oflag &= ~OPOST;
877 mode->c_lflag &= ~(ISIG | ICANON | XCASE);
878 mode->c_cc[VMIN] = 1;
879 mode->c_cc[VTIME] = 0;
880 }
881 }
882 else if (IXANY && info->name == decctlq) {
883 if (reversed)
884 mode->c_iflag |= IXANY;
885 else
886 mode->c_iflag &= ~IXANY;
887 } 841 }
888 else if (TABDLY && info->name == stty_tabs) { 842 } else if (info->name == stty_ek) {
889 if (reversed) 843 mode->c_cc[VERASE] = CERASE;
890 mode->c_oflag = (mode->c_oflag & ~TABDLY) | TAB3; 844 mode->c_cc[VKILL] = CKILL;
891 else 845 } else if (info->name == stty_sane) {
892 mode->c_oflag = (mode->c_oflag & ~TABDLY) | TAB0; 846 sane_mode(mode);
847 }
848 else if (info->name == cbreak) {
849 if (reversed)
850 mode->c_lflag |= ICANON;
851 else
852 mode->c_lflag &= ~ICANON;
853 } else if (info->name == stty_pass8) {
854 if (reversed) {
855 mode->c_cflag = (mode->c_cflag & ~CSIZE) | CS7 | PARENB;
856 mode->c_iflag |= ISTRIP;
857 } else {
858 mode->c_cflag = (mode->c_cflag & ~PARENB & ~CSIZE) | CS8;
859 mode->c_iflag &= ~ISTRIP;
893 } 860 }
894 else if (OXTABS && info->name == stty_tabs) { 861 } else if (info->name == litout) {
895 if (reversed) 862 if (reversed) {
896 mode->c_oflag = mode->c_oflag | OXTABS; 863 mode->c_cflag = (mode->c_cflag & ~CSIZE) | CS7 | PARENB;
897 else 864 mode->c_iflag |= ISTRIP;
898 mode->c_oflag = mode->c_oflag & ~OXTABS; 865 mode->c_oflag |= OPOST;
866 } else {
867 mode->c_cflag = (mode->c_cflag & ~PARENB & ~CSIZE) | CS8;
868 mode->c_iflag &= ~ISTRIP;
869 mode->c_oflag &= ~OPOST;
899 } 870 }
900 else if (XCASE && IUCLC && OLCUC 871 } else if (info->name == raw || info->name == cooked) {
901 && (info->name == stty_lcase || info->name == stty_LCASE)) { 872 if ((info->name[0] == 'r' && reversed)
902 if (reversed) { 873 || (info->name[0] == 'c' && !reversed)) {
903 mode->c_lflag &= ~XCASE; 874 /* Cooked mode */
904 mode->c_iflag &= ~IUCLC; 875 mode->c_iflag |= BRKINT | IGNPAR | ISTRIP | ICRNL | IXON;
905 mode->c_oflag &= ~OLCUC; 876 mode->c_oflag |= OPOST;
906 } else { 877 mode->c_lflag |= ISIG | ICANON;
907 mode->c_lflag |= XCASE; 878#if VMIN == VEOF
908 mode->c_iflag |= IUCLC; 879 mode->c_cc[VEOF] = CEOF;
909 mode->c_oflag |= OLCUC; 880#endif
910 } 881#if VTIME == VEOL
882 mode->c_cc[VEOL] = CEOL;
883#endif
884 } else {
885 /* Raw mode */
886 mode->c_iflag = 0;
887 mode->c_oflag &= ~OPOST;
888 mode->c_lflag &= ~(ISIG | ICANON | XCASE);
889 mode->c_cc[VMIN] = 1;
890 mode->c_cc[VTIME] = 0;
911 } 891 }
912 else if (info->name == stty_crt) 892 }
913 mode->c_lflag |= ECHOE | ECHOCTL | ECHOKE; 893 else if (IXANY && info->name == decctlq) {
914 else if (info->name == stty_dec) { 894 if (reversed)
915 mode->c_cc[VINTR] = 3; /* ^C */ 895 mode->c_iflag |= IXANY;
916 mode->c_cc[VERASE] = 127; /* DEL */ 896 else
917 mode->c_cc[VKILL] = 21; /* ^U */ 897 mode->c_iflag &= ~IXANY;
918 mode->c_lflag |= ECHOE | ECHOCTL | ECHOKE; 898 }
919 if (IXANY) mode->c_iflag &= ~IXANY; 899 else if (TABDLY && info->name == stty_tabs) {
900 if (reversed)
901 mode->c_oflag = (mode->c_oflag & ~TABDLY) | TAB3;
902 else
903 mode->c_oflag = (mode->c_oflag & ~TABDLY) | TAB0;
904 }
905 else if (OXTABS && info->name == stty_tabs) {
906 if (reversed)
907 mode->c_oflag |= OXTABS;
908 else
909 mode->c_oflag &= ~OXTABS;
910 }
911 else if (XCASE && IUCLC && OLCUC
912 && (info->name == stty_lcase || info->name == stty_LCASE)) {
913 if (reversed) {
914 mode->c_lflag &= ~XCASE;
915 mode->c_iflag &= ~IUCLC;
916 mode->c_oflag &= ~OLCUC;
917 } else {
918 mode->c_lflag |= XCASE;
919 mode->c_iflag |= IUCLC;
920 mode->c_oflag |= OLCUC;
920 } 921 }
921 } else if (reversed) 922 }
922 *bitsp = *bitsp & ~((unsigned long)info->mask) & ~info->bits; 923 else if (info->name == stty_crt) {
923 else 924 mode->c_lflag |= ECHOE | ECHOCTL | ECHOKE;
924 *bitsp = (*bitsp & ~((unsigned long)info->mask)) | info->bits; 925 }
926 else if (info->name == stty_dec) {
927 mode->c_cc[VINTR] = 3; /* ^C */
928 mode->c_cc[VERASE] = 127; /* DEL */
929 mode->c_cc[VKILL] = 21; /* ^U */
930 mode->c_lflag |= ECHOE | ECHOCTL | ECHOKE;
931 if (IXANY) mode->c_iflag &= ~IXANY;
932 }
925} 933}
926 934
927static void 935static void
928set_control_char(const struct control_info *info, const char *arg, 936set_control_char_or_die(const struct control_info *info, const char *arg,
929 struct termios *mode) 937 struct termios *mode)
930{ 938{
931 unsigned char value; 939 unsigned char value;
@@ -936,11 +944,11 @@ set_control_char(const struct control_info *info, const char *arg,
936 value = arg[0]; 944 value = arg[0];
937 else if (streq(arg, "^-") || streq(arg, "undef")) 945 else if (streq(arg, "^-") || streq(arg, "undef"))
938 value = _POSIX_VDISABLE; 946 value = _POSIX_VDISABLE;
939 else if (arg[0] == '^' && arg[1] != '\0') { /* Ignore any trailing junk */ 947 else if (arg[0] == '^') { /* Ignore any trailing junk (^Cjunk) */
940 if (arg[1] == '?') 948 if (arg[1] == '?')
941 value = 127; 949 value = 127;
942 else 950 else
943 value = arg[1] & ~0140; /* Non-letters get weird results */ 951 value = arg[1] & 0x1f; /* Non-letters get weird results */
944 } else 952 } else
945 value = bb_xparse_number(arg, stty_suffixes); 953 value = bb_xparse_number(arg, stty_suffixes);
946 mode->c_cc[info->offset] = value; 954 mode->c_cc[info->offset] = value;
@@ -963,9 +971,9 @@ set_speed(enum speed_setting type, const char *arg, struct termios *mode)
963 971
964#ifdef TIOCGWINSZ 972#ifdef TIOCGWINSZ
965 973
966static int get_win_size(int fd, struct winsize *win) 974static int get_win_size(struct winsize *win)
967{ 975{
968 return ioctl(fd, TIOCGWINSZ, (char *) win); 976 return ioctl(STDIN_FILENO, TIOCGWINSZ, (char *) win);
969} 977}
970 978
971static void 979static void
@@ -973,9 +981,11 @@ set_window_size(int rows, int cols)
973{ 981{
974 struct winsize win; 982 struct winsize win;
975 983
976 if (get_win_size(STDIN_FILENO, &win)) { 984 if (get_win_size(&win)) {
977 if (errno != EINVAL) 985 if (errno != EINVAL) {
978 perror_on_device("%s"); 986 perror_on_device("%s");
987 return;
988 }
979 memset(&win, 0, sizeof(win)); 989 memset(&win, 0, sizeof(win));
980 } 990 }
981 991
@@ -1000,7 +1010,7 @@ set_window_size(int rows, int cols)
1000 win.ws_row = win.ws_col = 1; 1010 win.ws_row = win.ws_col = 1;
1001 1011
1002 if ((ioctl(STDIN_FILENO, TIOCSWINSZ, (char *) &win) != 0) 1012 if ((ioctl(STDIN_FILENO, TIOCSWINSZ, (char *) &win) != 0)
1003 || (ioctl(STDIN_FILENO, TIOCSSIZE, (char *) &ttysz) != 0)) { 1013 || (ioctl(STDIN_FILENO, TIOCSSIZE, (char *) &ttysz) != 0)) {
1004 perror_on_device("%s"); 1014 perror_on_device("%s");
1005 } 1015 }
1006 return; 1016 return;
@@ -1013,19 +1023,24 @@ set_window_size(int rows, int cols)
1013 1023
1014static void display_window_size(int fancy) 1024static void display_window_size(int fancy)
1015{ 1025{
1016 const char *fmt_str = "%s" "\0" "%s: no size information for this device"; 1026 const char *fmt_str = "%s\0%s: no size information for this device";
1017 struct winsize win; 1027 struct winsize win;
1018 1028
1019 if (get_win_size(STDIN_FILENO, &win)) { 1029 if (get_win_size(&win)) {
1020 if ((errno != EINVAL) || ((fmt_str += 2), !fancy)) { 1030 if ((errno != EINVAL) || ((fmt_str += 2), !fancy)) {
1021 perror_on_device(fmt_str); 1031 perror_on_device(fmt_str);
1022 } 1032 }
1023 } else { 1033 } else {
1024 wrapf(fancy ? "rows %d; columns %d;" : "%d %d\n", 1034 wrapf(fancy ? "rows %d; columns %d;" : "%d %d\n",
1025 win.ws_row, win.ws_col); 1035 win.ws_row, win.ws_col);
1026 } 1036 }
1027} 1037}
1028#endif 1038
1039#else /* !TIOCGWINSZ */
1040
1041static inline void display_window_size(int fancy) {}
1042
1043#endif /* !TIOCGWINSZ */
1029 1044
1030static int screen_columns(void) 1045static int screen_columns(void)
1031{ 1046{
@@ -1041,7 +1056,7 @@ static int screen_columns(void)
1041 (but it works for ptys). 1056 (but it works for ptys).
1042 It can also fail on any system when stdout isn't a tty. 1057 It can also fail on any system when stdout isn't a tty.
1043 In case of any failure, just use the default */ 1058 In case of any failure, just use the default */
1044 if (get_win_size(STDOUT_FILENO, &win) == 0 && win.ws_col > 0) 1059 if (get_win_size(&win) == 0 && win.ws_col > 0)
1045 return win.ws_col; 1060 return win.ws_col;
1046#endif 1061#endif
1047 1062
@@ -1052,17 +1067,17 @@ static int screen_columns(void)
1052 return columns; 1067 return columns;
1053} 1068}
1054 1069
1055static tcflag_t *mode_type_flag(enum mode_type type, const struct termios *mode) 1070static tcflag_t *mode_type_flag(unsigned type, const struct termios *mode)
1056{ 1071{
1057 static const unsigned char tcflag_offsets[] = { 1072 static const unsigned char tcflag_offsets[] = {
1058 offsetof(struct termios, c_cflag), /* control */ 1073 offsetof(struct termios, c_cflag), /* control */
1059 offsetof(struct termios, c_iflag), /* input */ 1074 offsetof(struct termios, c_iflag), /* input */
1060 offsetof(struct termios, c_oflag), /* output */ 1075 offsetof(struct termios, c_oflag), /* output */
1061 offsetof(struct termios, c_lflag) /* local */ 1076 offsetof(struct termios, c_lflag), /* local */
1062 }; 1077 };
1063 1078
1064 if (((unsigned int) type) <= local) { 1079 if (type <= local) {
1065 return (tcflag_t *)(((char *) mode) + tcflag_offsets[(int)type]); 1080 return (tcflag_t*) (((char*)mode) + tcflag_offsets[type]);
1066 } 1081 }
1067 return NULL; 1082 return NULL;
1068} 1083}
@@ -1072,7 +1087,7 @@ static void display_changed(const struct termios *mode)
1072 int i; 1087 int i;
1073 tcflag_t *bitsp; 1088 tcflag_t *bitsp;
1074 unsigned long mask; 1089 unsigned long mask;
1075 enum mode_type prev_type = control; 1090 int prev_type = control;
1076 1091
1077 display_speed(mode, 1); 1092 display_speed(mode, 1);
1078#ifdef HAVE_C_LINE 1093#ifdef HAVE_C_LINE
@@ -1107,12 +1122,12 @@ static void display_changed(const struct termios *mode)
1107 for (i = 0; i < NUM_mode_info; ++i) { 1122 for (i = 0; i < NUM_mode_info; ++i) {
1108 if (mode_info[i].flags & OMIT) 1123 if (mode_info[i].flags & OMIT)
1109 continue; 1124 continue;
1110 if (EMT(mode_info[i].type) != prev_type) { 1125 if (mode_info[i].type != prev_type) {
1111 if (current_col) wrapf("\n"); 1126 if (current_col) wrapf("\n");
1112 prev_type = EMT(mode_info[i].type); 1127 prev_type = mode_info[i].type;
1113 } 1128 }
1114 1129
1115 bitsp = mode_type_flag(EMT(mode_info[i].type), mode); 1130 bitsp = mode_type_flag(mode_info[i].type, mode);
1116 mask = mode_info[i].mask ? mode_info[i].mask : mode_info[i].bits; 1131 mask = mode_info[i].mask ? mode_info[i].mask : mode_info[i].bits;
1117 if ((*bitsp & mask) == mode_info[i].bits) { 1132 if ((*bitsp & mask) == mode_info[i].bits) {
1118 if (mode_info[i].flags & SANE_UNSET) { 1133 if (mode_info[i].flags & SANE_UNSET) {
@@ -1131,12 +1146,10 @@ display_all(const struct termios *mode)
1131 int i; 1146 int i;
1132 tcflag_t *bitsp; 1147 tcflag_t *bitsp;
1133 unsigned long mask; 1148 unsigned long mask;
1134 enum mode_type prev_type = control; 1149 int prev_type = control;
1135 1150
1136 display_speed(mode, 1); 1151 display_speed(mode, 1);
1137#ifdef TIOCGWINSZ
1138 display_window_size(1); 1152 display_window_size(1);
1139#endif
1140#ifdef HAVE_C_LINE 1153#ifdef HAVE_C_LINE
1141 wrapf("line = %d;\n", mode->c_line); 1154 wrapf("line = %d;\n", mode->c_line);
1142#else 1155#else
@@ -1167,13 +1180,12 @@ display_all(const struct termios *mode)
1167 for (i = 0; i < NUM_mode_info; ++i) { 1180 for (i = 0; i < NUM_mode_info; ++i) {
1168 if (mode_info[i].flags & OMIT) 1181 if (mode_info[i].flags & OMIT)
1169 continue; 1182 continue;
1170 if (EMT(mode_info[i].type) != prev_type) { 1183 if (mode_info[i].type != prev_type) {
1171 putchar('\n'); 1184 wrapf("\n");
1172 current_col = 0; 1185 prev_type = mode_info[i].type;
1173 prev_type = EMT(mode_info[i].type);
1174 } 1186 }
1175 1187
1176 bitsp = mode_type_flag(EMT(mode_info[i].type), mode); 1188 bitsp = mode_type_flag(mode_info[i].type, mode);
1177 mask = mode_info[i].mask ? mode_info[i].mask : mode_info[i].bits; 1189 mask = mode_info[i].mask ? mode_info[i].mask : mode_info[i].bits;
1178 if ((*bitsp & mask) == mode_info[i].bits) 1190 if ((*bitsp & mask) == mode_info[i].bits)
1179 wrapf("%s", mode_info[i].name); 1191 wrapf("%s", mode_info[i].name);
@@ -1185,14 +1197,14 @@ display_all(const struct termios *mode)
1185 1197
1186static void display_speed(const struct termios *mode, int fancy) 1198static void display_speed(const struct termios *mode, int fancy)
1187{ 1199{
1188 //12345678 9 10 1200 //01234567 8 9
1189 const char *fmt_str = "%lu %lu\n\0ispeed %lu baud; ospeed %lu baud;"; 1201 const char *fmt_str = "%lu %lu\n\0ispeed %lu baud; ospeed %lu baud;";
1190 unsigned long ispeed, ospeed; 1202 unsigned long ispeed, ospeed;
1191 1203
1192 ospeed = ispeed = cfgetispeed(mode); 1204 ospeed = ispeed = cfgetispeed(mode);
1193 if (ispeed == 0 || ispeed == (ospeed = cfgetospeed(mode))) { 1205 if (ispeed == 0 || ispeed == (ospeed = cfgetospeed(mode))) {
1194 ispeed = ospeed; /* in case ispeed was 0 */ 1206 ispeed = ospeed; /* in case ispeed was 0 */
1195 //1234 5 6 7 8 9 10 1207 //0123 4 5 6 7 8 9
1196 fmt_str = "%lu\n\0\0\0\0\0speed %lu baud;"; 1208 fmt_str = "%lu\n\0\0\0\0\0speed %lu baud;";
1197 } 1209 }
1198 if (fancy) fmt_str += 9; 1210 if (fancy) fmt_str += 9;
@@ -1261,11 +1273,11 @@ static void sane_mode(struct termios *mode)
1261 1273
1262 for (i = 0; i < NUM_mode_info; ++i) { 1274 for (i = 0; i < NUM_mode_info; ++i) {
1263 if (mode_info[i].flags & SANE_SET) { 1275 if (mode_info[i].flags & SANE_SET) {
1264 bitsp = mode_type_flag(EMT(mode_info[i].type), mode); 1276 bitsp = mode_type_flag(mode_info[i].type, mode);
1265 *bitsp = (*bitsp & ~((unsigned long)mode_info[i].mask)) 1277 *bitsp = (*bitsp & ~((unsigned long)mode_info[i].mask))
1266 | mode_info[i].bits; 1278 | mode_info[i].bits;
1267 } else if (mode_info[i].flags & SANE_UNSET) { 1279 } else if (mode_info[i].flags & SANE_UNSET) {
1268 bitsp = mode_type_flag(EMT(mode_info[i].type), mode); 1280 bitsp = mode_type_flag(mode_info[i].type, mode);
1269 *bitsp = *bitsp & ~((unsigned long)mode_info[i].mask) 1281 *bitsp = *bitsp & ~((unsigned long)mode_info[i].mask)
1270 & ~mode_info[i].bits; 1282 & ~mode_info[i].bits;
1271 } 1283 }