~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to mysys/my_getopt.c

  • Committer: Patrick Galbraith
  • Date: 2008-07-24 16:57:40 UTC
  • mto: (202.2.4 rename-mysql-to-drizzle)
  • mto: This revision was merged to the branch mainline in revision 212.
  • Revision ID: patg@ishvara-20080724165740-x58yw6zs6d9o17lf
Most everything working with client rename
mysqlslap test still fails... can't connect to the server

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>
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
33
static int64_t getopt_ll(char *arg, const struct my_option *optp, int *err);
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};
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);
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;
 
112
  uint opt_found, argvpos= 0, length;
115
113
  bool end_of_options= 0, must_be_var, set_maximum_value=false,
116
114
          option_is_loose;
117
115
  char **pos, **pos_end, *optend, *prev_found=NULL,
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,
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"))
 
382
                !my_strcasecmp(&my_charset_latin1, optend, "true"))
390
383
              *((bool*) value)= (bool) 1;
391
384
            else if (*optend == '0' ||
392
 
                     !my_strcasecmp(&my_charset_utf8_general_ci, optend, "false"))
 
385
                     !my_strcasecmp(&my_charset_latin1, optend, "false"))
393
386
              *((bool*) value)= (bool) 0;
394
387
            else
395
388
            {
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
              }
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;
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;
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++)
722
715
*/
723
716
 
724
717
bool getopt_compare_strings(register const char *s, register const char *t,
725
 
                               uint32_t length)
 
718
                               uint length)
726
719
{
727
720
  char const *end= s + length;
728
721
  for (;s != end ; s++, t++)
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;
820
813
    }
821
814
    break;
822
815
  case GET_LONG:
823
 
    if (num > (int64_t) INT32_MAX)
 
816
#if SIZEOF_LONG < SIZEOF_LONG_LONG
 
817
    if (num > (int64_t) LONG_MAX)
824
818
    {
825
 
      num= ((int64_t) INT32_MAX);
 
819
      num= ((int64_t) LONG_MAX);
826
820
      adjusted= true;
827
821
    }
 
822
#endif
828
823
    break;
829
824
  default:
830
825
    assert((optp->var_type & GET_TYPE_MASK) == GET_LL);
852
847
/*
853
848
  function: getopt_ull
854
849
 
855
 
  This is the same as getopt_ll, but is meant for uint64_t
 
850
  This is the same as getopt_ll, but is meant for unsigned long long
856
851
  values.
857
852
*/
858
853
 
886
881
    }
887
882
    break;
888
883
  case GET_ULONG:
889
 
    if (num > (uint64_t) UINT32_MAX)
 
884
#if SIZEOF_LONG < SIZEOF_LONG_LONG
 
885
    if (num > (uint64_t) ULONG_MAX)
890
886
    {
891
 
      num= ((uint64_t) UINT32_MAX);
 
887
      num= ((uint64_t) ULONG_MAX);
892
888
      adjusted= true;
893
889
    }
 
890
#endif
894
891
    break;
895
892
  default:
896
893
    assert((optp->var_type & GET_TYPE_MASK) == GET_ULL);
941
938
  if (end[0] != 0 || error)
942
939
  {
943
940
    fprintf(stderr,
944
 
            _("%s: ERROR: Invalid decimal value for option '%s'\n"),
 
941
            "%s: ERROR: Invalid decimal value for option '%s'\n",
945
942
            my_progname, optp->name);
946
943
    *err= EXIT_ARGUMENT_INVALID;
947
944
    return 0.0;
948
945
  }
949
946
  if (optp->max_value && num > (double) optp->max_value)
950
947
    num= (double) optp->max_value;
951
 
  return cmax(num, (double) optp->min_value);
 
948
  return max(num, (double) optp->min_value);
952
949
}
953
950
 
954
951
/*
978
975
    *((long*) variable)= (long) value;
979
976
    break;
980
977
  case GET_ULONG:
981
 
    *((uint32_t*) variable)= (uint32_t) value;
 
978
    *((ulong*) variable)= (ulong) value;
982
979
    break;
983
980
  case GET_LL:
984
981
    *((int64_t*) variable)= (int64_t) value;
1009
1006
    */
1010
1007
    if ((char*) (intptr_t) value)
1011
1008
    {
1012
 
      free((*(char**) variable));
 
1009
      my_free((*(char**) variable), MYF(MY_ALLOW_ZERO_PTR));
1013
1010
      *((char**) variable)= my_strdup((char*) (intptr_t) value, MYF(MY_WME));
1014
1011
    }
1015
1012
    break;
