~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to client/drizzle.c

  • Committer: Jim Winstead
  • Date: 2008-07-19 02:56:45 UTC
  • mto: (202.1.8 codestyle)
  • mto: This revision was merged to the branch mainline in revision 207.
  • Revision ID: jimw@mysql.com-20080719025645-w2pwytebgzusjzjb
Various fixes to enable compilation on Mac OS X, and remove the glib dependency.
Temporarily disables tab-completion in the drizzle client until an appropriate
autoconf check can be added/enabled.

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19
19
 */
20
20
 
21
 
/* drizzle command tool
 
21
/* mysql command tool
22
22
 * Commands compatible with mSQL by David J. Hughes
23
23
 *
24
24
 * Written by:
33
33
 *
34
34
 **/
35
35
 
36
 
#include "config.h"
37
 
 
38
 
#include <string>
39
 
 
40
36
#include "client_priv.h"
41
 
#include <mystrings/m_ctype.h>
 
37
#include <my_sys.h>
 
38
#include <m_ctype.h>
42
39
#include <stdarg.h>
 
40
#include <my_dir.h>
43
41
#ifndef __GNU_LIBRARY__
44
 
#define __GNU_LIBRARY__          // Skip warnings in getopt.h
 
42
#define __GNU_LIBRARY__               // Skip warnings in getopt.h
45
43
#endif
46
44
#include <readline/history.h>
47
45
#include "my_readline.h"
48
46
#include <signal.h>
49
 
#include <sys/ioctl.h>
50
 
 
51
 
 
52
 
#if defined(HAVE_LOCALE_H)
 
47
#include <violite.h>
 
48
 
 
49
#if defined(USE_LIBEDIT_INTERFACE) && defined(HAVE_LOCALE_H)
53
50
#include <locale.h>
54
51
#endif
55
52
 
56
 
#include <libdrizzle/gettext.h>
57
 
 
58
53
const char *VER= "14.14";
59
54
 
60
55
/* Don't try to make a nice table if the data is too big */
61
 
#define MAX_COLUMN_LENGTH       (uint32_t)1024
 
56
#define MAX_COLUMN_LENGTH            1024
62
57
 
63
58
/* Buffer to hold 'version' and 'version_comment' */
64
59
#define MAX_SERVER_VERSION_LENGTH     128
65
60
 
66
 
/* Array of options to pass to libdrizzled */
 
61
/* Array of options to pass to libemysqld */
67
62
#define MAX_SERVER_ARGS               64
68
63
 
69
 
void* sql_alloc(unsigned size);       // Don't use drizzled alloc for these
 
64
void* sql_alloc(unsigned size);      // Don't use mysqld alloc for these
70
65
void sql_element_free(void *ptr);
71
66
 
72
 
 
73
67
#if defined(HAVE_CURSES_H) && defined(HAVE_TERM_H)
74
68
#include <curses.h>
75
 
#ifdef __sun
76
 
#undef clear
77
 
#undef erase
78
 
#endif
79
69
#include <term.h>
80
70
#else
81
71
#if defined(HAVE_TERMIOS_H)
84
74
#elif defined(HAVE_TERMBITS_H)
85
75
#include <termbits.h>
86
76
#elif defined(HAVE_ASM_TERMBITS_H) && (!defined __GLIBC__ || !(__GLIBC__ > 2 || __GLIBC__ == 2 && __GLIBC_MINOR__ > 0))
87
 
#include <asm/termbits.h>    // Standard linux
 
77
#include <asm/termbits.h>               // Standard linux
88
78
#endif
 
79
#undef VOID
89
80
#if defined(HAVE_TERMCAP_H)
90
81
#include <termcap.h>
91
82
#else
92
83
#ifdef HAVE_CURSES_H
93
84
#include <curses.h>
94
85
#endif
95
 
#undef SYSV        // hack to avoid syntax error
 
86
#undef SYSV                             // hack to avoid syntax error
96
87
#ifdef HAVE_TERM_H
97
88
#include <term.h>
98
89
#endif
101
92
 
102
93
#undef bcmp                             // Fix problem with new readline
103
94
 
104
 
#ifdef HAVE_READLINE_HISTORY_H
105
 
#include <readline/history.h>
106
 
#endif
107
95
#include <readline/readline.h>
108
96
 
109
 
/**
110
 
 Make the old readline interface look like the new one.
111
 
*/
112
 
#ifndef USE_NEW_READLINE_INTERFACE
113
 
typedef CPPFunction rl_completion_func_t;
114
 
typedef Function rl_compentry_func_t;
115
 
#define rl_completion_matches(str, func) \
116
 
  completion_matches((char *)str, (CPFunction *)func)
117
 
#endif
118
 
 
119
97
 
120
98
#if !defined(HAVE_VIDATTR)
121
99
#undef vidattr
122
 
#define vidattr(A) {}      // Can't get this to work
 
100
#define vidattr(A) {}                   // Can't get this to work
123
101
#endif
124
102
 
125
103
#ifdef FN_NO_CASE_SENCE
130
108
 
131
109
#include "completion_hash.h"
132
110
 
133
 
using namespace std;
134
 
 
135
111
#define PROMPT_CHAR '\\'
136
112
#define DEFAULT_DELIMITER ";"
137
113
 
151
127
enum enum_info_type { INFO_INFO,INFO_ERROR,INFO_RESULT};
152
128
typedef enum enum_info_type INFO_TYPE;
153
129
 
154
 
static DRIZZLE drizzle;      /* The connection */
155
 
static bool ignore_errors=0,quick=0,
 
130
static MYSQL mysql;                     /* The connection */
 
131
static bool ignore_errors=0,wait_flag=0,quick=0,
156
132
  connected=0,opt_raw_data=0,unbuffered=0,output_tables=0,
157
133
  opt_rehash=1,skip_updates=0,safe_updates=0,one_database=0,
158
134
  opt_compress=0, using_opt_local_infile=0,
159
 
  vertical=0, line_numbers=1, column_names=1,
160
 
  opt_nopager=1, opt_outfile=0, named_cmds= 0,
 
135
  vertical=0, line_numbers=1, column_names=1,opt_html=0,
 
136
  opt_xml=0,opt_nopager=1, opt_outfile=0, named_cmds= 0,
161
137
  tty_password= 0, opt_nobeep=0, opt_reconnect=1,
162
138
  default_charset_used= 0, opt_secure_auth= 0,
163
139
  default_pager_set= 0, opt_sigint_ignore= 0,
166
142
static bool debug_info_flag, debug_check_flag;
167
143
static bool column_types_flag;
168
144
static bool preserve_comments= 0;
169
 
static uint32_t opt_max_allowed_packet, opt_net_buffer_length;
170
 
static int verbose=0,opt_silent=0,opt_drizzle_port=0, opt_local_infile=0;
 
145
static ulong opt_max_allowed_packet, opt_net_buffer_length;
 
146
static int verbose=0,opt_silent=0,opt_mysql_port=0, opt_local_infile=0;
171
147
static uint my_end_arg;
172
 
static char * opt_drizzle_unix_port=0;
 
148
static char * opt_mysql_unix_port=0;
173
149
static int connect_flag=CLIENT_INTERACTIVE;
174
150
static char *current_host,*current_db,*current_user=0,*opt_password=0,
175
 
  *delimiter_str= 0,* current_prompt= 0;
 
151
  *delimiter_str= 0,*current_prompt=0,
 
152
  *default_charset= (char*) MYSQL_DEFAULT_CHARSET_NAME;
176
153
static char *histfile;
177
154
static char *histfile_tmp;
178
 
static string *glob_buffer;
179
 
static string *processed_prompt= NULL;
 
155
static DYNAMIC_STRING *glob_buffer;
 
156
static DYNAMIC_STRING *processed_prompt= NULL;
180
157
static char *default_prompt= NULL;
181
158
static char *full_username=0,*part_username=0;
 
159
static int wait_time = 5;
182
160
static STATUS status;
183
161
static uint32_t select_limit;
184
162
static uint32_t max_join_size;
185
 
static uint32_t opt_connect_timeout= 0;
186
 
static char drizzle_charsets_dir[FN_REFLEN+1];
187
 
// TODO: Need to i18n these
 
163
static ulong opt_connect_timeout= 0;
 
164
static char mysql_charsets_dir[FN_REFLEN+1];
 
165
static const char *xmlmeta[] = {
 
166
  "&", "&amp;",
 
167
  "<", "&lt;",
 
168
  ">", "&gt;",
 
169
  "\"", "&quot;",
 
170
  0, 0
 
171
};
188
172
static const char *day_names[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
189
173
static const char *month_names[]={"Jan","Feb","Mar","Apr","May","Jun","Jul",
190
174
                                  "Aug","Sep","Oct","Nov","Dec"};
197
181
static uint delimiter_length= 1;
198
182
unsigned short terminal_width= 80;
199
183
 
200
 
static const CHARSET_INFO *charset_info= &my_charset_utf8_general_ci;
 
184
static uint opt_protocol= MYSQL_PROTOCOL_TCP;
 
185
static CHARSET_INFO *charset_info= &my_charset_latin1;
201
186
 
202
 
int drizzle_real_query_for_lazy(const char *buf, int length);
203
 
int drizzle_store_result_for_lazy(DRIZZLE_RES **result);
 
187
const char *default_dbug_option="d:t:o,/tmp/mysql.trace";
 
188
int mysql_real_query_for_lazy(const char *buf, int length);
 
189
int mysql_store_result_for_lazy(MYSQL_RES **result);
204
190
 
205
191
 
206
192
void tee_fprintf(FILE *file, const char *fmt, ...);
212
198
static int get_options(int argc,char **argv);
213
199
bool get_one_option(int optid, const struct my_option *opt,
214
200
                    char *argument);
215
 
static int com_quit(string *str,const char*),
216
 
  com_go(string *str,const char*), com_ego(string *str,const char*),
217
 
  com_print(string *str,const char*),
218
 
  com_help(string *str,const char*), com_clear(string *str,const char*),
219
 
  com_connect(string *str,const char*), com_status(string *str,const char*),
220
 
  com_use(string *str,const char*), com_source(string *str, const char*),
221
 
  com_rehash(string *str, const char*), com_tee(string *str, const char*),
222
 
  com_notee(string *str, const char*),
223
 
  com_prompt(string *str, const char*), com_delimiter(string *str, const char*),
224
 
  com_warnings(string *str, const char*), com_nowarnings(string *str, const char*),
225
 
  com_nopager(string *str, const char*), com_pager(string *str, const char*);
 
201
static int com_quit(DYNAMIC_STRING *str,char*),
 
202
  com_go(DYNAMIC_STRING *str,char*), com_ego(DYNAMIC_STRING *str,char*),
 
203
  com_print(DYNAMIC_STRING *str,char*),
 
204
  com_help(DYNAMIC_STRING *str,char*), com_clear(DYNAMIC_STRING *str,char*),
 
205
  com_connect(DYNAMIC_STRING *str,char*), com_status(DYNAMIC_STRING *str,char*),
 
206
  com_use(DYNAMIC_STRING *str,char*), com_source(DYNAMIC_STRING *str, char*),
 
207
  com_rehash(DYNAMIC_STRING *str, char*), com_tee(DYNAMIC_STRING *str, char*),
 
208
  com_notee(DYNAMIC_STRING *str, char*), com_charset(DYNAMIC_STRING *str,char*),
 
209
  com_prompt(DYNAMIC_STRING *str, char*), com_delimiter(DYNAMIC_STRING *str, char*),
 
210
  com_warnings(DYNAMIC_STRING *str, char*), com_nowarnings(DYNAMIC_STRING *str, char*),
 
211
  com_nopager(DYNAMIC_STRING *str, char*), com_pager(DYNAMIC_STRING *str, char*);
226
212
 
227
213
static int read_and_execute(bool interactive);
228
214
static int sql_connect(char *host,char *database,char *user,char *password,
229
215
                       uint silent);
230
 
static const char *server_version_string(DRIZZLE *drizzle);
 
216
static const char *server_version_string(MYSQL *mysql);
231
217
static int put_info(const char *str,INFO_TYPE info,uint error,
232
218
                    const char *sql_state);
233
 
static int put_error(DRIZZLE *drizzle);
234
 
static void safe_put_field(const char *pos,uint32_t length);
 
219
static int put_error(MYSQL *mysql);
 
220
static void safe_put_field(const char *pos,ulong length);
 
221
static void xmlencode_print(const char *src, uint length);
235
222
static void init_pager(void);
236
223
static void end_pager(void);
237
224
static void init_tee(const char *);
240
227
static char *get_arg(char *line, bool get_next_arg);
241
228
static void init_username(void);
242
229
static void add_int_to_prompt(int toadd);
243
 
static int get_result_width(DRIZZLE_RES *res);
244
 
static int get_field_disp_length(DRIZZLE_FIELD * field);
245
 
static const char * strcont(register const char *str, register const char *set);
 
230
static int get_result_width(MYSQL_RES *res);
 
231
static int get_field_disp_length(MYSQL_FIELD * field);
246
232
 
247
233
/* A structure which contains information on the commands this program
248
234
   can understand. */
249
235
typedef struct {
250
 
  const char *name;        /* User printable name of the function. */
251
 
  char cmd_char;        /* msql command character */
252
 
  int (*func)(string *str,const char *); /* Function to call to do the job. */
253
 
  bool takes_params;        /* Max parameters for command */
254
 
  const char *doc;        /* Documentation for this function.  */
 
236
  const char *name;             /* User printable name of the function. */
 
237
  char cmd_char;                /* msql command character */
 
238
  int (*func)(DYNAMIC_STRING *str,char *); /* Function to call to do the job. */
 
239
  bool takes_params;            /* Max parameters for command */
 
240
  const char *doc;              /* Documentation for this function.  */
255
241
} COMMANDS;
256
242
 
257
243
 
258
244
static COMMANDS commands[] = {
259
 
  { "?",      '?', com_help,   1, N_("Synonym for `help'.") },
260
 
  { "clear",  'c', com_clear,  0, N_("Clear command.")},
 
245
  { "?",      '?', com_help,   1, "Synonym for `help'." },
 
246
  { "clear",  'c', com_clear,  0, "Clear command."},
261
247
  { "connect",'r', com_connect,1,
262
 
    N_("Reconnect to the server. Optional arguments are db and host." }),
 
248
    "Reconnect to the server. Optional arguments are db and host." },
263
249
  { "delimiter", 'd', com_delimiter,    1,
264
 
    N_("Set statement delimiter. NOTE: Takes the rest of the line as new delimiter.") },
 
250
    "Set statement delimiter. NOTE: Takes the rest of the line as new delimiter." },
265
251
  { "ego",    'G', com_ego,    0,
266
 
    N_("Send command to drizzle server, display result vertically.")},
267
 
  { "exit",   'q', com_quit,   0, N_("Exit drizzle. Same as quit.")},
268
 
  { "go",     'g', com_go,     0, N_("Send command to drizzle server.") },
269
 
  { "help",   'h', com_help,   1, N_("Display this help.") },
270
 
  { "nopager",'n', com_nopager,0, N_("Disable pager, print to stdout.") },
271
 
  { "notee",  't', com_notee,  0, N_("Don't write into outfile.") },
 
252
    "Send command to mysql server, display result vertically."},
 
253
  { "exit",   'q', com_quit,   0, "Exit mysql. Same as quit."},
 
254
  { "go",     'g', com_go,     0, "Send command to mysql server." },
 
255
  { "help",   'h', com_help,   1, "Display this help." },
 
256
  { "nopager",'n', com_nopager,0, "Disable pager, print to stdout." },
 
257
  { "notee",  't', com_notee,  0, "Don't write into outfile." },
272
258
  { "pager",  'P', com_pager,  1,
273
 
    N_("Set PAGER [to_pager]. Print the query results via PAGER.") },
274
 
  { "print",  'p', com_print,  0, N_("Print current command.") },
275
 
  { "prompt", 'R', com_prompt, 1, N_("Change your drizzle prompt.")},
276
 
  { "quit",   'q', com_quit,   0, N_("Quit drizzle.") },
277
 
  { "rehash", '#', com_rehash, 0, N_("Rebuild completion hash.") },
 
259
    "Set PAGER [to_pager]. Print the query results via PAGER." },
 
260
  { "print",  'p', com_print,  0, "Print current command." },
 
261
  { "prompt", 'R', com_prompt, 1, "Change your mysql prompt."},
 
262
  { "quit",   'q', com_quit,   0, "Quit mysql." },
 
263
  { "rehash", '#', com_rehash, 0, "Rebuild completion hash." },
278
264
  { "source", '.', com_source, 1,
279
 
    N_("Execute an SQL script file. Takes a file name as an argument.")},
280
 
  { "status", 's', com_status, 0, N_("Get status information from the server.")},
 
265
    "Execute an SQL script file. Takes a file name as an argument."},
 
266
  { "status", 's', com_status, 0, "Get status information from the server."},
281
267
  { "tee",    'T', com_tee,    1,
282
 
    N_("Set outfile [to_outfile]. Append everything into given outfile.") },
 
268
    "Set outfile [to_outfile]. Append everything into given outfile." },
283
269
  { "use",    'u', com_use,    1,
284
 
    N_("Use another database. Takes database name as argument.") },
 
270
    "Use another database. Takes database name as argument." },
 
271
  { "charset",    'C', com_charset,    1,
 
272
    "Switch to another charset. Might be needed for processing binlog with multi-byte charsets." },
285
273
  { "warnings", 'W', com_warnings,  0,
286
 
    N_("Show warnings after every statement.") },
 
274
    "Show warnings after every statement." },
287
275
  { "nowarning", 'w', com_nowarnings, 0,
288
 
    N_("Don't show warnings after every statement.") },
 
276
    "Don't show warnings after every statement." },
289
277
  /* Get bash-like expansion for some commands */
290
278
  { "create table",     0, 0, 0, ""},
291
279
  { "create database",  0, 0, 0, ""},
610
598
  { "QUARTER", 0, 0, 0, ""},
611
599
  { "QUERY", 0, 0, 0, ""},
612
600
  { "QUICK", 0, 0, 0, ""},
 
601
  { "RAID0", 0, 0, 0, ""},
 
602
  { "RAID_CHUNKS", 0, 0, 0, ""},
 
603
  { "RAID_CHUNKSIZE", 0, 0, 0, ""},
 
604
  { "RAID_TYPE", 0, 0, 0, ""},
613
605
  { "READ", 0, 0, 0, ""},
614
606
  { "READS", 0, 0, 0, ""},
615
607
  { "REAL", 0, 0, 0, ""},
992
984
  { (char *)NULL,       0, 0, 0, ""}
993
985
};
994
986
 
995
 
static const char *load_default_groups[]= { "drizzle","client",0 };
 
987
static const char *load_default_groups[]= { "mysql","client",0 };
 
988
 
 
989
static int         embedded_server_arg_count= 0;
 
990
static char       *embedded_server_args[MAX_SERVER_ARGS];
 
991
static const char *embedded_server_groups[]=
 
992
{ "server", "mysql_SERVER", 0 };
996
993
 
997
994
int history_length;
998
995
static int not_in_history(const char *line);
999
 
static void initialize_readline (char *name);
1000
 
static void fix_history(string *final_command);
 
996
static void initialize_readline (const char *name);
 
997
static void fix_history(DYNAMIC_STRING *final_command);
1001
998
 
1002
 
static COMMANDS *find_command(const char *name,char cmd_name);
1003
 
