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-1335 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 #ifdef DBUG_OFF
45 {"debug", '#', "This is a non-debug version. Catch this and exit",
46 0,0, 0, GET_DISABLED, OPT_ARG, 0, 0, 0, 0, 0, 0},
47 #else
48 {"debug", '#', "Output debug log", (char**) &default_dbug_option,
49 (char**) &default_dbug_option, 0, GET_STR, OPT_ARG, 0, 0, 0, 0, 0, 0},
50 #endif
51 {"defaults-file", 'c',
52 "Read this file only, do not read global or per-user config "
53 "files; should be the first option",
54 (char**) &config_file, (char*) &config_file, 0, GET_STR, REQUIRED_ARG,
55 0, 0, 0, 0, 0, 0},
56 {"defaults-extra-file", 'e',
57 "Read this file after the global config file and before the config "
58 "file in the users home directory; should be the first option",
59 (void *)&my_defaults_extra_file, (void *)&my_defaults_extra_file, 0,
60 GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
61 {"defaults-group-suffix", 'g',
62 "In addition to the given groups, read also groups with this suffix",
63 (char**) &my_defaults_group_suffix, (char**) &my_defaults_group_suffix,
64 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
65 {"mysqld", 0, "Read the same set of groups that the mysqld binary does.",
66 &opt_mysqld, &opt_mysqld, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
67 {"no-defaults", 'n', "Return an empty string (useful for scripts).",
68 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
69 {"help", '?', "Display this help message and exit.",
70 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
71 {"verbose", 'v', "Increase the output level",
72 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
73 {"version", 'V', "Output version information and exit.",
74 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
75 {0, 0, 0, 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0}
76 };
77
78 static void cleanup_and_exit(int exit_code) __attribute__ ((noreturn));
cleanup_and_exit(int exit_code)79 static void cleanup_and_exit(int exit_code)
80 {
81 my_end(0);
82 exit(exit_code);
83 }
84
version()85 static void version()
86 {
87 printf("%s Ver 1.7 for %s at %s\n",my_progname,SYSTEM_TYPE, MACHINE_TYPE);
88 }
89
90
91 static void usage() __attribute__ ((noreturn));
usage()92 static void usage()
93 {
94 version();
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("Displays the options from option groups of option files, which is useful to see which options a particular tool will use");
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 client-server mysql\n", my_progname);
102 cleanup_and_exit(0);
103 }
104
105
106 static my_bool
get_one_option(const struct my_option * opt,const char * argument,const char * filename)107 get_one_option(const struct my_option *opt __attribute__((unused)),
108 const char *argument __attribute__((unused)),
109 const char *filename)
110 {
111 switch (opt->id) {
112 case 'c':
113 opt_defaults_file_used= 1;
114 break;
115 case 'n':
116 cleanup_and_exit(0);
117 case 'I':
118 case '?':
119 usage();
120 case 'v':
121 verbose++;
122 break;
123 case 'V':
124 version();
125 /* fall through */
126 case '#':
127 DBUG_PUSH(argument ? argument : default_dbug_option);
128 break;
129 }
130 return 0;
131 }
132
133
get_options(int * argc,char *** argv)134 static int get_options(int *argc,char ***argv)
135 {
136 int ho_error;
137
138 if ((ho_error=handle_options(argc, argv, my_long_options, get_one_option)))
139 exit(ho_error);
140
141 return 0;
142 }
143
make_args(const char * s1,const char * s2)144 static char *make_args(const char *s1, const char *s2)
145 {
146 char *s= malloc(strlen(s1) + strlen(s2) + 1);
147 strmov(strmov(s, s1), s2);
148 return s;
149 }
150
main(int argc,char ** argv)151 int main(int argc, char **argv)
152 {
153 int count= 0, error, no_defaults= 0;
154 char **load_default_groups= 0, *tmp_arguments[6];
155 char **argument, **arguments, **org_argv;
156 int nargs, i= 0;
157 MY_INIT(argv[0]);
158
159 org_argv= argv;
160 if (*argv && !strcmp(*argv, "--no-defaults"))
161 {
162 argv++;
163 ++count;
164 no_defaults= 1;
165 }
166 /* Copy program name and --no-defaults if present*/
167 arguments= tmp_arguments;
168 memcpy((char*) arguments, (char*) org_argv, (++count)*sizeof(*org_argv));
169 arguments[count]= 0;
170
171 /* Check out the args */
172 if (get_options(&argc,&argv))
173 cleanup_and_exit(1);
174
175 if (!no_defaults)
176 {
177 if (opt_defaults_file_used)
178 arguments[count++]= make_args("--defaults-file=", config_file);
179 if (my_defaults_extra_file)
180 arguments[count++]= make_args("--defaults-extra-file=",
181 my_defaults_extra_file);
182 if (my_defaults_group_suffix)
183 arguments[count++]= make_args("--defaults-group-suffix=",
184 my_defaults_group_suffix);
185 arguments[count]= 0;
186 }
187
188 nargs= argc + 1;
189 if (opt_mysqld)
190 nargs+= array_elements(mysqld_groups);
191
192 if (nargs < 2)
193 usage();
194
195 load_default_groups=(char**) my_malloc(PSI_NOT_INSTRUMENTED,
196 nargs*sizeof(char*), MYF(MY_WME));
197 if (!load_default_groups)
198 exit(1);
199 if (opt_mysqld)
200 {
201 for (; mysqld_groups[i]; i++)
202 load_default_groups[i]= (char*) mysqld_groups[i];
203 }
204 memcpy(load_default_groups + i, argv, (argc + 1) * sizeof(*argv));
205 if ((error= load_defaults(config_file, (const char **) load_default_groups,
206 &count, &arguments)))
207 {
208 my_end(0);
209 if (error == 4)
210 return 0;
211 if (verbose && opt_defaults_file_used)
212 {
213 if (error == 1)
214 fprintf(stderr, "WARNING: Defaults file '%s' not found!\n",
215 config_file);
216 /* This error is not available now. For the future */
217 if (error == 2)
218 fprintf(stderr, "WARNING: Defaults file '%s' is not a regular file!\n",
219 config_file);
220 }
221 return 2;
222 }
223
224 for (argument= arguments+1 ; *argument ; argument++)
225 puts(*argument);
226 my_free(load_default_groups);
227 free_defaults(arguments);
228 my_end(0);
229 exit(0);
230 }
231