This patch simplifies exiting with help in ifconfig.
Has no change on binary size, but loses 37 lines.
Also means that ifconfig --help exits cleanly with help disabled.
On Sat, 13 Apr 2013 17:56:34 -0700
Isaac Dunham <[email protected]> wrote:
> I took a look at ifconfig to see about show_help vs toys.exithelp, and I saw
> this repeated 16 times ("string" is the only thing that changes):
> } else if (!strcmp(*argv, "string")) {
> if(*++argv == NULL) {
> errno = EINVAL;
> show_help();
> }
> set_string(...)
>
> This looks like a pretty obvious candidate for a helper function that does
> more than show help and exit.
Patch attached.
--
Isaac Dunham <[email protected]>
diff --git a/toys/pending/ifconfig.c b/toys/pending/ifconfig.c
index d5aa21e..913b829 100644
--- a/toys/pending/ifconfig.c
+++ b/toys/pending/ifconfig.c
@@ -395,20 +395,24 @@ IFACE_LIST *iface_list_head;
/*
* display help info and exit from application.
*/
-static void show_help(void)
+
+void nullarg_help(char * args)
{
- char **arg = xzalloc(sizeof(char*) *3);
- arg[0] = "help";
- arg[1] = xstrdup(toys.which->name);
- toy_exec(arg);
+ if (args == NULL) {
+ errno = EINVAL;
+ toys.exithelp++;
+ error_exit("missing argument");
+ }
}
void ifconfig_main(void)
{
char **argv = toys.optargs;
- if(*argv && (strcmp(*argv, "--help") == 0))
- show_help();
+ if(*argv && (strcmp(*argv, "--help") == 0)) {
+ toys.exithelp++;
+ error_exit("");
+ }
//"ifconfig" / "ifconfig eth0"
if(!argv[0] || !argv[1]) { //one or no argument
@@ -465,99 +469,54 @@ void ifconfig_main(void)
set_flags(sockfd, &ifre, 0, IFF_POINTOPOINT);
/*value setup */
else if (!strcmp(*argv, "pointopoint")) {
- if(*++argv == NULL) {
- errno = EINVAL;
- show_help();
- }
+ nullarg_help(*++argv);
set_address(sockfd, *argv, &ifre, SIOCSIFDSTADDR, "SIOCSIFDSTADDR");
set_flags(sockfd, &ifre, IFF_POINTOPOINT, 0);
} else if (!strcmp(*argv, "netmask")) {
- if(*++argv == NULL) {
- errno = EINVAL;
- show_help();
- }
+ nullarg_help(*++argv);
set_address(sockfd, *argv, &ifre, SIOCSIFNETMASK, "SIOCSIFNETMASK");
} else if (!strcmp(*argv, "-broadcast")) {
set_flags(sockfd, &ifre, 0, IFF_BROADCAST);
} else if (!strcmp(*argv, "broadcast")) {
- if(*++argv == NULL) {
- errno = EINVAL;
- show_help();
- }
+ nullarg_help(*++argv);
set_address(sockfd, *argv, &ifre, SIOCSIFBRDADDR, "SIOCSIFBRDADDR");
set_flags(sockfd, &ifre, IFF_BROADCAST, 0);
} else if (!strcmp(*argv, "dstaddr")) {
- if(*++argv == NULL) {
- errno = EINVAL;
- show_help();
- }
+ nullarg_help(*++argv);
set_address(sockfd, *argv, &ifre, SIOCSIFDSTADDR, "SIOCSIFDSTADDR");
} else if (!strcmp(*argv, "hw")) {
- if(*++argv == NULL) {
- errno = EINVAL;
- show_help();
- }
+ nullarg_help(*++argv);
set_hw_address(sockfd, &argv, &ifre, SIOCSIFHWADDR, "SIOCSIFHWADDR");
} else if (!strcmp(*argv, "mtu")) {
- if(*++argv == NULL) {
- errno = EINVAL;
- show_help();
- }
+ nullarg_help(*++argv);
set_mtu(sockfd, &ifre, *argv);
} else if (!strcmp(*argv, "metric")) {
- if(*++argv == NULL) {
- errno = EINVAL;
- show_help();
- }
+ nullarg_help(*++argv);
set_metric(sockfd, &ifre, *argv);
} else if (!strcmp(*argv, "txqueuelen")) {
- if(*++argv == NULL) {
- errno = EINVAL;
- show_help();
- }
+ nullarg_help(*++argv);
set_qlen(sockfd, &ifre, *argv);
} else if (!strcmp(*argv, "keepalive")) {
- if(*++argv == NULL) {
- errno = EINVAL;
- show_help();
- }
+ nullarg_help(*++argv);
set_data(sockfd, &ifre, *argv, SIOCSKEEPALIVE, "SIOCSKEEPALIVE");
}//end of keepalive
else if (!strcmp(*argv, "outfill")) {
- if(*++argv == NULL) {
- errno = EINVAL;
- show_help();
- }
+ nullarg_help(*++argv);
set_data(sockfd, &ifre, *argv, SIOCSOUTFILL, "SIOCSOUTFILL");
} else if (!strcmp(*argv, "add")) {
- if(*++argv == NULL) {
- errno = EINVAL;
- show_help();
- }
+ nullarg_help(*++argv);
set_ipv6_addr(sockfd, &ifre, *argv, SIOCSIFADDR, "SIOCSIFADDR");
} else if (!strcmp(*argv, "del")) {
- if(*++argv == NULL) {
- errno = EINVAL;
- show_help();
- }
+ nullarg_help(*++argv);
set_ipv6_addr(sockfd, &ifre, *argv, SIOCDIFADDR, "SIOCDIFADDR");
} else if (!strcmp(*argv, "mem_start")) {
- if(*++argv == NULL) {
- errno = EINVAL;
- show_help();
- }
+ nullarg_help(*++argv);
set_memstart(sockfd, &ifre, *argv, SIOCSIFMAP, "SIOCSIFMAP");
} else if (!strcmp(*argv, "io_addr")) {
- if(*++argv == NULL) {
- errno = EINVAL;
- show_help();
- }
+ nullarg_help(*++argv);
set_ioaddr(sockfd, &ifre, *argv, SIOCSIFMAP, "SIOCSIFMAP");
} else if (!strcmp(*argv, "irq")) {
- if(*++argv == NULL) {
- errno = EINVAL;
- show_help();
- }
+ nullarg_help(*++argv);
set_irq(sockfd, &ifre, *argv, SIOCSIFMAP, "SIOCSIFMAP");
} else {
if(isdigit(**argv) || !strcmp(*argv, "default")) {
@@ -578,7 +537,8 @@ void ifconfig_main(void)
continue;
else {
errno = EINVAL;
- show_help();
+ toys.exithelp++;
+ error_exit("bad argument");
}
}
@@ -756,8 +716,11 @@ static void set_hw_address(int sockfd, char ***argv, struct ifreq *ifre, int req
hw_class = 1;
else if(strcmp(hw_class_strings[1], **argv) == 0)
hw_class = 2;
- if(!hw_class || !(*argv += 1))
- show_help();
+ if(!hw_class || !(*argv += 1)) {
+ errno = EINVAL;
+ toys.exithelp++;
+ error_exit("bad hardware class");
+ }
memset(&sock, 0, sizeof(struct sockaddr));
hw_addr = **argv;
_______________________________________________
Toybox mailing list
[email protected]
http://lists.landley.net/listinfo.cgi/toybox-landley.net