~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to client/drizzle.cc

  • Committer: Brian Aker
  • Date: 2008-08-16 15:41:14 UTC
  • mto: This revision was merged to the branch mainline in revision 346.
  • Revision ID: brian@tangent.org-20080816154114-eufmwf31p6ie1nd6
Cleaned up depend in Proto utils. Modified int to bool. Put TmpTable class
into play.

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 <algorithm>
39
39
#include <mystrings/m_ctype.h>
40
40
#include <stdarg.h>
41
 
#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"
42
46
#include <signal.h>
 
47
#include <vio/violite.h>
43
48
#include <sys/ioctl.h>
44
 
#include <drizzled/configmake.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);
45
70
 
46
71
#if defined(HAVE_CURSES_H) && defined(HAVE_TERM_H)
47
72
#include <curses.h>
48
 
#ifdef __sun
49
 
#undef clear
50
 
#undef erase
51
 
#endif
52
73
#include <term.h>
53
74
#else
54
75
#if defined(HAVE_TERMIOS_H)
59
80
#elif defined(HAVE_ASM_TERMBITS_H) && (!defined __GLIBC__ || !(__GLIBC__ > 2 || __GLIBC__ == 2 && __GLIBC_MINOR__ > 0))
60
81
#include <asm/termbits.h>    // Standard linux
61
82
#endif
 
83
#undef VOID
62
84
#if defined(HAVE_TERMCAP_H)
63
85
#include <termcap.h>
64
86
#else
72
94
#endif
73
95
#endif
74
96
 
75
 
#ifdef HAVE_LIBREADLINE
76
 
#  if defined(HAVE_READLINE_READLINE_H)
77
 
#    include <readline/readline.h>
78
 
#  elif defined(HAVE_READLINE_H)
79
 
#    include <readline.h>
80
 
#  else /* !defined(HAVE_READLINE_H) */
81
 
extern char *readline ();
82
 
#  endif /* !defined(HAVE_READLINE_H) */
83
 
char *cmdline = NULL;
84
 
#else /* !defined(HAVE_READLINE_READLINE_H) */
85
 
  /* no readline */
86
 
#  error Readline Required
87
 
#endif /* HAVE_LIBREADLINE */
 
97
#undef bcmp                             // Fix problem with new readline
88
98
 
89
 
#ifdef HAVE_READLINE_HISTORY
90
 
#  if defined(HAVE_READLINE_HISTORY_H)
91
 
#    include <readline/history.h>
92
 
#  elif defined(HAVE_HISTORY_H)
93
 
#    include <history.h>
94
 
#  else /* !defined(HAVE_HISTORY_H) */
95
 
extern void add_history ();
96
 
extern int write_history ();
97
 
extern int read_history ();
98
 
#  endif /* defined(HAVE_READLINE_HISTORY_H) */
99
 
    /* no history */
100
 
#endif /* HAVE_READLINE_HISTORY */
 
99
#ifdef HAVE_READLINE_HISTORY_H
 
100
#include <readline/history.h>
 
101
#endif
 
102
#include <readline/readline.h>
101
103
 
102
104
/**
103
105
 Make the old readline interface look like the new one.
104
106
*/
105
 
#ifndef HAVE_RL_COMPLETION
106
 
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;
107
110
#define rl_completion_matches(str, func) \
108
111
  completion_matches((char *)str, (CPFunction *)func)
109
112
#endif
110
113
 
111
 
#ifdef HAVE_RL_COMPENTRY
112
 
# ifdef HAVE_WORKING_RL_COMPENTRY
113
 
typedef rl_compentry_func_t drizzle_compentry_func_t;
114
 
# else
115
 
/* Snow Leopard ships an rl_compentry which cannot be assigned to
116
 
 * rl_completion_entry_function. We must undo the complete and total
117
 
 * ass-bagery.
118
 
 */
119
 
typedef Function drizzle_compentry_func_t;
120
 
# endif
121
 
#else
122
 
typedef Function drizzle_compentry_func_t;
123
 
#endif
124
 
 
125
 
#if defined(HAVE_LOCALE_H)
126
 
#include <locale.h>
127
 
#endif
128
 
 
129
 
#include <drizzled/gettext.h>
130
 
 
131
 
 
132
 
void* sql_alloc(unsigned size);       // Don't use drizzled alloc for these
133
 
void sql_element_free(void *ptr);
134
 
 
135
114
 
136
115
#if !defined(HAVE_VIDATTR)
137
116
#undef vidattr
138
117
#define vidattr(A) {}      // Can't get this to work
139
118
#endif
140
119
 
141
 
#include <iostream>
142
 
#include <map>
 
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"
143
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
 
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;
191
162
static bool column_types_flag;
192
 
static bool preserve_comments= false;
193
 
static uint32_t opt_max_input_line, opt_drizzle_port= 0;
194
 
static int verbose= 0, opt_silent= 0, opt_local_infile= 0;
195
 
static char * opt_drizzle_unix_port= NULL;
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",
 
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",
212
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
 
221
 
static const CHARSET_INFO *charset_info= &my_charset_utf8_general_ci;
 
196
static uint opt_protocol= DRIZZLE_PROTOCOL_TCP;
 
197
static const CHARSET_INFO *charset_info= &my_charset_latin1;
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);
236
 
extern "C" bool get_one_option(int optid, const struct my_option *opt,
237
 
                               char *argument);
 
210
bool get_one_option(int optid, const struct my_option *opt,
 
211
                    char *argument);
238
212
static int com_quit(string *str,const char*),
239
213
  com_go(string *str,const char*), com_ego(string *str,const char*),
240
214
  com_print(string *str,const char*),
242
216
  com_connect(string *str,const char*), com_status(string *str,const char*),
243
217
  com_use(string *str,const char*), com_source(string *str, const char*),
244
218
  com_rehash(string *str, const char*), com_tee(string *str, const char*),
245
 
  com_notee(string *str, const char*),
 
219
  com_notee(string *str, const char*), com_charset(string *str,const char*),
246
220
  com_prompt(string *str, const char*), com_delimiter(string *str, const char*),
247
221
  com_warnings(string *str, const char*), com_nowarnings(string *str, const char*),
248
222
  com_nopager(string *str, const char*), com_pager(string *str, const char*);
249
223
 
250
224
static int read_and_execute(bool interactive);
251
225
static int sql_connect(char *host,char *database,char *user,char *password,
252
 
                       uint32_t silent);
253
 
static const char *server_version_string(drizzle_con_st *con);
254
 
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,
255
229
                    const char *sql_state);
256
 
static int put_error(drizzle_con_st *con, drizzle_result_st *res);
 
230
static int put_error(DRIZZLE *drizzle);
257
231
static void safe_put_field(const char *pos,uint32_t length);
258
232
static void init_pager(void);
259
233
static void end_pager(void);
263
237
static char *get_arg(char *line, bool get_next_arg);
264
238
static void init_username(void);
265
239
static void add_int_to_prompt(int toadd);
266
 
static int get_result_width(drizzle_result_st *res);
267
 
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);
268
242
static const char * strcont(register const char *str, register const char *set);
269
243
 
270
244
/* A structure which contains information on the commands this program
279
253
 
280
254
 
281
255
static COMMANDS commands[] = {
282
 
  { "?",      '?', com_help,   0, N_("Synonym for `help'.") },
 
256
  { "?",      '?', com_help,   1, N_("Synonym for `help'.") },
283
257
  { "clear",  'c', com_clear,  0, N_("Clear command.")},
284
258
  { "connect",'r', com_connect,1,
285
 
    N_("Reconnect to the server. Optional arguments are db and host.")},
 
259
    N_("Reconnect to the server. Optional arguments are db and host." }),
286
260
  { "delimiter", 'd', com_delimiter,    1,
287
261
    N_("Set statement delimiter. NOTE: Takes the rest of the line as new delimiter.") },
288
262
  { "ego",    'G', com_ego,    0,
289
263
    N_("Send command to drizzle server, display result vertically.")},
290
264
  { "exit",   'q', com_quit,   0, N_("Exit drizzle. Same as quit.")},
291
265
  { "go",     'g', com_go,     0, N_("Send command to drizzle server.") },
292
 
  { "help",   'h', com_help,   0, N_("Display this help.") },
 
266
  { "help",   'h', com_help,   1, N_("Display this help.") },
293
267
  { "nopager",'n', com_nopager,0, N_("Disable pager, print to stdout.") },
294
268
  { "notee",  't', com_notee,  0, N_("Don't write into outfile.") },
295
269
  { "pager",  'P', com_pager,  1,
305
279
    N_("Set outfile [to_outfile]. Append everything into given outfile.") },
306
280
  { "use",    'u', com_use,    1,
307
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.") },
308
284
  { "warnings", 'W', com_warnings,  0,
309
285
    N_("Show warnings after every statement.") },
310
286
  { "nowarning", 'w', com_nowarnings, 0,
395
371
  { "CROSS", 0, 0, 0, ""},
396
372
  { "CUBE", 0, 0, 0, ""},
397
373
  { "CURRENT_DATE", 0, 0, 0, ""},
 
374
  { "CURRENT_TIME", 0, 0, 0, ""},
398
375
  { "CURRENT_TIMESTAMP", 0, 0, 0, ""},
399
376
  { "CURRENT_USER", 0, 0, 0, ""},
400
377
  { "CURSOR", 0, 0, 0, ""},
473
450
  { "FULL", 0, 0, 0, ""},
474
451
  { "FULLTEXT", 0, 0, 0, ""},
475
452
  { "FUNCTION", 0, 0, 0, ""},
 
453
  { "GET_FORMAT", 0, 0, 0, ""},
476
454
  { "GLOBAL", 0, 0, 0, ""},
477
455
  { "GRANT", 0, 0, 0, ""},
478
456
  { "GRANTS", 0, 0, 0, ""},
534
512
  { "LINESTRING", 0, 0, 0, ""},
535
513
  { "LOAD", 0, 0, 0, ""},
536
514
  { "LOCAL", 0, 0, 0, ""},
 
515
  { "LOCALTIME", 0, 0, 0, ""},
537
516
  { "LOCALTIMESTAMP", 0, 0, 0, ""},
538
517
  { "LOCK", 0, 0, 0, ""},
539
518
  { "LOCKS", 0, 0, 0, ""},
630
609
  { "QUARTER", 0, 0, 0, ""},
631
610
  { "QUERY", 0, 0, 0, ""},
632
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, ""},
633
616
  { "READ", 0, 0, 0, ""},
634
617
  { "READS", 0, 0, 0, ""},
635
618
  { "REAL", 0, 0, 0, ""},
731
714
  { "TERMINATED", 0, 0, 0, ""},
732
715
  { "TEXT", 0, 0, 0, ""},
733
716
  { "THEN", 0, 0, 0, ""},
 
717
  { "TIME", 0, 0, 0, ""},
734
718
  { "TIMESTAMP", 0, 0, 0, ""},
735
719
  { "TIMESTAMPADD", 0, 0, 0, ""},
736
720
  { "TIMESTAMPDIFF", 0, 0, 0, ""},
 
721
  { "TINYINT", 0, 0, 0, ""},
737
722
  { "TINYTEXT", 0, 0, 0, ""},
738
723
  { "TO", 0, 0, 0, ""},
739
724
  { "TRAILING", 0, 0, 0, ""},
763
748
  { "USE_FRM", 0, 0, 0, ""},
764
749
  { "USING", 0, 0, 0, ""},
765
750
  { "UTC_DATE", 0, 0, 0, ""},
 
751
  { "UTC_TIME", 0, 0, 0, ""},
766
752
  { "UTC_TIMESTAMP", 0, 0, 0, ""},
767
753
  { "VALUE", 0, 0, 0, ""},
768
754
  { "VALUES", 0, 0, 0, ""},
789
775
  { "ABS", 0, 0, 0, ""},
790
776
  { "ACOS", 0, 0, 0, ""},
791
777
  { "ADDDATE", 0, 0, 0, ""},
 
778
  { "ADDTIME", 0, 0, 0, ""},
792
779
  { "AES_ENCRYPT", 0, 0, 0, ""},
793
780
  { "AES_DECRYPT", 0, 0, 0, ""},
794
781
  { "AREA", 0, 0, 0, ""},
801
788
  { "ATAN2", 0, 0, 0, ""},
802
789
  { "BENCHMARK", 0, 0, 0, ""},
803
790
  { "BIN", 0, 0, 0, ""},
 
791
  { "BIT_COUNT", 0, 0, 0, ""},
804
792
  { "BIT_OR", 0, 0, 0, ""},
805
793
  { "BIT_AND", 0, 0, 0, ""},
806
794
  { "BIT_XOR", 0, 0, 0, ""},
807
795
  { "CAST", 0, 0, 0, ""},
808
796
  { "CEIL", 0, 0, 0, ""},
809
797
  { "CEILING", 0, 0, 0, ""},
 
798
  { "BIT_LENGTH", 0, 0, 0, ""},
810
799
  { "CENTROID", 0, 0, 0, ""},
811
800
  { "CHAR_LENGTH", 0, 0, 0, ""},
812
801
  { "CHARACTER_LENGTH", 0, 0, 0, ""},
824
813
  { "CRC32", 0, 0, 0, ""},
825
814
  { "CROSSES", 0, 0, 0, ""},
826
815
  { "CURDATE", 0, 0, 0, ""},
 
816
  { "CURTIME", 0, 0, 0, ""},
827
817
  { "DATE_ADD", 0, 0, 0, ""},
828
818
  { "DATEDIFF", 0, 0, 0, ""},
829
819
  { "DATE_FORMAT", 0, 0, 0, ""},
893
883
  { "LTRIM", 0, 0, 0, ""},
894
884
  { "MAKE_SET", 0, 0, 0, ""},
895
885
  { "MAKEDATE", 0, 0, 0, ""},
 
886
  { "MAKETIME", 0, 0, 0, ""},
896
887
  { "MASTER_POS_WAIT", 0, 0, 0, ""},
897
888
  { "MAX", 0, 0, 0, ""},
898
889
  { "MBRCONTAINS", 0, 0, 0, ""},
949
940
  { "ROW_COUNT", 0, 0, 0, ""},
950
941
  { "RPAD", 0, 0, 0, ""},
951
942
  { "RTRIM", 0, 0, 0, ""},
 
943
  { "SEC_TO_TIME", 0, 0, 0, ""},
952
944
  { "SESSION_USER", 0, 0, 0, ""},
953
945
  { "SUBDATE", 0, 0, 0, ""},
954
946
  { "SIGN", 0, 0, 0, ""},
970
962
  { "SUBSTR", 0, 0, 0, ""},
971
963
  { "SUBSTRING", 0, 0, 0, ""},
972
964
  { "SUBSTRING_INDEX", 0, 0, 0, ""},
 
965
  { "SUBTIME", 0, 0, 0, ""},
973
966
  { "SUM", 0, 0, 0, ""},
974
967
  { "SYSDATE", 0, 0, 0, ""},
975
968
  { "SYSTEM_USER", 0, 0, 0, ""},
976
969
  { "TAN", 0, 0, 0, ""},
977
970
  { "TIME_FORMAT", 0, 0, 0, ""},
 
971
  { "TIME_TO_SEC", 0, 0, 0, ""},
 
972
  { "TIMEDIFF", 0, 0, 0, ""},
978
973
  { "TO_DAYS", 0, 0, 0, ""},
979
974
  { "TOUCHES", 0, 0, 0, ""},
980
975
  { "TRIM", 0, 0, 0, ""},
1011
1006
static bool add_line(string *buffer,char *line,char *in_string,
1012
1007
                     bool *ml_comment);
1013
1008
static void remove_cntrl(string *buffer);
1014
 
static void print_table_data(drizzle_result_st *result);
1015
 
static void print_tab_data(drizzle_result_st *result);
1016
 
static void print_table_data_vertically(drizzle_result_st *result);
1017
 
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);
1018
1013
static uint32_t start_timer(void);
1019
1014
static void end_timer(uint32_t start_time,char *buff);
1020
1015
static void drizzle_end_timer(uint32_t start_time,char *buff);
1021
1016
static void nice_time(double sec,char *buff,bool part_second);
1022
 
extern "C" void drizzle_end(int sig);
1023
 
extern "C" void handle_sigint(int sig);
 
1017
extern sig_handler drizzle_end(int sig);
 
1018
extern sig_handler handle_sigint(int sig);
1024
1019
#if defined(HAVE_TERMIOS_H) && defined(GWINSZ_IN_SYS_IOCTL)
1025
 
static void window_resize(int sig);
 
1020
static sig_handler window_resize(int sig);
1026
1021
#endif
1027
1022
 
1028
 
static inline int is_prefix(const char *s, const char *t)
1029
 
{
1030
 
  while (*t)
1031
 
    if (*s++ != *t++) return 0;
1032
 
  return 1;                                     /* WRONG */
1033
 
}
1034
 
 
1035
 
/**
1036
 
  Shutdown the server that we are currently connected to.
1037
 
 
1038
 
  @retval
1039
 
    true success
1040
 
  @retval
1041
 
    false failure
1042
 
*/
1043
 
static bool server_shutdown(void)
1044
 
