~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to client/drizzle.cc

Cleanup around SAFEMALLOC

Show diffs side-by-side

added added

removed removed

Lines of Context:
33
33
 *
34
34
 **/
35
35
 
 
36
#include <string>
 
37
 
36
38
#include "client_priv.h"
37
 
#include <string>
38
 
#include <drizzled/gettext.h>
39
 
#include <iostream>
40
 
#include <map>
41
 
#include <algorithm>
42
 
#include <limits.h>
43
 
#include <cassert>
44
 
#include "drizzled/charset_info.h"
 
39
#include <mystrings/m_ctype.h>
45
40
#include <stdarg.h>
46
 
#include <math.h>
47
 
#include "client/linebuffer.h"
 
41
#ifndef __GNU_LIBRARY__
 
42
#define __GNU_LIBRARY__          // Skip warnings in getopt.h
 
43
#endif
 
44
#include <readline/history.h>
 
45
#include "my_readline.h"
48
46
#include <signal.h>
 
47
#include <vio/violite.h>
49
48
#include <sys/ioctl.h>
50
 
#include <drizzled/configmake.h>
51
 
#include "drizzled/charset.h"
 
49
 
 
50
 
 
51
#if defined(HAVE_LOCALE_H)
 
52
#include <locale.h>
 
53
#endif
 
54
 
 
55
#include <libdrizzle/gettext.h>
 
56
 
 
57
const char *VER= "14.14";
 
58
 
 
59
/* Don't try to make a nice table if the data is too big */
 
60
#define MAX_COLUMN_LENGTH       (uint32_t)1024
 
61
 
 
62
/* Buffer to hold 'version' and 'version_comment' */
 
63
#define MAX_SERVER_VERSION_LENGTH     128
 
64
 
 
65
/* Array of options to pass to libdrizzled */
 
66
#define MAX_SERVER_ARGS               64
 
67
 
 
68
void* sql_alloc(unsigned size);       // Don't use drizzled alloc for these
 
69
void sql_element_free(void *ptr);
52
70
 
53
71
#if defined(HAVE_CURSES_H) && defined(HAVE_TERM_H)
54
72
#include <curses.h>
55
 
#ifdef __sun
56
 
#undef clear
57
 
#undef erase
58
 
#endif
59
73
#include <term.h>
60
74
#else
61
75
#if defined(HAVE_TERMIOS_H)
66
80
#elif defined(HAVE_ASM_TERMBITS_H) && (!defined __GLIBC__ || !(__GLIBC__ > 2 || __GLIBC__ == 2 && __GLIBC_MINOR__ > 0))
67
81
#include <asm/termbits.h>    // Standard linux
68
82
#endif
 
83
#undef VOID
69
84
#if defined(HAVE_TERMCAP_H)
70
85
#include <termcap.h>
71
86
#else
79
94
#endif
80
95
#endif
81
96
 
82
 
#ifdef HAVE_LIBREADLINE
83
 
#  if defined(HAVE_READLINE_READLINE_H)
84
 
#    include <readline/readline.h>
85
 
#  elif defined(HAVE_READLINE_H)
86
 
#    include <readline.h>
87
 
#  else /* !defined(HAVE_READLINE_H) */
88
 
extern char *readline ();
89
 
#  endif /* !defined(HAVE_READLINE_H) */
90
 
char *cmdline = NULL;
91
 
#else /* !defined(HAVE_READLINE_READLINE_H) */
92
 
/* no readline */
93
 
#  error Readline Required
94
 
#endif /* HAVE_LIBREADLINE */
 
97
#undef bcmp                             // Fix problem with new readline
95
98
 
96
 
#ifdef HAVE_READLINE_HISTORY
97
 
#  if defined(HAVE_READLINE_HISTORY_H)
98
 
#    include <readline/history.h>
99
 
#  elif defined(HAVE_HISTORY_H)
100
 
#    include <history.h>
101
 
#  else /* !defined(HAVE_HISTORY_H) */
102
 
extern void add_history ();
103
 
extern int write_history ();
104
 
extern int read_history ();
105
 
#  endif /* defined(HAVE_READLINE_HISTORY_H) */
106
 
/* no history */
107
 
#endif /* HAVE_READLINE_HISTORY */
 
99
#ifdef HAVE_READLINE_HISTORY_H
 
100
#include <readline/history.h>
 
101
#endif
 
102
#include <readline/readline.h>
108
103
 
109
104
/**
110
 
  Make the old readline interface look like the new one.
 
105
 Make the old readline interface look like the new one.
111
106
*/
112
 
#ifndef HAVE_RL_COMPLETION
113
 
typedef char **rl_completion_func_t(const char *, int, int);
 
107
#ifndef USE_NEW_READLINE_INTERFACE
 
108
typedef CPPFunction rl_completion_func_t;
 
109
typedef Function rl_compentry_func_t;
114
110
#define rl_completion_matches(str, func) \
115
111
  completion_matches((char *)str, (CPFunction *)func)
116
112
#endif
117
113
 
118
 
#ifdef HAVE_RL_COMPENTRY
119
 
# ifdef HAVE_WORKING_RL_COMPENTRY
120
 
typedef rl_compentry_func_t drizzle_compentry_func_t;
121
 
# else
122
 
/* Snow Leopard ships an rl_compentry which cannot be assigned to
123
 
 * rl_completion_entry_function. We must undo the complete and total
124
 
 * ass-bagery.
125
 
 */
126
 
typedef Function drizzle_compentry_func_t;
127
 
# endif
128
 
#else
129
 
typedef Function drizzle_compentry_func_t;
130
 
#endif
131
 
 
132
 
#if defined(HAVE_LOCALE_H)
133
 
#include <locale.h>
134
 
#endif
135
 
 
136
 
 
137
114
 
138
115
#if !defined(HAVE_VIDATTR)
139
116
#undef vidattr
140
117
#define vidattr(A) {}      // Can't get this to work
141
118
#endif
142
119
 
143
 
using namespace drizzled;
 
120
#ifdef FN_NO_CASE_SENCE
 
121
#define cmp_database(cs,A,B) my_strcasecmp((cs), (A), (B))
 
122
#else
 
123
#define cmp_database(cs,A,B) strcmp((A),(B))
 
124
#endif
 
125
 
 
126
#include "completion_hash.h"
 
127
 
144
128
using namespace std;
145
129
 
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
 
 
153
130
#define PROMPT_CHAR '\\'
154
131
#define DEFAULT_DELIMITER ";"
155
132
 
158
135
  int exit_status;
159
136
  uint32_t query_start_line;
160
137
  char *file_name;
161
 
  LineBuffer *line_buff;
 
138
  LINE_BUFFER *line_buff;
162
139
  bool batch,add_to_history;
163
140
} STATUS;
164
141
 
165
142
 
166
 
static map<string, string>::iterator completion_iter;
167
 
static map<string, string>::iterator completion_end;
168
 
static map<string, string> completion_map;
169
 
static string completion_string;
170
 
 
 
143
static HashTable ht;
171
144
static char **defaults_argv;
172
145
 
173
146
enum enum_info_type { INFO_INFO,INFO_ERROR,INFO_RESULT};
174
147
typedef enum enum_info_type INFO_TYPE;
175
148
 
176
 
static drizzle_st drizzle;      /* The library handle */
177
 
static drizzle_con_st con;      /* The connection */
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,
182
 
            opt_compress= false, opt_shutdown= false, opt_ping= false,
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,
189
 
            show_warnings= false, executing_query= false, interrupted_query= false,
190
 
            opt_mysql= false;
191
 
static uint32_t  show_progress_size= 0;
 
149
static DRIZZLE drizzle;      /* The connection */
 
150
static bool ignore_errors=0,quick=0,
 
151
  connected=0,opt_raw_data=0,unbuffered=0,output_tables=0,
 
152
  opt_rehash=1,skip_updates=0,safe_updates=0,one_database=0,
 
153
  opt_compress=0, using_opt_local_infile=0,
 
154
  vertical=0, line_numbers=1, column_names=1,
 
155
  opt_nopager=1, opt_outfile=0, named_cmds= 0,
 
156
  tty_password= 0, opt_nobeep=0, opt_reconnect=1,
 
157
  default_charset_used= 0, opt_secure_auth= 0,
 
158
  default_pager_set= 0, opt_sigint_ignore= 0,
 
159
  auto_vertical_output= 0,
 
160
  show_warnings= 0, executing_query= 0, interrupted_query= 0;
 
161
static bool debug_info_flag, debug_check_flag;
192
162
static bool column_types_flag;
193
 
static bool preserve_comments= false;
194
 
static uint32_t opt_max_input_line, opt_drizzle_port= 0;
195
 
static int verbose= 0, opt_silent= 0, opt_local_infile= 0;
196
 
static drizzle_capabilities_t connect_flag= DRIZZLE_CAPABILITIES_NONE;
197
 
static char *current_host, *current_db, *current_user= NULL,
198
 
            *opt_password= NULL, *delimiter_str= NULL, *current_prompt= NULL;
 
163
static bool preserve_comments= 0;
 
164
static uint32_t opt_max_allowed_packet, opt_net_buffer_length;
 
165
static int verbose=0,opt_silent=0,opt_drizzle_port=0, opt_local_infile=0;
 
166
static uint my_end_arg;
 
167
static char * opt_drizzle_unix_port=0;
 
168
static int connect_flag=CLIENT_INTERACTIVE;
 
169
static char *current_host,*current_db,*current_user=0,*opt_password=0,
 
170
  *delimiter_str= 0,* current_prompt= 0,
 
171
  *default_charset= (char*) DRIZZLE_DEFAULT_CHARSET_NAME;
199
172
static char *histfile;
200
173
static char *histfile_tmp;
201
174
static string *glob_buffer;
202
175
static string *processed_prompt= NULL;
203
176
static char *default_prompt= NULL;
204
 
static char *full_username= NULL,*part_username= NULL;
 
177
static char *full_username=0,*part_username=0;
205
178
static STATUS status;
206
179
static uint32_t select_limit;
207
180
static uint32_t max_join_size;
208
181
static uint32_t opt_connect_timeout= 0;
 
182
static char drizzle_charsets_dir[FN_REFLEN+1];
209
183
// TODO: Need to i18n these
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",
212
 
  "Aug","Sep","Oct","Nov","Dec"};
 
184
static const char *day_names[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
 
185
static const char *month_names[]={"Jan","Feb","Mar","Apr","May","Jun","Jul",
 
186
                                  "Aug","Sep","Oct","Nov","Dec"};
213
187
static char default_pager[FN_REFLEN];
214
188
static char pager[FN_REFLEN], outfile[FN_REFLEN];
215
189
static FILE *PAGER, *OUTFILE;
216
 
static uint32_t prompt_counter;
 
190
static MEM_ROOT hash_mem_root;
 
191
static uint prompt_counter;
217
192
static char delimiter[16]= DEFAULT_DELIMITER;
218
 
static uint32_t delimiter_length= 1;
 
193
static uint delimiter_length= 1;
219
194
unsigned short terminal_width= 80;
220
195
 
 
196
static uint opt_protocol= DRIZZLE_PROTOCOL_TCP;
221
197
static const CHARSET_INFO *charset_info= &my_charset_utf8_general_ci;
222
198
 
223
 
int drizzleclient_real_query_for_lazy(const char *buf, int length,
224
 
                                      drizzle_result_st *result,
225
 
                                      uint32_t *error_code);
226
 
int drizzleclient_store_result_for_lazy(drizzle_result_st *result);
 
199
int drizzle_real_query_for_lazy(const char *buf, int length);
 
200
int drizzle_store_result_for_lazy(DRIZZLE_RES **result);
227
201
 
228
202
 
229
203
void tee_fprintf(FILE *file, const char *fmt, ...);
233
207
static void tee_print_sized_data(const char *, unsigned int, unsigned int, bool);
234
208
/* The names of functions that actually do the manipulation. */
235
209
static int get_options(int argc,char **argv);
 
210
bool get_one_option(int optid, const struct my_option *opt,
 
211
                    char *argument);
236
212
static int com_quit(string *str,const char*),
237
 
           com_go(string *str,const char*), com_ego(string *str,const char*),
238
 
           com_print(string *str,const char*),
239
 
           com_help(string *str,const char*), com_clear(string *str,const char*),
240
 
           com_connect(string *str,const char*), com_status(string *str,const char*),
241
 
           com_use(string *str,const char*), com_source(string *str, const char*),
242
 
           com_rehash(string *str, const char*), com_tee(string *str, const char*),
243
 
           com_notee(string *str, const char*),
244
 
           com_prompt(string *str, const char*), com_delimiter(string *str, const char*),
245
 
           com_warnings(string *str, const char*), com_nowarnings(string *str, const char*),
246
 
           com_nopager(string *str, const char*), com_pager(string *str, const char*);
 
213
  com_go(string *str,const char*), com_ego(string *str,const char*),
 
214
  com_print(string *str,const char*),
 
215
  com_help(string *str,const char*), com_clear(string *str,const char*),
 
216
  com_connect(string *str,const char*), com_status(string *str,const char*),
 
217
  com_use(string *str,const char*), com_source(string *str, const char*),
 
218
  com_rehash(string *str, const char*), com_tee(string *str, const char*),
 
219
  com_notee(string *str, const char*), com_charset(string *str,const char*),
 
220
  com_prompt(string *str, const char*), com_delimiter(string *str, const char*),
 
221
  com_warnings(string *str, const char*), com_nowarnings(string *str, const char*),
 
222
  com_nopager(string *str, const char*), com_pager(string *str, const char*);
247
223
 
248
224
static int read_and_execute(bool interactive);
249
225
static int sql_connect(char *host,char *database,char *user,char *password,
250
 
                       uint32_t silent);
251
 
static const char *server_version_string(drizzle_con_st *con);
252
 
static int put_info(const char *str,INFO_TYPE info,uint32_t error,
 
226
                       uint silent);
 
227
static const char *server_version_string(DRIZZLE *drizzle);
 
228
static int put_info(const char *str,INFO_TYPE info,uint error,
253
229
                    const char *sql_state);
254
 
static int put_error(drizzle_con_st *con, drizzle_result_st *res);
 
230
static int put_error(DRIZZLE *drizzle);
255
231
static void safe_put_field(const char *pos,uint32_t length);
256
232
static void init_pager(void);
257
233
static void end_pager(void);
261
237
static char *get_arg(char *line, bool get_next_arg);
262
238
static void init_username(void);
263
239
static void add_int_to_prompt(int toadd);
264
 
static int get_result_width(drizzle_result_st *res);
265
 
static int get_field_disp_length(drizzle_column_st * field);
 
240
static int get_result_width(DRIZZLE_RES *res);
 
241
static int get_field_disp_length(DRIZZLE_FIELD * field);
266
242
static const char * strcont(register const char *str, register const char *set);
267
243
 
268
244
/* A structure which contains information on the commands this program
269
 
  can understand. */
 
245
   can understand. */
270
246
typedef struct {
271
247
  const char *name;        /* User printable name of the function. */
272
248
  char cmd_char;        /* msql command character */
277
253
 
278
254
 
279
255
static COMMANDS commands[] = {
280
 
  { "?",      '?', com_help,   0, N_("Synonym for `help'.") },
 
256
  { "?",      '?', com_help,   1, N_("Synonym for `help'.") },
281
257
  { "clear",  'c', com_clear,  0, N_("Clear command.")},
282
258
  { "connect",'r', com_connect,1,
283
 
    N_("Reconnect to the server. Optional arguments are db and host.")},
 
259
    N_("Reconnect to the server. Optional arguments are db and host." }),
284
260
  { "delimiter", 'd', com_delimiter,    1,
285
261
    N_("Set statement delimiter. NOTE: Takes the rest of the line as new delimiter.") },
286
262
  { "ego",    'G', com_ego,    0,
287
263
    N_("Send command to drizzle server, display result vertically.")},
288
264
  { "exit",   'q', com_quit,   0, N_("Exit drizzle. Same as quit.")},
289
265
  { "go",     'g', com_go,     0, N_("Send command to drizzle server.") },
290
 
  { "help",   'h', com_help,   0, N_("Display this help.") },
 
266
  { "help",   'h', com_help,   1, N_("Display this help.") },
291
267
  { "nopager",'n', com_nopager,0, N_("Disable pager, print to stdout.") },
292
268
  { "notee",  't', com_notee,  0, N_("Don't write into outfile.") },
293
269
  { "pager",  'P', com_pager,  1,
303
279
    N_("Set outfile [to_outfile]. Append everything into given outfile.") },
304
280
  { "use",    'u', com_use,    1,
305
281
    N_("Use another database. Takes database name as argument.") },
 
282
  { "charset",    'C', com_charset,    1,
 
283
    N_("Switch to another charset. Might be needed for processing binlog with multi-byte charsets.") },
306
284
  { "warnings", 'W', com_warnings,  0,
307
285
    N_("Show warnings after every statement.") },
308
286
  { "nowarning", 'w', com_nowarnings, 0,
393
371
  { "CROSS", 0, 0, 0, ""},
394
372
  { "CUBE", 0, 0, 0, ""},
395
373
  { "CURRENT_DATE", 0, 0, 0, ""},
 
374
  { "CURRENT_TIME", 0, 0, 0, ""},
396
375
  { "CURRENT_TIMESTAMP", 0, 0, 0, ""},
397
376
  { "CURRENT_USER", 0, 0, 0, ""},
398
377
  { "CURSOR", 0, 0, 0, ""},
471
450
  { "FULL", 0, 0, 0, ""},
472
451
  { "FULLTEXT", 0, 0, 0, ""},
473
452
  { "FUNCTION", 0, 0, 0, ""},
 
453
  { "GET_FORMAT", 0, 0, 0, ""},
474
454
  { "GLOBAL", 0, 0, 0, ""},
475
455
  { "GRANT", 0, 0, 0, ""},
476
456
  { "GRANTS", 0, 0, 0, ""},
532
512
  { "LINESTRING", 0, 0, 0, ""},
533
513
  { "LOAD", 0, 0, 0, ""},
534
514
  { "LOCAL", 0, 0, 0, ""},
 
515
  { "LOCALTIME", 0, 0, 0, ""},
535
516
  { "LOCALTIMESTAMP", 0, 0, 0, ""},
536
517
  { "LOCK", 0, 0, 0, ""},
537
518
  { "LOCKS", 0, 0, 0, ""},
628
609
  { "QUARTER", 0, 0, 0, ""},
629
610
  { "QUERY", 0, 0, 0, ""},
630
611
  { "QUICK", 0, 0, 0, ""},
 
612
  { "RAID0", 0, 0, 0, ""},
 
613
  { "RAID_CHUNKS", 0, 0, 0, ""},
 
614
  { "RAID_CHUNKSIZE", 0, 0, 0, ""},
 
615
  { "RAID_TYPE", 0, 0, 0, ""},
631
616
  { "READ", 0, 0, 0, ""},
632
617
  { "READS", 0, 0, 0, ""},
633
618
  { "REAL", 0, 0, 0, ""},
729
714
  { "TERMINATED", 0, 0, 0, ""},
730
715
  { "TEXT", 0, 0, 0, ""},
731
716
  { "THEN", 0, 0, 0, ""},
 
717
  { "TIME", 0, 0, 0, ""},
732
718
  { "TIMESTAMP", 0, 0, 0, ""},
733
719
  { "TIMESTAMPADD", 0, 0, 0, ""},
734
720
  { "TIMESTAMPDIFF", 0, 0, 0, ""},
 
721
  { "TINYINT", 0, 0, 0, ""},
735
722
  { "TINYTEXT", 0, 0, 0, ""},
736
723
  { "TO", 0, 0, 0, ""},
737
724
  { "TRAILING", 0, 0, 0, ""},
761
748
  { "USE_FRM", 0, 0, 0, ""},
762
749
  { "USING", 0, 0, 0, ""},
763
750
  { "UTC_DATE", 0, 0, 0, ""},
 
751
  { "UTC_TIME", 0, 0, 0, ""},
764
752
  { "UTC_TIMESTAMP", 0, 0, 0, ""},
765
753
  { "VALUE", 0, 0, 0, ""},
766
754
  { "VALUES", 0, 0, 0, ""},
787
775
  { "ABS", 0, 0, 0, ""},
788
776
  { "ACOS", 0, 0, 0, ""},
789
777
  { "ADDDATE", 0, 0, 0, ""},
 
778
  { "ADDTIME", 0, 0, 0, ""},
790
779
  { "AES_ENCRYPT", 0, 0, 0, ""},
791
780
  { "AES_DECRYPT", 0, 0, 0, ""},
792
781
  { "AREA", 0, 0, 0, ""},
799
788
  { "ATAN2", 0, 0, 0, ""},
800
789
  { "BENCHMARK", 0, 0, 0, ""},
801
790
  { "BIN", 0, 0, 0, ""},
 
791
  { "BIT_COUNT", 0, 0, 0, ""},
802
792
  { "BIT_OR", 0, 0, 0, ""},
803
793
  { "BIT_AND", 0, 0, 0, ""},
804
794
  { "BIT_XOR", 0, 0, 0, ""},
805
795
  { "CAST", 0, 0, 0, ""},
806
796
  { "CEIL", 0, 0, 0, ""},
807
797
  { "CEILING", 0, 0, 0, ""},
 
798
  { "BIT_LENGTH", 0, 0, 0, ""},
808
799
  { "CENTROID", 0, 0, 0, ""},
809
800
  { "CHAR_LENGTH", 0, 0, 0, ""},
810
801
  { "CHARACTER_LENGTH", 0, 0, 0, ""},
822
813
  { "CRC32", 0, 0, 0, ""},
823
814
  { "CROSSES", 0, 0, 0, ""},
824
815
  { "CURDATE", 0, 0, 0, ""},
 
816
  { "CURTIME", 0, 0, 0, ""},
825
817
  { "DATE_ADD", 0, 0, 0, ""},
826
818
  { "DATEDIFF", 0, 0, 0, ""},
827
819
  { "DATE_FORMAT", 0, 0, 0, ""},
891
883
  { "LTRIM", 0, 0, 0, ""},
892
884
  { "MAKE_SET", 0, 0, 0, ""},
893
885
  { "MAKEDATE", 0, 0, 0, ""},
 
886
  { "MAKETIME", 0, 0, 0, ""},
894
887
  { "MASTER_POS_WAIT", 0, 0, 0, ""},
895
888
  { "MAX", 0, 0, 0, ""},
896
889
  { "MBRCONTAINS", 0, 0, 0, ""},
947
940
  { "ROW_COUNT", 0, 0, 0, ""},
948
941
  { "RPAD", 0, 0, 0, ""},
949
942
  { "RTRIM", 0, 0, 0, ""},
 
943
  { "SEC_TO_TIME", 0, 0, 0, ""},
950
944
  { "SESSION_USER", 0, 0, 0, ""},
951
945
  { "SUBDATE", 0, 0, 0, ""},
952
946
  { "SIGN", 0, 0, 0, ""},
968
962
  { "SUBSTR", 0, 0, 0, ""},
969
963
  { "SUBSTRING", 0, 0, 0, ""},
970
964
  { "SUBSTRING_INDEX", 0, 0, 0, ""},
 
965
  { "SUBTIME", 0, 0, 0, ""},
971
966
  { "SUM", 0, 0, 0, ""},
972
967
  { "SYSDATE", 0, 0, 0, ""},
973
968
  { "SYSTEM_USER", 0, 0, 0, ""},
974
969
  { "TAN", 0, 0, 0, ""},
975
970
  { "TIME_FORMAT", 0, 0, 0, ""},
 
971
  { "TIME_TO_SEC", 0, 0, 0, ""},
 
972
  { "TIMEDIFF", 0, 0, 0, ""},
976
973
  { "TO_DAYS", 0, 0, 0, ""},
977
974
  { "TOUCHES", 0, 0, 0, ""},
978
975
  { "TRIM", 0, 0, 0, ""},
1009
1006
static bool add_line(string *buffer,char *line,char *in_string,
1010
1007
                     bool *ml_comment);
1011
1008
static void remove_cntrl(string *buffer);
1012
 
static void print_table_data(drizzle_result_st *result);
1013
 
static void print_tab_data(drizzle_result_st *result);
1014
 
static void print_table_data_vertically(drizzle_result_st *result);
1015
 
static void print_warnings(uint32_t error_code);
 
1009
static void print_table_data(DRIZZLE_RES *result);
 
1010
static void print_tab_data(DRIZZLE_RES *result);
 
1011
static void print_table_data_vertically(DRIZZLE_RES *result);
 
1012
static void print_warnings(void);
1016
1013
static uint32_t start_timer(void);
1017
1014
static void end_timer(uint32_t start_time,char *buff);
1018
1015
static void drizzle_end_timer(uint32_t start_time,char *buff);
1019
1016
static void nice_time(double sec,char *buff,bool part_second);
1020
 
extern "C" void drizzle_end(int sig);
1021
 
extern "C" void handle_sigint(int sig);
 
1017
extern sig_handler drizzle_end(int sig);
 
1018
extern sig_handler handle_sigint(int sig);
1022
1019
#if defined(HAVE_TERMIOS_H) && defined(GWINSZ_IN_SYS_IOCTL)
1023
 
static void window_resize(int sig);
 
1020
static sig_handler window_resize(int sig);
1024
1021
#endif
1025
1022
 
1026
 
/**
1027
 
  Shutdown the server that we are currently connected to.
1028
 
 
1029
 
  @retval
1030
 
  true success
1031
 
  @retval
1032
 
  false failure
1033
 
*/
1034
 
static bool server_shutdown(void)
1035
 
{
1036
 
  drizzle_result_st result;
1037
 
  drizzle_return_t ret;
1038
 
 
1039
 
  if (verbose)
1040
 
  {
1041
 
    printf("shutting down drizzled");
1042
 
    if (opt_drizzle_port > 0)
1043
 
      printf(" on port %d", opt_drizzle_port);
1044
 
    printf("... ");
1045
 
  }
1046
 
 
1047
 
  if (drizzle_shutdown(&con, &result, DRIZZLE_SHUTDOWN_DEFAULT,
1048
 
                       &ret) == NULL || ret != DRIZZLE_RETURN_OK)
1049
 
  {
1050
 
    if (ret == DRIZZLE_RETURN_ERROR_CODE)
1051
 
    {
1052
 
      fprintf(stderr, "shutdown failed; error: '%s'",
1053
 
              drizzle_result_error(&result));
1054
 
      drizzle_result_free(&result);
1055
 
    }
1056
 
    else
1057
 
    {
1058
 
      fprintf(stderr, "shutdown failed; error: '%s'",
1059
 
              drizzle_con_error(&con));
1060
 
    }
1061
 
    return false;
1062
 
  }
1063
 
 
1064
 
  drizzle_result_free(&result);
1065
 
 
1066
 
  if (verbose)
1067
 
    printf("done\n");
1068
 
 
1069
 
  return true;
1070
 
}
1071
 
 
1072
 
/**
1073
 
  Ping the server that we are currently connected to.
1074
 
 
1075
 
  @retval
1076
 
  true success
1077
 
  @retval
1078
 
  false failure
1079
 
*/
1080
 
static bool server_ping(void)
1081
 
{
1082
 
  drizzle_result_st result;
1083
 
  drizzle_return_t ret;
1084
 
 
1085
 
  if (drizzle_ping(&con, &result, &ret) != NULL && ret == DRIZZLE_RETURN_OK)
1086
 
  {
1087
 
    if (opt_silent < 2)
1088
 
      printf("drizzled is alive\n");
1089
 
  }
1090
 
  else
1091
 
  {
1092
 
    if (ret == DRIZZLE_RETURN_ERROR_CODE)
1093
 
    {
1094
 
      fprintf(stderr, "ping failed; error: '%s'",
1095
 
              drizzle_result_error(&result));
1096
 
      drizzle_result_free(&result);
1097
 
    }
1098
 
    else
1099
 
    {
1100
 
      fprintf(stderr, "drizzled won't answer to ping, error: '%s'",
1101
 
              drizzle_con_error(&con));
1102
 
    }
1103
 
    return false;
1104
 
  }
1105
 
  drizzle_result_free(&result);
1106
 
  return true;
1107
 
}
1108
 
 
1109
 
/**
1110
 
  Execute command(s) specified by the user.
1111
 
 
1112
 
  @param error  error status of command execution.
1113
 
  If an error had occurred, this variable will be set
1114
 
  to 1 whereas on success, it shall be set to 0. This
1115
 
  value will be supplied to the exit() function used
1116
 
  by the caller.
1117
 
 
1118
 
  @retval
1119
 
  false no commands were executed
1120
 
  @retval
1121
 
  true  at least one command was executed
1122
 
*/
1123
 
static bool execute_commands(int *error)
1124
 
{
1125
 
  bool executed= false;
1126
 
  *error= 0;
1127
 
 
1128
 
  if (opt_ping)
1129
 
  {
1130
 
    if (server_ping() == false)
1131
 
      *error= 1;
1132
 
    executed= true;
1133
 
  }
1134
 
 
1135
 
  if (opt_shutdown)
1136
 
  {
1137
 
    if (server_shutdown() == false)
1138
 
      *error= 1;
1139
 
    executed= true;
1140
 
  }
1141
 
  return executed;
1142
 
}
1143
 
 
1144
1023
int main(int argc,char *argv[])
1145
1024
{
 
1025
  char buff[80];
 
1026
 
1146
1027
#if defined(ENABLE_NLS)
1147
1028
# if defined(HAVE_LOCALE_H)
1148
1029
  setlocale(LC_ALL, "");
1153
1034
 
1154
1035
  MY_INIT(argv[0]);
1155
1036
  delimiter_str= delimiter;
1156
 
  default_prompt= strdup(getenv("DRIZZLE_PS1") ?
1157
 
                         getenv("DRIZZLE_PS1") :
1158
 
                         "drizzle> ");
1159
 
 
1160
 
  if (default_prompt == NULL)
1161
 
  {
1162
 
    fprintf(stderr, _("Memory allocation error while constructing initial "
1163
 
                      "prompt. Aborting.\n"));
1164
 
    exit(ENOMEM);
1165
 
  }
1166
 
  current_prompt= strdup(default_prompt);
1167
 
  if (current_prompt == NULL)
1168
 
  {
1169
 
    fprintf(stderr, _("Memory allocation error while constructing initial "
1170
 
                      "prompt. Aborting.\n"));
1171
 
    exit(ENOMEM);
1172
 
  }
 
1037
  default_prompt= my_strdup(getenv("DRIZZLE_PS1") ?
 
1038
                            getenv("DRIZZLE_PS1") :
 
1039
                            "drizzle>> ", MYF(0));
 
1040
  current_prompt= my_strdup(default_prompt, MYF(0));
1173
1041
  processed_prompt= new string();
1174
1042
  processed_prompt->reserve(32);
1175
1043
 
1176
1044
  prompt_counter=0;
1177
1045
 
1178
1046
  outfile[0]=0;      // no (default) outfile
1179
 
  strcpy(pager, "stdout");  // the default, if --pager wasn't given
 
1047
  stpcpy(pager, "stdout");  // the default, if --pager wasn't given
1180
1048
  {
1181
1049
    char *tmp=getenv("PAGER");
1182
1050
    if (tmp && strlen(tmp))
1183
1051
    {
1184
1052
      default_pager_set= 1;
1185
 
      strcpy(default_pager, tmp);
 
1053
      stpcpy(default_pager, tmp);
1186
1054
    }
1187
1055
  }
1188
1056
  if (!isatty(0) || !isatty(1))
1208
1076
      close(stdout_fileno_copy);             /* Clean up dup(). */
1209
1077
  }
1210
1078
 
1211
 
  internal::load_defaults("drizzle",load_default_groups,&argc,&argv);
 
1079
  load_defaults("my",load_default_groups,&argc,&argv);
1212
1080
  defaults_argv=argv;
1213
1081
  if (get_options(argc, (char **) argv))
1214
1082
  {
1215
 
    internal::free_defaults(defaults_argv);
1216
 
    internal::my_end();
1217
 
    exit(1);
1218
 
  }
1219
 
 
 
1083
    free_defaults(defaults_argv);
 
1084
    my_end(0);
 
1085
    exit(1);
 
1086
  }
 
1087
  if (status.batch && !status.line_buff &&
 
1088
      !(status.line_buff=batch_readline_init(opt_max_allowed_packet+512,stdin)))
 
1089
  {
 
1090
    free_defaults(defaults_argv);
 
1091
    my_end(0);
 
1092
    exit(1);
 
1093
  }
 
1094
  completion_hash_init(&ht, 128);
 
1095
  init_alloc_root(&hash_mem_root, 16384, 0);
1220
1096
  memset(&drizzle, 0, sizeof(drizzle));
1221
1097
  if (sql_connect(current_host,current_db,current_user,opt_password,
1222
1098
                  opt_silent))
1225
1101
    status.exit_status= 1;
1226
1102
    drizzle_end(-1);
1227
1103
  }
1228
 
 
1229
 
  int command_error;
1230
 
  if (execute_commands(&command_error) != false)
1231
 
  {
1232
 
    /* we've executed a command so exit before we go into readline mode */
1233
 
    internal::free_defaults(defaults_argv);
1234
 
    internal::my_end();
1235
 
    exit(command_error);
1236
 
  }
1237
 
 
1238
 
  if (status.batch && !status.line_buff)
1239
 
  {
1240
 
    status.line_buff= new(std::nothrow) LineBuffer(opt_max_input_line, stdin);
1241
 
    if (status.line_buff == NULL)
1242
 
    {
1243
 
      internal::free_defaults(defaults_argv);
1244
 
      internal::my_end();
1245
 
      exit(1);
1246
 
    }
1247
 
  }
1248
 
 
1249
1104
  if (!status.batch)
1250
1105
    ignore_errors=1;        // Don't abort monitor
1251
1106
 
1267
1122
 
1268
1123
  glob_buffer= new string();
1269
1124
  glob_buffer->reserve(512);
 
1125
  
 
1126
  char * output_buff= (char *)malloc(512);
 
1127
  memset(output_buff, '\0', 512);
1270
1128
 
1271
 
  ostringstream output_buff;
1272
 
  output_buff << _("Your Drizzle connection id is ");
1273
 
  output_buff << drizzle_con_thread_id(&con);
1274
 
  output_buff << "\n";
1275
 
  output_buff << _("Server version: ");
1276
 
  output_buff << server_version_string(&con) << "\n";
1277
 
  put_info(output_buff.str().c_str(), INFO_INFO, 0, 0);
 
1129
  sprintf(output_buff,
 
1130
          _("Your Drizzle connection id is %u\nServer version: %s\n"),
 
1131
          drizzle_thread_id(&drizzle),
 
1132
          server_version_string(&drizzle));
 
1133
  put_info(output_buff, INFO_INFO, 0, 0);
1278
1134
 
1279
1135
  initialize_readline(current_prompt);
1280
1136
  if (!status.batch && !quick)
1284
1140
      histfile= strdup(getenv("DRIZZLE_HISTFILE"));
1285
1141
    else if (getenv("HOME"))
1286
1142
    {
1287
 
      size_t histfile_size = strlen(getenv("HOME")) +
1288
 
        strlen("/.drizzle_history") + 2;
1289
 
      histfile=(char*) malloc(histfile_size);
 
1143
      histfile=(char*) my_malloc((uint) strlen(getenv("HOME"))
 
1144
                                 + (uint) strlen("/.drizzle_history")+2,
 
1145
                                 MYF(MY_WME));
1290
1146
      if (histfile)
1291
 
        snprintf(histfile, histfile_size, "%s/.drizzle_history",getenv("HOME"));
 
1147
        sprintf(histfile,"%s/.drizzle_history",getenv("HOME"));
1292
1148
      char link_name[FN_REFLEN];
1293
 
      ssize_t sym_link_size= readlink(histfile,link_name,FN_REFLEN-1);
1294
 
      if (sym_link_size >= 0)
 
1149
      if (my_readlink(link_name, histfile, 0) == 0 &&
 
1150
          strncmp(link_name, "/dev/null", 10) == 0)
1295
1151
      {
1296
 
        link_name[sym_link_size]= '\0';
1297
 
        if (strncmp(link_name, "/dev/null", 10) == 0)
1298
 
        {
1299
 
          /* The .drizzle_history file is a symlink to /dev/null, don't use it */
1300
 
          free(histfile);
1301
 
          histfile= 0;
1302
 
        }
 
1152
        /* The .drizzle_history file is a symlink to /dev/null, don't use it */
 
1153
        my_free(histfile, MYF(MY_ALLOW_ZERO_PTR));
 
1154
        histfile= 0;
1303
1155
      }
1304
1156
    }
1305
1157
    if (histfile)
1307
1159
      if (verbose)
1308
1160
        tee_fprintf(stdout, _("Reading history-file %s\n"),histfile);
1309
1161
      read_history(histfile);
1310
 
      size_t histfile_tmp_size = strlen(histfile) + 5;
1311
 
      if (!(histfile_tmp= (char*) malloc(histfile_tmp_size)))
 
1162
      if (!(histfile_tmp= (char*) my_malloc((uint) strlen(histfile) + 5,
 
1163
                                            MYF(MY_WME))))
1312
1164
      {
1313
1165
        fprintf(stderr, _("Couldn't allocate memory for temp histfile!\n"));
1314
1166
        exit(1);
1315
1167
      }
1316
 
      snprintf(histfile_tmp, histfile_tmp_size, "%s.TMP", histfile);
 
1168
      sprintf(histfile_tmp, "%s.TMP", histfile);
1317
1169
    }
