aboutsummaryrefslogtreecommitdiff
path: root/coreutils/stty.c
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2006-09-19 14:19:42 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2006-09-19 14:19:42 +0000
commite40c04b82695c1cde8ad9ed1e2aa1756463d73a7 (patch)
tree48da4f9e1f1a706d907cd95467995451b189ded6 /coreutils/stty.c
parent9efb070dcb8c1bf92c3a44b182e13c8da708bd6f (diff)
downloadbusybox-w32-e40c04b82695c1cde8ad9ed1e2aa1756463d73a7.tar.gz
busybox-w32-e40c04b82695c1cde8ad9ed1e2aa1756463d73a7.tar.bz2
busybox-w32-e40c04b82695c1cde8ad9ed1e2aa1756463d73a7.zip
stty: simplify linewrapping code a bit
Diffstat (limited to 'coreutils/stty.c')
-rw-r--r--coreutils/stty.c88
1 files changed, 33 insertions, 55 deletions
diff --git a/coreutils/stty.c b/coreutils/stty.c
index 6f14efe63..641bfb815 100644
--- a/coreutils/stty.c
+++ b/coreutils/stty.c
@@ -403,35 +403,36 @@ static ATTRIBUTE_NORETURN void perror_on_device(const char *fmt)
403 bb_perror_msg_and_die(fmt, device_name); 403 bb_perror_msg_and_die(fmt, device_name);
404} 404}
405 405
406static ATTRIBUTE_ALWAYS_INLINE int streq(const char *a, const char *b) 406/* No, inline won't be as efficient (gcc 3.4.3) */
407{ 407#define streq(a,b) (!strcmp((a),(b)))
408 return strcmp(a, b) == 0;
409}
410 408
411/* Print format string MESSAGE and optional args. 409/* Print format string MESSAGE and optional args.
412 Wrap to next line first if it won't fit. 410 Wrap to next line first if it won't fit.
413 Print a space first unless MESSAGE will start a new line */ 411 Print a space first unless MESSAGE will start a new line */
414
415static void wrapf(const char *message, ...) 412static void wrapf(const char *message, ...)
416{ 413{
414 char buf[128];
417 va_list args; 415 va_list args;
418 char buf[1024]; /* Plenty long for our needs */
419 int buflen; 416 int buflen;
420 417
421 va_start(args, message); 418 va_start(args, message);
422 vsprintf(buf, message, args); 419 vsnprintf(buf, sizeof(buf), message, args);
423 va_end(args); 420 va_end(args);
424 buflen = strlen(buf); 421 buflen = strlen(buf);
425 if (current_col + (current_col > 0) + buflen >= max_col) { 422 if (!buflen) return;
426 putchar('\n'); 423
427 current_col = 0;
428 }
429 if (current_col > 0) { 424 if (current_col > 0) {
430 putchar(' ');
431 current_col++; 425 current_col++;
426 if (current_col + buflen >= max_col) {
427 putchar('\n');
428 current_col = 0;
429 } else
430 if (buf[0] != '\n') putchar(' ');
432 } 431 }
433 fputs(buf, stdout); 432 fputs(buf, stdout);
434 current_col += buflen; 433 current_col += buflen;
434 if (buf[buflen-1] == '\n')
435 current_col = 0;
435} 436}
436 437
437static const struct suffix_mult stty_suffixes[] = { 438static const struct suffix_mult stty_suffixes[] = {
@@ -584,13 +585,13 @@ end_option:
584 switch (param) { 585 switch (param) {
585#ifdef HAVE_C_LINE 586#ifdef HAVE_C_LINE
586 case param_line: 587 case param_line:
588# ifndef TIOCGWINSZ
587 bb_xparse_number(argnext, stty_suffixes); 589 bb_xparse_number(argnext, stty_suffixes);
588 break; 590 break;
591# endif /* else fall-through */
589#endif 592#endif
590#ifdef TIOCGWINSZ 593#ifdef TIOCGWINSZ
591 case param_rows: 594 case param_rows:
592 bb_xparse_number(argnext, stty_suffixes);
593 break;
594 case param_cols: 595 case param_cols:
595 bb_xparse_number(argnext, stty_suffixes); 596 bb_xparse_number(argnext, stty_suffixes);
596 break; 597 break;
@@ -1029,8 +1030,6 @@ static void display_window_size(int fancy)
1029 } else { 1030 } else {
1030 wrapf(fancy ? "rows %d; columns %d;" : "%d %d\n", 1031 wrapf(fancy ? "rows %d; columns %d;" : "%d %d\n",
1031 win.ws_row, win.ws_col); 1032 win.ws_row, win.ws_col);
1032 if (!fancy)
1033 current_col = 0;
1034 } 1033 }
1035} 1034}
1036#endif 1035#endif
@@ -1078,19 +1077,17 @@ static tcflag_t *mode_type_flag(enum mode_type type, const struct termios *mode)
1078static void display_changed(const struct termios *mode) 1077static void display_changed(const struct termios *mode)
1079{ 1078{
1080 int i; 1079 int i;
1081 int empty_line;
1082 tcflag_t *bitsp; 1080 tcflag_t *bitsp;
1083 unsigned long mask; 1081 unsigned long mask;
1084 enum mode_type prev_type = control; 1082 enum mode_type prev_type = control;
1085 1083
1086 display_speed(mode, 1); 1084 display_speed(mode, 1);
1087#ifdef HAVE_C_LINE 1085#ifdef HAVE_C_LINE
1088 wrapf("line = %d;", mode->c_line); 1086 wrapf("line = %d;\n", mode->c_line);
1087#else
1088 wrapf("\n");
1089#endif 1089#endif
1090 putchar('\n');
1091 current_col = 0;
1092 1090
1093 empty_line = 1;
1094 for (i = 0; control_info[i].name != stty_min; ++i) { 1091 for (i = 0; control_info[i].name != stty_min; ++i) {
1095 if (mode->c_cc[control_info[i].offset] == control_info[i].saneval) 1092 if (mode->c_cc[control_info[i].offset] == control_info[i].saneval)
1096 continue; 1093 continue;
@@ -1105,28 +1102,20 @@ static void display_changed(const struct termios *mode)
1105 && (control_info[i].name == stty_eof 1102 && (control_info[i].name == stty_eof
1106 || control_info[i].name == stty_eol)) continue; 1103 || control_info[i].name == stty_eol)) continue;
1107#endif 1104#endif
1108
1109 empty_line = 0;
1110 wrapf("%s = %s;", control_info[i].name, 1105 wrapf("%s = %s;", control_info[i].name,
1111 visible(mode->c_cc[control_info[i].offset])); 1106 visible(mode->c_cc[control_info[i].offset]));
1112 } 1107 }
1113 if ((mode->c_lflag & ICANON) == 0) { 1108 if ((mode->c_lflag & ICANON) == 0) {
1114 wrapf("min = %d; time = %d;\n", (int) mode->c_cc[VMIN], 1109 wrapf("min = %d; time = %d;", (int) mode->c_cc[VMIN],
1115 (int) mode->c_cc[VTIME]); 1110 (int) mode->c_cc[VTIME]);
1116 } else if (empty_line == 0) 1111 }
1117 putchar('\n'); 1112 if (current_col) wrapf("\n");
1118 current_col = 0;
1119 1113
1120 empty_line = 1;
1121 for (i = 0; i < NUM_mode_info; ++i) { 1114 for (i = 0; i < NUM_mode_info; ++i) {
1122 if (mode_info[i].flags & OMIT) 1115 if (mode_info[i].flags & OMIT)
1123 continue; 1116 continue;
1124 if (EMT(mode_info[i].type) != prev_type) { 1117 if (EMT(mode_info[i].type) != prev_type) {
1125 if (empty_line == 0) { 1118 if (current_col) wrapf("\n");
1126 putchar('\n');
1127 current_col = 0;
1128 empty_line = 1;
1129 }
1130 prev_type = EMT(mode_info[i].type); 1119 prev_type = EMT(mode_info[i].type);
1131 } 1120 }
1132 1121
@@ -1135,16 +1124,12 @@ static void display_changed(const struct termios *mode)
1135 if ((*bitsp & mask) == mode_info[i].bits) { 1124 if ((*bitsp & mask) == mode_info[i].bits) {
1136 if (mode_info[i].flags & SANE_UNSET) { 1125 if (mode_info[i].flags & SANE_UNSET) {
1137 wrapf("%s", mode_info[i].name); 1126 wrapf("%s", mode_info[i].name);
1138 empty_line = 0;
1139 } 1127 }
1140 } else if ((mode_info[i].flags & (SANE_SET | REV)) == (SANE_SET | REV)) { 1128 } else if ((mode_info[i].flags & (SANE_SET | REV)) == (SANE_SET | REV)) {
1141 wrapf("-%s", mode_info[i].name); 1129 wrapf("-%s", mode_info[i].name);
1142 empty_line = 0;
1143 } 1130 }
1144 } 1131 }
1145 if (empty_line == 0) 1132 if (current_col) wrapf("\n");
1146 putchar('\n');
1147 current_col = 0;
1148} 1133}
1149 1134
1150static void 1135static void
@@ -1160,10 +1145,10 @@ display_all(const struct termios *mode)
1160 display_window_size(1); 1145 display_window_size(1);
1161#endif 1146#endif
1162#ifdef HAVE_C_LINE 1147#ifdef HAVE_C_LINE
1163 wrapf("line = %d;", mode->c_line); 1148 wrapf("line = %d;\n", mode->c_line);
1149#else
1150 wrapf("\n");
1164#endif 1151#endif
1165 putchar('\n');
1166 current_col = 0;
1167 1152
1168 for (i = 0; control_info[i].name != stty_min; ++i) { 1153 for (i = 0; control_info[i].name != stty_min; ++i) {
1169 /* If swtch is the same as susp, don't print both */ 1154 /* If swtch is the same as susp, don't print both */
@@ -1184,9 +1169,7 @@ display_all(const struct termios *mode)
1184 if ((mode->c_lflag & ICANON) == 0) 1169 if ((mode->c_lflag & ICANON) == 0)
1185#endif 1170#endif
1186 wrapf("min = %d; time = %d;", mode->c_cc[VMIN], mode->c_cc[VTIME]); 1171 wrapf("min = %d; time = %d;", mode->c_cc[VMIN], mode->c_cc[VTIME]);
1187 if (current_col != 0) 1172 if (current_col) wrapf("\n");
1188 putchar('\n');
1189 current_col = 0;
1190 1173
1191 for (i = 0; i < NUM_mode_info; ++i) { 1174 for (i = 0; i < NUM_mode_info; ++i) {
1192 if (mode_info[i].flags & OMIT) 1175 if (mode_info[i].flags & OMIT)
@@ -1204,28 +1187,23 @@ display_all(const struct termios *mode)
1204 else if (mode_info[i].flags & REV) 1187 else if (mode_info[i].flags & REV)
1205 wrapf("-%s", mode_info[i].name); 1188 wrapf("-%s", mode_info[i].name);
1206 } 1189 }
1207 putchar('\n'); 1190 if (current_col) wrapf("\n");
1208 current_col = 0;
1209} 1191}
1210 1192
1211static void display_speed(const struct termios *mode, int fancy) 1193static void display_speed(const struct termios *mode, int fancy)
1212{ 1194{
1195 //12345678 9 10
1196 const char *fmt_str = "%lu %lu\n\0ispeed %lu baud; ospeed %lu baud;";
1213 unsigned long ispeed, ospeed; 1197 unsigned long ispeed, ospeed;
1214 const char *fmt_str =
1215 "%lu %lu\n\0" "ispeed %lu baud; ospeed %lu baud;\0"
1216 "%lu\n\0" "\0\0\0\0" "speed %lu baud;";
1217 1198
1218 ospeed = ispeed = cfgetispeed(mode); 1199 ospeed = ispeed = cfgetispeed(mode);
1219 if (ispeed == 0 || ispeed == (ospeed = cfgetospeed(mode))) { 1200 if (ispeed == 0 || ispeed == (ospeed = cfgetospeed(mode))) {
1220 ispeed = ospeed; /* in case ispeed was 0 */ 1201 ispeed = ospeed; /* in case ispeed was 0 */
1221 fmt_str += 43; 1202 //1234 5 6 7 8 9 10
1222 } 1203 fmt_str = "%lu\n\0\0\0\0\0speed %lu baud;";
1223 if (fancy) {
1224 fmt_str += 9;
1225 } 1204 }
1205 if (fancy) fmt_str += 9;
1226 wrapf(fmt_str, tty_baud_to_value(ispeed), tty_baud_to_value(ospeed)); 1206 wrapf(fmt_str, tty_baud_to_value(ispeed), tty_baud_to_value(ospeed));
1227 if (!fancy)
1228 current_col = 0;
1229} 1207}
1230 1208
1231static void display_recoverable(const struct termios *mode) 1209static void display_recoverable(const struct termios *mode)