{
1045
 
  drizzle_result_st result;
1046
 
  drizzle_return_t ret;
1047
 
 
1048
 
  if (verbose)
1049
 
  {
1050
 
    printf("shutting down drizzled");
1051
 
    if (opt_drizzle_port > 0)
1052
 
      printf(" on port %d", opt_drizzle_port);
1053
 
    printf("... ");
1054
 
  }
1055
 
 
1056
 
  if (drizzle_shutdown(&con, &result, DRIZZLE_SHUTDOWN_DEFAULT,
1057
 
                       &ret) == NULL || ret != DRIZZLE_RETURN_OK)
1058
 
  {
1059
 
    if (ret == DRIZZLE_RETURN_ERROR_CODE)
1060
 
    {
1061
 
      fprintf(stderr, "shutdown failed; error: '%s'",
1062
 
              drizzle_result_error(&result));
1063
 
      drizzle_result_free(&result);
1064
 
    }
1065
 
    else
1066
 
    {
1067
 
      fprintf(stderr, "shutdown failed; error: '%s'",
1068
 
              drizzle_con_error(&con));
1069
 
    }
1070
 
    return false;
1071
 
  }
1072
 
 
1073
 
  drizzle_result_free(&result);
1074
 
 
1075
 
  if (verbose)
1076
 
    printf("done\n");
1077
 
 
1078
 
  return true;
1079
 
}
1080
 
 
1081
 
/**
1082
 
  Ping the server that we are currently connected to.
1083
 
 
1084
 
  @retval
1085
 
    true success
1086
 
  @retval
1087
 
    false failure
1088
 
*/
1089
 
static bool server_ping(void)
1090
 
{
1091
 
  drizzle_result_st result;
1092
 
  drizzle_return_t ret;
1093
 
 
1094
 
  if (drizzle_ping(&con, &result, &ret) != NULL && ret == DRIZZLE_RETURN_OK)
1095
 
  {
1096
 
    if (opt_silent < 2)
1097
 
      printf("drizzled is alive\n");
1098
 
  }
1099
 
  else
1100
 
  {
1101
 
    if (ret == DRIZZLE_RETURN_ERROR_CODE)
1102
 
    {
1103
 
      fprintf(stderr, "ping failed; error: '%s'",
1104
 
              drizzle_result_error(&result));
1105
 
      drizzle_result_free(&result);
1106
 
    }
1107
 
    else
1108
 
    {
1109
 
      fprintf(stderr, "drizzled won't answer to ping, error: '%s'",
1110
 
              drizzle_con_error(&con));
1111
 
    }
1112
 
    return false;
1113
 
  }
1114
 
  drizzle_result_free(&result);
1115
 
  return true;
1116
 
}
1117
 
 
1118
 
/**
1119
 
  Execute command(s) specified by the user.
1120
 
 
1121
 
  @param error  error status of command execution.
1122
 
                If an error had occurred, this variable will be set
1123
 
                to 1 whereas on success, it shall be set to 0. This
1124
 
                value will be supplied to the exit() function used
1125
 
                by the caller.
1126
 
 
1127
 
  @retval
1128
 
    false no commands were executed
1129
 
  @retval
1130
 
    true  at least one command was executed
1131
 
*/
1132
 
static bool execute_commands(int *error)
1133
 
{
1134
 
  bool executed= false;
1135
 
  *error= 0;
1136
 
 
1137
 
  if (opt_ping)
1138
 
  {
1139
 
    if (server_ping() == false)
1140
 
      *error= 1;
1141
 
    executed= true;
1142
 
  }
1143
 
 
1144
 
  if (opt_shutdown)
1145
 
  {
1146
 
    if (server_shutdown() == false)
1147
 
      *error= 1;
1148
 
    executed= true;
1149
 
  }
1150
 
  return executed;
1151
 
}
1152
 
 
1153
1023
int main(int argc,char *argv[])
1154
1024
{
 
1025
  char buff[80];
 
1026
 
1155
1027
#if defined(ENABLE_NLS)
1156
1028
# if defined(HAVE_LOCALE_H)
1157
1029
  setlocale(LC_ALL, "");
1162
1034
 
1163
1035
  MY_INIT(argv[0]);
1164
1036
  delimiter_str= delimiter;
1165
 
  default_prompt= strdup(getenv("DRIZZLE_PS1") ?
1166
 
                         getenv("DRIZZLE_PS1") :
1167
 
                         "drizzle> ");
1168
 
  
1169
 
  if (default_prompt == NULL)
1170
 
  {
1171
 
    fprintf(stderr, _("Memory allocation error while constructing initial "
1172
 
                      "prompt. Aborting.\n"));
1173
 
    exit(ENOMEM);
1174
 
  }
1175
 
  current_prompt= strdup(default_prompt);
1176
 
  if (current_prompt == NULL)
1177
 
  {
1178
 
    fprintf(stderr, _("Memory allocation error while constructing initial "
1179
 
                      "prompt. Aborting.\n"));
1180
 
    exit(ENOMEM);
1181
 
  }
 
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));
1182
1041
  processed_prompt= new string();
1183
1042
  processed_prompt->reserve(32);
1184
1043
 
1185
1044
  prompt_counter=0;
1186
1045
 
1187
1046
  outfile[0]=0;      // no (default) outfile
1188
 
  strcpy(pager, "stdout");  // the default, if --pager wasn't given
 
1047
  stpcpy(pager, "stdout");  // the default, if --pager wasn't given
1189
1048
  {
1190
1049
    char *tmp=getenv("PAGER");
1191
1050
    if (tmp && strlen(tmp))
1192
1051
    {
1193
1052
      default_pager_set= 1;
1194
 
      strcpy(default_pager, tmp);
 
1053
      stpcpy(default_pager, tmp);
1195
1054
    }
1196
1055
  }
1197
1056
  if (!isatty(0) || !isatty(1))
1217
1076
      close(stdout_fileno_copy);             /* Clean up dup(). */
1218
1077
  }
1219
1078
 
1220
 
  load_defaults("drizzle",load_default_groups,&argc,&argv);
 
1079
  load_defaults("my",load_default_groups,&argc,&argv);
1221
1080
  defaults_argv=argv;
1222
1081
  if (get_options(argc, (char **) argv))
1223
1082
  {
1224
1083
    free_defaults(defaults_argv);
1225
 
    my_end();
1226
 
    exit(1);
1227
 
  }
1228
 
 
 
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);
1229
1096
  memset(&drizzle, 0, sizeof(drizzle));
1230
1097
  if (sql_connect(current_host,current_db,current_user,opt_password,
1231
1098
                  opt_silent))
1234
1101
    status.exit_status= 1;
1235
1102
    drizzle_end(-1);
1236
1103
  }
1237
 
 
1238
 
  int command_error;
1239
 
  if (execute_commands(&command_error) != false)
1240
 
  {
1241
 
    /* we've executed a command so exit before we go into readline mode */
1242
 
    free_defaults(defaults_argv);
1243
 
    my_end();
1244
 
    exit(command_error);
1245
 
  }
1246
 
 
1247
 
  if (status.batch && !status.line_buff)
1248
 
  {
1249
 
    status.line_buff= new(std::nothrow) LineBuffer(opt_max_input_line, stdin);
1250
 
    if (status.line_buff == NULL)
1251
 
    {
1252
 
      free_defaults(defaults_argv);
1253
 
      my_end();
1254
 
      exit(1);
1255
 
    }
1256
 
  }
1257
 
 
1258
1104
  if (!status.batch)
1259
1105
    ignore_errors=1;        // Don't abort monitor
1260
1106
 
1276
1122
 
1277
1123
  glob_buffer= new string();
1278
1124
  glob_buffer->reserve(512);
1279
 
 
 
1125
  
1280
1126
  char * output_buff= (char *)malloc(512);
1281
1127
  memset(output_buff, '\0', 512);
1282
1128
 
1283
1129
  sprintf(output_buff,
1284
1130
          _("Your Drizzle connection id is %u\nServer version: %s\n"),
1285
 
          drizzle_con_thread_id(&con),
1286
 
          server_version_string(&con));
 
1131
          drizzle_thread_id(&drizzle),
 
1132
          server_version_string(&drizzle));
1287
1133
  put_info(output_buff, INFO_INFO, 0, 0);
1288
1134
 
1289
1135
  initialize_readline(current_prompt);
1294
1140
      histfile= strdup(getenv("DRIZZLE_HISTFILE"));
1295
1141
    else if (getenv("HOME"))
1296
1142
    {
1297
 
      histfile=(char*) malloc(strlen(getenv("HOME")) + strlen("/.drizzle_history") + 2);
 
1143
      histfile=(char*) my_malloc((uint) strlen(getenv("HOME"))
 
1144
                                 + (uint) strlen("/.drizzle_history")+2,
 
1145
                                 MYF(MY_WME));
1298
1146
      if (histfile)
1299
1147
        sprintf(histfile,"%s/.drizzle_history",getenv("HOME"));
1300
1148
      char link_name[FN_REFLEN];
1301
 
      ssize_t sym_link_size= readlink(histfile,link_name,FN_REFLEN-1);
1302
 
      if (sym_link_size >= 0)
 
1149
      if (my_readlink(link_name, histfile, 0) == 0 &&
 
1150
          strncmp(link_name, "/dev/null", 10) == 0)
1303
1151
      {
1304
 
        link_name[sym_link_size]= '\0';
1305
 
        if (strncmp(link_name, "/dev/null", 10) == 0)
1306
 
        {
1307
 
          /* The .drizzle_history file is a symlink to /dev/null, don't use it */
1308
 
          free(histfile);
1309
 
          histfile= 0;
1310
 
        }
 
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;
1311
1155
      }
1312
1156
    }
1313
1157
    if (histfile)
1315
1159
      if (verbose)
1316
1160
        tee_fprintf(stdout, _("Reading history-file %s\n"),histfile);
1317
1161
      read_history(histfile);
1318
 
      if (!(histfile_tmp= (char*) malloc((uint32_t) strlen(histfile) + 5)))
 
1162
      if (!(histfile_tmp= (char*) my_malloc((uint) strlen(histfile) + 5,
 
1163
                                            MYF(MY_WME))))
1319
1164
      {
1320
1165
        fprintf(stderr, _("Couldn't allocate memory for temp histfile!\n"));
1321
1166
        exit(1);
1323
1168
      sprintf(histfile_tmp, "%s.TMP", histfile);
1324
1169
    }
1325
1170
  }
 
1171
  sprintf(buff, "%s",
 
1172
          _("Type 'help;' or '\\h' for help. Type '\\c' to clear the buffer.\n"));
1326
1173
 
1327
 
  put_info(_("Type 'help;' or '\\h' for help. "
1328
 
             "Type '\\c' to clear the buffer.\n"),INFO_INFO,0,0);
 
1174
  put_info(buff,INFO_INFO,0,0);
1329
1175
  status.exit_status= read_and_execute(!status.batch);
1330
1176
  if (opt_outfile)
1331
1177
    end_tee();
1334
1180
  return(0);        // Keep compiler happy
1335
1181
}
1336
1182
 
1337
 
void drizzle_end(int sig)
 
1183
sig_handler drizzle_end(int sig)
1338
1184
{
1339
 
  drizzle_con_free(&con);
1340
 
  drizzle_free(&drizzle);
 
1185
  drizzle_close(&drizzle);
1341
1186
  if (!status.batch && !quick && histfile)
1342
1187
  {
1343
1188
    /* write-history */
1346
1191
    if (!write_history(histfile_tmp))
1347
1192
      my_rename(histfile_tmp, histfile, MYF(MY_WME));
1348
1193
  }
1349
 
  delete status.line_buff;
1350
 
  status.line_buff= 0;
 
1194
  batch_readline_end(status.line_buff);
 
1195
  completion_hash_free(&ht);
 
1196
  free_root(&hash_mem_root,MYF(0));
1351
1197
 
1352
1198
  if (sig >= 0)
1353
1199
    put_info(sig ? _("Aborted") : _("Bye"), INFO_RESULT,0,0);
1355
1201
    delete glob_buffer;
1356
1202
  if (processed_prompt)
1357
1203
    delete processed_prompt;
1358
 
  free(opt_password);
1359
 
  free(opt_drizzle_unix_port);
1360
 
  free(histfile);
1361
 
  free(histfile_tmp);
1362
 
  free(current_db);
1363
 
  free(current_host);
1364
 
  free(current_user);
1365
 
  free(full_username);
1366
 
  free(part_username);
1367
 
  free(default_prompt);
1368
 
  free(current_prompt);
 
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();
1369
1216
  free_defaults(defaults_argv);
1370
 
  my_end();
 
1217
  my_end(my_end_arg);
1371
1218
  exit(status.exit_status);
1372
1219
}
1373
1220
 
1377
1224
  If query is in process, kill query
1378
1225
  no query in process, terminate like previous behavior
1379
1226
*/
1380
 
extern "C"
1381
 
void handle_sigint(int sig)
 
1227
sig_handler handle_sigint(int sig)
1382
1228
{
1383
1229
  char kill_buffer[40];
1384
 
  drizzle_con_st kill_drizzle;
1385
 
  drizzle_result_st res;
1386
 
  drizzle_return_t ret;
 
1230
  DRIZZLE *kill_drizzle= NULL;
1387
1231
 
1388
1232
  /* terminate if no query being executed, or we already tried interrupting */
1389
1233
  if (!executing_query || interrupted_query) {
1390
1234
    goto err;
1391
1235
  }
1392
1236
 
1393
 
  if (drizzle_con_add_tcp(&drizzle, &kill_drizzle, current_host,
1394
 
                          opt_drizzle_port, current_user, opt_password, NULL,
1395
 
                          DRIZZLE_CON_NONE) == NULL)
 
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))
1396
1240
  {
1397
1241
    goto err;
1398
1242
  }
1399
1243
 
1400
1244
  /* kill_buffer is always big enough because max length of %lu is 15 */
1401
 
  sprintf(kill_buffer, "KILL /*!50000 QUERY */ %u",
1402
 
          drizzle_con_thread_id(&con));
1403
 
 
1404
 
  if (drizzle_query_str(&kill_drizzle, &res, kill_buffer, &ret) != NULL)
1405
 
    drizzle_result_free(&res);
1406
 
 
1407
 
  drizzle_con_free(&kill_drizzle);
 
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);
1408
1248
  tee_fprintf(stdout, _("Query aborted by Ctrl+C\n"));
1409
1249
 
1410
1250
  interrupted_query= 1;
1417
1257
 
1418
1258
 
1419
1259
#if defined(HAVE_TERMIOS_H) && defined(GWINSZ_IN_SYS_IOCTL)
1420
 
void window_resize(int)
 
1260
sig_handler window_resize(int sig __attribute__((unused)))
1421
1261
{
1422
1262
  struct winsize window_size;
1423
1263
 
1437
1277
   (char**) &opt_rehash, (char**) &opt_rehash, 0, GET_BOOL, NO_ARG, 1, 0, 0, 0,
1438
1278
   0, 0},
