mariadb/mysys/default.c
unknown a821703912 Merge with 4.0
BitKeeper/etc/ignore:
  auto-union
BitKeeper/etc/logging_ok:
  auto-union
BitKeeper/deleted/.del-compile-netware-standard:
  Delete: netware/BUILD/compile-netware-standard
BitKeeper/deleted/.del-mwenv:
  Delete: netware/BUILD/mwenv
BitKeeper/deleted/.del-nwbootstrap:
  Delete: netware/BUILD/nwbootstrap
BitKeeper/deleted/.del-compile-AUTOTOOLS:
  Delete: netware/BUILD/compile-AUTOTOOLS
BitKeeper/deleted/.del-compile-linux-tools:
  Delete: netware/BUILD/compile-linux-tools
BitKeeper/deleted/.del-compile-netware-END:
  Delete: netware/BUILD/compile-netware-END
BitKeeper/deleted/.del-compile-netware-START:
  Delete: netware/BUILD/compile-netware-START
BitKeeper/deleted/.del-compile-netware-all:
  Delete: netware/BUILD/compile-netware-all
BitKeeper/deleted/.del-compile-netware-debug:
  Delete: netware/BUILD/compile-netware-debug
BitKeeper/deleted/.del-mwasmnlm~bc5746809d67feb5:
  Auto merged
BitKeeper/deleted/.del-mwenv~35c8b56062f4b6aa:
  Auto merged
BitKeeper/deleted/.del-mwccnlm~be63afd25a14c3f:
  Auto merged
BitKeeper/deleted/.del-mwldnlm~efb26c57cba3c980:
  Auto merged
BitKeeper/deleted/.del-netware.patch~f70a3a965f54d9ee:
  Auto merged
Docs/internals.texi:
  Auto merged
VC++Files/bdb/bdb.dsp:
  Auto merged
VC++Files/bdb/build_win32/Berkeley_DB.dsw:
  Auto merged
VC++Files/bdb/build_win32/db_archive.dsp:
  Auto merged
VC++Files/bdb/build_win32/db_buildall.dsp:
  Auto merged
VC++Files/bdb/build_win32/db_checkpoint.dsp:
  Auto merged
VC++Files/bdb/build_win32/db_deadlock.dsp:
  Auto merged
VC++Files/bdb/build_win32/db_dll.dsp:
  Auto merged
VC++Files/bdb/build_win32/db_dump.dsp:
  Auto merged
VC++Files/bdb/build_win32/db_java.dsp:
  Auto merged
VC++Files/bdb/build_win32/db_load.dsp:
  Auto merged
VC++Files/bdb/build_win32/db_printlog.dsp:
  Auto merged
VC++Files/bdb/build_win32/db_recover.dsp:
  Auto merged
VC++Files/bdb/build_win32/db_stat.dsp:
  Auto merged
VC++Files/bdb/build_win32/db_static.dsp:
  Auto merged
VC++Files/bdb/build_win32/db_static1.dsp:
  Auto merged
VC++Files/bdb/build_win32/db_tcl.dsp:
  Auto merged
VC++Files/bdb/build_win32/db_test.dsp:
  Auto merged
VC++Files/bdb/build_win32/db_upgrade.dsp:
  Auto merged
VC++Files/bdb/build_win32/db_verify.dsp:
  Auto merged
VC++Files/bdb/build_win32/ex_access.dsp:
  Auto merged
VC++Files/bdb/build_win32/ex_btrec.dsp:
  Auto merged
VC++Files/bdb/build_win32/ex_env.dsp:
  Auto merged
VC++Files/bdb/build_win32/ex_lock.dsp:
  Auto merged
VC++Files/bdb/build_win32/ex_mpool.dsp:
  Auto merged
VC++Files/bdb/build_win32/ex_tpcb.dsp:
  Auto merged
VC++Files/bdb/build_win32/excxx_access.dsp:
  Auto merged
VC++Files/bdb/build_win32/excxx_btrec.dsp:
  Auto merged
VC++Files/bdb/build_win32/excxx_env.dsp:
  Auto merged