1318
1170
  }
 
1171
  sprintf(buff, "%s",
 
1172
          _("Type 'help;' or '\\h' for help. Type '\\c' to clear the buffer.\n"));
1319
1173
 
1320
 
  put_info(_("Type 'help;' or '\\h' for help. "
1321
 
             "Type '\\c' to clear the buffer.\n"),INFO_INFO,0,0);
 
1174
  put_info(buff,INFO_INFO,0,0);
1322
1175
  status.exit_status= read_and_execute(!status.batch);
1323
1176
  if (opt_outfile)
1324
1177
    end_tee();
1327
1180
  return(0);        // Keep compiler happy
1328
1181
}
1329
1182
 
1330
 
void drizzle_end(int sig)
 
1183
sig_handler drizzle_end(int sig)
1331
1184
{
1332
 
  drizzle_con_free(&con);
1333
 
  drizzle_free(&drizzle);
 
1185
  drizzle_close(&drizzle);
1334
1186
  if (!status.batch && !quick && histfile)
1335
1187
  {
1336
1188
    /* write-history */
1337
1189
    if (verbose)
1338
1190
      tee_fprintf(stdout, _("Writing history-file %s\n"),histfile);
1339
1191
    if (!write_history(histfile_tmp))
1340
 
      internal::my_rename(histfile_tmp, histfile, MYF(MY_WME));
 
1192
      my_rename(histfile_tmp, histfile, MYF(MY_WME));
1341
1193
  }
1342
 
  delete status.line_buff;
1343
 
  status.line_buff= 0;
 
1194
  batch_readline_end(status.line_buff);
 
1195
  completion_hash_free(&ht);
 
1196
  free_root(&hash_mem_root,MYF(0));
1344
1197
 
1345
1198
  if (sig >= 0)
1346
1199
    put_info(sig ? _("Aborted") : _("Bye"), INFO_RESULT,0,0);
1348
1201
    delete glob_buffer;
1349
1202
  if (processed_prompt)
1350
1203
    delete processed_prompt;
1351
 
  free(opt_password);
1352
 
  free(histfile);
1353
 
  free(histfile_tmp);
1354
 
  free(current_db);
1355
 
  free(current_host);
1356
 
  free(current_user);
1357
 
  free(full_username);
1358
 
  free(part_username);
1359
 
  free(default_prompt);
1360
 
  free(current_prompt);
1361
 
  internal::free_defaults(defaults_argv);
1362
 
  internal::my_end();
 
1204
  my_free(opt_password,MYF(MY_ALLOW_ZERO_PTR));
 
1205
  my_free(opt_drizzle_unix_port,MYF(MY_ALLOW_ZERO_PTR));
 
1206
  my_free(histfile,MYF(MY_ALLOW_ZERO_PTR));
 
1207
  my_free(histfile_tmp,MYF(MY_ALLOW_ZERO_PTR));
 
1208
  my_free(current_db,MYF(MY_ALLOW_ZERO_PTR));
 
1209
  my_free(current_host,MYF(MY_ALLOW_ZERO_PTR));
 
1210
  my_free(current_user,MYF(MY_ALLOW_ZERO_PTR));
 
1211
  my_free(full_username,MYF(MY_ALLOW_ZERO_PTR));
 
1212
  my_free(part_username,MYF(MY_ALLOW_ZERO_PTR));
 
1213
  my_free(default_prompt,MYF(MY_ALLOW_ZERO_PTR));
 
1214
  my_free(current_prompt,MYF(MY_ALLOW_ZERO_PTR));
 
1215
  drizzle_server_end();
 
1216
  free_defaults(defaults_argv);
 
1217
  my_end(my_end_arg);
1363
1218
  exit(status.exit_status);
1364
1219
}
1365
1220
 
1369
1224
  If query is in process, kill query
1370
1225
  no query in process, terminate like previous behavior
1371
1226
*/
1372
 
extern "C"
1373
 
void handle_sigint(int sig)
 
1227
sig_handler handle_sigint(int sig)
1374
1228
{
1375
1229
  char kill_buffer[40];
1376
 
  drizzle_con_st kill_drizzle;
1377
 
  drizzle_result_st res;
1378
 
  drizzle_return_t ret;
 
1230
  DRIZZLE *kill_drizzle= NULL;
1379
1231
 
1380
1232
  /* terminate if no query being executed, or we already tried interrupting */
1381
1233
  if (!executing_query || interrupted_query) {
1382
1234
    goto err;
1383
1235
  }
1384
1236
 
1385
 
  if (drizzle_con_add_tcp(&drizzle, &kill_drizzle, current_host,
1386
 
                          opt_drizzle_port, current_user, opt_password, NULL,
1387
 
                          opt_mysql ? DRIZZLE_CON_MYSQL : DRIZZLE_CON_NONE) == NULL)
 
1237
  kill_drizzle= drizzle_create(kill_drizzle);
 
1238
  if (!drizzle_connect(kill_drizzle,current_host, current_user, opt_password,
 
1239
                          "", opt_drizzle_port, opt_drizzle_unix_port,0))
1388
1240
  {
1389
1241
    goto err;
1390
1242
  }
1391
1243
 
1392
1244
  /* kill_buffer is always big enough because max length of %lu is 15 */
1393
 
  snprintf(kill_buffer, sizeof(kill_buffer), "KILL /*!50000 QUERY */ %u",
1394
 
           drizzle_con_thread_id(&con));
1395
 
 
1396
 
  if (drizzle_query_str(&kill_drizzle, &res, kill_buffer, &ret) != NULL)
1397
 
    drizzle_result_free(&res);
1398
 
 
1399
 
  drizzle_con_free(&kill_drizzle);
 
1245
  sprintf(kill_buffer, "KILL /*!50000 QUERY */ %u", drizzle_thread_id(&drizzle));
 
1246
  drizzle_real_query(kill_drizzle, kill_buffer, strlen(kill_buffer));
 
1247
  drizzle_close(kill_drizzle);
1400
1248
  tee_fprintf(stdout, _("Query aborted by Ctrl+C\n"));
1401
1249
 
1402
1250
  interrupted_query= 1;
1409
1257
 
1410
1258
 
1411
1259
#if defined(HAVE_TERMIOS_H) && defined(GWINSZ_IN_SYS_IOCTL)
1412
 
void window_resize(int)
 
1260
sig_handler window_resize(int sig __attribute__((unused)))
1413
1261
{
1414
1262
  struct winsize window_size;
1415
1263
 
1418
1266
}
1419
1267
#endif
1420
1268
 
1421
 
static struct option my_long_options[] =
 
1269
static struct my_option my_long_options[] =
1422
1270
{
1423
1271
  {"help", '?', N_("Display this help and exit."), 0, 0, 0, GET_NO_ARG, NO_ARG, 0,
1424
 
    0, 0, 0, 0, 0},
 
1272
   0, 0, 0, 0, 0},
1425
1273
  {"help", 'I', N_("Synonym for -?"), 0, 0, 0, GET_NO_ARG, NO_ARG, 0,
1426
 
    0, 0, 0, 0, 0},
 
1274
   0, 0, 0, 0, 0},
1427
1275
  {"auto-rehash", OPT_AUTO_REHASH,
1428
 
    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."),
1429
 
    (char**) &opt_rehash, (char**) &opt_rehash, 0, GET_BOOL, NO_ARG, 1, 0, 0, 0,
1430
 
    0, 0},
 
1276
   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."),
 
1277
   (char**) &opt_rehash, (char**) &opt_rehash, 0, GET_BOOL, NO_ARG, 1, 0, 0, 0,
 
1278
   0, 0},
