~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to mysys/my_getopt.c

  • Committer: Brian Aker
  • Date: 2008-07-01 20:14:24 UTC
  • Revision ID: brian@tangent.org-20080701201424-rsof5enxl7gkr50p
More cleanup on pread()

Show diffs side-by-side

added added

removed removed

Lines of Context:
13
13
   along with this program; if not, write to the Free Software
14
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
15
15
 
16
 
#include "mysys_priv.h"
17
 
#include <libdrizzle/gettext.h>
18
 
 
19
 
#include <mystrings/m_string.h>
 
16
#include <my_global.h>
 
17
#include <m_string.h>
20
18
#include <stdlib.h>
21
19
#include <my_sys.h>
22
20
#include <mysys_err.h>
23
21
#include <my_getopt.h>
24
22
#include <errno.h>
25
23
 
26
 
typedef void (*init_func_p)(const struct my_option *option, char **variable,
27
 
                            int64_t value);
 
24
typedef void (*init_func_p)(const struct my_option *option, uchar* *variable,
 
25
                            longlong value);
28
26
 
29
27
static void default_reporter(enum loglevel level, const char *format, ...);
30
28
my_error_reporter my_getopt_error_reporter= &default_reporter;
31
29
 
32
 
static int findopt(char *optpat, uint32_t length,
 
30
static int findopt(char *optpat, uint length,
33
31
                   const struct my_option **opt_res,
34
32
                   char **ffname);
35
 
static int64_t getopt_ll(char *arg, const struct my_option *optp, int *err);
36
 
static uint64_t getopt_ull(char *arg, const struct my_option *optp,
 
33
my_bool getopt_compare_strings(const char *s,
 
34
                               const char *t,
 
35
                               uint length);
 
36
static longlong getopt_ll(char *arg, const struct my_option *optp, int *err);
 
37
static ulonglong getopt_ull(char *arg, const struct my_option *optp,
37
38
                            int *err);
38
39
static double getopt_double(char *arg, const struct my_option *optp, int *err);
39
40
static void init_variables(const struct my_option *options,
40
41
                           init_func_p init_one_value);
41
 
static void init_one_value(const struct my_option *option, char **variable,
42
 
                           int64_t value);
43
 
static void fini_one_value(const struct my_option *option, char **variable,
44
 
                           int64_t value);
45
 
static int setval(const struct my_option *opts, char* *value, char *argument,
46
 
                  bool set_maximum_value);
 
42
static void init_one_value(const struct my_option *option, uchar* *variable,
 
43
                           longlong value);
 
44
static void fini_one_value(const struct my_option *option, uchar* *variable,
 
45
                           longlong value);
 
46
static int setval(const struct my_option *opts, uchar* *value, char *argument,
 
47
                  my_bool set_maximum_value);
47
48
static char *check_struct_option(char *cur_arg, char *key_name);
48
49
 
49
50
/*
52
53
*/
53
54
static const char *special_opt_prefix[]=
54
55
{"skip", "disable", "enable", "maximum", "loose", 0};
55
 
static const uint32_t special_opt_prefix_lengths[]=
 
56
static const uint special_opt_prefix_lengths[]=
56
57
{ 4,      7,         6,        7,         5,      0};
57
58
enum enum_special_opt
58
59
{ OPT_SKIP, OPT_DISABLE, OPT_ENABLE, OPT_MAXIMUM, OPT_LOOSE};
65
66
   it by itself
66
67
*/
67
68
 
68
 
bool my_getopt_print_errors= 1;
 
69
my_bool my_getopt_print_errors= 1;
69
70
 
70
71
/* 
71
72
   This is a flag that can be set in client programs. 1 means that
72
73
   my_getopt will skip over options it does not know how to handle.
73
74
*/
74
75
 
75
 
bool my_getopt_skip_unknown= 0;
 
76
my_bool my_getopt_skip_unknown= 0;
76
77
 
77
78
static void default_reporter(enum loglevel level,
78
79
                             const char *format, ...)
80
81
  va_list args;
81
82
  va_start(args, format);
82
83
  if (level == WARNING_LEVEL)
83
 
    fprintf(stderr, "%s", _("Warning: "));
 
84
    fprintf(stderr, "%s", "Warning: ");
84
85
  else if (level == INFORMATION_LEVEL)
85
 
    fprintf(stderr, "%s", _("Info: "));
 
86
    fprintf(stderr, "%s", "Info: ");
86
87
  vfprintf(stderr, format, args);
87
88
  va_end(args);
88
89
  fputc('\n', stderr);
99
100
  one. Call function 'get_one_option()' once for each option.
100
101
*/
101
102
 
102
 
static char** (*getopt_get_addr)(const char *, uint, const struct my_option *);
 
103
static uchar** (*getopt_get_addr)(const char *, uint, const struct my_option *);
103
104
 
104
 
void my_getopt_register_get_addr(char** (*func_addr)(const char *, uint,
 
105
void my_getopt_register_get_addr(uchar** (*func_addr)(const char *, uint,
105
106
                                                    const struct my_option *))
106
107
{
107
108
  getopt_get_addr= func_addr;
111
112
                   const struct my_option *longopts,
112
113
                   my_get_one_option get_one_option)
113
114
{
114
 
  uint32_t opt_found, argvpos= 0, length;
115
 
  bool end_of_options= 0, must_be_var, set_maximum_value=false,
 
115
  uint opt_found, argvpos= 0, length;
 
116
  my_bool end_of_options= 0, must_be_var, set_maximum_value,
116
117
          option_is_loose;
117
 
  char **pos, **pos_end, *optend, *prev_found=NULL,
 
118
  char **pos, **pos_end, *optend, *prev_found,
118
119
       *opt_str, key_name[FN_REFLEN];
119
120
  const struct my_option *optp;
120
 
  char* *value;
 
121
  uchar* *value;
121
122
  int error, i;
122
123
 
123
124
  /* handle_options() assumes arg0 (program name) always exists */
124
 
  assert(argc && *argc >= 1);
125
 
  assert(argv && *argv);
 
125
  DBUG_ASSERT(argc && *argc >= 1);
 
126
  DBUG_ASSERT(argv && *argv);
126
127
  (*argc)--; /* Skip the program name */
127
128
  (*argv)++; /*      --- || ----      */
128
129
  init_variables(longopts, init_one_value);
203
204
          }
204
205
        }
205
206
        opt_str= check_struct_option(cur_arg, key_name);
206
 
        optend= strrchr(opt_str, '=');
207
 
        if (optend != NULL)
208
 
        {
209
 
          length= (uint) (optend - opt_str);
 
207
        optend= strcend(opt_str, '=');
 
208
        length= (uint) (optend - opt_str);
 
209
        if (*optend == '=')
210
210
          optend++;
211
 
        }
212
211
        else
213
 
        {
214
 
          length= strlen(opt_str);
215
212
          optend= 0;
216
 
        }
217
213
 
218
214
        /*
219
215
          Find first the right option. Return error in case of an ambiguous,
261
257
                  case OPT_DISABLE: /* fall through */
262
258
                    /*
263
259
                      double negation is actually enable again,
264
 
                      for example: --skip-option=0 -> option = true
 
260
                      for example: --skip-option=0 -> option = TRUE
265
261
                    */
266
262
                    optend= (optend && *optend == '0' && !(*(optend + 1))) ?
267
263
                      (char*) "1" : disabled_my_option;
271
267
                      disabled_my_option : (char*) "1";
272
268
                    break;
273
269
                  case OPT_MAXIMUM:
274
 
                    set_maximum_value= true;
275
 
                    must_be_var= true;
 
270
                    set_maximum_value= 1;
 
271
                    must_be_var= 1;
276
272
                    break;
277
273
                  }
278
274
                  break; /* break from the inner loop, main loop continues */
347
343
        {
348
344
          if (my_getopt_print_errors)
349
345
            fprintf(stderr,
350
 
                    _("%s: %s: Option '%s' used, but is disabled\n"), my_progname,
351
 
                    option_is_loose ? _("WARNING") : _("ERROR"), opt_str);
 
346
                    "%s: %s: Option '%s' used, but is disabled\n", my_progname,
 
347
                    option_is_loose ? "WARNING" : "ERROR", opt_str);
352
348
          if (option_is_loose)
353
349
          {
354
350
            (*argc)--;
386
382
              */
387
383
            (*argc)--;
388
384
            if (!optend || *optend == '1' ||
389
 
                !my_strcasecmp(&my_charset_utf8_general_ci, optend, "true"))
390
 
              *((bool*) value)= (bool) 1;
 
385
                !my_strcasecmp(&my_charset_latin1, optend, "true"))
 
386
              *((my_bool*) value)= (my_bool) 1;
391
387
            else if (*optend == '0' ||
392
 
                     !my_strcasecmp(&my_charset_utf8_general_ci, optend, "false"))
393
 
              *((bool*) value)= (bool) 0;
 
388
                     !my_strcasecmp(&my_charset_latin1, optend, "false"))
 
389
              *((my_bool*) value)= (my_bool) 0;
394
390
            else
395
391
            {
396
392
              my_getopt_error_reporter(WARNING_LEVEL,
400
396
              continue;
401
397
            }
402
398
            get_one_option(optp->id, optp,
403
 
                           *((bool*) value) ?
 
399
                           *((my_bool*) value) ?
404
400
                           (char*) "1" : disabled_my_option);
405
401
            continue;
406
402
          }
410
406
                 (optp->var_type & GET_TYPE_MASK) == GET_BOOL)
