~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to mysys/default.cc

  • Committer: Brian Aker
  • Date: 2008-12-16 07:07:50 UTC
  • Revision ID: brian@tangent.org-20081216070750-o5ykltxxqvn2awrx
Fixed errors test.

Show diffs side-by-side

added added

removed removed

Lines of Context:
36
36
#include "mysys_priv.h"
37
37
#include <mystrings/m_string.h>
38
38
#include <mystrings/m_ctype.h>
39
 
#include <my_dir.h>
 
39
#include <mysys/my_dir.h>
 
40
 
 
41
#include <stdio.h>
40
42
 
41
43
const char *my_defaults_file=0;
42
44
const char *my_defaults_group_suffix=0;
48
50
const char *default_directories[MAX_DEFAULT_DIRS + 1];
49
51
 
50
52
static const char *f_extensions[]= { ".cnf", 0 };
51
 
#define NEWLINE "\n"
52
53
 
53
 
static int handle_default_option(void *in_ctx, const char *group_name,
54
 
                                 const char *option);
 
54
int handle_default_option(void *in_ctx, const char *group_name,
 
55
                          const char *option);
55
56
 
56
57
/*
57
58
   This structure defines the context that we pass to callback
126
127
*/
127
128
 
128
129
int my_search_option_files(const char *conf_file, int *argc, char ***argv,
129
 
                           uint *args_used, Process_option_func func,
 
130
                           uint32_t *args_used, Process_option_func func,
130
131
                           void *func_ctx)
