~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to client/drizzletest.cc

  • Committer: Brian Aker
  • Date: 2009-04-27 14:36:40 UTC
  • Revision ID: brian@gaz-20090427143640-f6zjmtt9vm55qgm2
Patch on show processlist from  davi@apache.org

Show diffs side-by-side

added added

removed removed

Lines of Context:
38
38
 
39
39
#define MTEST_VERSION "3.3"
40
40
 
41
 
#include "config.h"
 
41
#include "client_priv.h"
42
42
 
43
43
#include <queue>
44
44
#include <map>
45
45
#include <string>
46
 
 
47
 
#include <pcrecpp.h>
48
 
 
49
 
#include "client_priv.h"
 
46
#include <sstream>
 
47
#include <iostream>
 
48
#include <vector>
 
49
 
 
50
#include PCRE_HEADER
 
51
 
50
52
#include <mysys/hash.h>
51
53
#include <stdarg.h>
52
54
 
53
55
#include "errname.h"
54
56
 
 
57
/* Added this for string translation. */
 
58
#include <drizzled/gettext.h>
 
59
 
55
60
using namespace std;
56
61
 
57
62
#define MAX_VAR_NAME_LENGTH    256
58
63
#define MAX_COLUMNS            256
59
64
#define MAX_EMBEDDED_SERVER_ARGS 64
60
65
#define MAX_DELIMITER_LENGTH 16
61
 
 
62
66
/* Flags controlling send and reap */
63
67
#define QUERY_SEND_FLAG  1
64
68
#define QUERY_REAP_FLAG  2
65
69
 
66
70
enum {
67
71
  OPT_PS_PROTOCOL, OPT_SP_PROTOCOL, OPT_CURSOR_PROTOCOL, OPT_VIEW_PROTOCOL,
68
 
  OPT_MAX_CONNECT_RETRIES, OPT_MARK_PROGRESS, OPT_LOG_DIR, OPT_TAIL_LINES
 
72
  OPT_MAX_CONNECT_RETRIES, OPT_MARK_PROGRESS, OPT_LOG_DIR, OPT_TAIL_LINES,
 
73
  OPT_TESTDIR
69
74
};
70
75
 
71
76
static int record= 0, opt_sleep= -1;
72
 
static char *opt_db= 0, *opt_pass= 0;
73
 
const char *opt_user= 0, *opt_host= 0, *unix_sock= 0, *opt_basedir= "./";
 
77
static char *opt_db= NULL, *opt_pass= NULL;
 
78
const char *opt_user= NULL, *opt_host= NULL, *unix_sock= NULL,
 
79
           *opt_basedir= "./";
74
80
const char *opt_logdir= "";
75
 
const char *opt_include= 0, *opt_charsets_dir;
76
 
static int opt_port= 0;
 
81
const char *opt_include= NULL, *opt_charsets_dir;
 
82
const char *opt_testdir= NULL;
 
83
static uint32_t opt_port= 0;
77
84
static int opt_max_connect_retries;
78
 
static bool opt_compress= 0, silent= 0, verbose= 0;
79
 
static bool debug_info_flag= 0, debug_check_flag= 0;
80
 
static bool tty_password= 0;
81
 
static bool opt_mark_progress= 0;
82
 
static bool parsing_disabled= 0;
 
85
static bool opt_compress= false, silent= false, verbose= false;
 
86
static bool debug_info_flag= false, debug_check_flag= false;
 
87
static bool tty_password= false;
 
88
static bool opt_mark_progress= false;
 
89
static bool parsing_disabled= false;
83
90
static bool display_result_vertically= false,
84
91
  display_metadata= false, display_result_sorted= false;
85
 
static bool disable_query_log= 0, disable_result_log= 0;
86
 
static bool disable_warnings= 0;
87
 
static bool disable_info= 1;
88
 
static bool abort_on_error= 1;
89
 
static bool server_initialized= 0;
90
 
static bool is_windows= 0;
 
92
static bool disable_query_log= false, disable_result_log= false;
 
93
static bool disable_warnings= false;
 
94
static bool disable_info= true;
 
95
static bool abort_on_error= true;
 
96
static bool server_initialized= false;
 
97
static bool is_windows= false;
91
98
static char **default_argv;
92
99
static const char *load_default_groups[]= { "drizzletest", "client", 0 };
93
100
static char line_buffer[MAX_DELIMITER_LENGTH], *line_buffer_pos= line_buffer;
94
101
 
95
 
static uint start_lineno= 0; /* Start line of current command */
96
 
static uint my_end_arg= 0;
 
102
static uint32_t start_lineno= 0; /* Start line of current command */
 
103
static uint32_t my_end_arg= 0;
97
104
 
98
105
/* Number of lines of the result to include in failure report */
99
 
static uint opt_tail_lines= 0;
 
106
static uint32_t opt_tail_lines= 0;
100
107
 
101
108
static char delimiter[MAX_DELIMITER_LENGTH]= ";";
102
 
static uint delimiter_length= 1;
 
109
static uint32_t delimiter_length= 1;
103
110
 
104
111
static char TMPDIR[FN_REFLEN];
105
112
 
125
132
{
126
133
  FILE* file;
127
134
  const char *file_name;
128
 
  uint lineno; /* Current line in file */
 
135
  uint32_t lineno; /* Current line in file */
129
136
};
130
137
 
131
138
static struct st_test_file file_stack[16];
165
172
master_pos_st master_pos;
166
173
 
167
174
/* if set, all results are concated and compared against this file */
168
 
const char *result_file_name= 0;
 
175
const char *result_file_name= NULL;
169
176
 
170
177
typedef struct st_var
171
178
{
187
194
 
188
195
struct st_connection
189
196
{
190
 
  DRIZZLE drizzle;
 
197
  drizzle_st *drizzle;
 
198
  drizzle_con_st con;
191
199
  /* Used when creating views and sp, to avoid implicit commit */
192
 
  DRIZZLE *util_drizzle;
 
200
  drizzle_con_st *util_con;
193
201
  char *name;
194
202
};
195
203
struct st_connection connections[128];
227
235
  Q_DISPLAY_VERTICAL_RESULTS, Q_DISPLAY_HORIZONTAL_RESULTS,
228
236
  Q_QUERY_VERTICAL, Q_QUERY_HORIZONTAL, Q_SORTED_RESULT,
229
237
  Q_START_TIMER, Q_END_TIMER,
230
 
  Q_CHARACTER_SET, Q_DISABLE_PS_PROTOCOL, Q_ENABLE_PS_PROTOCOL,
 
238
  Q_CHARACTER_SET,
231
239
  Q_DISABLE_RECONNECT, Q_ENABLE_RECONNECT,
232
240
  Q_IF,
233
241
  Q_DISABLE_PARSING, Q_ENABLE_PARSING,
345
353
  enum match_err_type type;
346
354
  union
347
355
  {
348
 
    uint errnum;
349
 
    char sqlstate[SQLSTATE_LENGTH+1];  /* \0 terminated string */
 
356
    uint32_t errnum;
 
357
    char sqlstate[DRIZZLE_MAX_SQLSTATE_SIZE+1];  /* \0 terminated string */
350
358
  } code;
351
359
};
352
360
 
353
361
struct st_expected_errors
354
362
{
355
363
  struct st_match_err err[10];
356
 
  uint count;
 
364
  uint32_t count;
357
365
};
358
366
static struct st_expected_errors saved_expected_errors;
359
367
 
388
396
VAR* var_from_env(const char *, const char *);
389
397
VAR* var_init(VAR* v, const char *name, int name_len, const char *val,
390
398
              int val_len);
391
 
void var_free(void* v);
 
399
extern "C" void var_free(void* v);
392
400
VAR* var_get(const char *var_name, const char** var_name_end,
393
401
             bool raw, bool ignore_not_existing);
394
402
void eval_expr(VAR* v, const char *p, const char** p_end);
395
 
bool match_delimiter(int c, const char *delim, uint length);
 
403
bool match_delimiter(int c, const char *delim, uint32_t length);
396
404
void dump_result_to_reject_file(char *buf, int size);
397
405
void dump_result_to_log_file(const char *buf, int size);
398
406
void dump_warning_messages(void);
405
413
 
406
414
/* For replace_column */
407
415
static char *replace_column[MAX_COLUMNS];
408
 
static uint max_replace_column= 0;
 
416
static uint32_t max_replace_column= 0;
409
417
void do_get_replace_column(struct st_command*);
410
418
void free_replace_column(void);
411
419
 
430
438
void replace_append_mem(string *ds, const char *val,
431
439
                        int len);
432
440
void replace_append(string *ds, const char *val);
433
 
void replace_append_uint(string *ds, uint val);
 
441
void replace_append_uint(string *ds, uint32_t val);
434
442
void append_sorted(string* ds, string* ds_input);
435
443
 
436
444
void handle_error(struct st_command*,
439
447
void handle_no_error(struct st_command*);
440
448
 
441
449
 
442
 
#define do_send_query(cn,q,q_len,flags) drizzle_send_query(&cn->drizzle, q, q_len)
443
 
 
444
450
void do_eval(string *query_eval, const char *query,
445
451
             const char *query_end, bool pass_through_escape_chars)
446
452
{
514
520
void append_os_quoted(string *str, const char *append, ...)
515
521
{
516
522
  const char *quote_str= "\'";
517
 
  const uint  quote_len= 1;
 
523
  const uint32_t  quote_len= 1;
518
524
 
519
525
  va_list dirty_text;
520
526
 
533
539
      str->append(quote_str, quote_len);
534
540
      cur_pos= next_pos + 1;
535
541
    }
536
 
    str->append(cur_pos, next_pos - cur_pos);
 
542
    str->append(cur_pos);
537
543
    append= va_arg(dirty_text, char *);
538
544
  }
539
545
  va_end(dirty_text);
554
560
 
555
561
*/
556
562
 
557
 
static void show_query(DRIZZLE *drizzle, const char* query)
 
563
static void show_query(drizzle_con_st *con, const char* query)
558
564
{
559
 
  DRIZZLE_RES* res;
560
 
 
561
 
 
562
 
  if (!drizzle)
 
565
  drizzle_result_st res;
 
566
  drizzle_return_t ret;
 
567
 
 
568
  if (!con)
563
569
    return;
564
570
 
565
 
  if (drizzle_query(drizzle, query))
 
571
  if (drizzle_query_str(con, &res, query, &ret) == NULL ||
 
572
      ret != DRIZZLE_RETURN_OK)
566
573
  {
567
 
    log_msg("Error running query '%s': %d %s",
568
 
            query, drizzle_errno(drizzle), drizzle_error(drizzle));
 
574
    if (ret == DRIZZLE_RETURN_ERROR_CODE)
 
575
    {
 
576
      log_msg("Error running query '%s': %d %s",
 
577
              query, drizzle_result_error_code(&res),
 
578
              drizzle_result_error(&res));
 
579
      drizzle_result_free(&res);
 
580
    }
 
581
    else
 
582
    {
 
583
      log_msg("Error running query '%s': %d %s",
 
584
              query, ret, drizzle_con_error(con));
 
585
    }
569
586
    return;
570
587
  }
571
588
 
572
 
  if ((res= drizzle_store_result(drizzle)) == NULL)
 
589
  if (drizzle_result_column_count(&res) == 0 ||
 
590
      drizzle_result_buffer(&res) != DRIZZLE_RETURN_OK)
573
591
  {
574
592
    /* No result set returned */
 
593
    drizzle_result_free(&res);
575
594
    return;
576
595
  }
577
596
 
578
597
  {
579
 
    DRIZZLE_ROW row;
 
598
    drizzle_row_t row;
580
599
    unsigned int i;
581
600
    unsigned int row_num= 0;
582
 
    unsigned int num_fields= drizzle_num_fields(res);
583
 
    const DRIZZLE_FIELD *fields= drizzle_fetch_fields(res);
 
601
    unsigned int num_fields= drizzle_result_column_count(&res);
 
602
    drizzle_column_st *column;
584
603
 
585
604
    fprintf(stderr, "=== %s ===\n", query);
586
 
    while ((row= drizzle_fetch_row(res)))
 
605
    while ((row= drizzle_row_next(&res)))
587
606
    {
588
 
      uint32_t *lengths= drizzle_fetch_lengths(res);
 
607
      size_t *lengths= drizzle_row_field_sizes(&res);
589
608
      row_num++;
590
609
 
591
610
      fprintf(stderr, "---- %d. ----\n", row_num);
 
611
      drizzle_column_seek(&res, 0);
592
612
      for(i= 0; i < num_fields; i++)
593
613
      {
 
614
        column= drizzle_column_next(&res);
594
615
        fprintf(stderr, "%s\t%.*s\n",
595
 
                fields[i].name,
 
616
                drizzle_column_name(column),
596
617
                (int)lengths[i], row[i] ? row[i] : "NULL");
597
618
      }
598
619
    }
600
621
      fprintf(stderr, "=");
601
622
    fprintf(stderr, "\n\n");
602
623
  }
603
 
  drizzle_free_result(res);
 
624
  drizzle_result_free(&res);
604
625
 
605
626
  return;
606
627
}
619
640
 
620
641
*/
621
642
 
622
 
static void show_warnings_before_error(DRIZZLE *drizzle)
 
643
static void show_warnings_before_error(drizzle_con_st *con)
623
644
{
624
 
  DRIZZLE_RES* res;
 
645
  drizzle_result_st res;
 
646
  drizzle_return_t ret;
625
647
  const char* query= "SHOW WARNINGS";
626
648
 
627
 
 
628
 
  if (!drizzle)
 
649
  if (!con)
629
650
    return;
630
651
 
631
 
  if (drizzle_query(drizzle, query))
 
652
  if (drizzle_query_str(con, &res, query, &ret) == NULL ||
 
653
      ret != DRIZZLE_RETURN_OK)
632
654
  {
633
 
    log_msg("Error running query '%s': %d %s",
634
 
            query, drizzle_errno(drizzle), drizzle_error(drizzle));
 
655
    if (ret == DRIZZLE_RETURN_ERROR_CODE)
 
656
    {
 
657
      log_msg("Error running query '%s': %d %s",
 
658
              query, drizzle_result_error_code(&res),
 
659
              drizzle_result_error(&res));
 
660
      drizzle_result_free(&res);
 
661
    }
 
662
    else
 
663
    {
 
664
      log_msg("Error running query '%s': %d %s",
 
665
              query, ret, drizzle_con_error(con));
 
666
    }
635
667
    return;
636
668
  }
637
669
 
638
 
  if ((res= drizzle_store_result(drizzle)) == NULL)
 
670
  if (drizzle_result_column_count(&res) == 0 ||
 
671
      drizzle_result_buffer(&res) != DRIZZLE_RETURN_OK)
639
672
  {
640
673
    /* No result set returned */
 
674
    drizzle_result_free(&res);
641
675
    return;
642
676
  }
643
677
 
644
 
  if (drizzle_num_rows(res) <= 1)
 
678
  if (drizzle_result_row_count(&res) <= 1)
645
679
  {
646
680
    /* Don't display the last row, it's "last error" */
647
681
  }
648
682
  else
649
683
  {
650
 
    DRIZZLE_ROW row;
 
684
    drizzle_row_t row;
651
685
    unsigned int row_num= 0;
652
 
    unsigned int num_fields= drizzle_num_fields(res);
 
686
    unsigned int num_fields= drizzle_result_column_count(&res);
653
687
 
654
688
    fprintf(stderr, "\nWarnings from just before the error:\n");
655
 
    while ((row= drizzle_fetch_row(res)))
 
689
    while ((row= drizzle_row_next(&res)))
656
690
    {
657
691
      uint32_t i;
658
 
      uint32_t *lengths= drizzle_fetch_lengths(res);
 
692
      size_t *lengths= drizzle_row_field_sizes(&res);
659
693
 
660
 
      if (++row_num >= drizzle_num_rows(res))
 
694
      if (++row_num >= drizzle_result_row_count(&res))
661
695
      {
662
696
        /* Don't display the last row, it's "last error" */
663
697
        break;
671
705
      fprintf(stderr, "\n");
672
706
    }
673
707
  }
674
 
  drizzle_free_result(res);
 
708
  drizzle_result_free(&res);
675
709
 
676
710
  return;
677
711
}
762
796
}
763
797
 
764
798
 
765
 
static void handle_command_error(struct st_command *command, uint error)
 
799
static void handle_command_error(struct st_command *command, uint32_t error)
766
800
{
767
801
 
768
802
  if (error != 0)
769
803
  {
770
 
    uint i;
 
804
    uint32_t i;
771
805
 
772
806
    if (command->abort_on_error)
773
807
      die("command \"%.*s\" failed with error %d",
797
831
 
798
832
static void close_connections(void)
799
833
{
800
 
 
801
834
  for (--next_con; next_con >= connections; --next_con)
802
835
  {
803
 
    drizzle_close(&next_con->drizzle);
804
 
    if (next_con->util_drizzle)
805
 
      drizzle_close(next_con->util_drizzle);
 
836
    if (next_con->drizzle != NULL)
 
837
    {
 
838
      drizzle_free(next_con->drizzle);
 
839
      next_con->drizzle= NULL;
 
840
    }
806
841
    free(next_con->name);
807
842
  }
808
843
  return;
816
851
  {
817
852
    if (cur_file->file && cur_file->file != stdin)
818
853
    {
819
 
      my_fclose(cur_file->file, MYF(0));
 
854
      fclose(cur_file->file);
820
855
    }
821
856
    free((unsigned char*) cur_file->file_name);
822
857
    cur_file->file_name= 0;
827
862
 
828
863
static void free_used_memory(void)
829
864
{
830
 
  uint i;
 
865
  uint32_t i;
831
866
 
832
867
 
833
868
  close_connections();
837
872
  vector<struct st_command *>::iterator iter;
838
873
  for (iter= q_lines.begin() ; iter < q_lines.end() ; iter++)
839
874
  {
840
 
    struct st_command * q_line= *(iter.base());
 
875
    struct st_command * q_line= *iter;
841
876
    if (q_line->query_buf != NULL)
842
877
    {
843
878
      free(q_line->query_buf);
949
984
    been produced prior to the error
950
985
  */
951
986
  if (cur_con)
952
 
    show_warnings_before_error(&cur_con->drizzle);
 
987
    show_warnings_before_error(&cur_con->con);
953
988
 
954
989
  cleanup_and_exit(1);
955
990
}
1074
1109
static void cat_file(string* ds, const char* filename)
1075
1110
{
1076
1111
  int fd;
1077
 
  uint len;
 
1112
  uint32_t len;
1078
1113
  char buff[512];
1079
1114
 
1080
1115
  if ((fd= my_open(filename, O_RDONLY, MYF(0))) < 0)
1156
1191
  string ds_cmdline;
1157
1192
 
1158
1193
 
1159
 
  append_os_quoted(&ds_cmdline, tool_path);
 
1194
  append_os_quoted(&ds_cmdline, tool_path, NULL);
1160
1195
  ds_cmdline.append(" ");
1161
1196
 
1162
1197
  va_start(args, result);
1206
1241
               "2>&1",
1207
1242
               NULL) > 1) /* Most "diff" tools return >1 if error */
1208
1243
  {
1209
 
    ds_tmp= "";
1210
1244
 
1211
1245
    /* Fallback to context diff with "diff -c" */
1212
1246
    if (run_tool("diff",
1221
1255
        Fallback to dump both files to result file and inform
1222
1256
        about installing "diff"
1223
1257
      */
1224
 
      ds_tmp= "";
 
1258
      ds_tmp.clear();
1225
1259
 
1226
1260
      ds_tmp.append(
1227
1261
                    "\n"
1282
1316
{
1283
1317
  int error= RESULT_OK;
1284
1318
  File fd2;
1285
 
  uint len, len2;
 
1319
  uint32_t len, len2;
1286
1320
  char buff[512], buff2[512];
 
1321
  const char *fname= filename2;
 
1322
  string tmpfile;
1287
1323
 
1288
 
  if ((fd2= my_open(filename2, O_RDONLY, MYF(0))) < 0)
 
1324
  if ((fd2= my_open(fname, O_RDONLY, MYF(0))) < 0)
1289
1325
  {
1290
1326
    my_close(fd, MYF(0));
1291
 
    die("Failed to open second file: '%s'", filename2);
 
1327
    if (opt_testdir != NULL)
 
1328
    {
 
1329
      tmpfile= opt_testdir;
 
1330
      if (tmpfile[tmpfile.length()] != '/')
 
1331
        tmpfile.append("/");
 
1332
      tmpfile.append(filename2);
 
1333
      fname= tmpfile.c_str();
 
1334
    }
 
1335
    if ((fd2= my_open(fname, O_RDONLY, MYF(0))) < 0)
 
1336
    {
 
1337
      my_close(fd, MYF(0));
 
1338
    
 
1339
      die("Failed to open second file: '%s'", fname);
 
1340
    }
1292
1341
  }
1293
1342
  while((len= my_read(fd, (unsigned char*)&buff,
1294
1343
                      sizeof(buff), MYF(0))) > 0)
1374
1423
  char temp_file_path[FN_REFLEN];
1375
1424
 
1376
1425
  if ((fd= create_temp_file(temp_file_path, NULL,
1377
 
                            "tmp", O_CREAT | O_SHARE | O_RDWR,
 
1426
                            "tmp", O_CREAT | O_RDWR,
1378
1427
                            MYF(MY_WME))) < 0)
1379
1428
    die("Failed to create temporary file for ds");
1380
1429
 
1381
1430
  /* Write ds to temporary file and set file pos to beginning*/
1382
1431
  if (my_write(fd, (unsigned char *) ds->c_str(), ds->length(),
1383
1432
               MYF(MY_FNABP | MY_WME)) ||
1384
 
      my_seek(fd, 0, SEEK_SET, MYF(0)) == MY_FILEPOS_ERROR)
 
1433
      lseek(fd, 0, SEEK_SET) == MY_FILEPOS_ERROR)
1385
1434
  {
1386
1435
    my_close(fd, MYF(0));
1387
1436
    /* Remove the temporary file */
1454
1503
    ds->erase(); /* Don't create a .log file */
1455
1504
 
1456
1505
    show_diff(NULL, result_file_name, reject_file);
1457
 
    die(mess);
 
1506
    die("%s",mess);
1458
1507
    break;
1459
1508
  }
1460
1509
  default: /* impossible */
1538
1587
}
1539
1588
 
1540
1589
 
1541
 
static unsigned char *get_var_key(const unsigned char* var, size_t *len,
1542
 
                          bool __attribute__((unused)) t)
 
1590
extern "C"
 
1591
unsigned char *get_var_key(const unsigned char* var, size_t *len, bool)
1543
1592
{
1544
1593
  register char* key;
1545
1594
  key = ((VAR*)var)->name;
1558
1607
  if (!val_len && val)
1559
1608
    val_len = strlen(val) ;
1560
1609
  val_alloc_len = val_len + 16; /* room to grow */
1561
 
  if (!(tmp_var=v) && !(tmp_var = (VAR*)my_malloc(sizeof(*tmp_var)
1562
 
                                                  + name_len+1, MYF(MY_WME))))
 
1610
  if (!(tmp_var=v) && !(tmp_var = (VAR*)malloc(sizeof(*tmp_var)
 
1611
                                               + name_len+1)))
1563
1612
    die("Out of memory");
1564
1613
 
1565
1614
  tmp_var->name = (name) ? (char*) tmp_var + sizeof(*tmp_var) : 0;
1566
1615
  tmp_var->alloced = (v == 0);
1567
1616
 
1568
 
  if (!(tmp_var->str_val = (char *)my_malloc(val_alloc_len+1, MYF(MY_WME))))
 
1617
  if (!(tmp_var->str_val = (char *)malloc(val_alloc_len+1)))
1569
1618
    die("Out of memory");
1570
1619
 
1571
1620
  memcpy(tmp_var->name, name, name_len);
1618
1667
  if (digit < 0 || digit >= 10)
1619
1668
  {
1620
1669
    const char *save_var_name = var_name, *end;
1621
 
    uint length;
 
1670
    uint32_t length;
1622
1671
    end = (var_name_end) ? *var_name_end : 0;
1623
1672
    while (my_isvar(charset_info,*var_name) && var_name != end)
1624
1673
      var_name++;
1628
1677
        return(0);
1629
1678
      die("Empty variable");
1630
1679
    }
1631
 
    length= (uint) (var_name - save_var_name);
 
1680
    length= (uint32_t) (var_name - save_var_name);
1632
1681
    if (length >= MAX_VAR_NAME_LENGTH)
1633
1682
      die("Too long variable name: %s", save_var_name);
1634
1683
 
1636
1685
                                 length)))
1637
1686
    {
1638
1687
      char buff[MAX_VAR_NAME_LENGTH+1];
1639
 
      strmake(buff, save_var_name, length);
 
1688
      strncpy(buff, save_var_name, length);
 
1689
      buff[length]= '\0';
1640
1690
      v= var_from_env(buff, "");
1641
1691
    }
1642
1692
    var_name--;  /* Point at last character */
1692
1742
  digit= *var_name - '0';
1693
1743
  if (!(digit < 10 && digit >= 0))
1694
1744
  {
1695
 
    v= var_obtain(var_name, (uint) (var_name_end - var_name));
 
1745
    v= var_obtain(var_name, (uint32_t) (var_name_end - var_name));
1696
1746
  }
1697
1747
  else
1698
1748
    v= var_reg + digit;
1711
1761
    snprintf(buf, sizeof(buf), "%.*s=%.*s",
1712
1762
             v->name_len, v->name,
1713
1763
             v->str_val_len, v->str_val);
1714
 
    if (!(v->env_s= my_strdup(buf, MYF(MY_WME))))
 
1764
    if (!(v->env_s= strdup(buf)))
1715
1765
      die("Out of memory");
1716
1766
    putenv(v->env_s);
1717
1767
    free(old_env_s);
1736
1786
 
1737
1787
/*
1738
1788
  Store an integer (typically the returncode of the last SQL)
1739
 
  statement in the drizzletest builtin variable $drizzle_errno
 
1789
  statement in the drizzletest builtin variable $drizzleclient_errno
1740
1790
*/
1741
1791
 
1742
1792
static void var_set_errno(int sql_errno)
1743
1793
{
1744
 
  var_set_int("$drizzle_errno", sql_errno);
 
1794
  var_set_int("$drizzleclient_errno", sql_errno);
1745
1795
}
1746
1796
 
1747
1797
 
1748
1798
/*
1749
 
  Update $drizzle_get_server_version variable with version
 
1799
  Update $drizzleclient_get_server_version variable with version
1750
1800
  of the currently connected server
1751
1801
*/
1752
1802
 
1753
 
static void var_set_drizzle_get_server_version(DRIZZLE *drizzle)
 
1803
static void var_set_drizzleclient_get_server_version(drizzle_con_st *con)
1754
1804
{
1755
 
  var_set_int("$drizzle_get_server_version", drizzle_get_server_version(drizzle));
 
1805
  var_set_int("$drizzle_con_server_version", drizzle_con_server_version_number(con));
1756
1806
}
1757
1807
 
1758
1808
 
1783
1833
{
1784
1834
  const char *end = (char*)((query_end && *query_end) ?
1785
1835
                            *query_end : query + strlen(query));
1786
 
  DRIZZLE_RES *res;
1787
 
  DRIZZLE_ROW row;
1788
 
  DRIZZLE *drizzle= &cur_con->drizzle;
 
1836
  drizzle_result_st res;
 
1837
  drizzle_return_t ret;
 
1838
  drizzle_row_t row;
 
1839
  drizzle_con_st *con= &cur_con->con;
1789
1840
  string ds_query;
1790
1841
 
1791
1842
 
1798
1849
  /* Eval the query, thus replacing all environment variables */
1799
1850
  do_eval(&ds_query, query, end, false);
1800
1851
 
1801
 
  if (drizzle_real_query(drizzle, ds_query.c_str(), ds_query.length()))
1802
 
    die("Error running query '%s': %d %s", ds_query.c_str(),
1803
 
        drizzle_errno(drizzle), drizzle_error(drizzle));
1804
 
  if (!(res= drizzle_store_result(drizzle)))
 
1852
  if (drizzle_query(con, &res, ds_query.c_str(), ds_query.length(),
 
1853
                    &ret) == NULL ||
 
1854
      ret != DRIZZLE_RETURN_OK)
 
1855
  {
 
1856
    if (ret == DRIZZLE_RETURN_ERROR_CODE)
 
1857
    {
 
1858
      die("Error running query '%s': %d %s", ds_query.c_str(),
 
1859
          drizzle_result_error_code(&res), drizzle_result_error(&res));
 
1860
      drizzle_result_free(&res);
 
1861
    }
 
1862
    else
 
1863
    {
 
1864
      die("Error running query '%s': %d %s", ds_query.c_str(), ret,
 
1865
          drizzle_con_error(con));
 
1866
    }
 
1867
  }
 
1868
  if (drizzle_result_column_count(&res) == 0 ||
 
1869
      drizzle_result_buffer(&res) != DRIZZLE_RETURN_OK)
1805
1870
    die("Query '%s' didn't return a result set", ds_query.c_str());
1806
1871
 
1807
 
  if ((row= drizzle_fetch_row(res)) && row[0])
 
1872
  if ((row= drizzle_row_next(&res)) && row[0])
1808
1873
  {
1809
1874
    /*
1810
1875
      Concatenate all fields in the first row with tab in between
1812
1877
    */
1813
1878
    string result;
1814
1879
    uint32_t i;
1815
 
    uint32_t *lengths;
 
1880
    size_t *lengths;
1816
1881
 
1817
 
    lengths= drizzle_fetch_lengths(res);
1818
 
    for (i= 0; i < drizzle_num_fields(res); i++)
 
1882
    lengths= drizzle_row_field_sizes(&res);
 
1883
    for (i= 0; i < drizzle_result_column_count(&res); i++)
1819
1884
    {
1820
1885
      if (row[i])
1821
1886
      {
1830
1895
  else
1831
1896
    eval_expr(var, "", 0);
1832
1897
 
1833
 
  drizzle_free_result(res);
 
1898
  drizzle_result_free(&res);
1834
1899
  return;
1835
1900
}
1836
1901
 
1861
1926
{
1862
1927
  long row_no;
1863
1928
  int col_no= -1;
1864
 
  DRIZZLE_RES* res;
1865
 
  DRIZZLE *drizzle= &cur_con->drizzle;
 
1929
  drizzle_result_st res;
 
1930
  drizzle_return_t ret;
 
1931
  drizzle_con_st *con= &cur_con->con;
1866
1932
 
1867
1933
  string ds_query;
1868
1934
  string ds_col;
1881
1947
                     ',');
1882
1948
 
1883
1949
  /* Convert row number to int */
1884
 
  if (!str2int(ds_row.c_str(), 10, (long) 0, (long) INT_MAX, &row_no))
 
1950
  row_no= atoi(ds_row.c_str());
 
1951
  
 
1952
  istringstream buff(ds_row);
 
1953
  if ((buff >> row_no).fail())
1885
1954
    die("Invalid row number: '%s'", ds_row.c_str());
1886
1955
 
1887
1956
  /* Remove any surrounding "'s from the query - if there is any */
1889
1958
  char * unstripped_query= strdup(ds_query.c_str());
1890
1959
  if (strip_surrounding(unstripped_query, '"', '"'))
1891
1960
    die("Mismatched \"'s around query '%s'", ds_query.c_str());
1892
 
  ds_query= unstripped_query;
 
1961
  ds_query.clear();
 
1962
  ds_query.append(unstripped_query);
1893
1963
 
1894
1964
  /* Run the query */
1895
 
  if (drizzle_real_query(drizzle, ds_query.c_str(), ds_query.length()))
1896
 
    die("Error running query '%s': %d %s", ds_query.c_str(),
1897
 
        drizzle_errno(drizzle), drizzle_error(drizzle));
1898
 
  if (!(res= drizzle_store_result(drizzle)))
 
1965
  if (drizzle_query(con, &res, ds_query.c_str(), ds_query.length(),
 
1966
                    &ret) == NULL ||
 
1967
      ret != DRIZZLE_RETURN_OK)
 
1968
  {
 
1969
    if (ret == DRIZZLE_RETURN_ERROR_CODE)
 
1970
    {
 
1971
      die("Error running query '%s': %d %s", ds_query.c_str(),
 
1972
          drizzle_result_error_code(&res), drizzle_result_error(&res));
 
1973
      drizzle_result_free(&res);
 
1974
    }
 
1975
    else
 
1976
    {
 
1977
      die("Error running query '%s': %d %s", ds_query.c_str(), ret,
 
1978
          drizzle_con_error(con));
 
1979
    }
 
1980
  }
 
1981
  if (drizzle_result_column_count(&res) == 0 ||
 
1982
      drizzle_result_buffer(&res) != DRIZZLE_RETURN_OK)
1899
1983
    die("Query '%s' didn't return a result set", ds_query.c_str());
1900
1984
 
1901
1985
  {
1902
1986
    /* Find column number from the given column name */
1903
 
    uint i;
1904
 
    uint num_fields= drizzle_num_fields(res);
1905
 
    const DRIZZLE_FIELD *fields= drizzle_fetch_fields(res);
 
1987
    uint32_t i;
 
1988
    uint32_t num_fields= drizzle_result_column_count(&res);
 
1989
    drizzle_column_st *column;
1906
1990
 
1907
1991
    for (i= 0; i < num_fields; i++)
1908
1992
    {
1909
 
      if (strcmp(fields[i].name, ds_col.c_str()) == 0 &&
1910
 
          strlen(fields[i].name) == ds_col.length())
 
1993
      column= drizzle_column_next(&res);
 
1994
      if (strcmp(drizzle_column_name(column), ds_col.c_str()) == 0 &&
 
1995
          strlen(drizzle_column_name(column)) == ds_col.length())
1911
1996
      {
1912
1997
        col_no= i;
1913
1998
        break;
1915
2000
    }
1916
2001
    if (col_no == -1)
1917
2002
    {
1918
 
      drizzle_free_result(res);
 
2003
      drizzle_result_free(&res);
1919
2004
      die("Could not find column '%s' in the result of '%s'",
1920
2005
          ds_col.c_str(), ds_query.c_str());
1921
2006
    }
1923
2008
 
1924
2009
  {
1925
2010
    /* Get the value */
1926
 
    DRIZZLE_ROW row;
 
2011
    drizzle_row_t row;
1927
2012
    long rows= 0;
1928
2013
    const char* value= "No such row";
1929
2014
 
1930
 
    while ((row= drizzle_fetch_row(res)))
 
2015
    while ((row= drizzle_row_next(&res)))
1931
2016
    {
1932
2017
      if (++rows == row_no)
1933
2018
      {
1943
2028
    }
1944
2029
    eval_expr(var, value, 0);
1945
2030
  }
1946
 
  drizzle_free_result(res);
 
2031
  drizzle_result_free(&res);
1947
2032
 
1948
2033
  return;
1949
2034
}
1955
2040
  dest->int_dirty= src->int_dirty;
1956
2041
 
1957
2042
  /* Alloc/realloc data for str_val in dest */
1958
 
  if (dest->alloced_len < src->alloced_len &&
1959
 
      !(dest->str_val= dest->str_val
1960
 
        ? (char *)my_realloc(dest->str_val, src->alloced_len, MYF(MY_WME))
1961
 
        : (char *)my_malloc(src->alloced_len, MYF(MY_WME))))
1962
 
    die("Out of memory");
 
2043
  if (dest->alloced_len < src->alloced_len)
 
2044
  {
 
2045
    char *tmpptr= (char *)realloc(dest->str_val, src->alloced_len);
 
2046
    if (tmpptr == NULL)
 
2047
      die("Out of memory");
 
2048
    dest->str_val= tmpptr;
 
2049
  }
1963
2050
  else
1964
2051
    dest->alloced_len= src->alloced_len;
1965
2052
 
2011
2098
      static int MIN_VAR_ALLOC= 32;
2012
2099
      v->alloced_len = (new_val_len < MIN_VAR_ALLOC - 1) ?
2013
2100
        MIN_VAR_ALLOC : new_val_len + 1;
2014
 
      if (!(v->str_val =
2015
 
            v->str_val ? (char *)my_realloc(v->str_val, v->alloced_len+1,
2016
 
                                            MYF(MY_WME)) :
2017
 
            (char *)my_malloc(v->alloced_len+1, MYF(MY_WME))))
 
2101
      char *tmpptr= (char *)realloc(v->str_val, v->alloced_len+1);
 
2102
      if (tmpptr == NULL)
2018
2103
        die("Out of memory");
 
2104
      v->str_val= tmpptr;
2019
2105
    }
2020
2106
    v->str_val_len = new_val_len;
2021
2107
    memcpy(v->str_val, p, new_val_len);
2033
2119
 
2034
2120
  if (!test_if_hard_path(name))
2035
2121
  {
2036
 
    strxmov(buff, opt_basedir, name, NULL);
 
2122
    sprintf(buff,"%s%s",opt_basedir,name);
2037
2123
    name=buff;
2038
2124
  }
2039
2125
  fn_format(buff, name, "", "", MY_UNPACK_FILENAME);
2041
2127
  if (cur_file == file_stack_end)
2042
2128
    die("Source directives are nesting too deep");
2043
2129
  cur_file++;
2044
 
  if (!(cur_file->file = my_fopen(buff, O_RDONLY | FILE_BINARY, MYF(0))))
 
2130
  if (!(cur_file->file= fopen(buff, "r")))
2045
2131
  {
2046
2132
    cur_file--;
2047
2133
    die("Could not open '%s' for reading", buff);
2048
2134
  }
2049
 
  cur_file->file_name= my_strdup(buff, MYF(MY_FAE));
 
2135
  if (!(cur_file->file_name= strdup(buff)))
 
2136
    die("Out of memory");
2050
2137
  cur_file->lineno=1;
2051
2138
  return(0);
2052
2139
}
2086
2173
    ; /* Do nothing */
2087
2174
  else
2088
2175
  {
 
2176
    if (opt_testdir != NULL)
 
2177
    {
 
2178
      string testdir(opt_testdir);
 
2179
      if (testdir[testdir.length()] != '/')
 
2180
        testdir.append("/");
 
2181
      testdir.append(ds_filename);
 
2182
      ds_filename.swap(testdir);
 
2183
    }
2089
2184
    open_file(ds_filename.c_str());
2090
2185
  }
2091
2186
 
2197
2292
  error= pclose(res_file);
2198
2293
  if (error > 0)
2199
2294
  {
2200
 
    uint status= WEXITSTATUS(error), i;
 
2295
    uint32_t status= WEXITSTATUS(error), i;
2201
2296
    bool ok= 0;
2202
2297
 
2203
2298
    if (command->abort_on_error)
2415
2510
                     ' ');
2416
2511
 
2417
2512
  /* Parse what mode to set */
 
2513
  istringstream buff(ds_mode);
2418
2514
  if (ds_mode.length() != 4 ||
2419
 
      str2int(ds_mode.c_str(), 8, 0, INT_MAX, &mode) == NULL)
 
2515
      (buff >> mode).fail())
2420
2516
    die("You must write a 4 digit octal number for mode");
2421
2517
 
2422
2518
  handle_command_error(command, chmod(ds_file.c_str(), mode));
2466
2562
 
2467
2563
static void do_mkdir(struct st_command *command)
2468
2564
{
 
2565
  string ds_dirname;
2469
2566
  int error;
2470
 
  string ds_dirname;
2471
2567
  const struct command_arg mkdir_args[] = {
2472
2568
    {"dirname", ARG_STRING, true, &ds_dirname, "Directory to create"}
2473
2569
  };
2477
2573
                     mkdir_args, sizeof(mkdir_args)/sizeof(struct command_arg),
2478
2574
                     ' ');
2479
2575
 
2480
 
  error= my_mkdir(ds_dirname.c_str(), 0777, MYF(0)) != 0;
 
2576
  error= mkdir(ds_dirname.c_str(), (0777 & my_umask_dir)) != 0;
2481
2577
  handle_command_error(command, error);
2482
2578
  return;
2483
2579
}
2597
2693
 
2598
2694
  /* If no delimiter was provided, use EOF */
2599
2695
  if (ds_delimiter.length() == 0)
2600
 
    ds_delimiter= "EOF";
 
2696
    ds_delimiter.append("EOF");
2601
2697
 
2602
2698
  if (!append && access(ds_filename.c_str(), F_OK) == 0)
2603
2699
  {
2778
2874
{
2779
2875
  char *p= command->first_argument, *name;
2780
2876
  struct st_connection *con;
 
2877
  drizzle_result_st result;
 
2878
  drizzle_return_t ret;
2781
2879
 
2782
2880
  if (!*p)
2783
2881
    die("Missing connection name in send_quit");
2792
2890
  if (!(con= find_connection_by_name(name)))
2793
2891
    die("connection '%s' not found in connection pool", name);
2794
2892
 
2795
 
  simple_command(&con->drizzle,COM_QUIT,0,0,1);
 
2893
  if (drizzle_quit(&con->con,&result, &ret))
 
2894
    drizzle_result_free(&result);
2796
2895
 
2797
2896
  return;
2798
2897
}
2814
2913
 
2815
2914
*/
2816
2915
 
2817
 
static void do_change_user(struct st_command *command)
 
2916
static void do_change_user(struct st_command *)
2818
2917
{
2819
 
  DRIZZLE *drizzle= &cur_con->drizzle;
2820
 
  /* static keyword to make the NetWare compiler happy. */
2821
 
  string ds_user, ds_passwd, ds_db;
2822
 
  const struct command_arg change_user_args[] = {
2823
 
    { "user", ARG_STRING, false, &ds_user, "User to connect as" },
2824
 
    { "password", ARG_STRING, false, &ds_passwd, "Password used when connecting" },
2825
 
    { "database", ARG_STRING, false, &ds_db, "Database to select after connect" },
2826
 
  };
2827
 
 
2828
 
 
2829
 
 
2830
 
  check_command_args(command, command->first_argument,
2831
 
                     change_user_args,
2832
 
                     sizeof(change_user_args)/sizeof(struct command_arg),
2833
 
                     ',');
2834
 
 
2835
 
  if (!ds_user.length())
2836
 
    ds_user= drizzle->user;
2837
 
 
2838
 
  if (!ds_passwd.length())
2839
 
    ds_passwd= drizzle->passwd;
2840
 
 
2841
 
  if (!ds_db.length())
2842
 
    ds_db= drizzle->db;
2843
 
 
2844
 
  if (drizzle_change_user(drizzle, ds_user.c_str(),
2845
 
                          ds_passwd.c_str(), ds_db.c_str()))
2846
 
    die("change user failed: %s", drizzle_error(drizzle));
2847
 
 
2848
 
 
2849
 
  return;
 
2918
  assert(0);
2850
2919
}
2851
2920
 
2852
 
 
2853
2921
/*
2854
2922
  SYNOPSIS
2855
2923
  do_perl
2891
2959
 
2892
2960
  /* If no delimiter was provided, use EOF */
2893
2961
  if (ds_delimiter.length() == 0)
2894
 
    ds_delimiter= "EOF";
 
2962
    ds_delimiter.append("EOF");
2895
2963
 
2896
2964
  read_until_delimiter(&ds_script, &ds_delimiter);
2897
2965
 
2898
2966
  /* Create temporary file name */
2899
2967
  if ((fd= create_temp_file(temp_file_path, getenv("MYSQLTEST_VARDIR"),
2900
 
                            "tmp", O_CREAT | O_SHARE | O_RDWR,
 
2968
                            "tmp", O_CREAT | O_RDWR,
2901
2969
                            MYF(MY_WME))) < 0)
2902
2970
    die("Failed to create temporary file for perl command");
2903
2971
  my_close(fd, MYF(0));
2965
3033
 
2966
3034
 
2967
3035
static void
2968
 
do_wait_for_slave_to_stop(struct st_command *c __attribute__((unused)))
 
3036
do_wait_for_slave_to_stop(struct st_command *)
2969
3037
{
2970
3038
  static int SLAVE_POLL_INTERVAL= 300000;
2971
 
  DRIZZLE *drizzle= &cur_con->drizzle;
 
3039
  drizzle_con_st *con= &cur_con->con;
2972
3040
  for (;;)
2973
3041
  {
2974
 
    DRIZZLE_RES *res= NULL;
2975
 
    DRIZZLE_ROW row;
 
3042
    drizzle_result_st res;
 
3043
    drizzle_return_t ret;
 
3044
    drizzle_row_t row;
2976
3045
    int done;
2977
3046
 
2978
 
    if (drizzle_query(drizzle,"show status like 'Slave_running'") ||
2979
 
        !(res=drizzle_store_result(drizzle)))
 
3047
    if (drizzle_query_str(con,&res,"show status like 'Slave_running'",
 
3048
                          &ret) == NULL || ret != DRIZZLE_RETURN_OK)
 
3049
    {
 
3050
      if (ret == DRIZZLE_RETURN_ERROR_CODE)
 
3051
      {
 
3052
        die("Query failed while probing slave for stop: %s",
 
3053
            drizzle_result_error(&res));
 
3054
        drizzle_result_free(&res);
 
3055
      }
 
3056
      else
 
3057
      {
 
3058
        die("Query failed while probing slave for stop: %s",
 
3059
            drizzle_con_error(con));
 
3060
      }
 
3061
    }
 
3062
 
 
3063
    if (drizzle_result_column_count(&res) == 0 ||
 
3064
        drizzle_result_buffer(&res) != DRIZZLE_RETURN_OK)
 
3065
    {
2980
3066
      die("Query failed while probing slave for stop: %s",
2981
 
          drizzle_error(drizzle));
2982
 
    if (!(row=drizzle_fetch_row(res)) || !row[1])
 
3067
          drizzle_con_error(con));
 
3068
    }
 
3069
 
 
3070
    if (!(row=drizzle_row_next(&res)) || !row[1])
2983
3071
    {
2984
 
      drizzle_free_result(res);
 
3072
      drizzle_result_free(&res);
2985
3073
      die("Strange result from query while probing slave for stop");
2986
3074
    }
2987
3075
    done = !strcmp(row[1],"OFF");
2988
 
    drizzle_free_result(res);
 
3076
    drizzle_result_free(&res);
2989
3077
    if (done)
2990
3078
      break;
2991
 
    my_sleep(SLAVE_POLL_INTERVAL);
 
3079
    usleep(SLAVE_POLL_INTERVAL);
2992
3080
  }
2993
3081
  return;
2994
3082
}
2996
3084
 
2997
3085
static void do_sync_with_master2(long offset)
2998
3086
{
2999
 
  DRIZZLE_RES *res;
3000
 
  DRIZZLE_ROW row;
3001
 
  DRIZZLE *drizzle= &cur_con->drizzle;
 
3087
  drizzle_result_st res;
 
3088
  drizzle_return_t ret;
 
3089
  drizzle_row_t row;
 
3090
  drizzle_con_st *con= &cur_con->con;
3002
3091
  char query_buf[FN_REFLEN+128];
3003
3092
  int tries= 0;
3004
3093
 
3010
3099
 
3011
3100
wait_for_position:
3012
3101
 
3013
 
  if (drizzle_query(drizzle, query_buf))
3014
 
    die("failed in '%s': %d: %s", query_buf, drizzle_errno(drizzle),
3015
 
        drizzle_error(drizzle));
3016
 
 
3017
 
  if (!(res= drizzle_store_result(drizzle)))
3018
 
    die("drizzle_store_result() returned NULL for '%s'", query_buf);
3019
 
  if (!(row= drizzle_fetch_row(res)))
3020
 
  {
3021
 
    drizzle_free_result(res);
 
3102
  if (drizzle_query_str(con, &res, query_buf, &ret) == NULL ||
 
3103
      ret != DRIZZLE_RETURN_OK)
 
3104
  {
 
3105
    if (ret == DRIZZLE_RETURN_ERROR_CODE)
 
3106
    {
 
3107
      die("failed in '%s': %d: %s", query_buf, drizzle_result_error_code(&res),
 
3108
           drizzle_result_error(&res));
 
3109
      drizzle_result_free(&res);
 
3110
    }
 
3111
    else
 
3112
      die("failed in '%s': %d: %s", query_buf, ret, drizzle_con_error(con));
 
3113
  }
 
3114
 
 
3115
  if (drizzle_result_column_count(&res) == 0 ||
 
3116
      drizzle_result_buffer(&res) != DRIZZLE_RETURN_OK)
 
3117
    die("drizzle_result_buffer() returned NULL for '%s'", query_buf);
 
3118
 
 
3119
  if (!(row= drizzle_row_next(&res)))
 
3120
  {
 
3121
    drizzle_result_free(&res);
3022
3122
    die("empty result in %s", query_buf);
3023
3123
  }
3024
3124
  if (!row[0])
3027
3127
      It may be that the slave SQL thread has not started yet, though START
3028
3128
      SLAVE has been issued ?
3029
3129
    */
3030
 
    drizzle_free_result(res);
 
3130
    drizzle_result_free(&res);
3031
3131
    if (tries++ == 30)
3032
3132
    {
3033
 
      show_query(drizzle, "SHOW MASTER STATUS");
3034
 
      show_query(drizzle, "SHOW SLAVE STATUS");
 
3133
      show_query(con, "SHOW MASTER STATUS");
 
3134
      show_query(con, "SHOW SLAVE STATUS");
3035
3135
      die("could not sync with master ('%s' returned NULL)", query_buf);
3036
3136
    }
3037
3137
    sleep(1); /* So at most we will wait 30 seconds and make 31 tries */
3038
3138
    goto wait_for_position;
3039
3139
  }
3040
 
  drizzle_free_result(res);
 
3140
  drizzle_result_free(&res);
3041
3141
  return;
3042
3142
}
3043
3143
 
3067
3167
*/
3068
3168
static int do_save_master_pos(void)
3069
3169
{
3070
 
  DRIZZLE_RES *res;
3071
 
  DRIZZLE_ROW row;
3072
 
  DRIZZLE *drizzle= &cur_con->drizzle;
 
3170
  drizzle_result_st res;
 
3171
  drizzle_return_t ret;
 
3172
  drizzle_row_t row;
 
3173
  drizzle_con_st *con= &cur_con->con;
3073
3174
  const char *query;
3074
3175
 
3075
3176
 
3076
 
  if (drizzle_query(drizzle, query= "show master status"))
3077
 
    die("failed in 'show master status': %d %s",
3078
 
        drizzle_errno(drizzle), drizzle_error(drizzle));
 
3177
  if (drizzle_query_str(con, &res, query= "show master status", &ret) == NULL ||
 
3178
      ret != DRIZZLE_RETURN_OK)
 
3179
  {
 
3180
    if (ret == DRIZZLE_RETURN_ERROR_CODE)
 
3181
    {
 
3182
      die("failed in '%s': %d: %s", query, drizzle_result_error_code(&res),
 
3183
           drizzle_result_error(&res));
 
3184
      drizzle_result_free(&res);
 
3185
    }
 
3186
    else
 
3187
      die("failed in '%s': %d: %s", query, ret, drizzle_con_error(con));
 
3188
  }
3079
3189
 
3080
 
  if (!(res = drizzle_store_result(drizzle)))
3081
 
    die("drizzle_store_result() retuned NULL for '%s'", query);
3082
 
  if (!(row = drizzle_fetch_row(res)))
 
3190
  if (drizzle_result_column_count(&res) == 0 ||
 
3191
      drizzle_result_buffer(&res) != DRIZZLE_RETURN_OK)
 
3192
    die("drizzleclient_store_result() retuned NULL for '%s'", query);
 
3193
  if (!(row = drizzle_row_next(&res)))
3083
3194
    die("empty result in show master status");
3084
 
  my_stpncpy(master_pos.file, row[0], sizeof(master_pos.file)-1);
 
3195
  strncpy(master_pos.file, row[0], sizeof(master_pos.file)-1);
3085
3196
  master_pos.pos = strtoul(row[1], (char**) 0, 10);
3086
 
  drizzle_free_result(res);
 
3197
  drizzle_result_free(&res);
3087
3198
  return(0);
3088
3199
}
3089
3200
 
3167
3278
 
3168
3279
static int do_sleep(struct st_command *command, bool real_sleep)
3169
3280
{
3170
 
  int error= 0;
 
3281
  bool error= false;
3171
3282
  char *p= command->first_argument;
3172
3283
  char *sleep_start, *sleep_end= command->end;
3173
3284
  double sleep_val;
3181
3292
  if (!my_isdigit(charset_info, *sleep_start))
3182
3293
    die("Invalid argument to %.*s \"%s\"", command->first_word_len,
3183
3294
        command->query,command->first_argument);
3184
 
  sleep_val= my_strtod(sleep_start, &sleep_end, &error);
 
3295
  string buff_str(sleep_start, sleep_end-sleep_start);
 
3296
  istringstream buff(buff_str);
 
3297
  error= (buff >> sleep_val).fail();
3185
3298
  if (error)
3186
3299
    die("Invalid argument to %.*s \"%s\"", command->first_word_len,
3187
3300
        command->query, command->first_argument);
3191
3304
    sleep_val= opt_sleep;
3192
3305
 
3193
3306
  if (sleep_val)
3194
 
    my_sleep((uint32_t) (sleep_val * 1000000L));
 
3307
    usleep((uint32_t) (sleep_val * 1000000L));
3195
3308
  command->last_argument= sleep_end;
3196
3309
  return 0;
3197
3310
}
3198
3311
 
3199
3312
 
3200
3313
static void do_get_file_name(struct st_command *command,
3201
 
                             char* dest, uint dest_max_len)
 
3314
                             char* dest, uint32_t dest_max_len)
3202
3315
{
3203
3316
  char *p= command->first_argument, *name;
3204
3317
  if (!*p)
3209
3322
  if (*p)
3210
3323
    *p++= 0;
3211
3324
  command->last_argument= p;
3212
 
  strmake(dest, name, dest_max_len - 1);
 
3325
  strncpy(dest, name, dest_max_len - 1);
3213
3326
}
3214
3327
 
3215
3328
 
3227
3340
  if(*p)
3228
3341
    *p++= 0;
3229
3342
  command->last_argument= p;
3230
 
  charset_info= get_charset_by_csname(charset_name,MY_CS_PRIMARY,MYF(MY_WME));
 
3343
  charset_info= get_charset_by_csname(charset_name, MY_CS_PRIMARY);
3231
3344
  if (!charset_info)
3232
3345
    abort_not_supported_test("Test requires charset '%s'", charset_name);
3233
3346
}
3234
3347
 
3235
 
static uint get_errcode_from_name(char *error_name, char *error_end)
 
3348
static uint32_t get_errcode_from_name(char *error_name, char *error_end)
3236
3349
{
3237
3350
  /* SQL error as string */
3238
3351
  st_error *e= global_error_names;
3246
3359
      (as in ER_WRONG_VALUE vs. ER_WRONG_VALUE_COUNT).
3247
3360
    */
3248
3361
    if (!strncmp(error_name, e->name, (int) (error_end - error_name)) &&
3249
 
        (uint) strlen(e->name) == (uint) (error_end - error_name))
 
3362
        (uint32_t) strlen(e->name) == (uint32_t) (error_end - error_name))
3250
3363
    {
3251
3364
      return(e->code);
3252
3365
    }
3260
3373
{
3261
3374
  struct st_match_err *to= saved_expected_errors.err;
3262
3375
  char *p= command->first_argument;
3263
 
  uint count= 0;
 
3376
  uint32_t count= 0;
3264
3377
 
3265
3378
 
3266
3379
 
3286
3399
 
3287
3400
      /*
3288
3401
        SQLSTATE string
3289
 
        - Must be SQLSTATE_LENGTH long
 
3402
        - Must be DRIZZLE_MAX_SQLSTATE_SIZE long
3290
3403
        - May contain only digits[0-9] and _uppercase_ letters
3291
3404
      */
3292
3405
      p++; /* Step past the S */
3293
 
      if ((end - p) != SQLSTATE_LENGTH)
3294
 
        die("The sqlstate must be exactly %d chars long", SQLSTATE_LENGTH);
 
3406
      if ((end - p) != DRIZZLE_MAX_SQLSTATE_SIZE)
 
3407
        die("The sqlstate must be exactly %d chars long", DRIZZLE_MAX_SQLSTATE_SIZE);
3295
3408
 
3296
3409
      /* Check sqlstate string validity */
3297
3410
      while (*p && p < end)
3336
3449
      }
3337
3450
 
3338
3451
      /* Convert the sting to int */
3339
 
      if (!str2int(start, 10, (long) INT_MIN, (long) INT_MAX, &val))
 
3452
      istringstream buff(start);
 
3453
      if ((buff >> val).fail())
3340
3454
        die("Invalid argument to error: '%s'", command->first_argument);
3341
3455
 
3342
 
      to->code.errnum= (uint) val;
 
3456
      to->code.errnum= (uint32_t) val;
3343
3457
      to->type= ERR_ERRNO;
3344
3458
    }
3345
3459
    to++;
3446
3560
}
3447
3561
 
3448
3562
 
3449
 
static void set_reconnect(DRIZZLE *drizzle, int val)
 
3563
static void set_reconnect(drizzle_con_st *con, int val)
3450
3564
{
 
3565
  (void) con;
 
3566
  (void) val;
 
3567
/* XXX
3451
3568
  bool reconnect= val;
3452
3569
 
3453
 
  drizzle_options(drizzle, DRIZZLE_OPT_RECONNECT, (char *)&reconnect);
3454
 
 
3455
 
  return;
 
3570
  drizzleclient_options(drizzle, DRIZZLE_OPT_RECONNECT, (char *)&reconnect);
 
3571
*/
3456
3572
}
3457
3573
 
3458
3574
 
3461
3577
  if (!(cur_con= find_connection_by_name(name)))
3462
3578
    die("connection '%s' not found in connection pool", name);
3463
3579
 
3464
 
  /* Update $drizzle_get_server_version to that of current connection */
3465
 
  var_set_drizzle_get_server_version(&cur_con->drizzle);
 
3580
  /* Update $drizzleclient_get_server_version to that of current connection */
 
3581
  var_set_drizzleclient_get_server_version(&cur_con->con);
3466
3582
 
3467
3583
  return(0);
3468
3584
}
3504
3620
  if (!(con= find_connection_by_name(name)))
3505
3621
    die("connection '%s' not found in connection pool", name);
3506
3622
 
3507
 
  if (command->type == Q_DIRTY_CLOSE)
 
3623
  if (con->drizzle != NULL)
3508
3624
  {
3509
 
    if (con->drizzle.net.vio)
3510
 
    {
3511
 
      net_close(&(con->drizzle.net));
3512
 
    }
 
3625
    drizzle_free(con->drizzle);
 
3626
    con->drizzle= NULL;
3513
3627
  }
3514
 
 
3515
 
  drizzle_close(&con->drizzle);
3516
 
 
3517
 
  if (con->util_drizzle)
3518
 
    drizzle_close(con->util_drizzle);
3519
 
  con->util_drizzle= 0;
3520
 
 
3521
3628
  free(con->name);
3522
3629
 
3523
3630
  /*
3524
3631
    When the connection is closed set name to "-closed_connection-"
3525
3632
    to make it possible to reuse the connection name.
3526
3633
  */
3527
 
  if (!(con->name = my_strdup("-closed_connection-", MYF(MY_WME))))
 
3634
  if (!(con->name = strdup("-closed_connection-")))
3528
3635
    die("Out of memory");
3529
3636
 
3530
3637
  return;
3556
3663
 
3557
3664
*/
3558
3665
 
3559
 
static void safe_connect(DRIZZLE *drizzle, const char *name, const char *host,
3560
 
                         const char *user, const char *pass, const char *db,
3561
 
                         int port)
 
3666
static void safe_connect(drizzle_con_st *con, const char *name,
 
3667
                         const char *host, const char *user, const char *pass,
 
3668
                         const char *db, int port)
3562
3669
{
3563
3670
  int failed_attempts= 0;
3564
3671
  static uint32_t connection_retry_sleep= 100000; /* Microseconds */
3565
 
 
3566
 
 
3567
 
  while(!drizzle_connect(drizzle, host, user, pass, db, port, NULL,
3568
 
                         CLIENT_MULTI_STATEMENTS | CLIENT_REMEMBER_OPTIONS))
 
3672
  drizzle_return_t ret;
 
3673
 
 
3674
  drizzle_con_set_tcp(con, host, port);
 
3675
  drizzle_con_set_auth(con, user, pass);
 
3676
  drizzle_con_set_db(con, db);
 
3677
  while((ret= drizzle_con_connect(con)) != DRIZZLE_RETURN_OK)
3569
3678
  {
3570
3679
    /*
3571
3680
      Connect failed
3575
3684
      on protocol/connection type
3576
3685
    */
3577
3686
 
3578
 
    if ((drizzle_errno(drizzle) == CR_CONN_HOST_ERROR ||
3579
 
         drizzle_errno(drizzle) == CR_CONNECTION_ERROR) &&
 
3687
    if ((ret == DRIZZLE_RETURN_GETADDRINFO ||
 
3688
         ret == DRIZZLE_RETURN_COULD_NOT_CONNECT) &&
3580
3689
        failed_attempts < opt_max_connect_retries)
3581
3690
    {
3582
3691
      verbose_msg("Connect attempt %d/%d failed: %d: %s", failed_attempts,
3583
 
                  opt_max_connect_retries, drizzle_errno(drizzle),
3584
 
                  drizzle_error(drizzle));
3585
 
      my_sleep(connection_retry_sleep);
 
3692
                  opt_max_connect_retries, ret, drizzle_con_error(con));
 
3693
      usleep(connection_retry_sleep);
3586
3694
    }
3587
3695
    else
3588
3696
    {
3589
3697
      if (failed_attempts > 0)
3590
3698
        die("Could not open connection '%s' after %d attempts: %d %s", name,
3591
 
            failed_attempts, drizzle_errno(drizzle), drizzle_error(drizzle));
 
3699
            failed_attempts, ret, drizzle_con_error(con));
3592
3700
      else
3593
 
        die("Could not open connection '%s': %d %s", name,
3594
 
            drizzle_errno(drizzle), drizzle_error(drizzle));
 
3701
        die("Could not open connection '%s': %d %s", name, ret,
 
3702
            drizzle_con_error(con));
3595
3703
    }
3596
3704
    failed_attempts++;
3597
3705
  }
3623
3731
*/
3624
3732
 
3625
3733
static int connect_n_handle_errors(struct st_command *command,
3626
 
                                   DRIZZLE *con, const char* host,
 
3734
                                   drizzle_con_st *con, const char* host,
3627
3735
                                   const char* user, const char* pass,
3628
3736
                                   const char* db, int port, const char* sock)
3629
3737
{
 
3738
  drizzle_return_t ret;
3630
3739
 
3631
3740
  /* Only log if an error is expected */
3632
3741
  if (!command->abort_on_error &&
3653
3762
    ds_res.append(delimiter);
3654
3763
    ds_res.append("\n");
3655
3764
  }
3656
 
  if (!drizzle_connect(con, host, user, pass, db, port, 0,
3657
 
                       CLIENT_MULTI_STATEMENTS))
 
3765
  drizzle_con_set_tcp(con, host, port);
 
3766
  drizzle_con_set_auth(con, user, pass);
 
3767
  drizzle_con_set_db(con, db);
 
3768
  if ((ret= drizzle_con_connect(con)) != DRIZZLE_RETURN_OK)
3658
3769
  {
3659
 
    var_set_errno(drizzle_errno(con));
3660
 
    handle_error(command, drizzle_errno(con), drizzle_error(con),
3661
 
                 drizzle_sqlstate(con), &ds_res);
 
3770
    if (ret == DRIZZLE_RETURN_ERROR_CODE)
 
3771
    {
 
3772
      var_set_errno(drizzle_con_error_code(con));
 
3773
      handle_error(command, drizzle_con_error_code(con), drizzle_con_error(con),
 
3774
                   drizzle_con_sqlstate(con), &ds_res);
 
3775
    }
 
3776
    else
 
3777
    {
 
3778
      var_set_errno(ret);
 
3779
      handle_error(command, ret, drizzle_con_error(con), "", &ds_res);
 
3780
    }
 
3781
 
3662
3782
    return 0; /* Not connected */
3663
3783
  }
3664
3784
 
3744
3864
    {
3745
3865
      char buff[FN_REFLEN];
3746
3866
      fn_format(buff, ds_sock.c_str(), TMPDIR, "", 0);
3747
 
      ds_sock= buff;
 
3867
      ds_sock.clear();
 
3868
      ds_sock.append(buff);
3748
3869
    }
3749
3870
  }
3750
3871
 
3785
3906
          (int) (sizeof(connections)/sizeof(struct st_connection)));
3786
3907
  }
3787
3908
 
3788
 
  if (!drizzle_create(&con_slot->drizzle))
 
3909
  if ((con_slot->drizzle= drizzle_create(NULL)) == NULL)
3789
3910
    die("Failed on drizzle_create()");
3790
 
  if (opt_compress || con_compress)
3791
 
    drizzle_options(&con_slot->drizzle, DRIZZLE_OPT_COMPRESS, NULL);
3792
 
  drizzle_options(&con_slot->drizzle, DRIZZLE_OPT_LOCAL_INFILE, 0);
 
3911
  if (!drizzle_con_create(con_slot->drizzle, &con_slot->con))
 
3912
    die("Failed on drizzle_con_create()");
3793
3913
 
3794
3914
  /* Use default db name */
3795
3915
  if (ds_database.length() == 0)
3796
 
    ds_database= opt_db;
 
3916
    ds_database.append(opt_db);
3797
3917
 
3798
3918
  /* Special database to allow one to connect without a database name */
3799
3919
  if (ds_database.length() && !strcmp(ds_database.c_str(),"*NO-ONE*"))
3800
 
    ds_database= "";
 
3920
    ds_database.clear();
3801
3921
 
3802
 
  if (connect_n_handle_errors(command, &con_slot->drizzle,
 
3922
  if (connect_n_handle_errors(command, &con_slot->con,
3803
3923
                              ds_host.c_str(),ds_user.c_str(),
3804
3924
                              ds_password.c_str(), ds_database.c_str(),
3805
3925
                              con_port, ds_sock.c_str()))
3812
3932
      next_con++; /* if we used the next_con slot, advance the pointer */
3813
3933
  }
3814
3934
 
3815
 
  /* Update $drizzle_get_server_version to that of current connection */
3816
 
  var_set_drizzle_get_server_version(&cur_con->drizzle);
 
3935
  /* Update $drizzleclient_get_server_version to that of current connection */
 
3936
  var_set_drizzleclient_get_server_version(&cur_con->con);
3817
3937
 
3818
3938
  return;
3819
3939
}
3945
4065
  if (!(*p))
3946
4066
    die("Can't set empty delimiter");
3947
4067
 
3948
 
  strmake(delimiter, p, sizeof(delimiter) - 1);
 
4068
  strncpy(delimiter, p, sizeof(delimiter) - 1);
3949
4069
  delimiter_length= strlen(delimiter);
3950
4070
 
3951
4071
  command->last_argument= p + delimiter_length;
3953
4073
}
3954
4074
 
3955
4075
 
3956
 
bool match_delimiter(int c, const char *delim, uint length)
 
4076
bool match_delimiter(int c, const char *delim, uint32_t length)
3957
4077
{
3958
 
  uint i;
 
4078
  uint32_t i;
3959
4079
  char tmp[MAX_DELIMITER_LENGTH];
3960
4080
 
3961
4081
  if (c != *delim)
4026
4146
  found_eof:
4027
4147
      if (cur_file->file != stdin)
4028
4148
      {
4029
 
        my_fclose(cur_file->file, MYF(0));
 
4149
        fclose(cur_file->file);
4030
4150
        cur_file->file= 0;
4031
4151
      }
4032
4152
      free((unsigned char*) cur_file->file_name);
4255
4375
        ptr[2] && ptr[2] == '-' &&
4256
4376
        ptr[3])
4257
4377
    {
4258
 
      uint type;
 
4378
      uint32_t type;
4259
4379
      char save;
4260
4380
      char *end, *start= (char*)ptr+3;
4261
4381
      /* Skip leading spaces */
4366
4486
    return(0);
4367
4487
  }
4368
4488
  if (!(*command_ptr= command=
4369
 
        (struct st_command*) my_malloc(sizeof(*command),
4370
 
                                       MYF(MY_WME|MY_ZEROFILL))))
 
4489
        (struct st_command*) malloc(sizeof(*command))))
