~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/internal/default.cc

  • Committer: Brian Aker
  • Date: 2010-01-22 00:53:13 UTC
  • Revision ID: brian@gaz-20100122005313-jmizcbcdi1lt4tcx
Revert db patch.

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 "drizzled/internal/mysys_priv.h"
 
37
#include "drizzled/internal/m_string.h"
 
38
#include "drizzled/charset_info.h"
42
39
#include <drizzled/configmake.h>
43
40
#include <drizzled/gettext.h>
44
41
 
45
 
#include <drizzled/cached_directory.h>
 
42
#include "drizzled/cached_directory.h"
46
43
 
47
44
#ifdef HAVE_SYS_STAT_H
48
45
# include <sys/stat.h>
51
48
#include <cstdio>
52
49
#include <algorithm>
53
50
 
 
51
 
 
52
using namespace drizzled;
54
53
using namespace std;
55
54
 
56
 
namespace drizzled
57
 
{
58
 
namespace internal
59
 
{
60
 
 
61
55
const char *my_defaults_file=0;
62
56
const char *my_defaults_group_suffix=0;
63
57
char *my_defaults_extra_file=0;
182
176
    TYPELIB *group= ctx->group;
183
177
 
184
178
    if (!(extra_groups=
185
 
          (const char**)ctx->alloc->alloc_root(
 
179
          (const char**)alloc_root(ctx->alloc,
186
180
                                   (2*group->count+1)*sizeof(char*))))
187
181
      goto err;
188
182
 
192
186
      extra_groups[i]= group->type_names[i]; /** copy group */
193
187
 
194
188
      len= strlen(extra_groups[i]);
195
 
      if (!(ptr= (char *)ctx->alloc->alloc_root( len+instance_len+1)))
 
189
      if (!(ptr= (char *)alloc_root(ctx->alloc, len+instance_len+1)))
196
190
        goto err;
197
191
 
198
192
      extra_groups[i+group->count]= ptr;
288
282
  if (!option)
289
283
    return 0;
290
284
 
291
 
  if (ctx->group->find_type(const_cast<char*>(group_name), 3))
 
285
  if (find_type((char *)group_name, ctx->group, 3))
292
286
  {
293
 
    if (!(tmp= (char *)ctx->alloc->alloc_root(strlen(option) + 1)))
 
287
    if (!(tmp= (char *)alloc_root(ctx->alloc, strlen(option) + 1)))
294
288
      return 1;
295
289
    if (insert_dynamic(ctx->args, (unsigned char*) &tmp))
296
290
      return 1;
325
319
  int org_argc= argc, prev_argc= 0;
326
320
  *defaults= *extra_defaults= *group_suffix= 0;
327
321
 
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
322
  while (argc >= 2 && argc != prev_argc)
333
323
  {
334
324
    /* Skip program name or previously handled argument */
335
325
    argv++;
336
326
    prev_argc= argc;                            /* To check if we found */
337
 
    if (!*defaults && (strncmp(*argv,
338
 
                               DEFAULTS_FILE.c_str(),
339
 
                               DEFAULTS_FILE.size()) == 0))
 
327
    if (!*defaults && (strncmp(*argv,"--defaults-file=", sizeof("--defaults-file=")) == 0))
340
328
    {
341
 
      *defaults= *argv + DEFAULTS_FILE.size();
 
329
      *defaults= *argv + sizeof("--defaults-file=")-1;
342
330
       argc--;
343
331
       continue;
344
332
    }
345
 
    if (!*extra_defaults && (strncmp(*argv, 
346
 
                                     DEFAULTS_EXTRA_FILE.c_str(),
347
 
                                     DEFAULTS_EXTRA_FILE.size()) == 0))
 
333
    if (!*extra_defaults && (strncmp(*argv, "--defaults-extra-file=", sizeof("--defaults-extra-file=")) == 0))
348
334
    {
349
 
      *extra_defaults= *argv + DEFAULTS_EXTRA_FILE.size();
 
335
      *extra_defaults= *argv + sizeof("--defaults-extra-file=")-1;
350
336
      argc--;
351
337
      continue;
352
338
    }
353
 
    if (!*group_suffix && (strncmp(*argv, 
354
 
                                   DEFAULTS_GROUP_SUFFIX.c_str(),
355
 
                                   DEFAULTS_GROUP_SUFFIX.size()) == 0))
356
 
 
 
339
    if (!*group_suffix && (strncmp(*argv, "--defaults-group-suffix=", sizeof("--defaults-group-suffix=")) == 0))
357
340
    {
358
 
      *group_suffix= *argv + DEFAULTS_GROUP_SUFFIX.size();
 
341
      *group_suffix= *argv + sizeof("--defaults-group-suffix=")-1;
359
342
      argc--;
360
343
      continue;
361
344
    }
400
383
{
401
384
  DYNAMIC_ARRAY args;
402
385
  TYPELIB group;
 
386
  bool found_print_defaults= 0;
403
387
  uint32_t args_used= 0;
404
388
  int error= 0;
405
 
  memory::Root alloc(512);
 
389
  memory::Root alloc;
406
390
  char *ptr,**res;
407
391
  struct handle_option_ctx ctx;
408
392
 
409
393
  init_default_directories();
 
394
  init_alloc_root(&alloc,512);
410
395
  /*
411
396
    Check if the user doesn't want any default option processing
412
397
    --no-defaults is always the first option
415
400
  {
416
401
    /* remove the --no-defaults argument and return only the other arguments */
417
402
    uint32_t i;
418
 
    if (!(ptr=(char*) alloc.alloc_root(sizeof(alloc)+ (*argc + 1)*sizeof(char*))))
 
403
    if (!(ptr=(char*) alloc_root(&alloc,sizeof(alloc)+
 
404
                                 (*argc + 1)*sizeof(char*))))
419
405
      goto err;
420
406
    res= (char**) (ptr+sizeof(alloc));
421
407
    memset(res,0,(*argc + 1));
449
435
    Here error contains <> 0 only if we have a fully specified conf_file
450
436
    or a forced default file
451
437
  */
452
 
  if (!(ptr=(char*) alloc.alloc_root(sizeof(alloc)+ (args.elements + *argc +1) *sizeof(char*))))
 
438
  if (!(ptr=(char*) alloc_root(&alloc,sizeof(alloc)+
 
439
                               (args.elements + *argc +1) *sizeof(char*))))
453
440
    goto err;
454
441
  res= (char**) (ptr+sizeof(alloc));
455
442
 
464
451
    Check if we wan't to see the new argument list
465
452
    This options must always be the last of the default options
466
453
  */
 
454
  if (*argc >= 2 && !strcmp(argv[0][1],"--print-defaults"))
 
455
  {
 
456
    found_print_defaults=1;
 
457
    --*argc; ++*argv;                           /* skip argument */
 
458
  }
 
459
 
467
460
  if (*argc)
468
461
    memcpy(res+1+args.elements, *argv + 1, (*argc-1)*sizeof(char*));
469
462
  res[args.elements+ *argc]=0;                  /* last null */
472
465
  *argv= static_cast<char**>(res);
473
466
  *(memory::Root*) ptr= alloc;                  /* Save alloc root for free */
474
467
  delete_dynamic(&args);
475
 
 
 
468
  if (found_print_defaults)
 
469
  {
 
470
    int i;
 
471
    printf("%s would have been started with the following arguments:\n",
 
472
           **argv);
 
473
    for (i=1 ; i < *argc ; i++)
 
474
      printf("%s ", (*argv)[i]);
 
475
    puts("");
 
476
    exit(0);
 
477
  }
476
478
  return(error);
477
479
 
478
480
 err:
485
487
{
486
488
  memory::Root ptr;
487
489
  memcpy(&ptr, (char*) argv - sizeof(ptr), sizeof(ptr));
488
 
  ptr.free_root(MYF(0));
 
490
  free_root(&ptr,MYF(0));
489
491
}
490
492
 
491
493
 
674
676
                                ptr, name, line)))
675
677
          goto err;
676
678
 
677
 
        CachedDirectory dir_cache(ptr);
 
679
        drizzled::CachedDirectory dir_cache(ptr);
678
680
 
679
681
        if (dir_cache.fail())
680
682
        {
689
691
          goto err;
690
692
        }
691
693
 
692
 
        CachedDirectory::Entries files= dir_cache.getEntries();
693
 
        CachedDirectory::Entries::iterator file_iter= files.begin();
 
694
        drizzled::CachedDirectory::Entries files= dir_cache.getEntries();
 
695
        drizzled::CachedDirectory::Entries::iterator file_iter= files.begin();
694
696
 
695
697
        while (file_iter != files.end())
696
698
        {
697
 
          CachedDirectory::Entry *entry= *file_iter;
 
699
          drizzled::CachedDirectory::Entry *entry= *file_iter;
698
700
          ext= fn_ext(entry->filename.c_str());
699
701
 
700
702
          /* check extension */
937
939
    }
938
940
  }
939
941
  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");
 
942
--print-defaults        Print the program argument list and exit\n\
 
943
--no-defaults           Don't read default options from any options file\n\
 
944
--defaults-file=#       Only read default options from the given file #\n\
 
945
--defaults-extra-file=# Read this file after the global files are read");
943
946
}
944
947
 
945
948
/*
946
949
  This extra complexity is to avoid declaring 'rc' if it won't be
947
950
  used.
948
951
*/
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
 
}
 
952
#define ADD_DIRECTORY(DIR)  (void) array_append_string_unique((DIR), default_directories, \
 
953
                             array_elements(default_directories))
 
954
 
 
955
#define ADD_COMMON_DIRECTORIES() \
 
956
  do { \
 
957
    const char *env; \
 
958
    if ((env= getenv("DRIZZLE_HOME"))) \
 
959
      ADD_DIRECTORY(env); \
 
960
    /* Placeholder for --defaults-extra-file=<path> */ \
 
961
    ADD_DIRECTORY(""); \
 
962
  } while (0)
 
963
 
962
964
 
963
965
/**
964
966
  Initialize default directories for Unix
975
977
static void init_default_directories(void)
976
978
{
977
979
  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("~/");
 
980
  ADD_DIRECTORY("/etc/");
 
981
  ADD_DIRECTORY("/etc/drizzle/");
 
982
  ADD_DIRECTORY(SYSCONFDIR);
 
983
  ADD_COMMON_DIRECTORIES();
 
984
  ADD_DIRECTORY("~/");
983
985
}
984
 
 
985
 
} /* namespace internal */
986
 
} /* namespace drizzled */