131
132
{
132
133
  const char **dirs, *forced_default_file, *forced_extra_defaults;
143
144
 
144
145
  if (forced_extra_defaults)
145
146
    my_defaults_extra_file= (char *) forced_extra_defaults;
146
 
  
 
147
 
147
148
  if (forced_default_file)
148
149
    my_defaults_file= forced_default_file;
149
150
 
152
153
    load_defaults() as otherwise we can't know the type of 'func_ctx'
153
154
  */
154
155
 
155
 
  if (my_defaults_group_suffix && func == handle_default_option)
 
156
  if (my_defaults_group_suffix && (func == handle_default_option))
156
157
  {
157
158
    /* Handle --defaults-group-suffix= */
158
 
    uint i;
 
159
    uint32_t i;
159
160
    const char **extra_groups;
160
 
    const uint instance_len= strlen(my_defaults_group_suffix); 
 
161
    const uint32_t instance_len= strlen(my_defaults_group_suffix);
161
162
    struct handle_option_ctx *ctx= (struct handle_option_ctx*) func_ctx;
162
163
    char *ptr;
163
164
    TYPELIB *group= ctx->group;
164
 
    
165
 
    if (!(extra_groups= 
 
165
 
 
166
    if (!(extra_groups=
166
167
          (const char**)alloc_root(ctx->alloc,
167
168
                                   (2*group->count+1)*sizeof(char*))))
168
169
      goto err;
169
 
    
 
170
 
170
171
    for (i= 0; i < group->count; i++)
171
172
    {
172
 
      uint len;
 
173
      uint32_t len;
173
174
      extra_groups[i]= group->type_names[i]; /** copy group */
174
 
      
 
175
 
175
176
      len= strlen(extra_groups[i]);
176
 
      if (!(ptr= alloc_root(ctx->alloc, len+instance_len+1)))
 
177
      if (!(ptr= (char *)alloc_root(ctx->alloc, len+instance_len+1)))
177
178
        goto err;
178
 
      
 
179
 
179
180
      extra_groups[i+group->count]= ptr;
180
 
      
 
181
 
181
182
      /** Construct new group */
182
183
      memcpy(ptr, extra_groups[i], len);
183
184
      memcpy(ptr+len, my_defaults_group_suffix, instance_len+1);
184
185
    }
185
 
    
 
186
 
186
187
    group->count*= 2;
187
188
    group->type_names= extra_groups;
188
189
    group->type_names[group->count]= 0;
189
190
  }
190
 
  
 
191
 
191
192
  if (forced_default_file)
192
193
  {
193
194
    if ((error= search_default_file_with_ext(func, func_ctx, "", "",
202
203
  }
203
204
  else if (dirname_length(conf_file))
204
205
  {
205
 
    if ((error= search_default_file(func, func_ctx, NullS, conf_file)) < 0)
 
206
    if ((error= search_default_file(func, func_ctx, NULL, conf_file)) < 0)
206
207
      goto err;
207
208
  }
208
209
  else
234
235
err:
235
236
  fprintf(stderr,"Fatal error in defaults handling. Program aborted\n");
236
237
  exit(1);
237
 
  return 0;                                     /* Keep compiler happy */
238
238
}
239
239
 
240
240
 
261
261
    1 - error occured
262
262
*/
263
263
 
264
 
static int handle_default_option(void *in_ctx, const char *group_name,
265
 
                                 const char *option)
 
264
int handle_default_option(void *in_ctx, const char *group_name,
 
265
                          const char *option)
266
266
{
267
267
  char *tmp;
268
268
  struct handle_option_ctx *ctx= (struct handle_option_ctx *) in_ctx;
272
272
 
273
273
  if (find_type((char *)group_name, ctx->group, 3))
274
274
  {
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
 
    stpcpy(tmp, option);
 
275
    if (!(tmp= (char *)alloc_root(ctx->alloc, strlen(option) + 1)))
 
276
      return 1;
 
277
    if (insert_dynamic(ctx->args, (unsigned char*) &tmp))
 
278
      return 1;
 
279
    strcpy(tmp, option);
280
280
  }
281
281
 
282
282
  return 0;
356
356
   NOTES
357
357
    In case of fatal error, the function will print a warning and do
358
358
    exit(1)
359
 
 
 
359
 
360
360
    To free used memory one should call free_defaults() with the argument
361
361
    that was put in *argv
362
362
 
372
372
  DYNAMIC_ARRAY args;
373
373
  TYPELIB group;
374
374
  bool found_print_defaults= 0;
375
 
  uint args_used= 0;
 
375
  uint32_t args_used= 0;
376
376
  int error= 0;
377
377
  MEM_ROOT alloc;
378
378
  char *ptr,**res;
387
387
  if (*argc >= 2 && !strcmp(argv[0][1],"--no-defaults"))
388
388
  {
389
389
    /* remove the --no-defaults argument and return only the other arguments */
390
 
    uint i;
 
390
    uint32_t i;
391
391
    if (!(ptr=(char*) alloc_root(&alloc,sizeof(alloc)+
392
392
                                 (*argc + 1)*sizeof(char*))))
393
393
      goto err;
467
467
 err:
468
468
  fprintf(stderr,"Fatal error in defaults handling. Program aborted\n");
469
469
  exit(1);
470
 
  return 0;                                     /* Keep compiler happy */
471
470
}
472
471
 
473
472
 
517
516
*/
518
517
 
519
518
static char *get_argument(const char *keyword, size_t kwlen,
520
 
                          char *ptr, char *name, uint line)
 
519
                          char *ptr, char *name, uint32_t line)
521
520
{
522
521
  char *end;
523
522
 
524
523
  /* Skip over "include / includedir keyword" and following whitespace */
525
524
 
526
525
  for (ptr+= kwlen - 1;
527
 
       my_isspace(&my_charset_latin1, ptr[0]);
 
526
       my_isspace(&my_charset_utf8_general_ci, ptr[0]);
528
527
       ptr++)
529
528
  {}
530
529
 
534
533
    Note that my_isspace() is true for \r and \n
535
534
  */
536
535
  for (end= ptr + strlen(ptr) - 1;
537
 
       my_isspace(&my_charset_latin1, *(end - 1));
 
536
       my_isspace(&my_charset_utf8_general_ci, *(end - 1));
538
537
       end--)
539
538
  {}
540
539
  end[0]= 0;                                    /* Cut off end space */
558
557
    search_default_file_with_ext()
559
558
    opt_handler                 Option handler function. It is used to process
560
559
                                every separate option.
561
 
    handler_ctx                 Pointer to the structure to store actual 
 
560
    handler_ctx                 Pointer to the structure to store actual
562
561
                                parameters of the function.
563
562
    dir                         directory to read
564
563
    ext                         Extension for configuration file
586
585
  static const char include_keyword[]= "include";
587
586
  const int max_recursion_level= 10;
588
587
  FILE *fp;
589
 
  uint line=0;
 
588
  uint32_t line=0;
590
589
  bool found_group=0;
591
 
  uint i;
 
590
  uint32_t i;
592
591
  MY_DIR *search_dir;
593
592
  FILEINFO *search_file;
594
593
 
596
595
    return 0;                                   /* Ignore wrong paths */
597
596
  if (dir)
598
597
  {
599
 
    end=convert_dirname(name, dir, NullS);
 
598
    end=convert_dirname(name, dir, NULL);
600
599
    if (dir[0] == FN_HOMELIB)           /* Add . to filenames in home */
601
600
      *end++='.';
602
 
    strxmov(end,config_file,ext,NullS);
 
601
    sprintf(end,"%s%s",config_file,ext);
603
602
  }
604
603
  else
605
604
  {
606
 
    stpcpy(name,config_file);
 
605
    strcpy(name,config_file);
607
606
  }
608
607
  fn_format(name,name,"","",4);
609
608
  {
613
612
    /*
614
613
      Ignore world-writable regular files.
615
614
      This is mainly done to protect us to not read a file created by
616
 
      the mysqld server, but the check is still valid in most context. 
 
615
      the mysqld server, but the check is still valid in most context.
617
616
    */
618
617
    if ((stat_info.st_mode & S_IWOTH) &&
619
618
        (stat_info.st_mode & S_IFMT) == S_IFREG)
630
629
  {
631
630
    line++;
632
631
    /* Ignore comment and empty lines */
633
 
    for (ptr= buff; my_isspace(&my_charset_latin1, *ptr); ptr++)
 
632
    for (ptr= buff; my_isspace(&my_charset_utf8_general_ci, *ptr); ptr++)
634
633
    {}
635
634
 
636
635
    if (*ptr == '#' || *ptr == ';' || !*ptr)
641
640
    {
642
641
      if (recursion_level >= max_recursion_level)
643
642
      {
644
 
        for (end= ptr + strlen(ptr) - 1; 
645
 
             my_isspace(&my_charset_latin1, *(end - 1));
 
643
        for (end= ptr + strlen(ptr) - 1;
 
644
             my_isspace(&my_charset_utf8_general_ci, *(end - 1));
646
645
             end--)
647
646
        {}
648
647
        end[0]= 0;
654
653
      }
655
654
 
656
655
      /* skip over `!' and following whitespace */
657
 
      for (++ptr; my_isspace(&my_charset_latin1, ptr[0]); ptr++)
 
656
      for (++ptr; my_isspace(&my_charset_utf8_general_ci, ptr[0]); ptr++)
658
657
      {}
659
658
 
660
659
      if ((!strncmp(ptr, includedir_keyword,
661
660
                    sizeof(includedir_keyword) - 1)) &&
662
 
          my_isspace(&my_charset_latin1, ptr[sizeof(includedir_keyword) - 1]))
 
661
          my_isspace(&my_charset_utf8_general_ci, ptr[sizeof(includedir_keyword) - 1]))
663
662
      {
664
663
        if (!(ptr= get_argument(includedir_keyword,
665
664
                                sizeof(includedir_keyword),
694
693
        my_dirend(search_dir);
695
694
      }
696
695
      else if ((!strncmp(ptr, include_keyword, sizeof(include_keyword) - 1)) &&
697
 
               my_isspace(&my_charset_latin1, ptr[sizeof(include_keyword)-1]))
 
696
               my_isspace(&my_charset_utf8_general_ci, ptr[sizeof(include_keyword)-1]))
698
697
      {
699
698
        if (!(ptr= get_argument(include_keyword,
700
699
                                sizeof(include_keyword), ptr,
719
718
        goto err;
720
719
      }
721
720
      /* Remove end space */
722
 
      for ( ; my_isspace(&my_charset_latin1,end[-1]) ; end--) ;
 
721
      for ( ; my_isspace(&my_charset_utf8_general_ci,end[-1]) ; end--) ;
723
722
      end[0]=0;
724
723
 
725
 
      strmake(curr_gr, ptr, min((size_t) (end-ptr)+1, sizeof(curr_gr)-1));
 
724
      strncpy(curr_gr, ptr, cmin((size_t) (end-ptr)+1, sizeof(curr_gr)-1));
 
725
      curr_gr[cmin((size_t)(end-ptr)+1, sizeof(curr_gr)-1)] = '\0';
726
726
 
727
727
      /* signal that a new group is found */
728
728
      opt_handler(handler_ctx, curr_gr, NULL);
736
736
              name,line);
737
737
      goto err;
738
738
    }
739
 
    
740
 
   
 
739
 
 
740
 
741
741
    end= remove_end_comment(ptr);
742
742
    if ((value= strchr(ptr, '=')))
743
743
      end= value;                               /* Option without argument */
744
 
    for ( ; my_isspace(&my_charset_latin1,end[-1]) ; end--) ;
 
744
    for ( ; my_isspace(&my_charset_utf8_general_ci,end[-1]) ; end--) ;
745
745
    if (!value)
746
746
    {
747
 
      strmake(stpcpy(option,"--"),ptr, (size_t) (end-ptr));
 
747
      strncpy(strcpy(option,"--")+2,ptr,strlen(ptr)+1);
748
748
      if (opt_handler(handler_ctx, curr_gr, option))
749
749
        goto err;
750
750
    }
752
752
    {
753
753
      /* Remove pre- and end space */
754
754
      char *value_end;
755
 
      for (value++ ; my_isspace(&my_charset_latin1,*value); value++) ;
756
 
      value_end=strend(value);
 
755
      for (value++ ; my_isspace(&my_charset_utf8_general_ci,*value); value++) ;
 
756
      value_end= strchr(value, '\0');
757
757
      /*
758
 
        We don't have to test for value_end >= value as we know there is
759
 
        an '=' before
 
758
       We don't have to test for value_end >= value as we know there is
 
759
       an '=' before
760
760
      */
761
 
      for ( ; my_isspace(&my_charset_latin1,value_end[-1]) ; value_end--) ;
 
761
      for ( ; my_isspace(&my_charset_utf8_general_ci,value_end[-1]) ; value_end--) ;
762
762
      if (value_end < value)                    /* Empty string */
763
 
        value_end=value;
 
763
        value_end=value;
764
764
 
765
765
      /* remove quotes around argument */
766
766
      if ((*value == '\"' || *value == '\'') && /* First char is quote */
767
767
          (value + 1 < value_end ) && /* String is longer than 1 */
768
768
          *value == value_end[-1] ) /* First char is equal to last char */
769
769
      {
770
 
        value++;
771
 
        value_end--;
 
770
        value++;
 
771
        value_end--;
772
772
      }
773
 
      ptr=stpncpy(stpcpy(option,"--"),ptr,(size_t) (end-ptr));
 
773
      ptr= strncpy(strcpy(option,"--")+2,ptr,(size_t) (end-ptr));
 
774
      ptr+= strlen(ptr);
774
775
      *ptr++= '=';
775
776
 
776
777
      for ( ; value != value_end; value++)
877
878
          pos= my_defaults_extra_file;
878
879
        else
879
880
          continue;
880
 
        end= convert_dirname(name, pos, NullS);
 
881
        end= convert_dirname(name, pos, NULL);
881
882
        if (name[0] == FN_HOMELIB)      /* Add . to filenames in home */
882
883
          *end++='.';
883
 
        strxmov(end, conf_file, *ext, " ", NullS);
 
884
  sprintf(end,"%s%s ",conf_file, *ext);
884
885
        fputs(name,stdout);
885
886
      }
886
887
    }
939
940
 
940
941
  @details
941
942
    1. /etc/
942
 
    2. /etc/mysql/
 
943
    2. /etc/drizzle/
943
944
    3. --sysconfdir=<path> (compile-time option)
944
945
    4. getenv(DEFAULT_HOME_ENV)
945
946
    5. --defaults-extra-file=<path> (run-time option)
950
951
{
951
952
  memset(default_directories, 0, sizeof(default_directories));
952
953
  ADD_DIRECTORY("/etc/");
953
 
  ADD_DIRECTORY("/etc/mysql/");
 
954
  ADD_DIRECTORY("/etc/drizzle/");
954
955
#if defined(DEFAULT_SYSCONFDIR)
955
956
    ADD_DIRECTORY(DEFAULT_SYSCONFDIR);
956
957
#endif