~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to mysys/default.cc

  • Committer: Brian Aker
  • Date: 2009-02-21 00:18:15 UTC
  • Revision ID: brian@tangent.org-20090221001815-x20e8h71e984lvs1
Completion (?) of uint conversion.

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"
 
36
#include "mysys_priv.h"
 
37
#include <mystrings/m_string.h>
 
38
#include <mystrings/m_ctype.h>
 
39
#include <mysys/my_dir.h>
42
40
#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
 
{
 
41
 
 
42
#include <stdio.h>
60
43
 
61
44
const char *my_defaults_file=0;
62
45
const char *my_defaults_group_suffix=0;
81
64
 
82
65
struct handle_option_ctx
83
66
{
84
 
   memory::Root *alloc;
 
67
   MEM_ROOT *alloc;
85
68
   DYNAMIC_ARRAY *args;
86
69
   TYPELIB *group;
87
70
};
176
159
    /* Handle --defaults-group-suffix= */
177
160
    uint32_t i;
178
161
    const char **extra_groups;
179
 
    const size_t instance_len= strlen(my_defaults_group_suffix);
 
162
    const uint32_t instance_len= strlen(my_defaults_group_suffix);
180
163
    struct handle_option_ctx *ctx= (struct handle_option_ctx*) func_ctx;
181
164
    char *ptr;
182
165
    TYPELIB *group= ctx->group;
183
166
 
184
167
    if (!(extra_groups=
185
 
          (const char**)ctx->alloc->alloc_root(
 
168
          (const char**)alloc_root(ctx->alloc,
186
169
                                   (2*group->count+1)*sizeof(char*))))
187
170
      goto err;
188
171
 
189
172
    for (i= 0; i < group->count; i++)
190
173
    {
191
 
      size_t len;
 
174
      uint32_t len;
192
175
      extra_groups[i]= group->type_names[i]; /** copy group */
193
176
 
194
177
      len= strlen(extra_groups[i]);
195
 
      if (!(ptr= (char *)ctx->alloc->alloc_root( len+instance_len+1)))
 
178
      if (!(ptr= (char *)alloc_root(ctx->alloc, len+instance_len+1)))
196
179
        goto err;
197
180
 
198
181
      extra_groups[i+group->count]= ptr;
288
271
  if (!option)
289
272
    return 0;
290
273
 
291
 
  if (ctx->group->find_type(const_cast<char*>(group_name), 3))
 
274
  if (find_type((char *)group_name, ctx->group, 3))
292
275
  {
293
 
    if (!(tmp= (char *)ctx->alloc->alloc_root(strlen(option) + 1)))
 
276
    if (!(tmp= (char *)alloc_root(ctx->alloc, strlen(option) + 1)))
294
277
      return 1;
295
278
    if (insert_dynamic(ctx->args, (unsigned char*) &tmp))
296
279
      return 1;
325
308
  int org_argc= argc, prev_argc= 0;
326
309
  *defaults= *extra_defaults= *group_suffix= 0;
327
310
 
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
311
  while (argc >= 2 && argc != prev_argc)
333
312
  {
334
313
    /* Skip program name or previously handled argument */
335
314
    argv++;
336
315
    prev_argc= argc;                            /* To check if we found */
337
 
    if (!*defaults && (strncmp(*argv,
338
 
                               DEFAULTS_FILE.c_str(),
339
 
                               DEFAULTS_FILE.size()) == 0))
 
316
    if (!*defaults && is_prefix(*argv,"--defaults-file="))
340
317
    {
341
 
      *defaults= *argv + DEFAULTS_FILE.size();
 
318
      *defaults= *argv + sizeof("--defaults-file=")-1;
342
319
       argc--;
343
320
       continue;
344
321
    }
345
 
    if (!*extra_defaults && (strncmp(*argv, 
346
 
                                     DEFAULTS_EXTRA_FILE.c_str(),
347
 
                                     DEFAULTS_EXTRA_FILE.size()) == 0))
 
322
    if (!*extra_defaults && is_prefix(*argv,"--defaults-extra-file="))
348
323
    {
349
 
      *extra_defaults= *argv + DEFAULTS_EXTRA_FILE.size();
 
324
      *extra_defaults= *argv + sizeof("--defaults-extra-file=")-1;
350
325
      argc--;
351
326
      continue;
352
327
    }
353
 
    if (!*group_suffix && (strncmp(*argv, 
354
 
                                   DEFAULTS_GROUP_SUFFIX.c_str(),
355
 
                                   DEFAULTS_GROUP_SUFFIX.size()) == 0))
356
 
 
 
328
    if (!*group_suffix && is_prefix(*argv, "--defaults-group-suffix="))
357
329
    {
358
 
      *group_suffix= *argv + DEFAULTS_GROUP_SUFFIX.size();
 
330
      *group_suffix= *argv + sizeof("--defaults-group-suffix=")-1;
359
331
      argc--;
360
332
      continue;
361
333
    }
400
372
{
401
373
  DYNAMIC_ARRAY args;
402
374
  TYPELIB group;
 
375
  bool found_print_defaults= 0;
403
376
  uint32_t args_used= 0;
404
377
  int error= 0;
405
 
  memory::Root alloc(512);
 
378
  MEM_ROOT alloc;
406
379
  char *ptr,**res;
407
380
  struct handle_option_ctx ctx;
408
381
 
409
382
  init_default_directories();
 
383
  init_alloc_root(&alloc,512,0);
410
384
  /*
411
385
    Check if the user doesn't want any default option processing
412
386
    --no-defaults is always the first option
415
389
  {
416
390
    /* remove the --no-defaults argument and return only the other arguments */
417
391
    uint32_t i;
418
 
    if (!(ptr=(char*) alloc.alloc_root(sizeof(alloc)+ (*argc + 1)*sizeof(char*))))
 
392
    if (!(ptr=(char*) alloc_root(&alloc,sizeof(alloc)+
 
393
                                 (*argc + 1)*sizeof(char*))))
419
394
      goto err;
420
395
    res= (char**) (ptr+sizeof(alloc));
421
 
    memset(res,0,(*argc + 1));
422
396
    res[0]= **argv;                             /* Copy program name */
423
397
    for (i=2 ; i < (uint32_t) *argc ; i++)
424
398
      res[i-1]=argv[0][i];
425
399
    res[i-1]=0;                                 /* End pointer */
426
400
    (*argc)--;
427
401
    *argv=res;
428
 
    *(memory::Root*) ptr= alloc;                        /* Save alloc root for free */
 
402
    *(MEM_ROOT*) ptr= alloc;                    /* Save alloc root for free */
429
403
    return(0);
430
404
  }
431
405
 
449
423
    Here error contains <> 0 only if we have a fully specified conf_file
450
424
    or a forced default file
451
425
  */
452
 
  if (!(ptr=(char*) alloc.alloc_root(sizeof(alloc)+ (args.elements + *argc +1) *sizeof(char*))))
 
426
  if (!(ptr=(char*) alloc_root(&alloc,sizeof(alloc)+
 
427
                               (args.elements + *argc +1) *sizeof(char*))))
453
428
    goto err;
454
429
  res= (char**) (ptr+sizeof(alloc));
455
430
 
464
439
    Check if we wan't to see the new argument list
465
440
    This options must always be the last of the default options
466
441
  */
 
442
  if (*argc >= 2 && !strcmp(argv[0][1],"--print-defaults"))
 
443
  {
 
444
    found_print_defaults=1;
 
445
    --*argc; ++*argv;                           /* skip argument */
 
446
  }
 
447
 
467
448
  if (*argc)
468
449
    memcpy(res+1+args.elements, *argv + 1, (*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:
483
473
 
484
474
void free_defaults(char **argv)
485
475
{
486
 
  memory::Root ptr;
 
476
  MEM_ROOT ptr;
487
477
  memcpy(&ptr, (char*) argv - sizeof(ptr), sizeof(ptr));
488
 
  ptr.free_root(MYF(0));
 
478
  free_root(&ptr,MYF(0));
489
479
}
490
480
 
491
481
 
598
588
  FILE *fp;
599
589
  uint32_t line=0;
600
590
  bool found_group=0;
 
591
  uint32_t i;
 
592
  MY_DIR *search_dir;
 
593
  FILEINFO *search_file;
601
594
 
602
595
  if ((dir ? strlen(dir) : 0 )+strlen(config_file) >= FN_REFLEN-3)
603
596
    return 0;                                   /* Ignore wrong paths */
630
623
      return 0;
631
624
    }
632
625
  }
633
 
  if (!(fp= fopen(name, "r")))
 
626
  if (!(fp= my_fopen(name, O_RDONLY, MYF(0))))
634
627
    return 1;                                   /* Ignore wrong files */
635
628
 
636
 
  memset(buff,0,sizeof(buff));
637
629
  while (fgets(buff, sizeof(buff) - 1, fp))
638
630
  {
639
631
    line++;
674
666
                                ptr, name, line)))
675
667
          goto err;
676
668
 
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);
 
669
        if (!(search_dir= my_dir(ptr, MYF(MY_WME))))
689
670
          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())
 
671
 
 
672
        for (i= 0; i < (uint32_t) search_dir->number_off_files; i++)
696
673
        {
697
 
          CachedDirectory::Entry *entry= *file_iter;
698
 
          ext= fn_ext(entry->filename.c_str());
 
674
          search_file= search_dir->dir_entry + i;
 
675
          ext= fn_ext(search_file->name);
699
676
 
700
677
          /* check extension */
701
678
          for (tmp_ext= (char**) f_extensions; *tmp_ext; tmp_ext++)
702
679
          {
703
680
            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;
 
681
              break;
 
682
          }
 
683
 
 
684
          if (*tmp_ext)
 
685
          {
 
686
            fn_format(tmp, search_file->name, ptr, "",
 
687
                      MY_UNPACK_FILENAME | MY_SAFE_PATH);
 
688
 
 
689
            search_default_file_with_ext(opt_handler, handler_ctx, "", "", tmp,
 
690
                                         recursion_level + 1);
 
691
          }
714
692
        }
 
693
 
 
694
        my_dirend(search_dir);
715
695
      }
