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