aboutsummaryrefslogtreecommitdiff
path: root/miscutils
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2017-08-06 17:59:37 +0200
committerDenys Vlasenko <vda.linux@googlemail.com>2017-08-06 17:59:37 +0200
commit08e66a81495274dbe8a16f264761ccdf921b6564 (patch)
treef30a3c037d9d4b981c5295b71caa7f077a0843af /miscutils
parent2262746e2b798361a9c293e02f76cb4f06b7b100 (diff)
downloadbusybox-w32-08e66a81495274dbe8a16f264761ccdf921b6564.tar.gz
busybox-w32-08e66a81495274dbe8a16f264761ccdf921b6564.tar.bz2
busybox-w32-08e66a81495274dbe8a16f264761ccdf921b6564.zip
setserial: code shrink, better --help text
function old new delta packed_usage 31747 31749 +2 setserial_main 1152 1132 -20 Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'miscutils')
-rw-r--r--miscutils/setserial.c91
1 files changed, 46 insertions, 45 deletions
diff --git a/miscutils/setserial.c b/miscutils/setserial.c
index 28a1bef18..a4d59f898 100644
--- a/miscutils/setserial.c
+++ b/miscutils/setserial.c
@@ -210,35 +210,35 @@ struct serial_struct {
210#endif 210#endif
211 211
212//usage:#define setserial_trivial_usage 212//usage:#define setserial_trivial_usage
213//usage: "[-gabGvzV] DEVICE [PARAMETER [ARG]]..." 213//usage: "[-abGvz] { DEVICE [PARAMETER [ARG]]... | -g DEVICE... }"
214//usage:#define setserial_full_usage "\n\n" 214//usage:#define setserial_full_usage "\n\n"
215//usage: "Request or set Linux serial port information\n" 215//usage: "Print or set serial port parameters"
216//usage: "\n" 216//usage: "\n"
217//usage: " -g Interpret parameters as list of devices for reporting\n" 217//usage: "\n"" -a Print all"
218//usage: " -a Print all available information\n" 218//usage: "\n"" -b Print summary"
219//usage: " -b Print summary information\n" 219//usage: "\n"" -G Print as setserial PARAMETERs"
220//usage: " -G Print in form which can be fed back\n" 220//usage: "\n"" -v Verbose"
221//usage: " to setserial as command line parameters\n" 221//usage: "\n"" -z Zero out serial flags before setting"
222//usage: " -z Zero out serial flags before setting\n" 222//usage: "\n"" -g All args are device names"
223//usage: " -v Verbose\n" 223//usage: "\n"
224//usage: "\n" 224//usage: "\n""PARAMETERs: (* = takes ARG, ^ = can be turned off by preceding ^)"
225//usage: "Parameters: (* = takes an argument, ^ = can be turned off by preceding ^)\n" 225//usage: "\n"" *port, *irq, *divisor, *uart, *baud_base, *close_delay, *closing_wait,"
226//usage: " *port, *irq, *divisor, *uart, *baud_base, *close_delay, *closing_wait,\n" 226//usage: "\n"" ^fourport, ^auto_irq, ^skip_test, ^sak, ^session_lockout, ^pgrp_lockout,"
227//usage: " ^fourport, ^auto_irq, ^skip_test, ^sak, ^session_lockout, ^pgrp_lockout,\n" 227//usage: "\n"" ^callout_nohup, ^split_termios, ^hup_notify, ^low_latency, autoconfig,"
228//usage: " ^callout_nohup, ^split_termios, ^hup_notify, ^low_latency, autoconfig,\n" 228//usage: "\n"" spd_normal, spd_hi, spd_vhi, spd_shi, spd_warp, spd_cust"
229//usage: " spd_normal, spd_hi, spd_vhi, spd_shi, spd_warp, spd_cust\n" 229//usage: "\n""ARG for uart:"
230//usage: "\n" 230//usage: "\n"" unknown, 8250, 16450, 16550, 16550A, Cirrus, 16650, 16650V2, 16750,"
231//usage: "UART types:\n" 231//usage: "\n"" 16950, 16954, 16654, 16850, RSA, NS16550A, XSCALE, RM9000, OCTEON, AR7,"
232//usage: " unknown, 8250, 16450, 16550, 16550A, Cirrus, 16650, 16650V2, 16750,\n" 232//usage: "\n"" U6_16550A"
233//usage: " 16950, 16954, 16654, 16850, RSA, NS16550A, XSCALE, RM9000, OCTEON, AR7,\n" 233
234//usage: " U6_16550A" 234// option string is "bGavzgq". "q" is accepted but ignored.
235
236#define OPT_PRINT_SUMMARY (1 << 0) 235#define OPT_PRINT_SUMMARY (1 << 0)
237#define OPT_PRINT_FEDBACK (1 << 1) 236#define OPT_PRINT_FEDBACK (1 << 1)
238#define OPT_PRINT_ALL (1 << 2) 237#define OPT_PRINT_ALL (1 << 2)
239#define OPT_VERBOSE (1 << 3) 238#define OPT_VERBOSE (1 << 3)
240#define OPT_ZERO (1 << 4) 239#define OPT_ZERO (1 << 4)
241#define OPT_GET (1 << 5) 240#define OPT_LIST_OF_DEVS (1 << 5)
241/*#define OPT_QUIET (1 << 6)*/
242 242
243#define OPT_MODE_MASK \ 243#define OPT_MODE_MASK \
244 (OPT_PRINT_ALL | OPT_PRINT_SUMMARY | OPT_PRINT_FEDBACK) 244 (OPT_PRINT_ALL | OPT_PRINT_SUMMARY | OPT_PRINT_FEDBACK)
@@ -362,7 +362,7 @@ static bool cmd_is_flag(int cmd)
362 return (cmd >= CMD_FLAG_FIRST && cmd <= CMD_FLAG_LAST); 362 return (cmd >= CMD_FLAG_FIRST && cmd <= CMD_FLAG_LAST);
363} 363}
364 364
365static bool cmd_need_arg(int cmd) 365static bool cmd_needs_arg(int cmd)
366{ 366{
367 return (cmd >= CMD_PORT && cmd <= CMD_WAIT); 367 return (cmd >= CMD_PORT && cmd <= CMD_WAIT);
368} 368}
@@ -652,11 +652,9 @@ static int find_cmd(const char *cmd)
652static void serial_set(char **arg, int opts) 652static void serial_set(char **arg, int opts)
653{ 653{
654 struct serial_struct serinfo; 654 struct serial_struct serinfo;
655 int cmd;
656 const char *word;
657 int fd; 655 int fd;
658 656
659 fd = serial_open(*arg++, /*quiet:*/ false); 657 fd = serial_open(*arg, /*quiet:*/ false);
660 if (fd < 0) 658 if (fd < 0)
661 exit(201); 659 exit(201);
662 660
@@ -665,17 +663,20 @@ static void serial_set(char **arg, int opts)
665 if (opts & OPT_ZERO) 663 if (opts & OPT_ZERO)
666 serinfo.flags = 0; 664 serinfo.flags = 0;
667 665
668 while (*arg) { 666 while (*++arg) {
667 const char *word;
669 int invert; 668 int invert;
669 int cmd;
670 670
671 word = *arg++; 671 word = *arg;
672 invert = (*word == '^'); 672 invert = (word[0] == '^');
673 word += invert; 673 word += invert;
674 674
675 cmd = find_cmd(word); 675 cmd = find_cmd(word);
676 676
677 if (*arg == NULL && cmd_need_arg(cmd)) 677 if (cmd_needs_arg(cmd))
678 bb_error_msg_and_die(bb_msg_requires_arg, word); 678 if (*++arg == NULL)
679 bb_error_msg_and_die(bb_msg_requires_arg, word);
679 680
680 if (invert && !cmd_is_flag(cmd)) 681 if (invert && !cmd_is_flag(cmd))
681 bb_error_msg_and_die("can't invert %s", word); 682 bb_error_msg_and_die("can't invert %s", word);
@@ -705,25 +706,25 @@ static void serial_set(char **arg, int opts)
705 serinfo.flags |= setbits[cmd]; 706 serinfo.flags |= setbits[cmd];
706 break; 707 break;
707 case CMD_PORT: 708 case CMD_PORT:
708 serinfo.port = get_numeric(*arg++); 709 serinfo.port = get_numeric(*arg);
709 break; 710 break;
710 case CMD_IRQ: 711 case CMD_IRQ:
711 serinfo.irq = get_numeric(*arg++); 712 serinfo.irq = get_numeric(*arg);
712 break; 713 break;
713 case CMD_DIVISOR: 714 case CMD_DIVISOR:
714 serinfo.custom_divisor = get_numeric(*arg++); 715 serinfo.custom_divisor = get_numeric(*arg);
715 break; 716 break;
716 case CMD_UART: 717 case CMD_UART:
717 serinfo.type = get_uart(*arg++); 718 serinfo.type = get_uart(*arg);
718 break; 719 break;
719 case CMD_BASE: 720 case CMD_BASE:
720 serinfo.baud_base = get_numeric(*arg++); 721 serinfo.baud_base = get_numeric(*arg);
721 break; 722 break;
722 case CMD_DELAY: 723 case CMD_DELAY:
723 serinfo.close_delay = get_numeric(*arg++); 724 serinfo.close_delay = get_numeric(*arg);
724 break; 725 break;
725 case CMD_WAIT: 726 case CMD_WAIT:
726 serinfo.closing_wait = get_wait(*arg++); 727 serinfo.closing_wait = get_wait(*arg);
727 break; 728 break;
728 case CMD_AUTOCONFIG: 729 case CMD_AUTOCONFIG:
729 serial_ctl(fd, CTL_SET | CTL_CONFIG | CTL_GET, &serinfo); 730 serial_ctl(fd, CTL_SET | CTL_CONFIG | CTL_GET, &serinfo);
@@ -742,21 +743,21 @@ int setserial_main(int argc UNUSED_PARAM, char **argv)
742 int opts; 743 int opts;
743 744
744 opt_complementary = "-1:b-aG:G-ab:a-bG"; 745 opt_complementary = "-1:b-aG:G-ab:a-bG";
745 opts = getopt32(argv, "bGavzg"); 746 opts = getopt32(argv, "bGavzgq");
746 argv += optind; 747 argv += optind;
747 748
748 if (!argv[1]) /* one arg only? */ 749 if (!argv[1]) /* one arg only? */
749 opts |= OPT_GET; 750 opts |= OPT_LIST_OF_DEVS;
750 751
751 if (!(opts & OPT_GET)) { 752 if (!(opts & OPT_LIST_OF_DEVS)) {
752 serial_set(argv, opts); 753 serial_set(argv, opts);
753 argv[1] = NULL; 754 argv[1] = NULL;
754 } 755 }
755 756
756 if (opts & (OPT_VERBOSE | OPT_GET)) { 757 if (opts & (OPT_VERBOSE | OPT_LIST_OF_DEVS)) {
757 do { 758 do {
758 serial_get(*argv++, opts & OPT_MODE_MASK); 759 serial_get(*argv, opts & OPT_MODE_MASK);
759 } while (*argv); 760 } while (*++argv);
760 } 761 }
761 762
762 return EXIT_SUCCESS; 763 return EXIT_SUCCESS;