411
407
        {
412
408
          if (optend == disabled_my_option)
413
 
            *((bool*) value)= (bool) 0;
 
409
            *((my_bool*) value)= (my_bool) 0;
414
410
          else
415
411
          {
416
412
            if (!optend) /* No argument -> enable option */
417
 
              *((bool*) value)= (bool) 1;
 
413
              *((my_bool*) value)= (my_bool) 1;
418
414
            else
419
415
              argument= optend;
420
416
          }
443
439
          opt_found= 0;
444
440
          for (optp= longopts; optp->id; optp++)
445
441
          {
446
 
            if (optp->id == (int) (unsigned char) *optend)
 
442
            if (optp->id == (int) (uchar) *optend)
447
443
            {
448
444
              /* Option recognized. Find next what to do with it */
449
445
              opt_found= 1;
451
447
              {
452
448
                if (my_getopt_print_errors)
453
449
                  fprintf(stderr,
454
 
                          _("%s: ERROR: Option '-%c' used, but is disabled\n"),
 
450
                          "%s: ERROR: Option '-%c' used, but is disabled\n",
455
451
                          my_progname, optp->id);
456
452
                return EXIT_OPTION_DISABLED;
457
453
              }
458
454
              if ((optp->var_type & GET_TYPE_MASK) == GET_BOOL &&
459
455
                  optp->arg_type == NO_ARG)
460
456
              {
461
 
                *((bool*) optp->value)= (bool) 1;
 
457
                *((my_bool*) optp->value)= (my_bool) 1;
462
458
                get_one_option(optp->id, optp, argument);
463
459
                continue;
464
460
              }
477
473
                  if (optp->arg_type == OPT_ARG)
478
474
                  {
479
475
                    if (optp->var_type == GET_BOOL)
480
 
                      *((bool*) optp->value)= (bool) 1;
 
476
                      *((my_bool*) optp->value)= (my_bool) 1;
481
477
                    get_one_option(optp->id, optp, argument);
482
478
                    continue;
483
479
                  }
495
491
                  /* the other loop will break, because *optend + 1 == 0 */
496
492
                }
497
493
              }
498
 
              if ((error= setval(optp, optp->value, argument,
499
 
                                 set_maximum_value)))
500
 
              {
 
494
              if ((error= setval(optp, optp->value, argument,
 
495
                                 set_maximum_value)))
 
496
              {
501
497
                my_getopt_error_reporter(ERROR_LEVEL,
502
498
                                         "%s: Error while setting value '%s' to '%s'",
503
499
                                         my_progname, argument, optp->name);
504
 
                return error;
505
 
              }
506
 
              get_one_option(optp->id, optp, argument);
507
 
              break;
508
 
            }
509
 
          }
