~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to client/drizzletest.cc

  • Committer: Patrick Galbraith
  • Date: 2009-10-08 22:42:05 UTC
  • mto: (1166.5.3 memcached_functions)
  • mto: This revision was merged to the branch mainline in revision 1189.
  • Revision ID: patg@patrick-galbraiths-macbook-pro.local-20091008224205-gq1pehjsivvx0qo9
Starting over with a fresh tree, moved in memcached functions.

Memcached Functions for Drizzle. 

All tests pass.

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
{
438
428
VAR* var_from_env(const char *, const char *);
439
429
VAR* var_init(VAR* v, const char *name, int name_len, const char *val,
440
430
              int val_len);
441
 
void var_free(pair<string, VAR*> v);
 
431
extern "C" void var_free(void* v);
442
432
VAR* var_get(const char *var_name, const char** var_name_end,
443
433
             bool raw, bool ignore_not_existing);
444
434
void eval_expr(VAR* v, const char *p, const char** p_end);
782
772
    const struct command_arg *arg= &args[i];
783
773
    arg->ds->clear();
784
774
 
785
 
    bool known_arg_type= true;
786
775
    switch (arg->type) {
787
776
      /* A string */
788
777
    case ARG_STRING:
817
806
      break;
818
807
 
819
808
    default:
820
 
      known_arg_type= false;
 
809
      assert("Unknown argument type");
821
810
      break;
822
811
    }
823
 
    assert(known_arg_type);
824
812
 
825
813
    /* Check required arg */
826
814
    if (arg->ds->length() == 0 && arg->required)
912
900
 
913
901
  close_connections();
914
902
  close_files();
915
 
  for_each(var_hash.begin(), var_hash.end(), var_free);
916
 
  var_hash.clear();
 
903
  hash_free(&var_hash);
917
904
 
918
 
  vector<st_command *>::iterator iter;
 
905
  vector<struct st_command *>::iterator iter;
919
906
  for (iter= q_lines.begin() ; iter < q_lines.end() ; iter++)
920
907
  {
921
908
    struct st_command * q_line= *iter;
927
914
    if (var_reg[i].alloced_len)
928
915
      free(var_reg[i].str_val);
929
916
  }
 
917
  while (embedded_server_arg_count > 1)
 
918
    free(embedded_server_args[--embedded_server_arg_count]);
930
919
 
931
920
  free_all_replace();
932
921
  free(opt_pass);
 
922
  free_defaults(default_argv);
933
923
 
934
924
  return;
935
925
}
938
928
static void cleanup_and_exit(int exit_code)
939
929
{
940
930
  free_used_memory();
941
 
  internal::my_end();
 
931
  my_end(my_end_arg);
942
932
 
943
933
  if (!silent) {
944
934
    switch (exit_code) {
1011
1001
  }
1012
1002
 
1013
1003
  /* Dump the result that has been accumulated so far to .log file */
1014
 
  if (! result_file_name.empty() && ds_res.length())
 
1004
  if (result_file_name && ds_res.length())
1015
1005
    dump_result_to_log_file(ds_res.c_str(), ds_res.length());
1016
1006
 
1017
1007
  /* Dump warning messages */
1018
 
  if (! result_file_name.empty() && ds_warning_messages.length())
 
1008
  if (result_file_name && ds_warning_messages.length())
1019
1009
    dump_warning_messages();
1020
1010
 
1021
1011
  /*
1151
1141
  uint32_t len;
1152
1142
  char buff[512];
1153
1143
 
1154
 
  if ((fd= internal::my_open(filename, O_RDONLY, MYF(0))) < 0)
 
1144
  if ((fd= my_open(filename, O_RDONLY, MYF(0))) < 0)
1155
1145
    die("Failed to open file '%s'", filename);
1156
 
  while((len= internal::my_read(fd, (unsigned char*)&buff,
 
1146
  while((len= my_read(fd, (unsigned char*)&buff,
1157
1147
                      sizeof(buff), MYF(0))) > 0)
1158
1148
  {
1159
1149
    char *p= buff, *start= buff;
1175
1165
    /* Output any chars that might be left */
1176
1166
    ds->append(start, p-start);
1177
1167
  }
1178
 
  internal::my_close(fd, MYF(0));
 
1168
  my_close(fd, MYF(0));
1179
1169
}
1180
1170
 
1181
1171
 
1351
1341
 
1352
1342
*/
1353
1343
 
1354
 
static int compare_files2(int fd, const char* filename2)
 
