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(int optid,const struct my_option * opt,char * argument)107 get_one_option(int optid, const struct my_option *opt __attribute__((unused)),
108 char *argument __attribute__((unused)))
109 {
110 switch (optid) {
111 case 'c':
112 opt_defaults_file_used= 1;
113 break;
114 case 'n':
115 cleanup_and_exit(0);
116 case 'I':
117 case '?':
118 usage();
119 case 'v':
120 verbose++;
121 break;
122 case 'V':
123 version();
124 /* fall through */
125 case '#':
126 DBUG_PUSH(argument ? argument : default_dbug_option);
127 break;
128 }
129 return 0;
130 }
131
132
get_options(int * argc,char *** argv)133 static int get_options(int *argc,char ***argv)
134 {
135 int ho_error;
136
137 if ((ho_error=handle_options(argc, argv, my_long_options, get_one_option)))
138 exit(ho_error);
139
140 return 0;
141 }
142
143
main(int argc,char ** argv)144 int main(int argc, char **argv)
145 {
146 int count, error, args_used;
147 char **load_default_groups= 0, *tmp_arguments[6];
148 char **argument, **arguments, **org_argv;
149 char *defaults, *extra_defaults, *group_suffix;
150 int nargs, i= 0;
151 MY_INIT(argv[0]);
152
153 org_argv= argv;
154 args_used= get_defaults_options(argc, argv, &defaults, &extra_defaults,
155 &group_suffix);
156
157 /* Copy defaults-xxx arguments & program name */
158 count=args_used+1;
159 arguments= tmp_arguments;
160 memcpy((char*) arguments, (char*) org_argv, count * sizeof(*org_argv));
161 arguments[count]= 0;
162
163 /* Check out the args */
164 if (get_options(&argc,&argv))
165 cleanup_and_exit(1);
166
167 nargs= argc + 1;
168 if (opt_mysqld)
169 nargs+= array_elements(mysqld_groups);
170
171 if (nargs < 2)
172 usage();
173
174 load_default_groups=(char**) my_malloc(nargs*sizeof(char*), MYF(MY_WME));
175 if (!load_default_groups)
176 exit(1);
177 if (opt_mysqld)
178 {
179 for (; mysqld_groups[i]; i++)
180 load_default_groups[i]= (char*) mysqld_groups[i];
181 }
182 memcpy(load_default_groups + i, argv, (argc + 1) * sizeof(*argv));
183 if ((error= load_defaults(config_file, (const char **) load_default_groups,
184 &count, &arguments)))
185 {
186 my_end(0);
187 if (error == 4)
188 return 0;
189 if (verbose && opt_defaults_file_used)
190 {
191 if (error == 1)
192 fprintf(stderr, "WARNING: Defaults file '%s' not found!\n",
193 config_file);
194 /* This error is not available now. For the future */
195 if (error == 2)
196 fprintf(stderr, "WARNING: Defaults file '%s' is not a regular file!\n",
197 config_file);
198 }
199 return 2;
200 }
201
202 for (argument= arguments+1 ; *argument ; argument++)
203 if (!my_getopt_is_args_separator(*argument)) /* skip arguments separator */
204 puts(*argument);
205 my_free(load_default_groups);
206 free_defaults(arguments);
207 my_end(0);
208 exit(0);
209 }
210