1034
1031
{
1035
1032
  switch ((option->var_type & GET_TYPE_MASK)) {
1036
1033
  case GET_STR_ALLOC:
1037
 
    free((*(char**) variable));
 
1034
    my_free((*(char**) variable), MYF(MY_ALLOW_ZERO_PTR));
1038
1035
    *((char**) variable)= NULL;
1039
1036
    break;
1040
1037
  default: /* dummy default to avoid compiler warnings */
1092
1089
  Print help for all options and variables.
1093
1090
*/
1094
1091
 
 
1092
#include <help_start.h>
 
1093
 
1095
1094
void my_print_help(const struct my_option *options)
1096
1095
{
1097
 
  uint32_t col, name_space= 22, comment_space= 57;
 
1096
  uint col, name_space= 22, comment_space= 57;
1098
1097
  const char *line_end;
1099
1098
  const struct my_option *optp;
1100
1099
 
1143
1142
      putchar(' ');
1144
1143
    if (optp->comment && *optp->comment)
1145
1144
    {
1146
 
      const char *comment= _(optp->comment), *end= strchr(comment, '\0');
 
1145
      const char *comment= optp->comment, *end= strend(comment);
1147
1146
 
1148
1147
      while ((uint) (end - comment) > comment_space)
1149
1148
      {
1163
1162
    {
1164
1163
      if (optp->def_value != 0)
1165
1164
      {
1166
 
        printf(_("%*s(Defaults to on; use --skip-%s to disable.)\n"), name_space, "", optp->name);
 
1165
        printf("%*s(Defaults to on; use --skip-%s to disable.)\n", name_space, "", optp->name);
1167
1166
      }
1168
1167
    }
1169
1168
  }
1178
1177
 
1179
1178
void my_print_variables(const struct my_option *options)
1180
1179
{
1181
 
  uint32_t name_space= 34, length, nr;
 
1180
  uint name_space= 34, length, nr;
1182
1181
  uint64_t bit, llvalue;
1183
1182
  char buff[255];
1184
1183
  const struct my_option *optp;
1185
1184
 
1186
 
  printf(_("\nVariables (--variable-name=value)\n"
1187
 
         "and boolean options {false|true}  Value (after reading options)\n"
1188
 
         "--------------------------------- -----------------------------\n"));
 
1185
  printf("\nVariables (--variable-name=value)\n");
 
1186
  printf("and boolean options {false|true}  Value (after reading options)\n");
 
1187
  printf("--------------------------------- -----------------------------\n");
1189
1188
  for (optp= options; optp->id; optp++)
1190
1189
  {
1191
1190
    char* *value= (optp->var_type & GET_ASK_ADDR ?
1199
1198
      switch ((optp->var_type & GET_TYPE_MASK)) {
1200
1199
      case GET_SET:
1201
1200
        if (!(llvalue= *(uint64_t*) value))
1202
 
          printf("%s\n", _("(No default value)"));
 
1201
          printf("%s\n", "(No default value)");
1203
1202
        else
1204
1203
        for (nr= 0, bit= 1; llvalue && nr < optp->typelib->count; nr++, bit<<=1)
1205
1204
        {
1215
1214
      case GET_STR:
1216
1215
      case GET_STR_ALLOC:                    /* fall through */
1217
1216
        printf("%s\n", *((char**) value) ? *((char**) value) :
1218
 
               _("(No default value)"));
 
1217
               "(No default value)");
1219
1218
        break;
1220
1219
      case GET_BOOL:
1221
 
        printf("%s\n", *((bool*) value) ? _("true") : _("false"));
 
1220
        printf("%s\n", *((bool*) value) ? "true" : "false");
1222
1221
        break;
1223
1222
      case GET_INT:
1224
1223
        printf("%d\n", *((int*) value));
1230
1229
        printf("%ld\n", *((long*) value));
1231
1230
        break;
1232
1231
      case GET_ULONG:
1233
 
        printf("%u\n", *((uint32_t*) value));
 
1232
        printf("%lu\n", *((ulong*) value));
1234
1233
        break;
1235
1234
      case GET_LL:
1236
1235
        printf("%s\n", llstr(*((int64_t*) value), buff));
1243
1242
        printf("%g\n", *(double*) value);
1244
1243
        break;
1245
1244
      default:
1246
 
        printf(_("(Disabled)\n"));
 
1245
        printf("(Disabled)\n");
1247
1246
        break;
1248
1247
      }
1249
1248
    }
1250
1249
  }
1251
1250
}
 
1251
 
 
1252
#include <help_end.h>