716
696
      else if ((!strncmp(ptr, include_keyword, sizeof(include_keyword) - 1)) &&
717
697
               my_isspace(&my_charset_utf8_general_ci, ptr[sizeof(include_keyword)-1]))
742
722
      for ( ; my_isspace(&my_charset_utf8_general_ci,end[-1]) ; end--) ;
743
723
      end[0]=0;
744
724
 
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';
 
725
      strncpy(curr_gr, ptr, cmin((size_t) (end-ptr)+1, sizeof(curr_gr)-1));
 
726
      curr_gr[cmin((size_t)(end-ptr)+1, sizeof(curr_gr)-1)] = '\0';
747
727
 
748
728
      /* signal that a new group is found */
749
729
      opt_handler(handler_ctx, curr_gr, NULL);
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_utf8_general_ci,end[-1]) ; end--) ;
766
746
    if (!value)
767
747
    {
768
748
      strncpy(strcpy(option,"--")+2,ptr,strlen(ptr)+1);
791
771
        value++;
792
772
        value_end--;
793
773
      }
794
 
      
795
 
      memset(option,0,2+(size_t)(end-ptr)+1);
796
774
      ptr= strncpy(strcpy(option,"--")+2,ptr,(size_t) (end-ptr));
797
 
      ptr[end-ptr]= '\0';
