2001-12-06 13:10:51 +01:00
/* 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 ,
2000-07-31 21:29:14 +02:00
but WITHOUT ANY WARRANTY ; without even the implied warranty of
2001-12-06 13:10:51 +01:00
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 */
2000-07-31 21:29:14 +02:00
/*
* * print_default . c :
* * Print all parameters in a default file that will be given to some program .
* *
* * Written by Monty
*/
2001-09-14 01:54:33 +02:00
# include <my_global.h>
2000-07-31 21:29:14 +02:00
# include <my_sys.h>
2002-05-22 22:54:24 +02:00
# include <my_getopt.h>
2000-07-31 21:29:14 +02:00
const char * config_file = " my " ; /* Default config file */
2003-08-27 18:22:14 +02:00
uint verbose = 0 , opt_defaults_file_used = 0 ;
2005-04-27 02:33:49 +02:00
const char * default_dbug_option = " d:t:o,/tmp/my_print_defaults.trace " ;
2000-07-31 21:29:14 +02:00
2002-05-22 22:54:24 +02:00
static struct my_option my_long_options [ ] =
2000-07-31 21:29:14 +02:00
{
2003-06-13 10:59:02 +02:00
{ " config-file " , ' c ' , " The config file to be used. " ,
2002-05-22 22:54:24 +02:00
( gptr * ) & config_file , ( gptr * ) & config_file , 0 , GET_STR , REQUIRED_ARG ,
0 , 0 , 0 , 0 , 0 , 0 } ,
2005-04-27 02:33:49 +02:00
# ifdef DBUG_OFF
{ " debug " , ' # ' , " This is a non-debug version. Catch this and exit " ,
0 , 0 , 0 , GET_DISABLED , OPT_ARG , 0 , 0 , 0 , 0 , 0 , 0 } ,
# else
{ " debug " , ' # ' , " Output debug log " , ( gptr * ) & default_dbug_option ,
( gptr * ) & default_dbug_option , 0 , GET_STR , OPT_ARG , 0 , 0 , 0 , 0 , 0 , 0 } ,
# endif
2003-06-13 10:59:02 +02:00
{ " defaults-file " , ' c ' , " Synonym for --config-file. " ,
2002-05-22 22:54:24 +02:00
( gptr * ) & config_file , ( gptr * ) & config_file , 0 , GET_STR , REQUIRED_ARG ,
0 , 0 , 0 , 0 , 0 , 0 } ,
2003-06-13 10:59:02 +02:00
{ " defaults-extra-file " , ' e ' ,
2002-05-22 22:54:24 +02:00
" Read this file after the global /etc config file and before the config file in the users home directory. " ,
( gptr * ) & defaults_extra_file , ( gptr * ) & defaults_extra_file , 0 , GET_STR ,
REQUIRED_ARG , 0 , 0 , 0 , 0 , 0 , 0 } ,
2003-06-13 10:59:02 +02:00
{ " extra-file " , ' e ' ,
" Synonym for --defaults-extra-file. " ,
2002-05-22 22:54:24 +02:00
( gptr * ) & defaults_extra_file , ( gptr * ) & defaults_extra_file , 0 , GET_STR ,
REQUIRED_ARG , 0 , 0 , 0 , 0 , 0 , 0 } ,
2003-06-13 10:59:02 +02:00
{ " no-defaults " , ' n ' , " Return an empty string (useful for scripts). " ,
2002-05-22 22:54:24 +02:00
0 , 0 , 0 , GET_NO_ARG , NO_ARG , 0 , 0 , 0 , 0 , 0 , 0 } ,
{ " help " , ' ? ' , " Display this help message and exit. " ,
0 , 0 , 0 , GET_NO_ARG , NO_ARG , 0 , 0 , 0 , 0 , 0 , 0 } ,
2003-08-27 18:22:14 +02:00
{ " verbose " , ' v ' , " Increase the output level " ,
2004-05-07 00:43:17 +02:00
0 , 0 , 0 , GET_NO_ARG , NO_ARG , 0 , 0 , 0 , 0 , 0 , 0 } ,
2002-05-22 22:54:24 +02:00
{ " version " , ' V ' , " Output version information and exit. " ,
0 , 0 , 0 , GET_NO_ARG , NO_ARG , 0 , 0 , 0 , 0 , 0 , 0 } ,
{ 0 , 0 , 0 , 0 , 0 , 0 , GET_NO_ARG , NO_ARG , 0 , 0 , 0 , 0 , 0 , 0 }
2000-07-31 21:29:14 +02:00
} ;
2005-02-08 18:49:40 +01:00
# include <help_start.h>
2000-07-31 21:29:14 +02:00
static void usage ( my_bool version )
{
2003-08-27 18:22:14 +02:00
printf ( " %s Ver 1.6 for %s at %s \n " , my_progname , SYSTEM_TYPE ,
2000-07-31 21:29:14 +02:00
MACHINE_TYPE ) ;
if ( version )
return ;
puts ( " This software comes with ABSOLUTELY NO WARRANTY. This is free software, \n and you are welcome to modify and redistribute it under the GPL license \n " ) ;
puts ( " Prints all arguments that is give to some program using the default files " ) ;
2002-05-22 22:54:24 +02:00
printf ( " Usage: %s [OPTIONS] groups \n " , my_progname ) ;
my_print_help ( my_long_options ) ;
2005-04-28 21:11:48 +02:00
my_print_default_files ( config_file ) ;
2002-05-22 22:54:24 +02:00
my_print_variables ( my_long_options ) ;
printf ( " \n Example usage: \n %s --config-file=my client mysql \n " , my_progname ) ;
2000-07-31 21:29:14 +02:00
}
2005-02-08 18:49:40 +01:00
# include <help_end.h>
2000-07-31 21:29:14 +02:00
2002-05-22 22:54:24 +02:00
static my_bool
get_one_option ( int optid , const struct my_option * opt __attribute__ ( ( unused ) ) ,
char * argument __attribute__ ( ( unused ) ) )
{
switch ( optid ) {
2003-08-27 18:22:14 +02:00
case ' c ' :
opt_defaults_file_used = 1 ;
break ;
2000-07-31 21:29:14 +02:00
case ' n ' :
2002-05-22 22:54:24 +02:00
exit ( 0 ) ;
2000-07-31 21:29:14 +02:00
case ' I ' :
case ' ? ' :
2002-05-22 22:54:24 +02:00
usage ( 0 ) ;
exit ( 0 ) ;
2003-08-27 18:22:14 +02:00
case ' v ' :
verbose + + ;
break ;
2000-07-31 21:29:14 +02:00
case ' V ' :
2002-05-22 22:54:24 +02:00
usage ( 1 ) ;
exit ( 0 ) ;
2005-04-27 02:33:49 +02:00
case ' # ' :
DBUG_PUSH ( argument ? argument : default_dbug_option ) ;
break ;
2002-05-22 22:54:24 +02:00
}
return 0 ;
}
static int get_options ( int * argc , char * * * argv )
{
int ho_error ;
2004-08-31 18:27:58 +02:00
if ( ( ho_error = handle_options ( argc , argv , my_long_options , get_one_option ) ) )
2002-05-29 14:07:30 +02:00
exit ( ho_error ) ;
2000-07-31 21:29:14 +02:00
if ( * argc < 1 )
{
usage ( 0 ) ;
return 1 ;
}
return 0 ;
}
int main ( int argc , char * * argv )
{
2003-08-27 18:22:14 +02:00
int count , error ;
2005-04-27 02:33:49 +02:00
char * * load_default_groups , * tmp_arguments [ 3 ] ,
2000-07-31 21:29:14 +02:00
* * argument , * * arguments ;
2005-02-19 18:51:47 +01:00
char * defaults , * extra_defaults ;
2000-07-31 21:29:14 +02:00
MY_INIT ( argv [ 0 ] ) ;
2005-02-19 18:51:47 +01:00
get_defaults_files ( argc , argv , & defaults , & extra_defaults ) ;
2000-07-31 21:29:14 +02:00
/*
* * Check out the args
*/
if ( ! ( load_default_groups = ( char * * ) my_malloc ( ( argc + 2 ) * sizeof ( char * ) ,
2000-10-06 20:15:03 +02:00
MYF ( MY_WME ) ) ) )
2000-07-31 21:29:14 +02:00
exit ( 1 ) ;
2005-02-19 18:51:47 +01:00
if ( get_options ( & argc , & argv ) )
exit ( 1 ) ;
2000-07-31 21:29:14 +02:00
for ( count = 0 ; * argv ; argv + + , count + + )
load_default_groups [ count ] = * argv ;
load_default_groups [ count ] = 0 ;
2005-02-19 18:51:47 +01:00
count = 0 ;
2000-07-31 21:29:14 +02:00
arguments = tmp_arguments ;
2005-02-19 18:51:47 +01:00
arguments [ count + + ] = my_progname ;
if ( extra_defaults )
arguments [ count + + ] = extra_defaults ;
if ( defaults )
arguments [ count + + ] = defaults ;
arguments [ count ] = 0 ;
2003-08-27 18:22:14 +02:00
if ( ( error = load_defaults ( config_file , ( const char * * ) load_default_groups ,
2003-08-28 05:08:17 +02:00
& count , & arguments ) ) )
2003-08-27 18:22:14 +02:00
{
if ( verbose & & opt_defaults_file_used )
{
2003-08-28 05:08:17 +02:00
if ( error = = 1 )
fprintf ( stderr , " WARNING: Defaults file '%s' not found! \n " ,
2003-08-27 18:22:14 +02:00
config_file ) ;
2003-08-28 20:27:25 +02:00
/* This error is not available now. For the future */
2003-08-28 05:08:17 +02:00
if ( error = = 2 )
fprintf ( stderr , " WARNING: Defaults file '%s' is not a regular file! \n " ,
2003-08-27 18:22:14 +02:00
config_file ) ;
}
2003-08-28 05:08:17 +02:00
error = 2 ;
2003-08-27 18:22:14 +02:00
}
2000-07-31 21:29:14 +02:00
for ( argument = arguments + 1 ; * argument ; argument + + )
puts ( * argument ) ;
my_free ( ( char * ) load_default_groups , MYF ( 0 ) ) ;
free_defaults ( arguments ) ;
2003-08-28 05:08:17 +02:00
exit ( error ) ;
2000-07-31 21:29:14 +02:00
}