1439
1279
  {"no-auto-rehash", 'A',
1440
 
   N_("No automatic rehashing. One has to use 'rehash' to get table and field completion. This gives a quicker start of drizzle_st and disables rehashing on reconnect. WARNING: options deprecated; use --disable-auto-rehash instead."),
 
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."),
1441
1281
   0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
1442
1282
  {"auto-vertical-output", OPT_AUTO_VERTICAL_OUTPUT,
1443
1283
   N_("Automatically switch to vertical output mode if the result is wider than the terminal width."),
1444
1284
   (char**) &auto_vertical_output, (char**) &auto_vertical_output, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
1445
1285
  {"batch", 'B',
1446
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},
1447
1290
  {"column-type-info", OPT_COLUMN_TYPES, N_("Display column type information."),
1448
1291
   (char**) &column_types_flag, (char**) &column_types_flag,
1449
1292
   0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
1453
1296
  {"compress", 'C', N_("Use compression in server/client protocol."),
1454
1297
   (char**) &opt_compress, (char**) &opt_compress, 0, GET_BOOL, NO_ARG, 0, 0, 0,
1455
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},
1456
1304
  {"database", 'D', N_("Database to use."), (char**) &current_db,
1457
1305
   (char**) &current_db, 0, GET_STR_ALLOC, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
1458
1306
  {"default-character-set", OPT_DEFAULT_CHARSET,
1459
 
   N_("(not used)"), 0,
1460
 
   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},
1461
1309
  {"delimiter", OPT_DELIMITER, N_("Delimiter to be used."), (char**) &delimiter_str,
1462
1310
   (char**) &delimiter_str, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
1463
1311
  {"execute", 'e', N_("Execute command and quit. (Disables --force and history file)"), 0,
1512
1360
  {"no-pager", OPT_NOPAGER,
1513
1361
   N_("Disable pager and print to stdout. See interactive help (\\h) also. WARNING: option deprecated; use --disable-pager instead."),
1514
1362
   0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
1515
 
  {"password", 'P',
 
1363
  {"password", 'p',
1516
1364
   N_("Password to use when connecting to server. If password is not given it's asked from the tty."),
1517
1365
   0, 0, 0, GET_STR, OPT_ARG, 0, 0, 0, 0, 0, 0},
1518
 
  {"port", 'p', N_("Port number to use for connection or 0 for default to, in order of preference, drizzle.cnf, $DRIZZLE_TCP_PORT, ")
 
1366
  {"port", 'P', N_("Port number to use for connection or 0 for default to, in order of preference, my.cnf, $DRIZZLE_TCP_PORT, ")
1519
1367
   N_("built-in default") " (" STRINGIFY_ARG(DRIZZLE_PORT) ").",
1520
 
   0, 0, 0, GET_UINT, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
1368
   (char**) &opt_drizzle_port,
 
1369
   (char**) &opt_drizzle_port, 0, GET_UINT, REQUIRED_ARG, 0, 0, 0, 0, 0,  0},
1521
1370
  {"prompt", OPT_PROMPT, N_("Set the drizzle prompt to this value."),
1522
1371
   (char**) &current_prompt, (char**) &current_prompt, 0, GET_STR_ALLOC,
1523
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},
1524
1375
  {"quick", 'q',
1525
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."),
1526
1377
   (char**) &quick, (char**) &quick, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
1529
1380
   0, 0, 0},
1530
1381
  {"reconnect", OPT_RECONNECT, N_("Reconnect if the connection is lost. Disable with --disable-reconnect. This option is enabled by default."),
1531
1382
   (char**) &opt_reconnect, (char**) &opt_reconnect, 0, GET_BOOL, NO_ARG, 1, 0, 0, 0, 0, 0},
1532
 
  {"shutdown", OPT_SHUTDOWN, N_("Shutdown the server."),
1533
 
   (char**) &opt_shutdown, (char**) &opt_shutdown, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
1534
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,
1535
1384
   0, 0},
1536
1385
  {"socket", 'S', N_("Socket file to use for connection."),
1543
1392
   0, 0, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
1544
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,
1545
1394
   NO_ARG, 0, 0, 0, 0, 0, 0},
 
1395
#ifndef DONT_ALLOW_USER_CHANGE
1546
1396
  {"user", 'u', N_("User for login if not current user."), (char**) &current_user,
1547
1397
   (char**) &current_user, 0, GET_STR_ALLOC, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
1398
#endif
1548
1399
  {"safe-updates", 'U', N_("Only allow UPDATE and DELETE that uses keys."),
1549
1400
   (char**) &safe_updates, (char**) &safe_updates, 0, GET_BOOL, NO_ARG, 0, 0,
1550
1401
   0, 0, 0, 0},
1560
1411
  {"connect_timeout", OPT_CONNECT_TIMEOUT,
1561
1412
   N_("Number of seconds before connection timeout."),
1562
1413
   (char**) &opt_connect_timeout,
1563
 
   (char**) &opt_connect_timeout, 0, GET_UINT32, REQUIRED_ARG, 0, 0, 3600*12, 0,
 
1414
   (char**) &opt_connect_timeout, 0, GET_ULONG, REQUIRED_ARG, 0, 0, 3600*12, 0,
1564
1415
   0, 0},
1565
 
  {"max_input_line", OPT_MAX_INPUT_LINE,
1566
 
   N_("Max length of input line"),
1567
 
   (char**) &opt_max_input_line, (char**) &opt_max_input_line, 0,
1568
 
   GET_UINT32, REQUIRED_ARG, 16 *1024L*1024L, 4096,
 
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,
1569
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},
1570
1425
  {"select_limit", OPT_SELECT_LIMIT,
1571
1426
   N_("Automatic limit for SELECT when using --safe-updates"),
1572
1427
   (char**) &select_limit,
1573
 
   (char**) &select_limit, 0, GET_UINT32, REQUIRED_ARG, 1000L, 1, ULONG_MAX,
 
1428
   (char**) &select_limit, 0, GET_ULONG, REQUIRED_ARG, 1000L, 1, ULONG_MAX,
1574
1429
   0, 1, 0},
1575
1430
  {"max_join_size", OPT_MAX_JOIN_SIZE,
1576
1431
   N_("Automatic limit for rows in a join when using --safe-updates"),
1577
1432
   (char**) &max_join_size,
1578
 
   (char**) &max_join_size, 0, GET_UINT32, REQUIRED_ARG, 1000000L, 1, ULONG_MAX,
 
1433
   (char**) &max_join_size, 0, GET_ULONG, REQUIRED_ARG, 1000000L, 1, ULONG_MAX,
1579
1434
   0, 1, 0},
1580
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,
1581
1436
   (char**) &opt_secure_auth, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
1582
1437
  {"show-warnings", OPT_SHOW_WARNINGS, N_("Show warnings after every statement."),
1583
1438
   (char**) &show_warnings, (char**) &show_warnings, 0, GET_BOOL, NO_ARG,
1584
1439
   0, 0, 0, 0, 0, 0},
1585
 
  {"show-progress-size", OPT_SHOW_PROGRESS_SIZE, N_("Number of lines before each import progress report."),
1586
 
   (char**) &show_progress_size, (char**) &show_progress_size, 0, GET_UINT32, REQUIRED_ARG,
1587
 
   0, 0, 0, 0, 0, 0},
1588
 
  {"ping", OPT_PING, N_("Ping the server to check if it's alive."),
1589
 
   (char**) &opt_ping, (char**) &opt_ping, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
1590
1440
  { 0, 0, 0, 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0}
1591
1441
};
1592
1442
 
1595
1445
{
1596
1446
  const char* readline= "readline";
1597
1447
 
1598
 
  printf(_("%s  Ver %s Distrib %s, for %s-%s (%s) using %s %s\n"),
1599
 
         my_progname, VER.c_str(), drizzle_version(),
1600
 
         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,
1601
1451
         readline, rl_library_version);
1602
1452
 
1603
1453
  if (version)
1604
1454
    return;
1605
 
  printf(_("Copyright (C) 2008 Sun Microsystems\n"
1606
 
           "This software comes with ABSOLUTELY NO WARRANTY. "
1607
 
           "This is free software,\n"
1608
 
           "and you are welcome to modify and redistribute it "
1609
 
           "under the GPL license\n"));
 
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"));
1610
1459
  printf(_("Usage: %s [OPTIONS] [database]\n"), my_progname);
1611
1460
  my_print_help(my_long_options);
1612
 
  print_defaults("drizzle", load_default_groups);
 
1461
  print_defaults("my", load_default_groups);
1613
1462
  my_print_variables(my_long_options);
1614
1463
}
1615
1464
 
1616
1465
 
1617
 
extern "C" bool
1618
 
get_one_option(int optid, const struct my_option *, char *argument)
 
1466
bool
 
1467
get_one_option(int optid, const struct my_option *opt __attribute__((unused)),
 
1468
               char *argument)
1619
1469
{
1620
 
  char *endchar= NULL;
1621
 
  uint64_t temp_drizzle_port= 0;
1622
 
 
1623
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;
1624
1475
  case  OPT_DEFAULT_CHARSET:
1625
1476
    default_charset_used= 1;
1626
1477
    break;
1627
1478
  case OPT_DELIMITER:
1628
1479
    if (argument == disabled_my_option)
1629
1480
    {
1630
 
      strcpy(delimiter, DEFAULT_DELIMITER);
 
1481
      stpcpy(delimiter, DEFAULT_DELIMITER);
1631
1482
    }
1632
1483
    else
1633
1484
    {
1634
1485
      /* Check that delimiter does not contain a backslash */
1635
1486
      if (!strstr(argument, "\\"))
1636
1487
      {
1637
 
        strncpy(delimiter, argument, sizeof(delimiter) - 1);
 
1488
        strmake(delimiter, argument, sizeof(delimiter) - 1);
1638
1489
      }
1639
1490
      else
1640
1491
      {
1641
1492
        put_info(_("DELIMITER cannot contain a backslash character"),
1642
1493
                 INFO_ERROR,0,0);
1643
 
        return false;
 
1494
        return 0;
1644
1495
      }
1645
1496
    }
1646
 
    delimiter_length= (uint32_t)strlen(delimiter);
 
1497
    delimiter_length= (uint)strlen(delimiter);
1647
1498
    delimiter_str= delimiter;
1648
1499
    break;
 
1500
  case OPT_LOCAL_INFILE:
 
1501
    using_opt_local_infile=1;
 
1502
    break;
1649
1503
  case OPT_TEE:
1650
1504
    if (argument == disabled_my_option)
1651
1505
    {
1669
1523
      if (argument && strlen(argument))
1670
1524
      {
1671
1525
        default_pager_set= 1;
1672
 
        strncpy(pager, argument, sizeof(pager) - 1);
1673
 
        strcpy(default_pager, pager);
 
1526
        strmake(pager, argument, sizeof(pager) - 1);
 
1527
        stpcpy(default_pager, pager);
1674
1528
      }
1675
1529
      else if (default_pager_set)
1676
 
        strcpy(pager, default_pager);
 
1530
        stpcpy(pager, default_pager);
1677
1531
      else
1678
1532
        opt_nopager= 1;
1679
1533
    }
1694
1548
  case 'e':
1695
1549
    status.batch= 1;
1696
1550
    status.add_to_history= 0;
1697
 
    if (status.line_buff == NULL)
1698
 
      status.line_buff= new(std::nothrow) LineBuffer(opt_max_input_line,NULL);
1699
 
    if (status.line_buff == NULL)
1700
 
    {
1701
 
      my_end();
1702
 
      exit(1);
1703
 
    }
1704
 
    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;
1705
1555
    break;
1706
1556
  case 'o':
1707
1557
    if (argument == disabled_my_option)
1710
1560
      one_database= skip_updates= 1;
1711
1561
    break;
1712
1562
  case 'p':
1713
 
    temp_drizzle_port= (uint64_t) strtoul(argument, &endchar, 10);
1714
 
    /* if there is an alpha character this is not a valid port */
1715
 
    if (strlen(endchar) != 0)
1716
 
    {
1717
 
      put_info(_("Non-integer value supplied for port.  If you are trying to enter a password please use --password instead."), INFO_ERROR, 0, 0);
1718
 
      return false;
1719
 
    }
1720
 
    /* If the port number is > 65535 it is not a valid port
1721
 
       This also helps with potential data loss casting unsigned long to a
1722
 
       uint32_t. */
1723
 
    if ((temp_drizzle_port == 0) || (temp_drizzle_port > 65535))
1724
 
    {
1725
 
      put_info(_("Value supplied for port is not valid."), INFO_ERROR, 0, 0);
1726
 
      return false;
1727
 
    }
1728
 
    else
1729
 
    {
1730
 
      opt_drizzle_port= (uint32_t) temp_drizzle_port;
1731
 
    }
1732
 
    break;
1733
 
  case 'P':
1734
 
    /* Don't require password */
1735
1563
    if (argument == disabled_my_option)
1736
 
    {
1737
 
      argument= (char*) "";
1738
 
    }
 
1564
      argument= (char*) "";      // Don't require password
1739
1565
    if (argument)
1740
1566
    {
1741
1567
      char *start= argument;
1742
 
      free(opt_password);
 
1568
      my_free(opt_password, MYF(MY_ALLOW_ZERO_PTR));
1743
1569
      opt_password= strdup(argument);
1744
 
      while (*argument)
1745
 
      {
1746
 
        /* Overwriting password with 'x' */
1747
 
        *argument++= 'x';
1748
 
      }
 
1570
      while (*argument) *argument++= 'x';        // Destroy argument
1749
1571
      if (*start)
1750
 
      {
1751
 
        start[1]= 0;
1752
 
      }
 
1572
        start[1]=0 ;
1753
1573
      tty_password= 0;
1754
1574
    }
1755
1575
    else
1756
 
    {
1757
1576
      tty_password= 1;
1758
 
    }
1759
1577
    break;
1760
1578
  case 's':
1761
1579
    if (argument == disabled_my_option)
1774
1592
    status.add_to_history= 0;
1775
1593
    set_if_bigger(opt_silent,1);                         // more silent
1776
1594
    break;
 
1595
    break;
1777
1596
  case 'V':
1778
1597
    usage(1);
1779
1598
    exit(0);
1790
1609
{
1791
1610
  char *tmp, *pagpoint;
1792
1611
  int ho_error;
 
1612
  const DRIZZLE_PARAMETERS *drizzle_params= drizzle_get_parameters();
1793
1613
 
1794
1614
  tmp= (char *) getenv("DRIZZLE_HOST");
1795
1615
  if (tmp)
1798
1618
  pagpoint= getenv("PAGER");
1799
1619
  if (!((char*) (pagpoint)))
1800
1620
  {
1801
 
    strcpy(pager, "stdout");
 
1621
    stpcpy(pager, "stdout");
1802
1622
    opt_nopager= 1;
1803
1623
  }
1804
1624
  else
1805
 
    strcpy(pager, pagpoint);
1806
 
  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;
1807
1630
 
1808
1631
  if ((ho_error=handle_options(&argc, &argv, my_long_options, get_one_option)))
1809
1632
    exit(ho_error);
1810
1633
 
 
1634
  *drizzle_params->p_max_allowed_packet= opt_max_allowed_packet;
 
1635
  *drizzle_params->p_net_buffer_length= opt_net_buffer_length;
 
1636
 
1811
1637
  if (status.batch) /* disable pager and outfile in this case */
1812
1638
  {
1813
 
    strcpy(default_pager, "stdout");
1814
 
    strcpy(pager, "stdout");
 
1639
    stpcpy(default_pager, "stdout");
 
1640
    stpcpy(pager, "stdout");
1815
1641
    opt_nopager= 1;
1816
1642
    default_pager_set= 0;
1817
1643
    opt_outfile= 0;
1818
1644
    opt_reconnect= 0;
1819
 
    connect_flag= DRIZZLE_CAPABILITIES_NONE; /* Not in interactive mode */
 
1645
    connect_flag= 0; /* Not in interactive mode */
1820
1646
  }
1821
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);
1822
1652
  if (argc > 1)
1823
1653
  {
1824
1654
    usage(0);
1827
1657
  if (argc == 1)
1828
1658
  {
1829
1659
    skip_updates= 0;
1830
 
    free(current_db);
 
1660
    my_free(current_db, MYF(MY_ALLOW_ZERO_PTR));
1831
1661
    current_db= strdup(*argv);
1832
1662
  }
1833
1663
  if (tty_password)
1834
 
    opt_password= client_get_tty_password(NULL);
1835
 
 
 
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;
1836
1669
  return(0);
1837
1670
}
1838
1671
 
1849
1682
  {
1850
1683
    if (!interactive)
1851
1684
    {
1852
 
      if (status.line_buff)
1853
 
        line= status.line_buff->readline();
1854
 
      else
1855
 
        line= 0;
1856
 
 
 
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;
1857
1697
      line_number++;
1858
 
      if (show_progress_size > 0)
1859
 
      {
1860
 
        if ((line_number % show_progress_size) == 0)
1861
 
          fprintf(stderr, _("Processing line: %"PRIu32"\n"), line_number);
1862
 
      }
1863
1698
      if (!glob_buffer->empty())
1864
 
        status.query_start_line= line_number;
 
1699
        status.query_start_line=line_number;
1865
1700
    }
1866
1701
    else
1867
1702
    {
1931
1766
 
1932
1767
static COMMANDS *find_command(const char *name,char cmd_char)
1933
1768
{
1934
 
  uint32_t len;
 
1769
  uint len;
1935
1770
  const char *end;
1936
1771
 
1937
1772
  if (!name)
1951
1786
    if (strstr(name, "\\g") || (strstr(name, delimiter) &&
1952
1787
                                !(strlen(name) >= 9 &&
1953
1788
                                  !my_strnncoll(charset_info,
1954
 
                                                (unsigned char*) name, 9,
1955
 
                                                (const unsigned char*) "delimiter",
 
1789
                                                (uchar*) name, 9,
 
1790
                                                (const uchar*) "delimiter",
1956
1791
                                                9))))
1957
1792
      return((COMMANDS *) 0);
1958
1793
    if ((end=strcont(name," \t")))
1959
1794
    {
1960
 
      len=(uint32_t) (end - name);
 
1795
      len=(uint) (end - name);
1961
1796
      while (my_isspace(charset_info,*end))
1962
1797
        end++;
1963
1798
      if (!*end)
1964
1799
        end=0;          // no arguments to function
1965
1800
    }
1966
1801
    else
1967
 
      len=(uint32_t) strlen(name);
 
1802
      len=(uint) strlen(name);
1968
1803
  }
1969
1804
 
1970
 
  for (uint32_t i= 0; commands[i].name; i++)
 
1805
  for (uint i= 0; commands[i].name; i++)
1971
1806
  {
1972
1807
    if (commands[i].func &&
1973
 
        ((name && !my_strnncoll(charset_info,(const unsigned char*)name,len, (const unsigned char*)commands[i].name,len) && !commands[i].name[len] && (!end || (end && commands[i].takes_params))) || (!name && commands[i].cmd_char == cmd_char)))
 
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)))
1974
1809
    {
1975
1810
      return(&commands[i]);
1976
1811
    }
1982
1817
static bool add_line(string *buffer, char *line, char *in_string,
1983
1818
                        bool *ml_comment)
1984
1819
{
1985
 
  unsigned char inchar;
1986
 
  char *pos, *out;
 
1820
  uchar inchar;
 
1821
  char buff[80], *pos, *out;
1987
1822
  COMMANDS *com;
1988
1823
  bool need_space= 0;
1989
1824
  bool ss_comment= 0;
1993
1828
    return(0);
1994
1829
  if (status.add_to_history && line[0] && not_in_history(line))
1995
1830
    add_history(line);
1996
 
  char *end_of_line=line+(uint32_t) strlen(line);
 
1831
  char *end_of_line=line+(uint) strlen(line);
1997
1832
 
1998
 
  for (pos=out=line ; (inchar= (unsigned char) *pos) ; pos++)
 
1833
  for (pos=out=line ; (inchar= (uchar) *pos) ; pos++)
1999
1834
  {
2000
1835
    if (!preserve_comments)
2001
1836
    {
2005
1840
        continue;
2006
1841
    }
2007
1842
 
 
1843
#ifdef USE_MB
2008
1844
    // Accept multi-byte characters as-is
2009
1845
    int length;
2010
1846
    if (use_mb(charset_info) &&
2020
1856
        pos+= length - 1;
2021
1857
      continue;
2022
1858
    }
2023
 
    if (!*ml_comment && inchar == '\\' &&
2024
 
        !(*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)))
2025
1862
    {
2026
1863
      // Found possbile one character command like \c
2027
1864
 
2028
 
      if (!(inchar = (unsigned char) *++pos))
 
1865
      if (!(inchar = (uchar) *++pos))
2029
1866
        break;        // readline adds one '\'
2030
1867
      if (*in_string || inchar == 'N')  // \N is short for NULL
2031
1868
      {          // Don't allow commands in string
2033
1870
        *out++= (char) inchar;
2034
1871
        continue;
2035
1872
      }
2036
 
      if ((com=find_command(NULL,(char) inchar)))
 
1873
      if ((com=find_command(NullS,(char) inchar)))
2037
1874
      {
2038
1875
        // Flush previously accepted characters
2039
1876
        if (out != line)
2061
1898
          {
2062
1899
            for (pos++ ;
2063
1900
                 *pos && (*pos != *delimiter ||
2064
 
                          strncmp(pos + 1, delimiter + 1,
2065
 
                                  strlen(delimiter + 1))) ; pos++)
 
1901
                          !is_prefix(pos + 1, delimiter + 1)) ; pos++)
2066
1902
              ;  // Remove parameters
2067
1903
            if (!*pos)
2068
1904
              pos--;
2073
1909
      }
2074
1910
      else
2075
1911
      {
2076
 
        string buff(_("Unknown command: "));
2077
 
        buff.push_back('\'');
2078
 
        buff.push_back(inchar);
2079
 
        buff.push_back('\'');
2080
 
        buff.push_back('.');
2081
 
        if (put_info(buff.c_str(),INFO_ERROR,0,0) > 0)
 
1912
        sprintf(buff,_("Unknown command '\\%c'."),inchar);
 
1913
        if (put_info(buff,INFO_ERROR,0,0) > 0)
2082
1914
          return(1);
2083
1915
        *out++='\\';
2084
1916
        *out++=(char) inchar;
2087
1919
    }
2088
1920
    else if (!*ml_comment && !*in_string &&
2089
1921
             (end_of_line - pos) >= 10 &&
2090
 
             !my_strnncoll(charset_info, (unsigned char*) pos, 10,
2091
 
                           (const unsigned char*) "delimiter ", 10))
 
1922
             !my_strnncoll(charset_info, (uchar*) pos, 10,
 
1923
                           (const uchar*) "delimiter ", 10))
2092
1924
    {
2093
1925
      // Flush previously accepted characters
2094
1926
      if (out != line)
2117
1949
      buffer->clear();
2118
1950
      break;
2119
1951
    }
2120
 
    else if (!*ml_comment && !*in_string && !strncmp(pos, delimiter,
2121
 
                                                     strlen(delimiter)))
 
1952
    else if (!*ml_comment && !*in_string && is_prefix(pos, delimiter))
2122
1953
    {
2123
1954
      // Found a statement. Continue parsing after the delimiter
2124
1955
      pos+= delimiter_length;
2241
2072
  if (out != line || (buffer->length() > 0))
2242
2073
  {
2243
2074
    *out++='\n';
2244
 
    uint32_t length=(uint32_t) (out-line);
 
2075
    uint length=(uint) (out-line);
2245
2076
    if ((!*ml_comment || preserve_comments))
2246
2077
      buffer->append(line, length);
2247
2078
  }
2254
2085
 
2255
2086
 
2256
2087
static char **mysql_completion (const char *text, int start, int end);
2257
 
extern "C" char *new_command_generator(const char *text, int);
 
2088
static char *new_command_generator(const char *text, int);
2258
2089
 
2259
2090
/*
2260
2091
  Tell the GNU Readline library how to complete.  We want to try to complete
2261
2092
  on command names if this is the first word in the line, or on filenames
2262
2093
  if not.
2263
2094
*/
2264
 
static char *no_completion(const char *, int)
 
2095
static char *no_completion(const char * a __attribute__((unused)),
 
2096
                           int b __attribute__((unused)))
2265
2097
{
2266
2098
  /* No filename completion */
2267
2099
  return 0;
2346
2178
 
2347
2179
  /* Tell the completer that we want a crack first. */
2348
2180
  rl_attempted_completion_function= (rl_completion_func_t*)&mysql_completion;
2349
 
  rl_completion_entry_function= (drizzle_compentry_func_t*)&no_completion;
 
2181
  rl_completion_entry_function= (rl_compentry_func_t*)&no_completion;
2350
2182
}
2351
2183
 
2352
2184
 
2356
2188
  entire line in case we want to do some simple parsing.  Return the
2357
2189
  array of matches, or NULL if there aren't any.
2358
2190
*/
2359
 
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)))
2360
2194
{
2361
2195
  if (!status.batch && !quick)
2362
2196
    return rl_completion_matches(text, new_command_generator);
2364
2198
    return (char**) 0;
2365
2199
}
2366
2200
 
2367
 
inline string lower_string(const string &from_string)
2368
 
{
2369
 
  string to_string= from_string;
2370
 
  transform(to_string.begin(), to_string.end(),
2371
 
            to_string.begin(), ::tolower);
2372
 
  return to_string;
2373
 
}
2374
 
inline string lower_string(const char * from_string)
2375
 
{
2376
 
  string to_string= from_string;
2377
 
  return lower_string(to_string);
2378
 
}
2379
 
 
2380
 
template <class T>
2381
 
class CompletionMatch :
2382
 
  public unary_function<const string&, bool>
2383
 
{
2384
 
  string match_text; 
2385
 
  T match_func;
2386
 
public:
2387
 
  CompletionMatch(string text) : match_text(text) {}
2388
 
  inline bool operator() (const pair<string,string> &match_against) const
2389
 
  {
2390
 
    string sub_match=
2391
 
      lower_string(match_against.first.substr(0,match_text.size()));
2392
 
    return match_func(sub_match,match_text);
2393
 
  }
2394
 
};
2395
 
 
2396
 
 
2397
 
 
2398
 
extern "C"
2399
 
char *new_command_generator(const char *text, int state)
2400
 
{
 
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;
2401
2209
 
2402
2210
  if (!state)
2403
 
  {
2404
 
    completion_string= lower_string(text);
2405
 
    if (completion_string.size() == 0)
2406
 
    {
2407
 
      completion_iter= completion_map.begin();
2408
 
      completion_end= completion_map.end();
2409
 
    }
2410
 
    else
2411
 
    {
2412
 
      completion_iter= find_if(completion_map.begin(), completion_map.end(),
2413
 
                               CompletionMatch<equal_to<string> >(completion_string));
2414
 
      completion_end= find_if(completion_iter, completion_map.end(),
2415
 
                              CompletionMatch<not_equal_to<string> >(completion_string));
2416
 
    }
2417
 
  }
2418
 
  if (completion_iter == completion_end || (size_t)state > completion_map.size())
2419
 
    return NULL;
2420
 
  char *result= (char *)malloc((*completion_iter).second.size()+1);
2421
 
  strcpy(result, (*completion_iter).second.c_str());
2422
 
  completion_iter++;
2423
 
  return result;
 
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;
2424
2278
}
2425
2279
 
 
2280
 
2426
2281
/* Build up the completion hash */
2427
2282
 
2428
2283
static void build_completion_hash(bool rehash, bool write_info)
2429
2284
{
2430
2285
  COMMANDS *cmd=commands;
2431
 
  drizzle_return_t ret;
2432
 
  drizzle_result_st databases,tables,fields;
2433
 
  drizzle_row_t database_row,table_row;
2434
 
  drizzle_column_st *sql_field;
2435
 
  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
 
2436
2294
 
2437
2295
  if (status.batch || quick || !current_db)
2438
2296
    return;      // We don't need completion in batches
2439
2297
  if (!rehash)
2440
2298
    return;
2441
2299
 
2442
 
  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));
2443
2305
 
2444
2306
  /* hash this file's known subset of SQL commands */
2445
2307
  while (cmd->name) {
2446
 
    tmp_str= cmd->name;
2447
 
    tmp_str_lower= lower_string(tmp_str);
2448
 
    completion_map[tmp_str_lower]= tmp_str;
 
2308
    add_word(&ht,(char*) cmd->name);
2449
2309
    cmd++;
2450
2310
  }
2451
2311
 
2452
2312
  /* hash Drizzle functions (to be implemented) */
2453
2313
 
2454
2314
  /* hash all database names */
2455
 
  if (drizzle_query_str(&con, &databases, "show databases", &ret) != NULL)
 
2315
  if (drizzle_query(&drizzle,"show databases") == 0)
2456
2316
  {
2457
 
    if (ret == DRIZZLE_RETURN_OK)
 
2317
    if (!(databases = drizzle_store_result(&drizzle)))
 
2318
      put_info(drizzle_error(&drizzle),INFO_INFO,0,0);
 
2319
    else
2458
2320
    {
2459
 
      if (drizzle_result_buffer(&databases) != DRIZZLE_RETURN_OK)
2460
 
        put_info(drizzle_error(&drizzle),INFO_INFO,0,0);
2461
 
      else
 
2321
      while ((database_row=drizzle_fetch_row(databases)))
2462
2322
      {
2463
 
        while ((database_row=drizzle_row_next(&databases)))
2464
 
        {
2465
 
          tmp_str= database_row[0];
2466
 
          tmp_str_lower= lower_string(tmp_str);
2467
 
          completion_map[tmp_str_lower]= tmp_str;
2468
 
        }
 
2323
        char *str=strdup_root(&hash_mem_root, (char*) database_row[0]);
 
2324
        if (str)
 
2325
          add_word(&ht,(char*) str);
2469
2326
      }
 
2327
      drizzle_free_result(databases);
2470
2328
    }
2471
 
 
2472
 
    drizzle_result_free(&databases);
2473
2329
  }
2474
 
 
2475
2330
  /* hash all table names */
2476
 
  if (drizzle_query_str(&con, &tables, "show tables", &ret) != NULL)
 
2331
  if (drizzle_query(&drizzle,"show tables")==0)
2477
2332
  {
2478
 
    if (ret != DRIZZLE_RETURN_OK)
2479
 
    {
2480
 
      drizzle_result_free(&tables);
2481
 
      return;
2482
 
    }
2483
 
 
2484
 
    if (drizzle_result_buffer(&tables) != DRIZZLE_RETURN_OK)
 
2333
    if (!(tables = drizzle_store_result(&drizzle)))
2485
2334
      put_info(drizzle_error(&drizzle),INFO_INFO,0,0);
2486
2335
    else
2487
2336
    {
2488
 
      if (drizzle_result_row_count(&tables) > 0 && !opt_silent && write_info)
 
2337
      if (drizzle_num_rows(tables) > 0 && !opt_silent && write_info)
2489
2338
      {
2490
2339
        tee_fprintf(stdout, _("\
2491
2340
Reading table information for completion of table and column names\n    \
2492
2341
You can turn off this feature to get a quicker startup with -A\n\n"));
2493
2342
      }
2494
 
      while ((table_row=drizzle_row_next(&tables)))
 
2343
      while ((table_row=drizzle_fetch_row(tables)))
2495
2344
      {
2496
 
        tmp_str= table_row[0];
2497
 
        tmp_str_lower= lower_string(tmp_str);
2498
 
        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);
2499
2349
      }
2500
2350
    }
2501
2351
  }
2502
 
  else
2503
 
    return;
2504
2352
 
2505
2353
  /* hash all field names, both with the table prefix and without it */
2506
 
  if (drizzle_result_row_count(&tables) == 0)
2507
 
  {
2508
 
    drizzle_result_free(&tables);
2509
 
    return;
2510
 
  }
2511
 
 
2512
 
  drizzle_row_seek(&tables, 0);
2513
 
 
2514
 
  while ((table_row=drizzle_row_next(&tables)))
2515
 
  {
2516
 
    string query;
2517
 
 
2518
 
    query.append("show fields in '");
2519
 
    query.append(table_row[0]);
2520
 
    query.append("'");
2521
 
    
2522
 
    if (drizzle_query(&con, &fields, query.c_str(), query.length(),
2523
 
                      &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)))
2524
2369
    {
2525
 
      if (ret == DRIZZLE_RETURN_OK &&
2526
 
          drizzle_result_buffer(&fields) == DRIZZLE_RETURN_OK)
2527
 
      {
2528
 
        while ((sql_field=drizzle_column_next(&fields)))
2529
 
        {
2530
 
          tmp_str=table_row[0];
2531
 
          tmp_str.append(".");
2532
 
          tmp_str.append(drizzle_column_name(sql_field));
2533
 
          tmp_str_lower= lower_string(tmp_str);
2534
 
          completion_map[tmp_str_lower]= tmp_str;
2535
 
 
2536
 
          tmp_str=drizzle_column_name(sql_field);
2537
 
          tmp_str_lower= lower_string(tmp_str);
2538
 
          completion_map[tmp_str_lower]= tmp_str;
2539
 
        }
2540
 
      }
2541
 
      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);
2542
2393
    }
 
2394
    else
 
2395
      field_names[i]= 0;
 
2396
 
 
2397
    i++;
2543
2398
  }
2544
 
  drizzle_result_free(&tables);
2545
 
  completion_iter= completion_map.begin();
 
2399
  drizzle_free_result(tables);
 
2400
  field_names[i]=0;        // End pointer
 
2401
  return;
2546
2402
}
2547
2403
 
2548
2404
/* for gnu readline */
2556
2412
    for (;;)
2557
2413
    {
2558
2414
      if (*s == (char) c) return (char*) s;
2559
 
      if (!*s++) return NULL;
 
2415
      if (!*s++) return NullS;
2560
2416
    }
2561
2417
  }
2562
2418
 
2564
2420
  {
2565
2421
    register char *t;
2566
2422
 
2567
 
    t = NULL;
 
2423
    t = NullS;
2568
2424
    do if (*s == (char) c) t = (char*) s; while (*s++);
2569
2425
    return (char*) t;
2570
2426
  }
2574
2430
 
2575
2431
static int reconnect(void)
2576
2432
{
 
2433
  /* purecov: begin tested */
2577
2434
  if (opt_reconnect)
2578
2435
  {
2579
2436
    put_info(_("No connection. Trying to reconnect..."),INFO_INFO,0,0);
2580
2437
    (void) com_connect((string *)0, 0);
2581
 
    if (opt_rehash && connected)
 
2438
    if (opt_rehash)
2582
2439
      com_rehash(NULL, NULL);
2583
2440
  }
2584
 
  if (! connected)
 
2441
  if (!connected)
2585
2442
    return put_info(_("Can't connect to the server\n"),INFO_ERROR,0,0);
 
2443
  /* purecov: end */
2586
2444
  return 0;
2587
2445
}
2588
2446
 
2589
2447
static void get_current_db(void)
2590
2448
{
2591
 
  drizzle_return_t ret;
2592
 
  drizzle_result_st res;
 
2449
  DRIZZLE_RES *res;
2593
2450
 
2594
 
  free(current_db);
 
2451
  my_free(current_db, MYF(MY_ALLOW_ZERO_PTR));
2595
2452
  current_db= NULL;
2596
2453
  /* In case of error below current_db will be NULL */
2597
 
  if (drizzle_query_str(&con, &res, "SELECT DATABASE()", &ret) != NULL)
 
2454
  if (!drizzle_query(&drizzle, "SELECT DATABASE()") &&
 
2455
      (res= drizzle_use_result(&drizzle)))
2598
2456
  {
2599
 
    if (ret == DRIZZLE_RETURN_OK &&
2600
 
        drizzle_result_buffer(&res) == DRIZZLE_RETURN_OK)
2601
 
    {
2602
 
      drizzle_row_t row= drizzle_row_next(&res);
2603
 
      if (row[0])
2604
 
        current_db= strdup(row[0]);
2605
 
      drizzle_result_free(&res);
2606
 
    }
 
2457
    DRIZZLE_ROW row= drizzle_fetch_row(res);
 
2458
    if (row[0])
 
2459
      current_db= strdup(row[0]);
 
2460
    drizzle_free_result(res);
2607
2461
  }
2608
2462
}
2609
2463
 
2611
2465
 The different commands
2612
2466
***************************************************************************/
2613
2467
 
2614
 
int drizzleclient_real_query_for_lazy(const char *buf, int length,
2615
 
                                      drizzle_result_st *result,
2616
 
                                      uint32_t *error_code)
 
2468
int drizzle_real_query_for_lazy(const char *buf, int length)
2617
2469
{
2618
 
  drizzle_return_t ret;
2619
 
 
2620
 
  for (uint32_t retry=0;; retry++)
 
2470
  for (uint retry=0;; retry++)
2621
2471
  {
2622
2472
    int error;
2623
 
    if (drizzle_query(&con,result,buf,length,&ret) != NULL &&
2624
 
        ret == DRIZZLE_RETURN_OK)
2625
 
    {
 
2473
    if (!drizzle_real_query(&drizzle,buf,length))
2626
2474
      return 0;
2627
 
    }
2628
 
    error= put_error(&con, result);
2629
 
 
2630
 
    if (ret == DRIZZLE_RETURN_ERROR_CODE)
2631
 
    {
2632
 
      *error_code= drizzle_result_error_code(result);
2633
 
      drizzle_result_free(result);
2634
 
    }
2635
 
 
2636
 
    if (ret != DRIZZLE_RETURN_SERVER_GONE || retry > 1 ||
 
2475
    error= put_error(&drizzle);
 
2476
    if (drizzle_errno(&drizzle) != CR_SERVER_GONE_ERROR || retry > 1 ||
2637
2477
        !opt_reconnect)
2638
 
    {
2639
2478
      return error;
2640
 
    }
2641
 
 
2642
2479
    if (reconnect())
2643
2480
      return error;
2644
2481
  }
2645
2482
}
2646
2483
 
2647
 
int drizzleclient_store_result_for_lazy(drizzle_result_st *result)
 
2484
int drizzle_store_result_for_lazy(DRIZZLE_RES **result)
2648
2485
{
2649
 
  if (drizzle_result_buffer(result) == DRIZZLE_RETURN_OK)
 
2486
  if ((*result=drizzle_store_result(&drizzle)))
2650
2487
    return 0;
2651
2488
 
2652
 
  if (drizzle_con_error(&con)[0])
2653
 
    return put_error(&con, result);
 
2489
  if (drizzle_error(&drizzle)[0])
 
2490
    return put_error(&drizzle);
2654
2491
  return 0;
2655
2492
}
2656
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= strend(help_arg);
 
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
 
2657
2597
static int
2658
 
com_help(string *buffer, const char *)
 
2598
com_help(string *buffer __attribute__((unused)),
 
2599
         const char *line __attribute__((unused)))
2659
2600
{
2660
2601
  register int i, j;
2661
 
  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
  }
2662
2609
 
2663
2610
  put_info(_("List of all Drizzle commands:"), INFO_INFO,0,0);
2664
2611
  if (!named_cmds)
2665
2612
    put_info(_("Note that all text commands must be first on line and end with ';'"),INFO_INFO,0,0);
2666
2613
  for (i = 0; commands[i].name; i++)
2667
2614
  {
2668
 
    end= strcpy(buff, commands[i].name);
2669
 
    end+= strlen(commands[i].name);
 
2615
    end= stpcpy(buff, commands[i].name);
2670
2616
    for (j= (int)strlen(commands[i].name); j < 10; j++)
2671
 
      end= strcpy(end, " ")+1;
 
2617
      end= stpcpy(end, " ");
2672
2618
    if (commands[i].func)
2673
2619
      tee_fprintf(stdout, "%s(\\%c) %s\n", buff,
2674
2620
                  commands[i].cmd_char, _(commands[i].doc));
2675
2621
  }
2676
 
  tee_fprintf(stdout, "\n");
2677
 
  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);
2678
2624
  return 0;
2679
2625
}
2680
2626
 
2681
2627
 
2682
2628
static int
2683
 
com_clear(string *buffer, const char *)
 
2629
com_clear(string *buffer,
 
2630
          const char *line __attribute__((unused)))
2684
2631
{
2685
2632
  if (status.add_to_history)
2686
2633
    fix_history(buffer);
2688
2635
  return 0;
2689
2636
}
2690
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
}
2691
2663
 
2692
2664
/*
2693
2665
  Execute command
2696
2668
  1  if fatal error
2697
2669
*/
2698
2670
static int
2699
 
com_go(string *buffer, const char *)
 
2671
com_go(string *buffer,
 
2672
       const char *line __attribute__((unused)))
2700
2673
{
2701
2674
  char          buff[200]; /* about 110 chars used so far */
2702
2675
  char          time_buff[52+3+1]; /* time max + space&parens + NUL */
2703
 
  drizzle_result_st result;
2704
 
  drizzle_return_t ret;
2705
 
  uint32_t      timer, warnings= 0;
2706
 
  uint32_t      error= 0;
2707
 
  uint32_t      error_code= 0;
 
2676
  DRIZZLE_RES     *result;
 
2677
  uint32_t         timer, warnings= 0;
 
2678
  uint          error= 0;
2708
2679
  int           err= 0;
2709
2680
 
2710
2681
  interrupted_query= 0;
2738
2709
 
2739
2710
  timer=start_timer();
2740
2711
  executing_query= 1;
2741
 
  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());
2742
2713
 
2743
2714
  if (status.add_to_history)
2744
2715
  {
2758
2729
 
2759
2730
    if (quick)
2760
2731
    {
2761
 
      if (drizzle_column_buffer(&result) != DRIZZLE_RETURN_OK)
 
2732
      if (!(result=drizzle_use_result(&drizzle)) && drizzle_field_count(&drizzle))
2762
2733
      {
2763
 
        error= put_error(&con, &result);
 
2734
        error= put_error(&drizzle);
2764
2735
        goto end;
2765
2736
      }
2766
2737
    }
2767
2738
    else
2768
2739
    {
2769
 
      error= drizzleclient_store_result_for_lazy(&result);
 
2740
      error= drizzle_store_result_for_lazy(&result);
2770
2741
      if (error)
2771
2742
        goto end;
2772
2743
    }
2777
2748
      time_buff[0]= '\0';
2778
2749
 
2779
2750
    /* Every branch must truncate  buff . */
2780
 
    if (drizzle_result_column_count(&result) > 0)
 
2751
    if (result)
2781
2752
    {
2782
 
      if (!quick && drizzle_result_row_count(&result) == 0 &&
2783
 
          !column_types_flag)
 
2753
      if (!drizzle_num_rows(result) && ! quick && !column_types_flag)
2784
2754
      {
2785
 
        strcpy(buff, _("Empty set"));
 
2755
        stpcpy(buff, _("Empty set"));
2786
2756
      }
2787
2757
      else
2788
2758
      {
2789
2759
        init_pager();
2790
2760
        if (vertical || (auto_vertical_output &&
2791
 
                         (terminal_width < get_result_width(&result))))
2792
 
          print_table_data_vertically(&result);
 
2761
                         (terminal_width < get_result_width(result))))
 
2762
          print_table_data_vertically(result);
2793
2763
        else if (opt_silent && verbose <= 2 && !output_tables)
2794
 
          print_tab_data(&result);
 
2764
          print_tab_data(result);
2795
2765
        else
2796
 
          print_table_data(&result);
 
2766
          print_table_data(result);
2797
2767
        sprintf(buff,
2798
2768
                ngettext("%ld row in set","%ld rows in set",
2799
 
                         (long) drizzle_result_row_count(&result)),
2800
 
                (long) drizzle_result_row_count(&result));
 
2769
                         (long) drizzle_num_rows(result)),
 
2770
                (long) drizzle_num_rows(result));
2801
2771
        end_pager();
2802
 
        if (drizzle_result_error_code(&result))
2803
 
          error= put_error(&con, &result);
 
2772
        if (drizzle_errno(&drizzle))
 
2773
          error= put_error(&drizzle);
2804
2774
      }
2805
2775
    }
