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