~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to mysys/my_getopt.c

Removed/replaced DBUG symbols and standardized TRUE/FALSE

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