~drizzle-trunk/drizzle/development

1 by brian
clean slate
1
/* Copyright (C) 2000 MySQL AB
2
3
   This program is free software; you can redistribute it and/or modify
4
   it under the terms of the GNU General Public License as published by
5
   the Free Software Foundation; version 2 of the License.
6
7
   This program is distributed in the hope that it will be useful,
8
   but WITHOUT ANY WARRANTY; without even the implied warranty of
9
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
   GNU General Public License for more details.
11
12
   You should have received a copy of the GNU General Public License
13
   along with this program; if not, write to the Free Software
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
15
16
/*
17
**  print_default.c:
18
**  Print all parameters in a default file that will be given to some program.
19
**
20
**  Written by Monty
21
*/
22
212.5.39 by Monty Taylor
Phew. Moved my_base and my_global.
23
#include <drizzled/global.h>
212.5.13 by Monty Taylor
Moved my_sys/my_pthread/my_nosys and mysys_err to mysys.
24
#include <mysys/my_sys.h>
212.5.18 by Monty Taylor
Moved m_ctype, m_string and my_bitmap. Removed t_ctype.
25
#include <mystrings/m_string.h>
212.5.21 by Monty Taylor
Moved my_getopt.h
26
#include <mysys/my_getopt.h>
1 by brian
clean slate
27
28
29
const char *config_file="my";			/* Default config file */
30
uint verbose= 0, opt_defaults_file_used= 0;
31
32
static struct my_option my_long_options[] =
33
{
34
  /*
35
    NB: --config-file is troublesome, because get_defaults_options() doesn't
36
    know about it, but we pretend --config-file is like --defaults-file.  In
37
    fact they behave differently: see the comments at the top of
38
    mysys/default.c for how --defaults-file should behave.
39
40
    This --config-file option behaves as:
41
    - If it has a directory name part (absolute or relative), then only this
42
      file is read; no error is given if the file doesn't exist
43
    - If the file has no directory name part, the standard locations are
44
      searched for a file of this name (and standard filename extensions are
45
      added if the file has no extension)
46
  */
47
  {"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",
77.1.77 by Monty Taylor
A crapton more warning cleanups (I turned on more warnings)
48
   (char**) &config_file, (char**) &config_file, 0, GET_STR, REQUIRED_ARG,
1 by brian
clean slate
49
   0, 0, 0, 0, 0, 0},
50
  {"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",
77.1.77 by Monty Taylor
A crapton more warning cleanups (I turned on more warnings)
51
   (char**) &config_file, (char**) &config_file, 0, GET_STR, REQUIRED_ARG,
1 by brian
clean slate
52
   0, 0, 0, 0, 0, 0},
53
  {"defaults-extra-file", 'e',
54
   "Read this file after the global config file and before the config file in the users home directory; should be the first option",
77.1.77 by Monty Taylor
A crapton more warning cleanups (I turned on more warnings)
55
   (char**) &my_defaults_extra_file, (char**) &my_defaults_extra_file, 0,
1 by brian
clean slate
56
   GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
57
  {"defaults-group-suffix", 'g',
58
   "In addition to the given groups, read also groups with this suffix",
77.1.77 by Monty Taylor
A crapton more warning cleanups (I turned on more warnings)
59
   (char**) &my_defaults_group_suffix, (char**) &my_defaults_group_suffix,
1 by brian
clean slate
60
   0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
61
  {"extra-file", 'e',
62
   "Deprecated. Synonym for --defaults-extra-file.",
77.1.77 by Monty Taylor
A crapton more warning cleanups (I turned on more warnings)
63
   (char**) &my_defaults_extra_file,
64
   (char**) &my_defaults_extra_file, 0, GET_STR,
1 by brian
clean slate
65
   REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
66
  {"no-defaults", 'n', "Return an empty string (useful for scripts).",
67
   0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
68
  {"help", '?', "Display this help message and exit.",
69
   0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
70
  {"verbose", 'v', "Increase the output level",
71
   0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
72
  {"version", 'V', "Output version information and exit.",
73
   0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
74
  {0, 0, 0, 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0}
75
};
76
277 by Brian Aker
Cleanup of my_bool from extra and libdrizzle
77
static void usage(bool version)
1 by brian
clean slate
78
{
79
  printf("%s  Ver 1.6 for %s at %s\n",my_progname,SYSTEM_TYPE,
80
	 MACHINE_TYPE);
81
  if (version)
82
    return;
83
  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");
84
  puts("Prints all arguments that is give to some program using the default files");
85
  printf("Usage: %s [OPTIONS] groups\n", my_progname);
86
  my_print_help(my_long_options);
87
  my_print_default_files(config_file);
88
  my_print_variables(my_long_options);
89
  printf("\nExample usage:\n%s --defaults-file=example.cnf client mysql\n", my_progname);
90
}
91
146 by Brian Aker
my_bool cleanup.
92
static bool
1 by brian
clean slate
93
get_one_option(int optid, const struct my_option *opt __attribute__((unused)),
94
	       char *argument __attribute__((unused)))
95
{
96
  switch (optid) {
97
    case 'c':
98
      opt_defaults_file_used= 1;
99
      break;
100
    case 'n':
101
    exit(0);
102
    case 'I':
103
    case '?':
104
    usage(0);
105
    exit(0);
106
    case 'v':
107
      verbose++;
108
      break;
109
    case 'V':
110
    usage(1);
111
    exit(0);
112
  }
113
  return 0;
114
}
115
116
117
static int get_options(int *argc,char ***argv)
118
{
119
  int ho_error;
120
121
  if ((ho_error=handle_options(argc, argv, my_long_options, get_one_option)))
122
    exit(ho_error);
123
124
  if (*argc < 1)
125
  {
126
    usage(0);
127
    return 1;
128
  }
129
  return 0;
130
}
131
132
133
int main(int argc, char **argv)
134
{
135
  int count, error, args_used;
136
  char **load_default_groups, *tmp_arguments[6];
137
  char **argument, **arguments, **org_argv;
138
  char *defaults, *extra_defaults, *group_suffix;
139
  MY_INIT(argv[0]);
140
141
  org_argv= argv;
142
  args_used= get_defaults_options(argc, argv, &defaults, &extra_defaults,
143
                                  &group_suffix);
144
145
  /* Copy defaults-xxx arguments & program name */
146
  count=args_used+1;
147
  arguments= tmp_arguments;
212.6.11 by Mats Kindahl
emoving redundant use of casts in extra/ for memcmp(), memcpy(), memset(), and memmove().
148
  memcpy(arguments, org_argv, count * sizeof(*org_argv));
1 by brian
clean slate
149
  arguments[count]= 0;
150
151
  /* Check out the args */
152
  if (!(load_default_groups=(char**) my_malloc((argc+1)*sizeof(char*),
153
					       MYF(MY_WME))))
154
    exit(1);
155
  if (get_options(&argc,&argv))
156
    exit(1);
212.6.11 by Mats Kindahl
emoving redundant use of casts in extra/ for memcmp(), memcpy(), memset(), and memmove().
157
  memcpy(load_default_groups, argv, (argc + 1) * sizeof(*argv));
1 by brian
clean slate
158
159
  if ((error= load_defaults(config_file, (const char **) load_default_groups,
160
			   &count, &arguments)))
161
  {
162
    if (verbose && opt_defaults_file_used)
163
    {
164
      if (error == 1)
165
	fprintf(stderr, "WARNING: Defaults file '%s' not found!\n",
166
		config_file);
167
      /* This error is not available now. For the future */
168
      if (error == 2)
169
	fprintf(stderr, "WARNING: Defaults file '%s' is not a regular file!\n",
170
		config_file);
171
    }
172
    error= 2;
173
  }
174
175
  for (argument= arguments+1 ; *argument ; argument++)
176
    puts(*argument);
477 by Monty Taylor
Removed my_free(). It turns out that it had been def'd to ignore the flags passed to it in the second arg anyway. Gotta love that.
177
  free((char*) load_default_groups);
1 by brian
clean slate
178
  free_defaults(arguments);
179
180
  exit(error);
181
}