Commit cd8c55c1 authored by unknown's avatar unknown

Some enhancements in my_getopt. Made code more readable and better

optimized in some places.
parent 474f8070
...@@ -34,18 +34,24 @@ static void init_variables(const struct my_option *options); ...@@ -34,18 +34,24 @@ static void init_variables(const struct my_option *options);
static int setval (const struct my_option *opts, char *argument, static int setval (const struct my_option *opts, char *argument,
my_bool set_maximum_value); my_bool set_maximum_value);
#define DISABLE_OPTION_COUNT 2 /* currently 'skip' and 'disable' below */ /*
The following three variables belong to same group and the number and
/* 'disable'-option prefixes must be in the beginning, DISABLE_OPTION_COUNT order of their arguments must correspond to each other.
is the number of disabling prefixes */ */
static const char *special_opt_prefix[]= static const char *special_opt_prefix[]=
{"skip", "disable", "enable", "maximum", "loose", 0}; {"skip", "disable", "enable", "maximum", "loose", 0};
static const uint special_opt_prefix_lengths[]=
{ 4, 7, 6, 7, 5, 0};
enum enum_special_opt { OPT_SKIP, OPT_DISABLE, OPT_ENABLE, OPT_MAXIMUM,
OPT_LOOSE};
char *disabled_my_option= (char*) "0"; char *disabled_my_option= (char*) "0";
/* This is a flag that can be set in client programs. 0 means that /*
This is a flag that can be set in client programs. 0 means that
my_getopt will not print error messages, but the client should do my_getopt will not print error messages, but the client should do
it by itself */ it by itself
*/
my_bool my_getopt_print_errors= 1; my_bool my_getopt_print_errors= 1;
...@@ -66,7 +72,7 @@ int handle_options(int *argc, char ***argv, ...@@ -66,7 +72,7 @@ int handle_options(int *argc, char ***argv,
const struct my_option *, const struct my_option *,
char *)) char *))
{ {
uint opt_found, argvpos= 0, length, spec_len, i; uint opt_found, argvpos= 0, length, i;
my_bool end_of_options= 0, must_be_var, set_maximum_value, special_used, my_bool end_of_options= 0, must_be_var, set_maximum_value, special_used,
option_is_loose, option_used= 0; option_is_loose, option_used= 0;
char *progname= *(*argv), **pos, *optend, *prev_found; char *progname= *(*argv), **pos, *optend, *prev_found;
...@@ -111,15 +117,9 @@ int handle_options(int *argc, char ***argv, ...@@ -111,15 +117,9 @@ int handle_options(int *argc, char ***argv,
(*argc)--; (*argc)--;
} }
} }
else if (!compare_strings(cur_arg, "-set-variable", 13) || else if (!compare_strings(cur_arg, "-set-variable", 13))
!compare_strings(cur_arg, "-loose-set-variable", 19))
{ {
must_be_var= 1; must_be_var= 1;
if (cur_arg[1] == 'l')
{
option_is_loose= 1;
cur_arg+= 6;
}
if (cur_arg[13] == '=') if (cur_arg[13] == '=')
{ {
cur_arg+= 14; cur_arg+= 14;
...@@ -183,18 +183,19 @@ int handle_options(int *argc, char ***argv, ...@@ -183,18 +183,19 @@ int handle_options(int *argc, char ***argv,
must_be_var= 1; /* option is followed by an argument */ must_be_var= 1; /* option is followed by an argument */
for (i= 0; special_opt_prefix[i]; i++) for (i= 0; special_opt_prefix[i]; i++)
{ {
spec_len= strlen(special_opt_prefix[i]); if (!compare_strings(special_opt_prefix[i], cur_arg,
if (!compare_strings(special_opt_prefix[i], cur_arg, spec_len) && special_opt_prefix_lengths[i]) &&
cur_arg[spec_len] == '-') cur_arg[special_opt_prefix_lengths[i]] == '-')
{ {
/* /*
We were called with a special prefix, we can reuse opt_found We were called with a special prefix, we can reuse opt_found
*/ */
special_used= 1; special_used= 1;
cur_arg+= (spec_len + 1); cur_arg+= (special_opt_prefix_lengths[i] + 1);
if (!compare_strings(special_opt_prefix[i], "loose", 5)) if (i == OPT_LOOSE)
option_is_loose= 1; option_is_loose= 1;
if ((opt_found= findopt(cur_arg, length - (spec_len + 1), if ((opt_found= findopt(cur_arg, length -
(special_opt_prefix_lengths[i] + 1),
&optp, &prev_found))) &optp, &prev_found)))
{ {
if (opt_found > 1) if (opt_found > 1)
...@@ -206,14 +207,18 @@ int handle_options(int *argc, char ***argv, ...@@ -206,14 +207,18 @@ int handle_options(int *argc, char ***argv,
special_opt_prefix[i], prev_found); special_opt_prefix[i], prev_found);
return EXIT_AMBIGUOUS_OPTION; return EXIT_AMBIGUOUS_OPTION;
} }
if (i < DISABLE_OPTION_COUNT) switch (i) {
case OPT_SKIP:
case OPT_DISABLE: /* fall through */
optend= disabled_my_option; optend= disabled_my_option;
else if (!compare_strings(special_opt_prefix[i],"enable",6)) break;
case OPT_ENABLE:
optend= (char*) "1"; optend= (char*) "1";
else if (!compare_strings(special_opt_prefix[i],"maximum",7)) break;
{ case OPT_MAXIMUM:
set_maximum_value= 1; set_maximum_value= 1;
must_be_var= 1; must_be_var= 1;
break;
} }
break; /* break from the inner loop, main loop continues */ break; /* break from the inner loop, main loop continues */
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment