~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to mysys/my_getopt.cc

Moved the last of the libdrizzleclient calls into Protocol.

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
15
15
 
16
16
#include "mysys_priv.h"
17
 
#include <libdrizzle/gettext.h>
 
17
#include <drizzled/gettext.h>
18
18
 
19
19
#include <mystrings/m_string.h>
 
20
#include <mysys/my_sys.h>
 
21
#include <mysys/mysys_err.h>
 
22
#include <mysys/my_getopt.h>
 
23
 
 
24
#include <stdio.h>
20
25
#include <stdlib.h>
21
 
#include <my_sys.h>
22
 
#include <mysys_err.h>
23
 
#include <my_getopt.h>
24
26
#include <errno.h>
 
27
#include <iostream>
 
28
 
 
29
using namespace std;
25
30
 
26
31
typedef void (*init_func_p)(const struct my_option *option, char **variable,
27
32
                            int64_t value);
28
33
 
29
 
static void default_reporter(enum loglevel level, const char *format, ...);
 
34
void default_reporter(enum loglevel level, const char *format, ...);
30
35
my_error_reporter my_getopt_error_reporter= &default_reporter;
31
36
 
32
37
static int findopt(char *optpat, uint32_t length,
35
40
static int64_t getopt_ll(char *arg, const struct my_option *optp, int *err);
36
41
static uint64_t getopt_ull(char *arg, const struct my_option *optp,
37
42
                            int *err);
 
43
static size_t getopt_size(char *arg, const struct my_option *optp, int *err);
38
44
static double getopt_double(char *arg, const struct my_option *optp, int *err);
39
45
static void init_variables(const struct my_option *options,
40
46
                           init_func_p init_one_value);
59
65
 
60
66
char *disabled_my_option= (char*) "0";
61
67
 
62
 
/* 
 
68
/*
63
69
   This is a flag that can be set in client programs. 0 means that
64
70
   my_getopt will not print error messages, but the client should do
65
71
   it by itself
67
73
 
68
74
bool my_getopt_print_errors= 1;
69
75
 
70
 
/* 
 
76
/*
71
77
   This is a flag that can be set in client programs. 1 means that
72
78
   my_getopt will skip over options it does not know how to handle.
73
79
*/
74
80
 
75
81
bool my_getopt_skip_unknown= 0;
76
82
 
77
 
static void default_reporter(enum loglevel level,
78
 
                             const char *format, ...)
 
83
void default_reporter(enum loglevel level,
 
84
                      const char *format, ...)
79
85
{
80
86
  va_list args;
81
87
  va_start(args, format);
89
95
  fflush(stderr);
90
96
}
91
97
 
92
 
/* 
 
98
/*
93
99
  function: handle_options
94
100
 
95
101
  Sort options; put options first, until special end of options (--), or
99
105
  one. Call function 'get_one_option()' once for each option.
100
106
*/
101
107
 
102
 
static char** (*getopt_get_addr)(const char *, uint, const struct my_option *);
 
108
static getopt_get_addr_func getopt_get_addr;
103
109
 
104
 
void my_getopt_register_get_addr(char** (*func_addr)(const char *, uint,
105
 
                                                    const struct my_option *))
 
110
void my_getopt_register_get_addr(getopt_get_addr_func func_addr)
106
111
{
107
112
  getopt_get_addr= func_addr;
108
113
}
109
114
 
110
 
int handle_options(int *argc, char ***argv, 
 
115
int handle_options(int *argc, char ***argv,
111
116
                   const struct my_option *longopts,
112
117
                   my_get_one_option get_one_option)
113
118
{
206
211
        optend= strrchr(opt_str, '=');
207
212
        if (optend != NULL)
208
213
        {
209
 
          length= (uint) (optend - opt_str);
 
214
          length= (uint32_t) (optend - opt_str);
210
215
          optend++;
211
216
        }
212
217
        else
299
304
            if (must_be_var)
300
305
            {
301
306
              if (my_getopt_print_errors)
302
 
                my_getopt_error_reporter(option_is_loose ? 
 
307
                my_getopt_error_reporter(option_is_loose ?
303
308
                                           WARNING_LEVEL : ERROR_LEVEL,
304
309
                                         "%s: unknown variable '%s'",
305
310
                                         my_progname, cur_arg);
309
314
            else
310
315
            {
311
316
              if (my_getopt_print_errors)
312
 
                my_getopt_error_reporter(option_is_loose ? 
 
317
                my_getopt_error_reporter(option_is_loose ?
313
318
                                           WARNING_LEVEL : ERROR_LEVEL,
314
 
                                         "%s: unknown option '--%s'", 
 
319
                                         "%s: unknown option '--%s'",
315
320
                                         my_progname, cur_arg);
316
321
              if (!option_is_loose)
317
322
                return EXIT_UNKNOWN_OPTION;
338
343
            if (my_getopt_print_errors)
339
344
              my_getopt_error_reporter(ERROR_LEVEL,
340
345
                                       "%s: ambiguous option '--%s' (%s, %s)",
341
 
                                       my_progname, opt_str, prev_found, 
 
346
                                       my_progname, opt_str, prev_found,
342
347
                                       optp->name);
343
348
            return EXIT_AMBIGUOUS_OPTION;
344
349
          }
359
364
        if (must_be_var && (optp->var_type & GET_TYPE_MASK) == GET_NO_ARG)
360
365
        {
361
366
          if (my_getopt_print_errors)
362
 
            my_getopt_error_reporter(ERROR_LEVEL, 
 
367
            my_getopt_error_reporter(ERROR_LEVEL,
363
368
                                     "%s: option '%s' cannot take an argument",
364
369
                                     my_progname, optp->name);
365
370
          return EXIT_NO_ARGUMENT_ALLOWED;
366
371
        }
367
372
        value= optp->var_type & GET_ASK_ADDR ?
368
 
          (*getopt_get_addr)(key_name, (uint) strlen(key_name), optp) : optp->value;
369
 
  
 
373
          (*getopt_get_addr)(key_name, (uint32_t) strlen(key_name), optp) : optp->value;
 
374
 
370
375
        if (optp->arg_type == NO_ARG)
371
376
        {
372
377
          if (optend && (optp->var_type & GET_TYPE_MASK) != GET_BOOL)
511
516
          {
512
517
            if (my_getopt_print_errors)
513
518
              my_getopt_error_reporter(ERROR_LEVEL,
514
 
                                       "%s: unknown option '-%c'", 
 
519
                                       "%s: unknown option '-%c'",
515
520
                                       my_progname, *optend);
516
521
            return EXIT_UNKNOWN_OPTION;
517
522
          }
565
570
  ptr= strrchr(cur_arg + 1, '.'); /* Skip the first character */
566
571
  end= strrchr(cur_arg, '=');
567
572
 
568
 
  /* 
 
573
  /*
569
574
     If the first dot is after an equal sign, then it is part
570
575
     of a variable value and the option is not a struct option.
571
576
     Also, if the last character in the string before the ending
574
579
  */
575
580
  if ((ptr != NULL) && (end != NULL) && (end - ptr > 1))
576
581
  {
577
 
    uint32_t len= (uint) (ptr - cur_arg);
578
 
    set_if_smaller(len, FN_REFLEN-1);
579
 
    strmake(key_name, cur_arg, len);
 
582
    uint32_t len= (uint32_t) (ptr - cur_arg);
 
583
    set_if_smaller(len, (uint32_t)FN_REFLEN-1);
 
584
    strncpy(key_name, cur_arg, len);
580
585
    return ++ptr;
581
586
  }
582
 
  else
583
 
  {
584
 
    key_name[0]= 0;
585
 
    return cur_arg;
586
 
  }
 
587
  key_name[0]= 0;
 
588
  return cur_arg;
587
589
}
588
590
 
589
591
/*
611
613
      *((bool*) result_pos)= (bool) atoi(argument) != 0;
612
614
      break;
613
615
    case GET_INT:
614
 
      *((int*) result_pos)= (int) getopt_ll(argument, opts, &err);
 
616
      *((int32_t*) result_pos)= (int) getopt_ll(argument, opts, &err);
615
617
      break;
616
618
    case GET_UINT:
617
 
      *((uint*) result_pos)= (uint) getopt_ull(argument, opts, &err);
 
619
    case GET_UINT32:
 
620
      *((uint32_t*) result_pos)= (uint32_t) getopt_ull(argument, opts, &err);
 
621
      break;
 
622
    case GET_ULONG_IS_FAIL:
 
623
      *((ulong*) result_pos)= (ulong) getopt_ull(argument, opts, &err);
618
624
      break;
619
625
    case GET_LONG:
620
626
      *((long*) result_pos)= (long) getopt_ll(argument, opts, &err);
621
627
      break;
622
 
    case GET_ULONG:
623
 
      *((long*) result_pos)= (long) getopt_ull(argument, opts, &err);
624
 
      break;
625
628
    case GET_LL:
626
629
      *((int64_t*) result_pos)= getopt_ll(argument, opts, &err);
627
630
      break;
628
631
    case GET_ULL:
 
632
    case GET_UINT64:
629
633
      *((uint64_t*) result_pos)= getopt_ull(argument, opts, &err);
630
634
      break;
 
635
    case GET_SIZE:
 
636
      *((size_t*) result_pos)= getopt_size(argument, opts, &err);
 
637
      break;
631
638
    case GET_DOUBLE:
632
639
      *((double*) result_pos)= getopt_double(argument, opts, &err);
633
640
      break;
637
644
    case GET_STR_ALLOC:
638
645
      if ((*((char**) result_pos)))
639
646
        free((*(char**) result_pos));
640
 
      if (!(*((char**) result_pos)= my_strdup(argument, MYF(MY_WME))))
 
647
      if (!(*((char**) result_pos)= strdup(argument)))
641
648
        return EXIT_OUT_OF_MEMORY;
642
649
      break;
643
650
    case GET_ENUM:
659
666
}
660
667
 
661
668
 
662
 
/* 
 
669
/*
663
670
  Find option
664
671
 
665
672
  SYNOPSIS
714
721
}
715
722
 
716
723
 
717
 
/* 
 
724
/*
718
725
  function: compare_strings
719
726
 
720
727
  Works like strncmp, other than 1.) considers '-' and '_' the same.
744
751
{
745
752
  char *endchar;
746
753
  int64_t num;
747
 
  
 
754
 
748
755
  *error= 0;
749
756
  errno= 0;
750
757
  num= strtoll(argument, &endchar, 10);
772
779
  return num;
773
780
}
774
781
 
775
 
/* 
 
782
/*
776
783
  function: getopt_ll
777
784
 
778
785
  Evaluates and returns the value that user gave as an argument
820
827
    }
821
828
    break;
822
829
  case GET_LONG:
823
 
#if SIZEOF_LONG < SIZEOF_LONG_LONG
824
 
    if (num > (int64_t) LONG_MAX)
 
830
    if (num > (int64_t) INT32_MAX)
825
831
    {
826
 
      num= ((int64_t) LONG_MAX);
 
832
      num= ((int64_t) INT32_MAX);
827
833
      adjusted= true;
828
834
    }
829
 
#endif
830
835
    break;
831
836
  default:
832
837
    assert((optp->var_type & GET_TYPE_MASK) == GET_LL);
854
859
/*
855
860
  function: getopt_ull
856
861
 
857
 
  This is the same as getopt_ll, but is meant for unsigned long long
 
862
  This is the same as getopt_ll, but is meant for uint64_t
858
863
  values.
859
864
*/
860
865
 
865
870
}
866
871
 
867
872
 
 
873
static size_t getopt_size(char *arg, const struct my_option *optp, int *err)
 
874
{
 
875
  return (size_t)getopt_ull(arg, optp, err);
 
876
}
 
877
 
 
878
 
 
879
 
868
880
uint64_t getopt_ull_limit_value(uint64_t num, const struct my_option *optp,
869
 
                                 bool *fix)
 
881
                                bool *fix)
870
882
{
871
883
  bool adjusted= false;
872
884
  uint64_t old= num;
887
899
      adjusted= true;
888
900
    }
889
901
    break;
890
 
  case GET_ULONG:
891
 
#if SIZEOF_LONG < SIZEOF_LONG_LONG
892
 
    if (num > (uint64_t) ULONG_MAX)
893
 
    {
894
 
      num= ((uint64_t) ULONG_MAX);
895
 
      adjusted= true;
896
 
    }
897
 
#endif
 
902
  case GET_UINT32:
 
903
  case GET_ULONG_IS_FAIL:
 
904
    if (num > (uint64_t) UINT32_MAX)
 
905
    {
 
906
      num= ((uint64_t) UINT32_MAX);
 
907
      adjusted= true;
 
908
    }
 
909
    break;
 
910
  case GET_SIZE:
 
911
    if (num > (uint64_t) SIZE_MAX)
 
912
    {
 
913
      num= ((uint64_t) SIZE_MAX);
 
914
      adjusted= true;
 
915
    }
898
916
    break;
899
917
  default:
900
 
    assert((optp->var_type & GET_TYPE_MASK) == GET_ULL);
 
918
    assert(((optp->var_type & GET_TYPE_MASK) == GET_ULL)
 
919
           || ((optp->var_type & GET_TYPE_MASK) == GET_UINT64));
901
920
    break;
902
921
  }
903
922
 
976
995
    break;
977
996
  case GET_UINT:
978
997
  case GET_ENUM:
979
 
    *((uint*) variable)= (uint) value;
 
998
    *((uint*) variable)= (uint32_t) value;
980
999
    break;
981
1000
  case GET_LONG:
982
1001
    *((long*) variable)= (long) value;
983
1002
    break;
984
 
  case GET_ULONG:
 
1003
  case GET_UINT32:
985
1004
    *((uint32_t*) variable)= (uint32_t) value;
986
1005
    break;
 
1006
  case GET_ULONG_IS_FAIL:
 
1007
    *((ulong*) variable)= (ulong) value;
 
1008
    break;
987
1009
  case GET_LL:
988
1010
    *((int64_t*) variable)= (int64_t) value;
989
1011
    break;
 
1012
  case GET_SIZE:
 
1013
    *((size_t*) variable)= (size_t) value;
 
1014
    break;
990
1015
  case GET_ULL:
991
1016
  case GET_SET:
 
1017
  case GET_UINT64:
992
1018
    *((uint64_t*) variable)=  (uint64_t) value;
993
1019
    break;
994
1020
  case GET_DOUBLE:
1014
1040
    if ((char*) (intptr_t) value)
1015
1041
    {
1016
1042
      free((*(char**) variable));
1017
 
      *((char**) variable)= my_strdup((char*) (intptr_t) value, MYF(MY_WME));
 
1043
      char *tmpptr= strdup((char *) (intptr_t) value);
 
1044
      if (tmpptr != NULL)
 
1045
        *((char**) variable)= tmpptr;
1018
1046
    }
1019
1047
    break;
1020
1048
  default: /* dummy default to avoid compiler warnings */
1034
1062
*/
1035
1063
 
1036
1064
static void fini_one_value(const struct my_option *option, char **variable,
1037
 
                           int64_t value __attribute__ ((unused)))
 
1065
                           int64_t)
1038
1066
{
1039
1067
  switch ((option->var_type & GET_TYPE_MASK)) {
1040
1068
  case GET_STR_ALLOC:
1054
1082
}
1055
1083
 
1056
1084
 
1057
 
/* 
 
1085
/*
1058
1086
  initialize all variables to their default values
1059
1087
 
1060
1088
  SYNOPSIS
1117
1145
    if (strlen(optp->name))
1118
1146
    {
1119
1147
      printf("--%s", optp->name);
1120
 
      col+= 2 + (uint) strlen(optp->name);
 
1148
      col+= 2 + (uint32_t) strlen(optp->name);
1121
1149
      if ((optp->var_type & GET_TYPE_MASK) == GET_STR ||
1122
1150
          (optp->var_type & GET_TYPE_MASK) == GET_STR_ALLOC)
1123
1151
      {
1149
1177
    {
1150
1178
      const char *comment= _(optp->comment), *end= strchr(comment, '\0');
1151
1179
 
1152
 
      while ((uint) (end - comment) > comment_space)
 
1180
      while ((uint32_t) (end - comment) > comment_space)
1153
1181
      {
1154
 
        for (line_end= comment + comment_space; *line_end != ' '; line_end--);
 
1182
        for (line_end= comment + comment_space; *line_end != ' '; line_end--)
 
1183
          {}
1155
1184
        for (; comment != line_end; comment++)
1156
1185
          putchar(*comment);
1157
1186
        comment++; /* skip the space, as a newline will take it's place now */
1197
1226
    if (value)
1198
1227
    {
1199
1228
      printf("%s ", optp->name);
1200
 
      length= (uint) strlen(optp->name)+1;
 
1229
      length= (uint32_t) strlen(optp->name)+1;
1201
1230
      for (; length < name_space; length++)
1202
1231
        putchar(' ');
1203
1232
      switch ((optp->var_type & GET_TYPE_MASK)) {
1233
1262
      case GET_LONG:
1234
1263
        printf("%ld\n", *((long*) value));
1235
1264
        break;
1236
 
      case GET_ULONG:
 
1265
      case GET_UINT32:
1237
1266
        printf("%u\n", *((uint32_t*) value));
1238
1267
        break;
 
1268
      case GET_ULONG_IS_FAIL:
 
1269
        printf("%lu\n", *((ulong*) value));
 
1270
        break;
 
1271
      case GET_SIZE:
 
1272
        cout << value;
 
1273
        break;
1239
1274
      case GET_LL:
1240
1275
        printf("%s\n", llstr(*((int64_t*) value), buff));
1241
1276
        break;
1242
1277
      case GET_ULL:
 
1278
      case GET_UINT64:
1243
1279
        int64_t2str(*((uint64_t*) value), buff, 10);
1244
1280
        printf("%s\n", buff);
1245
1281
        break;