1344
static int compare_files2(File fd, const char* filename2)
1355
1345
{
1356
1346
  int error= RESULT_OK;
1357
 
  int fd2;
 
1347
  File fd2;
1358
1348
  uint32_t len, len2;
1359
1349
  char buff[512], buff2[512];
1360
1350
  const char *fname= filename2;
1361
1351
  string tmpfile;
1362
1352
 
1363
 
  if ((fd2= internal::my_open(fname, O_RDONLY, MYF(0))) < 0)
 
1353
  if ((fd2= my_open(fname, O_RDONLY, MYF(0))) < 0)
1364
1354
  {
1365
 
    internal::my_close(fd, MYF(0));
1366
 
    if (! opt_testdir.empty())
 
1355
    my_close(fd, MYF(0));
 
1356
    if (opt_testdir != NULL)
1367
1357
    {
1368
1358
      tmpfile= opt_testdir;
1369
1359
      if (tmpfile[tmpfile.length()] != '/')
1371
1361
      tmpfile.append(filename2);
1372
1362
      fname= tmpfile.c_str();
1373
1363
    }
1374
 
    if ((fd2= internal::my_open(fname, O_RDONLY, MYF(0))) < 0)
 
1364
    if ((fd2= my_open(fname, O_RDONLY, MYF(0))) < 0)
1375
1365
    {
1376
 
      internal::my_close(fd, MYF(0));
 
1366
      my_close(fd, MYF(0));
1377
1367
    
1378
1368
      die("Failed to open second file: '%s'", fname);
1379
1369
    }
1380
1370
  }
1381
 
  while((len= internal::my_read(fd, (unsigned char*)&buff,
 
1371
  while((len= my_read(fd, (unsigned char*)&buff,
1382
1372
                      sizeof(buff), MYF(0))) > 0)
1383
1373
  {
1384
 
    if ((len2= internal::my_read(fd2, (unsigned char*)&buff2,
 
1374
    if ((len2= my_read(fd2, (unsigned char*)&buff2,
1385
1375
                       sizeof(buff2), MYF(0))) < len)
1386
1376
    {
1387
1377
      /* File 2 was smaller */
1401
1391
      break;
1402
1392
    }
1403
1393
  }
1404
 
  if (!error && internal::my_read(fd2, (unsigned char*)&buff2,
 
1394
  if (!error && my_read(fd2, (unsigned char*)&buff2,
1405
1395
                        sizeof(buff2), MYF(0)) > 0)
1406
1396
  {
1407
1397
    /* File 1 was smaller */
1408
1398
    error= RESULT_LENGTH_MISMATCH;
1409
1399
  }
1410
1400
 
1411
 
  internal::my_close(fd2, MYF(0));
 
1401
  my_close(fd2, MYF(0));
1412
1402
 
1413
1403
  return error;
1414
1404
}
1429
1419
 
1430
1420
static int compare_files(const char* filename1, const char* filename2)
1431
1421
{
1432
 
  int fd;
 
1422
  File fd;
1433
1423
  int error;
1434
1424
 
1435
 
  if ((fd= internal::my_open(filename1, O_RDONLY, MYF(0))) < 0)
 
1425
  if ((fd= my_open(filename1, O_RDONLY, MYF(0))) < 0)
1436
1426
    die("Failed to open first file: '%s'", filename1);
1437
1427
 
1438
1428
  error= compare_files2(fd, filename2);
1439
1429
 
1440
 
  internal::my_close(fd, MYF(0));
 
1430
  my_close(fd, MYF(0));
1441
1431
 
1442
1432
  return error;
1443
1433
}
1458
1448
static int string_cmp(string* ds, const char *fname)
1459
1449
{
1460
1450
  int error;
1461
 
  int fd;
 
1451
  File fd;
1462
1452
  char temp_file_path[FN_REFLEN];
1463
1453
 
1464
 
  if ((fd= internal::create_temp_file(temp_file_path, TMPDIR,
 
1454
  if ((fd= create_temp_file(temp_file_path, NULL,
1465
1455
                            "tmp", MYF(MY_WME))) < 0)
1466
1456
    die("Failed to create temporary file for ds");
1467
1457
 
1468
1458
  /* Write ds to temporary file and set file pos to beginning*/
1469
 
  if (internal::my_write(fd, (unsigned char *) ds->c_str(), ds->length(),
 
1459
  if (my_write(fd, (unsigned char *) ds->c_str(), ds->length(),
1470
1460
               MYF(MY_FNABP | MY_WME)) ||
1471
1461
      lseek(fd, 0, SEEK_SET) == MY_FILEPOS_ERROR)
1472
1462
  {
1473
 
    internal::my_close(fd, MYF(0));
 
1463
    my_close(fd, MYF(0));
1474
1464
    /* Remove the temporary file */
1475
 
    internal::my_delete(temp_file_path, MYF(0));
 
1465
    my_delete(temp_file_path, MYF(0));
1476
1466
    die("Failed to write file '%s'", temp_file_path);
1477
1467
  }
1478
1468
 
1479
1469
  error= compare_files2(fd, fname);
1480
1470
 
1481
 
  internal::my_close(fd, MYF(0));
 
1471
  my_close(fd, MYF(0));
1482
1472
  /* Remove the temporary file */
1483
 
  internal::my_delete(temp_file_path, MYF(0));
 
1473
  my_delete(temp_file_path, MYF(0));
1484
1474
 
1485
1475
  return(error);
1486
1476
}
1503
1493
  const char* mess= "Result content mismatch\n";
1504
1494
 
1505
1495
 
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())) {
 
1496
  assert(result_file_name);
 
1497
 
 
1498
  if (access(result_file_name, F_OK) != 0)
 
1499
    die("The specified result file does not exist: '%s'", result_file_name);
 
1500
 
 
1501
  switch (string_cmp(ds, result_file_name)) {
1512
1502
  case RESULT_OK:
1513
1503
    break; /* ok */
1514
1504
  case RESULT_LENGTH_MISMATCH:
1522
1512
    */
1523
1513
    char reject_file[FN_REFLEN];
1524
1514
    size_t reject_length;
1525
 
    internal::dirname_part(reject_file, result_file_name.c_str(), &reject_length);
 
1515
    dirname_part(reject_file, result_file_name, &reject_length);
1526
1516
 
1527
1517
    if (access(reject_file, W_OK) == 0)
1528
1518
    {
1529
1519
      /* Result file directory is writable, save reject file there */
1530
 
      internal::fn_format(reject_file, result_file_name.c_str(), NULL,
 
1520
      fn_format(reject_file, result_file_name, NULL,
1531
1521
                ".reject", MY_REPLACE_EXT);
1532
1522
    }
1533
1523
    else
1534
1524
    {
1535
1525
      /* Put reject file in opt_logdir */
1536
 
      internal::fn_format(reject_file, result_file_name.c_str(), opt_logdir.c_str(),
 
1526
      fn_format(reject_file, result_file_name, opt_logdir,
1537
1527
                ".reject", MY_REPLACE_DIR | MY_REPLACE_EXT);
1538
1528
    }
1539
1529
    str_to_file(reject_file, ds->c_str(), ds->length());
1540
1530
 
1541
1531
    ds->erase(); /* Don't create a .log file */
1542
1532
 
1543
 
    show_diff(NULL, result_file_name.c_str(), reject_file);
 
1533
    show_diff(NULL, result_file_name, reject_file);
1544
1534
    die("%s",mess);
1545
1535
    break;
1546
1536
  }
1574
1564
  if (string_cmp(ds, fname.c_str()))
1575
1565
  {
1576
1566
    char reason[FN_REFLEN];
1577
 
    internal::fn_format(reason, fname.c_str(), "", "", MY_REPLACE_EXT | MY_REPLACE_DIR);
 
1567
    fn_format(reason, fname.c_str(), "", "", MY_REPLACE_EXT | MY_REPLACE_DIR);
1578
1568
    abort_not_supported_test("Test requires: '%s'", reason);
1579
1569
  }
1580
1570
  return;
1625
1615
}
1626
1616
 
1627
1617
 
 
1618
unsigned char *get_var_key(const unsigned char* var, size_t *len, bool)
 
1619
{
 
1620
  register char* key;
 
1621
  key = ((VAR*)var)->name;
 
1622
  *len = ((VAR*)var)->name_len;
 
1623
  return (unsigned char*)key;
 
1624
}
 
1625
 
1628
1626
 
1629
1627
VAR *var_init(VAR *v, const char *name, int name_len, const char *val,
1630
1628
              int val_len)
1662
1660
}
1663
1661
 
1664
1662
 
1665
 
void var_free(pair<string, VAR *> v)
 
1663
void var_free(void *v)
1666
1664
{
1667
 
  free(v.second->str_val);
1668
 
  free(v.second->env_s);
1669
 
  if (v.second->alloced)
1670
 
    free(v.second);
 
1665
  free(((VAR*) v)->str_val);
 
1666
  free(((VAR*) v)->env_s);
 
1667
  if (((VAR*)v)->alloced)
 
1668
    free(v);
1671
1669
}
1672
1670
 
1673
1671
 
1679
1677
    tmp = def_val;
1680
1678
 
1681
1679
  v = var_init(0, name, strlen(name), tmp, strlen(tmp));
1682
 
  string var_name(name);
1683
 
  var_hash.insert(make_pair(var_name, v));
 
1680
  my_hash_insert(&var_hash, (unsigned char*)v);
1684
1681
  return v;
1685
1682
}
1686
1683
 
1711
1708
    if (length >= MAX_VAR_NAME_LENGTH)
1712
1709
      die("Too long variable name: %s", save_var_name);
1713
1710
 
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())
 
1711
    if (!(v = (VAR*) hash_search(&var_hash, (const unsigned char*) save_var_name,
 
1712
                                 length)))
1718
1713
    {
1719
1714
      char buff[MAX_VAR_NAME_LENGTH+1];
1720
1715
      strncpy(buff, save_var_name, length);
1721
1716
      buff[length]= '\0';
1722
1717
      v= var_from_env(buff, "");
1723
1718
    }
1724
 
    else
1725
 
    {
1726
 
      v= (*iter).second;
1727
 
    }
1728
1719
    var_name--;  /* Point at last character */
1729
1720
  }
1730
1721
  else
1749
1740
 
1750
1741
static VAR *var_obtain(const char *name, int len)
1751
1742
{
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));
 
1743
  VAR* v;
 
1744
  if ((v = (VAR*)hash_search(&var_hash, (const unsigned char *) name, len)))
 
1745
    return v;
 
1746
  v = var_init(0, name, len, "", 0);
 
1747
  my_hash_insert(&var_hash, (unsigned char*)v);
1759
1748
  return v;
1760
1749
}
1761
1750
 
2154
2143
{
2155
2144
  char buff[FN_REFLEN];
2156
2145
 
2157
 
  if (!internal::test_if_hard_path(name))
 
2146
  if (!test_if_hard_path(name))
2158
2147
  {
2159
 
    snprintf(buff, sizeof(buff), "%s%s",opt_basedir.c_str(),name);
 
2148
    sprintf(buff,"%s%s",opt_basedir,name);
2160
2149
    name=buff;
2161
2150
  }
2162
 
  internal::fn_format(buff, name, "", "", MY_UNPACK_FILENAME);
 
2151
  fn_format(buff, name, "", "", MY_UNPACK_FILENAME);
2163
2152
 
2164
2153
  if (cur_file == file_stack_end)
2165
2154
    die("Source directives are nesting too deep");
2210
2199
    ; /* Do nothing */
2211
2200
  else
2212
2201
  {
2213
 
    if (! opt_testdir.empty())
 
2202
    if (opt_testdir != NULL)
2214
2203
    {
2215
2204
      string testdir(opt_testdir);
2216
2205
      if (testdir[testdir.length()] != '/')
2478
2467
                     rm_args, sizeof(rm_args)/sizeof(struct command_arg),
2479
2468
                     ' ');
2480
2469
 
2481
 
  error= internal::my_delete(ds_filename.c_str(), MYF(0)) != 0;
 
2470
  error= my_delete(ds_filename.c_str(), MYF(0)) != 0;
2482
2471
  handle_command_error(command, error);
2483
2472
  return;
2484
2473
}
2512
2501
                     sizeof(copy_file_args)/sizeof(struct command_arg),
2513
2502
                     ' ');
2514
2503
 
2515
 
  error= (internal::my_copy(ds_from_file.c_str(), ds_to_file.c_str(),
 
2504
  error= (my_copy(ds_from_file.c_str(), ds_to_file.c_str(),
2516
2505
                  MYF(MY_DONT_OVERWRITE_FILE)) != 0);
2517
2506
  handle_command_error(command, error);
2518
2507
  return;
2610
2599
                     mkdir_args, sizeof(mkdir_args)/sizeof(struct command_arg),
2611
2600
                     ' ');
2612
2601
 
2613
 
  error= mkdir(ds_dirname.c_str(), (0777 & internal::my_umask_dir)) != 0;
 
2602
  error= mkdir(ds_dirname.c_str(), (0777 & my_umask_dir)) != 0;
2614
2603
  handle_command_error(command, error);
2615
2604
  return;
2616
2605
}
2977
2966
static void do_perl(struct st_command *command)
2978
2967
{
2979
2968
  int error;
2980
 
  int fd;
 
2969
  File fd;
2981
2970
  FILE *res_file;
2982
2971
  char buf[FN_REFLEN];
2983
2972
  char temp_file_path[FN_REFLEN];
3001
2990
  read_until_delimiter(&ds_script, &ds_delimiter);
3002
2991
 
3003
2992
  /* Create temporary file name */
3004
 
  if ((fd= internal::create_temp_file(temp_file_path, getenv("MYSQLTEST_VARDIR"),
 
2993
  if ((fd= create_temp_file(temp_file_path, getenv("MYSQLTEST_VARDIR"),
3005
2994
                            "tmp", MYF(MY_WME))) < 0)
3006
2995
    die("Failed to create temporary file for perl command");
3007
 
  internal::my_close(fd, MYF(0));
 
2996
  my_close(fd, MYF(0));
3008
2997
 
3009
2998
  str_to_file(temp_file_path, ds_script.c_str(), ds_script.length());
3010
2999
 
3024
3013
  error= pclose(res_file);
3025
3014
 
3026
3015
  /* Remove the temporary file */
3027
 
  internal::my_delete(temp_file_path, MYF(0));
 
3016
  my_delete(temp_file_path, MYF(0));
3028
3017
 
3029
3018
  handle_command_error(command, WEXITSTATUS(error));
3030
3019
  return;
3130
3119
  if (!master_pos.file[0])
3131
3120
    die("Calling 'sync_with_master' without calling 'save_master_pos'");
3132
3121
 
3133
 
  snprintf(query_buf, sizeof(query_buf), "select master_pos_wait('%s', %ld)", master_pos.file,
 
3122
  sprintf(query_buf, "select master_pos_wait('%s', %ld)", master_pos.file,
3134
3123
          master_pos.pos + offset);
3135
3124
 
3136
3125
wait_for_position:
3317
3306
  bool error= false;
3318
3307
  char *p= command->first_argument;
3319
3308
  char *sleep_start, *sleep_end= command->end;
3320
 
  double sleep_val= 0;
 
3309
  double sleep_val;
3321
3310
 
3322
3311
  while (my_isspace(charset_info, *p))
3323
3312
    p++;
3324
3313
  if (!*p)
3325
3314
    die("Missing argument to %.*s", command->first_word_len, command->query);
3326
3315
  sleep_start= p;
3327
 
  /* Check that arg starts with a digit, not handled by internal::my_strtod */
 
3316
  /* Check that arg starts with a digit, not handled by my_strtod */
3328
3317
  if (!my_isdigit(charset_info, *sleep_start))
3329
3318
    die("Invalid argument to %.*s \"%s\"", command->first_word_len,
3330
3319
        command->query,command->first_argument);
3357
3346
  if (*p)
3358
3347
    *p++= 0;
3359
3348
  command->last_argument= p;
3360
 
  if (! opt_testdir.empty())
 
3349
  if (opt_testdir != NULL)
3361
3350
  {
3362
3351
    dest= opt_testdir;
3363
3352
    if (dest[dest.length()] != '/')
3386
3375
    abort_not_supported_test("Test requires charset '%s'", charset_name);
3387
3376
}
3388
3377
 
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
3378
static uint32_t get_errcode_from_name(char *error_name, char *error_end)
3449
3379
{
3450
3380
  size_t err_name_len= error_end - error_name;
3451
3381
  string error_name_s(error_name, err_name_len);
3452
3382
 
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;
 
3383
  uint32_t code= global_error_names.getErrorCode(error_name_s);
 
3384
 
 
3385
  if (!code)
 
3386
    die("Unknown SQL error name '%s'", error_name_s.c_str());
 
3387
 
 
3388
  return(code);
3461
3389
}
3462
3390
 
3463
3391
static void do_get_errcodes(struct st_command *command)
3755
3683
*/
3756
3684
 
3757
3685
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)
 
3686
                         const char *host, const char *user, const char *pass,
 
3687
                         const char *db, int port)
3760
3688
{
3761
 
  uint32_t failed_attempts= 0;
 
3689
  int failed_attempts= 0;
3762
3690
  static uint32_t connection_retry_sleep= 100000; /* Microseconds */
3763
3691
  drizzle_return_t ret;
3764
3692
 
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());
 
3693
  drizzle_con_set_tcp(con, host, port);
 
3694
  drizzle_con_set_auth(con, user, pass);
 
3695
  drizzle_con_set_db(con, db);
3768
3696
  while((ret= drizzle_con_connect(con)) != DRIZZLE_RETURN_OK)
3769
3697
  {
3770
3698
    /*
3906
3834
 
3907
3835
static void do_connect(struct st_command *command)
3908
3836
{
3909
 
  uint32_t con_port= opt_port;
 
3837
  int con_port= opt_port;
3910
3838
  const char *con_options;
3911
 
  bool con_ssl= 0;
 
3839
  bool con_ssl= 0, con_compress= 0;
3912
3840
  struct st_connection* con_slot;
3913
3841
 
3914
3842
  string ds_connection_name;
3954
3882
    if (*ds_sock.c_str() != FN_LIBCHAR)
3955
3883
    {
3956
3884
      char buff[FN_REFLEN];
3957
 
      internal::fn_format(buff, ds_sock.c_str(), TMPDIR, "", 0);
 
3885
      fn_format(buff, ds_sock.c_str(), TMPDIR, "", 0);
3958
3886
      ds_sock.clear();
3959
3887
      ds_sock.append(buff);
3960
3888
    }
3974
3902
      end++;
3975
3903
    if (!strncmp(con_options, "SSL", 3))
3976
3904
      con_ssl= 1;
 
3905
    else if (!strncmp(con_options, "COMPRESS", 8))
 
3906
      con_compress= 1;
3977
3907
    else
3978
3908
      die("Illegal option to connect: %.*s",
3979
3909
          (int) (end - con_options), con_options);
3999
3929
    die("Failed on drizzle_create()");
4000
3930
  if (!drizzle_con_create(con_slot->drizzle, &con_slot->con))
4001
3931
    die("Failed on drizzle_con_create()");
4002
 
  drizzle_con_add_options(&con_slot->con, use_drizzle_protocol ? DRIZZLE_CON_EXPERIMENTAL : DRIZZLE_CON_MYSQL);
4003
3932
 
4004
3933
  /* Use default db name */
4005
3934
  if (ds_database.length() == 0)
4219
4148
 
4220
4149
*/
4221
4150
 
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
4151
static int read_line(char *buf, int size)
4244
4152
{
4245
4153
  char c, last_quote= 0;
4563
4471
  return;
4564
4472
}
4565
4473
 
 
4474
 
 
4475
 
4566
4476
/*
4567
4477
  Create a command from a set of lines
4568
4478
 
4580
4490
  terminated by new line '\n' regardless how many "delimiter" it contain.
4581
4491
*/
4582
4492
 
4583
 
#define MAX_QUERY (768*1024*2) /* 256K -- a test in sp-big is >128K */
 
4493
#define MAX_QUERY (256*1024*2) /* 256K -- a test in sp-big is >128K */
4584
4494
static char read_command_buf[MAX_QUERY];
4585
4495
 
4586
4496
static int read_command(struct st_command** command_ptr)
4642
4552
  return(0);
4643
4553
}
4644
4554
 
 
4555
 
 
4556
static struct my_option my_long_options[] =
 
4557
{
 
4558
  {"help", '?', "Display this help and exit.", 0, 0, 0, GET_NO_ARG, NO_ARG,
 
4559
   0, 0, 0, 0, 0, 0},
 
4560
  {"basedir", 'b', "Basedir for tests.", (char**) &opt_basedir,
 
4561
   (char**) &opt_basedir, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
4562
  {"character-sets-dir", OPT_CHARSETS_DIR,
 
4563
   "Directory where character sets are.", (char**) &opt_charsets_dir,
 
4564
   (char**) &opt_charsets_dir, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
4565
  {"compress", 'C', "Use the compressed server/client protocol.",
 
4566
   (char**) &opt_compress, (char**) &opt_compress, 0, GET_BOOL, NO_ARG, 0, 0, 0,
 
4567
   0, 0, 0},
 
4568
  {"database", 'D', "Database to use.", (char**) &opt_db, (char**) &opt_db, 0,
 
4569
   GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
4570
  {"debug-check", OPT_DEBUG_CHECK, "Check memory and open file usage at exit.",
 
4571
   (char**) &debug_check_flag, (char**) &debug_check_flag, 0,
 
4572
   GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
 
4573
  {"debug-info", OPT_DEBUG_INFO, "Print some debug info at exit.",
 
4574
   (char**) &debug_info_flag, (char**) &debug_info_flag,
 
4575
   0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
 
4576
  {"host", 'h', "Connect to host.", (char**) &opt_host, (char**) &opt_host, 0,
 
4577
   GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
4578
  {"include", 'i', "Include SQL before each test case.", (char**) &opt_include,
 
4579
   (char**) &opt_include, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
4580
  {"testdir", OPT_TESTDIR, "Path to use to search for test files",
 
4581
   (char**) &opt_testdir,
 
4582
   (char**) &opt_testdir, 0,GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
4583
  {"logdir", OPT_LOG_DIR, "Directory for log files", (char**) &opt_logdir,
 
4584
   (char**) &opt_logdir, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
4585
  {"mark-progress", OPT_MARK_PROGRESS,
 
4586
   "Write linenumber and elapsed time to <testname>.progress ",
 
4587
   (char**) &opt_mark_progress, (char**) &opt_mark_progress, 0,
 
4588
   GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
 
4589
  {"max-connect-retries", OPT_MAX_CONNECT_RETRIES,
 
4590
   "Max number of connection attempts when connecting to server",
 
4591
   (char**) &opt_max_connect_retries, (char**) &opt_max_connect_retries, 0,
 
4592
   GET_INT, REQUIRED_ARG, 500, 1, 10000, 0, 0, 0},
 
4593
  {"password", 'P', "Password to use when connecting to server.",
 
4594
   0, 0, 0, GET_STR, OPT_ARG, 0, 0, 0, 0, 0, 0},
 
4595
  {"port", 'p', "Port number to use for connection or 0 for default to, in "
 
4596
   "order of preference, drizzle.cnf, $DRIZZLE_TCP_PORT, "
 
4597
   "built-in default (" STRINGIFY_ARG(DRIZZLE_PORT) ").",
 
4598
   0, 0, 0, GET_UINT, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
4599
  {"quiet", 's', "Suppress all normal output.", (char**) &silent,
 
4600
   (char**) &silent, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
 
4601
  {"record", 'r', "Record output of test_file into result file.",
 
4602
   0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
 
4603
  {"result-file", 'R', "Read/Store result from/in this file.",
 
4604
   (char**) &result_file_name, (char**) &result_file_name, 0,
 
4605
   GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
4606
  {"server-arg", 'A', "Send option value to embedded server as a parameter.",
 
4607
   0, 0, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
4608
  {"server-file", 'F', "Read embedded server arguments from file.",
 
4609
   0, 0, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
4610
  {"silent", 's', "Suppress all normal output. Synonym for --quiet.",
 
4611
   (char**) &silent, (char**) &silent, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
 
4612
  {"sleep", 'T', "Sleep always this many seconds on sleep commands.",
 
4613
   (char**) &opt_sleep, (char**) &opt_sleep, 0, GET_INT, REQUIRED_ARG, -1, -1, 0,
 
4614
   0, 0, 0},
 
4615
  {"tail-lines", OPT_TAIL_LINES,
 
4616
   "Number of lines of the resul to include in a failure report",
 
4617
   (char**) &opt_tail_lines, (char**) &opt_tail_lines, 0,
 
4618
   GET_INT, REQUIRED_ARG, 0, 0, 10000, 0, 0, 0},
 
4619
  {"test-file", 'x', "Read test from/in this file (default stdin).",
 
4620
   0, 0, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
4621
  {"timer-file", 'm', "File where the timing in micro seconds is stored.",
 
4622
   0, 0, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
4623
  {"tmpdir", 't', "Temporary directory where sockets are put.",
 
4624
   0, 0, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
4625
  {"user", 'u', "User for login.", (char**) &opt_user, (char**) &opt_user, 0,
 
4626
   GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
 
4627
  {"verbose", 'v', "Write more.", (char**) &verbose, (char**) &verbose, 0,
 
4628
   GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
 
4629
  {"version", 'V', "Output version information and exit.",
 
4630
   0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
 
4631
  { 0, 0, 0, 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0}
 
4632
};
 
4633
 
 
4634
 
 
4635
static void print_version(void)
 
4636
{
 
4637
  printf("%s  Ver %s Distrib %s, for %s-%s (%s)\n",my_progname,MTEST_VERSION,
 
4638
         drizzle_version(),HOST_VENDOR,HOST_OS,HOST_CPU);
 
4639
}
 
4640
 
 
4641
static void usage(void)
 
4642
{
 
4643
  print_version();
 
4644
  printf("MySQL AB, by Sasha, Matt, Monty & Jani\n");
 
4645
  printf("This software comes with ABSOLUTELY NO WARRANTY\n\n");
 
4646
  printf("Runs a test against the DRIZZLE server and compares output with a results file.\n\n");
 
4647
  printf("Usage: %s [OPTIONS] [database] < test_file\n", my_progname);
 
4648
  my_print_help(my_long_options);
 
4649
  printf("  --no-defaults       Don't read default options from any options file.\n");
 
4650
  my_print_variables(my_long_options);
 
4651
}
 
4652
 
 
4653
/*
 
4654
  Read arguments for embedded server and put them into
 
4655
  embedded_server_args[]
 
4656
*/
 
4657
 
 
4658
static void read_embedded_server_arguments(const char *name)
 
4659
{
 
4660
  char argument[1024],buff[FN_REFLEN], *str=0;
 
4661
  FILE *file;
 
4662
 
 
4663
  if (!test_if_hard_path(name))
 
4664
  {
 
4665
    sprintf(buff,"%s%s",opt_basedir,name);
 
4666
    name=buff;
 
4667
  }
 
4668
  fn_format(buff, name, "", "", MY_UNPACK_FILENAME);
 
4669
 
 
4670
  if (!embedded_server_arg_count)
 
4671
  {
 
4672
    embedded_server_arg_count=1;
 
4673
    embedded_server_args[0]= (char*) "";    /* Progname */
 
4674
  }
 
4675
  if (!(file= fopen(buff, "r")))
 
4676
    die("Failed to open file '%s'", buff);
 
4677
 
 
4678
  while (embedded_server_arg_count < MAX_EMBEDDED_SERVER_ARGS &&
 
4679
         (str=fgets(argument,sizeof(argument), file)))
 
4680
  {
 
4681
    *(strchr(str, '\0')-1)=0;        /* Remove end newline */
 
4682
    if (!(embedded_server_args[embedded_server_arg_count]=
 
4683
          (char*) strdup(str)))
 
4684
    {
 
4685
      fclose(file);
 
4686
      die("Out of memory");
 
4687
 
 
4688
    }
 
4689
    embedded_server_arg_count++;
 
4690
  }
 
4691
  fclose(file);
 
4692
  if (str)
 
4693
    die("Too many arguments in option file: %s",name);
 
4694
 
 
4695
  return;
 
4696
}
 
4697
 
 
4698
 
 
4699
bool get_one_option(int optid, const struct my_option *, char *argument)
 
4700
{
 
4701
  char *endchar= NULL;
 
4702
  uint64_t temp_drizzle_port= 0;
 
4703
 
 
4704
  switch(optid) {
 
4705
  case 'r':
 
4706
    record = 1;
 
4707
    break;
 
4708
  case 'x':
 
4709
  {
 
4710
    char buff[FN_REFLEN];
 
4711
    if (!test_if_hard_path(argument))
 
4712
    {
 
4713
      sprintf(buff,"%s%s",opt_basedir,argument);
 
4714
      argument= buff;
 
4715
    }
 
4716
    fn_format(buff, argument, "", "", MY_UNPACK_FILENAME);
 
4717
    assert(cur_file == file_stack && cur_file->file == 0);
 
4718
    if (!(cur_file->file= fopen(buff, "r")))
 
4719
      die("Could not open '%s' for reading: errno = %d", buff, errno);
 
4720
    if (!(cur_file->file_name= strdup(buff)))
 
4721
      die("Out of memory");
 
4722
    cur_file->lineno= 1;
 
4723
    break;
 
4724
  }
 
4725
  case 'm':
 
4726
  {
 
4727
    static char buff[FN_REFLEN];
 
4728
    if (!test_if_hard_path(argument))
 
4729
    {
 
4730
      sprintf(buff,"%s%s",opt_basedir,argument);
 
4731
      argument= buff;
 
4732
    }
 
4733
    fn_format(buff, argument, "", "", MY_UNPACK_FILENAME);
 
4734
    timer_file= buff;
 
4735
    unlink(timer_file);       /* Ignore error, may not exist */
 
4736
    break;
 
4737
  }
 
4738
  case 'p':
 
4739
    temp_drizzle_port= (uint64_t) strtoul(argument, &endchar, 10);
 
4740
    /* if there is an alpha character this is not a valid port */
 
4741
    if (strlen(endchar) != 0)
 
4742
    {
 
4743
      fprintf(stderr, _("Non-integer value supplied for port.  If you are trying to enter a password please use --password instead.\n"));
 
4744
      exit(1);
 
4745
    }
 
4746
    /* If the port number is > 65535 it is not a valid port
 
4747
       This also helps with potential data loss casting unsigned long to a
 
4748
       uint32_t. */
 
4749
    if ((temp_drizzle_port == 0) || (temp_drizzle_port > 65535))
 
4750
    {
 
4751
      fprintf(stderr, _("Value supplied for port is not valid.\n"));
 
4752
      exit(1);
 
4753
    }
 
4754
    else
 
4755
    {
 
4756
      opt_port= (uint32_t) temp_drizzle_port;
 
4757
    }
 
4758
    break;
 
4759
  case 'P':
 
4760
    if (argument)
 
4761
    {
 
4762
      if (opt_pass)
 
4763
        free(opt_pass);
 
4764
      opt_pass = strdup(argument);
 
4765
      if (opt_pass == NULL)
 
4766
        die("Out of memory");
 
4767
      while (*argument)
 
4768
      {
 
4769
        /* Overwriting password with 'x' */
 
4770
        *argument++= 'x';
 
4771
      }
 
4772
      tty_password= 0;
 
4773
    }
 
4774
    else
 
4775
      tty_password= 1;
 
4776
    break;
 
4777
  case 't':
 
4778
    strncpy(TMPDIR, argument, sizeof(TMPDIR));
 
4779
    break;
 
4780
  case 'A':
 
4781
    if (!embedded_server_arg_count)
 
4782
    {
 
4783
      embedded_server_arg_count=1;
 
4784
      embedded_server_args[0]= (char*) "";
 
4785
    }
 
4786
    if (embedded_server_arg_count == MAX_EMBEDDED_SERVER_ARGS-1 ||
 
4787
        !(embedded_server_args[embedded_server_arg_count++]=
 
4788
          strdup(argument)))
 
4789
    {
 
4790
      die("Can't use server argument");
 
4791
    }
 
4792
    break;
 
4793
  case 'F':
 
4794
    read_embedded_server_arguments(argument);
 
4795
    break;
 
4796
  case 'V':
 
4797
    print_version();
 
4798
    exit(0);
 
4799
  case '?':
 
4800
    usage();
 
4801
    exit(0);
 
4802
  }
 
4803
  return 0;
 
4804
}
 
4805
 
 
4806
 
 
4807
static int parse_args(int argc, char **argv)
 
4808
{
 
4809
  load_defaults("drizzle",load_default_groups,&argc,&argv);
 
4810
  default_argv= argv;
 
4811
 
 
4812
  if ((handle_options(&argc, &argv, my_long_options, get_one_option)))
 
4813
    exit(1);
 
4814
 
 
4815
  if (argc > 1)
 
4816
  {
 
4817
    usage();
 
4818
    exit(1);
 
4819
  }
 
4820
  if (argc == 1)
 
4821
    opt_db= *argv;
 
4822
  if (tty_password)
 
4823
    opt_pass= client_get_tty_password(NULL);          /* purify tested */
 
4824
  if (debug_info_flag)
 
4825
    my_end_arg= MY_CHECK_ERROR | MY_GIVE_INFO;
 
4826
  if (debug_check_flag)
 
4827
    my_end_arg= MY_CHECK_ERROR;
 
4828
 
 
4829
  return 0;
 
4830
}
 
4831
 
4645
4832
/*
4646
4833
  Write the content of str into file
4647
4834
 
4658
4845
  int fd;
4659
4846
  char buff[FN_REFLEN];
4660
4847
  int flags= O_WRONLY | O_CREAT;
4661
 
  if (!internal::test_if_hard_path(fname))
 
4848
  if (!test_if_hard_path(fname))
4662
4849
  {
4663
 
    snprintf(buff, sizeof(buff), "%s%s",opt_basedir.c_str(),fname);
 
4850
    sprintf(buff,"%s%s",opt_basedir,fname);
4664
4851
    fname= buff;
4665
4852
  }
4666
 
  internal::fn_format(buff, fname, "", "", MY_UNPACK_FILENAME);
 
4853
  fn_format(buff, fname, "", "", MY_UNPACK_FILENAME);
4667
4854
 
4668
4855
  if (!append)
4669
4856
    flags|= O_TRUNC;
4670
 
  if ((fd= internal::my_open(buff, flags,
 
4857
  if ((fd= my_open(buff, flags,
4671
4858
                   MYF(MY_WME | MY_FFNF))) < 0)
4672
4859
    die("Could not open '%s' for writing: errno = %d", buff, errno);
4673
4860
  if (append && lseek(fd, 0, SEEK_END) == MY_FILEPOS_ERROR)
4674
4861
    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)))
 
4862
  if (my_write(fd, (unsigned char*)str, size, MYF(MY_WME|MY_FNABP)))
4676
4863
    die("write failed");
4677
 
  internal::my_close(fd, MYF(0));
 
4864
  my_close(fd, MYF(0));
4678
4865
}
4679
4866
 
4680
4867
/*
4696
4883
void dump_result_to_log_file(const char *buf, int size)
4697
4884
{
4698
4885
  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 :
 
4886
  str_to_file(fn_format(log_file, result_file_name, opt_logdir, ".log",
 
4887
                        *opt_logdir ? MY_REPLACE_DIR | MY_REPLACE_EXT :
4701
4888
                        MY_REPLACE_EXT),
4702
4889
              buf, size);
4703
4890
  fprintf(stderr, "\nMore results from queries before failure can be found in %s\n",
4707
4894
void dump_progress(void)
4708
4895
{
4709
4896
  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 :
 
4897
  str_to_file(fn_format(progress_file, result_file_name,
 
4898
                        opt_logdir, ".progress",
 
4899
                        *opt_logdir ? MY_REPLACE_DIR | MY_REPLACE_EXT :
4713
4900
                        MY_REPLACE_EXT),
4714
4901
              ds_progress.c_str(), ds_progress.length());
4715
4902
}
4718
4905
{
4719
4906
  char warn_file[FN_REFLEN];
4720
4907
 
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 :
 
4908
  str_to_file(fn_format(warn_file, result_file_name, opt_logdir, ".warnings",
 
4909
                        *opt_logdir ? MY_REPLACE_DIR | MY_REPLACE_EXT :
4723
4910
                        MY_REPLACE_EXT),
4724
4911
              ds_warning_messages.c_str(), ds_warning_messages.length());
4725
4912
}
5023
5210
      }
5024
5211
 
5025
5212
      /*
5026
 
        Need to call drizzle_result_affected_rows() before the "new"
 
5213
        Need to call drizzleclient_affected_rows() before the "new"
5027
5214
        query to find the warnings
5028
5215
      */
5029
5216
      if (!disable_info)
5417
5604
 
5418
5605
}
5419
5606
 
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
5607
 
5450
5608
int main(int argc, char **argv)
5451
5609
{
5452
 
try
5453
 
{
5454
5610
  struct st_command *command;
5455
5611
  bool q_send_flag= 0, abort_flag= 0;
5456
5612
  uint32_t command_executed= 0, last_command_executed= 0;
5457
5613
  string save_file("");
5458
5614
  struct stat res_info;
 
5615
  MY_INIT(argv[0]);
5459
5616
 
5460
5617
  TMPDIR[0]= 0;
5461
5618
 
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
5619
  /* Init expected errors */
5581
5620
  memset(&saved_expected_errors, 0, sizeof(saved_expected_errors));
5582
5621
 
5600
5639
  cur_block->ok= true; /* Outer block should always be executed */
5601
5640
  cur_block->cmd= cmd_none;
5602
5641
 
 
5642
  if (hash_init(&var_hash, charset_info,
 
5643
                1024, 0, 0, get_var_key, var_free, MYF(0)))
 
5644
    die("Variable hash initialization failed");
 
5645
 
5603
5646
  var_set_string("$DRIZZLE_SERVER_VERSION", drizzle_version());
5604
5647
 
5605
5648
  memset(&master_pos, 0, sizeof(master_pos));
5613
5656
  ds_progress.reserve(2048);
5614
5657
  ds_warning_messages.reserve(2048);
5615
5658
 
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
 
  }
 
5659
  parse_args(argc, argv);
5735
5660
 
5736
5661
  server_initialized= 1;
5737
5662
  if (cur_file == file_stack && cur_file->file == 0)
5747
5672
    die("Failed in drizzle_create()");
5748
5673
  if (!( drizzle_con_create(cur_con->drizzle, &cur_con->con)))
5749
5674
    die("Failed in drizzle_con_create()");
5750
 
  drizzle_con_add_options(&cur_con->con, use_drizzle_protocol ? DRIZZLE_CON_EXPERIMENTAL : DRIZZLE_CON_MYSQL);
5751
5675
 
5752
5676
  if (!(cur_con->name = strdup("default")))
5753
5677
    die("Out of memory");
 
5678
 
5754
5679
  safe_connect(&cur_con->con, cur_con->name, opt_host, opt_user, opt_pass,
5755
5680
               opt_db, opt_port);
5756
5681
 
5757
 
  fill_global_error_names();
5758
 
 
5759
5682
  /* Use all time until exit if no explicit 'start_timer' */
5760
5683
  timer_start= timer_now();
5761
5684
 
5769
5692
  /* Update $drizzleclient_get_server_version to that of current connection */
5770
5693
  var_set_drizzleclient_get_server_version(&cur_con->con);
5771
5694
 
5772
 
  if (! opt_include.empty())
 
5695
  if (opt_include)
5773
5696
  {
5774
 
    open_file(opt_include.c_str());
 
5697
    open_file(opt_include);
5775
5698
  }
5776
5699
 
5777
5700
  while (!read_command(&command) && !abort_flag)
6075
5998
  */
6076
5999
  if (ds_res.length())
6077
6000
  {
6078
 
    if (! result_file_name.empty())
 
6001
    if (result_file_name)
6079
6002
    {
6080
6003
      /* A result file has been specified */
6081
6004
 
6082
6005
      if (record)
6083
6006
      {
6084
6007
        /* 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());
 
6008
        str_to_file(result_file_name, ds_res.c_str(), ds_res.length());
6086
6009
      }
6087
6010
      else
6088
6011
      {
6105
6028
  }
6106
6029
 
6107
6030
  if (!command_executed &&
6108
 
      ! result_file_name.empty() && !stat(result_file_name.c_str(), &res_info))
 
6031
      result_file_name && !stat(result_file_name, &res_info))
6109
6032
  {
6110
6033
    /*
6111
6034
      my_stat() successful on result file. Check if we have not run a
6117
6040
    die("No queries executed but result file found!");
6118
6041
  }
6119
6042
 
6120
 
  if ( opt_mark_progress && ! result_file_name.empty() )
 
6043
  if ( opt_mark_progress && result_file_name )
6121
6044
    dump_progress();
6122
6045
 
6123
6046
  /* Dump warning messages */
6124
 
  if (! result_file_name.empty() && ds_warning_messages.length())
 
6047
  if (result_file_name && ds_warning_messages.length())
6125
6048
    dump_warning_messages();
6126
6049
 
6127
6050
  timer_output();
6128
6051
  /* Yes, if we got this far the test has suceeded! Sakila smiles */
6129
6052
  cleanup_and_exit(0);
6130
 
}
6131
 
 
6132
 
  catch(exception &err)
6133
 
  {
6134
 
    cerr<<err.what()<<endl;
6135
 
  }
6136
 
 
6137
6053
  return 0; /* Keep compiler happy too */
6138
6054
}
6139
6055
 
6175
6091
 
6176
6092
uint64_t timer_now(void)
6177
6093
{
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) */
 
6094
  return my_micro_time() / 1000;
6191
6095
}
6192
6096
 
6193
6097
 
6402
6306
  char* pattern; /* Pattern to be replaced */
6403
6307
  char* replace; /* String or expression to replace the pattern with */
6404
6308
  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
6309
};
6408
6310
 
6409
6311
struct st_replace_regex
6427
6329
struct st_replace_regex *glob_replace_regex= 0;
6428
6330
 
6429
6331
int reg_replace(char** buf_p, int* buf_len_p, char *pattern, char *replace,
6430
 
                char *string, int icase, int global);
 
6332
                char *string, int icase);
6431
6333
 
6432
6334
 
6433
6335
 
6528
6430
 
6529
6431
    /* Check if we should do matching case insensitive */
6530
6432
    if (p < expr_end && *p == 'i')
6531
 
    {
6532
 
      p++;
6533
6433
      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
6434
 
6543
6435
    /* done parsing the statement, now place it in regex_arr */
6544
6436
    if (insert_dynamic(&res->regex_arr,(unsigned char*) &reg))
6596
6488
    get_dynamic(&r->regex_arr,(unsigned char*)&re,i);
6597
6489
 
6598
6490
    if (!reg_replace(&out_buf, buf_len_p, re.pattern, re.replace,
6599
 
                     in_buf, re.icase, re.global))
 
6491
                     in_buf, re.icase))
6600
6492
    {
6601
6493
      /* if the buffer has been reallocated, make adjustements */
6602
6494
      if (save_out_buf != out_buf)
6666
6558
  icase - flag, if set to 1 the match is case insensitive
6667
6559
*/
6668
6560
int reg_replace(char** buf_p, int* buf_len_p, char *pattern,
6669
 
                char *replace, char *in_string, int icase, int global)
 
6561
                char *replace, char *in_string, int icase)
6670
6562
{
 
6563
  string string_to_match(in_string);
6671
6564
  const char *error= NULL;
6672
6565
  int erroffset;
6673
6566
  int ovector[3];
6674
6567
  pcre *re= pcre_compile(pattern,
6675
 
                         icase ? PCRE_CASELESS | PCRE_MULTILINE : PCRE_MULTILINE,
 
6568
                         icase ? PCRE_CASELESS : 0,
6676
6569
                         &error, &erroffset, NULL);
6677
6570
  if (re == NULL)
6678
6571
    return 1;
6679
6572
 
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
 
  }
 
6573
  int rc= pcre_exec(re, NULL, in_string, (int)strlen(in_string),
 
6574
                    0, 0, ovector, 3);
 
6575
  if (rc < 0)
 
6576
  {
 
6577
    pcre_free(re);
 
6578
    return 1;
 
6579
  }
 
6580
 
 
6581
  char *substring_to_replace= in_string + ovector[0];
 
6582
  int substring_length= ovector[1] - ovector[0];
 
6583
  *buf_len_p= strlen(in_string) - substring_length + strlen(replace);
 
6584
  char * new_buf = (char *)malloc(*buf_len_p+1);
 
6585
  if (new_buf == NULL)
 
6586
  {
 
6587
    pcre_free(re);
 
6588
    return 1;
 
6589
  }
 
6590
 
 
6591
  memset(new_buf, 0, *buf_len_p+1);
 
6592
  strncpy(new_buf, in_string, substring_to_replace-in_string);
 
6593
  strncpy(new_buf+(substring_to_replace-in_string), replace, strlen(replace));
 
6594
  strncpy(new_buf+(substring_to_replace-in_string)+strlen(replace),
 
6595
          substring_to_replace + substring_length,
 
6596
          strlen(in_string)
 
6597
            - substring_length
 
6598
            - (substring_to_replace-in_string));
 
6599
  *buf_p= new_buf;
 
6600
 
 
6601
  pcre_free(re);
 
6602
  return 0;
6752
6603
}
6753
6604
 
6754
6605
 
7332
7183
      return(1);
7333
7184
    if (new_pos != pa->str)
7334
7185
    {
7335
 
      ptrdiff_t diff= PTR_BYTE_DIFF(new_pos,pa->str);
 
7186
      my_ptrdiff_t diff=PTR_BYTE_DIFF(new_pos,pa->str);
7336
7187
      for (i=0 ; i < pa->typelib.count ; i++)
7337
7188
        pa->typelib.type_names[i]= ADD_TO_PTR(pa->typelib.type_names[i],diff,
7338
7189
                                              char*);