[Toybox] ifconfig: error helper?

Isaac Dunham idunham at lavabit.com
Sat Apr 13 17:56:34 PDT 2013


Hello,
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.

It would seem one could do something like this:
void nullarg_help(char * args)
{
  if (*args == NULL) {
    errno = EINVAL;
    toys.exithelp++;
    error_exit("missing argument");
  }
}
..
} else if (!strcmp(*argv, "string")) {
  nullarg_help(*++argv);
  set_string(...)


I probably got the pointers wrong, but that should show the general idea.
It will drop 40 lines (16 occurrances * 3 lines saved - 8 lines to define).

Any thoughts on this?

HTH, 
Isaac Dunham



More information about the Toybox mailing list