~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to mysys/default.c

  • Committer: Brian Aker
  • Date: 2008-10-06 06:47:29 UTC
  • Revision ID: brian@tangent.org-20081006064729-2i9mhjkzyvow9xsm
RemoveĀ uint.

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 <mysys/my_dir.h>
40
 
#include <drizzled/configmake.h>
41
 
 
42
 
#include <stdio.h>
 
39
#include <my_dir.h>
43
40
 
44
41
const char *my_defaults_file=0;
45
42
const char *my_defaults_group_suffix=0;
51
48
const char *default_directories[MAX_DEFAULT_DIRS + 1];
52
49
 
53
50
static const char *f_extensions[]= { ".cnf", 0 };
 
51
#define NEWLINE "\n"
54
52
 
55
 
int handle_default_option(void *in_ctx, const char *group_name,
56
 
                          const char *option);
 
53
static int handle_default_option(void *in_ctx, const char *group_name,
 
54
                                 const char *option);
57
55
 
58
56
/*
59
57
   This structure defines the context that we pass to callback
141
139
                                    (char **) &my_defaults_group_suffix);
142
140
 
143
141
  if (! my_defaults_group_suffix)
144
 
    my_defaults_group_suffix= getenv("DRIZZLE_GROUP_SUFFIX");
 
142
    my_defaults_group_suffix= getenv(STRINGIFY_ARG(DEFAULT_GROUP_SUFFIX_ENV));
145
143
 
146
144
  if (forced_extra_defaults)
147
145
    my_defaults_extra_file= (char *) forced_extra_defaults;
148
 
 
 
146
  
149
147
  if (forced_default_file)
150
148
    my_defaults_file= forced_default_file;
151
149
 
154
152
    load_defaults() as otherwise we can't know the type of 'func_ctx'
155
153
  */
156
154
 
157
 
  if (my_defaults_group_suffix && (func == handle_default_option))
 
155
  if (my_defaults_group_suffix && func == handle_default_option)