VC++Files/bdb/build_win32/excxx_lock.dsp:
  Auto merged
VC++Files/bdb/build_win32/excxx_mpool.dsp:
  Auto merged
VC++Files/bdb/build_win32/excxx_tpcb.dsp:
  Auto merged
VC++Files/client/mysql.dsp:
  Auto merged
VC++Files/client/mysqladmin.dsp:
  Auto merged
VC++Files/client/mysqlcheck.dsp:
  Auto merged
VC++Files/client/mysqlclient.dsp:
  Auto merged
VC++Files/client/mysqlclient.dsw:
  Auto merged
VC++Files/client/mysqldump.dsp:
  Auto merged
VC++Files/client/mysqlimport.dsp:
  Auto merged
VC++Files/client/mysqlshow.dsp:
  Auto merged
VC++Files/comp_err/comp_err.dsp:
  Auto merged
VC++Files/contrib/asm386/zlibvc.dsp:
  Auto merged
VC++Files/contrib/asm386/zlibvc.dsw:
  Auto merged
VC++Files/contrib/minizip/zlibvc.dsp:
  Auto merged
VC++Files/contrib/minizip/zlibvc.dsw:
  Auto merged
VC++Files/dbug/dbug.dsp:
  Auto merged
VC++Files/dbug/dbug.dsw:
  Auto merged
VC++Files/heap/heap.dsp:
  Auto merged
VC++Files/innobase/innobase.dsp:
  Auto merged
VC++Files/isam/isam.dsp:
  Auto merged
VC++Files/isam/isam.dsw:
  Auto merged
VC++Files/isamchk/isamchk.dsp:
  Auto merged
VC++Files/libmysql/libmysql.dsp:
  Auto merged
VC++Files/libmysql/libmysql.dsw:
  Auto merged
VC++Files/libmysqld/examples/test_libmysqld.dsp:
  Auto merged
VC++Files/libmysqld/libmysqld.dsp:
  Auto merged
VC++Files/libmysqltest/myTest.dsp:
  Auto merged
VC++Files/libmysqltest/mytest.dsw:
  Auto merged
VC++Files/merge/merge.dsp:
  Auto merged
VC++Files/merge/merge.dsw:
  Auto merged
VC++Files/my_print_defaults/my_print_defaults.dsp:
  Auto merged
VC++Files/myisam/myisam.dsp:
  Auto merged
VC++Files/myisamchk/myisamchk.dsp:
  Auto merged
VC++Files/myisamlog/myisamlog.dsp:
  Auto merged
VC++Files/myisammrg/myisammrg.dsp:
  Auto merged
VC++Files/mysql.dsp:
  Auto merged
VC++Files/mysql.dsw:
  Auto merged
VC++Files/myisampack/myisampack.dsp:
  Auto merged
VC++Files/mysqlbinlog/mysqlbinlog.dsp:
  Auto merged
VC++Files/mysqlcheck/mysqlcheck.dsp:
  Auto merged
VC++Files/mysqldemb/mysqldemb.dsp:
  Auto merged
VC++Files/mysqlmanager/MySqlManager.dsp:
  Auto merged
VC++Files/mysqlmanager/mysqlmanager.dsw:
  Auto merged
VC++Files/mysqlserver/mysqlserver.dsp:
  Auto merged
VC++Files/mysqlshutdown/myshutdown.dsp:
  Auto merged
VC++Files/mysqlshutdown/mysqlshutdown.dsp:
  Auto merged
VC++Files/mysqlwatch/mysqlwatch.dsp:
  Auto merged
VC++Files/mysys/mysys.dsp:
  Auto merged
VC++Files/mysys/mysys.dsw:
  Auto merged
VC++Files/pack_isam/pack_isam.dsp:
  Auto merged
VC++Files/perror/perror.dsp:
  Auto merged
VC++Files/regex/regex.dsp:
  Auto merged
VC++Files/regex/regex.dsw:
  Auto merged
VC++Files/replace/replace.dsp:
  Auto merged
VC++Files/sql/mysqld.dsw:
  Auto merged
VC++Files/sql/mysqldmax.dsp:
  Auto merged