510
 
          if (!opt_found)
511
 
          {
512
 
            if (my_getopt_print_errors)
 
500
                return error;
 
501
              }
 
502
              get_one_option(optp->id, optp, argument);
 
503
              break;
 
504
            }
 
505
          }
 
506
          if (!opt_found)
 
507
          {
 
508
            if (my_getopt_print_errors)
513
509
              my_getopt_error_reporter(ERROR_LEVEL,
514
510
                                       "%s: unknown option '-%c'", 
515
511
                                       my_progname, *optend);
516
 
            return EXIT_UNKNOWN_OPTION;
517
 
          }
518
 
        }
519
 
        (*argc)--; /* option handled (short), decrease argument count */
520
 
        continue;
 
512
            return EXIT_UNKNOWN_OPTION;
 
513
          }
 
514
        }
 
515
        (*argc)--; /* option handled (short), decrease argument count */
 
516
        continue;
521
517
      }
522
518
      if ((error= setval(optp, value, argument, set_maximum_value)))
523
519
      {
524
520
        my_getopt_error_reporter(ERROR_LEVEL,
525
521
                                 "%s: Error while setting value '%s' to '%s'",
526
522
                                 my_progname, argument, optp->name);
527
 
        return error;
 
523
        return error;
528
524
      }
529
525
      get_one_option(optp->id, optp, argument);
530
526
 
