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