2806
 
    else if (drizzle_result_affected_rows(&result) == ~(uint64_t) 0)
2807
 
      strcpy(buff,_("Query OK"));
 
2776
    else if (drizzle_affected_rows(&drizzle) == ~(uint64_t) 0)
 
2777
      stpcpy(buff,_("Query OK"));
2808
2778
    else
2809
2779
      sprintf(buff, ngettext("Query OK, %ld row affected",
2810
2780
                             "Query OK, %ld rows affected",
2811
 
                             (long) drizzle_result_affected_rows(&result)),
2812
 
              (long) drizzle_result_affected_rows(&result));
 
2781
                             (long) drizzle_affected_rows(&drizzle)),
 
2782
              (long) drizzle_affected_rows(&drizzle));
2813
2783
 
2814
 
    pos= strchr(buff, '\0');
2815
 
    if ((warnings= drizzle_result_warning_count(&result)))
 
2784
    pos=strend(buff);
 
2785
    if ((warnings= drizzle_warning_count(&drizzle)))
2816
2786
    {
2817
2787
      *pos++= ',';
2818
2788
      *pos++= ' ';
2819
 
      char warnings_buff[20];
2820
 
      memset(warnings_buff,0,20);
2821
 
      sprintf(warnings_buff, "%d", warnings);
2822
 
      strcpy(pos, warnings_buff);
2823
 
      pos+= strlen(warnings_buff);
2824
 
      pos= strcpy(pos, " warning")+8;
 
2789
      pos=int10_to_str(warnings, pos, 10);
 
2790
      pos=stpcpy(pos, " warning");
2825
2791
      if (warnings != 1)
2826
2792
        *pos++= 's';
2827
2793
    }
