~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to mysys/default.c

  • Committer: Monty Taylor
  • Date: 2008-08-01 22:33:44 UTC
  • mto: (236.1.42 codestyle)
  • mto: This revision was merged to the branch mainline in revision 261.
  • Revision ID: monty@inaugust.com-20080801223344-vzhlflfmtijp1imv
First pass at gettexizing the error messages.

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
 
12
12
   You should have received a copy of the GNU General Public License
13
13
   along with this program; if not, write to the Free Software
14
 
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
 
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
15
15
 
16
16
/****************************************************************************
17
17
 Add all options from files named "group".cnf from the default_directories
33
33
 --print-defaults         ; Print the modified command line and exit
34
34
****************************************************************************/
35
35
 
36
 
#include <config.h>
37
 
 
38
 
#include <drizzled/internal/my_sys.h>
39
 
#include <drizzled/internal/m_string.h>
40
 
#include <drizzled/charset_info.h>
41
 
#include <drizzled/typelib.h>
42
 
#include <drizzled/configmake.h>
43
 
#include <drizzled/gettext.h>
44
 
 
45
 
#include <drizzled/cached_directory.h>
46
 
 
47
 
#ifdef HAVE_SYS_STAT_H
48
 
# include <sys/stat.h>
49
 
#endif
50
 
 
51
 
#include <cstdio>
52
 
#include <algorithm>
53
 
 
54
 
using namespace std;
55
 
 
56
 
namespace drizzled
57
 
