~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to client/drizzle.cc

  • Committer: Brian Aker
  • Date: 2008-10-06 06:47:29 UTC
  • Revision ID: brian@tangent.org-20081006064729-2i9mhjkzyvow9xsm
RemoveĀ uint.

Show diffs side-by-side

added added

removed removed

Lines of Context:
33
33
 *
34
34
 **/
35
35
 
 
36
#include "config.h"
 
37
 
 
38
#include <string>
 
39
 
36
40
#include "client_priv.h"
37
 
#include <string>
38
 
#include CMATH_H
39
 
#include <algorithm>
40
41
#include <mystrings/m_ctype.h>
41
42
#include <stdarg.h>
 
43
#ifndef __GNU_LIBRARY__
 
44
#define __GNU_LIBRARY__          // Skip warnings in getopt.h
 
45
#endif
 
46
#include <readline/history.h>
42
47
#include "my_readline.h"
43
48
#include <signal.h>
44
49
#include <sys/ioctl.h>
45
 
#include <drizzled/configmake.h>
 
50
 
 
51
 
 
52
#if defined(HAVE_LOCALE_H)
 
53
#include <locale.h>
 
54
#endif
 
55
 
 
56
#include <libdrizzle/gettext.h>
 
57
 
 
58
const char *VER= "14.14";
 
59
 
 
60
/* Don't try to make a nice table if the data is too big */
 
61
#define MAX_COLUMN_LENGTH       (uint32_t)1024
 
62
 
 
63
/* Buffer to hold 'version' and 'version_comment' */
 
64
#define MAX_SERVER_VERSION_LENGTH     128
 
65
 
 
66
/* Array of options to pass to libdrizzled */
 
67
#define MAX_SERVER_ARGS               64
 
68
 
 
69
void* sql_alloc(unsigned size);       // Don't use drizzled alloc for these
 
70
void sql_element_free(void *ptr);
 
71
 
46
72
 
47
73
#if defined(HAVE_CURSES_H) && defined(HAVE_TERM_H)
48
74
#include <curses.h>
73
99
#endif
74
100
#endif
75
101
 
76
 
#ifdef HAVE_LIBREADLINE
77
 
#  if defined(HAVE_READLINE_READLINE_H)
78
 
#    include <readline/readline.h>
79
 
#  elif defined(HAVE_READLINE_H)
80
 
#    include <readline.h>
81
 
#  else /* !defined(HAVE_READLINE_H) */
82
 
extern char *readline ();
83
 
#  endif /* !defined(HAVE_READLINE_H) */
84
 
char *cmdline = NULL;
85
 
#else /* !defined(HAVE_READLINE_READLINE_H) */
86
 
  /* no readline */
87
 
#  error Readline Required
88
 
#endif /* HAVE_LIBREADLINE */
 
102
#undef bcmp                             // Fix problem with new readline
89
103
 
90
 
#ifdef HAVE_READLINE_HISTORY
91
 
#  if defined(HAVE_READLINE_HISTORY_H)
92
 
#    include <readline/history.h>
93
 
#  elif defined(HAVE_HISTORY_H)
94
 
#    include <history.h>
95
 
#  else /* !defined(HAVE_HISTORY_H) */
96
 
extern void add_history ();
97
 
extern int write_history ();
98
 
extern int read_history ();
99
 
#  endif /* defined(HAVE_READLINE_HISTORY_H) */
100
 
    /* no history */
101
 
#endif /* HAVE_READLINE_HISTORY */
 
104
#ifdef HAVE_READLINE_HISTORY_H
 
105
#include <readline/history.h>
 
106
#endif
 
107
#include <readline/readline.h>
102
108
 
103
109
/**
104
110
 Make the old readline interface look like the new one.
110
116
  completion_matches((char *)str, (CPFunction *)func)
111
117
#endif
112
118
 
113
 
#if defined(HAVE_LOCALE_H)
114
 
#include <locale.h>
115
 
#endif
116
 
 
117
 
#include <drizzled/gettext.h>
118
 
 
119
 
#if defined(CMATH_NAMESPACE)
120
 
  using namespace CMATH_NAMESPACE;
121
 
#endif
122
 
 
123
 
const char *VER= "14.14";
124
 
 
125
 
/* Don't try to make a nice table if the data is too big */
126
 
#define MAX_COLUMN_LENGTH       (uint32_t)1024
127
 
 
128
 
/* Buffer to hold 'version' and 'version_comment' */
129
 
#define MAX_SERVER_VERSION_LENGTH     128
130
 
 
131
 
void* sql_alloc(unsigned size);       // Don't use drizzled alloc for these
132
 
void sql_element_free(void *ptr);
133
 
 
134
119
 
135
120
#if !defined(HAVE_VIDATTR)
136
121
#undef vidattr
167
152
typedef enum enum_info_type INFO_TYPE;
168
153
 
169
154
static DRIZZLE drizzle;      /* The connection */
170
 
static bool ignore_errors= false, quick= false,
171
 
  connected= false, opt_raw_data= false, unbuffered= false,
172
 
  output_tables= false, opt_rehash= true, skip_updates= false,
173
 
  safe_updates= false, one_database= false,
174
 
  opt_compress= false, using_opt_local_infile= false,
175
 
  vertical= false, line_numbers= true, column_names= true,
176
 
  opt_nopager= true, opt_outfile= false, named_cmds= false,
177
 
  tty_password= false, opt_nobeep= false, opt_reconnect= true,
178
 
  default_charset_used= false, opt_secure_auth= false,
179
 
  default_pager_set= false, opt_sigint_ignore= false,
180
 
  auto_vertical_output= false,
181
 
  show_warnings= false, executing_query= false, interrupted_query= false;
182
 
static uint32_t  show_progress_size= 0;
 
155
static bool ignore_errors=0,quick=0,
 
156
  connected=0,opt_raw_data=0,unbuffered=0,output_tables=0,
 
157
  opt_rehash=1,skip_updates=0,safe_updates=0,one_database=0,
 
158
  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,
 
161
  tty_password= 0, opt_nobeep=0, opt_reconnect=1,
 
162
  default_charset_used= 0, opt_secure_auth= 0,
 
163
  default_pager_set= 0, opt_sigint_ignore= 0,
 
164
  auto_vertical_output= 0,
 
165
  show_warnings= 0, executing_query= 0, interrupted_query= 0;
183
166
static bool debug_info_flag, debug_check_flag;
184
167
static bool column_types_flag;
185
 
static bool preserve_comments= false;
186
 
static uint32_t opt_max_allowed_packet, opt_net_buffer_length,
187
 
  opt_drizzle_port= 0;
188
 
static int verbose= 0, opt_silent= 0, opt_local_infile= 0;
 
168
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;
189
171
static uint my_end_arg;
190
 
static char * opt_drizzle_unix_port= NULL;
191
 
static int connect_flag= CLIENT_INTERACTIVE;
192
 
static char *current_host, *current_db, *current_user= NULL,
193
 
  *opt_password= NULL, *delimiter_str= NULL, *current_prompt= NULL;
 
172
static char * opt_drizzle_unix_port=0;
 
173
static int connect_flag=CLIENT_INTERACTIVE;
 
174
static char *current_host,*current_db,*current_user=0,*opt_password=0,
 
175
  *delimiter_str= 0,* current_prompt= 0;
194
176
static char *histfile;
195
177
static char *histfile_tmp;
196
178
static string *glob_buffer;
197
179
static string *processed_prompt= NULL;
198
180
static char *default_prompt= NULL;
199
 
static char *full_username= NULL,*part_username= NULL;
 
181
static char *full_username=0,*part_username=0;
200
182
static STATUS status;
201
183
static uint32_t select_limit;
202
184
static uint32_t max_join_size;
203
185
static uint32_t opt_connect_timeout= 0;
 
186
static char drizzle_charsets_dir[FN_REFLEN+1];
204
187
// TODO: Need to i18n these
205
 