2828
 
    strcpy(pos, time_buff);
 
2794
    stpcpy(pos, time_buff);
2829
2795
    put_info(buff,INFO_RESULT,0,0);
2830
 
    if (strcmp(drizzle_result_info(&result), ""))
2831
 
      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);
2832
2798
    put_info("",INFO_RESULT,0,0);      // Empty row
2833
2799
 
2834
 
    if (unbuffered)
 
2800
    if (result && !drizzle_eof(result))  /* Something wrong when using quick */
 
2801
      error= put_error(&drizzle);
 
2802
    else if (unbuffered)
2835
2803
      fflush(stdout);
2836
 
    drizzle_result_free(&result);
2837
 
 
2838
 
    if (drizzle_con_status(&con) & DRIZZLE_CON_STATUS_MORE_RESULTS_EXISTS)
2839
 
    {
2840
 
      if (drizzle_result_read(&con, &result, &ret) == NULL ||
2841
 
          ret != DRIZZLE_RETURN_OK)
2842
 
      {
2843
 
        if (ret == DRIZZLE_RETURN_ERROR_CODE)
2844
 
        {
2845
 
          error_code= drizzle_result_error_code(&result);
2846
 
          drizzle_result_free(&result);
2847
 
        }
2848
 
 
2849
 
        error= put_error(&con, NULL);
2850
 
        goto end;
2851
 
      }
2852
 
    }
2853
 
 
2854
 
  } while (drizzle_con_status(&con) & DRIZZLE_CON_STATUS_MORE_RESULTS_EXISTS);
 
2804
    drizzle_free_result(result);
 
2805
  } while (!(err= drizzle_next_result(&drizzle)));
2855
2806
  if (err >= 1)
2856
 
    error= put_error(&con, NULL);
 
2807
    error= put_error(&drizzle);
2857
2808
 
2858
2809
end:
2859
2810
 
2860
2811
  /* Show warnings if any or error occured */
2861
2812
  if (show_warnings == 1 && (warnings >= 1 || error))
2862
 
    print_warnings(error_code);
 
2813
    print_warnings();
2863
2814
 
2864
2815
  if (!error && !status.batch &&
2865
 
      drizzle_con_status(&con) & DRIZZLE_CON_STATUS_DB_DROPPED)
2866
 
  {
 
2816
      (drizzle.server_status & SERVER_STATUS_DB_DROPPED))
2867
2817
    get_current_db();
2868
 
  }
2869
2818
 
2870
2819
  executing_query= 0;
2871
2820
  return error;        /* New command follows */
2898
2847
  FILE* new_outfile;
2899
2848
  if (opt_outfile)
2900
2849
    end_tee();
2901
 
  if (!(new_outfile= fopen(file_name, "a")))
 
2850
  if (!(new_outfile= my_fopen(file_name, O_APPEND | O_WRONLY, MYF(MY_WME))))
2902
2851
  {
2903
2852
    tee_fprintf(stdout, "Error logging to file '%s'\n", file_name);
2904
2853
    return;
2905
2854
  }
2906
2855
  OUTFILE = new_outfile;
2907
 
  strncpy(outfile, file_name, FN_REFLEN-1);
 
2856
  strmake(outfile, file_name, FN_REFLEN-1);
2908
2857
  tee_fprintf(stdout, "Logging to file '%s'\n", file_name);
2909
2858
  opt_outfile= 1;
2910
 
 
2911
2859
  return;
2912
2860
}
2913
2861
 
2914
2862
 
2915
2863
static void end_tee()
2916
2864
{
2917
 
  fclose(OUTFILE);
 
2865
  my_fclose(OUTFILE, MYF(0));
2918
2866
  OUTFILE= 0;
2919
2867
  opt_outfile= 0;
2920
2868
  return;
2933
2881
}
2934
2882
 
2935
2883
 
2936
 
static const char *fieldtype2str(drizzle_column_type_t type)
 
2884
static const char *fieldtype2str(enum enum_field_types type)
2937
2885
{
2938
2886
  switch (type) {
2939
 
    case DRIZZLE_COLUMN_TYPE_BLOB:        return "BLOB";
2940
 
    case DRIZZLE_COLUMN_TYPE_DATE:        return "DATE";
2941
 
    case DRIZZLE_COLUMN_TYPE_DATETIME:    return "DATETIME";
2942
 
    case DRIZZLE_COLUMN_TYPE_NEWDECIMAL:  return "DECIMAL";
2943
 
    case DRIZZLE_COLUMN_TYPE_DOUBLE:      return "DOUBLE";
2944
 
    case DRIZZLE_COLUMN_TYPE_ENUM:        return "ENUM";
2945
 
    case DRIZZLE_COLUMN_TYPE_LONG:        return "LONG";
2946
 
    case DRIZZLE_COLUMN_TYPE_LONGLONG:    return "LONGLONG";
2947
 
    case DRIZZLE_COLUMN_TYPE_NULL:        return "NULL";
2948
 
    case DRIZZLE_COLUMN_TYPE_TIMESTAMP:   return "TIMESTAMP";
 
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";
2949
2900
    default:                     return "?-unknown-?";
2950
2901
  }
2951
2902
}
2952
2903
 
2953
 
static char *fieldflags2str(uint32_t f) {
 
2904
static char *fieldflags2str(uint f) {
2954
2905
  static char buf[1024];
2955
2906
  char *s=buf;
2956
2907
  *s=0;
2957
2908
#define ff2s_check_flag(X)                                              \
2958
 
  if (f & DRIZZLE_COLUMN_FLAGS_ ## X) { s=strcpy(s, # X " ")+strlen(# X " "); \
2959
 
                        f &= ~ DRIZZLE_COLUMN_FLAGS_ ## X; }
 
2909
  if (f & X ## _FLAG) { s=stpcpy(s, # X " "); f &= ~ X ## _FLAG; }
2960
2910
  ff2s_check_flag(NOT_NULL);
2961
2911
  ff2s_check_flag(PRI_KEY);
2962
2912
  ff2s_check_flag(UNIQUE_KEY);
2982
2932
}
2983
2933
 
2984
2934
static void
2985
 
print_field_types(drizzle_result_st *result)
 
2935
print_field_types(DRIZZLE_RES *result)
2986
2936
{
2987
 
  drizzle_column_st   *field;
2988
 
  uint32_t i=0;
 
2937
  DRIZZLE_FIELD   *field;
 
2938
  uint i=0;
2989
2939
 
2990
 
  while ((field = drizzle_column_next(result)))
 
2940
  while ((field = drizzle_fetch_field(result)))
2991
2941
  {
2992
2942
    tee_fprintf(PAGER, "Field %3u:  `%s`\n"
2993
2943
                "Catalog:    `%s`\n"
3001
2951
                "Decimals:   %u\n"
3002
2952
                "Flags:      %s\n\n",
3003
2953
                ++i,
3004
 
                drizzle_column_name(field), drizzle_column_catalog(field),
3005
 
                drizzle_column_db(field), drizzle_column_table(field),
3006
 
                drizzle_column_orig_table(field),
3007
 
                fieldtype2str(drizzle_column_type(field)),
3008
 
                get_charset_name(drizzle_column_charset(field)),
3009
 
                drizzle_column_charset(field), drizzle_column_size(field),
3010
 
                drizzle_column_max_size(field), drizzle_column_decimals(field),
3011
 
                fieldflags2str(drizzle_column_flags(field)));
 
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));
3012
2959
  }
3013
2960
  tee_puts("", PAGER);
3014
2961
}
3015
2962
 
3016
2963
 
3017
2964
static void
3018
 
print_table_data(drizzle_result_st *result)
 
2965
print_table_data(DRIZZLE_RES *result)
3019
2966
{
3020
 
  drizzle_row_t cur;
3021
 
  drizzle_return_t ret;
3022
 
  drizzle_column_st *field;
3023
 
  bool *num_flag;
 
2967
  DRIZZLE_ROW     cur;
 
2968
  DRIZZLE_FIELD   *field;
 
2969
  bool          *num_flag;
3024
2970
  string separator;
3025
 
 
 
2971
  
3026
2972
  separator.reserve(256);
3027
2973
 
3028
 
  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));
3029
2976
  if (column_types_flag)
3030
2977
  {
3031
2978
    print_field_types(result);
3032
 
    if (!drizzle_result_row_count(result))
 
2979
    if (!drizzle_num_rows(result))
3033
2980
      return;
3034
 
    drizzle_column_seek(result,0);
 
2981
    drizzle_field_seek(result,0);
3035
2982
  }
3036
2983
  separator.append("+");
3037
 
  while ((field = drizzle_column_next(result)))
 
2984
  while ((field = drizzle_fetch_field(result)))
