#include <my_global.h>#include <m_string.h>#include <stdlib.h>#include <my_sys.h>#include <mysys_err.h>#include <my_getopt.h>#include <help_start.h>#include <help_end.h>Include dependency graph for my_getopt.c:

Go to the source code of this file.
Enumerations | |
| enum | enum_special_opt { OPT_SKIP, OPT_DISABLE, OPT_ENABLE, OPT_MAXIMUM, OPT_LOOSE } |
Functions | |
| static void | default_reporter (enum loglevel level, const char *format,...) |
| static int | findopt (char *optpat, uint length, const struct my_option **opt_res, char **ffname) |
| my_bool | getopt_compare_strings (const char *s, const char *t, uint length) |
| static longlong | getopt_ll (char *arg, const struct my_option *optp, int *err) |
| static ulonglong | getopt_ull (char *arg, const struct my_option *optp, int *err) |
| static void | init_variables (const struct my_option *options) |
| static int | setval (const struct my_option *opts, gptr *value, char *argument, my_bool set_maximum_value) |
| static char * | check_struct_option (char *cur_arg, char *key_name) |
| static void | default_reporter (enum loglevel level __attribute__((unused)), const char *format,...) |
| void | my_getopt_register_get_addr (gptr *(*func_addr)(const char *, uint, const struct my_option *)) |
| int | handle_options (int *argc, char ***argv, const struct my_option *longopts, my_get_one_option get_one_option) |
| my_bool | getopt_compare_strings (register const char *s, register const char *t, uint length) |
| static longlong | eval_num_suffix (char *argument, int *error, char *option_name) |
| ulonglong | getopt_ull_limit_value (ulonglong num, const struct my_option *optp) |
| static void | init_one_value (const struct my_option *option, gptr *variable, longlong value) |
| void | my_print_help (const struct my_option *options) |
| void | my_print_variables (const struct my_option *options) |
Variables | |
| my_error_reporter | my_getopt_error_reporter = &default_reporter |
| static const char * | special_opt_prefix [] |
| static const uint | special_opt_prefix_lengths [] |
| char * | disabled_my_option = (char*) "0" |
| my_bool | my_getopt_print_errors = 1 |
| static gptr *(*) | getopt_get_addr (const char *, uint, const struct my_option *) |
| enum enum_special_opt |
Definition at line 49 of file my_getopt.c.
00050 { OPT_SKIP, OPT_DISABLE, OPT_ENABLE, OPT_MAXIMUM, OPT_LOOSE};
| static char * check_struct_option | ( | char * | cur_arg, | |
| char * | key_name | |||
| ) | [static] |
Definition at line 519 of file my_getopt.c.
References FN_REFLEN, set_if_smaller, strcend(), and strmake().
Referenced by handle_options().
00520 { 00521 char *ptr, *end; 00522 00523 ptr= strcend(cur_arg + 1, '.'); /* Skip the first character */ 00524 end= strcend(cur_arg, '='); 00525 00526 /* 00527 If the first dot is after an equal sign, then it is part 00528 of a variable value and the option is not a struct option. 00529 Also, if the last character in the string before the ending 00530 NULL, or the character right before equal sign is the first 00531 dot found, the option is not a struct option. 00532 */ 00533 if (end - ptr > 1) 00534 { 00535 uint len= (uint) (ptr - cur_arg); 00536 set_if_smaller(len, FN_REFLEN-1); 00537 strmake(key_name, cur_arg, len); 00538 return ++ptr; 00539 } 00540 else 00541 { 00542 key_name[0]= 0; 00543 return cur_arg; 00544 } 00545 }
Here is the call graph for this function:

Here is the caller graph for this function:

| static void default_reporter | ( | enum loglevel level | __attribute__((unused)), | |
| const char * | format, | |||
| ... | ||||
| ) | [static] |
| static void default_reporter | ( | enum loglevel | level, | |
| const char * | format, | |||
| ... | ||||
| ) | [static] |
| static longlong eval_num_suffix | ( | char * | argument, | |
| int * | error, | |||
| char * | option_name | |||
| ) | [static] |
Definition at line 685 of file my_getopt.c.
References strtoll.
Referenced by getopt_ll(), and getopt_ull().
00686 { 00687 char *endchar; 00688 longlong num; 00689 00690 *error= 0; 00691 num= strtoll(argument, &endchar, 10); 00692 if (*endchar == 'k' || *endchar == 'K') 00693 num*= 1024L; 00694 else if (*endchar == 'm' || *endchar == 'M') 00695 num*= 1024L * 1024L; 00696 else if (*endchar == 'g' || *endchar == 'G') 00697 num*= 1024L * 1024L * 1024L; 00698 else if (*endchar) 00699 { 00700 fprintf(stderr, 00701 "Unknown suffix '%c' used for variable '%s' (value '%s')\n", 00702 *endchar, option_name, argument); 00703 *error= 1; 00704 return 0; 00705 } 00706 return num; 00707 }
Here is the caller graph for this function:

