~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to mysys/my_getopt.c

  • Committer: Monty Taylor
  • Date: 2008-08-05 17:22:51 UTC
  • mto: (266.1.1 codestyle)
  • mto: This revision was merged to the branch mainline in revision 266.
  • Revision ID: monty@inaugust.com-20080805172251-tk91a74duyahmq65
Carried over longlong->int64_t change to assembler file.

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 <drizzled/gettext.h>
18
17
 
19
18
#include <mystrings/m_string.h>
20
19
#include <stdlib.h>
29
28
static void default_reporter(enum loglevel level, const char *format, ...);
30
29
my_error_reporter my_getopt_error_reporter= &default_reporter;
31
30
 
32
 
static int findopt(char *optpat, uint32_t length,
 
31
static int findopt(char *optpat, uint length,
33
32
                   const struct my_option **opt_res,
34
33
                   char **ffname);
35
34
static int64_t getopt_ll(char *arg, const struct my_option *optp, int *err);
52
51
*/
53
52
static const char *special_opt_prefix[]=
54
53
{"skip", "disable", "enable", "maximum", "loose", 0};
55
 
static const uint32_t special_opt_prefix_lengths[]=
 
54
static const uint special_opt_prefix_lengths[]=
56
55
{ 4,      7,         6,        7,         5,      0};
57
56
enum enum_special_opt
58
57
{ OPT_SKIP, OPT_DISABLE, OPT_ENABLE, OPT_MAXIMUM, OPT_LOOSE};
80
79
  va_list args;
81
80
  va_start(args, format);
82
81
  if (level == WARNING_LEVEL)
83
 
    fprintf(stderr, "%s", _("Warning: "));
 
82
    fprintf(stderr, "%s", "Warning: ");
84
83
  else if (level == INFORMATION_LEVEL)
85
 
    fprintf(stderr, "%s", _("Info: "));
 
84
    fprintf(stderr, "%s", "Info: ");
86
85
  vfprintf(stderr, format, args);
87
86
  va_end(args);
88
87
  fputc('\n', stderr);
111
110
                   const struct my_option *longopts,
112
111
                   my_get_one_option get_one_option)
113
112
{
114
 
  uint32_t opt_found, argvpos= 0, length;
 
113
  uint opt_found, argvpos= 0, length;
115
114
  bool end_of_options= 0, must_be_var, set_maximum_value=false,
116
115
          option_is_loose;
117
116
  char **pos, **pos_end, *optend, *prev_found=NULL,
203
202
          }
204
203
        }
205
204
        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);
 
205
        optend= strcend(opt_str, '=');
 
206
        length= (uint) (optend - opt_str);
 
207
        if (*optend == '=')
210
208
          optend++;
211
 
        }
212
209
        else
213
 
        {
214
 
          length= strlen(opt_str);
215
210
          optend= 0;
216
 
        }
217
211
 
218
212
        /*
219
213
          Find first the right option. Return error in case of an ambiguous,
347
341
        {
348
342
          if (my_getopt_print_errors)
349
343
            fprintf(stderr,
350
 
                    _("%s: %s: Option '%s' used, but is disabled\n"), my_progname,
351
 
                    option_is_loose ? _("WARNING") : _("ERROR"), opt_str);
 
344
                    "%s: %s: Option '%s' used, but is disabled\n", my_progname,
 
345
                    option_is_loose ? "WARNING" : "ERROR", opt_str);
352
346
          if (option_is_loose)
353
347
          {
354
348
            (*argc)--;
386
380
              */
387
381
            (*argc)--;
388
382
            if (!optend || *optend == '1' ||
389
 
                !my_strcasecmp(&my_charset_utf8_general_ci, optend, "true"))
 
383
                !my_strcasecmp(&my_charset_latin1, optend, "true"))
390
384
              *((bool*) value)= (bool) 1;
391
385
            else if (*optend == '0' ||
392
 
                     !my_strcasecmp(&my_charset_utf8_general_ci, optend, "false"))
 
386
                     !my_strcasecmp(&my_charset_latin1, optend, "false"))
393
387
              *((bool*) value)= (bool) 0;
394
388
            else