3038
2985
  {
3039
 
    uint32_t x, length= 0;
3040
 
 
3041
 
    if (column_names)
3042
 
    {
3043
 
      uint32_t name_length= strlen(drizzle_column_name(field));
3044
 
 
3045
 
      /* Check if the max_byte value is really the maximum in terms
3046
 
         of visual length since multibyte characters can affect the
3047
 
         length of the separator. */
3048
 
      length= charset_info->cset->numcells(charset_info,
3049
 
                                           drizzle_column_name(field),
3050
 
                                           drizzle_column_name(field) +
3051
 
                                           name_length);
3052
 
 
3053
 
      if (name_length == drizzle_column_max_size(field))
3054
 
      {
3055
 
        if (length < drizzle_column_max_size(field))
3056
 
          drizzle_column_set_max_size(field, length);
3057
 
      }
3058
 
      else
3059
 
      {
3060
 
        length= name_length;
3061
 
      }
3062
 
    }
3063
 
  
 
2986
    uint32_t length= column_names ? field->name_length : 0;
3064
2987
    if (quick)
3065
 
      length=max(length,drizzle_column_size(field));
 
2988
      length=max(length,field->length);
3066
2989
    else
3067
 
      length=max(length,(uint32_t)drizzle_column_max_size(field));
3068
 
    if (length < 4 &&
3069
 
        !(drizzle_column_flags(field) & DRIZZLE_COLUMN_FLAGS_NOT_NULL))
3070
 
    {
 
2990
      length=max(length,field->max_length);
 
2991
    if (length < 4 && !IS_NOT_NULL(field->flags))
3071
2992
      // Room for "NULL"
3072
2993
      length=4;
3073
 
    }
3074
 
    drizzle_column_set_max_size(field, length);
3075
 
 
 
2994
    field->max_length=length;
 
2995
    uint x;
3076
2996
    for (x=0; x< (length+2); x++)
3077
2997
      separator.append("-");
3078
2998
    separator.append("+");
3081
3001
  tee_puts((char*) separator.c_str(), PAGER);
3082
3002
  if (column_names)
3083
3003
  {
3084
 
    drizzle_column_seek(result,0);
 
3004
    drizzle_field_seek(result,0);
3085
3005
    (void) tee_fputs("|", PAGER);
3086
 
    for (uint32_t off=0; (field = drizzle_column_next(result)) ; off++)
 
3006
    for (uint off=0; (field = drizzle_fetch_field(result)) ; off++)
3087
3007
    {
3088
 
      uint32_t name_length= (uint32_t) strlen(drizzle_column_name(field));
3089
 
      uint32_t numcells= charset_info->cset->numcells(charset_info,
3090
 
                                                  drizzle_column_name(field),
3091
 
                                                  drizzle_column_name(field) +
3092
 
                                                  name_length);
3093
 
      uint32_t display_length= drizzle_column_max_size(field) + name_length -
3094
 
                               numcells;
 
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;
3095
3013
      tee_fprintf(PAGER, " %-*s |",(int) min(display_length,
3096
3014
                                             MAX_COLUMN_LENGTH),
3097
 
                  drizzle_column_name(field));
3098
 
      num_flag[off]= ((drizzle_column_type(field) <= DRIZZLE_COLUMN_TYPE_LONGLONG) ||
3099
 
                      (drizzle_column_type(field) == DRIZZLE_COLUMN_TYPE_NEWDECIMAL));
 
3015
                  field->name);
 
3016
      num_flag[off]= IS_NUM(field->type);
3100
3017
    }
3101
3018
    (void) tee_fputs("\n", PAGER);
3102
3019
    tee_puts((char*) separator.c_str(), PAGER);
3103
3020
  }
3104
3021
 
3105
 
  while (1)
 
3022
  while ((cur= drizzle_fetch_row(result)))
3106
3023
  {
3107
 
    if (quick)
3108
 
    {
3109
 
      cur= drizzle_row_buffer(result, &ret);
3110
 
      if (ret != DRIZZLE_RETURN_OK)
3111
 
      {
3112
 
        (void)put_error(&con, result);
3113
 
        break;
3114
 
      }
3115
 
    }
3116
 
    else
3117
 
      cur= drizzle_row_next(result);
3118
 
 
3119
 
    if (cur == NULL || interrupted_query)
 
3024
    if (interrupted_query)
3120
3025
      break;
3121
 
 
3122
 
    size_t *lengths= drizzle_row_field_sizes(result);
 
3026
    uint32_t *lengths= drizzle_fetch_lengths(result);
3123
3027
    (void) tee_fputs("| ", PAGER);
3124
 
    drizzle_column_seek(result, 0);
3125
 
    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++)
3126
3030
    {
3127
3031
      const char *buffer;
3128
 
      uint32_t data_length;
3129
 
      uint32_t field_max_length;
3130
 
      uint32_t visible_length;
3131
 
      uint32_t extra_padding;
 
3032
      uint data_length;
 
3033
      uint field_max_length;
 
3034
      uint visible_length;
 
3035
      uint extra_padding;
3132
3036
 
3133
3037
      if (cur[off] == NULL)
3134
3038
      {
3138
3042
      else
3139
3043
      {
3140
3044
        buffer= cur[off];
3141
 
        data_length= (uint32_t) lengths[off];
 
3045
        data_length= (uint) lengths[off];
3142
3046
      }
3143
3047
 
3144
 
      field= drizzle_column_next(result);
3145
 
      field_max_length= drizzle_column_max_size(field);
 
3048
      field= drizzle_fetch_field(result);
 
3049
      field_max_length= field->max_length;
3146
3050
 
3147
3051
      /*
3148
3052
        How many text cells on the screen will this string span?  If it contains
3156
3060
      extra_padding= data_length - visible_length;
3157
3061
 
3158
3062
      if (field_max_length > MAX_COLUMN_LENGTH)
3159
 
        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);
3160
3064
      else
3161
3065
      {
3162
3066
        if (num_flag[off] != 0) /* if it is numeric, we right-justify it */
3163
 
          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);
3164
3068
        else
3165
3069
          tee_print_sized_data(buffer, data_length,
3166
 
                               field_max_length+extra_padding, false);
 
3070
                               field_max_length+extra_padding, FALSE);
3167
3071
      }
3168
3072
      tee_fputs(" | ", PAGER);
3169
3073
    }
3170
3074
    (void) tee_fputs("\n", PAGER);
3171
 
    if (quick)
3172
 
      drizzle_row_free(result, cur);
3173
3075
  }
3174
3076
  tee_puts(separator.c_str(), PAGER);
3175
 
  free(num_flag);
 
3077
  my_free(num_flag, MYF(MY_ALLOW_ZERO_PTR));
3176
3078
}
3177
3079
 
3178
3080
/**
3191
3093
 
3192
3094
   @returns  number of character positions to be used, at most
3193
3095
*/
3194
 
static int get_field_disp_length(drizzle_column_st *field)
 
3096
static int get_field_disp_length(DRIZZLE_FIELD *field)
3195
3097
{
3196
 
  uint32_t length= column_names ? strlen(drizzle_column_name(field)) : 0;
 
3098
  uint length= column_names ? field->name_length : 0;
3197
3099
 
3198
3100
  if (quick)
3199
 
    length= max(length, drizzle_column_size(field));
 
3101
    length= max(length, field->length);
3200
3102
  else
3201
 
    length= max(length, (uint32_t)drizzle_column_max_size(field));
 
3103
    length= max(length, field->max_length);
3202
3104
 
3203
 
  if (length < 4 &&
3204
 
    !(drizzle_column_flags(field) & DRIZZLE_COLUMN_FLAGS_NOT_NULL))
3205
 
  {
 
3105
  if (length < 4 && !IS_NOT_NULL(field->flags))
3206
3106
    length= 4;        /* Room for "NULL" */
3207
 
  }
3208
3107
 
3209
3108
  return length;
3210
3109
}
3217
3116
 
3218
3117
   @returns  The max number of characters in any row of this result
3219
3118
*/
3220
 
static int get_result_width(drizzle_result_st *result)
 
3119
static int get_result_width(DRIZZLE_RES *result)
3221
3120
{
3222
3121
  unsigned int len= 0;
3223
 
  drizzle_column_st *field;
3224
 
  uint16_t offset;
 
3122
  DRIZZLE_FIELD *field;
 
3123
  DRIZZLE_FIELD_OFFSET offset;
3225
3124
 
3226
 
  offset= drizzle_column_current(result);
 
3125
  offset= drizzle_field_tell(result);
3227
3126
  assert(offset == 0);
3228
3127
 
3229
 
  while ((field= drizzle_column_next(result)) != NULL)
 
3128
  while ((field= drizzle_fetch_field(result)) != NULL)
3230
3129
    len+= get_field_disp_length(field) + 3; /* plus bar, space, & final space */
3231
3130
 
3232
 
  (void) drizzle_column_seek(result, offset);
 
3131
  (void) drizzle_field_seek(result, offset);
3233
3132
 
3234
3133
  return len + 1; /* plus final bar. */
3235
3134
}
3265
3164
 
3266
3165
 
3267
3166
static void
3268
 
print_table_data_vertically(drizzle_result_st *result)
 
3167
print_table_data_vertically(DRIZZLE_RES *result)
3269
3168
{
3270
 
  drizzle_row_t cur;
3271
 
  drizzle_return_t ret;
3272
 
  uint32_t max_length=0;
3273
 
  drizzle_column_st *field;
 
3169
  DRIZZLE_ROW  cur;
 
3170
  uint    max_length=0;
 
3171
  DRIZZLE_FIELD  *field;
3274
3172
 
3275
 
  while ((field = drizzle_column_next(result)))
 
3173
  while ((field = drizzle_fetch_field(result)))
3276
3174
  {
3277
 
    uint32_t length= strlen(drizzle_column_name(field));
 
3175
    uint length= field->name_length;
3278
3176
    if (length > max_length)
3279
3177
      max_length= length;
3280
 
    drizzle_column_set_max_size(field, length);
 
3178
    field->max_length=length;
3281
3179
  }
3282
3180
 
3283
 
  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++)
3284
3183
  {
3285
 
    if (quick)
3286
 
    {
3287
 
      cur= drizzle_row_buffer(result, &ret);
3288
 
      if (ret != DRIZZLE_RETURN_OK)
3289
 
      {
3290
 
        (void)put_error(&con, result);
3291
 
        break;
3292
 
      }
3293
 
    }
3294
 
    else
3295
 
      cur= drizzle_row_next(result);
3296
 
 
3297
 
    if (cur == NULL || interrupted_query)
 
3184
    if (interrupted_query)
3298
3185
      break;
3299
 
    drizzle_column_seek(result,0);
 
3186
    drizzle_field_seek(result,0);
3300
3187
    tee_fprintf(PAGER,
3301
3188
                "*************************** %d. row ***************************\n", row_count);
3302
 
    for (uint32_t off=0; off < drizzle_result_column_count(result); off++)
 
3189
    for (uint off=0; off < drizzle_num_fields(result); off++)
3303
3190
    {
3304
 
      field= drizzle_column_next(result);
3305
 
      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);
3306
3193
      tee_fprintf(PAGER, "%s\n",cur[off] ? (char*) cur[off] : "NULL");
3307
3194
    }
3308
 
    if (quick)
3309
 
      drizzle_row_free(result, cur);
3310
3195
  }
3311
3196
}
3312
3197
 
3313
3198
 
3314
3199
/* print_warnings should be called right after executing a statement */
3315
3200
 
3316
 
static void print_warnings(uint32_t error_code)
 
3201
static void print_warnings()
3317
3202
{
3318
 
  const char *query;
3319
 
  drizzle_result_st result;
3320
 
  drizzle_row_t cur;
 
3203
  const char   *query;
 
3204
  DRIZZLE_RES    *result;
 
3205
  DRIZZLE_ROW    cur;
3321
3206
  uint64_t num_rows;
3322
 
  uint32_t new_code= 0;
 
3207
 
 
3208
  /* Save current error before calling "show warnings" */
 
3209
  uint error= drizzle_errno(&drizzle);
3323
3210
 
3324
3211
  /* Get the warnings */
3325
3212
  query= "show warnings";
3326
 
  drizzleclient_real_query_for_lazy(query, strlen(query),&result,&new_code);
3327
 
  drizzleclient_store_result_for_lazy(&result);
 
3213
  drizzle_real_query_for_lazy(query, strlen(query));
 
3214
  drizzle_store_result_for_lazy(&result);
3328
3215
 
3329
3216
  /* Bail out when no warnings */
3330
 
  if (!(num_rows= drizzle_result_row_count(&result)))
 
3217
  if (!(num_rows= drizzle_num_rows(result)))
3331
3218
    goto end;
3332
3219
 
3333
 
  cur= drizzle_row_next(&result);
 
3220
  cur= drizzle_fetch_row(result);
3334
3221
 
3335
3222
  /*
3336
3223
    Don't print a duplicate of the current error.  It is possible for SHOW
3338
3225
    messages.  To be safe, skip printing the duplicate only if it is the only
3339
3226
    warning.
3340
3227
  */
3341
 
  if (!cur || (num_rows == 1 &&
3342
 
      error_code == (uint32_t) strtoul(cur[1], NULL, 10)))
3343
 
  {
 
3228
  if (!cur || (num_rows == 1 && error == (uint) strtoul(cur[1], NULL, 10)))
3344
3229
    goto end;
3345
 
  }
3346
3230
 
3347
3231
  /* Print the warnings */
3348
3232
  init_pager();
3349
3233
  do
3350
3234
  {
3351
3235
    tee_fprintf(PAGER, "%s (Code %s): %s\n", cur[0], cur[1], cur[2]);
3352
 
  } while ((cur= drizzle_row_next(&result)));
 
3236
  } while ((cur= drizzle_fetch_row(result)));
3353
3237
  end_pager();
3354
3238
 
3355
3239
end:
3356
 
  drizzle_result_free(&result);
 
3240
  drizzle_free_result(result);
3357
3241
}
3358
3242
 
3359
3243
 
3367
3251
    if (opt_raw_data)
3368
3252
      tee_fputs(pos, PAGER);
3369
3253
    else for (const char *end=pos+length ; pos != end ; pos++)
3370
 
    {
3371
 
      int l;
3372
 
      if (use_mb(charset_info) &&
3373
 
          (l = my_ismbchar(charset_info, pos, end)))
3374
 
      {
3375
 
        while (l--)
3376
 
          tee_putc(*pos++, PAGER);
3377
 
        pos--;
3378
 
        continue;
3379
 
      }
3380
 
      if (!*pos)
3381
 
        tee_fputs("\\0", PAGER); // This makes everything hard
3382
 
      else if (*pos == '\t')
3383
 
        tee_fputs("\\t", PAGER); // This would destroy tab format
3384
 
      else if (*pos == '\n')
3385
 
        tee_fputs("\\n", PAGER); // This too
3386
 
      else if (*pos == '\\')
3387
 
        tee_fputs("\\\\", PAGER);
3388
 
      else
3389
 
        tee_putc(*pos, PAGER);
3390
 
    }
 
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
         }
3391
3277
  }
3392
3278
}
3393
3279
 
3394
3280
 
3395
3281
static void
3396
 
print_tab_data(drizzle_result_st *result)
 
3282
print_tab_data(DRIZZLE_RES *result)
3397
3283
{
3398
 
  drizzle_row_t cur;
3399
 
  drizzle_return_t ret;
3400
 
  drizzle_column_st *field;
3401
 
  size_t *lengths;
 
3284
  DRIZZLE_ROW  cur;
 
3285
  DRIZZLE_FIELD  *field;
 
3286
  uint32_t    *lengths;
3402
3287
 
3403
3288
  if (opt_silent < 2 && column_names)
3404
3289
  {
3405
3290
    int first=0;
3406
 
    while ((field = drizzle_column_next(result)))
 
3291
    while ((field = drizzle_fetch_field(result)))
3407
3292
    {
3408
3293
      if (first++)
3409
3294
        (void) tee_fputs("\t", PAGER);
3410
 
      (void) tee_fputs(drizzle_column_name(field), PAGER);
 
3295
      (void) tee_fputs(field->name, PAGER);
3411
3296
    }
3412
3297
    (void) tee_fputs("\n", PAGER);
3413
3298
  }
3414
 
  while (1)
 
3299
  while ((cur = drizzle_fetch_row(result)))
3415
3300
  {
3416
 
    if (quick)
3417
 
    {
3418
 
      cur= drizzle_row_buffer(result, &ret);
3419
 
      if (ret != DRIZZLE_RETURN_OK)
3420
 
      {
3421
 
        (void)put_error(&con, result);
3422
 
        break;
3423
 
      }
3424
 
    }
3425
 
    else
3426
 
      cur= drizzle_row_next(result);
3427
 
 
3428
 
    if (cur == NULL)
3429
 
      break;
3430
 
 
3431
 
    lengths= drizzle_row_field_sizes(result);
 
3301
    lengths= drizzle_fetch_lengths(result);
3432
3302
    safe_put_field(cur[0],lengths[0]);
3433
 
    for (uint32_t off=1 ; off < drizzle_result_column_count(result); off++)
 
3303
    for (uint off=1 ; off < drizzle_num_fields(result); off++)
3434
3304
    {
3435
3305
      (void) tee_fputs("\t", PAGER);
3436
3306
      safe_put_field(cur[off], lengths[off]);
3437
3307
    }
3438
3308
    (void) tee_fputs("\n", PAGER);
3439
 
    if (quick)
3440
 
      drizzle_row_free(result, cur);
3441
3309
  }
3442
3310
}
3443
3311
 
3444
3312
static int
3445
 
com_tee(string *, const char *line )
 