static const char *day_names[]= {"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
206
 
static const char *month_names[]= {"Jan","Feb","Mar","Apr","May","Jun","Jul",
 
188
static const char *day_names[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
 
189
static const char *month_names[]={"Jan","Feb","Mar","Apr","May","Jun","Jul",
207
190
                                  "Aug","Sep","Oct","Nov","Dec"};
208
191
static char default_pager[FN_REFLEN];
209
192
static char pager[FN_REFLEN], outfile[FN_REFLEN];
216
199
 
217
200
static const CHARSET_INFO *charset_info= &my_charset_utf8_general_ci;
218
201
 
219
 
int drizzleclient_real_query_for_lazy(const char *buf, int length);
220
 
int drizzleclient_store_result_for_lazy(DRIZZLE_RES **result);
 
202
int drizzle_real_query_for_lazy(const char *buf, int length);
 
203
int drizzle_store_result_for_lazy(DRIZZLE_RES **result);
221
204
 
222
205
 
223
206
void tee_fprintf(FILE *file, const char *fmt, ...);
227
210
static void tee_print_sized_data(const char *, unsigned int, unsigned int, bool);
228
211
/* The names of functions that actually do the manipulation. */
229
212
static int get_options(int argc,char **argv);
230
 
extern "C" bool get_one_option(int optid, const struct my_option *opt,
231
 
                               char *argument);
 
213
bool get_one_option(int optid, const struct my_option *opt,
 
214
                    char *argument);
232
215
static int com_quit(string *str,const char*),
233
216
  com_go(string *str,const char*), com_ego(string *str,const char*),
234
217
  com_print(string *str,const char*),
273
256
 
274
257
 
275
258
static COMMANDS commands[] = {
276
 
  { "?",      '?', com_help,   0, N_("Synonym for `help'.") },
 
259
  { "?",      '?', com_help,   1, N_("Synonym for `help'.") },
277
260
  { "clear",  'c', com_clear,  0, N_("Clear command.")},
278
261
  { "connect",'r', com_connect,1,
279
262
    N_("Reconnect to the server. Optional arguments are db and host." }),
283
266
    N_("Send command to drizzle server, display result vertically.")},
284
267
  { "exit",   'q', com_quit,   0, N_("Exit drizzle. Same as quit.")},
285
268
  { "go",     'g', com_go,     0, N_("Send command to drizzle server.") },
286
 
  { "help",   'h', com_help,   0, N_("Display this help.") },
 
269
  { "help",   'h', com_help,   1, N_("Display this help.") },
287
270
  { "nopager",'n', com_nopager,0, N_("Disable pager, print to stdout.") },
288
271
  { "notee",  't', com_notee,  0, N_("Don't write into outfile.") },
289
272
  { "pager",  'P', com_pager,  1,
627
610
  { "QUARTER", 0, 0, 0, ""},
628
611
  { "QUERY", 0, 0, 0, ""},
629
612
  { "QUICK", 0, 0, 0, ""},
 
613
  { "RAID0", 0, 0, 0, ""},
 
614
  { "RAID_CHUNKS", 0, 0, 0, ""},
 
615
  { "RAID_CHUNKSIZE", 0, 0, 0, ""},
 
616
  { "RAID_TYPE", 0, 0, 0, ""},
630
617
  { "READ", 0, 0, 0, ""},
631
618
  { "READS", 0, 0, 0, ""},
632
619
  { "REAL", 0, 0, 0, ""},
1028
1015
static void end_timer(uint32_t start_time,char *buff);
1029
1016
static void drizzle_end_timer(uint32_t start_time,char *buff);
1030
1017
static void nice_time(double sec,char *buff,bool part_second);
1031
 
extern "C" void drizzle_end(int sig);
1032
 
extern "C" void handle_sigint(int sig);
 
1018
extern RETSIGTYPE drizzle_end(int sig);
 
1019
extern RETSIGTYPE handle_sigint(int sig);
1033
1020
#if defined(HAVE_TERMIOS_H) && defined(GWINSZ_IN_SYS_IOCTL)
1034
 
static void window_resize(int sig);
 
1021
static RETSIGTYPE window_resize(int sig);
1035
1022
#endif
1036
1023
 
1037
1024
int main(int argc,char *argv[])
1048
1035
 
1049
1036
  MY_INIT(argv[0]);
1050
1037
  delimiter_str= delimiter;
1051
 
  default_prompt= strdup(getenv("DRIZZLE_PS1") ?
1052
 
                         getenv("DRIZZLE_PS1") :
1053
 
                         "drizzle> ");
1054
 
  
1055
 
  if (default_prompt == NULL)
1056
 
  {
1057
 
    fprintf(stderr, _("Memory allocation error while constructing initial "
1058
 
                      "prompt. Aborting.\n"));
1059
 
    exit(ENOMEM);
1060
 
  }
1061
 
  current_prompt= strdup(default_prompt);
1062
 
  if (current_prompt == NULL)
1063
 
  {
1064
 
    fprintf(stderr, _("Memory allocation error while constructing initial "
1065
 
                      "prompt. Aborting.\n"));
1066
 
    exit(ENOMEM);
1067
 
  }
 
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));
1068
1042
  processed_prompt= new string();
1069
1043
  processed_prompt->reserve(32);
1070
1044
 
1071
1045
  prompt_counter=0;
1072
1046
 
1073
1047
  outfile[0]=0;      // no (default) outfile
1074
 
  strcpy(pager, "stdout");  // the default, if --pager wasn't given
 
1048
  my_stpcpy(pager, "stdout");  // the default, if --pager wasn't given
1075
1049
  {
1076
1050
    char *tmp=getenv("PAGER");
1077
1051
    if (tmp && strlen(tmp))
1078
1052
    {
1079
1053
      default_pager_set= 1;
1080
 
      strcpy(default_pager, tmp);
 
1054
      my_stpcpy(default_pager, tmp);
1081
1055
    }
1082
1056
  }
1083
1057
  if (!isatty(0) || !isatty(1))
1103
1077
      close(stdout_fileno_copy);             /* Clean up dup(). */
1104
1078
  }
1105
1079
 
1106
 
  load_defaults("drizzle",load_default_groups,&argc,&argv);
 
1080
  load_defaults("my",load_default_groups,&argc,&argv);
1107
1081
  defaults_argv=argv;
1108
1082
  if (get_options(argc, (char **) argv))
1109
1083
  {
1149
1123
 
1150
1124
  glob_buffer= new string();
1151
1125
  glob_buffer->reserve(512);
1152
 
 
 
1126
  
1153
1127
  char * output_buff= (char *)malloc(512);
1154
1128
  memset(output_buff, '\0', 512);
1155
1129
 
1156
1130
  sprintf(output_buff,
1157
1131
          _("Your Drizzle connection id is %u\nServer version: %s\n"),
1158
 
          drizzleclient_thread_id(&drizzle),
 
1132
          drizzle_thread_id(&drizzle),
1159
1133
          server_version_string(&drizzle));
1160
1134
  put_info(output_buff, INFO_INFO, 0, 0);
1161
1135
 
1167
1141
      histfile= strdup(getenv("DRIZZLE_HISTFILE"));
1168
1142
    else if (getenv("HOME"))
1169
1143
    {
1170
 
      histfile=(char*) malloc(strlen(getenv("HOME")) + strlen("/.drizzle_history") + 2);
 
1144
      histfile=(char*) my_malloc((uint) strlen(getenv("HOME"))
 
1145
                                 + (uint) strlen("/.drizzle_history")+2,
 
1146
                                 MYF(MY_WME));
1171
1147
      if (histfile)
1172
1148
        sprintf(histfile,"%s/.drizzle_history",getenv("HOME"));
1173
1149
      char link_name[FN_REFLEN];
1184
1160
      if (verbose)
1185
1161
        tee_fprintf(stdout, _("Reading history-file %s\n"),histfile);
1186
1162
      read_history(histfile);
1187
 
      if (!(histfile_tmp= (char*) malloc((uint) strlen(histfile) + 5)))
 
1163
      if (!(histfile_tmp= (char*) my_malloc((uint) strlen(histfile) + 5,
 
1164
                                            MYF(MY_WME))))
1188
1165
      {
1189
1166
        fprintf(stderr, _("Couldn't allocate memory for temp histfile!\n"));
1190
1167
        exit(1);
1204
1181
  return(0);        // Keep compiler happy
1205
1182
}
1206
1183
 
1207
 
void drizzle_end(int sig)
 
1184
RETSIGTYPE drizzle_end(int sig)
1208
1185
{
1209
 
  drizzleclient_close(&drizzle);
 
1186
  drizzle_close(&drizzle);
1210
1187
  if (!status.batch && !quick && histfile)
1211
1188
  {
1212
1189
    /* write-history */
1247
1224
  If query is in process, kill query
1248
1225
  no query in process, terminate like previous behavior
1249
1226
*/
1250
 
extern "C"
1251
 
void handle_sigint(int sig)
 
1227
RETSIGTYPE handle_sigint(int sig)
1252
1228
{
1253
1229
  char kill_buffer[40];
1254
1230
  DRIZZLE *kill_drizzle= NULL;
1258
1234
    goto err;
1259
1235
  }
1260
1236
 
1261
 
  kill_drizzle= drizzleclient_create(kill_drizzle);
1262
 
  if (!drizzleclient_connect(kill_drizzle,current_host, current_user, opt_password,
 
1237
  kill_drizzle= drizzle_create(kill_drizzle);
 
1238
  if (!drizzle_connect(kill_drizzle,current_host, current_user, opt_password,
1263
1239
                          "", opt_drizzle_port, opt_drizzle_unix_port,0))
1264
1240
  {
1265
1241
    goto err;
1266
1242
  }
1267
1243
 
1268
1244
  /* kill_buffer is always big enough because max length of %lu is 15 */
1269
 
  sprintf(kill_buffer, "KILL /*!50000 QUERY */ %u", drizzleclient_thread_id(&drizzle));
1270
 
  drizzleclient_real_query(kill_drizzle, kill_buffer, strlen(kill_buffer));
1271
 
  drizzleclient_close(kill_drizzle);
 
1245
  sprintf(kill_buffer, "KILL /*!50000 QUERY */ %u", drizzle_thread_id(&drizzle));
 
1246
  drizzle_real_query(kill_drizzle, kill_buffer, strlen(kill_buffer));
 
1247
  drizzle_close(kill_drizzle);
1272
1248
  tee_fprintf(stdout, _("Query aborted by Ctrl+C\n"));
1273
1249
 
1274
1250
  interrupted_query= 1;
1281
1257
 
1282
1258
 
1283
1259
#if defined(HAVE_TERMIOS_H) && defined(GWINSZ_IN_SYS_IOCTL)
1284
 
void window_resize(int)
 
1260
RETSIGTYPE window_resize(int sig __attribute__((unused)))
1285
1261
{
1286
1262
  struct winsize window_size;
1287
1263
 
1308
1284
   (char**) &auto_vertical_output, (char**) &auto_vertical_output, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
1309
1285
  {"batch", 'B',
1310
1286
   N_("Don't use history file. Disable interactive behavior. (Enables --silent)"), 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
 
1287
  {"character-sets-dir", OPT_CHARSETS_DIR,
 
1288
   N_("Directory where character sets are."), (char**) &charsets_dir,
 
1289
   (char**) &charsets_dir, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
1311
1290
  {"column-type-info", OPT_COLUMN_TYPES, N_("Display column type information."),
1312
1291
   (char**) &column_types_flag, (char**) &column_types_flag,
1313
1292
   0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
1381
1360
  {"no-pager", OPT_NOPAGER,
1382
1361
   N_("Disable pager and print to stdout. See interactive help (\\h) also. WARNING: option deprecated; use --disable-pager instead."),
1383
1362
   0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
1384
 
  {"password", 'P',
 
1363
  {"password", 'p',
1385
1364
   N_("Password to use when connecting to server. If password is not given it's asked from the tty."),
1386
1365
   0, 0, 0, GET_STR, OPT_ARG, 0, 0, 0, 0, 0, 0},
1387
 
  {"port", 'p', N_("Port number to use for connection or 0 for default to, in order of preference, drizzle.cnf, $DRIZZLE_TCP_PORT, ")
 
1366
  {"port", 'P', N_("Port number to use for connection or 0 for default to, in order of preference, my.cnf, $DRIZZLE_TCP_PORT, ")
1388
1367
   N_("built-in default") " (" STRINGIFY_ARG(DRIZZLE_PORT) ").",
1389
 
   0, 0, 0, GET_UINT, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
1368
   (char**) &opt_drizzle_port,
 
1369
   (char**) &opt_drizzle_port, 0, GET_UINT, REQUIRED_ARG, 0, 0, 0, 0, 0,  0},
1390
1370
  {"prompt", OPT_PROMPT, N_("Set the drizzle prompt to this value."),
1391
1371
   (char**) &current_prompt, (char**) &current_prompt, 0, GET_STR_ALLOC,
1392
1372
   REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
1373
  {"protocol", OPT_DRIZZLE_PROTOCOL, N_("The protocol of connection (tcp,socket,pipe,memory)."),
 
1374
   0, 0, 0, GET_STR,  REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
1393
1375
  {"quick", 'q',
1394
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."),
1395
1377
   (char**) &quick, (char**) &quick, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
1455
1437
  {"show-warnings", OPT_SHOW_WARNINGS, N_("Show warnings after every statement."),
1456
1438
   (char**) &show_warnings, (char**) &show_warnings, 0, GET_BOOL, NO_ARG,
1457
1439
   0, 0, 0, 0, 0, 0},
1458
 
  {"show-progress-size", OPT_SHOW_PROGRESS_SIZE, N_("Number of lines before each import progress report."),
1459
 
   (char**) &show_progress_size, (char**) &show_progress_size, 0, GET_ULONG, REQUIRED_ARG,
1460
 
   0, 0, 0, 0, 0, 0},
1461
1440
  { 0, 0, 0, 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0}
1462
1441
};
1463
1442
 
1467
1446
  const char* readline= "readline";
1468
1447
 
1469
1448
  printf(_("%s  Ver %s Distrib %s, for %s (%s) using %s %s\n"),
1470
 
         my_progname, VER, drizzleclient_get_client_info(),
 
1449
         my_progname, VER, drizzle_get_client_info(),
1471
1450
         SYSTEM_TYPE, MACHINE_TYPE,
1472
1451
         readline, rl_library_version);
1473
1452
 
1474
1453
  if (version)
1475
1454
    return;
1476
 
  printf(_("Copyright (C) 2008 Sun Microsystems\n"
1477
 
           "This software comes with ABSOLUTELY NO WARRANTY. "
1478
 
           "This is free software,\n"
1479
 
           "and you are welcome to modify and redistribute it "
1480
 
           "under the GPL license\n"));
 
1455
  printf(_("\
 
1456
Copyright (C) 2000-2008 MySQL AB\n                                      \
 
1457
This software comes with ABSOLUTELY NO WARRANTY. This is free software,\n \
 
1458
and you are welcome to modify and redistribute it under the GPL license\n"));
1481
1459
  printf(_("Usage: %s [OPTIONS] [database]\n"), my_progname);
1482
1460
  my_print_help(my_long_options);
1483
 
  print_defaults("drizzle", load_default_groups);
 
1461
  print_defaults("my", load_default_groups);
1484
1462
  my_print_variables(my_long_options);
1485
1463
}
1486
1464
 
1487
1465
 
1488
 
extern "C" bool
1489
 
get_one_option(int optid, const struct my_option *, char *argument)
 
1466
bool
 
1467
get_one_option(int optid, const struct my_option *opt __attribute__((unused)),
 
1468
               char *argument)
1490
1469
{
1491
 
  char *endchar= NULL;
1492
 
  uint64_t temp_drizzle_port= 0;
1493
 
 
1494
1470
  switch(optid) {
 
1471
  case OPT_CHARSETS_DIR:
 
1472
    strmake(drizzle_charsets_dir, argument, sizeof(drizzle_charsets_dir) - 1);
 
1473
    charsets_dir = drizzle_charsets_dir;
 
1474
    break;
1495
1475
  case  OPT_DEFAULT_CHARSET:
1496
1476
    default_charset_used= 1;
1497
1477
    break;
1498
1478
  case OPT_DELIMITER:
1499
1479
    if (argument == disabled_my_option)
1500
1480
    {
1501
 
      strcpy(delimiter, DEFAULT_DELIMITER);
 
1481
      my_stpcpy(delimiter, DEFAULT_DELIMITER);
1502
1482
    }
1503
1483
    else
1504
1484
    {
1505
1485
      /* Check that delimiter does not contain a backslash */
1506
1486
      if (!strstr(argument, "\\"))
1507
1487
      {
1508
 
        strncpy(delimiter, argument, sizeof(delimiter) - 1);
 
1488
        strmake(delimiter, argument, sizeof(delimiter) - 1);
1509
1489
      }
1510
1490
      else
1511
1491
      {
1512
1492
        put_info(_("DELIMITER cannot contain a backslash character"),
1513
1493
                 INFO_ERROR,0,0);
1514
 
        return false;
 
1494
        return 0;
1515
1495
      }
1516
1496
    }
1517
1497
    delimiter_length= (uint)strlen(delimiter);
1518
1498
    delimiter_str= delimiter;
1519
1499
    break;
1520
1500
  case OPT_LOCAL_INFILE:
1521
 
    using_opt_local_infile= 1;
 
1501
    using_opt_local_infile=1;
1522
1502
    break;
1523
1503
  case OPT_TEE:
1524
1504
    if (argument == disabled_my_option)
1543
1523
      if (argument && strlen(argument))
1544
1524
      {
1545
1525
        default_pager_set= 1;
1546
 
        strncpy(pager, argument, sizeof(pager) - 1);
1547
 
        strcpy(default_pager, pager);
 
1526
        strmake(pager, argument, sizeof(pager) - 1);
 
1527
        my_stpcpy(default_pager, pager);
1548
1528
      }
1549
1529
      else if (default_pager_set)
1550
 
        strcpy(pager, default_pager);
 
1530
        my_stpcpy(pager, default_pager);
1551
1531
      else
1552
1532
        opt_nopager= 1;
1553
1533
    }
1580
1560
      one_database= skip_updates= 1;
1581
1561
    break;
1582
1562
  case 'p':
1583
 
    temp_drizzle_port= (uint64_t) strtoul(argument, &endchar, 10);
1584
 
    /* if there is an alpha character this is not a valid port */
1585
 
    if (strlen(endchar) != 0)
1586
 
    {
1587
 
      put_info(_("Non-integer value supplied for port.  If you are trying to enter a password please use --password instead."), INFO_ERROR, 0, 0);
1588
 
      return false;
1589
 
    }
1590
 
    /* If the port number is > 65535 it is not a valid port
1591
 
       This also helps with potential data loss casting unsigned long to a
1592
 
       uint32_t. */
1593
 
    if ((temp_drizzle_port == 0) || (temp_drizzle_port > 65535))
1594
 
    {
1595
 
      put_info(_("Value supplied for port is not valid."), INFO_ERROR, 0, 0);
1596
 
      return false;
1597
 
    }
1598
 
    else
1599
 
    {
1600
 
      opt_drizzle_port= (uint32_t) temp_drizzle_port;
1601
 
    }
1602
 
    break;
1603
 
  case 'P':
1604
 
    /* Don't require password */
1605
1563
    if (argument == disabled_my_option)
1606
 
    {
1607
 
      argument= (char*) "";
1608
 
    }
 
1564
      argument= (char*) "";      // Don't require password
1609
1565
    if (argument)
1610
1566
    {
1611
1567
      char *start= argument;
1612
1568
      free(opt_password);
1613
1569
      opt_password= strdup(argument);
1614
 
      while (*argument)
1615
 
      {
1616
 
        /* Overwriting password with 'x' */
1617
 
        *argument++= 'x';
1618
 
      }
 
1570
      while (*argument) *argument++= 'x';        // Destroy argument
1619
1571
      if (*start)
1620
 
      {
1621
 
        start[1]= 0;
1622
 
      }
 
1572
        start[1]=0 ;
1623
1573
      tty_password= 0;
1624
1574
    }
1625
1575
    else
1626
 
    {
1627
1576
      tty_password= 1;
1628
 
    }
1629
1577
    break;
1630
1578
  case 's':
1631
1579
    if (argument == disabled_my_option)
1644
1592
    status.add_to_history= 0;
1645
1593
    set_if_bigger(opt_silent,1);                         // more silent
1646
1594
    break;
 
1595
    break;
1647
1596
  case 'V':
1648
1597
    usage(1);
1649
1598
    exit(0);
1660
1609
{
1661
1610
  char *tmp, *pagpoint;
1662
1611
  int ho_error;
1663
 
  const DRIZZLE_PARAMETERS *drizzle_params= drizzleclient_get_parameters();
 
1612
  const DRIZZLE_PARAMETERS *drizzle_params= drizzle_get_parameters();
1664
1613
 
1665
1614
  tmp= (char *) getenv("DRIZZLE_HOST");
1666
1615
  if (tmp)
1669
1618
  pagpoint= getenv("PAGER");
1670
1619
  if (!((char*) (pagpoint)))
1671
1620
  {
1672
 
    strcpy(pager, "stdout");
 
1621
    my_stpcpy(pager, "stdout");
1673
1622
    opt_nopager= 1;
1674
1623
  }
1675
1624
  else
1676
 
    strcpy(pager, pagpoint);
1677
 
  strcpy(default_pager, pager);
 
1625
    my_stpcpy(pager, pagpoint);
 
1626
  my_stpcpy(default_pager, pager);
1678
1627
 
1679
1628
  opt_max_allowed_packet= *drizzle_params->p_max_allowed_packet;
1680
1629
  opt_net_buffer_length= *drizzle_params->p_net_buffer_length;
1687
1636
 
1688
1637
  if (status.batch) /* disable pager and outfile in this case */
1689
1638
  {
1690
 
    strcpy(default_pager, "stdout");
1691
 
    strcpy(pager, "stdout");
 
1639
    my_stpcpy(default_pager, "stdout");
 
1640
    my_stpcpy(pager, "stdout");
1692
1641
    opt_nopager= 1;
1693
1642
    default_pager_set= 0;
1694
1643
    opt_outfile= 0;
1708
1657
    current_db= strdup(*argv);
1709
1658
  }
1710
1659
  if (tty_password)
1711
 
    opt_password= drizzleclient_get_tty_password(NULL);
 
1660
    opt_password= get_tty_password(NULL);
1712
1661
  if (debug_info_flag)
1713
1662
    my_end_arg= MY_CHECK_ERROR | MY_GIVE_INFO;
1714
1663
  if (debug_check_flag)
1742
1691
          (unsigned char) line[2] == 0xBF)
1743
1692
        line+= 3;
1744
1693
      line_number++;
1745
 
      if (show_progress_size > 0)
1746
 
      {
1747
 
        if ((line_number % show_progress_size) == 0)
1748
 
          fprintf(stderr, _("Processing line: %"PRIu32"\n"), line_number);
1749
 
      }
1750
1694
      if (!glob_buffer->empty())
1751
1695
        status.query_start_line=line_number;
1752
1696
    }
2137
2081
 
2138
2082
 
2139
2083
static char **mysql_completion (const char *text, int start, int end);
2140
 
extern "C" char *new_command_generator(const char *text, int);
 
2084
static char *new_command_generator(const char *text, int);
2141
2085
 
2142
2086
/*
2143
2087
  Tell the GNU Readline library how to complete.  We want to try to complete
2144
2088
  on command names if this is the first word in the line, or on filenames
2145
2089
  if not.
2146
2090
*/
2147
 
static char *no_completion(const char *, int)
 
2091
static char *no_completion(const char * a __attribute__((unused)),
 
2092
                           int b __attribute__((unused)))
2148
2093
{
2149
2094
  /* No filename completion */
2150
2095
  return 0;
2239
2184
  entire line in case we want to do some simple parsing.  Return the
2240
2185
  array of matches, or NULL if there aren't any.
2241
2186
*/
2242
 
char **mysql_completion (const char *text, int, int)
 
2187
char **mysql_completion (const char *text,
 
2188
                        int start __attribute__((unused)),
 
2189
                        int end __attribute__((unused)))
2243
2190
{
2244
2191
  if (!status.batch && !quick)
2245
2192
    return rl_completion_matches(text, new_command_generator);
2247
2194
    return (char**) 0;
2248
2195
}
2249
2196
 
2250
 
extern "C"
2251
 
char *new_command_generator(const char *text,int state)
 
2197
 
 
2198
static char *new_command_generator(const char *text,int state)
2252
2199
{
2253
2200
  static int textlen;
2254
2201
  char *ptr;
2361
2308
  /* hash Drizzle functions (to be implemented) */
2362
2309
 
2363
2310
  /* hash all database names */
2364
 
  if (drizzleclient_query(&drizzle,"show databases") == 0)
 
2311
  if (drizzle_query(&drizzle,"show databases") == 0)
2365
2312
  {
2366
 
    if (!(databases = drizzleclient_store_result(&drizzle)))
2367
 
      put_info(drizzleclient_error(&drizzle),INFO_INFO,0,0);
 
2313
    if (!(databases = drizzle_store_result(&drizzle)))
 
2314
      put_info(drizzle_error(&drizzle),INFO_INFO,0,0);
2368
2315
    else
2369
2316
    {
2370
 
      while ((database_row=drizzleclient_fetch_row(databases)))
 
2317
      while ((database_row=drizzle_fetch_row(databases)))
2371
2318
      {
2372
2319
        char *str=strdup_root(&hash_mem_root, (char*) database_row[0]);
2373
2320
        if (str)
2374
2321
          add_word(&ht,(char*) str);
2375
2322
      }
2376
 
      drizzleclient_free_result(databases);
 
2323
      drizzle_free_result(databases);
2377
2324
    }
2378
2325
  }
2379
2326
  /* hash all table names */
2380
 
  if (drizzleclient_query(&drizzle,"show tables")==0)
 
2327
  if (drizzle_query(&drizzle,"show tables")==0)
2381
2328
  {
2382
 
    if (!(tables = drizzleclient_store_result(&drizzle)))
2383
 
      put_info(drizzleclient_error(&drizzle),INFO_INFO,0,0);
 
2329
    if (!(tables = drizzle_store_result(&drizzle)))
 
2330
      put_info(drizzle_error(&drizzle),INFO_INFO,0,0);
2384
2331
    else
2385
2332
    {
2386
 
      if (drizzleclient_num_rows(tables) > 0 && !opt_silent && write_info)
 
2333
      if (drizzle_num_rows(tables) > 0 && !opt_silent && write_info)
2387
2334
      {
2388
2335
        tee_fprintf(stdout, _("\
2389
2336
Reading table information for completion of table and column names\n    \
2390
2337
You can turn off this feature to get a quicker startup with -A\n\n"));
2391
2338
      }
2392
 
      while ((table_row=drizzleclient_fetch_row(tables)))
 
2339
      while ((table_row=drizzle_fetch_row(tables)))
2393
2340
      {
2394
2341
        char *str=strdup_root(&hash_mem_root, (char*) table_row[0]);
2395
2342
        if (str &&
2404
2351
  {
2405
2352
    return;
2406
2353
  }
2407
 
  drizzleclient_data_seek(tables,0);
 
2354
  drizzle_data_seek(tables,0);
2408
2355
  if (!(field_names= (char ***) alloc_root(&hash_mem_root,sizeof(char **) *
2409
 
                                           (uint) (drizzleclient_num_rows(tables)+1))))
 
2356
                                           (uint) (drizzle_num_rows(tables)+1))))
2410
2357
  {
2411
 
    drizzleclient_free_result(tables);
 
2358
    drizzle_free_result(tables);
2412
2359
    return;
2413
2360
  }
2414
2361
  i=0;
2415
 
  while ((table_row=drizzleclient_fetch_row(tables)))
 
2362
  while ((table_row=drizzle_fetch_row(tables)))
2416
2363
  {
2417
 
    if ((fields=drizzleclient_list_fields(&drizzle,(const char*) table_row[0],NULL)))
 
2364
    if ((fields=drizzle_list_fields(&drizzle,(const char*) table_row[0],NULL)))
2418
2365
    {
2419
 
      num_fields=drizzleclient_num_fields(fields);
 
2366
      num_fields=drizzle_num_fields(fields);
2420
2367
      if (!(field_names[i] = (char **) alloc_root(&hash_mem_root,
2421
2368
                                                  sizeof(char *) *
2422
2369
                                                  (num_fields*2+1))))
2423
2370
      {
2424
 
        drizzleclient_free_result(fields);
 
2371
        drizzle_free_result(fields);
2425
2372
        break;
2426
2373
      }
2427
2374
      field_names[i][num_fields*2]= '\0';
2428
2375
      j=0;
2429
 
      while ((sql_field=drizzleclient_fetch_field(fields)))
 
2376
      while ((sql_field=drizzle_fetch_field(fields)))
2430
2377
      {
2431
2378
        sprintf(buf,"%.64s.%.64s",table_row[0],sql_field->name);
2432
2379
        field_names[i][j] = strdup_root(&hash_mem_root,buf);
2438
2385
          add_word(&ht,field_names[i][num_fields+j]);
2439
2386
        j++;
2440
2387
      }
2441
 
      drizzleclient_free_result(fields);
 
2388
      drizzle_free_result(fields);
2442
2389
    }
2443
2390
    else
2444
2391
      field_names[i]= 0;
2445
2392
 
2446
2393
    i++;
2447
2394
  }
2448
 
  drizzleclient_free_result(tables);
 
2395
  drizzle_free_result(tables);
2449
2396
  field_names[i]=0;        // End pointer
2450
2397
  return;
2451
2398
}
2500
2447
  free(current_db);
2501
2448
  current_db= NULL;
2502
2449
  /* In case of error below current_db will be NULL */
2503
 
  if (!drizzleclient_query(&drizzle, "SELECT DATABASE()") &&
2504
 
      (res= drizzleclient_use_result(&drizzle)))
 
2450
  if (!drizzle_query(&drizzle, "SELECT DATABASE()") &&
 
2451
      (res= drizzle_use_result(&drizzle)))
2505
2452
  {
2506
 
    DRIZZLE_ROW row= drizzleclient_fetch_row(res);
 
2453
    DRIZZLE_ROW row= drizzle_fetch_row(res);
2507
2454
    if (row[0])
2508
2455
      current_db= strdup(row[0]);
2509
 
    drizzleclient_free_result(res);
 
2456
    drizzle_free_result(res);
2510
2457
  }
2511
2458
}
2512
2459
 
2514
2461
 The different commands
2515
2462
***************************************************************************/
2516
2463
 
2517
 
int drizzleclient_real_query_for_lazy(const char *buf, int length)
 
2464
int drizzle_real_query_for_lazy(const char *buf, int length)
2518
2465
{
2519
2466
  for (uint retry=0;; retry++)
2520
2467
  {
2521
2468
    int error;
2522
 
    if (!drizzleclient_real_query(&drizzle,buf,length))
 
2469
    if (!drizzle_real_query(&drizzle,buf,length))
2523
2470
      return 0;
2524
2471
    error= put_error(&drizzle);
2525
 
    if (drizzleclient_errno(&drizzle) != CR_SERVER_GONE_ERROR || retry > 1 ||
 
2472
    if (drizzle_errno(&drizzle) != CR_SERVER_GONE_ERROR || retry > 1 ||
2526
2473
        !opt_reconnect)
2527
2474
      return error;
2528
2475
    if (reconnect())
2530
2477
  }
2531
2478
}
2532
2479
 
2533
 
int drizzleclient_store_result_for_lazy(DRIZZLE_RES **result)
 
2480
int drizzle_store_result_for_lazy(DRIZZLE_RES **result)
2534
2481
{
2535
 
  if ((*result=drizzleclient_store_result(&drizzle)))
 
2482
  if ((*result=drizzle_store_result(&drizzle)))
2536
2483
    return 0;
2537
2484
 
2538
 
  if (drizzleclient_error(&drizzle)[0])
 
2485
  if (drizzle_error(&drizzle)[0])
2539
2486
    return put_error(&drizzle);
2540
2487
  return 0;
2541
2488
}
2542
2489
 
 
2490
static void print_help_item(DRIZZLE_ROW *cur, int num_name, int num_cat, char *last_char)
 
2491
{
 
2492
  char ccat= (*cur)[num_cat][0];
 
2493
  if (*last_char != ccat)
 
2494
  {
 
2495
    put_info(ccat == 'Y' ? _("categories:") : _("topics:"), INFO_INFO,0,0);
 
2496
    *last_char= ccat;
 
2497
  }
 
2498
  tee_fprintf(PAGER, "   %s\n", (*cur)[num_name]);
 
2499
}
 
2500
 
 
2501
 
 
2502
static int com_server_help(string *buffer,
 
2503
                           const char *line __attribute__((unused)),
 
2504
                           char *help_arg)
 
2505
{
 
2506
  DRIZZLE_ROW cur;
 
2507
  const char *server_cmd= buffer->c_str();
 
2508
  char cmd_buf[100];
 
2509
  DRIZZLE_RES *result;
 
2510
  int error;
 
2511
 
 
2512
  if (help_arg[0] != '\'')
 
2513
  {
 
2514
    char *end_arg= strchr(help_arg, '\0');
 
2515
    if(--end_arg)
 
2516
    {
 
2517
      while (my_isspace(charset_info,*end_arg))
 
2518
        end_arg--;
 
2519
      *++end_arg= '\0';
 
2520
    }
 
2521
    (void) strxnmov(cmd_buf, sizeof(cmd_buf), "help '", help_arg, "'", NULL);
 
2522
    server_cmd= cmd_buf;
 
2523
  }
 
2524
 
 
2525
  if (!connected && reconnect())
 
2526
    return 1;
 
2527
 
 
2528
  if ((error= drizzle_real_query_for_lazy(server_cmd,(int)strlen(server_cmd))) ||
 
2529
      (error= drizzle_store_result_for_lazy(&result)))
 
2530
    return error;
 
2531
 
 
2532
  if (result)
 
2533
  {
 
2534
    unsigned int num_fields= drizzle_num_fields(result);
 
2535
    uint64_t num_rows= drizzle_num_rows(result);
 
2536
    drizzle_fetch_fields(result);
 
2537
    if (num_fields==3 && num_rows==1)
 
2538
    {
 
2539
      if (!(cur= drizzle_fetch_row(result)))
 
2540
      {
 
2541
        error= -1;
 
2542
        goto err;
 
2543
      }
 
2544
 
 
2545
      init_pager();
 
2546
      tee_fprintf(PAGER,   _("Name: \'%s\'\n"), cur[0]);
 
2547
      tee_fprintf(PAGER,   _("Description:\n%s"), cur[1]);
 
2548
      if (cur[2] && *((char*)cur[2]))
 
2549
        tee_fprintf(PAGER, _("Examples:\n%s"), cur[2]);
 
2550
      tee_fprintf(PAGER,   "\n");
 
2551
      end_pager();
 
2552
    }
 
2553
    else if (num_fields >= 2 && num_rows)
 
2554
    {
 
2555
      init_pager();
 
2556
      char last_char= 0;
 
2557
 
 
2558
      int num_name= 0, num_cat= 0;
 
2559
 
 
2560
      if (num_fields == 2)
 
2561
      {
 
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);
 
2564
        num_name= 0;
 
2565
        num_cat= 1;
 
2566
      }
 
2567
      else if ((cur= drizzle_fetch_row(result)))
 
2568
      {
 
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);
 
2571
        num_name= 1;
 
2572
        num_cat= 2;
 
2573
        print_help_item(&cur,1,2,&last_char);
 
2574
      }
 
2575
 
 
2576
      while ((cur= drizzle_fetch_row(result)))
 
2577
        print_help_item(&cur,num_name,num_cat,&last_char);
 
2578
      tee_fprintf(PAGER, "\n");
 
2579
      end_pager();
 
2580
    }
 
2581
    else
 
2582
    {
 
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);
 
2585
    }
 
2586
  }
 
2587
 
 
2588
err:
 
2589
  drizzle_free_result(result);
 
2590
  return error;
 
2591
}
 
2592
 
2543
2593
static int
2544
 
com_help(string *buffer, const char *)
 
2594
com_help(string *buffer __attribute__((unused)),
 
2595
         const char *line __attribute__((unused)))
2545
2596
{
2546
2597
  register int i, j;
2547
 
  char buff[32], *end;
 
2598
  char * help_arg= strchr(line,' '), buff[32], *end;
 
2599
  if (help_arg)
 
2600
  {
 
2601
    while (my_isspace(charset_info,*help_arg))
 
2602
      help_arg++;
 
2603
    if (*help_arg) return com_server_help(buffer,line,help_arg);
 
2604
  }
2548
2605
 
2549
2606
  put_info(_("List of all Drizzle commands:"), INFO_INFO,0,0);
2550
2607
  if (!named_cmds)
2551
2608
    put_info(_("Note that all text commands must be first on line and end with ';'"),INFO_INFO,0,0);
2552
2609
  for (i = 0; commands[i].name; i++)
2553
2610
  {
2554
 
    end= strcpy(buff, commands[i].name);
2555
 
    end+= strlen(commands[i].name);
 
2611
    end= my_stpcpy(buff, commands[i].name);
2556
2612
    for (j= (int)strlen(commands[i].name); j < 10; j++)
2557
 
      end= strcpy(end, " ")+1;
 
2613
      end= my_stpcpy(end, " ");
2558
2614
    if (commands[i].func)
2559
2615
      tee_fprintf(stdout, "%s(\\%c) %s\n", buff,
2560
2616
                  commands[i].cmd_char, _(commands[i].doc));
2561
2617
  }
2562
 
  tee_fprintf(stdout, "\n");
2563
 
  buffer->clear();
 
2618
  if (connected && drizzle_get_server_version(&drizzle) >= 40100)
 
2619
    put_info(_("\nFor server side help, type 'help contents'\n"), INFO_INFO,0,0);
2564
2620
  return 0;
2565
2621
}
2566
2622
 
2567
2623
 
2568
2624
static int
2569
 
com_clear(string *buffer, const char *)
 
2625
com_clear(string *buffer,
 
2626
          const char *line __attribute__((unused)))
2570
2627
{
2571
2628
  if (status.add_to_history)
2572
2629
    fix_history(buffer);
2582
2639
  1  if fatal error
2583
2640
*/
2584
2641
static int
2585
 
com_go(string *buffer, const char *)
 
2642
com_go(string *buffer,
 
2643
       const char *line __attribute__((unused)))
2586
2644
{
2587
2645
  char          buff[200]; /* about 110 chars used so far */
2588
2646
  char          time_buff[52+3+1]; /* time max + space&parens + NUL */
2622
2680
 
2623
2681
  timer=start_timer();
2624
2682
  executing_query= 1;
2625
 
  error= drizzleclient_real_query_for_lazy(buffer->c_str(),buffer->length());
 
2683
  error= drizzle_real_query_for_lazy(buffer->c_str(),buffer->length());
2626
2684
 
2627
2685
  if (status.add_to_history)
2628
2686
  {
2642
2700
 
2643
2701
    if (quick)
2644
2702
    {
2645
 
      if (!(result=drizzleclient_use_result(&drizzle)) && drizzleclient_field_count(&drizzle))
 
2703
      if (!(result=drizzle_use_result(&drizzle)) && drizzle_field_count(&drizzle))
2646
2704
      {
2647
2705
        error= put_error(&drizzle);
2648
2706
        goto end;
2650
2708
    }
2651
2709
    else
2652
2710
    {
2653
 
      error= drizzleclient_store_result_for_lazy(&result);
 
2711
      error= drizzle_store_result_for_lazy(&result);
2654
2712
      if (error)
2655
2713
        goto end;
2656
2714
    }
2663
2721
    /* Every branch must truncate  buff . */
2664
2722
    if (result)
2665
2723
    {
2666
 
      if (!drizzleclient_num_rows(result) && ! quick && !column_types_flag)
 
2724
      if (!drizzle_num_rows(result) && ! quick && !column_types_flag)
2667
2725
      {
2668
 
        strcpy(buff, _("Empty set"));
 
2726
        my_stpcpy(buff, _("Empty set"));
2669
2727
      }
2670
2728
      else
2671
2729
      {
2679
2737
          print_table_data(result);
2680
2738
        sprintf(buff,
2681
2739
                ngettext("%ld row in set","%ld rows in set",
2682
 
                         (long) drizzleclient_num_rows(result)),
2683
 
                (long) drizzleclient_num_rows(result));
 
2740
                         (long) drizzle_num_rows(result)),
 
2741
                (long) drizzle_num_rows(result));
2684
2742
        end_pager();
2685
 
        if (drizzleclient_errno(&drizzle))
 
2743
        if (drizzle_errno(&drizzle))
2686
2744
          error= put_error(&drizzle);
2687
2745
      }
2688
2746
    }
2689
 
    else if (drizzleclient_affected_rows(&drizzle) == ~(uint64_t) 0)
2690
 
      strcpy(buff,_("Query OK"));
 
2747
    else if (drizzle_affected_rows(&drizzle) == ~(uint64_t) 0)
 
2748
      my_stpcpy(buff,_("Query OK"));
2691
2749
    else
2692
2750
      sprintf(buff, ngettext("Query OK, %ld row affected",
2693
2751
                             "Query OK, %ld rows affected",
2694
 
                             (long) drizzleclient_affected_rows(&drizzle)),
2695
 
              (long) drizzleclient_affected_rows(&drizzle));
 
2752
                             (long) drizzle_affected_rows(&drizzle)),
 
2753
              (long) drizzle_affected_rows(&drizzle));
2696
2754
 
2697
2755
    pos= strchr(buff, '\0');
2698
 
    if ((warnings= drizzleclient_warning_count(&drizzle)))
 
2756
    if ((warnings= drizzle_warning_count(&drizzle)))
2699
2757
    {
2700
2758
      *pos++= ',';
2701
2759
      *pos++= ' ';
2702
 
      pos= int10_to_str(warnings, pos, 10);
2703
 
      pos= strcpy(pos, " warning")+8;
 
2760
      pos=int10_to_str(warnings, pos, 10);
 
2761
      pos=my_stpcpy(pos, " warning");
2704
2762
      if (warnings != 1)
2705
2763
        *pos++= 's';
2706
2764
    }
2707
 
    strcpy(pos, time_buff);
 
2765
    my_stpcpy(pos, time_buff);
2708
2766
    put_info(buff,INFO_RESULT,0,0);
2709
 
    if (drizzleclient_info(&drizzle))
2710
 
      put_info(drizzleclient_info(&drizzle),INFO_RESULT,0,0);
 
2767
    if (drizzle_info(&drizzle))
 
2768
      put_info(drizzle_info(&drizzle),INFO_RESULT,0,0);
2711
2769
    put_info("",INFO_RESULT,0,0);      // Empty row
2712
2770
 
2713
 
    if (result && !drizzleclient_eof(result))  /* Something wrong when using quick */
 
2771
    if (result && !drizzle_eof(result))  /* Something wrong when using quick */
2714
2772
      error= put_error(&drizzle);
2715
2773
    else if (unbuffered)
2716
2774
      fflush(stdout);
2717
 
    drizzleclient_free_result(result);
2718
 
  } while (!(err= drizzleclient_next_result(&drizzle)));
 
2775
    drizzle_free_result(result);
 
2776
  } while (!(err= drizzle_next_result(&drizzle)));
2719
2777
  if (err >= 1)
2720
2778
    error= put_error(&drizzle);
2721
2779
 
2766
2824
    return;
2767
2825
  }
2768
2826
  OUTFILE = new_outfile;
2769
 
  strncpy(outfile, file_name, FN_REFLEN-1);
 
2827
  strmake(outfile, file_name, FN_REFLEN-1);
2770
2828
  tee_fprintf(stdout, "Logging to file '%s'\n", file_name);
2771
2829
  opt_outfile= 1;
2772
2830
  return;
2798
2856
{
2799
2857
  switch (type) {
2800
2858
    case DRIZZLE_TYPE_BLOB:        return "BLOB";
2801
 
    case DRIZZLE_TYPE_DATE:        return "DATE";
 
2859
    case DRIZZLE_TYPE_NEWDATE:        return "DATE";
2802
2860
    case DRIZZLE_TYPE_DATETIME:    return "DATETIME";
2803
2861
    case DRIZZLE_TYPE_NEWDECIMAL:  return "DECIMAL";
2804
2862
    case DRIZZLE_TYPE_DOUBLE:      return "DOUBLE";
2809
2867
    case DRIZZLE_TYPE_TIME:        return "TIME";
2810
2868
    case DRIZZLE_TYPE_TIMESTAMP:   return "TIMESTAMP";
2811
2869
    case DRIZZLE_TYPE_TINY:        return "TINY";
2812
 
    case DRIZZLE_TYPE_VIRTUAL:     return "VIRTUAL";
2813
2870
    default:                     return "?-unknown-?";
2814
2871
  }
2815
2872
}
2819
2876
  char *s=buf;
2820
2877
  *s=0;
2821
2878
#define ff2s_check_flag(X)                                              \
2822
 
  if (f & X ## _FLAG) { s=strcpy(s, # X " ")+strlen(# X " "); \
2823
 
                        f &= ~ X ## _FLAG; }
 
2879
  if (f & X ## _FLAG) { s=my_stpcpy(s, # X " "); f &= ~ X ## _FLAG; }
2824
2880
  ff2s_check_flag(NOT_NULL);
2825
2881
  ff2s_check_flag(PRI_KEY);
2826
2882
  ff2s_check_flag(UNIQUE_KEY);
2851
2907
  DRIZZLE_FIELD   *field;
2852
2908
  uint i=0;
2853
2909
 
2854
 
  while ((field = drizzleclient_fetch_field(result)))
 
2910
  while ((field = drizzle_fetch_field(result)))
2855
2911
  {
2856
2912
    tee_fprintf(PAGER, "Field %3u:  `%s`\n"
2857
2913
                "Catalog:    `%s`\n"
2882
2938
  DRIZZLE_FIELD   *field;
2883
2939
  bool          *num_flag;
2884
2940
  string separator;
2885
 
 
 
2941
  
2886
2942
  separator.reserve(256);
2887
2943
 
2888
 
  num_flag=(bool*) malloc(sizeof(bool)*drizzleclient_num_fields(result));
 
2944
  num_flag=(bool*) my_malloc(sizeof(bool)*drizzle_num_fields(result),
 
2945
                             MYF(MY_WME));
2889
2946
  if (column_types_flag)
2890
2947
  {
2891
2948
    print_field_types(result);
2892
 
    if (!drizzleclient_num_rows(result))
 
2949
    if (!drizzle_num_rows(result))
2893
2950
      return;
2894
 
    drizzleclient_field_seek(result,0);
 
2951
    drizzle_field_seek(result,0);
2895
2952
  }
2896
2953
  separator.append("+");
2897
 
  while ((field = drizzleclient_fetch_field(result)))
 
2954
  while ((field = drizzle_fetch_field(result)))
2898
2955
  {
2899
 
    uint32_t x, length= 0;
2900
 
 
2901
 
    if (column_names)
2902
 
    {
2903
 
      /* Check if the max_byte value is really the maximum in terms
2904
 
         of visual length since multibyte characters can affect the
2905
 
         length of the separator. */
2906
 
      length= charset_info->cset->numcells(charset_info,
2907
 
                                           field->name,
2908
 
                                           field->name+field->name_length);
2909
 
 
2910
 
      if (field->name_length == field->max_length)
2911
 
      {
2912
 
        if (length < field->max_length)
2913
 
          field->max_length= length;
2914
 
      }
2915
 
      else
2916
 
      {
2917
 
        length= field->name_length;
2918
 
      }
2919
 
    }
2920
 
  
 
2956
    uint32_t length= column_names ? field->name_length : 0;
2921
2957
    if (quick)
2922
2958
      length=max(length,field->length);
2923
2959
    else
2926
2962
      // Room for "NULL"
2927
2963
      length=4;
2928
2964
    field->max_length=length;
2929
 
 
 
2965
    uint x;
2930
2966
    for (x=0; x< (length+2); x++)
2931
2967
      separator.append("-");
2932
2968
    separator.append("+");
2935
2971
  tee_puts((char*) separator.c_str(), PAGER);
2936
2972
  if (column_names)
2937
2973
  {
2938
 
    drizzleclient_field_seek(result,0);
 
2974
    drizzle_field_seek(result,0);
2939
2975
    (void) tee_fputs("|", PAGER);
2940
 
    for (uint off=0; (field = drizzleclient_fetch_field(result)) ; off++)
 
2976
    for (uint off=0; (field = drizzle_fetch_field(result)) ; off++)
2941
2977
    {
2942
2978
      uint name_length= (uint) strlen(field->name);
2943
2979
      uint numcells= charset_info->cset->numcells(charset_info,
2947
2983
      tee_fprintf(PAGER, " %-*s |",(int) min(display_length,
2948
2984
                                             MAX_COLUMN_LENGTH),
2949
2985
                  field->name);
2950
 
      num_flag[off]= ((field->type <= DRIZZLE_TYPE_LONGLONG) ||
 
2986
      num_flag[off]= ((field->type <= DRIZZLE_TYPE_LONGLONG) || 
2951
2987
                      (field->type == DRIZZLE_TYPE_NEWDECIMAL));
2952
2988
    }
2953
2989
    (void) tee_fputs("\n", PAGER);
2954
2990
    tee_puts((char*) separator.c_str(), PAGER);
2955
2991
  }
2956
2992
 
2957
 
  while ((cur= drizzleclient_fetch_row(result)))
 
2993
  while ((cur= drizzle_fetch_row(result)))
2958
2994
  {
2959
2995
    if (interrupted_query)
2960
2996
      break;
2961
 
    uint32_t *lengths= drizzleclient_fetch_lengths(result);
 
2997
    uint32_t *lengths= drizzle_fetch_lengths(result);
2962
2998
    (void) tee_fputs("| ", PAGER);
2963
 
    drizzleclient_field_seek(result, 0);
2964
 
    for (uint off= 0; off < drizzleclient_num_fields(result); off++)
 
2999
    drizzle_field_seek(result, 0);
 
3000
    for (uint off= 0; off < drizzle_num_fields(result); off++)
2965
3001
    {
2966
3002
      const char *buffer;
2967
3003
      uint data_length;
2980
3016
        data_length= (uint) lengths[off];
2981
3017
      }
2982
3018
 
2983
 
      field= drizzleclient_fetch_field(result);
 
3019
      field= drizzle_fetch_field(result);
2984
3020
      field_max_length= field->max_length;
2985
3021
 
2986
3022
      /*
3030
3066
*/
3031
3067
static int get_field_disp_length(DRIZZLE_FIELD *field)
3032
3068
{
3033
 
  uint32_t length= column_names ? field->name_length : 0;
 
3069
  uint length= column_names ? field->name_length : 0;
3034
3070
 
3035
3071
  if (quick)
3036
3072
    length= max(length, field->length);
3057
3093
  DRIZZLE_FIELD *field;
3058
3094
  DRIZZLE_FIELD_OFFSET offset;
3059
3095
 
3060
 
  offset= drizzleclient_field_tell(result);
 
3096
  offset= drizzle_field_tell(result);
3061
3097
  assert(offset == 0);
3062
3098
 
3063
 
  while ((field= drizzleclient_fetch_field(result)) != NULL)
 
3099
  while ((field= drizzle_fetch_field(result)) != NULL)
3064
3100
    len+= get_field_disp_length(field) + 3; /* plus bar, space, & final space */
3065
3101
 
3066
 
  (void) drizzleclient_field_seek(result, offset);
 
3102
  (void) drizzle_field_seek(result, offset);
3067
3103
 
3068
3104
  return len + 1; /* plus final bar. */
3069
3105
}
3105
3141
  uint    max_length=0;
3106
3142
  DRIZZLE_FIELD  *field;
3107
3143
 
3108
 
  while ((field = drizzleclient_fetch_field(result)))
 
3144
  while ((field = drizzle_fetch_field(result)))
3109
3145
  {
3110
3146
    uint length= field->name_length;
3111
3147
    if (length > max_length)
3113
3149
    field->max_length=length;
3114
3150
  }
3115
3151
 
3116
 
  drizzleclient_field_seek(result,0);
3117
 
  for (uint row_count=1; (cur= drizzleclient_fetch_row(result)); row_count++)
 
3152
  drizzle_field_seek(result,0);
 
3153
  for (uint row_count=1; (cur= drizzle_fetch_row(result)); row_count++)
3118
3154
  {
3119
3155
    if (interrupted_query)
3120
3156
      break;
3121
 
    drizzleclient_field_seek(result,0);
 
3157
    drizzle_field_seek(result,0);
3122
3158
    tee_fprintf(PAGER,
3123
3159
                "*************************** %d. row ***************************\n", row_count);
3124
 
    for (uint off=0; off < drizzleclient_num_fields(result); off++)
 
3160
    for (uint off=0; off < drizzle_num_fields(result); off++)
3125
3161
    {
3126
 
      field= drizzleclient_fetch_field(result);
 
3162
      field= drizzle_fetch_field(result);
3127
3163
      tee_fprintf(PAGER, "%*s: ",(int) max_length,field->name);
3128
3164
      tee_fprintf(PAGER, "%s\n",cur[off] ? (char*) cur[off] : "NULL");
3129
3165
    }
3139
3175
  DRIZZLE_RES    *result;
3140
3176
  DRIZZLE_ROW    cur;
3141
3177
  uint64_t num_rows;
3142
 
 
 
3178
 
3143
3179
  /* Save current error before calling "show warnings" */
3144
 
  uint error= drizzleclient_errno(&drizzle);
 
3180
  uint error= drizzle_errno(&drizzle);
3145
3181
 
3146
3182
  /* Get the warnings */
3147
3183
  query= "show warnings";
3148
 
  drizzleclient_real_query_for_lazy(query, strlen(query));
3149
 
  drizzleclient_store_result_for_lazy(&result);
 
3184
  drizzle_real_query_for_lazy(query, strlen(query));
 
3185
  drizzle_store_result_for_lazy(&result);
3150
3186
 
3151
3187
  /* Bail out when no warnings */
3152
 
  if (!(num_rows= drizzleclient_num_rows(result)))
 
3188
  if (!(num_rows= drizzle_num_rows(result)))
3153
3189
    goto end;
3154
3190
 
3155
 
  cur= drizzleclient_fetch_row(result);
 
3191
  cur= drizzle_fetch_row(result);
3156
3192
 
3157
3193
  /*
3158
3194
    Don't print a duplicate of the current error.  It is possible for SHOW
3168
3204
  do
3169
3205
  {
3170
3206
    tee_fprintf(PAGER, "%s (Code %s): %s\n", cur[0], cur[1], cur[2]);
3171
 
  } while ((cur= drizzleclient_fetch_row(result)));
 
3207
  } while ((cur= drizzle_fetch_row(result)));
3172
3208
  end_pager();
3173
3209
 
3174
3210
end:
3175
 
  drizzleclient_free_result(result);
 
3211
  drizzle_free_result(result);
3176
3212
}
3177
3213
 
3178
3214
 
3223
3259
  if (opt_silent < 2 && column_names)
3224
3260
  {
3225
3261
    int first=0;
3226
 
    while ((field = drizzleclient_fetch_field(result)))
 
3262
    while ((field = drizzle_fetch_field(result)))
3227
3263
    {
3228
3264
      if (first++)
3229
3265
        (void) tee_fputs("\t", PAGER);
3231
3267
    }
3232
3268
    (void) tee_fputs("\n", PAGER);
3233
3269
  }
3234
 
  while ((cur = drizzleclient_fetch_row(result)))
 
3270
  while ((cur = drizzle_fetch_row(result)))
3235
3271
  {
3236
 
    lengths= drizzleclient_fetch_lengths(result);
 
3272
    lengths= drizzle_fetch_lengths(result);
3237
3273
    safe_put_field(cur[0],lengths[0]);
3238
 
    for (uint off=1 ; off < drizzleclient_num_fields(result); off++)
 
3274
    for (uint off=1 ; off < drizzle_num_fields(result); off++)
3239
3275
    {
3240
3276
      (void) tee_fputs("\t", PAGER);
3241
3277
      safe_put_field(cur[off], lengths[off]);
3245
3281
}
3246
3282
 
3247
3283
static int
3248
 
com_tee(string *, const char *line )
 
3284
com_tee(string *buffer __attribute__((unused)), const char *line )
3249
3285
{
3250
 
  char file_name[FN_REFLEN], *end;
3251
 
  const char *param;
 
3286
  char file_name[FN_REFLEN], *end, *param;
3252
3287
 
3253
3288
  if (status.batch)
3254
3289
    return 0;
3273
3308
  /* eliminate the spaces before the parameters */
3274
3309
  while (my_isspace(charset_info,*param))
3275
3310
    param++;
3276
 
  strncpy(file_name, param, sizeof(file_name) - 1);
3277
 
  end= file_name + strlen(file_name);
 
3311
  end= strmake(file_name, param, sizeof(file_name) - 1);
3278
3312
  /* remove end space from command line */
3279
3313
  while (end > file_name && (my_isspace(charset_info,end[-1]) ||
3280
3314
                             my_iscntrl(charset_info,end[-1])))
3291
3325
 
3292
3326
 
3293
3327
static int
3294
 
com_notee(string *, const char *)
 
3328
com_notee(string *buffer __attribute__((unused)),
 
3329
          const char *line __attribute__((unused)))
3295
3330
{
3296
3331
  if (opt_outfile)
3297
3332
    end_tee();
3304
3339
*/
3305
3340
 
3306
3341
static int
3307
 
com_pager(string *, const char *line)
 
3342
com_pager(string *buffer __attribute__((unused)),
 
3343
          const char *line __attribute__((unused)))
3308
3344
{
3309
 
  char pager_name[FN_REFLEN], *end;
3310
 
  const char *param;
 
3345
  char pager_name[FN_REFLEN], *end, *param;
3311
3346
 
3312
3347
  if (status.batch)
3313
3348
    return 0;
3325
3360
    {
3326
3361
      tee_fprintf(stdout, "Default pager wasn't set, using stdout.\n");
3327
3362
      opt_nopager=1;
3328
 
      strcpy(pager, "stdout");
 
3363
      my_stpcpy(pager, "stdout");
3329
3364
      PAGER= stdout;
3330
3365
      return 0;
3331
3366
    }
3332
 
    strcpy(pager, default_pager);
 
3367
    my_stpcpy(pager, default_pager);
3333
3368
  }
3334
3369
  else
3335
3370
  {
3336
 
    end= strncpy(pager_name, param, sizeof(pager_name)-1);
3337
 
    end+= strlen(pager_name);
 
3371
    end= strmake(pager_name, param, sizeof(pager_name)-1);
3338
3372
    while (end > pager_name && (my_isspace(charset_info,end[-1]) ||
3339
3373
                                my_iscntrl(charset_info,end[-1])))
3340
3374
      end--;
3341
3375
    end[0]=0;
3342
 
    strcpy(pager, pager_name);
3343
 
    strcpy(default_pager, pager_name);
 
3376
    my_stpcpy(pager, pager_name);
 
3377
    my_stpcpy(default_pager, pager_name);
3344
3378
  }
3345
3379
  opt_nopager=0;
3346
3380
  tee_fprintf(stdout, "PAGER set to '%s'\n", pager);
3349
3383
 
3350
3384
 
3351
3385
static int
3352
 
com_nopager(string *, const char *)
 
3386
com_nopager(string *buffer __attribute__((unused)),
 
3387
            const char *line __attribute__((unused)))
3353
3388
{
3354
 
  strcpy(pager, "stdout");
 
3389
  my_stpcpy(pager, "stdout");
3355
3390
  opt_nopager=1;
3356
3391
  PAGER= stdout;
3357
3392
  tee_fprintf(stdout, "PAGER set to stdout\n");
3361
3396
/* If arg is given, exit without errors. This happens on command 'quit' */
3362
3397
 
3363
3398
static int
3364
 
com_quit(string *, const char *)
 
3399
com_quit(string *buffer __attribute__((unused)),
 
3400
         const char *line __attribute__((unused)))
3365
3401
{
3366
3402
  /* let the screen auto close on a normal shutdown */
3367
3403
  status.exit_status=0;
3369
3405
}
3370
3406
 
3371
3407
static int
3372
 
com_rehash(string *, const char *)
 
3408
com_rehash(string *buffer __attribute__((unused)),
 
3409
           const char *line __attribute__((unused)))
3373
3410
{
3374
3411
  build_completion_hash(1, 0);
3375
3412
  return 0;
3378
3415
 
3379
3416
 
3380
3417
static int
3381
 
com_print(string *buffer,const char *)
 
3418
com_print(string *buffer,const char *line __attribute__((unused)))
3382
3419
{
3383
3420
  tee_puts("--------------", stdout);
3384
3421
  (void) tee_fputs(buffer->c_str(), stdout);
3405
3442
      Two null bytes are needed in the end of buff to allow
3406
3443
      get_arg to find end of string the second time it's called.
3407
3444
    */
3408
 
    tmp= strncpy(buff, line, sizeof(buff)-2);
 
3445
    tmp= strmake(buff, line, sizeof(buff)-2);
3409
3446
#ifdef EXTRA_DEBUG
3410
3447
    tmp[1]= 0;
3411
3448
#endif
3437
3474
 
3438
3475
  if (connected)
3439
3476
  {
3440
 
    sprintf(buff,"Connection id:    %u",drizzleclient_thread_id(&drizzle));
 
3477
    sprintf(buff,"Connection id:    %u",drizzle_thread_id(&drizzle));
3441
3478
    put_info(buff,INFO_INFO,0,0);
3442
3479
    sprintf(buff,"Current database: %.128s\n",
3443
3480
            current_db ? current_db : "*** NONE ***");
3447
3484
}
3448
3485
 
3449
3486
 
3450
 
static int com_source(string *, const char *line)
 
3487
static int com_source(string *buffer __attribute__((unused)), const char *line)
3451
3488
{
3452
 
  char source_name[FN_REFLEN], *end;
3453
 
  const char *param;
 
3489
  char source_name[FN_REFLEN], *end, *param;
3454
3490
  LINE_BUFFER *line_buff;
3455
3491
  int error;
3456
3492
  STATUS old_status;
3464
3500
                    INFO_ERROR, 0,0);
3465
3501
  while (my_isspace(charset_info,*param))
3466
3502
    param++;
3467
 
  end= strncpy(source_name,param,sizeof(source_name)-1);
3468
 
  end+= strlen(source_name);
 
3503
  end=strmake(source_name,param,sizeof(source_name)-1);
3469
3504
  while (end > source_name && (my_isspace(charset_info,end[-1]) ||
3470
3505
                               my_iscntrl(charset_info,end[-1])))
3471
3506
    end--;
3472
3507
  end[0]=0;
3473
3508
  unpack_filename(source_name,source_name);
3474
3509
  /* open file name */
3475
 
  if (!(sql_file = my_fopen(source_name, O_RDONLY,MYF(0))))
 
3510
  if (!(sql_file = my_fopen(source_name, O_RDONLY | O_BINARY,MYF(0))))
3476
3511
  {
3477
3512
    char buff[FN_REFLEN+60];
3478
3513
    sprintf(buff,"Failed to open file '%s', error: %d", source_name,errno);
3507
3542
 
3508
3543
/* ARGSUSED */
3509
3544
static int
3510
 
com_delimiter(string *, const char *line)
 
3545
com_delimiter(string *buffer __attribute__((unused)), const char *line)
3511
3546
{
3512
3547
  char buff[256], *tmp;
3513
3548
 
3514
 
  strncpy(buff, line, sizeof(buff) - 1);
 
3549
  strmake(buff, line, sizeof(buff) - 1);
3515
3550
  tmp= get_arg(buff, 0);
3516
3551
 
3517
3552
  if (!tmp || !*tmp)
3529
3564
      return 0;
3530
3565
    }
3531
3566
  }
3532
 
  strncpy(delimiter, tmp, sizeof(delimiter) - 1);
 
3567
  strmake(delimiter, tmp, sizeof(delimiter) - 1);
3533
3568
  delimiter_length= (int)strlen(delimiter);
3534
3569
  delimiter_str= delimiter;
3535
3570
  return 0;
3537
3572
 
3538
3573
/* ARGSUSED */
3539
3574
static int
3540
 
com_use(string *, const char *line)
 
3575
com_use(string *buffer __attribute__((unused)), const char *line)
3541
3576
{
3542
3577
  char *tmp, buff[FN_REFLEN + 1];
3543
3578
  int select_db;
3544
3579
 
3545
3580
  memset(buff, 0, sizeof(buff));
3546
 
  strncpy(buff, line, sizeof(buff) - 1);
 
3581
  strmake(buff, line, sizeof(buff) - 1);
3547
3582
  tmp= get_arg(buff, 0);
3548
3583
  if (!tmp || !*tmp)
3549
3584
  {
3562
3597
    if (one_database)
3563
3598
    {
3564
3599
      skip_updates= 1;
3565
 
      select_db= 0;    // don't do drizzleclient_select_db()
 
3600
      select_db= 0;    // don't do drizzle_select_db()
3566
3601
    }
3567
3602
    else
3568
 
      select_db= 2;    // do drizzleclient_select_db() and build_completion_hash()
 
3603
      select_db= 2;    // do drizzle_select_db() and build_completion_hash()
3569
3604
  }
3570
3605
  else
3571
3606
  {
3572
3607
    /*
3573
3608
      USE to the current db specified.
3574
 
      We do need to send drizzleclient_select_db() to make server
 
3609
      We do need to send drizzle_select_db() to make server
3575
3610
      update database level privileges, which might
3576
3611
      change since last USE (see bug#10979).
3577
3612
      For performance purposes, we'll skip rebuilding of completion hash.
3578
3613
    */
3579
3614
    skip_updates= 0;
3580
 
    select_db= 1;      // do only drizzleclient_select_db(), without completion
 
3615
    select_db= 1;      // do only drizzle_select_db(), without completion
3581
3616
  }
3582
3617
 
3583
3618
  if (select_db)
3588
3623
    */
3589
3624
    if (!connected && reconnect())
3590
3625
      return opt_reconnect ? -1 : 1;                        // Fatal error
3591
 
    if (drizzleclient_select_db(&drizzle,tmp))
 
3626
    if (drizzle_select_db(&drizzle,tmp))
3592
3627
    {
3593
 
      if (drizzleclient_errno(&drizzle) != CR_SERVER_GONE_ERROR)
 
3628
      if (drizzle_errno(&drizzle) != CR_SERVER_GONE_ERROR)
3594
3629
        return put_error(&drizzle);
3595
3630
 
3596
3631
      if (reconnect())
3597
3632
        return opt_reconnect ? -1 : 1;                      // Fatal error
3598
 
      if (drizzleclient_select_db(&drizzle,tmp))
 
3633
      if (drizzle_select_db(&drizzle,tmp))
3599
3634
        return put_error(&drizzle);
3600
3635
    }
3601
3636
    free(current_db);
3609
3644
}
3610
3645
 
3611
3646
static int
3612
 
com_warnings(string *, const char *)
 
3647
com_warnings(string *buffer __attribute__((unused)),
 
3648
             const char *line __attribute__((unused)))
3613
3649
{
3614
3650
  show_warnings = 1;
3615
3651
  put_info("Show warnings enabled.",INFO_INFO, 0, 0);
3617
3653
}
3618
3654
 
3619
3655
static int
3620
 
com_nowarnings(string *, const char *)
 
3656
com_nowarnings(string *buffer __attribute__((unused)),
 
3657
               const char *line __attribute__((unused)))
3621
3658
{
3622
3659
  show_warnings = 0;
3623
3660
  put_info("Show warnings disabled.",INFO_INFO, 0, 0);
3673
3710
    if (*ptr == '\\' && ptr[1]) // escaped character
3674
3711
    {
3675
3712
      // Remove the backslash
3676
 
      strcpy(ptr, ptr+1);
 
3713
      my_stpcpy(ptr, ptr+1);
3677
3714
    }
3678
3715
    else if ((!quoted && *ptr == ' ') || (quoted && *ptr == qtype))
3679
3716
    {
3693
3730
  if (connected)
3694
3731
  {
3695
3732
    connected= 0;
3696
 
    drizzleclient_close(&drizzle);
 
3733
    drizzle_close(&drizzle);
3697
3734
  }
3698
 
  drizzleclient_create(&drizzle);
 
3735
  drizzle_create(&drizzle);
3699
3736
  if (opt_connect_timeout)
3700
3737
  {
3701
3738
    uint timeout=opt_connect_timeout;
3702
 
    drizzleclient_options(&drizzle,DRIZZLE_OPT_CONNECT_TIMEOUT,
 
3739
    drizzle_options(&drizzle,DRIZZLE_OPT_CONNECT_TIMEOUT,
3703
3740
                  (char*) &timeout);
3704
3741
  }
3705
3742
  if (opt_compress)
3706
 
    drizzleclient_options(&drizzle,DRIZZLE_OPT_COMPRESS,NULL);
 
3743
    drizzle_options(&drizzle,DRIZZLE_OPT_COMPRESS,NULL);
3707
3744
  if (opt_secure_auth)
3708
 
    drizzleclient_options(&drizzle, DRIZZLE_SECURE_AUTH, (char *) &opt_secure_auth);
 
3745
    drizzle_options(&drizzle, DRIZZLE_SECURE_AUTH, (char *) &opt_secure_auth);
3709
3746
  if (using_opt_local_infile)
3710
 
    drizzleclient_options(&drizzle,DRIZZLE_OPT_LOCAL_INFILE, (char*) &opt_local_infile);
 
3747
    drizzle_options(&drizzle,DRIZZLE_OPT_LOCAL_INFILE, (char*) &opt_local_infile);
3711
3748
  if (safe_updates)
3712
3749
  {
3713
3750
    char init_command[100];
3714
3751
    sprintf(init_command,
3715
3752
            "SET SQL_SAFE_UPDATES=1,SQL_SELECT_LIMIT=%"PRIu32
3716
 
            ",MAX_JOIN_SIZE=%"PRIu32,
 
3753
            ",SQL_MAX_JOIN_SIZE=%"PRIu32,
3717
3754
            select_limit, max_join_size);
3718
 
    drizzleclient_options(&drizzle, DRIZZLE_INIT_COMMAND, init_command);
 
3755
    drizzle_options(&drizzle, DRIZZLE_INIT_COMMAND, init_command);
3719
3756
  }
3720
 
  if (!drizzleclient_connect(&drizzle, host, user, password,
 
3757
  if (!drizzle_connect(&drizzle, host, user, password,
3721
3758
                          database, opt_drizzle_port, opt_drizzle_unix_port,
3722
3759
                          connect_flag | CLIENT_MULTI_STATEMENTS))
3723
3760
  {
3724
3761
    if (!silent ||
3725
 
        (drizzleclient_errno(&drizzle) != CR_CONN_HOST_ERROR &&
3726
 
         drizzleclient_errno(&drizzle) != CR_CONNECTION_ERROR))
 
3762
        (drizzle_errno(&drizzle) != CR_CONN_HOST_ERROR &&
 
3763
         drizzle_errno(&drizzle) != CR_CONNECTION_ERROR))
3727
3764
    {
3728
3765
      (void) put_error(&drizzle);
3729
3766
      (void) fflush(stdout);
3739
3776
 
3740
3777
 
3741
3778
static int
3742
 
com_status(string *, const char *)
 
3779
com_status(string *buffer __attribute__((unused)),
 
3780
           const char *line __attribute__((unused)))
3743
3781
{
3744
3782
  char buff[40];
3745
3783
  uint64_t id;
3749
3787
  usage(1);          /* Print version */
3750
3788
  if (connected)
3751
3789
  {
3752
 
    tee_fprintf(stdout, "\nConnection id:\t\t%lu\n",drizzleclient_thread_id(&drizzle));
 
3790
    tee_fprintf(stdout, "\nConnection id:\t\t%lu\n",drizzle_thread_id(&drizzle));
3753
3791
    /*
3754
3792
      Don't remove "limit 1",
3755
3793
      it is protection againts SQL_SELECT_LIMIT=0
3756
3794
    */
3757
 
    if (!drizzleclient_query(&drizzle,"select DATABASE(), USER() limit 1") &&
3758
 
        (result=drizzleclient_use_result(&drizzle)))
 
3795
    if (!drizzle_query(&drizzle,"select DATABASE(), USER() limit 1") &&
 
3796
        (result=drizzle_use_result(&drizzle)))
3759
3797
    {
3760
 
      DRIZZLE_ROW cur=drizzleclient_fetch_row(result);
 
3798
      DRIZZLE_ROW cur=drizzle_fetch_row(result);
3761
3799
      if (cur)
3762
3800
      {
3763
3801
        tee_fprintf(stdout, "Current database:\t%s\n", cur[0] ? cur[0] : "");
3764
3802
        tee_fprintf(stdout, "Current user:\t\t%s\n", cur[1]);
3765
3803
      }
3766
 
      drizzleclient_free_result(result);
 
3804
      drizzle_free_result(result);
3767
3805
    }
3768
3806
    tee_puts("SSL:\t\t\tNot in use", stdout);
3769
3807
  }
3784
3822
  tee_fprintf(stdout, "Using outfile:\t\t'%s'\n", opt_outfile ? outfile : "");
3785
3823
  tee_fprintf(stdout, "Using delimiter:\t%s\n", delimiter);
3786
3824
  tee_fprintf(stdout, "Server version:\t\t%s\n", server_version_string(&drizzle));
3787
 
  tee_fprintf(stdout, "Protocol version:\t%d\n", drizzleclient_get_proto_info(&drizzle));
3788
 
  tee_fprintf(stdout, "Connection:\t\t%s\n", drizzleclient_get_host_info(&drizzle));
3789
 
  if ((id= drizzleclient_insert_id(&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)))
3790
3828
    tee_fprintf(stdout, "Insert id:\t\t%s\n", llstr(id, buff));
3791
3829
 
3792
3830
  /* "limit 1" is protection against SQL_SELECT_LIMIT=0 */
3793
 
  if (!drizzleclient_query(&drizzle,"select @@character_set_client, @@character_set_connection, @@character_set_server, @@character_set_database limit 1") &&
3794
 
      (result=drizzleclient_use_result(&drizzle)))
 
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)))
3795
3833
  {
3796
 
    DRIZZLE_ROW cur=drizzleclient_fetch_row(result);
 
3834
    DRIZZLE_ROW cur=drizzle_fetch_row(result);
3797
3835
    if (cur)
3798
3836
    {
3799
3837
      tee_fprintf(stdout, "Server characterset:\t%s\n", cur[2] ? cur[2] : "");
3801
3839
      tee_fprintf(stdout, "Client characterset:\t%s\n", cur[0] ? cur[0] : "");
3802
3840
      tee_fprintf(stdout, "Conn.  characterset:\t%s\n", cur[1] ? cur[1] : "");
3803
3841
    }
3804
 
    drizzleclient_free_result(result);
 
3842
    drizzle_free_result(result);
3805
3843
  }
3806
3844
 
3807
 
  if (strstr(drizzleclient_get_host_info(&drizzle),"TCP/IP") || ! drizzle.unix_socket)
 
3845
  if (strstr(drizzle_get_host_info(&drizzle),"TCP/IP") || ! drizzle.unix_socket)
3808
3846
    tee_fprintf(stdout, "TCP port:\t\t%d\n", drizzle.port);
3809
3847
  else
3810
3848
    tee_fprintf(stdout, "UNIX socket:\t\t%s\n", drizzle.unix_socket);
3830
3868
static const char *
3831
3869
server_version_string(DRIZZLE *con)
3832
3870
{
3833
 
  static string buf("");
3834
 
  static bool server_version_string_reserved= false;
 
3871
  static char buf[MAX_SERVER_VERSION_LENGTH] = "";
3835
3872
 
3836
 
  if (!server_version_string_reserved)
3837
 
  {
3838
 
    buf.reserve(MAX_SERVER_VERSION_LENGTH);
3839
 
    server_version_string_reserved= true;
3840
 
  }
3841
3873
  /* Only one thread calls this, so no synchronization is needed */
3842
3874
  if (buf[0] == '\0')
3843
3875
  {
 
3876
    char *bufp = buf;
3844
3877
    DRIZZLE_RES *result;
3845
3878
 
3846
 
    buf.append(drizzleclient_get_server_info(con));
 
3879
    bufp= my_stpncpy(buf, drizzle_get_server_info(con), sizeof buf);
3847
3880
 
3848
3881
    /* "limit 1" is protection against SQL_SELECT_LIMIT=0 */
3849
 
    if (!drizzleclient_query(con, "select @@version_comment limit 1") &&
3850
 
        (result = drizzleclient_use_result(con)))
 
3882
    if (!drizzle_query(con, "select @@version_comment limit 1") &&
 
3883
        (result = drizzle_use_result(con)))
3851
3884
    {
3852
 
      DRIZZLE_ROW cur = drizzleclient_fetch_row(result);
 
3885
      DRIZZLE_ROW cur = drizzle_fetch_row(result);
3853
3886
      if (cur && cur[0])
3854
3887
      {
3855
 
        buf.append(" ");
3856
 
        buf.append(cur[0]);
 
3888
        bufp = strxnmov(bufp, sizeof buf - (bufp - buf), " ", cur[0], NULL);
3857
3889
      }
3858
 
      drizzleclient_free_result(result);
 
3890
      drizzle_free_result(result);
3859
3891
    }
 
3892
 
 
3893
    /* str*nmov doesn't guarantee NUL-termination */
 
3894
    if (bufp == buf + sizeof buf)
 
3895
      buf[sizeof buf - 1] = '\0';
3860
3896
  }
3861
3897
 
3862
 
  return buf.c_str();
 
3898
  return buf;
3863
3899
}
3864
3900
 
3865
3901
static int
3937
3973
static int
3938
3974
put_error(DRIZZLE *con)
3939
3975
{
3940
 
  return put_info(drizzleclient_error(con), INFO_ERROR, drizzleclient_errno(con),
3941
 
                  drizzleclient_sqlstate(con));
 
3976
  return put_info(drizzle_error(con), INFO_ERROR, drizzle_errno(con),
 
3977
                  drizzle_sqlstate(con));
3942
3978
}
3943
3979
 
3944
3980
 
4022
4058
  if (sec >= 3600.0*24)
4023
4059
  {
4024
4060
    tmp=(uint32_t) floor(sec/(3600.0*24));
4025
 
    sec-= 3600.0*24*tmp;
4026
 
    buff= int10_to_str((long) tmp, buff, 10);
4027
 
 
4028
 
    if (tmp > 1)
4029
 
      buff= strcpy(buff," days ")+6;
4030
 
    else
4031
 
      buff= strcpy(buff," day ")+5;
4032
 
 
 
4061
    sec-=3600.0*24*tmp;
 
4062
    buff=int10_to_str((long) tmp, buff, 10);
 
4063
    buff=my_stpcpy(buff,tmp > 1 ? " days " : " day ");
4033
4064
  }
4034
4065
  if (sec >= 3600.0)
4035
4066
  {
4036
4067
    tmp=(uint32_t) floor(sec/3600.0);
4037
4068
    sec-=3600.0*tmp;
4038
4069
    buff=int10_to_str((long) tmp, buff, 10);
4039
 
 
4040
 
    if (tmp > 1)
4041
 
      buff= strcpy(buff, " hours ")+7;
4042
 
    else
4043
 
      buff= strcpy(buff, " hour ")+6;
 
4070
    buff=my_stpcpy(buff,tmp > 1 ? " hours " : " hour ");
4044
4071
  }
4045
4072
  if (sec >= 60.0)
4046
4073
  {
4047
4074
    tmp=(uint32_t) floor(sec/60.0);
4048
4075
    sec-=60.0*tmp;
4049
4076
    buff=int10_to_str((long) tmp, buff, 10);
4050
 
    buff= strcpy(buff," min ")+5;
 
4077
    buff=my_stpcpy(buff," min ");
4051
4078
  }
4052
4079
  if (part_second)
4053
4080
    sprintf(buff,"%.2f sec",sec);
4068
4095
  buff[0]=' ';
4069
4096
  buff[1]='(';
4070
4097
  end_timer(start_time,buff+2);
4071
 
  strcpy(strchr(buff, '\0'),")");
 
4098
  my_stpcpy(strchr(buff, '\0'),")");
4072
4099
}
4073
4100
 
4074
4101
static const char * construct_prompt()
4103
4130
        break;
4104
4131
      case 'v':
4105
4132
        if (connected)
4106
 
          processed_prompt->append(drizzleclient_get_server_info(&drizzle));
 
4133
          processed_prompt->append(drizzle_get_server_info(&drizzle));
4107
4134
        else
4108
4135
          processed_prompt->append("not_connected");
4109
4136
        break;
4113
4140
      case 'h':
4114
4141
      {
4115
4142
        const char *prompt;
4116
 
        prompt= connected ? drizzleclient_get_host_info(&drizzle) : "not_connected";
 
4143
        prompt= connected ? drizzle_get_host_info(&drizzle) : "not_connected";
4117
4144
        if (strstr(prompt, "Localhost"))
4118
4145
          processed_prompt->append("localhost");
4119
4146
        else
4132
4159
          break;
4133
4160
        }
4134
4161
 
4135
 
        const char *host_info = drizzleclient_get_host_info(&drizzle);
 
4162
        const char *host_info = drizzle_get_host_info(&drizzle);
4136
4163
        if (strstr(host_info, "memory"))
4137
4164
        {
4138
4165
          processed_prompt->append(drizzle.host);
4161
4188
        break;
4162
4189
      case PROMPT_CHAR:
4163
4190
        {
4164
 
          processed_prompt->append(PROMPT_CHAR, 1);
 
4191
          char c= PROMPT_CHAR;
 
4192
          processed_prompt->append(&c, 1);
4165
4193
        }
4166
4194
        break;
4167
4195
      case 'n':
4168
4196
        {
4169
 
          processed_prompt->append('\n', 1);
 
4197
          char c= '\n';
 
4198
          processed_prompt->append(&c, 1);
4170
4199
        }
4171
4200
        break;
4172
4201
      case ' ':
4173
4202
      case '_':
4174
4203
        {
4175
 
          processed_prompt->append(' ', 1);
 
4204
          char c= ' ';
 
4205
          processed_prompt->append(&c, 1);
4176
4206
        }
4177
4207
        break;
4178
4208
      case 'R':
4260
4290
  free(part_username);
4261
4291
 
4262
4292
  DRIZZLE_RES *result;
4263
 
  if (!drizzleclient_query(&drizzle,"select USER()") &&
4264
 
      (result=drizzleclient_use_result(&drizzle)))
 
4293
  if (!drizzle_query(&drizzle,"select USER()") &&
 
4294
      (result=drizzle_use_result(&drizzle)))
4265
4295
  {
4266
 
    DRIZZLE_ROW cur=drizzleclient_fetch_row(result);
 
4296
    DRIZZLE_ROW cur=drizzle_fetch_row(result);
4267
4297
    full_username= strdup(cur[0]);
4268
4298
    part_username= strdup(strtok(cur[0],"@"));
4269
 
    (void) drizzleclient_fetch_row(result);        // Read eof
 
4299
    (void) drizzle_fetch_row(result);        // Read eof
4270
4300
  }
4271
4301
}
4272
4302
 
4273
 
static int com_prompt(string *, const char *line)
 
4303
static int com_prompt(string *buffer __attribute__((unused)),
 
4304
                      const char *line)
4274
4305
{
4275
 
  const char *ptr=strchr(line, ' ');
4276
 
  if (ptr == NULL)
 
4306
  char *ptr=strchr(line, ' ');
 
4307
  prompt_counter = 0;
 
4308
  free(current_prompt);
 
4309
  current_prompt= strdup(ptr ? ptr+1 : default_prompt);
 
4310
  if (!ptr)
4277
4311
    tee_fprintf(stdout, "Returning to default PROMPT of %s\n",
4278
4312
                default_prompt);
4279
 
  prompt_counter = 0;
4280
 
  char * tmpptr= strdup(ptr ? ptr+1 : default_prompt);
4281
 
  if (tmpptr == NULL)
4282
 
    tee_fprintf(stdout, "Memory allocation error. Not changing prompt\n");
4283
4313
  else
4284
 
  {
4285
 
    free(current_prompt);
4286
 
    current_prompt= tmpptr;
4287
4314
    tee_fprintf(stdout, "PROMPT set to '%s'\n", current_prompt);
4288
 
  }
4289
4315
  return 0;
4290
4316
}
4291
4317