static bool add_line(string *buffer,char *line,char *in_string,
 
999
static COMMANDS *find_command(char *name,char cmd_name);
 
1000
static bool add_line(DYNAMIC_STRING *buffer,char *line,char *in_string,
1004
1001
                     bool *ml_comment);
1005
 
static void remove_cntrl(string *buffer);
1006
 
static void print_table_data(DRIZZLE_RES *result);
1007
 
static void print_tab_data(DRIZZLE_RES *result);
1008
 
static void print_table_data_vertically(DRIZZLE_RES *result);
 
1002
static void remove_cntrl(DYNAMIC_STRING *buffer);
 
1003
static void print_table_data(MYSQL_RES *result);
 
1004
static void print_table_data_html(MYSQL_RES *result);
 
1005
static void print_table_data_xml(MYSQL_RES *result);
 
1006
static void print_tab_data(MYSQL_RES *result);
 
1007
static void print_table_data_vertically(MYSQL_RES *result);
1009
1008
static void print_warnings(void);
1010
 
static uint32_t start_timer(void);
1011
 
static void end_timer(uint32_t start_time,char *buff);
1012
 
static void drizzle_end_timer(uint32_t start_time,char *buff);
 
1009
static ulong start_timer(void);
 
1010
static void end_timer(ulong start_time,char *buff);
 
1011
static void mysql_end_timer(ulong start_time,char *buff);
1013
1012
static void nice_time(double sec,char *buff,bool part_second);
1014
 
extern RETSIGTYPE drizzle_end(int sig);
1015
 
extern RETSIGTYPE handle_sigint(int sig);
 
1013
extern sig_handler mysql_end(int sig);
 
1014
extern sig_handler handle_sigint(int sig);
1016
1015
#if defined(HAVE_TERMIOS_H) && defined(GWINSZ_IN_SYS_IOCTL)
1017
 
static RETSIGTYPE window_resize(int sig);
 
1016
static sig_handler window_resize(int sig);
1018
1017
#endif
1019
1018
 
1020
1019
int main(int argc,char *argv[])
1021
1020
{
1022
1021
  char buff[80];
1023
1022
 
1024
 
#if defined(ENABLE_NLS)
1025
 
# if defined(HAVE_LOCALE_H)
1026
 
  setlocale(LC_ALL, "");
1027
 
# endif
1028
 
  bindtextdomain("drizzle", LOCALEDIR);
1029
 
  textdomain("drizzle");
1030
 
#endif
1031
 
 
1032
1023
  MY_INIT(argv[0]);
1033
1024
  delimiter_str= delimiter;
1034
1025
  default_prompt= my_strdup(getenv("DRIZZLE_PS1") ?
1035
1026
                            getenv("DRIZZLE_PS1") :
1036
1027
                            "drizzle>> ", MYF(0));
1037
1028
  current_prompt= my_strdup(default_prompt, MYF(0));
1038
 
  processed_prompt= new string();
1039
 
  processed_prompt->reserve(32);
 
1029
 
 
1030
  processed_prompt= (DYNAMIC_STRING *)my_malloc(sizeof(DYNAMIC_STRING), MYF(0));
 
1031
  init_dynamic_string(processed_prompt, "", 32, 32);
1040
1032
 
1041
1033
  prompt_counter=0;
1042
1034
 
1043
 
  outfile[0]=0;      // no (default) outfile
1044
 
  my_stpcpy(pager, "stdout");  // the default, if --pager wasn't given
 
1035
  outfile[0]=0;                 // no (default) outfile
 
1036
  strmov(pager, "stdout");      // the default, if --pager wasn't given
1045
1037
  {
1046
1038
    char *tmp=getenv("PAGER");
1047
1039
    if (tmp && strlen(tmp))
1048
1040
    {
1049
1041
      default_pager_set= 1;
1050
 
      my_stpcpy(default_pager, tmp);
 
1042
      strmov(default_pager, tmp);
1051
1043
    }
1052
1044
  }
1053
1045
  if (!isatty(0) || !isatty(1))
1088
1080
    my_end(0);
1089
1081
    exit(1);
1090
1082
  }
 
1083
  if (mysql_server_init(embedded_server_arg_count, embedded_server_args,
 
1084
                        (char**) embedded_server_groups))
 
1085
  {
 
1086
    put_error(NULL);
 
1087
    free_defaults(defaults_argv);
 
1088
    my_end(0);
 
1089
    exit(1);
 
1090
  }
1091
1091
  completion_hash_init(&ht, 128);
1092
1092
  init_alloc_root(&hash_mem_root, 16384, 0);
1093
 
  memset(&drizzle, 0, sizeof(drizzle));
 
1093
  bzero((char*) &mysql, sizeof(mysql));
1094
1094
  if (sql_connect(current_host,current_db,current_user,opt_password,
1095
1095
                  opt_silent))
1096
1096
  {
1097
 
    quick= 1;          // Avoid history
 
1097
    quick= 1;                                   // Avoid history
1098
1098
    status.exit_status= 1;
1099
 
    drizzle_end(-1);
 
1099
    mysql_end(-1);
1100
1100
  }
1101
1101
  if (!status.batch)
1102
 
    ignore_errors=1;        // Don't abort monitor
 
1102
    ignore_errors=1;                            // Don't abort monitor
1103
1103
 
1104
1104
  if (opt_sigint_ignore)
1105
1105
    signal(SIGINT, SIG_IGN);
1106
1106
  else
1107
1107
    signal(SIGINT, handle_sigint);              // Catch SIGINT to clean up
1108
 
  signal(SIGQUIT, drizzle_end);      // Catch SIGQUIT to clean up
 
1108
  signal(SIGQUIT, mysql_end);                   // Catch SIGQUIT to clean up
1109
1109
 
1110
1110
#if defined(HAVE_TERMIOS_H) && defined(GWINSZ_IN_SYS_IOCTL)
1111
1111
  /* Readline will call this if it installs a handler */
1114
1114
  window_resize(0);
1115
1115
#endif
1116
1116
 
1117
 
  put_info(_("Welcome to the Drizzle client..  Commands end with ; or \\g."),
 
1117
  put_info("Welcome to the Drizzle client..  Commands end with ; or \\g.",
1118
1118
           INFO_INFO,0,0);
1119
1119
 
1120
 
  glob_buffer= new string();
1121
 
  glob_buffer->reserve(512);
1122
 
  
1123
 
  char * output_buff= (char *)malloc(512);
1124
 
  memset(output_buff, '\0', 512);
1125
 
 
1126
 
  sprintf(output_buff,
1127
 
          _("Your Drizzle connection id is %u\nServer version: %s\n"),
1128
 
          drizzle_thread_id(&drizzle),
1129
 
          server_version_string(&drizzle));
1130
 
  put_info(output_buff, INFO_INFO, 0, 0);
1131
 
 
1132
 
  initialize_readline(current_prompt);
1133
 
  if (!status.batch && !quick)
 
1120
  glob_buffer= (DYNAMIC_STRING *)my_malloc(sizeof(DYNAMIC_STRING), MYF(0));
 
1121
  init_dynamic_string(glob_buffer, "", 512, 512);
 
1122
 
 
1123
  /* this is a slight abuse of the DYNAMIC_STRING interface. deal. */
 
1124
  sprintf(glob_buffer->str,
 
1125
          "Your Drizzle connection id is %u\nServer version: %s\n",
 
1126
          mysql_thread_id(&mysql), server_version_string(&mysql));
 
1127
  put_info(glob_buffer->str, INFO_INFO, 0, 0);
 
1128
  dynstr_set(glob_buffer, NULL);
 
1129
 
 
1130
  initialize_readline(my_progname);
 
1131
  if (!status.batch && !quick && !opt_html && !opt_xml)
1134
1132
  {
1135
 
    /* read-history from file, default ~/.drizzle_history*/
1136
 
    if (getenv("DRIZZLE_HISTFILE"))
1137
 
      histfile= strdup(getenv("DRIZZLE_HISTFILE"));
 
1133
    /* read-history from file, default ~/.mysql_history*/
 
1134
    if (getenv("MYSQL_HISTFILE"))
 
1135
      histfile= strdup(getenv("MYSQL_HISTFILE"));
1138
1136
    else if (getenv("HOME"))
1139
1137
    {
1140
1138
      histfile=(char*) my_malloc((uint) strlen(getenv("HOME"))
1141
 
                                 + (uint) strlen("/.drizzle_history")+2,
 
1139
                                 + (uint) strlen("/.mysql_history")+2,
1142
1140
                                 MYF(MY_WME));
1143
1141
      if (histfile)
1144
 
        sprintf(histfile,"%s/.drizzle_history",getenv("HOME"));
 
1142
        sprintf(histfile,"%s/.mysql_history",getenv("HOME"));
1145
1143
      char link_name[FN_REFLEN];
1146
1144
      if (my_readlink(link_name, histfile, 0) == 0 &&
1147
1145
          strncmp(link_name, "/dev/null", 10) == 0)
1148
1146
      {
1149
 
        /* The .drizzle_history file is a symlink to /dev/null, don't use it */
1150
 
        free(histfile);
 
1147
        /* The .mysql_history file is a symlink to /dev/null, don't use it */
 
1148
        my_free(histfile, MYF(MY_ALLOW_ZERO_PTR));
1151
1149
        histfile= 0;
1152
1150
      }
1153
1151
    }
1154
1152
    if (histfile)
1155
1153
    {
1156
1154
      if (verbose)
1157
 
        tee_fprintf(stdout, _("Reading history-file %s\n"),histfile);
 
1155
        tee_fprintf(stdout, "Reading history-file %s\n",histfile);
1158
1156
      read_history(histfile);
1159
1157
      if (!(histfile_tmp= (char*) my_malloc((uint) strlen(histfile) + 5,
1160
1158
                                            MYF(MY_WME))))
1161
1159
      {
1162
 
        fprintf(stderr, _("Couldn't allocate memory for temp histfile!\n"));
 
1160
        fprintf(stderr, "Couldn't allocate memory for temp histfile!\n");
1163
1161
        exit(1);
1164
1162
      }
1165
1163
      sprintf(histfile_tmp, "%s.TMP", histfile);
1166
1164
    }
1167
1165
  }
1168
1166
  sprintf(buff, "%s",
1169
 
          _("Type 'help;' or '\\h' for help. Type '\\c' to clear the buffer.\n"));
 
1167
          "Type 'help;' or '\\h' for help. Type '\\c' to clear the buffer.\n");
1170
1168
 
1171
1169
  put_info(buff,INFO_INFO,0,0);
1172
1170
  status.exit_status= read_and_execute(!status.batch);
1173
1171
  if (opt_outfile)
1174
1172
    end_tee();
1175
 
  drizzle_end(0);
1176
 
 
1177
 
  return(0);        // Keep compiler happy
 
1173
  mysql_end(0);
 
1174
#ifndef _lint
 
1175
  return(0);                            // Keep compiler happy
 
1176
#endif
1178
1177
}
1179
1178
 
1180
 
RETSIGTYPE drizzle_end(int sig)
 
