~drizzle-trunk/drizzle/development

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/* Copyright (C) 2002-2006 MySQL AB

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; version 2 of the License.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */

#include <config.h>
#include <drizzled/definitions.h>
#include <drizzled/charset_info.h>
#include <drizzled/internal/my_sys.h>
#include <drizzled/gettext.h>

#include <drizzled/internal/m_string.h>
#include <drizzled/internal/my_sys.h>
#include <drizzled/error.h>
#include <drizzled/option.h>
#include <drizzled/typelib.h>

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <iostream>
#include <algorithm>

using namespace std;

namespace drizzled {

  typedef void (*init_func_p)(const struct option *option, char **variable,
      int64_t value);

  void default_reporter(enum loglevel level, const char *format, ...);
  my_error_reporter my_getopt_error_reporter= &default_reporter;

  /*
     The following three variables belong to same group and the number and
     order of their arguments must correspond to each other.
   */
  static const uint32_t special_opt_prefix_lengths[]=
  { 4,      7,         6,        7,         5,      0};
  enum enum_special_opt
  { OPT_SKIP, OPT_DISABLE, OPT_ENABLE, OPT_MAXIMUM, OPT_LOOSE};

  char *disabled_my_option= (char*) "0";

  /*
     This is a flag that can be set in client programs. 1 means that
     my_getopt will skip over options it does not know how to handle.
   */

  bool my_getopt_skip_unknown= 0;

  void default_reporter(enum loglevel level, const char *format, ...)
  {
    va_list args;
    va_start(args, format);
    if (level == WARNING_LEVEL)
      fprintf(stderr, "%s", _("Warning: "));
    else if (level == INFORMATION_LEVEL)
      fprintf(stderr, "%s", _("Info: "));
    vfprintf(stderr, format, args);
    va_end(args);
    fputc('\n', stderr);
    fflush(stderr);
  }

  /*
function: compare_strings

Works like strncmp, other than 1.) considers '-' and '_' the same.
2.) Returns -1 if strings differ, 0 if they are equal
   */

  bool getopt_compare_strings(const char *s, const char *t,
      uint32_t length)
  {
    char const *end= s + length;
    for (;s != end ; s++, t++)
    {
      if ((*s != '-' ? *s : '_') != (*t != '-' ? *t : '_'))
        return 1;
    }
    return 0;
  }

  /*
function: getopt_ll_limit_value

Applies min/max/block_size to a numeric value of an option.
Returns "fixed" value.
   */

  int64_t getopt_ll_limit_value(int64_t num, const struct option *optp,
      bool *fix)
  {
    int64_t old= num;
    bool adjusted= false;
    char buf1[255], buf2[255];
    uint64_t block_size= (optp->block_size ? (uint64_t) optp->block_size : 1L);

    if (num > 0 && ((uint64_t) num > (uint64_t) optp->max_value) &&
        optp->max_value) /* if max value is not set -> no upper limit */
    {
      num= (uint64_t) optp->max_value;
      adjusted= true;
    }

    switch ((optp->var_type & GET_TYPE_MASK)) {
      case GET_INT:
        if (num > (int64_t) INT_MAX)
        {
          num= ((int64_t) INT_MAX);
          adjusted= true;
        }
        break;
      case GET_LONG:
        if (num > (int64_t) INT32_MAX)
        {
          num= ((int64_t) INT32_MAX);
          adjusted= true;
        }
        break;
      default:
        assert((optp->var_type & GET_TYPE_MASK) == GET_LL);
        break;
    }

    num= ((num - optp->sub_size) / block_size);
    num= (int64_t) (num * block_size);

    if (num < optp->min_value)
    {
      num= optp->min_value;
      adjusted= true;
    }

    if (fix)
      *fix= adjusted;
    else if (adjusted)
      my_getopt_error_reporter(WARNING_LEVEL,
          "option '%s': signed value %s adjusted to %s",
          optp->name, internal::llstr(old, buf1), internal::llstr(num, buf2));
    return num;
  }

  /*
function: getopt_ull

This is the same as getopt_ll, but is meant for uint64_t
values.
   */

