~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,
2005.2.2 by Andrew Hutchings
Change most places to use 'schema' instead of 'database'
494
    N_("Use another schema. Takes schema 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"));
2005.2.2 by Andrew Hutchings
Change most places to use 'schema' instead of 'database'
1686
    printf(_("Usage: drizzle [OPTIONS] [schema]\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;
923.2.6 by Monty Taylor
Removing hand-done completion hash functions.
2540
  string tmp_str, tmp_str_lower;
2005.2.1 by Andrew Hutchings
Move to using I_S instead of SHOW for hash lookup table. Also kill bug in fields list in the processes
2541
  std::string query;
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 */
2005.2.1 by Andrew Hutchings
Move to using I_S instead of SHOW for hash lookup table. Also kill bug in fields list in the processes
2561
  if (drizzle_query_str(&con, &databases, "select schema_name from information_schema.schemata", &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
2005.2.1 by Andrew Hutchings
Move to using I_S instead of SHOW for hash lookup table. Also kill bug in fields list in the processes
2581
  query= "select table_name, column_name from information_schema.columns where table_schema='";
2582
  query.append(current_db);
2583
  query.append("' order by table_name");
2584
  
2585
  if (drizzle_query(&con, &fields, query.c_str(), query.length(),
2586
                    &ret) != NULL)
1 by brian
clean slate
2587
  {
2005.2.1 by Andrew Hutchings
Move to using I_S instead of SHOW for hash lookup table. Also kill bug in fields list in the processes
2588
    if (ret == DRIZZLE_RETURN_OK &&
2589
        drizzle_result_buffer(&fields) == DRIZZLE_RETURN_OK)
1 by brian
clean slate
2590
    {
928.1.1 by Eric Day
Started client changes.
2591
      if (drizzle_result_row_count(&tables) > 0 && !opt_silent && write_info)
1 by brian
clean slate
2592
      {
1491.5.1 by Monty Taylor
Removed bad spacing discovered while looking at translations.
2593
        tee_fprintf(stdout,
2594
                    _("Reading table information for completion of "
2595
                      "table and column names\n"
2596
                      "You can turn off this feature to get a quicker "
2597
                      "startup with -A\n\n"));
1 by brian
clean slate
2598
      }
2005.2.1 by Andrew Hutchings
Move to using I_S instead of SHOW for hash lookup table. Also kill bug in fields list in the processes
2599
2600
      std::string table_name;
2601
      while ((table_row=drizzle_row_next(&fields)))
1 by brian
clean slate
2602
      {
2005.2.1 by Andrew Hutchings
Move to using I_S instead of SHOW for hash lookup table. Also kill bug in fields list in the processes
2603
        if (table_name.compare(table_row[0]) != 0)
2604
        {
2605
          tmp_str= table_row[0];
2606
          tmp_str_lower= lower_string(tmp_str);
2607
          completion_map[tmp_str_lower]= tmp_str;
2608
          table_name= table_row[0];
2609
        }
928.1.4 by Eric Day
Fixed a few bugs, more progress on conversion.
2610
        tmp_str= table_row[0];
2005.2.1 by Andrew Hutchings
Move to using I_S instead of SHOW for hash lookup table. Also kill bug in fields list in the processes
2611
        tmp_str.append(".");
2612
        tmp_str.append(table_row[1]);
2613
        tmp_str_lower= lower_string(tmp_str);
2614
        completion_map[tmp_str_lower]= tmp_str;
2615
2616
        tmp_str= table_row[1];
2617
        tmp_str_lower= lower_string(tmp_str);
2618
        completion_map[tmp_str_lower]= tmp_str;
2619
      }
2620
    }
2621
  }
2622
  drizzle_result_free(&fields);
923.2.6 by Monty Taylor
Removing hand-done completion hash functions.
2623
  completion_iter= completion_map.begin();
1 by brian
clean slate
2624
}
2625
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
2626
/* for gnu readline */
1 by brian
clean slate
2627
2628
2629
static int reconnect(void)
2630
{
2631
  if (opt_reconnect)
2632
  {
202.3.13 by Monty Taylor
Changed gettext() to _() in drizzle.c.
2633
    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.
2634
    (void) com_connect((string *)0, 0);
1124.2.8 by Diego Medina
Fixes lp bug#432210
2635
    if (opt_rehash && connected)
1 by brian
clean slate
2636
      com_rehash(NULL, NULL);
2637
  }
1136 by Brian Aker
Merge for bug fixes.
2638
  if (! connected)
202.3.13 by Monty Taylor
Changed gettext() to _() in drizzle.c.
2639
    return put_info(_("Can't connect to the server\n"),INFO_ERROR,0,0);
1 by brian
clean slate
2640
  return 0;
2641
}
2642
77.3.7 by Monty Taylor
Made mysql into a pure-C program.
2643
static void get_current_db(void)
1 by brian
clean slate
2644
{
928.1.1 by Eric Day
Started client changes.
2645
  drizzle_return_t ret;
2646
  drizzle_result_st res;
1 by brian
clean slate
2647
1567.3.1 by Vijay Samuel
Refactoring of the drizzle client.
2648
  current_db.erase();
2649
  current_db= "";
1 by brian
clean slate
2650
  /* In case of error below current_db will be NULL */
928.1.4 by Eric Day
Fixed a few bugs, more progress on conversion.
2651
  if (drizzle_query_str(&con, &res, "SELECT DATABASE()", &ret) != NULL)
1 by brian
clean slate
2652
  {
928.1.4 by Eric Day
Fixed a few bugs, more progress on conversion.
2653
    if (ret == DRIZZLE_RETURN_OK &&
2654
        drizzle_result_buffer(&res) == DRIZZLE_RETURN_OK)
2655
    {
2656
      drizzle_row_t row= drizzle_row_next(&res);
2657
      if (row[0])
1593.1.4 by Vijay Samuel
Merge Fixes for client drizzle.
2658
        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
2659
      drizzle_result_free(&res);
928.1.4 by Eric Day
Fixed a few bugs, more progress on conversion.
2660
    }
1 by brian
clean slate
2661
  }
2662
}
2663
2664
/***************************************************************************
1441.3.1 by Vijay Samuel
all required updations have been made
2665
 The different commands
2666
***************************************************************************/
1 by brian
clean slate
2667
1877.1.1 by Andrew Hutchings
Use size_t for command in drizzle.cc
2668
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.
2669
                                      drizzle_result_st *result,
2670
                                      uint32_t *error_code)
1 by brian
clean slate
2671
{
928.1.1 by Eric Day
Started client changes.
2672
  drizzle_return_t ret;
2673
893 by Brian Aker
First pass of stripping uint
2674
  for (uint32_t retry=0;; retry++)
1 by brian
clean slate
2675
  {
2676
    int error;
928.1.1 by Eric Day
Started client changes.
2677
    if (drizzle_query(&con,result,buf,length,&ret) != NULL &&
2678
        ret == DRIZZLE_RETURN_OK)
2679
    {
1 by brian
clean slate
2680
      return 0;
928.1.1 by Eric Day
Started client changes.
2681
    }
2682
    error= put_error(&con, result);
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
2683
2684
    if (ret == DRIZZLE_RETURN_ERROR_CODE)
2685
    {
2686
      *error_code= drizzle_result_error_code(result);
2687
      drizzle_result_free(result);
2688
    }
2689
2690
    if (ret != DRIZZLE_RETURN_SERVER_GONE || retry > 1 ||
1 by brian
clean slate
2691
        !opt_reconnect)
928.1.1 by Eric Day
Started client changes.
2692
    {
1 by brian
clean slate
2693
      return error;
928.1.1 by Eric Day
Started client changes.
2694
    }
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
2695
1 by brian
clean slate
2696
    if (reconnect())
2697
      return error;
2698
  }
2699
}
2700
928.1.1 by Eric Day
Started client changes.
2701
int drizzleclient_store_result_for_lazy(drizzle_result_st *result)
1 by brian
clean slate
2702
{
928.1.1 by Eric Day
Started client changes.
2703
  if (drizzle_result_buffer(result) == DRIZZLE_RETURN_OK)
1 by brian
clean slate
2704
    return 0;
2705
928.1.1 by Eric Day
Started client changes.
2706
  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
2707
  {
2708
    int ret= put_error(&con, result);
2709
    drizzle_result_free(result);
2710
    return ret;
2711
  }
1 by brian
clean slate
2712
  return 0;
2713
}
2714
2715
static int
820.2.1 by Toru Maesaka
Killed dead code and removed a reference to a non-existent help option
2716
com_help(string *buffer, const char *)
1 by brian
clean slate
2717
{
2718
  register int i, j;
520.4.43 by mordred
A set of Solaris fixes.
2719
  char buff[32], *end;
1720.3.2 by LinuxJedi
Fix bug #617510:
2720
  std::vector<char> output_buff;
2721
  output_buff.resize(512);
1 by brian
clean slate
2722
202.3.13 by Monty Taylor
Changed gettext() to _() in drizzle.c.
2723
  put_info(_("List of all Drizzle commands:"), INFO_INFO,0,0);
1 by brian
clean slate
2724
  if (!named_cmds)
1720.3.2 by LinuxJedi
Fix bug #617510:
2725
  {
2726
    snprintf(&output_buff[0], output_buff.size(),
2727
             _("Note that all text commands must be first on line and end with '%s' or \\g"),
2728
             delimiter);
2729
    put_info(&output_buff[0], INFO_INFO, 0, 0);
2730
  }
1441.3.1 by Vijay Samuel
all required updations have been made
2731
  for (i = 0; commands[i].getName(); i++)
1 by brian
clean slate
2732
  {
1441.3.1 by Vijay Samuel
all required updations have been made
2733
    end= strcpy(buff, commands[i].getName());
2734
    end+= strlen(commands[i].getName());
2735
    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
2736
      end= strcpy(end, " ")+1;
1 by brian
clean slate
2737
    if (commands[i].func)
2738
      tee_fprintf(stdout, "%s(\\%c) %s\n", buff,
1441.3.1 by Vijay Samuel
all required updations have been made
2739
                  commands[i].getCmdChar(), _(commands[i].getDoc()));
1 by brian
clean slate
2740
  }
820.2.1 by Toru Maesaka
Killed dead code and removed a reference to a non-existent help option
2741
  tee_fprintf(stdout, "\n");
2742
  buffer->clear();
1 by brian
clean slate
2743
  return 0;
2744
}
2745
2746
2747
static int
632.1.12 by Monty Taylor
Fixed more sun studio warnings.
2748
com_clear(string *buffer, const char *)
1 by brian
clean slate
2749
{
1441.3.1 by Vijay Samuel
all required updations have been made
2750
  if (status.getAddToHistory())
1 by brian
clean slate
2751
    fix_history(buffer);
279.2.2 by Monty Taylor
Replaced DYNAMIC_STRING with C++ string in drizzle command line client.
2752
  buffer->clear();
1 by brian
clean slate
2753
  return 0;
2754
}
2755
2756
2757
/*
2758
  Execute command
1441.3.1 by Vijay Samuel
all required updations have been made
2759
  Returns: 0  if ok
2760
  -1 if not fatal error
2761
  1  if fatal error
1 by brian
clean slate
2762
*/
2763
static int
632.1.12 by Monty Taylor
Fixed more sun studio warnings.
2764
com_go(string *buffer, const char *)
1 by brian
clean slate
2765
{
77.3.1 by Monty Taylor
First steps in removing sql_string and using glib instead.
2766
  char          buff[200]; /* about 110 chars used so far */
2767
  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.
2768
  drizzle_result_st result;
2769
  drizzle_return_t ret;
2770
  uint32_t      timer, warnings= 0;
2771
  uint32_t      error= 0;
2772
  uint32_t      error_code= 0;
1 by brian
clean slate
2773
  int           err= 0;
2774
2775
  interrupted_query= 0;
2776
2777
  /* Remove garbage for nicer messages */
77.3.1 by Monty Taylor
First steps in removing sql_string and using glib instead.
2778
  remove_cntrl(buffer);
1 by brian
clean slate
2779
287.3.17 by Monty Taylor
Fixed null-query problem.
2780
  if (buffer->empty())
1 by brian
clean slate
2781
  {
77.3.1 by Monty Taylor
First steps in removing sql_string and using glib instead.
2782
    // Ignore empty quries
1441.3.1 by Vijay Samuel
all required updations have been made
2783
    if (status.getBatch())
1 by brian
clean slate
2784
      return 0;
202.3.13 by Monty Taylor
Changed gettext() to _() in drizzle.c.
2785
    return put_info(_("No query specified\n"),INFO_ERROR,0,0);
1 by brian
clean slate
2786
2787
  }
2788
  if (!connected && reconnect())
2789
  {
77.3.1 by Monty Taylor
First steps in removing sql_string and using glib instead.
2790
    // Remove query on error
279.2.2 by Monty Taylor
Replaced DYNAMIC_STRING with C++ string in drizzle command line client.
2791
    buffer->clear();
1 by brian
clean slate
2792
    return opt_reconnect ? -1 : 1;          // Fatal error
2793
  }
2794
  if (verbose)
77.3.1 by Monty Taylor
First steps in removing sql_string and using glib instead.
2795
    (void) com_print(buffer, 0);
1 by brian
clean slate
2796
2797
  if (skip_updates &&
279.2.2 by Monty Taylor
Replaced DYNAMIC_STRING with C++ string in drizzle command line client.
2798
      ((buffer->length() < 4) || (buffer->find( "SET ") != 0)))
1 by brian
clean slate
2799
  {
202.3.13 by Monty Taylor
Changed gettext() to _() in drizzle.c.
2800
    (void) put_info(_("Ignoring query to other database"),INFO_INFO,0,0);
1 by brian
clean slate
2801
    return 0;
2802
  }
2803
2804
  timer=start_timer();
2805
  executing_query= 1;
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
2806
  error= drizzleclient_real_query_for_lazy(buffer->c_str(),buffer->length(),&result, &error_code);
1 by brian
clean slate
2807
1441.3.1 by Vijay Samuel
all required updations have been made
2808
  if (status.getAddToHistory())
77.3.1 by Monty Taylor
First steps in removing sql_string and using glib instead.
2809
  {
279.2.2 by Monty Taylor
Replaced DYNAMIC_STRING with C++ string in drizzle command line client.
2810
    buffer->append(vertical ? "\\G" : delimiter);
1 by brian
clean slate
2811
    /* Append final command onto history */
2812
    fix_history(buffer);
2813
  }
2814
279.2.2 by Monty Taylor
Replaced DYNAMIC_STRING with C++ string in drizzle command line client.
2815
  buffer->clear();
1 by brian
clean slate
2816
2817
  if (error)
2818
    goto end;
2819
2820
  do
2821
  {
2822
    char *pos;
2823
2824
    if (quick)
2825
    {
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
2826
      if (drizzle_column_buffer(&result) != DRIZZLE_RETURN_OK)
1 by brian
clean slate
2827
      {
928.1.1 by Eric Day
Started client changes.
2828
        error= put_error(&con, &result);
1 by brian
clean slate
2829
        goto end;
2830
      }
2831
    }
2832
    else
2833
    {
840.1.21 by Monty Taylor
ZOMG. Renamed all the rest of the stuff in libdrizzleclient to be drizzleclient_*. I love commandline perl.
2834
      error= drizzleclient_store_result_for_lazy(&result);
1 by brian
clean slate
2835
      if (error)
2836
        goto end;
2837
    }
2838
2839
    if (verbose >= 3 || !opt_silent)
206.3.1 by Patrick Galbraith
Most everything working with client rename
2840
      drizzle_end_timer(timer,time_buff);
1 by brian
clean slate
2841
    else
2842
      time_buff[0]= '\0';
2843
2844
    /* Every branch must truncate  buff . */
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
2845
    if (drizzle_result_column_count(&result) > 0)
1 by brian
clean slate
2846
    {
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
2847
      if (!quick && drizzle_result_row_count(&result) == 0 &&
2848
          !column_types_flag)
1 by brian
clean slate
2849
      {
641.4.1 by Toru Maesaka
First pass of replacing MySQL's my_stpcpy() with appropriate libc calls
2850
        strcpy(buff, _("Empty set"));
1 by brian
clean slate
2851
      }
2852
      else
2853
      {
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
2854
        init_pager();
266.2.1 by Jim Winstead
Remove HTML and XML output support from drizzle client
2855
        if (vertical || (auto_vertical_output &&
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
2856
                         (terminal_width < get_result_width(&result))))
2857
          print_table_data_vertically(&result);
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
2858
        else if (opt_silent && verbose <= 2 && !output_tables)
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
2859
          print_tab_data(&result);
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
2860
        else
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
2861
          print_table_data(&result);
1441.3.1 by Vijay Samuel
all required updations have been made
2862
        sprintf(buff,
2863
                ngettext("%ld row in set","%ld rows in set",
2864
                         (long) drizzle_result_row_count(&result)),
2865
                (long) drizzle_result_row_count(&result));
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
2866
        end_pager();
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
2867
        if (drizzle_result_error_code(&result))
2868
          error= put_error(&con, &result);
1 by brian
clean slate
2869
      }
2870
    }
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
2871
    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
2872
      strcpy(buff,_("Query OK"));
1 by brian
clean slate
2873
    else
1441.3.1 by Vijay Samuel
all required updations have been made
2874
      sprintf(buff, ngettext("Query OK, %ld row affected",
2875
                             "Query OK, %ld rows affected",
2876
                             (long) drizzle_result_affected_rows(&result)),
2877
              (long) drizzle_result_affected_rows(&result));
1 by brian
clean slate
2878
376 by Brian Aker
strend remove
2879
    pos= strchr(buff, '\0');
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
2880
    if ((warnings= drizzle_result_warning_count(&result)))
1 by brian
clean slate
2881
    {
2882
      *pos++= ',';
2883
      *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.
2884
      char warnings_buff[20];
2885
      memset(warnings_buff,0,20);
1441.3.1 by Vijay Samuel
all required updations have been made
2886
      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.
2887
      strcpy(pos, warnings_buff);
2888
      pos+= strlen(warnings_buff);
641.4.1 by Toru Maesaka
First pass of replacing MySQL's my_stpcpy() with appropriate libc calls
2889
      pos= strcpy(pos, " warning")+8;
1 by brian
clean slate
2890
      if (warnings != 1)
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
2891
        *pos++= 's';
1 by brian
clean slate
2892
    }
641.4.1 by Toru Maesaka
First pass of replacing MySQL's my_stpcpy() with appropriate libc calls
2893
    strcpy(pos, time_buff);
77.3.7 by Monty Taylor
Made mysql into a pure-C program.
2894
    put_info(buff,INFO_RESULT,0,0);
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
2895
    if (strcmp(drizzle_result_info(&result), ""))
2896
      put_info(drizzle_result_info(&result),INFO_RESULT,0,0);
206.3.1 by Patrick Galbraith
Most everything working with client rename
2897
    put_info("",INFO_RESULT,0,0);      // Empty row
1 by brian
clean slate
2898
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
2899
    if (unbuffered)
1 by brian
clean slate
2900
      fflush(stdout);
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
2901
    drizzle_result_free(&result);
2902
2903
    if (drizzle_con_status(&con) & DRIZZLE_CON_STATUS_MORE_RESULTS_EXISTS)
2904
    {
2905
      if (drizzle_result_read(&con, &result, &ret) == NULL ||
2906
          ret != DRIZZLE_RETURN_OK)
2907
      {
2908
        if (ret == DRIZZLE_RETURN_ERROR_CODE)
2909
        {
2910
          error_code= drizzle_result_error_code(&result);
2911
          drizzle_result_free(&result);
2912
        }
2913
2914
        error= put_error(&con, NULL);
2915
        goto end;
2916
      }
2917
    }
2918
2919
  } while (drizzle_con_status(&con) & DRIZZLE_CON_STATUS_MORE_RESULTS_EXISTS);
1 by brian
clean slate
2920
  if (err >= 1)
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
2921
    error= put_error(&con, NULL);
1 by brian
clean slate
2922
2923
end:
2924
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
2925
  /* Show warnings if any or error occured */
1 by brian
clean slate
2926
  if (show_warnings == 1 && (warnings >= 1 || error))
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
2927
    print_warnings(error_code);
1 by brian
clean slate
2928
1441.3.1 by Vijay Samuel
all required updations have been made
2929
  if (!error && !status.getBatch() &&
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
2930
      drizzle_con_status(&con) & DRIZZLE_CON_STATUS_DB_DROPPED)
2931
  {
1 by brian
clean slate
2932
    get_current_db();
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
2933
  }
1 by brian
clean slate
2934
2935
  executing_query= 0;
206.3.1 by Patrick Galbraith
Most everything working with client rename
2936
  return error;        /* New command follows */
1 by brian
clean slate
2937
}
2938
2939
2940
static void init_pager()
2941
{
2942
  if (!opt_nopager)
2943
  {
1567.3.17 by Monty Taylor
Removed more C-string strings. replaced with std::string.
2944
    if (!(PAGER= popen(pager.c_str(), "w")))
1 by brian
clean slate
2945
    {
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
2946
      tee_fprintf(stdout,_( "popen() failed! defaulting PAGER to stdout!\n"));
1 by brian
clean slate
2947
      PAGER= stdout;
2948
    }
2949
  }
2950
  else
2951
    PAGER= stdout;
2952
}
2953
2954
static void end_pager()
2955
{
2956
  if (!opt_nopager)
2957
    pclose(PAGER);
2958
}
2959
2960
2961
static void init_tee(const char *file_name)
2962
{
2963
  FILE* new_outfile;
2964
  if (opt_outfile)
2965
    end_tee();
910.1.3 by Brian Aker
Remove my_fopen() and key_map.cc file (thanks to Jay's lcov)
2966
  if (!(new_outfile= fopen(file_name, "a")))
1 by brian
clean slate
2967
  {
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
2968
    tee_fprintf(stdout, _("Error logging to file '%s'\n"), file_name);
1 by brian
clean slate
2969
    return;
2970
  }
2971
  OUTFILE = new_outfile;
1567.3.17 by Monty Taylor
Removed more C-string strings. replaced with std::string.
2972
  outfile.assign(file_name);
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
2973
  tee_fprintf(stdout, _("Logging to file '%s'\n"), file_name);
1 by brian
clean slate
2974
  opt_outfile= 1;
910.1.3 by Brian Aker
Remove my_fopen() and key_map.cc file (thanks to Jay's lcov)
2975
1 by brian
clean slate
2976
  return;
2977
}
2978
2979
2980
static void end_tee()
2981
{
910.1.3 by Brian Aker
Remove my_fopen() and key_map.cc file (thanks to Jay's lcov)
2982
  fclose(OUTFILE);
1 by brian
clean slate
2983
  OUTFILE= 0;
2984
  opt_outfile= 0;
2985
  return;
2986
}
2987
2988
2989
static int
279.2.2 by Monty Taylor
Replaced DYNAMIC_STRING with C++ string in drizzle command line client.
2990
com_ego(string *buffer,const char *line)
1 by brian
clean slate
2991
{
2992
  int result;
2993
  bool oldvertical=vertical;
2994
  vertical=1;
2995
  result=com_go(buffer,line);
2996
  vertical=oldvertical;
2997
  return result;
2998
}
2999
3000
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3001
static const char *fieldtype2str(drizzle_column_type_t type)
1 by brian
clean slate
3002
{
3003
  switch (type) {
1441.3.1 by Vijay Samuel
all required updations have been made
3004
    case DRIZZLE_COLUMN_TYPE_BLOB:        return "BLOB";
3005
    case DRIZZLE_COLUMN_TYPE_DATE:        return "DATE";
3006
    case DRIZZLE_COLUMN_TYPE_DATETIME:    return "DATETIME";
3007
    case DRIZZLE_COLUMN_TYPE_NEWDECIMAL:  return "DECIMAL";
3008
    case DRIZZLE_COLUMN_TYPE_DOUBLE:      return "DOUBLE";
3009
    case DRIZZLE_COLUMN_TYPE_ENUM:        return "ENUM";
3010
    case DRIZZLE_COLUMN_TYPE_LONG:        return "LONG";
3011
    case DRIZZLE_COLUMN_TYPE_LONGLONG:    return "LONGLONG";
3012
    case DRIZZLE_COLUMN_TYPE_NULL:        return "NULL";
3013
    case DRIZZLE_COLUMN_TYPE_TIMESTAMP:   return "TIMESTAMP";
3014
    default:                     return "?-unknown-?";
1 by brian
clean slate
3015
  }
3016
}
3017
893 by Brian Aker
First pass of stripping uint
3018
static char *fieldflags2str(uint32_t f) {
1 by brian
clean slate
3019
  static char buf[1024];
3020
  char *s=buf;
3021
  *s=0;
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
3022
#define ff2s_check_flag(X)                                              \
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3023
  if (f & DRIZZLE_COLUMN_FLAGS_ ## X) { s=strcpy(s, # X " ")+strlen(# X " "); \
1441.3.1 by Vijay Samuel
all required updations have been made
3024
                        f &= ~ DRIZZLE_COLUMN_FLAGS_ ## X; }
1 by brian
clean slate
3025
  ff2s_check_flag(NOT_NULL);
3026
  ff2s_check_flag(PRI_KEY);
3027
  ff2s_check_flag(UNIQUE_KEY);
3028
  ff2s_check_flag(MULTIPLE_KEY);
3029
  ff2s_check_flag(BLOB);
3030
  ff2s_check_flag(UNSIGNED);
3031
  ff2s_check_flag(BINARY);
3032
  ff2s_check_flag(ENUM);
3033
  ff2s_check_flag(AUTO_INCREMENT);
3034
  ff2s_check_flag(TIMESTAMP);
3035
  ff2s_check_flag(SET);
3036
  ff2s_check_flag(NO_DEFAULT_VALUE);
3037
  ff2s_check_flag(NUM);
3038
  ff2s_check_flag(PART_KEY);
3039
  ff2s_check_flag(GROUP);
3040
  ff2s_check_flag(UNIQUE);
3041
  ff2s_check_flag(BINCMP);
3042
  ff2s_check_flag(ON_UPDATE_NOW);
3043
#undef ff2s_check_flag
3044
  if (f)
1441.3.1 by Vijay Samuel
all required updations have been made
3045
    sprintf(s, " unknows=0x%04x", f);
1 by brian
clean slate
3046
  return buf;
3047
}
3048
3049
static void
928.1.1 by Eric Day
Started client changes.
3050
print_field_types(drizzle_result_st *result)
1 by brian
clean slate
3051
{
928.1.1 by Eric Day
Started client changes.
3052
  drizzle_column_st   *field;
893 by Brian Aker
First pass of stripping uint
3053
  uint32_t i=0;
1 by brian
clean slate
3054
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3055
  while ((field = drizzle_column_next(result)))
1 by brian
clean slate
3056
  {
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
3057
    tee_fprintf(PAGER, _("Field %3u:  `%s`\n"
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
3058
                "Catalog:    `%s`\n"
2005.2.2 by Andrew Hutchings
Change most places to use 'schema' instead of 'database'
3059
                "Schema:     `%s`\n"
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
3060
                "Table:      `%s`\n"
3061
                "Org_table:  `%s`\n"
1567.3.14 by Monty Taylor
Removed the need for drizzled::internal usage in drizzle.cc.
3062
                "Type:       UTF-8\n"
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
3063
                "Collation:  %s (%u)\n"
3064
                "Length:     %lu\n"
3065
                "Max_length: %lu\n"
3066
                "Decimals:   %u\n"
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
3067
                "Flags:      %s\n\n"),
1 by brian
clean slate
3068
                ++i,
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3069
                drizzle_column_name(field), drizzle_column_catalog(field),
3070
                drizzle_column_db(field), drizzle_column_table(field),
3071
                drizzle_column_orig_table(field),
3072
                fieldtype2str(drizzle_column_type(field)),
3073
                drizzle_column_charset(field), drizzle_column_size(field),
3074
                drizzle_column_max_size(field), drizzle_column_decimals(field),
3075
                fieldflags2str(drizzle_column_flags(field)));
1 by brian
clean slate
3076
  }
3077
  tee_puts("", PAGER);
3078
}
3079
3080
static void
928.1.1 by Eric Day
Started client changes.
3081
print_table_data(drizzle_result_st *result)
1 by brian
clean slate
3082
{
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3083
  drizzle_row_t cur;
3084
  drizzle_return_t ret;
3085
  drizzle_column_st *field;
1707.1.22 by Brian Aker
Small cleanup in mysql.cc
3086
  std::vector<bool> num_flag;
279.2.2 by Monty Taylor
Replaced DYNAMIC_STRING with C++ string in drizzle command line client.
3087
  string separator;
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
3088
287.3.19 by Monty Taylor
Added string.reserve() calls.
3089
  separator.reserve(256);
1 by brian
clean slate
3090
1707.1.22 by Brian Aker
Small cleanup in mysql.cc
3091
  num_flag.resize(drizzle_result_column_count(result));
1 by brian
clean slate
3092
  if (column_types_flag)
3093
  {
3094
    print_field_types(result);
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3095
    if (!drizzle_result_row_count(result))
1 by brian
clean slate
3096
      return;
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3097
    drizzle_column_seek(result,0);
1 by brian
clean slate
3098
  }
279.2.2 by Monty Taylor
Replaced DYNAMIC_STRING with C++ string in drizzle command line client.
3099
  separator.append("+");
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3100
  while ((field = drizzle_column_next(result)))
1 by brian
clean slate
3101
  {
840.3.1 by Toru Maesaka
Fixed the issue of seeing an oversized separator when rendering a multibyte string to console.
3102
    uint32_t x, length= 0;
3103
3104
    if (column_names)
3105
    {
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3106
      uint32_t name_length= strlen(drizzle_column_name(field));
3107
840.3.1 by Toru Maesaka
Fixed the issue of seeing an oversized separator when rendering a multibyte string to console.
3108
      /* Check if the max_byte value is really the maximum in terms
1441.3.1 by Vijay Samuel
all required updations have been made
3109
         of visual length since multibyte characters can affect the
3110
         length of the separator. */
1567.3.21 by Monty Taylor
Moved function from drizzle client to the utf8 header where it belongs.
3111
      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.
3112
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3113
      if (name_length == drizzle_column_max_size(field))
840.1.15 by Monty Taylor
Merged from Toru.
3114
      {
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3115
        if (length < drizzle_column_max_size(field))
3116
          drizzle_column_set_max_size(field, length);
840.1.15 by Monty Taylor
Merged from Toru.
3117
      }
840.3.1 by Toru Maesaka
Fixed the issue of seeing an oversized separator when rendering a multibyte string to console.
3118
      else
840.1.15 by Monty Taylor
Merged from Toru.
3119
      {
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3120
        length= name_length;
840.1.15 by Monty Taylor
Merged from Toru.
3121
      }
840.3.1 by Toru Maesaka
Fixed the issue of seeing an oversized separator when rendering a multibyte string to console.
3122
    }
1441.3.1 by Vijay Samuel
all required updations have been made
3123
  
1 by brian
clean slate
3124
    if (quick)
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3125
      length=max(length,drizzle_column_size(field));
1 by brian
clean slate
3126
    else
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3127
      length=max(length,(uint32_t)drizzle_column_max_size(field));
3128
    if (length < 4 &&
3129
        !(drizzle_column_flags(field) & DRIZZLE_COLUMN_FLAGS_NOT_NULL))
3130
    {
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
3131
      // Room for "NULL"
3132
      length=4;
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3133
    }
3134
    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.
3135
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
3136
    for (x=0; x< (length+2); x++)
279.2.2 by Monty Taylor
Replaced DYNAMIC_STRING with C++ string in drizzle command line client.
3137
      separator.append("-");
3138
    separator.append("+");
1 by brian
clean slate
3139
  }
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
3140
279.2.2 by Monty Taylor
Replaced DYNAMIC_STRING with C++ string in drizzle command line client.
3141
  tee_puts((char*) separator.c_str(), PAGER);
1 by brian
clean slate
3142
  if (column_names)
3143
  {
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3144
    drizzle_column_seek(result,0);
1 by brian
clean slate
3145
    (void) tee_fputs("|", PAGER);
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3146
    for (uint32_t off=0; (field = drizzle_column_next(result)) ; off++)
1 by brian
clean slate
3147
    {
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3148
      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.
3149
      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.
3150
      uint32_t display_length= drizzle_column_max_size(field) + name_length -
1441.3.1 by Vijay Samuel
all required updations have been made
3151
                               numcells;
1 by brian
clean slate
3152
      tee_fprintf(PAGER, " %-*s |",(int) min(display_length,
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
3153
                                             MAX_COLUMN_LENGTH),
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3154
                  drizzle_column_name(field));
3155
      num_flag[off]= ((drizzle_column_type(field) <= DRIZZLE_COLUMN_TYPE_LONGLONG) ||
3156
                      (drizzle_column_type(field) == DRIZZLE_COLUMN_TYPE_NEWDECIMAL));
1 by brian
clean slate
3157
    }
3158
    (void) tee_fputs("\n", PAGER);
279.2.2 by Monty Taylor
Replaced DYNAMIC_STRING with C++ string in drizzle command line client.
3159
    tee_puts((char*) separator.c_str(), PAGER);
1 by brian
clean slate
3160
  }
3161
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3162
  while (1)
1 by brian
clean slate
3163
  {
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3164
    if (quick)
3165
    {
3166
      cur= drizzle_row_buffer(result, &ret);
3167
      if (ret != DRIZZLE_RETURN_OK)
3168
      {
3169
        (void)put_error(&con, result);
3170
        break;
3171
      }
3172
    }
3173
    else
3174
      cur= drizzle_row_next(result);
3175
3176
    if (cur == NULL || interrupted_query)
1 by brian
clean slate
3177
      break;
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3178
3179
    size_t *lengths= drizzle_row_field_sizes(result);
1 by brian
clean slate
3180
    (void) tee_fputs("| ", PAGER);
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3181
    drizzle_column_seek(result, 0);
3182
    for (uint32_t off= 0; off < drizzle_result_column_count(result); off++)
1 by brian
clean slate
3183
    {
3184
      const char *buffer;
893 by Brian Aker
First pass of stripping uint
3185
      uint32_t data_length;
3186
      uint32_t field_max_length;
3187
      uint32_t visible_length;
3188
      uint32_t extra_padding;
1 by brian
clean slate
3189
3190
      if (cur[off] == NULL)
3191
      {
3192
        buffer= "NULL";
3193
        data_length= 4;
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
3194
      }
3195
      else
1 by brian
clean slate
3196
      {
928.1.4 by Eric Day
Fixed a few bugs, more progress on conversion.
3197
        buffer= cur[off];
895 by Brian Aker
Completion (?) of uint conversion.
3198
        data_length= (uint32_t) lengths[off];
1 by brian
clean slate
3199
      }
3200
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3201
      field= drizzle_column_next(result);
3202
      field_max_length= drizzle_column_max_size(field);
1 by brian
clean slate
3203
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
3204
      /*
3205
        How many text cells on the screen will this string span?  If it contains
3206
        multibyte characters, then the number of characters we occupy on screen
3207
        will be fewer than the number of bytes we occupy in memory.
1 by brian
clean slate
3208
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
3209
        We need to find how much screen real-estate we will occupy to know how
3210
        many extra padding-characters we should send with the printing function.
1 by brian
clean slate
3211
      */
1567.3.21 by Monty Taylor
Moved function from drizzle client to the utf8 header where it belongs.
3212
      visible_length= drizzled::utf8::char_length(buffer);
1 by brian
clean slate
3213
      extra_padding= data_length - visible_length;
3214
3215
      if (field_max_length > MAX_COLUMN_LENGTH)
1075 by Brian Aker
Fix for CentOS
3216
        tee_print_sized_data(buffer, data_length, MAX_COLUMN_LENGTH+extra_padding, false);
1 by brian
clean slate
3217
      else
3218
      {
3219
        if (num_flag[off] != 0) /* if it is numeric, we right-justify it */
1075 by Brian Aker
Fix for CentOS
3220
          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.
3221
        else
77.3.1 by Monty Taylor
First steps in removing sql_string and using glib instead.
3222
          tee_print_sized_data(buffer, data_length,
1075 by Brian Aker
Fix for CentOS
3223
                               field_max_length+extra_padding, false);
1 by brian
clean slate
3224
      }
3225
      tee_fputs(" | ", PAGER);
3226
    }
3227
    (void) tee_fputs("\n", PAGER);
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3228
    if (quick)
3229
      drizzle_row_free(result, cur);
1 by brian
clean slate
3230
  }
279.2.2 by Monty Taylor
Replaced DYNAMIC_STRING with C++ string in drizzle command line client.
3231
  tee_puts(separator.c_str(), PAGER);
1 by brian
clean slate
3232
}
3233
3234
/**
1441.3.1 by Vijay Samuel
all required updations have been made
3235
   Return the length of a field after it would be rendered into text.
3236
3237
   This doesn't know or care about multibyte characters.  Assume we're
3238
   using such a charset.  We can't know that all of the upcoming rows
3239
   for this column will have bytes that each render into some fraction
3240
   of a character.  It's at least possible that a row has bytes that
3241
   all render into one character each, and so the maximum length is
3242
   still the number of bytes.  (Assumption 1:  This can't be better
3243
   because we can never know the number of characters that the DB is
3244
   going to send -- only the number of bytes.  2: Chars <= Bytes.)
3245
3246
   @param  field  Pointer to a field to be inspected
3247
3248
   @returns  number of character positions to be used, at most
1 by brian
clean slate
3249
*/
928.1.1 by Eric Day
Started client changes.
3250
static int get_field_disp_length(drizzle_column_st *field)
1 by brian
clean slate
3251
{
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3252
  uint32_t length= column_names ? strlen(drizzle_column_name(field)) : 0;
1 by brian
clean slate
3253
3254
  if (quick)
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3255
    length= max(length, drizzle_column_size(field));
1 by brian
clean slate
3256
  else
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3257
    length= max(length, (uint32_t)drizzle_column_max_size(field));
1 by brian
clean slate
3258
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3259
  if (length < 4 &&
1441.3.1 by Vijay Samuel
all required updations have been made
3260
    !(drizzle_column_flags(field) & DRIZZLE_COLUMN_FLAGS_NOT_NULL))
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3261
  {
206.3.1 by Patrick Galbraith
Most everything working with client rename
3262
    length= 4;        /* Room for "NULL" */
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3263
  }
1 by brian
clean slate
3264
3265
  return length;
3266
}
3267
3268
/**
1441.3.1 by Vijay Samuel
all required updations have been made
3269
   For a new result, return the max number of characters that any
3270
   upcoming row may return.
3271
3272
   @param  result  Pointer to the result to judge
3273
3274
   @returns  The max number of characters in any row of this result
1 by brian
clean slate
3275
*/
928.1.1 by Eric Day
Started client changes.
3276
static int get_result_width(drizzle_result_st *result)
1 by brian
clean slate
3277
{
3278
  unsigned int len= 0;
928.1.1 by Eric Day
Started client changes.
3279
  drizzle_column_st *field;
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3280
  uint16_t offset;
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
3281
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3282
  offset= drizzle_column_current(result);
142.1.2 by Patrick
All DBUG_x removed from client/
3283
  assert(offset == 0);
1 by brian
clean slate
3284
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3285
  while ((field= drizzle_column_next(result)) != NULL)
1 by brian
clean slate
3286
    len+= get_field_disp_length(field) + 3; /* plus bar, space, & final space */
3287
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3288
  (void) drizzle_column_seek(result, offset);
1 by brian
clean slate
3289
3290
  return len + 1; /* plus final bar. */
3291
}
3292
3293
static void
3294
tee_print_sized_data(const char *data, unsigned int data_length, unsigned int total_bytes_to_send, bool right_justified)
3295
{
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
3296
  /*
1 by brian
clean slate
3297
    For '\0's print ASCII spaces instead, as '\0' is eaten by (at
3298
    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.
3299
    grid.  (The \0 is also the reason we can't use fprintf() .)
1 by brian
clean slate
3300
  */
3301
  unsigned int i;
3302
  const char *p;
3303
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
3304
  if (right_justified)
1 by brian
clean slate
3305
    for (i= data_length; i < total_bytes_to_send; i++)
3306
      tee_putc((int)' ', PAGER);
3307
3308
  for (i= 0, p= data; i < data_length; i+= 1, p+= 1)
3309
  {
3310
    if (*p == '\0')
3311
      tee_putc((int)' ', PAGER);
3312
    else
3313
      tee_putc((int)*p, PAGER);
3314
  }
3315
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
3316
  if (! right_justified)
1 by brian
clean slate
3317
    for (i= data_length; i < total_bytes_to_send; i++)
3318
      tee_putc((int)' ', PAGER);
3319
}
3320
3321
3322
3323
static void
928.1.1 by Eric Day
Started client changes.
3324
print_table_data_vertically(drizzle_result_st *result)
1 by brian
clean slate
3325
{
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3326
  drizzle_row_t cur;
3327
  drizzle_return_t ret;
3328
  uint32_t max_length=0;
3329
  drizzle_column_st *field;
1 by brian
clean slate
3330
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3331
  while ((field = drizzle_column_next(result)))
1 by brian
clean slate
3332
  {
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3333
    uint32_t length= strlen(drizzle_column_name(field));
1 by brian
clean slate
3334
    if (length > max_length)
3335
      max_length= length;
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3336
    drizzle_column_set_max_size(field, length);
1 by brian
clean slate
3337
  }
3338
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3339
  for (uint32_t row_count=1;; row_count++)
1 by brian
clean slate
3340
  {
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3341
    if (quick)
3342
    {
3343
      cur= drizzle_row_buffer(result, &ret);
3344
      if (ret != DRIZZLE_RETURN_OK)
3345
      {
3346
        (void)put_error(&con, result);
3347
        break;
3348
      }
3349
    }
3350
    else
3351
      cur= drizzle_row_next(result);
3352
3353
    if (cur == NULL || interrupted_query)
1 by brian
clean slate
3354
      break;
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3355
    drizzle_column_seek(result,0);
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
3356
    tee_fprintf(PAGER,
3357
                "*************************** %d. row ***************************\n", row_count);
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3358
    for (uint32_t off=0; off < drizzle_result_column_count(result); off++)
1 by brian
clean slate
3359
    {
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3360
      field= drizzle_column_next(result);
3361
      tee_fprintf(PAGER, "%*s: ",(int) max_length,drizzle_column_name(field));
1 by brian
clean slate
3362
      tee_fprintf(PAGER, "%s\n",cur[off] ? (char*) cur[off] : "NULL");
3363
    }
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3364
    if (quick)
3365
      drizzle_row_free(result, cur);
1 by brian
clean slate
3366
  }
3367
}
3368
3369
3370
/* print_warnings should be called right after executing a statement */
3371
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3372
static void print_warnings(uint32_t error_code)
1 by brian
clean slate
3373
{
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3374
  const char *query;
3375
  drizzle_result_st result;
3376
  drizzle_row_t cur;
157 by Brian Aker
Second pass cleanup on removal of my_uint types
3377
  uint64_t num_rows;
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3378
  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)
3379
  FILE *out;
1 by brian
clean slate
3380
3381
  /* Get the warnings */
3382
  query= "show warnings";
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3383
  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.
3384
  drizzleclient_store_result_for_lazy(&result);
1 by brian
clean slate
3385
3386
  /* Bail out when no warnings */
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3387
  if (!(num_rows= drizzle_result_row_count(&result)))
1 by brian
clean slate
3388
    goto end;
3389
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3390
  cur= drizzle_row_next(&result);
1 by brian
clean slate
3391
3392
  /*
3393
    Don't print a duplicate of the current error.  It is possible for SHOW
3394
    WARNINGS to return multiple errors with the same code, but different
3395
    messages.  To be safe, skip printing the duplicate only if it is the only
3396
    warning.
3397
  */
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3398
  if (!cur || (num_rows == 1 &&
1441.3.1 by Vijay Samuel
all required updations have been made
3399
      error_code == (uint32_t) strtoul(cur[1], NULL, 10)))
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3400
  {
1 by brian
clean slate
3401
    goto end;
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3402
  }
1 by brian
clean slate
3403
3404
  /* Print the warnings */
1548.1.1 by Hartmut Holzgraefe
do not enable PAGER in print_warnings() when in batch mode (mysql bug #44732)
3405
  if (status.getBatch()) 
3406
  {
1563 by Brian Aker
Merge Hartmut
3407
    out= stderr;
1548.1.1 by Hartmut Holzgraefe
do not enable PAGER in print_warnings() when in batch mode (mysql bug #44732)
3408
  } 
3409
  else 
3410
  {
3411
    init_pager();
1563 by Brian Aker
Merge Hartmut
3412
    out= PAGER;
1548.1.1 by Hartmut Holzgraefe
do not enable PAGER in print_warnings() when in batch mode (mysql bug #44732)
3413
  }
1 by brian
clean slate
3414
  do
3415
  {
1548.1.1 by Hartmut Holzgraefe
do not enable PAGER in print_warnings() when in batch mode (mysql bug #44732)
3416
    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.
3417
  } while ((cur= drizzle_row_next(&result)));
1563 by Brian Aker
Merge Hartmut
3418
3419
  if (not status.getBatch())
1548.1.1 by Hartmut Holzgraefe
do not enable PAGER in print_warnings() when in batch mode (mysql bug #44732)
3420
    end_pager();
1 by brian
clean slate
3421
3422
end:
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3423
  drizzle_result_free(&result);
1 by brian
clean slate
3424
}
3425
3426
3427
static void
288 by Brian Aker
ulong cleanp in client apps
3428
safe_put_field(const char *pos,uint32_t length)
1 by brian
clean slate
3429
{
3430
  if (!pos)
3431
    tee_fputs("NULL", PAGER);
3432
  else
3433
  {
3434
    if (opt_raw_data)
3435
      tee_fputs(pos, PAGER);
3436
    else for (const char *end=pos+length ; pos != end ; pos++)
1085.3.3 by Monty Taylor
Got rid of #ifdef have utf8 stuff.
3437
    {
3438
      int l;
1567.3.20 by Monty Taylor
Removed UTF-8 lib that we don't use.
3439
      if ((l = drizzled::utf8::sequence_length(*pos)))
1085.3.3 by Monty Taylor
Got rid of #ifdef have utf8 stuff.
3440
      {
3441
        while (l--)
3442
          tee_putc(*pos++, PAGER);
3443
        pos--;
3444
        continue;
3445
      }
3446
      if (!*pos)
3447
        tee_fputs("\\0", PAGER); // This makes everything hard
3448
      else if (*pos == '\t')
3449
        tee_fputs("\\t", PAGER); // This would destroy tab format
3450
      else if (*pos == '\n')
3451
        tee_fputs("\\n", PAGER); // This too
3452
      else if (*pos == '\\')
3453
        tee_fputs("\\\\", PAGER);
3454
      else
3455
        tee_putc(*pos, PAGER);
3456
    }
1 by brian
clean slate
3457
  }
3458
}
3459
3460
3461
static void
928.1.1 by Eric Day
Started client changes.
3462
print_tab_data(drizzle_result_st *result)
1 by brian
clean slate
3463
{
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3464
  drizzle_row_t cur;
3465
  drizzle_return_t ret;
3466
  drizzle_column_st *field;
3467
  size_t *lengths;
1 by brian
clean slate
3468
3469
  if (opt_silent < 2 && column_names)
3470
  {
3471
    int first=0;
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3472
    while ((field = drizzle_column_next(result)))
1 by brian
clean slate
3473
    {
3474
      if (first++)
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
3475
        (void) tee_fputs("\t", PAGER);
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3476
      (void) tee_fputs(drizzle_column_name(field), PAGER);
1 by brian
clean slate
3477
    }
3478
    (void) tee_fputs("\n", PAGER);
3479
  }
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3480
  while (1)
1 by brian
clean slate
3481
  {
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3482
    if (quick)
3483
    {
3484
      cur= drizzle_row_buffer(result, &ret);
3485
      if (ret != DRIZZLE_RETURN_OK)
3486
      {
3487
        (void)put_error(&con, result);
3488
        break;
3489
      }
3490
    }
3491
    else
3492
      cur= drizzle_row_next(result);
3493
3494
    if (cur == NULL)
3495
      break;
3496
3497
    lengths= drizzle_row_field_sizes(result);
928.1.4 by Eric Day
Fixed a few bugs, more progress on conversion.
3498
    safe_put_field(cur[0],lengths[0]);
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3499
    for (uint32_t off=1 ; off < drizzle_result_column_count(result); off++)
1 by brian
clean slate
3500
    {
3501
      (void) tee_fputs("\t", PAGER);
928.1.4 by Eric Day
Fixed a few bugs, more progress on conversion.
3502
      safe_put_field(cur[off], lengths[off]);
1 by brian
clean slate
3503
    }
3504
    (void) tee_fputs("\n", PAGER);
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3505
    if (quick)
3506
      drizzle_row_free(result, cur);
1 by brian
clean slate
3507
  }
3508
}
3509
3510
static int
632.1.12 by Monty Taylor
Fixed more sun studio warnings.
3511
com_tee(string *, const char *line )
1 by brian
clean slate
3512
{
520.4.43 by mordred
A set of Solaris fixes.
3513
  char file_name[FN_REFLEN], *end;
3514
  const char *param;
1 by brian
clean slate
3515
1441.3.1 by Vijay Samuel
all required updations have been made
3516
  if (status.getBatch())
1 by brian
clean slate
3517
    return 0;
1567.3.14 by Monty Taylor
Removed the need for drizzled::internal usage in drizzle.cc.
3518
  while (isspace(*line))
1 by brian
clean slate
3519
    line++;
1567.3.17 by Monty Taylor
Removed more C-string strings. replaced with std::string.
3520
  if (!(param =strchr(line, ' '))) // if outfile wasn't given, use the default
1 by brian
clean slate
3521
  {
1567.3.17 by Monty Taylor
Removed more C-string strings. replaced with std::string.
3522
    if (outfile.empty())
1 by brian
clean slate
3523
    {
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
3524
      printf(_("No previous outfile available, you must give a filename!\n"));
1 by brian
clean slate
3525
      return 0;
3526
    }
3527
    else if (opt_outfile)
3528
    {
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
3529
      tee_fprintf(stdout, _("Currently logging to file '%s'\n"), outfile.c_str());
1 by brian
clean slate
3530
      return 0;
3531
    }
3532
    else
1567.3.17 by Monty Taylor
Removed more C-string strings. replaced with std::string.
3533
      param= outfile.c_str();      //resume using the old outfile
1 by brian
clean slate
3534
  }
3535
1567.3.17 by Monty Taylor
Removed more C-string strings. replaced with std::string.
3536
  /* @TODO: Replace this with string methods */
1 by brian
clean slate
3537
  /* eliminate the spaces before the parameters */
1567.3.14 by Monty Taylor
Removed the need for drizzled::internal usage in drizzle.cc.
3538
  while (isspace(*param))
1 by brian
clean slate
3539
    param++;
629.5.3 by Toru Maesaka
Third pass of replacing MySQL's strmake() with libc calls
3540
  strncpy(file_name, param, sizeof(file_name) - 1);
3541
  end= file_name + strlen(file_name);
1 by brian
clean slate
3542
  /* remove end space from command line */
1567.3.14 by Monty Taylor
Removed the need for drizzled::internal usage in drizzle.cc.
3543
  while (end > file_name && (isspace(end[-1]) ||
3544
                             iscntrl(end[-1])))
1 by brian
clean slate
3545
    end--;
3546
  end[0]= 0;
3547
  if (end == file_name)
3548
  {
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
3549
    printf(_("No outfile specified!\n"));
1 by brian
clean slate
3550
    return 0;
3551
  }
3552
  init_tee(file_name);
3553
  return 0;
3554
}
3555
3556
3557
static int
632.1.12 by Monty Taylor
Fixed more sun studio warnings.
3558
com_notee(string *, const char *)
1 by brian
clean slate
3559
{
3560
  if (opt_outfile)
3561
    end_tee();
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
3562
  tee_fprintf(stdout, _("Outfile disabled.\n"));
1 by brian
clean slate
3563
  return 0;
3564
}
3565
3566
/*
3567
  Sorry, this command is not available in Windows.
3568
*/
3569
3570
static int
632.1.12 by Monty Taylor
Fixed more sun studio warnings.
3571
com_pager(string *, const char *line)
1 by brian
clean slate
3572
{
520.4.43 by mordred
A set of Solaris fixes.
3573
  const char *param;
1 by brian
clean slate
3574
1441.3.1 by Vijay Samuel
all required updations have been made
3575
  if (status.getBatch())
1 by brian
clean slate
3576
    return 0;
3577
  /* Skip spaces in front of the pager command */
1567.3.14 by Monty Taylor
Removed the need for drizzled::internal usage in drizzle.cc.
3578
  while (isspace(*line))
1 by brian
clean slate
3579
    line++;
3580
  /* Skip the pager command */
3581
  param= strchr(line, ' ');
3582
  /* 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.
3583
  while (param && isspace(*param))
1 by brian
clean slate
3584
    param++;
1883.3.1 by Andrew Hutchings
Cleanup drizzledump and drizzle for cppcheck
3585
  if (!param || (*param == '\0')) // if pager was not given, use the default
1 by brian
clean slate
3586
  {
3587
    if (!default_pager_set)
3588
    {
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
3589
      tee_fprintf(stdout, _("Default pager wasn't set, using stdout.\n"));
1 by brian
clean slate
3590
      opt_nopager=1;
1567.3.17 by Monty Taylor
Removed more C-string strings. replaced with std::string.
3591
      pager.assign("stdout");
1 by brian
clean slate
3592
      PAGER= stdout;
3593
      return 0;
3594
    }
1567.3.17 by Monty Taylor
Removed more C-string strings. replaced with std::string.
3595
    pager.assign(default_pager);
1 by brian
clean slate
3596
  }
3597
  else
3598
  {
1567.3.17 by Monty Taylor
Removed more C-string strings. replaced with std::string.
3599
    string pager_name(param);
3600
    string::iterator end= pager_name.end();
3601
    while (end > pager_name.begin() &&
3602
           (isspace(*(end-1)) || iscntrl(*(end-1))))
3603
      --end;
3604
    pager_name.erase(end, pager_name.end());
3605
    pager.assign(pager_name);
3606
    default_pager.assign(pager_name);
1 by brian
clean slate
3607
  }
3608
  opt_nopager=0;
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
3609
  tee_fprintf(stdout, _("PAGER set to '%s'\n"), pager.c_str());
1 by brian
clean slate
3610
  return 0;
3611
}
3612
3613
3614
static int
632.1.12 by Monty Taylor
Fixed more sun studio warnings.
3615
com_nopager(string *, const char *)
1 by brian
clean slate
3616
{
1567.3.17 by Monty Taylor
Removed more C-string strings. replaced with std::string.
3617
  pager.assign("stdout");
1 by brian
clean slate
3618
  opt_nopager=1;
3619
  PAGER= stdout;
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
3620
  tee_fprintf(stdout, _("PAGER set to stdout\n"));
1 by brian
clean slate
3621
  return 0;
3622
}
3623
3624
/* If arg is given, exit without errors. This happens on command 'quit' */
3625
3626
static int
632.1.12 by Monty Taylor
Fixed more sun studio warnings.
3627
com_quit(string *, const char *)
1 by brian
clean slate
3628
{
3629
  /* let the screen auto close on a normal shutdown */
1441.3.1 by Vijay Samuel
all required updations have been made
3630
  status.setExitStatus(0);
1 by brian
clean slate
3631
  return 1;
3632
}
3633
3634
static int
632.1.12 by Monty Taylor
Fixed more sun studio warnings.
3635
com_rehash(string *, const char *)
1 by brian
clean slate
3636
{
3637
  build_completion_hash(1, 0);
3638
  return 0;
3639
}
3640
3641
3642
3643
static int
632.1.12 by Monty Taylor
Fixed more sun studio warnings.
3644
com_print(string *buffer,const char *)
1 by brian
clean slate
3645
{
3646
  tee_puts("--------------", stdout);
279.2.2 by Monty Taylor
Replaced DYNAMIC_STRING with C++ string in drizzle command line client.
3647
  (void) tee_fputs(buffer->c_str(), stdout);
3648
  if ( (buffer->length() == 0)
3649
       || (buffer->c_str())[(buffer->length())-1] != '\n')
1 by brian
clean slate
3650
    tee_putc('\n', stdout);
3651
  tee_puts("--------------\n", stdout);
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
3652
  /* If empty buffer */
3653
  return 0;
1 by brian
clean slate
3654
}
3655
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
3656
/* ARGSUSED */
1 by brian
clean slate
3657
static int
279.2.2 by Monty Taylor
Replaced DYNAMIC_STRING with C++ string in drizzle command line client.
3658
com_connect(string *buffer, const char *line)
1 by brian
clean slate
3659
{
3660
  char *tmp, buff[256];
3661
  bool save_rehash= opt_rehash;
3662
  int error;
3663
212.6.1 by Mats Kindahl
Replacing all bzero() calls with memset() calls and removing the bzero.c file.
3664
  memset(buff, 0, sizeof(buff));
1 by brian
clean slate
3665
  if (buffer)
3666
  {
3667
    /*
3668
      Two null bytes are needed in the end of buff to allow
3669
      get_arg to find end of string the second time it's called.
3670
    */
629.5.3 by Toru Maesaka
Third pass of replacing MySQL's strmake() with libc calls
3671
    tmp= strncpy(buff, line, sizeof(buff)-2);
1 by brian
clean slate
3672
#ifdef EXTRA_DEBUG
3673
    tmp[1]= 0;
3674
#endif
3675
    tmp= get_arg(buff, 0);
3676
    if (tmp && *tmp)
3677
    {
1567.3.1 by Vijay Samuel
Refactoring of the drizzle client.
3678
      current_db.erase();
1593.1.4 by Vijay Samuel
Merge Fixes for client drizzle.
3679
      current_db.assign(tmp);
1 by brian
clean slate
3680
      tmp= get_arg(buff, 1);
3681
      if (tmp)
3682
      {
1567.3.1 by Vijay Samuel
Refactoring of the drizzle client.
3683
        current_host.erase();
182.1.2 by Jim Winstead
Various fixes to enable compilation on Mac OS X, and remove the glib dependency.
3684
        current_host=strdup(tmp);
1 by brian
clean slate
3685
      }
3686
    }
3687
    else
3688
    {
3689
      /* Quick re-connect */
971.6.11 by Eric Day
Removed purecov messages.
3690
      opt_rehash= 0;
1 by brian
clean slate
3691
    }
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
3692
    // command used
77.3.3 by Monty Taylor
Last change to migrate to glib from sql_string.
3693
    assert(buffer!=NULL);
279.2.2 by Monty Taylor
Replaced DYNAMIC_STRING with C++ string in drizzle command line client.
3694
    buffer->clear();
1 by brian
clean slate
3695
  }
3696
  else
3697
    opt_rehash= 0;
1567.5.3 by Vijay Samuel
Merge patch for sql_connect() and style issue.
3698
  error=sql_connect(current_host, current_db, current_user, opt_password,0);
1 by brian
clean slate
3699
  opt_rehash= save_rehash;
3700
3701
  if (connected)
3702
  {
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
3703
    sprintf(buff, _("Connection id:    %u"), drizzle_con_thread_id(&con));
77.3.7 by Monty Taylor
Made mysql into a pure-C program.
3704
    put_info(buff,INFO_INFO,0,0);
2005.2.2 by Andrew Hutchings
Change most places to use 'schema' instead of 'database'
3705
    sprintf(buff, _("Current schema: %.128s\n"),
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
3706
            !current_db.empty() ? current_db.c_str() : _("*** NONE ***"));
77.3.7 by Monty Taylor
Made mysql into a pure-C program.
3707
    put_info(buff,INFO_INFO,0,0);
1 by brian
clean slate
3708
  }
3709
  return error;
3710
}
3711
3712
632.1.12 by Monty Taylor
Fixed more sun studio warnings.
3713
static int com_source(string *, const char *line)
1 by brian
clean slate
3714
{
520.4.43 by mordred
A set of Solaris fixes.
3715
  char source_name[FN_REFLEN], *end;
3716
  const char *param;
1095.2.1 by Robert Klahn
Replace typedef struct LINE_BUFFER with class LineBuffer, encapsulating current logic
3717
  LineBuffer *line_buff;
1 by brian
clean slate
3718
  int error;
1441.3.1 by Vijay Samuel
all required updations have been made
3719
  Status old_status;
1 by brian
clean slate
3720
  FILE *sql_file;
3721
3722
  /* Skip space from file name */
1567.3.14 by Monty Taylor
Removed the need for drizzled::internal usage in drizzle.cc.
3723
  while (isspace(*line))
1 by brian
clean slate
3724
    line++;
206.3.1 by Patrick Galbraith
Most everything working with client rename
3725
  if (!(param = strchr(line, ' ')))    // Skip command name
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
3726
    return put_info(_("Usage: \\. <filename> | source <filename>"),
77.3.7 by Monty Taylor
Made mysql into a pure-C program.
3727
                    INFO_ERROR, 0,0);
1567.3.14 by Monty Taylor
Removed the need for drizzled::internal usage in drizzle.cc.
3728
  while (isspace(*param))
1 by brian
clean slate
3729
    param++;
629.5.3 by Toru Maesaka
Third pass of replacing MySQL's strmake() with libc calls
3730
  end= strncpy(source_name,param,sizeof(source_name)-1);
3731
  end+= strlen(source_name);
1567.3.14 by Monty Taylor
Removed the need for drizzled::internal usage in drizzle.cc.
3732
  while (end > source_name && (isspace(end[-1]) ||
3733
                               iscntrl(end[-1])))
1 by brian
clean slate
3734
    end--;
3735
  end[0]=0;
1567.3.14 by Monty Taylor
Removed the need for drizzled::internal usage in drizzle.cc.
3736
1 by brian
clean slate
3737
  /* open file name */
910.1.3 by Brian Aker
Remove my_fopen() and key_map.cc file (thanks to Jay's lcov)
3738
  if (!(sql_file = fopen(source_name, "r")))
1 by brian
clean slate
3739
  {
3740
    char buff[FN_REFLEN+60];
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
3741
    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.
3742
    return put_info(buff, INFO_ERROR, 0 ,0);
1 by brian
clean slate
3743
  }
3744
1095.2.6 by Robert Klahn
Changes from additional code review feedback
3745
  line_buff= new(std::nothrow) LineBuffer(opt_max_input_line,sql_file);
1095.2.4 by Robert Klahn
changes from code review feedback
3746
  if (line_buff == NULL)
1 by brian
clean slate
3747
  {
910.1.3 by Brian Aker
Remove my_fopen() and key_map.cc file (thanks to Jay's lcov)
3748
    fclose(sql_file);
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
3749
    return put_info(_("Can't initialize LineBuffer"), INFO_ERROR, 0, 0);
1 by brian
clean slate
3750
  }
3751
3752
  /* Save old status */
3753
  old_status=status;
212.6.10 by Mats Kindahl
Removing redundant use of casts in client/ for memcmp(), memcpy(), memset(), and memmove().
3754
  memset(&status, 0, sizeof(status));
1 by brian
clean slate
3755
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
3756
  // Run in batch mode
1441.3.1 by Vijay Samuel
all required updations have been made
3757
  status.setBatch(old_status.getBatch());
3758
  status.setLineBuff(line_buff);
3759
  status.setFileName(source_name);
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
3760
  // Empty command buffer
77.3.3 by Monty Taylor
Last change to migrate to glib from sql_string.
3761
  assert(glob_buffer!=NULL);
279.2.2 by Monty Taylor
Replaced DYNAMIC_STRING with C++ string in drizzle command line client.
3762
  glob_buffer->clear();
1 by brian
clean slate
3763
  error= read_and_execute(false);
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
3764
  // Continue as before
3765
  status=old_status;
910.1.3 by Brian Aker
Remove my_fopen() and key_map.cc file (thanks to Jay's lcov)
3766
  fclose(sql_file);
1441.3.1 by Vijay Samuel
all required updations have been made
3767
  delete status.getLineBuff();
3768
  line_buff=0;
3769
  status.setLineBuff(0);
1 by brian
clean slate
3770
  return error;
3771
}
3772
3773
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
3774
/* ARGSUSED */
1 by brian
clean slate
3775
static int
632.1.12 by Monty Taylor
Fixed more sun studio warnings.
3776
com_delimiter(string *, const char *line)
1 by brian
clean slate
3777
{
3778
  char buff[256], *tmp;
3779
629.5.1 by Toru Maesaka
First pass of replacing MySQL's strmake() with libc calls
3780
  strncpy(buff, line, sizeof(buff) - 1);
1 by brian
clean slate
3781
  tmp= get_arg(buff, 0);
3782
3783
  if (!tmp || !*tmp)
3784
  {
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
3785
    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.
3786
             INFO_ERROR, 0, 0);
1 by brian
clean slate
3787
    return 0;
3788
  }
3789
  else
3790
  {
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
3791
    if (strstr(tmp, "\\"))
1 by brian
clean slate
3792
    {
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
3793
      put_info(_("DELIMITER cannot contain a backslash character"),
77.3.7 by Monty Taylor
Made mysql into a pure-C program.
3794
               INFO_ERROR, 0, 0);
1 by brian
clean slate
3795
      return 0;
3796
    }
3797
  }
629.5.1 by Toru Maesaka
First pass of replacing MySQL's strmake() with libc calls
3798
  strncpy(delimiter, tmp, sizeof(delimiter) - 1);
1 by brian
clean slate
3799
  delimiter_length= (int)strlen(delimiter);
3800
  delimiter_str= delimiter;
3801
  return 0;
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_use(string *, const char *line)
1 by brian
clean slate
3807
{
3808
  char *tmp, buff[FN_REFLEN + 1];
3809
  int select_db;
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3810
  drizzle_result_st result;
3811
  drizzle_return_t ret;
1 by brian
clean slate
3812
212.6.1 by Mats Kindahl
Replacing all bzero() calls with memset() calls and removing the bzero.c file.
3813
  memset(buff, 0, sizeof(buff));
629.5.1 by Toru Maesaka
First pass of replacing MySQL's strmake() with libc calls
3814
  strncpy(buff, line, sizeof(buff) - 1);
1 by brian
clean slate
3815
  tmp= get_arg(buff, 0);
3816
  if (!tmp || !*tmp)
3817
  {
2005.2.2 by Andrew Hutchings
Change most places to use 'schema' instead of 'database'
3818
    put_info(_("USE must be followed by a schema name"), INFO_ERROR, 0, 0);
1 by brian
clean slate
3819
    return 0;
3820
  }
3821
  /*
3822
    We need to recheck the current database, because it may change
3823
    under our feet, for example if DROP DATABASE or RENAME DATABASE
3824
    (latter one not yet available by the time the comment was written)
3825
  */
3826
  get_current_db();
3827
1567.3.1 by Vijay Samuel
Refactoring of the drizzle client.
3828
  if (current_db.empty() || strcmp(current_db.c_str(),tmp))
1 by brian
clean slate
3829
  {
3830
    if (one_database)
3831
    {
3832
      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.
3833
      select_db= 0;    // don't do drizzleclient_select_db()
1 by brian
clean slate
3834
    }
3835
    else
840.1.21 by Monty Taylor
ZOMG. Renamed all the rest of the stuff in libdrizzleclient to be drizzleclient_*. I love commandline perl.
3836
      select_db= 2;    // do drizzleclient_select_db() and build_completion_hash()
1 by brian
clean slate
3837
  }
3838
  else
3839
  {
3840
    /*
3841
      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.
3842
      We do need to send drizzleclient_select_db() to make server
1 by brian
clean slate
3843
      update database level privileges, which might
3844
      change since last USE (see bug#10979).
3845
      For performance purposes, we'll skip rebuilding of completion hash.
3846
    */
3847
    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.
3848
    select_db= 1;      // do only drizzleclient_select_db(), without completion
1 by brian
clean slate
3849
  }
3850
3851
  if (select_db)
3852
  {
3853
    /*
3854
      reconnect once if connection is down or if connection was found to
3855
      be down during query
3856
    */
3857
    if (!connected && reconnect())
3858
      return opt_reconnect ? -1 : 1;                        // Fatal error
928.1.4 by Eric Day
Fixed a few bugs, more progress on conversion.
3859
    for (bool try_again= true; try_again; try_again= false)
1 by brian
clean slate
3860
    {
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
3861
      if (drizzle_select_db(&con,&result,tmp,&ret) == NULL ||
3862
          ret != DRIZZLE_RETURN_OK)
3863
      {
3864
        if (ret == DRIZZLE_RETURN_ERROR_CODE)
3865
        {
3866
          int error= put_error(&con, &result);
3867
          drizzle_result_free(&result);
3868
          return error;
3869
        }
3870
3871
        if (ret != DRIZZLE_RETURN_SERVER_GONE || !try_again)
3872
          return put_error(&con, NULL);
3873
3874
        if (reconnect())
3875
          return opt_reconnect ? -1 : 1;                      // Fatal error
3876
      }
928.1.4 by Eric Day
Fixed a few bugs, more progress on conversion.
3877
      else
3878
        drizzle_result_free(&result);
1 by brian
clean slate
3879
    }
1567.3.1 by Vijay Samuel
Refactoring of the drizzle client.
3880
    current_db.erase();
1593.1.4 by Vijay Samuel
Merge Fixes for client drizzle.
3881
    current_db.assign(tmp);
1 by brian
clean slate
3882
    if (select_db > 1)
3883
      build_completion_hash(opt_rehash, 1);
3884
  }
3885
2005.2.2 by Andrew Hutchings
Change most places to use 'schema' instead of 'database'
3886
  put_info(_("Schema changed"),INFO_INFO, 0, 0);
1 by brian
clean slate
3887
  return 0;
3888
}
3889
1954 by Brian Aker
This adds the command "shutdown" to the command line client. It does require
3890
static int com_shutdown(string *, const char *)
3891
{
3892
  drizzle_result_st result;
3893
  drizzle_return_t ret;
3894
3895
  if (verbose)
3896
  {
3897
    printf(_("shutting down drizzled"));
3898
    if (opt_drizzle_port > 0)
3899
      printf(_(" on port %d"), opt_drizzle_port);
3900
    printf("... ");
3901
  }
3902
3903
  if (drizzle_shutdown(&con, &result, DRIZZLE_SHUTDOWN_DEFAULT,
3904
                       &ret) == NULL || ret != DRIZZLE_RETURN_OK)
3905
  {
3906
    if (ret == DRIZZLE_RETURN_ERROR_CODE)
3907
    {
3908
      fprintf(stderr, _("shutdown failed; error: '%s'"),
3909
              drizzle_result_error(&result));
3910
      drizzle_result_free(&result);
3911
    }
3912
    else
3913
    {
3914
      fprintf(stderr, _("shutdown failed; error: '%s'"),
3915
              drizzle_con_error(&con));
3916
    }
3917
    return false;
3918
  }
3919
3920
  drizzle_result_free(&result);
3921
3922
  if (verbose)
3923
    printf(_("done\n"));
3924
3925
  return false;
3926
}
3927
1 by brian
clean slate
3928
static int
632.1.12 by Monty Taylor
Fixed more sun studio warnings.
3929
com_warnings(string *, const char *)
1 by brian
clean slate
3930
{
3931
  show_warnings = 1;
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
3932
  put_info(_("Show warnings enabled."),INFO_INFO, 0, 0);
1 by brian
clean slate
3933
  return 0;
3934
}
3935
3936
static int
632.1.12 by Monty Taylor
Fixed more sun studio warnings.
3937
com_nowarnings(string *, const char *)
1 by brian
clean slate
3938
{
3939
  show_warnings = 0;
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
3940
  put_info(_("Show warnings disabled."),INFO_INFO, 0, 0);
1 by brian
clean slate
3941
  return 0;
3942
}
3943
3944
/*
3945
  Gets argument from a command on the command line. If get_next_arg is
3946
  not defined, skips the command and returns the first argument. The
3947
  line is modified by adding zero to the end of the argument. If
3948
  get_next_arg is defined, then the function searches for end of string
3949
  first, after found, returns the next argument and adds zero to the
3950
  end. If you ever wish to use this feature, remember to initialize all
3951
  items in the array to zero first.
3952
*/
3953
143 by Brian Aker
Bool cleanup.
3954
char *get_arg(char *line, bool get_next_arg)
1 by brian
clean slate
3955
{
3956
  char *ptr, *start;
143 by Brian Aker
Bool cleanup.
3957
  bool quoted= 0, valid_arg= 0;
1 by brian
clean slate
3958
  char qtype= 0;
3959
3960
  ptr= line;
3961
  if (get_next_arg)
3962
  {
3963
    for (; *ptr; ptr++) ;
3964
    if (*(ptr + 1))
3965
      ptr++;
3966
  }
3967
  else
3968
  {
3969
    /* skip leading white spaces */
1567.3.14 by Monty Taylor
Removed the need for drizzled::internal usage in drizzle.cc.
3970
    while (isspace(*ptr))
1 by brian
clean slate
3971
      ptr++;
3972
    if (*ptr == '\\') // short command was used
3973
      ptr+= 2;
3974
    else
1567.3.14 by Monty Taylor
Removed the need for drizzled::internal usage in drizzle.cc.
3975
      while (*ptr &&!isspace(*ptr)) // skip command
1 by brian
clean slate
3976
        ptr++;
3977
  }
3978
  if (!*ptr)
461 by Monty Taylor
Removed NullS. bu-bye.
3979
    return NULL;
1567.3.14 by Monty Taylor
Removed the need for drizzled::internal usage in drizzle.cc.
3980
  while (isspace(*ptr))
1 by brian
clean slate
3981
    ptr++;
3982
  if (*ptr == '\'' || *ptr == '\"' || *ptr == '`')
3983
  {
3984
    qtype= *ptr;
3985
    quoted= 1;
3986
    ptr++;
3987
  }
3988
  for (start=ptr ; *ptr; ptr++)
3989
  {
3990
    if (*ptr == '\\' && ptr[1]) // escaped character
3991
    {
3992
      // Remove the backslash
641.4.1 by Toru Maesaka
First pass of replacing MySQL's my_stpcpy() with appropriate libc calls
3993
      strcpy(ptr, ptr+1);
1 by brian
clean slate
3994
    }
3995
    else if ((!quoted && *ptr == ' ') || (quoted && *ptr == qtype))
3996
    {
3997
      *ptr= 0;
3998
      break;
3999
    }
4000
  }
4001
  valid_arg= ptr != start;
461 by Monty Taylor
Removed NullS. bu-bye.
4002
  return valid_arg ? start : NULL;
1 by brian
clean slate
4003
}
4004
4005
4006
static int
1567.5.3 by Vijay Samuel
Merge patch for sql_connect() and style issue.
4007
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
4008
                 uint32_t silent)
1 by brian
clean slate
4009
{
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
4010
  drizzle_return_t ret;
1 by brian
clean slate
4011
  if (connected)
4012
  {
4013
    connected= 0;
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
4014
    drizzle_con_free(&con);
4015
    drizzle_free(&drizzle);
4016
  }
4017
  drizzle_create(&drizzle);
1960.2.7 by Andrew Hutchings
Drop the drizzleadmin client and use ifdef instead
4018
4019
#ifdef DRIZZLE_ADMIN_TOOL
4020
  drizzle_con_options_t options= (drizzle_con_options_t) (DRIZZLE_CON_ADMIN | (use_drizzle_protocol ? DRIZZLE_CON_EXPERIMENTAL : DRIZZLE_CON_MYSQL));
4021
#else
4022
  drizzle_con_options_t options= (drizzle_con_options_t) (use_drizzle_protocol ? DRIZZLE_CON_EXPERIMENTAL : DRIZZLE_CON_MYSQL);
4023
#endif
4024
1745.2.1 by LinuxJedi
Remove the --mysql option and convert the --protocol option to do something similar.
4025
  if (drizzle_con_add_tcp(&drizzle, &con, (char *)host.c_str(),
4026
    opt_drizzle_port, (char *)user.c_str(),
4027
    (char *)password.c_str(), (char *)database.c_str(),
1960.2.7 by Andrew Hutchings
Drop the drizzleadmin client and use ifdef instead
4028
    options) == NULL)
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
4029
  {
4030
    (void) put_error(&con, NULL);
4031
    (void) fflush(stdout);
4032
    return 1;
4033
  }
4034
1441.3.1 by Vijay Samuel
all required updations have been made
4035
/* XXX add this back in
4036
  if (opt_connect_timeout)
4037
  {
893 by Brian Aker
First pass of stripping uint
4038
    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.
4039
    drizzleclient_options(&drizzle,DRIZZLE_OPT_CONNECT_TIMEOUT,
1441.3.1 by Vijay Samuel
all required updations have been made
4040
                  (char*) &timeout);
4041
  }
4042
*/
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
4043
1441.3.1 by Vijay Samuel
all required updations have been made
4044
/* XXX Do we need this?
4045
  if (safe_updates)
4046
  {
1 by brian
clean slate
4047
    char init_command[100];
4048
    sprintf(init_command,
1441.3.1 by Vijay Samuel
all required updations have been made
4049
            "SET SQL_SAFE_UPDATES=1,SQL_SELECT_LIMIT=%"PRIu32
4050
            ",MAX_JOIN_SIZE=%"PRIu32,
4051
            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.
4052
    drizzleclient_options(&drizzle, DRIZZLE_INIT_COMMAND, init_command);
1441.3.1 by Vijay Samuel
all required updations have been made
4053
  }
4054
*/
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
4055
  if ((ret= drizzle_con_connect(&con)) != DRIZZLE_RETURN_OK)
1 by brian
clean slate
4056
  {
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
4057
    if (!silent || (ret != DRIZZLE_RETURN_GETADDRINFO &&
4058
                    ret != DRIZZLE_RETURN_COULD_NOT_CONNECT))
1 by brian
clean slate
4059
    {
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
4060
      (void) put_error(&con, NULL);
1 by brian
clean slate
4061
      (void) fflush(stdout);
206.3.1 by Patrick Galbraith
Most everything working with client rename
4062
      return ignore_errors ? -1 : 1;    // Abort
1 by brian
clean slate
4063
    }
206.3.1 by Patrick Galbraith
Most everything working with client rename
4064
    return -1;          // Retryable
1 by brian
clean slate
4065
  }
4066
  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.
4067
1 by brian
clean slate
4068
  build_completion_hash(opt_rehash, 1);
4069
  return 0;
4070
}
4071
4072
4073
static int
632.1.12 by Monty Taylor
Fixed more sun studio warnings.
4074
com_status(string *, const char *)
1 by brian
clean slate
4075
{
1441.3.1 by Vijay Samuel
all required updations have been made
4076
/*
4077
  char buff[40];
4078
  uint64_t id;
4079
*/
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
4080
  drizzle_result_st result;
4081
  drizzle_return_t ret;
1 by brian
clean slate
4082
4083
  tee_puts("--------------", stdout);
1729.3.2 by LinuxJedi
Further cleanups based on Monty's comments (also revert a couple of things)
4084
  printf(_("Drizzle client %s build %s, for %s-%s (%s) using readline %s\n"),
4085
         drizzle_version(), VERSION,
1745.2.1 by LinuxJedi
Remove the --mysql option and convert the --protocol option to do something similar.
4086
         HOST_VENDOR, HOST_OS, HOST_CPU,
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
4087
         rl_library_version);
1567.3.1 by Vijay Samuel
Refactoring of the drizzle client.
4088
1 by brian
clean slate
4089
  if (connected)
4090
  {
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
4091
    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.
4092
    /*
4093
      Don't remove "limit 1",
1 by brian
clean slate
4094
      it is protection againts SQL_SELECT_LIMIT=0
4095
    */
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
4096
    if (drizzle_query_str(&con,&result,"select DATABASE(), USER() limit 1",
4097
                          &ret) != NULL && ret == DRIZZLE_RETURN_OK &&
4098
        drizzle_result_buffer(&result) == DRIZZLE_RETURN_OK)
1 by brian
clean slate
4099
    {
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
4100
      drizzle_row_t cur=drizzle_row_next(&result);
1 by brian
clean slate
4101
      if (cur)
4102
      {
2005.2.2 by Andrew Hutchings
Change most places to use 'schema' instead of 'database'
4103
        tee_fprintf(stdout, _("Current schema:\t%s\n"), cur[0] ? cur[0] : "");
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
4104
        tee_fprintf(stdout, _("Current user:\t\t%s\n"), cur[1]);
1 by brian
clean slate
4105
      }
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
4106
      drizzle_result_free(&result);
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
4107
    }
928.1.4 by Eric Day
Fixed a few bugs, more progress on conversion.
4108
    else if (ret == DRIZZLE_RETURN_ERROR_CODE)
4109
      drizzle_result_free(&result);
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
4110
    tee_puts(_("SSL:\t\t\tNot in use"), stdout);
1 by brian
clean slate
4111
  }
4112
  else
4113
  {
4114
    vidattr(A_BOLD);
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
4115
    tee_fprintf(stdout, _("\nNo connection\n"));
1 by brian
clean slate
4116
    vidattr(A_NORMAL);
4117
    return 0;
4118
  }
4119
  if (skip_updates)
4120
  {
4121
    vidattr(A_BOLD);
2005.2.2 by Andrew Hutchings
Change most places to use 'schema' instead of 'database'
4122
    tee_fprintf(stdout, _("\nAll updates ignored to this schema\n"));
1 by brian
clean slate
4123
    vidattr(A_NORMAL);
4124
  }
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
4125
  tee_fprintf(stdout, _("Current pager:\t\t%s\n"), pager.c_str());
4126
  tee_fprintf(stdout, _("Using outfile:\t\t'%s'\n"), opt_outfile ? outfile.c_str() : "");
4127
  tee_fprintf(stdout, _("Using delimiter:\t%s\n"), delimiter);
4128
  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.
4129
  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
4130
  tee_fprintf(stdout, _("Protocol version:\t%d\n"), drizzle_con_protocol_version(&con));
4131
  tee_fprintf(stdout, _("Connection:\t\t%s\n"), drizzle_con_host(&con));
1441.3.1 by Vijay Samuel
all required updations have been made
4132
/* XXX need to save this from result
4133
  if ((id= drizzleclient_insert_id(&drizzle)))
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
4134
    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
4135
*/
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
4136
1377.2.2 by Zimin
bug fix: ./bin/drizzle with \s would segfault.
4137
  if (drizzle_con_uds(&con))
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
4138
    tee_fprintf(stdout, _("UNIX socket:\t\t%s\n"), drizzle_con_uds(&con));
1 by brian
clean slate
4139
  else
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
4140
    tee_fprintf(stdout, _("TCP port:\t\t%d\n"), drizzle_con_port(&con));
1 by brian
clean slate
4141
4142
  if (safe_updates)
4143
  {
4144
    vidattr(A_BOLD);
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
4145
    tee_fprintf(stdout, _("\nNote that you are running in safe_update_mode:\n"));
1 by brian
clean slate
4146
    vidattr(A_NORMAL);
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
4147
    tee_fprintf(stdout, _("\
1441.3.1 by Vijay Samuel
all required updations have been made
4148
UPDATEs and DELETEs that don't use a key in the WHERE clause are not allowed.\n\
4149
(One can force an UPDATE/DELETE by adding LIMIT # at the end of the command.)\n \
4150
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
4151
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.
4152
                select_limit, max_join_size);
1 by brian
clean slate
4153
  }
4154
  tee_puts("--------------\n", stdout);
4155
  return 0;
4156
}
4157
4158
static const char *
928.1.7 by Eric Day
Tools mostly converted, still fixing bugs from test suite.
4159
server_version_string(drizzle_con_st *local_con)
1 by brian
clean slate
4160
{
534 by Monty Taylor
Removed stxnmov. Also deleted strstr which had already been removed.
4161
  static string buf("");
4162
  static bool server_version_string_reserved= false;
1 by brian
clean slate
4163
534 by Monty Taylor
Removed stxnmov. Also deleted strstr which had already been removed.
4164
  if (!server_version_string_reserved)
4165
  {
4166
    buf.reserve(MAX_SERVER_VERSION_LENGTH);
4167
    server_version_string_reserved= true;
4168
  }
1 by brian
clean slate
4169
  /* Only one thread calls this, so no synchronization is needed */
4170
  if (buf[0] == '\0')
4171
  {
928.1.1 by Eric Day
Started client changes.
4172
    drizzle_result_st result;
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
4173
    drizzle_return_t ret;
1 by brian
clean slate
4174
928.1.7 by Eric Day
Tools mostly converted, still fixing bugs from test suite.
4175
    buf.append(drizzle_con_server_version(local_con));
1 by brian
clean slate
4176
4177
    /* "limit 1" is protection against SQL_SELECT_LIMIT=0 */
928.1.7 by Eric Day
Tools mostly converted, still fixing bugs from test suite.
4178
    (void)drizzle_query_str(local_con, &result,
4179
                            "select @@version_comment limit 1", &ret);
928.1.1 by Eric Day
Started client changes.
4180
    if (ret == DRIZZLE_RETURN_OK &&
4181
        drizzle_result_buffer(&result) == DRIZZLE_RETURN_OK)
1 by brian
clean slate
4182
    {
928.1.1 by Eric Day
Started client changes.
4183
      drizzle_row_t cur = drizzle_row_next(&result);
1 by brian
clean slate
4184
      if (cur && cur[0])
4185
      {
534 by Monty Taylor
Removed stxnmov. Also deleted strstr which had already been removed.
4186
        buf.append(" ");
928.1.4 by Eric Day
Fixed a few bugs, more progress on conversion.
4187
        buf.append(cur[0]);
1 by brian
clean slate
4188
      }
928.1.1 by Eric Day
Started client changes.
4189
      drizzle_result_free(&result);
1 by brian
clean slate
4190
    }
928.1.4 by Eric Day
Fixed a few bugs, more progress on conversion.
4191
    else if (ret == DRIZZLE_RETURN_ERROR_CODE)
4192
      drizzle_result_free(&result);
1 by brian
clean slate
4193
  }
4194
534 by Monty Taylor
Removed stxnmov. Also deleted strstr which had already been removed.
4195
  return buf.c_str();
1 by brian
clean slate
4196
}
4197
4198
static int
893 by Brian Aker
First pass of stripping uint
4199
put_info(const char *str,INFO_TYPE info_type, uint32_t error, const char *sqlstate)
1 by brian
clean slate
4200
{
4201
  FILE *file= (info_type == INFO_ERROR ? stderr : stdout);
4202
  static int inited=0;
4203
1441.3.1 by Vijay Samuel
all required updations have been made
4204
  if (status.getBatch())
1 by brian
clean slate
4205
  {
4206
    if (info_type == INFO_ERROR)
4207
    {
4208
      (void) fflush(file);
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
4209
      fprintf(file,_("ERROR"));
1 by brian
clean slate
4210
      if (error)
4211
      {
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
4212
        if (sqlstate)
4213
          (void) fprintf(file," %d (%s)",error, sqlstate);
1 by brian
clean slate
4214
        else
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
4215
          (void) fprintf(file," %d",error);
1 by brian
clean slate
4216
      }
1441.3.1 by Vijay Samuel
all required updations have been made
4217
      if (status.getQueryStartLine() && line_numbers)
1 by brian
clean slate
4218
      {
1441.3.1 by Vijay Samuel
all required updations have been made
4219
        (void) fprintf(file," at line %"PRIu32,status.getQueryStartLine());
4220
        if (status.getFileName())
4221
          (void) fprintf(file," in file: '%s'", status.getFileName());
1 by brian
clean slate
4222
      }
4223
      (void) fprintf(file,": %s\n",str);
4224
      (void) fflush(file);
4225
      if (!ignore_errors)
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
4226
        return 1;
1 by brian
clean slate
4227
    }
4228
    else if (info_type == INFO_RESULT && verbose > 1)
4229
      tee_puts(str, file);
4230
    if (unbuffered)
4231
      fflush(file);
4232
    return info_type == INFO_ERROR ? -1 : 0;
4233
  }
4234
  if (!opt_silent || info_type == INFO_ERROR)
4235
  {
4236
    if (!inited)
4237
    {
4238
      inited=1;
4239
#ifdef HAVE_SETUPTERM
4240
      (void) setupterm((char *)0, 1, (int *) 0);
4241
#endif
4242
    }
4243
    if (info_type == INFO_ERROR)
4244
    {
4245
      if (!opt_nobeep)
77.3.7 by Monty Taylor
Made mysql into a pure-C program.
4246
        /* This should make a bell */
4247
        putchar('\a');
1 by brian
clean slate
4248
      vidattr(A_STANDOUT);
4249
      if (error)
4250
      {
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
4251
        if (sqlstate)
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
4252
          (void) tee_fprintf(file, _("ERROR %d (%s): "), error, sqlstate);
1 by brian
clean slate
4253
        else
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
4254
          (void) tee_fprintf(file, _("ERROR %d: "), error);
1 by brian
clean slate
4255
      }
4256
      else
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
4257
        tee_puts(_("ERROR: "), file);
1 by brian
clean slate
4258
    }
4259
    else
4260
      vidattr(A_BOLD);
4261
    (void) tee_puts(str, file);
4262
    vidattr(A_NORMAL);
4263
  }
4264
  if (unbuffered)
4265
    fflush(file);
4266
  return info_type == INFO_ERROR ? -1 : 0;
4267
}
4268
4269
4270
static int
928.1.7 by Eric Day
Tools mostly converted, still fixing bugs from test suite.
4271
put_error(drizzle_con_st *local_con, drizzle_result_st *res)
1 by brian
clean slate
4272
{
928.1.4 by Eric Day
Fixed a few bugs, more progress on conversion.
4273
  const char *error;
4274
4275
  if (res != NULL)
4276
  {
4277
    error= drizzle_result_error(res);
4278
    if (!strcmp(error, ""))
928.1.7 by Eric Day
Tools mostly converted, still fixing bugs from test suite.
4279
      error= drizzle_con_error(local_con);
928.1.4 by Eric Day
Fixed a few bugs, more progress on conversion.
4280
  }
4281
  else
928.1.7 by Eric Day
Tools mostly converted, still fixing bugs from test suite.
4282
    error= drizzle_con_error(local_con);
928.1.4 by Eric Day
Fixed a few bugs, more progress on conversion.
4283
928.1.7 by Eric Day
Tools mostly converted, still fixing bugs from test suite.
4284
  return put_info(error, INFO_ERROR,
928.1.8 by Eric Day
All tests now passing now, fixed a few more client utility bugs.
4285
                  res == NULL ? drizzle_con_error_code(local_con) :
1441.3.1 by Vijay Samuel
all required updations have been made
4286
                                drizzle_result_error_code(res),
928.1.8 by Eric Day
All tests now passing now, fixed a few more client utility bugs.
4287
                  res == NULL ? drizzle_con_sqlstate(local_con) :
1441.3.1 by Vijay Samuel
all required updations have been made
4288
                                drizzle_result_sqlstate(res));
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
4289
}
1 by brian
clean slate
4290
4291
279.2.2 by Monty Taylor
Replaced DYNAMIC_STRING with C++ string in drizzle command line client.
4292
static void remove_cntrl(string *buffer)
1 by brian
clean slate
4293
{
279.2.2 by Monty Taylor
Replaced DYNAMIC_STRING with C++ string in drizzle command line client.
4294
  const char *start=  buffer->c_str();
4295
  const char *end= start + (buffer->length());
1567.3.14 by Monty Taylor
Removed the need for drizzled::internal usage in drizzle.cc.
4296
  while (start < end && !isgraph(end[-1]))
1 by brian
clean slate
4297
    end--;
893 by Brian Aker
First pass of stripping uint
4298
  uint32_t pos_to_truncate= (end-start);
287.3.17 by Monty Taylor
Fixed null-query problem.
4299
  if (buffer->length() > pos_to_truncate)
4300
    buffer->erase(pos_to_truncate);
1 by brian
clean slate
4301
}
4302
4303
4304
void tee_fprintf(FILE *file, const char *fmt, ...)
4305
{
4306
  va_list args;
4307
4308
  va_start(args, fmt);
4309
  (void) vfprintf(file, fmt, args);
4310
  va_end(args);
4311
4312
  if (opt_outfile)
4313
  {
4314
    va_start(args, fmt);
4315
    (void) vfprintf(OUTFILE, fmt, args);
4316
    va_end(args);
4317
  }
4318
}
4319
4320
4321
void tee_fputs(const char *s, FILE *file)
4322
{
4323
  fputs(s, file);
4324
  if (opt_outfile)
4325
    fputs(s, OUTFILE);
4326
}
4327
4328
4329
void tee_puts(const char *s, FILE *file)
4330
{
4331
  fputs(s, file);
4332
  fputc('\n', file);
4333
  if (opt_outfile)
4334
  {
4335
    fputs(s, OUTFILE);
4336
    fputc('\n', OUTFILE);
4337
  }
4338
}
4339
4340
void tee_putc(int c, FILE *file)
4341
{
4342
  putc(c, file);
4343
  if (opt_outfile)
4344
    putc(c, OUTFILE);
4345
}
4346
4347
#include <sys/times.h>
206.3.1 by Patrick Galbraith
Most everything working with client rename
4348
#ifdef _SC_CLK_TCK        // For mit-pthreads
1 by brian
clean slate
4349
#undef CLOCKS_PER_SEC
4350
#define CLOCKS_PER_SEC (sysconf(_SC_CLK_TCK))
4351
#endif
4352
288 by Brian Aker
ulong cleanp in client apps
4353
static uint32_t start_timer(void)
1 by brian
clean slate
4354
{
4355
  struct tms tms_tmp;
4356
  return times(&tms_tmp);
4357
}
4358
4359
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
4360
/**
1441.3.1 by Vijay Samuel
all required updations have been made
4361
   Write as many as 52+1 bytes to buff, in the form of a legible
4362
   duration of time.
1 by brian
clean slate
4363
1441.3.1 by Vijay Samuel
all required updations have been made
4364
   len("4294967296 days, 23 hours, 59 minutes, 60.00 seconds")  ->  52
1 by brian
clean slate
4365
*/
4366
static void nice_time(double sec,char *buff,bool part_second)
4367
{
288 by Brian Aker
ulong cleanp in client apps
4368
  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.
4369
  ostringstream tmp_buff_str;
4370
1 by brian
clean slate
4371
  if (sec >= 3600.0*24)
4372
  {
288 by Brian Aker
ulong cleanp in client apps
4373
    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
4374
    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.
4375
    tmp_buff_str << tmp;
641.4.1 by Toru Maesaka
First pass of replacing MySQL's my_stpcpy() with appropriate libc calls
4376
4377
    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.
4378
      tmp_buff_str << " days ";
641.4.1 by Toru Maesaka
First pass of replacing MySQL's my_stpcpy() with appropriate libc calls
4379
    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.
4380
      tmp_buff_str << " day ";
641.4.1 by Toru Maesaka
First pass of replacing MySQL's my_stpcpy() with appropriate libc calls
4381
1 by brian
clean slate
4382
  }
4383
  if (sec >= 3600.0)
4384
  {
288 by Brian Aker
ulong cleanp in client apps
4385
    tmp=(uint32_t) floor(sec/3600.0);
1 by brian
clean slate
4386
    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.
4387
    tmp_buff_str << tmp;
641.4.1 by Toru Maesaka
First pass of replacing MySQL's my_stpcpy() with appropriate libc calls
4388
4389
    if (tmp > 1)
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
4390
      tmp_buff_str << _(" hours ");
641.4.1 by Toru Maesaka
First pass of replacing MySQL's my_stpcpy() with appropriate libc calls
4391
    else
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
4392
      tmp_buff_str << _(" hour ");
1 by brian
clean slate
4393
  }
4394
  if (sec >= 60.0)
4395
  {
288 by Brian Aker
ulong cleanp in client apps
4396
    tmp=(uint32_t) floor(sec/60.0);
1 by brian
clean slate
4397
    sec-=60.0*tmp;
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
4398
    tmp_buff_str << tmp << _(" min ");
1 by brian
clean slate
4399
  }
4400
  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.
4401
    tmp_buff_str.precision(2);
1 by brian
clean slate
4402
  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.
4403
    tmp_buff_str.precision(0);
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
4404
  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.
4405
  strcpy(buff, tmp_buff_str.str().c_str());
1 by brian
clean slate
4406
}
4407
4408
288 by Brian Aker
ulong cleanp in client apps
4409
static void end_timer(uint32_t start_time,char *buff)
1 by brian
clean slate
4410
{
4411
  nice_time((double) (start_timer() - start_time) /
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
4412
            CLOCKS_PER_SEC,buff,1);
1 by brian
clean slate
4413
}
4414
4415
288 by Brian Aker
ulong cleanp in client apps
4416
static void drizzle_end_timer(uint32_t start_time,char *buff)
1 by brian
clean slate
4417
{
4418
  buff[0]=' ';
4419
  buff[1]='(';
4420
  end_timer(start_time,buff+2);
641.4.1 by Toru Maesaka
First pass of replacing MySQL's my_stpcpy() with appropriate libc calls
4421
  strcpy(strchr(buff, '\0'),")");
1 by brian
clean slate
4422
}
4423
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
4424
static const char * construct_prompt()
1 by brian
clean slate
4425
{
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
4426
  // Erase the old prompt
77.3.3 by Monty Taylor
Last change to migrate to glib from sql_string.
4427
  assert(processed_prompt!=NULL);
279.2.2 by Monty Taylor
Replaced DYNAMIC_STRING with C++ string in drizzle command line client.
4428
  processed_prompt->clear();
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
4429
4430
  // Get the date struct
4431
  time_t  lclock = time(NULL);
1 by brian
clean slate
4432
  struct tm *t = localtime(&lclock);
4433
4434
  /* parse thru the settings for the prompt */
1596.1.1 by Monty Taylor
Merged in remove-internal-from-drizzle-cc
4435
  string::iterator c= current_prompt.begin();
4436
  while (c != current_prompt.end())
1 by brian
clean slate
4437
  {
4438
    if (*c != PROMPT_CHAR)
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
4439
    {
1596.1.1 by Monty Taylor
Merged in remove-internal-from-drizzle-cc
4440
      processed_prompt->push_back(*c);
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
4441
    }
1 by brian
clean slate
4442
    else
4443
    {
77.3.7 by Monty Taylor
Made mysql into a pure-C program.
4444
      int getHour;
4445
      int getYear;
971.4.1 by Monty Taylor
GCC on Solaris build fixes.
4446
      /* Room for Dow MMM DD HH:MM:SS YYYY */ 
4447
      char dateTime[32];
1 by brian
clean slate
4448
      switch (*++c) {
4449
      case '\0':
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
4450
        // stop it from going beyond if ends with %
1596.1.1 by Monty Taylor
Merged in remove-internal-from-drizzle-cc
4451
        --c;
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
4452
        break;
1 by brian
clean slate
4453
      case 'c':
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
4454
        add_int_to_prompt(++prompt_counter);
4455
        break;
1 by brian
clean slate
4456
      case 'v':
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
4457
        if (connected)
928.1.3 by Eric Day
More changes towards getting the client utilities converted.
4458
          processed_prompt->append(drizzle_con_server_version(&con));
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
4459
        else
279.2.2 by Monty Taylor
Replaced DYNAMIC_STRING with C++ string in drizzle command line client.
4460
          processed_prompt->append("not_connected");
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
4461
        break;
1 by brian
clean slate
4462
      case 'd':
1596.1.1 by Monty Taylor
Merged in remove-internal-from-drizzle-cc
4463
        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.
4464
        break;
1 by brian
clean slate
4465
      case 'h':
1441.3.1 by Vijay Samuel
all required updations have been made
4466
      {
1596.1.1 by Monty Taylor
Merged in remove-internal-from-drizzle-cc
4467
        const char *prompt= connected ? drizzle_con_host(&con) : "not_connected";
1441.3.1 by Vijay Samuel
all required updations have been made
4468
        if (strstr(prompt, "Localhost"))
4469
          processed_prompt->append("localhost");
4470
        else
4471
        {
4472
          const char *end=strrchr(prompt,' ');
4473
          if (end != NULL)
4474
            processed_prompt->append(prompt, (end-prompt));
4475
        }
4476
        break;
4477
      }
4478
      case 'p':
4479
      {
4480
        if (!connected)
4481
        {
4482
          processed_prompt->append("not_connected");
1407 by Brian Aker
Merge Siddharth, ran formatting across it.
4483
          break;
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
4484
        }
1441.3.1 by Vijay Samuel
all required updations have been made
4485
4486
        if (drizzle_con_uds(&con))
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
4487
        {
1441.3.1 by Vijay Samuel
all required updations have been made
4488
          const char *pos=strrchr(drizzle_con_uds(&con),'/');
4489
          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.
4490
        }
1441.3.1 by Vijay Samuel
all required updations have been made
4491
        else
4492
          add_int_to_prompt(drizzle_con_port(&con));
4493
      }
4494
      break;
1 by brian
clean slate
4495
      case 'U':
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
4496
        if (!full_username)
4497
          init_username();
279.2.2 by Monty Taylor
Replaced DYNAMIC_STRING with C++ string in drizzle command line client.
4498
        processed_prompt->append(full_username ? full_username :
1596.1.1 by Monty Taylor
Merged in remove-internal-from-drizzle-cc
4499
                                 (!current_user.empty() ?  current_user : "(unknown)"));
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
4500
        break;
1 by brian
clean slate
4501
      case 'u':
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
4502
        if (!full_username)
4503
          init_username();
279.2.2 by Monty Taylor
Replaced DYNAMIC_STRING with C++ string in drizzle command line client.
4504
        processed_prompt->append(part_username ? part_username :
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
4505
                                 (!current_user.empty() ?  current_user : _("(unknown)")));
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
4506
        break;
1 by brian
clean slate
4507
      case PROMPT_CHAR:
182.1.2 by Jim Winstead
Various fixes to enable compilation on Mac OS X, and remove the glib dependency.
4508
        {
632.1.12 by Monty Taylor
Fixed more sun studio warnings.
4509
          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.
4510
        }
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
4511
        break;
1 by brian
clean slate
4512
      case 'n':
182.1.2 by Jim Winstead
Various fixes to enable compilation on Mac OS X, and remove the glib dependency.
4513
        {
632.1.12 by Monty Taylor
Fixed more sun studio warnings.
4514
          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.
4515
        }
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
4516
        break;
1 by brian
clean slate
4517
      case ' ':
4518
      case '_':
182.1.2 by Jim Winstead
Various fixes to enable compilation on Mac OS X, and remove the glib dependency.
4519
        {
632.1.12 by Monty Taylor
Fixed more sun studio warnings.
4520
          processed_prompt->append(' ', 1);
182.1.2 by Jim Winstead
Various fixes to enable compilation on Mac OS X, and remove the glib dependency.
4521
        }
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
4522
        break;
1 by brian
clean slate
4523
      case 'R':
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
4524
        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.
4525
          add_int_to_prompt(0);
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
4526
        add_int_to_prompt(t->tm_hour);
4527
        break;
1 by brian
clean slate
4528
      case 'r':
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
4529
        getHour = t->tm_hour % 12;
4530
        if (getHour == 0)
4531
          getHour=12;
4532
        if (getHour < 10)
182.1.2 by Jim Winstead
Various fixes to enable compilation on Mac OS X, and remove the glib dependency.
4533
          add_int_to_prompt(0);
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
4534
        add_int_to_prompt(getHour);
4535
        break;
1 by brian
clean slate
4536
      case 'm':
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
4537
        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.
4538
          add_int_to_prompt(0);
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
4539
        add_int_to_prompt(t->tm_min);
4540
        break;
1 by brian
clean slate
4541
      case 'y':
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
4542
        getYear = t->tm_year % 100;
4543
        if (getYear < 10)
182.1.2 by Jim Winstead
Various fixes to enable compilation on Mac OS X, and remove the glib dependency.
4544
          add_int_to_prompt(0);
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
4545
        add_int_to_prompt(getYear);
4546
        break;
1 by brian
clean slate
4547
      case 'Y':
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
4548
        add_int_to_prompt(t->tm_year+1900);
4549
        break;
1 by brian
clean slate
4550
      case 'D':
971.4.1 by Monty Taylor
GCC on Solaris build fixes.
4551
        strftime(dateTime, 32, "%a %b %d %H:%M:%S %Y", localtime(&lclock));
4552
        processed_prompt->append(dateTime);
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
4553
        break;
1 by brian
clean slate
4554
      case 's':
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
4555
        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.
4556
          add_int_to_prompt(0);
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
4557
        add_int_to_prompt(t->tm_sec);
4558
        break;
1 by brian
clean slate
4559
      case 'w':
279.2.2 by Monty Taylor
Replaced DYNAMIC_STRING with C++ string in drizzle command line client.
4560
        processed_prompt->append(day_names[t->tm_wday]);
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
4561
        break;
1 by brian
clean slate
4562
      case 'P':
279.2.2 by Monty Taylor
Replaced DYNAMIC_STRING with C++ string in drizzle command line client.
4563
        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.
4564
        break;
1 by brian
clean slate
4565
      case 'o':
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
4566
        add_int_to_prompt(t->tm_mon+1);
4567
        break;
1 by brian
clean slate
4568
      case 'O':
279.2.2 by Monty Taylor
Replaced DYNAMIC_STRING with C++ string in drizzle command line client.
4569
        processed_prompt->append(month_names[t->tm_mon]);
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
4570
        break;
1 by brian
clean slate
4571
      case '\'':
279.2.2 by Monty Taylor
Replaced DYNAMIC_STRING with C++ string in drizzle command line client.
4572
        processed_prompt->append("'");
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
4573
        break;
1 by brian
clean slate
4574
      case '"':
279.2.2 by Monty Taylor
Replaced DYNAMIC_STRING with C++ string in drizzle command line client.
4575
        processed_prompt->append("\"");
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
4576
        break;
1 by brian
clean slate
4577
      case 'S':
279.2.2 by Monty Taylor
Replaced DYNAMIC_STRING with C++ string in drizzle command line client.
4578
        processed_prompt->append(";");
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
4579
        break;
1 by brian
clean slate
4580
      case 't':
279.2.2 by Monty Taylor
Replaced DYNAMIC_STRING with C++ string in drizzle command line client.
4581
        processed_prompt->append("\t");
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
4582
        break;
1 by brian
clean slate
4583
      case 'l':
1596.1.1 by Monty Taylor
Merged in remove-internal-from-drizzle-cc
4584
        processed_prompt->append(delimiter_str);
77.3.2 by Monty Taylor
Got it compiling... now, too bad it doesn't work.
4585
        break;
1 by brian
clean slate
4586
      default:
1596.1.1 by Monty Taylor
Merged in remove-internal-from-drizzle-cc
4587
        processed_prompt->push_back(*c);
1 by brian
clean slate
4588
      }
4589
    }
1596.1.1 by Monty Taylor
Merged in remove-internal-from-drizzle-cc
4590
    ++c;
1 by brian
clean slate
4591
  }
279.2.2 by Monty Taylor
Replaced DYNAMIC_STRING with C++ string in drizzle command line client.
4592
  return processed_prompt->c_str();
1 by brian
clean slate
4593
}
4594
4595
4596
static void add_int_to_prompt(int toadd)
4597
{
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.
4598
  ostringstream buffer;
4599
  buffer << toadd;
4600
  processed_prompt->append(buffer.str().c_str());
1 by brian
clean slate
4601
}
4602
4603
static void init_username()
4604
{
1441.3.1 by Vijay Samuel
all required updations have been made
4605
/* XXX need this?
4606
  free(full_username);
4607
  free(part_username);
1 by brian
clean slate
4608
1441.3.1 by Vijay Samuel
all required updations have been made
4609
  drizzle_result_st *result;
4610
  if (!drizzleclient_query(&drizzle,"select USER()") &&
4611
      (result=drizzleclient_use_result(&drizzle)))
4612
  {
928.1.1 by Eric Day
Started client changes.
4613
    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.
4614
    full_username= strdup(cur[0]);
4615
    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.
4616
    (void) drizzleclient_fetch_row(result);        // Read eof
1441.3.1 by Vijay Samuel
all required updations have been made
4617
  }
4618
*/
1 by brian
clean slate
4619
}
4620
632.1.12 by Monty Taylor
Fixed more sun studio warnings.
4621
static int com_prompt(string *, const char *line)
1 by brian
clean slate
4622
{
520.4.43 by mordred
A set of Solaris fixes.
4623
  const char *ptr=strchr(line, ' ');
656.1.51 by Monty Taylor
Fixed strdup return checking in client/
4624
  if (ptr == NULL)
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
4625
    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.
4626
                default_prompt);
656.1.51 by Monty Taylor
Fixed strdup return checking in client/
4627
  prompt_counter = 0;
4628
  char * tmpptr= strdup(ptr ? ptr+1 : default_prompt);
4629
  if (tmpptr == NULL)
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
4630
    tee_fprintf(stdout, _("Memory allocation error. Not changing prompt\n"));
1 by brian
clean slate
4631
  else
656.1.51 by Monty Taylor
Fixed strdup return checking in client/
4632
  {
1567.3.1 by Vijay Samuel
Refactoring of the drizzle client.
4633
    current_prompt.erase();
656.1.51 by Monty Taylor
Fixed strdup return checking in client/
4634
    current_prompt= tmpptr;
1729.3.1 by LinuxJedi
some string and options cleanups for drizzle and drizzledump
4635
    tee_fprintf(stdout, _("PROMPT set to '%s'\n"), current_prompt.c_str());
656.1.51 by Monty Taylor
Fixed strdup return checking in client/
4636
  }
1 by brian
clean slate
4637
  return 0;
4638
}
266.6.1 by Andy Lester
merging from main
4639
4640
/*
1441.3.1 by Vijay Samuel
all required updations have been made
4641
    strcont(str, set) if str contanies any character in the string set.
4642
    The result is the position of the first found character in str, or NULL
4643
    if there isn't anything found.
266.6.1 by Andy Lester
merging from main
4644
*/
4645
4646
static const char * strcont(register const char *str, register const char *set)
4647
{
4648
  register const char * start = (const char *) set;
4649
4650
  while (*str)
4651
  {
4652
    while (*set)
4653
    {
4654
      if (*set++ == *str)
4655
        return ((const char*) str);
4656
    }
4657
    set=start; str++;
4658
  }
4659
  return NULL;
4660
} /* strcont */