| static int findopt | ( | char * | optpat, | |
| uint | length, | |||
| const struct my_option ** | opt_res, | |||
| char ** | ffname | |||
| ) | [static] |
Definition at line 626 of file my_getopt.c.
References count, getopt_compare_strings(), opt(), and strcmp().
Referenced by handle_options().
00629 { 00630 uint count; 00631 struct my_option *opt= (struct my_option *) *opt_res; 00632 00633 for (count= 0; opt->name; opt++) 00634 { 00635 if (!getopt_compare_strings(opt->name, optpat, length)) /* match found */ 00636 { 00637 (*opt_res)= opt; 00638 if (!opt->name[length]) /* Exact match */ 00639 return 1; 00640 if (!count) 00641 { 00642 count= 1; 00643 *ffname= (char *) opt->name; /* We only need to know one prev */ 00644 } 00645 else if (strcmp(*ffname, opt->name)) 00646 { 00647 /* 00648 The above test is to not count same option twice 00649 (see mysql.cc, option "help") 00650 */ 00651 count++; 00652 } 00653 } 00654 } 00655 return count; 00656 }
Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 666 of file my_getopt.c.
00668 { 00669 char const *end= s + length; 00670 for (;s != end ; s++, t++) 00671 { 00672 if ((*s != '-' ? *s : '_') != (*t != '-' ? *t : '_')) 00673 return 1; 00674 } 00675 return 0; 00676 }
Definition at line 720 of file my_getopt.c.
References my_option::block_size, eval_num_suffix(), max, my_option::max_value, my_option::min_value, my_option::name, and my_option::sub_size.
Referenced by setval().
00721 { 00722 longlong num; 00723 ulonglong block_size= (optp->block_size ? (ulonglong) optp->block_size : 1L); 00724 00725 num= eval_num_suffix(arg, err, (char*) optp->name); 00726 if (num > 0 && (ulonglong) num > (ulonglong) optp->max_value && 00727 optp->max_value) /* if max value is not set -> no upper limit */ 00728 num= (ulonglong) optp->max_value; 00729 num= ((num - optp->sub_size) / block_size); 00730 num= (longlong) (num * block_size); 00731 return max(num, optp->min_value); 00732 }
Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 741 of file my_getopt.c.
References eval_num_suffix(), getopt_ull_limit_value(), and my_option::name.
Referenced by setval().
00742 { 00743 ulonglong num; 00744 00745 num= eval_num_suffix(arg, err, (char*) optp->name); 00746 return getopt_ull_limit_value(num, optp); 00747 }
Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 750 of file my_getopt.c.
References my_option::block_size, my_option::max_value, and my_option::min_value.
00751 { 00752 if ((ulonglong) num > (ulonglong) optp->max_value && 00753 optp->max_value) /* if max value is not set -> no upper limit */ 00754 num= (ulonglong) optp->max_value; 00755 if (optp->block_size > 1) 00756 { 00757 num/= (ulonglong) optp->block_size; 00758 num*= (ulonglong) optp->block_size; 00759 } 00760 if (num < (ulonglong) optp->min_value) 00761 num= (ulonglong) optp->min_value; 00762 return num; 00763 }
| int handle_options | ( | int * | argc, | |
| char *** | argv, | |||
| const struct my_option * | longopts, | |||
| my_get_one_option | get_one_option | |||
| ) |
Definition at line 89 of file my_getopt.c.
References my_option::arg_type, check_struct_option(), disabled_my_option, error, ERROR_LEVEL, EXIT_AMBIGUOUS_OPTION, EXIT_ARGUMENT_REQUIRED, EXIT_NO_ARGUMENT_ALLOWED, EXIT_OPTION_DISABLED, EXIT_UNKNOWN_OPTION, EXIT_UNKNOWN_VARIABLE, EXIT_VAR_PREFIX_NOT_UNIQUE, findopt(), FN_REFLEN, GET_ASK_ADDR, GET_BOOL, GET_DISABLED, GET_NO_ARG, get_one_option(), GET_TYPE_MASK, getopt_compare_strings(), my_option::id, init_variables(), LINT_INIT, my_charset_latin1, my_getopt_error_reporter, my_getopt_print_errors, my_progname, my_strcasecmp, my_option::name, NO_ARG, OPT_ARG, OPT_DISABLE, OPT_ENABLE, OPT_LOOSE, OPT_MAXIMUM, OPT_SKIP, pos(), REQUIRED_ARG, setval(), special_opt_prefix, special_opt_prefix_lengths, strcend(), strlen(), my_option::value, value, my_option::var_type, and WARNING_LEVEL.
00092 { 00093 uint opt_found, argvpos= 0, length, i; 00094 my_bool end_of_options= 0, must_be_var, set_maximum_value, 00095 option_is_loose; 00096 char **pos, **pos_end, *optend, *prev_found, 00097 *opt_str, key_name[FN_REFLEN]; 00098 const struct my_option *optp; 00099 gptr *value; 00100 int error; 00101 00102 LINT_INIT(opt_found); 00103 (*argc)--; /* Skip the program name */ 00104 (*argv)++; /* --- || ---- */ 00105 init_variables(longopts); 00106 00107 for (pos= *argv, pos_end=pos+ *argc; pos != pos_end ; pos++) 00108 { 00109 char *cur_arg= *pos; 00110 if (cur_arg[0] == '-' && cur_arg[1] && !end_of_options) /* must be opt */ 00111 { 00112 char *argument= 0; 00113 must_be_var= 0; 00114 set_maximum_value= 0; 00115 option_is_loose= 0; 00116 00117 cur_arg++; /* skip '-' */ 00118 if (*cur_arg == '-' || *cur_arg == 'O') /* check for long option, */ 00119 { /* --set-variable, or -O */ 00120 if (*cur_arg == 'O') 00121 { 00122 must_be_var= 1; 00123 00124 if (!(*++cur_arg)) /* If not -Ovar=# */ 00125 { 00126 /* the argument must be in next argv */ 00127 if (!*++pos) 00128 { 00129 if (my_getopt_print_errors) 00130 my_getopt_error_reporter(ERROR_LEVEL, 00131 "%s: Option '-O' requires an argument\n", 00132 my_progname); 00133 return EXIT_ARGUMENT_REQUIRED; 00134 } 00135 cur_arg= *pos; 00136 (*argc)--; 00137 } 00138 } 00139 else if (!getopt_compare_strings(cur_arg, "-set-variable", 13)) 00140 { 00141 must_be_var= 1; 00142 if (cur_arg[13] == '=') 00143 { 00144 cur_arg+= 14; 00145 if (!*cur_arg) 00146 { 00147 if (my_getopt_print_errors) 00148 my_getopt_error_reporter(ERROR_LEVEL, 00149 "%s: Option '--set-variable' requires an argument\n", 00150 my_progname); 00151 return EXIT_ARGUMENT_REQUIRED; 00152 } 00153 } 00154 else if (cur_arg[14]) /* garbage, or another option. break out */ 00155 must_be_var= 0; 00156 else 00157 { 00158 /* the argument must be in next argv */ 00159 if (!*++pos) 00160 { 00161 if (my_getopt_print_errors) 00162 my_getopt_error_reporter(ERROR_LEVEL, 00163 "%s: Option '--set-variable' requires an argument\n", 00164 my_progname); 00165 return EXIT_ARGUMENT_REQUIRED; 00166 } 00167 cur_arg= *pos; 00168 (*argc)--; 00169 } 00170 } 00171 else if (!must_be_var) 00172 { 00173 if (!*++cur_arg) /* skip the double dash */ 00174 { 00175 /* '--' means end of options, look no further */ 00176 end_of_options= 1; 00177 (*argc)--; 00178 continue; 00179 } 00180 } 00181 opt_str= check_struct_option(cur_arg, key_name); 00182 optend= strcend(opt_str, '='); 00183 length= (uint) (optend - opt_str); 00184 if (*optend == '=') 00185 optend++; 00186 else 00187 optend= 0; 00188 00189 /* 00190 Find first the right option. Return error in case of an ambiguous, 00191 or unknown option 00192 */ 00193 optp= longopts; 00194 if (!(opt_found= findopt(opt_str, length, &optp, &prev_found))) 00195 { 00196 /* 00197 Didn't find any matching option. Let's see if someone called 00198 option with a special option prefix 00199 */ 00200 if (!must_be_var) 00201 { 00202 if (optend) 00203 must_be_var= 1; /* option is followed by an argument */ 00204 for (i= 0; special_opt_prefix[i]; i++) 00205 { 00206 if (!getopt_compare_strings(special_opt_prefix[i], opt_str, 00207 special_opt_prefix_lengths[i]) && 00208 (opt_str[special_opt_prefix_lengths[i]] == '-' || 00209 opt_str[special_opt_prefix_lengths[i]] == '_')) 00210 { 00211 /* 00212 We were called with a special prefix, we can reuse opt_found 00213 */ 00214 opt_str+= (special_opt_prefix_lengths[i] + 1); 00215 if (i == OPT_LOOSE) 00216 option_is_loose= 1; 00217 if ((opt_found= findopt(opt_str, length - 00218 (special_opt_prefix_lengths[i] + 1), 00219 &optp, &prev_found))) 00220 { 00221 if (opt_found > 1) 00222 { 00223 if (my_getopt_print_errors) 00224 my_getopt_error_reporter(ERROR_LEVEL, 00225 "%s: ambiguous option '--%s-%s' (--%s-%s)\n", 00226 my_progname, special_opt_prefix[i], 00227 cur_arg, special_opt_prefix[i], 00228 prev_found); 00229 return EXIT_AMBIGUOUS_OPTION; 00230 } 00231 switch (i) { 00232 case OPT_SKIP: 00233 case OPT_DISABLE: /* fall through */ 00234 /* 00235 double negation is actually enable again, 00236 for example: --skip-option=0 -> option = TRUE 00237 */ 00238 optend= (optend && *optend == '0' && !(*(optend + 1))) ? 00239 (char*) "1" : disabled_my_option; 00240 break; 00241 case OPT_ENABLE: 00242 optend= (optend && *optend == '0' && !(*(optend + 1))) ? 00243 disabled_my_option : (char*) "1"; 00244 break; 00245 case OPT_MAXIMUM: 00246 set_maximum_value= 1; 00247 must_be_var= 1; 00248 break; 00249 } 00250 break; /* break from the inner loop, main loop continues */ 00251 } 00252 } 00253 } 00254 } 00255 if (!opt_found) 00256 { 00257 if (must_be_var) 00258 { 00259 if (my_getopt_print_errors) 00260 my_getopt_error_reporter(option_is_loose ? 00261 WARNING_LEVEL : ERROR_LEVEL, 00262 "%s: unknown variable '%s'\n", 00263 my_progname, cur_arg); 00264 if (!option_is_loose) 00265 return EXIT_UNKNOWN_VARIABLE; 00266 } 00267 else 00268 { 00269 if (my_getopt_print_errors) 00270 my_getopt_error_reporter(option_is_loose ? 00271 WARNING_LEVEL : ERROR_LEVEL, 00272 "%s: unknown option '--%s'\n", 00273 my_progname, cur_arg); 00274 if (!option_is_loose) 00275 return EXIT_UNKNOWN_OPTION; 00276 } 00277 if (option_is_loose) 00278 { 00279 (*argc)--; 00280 continue; 00281 } 00282 } 00283 } 00284 if (opt_found > 1) 00285 { 00286 if (must_be_var) 00287 { 00288 if (my_getopt_print_errors) 00289 my_getopt_error_reporter(ERROR_LEVEL, 00290 "%s: variable prefix '%s' is not unique\n", 00291 my_progname, opt_str); 00292 return EXIT_VAR_PREFIX_NOT_UNIQUE; 00293 } 00294 else 00295 { 00296 if (my_getopt_print_errors) 00297 my_getopt_error_reporter(ERROR_LEVEL, 00298 "%s: ambiguous option '--%s' (%s, %s)\n", 00299 my_progname, opt_str, prev_found, 00300 optp->name); 00301 return EXIT_AMBIGUOUS_OPTION; 00302 } 00303 } 00304 if ((optp->var_type & GET_TYPE_MASK) == GET_DISABLED) 00305 { 00306 if (my_getopt_print_errors) 00307 fprintf(stderr, 00308 "%s: %s: Option '%s' used, but is disabled\n", my_progname, 00309 option_is_loose ? "WARNING" : "ERROR", opt_str); 00310 if (option_is_loose) 00311 { 00312 (*argc)--; 00313 continue; 00314 } 00315 return EXIT_OPTION_DISABLED; 00316 } 00317 if (must_be_var && (optp->var_type & GET_TYPE_MASK) == GET_NO_ARG) 00318 { 00319 if (my_getopt_print_errors) 00320 my_getopt_error_reporter(ERROR_LEVEL, 00321 "%s: option '%s' cannot take an argument\n", 00322 my_progname, optp->name); 00323 return EXIT_NO_ARGUMENT_ALLOWED; 00324 } 00325 value= optp->var_type & GET_ASK_ADDR ? 00326 (*getopt_get_addr)(key_name, (uint) strlen(key_name), optp) : optp->value; 00327 00328 if (optp->arg_type == NO_ARG) 00329 { 00330 if (optend && (optp->var_type & GET_TYPE_MASK) != GET_BOOL) 00331 { 00332 if (my_getopt_print_errors) 00333 my_getopt_error_reporter(ERROR_LEVEL, 00334 "%s: option '--%s' cannot take an argument\n", 00335 my_progname, optp->name); 00336 return EXIT_NO_ARGUMENT_ALLOWED; 00337 } 00338 if ((optp->var_type & GET_TYPE_MASK) == GET_BOOL) 00339 { 00340 /* 00341 Set bool to 1 if no argument or if the user has used 00342 --enable-'option-name'. 00343 *optend was set to '0' if one used --disable-option 00344 */ 00345 (*argc)--; 00346 if (!optend || *optend == '1' || 00347 !my_strcasecmp(&my_charset_latin1, optend, "true")) 00348 *((my_bool*) value)= (my_bool) 1; 00349 else if (*optend == '0' || 00350 !my_strcasecmp(&my_charset_latin1, optend, "false")) 00351 *((my_bool*) value)= (my_bool) 0; 00352 else 00353 { 00354 my_getopt_error_reporter(WARNING_LEVEL, 00355 "%s: ignoring option '--%s' due to \ 00356 invalid value '%s'\n", 00357 my_progname, optp->name, optend); 00358 continue; 00359 } 00360 get_one_option(optp->id, optp, 00361 *((my_bool*) value) ? 00362 (char*) "1" : disabled_my_option); 00363 continue; 00364 } 00365 argument= optend; 00366 } 00367 else if (optp->arg_type == OPT_ARG && 00368 (optp->var_type & GET_TYPE_MASK) == GET_BOOL) 00369 { 00370 if (optend == disabled_my_option) 00371 *((my_bool*) value)= (my_bool) 0; 00372 else 00373 { 00374 if (!optend) /* No argument -> enable option */ 00375 *((my_bool*) value)= (my_bool) 1; 00376 else 00377 argument= optend; 00378 } 00379 } 00380 else if (optp->arg_type == REQUIRED_ARG && !optend) 00381 { 00382 /* Check if there are more arguments after this one */ 00383 if (!*++pos) 00384 { 00385 if (my_getopt_print_errors) 00386 my_getopt_error_reporter(ERROR_LEVEL, 00387 "%s: option '--%s' requires an argument\n", 00388 my_progname, optp->name); 00389 return EXIT_ARGUMENT_REQUIRED; 00390 } 00391 argument= *pos; 00392 (*argc)--; 00393 } 00394 else 00395 argument= optend; 00396 } 00397 else /* must be short option */ 00398 { 00399 for (optend= cur_arg; *optend; optend++) 00400 { 00401 opt_found= 0; 00402 for (optp= longopts; optp->id; optp++) 00403 { 00404 if (optp->id == (int) (uchar) *optend) 00405 { 00406 /* Option recognized. Find next what to do with it */ 00407 opt_found= 1; 00408 if ((optp->var_type & GET_TYPE_MASK) == GET_DISABLED) 00409 { 00410 if (my_getopt_print_errors) 00411 fprintf(stderr, 00412 "%s: ERROR: Option '-%c' used, but is disabled\n", 00413 my_progname, optp->id); 00414 return EXIT_OPTION_DISABLED; 00415 } 00416 if ((optp->var_type & GET_TYPE_MASK) == GET_BOOL && 00417 optp->arg_type == NO_ARG) 00418 { 00419 *((my_bool*) optp->value)= (my_bool) 1; 00420 get_one_option(optp->id, optp, argument); 00421 continue; 00422 } 00423 else if (optp->arg_type == REQUIRED_ARG || 00424 optp->arg_type == OPT_ARG) 00425 { 00426 if (*(optend + 1)) 00427 { 00428 /* The rest of the option is option argument */ 00429 argument= optend + 1; 00430 /* This is in effect a jump out of the outer loop */ 00431 optend= (char*) " "; 00432 } 00433 else 00434 { 00435 if (optp->arg_type == OPT_ARG) 00436 { 00437 if (optp->var_type == GET_BOOL) 00438 *((my_bool*) optp->value)= (my_bool) 1; 00439 get_one_option(optp->id, optp, argument); 00440 continue; 00441 } 00442 /* Check if there are more arguments after this one */ 00443 if (!pos[1]) 00444 { 00445 if (my_getopt_print_errors) 00446 my_getopt_error_reporter(ERROR_LEVEL, 00447 "%s: option '-%c' requires an argument\n", 00448 my_progname, optp->id); 00449 return EXIT_ARGUMENT_REQUIRED; 00450 } 00451 argument= *++pos; 00452 (*argc)--; 00453 /* the other loop will break, because *optend + 1 == 0 */ 00454 } 00455 } 00456 if ((error= setval(optp, optp->value, argument, 00457 set_maximum_value))) 00458 { 00459 my_getopt_error_reporter(ERROR_LEVEL, 00460 "%s: Error while setting value '%s' to '%s'\n", 00461 my_progname, argument, optp->name); 00462 return error; 00463 } 00464 get_one_option(optp->id, optp, argument); 00465 break; 00466 } 00467 } 00468 if (!opt_found) 00469 { 00470 if (my_getopt_print_errors) 00471 my_getopt_error_reporter(ERROR_LEVEL, 00472 "%s: unknown option '-%c'\n", 00473 my_progname, *optend); 00474 return EXIT_UNKNOWN_OPTION; 00475 } 00476 } 00477 (*argc)--; /* option handled (short), decrease argument count */ 00478 continue; 00479 } 00480 if ((error= setval(optp, value, argument, set_maximum_value))) 00481 { 00482 my_getopt_error_reporter(ERROR_LEVEL, 00483 "%s: Error while setting value '%s' to '%s'\n", 00484 my_progname, argument, optp->name); 00485 return error; 00486 } 00487 get_one_option(optp->id, optp, argument); 00488 00489 (*argc)--; /* option handled (short or long), decrease argument count */ 00490 } 00491 else /* non-option found */ 00492 (*argv)[argvpos++]= cur_arg; 00493 } 00494 /* 00495 Destroy the first, already handled option, so that programs that look 00496 for arguments in 'argv', without checking 'argc', know when to stop. 00497 Items in argv, before the destroyed one, are all non-option -arguments 00498 to the program, yet to be (possibly) handled. 00499 */ 00500 (*argv)[argvpos]= 0; 00501 return 0; 00502 }
Here is the call graph for this function:

