~ubuntu-branches/ubuntu/precise/mysql-5.1/precise

« back to all changes in this revision

Viewing changes to extra/my_print_defaults.c

  • Committer: Bazaar Package Importer
  • Author(s): Norbert Tretkowski
  • Date: 2010-03-17 14:56:02 UTC
  • Revision ID: james.westby@ubuntu.com-20100317145602-x7e30l1b2sb5s6w6
Tags: upstream-5.1.45
ImportĀ upstreamĀ versionĀ 5.1.45

Show diffs side-by-side

added added

removed removed

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