158
156
  {
159
157
    /* Handle --defaults-group-suffix= */
160
158
    uint32_t i;
161
159
    const char **extra_groups;
162
 
    const uint32_t instance_len= strlen(my_defaults_group_suffix);
 
160
    const uint32_t instance_len= strlen(my_defaults_group_suffix); 
163
161
    struct handle_option_ctx *ctx= (struct handle_option_ctx*) func_ctx;
164
162
    char *ptr;
165
163
    TYPELIB *group= ctx->group;
166
 
 
167
 
    if (!(extra_groups=
 
164
    
 
165
    if (!(extra_groups= 
168
166
          (const char**)alloc_root(ctx->alloc,
169
167
                                   (2*group->count+1)*sizeof(char*))))
170
168
      goto err;
171
 
 
 
169
    
172
170
    for (i= 0; i < group->count; i++)
173
171
    {
174
172
      uint32_t len;
175
173
      extra_groups[i]= group->type_names[i]; /** copy group */
176
 
 
 
174
      
177
175
      len= strlen(extra_groups[i]);
178
 
      if (!(ptr= (char *)alloc_root(ctx->alloc, len+instance_len+1)))
 
176
      if (!(ptr= alloc_root(ctx->alloc, len+instance_len+1)))
179
177
        goto err;
180
 
 
 
178
      
181
179
      extra_groups[i+group->count]= ptr;
182
 
 
 
180
      
183
181
      /** Construct new group */
184
182
      memcpy(ptr, extra_groups[i], len);
185
183
      memcpy(ptr+len, my_defaults_group_suffix, instance_len+1);
186
184
    }
187
 
 
 
185
    
188
186
    group->count*= 2;
189
187
    group->type_names= extra_groups;
190
188
    group->type_names[group->count]= 0;
191
189
  }
192
 
 
 
190
  
193
191
  if (forced_default_file)
194
192
  {
195
193
    if ((error= search_default_file_with_ext(func, func_ctx, "", "",
236
234
err:
237
235
  fprintf(stderr,"Fatal error in defaults handling. Program aborted\n");
238
236
  exit(1);
 
237
  return 0;                                     /* Keep compiler happy */
239
238
}
240
239
 
241
240
 
262
261
    1 - error occured
263
262
*/
264
263
 
265
 
int handle_default_option(void *in_ctx, const char *group_name,
266
 
                          const char *option)
 
264
static int handle_default_option(void *in_ctx, const char *group_name,
 
265
                                 const char *option)
267
266
{
268
267
  char *tmp;
269
268
  struct handle_option_ctx *ctx= (struct handle_option_ctx *) in_ctx;
273
272
 
274
273
  if (find_type((char *)group_name, ctx->group, 3))
275
274
  {
276
 
    if (!(tmp= (char *)alloc_root(ctx->alloc, strlen(option) + 1)))
 
275
    if (!(tmp= alloc_root(ctx->alloc, strlen(option) + 1)))
277
276
      return 1;
278
277
    if (insert_dynamic(ctx->args, (unsigned char*) &tmp))
279
278
      return 1;
280
 
    strcpy(tmp, option);
 
279
    my_stpcpy(tmp, option);
281
280
  }
282
281
 
283
282
  return 0;
357
356
   NOTES
358
357
    In case of fatal error, the function will print a warning and do
359
358
    exit(1)
360
 
 
 
359
 
361
360
    To free used memory one should call free_defaults() with the argument
362
361
    that was put in *argv
363
362
 
393
392
                                 (*argc + 1)*sizeof(char*))))
394
393
      goto err;
395
394
    res= (char**) (ptr+sizeof(alloc));
396
 
    memset(res,0,(*argc + 1));
397
395
    res[0]= **argv;                             /* Copy program name */
398
 
    for (i=2 ; i < (uint32_t) *argc ; i++)
 
396
    for (i=2 ; i < (uint) *argc ; i++)
399
397
      res[i-1]=argv[0][i];
400
398
    res[i-1]=0;                                 /* End pointer */
401
399
    (*argc)--;
469
467
 err:
470
468
  fprintf(stderr,"Fatal error in defaults handling. Program aborted\n");
471
469
  exit(1);
 
470
  return 0;                                     /* Keep compiler happy */
472
471
}
473
472
 
474
473
 
559
558
    search_default_file_with_ext()
560
559
    opt_handler                 Option handler function. It is used to process
561
560
                                every separate option.
562
 
    handler_ctx                 Pointer to the structure to store actual
 
561
    handler_ctx                 Pointer to the structure to store actual 
563
562
                                parameters of the function.
564
563
    dir                         directory to read
565
564
    ext                         Extension for configuration file
600
599
    end=convert_dirname(name, dir, NULL);
601
600
    if (dir[0] == FN_HOMELIB)           /* Add . to filenames in home */
602
601
      *end++='.';
603
 
    sprintf(end,"%s%s",config_file,ext);
 
602
    strxmov(end,config_file,ext,NULL);
604
603
  }
605
604
  else
606
605
  {
607
 
    strcpy(name,config_file);
 
606
    my_stpcpy(name,config_file);
608
607
  }
609
608
  fn_format(name,name,"","",4);
610
609
  {
614
613
    /*
615
614
      Ignore world-writable regular files.
616
615
      This is mainly done to protect us to not read a file created by
617
 
      the mysqld server, but the check is still valid in most context.
 
616
      the mysqld server, but the check is still valid in most context. 
618
617
    */
619
618
    if ((stat_info.st_mode & S_IWOTH) &&
620
619
        (stat_info.st_mode & S_IFMT) == S_IFREG)
624
623
      return 0;
625
624
    }
626
625
  }
627
 
  if (!(fp= fopen(name, "r")))
 
626
  if (!(fp= my_fopen(name, O_RDONLY, MYF(0))))
628
627
    return 1;                                   /* Ignore wrong files */
629
628
 
630
 
  memset(buff,0,sizeof(buff));
631
629
  while (fgets(buff, sizeof(buff) - 1, fp))