395
389
            {
443
437
          opt_found= 0;
444
438
          for (optp= longopts; optp->id; optp++)
445
439
          {
446
 
            if (optp->id == (int) (unsigned char) *optend)
 
440
            if (optp->id == (int) (uchar) *optend)
447
441
            {
448
442
              /* Option recognized. Find next what to do with it */
449
443
              opt_found= 1;
451
445
              {
452
446
                if (my_getopt_print_errors)
453
447
                  fprintf(stderr,
454
 
                          _("%s: ERROR: Option '-%c' used, but is disabled\n"),
 
448
                          "%s: ERROR: Option '-%c' used, but is disabled\n",
455
449
                          my_progname, optp->id);
456
450
                return EXIT_OPTION_DISABLED;
457
451
              }
562
556
{
563
557
  char *ptr, *end;
564
558
 
565
 
  ptr= strrchr(cur_arg + 1, '.'); /* Skip the first character */
566
 
  end= strrchr(cur_arg, '=');
 
559
  ptr= strcend(cur_arg + 1, '.'); /* Skip the first character */
 
560
  end= strcend(cur_arg, '=');
567
561
 
568
562
  /* 
569
563
     If the first dot is after an equal sign, then it is part
572
566
     NULL, or the character right before equal sign is the first
573
567
     dot found, the option is not a struct option.
574
568
  */
575
 
  if ((ptr != NULL) && (end != NULL) && (end - ptr > 1))
 
569
  if (end - ptr > 1)
576
570
  {
577
 
    uint32_t len= (uint) (ptr - cur_arg);
 
571
    uint len= (uint) (ptr - cur_arg);
578
572
    set_if_smaller(len, FN_REFLEN-1);
579
573
    strmake(key_name, cur_arg, len);
580
574
    return ++ptr;
636
630
      break;
637
631
    case GET_STR_ALLOC:
638
632
      if ((*((char**) result_pos)))
639
 
        free((*(char**) result_pos));
 
633
        my_free((*(char**) result_pos), MYF(MY_WME | MY_FAE));
640
634
      if (!(*((char**) result_pos)= my_strdup(argument, MYF(MY_WME))))
641
635
        return EXIT_OUT_OF_MEMORY;
642
636
      break;
681
675
        ffname points to first matching option
682
676
*/
683
677
 
684
 
static int findopt(char *optpat, uint32_t length,
 
678
static int findopt(char *optpat, uint length,
685
679
                   const struct my_option **opt_res,
686
680
                   char **ffname)
687
681
{
688
 
  uint32_t count;
 
682
  uint count;
689
683
  struct my_option *opt= (struct my_option *) *opt_res;
690
684
 
691
685
  for (count= 0; opt->name; opt++)
722
716
*/
723
717
 
724
718
bool getopt_compare_strings(register const char *s, register const char *t,
725
 
                               uint32_t length)
 
719
                               uint length)
726
720
{
727
721
  char const *end= s + length;
728
722
  for (;s != end ; s++, t++)
764
758
  else if (*endchar)
765
759
  {
766
760
    fprintf(stderr,
767
 
            _("Unknown suffix '%c' used for variable '%s' (value '%s')\n"),
 
761
            "Unknown suffix '%c' used for variable '%s' (value '%s')\n",
768
762
            *endchar, option_name, argument);
769
763
    *error= 1;
770
764
    return 0;
820
814
    }
821
815
    break;
822
816
  case GET_LONG:
823
 
    if (num > (int64_t) INT32_MAX)
 
817
#if SIZEOF_LONG < SIZEOF_LONG_LONG
 
818
    if (num > (int64_t) LONG_MAX)
824
819
    {
825
 
      num= ((int64_t) INT32_MAX);
 
820
      num= ((int64_t) LONG_MAX);
826
821
      adjusted= true;
827
822
    }
 
823
#endif
828
824
    break;
829
825
  default:
830
826
    assert((optp->var_type & GET_TYPE_MASK) == GET_LL);
852
848
/*
853
849
  function: getopt_ull
854
850
 
855
 
  This is the same as getopt_ll, but is meant for uint64_t
 
851
  This is the same as getopt_ll, but is meant for unsigned long long
856
852
  values.
857
853
*/
858
854
 
886
882
    }
887
883
    break;
888
884
  case GET_ULONG:
889
 
    if (num > (uint64_t) UINT32_MAX)
 
885
#if SIZEOF_LONG < SIZEOF_LONG_LONG
 
886
    if (num > (uint64_t) ULONG_MAX)
890
887
    {
891
 
      num= ((uint64_t) UINT32_MAX);
 
888
      num= ((uint64_t) ULONG_MAX);
892
889
      adjusted= true;
893
890
    }
 
891
#endif
894
892
    break;
895
893
  default:
896
894
    assert((optp->var_type & GET_TYPE_MASK) == GET_ULL);
941
939
  if (end[0] != 0 || error)
942
940
  {
943
941
    fprintf(stderr,
944
 
            _("%s: ERROR: Invalid decimal value for option '%s'\n"),
 
942
            "%s: ERROR: Invalid decimal value for option '%s'\n",
945
943
            my_progname, optp->name);
946
944
    *err= EXIT_ARGUMENT_INVALID;
947
945
    return 0.0;
948
946
  }
949
947
  if (optp->max_value && num > (double) optp->max_value)
950
948
    num= (double) optp->max_value;
951
 
  return cmax(num, (double) optp->min_value);
 
949
  return max(num, (double) optp->min_value);
952
950
}
953
951
 
954
952
/*
978
976
    *((long*) variable)= (long) value;
979
977
    break;
980
978
  case GET_ULONG:
981
 
    *((uint32_t*) variable)= (uint32_t) value;
 
979
    *((ulong*) variable)= (ulong) value;
982
980
    break;
983
981
  case GET_LL:
984
982
    *((int64_t*) variable)= (int64_t) value;
1009
1007
    */
1010
1008
    if ((char*) (intptr_t) value)
1011
1009
    {
1012
 
      free((*(char**) variable));
 
1010
      my_free((*(char**) variable), MYF(MY_ALLOW_ZERO_PTR));
1013
1011
      *((char**) variable)= my_strdup((char*) (intptr_t) value, MYF(MY_WME));
1014
1012
    }
1015
1013
    break;
1034
1032
{
1035
1033
  switch ((option->var_type & GET_TYPE_MASK)) {
1036
1034
  case GET_STR_ALLOC:
1037
 
    free((*(char**) variable));
 
1035
    my_free((*(char**) variable), MYF(MY_ALLOW_ZERO_PTR));
1038
1036
    *((char**) variable)= NULL;
1039
1037
    break;
1040
1038
  default: /* dummy default to avoid compiler warnings */
1094
1092
 
1095
1093
void my_print_help(const struct my_option *options)
1096
1094
{
1097
 
  uint32_t col, name_space= 22, comment_space= 57;
 
1095
  uint col, name_space= 22, comment_space= 57;
1098
1096
  const char *line_end;
1099
1097
  const struct my_option *optp;
1100
1098
 
1143
1141
      putchar(' ');
1144
1142
    if (optp->comment && *optp->comment)
1145
1143
    {
1146
 
      const char *comment= _(optp->comment), *end= strchr(comment, '\0');
 
1144
      const char *comment= gettext(optp->comment), *end= strend(comment);
1147
1145
 
1148
1146
      while ((uint) (end - comment) > comment_space)
1149
1147
      {
1150
 
        for (line_end= comment + comment_space; *line_end != ' '; line_end--)
1151
 
          {}
 
1148
        for (line_end= comment + comment_space; *line_end != ' '; line_end--);
1152
1149
        for (; comment != line_end; comment++)
1153
1150
          putchar(*comment);
1154
1151
        comment++; /* skip the space, as a newline will take it's place now */
1164
1161
    {
1165
1162
      if (optp->def_value != 0)
1166
1163
      {
1167
 
        printf(_("%*s(Defaults to on; use --skip-%s to disable.)\n"), name_space, "", optp->name);
 
1164
        printf(gettext("%*s(Defaults to on; use --skip-%s to disable.)\n"), name_space, "", optp->name);
1168
1165
      }
1169
1166
    }
1170
1167
  }
1179
1176
 
1180
1177
void my_print_variables(const struct my_option *options)
1181
1178
{
1182
 
  uint32_t name_space= 34, length, nr;
 
1179
  uint name_space= 34, length, nr;
1183
1180
  uint64_t bit, llvalue;
1184
1181
  char buff[255];
1185
1182
  const struct my_option *optp;
1186
1183
 
1187
 
  printf(_("\nVariables (--variable-name=value)\n"
1188
 
         "and boolean options {false|true}  Value (after reading options)\n"
1189
 
         "--------------------------------- -----------------------------\n"));
 
1184
  printf("\nVariables (--variable-name=value)\n");
 
1185
  printf("and boolean options {false|true}  Value (after reading options)\n");
 
1186
  printf("--------------------------------- -----------------------------\n");
1190
1187
  for (optp= options; optp->id; optp++)
1191
1188
  {
1192
1189
    char* *value= (optp->var_type & GET_ASK_ADDR ?
1200
1197
      switch ((optp->var_type & GET_TYPE_MASK)) {
1201
1198
      case GET_SET:
1202
1199
        if (!(llvalue= *(uint64_t*) value))
1203
 
          printf("%s\n", _("(No default value)"));
 
1200
          printf("%s\n", "(No default value)");
1204
1201
        else
1205
1202
        for (nr= 0, bit= 1; llvalue && nr < optp->typelib->count; nr++, bit<<=1)
1206
1203
        {
1216
1213
      case GET_STR:
1217
1214
      case GET_STR_ALLOC:                    /* fall through */
1218
1215
        printf("%s\n", *((char**) value) ? *((char**) value) :
1219
 
               _("(No default value)"));
 
1216
               "(No default value)");
1220
1217
        break;
1221
1218
      case GET_BOOL:
1222
 
        printf("%s\n", *((bool*) value) ? _("true") : _("false"));
 
1219
        printf("%s\n", *((bool*) value) ? "true" : "false");
1223
1220
        break;
1224
1221
      case GET_INT:
1225
1222
        printf("%d\n", *((int*) value));
1231
1228
        printf("%ld\n", *((long*) value));
1232
1229
        break;
1233
1230
      case GET_ULONG:
1234
 
        printf("%u\n", *((uint32_t*) value));
 
1231
        printf("%lu\n", *((ulong*) value));
1235
1232
        break;
1236
1233
      case GET_LL:
1237
1234
        printf("%s\n", llstr(*((int64_t*) value), buff));
1244
1241
        printf("%g\n", *(double*) value);
1245
1242
        break;
1246
1243
      default:
1247
 
        printf(_("(Disabled)\n"));
 
1244
        printf("(Disabled)\n");
1248
1245
        break;
1249
1246
      }
1250
1247
    }