  uint64_t getopt_ull_limit_value(uint64_t num, const struct option *optp,
      bool *fix)
  {
    bool adjusted= false;
    uint64_t old= num;
    char buf1[255], buf2[255];

    if ((uint64_t) num > (uint64_t) optp->max_value &&
        optp->max_value) /* if max value is not set -> no upper limit */
    {
      num= (uint64_t) optp->max_value;
      adjusted= true;
    }

    switch ((optp->var_type & GET_TYPE_MASK)) {
      case GET_UINT:
        if (num > (uint64_t) UINT_MAX)
        {
          num= ((uint64_t) UINT_MAX);
          adjusted= true;
        }
        break;
      case GET_UINT32:
      case GET_ULONG_IS_FAIL:
        if (num > (uint64_t) UINT32_MAX)
        {
          num= ((uint64_t) UINT32_MAX);
          adjusted= true;
        }
        break;
      case GET_SIZE:
        if (num > (uint64_t) SIZE_MAX)
        {
          num= ((uint64_t) SIZE_MAX);
          adjusted= true;
        }
        break;
      default:
        assert(((optp->var_type & GET_TYPE_MASK) == GET_ULL)
            || ((optp->var_type & GET_TYPE_MASK) == GET_UINT64));
        break;
    }

    if (optp->block_size > 1)
    {
      num/= (uint64_t) optp->block_size;
      num*= (uint64_t) optp->block_size;
    }

    if (num < (uint64_t) optp->min_value)
    {
      num= (uint64_t) optp->min_value;
      adjusted= true;
    }

    if (fix)
      *fix= adjusted;
    else if (adjusted)
      my_getopt_error_reporter(WARNING_LEVEL,
          "option '%s': unsigned value %s adjusted to %s",
          optp->name, internal::ullstr(old, buf1), internal::ullstr(num, buf2));

    return num;
  }

  
  /*
function: my_print_options

Print help for all options and variables.
   */

  void my_print_help(const struct option *options)
  {
    uint32_t col, name_space= 22, comment_space= 57;
    const char *line_end;
    const struct option *optp;

    for (optp= options; optp->id; optp++)
    {
      if (optp->id < 256)
      {
        printf("  -%c%s", optp->id, strlen(optp->name) ? ", " : "  ");
        col= 6;
      }
      else
      {
        printf("  ");
        col= 2;
      }
      if (strlen(optp->name))
      {
        printf("--%s", optp->name);
        col+= 2 + (uint32_t) strlen(optp->name);
        if ((optp->var_type & GET_TYPE_MASK) == GET_STR ||
            (optp->var_type & GET_TYPE_MASK) == GET_STR_ALLOC)
        {
          printf("%s=name%s ", optp->arg_type == OPT_ARG ? "[" : "",
              optp->arg_type == OPT_ARG ? "]" : "");
          col+= (optp->arg_type == OPT_ARG) ? 8 : 6;
        }
        else if ((optp->var_type & GET_TYPE_MASK) == GET_NO_ARG ||
            (optp->var_type & GET_TYPE_MASK) == GET_BOOL)
        {
          putchar(' ');
          col++;
        }
        else
        {
          printf("%s=#%s ", optp->arg_type == OPT_ARG ? "[" : "",
              optp->arg_type == OPT_ARG ? "]" : "");
          col+= (optp->arg_type == OPT_ARG) ? 5 : 3;
        }
        if (col > name_space && optp->comment && *optp->comment)
        {
          putchar('\n');
          col= 0;
        }
      }
      for (; col < name_space; col++)
        putchar(' ');
      if (optp->comment && *optp->comment)
      {
        const char *comment= _(optp->comment), *end= strchr(comment, '\0');

        while ((uint32_t) (end - comment) > comment_space)
        {
          for (line_end= comment + comment_space; *line_end != ' '; line_end--)
          {}
          for (; comment != line_end; comment++)
            putchar(*comment);
          comment++; /* skip the space, as a newline will take it's place now */
          putchar('\n');
          for (col= 0; col < name_space; col++)
            putchar(' ');
        }
        printf("%s", comment);
      }
      putchar('\n');
      if ((optp->var_type & GET_TYPE_MASK) == GET_NO_ARG ||
          (optp->var_type & GET_TYPE_MASK) == GET_BOOL)
      {
        if (optp->def_value != 0)
        {
          printf(_("%*s(Defaults to on; use --skip-%s to disable.)\n"), name_space, "", optp->name);
        }
      }
    }
  }


} /* namespace drizzled */