~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to client/drizzletest.cc

  • Committer: devananda
  • Date: 2009-07-01 17:38:47 UTC
  • mto: (1093.1.7 captain)
  • mto: This revision was merged to the branch mainline in revision 1095.
  • Revision ID: devananda.vdv@gmail.com-20090701173847-3n3mbtessg5ff35e
refactored function/benchmark into plugin/benchmark

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 
 * 
4
 
 *  Copyright (C) 2010 Vijay Samuel
 
3
 *
5
4
 *  Copyright (C) 2008 MySQL
6
5
 *
7
6
 *  This program is free software; you can redistribute it and/or modify
45
44
#include <map>
46
45
#include <string>
47
46
#include <sstream>
48
 
#include <fstream>
49
47
#include <iostream>
50
48
#include <vector>
51
 
#include <algorithm>
52
 
#ifdef HAVE_SYS_WAIT_H
53
 
#include <sys/wait.h>
54
 
#endif
55
 
#include <cassert>
56
 
#include <sys/stat.h>
57
 
#include <sys/types.h>
58
 
#include <fcntl.h>
59
 
#include <boost/program_options.hpp>
60
49
 
61
50
#include PCRE_HEADER
62
51
 
 
52
#include <mysys/hash.h>
63
53
#include <stdarg.h>
64
 
#include <boost/unordered_map.hpp>
 
54
 
 
55
#include "errname.h"
65
56
 
66
57
/* Added this for string translation. */
67
 
#include "drizzled/gettext.h"
68
 
#include "drizzled/drizzle_time.h"
69
 
#include "drizzled/charset.h"
70
 
#include <drizzled/configmake.h>
 
58
#include <drizzled/gettext.h>
71
59
 
72
60
#ifndef DRIZZLE_RETURN_SERVER_GONE
73
61
#define DRIZZLE_RETURN_HANDSHAKE_FAILED DRIZZLE_RETURN_ERROR_CODE
74
62
#endif
75
 
namespace po= boost::program_options;
 
63
 
76
64
using namespace std;
77
 
using namespace drizzled;
78
 
 
79
 
extern "C"
80
 
unsigned char *get_var_key(const unsigned char* var, size_t *len, bool);
81
 
 
82
 
int get_one_option(int optid, const struct option *, char *argument);
83
65
 
84
66
#define MAX_VAR_NAME_LENGTH    256
85
67
#define MAX_COLUMNS            256
 
68
#define MAX_EMBEDDED_SERVER_ARGS 64
86
69
#define MAX_DELIMITER_LENGTH 16
87
70
/* Flags controlling send and reap */
88
71
#define QUERY_SEND_FLAG  1
89
72
#define QUERY_REAP_FLAG  2
90
73
 
91
 
typedef boost::unordered_map<std::string, uint32_t> ErrorCodes;
92
 
ErrorCodes global_error_names;
93
 
 
94
74
enum {
95
75
  OPT_PS_PROTOCOL, OPT_SP_PROTOCOL, OPT_CURSOR_PROTOCOL, OPT_VIEW_PROTOCOL,
96
76
  OPT_MAX_CONNECT_RETRIES, OPT_MARK_PROGRESS, OPT_LOG_DIR, OPT_TAIL_LINES,
98
78
};
99
79
 
100
80
static int record= 0, opt_sleep= -1;
101
 
static char *opt_pass= NULL;
102
 
const char *unix_sock= NULL;
 
81
static char *opt_db= NULL, *opt_pass= NULL;
 
82
const char *opt_user= NULL, *opt_host= NULL, *unix_sock= NULL,
 
83
           *opt_basedir= "./";
 
84
const char *opt_logdir= "";
 
85
const char *opt_include= NULL, *opt_charsets_dir;
 
86
const char *opt_testdir= NULL;
103
87
static uint32_t opt_port= 0;
104
 
static uint32_t opt_max_connect_retries;
105
 
static bool silent= false, verbose= false;
 
88
static int opt_max_connect_retries;
 
89
static bool opt_compress= false, silent= false, verbose= false;
 
90
static bool debug_info_flag= false, debug_check_flag= false;
 
91
static bool tty_password= false;
106
92
static bool opt_mark_progress= false;
107
93
static bool parsing_disabled= false;
108
94
static bool display_result_vertically= false,
113
99
static bool abort_on_error= true;
114
100
static bool server_initialized= false;
115
101
static bool is_windows= false;
116
 
static bool use_drizzle_protocol= false;
 
102
static char **default_argv;
 
103
static const char *load_default_groups[]= { "drizzletest", "client", 0 };
117
104
static char line_buffer[MAX_DELIMITER_LENGTH], *line_buffer_pos= line_buffer;
118
105
 
119
 
std::string opt_basedir,
120
 
  opt_charsets_dir,
121
 
  opt_db,
122
 
  opt_host,
123
 
  opt_include,
124
 
  opt_testdir,
125
 
  opt_logdir,
126
 
  password,
127
 
  opt_password,
128
 
  result_file_name,
129
 
  opt_user,
130
 
  opt_protocol;
131
 
 
132
106
static uint32_t start_lineno= 0; /* Start line of current command */
 
107
static uint32_t my_end_arg= 0;
133
108
 
134
109
/* Number of lines of the result to include in failure report */
135
110
static uint32_t opt_tail_lines= 0;
171
146
 
172
147
static const CHARSET_INFO *charset_info= &my_charset_utf8_general_ci; /* Default charset */
173
148
 
 
149
static int embedded_server_arg_count=0;
 
150
static char *embedded_server_args[MAX_EMBEDDED_SERVER_ARGS];
 
151
 
174
152
/*
175
153
  Timer related variables
176
154
  See the timer_output() definition for details
198
176
master_pos_st master_pos;
199
177
 
200
178
/* if set, all results are concated and compared against this file */
 
179
const char *result_file_name= NULL;
201
180
 