1431
1279
  {"no-auto-rehash", 'A',
1432
 
    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."),
1433
 
    0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
 
1280
   N_("No automatic rehashing. One has to use 'rehash' to get table and field completion. This gives a quicker start of DRIZZLE and disables rehashing on reconnect. WARNING: options deprecated; use --disable-auto-rehash instead."),
 
1281
   0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
1434
1282
  {"auto-vertical-output", OPT_AUTO_VERTICAL_OUTPUT,
1435
 
    N_("Automatically switch to vertical output mode if the result is wider than the terminal width."),
1436
 
    (char**) &auto_vertical_output, (char**) &auto_vertical_output, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
 
1283
   N_("Automatically switch to vertical output mode if the result is wider than the terminal width."),
 
1284
   (char**) &auto_vertical_output, (char**) &auto_vertical_output, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
1437
1285
  {"batch", 'B',
1438
 
    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},
 
1286
   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},
 
1287
  {"character-sets-dir", OPT_CHARSETS_DIR,
 
1288
   N_("Directory where character sets are."), (char**) &charsets_dir,
 
1289
   (char**) &charsets_dir, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
1439
1290
  {"column-type-info", OPT_COLUMN_TYPES, N_("Display column type information."),
1440
 
    (char**) &column_types_flag, (char**) &column_types_flag,
1441
 
    0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
 
1291
   (char**) &column_types_flag, (char**) &column_types_flag,
 
1292
   0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
1442
1293
  {"comments", 'c', N_("Preserve comments. Send comments to the server. The default is --skip-comments (discard comments), enable with --comments"),
1443
 
    (char**) &preserve_comments, (char**) &preserve_comments,
1444
 
    0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
 
1294
   (char**) &preserve_comments, (char**) &preserve_comments,
 
1295
   0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
1445
1296
  {"compress", 'C', N_("Use compression in server/client protocol."),
1446
 
    (char**) &opt_compress, (char**) &opt_compress, 0, GET_BOOL, NO_ARG, 0, 0, 0,
1447
 
    0, 0, 0},
 
1297
   (char**) &opt_compress, (char**) &opt_compress, 0, GET_BOOL, NO_ARG, 0, 0, 0,
 
1298
   0, 0, 0},
 
1299
  {"debug-check", OPT_DEBUG_CHECK, N_("Check memory and open file usage at exit ."),
 
1300
   (char**) &debug_check_flag, (char**) &debug_check_flag, 0,
 
1301
   GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
 
1302
  {"debug-info", 'T', N_("Print some debug info at exit."), (char**) &debug_info_flag,
 
1303
   (char**) &debug_info_flag, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
1448
1304
  {"database", 'D', N_("Database to use."), (char**) &current_db,
1449
 
    (char**) &current_db, 0, GET_STR_ALLOC, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
1305
   (char**) &current_db, 0, GET_STR_ALLOC, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
1450
1306
  {"default-character-set", OPT_DEFAULT_CHARSET,
1451
 
    N_("(not used)"), 0,
1452
 
    0, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
1307
   N_("Set the default character set."), (char**) &default_charset,
 
1308
   (char**) &default_charset, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
1453
1309
  {"delimiter", OPT_DELIMITER, N_("Delimiter to be used."), (char**) &delimiter_str,
1454
 
    (char**) &delimiter_str, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
1310
   (char**) &delimiter_str, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
1455
1311
  {"execute", 'e', N_("Execute command and quit. (Disables --force and history file)"), 0,
1456
 
    0, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
1312
   0, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
1457
1313
  {"vertical", 'E', N_("Print the output of a query (rows) vertically."),
1458
 
    (char**) &vertical, (char**) &vertical, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0,
1459
 
    0},
 
1314
   (char**) &vertical, (char**) &vertical, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0,
 
1315
   0},
1460
1316
  {"force", 'f', N_("Continue even if we get an sql error."),
1461
 
    (char**) &ignore_errors, (char**) &ignore_errors, 0, GET_BOOL, NO_ARG, 0, 0,
1462
 
    0, 0, 0, 0},
 
1317
   (char**) &ignore_errors, (char**) &ignore_errors, 0, GET_BOOL, NO_ARG, 0, 0,
 
1318
   0, 0, 0, 0},
1463
1319
  {"named-commands", 'G',
1464
 
    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."),
1465
 
    (char**) &named_cmds, (char**) &named_cmds, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0,
1466
 
    0, 0},
 
1320
   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."),
 
1321
   (char**) &named_cmds, (char**) &named_cmds, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0,
 
1322
   0, 0},
1467
1323
  {"no-named-commands", 'g',
1468
 
    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."),
1469
 
    0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
 
1324
   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."),
 
1325
   0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
1470
1326
  {"ignore-spaces", 'i', N_("Ignore space after function names."), 0, 0, 0,
1471
 
    GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
 
1327
   GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
1472
1328
  {"local-infile", OPT_LOCAL_INFILE, N_("Enable/disable LOAD DATA LOCAL INFILE."),
1473
 
    (char**) &opt_local_infile,
1474
 
    (char**) &opt_local_infile, 0, GET_BOOL, OPT_ARG, 0, 0, 0, 0, 0, 0},
 
1329
   (char**) &opt_local_infile,
 
1330
   (char**) &opt_local_infile, 0, GET_BOOL, OPT_ARG, 0, 0, 0, 0, 0, 0},
1475
1331
  {"no-beep", 'b', N_("Turn off beep on error."), (char**) &opt_nobeep,
1476
 
    (char**) &opt_nobeep, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
 
1332
   (char**) &opt_nobeep, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
1477
1333
  {"host", 'h', N_("Connect to host."), (char**) &current_host,
1478
 
    (char**) &current_host, 0, GET_STR_ALLOC, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
1334
   (char**) &current_host, 0, GET_STR_ALLOC, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
1479
1335
  {"line-numbers", OPT_LINE_NUMBERS, N_("Write line numbers for errors."),
1480
 
    (char**) &line_numbers, (char**) &line_numbers, 0, GET_BOOL,
1481
 
    NO_ARG, 1, 0, 0, 0, 0, 0},
 
1336
   (char**) &line_numbers, (char**) &line_numbers, 0, GET_BOOL,
 
1337
   NO_ARG, 1, 0, 0, 0, 0, 0},
1482
1338
  {"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,
1483
 
    NO_ARG, 0, 0, 0, 0, 0, 0},
 
1339
   NO_ARG, 0, 0, 0, 0, 0, 0},
1484
1340
  {"unbuffered", 'n', N_("Flush buffer after each query."), (char**) &unbuffered,
1485
 
    (char**) &unbuffered, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
 
1341
   (char**) &unbuffered, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
1486
1342
  {"column-names", OPT_COLUMN_NAMES, N_("Write column names in results."),
1487
 
    (char**) &column_names, (char**) &column_names, 0, GET_BOOL,
1488
 
    NO_ARG, 1, 0, 0, 0, 0, 0},
 
1343
   (char**) &column_names, (char**) &column_names, 0, GET_BOOL,
 
1344
   NO_ARG, 1, 0, 0, 0, 0, 0},
1489
1345
  {"skip-column-names", 'N',
1490
 
    N_("Don't write column names in results. WARNING: -N is deprecated, use long version of this options instead."),
1491
 
    0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
 
1346
   N_("Don't write column names in results. WARNING: -N is deprecated, use long version of this options instead."),
 
1347
   0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
1492
1348
  {"set-variable", 'O',
1493
 
    N_("Change the value of a variable. Please note that this option is deprecated; you can set variables directly with --variable-name=value."),
1494
 
    0, 0, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
1349
   N_("Change the value of a variable. Please note that this option is deprecated; you can set variables directly with --variable-name=value."),
 
1350
   0, 0, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
1495
1351
  {"sigint-ignore", OPT_SIGINT_IGNORE, N_("Ignore SIGINT (CTRL-C)"),
1496
 
    (char**) &opt_sigint_ignore,  (char**) &opt_sigint_ignore, 0, GET_BOOL,
1497
 
    NO_ARG, 0, 0, 0, 0, 0, 0},
 
1352
   (char**) &opt_sigint_ignore,  (char**) &opt_sigint_ignore, 0, GET_BOOL,
 
1353
   NO_ARG, 0, 0, 0, 0, 0, 0},
1498
1354
  {"one-database", 'o',
1499
 
    N_("Only update the default database. This is useful for skipping updates to other database in the update log."),
1500
 
    0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
 
1355
   N_("Only update the default database. This is useful for skipping updates to other database in the update log."),
 
1356
   0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
1501
1357
  {"pager", OPT_PAGER,
1502
 
    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."),
1503
 
    0, 0, 0, GET_STR, OPT_ARG, 0, 0, 0, 0, 0, 0},
 
1358
   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."),
 
1359
   0, 0, 0, GET_STR, OPT_ARG, 0, 0, 0, 0, 0, 0},
1504
1360
  {"no-pager", OPT_NOPAGER,
1505
 
    N_("Disable pager and print to stdout. See interactive help (\\h) also. WARNING: option deprecated; use --disable-pager instead."),
1506
 
    0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
1507
 
  {"password", 'P',
1508
 
    N_("Password to use when connecting to server. If password is not given it's asked from the tty."),
1509
 
    0, 0, 0, GET_STR, OPT_ARG, 0, 0, 0, 0, 0, 0},
1510
 
  {"port", 'p', N_("Port number to use for connection or 0 for default to, in order of preference, drizzle.cnf, $DRIZZLE_TCP_PORT, ")
1511
 
    N_("built-in default") " (" STRINGIFY_ARG(DRIZZLE_PORT) ").",
1512
 
    0, 0, 0, GET_UINT, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
1361
   N_("Disable pager and print to stdout. See interactive help (\\h) also. WARNING: option deprecated; use --disable-pager instead."),
 
1362
   0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
 
1363
  {"password", 'p',
 
1364
   N_("Password to use when connecting to server. If password is not given it's asked from the tty."),
 
1365
   0, 0, 0, GET_STR, OPT_ARG, 0, 0, 0, 0, 0, 0},
 
1366
  {"port", 'P', N_("Port number to use for connection or 0 for default to, in order of preference, my.cnf, $DRIZZLE_TCP_PORT, ")
 
1367
   N_("built-in default") " (" STRINGIFY_ARG(DRIZZLE_PORT) ").",
 
1368
   (char**) &opt_drizzle_port,
 
1369
   (char**) &opt_drizzle_port, 0, GET_UINT, REQUIRED_ARG, 0, 0, 0, 0, 0,  0},
1513
1370
  {"prompt", OPT_PROMPT, N_("Set the drizzle prompt to this value."),
1514
 
    (char**) &current_prompt, (char**) &current_prompt, 0, GET_STR_ALLOC,
1515
 
    REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
1371
   (char**) &current_prompt, (char**) &current_prompt, 0, GET_STR_ALLOC,
 
1372
   REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
1373
  {"protocol", OPT_DRIZZLE_PROTOCOL, N_("The protocol of connection (tcp,socket,pipe,memory)."),
 
1374
   0, 0, 0, GET_STR,  REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
1516
1375
  {"quick", 'q',
1517
 
    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."),
1518
 
    (char**) &quick, (char**) &quick, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
 
1376
   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."),
 
1377
   (char**) &quick, (char**) &quick, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
1519
1378
  {"raw", 'r', N_("Write fields without conversion. Used with --batch."),
1520
 
    (char**) &opt_raw_data, (char**) &opt_raw_data, 0, GET_BOOL, NO_ARG, 0, 0, 0,
1521
 
    0, 0, 0},
 
1379
   (char**) &opt_raw_data, (char**) &opt_raw_data, 0, GET_BOOL, NO_ARG, 0, 0, 0,
 
1380
   0, 0, 0},
1522
1381
  {"reconnect", OPT_RECONNECT, N_("Reconnect if the connection is lost. Disable with --disable-reconnect. This option is enabled by default."),
1523
 
    (char**) &opt_reconnect, (char**) &opt_reconnect, 0, GET_BOOL, NO_ARG, 1, 0, 0, 0, 0, 0},
1524
 
  {"shutdown", OPT_SHUTDOWN, N_("Shutdown the server."),
1525
 
    (char**) &opt_shutdown, (char**) &opt_shutdown, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
 
1382
   (char**) &opt_reconnect, (char**) &opt_reconnect, 0, GET_BOOL, NO_ARG, 1, 0, 0, 0, 0, 0},
1526
1383
  {"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,
1527
 
    0, 0},
 
1384
   0, 0},
 
1385
  {"socket", 'S', N_("Socket file to use for connection."),
 
1386
   (char**) &opt_drizzle_unix_port, (char**) &opt_drizzle_unix_port, 0, GET_STR_ALLOC,
 
1387
   REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
1528
1388
  {"table", 't', N_("Output in table format."), (char**) &output_tables,
1529
 
    (char**) &output_tables, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
 
1389
   (char**) &output_tables, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
1530
1390
  {"tee", OPT_TEE,
1531
 
    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."),
1532
 
    0, 0, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
1391
   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."),
 
1392
   0, 0, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
1533
1393
  {"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,
1534
 
    NO_ARG, 0, 0, 0, 0, 0, 0},
 
1394
   NO_ARG, 0, 0, 0, 0, 0, 0},
 
1395
#ifndef DONT_ALLOW_USER_CHANGE
1535
1396
  {"user", 'u', N_("User for login if not current user."), (char**) &current_user,
1536
 
    (char**) &current_user, 0, GET_STR_ALLOC, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
1397
   (char**) &current_user, 0, GET_STR_ALLOC, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
1398
#endif
1537
1399
  {"safe-updates", 'U', N_("Only allow UPDATE and DELETE that uses keys."),
1538
 
    (char**) &safe_updates, (char**) &safe_updates, 0, GET_BOOL, NO_ARG, 0, 0,
1539
 
    0, 0, 0, 0},
 
1400
   (char**) &safe_updates, (char**) &safe_updates, 0, GET_BOOL, NO_ARG, 0, 0,
 
1401
   0, 0, 0, 0},
1540
1402
  {"i-am-a-dummy", 'U', N_("Synonym for option --safe-updates, -U."),
1541
 
    (char**) &safe_updates, (char**) &safe_updates, 0, GET_BOOL, NO_ARG, 0, 0,
1542
 
    0, 0, 0, 0},
 
1403
   (char**) &safe_updates, (char**) &safe_updates, 0, GET_BOOL, NO_ARG, 0, 0,
 
1404
   0, 0, 0, 0},
1543
1405
  {"verbose", 'v', N_("Write more. (-v -v -v gives the table output format)."), 0,
1544
 
    0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
 
1406
   0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
1545
1407
  {"version", 'V', N_("Output version information and exit."), 0, 0, 0,
1546
 
    GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
 
1408
   GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
1547
1409
  {"wait", 'w', N_("Wait and retry if connection is down."), 0, 0, 0, GET_NO_ARG,
1548
 
    NO_ARG, 0, 0, 0, 0, 0, 0},
 
1410
   NO_ARG, 0, 0, 0, 0, 0, 0},
1549
1411
  {"connect_timeout", OPT_CONNECT_TIMEOUT,
1550
 
    N_("Number of seconds before connection timeout."),
1551
 
    (char**) &opt_connect_timeout,
1552
 
    (char**) &opt_connect_timeout, 0, GET_UINT32, REQUIRED_ARG, 0, 0, 3600*12, 0,
1553
 
    0, 0},
1554
 
  {"max_input_line", OPT_MAX_INPUT_LINE,
1555
 
    N_("Max length of input line"),
1556
 
    (char**) &opt_max_input_line, (char**) &opt_max_input_line, 0,
1557
 
    GET_UINT32, REQUIRED_ARG, 16 *1024L*1024L, 4096,
1558
 
    (int64_t) 2*1024L*1024L*1024L, MALLOC_OVERHEAD, 1024, 0},
 
1412
   N_("Number of seconds before connection timeout."),
 
1413
   (char**) &opt_connect_timeout,
 
1414
   (char**) &opt_connect_timeout, 0, GET_ULONG, REQUIRED_ARG, 0, 0, 3600*12, 0,
 
1415
   0, 0},
 
1416
  {"max_allowed_packet", OPT_MAX_ALLOWED_PACKET,
 
1417
   N_("Max packet length to send to, or receive from server"),
 
1418
   (char**) &opt_max_allowed_packet, (char**) &opt_max_allowed_packet, 0,
 
1419
   GET_ULONG, REQUIRED_ARG, 16 *1024L*1024L, 4096,
 
1420
   (int64_t) 2*1024L*1024L*1024L, MALLOC_OVERHEAD, 1024, 0},
 
1421
  {"net_buffer_length", OPT_NET_BUFFER_LENGTH,
 
1422
   N_("Buffer for TCP/IP and socket communication"),
 
1423
   (char**) &opt_net_buffer_length, (char**) &opt_net_buffer_length, 0, GET_ULONG,
 
1424
   REQUIRED_ARG, 16384, 1024, 512*1024*1024L, MALLOC_OVERHEAD, 1024, 0},
1559
1425
  {"select_limit", OPT_SELECT_LIMIT,
1560
 
    N_("Automatic limit for SELECT when using --safe-updates"),
1561
 
    (char**) &select_limit,
1562
 
    (char**) &select_limit, 0, GET_UINT32, REQUIRED_ARG, 1000L, 1, ULONG_MAX,
1563
 
    0, 1, 0},
 
1426
   N_("Automatic limit for SELECT when using --safe-updates"),
 
1427
   (char**) &select_limit,
 
1428
   (char**) &select_limit, 0, GET_ULONG, REQUIRED_ARG, 1000L, 1, ULONG_MAX,
 
1429
   0, 1, 0},
1564
1430
  {"max_join_size", OPT_MAX_JOIN_SIZE,
1565
 
    N_("Automatic limit for rows in a join when using --safe-updates"),
1566
 
    (char**) &max_join_size,
1567
 
    (char**) &max_join_size, 0, GET_UINT32, REQUIRED_ARG, 1000000L, 1, ULONG_MAX,
1568
 
    0, 1, 0},
 
1431
   N_("Automatic limit for rows in a join when using --safe-updates"),
 
1432
   (char**) &max_join_size,
 
1433
   (char**) &max_join_size, 0, GET_ULONG, REQUIRED_ARG, 1000000L, 1, ULONG_MAX,
 
1434
   0, 1, 0},
1569
1435
  {"secure-auth", OPT_SECURE_AUTH, N_("Refuse client connecting to server if it uses old (pre-4.1.1) protocol"), (char**) &opt_secure_auth,
1570
 
    (char**) &opt_secure_auth, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
 
1436
   (char**) &opt_secure_auth, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
1571
1437
  {"show-warnings", OPT_SHOW_WARNINGS, N_("Show warnings after every statement."),
1572
 
    (char**) &show_warnings, (char**) &show_warnings, 0, GET_BOOL, NO_ARG,
1573
 
    0, 0, 0, 0, 0, 0},
1574
 
  {"show-progress-size", OPT_SHOW_PROGRESS_SIZE, N_("Number of lines before each import progress report."),
1575
 
    (char**) &show_progress_size, (char**) &show_progress_size, 0, GET_UINT32, REQUIRED_ARG,
1576
 
    0, 0, 0, 0, 0, 0},
1577
 
  {"ping", OPT_PING, N_("Ping the server to check if it's alive."),
1578
 
    (char**) &opt_ping, (char**) &opt_ping, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
1579
 
  {"mysql", 'm', N_("Use MySQL Protocol."),
1580
 
    (char**) &opt_mysql, (char**) &opt_mysql, 0, GET_BOOL, NO_ARG, 1, 0, 0,
1581
 
    0, 0, 0},
 
1438
   (char**) &show_warnings, (char**) &show_warnings, 0, GET_BOOL, NO_ARG,
 
1439
   0, 0, 0, 0, 0, 0},
1582
1440
  { 0, 0, 0, 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0}
1583
1441
};
1584
1442
 
1587
1445
{
1588
1446
  const char* readline= "readline";
1589
1447
 
1590
 
  printf(_("%s  Ver %s Distrib %s, for %s-%s (%s) using %s %s\n"),
1591
 
         internal::my_progname, VER.c_str(), drizzle_version(),
1592
 
         HOST_VENDOR, HOST_OS, HOST_CPU,
 
1448
  printf(_("%s  Ver %s Distrib %s, for %s (%s) using %s %s\n"),
 
1449
         my_progname, VER, drizzle_get_client_info(),
 
1450
         SYSTEM_TYPE, MACHINE_TYPE,
1593
1451
         readline, rl_library_version);
1594
1452
 
1595
1453
  if (version)
1596
1454
    return;
1597
 
  printf(_("Copyright (C) 2008 Sun Microsystems\n"
1598
 
           "This software comes with ABSOLUTELY NO WARRANTY. "
1599
 
           "This is free software,\n"
1600
 
           "and you are welcome to modify and redistribute it "
1601
 
           "under the GPL license\n"));
1602
 
  printf(_("Usage: %s [OPTIONS] [database]\n"), internal::my_progname);
 
1455
  printf(_("\
 
1456
Copyright (C) 2000-2008 MySQL AB\n                                      \
 
1457
This software comes with ABSOLUTELY NO WARRANTY. This is free software,\n \
 
1458
and you are welcome to modify and redistribute it under the GPL license\n"));
 
1459
  printf(_("Usage: %s [OPTIONS] [database]\n"), my_progname);
1603
1460
  my_print_help(my_long_options);
1604
 
  internal::print_defaults("drizzle", load_default_groups);
 
1461
  print_defaults("my", load_default_groups);
1605
1462
  my_print_variables(my_long_options);
1606
1463
}
1607
1464
 
1608
1465
 
1609
 
static int get_one_option(int optid, const struct option *, char *argument)
 
1466
bool
 
1467
get_one_option(int optid, const struct my_option *opt __attribute__((unused)),
 
1468
               char *argument)
1610
1469
{
1611
 
  char *endchar= NULL;
1612
 
  uint64_t temp_drizzle_port= 0;
1613
 
 
1614
1470
  switch(optid) {
 
1471
  case OPT_CHARSETS_DIR:
 
1472
    strmake(drizzle_charsets_dir, argument, sizeof(drizzle_charsets_dir) - 1);
 
1473
    charsets_dir = drizzle_charsets_dir;
 
1474
    break;
1615
1475
  case  OPT_DEFAULT_CHARSET:
1616
1476
    default_charset_used= 1;
1617
1477
    break;
1618
1478
  case OPT_DELIMITER:
1619
1479
    if (argument == disabled_my_option)
1620
1480
    {
1621
 
      strcpy(delimiter, DEFAULT_DELIMITER);
 
1481
      stpcpy(delimiter, DEFAULT_DELIMITER);
1622
1482
    }
1623
1483
    else
1624
1484
    {
1625
1485
      /* Check that delimiter does not contain a backslash */
1626
1486
      if (!strstr(argument, "\\"))
1627
1487
      {
1628
 
        strncpy(delimiter, argument, sizeof(delimiter) - 1);
 
1488
        strmake(delimiter, argument, sizeof(delimiter) - 1);
1629
1489
      }
1630
1490
      else
1631
1491
      {
1632
1492
        put_info(_("DELIMITER cannot contain a backslash character"),
1633
1493
                 INFO_ERROR,0,0);
1634
 
        return EXIT_ARGUMENT_INVALID;
 
1494
        return 0;
1635
1495
      }
1636
1496
    }
1637
 
    delimiter_length= (uint32_t)strlen(delimiter);
 
1497
    delimiter_length= (uint)strlen(delimiter);
1638
1498
    delimiter_str= delimiter;
1639
1499
    break;
 
1500
  case OPT_LOCAL_INFILE:
 
1501
    using_opt_local_infile=1;
 
1502
    break;
1640
1503
  case OPT_TEE:
1641
1504
    if (argument == disabled_my_option)
1642
1505
    {
1660
1523
      if (argument && strlen(argument))
1661
1524
      {
1662
1525
        default_pager_set= 1;
1663
 
        strncpy(pager, argument, sizeof(pager) - 1);
1664
 
        strcpy(default_pager, pager);
 
1526
        strmake(pager, argument, sizeof(pager) - 1);
 
1527
        stpcpy(default_pager, pager);
1665
1528
      }
1666
1529
      else if (default_pager_set)
1667
 
        strcpy(pager, default_pager);
 
1530
        stpcpy(pager, default_pager);
1668
1531
      else
1669
1532
        opt_nopager= 1;
1670
1533
    }
1685
1548
  case 'e':
1686
1549
    status.batch= 1;
1687
1550
    status.add_to_history= 0;
1688
 
    if (status.line_buff == NULL)
1689
 
      status.line_buff= new(std::nothrow) LineBuffer(opt_max_input_line,NULL);
1690
 
    if (status.line_buff == NULL)
1691
 
    {
1692
 
      internal::my_end();
1693
 
      exit(1);
1694
 
    }
1695
 
    status.line_buff->addString(argument);
 
1551
    if (!status.line_buff)
 
1552
      ignore_errors= 0;                         // do it for the first -e only
 
1553
    if (!(status.line_buff= batch_readline_command(status.line_buff, argument)))
 
1554
      return 1;
1696
1555
    break;
1697
1556
  case 'o':
1698
1557
    if (argument == disabled_my_option)
1701
1560
      one_database= skip_updates= 1;
1702
1561
    break;
1703
1562
  case 'p':
1704
 
    temp_drizzle_port= (uint64_t) strtoul(argument, &endchar, 10);
1705
 
    /* if there is an alpha character this is not a valid port */
1706
 
    if (strlen(endchar) != 0)
1707
 
    {
1708
 
      put_info(_("Non-integer value supplied for port.  If you are trying to enter a password please use --password instead."), INFO_ERROR, 0, 0);
1709
 
      return EXIT_ARGUMENT_INVALID;
1710
 
    }
1711
 
    /* If the port number is > 65535 it is not a valid port
1712
 
      This also helps with potential data loss casting unsigned long to a
1713
 
      uint32_t. */
1714
 
    if ((temp_drizzle_port == 0) || (temp_drizzle_port > 65535))
1715
 
    {
1716
 
      put_info(_("Value supplied for port is not valid."), INFO_ERROR, 0, 0);
1717
 
      return EXIT_ARGUMENT_INVALID;
1718
 
    }
1719
 
    else
1720
 
    {
1721
 
      opt_drizzle_port= (uint32_t) temp_drizzle_port;
1722
 
    }
1723
 
    break;
1724
 
  case 'P':
1725
 
    /* Don't require password */
1726
1563
    if (argument == disabled_my_option)
1727
 
    {
1728
 
      argument= (char*) "";
1729
 
    }
 
1564
      argument= (char*) "";      // Don't require password
1730
1565
    if (argument)
1731
1566
    {
1732
1567
      char *start= argument;
1733
 
      free(opt_password);
 
1568
      my_free(opt_password, MYF(MY_ALLOW_ZERO_PTR));
1734
1569
      opt_password= strdup(argument);
1735
 
      while (*argument)
1736
 
      {
1737
 
        /* Overwriting password with 'x' */
1738
 
        *argument++= 'x';
1739
 
      }
 
1570
      while (*argument) *argument++= 'x';        // Destroy argument
1740
1571
      if (*start)
1741
 
      {
1742
 
        start[1]= 0;
1743
 
      }
 
1572
        start[1]=0 ;
1744
1573
      tty_password= 0;
1745
1574
    }
1746
1575
    else
1747
 
    {
1748
1576
      tty_password= 1;
1749
 
    }
1750
1577
    break;
1751
1578
  case 's':
1752
1579
    if (argument == disabled_my_option)
1765
1592
    status.add_to_history= 0;
1766
1593
    set_if_bigger(opt_silent,1);                         // more silent
1767
1594
    break;
 
1595
    break;
1768
1596
  case 'V':
1769
1597
    usage(1);
1770
1598
    exit(0);
1781
1609
{
1782
1610
  char *tmp, *pagpoint;
1783
1611
  int ho_error;
 
1612
  const DRIZZLE_PARAMETERS *drizzle_params= drizzle_get_parameters();
1784
1613
 
1785
1614
  tmp= (char *) getenv("DRIZZLE_HOST");
1786
1615
  if (tmp)
1789
1618
  pagpoint= getenv("PAGER");
1790
1619
  if (!((char*) (pagpoint)))
1791
1620
  {
1792
 
    strcpy(pager, "stdout");
 
1621
    stpcpy(pager, "stdout");
1793
1622
    opt_nopager= 1;
1794
1623
  }
1795
1624
  else
1796
 
    strcpy(pager, pagpoint);
1797
 
  strcpy(default_pager, pager);
 
1625
    stpcpy(pager, pagpoint);
 
1626
  stpcpy(default_pager, pager);
 
1627
 
 
1628
  opt_max_allowed_packet= *drizzle_params->p_max_allowed_packet;
 
1629
  opt_net_buffer_length= *drizzle_params->p_net_buffer_length;
1798
1630
 
1799
1631
  if ((ho_error=handle_options(&argc, &argv, my_long_options, get_one_option)))
1800
1632
    exit(ho_error);
1801
1633
 
 
1634
  *drizzle_params->p_max_allowed_packet= opt_max_allowed_packet;
 
1635
  *drizzle_params->p_net_buffer_length= opt_net_buffer_length;
 
1636
 
1802
1637
  if (status.batch) /* disable pager and outfile in this case */
1803
1638
  {
1804
 
    strcpy(default_pager, "stdout");
1805
 
    strcpy(pager, "stdout");
 
1639
    stpcpy(default_pager, "stdout");
 
1640
    stpcpy(pager, "stdout");
1806
1641
    opt_nopager= 1;
1807
1642
    default_pager_set= 0;
1808
1643
    opt_outfile= 0;
1809
1644
    opt_reconnect= 0;
1810
 
    connect_flag= DRIZZLE_CAPABILITIES_NONE; /* Not in interactive mode */
 
1645
    connect_flag= 0; /* Not in interactive mode */
1811
1646
  }
1812
1647
 
 
1648
  if (strcmp(default_charset, charset_info->csname) &&
 
1649
      !(charset_info= get_charset_by_csname(default_charset,
 
1650
                                            MY_CS_PRIMARY, MYF(MY_WME))))
 
1651
    exit(1);
1813
1652
  if (argc > 1)
1814
1653
  {
1815
1654
    usage(0);
1818
1657
  if (argc == 1)
1819
1658
  {
1820
1659
    skip_updates= 0;
1821
 
    free(current_db);
 
1660
    my_free(current_db, MYF(MY_ALLOW_ZERO_PTR));
1822
1661
    current_db= strdup(*argv);
1823
1662
  }
1824
1663
  if (tty_password)
1825
 
    opt_password= client_get_tty_password(NULL);
1826
 
 
 
1664
    opt_password= get_tty_password(NullS);
 
1665
  if (debug_info_flag)
 
1666
    my_end_arg= MY_CHECK_ERROR | MY_GIVE_INFO;
 
1667
  if (debug_check_flag)
 
1668
    my_end_arg= MY_CHECK_ERROR;
1827
1669
  return(0);
1828
1670
}
1829
1671
 
1840
1682
  {
1841
1683
    if (!interactive)
1842
1684
    {
1843
 
      if (status.line_buff)
1844
 
        line= status.line_buff->readline();
1845
 
      else
1846
 
        line= 0;
1847
 
 
 
1685
      line=batch_readline(status.line_buff);
 
1686
      /*
 
1687
        Skip UTF8 Byte Order Marker (BOM) 0xEFBBBF.
 
1688
        Editors like "notepad" put this marker in
 
1689
        the very beginning of a text file when
 
1690
        you save the file using "Unicode UTF-8" format.
 
1691
      */
 
1692
      if (!line_number &&
 
1693
          (uchar) line[0] == 0xEF &&
 
1694
          (uchar) line[1] == 0xBB &&
 
1695
          (uchar) line[2] == 0xBF)
 
1696
        line+= 3;
1848
1697
      line_number++;
1849
 
      if (show_progress_size > 0)
1850
 
      {
1851
 
        if ((line_number % show_progress_size) == 0)
1852
 
          fprintf(stderr, _("Processing line: %"PRIu32"\n"), line_number);
1853
 
      }
1854
1698
      if (!glob_buffer->empty())
1855
 
        status.query_start_line= line_number;
 
1699
        status.query_start_line=line_number;
1856
1700
    }
1857
1701
    else
1858
1702
    {
1922
1766
 
1923
1767
static COMMANDS *find_command(const char *name,char cmd_char)
1924
1768
{
1925
 
  uint32_t len;
 
1769
  uint len;
1926
1770
  const char *end;
1927
1771
 
1928
1772
  if (!name)
1942
1786
    if (strstr(name, "\\g") || (strstr(name, delimiter) &&
1943
1787
                                !(strlen(name) >= 9 &&
1944
1788
                                  !my_strnncoll(charset_info,
1945
 
                                                (unsigned char*) name, 9,
1946
 
                                                (const unsigned char*) "delimiter",
 
1789
                                                (uchar*) name, 9,
 
1790
                                                (const uchar*) "delimiter",
1947
1791
                                                9))))
1948
1792
      return((COMMANDS *) 0);
1949
1793
    if ((end=strcont(name," \t")))
1950
1794
    {
1951
 
      len=(uint32_t) (end - name);
 
1795
      len=(uint) (end - name);
1952
1796
      while (my_isspace(charset_info,*end))
1953
1797
        end++;
1954
1798
      if (!*end)
1955
1799
        end=0;          // no arguments to function
1956
1800
    }
1957
1801
    else
1958
 
      len=(uint32_t) strlen(name);
 
1802
      len=(uint) strlen(name);
1959
1803
  }
1960
1804
 
1961
 
  for (uint32_t i= 0; commands[i].name; i++)
 
1805
  for (uint i= 0; commands[i].name; i++)
1962
1806
  {
1963
1807
    if (commands[i].func &&
1964
 
        ((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)))
 
1808
        ((name && !my_strnncoll(charset_info,(const uchar*)name,len, (const uchar*)commands[i].name,len) && !commands[i].name[len] && (!end || (end && commands[i].takes_params))) || (!name && commands[i].cmd_char == cmd_char)))
1965
1809
    {
1966
1810
      return(&commands[i]);
1967
1811
    }
1971
1815
 
1972
1816
 
1973
1817
static bool add_line(string *buffer, char *line, char *in_string,
1974
 
                     bool *ml_comment)
 
1818
                        bool *ml_comment)
1975
1819
{
1976
 
  unsigned char inchar;
1977
 
  char *pos, *out;
 
1820
  uchar inchar;
 
1821
  char buff[80], *pos, *out;
1978
1822
  COMMANDS *com;
1979
1823
  bool need_space= 0;
1980
1824
  bool ss_comment= 0;
1984
1828
    return(0);
1985
1829
  if (status.add_to_history && line[0] && not_in_history(line))
1986
1830
    add_history(line);
1987
 
  char *end_of_line=line+(uint32_t) strlen(line);
 
1831
  char *end_of_line=line+(uint) strlen(line);
1988
1832
 
1989
 
  for (pos=out=line ; (inchar= (unsigned char) *pos) ; pos++)
 
1833
  for (pos=out=line ; (inchar= (uchar) *pos) ; pos++)
1990
1834
  {
1991
1835
    if (!preserve_comments)
1992
1836
    {
1996
1840
        continue;
1997
1841
    }
1998
1842
 
 
1843
#ifdef USE_MB
1999
1844
    // Accept multi-byte characters as-is
2000
1845
    int length;
2001
1846
    if (use_mb(charset_info) &&
2011
1856
        pos+= length - 1;
2012
1857
      continue;
2013
1858
    }
2014
 
    if (!*ml_comment && inchar == '\\' &&
2015
 
        !(*in_string && (drizzle_con_status(&con) & DRIZZLE_CON_STATUS_NO_BACKSLASH_ESCAPES)))
 
1859
#endif
 
1860
        if (!*ml_comment && inchar == '\\' &&
 
1861
            !(*in_string && (drizzle.server_status & SERVER_STATUS_NO_BACKSLASH_ESCAPES)))
2016
1862
    {
2017
1863
      // Found possbile one character command like \c
2018
1864
 
2019
 
      if (!(inchar = (unsigned char) *++pos))
 
1865
      if (!(inchar = (uchar) *++pos))
2020
1866
        break;        // readline adds one '\'
2021
1867
      if (*in_string || inchar == 'N')  // \N is short for NULL
2022
1868
      {          // Don't allow commands in string
2024
1870
        *out++= (char) inchar;
2025
1871
        continue;
2026
1872
      }
2027
 
      if ((com=find_command(NULL,(char) inchar)))
 
1873
      if ((com=find_command(NullS,(char) inchar)))
2028
1874
      {
2029
1875
        // Flush previously accepted characters
2030
1876
        if (out != line)
2052
1898
          {
2053
1899
            for (pos++ ;
2054
1900
                 *pos && (*pos != *delimiter ||
2055
 
                          strncmp(pos + 1, delimiter + 1,
2056
 
                                  strlen(delimiter + 1))) ; pos++)
 
1901
                          !is_prefix(pos + 1, delimiter + 1)) ; pos++)
2057
1902
              ;  // Remove parameters
2058
1903
            if (!*pos)
2059
1904
              pos--;
2064
1909
      }
2065
1910
      else
2066
1911
      {
2067
 
        string buff(_("Unknown command: "));
2068
 
        buff.push_back('\'');
2069
 
        buff.push_back(inchar);
2070
 
        buff.push_back('\'');
2071
 
        buff.push_back('.');
2072
 
        if (put_info(buff.c_str(),INFO_ERROR,0,0) > 0)
 
1912
        sprintf(buff,_("Unknown command '\\%c'."),inchar);
 
1913
        if (put_info(buff,INFO_ERROR,0,0) > 0)
2073
1914
          return(1);
2074
1915
        *out++='\\';
2075
1916
        *out++=(char) inchar;
2078
1919
    }
2079
1920
    else if (!*ml_comment && !*in_string &&
2080
1921
             (end_of_line - pos) >= 10 &&
2081
 
             !my_strnncoll(charset_info, (unsigned char*) pos, 10,
2082
 
                           (const unsigned char*) "delimiter ", 10))
 
1922
             !my_strnncoll(charset_info, (uchar*) pos, 10,
 
1923
                           (const uchar*) "delimiter ", 10))
2083
1924
    {
2084
1925
      // Flush previously accepted characters
2085
1926
      if (out != line)
2108
1949
      buffer->clear();
2109
1950
      break;
2110
1951
    }
2111
 
    else if (!*ml_comment && !*in_string && !strncmp(pos, delimiter,
2112
 
                                                     strlen(delimiter)))
 
1952
    else if (!*ml_comment && !*in_string && is_prefix(pos, delimiter))
2113
1953
    {
2114
1954
      // Found a statement. Continue parsing after the delimiter
2115
1955
      pos+= delimiter_length;
2232
2072
  if (out != line || (buffer->length() > 0))
2233
2073
  {
2234
2074
    *out++='\n';
2235
 
    uint32_t length=(uint32_t) (out-line);
 
2075
    uint length=(uint) (out-line);
2236
2076
    if ((!*ml_comment || preserve_comments))
2237
2077
      buffer->append(line, length);
2238
2078
  }
2240
2080
}
2241
2081
 
2242
2082
/*****************************************************************
2243
 
  Interface to Readline Completion
2244
 
 ******************************************************************/
 
2083
            Interface to Readline Completion
 
2084
******************************************************************/
2245
2085
 
2246
2086
 
2247
2087
static char **mysql_completion (const char *text, int start, int end);
2248
 
extern "C" char *new_command_generator(const char *text, int);
 
2088
static char *new_command_generator(const char *text, int);
2249
2089
 
2250
2090
/*
2251
2091
  Tell the GNU Readline library how to complete.  We want to try to complete
2252
2092
  on command names if this is the first word in the line, or on filenames
2253
2093
  if not.
2254
2094
*/
2255
 
static char *no_completion(const char *, int)
 
2095
static char *no_completion(const char * a __attribute__((unused)),
 
2096
                           int b __attribute__((unused)))
2256
2097
{
2257
2098
  /* No filename completion */
2258
2099
  return 0;
2337
2178
 
2338
2179
  /* Tell the completer that we want a crack first. */
2339
2180
  rl_attempted_completion_function= (rl_completion_func_t*)&mysql_completion;
2340
 
  rl_completion_entry_function= (drizzle_compentry_func_t*)&no_completion;
 
2181
  rl_completion_entry_function= (rl_compentry_func_t*)&no_completion;
2341
2182
}
2342
2183
 
2343
2184
 
2347
2188
  entire line in case we want to do some simple parsing.  Return the
2348
2189
  array of matches, or NULL if there aren't any.
2349
2190
*/
2350
 
char **mysql_completion (const char *text, int, int)
 
2191
char **mysql_completion (const char *text,
 
2192
                        int start __attribute__((unused)),
 
2193
                        int end __attribute__((unused)))
2351
2194
{
2352
2195
  if (!status.batch && !quick)
2353
2196
    return rl_completion_matches(text, new_command_generator);
2355
2198
    return (char**) 0;
2356
2199
}
2357
2200
 
2358
 
inline string lower_string(const string &from_string)
2359
 
{
2360
 
  string to_string= from_string;
2361
 
  transform(to_string.begin(), to_string.end(),
2362
 
            to_string.begin(), ::tolower);
2363
 
  return to_string;
2364
 
}
2365
 
inline string lower_string(const char * from_string)
2366
 
{
2367
 
  string to_string= from_string;
2368
 
  return lower_string(to_string);
2369
 
}
2370
 
 
2371
 
template <class T>
2372
 
class CompletionMatch :
2373
 
  public unary_function<const string&, bool>
2374
 
{
2375
 
  string match_text; 
2376
 
  T match_func;
2377
 
public:
2378
 
  CompletionMatch(string text) : match_text(text) {}
2379
 
  inline bool operator() (const pair<string,string> &match_against) const
2380
 
  {
2381
 
    string sub_match=
2382
 
      lower_string(match_against.first.substr(0,match_text.size()));
2383
 
    return match_func(sub_match,match_text);
2384
 
  }
2385
 
};
2386
 
 
2387
 
 
2388
 
 
2389
 
extern "C"
2390
 
char *new_command_generator(const char *text, int state)
2391
 
{
 
2201
 
 
2202
static char *new_command_generator(const char *text,int state)
 
2203
{
 
2204
  static int textlen;
 
2205
  char *ptr;
 
2206
  static Bucket *b;
 
2207
  static entry *e;
 
2208
  static uint i;
2392
2209
 
2393
2210
  if (!state)
2394
 
  {
2395
 
    completion_string= lower_string(text);
2396
 
    if (completion_string.size() == 0)
2397
 
    {
2398
 
      completion_iter= completion_map.begin();
2399
 
      completion_end= completion_map.end();
2400
 
    }
2401
 
    else
2402
 
    {
2403
 
      completion_iter= find_if(completion_map.begin(), completion_map.end(),
2404
 
                               CompletionMatch<equal_to<string> >(completion_string));
2405
 
      completion_end= find_if(completion_iter, completion_map.end(),
2406
 
                              CompletionMatch<not_equal_to<string> >(completion_string));
2407
 
    }
2408
 
  }
2409
 
  if (completion_iter == completion_end || (size_t)state > completion_map.size())
2410
 
    return NULL;
2411
 
  char *result= (char *)malloc((*completion_iter).second.size()+1);
2412
 
  strcpy(result, (*completion_iter).second.c_str());
2413
 
  completion_iter++;
2414
 
  return result;
 
2211
    textlen=(uint) strlen(text);
 
2212
 
 
2213
  if (textlen>0)
 
2214
  {            /* lookup in the hash */
 
2215
    if (!state)
 
2216
    {
 
2217
      uint len;
 
2218
 
 
2219
      b = find_all_matches(&ht,text,(uint) strlen(text),&len);
 
2220
      if (!b)
 
2221
        return NullS;
 
2222
      e = b->pData;
 
2223
    }
 
2224
 
 
2225
    if (e)
 
2226
    {
 
2227
      ptr= strdup(e->str);
 
2228
      e = e->pNext;
 
2229
      return ptr;
 
2230
    }
 
2231
  }
 
2232
  else
 
2233
  { /* traverse the entire hash, ugly but works */
 
2234
 
 
2235
    if (!state)
 
2236
    {
 
2237
      /* find the first used bucket */
 
2238
      for (i=0 ; i < ht.nTableSize ; i++)
 
2239
      {
 
2240
        if (ht.arBuckets[i])
 
2241
        {
 
2242
          b = ht.arBuckets[i];
 
2243
          e = b->pData;
 
2244
          break;
 
2245
        }
 
2246
      }
 
2247
    }
 
2248
    ptr= NullS;
 
2249
    while (e && !ptr)
 
2250
    {          /* find valid entry in bucket */
 
2251
      if ((uint) strlen(e->str) == b->nKeyLength)
 
2252
        ptr = strdup(e->str);
 
2253
      /* find the next used entry */
 
2254
      e = e->pNext;
 
2255
      if (!e)
 
2256
      { /* find the next used bucket */
 
2257
        b = b->pNext;
 
2258
        if (!b)
 
2259
        {
 
2260
          for (i++ ; i<ht.nTableSize; i++)
 
2261
          {
 
2262
            if (ht.arBuckets[i])
 
2263
            {
 
2264
              b = ht.arBuckets[i];
 
2265
              e = b->pData;
 
2266
              break;
 
2267
            }
 
2268
          }
 
2269
        }
 
2270
        else
 
2271
          e = b->pData;
 
2272
      }
 
2273
    }
 
2274
    if (ptr)
 
2275
      return ptr;
 
2276
  }
 
2277
  return NullS;
2415
2278
}
2416
2279
 
 
2280
 
2417
2281
/* Build up the completion hash */
2418
2282
 
2419
2283
static void build_completion_hash(bool rehash, bool write_info)
2420
2284
{
2421
2285
  COMMANDS *cmd=commands;
2422
 
  drizzle_return_t ret;
2423
 
  drizzle_result_st databases,tables,fields;
2424
 
  drizzle_row_t database_row,table_row;
2425
 
  drizzle_column_st *sql_field;
2426
 
  string tmp_str, tmp_str_lower;
 
2286
  DRIZZLE_RES *databases=0,*tables=0;
 
2287
  DRIZZLE_RES *fields;
 
2288
  static char ***field_names= 0;
 
2289
  DRIZZLE_ROW database_row,table_row;
 
2290
  DRIZZLE_FIELD *sql_field;
 
2291
  char buf[NAME_LEN*2+2];     // table name plus field name plus 2
 
2292
  int i,j,num_fields;
 
2293
 
2427
2294
 
2428
2295
  if (status.batch || quick || !current_db)
2429
2296
    return;      // We don't need completion in batches
2430
2297
  if (!rehash)
2431
2298
    return;
2432
2299
 
2433
 
  completion_map.clear();
 
2300
  /* Free old used memory */
 
2301
  if (field_names)
 
2302
    field_names=0;
 
2303
  completion_hash_clean(&ht);
 
2304
  free_root(&hash_mem_root,MYF(0));
2434
2305
 
2435
2306
  /* hash this file's known subset of SQL commands */
2436
2307
  while (cmd->name) {
2437
 
    tmp_str= cmd->name;
2438
 
    tmp_str_lower= lower_string(tmp_str);
2439
 
    completion_map[tmp_str_lower]= tmp_str;
 
2308
    add_word(&ht,(char*) cmd->name);
2440
2309
    cmd++;
2441
2310
  }
2442
2311
 
2443
2312
  /* hash Drizzle functions (to be implemented) */
2444
2313
 
2445
2314
  /* hash all database names */
2446
 
  if (drizzle_query_str(&con, &databases, "show databases", &ret) != NULL)
 
2315
  if (drizzle_query(&drizzle,"show databases") == 0)
2447
2316
  {
2448
 
    if (ret == DRIZZLE_RETURN_OK)
 
2317
    if (!(databases = drizzle_store_result(&drizzle)))
 
2318
      put_info(drizzle_error(&drizzle),INFO_INFO,0,0);
 
2319
    else
2449
2320
    {
2450
 
      if (drizzle_result_buffer(&databases) != DRIZZLE_RETURN_OK)
2451
 
        put_info(drizzle_error(&drizzle),INFO_INFO,0,0);
2452
 
      else
 
2321
      while ((database_row=drizzle_fetch_row(databases)))
2453
2322
      {
2454
 
        while ((database_row=drizzle_row_next(&databases)))
2455
 
        {
2456
 
          tmp_str= database_row[0];
2457
 
          tmp_str_lower= lower_string(tmp_str);
2458
 
          completion_map[tmp_str_lower]= tmp_str;
2459
 
        }
 
2323
        char *str=strdup_root(&hash_mem_root, (char*) database_row[0]);
 
2324
        if (str)
 
2325
          add_word(&ht,(char*) str);
2460
2326
      }
 
2327
      drizzle_free_result(databases);
2461
2328
    }
2462
 
 
2463
 
    drizzle_result_free(&databases);
2464
2329
  }
2465
 
 
2466
2330
  /* hash all table names */
2467
 
  if (drizzle_query_str(&con, &tables, "show tables", &ret) != NULL)
 
2331
  if (drizzle_query(&drizzle,"show tables")==0)
2468
2332
  {
2469
 
    if (ret != DRIZZLE_RETURN_OK)
2470
 
    {
2471
 
      drizzle_result_free(&tables);
2472
 
      return;
2473
 
    }
2474
 
 
2475
 
    if (drizzle_result_buffer(&tables) != DRIZZLE_RETURN_OK)
 
2333
    if (!(tables = drizzle_store_result(&drizzle)))
2476
2334
      put_info(drizzle_error(&drizzle),INFO_INFO,0,0);
2477
2335
    else
2478
2336
    {
2479
 
      if (drizzle_result_row_count(&tables) > 0 && !opt_silent && write_info)
 
2337
      if (drizzle_num_rows(tables) > 0 && !opt_silent && write_info)
2480
2338
      {
2481
2339
        tee_fprintf(stdout, _("\
2482
 
                              Reading table information for completion of table and column names\n    \
2483
 
                              You can turn off this feature to get a quicker startup with -A\n\n"));
 
2340
Reading table information for completion of table and column names\n    \
 
2341
You can turn off this feature to get a quicker startup with -A\n\n"));
2484
2342
      }
2485
 
      while ((table_row=drizzle_row_next(&tables)))
 
2343
      while ((table_row=drizzle_fetch_row(tables)))
2486
2344
      {
2487
 
        tmp_str= table_row[0];
2488
 
        tmp_str_lower= lower_string(tmp_str);
2489
 
        completion_map[tmp_str_lower]= tmp_str;
 
2345
        char *str=strdup_root(&hash_mem_root, (char*) table_row[0]);
 
2346
        if (str &&
 
2347
            !completion_hash_exists(&ht,(char*) str, (uint) strlen(str)))
 
2348
          add_word(&ht,str);
2490
2349
      }
2491
2350
    }
2492
2351
  }
2493
 
  else
2494
 
    return;
2495
2352
 
2496
2353
  /* hash all field names, both with the table prefix and without it */
2497
 
  if (drizzle_result_row_count(&tables) == 0)
2498
 
  {
2499
 
    drizzle_result_free(&tables);
2500
 
    return;
2501
 
  }
2502
 
 
2503
 
  drizzle_row_seek(&tables, 0);
2504
 
 
2505
 
  while ((table_row=drizzle_row_next(&tables)))
2506
 
  {
2507
 
    string query;
2508
 
 
2509
 
    query.append("show fields in '");
2510
 
    query.append(table_row[0]);
2511
 
    query.append("'");
2512
 
 
2513
 
    if (drizzle_query(&con, &fields, query.c_str(), query.length(),
2514
 
                      &ret) != NULL)
 
2354
  if (!tables)          /* no tables */
 
2355
  {
 
2356
    return;
 
2357
  }
 
2358
  drizzle_data_seek(tables,0);
 
2359
  if (!(field_names= (char ***) alloc_root(&hash_mem_root,sizeof(char **) *
 
2360
                                           (uint) (drizzle_num_rows(tables)+1))))
 
2361
  {
 
2362
    drizzle_free_result(tables);
 
2363
    return;
 
2364
  }
 
2365
  i=0;
 
2366
  while ((table_row=drizzle_fetch_row(tables)))
 
2367
  {
 
2368
    if ((fields=drizzle_list_fields(&drizzle,(const char*) table_row[0],NullS)))
2515
2369
    {
2516
 
      if (ret == DRIZZLE_RETURN_OK &&
2517
 
          drizzle_result_buffer(&fields) == DRIZZLE_RETURN_OK)
2518
 
      {
2519
 
        while ((sql_field=drizzle_column_next(&fields)))
2520
 
        {
2521
 
          tmp_str=table_row[0];
2522
 
          tmp_str.append(".");
2523
 
          tmp_str.append(drizzle_column_name(sql_field));
2524
 
          tmp_str_lower= lower_string(tmp_str);
2525
 
          completion_map[tmp_str_lower]= tmp_str;
2526
 
 
2527
 
          tmp_str=drizzle_column_name(sql_field);
2528
 
          tmp_str_lower= lower_string(tmp_str);
2529
 
          completion_map[tmp_str_lower]= tmp_str;
2530
 
        }
2531
 
      }
2532
 
      drizzle_result_free(&fields);
 
2370
      num_fields=drizzle_num_fields(fields);
 
2371
      if (!(field_names[i] = (char **) alloc_root(&hash_mem_root,
 
2372
                                                  sizeof(char *) *
 
2373
                                                  (num_fields*2+1))))
 
2374
      {
 
2375
        drizzle_free_result(fields);
 
2376
        break;
 
2377
      }
 
2378
      field_names[i][num_fields*2]= '\0';
 
2379
      j=0;
 
2380
      while ((sql_field=drizzle_fetch_field(fields)))
 
2381
      {
 
2382
        sprintf(buf,"%.64s.%.64s",table_row[0],sql_field->name);
 
2383
        field_names[i][j] = strdup_root(&hash_mem_root,buf);
 
2384
        add_word(&ht,field_names[i][j]);
 
2385
        field_names[i][num_fields+j] = strdup_root(&hash_mem_root,
 
2386
                                                   sql_field->name);
 
2387
        if (!completion_hash_exists(&ht,field_names[i][num_fields+j],
 
2388
                                    (uint) strlen(field_names[i][num_fields+j])))
 
2389
          add_word(&ht,field_names[i][num_fields+j]);
 
2390
        j++;
 
2391
      }
 
2392
      drizzle_free_result(fields);
2533
2393
    }
 
2394
    else
 
2395
      field_names[i]= 0;
 
2396
 
 
2397
    i++;
2534
2398
  }
2535
 
  drizzle_result_free(&tables);
2536
 
  completion_iter= completion_map.begin();
 
2399
  drizzle_free_result(tables);
 
2400
  field_names[i]=0;        // End pointer
 
2401
  return;
2537
2402
}
2538
2403
 
2539
2404
/* for gnu readline */
2540
2405
 
 
2406
#ifndef HAVE_INDEX
 
2407
extern "C" {
 
2408
  extern char *index(const char *,int c),*rindex(const char *,int);
 
2409
 
 
2410
  char *index(const char *s,int c)
 
2411
  {
 
2412
    for (;;)
 
2413
    {
 
2414
      if (*s == (char) c) return (char*) s;
 
2415
      if (!*s++) return NullS;
 
2416
    }
 
2417
  }
 
2418
 
 
2419
  char *rindex(const char *s,int c)
 
2420
  {
 
2421
    register char *t;
 
2422
 
 
2423
    t = NullS;
 
2424
    do if (*s == (char) c) t = (char*) s; while (*s++);
 
2425
    return (char*) t;
 
2426
  }
 
2427
}
 
2428
#endif
 
2429
 
2541
2430
 
2542
2431
static int reconnect(void)
2543
2432
{
 
2433
  /* purecov: begin tested */
2544
2434
  if (opt_reconnect)
2545
2435
  {
2546
2436
    put_info(_("No connection. Trying to reconnect..."),INFO_INFO,0,0);
2547
2437
    (void) com_connect((string *)0, 0);
2548
 
    if (opt_rehash && connected)
 
2438
    if (opt_rehash)
2549
2439
      com_rehash(NULL, NULL);
2550
2440
  }
2551
 
  if (! connected)
 
2441
  if (!connected)
2552
2442
    return put_info(_("Can't connect to the server\n"),INFO_ERROR,0,0);
 
2443
  /* purecov: end */
2553
2444
  return 0;
2554
2445
}
2555
2446
 
2556
2447
static void get_current_db(void)
2557
2448
{
2558
 
  drizzle_return_t ret;
2559
 
  drizzle_result_st res;
 
2449
  DRIZZLE_RES *res;
2560
2450
 
2561
 
  free(current_db);
 
2451
  my_free(current_db, MYF(MY_ALLOW_ZERO_PTR));
2562
2452
  current_db= NULL;
2563
2453
  /* In case of error below current_db will be NULL */
2564
 
  if (drizzle_query_str(&con, &res, "SELECT DATABASE()", &ret) != NULL)
 
2454
  if (!drizzle_query(&drizzle, "SELECT DATABASE()") &&
 
2455
      (res= drizzle_use_result(&drizzle)))
2565
2456
  {
2566
 
    if (ret == DRIZZLE_RETURN_OK &&
2567
 
        drizzle_result_buffer(&res) == DRIZZLE_RETURN_OK)
2568
 
    {
2569
 
      drizzle_row_t row= drizzle_row_next(&res);
2570
 
      if (row[0])
2571
 
        current_db= strdup(row[0]);
2572
 
      drizzle_result_free(&res);
2573
 
    }
 
2457
    DRIZZLE_ROW row= drizzle_fetch_row(res);
 
2458
    if (row[0])
 
2459
      current_db= strdup(row[0]);
 
2460
    drizzle_free_result(res);
2574
2461
  }
2575
2462
}
2576
2463
 
2577
2464
/***************************************************************************
2578
 
  The different commands
2579
 
 ***************************************************************************/
 
2465
 The different commands
 
2466
***************************************************************************/
2580
2467
 
2581
 
int drizzleclient_real_query_for_lazy(const char *buf, int length,
2582
 
                                      drizzle_result_st *result,
2583
 
                                      uint32_t *error_code)
 
2468
int drizzle_real_query_for_lazy(const char *buf, int length)
2584
2469
{
2585
 
  drizzle_return_t ret;
2586
 
 
2587
 
  for (uint32_t retry=0;; retry++)
 
2470
  for (uint retry=0;; retry++)
2588
2471
  {
2589
2472
    int error;
2590
 
    if (drizzle_query(&con,result,buf,length,&ret) != NULL &&
2591
 
        ret == DRIZZLE_RETURN_OK)
2592
 
    {
 
2473
    if (!drizzle_real_query(&drizzle,buf,length))
2593
2474
      return 0;
2594
 
    }
2595
 
    error= put_error(&con, result);
2596
 
 
2597
 
    if (ret == DRIZZLE_RETURN_ERROR_CODE)
2598
 
    {
2599
 
      *error_code= drizzle_result_error_code(result);
2600
 
      drizzle_result_free(result);
2601
 
    }
2602
 
 
2603
 
    if (ret != DRIZZLE_RETURN_SERVER_GONE || retry > 1 ||
 
2475
    error= put_error(&drizzle);
 
2476
    if (drizzle_errno(&drizzle) != CR_SERVER_GONE_ERROR || retry > 1 ||
2604
2477
        !opt_reconnect)
2605
 
    {
2606
2478
      return error;
2607
 
    }
2608
 
 
2609
2479
    if (reconnect())
2610
2480
      return error;
2611
2481
  }
2612
2482
}
2613
2483
 
2614
 
int drizzleclient_store_result_for_lazy(drizzle_result_st *result)
 
2484
int drizzle_store_result_for_lazy(DRIZZLE_RES **result)
2615
2485
{
2616
 
  if (drizzle_result_buffer(result) == DRIZZLE_RETURN_OK)
 
2486
  if ((*result=drizzle_store_result(&drizzle)))
2617
2487
    return 0;
2618
2488
 
2619
 
  if (drizzle_con_error(&con)[0])
2620
 
  {
2621
 
    int ret = put_error(&con, result);
2622
 
    drizzle_result_free(result);
2623
 
    return ret;
2624
 
  }
 
2489
  if (drizzle_error(&drizzle)[0])
 
2490
    return put_error(&drizzle);
2625
2491
  return 0;
2626
2492
}
2627
2493
 
 
2494
static void print_help_item(DRIZZLE_ROW *cur, int num_name, int num_cat, char *last_char)
 
2495
{
 
2496
  char ccat= (*cur)[num_cat][0];
 
2497
  if (*last_char != ccat)
 
2498
  {
 
2499
    put_info(ccat == 'Y' ? _("categories:") : _("topics:"), INFO_INFO,0,0);
 
2500
    *last_char= ccat;
 
2501
  }
 
2502
  tee_fprintf(PAGER, "   %s\n", (*cur)[num_name]);
 
2503
}
 
2504
 
 
2505
 
 
2506
static int com_server_help(string *buffer,
 
2507
                           const char *line __attribute__((unused)),
 
2508
                           char *help_arg)
 
2509
{
 
2510
  DRIZZLE_ROW cur;
 
2511
  const char *server_cmd= buffer->c_str();
 
2512
  char cmd_buf[100];
 
2513
  DRIZZLE_RES *result;
 
2514
  int error;
 
2515
 
 
2516
  if (help_arg[0] != '\'')
 
2517
  {
 
2518
    char *end_arg= strchr(help_arg, '\0');
 
2519
    if(--end_arg)
 
2520
    {
 
2521
      while (my_isspace(charset_info,*end_arg))
 
2522
        end_arg--;
 
2523
      *++end_arg= '\0';
 
2524
    }
 
2525
    (void) strxnmov(cmd_buf, sizeof(cmd_buf), "help '", help_arg, "'", NullS);
 
2526
    server_cmd= cmd_buf;
 
2527
  }
 
2528
 
 
2529
  if (!connected && reconnect())
 
2530
    return 1;
 
2531
 
 
2532
  if ((error= drizzle_real_query_for_lazy(server_cmd,(int)strlen(server_cmd))) ||
 
2533
      (error= drizzle_store_result_for_lazy(&result)))
 
2534
    return error;
 
2535
 
 
2536
  if (result)
 
2537
  {
 
2538
    unsigned int num_fields= drizzle_num_fields(result);
 
2539
    uint64_t num_rows= drizzle_num_rows(result);
 
2540
    drizzle_fetch_fields(result);
 
2541
    if (num_fields==3 && num_rows==1)
 
2542
    {
 
2543
      if (!(cur= drizzle_fetch_row(result)))
 
2544
      {
 
2545
        error= -1;
 
2546
        goto err;
 
2547
      }
 
2548
 
 
2549
      init_pager();
 
2550
      tee_fprintf(PAGER,   _("Name: \'%s\'\n"), cur[0]);
 
2551
      tee_fprintf(PAGER,   _("Description:\n%s"), cur[1]);
 
2552
      if (cur[2] && *((char*)cur[2]))
 
2553
        tee_fprintf(PAGER, _("Examples:\n%s"), cur[2]);
 
2554
      tee_fprintf(PAGER,   "\n");
 
2555
      end_pager();
 
2556
    }
 
2557
    else if (num_fields >= 2 && num_rows)
 
2558
    {
 
2559
      init_pager();
 
2560
      char last_char= 0;
 
2561
 
 
2562
      int num_name= 0, num_cat= 0;
 
2563
 
 
2564
      if (num_fields == 2)
 
2565
      {
 
2566
        put_info(_("Many help items for your request exist."), INFO_INFO,0,0);
 
2567
        put_info(_("To make a more specific request, please type 'help <item>',\nwhere <item> is one of the following"), INFO_INFO,0,0);
 
2568
        num_name= 0;
 
2569
        num_cat= 1;
 
2570
      }
 
2571
      else if ((cur= drizzle_fetch_row(result)))
 
2572
      {
 
2573
        tee_fprintf(PAGER, _("You asked for help about help category: '%s'\n"), cur[0]);
 
2574
        put_info(_("For more information, type 'help <item>', where <item> is one of the following"), INFO_INFO,0,0);
 
2575
        num_name= 1;
 
2576
        num_cat= 2;
 
2577
        print_help_item(&cur,1,2,&last_char);
 
2578
      }
 
2579
 
 
2580
      while ((cur= drizzle_fetch_row(result)))
 
2581
        print_help_item(&cur,num_name,num_cat,&last_char);
 
2582
      tee_fprintf(PAGER, "\n");
 
2583
      end_pager();
 
2584
    }
 
2585
    else
 
2586
    {
 
2587
      put_info(_("\nNothing found"), INFO_INFO,0,0);
 
2588
      put_info(_("Please try to run 'help contents' for a list of all accessible topics\n"), INFO_INFO,0,0);
 
2589
    }
 
2590
  }
 
2591
 
 
2592
err:
 
2593
  drizzle_free_result(result);
 
2594
  return error;
 
2595
}
 
2596
 
2628
2597
static int
2629
 
com_help(string *buffer, const char *)
 
2598
com_help(string *buffer __attribute__((unused)),
 
2599
         const char *line __attribute__((unused)))
2630
2600
{
2631
2601
  register int i, j;
2632
 
  char buff[32], *end;
 
2602
  char * help_arg= strchr(line,' '), buff[32], *end;
 
2603
  if (help_arg)
 
2604
  {
 
2605
    while (my_isspace(charset_info,*help_arg))
 
2606
      help_arg++;
 
2607
    if (*help_arg) return com_server_help(buffer,line,help_arg);
 
2608
  }
2633
2609
 
2634
2610
  put_info(_("List of all Drizzle commands:"), INFO_INFO,0,0);
2635
2611
  if (!named_cmds)
2636
2612
    put_info(_("Note that all text commands must be first on line and end with ';'"),INFO_INFO,0,0);
2637
2613
  for (i = 0; commands[i].name; i++)
2638
2614
  {
2639
 
    end= strcpy(buff, commands[i].name);
2640
 
    end+= strlen(commands[i].name);
 
2615
    end= stpcpy(buff, commands[i].name);
2641
2616
    for (j= (int)strlen(commands[i].name); j < 10; j++)
2642
 
      end= strcpy(end, " ")+1;
 
2617
      end= stpcpy(end, " ");
2643
2618
    if (commands[i].func)
2644
2619
      tee_fprintf(stdout, "%s(\\%c) %s\n", buff,
2645
2620
                  commands[i].cmd_char, _(commands[i].doc));
2646
2621
  }
2647
 
  tee_fprintf(stdout, "\n");
2648
 
  buffer->clear();
 
2622
  if (connected && drizzle_get_server_version(&drizzle) >= 40100)
 
2623
    put_info(_("\nFor server side help, type 'help contents'\n"), INFO_INFO,0,0);
2649
2624
  return 0;
2650
2625
}
2651
2626
 
2652
2627
 
2653
2628
static int
2654
 
com_clear(string *buffer, const char *)
 
2629
com_clear(string *buffer,
 
2630
          const char *line __attribute__((unused)))
2655
2631
{
2656
2632
  if (status.add_to_history)
2657
2633
    fix_history(buffer);
2659
2635
  return 0;
2660
2636
}
2661
2637
 
 
2638
static int
 
2639
com_charset(string *buffer __attribute__((unused)),
 
2640
            const char *line)
 
2641
{
 
2642
  char buff[256], *param;
 
2643
  const CHARSET_INFO * new_cs;
 
2644
  strmake(buff, line, sizeof(buff) - 1);
 
2645
  param= get_arg(buff, 0);
 
2646
  if (!param || !*param)
 
2647
  {
 
2648
    return put_info(_("Usage: \\C char_setname | charset charset_name"),
 
2649
                    INFO_ERROR, 0, 0);
 
2650
  }
 
2651
  new_cs= get_charset_by_csname(param, MY_CS_PRIMARY, MYF(MY_WME));
 
2652
  if (new_cs)
 
2653
  {
 
2654
    charset_info= new_cs;
 
2655
    drizzle_set_character_set(&drizzle, charset_info->csname);
 
2656
    default_charset= (char *)charset_info->csname;
 
2657
    default_charset_used= 1;
 
2658
    put_info(_("Charset changed"), INFO_INFO,0,0);
 
2659
  }
 
2660
  else put_info(_("Charset is not found"), INFO_INFO,0,0);
 
2661
  return 0;
 
2662
}
2662
2663
 
2663
2664
/*
2664
2665
  Execute command
2665
 
Returns: 0  if ok
2666
 
-1 if not fatal error
2667
 
1  if fatal error
 
2666
  Returns: 0  if ok
 
2667
  -1 if not fatal error
 
2668
  1  if fatal error
2668
2669
*/
2669
2670
static int
2670
 
com_go(string *buffer, const char *)
 
2671
com_go(string *buffer,
 
2672
       const char *line __attribute__((unused)))
2671
2673
{
2672
2674
  char          buff[200]; /* about 110 chars used so far */
2673
2675
  char          time_buff[52+3+1]; /* time max + space&parens + NUL */
2674
 
  drizzle_result_st result;
2675
 
  drizzle_return_t ret;
2676
 
  uint32_t      timer, warnings= 0;
2677
 
  uint32_t      error= 0;
2678
 
  uint32_t      error_code= 0;
 
2676
  DRIZZLE_RES     *result;
 
2677
  uint32_t         timer, warnings= 0;
 
2678
  uint          error= 0;
2679
2679
  int           err= 0;
2680
2680
 
2681
2681
  interrupted_query= 0;
2709
2709
 
2710
2710
  timer=start_timer();
2711
2711
  executing_query= 1;
2712
 
  error= drizzleclient_real_query_for_lazy(buffer->c_str(),buffer->length(),&result, &error_code);
 
2712
  error= drizzle_real_query_for_lazy(buffer->c_str(),buffer->length());
2713
2713
 
2714
2714
  if (status.add_to_history)
2715
2715
  {
2729
2729
 
2730
2730
    if (quick)
2731
2731
    {
2732
 
      if (drizzle_column_buffer(&result) != DRIZZLE_RETURN_OK)
 
2732
      if (!(result=drizzle_use_result(&drizzle)) && drizzle_field_count(&drizzle))
2733
2733
      {
2734
 
        error= put_error(&con, &result);
 
2734
        error= put_error(&drizzle);
2735
2735
        goto end;
2736
2736
      }
2737
2737
    }
2738
2738
    else
2739
2739
    {
2740
 
      error= drizzleclient_store_result_for_lazy(&result);
 
2740
      error= drizzle_store_result_for_lazy(&result);
2741
2741
      if (error)
2742
2742
        goto end;
2743
2743
    }
2748
2748
      time_buff[0]= '\0';
2749
2749
 
2750
2750
    /* Every branch must truncate  buff . */
2751
 
    if (drizzle_result_column_count(&result) > 0)
 
2751
    if (result)
2752
2752
    {
2753
 
      if (!quick && drizzle_result_row_count(&result) == 0 &&
2754
 
          !column_types_flag)
 
2753
      if (!drizzle_num_rows(result) && ! quick && !column_types_flag)
2755
2754
      {
2756
 
        strcpy(buff, _("Empty set"));
 
2755
        stpcpy(buff, _("Empty set"));
2757
2756
      }
2758
2757
      else
2759
2758
      {
2760
2759
        init_pager();
2761
2760
        if (vertical || (auto_vertical_output &&
2762
 
                         (terminal_width < get_result_width(&result))))
2763
 
          print_table_data_vertically(&result);
 
2761
                         (terminal_width < get_result_width(result))))
 
2762
          print_table_data_vertically(result);
2764
2763
        else if (opt_silent && verbose <= 2 && !output_tables)
2765
 
          print_tab_data(&result);
 
2764
          print_tab_data(result);
2766
2765
        else
2767
 
          print_table_data(&result);
2768
 
        snprintf(buff, sizeof(buff), 
2769
 
                 ngettext("%ld row in set","%ld rows in set",
2770
 
                          (long) drizzle_result_row_count(&result)),
2771
 
                 (long) drizzle_result_row_count(&result));
 
2766
          print_table_data(result);
 
2767
        sprintf(buff,
 
2768
                ngettext("%ld row in set","%ld rows in set",
 
2769
                         (long) drizzle_num_rows(result)),
 
2770
                (long) drizzle_num_rows(result));
2772
2771
        end_pager();
2773
 
        if (drizzle_result_error_code(&result))
2774
 
          error= put_error(&con, &result);
 
2772
        if (drizzle_errno(&drizzle))
 
2773
          error= put_error(&drizzle);
2775
2774
      }
2776
2775
    }
2777
 
    else if (drizzle_result_affected_rows(&result) == ~(uint64_t) 0)
2778
 
      strcpy(buff,_("Query OK"));
 
2776
    else if (drizzle_affected_rows(&drizzle) == ~(uint64_t) 0)
 
2777
      stpcpy(buff,_("Query OK"));
2779
2778
    else
2780
 
      snprintf(buff, sizeof(buff), ngettext("Query OK, %ld row affected",
2781
 
                                            "Query OK, %ld rows affected",
2782
 
                                            (long) drizzle_result_affected_rows(&result)),
2783
 
               (long) drizzle_result_affected_rows(&result));
 
2779
      sprintf(buff, ngettext("Query OK, %ld row affected",
 
2780
                             "Query OK, %ld rows affected",
 
2781
                             (long) drizzle_affected_rows(&drizzle)),
 
2782
              (long) drizzle_affected_rows(&drizzle));
2784
2783
 
2785
2784
    pos= strchr(buff, '\0');
2786
 
    if ((warnings= drizzle_result_warning_count(&result)))
 
2785
    if ((warnings= drizzle_warning_count(&drizzle)))
2787
2786
    {
2788
2787
      *pos++= ',';
2789
2788
      *pos++= ' ';
2790
 
      char warnings_buff[20];
2791
 
      memset(warnings_buff,0,20);
2792
 
      snprintf(warnings_buff, sizeof(warnings_buff), "%d", warnings);
2793
 
      strcpy(pos, warnings_buff);
2794
 
      pos+= strlen(warnings_buff);
2795
 
      pos= strcpy(pos, " warning")+8;
 
2789
      pos=int10_to_str(warnings, pos, 10);
 
2790
      pos=stpcpy(pos, " warning");
2796
2791
      if (warnings != 1)
2797
2792
        *pos++= 's';
2798
2793
    }
2799
 
    strcpy(pos, time_buff);
 
2794
    stpcpy(pos, time_buff);
2800
2795
    put_info(buff,INFO_RESULT,0,0);
2801
 
    if (strcmp(drizzle_result_info(&result), ""))
2802
 
      put_info(drizzle_result_info(&result),INFO_RESULT,0,0);
 
2796
    if (drizzle_info(&drizzle))
 
2797
      put_info(drizzle_info(&drizzle),INFO_RESULT,0,0);
2803
2798
    put_info("",INFO_RESULT,0,0);      // Empty row
2804
2799
 
2805
 
    if (unbuffered)
 
2800
    if (result && !drizzle_eof(result))  /* Something wrong when using quick */
 
2801
      error= put_error(&drizzle);
 
2802
    else if (unbuffered)
2806
2803
      fflush(stdout);
2807
 
    drizzle_result_free(&result);
2808
 
 
2809
 
    if (drizzle_con_status(&con) & DRIZZLE_CON_STATUS_MORE_RESULTS_EXISTS)
2810
 
    {
2811
 
      if (drizzle_result_read(&con, &result, &ret) == NULL ||
2812
 
          ret != DRIZZLE_RETURN_OK)
2813
 
      {
2814
 
        if (ret == DRIZZLE_RETURN_ERROR_CODE)
2815
 
        {
2816
 
          error_code= drizzle_result_error_code(&result);
2817
 
          drizzle_result_free(&result);
2818
 
        }
2819
 
 
2820
 
        error= put_error(&con, NULL);
2821
 
        goto end;
2822
 
      }
2823
 
    }
2824
 
 
2825
 
  } while (drizzle_con_status(&con) & DRIZZLE_CON_STATUS_MORE_RESULTS_EXISTS);
 
2804
    drizzle_free_result(result);
 
2805
  } while (!(err= drizzle_next_result(&drizzle)));
2826
2806
  if (err >= 1)
2827
 
    error= put_error(&con, NULL);
 
2807
    error= put_error(&drizzle);
2828
2808
 
2829
2809
end:
2830
2810
 
2831
2811
  /* Show warnings if any or error occured */
2832
2812
  if (show_warnings == 1 && (warnings >= 1 || error))
2833
 
    print_warnings(error_code);
 
2813
    print_warnings();
2834
2814
 
2835
2815
  if (!error && !status.batch &&
2836
 
      drizzle_con_status(&con) & DRIZZLE_CON_STATUS_DB_DROPPED)
2837
 
  {
 
2816
      (drizzle.server_status & SERVER_STATUS_DB_DROPPED))
2838
2817
    get_current_db();
2839
 
  }
2840
2818
 
2841
2819
  executing_query= 0;
2842
2820
  return error;        /* New command follows */
2869
2847
  FILE* new_outfile;
2870
2848
  if (opt_outfile)
2871
2849
    end_tee();
2872
 
  if (!(new_outfile= fopen(file_name, "a")))
 
2850
  if (!(new_outfile= my_fopen(file_name, O_APPEND | O_WRONLY, MYF(MY_WME))))
2873
2851
  {
2874
2852
    tee_fprintf(stdout, "Error logging to file '%s'\n", file_name);
2875
2853
    return;
2876
2854
  }
2877
2855
  OUTFILE = new_outfile;
2878
 
  strncpy(outfile, file_name, FN_REFLEN-1);
 
2856
  strmake(outfile, file_name, FN_REFLEN-1);
2879
2857
  tee_fprintf(stdout, "Logging to file '%s'\n", file_name);
2880
2858
  opt_outfile= 1;
2881
 
 
2882
2859
  return;
2883
2860
}
2884
2861
 
2885
2862
 
2886
2863
static void end_tee()
2887
2864
{
2888
 
  fclose(OUTFILE);
 
2865
  my_fclose(OUTFILE, MYF(0));
2889
2866
  OUTFILE= 0;
2890
2867
  opt_outfile= 0;
2891
2868
  return;
2904
2881
}
2905
2882
 
2906
2883
 
2907
 
static const char *fieldtype2str(drizzle_column_type_t type)
 
2884
static const char *fieldtype2str(enum enum_field_types type)
2908
2885
{
2909
2886
  switch (type) {
2910
 
  case DRIZZLE_COLUMN_TYPE_BLOB:        return "BLOB";
2911
 
  case DRIZZLE_COLUMN_TYPE_DATE:        return "DATE";
2912
 
  case DRIZZLE_COLUMN_TYPE_DATETIME:    return "DATETIME";
2913
 
  case DRIZZLE_COLUMN_TYPE_NEWDECIMAL:  return "DECIMAL";
2914
 
  case DRIZZLE_COLUMN_TYPE_DOUBLE:      return "DOUBLE";
2915
 
  case DRIZZLE_COLUMN_TYPE_ENUM:        return "ENUM";
2916
 
  case DRIZZLE_COLUMN_TYPE_LONG:        return "LONG";
2917
 
  case DRIZZLE_COLUMN_TYPE_LONGLONG:    return "LONGLONG";
2918
 
  case DRIZZLE_COLUMN_TYPE_NULL:        return "NULL";
2919
 
  case DRIZZLE_COLUMN_TYPE_TIMESTAMP:   return "TIMESTAMP";
2920
 
  default:                     return "?-unknown-?";
 
2887
    case DRIZZLE_TYPE_BLOB:        return "BLOB";
 
2888
    case DRIZZLE_TYPE_NEWDATE:        return "DATE";
 
2889
    case DRIZZLE_TYPE_DATETIME:    return "DATETIME";
 
2890
    case DRIZZLE_TYPE_NEWDECIMAL:  return "DECIMAL";
 
2891
    case DRIZZLE_TYPE_DOUBLE:      return "DOUBLE";
 
2892
    case DRIZZLE_TYPE_ENUM:        return "ENUM";
 
2893
    case DRIZZLE_TYPE_LONG:        return "LONG";
 
2894
    case DRIZZLE_TYPE_LONGLONG:    return "LONGLONG";
 
2895
    case DRIZZLE_TYPE_NULL:        return "NULL";
 
2896
    case DRIZZLE_TYPE_SHORT:       return "SHORT";
 
2897
    case DRIZZLE_TYPE_TIME:        return "TIME";
 
2898
    case DRIZZLE_TYPE_TIMESTAMP:   return "TIMESTAMP";
 
2899
    case DRIZZLE_TYPE_TINY:        return "TINY";
 
2900
    default:                     return "?-unknown-?";
2921
2901
  }
2922
2902
}
2923
2903
 
2924
 
static char *fieldflags2str(uint32_t f) {
 
2904
static char *fieldflags2str(uint f) {
2925
2905
  static char buf[1024];
2926
2906
  char *s=buf;
2927
2907
  *s=0;
2928
2908
#define ff2s_check_flag(X)                                              \
2929
 
  if (f & DRIZZLE_COLUMN_FLAGS_ ## X) { s=strcpy(s, # X " ")+strlen(# X " "); \
2930
 
    f &= ~ DRIZZLE_COLUMN_FLAGS_ ## X; }
 
2909
  if (f & X ## _FLAG) { s=stpcpy(s, # X " "); f &= ~ X ## _FLAG; }
2931
2910
  ff2s_check_flag(NOT_NULL);
2932
2911
  ff2s_check_flag(PRI_KEY);
2933
2912
  ff2s_check_flag(UNIQUE_KEY);
2948
2927
  ff2s_check_flag(ON_UPDATE_NOW);
2949
2928
#undef ff2s_check_flag
2950
2929
  if (f)
2951
 
    snprintf(s, sizeof(buf), " unknows=0x%04x", f);
 
2930
    sprintf(s, " unknows=0x%04x", f);
2952
2931
  return buf;
2953
2932
}
2954
2933
 
2955
2934
static void
2956
 
print_field_types(drizzle_result_st *result)
 
2935
print_field_types(DRIZZLE_RES *result)
2957
2936
{
2958
 
  drizzle_column_st   *field;
2959
 
  uint32_t i=0;
 
2937
  DRIZZLE_FIELD   *field;
 
2938
  uint i=0;
2960
2939
 
2961
 
  while ((field = drizzle_column_next(result)))
 
2940
  while ((field = drizzle_fetch_field(result)))
2962
2941
  {
2963
2942
    tee_fprintf(PAGER, "Field %3u:  `%s`\n"
2964
2943
                "Catalog:    `%s`\n"
2972
2951
                "Decimals:   %u\n"
2973
2952
                "Flags:      %s\n\n",
2974
2953
                ++i,
2975
 
                drizzle_column_name(field), drizzle_column_catalog(field),
2976
 
                drizzle_column_db(field), drizzle_column_table(field),
2977
 
                drizzle_column_orig_table(field),
2978
 
                fieldtype2str(drizzle_column_type(field)),
2979
 
                get_charset_name(drizzle_column_charset(field)),
2980
 
                drizzle_column_charset(field), drizzle_column_size(field),
2981
 
                drizzle_column_max_size(field), drizzle_column_decimals(field),
2982
 
                fieldflags2str(drizzle_column_flags(field)));
 
2954
                field->name, field->catalog, field->db, field->table,
 
2955
                field->org_table, fieldtype2str(field->type),
 
2956
                get_charset_name(field->charsetnr), field->charsetnr,
 
2957
                field->length, field->max_length, field->decimals,
 
2958
                fieldflags2str(field->flags));
2983
2959
  }
2984
2960
  tee_puts("", PAGER);
2985
2961
}
2986
2962
 
2987
2963
 
2988
2964
static void
2989
 
print_table_data(drizzle_result_st *result)
 
2965
print_table_data(DRIZZLE_RES *result)
2990
2966
{
2991
 
  drizzle_row_t cur;
2992
 
  drizzle_return_t ret;
2993
 
  drizzle_column_st *field;
2994
 
  bool *num_flag;
 
2967
  DRIZZLE_ROW     cur;
 
2968
  DRIZZLE_FIELD   *field;
 
2969
  bool          *num_flag;
2995
2970
  string separator;
2996
 
 
 
2971
  
2997
2972
  separator.reserve(256);
2998
2973
 
2999
 
  num_flag=(bool*) malloc(sizeof(bool)*drizzle_result_column_count(result));
 
2974
  num_flag=(bool*) my_malloc(sizeof(bool)*drizzle_num_fields(result),
 
2975
                             MYF(MY_WME));
3000
2976
  if (column_types_flag)
3001
2977
  {
3002
2978
    print_field_types(result);
3003
 
    if (!drizzle_result_row_count(result))
 
2979
    if (!drizzle_num_rows(result))
3004
2980
      return;
3005
 
    drizzle_column_seek(result,0);
 
2981
    drizzle_field_seek(result,0);
3006
2982
  }
3007
2983
  separator.append("+");
3008
 
  while ((field = drizzle_column_next(result)))
 
2984
  while ((field = drizzle_fetch_field(result)))
3009
2985
  {
3010
 
    uint32_t x, length= 0;
3011
 
 
3012
 
    if (column_names)
3013
 
    {
3014
 
      uint32_t name_length= strlen(drizzle_column_name(field));
3015
 
 
3016
 
      /* Check if the max_byte value is really the maximum in terms
3017
 
        of visual length since multibyte characters can affect the
3018
 
        length of the separator. */
3019
 
      length= charset_info->cset->numcells(charset_info,
3020
 
                                           drizzle_column_name(field),
3021
 
                                           drizzle_column_name(field) +
3022
 
                                           name_length);
3023
 
 
3024
 
      if (name_length == drizzle_column_max_size(field))
3025
 
      {
3026
 
        if (length < drizzle_column_max_size(field))
3027
 
          drizzle_column_set_max_size(field, length);
3028
 
      }
3029
 
      else
3030
 
      {
3031
 
        length= name_length;
3032
 
      }
3033
 
    }
3034
 
 
 
2986
    uint32_t length= column_names ? field->name_length : 0;
3035
2987
    if (quick)
3036
 
      length=max(length,drizzle_column_size(field));
 
2988
      length=max(length,field->length);
3037
2989
    else
3038
 
      length=max(length,(uint32_t)drizzle_column_max_size(field));
3039
 
    if (length < 4 &&
3040
 
        !(drizzle_column_flags(field) & DRIZZLE_COLUMN_FLAGS_NOT_NULL))
3041
 
    {
 
2990
      length=max(length,field->max_length);
 
2991
    if (length < 4 && !IS_NOT_NULL(field->flags))
3042
2992
      // Room for "NULL"
3043
2993
      length=4;
3044
 
    }
3045
 
    drizzle_column_set_max_size(field, length);
3046
 
 
 
2994
    field->max_length=length;
 
2995
    uint x;
3047
2996
    for (x=0; x< (length+2); x++)
3048
2997
      separator.append("-");
3049
2998
    separator.append("+");
3052
3001
  tee_puts((char*) separator.c_str(), PAGER);
3053
3002
  if (column_names)
3054
3003
  {
3055
 
    drizzle_column_seek(result,0);
 
3004
    drizzle_field_seek(result,0);
3056
3005
    (void) tee_fputs("|", PAGER);
3057
 
    for (uint32_t off=0; (field = drizzle_column_next(result)) ; off++)
 
3006
    for (uint off=0; (field = drizzle_fetch_field(result)) ; off++)
3058
3007
    {
3059
 
      uint32_t name_length= (uint32_t) strlen(drizzle_column_name(field));
3060
 
      uint32_t numcells= charset_info->cset->numcells(charset_info,
3061
 
                                                      drizzle_column_name(field),
3062
 
                                                      drizzle_column_name(field) +
3063
 
                                                      name_length);
3064
 
      uint32_t display_length= drizzle_column_max_size(field) + name_length -
3065
 
        numcells;
 
3008
      uint name_length= (uint) strlen(field->name);
 
3009
      uint numcells= charset_info->cset->numcells(charset_info,
 
3010
                                                  field->name,
 
3011
                                                  field->name + name_length);
 
3012
      uint32_t display_length= field->max_length + name_length - numcells;
3066
3013
      tee_fprintf(PAGER, " %-*s |",(int) min(display_length,
3067
3014
                                             MAX_COLUMN_LENGTH),
3068
 
                  drizzle_column_name(field));
3069
 
      num_flag[off]= ((drizzle_column_type(field) <= DRIZZLE_COLUMN_TYPE_LONGLONG) ||
3070
 
                      (drizzle_column_type(field) == DRIZZLE_COLUMN_TYPE_NEWDECIMAL));
 
3015
                  field->name);
 
3016
      num_flag[off]= IS_NUM(field->type);
3071
3017
    }
3072
3018
    (void) tee_fputs("\n", PAGER);
3073
3019
    tee_puts((char*) separator.c_str(), PAGER);
3074
3020
  }
3075
3021
 
3076
 
  while (1)
 
3022
  while ((cur= drizzle_fetch_row(result)))
3077
3023
  {
3078
 
    if (quick)
3079
 
    {
3080
 
      cur= drizzle_row_buffer(result, &ret);
3081
 
      if (ret != DRIZZLE_RETURN_OK)
3082
 
      {
3083
 
        (void)put_error(&con, result);
3084
 
        break;
3085
 
      }
3086
 
    }
3087
 
    else
3088
 
      cur= drizzle_row_next(result);
3089
 
 
3090
 
    if (cur == NULL || interrupted_query)
 
3024
    if (interrupted_query)
3091
3025
      break;
3092
 
 
3093
 
    size_t *lengths= drizzle_row_field_sizes(result);
 
3026
    uint32_t *lengths= drizzle_fetch_lengths(result);
3094
3027
    (void) tee_fputs("| ", PAGER);
3095
 
    drizzle_column_seek(result, 0);
3096
 
    for (uint32_t off= 0; off < drizzle_result_column_count(result); off++)
 
3028
    drizzle_field_seek(result, 0);
 
3029
    for (uint off= 0; off < drizzle_num_fields(result); off++)
3097
3030
    {
3098
3031
      const char *buffer;
3099
 
      uint32_t data_length;
3100
 
      uint32_t field_max_length;
3101
 
      uint32_t visible_length;
3102
 
      uint32_t extra_padding;
 
3032
      uint data_length;
 
3033
      uint field_max_length;
 
3034
      uint visible_length;
 
3035
      uint extra_padding;
3103
3036
 
3104
3037
      if (cur[off] == NULL)
3105
3038
      {
3109
3042
      else
3110
3043
      {
3111
3044
        buffer= cur[off];
3112
 
        data_length= (uint32_t) lengths[off];
 
3045
        data_length= (uint) lengths[off];
3113
3046
      }
3114
3047
 
3115
 
      field= drizzle_column_next(result);
3116
 
      field_max_length= drizzle_column_max_size(field);
 
3048
      field= drizzle_fetch_field(result);
 
3049
      field_max_length= field->max_length;
3117
3050
 
3118
3051
      /*
3119
3052
        How many text cells on the screen will this string span?  If it contains
3127
3060
      extra_padding= data_length - visible_length;
3128
3061
 
3129
3062
      if (field_max_length > MAX_COLUMN_LENGTH)
3130
 
        tee_print_sized_data(buffer, data_length, MAX_COLUMN_LENGTH+extra_padding, false);
 
3063
        tee_print_sized_data(buffer, data_length, MAX_COLUMN_LENGTH+extra_padding, FALSE);
3131
3064
      else
3132
3065
      {
3133
3066
        if (num_flag[off] != 0) /* if it is numeric, we right-justify it */
3134
 
          tee_print_sized_data(buffer, data_length, field_max_length+extra_padding, true);
 
3067
          tee_print_sized_data(buffer, data_length, field_max_length+extra_padding, TRUE);
3135
3068
        else
3136
3069
          tee_print_sized_data(buffer, data_length,
3137
 
                               field_max_length+extra_padding, false);
 
3070
                               field_max_length+extra_padding, FALSE);
3138
3071
      }
3139
3072
      tee_fputs(" | ", PAGER);
3140
3073
    }
3141
3074
    (void) tee_fputs("\n", PAGER);
3142
 
    if (quick)
3143
 
      drizzle_row_free(result, cur);
3144
3075
  }
3145
3076
  tee_puts(separator.c_str(), PAGER);
3146
 
  free(num_flag);
 
3077
  my_free(num_flag, MYF(MY_ALLOW_ZERO_PTR));
3147
3078
}
3148
3079
 
3149
3080
/**
3150
 
  Return the length of a field after it would be rendered into text.
3151
 
 
3152
 
  This doesn't know or care about multibyte characters.  Assume we're
3153
 
  using such a charset.  We can't know that all of the upcoming rows
3154
 
  for this column will have bytes that each render into some fraction
3155
 
  of a character.  It's at least possible that a row has bytes that
3156
 
  all render into one character each, and so the maximum length is
3157
 
  still the number of bytes.  (Assumption 1:  This can't be better
3158
 
  because we can never know the number of characters that the DB is
3159
 
  going to send -- only the number of bytes.  2: Chars <= Bytes.)
3160
 
 
3161
 
  @param  field  Pointer to a field to be inspected
3162
 
 
3163
 
  @returns  number of character positions to be used, at most
 
3081
   Return the length of a field after it would be rendered into text.
 
3082
 
 
3083
   This doesn't know or care about multibyte characters.  Assume we're
 
3084
   using such a charset.  We can't know that all of the upcoming rows
 
3085
   for this column will have bytes that each render into some fraction
 
3086
   of a character.  It's at least possible that a row has bytes that
 
3087
   all render into one character each, and so the maximum length is
 
3088
   still the number of bytes.  (Assumption 1:  This can't be better
 
3089
   because we can never know the number of characters that the DB is
 
3090
   going to send -- only the number of bytes.  2: Chars <= Bytes.)
 
3091
 
 
3092
   @param  field  Pointer to a field to be inspected
 
3093
 
 
3094
   @returns  number of character positions to be used, at most
3164
3095
*/
3165
 
static int get_field_disp_length(drizzle_column_st *field)
 
3096
static int get_field_disp_length(DRIZZLE_FIELD *field)
3166
3097
{
3167
 
  uint32_t length= column_names ? strlen(drizzle_column_name(field)) : 0;
 
3098
  uint length= column_names ? field->name_length : 0;
3168
3099
 
3169
3100
  if (quick)
3170
 
    length= max(length, drizzle_column_size(field));
 
3101
    length= max(length, field->length);
3171
3102
  else
3172
 
    length= max(length, (uint32_t)drizzle_column_max_size(field));
 
3103
    length= max(length, field->max_length);
3173
3104
 
3174
 
  if (length < 4 &&
3175
 
      !(drizzle_column_flags(field) & DRIZZLE_COLUMN_FLAGS_NOT_NULL))
3176
 
  {
 
3105
  if (length < 4 && !IS_NOT_NULL(field->flags))
3177
3106
    length= 4;        /* Room for "NULL" */
3178
 
  }
3179
3107
 
3180
3108
  return length;
3181
3109
}
3182
3110
 
3183
3111
/**
3184
 
  For a new result, return the max number of characters that any
3185
 
  upcoming row may return.
3186
 
 
3187
 
  @param  result  Pointer to the result to judge
3188
 
 
3189
 
  @returns  The max number of characters in any row of this result
 
3112
   For a new result, return the max number of characters that any
 
3113
   upcoming row may return.
 
3114
 
 
3115
   @param  result  Pointer to the result to judge
 
3116
 
 
3117
   @returns  The max number of characters in any row of this result
3190
3118
*/
3191
 
static int get_result_width(drizzle_result_st *result)
 
3119
static int get_result_width(DRIZZLE_RES *result)
3192
3120
{
3193
3121
  unsigned int len= 0;
3194
 
  drizzle_column_st *field;
3195
 
  uint16_t offset;
 
3122
  DRIZZLE_FIELD *field;
 
3123
  DRIZZLE_FIELD_OFFSET offset;
3196
3124
 
3197
 
  offset= drizzle_column_current(result);
 
3125
  offset= drizzle_field_tell(result);
3198
3126
  assert(offset == 0);
3199
3127
 
3200
 
  while ((field= drizzle_column_next(result)) != NULL)
 
3128
  while ((field= drizzle_fetch_field(result)) != NULL)
3201
3129
    len+= get_field_disp_length(field) + 3; /* plus bar, space, & final space */
3202
3130
 
3203
 
  (void) drizzle_column_seek(result, offset);
 
3131
  (void) drizzle_field_seek(result, offset);
3204
3132
 
3205
3133
  return len + 1; /* plus final bar. */
3206
3134
}
3236
3164
 
3237
3165
 
3238
3166
static void
3239
 
print_table_data_vertically(drizzle_result_st *result)
 
3167
print_table_data_vertically(DRIZZLE_RES *result)
3240
3168
{
3241
 
  drizzle_row_t cur;
3242
 
  drizzle_return_t ret;
3243
 
  uint32_t max_length=0;
3244
 
  drizzle_column_st *field;
 
3169
  DRIZZLE_ROW  cur;
 
3170
  uint    max_length=0;
 
3171
  DRIZZLE_FIELD  *field;
3245
3172
 
3246
 
  while ((field = drizzle_column_next(result)))
 
3173
  while ((field = drizzle_fetch_field(result)))
3247
3174
  {
3248
 
    uint32_t length= strlen(drizzle_column_name(field));
 
3175
    uint length= field->name_length;
3249
3176
    if (length > max_length)
3250
3177
      max_length= length;
3251
 
    drizzle_column_set_max_size(field, length);
 
3178
    field->max_length=length;
3252
3179
  }
3253
3180
 
3254
 
  for (uint32_t row_count=1;; row_count++)
 
3181
  drizzle_field_seek(result,0);
 
3182
  for (uint row_count=1; (cur= drizzle_fetch_row(result)); row_count++)
3255
3183
  {
3256
 
    if (quick)
3257
 
    {
3258
 
      cur= drizzle_row_buffer(result, &ret);
3259
 
      if (ret != DRIZZLE_RETURN_OK)
3260
 
      {
3261
 
        (void)put_error(&con, result);
3262
 
        break;
3263
 
      }
3264
 
    }
3265
 
    else
3266
 
      cur= drizzle_row_next(result);
3267
 
 
3268
 
    if (cur == NULL || interrupted_query)
 
3184
    if (interrupted_query)
3269
3185
      break;
3270
 
    drizzle_column_seek(result,0);
 
3186
    drizzle_field_seek(result,0);
3271
3187
    tee_fprintf(PAGER,
3272
3188
                "*************************** %d. row ***************************\n", row_count);
3273
 
    for (uint32_t off=0; off < drizzle_result_column_count(result); off++)
 
3189
    for (uint off=0; off < drizzle_num_fields(result); off++)
3274
3190
    {
3275
 
      field= drizzle_column_next(result);
3276
 
      tee_fprintf(PAGER, "%*s: ",(int) max_length,drizzle_column_name(field));
 
3191
      field= drizzle_fetch_field(result);
 
3192
      tee_fprintf(PAGER, "%*s: ",(int) max_length,field->name);
3277
3193
      tee_fprintf(PAGER, "%s\n",cur[off] ? (char*) cur[off] : "NULL");
3278
3194
    }
3279
 
    if (quick)
3280
 
      drizzle_row_free(result, cur);
3281
3195
  }
3282
3196
}
3283
3197
 
3284
3198
 
3285
3199
/* print_warnings should be called right after executing a statement */
3286
3200
 
3287
 
static void print_warnings(uint32_t error_code)
 
3201
static void print_warnings()
3288
3202
{
3289
 
  const char *query;
3290
 
  drizzle_result_st result;
3291
 
  drizzle_row_t cur;
 
3203
  const char   *query;
 
3204
  DRIZZLE_RES    *result;
 
3205
  DRIZZLE_ROW    cur;
3292
3206
  uint64_t num_rows;
3293
 
  uint32_t new_code= 0;
 
3207
 
 
3208
  /* Save current error before calling "show warnings" */
 
3209
  uint error= drizzle_errno(&drizzle);
3294
3210
 
3295
3211
  /* Get the warnings */
3296
3212
  query= "show warnings";
3297
 
  drizzleclient_real_query_for_lazy(query, strlen(query),&result,&new_code);
3298
 
  drizzleclient_store_result_for_lazy(&result);
 
3213
  drizzle_real_query_for_lazy(query, strlen(query));
 
3214
  drizzle_store_result_for_lazy(&result);
3299
3215
 
3300
3216
  /* Bail out when no warnings */
3301
 
  if (!(num_rows= drizzle_result_row_count(&result)))
 
3217
  if (!(num_rows= drizzle_num_rows(result)))
3302
3218
    goto end;
3303
3219
 
3304
 
  cur= drizzle_row_next(&result);
 
3220
  cur= drizzle_fetch_row(result);
3305
3221
 
3306
3222
  /*
3307
3223
    Don't print a duplicate of the current error.  It is possible for SHOW
3309
3225
    messages.  To be safe, skip printing the duplicate only if it is the only
3310
3226
    warning.
3311
3227
  */
3312
 
  if (!cur || (num_rows == 1 &&
3313
 
               error_code == (uint32_t) strtoul(cur[1], NULL, 10)))
3314
 
  {
 
3228
  if (!cur || (num_rows == 1 && error == (uint) strtoul(cur[1], NULL, 10)))
3315
3229
    goto end;
3316
 
  }
3317
3230
 
3318
3231
  /* Print the warnings */
3319
3232
  init_pager();
3320
3233
  do
3321
3234
  {
3322
3235
    tee_fprintf(PAGER, "%s (Code %s): %s\n", cur[0], cur[1], cur[2]);
3323
 
  } while ((cur= drizzle_row_next(&result)));
 
3236
  } while ((cur= drizzle_fetch_row(result)));
3324
3237
  end_pager();
3325
3238
 
3326
3239
end:
3327
 
  drizzle_result_free(&result);
 
3240
  drizzle_free_result(result);
3328
3241
}
3329
3242
 
3330
3243
 
3338
3251
    if (opt_raw_data)
3339
3252
      tee_fputs(pos, PAGER);
3340
3253
    else for (const char *end=pos+length ; pos != end ; pos++)
3341
 
    {
3342
 
      int l;
3343
 
      if (use_mb(charset_info) &&
3344
 
          (l = my_ismbchar(charset_info, pos, end)))
3345
 
      {
3346
 
        while (l--)
3347
 
          tee_putc(*pos++, PAGER);
3348
 
        pos--;
3349
 
        continue;
3350
 
      }
3351
 
      if (!*pos)
3352
 
        tee_fputs("\\0", PAGER); // This makes everything hard
3353
 
      else if (*pos == '\t')
3354
 
        tee_fputs("\\t", PAGER); // This would destroy tab format
3355
 
      else if (*pos == '\n')
3356
 
        tee_fputs("\\n", PAGER); // This too
3357
 
      else if (*pos == '\\')
3358
 
        tee_fputs("\\\\", PAGER);
3359
 
      else
3360
 
        tee_putc(*pos, PAGER);
3361
 
    }
 
3254
         {
 
3255
#ifdef USE_MB
 
3256
           int l;
 
3257
           if (use_mb(charset_info) &&
 
3258
               (l = my_ismbchar(charset_info, pos, end)))
 
3259
           {
 
3260
             while (l--)
 
3261
               tee_putc(*pos++, PAGER);
 
3262
             pos--;
 
3263
             continue;
 
3264
           }
 
3265
#endif
 
3266
           if (!*pos)
 
3267
             tee_fputs("\\0", PAGER); // This makes everything hard
 
3268
           else if (*pos == '\t')
 
3269
             tee_fputs("\\t", PAGER); // This would destroy tab format
 
3270
           else if (*pos == '\n')
 
3271
             tee_fputs("\\n", PAGER); // This too
 
3272
           else if (*pos == '\\')
 
3273
             tee_fputs("\\\\", PAGER);
 
3274
           else
 
3275
             tee_putc(*pos, PAGER);
 
3276
         }
3362
3277
  }
3363
3278
}
3364
3279
 
3365
3280
 
3366
3281
static void
3367
 
print_tab_data(drizzle_result_st *result)
 
3282
print_tab_data(DRIZZLE_RES *result)
3368
3283
{
3369
 
  drizzle_row_t cur;
3370
 
  drizzle_return_t ret;
3371
 
  drizzle_column_st *field;
3372
 
  size_t *lengths;
 
3284
  DRIZZLE_ROW  cur;
 
3285
  DRIZZLE_FIELD  *field;
 
3286
  uint32_t    *lengths;
3373
3287
 
3374
3288
  if (opt_silent < 2 && column_names)
3375
3289
  {
3376
3290
    int first=0;
3377
 
    while ((field = drizzle_column_next(result)))
 
3291
    while ((field = drizzle_fetch_field(result)))
3378
3292
    {
3379
3293
      if (first++)
3380
3294
        (void) tee_fputs("\t", PAGER);
3381
 
      (void) tee_fputs(drizzle_column_name(field), PAGER);
 
3295
      (void) tee_fputs(field->name, PAGER);
3382
3296
    }
3383
3297
    (void) tee_fputs("\n", PAGER);
3384
3298
  }
3385
 
  while (1)
 
3299
  while ((cur = drizzle_fetch_row(result)))
3386
3300
  {
3387
 
    if (quick)
3388
 
    {
3389
 
      cur= drizzle_row_buffer(result, &ret);
3390
 
      if (ret != DRIZZLE_RETURN_OK)
3391
 
      {
3392
 
        (void)put_error(&con, result);
3393
 
        break;
3394
 
      }
3395
 
    }
3396
 
    else
3397
 
      cur= drizzle_row_next(result);
3398
 
 
3399
 
    if (cur == NULL)
3400
 
      break;
3401
 
 
3402
 
    lengths= drizzle_row_field_sizes(result);
 
3301
    lengths= drizzle_fetch_lengths(result);
3403
3302
    safe_put_field(cur[0],lengths[0]);
3404
 
    for (uint32_t off=1 ; off < drizzle_result_column_count(result); off++)
 
3303
    for (uint off=1 ; off < drizzle_num_fields(result); off++)
3405
3304
    {
3406
3305
      (void) tee_fputs("\t", PAGER);
3407
3306
      safe_put_field(cur[off], lengths[off]);
3408
3307
    }
3409
3308
    (void) tee_fputs("\n", PAGER);
3410
 
    if (quick)
3411
 
      drizzle_row_free(result, cur);
3412
3309
  }
3413
3310
}
3414
3311
 
3415
3312
static int
3416
 
com_tee(string *, const char *line )
 
3313
com_tee(string *buffer __attribute__((unused)), const char *line )
3417
3314
{
3418
 
  char file_name[FN_REFLEN], *end;
3419
 
  const char *param;
 
3315
  char file_name[FN_REFLEN], *end, *param;
3420
3316
 
3421
3317
  if (status.batch)
3422
3318
    return 0;
3441
3337
  /* eliminate the spaces before the parameters */
3442
3338
  while (my_isspace(charset_info,*param))
3443
3339
    param++;
3444
 
  strncpy(file_name, param, sizeof(file_name) - 1);
3445
 
  end= file_name + strlen(file_name);
 
3340
  end= strmake(file_name, param, sizeof(file_name) - 1);
3446
3341
  /* remove end space from command line */
3447
3342
  while (end > file_name && (my_isspace(charset_info,end[-1]) ||
3448
3343
                             my_iscntrl(charset_info,end[-1])))
3459
3354
 
3460
3355
 
3461
3356
static int
3462
 
com_notee(string *, const char *)
 
3357
com_notee(string *buffer __attribute__((unused)),
 
3358
          const char *line __attribute__((unused)))
3463
3359
{
3464
3360
  if (opt_outfile)
3465
3361
    end_tee();
3472
3368
*/
3473
3369
 
3474
3370
static int
3475
 
com_pager(string *, const char *line)
 
3371
com_pager(string *buffer __attribute__((unused)),
 
3372
          const char *line __attribute__((unused)))
3476
3373
{
3477
 
  char pager_name[FN_REFLEN], *end;
3478
 
  const char *param;
 
3374
  char pager_name[FN_REFLEN], *end, *param;
3479
3375
 
3480
3376
  if (status.batch)
3481
3377
    return 0;
3493
3389
    {
3494
3390
      tee_fprintf(stdout, "Default pager wasn't set, using stdout.\n");
3495
3391
      opt_nopager=1;
3496
 
      strcpy(pager, "stdout");
 
3392
      stpcpy(pager, "stdout");
3497
3393
      PAGER= stdout;
3498
3394
      return 0;
3499
3395
    }
3500
 
    strcpy(pager, default_pager);
 
3396
    stpcpy(pager, default_pager);
3501
3397
  }
3502
3398
  else
3503
3399
  {
3504
 
    end= strncpy(pager_name, param, sizeof(pager_name)-1);
3505
 
    end+= strlen(pager_name);
 
3400
    end= strmake(pager_name, param, sizeof(pager_name)-1);
3506
3401
    while (end > pager_name && (my_isspace(charset_info,end[-1]) ||
3507
3402
                                my_iscntrl(charset_info,end[-1])))
3508
3403
      end--;
3509
3404
    end[0]=0;
3510
 
    strcpy(pager, pager_name);
3511
 
    strcpy(default_pager, pager_name);
 
3405
    stpcpy(pager, pager_name);
 
3406
    stpcpy(default_pager, pager_name);
3512
3407
  }
3513
3408
  opt_nopager=0;
3514
3409
  tee_fprintf(stdout, "PAGER set to '%s'\n", pager);
3517
3412
 
3518
3413
 
3519
3414
static int
3520
 
com_nopager(string *, const char *)
 
3415
com_nopager(string *buffer __attribute__((unused)),
 
3416
            const char *line __attribute__((unused)))
3521
3417
{
3522
 
  strcpy(pager, "stdout");
 
3418
  stpcpy(pager, "stdout");
3523
3419
  opt_nopager=1;
3524
3420
  PAGER= stdout;
3525
3421
  tee_fprintf(stdout, "PAGER set to stdout\n");
3529
3425
/* If arg is given, exit without errors. This happens on command 'quit' */
3530
3426
 
3531
3427
static int
3532
 
com_quit(string *, const char *)
 
3428
com_quit(string *buffer __attribute__((unused)),
 
3429
         const char *line __attribute__((unused)))
3533
3430
{
3534
3431
  /* let the screen auto close on a normal shutdown */
3535
3432
  status.exit_status=0;
3537
3434
}
3538
3435
 
3539
3436
static int
3540
 
com_rehash(string *, const char *)
 
3437
com_rehash(string *buffer __attribute__((unused)),
 
3438
           const char *line __attribute__((unused)))
3541
3439
{
3542
3440
  build_completion_hash(1, 0);
3543
3441
  return 0;
3546
3444
 
3547
3445
 
3548
3446
static int
3549
 
com_print(string *buffer,const char *)
 
3447
com_print(string *buffer,const char *line __attribute__((unused)))
3550
3448
{
3551
3449
  tee_puts("--------------", stdout);
3552
3450
  (void) tee_fputs(buffer->c_str(), stdout);
3573
3471
      Two null bytes are needed in the end of buff to allow
3574
3472
      get_arg to find end of string the second time it's called.
3575
3473
    */
3576
 
    tmp= strncpy(buff, line, sizeof(buff)-2);
 
3474
    tmp= strmake(buff, line, sizeof(buff)-2);
3577
3475
#ifdef EXTRA_DEBUG
3578
3476
    tmp[1]= 0;
3579
3477
#endif
3580
3478
    tmp= get_arg(buff, 0);
3581
3479
    if (tmp && *tmp)
3582
3480
    {
3583
 
      free(current_db);
 
3481
      my_free(current_db, MYF(MY_ALLOW_ZERO_PTR));
3584
3482
      current_db= strdup(tmp);
3585
3483
      tmp= get_arg(buff, 1);
3586
3484
      if (tmp)
3587
3485
      {
3588
 
        free(current_host);
 
3486
        my_free(current_host,MYF(MY_ALLOW_ZERO_PTR));
3589
3487
        current_host=strdup(tmp);
3590
3488
      }
3591
3489
    }
3592
3490
    else
3593
3491
    {
3594
3492
      /* Quick re-connect */
3595
 
      opt_rehash= 0;
 
3493
      opt_rehash= 0;                            /* purecov: tested */
3596
3494
    }
3597
3495
    // command used
3598
3496
    assert(buffer!=NULL);
3605
3503
 
3606
3504
  if (connected)
3607
3505
  {
3608
 
    snprintf(buff, sizeof(buff), "Connection id:    %u",drizzle_con_thread_id(&con));
 
3506
    sprintf(buff,"Connection id:    %u",drizzle_thread_id(&drizzle));
3609
3507
    put_info(buff,INFO_INFO,0,0);
3610
 
    snprintf(buff, sizeof(buff), "Current database: %.128s\n",
3611
 
             current_db ? current_db : "*** NONE ***");
 
3508
    sprintf(buff,"Current database: %.128s\n",
 
3509
            current_db ? current_db : "*** NONE ***");
3612
3510
    put_info(buff,INFO_INFO,0,0);
3613
3511
  }
3614
3512
  return error;
3615
3513
}
3616
3514
 
3617
3515
 
3618
 
static int com_source(string *, const char *line)
 
3516
static int com_source(string *buffer __attribute__((unused)), const char *line)
3619
3517
{
3620
 
  char source_name[FN_REFLEN], *end;
3621
 
  const char *param;
3622
 
  LineBuffer *line_buff;
 
3518
  char source_name[FN_REFLEN], *end, *param;
 
3519
  LINE_BUFFER *line_buff;
3623
3520
  int error;
3624
3521
  STATUS old_status;
3625
3522
  FILE *sql_file;
3632
3529
                    INFO_ERROR, 0,0);
3633
3530
  while (my_isspace(charset_info,*param))
3634
3531
    param++;
3635
 
  end= strncpy(source_name,param,sizeof(source_name)-1);
3636
 
  end+= strlen(source_name);
 
3532
  end=strmake(source_name,param,sizeof(source_name)-1);
3637
3533
  while (end > source_name && (my_isspace(charset_info,end[-1]) ||
3638
3534
                               my_iscntrl(charset_info,end[-1])))
3639
3535
    end--;
3640
3536
  end[0]=0;
3641
 
  internal::unpack_filename(source_name,source_name);
 
3537
  unpack_filename(source_name,source_name);
3642
3538
  /* open file name */
3643
 
  if (!(sql_file = fopen(source_name, "r")))
 
3539
  if (!(sql_file = my_fopen(source_name, O_RDONLY | O_BINARY,MYF(0))))
3644
3540
  {
3645
3541
    char buff[FN_REFLEN+60];
3646
 
    snprintf(buff, sizeof(buff), "Failed to open file '%s', error: %d", source_name,errno);
 
3542
    sprintf(buff,"Failed to open file '%s', error: %d", source_name,errno);
3647
3543
    return put_info(buff, INFO_ERROR, 0 ,0);
3648
3544
  }
3649
3545
 
3650
 
  line_buff= new(std::nothrow) LineBuffer(opt_max_input_line,sql_file);
3651
 
  if (line_buff == NULL)
 
3546
  if (!(line_buff=batch_readline_init(opt_max_allowed_packet+512,sql_file)))
3652
3547
  {
3653
 
    fclose(sql_file);
3654
 
    return put_info("Can't initialize LineBuffer", INFO_ERROR, 0, 0);
 
3548
    my_fclose(sql_file,MYF(0));
 
3549
    return put_info("Can't initialize batch_readline", INFO_ERROR, 0 ,0);
3655
3550
  }
3656
3551
 
3657
3552
  /* Save old status */
3668
3563
  error= read_and_execute(false);
3669
3564
  // Continue as before
3670
3565
  status=old_status;
3671
 
  fclose(sql_file);
3672
 
  delete status.line_buff;
3673
 
  line_buff= status.line_buff= 0;
 
3566
  my_fclose(sql_file,MYF(0));
 
3567
  batch_readline_end(line_buff);
3674
3568
  return error;
3675
3569
}
3676
3570
 
3677
3571
 
3678
3572
/* ARGSUSED */
3679
3573
static int
3680
 
com_delimiter(string *, const char *line)
 
3574
com_delimiter(string *buffer __attribute__((unused)), const char *line)
3681
3575
{
3682
3576
  char buff[256], *tmp;
3683
3577
 
3684
 
  strncpy(buff, line, sizeof(buff) - 1);
 
3578
  strmake(buff, line, sizeof(buff) - 1);
3685
3579
  tmp= get_arg(buff, 0);
3686
3580
 
3687
3581
  if (!tmp || !*tmp)
3699
3593
      return 0;
3700
3594
    }
3701
3595
  }
3702
 
  strncpy(delimiter, tmp, sizeof(delimiter) - 1);
 
3596
  strmake(delimiter, tmp, sizeof(delimiter) - 1);
3703
3597
  delimiter_length= (int)strlen(delimiter);
3704
3598
  delimiter_str= delimiter;
3705
3599
  return 0;
3707
3601
 
3708
3602
/* ARGSUSED */
3709
3603
static int
3710
 
com_use(string *, const char *line)
 
3604
com_use(string *buffer __attribute__((unused)), const char *line)
3711
3605
{
3712
3606
  char *tmp, buff[FN_REFLEN + 1];
3713
3607
  int select_db;
3714
 
  drizzle_result_st result;
3715
 
  drizzle_return_t ret;
3716
3608
 
3717
3609
  memset(buff, 0, sizeof(buff));
3718
 
  strncpy(buff, line, sizeof(buff) - 1);
 
3610
  strmake(buff, line, sizeof(buff) - 1);
3719
3611
  tmp= get_arg(buff, 0);
3720
3612
  if (!tmp || !*tmp)
3721
3613
  {
3729
3621
  */
3730
3622
  get_current_db();
3731
3623
 
3732
 
  if (!current_db || strcmp(current_db,tmp))
 
3624
  if (!current_db || cmp_database(charset_info, current_db,tmp))
3733
3625
  {
3734
3626
    if (one_database)
3735
3627
    {
3736
3628
      skip_updates= 1;
3737
 
      select_db= 0;    // don't do drizzleclient_select_db()
 
3629
      select_db= 0;    // don't do drizzle_select_db()
3738
3630
    }
3739
3631
    else
3740
 
      select_db= 2;    // do drizzleclient_select_db() and build_completion_hash()
 
3632
      select_db= 2;    // do drizzle_select_db() and build_completion_hash()
3741
3633
  }
3742
3634
  else
3743
3635
  {
3744
3636
    /*
3745
3637
      USE to the current db specified.
3746
 
      We do need to send drizzleclient_select_db() to make server
 
3638
      We do need to send drizzle_select_db() to make server
3747
3639
      update database level privileges, which might
3748
3640
      change since last USE (see bug#10979).
3749
3641
      For performance purposes, we'll skip rebuilding of completion hash.
3750
3642
    */
3751
3643
    skip_updates= 0;
3752
 
    select_db= 1;      // do only drizzleclient_select_db(), without completion
 
3644
    select_db= 1;      // do only drizzle_select_db(), without completion
3753
3645
  }
3754
3646
 
3755
3647
  if (select_db)
3760
3652
    */
3761
3653
    if (!connected && reconnect())
3762
3654
      return opt_reconnect ? -1 : 1;                        // Fatal error
3763
 
    for (bool try_again= true; try_again; try_again= false)
 
3655
    if (drizzle_select_db(&drizzle,tmp))
3764
3656
    {
3765
 
      if (drizzle_select_db(&con,&result,tmp,&ret) == NULL ||
3766
 
          ret != DRIZZLE_RETURN_OK)
3767
 
      {
3768
 
        if (ret == DRIZZLE_RETURN_ERROR_CODE)
3769
 
        {
3770
 
          int error= put_error(&con, &result);
3771
 
          drizzle_result_free(&result);
3772
 
          return error;
3773
 
        }
3774
 
 
3775
 
        if (ret != DRIZZLE_RETURN_SERVER_GONE || !try_again)
3776
 
          return put_error(&con, NULL);
3777
 
 
3778
 
        if (reconnect())
3779
 
          return opt_reconnect ? -1 : 1;                      // Fatal error
3780
 
      }
3781
 
      else
3782
 
        drizzle_result_free(&result);
 
3657
      if (drizzle_errno(&drizzle) != CR_SERVER_GONE_ERROR)
 
3658
        return put_error(&drizzle);
 
3659
 
 
3660
      if (reconnect())
 
3661
        return opt_reconnect ? -1 : 1;                      // Fatal error
 
3662
      if (drizzle_select_db(&drizzle,tmp))
 
3663
        return put_error(&drizzle);
3783
3664
    }
3784
 
    free(current_db);
 
3665
    my_free(current_db,MYF(MY_ALLOW_ZERO_PTR));
3785
3666
    current_db= strdup(tmp);
3786
3667
    if (select_db > 1)
3787
3668
      build_completion_hash(opt_rehash, 1);
3792
3673
}
3793
3674
 
3794
3675
static int
3795
 
com_warnings(string *, const char *)
 
3676
com_warnings(string *buffer __attribute__((unused)),
 
3677
             const char *line __attribute__((unused)))
3796
3678
{
3797
3679
  show_warnings = 1;
3798
3680
  put_info("Show warnings enabled.",INFO_INFO, 0, 0);
3800
3682
}
3801
3683
 
3802
3684
static int
3803
 
com_nowarnings(string *, const char *)
 
3685
com_nowarnings(string *buffer __attribute__((unused)),
 
3686
               const char *line __attribute__((unused)))
3804
3687
{
3805
3688
  show_warnings = 0;
3806
3689
  put_info("Show warnings disabled.",INFO_INFO, 0, 0);
3842
3725
        ptr++;
3843
3726
  }
3844
3727
  if (!*ptr)
3845
 
    return NULL;
 
3728
    return NullS;
3846
3729
  while (my_isspace(charset_info, *ptr))
3847
3730
    ptr++;
3848
3731
  if (*ptr == '\'' || *ptr == '\"' || *ptr == '`')
3856
3739
    if (*ptr == '\\' && ptr[1]) // escaped character
3857
3740
    {
3858
3741
      // Remove the backslash
3859
 
      strcpy(ptr, ptr+1);
 
3742
      stpcpy(ptr, ptr+1);
3860
3743
    }
3861
3744
    else if ((!quoted && *ptr == ' ') || (quoted && *ptr == qtype))
3862
3745
    {
3865
3748
    }
3866
3749
  }
3867
3750
  valid_arg= ptr != start;
3868
 
  return valid_arg ? start : NULL;
 
3751
  return valid_arg ? start : NullS;
3869
3752
}
3870
3753
 
3871
3754
 
3872
3755
static int
3873
3756
sql_connect(char *host,char *database,char *user,char *password,
3874
 
            uint32_t silent)
 
3757
                 uint silent)
3875
3758
{
3876
 
  drizzle_return_t ret;
3877
 
 
3878
3759
  if (connected)
3879
3760
  {
3880
3761
    connected= 0;
3881
 
    drizzle_con_free(&con);
3882
 
    drizzle_free(&drizzle);
 
3762
    drizzle_close(&drizzle);
3883
3763
  }
3884
3764
  drizzle_create(&drizzle);
3885
 
  if (drizzle_con_add_tcp(&drizzle, &con, host, opt_drizzle_port, user,
3886
 
                          password, database, opt_mysql ? DRIZZLE_CON_MYSQL : DRIZZLE_CON_NONE) == NULL)
 
3765
  if (opt_connect_timeout)
3887
3766
  {
3888
 
    (void) put_error(&con, NULL);
3889
 
    (void) fflush(stdout);
3890
 
    return 1;
 
3767
    uint timeout=opt_connect_timeout;
 
3768
    drizzle_options(&drizzle,DRIZZLE_OPT_CONNECT_TIMEOUT,
 
3769
                  (char*) &timeout);
3891
3770
  }
3892
 
 
3893
 
  /* XXX add this back in
3894
 
    if (opt_connect_timeout)
3895
 
    {
3896
 
    uint32_t timeout=opt_connect_timeout;
3897
 
    drizzleclient_options(&drizzle,DRIZZLE_OPT_CONNECT_TIMEOUT,
3898
 
    (char*) &timeout);
3899
 
    }
3900
 
  */
3901
 
 
3902
 
  /* XXX Do we need this?
3903
 
    if (safe_updates)
3904
 
    {
 
3771
  if (opt_compress)
 
3772
    drizzle_options(&drizzle,DRIZZLE_OPT_COMPRESS,NullS);
 
3773
  if (opt_secure_auth)
 
3774
    drizzle_options(&drizzle, DRIZZLE_SECURE_AUTH, (char *) &opt_secure_auth);
 
3775
  if (using_opt_local_infile)
 
3776
    drizzle_options(&drizzle,DRIZZLE_OPT_LOCAL_INFILE, (char*) &opt_local_infile);
 
3777
  drizzle_options(&drizzle,DRIZZLE_OPT_PROTOCOL,(char*)&opt_protocol);
 
3778
  if (safe_updates)
 
3779
  {
3905
3780
    char init_command[100];
3906
3781
    sprintf(init_command,
3907
 
    "SET SQL_SAFE_UPDATES=1,SQL_SELECT_LIMIT=%"PRIu32
3908
 
    ",MAX_JOIN_SIZE=%"PRIu32,
3909
 
    select_limit, max_join_size);
3910
 
    drizzleclient_options(&drizzle, DRIZZLE_INIT_COMMAND, init_command);
3911
 
    }
3912
 
  */
3913
 
  if ((ret= drizzle_con_connect(&con)) != DRIZZLE_RETURN_OK)
 
3782
            "SET SQL_SAFE_UPDATES=1,SQL_SELECT_LIMIT=%"PRIu32
 
3783
            ",SQL_MAX_JOIN_SIZE=%"PRIu32,
 
3784
            select_limit, max_join_size);
 
3785
    drizzle_options(&drizzle, DRIZZLE_INIT_COMMAND, init_command);
 
3786
  }
 
3787
  if (default_charset_used)
 
3788
    drizzle_options(&drizzle, DRIZZLE_SET_CHARSET_NAME, default_charset);
 
3789
  if (!drizzle_connect(&drizzle, host, user, password,
 
3790
                          database, opt_drizzle_port, opt_drizzle_unix_port,
 
3791
                          connect_flag | CLIENT_MULTI_STATEMENTS))
3914
3792
  {
3915
 
    if (!silent || (ret != DRIZZLE_RETURN_GETADDRINFO &&
3916
 
                    ret != DRIZZLE_RETURN_COULD_NOT_CONNECT))
 
3793
    if (!silent ||
 
3794
        (drizzle_errno(&drizzle) != CR_CONN_HOST_ERROR &&
 
3795
         drizzle_errno(&drizzle) != CR_CONNECTION_ERROR))
3917
3796
    {
3918
 
      (void) put_error(&con, NULL);
 
3797
      (void) put_error(&drizzle);
3919
3798
      (void) fflush(stdout);
3920
3799
      return ignore_errors ? -1 : 1;    // Abort
3921
3800
    }
3922
3801
    return -1;          // Retryable
3923
3802
  }
3924
3803
  connected=1;
3925
 
 
 
3804
  drizzle.reconnect= debug_info_flag; // We want to know if this happens
3926
3805
  build_completion_hash(opt_rehash, 1);
3927
3806
  return 0;
3928
3807
}
3929
3808
 
3930
3809
 
3931
3810
static int
3932
 
com_status(string *, const char *)
 
3811
com_status(string *buffer __attribute__((unused)),
 
3812
           const char *line __attribute__((unused)))
3933
3813
{
3934
 
  /*
3935
 
    char buff[40];
3936
 
    uint64_t id;
3937
 
  */
3938
 
  drizzle_result_st result;
3939
 
  drizzle_return_t ret;
 
3814
  char buff[40];
 
3815
  uint64_t id;
 
3816
  DRIZZLE_RES *result;
3940
3817
 
3941
3818
  tee_puts("--------------", stdout);
3942
3819
  usage(1);          /* Print version */
3943
3820
  if (connected)
3944
3821
  {
3945
 
    tee_fprintf(stdout, "\nConnection id:\t\t%lu\n",drizzle_con_thread_id(&con));
 
3822
    tee_fprintf(stdout, "\nConnection id:\t\t%lu\n",drizzle_thread_id(&drizzle));
3946
3823
    /*
3947
3824
      Don't remove "limit 1",
3948
3825
      it is protection againts SQL_SELECT_LIMIT=0
3949
3826
    */
3950
 
    if (drizzle_query_str(&con,&result,"select DATABASE(), USER() limit 1",
3951
 
                          &ret) != NULL && ret == DRIZZLE_RETURN_OK &&
3952
 
        drizzle_result_buffer(&result) == DRIZZLE_RETURN_OK)
 
3827
    if (!drizzle_query(&drizzle,"select DATABASE(), USER() limit 1") &&
 
3828
        (result=drizzle_use_result(&drizzle)))
3953
3829
    {
3954
 
      drizzle_row_t cur=drizzle_row_next(&result);
 
3830
      DRIZZLE_ROW cur=drizzle_fetch_row(result);
3955
3831
      if (cur)
3956
3832
      {
3957
3833
        tee_fprintf(stdout, "Current database:\t%s\n", cur[0] ? cur[0] : "");
3958
3834
        tee_fprintf(stdout, "Current user:\t\t%s\n", cur[1]);
3959
3835
      }
3960
 
      drizzle_result_free(&result);
 
3836
      drizzle_free_result(result);
3961
3837
    }
3962
 
    else if (ret == DRIZZLE_RETURN_ERROR_CODE)
3963
 
      drizzle_result_free(&result);
3964
3838
    tee_puts("SSL:\t\t\tNot in use", stdout);
3965
3839
  }
3966
3840
  else
3979
3853
  tee_fprintf(stdout, "Current pager:\t\t%s\n", pager);
3980
3854
  tee_fprintf(stdout, "Using outfile:\t\t'%s'\n", opt_outfile ? outfile : "");
3981
3855
  tee_fprintf(stdout, "Using delimiter:\t%s\n", delimiter);
3982
 
  tee_fprintf(stdout, "Server version:\t\t%s\n", server_version_string(&con));
3983
 
  tee_fprintf(stdout, "Protocol version:\t%d\n", drizzle_con_protocol_version(&con));
3984
 
  tee_fprintf(stdout, "Connection:\t\t%s\n", drizzle_con_host(&con));
3985
 
  /* XXX need to save this from result
3986
 
    if ((id= drizzleclient_insert_id(&drizzle)))
3987
 
    tee_fprintf(stdout, "Insert id:\t\t%s\n", internal::llstr(id, buff));
3988
 
  */
3989
 
 
3990
 
  if (drizzle_con_uds(&con))
3991
 
    tee_fprintf(stdout, "UNIX socket:\t\t%s\n", drizzle_con_uds(&con));
3992
 
  else
3993
 
    tee_fprintf(stdout, "TCP port:\t\t%d\n", drizzle_con_port(&con));
 
3856
  tee_fprintf(stdout, "Server version:\t\t%s\n", server_version_string(&drizzle));
 
3857
  tee_fprintf(stdout, "Protocol version:\t%d\n", drizzle_get_proto_info(&drizzle));
 
3858
  tee_fprintf(stdout, "Connection:\t\t%s\n", drizzle_get_host_info(&drizzle));
 
3859
  if ((id= drizzle_insert_id(&drizzle)))
 
3860
    tee_fprintf(stdout, "Insert id:\t\t%s\n", llstr(id, buff));
 
3861
 
 
3862
  /* "limit 1" is protection against SQL_SELECT_LIMIT=0 */
 
3863
  if (!drizzle_query(&drizzle,"select @@character_set_client, @@character_set_connection, @@character_set_server, @@character_set_database limit 1") &&
 
3864
      (result=drizzle_use_result(&drizzle)))
 
3865
  {
 
3866
    DRIZZLE_ROW cur=drizzle_fetch_row(result);
 
3867
    if (cur)
 
3868
    {
 
3869
      tee_fprintf(stdout, "Server characterset:\t%s\n", cur[2] ? cur[2] : "");
 
3870
      tee_fprintf(stdout, "Db     characterset:\t%s\n", cur[3] ? cur[3] : "");
 
3871
      tee_fprintf(stdout, "Client characterset:\t%s\n", cur[0] ? cur[0] : "");
 
3872
      tee_fprintf(stdout, "Conn.  characterset:\t%s\n", cur[1] ? cur[1] : "");
 
3873
    }
 
3874
    drizzle_free_result(result);
 
3875
  }
 
3876
  else
 
3877
  {
 
3878
    /* Probably pre-4.1 server */
 
3879
    tee_fprintf(stdout, "Client characterset:\t%s\n", charset_info->csname);
 
3880
    tee_fprintf(stdout, "Server characterset:\t%s\n", drizzle.charset->csname);
 
3881
  }
 
3882
 
 
3883
  if (strstr(drizzle_get_host_info(&drizzle),"TCP/IP") || ! drizzle.unix_socket)
 
3884
    tee_fprintf(stdout, "TCP port:\t\t%d\n", drizzle.port);
 
3885
  else
 
3886
    tee_fprintf(stdout, "UNIX socket:\t\t%s\n", drizzle.unix_socket);
 
3887
  if (drizzle.net.compress)
 
3888
    tee_fprintf(stdout, "Protocol:\t\tCompressed\n");
3994
3889
 
3995
3890
  if (safe_updates)
3996
3891
  {
3998
3893
    tee_fprintf(stdout, "\nNote that you are running in safe_update_mode:\n");
3999
3894
    vidattr(A_NORMAL);
4000
3895
    tee_fprintf(stdout, "\
4001
 
                UPDATEs and DELETEs that don't use a key in the WHERE clause are not allowed.\n\
4002
 
                (One can force an UPDATE/DELETE by adding LIMIT # at the end of the command.)\n \
4003
 
                SELECT has an automatic 'LIMIT %lu' if LIMIT is not used.\n             \
4004
 
                Max number of examined row combination in a join is set to: %lu\n\n",
 
3896
UPDATEs and DELETEs that don't use a key in the WHERE clause are not allowed.\n\
 
3897
(One can force an UPDATE/DELETE by adding LIMIT # at the end of the command.)\n \
 
3898
SELECT has an automatic 'LIMIT %lu' if LIMIT is not used.\n             \
 
3899
Max number of examined row combination in a join is set to: %lu\n\n",
4005
3900
                select_limit, max_join_size);
4006
3901
  }
4007
3902
  tee_puts("--------------\n", stdout);
4009
3904
}
4010
3905
 
4011
3906
static const char *
4012
 
server_version_string(drizzle_con_st *local_con)
 
3907
server_version_string(DRIZZLE *con)
4013
3908
{
4014
 
  static string buf("");
4015
 
  static bool server_version_string_reserved= false;
 
3909
  static char buf[MAX_SERVER_VERSION_LENGTH] = "";
4016
3910
 
4017
 
  if (!server_version_string_reserved)
4018
 
  {
4019
 
    buf.reserve(MAX_SERVER_VERSION_LENGTH);
4020
 
    server_version_string_reserved= true;
4021
 
  }
4022
3911
  /* Only one thread calls this, so no synchronization is needed */
4023
3912
  if (buf[0] == '\0')
4024
3913
  {
4025
 
    drizzle_result_st result;
4026
 
    drizzle_return_t ret;
 
3914
    char *bufp = buf;
 
3915
    DRIZZLE_RES *result;
4027
3916
 
4028
 
    buf.append(drizzle_con_server_version(local_con));
 
3917
    bufp= stpncpy(buf, drizzle_get_server_info(con), sizeof buf);
4029
3918
 
4030
3919
    /* "limit 1" is protection against SQL_SELECT_LIMIT=0 */
4031
 
    (void)drizzle_query_str(local_con, &result,
4032
 
                            "select @@version_comment limit 1", &ret);
4033
 
    if (ret == DRIZZLE_RETURN_OK &&
4034
 
        drizzle_result_buffer(&result) == DRIZZLE_RETURN_OK)
 
3920
    if (!drizzle_query(con, "select @@version_comment limit 1") &&
 
3921
        (result = drizzle_use_result(con)))
4035
3922
    {
4036
 
      drizzle_row_t cur = drizzle_row_next(&result);
 
3923
      DRIZZLE_ROW cur = drizzle_fetch_row(result);
4037
3924
      if (cur && cur[0])
4038
3925
      {
4039
 
        buf.append(" ");
4040
 
        buf.append(cur[0]);
 
3926
        bufp = strxnmov(bufp, sizeof buf - (bufp - buf), " ", cur[0], NullS);
4041
3927
      }
4042
 
      drizzle_result_free(&result);
 
3928
      drizzle_free_result(result);
4043
3929
    }
4044
 
    else if (ret == DRIZZLE_RETURN_ERROR_CODE)
4045
 
      drizzle_result_free(&result);
 
3930
 
 
3931
    /* str*nmov doesn't guarantee NUL-termination */
 
3932
    if (bufp == buf + sizeof buf)
 
3933
      buf[sizeof buf - 1] = '\0';
4046
3934
  }
4047
3935
 
4048
 
  return buf.c_str();
 
3936
  return buf;
4049
3937
}
4050
3938
 
4051
3939
static int
4052
 
put_info(const char *str,INFO_TYPE info_type, uint32_t error, const char *sqlstate)
 
3940
put_info(const char *str,INFO_TYPE info_type, uint error, const char *sqlstate)
4053
3941
{
4054
3942
  FILE *file= (info_type == INFO_ERROR ? stderr : stdout);
4055
3943
  static int inited=0;
4121
4009
 
4122
4010
 
4123
4011
static int
4124
 
put_error(drizzle_con_st *local_con, drizzle_result_st *res)
 
4012
put_error(DRIZZLE *con)
4125
4013
{
4126
 
  const char *error;
4127
 
 
4128
 
  if (res != NULL)
4129
 
  {
4130
 
    error= drizzle_result_error(res);
4131
 
    if (!strcmp(error, ""))
4132
 
      error= drizzle_con_error(local_con);
4133
 
  }
4134
 
  else
4135
 
    error= drizzle_con_error(local_con);
4136
 
 
4137
 
  return put_info(error, INFO_ERROR,
4138
 
                  res == NULL ? drizzle_con_error_code(local_con) :
4139
 
                  drizzle_result_error_code(res),
4140
 
                  res == NULL ? drizzle_con_sqlstate(local_con) :
4141
 
                  drizzle_result_sqlstate(res));
 
4014
  return put_info(drizzle_error(con), INFO_ERROR, drizzle_errno(con),
 
4015
                  drizzle_sqlstate(con));
4142
4016
}
4143
4017
 
4144
4018
 
4148
4022
  const char *end= start + (buffer->length());
4149
4023
  while (start < end && !my_isgraph(charset_info,end[-1]))
4150
4024
    end--;
4151
 
  uint32_t pos_to_truncate= (end-start);
 
4025
  uint pos_to_truncate= (end-start);
4152
4026
  if (buffer->length() > pos_to_truncate)
4153
4027
    buffer->erase(pos_to_truncate);
4154
4028
}
4211
4085
 
4212
4086
 
4213
4087
/**
4214
 
  Write as many as 52+1 bytes to buff, in the form of a legible
4215
 
  duration of time.
 
4088
   Write as many as 52+1 bytes to buff, in the form of a legible
 
4089
   duration of time.
4216
4090
 
4217
 
  len("4294967296 days, 23 hours, 59 minutes, 60.00 seconds")  ->  52
 
4091
   len("4294967296 days, 23 hours, 59 minutes, 60.00 seconds")  ->  52
4218
4092
*/
4219
4093
static void nice_time(double sec,char *buff,bool part_second)
4220
4094
{
4221
4095
  uint32_t tmp;
4222
 
  ostringstream tmp_buff_str;
4223
 
 
4224
4096
  if (sec >= 3600.0*24)
4225
4097
  {
4226
4098
    tmp=(uint32_t) floor(sec/(3600.0*24));
4227
 
    sec-= 3600.0*24*tmp;
4228
 
    tmp_buff_str << tmp;
4229
 
 
4230
 
    if (tmp > 1)
4231
 
      tmp_buff_str << " days ";
4232
 
    else
4233
 
      tmp_buff_str << " day ";
4234
 
 
 
4099
    sec-=3600.0*24*tmp;
 
4100
    buff=int10_to_str((long) tmp, buff, 10);
 
4101
    buff=stpcpy(buff,tmp > 1 ? " days " : " day ");
4235
4102
  }
4236
4103
  if (sec >= 3600.0)
4237
4104
  {
4238
4105
    tmp=(uint32_t) floor(sec/3600.0);
4239
4106
    sec-=3600.0*tmp;
4240
 
    tmp_buff_str << tmp;
4241
 
 
4242
 
    if (tmp > 1)
4243
 
      tmp_buff_str << " hours ";
4244
 
    else
4245
 
      tmp_buff_str << " hour ";
 
4107
    buff=int10_to_str((long) tmp, buff, 10);
 
4108
    buff=stpcpy(buff,tmp > 1 ? " hours " : " hour ");
4246
4109
  }
4247
4110
  if (sec >= 60.0)
4248
4111
  {
4249
4112
    tmp=(uint32_t) floor(sec/60.0);
4250
4113
    sec-=60.0*tmp;
4251
 
    tmp_buff_str << tmp << " min ";
 
4114
    buff=int10_to_str((long) tmp, buff, 10);
 
4115
    buff=stpcpy(buff," min ");
4252
4116
  }
4253
4117
  if (part_second)
4254
 
    tmp_buff_str.precision(2);
 
4118
    sprintf(buff,"%.2f sec",sec);
4255
4119
  else
4256
 
    tmp_buff_str.precision(0);
4257
 
  tmp_buff_str << sec << " sec";
4258
 
  strcpy(buff, tmp_buff_str.str().c_str());
 
4120
    sprintf(buff,"%d sec",(int) sec);
4259
4121
}
4260
4122
 
4261
4123
 
4271
4133
  buff[0]=' ';
4272
4134
  buff[1]='(';
4273
4135
  end_timer(start_time,buff+2);
4274
 
  strcpy(strchr(buff, '\0'),")");
 
4136
  stpcpy(strchr(buff, '\0'),")");
4275
4137
}
4276
4138
 
4277
4139
static const char * construct_prompt()
4295
4157
    {
4296
4158
      int getHour;
4297
4159
      int getYear;
4298
 
      /* Room for Dow MMM DD HH:MM:SS YYYY */ 
4299
 
      char dateTime[32];
 
4160
      char* dateTime= NULL;
4300
4161
      switch (*++c) {
4301
4162
      case '\0':
4302
4163
        // stop it from going beyond if ends with %
4307
4168
        break;
4308
4169
      case 'v':
4309
4170
        if (connected)
4310
 
          processed_prompt->append(drizzle_con_server_version(&con));
 
4171
          processed_prompt->append(drizzle_get_server_info(&drizzle));
4311
4172
        else
4312
4173
          processed_prompt->append("not_connected");
4313
4174
        break;
4315
4176
        processed_prompt->append(current_db ? current_db : "(none)");
4316
4177
        break;
4317
4178
      case 'h':
 
4179
      {
 
4180
        const char *prompt;
 
4181
        prompt= connected ? drizzle_get_host_info(&drizzle) : "not_connected";
 
4182
        if (strstr(prompt, "Localhost"))
 
4183
          processed_prompt->append("localhost");
 
4184
        else
4318
4185
        {
4319
 
          const char *prompt;
4320
 
          prompt= connected ? drizzle_con_host(&con) : "not_connected";
4321
 
          if (strstr(prompt, "Localhost"))
4322
 
            processed_prompt->append("localhost");
4323
 
          else
4324
 
          {
4325
 
            const char *end=strrchr(prompt,' ');
4326
 
            if (end != NULL)
4327
 
              processed_prompt->append(prompt, (end-prompt));
4328
 
          }
4329
 
          break;
 
4186
          const char *end=strrchr(prompt,' ');
 
4187
          if (end != NULL)
 
4188
            processed_prompt->append(prompt, (end-prompt));
4330
4189
        }
 
4190
        break;
 
4191
      }
4331
4192
      case 'p':
 
4193
      {
 
4194
        if (!connected)
4332
4195
        {
4333
 
          if (!connected)
4334
 
          {
4335
 
            processed_prompt->append("not_connected");
4336
 
            break;
4337
 
          }
 
4196
          processed_prompt->append("not_connected");
 
4197
          break;
 
4198
        }
4338
4199
 
4339
 
          if (drizzle_con_uds(&con))
4340
 
          {
4341
 
            const char *pos=strrchr(drizzle_con_uds(&con),'/');
4342
 
            processed_prompt->append(pos ? pos+1 : drizzle_con_uds(&con));
4343
 
          }
4344
 
          else
4345
 
            add_int_to_prompt(drizzle_con_port(&con));
4346
 
        }
4347
 
        break;
 
4200
        const char *host_info = drizzle_get_host_info(&drizzle);
 
4201
        if (strstr(host_info, "memory"))
 
4202
        {
 
4203
          processed_prompt->append(drizzle.host);
 
4204
        }
 
4205
        else if (strstr(host_info,"TCP/IP") ||
 
4206
                 !drizzle.unix_socket)
 
4207
          add_int_to_prompt(drizzle.port);
 
4208
        else
 
4209
        {
 
4210
          char *pos=strrchr(drizzle.unix_socket,'/');
 
4211
          processed_prompt->append(pos ? pos+1 : drizzle.unix_socket);
 
4212
        }
 
4213
      }
 
4214
      break;
4348
4215
      case 'U':
4349
4216
        if (!full_username)
4350
4217
          init_username();
4359
4226
        break;
4360
4227
      case PROMPT_CHAR:
4361
4228
        {
4362
 
          processed_prompt->append(PROMPT_CHAR, 1);
 
4229
          char c= PROMPT_CHAR;
 
4230
          processed_prompt->append(&c, 1);
4363
4231
        }
4364
4232
        break;
4365
4233
      case 'n':
4366
4234
        {
4367
 
          processed_prompt->append('\n', 1);
 
4235
          char c= '\n';
 
4236
          processed_prompt->append(&c, 1);
4368
4237
        }
4369
4238
        break;
4370
4239
      case ' ':
4371
4240
      case '_':
4372
4241
        {
4373
 
          processed_prompt->append(' ', 1);
 
4242
          char c= ' ';
 
4243
          processed_prompt->append(&c, 1);
4374
4244
        }
4375
4245
        break;
4376
4246
      case 'R':
4401
4271
        add_int_to_prompt(t->tm_year+1900);
4402
4272
        break;
4403
4273
      case 'D':
4404
 
        strftime(dateTime, 32, "%a %b %d %H:%M:%S %Y", localtime(&lclock));
4405
 
        processed_prompt->append(dateTime);
 
4274
        dateTime = ctime(&lclock);
 
4275
        processed_prompt->append(strtok(dateTime,"\n"));
4406
4276
        break;
4407
4277
      case 's':
4408
4278
        if (t->tm_sec < 10)
4447
4317
 
4448
4318
static void add_int_to_prompt(int toadd)
4449
4319
{
4450
 
  ostringstream buffer;
4451
 
  buffer << toadd;
4452
 
  processed_prompt->append(buffer.str().c_str());
 
4320
  char buffer[16];
 
4321
  int10_to_str(toadd, buffer, 10);
 
4322
  processed_prompt->append(buffer);
4453
4323
}
4454
4324
 
4455
4325
static void init_username()
4456
4326
{
4457
 
  /* XXX need this?
4458
 
    free(full_username);
4459
 
    free(part_username);
 
4327
  my_free(full_username,MYF(MY_ALLOW_ZERO_PTR));
 
4328
  my_free(part_username,MYF(MY_ALLOW_ZERO_PTR));
4460
4329
 
4461
 
    drizzle_result_st *result;
4462
 
    if (!drizzleclient_query(&drizzle,"select USER()") &&
4463
 
    (result=drizzleclient_use_result(&drizzle)))
4464
 
    {
4465
 
    drizzle_row_t cur=drizzleclient_fetch_row(result);
 
4330
  DRIZZLE_RES *result;
 
4331
  if (!drizzle_query(&drizzle,"select USER()") &&
 
4332
      (result=drizzle_use_result(&drizzle)))
 
4333
  {
 
4334
    DRIZZLE_ROW cur=drizzle_fetch_row(result);
4466
4335
    full_username= strdup(cur[0]);
4467
4336
    part_username= strdup(strtok(cur[0],"@"));
4468
 
    (void) drizzleclient_fetch_row(result);        // Read eof
4469
 
    }
4470
 
  */
 
4337
    (void) drizzle_fetch_row(result);        // Read eof
 
4338
  }
4471
4339
}
4472
4340
 
4473
 
static int com_prompt(string *, const char *line)
 
4341
static int com_prompt(string *buffer __attribute__((unused)),
 
4342
                      const char *line)
4474
4343
{
4475
 
  const char *ptr=strchr(line, ' ');
4476
 
  if (ptr == NULL)
 
4344
  char *ptr=strchr(line, ' ');
 
4345
  prompt_counter = 0;
 
4346
  my_free(current_prompt, MYF(MY_ALLOW_ZERO_PTR));
 
4347
  current_prompt= strdup(ptr ? ptr+1 : default_prompt);
 
4348
  if (!ptr)
4477
4349
    tee_fprintf(stdout, "Returning to default PROMPT of %s\n",
4478
4350
                default_prompt);
4479
 
  prompt_counter = 0;
4480
 
  char * tmpptr= strdup(ptr ? ptr+1 : default_prompt);
4481
 
  if (tmpptr == NULL)
4482
 
    tee_fprintf(stdout, "Memory allocation error. Not changing prompt\n");
4483
4351
  else
4484
 
  {
4485
 
    free(current_prompt);
4486
 
    current_prompt= tmpptr;
4487
4352
    tee_fprintf(stdout, "PROMPT set to '%s'\n", current_prompt);
4488
 
  }
4489
4353
  return 0;
4490
4354
}
4491
4355
 
4492
4356
/*
4493
 
  strcont(str, set) if str contanies any character in the string set.
4494
 
  The result is the position of the first found character in str, or NULL
4495
 
  if there isn't anything found.
 
4357
    strcont(str, set) if str contanies any character in the string set.
 
4358
    The result is the position of the first found character in str, or NullS
 
4359
    if there isn't anything found.
4496
4360
*/
4497
4361
 
4498
4362
static const char * strcont(register const char *str, register const char *set)