| static void init_one_value | ( | const struct my_option * | option, | |
| gptr * | variable, | |||
| longlong | value | |||
| ) | [static] |
Definition at line 775 of file my_getopt.c.
References GET_BOOL, GET_INT, GET_LL, GET_LONG, GET_TYPE_MASK, GET_UINT, GET_ULL, GET_ULONG, int(), and my_option::var_type.
Referenced by init_variables().
00777 { 00778 switch ((option->var_type & GET_TYPE_MASK)) { 00779 case GET_BOOL: 00780 *((my_bool*) variable)= (my_bool) value; 00781 break; 00782 case GET_INT: 00783 *((int*) variable)= (int) value; 00784 break; 00785 case GET_UINT: 00786 *((uint*) variable)= (uint) value; 00787 break; 00788 case GET_LONG: 00789 *((long*) variable)= (long) value; 00790 break; 00791 case GET_ULONG: 00792 *((ulong*) variable)= (ulong) value; 00793 break; 00794 case GET_LL: 00795 *((longlong*) variable)= (longlong) value; 00796 break; 00797 case GET_ULL: 00798 *((ulonglong*) variable)= (ulonglong) value; 00799 break; 00800 default: /* dummy default to avoid compiler warnings */ 00801 break; 00802 } 00803 }
Here is the call graph for this function:

