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