3313
com_tee(string *buffer __attribute__((unused)), const char *line )
3446
3314
{
3447
 
  char file_name[FN_REFLEN], *end;
3448
 
  const char *param;
 
3315
  char file_name[FN_REFLEN], *end, *param;
3449
3316
 
3450
3317
  if (status.batch)
3451
3318
    return 0;
3470
3337
  /* eliminate the spaces before the parameters */
3471
3338
  while (my_isspace(charset_info,*param))
3472
3339
    param++;
3473
 
  strncpy(file_name, param, sizeof(file_name) - 1);
3474
 
  end= file_name + strlen(file_name);
 
3340
  end= strmake(file_name, param, sizeof(file_name) - 1);
3475
3341
  /* remove end space from command line */
3476
3342
  while (end > file_name && (my_isspace(charset_info,end[-1]) ||
3477
3343
                             my_iscntrl(charset_info,end[-1])))
3488
3354
 
3489
3355
 
3490
3356
static int
3491
 
com_notee(string *, const char *)
 
3357
com_notee(string *buffer __attribute__((unused)),
 
3358
          const char *line __attribute__((unused)))
3492
3359
{
3493
3360
  if (opt_outfile)
3494
3361
    end_tee();
3501
3368
*/
3502
3369
 
3503
3370
static int
3504
 
com_pager(string *, const char *line)
 
3371
com_pager(string *buffer __attribute__((unused)),
 
3372
          const char *line __attribute__((unused)))
3505
3373
{
3506
 
  char pager_name[FN_REFLEN], *end;
3507
 
  const char *param;
 
3374
  char pager_name[FN_REFLEN], *end, *param;
3508
3375
 
3509
3376
  if (status.batch)
3510
3377
    return 0;
3522
3389
    {
3523
3390
      tee_fprintf(stdout, "Default pager wasn't set, using stdout.\n");
3524
3391
      opt_nopager=1;
3525
 
      strcpy(pager, "stdout");
 
3392
      stpcpy(pager, "stdout");
3526
3393
      PAGER= stdout;
3527
3394
      return 0;
3528
3395
    }
3529
 
    strcpy(pager, default_pager);
 
3396
    stpcpy(pager, default_pager);
3530
3397
  }
3531
3398
  else
3532
3399
  {
3533
 
    end= strncpy(pager_name, param, sizeof(pager_name)-1);
3534
 
    end+= strlen(pager_name);
 
3400
    end= strmake(pager_name, param, sizeof(pager_name)-1);
3535
3401
    while (end > pager_name && (my_isspace(charset_info,end[-1]) ||
3536
3402
                                my_iscntrl(charset_info,end[-1])))
3537
3403
      end--;
3538
3404
    end[0]=0;
3539
 
    strcpy(pager, pager_name);
3540
 
    strcpy(default_pager, pager_name);
 
3405
    stpcpy(pager, pager_name);
 
3406
    stpcpy(default_pager, pager_name);
3541
3407
  }
3542
3408
  opt_nopager=0;
3543
3409
  tee_fprintf(stdout, "PAGER set to '%s'\n", pager);
3546
3412
 
3547
3413
 
3548
3414
static int
3549
 
com_nopager(string *, const char *)
 
3415
com_nopager(string *buffer __attribute__((unused)),
 
3416
            const char *line __attribute__((unused)))
3550
3417
{
3551
 
  strcpy(pager, "stdout");
 
3418
  stpcpy(pager, "stdout");
3552
3419
  opt_nopager=1;
3553
3420
  PAGER= stdout;
3554
3421
  tee_fprintf(stdout, "PAGER set to stdout\n");
3558
3425
/* If arg is given, exit without errors. This happens on command 'quit' */
3559
3426
 
3560
3427
static int
3561
 
com_quit(string *, const char *)
 
3428
com_quit(string *buffer __attribute__((unused)),
 
3429
         const char *line __attribute__((unused)))
3562
3430
{
3563
3431
  /* let the screen auto close on a normal shutdown */
3564
3432
  status.exit_status=0;
3566
3434
}
3567
3435
 
3568
3436
static int
3569
 
com_rehash(string *, const char *)
 
3437
com_rehash(string *buffer __attribute__((unused)),
 
3438
           const char *line __attribute__((unused)))
3570
3439
{
3571
3440
  build_completion_hash(1, 0);
3572
3441
  return 0;
3575
3444
 
3576
3445
 
3577
3446
static int
3578
 
com_print(string *buffer,const char *)
 
3447
com_print(string *buffer,const char *line __attribute__((unused)))
3579
3448
{
3580
3449
  tee_puts("--------------", stdout);
3581
3450
  (void) tee_fputs(buffer->c_str(), stdout);
3602
3471
      Two null bytes are needed in the end of buff to allow
3603
3472
      get_arg to find end of string the second time it's called.
3604
3473
    */
3605
 
    tmp= strncpy(buff, line, sizeof(buff)-2);
 
3474
    tmp= strmake(buff, line, sizeof(buff)-2);
3606
3475
#ifdef EXTRA_DEBUG
3607
3476
    tmp[1]= 0;
3608
3477
#endif
3609
3478
    tmp= get_arg(buff, 0);
3610
3479
    if (tmp && *tmp)
3611
3480
    {
3612
 
      free(current_db);
 
3481
      my_free(current_db, MYF(MY_ALLOW_ZERO_PTR));
3613
3482
      current_db= strdup(tmp);
3614
3483
      tmp= get_arg(buff, 1);
3615
3484
      if (tmp)
3616
3485
      {
3617
 
        free(current_host);
 
3486
        my_free(current_host,MYF(MY_ALLOW_ZERO_PTR));
3618
3487
        current_host=strdup(tmp);
3619
3488
      }
3620
3489
    }
3621
3490
    else
3622
3491
    {
3623
3492
      /* Quick re-connect */
3624
 
      opt_rehash= 0;
 
3493
      opt_rehash= 0;                            /* purecov: tested */
3625
3494
    }
3626
3495
    // command used
3627
3496
    assert(buffer!=NULL);
3634
3503
 
3635
3504
  if (connected)
3636
3505
  {
3637
 
    sprintf(buff,"Connection id:    %u",drizzle_con_thread_id(&con));
 
3506
    sprintf(buff,"Connection id:    %u",drizzle_thread_id(&drizzle));
3638
3507
    put_info(buff,INFO_INFO,0,0);
3639
3508
    sprintf(buff,"Current database: %.128s\n",
3640
3509
            current_db ? current_db : "*** NONE ***");
3644
3513
}
3645
3514
 
3646
3515
 
3647
 
static int com_source(string *, const char *line)
 
3516
static int com_source(string *buffer __attribute__((unused)), const char *line)
3648
3517
{
3649
 
  char source_name[FN_REFLEN], *end;
3650
 
  const char *param;
3651
 
  LineBuffer *line_buff;
 
3518
  char source_name[FN_REFLEN], *end, *param;
 
3519
  LINE_BUFFER *line_buff;
3652
3520
  int error;
3653
3521
  STATUS old_status;
3654
3522
  FILE *sql_file;
3661
3529
                    INFO_ERROR, 0,0);
3662
3530
  while (my_isspace(charset_info,*param))
3663
3531
    param++;
3664
 
  end= strncpy(source_name,param,sizeof(source_name)-1);
3665
 
  end+= strlen(source_name);
 
3532
  end=strmake(source_name,param,sizeof(source_name)-1);
3666
3533
  while (end > source_name && (my_isspace(charset_info,end[-1]) ||
3667
3534
                               my_iscntrl(charset_info,end[-1])))
3668
3535
    end--;
3669
3536
  end[0]=0;
3670
3537
  unpack_filename(source_name,source_name);
3671
3538
  /* open file name */
3672
 
  if (!(sql_file = fopen(source_name, "r")))
 
3539
  if (!(sql_file = my_fopen(source_name, O_RDONLY | O_BINARY,MYF(0))))
3673
3540
  {
3674
3541
    char buff[FN_REFLEN+60];
3675
3542
    sprintf(buff,"Failed to open file '%s', error: %d", source_name,errno);
3676
3543
    return put_info(buff, INFO_ERROR, 0 ,0);
3677
3544
  }
3678
3545
 
3679
 
  line_buff= new(std::nothrow) LineBuffer(opt_max_input_line,sql_file);
3680
 
  if (line_buff == NULL)
 
3546
  if (!(line_buff=batch_readline_init(opt_max_allowed_packet+512,sql_file)))
3681
3547
  {
3682
 
    fclose(sql_file);
3683
 
    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);
3684
3550
  }
3685
3551
 
3686
3552
  /* Save old status */
3697
3563
  error= read_and_execute(false);
3698
3564
  // Continue as before
3699
3565
  status=old_status;
3700
 
  fclose(sql_file);
3701
 
  delete status.line_buff;
3702
 
  line_buff= status.line_buff= 0;
 
3566
  my_fclose(sql_file,MYF(0));
 
3567
  batch_readline_end(line_buff);
3703
3568
  return error;
3704
3569
}
3705
3570
 
3706
3571
 
3707
3572
/* ARGSUSED */
3708
3573
static int
3709
 
com_delimiter(string *, const char *line)
 
3574
com_delimiter(string *buffer __attribute__((unused)), const char *line)
3710
3575
{
3711
3576
  char buff[256], *tmp;
3712
3577
 
3713
 
  strncpy(buff, line, sizeof(buff) - 1);
 
3578
  strmake(buff, line, sizeof(buff) - 1);
3714
3579
  tmp= get_arg(buff, 0);
3715
3580
 
3716
3581
  if (!tmp || !*tmp)
3728
3593
      return 0;
3729
3594
    }
3730
3595
  }
3731
 
  strncpy(delimiter, tmp, sizeof(delimiter) - 1);
 
3596
  strmake(delimiter, tmp, sizeof(delimiter) - 1);
3732
3597
  delimiter_length= (int)strlen(delimiter);
3733
3598
  delimiter_str= delimiter;
3734
3599
  return 0;
3736
3601
 
3737
3602
/* ARGSUSED */
3738
3603
static int
3739
 
com_use(string *, const char *line)
 
3604
com_use(string *buffer __attribute__((unused)), const char *line)
3740
3605
{
3741
3606
  char *tmp, buff[FN_REFLEN + 1];
3742
3607
  int select_db;
3743
 
  drizzle_result_st result;
3744
 
  drizzle_return_t ret;
3745
3608
 
3746
3609
  memset(buff, 0, sizeof(buff));
3747
 
  strncpy(buff, line, sizeof(buff) - 1);
 
3610
  strmake(buff, line, sizeof(buff) - 1);
3748
3611
  tmp= get_arg(buff, 0);
3749
3612
  if (!tmp || !*tmp)
3750
3613
  {
3758
3621
  */
3759
3622
  get_current_db();
3760
3623
 
3761
 
  if (!current_db || strcmp(current_db,tmp))
 
3624
  if (!current_db || cmp_database(charset_info, current_db,tmp))
3762
3625
  {
3763
3626
    if (one_database)
3764
3627
    {
3765
3628
      skip_updates= 1;
3766
 
      select_db= 0;    // don't do drizzleclient_select_db()
 
3629
      select_db= 0;    // don't do drizzle_select_db()
3767
3630
    }
3768
3631
    else
3769
 
      select_db= 2;    // do drizzleclient_select_db() and build_completion_hash()
 
3632
      select_db= 2;    // do drizzle_select_db() and build_completion_hash()
3770
3633
  }
3771
3634
  else
3772
3635
  {
3773
3636
    /*
3774
3637
      USE to the current db specified.
3775
 
      We do need to send drizzleclient_select_db() to make server
 
3638
      We do need to send drizzle_select_db() to make server
3776
3639
      update database level privileges, which might
3777
3640
      change since last USE (see bug#10979).
3778
3641
      For performance purposes, we'll skip rebuilding of completion hash.
3779
3642
    */
3780
3643
    skip_updates= 0;
3781
 
    select_db= 1;      // do only drizzleclient_select_db(), without completion
 
3644
    select_db= 1;      // do only drizzle_select_db(), without completion
3782
3645
  }
3783
3646
 
3784
3647
  if (select_db)
3789
3652
    */
3790
3653
    if (!connected && reconnect())
3791
3654
      return opt_reconnect ? -1 : 1;                        // Fatal error
3792
 
    for (bool try_again= true; try_again; try_again= false)
 
3655
    if (drizzle_select_db(&drizzle,tmp))
3793
3656
    {
3794
 
      if (drizzle_select_db(&con,&result,tmp,&ret) == NULL ||
3795
 
          ret != DRIZZLE_RETURN_OK)
3796
 
      {
3797
 
        if (ret == DRIZZLE_RETURN_ERROR_CODE)
3798
 
        {
3799
 
          int error= put_error(&con, &result);
3800
 
          drizzle_result_free(&result);
3801
 
          return error;
3802
 
        }
3803
 
 
3804
 
        if (ret != DRIZZLE_RETURN_SERVER_GONE || !try_again)
3805
 
          return put_error(&con, NULL);
3806
 
 
3807
 
        if (reconnect())
3808
 
          return opt_reconnect ? -1 : 1;                      // Fatal error
3809
 
      }
3810
 
      else
3811
 
        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);
3812
3664
    }
3813
 
    free(current_db);
 
3665
    my_free(current_db,MYF(MY_ALLOW_ZERO_PTR));
3814
3666
    current_db= strdup(tmp);
3815
3667
    if (select_db > 1)
3816
3668
      build_completion_hash(opt_rehash, 1);
3821
3673
}
3822
3674
 
3823
3675
static int
3824
 
com_warnings(string *, const char *)
 
3676
com_warnings(string *buffer __attribute__((unused)),
 
3677
             const char *line __attribute__((unused)))
3825
3678
{
3826
3679
  show_warnings = 1;
3827
3680
  put_info("Show warnings enabled.",INFO_INFO, 0, 0);
3829
3682
}
3830
3683
 
3831
3684
static int
3832
 
com_nowarnings(string *, const char *)
 
3685
com_nowarnings(string *buffer __attribute__((unused)),
 
3686
               const char *line __attribute__((unused)))
3833
3687
{
3834
3688
  show_warnings = 0;
3835
3689
  put_info("Show warnings disabled.",INFO_INFO, 0, 0);
3871
3725
        ptr++;
3872
3726
  }
3873
3727
  if (!*ptr)
3874
 
    return NULL;
 
3728
    return NullS;
3875
3729
  while (my_isspace(charset_info, *ptr))
3876
3730
    ptr++;
3877
3731
  if (*ptr == '\'' || *ptr == '\"' || *ptr == '`')
3885
3739
    if (*ptr == '\\' && ptr[1]) // escaped character
3886
3740
    {
3887
3741
      // Remove the backslash
3888
 
      strcpy(ptr, ptr+1);
 
3742
      stpcpy(ptr, ptr+1);
3889
3743
    }
3890
3744
    else if ((!quoted && *ptr == ' ') || (quoted && *ptr == qtype))
3891
3745
    {
3894
3748
    }
3895
3749
  }
3896
3750
  valid_arg= ptr != start;
3897
 
  return valid_arg ? start : NULL;
 
3751
  return valid_arg ? start : NullS;
3898
3752
}
3899
3753
 
3900
3754
 
3901
3755
static int
3902
3756
sql_connect(char *host,char *database,char *user,char *password,
3903
 
                 uint32_t silent)
 
3757
                 uint silent)
3904
3758
{
3905
 
  drizzle_return_t ret;
3906
 
 
3907
3759
  if (connected)
3908
3760
  {
3909
3761
    connected= 0;
3910
 
    drizzle_con_free(&con);
3911
 
    drizzle_free(&drizzle);
 
3762
    drizzle_close(&drizzle);
3912
3763
  }
3913
3764
  drizzle_create(&drizzle);
3914
 
  if (drizzle_con_add_tcp(&drizzle, &con, host, opt_drizzle_port, user,
3915
 
                          password, database, DRIZZLE_CON_NONE) == NULL)
3916
 
  {
3917
 
    (void) put_error(&con, NULL);
3918
 
    (void) fflush(stdout);
3919
 
    return 1;
3920
 
  }
3921
 
 
3922
 
/* XXX add this back in
3923
3765
  if (opt_connect_timeout)
3924
3766
  {
3925
 
    uint32_t timeout=opt_connect_timeout;
3926
 
    drizzleclient_options(&drizzle,DRIZZLE_OPT_CONNECT_TIMEOUT,
 
3767
    uint timeout=opt_connect_timeout;
 
3768
    drizzle_options(&drizzle,DRIZZLE_OPT_CONNECT_TIMEOUT,
3927
3769
                  (char*) &timeout);
3928
3770
  }
3929
 
*/
3930
 
 
3931
 
/* XXX Do we need this?
 
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);
3932
3778
  if (safe_updates)
3933
3779
  {
3934
3780
    char init_command[100];
3935
3781
    sprintf(init_command,
3936
3782
            "SET SQL_SAFE_UPDATES=1,SQL_SELECT_LIMIT=%"PRIu32
3937
 
            ",MAX_JOIN_SIZE=%"PRIu32,
 
3783
            ",SQL_MAX_JOIN_SIZE=%"PRIu32,
3938
3784
            select_limit, max_join_size);
3939
 
    drizzleclient_options(&drizzle, DRIZZLE_INIT_COMMAND, init_command);
 
3785
    drizzle_options(&drizzle, DRIZZLE_INIT_COMMAND, init_command);
3940
3786
  }
3941
 
*/
3942
 
  if ((ret= drizzle_con_connect(&con)) != DRIZZLE_RETURN_OK)
 
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))
3943
3792
  {
3944
 
    if (!silent || (ret != DRIZZLE_RETURN_GETADDRINFO &&
3945
 
                    ret != DRIZZLE_RETURN_COULD_NOT_CONNECT))
 
3793
    if (!silent ||
 
3794
        (drizzle_errno(&drizzle) != CR_CONN_HOST_ERROR &&
 
3795
         drizzle_errno(&drizzle) != CR_CONNECTION_ERROR))
3946
3796
    {
3947
 
      (void) put_error(&con, NULL);
 
3797
      (void) put_error(&drizzle);
3948
3798
      (void) fflush(stdout);
3949
3799
      return ignore_errors ? -1 : 1;    // Abort
3950
3800
    }
3951
3801
    return -1;          // Retryable
3952
3802
  }
