~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to client/drizzletest.cc

  • Committer: Monty Taylor
  • Date: 2009-09-22 23:50:12 UTC
  • mto: This revision was merged to the branch mainline in revision 1184.
  • Revision ID: mordred@inaugust.com-20090922235012-i0a3bs91f6krqduc
Fixed multi_malloc.h include guard.
Added include guard checking script.

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
49
#include <algorithm>
52
50
#ifdef HAVE_SYS_WAIT_H
53
51
#include <sys/wait.h>
54
52
#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
53
 
61
54
#include PCRE_HEADER
62
55
 
 
56
#include <mysys/hash.h>
63
57
#include <stdarg.h>
64
 
#include <boost/unordered_map.hpp>
 
58
 
 
59
#include "errname.h"
65
60
 
66
61
/* 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>
 
62
#include <drizzled/gettext.h>
71
63
 
72
64
#ifndef DRIZZLE_RETURN_SERVER_GONE
73
65
#define DRIZZLE_RETURN_HANDSHAKE_FAILED DRIZZLE_RETURN_ERROR_CODE
74
66
#endif
75
 
namespace po= boost::program_options;
 
67
 
76
68
using namespace std;
77
 
using namespace drizzled;
78
69
 
79
70
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);
 
71
{
 
72
  unsigned char *get_var_key(const unsigned char* var, size_t *len, bool);
 
73
  bool get_one_option(int optid, const struct my_option *, char *argument);
 
74
}
83
75
 
84
76
#define MAX_VAR_NAME_LENGTH    256
85
77
#define MAX_COLUMNS            256
 
78
#define MAX_EMBEDDED_SERVER_ARGS 64
86
79
#define MAX_DELIMITER_LENGTH 16
87
80
/* Flags controlling send and reap */
88
81
#define QUERY_SEND_FLAG  1
89
82
#define QUERY_REAP_FLAG  2
90
83
 
91
 
typedef boost::unordered_map<std::string, uint32_t> ErrorCodes;
92
84
ErrorCodes global_error_names;
93
85
 
94
86
enum {
98
90
};
99
91
 
100
92
static int record= 0, opt_sleep= -1;
101
 
static char *opt_pass= NULL;
102
 
const char *unix_sock= NULL;
 
93
static char *opt_db= NULL, *opt_pass= NULL;
 
94
const char *opt_user= NULL, *opt_host= NULL, *unix_sock= NULL,
 
95
           *opt_basedir= "./";
 
96
const char *opt_logdir= "";
 
97
const char *opt_include= NULL, *opt_charsets_dir;
 
98
const char *opt_testdir= NULL;
103
99
static uint32_t opt_port= 0;
104
 
static uint32_t opt_max_connect_retries;
105
 
static bool silent= false, verbose= false;
 
100
static int opt_max_connect_retries;
 
101
static bool opt_compress= false, silent= false, verbose= false;
 
102
static bool debug_info_flag= false, debug_check_flag= false;
 
103
static bool tty_password= false;
106
104
static bool opt_mark_progress= false;
107
105
static bool parsing_disabled= false;
108
106
static bool display_result_vertically= false,
113
111
static bool abort_on_error= true;
114
112
static bool server_initialized= false;
115
113
static bool is_windows= false;
116
 
static bool use_drizzle_protocol= false;
 
114
static char **default_argv;
 
115
static const char *load_default_groups[]= { "drizzletest", "client", 0 };
117
116
static char line_buffer[MAX_DELIMITER_LENGTH], *line_buffer_pos= line_buffer;
118
117
 
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
118
static uint32_t start_lineno= 0; /* Start line of current command */
 
119
static uint32_t my_end_arg= 0;
133
120
 
134
121
/* Number of lines of the result to include in failure report */
135
122
static uint32_t opt_tail_lines= 0;
171
158
 
172
159
static const CHARSET_INFO *charset_info= &my_charset_utf8_general_ci; /* Default charset */
173
160
 
 
161
static int embedded_server_arg_count=0;
 
162
static char *embedded_server_args[MAX_EMBEDDED_SERVER_ARGS];
 
163
 
174
164
/*
175
165
  Timer related variables
176
166
  See the timer_output() definition for details
198
188
master_pos_st master_pos;
199
189
 
200
190
/* if set, all results are concated and compared against this file */
 
191
const char *result_file_name= NULL;
201
192
 
202
193
typedef struct st_var
203
194
{
215
206
/*Perl/shell-like variable registers */
216
207
VAR var_reg[10];
217
208
 
218
 
 
219
 
boost::unordered_map<string, VAR *> var_hash;
 
209
HASH var_hash;
220
210
 
221
211
struct st_connection
222
212
{
396
386
  char *query, *query_buf,*first_argument,*last_argument,*end;
397
387
  int first_word_len, query_len;
398
388
  bool abort_on_error;
399
 
  st_expected_errors expected_errors;
400
 
  string require_file;
 
389
  struct st_expected_errors expected_errors;
 
390
  char require_file[FN_REFLEN];
401
391
  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
392
};
419
393
 
420
394
TYPELIB command_typelib= {array_elements(command_names),"",
438
412
VAR* var_from_env(const char *, const char *);
439
413
VAR* var_init(VAR* v, const char *name, int name_len, const char *val,
440
414
              int val_len);
441
 
void var_free(pair<string, VAR*> v);
 
415
extern "C" void var_free(void* v);
442
416
VAR* var_get(const char *var_name, const char** var_name_end,
443
417
             bool raw, bool ignore_not_existing);
444
418
void eval_expr(VAR* v, const char *p, const char** p_end);
782
756
    const struct command_arg *arg= &args[i];
783
757
    arg->ds->clear();
784
758
 
785
 
    bool known_arg_type= true;
786
759
    switch (arg->type) {
787
760
      /* A string */
788
761
    case ARG_STRING:
817
790
      break;
818
791
 
819
792
    default:
820
 
      known_arg_type= false;
 
793
      assert("Unknown argument type");
821
794
      break;
822
795
    }
823
 
    assert(known_arg_type);
824
796
 
825
797
    /* Check required arg */
826
798
    if (arg->ds->length() == 0 && arg->required)
912
884
 
913
885
  close_connections();
914
886
  close_files();
915
 
  for_each(var_hash.begin(), var_hash.end(), var_free);
916
 
  var_hash.clear();
 
887
  hash_free(&var_hash);
917
888
 
918
 
  vector<st_command *>::iterator iter;
 
889
  vector<struct st_command *>::iterator iter;
919
890
  for (iter= q_lines.begin() ; iter < q_lines.end() ; iter++)
920
891
  {
921
892
    struct st_command * q_line= *iter;
922
 
    delete q_line;
 
893
    if (q_line->query_buf != NULL)
 
894
    {
 
895
      free(q_line->query_buf);
 
896
    }
 
897
    free(q_line);
923
898
  }
924
899
 
925
900
  for (i= 0; i < 10; i++)
927
902
    if (var_reg[i].alloced_len)
928
903
      free(var_reg[i].str_val);
929
904
  }
 
905
  while (embedded_server_arg_count > 1)
 
906
    free(embedded_server_args[--embedded_server_arg_count]);
930
907
 
931
908
  free_all_replace();
932
909
  free(opt_pass);
 
910
  free_defaults(default_argv);
933
911
 
934
912
  return;
935
913
}
938
916
static void cleanup_and_exit(int exit_code)
939
917
{
940
918
  free_used_memory();
941
 
  internal::my_end();
 
919
  my_end(my_end_arg);
942
920
 
943
921
  if (!silent) {
944
922
    switch (exit_code) {
1011
989
  }
1012
990
 
1013
991
  /* Dump the result that has been accumulated so far to .log file */
1014
 
  if (! result_file_name.empty() && ds_res.length())
 
992
  if (result_file_name && ds_res.length())
1015
993
    dump_result_to_log_file(ds_res.c_str(), ds_res.length());
1016
994
 
1017
995
  /* Dump warning messages */
1018
 
  if (! result_file_name.empty() && ds_warning_messages.length())
 
996
  if (result_file_name && ds_warning_messages.length())
1019
997
    dump_warning_messages();
1020
998
 
1021
999
  /*
1151
1129
  uint32_t len;
1152
1130
  char buff[512];
1153
1131
 
1154
 
  if ((fd= internal::my_open(filename, O_RDONLY, MYF(0))) < 0)
 
1132
  if ((fd= my_open(filename, O_RDONLY, MYF(0))) < 0)
1155
1133
    die("Failed to open file '%s'", filename);
1156
 
  while((len= internal::my_read(fd, (unsigned char*)&buff,
 
1134
  while((len= my_read(fd, (unsigned char*)&buff,
1157
1135
                      sizeof(buff), MYF(0))) > 0)
1158
1136
  {
1159
1137
    char *p= buff, *start= buff;
1175
1153
    /* Output any chars that might be left */
1176
1154
    ds->append(start, p-start);
1177
1155
  }
1178
 
  internal::my_close(fd, MYF(0));
 
1156
  my_close(fd, MYF(0));
1179
1157
}
1180
1158
 
