~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to client/drizzleadmin.cc

MergedĀ fromĀ Padraig.

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
#include <signal.h>
25
25
#include <mysys/my_pthread.h>                           /* because of signal()  */
26
26
#include <sys/stat.h>
 
27
#include <vector>
 
28
#include <algorithm>
 
29
#include <string>
27
30
 
28
31
/* Added this for string translation. */
29
32
#include <drizzled/gettext.h>
30
33
 
31
34
#define ADMIN_VERSION "8.42"
32
35
#define SHUTDOWN_DEF_TIMEOUT 3600               /* Wait for shutdown */
 
36
#define NUM_COMMAND_NAMES 2
33
37
 
34
38
char *host= NULL, *user= NULL, *opt_password= NULL;
35
39
static bool interrupted= false, opt_verbose= false,tty_password= false;
60
64
  ADMIN_PING
61
65
};
62
66
 
63
 
static const char *command_names[]= {
 
67
static string command_names[]= {
64
68
  "shutdown",
65
 
  "ping",
66
 
  NULL
 
69
  "ping"
67
70
};
68
71
 
69
 
static TYPELIB command_typelib=
70
 
{ array_elements(command_names)-1,"commands", command_names, NULL };
 
72
static vector<string> 
 
73
  command_vector(command_names, command_names + NUM_COMMAND_NAMES);
71
74
 
72
75
static struct my_option my_long_options[] =
73
76
{
106
109
 
107
110
static const char *load_default_groups[]= { "drizzleadmin","client",0 };
108
111
 
 
112
inline string lower_string(const char * from_string)
 
113
{
 
114
  string to_string= from_string;
 
115
  transform(to_string.begin(), to_string.end(),
 
116
            to_string.begin(), ::tolower);
 
117
  return to_string;
 
118
}
 
119
 
 
120
/*
 
121
 * Searches for the given command and determines
 
122
 * its type.
 
123
 *
 
124
 * Returns the type of the command corresponding
 
125
 * to the commands enum defined in drizzleadmin.cc
 
126
 * If the command is not supported, return ADMIN_ERROR.
 
127
 *
 
128
 * @param command name to search for
 
129
 */
 
130
static int get_command_type(const char *name)
 
131
{
 
132
  int type= ADMIN_ERROR;
 
133
  string comp_string= lower_string(name);
 
134
  vector<string>::iterator it= 
 
135
    std::find(command_vector.begin(), command_vector.end(),
 
136
           comp_string);
 
137
  if (it != command_vector.end())
 
138
  {
 
139
    /* add 1 due to the way the commands ENUM is defined */
 
140
    type= distance(command_vector.begin(), it) + 1;
 
141
  }
 
142
  return type;
 
143
}
 
144
 
109
145
bool
110
146
get_one_option(int optid, const struct my_option *, char *argument)
111
147
{
112
148
  char *endchar= NULL;
113
149
  uint64_t temp_drizzle_port= 0;
114
 
  int error = 0;
 
150
  int error= 0;
115
151
 
116
152
  switch(optid) {
117
153
  case 'p':
138
174
  case 'P':
139
175
    if (argument)
140
176
    {
141
 
      char *start=argument;
 
177
      char *start= argument;
142
178
      if (opt_password)
143
179
        free(opt_password);
144
180
 
174
210
    if (argument)
175
211
    {
176
212
      if ((option_wait=atoi(argument)) <= 0)
177
 
        option_wait=1;
 
213
        option_wait= 1;
178
214
    }
179
215
    else
180
216
      option_wait= ~(uint32_t)0;
204
240
  drizzle_create(&drizzle);
205
241
  drizzle_con_create(&drizzle, &con);
206
242
  load_defaults("drizzle",load_default_groups,&argc,&argv);
207
 
  save_argv = argv;                             /* Save for free_defaults */
 
243
  save_argv= argv;                              /* Save for free_defaults */
208
244
  if ((ho_error=handle_options(&argc, &argv, my_long_options, get_one_option)))
209
245
  {
210
246
    free_defaults(save_argv);
217
253
    exit(1);
218
254
  }
219
255
 
220
 
  commands = argv;
 
256
  commands= argv;
221
257
  if (tty_password)
222
258
    opt_password = client_get_tty_password(NULL);
223
259
 
227
263
/* XXX
228
264
  if (opt_connect_timeout)
229
265
  {
230
 
    uint32_t tmp=opt_connect_timeout;
 
266
    uint32_t tmp= opt_connect_timeout;
231
267
    drizzleclient_options(&drizzle,DRIZZLE_OPT_CONNECT_TIMEOUT, (char*) &tmp);
232
268
  }
233
269
*/
237
273
    /* Return 0 if all commands are PING */
238
274
    for (; argc > 0; argv++, argc--)
239
275
    {
240
 
      if (find_type(argv[0], &command_typelib, 2) != ADMIN_PING)
 
276
      int type= get_command_type(argv[0]);
 
277
      if (type != ADMIN_PING)
241
278
      {
242
279
        error= 1;
243
280
        break;
259
296
 
260
297
void endprog(int)
261
298
{
262
 
  interrupted=1;
 
299
  interrupted= 1;
263
300
}
264
301
 
265
302
static bool sql_connect(drizzle_con_st *con, uint32_t wait)
266
303
{
267
 
  bool info=0;
 
304
  bool info= 0;
268
305
  drizzle_return_t ret;
269
306
 
270
307
  drizzle_con_set_tcp(con, host, tcp_port);
316
353
    {
317
354
      if (!info)
318
355
      {
319
 
        info=1;
 
356
        info= 1;
320
357
        fputs(_("Waiting for Drizzle server to answer"),stderr);
321
358
        (void) fflush(stderr);
322
359
      }
348
385
  */
349
386
  for (; argc > 0 ; argv++,argc--)
350
387
  {
351
 
    switch (find_type(argv[0],&command_typelib,2)) {
 
388
    int type= get_command_type(argv[0]);
 
389
    switch (type) {
352
390
    case ADMIN_SHUTDOWN:
353
391
    {
354
392
      if (opt_verbose)
376
414
      if (opt_verbose)
377
415
        printf(_("done\n"));
378
416
 
379
 
      argc=1;             /* Force SHUTDOWN to be the last command */
 
417
      argc= 1;             /* Force SHUTDOWN to be the last command */
380
418
      break;
381
419
    }
382
420
    case ADMIN_PING: