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