1181
1159
 
1351
1329
 
1352
1330
*/
1353
1331
 
1354
 
static int compare_files2(int fd, const char* filename2)
 
1332
static int compare_files2(File fd, const char* filename2)
1355
1333
{
1356
1334
  int error= RESULT_OK;
1357
 
  int fd2;
 
1335
  File fd2;
1358
1336
  uint32_t len, len2;
1359
1337
  char buff[512], buff2[512];
1360
1338
  const char *fname= filename2;
1361
1339
  string tmpfile;
1362
1340
 
1363
 
  if ((fd2= internal::my_open(fname, O_RDONLY, MYF(0))) < 0)
 
1341
  if ((fd2= my_open(fname, O_RDONLY, MYF(0))) < 0)
1364
1342
  {
1365
 
    internal::my_close(fd, MYF(0));
1366
 
    if (! opt_testdir.empty())
 
1343
    my_close(fd, MYF(0));
 
1344
    if (opt_testdir != NULL)
1367
1345
    {
1368
1346
      tmpfile= opt_testdir;
1369
1347
      if (tmpfile[tmpfile.length()] != '/')
1371
1349
      tmpfile.append(filename2);
1372
1350
      fname= tmpfile.c_str();
1373
1351
    }
1374
 
    if ((fd2= internal::my_open(fname, O_RDONLY, MYF(0))) < 0)
 
1352
    if ((fd2= my_open(fname, O_RDONLY, MYF(0))) < 0)
1375
1353
    {
1376
 
      internal::my_close(fd, MYF(0));
 
1354
      my_close(fd, MYF(0));
1377
1355
    
1378
1356
      die("Failed to open second file: '%s'", fname);
1379
1357
    }
1380
1358
  }
1381
 
  while((len= internal::my_read(fd, (unsigned char*)&buff,
 
1359
  while((len= my_read(fd, (unsigned char*)&buff,
1382
1360
                      sizeof(buff), MYF(0))) > 0)
1383
1361
  {
1384
 
    if ((len2= internal::my_read(fd2, (unsigned char*)&buff2,
 
1362
    if ((len2= my_read(fd2, (unsigned char*)&buff2,
1385
1363
                       sizeof(buff2), MYF(0))) < len)
1386
1364
    {
1387
1365
      /* File 2 was smaller */
1401
1379
      break;
1402
1380
    }
1403
1381
  }
1404
 
  if (!error && internal::my_read(fd2, (unsigned char*)&buff2,
 
1382
  if (!error && my_read(fd2, (unsigned char*)&buff2,
1405
1383
                        sizeof(buff2), MYF(0)) > 0)
1406
1384
  {
1407
1385
    /* File 1 was smaller */
1408
1386
    error= RESULT_LENGTH_MISMATCH;
1409
1387
  }
1410
1388
 
1411
 
  internal::my_close(fd2, MYF(0));
 
1389
  my_close(fd2, MYF(0));
1412
1390
 
1413
1391
  return error;
1414
1392
}
1429
1407
 
1430
1408
static int compare_files(const char* filename1, const char* filename2)
1431
1409
{
1432
 
  int fd;
 
1410
  File fd;
1433
1411
  int error;
1434
1412
 
1435
 
  if ((fd= internal::my_open(filename1, O_RDONLY, MYF(0))) < 0)
 
1413
  if ((fd= my_open(filename1, O_RDONLY, MYF(0))) < 0)
1436
1414
    die("Failed to open first file: '%s'", filename1);
1437
1415
 
1438
1416
  error= compare_files2(fd, filename2);
1439
1417
 
1440
 
  internal::my_close(fd, MYF(0));
 
1418
  my_close(fd, MYF(0));
1441
1419
 
1442
1420
  return error;
1443
1421
}
1458
1436
static int string_cmp(string* ds, const char *fname)
1459
1437
{
1460
1438
  int error;
1461
 
  int fd;
 
1439
  File fd;
1462
1440
  char temp_file_path[FN_REFLEN];
1463
1441
 
1464
 
  if ((fd= internal::create_temp_file(temp_file_path, TMPDIR,
 
1442
  if ((fd= create_temp_file(temp_file_path, NULL,
1465
1443
                            "tmp", MYF(MY_WME))) < 0)
1466
1444
    die("Failed to create temporary file for ds");
1467
1445
 
1468
1446
  /* Write ds to temporary file and set file pos to beginning*/
1469
 
  if (internal::my_write(fd, (unsigned char *) ds->c_str(), ds->length(),
 
1447
  if (my_write(fd, (unsigned char *) ds->c_str(), ds->length(),
1470
1448
               MYF(MY_FNABP | MY_WME)) ||
1471
1449
      lseek(fd, 0, SEEK_SET) == MY_FILEPOS_ERROR)
1472
1450
  {
1473
 
    internal::my_close(fd, MYF(0));
 
1451
    my_close(fd, MYF(0));
1474
1452
    /* Remove the temporary file */
1475
 
    internal::my_delete(temp_file_path, MYF(0));
 
1453
    my_delete(temp_file_path, MYF(0));
1476
1454
    die("Failed to write file '%s'", temp_file_path);
1477
1455
  }
1478
1456
 
1479
1457
  error= compare_files2(fd, fname);
1480
1458
 
1481
 
  internal::my_close(fd, MYF(0));
 
1459
  my_close(fd, MYF(0));
1482
1460
  /* Remove the temporary file */
1483
 
  internal::my_delete(temp_file_path, MYF(0));
 
1461
  my_delete(temp_file_path, MYF(0));
1484
1462
 
1485
1463
  return(error);
1486
1464
}
1503
1481
  const char* mess= "Result content mismatch\n";
1504
1482
 
1505
1483
 
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())) {
 
1484
  assert(result_file_name);
 
1485
 
 
1486
  if (access(result_file_name, F_OK) != 0)
 
1487
    die("The specified result file does not exist: '%s'", result_file_name);
 
1488
 
 
1489
  switch (string_cmp(ds, result_file_name)) {
1512
1490
  case RESULT_OK:
1513
1491
    break; /* ok */
1514
1492
  case RESULT_LENGTH_MISMATCH:
1522
1500
    */
1523
1501
    char reject_file[FN_REFLEN];
1524
1502
    size_t reject_length;
1525
 
    internal::dirname_part(reject_file, result_file_name.c_str(), &reject_length);
 
1503
    dirname_part(reject_file, result_file_name, &reject_length);
1526
1504
 
1527
1505
    if (access(reject_file, W_OK) == 0)
1528
1506
    {
1529
1507
      /* Result file directory is writable, save reject file there */
1530
 
      internal::fn_format(reject_file, result_file_name.c_str(), NULL,
 
1508
      fn_format(reject_file, result_file_name, NULL,
1531
1509
                ".reject", MY_REPLACE_EXT);
1532
1510
    }
1533
1511
    else
1534
1512
    {
1535
1513
      /* Put reject file in opt_logdir */
1536
 
      internal::fn_format(reject_file, result_file_name.c_str(), opt_logdir.c_str(),
 
1514
      fn_format(reject_file, result_file_name, opt_logdir,
1537
1515
                ".reject", MY_REPLACE_DIR | MY_REPLACE_EXT);
1538
1516
    }
1539
1517
    str_to_file(reject_file, ds->c_str(), ds->length());
1540
1518
 
1541
1519
    ds->erase(); /* Don't create a .log file */
1542
1520
 
1543
 
    show_diff(NULL, result_file_name.c_str(), reject_file);
 
1521
    show_diff(NULL, result_file_name, reject_file);
1544
1522
    die("%s",mess);
1545
1523
    break;
1546
1524
  }
1567
1545
 
1568
1546
*/
1569
1547
 
1570
 
static void check_require(string* ds, const string &fname)
 
1548
static void check_require(string* ds, const char *fname)
1571
1549
{
1572
1550
 
1573
1551
 
1574
 
  if (string_cmp(ds, fname.c_str()))
 
1552
  if (string_cmp(ds, fname))
1575
1553
  {
1576
1554
    char reason[FN_REFLEN];
1577
 
    internal::fn_format(reason, fname.c_str(), "", "", MY_REPLACE_EXT | MY_REPLACE_DIR);
 
1555
    fn_format(reason, fname, "", "", MY_REPLACE_EXT | MY_REPLACE_DIR);
1578
1556
    abort_not_supported_test("Test requires: '%s'", reason);
1579
1557
  }
1580
1558
  return;
1625
1603
}
1626
1604
 
1627
1605
 
 
1606
unsigned char *get_var_key(const unsigned char* var, size_t *len, bool)
 
1607
{
 
1608
  register char* key;
 
1609
  key = ((VAR*)var)->name;
 
1610
  *len = ((VAR*)var)->name_len;
 
1611
  return (unsigned char*)key;
 
1612
}
 
1613
 
1628
1614
 
1629
1615
VAR *var_init(VAR *v, const char *name, int name_len, const char *val,
1630
1616
              int val_len)
1662
1648
}
1663
1649
 
1664
1650
 
1665
 
void var_free(pair<string, VAR *> v)
 
1651
void var_free(void *v)
1666
1652
{
1667
 
  free(v.second->str_val);
1668
 
  free(v.second->env_s);
1669
 
  if (v.second->alloced)
1670
 
    free(v.second);
 
1653
  free(((VAR*) v)->str_val);
 
1654
  free(((VAR*) v)->env_s);
 
1655
  if (((VAR*)v)->alloced)
 
1656
    free(v);
1671
1657
}
1672
1658
 
1673
1659
 
1679
1665
    tmp = def_val;
1680
1666
 
1681
1667
  v = var_init(0, name, strlen(name), tmp, strlen(tmp));
1682
 
  string var_name(name);
1683
 
  var_hash.insert(make_pair(var_name, v));
 
1668
  my_hash_insert(&var_hash, (unsigned char*)v);
1684
1669
  return v;
1685
1670
}
1686
1671
 
1711
1696
    if (length >= MAX_VAR_NAME_LENGTH)
1712
1697
      die("Too long variable name: %s", save_var_name);
1713
1698
 
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())
 
1699
    if (!(v = (VAR*) hash_search(&var_hash, (const unsigned char*) save_var_name,
 
1700
                                 length)))
1718
1701
    {
1719
1702
      char buff[MAX_VAR_NAME_LENGTH+1];
1720
1703
      strncpy(buff, save_var_name, length);
1721
1704
      buff[length]= '\0';
1722
1705
      v= var_from_env(buff, "");
1723
1706
    }
1724
 
    else
1725
 
    {
1726
 
      v= (*iter).second;
1727
 
    }
1728
1707
    var_name--;  /* Point at last character */
1729
1708
  }
1730
1709
  else
1749
1728
 
1750
1729
static VAR *var_obtain(const char *name, int len)
1751
1730
{
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));
 
1731
  VAR* v;
 
1732
  if ((v = (VAR*)hash_search(&var_hash, (const unsigned char *) name, len)))
 
1733
    return v;
 
1734
  v = var_init(0, name, len, "", 0);
 
1735
  my_hash_insert(&var_hash, (unsigned char*)v);
1759
1736
  return v;
1760
1737
}
1761
1738
 
2117
2094
    const size_t len= strlen(get_value_str);
2118
2095
    if (strncmp(p, get_value_str, len)==0)
2119
2096
    {
2120
 
      st_command command;
 
2097
      struct st_command command;
 
2098
      memset(&command, 0, sizeof(command));
2121
2099
      command.query= (char*)p;
2122
2100
      command.first_word_len= len;
2123
2101
      command.first_argument= command.query + len;
2154
2132
{
2155
2133
  char buff[FN_REFLEN];
2156
2134
 
2157
 
  if (!internal::test_if_hard_path(name))
 
2135
  if (!test_if_hard_path(name))
2158
2136
  {
2159
 
    snprintf(buff, sizeof(buff), "%s%s",opt_basedir.c_str(),name);
 
2137
    sprintf(buff,"%s%s",opt_basedir,name);
2160
2138
    name=buff;
2161
2139
  }
2162
 
  internal::fn_format(buff, name, "", "", MY_UNPACK_FILENAME);
 
2140
  fn_format(buff, name, "", "", MY_UNPACK_FILENAME);
2163
2141
 
2164
2142
  if (cur_file == file_stack_end)
2165
2143
    die("Source directives are nesting too deep");
2210
2188
    ; /* Do nothing */
2211
2189
  else
2212
2190
  {
2213
 
    if (! opt_testdir.empty())
 
2191
    if (opt_testdir != NULL)
2214
2192
    {
2215
2193
      string testdir(opt_testdir);
2216
2194
      if (testdir[testdir.length()] != '/')
2478
2456
                     rm_args, sizeof(rm_args)/sizeof(struct command_arg),
2479
2457
                     ' ');
2480
2458
 
2481
 
  error= internal::my_delete(ds_filename.c_str(), MYF(0)) != 0;
 
2459
  error= my_delete(ds_filename.c_str(), MYF(0)) != 0;
2482
2460
  handle_command_error(command, error);
2483
2461
  return;
2484
2462
}
2512
2490
                     sizeof(copy_file_args)/sizeof(struct command_arg),
2513
2491
                     ' ');
2514
2492
 
2515
 
  error= (internal::my_copy(ds_from_file.c_str(), ds_to_file.c_str(),
 
2493
  error= (my_copy(ds_from_file.c_str(), ds_to_file.c_str(),
2516
2494
                  MYF(MY_DONT_OVERWRITE_FILE)) != 0);
2517
2495
  handle_command_error(command, error);
2518
2496
  return;
2610
2588
                     mkdir_args, sizeof(mkdir_args)/sizeof(struct command_arg),
2611
2589
                     ' ');
2612
2590
 
2613
 
  error= mkdir(ds_dirname.c_str(), (0777 & internal::my_umask_dir)) != 0;
 
2591
  error= mkdir(ds_dirname.c_str(), (0777 & my_umask_dir)) != 0;
2614
2592
  handle_command_error(command, error);
2615
2593
  return;
2616
2594
}
2977
2955
static void do_perl(struct st_command *command)
2978
2956
{
2979
2957
  int error;
2980
 
  int fd;
 
2958
  File fd;
2981
2959
  FILE *res_file;
2982
2960
  char buf[FN_REFLEN];
2983
2961
  char temp_file_path[FN_REFLEN];
3001
2979
  read_until_delimiter(&ds_script, &ds_delimiter);
3002
2980
 
3003
2981
  /* Create temporary file name */
3004
 
  if ((fd= internal::create_temp_file(temp_file_path, getenv("MYSQLTEST_VARDIR"),
 
2982
  if ((fd= create_temp_file(temp_file_path, getenv("MYSQLTEST_VARDIR"),
3005
2983
                            "tmp", MYF(MY_WME))) < 0)
3006
2984
    die("Failed to create temporary file for perl command");
3007
 
  internal::my_close(fd, MYF(0));
 
2985
  my_close(fd, MYF(0));
3008
2986
 
3009
2987
  str_to_file(temp_file_path, ds_script.c_str(), ds_script.length());
3010
2988
 
3024
3002
  error= pclose(res_file);
3025
3003
 
3026
3004
  /* Remove the temporary file */
3027
 
  internal::my_delete(temp_file_path, MYF(0));
 
3005
  my_delete(temp_file_path, MYF(0));
3028
3006
 
3029
3007
  handle_command_error(command, WEXITSTATUS(error));
3030
3008
  return;
3130
3108
  if (!master_pos.file[0])
3131
3109
    die("Calling 'sync_with_master' without calling 'save_master_pos'");
3132
3110
 
3133
 
  snprintf(query_buf, sizeof(query_buf), "select master_pos_wait('%s', %ld)", master_pos.file,
 
3111
  sprintf(query_buf, "select master_pos_wait('%s', %ld)", master_pos.file,
3134
3112
          master_pos.pos + offset);
3135
3113
 
3136
3114
wait_for_position:
3317
3295
  bool error= false;
3318
3296
  char *p= command->first_argument;
3319
3297
  char *sleep_start, *sleep_end= command->end;
3320
 
  double sleep_val= 0;
 
3298
  double sleep_val;
3321
3299
 
3322
3300
  while (my_isspace(charset_info, *p))
3323
3301
    p++;
3324
3302
  if (!*p)
3325
3303
    die("Missing argument to %.*s", command->first_word_len, command->query);
3326
3304
  sleep_start= p;
3327
 
  /* Check that arg starts with a digit, not handled by internal::my_strtod */
 
3305
  /* Check that arg starts with a digit, not handled by my_strtod */
3328
3306
  if (!my_isdigit(charset_info, *sleep_start))
3329
3307
    die("Invalid argument to %.*s \"%s\"", command->first_word_len,
3330
3308
        command->query,command->first_argument);
3346
3324
}
3347
3325
 
3348
3326
 
3349
 
static void do_get_file_name(struct st_command *command, string &dest)
 
3327
static void do_get_file_name(struct st_command *command,
 
3328
                             char* dest, uint32_t dest_max_len)
3350
3329
{
3351
3330
  char *p= command->first_argument, *name;
3352
3331
  if (!*p)
3357
3336
  if (*p)
3358
3337
    *p++= 0;
3359
3338
  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);
 
3339
  strncpy(dest, name, dest_max_len - 1);
3367
3340
}
3368
3341
 
3369
3342
 
3386
3359
    abort_not_supported_test("Test requires charset '%s'", charset_name);
3387
3360
}
3388
3361
 
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
3362
static uint32_t get_errcode_from_name(char *error_name, char *error_end)
3449
3363
{
3450
3364
  size_t err_name_len= error_end - error_name;
3451
3365
  string error_name_s(error_name, err_name_len);
3452
3366
 
3453
 
  ErrorCodes::iterator it= global_error_names.find(error_name_s);
3454
 
  if (it != global_error_names.end())
3455
 
  {
3456
 
    return (*it).second;
3457
 
  }
3458
 
 
3459
 
  die("Unknown SQL error name '%s'", error_name_s.c_str());
3460
 
  return 0;
 
3367
  uint32_t code= global_error_names.getErrorCode(error_name_s);
 
3368
 
 
3369
  if (!code)
 
3370
    die("Unknown SQL error name '%s'", error_name_s.c_str());
 
3371
 
 
3372
  return(code);
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;
4563
4455
  return;
4564
4456
}
4565
4457
 
 
4458
 
 
4459
 
4566
4460
/*
4567
4461
  Create a command from a set of lines
4568
4462
 
4580
4474
  terminated by new line '\n' regardless how many "delimiter" it contain.
4581
4475
*/
4582
4476
 
4583
 
#define MAX_QUERY (768*1024*2) /* 256K -- a test in sp-big is >128K */
 
4477
#define MAX_QUERY (256*1024*2) /* 256K -- a test in sp-big is >128K */
4584
4478
static char read_command_buf[MAX_QUERY];
4585
4479
 
4586
4480
static int read_command(struct st_command** command_ptr)
4594
4488
    *command_ptr= q_lines[parser.current_line];
4595
4489
    return(0);
4596
4490
  }
4597
 
  if (!(*command_ptr= command= new st_command))
4598
 
    die("command construction failed");
 
4491
  if (!(*command_ptr= command=
 
4492
        (struct st_command*) malloc(sizeof(*command))))
 
4493
    die("command malloc failed");
 
4494
  memset(command, 0, sizeof(*command));
4599
4495
  q_lines.push_back(command);
4600
4496
  command->type= Q_UNKNOWN;
4601
4497
 
4642
4538
  return(0);
4643
4539
}
4644
4540
 
 
4541
 
 
4542
static struct my_option my_long_options[] =
 
4543
{
 
4544
  {"help", '?', "Display this help and exit.", 0, 0, 0, GET_NO_ARG, NO_ARG,
 
4545
   0, 0, 0, 0, 0, 0},
 
4546
  {"basedir", 'b', "Basedir for tests.", (char**) &opt_basedir,
 
4547
   (char**) &opt_basedir, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
4548
  {"character-sets-dir", OPT_CHARSETS_DIR,
 
4549
   "Directory where character sets are.", (char**) &opt_charsets_dir,
 
4550
   (char**) &opt_charsets_dir, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
4551
  {"compress", 'C', "Use the compressed server/client protocol.",
 
4552
   (char**) &opt_compress, (char**) &opt_compress, 0, GET_BOOL, NO_ARG, 0, 0, 0,
 
4553
   0, 0, 0},
 
4554
  {"database", 'D', "Database to use.", (char**) &opt_db, (char**) &opt_db, 0,
 
4555
   GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
4556
  {"debug-check", OPT_DEBUG_CHECK, "Check memory and open file usage at exit.",
 
4557
   (char**) &debug_check_flag, (char**) &debug_check_flag, 0,
 
4558
   GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
 
4559
  {"debug-info", OPT_DEBUG_INFO, "Print some debug info at exit.",
 
4560
   (char**) &debug_info_flag, (char**) &debug_info_flag,
 
4561
   0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
 
4562
  {"host", 'h', "Connect to host.", (char**) &opt_host, (char**) &opt_host, 0,
 
4563
   GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
4564
  {"include", 'i', "Include SQL before each test case.", (char**) &opt_include,
 
4565
   (char**) &opt_include, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
4566
  {"testdir", OPT_TESTDIR, "Path to use to search for test files",
 
4567
   (char**) &opt_testdir,
 
4568
   (char**) &opt_testdir, 0,GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
4569
  {"logdir", OPT_LOG_DIR, "Directory for log files", (char**) &opt_logdir,
 
4570
   (char**) &opt_logdir, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
4571
  {"mark-progress", OPT_MARK_PROGRESS,
 
4572
   "Write linenumber and elapsed time to <testname>.progress ",
 
4573
   (char**) &opt_mark_progress, (char**) &opt_mark_progress, 0,
 
4574
   GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
 
4575
  {"max-connect-retries", OPT_MAX_CONNECT_RETRIES,
 
4576
   "Max number of connection attempts when connecting to server",
 
4577
   (char**) &opt_max_connect_retries, (char**) &opt_max_connect_retries, 0,
 
4578
   GET_INT, REQUIRED_ARG, 500, 1, 10000, 0, 0, 0},
 
4579
  {"password", 'P', "Password to use when connecting to server.",
 
4580
   0, 0, 0, GET_STR, OPT_ARG, 0, 0, 0, 0, 0, 0},
 
4581
  {"port", 'p', "Port number to use for connection or 0 for default to, in "
 
4582
   "order of preference, drizzle.cnf, $DRIZZLE_TCP_PORT, "
 
4583
   "built-in default (" STRINGIFY_ARG(DRIZZLE_PORT) ").",
 
4584
   0, 0, 0, GET_UINT, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
4585
  {"quiet", 's', "Suppress all normal output.", (char**) &silent,
 
4586
   (char**) &silent, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
 
4587
  {"record", 'r', "Record output of test_file into result file.",
 
4588
   0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
 
4589
  {"result-file", 'R', "Read/Store result from/in this file.",
 
4590
   (char**) &result_file_name, (char**) &result_file_name, 0,
 
4591
   GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
4592
  {"server-arg", 'A', "Send option value to embedded server as a parameter.",
 
4593
   0, 0, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
4594
  {"server-file", 'F', "Read embedded server arguments from file.",
 
4595
   0, 0, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
4596
  {"silent", 's', "Suppress all normal output. Synonym for --quiet.",
 
4597
   (char**) &silent, (char**) &silent, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
 
4598
  {"sleep", 'T', "Sleep always this many seconds on sleep commands.",
 
4599
   (char**) &opt_sleep, (char**) &opt_sleep, 0, GET_INT, REQUIRED_ARG, -1, -1, 0,
 
4600
   0, 0, 0},
 
4601
  {"tail-lines", OPT_TAIL_LINES,
 
4602
   "Number of lines of the resul to include in a failure report",
 
4603
   (char**) &opt_tail_lines, (char**) &opt_tail_lines, 0,
 
4604
   GET_INT, REQUIRED_ARG, 0, 0, 10000, 0, 0, 0},
 
4605
  {"test-file", 'x', "Read test from/in this file (default stdin).",
 
4606
   0, 0, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
4607
  {"timer-file", 'm', "File where the timing in micro seconds is stored.",
 
4608
   0, 0, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
4609
  {"tmpdir", 't', "Temporary directory where sockets are put.",
 
4610
   0, 0, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
4611
  {"user", 'u', "User for login.", (char**) &opt_user, (char**) &opt_user, 0,
 
4612
   GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
4613
  {"verbose", 'v', "Write more.", (char**) &verbose, (char**) &verbose, 0,
 
4614
   GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
 
4615
  {"version", 'V', "Output version information and exit.",
 
4616
   0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
 
4617
  { 0, 0, 0, 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0}
 
4618
};
 
4619
 
 
4620
 
 
4621
static void print_version(void)
 
4622
{
 
4623
  printf("%s  Ver %s Distrib %s, for %s-%s (%s)\n",my_progname,MTEST_VERSION,
 
4624
         drizzle_version(),HOST_VENDOR,HOST_OS,HOST_CPU);
 
4625
}
 
4626
 
 
4627
static void usage(void)
 
4628
{
 
4629
  print_version();
 
4630
  printf("MySQL AB, by Sasha, Matt, Monty & Jani\n");
 
4631
  printf("This software comes with ABSOLUTELY NO WARRANTY\n\n");
 
4632
  printf("Runs a test against the DRIZZLE server and compares output with a results file.\n\n");
 
4633
  printf("Usage: %s [OPTIONS] [database] < test_file\n", my_progname);
 
4634
  my_print_help(my_long_options);
 
4635
  printf("  --no-defaults       Don't read default options from any options file.\n");
 
4636
  my_print_variables(my_long_options);
 
4637
}
 
4638
 
 
4639
/*
 
4640
  Read arguments for embedded server and put them into
 
4641
  embedded_server_args[]
 
4642
*/
 
4643
 
 
4644
static void read_embedded_server_arguments(const char *name)
 
4645
{
 
4646
  char argument[1024],buff[FN_REFLEN], *str=0;
 
4647
  FILE *file;
 
4648
 
 
4649
  if (!test_if_hard_path(name))
 
4650
  {
 
4651
    sprintf(buff,"%s%s",opt_basedir,name);
 
4652
    name=buff;
 
4653
  }
 
4654
  fn_format(buff, name, "", "", MY_UNPACK_FILENAME);
 
4655
 
 
4656
  if (!embedded_server_arg_count)
 
4657
  {
 
4658
    embedded_server_arg_count=1;
 
4659
    embedded_server_args[0]= (char*) "";    /* Progname */
 
4660
  }
 
4661
  if (!(file= fopen(buff, "r")))
 
4662
    die("Failed to open file '%s'", buff);
 
4663
 
 
4664
  while (embedded_server_arg_count < MAX_EMBEDDED_SERVER_ARGS &&
 
4665
         (str=fgets(argument,sizeof(argument), file)))
 
4666
  {
 
4667
    *(strchr(str, '\0')-1)=0;        /* Remove end newline */
 
4668
    if (!(embedded_server_args[embedded_server_arg_count]=
 
4669
          (char*) strdup(str)))
 
4670
    {
 
4671
      fclose(file);
 
4672
      die("Out of memory");
 
4673
 
 
4674
    }
 
4675
    embedded_server_arg_count++;
 
4676
  }
 
4677
  fclose(file);
 
4678
  if (str)
 
4679
    die("Too many arguments in option file: %s",name);
 
4680
 
 
4681
  return;
 
4682
}
 
4683
 
 
4684
 
 
4685
bool get_one_option(int optid, const struct my_option *, char *argument)
 
4686
{
 
4687
  char *endchar= NULL;
 
4688
  uint64_t temp_drizzle_port= 0;
 
4689
 
 
4690
  switch(optid) {
 
4691
  case 'r':
 
4692
    record = 1;
 
4693
    break;
 
4694
  case 'x':
 
4695
  {
 
4696
    char buff[FN_REFLEN];
 
4697
    if (!test_if_hard_path(argument))
 
4698
    {
 
4699
      sprintf(buff,"%s%s",opt_basedir,argument);
 
4700
      argument= buff;
 
4701
    }
 
4702
    fn_format(buff, argument, "", "", MY_UNPACK_FILENAME);
 
4703
    assert(cur_file == file_stack && cur_file->file == 0);
 
4704
    if (!(cur_file->file= fopen(buff, "r")))
 
4705
      die("Could not open '%s' for reading: errno = %d", buff, errno);
 
4706
    if (!(cur_file->file_name= strdup(buff)))
 
4707
      die("Out of memory");
 
4708
    cur_file->lineno= 1;
 
4709
    break;
 
4710
  }
 
4711
  case 'm':
 
4712
  {
 
4713
    static char buff[FN_REFLEN];
 
4714
    if (!test_if_hard_path(argument))
 
4715
    {
 
4716
      sprintf(buff,"%s%s",opt_basedir,argument);
 
4717
      argument= buff;
 
4718
    }
 
4719
    fn_format(buff, argument, "", "", MY_UNPACK_FILENAME);
 
4720
    timer_file= buff;
 
4721
    unlink(timer_file);       /* Ignore error, may not exist */
 
4722
    break;
 
4723
  }
 
4724
  case 'p':
 
4725
    temp_drizzle_port= (uint64_t) strtoul(argument, &endchar, 10);
 
4726
    /* if there is an alpha character this is not a valid port */
 
4727
    if (strlen(endchar) != 0)
 
4728
    {
 
4729
      fprintf(stderr, _("Non-integer value supplied for port.  If you are trying to enter a password please use --password instead.\n"));
 
4730
      exit(1);
 
4731
    }
 
4732
    /* If the port number is > 65535 it is not a valid port
 
4733
       This also helps with potential data loss casting unsigned long to a
 
4734
       uint32_t. */
 
4735
    if ((temp_drizzle_port == 0) || (temp_drizzle_port > 65535))
 
4736
    {
 
4737
      fprintf(stderr, _("Value supplied for port is not valid.\n"));
 
4738
      exit(1);
 
4739
    }
 
4740
    else
 
4741
    {
 
4742
      opt_port= (uint32_t) temp_drizzle_port;
 
4743
    }
 
4744
    break;
 
4745
  case 'P':
 
4746
    if (argument)
 
4747
    {
 
4748
      if (opt_pass)
 
4749
        free(opt_pass);
 
4750
      opt_pass = strdup(argument);
 
4751
      if (opt_pass == NULL)
 
4752
        die("Out of memory");
 
4753
      while (*argument)
 
4754
      {
 
4755
        /* Overwriting password with 'x' */
 
4756
        *argument++= 'x';
 
4757
      }
 
4758
      tty_password= 0;
 
4759
    }
 
4760
    else
 
4761
      tty_password= 1;
 
4762
    break;
 
4763
  case 't':
 
4764
    strncpy(TMPDIR, argument, sizeof(TMPDIR));
 
4765
    break;
 
4766
  case 'A':
 
4767
    if (!embedded_server_arg_count)
 
4768
    {
 
4769
      embedded_server_arg_count=1;
 
4770
      embedded_server_args[0]= (char*) "";
 
4771
    }
 
4772
    if (embedded_server_arg_count == MAX_EMBEDDED_SERVER_ARGS-1 ||
 
4773
        !(embedded_server_args[embedded_server_arg_count++]=
 
4774
          strdup(argument)))
 
4775
    {
 
4776
      die("Can't use server argument");
 
4777
    }
 
4778
    break;
 
4779
  case 'F':
 
4780
    read_embedded_server_arguments(argument);
 
4781
    break;
 
4782
  case 'V':
 
4783
    print_version();
 
4784
    exit(0);
 
4785
  case '?':
 
4786
    usage();
 
4787
    exit(0);
 
4788
  }
 
4789
  return 0;
 
4790
}
 
4791
 
 
4792
 
 
4793
static int parse_args(int argc, char **argv)
 
4794
{
 
4795
  load_defaults("drizzle",load_default_groups,&argc,&argv);
 
4796
  default_argv= argv;
 
4797
 
 
4798
  if ((handle_options(&argc, &argv, my_long_options, get_one_option)))
 
4799
    exit(1);
 
4800
 
 
4801
  if (argc > 1)
 
4802
  {
 
4803
    usage();
 
4804
    exit(1);
 
4805
  }
 
4806
  if (argc == 1)
 
4807
    opt_db= *argv;
 
4808
  if (tty_password)
 
4809
    opt_pass= client_get_tty_password(NULL);          /* purify tested */
 
4810
  if (debug_info_flag)
 
4811
    my_end_arg= MY_CHECK_ERROR | MY_GIVE_INFO;
 
4812
  if (debug_check_flag)
 
4813
    my_end_arg= MY_CHECK_ERROR;
 
4814
 
 
4815
  return 0;
 
4816
}
 
4817
 
4645
4818
/*
4646
4819
  Write the content of str into file
4647
4820
 
4658
4831
  int fd;
4659
4832
  char buff[FN_REFLEN];
4660
4833
  int flags= O_WRONLY | O_CREAT;
4661
 
  if (!internal::test_if_hard_path(fname))
 
4834
  if (!test_if_hard_path(fname))
4662
4835
  {
4663
 
    snprintf(buff, sizeof(buff), "%s%s",opt_basedir.c_str(),fname);
 
4836
    sprintf(buff,"%s%s",opt_basedir,fname);
4664
4837
    fname= buff;
4665
4838
  }
4666
 
  internal::fn_format(buff, fname, "", "", MY_UNPACK_FILENAME);
 
4839
  fn_format(buff, fname, "", "", MY_UNPACK_FILENAME);
4667
4840
 
4668
4841
  if (!append)
4669
4842
    flags|= O_TRUNC;
4670
 
  if ((fd= internal::my_open(buff, flags,
 
4843
  if ((fd= my_open(buff, flags,
4671
4844
                   MYF(MY_WME | MY_FFNF))) < 0)
4672
4845
    die("Could not open '%s' for writing: errno = %d", buff, errno);
4673
4846
  if (append && lseek(fd, 0, SEEK_END) == MY_FILEPOS_ERROR)
4674
4847
    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)))
 
4848
  if (my_write(fd, (unsigned char*)str, size, MYF(MY_WME|MY_FNABP)))
4676
4849
    die("write failed");
4677
 
  internal::my_close(fd, MYF(0));
 
4850
  my_close(fd, MYF(0));
4678
4851
}
4679
4852
 
4680
4853
/*
4696
4869
void dump_result_to_log_file(const char *buf, int size)
4697
4870
{
4698
4871
  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 :
 
4872
  str_to_file(fn_format(log_file, result_file_name, opt_logdir, ".log",
 
4873
                        *opt_logdir ? MY_REPLACE_DIR | MY_REPLACE_EXT :
4701
4874
                        MY_REPLACE_EXT),
4702
4875
              buf, size);
4703
4876
  fprintf(stderr, "\nMore results from queries before failure can be found in %s\n",
4707
4880
void dump_progress(void)
4708
4881
{
4709
4882
  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 :
 
4883
  str_to_file(fn_format(progress_file, result_file_name,
 
4884
                        opt_logdir, ".progress",
 
4885
                        *opt_logdir ? MY_REPLACE_DIR | MY_REPLACE_EXT :
4713
4886
                        MY_REPLACE_EXT),
4714
4887
              ds_progress.c_str(), ds_progress.length());
4715
4888
}
4718
4891
{
4719
4892
  char warn_file[FN_REFLEN];
4720
4893
 
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 :
 
4894
  str_to_file(fn_format(warn_file, result_file_name, opt_logdir, ".warnings",
 
4895
                        *opt_logdir ? MY_REPLACE_DIR | MY_REPLACE_EXT :
4723
4896
                        MY_REPLACE_EXT),
4724
4897
              ds_warning_messages.c_str(), ds_warning_messages.length());
4725
4898
}
5023
5196
      }
5024
5197
 
5025
5198
      /*
5026
 
        Need to call drizzle_result_affected_rows() before the "new"
 
5199
        Need to call drizzleclient_affected_rows() before the "new"
5027
5200
        query to find the warnings
5028
5201
      */
5029
5202
      if (!disable_info)
5090
5263
  uint32_t i;
5091
5264
 
5092
5265
 
5093
 
  if (! command->require_file.empty())
 
5266
  if (command->require_file[0])
5094
5267
  {
5095
5268
    /*
5096
5269
      The query after a "--require" failed. This is fine as long the server
5249
5422
    Create a temporary dynamic string to contain the output from
5250
5423
    this query.
5251
5424
  */
5252
 
  if (! command->require_file.empty())
 
5425
  if (command->require_file[0])
5253
5426
  {
5254
5427
    ds= &ds_result;
5255
5428
  }
5292
5465
    ds= save_ds;
5293
5466
  }
5294
5467
 
5295
 
  if (! command->require_file.empty())
 
5468
  if (command->require_file[0])
5296
5469
  {
5297
5470
    /* A result file was specified for _this_ query
5298
5471
       and the output should be checked against an already
5417
5590
 
5418
5591
}
5419
5592
 
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
5593
 
5450
5594
int main(int argc, char **argv)
5451
5595
{
5452
 
try
5453
 
{
5454
5596
  struct st_command *command;
5455
5597
  bool q_send_flag= 0, abort_flag= 0;
5456
5598
  uint32_t command_executed= 0, last_command_executed= 0;
5457
 
  string save_file("");
 
5599
  char save_file[FN_REFLEN];
5458
5600
  struct stat res_info;
 
5601
  MY_INIT(argv[0]);
5459
5602
 
 
5603
  save_file[0]= 0;
5460
5604
  TMPDIR[0]= 0;
5461
5605
 
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
5606
  /* Init expected errors */
5581
5607
  memset(&saved_expected_errors, 0, sizeof(saved_expected_errors));
5582
5608
 
5600
5626
  cur_block->ok= true; /* Outer block should always be executed */
5601
5627
  cur_block->cmd= cmd_none;
5602
5628
 
 
5629
  if (hash_init(&var_hash, charset_info,
 
5630
                1024, 0, 0, get_var_key, var_free, MYF(0)))
 
5631
    die("Variable hash initialization failed");
 
5632
 
5603
5633
  var_set_string("$DRIZZLE_SERVER_VERSION", drizzle_version());
5604
5634
 
5605
5635
  memset(&master_pos, 0, sizeof(master_pos));
5613
5643
  ds_progress.reserve(2048);
5614
5644
  ds_warning_messages.reserve(2048);
5615
5645
 
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
 
  }
 
5646
  parse_args(argc, argv);
5735
5647
 
5736
5648
  server_initialized= 1;
5737
5649
  if (cur_file == file_stack && cur_file->file == 0)
5747
5659
    die("Failed in drizzle_create()");
5748
5660
  if (!( drizzle_con_create(cur_con->drizzle, &cur_con->con)))
5749
5661
    die("Failed in drizzle_con_create()");
5750
 
  drizzle_con_add_options(&cur_con->con, use_drizzle_protocol ? DRIZZLE_CON_EXPERIMENTAL : DRIZZLE_CON_MYSQL);
5751
5662
 
5752
5663
  if (!(cur_con->name = strdup("default")))
5753
5664
    die("Out of memory");
 
5665
 
5754
5666
  safe_connect(&cur_con->con, cur_con->name, opt_host, opt_user, opt_pass,
5755
5667
               opt_db, opt_port);
5756
5668
 
5757
 
  fill_global_error_names();
5758
 
 
5759
5669
  /* Use all time until exit if no explicit 'start_timer' */
5760
5670
  timer_start= timer_now();
5761
5671
 
5769
5679
  /* Update $drizzleclient_get_server_version to that of current connection */
5770
5680
  var_set_drizzleclient_get_server_version(&cur_con->con);
5771
5681
 
5772
 
  if (! opt_include.empty())
 
5682
  if (opt_include)
5773
5683
  {
5774
 
    open_file(opt_include.c_str());
 
5684
    open_file(opt_include);
5775
5685
  }
5776
5686
 
5777
5687
  while (!read_command(&command) && !abort_flag)
5885
5795
        /* Check for special property for this query */
5886
5796
        display_result_vertically|= (command->type == Q_QUERY_VERTICAL);
5887
5797
 
5888
 
        if (! save_file.empty())
 
5798
        if (save_file[0])
5889
5799
        {
5890
 
          command->require_file= save_file;
5891
 
          save_file.clear();
 
5800
          strncpy(command->require_file, save_file, sizeof(save_file) - 1);
 
5801
          save_file[0]= 0;
5892
5802
        }
5893
5803
        run_query(cur_con, command, flags);
5894
5804
        command_executed++;
5925
5835
        command->last_argument= command->end;
5926
5836
        break;
5927
5837
      case Q_REQUIRE:
5928
 
        do_get_file_name(command, save_file);
 
5838
        do_get_file_name(command, save_file, sizeof(save_file));
5929
5839
        break;
5930
5840
      case Q_ERROR:
5931
5841
        do_get_errcodes(command);
6075
5985
  */
6076
5986
  if (ds_res.length())
6077
5987
  {
6078
 
    if (! result_file_name.empty())
 
5988
    if (result_file_name)
6079
5989
    {
6080
5990
      /* A result file has been specified */
6081
5991
 
6082
5992
      if (record)
6083
5993
      {
6084
5994
        /* 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());
 
5995
        str_to_file(result_file_name, ds_res.c_str(), ds_res.length());
6086
5996
      }
6087
5997
      else
6088
5998
      {
6105
6015
  }
6106
6016
 
6107
6017
  if (!command_executed &&
6108
 
      ! result_file_name.empty() && !stat(result_file_name.c_str(), &res_info))
 
6018
      result_file_name && !stat(result_file_name, &res_info))
6109
6019
  {
6110
6020
    /*
6111
6021
      my_stat() successful on result file. Check if we have not run a
6117
6027
    die("No queries executed but result file found!");
6118
6028
  }
6119
6029
 
6120
 
  if ( opt_mark_progress && ! result_file_name.empty() )
 
6030
  if ( opt_mark_progress && result_file_name )
6121
6031
    dump_progress();
6122
6032
 
6123
6033
  /* Dump warning messages */
6124
 
  if (! result_file_name.empty() && ds_warning_messages.length())
 
6034
  if (result_file_name && ds_warning_messages.length())
6125
6035
    dump_warning_messages();
6126
6036
 
6127
6037
  timer_output();
6128
6038
  /* Yes, if we got this far the test has suceeded! Sakila smiles */
6129
6039
  cleanup_and_exit(0);
6130
 
}
6131
 
 
6132
 
  catch(exception &err)
6133
 
  {
6134
 
    cerr<<err.what()<<endl;
6135
 
  }
6136
 
 
6137
6040
  return 0; /* Keep compiler happy too */
6138
6041
}
6139
6042
 
6175
6078
 
6176
6079
uint64_t timer_now(void)
6177
6080
{
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) */
 
6081
  return my_micro_time() / 1000;
6191
6082
}
6192
6083
 
6193
6084
 
6402
6293
  char* pattern; /* Pattern to be replaced */
6403
6294
  char* replace; /* String or expression to replace the pattern with */
6404
6295
  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
6296
};
6408
6297
 
6409
6298
struct st_replace_regex
6427
6316
struct st_replace_regex *glob_replace_regex= 0;
6428
6317
 
6429
6318
int reg_replace(char** buf_p, int* buf_len_p, char *pattern, char *replace,
6430
 
                char *string, int icase, int global);
 
6319
                char *string, int icase);
6431
6320
 
6432
6321
 
6433
6322
 
6528
6417
 
6529
6418
    /* Check if we should do matching case insensitive */
6530
6419
    if (p < expr_end && *p == 'i')
6531
 
    {
6532
 
      p++;
6533
6420
      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
6421
 
6543
6422
    /* done parsing the statement, now place it in regex_arr */
6544
6423
    if (insert_dynamic(&res->regex_arr,(unsigned char*) &reg))
6596
6475
    get_dynamic(&r->regex_arr,(unsigned char*)&re,i);
6597
6476
 
6598
6477
    if (!reg_replace(&out_buf, buf_len_p, re.pattern, re.replace,
6599
 
                     in_buf, re.icase, re.global))
 
6478
                     in_buf, re.icase))
6600
6479
    {
6601
6480
      /* if the buffer has been reallocated, make adjustements */
6602
6481
      if (save_out_buf != out_buf)
6666
6545
  icase - flag, if set to 1 the match is case insensitive
6667
6546
*/
6668
6547
int reg_replace(char** buf_p, int* buf_len_p, char *pattern,
6669
 
                char *replace, char *in_string, int icase, int global)
 
6548
                char *replace, char *in_string, int icase)
6670
6549
{
 
6550
  string string_to_match(in_string);
6671
6551
  const char *error= NULL;
6672
6552
  int erroffset;
6673
6553
  int ovector[3];
6674
6554
  pcre *re= pcre_compile(pattern,
6675
 
                         icase ? PCRE_CASELESS | PCRE_MULTILINE : PCRE_MULTILINE,
 
6555
                         icase ? PCRE_CASELESS : 0,
6676
6556
                         &error, &erroffset, NULL);
6677
6557
  if (re == NULL)
6678
6558
    return 1;
6679
6559
 
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
 
  }
 
6560
  int rc= pcre_exec(re, NULL, in_string, (int)strlen(in_string),
 
6561
                    0, 0, ovector, 3);
 
6562
  if (rc < 0)
 
6563
  {
 
6564
    pcre_free(re);
 
6565
    return 1;
 
6566
  }
 
6567
 
 
6568
  char *substring_to_replace= in_string + ovector[0];
 
6569
  int substring_length= ovector[1] - ovector[0];
 
6570
  *buf_len_p= strlen(in_string) - substring_length + strlen(replace);
 
6571
  char * new_buf = (char *)malloc(*buf_len_p+1);
 
6572
  if (new_buf == NULL)
 
6573
  {
 
6574
    pcre_free(re);
 
6575
    return 1;
 
6576
  }
 
6577
 
 
6578
  memset(new_buf, 0, *buf_len_p+1);
 
6579
  strncpy(new_buf, in_string, substring_to_replace-in_string);
 
6580
  strncpy(new_buf+(substring_to_replace-in_string), replace, strlen(replace));
 
6581
  strncpy(new_buf+(substring_to_replace-in_string)+strlen(replace),
 
6582
          substring_to_replace + substring_length,
 
6583
          strlen(in_string)
 
6584
            - substring_length
 
6585
            - (substring_to_replace-in_string));
 
6586
  *buf_p= new_buf;
 
6587
 
 
6588
  pcre_free(re);
 
6589
  return 0;
6752
6590
}
6753
6591
 
6754
6592
 
7332
7170
      return(1);
7333
7171
    if (new_pos != pa->str)
7334
7172
    {
7335
 
      ptrdiff_t diff= PTR_BYTE_DIFF(new_pos,pa->str);
 
7173
      ptrdiff_t diff=PTR_BYTE_DIFF(new_pos,pa->str);
7336
7174
      for (i=0 ; i < pa->typelib.count ; i++)
7337
7175
        pa->typelib.type_names[i]= ADD_TO_PTR(pa->typelib.type_names[i],diff,
7338
7176
                                              char*);