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 */
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
****************************************************************************/
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/mysys_priv.h"
37
#include <mystrings/m_string.h>
38
#include <mystrings/m_ctype.h>
42
39
#include <drizzled/configmake.h>
43
40
#include <drizzled/gettext.h>
45
#include "drizzled/cached_directory.h"
47
#ifdef HAVE_SYS_STAT_H
48
# include <sys/stat.h>
42
#include <mysys/cached_directory.h>
52
45
#include <algorithm>
54
47
using namespace std;
61
49
const char *my_defaults_file=0;
62
50
const char *my_defaults_group_suffix=0;
63
51
char *my_defaults_extra_file=0;
182
170
TYPELIB *group= ctx->group;
184
172
if (!(extra_groups=
185
(const char**)ctx->alloc->alloc_root(
173
(const char**)alloc_root(ctx->alloc,
186
174
(2*group->count+1)*sizeof(char*))))
192
180
extra_groups[i]= group->type_names[i]; /** copy group */
194
182
len= strlen(extra_groups[i]);
195
if (!(ptr= (char *)ctx->alloc->alloc_root( len+instance_len+1)))
183
if (!(ptr= (char *)alloc_root(ctx->alloc, len+instance_len+1)))
198
186
extra_groups[i+group->count]= ptr;
291
if (ctx->group->find_type(const_cast<char*>(group_name), 3))
279
if (find_type((char *)group_name, ctx->group, 3))
293
if (!(tmp= (char *)ctx->alloc->alloc_root(strlen(option) + 1)))
281
if (!(tmp= (char *)alloc_root(ctx->alloc, strlen(option) + 1)))
295
283
if (insert_dynamic(ctx->args, (unsigned char*) &tmp))
325
313
int org_argc= argc, prev_argc= 0;
326
314
*defaults= *extra_defaults= *group_suffix= 0;
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=");
332
316
while (argc >= 2 && argc != prev_argc)
334
318
/* Skip program name or previously handled argument */
336
320
prev_argc= argc; /* To check if we found */
337
if (!*defaults && (strncmp(*argv,
338
DEFAULTS_FILE.c_str(),
339
DEFAULTS_FILE.size()) == 0))
321
if (!*defaults && is_prefix(*argv,"--defaults-file="))
341
*defaults= *argv + DEFAULTS_FILE.size();
323
*defaults= *argv + sizeof("--defaults-file=")-1;
345
if (!*extra_defaults && (strncmp(*argv,
346
DEFAULTS_EXTRA_FILE.c_str(),
347
DEFAULTS_EXTRA_FILE.size()) == 0))
327
if (!*extra_defaults && is_prefix(*argv,"--defaults-extra-file="))
349
*extra_defaults= *argv + DEFAULTS_EXTRA_FILE.size();
329
*extra_defaults= *argv + sizeof("--defaults-extra-file=")-1;
353
if (!*group_suffix && (strncmp(*argv,
354
DEFAULTS_GROUP_SUFFIX.c_str(),
355
DEFAULTS_GROUP_SUFFIX.size()) == 0))
333
if (!*group_suffix && is_prefix(*argv, "--defaults-group-suffix="))
358
*group_suffix= *argv + DEFAULTS_GROUP_SUFFIX.size();
335
*group_suffix= *argv + sizeof("--defaults-group-suffix=")-1;
401
378
DYNAMIC_ARRAY args;
380
bool found_print_defaults= 0;
403
381
uint32_t args_used= 0;
405
memory::Root alloc(512);
407
385
struct handle_option_ctx ctx;
409
387
init_default_directories();
388
init_alloc_root(&alloc,512,0);
411
390
Check if the user doesn't want any default option processing
412
391
--no-defaults is always the first option
416
395
/* remove the --no-defaults argument and return only the other arguments */
418
if (!(ptr=(char*) alloc.alloc_root(sizeof(alloc)+ (*argc + 1)*sizeof(char*))))
397
if (!(ptr=(char*) alloc_root(&alloc,sizeof(alloc)+
398
(*argc + 1)*sizeof(char*))))
420
400
res= (char**) (ptr+sizeof(alloc));
421
401
memset(res,0,(*argc + 1));
449
429
Here error contains <> 0 only if we have a fully specified conf_file
450
430
or a forced default file
452
if (!(ptr=(char*) alloc.alloc_root(sizeof(alloc)+ (args.elements + *argc +1) *sizeof(char*))))
432
if (!(ptr=(char*) alloc_root(&alloc,sizeof(alloc)+
433
(args.elements + *argc +1) *sizeof(char*))))
454
435
res= (char**) (ptr+sizeof(alloc));
464
445
Check if we wan't to see the new argument list
465
446
This options must always be the last of the default options
448
if (*argc >= 2 && !strcmp(argv[0][1],"--print-defaults"))
450
found_print_defaults=1;
451
--*argc; ++*argv; /* skip argument */
468
455
memcpy(res+1+args.elements, *argv + 1, (*argc-1)*sizeof(char*));
469
456
res[args.elements+ *argc]=0; /* last null */
471
458
(*argc)+=int(args.elements);
472
459
*argv= static_cast<char**>(res);
473
*(memory::Root*) ptr= alloc; /* Save alloc root for free */
460
*(MEM_ROOT*) ptr= alloc; /* Save alloc root for free */
474
461
delete_dynamic(&args);
462
if (found_print_defaults)
465
printf("%s would have been started with the following arguments:\n",
467
for (i=1 ; i < *argc ; i++)
468
printf("%s ", (*argv)[i]);
939
935
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");
936
--print-defaults Print the program argument list and exit\n\
937
--no-defaults Don't read default options from any options file\n\
938
--defaults-file=# Only read default options from the given file #\n\
939
--defaults-extra-file=# Read this file after the global files are read");
946
943
This extra complexity is to avoid declaring 'rc' if it won't be
949
static void add_directory(const char* dir)
951
array_append_string_unique(dir, default_directories, array_elements(default_directories));
954
static void add_common_directories()
956
const char *env= getenv("DRIZZLE_HOME");
959
// Placeholder for --defaults-extra-file=<path>
946
#define ADD_DIRECTORY(DIR) (void) array_append_string_unique((DIR), default_directories, \
947
array_elements(default_directories))
949
#define ADD_COMMON_DIRECTORIES() \
952
if ((env= getenv("DRIZZLE_HOME"))) \
953
ADD_DIRECTORY(env); \
954
/* Placeholder for --defaults-extra-file=<path> */ \
964
960
Initialize default directories for Unix
975
971
static void init_default_directories(void)
977
973
memset(default_directories, 0, sizeof(default_directories));
978
add_directory("/etc/");
979
add_directory("/etc/drizzle/");
980
add_directory(SYSCONFDIR);
981
add_common_directories();
974
ADD_DIRECTORY("/etc/");
975
ADD_DIRECTORY("/etc/drizzle/");
976
ADD_DIRECTORY(SYSCONFDIR);
977
ADD_COMMON_DIRECTORIES();
985
} /* namespace internal */
986
} /* namespace drizzled */