1179
sig_handler mysql_end(int sig)
1181
1180
{
1182
 
  drizzle_close(&drizzle);
1183
 
  if (!status.batch && !quick && histfile)
 
1181
  mysql_close(&mysql);
 
1182
  if (!status.batch && !quick && !opt_html && !opt_xml && histfile)
1184
1183
  {
1185
1184
    /* write-history */
1186
1185
    if (verbose)
1187
 
      tee_fprintf(stdout, _("Writing history-file %s\n"),histfile);
 
1186
      tee_fprintf(stdout, "Writing history-file %s\n",histfile);
1188
1187
    if (!write_history(histfile_tmp))
1189
1188
      my_rename(histfile_tmp, histfile, MYF(MY_WME));
1190
1189
  }
1193
1192
  free_root(&hash_mem_root,MYF(0));
1194
1193
 
1195
1194
  if (sig >= 0)
1196
 
    put_info(sig ? _("Aborted") : _("Bye"), INFO_RESULT,0,0);
 
1195
    put_info(sig ? "Aborted" : "Bye", INFO_RESULT,0,0);
1197
1196
  if (glob_buffer)
1198
 
    delete glob_buffer;
 
1197
    dynstr_free(glob_buffer);
 
1198
  my_free(glob_buffer, MYF(MY_ALLOW_ZERO_PTR));
1199
1199
  if (processed_prompt)
1200
 
    delete processed_prompt;
1201
 
  free(opt_password);
1202
 
  free(opt_drizzle_unix_port);
1203
 
  free(histfile);
1204
 
  free(histfile_tmp);
1205
 
  free(current_db);
1206
 
  free(current_host);
1207
 
  free(current_user);
1208
 
  free(full_username);
1209
 
  free(part_username);
1210
 
  free(default_prompt);
1211
 
  free(current_prompt);
 
1200
    dynstr_free(processed_prompt);
 
1201
  my_free(processed_prompt,MYF(MY_ALLOW_ZERO_PTR));
 
1202
  my_free(opt_password,MYF(MY_ALLOW_ZERO_PTR));
 
1203
  my_free(opt_mysql_unix_port,MYF(MY_ALLOW_ZERO_PTR));
 
1204
  my_free(histfile,MYF(MY_ALLOW_ZERO_PTR));
 
1205
  my_free(histfile_tmp,MYF(MY_ALLOW_ZERO_PTR));
 
1206
  my_free(current_db,MYF(MY_ALLOW_ZERO_PTR));
 
1207
  my_free(current_host,MYF(MY_ALLOW_ZERO_PTR));
 
1208
  my_free(current_user,MYF(MY_ALLOW_ZERO_PTR));
 
1209
  my_free(full_username,MYF(MY_ALLOW_ZERO_PTR));
 
1210
  my_free(part_username,MYF(MY_ALLOW_ZERO_PTR));
 
1211
  my_free(default_prompt,MYF(MY_ALLOW_ZERO_PTR));
 
1212
  my_free(current_prompt,MYF(MY_ALLOW_ZERO_PTR));
 
1213
  while (embedded_server_arg_count > 1)
 
1214
    my_free(embedded_server_args[--embedded_server_arg_count],MYF(0));
 
1215
  mysql_server_end();
1212
1216
  free_defaults(defaults_argv);
1213
1217
  my_end(my_end_arg);
1214
1218
  exit(status.exit_status);
1220
1224
  If query is in process, kill query
1221
1225
  no query in process, terminate like previous behavior
1222
1226
*/
1223
 
RETSIGTYPE handle_sigint(int sig)
 
1227
sig_handler handle_sigint(int sig)
1224
1228
{
1225
1229
  char kill_buffer[40];
1226
 
  DRIZZLE *kill_drizzle= NULL;
 
1230
  MYSQL *kill_mysql= NULL;
1227
1231
 
1228
1232
  /* terminate if no query being executed, or we already tried interrupting */
1229
1233
  if (!executing_query || interrupted_query) {
1230
1234
    goto err;
1231
1235
  }
1232
1236
 
1233
 
  kill_drizzle= drizzle_create(kill_drizzle);
1234
 
  if (!drizzle_connect(kill_drizzle,current_host, current_user, opt_password,
1235
 
                          "", opt_drizzle_port, opt_drizzle_unix_port,0))
 
1237
  kill_mysql= mysql_init(kill_mysql);
 
1238
  if (!mysql_real_connect(kill_mysql,current_host, current_user, opt_password,
 
1239
                          "", opt_mysql_port, opt_mysql_unix_port,0))
1236
1240
  {
1237
1241
    goto err;
1238
1242
  }
1239
1243
 
1240
1244
  /* kill_buffer is always big enough because max length of %lu is 15 */
1241
 
  sprintf(kill_buffer, "KILL /*!50000 QUERY */ %u", drizzle_thread_id(&drizzle));
1242
 
  drizzle_real_query(kill_drizzle, kill_buffer, strlen(kill_buffer));
1243
 
  drizzle_close(kill_drizzle);
1244
 
  tee_fprintf(stdout, _("Query aborted by Ctrl+C\n"));
 
1245
  sprintf(kill_buffer, "KILL /*!50000 QUERY */ %u", mysql_thread_id(&mysql));
 
1246
  mysql_real_query(kill_mysql, kill_buffer, strlen(kill_buffer));
 
1247
  mysql_close(kill_mysql);
 
1248
  tee_fprintf(stdout, "Query aborted by Ctrl+C\n");
1245
1249
 
1246
1250
  interrupted_query= 1;
1247
1251
 
1248
1252
  return;
1249
1253
 
1250
1254
err:
1251
 
  drizzle_end(sig);
 
1255
  mysql_end(sig);
1252
1256
}
1253
1257
 
1254
1258
 
1255
1259
#if defined(HAVE_TERMIOS_H) && defined(GWINSZ_IN_SYS_IOCTL)
1256
 
RETSIGTYPE window_resize(int sig __attribute__((unused)))
 
1260
sig_handler window_resize(int sig __attribute__((__unused__)))
1257
1261
{
1258
1262
  struct winsize window_size;
1259
1263
 
1264
1268
 
1265
1269
static struct my_option my_long_options[] =
1266
1270
{
1267
 
  {"help", '?', N_("Display this help and exit."), 0, 0, 0, GET_NO_ARG, NO_ARG, 0,
 
1271
  {"help", '?', "Display this help and exit.", 0, 0, 0, GET_NO_ARG, NO_ARG, 0,
1268
1272
   0, 0, 0, 0, 0},
1269
 
  {"help", 'I', N_("Synonym for -?"), 0, 0, 0, GET_NO_ARG, NO_ARG, 0,
 
1273
  {"help", 'I', "Synonym for -?", 0, 0, 0, GET_NO_ARG, NO_ARG, 0,
1270
1274
   0, 0, 0, 0, 0},
1271
1275
  {"auto-rehash", OPT_AUTO_REHASH,
1272
 
   N_("Enable automatic rehashing. One doesn't need to use 'rehash' to get table and field completion, but startup and reconnecting may take a longer time. Disable with --disable-auto-rehash."),
 
1276
   "Enable automatic rehashing. One doesn't need to use 'rehash' to get table and field completion, but startup and reconnecting may take a longer time. Disable with --disable-auto-rehash.",
1273
1277
   (char**) &opt_rehash, (char**) &opt_rehash, 0, GET_BOOL, NO_ARG, 1, 0, 0, 0,
1274
1278
   0, 0},
1275
1279
  {"no-auto-rehash", 'A',
1276
 
   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."),
 
1280
   "No automatic rehashing. One has to use 'rehash' to get table and field completion. This gives a quicker start of mysql and disables rehashing on reconnect. WARNING: options deprecated; use --disable-auto-rehash instead.",
1277
1281
   0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
1278
1282
  {"auto-vertical-output", OPT_AUTO_VERTICAL_OUTPUT,
1279
 
   N_("Automatically switch to vertical output mode if the result is wider than the terminal width."),
 
1283
   "Automatically switch to vertical output mode if the result is wider than the terminal width.",
1280
1284
   (char**) &auto_vertical_output, (char**) &auto_vertical_output, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
1281
1285
  {"batch", 'B',
1282
 
   N_("Don't use history file. Disable interactive behavior. (Enables --silent)"), 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
 
1286
   "Don't use history file. Disable interactive behavior. (Enables --silent)", 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
1283
1287
  {"character-sets-dir", OPT_CHARSETS_DIR,
1284
 
   N_("Directory where character sets are."), (char**) &charsets_dir,
 
1288
   "Directory where character sets are.", (char**) &charsets_dir,
1285
1289
   (char**) &charsets_dir, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
1286
 
  {"column-type-info", OPT_COLUMN_TYPES, N_("Display column type information."),
 
1290
  {"column-type-info", OPT_COLUMN_TYPES, "Display column type information.",
1287
1291
   (char**) &column_types_flag, (char**) &column_types_flag,
1288
1292
   0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
1289
 
  {"comments", 'c', N_("Preserve comments. Send comments to the server. The default is --skip-comments (discard comments), enable with --comments"),
 
1293
  {"comments", 'c', "Preserve comments. Send comments to the server."
 
1294
   " The default is --skip-comments (discard comments), enable with --comments",
1290
1295
   (char**) &preserve_comments, (char**) &preserve_comments,
1291
1296
   0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
1292
 
  {"compress", 'C', N_("Use compression in server/client protocol."),
 
1297
  {"compress", 'C', "Use compression in server/client protocol.",
1293
1298
   (char**) &opt_compress, (char**) &opt_compress, 0, GET_BOOL, NO_ARG, 0, 0, 0,
1294
1299
   0, 0, 0},
1295
 
  {"debug-check", OPT_DEBUG_CHECK, N_("Check memory and open file usage at exit ."),
 
1300
  {"debug-check", OPT_DEBUG_CHECK, "Check memory and open file usage at exit .",
1296
1301
   (char**) &debug_check_flag, (char**) &debug_check_flag, 0,
1297
1302
   GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
1298
 
  {"debug-info", 'T', N_("Print some debug info at exit."), (char**) &debug_info_flag,
 
1303
  {"debug-info", 'T', "Print some debug info at exit.", (char**) &debug_info_flag,
1299
1304
   (char**) &debug_info_flag, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
1300
 
  {"database", 'D', N_("Database to use."), (char**) &current_db,
 
1305
  {"database", 'D', "Database to use.", (char**) &current_db,
1301
1306
   (char**) &current_db, 0, GET_STR_ALLOC, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
1302
1307
  {"default-character-set", OPT_DEFAULT_CHARSET,
1303
 
   N_("(not used)"), 0,
1304
 
   0, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
1305
 
  {"delimiter", OPT_DELIMITER, N_("Delimiter to be used."), (char**) &delimiter_str,
 
1308
   "Set the default character set.", (char**) &default_charset,
 
1309
   (char**) &default_charset, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
1310
  {"delimiter", OPT_DELIMITER, "Delimiter to be used.", (char**) &delimiter_str,
1306
1311
   (char**) &delimiter_str, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
1307
 
  {"execute", 'e', N_("Execute command and quit. (Disables --force and history file)"), 0,
 
1312
  {"execute", 'e', "Execute command and quit. (Disables --force and history file)", 0,
1308
1313
   0, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
1309
 
  {"vertical", 'E', N_("Print the output of a query (rows) vertically."),
 
1314
  {"vertical", 'E', "Print the output of a query (rows) vertically.",
1310
1315
   (char**) &vertical, (char**) &vertical, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0,
1311
1316
   0},
1312
 
  {"force", 'f', N_("Continue even if we get an sql error."),
 
1317
  {"force", 'f', "Continue even if we get an sql error.",
1313
1318
   (char**) &ignore_errors, (char**) &ignore_errors, 0, GET_BOOL, NO_ARG, 0, 0,
1314
1319
   0, 0, 0, 0},
1315
1320
  {"named-commands", 'G',
1316
 
   N_("Enable named commands. Named commands mean this program's internal commands; see drizzle> help . When enabled, the named commands can be used from any line of the query, otherwise only from the first line, before an enter. Disable with --disable-named-commands. This option is disabled by default."),
 
1321
   "Enable named commands. Named commands mean this program's internal commands; see mysql> help . When enabled, the named commands can be used from any line of the query, otherwise only from the first line, before an enter. Disable with --disable-named-commands. This option is disabled by default.",
1317
1322
   (char**) &named_cmds, (char**) &named_cmds, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0,
1318
1323
   0, 0},
1319
1324
  {"no-named-commands", 'g',
1320
 
   N_("Named commands are disabled. Use \\* form only, or use named commands only in the beginning of a line ending with a semicolon (;) Since version 10.9 the client now starts with this option ENABLED by default! Disable with '-G'. Long format commands still work from the first line. WARNING: option deprecated; use --disable-named-commands instead."),
 
1325
   "Named commands are disabled. Use \\* form only, or use named commands only in the beginning of a line ending with a semicolon (;) Since version 10.9 the client now starts with this option ENABLED by default! Disable with '-G'. Long format commands still work from the first line. WARNING: option deprecated; use --disable-named-commands instead.",
1321
1326
   0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
1322
 
  {"ignore-spaces", 'i', N_("Ignore space after function names."), 0, 0, 0,
 
1327
  {"ignore-spaces", 'i', "Ignore space after function names.", 0, 0, 0,
1323
1328
   GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
1324
 
  {"local-infile", OPT_LOCAL_INFILE, N_("Enable/disable LOAD DATA LOCAL INFILE."),
 
1329
  {"local-infile", OPT_LOCAL_INFILE, "Enable/disable LOAD DATA LOCAL INFILE.",
1325
1330
   (char**) &opt_local_infile,
1326
1331
   (char**) &opt_local_infile, 0, GET_BOOL, OPT_ARG, 0, 0, 0, 0, 0, 0},
1327
 
  {"no-beep", 'b', N_("Turn off beep on error."), (char**) &opt_nobeep,
 
1332
  {"no-beep", 'b', "Turn off beep on error.", (char**) &opt_nobeep,
1328
1333
   (char**) &opt_nobeep, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
1329
 
  {"host", 'h', N_("Connect to host."), (char**) &current_host,
 
1334
  {"host", 'h', "Connect to host.", (char**) &current_host,
1330
1335
   (char**) &current_host, 0, GET_STR_ALLOC, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
1331
 
  {"line-numbers", OPT_LINE_NUMBERS, N_("Write line numbers for errors."),
 
1336
  {"html", 'H', "Produce HTML output.", (char**) &opt_html, (char**) &opt_html,
 
1337
   0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
 
1338
  {"xml", 'X', "Produce XML output", (char**) &opt_xml, (char**) &opt_xml, 0,
 
1339
   GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
 
1340
  {"line-numbers", OPT_LINE_NUMBERS, "Write line numbers for errors.",
1332
1341
   (char**) &line_numbers, (char**) &line_numbers, 0, GET_BOOL,
1333
1342
   NO_ARG, 1, 0, 0, 0, 0, 0},
1334
 
  {"skip-line-numbers", 'L', N_("Don't write line number for errors. WARNING: -L is deprecated, use long version of this option instead."), 0, 0, 0, GET_NO_ARG,
 
1343
  {"skip-line-numbers", 'L', "Don't write line number for errors. WARNING: -L is deprecated, use long version of this option instead.", 0, 0, 0, GET_NO_ARG,
1335
1344
   NO_ARG, 0, 0, 0, 0, 0, 0},
1336
 
  {"unbuffered", 'n', N_("Flush buffer after each query."), (char**) &unbuffered,
 
1345
  {"unbuffered", 'n', "Flush buffer after each query.", (char**) &unbuffered,
1337
1346
   (char**) &unbuffered, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
1338
 
  {"column-names", OPT_COLUMN_NAMES, N_("Write column names in results."),
 
1347
  {"column-names", OPT_COLUMN_NAMES, "Write column names in results.",
1339
1348
   (char**) &column_names, (char**) &column_names, 0, GET_BOOL,
1340
1349
   NO_ARG, 1, 0, 0, 0, 0, 0},
1341
1350
  {"skip-column-names", 'N',
1342
 
   N_("Don't write column names in results. WARNING: -N is deprecated, use long version of this options instead."),
 
1351
   "Don't write column names in results. WARNING: -N is deprecated, use long version of this options instead.",
1343
1352
   0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
1344
1353
  {"set-variable", 'O',
1345
 
   N_("Change the value of a variable. Please note that this option is deprecated; you can set variables directly with --variable-name=value."),
 
1354
   "Change the value of a variable. Please note that this option is deprecated; you can set variables directly with --variable-name=value.",
1346
1355
   0, 0, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
1347
 
  {"sigint-ignore", OPT_SIGINT_IGNORE, N_("Ignore SIGINT (CTRL-C)"),
 
1356
  {"sigint-ignore", OPT_SIGINT_IGNORE, "Ignore SIGINT (CTRL-C)",
1348
1357
   (char**) &opt_sigint_ignore,  (char**) &opt_sigint_ignore, 0, GET_BOOL,
1349
1358
   NO_ARG, 0, 0, 0, 0, 0, 0},
1350
1359
  {"one-database", 'o',
1351
 
   N_("Only update the default database. This is useful for skipping updates to other database in the update log."),
 
1360
   "Only update the default database. This is useful for skipping updates to other database in the update log.",
1352
1361
   0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
1353
1362
  {"pager", OPT_PAGER,
1354
 
   N_("Pager to use to display results. If you don't supply an option the default pager is taken from your ENV variable PAGER. Valid pagers are less, more, cat [> filename], etc. See interactive help (\\h) also. This option does not work in batch mode. Disable with --disable-pager. This option is disabled by default."),
 
1363
   "Pager to use to display results. If you don't supply an option the default pager is taken from your ENV variable PAGER. Valid pagers are less, more, cat [> filename], etc. See interactive help (\\h) also. This option does not work in batch mode. Disable with --disable-pager. This option is disabled by default.",
1355
1364
   0, 0, 0, GET_STR, OPT_ARG, 0, 0, 0, 0, 0, 0},
1356
1365
  {"no-pager", OPT_NOPAGER,
1357
 
   N_("Disable pager and print to stdout. See interactive help (\\h) also. WARNING: option deprecated; use --disable-pager instead."),
 
1366
   "Disable pager and print to stdout. See interactive help (\\h) also. WARNING: option deprecated; use --disable-pager instead.",
1358
1367
   0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
1359
1368
  {"password", 'p',
1360
 
   N_("Password to use when connecting to server. If password is not given it's asked from the tty."),
 
1369
   "Password to use when connecting to server. If password is not given it's asked from the tty.",
1361
1370
   0, 0, 0, GET_STR, OPT_ARG, 0, 0, 0, 0, 0, 0},
1362
 
  {"port", 'P', N_("Port number to use for connection or 0 for default to, in order of preference, my.cnf, $DRIZZLE_TCP_PORT, ")
1363
 
   N_("built-in default") " (" STRINGIFY_ARG(DRIZZLE_PORT) ").",
1364
 
   (char**) &opt_drizzle_port,
1365
 
   (char**) &opt_drizzle_port, 0, GET_UINT, REQUIRED_ARG, 0, 0, 0, 0, 0,  0},
1366
 
  {"prompt", OPT_PROMPT, N_("Set the drizzle prompt to this value."),
 
1371
  {"port", 'P', "Port number to use for connection or 0 for default to, in "
 
1372
   "order of preference, my.cnf, $MYSQL_TCP_PORT, "
 
1373
#if MYSQL_PORT_DEFAULT == 0
 
1374
   "/etc/services, "
 
1375
#endif
 
1376
   "built-in default (" STRINGIFY_ARG(MYSQL_PORT) ").",
 
1377
   (char**) &opt_mysql_port,
 
1378
   (char**) &opt_mysql_port, 0, GET_UINT, REQUIRED_ARG, 0, 0, 0, 0, 0,  0},
 
1379
  {"prompt", OPT_PROMPT, "Set the mysql prompt to this value.",
1367
1380
   (char**) &current_prompt, (char**) &current_prompt, 0, GET_STR_ALLOC,
1368
1381
   REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
1369
 
  {"protocol", OPT_DRIZZLE_PROTOCOL, N_("The protocol of connection (tcp,socket,pipe,memory)."),
 
1382
  {"protocol", OPT_MYSQL_PROTOCOL, "The protocol of connection (tcp,socket,pipe,memory).",
1370
1383
   0, 0, 0, GET_STR,  REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
1371
1384
  {"quick", 'q',
1372
 
   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."),
 
1385
   "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.",
1373
1386
   (char**) &quick, (char**) &quick, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
1374
 
  {"raw", 'r', N_("Write fields without conversion. Used with --batch."),
 
1387
  {"raw", 'r', "Write fields without conversion. Used with --batch.",
1375
1388
   (char**) &opt_raw_data, (char**) &opt_raw_data, 0, GET_BOOL, NO_ARG, 0, 0, 0,
1376
1389
   0, 0, 0},
1377
 
  {"reconnect", OPT_RECONNECT, N_("Reconnect if the connection is lost. Disable with --disable-reconnect. This option is enabled by default."),
 
1390
  {"reconnect", OPT_RECONNECT, "Reconnect if the connection is lost. Disable with --disable-reconnect. This option is enabled by default.",
1378
1391
   (char**) &opt_reconnect, (char**) &opt_reconnect, 0, GET_BOOL, NO_ARG, 1, 0, 0, 0, 0, 0},
1379
 
  {"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,
 
1392
  {"silent", 's', "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,
1380
1393
   0, 0},
1381
 
  {"socket", 'S', N_("Socket file to use for connection."),
1382
 
   (char**) &opt_drizzle_unix_port, (char**) &opt_drizzle_unix_port, 0, GET_STR_ALLOC,
 
1394
  {"socket", 'S', "Socket file to use for connection.",
 
1395
   (char**) &opt_mysql_unix_port, (char**) &opt_mysql_unix_port, 0, GET_STR_ALLOC,
1383
1396
   REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
1384
 
  {"table", 't', N_("Output in table format."), (char**) &output_tables,
 
1397
  {"table", 't', "Output in table format.", (char**) &output_tables,
1385
1398
   (char**) &output_tables, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
1386
1399
  {"tee", OPT_TEE,
1387
 
   N_("Append everything into outfile. See interactive help (\\h) also. Does not work in batch mode. Disable with --disable-tee. This option is disabled by default."),
 
1400
   "Append everything into outfile. See interactive help (\\h) also. Does not work in batch mode. Disable with --disable-tee. This option is disabled by default.",
1388
1401
   0, 0, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
1389
 
  {"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,
 
1402
  {"no-tee", OPT_NOTEE, "Disable outfile. See interactive help (\\h) also. WARNING: option deprecated; use --disable-tee instead", 0, 0, 0, GET_NO_ARG,
1390
1403
   NO_ARG, 0, 0, 0, 0, 0, 0},
1391
1404
#ifndef DONT_ALLOW_USER_CHANGE
1392
 
  {"user", 'u', N_("User for login if not current user."), (char**) &current_user,
 
1405
  {"user", 'u', "User for login if not current user.", (char**) &current_user,
1393
1406
   (char**) &current_user, 0, GET_STR_ALLOC, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
1394
1407
#endif
1395
 
  {"safe-updates", 'U', N_("Only allow UPDATE and DELETE that uses keys."),
1396
 
   (char**) &safe_updates, (char**) &safe_updates, 0, GET_BOOL, NO_ARG, 0, 0,
1397
 
   0, 0, 0, 0},
1398
 
  {"i-am-a-dummy", 'U', N_("Synonym for option --safe-updates, -U."),
1399
 
   (char**) &safe_updates, (char**) &safe_updates, 0, GET_BOOL, NO_ARG, 0, 0,
1400
 
   0, 0, 0, 0},
1401
 
  {"verbose", 'v', N_("Write more. (-v -v -v gives the table output format)."), 0,
 
1408
  {"safe-updates", 'U', "Only allow UPDATE and DELETE that uses keys.",
 
1409
   (char**) &safe_updates, (char**) &safe_updates, 0, GET_BOOL, NO_ARG, 0, 0,
 
1410
   0, 0, 0, 0},
 
1411
  {"i-am-a-dummy", 'U', "Synonym for option --safe-updates, -U.",
 
1412
   (char**) &safe_updates, (char**) &safe_updates, 0, GET_BOOL, NO_ARG, 0, 0,
 
1413
   0, 0, 0, 0},
 
1414
  {"verbose", 'v', "Write more. (-v -v -v gives the table output format).", 0,
1402
1415
   0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
1403
 
  {"version", 'V', N_("Output version information and exit."), 0, 0, 0,
 
1416
  {"version", 'V', "Output version information and exit.", 0, 0, 0,
1404
1417
   GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
1405
 
  {"wait", 'w', N_("Wait and retry if connection is down."), 0, 0, 0, GET_NO_ARG,
 
1418
  {"wait", 'w', "Wait and retry if connection is down.", 0, 0, 0, GET_NO_ARG,
1406
1419
   NO_ARG, 0, 0, 0, 0, 0, 0},
1407
1420
  {"connect_timeout", OPT_CONNECT_TIMEOUT,
1408
 
   N_("Number of seconds before connection timeout."),
 
1421
   "Number of seconds before connection timeout.",
1409
1422
   (char**) &opt_connect_timeout,
1410
1423
   (char**) &opt_connect_timeout, 0, GET_ULONG, REQUIRED_ARG, 0, 0, 3600*12, 0,
1411
1424
   0, 0},
1412
1425
  {"max_allowed_packet", OPT_MAX_ALLOWED_PACKET,
1413
 
   N_("Max packet length to send to, or receive from server"),
 
1426
   "Max packet length to send to, or receive from server",
1414
1427
   (char**) &opt_max_allowed_packet, (char**) &opt_max_allowed_packet, 0,
1415
1428
   GET_ULONG, REQUIRED_ARG, 16 *1024L*1024L, 4096,
1416
1429
   (int64_t) 2*1024L*1024L*1024L, MALLOC_OVERHEAD, 1024, 0},
1417
1430
  {"net_buffer_length", OPT_NET_BUFFER_LENGTH,
1418
 
   N_("Buffer for TCP/IP and socket communication"),
 
1431
   "Buffer for TCP/IP and socket communication",
1419
1432
   (char**) &opt_net_buffer_length, (char**) &opt_net_buffer_length, 0, GET_ULONG,
1420
1433
   REQUIRED_ARG, 16384, 1024, 512*1024*1024L, MALLOC_OVERHEAD, 1024, 0},
1421
1434
  {"select_limit", OPT_SELECT_LIMIT,
1422
 
   N_("Automatic limit for SELECT when using --safe-updates"),
 
1435
   "Automatic limit for SELECT when using --safe-updates",
1423
1436
   (char**) &select_limit,
1424
1437
   (char**) &select_limit, 0, GET_ULONG, REQUIRED_ARG, 1000L, 1, ULONG_MAX,
1425
1438
   0, 1, 0},
1426
1439
  {"max_join_size", OPT_MAX_JOIN_SIZE,
1427
 
   N_("Automatic limit for rows in a join when using --safe-updates"),
 
1440
   "Automatic limit for rows in a join when using --safe-updates",
1428
1441
   (char**) &max_join_size,
1429
1442
   (char**) &max_join_size, 0, GET_ULONG, REQUIRED_ARG, 1000000L, 1, ULONG_MAX,
1430
1443
   0, 1, 0},
1431
 
  {"secure-auth", OPT_SECURE_AUTH, N_("Refuse client connecting to server if it uses old (pre-4.1.1) protocol"), (char**) &opt_secure_auth,
 
1444
  {"secure-auth", OPT_SECURE_AUTH, "Refuse client connecting to server if it"
 
1445
   " uses old (pre-4.1.1) protocol", (char**) &opt_secure_auth,
1432
1446
   (char**) &opt_secure_auth, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
1433
 
  {"show-warnings", OPT_SHOW_WARNINGS, N_("Show warnings after every statement."),
 
1447
  {"server-arg", OPT_SERVER_ARG, "Send embedded server this as a parameter.",
 
1448
   0, 0, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
1449
  {"show-warnings", OPT_SHOW_WARNINGS, "Show warnings after every statement.",
1434
1450
   (char**) &show_warnings, (char**) &show_warnings, 0, GET_BOOL, NO_ARG,
1435
1451
   0, 0, 0, 0, 0, 0},
1436
1452
  { 0, 0, 0, 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0}
1441
1457
{
1442
1458
  const char* readline= "readline";
1443
1459
 
1444
 
  printf(_("%s  Ver %s Distrib %s, for %s (%s) using %s %s\n"),
1445
 
         my_progname, VER, drizzle_get_client_info(),
1446
 
         SYSTEM_TYPE, MACHINE_TYPE,
 
1460
  printf("%s  Ver %s Distrib %s, for %s (%s) using %s %s\n",
 
1461
         my_progname, VER, MYSQL_SERVER_VERSION, SYSTEM_TYPE, MACHINE_TYPE,
1447
1462
         readline, rl_library_version);
1448
1463
 
1449
1464
  if (version)
1450
1465
    return;
1451
 
  printf(_("\
 
1466
  printf("\
1452
1467
Copyright (C) 2000-2008 MySQL AB\n                                      \
1453
1468
This software comes with ABSOLUTELY NO WARRANTY. This is free software,\n \
1454
 
and you are welcome to modify and redistribute it under the GPL license\n"));
1455
 
  printf(_("Usage: %s [OPTIONS] [database]\n"), my_progname);
 
1469
and you are welcome to modify and redistribute it under the GPL license\n");
 
1470
  printf("Usage: %s [OPTIONS] [database]\n", my_progname);
1456
1471
  my_print_help(my_long_options);
1457
1472
  print_defaults("my", load_default_groups);
1458
1473
  my_print_variables(my_long_options);
1465
1480
{
1466
1481
  switch(optid) {
1467
1482
  case OPT_CHARSETS_DIR:
1468
 
    strmake(drizzle_charsets_dir, argument, sizeof(drizzle_charsets_dir) - 1);
1469
 
    charsets_dir = drizzle_charsets_dir;
 
1483
    strmake(mysql_charsets_dir, argument, sizeof(mysql_charsets_dir) - 1);
 
1484
    charsets_dir = mysql_charsets_dir;
1470
1485
    break;
1471
1486
  case  OPT_DEFAULT_CHARSET:
1472
1487
    default_charset_used= 1;
1474
1489
  case OPT_DELIMITER:
1475
1490
    if (argument == disabled_my_option)
1476
1491
    {
1477
 
      my_stpcpy(delimiter, DEFAULT_DELIMITER);
 
1492
      strmov(delimiter, DEFAULT_DELIMITER);
1478
1493
    }
1479
1494
    else
1480
1495
    {
1485
1500
      }
1486
1501
      else
1487
1502
      {
1488
 
        put_info(_("DELIMITER cannot contain a backslash character"),
 
1503
        put_info("DELIMITER cannot contain a backslash character",
1489
1504
                 INFO_ERROR,0,0);
1490
1505
        return 0;
1491
1506
      }
1506
1521
      init_tee(argument);
1507
1522
    break;
1508
1523
  case OPT_NOTEE:
1509
 
    printf(_("WARNING: option deprecated; use --disable-tee instead.\n"));
 
1524
    printf("WARNING: option deprecated; use --disable-tee instead.\n");
1510
1525
    if (opt_outfile)
1511
1526
      end_tee();
1512
1527
    break;
1520
1535
      {
1521
1536
        default_pager_set= 1;
1522
1537
        strmake(pager, argument, sizeof(pager) - 1);
1523
 
        my_stpcpy(default_pager, pager);
 
1538
        strmov(default_pager, pager);
1524
1539
      }
1525
1540
      else if (default_pager_set)
1526
 
        my_stpcpy(pager, default_pager);
 
1541
        strmov(pager, default_pager);
1527
1542
      else
1528
1543
        opt_nopager= 1;
1529
1544
    }
1530
1545
    break;
1531
1546
  case OPT_NOPAGER:
1532
 
    printf(_("WARNING: option deprecated; use --disable-pager instead.\n"));
 
1547
    printf("WARNING: option deprecated; use --disable-pager instead.\n");
1533
1548
    opt_nopager= 1;
1534
1549
    break;
 
1550
  case OPT_MYSQL_PROTOCOL:
 
1551
    opt_protocol= find_type_or_exit(argument, &sql_protocol_typelib,
 
1552
                                    opt->name);
 
1553
    break;
1535
1554
  case OPT_SERVER_ARG:
1536
 
    printf(_("WARNING: --server-arg option not supported in this configuration.\n"));
 
1555
    printf("WARNING: --server-arg option not supported in this configuration.\n");
1537
1556
    break;
1538
1557
  case 'A':
1539
1558
    opt_rehash= 0;
1557
1576
    break;
1558
1577
  case 'p':
1559
1578
    if (argument == disabled_my_option)
1560
 
      argument= (char*) "";      // Don't require password
 
1579
      argument= (char*) "";                     // Don't require password
1561
1580
    if (argument)
1562
1581
    {
1563
1582
      char *start= argument;
1564
 
      free(opt_password);
 
1583
      my_free(opt_password, MYF(MY_ALLOW_ZERO_PTR));
1565
1584
      opt_password= strdup(argument);
1566
 
      while (*argument) *argument++= 'x';        // Destroy argument
 
1585
      while (*argument) *argument++= 'x';               // Destroy argument
1567
1586
      if (*start)
1568
1587
        start[1]=0 ;
1569
1588
      tty_password= 0;
1605
1624
{
1606
1625
  char *tmp, *pagpoint;
1607
1626
  int ho_error;
1608
 
  const DRIZZLE_PARAMETERS *drizzle_params= drizzle_get_parameters();
 
1627
  MYSQL_PARAMETERS *mysql_params= mysql_get_parameters();
1609
1628
 
1610
 
  tmp= (char *) getenv("DRIZZLE_HOST");
 
1629
  tmp= (char *) getenv("MYSQL_HOST");
1611
1630
  if (tmp)
1612
1631
    current_host= strdup(tmp);
1613
1632
 
1614
1633
  pagpoint= getenv("PAGER");
1615
1634
  if (!((char*) (pagpoint)))
1616
1635
  {
1617
 
    my_stpcpy(pager, "stdout");
 
1636
    strmov(pager, "stdout");
1618
1637
    opt_nopager= 1;
1619
1638
  }
1620
1639
  else
1621
 
    my_stpcpy(pager, pagpoint);
1622
 
  my_stpcpy(default_pager, pager);
 
1640
    strmov(pager, pagpoint);
 
1641
  strmov(default_pager, pager);
1623
1642
 
1624
 
  opt_max_allowed_packet= *drizzle_params->p_max_allowed_packet;
1625
 
  opt_net_buffer_length= *drizzle_params->p_net_buffer_length;
 
1643
  opt_max_allowed_packet= *mysql_params->p_max_allowed_packet;
 
1644
  opt_net_buffer_length= *mysql_params->p_net_buffer_length;
1626
1645
 
1627
1646
  if ((ho_error=handle_options(&argc, &argv, my_long_options, get_one_option)))
1628
1647
    exit(ho_error);
1629
1648
 
1630
 
  *drizzle_params->p_max_allowed_packet= opt_max_allowed_packet;
1631
 
  *drizzle_params->p_net_buffer_length= opt_net_buffer_length;
 
1649
  *mysql_params->p_max_allowed_packet= opt_max_allowed_packet;
 
1650
  *mysql_params->p_net_buffer_length= opt_net_buffer_length;
1632
1651
 
1633
1652
  if (status.batch) /* disable pager and outfile in this case */
1634
1653
  {
1635
 
    my_stpcpy(default_pager, "stdout");
1636
 
    my_stpcpy(pager, "stdout");
 
1654
    strmov(default_pager, "stdout");
 
1655
    strmov(pager, "stdout");
1637
1656
    opt_nopager= 1;
1638
1657
    default_pager_set= 0;
1639
1658
    opt_outfile= 0;
1641
1660
    connect_flag= 0; /* Not in interactive mode */
1642
1661
  }
1643
1662
 
 
1663
  if (strcmp(default_charset, charset_info->csname) &&
 
1664
      !(charset_info= get_charset_by_csname(default_charset,
 
1665
                                            MY_CS_PRIMARY, MYF(MY_WME))))
 
1666
    exit(1);
1644
1667
  if (argc > 1)
1645
1668
  {
1646
1669
    usage(0);
1649
1672
  if (argc == 1)
1650
1673
  {
1651
1674
    skip_updates= 0;
1652
 
    free(current_db);
 
1675
    my_free(current_db, MYF(MY_ALLOW_ZERO_PTR));
1653
1676
    current_db= strdup(*argv);
1654
1677
  }
1655
1678
  if (tty_password)
1656
 
    opt_password= get_tty_password(NULL);
 
1679
    opt_password= get_tty_password(NullS);
1657
1680
  if (debug_info_flag)
1658
1681
    my_end_arg= MY_CHECK_ERROR | MY_GIVE_INFO;
1659
1682
  if (debug_check_flag)
1665
1688
{
1666
1689
  char *line;
1667
1690
  char in_string=0;
1668
 
  uint32_t line_number=0;
 
1691
  ulong line_number=0;
1669
1692
  bool ml_comment= 0;
1670
1693
  COMMANDS *com;
1671
1694
  status.exit_status=1;
1682
1705
        you save the file using "Unicode UTF-8" format.
1683
1706
      */
1684
1707
      if (!line_number &&
1685
 
          (unsigned char) line[0] == 0xEF &&
1686
 
          (unsigned char) line[1] == 0xBB &&
1687
 
          (unsigned char) line[2] == 0xBF)
 
1708
          (uchar) line[0] == 0xEF &&
 
1709
          (uchar) line[1] == 0xBB &&
 
1710
          (uchar) line[2] == 0xBF)
1688
1711
        line+= 3;
1689
1712
      line_number++;
1690
 
      if (!glob_buffer->empty())
 
1713
      if (glob_buffer->length!=0)
1691
1714
        status.query_start_line=line_number;
1692
1715
    }
1693
1716
    else
1694
1717
    {
1695
 
      const char *prompt= (const char*) (ml_comment ? "   /*> " :
1696
 
                                         (glob_buffer->empty())
1697
 
                                         ?  construct_prompt()
1698
 
                                         : !in_string ? "    -> " :
1699
 
                                         in_string == '\'' ?
1700
 
                                         "    '> " : (in_string == '`' ?
1701
 
                                                      "    `> " :
1702
 
                                                      "    \"> "));
1703
 
      if (opt_outfile && glob_buffer->empty())
 
1718
      char *prompt= (char*) (ml_comment ? "   /*> " :
 
1719
                             (glob_buffer->length == 0) ?  construct_prompt() :
 
1720
                             !in_string ? "    -> " :
 
1721
                             in_string == '\'' ?
 
1722
                             "    '> " : (in_string == '`' ?
 
1723
                                          "    `> " :
 
1724
                                          "    \"> "));
 
1725
      if (opt_outfile && (glob_buffer->length==0))
1704
1726
        fflush(OUTFILE);
1705
1727
 
1706
1728
      if (opt_outfile)
1721
1743
    }
1722
1744
 
1723
1745
    /*
1724
 
      Check if line is a drizzle command line
 
1746
      Check if line is a mysql command line
1725
1747
      (We want to allow help, print and clear anywhere at line start
1726
1748
    */
1727
 
    if ((named_cmds || (glob_buffer->empty()))
 
1749
    if ((named_cmds || (glob_buffer->length==0))
1728
1750
        && !ml_comment && !in_string && (com=find_command(line,0)))
1729
1751
    {
1730
1752
      if ((*com->func)(glob_buffer,line) > 0)
1731
1753
        break;
1732
1754
      // If buffer was emptied
1733
 
      if (glob_buffer->empty())
 
1755
      if (glob_buffer->length==0)
1734
1756
        in_string=0;
1735
1757
      if (interactive && status.add_to_history && not_in_history(line))
1736
1758
        add_history(line);
1744
1766
  if (!interactive && !status.exit_status)
1745
1767
  {
1746
1768
    remove_cntrl(glob_buffer);
1747
 
    if (!glob_buffer->empty())
 
1769
    if (glob_buffer->length != 0)
1748
1770
    {
1749
1771
      status.exit_status=1;
1750
1772
      if (com_go(glob_buffer,line) <= 0)
1756
1778
}
1757
1779
 
1758
1780
 
1759
 
static COMMANDS *find_command(const char *name,char cmd_char)
 
1781
static COMMANDS *find_command(char *name,char cmd_char)
1760
1782
{
1761
1783
  uint len;
1762
 
  const char *end;
 
1784
  char *end;
1763
1785
 
1764
1786
  if (!name)
1765
1787
  {
1778
1800
    if (strstr(name, "\\g") || (strstr(name, delimiter) &&
1779
1801
                                !(strlen(name) >= 9 &&
1780
1802
                                  !my_strnncoll(charset_info,
1781
 
                                                (unsigned char*) name, 9,
1782
 
                                                (const unsigned char*) "delimiter",
 
1803
                                                (uchar*) name, 9,
 
1804
                                                (const uchar*) "delimiter",
1783
1805
                                                9))))
1784
1806
      return((COMMANDS *) 0);
1785
1807
    if ((end=strcont(name," \t")))
1788
1810
      while (my_isspace(charset_info,*end))
1789
1811
        end++;
1790
1812
      if (!*end)
1791
 
        end=0;          // no arguments to function
 
1813
        end=0;                                  // no arguments to function
1792
1814
    }
1793
1815
    else
1794
1816
      len=(uint) strlen(name);
1797
1819
  for (uint i= 0; commands[i].name; i++)
1798
1820
  {
1799
1821
    if (commands[i].func &&
1800
 
        ((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)))
 
1822
        ((name && !my_strnncoll(charset_info,(uchar*)name,len, (uchar*)commands[i].name,len) && !commands[i].name[len] && (!end || (end && commands[i].takes_params))) || (!name && commands[i].cmd_char == cmd_char)))
1801
1823
    {
1802
1824
      return(&commands[i]);
1803
1825
    }
1806
1828
}
1807
1829
 
1808
1830
 
1809
 
static bool add_line(string *buffer, char *line, char *in_string,
 
1831
static bool add_line(DYNAMIC_STRING *buffer,char *line,char *in_string,
1810
1832
                        bool *ml_comment)
1811
1833
{
1812
 
  unsigned char inchar;
 
1834
  uchar inchar;
1813
1835
  char buff[80], *pos, *out;
1814
1836
  COMMANDS *com;
1815
1837
  bool need_space= 0;
1816
1838
  bool ss_comment= 0;
1817
1839
 
1818
1840
 
1819
 
  if (!line[0] && (buffer->empty()))
 
1841
  if (!line[0] && (buffer->length==0))
1820
1842
    return(0);
1821
1843
  if (status.add_to_history && line[0] && not_in_history(line))
1822
1844
    add_history(line);
1823
1845
  char *end_of_line=line+(uint) strlen(line);
1824
1846
 
1825
 
  for (pos=out=line ; (inchar= (unsigned char) *pos) ; pos++)
 
1847
  for (pos=out=line ; (inchar= (uchar) *pos) ; pos++)
1826
1848
  {
1827
1849
    if (!preserve_comments)
1828
1850
    {
1829
1851
      // Skip spaces at the beggining of a statement
1830
1852
      if (my_isspace(charset_info,inchar) && (out == line) &&
1831
 
          (buffer->empty()))
 
1853
          (buffer->length==0))
1832
1854
        continue;
1833
1855
    }
1834
1856
 
1850
1872
    }
1851
1873
#endif
1852
1874
        if (!*ml_comment && inchar == '\\' &&
1853
 
            !(*in_string && (drizzle.server_status & SERVER_STATUS_NO_BACKSLASH_ESCAPES)))
 
1875
        !(mysql.server_status & SERVER_STATUS_NO_BACKSLASH_ESCAPES))
1854
1876
    {
1855
1877
      // Found possbile one character command like \c
1856
1878
 
1857
 
      if (!(inchar = (unsigned char) *++pos))
1858
 
        break;        // readline adds one '\'
1859
 
      if (*in_string || inchar == 'N')  // \N is short for NULL
1860
 
      {          // Don't allow commands in string
 
1879
      if (!(inchar = (uchar) *++pos))
 
1880
        break;                          // readline adds one '\'
 
1881
      if (*in_string || inchar == 'N')  // \N is short for NULL
 
1882
      {                                 // Don't allow commands in string
1861
1883
        *out++='\\';
1862
1884
        *out++= (char) inchar;
1863
1885
        continue;
1864
1886
      }
1865
 
      if ((com=find_command(NULL,(char) inchar)))
 
1887
      if ((com=find_command(NullS,(char) inchar)))
1866
1888
      {
1867
1889
        // Flush previously accepted characters
1868
1890
        if (out != line)
1869
1891
        {
1870
 
          buffer->append(line, (out-line));
 
1892
          dynstr_append_mem(buffer, line, (out-line));
1871
1893
          out= line;
1872
1894
        }
1873
1895
 
1891
1913
            for (pos++ ;
1892
1914
                 *pos && (*pos != *delimiter ||
1893
1915
                          !is_prefix(pos + 1, delimiter + 1)) ; pos++)
1894
 
              ;  // Remove parameters
 
1916
              ; // Remove parameters
1895
1917
            if (!*pos)
1896
1918
              pos--;
1897
1919
            else
1901
1923
      }
1902
1924
      else
1903
1925
      {
1904
 
        sprintf(buff,_("Unknown command '\\%c'."),inchar);
 
1926
        sprintf(buff,"Unknown command '\\%c'.",inchar);
1905
1927
        if (put_info(buff,INFO_ERROR,0,0) > 0)
1906
1928
          return(1);
1907
1929
        *out++='\\';
1911
1933
    }
1912
1934
    else if (!*ml_comment && !*in_string &&
1913
1935
             (end_of_line - pos) >= 10 &&
1914
 
             !my_strnncoll(charset_info, (unsigned char*) pos, 10,
1915
 
                           (const unsigned char*) "delimiter ", 10))
 
1936
             !my_strnncoll(charset_info, (uchar*) pos, 10,
 
1937
                           (const uchar*) "delimiter ", 10))
1916
1938
    {
1917
1939
      // Flush previously accepted characters
1918
1940
      if (out != line)
1919
1941
      {
1920
 
        buffer->append(line, (out - line));
 
1942
        dynstr_append_mem(buffer, line, (out - line));
1921
1943
        out= line;
1922
1944
      }
1923
1945
 
1924
1946
      // Flush possible comments in the buffer
1925
 
      if (!buffer->empty())
 
1947
      if (buffer->length != 0)
1926
1948
      {
1927
1949
        if (com_go(buffer, 0) > 0) // < 0 is not fatal
1928
1950
          return(1);
1929
1951
        assert(buffer!=NULL);
1930
 
        buffer->clear();
 
1952
        dynstr_set(buffer, NULL);
1931
1953
      }
1932
1954
 
1933
1955
      /*
1934
1956
        Delimiter wants the get rest of the given line as argument to
1935
1957
        allow one to change ';' to ';;' and back
1936
1958
      */
1937
 
      buffer->append(pos);
 
1959
      dynstr_append(buffer,pos);
1938
1960
      if (com_delimiter(buffer, pos) > 0)
1939
1961
        return(1);
1940
1962
 
1941
 
      buffer->clear();
 
1963
      dynstr_set(buffer, NULL);
1942
1964
      break;
1943
1965
    }
1944
1966
    else if (!*ml_comment && !*in_string && is_prefix(pos, delimiter))
1954
1976
      // Flush previously accepted characters
1955
1977
      if (out != line)
1956
1978
      {
1957
 
        buffer->append(line, (out-line));
 
1979
        dynstr_append_mem(buffer, line, (out-line));
1958
1980
        out= line;
1959
1981
      }
1960
1982
 
1964
1986
                                 my_isspace(charset_info, pos[2]))))
1965
1987
      {
1966
1988
        // Add trailing single line comments to this statement
1967
 
        buffer->append(pos);
 
1989
        dynstr_append(buffer, pos);
1968
1990
        pos+= strlen(pos);
1969
1991
      }
1970
1992
 
1971
1993
      pos--;
1972
1994
 
1973
 
      if ((com= find_command(buffer->c_str(), 0)))
 
1995
      if ((com= find_command(buffer->str, 0)))
1974
1996
      {
1975
1997
 
1976
 
        if ((*com->func)(buffer, buffer->c_str()) > 0)
 
1998
        if ((*com->func)(buffer, buffer->str) > 0)
1977
1999
          return(1);                       // Quit
1978
2000
      }
1979
2001
      else
1981
2003
        if (com_go(buffer, 0) > 0)             // < 0 is not fatal
1982
2004
          return(1);
1983
2005
      }
1984
 
      buffer->clear();
 
2006
      dynstr_set(buffer, NULL);
1985
2007
    }
1986
2008
    else if (!*ml_comment
1987
2009
             && (!*in_string
1993
2015
      // Flush previously accepted characters
1994
2016
      if (out != line)
1995
2017
      {
1996
 
        buffer->append(line, (out - line));
 
2018
        dynstr_append_mem(buffer, line, (out - line));
1997
2019
        out= line;
1998
2020
      }
1999
2021
 
2000
2022
      // comment to end of line
2001
2023
      if (preserve_comments)
2002
 
        buffer->append(pos);
 
2024
        dynstr_append(buffer,pos);
2003
2025
 
2004
2026
      break;
2005
2027
    }
2016
2038
      *ml_comment= 1;
2017
2039
      if (out != line)
2018
2040
      {
2019
 
        buffer->append(line, (out-line));
 
2041
        dynstr_append_mem(buffer, line, (out-line));
2020
2042
        out=line;
2021
2043
      }
2022
2044
    }
2032
2054
      *ml_comment= 0;
2033
2055
      if (out != line)
2034
2056
      {
2035
 
        buffer->append(line, (out - line));
 
2057
        dynstr_append_mem(buffer, line, (out - line));
2036
2058
        out= line;
2037
2059
      }
2038
2060
      // Consumed a 2 chars or more, and will add 1 at most,
2061
2083
      }
2062
2084
    }
2063
2085
  }
2064
 
  if (out != line || (buffer->length() > 0))
 
2086
  if (out != line || (buffer->length > 0))
2065
2087
  {
2066
2088
    *out++='\n';
2067
2089
    uint length=(uint) (out-line);
2068
 
    if ((!*ml_comment || preserve_comments))
2069
 
      buffer->append(line, length);
 
2090
    if ((!*ml_comment || preserve_comments)
 
2091
        && dynstr_append_mem(buffer, line, length))
 
2092
      return(1);
2070
2093
  }
2071
2094
  return(0);
2072
2095
}
2076
2099
******************************************************************/
2077
2100
 
2078
2101
 
2079
 
static char **mysql_completion (const char *text, int start, int end);
 
2102
#ifdef HAVE_READLINE_COMPLETION
2080
2103
static char *new_command_generator(const char *text, int);
 
2104
extern char **new_mysql_completion (const char *text, int start, int end);
 
2105
#endif
2081
2106
 
2082
2107
/*
2083
2108
  Tell the GNU Readline library how to complete.  We want to try to complete
2084
2109
  on command names if this is the first word in the line, or on filenames
2085
2110
  if not.
2086
2111
*/
2087
 
static char *no_completion(const char * a __attribute__((unused)),
2088
 
                           int b __attribute__((unused)))
 
2112
 
 
2113
#if defined(USE_NEW_READLINE_INTERFACE) || defined(USE_LIBEDIT_INTERFACE)
 
2114
extern char *no_completion(const char*,int);
 
2115
#else
 
2116
char *no_completion(void);
 
2117
char *no_completion(void)
2089
2118
{
2090
2119
  /* No filename completion */
2091
2120
  return 0;
2092
2121
}
2093
 
 
 
2122
#endif
2094
2123
 
2095
2124
/* glues pieces of history back together if in pieces   */
2096
 
static void fix_history(string *final_command)
 
2125
static void fix_history(DYNAMIC_STRING *final_command)
2097
2126
{
2098
2127
  int total_lines = 1;
2099
 
  const char *ptr = final_command->c_str();
 
2128
  const char *ptr = final_command->str;
2100
2129
  char str_char = '\0';  /* Character if we are in a string or not */
2101
2130
 
2102
2131
  /* Converted buffer */
2103
 
  string fixed_buffer;
2104
 
  fixed_buffer.reserve(512);
 
2132
  DYNAMIC_STRING *fixed_buffer=
 
2133
    (DYNAMIC_STRING *)my_malloc(sizeof(DYNAMIC_STRING), MYF(0));
 
2134
 
 
2135
  init_dynamic_string(fixed_buffer, "", 512, 512);
2105
2136
 
2106
2137
  /* find out how many lines we have and remove newlines */
2107
2138
  while (*ptr != '\0')
2116
2147
        str_char = *ptr;
2117
2148
      else if (str_char == *ptr)   /* close string */
2118
2149
        str_char = '\0';
2119
 
      fixed_buffer.append(ptr, 1);
 
2150
      dynstr_append_mem(fixed_buffer, ptr, 1);
2120
2151
      break;
2121
2152
    case '\n':
2122
2153
      /*
2123
2154
        not in string, change to space
2124
2155
        if in string, leave it alone
2125
2156
      */
2126
 
      fixed_buffer.append((str_char == '\0') ? " " : "\n");
 
2157
      dynstr_append(fixed_buffer,(str_char == '\0') ? " " : "\n");
2127
2158
      total_lines++;
2128
2159
      break;
2129
2160
    case '\\':
2130
 
      fixed_buffer.append("\\");
 
2161
      dynstr_append(fixed_buffer, "\\");
2131
2162
      /* need to see if the backslash is escaping anything */
2132
2163
      if (str_char)
2133
2164
      {
2134
2165
        ptr++;
2135
2166
        /* special characters that need escaping */
2136
2167
        if (*ptr == '\'' || *ptr == '"' || *ptr == '\\')
2137
 
          fixed_buffer.append(ptr, 1);
 
2168
          dynstr_append_mem(fixed_buffer, ptr, 1);
2138
2169
        else
2139
2170
          ptr--;
2140
2171
      }
2141
2172
      break;
2142
2173
    default:
2143
 
      fixed_buffer.append(ptr, 1);
 
2174
      dynstr_append_mem(fixed_buffer, ptr, 1);
2144
2175
    }
2145
2176
    ptr++;
2146
2177
  }
2147
2178
  if (total_lines > 1)
2148
 
    add_history(fixed_buffer.c_str());
 
2179
    add_history(fixed_buffer->str);
2149
2180
}
2150
2181
 
2151
2182
/*
2163
2194
  return 1;
2164
2195
}
2165
2196
 
2166
 
static void initialize_readline (char *name)
 
2197
static void initialize_readline (const char *name)
2167
2198
{
2168
2199
  /* Allow conditional parsing of the ~/.inputrc file. */
2169
 
  rl_readline_name= name;
 
2200
  rl_readline_name= (char *)name;
2170
2201
 
 
2202
#ifdef HAVE_READLINE_COMPLETION
2171
2203
  /* Tell the completer that we want a crack first. */
2172
 
  rl_attempted_completion_function= (rl_completion_func_t*)&mysql_completion;
 
2204
  rl_attempted_completion_function= (rl_completion_func_t*)&new_mysql_completion;
2173
2205
  rl_completion_entry_function= (rl_compentry_func_t*)&no_completion;
 
2206
#endif
2174
2207
}
2175
2208
 
2176
 
 
 
2209
#ifdef HAVE_READLINE_COMPLETION
2177
2210
/*
2178
2211
  Attempt to complete on the contents of TEXT.  START and END show the
2179
2212
  region of TEXT that contains the word to complete.  We can use the
2180
2213
  entire line in case we want to do some simple parsing.  Return the
2181
2214
  array of matches, or NULL if there aren't any.
2182
2215
*/
2183
 
char **mysql_completion (const char *text,
2184
 
                        int start __attribute__((unused)),
2185
 
                        int end __attribute__((unused)))
 
2216
 
 
2217
char **new_mysql_completion (const char *text,
 
2218
                             int start __attribute__((unused)),
 
2219
                             int end __attribute__((unused)))
2186
2220
{
2187
2221
  if (!status.batch && !quick)
2188
2222
    return rl_completion_matches(text, new_command_generator);
2190
2224
    return (char**) 0;
2191
2225
}
2192
2226
 
2193
 
 
2194
2227
static char *new_command_generator(const char *text,int state)
2195
2228
{
2196
2229
  static int textlen;
2203
2236
    textlen=(uint) strlen(text);
2204
2237
 
2205
2238
  if (textlen>0)
2206
 
  {            /* lookup in the hash */
 
2239
  {                                             /* lookup in the hash */
2207
2240
    if (!state)
2208
2241
    {
2209
2242
      uint len;
2210
2243
 
2211
2244
      b = find_all_matches(&ht,text,(uint) strlen(text),&len);
2212
2245
      if (!b)
2213
 
        return NULL;
 
2246
        return NullS;
2214
2247
      e = b->pData;
2215
2248
    }
2216
2249
 
2237
2270
        }
2238
2271
      }
2239
2272
    }
2240
 
    ptr= NULL;
 
2273
    ptr= NullS;
2241
2274
    while (e && !ptr)
2242
 
    {          /* find valid entry in bucket */
 
2275
    {                                   /* find valid entry in bucket */
2243
2276
      if ((uint) strlen(e->str) == b->nKeyLength)
2244
2277
        ptr = strdup(e->str);
2245
2278
      /* find the next used entry */
2266
2299
    if (ptr)
2267
2300
      return ptr;
2268
2301
  }
2269
 
  return NULL;
 
2302
  return NullS;
2270
2303
}
 
2304
#endif
2271
2305
 
2272
2306
 
2273
2307
/* Build up the completion hash */
2275
2309
static void build_completion_hash(bool rehash, bool write_info)
2276
2310
{
2277
2311
  COMMANDS *cmd=commands;
2278
 
  DRIZZLE_RES *databases=0,*tables=0;
2279
 
  DRIZZLE_RES *fields;
 
2312
  MYSQL_RES *databases=0,*tables=0;
 
2313
  MYSQL_RES *fields;
2280
2314
  static char ***field_names= 0;
2281
 
  DRIZZLE_ROW database_row,table_row;
2282
 
  DRIZZLE_FIELD *sql_field;
2283
 
  char buf[NAME_LEN*2+2];     // table name plus field name plus 2
 
2315
  MYSQL_ROW database_row,table_row;
 
2316
  MYSQL_FIELD *sql_field;
 
2317
  char buf[NAME_LEN*2+2];                // table name plus field name plus 2
2284
2318
  int i,j,num_fields;
2285
2319
 
2286
2320
 
2287
2321
  if (status.batch || quick || !current_db)
2288
 
    return;      // We don't need completion in batches
 
2322
    return;                     // We don't need completion in batches
2289
2323
  if (!rehash)
2290
2324
    return;
2291
2325
 
2304
2338
  /* hash Drizzle functions (to be implemented) */
2305
2339
 
2306
2340
  /* hash all database names */
2307
 
  if (drizzle_query(&drizzle,"show databases") == 0)
 
2341
  if (mysql_query(&mysql,"show databases") == 0)
2308
2342
  {
2309
 
    if (!(databases = drizzle_store_result(&drizzle)))
2310
 
      put_info(drizzle_error(&drizzle),INFO_INFO,0,0);
 
2343
    if (!(databases = mysql_store_result(&mysql)))
 
2344
      put_info(mysql_error(&mysql),INFO_INFO,0,0);
2311
2345
    else
2312
2346
    {
2313
 
      while ((database_row=drizzle_fetch_row(databases)))
 
2347
      while ((database_row=mysql_fetch_row(databases)))
2314
2348
      {
2315
2349
        char *str=strdup_root(&hash_mem_root, (char*) database_row[0]);
2316
2350
        if (str)
2317
2351
          add_word(&ht,(char*) str);
2318
2352
      }
2319
 
      drizzle_free_result(databases);
 
2353
      mysql_free_result(databases);
2320
2354
    }
2321
2355
  }
2322
2356
  /* hash all table names */
2323
 
  if (drizzle_query(&drizzle,"show tables")==0)
 
2357
  if (mysql_query(&mysql,"show tables")==0)
2324
2358
  {
2325
 
    if (!(tables = drizzle_store_result(&drizzle)))
2326
 
      put_info(drizzle_error(&drizzle),INFO_INFO,0,0);
 
2359
    if (!(tables = mysql_store_result(&mysql)))
 
2360
      put_info(mysql_error(&mysql),INFO_INFO,0,0);
2327
2361
    else
2328
2362
    {
2329
 
      if (drizzle_num_rows(tables) > 0 && !opt_silent && write_info)
 
2363
      if (mysql_num_rows(tables) > 0 && !opt_silent && write_info)
2330
2364
      {
2331
 
        tee_fprintf(stdout, _("\
 
2365
        tee_fprintf(stdout, "\
2332
2366
Reading table information for completion of table and column names\n    \
2333
 
You can turn off this feature to get a quicker startup with -A\n\n"));
 
2367
You can turn off this feature to get a quicker startup with -A\n\n");
2334
2368
      }
2335
 
      while ((table_row=drizzle_fetch_row(tables)))
 
2369
      while ((table_row=mysql_fetch_row(tables)))
2336
2370
      {
2337
2371
        char *str=strdup_root(&hash_mem_root, (char*) table_row[0]);
2338
2372
        if (str &&
2343
2377
  }
2344
2378
 
2345
2379
  /* hash all field names, both with the table prefix and without it */
2346
 
  if (!tables)          /* no tables */
 
2380
  if (!tables)                                  /* no tables */
2347
2381
  {
2348
2382
    return;
2349
2383
  }
2350
 
  drizzle_data_seek(tables,0);
 
2384
  mysql_data_seek(tables,0);
2351
2385
  if (!(field_names= (char ***) alloc_root(&hash_mem_root,sizeof(char **) *
2352
 
                                           (uint) (drizzle_num_rows(tables)+1))))
 
2386
                                           (uint) (mysql_num_rows(tables)+1))))
2353
2387
  {
2354
 
    drizzle_free_result(tables);
 
2388
    mysql_free_result(tables);
2355
2389
    return;
2356
2390
  }
2357
2391
  i=0;
2358
 
  while ((table_row=drizzle_fetch_row(tables)))
 
2392
  while ((table_row=mysql_fetch_row(tables)))
2359
2393
  {
2360
 
    if ((fields=drizzle_list_fields(&drizzle,(const char*) table_row[0],NULL)))
 
2394
    if ((fields=mysql_list_fields(&mysql,(const char*) table_row[0],NullS)))
2361
2395
    {
2362
 
      num_fields=drizzle_num_fields(fields);
 
2396
      num_fields=mysql_num_fields(fields);
2363
2397
      if (!(field_names[i] = (char **) alloc_root(&hash_mem_root,
2364
2398
                                                  sizeof(char *) *
2365
2399
                                                  (num_fields*2+1))))
2366
2400
      {
2367
 
        drizzle_free_result(fields);
 
2401
        mysql_free_result(fields);
2368
2402
        break;
2369
2403
      }
2370
2404
      field_names[i][num_fields*2]= '\0';
2371
2405
      j=0;
2372
 
      while ((sql_field=drizzle_fetch_field(fields)))
 
2406
      while ((sql_field=mysql_fetch_field(fields)))
2373
2407
      {
2374
2408
        sprintf(buf,"%.64s.%.64s",table_row[0],sql_field->name);
2375
2409
        field_names[i][j] = strdup_root(&hash_mem_root,buf);
2381
2415
          add_word(&ht,field_names[i][num_fields+j]);
2382
2416
        j++;
2383
2417
      }
2384
 
      drizzle_free_result(fields);
 
2418
      mysql_free_result(fields);
2385
2419
    }
2386
2420
    else
2387
2421
      field_names[i]= 0;
2388
2422
 
2389
2423
    i++;
2390
2424
  }
2391
 
  drizzle_free_result(tables);
2392
 
  field_names[i]=0;        // End pointer
 
2425
  mysql_free_result(tables);
 
2426
  field_names[i]=0;                             // End pointer
2393
2427
  return;
2394
2428
}
2395
2429
 
2404
2438
    for (;;)
2405
2439
    {
2406
2440
      if (*s == (char) c) return (char*) s;
2407
 
      if (!*s++) return NULL;
 
2441
      if (!*s++) return NullS;
2408
2442
    }
2409
2443
  }
2410
2444
 
2412
2446
  {
2413
2447
    register char *t;
2414
2448
 
2415
 
    t = NULL;
 
2449
    t = NullS;
2416
2450
    do if (*s == (char) c) t = (char*) s; while (*s++);
2417
2451
    return (char*) t;
2418
2452
  }
2425
2459
  /* purecov: begin tested */
2426
2460
  if (opt_reconnect)
2427
2461
  {
2428
 
    put_info(_("No connection. Trying to reconnect..."),INFO_INFO,0,0);
2429
 
    (void) com_connect((string *)0, 0);
 
2462
    put_info("No connection. Trying to reconnect...",INFO_INFO,0,0);
 
2463
    (void) com_connect((DYNAMIC_STRING *) 0, 0);
2430
2464
    if (opt_rehash)
2431
2465
      com_rehash(NULL, NULL);
2432
2466
  }
2433
2467
  if (!connected)
2434
 
    return put_info(_("Can't connect to the server\n"),INFO_ERROR,0,0);
 
2468
    return put_info("Can't connect to the server\n",INFO_ERROR,0,0);
2435
2469
  /* purecov: end */
2436
2470
  return 0;
2437
2471
}
2438
2472
 
2439
2473
static void get_current_db(void)
2440
2474
{
2441
 
  DRIZZLE_RES *res;
 
2475
  MYSQL_RES *res;
2442
2476
 
2443
 
  free(current_db);
 
2477
  my_free(current_db, MYF(MY_ALLOW_ZERO_PTR));
2444
2478
  current_db= NULL;
2445
2479
  /* In case of error below current_db will be NULL */
2446
 
  if (!drizzle_query(&drizzle, "SELECT DATABASE()") &&
2447
 
      (res= drizzle_use_result(&drizzle)))
 
2480
  if (!mysql_query(&mysql, "SELECT DATABASE()") &&
 
2481
      (res= mysql_use_result(&mysql)))
2448
2482
  {
2449
 
    DRIZZLE_ROW row= drizzle_fetch_row(res);
 
2483
    MYSQL_ROW row= mysql_fetch_row(res);
2450
2484
    if (row[0])
2451
2485
      current_db= strdup(row[0]);
2452
 
    drizzle_free_result(res);
 
2486
    mysql_free_result(res);
2453
2487
  }
2454
2488
}
2455
2489
 
2457
2491
 The different commands
2458
2492
***************************************************************************/
2459
2493
 
2460
 
int drizzle_real_query_for_lazy(const char *buf, int length)
 
2494
int mysql_real_query_for_lazy(const char *buf, int length)
2461
2495
{
2462
2496
  for (uint retry=0;; retry++)
2463
2497
  {
2464
2498
    int error;
2465
 
    if (!drizzle_real_query(&drizzle,buf,length))
 
2499
    if (!mysql_real_query(&mysql,buf,length))
2466
2500
      return 0;
2467
 
    error= put_error(&drizzle);
2468
 
    if (drizzle_errno(&drizzle) != CR_SERVER_GONE_ERROR || retry > 1 ||
 
2501
    error= put_error(&mysql);
 
2502
    if (mysql_errno(&mysql) != CR_SERVER_GONE_ERROR || retry > 1 ||
2469
2503
        !opt_reconnect)
2470
2504
      return error;
2471
2505
    if (reconnect())
2473
2507
  }
2474
2508
}
2475
2509
 
2476
 
int drizzle_store_result_for_lazy(DRIZZLE_RES **result)
 
2510
int mysql_store_result_for_lazy(MYSQL_RES **result)
2477
2511
{
2478
 
  if ((*result=drizzle_store_result(&drizzle)))
 
2512
  if ((*result=mysql_store_result(&mysql)))
2479
2513
    return 0;
2480
2514
 
2481
 
  if (drizzle_error(&drizzle)[0])
2482
 
    return put_error(&drizzle);
 
2515
  if (mysql_error(&mysql)[0])
 
2516
    return put_error(&mysql);
2483
2517
  return 0;
2484
2518
}
2485
2519
 
2486
 
static void print_help_item(DRIZZLE_ROW *cur, int num_name, int num_cat, char *last_char)
 
2520
static void print_help_item(MYSQL_ROW *cur, int num_name, int num_cat, char *last_char)
2487
2521
{
2488
2522
  char ccat= (*cur)[num_cat][0];
2489
2523
  if (*last_char != ccat)
2490
2524
  {
2491
 
    put_info(ccat == 'Y' ? _("categories:") : _("topics:"), INFO_INFO,0,0);
 
2525
    put_info(ccat == 'Y' ? "categories:" : "topics:", INFO_INFO,0,0);
2492
2526
    *last_char= ccat;
2493
2527
  }
2494
2528
  tee_fprintf(PAGER, "   %s\n", (*cur)[num_name]);
2495
2529
}
2496
2530
 
2497
2531
 
2498
 
static int com_server_help(string *buffer,
2499
 
                           const char *line __attribute__((unused)),
 
2532
static int com_server_help(DYNAMIC_STRING *buffer,
 
2533
                           char *line __attribute__((unused)),
2500
2534
                           char *help_arg)
2501
2535
{
2502
 
  DRIZZLE_ROW cur;
2503
 
  const char *server_cmd= buffer->c_str();
 
2536
  MYSQL_ROW cur;
 
2537
  const char *server_cmd= buffer->str;
2504
2538
  char cmd_buf[100];
2505
 
  DRIZZLE_RES *result;
 
2539
  MYSQL_RES *result;
2506
2540
  int error;
2507
2541
 
2508
2542
  if (help_arg[0] != '\'')
2509
2543
  {
2510
 
    char *end_arg= strchr(help_arg, '\0');
 
2544
    char *end_arg= strend(help_arg);
2511
2545
    if(--end_arg)
2512
2546
    {
2513
2547
      while (my_isspace(charset_info,*end_arg))
2514
2548
        end_arg--;
2515
2549
      *++end_arg= '\0';
2516
2550
    }
2517
 
    (void) strxnmov(cmd_buf, sizeof(cmd_buf), "help '", help_arg, "'", NULL);
 
2551
    (void) strxnmov(cmd_buf, sizeof(cmd_buf), "help '", help_arg, "'", NullS);
2518
2552
    server_cmd= cmd_buf;
2519
2553
  }
2520
2554
 
2521
2555
  if (!connected && reconnect())
2522
2556
    return 1;
2523
2557
 
2524
 
  if ((error= drizzle_real_query_for_lazy(server_cmd,(int)strlen(server_cmd))) ||
2525
 
      (error= drizzle_store_result_for_lazy(&result)))
 
2558
  if ((error= mysql_real_query_for_lazy(server_cmd,(int)strlen(server_cmd))) ||
 
2559
      (error= mysql_store_result_for_lazy(&result)))
2526
2560
    return error;
2527
2561
 
2528
2562
  if (result)
2529
2563
  {
2530
 
    unsigned int num_fields= drizzle_num_fields(result);
2531
 
    uint64_t num_rows= drizzle_num_rows(result);
2532
 
    drizzle_fetch_fields(result);
 
2564
    unsigned int num_fields= mysql_num_fields(result);
 
2565
    uint64_t num_rows= mysql_num_rows(result);
 
2566
    mysql_fetch_fields(result);
2533
2567
    if (num_fields==3 && num_rows==1)
2534
2568
    {
2535
 
      if (!(cur= drizzle_fetch_row(result)))
 
2569
      if (!(cur= mysql_fetch_row(result)))
2536
2570
      {
2537
2571
        error= -1;
2538
2572
        goto err;
2539
2573
      }
2540
2574
 
2541
2575
      init_pager();
2542
 
      tee_fprintf(PAGER,   _("Name: \'%s\'\n"), cur[0]);
2543
 
      tee_fprintf(PAGER,   _("Description:\n%s"), cur[1]);
 
2576
      tee_fprintf(PAGER,   "Name: \'%s\'\n", cur[0]);
 
2577
      tee_fprintf(PAGER,   "Description:\n%s", cur[1]);
2544
2578
      if (cur[2] && *((char*)cur[2]))
2545
 
        tee_fprintf(PAGER, _("Examples:\n%s"), cur[2]);
 
2579
        tee_fprintf(PAGER, "Examples:\n%s", cur[2]);
2546
2580
      tee_fprintf(PAGER,   "\n");
2547
2581
      end_pager();
2548
2582
    }
2555
2589
 
2556
2590
      if (num_fields == 2)
2557
2591
      {
2558
 
        put_info(_("Many help items for your request exist."), INFO_INFO,0,0);
2559
 
        put_info(_("To make a more specific request, please type 'help <item>',\nwhere <item> is one of the following"), INFO_INFO,0,0);
 
2592
        put_info("Many help items for your request exist.", INFO_INFO,0,0);
 
2593
        put_info("To make a more specific request, please type 'help <item>',\nwhere <item> is one of the following", INFO_INFO,0,0);
2560
2594
        num_name= 0;
2561
2595
        num_cat= 1;
2562
2596
      }
2563
 
      else if ((cur= drizzle_fetch_row(result)))
 
2597
      else if ((cur= mysql_fetch_row(result)))
2564
2598
      {
2565
 
        tee_fprintf(PAGER, _("You asked for help about help category: '%s'\n"), cur[0]);
2566
 
        put_info(_("For more information, type 'help <item>', where <item> is one of the following"), INFO_INFO,0,0);
 
2599
        tee_fprintf(PAGER, "You asked for help about help category: \"%s\"\n", cur[0]);
 
2600
        put_info("For more information, type 'help <item>', where <item> is one of the following", INFO_INFO,0,0);
2567
2601
        num_name= 1;
2568
2602
        num_cat= 2;
2569
2603
        print_help_item(&cur,1,2,&last_char);
2570
2604
      }
2571
2605
 
2572
 
      while ((cur= drizzle_fetch_row(result)))
 
2606
      while ((cur= mysql_fetch_row(result)))
2573
2607
        print_help_item(&cur,num_name,num_cat,&last_char);
2574
2608
      tee_fprintf(PAGER, "\n");
2575
2609
      end_pager();
2576
2610
    }
2577
2611
    else
2578
2612
    {
2579
 
      put_info(_("\nNothing found"), INFO_INFO,0,0);
2580
 
      put_info(_("Please try to run 'help contents' for a list of all accessible topics\n"), INFO_INFO,0,0);
 
2613
      put_info("\nNothing found", INFO_INFO,0,0);
 
2614
      put_info("Please try to run 'help contents' for a list of all accessible topics\n", INFO_INFO,0,0);
2581
2615
    }
2582
2616
  }
2583
2617
 
2584
2618
err:
2585
 
  drizzle_free_result(result);
 
2619
  mysql_free_result(result);
2586
2620
  return error;
2587
2621
}
2588
2622
 
2589
2623
static int
2590
 
com_help(string *buffer __attribute__((unused)),
2591
 
         const char *line __attribute__((unused)))
 
2624
com_help(DYNAMIC_STRING *buffer __attribute__((unused)),
 
2625
         char *line __attribute__((unused)))
2592
2626
{
2593
2627
  register int i, j;
2594
2628
  char * help_arg= strchr(line,' '), buff[32], *end;
2596
2630
  {
2597
2631
    while (my_isspace(charset_info,*help_arg))
2598
2632
      help_arg++;
2599
 
    if (*help_arg) return com_server_help(buffer,line,help_arg);
 
2633
    if (*help_arg)
 
2634
      return com_server_help(buffer,line,help_arg);
2600
2635
  }
2601
2636
 
2602
 
  put_info(_("List of all Drizzle commands:"), INFO_INFO,0,0);
 
2637
  put_info("List of all Drizzle commands:", INFO_INFO,0,0);
2603
2638
  if (!named_cmds)
2604
 
    put_info(_("Note that all text commands must be first on line and end with ';'"),INFO_INFO,0,0);
 
2639
    put_info("Note that all text commands must be first on line and end with ';'",INFO_INFO,0,0);
2605
2640
  for (i = 0; commands[i].name; i++)
2606
2641
  {
2607
 
    end= my_stpcpy(buff, commands[i].name);
 
2642
    end= strmov(buff, commands[i].name);
2608
2643
    for (j= (int)strlen(commands[i].name); j < 10; j++)
2609
 
      end= my_stpcpy(end, " ");
 
2644
      end= strmov(end, " ");
2610
2645
    if (commands[i].func)
2611
2646
      tee_fprintf(stdout, "%s(\\%c) %s\n", buff,
2612
 
                  commands[i].cmd_char, _(commands[i].doc));
 
2647
                  commands[i].cmd_char, commands[i].doc);
2613
2648
  }
2614
 
  if (connected && drizzle_get_server_version(&drizzle) >= 40100)
2615
 
    put_info(_("\nFor server side help, type 'help contents'\n"), INFO_INFO,0,0);
 
2649
  if (connected && mysql_get_server_version(&mysql) >= 40100)
 
2650
    put_info("\nFor server side help, type 'help contents'\n", INFO_INFO,0,0);
2616
2651
  return 0;
2617
2652
}
2618
2653
 
2619
2654
 
2620
2655
static int
2621
 
com_clear(string *buffer,
2622
 
          const char *line __attribute__((unused)))
 
2656
com_clear(DYNAMIC_STRING *buffer,char *line __attribute__((unused)))
2623
2657
{
2624
2658
  if (status.add_to_history)
2625
2659
    fix_history(buffer);
2626
 
  buffer->clear();
 
2660
  dynstr_set(buffer, NULL);
2627
2661
  return 0;
2628
2662
}
2629
2663
 
 
2664
static int
 
2665
com_charset(DYNAMIC_STRING *buffer __attribute__((unused)), char *line)
 
2666
{
 
2667
  char buff[256], *param;
 
2668
  CHARSET_INFO * new_cs;
 
2669
  strmake(buff, line, sizeof(buff) - 1);
 
2670
  param= get_arg(buff, 0);
 
2671
  if (!param || !*param)
 
2672
  {
 
2673
    return put_info("Usage: \\C char_setname | charset charset_name",
 
2674
                    INFO_ERROR, 0, 0);
 
2675
  }
 
2676
  new_cs= get_charset_by_csname(param, MY_CS_PRIMARY, MYF(MY_WME));
 
2677
  if (new_cs)
 
2678
  {
 
2679
    charset_info= new_cs;
 
2680
    mysql_set_character_set(&mysql, charset_info->csname);
 
2681
    default_charset= (char *)charset_info->csname;
 
2682
    default_charset_used= 1;
 
2683
    put_info("Charset changed", INFO_INFO,0,0);
 
2684
  }
 
2685
  else put_info("Charset is not found", INFO_INFO,0,0);
 
2686
  return 0;
 
2687
}
2630
2688
 
2631
2689
/*
2632
2690
  Execute command
2635
2693
  1  if fatal error
2636
2694
*/
2637
2695
static int
2638
 
com_go(string *buffer,
2639
 
       const char *line __attribute__((unused)))
 
2696
com_go(DYNAMIC_STRING *buffer,
 
2697
       char *line __attribute__((unused)))
2640
2698
{
2641
2699
  char          buff[200]; /* about 110 chars used so far */
2642
2700
  char          time_buff[52+3+1]; /* time max + space&parens + NUL */
2643
 
  DRIZZLE_RES     *result;
2644
 
  uint32_t         timer, warnings= 0;
 
2701
  MYSQL_RES     *result;
 
2702
  ulong         timer, warnings= 0;
2645
2703
  uint          error= 0;
2646
2704
  int           err= 0;
2647
2705
 
2650
2708
  /* Remove garbage for nicer messages */
2651
2709
  remove_cntrl(buffer);
2652
2710
 
2653
 
  if (buffer->empty())
 
2711
  if (buffer->length == 0)
2654
2712
  {
2655
2713
    // Ignore empty quries
2656
2714
    if (status.batch)
2657
2715
      return 0;
2658
 
    return put_info(_("No query specified\n"),INFO_ERROR,0,0);
 
2716
    return put_info("No query specified\n",INFO_ERROR,0,0);
2659
2717
 
2660
2718
  }
2661
2719
  if (!connected && reconnect())
2662
2720
  {
2663
2721
    // Remove query on error
2664
 
    buffer->clear();
 
2722
    dynstr_set(buffer, NULL);
2665
2723
    return opt_reconnect ? -1 : 1;          // Fatal error
2666
2724
  }
2667
2725
  if (verbose)
2668
2726
    (void) com_print(buffer, 0);
2669
2727
 
2670
2728
  if (skip_updates &&
2671
 
      ((buffer->length() < 4) || (buffer->find( "SET ") != 0)))
 
2729
      ((buffer->length < 4) || !strncmp(buffer->str, "SET ", 4)))
2672
2730
  {
2673
 
    (void) put_info(_("Ignoring query to other database"),INFO_INFO,0,0);
 
2731
    (void) put_info("Ignoring query to other database",INFO_INFO,0,0);
2674
2732
    return 0;
2675
2733
  }
2676
2734
 
2677
2735
  timer=start_timer();
2678
2736
  executing_query= 1;
2679
 
  error= drizzle_real_query_for_lazy(buffer->c_str(),buffer->length());
 
2737
  error= mysql_real_query_for_lazy(buffer->str,buffer->length);
2680
2738
 
2681
2739
  if (status.add_to_history)
2682
2740
  {
2683
 
    buffer->append(vertical ? "\\G" : delimiter);
 
2741
    dynstr_append(buffer, vertical ? "\\G" : delimiter);
2684
2742
    /* Append final command onto history */
2685
2743
    fix_history(buffer);
2686
2744
  }
2687
2745
 
2688
 
  buffer->clear();
 
2746
  dynstr_set(buffer, NULL);
2689
2747
 
2690
2748
  if (error)
2691
2749
    goto end;
2696
2754
 
2697
2755
    if (quick)
2698
2756
    {
2699
 
      if (!(result=drizzle_use_result(&drizzle)) && drizzle_field_count(&drizzle))
 
2757
      if (!(result=mysql_use_result(&mysql)) && mysql_field_count(&mysql))
2700
2758
      {
2701
 
        error= put_error(&drizzle);
 
2759
        error= put_error(&mysql);
2702
2760
        goto end;
2703
2761
      }
2704
2762
    }
2705
2763
    else
2706
2764
    {
2707
 
      error= drizzle_store_result_for_lazy(&result);
 
2765
      error= mysql_store_result_for_lazy(&result);
2708
2766
      if (error)
2709
2767
        goto end;
2710
2768
    }
2711
2769
 
2712
2770
    if (verbose >= 3 || !opt_silent)
2713
 
      drizzle_end_timer(timer,time_buff);
 
2771
      mysql_end_timer(timer,time_buff);
2714
2772
    else
2715
2773
      time_buff[0]= '\0';
2716
2774
 
2717
2775
    /* Every branch must truncate  buff . */
2718
2776
    if (result)
2719
2777
    {
2720
 
      if (!drizzle_num_rows(result) && ! quick && !column_types_flag)
 
2778
      if (!mysql_num_rows(result) && ! quick && !column_types_flag)
2721
2779
      {
2722
 
        my_stpcpy(buff, _("Empty set"));
 
2780
        strmov(buff, "Empty set");
 
2781
        if (opt_xml)
 
2782
        {
 
2783
          /*
 
2784
            We must print XML header and footer
 
2785
            to produce a well-formed XML even if
 
2786
            the result set is empty (Bug#27608).
 
2787
          */
 
2788
          init_pager();
 
2789
          print_table_data_xml(result);
 
2790
          end_pager();
 
2791
        }
2723
2792
      }
2724
2793
      else
2725
2794
      {
2726
2795
        init_pager();
2727
 
        if (vertical || (auto_vertical_output &&
2728
 
                         (terminal_width < get_result_width(result))))
 
2796
        if (opt_html)
 
2797
          print_table_data_html(result);
 
2798
        else if (opt_xml)
 
2799
          print_table_data_xml(result);
 
2800
        else if (vertical || (auto_vertical_output && (terminal_width < get_result_width(result))))
2729
2801
          print_table_data_vertically(result);
2730
2802
        else if (opt_silent && verbose <= 2 && !output_tables)
2731
2803
          print_tab_data(result);
2732
2804
        else
2733
2805
          print_table_data(result);
2734
 
        sprintf(buff,
2735
 
                ngettext("%ld row in set","%ld rows in set",
2736
 
                         (long) drizzle_num_rows(result)),
2737
 
                (long) drizzle_num_rows(result));
 
2806
        sprintf(buff,"%ld %s in set",
 
2807
                (long) mysql_num_rows(result),
 
2808
                (long) mysql_num_rows(result) == 1 ? "row" : "rows");
2738
2809
        end_pager();
2739
 
        if (drizzle_errno(&drizzle))
2740
 
          error= put_error(&drizzle);
 
2810
        if (mysql_errno(&mysql))
 
2811
          error= put_error(&mysql);
2741
2812
      }
2742
2813
    }
2743
 
    else if (drizzle_affected_rows(&drizzle) == ~(uint64_t) 0)
2744
 
      my_stpcpy(buff,_("Query OK"));
 
2814
    else if (mysql_affected_rows(&mysql) == ~(uint64_t) 0)
 
2815
      strmov(buff,"Query OK");
2745
2816
    else
2746
 
      sprintf(buff, ngettext("Query OK, %ld row affected",
2747
 
                             "Query OK, %ld rows affected",
2748
 
                             (long) drizzle_affected_rows(&drizzle)),
2749
 
              (long) drizzle_affected_rows(&drizzle));
 
2817
      sprintf(buff,"Query OK, %ld %s affected",
 
2818
              (long) mysql_affected_rows(&mysql),
 
2819
              (long) mysql_affected_rows(&mysql) == 1 ? "row" : "rows");
2750
2820
 
2751
 
    pos= strchr(buff, '\0');
2752
 
    if ((warnings= drizzle_warning_count(&drizzle)))
 
2821
    pos=strend(buff);
 
2822
    if ((warnings= mysql_warning_count(&mysql)))
2753
2823
    {
2754
2824
      *pos++= ',';
2755
2825
      *pos++= ' ';
2756
2826
      pos=int10_to_str(warnings, pos, 10);
2757
 
      pos=my_stpcpy(pos, " warning");
 
2827
      pos=strmov(pos, " warning");
2758
2828
      if (warnings != 1)
2759
2829
        *pos++= 's';
2760
2830
    }
2761
 
    my_stpcpy(pos, time_buff);
 
2831
    strmov(pos, time_buff);
2762
2832
    put_info(buff,INFO_RESULT,0,0);
2763
 
    if (drizzle_info(&drizzle))
2764
 
      put_info(drizzle_info(&drizzle),INFO_RESULT,0,0);
2765
 
    put_info("",INFO_RESULT,0,0);      // Empty row
 
2833
    if (mysql_info(&mysql))
 
2834
      put_info(mysql_info(&mysql),INFO_RESULT,0,0);
 
2835
    put_info("",INFO_RESULT,0,0);                       // Empty row
2766
2836
 
2767
 
    if (result && !drizzle_eof(result))  /* Something wrong when using quick */
2768
 
      error= put_error(&drizzle);
 
2837
    if (result && !mysql_eof(result))   /* Something wrong when using quick */
 
2838
      error= put_error(&mysql);
2769
2839
    else if (unbuffered)
2770
2840
      fflush(stdout);
2771
 
    drizzle_free_result(result);
2772
 
  } while (!(err= drizzle_next_result(&drizzle)));
 
2841
    mysql_free_result(result);
 
2842
  } while (!(err= mysql_next_result(&mysql)));
2773
2843
  if (err >= 1)
2774
 
    error= put_error(&drizzle);
 
2844
    error= put_error(&mysql);
2775
2845
 
2776
2846
end:
2777
2847
 
2780
2850
    print_warnings();
2781
2851
 
2782
2852
  if (!error && !status.batch &&
2783
 
      (drizzle.server_status & SERVER_STATUS_DB_DROPPED))
 
2853
      (mysql.server_status & SERVER_STATUS_DB_DROPPED))
2784
2854
    get_current_db();
2785
2855
 
2786
2856
  executing_query= 0;
2787
 
  return error;        /* New command follows */
 
2857
  return error;                         /* New command follows */
2788
2858
}
2789
2859
 
2790
2860
 
2837
2907
 
2838
2908
 
2839
2909
static int
2840
 
com_ego(string *buffer,const char *line)
 
2910
com_ego(DYNAMIC_STRING *buffer,char *line)
2841
2911
{
2842
2912
  int result;
2843
2913
  bool oldvertical=vertical;
2851
2921
static const char *fieldtype2str(enum enum_field_types type)
2852
2922
{
2853
2923
  switch (type) {
2854
 
    case DRIZZLE_TYPE_BLOB:        return "BLOB";
2855
 
    case DRIZZLE_TYPE_NEWDATE:        return "DATE";
2856
 
    case DRIZZLE_TYPE_DATETIME:    return "DATETIME";
2857
 
    case DRIZZLE_TYPE_NEWDECIMAL:  return "DECIMAL";
2858
 
    case DRIZZLE_TYPE_DOUBLE:      return "DOUBLE";
2859
 
    case DRIZZLE_TYPE_ENUM:        return "ENUM";
2860
 
    case DRIZZLE_TYPE_LONG:        return "LONG";
2861
 
    case DRIZZLE_TYPE_LONGLONG:    return "LONGLONG";
2862
 
    case DRIZZLE_TYPE_NULL:        return "NULL";
2863
 
    case DRIZZLE_TYPE_TIME:        return "TIME";
2864
 
    case DRIZZLE_TYPE_TIMESTAMP:   return "TIMESTAMP";
2865
 
    case DRIZZLE_TYPE_TINY:        return "TINY";
2866
 
    case DRIZZLE_TYPE_VIRTUAL:     return "VIRTUAL";
 
2924
    case MYSQL_TYPE_BLOB:        return "BLOB";
 
2925
    case MYSQL_TYPE_NEWDATE:        return "DATE";
 
2926
    case MYSQL_TYPE_DATETIME:    return "DATETIME";
 
2927
    case MYSQL_TYPE_NEWDECIMAL:  return "DECIMAL";
 
2928
    case MYSQL_TYPE_DOUBLE:      return "DOUBLE";
 
2929
    case MYSQL_TYPE_ENUM:        return "ENUM";
 
2930
    case MYSQL_TYPE_LONG:        return "LONG";
 
2931
    case MYSQL_TYPE_LONGLONG:    return "LONGLONG";
 
2932
    case MYSQL_TYPE_NULL:        return "NULL";
 
2933
    case MYSQL_TYPE_SET:         return "SET";
 
2934
    case MYSQL_TYPE_SHORT:       return "SHORT";
 
2935
    case MYSQL_TYPE_STRING:      return "STRING";
 
2936
    case MYSQL_TYPE_TIME:        return "TIME";
 
2937
    case MYSQL_TYPE_TIMESTAMP:   return "TIMESTAMP";
 
2938
    case MYSQL_TYPE_TINY:        return "TINY";
 
2939
    case MYSQL_TYPE_VAR_STRING:  return "VAR_STRING";
 
2940
    case MYSQL_TYPE_YEAR:        return "YEAR";
2867
2941
    default:                     return "?-unknown-?";
2868
2942
  }
2869
2943
}
2873
2947
  char *s=buf;
2874
2948
  *s=0;
2875
2949
#define ff2s_check_flag(X)                                              \
2876
 
  if (f & X ## _FLAG) { s=my_stpcpy(s, # X " "); f &= ~ X ## _FLAG; }
 
2950
  if (f & X ## _FLAG) { s=strmov(s, # X " "); f &= ~ X ## _FLAG; }
2877
2951
  ff2s_check_flag(NOT_NULL);
2878
2952
  ff2s_check_flag(PRI_KEY);
2879
2953
  ff2s_check_flag(UNIQUE_KEY);
2880
2954
  ff2s_check_flag(MULTIPLE_KEY);
2881
2955
  ff2s_check_flag(BLOB);
2882
2956
  ff2s_check_flag(UNSIGNED);
 
2957
  ff2s_check_flag(ZEROFILL);
2883
2958
  ff2s_check_flag(BINARY);
2884
2959
  ff2s_check_flag(ENUM);
2885
2960
  ff2s_check_flag(AUTO_INCREMENT);
2899
2974
}
2900
2975
 
2901
2976
static void
2902
 
print_field_types(DRIZZLE_RES *result)
 
2977
print_field_types(MYSQL_RES *result)
2903
2978
{
2904
 
  DRIZZLE_FIELD   *field;
 
2979
  MYSQL_FIELD   *field;
2905
2980
  uint i=0;
2906
2981
 
2907
 
  while ((field = drizzle_fetch_field(result)))
 
2982
  while ((field = mysql_fetch_field(result)))
2908
2983
  {
2909
2984
    tee_fprintf(PAGER, "Field %3u:  `%s`\n"
2910
2985
                "Catalog:    `%s`\n"
2929
3004
 
2930
3005
 
2931
3006
static void
2932
 
print_table_data(DRIZZLE_RES *result)
 
3007
print_table_data(MYSQL_RES *result)
2933
3008
{
2934
 
  DRIZZLE_ROW     cur;
2935
 
  DRIZZLE_FIELD   *field;
 
3009
  MYSQL_ROW     cur;
 
3010
  MYSQL_FIELD   *field;
2936
3011
  bool          *num_flag;
2937
 
  string separator;
2938
 
  
2939
 
  separator.reserve(256);
 
3012
  DYNAMIC_STRING *separator=
 
3013
    (DYNAMIC_STRING *)my_malloc(sizeof(DYNAMIC_STRING), MYF(0));
 
3014
  init_dynamic_string(separator, "", 256, 256);
2940
3015
 
2941
 
  num_flag=(bool*) my_malloc(sizeof(bool)*drizzle_num_fields(result),
 
3016
  num_flag=(bool*) my_malloc(sizeof(bool)*mysql_num_fields(result),
2942
3017
                             MYF(MY_WME));
2943
3018
  if (column_types_flag)
2944
3019
  {
2945
3020
    print_field_types(result);
2946
 
    if (!drizzle_num_rows(result))
 
3021
    if (!mysql_num_rows(result))
2947
3022
      return;
2948
 
    drizzle_field_seek(result,0);
 
3023
    mysql_field_seek(result,0);
2949
3024
  }
2950
 
  separator.append("+");
2951
 
  while ((field = drizzle_fetch_field(result)))
 
3025
  dynstr_append(separator, "+");
 
3026
  while ((field = mysql_fetch_field(result)))
2952
3027
  {
2953
 
    uint32_t length= column_names ? field->name_length : 0;
 
3028
    uint length= column_names ? field->name_length : 0;
2954
3029
    if (quick)
2955
3030
      length=max(length,field->length);
2956
3031
    else
2957
3032
      length=max(length,field->max_length);
2958
 
    if (length < 4 && !(field->flags & NOT_NULL_FLAG))
 
3033
    if (length < 4 && !IS_NOT_NULL(field->flags))
2959
3034
      // Room for "NULL"
2960
3035
      length=4;
2961
3036
    field->max_length=length;
2962
3037
    uint x;
2963
3038
    for (x=0; x< (length+2); x++)
2964
 
      separator.append("-");
2965
 
    separator.append("+");
 
3039
      dynstr_append(separator, "-");
 
3040
    dynstr_append(separator, "+");
2966
3041
  }
2967
3042
 
2968
 
  tee_puts((char*) separator.c_str(), PAGER);
 
3043
  tee_puts((char*) separator->str, PAGER);
2969
3044
  if (column_names)
2970
3045
  {
2971
 
    drizzle_field_seek(result,0);
 
3046
    mysql_field_seek(result,0);
2972
3047
    (void) tee_fputs("|", PAGER);
2973
 
    for (uint off=0; (field = drizzle_fetch_field(result)) ; off++)
 
3048
    for (uint off=0; (field = mysql_fetch_field(result)) ; off++)
2974
3049
    {
2975
3050
      uint name_length= (uint) strlen(field->name);
2976
3051
      uint numcells= charset_info->cset->numcells(charset_info,
2977
3052
                                                  field->name,
2978
3053
                                                  field->name + name_length);
2979
 
      uint32_t display_length= field->max_length + name_length - numcells;
 
3054
      uint display_length= field->max_length + name_length - numcells;
2980
3055
      tee_fprintf(PAGER, " %-*s |",(int) min(display_length,
2981
3056
                                             MAX_COLUMN_LENGTH),
2982
3057
                  field->name);
2983
 
      num_flag[off]= ((field->type <= DRIZZLE_TYPE_LONGLONG) || 
2984
 
                      (field->type == DRIZZLE_TYPE_NEWDECIMAL));
 
3058
      num_flag[off]= IS_NUM(field->type);
2985
3059
    }
2986
3060
    (void) tee_fputs("\n", PAGER);
2987
 
    tee_puts((char*) separator.c_str(), PAGER);
 
3061
    tee_puts((char*) separator->str, PAGER);
2988
3062
  }
2989
3063
 
2990
 
  while ((cur= drizzle_fetch_row(result)))
 
3064
  while ((cur= mysql_fetch_row(result)))
2991
3065
  {
2992
3066
    if (interrupted_query)
2993
3067
      break;
2994
 
    uint32_t *lengths= drizzle_fetch_lengths(result);
 
3068
    uint32_t *lengths= mysql_fetch_lengths(result);
2995
3069
    (void) tee_fputs("| ", PAGER);
2996
 
    drizzle_field_seek(result, 0);
2997
 
    for (uint off= 0; off < drizzle_num_fields(result); off++)
 
3070
    mysql_field_seek(result, 0);
 
3071
    for (uint off= 0; off < mysql_num_fields(result); off++)
2998
3072
    {
2999
3073
      const char *buffer;
3000
3074
      uint data_length;
3013
3087
        data_length= (uint) lengths[off];
3014
3088
      }
3015
3089
 
3016
 
      field= drizzle_fetch_field(result);
 
3090
      field= mysql_fetch_field(result);
3017
3091
      field_max_length= field->max_length;
3018
3092
 
3019
3093
      /*
3041
3115
    }
3042
3116
    (void) tee_fputs("\n", PAGER);
3043
3117
  }
3044
 
  tee_puts(separator.c_str(), PAGER);
3045
 
  free(num_flag);
 
3118
  tee_puts(separator->str, PAGER);
 
3119
  my_free(num_flag, MYF(MY_ALLOW_ZERO_PTR));
3046
3120
}
3047
3121
 
3048
3122
/**
3061
3135
 
3062
3136
   @returns  number of character positions to be used, at most
3063
3137
*/
3064
 
static int get_field_disp_length(DRIZZLE_FIELD *field)
 
3138
static int get_field_disp_length(MYSQL_FIELD *field)
3065
3139
{
3066
3140
  uint length= column_names ? field->name_length : 0;
3067
3141
 
3070
3144
  else
3071
3145
    length= max(length, field->max_length);
3072
3146
 
3073
 
  if (length < 4 && !(field->flags & NOT_NULL_FLAG))
3074
 
    length= 4;        /* Room for "NULL" */
 
3147
  if (length < 4 && !IS_NOT_NULL(field->flags))
 
3148
    length= 4;                          /* Room for "NULL" */
3075
3149
 
3076
3150
  return length;
3077
3151
}
3084
3158
 
3085
3159
   @returns  The max number of characters in any row of this result
3086
3160
*/
3087
 
static int get_result_width(DRIZZLE_RES *result)
 
3161
static int get_result_width(MYSQL_RES *result)
3088
3162
{
3089
3163
  unsigned int len= 0;
3090
 
  DRIZZLE_FIELD *field;
3091
 
  DRIZZLE_FIELD_OFFSET offset;
 
3164
  MYSQL_FIELD *field;
 
3165
  MYSQL_FIELD_OFFSET offset;
3092
3166
 
3093
 
  offset= drizzle_field_tell(result);
 
3167
  offset= mysql_field_tell(result);
3094
3168
  assert(offset == 0);
3095
3169
 
3096
 
  while ((field= drizzle_fetch_field(result)) != NULL)
 
3170
  while ((field= mysql_fetch_field(result)) != NULL)
3097
3171
    len+= get_field_disp_length(field) + 3; /* plus bar, space, & final space */
3098
3172
 
3099
 
  (void) drizzle_field_seek(result, offset);
 
3173
  (void) mysql_field_seek(result, offset);
3100
3174
 
3101
3175
  return len + 1; /* plus final bar. */
3102
3176
}
3132
3206
 
3133
3207
 
3134
3208
static void
3135
 
print_table_data_vertically(DRIZZLE_RES *result)
3136
 
{
3137
 
  DRIZZLE_ROW  cur;
3138
 
  uint    max_length=0;
3139
 
  DRIZZLE_FIELD  *field;
3140
 
 
3141
 
  while ((field = drizzle_fetch_field(result)))
 
3209
print_table_data_html(MYSQL_RES *result)
 
3210
{
 
3211
  MYSQL_ROW     cur;
 
3212
  MYSQL_FIELD   *field;
 
3213
 
 
3214
  mysql_field_seek(result,0);
 
3215
  (void) tee_fputs("<TABLE BORDER=1><TR>", PAGER);
 
3216
  if (column_names)
 
3217
  {
 
3218
    while((field = mysql_fetch_field(result)))
 
3219
    {
 
3220
      tee_fprintf(PAGER, "<TH>%s</TH>", (field->name ?
 
3221
                                         (field->name[0] ? field->name :
 
3222
                                          " &nbsp; ") : "NULL"));
 
3223
    }
 
3224
    (void) tee_fputs("</TR>", PAGER);
 
3225
  }
 
3226
  while ((cur = mysql_fetch_row(result)))
 
3227
  {
 
3228
    if (interrupted_query)
 
3229
      break;
 
3230
    uint32_t *lengths=mysql_fetch_lengths(result);
 
3231
    (void) tee_fputs("<TR>", PAGER);
 
3232
    for (uint32_t i= 0; i < mysql_num_fields(result); i++)
 
3233
    {
 
3234
      (void) tee_fputs("<TD>", PAGER);
 
3235
      safe_put_field(cur[i],lengths[i]);
 
3236
      (void) tee_fputs("</TD>", PAGER);
 
3237
    }
 
3238
    (void) tee_fputs("</TR>", PAGER);
 
3239
  }
 
3240
  (void) tee_fputs("</TABLE>", PAGER);
 
3241
}
 
3242
 
 
3243
 
 
3244
static void
 
3245
print_table_data_xml(MYSQL_RES *result)
 
3246
{
 
3247
  MYSQL_ROW   cur;
 
3248
  MYSQL_FIELD *fields;
 
3249
 
 
3250
  mysql_field_seek(result,0);
 
3251
 
 
3252
  tee_fputs("<?xml version=\"1.0\"?>\n\n<resultset statement=\"", PAGER);
 
3253
  xmlencode_print(glob_buffer->str, glob_buffer->length);
 
3254
  tee_fputs("\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">",
 
3255
            PAGER);
 
3256
 
 
3257
  fields = mysql_fetch_fields(result);
 
3258
  while ((cur = mysql_fetch_row(result)))
 
3259
  {
 
3260
    if (interrupted_query)
 
3261
      break;
 
3262
    uint32_t *lengths=mysql_fetch_lengths(result);
 
3263
    (void) tee_fputs("\n  <row>\n", PAGER);
 
3264
    for (uint i=0; i < mysql_num_fields(result); i++)
 
3265
    {
 
3266
      tee_fprintf(PAGER, "\t<field name=\"");
 
3267
      xmlencode_print(fields[i].name, (uint) strlen(fields[i].name));
 
3268
      if (cur[i])
 
3269
      {
 
3270
        tee_fprintf(PAGER, "\">");
 
3271
        xmlencode_print(cur[i], lengths[i]);
 
3272
        tee_fprintf(PAGER, "</field>\n");
 
3273
      }
 
3274
      else
 
3275
        tee_fprintf(PAGER, "\" xsi:nil=\"true\" />\n");
 
3276
    }
 
3277
    (void) tee_fputs("  </row>\n", PAGER);
 
3278
  }
 
3279
  (void) tee_fputs("</resultset>\n", PAGER);
 
3280
}
 
3281
 
 
3282
 
 
3283
static void
 
3284
print_table_data_vertically(MYSQL_RES *result)
 
3285
{
 
3286
  MYSQL_ROW     cur;
 
3287
  uint          max_length=0;
 
3288
  MYSQL_FIELD   *field;
 
3289
 
 
3290
  while ((field = mysql_fetch_field(result)))
3142
3291
  {
3143
3292
    uint length= field->name_length;
3144
3293
    if (length > max_length)
3146
3295
    field->max_length=length;
3147
3296
  }
3148
3297
 
3149
 
  drizzle_field_seek(result,0);
3150
 
  for (uint row_count=1; (cur= drizzle_fetch_row(result)); row_count++)
 
3298
  mysql_field_seek(result,0);
 
3299
  for (uint row_count=1; (cur= mysql_fetch_row(result)); row_count++)
3151
3300
  {
3152
3301
    if (interrupted_query)
3153
3302
      break;
3154
 
    drizzle_field_seek(result,0);
 
3303
    mysql_field_seek(result,0);
3155
3304
    tee_fprintf(PAGER,
3156
3305
                "*************************** %d. row ***************************\n", row_count);
3157
 
    for (uint off=0; off < drizzle_num_fields(result); off++)
 
3306
    for (uint off=0; off < mysql_num_fields(result); off++)
3158
3307
    {
3159
 
      field= drizzle_fetch_field(result);
 
3308
      field= mysql_fetch_field(result);
3160
3309
      tee_fprintf(PAGER, "%*s: ",(int) max_length,field->name);
3161
3310
      tee_fprintf(PAGER, "%s\n",cur[off] ? (char*) cur[off] : "NULL");
3162
3311
    }
3169
3318
static void print_warnings()
3170
3319
{
3171
3320
  const char   *query;
3172
 
  DRIZZLE_RES    *result;
3173
 
  DRIZZLE_ROW    cur;
 
3321
  MYSQL_RES    *result;
 
3322
  MYSQL_ROW    cur;
3174
3323
  uint64_t num_rows;
3175
 
 
 
3324
  
3176
3325
  /* Save current error before calling "show warnings" */
3177
 
  uint error= drizzle_errno(&drizzle);
 
3326
  uint error= mysql_errno(&mysql);
3178
3327
 
3179
3328
  /* Get the warnings */
3180
3329
  query= "show warnings";
3181
 
  drizzle_real_query_for_lazy(query, strlen(query));
3182
 
  drizzle_store_result_for_lazy(&result);
 
3330
  mysql_real_query_for_lazy(query, strlen(query));
 
3331
  mysql_store_result_for_lazy(&result);
3183
3332
 
3184
3333
  /* Bail out when no warnings */
3185
 
  if (!(num_rows= drizzle_num_rows(result)))
 
3334
  if (!(num_rows= mysql_num_rows(result)))
3186
3335
    goto end;
3187
3336
 
3188
 
  cur= drizzle_fetch_row(result);
 
3337
  cur= mysql_fetch_row(result);
3189
3338
 
3190
3339
  /*
3191
3340
    Don't print a duplicate of the current error.  It is possible for SHOW
3201
3350
  do
3202
3351
  {
3203
3352
    tee_fprintf(PAGER, "%s (Code %s): %s\n", cur[0], cur[1], cur[2]);
3204
 
  } while ((cur= drizzle_fetch_row(result)));
 
3353
  } while ((cur= mysql_fetch_row(result)));
3205
3354
  end_pager();
3206
3355
 
3207
3356
end:
3208
 
  drizzle_free_result(result);
3209
 
}
3210
 
 
3211
 
 
3212
 
static void
3213
 
safe_put_field(const char *pos,uint32_t length)
 
3357
  mysql_free_result(result);
 
3358
}
 
3359
 
 
3360
 
 
3361
static const char *array_value(const char **array, char key)
 
3362
{
 
3363
  for (; *array; array+= 2)
 
3364
    if (**array == key)
 
3365
      return array[1];
 
3366
  return 0;
 
3367
}
 
3368
 
 
3369
 
 
3370
static void
 
3371
xmlencode_print(const char *src, uint length)
 
3372
{
 
3373
  if (!src)
 
3374
    tee_fputs("NULL", PAGER);
 
3375
  else
 
3376
  {
 
3377
    for (const char *p = src; *p && length; length--)
 
3378
    {
 
3379
      const char *t;
 
3380
      if ((t = array_value(xmlmeta, *p++)))
 
3381
        tee_fputs(t, PAGER);
 
3382
      else
 
3383
        tee_putc(*p, PAGER);
 
3384
    }
 
3385
  }
 
3386
}
 
3387
 
 
3388
 
 
3389
static void
 
3390
safe_put_field(const char *pos,ulong length)
3214
3391
{
3215
3392
  if (!pos)
3216
3393
    tee_fputs("NULL", PAGER);
3247
3424
 
3248
3425
 
3249
3426
static void
3250
 
print_tab_data(DRIZZLE_RES *result)
 
3427
print_tab_data(MYSQL_RES *result)
3251
3428
{
3252
 
  DRIZZLE_ROW  cur;
3253
 
  DRIZZLE_FIELD  *field;
3254
 
  uint32_t    *lengths;
 
3429
  MYSQL_ROW     cur;
 
3430
  MYSQL_FIELD   *field;
 
3431
  uint32_t              *lengths;
3255
3432
 
3256
3433
  if (opt_silent < 2 && column_names)
3257
3434
  {
3258
3435
    int first=0;
3259
 
    while ((field = drizzle_fetch_field(result)))
 
3436
    while ((field = mysql_fetch_field(result)))
3260
3437
    {
3261
3438
      if (first++)
3262
3439
        (void) tee_fputs("\t", PAGER);
3264
3441
    }
3265
3442
    (void) tee_fputs("\n", PAGER);
3266
3443
  }
3267
 
  while ((cur = drizzle_fetch_row(result)))
 
3444
  while ((cur = mysql_fetch_row(result)))
3268
3445
  {
3269
 
    lengths= drizzle_fetch_lengths(result);
 
3446
    lengths= mysql_fetch_lengths(result);
3270
3447
    safe_put_field(cur[0],lengths[0]);
3271
 
    for (uint off=1 ; off < drizzle_num_fields(result); off++)
 
3448
    for (uint off=1 ; off < mysql_num_fields(result); off++)
3272
3449
    {
3273
3450
      (void) tee_fputs("\t", PAGER);
3274
3451
      safe_put_field(cur[off], lengths[off]);
3278
3455
}
3279
3456
 
3280
3457
static int
3281
 
com_tee(string *buffer __attribute__((unused)), const char *line )
 
3458
com_tee(DYNAMIC_STRING *buffer __attribute__((__unused__)), char *line )
3282
3459
{
3283
3460
  char file_name[FN_REFLEN], *end, *param;
3284
3461
 
3299
3476
      return 0;
3300
3477
    }
3301
3478
    else
3302
 
      param = outfile;      //resume using the old outfile
 
3479
      param = outfile;                  //resume using the old outfile
3303
3480
  }
3304
3481
 
3305
3482
  /* eliminate the spaces before the parameters */
3322
3499
 
3323
3500
 
3324
3501
static int
3325
 
com_notee(string *buffer __attribute__((unused)),
3326
 
          const char *line __attribute__((unused)))
 
3502
com_notee(DYNAMIC_STRING *buffer __attribute__((unused)),
 
3503
          char *line __attribute__((unused)))
3327
3504
{
3328
3505
  if (opt_outfile)
3329
3506
    end_tee();
3336
3513
*/
3337
3514
 
3338
3515
static int
3339
 
com_pager(string *buffer __attribute__((unused)),
3340
 
          const char *line __attribute__((unused)))
 
3516
com_pager(DYNAMIC_STRING *buffer __attribute__((__unused__)),
 
3517
          char *line __attribute__((unused)))
3341
3518
{
3342
3519
  char pager_name[FN_REFLEN], *end, *param;
3343
3520
 
3357
3534
    {
3358
3535
      tee_fprintf(stdout, "Default pager wasn't set, using stdout.\n");
3359
3536
      opt_nopager=1;
3360
 
      my_stpcpy(pager, "stdout");
 
3537
      strmov(pager, "stdout");
3361
3538
      PAGER= stdout;
3362
3539
      return 0;
3363
3540
    }
3364
 
    my_stpcpy(pager, default_pager);
 
3541
    strmov(pager, default_pager);
3365
3542
  }
3366
3543
  else
3367
3544
  {
3370
3547
                                my_iscntrl(charset_info,end[-1])))
3371
3548
      end--;
3372
3549
    end[0]=0;
3373
 
    my_stpcpy(pager, pager_name);
3374
 
    my_stpcpy(default_pager, pager_name);
 
3550
    strmov(pager, pager_name);
 
3551
    strmov(default_pager, pager_name);
3375
3552
  }
3376
3553
  opt_nopager=0;
3377
3554
  tee_fprintf(stdout, "PAGER set to '%s'\n", pager);
3380
3557
 
3381
3558
 
3382
3559
static int
3383
 
com_nopager(string *buffer __attribute__((unused)),
3384
 
            const char *line __attribute__((unused)))
 
3560
com_nopager(DYNAMIC_STRING *buffer __attribute__((unused)),
 
3561
            char *line __attribute__((unused)))
3385
3562
{
3386
 
  my_stpcpy(pager, "stdout");
 
3563
  strmov(pager, "stdout");
3387
3564
  opt_nopager=1;
3388
3565
  PAGER= stdout;
3389
3566
  tee_fprintf(stdout, "PAGER set to stdout\n");
3393
3570
/* If arg is given, exit without errors. This happens on command 'quit' */
3394
3571
 
3395
3572
static int
3396
 
com_quit(string *buffer __attribute__((unused)),
3397
 
         const char *line __attribute__((unused)))
 
3573
com_quit(DYNAMIC_STRING *buffer __attribute__((unused)),
 
3574
         char *line __attribute__((unused)))
3398
3575
{
3399
3576
  /* let the screen auto close on a normal shutdown */
3400
3577
  status.exit_status=0;
3402
3579
}
3403
3580
 
3404
3581
static int
3405
 
com_rehash(string *buffer __attribute__((unused)),
3406
 
           const char *line __attribute__((unused)))
 
3582
com_rehash(DYNAMIC_STRING *buffer __attribute__((unused)),
 
3583
           char *line __attribute__((unused)))
3407
3584
{
3408
3585
  build_completion_hash(1, 0);
3409
3586
  return 0;
3412
3589
 
3413
3590
 
3414
3591
static int
3415
 
com_print(string *buffer,const char *line __attribute__((unused)))
 
3592
com_print(DYNAMIC_STRING *buffer,char *line __attribute__((unused)))
3416
3593
{
3417
3594
  tee_puts("--------------", stdout);
3418
 
  (void) tee_fputs(buffer->c_str(), stdout);
3419
 
  if ( (buffer->length() == 0)
3420
 
       || (buffer->c_str())[(buffer->length())-1] != '\n')
 
3595
  (void) tee_fputs(buffer->str, stdout);
 
3596
  if ( (buffer->length == 0)
 
3597
       || (buffer->str)[(buffer->length)-1] != '\n')
3421
3598
    tee_putc('\n', stdout);
3422
3599
  tee_puts("--------------\n", stdout);
3423
3600
  /* If empty buffer */
3426
3603
 
3427
3604
/* ARGSUSED */
3428
3605
static int
3429
 
com_connect(string *buffer, const char *line)
 
3606
com_connect(DYNAMIC_STRING *buffer, char *line)
3430
3607
{
3431
3608
  char *tmp, buff[256];
3432
3609
  bool save_rehash= opt_rehash;
3433
3610
  int error;
3434
3611
 
3435
 
  memset(buff, 0, sizeof(buff));
 
3612
  bzero(buff, sizeof(buff));
3436
3613
  if (buffer)
3437
3614
  {
3438
3615
    /*
3446
3623
    tmp= get_arg(buff, 0);
3447
3624
    if (tmp && *tmp)
3448
3625
    {
3449
 
      free(current_db);
 
3626
      my_free(current_db, MYF(MY_ALLOW_ZERO_PTR));
3450
3627
      current_db= strdup(tmp);
3451
3628
      tmp= get_arg(buff, 1);
3452
3629
      if (tmp)
3453
3630
      {
3454
 
        free(current_host);
 
3631
        my_free(current_host,MYF(MY_ALLOW_ZERO_PTR));
3455
3632
        current_host=strdup(tmp);
3456
3633
      }
3457
3634
    }
3462
3639
    }
3463
3640
    // command used
3464
3641
    assert(buffer!=NULL);
3465
 
    buffer->clear();
 
3642
    dynstr_set(buffer, NULL);
3466
3643
  }
3467
3644
  else
3468
3645
    opt_rehash= 0;
3471
3648
 
3472
3649
  if (connected)
3473
3650
  {
3474
 
    sprintf(buff,"Connection id:    %u",drizzle_thread_id(&drizzle));
 
3651
    sprintf(buff,"Connection id:    %u",mysql_thread_id(&mysql));
3475
3652
    put_info(buff,INFO_INFO,0,0);
3476
3653
    sprintf(buff,"Current database: %.128s\n",
3477
3654
            current_db ? current_db : "*** NONE ***");
3481
3658
}
3482
3659
 
3483
3660
 
3484
 
static int com_source(string *buffer __attribute__((unused)), const char *line)
 
3661
static int com_source(DYNAMIC_STRING *buffer __attribute__((__unused__)), char *line)
3485
3662
{
3486
3663
  char source_name[FN_REFLEN], *end, *param;
3487
3664
  LINE_BUFFER *line_buff;
3492
3669
  /* Skip space from file name */
3493
3670
  while (my_isspace(charset_info,*line))
3494
3671
    line++;
3495
 
  if (!(param = strchr(line, ' ')))    // Skip command name
 
3672
  if (!(param = strchr(line, ' ')))             // Skip command name
3496
3673
    return put_info("Usage: \\. <filename> | source <filename>",
3497
3674
                    INFO_ERROR, 0,0);
3498
3675
  while (my_isspace(charset_info,*param))
3504
3681
  end[0]=0;
3505
3682
  unpack_filename(source_name,source_name);
3506
3683
  /* open file name */
3507
 
  if (!(sql_file = my_fopen(source_name, O_RDONLY,MYF(0))))
 
3684
  if (!(sql_file = my_fopen(source_name, O_RDONLY | O_BINARY,MYF(0))))
3508
3685
  {
3509
3686
    char buff[FN_REFLEN+60];
3510
3687
    sprintf(buff,"Failed to open file '%s', error: %d", source_name,errno);
3519
3696
 
3520
3697
  /* Save old status */
3521
3698
  old_status=status;
3522
 
  memset(&status, 0, sizeof(status));
 
3699
  bfill((char*) &status,sizeof(status),(char) 0);
3523
3700
 
3524
3701
  // Run in batch mode
3525
3702
  status.batch=old_status.batch;
3527
3704
  status.file_name=source_name;
3528
3705
  // Empty command buffer
3529
3706
  assert(glob_buffer!=NULL);
3530
 
  glob_buffer->clear();
 
3707
  dynstr_set(glob_buffer, NULL);
3531
3708
  error= read_and_execute(false);
3532
3709
  // Continue as before
3533
3710
  status=old_status;
3539
3716
 
3540
3717
/* ARGSUSED */
3541
3718
static int
3542
 
com_delimiter(string *buffer __attribute__((unused)), const char *line)
 
3719
com_delimiter(DYNAMIC_STRING *buffer __attribute__((unused)), char *line)
3543
3720
{
3544
3721
  char buff[256], *tmp;
3545
3722
 
3569
3746
 
3570
3747
/* ARGSUSED */
3571
3748
static int
3572
 
com_use(string *buffer __attribute__((unused)), const char *line)
 
3749
com_use(DYNAMIC_STRING *buffer __attribute__((unused)), char *line)
3573
3750
{
3574
3751
  char *tmp, buff[FN_REFLEN + 1];
3575
3752
  int select_db;
3576
3753
 
3577
 
  memset(buff, 0, sizeof(buff));
 
3754
  bzero(buff, sizeof(buff));
3578
3755
  strmake(buff, line, sizeof(buff) - 1);
3579
3756
  tmp= get_arg(buff, 0);
3580
3757
  if (!tmp || !*tmp)
3594
3771
    if (one_database)
3595
3772
    {
3596
3773
      skip_updates= 1;
3597
 
      select_db= 0;    // don't do drizzle_select_db()
 
3774
      select_db= 0;    // don't do mysql_select_db()
3598
3775
    }
3599
3776
    else
3600
 
      select_db= 2;    // do drizzle_select_db() and build_completion_hash()
 
3777
      select_db= 2;    // do mysql_select_db() and build_completion_hash()
3601
3778
  }
3602
3779
  else
3603
3780
  {
3604
3781
    /*
3605
3782
      USE to the current db specified.
3606
 
      We do need to send drizzle_select_db() to make server
 
3783
      We do need to send mysql_select_db() to make server
3607
3784
      update database level privileges, which might
3608
3785
      change since last USE (see bug#10979).
3609
3786
      For performance purposes, we'll skip rebuilding of completion hash.
3610
3787
    */
3611
3788
    skip_updates= 0;
3612
 
    select_db= 1;      // do only drizzle_select_db(), without completion
 
3789
    select_db= 1;      // do only mysql_select_db(), without completion
3613
3790
  }
3614
3791
 
3615
3792
  if (select_db)
3620
3797
    */
3621
3798
    if (!connected && reconnect())
3622
3799
      return opt_reconnect ? -1 : 1;                        // Fatal error
3623
 
    if (drizzle_select_db(&drizzle,tmp))
 
3800
    if (mysql_select_db(&mysql,tmp))
3624
3801
    {
3625
 
      if (drizzle_errno(&drizzle) != CR_SERVER_GONE_ERROR)
3626
 
        return put_error(&drizzle);
 
3802
      if (mysql_errno(&mysql) != CR_SERVER_GONE_ERROR)
 
3803
        return put_error(&mysql);
3627
3804
 
3628
3805
      if (reconnect())
3629
3806
        return opt_reconnect ? -1 : 1;                      // Fatal error
3630
 
      if (drizzle_select_db(&drizzle,tmp))
3631
 
        return put_error(&drizzle);
 
3807
      if (mysql_select_db(&mysql,tmp))
 
3808
        return put_error(&mysql);
3632
3809
    }
3633
 
    free(current_db);
 
3810
    my_free(current_db,MYF(MY_ALLOW_ZERO_PTR));
3634
3811
    current_db= strdup(tmp);
3635
3812
    if (select_db > 1)
3636
3813
      build_completion_hash(opt_rehash, 1);
3641
3818
}
3642
3819
 
3643
3820
static int
3644
 
com_warnings(string *buffer __attribute__((unused)),
3645
 
             const char *line __attribute__((unused)))
 
3821
com_warnings(DYNAMIC_STRING *buffer __attribute__((unused)),
 
3822
             char *line __attribute__((unused)))
3646
3823
{
3647
3824
  show_warnings = 1;
3648
3825
  put_info("Show warnings enabled.",INFO_INFO, 0, 0);
3650
3827
}
3651
3828
 
3652
3829
static int
3653
 
com_nowarnings(string *buffer __attribute__((unused)),
3654
 
               const char *line __attribute__((unused)))
 
3830
com_nowarnings(DYNAMIC_STRING *buffer __attribute__((unused)),
 
3831
               char *line __attribute__((unused)))
3655
3832
{
3656
3833
  show_warnings = 0;
3657
3834
  put_info("Show warnings disabled.",INFO_INFO, 0, 0);
3693
3870
        ptr++;
3694
3871
  }
3695
3872
  if (!*ptr)
3696
 
    return NULL;
 
3873
    return NullS;
3697
3874
  while (my_isspace(charset_info, *ptr))
3698
3875
    ptr++;
3699
3876
  if (*ptr == '\'' || *ptr == '\"' || *ptr == '`')
3707
3884
    if (*ptr == '\\' && ptr[1]) // escaped character
3708
3885
    {
3709
3886
      // Remove the backslash
3710
 
      my_stpcpy(ptr, ptr+1);
 
3887
      strmov(ptr, ptr+1);
3711
3888
    }
3712
3889
    else if ((!quoted && *ptr == ' ') || (quoted && *ptr == qtype))
3713
3890
    {
3716
3893
    }
3717
3894
  }
3718
3895
  valid_arg= ptr != start;
3719
 
  return valid_arg ? start : NULL;
 
3896
  return valid_arg ? start : NullS;
3720
3897
}
3721
3898
 
3722
3899
 
3723
3900
static int
3724
 
sql_connect(char *host,char *database,char *user,char *password,
 
3901
sql_real_connect(char *host,char *database,char *user,char *password,
3725
3902
                 uint silent)
3726
3903
{
3727
3904
  if (connected)
3728
3905
  {
3729
3906
    connected= 0;
3730
 
    drizzle_close(&drizzle);
 
3907
    mysql_close(&mysql);
3731
3908
  }
3732
 
  drizzle_create(&drizzle);
 
3909
  mysql_init(&mysql);
3733
3910
  if (opt_connect_timeout)
3734
3911
  {
3735
3912
    uint timeout=opt_connect_timeout;
3736
 
    drizzle_options(&drizzle,DRIZZLE_OPT_CONNECT_TIMEOUT,
 
3913
    mysql_options(&mysql,MYSQL_OPT_CONNECT_TIMEOUT,
3737
3914
                  (char*) &timeout);
3738
3915
  }
3739
3916
  if (opt_compress)
3740
 
    drizzle_options(&drizzle,DRIZZLE_OPT_COMPRESS,NULL);
 
3917
    mysql_options(&mysql,MYSQL_OPT_COMPRESS,NullS);
3741
3918
  if (opt_secure_auth)
3742
 
    drizzle_options(&drizzle, DRIZZLE_SECURE_AUTH, (char *) &opt_secure_auth);
 
3919
    mysql_options(&mysql, MYSQL_SECURE_AUTH, (char *) &opt_secure_auth);
3743
3920
  if (using_opt_local_infile)
3744
 
    drizzle_options(&drizzle,DRIZZLE_OPT_LOCAL_INFILE, (char*) &opt_local_infile);
 
3921
    mysql_options(&mysql,MYSQL_OPT_LOCAL_INFILE, (char*) &opt_local_infile);
 
3922
  if (opt_protocol)
 
3923
    mysql_options(&mysql,MYSQL_OPT_PROTOCOL,(char*)&opt_protocol);
3745
3924
  if (safe_updates)
3746
3925
  {
3747
3926
    char init_command[100];
3748
3927
    sprintf(init_command,
3749
 
            "SET SQL_SAFE_UPDATES=1,SQL_SELECT_LIMIT=%"PRIu32
 
3928
                  "SET SQL_SAFE_UPDATES=1,SQL_SELECT_LIMIT=%"PRIu32
3750
3929
            ",SQL_MAX_JOIN_SIZE=%"PRIu32,
3751
 
            select_limit, max_join_size);
3752
 
    drizzle_options(&drizzle, DRIZZLE_INIT_COMMAND, init_command);
 
3930
                  select_limit, max_join_size);
 
3931
    mysql_options(&mysql, MYSQL_INIT_COMMAND, init_command);
3753
3932
  }
3754
 
  if (!drizzle_connect(&drizzle, host, user, password,
3755
 
                          database, opt_drizzle_port, opt_drizzle_unix_port,
 
3933
  if (default_charset_used)
 
3934
    mysql_options(&mysql, MYSQL_SET_CHARSET_NAME, default_charset);
 
3935
  if (!mysql_real_connect(&mysql, host, user, password,
 
3936
                          database, opt_mysql_port, opt_mysql_unix_port,
3756
3937
                          connect_flag | CLIENT_MULTI_STATEMENTS))
3757
3938
  {
3758
3939
    if (!silent ||
3759
 
        (drizzle_errno(&drizzle) != CR_CONN_HOST_ERROR &&
3760
 
         drizzle_errno(&drizzle) != CR_CONNECTION_ERROR))
 
3940
        (mysql_errno(&mysql) != CR_CONN_HOST_ERROR &&
 
3941
         mysql_errno(&mysql) != CR_CONNECTION_ERROR))
3761
3942
    {
3762
 
      (void) put_error(&drizzle);
 
3943
      (void) put_error(&mysql);
3763
3944
      (void) fflush(stdout);
3764
 
      return ignore_errors ? -1 : 1;    // Abort
 
3945
      return ignore_errors ? -1 : 1;            // Abort
3765
3946
    }
3766
 
    return -1;          // Retryable
 
3947
    return -1;                                  // Retryable
3767
3948
  }
3768
3949
  connected=1;
3769
 
  drizzle.reconnect= debug_info_flag; // We want to know if this happens
 
3950
  mysql.reconnect= debug_info_flag; // We want to know if this happens
3770
3951
  build_completion_hash(opt_rehash, 1);
3771
3952
  return 0;
3772
3953
}
3773
3954
 
3774
3955
 
3775
3956
static int
3776
 
com_status(string *buffer __attribute__((unused)),
3777
 
           const char *line __attribute__((unused)))
3778
 
{
 
3957
sql_connect(char *host,char *database,char *user,char *password,uint silent)
 
3958
{
 
3959
  bool message=0;
 
3960
  uint count=0;
 
3961
  int error;
 
3962
  for (;;)
 
3963
  {
 
3964
    if ((error=sql_real_connect(host,database,user,password,wait_flag)) >= 0)
 
3965
    {
 
3966
      if (count)
 
3967
      {
 
3968
        tee_fputs("\n", stderr);
 
3969
        (void) fflush(stderr);
 
3970
      }
 
3971
      return error;
 
3972
    }
 
3973
    if (!wait_flag)
 
3974
      return ignore_errors ? -1 : 1;
 
3975
    if (!message && !silent)
 
3976
    {
 
3977
      message=1;
 
3978
      tee_fputs("Waiting",stderr); (void) fflush(stderr);
 
3979
    }
 
3980
    (void) sleep(wait_time);
 
3981
    if (!silent)
 
3982
    {
 
3983
      putc('.',stderr); (void) fflush(stderr);
 
3984
      count++;
 
3985
    }
 
3986
  }
 
3987
}
 
3988
 
 
3989
 
 
3990
 
 
3991
static int
 
3992
com_status(DYNAMIC_STRING *buffer __attribute__((unused)),
 
3993
           char *line __attribute__((unused)))
 
3994
{
 
3995
  const char *status_str;
3779
3996
  char buff[40];
3780
3997
  uint64_t id;
3781
 
  DRIZZLE_RES *result;
 
3998
  MYSQL_RES *result;
3782
3999
 
3783
4000
  tee_puts("--------------", stdout);
3784
 
  usage(1);          /* Print version */
 
4001
  usage(1);                                     /* Print version */
3785
4002
  if (connected)
3786
4003
  {
3787
 
    tee_fprintf(stdout, "\nConnection id:\t\t%lu\n",drizzle_thread_id(&drizzle));
 
4004
    tee_fprintf(stdout, "\nConnection id:\t\t%lu\n",mysql_thread_id(&mysql));
3788
4005
    /*
3789
4006
      Don't remove "limit 1",
3790
4007
      it is protection againts SQL_SELECT_LIMIT=0
3791
4008
    */
3792
 
    if (!drizzle_query(&drizzle,"select DATABASE(), USER() limit 1") &&
3793
 
        (result=drizzle_use_result(&drizzle)))
 
4009
    if (!mysql_query(&mysql,"select DATABASE(), USER() limit 1") &&
 
4010
        (result=mysql_use_result(&mysql)))
3794
4011
    {
3795
 
      DRIZZLE_ROW cur=drizzle_fetch_row(result);
 
4012
      MYSQL_ROW cur=mysql_fetch_row(result);
3796
4013
      if (cur)
3797
4014
      {
3798
4015
        tee_fprintf(stdout, "Current database:\t%s\n", cur[0] ? cur[0] : "");
3799
4016
        tee_fprintf(stdout, "Current user:\t\t%s\n", cur[1]);
3800
4017
      }
3801
 
      drizzle_free_result(result);
 
4018
      mysql_free_result(result);
3802
4019
    }
3803
4020
    tee_puts("SSL:\t\t\tNot in use", stdout);
3804
4021
  }
3818
4035
  tee_fprintf(stdout, "Current pager:\t\t%s\n", pager);
3819
4036
  tee_fprintf(stdout, "Using outfile:\t\t'%s'\n", opt_outfile ? outfile : "");
3820
4037
  tee_fprintf(stdout, "Using delimiter:\t%s\n", delimiter);
3821
 
  tee_fprintf(stdout, "Server version:\t\t%s\n", server_version_string(&drizzle));
3822
 
  tee_fprintf(stdout, "Protocol version:\t%d\n", drizzle_get_proto_info(&drizzle));
3823
 
  tee_fprintf(stdout, "Connection:\t\t%s\n", drizzle_get_host_info(&drizzle));
3824
 
  if ((id= drizzle_insert_id(&drizzle)))
 
4038
  tee_fprintf(stdout, "Server version:\t\t%s\n", server_version_string(&mysql));
 
4039
  tee_fprintf(stdout, "Protocol version:\t%d\n", mysql_get_proto_info(&mysql));
 
4040
  tee_fprintf(stdout, "Connection:\t\t%s\n", mysql_get_host_info(&mysql));
 
4041
  if ((id= mysql_insert_id(&mysql)))
3825
4042
    tee_fprintf(stdout, "Insert id:\t\t%s\n", llstr(id, buff));
3826
4043
 
3827
4044
  /* "limit 1" is protection against SQL_SELECT_LIMIT=0 */
3828
 
  if (!drizzle_query(&drizzle,"select @@character_set_client, @@character_set_connection, @@character_set_server, @@character_set_database limit 1") &&
3829
 
      (result=drizzle_use_result(&drizzle)))
 
4045
  if (!mysql_query(&mysql,"select @@character_set_client, @@character_set_connection, @@character_set_server, @@character_set_database limit 1") &&
 
4046
      (result=mysql_use_result(&mysql)))
3830
4047
  {
3831
 
    DRIZZLE_ROW cur=drizzle_fetch_row(result);
 
4048
    MYSQL_ROW cur=mysql_fetch_row(result);
3832
4049
    if (cur)
3833
4050
    {
3834
4051
      tee_fprintf(stdout, "Server characterset:\t%s\n", cur[2] ? cur[2] : "");
3836
4053
      tee_fprintf(stdout, "Client characterset:\t%s\n", cur[0] ? cur[0] : "");
3837
4054
      tee_fprintf(stdout, "Conn.  characterset:\t%s\n", cur[1] ? cur[1] : "");
3838
4055
    }
3839
 
    drizzle_free_result(result);
 
4056
    mysql_free_result(result);
 
4057
  }
 
4058
  else
 
4059
  {
 
4060
    /* Probably pre-4.1 server */
 
4061
    tee_fprintf(stdout, "Client characterset:\t%s\n", charset_info->csname);
 
4062
    tee_fprintf(stdout, "Server characterset:\t%s\n", mysql.charset->csname);
3840
4063
  }
3841
4064
 
3842
 
  if (strstr(drizzle_get_host_info(&drizzle),"TCP/IP") || ! drizzle.unix_socket)
3843
 
    tee_fprintf(stdout, "TCP port:\t\t%d\n", drizzle.port);
 
4065
  if (strstr(mysql_get_host_info(&mysql),"TCP/IP") || ! mysql.unix_socket)
 
4066
    tee_fprintf(stdout, "TCP port:\t\t%d\n", mysql.port);
3844
4067
  else
3845
 
    tee_fprintf(stdout, "UNIX socket:\t\t%s\n", drizzle.unix_socket);
3846
 
  if (drizzle.net.compress)
 
4068
    tee_fprintf(stdout, "UNIX socket:\t\t%s\n", mysql.unix_socket);
 
4069
  if (mysql.net.compress)
3847
4070
    tee_fprintf(stdout, "Protocol:\t\tCompressed\n");
3848
4071
 
 
4072
  if ((status_str= mysql_stat(&mysql)) && !mysql_error(&mysql)[0])
 
4073
  {
 
4074
    ulong sec;
 
4075
    const char *pos= strchr(status_str,' ');
 
4076
    /* print label */
 
4077
    tee_fprintf(stdout, "%.*s\t\t\t", (int) (pos-status_str), status_str);
 
4078
    if ((status_str= str2int(pos,10,0,LONG_MAX,(long*) &sec)))
 
4079
    {
 
4080
      nice_time((double) sec,buff,0);
 
4081
      tee_puts(buff, stdout);                   /* print nice time */
 
4082
      while (*status_str == ' ')
 
4083
        status_str++;  /* to next info */
 
4084
      tee_putc('\n', stdout);
 
4085
      tee_puts(status_str, stdout);
 
4086
    }
 
4087
  }
3849
4088
  if (safe_updates)
3850
4089
  {
3851
4090
    vidattr(A_BOLD);
3863
4102
}
3864
4103
 
3865
4104
static const char *
3866
 
server_version_string(DRIZZLE *con)
 
4105
server_version_string(MYSQL *con)
3867
4106
{
3868
4107
  static char buf[MAX_SERVER_VERSION_LENGTH] = "";
3869
4108
 
3871
4110
  if (buf[0] == '\0')
3872
4111
  {
3873
4112
    char *bufp = buf;
3874
 
    DRIZZLE_RES *result;
 
4113
    MYSQL_RES *result;
3875
4114
 
3876
 
    bufp= my_stpncpy(buf, drizzle_get_server_info(con), sizeof buf);
 
4115
    bufp= strnmov(buf, mysql_get_server_info(con), sizeof buf);
3877
4116
 
3878
4117
    /* "limit 1" is protection against SQL_SELECT_LIMIT=0 */
3879
 
    if (!drizzle_query(con, "select @@version_comment limit 1") &&
3880
 
        (result = drizzle_use_result(con)))
 
4118
    if (!mysql_query(con, "select @@version_comment limit 1") &&
 
4119
        (result = mysql_use_result(con)))
3881
4120
    {
3882
 
      DRIZZLE_ROW cur = drizzle_fetch_row(result);
 
4121
      MYSQL_ROW cur = mysql_fetch_row(result);
3883
4122
      if (cur && cur[0])
3884
4123
      {
3885
 
        bufp = strxnmov(bufp, sizeof buf - (bufp - buf), " ", cur[0], NULL);
 
4124
        bufp = strxnmov(bufp, sizeof buf - (bufp - buf), " ", cur[0], NullS);
3886
4125
      }
3887
 
      drizzle_free_result(result);
 
4126
      mysql_free_result(result);
3888
4127
    }
3889
4128
 
3890
4129
    /* str*nmov doesn't guarantee NUL-termination */
3968
4207
 
3969
4208
 
3970
4209
static int
3971
 
put_error(DRIZZLE *con)
 
4210
put_error(MYSQL *con)
3972
4211
{
3973
 
  return put_info(drizzle_error(con), INFO_ERROR, drizzle_errno(con),
3974
 
                  drizzle_sqlstate(con));
 
4212
  return put_info(mysql_error(con), INFO_ERROR, mysql_errno(con),
 
4213
                  mysql_sqlstate(con));
3975
4214
}
3976
4215
 
3977
4216
 
3978
 
static void remove_cntrl(string *buffer)
 
4217
static void remove_cntrl(DYNAMIC_STRING *buffer)
3979
4218
{
3980
 
  const char *start=  buffer->c_str();
3981
 
  const char *end= start + (buffer->length());
 
4219
  char *start=  buffer->str;
 
4220
  char *end= start + (buffer->length);
3982
4221
  while (start < end && !my_isgraph(charset_info,end[-1]))
3983
4222
    end--;
3984
 
  uint pos_to_truncate= (end-start);
3985
 
  if (buffer->length() > pos_to_truncate)
3986
 
    buffer->erase(pos_to_truncate);
 
4223
  uint chars_to_truncate = end-start;
 
4224
  if (buffer->length > chars_to_truncate)
 
4225
    dynstr_trunc(buffer, chars_to_truncate);
3987
4226
}
3988
4227
 
3989
4228
 
4031
4270
}
4032
4271
 
4033
4272
#include <sys/times.h>
4034
 
#ifdef _SC_CLK_TCK        // For mit-pthreads
 
4273
#ifdef _SC_CLK_TCK                              // For mit-pthreads
4035
4274
#undef CLOCKS_PER_SEC
4036
4275
#define CLOCKS_PER_SEC (sysconf(_SC_CLK_TCK))
4037
4276
#endif
4038
4277
 
4039
 
static uint32_t start_timer(void)
 
4278
static ulong start_timer(void)
4040
4279
{
4041
4280
  struct tms tms_tmp;
4042
4281
  return times(&tms_tmp);
4044
4283
 
4045
4284
 
4046
4285
/**
4047
 
   Write as many as 52+1 bytes to buff, in the form of a legible
4048
 
   duration of time.
 
4286
   Write as many as 52+1 bytes to buff, in the form of a legible duration of time.
4049
4287
 
4050
4288
   len("4294967296 days, 23 hours, 59 minutes, 60.00 seconds")  ->  52
4051
4289
*/
4052
4290
static void nice_time(double sec,char *buff,bool part_second)
4053
4291
{
4054
 
  uint32_t tmp;
 
4292
  ulong tmp;
4055
4293
  if (sec >= 3600.0*24)
4056
4294
  {
4057
 
    tmp=(uint32_t) floor(sec/(3600.0*24));
 
4295
    tmp=(ulong) floor(sec/(3600.0*24));
4058
4296
    sec-=3600.0*24*tmp;
4059
4297
    buff=int10_to_str((long) tmp, buff, 10);
4060
 
    buff=my_stpcpy(buff,tmp > 1 ? " days " : " day ");
 
4298
    buff=strmov(buff,tmp > 1 ? " days " : " day ");
4061
4299
  }
4062
4300
  if (sec >= 3600.0)
4063
4301
  {
4064
 
    tmp=(uint32_t) floor(sec/3600.0);
 
4302
    tmp=(ulong) floor(sec/3600.0);
4065
4303
    sec-=3600.0*tmp;
4066
4304
    buff=int10_to_str((long) tmp, buff, 10);
4067
 
    buff=my_stpcpy(buff,tmp > 1 ? " hours " : " hour ");
 
4305
    buff=strmov(buff,tmp > 1 ? " hours " : " hour ");
4068
4306
  }
4069
4307
  if (sec >= 60.0)
4070
4308
  {
4071
 
    tmp=(uint32_t) floor(sec/60.0);
 
4309
    tmp=(ulong) floor(sec/60.0);
4072
4310
    sec-=60.0*tmp;
4073
4311
    buff=int10_to_str((long) tmp, buff, 10);
4074
 
    buff=my_stpcpy(buff," min ");
 
4312
    buff=strmov(buff," min ");
4075
4313
  }
4076
4314
  if (part_second)
4077
4315
    sprintf(buff,"%.2f sec",sec);
4080
4318
}
4081
4319
 
4082
4320
 
4083
 
static void end_timer(uint32_t start_time,char *buff)
 
4321
static void end_timer(ulong start_time,char *buff)
4084
4322
{
4085
4323
  nice_time((double) (start_timer() - start_time) /
4086
4324
            CLOCKS_PER_SEC,buff,1);
4087
4325
}
4088
4326
 
4089
4327
 
4090
 
static void drizzle_end_timer(uint32_t start_time,char *buff)
 
4328
static void mysql_end_timer(ulong start_time,char *buff)
4091
4329
{
4092
4330
  buff[0]=' ';
4093
4331
  buff[1]='(';
4094
4332
  end_timer(start_time,buff+2);
4095
 
  my_stpcpy(strchr(buff, '\0'),")");
 
4333
  strmov(strend(buff),")");
4096
4334
}
4097
4335
 
4098
4336
static const char * construct_prompt()
4099
4337
{
4100
4338
  // Erase the old prompt
4101
4339
  assert(processed_prompt!=NULL);
4102
 
  processed_prompt->clear();
 
4340
  dynstr_set(processed_prompt, NULL);
4103
4341
 
4104
4342
  // Get the date struct
4105
4343
  time_t  lclock = time(NULL);
4110
4348
  {
4111
4349
    if (*c != PROMPT_CHAR)
4112
4350
    {
4113
 
      processed_prompt->append(c, 1);
 
4351
      dynstr_append_mem(processed_prompt, c, 1);
4114
4352
    }
4115
4353
    else
4116
4354
    {
4127
4365
        break;
4128
4366
      case 'v':
4129
4367
        if (connected)
4130
 
          processed_prompt->append(drizzle_get_server_info(&drizzle));
 
4368
          dynstr_append(processed_prompt, mysql_get_server_info(&mysql));
4131
4369
        else
4132
 
          processed_prompt->append("not_connected");
 
4370
          dynstr_append(processed_prompt, "not_connected");
4133
4371
        break;
4134
4372
      case 'd':
4135
 
        processed_prompt->append(current_db ? current_db : "(none)");
 
4373
        dynstr_append(processed_prompt, current_db ? current_db : "(none)");
4136
4374
        break;
4137
4375
      case 'h':
4138
4376
      {
4139
4377
        const char *prompt;
4140
 
        prompt= connected ? drizzle_get_host_info(&drizzle) : "not_connected";
 
4378
        prompt= connected ? mysql_get_host_info(&mysql) : "not_connected";
4141
4379
        if (strstr(prompt, "Localhost"))
4142
 
          processed_prompt->append("localhost");
 
4380
          dynstr_append(processed_prompt, "localhost");
4143
4381
        else
4144
4382
        {
4145
 
          const char *end=strrchr(prompt,' ');
4146
 
          if (end != NULL)
4147
 
            processed_prompt->append(prompt, (end-prompt));
 
4383
          const char *end=strcend(prompt,' ');
 
4384
          dynstr_append_mem(processed_prompt, prompt, (end-prompt));
4148
4385
        }
4149
4386
        break;
4150
4387
      }
4152
4389
      {
4153
4390
        if (!connected)
4154
4391
        {
4155
 
          processed_prompt->append("not_connected");
 
4392
          dynstr_append(processed_prompt, "not_connected");
4156
4393
          break;
4157
4394
        }
4158
4395
 
4159
 
        const char *host_info = drizzle_get_host_info(&drizzle);
 
4396
        const char *host_info = mysql_get_host_info(&mysql);
4160
4397
        if (strstr(host_info, "memory"))
4161
4398
        {
4162
 
          processed_prompt->append(drizzle.host);
 
4399
          dynstr_append(processed_prompt, mysql.host);
4163
4400
        }
4164
4401
        else if (strstr(host_info,"TCP/IP") ||
4165
 
                 !drizzle.unix_socket)
4166
 
          add_int_to_prompt(drizzle.port);
 
4402
                 !mysql.unix_socket)
 
4403
          add_int_to_prompt(mysql.port);
4167
4404
        else
4168
4405
        {
4169
 
          char *pos=strrchr(drizzle.unix_socket,'/');
4170
 
          processed_prompt->append(pos ? pos+1 : drizzle.unix_socket);
 
4406
          char *pos=strrchr(mysql.unix_socket,'/');
 
4407
          dynstr_append(processed_prompt, pos ? pos+1 : mysql.unix_socket);
4171
4408
        }
4172
4409
      }
4173
4410
      break;
4174
4411
      case 'U':
4175
4412
        if (!full_username)
4176
4413
          init_username();
4177
 
        processed_prompt->append(full_username ? full_username :
4178
 
                                 (current_user ?  current_user : "(unknown)"));
 
4414
        dynstr_append(processed_prompt, full_username ? full_username :
 
4415
                        (current_user ?  current_user : "(unknown)"));
4179
4416
        break;
4180
4417
      case 'u':
4181
4418
        if (!full_username)
4182
4419
          init_username();
4183
 
        processed_prompt->append(part_username ? part_username :
4184
 
                                 (current_user ?  current_user : "(unknown)"));
 
4420
        dynstr_append(processed_prompt, part_username ? part_username :
 
4421
                        (current_user ?  current_user : "(unknown)"));
4185
4422
        break;
4186
4423
      case PROMPT_CHAR:
4187
4424
        {
4188
4425
          char c= PROMPT_CHAR;
4189
 
          processed_prompt->append(&c, 1);
 
4426
          dynstr_append_mem(processed_prompt, &c, 1);
4190
4427
        }
4191
4428
        break;
4192
4429
      case 'n':
4193
4430
        {
4194
4431
          char c= '\n';
4195
 
          processed_prompt->append(&c, 1);
 
4432
          dynstr_append_mem(processed_prompt, &c, 1);
4196
4433
        }
4197
4434
        break;
4198
4435
      case ' ':
4199
4436
      case '_':
4200
4437
        {
4201
4438
          char c= ' ';
4202
 
          processed_prompt->append(&c, 1);
 
4439
          dynstr_append_mem(processed_prompt, &c, 1);
4203
4440
        }
4204
4441
        break;
4205
4442
      case 'R':
4231
4468
        break;
4232
4469
      case 'D':
4233
4470
        dateTime = ctime(&lclock);
4234
 
        processed_prompt->append(strtok(dateTime,"\n"));
 
4471
        dynstr_append(processed_prompt, strtok(dateTime,"\n"));
4235
4472
        break;
4236
4473
      case 's':
4237
4474
        if (t->tm_sec < 10)
4239
4476
        add_int_to_prompt(t->tm_sec);
4240
4477
        break;
4241
4478
      case 'w':
4242
 
        processed_prompt->append(day_names[t->tm_wday]);
 
4479
        dynstr_append(processed_prompt, (day_names[t->tm_wday]));
4243
4480
        break;
4244
4481
      case 'P':
4245
 
        processed_prompt->append(t->tm_hour < 12 ? "am" : "pm");
 
4482
        dynstr_append(processed_prompt, t->tm_hour < 12 ? "am" : "pm");
4246
4483
        break;
4247
4484
      case 'o':
4248
4485
        add_int_to_prompt(t->tm_mon+1);
4249
4486
        break;
4250
4487
      case 'O':
4251
 
        processed_prompt->append(month_names[t->tm_mon]);
 
4488
        dynstr_append(processed_prompt, month_names[t->tm_mon]);
4252
4489
        break;
4253
4490
      case '\'':
4254
 
        processed_prompt->append("'");
 
4491
        dynstr_append(processed_prompt, "'");
4255
4492
        break;
4256
4493
      case '"':
4257
 
        processed_prompt->append("\"");
 
4494
        dynstr_append(processed_prompt, "\"");
4258
4495
        break;
4259
4496
      case 'S':
4260
 
        processed_prompt->append(";");
 
4497
        dynstr_append(processed_prompt, ";");
4261
4498
        break;
4262
4499
      case 't':
4263
 
        processed_prompt->append("\t");
 
4500
        dynstr_append(processed_prompt, "\t");
4264
4501
        break;
4265
4502
      case 'l':
4266
 
        processed_prompt->append(delimiter_str);
 
4503
        dynstr_append(processed_prompt, delimiter_str);
4267
4504
        break;
4268
4505
      default:
4269
 
        processed_prompt->append(c, 1);
 
4506
        dynstr_append_mem(processed_prompt, c, 1);
4270
4507
      }
4271
4508
    }
4272
4509
  }
4273
 
  return processed_prompt->c_str();
 
4510
  return processed_prompt->str;
4274
4511
}
4275
4512
 
4276
4513
 
4278
4515
{
4279
4516
  char buffer[16];
4280
4517
  int10_to_str(toadd, buffer, 10);
4281
 
  processed_prompt->append(buffer);
 
4518
  dynstr_append(processed_prompt, buffer);
4282
4519
}
4283
4520
 
4284
4521
static void init_username()
4285
4522
{
4286
 
  free(full_username);
4287
 
  free(part_username);
 
4523
  my_free(full_username,MYF(MY_ALLOW_ZERO_PTR));
 
4524
  my_free(part_username,MYF(MY_ALLOW_ZERO_PTR));
4288
4525
 
4289
 
  DRIZZLE_RES *result;
4290
 
  if (!drizzle_query(&drizzle,"select USER()") &&
4291
 
      (result=drizzle_use_result(&drizzle)))
 
4526
  MYSQL_RES *result;
 
4527
  if (!mysql_query(&mysql,"select USER()") &&
 
4528
      (result=mysql_use_result(&mysql)))
4292
4529
  {
4293
 
    DRIZZLE_ROW cur=drizzle_fetch_row(result);
 
4530
    MYSQL_ROW cur=mysql_fetch_row(result);
4294
4531
    full_username= strdup(cur[0]);
4295
4532
    part_username= strdup(strtok(cur[0],"@"));
4296
 
    (void) drizzle_fetch_row(result);        // Read eof
 
4533
    (void) mysql_fetch_row(result);             // Read eof
4297
4534
  }
4298
4535
}
4299
4536
 
4300
 
static int com_prompt(string *buffer __attribute__((unused)),
4301
 
                      const char *line)
 
4537
static int com_prompt(DYNAMIC_STRING *buffer __attribute__((__unused__)), char *line)
4302
4538
{
4303
4539
  char *ptr=strchr(line, ' ');
4304
4540
  prompt_counter = 0;
4305
 
  free(current_prompt);
 
4541
  my_free(current_prompt,MYF(MY_ALLOW_ZERO_PTR));
4306
4542
  current_prompt= strdup(ptr ? ptr+1 : default_prompt);
4307
4543
  if (!ptr)
4308
4544
    tee_fprintf(stdout, "Returning to default PROMPT of %s\n",
4311
4547
    tee_fprintf(stdout, "PROMPT set to '%s'\n", current_prompt);
4312
4548
  return 0;
4313
4549
}
4314
 
 
4315
 
/*
4316
 
    strcont(str, set) if str contanies any character in the string set.
4317
 
    The result is the position of the first found character in str, or NULL
4318
 
    if there isn't anything found.
4319
 
*/
4320
 
 
4321
 
static const char * strcont(register const char *str, register const char *set)
4322
 
{
4323
 
  register const char * start = (const char *) set;
4324
 
 
4325
 
  while (*str)
4326
 
  {
4327
 
    while (*set)
4328
 
    {
4329
 
      if (*set++ == *str)
4330
 
        return ((const char*) str);
4331
 
    }
4332
 
    set=start; str++;
4333
 
  }
4334
 
  return NULL;
4335
 
} /* strcont */