summaryrefslogtreecommitdiff
path: root/mysys/default.c
diff options
context:
space:
mode:
Diffstat (limited to 'mysys/default.c')
-rw-r--r--mysys/default.c131
1 files changed, 87 insertions, 44 deletions
diff --git a/mysys/default.c b/mysys/default.c
index 0d162dc13d5..4e9d4faae91 100644
--- a/mysys/default.c
+++ b/mysys/default.c
@@ -96,9 +96,9 @@ static int search_default_file_with_ext(Process_option_func func,
- Windows: C:/
- Windows: Directory above where the executable is located
- Netware: sys:/etc/
- - Unix & OS/2: /etc/
+ - Unix: /etc/
+ - Unix: /etc/mysql/
- Unix: --sysconfdir=<path> (compile-time option)
- - OS/2: getenv(ETC)
- ALL: getenv(DEFAULT_HOME_ENV)
- ALL: --defaults-extra-file=<path> (run-time option)
- Unix: ~/
@@ -144,6 +144,7 @@ static char *remove_end_comment(char *ptr);
RETURN
0 ok
1 given cinf_file doesn't exist
+ 2 out of memory
The global variable 'my_defaults_group_suffix' is updated with value for
--defaults_group_suffix
@@ -151,7 +152,7 @@ static char *remove_end_comment(char *ptr);
int my_search_option_files(const char *conf_file, int *argc, char ***argv,
uint *args_used, Process_option_func func,
- void *func_ctx)
+ void *func_ctx, const char **default_directories)
{
const char **dirs, *forced_default_file, *forced_extra_defaults;
int error= 0;
@@ -190,7 +191,7 @@ int my_search_option_files(const char *conf_file, int *argc, char ***argv,
if (!(extra_groups=
(const char**)alloc_root(ctx->alloc,
(2*group->count+1)*sizeof(char*))))
- goto err;
+ DBUG_RETURN(2);
for (i= 0; i < group->count; i++)
{
@@ -199,7 +200,7 @@ int my_search_option_files(const char *conf_file, int *argc, char ***argv,
len= strlen(extra_groups[i]);
if (!(ptr= alloc_root(ctx->alloc, (uint) (len+instance_len+1))))
- goto err;
+ DBUG_RETURN(2);
extra_groups[i+group->count]= ptr;
@@ -254,12 +255,11 @@ int my_search_option_files(const char *conf_file, int *argc, char ***argv,
}
}
- DBUG_RETURN(error);
+ DBUG_RETURN(0);
err:
fprintf(stderr,"Fatal error in defaults handling. Program aborted\n");
- exit(1);
- return 0; /* Keep compiler happy */
+ DBUG_RETURN(1);
}
@@ -272,7 +272,8 @@ err:
handle_option_ctx structure.
group_name The name of the group the option belongs to.
option The very option to be processed. It is already
- prepared to be used in argv (has -- prefix)
+ prepared to be used in argv (has -- prefix). If it
+ is NULL, we are handling a new group (section).
DESCRIPTION
This handler checks whether a group is one of the listed and adds an option
@@ -291,11 +292,14 @@ static int handle_default_option(void *in_ctx, const char *group_name,
char *tmp;
struct handle_option_ctx *ctx= (struct handle_option_ctx *) in_ctx;
+ if (!option)
+ return 0;
+
if (find_type((char *)group_name, ctx->group, 3))
{
- if (!(tmp= alloc_root(ctx->alloc, (uint) strlen(option) + 1)))
+ if (!(tmp= alloc_root(ctx->alloc, strlen(option) + 1)))
return 1;
- if (insert_dynamic(ctx->args, (gptr) &tmp))
+ if (insert_dynamic(ctx->args, (uchar*) &tmp))
return 1;
strmov(tmp, option);
}
@@ -355,18 +359,46 @@ int get_defaults_options(int argc, char **argv,
return org_argc - argc;
}
+/*
+ Wrapper around my_load_defaults() for interface compatibility.
+
+ SYNOPSIS
+ load_defaults()
+ conf_file Basename for configuration file to search for.
+ If this is a path, then only this file is read.
+ groups Which [group] entrys to read.
+ Points to an null terminated array of pointers
+ argc Pointer to argc of original program
+ argv Pointer to argv of original program
+
+ NOTES
+
+ This function is NOT thread-safe as it uses a global pointer internally.
+ See also notes for my_load_defaults().
+
+ RETURN
+ 0 ok
+ 1 The given conf_file didn't exists
+*/
+int load_defaults(const char *conf_file, const char **groups,
+ int *argc, char ***argv)
+{
+ return my_load_defaults(conf_file, groups, argc, argv, &default_directories);
+}
/*
Read options from configurations files
SYNOPSIS
- load_defaults()
+ my_load_defaults()
conf_file Basename for configuration file to search for.
If this is a path, then only this file is read.
groups Which [group] entrys to read.
Points to an null terminated array of pointers
argc Pointer to argc of original program
argv Pointer to argv of original program
+ default_directories Pointer to a location where a pointer to the list
+ of default directories will be stored
IMPLEMENTATION
@@ -382,13 +414,18 @@ int get_defaults_options(int argc, char **argv,
that was put in *argv
RETURN
- 0 ok
- 1 The given conf_file didn't exists
+ - If successful, 0 is returned. If 'default_directories' is not NULL,
+ a pointer to the array of default directory paths is stored to a location
+ it points to. That stored value must be passed to my_search_option_files()
+ later.
+
+ - 1 is returned if the given conf_file didn't exist. In this case, the
+ value pointed to by default_directories is undefined.
*/
-int load_defaults(const char *conf_file, const char **groups,
- int *argc, char ***argv)
+int my_load_defaults(const char *conf_file, const char **groups,
+ int *argc, char ***argv, const char ***default_directories)
{
DYNAMIC_ARRAY args;
TYPELIB group;
@@ -398,10 +435,11 @@ int load_defaults(const char *conf_file, const char **groups,
MEM_ROOT alloc;
char *ptr,**res;
struct handle_option_ctx ctx;
+ const char **dirs;
DBUG_ENTER("load_defaults");
init_alloc_root(&alloc,512,0);
- if ((default_directories= init_default_directories(&alloc)) == NULL)
+ if ((dirs= init_default_directories(&alloc)) == NULL)
goto err;
/*
Check if the user doesn't want any default option processing
@@ -422,6 +460,8 @@ int load_defaults(const char *conf_file, const char **groups,
(*argc)--;
*argv=res;
*(MEM_ROOT*) ptr= alloc; /* Save alloc root for free */
+ if (default_directories)
+ *default_directories= dirs;
DBUG_RETURN(0);
}
@@ -440,7 +480,8 @@ int load_defaults(const char *conf_file, const char **groups,
ctx.group= &group;
error= my_search_option_files(conf_file, argc, argv, &args_used,
- handle_default_option, (void *) &ctx);
+ handle_default_option, (void *) &ctx,
+ dirs);
/*
Here error contains <> 0 only if we have a fully specified conf_file
or a forced default file
@@ -452,7 +493,7 @@ int load_defaults(const char *conf_file, const char **groups,
/* copy name + found arguments + command line arguments to new array */
res[0]= argv[0][0]; /* Name MUST be set, even by embedded library */
- memcpy((gptr) (res+1), args.buffer, args.elements*sizeof(char*));
+ memcpy((uchar*) (res+1), args.buffer, args.elements*sizeof(char*));
/* Skip --defaults-xxx options */
(*argc)-= args_used;
(*argv)+= args_used;
@@ -468,7 +509,7 @@ int load_defaults(const char *conf_file, const char **groups,
}
if (*argc)
- memcpy((gptr) (res+1+args.elements), (char*) ((*argv)+1),
+ memcpy((uchar*) (res+1+args.elements), (char*) ((*argv)+1),
(*argc-1)*sizeof(char*));
res[args.elements+ *argc]=0; /* last null */
@@ -486,6 +527,10 @@ int load_defaults(const char *conf_file, const char **groups,
puts("");
exit(0);
}
+
+ if (error == 0 && default_directories)
+ *default_directories= dirs;
+
DBUG_RETURN(error);
err:
@@ -540,7 +585,7 @@ static int search_default_file(Process_option_func opt_handler,
# Returns pointer to the argument after the keyword.
*/
-static char *get_argument(const char *keyword, uint kwlen,
+static char *get_argument(const char *keyword, size_t kwlen,
char *ptr, char *name, uint line)
{
char *end;
@@ -630,7 +675,7 @@ static int search_default_file_with_ext(Process_option_func opt_handler,
strmov(name,config_file);
}
fn_format(name,name,"","",4);
-#if !defined(__WIN__) && !defined(OS2) && !defined(__NETWARE__)
+#if !defined(__WIN__) && !defined(__NETWARE__)
{
MY_STAT stat_info;
if (!my_stat(name,&stat_info,MYF(0)))
@@ -744,10 +789,15 @@ static int search_default_file_with_ext(Process_option_func opt_handler,
name,line);
goto err;
}
- for ( ; my_isspace(&my_charset_latin1,end[-1]) ; end--) ;/* Remove end space */
+ /* Remove end space */
+ for ( ; my_isspace(&my_charset_latin1,end[-1]) ; end--) ;
end[0]=0;
- strnmov(curr_gr, ptr, min((uint) (end-ptr)+1, 4096));
+ strmake(curr_gr, ptr, min((size_t) (end-ptr)+1, sizeof(curr_gr)-1));
+
+ /* signal that a new group is found */
+ opt_handler(handler_ctx, curr_gr, NULL);
+
continue;
}
if (!found_group)
@@ -765,7 +815,7 @@ static int search_default_file_with_ext(Process_option_func opt_handler,
for ( ; my_isspace(&my_charset_latin1,end[-1]) ; end--) ;
if (!value)
{
- strmake(strmov(option,"--"),ptr,(uint) (end-ptr));
+ strmake(strmov(option,"--"),ptr, (size_t) (end-ptr));
if (opt_handler(handler_ctx, curr_gr, option))
goto err;
}
@@ -791,7 +841,7 @@ static int search_default_file_with_ext(Process_option_func opt_handler,
value++;
value_end--;
}
- ptr=strnmov(strmov(option,"--"),ptr,(uint) (end-ptr));
+ ptr=strnmov(strmov(option,"--"),ptr,(size_t) (end-ptr));
*ptr++= '=';
for ( ; value != value_end; value++)
@@ -886,15 +936,11 @@ void my_print_default_files(const char *conf_file)
fputs(conf_file,stdout);
else
{
- /*
- If default_directories is already initialized, use it. Otherwise,
- use a private MEM_ROOT.
- */
- const char **dirs = default_directories;
+ const char **dirs;
MEM_ROOT alloc;
init_alloc_root(&alloc,512,0);
- if (!dirs && (dirs= init_default_directories(&alloc)) == NULL)
+ if ((dirs= init_default_directories(&alloc)) == NULL)
{
fputs("Internal error initializing default directories list", stdout);
}
@@ -961,7 +1007,7 @@ void print_defaults(const char *conf_file, const char **groups)
static int add_directory(MEM_ROOT *alloc, const char *dir, const char **dirs)
{
char buf[FN_REFLEN];
- uint len;
+ size_t len;
char *p;
my_bool err __attribute__((unused));
@@ -986,23 +1032,23 @@ static int add_directory(MEM_ROOT *alloc, const char *dir, const char **dirs)
typedef UINT (WINAPI *GET_SYSTEM_WINDOWS_DIRECTORY)(LPSTR, UINT);
-static uint my_get_system_windows_directory(char *buffer, uint size)
+static size_t my_get_system_windows_directory(char *buffer, size_t size)
{
- uint count;
+ size_t count;
GET_SYSTEM_WINDOWS_DIRECTORY
func_ptr= (GET_SYSTEM_WINDOWS_DIRECTORY)
GetProcAddress(GetModuleHandle("kernel32.dll"),
"GetSystemWindowsDirectoryA");
if (func_ptr)
- return func_ptr(buffer, size);
+ return func_ptr(buffer, (uint) size);
/*
Windows NT 4.0 Terminal Server Edition:
To retrieve the shared Windows directory, call GetSystemDirectory and
trim the "System32" element from the end of the returned path.
*/
- count= GetSystemDirectory(buffer, size);
+ count= GetSystemDirectory(buffer, (uint) size);
if (count > 8 && stricmp(buffer+(count-8), "\\System32") == 0)
{
count-= 8;
@@ -1078,14 +1124,12 @@ static const char **init_default_directories(MEM_ROOT *alloc)
#else
errors += add_directory(alloc, "/etc/", dirs);
+ errors += add_directory(alloc, "/etc/mysql/", dirs);
-#if defined(__EMX__) || defined(OS2)
- if ((env= getenv("ETC")))
- errors += add_directory(alloc, env, dirs);
-#elif defined(DEFAULT_SYSCONFDIR)
+#if defined(DEFAULT_SYSCONFDIR)
if (DEFAULT_SYSCONFDIR[0])
errors += add_directory(alloc, DEFAULT_SYSCONFDIR, dirs);
-#endif /* __EMX__ || __OS2__ */
+#endif /* DEFAULT_SYSCONFDIR */
#endif
@@ -1095,8 +1139,7 @@ static const char **init_default_directories(MEM_ROOT *alloc)
/* Placeholder for --defaults-extra-file=<path> */
errors += add_directory(alloc, "", dirs);
-#if !defined(__WIN__) && !defined(__NETWARE__) && \
- !defined(__EMX__) && !defined(OS2)
+#if !defined(__WIN__) && !defined(__NETWARE__)
errors += add_directory(alloc, "~/", dirs);
#endif