~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to mysys/default.cc

Moved the last of the libdrizzleclient calls into Protocol.

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