632
630
  {
633
631
    line++;
643
641
    {
644
642
      if (recursion_level >= max_recursion_level)
645
643
      {
646
 
        for (end= ptr + strlen(ptr) - 1;
 
644
        for (end= ptr + strlen(ptr) - 1; 
647
645
             my_isspace(&my_charset_utf8_general_ci, *(end - 1));
648
646
             end--)
649
647
        {}
671
669
        if (!(search_dir= my_dir(ptr, MYF(MY_WME))))
672
670
          goto err;
673
671
 
674
 
        for (i= 0; i < (uint32_t) search_dir->number_off_files; i++)
 
672
        for (i= 0; i < (uint) search_dir->number_off_files; i++)
675
673
        {
676
674
          search_file= search_dir->dir_entry + i;
677
675
          ext= fn_ext(search_file->name);
724
722
      for ( ; my_isspace(&my_charset_utf8_general_ci,end[-1]) ; end--) ;
725
723
      end[0]=0;
726
724
 
727
 
      strncpy(curr_gr, ptr, cmin((size_t) (end-ptr)+1, sizeof(curr_gr)-1));
728
 
      curr_gr[cmin((size_t)(end-ptr)+1, sizeof(curr_gr)-1)] = '\0';
 
725
      strmake(curr_gr, ptr, cmin((size_t) (end-ptr)+1, sizeof(curr_gr)-1));
729
726
 
730
727
      /* signal that a new group is found */
731
728
      opt_handler(handler_ctx, curr_gr, NULL);
739
736
              name,line);
740
737
      goto err;
741
738
    }
742
 
 
743
 
 
 
739
    
 
740
   
744
741
    end= remove_end_comment(ptr);
745
742
    if ((value= strchr(ptr, '=')))
746
743
      end= value;                               /* Option without argument */
747
 
    for ( ; my_isspace(&my_charset_utf8_general_ci,end[-1]) || end[-1]== '\n'; end--) ;
 
744
    for ( ; my_isspace(&my_charset_utf8_general_ci,end[-1]) ; end--) ;
748
745
    if (!value)
749
746
    {
750
 
      strncpy(strcpy(option,"--")+2,ptr,strlen(ptr)+1);
 
747
      strmake(my_stpcpy(option,"--"),ptr, (size_t) (end-ptr));
751
748
      if (opt_handler(handler_ctx, curr_gr, option))
752
749
        goto err;
753
750
    }
758
755
      for (value++ ; my_isspace(&my_charset_utf8_general_ci,*value); value++) ;
759
756
      value_end= strchr(value, '\0');
760
757
      /*
761
 
       We don't have to test for value_end >= value as we know there is
762
 
       an '=' before
 
758
        We don't have to test for value_end >= value as we know there is
 
759
        an '=' before
763
760
      */
764
761
      for ( ; my_isspace(&my_charset_utf8_general_ci,value_end[-1]) ; value_end--) ;
765
762
      if (value_end < value)                    /* Empty string */
766
 
        value_end=value;
 
763
        value_end=value;
767
764
 
768
765
      /* remove quotes around argument */
769
766
      if ((*value == '\"' || *value == '\'') && /* First char is quote */
770
767
          (value + 1 < value_end ) && /* String is longer than 1 */
771
768
          *value == value_end[-1] ) /* First char is equal to last char */
772
769
      {
773
 
        value++;
774
 
        value_end--;
 
770
        value++;
 
771
        value_end--;
775
772
      }
776
 
      
777
 
      memset(option,0,2+(size_t)(end-ptr)+1);
778
 
      ptr= strncpy(strcpy(option,"--")+2,ptr,(size_t) (end-ptr));
779
 
      ptr[end-ptr]= '\0';
780
 
      ptr+= strlen(ptr);
 
773
      ptr=my_stpncpy(my_stpcpy(option,"--"),ptr,(size_t) (end-ptr));
781
774
      *ptr++= '=';
782
775
 
783
776
      for ( ; value != value_end; value++)
823
816
        goto err;
824
817
    }
825
818
  }
826
 
  fclose(fp);
 
819
  my_fclose(fp,MYF(0));
827
820
  return(0);
828
821
 
829
822
 err:
830
 
  fclose(fp);
831
 
 
 
823
  my_fclose(fp,MYF(0));
832
824
  return -1;                                    /* Fatal error */
833
825
}
834
826
 
888
880
        end= convert_dirname(name, pos, NULL);
889
881
        if (name[0] == FN_HOMELIB)      /* Add . to filenames in home */
890
882
          *end++='.';
891
 
  sprintf(end,"%s%s ",conf_file, *ext);
 
883
        strxmov(end, conf_file, *ext, " ", NULL);
892
884
        fputs(name,stdout);
893
885
      }
894
886
    }
934
926
 
935
927
#define ADD_COMMON_DIRECTORIES() \
936
928
  do { \
937
 
    const char *env; \
938
 
    if ((env= getenv("DRIZZLE_HOME"))) \
 
929
    char *env; \
 
930
    if ((env= getenv(STRINGIFY_ARG(DEFAULT_HOME_ENV)))) \
939
931
      ADD_DIRECTORY(env); \
940
932
    /* Placeholder for --defaults-extra-file=<path> */ \
941
933
    ADD_DIRECTORY(""); \
947
939
 
948
940
  @details
949
941
    1. /etc/
950
 
    2. /etc/drizzle/
 
942
    2. /etc/mysql/
951
943
    3. --sysconfdir=<path> (compile-time option)
952
 
    4. getenv("DRIZZLE_HOME")
 
944
    4. getenv(DEFAULT_HOME_ENV)
953
945
    5. --defaults-extra-file=<path> (run-time option)
954
946
    6. "~/"
955
947
*/
958
950
{
959
951
  memset(default_directories, 0, sizeof(default_directories));
960
952
  ADD_DIRECTORY("/etc/");
961
 
  ADD_DIRECTORY("/etc/drizzle/");
962
 
  ADD_DIRECTORY(SYSCONFDIR);
 
953
  ADD_DIRECTORY("/etc/mysql/");
 
954
#if defined(DEFAULT_SYSCONFDIR)
 
955
    ADD_DIRECTORY(DEFAULT_SYSCONFDIR);
 
956
#endif
963
957
  ADD_COMMON_DIRECTORIES();
964
958
  ADD_DIRECTORY("~/");
965
959
}