{
58
 
namespace internal
59
 
{
 
36
#include "mysys_priv.h"
 
37
#include <mystrings/m_string.h>
 
38
#include <mystrings/m_ctype.h>
 
39
#include <my_dir.h>
60
40
 
61
41
const char *my_defaults_file=0;
62
42
const char *my_defaults_group_suffix=0;
68
48
const char *default_directories[MAX_DEFAULT_DIRS + 1];
69
49
 
70
50
static const char *f_extensions[]= { ".cnf", 0 };
 
51
#define NEWLINE "\n"
71
52
 
72
 
int handle_default_option(void *in_ctx, const char *group_name,
73
 
                          const char *option);
 
53
static int handle_default_option(void *in_ctx, const char *group_name,
 
54
                                 const char *option);
74
55
 
75
56
/*
76
57
   This structure defines the context that we pass to callback
81
62
 
82
63
struct handle_option_ctx
83
64
{
84
 
   memory::Root *alloc;
 
65
   MEM_ROOT *alloc;
85
66
   DYNAMIC_ARRAY *args;
86
67
   TYPELIB *group;
87
68
};
145
126
*/
146
127
 
147
128
int my_search_option_files(const char *conf_file, int *argc, char ***argv,
148
 
                           uint32_t *args_used, Process_option_func func,
 
129
                           uint *args_used, Process_option_func func,
149
130
                           void *func_ctx)
150
131
{
151
132
  const char **dirs, *forced_default_file, *forced_extra_defaults;
158
139
                                    (char **) &my_defaults_group_suffix);
159
140
 
160
141
  if (! my_defaults_group_suffix)
161
 
    my_defaults_group_suffix= getenv("DRIZZLE_GROUP_SUFFIX");
 
142
    my_defaults_group_suffix= getenv(STRINGIFY_ARG(DEFAULT_GROUP_SUFFIX_ENV));
162
143
 
163
144
  if (forced_extra_defaults)
164
145
    my_defaults_extra_file= (char *) forced_extra_defaults;
165
 
 
 
146
  
166
147
  if (forced_default_file)
167
148
    my_defaults_file= forced_default_file;
168
149
 
171
152
    load_defaults() as otherwise we can't know the type of 'func_ctx'
172
153
  */
173
154
 
174
 
  if (my_defaults_group_suffix && (func == handle_default_option))
 
155
  if (my_defaults_group_suffix && func == handle_default_option)
175
156
  {
176
157
    /* Handle --defaults-group-suffix= */
177
 
    uint32_t i;
 
158
    uint i;
178
159
    const char **extra_groups;
179
 
    const size_t instance_len= strlen(my_defaults_group_suffix);
 
160
    const uint instance_len= strlen(my_defaults_group_suffix); 
180
161
    struct handle_option_ctx *ctx= (struct handle_option_ctx*) func_ctx;
181
162
    char *ptr;
182
163
    TYPELIB *group= ctx->group;
183
 
 
184
 
    if (!(extra_groups=
185
 
          (const char**)ctx->alloc->alloc_root(
 
164
    
 
165
    if (!(extra_groups= 
 
166
          (const char**)alloc_root(ctx->alloc,
186
167
                                   (2*group->count+1)*sizeof(char*))))
187
168
      goto err;
188
 
 
 
169
    
189
170
    for (i= 0; i < group->count; i++)
190
171
    {
191
 
      size_t len;
 
172
      uint len;
192
173
      extra_groups[i]= group->type_names[i]; /** copy group */
193
 
 
 
174
      
194
175
      len= strlen(extra_groups[i]);
195
 
      if (!(ptr= (char *)ctx->alloc->alloc_root( len+instance_len+1)))
 
176
      if (!(ptr= alloc_root(ctx->alloc, len+instance_len+1)))
196
177
        goto err;
197
 
 
 
178
      
198
179
      extra_groups[i+group->count]= ptr;
199
 
 
 
180
      
200
181
      /** Construct new group */
201
182
      memcpy(ptr, extra_groups[i], len);
202
183
      memcpy(ptr+len, my_defaults_group_suffix, instance_len+1);
203
184
    }
204
 
 
 
185
    
205
186
    group->count*= 2;
206
187
    group->type_names= extra_groups;
207
188
    group->type_names[group->count]= 0;
208
189
  }
209
 
 
 
190
  
210
191
  if (forced_default_file)
211
192
  {
212
193
    if ((error= search_default_file_with_ext(func, func_ctx, "", "",
221
202
  }
222
203
  else if (dirname_length(conf_file))
223
204
  {
224
 
    if ((error= search_default_file(func, func_ctx, NULL, conf_file)) < 0)
 
205
    if ((error= search_default_file(func, func_ctx, NullS, conf_file)) < 0)
225
206
      goto err;
226
207
  }
227
208
  else
253
234
err:
254
235
  fprintf(stderr,"Fatal error in defaults handling. Program aborted\n");
255
236
  exit(1);
 
237
  return 0;                                     /* Keep compiler happy */
256
238
}
257
239
 
258
240
 
279
261
    1 - error occured
280
262
*/
281
263
 
282
 
int handle_default_option(void *in_ctx, const char *group_name,
283
 
                          const char *option)
 
264
static int handle_default_option(void *in_ctx, const char *group_name,
 
265
                                 const char *option)
284
266
{
285
267
  char *tmp;
286
268
  struct handle_option_ctx *ctx= (struct handle_option_ctx *) in_ctx;
288
270
  if (!option)
289
271
    return 0;
290
272
 
291
 
  if (ctx->group->find_type(const_cast<char*>(group_name), 3))
 
273
  if (find_type((char *)group_name, ctx->group, 3))
292
274
  {
293
 
    if (!(tmp= (char *)ctx->alloc->alloc_root(strlen(option) + 1)))
294
 
      return 1;
295
 
    if (insert_dynamic(ctx->args, (unsigned char*) &tmp))
296
 
      return 1;
297
 
    strcpy(tmp, option);
 
275
    if (!(tmp= alloc_root(ctx->alloc, strlen(option) + 1)))
 
276
      return 1;
 
277
    if (insert_dynamic(ctx->args, (uchar*) &tmp))
 
278
      return 1;
 
279
    strmov(tmp, option);
298
280
  }
299
281
 
300
282
  return 0;
325
307
  int org_argc= argc, prev_argc= 0;
326
308
  *defaults= *extra_defaults= *group_suffix= 0;
327
309
 
328
 
  const std::string DEFAULTS_FILE("--defaults-file=");
329
 
  const std::string DEFAULTS_EXTRA_FILE("--defaults-extra-file=");
330
 
  const std::string DEFAULTS_GROUP_SUFFIX("--defaults-group-suffix=");
331
 
 
332
310
  while (argc >= 2 && argc != prev_argc)
333
311
  {
334
312
    /* Skip program name or previously handled argument */
335
313
    argv++;
336
314
    prev_argc= argc;                            /* To check if we found */
337
 
    if (!*defaults && (strncmp(*argv,
338
 
                               DEFAULTS_FILE.c_str(),
339
 
                               DEFAULTS_FILE.size()) == 0))
 
315
    if (!*defaults && is_prefix(*argv,"--defaults-file="))
340
316
    {
341
 
      *defaults= *argv + DEFAULTS_FILE.size();
 
317
      *defaults= *argv + sizeof("--defaults-file=")-1;
342
318
       argc--;
343
319
       continue;
344
320
    }
345
 
    if (!*extra_defaults && (strncmp(*argv, 
346
 
                                     DEFAULTS_EXTRA_FILE.c_str(),
347
 
                                     DEFAULTS_EXTRA_FILE.size()) == 0))
 
321
    if (!*extra_defaults && is_prefix(*argv,"--defaults-extra-file="))
348
322
    {
349
 
      *extra_defaults= *argv + DEFAULTS_EXTRA_FILE.size();
 
323
      *extra_defaults= *argv + sizeof("--defaults-extra-file=")-1;
350
324
      argc--;
351
325
      continue;
352
326
    }
353
 
    if (!*group_suffix && (strncmp(*argv, 
354
 
                                   DEFAULTS_GROUP_SUFFIX.c_str(),
355
 
                                   DEFAULTS_GROUP_SUFFIX.size()) == 0))
356
 
 
 
327
    if (!*group_suffix && is_prefix(*argv, "--defaults-group-suffix="))
357
328
    {
358
 
      *group_suffix= *argv + DEFAULTS_GROUP_SUFFIX.size();
 
329
      *group_suffix= *argv + sizeof("--defaults-group-suffix=")-1;
359
330
      argc--;
360
331
      continue;
361
332
    }
385
356
   NOTES
386
357
    In case of fatal error, the function will print a warning and do
387
358
    exit(1)
388
 
 
 
359
 
389
360
    To free used memory one should call free_defaults() with the argument
390
361
    that was put in *argv
391
362
 
400
371
{
401
372
  DYNAMIC_ARRAY args;
402
373
  TYPELIB group;
403
 
  uint32_t args_used= 0;
 
374
  bool found_print_defaults= 0;
 
375
  uint args_used= 0;
404
376
  int error= 0;
405
 
  memory::Root alloc(512);
 
377
  MEM_ROOT alloc;
406
378
  char *ptr,**res;
407
379
  struct handle_option_ctx ctx;
408
380
 
409
381
  init_default_directories();
 
382
  init_alloc_root(&alloc,512,0);
410
383
  /*
411
384
    Check if the user doesn't want any default option processing
412
385
    --no-defaults is always the first option
414
387
  if (*argc >= 2 && !strcmp(argv[0][1],"--no-defaults"))
415
388
  {
416
389
    /* remove the --no-defaults argument and return only the other arguments */
417
 
    uint32_t i;
418
 
    if (!(ptr=(char*) alloc.alloc_root(sizeof(alloc)+ (*argc + 1)*sizeof(char*))))
 
390
    uint i;
 
391
    if (!(ptr=(char*) alloc_root(&alloc,sizeof(alloc)+
 
392
                                 (*argc + 1)*sizeof(char*))))
419
393
      goto err;
420
394
    res= (char**) (ptr+sizeof(alloc));
421
 
    memset(res,0,(*argc + 1));
422
395
    res[0]= **argv;                             /* Copy program name */
423
 
    for (i=2 ; i < (uint32_t) *argc ; i++)
 
396
    for (i=2 ; i < (uint) *argc ; i++)
424
397
      res[i-1]=argv[0][i];
425
398
    res[i-1]=0;                                 /* End pointer */
426
399
    (*argc)--;
427
400
    *argv=res;
428
 
    *(memory::Root*) ptr= alloc;                        /* Save alloc root for free */
 
401
    *(MEM_ROOT*) ptr= alloc;                    /* Save alloc root for free */
429
402
    return(0);
430
403
  }
431
404
 
449
422
    Here error contains <> 0 only if we have a fully specified conf_file
450
423
    or a forced default file
451
424
  */
452
 
  if (!(ptr=(char*) alloc.alloc_root(sizeof(alloc)+ (args.elements + *argc +1) *sizeof(char*))))
 
425
  if (!(ptr=(char*) alloc_root(&alloc,sizeof(alloc)+
 
426
                               (args.elements + *argc +1) *sizeof(char*))))
453
427
    goto err;
454
428
  res= (char**) (ptr+sizeof(alloc));
455
429
 
456
430
  /* copy name + found arguments + command line arguments to new array */
457
431
  res[0]= argv[0][0];  /* Name MUST be set, even by embedded library */
458
 
  memcpy(res+1, args.buffer, args.elements*sizeof(char*));
 
432
  memcpy((uchar*) (res+1), args.buffer, args.elements*sizeof(char*));
459
433
  /* Skip --defaults-xxx options */
460
434
  (*argc)-= args_used;
461
435
  (*argv)+= args_used;
464
438
    Check if we wan't to see the new argument list
465
439
    This options must always be the last of the default options
466
440
  */
 
441
  if (*argc >= 2 && !strcmp(argv[0][1],"--print-defaults"))
 
442
  {
 
443
    found_print_defaults=1;
 
444
    --*argc; ++*argv;                           /* skip argument */
 
445
  }
 
446
 
467
447
  if (*argc)
468
 
    memcpy(res+1+args.elements, *argv + 1, (*argc-1)*sizeof(char*));
 
448
    memcpy((uchar*) (res+1+args.elements), (char*) ((*argv)+1),
 
449
           (*argc-1)*sizeof(char*));
469
450
  res[args.elements+ *argc]=0;                  /* last null */
470
451
 
471
 
  (*argc)+=int(args.elements);
472
 
  *argv= static_cast<char**>(res);
473
 
  *(memory::Root*) ptr= alloc;                  /* Save alloc root for free */
 
452
  (*argc)+=args.elements;
 
453
  *argv= (char**) res;
 
454
  *(MEM_ROOT*) ptr= alloc;                      /* Save alloc root for free */
474
455
  delete_dynamic(&args);
475
 
 
 
456
  if (found_print_defaults)
 
457
  {
 
458
    int i;
 
459
    printf("%s would have been started with the following arguments:\n",
 
460
           **argv);
 
461
    for (i=1 ; i < *argc ; i++)
 
462
      printf("%s ", (*argv)[i]);
 
463
    puts("");
 
464
    exit(0);
 
465
  }
476
466
  return(error);
477
467
 
478
468
 err:
479
469
  fprintf(stderr,"Fatal error in defaults handling. Program aborted\n");
480
470
  exit(1);
 
471
  return 0;                                     /* Keep compiler happy */
481
472
}
482
473
 
483
474
 
484
475
void free_defaults(char **argv)
485
476
{
486
 
  memory::Root ptr;
487
 
  memcpy(&ptr, (char*) argv - sizeof(ptr), sizeof(ptr));
488
 
  ptr.free_root(MYF(0));
 
477
  MEM_ROOT ptr;
 
478
  memcpy((char*) &ptr,(char *) argv - sizeof(ptr), sizeof(ptr));
 
479
  free_root(&ptr,MYF(0));
489
480
}
490
481
 
491
482
 
527
518
*/
528
519
 
529
520
static char *get_argument(const char *keyword, size_t kwlen,
530
 
                          char *ptr, char *name, uint32_t line)
 
521
                          char *ptr, char *name, uint line)
531
522
{
532
523
  char *end;
533
524
 
534
525
  /* Skip over "include / includedir keyword" and following whitespace */
535
526
 
536
527
  for (ptr+= kwlen - 1;
537
 
       my_isspace(&my_charset_utf8_general_ci, ptr[0]);
 
528
       my_isspace(&my_charset_latin1, ptr[0]);
538
529
       ptr++)
539
530
  {}
540
531
 
544
535
    Note that my_isspace() is true for \r and \n
545
536
  */
546
537
  for (end= ptr + strlen(ptr) - 1;
547
 
       my_isspace(&my_charset_utf8_general_ci, *(end - 1));
 
538
       my_isspace(&my_charset_latin1, *(end - 1));
548
539
       end--)
549
540
  {}
550
541
  end[0]= 0;                                    /* Cut off end space */
568
559
    search_default_file_with_ext()
569
560
    opt_handler                 Option handler function. It is used to process
570
561
                                every separate option.
571
 
    handler_ctx                 Pointer to the structure to store actual
 
562
    handler_ctx                 Pointer to the structure to store actual 
572
563
                                parameters of the function.
573
564
    dir                         directory to read
574
565
    ext                         Extension for configuration file
596
587
  static const char include_keyword[]= "include";
597
588
  const int max_recursion_level= 10;
598
589
  FILE *fp;
599
 
  uint32_t line=0;
 
590
  uint line=0;
600
591
  bool found_group=0;
 
592
  uint i;
 
593
  MY_DIR *search_dir;
 
594
  FILEINFO *search_file;
601
595
 
602
596
  if ((dir ? strlen(dir) : 0 )+strlen(config_file) >= FN_REFLEN-3)
603
597
    return 0;                                   /* Ignore wrong paths */
604
598
  if (dir)
605
599
  {
606
 
    end=convert_dirname(name, dir, NULL);
 
600
    end=convert_dirname(name, dir, NullS);
607
601
    if (dir[0] == FN_HOMELIB)           /* Add . to filenames in home */
608
602
      *end++='.';
609
 
    sprintf(end,"%s%s",config_file,ext);
 
603
    strxmov(end,config_file,ext,NullS);
610
604
  }
611
605
  else
612
606
  {
613
 
    strcpy(name,config_file);
 
607
    strmov(name,config_file);
614
608
  }
615
609
  fn_format(name,name,"","",4);
616
610
  {
620
614
    /*
621
615
      Ignore world-writable regular files.
622
616
      This is mainly done to protect us to not read a file created by
623
 
      the mysqld server, but the check is still valid in most context.
 
617
      the mysqld server, but the check is still valid in most context. 
624
618
    */
625
619
    if ((stat_info.st_mode & S_IWOTH) &&
626
620
        (stat_info.st_mode & S_IFMT) == S_IFREG)
630
624
      return 0;
631
625
    }
632
626
  }
633
 
  if (!(fp= fopen(name, "r")))
 
627
  if (!(fp= my_fopen(name, O_RDONLY, MYF(0))))
634
628
    return 1;                                   /* Ignore wrong files */
635
629
 
636
 
  memset(buff,0,sizeof(buff));
637
630
  while (fgets(buff, sizeof(buff) - 1, fp))
638
631
  {
639
632
    line++;
640
633
    /* Ignore comment and empty lines */
641
 
    for (ptr= buff; my_isspace(&my_charset_utf8_general_ci, *ptr); ptr++)
 
634
    for (ptr= buff; my_isspace(&my_charset_latin1, *ptr); ptr++)
642
635
    {}
643
636
 
644
637
    if (*ptr == '#' || *ptr == ';' || !*ptr)
649
642
    {
650
643
      if (recursion_level >= max_recursion_level)
651
644
      {
652
 
        for (end= ptr + strlen(ptr) - 1;
653
 
             my_isspace(&my_charset_utf8_general_ci, *(end - 1));
 
645
        for (end= ptr + strlen(ptr) - 1; 
 
646
             my_isspace(&my_charset_latin1, *(end - 1));
654
647
             end--)
655
648
        {}
656
649
        end[0]= 0;
662
655
      }
663
656
 
664
657
      /* skip over `!' and following whitespace */
665
 
      for (++ptr; my_isspace(&my_charset_utf8_general_ci, ptr[0]); ptr++)
 
658
      for (++ptr; my_isspace(&my_charset_latin1, ptr[0]); ptr++)
666
659
      {}
667
660
 
668
661
      if ((!strncmp(ptr, includedir_keyword,
669
662
                    sizeof(includedir_keyword) - 1)) &&
670
 
          my_isspace(&my_charset_utf8_general_ci, ptr[sizeof(includedir_keyword) - 1]))
 
663
          my_isspace(&my_charset_latin1, ptr[sizeof(includedir_keyword) - 1]))
671
664
      {
672
665
        if (!(ptr= get_argument(includedir_keyword,
673
666
                                sizeof(includedir_keyword),
674
667
                                ptr, name, line)))
675
668
          goto err;
676
669
 
677
 
        CachedDirectory dir_cache(ptr);
678
 
 
679
 
        if (dir_cache.fail())
680
 
        {
681
 
          /**
682
 
           * @todo
683
 
           * Since clients still use this code, we use fprintf here.
684
 
           * This fprintf needs to be turned into errmsg_printf
685
 
           * as soon as the client programs no longer use mysys
686
 
           * and can use the pluggable error message system.
687
 
           */
688
 
          fprintf(stderr, _("error: could not open directory: %s\n"), ptr);
 
670
        if (!(search_dir= my_dir(ptr, MYF(MY_WME))))
689
671
          goto err;
690
 
        }
691
 
 
692
 
        CachedDirectory::Entries files= dir_cache.getEntries();
693
 
        CachedDirectory::Entries::iterator file_iter= files.begin();
694
 
 
695
 
        while (file_iter != files.end())
 
672
 
 
673
        for (i= 0; i < (uint) search_dir->number_off_files; i++)
696
674
        {
697
 
          CachedDirectory::Entry *entry= *file_iter;
698
 
          ext= fn_ext(entry->filename.c_str());
 
675
          search_file= search_dir->dir_entry + i;
 
676
          ext= fn_ext(search_file->name);
699
677
 
700
678
          /* check extension */
701
679
          for (tmp_ext= (char**) f_extensions; *tmp_ext; tmp_ext++)
702
680
          {
703
681
            if (!strcmp(ext, *tmp_ext))
704
 
            {
705
 
              fn_format(tmp, entry->filename.c_str(), ptr, "",
706
 
                        MY_UNPACK_FILENAME | MY_SAFE_PATH);
707
 
 
708
 
              search_default_file_with_ext(opt_handler, handler_ctx, "", "",
709
 
                                           tmp, recursion_level + 1);
710
 
            }
711
 
          }
712
 
 
713
 
          ++file_iter;
 
682
              break;
 
683
          }
 
684
 
 
685
          if (*tmp_ext)
 
686
          {
 
687
            fn_format(tmp, search_file->name, ptr, "",
 
688
                      MY_UNPACK_FILENAME | MY_SAFE_PATH);
 
689
 
 
690
            search_default_file_with_ext(opt_handler, handler_ctx, "", "", tmp,
 
691
                                         recursion_level + 1);
 
692
          }
714
693
        }
 
694
 
 
695
        my_dirend(search_dir);
715
696
      }
716
697
      else if ((!strncmp(ptr, include_keyword, sizeof(include_keyword) - 1)) &&
717
 
               my_isspace(&my_charset_utf8_general_ci, ptr[sizeof(include_keyword)-1]))
 
698
               my_isspace(&my_charset_latin1, ptr[sizeof(include_keyword)-1]))
718
699
      {
719
700
        if (!(ptr= get_argument(include_keyword,
720
701
                                sizeof(include_keyword), ptr,
739
720
        goto err;
740
721
      }
741
722
      /* Remove end space */
742
 
      for ( ; my_isspace(&my_charset_utf8_general_ci,end[-1]) ; end--) ;
 
723
      for ( ; my_isspace(&my_charset_latin1,end[-1]) ; end--) ;
743
724
      end[0]=0;
744
725
 
745
 
      strncpy(curr_gr, ptr, min((size_t) (end-ptr)+1, sizeof(curr_gr)-1));
746
 
      curr_gr[min((size_t)(end-ptr)+1, sizeof(curr_gr)-1)] = '\0';
 
726
      strmake(curr_gr, ptr, min((size_t) (end-ptr)+1, sizeof(curr_gr)-1));
747
727
 
748
728
      /* signal that a new group is found */
749
729
      opt_handler(handler_ctx, curr_gr, NULL);
757
737
              name,line);
758
738
      goto err;
759
739
    }
760
 
 
761
 
 
 
740
    
 
741
   
762
742
    end= remove_end_comment(ptr);
763
743
    if ((value= strchr(ptr, '=')))
764
744
      end= value;                               /* Option without argument */
765
 
    for ( ; my_isspace(&my_charset_utf8_general_ci,end[-1]) || end[-1]== '\n'; end--) ;
 
745
    for ( ; my_isspace(&my_charset_latin1,end[-1]) ; end--) ;
766
746
    if (!value)
767
747
    {
768
 
      strncpy(strcpy(option,"--")+2,ptr,strlen(ptr)+1);
 
748
      strmake(strmov(option,"--"),ptr, (size_t) (end-ptr));
769
749
      if (opt_handler(handler_ctx, curr_gr, option))
770
750
        goto err;
771
751
    }
773
753
    {
774
754
      /* Remove pre- and end space */
775
755
      char *value_end;
776
 
      for (value++ ; my_isspace(&my_charset_utf8_general_ci,*value); value++) ;
777
 
      value_end= strchr(value, '\0');
 
756
      for (value++ ; my_isspace(&my_charset_latin1,*value); value++) ;
 
757
      value_end=strend(value);
778
758
      /*
779
 
       We don't have to test for value_end >= value as we know there is
780
 
       an '=' before
 
759
        We don't have to test for value_end >= value as we know there is
 
760
        an '=' before
781
761
      */
782
 
      for ( ; my_isspace(&my_charset_utf8_general_ci,value_end[-1]) ; value_end--) ;
 
762
      for ( ; my_isspace(&my_charset_latin1,value_end[-1]) ; value_end--) ;
783
763
      if (value_end < value)                    /* Empty string */
784
 
        value_end=value;
 
764
        value_end=value;
785
765
 
786
766
      /* remove quotes around argument */
787
767
      if ((*value == '\"' || *value == '\'') && /* First char is quote */
788
768
          (value + 1 < value_end ) && /* String is longer than 1 */
789
769
          *value == value_end[-1] ) /* First char is equal to last char */
790
770
      {
791
 
        value++;
792
 
        value_end--;
 
771
        value++;
 
772
        value_end--;
793
773
      }
794
 
      
795
 
      memset(option,0,2+(size_t)(end-ptr)+1);
796
 
      ptr= strncpy(strcpy(option,"--")+2,ptr,(size_t) (end-ptr));
797
 
      ptr[end-ptr]= '\0';
798
 
      ptr+= strlen(ptr);
 
774
      ptr=strnmov(strmov(option,"--"),ptr,(size_t) (end-ptr));
799
775
      *ptr++= '=';
800
776
 
801
777
      for ( ; value != value_end; value++)
841
817
        goto err;
842
818
    }
843
819
  }
844
 
  fclose(fp);
 
820
  my_fclose(fp,MYF(0));
845
821
  return(0);
846
822
 
847
823
 err:
848
 
  fclose(fp);
849
 
 
 
824
  my_fclose(fp,MYF(0));
850
825
  return -1;                                    /* Fatal error */
851
826
}
852
827
 
903
878
          pos= my_defaults_extra_file;
904
879
        else
905
880
          continue;
906
 
        end= convert_dirname(name, pos, NULL);
 
881
        end= convert_dirname(name, pos, NullS);
907
882
        if (name[0] == FN_HOMELIB)      /* Add . to filenames in home */
908
883
          *end++='.';
909
 
  sprintf(end,"%s%s ",conf_file, *ext);
 
884
        strxmov(end, conf_file, *ext, " ", NullS);
910
885
        fputs(name,stdout);
911
886
      }
912
887
    }
937
912
    }
938
913
  }
939
914
  puts("\nThe following options may be given as the first argument:\n\
940
 
  --no-defaults         Don't read default options from any options file\n\
941
 
  --defaults-file=#     Only read default options from the given file #\n\
942
 
  --defaults-extra-file=# Read this file after the global files are read");
 
915
--print-defaults        Print the program argument list and exit\n\
 
916
--no-defaults           Don't read default options from any options file\n\
 
917
--defaults-file=#       Only read default options from the given file #\n\
 
918
--defaults-extra-file=# Read this file after the global files are read");
943
919
}
944
920
 
945
921
/*
946
922
  This extra complexity is to avoid declaring 'rc' if it won't be
947
923
  used.
948
924
*/
949
 
static void add_directory(const char* dir)
950
 
{
951
 
  array_append_string_unique(dir, default_directories, array_elements(default_directories));
952
 
}
953
 
 
954
 
static void add_common_directories()
955
 
{
956
 
  const char *env= getenv("DRIZZLE_HOME"); 
957
 
  if (env) 
958
 
    add_directory(env); 
959
 
  // Placeholder for --defaults-extra-file=<path>
960
 
  add_directory(""); 
961
 
}
 
925
#define ADD_DIRECTORY(DIR)  (void) array_append_string_unique((DIR), default_directories, \
 
926
                             array_elements(default_directories))
 
927
 
 
928
#define ADD_COMMON_DIRECTORIES() \
 
929
  do { \
 
930
    char *env; \
 
931
    if ((env= getenv(STRINGIFY_ARG(DEFAULT_HOME_ENV)))) \
 
932
      ADD_DIRECTORY(env); \
 
933
    /* Placeholder for --defaults-extra-file=<path> */ \
 
934
    ADD_DIRECTORY(""); \
 
935
  } while (0)
 
936
 
962
937
 
963
938
/**
964
939
  Initialize default directories for Unix
965
940
 
966
941
  @details
967
942
    1. /etc/
968
 
    2. /etc/drizzle/
 
943
    2. /etc/mysql/
969
944
    3. --sysconfdir=<path> (compile-time option)
970
 
    4. getenv("DRIZZLE_HOME")
 
945
    4. getenv(DEFAULT_HOME_ENV)
971
946
    5. --defaults-extra-file=<path> (run-time option)
972
947
    6. "~/"
973
948
*/
974
949
 
975
950
static void init_default_directories(void)
976
951
{
977
 
  memset(default_directories, 0, sizeof(default_directories));
978
 
  add_directory("/etc/");
979
 
  add_directory("/etc/drizzle/");
980
 
  add_directory(SYSCONFDIR);
981
 
  add_common_directories();
982
 
  add_directory("~/");
 
952
  memset((char *) default_directories, 0, sizeof(default_directories));
 
953
  ADD_DIRECTORY("/etc/");
 
954
  ADD_DIRECTORY("/etc/mysql/");
 
955
#if defined(DEFAULT_SYSCONFDIR)
 
956
    ADD_DIRECTORY(DEFAULT_SYSCONFDIR);
 
957
#endif
 
958
  ADD_COMMON_DIRECTORIES();
 
959
  ADD_DIRECTORY("~/");
983
960
}
984
 
 
985
 
} /* namespace internal */
986
 
} /* namespace drizzled */