4371
4490
    die(NULL);
 
4491
  memset(command, 0, sizeof(*command));
4372
4492
  q_lines.push_back(command);
4373
4493
  command->type= Q_UNKNOWN;
4374
4494
 
4395
4515
  while (*p && my_isspace(charset_info, *p))
4396
4516
    p++;
4397
4517
 
4398
 
  if (!(command->query_buf= command->query= my_strdup(p, MYF(MY_WME))))
 
4518
  if (!(command->query_buf= command->query= strdup(p)))
4399
4519
    die("Out of memory");
4400
4520
 
4401
4521
  /* Calculate first word length(the command), terminated by space or ( */
4402
4522
  p= command->query;
4403
4523
  while (*p && !my_isspace(charset_info, *p) && *p != '(')
4404
4524
    p++;
4405
 
  command->first_word_len= (uint) (p - command->query);
 
4525
  command->first_word_len= (uint32_t) (p - command->query);
4406
4526
 
4407
4527
  /* Skip spaces between command and first argument */
4408
4528
  while (*p && my_isspace(charset_info, *p))
4440
4560
   GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
4441
4561
  {"include", 'i', "Include SQL before each test case.", (char**) &opt_include,
4442
4562
   (char**) &opt_include, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
4563
  {"testdir", OPT_TESTDIR, "Path to use to search for test files",
 
4564
   (char**) &opt_testdir,
 
4565
   (char**) &opt_testdir, 0,GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
4443
4566
  {"logdir", OPT_LOG_DIR, "Directory for log files", (char**) &opt_logdir,
4444
4567
   (char**) &opt_logdir, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
4445
4568
  {"mark-progress", OPT_MARK_PROGRESS,
4450
4573
   "Max number of connection attempts when connecting to server",
4451
4574
   (char**) &opt_max_connect_retries, (char**) &opt_max_connect_retries, 0,
4452
4575
   GET_INT, REQUIRED_ARG, 500, 1, 10000, 0, 0, 0},
4453
 
  {"password", 'p', "Password to use when connecting to server.",
 
4576
  {"password", 'P', "Password to use when connecting to server.",
4454
4577
   0, 0, 0, GET_STR, OPT_ARG, 0, 0, 0, 0, 0, 0},
4455
 
  {"port", 'P', "Port number to use for connection or 0 for default to, in "
4456
 
   "order of preference, my.cnf, $DRIZZLE_TCP_PORT, "
 
4578
  {"port", 'p', "Port number to use for connection or 0 for default to, in "
 
4579
   "order of preference, drizzle.cnf, $DRIZZLE_TCP_PORT, "
4457
4580
   "built-in default (" STRINGIFY_ARG(DRIZZLE_PORT) ").",
4458
 
   (char**) &opt_port,
4459
 
   (char**) &opt_port, 0, GET_INT, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
4581
   0, 0, 0, GET_UINT, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
4460
4582
  {"quiet", 's', "Suppress all normal output.", (char**) &silent,
4461
4583
   (char**) &silent, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
4462
4584
  {"record", 'r', "Record output of test_file into result file.",
4496
4618
static void print_version(void)
4497
4619
{
4498
4620
  printf("%s  Ver %s Distrib %s, for %s (%s)\n",my_progname,MTEST_VERSION,
4499
 
         drizzle_get_client_info(),SYSTEM_TYPE,MACHINE_TYPE);
 
4621
         drizzle_version(),SYSTEM_TYPE,MACHINE_TYPE);
4500
4622
}
4501
4623
 
4502
4624
static void usage(void)
4523
4645
 
4524
4646
  if (!test_if_hard_path(name))
4525
4647
  {
4526
 
    strxmov(buff, opt_basedir, name, NULL);
 
4648
    sprintf(buff,"%s%s",opt_basedir,name);
4527
4649
    name=buff;
4528
4650
  }
4529
4651
  fn_format(buff, name, "", "", MY_UNPACK_FILENAME);
4533
4655
    embedded_server_arg_count=1;
4534
4656
    embedded_server_args[0]= (char*) "";    /* Progname */
4535
4657
  }
4536
 
  if (!(file=my_fopen(buff, O_RDONLY | FILE_BINARY, MYF(MY_WME))))
 
4658
  if (!(file= fopen(buff, "r")))
4537
4659
    die("Failed to open file '%s'", buff);
4538
4660
 
4539
4661
  while (embedded_server_arg_count < MAX_EMBEDDED_SERVER_ARGS &&
4541
4663
  {
4542
4664
    *(strchr(str, '\0')-1)=0;        /* Remove end newline */
4543
4665
    if (!(embedded_server_args[embedded_server_arg_count]=
4544
 
          (char*) my_strdup(str,MYF(MY_WME))))
 
4666
          (char*) strdup(str)))
4545
4667
    {
4546
 
      my_fclose(file,MYF(0));
 
4668
      fclose(file);
4547
4669
      die("Out of memory");
4548
4670
 
4549
4671
    }
4550
4672
    embedded_server_arg_count++;
4551
4673
  }
4552
 
  my_fclose(file,MYF(0));
 
4674
  fclose(file);
4553
4675
  if (str)
4554
4676
    die("Too many arguments in option file: %s",name);
4555
4677
 
4557
4679
}
4558
4680
 
4559
4681
 
4560
 
static bool
4561
 
get_one_option(int optid, const struct my_option *opt __attribute__((unused)),
4562
 
               char *argument)
 
4682
extern "C" bool
 
4683
get_one_option(int optid, const struct my_option *, char *argument)
4563
4684
{
 
4685
  char *endchar= NULL;
 
4686
  uint64_t temp_drizzle_port= 0;
 
4687
 
4564
4688
  switch(optid) {
4565
4689
  case 'r':
4566
4690
    record = 1;
4570
4694
    char buff[FN_REFLEN];
4571
4695
    if (!test_if_hard_path(argument))
4572
4696
    {
4573
 
      strxmov(buff, opt_basedir, argument, NULL);
 
4697
      sprintf(buff,"%s%s",opt_basedir,argument);
4574
4698
      argument= buff;
4575
4699
    }
4576
4700
    fn_format(buff, argument, "", "", MY_UNPACK_FILENAME);
4577
4701
    assert(cur_file == file_stack && cur_file->file == 0);
4578
 
    if (!(cur_file->file=
4579
 
          my_fopen(buff, O_RDONLY | FILE_BINARY, MYF(0))))
 
4702
    if (!(cur_file->file= fopen(buff, "r")))
4580
4703
      die("Could not open '%s' for reading: errno = %d", buff, errno);
4581
 
    cur_file->file_name= my_strdup(buff, MYF(MY_FAE));
 
4704
    if (!(cur_file->file_name= strdup(buff)))
 
4705
      die("Out of memory");
4582
4706
    cur_file->lineno= 1;
4583
4707
    break;
4584
4708
  }
4587
4711
    static char buff[FN_REFLEN];
4588
4712
    if (!test_if_hard_path(argument))
4589
4713
    {
4590
 
      strxmov(buff, opt_basedir, argument, NULL);
 
4714
      sprintf(buff,"%s%s",opt_basedir,argument);
4591
4715
      argument= buff;
4592
4716
    }
4593
4717
    fn_format(buff, argument, "", "", MY_UNPACK_FILENAME);
4596
4720
    break;
4597
4721
  }
4598
4722
  case 'p':
 
4723
    temp_drizzle_port= (uint64_t) strtoul(argument, &endchar, 10);
 
4724
    /* if there is an alpha character this is not a valid port */
 
4725
    if (strlen(endchar) != 0)
 
4726
    {
 
4727
      fprintf(stderr, _("Non-integer value supplied for port.  If you are trying to enter a password please use --password instead.\n"));
 
4728
      exit(1);
 
4729
    }
 
4730
    /* If the port number is > 65535 it is not a valid port
 
4731
       This also helps with potential data loss casting unsigned long to a
 
4732
       uint32_t. */
 
4733
    if ((temp_drizzle_port == 0) || (temp_drizzle_port > 65535))
 
4734
    {
 
4735
      fprintf(stderr, _("Value supplied for port is not valid.\n"));
 
4736
      exit(1);
 
4737
    }
 
4738
    else
 
4739
    {
 
4740
      opt_port= (uint32_t) temp_drizzle_port;
 
4741
    }
 
4742
    break;
 
4743
  case 'P':
4599
4744
    if (argument)
4600
4745
    {
4601
 
      free(opt_pass);
4602
 
      opt_pass= my_strdup(argument, MYF(MY_FAE));
4603
 
      while (*argument) *argument++= 'x';    /* Destroy argument */
 
4746
      if (opt_pass)
 
4747
        free(opt_pass);
 
4748
      opt_pass = strdup(argument);
 
4749
      if (opt_pass == NULL)
 
4750
        die("Out of memory");
 
4751
      while (*argument)
 
4752
      {
 
4753
        /* Overwriting password with 'x' */
 
4754
        *argument++= 'x';
 
4755
      }
4604
4756
      tty_password= 0;
4605
4757
    }
4606
4758
    else
4607
4759
      tty_password= 1;
4608
4760
    break;
4609
4761
  case 't':
4610
 
    my_stpncpy(TMPDIR, argument, sizeof(TMPDIR));
 
4762
    strncpy(TMPDIR, argument, sizeof(TMPDIR));
4611
4763
    break;
4612
4764
  case 'A':
4613
4765
    if (!embedded_server_arg_count)
4617
4769
    }
4618
4770
    if (embedded_server_arg_count == MAX_EMBEDDED_SERVER_ARGS-1 ||
4619
4771
        !(embedded_server_args[embedded_server_arg_count++]=
4620
 
          my_strdup(argument, MYF(MY_FAE))))
 
4772
          strdup(argument)))
4621
4773
    {
4622
4774
      die("Can't use server argument");
4623
4775
    }
4638
4790
 
4639
4791
static int parse_args(int argc, char **argv)
4640
4792
{
4641
 
  load_defaults("my",load_default_groups,&argc,&argv);
 
4793
  load_defaults("drizzle",load_default_groups,&argc,&argv);
4642
4794
  default_argv= argv;
4643
4795
 
4644
4796
  if ((handle_options(&argc, &argv, my_long_options, get_one_option)))
4652
4804
  if (argc == 1)
4653
4805
    opt_db= *argv;
4654
4806
  if (tty_password)
4655
 
    opt_pass= get_tty_password(NULL);          /* purify tested */
 
4807
    opt_pass= client_get_tty_password(NULL);          /* purify tested */
4656
4808
  if (debug_info_flag)
4657
4809
    my_end_arg= MY_CHECK_ERROR | MY_GIVE_INFO;
4658
4810
  if (debug_check_flag)
4679
4831
  int flags= O_WRONLY | O_CREAT;
4680
4832
  if (!test_if_hard_path(fname))
4681
4833
  {
4682
 
    strxmov(buff, opt_basedir, fname, NULL);
 
4834
    sprintf(buff,"%s%s",opt_basedir,fname);
4683
4835
    fname= buff;
4684
4836
  }
4685
4837
  fn_format(buff, fname, "", "", MY_UNPACK_FILENAME);
4689
4841
  if ((fd= my_open(buff, flags,
4690
4842
                   MYF(MY_WME | MY_FFNF))) < 0)
4691
4843
    die("Could not open '%s' for writing: errno = %d", buff, errno);
4692
 
  if (append && my_seek(fd, 0, SEEK_END, MYF(0)) == MY_FILEPOS_ERROR)
 
4844
  if (append && lseek(fd, 0, SEEK_END) == MY_FILEPOS_ERROR)
4693
4845
    die("Could not find end of file '%s': errno = %d", buff, errno);
4694
4846
  if (my_write(fd, (unsigned char*)str, size, MYF(MY_WME|MY_FNABP)))
4695
4847
    die("write failed");
4748
4900
  Append the result for one field to the dynamic string ds
4749
4901
*/
4750
4902
 
4751
 
static void append_field(string *ds, uint col_idx, const DRIZZLE_FIELD* field,
 
4903
static void append_field(string *ds, uint32_t col_idx, drizzle_column_st *column,
4752
4904
                         const char* val, uint64_t len, bool is_null)
4753
4905
{
4754
4906
  if (col_idx < max_replace_column && replace_column[col_idx])
4770
4922
  }
4771
4923
  else
4772
4924
  {
4773
 
    ds->append(field->name);
 
4925
    ds->append(drizzle_column_name(column));
4774
4926
    ds->append("\t");
4775
4927
    replace_append_mem(ds, val, (int)len);
4776
4928
    ds->append("\n");
4783
4935
  Values may be converted with 'replace_column'
4784
4936
*/
4785
4937
 
4786
 
static void append_result(string *ds, DRIZZLE_RES *res)
 
4938
static void append_result(string *ds, drizzle_result_st *res)
4787
4939
{
4788
 
  DRIZZLE_ROW row;
4789
 
  uint32_t num_fields= drizzle_num_fields(res);
4790
 
  const DRIZZLE_FIELD *fields= drizzle_fetch_fields(res);
4791
 
  uint32_t *lengths;
 
4940
  drizzle_row_t row;
 
4941
  uint32_t num_fields= drizzle_result_column_count(res);
 
4942
  drizzle_column_st *column;
 
4943
  size_t *lengths;
4792
4944
 
4793
 
  while ((row = drizzle_fetch_row(res)))
 
4945
  while ((row = drizzle_row_next(res)))
4794
4946
  {
4795
4947
    uint32_t i;
4796
 
    lengths = drizzle_fetch_lengths(res);
 
4948
    lengths = drizzle_row_field_sizes(res);
 
4949
    drizzle_column_seek(res, 0);
4797
4950
    for (i = 0; i < num_fields; i++)
4798
 
      append_field(ds, i, &fields[i],
 
4951
    {
 
4952
      column= drizzle_column_next(res);
 
4953
      append_field(ds, i, column,
4799
4954
                   (const char*)row[i], lengths[i], !row[i]);
 
4955
    }
4800
4956
    if (!display_result_vertically)
4801
4957
      ds->append("\n");
4802
4958
 
4808
4964
  Append metadata for fields to output
4809
4965
*/
4810
4966
 
4811
 
static void append_metadata(string *ds,
4812
 
                            const DRIZZLE_FIELD *field,
4813
 
                            uint num_fields)
 
4967
static void append_metadata(string *ds, drizzle_result_st *res)
4814
4968
{
4815
 
  const DRIZZLE_FIELD *field_end;
 
4969
  drizzle_column_st *column;
4816
4970
  ds->append("Catalog\tDatabase\tTable\tTable_alias\tColumn\t"
4817
4971
             "Column_alias\tType\tLength\tMax length\tIs_null\t"
4818
4972
             "Flags\tDecimals\tCharsetnr\n");
4819
4973
 
4820
 
  for (field_end= field+num_fields ;
4821
 
       field < field_end ;
4822
 
       field++)
 
4974
  drizzle_column_seek(res, 0);
 
4975
  while ((column= drizzle_column_next(res)))
4823
4976
  {
4824
 
    ds->append(field->catalog,
4825
 
               field->catalog_length);
4826
 
    ds->append("\t", 1);
4827
 
    ds->append(field->db, field->db_length);
4828
 
    ds->append("\t", 1);
4829
 
    ds->append(field->org_table,
4830
 
               field->org_table_length);
4831
 
    ds->append("\t", 1);
4832
 
    ds->append(field->table,
4833
 
               field->table_length);
4834
 
    ds->append("\t", 1);
4835
 
    ds->append(field->org_name,
4836
 
               field->org_name_length);
4837
 
    ds->append("\t", 1);
4838
 
    ds->append(field->name, field->name_length);
4839
 
    ds->append("\t", 1);
4840
 
    replace_append_uint(ds, field->type);
4841
 
    ds->append("\t", 1);
4842
 
    replace_append_uint(ds, field->length);
4843
 
    ds->append("\t", 1);
4844
 
    replace_append_uint(ds, field->max_length);
4845
 
    ds->append("\t", 1);
4846
 
    ds->append((char*) ((field->flags & NOT_NULL_FLAG) ?
4847
 
                        "N" : "Y"), 1);
4848
 
    ds->append("\t", 1);
4849
 
    replace_append_uint(ds, field->flags);
4850
 
    ds->append("\t", 1);
4851
 
    replace_append_uint(ds, field->decimals);
4852
 
    ds->append("\t", 1);
4853
 
    replace_append_uint(ds, field->charsetnr);
 
4977
    ds->append(drizzle_column_catalog(column),
 
4978
               strlen(drizzle_column_catalog(column)));
 
4979
    ds->append("\t", 1);
 
4980
    ds->append(drizzle_column_db(column), strlen(drizzle_column_db(column)));
 
4981
    ds->append("\t", 1);
 
4982
    ds->append(drizzle_column_orig_table(column),
 
4983
               strlen(drizzle_column_orig_table(column)));
 
4984
    ds->append("\t", 1);
 
4985
    ds->append(drizzle_column_table(column),
 
4986
               strlen(drizzle_column_table(column)));
 
4987
    ds->append("\t", 1);
 
4988
    ds->append(drizzle_column_orig_name(column),
 
4989
               strlen(drizzle_column_orig_name(column)));
 
4990
    ds->append("\t", 1);
 
4991
    ds->append(drizzle_column_name(column),
 
4992
               strlen(drizzle_column_name(column)));
 
4993
    ds->append("\t", 1);
 
4994
    replace_append_uint(ds, drizzle_column_type_drizzle(column));
 
4995
    ds->append("\t", 1);
 
4996
    replace_append_uint(ds, drizzle_column_size(column));
 
4997
    ds->append("\t", 1);
 
4998
    replace_append_uint(ds, drizzle_column_max_size(column));
 
4999
    ds->append("\t", 1);
 
5000
    ds->append((char*) ((drizzle_column_flags(column) & DRIZZLE_COLUMN_FLAGS_NOT_NULL) ? "N" : "Y"), 1);
 
5001
    ds->append("\t", 1);
 
5002
    replace_append_uint(ds, drizzle_column_flags(column));
 
5003
    ds->append("\t", 1);
 
5004
    replace_append_uint(ds, drizzle_column_decimals(column));
 
5005
    ds->append("\t", 1);
 
5006
    replace_append_uint(ds, drizzle_column_charset(column));
4854
5007
    ds->append("\n", 1);
4855
5008
  }
4856
5009
}
4863
5016
static void append_info(string *ds, uint64_t affected_rows,
4864
5017
                        const char *info)
4865
5018
{
4866
 
  char buf[40], buff2[21];
4867
 
  sprintf(buf,"affected rows: %s\n", llstr(affected_rows, buff2));
4868
 
  ds->append(buf);
4869
 
  if (info)
 
5019
  ostringstream buf;
 
5020
  buf << "affected rows: " << affected_rows << endl;
 
5021
  ds->append(buf.str());
 
5022
  if (info && strcmp(info, ""))
4870
5023
  {
4871
5024
    ds->append("info: ");
4872
5025
    ds->append(info);
4879
5032
  Display the table headings with the names tab separated
4880
5033
*/
4881
5034
 
4882
 
static void append_table_headings(string *ds,
4883
 
                                  const DRIZZLE_FIELD *field,
4884
 
                                  uint num_fields)
 
5035
static void append_table_headings(string *ds, drizzle_result_st *res)
4885
5036
{
4886
 
  uint col_idx;
4887
 
  for (col_idx= 0; col_idx < num_fields; col_idx++)
 
5037
  uint32_t col_idx= 0;
 
5038
  drizzle_column_st *column;
 
5039
  drizzle_column_seek(res, 0);
 
5040
  while ((column= drizzle_column_next(res)))
4888
5041
  {
4889
5042
    if (col_idx)
4890
5043
      ds->append("\t", 1);
4891
 
    replace_append(ds, field[col_idx].name);
 
5044
    replace_append(ds, drizzle_column_name(column));
 
5045
    col_idx++;
4892
5046
  }
4893
5047
  ds->append("\n", 1);
4894
5048
}
4900
5054
  Number of warnings appended to ds
4901
5055
*/
4902
5056
 
4903
 
static int append_warnings(string *ds, DRIZZLE *drizzle)
 
5057
static int append_warnings(string *ds, drizzle_con_st *con,
 
5058
                           drizzle_result_st *res)
4904
5059
{
4905
 
  uint count;
4906
 
  DRIZZLE_RES *warn_res;
4907
 
 
4908
 
 
4909
 
  if (!(count= drizzle_warning_count(drizzle)))
 
5060
  uint32_t count;
 
5061
  drizzle_result_st warn_res;
 
5062
  drizzle_return_t ret;
 
5063
 
 
5064
 
 
5065
  if (!(count= drizzle_result_warning_count(res)))
4910
5066
    return(0);
4911
5067
 
4912
 
  /*
4913
 
    If one day we will support execution of multi-statements
4914
 
    through PS API we should not issue SHOW WARNINGS until
4915
 
    we have not read all results...
4916
 
  */
4917
 
  assert(!drizzle_more_results(drizzle));
4918
 
 
4919
 
  if (drizzle_real_query(drizzle, "SHOW WARNINGS", 13))
4920
 
    die("Error running query \"SHOW WARNINGS\": %s", drizzle_error(drizzle));
4921
 
 
4922
 
  if (!(warn_res= drizzle_store_result(drizzle)))
4923
 
    die("Warning count is %u but didn't get any warnings",
4924
 
        count);
4925
 
 
4926
 
  append_result(ds, warn_res);
 
5068
  if (drizzle_query_str(con, &warn_res, "SHOW WARNINGS", &ret) == NULL ||
 
5069
      ret != DRIZZLE_RETURN_OK)
 
5070
  {
 
5071
    if (ret == DRIZZLE_RETURN_ERROR_CODE)
 
5072
      die("Error running query \"SHOW WARNINGS\": %s", drizzle_result_error(&warn_res));
 
5073
    else
 
5074
      die("Error running query \"SHOW WARNINGS\": %s", drizzle_con_error(con));
 
5075
  }
 
5076
 
 
5077
  if (drizzle_result_column_count(&warn_res) == 0 ||
 
5078
      drizzle_result_buffer(&warn_res) != DRIZZLE_RETURN_OK)
 
5079
    die("Warning count is %u but didn't get any warnings", count);
 
5080
 
 
5081
  append_result(ds, &warn_res);
 
5082
  drizzle_result_free(&warn_res);
4927
5083
 
4928
5084
  return(count);
4929
5085
}
4947
5103
                             int flags, char *query, int query_len,
4948
5104
                             string *ds, string *ds_warnings)
4949
5105
{
4950
 
  DRIZZLE_RES *res= 0;
4951
 
  DRIZZLE *drizzle= &cn->drizzle;
4952
 
  int err= 0, counter= 0;
 
5106
  drizzle_result_st res;
 
5107
  drizzle_return_t ret;
 
5108
  drizzle_con_st *con= &cn->con;
 
5109
  int err= 0;
 
5110
 
 
5111
  drizzle_con_add_options(con, DRIZZLE_CON_NO_RESULT_READ);
4953
5112
 
4954
5113
  if (flags & QUERY_SEND_FLAG)
4955
5114
  {
4956
5115
    /*
4957
5116
     * Send the query
4958
5117
     */
4959
 
    if (do_send_query(cn, query, query_len, flags))
 
5118
 
 
5119
    (void) drizzle_query(con, &res, query, query_len, &ret);
 
5120
    if (ret != DRIZZLE_RETURN_OK)
4960
5121
    {
4961
 
      handle_error(command, drizzle_errno(drizzle), drizzle_error(drizzle),
4962
 
                   drizzle_sqlstate(drizzle), ds);
 
5122
      if (ret == DRIZZLE_RETURN_ERROR_CODE)
 
5123
      {
 
5124
        err= drizzle_result_error_code(&res);
 
5125
        handle_error(command, err, drizzle_result_error(&res),
 
5126
                     drizzle_result_sqlstate(&res), ds);
 
5127
        drizzle_result_free(&res);
 
5128
      }
 
5129
      else
 
5130
      {
 
5131
        handle_error(command, ret, drizzle_con_error(con), "", ds);
 
5132
        err= ret;
 
5133
      }
4963
5134
      goto end;
4964
5135
    }
4965
5136
  }
4966
5137
  if (!(flags & QUERY_REAP_FLAG))
4967
5138
    return;
4968
5139
 
4969
 
  do
4970
5140
  {
4971
5141
    /*
4972
 
      When  on first result set, call drizzle_read_query_result to retrieve
4973
 
      answer to the query sent earlier
4974
 
    */
4975
 
    if ((counter==0) && drizzle_read_query_result(drizzle))
 
5142
     * Read the result packet
 
5143
     */
 
5144
    if (drizzle_result_read(con, &res, &ret) == NULL ||
 
5145
        ret != DRIZZLE_RETURN_OK)
4976
5146
    {
4977
 
      handle_error(command, drizzle_errno(drizzle), drizzle_error(drizzle),
4978
 
                   drizzle_sqlstate(drizzle), ds);
 
5147
      if (ret == DRIZZLE_RETURN_ERROR_CODE)
 
5148
      {
 
5149
        handle_error(command, drizzle_result_error_code(&res),
 
5150
                     drizzle_result_error(&res), drizzle_result_sqlstate(&res),
 
5151
                     ds);
 
5152
      }
 
5153
      else
 
5154
        handle_error(command, ret, drizzle_con_error(con), "", ds);
 
5155
      drizzle_result_free(&res);
 
5156
      err= ret;
4979
5157
      goto end;
4980
 
 
4981
5158
    }
4982
5159
 
4983
5160
    /*
4984
5161
      Store the result of the query if it will return any fields
4985
5162
    */
4986
 
    if (drizzle_field_count(drizzle) && ((res= drizzle_store_result(drizzle)) == 0))
 
5163
    if (drizzle_result_column_count(&res) &&
 
5164
        (ret= drizzle_result_buffer(&res)) != DRIZZLE_RETURN_OK)
4987
5165
    {
4988
 
      handle_error(command, drizzle_errno(drizzle), drizzle_error(drizzle),
4989
 
                   drizzle_sqlstate(drizzle), ds);
 
5166
      if (ret == DRIZZLE_RETURN_ERROR_CODE)
 
5167
      {
 
5168
        handle_error(command, drizzle_result_error_code(&res),
 
5169
                     drizzle_result_error(&res), drizzle_result_sqlstate(&res),
 
5170
                     ds);
 
5171
      }
 
5172
      else
 
5173
        handle_error(command, ret, drizzle_con_error(con), "", ds);
 
5174
      drizzle_result_free(&res);
 
5175
      err= ret;
4990
5176
      goto end;
4991
5177
    }
4992
5178
 
4994
5180
    {
4995
5181
      uint64_t affected_rows= 0;    /* Ok to be undef if 'disable_info' is set */
4996
5182
 
4997
 
      if (res)
 
5183
      if (drizzle_result_column_count(&res))
4998
5184
      {
4999
 
        const DRIZZLE_FIELD *fields= drizzle_fetch_fields(res);
5000
 
        uint num_fields= drizzle_num_fields(res);
5001
 
 
5002
5185
        if (display_metadata)
5003
 
          append_metadata(ds, fields, num_fields);
 
5186
          append_metadata(ds, &res);
5004
5187
 
5005
5188
        if (!display_result_vertically)
5006
 
          append_table_headings(ds, fields, num_fields);
 
5189
          append_table_headings(ds, &res);
5007
5190
 
5008
 
        append_result(ds, res);
 
5191
        append_result(ds, &res);
5009
5192
      }
5010
5193
 
5011
5194
      /*
5012
 
        Need to call drizzle_affected_rows() before the "new"
 
5195
        Need to call drizzleclient_affected_rows() before the "new"
5013
5196
        query to find the warnings
5014
5197
      */
5015
5198
      if (!disable_info)
5016
 
        affected_rows= drizzle_affected_rows(drizzle);
 
5199
        affected_rows= drizzle_result_affected_rows(&res);
5017
5200
 
5018
5201
      /*
5019
5202
        Add all warnings to the result. We can't do this if we are in
5020
5203
        the middle of processing results from multi-statement, because
5021
5204
        this will break protocol.
5022
5205
      */
5023
 
      if (!disable_warnings && !drizzle_more_results(drizzle))
 
5206
      if (!disable_warnings)
5024
5207
      {
5025
 
        if (append_warnings(ds_warnings, drizzle) || ds_warnings->length())
 
5208
        drizzle_con_remove_options(con, DRIZZLE_CON_NO_RESULT_READ);
 
5209
        if (append_warnings(ds_warnings, con, &res) || ds_warnings->length())
5026
5210
        {
5027
5211
          ds->append("Warnings:\n", 10);
5028
5212
          ds->append(ds_warnings->c_str(), ds_warnings->length());
5030
5214
      }
5031
5215
 
5032
5216
      if (!disable_info)
5033
 
        append_info(ds, affected_rows, drizzle_info(drizzle));
 
5217
        append_info(ds, affected_rows, drizzle_result_info(&res));
5034
5218
    }
5035
5219
 
5036
 
    if (res)
5037
 
    {
5038
 
      drizzle_free_result(res);
5039
 
      res= 0;
5040
 
    }
5041
 
    counter++;
5042
 
  } while (!(err= drizzle_next_result(drizzle)));
5043
 
  if (err > 0)
5044
 
  {
5045
 
    /* We got an error from drizzle_next_result, maybe expected */
5046
 
    handle_error(command, drizzle_errno(drizzle), drizzle_error(drizzle),
5047
 
                 drizzle_sqlstate(drizzle), ds);
5048
 
    goto end;
 
5220
    drizzle_result_free(&res);
5049
5221
  }
5050
 
  assert(err == -1); /* Successful and there are no more results */
5051
5222
 
5052
5223
  /* If we come here the query is both executed and read successfully */
5053
5224
  handle_no_error(command);
5055
5226
end:
5056
5227
 
5057
5228
  /*
5058
 
    We save the return code (drizzle_errno(drizzle)) from the last call sent
5059
 
    to the server into the drizzletest builtin variable $drizzle_errno. This
 
5229
    We save the return code (drizzleclient_errno(drizzle)) from the last call sent
 
5230
    to the server into the drizzletest builtin variable $drizzleclient_errno. This
5060
5231
    variable then can be used from the test case itself.
5061
5232
  */
5062
 
  var_set_errno(drizzle_errno(drizzle));
 
5233
  drizzle_con_remove_options(con, DRIZZLE_CON_NO_RESULT_READ);
 
5234
  var_set_errno(err);
5063
5235
  return;
5064
5236
}
5065
5237
 
5084
5256
                  unsigned int err_errno, const char *err_error,
5085
5257
                  const char *err_sqlstate, string *ds)
5086
5258
{
5087
 
  uint i;
 
5259
  uint32_t i;
5088
5260
 
5089
5261
 
5090
5262
  if (command->require_file[0])
5094
5266
      returned a valid reponse. Don't allow 2013 or 2006 to trigger an
5095
5267
      abort_not_supported_test
5096
5268
    */
5097
 
    if (err_errno == CR_SERVER_LOST ||
5098
 
        err_errno == CR_SERVER_GONE_ERROR)
 
5269
    if (err_errno == DRIZZLE_RETURN_SERVER_GONE)
5099
5270
      die("require query '%s' failed: %d: %s", command->query,
5100
5271
          err_errno, err_error);
5101
5272
 
5107
5278
  if (command->abort_on_error)
5108
5279
    die("query '%s' failed: %d: %s", command->query, err_errno, err_error);
5109
5280
 
5110
 
  for (i= 0 ; (uint) i < command->expected_errors.count ; i++)
 
5281
  for (i= 0 ; (uint32_t) i < command->expected_errors.count ; i++)
5111
5282
  {
5112
5283
    if (((command->expected_errors.err[i].type == ERR_ERRNO) &&
5113
5284
         (command->expected_errors.err[i].code.errnum == err_errno)) ||
5114
5285
        ((command->expected_errors.err[i].type == ERR_SQLSTATE) &&
5115
5286
         (strncmp(command->expected_errors.err[i].code.sqlstate,
5116
 
                  err_sqlstate, SQLSTATE_LENGTH) == 0)))
 
5287
                  err_sqlstate, DRIZZLE_MAX_SQLSTATE_SIZE) == 0)))
5117
5288
    {
5118
5289
      if (!disable_result_log)
5119
5290
      {
5308
5479
static void get_command_type(struct st_command* command)
5309
5480
{
5310
5481
  char save;
5311
 
  uint type;
 
5482
  uint32_t type;
5312
5483
 
5313
5484
 
5314
5485
  if (*command->query == '}')
5391
5562
 
5392
5563
*/
5393
5564
 
5394
 
static void mark_progress(struct st_command* command __attribute__((unused)),
5395
 
                          int line)
 
5565
static void mark_progress(struct st_command*, int line)
5396
5566
{
5397
 
  char buf[32], *end;
5398
5567
  uint64_t timer= timer_now();
5399
5568
  if (!progress_start)
5400
5569
    progress_start= timer;
5401
5570
  timer-= progress_start;
5402
5571
 
 
5572
  ostringstream buf;
5403
5573
  /* Milliseconds since start */
5404
 
  end= int64_t2str(timer, buf, 10);
5405
 
  ds_progress.append(buf, (int)(end-buf));
5406
 
  ds_progress.append("\t", 1);
 
5574
  buf << timer << "\t";
5407
5575
 
5408
5576
  /* Parser line number */
5409
 
  end= int10_to_str(line, buf, 10);
5410
 
  ds_progress.append(buf, (int)(end-buf));
5411
 
  ds_progress.append("\t", 1);
 
5577
  buf << line << "\t";
5412
5578
 
5413
5579
  /* Filename */
5414
 
  ds_progress.append(cur_file->file_name);
5415
 
  ds_progress.append(":", 1);
 
5580
  buf << cur_file->file_name << ":";
5416
5581
 
5417
5582
  /* Line in file */
5418
 
  end= int10_to_str(cur_file->lineno, buf, 10);
5419
 
  ds_progress.append(buf, (int)(end-buf));
5420
 
 
5421
 
 
5422
 
  ds_progress.append("\n", 1);
 
5583
  buf << cur_file->lineno << endl;
 
5584
 
 
5585
  ds_progress.append(buf.str());
5423
5586
 
5424
5587
}
5425
5588
 
5428
5591
{
5429
5592
  struct st_command *command;
5430
5593
  bool q_send_flag= 0, abort_flag= 0;
5431
 
  uint command_executed= 0, last_command_executed= 0;
 
5594
  uint32_t command_executed= 0, last_command_executed= 0;
5432
5595
  char save_file[FN_REFLEN];
5433
5596
  struct stat res_info;
5434
5597
  MY_INIT(argv[0]);
5463
5626
                1024, 0, 0, get_var_key, var_free, MYF(0)))
5464
5627
    die("Variable hash initialization failed");
5465
5628
 
5466
 
  var_set_string("$DRIZZLE_SERVER_VERSION", drizzle_get_client_info());
 
5629
  var_set_string("$DRIZZLE_SERVER_VERSION", drizzle_version());
5467
5630
 
5468
5631
  memset(&master_pos, 0, sizeof(master_pos));
5469
5632
 
5482
5645
  if (cur_file == file_stack && cur_file->file == 0)
5483
5646
  {
5484
5647
    cur_file->file= stdin;
5485
 
    cur_file->file_name= my_strdup("<stdin>", MYF(MY_WME));
 
5648
    cur_file->file_name= strdup("<stdin>");
 
5649
    if (cur_file->file_name == NULL)
 
5650
      die("Out of memory");
5486
5651
    cur_file->lineno= 1;
5487
5652
  }
5488
5653
  cur_con= connections;
5489
 
  if (!( drizzle_create(&cur_con->drizzle)))
 
5654
  if ((cur_con->drizzle= drizzle_create(NULL)) == NULL)
5490
5655
    die("Failed in drizzle_create()");
5491
 
  if (opt_compress)
5492
 
    drizzle_options(&cur_con->drizzle,DRIZZLE_OPT_COMPRESS,NULL);
5493
 
  drizzle_options(&cur_con->drizzle, DRIZZLE_OPT_LOCAL_INFILE, 0);
 
5656
  if (!( drizzle_con_create(cur_con->drizzle, &cur_con->con)))
 
5657
    die("Failed in drizzle_con_create()");
5494
5658
 
5495
 
  if (!(cur_con->name = my_strdup("default", MYF(MY_WME))))
 
5659
  if (!(cur_con->name = strdup("default")))
5496
5660
    die("Out of memory");
5497
5661
 
5498
 
  safe_connect(&cur_con->drizzle, cur_con->name, opt_host, opt_user, opt_pass,
 
5662
  safe_connect(&cur_con->con, cur_con->name, opt_host, opt_user, opt_pass,
5499
5663
               opt_db, opt_port);
5500
5664
 
5501
5665
  /* Use all time until exit if no explicit 'start_timer' */
5502
5666
  timer_start= timer_now();
5503
5667
 
5504
5668
  /*
5505
 
    Initialize $drizzle_errno with -1, so we can
 
5669
    Initialize $drizzleclient_errno with -1, so we can
5506
5670
    - distinguish it from valid values ( >= 0 ) and
5507
5671
    - detect if there was never a command sent to the server
5508
5672
  */
5509
5673
  var_set_errno(-1);
5510
5674
 
5511
 
  /* Update $drizzle_get_server_version to that of current connection */
5512
 
  var_set_drizzle_get_server_version(&cur_con->drizzle);
 
5675
  /* Update $drizzleclient_get_server_version to that of current connection */
 
5676
  var_set_drizzleclient_get_server_version(&cur_con->con);
5513
5677
 
5514
5678
  if (opt_include)
5515
5679
  {
5629
5793
 
5630
5794
        if (save_file[0])
5631
5795
        {
5632
 
          strmake(command->require_file, save_file, sizeof(save_file) - 1);
 
5796
          strncpy(command->require_file, save_file, sizeof(save_file) - 1);
5633
5797
          save_file[0]= 0;
5634
5798
        }
5635
5799
        run_query(cur_con, command, flags);
5697
5861
        command->last_argument= command->end;
5698
5862
        break;
5699
5863
      case Q_PING:
5700
 
        (void) drizzle_ping(&cur_con->drizzle);
 
5864
        {
 
5865
          drizzle_result_st result;
 
5866
          drizzle_return_t ret;
 
5867
          (void) drizzle_ping(&cur_con->con, &result, &ret);
 
5868
          if (ret == DRIZZLE_RETURN_OK || ret == DRIZZLE_RETURN_ERROR_CODE)
 
5869
            drizzle_result_free(&result);
 
5870
        }
5701
5871
        break;
5702
5872
      case Q_EXEC:
5703
5873
        do_exec(command);
5715
5885
        do_set_charset(command);
5716
5886
        break;
5717
5887
      case Q_DISABLE_RECONNECT:
5718
 
        set_reconnect(&cur_con->drizzle, 0);
 
5888
        set_reconnect(&cur_con->con, 0);
5719
5889
        break;
5720
5890
      case Q_ENABLE_RECONNECT:
5721
 
        set_reconnect(&cur_con->drizzle, 1);
 
5891
        set_reconnect(&cur_con->con, 1);
5722
5892
        break;
5723
5893
      case Q_DISABLE_PARSING:
5724
5894
        if (parsing_disabled == 0)
5892
6062
{
5893
6063
  if (timer_file)
5894
6064
  {
5895
 
    char buf[32], *end;
 
6065
    ostringstream buf;
5896
6066
    uint64_t timer= timer_now() - timer_start;
5897
 
    end= int64_t2str(timer, buf, 10);
5898
 
    str_to_file(timer_file,buf, (int) (end-buf));
 
6067
    buf << timer;
 
6068
    str_to_file(timer_file,buf.str().c_str(), buf.str().size() );
5899
6069
    /* Timer has been written to the file, don't use it anymore */
5900
6070
    timer_file= 0;
5901
6071
  }
5927
6097
    die("Missing argument in %s", command->query);
5928
6098
 
5929
6099
  /* Allocate a buffer for results */
5930
 
  start= buff= (char *)my_malloc(strlen(from)+1,MYF(MY_WME | MY_FAE));
 
6100
  start= buff= (char *)malloc(strlen(from)+1);
5931
6101
  while (*from)
5932
6102
  {
5933
6103
    char *to;
5934
 
    uint column_number;
 
6104
    uint32_t column_number;
5935
6105
 
5936
6106
    to= get_string(&buff, &from, command);
5937
6107
    if (!(column_number= atoi(to)) || column_number > MAX_COLUMNS)
5940
6110
      die("Wrong number of arguments to replace_column in '%s'", command->query);
5941
6111
    to= get_string(&buff, &from, command);
5942
6112
    free(replace_column[column_number-1]);
5943
 
    replace_column[column_number-1]= my_strdup(to, MYF(MY_WME | MY_FAE));
 
6113
    replace_column[column_number-1]= strdup(to);
 
6114
    if (replace_column[column_number-1] == NULL)
 
6115
      die("Out of memory");
5944
6116
    set_if_bigger(max_replace_column, column_number);
5945
6117
  }
5946
6118
  free(start);
5950
6122
 
5951
6123
void free_replace_column()
5952
6124
{
5953
 
  uint i;
 
6125
  uint32_t i;
5954
6126
  for (i=0 ; i < max_replace_column ; i++)
5955
6127
  {
5956
6128
    if (replace_column[i])
5974
6146
  TYPELIB typelib;        /* Pointer to strings */
5975
6147
  unsigned char  *str;          /* Strings is here */
5976
6148
  uint8_t *flag;          /* Flag about each var. */
5977
 
  uint  array_allocs,max_count,length,max_length;
 
6149
  uint32_t  array_allocs,max_count,length,max_length;
5978
6150
} POINTER_ARRAY;
5979
6151
 
5980
6152
struct st_replace;
5981
 
struct st_replace *init_replace(char * *from, char * *to, uint count,
 
6153
struct st_replace *init_replace(char * *from, char * *to, uint32_t count,
5982
6154
                                char * word_end_chars);
5983
6155
int insert_pointer_name(POINTER_ARRAY *pa,char * name);
5984
6156
void replace_strings_append(struct st_replace *rep, string* ds,
5985
6157
                            const char *from, int len);
5986
6158
void free_pointer_array(POINTER_ARRAY *pa);
5987
6159
 
5988
 
struct st_replace *glob_replace;
 
6160
struct st_replace *glob_replace= NULL;
5989
6161
 
5990
6162
/*
5991
6163
  Get arguments for replace. The syntax is:
5997
6169
 
5998
6170
void do_get_replace(struct st_command *command)
5999
6171
{
6000
 
  uint i;
 
6172
  uint32_t i;
6001
6173
  char *from= command->first_argument;
6002
6174
  char *buff, *start;
6003
6175
  char word_end_chars[256], *pos;
6010
6182
  memset(&from_array, 0, sizeof(from_array));
6011
6183
  if (!*from)
6012
6184
    die("Missing argument in %s", command->query);
6013
 
  start= buff= (char *)my_malloc(strlen(from)+1,MYF(MY_WME | MY_FAE));
 
6185
  start= buff= (char *)malloc(strlen(from)+1);
6014
6186
  while (*from)
6015
6187
  {
6016
6188
    char *to= buff;
6028
6200
  *pos=0;          /* End pointer */
6029
6201
  if (!(glob_replace= init_replace((char**) from_array.typelib.type_names,
6030
6202
                                   (char**) to_array.typelib.type_names,
6031
 
                                   (uint) from_array.typelib.count,
 
6203
                                   (uint32_t) from_array.typelib.count,
6032
6204
                                   word_end_chars)))
6033
6205
    die("Can't initialize replace from '%s'", command->query);
6034
6206
  free_pointer_array(&from_array);
6059
6231
typedef struct st_replace_found {
6060
6232
  bool found;
6061
6233
  char *replace_string;
6062
 
  uint to_offset;
 
6234
  uint32_t to_offset;
6063
6235
  int from_offset;
6064
6236
} REPLACE_STRING;
6065
6237
 
6185
6357
  char* buf,*expr_end;
6186
6358
  char* p;
6187
6359
  char* buf_p;
6188
 
  uint expr_len= strlen(expr);
 
6360
  uint32_t expr_len= strlen(expr);
6189
6361
  char last_c = 0;
6190
6362
  struct st_regex reg;
6191
6363
 
6192
 
  /* my_malloc() will die on fail with MY_FAE */
6193
 
  res=(struct st_replace_regex*)my_malloc(
6194
 
                                          sizeof(*res)+expr_len ,MYF(MY_FAE+MY_WME));
 
6364
  res=(st_replace_regex*)malloc(sizeof(*res)+expr_len);
 
6365
  if (!res)
 
6366
    return NULL;
6195
6367
  my_init_dynamic_array(&res->regex_arr,sizeof(struct st_regex),128,128);
6196
6368
 
6197
6369
  buf= (char*)res + sizeof(*res);
6248
6420
      die("Out of memory");
6249
6421
  }
6250
6422
  res->odd_buf_len= res->even_buf_len= 8192;
6251
 
  res->even_buf= (char*)my_malloc(res->even_buf_len,MYF(MY_WME+MY_FAE));
6252
 
  res->odd_buf= (char*)my_malloc(res->odd_buf_len,MYF(MY_WME+MY_FAE));
 
6423
  res->even_buf= (char*)malloc(res->even_buf_len);
 
6424
  res->odd_buf= (char*)malloc(res->odd_buf_len);
6253
6425
  res->buf= res->even_buf;
6254
6426
 
6255
6427
  return res;
6281
6453
 
6282
6454
static int multi_reg_replace(struct st_replace_regex* r,char* val)
6283
6455
{
6284
 
  uint i;
 
6456
  uint32_t i;
6285
6457
  char* in_buf, *out_buf;
6286
6458
  int* buf_len_p;
6287
6459
 
6357
6529
 
6358
6530
 
6359
6531
/*
6360
 
  auxiluary macro used by reg_replace
6361
 
  makes sure the result buffer has sufficient length
6362
 
*/
6363
 
#define SECURE_REG_BUF   if (buf_len < need_buf_len)                    \
6364
 
  {                                                                     \
6365
 
    int off= res_p - buf;                                               \
6366
 
    buf= (char*)my_realloc(buf,need_buf_len,MYF(MY_WME+MY_FAE));        \
6367
 
    res_p= buf + off;                                                   \
6368
 
    buf_len= need_buf_len;                                              \
6369
 
  }                                                                     \
6370
 
                                                                        \
6371
 
/*
6372
6532
  Performs a regex substitution
6373
6533
 
6374
6534
  IN:
6384
6544
                char *replace, char *in_string, int icase)
6385
6545
{
6386
6546
  string string_to_match(in_string);
6387
 
  pcrecpp::RE_Options opt;
6388
 
 
6389
 
  if (icase)
6390
 
    opt.set_caseless(true);
6391
 
 
6392
 
  if (!pcrecpp::RE(pattern, opt).Replace(replace,&string_to_match)){
 
6547
  const char *error= NULL;
 
6548
  int erroffset;
 
6549
  int ovector[3];
 
6550
  pcre *re= pcre_compile(pattern,
 
6551
                         icase ? PCRE_CASELESS : 0,
 
6552
                         &error, &erroffset, NULL);
 
6553
  if (re == NULL)
 
6554
    return 1;
 
6555
 
 
6556
  int rc= pcre_exec(re, NULL, in_string, (int)strlen(in_string),
 
6557
                    0, 0, ovector, 3);
 
6558
  if (rc < 0)
 
6559
  {
 
6560
    pcre_free(re);
6393
6561
    return 1;
6394
6562
  }
6395
6563
 
6396
 
  const char * new_str= string_to_match.c_str();
6397
 
  *buf_len_p= strlen(new_str);
 
6564
  char *substring_to_replace= in_string + ovector[0];
 
6565
  int substring_length= ovector[1] - ovector[0];
 
6566
  *buf_len_p= strlen(in_string) - substring_length + strlen(replace);
6398
6567
  char * new_buf = (char *)malloc(*buf_len_p+1);
6399
6568
  if (new_buf == NULL)
6400
6569
  {
 
6570
    pcre_free(re);
6401
6571
    return 1;
6402
6572
  }
6403
 
  strcpy(new_buf, new_str);
6404
 
  buf_p= &new_buf;
6405
 
 
 
6573
 
 
6574
  memset(new_buf, 0, *buf_len_p+1);
 
6575
  strncpy(new_buf, in_string, substring_to_replace-in_string);
 
6576
  strncpy(new_buf+(substring_to_replace-in_string), replace, strlen(replace));
 
6577
  strncpy(new_buf+(substring_to_replace-in_string)+strlen(replace),
 
6578
          substring_to_replace + substring_length,
 
6579
          strlen(in_string)
 
6580
            - substring_length
 
6581
            - (substring_to_replace-in_string));
 
6582
  *buf_p= new_buf;
 
6583
 
 
6584
  pcre_free(re);
6406
6585
  return 0;
6407
6586
}
6408
6587
 
6409
6588
 
6410
6589
#ifndef WORD_BIT
6411
 
#define WORD_BIT (8*sizeof(uint))
 
6590
#define WORD_BIT (8*sizeof(uint32_t))
6412
6591
#endif
6413
6592
 
6414
6593
#define SET_MALLOC_HUNC 64
6415
6594
#define LAST_CHAR_CODE 259
6416
6595
 
6417
6596
typedef struct st_rep_set {
6418
 
  uint  *bits;        /* Pointer to used sets */
 
6597
  uint32_t  *bits;        /* Pointer to used sets */
6419
6598
  short next[LAST_CHAR_CODE];    /* Pointer to next sets */
6420
 
  uint  found_len;      /* Best match to date */
 
6599
  uint32_t  found_len;      /* Best match to date */
6421
6600
  int  found_offset;
6422
 
  uint  table_offset;
6423
 
  uint  size_of_bits;      /* For convinience */
 
6601
  uint32_t  table_offset;
 
6602
  uint32_t  size_of_bits;      /* For convinience */
6424
6603
} REP_SET;
6425
6604
 
6426
6605
typedef struct st_rep_sets {
6427
 
  uint    count;      /* Number of sets */
6428
 
  uint    extra;      /* Extra sets in buffer */
6429
 
  uint    invisible;    /* Sets not chown */
6430
 
  uint    size_of_bits;
 
6606
  uint32_t    count;      /* Number of sets */
 
6607
  uint32_t    extra;      /* Extra sets in buffer */
 
6608
  uint32_t    invisible;    /* Sets not chown */
 
6609
  uint32_t    size_of_bits;
6431
6610
  REP_SET  *set,*set_buffer;
6432
 
  uint    *bit_buffer;
 
6611
  uint32_t    *bit_buffer;
6433
6612
} REP_SETS;
6434
6613
 
6435
6614
typedef struct st_found_set {
6436
 
  uint table_offset;
 
6615
  uint32_t table_offset;
6437
6616
  int found_offset;
6438
6617
} FOUND_SET;
6439
6618
 
6440
6619
typedef struct st_follow {
6441
6620
  int chr;
6442
 
  uint table_offset;
6443
 
  uint len;
 
6621
  uint32_t table_offset;
 
6622
  uint32_t len;
6444
6623
} FOLLOWS;
6445
6624
 
6446
6625
 
6447
 
int init_sets(REP_SETS *sets,uint states);
 
6626
int init_sets(REP_SETS *sets,uint32_t states);
6448
6627
REP_SET *make_new_set(REP_SETS *sets);
6449
6628
void make_sets_invisible(REP_SETS *sets);
6450
6629
void free_last_set(REP_SETS *sets);
6451
6630
void free_sets(REP_SETS *sets);
6452
 
void internal_set_bit(REP_SET *set, uint bit);
6453
 
void internal_clear_bit(REP_SET *set, uint bit);
 
6631
void internal_set_bit(REP_SET *set, uint32_t bit);
 
6632
void internal_clear_bit(REP_SET *set, uint32_t bit);
6454
6633
void or_bits(REP_SET *to,REP_SET *from);
6455
6634
void copy_bits(REP_SET *to,REP_SET *from);
6456
6635
int cmp_bits(REP_SET *set1,REP_SET *set2);
6457
 
int get_next_bit(REP_SET *set,uint lastpos);
 
6636
int get_next_bit(REP_SET *set,uint32_t lastpos);
6458
6637
int find_set(REP_SETS *sets,REP_SET *find);
6459
 
int find_found(FOUND_SET *found_set,uint table_offset,
 
6638
int find_found(FOUND_SET *found_set,uint32_t table_offset,
6460
6639
               int found_offset);
6461
 
uint start_at_word(char * pos);
6462
 
uint end_of_word(char * pos);
6463
 
 
6464
 
static uint found_sets=0;
6465
 
 
6466
 
 
6467
 
static uint replace_len(char * str)
 
6640
uint32_t start_at_word(char * pos);
 
6641
uint32_t end_of_word(char * pos);
 
6642
 
 
6643
static uint32_t found_sets=0;
 
6644
 
 
6645
 
 
6646
static uint32_t replace_len(char * str)
6468
6647
{
6469
 
  uint len=0;
 
6648
  uint32_t len=0;
6470
6649
  while (*str)
6471
6650
  {
6472
6651
    if (str[0] == '\\' && str[1])
6479
6658
 
6480
6659
/* Init a replace structure for further calls */
6481
6660
 
6482
 
REPLACE *init_replace(char * *from, char * *to,uint count,
 
6661
REPLACE *init_replace(char * *from, char * *to,uint32_t count,
6483
6662
                      char * word_end_chars)
6484
6663
{
6485
6664
  static const int SPACE_CHAR= 256;
6486
6665
  static const int START_OF_LINE= 257;
6487
6666
  static const int END_OF_LINE= 258;
6488
6667
 
6489
 
  uint i,j,states,set_nr,len,result_len,max_length,found_end,bits_set,bit_nr;
 
6668
  uint32_t i,j,states,set_nr,len,result_len,max_length,found_end,bits_set,bit_nr;
6490
6669
  int used_sets,chr,default_state;
6491
6670
  char used_chars[LAST_CHAR_CODE],is_word_end[256];
6492
6671
  char * pos, *to_pos, **to_array;
6508
6687
      return(0);
6509
6688
    }
6510
6689
    states+=len+1;
6511
 
    result_len+=(uint) strlen(to[i])+1;
 
6690
    result_len+=(uint32_t) strlen(to[i])+1;
6512
6691
    if (len > max_length)
6513
6692
      max_length=len;
6514
6693
  }
6519
6698
  if (init_sets(&sets,states))
6520
6699
    return(0);
6521
6700
  found_sets=0;
6522
 
  if (!(found_set= (FOUND_SET*) my_malloc(sizeof(FOUND_SET)*max_length*count,
6523
 
                                          MYF(MY_WME))))
 
6701
  if (!(found_set= (FOUND_SET*) malloc(sizeof(FOUND_SET)*max_length*count)))
 
6702
                                
6524
6703
  {
6525
6704
    free_sets(&sets);
6526
6705
    return(0);
6530
6709
  used_sets=-1;
6531
6710
  word_states=make_new_set(&sets);    /* Start of new word */
6532
6711
  start_states=make_new_set(&sets);    /* This is first state */
6533
 
  if (!(follow=(FOLLOWS*) my_malloc((states+2)*sizeof(FOLLOWS),MYF(MY_WME))))
 
6712
  if (!(follow=(FOLLOWS*) malloc((states+2)*sizeof(FOLLOWS))))
6534
6713
  {
6535
6714
    free_sets(&sets);
6536
6715
    free(found_set);
6606
6785
    follow_ptr->table_offset=i;
6607
6786
    follow_ptr->len=len;
6608
6787
    follow_ptr++;
6609
 
    states+=(uint) len+1;
 
6788
    states+=(uint32_t) len+1;
6610
6789
  }
6611
6790
 
6612
6791
 
6722
6901
 
6723
6902
  /* Alloc replace structure for the replace-state-machine */
6724
6903
 
6725
 
  if ((replace=(REPLACE*) my_malloc(sizeof(REPLACE)*(sets.count)+
6726
 
                                    sizeof(REPLACE_STRING)*(found_sets+1)+
6727
 
                                    sizeof(char *)*count+result_len,
6728
 
                                    MYF(MY_WME | MY_ZEROFILL))))
 
6904
  if ((replace=(REPLACE*) malloc(sizeof(REPLACE)*(sets.count)+
 
6905
                                 sizeof(REPLACE_STRING)*(found_sets+1)+
 
6906
                                 sizeof(char *)*count+result_len)))
6729
6907
  {
 
6908
    memset(replace, 0, sizeof(REPLACE)*(sets.count)+
 
6909
                       sizeof(REPLACE_STRING)*(found_sets+1)+
 
6910
                       sizeof(char *)*count+result_len);
6730
6911
    rep_str=(REPLACE_STRING*) (replace+sets.count);
6731
6912
    to_array= (char **) (rep_str+found_sets+1);
6732
6913
    to_pos=(char *) (to_array+count);
6733
6914
    for (i=0 ; i < count ; i++)
6734
6915
    {
6735
6916
      to_array[i]=to_pos;
6736
 
      to_pos=my_stpcpy(to_pos,to[i])+1;
 
6917
      to_pos=strcpy(to_pos,to[i])+strlen(to[i])+1;
6737
6918
    }
6738
6919
    rep_str[0].found=1;
6739
6920
    rep_str[0].replace_string=0;
6762
6943
}
6763
6944
 
6764
6945
 
6765
 
int init_sets(REP_SETS *sets,uint states)
 
6946
int init_sets(REP_SETS *sets,uint32_t states)
6766
6947
{
6767
6948
  memset(sets, 0, sizeof(*sets));
6768
6949
  sets->size_of_bits=((states+7)/8);
6769
 
  if (!(sets->set_buffer=(REP_SET*) my_malloc(sizeof(REP_SET)*SET_MALLOC_HUNC,
6770
 
                                              MYF(MY_WME))))
 
6950
  if (!(sets->set_buffer=(REP_SET*) malloc(sizeof(REP_SET)*SET_MALLOC_HUNC)))
6771
6951
    return 1;
6772
 
  if (!(sets->bit_buffer=(uint*) my_malloc(sizeof(uint)*sets->size_of_bits*
6773
 
                                           SET_MALLOC_HUNC,MYF(MY_WME))))
 
6952
  if (!(sets->bit_buffer=(uint*) malloc(sizeof(uint32_t)*sets->size_of_bits*
 
6953
                                        SET_MALLOC_HUNC)))
6774
6954
  {
6775
6955
    free(sets->set);
6776
6956
    return 1;
6789
6969
 
6790
6970
REP_SET *make_new_set(REP_SETS *sets)
6791
6971
{
6792
 
  uint i,count,*bit_buffer;
 
6972
  uint32_t i,count,*bit_buffer;
6793
6973
  REP_SET *set;
6794
6974
  if (sets->extra)
6795
6975
  {
6796
6976
    sets->extra--;
6797
6977
    set=sets->set+ sets->count++;
6798
 
    memset(set->bits, 0, sizeof(uint)*sets->size_of_bits);
 
6978
    memset(set->bits, 0, sizeof(uint32_t)*sets->size_of_bits);
6799
6979
    memset(&set->next[0], 0, sizeof(set->next[0])*LAST_CHAR_CODE);
6800
6980
    set->found_offset=0;
6801
6981
    set->found_len=0;
6804
6984
    return set;
6805
6985
  }
6806
6986
  count=sets->count+sets->invisible+SET_MALLOC_HUNC;
6807
 
  if (!(set=(REP_SET*) my_realloc((unsigned char*) sets->set_buffer,
6808
 
                                  sizeof(REP_SET)*count,
6809
 
                                  MYF(MY_WME))))
 
6987
  if (!(set=(REP_SET*) realloc((unsigned char*) sets->set_buffer,
 
6988
                                  sizeof(REP_SET)*count)))
6810
6989
    return 0;
6811
6990
  sets->set_buffer=set;
6812
6991
  sets->set=set+sets->invisible;
6813
 
  if (!(bit_buffer=(uint*) my_realloc((unsigned char*) sets->bit_buffer,
6814
 
                                      (sizeof(uint)*sets->size_of_bits)*count,
6815
 
                                      MYF(MY_WME))))
 
6992
  if (!(bit_buffer=(uint*) realloc((unsigned char*) sets->bit_buffer,
 
6993
                                   (sizeof(uint32_t)*sets->size_of_bits)*count)))
6816
6994
    return 0;
6817
6995
  sets->bit_buffer=bit_buffer;
6818
6996
  for (i=0 ; i < count ; i++)
6838
7016
  return;
6839
7017
}
6840
7018
 
6841
 
void internal_set_bit(REP_SET *set, uint bit)
 
7019
void internal_set_bit(REP_SET *set, uint32_t bit)
6842
7020
{
6843
7021
  set->bits[bit / WORD_BIT] |= 1 << (bit % WORD_BIT);
6844
7022
  return;
6845
7023
}
6846
7024
 
6847
 
void internal_clear_bit(REP_SET *set, uint bit)
 
7025
void internal_clear_bit(REP_SET *set, uint32_t bit)
6848
7026
{
6849
7027
  set->bits[bit / WORD_BIT] &= ~ (1 << (bit % WORD_BIT));
6850
7028
  return;
6853
7031
 
6854
7032
void or_bits(REP_SET *to,REP_SET *from)
6855
7033
{
6856
 
  register uint i;
 
7034
  register uint32_t i;
6857
7035
  for (i=0 ; i < to->size_of_bits ; i++)
6858
7036
    to->bits[i]|=from->bits[i];
6859
7037
  return;
6862
7040
void copy_bits(REP_SET *to,REP_SET *from)
6863
7041
{
6864
7042
  memcpy(to->bits,from->bits,
6865
 
         (size_t) (sizeof(uint) * to->size_of_bits));
 
7043
         (size_t) (sizeof(uint32_t) * to->size_of_bits));
6866
7044
}
6867
7045
 
6868
7046
int cmp_bits(REP_SET *set1,REP_SET *set2)
6869
7047
{
6870
 
  return memcmp(set1->bits,set2->bits, sizeof(uint) * set1->size_of_bits);
 
7048
  return memcmp(set1->bits,set2->bits, sizeof(uint32_t) * set1->size_of_bits);
6871
7049
}
6872
7050
 
6873
7051
 
6874
7052
/* Get next set bit from set. */
6875
7053
 
6876
 
int get_next_bit(REP_SET *set,uint lastpos)
 
7054
int get_next_bit(REP_SET *set,uint32_t lastpos)
6877
7055
{
6878
 
  uint pos,*start,*end,bits;
 
7056
  uint32_t pos,*start,*end,bits;
6879
7057
 
6880
7058
  start=set->bits+ ((lastpos+1) / WORD_BIT);
6881
7059
  end=set->bits + set->size_of_bits;
6885
7063
    bits=start[0];
6886
7064
  if (!bits)
6887
7065
    return 0;
6888
 
  pos=(uint) (start-set->bits)*WORD_BIT;
 
7066
  pos=(uint32_t) (start-set->bits)*WORD_BIT;
6889
7067
  while (! (bits & 1))
6890
7068
  {
6891
7069
    bits>>=1;
6900
7078
 
6901
7079
int find_set(REP_SETS *sets,REP_SET *find)
6902
7080
{
6903
 
  uint i;
 
7081
  uint32_t i;
6904
7082
  for (i=0 ; i < sets->count-1 ; i++)
6905
7083
  {
6906
7084
    if (!cmp_bits(sets->set+i,find))
6919
7097
   set->next[] == -1 is reserved for end without replaces.
6920
7098
*/
6921
7099
 
6922
 
int find_found(FOUND_SET *found_set,uint table_offset, int found_offset)
 
7100
int find_found(FOUND_SET *found_set,uint32_t table_offset, int found_offset)
6923
7101
{
6924
7102
  int i;
6925
 
  for (i=0 ; (uint) i < found_sets ; i++)
 
7103
  for (i=0 ; (uint32_t) i < found_sets ; i++)
6926
7104
    if (found_set[i].table_offset == table_offset &&
6927
7105
        found_set[i].found_offset == found_offset)
6928
7106
      return -i-2;
6934
7112
 
6935
7113
/* Return 1 if regexp starts with \b or ends with \b*/
6936
7114
 
6937
 
uint start_at_word(char * pos)
 
7115
uint32_t start_at_word(char * pos)
6938
7116
{
6939
7117
  return (((!memcmp(pos, "\\b",2) && pos[2]) ||
6940
7118
           !memcmp(pos, "\\^", 2)) ? 1 : 0);
6941
7119
}
6942
7120
 
6943
 
uint end_of_word(char * pos)
 
7121
uint32_t end_of_word(char * pos)
6944
7122
{
6945
7123
  char * end= strchr(pos, '\0');
6946
7124
  return ((end > pos+2 && !memcmp(end-2, "\\b", 2)) ||
6956
7134
 
6957
7135
int insert_pointer_name(POINTER_ARRAY *pa,char * name)
6958
7136
{
6959
 
  uint i,length,old_count;
 
7137
  uint32_t i,length,old_count;
6960
7138
  unsigned char *new_pos;
6961
7139
  const char **new_array;
6962
7140
 
6964
7142
  if (! pa->typelib.count)
6965
7143
  {
6966
7144
    if (!(pa->typelib.type_names=(const char **)
6967
 
          my_malloc(((PC_MALLOC-MALLOC_OVERHEAD)/
 
7145
          malloc(((PC_MALLOC-MALLOC_OVERHEAD)/
6968
7146
                     (sizeof(char *)+sizeof(*pa->flag))*
6969
 
                     (sizeof(char *)+sizeof(*pa->flag))),MYF(MY_WME))))
 
7147
                     (sizeof(char *)+sizeof(*pa->flag))))))
6970
7148
      return(-1);
6971
 
    if (!(pa->str= (unsigned char*) my_malloc((uint) (PS_MALLOC-MALLOC_OVERHEAD),
6972
 
                                      MYF(MY_WME))))
 
7149
    if (!(pa->str= (unsigned char*) malloc(PS_MALLOC-MALLOC_OVERHEAD)))
6973
7150
    {
6974
7151
      free((char*) pa->typelib.type_names);
6975
7152
      return (-1);
6981
7158
    pa->max_length=PS_MALLOC-MALLOC_OVERHEAD;
6982
7159
    pa->array_allocs=1;
6983
7160
  }
6984
 
  length=(uint) strlen(name)+1;
 
7161
  length=(uint32_t) strlen(name)+1;
6985
7162
  if (pa->length+length >= pa->max_length)
6986
7163
  {
6987
 
    if (!(new_pos= (unsigned char*) my_realloc((unsigned char*) pa->str,
6988
 
                                       (uint) (pa->max_length+PS_MALLOC),
6989
 
                                       MYF(MY_WME))))
 
7164
    if (!(new_pos= (unsigned char*)realloc((unsigned char*)pa->str,
 
7165
                                           (size_t)(pa->max_length+PS_MALLOC))))
6990
7166
      return(1);
6991
7167
    if (new_pos != pa->str)
6992
7168
    {
7000
7176
  }
7001
7177
  if (pa->typelib.count >= pa->max_count-1)
7002
7178
  {
7003
 
    int len;
 
7179
    size_t len;
7004
7180
    pa->array_allocs++;
7005
7181
    len=(PC_MALLOC*pa->array_allocs - MALLOC_OVERHEAD);
7006
 
    if (!(new_array=(const char **) my_realloc((unsigned char*) pa->typelib.type_names,
7007
 
                                               (uint) len/
7008
 
                                               (sizeof(unsigned char*)+sizeof(*pa->flag))*
7009
 
                                               (sizeof(unsigned char*)+sizeof(*pa->flag)),
7010
 
                                               MYF(MY_WME))))
 
7182
    if (!(new_array=
 
7183
         (const char **)realloc((unsigned char*) pa->typelib.type_names,
 
7184
                                 len/
 
7185
                                  (sizeof(unsigned char*)+sizeof(*pa->flag))*
 
7186
                                  (sizeof(unsigned char*)+sizeof(*pa->flag)))))
7011
7187
      return(1);
7012
7188
    pa->typelib.type_names=new_array;
7013
7189
    old_count=pa->max_count;
7019
7195
  pa->flag[pa->typelib.count]=0;      /* Reset flag */
7020
7196
  pa->typelib.type_names[pa->typelib.count++]= (char*) pa->str+pa->length;
7021
7197
  pa->typelib.type_names[pa->typelib.count]= NULL;  /* Put end-mark */
7022
 
  my_stpcpy((char*) pa->str+pa->length,name);
 
7198
  strcpy((char*) pa->str+pa->length,name);
7023
7199
  pa->length+=length;
7024
7200
  return(0);
7025
7201
} /* insert_pointer_name */
7075
7251
  replace_append_mem(ds, val, strlen(val));
7076
7252
}
7077
7253
 
7078
 
/* Append uint to ds, with optional replace */
7079
 
void replace_append_uint(string *ds, uint val)
 
7254
/* Append uint32_t to ds, with optional replace */
 
7255
void replace_append_uint(string *ds, uint32_t val)
7080
7256
{
7081
 
  char buff[22]; /* This should be enough for any int */
7082
 
  char *end= int64_t10_to_str(val, buff, 10);
7083
 
  replace_append_mem(ds, buff, end - buff);
 
7257
  ostringstream buff;
 
7258
  buff << val;
 
7259
  replace_append_mem(ds, buff.str().c_str(), buff.str().size());
7084
7260
 
7085
7261
}
7086
7262
 
7101
7277
 
7102
7278
void append_sorted(string* ds, string *ds_input)
7103
7279
{
7104
 
  priority_queue<string> lines;
 
7280
  priority_queue<string, vector<string>, greater<string> > lines;
7105
7281
 
7106
7282
  if (ds_input->empty())
7107
7283
    return;  /* No input */
7112
7288
  if (eol_pos == string::npos)
7113
7289
    return; // We should have at least one header here
7114
7290
 
7115
 
  ds->append(ds_input->substr(0, eol_pos));
 
7291
  ds->append(ds_input->substr(0, eol_pos+1));
7116
7292
 
7117
7293
  unsigned long start_pos= eol_pos+1;
7118
7294
 
7121
7297
 
7122
7298
    eol_pos= ds_input->find_first_of('\n', start_pos);
7123
7299
    /* Find end of line */
7124
 
    lines.push(ds_input->substr(start_pos, eol_pos-start_pos));
 
7300
    lines.push(ds_input->substr(start_pos, eol_pos-start_pos+1));
7125
7301
    start_pos= eol_pos+1;
7126
7302
 
7127
7303
  } while ( eol_pos != string::npos);