~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to client/drizzle.c

Merge/fix in FAQ.

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