202
181
typedef struct st_var
203
182
{
215
194
/*Perl/shell-like variable registers */
216
195
VAR var_reg[10];
217
196
 
218
 
 
219
 
boost::unordered_map<string, VAR *> var_hash;
 
197
HASH var_hash;
220
198
 
221
199
struct st_connection
222
200
{
396
374
  char *query, *query_buf,*first_argument,*last_argument,*end;
397
375
  int first_word_len, query_len;
398
376
  bool abort_on_error;
399
 
  st_expected_errors expected_errors;
400
 
  string require_file;
 
377
  struct st_expected_errors expected_errors;
 
378
  char require_file[FN_REFLEN];
401
379
  enum enum_commands type;
402
 
 
403
 
  st_command()
404
 
    : query(NULL), query_buf(NULL), first_argument(NULL), last_argument(NULL),
405
 
      end(NULL), first_word_len(0), query_len(0), abort_on_error(false),
406
 
      require_file(""), type(Q_CONNECTION)
407
 
  {
408
 
    memset(&expected_errors, 0, sizeof(st_expected_errors));
409
 
  }
410
 
 
411
 
  ~st_command()
412
 
  {
413
 
    if (query_buf != NULL)
414
 
    {
415
 
      free(query_buf);
416
 
    }
417
 
  }
418
380
};
419
381
 
420
382
TYPELIB command_typelib= {array_elements(command_names),"",
438
400
VAR* var_from_env(const char *, const char *);
439
401
VAR* var_init(VAR* v, const char *name, int name_len, const char *val,
440
402
              int val_len);
441
 
void var_free(pair<string, VAR*> v);
 
403
extern "C" void var_free(void* v);
442
404
VAR* var_get(const char *var_name, const char** var_name_end,
443
405
             bool raw, bool ignore_not_existing);
444
406
void eval_expr(VAR* v, const char *p, const char** p_end);
559
521
  options are passed.
560
522
*/
561
523
 
562
 
static void append_os_quoted(string *str, const char *append, ...)
 
524
void append_os_quoted(string *str, const char *append, ...)
563
525
{
564
526
  const char *quote_str= "\'";
565
527
  const uint32_t  quote_len= 1;
782
744
    const struct command_arg *arg= &args[i];
783
745
    arg->ds->clear();
784
746
 
785
 
    bool known_arg_type= true;
786
747
    switch (arg->type) {
787
748
      /* A string */
788
749
    case ARG_STRING:
817
778
      break;
818
779
 
819
780
    default:
820
 
      known_arg_type= false;
 
781
      assert("Unknown argument type");
821
782
      break;
822
783
    }
823
 
    assert(known_arg_type);
824
784
 
825
785
    /* Check required arg */
826
786
    if (arg->ds->length() == 0 && arg->required)
912
872
 
913
873
  close_connections();
914
874
  close_files();
915
 
  for_each(var_hash.begin(), var_hash.end(), var_free);
916
 
  var_hash.clear();
 
875
  hash_free(&var_hash);
917
876
 
918
 
  vector<st_command *>::iterator iter;
 
877
  vector<struct st_command *>::iterator iter;
919
878
  for (iter= q_lines.begin() ; iter < q_lines.end() ; iter++)
920
879
  {
921
880
    struct st_command * q_line= *iter;
922
 
    delete q_line;
 
881
    if (q_line->query_buf != NULL)
 
882
    {
 
883
      free(q_line->query_buf);
 
884
    }
 
885
    free(q_line);
923
886
  }
924
887
 
925
888
  for (i= 0; i < 10; i++)
927
890
    if (var_reg[i].alloced_len)
928
891
      free(var_reg[i].str_val);
929
892
  }
 
893
  while (embedded_server_arg_count > 1)
 
894
    free(embedded_server_args[--embedded_server_arg_count]);
930
895
 
931
896
  free_all_replace();
932
897
  free(opt_pass);
 
898
  free_defaults(default_argv);
933
899
 
934
900
  return;
935
901
}
938
904
static void cleanup_and_exit(int exit_code)
939
905
{
940
906
  free_used_memory();
941
 
  internal::my_end();
 
907
  my_end(my_end_arg);
942
908
 
943
909
  if (!silent) {
944
910
    switch (exit_code) {
1011
977
  }
1012
978
 
1013
979
  /* Dump the result that has been accumulated so far to .log file */
1014
 
  if (! result_file_name.empty() && ds_res.length())
 
980
  if (result_file_name && ds_res.length())
1015
981
    dump_result_to_log_file(ds_res.c_str(), ds_res.length());
1016
982
 
1017
983
  /* Dump warning messages */
1018
 
  if (! result_file_name.empty() && ds_warning_messages.length())
 
984
  if (result_file_name && ds_warning_messages.length())
1019
985
    dump_warning_messages();
1020
986
 
1021
987
  /*
1151
1117
  uint32_t len;
1152
1118
  char buff[512];
1153
1119
 
1154
 
  if ((fd= internal::my_open(filename, O_RDONLY, MYF(0))) < 0)
 
1120
  if ((fd= my_open(filename, O_RDONLY, MYF(0))) < 0)
1155
1121
    die("Failed to open file '%s'", filename);
1156
 
  while((len= internal::my_read(fd, (unsigned char*)&buff,
 
1122
  while((len= my_read(fd, (unsigned char*)&buff,
1157
1123
                      sizeof(buff), MYF(0))) > 0)
1158
1124
  {
1159
1125
    char *p= buff, *start= buff;
1175
1141
    /* Output any chars that might be left */
1176
1142
    ds->append(start, p-start);
1177
1143
  }
1178
 
  internal::my_close(fd, MYF(0));
 
1144
  my_close(fd, MYF(0));
1179
1145
}
1180
1146
 
1181
1147
 
1351
1317
 
1352
1318
*/
1353
1319
 
1354
 
static int compare_files2(int fd, const char* filename2)
 
1320
static int compare_files2(File fd, const char* filename2)
1355
1321
{
1356
1322
  int error= RESULT_OK;
1357
 
  int fd2;
 
1323
  File fd2;
1358
1324
  uint32_t len, len2;
1359
1325
  char buff[512], buff2[512];
1360
1326
  const char *fname= filename2;
1361
1327
  string tmpfile;
1362
1328
 
1363
 
  if ((fd2= internal::my_open(fname, O_RDONLY, MYF(0))) < 0)
 
1329
  if ((fd2= my_open(fname, O_RDONLY, MYF(0))) < 0)
1364
1330
  {
1365
 
    internal::my_close(fd, MYF(0));
1366
 
    if (! opt_testdir.empty())
 
1331
    my_close(fd, MYF(0));
 
1332
    if (opt_testdir != NULL)
1367
1333
    {
1368
1334
      tmpfile= opt_testdir;
1369
1335
      if (tmpfile[tmpfile.length()] != '/')
1371
1337
      tmpfile.append(filename2);
1372
1338
      fname= tmpfile.c_str();
1373
1339
    }
1374
 
    if ((fd2= internal::my_open(fname, O_RDONLY, MYF(0))) < 0)
 
1340
    if ((fd2= my_open(fname, O_RDONLY, MYF(0))) < 0)
1375
1341
    {
1376
 
      internal::my_close(fd, MYF(0));
 
1342
      my_close(fd, MYF(0));
1377
1343
    
1378
1344
      die("Failed to open second file: '%s'", fname);
1379
1345
    }
1380
1346
  }
1381
 
  while((len= internal::my_read(fd, (unsigned char*)&buff,
 
1347
  while((len= my_read(fd, (unsigned char*)&buff,
1382
1348
                      sizeof(buff), MYF(0))) > 0)
1383
1349
  {
1384
 
    if ((len2= internal::my_read(fd2, (unsigned char*)&buff2,
 
1350
    if ((len2= my_read(fd2, (unsigned char*)&buff2,
1385
1351
                       sizeof(buff2), MYF(0))) < len)
1386
1352
    {
1387
1353
      /* File 2 was smaller */
1401
1367
      break;
1402
1368
    }
1403
1369
  }
1404
 
  if (!error && internal::my_read(fd2, (unsigned char*)&buff2,
 
1370
  if (!error && my_read(fd2, (unsigned char*)&buff2,
1405
1371
                        sizeof(buff2), MYF(0)) > 0)
1406
1372
  {
1407
1373
    /* File 1 was smaller */
1408
1374
    error= RESULT_LENGTH_MISMATCH;
1409
1375
  }
1410
1376
 
1411
 
  internal::my_close(fd2, MYF(0));
 
1377
  my_close(fd2, MYF(0));
1412
1378
 
1413
1379
  return error;
1414
1380
}
1429
1395
 
1430
1396
static int compare_files(const char* filename1, const char* filename2)
1431
1397
{
1432
 
  int fd;
 
1398
  File fd;
1433
1399
  int error;
1434
1400
 
1435
 
  if ((fd= internal::my_open(filename1, O_RDONLY, MYF(0))) < 0)
 
1401
  if ((fd= my_open(filename1, O_RDONLY, MYF(0))) < 0)
1436
1402
    die("Failed to open first file: '%s'", filename1);
1437
1403
 
1438
1404
  error= compare_files2(fd, filename2);
1439
1405
 
1440
 
  internal::my_close(fd, MYF(0));
 
1406
  my_close(fd, MYF(0));
1441
1407
 
1442
1408
  return error;
1443
1409
}
1458
1424
static int string_cmp(string* ds, const char *fname)
1459
1425
{
1460
1426
  int error;
1461
 
  int fd;
 
1427
  File fd;
1462
1428
  char temp_file_path[FN_REFLEN];
1463
1429
 
1464
 
  if ((fd= internal::create_temp_file(temp_file_path, TMPDIR,
 
1430
  if ((fd= create_temp_file(temp_file_path, NULL,
1465
1431
                            "tmp", MYF(MY_WME))) < 0)
1466
1432
    die("Failed to create temporary file for ds");
1467
1433
 
1468
1434
  /* Write ds to temporary file and set file pos to beginning*/
1469
 
  if (internal::my_write(fd, (unsigned char *) ds->c_str(), ds->length(),
 
1435
  if (my_write(fd, (unsigned char *) ds->c_str(), ds->length(),
1470
1436
               MYF(MY_FNABP | MY_WME)) ||
1471
1437
      lseek(fd, 0, SEEK_SET) == MY_FILEPOS_ERROR)
1472
1438
  {
1473
 
    internal::my_close(fd, MYF(0));
 
1439
    my_close(fd, MYF(0));
1474
1440
    /* Remove the temporary file */
1475
 
    internal::my_delete(temp_file_path, MYF(0));
 
1441
    my_delete(temp_file_path, MYF(0));
1476
1442
    die("Failed to write file '%s'", temp_file_path);
1477
1443
  }
1478
1444
 
1479
1445
  error= compare_files2(fd, fname);
1480
1446
 
1481
 
  internal::my_close(fd, MYF(0));
 
1447
  my_close(fd, MYF(0));
1482
1448
  /* Remove the temporary file */
1483
 
  internal::my_delete(temp_file_path, MYF(0));
 
1449
  my_delete(temp_file_path, MYF(0));
1484
1450
 
1485
1451
  return(error);
1486
1452
}
1503
1469
  const char* mess= "Result content mismatch\n";
1504
1470
 
1505
1471
 
1506
 
  assert(result_file_name.c_str());
1507
 
 
1508
 
  if (access(result_file_name.c_str(), F_OK) != 0)
1509
 
    die("The specified result file does not exist: '%s'", result_file_name.c_str());
1510
 
 
1511
 
  switch (string_cmp(ds, result_file_name.c_str())) {
 
1472
  assert(result_file_name);
 
1473
 
 
1474
  if (access(result_file_name, F_OK) != 0)
 
1475
    die("The specified result file does not exist: '%s'", result_file_name);
 
1476
 
 
1477
  switch (string_cmp(ds, result_file_name)) {
1512
1478
  case RESULT_OK:
1513
1479
    break; /* ok */
1514
1480
  case RESULT_LENGTH_MISMATCH:
1522
1488
    */
1523
1489
    char reject_file[FN_REFLEN];
1524
1490
    size_t reject_length;
1525
 
    internal::dirname_part(reject_file, result_file_name.c_str(), &reject_length);
 
1491
    dirname_part(reject_file, result_file_name, &reject_length);
1526
1492
 
1527
1493
    if (access(reject_file, W_OK) == 0)
1528
1494
    {
1529
1495
      /* Result file directory is writable, save reject file there */
1530
 
      internal::fn_format(reject_file, result_file_name.c_str(), NULL,
 
1496
      fn_format(reject_file, result_file_name, NULL,
1531
1497
                ".reject", MY_REPLACE_EXT);
1532
1498
    }
1533
1499
    else
1534
1500
    {
1535
1501
      /* Put reject file in opt_logdir */
1536
 
      internal::fn_format(reject_file, result_file_name.c_str(), opt_logdir.c_str(),
 
1502
      fn_format(reject_file, result_file_name, opt_logdir,
1537
1503
                ".reject", MY_REPLACE_DIR | MY_REPLACE_EXT);
1538
1504
    }
1539
1505
    str_to_file(reject_file, ds->c_str(), ds->length());
1540
1506
 
1541
1507
    ds->erase(); /* Don't create a .log file */
1542
1508
 
1543
 
    show_diff(NULL, result_file_name.c_str(), reject_file);
 
1509
    show_diff(NULL, result_file_name, reject_file);
1544
1510
    die("%s",mess);
1545
1511
    break;
1546
1512
  }
1567
1533
 
1568
1534
*/
1569
1535
 
1570
 
static void check_require(string* ds, const string &fname)
 
1536
static void check_require(string* ds, const char *fname)
1571
1537
{
1572
1538
 
1573
1539
 
1574
 
  if (string_cmp(ds, fname.c_str()))
 
1540
  if (string_cmp(ds, fname))
1575
1541
  {
1576
1542
    char reason[FN_REFLEN];
1577
 
    internal::fn_format(reason, fname.c_str(), "", "", MY_REPLACE_EXT | MY_REPLACE_DIR);
 
1543
    fn_format(reason, fname, "", "", MY_REPLACE_EXT | MY_REPLACE_DIR);
1578
1544
    abort_not_supported_test("Test requires: '%s'", reason);
1579
1545
  }
1580
1546
  return;
1625
1591
}
1626
1592
 
1627
1593
 
 
1594
extern "C"
 
1595
unsigned char *get_var_key(const unsigned char* var, size_t *len, bool)
 
1596
{
 
1597
  register char* key;
 
1598
  key = ((VAR*)var)->name;
 
1599
  *len = ((VAR*)var)->name_len;
 
1600
  return (unsigned char*)key;
 
1601
}
 
1602
 
1628
1603
 
1629
1604
VAR *var_init(VAR *v, const char *name, int name_len, const char *val,
1630
1605
              int val_len)
1662
1637
}
1663
1638
 
1664
1639
 
1665
 
void var_free(pair<string, VAR *> v)
 
1640
void var_free(void *v)
1666
1641
{
1667
 
  free(v.second->str_val);
1668
 
  free(v.second->env_s);
1669
 
  if (v.second->alloced)
1670
 
    free(v.second);
 
1642
  free(((VAR*) v)->str_val);
 
1643
  free(((VAR*) v)->env_s);
 
1644
  if (((VAR*)v)->alloced)
 
1645
    free(v);
1671
1646
}
1672
1647
 
1673
1648
 
1679
1654
    tmp = def_val;
1680
1655
 
1681
1656
  v = var_init(0, name, strlen(name), tmp, strlen(tmp));
1682
 
  string var_name(name);
1683
 
  var_hash.insert(make_pair(var_name, v));
 
1657
  my_hash_insert(&var_hash, (unsigned char*)v);
1684
1658
  return v;
1685
1659
}
1686
1660
 
1711
1685
    if (length >= MAX_VAR_NAME_LENGTH)
1712
1686
      die("Too long variable name: %s", save_var_name);
1713
1687
 
1714
 
    string save_var_name_str(save_var_name, length);
1715
 
    boost::unordered_map<string, VAR*>::iterator iter=
1716
 
      var_hash.find(save_var_name_str);
1717
 
    if (iter == var_hash.end())
 
1688
    if (!(v = (VAR*) hash_search(&var_hash, (const unsigned char*) save_var_name,
 
1689
                                 length)))
1718
1690
    {
1719
1691
      char buff[MAX_VAR_NAME_LENGTH+1];
1720
1692
      strncpy(buff, save_var_name, length);
1721
1693
      buff[length]= '\0';
1722
1694
      v= var_from_env(buff, "");
1723
1695
    }
1724
 
    else
1725
 
    {
1726
 
      v= (*iter).second;
1727
 
    }
1728
1696
    var_name--;  /* Point at last character */
1729
1697
  }
1730
1698
  else
1749
1717
 
1750
1718
static VAR *var_obtain(const char *name, int len)
1751
1719
{
1752
 
  string var_name(name, len);
1753
 
  boost::unordered_map<string, VAR*>::iterator iter=
1754
 
    var_hash.find(var_name);
1755
 
  if (iter != var_hash.end())
1756
 
    return (*iter).second;
1757
 
  VAR *v = var_init(0, name, len, "", 0);
1758
 
  var_hash.insert(make_pair(var_name, v));
 
1720
  VAR* v;
 
1721
  if ((v = (VAR*)hash_search(&var_hash, (const unsigned char *) name, len)))
 
1722
    return v;
 
1723
  v = var_init(0, name, len, "", 0);
 
1724
  my_hash_insert(&var_hash, (unsigned char*)v);
1759
1725
  return v;
1760
1726
}
1761
1727
 
2117
2083
    const size_t len= strlen(get_value_str);
2118
2084
    if (strncmp(p, get_value_str, len)==0)
2119
2085
    {
2120
 
      st_command command;
 
2086
      struct st_command command;
 
2087
      memset(&command, 0, sizeof(command));
2121
2088
      command.query= (char*)p;
2122
2089
      command.first_word_len= len;
2123
2090
      command.first_argument= command.query + len;
2154
2121
{
2155
2122
  char buff[FN_REFLEN];
2156
2123
 
2157
 
  if (!internal::test_if_hard_path(name))
 
2124
  if (!test_if_hard_path(name))
2158
2125
  {
2159
 
    snprintf(buff, sizeof(buff), "%s%s",opt_basedir.c_str(),name);
 
2126
    sprintf(buff,"%s%s",opt_basedir,name);
2160
2127
    name=buff;
2161
2128
  }
2162
 
  internal::fn_format(buff, name, "", "", MY_UNPACK_FILENAME);
 
2129
  fn_format(buff, name, "", "", MY_UNPACK_FILENAME);
2163
2130
 
2164
2131
  if (cur_file == file_stack_end)
2165
2132
    die("Source directives are nesting too deep");
2210
2177
    ; /* Do nothing */
2211
2178
  else
2212
2179
  {
2213
 
    if (! opt_testdir.empty())
 
2180
    if (opt_testdir != NULL)
2214
2181
    {
2215
2182
      string testdir(opt_testdir);
2216
2183
      if (testdir[testdir.length()] != '/')
2478
2445
                     rm_args, sizeof(rm_args)/sizeof(struct command_arg),
2479
2446
                     ' ');
2480
2447
 
2481
 
  error= internal::my_delete(ds_filename.c_str(), MYF(0)) != 0;
 
2448
  error= my_delete(ds_filename.c_str(), MYF(0)) != 0;
2482
2449
  handle_command_error(command, error);
2483
2450
  return;
2484
2451
}
2512
2479
                     sizeof(copy_file_args)/sizeof(struct command_arg),
2513
2480
                     ' ');
2514
2481
 
2515
 
  error= (internal::my_copy(ds_from_file.c_str(), ds_to_file.c_str(),
 
2482
  error= (my_copy(ds_from_file.c_str(), ds_to_file.c_str(),
2516
2483
                  MYF(MY_DONT_OVERWRITE_FILE)) != 0);
2517
2484
  handle_command_error(command, error);
2518
2485
  return;
2549
2516
  /* Parse what mode to set */
2550
2517
  istringstream buff(ds_mode);
2551
2518
  if (ds_mode.length() != 4 ||
2552
 
      (buff >> oct >> mode).fail())
 
2519
      (buff >> mode).fail())
2553
2520
    die("You must write a 4 digit octal number for mode");
2554
2521
 
2555
2522
  handle_command_error(command, chmod(ds_file.c_str(), mode));
2610
2577
                     mkdir_args, sizeof(mkdir_args)/sizeof(struct command_arg),
2611
2578
                     ' ');
2612
2579
 
2613
 
  error= mkdir(ds_dirname.c_str(), (0777 & internal::my_umask_dir)) != 0;
 
2580
  error= mkdir(ds_dirname.c_str(), (0777 & my_umask_dir)) != 0;
2614
2581
  handle_command_error(command, error);
2615
2582
  return;
2616
2583
}
2977
2944
static void do_perl(struct st_command *command)
2978
2945
{
2979
2946
  int error;
2980
 
  int fd;
 
2947
  File fd;
2981
2948
  FILE *res_file;
2982
2949
  char buf[FN_REFLEN];
2983
2950
  char temp_file_path[FN_REFLEN];
3001
2968
  read_until_delimiter(&ds_script, &ds_delimiter);
3002
2969
 
3003
2970
  /* Create temporary file name */
3004
 
  if ((fd= internal::create_temp_file(temp_file_path, getenv("MYSQLTEST_VARDIR"),
 
2971
  if ((fd= create_temp_file(temp_file_path, getenv("MYSQLTEST_VARDIR"),
3005
2972
                            "tmp", MYF(MY_WME))) < 0)
3006
2973
    die("Failed to create temporary file for perl command");
3007
 
  internal::my_close(fd, MYF(0));
 
2974
  my_close(fd, MYF(0));
3008
2975
 
3009
2976
  str_to_file(temp_file_path, ds_script.c_str(), ds_script.length());
3010
2977
 
3024
2991
  error= pclose(res_file);
3025
2992
 
3026
2993
  /* Remove the temporary file */
3027
 
  internal::my_delete(temp_file_path, MYF(0));
 
2994
  my_delete(temp_file_path, MYF(0));
3028
2995
 
3029
2996
  handle_command_error(command, WEXITSTATUS(error));
3030
2997
  return;
3130
3097
  if (!master_pos.file[0])
3131
3098
    die("Calling 'sync_with_master' without calling 'save_master_pos'");
3132
3099
 
3133
 
  snprintf(query_buf, sizeof(query_buf), "select master_pos_wait('%s', %ld)", master_pos.file,
 
3100
  sprintf(query_buf, "select master_pos_wait('%s', %ld)", master_pos.file,
3134
3101
          master_pos.pos + offset);
3135
3102
 
3136
3103
wait_for_position:
3317
3284
  bool error= false;
3318
3285
  char *p= command->first_argument;
3319
3286
  char *sleep_start, *sleep_end= command->end;
3320
 
  double sleep_val= 0;
 
3287
  double sleep_val;
3321
3288
 
3322
3289
  while (my_isspace(charset_info, *p))
3323
3290
    p++;
3324
3291
  if (!*p)
3325
3292
    die("Missing argument to %.*s", command->first_word_len, command->query);
3326
3293
  sleep_start= p;
3327
 
  /* Check that arg starts with a digit, not handled by internal::my_strtod */
 
3294
  /* Check that arg starts with a digit, not handled by my_strtod */
3328
3295
  if (!my_isdigit(charset_info, *sleep_start))
3329
3296
    die("Invalid argument to %.*s \"%s\"", command->first_word_len,
3330
3297
        command->query,command->first_argument);
3346
3313
}
3347
3314
 
3348
3315
 
3349
 
static void do_get_file_name(struct st_command *command, string &dest)
 
3316
static void do_get_file_name(struct st_command *command,
 
3317
                             char* dest, uint32_t dest_max_len)
3350
3318
{
3351
3319
  char *p= command->first_argument, *name;
3352
3320
  if (!*p)
3357
3325
  if (*p)
3358
3326
    *p++= 0;
3359
3327
  command->last_argument= p;
3360
 
  if (! opt_testdir.empty())
3361
 
  {
3362
 
    dest= opt_testdir;
3363
 
    if (dest[dest.length()] != '/')
3364
 
      dest.append("/");
3365
 
  }
3366
 
  dest.append(name);
 
3328
  strncpy(dest, name, dest_max_len - 1);
3367
3329
}
3368
3330
 
3369
3331
 
3386
3348
    abort_not_supported_test("Test requires charset '%s'", charset_name);
3387
3349
}
3388
3350
 
3389
 
static void fill_global_error_names()
3390
 
{
3391
 
  drizzle_result_st res;
3392
 
  drizzle_return_t ret;
3393
 
  drizzle_row_t row;
3394
 
  drizzle_con_st *con= &cur_con->con;
3395
 
 
3396
 
  global_error_names.clear();
3397
 
 
3398
 
  const std::string ds_query("select error_name, error_code "
3399
 
                             "from data_dictionary.errors");
3400
 
  if (drizzle_query_str(con, &res, ds_query.c_str(), &ret) == NULL ||
3401
 
      ret != DRIZZLE_RETURN_OK)
3402
 
  {
3403
 
    if (ret == DRIZZLE_RETURN_ERROR_CODE)
3404
 
    {
3405
 
      die("Error running query '%s': %d %s", ds_query.c_str(),
3406
 
          drizzle_result_error_code(&res), drizzle_result_error(&res));
3407
 
      drizzle_result_free(&res);
3408
 
    }
3409
 
    else
3410
 
    {
3411
 
      die("Error running query '%s': %d %s", ds_query.c_str(), ret,
3412
 
          drizzle_con_error(con));
3413
 
    }
3414
 
  }
3415
 
  if (drizzle_result_column_count(&res) == 0 ||
3416
 
      drizzle_result_buffer(&res) != DRIZZLE_RETURN_OK)
3417
 
  {
3418
 
    drizzle_result_free(&res);
3419
 
    die("Query '%s' didn't return a result set", ds_query.c_str());
3420
 
  }
3421
 
 
3422
 
  while ((row= drizzle_row_next(&res)) && row[0])
3423
 
  {
3424
 
    /*
3425
 
      Concatenate all fields in the first row with tab in between
3426
 
      and assign that string to the $variable
3427
 
    */
3428
 
    size_t *lengths= drizzle_row_field_sizes(&res);
3429
 
    const std::string error_name(row[0], lengths[0]);
3430
 
    const std::string error_code(row[1], lengths[1]);
3431
 
 
3432
 
    try
3433
 
    {
3434
 
      global_error_names.insert(ErrorCodes::value_type(error_name,
3435
 
                                                       boost::lexical_cast<uint32_t>(error_code)));
3436
 
    }
3437
 
    catch (boost::bad_lexical_cast &ex)
3438
 
    {
3439
 
      drizzle_result_free(&res);
3440
 
      die("Invalid error_code from Drizzle: %s", ex.what());
3441
 
    }
3442
 
 
3443
 
  }
3444
 
 
3445
 
  drizzle_result_free(&res);
3446
 
}
3447
 
 
3448
3351
static uint32_t get_errcode_from_name(char *error_name, char *error_end)
3449
3352
{
3450
 
  size_t err_name_len= error_end - error_name;
3451
 
  string error_name_s(error_name, err_name_len);
 
3353
  /* SQL error as string */
 
3354
  st_error *e= global_error_names;
3452
3355
 
3453
 
  ErrorCodes::iterator it= global_error_names.find(error_name_s);
3454
 
  if (it != global_error_names.end())
 
3356
  /* Loop through the array of known error names */
 
3357
  for (; e->name; e++)
3455
3358
  {
3456
 
    return (*it).second;
 
3359
    /*
 
3360
      If we get a match, we need to check the length of the name we
 
3361
      matched against in case it was longer than what we are checking
 
3362
      (as in ER_WRONG_VALUE vs. ER_WRONG_VALUE_COUNT).
 
3363
    */
 
3364
    if (!strncmp(error_name, e->name, (int) (error_end - error_name)) &&
 
3365
        (uint32_t) strlen(e->name) == (uint32_t) (error_end - error_name))
 
3366
    {
 
3367
      return(e->code);
 
3368
    }
3457
3369
  }
3458
 
 
3459
 
  die("Unknown SQL error name '%s'", error_name_s.c_str());
3460
 
  return 0;
 
3370
  if (!e->name)
 
3371
    die("Unknown SQL error name '%s'", error_name);
 
3372
  return(0);
3461
3373
}
3462
3374
 
3463
3375
static void do_get_errcodes(struct st_command *command)
3755
3667
*/
3756
3668
 
3757
3669
static void safe_connect(drizzle_con_st *con, const char *name,
3758
 
                         const string host, const string user, const char *pass,
3759
 
                         const string db, uint32_t port)
 
3670
                         const char *host, const char *user, const char *pass,
 
3671
                         const char *db, int port)
3760
3672
{
3761
 
  uint32_t failed_attempts= 0;
 
3673
  int failed_attempts= 0;
3762
3674
  static uint32_t connection_retry_sleep= 100000; /* Microseconds */
3763
3675
  drizzle_return_t ret;
3764
3676
 
3765
 
  drizzle_con_set_tcp(con, host.c_str(), port);
3766
 
  drizzle_con_set_auth(con, user.c_str(), pass);
3767
 
  drizzle_con_set_db(con, db.c_str());
 
3677
  drizzle_con_set_tcp(con, host, port);
 
3678
  drizzle_con_set_auth(con, user, pass);
 
3679
  drizzle_con_set_db(con, db);
3768
3680
  while((ret= drizzle_con_connect(con)) != DRIZZLE_RETURN_OK)
3769
3681
  {
3770
3682
    /*
3906
3818
 
3907
3819
static void do_connect(struct st_command *command)
3908
3820
{
3909
 
  uint32_t con_port= opt_port;
 
3821
  int con_port= opt_port;
3910
3822
  const char *con_options;
3911
 
  bool con_ssl= 0;
 
3823
  bool con_ssl= 0, con_compress= 0;
3912
3824
  struct st_connection* con_slot;
3913
3825
 
3914
3826
  string ds_connection_name;
3954
3866
    if (*ds_sock.c_str() != FN_LIBCHAR)
3955
3867
    {
3956
3868
      char buff[FN_REFLEN];
3957
 
      internal::fn_format(buff, ds_sock.c_str(), TMPDIR, "", 0);
 
3869
      fn_format(buff, ds_sock.c_str(), TMPDIR, "", 0);
3958
3870
      ds_sock.clear();
3959
3871
      ds_sock.append(buff);
3960
3872
    }
3974
3886
      end++;
3975
3887
    if (!strncmp(con_options, "SSL", 3))
3976
3888
      con_ssl= 1;
 
3889
    else if (!strncmp(con_options, "COMPRESS", 8))
 
3890
      con_compress= 1;
3977
3891
    else
3978
3892
      die("Illegal option to connect: %.*s",
3979
3893
          (int) (end - con_options), con_options);
3999
3913
    die("Failed on drizzle_create()");
4000
3914
  if (!drizzle_con_create(con_slot->drizzle, &con_slot->con))
4001
3915
    die("Failed on drizzle_con_create()");
4002
 
  drizzle_con_add_options(&con_slot->con, use_drizzle_protocol ? DRIZZLE_CON_EXPERIMENTAL : DRIZZLE_CON_MYSQL);
4003
3916
 
4004
3917
  /* Use default db name */
4005
3918
  if (ds_database.length() == 0)
4219
4132
 
4220
4133
*/
4221
4134
 
4222
 
 
4223
 
static int my_strnncoll_simple(const CHARSET_INFO * const  cs, const unsigned char *s, size_t slen,
4224
 
                               const unsigned char *t, size_t tlen,
4225
 
                               bool t_is_prefix)
4226
 
{
4227
 
  size_t len = ( slen > tlen ) ? tlen : slen;
4228
 
  unsigned char *map= cs->sort_order;
4229
 
  if (t_is_prefix && slen > tlen)
4230
 
    slen=tlen;
4231
 
  while (len--)
4232
 
  {
4233
 
    if (map[*s++] != map[*t++])
4234
 
      return ((int) map[s[-1]] - (int) map[t[-1]]);
4235
 
  }
4236
 
  /*
4237
 
    We can't use (slen - tlen) here as the result may be outside of the
4238
 
    precision of a signed int
4239
 
  */
4240
 
  return slen > tlen ? 1 : slen < tlen ? -1 : 0 ;
4241
 
}
4242
 
 
4243
4135
static int read_line(char *buf, int size)
4244
4136
{
4245
4137
  char c, last_quote= 0;
4299
4191
      }
4300
4192
      else if ((c == '{' &&
4301
4193
                (!my_strnncoll_simple(charset_info, (const unsigned char*) "while", 5,
4302
 
                                      (unsigned char*) buf, min((ptrdiff_t)5, p - buf), 0) ||
 
4194
                                      (unsigned char*) buf, cmin((long)5, p - buf), 0) ||
4303
4195
                 !my_strnncoll_simple(charset_info, (const unsigned char*) "if", 2,
4304
 
                                      (unsigned char*) buf, min((ptrdiff_t)2, p - buf), 0))))
 
4196
                                      (unsigned char*) buf, cmin((long)2, p - buf), 0))))
4305
4197
      {
4306
4198
        /* Only if and while commands can be terminated by { */
4307
4199
        *p++= c;
4378
4270
    {
4379
4271
      /* Could be a multibyte character */
4380
4272
      /* This code is based on the code in "sql_load.cc" */
 
4273
#ifdef USE_MB
4381
4274
      int charlen = my_mbcharlen(charset_info, c);
4382
4275
      /* We give up if multibyte character is started but not */
4383
4276
      /* completed before we pass buf_end */
4404
4297
        }
4405
4298
      }
4406
4299
      else
 
4300
#endif
4407
4301
        *p++= c;
4408
4302
    }
4409
4303
  }
4563
4457
  return;
4564
4458
}
4565
4459
 
 
4460
 
 
4461
 
4566
4462
/*
4567
4463
  Create a command from a set of lines
4568
4464
 
4580
4476
  terminated by new line '\n' regardless how many "delimiter" it contain.
4581
4477
*/
4582
4478
 
4583
 
#define MAX_QUERY (768*1024*2) /* 256K -- a test in sp-big is >128K */
 
4479
#define MAX_QUERY (256*1024*2) /* 256K -- a test in sp-big is >128K */
4584
4480
static char read_command_buf[MAX_QUERY];
4585
4481
 
4586
4482
static int read_command(struct st_command** command_ptr)
4594
4490
    *command_ptr= q_lines[parser.current_line];
4595
4491
    return(0);
4596
4492
  }
4597
 
  if (!(*command_ptr= command= new st_command))
4598
 
    die("command construction failed");
 
4493
  if (!(*command_ptr= command=
 
4494
        (struct st_command*) malloc(sizeof(*command))))
 
4495
    die(NULL);
 
4496
  memset(command, 0, sizeof(*command));
4599
4497
  q_lines.push_back(command);
4600
4498
  command->type= Q_UNKNOWN;
4601
4499
 
4642
4540
  return(0);
4643
4541
}
4644
4542
 
 
4543
 
 
4544
static struct my_option my_long_options[] =
 
4545
{
 
4546
  {"help", '?', "Display this help and exit.", 0, 0, 0, GET_NO_ARG, NO_ARG,
 
4547
   0, 0, 0, 0, 0, 0},
 
4548
  {"basedir", 'b', "Basedir for tests.", (char**) &opt_basedir,
 
4549
   (char**) &opt_basedir, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
4550
  {"character-sets-dir", OPT_CHARSETS_DIR,
 
4551
   "Directory where character sets are.", (char**) &opt_charsets_dir,
 
4552
   (char**) &opt_charsets_dir, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
4553
  {"compress", 'C', "Use the compressed server/client protocol.",
 
4554
   (char**) &opt_compress, (char**) &opt_compress, 0, GET_BOOL, NO_ARG, 0, 0, 0,
 
4555
   0, 0, 0},
 
4556
  {"database", 'D', "Database to use.", (char**) &opt_db, (char**) &opt_db, 0,
 
4557
   GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
4558
  {"debug-check", OPT_DEBUG_CHECK, "Check memory and open file usage at exit.",
 
4559
   (char**) &debug_check_flag, (char**) &debug_check_flag, 0,
 
4560
   GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
 
4561
  {"debug-info", OPT_DEBUG_INFO, "Print some debug info at exit.",
 
4562
   (char**) &debug_info_flag, (char**) &debug_info_flag,
 
4563
   0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
 
4564
  {"host", 'h', "Connect to host.", (char**) &opt_host, (char**) &opt_host, 0,
 
4565
   GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
4566
  {"include", 'i', "Include SQL before each test case.", (char**) &opt_include,
 
4567
   (char**) &opt_include, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
4568
  {"testdir", OPT_TESTDIR, "Path to use to search for test files",
 
4569
   (char**) &opt_testdir,
 
4570
   (char**) &opt_testdir, 0,GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
4571
  {"logdir", OPT_LOG_DIR, "Directory for log files", (char**) &opt_logdir,
 
4572
   (char**) &opt_logdir, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
4573
  {"mark-progress", OPT_MARK_PROGRESS,
 
4574
   "Write linenumber and elapsed time to <testname>.progress ",
 
4575
   (char**) &opt_mark_progress, (char**) &opt_mark_progress, 0,
 
4576
   GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
 
4577
  {"max-connect-retries", OPT_MAX_CONNECT_RETRIES,
 
4578
   "Max number of connection attempts when connecting to server",
 
4579
   (char**) &opt_max_connect_retries, (char**) &opt_max_connect_retries, 0,
 
4580
   GET_INT, REQUIRED_ARG, 500, 1, 10000, 0, 0, 0},
 
4581
  {"password", 'P', "Password to use when connecting to server.",
 
4582
   0, 0, 0, GET_STR, OPT_ARG, 0, 0, 0, 0, 0, 0},
 
4583
  {"port", 'p', "Port number to use for connection or 0 for default to, in "
 
4584
   "order of preference, drizzle.cnf, $DRIZZLE_TCP_PORT, "
 
4585
   "built-in default (" STRINGIFY_ARG(DRIZZLE_PORT) ").",
 
4586
   0, 0, 0, GET_UINT, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
4587
  {"quiet", 's', "Suppress all normal output.", (char**) &silent,
 
4588
   (char**) &silent, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
 
4589
  {"record", 'r', "Record output of test_file into result file.",
 
4590
   0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
 
4591
  {"result-file", 'R', "Read/Store result from/in this file.",
 
4592
   (char**) &result_file_name, (char**) &result_file_name, 0,
 
4593
   GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
4594
  {"server-arg", 'A', "Send option value to embedded server as a parameter.",
 
4595
   0, 0, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
4596
  {"server-file", 'F', "Read embedded server arguments from file.",
 
4597
   0, 0, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
4598
  {"silent", 's', "Suppress all normal output. Synonym for --quiet.",
 
4599
   (char**) &silent, (char**) &silent, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
 
4600
  {"sleep", 'T', "Sleep always this many seconds on sleep commands.",
 
4601
   (char**) &opt_sleep, (char**) &opt_sleep, 0, GET_INT, REQUIRED_ARG, -1, -1, 0,
 
4602
   0, 0, 0},
 
4603
  {"tail-lines", OPT_TAIL_LINES,
 
4604
   "Number of lines of the resul to include in a failure report",
 
4605
   (char**) &opt_tail_lines, (char**) &opt_tail_lines, 0,
 
4606
   GET_INT, REQUIRED_ARG, 0, 0, 10000, 0, 0, 0},
 
4607
  {"test-file", 'x', "Read test from/in this file (default stdin).",
 
4608
   0, 0, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
4609
  {"timer-file", 'm', "File where the timing in micro seconds is stored.",
 
4610
   0, 0, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
4611
  {"tmpdir", 't', "Temporary directory where sockets are put.",
 
4612
   0, 0, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
4613
  {"user", 'u', "User for login.", (char**) &opt_user, (char**) &opt_user, 0,
 
4614
   GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
4615
  {"verbose", 'v', "Write more.", (char**) &verbose, (char**) &verbose, 0,
 
4616
   GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
 
4617
  {"version", 'V', "Output version information and exit.",
 
4618
   0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
 
4619
  { 0, 0, 0, 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0}
 
4620
};
 
4621
 
 
4622
 
 
4623
static void print_version(void)
 
4624
{
 
4625
  printf("%s  Ver %s Distrib %s, for %s (%s)\n",my_progname,MTEST_VERSION,
 
4626
         drizzle_version(),SYSTEM_TYPE,MACHINE_TYPE);
 
4627
}
 
4628
 
 
4629
static void usage(void)
 
4630
{
 
4631
  print_version();
 
4632
  printf("MySQL AB, by Sasha, Matt, Monty & Jani\n");
 
4633
  printf("This software comes with ABSOLUTELY NO WARRANTY\n\n");
 
4634
  printf("Runs a test against the DRIZZLE server and compares output with a results file.\n\n");
 
4635
  printf("Usage: %s [OPTIONS] [database] < test_file\n", my_progname);
 
4636
  my_print_help(my_long_options);
 
4637
  printf("  --no-defaults       Don't read default options from any options file.\n");
 
4638
  my_print_variables(my_long_options);
 
4639
}
 
4640
 
 
4641
/*
 
4642
  Read arguments for embedded server and put them into
 
4643
  embedded_server_args[]
 
4644
*/
 
4645
 
 
4646
static void read_embedded_server_arguments(const char *name)
 
4647
{
 
4648
  char argument[1024],buff[FN_REFLEN], *str=0;
 
4649
  FILE *file;
 
4650
 
 
4651
  if (!test_if_hard_path(name))
 
4652
  {
 
4653
    sprintf(buff,"%s%s",opt_basedir,name);
 
4654
    name=buff;
 
4655
  }
 
4656
  fn_format(buff, name, "", "", MY_UNPACK_FILENAME);
 
4657
 
 
4658
  if (!embedded_server_arg_count)
 
4659
  {
 
4660
    embedded_server_arg_count=1;
 
4661
    embedded_server_args[0]= (char*) "";    /* Progname */
 
4662
  }
 
4663
  if (!(file= fopen(buff, "r")))
 
4664
    die("Failed to open file '%s'", buff);
 
4665
 
 
4666
  while (embedded_server_arg_count < MAX_EMBEDDED_SERVER_ARGS &&
 
4667
         (str=fgets(argument,sizeof(argument), file)))
 
4668
  {
 
4669
    *(strchr(str, '\0')-1)=0;        /* Remove end newline */
 
4670
    if (!(embedded_server_args[embedded_server_arg_count]=
 
4671
          (char*) strdup(str)))
 
4672
    {
 
4673
      fclose(file);
 
4674
      die("Out of memory");
 
4675
 
 
4676
    }
 
4677
    embedded_server_arg_count++;
 
4678
  }
 
4679
  fclose(file);
 
4680
  if (str)
 
4681
    die("Too many arguments in option file: %s",name);
 
4682
 
 
4683
  return;
 
4684
}
 
4685
 
 
4686
 
 
4687
extern "C" bool
 
4688
get_one_option(int optid, const struct my_option *, char *argument)
 
4689
{
 
4690
  char *endchar= NULL;
 
4691
  uint64_t temp_drizzle_port= 0;
 
4692
 
 
4693
  switch(optid) {
 
4694
  case 'r':
 
4695
    record = 1;
 
4696
    break;
 
4697
  case 'x':
 
4698
  {
 
4699
    char buff[FN_REFLEN];
 
4700
    if (!test_if_hard_path(argument))
 
4701
    {
 
4702
      sprintf(buff,"%s%s",opt_basedir,argument);
 
4703
      argument= buff;
 
4704
    }
 
4705
    fn_format(buff, argument, "", "", MY_UNPACK_FILENAME);
 
4706
    assert(cur_file == file_stack && cur_file->file == 0);
 
4707
    if (!(cur_file->file= fopen(buff, "r")))
 
4708
      die("Could not open '%s' for reading: errno = %d", buff, errno);
 
4709
    if (!(cur_file->file_name= strdup(buff)))
 
4710
      die("Out of memory");
 
4711
    cur_file->lineno= 1;
 
4712
    break;
 
4713
  }
 
4714
  case 'm':
 
4715
  {
 
4716
    static char buff[FN_REFLEN];
 
4717
    if (!test_if_hard_path(argument))
 
4718
    {
 
4719
      sprintf(buff,"%s%s",opt_basedir,argument);
 
4720
      argument= buff;
 
4721
    }
 
4722
    fn_format(buff, argument, "", "", MY_UNPACK_FILENAME);
 
4723
    timer_file= buff;
 
4724
    unlink(timer_file);       /* Ignore error, may not exist */
 
4725
    break;
 
4726
  }
 
4727
  case 'p':
 
4728
    temp_drizzle_port= (uint64_t) strtoul(argument, &endchar, 10);
 
4729
    /* if there is an alpha character this is not a valid port */
 
4730
    if (strlen(endchar) != 0)
 
4731
    {
 
4732
      fprintf(stderr, _("Non-integer value supplied for port.  If you are trying to enter a password please use --password instead.\n"));
 
4733
      exit(1);
 
4734
    }
 
4735
    /* If the port number is > 65535 it is not a valid port
 
4736
       This also helps with potential data loss casting unsigned long to a
 
4737
       uint32_t. */
 
4738
    if ((temp_drizzle_port == 0) || (temp_drizzle_port > 65535))
 
4739
    {
 
4740
      fprintf(stderr, _("Value supplied for port is not valid.\n"));
 
4741
      exit(1);
 
4742
    }
 
4743
    else
 
4744
    {
 
4745
      opt_port= (uint32_t) temp_drizzle_port;
 
4746
    }
 
4747
    break;
 
4748
  case 'P':
 
4749
    if (argument)
 
4750
    {
 
4751
      if (opt_pass)
 
4752
        free(opt_pass);
 
4753
      opt_pass = strdup(argument);
 
4754
      if (opt_pass == NULL)
 
4755
        die("Out of memory");
 
4756
      while (*argument)
 
4757
      {
 
4758
        /* Overwriting password with 'x' */
 
4759
        *argument++= 'x';
 
4760
      }
 
4761
      tty_password= 0;
 
4762
    }
 
4763
    else
 
4764
      tty_password= 1;
 
4765
    break;
 
4766
  case 't':
 
4767
    strncpy(TMPDIR, argument, sizeof(TMPDIR));
 
4768
    break;
 
4769
  case 'A':
 
4770
    if (!embedded_server_arg_count)
 
4771
    {
 
4772
      embedded_server_arg_count=1;
 
4773
      embedded_server_args[0]= (char*) "";
 
4774
    }
 
4775
    if (embedded_server_arg_count == MAX_EMBEDDED_SERVER_ARGS-1 ||
 
4776
        !(embedded_server_args[embedded_server_arg_count++]=
 
4777
          strdup(argument)))
 
4778
    {
 
4779
      die("Can't use server argument");
 
4780
    }
 
4781
    break;
 
4782
  case 'F':
 
4783
    read_embedded_server_arguments(argument);
 
4784
    break;
 
4785
  case 'V':
 
4786
    print_version();
 
4787
    exit(0);
 
4788
  case '?':
 
4789
    usage();
 
4790
    exit(0);
 
4791
  }
 
4792
  return 0;
 
4793
}
 
4794
 
 
4795
 
 
4796
static int parse_args(int argc, char **argv)
 
4797
{
 
4798
  load_defaults("drizzle",load_default_groups,&argc,&argv);
 
4799
  default_argv= argv;
 
4800
 
 
4801
  if ((handle_options(&argc, &argv, my_long_options, get_one_option)))
 
4802
    exit(1);
 
4803
 
 
4804
  if (argc > 1)
 
4805
  {
 
4806
    usage();
 
4807
    exit(1);
 
4808
  }
 
4809
  if (argc == 1)
 
4810
    opt_db= *argv;
 
4811
  if (tty_password)
 
4812
    opt_pass= client_get_tty_password(NULL);          /* purify tested */
 
4813
  if (debug_info_flag)
 
4814
    my_end_arg= MY_CHECK_ERROR | MY_GIVE_INFO;
 
4815
  if (debug_check_flag)
 
4816
    my_end_arg= MY_CHECK_ERROR;
 
4817
 
 
4818
  return 0;
 
4819
}
 
4820
 
4645
4821
/*
4646
4822
  Write the content of str into file
4647
4823
 
4658
4834
  int fd;
4659
4835
  char buff[FN_REFLEN];
4660
4836
  int flags= O_WRONLY | O_CREAT;
4661
 
  if (!internal::test_if_hard_path(fname))
 
4837
  if (!test_if_hard_path(fname))
4662
4838
  {
4663
 
    snprintf(buff, sizeof(buff), "%s%s",opt_basedir.c_str(),fname);
 
4839
    sprintf(buff,"%s%s",opt_basedir,fname);
4664
4840
    fname= buff;
4665
4841
  }
4666
 
  internal::fn_format(buff, fname, "", "", MY_UNPACK_FILENAME);
 
4842
  fn_format(buff, fname, "", "", MY_UNPACK_FILENAME);
4667
4843
 
4668
4844
  if (!append)
4669
4845
    flags|= O_TRUNC;
4670
 
  if ((fd= internal::my_open(buff, flags,
 
4846
  if ((fd= my_open(buff, flags,
4671
4847
                   MYF(MY_WME | MY_FFNF))) < 0)
4672
4848
    die("Could not open '%s' for writing: errno = %d", buff, errno);
4673
4849
  if (append && lseek(fd, 0, SEEK_END) == MY_FILEPOS_ERROR)
4674
4850
    die("Could not find end of file '%s': errno = %d", buff, errno);
4675
 
  if (internal::my_write(fd, (unsigned char*)str, size, MYF(MY_WME|MY_FNABP)))
 
4851
  if (my_write(fd, (unsigned char*)str, size, MYF(MY_WME|MY_FNABP)))
4676
4852
    die("write failed");
4677
 
  internal::my_close(fd, MYF(0));
 
4853
  my_close(fd, MYF(0));
4678
4854
}
4679
4855
 
4680
4856
/*
4696
4872
void dump_result_to_log_file(const char *buf, int size)
4697
4873
{
4698
4874
  char log_file[FN_REFLEN];
4699
 
  str_to_file(internal::fn_format(log_file, result_file_name.c_str(), opt_logdir.c_str(), ".log",
4700
 
                        ! opt_logdir.empty() ? MY_REPLACE_DIR | MY_REPLACE_EXT :
 
4875
  str_to_file(fn_format(log_file, result_file_name, opt_logdir, ".log",
 
4876
                        *opt_logdir ? MY_REPLACE_DIR | MY_REPLACE_EXT :
4701
4877
                        MY_REPLACE_EXT),
4702
4878
              buf, size);
4703
4879
  fprintf(stderr, "\nMore results from queries before failure can be found in %s\n",
4707
4883
void dump_progress(void)
4708
4884
{
4709
4885
  char progress_file[FN_REFLEN];
4710
 
  str_to_file(internal::fn_format(progress_file, result_file_name.c_str(),
4711
 
                        opt_logdir.c_str(), ".progress",
4712
 
                        ! opt_logdir.empty() ? MY_REPLACE_DIR | MY_REPLACE_EXT :
 
4886
  str_to_file(fn_format(progress_file, result_file_name,
 
4887
                        opt_logdir, ".progress",
 
4888
                        *opt_logdir ? MY_REPLACE_DIR | MY_REPLACE_EXT :
4713
4889
                        MY_REPLACE_EXT),
4714
4890
              ds_progress.c_str(), ds_progress.length());
4715
4891
}
4718
4894
{
4719
4895
  char warn_file[FN_REFLEN];
4720
4896
 
4721
 
  str_to_file(internal::fn_format(warn_file, result_file_name.c_str(), opt_logdir.c_str(), ".warnings",
4722
 
                        ! opt_logdir.empty() ? MY_REPLACE_DIR | MY_REPLACE_EXT :
 
4897
  str_to_file(fn_format(warn_file, result_file_name, opt_logdir, ".warnings",
 
4898
                        *opt_logdir ? MY_REPLACE_DIR | MY_REPLACE_EXT :
4723
4899
                        MY_REPLACE_EXT),
4724
4900
              ds_warning_messages.c_str(), ds_warning_messages.length());
4725
4901
}
5023
5199
      }
5024
5200
 
5025
5201
      /*
5026
 
        Need to call drizzle_result_affected_rows() before the "new"
 
5202
        Need to call drizzleclient_affected_rows() before the "new"
5027
5203
        query to find the warnings
5028
5204
      */
5029
5205
      if (!disable_info)
5090
5266
  uint32_t i;
5091
5267
 
5092
5268
 
5093
 
  if (! command->require_file.empty())
 
5269
  if (command->require_file[0])
5094
5270
  {
5095
5271
    /*
5096
5272
      The query after a "--require" failed. This is fine as long the server
5249
5425
    Create a temporary dynamic string to contain the output from
5250
5426
    this query.
5251
5427
  */
5252
 
  if (! command->require_file.empty())
 
5428
  if (command->require_file[0])
5253
5429
  {
5254
5430
    ds= &ds_result;
5255
5431
  }
5292
5468
    ds= save_ds;
5293
5469
  }
5294
5470
 
5295
 
  if (! command->require_file.empty())
 
5471
  if (command->require_file[0])
5296
5472
  {
5297
5473
    /* A result file was specified for _this_ query
5298
5474
       and the output should be checked against an already
5417
5593
 
5418
5594
}
5419
5595
 
5420
 
static void check_retries(uint32_t in_opt_max_connect_retries)
5421
 
{
5422
 
  if (in_opt_max_connect_retries > 10000 || opt_max_connect_retries<1)
5423
 
  {
5424
 
    cout << N_("Error: Invalid Value for opt_max_connect_retries"); 
5425
 
    exit(-1);
5426
 
  }
5427
 
  opt_max_connect_retries= in_opt_max_connect_retries;
5428
 
}
5429
 
 
5430
 
static void check_tail_lines(uint32_t in_opt_tail_lines)
5431
 
{
5432
 
  if (in_opt_tail_lines > 10000)
5433
 
  {
5434
 
    cout << N_("Error: Invalid Value for opt_tail_lines"); 
5435
 
    exit(-1);
5436
 
  }
5437
 
  opt_tail_lines= in_opt_tail_lines;
5438
 
}
5439
 
 
5440
 
static void check_sleep(int32_t in_opt_sleep)
5441
 
{
5442
 
  if (in_opt_sleep < -1)
5443
 
  {
5444
 
    cout << N_("Error: Invalid Value for opt_sleep"); 
5445
 
    exit(-1);
5446
 
  }
5447
 
  opt_sleep= in_opt_sleep;
5448
 
}
5449
5596
 
5450
5597
int main(int argc, char **argv)
5451
5598
{
5452
 
try
5453
 
{
5454
5599
  struct st_command *command;
5455
5600
  bool q_send_flag= 0, abort_flag= 0;
5456
5601
  uint32_t command_executed= 0, last_command_executed= 0;
5457
 
  string save_file("");
 
5602
  char save_file[FN_REFLEN];
5458
5603
  struct stat res_info;
 
5604
  MY_INIT(argv[0]);
5459
5605
 
 
5606
  save_file[0]= 0;
5460
5607
  TMPDIR[0]= 0;
5461
5608
 
5462
 
  internal::my_init();
5463
 
 
5464
 
  po::options_description commandline_options("Options used only in command line");
5465
 
  commandline_options.add_options()
5466
 
  ("help,?", "Display this help and exit.")
5467
 
  ("mark-progress", po::value<bool>(&opt_mark_progress)->default_value(false)->zero_tokens(),
5468
 
  "Write linenumber and elapsed time to <testname>.progress ")
5469
 
  ("sleep,T", po::value<int32_t>(&opt_sleep)->default_value(-1)->notifier(&check_sleep),
5470
 
  "Sleep always this many seconds on sleep commands.")
5471
 
  ("test-file,x", po::value<string>(),
5472
 
  "Read test from/in this file (default stdin).")
5473
 
  ("timer-file,f", po::value<string>(),
5474
 
  "File where the timing in micro seconds is stored.")
5475
 
  ("tmpdir,t", po::value<string>(),
5476
 
  "Temporary directory where sockets are put.")
5477
 
  ("verbose,v", po::value<bool>(&verbose)->default_value(false),
5478
 
  "Write more.")
5479
 
  ("version,V", "Output version information and exit.")
5480
 
  ("no-defaults", po::value<bool>()->default_value(false)->zero_tokens(),
5481
 
  "Configuration file defaults are not used if no-defaults is set")
5482
 
  ;
5483
 
 
5484
 
  po::options_description test_options("Options specific to the drizzleimport");
5485
 
  test_options.add_options()
5486
 
  ("basedir,b", po::value<string>(&opt_basedir)->default_value(""),
5487
 
  "Basedir for tests.")
5488
 
  ("character-sets-dir", po::value<string>(&opt_charsets_dir)->default_value(""),
5489
 
  "Directory where character sets are.")
5490
 
  ("database,D", po::value<string>(&opt_db)->default_value(""),
5491
 
  "Database to use.")
5492
 
  ("include,i", po::value<string>(&opt_include)->default_value(""),
5493
 
  "Include SQL before each test case.")  
5494
 
  ("testdir", po::value<string>(&opt_testdir)->default_value(""),
5495
 
  "Path to use to search for test files")
5496
 
  ("logdir", po::value<string>(&opt_logdir)->default_value(""),
5497
 
  "Directory for log files")
5498
 
  ("max-connect-retries", po::value<uint32_t>(&opt_max_connect_retries)->default_value(500)->notifier(&check_retries),
5499
 
  "Max number of connection attempts when connecting to server")
5500
 
  ("quiet,s", po::value<bool>(&silent)->default_value(false)->zero_tokens(),
5501
 
  "Suppress all normal output.")
5502
 
  ("record,r", "Record output of test_file into result file.")
5503
 
  ("result-file,R", po::value<string>(&result_file_name)->default_value(""),
5504
 
  "Read/Store result from/in this file.")
5505
 
  ("silent,s", po::value<bool>(&silent)->default_value(false)->zero_tokens(),
5506
 
  "Suppress all normal output. Synonym for --quiet.")
5507
 
  ("tail-lines", po::value<uint32_t>(&opt_tail_lines)->default_value(0)->notifier(&check_tail_lines),
5508
 
  "Number of lines of the resul to include in a failure report")
5509
 
  ;
5510
 
 
5511
 
  po::options_description client_options("Options specific to the client");
5512
 
  client_options.add_options()
5513
 
 
5514
 
  ("host,h", po::value<string>(&opt_host)->default_value("localhost"),
5515
 
  "Connect to host.")
5516
 
  ("password,P", po::value<string>(&password)->default_value("PASSWORD_SENTINEL"),
5517
 
  "Password to use when connecting to server.")
5518
 
  ("port,p", po::value<uint32_t>(&opt_port)->default_value(0),
5519
 
  "Port number to use for connection or 0 for default")
5520
 
  ("protocol", po::value<string>(&opt_protocol),
5521
 
  "The protocol of connection (mysql or drizzle).")
5522
 
  ("user,u", po::value<string>(&opt_user)->default_value(""),
5523
 
  "User for login.")
5524
 
  ;
5525
 
 
5526
 
  po::positional_options_description p;
5527
 
  p.add("database", 1);
5528
 
 
5529
 
  po::options_description long_options("Allowed Options");
5530
 
  long_options.add(commandline_options).add(test_options).add(client_options);
5531
 
 
5532
 
  std::string system_config_dir_test(SYSCONFDIR); 
5533
 
  system_config_dir_test.append("/drizzle/drizzletest.cnf");
5534
 
 
5535
 
  std::string system_config_dir_client(SYSCONFDIR); 
5536
 
  system_config_dir_client.append("/drizzle/client.cnf");
5537
 
 
5538
 
  std::string user_config_dir((getenv("XDG_CONFIG_HOME")? getenv("XDG_CONFIG_HOME"):"~/.config"));
5539
 
 
5540
 
  if (user_config_dir.compare(0, 2, "~/") == 0)
5541
 
  {
5542
 
    char *homedir;
5543
 
    homedir= getenv("HOME");
5544
 
    if (homedir != NULL)
5545
 
      user_config_dir.replace(0, 1, homedir);
5546
 
  }
5547
 
 
5548
 
  po::variables_map vm;
5549
 
 
5550
 
  // Disable allow_guessing
5551
 
  int style = po::command_line_style::default_style & ~po::command_line_style::allow_guessing;
5552
 
 
5553
 
  po::store(po::command_line_parser(argc, argv).options(long_options).
5554
 
            style(style).positional(p).extra_parser(parse_password_arg).run(),
5555
 
            vm);
5556
 
 
5557
 
  if (! vm["no-defaults"].as<bool>())
5558
 
  {
5559
 
    std::string user_config_dir_test(user_config_dir);
5560
 
    user_config_dir_test.append("/drizzle/drizzletest.cnf"); 
5561
 
 
5562
 
    std::string user_config_dir_client(user_config_dir);
5563
 
    user_config_dir_client.append("/drizzle/client.cnf");
5564
 
 
5565
 
    ifstream user_test_ifs(user_config_dir_test.c_str());
5566
 
    po::store(parse_config_file(user_test_ifs, test_options), vm);
5567
 
 
5568
 
    ifstream user_client_ifs(user_config_dir_client.c_str());
5569
 
    po::store(parse_config_file(user_client_ifs, client_options), vm);
5570
 
 
5571
 
    ifstream system_test_ifs(system_config_dir_test.c_str());
5572
 
    store(parse_config_file(system_test_ifs, test_options), vm);
5573
 
 
5574
 
    ifstream system_client_ifs(system_config_dir_client.c_str());
5575
 
    po::store(parse_config_file(system_client_ifs, client_options), vm);
5576
 
  }
5577
 
 
5578
 
  po::notify(vm);
5579
 
 
5580
5609
  /* Init expected errors */
5581
5610
  memset(&saved_expected_errors, 0, sizeof(saved_expected_errors));
5582
5611
 
5600
5629
  cur_block->ok= true; /* Outer block should always be executed */
5601
5630
  cur_block->cmd= cmd_none;
5602
5631
 
 
5632
  if (hash_init(&var_hash, charset_info,
 
5633
                1024, 0, 0, get_var_key, var_free, MYF(0)))
 
5634
    die("Variable hash initialization failed");
 
5635
 
5603
5636
  var_set_string("$DRIZZLE_SERVER_VERSION", drizzle_version());
5604
5637
 
5605
5638
  memset(&master_pos, 0, sizeof(master_pos));
5613
5646
  ds_progress.reserve(2048);
5614
5647
  ds_warning_messages.reserve(2048);
5615
5648
 
5616
 
 
5617
 
  if (vm.count("record"))
5618
 
  {
5619
 
    record = 1;
5620
 
  }
5621
 
 
5622
 
  if (vm.count("test-file"))
5623
 
  {
5624
 
    string tmp= vm["test-file"].as<string>();
5625
 
    char buff[FN_REFLEN];
5626
 
    if (!internal::test_if_hard_path(tmp.c_str()))
5627
 
    {
5628
 
      snprintf(buff, sizeof(buff), "%s%s",opt_basedir.c_str(),tmp.c_str());
5629
 
      tmp= buff;
5630
 
    }
5631
 
    internal::fn_format(buff, tmp.c_str(), "", "", MY_UNPACK_FILENAME);
5632
 
    assert(cur_file == file_stack && cur_file->file == 0);
5633
 
    if (!(cur_file->file= fopen(buff, "r")))
5634
 
    {
5635
 
      fprintf(stderr, _("Could not open '%s' for reading: errno = %d"), buff, errno);
5636
 
      return EXIT_ARGUMENT_INVALID;
5637
 
    }
5638
 
    if (!(cur_file->file_name= strdup(buff)))
5639
 
    {
5640
 
      fprintf(stderr, _("Out of memory"));
5641
 
      return EXIT_OUT_OF_MEMORY;
5642
 
    }
5643
 
    cur_file->lineno= 1;
5644
 
  }
5645
 
 
5646
 
  if (vm.count("timer-file"))
5647
 
  {
5648
 
    string tmp= vm["timer-file"].as<string>().c_str();
5649
 
    static char buff[FN_REFLEN];
5650
 
    if (!internal::test_if_hard_path(tmp.c_str()))
5651
 
    {
5652
 
      snprintf(buff, sizeof(buff), "%s%s",opt_basedir.c_str(),tmp.c_str());
5653
 
      tmp= buff;
5654
 
    }
5655
 
    internal::fn_format(buff, tmp.c_str(), "", "", MY_UNPACK_FILENAME);
5656
 
    timer_file= buff;
5657
 
    unlink(timer_file);       /* Ignore error, may not exist */
5658
 
  }
5659
 
 
5660
 
  if (vm.count("protocol"))
5661
 
  {
5662
 
    std::transform(opt_protocol.begin(), opt_protocol.end(),
5663
 
      opt_protocol.begin(), ::tolower);
5664
 
 
5665
 
    if (not opt_protocol.compare("mysql"))
5666
 
      use_drizzle_protocol=false;
5667
 
    else if (not opt_protocol.compare("drizzle"))
5668
 
      use_drizzle_protocol=true;
5669
 
    else
5670
 
    {
5671
 
      cout << _("Error: Unknown protocol") << " '" << opt_protocol << "'" << endl;
5672
 
      exit(-1);
5673
 
    }
5674
 
  }
5675
 
 
5676
 
  if (vm.count("port"))
5677
 
  {
5678
 
    /* If the port number is > 65535 it is not a valid port
5679
 
       This also helps with potential data loss casting unsigned long to a
5680
 
       uint32_t. */
5681
 
    if (opt_port > 65535)
5682
 
    {
5683
 
      fprintf(stderr, _("Value supplied for port is not valid.\n"));
5684
 
      exit(EXIT_ARGUMENT_INVALID);
5685
 
    }
5686
 
  }
5687
 
 
5688
 
  if( vm.count("password") )
5689
 
  {
5690
 
    if (!opt_password.empty())
5691
 
      opt_password.erase();
5692
 
    if (password == PASSWORD_SENTINEL)
5693
 
    {
5694
 
      opt_password= "";
5695
 
    }
5696
 
    else
5697
 
    {
5698
 
      opt_password= password;
5699
 
      tty_password= false;
5700
 
    }
5701
 
  }
5702
 
  else
5703
 
  {
5704
 
      tty_password= true;
5705
 
  }
5706
 
 
5707
 
  if (vm.count("tmpdir"))
5708
 
  {
5709
 
    strncpy(TMPDIR, vm["tmpdir"].as<string>().c_str(), sizeof(TMPDIR));
5710
 
  }
5711
 
 
5712
 
  if (vm.count("version"))
5713
 
  {
5714
 
    printf("%s  Ver %s Distrib %s, for %s-%s (%s)\n",internal::my_progname,MTEST_VERSION,
5715
 
    drizzle_version(),HOST_VENDOR,HOST_OS,HOST_CPU);
5716
 
    exit(0);
5717
 
  }
5718
 
  
5719
 
  if (vm.count("help"))
5720
 
  {
5721
 
    printf("%s  Ver %s Distrib %s, for %s-%s (%s)\n",internal::my_progname,MTEST_VERSION,
5722
 
    drizzle_version(),HOST_VENDOR,HOST_OS,HOST_CPU);
5723
 
    printf("MySQL AB, by Sasha, Matt, Monty & Jani\n");
5724
 
    printf("Drizzle version modified by Brian, Jay, Monty Taylor, PatG and Stewart\n");
5725
 
    printf("This software comes with ABSOLUTELY NO WARRANTY\n\n");
5726
 
    printf("Runs a test against the DRIZZLE server and compares output with a results file.\n\n");
5727
 
    printf("Usage: %s [OPTIONS] [database] < test_file\n", internal::my_progname);
5728
 
    exit(0);
5729
 
  }
5730
 
 
5731
 
  if (tty_password)
5732
 
  {
5733
 
    opt_pass= client_get_tty_password(NULL);          /* purify tested */
5734
 
  }
 
5649
  parse_args(argc, argv);
5735
5650
 
5736
5651
  server_initialized= 1;
5737
5652
  if (cur_file == file_stack && cur_file->file == 0)
5747
5662
    die("Failed in drizzle_create()");
5748
5663
  if (!( drizzle_con_create(cur_con->drizzle, &cur_con->con)))
5749
5664
    die("Failed in drizzle_con_create()");
5750
 
  drizzle_con_add_options(&cur_con->con, use_drizzle_protocol ? DRIZZLE_CON_EXPERIMENTAL : DRIZZLE_CON_MYSQL);
5751
5665
 
5752
5666
  if (!(cur_con->name = strdup("default")))
5753
5667
    die("Out of memory");
 
5668
 
5754
5669
  safe_connect(&cur_con->con, cur_con->name, opt_host, opt_user, opt_pass,
5755
5670
               opt_db, opt_port);
5756
5671
 
5757
 
  fill_global_error_names();
5758
 
 
5759
5672
  /* Use all time until exit if no explicit 'start_timer' */
5760
5673
  timer_start= timer_now();
5761
5674
 
5769
5682
  /* Update $drizzleclient_get_server_version to that of current connection */
5770
5683
  var_set_drizzleclient_get_server_version(&cur_con->con);
5771
5684
 
5772
 
  if (! opt_include.empty())
 
5685
  if (opt_include)
5773
5686
  {
5774
 
    open_file(opt_include.c_str());
 
5687
    open_file(opt_include);
5775
5688
  }
5776
5689
 
5777
5690
  while (!read_command(&command) && !abort_flag)
5885
5798
        /* Check for special property for this query */
5886
5799
        display_result_vertically|= (command->type == Q_QUERY_VERTICAL);
5887
5800
 
5888
 
        if (! save_file.empty())
 
5801
        if (save_file[0])
5889
5802
        {
5890
 
          command->require_file= save_file;
5891
 
          save_file.clear();
 
5803
          strncpy(command->require_file, save_file, sizeof(save_file) - 1);
 
5804
          save_file[0]= 0;
5892
5805
        }
5893
5806
        run_query(cur_con, command, flags);
5894
5807
        command_executed++;
5925
5838
        command->last_argument= command->end;
5926
5839
        break;
5927
5840
      case Q_REQUIRE:
5928
 
        do_get_file_name(command, save_file);
 
5841
        do_get_file_name(command, save_file, sizeof(save_file));
5929
5842
        break;
5930
5843
      case Q_ERROR:
5931
5844
        do_get_errcodes(command);
6075
5988
  */
6076
5989
  if (ds_res.length())
6077
5990
  {
6078
 
    if (! result_file_name.empty())
 
5991
    if (result_file_name)
6079
5992
    {
6080
5993
      /* A result file has been specified */
6081
5994
 
6082
5995
      if (record)
6083
5996
      {
6084
5997
        /* Recording - dump the output from test to result file */
6085
 
        str_to_file(result_file_name.c_str(), ds_res.c_str(), ds_res.length());
 
5998
        str_to_file(result_file_name, ds_res.c_str(), ds_res.length());
6086
5999
      }
6087
6000
      else
6088
6001
      {
6105
6018
  }
6106
6019
 
6107
6020
  if (!command_executed &&
6108
 
      ! result_file_name.empty() && !stat(result_file_name.c_str(), &res_info))
 
6021
      result_file_name && !stat(result_file_name, &res_info))
6109
6022
  {
6110
6023
    /*
6111
6024
      my_stat() successful on result file. Check if we have not run a
6117
6030
    die("No queries executed but result file found!");
6118
6031
  }
6119
6032
 
6120
 
  if ( opt_mark_progress && ! result_file_name.empty() )
 
6033
  if ( opt_mark_progress && result_file_name )
6121
6034
    dump_progress();
6122
6035
 
6123
6036
  /* Dump warning messages */
6124
 
  if (! result_file_name.empty() && ds_warning_messages.length())
 
6037
  if (result_file_name && ds_warning_messages.length())
6125
6038
    dump_warning_messages();
6126
6039
 
6127
6040
  timer_output();
6128
6041
  /* Yes, if we got this far the test has suceeded! Sakila smiles */
6129
6042
  cleanup_and_exit(0);
6130
 
}
6131
 
 
6132
 
  catch(exception &err)
6133
 
  {
6134
 
    cerr<<err.what()<<endl;
6135
 
  }
6136
 
 
6137
6043
  return 0; /* Keep compiler happy too */
6138
6044
}
6139
6045
 
6175
6081
 
6176
6082
uint64_t timer_now(void)
6177
6083
{
6178
 
#if defined(HAVE_GETHRTIME)
6179
 
  return gethrtime()/1000/1000;
6180
 
#else
6181
 
  uint64_t newtime;
6182
 
  struct timeval t;
6183
 
  /*
6184
 
    The following loop is here because gettimeofday may fail on some systems
6185
 
  */
6186
 
  while (gettimeofday(&t, NULL) != 0)
6187
 
  {}
6188
 
  newtime= (uint64_t)t.tv_sec * 1000000 + t.tv_usec;
6189
 
  return newtime/1000;
6190
 
#endif  /* defined(HAVE_GETHRTIME) */
 
6084
  return my_micro_time() / 1000;
6191
6085
}
6192
6086
 
6193
6087
 
6402
6296
  char* pattern; /* Pattern to be replaced */
6403
6297
  char* replace; /* String or expression to replace the pattern with */
6404
6298
  int icase; /* true if the match is case insensitive */
6405
 
  int global; /* true if the match should be global -- 
6406
 
                 i.e. repeat the matching until the end of the string */
6407
6299
};
6408
6300
 
6409
6301
struct st_replace_regex
6427
6319
struct st_replace_regex *glob_replace_regex= 0;
6428
6320
 
6429
6321
int reg_replace(char** buf_p, int* buf_len_p, char *pattern, char *replace,
6430
 
                char *string, int icase, int global);
 
6322
                char *string, int icase);
6431
6323
 
6432
6324
 
6433
6325
 
6528
6420
 
6529
6421
    /* Check if we should do matching case insensitive */
6530
6422
    if (p < expr_end && *p == 'i')
6531
 
    {
6532
 
      p++;
6533
6423
      reg.icase= 1;
6534
 
    }
6535
 
 
6536
 
    /* Check if we should do matching globally */
6537
 
    if (p < expr_end && *p == 'g')
6538
 
    {
6539
 
      p++;
6540
 
      reg.global= 1;
6541
 
    }
6542
6424
 
6543
6425
    /* done parsing the statement, now place it in regex_arr */
6544
6426
    if (insert_dynamic(&res->regex_arr,(unsigned char*) &reg))
6596
6478
    get_dynamic(&r->regex_arr,(unsigned char*)&re,i);
6597
6479
 
6598
6480
    if (!reg_replace(&out_buf, buf_len_p, re.pattern, re.replace,
6599
 
                     in_buf, re.icase, re.global))
 
6481
                     in_buf, re.icase))
6600
6482
    {
6601
6483
      /* if the buffer has been reallocated, make adjustements */
6602
6484
      if (save_out_buf != out_buf)
6666
6548
  icase - flag, if set to 1 the match is case insensitive
6667
6549
*/
6668
6550
int reg_replace(char** buf_p, int* buf_len_p, char *pattern,
6669
 
                char *replace, char *in_string, int icase, int global)
 
6551
                char *replace, char *in_string, int icase)
6670
6552
{
 
6553
  string string_to_match(in_string);
6671
6554
  const char *error= NULL;
6672
6555
  int erroffset;
6673
6556
  int ovector[3];
6674
6557
  pcre *re= pcre_compile(pattern,
6675
 
                         icase ? PCRE_CASELESS | PCRE_MULTILINE : PCRE_MULTILINE,
 
6558
                         icase ? PCRE_CASELESS : 0,
6676
6559
                         &error, &erroffset, NULL);
6677
6560
  if (re == NULL)
6678
6561
    return 1;
6679
6562
 
6680
 
  if (! global)
6681
 
  {
6682
 
 
6683
 
    int rc= pcre_exec(re, NULL, in_string, (int)strlen(in_string),
6684
 
                      0, 0, ovector, 3);
6685
 
    if (rc < 0)
6686
 
    {
6687
 
      pcre_free(re);
6688
 
      return 1;
6689
 
    }
6690
 
 
6691
 
    char *substring_to_replace= in_string + ovector[0];
6692
 
    int substring_length= ovector[1] - ovector[0];
6693
 
    *buf_len_p= strlen(in_string) - substring_length + strlen(replace);
6694
 
    char * new_buf = (char *)malloc(*buf_len_p+1);
6695
 
    if (new_buf == NULL)
6696
 
    {
6697
 
      pcre_free(re);
6698
 
      return 1;
6699
 
    }
6700
 
 
6701
 
    memset(new_buf, 0, *buf_len_p+1);
6702
 
    strncpy(new_buf, in_string, substring_to_replace-in_string);
6703
 
    strncpy(new_buf+(substring_to_replace-in_string), replace, strlen(replace));
6704
 
    strncpy(new_buf+(substring_to_replace-in_string)+strlen(replace),
6705
 
            substring_to_replace + substring_length,
6706
 
            strlen(in_string)
6707
 
              - substring_length
6708
 
              - (substring_to_replace-in_string));
6709
 
    *buf_p= new_buf;
6710
 
 
6711
 
    pcre_free(re);
6712
 
    return 0;
6713
 
  }
6714
 
  else
6715
 
  {
6716
 
    /* Repeatedly replace the string with the matched regex */
6717
 
    string subject(in_string);
6718
 
    size_t replace_length= strlen(replace);
6719
 
    size_t length_of_replacement= strlen(replace);
6720
 
    size_t current_position= 0;
6721
 
    int rc= 0;
6722
 
 
6723
 
    while (true) 
6724
 
    {
6725
 
      rc= pcre_exec(re, NULL, subject.c_str(), subject.length(), 
6726
 
                    current_position, 0, ovector, 3);
6727
 
      if (rc < 0)
6728
 
      {
6729
 
        break;
6730
 
      }
6731
 
 
6732
 
      current_position= static_cast<size_t>(ovector[0]);
6733
 
      replace_length= static_cast<size_t>(ovector[1] - ovector[0]);
6734
 
      subject.replace(current_position, replace_length, replace, length_of_replacement);
6735
 
      current_position= current_position + length_of_replacement;
6736
 
    }
6737
 
 
6738
 
    char *new_buf = (char *) malloc(subject.length() + 1);
6739
 
    if (new_buf == NULL)
6740
 
    {
6741
 
      pcre_free(re);
6742
 
      return 1;
6743
 
    }
6744
 
    memset(new_buf, 0, subject.length() + 1);
6745
 
    strncpy(new_buf, subject.c_str(), subject.length());
6746
 
    *buf_len_p= subject.length() + 1;
6747
 
    *buf_p= new_buf;
6748
 
          
6749
 
    pcre_free(re);
6750
 
    return 0;
6751
 
  }
 
6563
  int rc= pcre_exec(re, NULL, in_string, (int)strlen(in_string),
 
6564
                    0, 0, ovector, 3);
 
6565
  if (rc < 0)
 
6566
  {
 
6567
    pcre_free(re);
 
6568
    return 1;
 
6569
  }
 
6570
 
 
6571
  char *substring_to_replace= in_string + ovector[0];
 
6572
  int substring_length= ovector[1] - ovector[0];
 
6573
  *buf_len_p= strlen(in_string) - substring_length + strlen(replace);
 
6574
  char * new_buf = (char *)malloc(*buf_len_p+1);
 
6575
  if (new_buf == NULL)
 
6576
  {
 
6577
    pcre_free(re);
 
6578
    return 1;
 
6579
  }
 
6580
 
 
6581
  memset(new_buf, 0, *buf_len_p+1);
 
6582
  strncpy(new_buf, in_string, substring_to_replace-in_string);
 
6583
  strncpy(new_buf+(substring_to_replace-in_string), replace, strlen(replace));
 
6584
  strncpy(new_buf+(substring_to_replace-in_string)+strlen(replace),
 
6585
          substring_to_replace + substring_length,
 
6586
          strlen(in_string)
 
6587
            - substring_length
 
6588
            - (substring_to_replace-in_string));
 
6589
  *buf_p= new_buf;
 
6590
 
 
6591
  pcre_free(re);
 
6592
  return 0;
6752
6593
}
6753
6594
 
6754
6595
 
7332
7173
      return(1);
7333
7174
    if (new_pos != pa->str)
7334
7175
    {
7335
 
      ptrdiff_t diff= PTR_BYTE_DIFF(new_pos,pa->str);
 
7176
      my_ptrdiff_t diff=PTR_BYTE_DIFF(new_pos,pa->str);
7336
7177
      for (i=0 ; i < pa->typelib.count ; i++)
7337
7178
        pa->typelib.type_names[i]= ADD_TO_PTR(pa->typelib.type_names[i],diff,
7338
7179
                                              char*);