VC++Files/sql/old/mysqld.dsw:
  Auto merged
VC++Files/strings/MASM6x/strings.dsp:
  Auto merged
VC++Files/strings/MASM6x/strings.dsw:
  Auto merged
VC++Files/strings/backup/strings.dsp:
  Auto merged
VC++Files/strings/backup/strings.dsw:
  Auto merged
VC++Files/strings/noMASM/strings.dsp:
  Auto merged
VC++Files/strings/noMASM/strings.dsw:
  Auto merged
VC++Files/strings/strings.dsw:
  Auto merged
VC++Files/test1/test1.dsp:
  Auto merged
VC++Files/thr_insert_test/thr_insert_test.dsp:
  Auto merged
VC++Files/thr_test/thr_test.dsp:
  Auto merged
VC++Files/vio/vio.dsp:
  Auto merged
VC++Files/zlib/zlib.dsp:
  Auto merged
client/mysqlbinlog.cc:
  Auto merged
client/mysqlshow.c:
  Auto merged
include/my_global.h:
  Auto merged
include/my_sys.h:
  Auto merged
include/myisam.h:
  Auto merged
include/thr_lock.h:
  Auto merged
include/violite.h:
  Auto merged
innobase/buf/buf0buf.c:
  Auto merged
innobase/os/os0file.c:
  Auto merged
innobase/row/row0sel.c:
  Auto merged
innobase/srv/srv0start.c:
  Auto merged
innobase/trx/trx0sys.c:
  Auto merged
libmysqld/lib_vio.c:
  Auto merged
myisam/mi_create.c:
  Auto merged
mysql-test/Makefile.am:
  Auto merged
mysql-test/r/create.result:
  Auto merged
mysql-test/r/group_by.result:
  Auto merged
mysql-test/r/innodb.result:
  Auto merged
mysql-test/r/join.result:
  Auto merged
mysql-test/r/rpl000001.result:
  Auto merged
mysql-test/r/select.result:
  Auto merged
mysql-test/t/auto_increment.test:
  Auto merged
mysql-test/t/create.test:
  Auto merged
mysql-test/t/func_like.test:
  Auto merged
mysql-test/t/group_by.test:
  Auto merged
mysql-test/t/innodb.test:
  Auto merged
mysql-test/t/join.test:
  Auto merged
mysql-test/t/type_datetime.test:
  Auto merged
mysql-test/t/type_timestamp.test:
  Auto merged
mysys/default.c:
  Auto merged
mysys/thr_lock.c:
  Auto merged
scripts/make_binary_distribution.sh:
  Auto merged
scripts/mysqld_safe.sh:
  Auto merged
sql/filesort.cc:
  Auto merged
sql/ha_innodb.h:
  Auto merged
sql/ha_myisam.cc:
  Auto merged
sql/handler.cc:
  Auto merged
sql/handler.h:
  Auto merged
sql/item.cc:
  Auto merged
sql/item_cmpfunc.cc:
  Auto merged
sql/item_func.cc:
  Auto merged
sql/lock.cc:
  Auto merged
sql/log.cc:
  Auto merged
sql/log_event.h:
  Auto merged
sql/mf_iocache.cc:
  Auto merged
sql/mysql_priv.h:
  Auto merged
sql/net_serv.cc:
  Auto merged
sql/opt_range.cc:
  Auto merged
sql/repl_failsafe.cc:
  Auto merged
sql/repl_failsafe.h:
  Auto merged
sql/slave.cc:
  Auto merged
sql/slave.h:
  Auto merged
sql/sql_analyse.cc:
  Auto merged
sql/sql_base.cc:
  Auto merged
sql-bench/crash-me.sh:
  Auto merged
sql/share/polish/errmsg.txt:
  Auto merged
sql/sql_class.h:
  Auto merged
sql/sql_rename.cc:
  Auto merged
sql/sql_repl.cc:
  Auto merged
sql/sql_repl.h:
  Auto merged
sql/sql_update.cc:
  Auto merged
sql/stacktrace.c:
  Auto merged
sql/table.cc:
  Auto merged
sql/unireg.h:
  Auto merged