3953
3803
  connected=1;
3954
 
 
 
3804
  drizzle.reconnect= debug_info_flag; // We want to know if this happens
3955
3805
  build_completion_hash(opt_rehash, 1);
3956
3806
  return 0;
3957
3807
}
3958
3808
 
3959
3809
 
3960
3810
static int
3961
 
com_status(string *, const char *)
 
3811
com_status(string *buffer __attribute__((unused)),
 
3812
           const char *line __attribute__((unused)))
3962
3813
{
3963
 
/*
3964
3814
  char buff[40];
3965
3815
  uint64_t id;
3966
 
*/
3967
 
  drizzle_result_st result;
3968
 
  drizzle_return_t ret;
 
3816
  DRIZZLE_RES *result;
3969
3817
 
3970
3818
  tee_puts("--------------", stdout);
3971
3819
  usage(1);          /* Print version */
3972
3820
  if (connected)
3973
3821
  {
3974
 
    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));
3975
3823
    /*
3976
3824
      Don't remove "limit 1",
3977
3825
      it is protection againts SQL_SELECT_LIMIT=0
3978
3826
    */
3979
 
    if (drizzle_query_str(&con,&result,"select DATABASE(), USER() limit 1",
3980
 
                          &ret) != NULL && ret == DRIZZLE_RETURN_OK &&
3981
 
        drizzle_result_buffer(&result) == DRIZZLE_RETURN_OK)
 
3827
    if (!drizzle_query(&drizzle,"select DATABASE(), USER() limit 1") &&
 
3828
        (result=drizzle_use_result(&drizzle)))
3982
3829
    {
3983
 
      drizzle_row_t cur=drizzle_row_next(&result);
 
3830
      DRIZZLE_ROW cur=drizzle_fetch_row(result);
3984
3831
      if (cur)
3985
3832
      {
3986
3833
        tee_fprintf(stdout, "Current database:\t%s\n", cur[0] ? cur[0] : "");
3987
3834
        tee_fprintf(stdout, "Current user:\t\t%s\n", cur[1]);
3988
3835
      }
3989
 
      drizzle_result_free(&result);
 
3836
      drizzle_free_result(result);
3990
3837
    }
3991
 
    else if (ret == DRIZZLE_RETURN_ERROR_CODE)
3992
 
      drizzle_result_free(&result);
3993
3838
    tee_puts("SSL:\t\t\tNot in use", stdout);
3994
3839
  }
3995
3840
  else
4008
3853
  tee_fprintf(stdout, "Current pager:\t\t%s\n", pager);
4009
3854
  tee_fprintf(stdout, "Using outfile:\t\t'%s'\n", opt_outfile ? outfile : "");
4010
3855
  tee_fprintf(stdout, "Using delimiter:\t%s\n", delimiter);
4011
 
  tee_fprintf(stdout, "Server version:\t\t%s\n", server_version_string(&con));
4012
 
  tee_fprintf(stdout, "Protocol version:\t%d\n", drizzle_con_protocol_version(&con));
4013
 
  tee_fprintf(stdout, "Connection:\t\t%s\n", drizzle_con_host(&con));
4014
 
/* XXX need to save this from result
4015
 
  if ((id= drizzleclient_insert_id(&drizzle)))
 
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)))
4016
3860
    tee_fprintf(stdout, "Insert id:\t\t%s\n", llstr(id, buff));
4017
 
*/
4018
 
 
4019
 
  if (strcmp(drizzle_con_uds(&con), ""))
4020
 
    tee_fprintf(stdout, "UNIX socket:\t\t%s\n", drizzle_con_uds(&con));
4021
 
  else
4022
 
    tee_fprintf(stdout, "TCP port:\t\t%d\n", drizzle_con_port(&con));
 
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");
4023
3889
 
4024
3890
  if (safe_updates)
4025
3891
  {
4038
3904
}
4039
3905
 
4040
3906
static const char *
4041
 
server_version_string(drizzle_con_st *local_con)
 
3907
server_version_string(DRIZZLE *con)
4042
3908
{
4043
 
  static string buf("");
4044
 
  static bool server_version_string_reserved= false;
 
3909
  static char buf[MAX_SERVER_VERSION_LENGTH] = "";
4045
3910
 
4046
 
  if (!server_version_string_reserved)
4047
 
  {
4048
 
    buf.reserve(MAX_SERVER_VERSION_LENGTH);
4049
 
    server_version_string_reserved= true;
4050
 
  }
4051
3911
  /* Only one thread calls this, so no synchronization is needed */
4052
3912
  if (buf[0] == '\0')
4053
3913
  {
4054
 
    drizzle_result_st result;
4055
 
    drizzle_return_t ret;
 
3914
    char *bufp = buf;
 
3915
    DRIZZLE_RES *result;
4056
3916
 
4057
 
    buf.append(drizzle_con_server_version(local_con));
 
3917
    bufp= stpncpy(buf, drizzle_get_server_info(con), sizeof buf);
4058
3918
 
4059
3919
    /* "limit 1" is protection against SQL_SELECT_LIMIT=0 */
4060
 
    (void)drizzle_query_str(local_con, &result,
4061
 
                            "select @@version_comment limit 1", &ret);
4062
 
    if (ret == DRIZZLE_RETURN_OK &&
4063
 
        drizzle_result_buffer(&result) == DRIZZLE_RETURN_OK)
 
3920
    if (!drizzle_query(con, "select @@version_comment limit 1") &&
 
3921
        (result = drizzle_use_result(con)))
4064
3922
    {
4065
 
      drizzle_row_t cur = drizzle_row_next(&result);
 
3923
      DRIZZLE_ROW cur = drizzle_fetch_row(result);
4066
3924
      if (cur && cur[0])
4067
3925
      {
4068
 
        buf.append(" ");
4069
 
        buf.append(cur[0]);
 
3926
        bufp = strxnmov(bufp, sizeof buf - (bufp - buf), " ", cur[0], NullS);
4070
3927
      }
4071
 
      drizzle_result_free(&result);
 
3928
      drizzle_free_result(result);
4072
3929
    }
4073
 
    else if (ret == DRIZZLE_RETURN_ERROR_CODE)
4074
 
      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';
4075
3934
  }
4076
3935
 
4077
 
  return buf.c_str();
 
3936
  return buf;
4078
3937
}
4079
3938
 
4080
3939
static int
4081
 
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)
4082
3941
{
4083
3942
  FILE *file= (info_type == INFO_ERROR ? stderr : stdout);
4084
3943
  static int inited=0;
4150
4009
 
4151
4010
 
4152
4011
static int
4153
 
put_error(drizzle_con_st *local_con, drizzle_result_st *res)
 
4012
put_error(DRIZZLE *con)
4154
4013
{
4155
 
  const char *error;
4156
 
 
4157
 
  if (res != NULL)
4158
 
  {
4159
 
    error= drizzle_result_error(res);
4160
 
    if (!strcmp(error, ""))
4161
 
      error= drizzle_con_error(local_con);
4162
 
  }
4163
 
  else
4164
 
    error= drizzle_con_error(local_con);
4165
 
 
4166
 
  return put_info(error, INFO_ERROR,
4167
 
                  res == NULL ? drizzle_con_error_code(local_con) :
4168
 
                                drizzle_result_error_code(res),
4169
 
                  res == NULL ? drizzle_con_sqlstate(local_con) :
4170
 
                                drizzle_result_sqlstate(res));
 
4014
  return put_info(drizzle_error(con), INFO_ERROR, drizzle_errno(con),
 
4015
                  drizzle_sqlstate(con));
4171
4016
}
4172
4017
 
4173
4018
 
4177
4022
  const char *end= start + (buffer->length());
4178
4023
  while (start < end && !my_isgraph(charset_info,end[-1]))
4179
4024
    end--;
4180
 
  uint32_t pos_to_truncate= (end-start);
 
4025
  uint pos_to_truncate= (end-start);
4181
4026
  if (buffer->length() > pos_to_truncate)
4182
4027
    buffer->erase(pos_to_truncate);
4183
4028
}
4248
4093
static void nice_time(double sec,char *buff,bool part_second)
4249
4094
{
4250
4095
  uint32_t tmp;
4251
 
  ostringstream tmp_buff_str;
4252
 
 
4253
4096
  if (sec >= 3600.0*24)
4254
4097
  {
4255
4098
    tmp=(uint32_t) floor(sec/(3600.0*24));
4256
 
    sec-= 3600.0*24*tmp;
4257
 
    tmp_buff_str << tmp;
4258
 
 
4259
 
    if (tmp > 1)
4260
 
      tmp_buff_str << " days ";
4261
 
    else
4262
 
      tmp_buff_str << " day ";
4263
 
 
 
4099
    sec-=3600.0*24*tmp;
 
4100
    buff=int10_to_str((long) tmp, buff, 10);
 
4101
    buff=stpcpy(buff,tmp > 1 ? " days " : " day ");
4264
4102
  }
4265
4103
  if (sec >= 3600.0)
4266
4104
  {
4267
4105
    tmp=(uint32_t) floor(sec/3600.0);
4268
4106
    sec-=3600.0*tmp;
4269
 
    tmp_buff_str << tmp;
4270
 
 
4271
 
    if (tmp > 1)
4272
 
      tmp_buff_str << " hours ";
4273
 
    else
4274
 
      tmp_buff_str << " hour ";
 
4107
    buff=int10_to_str((long) tmp, buff, 10);
 
4108
    buff=stpcpy(buff,tmp > 1 ? " hours " : " hour ");
4275
4109
  }
4276
4110
  if (sec >= 60.0)
4277
4111
  {
4278
4112
    tmp=(uint32_t) floor(sec/60.0);
4279
4113
    sec-=60.0*tmp;
4280
 
    tmp_buff_str << tmp << " min ";
 
4114
    buff=int10_to_str((long) tmp, buff, 10);
 
4115
    buff=stpcpy(buff," min ");
4281
4116
  }
4282
4117
  if (part_second)
4283
 
    tmp_buff_str.precision(2);
 
4118
    sprintf(buff,"%.2f sec",sec);
4284
4119
  else
4285
 
    tmp_buff_str.precision(0);
4286
 
  tmp_buff_str << sec << " sec";
4287
 
  strcpy(buff, tmp_buff_str.str().c_str());
 
4120
    sprintf(buff,"%d sec",(int) sec);
4288
4121
}
4289
4122
 
4290
4123
 
4300
4133
  buff[0]=' ';
4301
4134
  buff[1]='(';
4302
4135
  end_timer(start_time,buff+2);
4303
 
  strcpy(strchr(buff, '\0'),")");
 
4136
  stpcpy(strend(buff),")");
4304
4137
}
4305
4138
 
4306
4139
static const char * construct_prompt()
4324
4157
    {
4325
4158
      int getHour;
4326
4159
      int getYear;
4327
 
      /* Room for Dow MMM DD HH:MM:SS YYYY */ 
4328
 
      char dateTime[32];
 
4160
      char* dateTime= NULL;
4329
4161
      switch (*++c) {
4330
4162
      case '\0':
4331
4163
        // stop it from going beyond if ends with %
4336
4168
        break;
4337
4169
      case 'v':
4338
4170
        if (connected)
4339
 
          processed_prompt->append(drizzle_con_server_version(&con));
 
4171
          processed_prompt->append(drizzle_get_server_info(&drizzle));
4340
4172
        else
4341
4173
          processed_prompt->append("not_connected");
4342
4174
        break;
4346
4178
      case 'h':
4347
4179
      {
4348
4180
        const char *prompt;
4349
 
        prompt= connected ? drizzle_con_host(&con) : "not_connected";
 
4181
        prompt= connected ? drizzle_get_host_info(&drizzle) : "not_connected";
4350
4182
        if (strstr(prompt, "Localhost"))
4351
4183
          processed_prompt->append("localhost");
4352
4184
        else
4365
4197
          break;
4366
4198
        }
4367
4199
 
4368
 
        if (strcmp(drizzle_con_uds(&con), ""))
 
4200
        const char *host_info = drizzle_get_host_info(&drizzle);
 
4201
        if (strstr(host_info, "memory"))
4369
4202
        {
4370
 
          const char *pos=strrchr(drizzle_con_uds(&con),'/');
4371
 
          processed_prompt->append(pos ? pos+1 : drizzle_con_uds(&con));
 
4203
          processed_prompt->append(drizzle.host);
4372
4204
        }
 
4205
        else if (strstr(host_info,"TCP/IP") ||
 
4206
                 !drizzle.unix_socket)
 
4207
          add_int_to_prompt(drizzle.port);
4373
4208
        else
4374
 
          add_int_to_prompt(drizzle_con_port(&con));
 
4209
        {
 
4210
          char *pos=strrchr(drizzle.unix_socket,'/');
 
4211
          processed_prompt->append(pos ? pos+1 : drizzle.unix_socket);
 
4212
        }
4375
4213
      }
4376
4214
      break;
4377
4215
      case 'U':
4388
4226
        break;
4389
4227
      case PROMPT_CHAR:
4390
4228
        {
4391
 
          processed_prompt->append(PROMPT_CHAR, 1);
 
4229
          char c= PROMPT_CHAR;
 
4230
          processed_prompt->append(&c, 1);
4392
4231
        }
4393
4232
        break;
4394
4233
      case 'n':
4395
4234
        {
4396
 
          processed_prompt->append('\n', 1);
 
4235
          char c= '\n';
 
4236
          processed_prompt->append(&c, 1);
4397
4237
        }
4398
4238
        break;
4399
4239
      case ' ':
4400
4240
      case '_':
4401
4241
        {
4402
 
          processed_prompt->append(' ', 1);
 
4242
          char c= ' ';
 
4243
          processed_prompt->append(&c, 1);
4403
4244
        }
4404
4245
        break;
4405
4246
      case 'R':
4430
4271
        add_int_to_prompt(t->tm_year+1900);
4431
4272
        break;
4432
4273
      case 'D':
4433
 
        strftime(dateTime, 32, "%a %b %d %H:%M:%S %Y", localtime(&lclock));
4434
 
        processed_prompt->append(dateTime);
 
4274
        dateTime = ctime(&lclock);
 
4275
        processed_prompt->append(strtok(dateTime,"\n"));
4435
4276
        break;
4436
4277
      case 's':
4437
4278
        if (t->tm_sec < 10)
4476
4317
 
4477
4318
static void add_int_to_prompt(int toadd)
4478
4319
{
4479
 
  ostringstream buffer;
4480
 
  buffer << toadd;
4481
 
  processed_prompt->append(buffer.str().c_str());
 
4320
  char buffer[16];
 
4321
  int10_to_str(toadd, buffer, 10);
 
4322
  processed_prompt->append(buffer);
4482
4323
}
4483
4324
 
4484
4325
static void init_username()
4485
4326
{
4486
 
/* XXX need this?
4487
 
  free(full_username);
4488
 
  free(part_username);
 
4327
  my_free(full_username,MYF(MY_ALLOW_ZERO_PTR));
 
4328
  my_free(part_username,MYF(MY_ALLOW_ZERO_PTR));
4489
4329
 
4490
 
  drizzle_result_st *result;
4491
 
  if (!drizzleclient_query(&drizzle,"select USER()") &&
4492
 
      (result=drizzleclient_use_result(&drizzle)))
 
4330
  DRIZZLE_RES *result;
 
4331
  if (!drizzle_query(&drizzle,"select USER()") &&
 
4332
      (result=drizzle_use_result(&drizzle)))
4493
4333
  {
4494
 
    drizzle_row_t cur=drizzleclient_fetch_row(result);
 
4334
    DRIZZLE_ROW cur=drizzle_fetch_row(result);
4495
4335
    full_username= strdup(cur[0]);
4496
4336
    part_username= strdup(strtok(cur[0],"@"));
4497
 
    (void) drizzleclient_fetch_row(result);        // Read eof
 
4337
    (void) drizzle_fetch_row(result);        // Read eof
4498
4338
  }
4499
 
*/
4500
4339
}
4501
4340
 
4502
 
static int com_prompt(string *, const char *line)
 
4341
static int com_prompt(string *buffer __attribute__((unused)),
 
4342
                      const char *line)
4503
4343
{
4504
 
  const char *ptr=strchr(line, ' ');
4505
 
  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)
4506
4349
    tee_fprintf(stdout, "Returning to default PROMPT of %s\n",
4507
4350
                default_prompt);
4508
 
  prompt_counter = 0;
4509
 
  char * tmpptr= strdup(ptr ? ptr+1 : default_prompt);
4510
 
  if (tmpptr == NULL)
4511
 
    tee_fprintf(stdout, "Memory allocation error. Not changing prompt\n");
4512
4351
  else
4513
 
  {
4514
 
    free(current_prompt);
4515
 
    current_prompt= tmpptr;
4516
4352
    tee_fprintf(stdout, "PROMPT set to '%s'\n", current_prompt);
4517
 
  }
4518
4353
  return 0;
4519
4354
}
4520
4355
 
4521
4356
/*
4522
4357
    strcont(str, set) if str contanies any character in the string set.
4523
 
    The result is the position of the first found character in str, or NULL
 
4358
    The result is the position of the first found character in str, or NullS
4524
4359
    if there isn't anything found.
4525
4360
*/
4526
4361