1
by brian
clean slate |
1 |
/* Copyright (C) 2000 MySQL AB
|
2 |
||
3 |
This program is free software; you can redistribute it and/or modify
|
|
4 |
it under the terms of the GNU General Public License as published by
|
|
5 |
the Free Software Foundation; version 2 of the License.
|
|
6 |
||
7 |
This program is distributed in the hope that it will be useful,
|
|
8 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
9 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
10 |
GNU General Public License for more details.
|
|
11 |
||
12 |
You should have received a copy of the GNU General Public License
|
|
13 |
along with this program; if not, write to the Free Software
|
|
14 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
|
|
15 |
||
16 |
/*
|
|
17 |
** print_default.c:
|
|
18 |
** Print all parameters in a default file that will be given to some program.
|
|
19 |
**
|
|
20 |
** Written by Monty
|
|
21 |
*/
|
|
22 |
||
23 |
#include <my_global.h> |
|
24 |
#include <my_sys.h> |
|
25 |
#include <m_string.h> |
|
26 |
#include <my_getopt.h> |
|
27 |
||
28 |
||
29 |
const char *config_file="my"; /* Default config file */ |
|
30 |
uint verbose= 0, opt_defaults_file_used= 0; |
|
31 |
const char *default_dbug_option="d:t:o,/tmp/my_print_defaults.trace"; |
|
32 |
||
33 |
static struct my_option my_long_options[] = |
|
34 |
{
|
|
35 |
/*
|
|
36 |
NB: --config-file is troublesome, because get_defaults_options() doesn't
|
|
37 |
know about it, but we pretend --config-file is like --defaults-file. In
|
|
38 |
fact they behave differently: see the comments at the top of
|
|
39 |
mysys/default.c for how --defaults-file should behave.
|
|
40 |
||
41 |
This --config-file option behaves as:
|
|
42 |
- If it has a directory name part (absolute or relative), then only this
|
|
43 |
file is read; no error is given if the file doesn't exist
|
|
44 |
- If the file has no directory name part, the standard locations are
|
|
45 |
searched for a file of this name (and standard filename extensions are
|
|
46 |
added if the file has no extension)
|
|
47 |
*/
|
|
48 |
{"config-file", 'c', "Deprecated, please use --defaults-file instead. Name of config file to read; if no extension is given, default extension (e.g., .ini or .cnf) will be added", |
|
49 |
(uchar**) &config_file, (uchar**) &config_file, 0, GET_STR, REQUIRED_ARG, |
|
50 |
0, 0, 0, 0, 0, 0}, |
|
51 |
#ifdef DBUG_OFF
|
|
52 |
{"debug", '#', "This is a non-debug version. Catch this and exit", |
|
53 |
0,0, 0, GET_DISABLED, OPT_ARG, 0, 0, 0, 0, 0, 0}, |
|
54 |
#else
|
|
55 |
{"debug", '#', "Output debug log", (uchar**) &default_dbug_option, |
|
56 |
(uchar**) &default_dbug_option, 0, GET_STR, OPT_ARG, 0, 0, 0, 0, 0, 0}, |
|
57 |
#endif
|
|
58 |
{"defaults-file", 'c', "Like --config-file, except: if first option, then read this file only, do not read global or per-user config files; should be the first option", |
|
59 |
(uchar**) &config_file, (uchar**) &config_file, 0, GET_STR, REQUIRED_ARG, |
|
60 |
0, 0, 0, 0, 0, 0}, |
|
61 |
{"defaults-extra-file", 'e', |
|
62 |
"Read this file after the global config file and before the config file in the users home directory; should be the first option", |
|
63 |
(uchar**) &my_defaults_extra_file, (uchar**) &my_defaults_extra_file, 0, |
|
64 |
GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0}, |
|
65 |
{"defaults-group-suffix", 'g', |
|
66 |
"In addition to the given groups, read also groups with this suffix", |
|
67 |
(uchar**) &my_defaults_group_suffix, (uchar**) &my_defaults_group_suffix, |
|
68 |
0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0}, |
|
69 |
{"extra-file", 'e', |
|
70 |
"Deprecated. Synonym for --defaults-extra-file.", |
|
71 |
(uchar**) &my_defaults_extra_file, |
|
72 |
(uchar**) &my_defaults_extra_file, 0, GET_STR, |
|
73 |
REQUIRED_ARG, 0, 0, 0, 0, 0, 0}, |
|
74 |
{"no-defaults", 'n', "Return an empty string (useful for scripts).", |
|
75 |
0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0}, |
|
76 |
{"help", '?', "Display this help message and exit.", |
|
77 |
0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0}, |
|
78 |
{"verbose", 'v', "Increase the output level", |
|
79 |
0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0}, |
|
80 |
{"version", 'V', "Output version information and exit.", |
|
81 |
0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0}, |
|
82 |
{0, 0, 0, 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0} |
|
83 |
};
|
|
84 |
||
85 |
||
86 |
||
87 |
#include <help_start.h> |
|
88 |
||
89 |
static void usage(my_bool version) |
|
90 |
{
|
|
91 |
printf("%s Ver 1.6 for %s at %s\n",my_progname,SYSTEM_TYPE, |
|
92 |
MACHINE_TYPE); |
|
93 |
if (version) |
|
94 |
return; |
|
95 |
puts("This software comes with ABSOLUTELY NO WARRANTY. This is free software,\nand you are welcome to modify and redistribute it under the GPL license\n"); |
|
96 |
puts("Prints all arguments that is give to some program using the default files"); |
|
97 |
printf("Usage: %s [OPTIONS] groups\n", my_progname); |
|
98 |
my_print_help(my_long_options); |
|
99 |
my_print_default_files(config_file); |
|
100 |
my_print_variables(my_long_options); |
|
101 |
printf("\nExample usage:\n%s --defaults-file=example.cnf client mysql\n", my_progname); |
|
102 |
}
|
|
103 |
||
104 |
#include <help_end.h> |
|
105 |
||
106 |
||
107 |
static my_bool |
|
108 |
get_one_option(int optid, const struct my_option *opt __attribute__((unused)), |
|
109 |
char *argument __attribute__((unused))) |
|
110 |
{
|
|
111 |
switch (optid) { |
|
112 |
case 'c': |
|
113 |
opt_defaults_file_used= 1; |
|
114 |
break; |
|
115 |
case 'n': |
|
116 |
exit(0); |
|
117 |
case 'I': |
|
118 |
case '?': |
|
119 |
usage(0); |
|
120 |
exit(0); |
|
121 |
case 'v': |
|
122 |
verbose++; |
|
123 |
break; |
|
124 |
case 'V': |
|
125 |
usage(1); |
|
126 |
exit(0); |
|
127 |
case '#': |
|
128 |
DBUG_PUSH(argument ? argument : default_dbug_option); |
|
129 |
break; |
|
130 |
}
|
|
131 |
return 0; |
|
132 |
}
|
|
133 |
||
134 |
||
135 |
static int get_options(int *argc,char ***argv) |
|
136 |
{
|
|
137 |
int ho_error; |
|
138 |
||
139 |
if ((ho_error=handle_options(argc, argv, my_long_options, get_one_option))) |
|
140 |
exit(ho_error); |
|
141 |
||
142 |
if (*argc < 1) |
|
143 |
{
|
|
144 |
usage(0); |
|
145 |
return 1; |
|
146 |
}
|
|
147 |
return 0; |
|
148 |
}
|
|
149 |
||
150 |
||
151 |
int main(int argc, char **argv) |
|
152 |
{
|
|
153 |
int count, error, args_used; |
|
154 |
char **load_default_groups, *tmp_arguments[6]; |
|
155 |
char **argument, **arguments, **org_argv; |
|
156 |
char *defaults, *extra_defaults, *group_suffix; |
|
157 |
MY_INIT(argv[0]); |
|
158 |
||
159 |
org_argv= argv; |
|
160 |
args_used= get_defaults_options(argc, argv, &defaults, &extra_defaults, |
|
161 |
&group_suffix); |
|
162 |
||
163 |
/* Copy defaults-xxx arguments & program name */
|
|
164 |
count=args_used+1; |
|
165 |
arguments= tmp_arguments; |
|
166 |
memcpy((char*) arguments, (char*) org_argv, count * sizeof(*org_argv)); |
|
167 |
arguments[count]= 0; |
|
168 |
||
169 |
/* Check out the args */
|
|
170 |
if (!(load_default_groups=(char**) my_malloc((argc+1)*sizeof(char*), |
|
171 |
MYF(MY_WME)))) |
|
172 |
exit(1); |
|
173 |
if (get_options(&argc,&argv)) |
|
174 |
exit(1); |
|
175 |
memcpy((char*) load_default_groups, (char*) argv, (argc + 1) * sizeof(*argv)); |
|
176 |
||
177 |
if ((error= load_defaults(config_file, (const char **) load_default_groups, |
|
178 |
&count, &arguments))) |
|
179 |
{
|
|
180 |
if (verbose && opt_defaults_file_used) |
|
181 |
{
|
|
182 |
if (error == 1) |
|
183 |
fprintf(stderr, "WARNING: Defaults file '%s' not found!\n", |
|
184 |
config_file); |
|
185 |
/* This error is not available now. For the future */
|
|
186 |
if (error == 2) |
|
187 |
fprintf(stderr, "WARNING: Defaults file '%s' is not a regular file!\n", |
|
188 |
config_file); |
|
189 |
}
|
|
190 |
error= 2; |
|
191 |
}
|
|
192 |
||
193 |
for (argument= arguments+1 ; *argument ; argument++) |
|
194 |
puts(*argument); |
|
195 |
my_free((char*) load_default_groups,MYF(0)); |
|
196 |
free_defaults(arguments); |
|
197 |
||
198 |
exit(error); |
|
199 |
}
|
|
200 |