Here is the caller graph for this function:

| static void init_variables | ( | const struct my_option * | options | ) | [static] |
Definition at line 819 of file my_getopt.c.
References GET_ASK_ADDR, getopt_get_addr, init_one_value(), and options().
Referenced by handle_options().
00820 { 00821 for (; options->name; options++) 00822 { 00823 gptr *variable; 00824 /* 00825 We must set u_max_value first as for some variables 00826 options->u_max_value == options->value and in this case we want to 00827 set the value to default value. 00828 */ 00829 if (options->u_max_value) 00830 init_one_value(options, options->u_max_value, options->max_value); 00831 if (options->value) 00832 init_one_value(options, options->value, options->def_value); 00833 if (options->var_type & GET_ASK_ADDR && 00834 (variable= (*getopt_get_addr)("", 0, options))) 00835 init_one_value(options, variable, options->def_value); 00836 } 00837 }
Here is the call graph for this function:

Here is the caller graph for this function:

| void my_getopt_register_get_addr | ( | gptr *(*)(const char *, uint, const struct my_option *) | func_addr | ) |
Definition at line 83 of file my_getopt.c.
References getopt_get_addr.
00085 { 00086 getopt_get_addr= func_addr; 00087 }
| void my_print_help | ( | const struct my_option * | options | ) |
Definition at line 848 of file my_getopt.c.
References my_option::arg_type, comment, my_option::comment, GET_BOOL, GET_NO_ARG, GET_STR, GET_STR_ALLOC, GET_TYPE_MASK, my_option::id, my_option::name, OPT_ARG, options(), strend(), strlen(), and my_option::var_type.
00849 { 00850 uint col, name_space= 22, comment_space= 57; 00851 const char *line_end; 00852 const struct my_option *optp; 00853 00854 for (optp= options; optp->id; optp++) 00855 { 00856 if (optp->id < 256) 00857 { 00858 printf(" -%c%s", optp->id, strlen(optp->name) ? ", " : " "); 00859 col= 6; 00860 } 00861 else 00862 { 00863 printf(" "); 00864 col= 2; 00865 } 00866 if (strlen(optp->name)) 00867 { 00868 printf("--%s", optp->name); 00869 col+= 2 + (uint) strlen(optp->name); 00870 if ((optp->var_type & GET_TYPE_MASK) == GET_STR || 00871 (optp->var_type & GET_TYPE_MASK) == GET_STR_ALLOC) 00872 { 00873 printf("%s=name%s ", optp->arg_type == OPT_ARG ? "[" : "", 00874 optp->arg_type == OPT_ARG ? "]" : ""); 00875 col+= (optp->arg_type == OPT_ARG) ? 8 : 6; 00876 } 00877 else if ((optp->var_type & GET_TYPE_MASK) == GET_NO_ARG || 00878 (optp->var_type & GET_TYPE_MASK) == GET_BOOL) 00879 { 00880 putchar(' '); 00881 col++; 00882 } 00883 else 00884 { 00885 printf("%s=#%s ", optp->arg_type == OPT_ARG ? "[" : "", 00886 optp->arg_type == OPT_ARG ? "]" : ""); 00887 col+= (optp->arg_type == OPT_ARG) ? 5 : 3; 00888 } 00889 if (col > name_space && optp->comment && *optp->comment) 00890 { 00891 putchar('\n'); 00892 col= 0; 00893 } 00894 } 00895 for (; col < name_space; col++) 00896 putchar(' '); 00897 if (optp->comment && *optp->comment) 00898 { 00899 const char *comment= optp->comment, *end= strend(comment); 00900 00901 while ((uint) (end - comment) > comment_space) 00902 { 00903 for (line_end= comment + comment_space; *line_end != ' '; line_end--); 00904 for (; comment != line_end; comment++) 00905 putchar(*comment); 00906 comment++; /* skip the space, as a newline will take it's place now */ 00907 putchar('\n'); 00908 for (col= 0; col < name_space; col++) 00909 putchar(' '); 00910 } 00911 printf("%s", comment); 00912 } 00913 putchar('\n'); 00914 } 00915 }
Here is the call graph for this function:

