~drizzle-trunk/drizzle/development

77.3.7 by Monty Taylor
Made mysql into a pure-C program.
1
/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
77.3.1 by Monty Taylor
First steps in removing sql_string and using glib instead.
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 *
1719.2.3 by Vijay Samuel
Merge removed the drizzled/option.h include from client_priv.h and archive_reader.
4
 *  Copyright (C) 2010 Vijay Samuel
77.3.1 by Monty Taylor
First steps in removing sql_string and using glib instead.
5
 *  Copyright (C) 2008 MySQL
6
 *
7
 *  This program is free software; you can redistribute it and/or modify
8
 *  it under the terms of the GNU General Public License as published by
9
 *  the Free Software Foundation; either version 2 of the License, or
10
 *  (at your option) any later version.
11
 *
12
 *  This program is distributed in the hope that it will be useful,
1675.1.3 by Monty Taylor
Merged in local client config lookup paths from vijay. Thanks.
13
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
77.3.1 by Monty Taylor
First steps in removing sql_string and using glib instead.
14
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 *  GNU General Public License for more details.
16
 *
17
 *  You should have received a copy of the GNU General Public License
18
 *  along with this program; if not, write to the Free Software
19
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20
 */
1 by brian
clean slate
21
206.3.1 by Patrick Galbraith
Most everything working with client rename
22
/* drizzle command tool
1 by brian
clean slate
23
 * Commands compatible with mSQL by David J. Hughes
24
 *
25
 * Written by:
26
 *   Michael 'Monty' Widenius
27
 *   Andi Gutmans  <andi@zend.com>
28
 *   Zeev Suraski  <zeev@zend.com>
202.1.25 by Monty Taylor
Fixed change of mysql.com -> drizzle.com.
29
 *   Jani Tolonen  <jani@mysql.com>
30
 *   Matt Wagner   <matt@mysql.com>
31
 *   Jeremy Cole   <jcole@mysql.com>
32
 *   Tonu Samuel   <tonu@mysql.com>
33
 *   Harrison Fisk <harrison@mysql.com>
1 by brian
clean slate
34
 *
35
 **/
36
1567.3.15 by Monty Taylor
Removed the last vestiges of my_sys from drizzle.cc.
37
#include "config.h"
38
#include <libdrizzle/drizzle_client.h>
39
40
#include "client/get_password.h"
41
42
#if TIME_WITH_SYS_TIME
43
# include <sys/time.h>
44
# include <time.h>
45
#else
46
# if HAVE_SYS_TIME_H
47
#  include <sys/time.h>
48
# else
49
#  include <time.h>
50
# endif
51
#endif
52
1567.3.22 by Monty Taylor
Include cerrno for ENOMEM on FreeBSD.
53
#include <cerrno>
279.2.2 by Monty Taylor
Replaced DYNAMIC_STRING with C++ string in drizzle command line client.
54
#include <string>
1241.9.1 by Monty Taylor
Removed global.h. Fixed all the headers.
55
#include <drizzled/gettext.h>
56
#include <iostream>
1567.3.8 by Vijay Samuel
Refactor drizzle client.
57
#include <fstream>
1241.9.1 by Monty Taylor
Removed global.h. Fixed all the headers.
58
#include <map>
713.1.4 by Monty Taylor
Fixed max() problem for solaris
59
#include <algorithm>
1241.9.1 by Monty Taylor
Removed global.h. Fixed all the headers.
60
#include <limits.h>
61
#include <cassert>
1 by brian
clean slate
62
#include <stdarg.h>
1241.9.1 by Monty Taylor
Removed global.h. Fixed all the headers.
63
#include <math.h>
1929.1.13 by Stewart Smith
fix large stack usage in handle_sigint in drizzle client
64
#include <memory>
1095.2.1 by Robert Klahn
Replace typedef struct LINE_BUFFER with class LineBuffer, encapsulating current logic
65
#include "client/linebuffer.h"
1 by brian
clean slate
66
#include <signal.h>
212.5.30 by Monty Taylor
Removed my_net.h. Pointless.
67
#include <sys/ioctl.h>
722.1.4 by Monty Taylor
Removed all the setting of DEFS everywhere. Use configmake.h to get the values
68
#include <drizzled/configmake.h>
1567.3.20 by Monty Taylor
Removed UTF-8 lib that we don't use.
69
#include "drizzled/utf8/utf8.h"
1671.2.1 by Vijay Samuel
Merge new user config file processing system.
70
#include <cstdlib>
212.5.30 by Monty Taylor
Removed my_net.h. Pointless.
71
1 by brian
clean slate
72
#if defined(HAVE_CURSES_H) && defined(HAVE_TERM_H)
73
#include <curses.h>
139.1.15 by Trond Norbye
curses.h defines clear and erase as macros causing problems calling the std::string members
74
#ifdef __sun
75
#undef clear
76
#undef erase
77
#endif
1 by brian
clean slate
78
#include <term.h>
79
#else
80
#if defined(HAVE_TERMIOS_H)
81
#include <termios.h>
82
#include <unistd.h>
83
#elif defined(HAVE_TERMBITS_H)
84
#include <termbits.h>
85
#elif defined(HAVE_ASM_TERMBITS_H) && (!defined __GLIBC__ || !(__GLIBC__ > 2 || __GLIBC__ == 2 && __GLIBC_MINOR__ > 0))
206.3.1 by Patrick Galbraith
Most everything working with client rename
86
#include <asm/termbits.h>    // Standard linux
1 by brian
clean slate
87
#endif
88
#if defined(HAVE_TERMCAP_H)
89
#include <termcap.h>
90
#else
91
#ifdef HAVE_CURSES_H
92
#include <curses.h>
93
#endif
206.3.1 by Patrick Galbraith
Most everything working with client rename
94
#undef SYSV        // hack to avoid syntax error
1 by brian
clean slate
95
#ifdef HAVE_TERM_H
96
#include <term.h>
97
#endif
98
#endif
99
#endif
100
779.2.4 by Monty Taylor
Updated some more build stuff.
101
#ifdef HAVE_LIBREADLINE
102
#  if defined(HAVE_READLINE_READLINE_H)
103
#    include <readline/readline.h>
104
#  elif defined(HAVE_READLINE_H)
105
#    include <readline.h>
106
#  else /* !defined(HAVE_READLINE_H) */
107
extern char *readline ();
108
#  endif /* !defined(HAVE_READLINE_H) */
109
char *cmdline = NULL;
110
#else /* !defined(HAVE_READLINE_READLINE_H) */
1441.3.1 by Vijay Samuel
all required updations have been made
111
  /* no readline */
779.2.4 by Monty Taylor
Updated some more build stuff.
112
#  error Readline Required
113
#endif /* HAVE_LIBREADLINE */
114
115
#ifdef HAVE_READLINE_HISTORY
116
#  if defined(HAVE_READLINE_HISTORY_H)
117
#    include <readline/history.h>
118
#  elif defined(HAVE_HISTORY_H)
119
#    include <history.h>
120
#  else /* !defined(HAVE_HISTORY_H) */
121
extern void add_history ();
122
extern int write_history ();
123
extern int read_history ();
124
#  endif /* defined(HAVE_READLINE_HISTORY_H) */
1441.3.1 by Vijay Samuel
all required updations have been made
125
    /* no history */
779.2.4 by Monty Taylor
Updated some more build stuff.
126
#endif /* HAVE_READLINE_HISTORY */
127
779.2.14 by Monty Taylor
Fixed a coupla oopses on the Mac.
128
/**
1441.3.1 by Vijay Samuel
all required updations have been made
129
 Make the old readline interface look like the new one.
779.2.14 by Monty Taylor
Fixed a coupla oopses on the Mac.
130
*/
1149.2.1 by Monty Taylor
Workaround for half-present new-interface on Snow Leopard, combined
131
#ifndef HAVE_RL_COMPLETION
132
typedef char **rl_completion_func_t(const char *, int, int);
779.2.14 by Monty Taylor
Fixed a coupla oopses on the Mac.
133
#define rl_completion_matches(str, func) \
134
  completion_matches((char *)str, (CPFunction *)func)
135
#endif
779.2.4 by Monty Taylor
Updated some more build stuff.
136
1149.2.1 by Monty Taylor
Workaround for half-present new-interface on Snow Leopard, combined
137
#ifdef HAVE_RL_COMPENTRY
138
# ifdef HAVE_WORKING_RL_COMPENTRY
139
typedef rl_compentry_func_t drizzle_compentry_func_t;
140
# else
141
/* Snow Leopard ships an rl_compentry which cannot be assigned to
142
 * rl_completion_entry_function. We must undo the complete and total
143
 * ass-bagery.
144
 */
145
typedef Function drizzle_compentry_func_t;
146
# endif
147
#else
1149.2.2 by Monty Taylor
Fixed bad assumption about non-Snow Leopard.
148
typedef Function drizzle_compentry_func_t;
1149.2.1 by Monty Taylor
Workaround for half-present new-interface on Snow Leopard, combined
149
#endif
150
779.2.4 by Monty Taylor
Updated some more build stuff.
151
#if defined(HAVE_LOCALE_H)
152
#include <locale.h>
153
#endif
154
155
156
1 by brian
clean slate
157
#if !defined(HAVE_VIDATTR)
158
#undef vidattr
206.3.1 by Patrick Galbraith
Most everything working with client rename
159
#define vidattr(A) {}      // Can't get this to work
1 by brian
clean slate
160
#endif
1567.3.5 by Vijay Samuel
Refactored client.
161
#include <boost/program_options.hpp>
1929.1.40 by Monty Taylor
Replaced auto_ptr with scoped_ptr.
162
#include <boost/scoped_ptr.hpp>
1815.1.1 by Monty Taylor
Embed a modified version of parse_config_file. There are several more bugs
163
#include "drizzled/program_options/config_file.h"
1 by brian
clean slate
164
279.2.2 by Monty Taylor
Replaced DYNAMIC_STRING with C++ string in drizzle command line client.
165
using namespace std;
1567.3.5 by Vijay Samuel
Refactored client.
166
namespace po=boost::program_options;
1815.1.1 by Monty Taylor
Embed a modified version of parse_config_file. There are several more bugs
167
namespace dpo=drizzled::program_options;
279.2.2 by Monty Taylor
Replaced DYNAMIC_STRING with C++ string in drizzle command line client.
168
919.2.11 by Monty Taylor
Removed C99 isnan() usage, which allows us to remove the util/math.{cc,h} workarounds. Yay for standards!
169
/* Don't try to make a nice table if the data is too big */
170
const uint32_t MAX_COLUMN_LENGTH= 1024;
171
172
/* Buffer to hold 'version' and 'version_comment' */
173
const int MAX_SERVER_VERSION_LENGTH= 128;
174
1 by brian
clean slate
175
#define PROMPT_CHAR '\\'
176
1441.3.1 by Vijay Samuel
all required updations have been made
177
class Status
1 by brian
clean slate
178
{
1441.3.1 by Vijay Samuel
all required updations have been made
179
public:
180
181
  Status(int in_exit_status, 
182
         uint32_t in_query_start_line,
1891.2.1 by Monty Taylor
Fixed things to make things compile with clang
183
         char *in_file_name,
1441.3.1 by Vijay Samuel
all required updations have been made
184
         LineBuffer *in_line_buff,
1891.2.1 by Monty Taylor
Fixed things to make things compile with clang
185
         bool in_batch,
186
         bool in_add_to_history)
1441.3.1 by Vijay Samuel
all required updations have been made
187
    :
188
    exit_status(in_exit_status),
189
    query_start_line(in_query_start_line),
190
    file_name(in_file_name),
191
    line_buff(in_line_buff),
192
    batch(in_batch),
193
    add_to_history(in_add_to_history)
194
    {}
195
1891.2.1 by Monty Taylor
Fixed things to make things compile with clang
196
  Status() :
197
    exit_status(0),
198
    query_start_line(0),
199
    file_name(NULL),
200
    line_buff(NULL),
201
    batch(false),        
202
    add_to_history(false)
203
  {}
1441.3.1 by Vijay Samuel
all required updations have been made
204
  
205
  int getExitStatus() const
206
  {
207
    return exit_status;
208
  }
209
210
  uint32_t getQueryStartLine() const
211
  {
212
    return query_start_line;
213
  }
214
215
  const char *getFileName() const
216
  {
217
    return file_name;
218
  }
219
220
  LineBuffer *getLineBuff() const
221
  {
222
    return line_buff;
223
  }
224
225
  bool getBatch() const
226
  {
227
    return batch;
228
  }
229
230
  bool getAddToHistory() const
231
  {
232
    return add_to_history;
233
  }
234
235
  void setExitStatus(int in_exit_status)
236
  {
237
    exit_status= in_exit_status;
238
  }
239
240
  void setQueryStartLine(uint32_t in_query_start_line)
241
  {
242
    query_start_line= in_query_start_line;
243
  }
244
245
  void setFileName(char *in_file_name)
246
  {
247
    file_name= in_file_name;
248
  }
249
250
  void setLineBuff(int max_size, FILE *file=NULL)
251
  {
252
    line_buff= new(std::nothrow) LineBuffer(max_size, file);
253
  }
254
255
  void setLineBuff(LineBuffer *in_line_buff)
256
  {
257
    line_buff= in_line_buff;
258
  }
259
260
  void setBatch(bool in_batch)
261
  {
262
    batch= in_batch;
263
  }
264
265
  void setAddToHistory(bool in_add_to_history)
266
  {
267
    add_to_history= in_add_to_history;
268
  }
269
270
private:
1 by brian
clean slate
271
  int exit_status;
77.1.107 by Monty Taylor
Fixed build warnings.
272
  uint32_t query_start_line;
1 by brian
clean slate
273
  char *file_name;
1095.2.1 by Robert Klahn
Replace typedef struct LINE_BUFFER with class LineBuffer, encapsulating current logic
274
  LineBuffer *line_buff;
1 by brian
clean slate
275
  bool batch,add_to_history;
1441.3.1 by Vijay Samuel
all required updations have been made
276
}; 
1 by brian
clean slate
277
923.2.6 by Monty Taylor
Removing hand-done completion hash functions.
278
static map<string, string>::iterator completion_iter;
923.2.7 by Monty Taylor
Finished replacing custom HashMap with std::map. We use a map and not a set
279
static map<string, string>::iterator completion_end;
923.2.6 by Monty Taylor
Removing hand-done completion hash functions.
280
static map<string, string> completion_map;
923.2.7 by Monty Taylor
Finished replacing custom HashMap with std::map. We use a map and not a set
281
static string completion_string;
282
1 by brian
clean slate
283
284
enum enum_info_type { INFO_INFO,INFO_ERROR,INFO_RESULT};
285
typedef enum enum_info_type INFO_TYPE;
286
928.1.1 by Eric Day
Started client changes.
287
static drizzle_st drizzle;      /* The library handle */
288
static drizzle_con_st con;      /* The connection */
673.5.2 by Andrew Hutchings
Code cleanups
289
static bool ignore_errors= false, quick= false,
1441.3.1 by Vijay Samuel
all required updations have been made
290
  connected= false, opt_raw_data= false, unbuffered= false,
291
  output_tables= false, opt_rehash= true, skip_updates= false,
292
  safe_updates= false, one_database= false,
1802.13.2 by Andrew Hutchings
Remove compress option
293
  opt_shutdown= false, opt_ping= false,
1441.3.1 by Vijay Samuel
all required updations have been made
294
  vertical= false, line_numbers= true, column_names= true,
295
  opt_nopager= true, opt_outfile= false, named_cmds= false,
1627.2.2 by Monty Taylor
Moved password parsing code into get_password.cc.
296
  opt_nobeep= false, opt_reconnect= true,
1567.3.15 by Monty Taylor
Removed the last vestiges of my_sys from drizzle.cc.
297
  opt_secure_auth= false,
1441.3.1 by Vijay Samuel
all required updations have been made
298
  default_pager_set= false, opt_sigint_ignore= false,
299
  auto_vertical_output= false,
300
  show_warnings= false, executing_query= false, interrupted_query= false,
1745.2.1 by LinuxJedi
Remove the --mysql option and convert the --protocol option to do something similar.
301
  use_drizzle_protocol= false, opt_local_infile;
1567.3.1 by Vijay Samuel
Refactoring of the drizzle client.
302
static uint32_t show_progress_size= 0;
143 by Brian Aker
Bool cleanup.
303
static bool column_types_flag;
673.5.2 by Andrew Hutchings
Code cleanups
304
static bool preserve_comments= false;
1567.4.2 by Jay Pipes
Some style fixes and removal of deprecated stuff...
305
static uint32_t opt_max_input_line;
306
static uint32_t opt_drizzle_port= 0;
1923.3.4 by Andrew Hutchings
Revert -v fix
307
static int  opt_silent, verbose= 0;
928.1.5 by Eric Day
Merged trunk.
308
static drizzle_capabilities_t connect_flag= DRIZZLE_CAPABILITIES_NONE;
1 by brian
clean slate
309
static char *histfile;
310
static char *histfile_tmp;
279.2.2 by Monty Taylor
Replaced DYNAMIC_STRING with C++ string in drizzle command line client.
311
static string *glob_buffer;
312
static string *processed_prompt= NULL;
182.1.2 by Jim Winstead
Various fixes to enable compilation on Mac OS X, and remove the glib dependency.
313
static char *default_prompt= NULL;
673.5.2 by Andrew Hutchings
Code cleanups
314
static char *full_username= NULL,*part_username= NULL;
1441.3.1 by Vijay Samuel
all required updations have been made
315
static Status status;
164 by Brian Aker
Commit cleanup of export types.
316
static uint32_t select_limit;
77.1.107 by Monty Taylor
Fixed build warnings.
317
static uint32_t max_join_size;
288 by Brian Aker
ulong cleanp in client apps
318
static uint32_t opt_connect_timeout= 0;
1567.3.1 by Vijay Samuel
Refactoring of the drizzle client.
319
std::string current_db,
320
  delimiter_str,  
321
  current_host,
322
  current_prompt,
323
  current_user,
1923.3.4 by Andrew Hutchings
Revert -v fix
324
  opt_verbose,
1567.3.5 by Vijay Samuel
Refactored client.
325
  current_password,
1745.2.1 by LinuxJedi
Remove the --mysql option and convert the --protocol option to do something similar.
326
  opt_password,
327
  opt_protocol;