strings/ctype-tis620.c:
  Auto merged
strings/t_ctype.h:
  Auto merged
support-files/mysql.spec.sh:
  Auto merged
tests/grant.res:
  Auto merged
vio/viosocket.c:
  Auto merged
2003-03-16 19:17:54 +02:00

415 lines
11 KiB
C

/* Copyright (C) 2000 MySQL AB
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
/****************************************************************************
** Add all options from files named "group".cnf from the default_directories
** before the command line arguments.
** On Windows defaults will also search in the Windows directory for a file
** called 'group'.ini
** As long as the program uses the last argument for conflicting
** options one only have to add a call to "load_defaults" to enable
** use of default values.
** pre- and end 'blank space' are removed from options and values. The
** following escape sequences are recognized in values: \b \t \n \r \\
**
** The following arguments are handled automaticly; If used, they must be
** first argument on the command line!
** --no-defaults ; no options are read.
** --defaults-file=full-path-to-default-file ; Only this file will be read.
** --defaults-extra-file=full-path-to-default-file ; Read this file before ~/
** --print-defaults ; Print the modified command line and exit
****************************************************************************/
#undef SAFEMALLOC /* safe_malloc is not yet initailized */
#include "mysys_priv.h"
#include "m_string.h"
#include "m_ctype.h"
#include <my_dir.h>
char *defaults_extra_file=0;
/* Which directories are searched for options (and in which order) */
const char *default_directories[]= {
#ifdef __WIN__
"C:/",
#elif defined(__NETWARE__)
"sys:/etc/",
#else
"/etc/",
#endif
#ifdef DATADIR
DATADIR,
#endif
"", /* Place for defaults_extra_dir */
#if !defined(__WIN__) && !defined(__NETWARE__)
"~/",
#endif
NullS,
};
#define default_ext ".cnf" /* extension for config file */
#ifdef __WIN__
#include <winbase.h>
#define windows_ext ".ini"
#endif
static my_bool search_default_file(DYNAMIC_ARRAY *args,MEM_ROOT *alloc,
const char *dir, const char *config_file,
const char *ext, TYPELIB *group);
void load_defaults(const char *conf_file, const char **groups,
int *argc, char ***argv)
{
DYNAMIC_ARRAY args;
const char **dirs, *forced_default_file;
TYPELIB group;
my_bool found_print_defaults=0;
uint args_used=0;
MEM_ROOT alloc;
char *ptr,**res;
DBUG_ENTER("load_defaults");
init_alloc_root(&alloc,512,0);
if (*argc >= 2 && !strcmp(argv[0][1],"--no-defaults"))
{
/* remove the --no-defaults argument and return only the other arguments */
uint i;
if (!(ptr=(char*) alloc_root(&alloc,sizeof(alloc)+
(*argc + 1)*sizeof(char*))))
goto err;
res= (char**) (ptr+sizeof(alloc));
res[0]= **argv; /* Copy program name */
for (i=2 ; i < (uint) *argc ; i++)
res[i-1]=argv[0][i];
res[i-1]=0; /* End pointer */
(*argc)--;
*argv=res;
*(MEM_ROOT*) ptr= alloc; /* Save alloc root for free */
DBUG_VOID_RETURN;
}
/* Check if we want to force the use a specific default file */
forced_default_file=0;
if (*argc >= 2)
{
if (is_prefix(argv[0][1],"--defaults-file="))
{
forced_default_file=strchr(argv[0][1],'=')+1;
args_used++;
}
else if (is_prefix(argv[0][1],"--defaults-extra-file="))
{
defaults_extra_file=strchr(argv[0][1],'=')+1;
args_used++;
}
}
group.count=0;
group.name= "defaults";
group.type_names= groups;
for (; *groups ; groups++)
group.count++;
if (my_init_dynamic_array(&args, sizeof(char*),*argc, 32))
goto err;
if (forced_default_file)
{
if (search_default_file(&args, &alloc, "", forced_default_file, "",
&group))
goto err;
}
else if (dirname_length(conf_file))
{
if (search_default_file(&args, &alloc, NullS, conf_file, default_ext,
&group))
goto err;
}
else
{
#ifdef __WIN__
char system_dir[FN_REFLEN];
GetWindowsDirectory(system_dir,sizeof(system_dir));
if (search_default_file(&args, &alloc, system_dir, conf_file, windows_ext,
&group))
goto err;
#endif
#if defined(__EMX__) || defined(OS2)
if (getenv("ETC") &&
search_default_file(&args, &alloc, getenv("ETC"), conf_file,
default_ext, &group))
goto err;
#endif
for (dirs=default_directories ; *dirs; dirs++)
{
int error=0;
if (**dirs)
error=search_default_file(&args, &alloc, *dirs, conf_file,
default_ext, &group);
else if (defaults_extra_file)
error=search_default_file(&args, &alloc, NullS, defaults_extra_file,
default_ext, &group);
if (error)
goto err;
}
}
if (!(ptr=(char*) alloc_root(&alloc,sizeof(alloc)+
(args.elements + *argc +1) *sizeof(char*))))
goto err;
res= (char**) (ptr+sizeof(alloc));
/* copy name + found arguments + command line arguments to new array */
res[0]=argv[0][0];
memcpy((gptr) (res+1), args.buffer, args.elements*sizeof(char*));
/* Skipp --defaults-file and --defaults-extra-file */
(*argc)-= args_used;
(*argv)+= args_used;
/* Check if we wan't to see the new argument list */
if (*argc >= 2 && !strcmp(argv[0][1],"--print-defaults"))
{
found_print_defaults=1;
--*argc; ++*argv; /* skipp argument */
}
memcpy((gptr) (res+1+args.elements), (char*) ((*argv)+1),
(*argc-1)*sizeof(char*));
res[args.elements+ *argc]=0; /* last null */
(*argc)+=args.elements;
*argv= (char**) res;
*(MEM_ROOT*) ptr= alloc; /* Save alloc root for free */
delete_dynamic(&args);
if (found_print_defaults)
{
int i;
printf("%s would have been started with the following arguments:\n",
**argv);
for (i=1 ; i < *argc ; i++)
printf("%s ", (*argv)[i]);
puts("");
exit(1);
}
DBUG_VOID_RETURN;
err:
fprintf(stderr,"Program aborted\n");
exit(1);
}
void free_defaults(char **argv)
{
MEM_ROOT ptr;
memcpy_fixed((char*) &ptr,(char *) argv - sizeof(ptr), sizeof(ptr));
free_root(&ptr,MYF(0));
}
static my_bool search_default_file(DYNAMIC_ARRAY *args, MEM_ROOT *alloc,
const char *dir, const char *config_file,
const char *ext, TYPELIB *group)
{
char name[FN_REFLEN+10],buff[4096],*ptr,*end,*value,*tmp;
FILE *fp;
uint line=0;
my_bool read_values=0,found_group=0;
if ((dir ? strlen(dir) : 0 )+strlen(config_file) >= FN_REFLEN-3)
return 0; /* Ignore wrong paths */
if (dir)
{
end=convert_dirname(name, dir, NullS);
if (dir[0] == FN_HOMELIB) /* Add . to filenames in home */
*end++='.';
strxmov(end,config_file,ext,NullS);
}
else
{
strmov(name,config_file);
}
fn_format(name,name,"","",4);
#if !defined(__WIN__) && !defined(OS2)
{
MY_STAT stat_info;
if (!my_stat(name,&stat_info,MYF(0)))
return 0;
if (stat_info.st_mode & S_IWOTH) /* ignore world-writeable files */
{
fprintf(stderr, "warning: World-writeable config file %s is ignored\n",
name);
return 0;
}
}
#endif
if (!(fp = my_fopen(fn_format(name,name,"","",4),O_RDONLY,MYF(0))))
return 0; /* Ignore wrong files */
while (fgets(buff,sizeof(buff)-1,fp))
{
line++;
/* Ignore comment and empty lines */
for (ptr=buff ; my_isspace(&my_charset_latin1,*ptr) ; ptr++ ) ;
if (*ptr == '#' || *ptr == ';' || !*ptr)
continue;
if (*ptr == '[') /* Group name */
{
found_group=1;
if (!(end=(char *) strchr(++ptr,']')))
{
fprintf(stderr,
"error: Wrong group definition in config file: %s at line %d\n",
name,line);
goto err;
}
for ( ; my_isspace(&my_charset_latin1,end[-1]) ; end--) ;/* Remove end space */
end[0]=0;
read_values=find_type(ptr,group,3) > 0;
continue;
}
if (!found_group)
{
fprintf(stderr,
"error: Found option without preceding group in config file: %s at line: %d\n",
name,line);
goto err;
}
if (!read_values)
continue;
if (!(end=value=strchr(ptr,'=')))
end=strend(ptr); /* Option without argument */
for ( ; my_isspace(&my_charset_latin1,end[-1]) ; end--) ;
if (!value)
{
if (!(tmp=alloc_root(alloc,(uint) (end-ptr)+3)))
goto err;
strmake(strmov(tmp,"--"),ptr,(uint) (end-ptr));
if (insert_dynamic(args,(gptr) &tmp))
goto err;
}
else
{
/* Remove pre- and end space */
char *value_end;
for (value++ ; my_isspace(&my_charset_latin1,*value); value++) ;
value_end=strend(value);
for ( ; my_isspace(&my_charset_latin1,value_end[-1]) ; value_end--) ;
if (value_end < value) /* Empty string */
value_end=value;
if (!(tmp=alloc_root(alloc,(uint) (end-ptr)+3 +
(uint) (value_end-value)+1)))
goto err;
if (insert_dynamic(args,(gptr) &tmp))
goto err;
ptr=strnmov(strmov(tmp,"--"),ptr,(uint) (end-ptr));
*ptr++= '=';
for ( ; value != value_end; value++)
{
if (*value == '\\' && value != value_end-1)
{
switch(*++value) {
case 'n':
*ptr++='\n';
break;
case 't':
*ptr++= '\t';
break;
case 'r':
*ptr++ = '\r';
break;
case 'b':
*ptr++ = '\b';
break;
case 's':
*ptr++= ' '; /* space */
break;
case '\\':
*ptr++= '\\';
break;
default: /* Unknown; Keep '\' */
*ptr++= '\\';
*ptr++= *value;
break;
}
}
else
*ptr++= *value;
}
*ptr=0;
}
}
my_fclose(fp,MYF(0));
return(0);
err:
my_fclose(fp,MYF(0));
return 1;
}
void print_defaults(const char *conf_file, const char **groups)
{
#ifdef __WIN__
bool have_ext=fn_ext(conf_file)[0] != 0;
#endif
char name[FN_REFLEN];
const char **dirs;
puts("\nDefault options are read from the following files in the given order:");
if (dirname_length(conf_file))
fputs(conf_file,stdout);
else
{
#ifdef __WIN__
GetWindowsDirectory(name,sizeof(name));
printf("%s\\%s%s ",name,conf_file,have_ext ? "" : windows_ext);
#endif
#if defined(__EMX__) || defined(OS2)
if (getenv("ETC"))
printf("%s\\%s%s ", getenv("ETC"), conf_file, default_ext);
#endif
for (dirs=default_directories ; *dirs; dirs++)
{
const char *pos;
char *end;
if (**dirs)
pos= *dirs;
else if (defaults_extra_file)
pos= defaults_extra_file;
else
continue;
end=convert_dirname(name, pos, NullS);
if (name[0] == FN_HOMELIB) /* Add . to filenames in home */
*end++='.';
strxmov(end,conf_file,default_ext," ",NullS);
fputs(name,stdout);
}
puts("");
}
fputs("The following groups are read:",stdout);
for ( ; *groups ; groups++)
{
fputc(' ',stdout);
fputs(*groups,stdout);
}
puts("\nThe following options may be given as the first argument:\n\
--print-defaults Print the program argument list and exit\n\
--no-defaults Don't read default options from any options file\n\
--defaults-file=# Only read default options from the given file #\n\
--defaults-extra-file=# Read this file after the global files are read");
}