798
775
      ptr+= strlen(ptr);
799
776
      *ptr++= '=';
800
777
 
841
818
        goto err;
842
819
    }
843
820
  }
844
 
  fclose(fp);
 
821
  my_fclose(fp,MYF(0));
845
822
  return(0);
846
823
 
847
824
 err:
848
 
  fclose(fp);
849
 
 
 
825
  my_fclose(fp,MYF(0));
850
826
  return -1;                                    /* Fatal error */
851
827
}
852
828
 
937
913
    }
938
914
  }
939
915
  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");
 
916
--print-defaults        Print the program argument list and exit\n\
 
917
--no-defaults           Don't read default options from any options file\n\
 
918
--defaults-file=#       Only read default options from the given file #\n\
 
919
--defaults-extra-file=# Read this file after the global files are read");
943
920
}
944
921
 
945
922
/*
946
923
  This extra complexity is to avoid declaring 'rc' if it won't be
947
924
  used.
948
925
*/
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
 
}
 
926
#define ADD_DIRECTORY(DIR)  (void) array_append_string_unique((DIR), default_directories, \
 
927
                             array_elements(default_directories))
 
928
 
 
929
#define ADD_COMMON_DIRECTORIES() \
 
930
  do { \
 
931
    const char *env; \
 
932
    if ((env= getenv("DRIZZLE_HOME"))) \
 
933
      ADD_DIRECTORY(env); \
 
934
    /* Placeholder for --defaults-extra-file=<path> */ \
 
935
    ADD_DIRECTORY(""); \
 
936
  } while (0)
 
937
 
962
938
 
963
939
/**
964
940
  Initialize default directories for Unix
975
951
static void init_default_directories(void)
976
952
{
977
953
  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("~/");
 
954
  ADD_DIRECTORY("/etc/");
 
955
  ADD_DIRECTORY("/etc/drizzle/");
 
956
  ADD_DIRECTORY(SYSCONFDIR);
 
957
  ADD_COMMON_DIRECTORIES();
 
958
  ADD_DIRECTORY("~/");
983
959
}
984
 
 
985
 
} /* namespace internal */
986
 
} /* namespace drizzled */