562
558
{
563
559
  char *ptr, *end;
564
560
 
565
 
  ptr= strrchr(cur_arg + 1, '.'); /* Skip the first character */
566
 
  end= strrchr(cur_arg, '=');
 
561
  ptr= strcend(cur_arg + 1, '.'); /* Skip the first character */
 
562
  end= strcend(cur_arg, '=');
567
563
 
568
564
  /* 
569
565
     If the first dot is after an equal sign, then it is part
572
568
     NULL, or the character right before equal sign is the first
573
569
     dot found, the option is not a struct option.
574
570
  */
575
 
  if ((ptr != NULL) && (end != NULL) && (end - ptr > 1))
 
571
  if (end - ptr > 1)
576
572
  {
577
 
    uint32_t len= (uint) (ptr - cur_arg);
 
573
    uint len= (uint) (ptr - cur_arg);
578
574
    set_if_smaller(len, FN_REFLEN-1);
579
575
    strmake(key_name, cur_arg, len);
580
576
    return ++ptr;
593
589
  Will set the option value to given value
594
590
*/
595
591
 
596
 
static int setval(const struct my_option *opts, char **value, char *argument,
597
 
                  bool set_maximum_value)
 
592
static int setval(const struct my_option *opts, uchar* *value, char *argument,
 
593
                  my_bool set_maximum_value)
598
594
{
599
595
  int err= 0;
600
596
 
601
597
  if (value && argument)
602
598
  {
603
 
    char* *result_pos= ((set_maximum_value) ?
604
 
                        opts->u_max_value : value);
 
599
    uchar* *result_pos= ((set_maximum_value) ?
 
600
                       opts->u_max_value : value);
605
601
 
606
602
    if (!result_pos)
607
603
      return EXIT_NO_PTR_TO_VARIABLE;
608
604
 
609
605
    switch ((opts->var_type & GET_TYPE_MASK)) {
610
606
    case GET_BOOL: /* If argument differs from 0, enable option, else disable */
611
 
      *((bool*) result_pos)= (bool) atoi(argument) != 0;
 
607
      *((my_bool*) result_pos)= (my_bool) atoi(argument) != 0;
612
608
      break;
613
609
    case GET_INT:
614
610
      *((int*) result_pos)= (int) getopt_ll(argument, opts, &err);
623
619
      *((long*) result_pos)= (long) getopt_ull(argument, opts, &err);
624
620
      break;
625
621
    case GET_LL:
626
 
      *((int64_t*) result_pos)= getopt_ll(argument, opts, &err);
 
622
      *((longlong*) result_pos)= getopt_ll(argument, opts, &err);
627
623
      break;
628
624
    case GET_ULL:
629
 
      *((uint64_t*) result_pos)= getopt_ull(argument, opts, &err);
 
625
      *((ulonglong*) result_pos)= getopt_ull(argument, opts, &err);
630
626
      break;
631
627
    case GET_DOUBLE:
632
628
      *((double*) result_pos)= getopt_double(argument, opts, &err);
636
632
      break;
637
633
    case GET_STR_ALLOC:
638
634
      if ((*((char**) result_pos)))
639
 
        free((*(char**) result_pos));
 
635
        my_free((*(char**) result_pos), MYF(MY_WME | MY_FAE));
640
636
      if (!(*((char**) result_pos)= my_strdup(argument, MYF(MY_WME))))
641
637
        return EXIT_OUT_OF_MEMORY;
642
638
      break;
645
641
        return EXIT_ARGUMENT_INVALID;
646
642
      break;
647
643
    case GET_SET:
648
 
      *((uint64_t*)result_pos)= find_typeset(argument, opts->typelib, &err);
 
644
      *((ulonglong*)result_pos)= find_typeset(argument, opts->typelib, &err);
649
645
      if (err)
650
646
        return EXIT_ARGUMENT_INVALID;
651
647
      break;
681
677
        ffname points to first matching option
682
678
*/
683
679
 
684
 
static int findopt(char *optpat, uint32_t length,
 
680
static int findopt(char *optpat, uint length,
685
681
                   const struct my_option **opt_res,
686
682
                   char **ffname)
687
683
{
688
 
  uint32_t count;
 
684
  uint count;
689
685
  struct my_option *opt= (struct my_option *) *opt_res;
690
686
 
691
687
  for (count= 0; opt->name; opt++)
721
717
  2.) Returns -1 if strings differ, 0 if they are equal
722
718
*/
723
719
 
724
 
bool getopt_compare_strings(register const char *s, register const char *t,
725
 
                               uint32_t length)
 
720
my_bool getopt_compare_strings(register const char *s, register const char *t,
 
721
                               uint length)
726
722
{
727
723
  char const *end= s + length;
728
724
  for (;s != end ; s++, t++)
740
736
  be k|K for kilo, m|M for mega or g|G for giga.
741
737
*/
742
738
 
743
 
static int64_t eval_num_suffix(char *argument, int *error, char *option_name)
 
739
static longlong eval_num_suffix(char *argument, int *error, char *option_name)
744
740
{
745
741
  char *endchar;
746
 
  int64_t num;
 
742
  longlong num;
747
743
  
748
744
  *error= 0;
749
745
  errno= 0;
764
760
  else if (*endchar)
765
761
  {
766
762
    fprintf(stderr,
767
 
            _("Unknown suffix '%c' used for variable '%s' (value '%s')\n"),
 
763
            "Unknown suffix '%c' used for variable '%s' (value '%s')\n",
768
764
            *endchar, option_name, argument);
769
765
    *error= 1;
770
766
    return 0;
783
779
  In case of an error, set error value in *err.
784
780
*/
785
781
 
786
 
static int64_t getopt_ll(char *arg, const struct my_option *optp, int *err)
 
782
static longlong getopt_ll(char *arg, const struct my_option *optp, int *err)
787
783
{
788
 
  int64_t num=eval_num_suffix(arg, err, (char*) optp->name);
 
784
  longlong num=eval_num_suffix(arg, err, (char*) optp->name);
789
785
  return getopt_ll_limit_value(num, optp, NULL);
790
786
}
791
787
 
796
792
  Returns "fixed" value.
797
793
*/
798
794
 
799
 
int64_t getopt_ll_limit_value(int64_t num, const struct my_option *optp,
800
 
                               bool *fix)
 
795
longlong getopt_ll_limit_value(longlong num, const struct my_option *optp,
 
796
                               my_bool *fix)
801
797
{
802
 
  int64_t old= num;
803
 
  bool adjusted= false;
 
798
  longlong old= num;
 
799
  my_bool adjusted= FALSE;
804
800
  char buf1[255], buf2[255];
805
 
  uint64_t block_size= (optp->block_size ? (uint64_t) optp->block_size : 1L);
 
801
  ulonglong block_size= (optp->block_size ? (ulonglong) optp->block_size : 1L);
806
802
 
807
 
  if (num > 0 && ((uint64_t) num > (uint64_t) optp->max_value) &&
 
803
  if (num > 0 && ((ulonglong) num > (ulonglong) optp->max_value) &&
808
804
      optp->max_value) /* if max value is not set -> no upper limit */
809
805
  {
810
 
    num= (uint64_t) optp->max_value;
811
 
    adjusted= true;
 
806
    num= (ulonglong) optp->max_value;
 
807
    adjusted= TRUE;
812
808
  }
813
809
 
814
810
  switch ((optp->var_type & GET_TYPE_MASK)) {
815
811
  case GET_INT:
816
 
    if (num > (int64_t) INT_MAX)
 
812
    if (num > (longlong) INT_MAX)
817
813
    {
818
 
      num= ((int64_t) INT_MAX);
819
 
      adjusted= true;
 
814
      num= ((longlong) INT_MAX);
 
815
      adjusted= TRUE;
820
816
    }
821
817
    break;
822
818
  case GET_LONG:
823
 
    if (num > (int64_t) INT32_MAX)
 
819
#if SIZEOF_LONG < SIZEOF_LONG_LONG
 
820
    if (num > (longlong) LONG_MAX)
824
821
    {
825
 
      num= ((int64_t) INT32_MAX);
826
 
      adjusted= true;
 
822
      num= ((longlong) LONG_MAX);
 
823
      adjusted= TRUE;
827
824
    }
 
825
#endif
828
826
    break;
829
827
  default:
830
 
    assert((optp->var_type & GET_TYPE_MASK) == GET_LL);
 
828
    DBUG_ASSERT((optp->var_type & GET_TYPE_MASK) == GET_LL);
831
829
    break;
832
830
  }
833
831
 
834
832
  num= ((num - optp->sub_size) / block_size);
835
 
  num= (int64_t) (num * block_size);
 
833
  num= (longlong) (num * block_size);
836
834
 
837
835
  if (num < optp->min_value)
838
836
  {
839
837
    num= optp->min_value;
840
 
    adjusted= true;
 
838
    adjusted= TRUE;
841
839
  }
842
840
 
843
841
  if (fix)
852
850
/*
853
851
  function: getopt_ull
854
852
 
855
 
  This is the same as getopt_ll, but is meant for uint64_t
 
853
  This is the same as getopt_ll, but is meant for unsigned long long
856
854
  values.
857
855
*/
858
856
 
859
 
static uint64_t getopt_ull(char *arg, const struct my_option *optp, int *err)
 
857
static ulonglong getopt_ull(char *arg, const struct my_option *optp, int *err)
860
858
{
861
 
  uint64_t num= eval_num_suffix(arg, err, (char*) optp->name);
 
859
  ulonglong num= eval_num_suffix(arg, err, (char*) optp->name);
862
860
  return getopt_ull_limit_value(num, optp, NULL);
863
861
}
864
862
 
865
863
 
866
 
uint64_t getopt_ull_limit_value(uint64_t num, const struct my_option *optp,
867
 
                                 bool *fix)
 
864
ulonglong getopt_ull_limit_value(ulonglong num, const struct my_option *optp,
 
865
                                 my_bool *fix)
868
866
{
869
 
  bool adjusted= false;
870
 
  uint64_t old= num;
 
867
  my_bool adjusted= FALSE;
 
868
  ulonglong old= num;
871
869
  char buf1[255], buf2[255];
872
870
 
873
 
  if ((uint64_t) num > (uint64_t) optp->max_value &&
 
871
  if ((ulonglong) num > (ulonglong) optp->max_value &&
874
872
      optp->max_value) /* if max value is not set -> no upper limit */
875
873
  {
876
 
    num= (uint64_t) optp->max_value;
877
 
    adjusted= true;
 
874
    num= (ulonglong) optp->max_value;
 
875
    adjusted= TRUE;
878
876
  }
879
877
 
880
878
  switch ((optp->var_type & GET_TYPE_MASK)) {
881
879
  case GET_UINT:
882
 
    if (num > (uint64_t) UINT_MAX)
 
880
    if (num > (ulonglong) UINT_MAX)
883
881
    {
884
 
      num= ((uint64_t) UINT_MAX);
885
 
      adjusted= true;
 
882
      num= ((ulonglong) UINT_MAX);
 
883
      adjusted= TRUE;
886
884
    }
887
885
    break;
888
886
  case GET_ULONG:
889
 
    if (num > (uint64_t) UINT32_MAX)
 
887
#if SIZEOF_LONG < SIZEOF_LONG_LONG
 
888
    if (num > (ulonglong) ULONG_MAX)
890
889
    {
891
 
      num= ((uint64_t) UINT32_MAX);
892
 
      adjusted= true;
 
890
      num= ((ulonglong) ULONG_MAX);
 
891
      adjusted= TRUE;
893
892
    }
 
893
#endif
894
894
    break;
895
895
  default:
896
 
    assert((optp->var_type & GET_TYPE_MASK) == GET_ULL);
 
896
    DBUG_ASSERT((optp->var_type & GET_TYPE_MASK) == GET_ULL);
897
897
    break;
898
898
  }
899
899
 
900
900
  if (optp->block_size > 1)
901
901
  {
902
 
    num/= (uint64_t) optp->block_size;
903
 
    num*= (uint64_t) optp->block_size;
 
902
    num/= (ulonglong) optp->block_size;
 
903
    num*= (ulonglong) optp->block_size;
904
904
  }
905
905
 
906
 
  if (num < (uint64_t) optp->min_value)
 
906
  if (num < (ulonglong) optp->min_value)
907
907
  {
908
 
    num= (uint64_t) optp->min_value;
909
 
    adjusted= true;
 
908
    num= (ulonglong) optp->min_value;
 
909
    adjusted= TRUE;
910
910
  }
911
911
 
912
912
  if (fix)
941
941
  if (end[0] != 0 || error)
942
942
  {
943
943
    fprintf(stderr,
944
 
            _("%s: ERROR: Invalid decimal value for option '%s'\n"),
 
944
            "%s: ERROR: Invalid decimal value for option '%s'\n",
945
945
            my_progname, optp->name);
946
946
    *err= EXIT_ARGUMENT_INVALID;
947
947
    return 0.0;
948
948
  }
949
949
  if (optp->max_value && num > (double) optp->max_value)
950
950
    num= (double) optp->max_value;
951
 
  return cmax(num, (double) optp->min_value);
 
951
  return max(num, (double) optp->min_value);
952
952
}
953
953
 
954
954
/*
960
960
    value               Pointer to variable
961
961
*/
962
962
 
963
 
static void init_one_value(const struct my_option *option, char** variable,
964
 
                           int64_t value)
 
963
static void init_one_value(const struct my_option *option, uchar* *variable,
 
964
                           longlong value)
965
965
{
 
966
  DBUG_ENTER("init_one_value");
966
967
  switch ((option->var_type & GET_TYPE_MASK)) {
967
968
  case GET_BOOL:
968
 
    *((bool*) variable)= (bool) value;
 
969
    *((my_bool*) variable)= (my_bool) value;
969
970
    break;
970
971
  case GET_INT:
971
972
    *((int*) variable)= (int) value;
978
979
    *((long*) variable)= (long) value;
979
980
    break;
980
981
  case GET_ULONG:
981
 
    *((uint32_t*) variable)= (uint32_t) value;
 
982
    *((ulong*) variable)= (ulong) value;
982
983
    break;
983
984
  case GET_LL:
984
 
    *((int64_t*) variable)= (int64_t) value;
 
985
    *((longlong*) variable)= (longlong) value;
985
986
    break;
986
987
  case GET_ULL:
987
988
  case GET_SET:
988
 
    *((uint64_t*) variable)=  (uint64_t) value;
 
989
    *((ulonglong*) variable)=  (ulonglong) value;
989
990
    break;
990
991
  case GET_DOUBLE:
991
992
    *((double*) variable)=  (double) value;
994
995
    /*
995
996
      Do not clear variable value if it has no default value.
996
997
      The default value may already be set.
997
 
      NOTE: To avoid compiler warnings, we first cast int64_t to intptr_t,
 
998
      NOTE: To avoid compiler warnings, we first cast longlong to intptr,
998
999
      so that the value has the same size as a pointer.
999
1000
    */
1000
 
    if ((char*) (intptr_t) value)
1001
 
      *((char**) variable)= (char*) (intptr_t) value;
 
1001
    if ((char*) (intptr) value)
 
1002
      *((char**) variable)= (char*) (intptr) value;
1002
1003
    break;
1003
1004
  case GET_STR_ALLOC:
1004
1005
    /*
1005
1006
      Do not clear variable value if it has no default value.
1006
1007
      The default value may already be set.
1007
 
      NOTE: To avoid compiler warnings, we first cast int64_t to intptr_t,
 
1008
      NOTE: To avoid compiler warnings, we first cast longlong to intptr,
1008
1009
      so that the value has the same size as a pointer.
1009
1010
    */
1010
 
    if ((char*) (intptr_t) value)
 
1011
    if ((char*) (intptr) value)
1011
1012
    {
1012
 
      free((*(char**) variable));
1013
 
      *((char**) variable)= my_strdup((char*) (intptr_t) value, MYF(MY_WME));
 
1013
      my_free((*(char**) variable), MYF(MY_ALLOW_ZERO_PTR));
 
1014
      *((char**) variable)= my_strdup((char*) (intptr) value, MYF(MY_WME));
1014
1015
    }
1015
1016
    break;
1016
1017
  default: /* dummy default to avoid compiler warnings */
1017
1018
    break;
1018
1019
  }
1019
 
  return;
 
1020
  DBUG_VOID_RETURN;
1020
1021
}
1021
1022
 
1022
1023
 
1029
1030
    value               Pointer to variable
1030
1031
*/
1031
1032
 
1032
 
static void fini_one_value(const struct my_option *option, char **variable,
1033
 
                           int64_t value __attribute__ ((unused)))
 
1033
static void fini_one_value(const struct my_option *option, uchar* *variable,
 
1034
                           longlong value __attribute__ ((unused)))
1034
1035
{
 
1036
  DBUG_ENTER("fini_one_value");
1035
1037
  switch ((option->var_type & GET_TYPE_MASK)) {
1036
1038
  case GET_STR_ALLOC:
1037
 
    free((*(char**) variable));
 
1039
    my_free((*(char**) variable), MYF(MY_ALLOW_ZERO_PTR));
1038
1040
    *((char**) variable)= NULL;
1039
1041
    break;
1040
1042
  default: /* dummy default to avoid compiler warnings */
1041
1043
    break;
1042
1044
  }
1043
 
  return;
 
1045
  DBUG_VOID_RETURN;
1044
1046
}
1045
1047
 
1046
1048
 
1066
1068
static void init_variables(const struct my_option *options,
1067
1069
                           init_func_p init_one_value)
1068
1070
{
 
1071
  DBUG_ENTER("init_variables");
1069
1072
  for (; options->name; options++)
1070
1073
  {
1071
 
    char* *variable;
 
1074
    uchar* *variable;
 
1075
    DBUG_PRINT("options", ("name: '%s'", options->name));
1072
1076
    /*
1073
1077
      We must set u_max_value first as for some variables
1074
1078
      options->u_max_value == options->value and in this case we want to
1082
1086
        (variable= (*getopt_get_addr)("", 0, options)))
1083
1087
      init_one_value(options, variable, options->def_value);
1084
1088
  }
1085
 
  return;
 
1089
  DBUG_VOID_RETURN;
1086
1090
}
1087
1091
 
1088
1092
 
1092
1096
  Print help for all options and variables.
1093
1097
*/
1094
1098
 
 
1099
#include <help_start.h>
 
1100
 
1095
1101
void my_print_help(const struct my_option *options)
1096
1102
{
1097
 
  uint32_t col, name_space= 22, comment_space= 57;
 
1103
  uint col, name_space= 22, comment_space= 57;
1098
1104
  const char *line_end;
1099
1105
  const struct my_option *optp;
1100
1106
 
1143
1149
      putchar(' ');
1144
1150
    if (optp->comment && *optp->comment)
1145
1151
    {
1146
 
      const char *comment= _(optp->comment), *end= strchr(comment, '\0');
 
1152
      const char *comment= optp->comment, *end= strend(comment);
1147
1153
 
1148
1154
      while ((uint) (end - comment) > comment_space)
1149
1155
      {
1163
1169
    {
1164
1170
      if (optp->def_value != 0)
1165
1171
      {
1166
 
        printf(_("%*s(Defaults to on; use --skip-%s to disable.)\n"), name_space, "", optp->name);
 
1172
        printf("%*s(Defaults to on; use --skip-%s to disable.)\n", name_space, "", optp->name);
1167
1173
      }
1168
1174
    }
1169
1175
  }
1178
1184
 
1179
1185
void my_print_variables(const struct my_option *options)
1180
1186
{
1181
 
  uint32_t name_space= 34, length, nr;
1182
 
  uint64_t bit, llvalue;
 
1187
  uint name_space= 34, length, nr;
 
1188
  ulonglong bit, llvalue;
1183
1189
  char buff[255];
1184
1190
  const struct my_option *optp;
1185
1191
 
1186
 
  printf(_("\nVariables (--variable-name=value)\n"
1187
 
         "and boolean options {false|true}  Value (after reading options)\n"
1188
 
         "--------------------------------- -----------------------------\n"));
 
1192
  printf("\nVariables (--variable-name=value)\n");
 
1193
  printf("and boolean options {FALSE|TRUE}  Value (after reading options)\n");
 
1194
  printf("--------------------------------- -----------------------------\n");
1189
1195
  for (optp= options; optp->id; optp++)
1190
1196
  {
1191
 
    char* *value= (optp->var_type & GET_ASK_ADDR ?
 
1197
    uchar* *value= (optp->var_type & GET_ASK_ADDR ?
1192
1198
                  (*getopt_get_addr)("", 0, optp) : optp->value);
1193
1199
    if (value)
1194
1200
    {
1198
1204
        putchar(' ');
1199
1205
      switch ((optp->var_type & GET_TYPE_MASK)) {
1200
1206
      case GET_SET:
1201
 
        if (!(llvalue= *(uint64_t*) value))
1202
 
          printf("%s\n", _("(No default value)"));
 
1207
        if (!(llvalue= *(ulonglong*) value))
 
1208
          printf("%s\n", "(No default value)");
1203
1209
        else
1204
1210
        for (nr= 0, bit= 1; llvalue && nr < optp->typelib->count; nr++, bit<<=1)
1205
1211
        {
1215
1221
      case GET_STR:
1216
1222
      case GET_STR_ALLOC:                    /* fall through */
1217
1223
        printf("%s\n", *((char**) value) ? *((char**) value) :
1218
 
               _("(No default value)"));
 
1224
               "(No default value)");
1219
1225
        break;
1220
1226
      case GET_BOOL:
1221
 
        printf("%s\n", *((bool*) value) ? _("true") : _("false"));
 
1227
        printf("%s\n", *((my_bool*) value) ? "TRUE" : "FALSE");
1222
1228
        break;
1223
1229
      case GET_INT:
1224
1230
        printf("%d\n", *((int*) value));
1230
1236
        printf("%ld\n", *((long*) value));
1231
1237
        break;
1232
1238
      case GET_ULONG:
1233
 
        printf("%u\n", *((uint32_t*) value));
 
1239
        printf("%lu\n", *((ulong*) value));
1234
1240
        break;
1235
1241
      case GET_LL:
1236
 
        printf("%s\n", llstr(*((int64_t*) value), buff));
 
1242
        printf("%s\n", llstr(*((longlong*) value), buff));
1237
1243
        break;
1238
1244
      case GET_ULL:
1239
 
        int64_t2str(*((uint64_t*) value), buff, 10);
 
1245
        longlong2str(*((ulonglong*) value), buff, 10);
1240
1246
        printf("%s\n", buff);
1241
1247
        break;
1242
1248
      case GET_DOUBLE:
1243
1249
        printf("%g\n", *(double*) value);
1244
1250
        break;
1245
1251
      default:
1246
 
        printf(_("(Disabled)\n"));
 
1252
        printf("(Disabled)\n");
1247
1253
        break;
1248
1254
      }
1249
1255
    }
1250
1256
  }
1251
1257
}
 
1258
 
 
1259
#include <help_end.h>