202.3.2 by Monty Taylor
Added gettext calls in to my_getopt.c and drizzle.c
328
// TODO: Need to i18n these
673.5.1 by Andrew Hutchings
Minor coding standards cleanup
329
static const char *day_names[]= {"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
330
static const char *month_names[]= {"Jan","Feb","Mar","Apr","May","Jun","Jul",
1441.3.1 by Vijay Samuel
all required updations have been made
331
                                  "Aug","Sep","Oct","Nov","Dec"};
1567.3.15 by Monty Taylor
Removed the last vestiges of my_sys from drizzle.cc.
332
/* @TODO: Remove this */
333
#define FN_REFLEN 512
334
1567.3.17 by Monty Taylor
Removed more C-string strings. replaced with std::string.
335
static string default_pager("");
336
static string pager("");
337
static string outfile("");
1 by brian
clean slate
338
static FILE *PAGER, *OUTFILE;
893 by Brian Aker
First pass of stripping uint
339
static uint32_t prompt_counter;
1567.3.1 by Vijay Samuel
Refactoring of the drizzle client.
340
static char *delimiter= NULL;
893 by Brian Aker
First pass of stripping uint
341
static uint32_t delimiter_length= 1;
1 by brian
clean slate
342
unsigned short terminal_width= 80;
343
1877.1.1 by Andrew Hutchings
Use size_t for command in drizzle.cc
344
int drizzleclient_real_query_for_lazy(const char *buf, size_t length,
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
345
                                      drizzle_result_st *result,
346
                                      uint32_t *error_code);
928.1.4 by Eric Day
Fixed a few bugs, more progress on conversion.
347
int drizzleclient_store_result_for_lazy(drizzle_result_st *result);
77.3.7 by Monty Taylor
Made mysql into a pure-C program.
348
1 by brian
clean slate
349
350
void tee_fprintf(FILE *file, const char *fmt, ...);
351
void tee_fputs(const char *s, FILE *file);
352
void tee_puts(const char *s, FILE *file);
353
void tee_putc(int c, FILE *file);
354
static void tee_print_sized_data(const char *, unsigned int, unsigned int, bool);
355
/* The names of functions that actually do the manipulation. */
1567.5.3 by Vijay Samuel
Merge patch for sql_connect() and style issue.
356
static int process_options(void);
279.2.2 by Monty Taylor
Replaced DYNAMIC_STRING with C++ string in drizzle command line client.
357
static int com_quit(string *str,const char*),
1441.3.1 by Vijay Samuel
all required updations have been made
358
  com_go(string *str,const char*), com_ego(string *str,const char*),
359
  com_print(string *str,const char*),
360
  com_help(string *str,const char*), com_clear(string *str,const char*),
361
  com_connect(string *str,const char*), com_status(string *str,const char*),
362
  com_use(string *str,const char*), com_source(string *str, const char*),
1954 by Brian Aker
This adds the command "shutdown" to the command line client. It does require
363
  com_shutdown(string *str,const char*),
1441.3.1 by Vijay Samuel
all required updations have been made
364
  com_rehash(string *str, const char*), com_tee(string *str, const char*),
365
  com_notee(string *str, const char*),
366
  com_prompt(string *str, const char*), com_delimiter(string *str, const char*),
367
  com_warnings(string *str, const char*), com_nowarnings(string *str, const char*),
368
  com_nopager(string *str, const char*), com_pager(string *str, const char*);
1 by brian
clean slate
369
370
static int read_and_execute(bool interactive);
1567.5.3 by Vijay Samuel
Merge patch for sql_connect() and style issue.
371
static int sql_connect(const string &host, const string &database, const string &user, const string &password,
893 by Brian Aker
First pass of stripping uint
372
                       uint32_t silent);
928.1.1 by Eric Day
Started client changes.
373
static const char *server_version_string(drizzle_con_st *con);
893 by Brian Aker
First pass of stripping uint
374
static int put_info(const char *str,INFO_TYPE info,uint32_t error,
77.3.7 by Monty Taylor
Made mysql into a pure-C program.
375
                    const char *sql_state);
928.1.1 by Eric Day
Started client changes.
376
static int put_error(drizzle_con_st *con, drizzle_result_st *res);
288 by Brian Aker
ulong cleanp in client apps
377
static void safe_put_field(const char *pos,uint32_t length);
77.3.7 by Monty Taylor
Made mysql into a pure-C program.
378
static void init_pager(void);
379
static void end_pager(void);
1 by brian
clean slate
380
static void init_tee(const char *);
77.3.7 by Monty Taylor
Made mysql into a pure-C program.
381
static void end_tee(void);
382
static const char* construct_prompt(void);
143 by Brian Aker
Bool cleanup.
383
static char *get_arg(char *line, bool get_next_arg);
77.3.7 by Monty Taylor
Made mysql into a pure-C program.
384
static void init_username(void);
1 by brian
clean slate
385
static void add_int_to_prompt(int toadd);
928.1.1 by Eric Day
Started client changes.
386
static int get_result_width(drizzle_result_st *res);
387
static int get_field_disp_length(drizzle_column_st * field);
266.6.1 by Andy Lester
merging from main
388
static const char * strcont(register const char *str, register const char *set);
1 by brian
clean slate
389
1441.3.1 by Vijay Samuel
all required updations have been made
390
/* A class which contains information on the commands this program
391
   can understand. */
392
class Commands
393
{
394
private:
202.1.25 by Monty Taylor
Fixed change of mysql.com -> drizzle.com.
395
  const char *name;        /* User printable name of the function. */
396
  char cmd_char;        /* msql command character */
1441.3.1 by Vijay Samuel
all required updations have been made
397
public:
398
Commands(const char *in_name,
399
         char in_cmd_char,
400
         int (*in_func)(string *str,const char *name),
401
         bool in_takes_params,
402
         const char *in_doc)
403
  :
404
  name(in_name),
405
  cmd_char(in_cmd_char),
406
  func(in_func),
407
  takes_params(in_takes_params),
408
  doc(in_doc)
409
  {}
410
411
  Commands()
412
  :
413
  name(),
414
  cmd_char(),
415
  func(NULL),
416
  takes_params(false),
417
  doc()
418
  {}
419
420
  int (*func)(string *str,const char *);/* Function to call to do the job. */
421
422
  const char *getName() const
423
  {
424
    return name;
425
  }
426
427
  char getCmdChar() const
428
  {
429
    return cmd_char;
430
  }
431
432
  bool getTakesParams() const
433
  {
434
    return takes_params;
435
  }
436
437
  const char *getDoc() const
438
  {
439
    return doc;
440
  }
441
442
  void setName(const char *in_name)
443
  {
444
     name= in_name;
445
  }
446
447
  void setCmdChar(char in_cmd_char)
448
  {
449
    cmd_char= in_cmd_char;
450
  }
451
452
  void setTakesParams(bool in_takes_params)
453
  {
454
    takes_params= in_takes_params;
455
  }
456
457
  void setDoc(const char *in_doc)
458
  {
459
    doc= in_doc;
460
  }
461
462
private:
202.1.25 by Monty Taylor
Fixed change of mysql.com -> drizzle.com.
463
  bool takes_params;        /* Max parameters for command */
464
  const char *doc;        /* Documentation for this function.  */
1441.3.1 by Vijay Samuel
all required updations have been made
465
}; 
466
467
468
static Commands commands[] = {
1441.3.3 by Vijay Samuel
Changes made to the struct conversion
469
  Commands( "?",      '?', com_help,   0, N_("Synonym for `help'.") ),
470
  Commands( "clear",  'c', com_clear,  0, N_("Clear command.")),
471
  Commands( "connect",'r', com_connect,1,
472
    N_("Reconnect to the server. Optional arguments are db and host.")),
473
  Commands( "delimiter", 'd', com_delimiter,    1,
474
    N_("Set statement delimiter. NOTE: Takes the rest of the line as new delimiter.") ),
475
  Commands( "ego",    'G', com_ego,    0,
476
    N_("Send command to drizzle server, display result vertically.")),
477
  Commands( "exit",   'q', com_quit,   0, N_("Exit drizzle. Same as quit.")),
478
  Commands( "go",     'g', com_go,     0, N_("Send command to drizzle server.") ),
479
  Commands( "help",   'h', com_help,   0, N_("Display this help.") ),
480
  Commands( "nopager",'n', com_nopager,0, N_("Disable pager, print to stdout.") ),
481
  Commands( "notee",  't', com_notee,  0, N_("Don't write into outfile.") ),
482
  Commands( "pager",  'P', com_pager,  1,
483
    N_("Set PAGER [to_pager]. Print the query results via PAGER.") ),
484
  Commands( "print",  'p', com_print,  0, N_("Print current command.") ),
485
  Commands( "prompt", 'R', com_prompt, 1, N_("Change your drizzle prompt.")),
486
  Commands( "quit",   'q', com_quit,   0, N_("Quit drizzle.") ),
487
  Commands( "rehash", '#', com_rehash, 0, N_("Rebuild completion hash.") ),
488
  Commands( "source", '.', com_source, 1,
489
    N_("Execute an SQL script file. Takes a file name as an argument.")),
490
  Commands( "status", 's', com_status, 0, N_("Get status information from the server.")),
491
  Commands( "tee",    'T', com_tee,    1,
492
    N_("Set outfile [to_outfile]. Append everything into given outfile.") ),
493
  Commands( "use",    'u', com_use,    1,
494
    N_("Use another database. Takes database name as argument.") ),
1954 by Brian Aker
This adds the command "shutdown" to the command line client. It does require
495
  Commands( "shutdown",    'u', com_shutdown,    1,
496
    N_("Shutdown the instance you are connected too.") ),
1441.3.3 by Vijay Samuel
Changes made to the struct conversion
497
  Commands( "warnings", 'W', com_warnings,  0,
498
    N_("Show warnings after every statement.") ),
499
  Commands( "nowarning", 'w', com_nowarnings, 0,
500
    N_("Don't show warnings after every statement.") ),
1 by brian
clean slate
501
  /* Get bash-like expansion for some commands */
1441.3.3 by Vijay Samuel
Changes made to the struct conversion
502
  Commands( "create table",     0, 0, 0, ""),
503
  Commands( "create database",  0, 0, 0, ""),
504
  Commands( "show databases",   0, 0, 0, ""),
505
  Commands( "show fields from", 0, 0, 0, ""),
506
  Commands( "show keys from",   0, 0, 0, ""),
507
  Commands( "show tables",      0, 0, 0, ""),
508
  Commands( "load data from",   0, 0, 0, ""),
509
  Commands( "alter table",      0, 0, 0, ""),
510
  Commands( "set option",       0, 0, 0, ""),
511
  Commands( "lock tables",      0, 0, 0, ""),
512
  Commands( "unlock tables",    0, 0, 0, ""),
1 by brian
clean slate
513
  /* generated 2006-12-28.  Refresh occasionally from lexer. */
1441.3.3 by Vijay Samuel
Changes made to the struct conversion
514
  Commands( "ACTION", 0, 0, 0, ""),
515
  Commands( "ADD", 0, 0, 0, ""),
516
  Commands( "AFTER", 0, 0, 0, ""),
517
  Commands( "AGAINST", 0, 0, 0, ""),
518
  Commands( "AGGREGATE", 0, 0, 0, ""),
519
  Commands( "ALL", 0, 0, 0, ""),
520
  Commands( "ALGORITHM", 0, 0, 0, ""),
521
  Commands( "ALTER", 0, 0, 0, ""),
522
  Commands( "ANALYZE", 0, 0, 0, ""),
523
  Commands( "AND", 0, 0, 0, ""),
524
  Commands( "ANY", 0, 0, 0, ""),
525
  Commands( "AS", 0, 0, 0, ""),
526
  Commands( "ASC", 0, 0, 0, ""),
527
  Commands( "ASCII", 0, 0, 0, ""),
528
  Commands( "ASENSITIVE", 0, 0, 0, ""),
529
  Commands( "AUTO_INCREMENT", 0, 0, 0, ""),
530
  Commands( "AVG", 0, 0, 0, ""),
531
  Commands( "AVG_ROW_LENGTH", 0, 0, 0, ""),
532
  Commands( "BEFORE", 0, 0, 0, ""),
533
  Commands( "BEGIN", 0, 0, 0, ""),
534
  Commands( "BETWEEN", 0, 0, 0, ""),
535
  Commands( "BIGINT", 0, 0, 0, ""),
536
  Commands( "BINARY", 0, 0, 0, ""),
537
  Commands( "BIT", 0, 0, 0, ""),
538
  Commands( "BLOB", 0, 0, 0, ""),
539
  Commands( "BOOL", 0, 0, 0, ""),
540
  Commands( "BOOLEAN", 0, 0, 0, ""),
541
  Commands( "BOTH", 0, 0, 0, ""),
542
  Commands( "BTREE", 0, 0, 0, ""),
543
  Commands( "BY", 0, 0, 0, ""),
544
  Commands( "BYTE", 0, 0, 0, ""),
545
  Commands( "CACHE", 0, 0, 0, ""),
546
  Commands( "CALL", 0, 0, 0, ""),
547
  Commands( "CASCADE", 0, 0, 0, ""),
548
  Commands( "CASCADED", 0, 0, 0, ""),
549
  Commands( "CASE", 0, 0, 0, ""),
550
  Commands( "CHAIN", 0, 0, 0, ""),
551
  Commands( "CHANGE", 0, 0, 0, ""),
552
  Commands( "CHANGED", 0, 0, 0, ""),
553
  Commands( "CHAR", 0, 0, 0, ""),
554
  Commands( "CHARACTER", 0, 0, 0, ""),
555
  Commands( "CHECK", 0, 0, 0, ""),
556
  Commands( "CHECKSUM", 0, 0, 0, ""),
557
  Commands( "CLIENT", 0, 0, 0, ""),
558
  Commands( "CLOSE", 0, 0, 0, ""),
559
  Commands( "COLLATE", 0, 0, 0, ""),
560
  Commands( "COLLATION", 0, 0, 0, ""),
561
  Commands( "COLUMN", 0, 0, 0, ""),
562
  Commands( "COLUMNS", 0, 0, 0, ""),
563
  Commands( "COMMENT", 0, 0, 0, ""),
564
  Commands( "COMMIT", 0, 0, 0, ""),
565
  Commands( "COMMITTED", 0, 0, 0, ""),
566
  Commands( "COMPACT", 0, 0, 0, ""),
567
  Commands( "COMPRESSED", 0, 0, 0, ""),
568
  Commands( "CONCURRENT", 0, 0, 0, ""),
569
  Commands( "CONDITION", 0, 0, 0, ""),
570
  Commands( "CONNECTION", 0, 0, 0, ""),
571
  Commands( "CONSISTENT", 0, 0, 0, ""),
572
  Commands( "CONSTRAINT", 0, 0, 0, ""),
573
  Commands( "CONTAINS", 0, 0, 0, ""),
574
  Commands( "CONTINUE", 0, 0, 0, ""),
575
  Commands( "CONVERT", 0, 0, 0, ""),
576
  Commands( "CREATE", 0, 0, 0, ""),
577
  Commands( "CROSS", 0, 0, 0, ""),
578
  Commands( "CUBE", 0, 0, 0, ""),
579
  Commands( "CURRENT_DATE", 0, 0, 0, ""),
580
  Commands( "CURRENT_TIMESTAMP", 0, 0, 0, ""),
581
  Commands( "CURRENT_USER", 0, 0, 0, ""),
582
  Commands( "CURSOR", 0, 0, 0, ""),
583
  Commands( "DATA", 0, 0, 0, ""),
584
  Commands( "DATABASE", 0, 0, 0, ""),
585
  Commands( "DATABASES", 0, 0, 0, ""),
586
  Commands( "DATE", 0, 0, 0, ""),
587
  Commands( "DATETIME", 0, 0, 0, ""),
588
  Commands( "DAY", 0, 0, 0, ""),
589
  Commands( "DAY_HOUR", 0, 0, 0, ""),
590
  Commands( "DAY_MICROSECOND", 0, 0, 0, ""),
591
  Commands( "DAY_MINUTE", 0, 0, 0, ""),
592
  Commands( "DAY_SECOND", 0, 0, 0, ""),
593
  Commands( "DEALLOCATE", 0, 0, 0, ""),
594
  Commands( "DEC", 0, 0, 0, ""),
595
  Commands( "DECIMAL", 0, 0, 0, ""),
596
  Commands( "DECLARE", 0, 0, 0, ""),
597
  Commands( "DEFAULT", 0, 0, 0, ""),
598
  Commands( "DEFINER", 0, 0, 0, ""),
599
  Commands( "DELAYED", 0, 0, 0, ""),
600
  Commands( "DELETE", 0, 0, 0, ""),
601
  Commands( "DESC", 0, 0, 0, ""),
602
  Commands( "DESCRIBE", 0, 0, 0, ""),
603
  Commands( "DETERMINISTIC", 0, 0, 0, ""),
604
  Commands( "DISABLE", 0, 0, 0, ""),
605
  Commands( "DISCARD", 0, 0, 0, ""),
606
  Commands( "DISTINCT", 0, 0, 0, ""),
607
  Commands( "DISTINCTROW", 0, 0, 0, ""),
608
  Commands( "DIV", 0, 0, 0, ""),
609
  Commands( "DOUBLE", 0, 0, 0, ""),
610
  Commands( "DROP", 0, 0, 0, ""),
611
  Commands( "DUMPFILE", 0, 0, 0, ""),
612
  Commands( "DUPLICATE", 0, 0, 0, ""),
613
  Commands( "DYNAMIC", 0, 0, 0, ""),
614
  Commands( "EACH", 0, 0, 0, ""),
615
  Commands( "ELSE", 0, 0, 0, ""),
616
  Commands( "ELSEIF", 0, 0, 0, ""),
617
  Commands( "ENABLE", 0, 0, 0, ""),
618
  Commands( "ENCLOSED", 0, 0, 0, ""),
619
  Commands( "END", 0, 0, 0, ""),
620
  Commands( "ENGINE", 0, 0, 0, ""),
621
  Commands( "ENGINES", 0, 0, 0, ""),
622
  Commands( "ENUM", 0, 0, 0, ""),
623
  Commands( "ERRORS", 0, 0, 0, ""),
624
  Commands( "ESCAPE", 0, 0, 0, ""),
625
  Commands( "ESCAPED", 0, 0, 0, ""),
626
  Commands( "EXISTS", 0, 0, 0, ""),
627
  Commands( "EXIT", 0, 0, 0, ""),
628
  Commands( "EXPLAIN", 0, 0, 0, ""),
629
  Commands( "EXTENDED", 0, 0, 0, ""),
630
  Commands( "FALSE", 0, 0, 0, ""),
631
  Commands( "FAST", 0, 0, 0, ""),
632
  Commands( "FETCH", 0, 0, 0, ""),
633
  Commands( "FIELDS", 0, 0, 0, ""),
634
  Commands( "FILE", 0, 0, 0, ""),
635
  Commands( "FIRST", 0, 0, 0, ""),
636
  Commands( "FIXED", 0, 0, 0, ""),
637
  Commands( "FLOAT", 0, 0, 0, ""),
638
  Commands( "FLOAT4", 0, 0, 0, ""),
639
  Commands( "FLOAT8", 0, 0, 0, ""),
640
  Commands( "FLUSH", 0, 0, 0, ""),
641
  Commands( "FOR", 0, 0, 0, ""),
642
  Commands( "FORCE", 0, 0, 0, ""),
643
  Commands( "FOREIGN", 0, 0, 0, ""),
644
  Commands( "FOUND", 0, 0, 0, ""),
645
  Commands( "FRAC_SECOND", 0, 0, 0, ""),
646
  Commands( "FROM", 0, 0, 0, ""),
647
  Commands( "FULL", 0, 0, 0, ""),
648
  Commands( "FUNCTION", 0, 0, 0, ""),
649
  Commands( "GLOBAL", 0, 0, 0, ""),
650
  Commands( "GRANT", 0, 0, 0, ""),
651
  Commands( "GRANTS", 0, 0, 0, ""),
652
  Commands( "GROUP", 0, 0, 0, ""),
653
  Commands( "HANDLER", 0, 0, 0, ""),
654
  Commands( "HASH", 0, 0, 0, ""),
655
  Commands( "HAVING", 0, 0, 0, ""),
656
  Commands( "HELP", 0, 0, 0, ""),
657
  Commands( "HIGH_PRIORITY", 0, 0, 0, ""),
658
  Commands( "HOSTS", 0, 0, 0, ""),
659
  Commands( "HOUR", 0, 0, 0, ""),
660
  Commands( "HOUR_MICROSECOND", 0, 0, 0, ""),
661
  Commands( "HOUR_MINUTE", 0, 0, 0, ""),
662
  Commands( "HOUR_SECOND", 0, 0, 0, ""),
663
  Commands( "IDENTIFIED", 0, 0, 0, ""),
664
  Commands( "IF", 0, 0, 0, ""),
665
  Commands( "IGNORE", 0, 0, 0, ""),
666
  Commands( "IMPORT", 0, 0, 0, ""),
667
  Commands( "IN", 0, 0, 0, ""),
668
  Commands( "INDEX", 0, 0, 0, ""),
669
  Commands( "INDEXES", 0, 0, 0, ""),
670
  Commands( "INFILE", 0, 0, 0, ""),
671
  Commands( "INNER", 0, 0, 0, ""),
672
  Commands( "INNOBASE", 0, 0, 0, ""),
673
  Commands( "INNODB", 0, 0, 0, ""),
674
  Commands( "INOUT", 0, 0, 0, ""),
675
  Commands( "INSENSITIVE", 0, 0, 0, ""),
676
  Commands( "INSERT", 0, 0, 0, ""),
677
  Commands( "INSERT_METHOD", 0, 0, 0, ""),
678
  Commands( "INT", 0, 0, 0, ""),
679
  Commands( "INT1", 0, 0, 0, ""),
680
  Commands( "INT2", 0, 0, 0, ""),
681
  Commands( "INT3", 0, 0, 0, ""),
682
  Commands( "INT4", 0, 0, 0, ""),
683
  Commands( "INT8", 0, 0, 0, ""),
684
  Commands( "INTEGER", 0, 0, 0, ""),
685
  Commands( "INTERVAL", 0, 0, 0, ""),
686
  Commands( "INTO", 0, 0, 0, ""),
687
  Commands( "IO_THREAD", 0, 0, 0, ""),
688
  Commands( "IS", 0, 0, 0, ""),
689
  Commands( "ISOLATION", 0, 0, 0, ""),
690
  Commands( "ISSUER", 0, 0, 0, ""),
691
  Commands( "ITERATE", 0, 0, 0, ""),
692
  Commands( "INVOKER", 0, 0, 0, ""),
693
  Commands( "JOIN", 0, 0, 0, ""),
694
  Commands( "KEY", 0, 0, 0, ""),
695
  Commands( "KEYS", 0, 0, 0, ""),
696
  Commands( "KILL", 0, 0, 0, ""),
697
  Commands( "LANGUAGE", 0, 0, 0, ""),
698
  Commands( "LAST", 0, 0, 0, ""),
699
  Commands( "LEADING", 0, 0, 0, ""),
700
  Commands( "LEAVE", 0, 0, 0, ""),
701
  Commands( "LEAVES", 0, 0, 0, ""),
702
  Commands( "LEFT", 0, 0, 0, ""),
703
  Commands( "LEVEL", 0, 0, 0, ""),
704
  Commands( "LIKE", 0, 0, 0, ""),
705
  Commands( "LIMIT", 0, 0, 0, ""),
706
  Commands( "LINES", 0, 0, 0, ""),
707
  Commands( "LINESTRING", 0, 0, 0, ""),
708
  Commands( "LOAD", 0, 0, 0, ""),
709
  Commands( "LOCAL", 0, 0, 0, ""),
710
  Commands( "LOCALTIMESTAMP", 0, 0, 0, ""),
711
  Commands( "LOCK", 0, 0, 0, ""),
712
  Commands( "LOCKS", 0, 0, 0, ""),
713
  Commands( "LOGS", 0, 0, 0, ""),
714
  Commands( "LONG", 0, 0, 0, ""),
715
  Commands( "LOOP", 0, 0, 0, ""),
716
  Commands( "MATCH", 0, 0, 0, ""),
717
  Commands( "MAX_CONNECTIONS_PER_HOUR", 0, 0, 0, ""),
718
  Commands( "MAX_QUERIES_PER_HOUR", 0, 0, 0, ""),
719
  Commands( "MAX_ROWS", 0, 0, 0, ""),
720
  Commands( "MAX_UPDATES_PER_HOUR", 0, 0, 0, ""),
721
  Commands( "MAX_USER_CONNECTIONS", 0, 0, 0, ""),
722
  Commands( "MEDIUM", 0, 0, 0, ""),
723
  Commands( "MERGE", 0, 0, 0, ""),
724
  Commands( "MICROSECOND", 0, 0, 0, ""),
725
  Commands( "MIGRATE", 0, 0, 0, ""),
726
  Commands( "MINUTE", 0, 0, 0, ""),
727
  Commands( "MINUTE_MICROSECOND", 0, 0, 0, ""),
728
  Commands( "MINUTE_SECOND", 0, 0, 0, ""),
729
  Commands( "MIN_ROWS", 0, 0, 0, ""),
730
  Commands( "MOD", 0, 0, 0, ""),
731
  Commands( "MODE", 0, 0, 0, ""),
732
  Commands( "MODIFIES", 0, 0, 0, ""),
733
  Commands( "MODIFY", 0, 0, 0, ""),
734
  Commands( "MONTH", 0, 0, 0, ""),
735
  Commands( "MULTILINESTRING", 0, 0, 0, ""),
736
  Commands( "MULTIPOINT", 0, 0, 0, ""),
737
  Commands( "MULTIPOLYGON", 0, 0, 0, ""),
738
  Commands( "MUTEX", 0, 0, 0, ""),
739
  Commands( "NAME", 0, 0, 0, ""),
740
  Commands( "NAMES", 0, 0, 0, ""),
741
  Commands( "NATIONAL", 0, 0, 0, ""),
742
  Commands( "NATURAL", 0, 0, 0, ""),
743
  Commands( "NCHAR", 0, 0, 0, ""),
744
  Commands( "NEW", 0, 0, 0, ""),
745
  Commands( "NEXT", 0, 0, 0, ""),
746
  Commands( "NO", 0, 0, 0, ""),
747
  Commands( "NONE", 0, 0, 0, ""),
748
  Commands( "NOT", 0, 0, 0, ""),
749
  Commands( "NULL", 0, 0, 0, ""),
750
  Commands( "NUMERIC", 0, 0, 0, ""),
751
  Commands( "NVARCHAR", 0, 0, 0, ""),
752
  Commands( "OFFSET", 0, 0, 0, ""),
753
  Commands( "ON", 0, 0, 0, ""),
754
  Commands( "ONE", 0, 0, 0, ""),
755
  Commands( "ONE_SHOT", 0, 0, 0, ""),
756
  Commands( "OPEN", 0, 0, 0, ""),
757
  Commands( "OPTIMIZE", 0, 0, 0, ""),
758
  Commands( "OPTION", 0, 0, 0, ""),
759
  Commands( "OPTIONALLY", 0, 0, 0, ""),
760
  Commands( "OR", 0, 0, 0, ""),
761
  Commands( "ORDER", 0, 0, 0, ""),
762
  Commands( "OUT", 0, 0, 0, ""),
763
  Commands( "OUTER", 0, 0, 0, ""),
764
  Commands( "OUTFILE", 0, 0, 0, ""),
765
  Commands( "PACK_KEYS", 0, 0, 0, ""),
766
  Commands( "PARTIAL", 0, 0, 0, ""),
767
  Commands( "PASSWORD", 0, 0, 0, ""),
768
  Commands( "PHASE", 0, 0, 0, ""),
769
  Commands( "PRECISION", 0, 0, 0, ""),
770
  Commands( "PREPARE", 0, 0, 0, ""),
771
  Commands( "PREV", 0, 0, 0, ""),
772
  Commands( "PRIMARY", 0, 0, 0, ""),
773
  Commands( "PRIVILEGES", 0, 0, 0, ""),
774
  Commands( "PROCEDURE", 0, 0, 0, ""),
775
  Commands( "PROCESS", 0, 0, 0, ""),
776
  Commands( "PROCESSLIST", 0, 0, 0, ""),
777
  Commands( "PURGE", 0, 0, 0, ""),
778
  Commands( "QUARTER", 0, 0, 0, ""),
779
  Commands( "QUERY", 0, 0, 0, ""),
780
  Commands( "QUICK", 0, 0, 0, ""),
781
  Commands( "READ", 0, 0, 0, ""),
782
  Commands( "READS", 0, 0, 0, ""),
783
  Commands( "REAL", 0, 0, 0, ""),
784
  Commands( "RECOVER", 0, 0, 0, ""),
785
  Commands( "REDUNDANT", 0, 0, 0, ""),
786
  Commands( "REFERENCES", 0, 0, 0, ""),
787
  Commands( "REGEXP", 0, 0, 0, ""),
788
  Commands( "RELEASE", 0, 0, 0, ""),
789
  Commands( "RELOAD", 0, 0, 0, ""),
790
  Commands( "RENAME", 0, 0, 0, ""),
791
  Commands( "REPAIR", 0, 0, 0, ""),
792
  Commands( "REPEATABLE", 0, 0, 0, ""),
793
  Commands( "REPLACE", 0, 0, 0, ""),
794
  Commands( "REPEAT", 0, 0, 0, ""),
795
  Commands( "REQUIRE", 0, 0, 0, ""),
796
  Commands( "RESET", 0, 0, 0, ""),
797
  Commands( "RESTORE", 0, 0, 0, ""),
798
  Commands( "RESTRICT", 0, 0, 0, ""),
799
  Commands( "RESUME", 0, 0, 0, ""),
800
  Commands( "RETURN", 0, 0, 0, ""),
801
  Commands( "RETURNS", 0, 0, 0, ""),
802
  Commands( "REVOKE", 0, 0, 0, ""),
803
  Commands( "RIGHT", 0, 0, 0, ""),
804
  Commands( "RLIKE", 0, 0, 0, ""),
805
  Commands( "ROLLBACK", 0, 0, 0, ""),
806
  Commands( "ROLLUP", 0, 0, 0, ""),
807
  Commands( "ROUTINE", 0, 0, 0, ""),
808
  Commands( "ROW", 0, 0, 0, ""),
809
  Commands( "ROWS", 0, 0, 0, ""),
810
  Commands( "ROW_FORMAT", 0, 0, 0, ""),
811
  Commands( "RTREE", 0, 0, 0, ""),
812
  Commands( "SAVEPOINT", 0, 0, 0, ""),
813
  Commands( "SCHEMA", 0, 0, 0, ""),
814
  Commands( "SCHEMAS", 0, 0, 0, ""),
815
  Commands( "SECOND", 0, 0, 0, ""),
816
  Commands( "SECOND_MICROSECOND", 0, 0, 0, ""),
817
  Commands( "SECURITY", 0, 0, 0, ""),
818
  Commands( "SELECT", 0, 0, 0, ""),
819
  Commands( "SENSITIVE", 0, 0, 0, ""),
820
  Commands( "SEPARATOR", 0, 0, 0, ""),
821
  Commands( "SERIAL", 0, 0, 0, ""),
822
  Commands( "SERIALIZABLE", 0, 0, 0, ""),
823
  Commands( "SESSION", 0, 0, 0, ""),
824
  Commands( "SET", 0, 0, 0, ""),
825
  Commands( "SHARE", 0, 0, 0, ""),
826
  Commands( "SHOW", 0, 0, 0, ""),
827
  Commands( "SHUTDOWN", 0, 0, 0, ""),
828
  Commands( "SIGNED", 0, 0, 0, ""),
829
  Commands( "SIMPLE", 0, 0, 0, ""),
830
  Commands( "SLAVE", 0, 0, 0, ""),
831
  Commands( "SNAPSHOT", 0, 0, 0, ""),
832
  Commands( "SOME", 0, 0, 0, ""),
833
  Commands( "SONAME", 0, 0, 0, ""),
834
  Commands( "SOUNDS", 0, 0, 0, ""),
835
  Commands( "SPATIAL", 0, 0, 0, ""),
836
  Commands( "SPECIFIC", 0, 0, 0, ""),
837
  Commands( "SQL", 0, 0, 0, ""),
838
  Commands( "SQLEXCEPTION", 0, 0, 0, ""),
839
  Commands( "SQLSTATE", 0, 0, 0, ""),
840
  Commands( "SQLWARNING", 0, 0, 0, ""),
841
  Commands( "SQL_BIG_RESULT", 0, 0, 0, ""),
842
  Commands( "SQL_BUFFER_RESULT", 0, 0, 0, ""),
843
  Commands( "SQL_CACHE", 0, 0, 0, ""),
844
  Commands( "SQL_CALC_FOUND_ROWS", 0, 0, 0, ""),
845
  Commands( "SQL_NO_CACHE", 0, 0, 0, ""),
846
  Commands( "SQL_SMALL_RESULT", 0, 0, 0, ""),
847
  Commands( "SQL_THREAD", 0, 0, 0, ""),
848
  Commands( "SQL_TSI_FRAC_SECOND", 0, 0, 0, ""),
849
  Commands( "SQL_TSI_SECOND", 0, 0, 0, ""),
850
  Commands( "SQL_TSI_MINUTE", 0, 0, 0, ""),
851
  Commands( "SQL_TSI_HOUR", 0, 0, 0, ""),
852
  Commands( "SQL_TSI_DAY", 0, 0, 0, ""),
853
  Commands( "SQL_TSI_WEEK", 0, 0, 0, ""),
854
  Commands( "SQL_TSI_MONTH", 0, 0, 0, ""),
855
  Commands( "SQL_TSI_QUARTER", 0, 0, 0, ""),
856
  Commands( "SQL_TSI_YEAR", 0, 0, 0, ""),
857
  Commands( "SSL", 0, 0, 0, ""),
858
  Commands( "START", 0, 0, 0, ""),
859
  Commands( "STARTING", 0, 0, 0, ""),
860
  Commands( "STATUS", 0, 0, 0, ""),
861
  Commands( "STOP", 0, 0, 0, ""),
862
  Commands( "STORAGE", 0, 0, 0, ""),
863
  Commands( "STRAIGHT_JOIN", 0, 0, 0, ""),
864
  Commands( "STRING", 0, 0, 0, ""),
865
  Commands( "STRIPED", 0, 0, 0, ""),
866
  Commands( "SUBJECT", 0, 0, 0, ""),
867
  Commands( "SUPER", 0, 0, 0, ""),
868
  Commands( "SUSPEND", 0, 0, 0, ""),
869
  Commands( "TABLE", 0, 0, 0, ""),
870
  Commands( "TABLES", 0, 0, 0, ""),
871
  Commands( "TABLESPACE", 0, 0, 0, ""),
872
  Commands( "TEMPORARY", 0, 0, 0, ""),
873
  Commands( "TEMPTABLE", 0, 0, 0, ""),
874
  Commands( "TERMINATED", 0, 0, 0, ""),
875
  Commands( "TEXT", 0, 0, 0, ""),
876
  Commands( "THEN", 0, 0, 0, ""),
877
  Commands( "TIMESTAMP", 0, 0, 0, ""),
878
  Commands( "TIMESTAMPADD", 0, 0, 0, ""),
879
  Commands( "TIMESTAMPDIFF", 0, 0, 0, ""),
880
  Commands( "TO", 0, 0, 0, ""),
881
  Commands( "TRAILING", 0, 0, 0, ""),
882
  Commands( "TRANSACTION", 0, 0, 0, ""),
883
  Commands( "TRUE", 0, 0, 0, ""),
884
  Commands( "TRUNCATE", 0, 0, 0, ""),
885
  Commands( "TYPE", 0, 0, 0, ""),
886
  Commands( "TYPES", 0, 0, 0, ""),
887
  Commands( "UNCOMMITTED", 0, 0, 0, ""),
888
  Commands( "UNDEFINED", 0, 0, 0, ""),
889
  Commands( "UNDO", 0, 0, 0, ""),
890
  Commands( "UNICODE", 0, 0, 0, ""),
891
  Commands( "UNION", 0, 0, 0, ""),
892
  Commands( "UNIQUE", 0, 0, 0, ""),
893
  Commands( "UNKNOWN", 0, 0, 0, ""),
894
  Commands( "UNLOCK", 0, 0, 0, ""),
895
  Commands( "UNTIL", 0, 0, 0, ""),
896
  Commands( "UPDATE", 0, 0, 0, ""),
897
  Commands( "UPGRADE", 0, 0, 0, ""),
898
  Commands( "USAGE", 0, 0, 0, ""),
899
  Commands( "USE", 0, 0, 0, ""),
900
  Commands( "USER", 0, 0, 0, ""),
901
  Commands( "USER_RESOURCES", 0, 0, 0, ""),
902
  Commands( "USING", 0, 0, 0, ""),
903
  Commands( "UTC_DATE", 0, 0, 0, ""),
904
  Commands( "UTC_TIMESTAMP", 0, 0, 0, ""),
905
  Commands( "VALUE", 0, 0, 0, ""),
906
  Commands( "VALUES", 0, 0, 0, ""),
907
  Commands( "VARBINARY", 0, 0, 0, ""),
908
  Commands( "VARCHAR", 0, 0, 0, ""),
909
  Commands( "VARCHARACTER", 0, 0, 0, ""),
910
  Commands( "VARIABLES", 0, 0, 0, ""),
911
  Commands( "VARYING", 0, 0, 0, ""),
912
  Commands( "WARNINGS", 0, 0, 0, ""),
913
  Commands( "WEEK", 0, 0, 0, ""),
914
  Commands( "WHEN", 0, 0, 0, ""),
915
  Commands( "WHERE", 0, 0, 0, ""),
916
  Commands( "WHILE", 0, 0, 0, ""),
917
  Commands( "VIEW", 0, 0, 0, ""),
918
  Commands( "WITH", 0, 0, 0, ""),
919
  Commands( "WORK", 0, 0, 0, ""),
920
  Commands( "WRITE", 0, 0, 0, ""),
921
  Commands( "XOR", 0, 0, 0, ""),
922
  Commands( "XA", 0, 0, 0, ""),
923
  Commands( "YEAR", 0, 0, 0, ""),
924
  Commands( "YEAR_MONTH", 0, 0, 0, ""),
925
  Commands( "ZEROFILL", 0, 0, 0, ""),
926
  Commands( "ABS", 0, 0, 0, ""),
927
  Commands( "ACOS", 0, 0, 0, ""),
928
  Commands( "ADDDATE", 0, 0, 0, ""),
929
  Commands( "AREA", 0, 0, 0, ""),
930
  Commands( "ASIN", 0, 0, 0, ""),
931
  Commands( "ASBINARY", 0, 0, 0, ""),
932
  Commands( "ASTEXT", 0, 0, 0, ""),
933
  Commands( "ATAN", 0, 0, 0, ""),
934
  Commands( "ATAN2", 0, 0, 0, ""),
935
  Commands( "BENCHMARK", 0, 0, 0, ""),
936
  Commands( "BIN", 0, 0, 0, ""),
937
  Commands( "BIT_OR", 0, 0, 0, ""),
938
  Commands( "BIT_AND", 0, 0, 0, ""),
939
  Commands( "BIT_XOR", 0, 0, 0, ""),
940
  Commands( "CAST", 0, 0, 0, ""),
941
  Commands( "CEIL", 0, 0, 0, ""),
942
  Commands( "CEILING", 0, 0, 0, ""),
943
  Commands( "CENTROID", 0, 0, 0, ""),
944
  Commands( "CHAR_LENGTH", 0, 0, 0, ""),
945
  Commands( "CHARACTER_LENGTH", 0, 0, 0, ""),
946
  Commands( "COALESCE", 0, 0, 0, ""),
947
  Commands( "COERCIBILITY", 0, 0, 0, ""),
948
  Commands( "COMPRESS", 0, 0, 0, ""),
949
  Commands( "CONCAT", 0, 0, 0, ""),
950
  Commands( "CONCAT_WS", 0, 0, 0, ""),
951
  Commands( "CONNECTION_ID", 0, 0, 0, ""),
952
  Commands( "CONV", 0, 0, 0, ""),
953
  Commands( "CONVERT_TZ", 0, 0, 0, ""),
954
  Commands( "COUNT", 0, 0, 0, ""),
955
  Commands( "COS", 0, 0, 0, ""),
956
  Commands( "COT", 0, 0, 0, ""),
957
  Commands( "CRC32", 0, 0, 0, ""),
958
  Commands( "CROSSES", 0, 0, 0, ""),
959
  Commands( "CURDATE", 0, 0, 0, ""),
960
  Commands( "DATE_ADD", 0, 0, 0, ""),
961
  Commands( "DATEDIFF", 0, 0, 0, ""),
962
  Commands( "DATE_FORMAT", 0, 0, 0, ""),
963
  Commands( "DATE_SUB", 0, 0, 0, ""),
964
  Commands( "DAYNAME", 0, 0, 0, ""),
965
  Commands( "DAYOFMONTH", 0, 0, 0, ""),
966
  Commands( "DAYOFWEEK", 0, 0, 0, ""),
967
  Commands( "DAYOFYEAR", 0, 0, 0, ""),
968
  Commands( "DECODE", 0, 0, 0, ""),
969
  Commands( "DEGREES", 0, 0, 0, ""),
970
  Commands( "DES_ENCRYPT", 0, 0, 0, ""),
971
  Commands( "DES_DECRYPT", 0, 0, 0, ""),
972
  Commands( "DIMENSION", 0, 0, 0, ""),
973
  Commands( "DISJOINT", 0, 0, 0, ""),
974
  Commands( "ELT", 0, 0, 0, ""),
975
  Commands( "ENCODE", 0, 0, 0, ""),
976
  Commands( "ENCRYPT", 0, 0, 0, ""),
977
  Commands( "ENDPOINT", 0, 0, 0, ""),
978
  Commands( "ENVELOPE", 0, 0, 0, ""),
979
  Commands( "EQUALS", 0, 0, 0, ""),
980
  Commands( "EXTERIORRING", 0, 0, 0, ""),
981
  Commands( "EXTRACT", 0, 0, 0, ""),
982
  Commands( "EXP", 0, 0, 0, ""),
983
  Commands( "EXPORT_SET", 0, 0, 0, ""),
984
  Commands( "FIELD", 0, 0, 0, ""),
985
  Commands( "FIND_IN_SET", 0, 0, 0, ""),
986
  Commands( "FLOOR", 0, 0, 0, ""),
987
  Commands( "FORMAT", 0, 0, 0, ""),
988
  Commands( "FOUND_ROWS", 0, 0, 0, ""),
989
  Commands( "FROM_DAYS", 0, 0, 0, ""),
990
  Commands( "FROM_UNIXTIME", 0, 0, 0, ""),
991
  Commands( "GET_LOCK", 0, 0, 0, ""),
992
  Commands( "GLENGTH", 0, 0, 0, ""),
993
  Commands( "GREATEST", 0, 0, 0, ""),
994
  Commands( "GROUP_CONCAT", 0, 0, 0, ""),
995
  Commands( "GROUP_UNIQUE_USERS", 0, 0, 0, ""),
996
  Commands( "HEX", 0, 0, 0, ""),
997
  Commands( "IFNULL", 0, 0, 0, ""),
998
  Commands( "INSTR", 0, 0, 0, ""),
999
  Commands( "INTERIORRINGN", 0, 0, 0, ""),
1000
  Commands( "INTERSECTS", 0, 0, 0, ""),
1001
  Commands( "ISCLOSED", 0, 0, 0, ""),
1002
  Commands( "ISEMPTY", 0, 0, 0, ""),
1003
  Commands( "ISNULL", 0, 0, 0, ""),
1004
  Commands( "IS_FREE_LOCK", 0, 0, 0, ""),
1005
  Commands( "IS_USED_LOCK", 0, 0, 0, ""),
1006
  Commands( "LAST_INSERT_ID", 0, 0, 0, ""),
1007
  Commands( "ISSIMPLE", 0, 0, 0, ""),
1008
  Commands( "LAST_DAY", 0, 0, 0, ""),
1009
  Commands( "LCASE", 0, 0, 0, ""),
1010
  Commands( "LEAST", 0, 0, 0, ""),
1011
  Commands( "LENGTH", 0, 0, 0, ""),
1012
  Commands( "LN", 0, 0, 0, ""),
1013
  Commands( "LOAD_FILE", 0, 0, 0, ""),
1014
  Commands( "LOCATE", 0, 0, 0, ""),
1015
  Commands( "LOG", 0, 0, 0, ""),
1016
  Commands( "LOG2", 0, 0, 0, ""),
1017
  Commands( "LOG10", 0, 0, 0, ""),
1018
  Commands( "LOWER", 0, 0, 0, ""),
1019
  Commands( "LPAD", 0, 0, 0, ""),
1020
  Commands( "LTRIM", 0, 0, 0, ""),
1021
  Commands( "MAKE_SET", 0, 0, 0, ""),
1022
  Commands( "MAKEDATE", 0, 0, 0, ""),
1023
  Commands( "MASTER_POS_WAIT", 0, 0, 0, ""),
1024
  Commands( "MAX", 0, 0, 0, ""),
1025
  Commands( "MBRCONTAINS", 0, 0, 0, ""),
1026
  Commands( "MBRDISJOINT", 0, 0, 0, ""),
1027
  Commands( "MBREQUAL", 0, 0, 0, ""),
1028
  Commands( "MBRINTERSECTS", 0, 0, 0, ""),
1029
  Commands( "MBROVERLAPS", 0, 0, 0, ""),
1030
  Commands( "MBRTOUCHES", 0, 0, 0, ""),
1031
  Commands( "MBRWITHIN", 0, 0, 0, ""),
1032
  Commands( "MD5", 0, 0, 0, ""),
1033
  Commands( "MID", 0, 0, 0, ""),
1034
  Commands( "MIN", 0, 0, 0, ""),
1035
  Commands( "MONTHNAME", 0, 0, 0, ""),
1036
  Commands( "NAME_CONST", 0, 0, 0, ""),
1037
  Commands( "NOW", 0, 0, 0, ""),
1038
  Commands( "NULLIF", 0, 0, 0, ""),
1039
  Commands( "NUMPOINTS", 0, 0, 0, ""),
1040
  Commands( "OCTET_LENGTH", 0, 0, 0, ""),
1041
  Commands( "OCT", 0, 0, 0, ""),
1042
  Commands( "ORD", 0, 0, 0, ""),
1043
  Commands( "OVERLAPS", 0, 0, 0, ""),
1044
  Commands( "PERIOD_ADD", 0, 0, 0, ""),
1045
  Commands( "PERIOD_DIFF", 0, 0, 0, ""),
1046
  Commands( "PI", 0, 0, 0, ""),
1047
  Commands( "POINTN", 0, 0, 0, ""),
1048
  Commands( "POSITION", 0, 0, 0, ""),
1049
  Commands( "POW", 0, 0, 0, ""),
1050
  Commands( "POWER", 0, 0, 0, ""),
1051
  Commands( "QUOTE", 0, 0, 0, ""),
1052
  Commands( "RADIANS", 0, 0, 0, ""),
1053
  Commands( "RAND", 0, 0, 0, ""),
1054
  Commands( "RELEASE_LOCK", 0, 0, 0, ""),
1055
  Commands( "REVERSE", 0, 0, 0, ""),
1056
  Commands( "ROUND", 0, 0, 0, ""),
1057
  Commands( "ROW_COUNT", 0, 0, 0, ""),
1058
  Commands( "RPAD", 0, 0, 0, ""),
1059
  Commands( "RTRIM", 0, 0, 0, ""),
1060
  Commands( "SESSION_USER", 0, 0, 0, ""),
1061
  Commands( "SUBDATE", 0, 0, 0, ""),
1062
  Commands( "SIGN", 0, 0, 0, ""),
1063
  Commands( "SIN", 0, 0, 0, ""),
1064
  Commands( "SHA", 0, 0, 0, ""),
1065
  Commands( "SHA1", 0, 0, 0, ""),
1066
  Commands( "SLEEP", 0, 0, 0, ""),
1067
  Commands( "SOUNDEX", 0, 0, 0, ""),
1068
  Commands( "SPACE", 0, 0, 0, ""),
1069
  Commands( "SQRT", 0, 0, 0, ""),
1070
  Commands( "SRID", 0, 0, 0, ""),
1071
  Commands( "STARTPOINT", 0, 0, 0, ""),
1072
  Commands( "STD", 0, 0, 0, ""),
1073
  Commands( "STDDEV", 0, 0, 0, ""),
1074
  Commands( "STDDEV_POP", 0, 0, 0, ""),
1075
  Commands( "STDDEV_SAMP", 0, 0, 0, ""),
1076
  Commands( "STR_TO_DATE", 0, 0, 0, ""),
1077
  Commands( "STRCMP", 0, 0, 0, ""),
1078
  Commands( "SUBSTR", 0, 0, 0, ""),
1079
  Commands( "SUBSTRING", 0, 0, 0, ""),
1080
  Commands( "SUBSTRING_INDEX", 0, 0, 0, ""),
1081
  Commands( "SUM", 0, 0, 0, ""),
1082
  Commands( "SYSDATE", 0, 0, 0, ""),
1083
  Commands( "SYSTEM_USER", 0, 0, 0, ""),
1084
  Commands( "TAN", 0, 0, 0, ""),
1085
  Commands( "TIME_FORMAT", 0, 0, 0, ""),
1086
  Commands( "TO_DAYS", 0, 0, 0, ""),
1087
  Commands( "TOUCHES", 0, 0, 0, ""),
1088
  Commands( "TRIM", 0, 0, 0, ""),
1089
  Commands( "UCASE", 0, 0, 0, ""),
1090
  Commands( "UNCOMPRESS", 0, 0, 0, ""),
1091
  Commands( "UNCOMPRESSED_LENGTH", 0, 0, 0, ""),
1092
  Commands( "UNHEX", 0, 0, 0, ""),
1093
  Commands( "UNIQUE_USERS", 0, 0, 0, ""),
1094
  Commands( "UNIX_TIMESTAMP", 0, 0, 0, ""),
1095
  Commands( "UPPER", 0, 0, 0, ""),
1096
  Commands( "UUID", 0, 0, 0, ""),
1097
  Commands( "VARIANCE", 0, 0, 0, ""),
1098
  Commands( "VAR_POP", 0, 0, 0, ""),
1099
  Commands( "VAR_SAMP", 0, 0, 0, ""),
1100
  Commands( "VERSION", 0, 0, 0, ""),
1101
  Commands( "WEEKDAY", 0, 0, 0, ""),
1102
  Commands( "WEEKOFYEAR", 0, 0, 0, ""),
1103
  Commands( "WITHIN", 0, 0, 0, ""),
1104
  Commands( "X", 0, 0, 0, ""),
1105
  Commands( "Y", 0, 0, 0, ""),
1106
  Commands( "YEARWEEK", 0, 0, 0, ""),
1 by brian
clean slate
1107
  /* end sentinel */
1441.3.3 by Vijay Samuel
Changes made to the struct conversion
1108
  Commands((char *)NULL,       0, 0, 0, "")
1 by brian
clean slate
1109
};
1110
1111
77.3.18 by Monty Taylor
Merged in codestyle.
1112
int history_length;
1 by brian
clean slate
1113
static int not_in_history(const char *line);
1567.5.2 by Vijay Samuel
Merge OSX fix for client drizzle.
1114
static void initialize_readline (char *name);
279.2.2 by Monty Taylor
Replaced DYNAMIC_STRING with C++ string in drizzle command line client.
1115
static void fix_history(string *final_command);
1 by brian
clean slate
1116
1441.3.1 by Vijay Samuel
all required updations have been made
1117
static Commands *find_command(const char *name,char cmd_name);
279.2.2 by Monty Taylor
Replaced DYNAMIC_STRING with C++ string in drizzle command line client.
1118
static bool add_line(string *buffer,char *line,char *in_string,
1 by brian
clean slate
1119
                     bool *ml_comment);
279.2.2 by Monty Taylor
Replaced DYNAMIC_STRING with C++ string in drizzle command line client.
1120
static void remove_cntrl(string *buffer);
928.1.1 by Eric Day
Started client changes.
1121
static void print_table_data(drizzle_result_st *result);
1122
static void print_tab_data(drizzle_result_st *result);
1123
static void print_table_data_vertically(drizzle_result_st *result);
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
1124
static void print_warnings(uint32_t error_code);
288 by Brian Aker
ulong cleanp in client apps
1125
static uint32_t start_timer(void);
1126
static void end_timer(uint32_t start_time,char *buff);
1127
static void drizzle_end_timer(uint32_t start_time,char *buff);
1 by brian
clean slate
1128
static void nice_time(double sec,char *buff,bool part_second);
779.2.11 by Monty Taylor
General build cleanup - removed cruft, removed depreated checks.
1129
extern "C" void drizzle_end(int sig);
1130
extern "C" void handle_sigint(int sig);
1 by brian
clean slate
1131
#if defined(HAVE_TERMIOS_H) && defined(GWINSZ_IN_SYS_IOCTL)
779.2.11 by Monty Taylor
General build cleanup - removed cruft, removed depreated checks.
1132
static void window_resize(int sig);
1 by brian
clean slate
1133
#endif
1134
973.1.2 by Toru Maesaka
Cosmetic fixes pointed out by Jay and added comments for doxygen
1135
/**
1136
  Shutdown the server that we are currently connected to.
1137
1138
  @retval
1441.3.1 by Vijay Samuel
all required updations have been made
1139
    true success
973.1.2 by Toru Maesaka
Cosmetic fixes pointed out by Jay and added comments for doxygen
1140
  @retval
1441.3.1 by Vijay Samuel
all required updations have been made
1141
    false failure
973.1.2 by Toru Maesaka
Cosmetic fixes pointed out by Jay and added comments for doxygen
1142
*/
973.1.1 by Toru Maesaka
Added server shutdown and ping functionalities to the drizzle client app
1143
static bool server_shutdown(void)
1144
{
1145
  drizzle_result_st result;
1146
  drizzle_return_t ret;
1147
1148
  if (verbose)
1149
  {
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
1150
    printf(_("shutting down drizzled"));
973.1.1 by Toru Maesaka
Added server shutdown and ping functionalities to the drizzle client app
1151
    if (opt_drizzle_port > 0)
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
1152
      printf(_(" on port %d"), opt_drizzle_port);
973.1.2 by Toru Maesaka
Cosmetic fixes pointed out by Jay and added comments for doxygen
1153
    printf("... ");
973.1.1 by Toru Maesaka
Added server shutdown and ping functionalities to the drizzle client app
1154
  }
1155
1156
  if (drizzle_shutdown(&con, &result, DRIZZLE_SHUTDOWN_DEFAULT,
1157
                       &ret) == NULL || ret != DRIZZLE_RETURN_OK)
1158
  {
1159
    if (ret == DRIZZLE_RETURN_ERROR_CODE)
1160
    {
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
1161
      fprintf(stderr, _("shutdown failed; error: '%s'"),
973.1.1 by Toru Maesaka
Added server shutdown and ping functionalities to the drizzle client app
1162
              drizzle_result_error(&result));
1163
      drizzle_result_free(&result);
1164
    }
1165
    else
1166
    {
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
1167
      fprintf(stderr, _("shutdown failed; error: '%s'"),
973.1.1 by Toru Maesaka
Added server shutdown and ping functionalities to the drizzle client app
1168
              drizzle_con_error(&con));
1169
    }
1170
    return false;
1171
  }
1172
1173
  drizzle_result_free(&result);
1174
1175
  if (verbose)
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
1176
    printf(_("done\n"));
973.1.1 by Toru Maesaka
Added server shutdown and ping functionalities to the drizzle client app
1177
1178
  return true;
1179
}
1180
973.1.2 by Toru Maesaka
Cosmetic fixes pointed out by Jay and added comments for doxygen
1181
/**
1182
  Ping the server that we are currently connected to.
1183
1184
  @retval
1441.3.1 by Vijay Samuel
all required updations have been made
1185
    true success
973.1.2 by Toru Maesaka
Cosmetic fixes pointed out by Jay and added comments for doxygen
1186
  @retval
1441.3.1 by Vijay Samuel
all required updations have been made
1187
    false failure
973.1.2 by Toru Maesaka
Cosmetic fixes pointed out by Jay and added comments for doxygen
1188
*/
973.1.1 by Toru Maesaka
Added server shutdown and ping functionalities to the drizzle client app
1189
static bool server_ping(void)
1190
{
1191
  drizzle_result_st result;
1192
  drizzle_return_t ret;
1193
1194
  if (drizzle_ping(&con, &result, &ret) != NULL && ret == DRIZZLE_RETURN_OK)
1195
  {
1196
    if (opt_silent < 2)
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
1197
      printf(_("drizzled is alive\n"));
973.1.1 by Toru Maesaka
Added server shutdown and ping functionalities to the drizzle client app
1198
  }
1199
  else
1200
  {
1201
    if (ret == DRIZZLE_RETURN_ERROR_CODE)
1202
    {
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
1203
      fprintf(stderr, _("ping failed; error: '%s'"),
973.1.1 by Toru Maesaka
Added server shutdown and ping functionalities to the drizzle client app
1204
              drizzle_result_error(&result));
1205
      drizzle_result_free(&result);
1206
    }
1207
    else
1208
    {
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
1209
      fprintf(stderr, _("drizzled won't answer to ping, error: '%s'"),
973.1.1 by Toru Maesaka
Added server shutdown and ping functionalities to the drizzle client app
1210
              drizzle_con_error(&con));
1211
    }
1212
    return false;
1213
  }
1214
  drizzle_result_free(&result);
1215
  return true;
1216
}
1217
973.1.2 by Toru Maesaka
Cosmetic fixes pointed out by Jay and added comments for doxygen
1218
/**
1219
  Execute command(s) specified by the user.
1220
1221
  @param error  error status of command execution.
1441.3.1 by Vijay Samuel
all required updations have been made
1222
                If an error had occurred, this variable will be set
1223
                to 1 whereas on success, it shall be set to 0. This
1224
                value will be supplied to the exit() function used
1225
                by the caller.
973.1.2 by Toru Maesaka
Cosmetic fixes pointed out by Jay and added comments for doxygen
1226
1227
  @retval
1441.3.1 by Vijay Samuel
all required updations have been made
1228
    false no commands were executed
973.1.2 by Toru Maesaka
Cosmetic fixes pointed out by Jay and added comments for doxygen
1229
  @retval
1441.3.1 by Vijay Samuel
all required updations have been made
1230
    true  at least one command was executed
973.1.2 by Toru Maesaka
Cosmetic fixes pointed out by Jay and added comments for doxygen
1231
*/
973.1.1 by Toru Maesaka
Added server shutdown and ping functionalities to the drizzle client app
1232
static bool execute_commands(int *error)
1233
{
1234
  bool executed= false;
1235
  *error= 0;
1236
1237
  if (opt_ping)
1238
  {
973.1.2 by Toru Maesaka
Cosmetic fixes pointed out by Jay and added comments for doxygen
1239
    if (server_ping() == false)
973.1.1 by Toru Maesaka
Added server shutdown and ping functionalities to the drizzle client app
1240
      *error= 1;
1241
    executed= true;
1242
  }
1243
1244
  if (opt_shutdown)
1245
  {
973.1.2 by Toru Maesaka
Cosmetic fixes pointed out by Jay and added comments for doxygen
1246
    if (server_shutdown() == false)
973.1.1 by Toru Maesaka
Added server shutdown and ping functionalities to the drizzle client app
1247
      *error= 1;
1248
    executed= true;
1249
  }
1250
  return executed;
1251
}
1252
1567.3.1 by Vijay Samuel
Refactoring of the drizzle client.
1253
static void check_timeout_value(uint32_t in_connect_timeout)
1254
{
1255
  opt_connect_timeout= 0;
1567.5.3 by Vijay Samuel
Merge patch for sql_connect() and style issue.
1256
  if (in_connect_timeout > 3600*12)
1567.3.1 by Vijay Samuel
Refactoring of the drizzle client.
1257
  {
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
1258
    cout << _("Error: Invalid Value for connect_timeout"); 
1567.3.1 by Vijay Samuel
Refactoring of the drizzle client.
1259
    exit(-1);
1260
  }
1261
  opt_connect_timeout= in_connect_timeout;
1262
}
1263
1264
static void check_max_input_line(uint32_t in_max_input_line)
1265
{
1266
  opt_max_input_line= 0;
1700.1.1 by Vijay Samuel
Merge fix for block_size corrections in client applications.
1267
  if (in_max_input_line < 4096 || in_max_input_line > (int64_t)2*1024L*1024L*1024L)
1567.3.1 by Vijay Samuel
Refactoring of the drizzle client.
1268
  {
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
1269
    cout << _("Error: Invalid Value for max_input_line");
1567.3.1 by Vijay Samuel
Refactoring of the drizzle client.
1270
    exit(-1);
1271
  }
1700.1.1 by Vijay Samuel
Merge fix for block_size corrections in client applications.
1272
  opt_max_input_line= in_max_input_line - (in_max_input_line % 1024);
1567.3.1 by Vijay Samuel
Refactoring of the drizzle client.
1273
}
1274
1 by brian
clean slate
1275
int main(int argc,char *argv[])
1276
{
1567.3.5 by Vijay Samuel
Refactored client.
1277
try
1278
{
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
1279
236.1.60 by Monty Taylor
Another stab at protecting NLS stuff.
1280
#if defined(ENABLE_NLS)
1281
# if defined(HAVE_LOCALE_H)
202.3.2 by Monty Taylor
Added gettext calls in to my_getopt.c and drizzle.c
1282
  setlocale(LC_ALL, "");
236.1.60 by Monty Taylor
Another stab at protecting NLS stuff.
1283
# endif
202.3.2 by Monty Taylor
Added gettext calls in to my_getopt.c and drizzle.c
1284
  bindtextdomain("drizzle", LOCALEDIR);
1285
  textdomain("drizzle");
236.1.60 by Monty Taylor
Another stab at protecting NLS stuff.
1286
#endif
1567.3.8 by Vijay Samuel
Refactor drizzle client.
1287
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
1288
  po::options_description commandline_options(N_("Options used only in command line"));
1567.3.8 by Vijay Samuel
Refactor drizzle client.
1289
  commandline_options.add_options()
1567.3.1 by Vijay Samuel
Refactoring of the drizzle client.
1290
  ("help,?",N_("Displays this help and exit."))
1291
  ("batch,B",N_("Don't use history file. Disable interactive behavior. (Enables --silent)"))
1292
  ("column-type-info", po::value<bool>(&column_types_flag)->default_value(false)->zero_tokens(),
1293
  N_("Display column type information."))
1294
  ("comments,c", po::value<bool>(&preserve_comments)->default_value(false)->zero_tokens(),
1295
  N_("Preserve comments. Send comments to the server. The default is --skip-comments (discard comments), enable with --comments"))
1296
  ("vertical,E", po::value<bool>(&vertical)->default_value(false)->zero_tokens(),
1297
  N_("Print the output of a query (rows) vertically."))
1298
  ("force,f", po::value<bool>(&ignore_errors)->default_value(false)->zero_tokens(),
1299
  N_("Continue even if we get an sql error."))
1300
  ("named-commands,G", po::value<bool>(&named_cmds)->default_value(false)->zero_tokens(),
1757.5.1 by Andrew Hutchings
Rework the boolean options so that true-by-default options change to --disable-option.
1301
  N_("Enable named commands. Named commands mean this program's internal commands; see drizzle> help . When enabled, the named commands can be used from any line of the query, otherwise only from the first line, before an enter."))
1567.3.1 by Vijay Samuel
Refactoring of the drizzle client.
1302
  ("no-beep,b", po::value<bool>(&opt_nobeep)->default_value(false)->zero_tokens(),
1303
  N_("Turn off beep on error."))
1757.5.1 by Andrew Hutchings
Rework the boolean options so that true-by-default options change to --disable-option.
1304
  ("disable-line-numbers", N_("Do not write line numbers for errors."))
1305
  ("disable-column-names", N_("Do not write column names in results."))
1567.3.1 by Vijay Samuel
Refactoring of the drizzle client.
1306
  ("skip-column-names,N", 
1307
  N_("Don't write column names in results. WARNING: -N is deprecated, use long version of this options instead."))
1308
  ("set-variable,O", po::value<string>(),
1309
  N_("Change the value of a variable. Please note that this option is deprecated; you can set variables directly with --variable-name=value."))
1567.3.8 by Vijay Samuel
Refactor drizzle client.
1310
  ("table,t", po::value<bool>(&output_tables)->default_value(false)->zero_tokens(),
1311
  N_("Output in table format.")) 
1757.5.1 by Andrew Hutchings
Rework the boolean options so that true-by-default options change to --disable-option.
1312
  ("safe-updates,U", po::value<bool>(&safe_updates)->default_value(false)->zero_tokens(),
1567.3.8 by Vijay Samuel
Refactor drizzle client.
1313
  N_("Only allow UPDATE and DELETE that uses keys."))
1757.5.1 by Andrew Hutchings
Rework the boolean options so that true-by-default options change to --disable-option.
1314
  ("i-am-a-dummy,U", po::value<bool>(&safe_updates)->default_value(false)->zero_tokens(),
1567.3.8 by Vijay Samuel
Refactor drizzle client.
1315
  N_("Synonym for option --safe-updates, -U."))
1923.3.4 by Andrew Hutchings
Revert -v fix
1316
  ("verbose,v", po::value<string>(&opt_verbose)->default_value(""),
1317
  N_("-v vvv implies that verbose= 3, Used to specify verbose"))
1567.3.8 by Vijay Samuel
Refactor drizzle client.
1318
  ("version,V", N_("Output version information and exit."))
1319
  ("secure-auth", po::value<bool>(&opt_secure_auth)->default_value(false)->zero_tokens(),
1320
  N_("Refuse client connecting to server if it uses old (pre-4.1.1) protocol"))
1321
  ("show-warnings", po::value<bool>(&show_warnings)->default_value(false)->zero_tokens(),
1322
  N_("Show warnings after every statement."))
1323
  ("show-progress-size", po::value<uint32_t>(&show_progress_size)->default_value(0),
1324
  N_("Number of lines before each import progress report."))
1325
  ("ping", po::value<bool>(&opt_ping)->default_value(false)->zero_tokens(),
1326
  N_("Ping the server to check if it's alive."))
1567.3.10 by Vijay Samuel
Client refactored completely using boost::program_options.
1327
  ("no-defaults", po::value<bool>()->default_value(false)->zero_tokens(),
1328
  N_("Configuration file defaults are not used if no-defaults is set"))
1567.3.8 by Vijay Samuel
Refactor drizzle client.
1329
  ;
1330
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
1331
  po::options_description drizzle_options(N_("Options specific to the drizzle client"));
1567.3.8 by Vijay Samuel
Refactor drizzle client.
1332
  drizzle_options.add_options()
1802.13.1 by Andrew Hutchings
Fix -A option in drizzle client
1333
  ("disable-auto-rehash,A",
1757.5.1 by Andrew Hutchings
Rework the boolean options so that true-by-default options change to --disable-option.
1334
  N_("Disable automatic rehashing. One doesn't need to use 'rehash' to get table and field completion, but startup and reconnecting may take a longer time."))
1567.3.8 by Vijay Samuel
Refactor drizzle client.
1335
  ("auto-vertical-output", po::value<bool>(&auto_vertical_output)->default_value(false)->zero_tokens(),
1336
  N_("Automatically switch to vertical output mode if the result is wider than the terminal width."))
1567.3.31 by Monty Taylor
The default value for database should not be "test".
1337
  ("database,D", po::value<string>(&current_db)->default_value(""),
1567.3.8 by Vijay Samuel
Refactor drizzle client.
1338
  N_("Database to use."))
1339
  ("default-character-set",po::value<string>(),
1340
  N_("(not used)"))
1341
  ("delimiter", po::value<string>(&delimiter_str)->default_value(";"),
1342
  N_("Delimiter to be used."))
1343
  ("execute,e", po::value<string>(),
1344
  N_("Execute command and quit. (Disables --force and history file)"))
1345
  ("local-infile", po::value<bool>(&opt_local_infile)->default_value(false)->zero_tokens(),
1757.5.1 by Andrew Hutchings
Rework the boolean options so that true-by-default options change to --disable-option.
1346
  N_("Enable LOAD DATA LOCAL INFILE."))
1567.3.8 by Vijay Samuel
Refactor drizzle client.
1347
  ("unbuffered,n", po::value<bool>(&unbuffered)->default_value(false)->zero_tokens(),
1348
  N_("Flush buffer after each query."))
1567.3.1 by Vijay Samuel
Refactoring of the drizzle client.
1349
  ("sigint-ignore", po::value<bool>(&opt_sigint_ignore)->default_value(false)->zero_tokens(),
1350
  N_("Ignore SIGINT (CTRL-C)"))
1351
  ("one-database,o", po::value<bool>(&one_database)->default_value(false)->zero_tokens(),
1352
  N_("Only update the default database. This is useful for skipping updates to other database in the update log."))
1353
  ("pager", po::value<string>(),
1354
  N_("Pager to use to display results. If you don't supply an option the default pager is taken from your ENV variable PAGER. Valid pagers are less, more, cat [> filename], etc. See interactive help (\\h) also. This option does not work in batch mode. Disable with --disable-pager. This option is disabled by default."))
1567.4.2 by Jay Pipes
Some style fixes and removal of deprecated stuff...
1355
  ("disable-pager", po::value<bool>(&opt_nopager)->default_value(false)->zero_tokens(),
1356
  N_("Disable pager and print to stdout. See interactive help (\\h) also."))
1567.3.1 by Vijay Samuel
Refactoring of the drizzle client.
1357
  ("prompt", po::value<string>(&current_prompt)->default_value(""),  
1358
  N_("Set the drizzle prompt to this value."))
1359
  ("quick,q", po::value<bool>(&quick)->default_value(false)->zero_tokens(),
1360
  N_("Don't cache result, print it row by row. This may slow down the server if the output is suspended. Doesn't use history file."))
1361
  ("raw,r", po::value<bool>(&opt_raw_data)->default_value(false)->zero_tokens(),
1362
  N_("Write fields without conversion. Used with --batch.")) 
1757.5.1 by Andrew Hutchings
Rework the boolean options so that true-by-default options change to --disable-option.
1363
  ("disable-reconnect", N_("Do not reconnect if the connection is lost."))
1810.3.1 by Andrew Hutchings
Don't do completion hash rebuild on --shutdown
1364
  ("shutdown", po::value<bool>()->zero_tokens(),
1567.3.1 by Vijay Samuel
Refactoring of the drizzle client.
1365
  N_("Shutdown the server"))
1366
  ("silent,s", N_("Be more silent. Print results with a tab as separator, each row on new line."))
1367
  ("tee", po::value<string>(),
1368
  N_("Append everything into outfile. See interactive help (\\h) also. Does not work in batch mode. Disable with --disable-tee. This option is disabled by default."))
1567.4.2 by Jay Pipes
Some style fixes and removal of deprecated stuff...
1369
  ("disable-tee", po::value<bool>()->default_value(false)->zero_tokens(), 
1370
  N_("Disable outfile. See interactive help (\\h) also."))
1921.2.1 by Andrew Hutchings
Fix some drizzle client options that were still using '_' instead of '-'
1371
  ("connect-timeout", po::value<uint32_t>(&opt_connect_timeout)->default_value(0)->notifier(&check_timeout_value),
1567.3.1 by Vijay Samuel
Refactoring of the drizzle client.
1372
  N_("Number of seconds before connection timeout."))
1921.2.1 by Andrew Hutchings
Fix some drizzle client options that were still using '_' instead of '-'
1373
  ("max-input-line", po::value<uint32_t>(&opt_max_input_line)->default_value(16*1024L*1024L)->notifier(&check_max_input_line),
1567.3.1 by Vijay Samuel
Refactoring of the drizzle client.
1374
  N_("Max length of input line"))
1921.2.1 by Andrew Hutchings
Fix some drizzle client options that were still using '_' instead of '-'
1375
  ("select-limit", po::value<uint32_t>(&select_limit)->default_value(1000L),
1567.3.1 by Vijay Samuel
Refactoring of the drizzle client.
1376
  N_("Automatic limit for SELECT when using --safe-updates"))
1921.2.1 by Andrew Hutchings
Fix some drizzle client options that were still using '_' instead of '-'
1377
  ("max-join-size", po::value<uint32_t>(&max_join_size)->default_value(1000000L),
1567.3.1 by Vijay Samuel
Refactoring of the drizzle client.
1378
  N_("Automatic limit for rows in a join when using --safe-updates"))
1567.3.8 by Vijay Samuel
Refactor drizzle client.
1379
  ;
1380
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
1381
  po::options_description client_options(N_("Options specific to the client"));
1567.3.8 by Vijay Samuel
Refactor drizzle client.
1382
  client_options.add_options()
1383
  ("host,h", po::value<string>(&current_host)->default_value("localhost"),
1384
  N_("Connect to host"))
1567.6.1 by Monty Taylor
Replaced the snowman sentinel with a series of nulls. As it is physically
1385
  ("password,P", po::value<string>(&current_password)->default_value(PASSWORD_SENTINEL),
1567.3.8 by Vijay Samuel
Refactor drizzle client.
1386
  N_("Password to use when connecting to server. If password is not given it's asked from the tty."))
1567.3.30 by Monty Taylor
Remove special behavior of 0.
1387
  ("port,p", po::value<uint32_t>()->default_value(0),
1567.3.8 by Vijay Samuel
Refactor drizzle client.
1388
  N_("Port number to use for connection or 0 for default to, in order of preference, drizzle.cnf, $DRIZZLE_TCP_PORT, built-in default"))
1960.2.8 by Andrew Hutchings
Modify dtr to cope with drizzleadmin and create test case
1389
#ifdef DRIZZLE_ADMIN_TOOL
1390
  ("user,u", po::value<string>(&current_user)->default_value("root"),
1391
#else
1567.3.8 by Vijay Samuel
Refactor drizzle client.
1392
  ("user,u", po::value<string>(&current_user)->default_value(""),
1960.2.8 by Andrew Hutchings
Modify dtr to cope with drizzleadmin and create test case
1393
#endif
1567.3.8 by Vijay Samuel
Refactor drizzle client.
1394
  N_("User for login if not current user."))
1745.2.1 by LinuxJedi
Remove the --mysql option and convert the --protocol option to do something similar.
1395
  ("protocol",po::value<string>(&opt_protocol)->default_value("mysql"),
1396
  N_("The protocol of connection (mysql or drizzle)."))
1567.3.1 by Vijay Samuel
Refactoring of the drizzle client.
1397
  ;
1398
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
1399
  po::options_description long_options(N_("Allowed Options"));
1567.3.8 by Vijay Samuel
Refactor drizzle client.
1400
  long_options.add(commandline_options).add(drizzle_options).add(client_options);
1401
1402
  std::string system_config_dir_drizzle(SYSCONFDIR); 
1403
  system_config_dir_drizzle.append("/drizzle/drizzle.cnf");
1404
1405
  std::string system_config_dir_client(SYSCONFDIR); 
1406
  system_config_dir_client.append("/drizzle/client.cnf");
202.3.2 by Monty Taylor
Added gettext calls in to my_getopt.c and drizzle.c
1407
1671.2.1 by Vijay Samuel
Merge new user config file processing system.
1408
  std::string user_config_dir((getenv("XDG_CONFIG_HOME")? getenv("XDG_CONFIG_HOME"):"~/.config"));
1921.3.1 by Andrew Hutchings
Using '~' to represent home is a shell standard, POSIX functions do not understand it. So for home config file loading substitute it for env variable $HOME if possible (also a POSIX standard)
1409
 
1410
  if (user_config_dir.compare(0, 2, "~/") == 0)
1411
  {
1412
    char *homedir;
1413
    homedir= getenv("HOME");
1414
    if (homedir != NULL)
1415
      user_config_dir.replace(0, 1, homedir);
1416
  }
1417
 
1567.3.1 by Vijay Samuel
Refactoring of the drizzle client.
1418
  po::variables_map vm;
1567.3.9 by Vijay Samuel
Client refactored.
1419
1596.1.1 by Monty Taylor
Merged in remove-internal-from-drizzle-cc
1420
  po::positional_options_description p;
1608.5.1 by Vijay Samuel
Merge refactor drizzletest
1421
  p.add("database", 1);
1596.1.1 by Monty Taylor
Merged in remove-internal-from-drizzle-cc
1422
1793.3.1 by Andrew Hutchings
Disable boost:po allow_guessing which was making some wrong assumptions
1423
  // Disable allow_guessing
1424
  int style = po::command_line_style::default_style & ~po::command_line_style::allow_guessing;
1425
1596.1.1 by Monty Taylor
Merged in remove-internal-from-drizzle-cc
1426
  po::store(po::command_line_parser(argc, argv).options(long_options).
1793.3.1 by Andrew Hutchings
Disable boost:po allow_guessing which was making some wrong assumptions
1427
            style(style).positional(p).extra_parser(parse_password_arg).run(),
1428
            vm);
1567.3.8 by Vijay Samuel
Refactor drizzle client.
1429
1567.5.3 by Vijay Samuel
Merge patch for sql_connect() and style issue.
1430
  if (! vm["no-defaults"].as<bool>())
1567.3.10 by Vijay Samuel
Client refactored completely using boost::program_options.
1431
  {
1671.2.1 by Vijay Samuel
Merge new user config file processing system.
1432
    std::string user_config_dir_drizzle(user_config_dir);
1433
    user_config_dir_drizzle.append("/drizzle/drizzle.cnf"); 
1434
1435
    std::string user_config_dir_client(user_config_dir);
1436
    user_config_dir_client.append("/drizzle/client.cnf");
1437
1438
    ifstream user_drizzle_ifs(user_config_dir_drizzle.c_str());
1815.1.1 by Monty Taylor
Embed a modified version of parse_config_file. There are several more bugs
1439
    po::store(dpo::parse_config_file(user_drizzle_ifs, drizzle_options), vm);
1671.2.1 by Vijay Samuel
Merge new user config file processing system.
1440
1441
    ifstream user_client_ifs(user_config_dir_client.c_str());
1815.1.1 by Monty Taylor
Embed a modified version of parse_config_file. There are several more bugs
1442
    po::store(dpo::parse_config_file(user_client_ifs, client_options), vm);
1671.2.1 by Vijay Samuel
Merge new user config file processing system.
1443
1567.3.11 by Vijay Samuel
Client completely refactored using boost::program_options.
1444
    ifstream system_drizzle_ifs(system_config_dir_drizzle.c_str());
1815.1.1 by Monty Taylor
Embed a modified version of parse_config_file. There are several more bugs
1445
    store(dpo::parse_config_file(system_drizzle_ifs, drizzle_options), vm);
1567.3.8 by Vijay Samuel
Refactor drizzle client.
1446
 
1567.3.11 by Vijay Samuel
Client completely refactored using boost::program_options.
1447
    ifstream system_client_ifs(system_config_dir_client.c_str());
1815.1.1 by Monty Taylor
Embed a modified version of parse_config_file. There are several more bugs
1448
    po::store(dpo::parse_config_file(system_client_ifs, client_options), vm);
1567.3.10 by Vijay Samuel
Client refactored completely using boost::program_options.
1449
  }
1567.3.8 by Vijay Samuel
Refactor drizzle client.
1450
1567.3.5 by Vijay Samuel
Refactored client.
1451
  po::notify(vm);
1567.3.8 by Vijay Samuel
Refactor drizzle client.
1452
1960.2.7 by Andrew Hutchings
Drop the drizzleadmin client and use ifdef instead
1453
#ifdef DRIZZLE_ADMIN_TOOL
1454
  default_prompt= strdup(getenv("DRIZZLE_PS1") ?
1455
                         getenv("DRIZZLE_PS1") :
1456
                         "drizzleadmin> ");
1457
#else
1567.5.1 by Vijay Samuel
Client drizzle final version with all changes.
1458
  default_prompt= strdup(getenv("DRIZZLE_PS1") ?
1459
                         getenv("DRIZZLE_PS1") :
1460
                         "drizzle> ");
1960.2.7 by Andrew Hutchings
Drop the drizzleadmin client and use ifdef instead
1461
#endif
656.1.51 by Monty Taylor
Fixed strdup return checking in client/
1462
  if (default_prompt == NULL)
1463
  {
1464
    fprintf(stderr, _("Memory allocation error while constructing initial "
1465
                      "prompt. Aborting.\n"));
1466
    exit(ENOMEM);
1467
  }
656.1.20 by Monty Taylor
Removed my_strdup, my_malloc, my_realloc from client/
1468
  current_prompt= strdup(default_prompt);
1567.3.1 by Vijay Samuel
Refactoring of the drizzle client.
1469
  if (current_prompt.empty())
656.1.51 by Monty Taylor
Fixed strdup return checking in client/
1470
  {
1471
    fprintf(stderr, _("Memory allocation error while constructing initial "
1472
                      "prompt. Aborting.\n"));
1473
    exit(ENOMEM);
1474
  }
279.2.2 by Monty Taylor
Replaced DYNAMIC_STRING with C++ string in drizzle command line client.
1475
  processed_prompt= new string();
287.3.22 by Monty Taylor
Fixed typo.
1476
  processed_prompt->reserve(32);
182.1.2 by Jim Winstead
Various fixes to enable compilation on Mac OS X, and remove the glib dependency.
1477
1 by brian
clean slate
1478
  prompt_counter=0;
1479
1567.3.17 by Monty Taylor
Removed more C-string strings. replaced with std::string.
1480
  outfile.clear();      // no (default) outfile
1481
  pager.assign("stdout");  // the default, if --pager wasn't given
1 by brian
clean slate
1482
  {
1567.3.17 by Monty Taylor
Removed more C-string strings. replaced with std::string.
1483
    const char *tmp= getenv("PAGER");
1 by brian
clean slate
1484
    if (tmp && strlen(tmp))
1485
    {
1486
      default_pager_set= 1;
1567.3.17 by Monty Taylor
Removed more C-string strings. replaced with std::string.
1487
      default_pager.assign(tmp);
1 by brian
clean slate
1488
    }
1489
  }
1567.4.2 by Jay Pipes
Some style fixes and removal of deprecated stuff...
1490
  if (! isatty(0) || ! isatty(1))
1 by brian
clean slate
1491
  {
1441.3.1 by Vijay Samuel
all required updations have been made
1492
    status.setBatch(1); opt_silent=1;
1 by brian
clean slate
1493
    ignore_errors=0;
1494
  }
1495
  else
1441.3.1 by Vijay Samuel
all required updations have been made
1496
    status.setAddToHistory(1);
1497
  status.setExitStatus(1);
1 by brian
clean slate
1498
1499
  {
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
1500
    /*
1501
      The file descriptor-layer may be out-of-sync with the file-number layer,
1502
      so we make sure that "stdout" is really open.  If its file is closed then
1503
      explicitly close the FD layer.
1 by brian
clean slate
1504
    */
1505
    int stdout_fileno_copy;
1506
    stdout_fileno_copy= dup(fileno(stdout)); /* Okay if fileno fails. */
1507
    if (stdout_fileno_copy == -1)
1508
      fclose(stdout);
1509
    else
1510
      close(stdout_fileno_copy);             /* Clean up dup(). */
1511
  }
1512
1757.5.1 by Andrew Hutchings
Rework the boolean options so that true-by-default options change to --disable-option.
1513
  /* Inverted Booleans */
1514
1515
  line_numbers= (vm.count("disable-line-numbers")) ? false : true;
1516
  column_names= (vm.count("disable-column-names")) ? false : true;
1517
  opt_rehash= (vm.count("disable-auto-rehash")) ? false : true;
1518
  opt_reconnect= (vm.count("disable-reconnect")) ? false : true;
1519
1810.3.1 by Andrew Hutchings
Don't do completion hash rebuild on --shutdown
1520
  /* Don't rehash with --shutdown */
1521
  if (vm.count("shutdown"))
1522
  {
1523
    opt_rehash= false;
1524
    opt_shutdown= true;
1525
  }
1526
1567.4.2 by Jay Pipes
Some style fixes and removal of deprecated stuff...
1527
  if (vm.count("delimiter"))
1567.3.1 by Vijay Samuel
Refactoring of the drizzle client.
1528
  {
1529
    /* Check that delimiter does not contain a backslash */
1567.4.2 by Jay Pipes
Some style fixes and removal of deprecated stuff...
1530
    if (! strstr(delimiter_str.c_str(), "\\"))
1567.3.1 by Vijay Samuel
Refactoring of the drizzle client.
1531
    {
1532
      delimiter= (char *)delimiter_str.c_str();  
1533
    }
1534
    else
1535
    {
1536
      put_info(_("DELIMITER cannot contain a backslash character"),
1537
      INFO_ERROR,0,0);
1567.3.4 by Vijay Samuel
Refactored drizzle client.
1538
      exit(-1);
1567.3.1 by Vijay Samuel
Refactoring of the drizzle client.
1539
    }
1540
   
1541
    delimiter_length= (uint32_t)strlen(delimiter);
1542
  }
1567.4.2 by Jay Pipes
Some style fixes and removal of deprecated stuff...
1543
  if (vm.count("tee"))
1567.3.1 by Vijay Samuel
Refactoring of the drizzle client.
1544
  { 
1545
    if (vm["tee"].as<string>().empty())
1546
    {
1547
      if (opt_outfile)
1548
        end_tee();
1549
    }
1550
    else
1551
      init_tee(vm["tee"].as<string>().c_str());
1552
  }
1567.4.2 by Jay Pipes
Some style fixes and removal of deprecated stuff...
1553
  if (vm["disable-tee"].as<bool>() == true)
1567.3.1 by Vijay Samuel
Refactoring of the drizzle client.
1554
  {
1555
    if (opt_outfile)
1556
      end_tee();
1557
  }
1567.4.2 by Jay Pipes
Some style fixes and removal of deprecated stuff...
1558
  if (vm.count("pager"))
1567.3.1 by Vijay Samuel
Refactoring of the drizzle client.
1559
  {
1560
    if (vm["pager"].as<string>().empty())
1561
      opt_nopager= 1;
1562
    else
1563
    {
1564
      opt_nopager= 0;
1565
      if (vm[pager].as<string>().length())
1566
      {
1567
        default_pager_set= 1;
1567.3.17 by Monty Taylor
Removed more C-string strings. replaced with std::string.
1568
        pager.assign(vm["pager"].as<string>());
1569
        default_pager.assign(pager);
1567.3.1 by Vijay Samuel
Refactoring of the drizzle client.
1570
      }
1571
      else if (default_pager_set)
1567.3.17 by Monty Taylor
Removed more C-string strings. replaced with std::string.
1572
        pager.assign(default_pager);
1567.3.1 by Vijay Samuel
Refactoring of the drizzle client.
1573
      else
1574
        opt_nopager= 1;
1575
    }
1576
  }
1567.4.2 by Jay Pipes
Some style fixes and removal of deprecated stuff...
1577
  if (vm.count("disable-pager"))
1567.3.1 by Vijay Samuel
Refactoring of the drizzle client.
1578
  {
1579
    opt_nopager= 1;
1580
  }
1581
1567.4.2 by Jay Pipes
Some style fixes and removal of deprecated stuff...
1582
  if (vm.count("no-auto-rehash"))
1567.3.1 by Vijay Samuel
Refactoring of the drizzle client.
1583
    opt_rehash= 0;
1584
1567.4.2 by Jay Pipes
Some style fixes and removal of deprecated stuff...
1585
  if (vm.count("skip-column-names"))
1567.3.1 by Vijay Samuel
Refactoring of the drizzle client.
1586
    column_names= 0;
1587
    
1567.4.2 by Jay Pipes
Some style fixes and removal of deprecated stuff...
1588
  if (vm.count("execute"))
1567.3.1 by Vijay Samuel
Refactoring of the drizzle client.
1589
  {  
1590
    status.setBatch(1);
1591
    status.setAddToHistory(1);
1592
    if (status.getLineBuff() == NULL)
1593
      status.setLineBuff(opt_max_input_line,NULL);
1594
    if (status.getLineBuff() == NULL)
1595
    {
1596
      exit(1);
1597
    }
1598
    status.getLineBuff()->addString(vm["execute"].as<string>().c_str());
1599
  }
1600
1601
  if (one_database)
1602
    skip_updates= true;
1745.2.1 by LinuxJedi
Remove the --mysql option and convert the --protocol option to do something similar.
1603
1604
  if (vm.count("protocol"))
1605
  {
1606
    std::transform(opt_protocol.begin(), opt_protocol.end(), 
1607
      opt_protocol.begin(), ::tolower);
1608
1609
    if (not opt_protocol.compare("mysql"))
1610
      use_drizzle_protocol=false;
1611
    else if (not opt_protocol.compare("drizzle"))
1612
      use_drizzle_protocol=true;
1613
    else
1614
    {
1615
      cout << _("Error: Unknown protocol") << " '" << opt_protocol << "'" << endl;
1616
      exit(-1);
1617
    }
1618
  }
1619
 
1567.4.2 by Jay Pipes
Some style fixes and removal of deprecated stuff...
1620
  if (vm.count("port"))
1567.3.1 by Vijay Samuel
Refactoring of the drizzle client.
1621
  {
1567.4.2 by Jay Pipes
Some style fixes and removal of deprecated stuff...
1622
    opt_drizzle_port= vm["port"].as<uint32_t>();
1623
1567.3.1 by Vijay Samuel
Refactoring of the drizzle client.
1624
    /* If the port number is > 65535 it is not a valid port
1625
       This also helps with potential data loss casting unsigned long to a
1626
       uint32_t. */
1567.3.30 by Monty Taylor
Remove special behavior of 0.
1627
    if (opt_drizzle_port > 65535)
1567.3.1 by Vijay Samuel
Refactoring of the drizzle client.
1628
    {
1567.4.2 by Jay Pipes
Some style fixes and removal of deprecated stuff...
1629
      printf(_("Error: Value of %" PRIu32 " supplied for port is not valid.\n"), opt_drizzle_port);
1567.3.1 by Vijay Samuel
Refactoring of the drizzle client.
1630
      exit(-1);
1631
    }
1567.4.2 by Jay Pipes
Some style fixes and removal of deprecated stuff...
1632
  }
1567.3.1 by Vijay Samuel
Refactoring of the drizzle client.
1633
1567.4.2 by Jay Pipes
Some style fixes and removal of deprecated stuff...
1634
  if (vm.count("password"))
1635
  {
1567.3.3 by Vijay Samuel
Client refactored.
1636
    if (!opt_password.empty())
1637
      opt_password.erase();
1567.6.1 by Monty Taylor
Replaced the snowman sentinel with a series of nulls. As it is physically
1638
    if (current_password == PASSWORD_SENTINEL)
1639
    {
1567.3.5 by Vijay Samuel
Refactored client.
1640
      opt_password= "";
1567.6.1 by Monty Taylor
Replaced the snowman sentinel with a series of nulls. As it is physically
1641
    }
1567.3.5 by Vijay Samuel
Refactored client.
1642
    else
1567.6.1 by Monty Taylor
Replaced the snowman sentinel with a series of nulls. As it is physically
1643
    {
1567.3.5 by Vijay Samuel
Refactored client.
1644
      opt_password= current_password;
1567.6.1 by Monty Taylor
Replaced the snowman sentinel with a series of nulls. As it is physically
1645
      tty_password= false;
1646
    }
1567.3.3 by Vijay Samuel
Client refactored.
1647
  }
1648
  else
1649
  {
1567.6.1 by Monty Taylor
Replaced the snowman sentinel with a series of nulls. As it is physically
1650
      tty_password= true;
1567.3.3 by Vijay Samuel
Client refactored.
1651
  }
1652
  
1923.3.4 by Andrew Hutchings
Revert -v fix
1653
1654
  if (!opt_verbose.empty())
1655
  {
1656
    verbose= opt_verbose.length();
1657
  }
1658
1567.5.3 by Vijay Samuel
Merge patch for sql_connect() and style issue.
1659
  if (vm.count("batch"))
1567.3.1 by Vijay Samuel
Refactoring of the drizzle client.
1660
  {
1661
    status.setBatch(1);
1662
    status.setAddToHistory(0);
1567.3.15 by Monty Taylor
Removed the last vestiges of my_sys from drizzle.cc.
1663
    if (opt_silent < 1)
1664
    {
1665
      opt_silent= 1;
1666
    }
1567.3.1 by Vijay Samuel
Refactoring of the drizzle client.
1667
  }
1567.5.3 by Vijay Samuel
Merge patch for sql_connect() and style issue.
1668
  if (vm.count("silent"))
1567.3.1 by Vijay Samuel
Refactoring of the drizzle client.
1669
  {
1670
    opt_silent++;
1671
  }
1672
  
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
1673
  if (vm.count("help") || vm.count("version"))
1567.3.1 by Vijay Samuel
Refactoring of the drizzle client.
1674
  {
1729.3.2 by LinuxJedi
Further cleanups based on Monty's comments (also revert a couple of things)
1675
    printf(_("Drizzle client %s build %s, for %s-%s (%s) using readline %s\n"),
1676
           drizzle_version(), VERSION,
1677
           HOST_VENDOR, HOST_OS, HOST_CPU,
1567.3.14 by Monty Taylor
Removed the need for drizzled::internal usage in drizzle.cc.
1678
           rl_library_version);
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
1679
    if (vm.count("version"))
1680
      exit(0);
1567.3.1 by Vijay Samuel
Refactoring of the drizzle client.
1681
    printf(_("Copyright (C) 2008 Sun Microsystems\n"
1682
           "This software comes with ABSOLUTELY NO WARRANTY. "
1683
           "This is free software,\n"
1684
           "and you are welcome to modify and redistribute it "
1685
           "under the GPL license\n"));
1567.3.14 by Monty Taylor
Removed the need for drizzled::internal usage in drizzle.cc.
1686
    printf(_("Usage: drizzle [OPTIONS] [database]\n"));
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
1687
    cout << long_options;
1567.3.1 by Vijay Samuel
Refactoring of the drizzle client.
1688
    exit(0);
1689
  }
1690
 
1691
1567.5.3 by Vijay Samuel
Merge patch for sql_connect() and style issue.
1692
  if (process_options())
1 by brian
clean slate
1693
  {
1694
    exit(1);
1695
  }
973.1.1 by Toru Maesaka
Added server shutdown and ping functionalities to the drizzle client app
1696
212.6.10 by Mats Kindahl
Removing redundant use of casts in client/ for memcmp(), memcpy(), memset(), and memmove().
1697
  memset(&drizzle, 0, sizeof(drizzle));
1567.5.3 by Vijay Samuel
Merge patch for sql_connect() and style issue.
1698
  if (sql_connect(current_host, current_db, current_user, opt_password,opt_silent))
1 by brian
clean slate
1699
  {
206.3.1 by Patrick Galbraith
Most everything working with client rename
1700
    quick= 1;          // Avoid history
1441.3.1 by Vijay Samuel
all required updations have been made
1701
    status.setExitStatus(1);
206.3.1 by Patrick Galbraith
Most everything working with client rename
1702
    drizzle_end(-1);
1 by brian
clean slate
1703
  }
973.1.1 by Toru Maesaka
Added server shutdown and ping functionalities to the drizzle client app
1704
1705
  int command_error;
973.1.2 by Toru Maesaka
Cosmetic fixes pointed out by Jay and added comments for doxygen
1706
  if (execute_commands(&command_error) != false)
973.1.1 by Toru Maesaka
Added server shutdown and ping functionalities to the drizzle client app
1707
  {
1708
    /* we've executed a command so exit before we go into readline mode */
1709
    exit(command_error);
1710
  }
1711
1441.3.1 by Vijay Samuel
all required updations have been made
1712
  if (status.getBatch() && !status.getLineBuff())
973.1.1 by Toru Maesaka
Added server shutdown and ping functionalities to the drizzle client app
1713
  {
1441.3.1 by Vijay Samuel
all required updations have been made
1714
    status.setLineBuff(opt_max_input_line, stdin);
1715
    if (status.getLineBuff() == NULL)
973.1.2 by Toru Maesaka
Cosmetic fixes pointed out by Jay and added comments for doxygen
1716
    {
1717
      exit(1);
1718
    }
973.1.1 by Toru Maesaka
Added server shutdown and ping functionalities to the drizzle client app
1719
  }
973.1.2 by Toru Maesaka
Cosmetic fixes pointed out by Jay and added comments for doxygen
1720
1441.3.1 by Vijay Samuel
all required updations have been made
1721
  if (!status.getBatch())
206.3.1 by Patrick Galbraith
Most everything working with client rename
1722
    ignore_errors=1;        // Don't abort monitor
1 by brian
clean slate
1723
1724
  if (opt_sigint_ignore)
1725
    signal(SIGINT, SIG_IGN);
1726
  else
1727
    signal(SIGINT, handle_sigint);              // Catch SIGINT to clean up
206.3.1 by Patrick Galbraith
Most everything working with client rename
1728
  signal(SIGQUIT, drizzle_end);      // Catch SIGQUIT to clean up
1 by brian
clean slate
1729
1730
#if defined(HAVE_TERMIOS_H) && defined(GWINSZ_IN_SYS_IOCTL)
1731
  /* Readline will call this if it installs a handler */
1732
  signal(SIGWINCH, window_resize);
1733
  /* call the SIGWINCH handler to get the default term width */
1734
  window_resize(0);
1735
#endif
1720.3.2 by LinuxJedi
Fix bug #617510:
1736
  std::vector<char> output_buff;
1737
  output_buff.resize(512);
1738
1739
  snprintf(&output_buff[0], output_buff.size(), 
1740
           _("Welcome to the Drizzle client..  Commands end with %s or \\g."), 
1741
           delimiter);
1742
1743
  put_info(&output_buff[0], INFO_INFO, 0, 0);
182.1.2 by Jim Winstead
Various fixes to enable compilation on Mac OS X, and remove the glib dependency.
1744
279.2.2 by Monty Taylor
Replaced DYNAMIC_STRING with C++ string in drizzle command line client.
1745
  glob_buffer= new string();
287.3.19 by Monty Taylor
Added string.reserve() calls.
1746
  glob_buffer->reserve(512);
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1747
1707.1.22 by Brian Aker
Small cleanup in mysql.cc
1748
  snprintf(&output_buff[0], output_buff.size(),
1745.2.1 by LinuxJedi
Remove the --mysql option and convert the --protocol option to do something similar.
1749
          _("Your Drizzle connection id is %u\nConnection protocol: %s\nServer version: %s\n"),
1441.3.1 by Vijay Samuel
all required updations have been made
1750
          drizzle_con_thread_id(&con),
1745.2.1 by LinuxJedi
Remove the --mysql option and convert the --protocol option to do something similar.
1751
          opt_protocol.c_str(),
1441.3.1 by Vijay Samuel
all required updations have been made
1752
          server_version_string(&con));
1707.1.22 by Brian Aker
Small cleanup in mysql.cc
1753
  put_info(&output_buff[0], INFO_INFO, 0, 0);
1 by brian
clean slate
1754
1745.2.1 by LinuxJedi
Remove the --mysql option and convert the --protocol option to do something similar.
1755
1567.5.2 by Vijay Samuel
Merge OSX fix for client drizzle.
1756
  initialize_readline((char *)current_prompt.c_str());
1441.3.1 by Vijay Samuel
all required updations have been made
1757
  if (!status.getBatch() && !quick)
1 by brian
clean slate
1758
  {
206.3.1 by Patrick Galbraith
Most everything working with client rename
1759
    /* read-history from file, default ~/.drizzle_history*/
266.2.3 by Jim Winstead
Use DRIZZLE_* environment variables instead of MYSQL_*
1760
    if (getenv("DRIZZLE_HISTFILE"))
1761
      histfile= strdup(getenv("DRIZZLE_HISTFILE"));
1 by brian
clean slate
1762
    else if (getenv("HOME"))
1763
    {
1441.3.1 by Vijay Samuel
all required updations have been made
1764
      histfile=(char*) malloc(strlen(getenv("HOME")) + strlen("/.drizzle_history") + 2);
1 by brian
clean slate
1765
      if (histfile)
1441.3.1 by Vijay Samuel
all required updations have been made
1766
        sprintf(histfile,"%s/.drizzle_history",getenv("HOME"));
1 by brian
clean slate
1767
      char link_name[FN_REFLEN];
1060.2.1 by Eric Lambert
-replace calls to my_readlink with readlink
1768
      ssize_t sym_link_size= readlink(histfile,link_name,FN_REFLEN-1);
1769
      if (sym_link_size >= 0)
1 by brian
clean slate
1770
      {
1060.2.1 by Eric Lambert
-replace calls to my_readlink with readlink
1771
        link_name[sym_link_size]= '\0';
1772
        if (strncmp(link_name, "/dev/null", 10) == 0)
1773
        {
1774
          /* The .drizzle_history file is a symlink to /dev/null, don't use it */
1775
          free(histfile);
1776
          histfile= 0;
1777
        }
1 by brian
clean slate
1778
      }
1779
    }
1780
    if (histfile)
1781
    {
1782
      if (verbose)
202.3.13 by Monty Taylor
Changed gettext() to _() in drizzle.c.
1783
        tee_fprintf(stdout, _("Reading history-file %s\n"),histfile);
1 by brian
clean slate
1784
      read_history(histfile);
1441.3.1 by Vijay Samuel
all required updations have been made
1785
      if (!(histfile_tmp= (char*) malloc((uint32_t) strlen(histfile) + 5)))
1 by brian
clean slate
1786
      {
202.3.13 by Monty Taylor
Changed gettext() to _() in drizzle.c.
1787
        fprintf(stderr, _("Couldn't allocate memory for temp histfile!\n"));
77.3.1 by Monty Taylor
First steps in removing sql_string and using glib instead.
1788
        exit(1);
1 by brian
clean slate
1789
      }
1441.3.1 by Vijay Samuel
all required updations have been made
1790
      sprintf(histfile_tmp, "%s.TMP", histfile);
1 by brian
clean slate
1791
    }
1792
  }
77.3.1 by Monty Taylor
First steps in removing sql_string and using glib instead.
1793
1126.4.1 by Monty Taylor
Fixed a buffer overrun that was causing some translated message output to suck.
1794
  put_info(_("Type 'help;' or '\\h' for help. "
1795
             "Type '\\c' to clear the buffer.\n"),INFO_INFO,0,0);
1441.3.1 by Vijay Samuel
all required updations have been made
1796
  status.setExitStatus(read_and_execute(!status.getBatch()));
1 by brian
clean slate
1797
  if (opt_outfile)
1798
    end_tee();
206.3.1 by Patrick Galbraith
Most everything working with client rename
1799
  drizzle_end(0);
1567.3.1 by Vijay Samuel
Refactoring of the drizzle client.
1800
}
319 by Brian Aker
Fix (yuck!) for OSX/Google bug.
1801
1567.3.1 by Vijay Samuel
Refactoring of the drizzle client.
1802
  catch(exception &err)
1803
  {
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
1804
    cerr << _("Error:") << err.what() << endl;
1567.3.1 by Vijay Samuel
Refactoring of the drizzle client.
1805
  }
206.3.1 by Patrick Galbraith
Most everything working with client rename
1806
  return(0);        // Keep compiler happy
1 by brian
clean slate
1807
}
1808
779.2.11 by Monty Taylor
General build cleanup - removed cruft, removed depreated checks.
1809
void drizzle_end(int sig)
1 by brian
clean slate
1810
{
928.1.1 by Eric Day
Started client changes.
1811
  drizzle_con_free(&con);
1812
  drizzle_free(&drizzle);
1441.3.1 by Vijay Samuel
all required updations have been made
1813
  if (!status.getBatch() && !quick && histfile)
1 by brian
clean slate
1814
  {
1815
    /* write-history */
1816
    if (verbose)
202.3.13 by Monty Taylor
Changed gettext() to _() in drizzle.c.
1817
      tee_fprintf(stdout, _("Writing history-file %s\n"),histfile);
1 by brian
clean slate
1818
    if (!write_history(histfile_tmp))
1567.3.14 by Monty Taylor
Removed the need for drizzled::internal usage in drizzle.cc.
1819
      rename(histfile_tmp, histfile);
1 by brian
clean slate
1820
  }
1441.3.1 by Vijay Samuel
all required updations have been made
1821
  delete status.getLineBuff();
1822
  status.setLineBuff(0);
1 by brian
clean slate
1823
1824
  if (sig >= 0)
202.3.13 by Monty Taylor
Changed gettext() to _() in drizzle.c.
1825
    put_info(sig ? _("Aborted") : _("Bye"), INFO_RESULT,0,0);
1883.3.1 by Andrew Hutchings
Cleanup drizzledump and drizzle for cppcheck
1826
  delete glob_buffer;
1827
  delete processed_prompt;
1567.3.1 by Vijay Samuel
Refactoring of the drizzle client.
1828
  opt_password.erase();
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.
1829
  free(histfile);
1830
  free(histfile_tmp);
1567.3.1 by Vijay Samuel
Refactoring of the drizzle client.
1831
  current_db.erase();
1832
  current_host.erase();
1833
  current_user.erase();
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.
1834
  free(full_username);
1835
  free(part_username);
1836
  free(default_prompt);
1567.3.1 by Vijay Samuel
Refactoring of the drizzle client.
1837
  current_prompt.erase();
1441.3.1 by Vijay Samuel
all required updations have been made
1838
  exit(status.getExitStatus());
1 by brian
clean slate
1839
}
1840
1841
1842
/*
1843
  This function handles sigint calls
1844
  If query is in process, kill query
1845
  no query in process, terminate like previous behavior
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
1846
*/
520.4.43 by mordred
A set of Solaris fixes.
1847
extern "C"
779.2.11 by Monty Taylor
General build cleanup - removed cruft, removed depreated checks.
1848
void handle_sigint(int sig)
1 by brian
clean slate
1849
{
1850
  char kill_buffer[40];
1929.1.40 by Monty Taylor
Replaced auto_ptr with scoped_ptr.
1851
  boost::scoped_ptr<drizzle_con_st> kill_drizzle(new drizzle_con_st);
928.1.1 by Eric Day
Started client changes.
1852
  drizzle_result_st res;
1853
  drizzle_return_t ret;
1 by brian
clean slate
1854
1855
  /* terminate if no query being executed, or we already tried interrupting */
77.3.3 by Monty Taylor
Last change to migrate to glib from sql_string.
1856
  if (!executing_query || interrupted_query) {
1 by brian
clean slate
1857
    goto err;
77.3.3 by Monty Taylor
Last change to migrate to glib from sql_string.
1858
  }
1 by brian
clean slate
1859
1929.1.13 by Stewart Smith
fix large stack usage in handle_sigint in drizzle client
1860
  if (drizzle_con_add_tcp(&drizzle, kill_drizzle.get(), current_host.c_str(),
1745.2.1 by LinuxJedi
Remove the --mysql option and convert the --protocol option to do something similar.
1861
    opt_drizzle_port, current_user.c_str(), opt_password.c_str(), NULL,
1862
    use_drizzle_protocol ? DRIZZLE_CON_EXPERIMENTAL : DRIZZLE_CON_MYSQL) == NULL)
77.3.3 by Monty Taylor
Last change to migrate to glib from sql_string.
1863
  {
1 by brian
clean slate
1864
    goto err;
77.3.3 by Monty Taylor
Last change to migrate to glib from sql_string.
1865
  }
1 by brian
clean slate
1866
1867
  /* kill_buffer is always big enough because max length of %lu is 15 */
1441.3.1 by Vijay Samuel
all required updations have been made
1868
  sprintf(kill_buffer, "KILL /*!50000 QUERY */ %u",
1869
          drizzle_con_thread_id(&con));
928.1.1 by Eric Day
Started client changes.
1870
1929.1.13 by Stewart Smith
fix large stack usage in handle_sigint in drizzle client
1871
  if (drizzle_query_str(kill_drizzle.get(), &res, kill_buffer, &ret) != NULL)
928.1.1 by Eric Day
Started client changes.
1872
    drizzle_result_free(&res);
1873
1929.1.13 by Stewart Smith
fix large stack usage in handle_sigint in drizzle client
1874
  drizzle_con_free(kill_drizzle.get());
202.3.13 by Monty Taylor
Changed gettext() to _() in drizzle.c.
1875
  tee_fprintf(stdout, _("Query aborted by Ctrl+C\n"));
1 by brian
clean slate
1876
1877
  interrupted_query= 1;
1878
1879
  return;
1880
1881
err:
206.3.1 by Patrick Galbraith
Most everything working with client rename
1882
  drizzle_end(sig);
1 by brian
clean slate
1883
}
1884
1885
1886
#if defined(HAVE_TERMIOS_H) && defined(GWINSZ_IN_SYS_IOCTL)
779.2.11 by Monty Taylor
General build cleanup - removed cruft, removed depreated checks.
1887
void window_resize(int)
1 by brian
clean slate
1888
{
1889
  struct winsize window_size;
1890
1891
  if (ioctl(fileno(stdin), TIOCGWINSZ, &window_size) == 0)
1892
    terminal_width= window_size.ws_col;
1893
}
1894
#endif
1895
1567.3.1 by Vijay Samuel
Refactoring of the drizzle client.
1896
1897
1567.5.3 by Vijay Samuel
Merge patch for sql_connect() and style issue.
1898
static int process_options(void)
1 by brian
clean slate
1899
{
1900
  char *tmp, *pagpoint;
1567.3.1 by Vijay Samuel
Refactoring of the drizzle client.
1901
  
1 by brian
clean slate
1902
266.2.3 by Jim Winstead
Use DRIZZLE_* environment variables instead of MYSQL_*
1903
  tmp= (char *) getenv("DRIZZLE_HOST");
1 by brian
clean slate
1904
  if (tmp)
1593.1.4 by Vijay Samuel
Merge Fixes for client drizzle.
1905
    current_host.assign(tmp);
1 by brian
clean slate
1906
1907
  pagpoint= getenv("PAGER");
1908
  if (!((char*) (pagpoint)))
1909
  {
1567.3.17 by Monty Taylor
Removed more C-string strings. replaced with std::string.
1910
    pager.assign("stdout");
1 by brian
clean slate
1911
    opt_nopager= 1;
1912
  }
1913
  else
1567.3.17 by Monty Taylor
Removed more C-string strings. replaced with std::string.
1914
  {
1915
    pager.assign(pagpoint);
1916
  }
1917
  default_pager.assign(pager);
1 by brian
clean slate
1918
1567.3.1 by Vijay Samuel
Refactoring of the drizzle client.
1919
  //
1 by brian
clean slate
1920
1441.3.1 by Vijay Samuel
all required updations have been made
1921
  if (status.getBatch()) /* disable pager and outfile in this case */
1 by brian
clean slate
1922
  {
1567.3.17 by Monty Taylor
Removed more C-string strings. replaced with std::string.
1923
    default_pager.assign("stdout");
1924
    pager.assign("stdout");
1 by brian
clean slate
1925
    opt_nopager= 1;
1926
    default_pager_set= 0;
1927
    opt_outfile= 0;
1928
    opt_reconnect= 0;
928.1.1 by Eric Day
Started client changes.
1929
    connect_flag= DRIZZLE_CAPABILITIES_NONE; /* Not in interactive mode */
1 by brian
clean slate
1930
  }
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
1931
1 by brian
clean slate
1932
  if (tty_password)
928.1.1 by Eric Day
Started client changes.
1933
    opt_password= client_get_tty_password(NULL);
1 by brian
clean slate
1934
  return(0);
1935
}
1936
1937
static int read_and_execute(bool interactive)
1938
{
77.3.7 by Monty Taylor
Made mysql into a pure-C program.
1939
  char *line;
1940
  char in_string=0;
288 by Brian Aker
ulong cleanp in client apps
1941
  uint32_t line_number=0;
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
1942
  bool ml_comment= 0;
1441.3.1 by Vijay Samuel
all required updations have been made
1943
  Commands *com;
1944
  status.setExitStatus(1);
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
1945
1 by brian
clean slate
1946
  for (;;)
1947
  {
1948
    if (!interactive)
1949
    {
1441.3.1 by Vijay Samuel
all required updations have been made
1950
      if (status.getLineBuff())
1951
        line= status.getLineBuff()->readline();
1095.2.1 by Robert Klahn
Replace typedef struct LINE_BUFFER with class LineBuffer, encapsulating current logic
1952
      else
1095.2.4 by Robert Klahn
changes from code review feedback
1953
        line= 0;
1095.2.2 by Robert Klahn
convert logic to use std::stringstream for buffer
1954
1095.2.4 by Robert Klahn
changes from code review feedback
1955
      line_number++;
1956
      if (show_progress_size > 0)
670.1.4 by Monty Taylor
Imported patch from LinuxJedi to add import status messages.
1957
      {
1095.2.4 by Robert Klahn
changes from code review feedback
1958
        if ((line_number % show_progress_size) == 0)
1959
          fprintf(stderr, _("Processing line: %"PRIu32"\n"), line_number);
670.1.4 by Monty Taylor
Imported patch from LinuxJedi to add import status messages.
1960
      }
1095.2.4 by Robert Klahn
changes from code review feedback
1961
      if (!glob_buffer->empty())
1441.3.1 by Vijay Samuel
all required updations have been made
1962
        status.setQueryStartLine(line_number);
1 by brian
clean slate
1963
    }
1964
    else
1965
    {
1596.1.1 by Monty Taylor
Merged in remove-internal-from-drizzle-cc
1966
      string prompt(ml_comment
1967
                      ? "   /*> " 
1968
                      : glob_buffer->empty()
1969
                        ? construct_prompt()
1970
                        : not in_string
1971
                          ? "    -> "
1972
                          : in_string == '\''
1973
                            ? "    '> "
1974
                            : in_string == '`'
1975
                              ? "    `> "
1976
                              : "    \"> ");
279.2.2 by Monty Taylor
Replaced DYNAMIC_STRING with C++ string in drizzle command line client.
1977
      if (opt_outfile && glob_buffer->empty())
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
1978
        fflush(OUTFILE);
1 by brian
clean slate
1979
1980
      if (opt_outfile)
1596.1.1 by Monty Taylor
Merged in remove-internal-from-drizzle-cc
1981
        fputs(prompt.c_str(), OUTFILE);
1982
      line= readline(prompt.c_str());
1 by brian
clean slate
1983
      /*
1984
        When Ctrl+d or Ctrl+z is pressed, the line may be NULL on some OS
1985
        which may cause coredump.
1986
      */
1987
      if (opt_outfile && line)
77.3.1 by Monty Taylor
First steps in removing sql_string and using glib instead.
1988
        fprintf(OUTFILE, "%s\n", line);
1 by brian
clean slate
1989
    }
77.3.1 by Monty Taylor
First steps in removing sql_string and using glib instead.
1990
    // End of file
1991
    if (!line)
1 by brian
clean slate
1992
    {
1441.3.1 by Vijay Samuel
all required updations have been made
1993
      status.setExitStatus(0);
1 by brian
clean slate
1994
      break;
1995
    }
1996
1997
    /*
206.3.1 by Patrick Galbraith
Most everything working with client rename
1998
      Check if line is a drizzle command line
1 by brian
clean slate
1999
      (We want to allow help, print and clear anywhere at line start
2000
    */
279.2.2 by Monty Taylor
Replaced DYNAMIC_STRING with C++ string in drizzle command line client.
2001
    if ((named_cmds || (glob_buffer->empty()))
77.3.1 by Monty Taylor
First steps in removing sql_string and using glib instead.
2002
        && !ml_comment && !in_string && (com=find_command(line,0)))
1 by brian
clean slate
2003
    {
77.3.1 by Monty Taylor
First steps in removing sql_string and using glib instead.
2004
      if ((*com->func)(glob_buffer,line) > 0)
2005
        break;
2006
      // If buffer was emptied
279.2.2 by Monty Taylor
Replaced DYNAMIC_STRING with C++ string in drizzle command line client.
2007
      if (glob_buffer->empty())
77.3.1 by Monty Taylor
First steps in removing sql_string and using glib instead.
2008
        in_string=0;
1441.3.1 by Vijay Samuel
all required updations have been made
2009
      if (interactive && status.getAddToHistory() && not_in_history(line))
77.3.1 by Monty Taylor
First steps in removing sql_string and using glib instead.
2010
        add_history(line);
1 by brian
clean slate
2011
      continue;
2012
    }
77.3.4 by Monty Taylor
Removed debugging. Fixed multi-line processing error.
2013
    if (add_line(glob_buffer,line,&in_string,&ml_comment))
1 by brian
clean slate
2014
      break;
2015
  }
2016
  /* if in batch mode, send last query even if it doesn't end with \g or go */
2017
1441.3.1 by Vijay Samuel
all required updations have been made
2018
  if (!interactive && !status.getExitStatus())
1 by brian
clean slate
2019
  {
2020
    remove_cntrl(glob_buffer);
279.2.2 by Monty Taylor
Replaced DYNAMIC_STRING with C++ string in drizzle command line client.
2021
    if (!glob_buffer->empty())
1 by brian
clean slate
2022
    {
1441.3.1 by Vijay Samuel
all required updations have been made
2023
      status.setExitStatus(1);
77.3.1 by Monty Taylor
First steps in removing sql_string and using glib instead.
2024
      if (com_go(glob_buffer,line) <= 0)
1441.3.1 by Vijay Samuel
all required updations have been made
2025
        status.setExitStatus(0);
1 by brian
clean slate
2026
    }
2027
  }
2028
1441.3.1 by Vijay Samuel
all required updations have been made
2029
  return status.getExitStatus();
1 by brian
clean slate
2030
}
2031
2032
1441.3.1 by Vijay Samuel
all required updations have been made
2033
static Commands *find_command(const char *name,char cmd_char)
1 by brian
clean slate
2034
{
893 by Brian Aker
First pass of stripping uint
2035
  uint32_t len;
266.1.19 by Monty Taylor
Fixed const thing.
2036
  const char *end;
1 by brian
clean slate
2037
2038
  if (!name)
2039
  {
2040
    len=0;
2041
    end=0;
2042
  }
2043
  else
2044
  {
1567.3.14 by Monty Taylor
Removed the need for drizzled::internal usage in drizzle.cc.
2045
    while (isspace(*name))
1 by brian
clean slate
2046
      name++;
2047
    /*
2048
      If there is an \\g in the row or if the row has a delimiter but
2049
      this is not a delimiter command, let add_line() take care of
2050
      parsing the row and calling find_command()
2051
    */
2052
    if (strstr(name, "\\g") || (strstr(name, delimiter) &&
2053
                                !(strlen(name) >= 9 &&
1567.3.14 by Monty Taylor
Removed the need for drizzled::internal usage in drizzle.cc.
2054
                                  !strcmp(name, "delimiter"))))
1441.3.3 by Vijay Samuel
Changes made to the struct conversion
2055
      return(NULL);
1 by brian
clean slate
2056
    if ((end=strcont(name," \t")))
2057
    {
895 by Brian Aker
Completion (?) of uint conversion.
2058
      len=(uint32_t) (end - name);
1567.3.14 by Monty Taylor
Removed the need for drizzled::internal usage in drizzle.cc.
2059
      while (isspace(*end))
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
2060
        end++;
1 by brian
clean slate
2061
      if (!*end)
206.3.1 by Patrick Galbraith
Most everything working with client rename
2062
        end=0;          // no arguments to function
1 by brian
clean slate
2063
    }
2064
    else
895 by Brian Aker
Completion (?) of uint conversion.
2065
      len=(uint32_t) strlen(name);
1 by brian
clean slate
2066
  }
2067
1441.3.1 by Vijay Samuel
all required updations have been made
2068
  for (uint32_t i= 0; commands[i].getName(); i++)
1 by brian
clean slate
2069
  {
2070
    if (commands[i].func &&
1567.3.14 by Monty Taylor
Removed the need for drizzled::internal usage in drizzle.cc.
2071
        ((name && !strncmp(name, commands[i].getName(), len)
2072
          && !commands[i].getName()[len] && (!end || (end && commands[i].getTakesParams()))) || (!name && commands[i].getCmdChar() == cmd_char)))
1 by brian
clean slate
2073
    {
142.1.2 by Patrick
All DBUG_x removed from client/
2074
      return(&commands[i]);
1 by brian
clean slate
2075
    }
2076
  }
1441.3.3 by Vijay Samuel
Changes made to the struct conversion
2077
  return(NULL);
1 by brian
clean slate
2078
}
2079
2080
279.2.2 by Monty Taylor
Replaced DYNAMIC_STRING with C++ string in drizzle command line client.
2081
static bool add_line(string *buffer, char *line, char *in_string,
1441.3.1 by Vijay Samuel
all required updations have been made
2082
                        bool *ml_comment)
1 by brian
clean slate
2083
{
481 by Brian Aker
Remove all of uchar.
2084
  unsigned char inchar;
1126.4.1 by Monty Taylor
Fixed a buffer overrun that was causing some translated message output to suck.
2085
  char *pos, *out;
1441.3.1 by Vijay Samuel
all required updations have been made
2086
  Commands *com;
1 by brian
clean slate
2087
  bool need_space= 0;
2088
  bool ss_comment= 0;
142.1.2 by Patrick
All DBUG_x removed from client/
2089
1 by brian
clean slate
2090
279.2.2 by Monty Taylor
Replaced DYNAMIC_STRING with C++ string in drizzle command line client.
2091
  if (!line[0] && (buffer->empty()))
142.1.2 by Patrick
All DBUG_x removed from client/
2092
    return(0);
1441.3.1 by Vijay Samuel
all required updations have been made
2093
  if (status.getAddToHistory() && line[0] && not_in_history(line))
1 by brian
clean slate
2094
    add_history(line);
895 by Brian Aker
Completion (?) of uint conversion.
2095
  char *end_of_line=line+(uint32_t) strlen(line);
1 by brian
clean slate
2096
481 by Brian Aker
Remove all of uchar.
2097
  for (pos=out=line ; (inchar= (unsigned char) *pos) ; pos++)
1 by brian
clean slate
2098
  {
2099
    if (!preserve_comments)
2100
    {
2101
      // Skip spaces at the beggining of a statement
1567.3.14 by Monty Taylor
Removed the need for drizzled::internal usage in drizzle.cc.
2102
      if (isspace(inchar) && (out == line) &&
279.2.2 by Monty Taylor
Replaced DYNAMIC_STRING with C++ string in drizzle command line client.
2103
          (buffer->empty()))
1 by brian
clean slate
2104
        continue;
2105
    }
77.3.1 by Monty Taylor
First steps in removing sql_string and using glib instead.
2106
1 by brian
clean slate
2107
    // Accept multi-byte characters as-is
1567.3.20 by Monty Taylor
Removed UTF-8 lib that we don't use.
2108
    if (not drizzled::utf8::is_single(*pos))
1 by brian
clean slate
2109
    {
1567.3.16 by Monty Taylor
Added in proper check for mb quality before skipping based on mb length.
2110
      int length;
1567.3.20 by Monty Taylor
Removed UTF-8 lib that we don't use.
2111
      if ((length= drizzled::utf8::sequence_length(*pos)))
1 by brian
clean slate
2112
      {
1567.3.16 by Monty Taylor
Added in proper check for mb quality before skipping based on mb length.
2113
        if (!*ml_comment || preserve_comments)
2114
        {
2115
          while (length--)
2116
            *out++ = *pos++;
2117
          pos--;
2118
        }
2119
        else
2120
          pos+= length - 1;
2121
        continue;
1 by brian
clean slate
2122
      }
2123
    }
1085.3.3 by Monty Taylor
Got rid of #ifdef have utf8 stuff.
2124
    if (!*ml_comment && inchar == '\\' &&
2125
        !(*in_string && (drizzle_con_status(&con) & DRIZZLE_CON_STATUS_NO_BACKSLASH_ESCAPES)))
1 by brian
clean slate
2126
    {
2127
      // Found possbile one character command like \c
2128
481 by Brian Aker
Remove all of uchar.
2129
      if (!(inchar = (unsigned char) *++pos))
206.3.1 by Patrick Galbraith
Most everything working with client rename
2130
        break;        // readline adds one '\'
2131
      if (*in_string || inchar == 'N')  // \N is short for NULL
2132
      {          // Don't allow commands in string
77.3.1 by Monty Taylor
First steps in removing sql_string and using glib instead.
2133
        *out++='\\';
2134
        *out++= (char) inchar;
2135
        continue;
1 by brian
clean slate
2136
      }
461 by Monty Taylor
Removed NullS. bu-bye.
2137
      if ((com=find_command(NULL,(char) inchar)))
1 by brian
clean slate
2138
      {
2139
        // Flush previously accepted characters
2140
        if (out != line)
2141
        {
279.2.2 by Monty Taylor
Replaced DYNAMIC_STRING with C++ string in drizzle command line client.
2142
          buffer->append(line, (out-line));
1 by brian
clean slate
2143
          out= line;
2144
        }
77.3.1 by Monty Taylor
First steps in removing sql_string and using glib instead.
2145
2146
        if ((*com->func)(buffer,pos-1) > 0)
142.1.2 by Patrick
All DBUG_x removed from client/
2147
          return(1);                       // Quit
1441.3.1 by Vijay Samuel
all required updations have been made
2148
        if (com->getTakesParams())
1 by brian
clean slate
2149
        {
2150
          if (ss_comment)
2151
          {
2152
            /*
2153
              If a client-side macro appears inside a server-side comment,
2154
              discard all characters in the comment after the macro (that is,
2155
              until the end of the comment rather than the next delimiter)
2156
            */
2157
            for (pos++; *pos && (*pos != '*' || *(pos + 1) != '/'); pos++)
2158
              ;
2159
            pos--;
2160
          }
2161
          else
2162
          {
2163
            for (pos++ ;
2164
                 *pos && (*pos != *delimiter ||
971.1.77 by Monty Taylor
Removed mysys/mystrings things from client/ progs. Still need to get rid of my_getopt, but I think that's all now.
2165
                          strncmp(pos + 1, delimiter + 1,
2166
                                  strlen(delimiter + 1))) ; pos++)
206.3.1 by Patrick Galbraith
Most everything working with client rename
2167
              ;  // Remove parameters
1 by brian
clean slate
2168
            if (!*pos)
2169
              pos--;
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
2170
            else
1 by brian
clean slate
2171
              pos+= delimiter_length - 1; // Point at last delim char
2172
          }
2173
        }
2174
      }
2175
      else
2176
      {
1126.4.1 by Monty Taylor
Fixed a buffer overrun that was causing some translated message output to suck.
2177
        string buff(_("Unknown command: "));
2178
        buff.push_back('\'');
2179
        buff.push_back(inchar);
2180
        buff.push_back('\'');
2181
        buff.push_back('.');
2182
        if (put_info(buff.c_str(),INFO_ERROR,0,0) > 0)
77.4.1 by Monty Taylor
Merged from trunk.
2183
          return(1);
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
2184
        *out++='\\';
2185
        *out++=(char) inchar;
2186
        continue;
1 by brian
clean slate
2187
      }
2188
    }
2189
    else if (!*ml_comment && !*in_string &&
2190
             (end_of_line - pos) >= 10 &&
1567.3.14 by Monty Taylor
Removed the need for drizzled::internal usage in drizzle.cc.
2191
             !strncmp(pos, "delimiter ", 10))
1 by brian
clean slate
2192
    {
2193
      // Flush previously accepted characters
2194
      if (out != line)
2195
      {
279.2.2 by Monty Taylor
Replaced DYNAMIC_STRING with C++ string in drizzle command line client.
2196
        buffer->append(line, (out - line));
1 by brian
clean slate
2197
        out= line;
2198
      }
2199
2200
      // Flush possible comments in the buffer
279.2.2 by Monty Taylor
Replaced DYNAMIC_STRING with C++ string in drizzle command line client.
2201
      if (!buffer->empty())
1 by brian
clean slate
2202
      {
77.3.1 by Monty Taylor
First steps in removing sql_string and using glib instead.
2203
        if (com_go(buffer, 0) > 0) // < 0 is not fatal
142.1.2 by Patrick
All DBUG_x removed from client/
2204
          return(1);
77.3.3 by Monty Taylor
Last change to migrate to glib from sql_string.
2205
        assert(buffer!=NULL);
279.2.2 by Monty Taylor
Replaced DYNAMIC_STRING with C++ string in drizzle command line client.
2206
        buffer->clear();
1 by brian
clean slate
2207
      }
2208
2209
      /*
2210
        Delimiter wants the get rest of the given line as argument to
2211
        allow one to change ';' to ';;' and back
2212
      */
279.2.2 by Monty Taylor
Replaced DYNAMIC_STRING with C++ string in drizzle command line client.
2213
      buffer->append(pos);
77.3.1 by Monty Taylor
First steps in removing sql_string and using glib instead.
2214
      if (com_delimiter(buffer, pos) > 0)
142.1.2 by Patrick
All DBUG_x removed from client/
2215
        return(1);
1 by brian
clean slate
2216
279.2.2 by Monty Taylor
Replaced DYNAMIC_STRING with C++ string in drizzle command line client.
2217
      buffer->clear();
1 by brian
clean slate
2218
      break;
2219
    }
971.1.77 by Monty Taylor
Removed mysys/mystrings things from client/ progs. Still need to get rid of my_getopt, but I think that's all now.
2220
    else if (!*ml_comment && !*in_string && !strncmp(pos, delimiter,
2221
                                                     strlen(delimiter)))
1 by brian
clean slate
2222
    {
2223
      // Found a statement. Continue parsing after the delimiter
2224
      pos+= delimiter_length;
2225
2226
      if (preserve_comments)
2227
      {
1567.3.14 by Monty Taylor
Removed the need for drizzled::internal usage in drizzle.cc.
2228
        while (isspace(*pos))
1 by brian
clean slate
2229
          *out++= *pos++;
2230
      }
2231
      // Flush previously accepted characters
2232
      if (out != line)
2233
      {
279.2.2 by Monty Taylor
Replaced DYNAMIC_STRING with C++ string in drizzle command line client.
2234
        buffer->append(line, (out-line));
1 by brian
clean slate
2235
        out= line;
2236
      }
2237
2238
      if (preserve_comments && ((*pos == '#') ||
2239
                                ((*pos == '-') &&
2240
                                 (pos[1] == '-') &&
1567.3.14 by Monty Taylor
Removed the need for drizzled::internal usage in drizzle.cc.
2241
                                 isspace(pos[2]))))
1 by brian
clean slate
2242
      {
2243
        // Add trailing single line comments to this statement
279.2.2 by Monty Taylor
Replaced DYNAMIC_STRING with C++ string in drizzle command line client.
2244
        buffer->append(pos);
1 by brian
clean slate
2245
        pos+= strlen(pos);
2246
      }
2247
2248
      pos--;
2249
279.2.2 by Monty Taylor
Replaced DYNAMIC_STRING with C++ string in drizzle command line client.
2250
      if ((com= find_command(buffer->c_str(), 0)))
1 by brian
clean slate
2251
      {
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
2252
279.2.2 by Monty Taylor
Replaced DYNAMIC_STRING with C++ string in drizzle command line client.
2253
        if ((*com->func)(buffer, buffer->c_str()) > 0)
77.4.1 by Monty Taylor
Merged from trunk.
2254
          return(1);                       // Quit
1 by brian
clean slate
2255
      }
2256
      else
2257
      {
77.3.1 by Monty Taylor
First steps in removing sql_string and using glib instead.
2258
        if (com_go(buffer, 0) > 0)             // < 0 is not fatal
142.1.2 by Patrick
All DBUG_x removed from client/
2259
          return(1);
1 by brian
clean slate
2260
      }
279.2.2 by Monty Taylor
Replaced DYNAMIC_STRING with C++ string in drizzle command line client.
2261
      buffer->clear();
1 by brian
clean slate
2262
    }
77.3.1 by Monty Taylor
First steps in removing sql_string and using glib instead.
2263
    else if (!*ml_comment
2264
             && (!*in_string
2265
                 && (inchar == '#'
2266
                     || (inchar == '-'
2267
                         && pos[1] == '-'
1567.3.14 by Monty Taylor
Removed the need for drizzled::internal usage in drizzle.cc.
2268
                         && isspace(pos[2])))))
1 by brian
clean slate
2269
    {
2270
      // Flush previously accepted characters
2271
      if (out != line)
2272
      {
279.2.2 by Monty Taylor
Replaced DYNAMIC_STRING with C++ string in drizzle command line client.
2273
        buffer->append(line, (out - line));
1 by brian
clean slate
2274
        out= line;
2275
      }
2276
2277
      // comment to end of line
2278
      if (preserve_comments)
279.2.2 by Monty Taylor
Replaced DYNAMIC_STRING with C++ string in drizzle command line client.
2279
        buffer->append(pos);
1 by brian
clean slate
2280
2281
      break;
2282
    }
2283
    else if (!*in_string && inchar == '/' && *(pos+1) == '*' &&
77.3.1 by Monty Taylor
First steps in removing sql_string and using glib instead.
2284
             *(pos+2) != '!')
1 by brian
clean slate
2285
    {
2286
      if (preserve_comments)
2287
      {
2288
        *out++= *pos++;                       // copy '/'
2289
        *out++= *pos;                         // copy '*'
2290
      }
2291
      else
2292
        pos++;
2293
      *ml_comment= 1;
2294
      if (out != line)
2295
      {
279.2.2 by Monty Taylor
Replaced DYNAMIC_STRING with C++ string in drizzle command line client.
2296
        buffer->append(line, (out-line));
1 by brian
clean slate
2297
        out=line;
2298
      }
2299
    }
2300
    else if (*ml_comment && !ss_comment && inchar == '*' && *(pos + 1) == '/')
2301
    {
2302
      if (preserve_comments)
2303
      {
2304
        *out++= *pos++;                       // copy '*'
2305
        *out++= *pos;                         // copy '/'
2306
      }
2307
      else
2308
        pos++;
2309
      *ml_comment= 0;
2310
      if (out != line)
2311
      {
279.2.2 by Monty Taylor
Replaced DYNAMIC_STRING with C++ string in drizzle command line client.
2312
        buffer->append(line, (out - line));
1 by brian
clean slate
2313
        out= line;
2314
      }
2315
      // Consumed a 2 chars or more, and will add 1 at most,
2316
      // so using the 'line' buffer to edit data in place is ok.
2317
      need_space= 1;
77.3.1 by Monty Taylor
First steps in removing sql_string and using glib instead.
2318
    }
1 by brian
clean slate
2319
    else
77.3.1 by Monty Taylor
First steps in removing sql_string and using glib instead.
2320
    {
2321
      // Add found char to buffer
1 by brian
clean slate
2322
      if (!*in_string && inchar == '/' && *(pos + 1) == '*' &&
2323
          *(pos + 2) == '!')
2324
        ss_comment= 1;
2325
      else if (!*in_string && ss_comment && inchar == '*' && *(pos + 1) == '/')
2326
        ss_comment= 0;
2327
      if (inchar == *in_string)
77.3.1 by Monty Taylor
First steps in removing sql_string and using glib instead.
2328
        *in_string= 0;
1 by brian
clean slate
2329
      else if (!*ml_comment && !*in_string &&
77.3.1 by Monty Taylor
First steps in removing sql_string and using glib instead.
2330
               (inchar == '\'' || inchar == '"' || inchar == '`'))
2331
        *in_string= (char) inchar;
1 by brian
clean slate
2332
      if (!*ml_comment || preserve_comments)
2333
      {
1567.3.14 by Monty Taylor
Removed the need for drizzled::internal usage in drizzle.cc.
2334
        if (need_space && !isspace((char)inchar))
1 by brian
clean slate
2335
          *out++= ' ';
2336
        need_space= 0;
2337
        *out++= (char) inchar;
2338
      }
2339
    }
2340
  }
279.2.2 by Monty Taylor
Replaced DYNAMIC_STRING with C++ string in drizzle command line client.
2341
  if (out != line || (buffer->length() > 0))
1 by brian
clean slate
2342
  {
2343
    *out++='\n';
895 by Brian Aker
Completion (?) of uint conversion.
2344
    uint32_t length=(uint32_t) (out-line);
1909.5.1 by Andrew Hutchings
Re-write of linebuffer to stop buffer overrrun and hopefully improve performance
2345
    if ((buffer->length() + length) > opt_max_input_line)
2346
    {
2347
      status.setExitStatus(1);
2348
      put_info(_("Not found a delimiter within max_input_line of input"), INFO_ERROR, 0, 0);
2349
      return 1;
2350
    }
279.2.2 by Monty Taylor
Replaced DYNAMIC_STRING with C++ string in drizzle command line client.
2351
    if ((!*ml_comment || preserve_comments))
2352
      buffer->append(line, length);
1 by brian
clean slate
2353
  }
142.1.2 by Patrick
All DBUG_x removed from client/
2354
  return(0);
1 by brian
clean slate
2355
}
2356
2357
/*****************************************************************
1441.3.1 by Vijay Samuel
all required updations have been made
2358
            Interface to Readline Completion
2359
******************************************************************/
1 by brian
clean slate
2360
2361
182.1.6 by Jim Winstead
Fix declaration of no_completion, drop 'new_' prefix from a function to make
2362
static char **mysql_completion (const char *text, int start, int end);
520.4.43 by mordred
A set of Solaris fixes.
2363
extern "C" char *new_command_generator(const char *text, int);
182.1.6 by Jim Winstead
Fix declaration of no_completion, drop 'new_' prefix from a function to make
2364
1 by brian
clean slate
2365
/*
2366
  Tell the GNU Readline library how to complete.  We want to try to complete
2367
  on command names if this is the first word in the line, or on filenames
2368
  if not.
2369
*/
632.1.12 by Monty Taylor
Fixed more sun studio warnings.
2370
static char *no_completion(const char *, int)
1 by brian
clean slate
2371
{
77.3.1 by Monty Taylor
First steps in removing sql_string and using glib instead.
2372
  /* No filename completion */
2373
  return 0;
1 by brian
clean slate
2374
}
182.1.6 by Jim Winstead
Fix declaration of no_completion, drop 'new_' prefix from a function to make
2375
1 by brian
clean slate
2376
77.3.1 by Monty Taylor
First steps in removing sql_string and using glib instead.
2377
/* glues pieces of history back together if in pieces   */
279.2.2 by Monty Taylor
Replaced DYNAMIC_STRING with C++ string in drizzle command line client.
2378
static void fix_history(string *final_command)
1 by brian
clean slate
2379
{
2380
  int total_lines = 1;
279.2.2 by Monty Taylor
Replaced DYNAMIC_STRING with C++ string in drizzle command line client.
2381
  const char *ptr = final_command->c_str();
182.1.2 by Jim Winstead
Various fixes to enable compilation on Mac OS X, and remove the glib dependency.
2382
  char str_char = '\0';  /* Character if we are in a string or not */
2383
77.3.1 by Monty Taylor
First steps in removing sql_string and using glib instead.
2384
  /* Converted buffer */
279.2.2 by Monty Taylor
Replaced DYNAMIC_STRING with C++ string in drizzle command line client.
2385
  string fixed_buffer;
287.3.19 by Monty Taylor
Added string.reserve() calls.
2386
  fixed_buffer.reserve(512);
77.3.1 by Monty Taylor
First steps in removing sql_string and using glib instead.
2387
1 by brian
clean slate
2388
  /* find out how many lines we have and remove newlines */
77.3.1 by Monty Taylor
First steps in removing sql_string and using glib instead.
2389
  while (*ptr != '\0')
1 by brian
clean slate
2390
  {
2391
    switch (*ptr) {
2392
      /* string character */
2393
    case '"':
2394
    case '\'':
2395
    case '`':
77.3.1 by Monty Taylor
First steps in removing sql_string and using glib instead.
2396
      // open string
2397
      if (str_char == '\0')
2398
        str_char = *ptr;
1 by brian
clean slate
2399
      else if (str_char == *ptr)   /* close string */
77.3.1 by Monty Taylor
First steps in removing sql_string and using glib instead.
2400
        str_char = '\0';
279.2.2 by Monty Taylor
Replaced DYNAMIC_STRING with C++ string in drizzle command line client.
2401
      fixed_buffer.append(ptr, 1);
1 by brian
clean slate
2402
      break;
2403
    case '\n':
77.3.1 by Monty Taylor
First steps in removing sql_string and using glib instead.
2404
      /*
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
2405
        not in string, change to space
2406
        if in string, leave it alone
1 by brian
clean slate
2407
      */
279.2.2 by Monty Taylor
Replaced DYNAMIC_STRING with C++ string in drizzle command line client.
2408
      fixed_buffer.append((str_char == '\0') ? " " : "\n");
1 by brian
clean slate
2409
      total_lines++;
2410
      break;
2411
    case '\\':
279.2.2 by Monty Taylor
Replaced DYNAMIC_STRING with C++ string in drizzle command line client.
2412
      fixed_buffer.append("\\");
1 by brian
clean slate
2413
      /* need to see if the backslash is escaping anything */
77.3.1 by Monty Taylor
First steps in removing sql_string and using glib instead.
2414
      if (str_char)
1 by brian
clean slate
2415
      {
77.3.1 by Monty Taylor
First steps in removing sql_string and using glib instead.
2416
        ptr++;
2417
        /* special characters that need escaping */
2418
        if (*ptr == '\'' || *ptr == '"' || *ptr == '\\')
279.2.2 by Monty Taylor
Replaced DYNAMIC_STRING with C++ string in drizzle command line client.
2419
          fixed_buffer.append(ptr, 1);
77.3.1 by Monty Taylor
First steps in removing sql_string and using glib instead.
2420
        else
2421
          ptr--;
1 by brian
clean slate
2422
      }
2423
      break;
2424
    default:
279.2.2 by Monty Taylor
Replaced DYNAMIC_STRING with C++ string in drizzle command line client.
2425
      fixed_buffer.append(ptr, 1);
1 by brian
clean slate
2426
    }
2427
    ptr++;
2428
  }
77.3.1 by Monty Taylor
First steps in removing sql_string and using glib instead.
2429
  if (total_lines > 1)
279.2.2 by Monty Taylor
Replaced DYNAMIC_STRING with C++ string in drizzle command line client.
2430
    add_history(fixed_buffer.c_str());
1 by brian
clean slate
2431
}
2432
77.3.1 by Monty Taylor
First steps in removing sql_string and using glib instead.
2433
/*
1 by brian
clean slate
2434
  returns 0 if line matches the previous history entry
2435
  returns 1 if the line doesn't match the previous history entry
2436
*/
77.3.1 by Monty Taylor
First steps in removing sql_string and using glib instead.
2437
static int not_in_history(const char *line)
1 by brian
clean slate
2438
{
2439
  HIST_ENTRY *oldhist = history_get(history_length);
77.3.1 by Monty Taylor
First steps in removing sql_string and using glib instead.
2440
1 by brian
clean slate
2441
  if (oldhist == 0)
2442
    return 1;
2443
  if (strcmp(oldhist->line,line) == 0)
2444
    return 0;
2445
  return 1;
2446
}
2447
1567.5.2 by Vijay Samuel
Merge OSX fix for client drizzle.
2448
static void initialize_readline (char *name)
1 by brian
clean slate
2449
{
2450
  /* Allow conditional parsing of the ~/.inputrc file. */
319 by Brian Aker
Fix (yuck!) for OSX/Google bug.
2451
  rl_readline_name= name;
1 by brian
clean slate
2452
2453
  /* Tell the completer that we want a crack first. */
236.1.15 by Monty Taylor
Fixed the previous merge. (since I suck)
2454
  rl_attempted_completion_function= (rl_completion_func_t*)&mysql_completion;
1149.2.1 by Monty Taylor
Workaround for half-present new-interface on Snow Leopard, combined
2455
  rl_completion_entry_function= (drizzle_compentry_func_t*)&no_completion;
1 by brian
clean slate
2456
}
2457
182.1.6 by Jim Winstead
Fix declaration of no_completion, drop 'new_' prefix from a function to make
2458
1 by brian
clean slate
2459
/*
2460
  Attempt to complete on the contents of TEXT.  START and END show the
2461
  region of TEXT that contains the word to complete.  We can use the
2462
  entire line in case we want to do some simple parsing.  Return the
2463
  array of matches, or NULL if there aren't any.
2464
*/
632.1.12 by Monty Taylor
Fixed more sun studio warnings.
2465
char **mysql_completion (const char *text, int, int)
1 by brian
clean slate
2466
{
1441.3.1 by Vijay Samuel
all required updations have been made
2467
  if (!status.getBatch() && !quick)
1 by brian
clean slate
2468
    return rl_completion_matches(text, new_command_generator);
2469
  else
2470
    return (char**) 0;
2471
}
2472
923.2.6 by Monty Taylor
Removing hand-done completion hash functions.
2473
inline string lower_string(const string &from_string)
2474
{
2475
  string to_string= from_string;
2476
  transform(to_string.begin(), to_string.end(),
2477
            to_string.begin(), ::tolower);
2478
  return to_string;
2479
}
923.2.7 by Monty Taylor
Finished replacing custom HashMap with std::map. We use a map and not a set
2480
inline string lower_string(const char * from_string)
2481
{
2482
  string to_string= from_string;
2483
  return lower_string(to_string);
2484
}
923.2.6 by Monty Taylor
Removing hand-done completion hash functions.
2485
923.2.7 by Monty Taylor
Finished replacing custom HashMap with std::map. We use a map and not a set
2486
template <class T>
923.2.6 by Monty Taylor
Removing hand-done completion hash functions.
2487
class CompletionMatch :
2488
  public unary_function<const string&, bool>
2489
{
2490
  string match_text; 
923.2.7 by Monty Taylor
Finished replacing custom HashMap with std::map. We use a map and not a set
2491
  T match_func;
923.2.6 by Monty Taylor
Removing hand-done completion hash functions.
2492
public:
923.2.7 by Monty Taylor
Finished replacing custom HashMap with std::map. We use a map and not a set
2493
  CompletionMatch(string text) : match_text(text) {}
923.2.6 by Monty Taylor
Removing hand-done completion hash functions.
2494
  inline bool operator() (const pair<string,string> &match_against) const
2495
  {
2496
    string sub_match=
2497
      lower_string(match_against.first.substr(0,match_text.size()));
923.2.7 by Monty Taylor
Finished replacing custom HashMap with std::map. We use a map and not a set
2498
    return match_func(sub_match,match_text);
923.2.6 by Monty Taylor
Removing hand-done completion hash functions.
2499
  }
2500
};
2501
2502
2503
520.4.43 by mordred
A set of Solaris fixes.
2504
extern "C"
923.2.6 by Monty Taylor
Removing hand-done completion hash functions.
2505
char *new_command_generator(const char *text, int state)
1 by brian
clean slate
2506
{
2507
2508
  if (!state)
923.2.6 by Monty Taylor
Removing hand-done completion hash functions.
2509
  {
923.2.7 by Monty Taylor
Finished replacing custom HashMap with std::map. We use a map and not a set
2510
    completion_string= lower_string(text);
2511
    if (completion_string.size() == 0)
923.2.6 by Monty Taylor
Removing hand-done completion hash functions.
2512
    {
2513
      completion_iter= completion_map.begin();
923.2.7 by Monty Taylor
Finished replacing custom HashMap with std::map. We use a map and not a set
2514
      completion_end= completion_map.end();
923.2.6 by Monty Taylor
Removing hand-done completion hash functions.
2515
    }
2516
    else
923.2.7 by Monty Taylor
Finished replacing custom HashMap with std::map. We use a map and not a set
2517
    {
923.2.6 by Monty Taylor
Removing hand-done completion hash functions.
2518
      completion_iter= find_if(completion_map.begin(), completion_map.end(),
923.2.7 by Monty Taylor
Finished replacing custom HashMap with std::map. We use a map and not a set
2519
                               CompletionMatch<equal_to<string> >(completion_string));
2520
      completion_end= find_if(completion_iter, completion_map.end(),
2521
                              CompletionMatch<not_equal_to<string> >(completion_string));
2522
    }
923.2.6 by Monty Taylor
Removing hand-done completion hash functions.
2523
  }
923.2.7 by Monty Taylor
Finished replacing custom HashMap with std::map. We use a map and not a set
2524
  if (completion_iter == completion_end || (size_t)state > completion_map.size())
923.2.6 by Monty Taylor
Removing hand-done completion hash functions.
2525
    return NULL;
2526
  char *result= (char *)malloc((*completion_iter).second.size()+1);
2527
  strcpy(result, (*completion_iter).second.c_str());
2528
  completion_iter++;
2529
  return result;
1 by brian
clean slate
2530
}
2531
2532
/* Build up the completion hash */
2533
2534
static void build_completion_hash(bool rehash, bool write_info)
2535
{
1441.3.1 by Vijay Samuel
all required updations have been made
2536
  Commands *cmd=commands;
928.1.1 by Eric Day
Started client changes.
2537
  drizzle_return_t ret;
2538
  drizzle_result_st databases,tables,fields;
2539
  drizzle_row_t database_row,table_row;
2540
  drizzle_column_st *sql_field;
923.2.6 by Monty Taylor
Removing hand-done completion hash functions.
2541
  string tmp_str, tmp_str_lower;
142.1.2 by Patrick
All DBUG_x removed from client/
2542
1567.3.1 by Vijay Samuel
Refactoring of the drizzle client.
2543
  if (status.getBatch() || quick || current_db.empty())
206.3.1 by Patrick Galbraith
Most everything working with client rename
2544
    return;      // We don't need completion in batches
1 by brian
clean slate
2545
  if (!rehash)
142.1.2 by Patrick
All DBUG_x removed from client/
2546
    return;
1 by brian
clean slate
2547
923.2.6 by Monty Taylor
Removing hand-done completion hash functions.
2548
  completion_map.clear();
1 by brian
clean slate
2549
2550
  /* hash this file's known subset of SQL commands */
1441.3.1 by Vijay Samuel
all required updations have been made
2551
  while (cmd->getName()) {
2552
    tmp_str= cmd->getName();
923.2.6 by Monty Taylor
Removing hand-done completion hash functions.
2553
    tmp_str_lower= lower_string(tmp_str);
2554
    completion_map[tmp_str_lower]= tmp_str;
1 by brian
clean slate
2555
    cmd++;
2556
  }
2557
77.1.40 by Monty Taylor
More naming changes.
2558
  /* hash Drizzle functions (to be implemented) */
1 by brian
clean slate
2559
2560
  /* hash all database names */
928.1.4 by Eric Day
Fixed a few bugs, more progress on conversion.
2561
  if (drizzle_query_str(&con, &databases, "show databases", &ret) != NULL)
1 by brian
clean slate
2562
  {
928.1.4 by Eric Day
Fixed a few bugs, more progress on conversion.
2563
    if (ret == DRIZZLE_RETURN_OK)
1 by brian
clean slate
2564
    {
928.1.4 by Eric Day
Fixed a few bugs, more progress on conversion.
2565
      if (drizzle_result_buffer(&databases) != DRIZZLE_RETURN_OK)
2566
        put_info(drizzle_error(&drizzle),INFO_INFO,0,0);
2567
      else
1 by brian
clean slate
2568
      {
928.1.4 by Eric Day
Fixed a few bugs, more progress on conversion.
2569
        while ((database_row=drizzle_row_next(&databases)))
2570
        {
2571
          tmp_str= database_row[0];
2572
          tmp_str_lower= lower_string(tmp_str);
2573
          completion_map[tmp_str_lower]= tmp_str;
2574
        }
1 by brian
clean slate
2575
      }
2576
    }
928.1.4 by Eric Day
Fixed a few bugs, more progress on conversion.
2577
2578
    drizzle_result_free(&databases);
1 by brian
clean slate
2579
  }
928.1.4 by Eric Day
Fixed a few bugs, more progress on conversion.
2580
1 by brian
clean slate
2581
  /* hash all table names */
928.1.4 by Eric Day
Fixed a few bugs, more progress on conversion.
2582
  if (drizzle_query_str(&con, &tables, "show tables", &ret) != NULL)
1 by brian
clean slate
2583
  {
928.1.4 by Eric Day
Fixed a few bugs, more progress on conversion.
2584
    if (ret != DRIZZLE_RETURN_OK)
2585
    {
2586
      drizzle_result_free(&tables);
2587
      return;
2588
    }
2589
928.1.1 by Eric Day
Started client changes.
2590
    if (drizzle_result_buffer(&tables) != DRIZZLE_RETURN_OK)
2591
      put_info(drizzle_error(&drizzle),INFO_INFO,0,0);
1 by brian
clean slate
2592
    else
2593
    {
928.1.1 by Eric Day
Started client changes.
2594
      if (drizzle_result_row_count(&tables) > 0 && !opt_silent && write_info)
1 by brian
clean slate
2595
      {
1491.5.1 by Monty Taylor
Removed bad spacing discovered while looking at translations.
2596
        tee_fprintf(stdout,
2597
                    _("Reading table information for completion of "
2598
                      "table and column names\n"
2599
                      "You can turn off this feature to get a quicker "
2600
                      "startup with -A\n\n"));
1 by brian
clean slate
2601
      }
928.1.1 by Eric Day
Started client changes.
2602
      while ((table_row=drizzle_row_next(&tables)))
1 by brian
clean slate
2603
      {
928.1.4 by Eric Day
Fixed a few bugs, more progress on conversion.
2604
        tmp_str= table_row[0];
923.2.6 by Monty Taylor
Removing hand-done completion hash functions.
2605
        tmp_str_lower= lower_string(tmp_str);
2606
        completion_map[tmp_str_lower]= tmp_str;
1 by brian
clean slate
2607
      }
2608
    }
2609
  }
928.1.1 by Eric Day
Started client changes.
2610
  else
2611
    return;
1 by brian
clean slate
2612
2613
  /* hash all field names, both with the table prefix and without it */
928.1.1 by Eric Day
Started client changes.
2614
  if (drizzle_result_row_count(&tables) == 0)
928.1.4 by Eric Day
Fixed a few bugs, more progress on conversion.
2615
  {
2616
    drizzle_result_free(&tables);
142.1.2 by Patrick
All DBUG_x removed from client/
2617
    return;
928.1.4 by Eric Day
Fixed a few bugs, more progress on conversion.
2618
  }
928.1.1 by Eric Day
Started client changes.
2619
2620
  drizzle_row_seek(&tables, 0);
928.1.2 by Eric Day
Merged trunk.
2621
928.1.1 by Eric Day
Started client changes.
2622
  while ((table_row=drizzle_row_next(&tables)))
1 by brian
clean slate
2623
  {
923.1.7 by Brian Aker
Remove dead COM for Field lists.
2624
    string query;
2625
1937.3.1 by Andrew Hutchings
Quote marks incorrect in hash completion build query
2626
    query.append("show fields in `");
928.1.4 by Eric Day
Fixed a few bugs, more progress on conversion.
2627
    query.append(table_row[0]);
1937.3.1 by Andrew Hutchings
Quote marks incorrect in hash completion build query
2628
    query.append("`");
1441.3.1 by Vijay Samuel
all required updations have been made
2629
    
928.1.1 by Eric Day
Started client changes.
2630
    if (drizzle_query(&con, &fields, query.c_str(), query.length(),
928.1.4 by Eric Day
Fixed a few bugs, more progress on conversion.
2631
                      &ret) != NULL)
1 by brian
clean slate
2632
    {
928.1.4 by Eric Day
Fixed a few bugs, more progress on conversion.
2633
      if (ret == DRIZZLE_RETURN_OK &&
2634
          drizzle_result_buffer(&fields) == DRIZZLE_RETURN_OK)
1 by brian
clean slate
2635
      {
928.1.1 by Eric Day
Started client changes.
2636
        while ((sql_field=drizzle_column_next(&fields)))
923.1.7 by Brian Aker
Remove dead COM for Field lists.
2637
        {
928.1.4 by Eric Day
Fixed a few bugs, more progress on conversion.
2638
          tmp_str=table_row[0];
923.2.6 by Monty Taylor
Removing hand-done completion hash functions.
2639
          tmp_str.append(".");
928.1.2 by Eric Day
Merged trunk.
2640
          tmp_str.append(drizzle_column_name(sql_field));
2641
          tmp_str_lower= lower_string(tmp_str);
2642
          completion_map[tmp_str_lower]= tmp_str;
2643
2644
          tmp_str=drizzle_column_name(sql_field);
2645
          tmp_str_lower= lower_string(tmp_str);
2646
          completion_map[tmp_str_lower]= tmp_str;
923.1.7 by Brian Aker
Remove dead COM for Field lists.
2647
        }
2648
      }
928.1.4 by Eric Day
Fixed a few bugs, more progress on conversion.
2649
      drizzle_result_free(&fields);
1 by brian
clean slate
2650
    }
2651
  }
928.1.1 by Eric Day
Started client changes.
2652
  drizzle_result_free(&tables);
923.2.6 by Monty Taylor
Removing hand-done completion hash functions.
2653
  completion_iter= completion_map.begin();
1 by brian
clean slate
2654
}
2655
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
2656
/* for gnu readline */
1 by brian
clean slate
2657
2658
2659
static int reconnect(void)
2660
{
2661
  if (opt_reconnect)
2662
  {
202.3.13 by Monty Taylor
Changed gettext() to _() in drizzle.c.
2663
    put_info(_("No connection. Trying to reconnect..."),INFO_INFO,0,0);
279.2.2 by Monty Taylor
Replaced DYNAMIC_STRING with C++ string in drizzle command line client.
2664
    (void) com_connect((string *)0, 0);
1124.2.8 by Diego Medina
Fixes lp bug#432210
2665
    if (opt_rehash && connected)
1 by brian
clean slate
2666
      com_rehash(NULL, NULL);
2667
  }
1136 by Brian Aker
Merge for bug fixes.
2668
  if (! connected)
202.3.13 by Monty Taylor
Changed gettext() to _() in drizzle.c.
2669
    return put_info(_("Can't connect to the server\n"),INFO_ERROR,0,0);
1 by brian
clean slate
2670
  return 0;
2671
}
2672
77.3.7 by Monty Taylor
Made mysql into a pure-C program.
2673
static void get_current_db(void)
1 by brian
clean slate
2674
{
928.1.1 by Eric Day
Started client changes.
2675
  drizzle_return_t ret;
2676
  drizzle_result_st res;
1 by brian
clean slate
2677
1567.3.1 by Vijay Samuel
Refactoring of the drizzle client.
2678
  current_db.erase();
2679
  current_db= "";
1 by brian
clean slate
2680
  /* In case of error below current_db will be NULL */
928.1.4 by Eric Day
Fixed a few bugs, more progress on conversion.
2681
  if (drizzle_query_str(&con, &res, "SELECT DATABASE()", &ret) != NULL)
1 by brian
clean slate
2682
  {
928.1.4 by Eric Day
Fixed a few bugs, more progress on conversion.
2683
    if (ret == DRIZZLE_RETURN_OK &&
2684
        drizzle_result_buffer(&res) == DRIZZLE_RETURN_OK)
2685
    {
2686
      drizzle_row_t row= drizzle_row_next(&res);
2687
      if (row[0])
1593.1.4 by Vijay Samuel
Merge Fixes for client drizzle.
2688
        current_db.assign(row[0]);
1124.2.9 by Diego Medina
Fixed a crash that came after fixing lp bug#432210 if you call use <db_name> twice on a
2689
      drizzle_result_free(&res);
928.1.4 by Eric Day
Fixed a few bugs, more progress on conversion.
2690
    }
1 by brian
clean slate
2691
  }
2692
}
2693
2694
/***************************************************************************
1441.3.1 by Vijay Samuel
all required updations have been made
2695
 The different commands
2696
***************************************************************************/
1 by brian
clean slate
2697
1877.1.1 by Andrew Hutchings
Use size_t for command in drizzle.cc
2698
int drizzleclient_real_query_for_lazy(const char *buf, size_t length,
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
2699
                                      drizzle_result_st *result,
2700
                                      uint32_t *error_code)
1 by brian
clean slate
2701
{
928.1.1 by Eric Day
Started client changes.
2702
  drizzle_return_t ret;
2703
893 by Brian Aker
First pass of stripping uint
2704
  for (uint32_t retry=0;; retry++)
1 by brian
clean slate
2705
  {
2706
    int error;
928.1.1 by Eric Day
Started client changes.
2707
    if (drizzle_query(&con,result,buf,length,&ret) != NULL &&
2708
        ret == DRIZZLE_RETURN_OK)
2709
    {
1 by brian
clean slate
2710
      return 0;
928.1.1 by Eric Day
Started client changes.
2711
    }
2712
    error= put_error(&con, result);
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
2713
2714
    if (ret == DRIZZLE_RETURN_ERROR_CODE)
2715
    {
2716
      *error_code= drizzle_result_error_code(result);
2717
      drizzle_result_free(result);
2718
    }
2719
2720
    if (ret != DRIZZLE_RETURN_SERVER_GONE || retry > 1 ||
1 by brian
clean slate
2721
        !opt_reconnect)
928.1.1 by Eric Day
Started client changes.
2722
    {
1 by brian
clean slate
2723
      return error;
928.1.1 by Eric Day
Started client changes.
2724
    }
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
2725
1 by brian
clean slate
2726
    if (reconnect())
2727
      return error;
2728
  }
2729
}
2730
928.1.1 by Eric Day
Started client changes.
2731
int drizzleclient_store_result_for_lazy(drizzle_result_st *result)
1 by brian
clean slate
2732
{
928.1.1 by Eric Day
Started client changes.
2733
  if (drizzle_result_buffer(result) == DRIZZLE_RETURN_OK)
1 by brian
clean slate
2734
    return 0;
2735
928.1.1 by Eric Day
Started client changes.
2736
  if (drizzle_con_error(&con)[0])
1746.4.1 by Andrew Hutchings
An error whilst getting results instead of query meant results were not freed causing segfault on exit cleanup
2737
  {
2738
    int ret= put_error(&con, result);
2739
    drizzle_result_free(result);
2740
    return ret;
2741
  }
1 by brian
clean slate
2742
  return 0;
2743
}
2744
2745
static int
820.2.1 by Toru Maesaka
Killed dead code and removed a reference to a non-existent help option
2746
com_help(string *buffer, const char *)
1 by brian
clean slate
2747
{
2748
  register int i, j;
520.4.43 by mordred
A set of Solaris fixes.
2749
  char buff[32], *end;
1720.3.2 by LinuxJedi
Fix bug #617510:
2750
  std::vector<char> output_buff;
2751
  output_buff.resize(512);
1 by brian
clean slate
2752
202.3.13 by Monty Taylor
Changed gettext() to _() in drizzle.c.
2753
  put_info(_("List of all Drizzle commands:"), INFO_INFO,0,0);
1 by brian
clean slate
2754
  if (!named_cmds)
1720.3.2 by LinuxJedi
Fix bug #617510:
2755
  {
2756
    snprintf(&output_buff[0], output_buff.size(),
2757
             _("Note that all text commands must be first on line and end with '%s' or \\g"),
2758
             delimiter);
2759
    put_info(&output_buff[0], INFO_INFO, 0, 0);
2760
  }
1441.3.1 by Vijay Samuel
all required updations have been made
2761
  for (i = 0; commands[i].getName(); i++)
1 by brian
clean slate
2762
  {
1441.3.1 by Vijay Samuel
all required updations have been made
2763
    end= strcpy(buff, commands[i].getName());
2764
    end+= strlen(commands[i].getName());
2765
    for (j= (int)strlen(commands[i].getName()); j < 10; j++)
641.4.1 by Toru Maesaka
First pass of replacing MySQL's my_stpcpy() with appropriate libc calls
2766
      end= strcpy(end, " ")+1;
1 by brian
clean slate
2767
    if (commands[i].func)
2768
      tee_fprintf(stdout, "%s(\\%c) %s\n", buff,
1441.3.1 by Vijay Samuel
all required updations have been made
2769
                  commands[i].getCmdChar(), _(commands[i].getDoc()));
1 by brian
clean slate
2770
  }
820.2.1 by Toru Maesaka
Killed dead code and removed a reference to a non-existent help option
2771
  tee_fprintf(stdout, "\n");
2772
  buffer->clear();
1 by brian
clean slate
2773
  return 0;
2774
}
2775
2776
2777
static int
632.1.12 by Monty Taylor
Fixed more sun studio warnings.
2778
com_clear(string *buffer, const char *)
1 by brian
clean slate
2779
{
1441.3.1 by Vijay Samuel
all required updations have been made
2780
  if (status.getAddToHistory())
1 by brian
clean slate
2781
    fix_history(buffer);
279.2.2 by Monty Taylor
Replaced DYNAMIC_STRING with C++ string in drizzle command line client.
2782
  buffer->clear();
1 by brian
clean slate
2783
  return 0;
2784
}
2785
2786
2787
/*
2788
  Execute command
1441.3.1 by Vijay Samuel
all required updations have been made
2789
  Returns: 0  if ok
2790
  -1 if not fatal error
2791
  1  if fatal error
1 by brian
clean slate
2792
*/
2793
static int
632.1.12 by Monty Taylor
Fixed more sun studio warnings.
2794
com_go(string *buffer, const char *)
1 by brian
clean slate
2795
{
77.3.1 by Monty Taylor
First steps in removing sql_string and using glib instead.
2796
  char          buff[200]; /* about 110 chars used so far */
2797
  char          time_buff[52+3+1]; /* time max + space&parens + NUL */
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
2798
  drizzle_result_st result;
2799
  drizzle_return_t ret;
2800
  uint32_t      timer, warnings= 0;
2801
  uint32_t      error= 0;
2802
  uint32_t      error_code= 0;
1 by brian
clean slate
2803
  int           err= 0;
2804
2805
  interrupted_query= 0;
2806
2807
  /* Remove garbage for nicer messages */
77.3.1 by Monty Taylor
First steps in removing sql_string and using glib instead.
2808
  remove_cntrl(buffer);
1 by brian
clean slate
2809
287.3.17 by Monty Taylor
Fixed null-query problem.
2810
  if (buffer->empty())
1 by brian
clean slate
2811
  {
77.3.1 by Monty Taylor
First steps in removing sql_string and using glib instead.
2812
    // Ignore empty quries
1441.3.1 by Vijay Samuel
all required updations have been made
2813
    if (status.getBatch())
1 by brian
clean slate
2814
      return 0;
202.3.13 by Monty Taylor
Changed gettext() to _() in drizzle.c.
2815
    return put_info(_("No query specified\n"),INFO_ERROR,0,0);
1 by brian
clean slate
2816
2817
  }
2818
  if (!connected && reconnect())
2819
  {
77.3.1 by Monty Taylor
First steps in removing sql_string and using glib instead.
2820
    // Remove query on error
279.2.2 by Monty Taylor
Replaced DYNAMIC_STRING with C++ string in drizzle command line client.
2821
    buffer->clear();
1 by brian
clean slate
2822
    return opt_reconnect ? -1 : 1;          // Fatal error
2823
  }
2824
  if (verbose)
77.3.1 by Monty Taylor
First steps in removing sql_string and using glib instead.
2825
    (void) com_print(buffer, 0);
1 by brian
clean slate
2826
2827
  if (skip_updates &&
279.2.2 by Monty Taylor
Replaced DYNAMIC_STRING with C++ string in drizzle command line client.
2828
      ((buffer->length() < 4) || (buffer->find( "SET ") != 0)))
1 by brian
clean slate
2829
  {
202.3.13 by Monty Taylor
Changed gettext() to _() in drizzle.c.
2830
    (void) put_info(_("Ignoring query to other database"),INFO_INFO,0,0);
1 by brian
clean slate
2831
    return 0;
2832
  }
2833
2834
  timer=start_timer();
2835
  executing_query= 1;
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
2836
  error= drizzleclient_real_query_for_lazy(buffer->c_str(),buffer->length(),&result, &error_code);
1 by brian
clean slate
2837
1441.3.1 by Vijay Samuel
all required updations have been made
2838
  if (status.getAddToHistory())
77.3.1 by Monty Taylor
First steps in removing sql_string and using glib instead.
2839
  {
279.2.2 by Monty Taylor
Replaced DYNAMIC_STRING with C++ string in drizzle command line client.
2840
    buffer->append(vertical ? "\\G" : delimiter);
1 by brian
clean slate
2841
    /* Append final command onto history */
2842
    fix_history(buffer);
2843
  }
2844
279.2.2 by Monty Taylor
Replaced DYNAMIC_STRING with C++ string in drizzle command line client.
2845
  buffer->clear();
1 by brian
clean slate
2846
2847
  if (error)
2848
    goto end;
2849
2850
  do
2851
  {
2852
    char *pos;
2853
2854
    if (quick)
2855
    {
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
2856
      if (drizzle_column_buffer(&result) != DRIZZLE_RETURN_OK)
1 by brian
clean slate
2857
      {
928.1.1 by Eric Day
Started client changes.
2858
        error= put_error(&con, &result);
1 by brian
clean slate
2859
        goto end;
2860
      }
2861
    }
2862
    else
2863
    {
840.1.21 by Monty Taylor
ZOMG. Renamed all the rest of the stuff in libdrizzleclient to be drizzleclient_*. I love commandline perl.
2864
      error= drizzleclient_store_result_for_lazy(&result);
1 by brian
clean slate
2865
      if (error)
2866
        goto end;
2867
    }
2868
2869
    if (verbose >= 3 || !opt_silent)
206.3.1 by Patrick Galbraith
Most everything working with client rename
2870
      drizzle_end_timer(timer,time_buff);
1 by brian
clean slate
2871
    else
2872
      time_buff[0]= '\0';
2873
2874
    /* Every branch must truncate  buff . */
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
2875
    if (drizzle_result_column_count(&result) > 0)
1 by brian
clean slate
2876
    {
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
2877
      if (!quick && drizzle_result_row_count(&result) == 0 &&
2878
          !column_types_flag)
1 by brian
clean slate
2879
      {
641.4.1 by Toru Maesaka
First pass of replacing MySQL's my_stpcpy() with appropriate libc calls
2880
        strcpy(buff, _("Empty set"));
1 by brian
clean slate
2881
      }
2882
      else
2883
      {
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
2884
        init_pager();
266.2.1 by Jim Winstead
Remove HTML and XML output support from drizzle client
2885
        if (vertical || (auto_vertical_output &&
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
2886
                         (terminal_width < get_result_width(&result))))
2887
          print_table_data_vertically(&result);
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
2888
        else if (opt_silent && verbose <= 2 && !output_tables)
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
2889
          print_tab_data(&result);
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
2890
        else
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
2891
          print_table_data(&result);
1441.3.1 by Vijay Samuel
all required updations have been made
2892
        sprintf(buff,
2893
                ngettext("%ld row in set","%ld rows in set",
2894
                         (long) drizzle_result_row_count(&result)),
2895
                (long) drizzle_result_row_count(&result));
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
2896
        end_pager();
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
2897
        if (drizzle_result_error_code(&result))
2898
          error= put_error(&con, &result);
1 by brian
clean slate
2899
      }
2900
    }
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
2901
    else if (drizzle_result_affected_rows(&result) == ~(uint64_t) 0)
641.4.1 by Toru Maesaka
First pass of replacing MySQL's my_stpcpy() with appropriate libc calls
2902
      strcpy(buff,_("Query OK"));
1 by brian
clean slate
2903
    else
1441.3.1 by Vijay Samuel
all required updations have been made
2904
      sprintf(buff, ngettext("Query OK, %ld row affected",
2905
                             "Query OK, %ld rows affected",
2906
                             (long) drizzle_result_affected_rows(&result)),
2907
              (long) drizzle_result_affected_rows(&result));
1 by brian
clean slate
2908
376 by Brian Aker
strend remove
2909
    pos= strchr(buff, '\0');
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
2910
    if ((warnings= drizzle_result_warning_count(&result)))
1 by brian
clean slate
2911
    {
2912
      *pos++= ',';
2913
      *pos++= ' ';
971.1.77 by Monty Taylor
Removed mysys/mystrings things from client/ progs. Still need to get rid of my_getopt, but I think that's all now.
2914
      char warnings_buff[20];
2915
      memset(warnings_buff,0,20);
1441.3.1 by Vijay Samuel
all required updations have been made
2916
      sprintf(warnings_buff, "%d", warnings);
971.1.77 by Monty Taylor
Removed mysys/mystrings things from client/ progs. Still need to get rid of my_getopt, but I think that's all now.
2917
      strcpy(pos, warnings_buff);
2918
      pos+= strlen(warnings_buff);
641.4.1 by Toru Maesaka
First pass of replacing MySQL's my_stpcpy() with appropriate libc calls
2919
      pos= strcpy(pos, " warning")+8;
1 by brian
clean slate
2920
      if (warnings != 1)
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
2921
        *pos++= 's';
1 by brian
clean slate
2922
    }
641.4.1 by Toru Maesaka
First pass of replacing MySQL's my_stpcpy() with appropriate libc calls
2923
    strcpy(pos, time_buff);
77.3.7 by Monty Taylor
Made mysql into a pure-C program.
2924
    put_info(buff,INFO_RESULT,0,0);
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
2925
    if (strcmp(drizzle_result_info(&result), ""))
2926
      put_info(drizzle_result_info(&result),INFO_RESULT,0,0);
206.3.1 by Patrick Galbraith
Most everything working with client rename
2927
    put_info("",INFO_RESULT,0,0);      // Empty row
1 by brian
clean slate
2928
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
2929
    if (unbuffered)
1 by brian
clean slate
2930
      fflush(stdout);
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
2931
    drizzle_result_free(&result);
2932
2933
    if (drizzle_con_status(&con) & DRIZZLE_CON_STATUS_MORE_RESULTS_EXISTS)
2934
    {
2935
      if (drizzle_result_read(&con, &result, &ret) == NULL ||
2936
          ret != DRIZZLE_RETURN_OK)
2937
      {
2938
        if (ret == DRIZZLE_RETURN_ERROR_CODE)
2939
        {
2940
          error_code= drizzle_result_error_code(&result);
2941
          drizzle_result_free(&result);
2942
        }
2943
2944
        error= put_error(&con, NULL);
2945
        goto end;
2946
      }
2947
    }
2948
2949
  } while (drizzle_con_status(&con) & DRIZZLE_CON_STATUS_MORE_RESULTS_EXISTS);
1 by brian
clean slate
2950
  if (err >= 1)
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
2951
    error= put_error(&con, NULL);
1 by brian
clean slate
2952
2953
end:
2954
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
2955
  /* Show warnings if any or error occured */
1 by brian
clean slate
2956
  if (show_warnings == 1 && (warnings >= 1 || error))
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
2957
    print_warnings(error_code);
1 by brian
clean slate
2958
1441.3.1 by Vijay Samuel
all required updations have been made
2959
  if (!error && !status.getBatch() &&
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
2960
      drizzle_con_status(&con) & DRIZZLE_CON_STATUS_DB_DROPPED)
2961
  {
1 by brian
clean slate
2962
    get_current_db();
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
2963
  }
1 by brian
clean slate
2964
2965
  executing_query= 0;
206.3.1 by Patrick Galbraith
Most everything working with client rename
2966
  return error;        /* New command follows */
1 by brian
clean slate
2967
}
2968
2969
2970
static void init_pager()
2971
{
2972
  if (!opt_nopager)
2973
  {
1567.3.17 by Monty Taylor
Removed more C-string strings. replaced with std::string.
2974
    if (!(PAGER= popen(pager.c_str(), "w")))
1 by brian
clean slate
2975
    {
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
2976
      tee_fprintf(stdout,_( "popen() failed! defaulting PAGER to stdout!\n"));
1 by brian
clean slate
2977
      PAGER= stdout;
2978
    }
2979
  }
2980
  else
2981
    PAGER= stdout;
2982
}
2983
2984
static void end_pager()
2985
{
2986
  if (!opt_nopager)
2987
    pclose(PAGER);
2988
}
2989
2990
2991
static void init_tee(const char *file_name)
2992
{
2993
  FILE* new_outfile;
2994
  if (opt_outfile)
2995
    end_tee();
910.1.3 by Brian Aker
Remove my_fopen() and key_map.cc file (thanks to Jay's lcov)
2996
  if (!(new_outfile= fopen(file_name, "a")))
1 by brian
clean slate
2997
  {
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
2998
    tee_fprintf(stdout, _("Error logging to file '%s'\n"), file_name);
1 by brian
clean slate
2999
    return;
3000
  }
3001
  OUTFILE = new_outfile;
1567.3.17 by Monty Taylor
Removed more C-string strings. replaced with std::string.
3002
  outfile.assign(file_name);
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
3003
  tee_fprintf(stdout, _("Logging to file '%s'\n"), file_name);
1 by brian
clean slate
3004
  opt_outfile= 1;
910.1.3 by Brian Aker
Remove my_fopen() and key_map.cc file (thanks to Jay's lcov)
3005
1 by brian
clean slate
3006
  return;
3007
}
3008
3009
3010
static void end_tee()
3011
{
910.1.3 by Brian Aker
Remove my_fopen() and key_map.cc file (thanks to Jay's lcov)
3012
  fclose(OUTFILE);
1 by brian
clean slate
3013
  OUTFILE= 0;
3014
  opt_outfile= 0;
3015
  return;
3016
}
3017
3018
3019
static int
279.2.2 by Monty Taylor
Replaced DYNAMIC_STRING with C++ string in drizzle command line client.
3020
com_ego(string *buffer,const char *line)
1 by brian
clean slate
3021
{
3022
  int result;
3023
  bool oldvertical=vertical;
3024
  vertical=1;
3025
  result=com_go(buffer,line);
3026
  vertical=oldvertical;
3027
  return result;
3028
}
3029
3030
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3031
static const char *fieldtype2str(drizzle_column_type_t type)
1 by brian
clean slate
3032
{
3033
  switch (type) {
1441.3.1 by Vijay Samuel
all required updations have been made
3034
    case DRIZZLE_COLUMN_TYPE_BLOB:        return "BLOB";
3035
    case DRIZZLE_COLUMN_TYPE_DATE:        return "DATE";
3036
    case DRIZZLE_COLUMN_TYPE_DATETIME:    return "DATETIME";
3037
    case DRIZZLE_COLUMN_TYPE_NEWDECIMAL:  return "DECIMAL";
3038
    case DRIZZLE_COLUMN_TYPE_DOUBLE:      return "DOUBLE";
3039
    case DRIZZLE_COLUMN_TYPE_ENUM:        return "ENUM";
3040
    case DRIZZLE_COLUMN_TYPE_LONG:        return "LONG";
3041
    case DRIZZLE_COLUMN_TYPE_LONGLONG:    return "LONGLONG";
3042
    case DRIZZLE_COLUMN_TYPE_NULL:        return "NULL";
3043
    case DRIZZLE_COLUMN_TYPE_TIMESTAMP:   return "TIMESTAMP";
3044
    default:                     return "?-unknown-?";
1 by brian
clean slate
3045
  }
3046
}
3047
893 by Brian Aker
First pass of stripping uint
3048
static char *fieldflags2str(uint32_t f) {
1 by brian
clean slate
3049
  static char buf[1024];
3050
  char *s=buf;
3051
  *s=0;
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
3052
#define ff2s_check_flag(X)                                              \
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3053
  if (f & DRIZZLE_COLUMN_FLAGS_ ## X) { s=strcpy(s, # X " ")+strlen(# X " "); \
1441.3.1 by Vijay Samuel
all required updations have been made
3054
                        f &= ~ DRIZZLE_COLUMN_FLAGS_ ## X; }
1 by brian
clean slate
3055
  ff2s_check_flag(NOT_NULL);
3056
  ff2s_check_flag(PRI_KEY);
3057
  ff2s_check_flag(UNIQUE_KEY);
3058
  ff2s_check_flag(MULTIPLE_KEY);
3059
  ff2s_check_flag(BLOB);
3060
  ff2s_check_flag(UNSIGNED);
3061
  ff2s_check_flag(BINARY);
3062
  ff2s_check_flag(ENUM);
3063
  ff2s_check_flag(AUTO_INCREMENT);
3064
  ff2s_check_flag(TIMESTAMP);
3065
  ff2s_check_flag(SET);
3066
  ff2s_check_flag(NO_DEFAULT_VALUE);
3067
  ff2s_check_flag(NUM);
3068
  ff2s_check_flag(PART_KEY);
3069
  ff2s_check_flag(GROUP);
3070
  ff2s_check_flag(UNIQUE);
3071
  ff2s_check_flag(BINCMP);
3072
  ff2s_check_flag(ON_UPDATE_NOW);
3073
#undef ff2s_check_flag
3074
  if (f)
1441.3.1 by Vijay Samuel
all required updations have been made
3075
    sprintf(s, " unknows=0x%04x", f);
1 by brian
clean slate
3076
  return buf;
3077
}
3078
3079
static void
928.1.1 by Eric Day
Started client changes.
3080
print_field_types(drizzle_result_st *result)
1 by brian
clean slate
3081
{
928.1.1 by Eric Day
Started client changes.
3082
  drizzle_column_st   *field;
893 by Brian Aker
First pass of stripping uint
3083
  uint32_t i=0;
1 by brian
clean slate
3084
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3085
  while ((field = drizzle_column_next(result)))
1 by brian
clean slate
3086
  {
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
3087
    tee_fprintf(PAGER, _("Field %3u:  `%s`\n"
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
3088
                "Catalog:    `%s`\n"
3089
                "Database:   `%s`\n"
3090
                "Table:      `%s`\n"
3091
                "Org_table:  `%s`\n"
1567.3.14 by Monty Taylor
Removed the need for drizzled::internal usage in drizzle.cc.
3092
                "Type:       UTF-8\n"
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
3093
                "Collation:  %s (%u)\n"
3094
                "Length:     %lu\n"
3095
                "Max_length: %lu\n"
3096
                "Decimals:   %u\n"
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
3097
                "Flags:      %s\n\n"),
1 by brian
clean slate
3098
                ++i,
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3099
                drizzle_column_name(field), drizzle_column_catalog(field),
3100
                drizzle_column_db(field), drizzle_column_table(field),
3101
                drizzle_column_orig_table(field),
3102
                fieldtype2str(drizzle_column_type(field)),
3103
                drizzle_column_charset(field), drizzle_column_size(field),
3104
                drizzle_column_max_size(field), drizzle_column_decimals(field),
3105
                fieldflags2str(drizzle_column_flags(field)));
1 by brian
clean slate
3106
  }
3107
  tee_puts("", PAGER);
3108
}
3109
3110
static void
928.1.1 by Eric Day
Started client changes.
3111
print_table_data(drizzle_result_st *result)
1 by brian
clean slate
3112
{
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3113
  drizzle_row_t cur;
3114
  drizzle_return_t ret;
3115
  drizzle_column_st *field;
1707.1.22 by Brian Aker
Small cleanup in mysql.cc
3116
  std::vector<bool> num_flag;
279.2.2 by Monty Taylor
Replaced DYNAMIC_STRING with C++ string in drizzle command line client.
3117
  string separator;
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
3118
287.3.19 by Monty Taylor
Added string.reserve() calls.
3119
  separator.reserve(256);
1 by brian
clean slate
3120
1707.1.22 by Brian Aker
Small cleanup in mysql.cc
3121
  num_flag.resize(drizzle_result_column_count(result));
1 by brian
clean slate
3122
  if (column_types_flag)
3123
  {
3124
    print_field_types(result);
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3125
    if (!drizzle_result_row_count(result))
1 by brian
clean slate
3126
      return;
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3127
    drizzle_column_seek(result,0);
1 by brian
clean slate
3128
  }
279.2.2 by Monty Taylor
Replaced DYNAMIC_STRING with C++ string in drizzle command line client.
3129
  separator.append("+");
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3130
  while ((field = drizzle_column_next(result)))
1 by brian
clean slate
3131
  {
840.3.1 by Toru Maesaka
Fixed the issue of seeing an oversized separator when rendering a multibyte string to console.
3132
    uint32_t x, length= 0;
3133
3134
    if (column_names)
3135
    {
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3136
      uint32_t name_length= strlen(drizzle_column_name(field));
3137
840.3.1 by Toru Maesaka
Fixed the issue of seeing an oversized separator when rendering a multibyte string to console.
3138
      /* Check if the max_byte value is really the maximum in terms
1441.3.1 by Vijay Samuel
all required updations have been made
3139
         of visual length since multibyte characters can affect the
3140
         length of the separator. */
1567.3.21 by Monty Taylor
Moved function from drizzle client to the utf8 header where it belongs.
3141
      length= drizzled::utf8::char_length(drizzle_column_name(field));
840.3.1 by Toru Maesaka
Fixed the issue of seeing an oversized separator when rendering a multibyte string to console.
3142
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3143
      if (name_length == drizzle_column_max_size(field))
840.1.15 by Monty Taylor
Merged from Toru.
3144
      {
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3145
        if (length < drizzle_column_max_size(field))
3146
          drizzle_column_set_max_size(field, length);
840.1.15 by Monty Taylor
Merged from Toru.
3147
      }
840.3.1 by Toru Maesaka
Fixed the issue of seeing an oversized separator when rendering a multibyte string to console.
3148
      else
840.1.15 by Monty Taylor
Merged from Toru.
3149
      {
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3150
        length= name_length;
840.1.15 by Monty Taylor
Merged from Toru.
3151
      }
840.3.1 by Toru Maesaka
Fixed the issue of seeing an oversized separator when rendering a multibyte string to console.
3152
    }
1441.3.1 by Vijay Samuel
all required updations have been made
3153
  
1 by brian
clean slate
3154
    if (quick)
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3155
      length=max(length,drizzle_column_size(field));
1 by brian
clean slate
3156
    else
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3157
      length=max(length,(uint32_t)drizzle_column_max_size(field));
3158
    if (length < 4 &&
3159
        !(drizzle_column_flags(field) & DRIZZLE_COLUMN_FLAGS_NOT_NULL))
3160
    {
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
3161
      // Room for "NULL"
3162
      length=4;
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3163
    }
3164
    drizzle_column_set_max_size(field, length);
840.3.1 by Toru Maesaka
Fixed the issue of seeing an oversized separator when rendering a multibyte string to console.
3165
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
3166
    for (x=0; x< (length+2); x++)
279.2.2 by Monty Taylor
Replaced DYNAMIC_STRING with C++ string in drizzle command line client.
3167
      separator.append("-");
3168
    separator.append("+");
1 by brian
clean slate
3169
  }
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
3170
279.2.2 by Monty Taylor
Replaced DYNAMIC_STRING with C++ string in drizzle command line client.
3171
  tee_puts((char*) separator.c_str(), PAGER);
1 by brian
clean slate
3172
  if (column_names)
3173
  {
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3174
    drizzle_column_seek(result,0);
1 by brian
clean slate
3175
    (void) tee_fputs("|", PAGER);
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3176
    for (uint32_t off=0; (field = drizzle_column_next(result)) ; off++)
1 by brian
clean slate
3177
    {
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3178
      uint32_t name_length= (uint32_t) strlen(drizzle_column_name(field));
1567.3.21 by Monty Taylor
Moved function from drizzle client to the utf8 header where it belongs.
3179
      uint32_t numcells= drizzled::utf8::char_length(drizzle_column_name(field));
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3180
      uint32_t display_length= drizzle_column_max_size(field) + name_length -
1441.3.1 by Vijay Samuel
all required updations have been made
3181
                               numcells;
1 by brian
clean slate
3182
      tee_fprintf(PAGER, " %-*s |",(int) min(display_length,
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
3183
                                             MAX_COLUMN_LENGTH),
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3184
                  drizzle_column_name(field));
3185
      num_flag[off]= ((drizzle_column_type(field) <= DRIZZLE_COLUMN_TYPE_LONGLONG) ||
3186
                      (drizzle_column_type(field) == DRIZZLE_COLUMN_TYPE_NEWDECIMAL));
1 by brian
clean slate
3187
    }
3188
    (void) tee_fputs("\n", PAGER);
279.2.2 by Monty Taylor
Replaced DYNAMIC_STRING with C++ string in drizzle command line client.
3189
    tee_puts((char*) separator.c_str(), PAGER);
1 by brian
clean slate
3190
  }
3191
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3192
  while (1)
1 by brian
clean slate
3193
  {
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3194
    if (quick)
3195
    {
3196
      cur= drizzle_row_buffer(result, &ret);
3197
      if (ret != DRIZZLE_RETURN_OK)
3198
      {
3199
        (void)put_error(&con, result);
3200
        break;
3201
      }
3202
    }
3203
    else
3204
      cur= drizzle_row_next(result);
3205
3206
    if (cur == NULL || interrupted_query)
1 by brian
clean slate
3207
      break;
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3208
3209
    size_t *lengths= drizzle_row_field_sizes(result);
1 by brian
clean slate
3210
    (void) tee_fputs("| ", PAGER);
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3211
    drizzle_column_seek(result, 0);
3212
    for (uint32_t off= 0; off < drizzle_result_column_count(result); off++)
1 by brian
clean slate
3213
    {
3214
      const char *buffer;
893 by Brian Aker
First pass of stripping uint
3215
      uint32_t data_length;
3216
      uint32_t field_max_length;
3217
      uint32_t visible_length;
3218
      uint32_t extra_padding;
1 by brian
clean slate
3219
3220
      if (cur[off] == NULL)
3221
      {
3222
        buffer= "NULL";
3223
        data_length= 4;
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
3224
      }
3225
      else
1 by brian
clean slate
3226
      {
928.1.4 by Eric Day
Fixed a few bugs, more progress on conversion.
3227
        buffer= cur[off];
895 by Brian Aker
Completion (?) of uint conversion.
3228
        data_length= (uint32_t) lengths[off];
1 by brian
clean slate
3229
      }
3230
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3231
      field= drizzle_column_next(result);
3232
      field_max_length= drizzle_column_max_size(field);
1 by brian
clean slate
3233
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
3234
      /*
3235
        How many text cells on the screen will this string span?  If it contains
3236
        multibyte characters, then the number of characters we occupy on screen
3237
        will be fewer than the number of bytes we occupy in memory.
1 by brian
clean slate
3238
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
3239
        We need to find how much screen real-estate we will occupy to know how
3240
        many extra padding-characters we should send with the printing function.
1 by brian
clean slate
3241
      */
1567.3.21 by Monty Taylor
Moved function from drizzle client to the utf8 header where it belongs.
3242
      visible_length= drizzled::utf8::char_length(buffer);
1 by brian
clean slate
3243
      extra_padding= data_length - visible_length;
3244
3245
      if (field_max_length > MAX_COLUMN_LENGTH)
1075 by Brian Aker
Fix for CentOS
3246
        tee_print_sized_data(buffer, data_length, MAX_COLUMN_LENGTH+extra_padding, false);
1 by brian
clean slate
3247
      else
3248
      {
3249
        if (num_flag[off] != 0) /* if it is numeric, we right-justify it */
1075 by Brian Aker
Fix for CentOS
3250
          tee_print_sized_data(buffer, data_length, field_max_length+extra_padding, true);
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
3251
        else
77.3.1 by Monty Taylor
First steps in removing sql_string and using glib instead.
3252
          tee_print_sized_data(buffer, data_length,
1075 by Brian Aker
Fix for CentOS
3253
                               field_max_length+extra_padding, false);
1 by brian
clean slate
3254
      }
3255
      tee_fputs(" | ", PAGER);
3256
    }
3257
    (void) tee_fputs("\n", PAGER);
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3258
    if (quick)
3259
      drizzle_row_free(result, cur);
1 by brian
clean slate
3260
  }
279.2.2 by Monty Taylor
Replaced DYNAMIC_STRING with C++ string in drizzle command line client.
3261
  tee_puts(separator.c_str(), PAGER);
1 by brian
clean slate
3262
}
3263
3264
/**
1441.3.1 by Vijay Samuel
all required updations have been made
3265
   Return the length of a field after it would be rendered into text.
3266
3267
   This doesn't know or care about multibyte characters.  Assume we're
3268
   using such a charset.  We can't know that all of the upcoming rows
3269
   for this column will have bytes that each render into some fraction
3270
   of a character.  It's at least possible that a row has bytes that
3271
   all render into one character each, and so the maximum length is
3272
   still the number of bytes.  (Assumption 1:  This can't be better
3273
   because we can never know the number of characters that the DB is
3274
   going to send -- only the number of bytes.  2: Chars <= Bytes.)
3275
3276
   @param  field  Pointer to a field to be inspected
3277
3278
   @returns  number of character positions to be used, at most
1 by brian
clean slate
3279
*/
928.1.1 by Eric Day
Started client changes.
3280
static int get_field_disp_length(drizzle_column_st *field)
1 by brian
clean slate
3281
{
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3282
  uint32_t length= column_names ? strlen(drizzle_column_name(field)) : 0;
1 by brian
clean slate
3283
3284
  if (quick)
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3285
    length= max(length, drizzle_column_size(field));
1 by brian
clean slate
3286
  else
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3287
    length= max(length, (uint32_t)drizzle_column_max_size(field));
1 by brian
clean slate
3288
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3289
  if (length < 4 &&
1441.3.1 by Vijay Samuel
all required updations have been made
3290
    !(drizzle_column_flags(field) & DRIZZLE_COLUMN_FLAGS_NOT_NULL))
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3291
  {
206.3.1 by Patrick Galbraith
Most everything working with client rename
3292
    length= 4;        /* Room for "NULL" */
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3293
  }
1 by brian
clean slate
3294
3295
  return length;
3296
}
3297
3298
/**
1441.3.1 by Vijay Samuel
all required updations have been made
3299
   For a new result, return the max number of characters that any
3300
   upcoming row may return.
3301
3302
   @param  result  Pointer to the result to judge
3303
3304
   @returns  The max number of characters in any row of this result
1 by brian
clean slate
3305
*/
928.1.1 by Eric Day
Started client changes.
3306
static int get_result_width(drizzle_result_st *result)
1 by brian
clean slate
3307
{
3308
  unsigned int len= 0;
928.1.1 by Eric Day
Started client changes.
3309
  drizzle_column_st *field;
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3310
  uint16_t offset;
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
3311
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3312
  offset= drizzle_column_current(result);
142.1.2 by Patrick
All DBUG_x removed from client/
3313
  assert(offset == 0);
1 by brian
clean slate
3314
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3315
  while ((field= drizzle_column_next(result)) != NULL)
1 by brian
clean slate
3316
    len+= get_field_disp_length(field) + 3; /* plus bar, space, & final space */
3317
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3318
  (void) drizzle_column_seek(result, offset);
1 by brian
clean slate
3319
3320
  return len + 1; /* plus final bar. */
3321
}
3322
3323
static void
3324
tee_print_sized_data(const char *data, unsigned int data_length, unsigned int total_bytes_to_send, bool right_justified)
3325
{
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
3326
  /*
1 by brian
clean slate
3327
    For '\0's print ASCII spaces instead, as '\0' is eaten by (at
3328
    least my) console driver, and that messes up the pretty table
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
3329
    grid.  (The \0 is also the reason we can't use fprintf() .)
1 by brian
clean slate
3330
  */
3331
  unsigned int i;
3332
  const char *p;
3333
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
3334
  if (right_justified)
1 by brian
clean slate
3335
    for (i= data_length; i < total_bytes_to_send; i++)
3336
      tee_putc((int)' ', PAGER);
3337
3338
  for (i= 0, p= data; i < data_length; i+= 1, p+= 1)
3339
  {
3340
    if (*p == '\0')
3341
      tee_putc((int)' ', PAGER);
3342
    else
3343
      tee_putc((int)*p, PAGER);
3344
  }
3345
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
3346
  if (! right_justified)
1 by brian
clean slate
3347
    for (i= data_length; i < total_bytes_to_send; i++)
3348
      tee_putc((int)' ', PAGER);
3349
}
3350
3351
3352
3353
static void
928.1.1 by Eric Day
Started client changes.
3354
print_table_data_vertically(drizzle_result_st *result)
1 by brian
clean slate
3355
{
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3356
  drizzle_row_t cur;
3357
  drizzle_return_t ret;
3358
  uint32_t max_length=0;
3359
  drizzle_column_st *field;
1 by brian
clean slate
3360
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3361
  while ((field = drizzle_column_next(result)))
1 by brian
clean slate
3362
  {
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3363
    uint32_t length= strlen(drizzle_column_name(field));
1 by brian
clean slate
3364
    if (length > max_length)
3365
      max_length= length;
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3366
    drizzle_column_set_max_size(field, length);
1 by brian
clean slate
3367
  }
3368
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3369
  for (uint32_t row_count=1;; row_count++)
1 by brian
clean slate
3370
  {
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3371
    if (quick)
3372
    {
3373
      cur= drizzle_row_buffer(result, &ret);
3374
      if (ret != DRIZZLE_RETURN_OK)
3375
      {
3376
        (void)put_error(&con, result);
3377
        break;
3378
      }
3379
    }
3380
    else
3381
      cur= drizzle_row_next(result);
3382
3383
    if (cur == NULL || interrupted_query)
1 by brian
clean slate
3384
      break;
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3385
    drizzle_column_seek(result,0);
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
3386
    tee_fprintf(PAGER,
3387
                "*************************** %d. row ***************************\n", row_count);
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3388
    for (uint32_t off=0; off < drizzle_result_column_count(result); off++)
1 by brian
clean slate
3389
    {
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3390
      field= drizzle_column_next(result);
3391
      tee_fprintf(PAGER, "%*s: ",(int) max_length,drizzle_column_name(field));
1 by brian
clean slate
3392
      tee_fprintf(PAGER, "%s\n",cur[off] ? (char*) cur[off] : "NULL");
3393
    }
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3394
    if (quick)
3395
      drizzle_row_free(result, cur);
1 by brian
clean slate
3396
  }
3397
}
3398
3399
3400
/* print_warnings should be called right after executing a statement */
3401
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3402
static void print_warnings(uint32_t error_code)
1 by brian
clean slate
3403
{
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3404
  const char *query;
3405
  drizzle_result_st result;
3406
  drizzle_row_t cur;
157 by Brian Aker
Second pass cleanup on removal of my_uint types
3407
  uint64_t num_rows;
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3408
  uint32_t new_code= 0;
1548.1.1 by Hartmut Holzgraefe
do not enable PAGER in print_warnings() when in batch mode (mysql bug #44732)
3409
  FILE *out;
1 by brian
clean slate
3410
3411
  /* Get the warnings */
3412
  query= "show warnings";
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3413
  drizzleclient_real_query_for_lazy(query, strlen(query),&result,&new_code);
840.1.21 by Monty Taylor
ZOMG. Renamed all the rest of the stuff in libdrizzleclient to be drizzleclient_*. I love commandline perl.
3414
  drizzleclient_store_result_for_lazy(&result);
1 by brian
clean slate
3415
3416
  /* Bail out when no warnings */
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3417
  if (!(num_rows= drizzle_result_row_count(&result)))
1 by brian
clean slate
3418
    goto end;
3419
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3420
  cur= drizzle_row_next(&result);
1 by brian
clean slate
3421
3422
  /*
3423
    Don't print a duplicate of the current error.  It is possible for SHOW
3424
    WARNINGS to return multiple errors with the same code, but different
3425
    messages.  To be safe, skip printing the duplicate only if it is the only
3426
    warning.
3427
  */
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3428
  if (!cur || (num_rows == 1 &&
1441.3.1 by Vijay Samuel
all required updations have been made
3429
      error_code == (uint32_t) strtoul(cur[1], NULL, 10)))
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3430
  {
1 by brian
clean slate
3431
    goto end;
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3432
  }
1 by brian
clean slate
3433
3434
  /* Print the warnings */
1548.1.1 by Hartmut Holzgraefe
do not enable PAGER in print_warnings() when in batch mode (mysql bug #44732)
3435
  if (status.getBatch()) 
3436
  {
1563 by Brian Aker
Merge Hartmut
3437
    out= stderr;
1548.1.1 by Hartmut Holzgraefe
do not enable PAGER in print_warnings() when in batch mode (mysql bug #44732)
3438
  } 
3439
  else 
3440
  {
3441
    init_pager();
1563 by Brian Aker
Merge Hartmut
3442
    out= PAGER;
1548.1.1 by Hartmut Holzgraefe
do not enable PAGER in print_warnings() when in batch mode (mysql bug #44732)
3443
  }
1 by brian
clean slate
3444
  do
3445
  {
1548.1.1 by Hartmut Holzgraefe
do not enable PAGER in print_warnings() when in batch mode (mysql bug #44732)
3446
    tee_fprintf(out, "%s (Code %s): %s\n", cur[0], cur[1], cur[2]);
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3447
  } while ((cur= drizzle_row_next(&result)));
1563 by Brian Aker
Merge Hartmut
3448
3449
  if (not status.getBatch())
1548.1.1 by Hartmut Holzgraefe
do not enable PAGER in print_warnings() when in batch mode (mysql bug #44732)
3450
    end_pager();
1 by brian
clean slate
3451
3452
end:
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3453
  drizzle_result_free(&result);
1 by brian
clean slate
3454
}
3455
3456
3457
static void
288 by Brian Aker
ulong cleanp in client apps
3458
safe_put_field(const char *pos,uint32_t length)
1 by brian
clean slate
3459
{
3460
  if (!pos)
3461
    tee_fputs("NULL", PAGER);
3462
  else
3463
  {
3464
    if (opt_raw_data)
3465
      tee_fputs(pos, PAGER);
3466
    else for (const char *end=pos+length ; pos != end ; pos++)
1085.3.3 by Monty Taylor
Got rid of #ifdef have utf8 stuff.
3467
    {
3468
      int l;
1567.3.20 by Monty Taylor
Removed UTF-8 lib that we don't use.
3469
      if ((l = drizzled::utf8::sequence_length(*pos)))
1085.3.3 by Monty Taylor
Got rid of #ifdef have utf8 stuff.
3470
      {
3471
        while (l--)
3472
          tee_putc(*pos++, PAGER);
3473
        pos--;
3474
        continue;
3475
      }
3476
      if (!*pos)
3477
        tee_fputs("\\0", PAGER); // This makes everything hard
3478
      else if (*pos == '\t')
3479
        tee_fputs("\\t", PAGER); // This would destroy tab format
3480
      else if (*pos == '\n')
3481
        tee_fputs("\\n", PAGER); // This too
3482
      else if (*pos == '\\')
3483
        tee_fputs("\\\\", PAGER);
3484
      else
3485
        tee_putc(*pos, PAGER);
3486
    }
1 by brian
clean slate
3487
  }
3488
}
3489
3490
3491
static void
928.1.1 by Eric Day
Started client changes.
3492
print_tab_data(drizzle_result_st *result)
1 by brian
clean slate
3493
{
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3494
  drizzle_row_t cur;
3495
  drizzle_return_t ret;
3496
  drizzle_column_st *field;
3497
  size_t *lengths;
1 by brian
clean slate
3498
3499
  if (opt_silent < 2 && column_names)
3500
  {
3501
    int first=0;
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3502
    while ((field = drizzle_column_next(result)))
1 by brian
clean slate
3503
    {
3504
      if (first++)
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
3505
        (void) tee_fputs("\t", PAGER);
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3506
      (void) tee_fputs(drizzle_column_name(field), PAGER);
1 by brian
clean slate
3507
    }
3508
    (void) tee_fputs("\n", PAGER);
3509
  }
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3510
  while (1)
1 by brian
clean slate
3511
  {
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3512
    if (quick)
3513
    {
3514
      cur= drizzle_row_buffer(result, &ret);
3515
      if (ret != DRIZZLE_RETURN_OK)
3516
      {
3517
        (void)put_error(&con, result);
3518
        break;
3519
      }
3520
    }
3521
    else
3522
      cur= drizzle_row_next(result);
3523
3524
    if (cur == NULL)
3525
      break;
3526
3527
    lengths= drizzle_row_field_sizes(result);
928.1.4 by Eric Day
Fixed a few bugs, more progress on conversion.
3528
    safe_put_field(cur[0],lengths[0]);
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3529
    for (uint32_t off=1 ; off < drizzle_result_column_count(result); off++)
1 by brian
clean slate
3530
    {
3531
      (void) tee_fputs("\t", PAGER);
928.1.4 by Eric Day
Fixed a few bugs, more progress on conversion.
3532
      safe_put_field(cur[off], lengths[off]);
1 by brian
clean slate
3533
    }
3534
    (void) tee_fputs("\n", PAGER);
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3535
    if (quick)
3536
      drizzle_row_free(result, cur);
1 by brian
clean slate
3537
  }
3538
}
3539
3540
static int
632.1.12 by Monty Taylor
Fixed more sun studio warnings.
3541
com_tee(string *, const char *line )
1 by brian
clean slate
3542
{
520.4.43 by mordred
A set of Solaris fixes.
3543
  char file_name[FN_REFLEN], *end;
3544
  const char *param;
1 by brian
clean slate
3545
1441.3.1 by Vijay Samuel
all required updations have been made
3546
  if (status.getBatch())
1 by brian
clean slate
3547
    return 0;
1567.3.14 by Monty Taylor
Removed the need for drizzled::internal usage in drizzle.cc.
3548
  while (isspace(*line))
1 by brian
clean slate
3549
    line++;
1567.3.17 by Monty Taylor
Removed more C-string strings. replaced with std::string.
3550
  if (!(param =strchr(line, ' '))) // if outfile wasn't given, use the default
1 by brian
clean slate
3551
  {
1567.3.17 by Monty Taylor
Removed more C-string strings. replaced with std::string.
3552
    if (outfile.empty())
1 by brian
clean slate
3553
    {
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
3554
      printf(_("No previous outfile available, you must give a filename!\n"));
1 by brian
clean slate
3555
      return 0;
3556
    }
3557
    else if (opt_outfile)
3558
    {
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
3559
      tee_fprintf(stdout, _("Currently logging to file '%s'\n"), outfile.c_str());
1 by brian
clean slate
3560
      return 0;
3561
    }
3562
    else
1567.3.17 by Monty Taylor
Removed more C-string strings. replaced with std::string.
3563
      param= outfile.c_str();      //resume using the old outfile
1 by brian
clean slate
3564
  }
3565
1567.3.17 by Monty Taylor
Removed more C-string strings. replaced with std::string.
3566
  /* @TODO: Replace this with string methods */
1 by brian
clean slate
3567
  /* eliminate the spaces before the parameters */
1567.3.14 by Monty Taylor
Removed the need for drizzled::internal usage in drizzle.cc.
3568
  while (isspace(*param))
1 by brian
clean slate
3569
    param++;
629.5.3 by Toru Maesaka
Third pass of replacing MySQL's strmake() with libc calls
3570
  strncpy(file_name, param, sizeof(file_name) - 1);
3571
  end= file_name + strlen(file_name);
1 by brian
clean slate
3572
  /* remove end space from command line */
1567.3.14 by Monty Taylor
Removed the need for drizzled::internal usage in drizzle.cc.
3573
  while (end > file_name && (isspace(end[-1]) ||
3574
                             iscntrl(end[-1])))
1 by brian
clean slate
3575
    end--;
3576
  end[0]= 0;
3577
  if (end == file_name)
3578
  {
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
3579
    printf(_("No outfile specified!\n"));
1 by brian
clean slate
3580
    return 0;
3581
  }
3582
  init_tee(file_name);
3583
  return 0;
3584
}
3585
3586
3587
static int
632.1.12 by Monty Taylor
Fixed more sun studio warnings.
3588
com_notee(string *, const char *)
1 by brian
clean slate
3589
{
3590
  if (opt_outfile)
3591
    end_tee();
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
3592
  tee_fprintf(stdout, _("Outfile disabled.\n"));
1 by brian
clean slate
3593
  return 0;
3594
}
3595
3596
/*
3597
  Sorry, this command is not available in Windows.
3598
*/
3599
3600
static int
632.1.12 by Monty Taylor
Fixed more sun studio warnings.
3601
com_pager(string *, const char *line)
1 by brian
clean slate
3602
{
520.4.43 by mordred
A set of Solaris fixes.
3603
  const char *param;
1 by brian
clean slate
3604
1441.3.1 by Vijay Samuel
all required updations have been made
3605
  if (status.getBatch())
1 by brian
clean slate
3606
    return 0;
3607
  /* Skip spaces in front of the pager command */
1567.3.14 by Monty Taylor
Removed the need for drizzled::internal usage in drizzle.cc.
3608
  while (isspace(*line))
1 by brian
clean slate
3609
    line++;
3610
  /* Skip the pager command */
3611
  param= strchr(line, ' ');
3612
  /* Skip the spaces between the command and the argument */
1567.3.14 by Monty Taylor
Removed the need for drizzled::internal usage in drizzle.cc.
3613
  while (param && isspace(*param))
1 by brian
clean slate
3614
    param++;
1883.3.1 by Andrew Hutchings
Cleanup drizzledump and drizzle for cppcheck
3615
  if (!param || (*param == '\0')) // if pager was not given, use the default
1 by brian
clean slate
3616
  {
3617
    if (!default_pager_set)
3618
    {
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
3619
      tee_fprintf(stdout, _("Default pager wasn't set, using stdout.\n"));
1 by brian
clean slate
3620
      opt_nopager=1;
1567.3.17 by Monty Taylor
Removed more C-string strings. replaced with std::string.
3621
      pager.assign("stdout");
1 by brian
clean slate
3622
      PAGER= stdout;
3623
      return 0;
3624
    }
1567.3.17 by Monty Taylor
Removed more C-string strings. replaced with std::string.
3625
    pager.assign(default_pager);
1 by brian
clean slate
3626
  }
3627
  else
3628
  {
1567.3.17 by Monty Taylor
Removed more C-string strings. replaced with std::string.
3629
    string pager_name(param);
3630
    string::iterator end= pager_name.end();
3631
    while (end > pager_name.begin() &&
3632
           (isspace(*(end-1)) || iscntrl(*(end-1))))
3633
      --end;
3634
    pager_name.erase(end, pager_name.end());
3635
    pager.assign(pager_name);
3636
    default_pager.assign(pager_name);
1 by brian
clean slate
3637
  }
3638
  opt_nopager=0;
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
3639
  tee_fprintf(stdout, _("PAGER set to '%s'\n"), pager.c_str());
1 by brian
clean slate
3640
  return 0;
3641
}
3642
3643
3644
static int
632.1.12 by Monty Taylor
Fixed more sun studio warnings.
3645
com_nopager(string *, const char *)
1 by brian
clean slate
3646
{
1567.3.17 by Monty Taylor
Removed more C-string strings. replaced with std::string.
3647
  pager.assign("stdout");
1 by brian
clean slate
3648
  opt_nopager=1;
3649
  PAGER= stdout;
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
3650
  tee_fprintf(stdout, _("PAGER set to stdout\n"));
1 by brian
clean slate
3651
  return 0;
3652
}
3653
3654
/* If arg is given, exit without errors. This happens on command 'quit' */
3655
3656
static int
632.1.12 by Monty Taylor
Fixed more sun studio warnings.
3657
com_quit(string *, const char *)
1 by brian
clean slate
3658
{
3659
  /* let the screen auto close on a normal shutdown */
1441.3.1 by Vijay Samuel
all required updations have been made
3660
  status.setExitStatus(0);
1 by brian
clean slate
3661
  return 1;
3662
}
3663
3664
static int
632.1.12 by Monty Taylor
Fixed more sun studio warnings.
3665
com_rehash(string *, const char *)
1 by brian
clean slate
3666
{
3667
  build_completion_hash(1, 0);
3668
  return 0;
3669
}
3670
3671
3672
3673
static int
632.1.12 by Monty Taylor
Fixed more sun studio warnings.
3674
com_print(string *buffer,const char *)
1 by brian
clean slate
3675
{
3676
  tee_puts("--------------", stdout);
279.2.2 by Monty Taylor
Replaced DYNAMIC_STRING with C++ string in drizzle command line client.
3677
  (void) tee_fputs(buffer->c_str(), stdout);
3678
  if ( (buffer->length() == 0)
3679
       || (buffer->c_str())[(buffer->length())-1] != '\n')
1 by brian
clean slate
3680
    tee_putc('\n', stdout);
3681
  tee_puts("--------------\n", stdout);
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
3682
  /* If empty buffer */
3683
  return 0;
1 by brian
clean slate
3684
}
3685
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
3686
/* ARGSUSED */
1 by brian
clean slate
3687
static int
279.2.2 by Monty Taylor
Replaced DYNAMIC_STRING with C++ string in drizzle command line client.
3688
com_connect(string *buffer, const char *line)
1 by brian
clean slate
3689
{
3690
  char *tmp, buff[256];
3691
  bool save_rehash= opt_rehash;
3692
  int error;
3693
212.6.1 by Mats Kindahl
Replacing all bzero() calls with memset() calls and removing the bzero.c file.
3694
  memset(buff, 0, sizeof(buff));
1 by brian
clean slate
3695
  if (buffer)
3696
  {
3697
    /*
3698
      Two null bytes are needed in the end of buff to allow
3699
      get_arg to find end of string the second time it's called.
3700
    */
629.5.3 by Toru Maesaka
Third pass of replacing MySQL's strmake() with libc calls
3701
    tmp= strncpy(buff, line, sizeof(buff)-2);
1 by brian
clean slate
3702
#ifdef EXTRA_DEBUG
3703
    tmp[1]= 0;
3704
#endif
3705
    tmp= get_arg(buff, 0);
3706
    if (tmp && *tmp)
3707
    {
1567.3.1 by Vijay Samuel
Refactoring of the drizzle client.
3708
      current_db.erase();
1593.1.4 by Vijay Samuel
Merge Fixes for client drizzle.
3709
      current_db.assign(tmp);
1 by brian
clean slate
3710
      tmp= get_arg(buff, 1);
3711
      if (tmp)
3712
      {
1567.3.1 by Vijay Samuel
Refactoring of the drizzle client.
3713
        current_host.erase();
182.1.2 by Jim Winstead
Various fixes to enable compilation on Mac OS X, and remove the glib dependency.
3714
        current_host=strdup(tmp);
1 by brian
clean slate
3715
      }
3716
    }
3717
    else
3718
    {
3719
      /* Quick re-connect */
971.6.11 by Eric Day
Removed purecov messages.
3720
      opt_rehash= 0;
1 by brian
clean slate
3721
    }
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
3722
    // command used
77.3.3 by Monty Taylor
Last change to migrate to glib from sql_string.
3723
    assert(buffer!=NULL);
279.2.2 by Monty Taylor
Replaced DYNAMIC_STRING with C++ string in drizzle command line client.
3724
    buffer->clear();
1 by brian
clean slate
3725
  }
3726
  else
3727
    opt_rehash= 0;
1567.5.3 by Vijay Samuel
Merge patch for sql_connect() and style issue.
3728
  error=sql_connect(current_host, current_db, current_user, opt_password,0);
1 by brian
clean slate
3729
  opt_rehash= save_rehash;
3730
3731
  if (connected)
3732
  {
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
3733
    sprintf(buff, _("Connection id:    %u"), drizzle_con_thread_id(&con));
77.3.7 by Monty Taylor
Made mysql into a pure-C program.
3734
    put_info(buff,INFO_INFO,0,0);
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
3735
    sprintf(buff, _("Current database: %.128s\n"),
3736
            !current_db.empty() ? current_db.c_str() : _("*** NONE ***"));
77.3.7 by Monty Taylor
Made mysql into a pure-C program.
3737
    put_info(buff,INFO_INFO,0,0);
1 by brian
clean slate
3738
  }
3739
  return error;
3740
}
3741
3742
632.1.12 by Monty Taylor
Fixed more sun studio warnings.
3743
static int com_source(string *, const char *line)
1 by brian
clean slate
3744
{
520.4.43 by mordred
A set of Solaris fixes.
3745
  char source_name[FN_REFLEN], *end;
3746
  const char *param;
1095.2.1 by Robert Klahn
Replace typedef struct LINE_BUFFER with class LineBuffer, encapsulating current logic
3747
  LineBuffer *line_buff;
1 by brian
clean slate
3748
  int error;
1441.3.1 by Vijay Samuel
all required updations have been made
3749
  Status old_status;
1 by brian
clean slate
3750
  FILE *sql_file;
3751
3752
  /* Skip space from file name */
1567.3.14 by Monty Taylor
Removed the need for drizzled::internal usage in drizzle.cc.
3753
  while (isspace(*line))
1 by brian
clean slate
3754
    line++;
206.3.1 by Patrick Galbraith
Most everything working with client rename
3755
  if (!(param = strchr(line, ' ')))    // Skip command name
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
3756
    return put_info(_("Usage: \\. <filename> | source <filename>"),
77.3.7 by Monty Taylor
Made mysql into a pure-C program.
3757
                    INFO_ERROR, 0,0);
1567.3.14 by Monty Taylor
Removed the need for drizzled::internal usage in drizzle.cc.
3758
  while (isspace(*param))
1 by brian
clean slate
3759
    param++;
629.5.3 by Toru Maesaka
Third pass of replacing MySQL's strmake() with libc calls
3760
  end= strncpy(source_name,param,sizeof(source_name)-1);
3761
  end+= strlen(source_name);
1567.3.14 by Monty Taylor
Removed the need for drizzled::internal usage in drizzle.cc.
3762
  while (end > source_name && (isspace(end[-1]) ||
3763
                               iscntrl(end[-1])))
1 by brian
clean slate
3764
    end--;
3765
  end[0]=0;
1567.3.14 by Monty Taylor
Removed the need for drizzled::internal usage in drizzle.cc.
3766
1 by brian
clean slate
3767
  /* open file name */
910.1.3 by Brian Aker
Remove my_fopen() and key_map.cc file (thanks to Jay's lcov)
3768
  if (!(sql_file = fopen(source_name, "r")))
1 by brian
clean slate
3769
  {
3770
    char buff[FN_REFLEN+60];
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
3771
    sprintf(buff, _("Failed to open file '%s', error: %d"), source_name,errno);
77.3.7 by Monty Taylor
Made mysql into a pure-C program.
3772
    return put_info(buff, INFO_ERROR, 0 ,0);
1 by brian
clean slate
3773
  }
3774
1095.2.6 by Robert Klahn
Changes from additional code review feedback
3775
  line_buff= new(std::nothrow) LineBuffer(opt_max_input_line,sql_file);
1095.2.4 by Robert Klahn
changes from code review feedback
3776
  if (line_buff == NULL)
1 by brian
clean slate
3777
  {
910.1.3 by Brian Aker
Remove my_fopen() and key_map.cc file (thanks to Jay's lcov)
3778
    fclose(sql_file);
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
3779
    return put_info(_("Can't initialize LineBuffer"), INFO_ERROR, 0, 0);
1 by brian
clean slate
3780
  }
3781
3782
  /* Save old status */
3783
  old_status=status;
212.6.10 by Mats Kindahl
Removing redundant use of casts in client/ for memcmp(), memcpy(), memset(), and memmove().
3784
  memset(&status, 0, sizeof(status));
1 by brian
clean slate
3785
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
3786
  // Run in batch mode
1441.3.1 by Vijay Samuel
all required updations have been made
3787
  status.setBatch(old_status.getBatch());
3788
  status.setLineBuff(line_buff);
3789
  status.setFileName(source_name);
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
3790
  // Empty command buffer
77.3.3 by Monty Taylor
Last change to migrate to glib from sql_string.
3791
  assert(glob_buffer!=NULL);
279.2.2 by Monty Taylor
Replaced DYNAMIC_STRING with C++ string in drizzle command line client.
3792
  glob_buffer->clear();
1 by brian
clean slate
3793
  error= read_and_execute(false);
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
3794
  // Continue as before
3795
  status=old_status;
910.1.3 by Brian Aker
Remove my_fopen() and key_map.cc file (thanks to Jay's lcov)
3796
  fclose(sql_file);
1441.3.1 by Vijay Samuel
all required updations have been made
3797
  delete status.getLineBuff();
3798
  line_buff=0;
3799
  status.setLineBuff(0);
1 by brian
clean slate
3800
  return error;
3801
}
3802
3803
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
3804
/* ARGSUSED */
1 by brian
clean slate
3805
static int
632.1.12 by Monty Taylor
Fixed more sun studio warnings.
3806
com_delimiter(string *, const char *line)
1 by brian
clean slate
3807
{
3808
  char buff[256], *tmp;
3809
629.5.1 by Toru Maesaka
First pass of replacing MySQL's strmake() with libc calls
3810
  strncpy(buff, line, sizeof(buff) - 1);
1 by brian
clean slate
3811
  tmp= get_arg(buff, 0);
3812
3813
  if (!tmp || !*tmp)
3814
  {
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
3815
    put_info(_("DELIMITER must be followed by a 'delimiter' character or string"),
77.3.7 by Monty Taylor
Made mysql into a pure-C program.
3816
             INFO_ERROR, 0, 0);
1 by brian
clean slate
3817
    return 0;
3818
  }
3819
  else
3820
  {
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
3821
    if (strstr(tmp, "\\"))
1 by brian
clean slate
3822
    {
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
3823
      put_info(_("DELIMITER cannot contain a backslash character"),
77.3.7 by Monty Taylor
Made mysql into a pure-C program.
3824
               INFO_ERROR, 0, 0);
1 by brian
clean slate
3825
      return 0;
3826
    }
3827
  }
629.5.1 by Toru Maesaka
First pass of replacing MySQL's strmake() with libc calls
3828
  strncpy(delimiter, tmp, sizeof(delimiter) - 1);
1 by brian
clean slate
3829
  delimiter_length= (int)strlen(delimiter);
3830
  delimiter_str= delimiter;
3831
  return 0;
3832
}
3833
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
3834
/* ARGSUSED */
1 by brian
clean slate
3835
static int
632.1.12 by Monty Taylor
Fixed more sun studio warnings.
3836
com_use(string *, const char *line)
1 by brian
clean slate
3837
{
3838
  char *tmp, buff[FN_REFLEN + 1];
3839
  int select_db;
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3840
  drizzle_result_st result;
3841
  drizzle_return_t ret;
1 by brian
clean slate
3842
212.6.1 by Mats Kindahl
Replacing all bzero() calls with memset() calls and removing the bzero.c file.
3843
  memset(buff, 0, sizeof(buff));
629.5.1 by Toru Maesaka
First pass of replacing MySQL's strmake() with libc calls
3844
  strncpy(buff, line, sizeof(buff) - 1);
1 by brian
clean slate
3845
  tmp= get_arg(buff, 0);
3846
  if (!tmp || !*tmp)
3847
  {
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
3848
    put_info(_("USE must be followed by a database name"), INFO_ERROR, 0, 0);
1 by brian
clean slate
3849
    return 0;
3850
  }
3851
  /*
3852
    We need to recheck the current database, because it may change
3853
    under our feet, for example if DROP DATABASE or RENAME DATABASE
3854
    (latter one not yet available by the time the comment was written)
3855
  */
3856
  get_current_db();
3857
1567.3.1 by Vijay Samuel
Refactoring of the drizzle client.
3858
  if (current_db.empty() || strcmp(current_db.c_str(),tmp))
1 by brian
clean slate
3859
  {
3860
    if (one_database)
3861
    {
3862
      skip_updates= 1;
840.1.21 by Monty Taylor
ZOMG. Renamed all the rest of the stuff in libdrizzleclient to be drizzleclient_*. I love commandline perl.
3863
      select_db= 0;    // don't do drizzleclient_select_db()
1 by brian
clean slate
3864
    }
3865
    else
840.1.21 by Monty Taylor
ZOMG. Renamed all the rest of the stuff in libdrizzleclient to be drizzleclient_*. I love commandline perl.
3866
      select_db= 2;    // do drizzleclient_select_db() and build_completion_hash()
1 by brian
clean slate
3867
  }
3868
  else
3869
  {
3870
    /*
3871
      USE to the current db specified.
840.1.21 by Monty Taylor
ZOMG. Renamed all the rest of the stuff in libdrizzleclient to be drizzleclient_*. I love commandline perl.
3872
      We do need to send drizzleclient_select_db() to make server
1 by brian
clean slate
3873
      update database level privileges, which might
3874
      change since last USE (see bug#10979).
3875
      For performance purposes, we'll skip rebuilding of completion hash.
3876
    */
3877
    skip_updates= 0;
840.1.21 by Monty Taylor
ZOMG. Renamed all the rest of the stuff in libdrizzleclient to be drizzleclient_*. I love commandline perl.
3878
    select_db= 1;      // do only drizzleclient_select_db(), without completion
1 by brian
clean slate
3879
  }
3880
3881
  if (select_db)
3882
  {
3883
    /*
3884
      reconnect once if connection is down or if connection was found to
3885
      be down during query
3886
    */
3887
    if (!connected && reconnect())
3888
      return opt_reconnect ? -1 : 1;                        // Fatal error
928.1.4 by Eric Day
Fixed a few bugs, more progress on conversion.
3889
    for (bool try_again= true; try_again; try_again= false)
1 by brian
clean slate
3890
    {
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3891
      if (drizzle_select_db(&con,&result,tmp,&ret) == NULL ||
3892
          ret != DRIZZLE_RETURN_OK)
3893
      {
3894
        if (ret == DRIZZLE_RETURN_ERROR_CODE)
3895
        {
3896
          int error= put_error(&con, &result);
3897
          drizzle_result_free(&result);
3898
          return error;
3899
        }
3900
3901
        if (ret != DRIZZLE_RETURN_SERVER_GONE || !try_again)
3902
          return put_error(&con, NULL);
3903
3904
        if (reconnect())
3905
          return opt_reconnect ? -1 : 1;                      // Fatal error
3906
      }
928.1.4 by Eric Day
Fixed a few bugs, more progress on conversion.
3907
      else
3908
        drizzle_result_free(&result);
1 by brian
clean slate
3909
    }
1567.3.1 by Vijay Samuel
Refactoring of the drizzle client.
3910
    current_db.erase();
1593.1.4 by Vijay Samuel
Merge Fixes for client drizzle.
3911
    current_db.assign(tmp);
1 by brian
clean slate
3912
    if (select_db > 1)
3913
      build_completion_hash(opt_rehash, 1);
3914
  }
3915
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
3916
  put_info(_("Database changed"),INFO_INFO, 0, 0);
1 by brian
clean slate
3917
  return 0;
3918
}
3919
1954 by Brian Aker
This adds the command "shutdown" to the command line client. It does require
3920
static int com_shutdown(string *, const char *)
3921
{
3922
  drizzle_result_st result;
3923
  drizzle_return_t ret;
3924
3925
  if (verbose)
3926
  {
3927
    printf(_("shutting down drizzled"));
3928
    if (opt_drizzle_port > 0)
3929
      printf(_(" on port %d"), opt_drizzle_port);
3930
    printf("... ");
3931
  }
3932
3933
  if (drizzle_shutdown(&con, &result, DRIZZLE_SHUTDOWN_DEFAULT,
3934
                       &ret) == NULL || ret != DRIZZLE_RETURN_OK)
3935
  {
3936
    if (ret == DRIZZLE_RETURN_ERROR_CODE)
3937
    {
3938
      fprintf(stderr, _("shutdown failed; error: '%s'"),
3939
              drizzle_result_error(&result));
3940
      drizzle_result_free(&result);
3941
    }
3942
    else
3943
    {
3944
      fprintf(stderr, _("shutdown failed; error: '%s'"),
3945
              drizzle_con_error(&con));
3946
    }
3947
    return false;
3948
  }
3949
3950
  drizzle_result_free(&result);
3951
3952
  if (verbose)
3953
    printf(_("done\n"));
3954
3955
  return false;
3956
}
3957
1 by brian
clean slate
3958
static int
632.1.12 by Monty Taylor
Fixed more sun studio warnings.
3959
com_warnings(string *, const char *)
1 by brian
clean slate
3960
{
3961
  show_warnings = 1;
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
3962
  put_info(_("Show warnings enabled."),INFO_INFO, 0, 0);
1 by brian
clean slate
3963
  return 0;
3964
}
3965
3966
static int
632.1.12 by Monty Taylor
Fixed more sun studio warnings.
3967
com_nowarnings(string *, const char *)
1 by brian
clean slate
3968
{
3969
  show_warnings = 0;
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
3970
  put_info(_("Show warnings disabled."),INFO_INFO, 0, 0);
1 by brian
clean slate
3971
  return 0;
3972
}
3973
3974
/*
3975
  Gets argument from a command on the command line. If get_next_arg is
3976
  not defined, skips the command and returns the first argument. The
3977
  line is modified by adding zero to the end of the argument. If
3978
  get_next_arg is defined, then the function searches for end of string
3979
  first, after found, returns the next argument and adds zero to the
3980
  end. If you ever wish to use this feature, remember to initialize all
3981
  items in the array to zero first.
3982
*/
3983
143 by Brian Aker
Bool cleanup.
3984
char *get_arg(char *line, bool get_next_arg)
1 by brian
clean slate
3985
{
3986
  char *ptr, *start;
143 by Brian Aker
Bool cleanup.
3987
  bool quoted= 0, valid_arg= 0;
1 by brian
clean slate
3988
  char qtype= 0;
3989
3990
  ptr= line;
3991
  if (get_next_arg)
3992
  {
3993
    for (; *ptr; ptr++) ;
3994
    if (*(ptr + 1))
3995
      ptr++;
3996
  }
3997
  else
3998
  {
3999
    /* skip leading white spaces */
1567.3.14 by Monty Taylor
Removed the need for drizzled::internal usage in drizzle.cc.
4000
    while (isspace(*ptr))
1 by brian
clean slate
4001
      ptr++;
4002
    if (*ptr == '\\') // short command was used
4003
      ptr+= 2;
4004
    else
1567.3.14 by Monty Taylor
Removed the need for drizzled::internal usage in drizzle.cc.
4005
      while (*ptr &&!isspace(*ptr)) // skip command
1 by brian
clean slate
4006
        ptr++;
4007
  }
4008
  if (!*ptr)
461 by Monty Taylor
Removed NullS. bu-bye.
4009
    return NULL;
1567.3.14 by Monty Taylor
Removed the need for drizzled::internal usage in drizzle.cc.
4010
  while (isspace(*ptr))
1 by brian
clean slate
4011
    ptr++;
4012
  if (*ptr == '\'' || *ptr == '\"' || *ptr == '`')
4013
  {
4014
    qtype= *ptr;
4015
    quoted= 1;
4016
    ptr++;
4017
  }
4018
  for (start=ptr ; *ptr; ptr++)
4019
  {
4020
    if (*ptr == '\\' && ptr[1]) // escaped character
4021
    {
4022
      // Remove the backslash
641.4.1 by Toru Maesaka
First pass of replacing MySQL's my_stpcpy() with appropriate libc calls
4023
      strcpy(ptr, ptr+1);
1 by brian
clean slate
4024
    }
4025
    else if ((!quoted && *ptr == ' ') || (quoted && *ptr == qtype))
4026
    {
4027
      *ptr= 0;
4028
      break;
4029
    }
4030
  }
4031
  valid_arg= ptr != start;
461 by Monty Taylor
Removed NullS. bu-bye.
4032
  return valid_arg ? start : NULL;
1 by brian
clean slate
4033
}
4034
4035
4036
static int
1567.5.3 by Vijay Samuel
Merge patch for sql_connect() and style issue.
4037
sql_connect(const string &host, const string &database, const string &user, const string &password,
1441.3.1 by Vijay Samuel
all required updations have been made
4038
                 uint32_t silent)
1 by brian
clean slate
4039
{
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
4040
  drizzle_return_t ret;
1 by brian
clean slate
4041
  if (connected)
4042
  {
4043
    connected= 0;
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
4044
    drizzle_con_free(&con);
4045
    drizzle_free(&drizzle);
4046
  }
4047
  drizzle_create(&drizzle);
1960.2.7 by Andrew Hutchings
Drop the drizzleadmin client and use ifdef instead
4048
4049
#ifdef DRIZZLE_ADMIN_TOOL
4050
  drizzle_con_options_t options= (drizzle_con_options_t) (DRIZZLE_CON_ADMIN | (use_drizzle_protocol ? DRIZZLE_CON_EXPERIMENTAL : DRIZZLE_CON_MYSQL));
4051
#else
4052
  drizzle_con_options_t options= (drizzle_con_options_t) (use_drizzle_protocol ? DRIZZLE_CON_EXPERIMENTAL : DRIZZLE_CON_MYSQL);
4053
#endif
4054
1745.2.1 by LinuxJedi
Remove the --mysql option and convert the --protocol option to do something similar.
4055
  if (drizzle_con_add_tcp(&drizzle, &con, (char *)host.c_str(),
4056
    opt_drizzle_port, (char *)user.c_str(),
4057
    (char *)password.c_str(), (char *)database.c_str(),
1960.2.7 by Andrew Hutchings
Drop the drizzleadmin client and use ifdef instead
4058
    options) == NULL)
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
4059
  {
4060
    (void) put_error(&con, NULL);
4061
    (void) fflush(stdout);
4062
    return 1;
4063
  }
4064
1441.3.1 by Vijay Samuel
all required updations have been made
4065
/* XXX add this back in
4066
  if (opt_connect_timeout)
4067
  {
893 by Brian Aker
First pass of stripping uint
4068
    uint32_t timeout=opt_connect_timeout;
840.1.21 by Monty Taylor
ZOMG. Renamed all the rest of the stuff in libdrizzleclient to be drizzleclient_*. I love commandline perl.
4069
    drizzleclient_options(&drizzle,DRIZZLE_OPT_CONNECT_TIMEOUT,
1441.3.1 by Vijay Samuel
all required updations have been made
4070
                  (char*) &timeout);
4071
  }
4072
*/
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
4073
1441.3.1 by Vijay Samuel
all required updations have been made
4074
/* XXX Do we need this?
4075
  if (safe_updates)
4076
  {
1 by brian
clean slate
4077
    char init_command[100];
4078
    sprintf(init_command,
1441.3.1 by Vijay Samuel
all required updations have been made
4079
            "SET SQL_SAFE_UPDATES=1,SQL_SELECT_LIMIT=%"PRIu32
4080
            ",MAX_JOIN_SIZE=%"PRIu32,
4081
            select_limit, max_join_size);
840.1.21 by Monty Taylor
ZOMG. Renamed all the rest of the stuff in libdrizzleclient to be drizzleclient_*. I love commandline perl.
4082
    drizzleclient_options(&drizzle, DRIZZLE_INIT_COMMAND, init_command);
1441.3.1 by Vijay Samuel
all required updations have been made
4083
  }
4084
*/
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
4085
  if ((ret= drizzle_con_connect(&con)) != DRIZZLE_RETURN_OK)
1 by brian
clean slate
4086
  {
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
4087
    if (!silent || (ret != DRIZZLE_RETURN_GETADDRINFO &&
4088
                    ret != DRIZZLE_RETURN_COULD_NOT_CONNECT))
1 by brian
clean slate
4089
    {
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
4090
      (void) put_error(&con, NULL);
1 by brian
clean slate
4091
      (void) fflush(stdout);
206.3.1 by Patrick Galbraith
Most everything working with client rename
4092
      return ignore_errors ? -1 : 1;    // Abort
1 by brian
clean slate
4093
    }
206.3.1 by Patrick Galbraith
Most everything working with client rename
4094
    return -1;          // Retryable
1 by brian
clean slate
4095
  }
4096
  connected=1;
1235.3.13 by Stewart Smith
remove MY_GIVE_INFO parameter to my_end() that really just gave us the output of time(1). Use UNIX, don't re-implement the kitchen sink. This also removes --debug-info from most command line utilities.
4097
1 by brian
clean slate
4098
  build_completion_hash(opt_rehash, 1);
4099
  return 0;
4100
}
4101
4102
4103
static int
632.1.12 by Monty Taylor
Fixed more sun studio warnings.
4104
com_status(string *, const char *)
1 by brian
clean slate
4105
{
1441.3.1 by Vijay Samuel
all required updations have been made
4106
/*
4107
  char buff[40];
4108
  uint64_t id;
4109
*/
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
4110
  drizzle_result_st result;
4111
  drizzle_return_t ret;
1 by brian
clean slate
4112
4113
  tee_puts("--------------", stdout);
1729.3.2 by LinuxJedi
Further cleanups based on Monty's comments (also revert a couple of things)
4114
  printf(_("Drizzle client %s build %s, for %s-%s (%s) using readline %s\n"),
4115
         drizzle_version(), VERSION,
1745.2.1 by LinuxJedi
Remove the --mysql option and convert the --protocol option to do something similar.
4116
         HOST_VENDOR, HOST_OS, HOST_CPU,
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
4117
         rl_library_version);
1567.3.1 by Vijay Samuel
Refactoring of the drizzle client.
4118
1 by brian
clean slate
4119
  if (connected)
4120
  {
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
4121
    tee_fprintf(stdout, _("\nConnection id:\t\t%lu\n"),drizzle_con_thread_id(&con));
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
4122
    /*
4123
      Don't remove "limit 1",
1 by brian
clean slate
4124
      it is protection againts SQL_SELECT_LIMIT=0
4125
    */
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
4126
    if (drizzle_query_str(&con,&result,"select DATABASE(), USER() limit 1",
4127
                          &ret) != NULL && ret == DRIZZLE_RETURN_OK &&
4128
        drizzle_result_buffer(&result) == DRIZZLE_RETURN_OK)
1 by brian
clean slate
4129
    {
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
4130
      drizzle_row_t cur=drizzle_row_next(&result);
1 by brian
clean slate
4131
      if (cur)
4132
      {
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
4133
        tee_fprintf(stdout, _("Current database:\t%s\n"), cur[0] ? cur[0] : "");
4134
        tee_fprintf(stdout, _("Current user:\t\t%s\n"), cur[1]);
1 by brian
clean slate
4135
      }
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
4136
      drizzle_result_free(&result);
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
4137
    }
928.1.4 by Eric Day
Fixed a few bugs, more progress on conversion.
4138
    else if (ret == DRIZZLE_RETURN_ERROR_CODE)
4139
      drizzle_result_free(&result);
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
4140
    tee_puts(_("SSL:\t\t\tNot in use"), stdout);
1 by brian
clean slate
4141
  }
4142
  else
4143
  {
4144
    vidattr(A_BOLD);
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
4145
    tee_fprintf(stdout, _("\nNo connection\n"));
1 by brian
clean slate
4146
    vidattr(A_NORMAL);
4147
    return 0;
4148
  }
4149
  if (skip_updates)
4150
  {
4151
    vidattr(A_BOLD);
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
4152
    tee_fprintf(stdout, _("\nAll updates ignored to this database\n"));
1 by brian
clean slate
4153
    vidattr(A_NORMAL);
4154
  }
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
4155
  tee_fprintf(stdout, _("Current pager:\t\t%s\n"), pager.c_str());
4156
  tee_fprintf(stdout, _("Using outfile:\t\t'%s'\n"), opt_outfile ? outfile.c_str() : "");
4157
  tee_fprintf(stdout, _("Using delimiter:\t%s\n"), delimiter);
4158
  tee_fprintf(stdout, _("Server version:\t\t%s\n"), server_version_string(&con));
1745.2.1 by LinuxJedi
Remove the --mysql option and convert the --protocol option to do something similar.
4159
  tee_fprintf(stdout, _("Protocol:\t\t%s\n"), opt_protocol.c_str());
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
4160
  tee_fprintf(stdout, _("Protocol version:\t%d\n"), drizzle_con_protocol_version(&con));
4161
  tee_fprintf(stdout, _("Connection:\t\t%s\n"), drizzle_con_host(&con));
1441.3.1 by Vijay Samuel
all required updations have been made
4162
/* XXX need to save this from result
4163
  if ((id= drizzleclient_insert_id(&drizzle)))
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
4164
    tee_fprintf(stdout, "Insert id:\t\t%s\n", internal::llstr(id, buff));
1441.3.1 by Vijay Samuel
all required updations have been made
4165
*/
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
4166
1377.2.2 by Zimin
bug fix: ./bin/drizzle with \s would segfault.
4167
  if (drizzle_con_uds(&con))
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
4168
    tee_fprintf(stdout, _("UNIX socket:\t\t%s\n"), drizzle_con_uds(&con));
1 by brian
clean slate
4169
  else
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
4170
    tee_fprintf(stdout, _("TCP port:\t\t%d\n"), drizzle_con_port(&con));
1 by brian
clean slate
4171
4172
  if (safe_updates)
4173
  {
4174
    vidattr(A_BOLD);
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
4175
    tee_fprintf(stdout, _("\nNote that you are running in safe_update_mode:\n"));
1 by brian
clean slate
4176
    vidattr(A_NORMAL);
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
4177
    tee_fprintf(stdout, _("\
1441.3.1 by Vijay Samuel
all required updations have been made
4178
UPDATEs and DELETEs that don't use a key in the WHERE clause are not allowed.\n\
4179
(One can force an UPDATE/DELETE by adding LIMIT # at the end of the command.)\n \
4180
SELECT has an automatic 'LIMIT %lu' if LIMIT is not used.\n             \
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
4181
Max number of examined row combination in a join is set to: %lu\n\n"),
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
4182
                select_limit, max_join_size);
1 by brian
clean slate
4183
  }
4184
  tee_puts("--------------\n", stdout);
4185
  return 0;
4186
}
4187
4188
static const char *
928.1.7 by Eric Day
Tools mostly converted, still fixing bugs from test suite.
4189
server_version_string(drizzle_con_st *local_con)
1 by brian
clean slate
4190
{
534 by Monty Taylor
Removed stxnmov. Also deleted strstr which had already been removed.
4191
  static string buf("");
4192
  static bool server_version_string_reserved= false;
1 by brian
clean slate
4193
534 by Monty Taylor
Removed stxnmov. Also deleted strstr which had already been removed.
4194
  if (!server_version_string_reserved)
4195
  {
4196
    buf.reserve(MAX_SERVER_VERSION_LENGTH);
4197
    server_version_string_reserved= true;
4198
  }
1 by brian
clean slate
4199
  /* Only one thread calls this, so no synchronization is needed */
4200
  if (buf[0] == '\0')
4201
  {
928.1.1 by Eric Day
Started client changes.
4202
    drizzle_result_st result;
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
4203
    drizzle_return_t ret;
1 by brian
clean slate
4204
928.1.7 by Eric Day
Tools mostly converted, still fixing bugs from test suite.
4205
    buf.append(drizzle_con_server_version(local_con));
1 by brian
clean slate
4206
4207
    /* "limit 1" is protection against SQL_SELECT_LIMIT=0 */
928.1.7 by Eric Day
Tools mostly converted, still fixing bugs from test suite.
4208
    (void)drizzle_query_str(local_con, &result,
4209
                            "select @@version_comment limit 1", &ret);
928.1.1 by Eric Day
Started client changes.
4210
    if (ret == DRIZZLE_RETURN_OK &&
4211
        drizzle_result_buffer(&result) == DRIZZLE_RETURN_OK)
1 by brian
clean slate
4212
    {
928.1.1 by Eric Day
Started client changes.
4213
      drizzle_row_t cur = drizzle_row_next(&result);
1 by brian
clean slate
4214
      if (cur && cur[0])
4215
      {
534 by Monty Taylor
Removed stxnmov. Also deleted strstr which had already been removed.
4216
        buf.append(" ");
928.1.4 by Eric Day
Fixed a few bugs, more progress on conversion.
4217
        buf.append(cur[0]);
1 by brian
clean slate
4218
      }
928.1.1 by Eric Day
Started client changes.
4219
      drizzle_result_free(&result);
1 by brian
clean slate
4220
    }
928.1.4 by Eric Day
Fixed a few bugs, more progress on conversion.
4221
    else if (ret == DRIZZLE_RETURN_ERROR_CODE)
4222
      drizzle_result_free(&result);
1 by brian
clean slate
4223
  }
4224
534 by Monty Taylor
Removed stxnmov. Also deleted strstr which had already been removed.
4225
  return buf.c_str();
1 by brian
clean slate
4226
}
4227
4228
static int
893 by Brian Aker
First pass of stripping uint
4229
put_info(const char *str,INFO_TYPE info_type, uint32_t error, const char *sqlstate)
1 by brian
clean slate
4230
{
4231
  FILE *file= (info_type == INFO_ERROR ? stderr : stdout);
4232
  static int inited=0;
4233
1441.3.1 by Vijay Samuel
all required updations have been made
4234
  if (status.getBatch())
1 by brian
clean slate
4235
  {
4236
    if (info_type == INFO_ERROR)
4237
    {
4238
      (void) fflush(file);
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
4239
      fprintf(file,_("ERROR"));
1 by brian
clean slate
4240
      if (error)
4241
      {
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
4242
        if (sqlstate)
4243
          (void) fprintf(file," %d (%s)",error, sqlstate);
1 by brian
clean slate
4244
        else
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
4245
          (void) fprintf(file," %d",error);
1 by brian
clean slate
4246
      }
1441.3.1 by Vijay Samuel
all required updations have been made
4247
      if (status.getQueryStartLine() && line_numbers)
1 by brian
clean slate
4248
      {
1441.3.1 by Vijay Samuel
all required updations have been made
4249
        (void) fprintf(file," at line %"PRIu32,status.getQueryStartLine());
4250
        if (status.getFileName())
4251
          (void) fprintf(file," in file: '%s'", status.getFileName());
1 by brian
clean slate
4252
      }
4253
      (void) fprintf(file,": %s\n",str);
4254
      (void) fflush(file);
4255
      if (!ignore_errors)
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
4256
        return 1;
1 by brian
clean slate
4257
    }
4258
    else if (info_type == INFO_RESULT && verbose > 1)
4259
      tee_puts(str, file);
4260
    if (unbuffered)
4261
      fflush(file);
4262
    return info_type == INFO_ERROR ? -1 : 0;
4263
  }
4264
  if (!opt_silent || info_type == INFO_ERROR)
4265
  {
4266
    if (!inited)
4267
    {
4268
      inited=1;
4269
#ifdef HAVE_SETUPTERM
4270
      (void) setupterm((char *)0, 1, (int *) 0);
4271
#endif
4272
    }
4273
    if (info_type == INFO_ERROR)
4274
    {
4275
      if (!opt_nobeep)
77.3.7 by Monty Taylor
Made mysql into a pure-C program.
4276
        /* This should make a bell */
4277
        putchar('\a');
1 by brian
clean slate
4278
      vidattr(A_STANDOUT);
4279
      if (error)
4280
      {
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
4281
        if (sqlstate)
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
4282
          (void) tee_fprintf(file, _("ERROR %d (%s): "), error, sqlstate);
1 by brian
clean slate
4283
        else
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
4284
          (void) tee_fprintf(file, _("ERROR %d: "), error);
1 by brian
clean slate
4285
      }
4286
      else
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
4287
        tee_puts(_("ERROR: "), file);
1 by brian
clean slate
4288
    }
4289
    else
4290
      vidattr(A_BOLD);
4291
    (void) tee_puts(str, file);
4292
    vidattr(A_NORMAL);
4293
  }
4294
  if (unbuffered)
4295
    fflush(file);
4296
  return info_type == INFO_ERROR ? -1 : 0;
4297
}
4298
4299
4300
static int
928.1.7 by Eric Day
Tools mostly converted, still fixing bugs from test suite.
4301
put_error(drizzle_con_st *local_con, drizzle_result_st *res)
1 by brian
clean slate
4302
{
928.1.4 by Eric Day
Fixed a few bugs, more progress on conversion.
4303
  const char *error;
4304
4305
  if (res != NULL)
4306
  {
4307
    error= drizzle_result_error(res);
4308
    if (!strcmp(error, ""))
928.1.7 by Eric Day
Tools mostly converted, still fixing bugs from test suite.
4309
      error= drizzle_con_error(local_con);
928.1.4 by Eric Day
Fixed a few bugs, more progress on conversion.
4310
  }
4311
  else
928.1.7 by Eric Day
Tools mostly converted, still fixing bugs from test suite.
4312
    error= drizzle_con_error(local_con);
928.1.4 by Eric Day
Fixed a few bugs, more progress on conversion.
4313
928.1.7 by Eric Day
Tools mostly converted, still fixing bugs from test suite.
4314
  return put_info(error, INFO_ERROR,
928.1.8 by Eric Day
All tests now passing now, fixed a few more client utility bugs.
4315
                  res == NULL ? drizzle_con_error_code(local_con) :
1441.3.1 by Vijay Samuel
all required updations have been made
4316
                                drizzle_result_error_code(res),
928.1.8 by Eric Day
All tests now passing now, fixed a few more client utility bugs.
4317
                  res == NULL ? drizzle_con_sqlstate(local_con) :
1441.3.1 by Vijay Samuel
all required updations have been made
4318
                                drizzle_result_sqlstate(res));
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
4319
}
1 by brian
clean slate
4320
4321
279.2.2 by Monty Taylor
Replaced DYNAMIC_STRING with C++ string in drizzle command line client.
4322
static void remove_cntrl(string *buffer)
1 by brian
clean slate
4323
{
279.2.2 by Monty Taylor
Replaced DYNAMIC_STRING with C++ string in drizzle command line client.
4324
  const char *start=  buffer->c_str();
4325
  const char *end= start + (buffer->length());
1567.3.14 by Monty Taylor
Removed the need for drizzled::internal usage in drizzle.cc.
4326
  while (start < end && !isgraph(end[-1]))
1 by brian
clean slate
4327
    end--;
893 by Brian Aker
First pass of stripping uint
4328
  uint32_t pos_to_truncate= (end-start);
287.3.17 by Monty Taylor
Fixed null-query problem.
4329
  if (buffer->length() > pos_to_truncate)
4330
    buffer->erase(pos_to_truncate);
1 by brian
clean slate
4331
}
4332
4333
4334
void tee_fprintf(FILE *file, const char *fmt, ...)
4335
{
4336
  va_list args;
4337
4338
  va_start(args, fmt);
4339
  (void) vfprintf(file, fmt, args);
4340
  va_end(args);
4341
4342
  if (opt_outfile)
4343
  {
4344
    va_start(args, fmt);
4345
    (void) vfprintf(OUTFILE, fmt, args);
4346
    va_end(args);
4347
  }
4348
}
4349
4350
4351
void tee_fputs(const char *s, FILE *file)
4352
{
4353
  fputs(s, file);
4354
  if (opt_outfile)
4355
    fputs(s, OUTFILE);
4356
}
4357
4358
4359
void tee_puts(const char *s, FILE *file)
4360
{
4361
  fputs(s, file);
4362
  fputc('\n', file);
4363
  if (opt_outfile)
4364
  {
4365
    fputs(s, OUTFILE);
4366
    fputc('\n', OUTFILE);
4367
  }
4368
}
4369
4370
void tee_putc(int c, FILE *file)
4371
{
4372
  putc(c, file);
4373
  if (opt_outfile)
4374
    putc(c, OUTFILE);
4375
}
4376
4377
#include <sys/times.h>
206.3.1 by Patrick Galbraith
Most everything working with client rename
4378
#ifdef _SC_CLK_TCK        // For mit-pthreads
1 by brian
clean slate
4379
#undef CLOCKS_PER_SEC
4380
#define CLOCKS_PER_SEC (sysconf(_SC_CLK_TCK))
4381
#endif
4382
288 by Brian Aker
ulong cleanp in client apps
4383
static uint32_t start_timer(void)
1 by brian
clean slate
4384
{
4385
  struct tms tms_tmp;
4386
  return times(&tms_tmp);
4387
}
4388
4389
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
4390
/**
1441.3.1 by Vijay Samuel
all required updations have been made
4391
   Write as many as 52+1 bytes to buff, in the form of a legible
4392
   duration of time.
1 by brian
clean slate
4393
1441.3.1 by Vijay Samuel
all required updations have been made
4394
   len("4294967296 days, 23 hours, 59 minutes, 60.00 seconds")  ->  52
1 by brian
clean slate
4395
*/
4396
static void nice_time(double sec,char *buff,bool part_second)
4397
{
288 by Brian Aker
ulong cleanp in client apps
4398
  uint32_t tmp;
971.1.77 by Monty Taylor
Removed mysys/mystrings things from client/ progs. Still need to get rid of my_getopt, but I think that's all now.
4399
  ostringstream tmp_buff_str;
4400
1 by brian
clean slate
4401
  if (sec >= 3600.0*24)
4402
  {
288 by Brian Aker
ulong cleanp in client apps
4403
    tmp=(uint32_t) floor(sec/(3600.0*24));
641.4.1 by Toru Maesaka
First pass of replacing MySQL's my_stpcpy() with appropriate libc calls
4404
    sec-= 3600.0*24*tmp;
971.1.77 by Monty Taylor
Removed mysys/mystrings things from client/ progs. Still need to get rid of my_getopt, but I think that's all now.
4405
    tmp_buff_str << tmp;
641.4.1 by Toru Maesaka
First pass of replacing MySQL's my_stpcpy() with appropriate libc calls
4406
4407
    if (tmp > 1)
971.1.77 by Monty Taylor
Removed mysys/mystrings things from client/ progs. Still need to get rid of my_getopt, but I think that's all now.
4408
      tmp_buff_str << " days ";
641.4.1 by Toru Maesaka
First pass of replacing MySQL's my_stpcpy() with appropriate libc calls
4409
    else
971.1.77 by Monty Taylor
Removed mysys/mystrings things from client/ progs. Still need to get rid of my_getopt, but I think that's all now.
4410
      tmp_buff_str << " day ";
641.4.1 by Toru Maesaka
First pass of replacing MySQL's my_stpcpy() with appropriate libc calls
4411
1 by brian
clean slate
4412
  }
4413
  if (sec >= 3600.0)
4414
  {
288 by Brian Aker
ulong cleanp in client apps
4415
    tmp=(uint32_t) floor(sec/3600.0);
1 by brian
clean slate
4416
    sec-=3600.0*tmp;
971.1.77 by Monty Taylor
Removed mysys/mystrings things from client/ progs. Still need to get rid of my_getopt, but I think that's all now.
4417
    tmp_buff_str << tmp;
641.4.1 by Toru Maesaka
First pass of replacing MySQL's my_stpcpy() with appropriate libc calls
4418
4419
    if (tmp > 1)
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
4420
      tmp_buff_str << _(" hours ");
641.4.1 by Toru Maesaka
First pass of replacing MySQL's my_stpcpy() with appropriate libc calls
4421
    else
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
4422
      tmp_buff_str << _(" hour ");
1 by brian
clean slate
4423
  }
4424
  if (sec >= 60.0)
4425
  {
288 by Brian Aker
ulong cleanp in client apps
4426
    tmp=(uint32_t) floor(sec/60.0);
1 by brian
clean slate
4427
    sec-=60.0*tmp;
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
4428
    tmp_buff_str << tmp << _(" min ");
1 by brian
clean slate
4429
  }
4430
  if (part_second)
971.1.77 by Monty Taylor
Removed mysys/mystrings things from client/ progs. Still need to get rid of my_getopt, but I think that's all now.
4431
    tmp_buff_str.precision(2);
1 by brian
clean slate
4432
  else
971.1.77 by Monty Taylor
Removed mysys/mystrings things from client/ progs. Still need to get rid of my_getopt, but I think that's all now.
4433
    tmp_buff_str.precision(0);
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
4434
  tmp_buff_str << sec << _(" sec");
971.1.77 by Monty Taylor
Removed mysys/mystrings things from client/ progs. Still need to get rid of my_getopt, but I think that's all now.
4435
  strcpy(buff, tmp_buff_str.str().c_str());
1 by brian
clean slate
4436
}
4437
4438
288 by Brian Aker
ulong cleanp in client apps
4439
static void end_timer(uint32_t start_time,char *buff)
1 by brian
clean slate
4440
{
4441
  nice_time((double) (start_timer() - start_time) /
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
4442
            CLOCKS_PER_SEC,buff,1);
1 by brian
clean slate
4443
}
4444
4445
288 by Brian Aker
ulong cleanp in client apps
4446
static void drizzle_end_timer(uint32_t start_time,char *buff)
1 by brian
clean slate
4447
{
4448
  buff[0]=' ';
4449
  buff[1]='(';
4450
  end_timer(start_time,buff+2);
641.4.1 by Toru Maesaka
First pass of replacing MySQL's my_stpcpy() with appropriate libc calls
4451
  strcpy(strchr(buff, '\0'),")");
1 by brian
clean slate
4452
}
4453
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
4454
static const char * construct_prompt()
1 by brian
clean slate
4455
{
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
4456
  // Erase the old prompt
77.3.3 by Monty Taylor
Last change to migrate to glib from sql_string.
4457
  assert(processed_prompt!=NULL);
279.2.2 by Monty Taylor
Replaced DYNAMIC_STRING with C++ string in drizzle command line client.
4458
  processed_prompt->clear();
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
4459
4460
  // Get the date struct
4461
  time_t  lclock = time(NULL);
1 by brian
clean slate
4462
  struct tm *t = localtime(&lclock);
4463
4464
  /* parse thru the settings for the prompt */
1596.1.1 by Monty Taylor
Merged in remove-internal-from-drizzle-cc
4465
  string::iterator c= current_prompt.begin();
4466
  while (c != current_prompt.end())
1 by brian
clean slate
4467
  {
4468
    if (*c != PROMPT_CHAR)
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
4469
    {
1596.1.1 by Monty Taylor
Merged in remove-internal-from-drizzle-cc
4470
      processed_prompt->push_back(*c);
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
4471
    }
1 by brian
clean slate
4472
    else
4473
    {
77.3.7 by Monty Taylor
Made mysql into a pure-C program.
4474
      int getHour;
4475
      int getYear;
971.4.1 by Monty Taylor
GCC on Solaris build fixes.
4476
      /* Room for Dow MMM DD HH:MM:SS YYYY */ 
4477
      char dateTime[32];
1 by brian
clean slate
4478
      switch (*++c) {
4479
      case '\0':
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
4480
        // stop it from going beyond if ends with %
1596.1.1 by Monty Taylor
Merged in remove-internal-from-drizzle-cc
4481
        --c;
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
4482
        break;
1 by brian
clean slate
4483
      case 'c':
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
4484
        add_int_to_prompt(++prompt_counter);
4485
        break;
1 by brian
clean slate
4486
      case 'v':
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
4487
        if (connected)
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
4488
          processed_prompt->append(drizzle_con_server_version(&con));
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
4489
        else
279.2.2 by Monty Taylor
Replaced DYNAMIC_STRING with C++ string in drizzle command line client.
4490
          processed_prompt->append("not_connected");
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
4491
        break;
1 by brian
clean slate
4492
      case 'd':
1596.1.1 by Monty Taylor
Merged in remove-internal-from-drizzle-cc
4493
        processed_prompt->append(not current_db.empty() ? current_db : "(none)");
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
4494
        break;
1 by brian
clean slate
4495
      case 'h':
1441.3.1 by Vijay Samuel
all required updations have been made
4496
      {
1596.1.1 by Monty Taylor
Merged in remove-internal-from-drizzle-cc
4497
        const char *prompt= connected ? drizzle_con_host(&con) : "not_connected";
1441.3.1 by Vijay Samuel
all required updations have been made
4498
        if (strstr(prompt, "Localhost"))
4499
          processed_prompt->append("localhost");
4500
        else
4501
        {
4502
          const char *end=strrchr(prompt,' ');
4503
          if (end != NULL)
4504
            processed_prompt->append(prompt, (end-prompt));
4505
        }
4506
        break;
4507
      }
4508
      case 'p':
4509
      {
4510
        if (!connected)
4511
        {
4512
          processed_prompt->append("not_connected");
1407 by Brian Aker
Merge Siddharth, ran formatting across it.
4513
          break;
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
4514
        }
1441.3.1 by Vijay Samuel
all required updations have been made
4515
4516
        if (drizzle_con_uds(&con))
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
4517
        {
1441.3.1 by Vijay Samuel
all required updations have been made
4518
          const char *pos=strrchr(drizzle_con_uds(&con),'/');
4519
          processed_prompt->append(pos ? pos+1 : drizzle_con_uds(&con));
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
4520
        }
1441.3.1 by Vijay Samuel
all required updations have been made
4521
        else
4522
          add_int_to_prompt(drizzle_con_port(&con));
4523
      }
4524
      break;
1 by brian
clean slate
4525
      case 'U':
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
4526
        if (!full_username)
4527
          init_username();
279.2.2 by Monty Taylor
Replaced DYNAMIC_STRING with C++ string in drizzle command line client.
4528
        processed_prompt->append(full_username ? full_username :
1596.1.1 by Monty Taylor
Merged in remove-internal-from-drizzle-cc
4529
                                 (!current_user.empty() ?  current_user : "(unknown)"));
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
4530
        break;
1 by brian
clean slate
4531
      case 'u':
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
4532
        if (!full_username)
4533
          init_username();
279.2.2 by Monty Taylor
Replaced DYNAMIC_STRING with C++ string in drizzle command line client.
4534
        processed_prompt->append(part_username ? part_username :
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
4535
                                 (!current_user.empty() ?  current_user : _("(unknown)")));
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
4536
        break;
1 by brian
clean slate
4537
      case PROMPT_CHAR:
182.1.2 by Jim Winstead
Various fixes to enable compilation on Mac OS X, and remove the glib dependency.
4538
        {
632.1.12 by Monty Taylor
Fixed more sun studio warnings.
4539
          processed_prompt->append(PROMPT_CHAR, 1);
182.1.2 by Jim Winstead
Various fixes to enable compilation on Mac OS X, and remove the glib dependency.
4540
        }
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
4541
        break;
1 by brian
clean slate
4542
      case 'n':
182.1.2 by Jim Winstead
Various fixes to enable compilation on Mac OS X, and remove the glib dependency.
4543
        {
632.1.12 by Monty Taylor
Fixed more sun studio warnings.
4544
          processed_prompt->append('\n', 1);
182.1.2 by Jim Winstead
Various fixes to enable compilation on Mac OS X, and remove the glib dependency.
4545
        }
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
4546
        break;
1 by brian
clean slate
4547
      case ' ':
4548
      case '_':
182.1.2 by Jim Winstead
Various fixes to enable compilation on Mac OS X, and remove the glib dependency.
4549
        {
632.1.12 by Monty Taylor
Fixed more sun studio warnings.
4550
          processed_prompt->append(' ', 1);
182.1.2 by Jim Winstead
Various fixes to enable compilation on Mac OS X, and remove the glib dependency.
4551
        }
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
4552
        break;
1 by brian
clean slate
4553
      case 'R':
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
4554
        if (t->tm_hour < 10)
182.1.2 by Jim Winstead
Various fixes to enable compilation on Mac OS X, and remove the glib dependency.
4555
          add_int_to_prompt(0);
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
4556
        add_int_to_prompt(t->tm_hour);
4557
        break;
1 by brian
clean slate
4558
      case 'r':
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
4559
        getHour = t->tm_hour % 12;
4560
        if (getHour == 0)
4561
          getHour=12;
4562
        if (getHour < 10)
182.1.2 by Jim Winstead
Various fixes to enable compilation on Mac OS X, and remove the glib dependency.
4563
          add_int_to_prompt(0);
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
4564
        add_int_to_prompt(getHour);
4565
        break;
1 by brian
clean slate
4566
      case 'm':
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
4567
        if (t->tm_min < 10)
182.1.2 by Jim Winstead
Various fixes to enable compilation on Mac OS X, and remove the glib dependency.
4568
          add_int_to_prompt(0);
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
4569
        add_int_to_prompt(t->tm_min);
4570
        break;
1 by brian
clean slate
4571
      case 'y':
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
4572
        getYear = t->tm_year % 100;
4573
        if (getYear < 10)
182.1.2 by Jim Winstead
Various fixes to enable compilation on Mac OS X, and remove the glib dependency.
4574
          add_int_to_prompt(0);
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
4575
        add_int_to_prompt(getYear);
4576
        break;
1 by brian
clean slate
4577
      case 'Y':
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
4578
        add_int_to_prompt(t->tm_year+1900);
4579
        break;
1 by brian
clean slate
4580
      case 'D':
971.4.1 by Monty Taylor
GCC on Solaris build fixes.
4581
        strftime(dateTime, 32, "%a %b %d %H:%M:%S %Y", localtime(&lclock));
4582
        processed_prompt->append(dateTime);
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
4583
        break;
1 by brian
clean slate
4584
      case 's':
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
4585
        if (t->tm_sec < 10)
182.1.2 by Jim Winstead
Various fixes to enable compilation on Mac OS X, and remove the glib dependency.
4586
          add_int_to_prompt(0);
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
4587
        add_int_to_prompt(t->tm_sec);
4588
        break;
1 by brian
clean slate
4589
      case 'w':
279.2.2 by Monty Taylor
Replaced DYNAMIC_STRING with C++ string in drizzle command line client.
4590
        processed_prompt->append(day_names[t->tm_wday]);
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
4591
        break;
1 by brian
clean slate
4592
      case 'P':
279.2.2 by Monty Taylor
Replaced DYNAMIC_STRING with C++ string in drizzle command line client.
4593
        processed_prompt->append(t->tm_hour < 12 ? "am" : "pm");
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
4594
        break;
1 by brian
clean slate
4595
      case 'o':
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
4596
        add_int_to_prompt(t->tm_mon+1);
4597
        break;
1 by brian
clean slate
4598
      case 'O':
279.2.2 by Monty Taylor
Replaced DYNAMIC_STRING with C++ string in drizzle command line client.
4599
        processed_prompt->append(month_names[t->tm_mon]);
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
4600
        break;
1 by brian
clean slate
4601
      case '\'':
279.2.2 by Monty Taylor
Replaced DYNAMIC_STRING with C++ string in drizzle command line client.
4602
        processed_prompt->append("'");
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
4603
        break;
1 by brian
clean slate
4604
      case '"':
279.2.2 by Monty Taylor
Replaced DYNAMIC_STRING with C++ string in drizzle command line client.
4605
        processed_prompt->append("\"");
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
4606
        break;
1 by brian
clean slate
4607
      case 'S':
279.2.2 by Monty Taylor
Replaced DYNAMIC_STRING with C++ string in drizzle command line client.
4608
        processed_prompt->append(";");
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
4609
        break;
1 by brian
clean slate
4610
      case 't':
279.2.2 by Monty Taylor
Replaced DYNAMIC_STRING with C++ string in drizzle command line client.
4611
        processed_prompt->append("\t");
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
4612
        break;
1 by brian
clean slate
4613
      case 'l':
1596.1.1 by Monty Taylor
Merged in remove-internal-from-drizzle-cc
4614
        processed_prompt->append(delimiter_str);
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
4615
        break;
1 by brian
clean slate
4616
      default:
1596.1.1 by Monty Taylor
Merged in remove-internal-from-drizzle-cc
4617
        processed_prompt->push_back(*c);
1 by brian
clean slate
4618
      }
4619
    }
1596.1.1 by Monty Taylor
Merged in remove-internal-from-drizzle-cc
4620
    ++c;
1 by brian
clean slate
4621
  }
279.2.2 by Monty Taylor
Replaced DYNAMIC_STRING with C++ string in drizzle command line client.
4622
  return processed_prompt->c_str();
1 by brian
clean slate
4623
}
4624
4625
4626
static void add_int_to_prompt(int toadd)
4627
{
971.1.77 by Monty Taylor
Removed mysys/mystrings things from client/ progs. Still need to get rid of my_getopt, but I think that's all now.
4628
  ostringstream buffer;
4629
  buffer << toadd;
4630
  processed_prompt->append(buffer.str().c_str());
1 by brian
clean slate
4631
}
4632
4633
static void init_username()
4634
{
1441.3.1 by Vijay Samuel
all required updations have been made
4635
/* XXX need this?
4636
  free(full_username);
4637
  free(part_username);
1 by brian
clean slate
4638
1441.3.1 by Vijay Samuel
all required updations have been made
4639
  drizzle_result_st *result;
4640
  if (!drizzleclient_query(&drizzle,"select USER()") &&
4641
      (result=drizzleclient_use_result(&drizzle)))
4642
  {
928.1.1 by Eric Day
Started client changes.
4643
    drizzle_row_t cur=drizzleclient_fetch_row(result);
182.1.2 by Jim Winstead
Various fixes to enable compilation on Mac OS X, and remove the glib dependency.
4644
    full_username= strdup(cur[0]);
4645
    part_username= strdup(strtok(cur[0],"@"));
840.1.21 by Monty Taylor
ZOMG. Renamed all the rest of the stuff in libdrizzleclient to be drizzleclient_*. I love commandline perl.
4646
    (void) drizzleclient_fetch_row(result);        // Read eof
1441.3.1 by Vijay Samuel
all required updations have been made
4647
  }
4648
*/
1 by brian
clean slate
4649
}
4650
632.1.12 by Monty Taylor
Fixed more sun studio warnings.
4651
static int com_prompt(string *, const char *line)
1 by brian
clean slate
4652
{
520.4.43 by mordred
A set of Solaris fixes.
4653
  const char *ptr=strchr(line, ' ');
656.1.51 by Monty Taylor
Fixed strdup return checking in client/
4654
  if (ptr == NULL)
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
4655
    tee_fprintf(stdout, _("Returning to default PROMPT of %s\n"),
182.1.2 by Jim Winstead
Various fixes to enable compilation on Mac OS X, and remove the glib dependency.
4656
                default_prompt);
656.1.51 by Monty Taylor
Fixed strdup return checking in client/
4657
  prompt_counter = 0;
4658
  char * tmpptr= strdup(ptr ? ptr+1 : default_prompt);
4659
  if (tmpptr == NULL)
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
4660
    tee_fprintf(stdout, _("Memory allocation error. Not changing prompt\n"));
1 by brian
clean slate
4661
  else
656.1.51 by Monty Taylor
Fixed strdup return checking in client/
4662
  {
1567.3.1 by Vijay Samuel
Refactoring of the drizzle client.
4663
    current_prompt.erase();
656.1.51 by Monty Taylor
Fixed strdup return checking in client/
4664
    current_prompt= tmpptr;
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
4665
    tee_fprintf(stdout, _("PROMPT set to '%s'\n"), current_prompt.c_str());
656.1.51 by Monty Taylor
Fixed strdup return checking in client/
4666
  }
1 by brian
clean slate
4667
  return 0;
4668
}
266.6.1 by Andy Lester
merging from main
4669
4670
/*
1441.3.1 by Vijay Samuel
all required updations have been made
4671
    strcont(str, set) if str contanies any character in the string set.
4672
    The result is the position of the first found character in str, or NULL
4673
    if there isn't anything found.
266.6.1 by Andy Lester
merging from main
4674
*/
4675
4676
static const char * strcont(register const char *str, register const char *set)
4677
{
4678
  register const char * start = (const char *) set;
4679
4680
  while (*str)
4681
  {
4682
    while (*set)
4683
    {
4684
      if (*set++ == *str)
4685
        return ((const char*) str);
4686
    }
4687
    set=start; str++;
4688
  }
4689
  return NULL;
4690
} /* strcont */