| void my_print_variables | ( | const struct my_option * | options | ) |
Definition at line 924 of file my_getopt.c.
References GET_ASK_ADDR, GET_BOOL, GET_INT, GET_LL, GET_LONG, GET_STR, GET_STR_ALLOC, GET_TYPE_MASK, GET_UINT, GET_ULL, GET_ULONG, getopt_get_addr, my_option::id, llstr(), longlong2str, my_option::name, options(), strlen(), my_option::value, value, and my_option::var_type.
00925 { 00926 uint name_space= 34, length; 00927 char buff[255]; 00928 const struct my_option *optp; 00929 00930 printf("\nVariables (--variable-name=value)\n"); 00931 printf("and boolean options {FALSE|TRUE} Value (after reading options)\n"); 00932 printf("--------------------------------- -----------------------------\n"); 00933 for (optp= options; optp->id; optp++) 00934 { 00935 gptr *value= (optp->var_type & GET_ASK_ADDR ? 00936 (*getopt_get_addr)("", 0, optp) : optp->value); 00937 if (value) 00938 { 00939 printf("%s", optp->name); 00940 length= (uint) strlen(optp->name); 00941 for (; length < name_space; length++) 00942 putchar(' '); 00943 switch ((optp->var_type & GET_TYPE_MASK)) { 00944 case GET_STR: 00945 case GET_STR_ALLOC: /* fall through */ 00946 printf("%s\n", *((char**) value) ? *((char**) value) : 00947 "(No default value)"); 00948 break; 00949 case GET_BOOL: 00950 printf("%s\n", *((my_bool*) value) ? "TRUE" : "FALSE"); 00951 break; 00952 case GET_INT: 00953 printf("%d\n", *((int*) value)); 00954 break; 00955 case GET_UINT: 00956 printf("%d\n", *((uint*) value)); 00957 break; 00958 case GET_LONG: 00959 printf("%lu\n", *((long*) value)); 00960 break; 00961 case GET_ULONG: 00962 printf("%lu\n", *((ulong*) value)); 00963 break; 00964 case GET_LL: 00965 printf("%s\n", llstr(*((longlong*) value), buff)); 00966 break; 00967 case GET_ULL: 00968 longlong2str(*((ulonglong*) value), buff, 10); 00969 printf("%s\n", buff); 00970 break; 00971 default: 00972 printf("(Disabled)\n"); 00973 break; 00974 } 00975 } 00976 } 00977 }
Here is the call graph for this function:

| static int setval | ( | const struct my_option * | opts, | |
| gptr * | value, | |||
| char * | argument, | |||
| my_bool | set_maximum_value | |||
| ) | [static] |
Definition at line 554 of file my_getopt.c.
References atoi(), err, EXIT_NO_PTR_TO_VARIABLE, EXIT_OUT_OF_MEMORY, EXIT_UNKNOWN_SUFFIX, GET_BOOL, GET_INT, GET_LL, GET_LONG, GET_STR, GET_STR_ALLOC, GET_TYPE_MASK, GET_UINT, GET_ULL, GET_ULONG, getopt_ll(), getopt_ull(), int(), MY_FAE, my_free, my_strdup(), MY_WME, MYF, my_option::u_max_value, and my_option::var_type.
Referenced by handle_options().
00556 { 00557 int err= 0; 00558 00559 if (value && argument) 00560 { 00561 gptr *result_pos= ((set_maximum_value) ? 00562 opts->u_max_value : value); 00563 00564 if (!result_pos) 00565 return EXIT_NO_PTR_TO_VARIABLE; 00566 00567 switch ((opts->var_type & GET_TYPE_MASK)) { 00568 case GET_BOOL: /* If argument differs from 0, enable option, else disable */ 00569 *((my_bool*) result_pos)= (my_bool) atoi(argument) != 0; 00570 break; 00571 case GET_INT: 00572 case GET_UINT: /* fall through */ 00573 *((int*) result_pos)= (int) getopt_ll(argument, opts, &err); 00574 break; 00575 case GET_LONG: 00576 case GET_ULONG: /* fall through */ 00577 *((long*) result_pos)= (long) getopt_ll(argument, opts, &err); 00578 break; 00579 case GET_LL: 00580 *((longlong*) result_pos)= getopt_ll(argument, opts, &err); 00581 break; 00582 case GET_ULL: 00583 *((ulonglong*) result_pos)= getopt_ull(argument, opts, &err); 00584 break; 00585 case GET_STR: 00586 *((char**) result_pos)= argument; 00587 break; 00588 case GET_STR_ALLOC: 00589 if ((*((char**) result_pos))) 00590 my_free((*(char**) result_pos), MYF(MY_WME | MY_FAE)); 00591 if (!(*((char**) result_pos)= my_strdup(argument, MYF(MY_WME)))) 00592 return EXIT_OUT_OF_MEMORY; 00593 break; 00594 default: /* dummy default to avoid compiler warnings */ 00595 break; 00596 } 00597 if (err) 00598 return EXIT_UNKNOWN_SUFFIX; 00599 } 00600 return 0; 00601 }
Here is the call graph for this function:

Here is the caller graph for this function:

| char* disabled_my_option = (char*) "0" |
Definition at line 52 of file my_getopt.c.
gptr*(*) getopt_get_addr(const char *, uint, const struct my_option *) [static] |
Definition at line 81 of file my_getopt.c.
Referenced by init_variables(), my_getopt_register_get_addr(), and my_print_variables().
| my_error_reporter my_getopt_error_reporter = &default_reporter |
Definition at line 25 of file my_getopt.c.
Definition at line 60 of file my_getopt.c.
const char* special_opt_prefix[] [static] |
Initial value:
{"skip", "disable", "enable", "maximum", "loose", 0}
Definition at line 45 of file my_getopt.c.
Referenced by handle_options().
const uint special_opt_prefix_lengths[] [static] |
Initial value:
{ 4, 7, 6, 7, 5, 0}
Definition at line 47 of file my_getopt.c.
Referenced by handle_options().
1.4.7

