mysql_test_run_new.c 45 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
  Copyright (c) 2002, 2003 Novell, Inc. All Rights Reserved. 

  This program is free software; you can redistribute it and/or modify 
  it under the terms of the GNU General Public License as published by 
  the Free Software Foundation; either version 2 of the License, or 
  (at your option) any later version. 

  This program is distributed in the hope that it will be useful, 
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
  GNU General Public License for more details. 

  You should have received a copy of the GNU General Public License 
  along with this program; if not, write to the Free Software 
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/ 

#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#ifndef __WIN__
#include <dirent.h>
kent@mysql.com's avatar
kent@mysql.com committed
24
#endif
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
#include <string.h>
#ifdef __NETWARE__
#include <screen.h>
#include <nks/vm.h>
#endif
#include <ctype.h>
#include <sys/stat.h>
#ifndef __WIN__
#include <unistd.h>
#endif
#include <fcntl.h>
#ifdef __NETWARE__
#include <sys/mode.h>
#endif
#ifdef __WIN__
kent@mysql.com's avatar
kent@mysql.com committed
40 41
#include <windows.h>
#include <shlwapi.h>
42 43 44 45 46 47 48 49
#include <direct.h>
#endif

#include "my_manage.h"

/******************************************************************************

  macros
kent@mysql.com's avatar
kent@mysql.com committed
50

51 52 53 54 55
******************************************************************************/

#define HEADER  "TEST                                           RESULT  \n"
#define DASH    "-------------------------------------------------------\n"

kent@mysql.com's avatar
kent@mysql.com committed
56 57 58 59 60 61 62
#define NW_TEST_SUFFIX   ".nw-test"
#define NW_RESULT_SUFFIX ".nw-result"
#define TEST_SUFFIX      ".test"
#define RESULT_SUFFIX     ".result"
#define REJECT_SUFFIX    ".reject"
#define OUT_SUFFIX       ".out"
#define ERR_SUFFIX       ".err"
63

kent@mysql.com's avatar
kent@mysql.com committed
64 65 66 67 68
const char *TEST_PASS=   "[ pass ]";
const char *TEST_SKIP=   "[ skip ]";
const char *TEST_FAIL=   "[ fail ]";
const char *TEST_BAD=    "[ bad  ]";
const char *TEST_IGNORE= "[ignore]";
69 70 71 72

/******************************************************************************

  global variables
kent@mysql.com's avatar
kent@mysql.com committed
73

74
******************************************************************************/
kent@mysql.com's avatar
kent@mysql.com committed
75

76
#ifdef __NETWARE__
77
static char base_dir[FN_REFLEN]= "sys:/mysql";
78
#else
79
static char base_dir[FN_REFLEN]= "..";
80
#endif
81 82 83
static char db[FN_LEN]=       "test";
static char user[FN_LEN]=     "root";
static char password[FN_LEN]= "";
84

kent@mysql.com's avatar
kent@mysql.com committed
85 86
int master_port= 9306;
int slave_port=  9307;
87 88

#if !defined(__NETWARE__) && !defined(__WIN__)
89 90
static char master_socket[FN_REFLEN]= "./var/tmp/master.sock";
static char slave_socket[FN_REFLEN]=  "./var/tmp/slave.sock";
91 92
#endif

kent@mysql.com's avatar
kent@mysql.com committed
93
/* comma delimited list of tests to skip or empty string */
94
#ifndef __WIN__
95
static char skip_test[FN_REFLEN]= " lowercase_table3 , system_mysql_db_fix ";
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
#else
/*
  The most ignore testes contain the calls of system command
*/
#define MAX_COUNT_TESTES 1024
/*
  lowercase_table3 is disabled by Gerg
  system_mysql_db_fix  is disabled by Gerg
  sp contains a command system
  rpl_EE_error contains a command system
  rpl_loaddatalocal contains a command system
  ndb_autodiscover contains a command system
  rpl_rotate_logs contains a command system
  repair contains a command system
  rpl_trunc_binlog contains a command system
  mysqldump contains a command system
  rpl000001 makes non-exit loop...temporary skiped
*/
114
static char skip_test[FN_REFLEN]=
kent@mysql.com's avatar
kent@mysql.com committed
115 116 117 118 119 120 121 122 123 124 125
" lowercase_table3 ,"
" system_mysql_db_fix ,"
" sp ,"
" rpl_EE_error ,"
" rpl_loaddatalocal ,"
" ndb_autodiscover ,"
" rpl_rotate_logs ,"
" repair ,"
" rpl_trunc_binlog ,"
" mysqldump ,"
" rpl000001 ";
126
#endif
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
static char ignore_test[FN_REFLEN]= "";

static char bin_dir[FN_REFLEN];
static char mysql_test_dir[FN_REFLEN];
static char test_dir[FN_REFLEN];
static char mysql_tmp_dir[FN_REFLEN];
static char result_dir[FN_REFLEN];
static char master_dir[FN_REFLEN];
static char slave_dir[FN_REFLEN];
static char lang_dir[FN_REFLEN];
static char char_dir[FN_REFLEN];

static char mysqladmin_file[FN_REFLEN];
static char mysqld_file[FN_REFLEN];
static char mysqltest_file[FN_REFLEN];
142
#ifndef __WIN__
143 144 145
static char master_pid[FN_REFLEN];
static char slave_pid[FN_REFLEN];
static char sh_file[FN_REFLEN]= "/bin/sh";
146 147 148 149 150
#else
static HANDLE master_pid;
static HANDLE slave_pid;
#endif

151 152
static char master_opt[FN_REFLEN]= "";
static char slave_opt[FN_REFLEN]= "";
153

154
static char slave_master_info[FN_REFLEN]= "";
155

156 157
static char master_init_script[FN_REFLEN]= "";
static char slave_init_script[FN_REFLEN]= "";
158

kent@mysql.com's avatar
kent@mysql.com committed
159
/* OpenSSL */
160 161 162 163 164
static char ca_cert[FN_REFLEN];
static char server_cert[FN_REFLEN];
static char server_key[FN_REFLEN];
static char client_cert[FN_REFLEN];
static char client_key[FN_REFLEN];
165

kent@mysql.com's avatar
kent@mysql.com committed
166 167 168 169
int total_skip= 0;
int total_pass= 0;
int total_fail= 0;
int total_test= 0;
170

kent@mysql.com's avatar
kent@mysql.com committed
171
int total_ignore= 0;
172

kent@mysql.com's avatar
kent@mysql.com committed
173 174 175 176 177
int use_openssl=    FALSE;
int master_running= FALSE;
int slave_running=  FALSE;
int skip_slave=     TRUE;
int single_test=    TRUE;
178

kent@mysql.com's avatar
kent@mysql.com committed
179
int restarts= 0;
180

kent@mysql.com's avatar
kent@mysql.com committed
181
FILE *log_fd= NULL;
182 183 184 185

/******************************************************************************

  functions
kent@mysql.com's avatar
kent@mysql.com committed
186

187 188 189 190 191
******************************************************************************/

/******************************************************************************

  prototypes
kent@mysql.com's avatar
kent@mysql.com committed
192

193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
******************************************************************************/

void report_stats();
void install_db(char *);
void mysql_install_db();
void start_master();
void start_slave();
void mysql_start();
void stop_slave();
void stop_master();
void mysql_stop();
void mysql_restart();
int read_option(char *, char *);
void run_test(char *);
void setup(char *);
void vlog(const char *, va_list);
void mlog(const char *, ...);
void log_info(const char *, ...);
void log_error(const char *, ...);
void log_errno(const char *, ...);
void die(const char *);
char *str_tok(char *string, const char *delim);
#ifndef __WIN__
void run_init_script(const char *script_name);
#endif
/******************************************************************************

  report_stats()
kent@mysql.com's avatar
kent@mysql.com committed
221

222 223 224
  Report the gathered statistics.

******************************************************************************/
kent@mysql.com's avatar
kent@mysql.com committed
225

226 227 228 229 230 231 232 233
void report_stats()
{
  if (total_fail == 0)
  {
    mlog("\nAll %d test(s) were successful.\n", total_test);
  }
  else
  {
kent@mysql.com's avatar
kent@mysql.com committed
234 235
    double percent= ((double)total_pass / total_test) * 100;

236
    mlog("\nFailed %u/%u test(s), %.02f%% successful.\n",
kent@mysql.com's avatar
kent@mysql.com committed
237 238 239 240 241 242
         total_fail, total_test, percent);
    mlog("\nThe .out and .err files in %s may give you some\n", result_dir);
    mlog("hint of what when wrong.\n");
    mlog("\nIf you want to report this error, please first read "
         "the documentation\n");
    mlog("at: http://www.mysql.com/doc/M/y/MySQL_test_suite.html\n");
243 244 245 246 247 248
  }
}

/******************************************************************************

  install_db()
kent@mysql.com's avatar
kent@mysql.com committed
249

250 251 252
  Install the a database.

******************************************************************************/
kent@mysql.com's avatar
kent@mysql.com committed
253

254 255 256 257
void install_db(char *datadir)
{
  arg_list_t al;
  int err;
258 259 260
  char input[FN_REFLEN];
  char output[FN_REFLEN];
  char error[FN_REFLEN];
261

kent@mysql.com's avatar
kent@mysql.com committed
262 263
  /* input file */
#ifdef __NETWARE__
264
  snprintf(input, FN_REFLEN, "%s/bin/init_db.sql", base_dir);
265
#else
266
  snprintf(input, FN_REFLEN, "%s/mysql-test/init_db.sql", base_dir);
kent@mysql.com's avatar
kent@mysql.com committed
267
#endif
268 269
  snprintf(output, FN_REFLEN, "%s/install.out", datadir);
  snprintf(error, FN_REFLEN, "%s/install.err", datadir);
kent@mysql.com's avatar
kent@mysql.com committed
270

wax@kishkin.ru's avatar
wax@kishkin.ru committed
271 272
  if (create_system_files(datadir,input, TRUE))
    die("Unable to create init_db.sql.");
kent@mysql.com's avatar
kent@mysql.com committed
273
  /* args */
274 275 276 277 278 279 280 281 282 283 284 285
  init_args(&al);
  add_arg(&al, mysqld_file);
  add_arg(&al, "--no-defaults");
  add_arg(&al, "--bootstrap");
  add_arg(&al, "--skip-grant-tables");
  add_arg(&al, "--basedir=%s", base_dir);
  add_arg(&al, "--datadir=%s", datadir);
  add_arg(&al, "--skip-innodb");
  add_arg(&al, "--skip-bdb");
#ifndef __NETWARE__
  add_arg(&al, "--character-sets-dir=%s", char_dir);
  add_arg(&al, "--language=%s", lang_dir);
kent@mysql.com's avatar
kent@mysql.com committed
286
#endif
287

kent@mysql.com's avatar
kent@mysql.com committed
288 289
  /* spawn */
  if ((err= spawn(mysqld_file, &al, TRUE, input, output, error, NULL)) != 0)
290 291 292
  {
    die("Unable to create database.");
  }
kent@mysql.com's avatar
kent@mysql.com committed
293 294

  /* free args */
295 296 297 298 299 300
  free_args(&al);
}

/******************************************************************************

  mysql_install_db()
kent@mysql.com's avatar
kent@mysql.com committed
301

302 303 304
  Install the test databases.

******************************************************************************/
kent@mysql.com's avatar
kent@mysql.com committed
305

306 307
void mysql_install_db()
{
308
  char temp[FN_REFLEN];
kent@mysql.com's avatar
kent@mysql.com committed
309 310

  /* var directory */
311
  snprintf(temp, FN_REFLEN, "%s/var", mysql_test_dir);
kent@mysql.com's avatar
kent@mysql.com committed
312 313

  /* create var directory */
314 315
#ifndef __WIN__
  mkdir(temp, S_IRWXU);
kent@mysql.com's avatar
kent@mysql.com committed
316
  /* create subdirectories */
317
  mlog("Creating test-suite folders...\n");
318
  snprintf(temp, FN_REFLEN, "%s/var/run", mysql_test_dir);
319
  mkdir(temp, S_IRWXU);
320
  snprintf(temp, FN_REFLEN, "%s/var/tmp", mysql_test_dir);
321
  mkdir(temp, S_IRWXU);
322
  snprintf(temp, FN_REFLEN, "%s/var/master-data", mysql_test_dir);
323
  mkdir(temp, S_IRWXU);
324
  snprintf(temp, FN_REFLEN, "%s/var/master-data/mysql", mysql_test_dir);
325
  mkdir(temp, S_IRWXU);
326
  snprintf(temp, FN_REFLEN, "%s/var/master-data/test", mysql_test_dir);
327
  mkdir(temp, S_IRWXU);
328
  snprintf(temp, FN_REFLEN, "%s/var/slave-data", mysql_test_dir);
329
  mkdir(temp, S_IRWXU);
330
  snprintf(temp, FN_REFLEN, "%s/var/slave-data/mysql", mysql_test_dir);
331
  mkdir(temp, S_IRWXU);
332
  snprintf(temp, FN_REFLEN, "%s/var/slave-data/test", mysql_test_dir);
333 334 335
  mkdir(temp, S_IRWXU);
#else
  mkdir(temp);
kent@mysql.com's avatar
kent@mysql.com committed
336
  /* create subdirectories */
337
  mlog("Creating test-suite folders...\n");
338
  snprintf(temp, FN_REFLEN, "%s/var/run", mysql_test_dir);
339
  mkdir(temp);
340
  snprintf(temp, FN_REFLEN, "%s/var/tmp", mysql_test_dir);
341
  mkdir(temp);
342
  snprintf(temp, FN_REFLEN, "%s/var/master-data", mysql_test_dir);
343
  mkdir(temp);
344
  snprintf(temp, FN_REFLEN, "%s/var/master-data/mysql", mysql_test_dir);
345
  mkdir(temp);
346
  snprintf(temp, FN_REFLEN, "%s/var/master-data/test", mysql_test_dir);
347
  mkdir(temp);
348
  snprintf(temp, FN_REFLEN, "%s/var/slave-data", mysql_test_dir);
349
  mkdir(temp);
350
  snprintf(temp, FN_REFLEN, "%s/var/slave-data/mysql", mysql_test_dir);
351
  mkdir(temp);
352
  snprintf(temp, FN_REFLEN, "%s/var/slave-data/test", mysql_test_dir);
353 354 355
  mkdir(temp);
#endif

kent@mysql.com's avatar
kent@mysql.com committed
356
  /* install databases */
357 358 359 360 361 362 363 364 365
  mlog("Creating test databases for master... \n");
  install_db(master_dir);
  mlog("Creating test databases for slave... \n");
  install_db(slave_dir);
}

/******************************************************************************

  start_master()
kent@mysql.com's avatar
kent@mysql.com committed
366

367 368 369
  Start the master server.

******************************************************************************/
kent@mysql.com's avatar
kent@mysql.com committed
370

371 372 373 374
void start_master()
{
  arg_list_t al;
  int err;
375 376 377 378
  char master_out[FN_REFLEN];
  char master_err[FN_REFLEN];
/*  char temp[FN_REFLEN]; */
  char temp2[FN_REFLEN];
379

kent@mysql.com's avatar
kent@mysql.com committed
380
  /* remove old berkeley db log files that can confuse the server */
381 382
  removef("%s/log.*", master_dir);

kent@mysql.com's avatar
kent@mysql.com committed
383
  /* remove stale binary logs */
384 385
  removef("%s/var/log/*-bin.*", mysql_test_dir);

kent@mysql.com's avatar
kent@mysql.com committed
386
  /* remove stale binary logs */
387 388
  removef("%s/var/log/*.index", mysql_test_dir);

kent@mysql.com's avatar
kent@mysql.com committed
389
  /* remove master.info file */
390 391
  removef("%s/master.info", master_dir);

kent@mysql.com's avatar
kent@mysql.com committed
392
  /* remove relay files */
393 394
  removef("%s/var/log/*relay*", mysql_test_dir);

kent@mysql.com's avatar
kent@mysql.com committed
395
  /* remove relay-log.info file */
396 397
  removef("%s/relay-log.info", master_dir);

kent@mysql.com's avatar
kent@mysql.com committed
398
  /* init script */
399 400 401
  if (master_init_script[0] != 0)
  {
#ifdef __NETWARE__
kent@mysql.com's avatar
kent@mysql.com committed
402
    /* TODO: use the scripts */
403 404 405 406
    if (strinstr(master_init_script, "repair_part2-master.sh") != 0)
    {
      FILE *fp;

kent@mysql.com's avatar
kent@mysql.com committed
407
      /* create an empty index file */
408
      snprintf(temp, FN_REFLEN, "%s/test/t1.MYI", master_dir);
kent@mysql.com's avatar
kent@mysql.com committed
409
      fp= fopen(temp, "wb+");
410 411 412 413 414 415 416

      fputs("1", fp);

      fclose(fp);
    }
#elif !defined(__WIN__)
    run_init_script(master_init_script);
kent@mysql.com's avatar
kent@mysql.com committed
417
#endif
418 419
  }

kent@mysql.com's avatar
kent@mysql.com committed
420
  /* redirection files */
421
  snprintf(master_out, FN_REFLEN, "%s/var/run/master%u.out",
422
           mysql_test_dir, restarts);
423
  snprintf(master_err, FN_REFLEN, "%s/var/run/master%u.err",
424 425
           mysql_test_dir, restarts);
#ifndef __WIN__
426
  snprintf(temp2,FN_REFLEN,"%s/var",mysql_test_dir);
427
  mkdir(temp2,S_IRWXU);
428
  snprintf(temp2,FN_REFLEN,"%s/var/log",mysql_test_dir);
429 430
  mkdir(temp2,S_IRWXU);
#else
431
  snprintf(temp2,FN_REFLEN,"%s/var",mysql_test_dir);
432
  mkdir(temp2);
433
  snprintf(temp2,FN_REFLEN,"%s/var/log",mysql_test_dir);
434 435
  mkdir(temp2);
#endif
kent@mysql.com's avatar
kent@mysql.com committed
436
  /* args */
437 438 439 440 441 442 443 444 445
  init_args(&al);
  add_arg(&al, "%s", mysqld_file);
  add_arg(&al, "--no-defaults");
  add_arg(&al, "--log-bin=%s/var/log/master-bin",mysql_test_dir);
  add_arg(&al, "--server-id=1");
  add_arg(&al, "--basedir=%s", base_dir);
  add_arg(&al, "--port=%u", master_port);
#if !defined(__NETWARE__) && !defined(__WIN__)
  add_arg(&al, "--socket=%s",master_socket);
kent@mysql.com's avatar
kent@mysql.com committed
446
#endif
447 448 449 450 451 452 453 454 455
  add_arg(&al, "--local-infile");
  add_arg(&al, "--core");
  add_arg(&al, "--datadir=%s", master_dir);
#ifndef __WIN__
  add_arg(&al, "--pid-file=%s", master_pid);
#endif
  add_arg(&al, "--character-sets-dir=%s", char_dir);
  add_arg(&al, "--tmpdir=%s", mysql_tmp_dir);
  add_arg(&al, "--language=%s", lang_dir);
kent@mysql.com's avatar
kent@mysql.com committed
456
#ifdef DEBUG /* only for debug builds */
457 458 459 460 461 462 463 464 465 466
  add_arg(&al, "--debug");
#endif

  if (use_openssl)
  {
    add_arg(&al, "--ssl-ca=%s", ca_cert);
    add_arg(&al, "--ssl-cert=%s", server_cert);
    add_arg(&al, "--ssl-key=%s", server_key);
  }

kent@mysql.com's avatar
kent@mysql.com committed
467
  /* $MASTER_40_ARGS */
468 469 470
  add_arg(&al, "--rpl-recovery-rank=1");
  add_arg(&al, "--init-rpl-role=master");

kent@mysql.com's avatar
kent@mysql.com committed
471
  /* $SMALL_SERVER */
472 473 474 475 476 477 478
  add_arg(&al, "-O");
  add_arg(&al, "key_buffer_size=1M");
  add_arg(&al, "-O");
  add_arg(&al, "sort_buffer=256K");
  add_arg(&al, "-O");
  add_arg(&al, "max_heap_table_size=1M");

kent@mysql.com's avatar
kent@mysql.com committed
479
  /* $EXTRA_MASTER_OPT */
480 481 482 483
  if (master_opt[0] != 0)
  {
    char *p;

kent@mysql.com's avatar
kent@mysql.com committed
484
    p= (char *)str_tok(master_opt, " \t");
485 486 487 488 489
    if (!strstr(master_opt, "timezone"))
    {
      while (p)
      {
        add_arg(&al, "%s", p);
kent@mysql.com's avatar
kent@mysql.com committed
490
        p= (char *)str_tok(NULL, " \t");
491 492 493 494
      }
    }
  }

kent@mysql.com's avatar
kent@mysql.com committed
495
  /* remove the pid file if it exists */
496 497 498 499
#ifndef __WIN__
  remove(master_pid);
#endif

kent@mysql.com's avatar
kent@mysql.com committed
500
  /* spawn */
501
#ifdef __WIN__
kent@mysql.com's avatar
kent@mysql.com committed
502 503
  if ((err= spawn(mysqld_file, &al, FALSE, NULL,
                  master_out, master_err, &master_pid)) == 0)
504
#else
kent@mysql.com's avatar
kent@mysql.com committed
505 506 507
  if ((err= spawn(mysqld_file, &al, FALSE, NULL,
                  master_out, master_err, master_pid)) == 0)
#endif
508 509 510
  {
    sleep_until_file_exists(master_pid);

kent@mysql.com's avatar
kent@mysql.com committed
511 512
    if ((err= wait_for_server_start(bin_dir, mysqladmin_file, user, password,
                                    master_port, mysql_tmp_dir)) == 0)
513
    {
kent@mysql.com's avatar
kent@mysql.com committed
514
      master_running= TRUE;
515 516 517
    }
    else
    {
kent@mysql.com's avatar
kent@mysql.com committed
518
      log_error("The master server went down early.");
519 520 521 522 523 524 525
    }
  }
  else
  {
    log_error("Unable to start master server.");
  }

kent@mysql.com's avatar
kent@mysql.com committed
526
  /* free_args */
527 528 529 530 531 532
  free_args(&al);
}

/******************************************************************************

  start_slave()
kent@mysql.com's avatar
kent@mysql.com committed
533

534 535 536
  Start the slave server.

******************************************************************************/
kent@mysql.com's avatar
kent@mysql.com committed
537

538 539 540 541
void start_slave()
{
  arg_list_t al;
  int err;
542 543
  char slave_out[FN_REFLEN];
  char slave_err[FN_REFLEN];
544

kent@mysql.com's avatar
kent@mysql.com committed
545
  /* skip? */
546 547
  if (skip_slave) return;

kent@mysql.com's avatar
kent@mysql.com committed
548
  /* remove stale binary logs */
549 550
  removef("%s/*-bin.*", slave_dir);

kent@mysql.com's avatar
kent@mysql.com committed
551
  /* remove stale binary logs */
552 553
  removef("%s/*.index", slave_dir);

kent@mysql.com's avatar
kent@mysql.com committed
554
  /* remove master.info file */
555 556
  removef("%s/master.info", slave_dir);

kent@mysql.com's avatar
kent@mysql.com committed
557
  /* remove relay files */
558 559
  removef("%s/var/log/*relay*", mysql_test_dir);

kent@mysql.com's avatar
kent@mysql.com committed
560
  /* remove relay-log.info file */
561 562
  removef("%s/relay-log.info", slave_dir);

kent@mysql.com's avatar
kent@mysql.com committed
563
  /* init script */
564 565 566
  if (slave_init_script[0] != 0)
  {
#ifdef __NETWARE__
kent@mysql.com's avatar
kent@mysql.com committed
567
    /* TODO: use the scripts */
568 569
    if (strinstr(slave_init_script, "rpl000016-slave.sh") != 0)
    {
kent@mysql.com's avatar
kent@mysql.com committed
570
      /* create empty master.info file */
571
      snprintf(temp, FN_REFLEN, "%s/master.info", slave_dir);
572 573 574 575 576
      close(open(temp, O_WRONLY | O_CREAT,S_IRWXU|S_IRWXG|S_IRWXO));
    }
    else if (strinstr(slave_init_script, "rpl000017-slave.sh") != 0)
    {
      FILE *fp;
kent@mysql.com's avatar
kent@mysql.com committed
577 578

      /* create a master.info file */
579
      snprintf(temp, FN_REFLEN, "%s/master.info", slave_dir);
kent@mysql.com's avatar
kent@mysql.com committed
580 581
      fp= fopen(temp, "wb+");

582 583 584 585 586 587 588 589 590 591 592 593 594
      fputs("master-bin.000001\n", fp);
      fputs("4\n", fp);
      fputs("127.0.0.1\n", fp);
      fputs("replicate\n", fp);
      fputs("aaaaaaaaaaaaaaab\n", fp);
      fputs("9306\n", fp);
      fputs("1\n", fp);
      fputs("0\n", fp);

      fclose(fp);
    }
    else if (strinstr(slave_init_script, "rpl_rotate_logs-slave.sh") != 0)
    {
kent@mysql.com's avatar
kent@mysql.com committed
595
      /* create empty master.info file */
596
      snprintf(temp, FN_REFLEN, "%s/master.info", slave_dir);
597 598
      close(open(temp, O_WRONLY | O_CREAT,S_IRWXU|S_IRWXG|S_IRWXO));
    }
kent@mysql.com's avatar
kent@mysql.com committed
599
#elif !defined(__WIN__)
600
    run_init_script(slave_init_script);
kent@mysql.com's avatar
kent@mysql.com committed
601
#endif
602 603
  }

kent@mysql.com's avatar
kent@mysql.com committed
604
  /* redirection files */
605
  snprintf(slave_out, FN_REFLEN, "%s/var/run/slave%u.out",
606
           mysql_test_dir, restarts);
607
  snprintf(slave_err, FN_REFLEN, "%s/var/run/slave%u.err",
608
           mysql_test_dir, restarts);
kent@mysql.com's avatar
kent@mysql.com committed
609 610

  /* args */
611 612 613 614 615 616 617 618 619
  init_args(&al);
  add_arg(&al, "%s", mysqld_file);
  add_arg(&al, "--no-defaults");
  add_arg(&al, "--log-bin=slave-bin");
  add_arg(&al, "--relay_log=slave-relay-bin");
  add_arg(&al, "--basedir=%s", base_dir);
  add_arg(&al, "--port=%u", slave_port);
#if !defined(__NETWARE__) && !defined(__WIN__)
  add_arg(&al, "--socket=%s",slave_socket);
kent@mysql.com's avatar
kent@mysql.com committed
620
#endif
621 622 623 624 625 626 627 628 629 630 631 632 633 634 635
  add_arg(&al, "--datadir=%s", slave_dir);
#ifndef __WIN__
  add_arg(&al, "--pid-file=%s", slave_pid);
#endif
  add_arg(&al, "--character-sets-dir=%s", char_dir);
  add_arg(&al, "--core");
  add_arg(&al, "--tmpdir=%s", mysql_tmp_dir);
  add_arg(&al, "--language=%s", lang_dir);

  add_arg(&al, "--exit-info=256");
  add_arg(&al, "--log-slave-updates");
  add_arg(&al, "--init-rpl-role=slave");
  add_arg(&al, "--skip-innodb");
  add_arg(&al, "--skip-slave-start");
  add_arg(&al, "--slave-load-tmpdir=../../var/tmp");
kent@mysql.com's avatar
kent@mysql.com committed
636

637 638 639 640 641 642 643
  add_arg(&al, "--report-user=%s", user);
  add_arg(&al, "--report-host=127.0.0.1");
  add_arg(&al, "--report-port=%u", slave_port);

  add_arg(&al, "--master-retry-count=10");
  add_arg(&al, "-O");
  add_arg(&al, "slave_net_timeout=10");
kent@mysql.com's avatar
kent@mysql.com committed
644
#ifdef DEBUG /* only for debug builds */
645 646 647 648 649 650 651 652 653 654
  add_arg(&al, "--debug");
#endif

  if (use_openssl)
  {
    add_arg(&al, "--ssl-ca=%s", ca_cert);
    add_arg(&al, "--ssl-cert=%s", server_cert);
    add_arg(&al, "--ssl-key=%s", server_key);
  }

kent@mysql.com's avatar
kent@mysql.com committed
655
  /* slave master info */
656 657 658 659
  if (slave_master_info[0] != 0)
  {
    char *p;

kent@mysql.com's avatar
kent@mysql.com committed
660
    p= (char *)str_tok(slave_master_info, " \t");
661

kent@mysql.com's avatar
kent@mysql.com committed
662
    while (p)
663 664
    {
      add_arg(&al, "%s", p);
kent@mysql.com's avatar
kent@mysql.com committed
665
      p= (char *)str_tok(NULL, " \t");
666 667 668 669 670 671 672 673 674 675 676 677
    }
  }
  else
  {
    add_arg(&al, "--master-user=%s", user);
    add_arg(&al, "--master-password=%s", password);
    add_arg(&al, "--master-host=127.0.0.1");
    add_arg(&al, "--master-port=%u", master_port);
    add_arg(&al, "--master-connect-retry=1");
    add_arg(&al, "--server-id=2");
    add_arg(&al, "--rpl-recovery-rank=2");
  }
kent@mysql.com's avatar
kent@mysql.com committed
678 679

  /* small server */
680 681 682 683 684 685 686 687
  add_arg(&al, "-O");
  add_arg(&al, "key_buffer_size=1M");
  add_arg(&al, "-O");
  add_arg(&al, "sort_buffer=256K");
  add_arg(&al, "-O");
  add_arg(&al, "max_heap_table_size=1M");


kent@mysql.com's avatar
kent@mysql.com committed
688
  /* opt args */
689 690 691 692
  if (slave_opt[0] != 0)
  {
    char *p;

kent@mysql.com's avatar
kent@mysql.com committed
693
    p= (char *)str_tok(slave_opt, " \t");
694

kent@mysql.com's avatar
kent@mysql.com committed
695
    while (p)
696 697
    {
      add_arg(&al, "%s", p);
kent@mysql.com's avatar
kent@mysql.com committed
698
      p= (char *)str_tok(NULL, " \t");
699 700
    }
  }
kent@mysql.com's avatar
kent@mysql.com committed
701 702

  /* remove the pid file if it exists */
703 704 705
#ifndef __WIN__
  remove(slave_pid);
#endif
kent@mysql.com's avatar
kent@mysql.com committed
706 707 708 709
  /* spawn */
#ifdef __WIN__
  if ((err= spawn(mysqld_file, &al, FALSE, NULL,
                  slave_out, slave_err, &slave_pid)) == 0)
710
#else
kent@mysql.com's avatar
kent@mysql.com committed
711 712 713
  if ((err= spawn(mysqld_file, &al, FALSE, NULL,
                  slave_out, slave_err, slave_pid)) == 0)
#endif
714 715
  {
    sleep_until_file_exists(slave_pid);
kent@mysql.com's avatar
kent@mysql.com committed
716 717 718

    if ((err= wait_for_server_start(bin_dir, mysqladmin_file, user, password,
                                    slave_port, mysql_tmp_dir)) == 0)
719
    {
kent@mysql.com's avatar
kent@mysql.com committed
720
      slave_running= TRUE;
721 722 723 724 725 726 727 728 729 730
    }
    else
    {
      log_error("The slave server went down early.");
    }
  }
  else
  {
    log_error("Unable to start slave server.");
  }
kent@mysql.com's avatar
kent@mysql.com committed
731 732

  /* free args */
733 734 735 736 737 738
  free_args(&al);
}

/******************************************************************************

  mysql_start()
kent@mysql.com's avatar
kent@mysql.com committed
739

740 741 742
  Start the mysql servers.

******************************************************************************/
kent@mysql.com's avatar
kent@mysql.com committed
743

744 745
void mysql_start()
{
kent@mysql.com's avatar
kent@mysql.com committed
746
/*  log_info("Starting the MySQL server(s): %u", ++restarts); */
747 748 749 750
  start_master();

  start_slave();

kent@mysql.com's avatar
kent@mysql.com committed
751 752
  /* activate the test screen */
#ifdef __NETWARE__
753
  ActivateScreen(getscreenhandle());
kent@mysql.com's avatar
kent@mysql.com committed
754
#endif
755 756 757 758 759 760 761 762 763
}

/******************************************************************************

  stop_slave()

  Stop the slave server.

******************************************************************************/
kent@mysql.com's avatar
kent@mysql.com committed
764

765 766 767 768
void stop_slave()
{
  int err;

kent@mysql.com's avatar
kent@mysql.com committed
769
  /* running? */
770 771
  if (!slave_running) return;

kent@mysql.com's avatar
kent@mysql.com committed
772 773 774
  /* stop */
  if ((err= stop_server(bin_dir, mysqladmin_file, user, password,
                        slave_port, slave_pid, mysql_tmp_dir)) == 0)
775
  {
kent@mysql.com's avatar
kent@mysql.com committed
776
    slave_running= FALSE;
777 778 779 780 781 782 783 784 785 786 787 788 789 790
  }
  else
  {
    log_error("Unable to stop slave server.");
  }
}

/******************************************************************************

  stop_master()

  Stop the master server.

******************************************************************************/
kent@mysql.com's avatar
kent@mysql.com committed
791

792 793 794 795
void stop_master()
{
  int err;

kent@mysql.com's avatar
kent@mysql.com committed
796
  /* running? */
797 798
  if (!master_running) return;

kent@mysql.com's avatar
kent@mysql.com committed
799 800
  if ((err= stop_server(bin_dir, mysqladmin_file, user, password,
                        master_port, master_pid, mysql_tmp_dir)) == 0)
801
  {
kent@mysql.com's avatar
kent@mysql.com committed
802
    master_running= FALSE;
803 804 805 806 807 808 809 810 811 812 813 814 815 816
  }
  else
  {
    log_error("Unable to stop master server.");
  }
}

/******************************************************************************

  mysql_stop()

  Stop the mysql servers.

******************************************************************************/
kent@mysql.com's avatar
kent@mysql.com committed
817

818 819 820 821 822 823 824
void mysql_stop()
{

  stop_master();

  stop_slave();

kent@mysql.com's avatar
kent@mysql.com committed
825 826
  /* activate the test screen */
#ifdef __NETWARE__
827
  ActivateScreen(getscreenhandle());
kent@mysql.com's avatar
kent@mysql.com committed
828
#endif
829 830 831 832 833 834 835 836 837
}

/******************************************************************************

  mysql_restart()

  Restart the mysql servers.

******************************************************************************/
kent@mysql.com's avatar
kent@mysql.com committed
838

839 840
void mysql_restart()
{
kent@mysql.com's avatar
kent@mysql.com committed
841
/*  log_info("Restarting the MySQL server(s): %u", ++restarts); */
842 843 844 845 846 847 848 849 850 851 852 853 854 855 856

  mysql_stop();

  mlog(DASH);

  mysql_start();
}

/******************************************************************************

  read_option()

  Read the option file.

******************************************************************************/
kent@mysql.com's avatar
kent@mysql.com committed
857

858 859 860 861
int read_option(char *opt_file, char *opt)
{
  int fd, err;
  char *p;
862
  char buf[FN_REFLEN];
863

kent@mysql.com's avatar
kent@mysql.com committed
864
  /* copy current option */
865
  strncpy(buf, opt, FN_REFLEN);
866

kent@mysql.com's avatar
kent@mysql.com committed
867 868
  /* open options file */
  fd= open(opt_file, O_RDONLY);
869
  err= read(fd, opt, FN_REFLEN);
870
  close(fd);
kent@mysql.com's avatar
kent@mysql.com committed
871

872 873
  if (err > 0)
  {
kent@mysql.com's avatar
kent@mysql.com committed
874 875
    /* terminate string */
    if ((p= strchr(opt, '\n')) != NULL)
876
    {
kent@mysql.com's avatar
kent@mysql.com committed
877 878 879 880
      *p= 0;

      /* check for a '\r' */
      if ((p= strchr(opt, '\r')) != NULL)
881
      {
kent@mysql.com's avatar
kent@mysql.com committed
882
        *p= 0;
883 884 885 886
      }
    }
    else
    {
kent@mysql.com's avatar
kent@mysql.com committed
887
      opt[err]= 0;
888 889
    }

kent@mysql.com's avatar
kent@mysql.com committed
890 891
    /* check for $MYSQL_TEST_DIR */
    if ((p= strstr(opt, "$MYSQL_TEST_DIR")) != NULL)
892
    {
893
      char temp[FN_REFLEN];
kent@mysql.com's avatar
kent@mysql.com committed
894 895 896

      *p= 0;

897 898 899 900
      strcpy(temp, p + strlen("$MYSQL_TEST_DIR"));
      strcat(opt, mysql_test_dir);
      strcat(opt, temp);
    }
kent@mysql.com's avatar
kent@mysql.com committed
901 902
    /* Check for double backslash and replace it with single bakslash */
    if ((p= strstr(opt, "\\\\")) != NULL)
903 904 905 906 907 908 909
    {
      /* bmove is guranteed to work byte by byte */
      bmove(p, p+1, strlen(p+1));
    }
  }
  else
  {
kent@mysql.com's avatar
kent@mysql.com committed
910 911
    /* clear option */
    *opt= 0;
912
  }
kent@mysql.com's avatar
kent@mysql.com committed
913 914

  /* compare current option with previous */
915 916 917 918 919 920
  return strcmp(opt, buf);
}

/******************************************************************************

  run_test()
kent@mysql.com's avatar
kent@mysql.com committed
921

922 923 924
  Run the given test case.

******************************************************************************/
kent@mysql.com's avatar
kent@mysql.com committed
925

926 927
void run_test(char *test)
{
928
  char temp[FN_REFLEN];
929
  const char *rstr;
kent@mysql.com's avatar
kent@mysql.com committed
930 931 932
  int skip= FALSE, ignore=FALSE;
  int restart= FALSE;
  int flag= FALSE;
933
  struct stat info;
kent@mysql.com's avatar
kent@mysql.com committed
934 935

  /* skip tests in the skip list */
936
  snprintf(temp, FN_REFLEN, " %s ", test);
kent@mysql.com's avatar
kent@mysql.com committed
937
  skip= (strinstr(skip_test, temp) != 0);
938
  if (skip == FALSE)
kent@mysql.com's avatar
kent@mysql.com committed
939
    ignore= (strinstr(ignore_test, temp) != 0);
940

941 942
  snprintf(master_init_script, FN_REFLEN, "%s/%s-master.sh", test_dir, test);
  snprintf(slave_init_script, FN_REFLEN, "%s/%s-slave.sh", test_dir, test);
kent@mysql.com's avatar
kent@mysql.com committed
943
#ifdef __WIN__
944
  if (! stat(master_init_script, &info))
kent@mysql.com's avatar
kent@mysql.com committed
945
      skip= TRUE;
946
  if (!stat(slave_init_script, &info))
kent@mysql.com's avatar
kent@mysql.com committed
947
      skip= TRUE;
948 949 950
#endif
  if (ignore)
  {
kent@mysql.com's avatar
kent@mysql.com committed
951
    /* show test */
952
    mlog("%-46s ", test);
kent@mysql.com's avatar
kent@mysql.com committed
953 954 955

    /* ignore */
    rstr= TEST_IGNORE;
956
    ++total_ignore;
kent@mysql.com's avatar
kent@mysql.com committed
957 958
  }
  else if (!skip)     /* skip test? */
959
  {
960 961 962 963 964 965 966 967
    char test_file[FN_REFLEN];
    char master_opt_file[FN_REFLEN];
    char slave_opt_file[FN_REFLEN];
    char slave_master_info_file[FN_REFLEN];
    char result_file[FN_REFLEN];
    char reject_file[FN_REFLEN];
    char out_file[FN_REFLEN];
    char err_file[FN_REFLEN];
968 969 970
    int err;
    arg_list_t al;
#ifdef __WIN__
kent@mysql.com's avatar
kent@mysql.com committed
971
    /* Clean test database */
972 973 974 975 976 977
    removef("%s/test/*.*", master_dir);
    removef("%s/test/*.*", slave_dir);
    removef("%s/mysqltest/*.*", master_dir);
    removef("%s/mysqltest/*.*", slave_dir);

#endif
kent@mysql.com's avatar
kent@mysql.com committed
978 979 980 981 982 983
    /* skip slave? */
    flag= skip_slave;
    skip_slave= (strncmp(test, "rpl", 3) != 0);
    if (flag != skip_slave) restart= TRUE;

    /* create files */
984 985 986
    snprintf(master_opt_file, FN_REFLEN, "%s/%s-master.opt", test_dir, test);
    snprintf(slave_opt_file, FN_REFLEN, "%s/%s-slave.opt", test_dir, test);
    snprintf(slave_master_info_file, FN_REFLEN, "%s/%s.slave-mi",
kent@mysql.com's avatar
kent@mysql.com committed
987
             test_dir, test);
988
    snprintf(reject_file, FN_REFLEN, "%s/%s%s",
kent@mysql.com's avatar
kent@mysql.com committed
989
             result_dir, test, REJECT_SUFFIX);
990 991
    snprintf(out_file, FN_REFLEN, "%s/%s%s", result_dir, test, OUT_SUFFIX);
    snprintf(err_file, FN_REFLEN, "%s/%s%s", result_dir, test, ERR_SUFFIX);
kent@mysql.com's avatar
kent@mysql.com committed
992 993

    /* netware specific files */
994
    snprintf(test_file, FN_REFLEN, "%s/%s%s", test_dir, test, NW_TEST_SUFFIX);
995 996
    if (stat(test_file, &info))
    {
997
      snprintf(test_file, FN_REFLEN, "%s/%s%s", test_dir, test, TEST_SUFFIX);
998 999 1000 1001 1002 1003 1004
      if (access(test_file,0))
      {
        printf("Invalid test name %s, %s file not found\n",test,test_file);
        return;
      }
    }

1005
    snprintf(result_file, FN_REFLEN, "%s/%s%s",
kent@mysql.com's avatar
kent@mysql.com committed
1006
             result_dir, test, NW_RESULT_SUFFIX);
1007 1008
    if (stat(result_file, &info))
    {
1009
      snprintf(result_file, FN_REFLEN, "%s/%s%s",
kent@mysql.com's avatar
kent@mysql.com committed
1010
               result_dir, test, RESULT_SUFFIX);
1011 1012
    }

kent@mysql.com's avatar
kent@mysql.com committed
1013
    /* init scripts */
1014
    if (stat(master_init_script, &info))
kent@mysql.com's avatar
kent@mysql.com committed
1015
      master_init_script[0]= 0;
1016
    else
kent@mysql.com's avatar
kent@mysql.com committed
1017 1018
      restart= TRUE;

1019
    if (stat(slave_init_script, &info))
kent@mysql.com's avatar
kent@mysql.com committed
1020
      slave_init_script[0]= 0;
1021
    else
kent@mysql.com's avatar
kent@mysql.com committed
1022 1023 1024 1025 1026 1027 1028 1029
      restart= TRUE;

    /* read options */
    if (read_option(master_opt_file, master_opt)) restart= TRUE;
    if (read_option(slave_opt_file, slave_opt)) restart= TRUE;
    if (read_option(slave_master_info_file, slave_master_info)) restart= TRUE;

    /* cleanup previous run */
1030 1031 1032
    remove(reject_file);
    remove(out_file);
    remove(err_file);
kent@mysql.com's avatar
kent@mysql.com committed
1033 1034

    /* start or restart? */
1035 1036
    if (!master_running) mysql_start();
      else if (restart) mysql_restart();
kent@mysql.com's avatar
kent@mysql.com committed
1037 1038

    /* let the system stabalize */
1039 1040
    sleep(1);

kent@mysql.com's avatar
kent@mysql.com committed
1041
    /* show test */
1042
    mlog("%-46s ", test);
kent@mysql.com's avatar
kent@mysql.com committed
1043 1044

    /* args */
1045 1046 1047 1048 1049 1050 1051
    init_args(&al);
    add_arg(&al, "%s", mysqltest_file);
    add_arg(&al, "--no-defaults");
    add_arg(&al, "--port=%u", master_port);
#if !defined(__NETWARE__) && !defined(__WIN__)
    add_arg(&al, "--socket=%s", master_socket);
    add_arg(&al, "--tmpdir=%s", mysql_tmp_dir);
kent@mysql.com's avatar
kent@mysql.com committed
1052
#endif
1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069
    add_arg(&al, "--database=%s", db);
    add_arg(&al, "--user=%s", user);
    add_arg(&al, "--password=%s", password);
    add_arg(&al, "--silent");
    add_arg(&al, "--basedir=%s/", mysql_test_dir);
    add_arg(&al, "--host=127.0.0.1");
    add_arg(&al, "-v");
    add_arg(&al, "-R");
    add_arg(&al, "%s", result_file);

    if (use_openssl)
    {
      add_arg(&al, "--ssl-ca=%s", ca_cert);
      add_arg(&al, "--ssl-cert=%s", client_cert);
      add_arg(&al, "--ssl-key=%s", client_key);
    }

kent@mysql.com's avatar
kent@mysql.com committed
1070 1071 1072 1073
    /* spawn */
    err= spawn(mysqltest_file, &al, TRUE, test_file, out_file, err_file, NULL);

    /* free args */
1074 1075
    free_args(&al);

kent@mysql.com's avatar
kent@mysql.com committed
1076 1077 1078
    remove_empty_file(out_file);
    remove_empty_file(err_file);

1079 1080
    if (err == 0)
    {
kent@mysql.com's avatar
kent@mysql.com committed
1081 1082
      /* pass */
      rstr= TEST_PASS;
1083
      ++total_pass;
kent@mysql.com's avatar
kent@mysql.com committed
1084 1085

      /* increment total */
1086 1087 1088 1089
      ++total_test;
    }
    else if (err == 2)
    {
kent@mysql.com's avatar
kent@mysql.com committed
1090 1091
      /* skip */
      rstr= TEST_SKIP;
1092 1093 1094 1095
      ++total_skip;
    }
    else if (err == 1)
    {
kent@mysql.com's avatar
kent@mysql.com committed
1096 1097
      /* fail */
      rstr= TEST_FAIL;
1098 1099
      ++total_fail;

kent@mysql.com's avatar
kent@mysql.com committed
1100
      /* increment total */
1101 1102 1103 1104
      ++total_test;
    }
    else
    {
kent@mysql.com's avatar
kent@mysql.com committed
1105
      rstr= TEST_BAD;
1106 1107
    }
  }
kent@mysql.com's avatar
kent@mysql.com committed
1108
  else /* early skips */
1109
  {
kent@mysql.com's avatar
kent@mysql.com committed
1110
    /* show test */
1111
    mlog("%-46s ", test);
kent@mysql.com's avatar
kent@mysql.com committed
1112 1113 1114

    /* skip */
    rstr= TEST_SKIP;
1115 1116 1117
    ++total_skip;
  }

kent@mysql.com's avatar
kent@mysql.com committed
1118
  /* result */
1119 1120 1121 1122 1123 1124
  mlog("%-14s\n", rstr);
}

/******************************************************************************

  vlog()
kent@mysql.com's avatar
kent@mysql.com committed
1125

1126 1127 1128
  Log the message.

******************************************************************************/
kent@mysql.com's avatar
kent@mysql.com committed
1129

1130 1131 1132 1133
void vlog(const char *format, va_list ap)
{
  vfprintf(stdout, format, ap);
  fflush(stdout);
kent@mysql.com's avatar
kent@mysql.com committed
1134

1135 1136 1137 1138 1139 1140 1141 1142 1143 1144
  if (log_fd)
  {
    vfprintf(log_fd, format, ap);
    fflush(log_fd);
  }
}

/******************************************************************************

  log()
kent@mysql.com's avatar
kent@mysql.com committed
1145

1146 1147 1148
  Log the message.

******************************************************************************/
kent@mysql.com's avatar
kent@mysql.com committed
1149

1150 1151 1152 1153 1154 1155 1156
void mlog(const char *format, ...)
{
  va_list ap;

  va_start(ap, format);

  vlog(format, ap);
kent@mysql.com's avatar
kent@mysql.com committed
1157

1158 1159 1160 1161 1162 1163
  va_end(ap);
}

/******************************************************************************

  log_info()
kent@mysql.com's avatar
kent@mysql.com committed
1164

1165 1166 1167
  Log the given information.

******************************************************************************/
kent@mysql.com's avatar
kent@mysql.com committed
1168

1169 1170 1171
void log_info(const char *format, ...)
{
  va_list ap;
kent@mysql.com's avatar
kent@mysql.com committed
1172

1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184
  va_start(ap, format);

  mlog("-- INFO : ");
  vlog(format, ap);
  mlog("\n");

  va_end(ap);
}

/******************************************************************************

  log_error()
kent@mysql.com's avatar
kent@mysql.com committed
1185

1186 1187 1188
  Log the given error.

******************************************************************************/
kent@mysql.com's avatar
kent@mysql.com committed
1189

1190 1191 1192
void log_error(const char *format, ...)
{
  va_list ap;
kent@mysql.com's avatar
kent@mysql.com committed
1193

1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205
  va_start(ap, format);

  mlog("-- ERROR: ");
  vlog(format, ap);
  mlog("\n");

  va_end(ap);
}

/******************************************************************************

  log_errno()
kent@mysql.com's avatar
kent@mysql.com committed
1206

1207 1208 1209
  Log the given error and errno.

******************************************************************************/
kent@mysql.com's avatar
kent@mysql.com committed
1210

1211 1212 1213
void log_errno(const char *format, ...)
{
  va_list ap;
kent@mysql.com's avatar
kent@mysql.com committed
1214

1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226
  va_start(ap, format);

  mlog("-- ERROR: (%003u) ", errno);
  vlog(format, ap);
  mlog("\n");

  va_end(ap);
}

/******************************************************************************

  die()
kent@mysql.com's avatar
kent@mysql.com committed
1227

1228 1229 1230
  Exit the application.

******************************************************************************/
kent@mysql.com's avatar
kent@mysql.com committed
1231

1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243
void die(const char *msg)
{
  log_error(msg);
#ifdef __NETWARE__
  pressanykey();
#endif
  exit(-1);
}

/******************************************************************************

  setup()
kent@mysql.com's avatar
kent@mysql.com committed
1244

1245 1246 1247
  Setup the mysql test enviornment.

******************************************************************************/
kent@mysql.com's avatar
kent@mysql.com committed
1248 1249

void setup(char *file __attribute__((unused)))
1250
{
1251 1252
  char temp[FN_REFLEN];
  char file_path[FN_REFLEN*2];
1253 1254 1255
  char *p;
  int position;

kent@mysql.com's avatar
kent@mysql.com committed
1256
  /* set the timezone for the timestamp test */
1257 1258 1259
#ifdef __WIN__
  _putenv( "TZ=GMT-3" );
#else
1260
  putenv((char *)"TZ=GMT-3");
1261
#endif
kent@mysql.com's avatar
kent@mysql.com committed
1262 1263
  /* find base dir */
#ifdef __NETWARE__
1264
  strcpy(temp, strlwr(file));
kent@mysql.com's avatar
kent@mysql.com committed
1265 1266
  while ((p= strchr(temp, '\\')) != NULL) *p= '/';
#else
1267
  getcwd(temp, FN_REFLEN);
kent@mysql.com's avatar
kent@mysql.com committed
1268 1269 1270
  position= strlen(temp);
  temp[position]= '/';
  temp[position+1]= 0;
1271
#ifdef __WIN__
kent@mysql.com's avatar
kent@mysql.com committed
1272 1273
  while ((p= strchr(temp, '\\')) != NULL) *p= '/';
#endif
1274 1275
#endif

kent@mysql.com's avatar
kent@mysql.com committed
1276
  if ((position= strinstr(temp, "/mysql-test/")) != 0)
1277
  {
kent@mysql.com's avatar
kent@mysql.com committed
1278 1279
    p= temp + position - 1;
    *p= 0;
1280
    strcpy(base_dir, temp);
kent@mysql.com's avatar
kent@mysql.com committed
1281
  }
1282 1283 1284 1285

  log_info("Currect directory: %s",base_dir);

#ifdef __NETWARE__
kent@mysql.com's avatar
kent@mysql.com committed
1286
  /* setup paths */
1287 1288 1289 1290 1291 1292 1293 1294 1295
  snprintf(bin_dir, FN_REFLEN, "%s/bin", base_dir);
  snprintf(mysql_test_dir, FN_REFLEN, "%s/mysql-test", base_dir);
  snprintf(test_dir, FN_REFLEN, "%s/t", mysql_test_dir);
  snprintf(mysql_tmp_dir, FN_REFLEN, "%s/var/tmp", mysql_test_dir);
  snprintf(result_dir, FN_REFLEN, "%s/r", mysql_test_dir);
  snprintf(master_dir, FN_REFLEN, "%s/var/master-data", mysql_test_dir);
  snprintf(slave_dir, FN_REFLEN, "%s/var/slave-data", mysql_test_dir);
  snprintf(lang_dir, FN_REFLEN, "%s/share/english", base_dir);
  snprintf(char_dir, FN_REFLEN, "%s/share/charsets", base_dir);
1296 1297

#ifdef HAVE_OPENSSL
kent@mysql.com's avatar
kent@mysql.com committed
1298 1299
  use_openssl= TRUE;
#endif /* HAVE_OPENSSL */
1300

kent@mysql.com's avatar
kent@mysql.com committed
1301
  /* OpenSSL paths */
1302 1303 1304 1305 1306
  snprintf(ca_cert, FN_REFLEN, "%s/SSL/cacert.pem", base_dir);
  snprintf(server_cert, FN_REFLEN, "%s/SSL/server-cert.pem", base_dir);
  snprintf(server_key, FN_REFLEN, "%s/SSL/server-key.pem", base_dir);
  snprintf(client_cert, FN_REFLEN, "%s/SSL/client-cert.pem", base_dir);
  snprintf(client_key, FN_REFLEN, "%s/SSL/client-key.pem", base_dir);
1307

kent@mysql.com's avatar
kent@mysql.com committed
1308
  /* setup files */
1309 1310 1311 1312 1313
  snprintf(mysqld_file, FN_REFLEN, "%s/mysqld", bin_dir);
  snprintf(mysqltest_file, FN_REFLEN, "%s/mysqltest", bin_dir);
  snprintf(mysqladmin_file, FN_REFLEN, "%s/mysqladmin", bin_dir);
  snprintf(master_pid, FN_REFLEN, "%s/var/run/master.pid", mysql_test_dir);
  snprintf(slave_pid, FN_REFLEN, "%s/var/run/slave.pid", mysql_test_dir);
1314
#elif __WIN__
kent@mysql.com's avatar
kent@mysql.com committed
1315
  /* setup paths */
1316
#ifdef _DEBUG
1317
  snprintf(bin_dir, FN_REFLEN, "%s/client_debug", base_dir);
1318
#else
1319
  snprintf(bin_dir, FN_REFLEN, "%s/client_release", base_dir);
1320
#endif
1321 1322 1323 1324 1325 1326 1327 1328
  snprintf(mysql_test_dir, FN_REFLEN, "%s/mysql-test", base_dir);
  snprintf(test_dir, FN_REFLEN, "%s/t", mysql_test_dir);
  snprintf(mysql_tmp_dir, FN_REFLEN, "%s/var/tmp", mysql_test_dir);
  snprintf(result_dir, FN_REFLEN, "%s/r", mysql_test_dir);
  snprintf(master_dir, FN_REFLEN, "%s/var/master-data", mysql_test_dir);
  snprintf(slave_dir, FN_REFLEN, "%s/var/slave-data", mysql_test_dir);
  snprintf(lang_dir, FN_REFLEN, "%s/share/english", base_dir);
  snprintf(char_dir, FN_REFLEN, "%s/share/charsets", base_dir);
1329 1330

#ifdef HAVE_OPENSSL
kent@mysql.com's avatar
kent@mysql.com committed
1331 1332
  use_openssl= TRUE;
#endif /* HAVE_OPENSSL */
1333

kent@mysql.com's avatar
kent@mysql.com committed
1334
  /* OpenSSL paths */
1335 1336 1337 1338 1339
  snprintf(ca_cert, FN_REFLEN, "%s/SSL/cacert.pem", base_dir);
  snprintf(server_cert, FN_REFLEN, "%s/SSL/server-cert.pem", base_dir);
  snprintf(server_key, FN_REFLEN, "%s/SSL/server-key.pem", base_dir);
  snprintf(client_cert, FN_REFLEN, "%s/SSL/client-cert.pem", base_dir);
  snprintf(client_key, FN_REFLEN, "%s/SSL/client-key.pem", base_dir);
1340

kent@mysql.com's avatar
kent@mysql.com committed
1341
  /* setup files */
1342 1343 1344
  snprintf(mysqld_file, FN_REFLEN, "%s/mysqld.exe", bin_dir);
  snprintf(mysqltest_file, FN_REFLEN, "%s/mysqltest.exe", bin_dir);
  snprintf(mysqladmin_file, FN_REFLEN, "%s/mysqladmin.exe", bin_dir);
kent@mysql.com's avatar
kent@mysql.com committed
1345 1346
#else
  /* setup paths */
1347 1348 1349 1350 1351 1352 1353 1354 1355
  snprintf(bin_dir, FN_REFLEN, "%s/client", base_dir);
  snprintf(mysql_test_dir, FN_REFLEN, "%s/mysql-test", base_dir);
  snprintf(test_dir, FN_REFLEN, "%s/t", mysql_test_dir);
  snprintf(mysql_tmp_dir, FN_REFLEN, "%s/var/tmp", mysql_test_dir);
  snprintf(result_dir, FN_REFLEN, "%s/r", mysql_test_dir);
  snprintf(master_dir, FN_REFLEN, "%s/var/master-data", mysql_test_dir);
  snprintf(slave_dir, FN_REFLEN, "%s/var/slave-data", mysql_test_dir);
  snprintf(lang_dir, FN_REFLEN, "%s/sql/share/english", base_dir);
  snprintf(char_dir, FN_REFLEN, "%s/sql/share/charsets", base_dir);
1356 1357

#ifdef HAVE_OPENSSL
kent@mysql.com's avatar
kent@mysql.com committed
1358 1359
  use_openssl= TRUE;
#endif /* HAVE_OPENSSL */
1360

kent@mysql.com's avatar
kent@mysql.com committed
1361
  /* OpenSSL paths */
1362 1363 1364 1365 1366
  snprintf(ca_cert, FN_REFLEN, "%s/SSL/cacert.pem", base_dir);
  snprintf(server_cert, FN_REFLEN, "%s/SSL/server-cert.pem", base_dir);
  snprintf(server_key, FN_REFLEN, "%s/SSL/server-key.pem", base_dir);
  snprintf(client_cert, FN_REFLEN, "%s/SSL/client-cert.pem", base_dir);
  snprintf(client_key, FN_REFLEN, "%s/SSL/client-key.pem", base_dir);
1367

kent@mysql.com's avatar
kent@mysql.com committed
1368
  /* setup files */
1369 1370 1371 1372 1373
  snprintf(mysqld_file, FN_REFLEN, "%s/sql/mysqld", base_dir);
  snprintf(mysqltest_file, FN_REFLEN, "%s/mysqltest", bin_dir);
  snprintf(mysqladmin_file, FN_REFLEN, "%s/mysqladmin", bin_dir);
  snprintf(master_pid, FN_REFLEN, "%s/var/run/master.pid", mysql_test_dir);
  snprintf(slave_pid, FN_REFLEN, "%s/var/run/slave.pid", mysql_test_dir);
kent@mysql.com's avatar
kent@mysql.com committed
1374

1375 1376
  snprintf(master_socket,FN_REFLEN, "%s/var/tmp/master.sock", mysql_test_dir);
  snprintf(slave_socket,FN_REFLEN, "%s/var/tmp/slave.sock", mysql_test_dir);
1377 1378

#endif
kent@mysql.com's avatar
kent@mysql.com committed
1379
  /* create log file */
1380
  snprintf(temp, FN_REFLEN, "%s/mysql-test-run.log", mysql_test_dir);
kent@mysql.com's avatar
kent@mysql.com committed
1381
  if ((log_fd= fopen(temp, "w+")) == NULL)
1382 1383 1384 1385
  {
    log_errno("Unable to create log file.");
  }

kent@mysql.com's avatar
kent@mysql.com committed
1386 1387
  /* prepare skip test list */
  while ((p= strchr(skip_test, ',')) != NULL) *p= ' ';
1388
  strcpy(temp, strlwr(skip_test));
1389
  snprintf(skip_test, FN_REFLEN, " %s ", temp);
1390

kent@mysql.com's avatar
kent@mysql.com committed
1391 1392
  /* environment */
#ifdef __NETWARE__
1393
  setenv("MYSQL_TEST_DIR", mysql_test_dir, 1);
1394
  snprintf(file_path, FN_REFLEN*2,
kent@mysql.com's avatar
kent@mysql.com committed
1395 1396
           "%s/client/mysqldump --no-defaults -u root --port=%u",
           bin_dir, master_port);
1397
  setenv("MYSQL_DUMP", file_path, 1);
1398
  snprintf(file_path, FN_REFLEN*2,
kent@mysql.com's avatar
kent@mysql.com committed
1399 1400
           "%s/client/mysqlbinlog --no-defaults --local-load=%s",
           bin_dir, mysql_tmp_dir);
1401 1402
  setenv("MYSQL_BINLOG", file_path, 1);
#elif __WIN__
1403
  snprintf(file_path,FN_REFLEN,"MYSQL_TEST_DIR=%s",mysql_test_dir);
1404
  _putenv(file_path);
1405
  snprintf(file_path, FN_REFLEN*2,
kent@mysql.com's avatar
kent@mysql.com committed
1406 1407
           "MYSQL_DUMP=%s/mysqldump.exe --no-defaults -u root --port=%u",
           bin_dir, master_port);
1408
  _putenv(file_path);
1409
  snprintf(file_path, FN_REFLEN*2,
kent@mysql.com's avatar
kent@mysql.com committed
1410 1411
           "MYSQL_BINLOG=%s/mysqlbinlog.exe --no-defaults --local-load=%s",
           bin_dir, mysql_tmp_dir);
1412 1413
  _putenv(file_path);
#else
1414 1415 1416 1417
  snprintf(file_path,FN_REFLEN,"MYSQL_TEST_DIR=%s",mysql_test_dir);
  putenv(file_path);
  snprintf(file_path, FN_REFLEN*2,
           "MYSQL_DUMP=%s/mysqldump --no-defaults -u root --port=%u --socket=%s",
kent@mysql.com's avatar
kent@mysql.com committed
1418
           bin_dir, master_port, master_socket);
1419 1420 1421
  putenv(file_path);
  snprintf(file_path, FN_REFLEN*2,
           "MYSQL_BINLOG=%s/mysqlbinlog --no-defaults --local-load=%s",
kent@mysql.com's avatar
kent@mysql.com committed
1422
           bin_dir, mysql_tmp_dir);
1423
  putenv(file_path);
kent@mysql.com's avatar
kent@mysql.com committed
1424
#endif
1425 1426

#ifndef __WIN__
1427 1428 1429
  putenv((char *)"MASTER_MYPORT=9306");
  putenv((char *)"SLAVE_MYPORT=9307");
  putenv((char *)"MYSQL_TCP_PORT=3306");
1430 1431 1432 1433 1434 1435 1436 1437
#else
  _putenv("MASTER_MYPORT=9306");
  _putenv("SLAVE_MYPORT=9307");
  _putenv("MYSQL_TCP_PORT=3306");
#endif

}

wax@kishkin.ru's avatar
wax@kishkin.ru committed
1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448
/*
  Compare names of testes for right order
*/
#ifdef __WIN__
int compare( const void *arg1, const void *arg2 )
{
  return _stricmp( * ( char** ) arg1, * ( char** ) arg2 );
}
#endif


1449 1450 1451
/******************************************************************************

  main()
kent@mysql.com's avatar
kent@mysql.com committed
1452

1453
******************************************************************************/
kent@mysql.com's avatar
kent@mysql.com committed
1454

1455 1456
int main(int argc, char **argv)
{
kent@mysql.com's avatar
kent@mysql.com committed
1457 1458
  int is_ignore_list= 0;
  /* setup */
1459
  setup(argv[0]);
kent@mysql.com's avatar
kent@mysql.com committed
1460 1461 1462 1463 1464 1465 1466 1467 1468

  /*
    The --ignore option is comma saperated list of test cases to skip and
    should be very first command line option to the test suite.

    The usage is now:
    mysql_test_run --ignore=test1,test2 test3 test4
    where test1 and test2 are test cases to ignore
    and test3 and test4 are test cases to run.
1469 1470 1471 1472 1473 1474 1475
  */
  if (argc >= 2 && !strnicmp(argv[1], "--ignore=", sizeof("--ignore=")-1))
  {
    char *temp, *token;
    temp= strdup(strchr(argv[1],'=') + 1);
    for (token=str_tok(temp, ","); token != NULL; token=str_tok(NULL, ","))
    {
1476
      if (strlen(ignore_test) + strlen(token) + 2 <= FN_REFLEN-1)
1477 1478 1479 1480 1481 1482 1483 1484
        sprintf(ignore_test+strlen(ignore_test), " %s ", token);
      else
      {
        free(temp);
        die("ignore list too long.");
      }
    }
    free(temp);
kent@mysql.com's avatar
kent@mysql.com committed
1485
    is_ignore_list= 1;
1486
  }
kent@mysql.com's avatar
kent@mysql.com committed
1487
  /* header */
1488 1489 1490 1491 1492
#ifndef __WIN__
  mlog("MySQL Server %s, for %s (%s)\n\n", VERSION, SYSTEM_TYPE, MACHINE_TYPE);
#else
  mlog("MySQL Server ---, for %s (%s)\n\n", SYSTEM_TYPE, MACHINE_TYPE);
#endif
kent@mysql.com's avatar
kent@mysql.com committed
1493

1494
  mlog("Initializing Tests...\n");
kent@mysql.com's avatar
kent@mysql.com committed
1495 1496

  /* install test databases */
1497
  mysql_install_db();
kent@mysql.com's avatar
kent@mysql.com committed
1498

1499
  mlog("Starting Tests...\n");
kent@mysql.com's avatar
kent@mysql.com committed
1500

1501 1502 1503 1504 1505 1506 1507 1508
  mlog("\n");
  mlog(HEADER);
  mlog(DASH);

  if ( argc > 1 + is_ignore_list )
  {
    int i;

kent@mysql.com's avatar
kent@mysql.com committed
1509 1510
    /* single test */
    single_test= TRUE;
1511

kent@mysql.com's avatar
kent@mysql.com committed
1512
    for (i= 1 + is_ignore_list; i < argc; i++)
1513
    {
kent@mysql.com's avatar
kent@mysql.com committed
1514
      /* run given test */
1515 1516 1517 1518 1519
      run_test(argv[i]);
    }
  }
  else
  {
kent@mysql.com's avatar
kent@mysql.com committed
1520
    /* run all tests */
1521
#ifndef __WIN__
1522 1523 1524
    struct dirent *entry;
    DIR *parent;
    char test[FN_LEN];
1525
    int position;
kent@mysql.com's avatar
kent@mysql.com committed
1526

1527 1528
    /* FIXME are we sure the list is sorted if using readdir()? */
    if ((parent= opendir(test_dir)) == NULL)    /* Not thread safe */
1529
      die("Unable to open tests directory.");
kent@mysql.com's avatar
kent@mysql.com committed
1530
    else
1531
    {
1532
      while ((entry= readdir(parent)) != NULL)  /* Not thread safe */
1533
      {
1534
        strcpy(test, strlwr(entry->d_name));
kent@mysql.com's avatar
kent@mysql.com committed
1535 1536
        /* find the test suffix */
        if ((position= strinstr(test, TEST_SUFFIX)) != 0)
1537
        {
kent@mysql.com's avatar
kent@mysql.com committed
1538
          /* null terminate at the suffix */
1539
          *(test + position - 1)= '\0';
kent@mysql.com's avatar
kent@mysql.com committed
1540
          /* run test */
1541 1542
          run_test(test);
        }
kent@mysql.com's avatar
kent@mysql.com committed
1543
      }
1544
      closedir(parent);
kent@mysql.com's avatar
kent@mysql.com committed
1545
    }
1546 1547
#else
    struct _finddata_t dir;
kent@mysql.com's avatar
kent@mysql.com committed
1548
#if defined(_MSC_VER) && _MSC_VER > 1200
1549
    intptr_t handle;
kent@mysql.com's avatar
kent@mysql.com committed
1550 1551 1552
#else
    long handle;
#endif  /* _MSC_VER && _MSC_VER > 1200  */ 
1553 1554
    char test[FN_LEN];
    char mask[FN_REFLEN];
1555 1556
    char *p;
    int position;
kent@mysql.com's avatar
kent@mysql.com committed
1557 1558
    char **names= 0;
    char **testes= 0;
1559 1560
    int name_index;
    int index;
kent@mysql.com's avatar
kent@mysql.com committed
1561 1562 1563

    /* single test */
    single_test= FALSE;
1564

1565
    snprintf(mask,FN_REFLEN,"%s/*.test",test_dir);
kent@mysql.com's avatar
kent@mysql.com committed
1566

1567 1568 1569 1570 1571
    if ((handle=_findfirst(mask,&dir)) == -1L)
    {
      die("Unable to open tests directory.");
    }

kent@mysql.com's avatar
kent@mysql.com committed
1572 1573 1574
    names= malloc(MAX_COUNT_TESTES*4);
    testes= names;
    name_index= 0;
1575 1576 1577

    do
    {
kent@mysql.com's avatar
kent@mysql.com committed
1578
      if (!(dir.attrib & _A_SUBDIR))
1579 1580
      {
        strcpy(test, strlwr(dir.name));
kent@mysql.com's avatar
kent@mysql.com committed
1581 1582 1583

        /* find the test suffix */
        if ((position= strinstr(test, TEST_SUFFIX)) != 0)
1584
        {
kent@mysql.com's avatar
kent@mysql.com committed
1585 1586 1587
          p= test + position - 1;
          /* null terminate at the suffix */
          *p= 0;
1588

kent@mysql.com's avatar
kent@mysql.com committed
1589
          /* insert test */
1590
          *names= malloc(FN_REFLEN);
1591 1592 1593 1594
          strcpy(*names,test);
          names++;
          name_index++;
        }
kent@mysql.com's avatar
kent@mysql.com committed
1595
      }
1596 1597 1598 1599 1600 1601
    }while (_findnext(handle,&dir) == 0);

    _findclose(handle);

    qsort( (void *)testes, name_index, sizeof( char * ), compare );

kent@mysql.com's avatar
kent@mysql.com committed
1602
    for (index= 0; index <= name_index; index++)
1603 1604 1605 1606 1607 1608 1609 1610 1611
    {
      run_test(testes[index]);
      free(testes[index]);
    }

    free(testes);
#endif
  }

kent@mysql.com's avatar
kent@mysql.com committed
1612
  /* stop server */
1613 1614 1615 1616 1617 1618 1619
  mysql_stop();

  mlog(DASH);
  mlog("\n");

  mlog("Ending Tests...\n");

kent@mysql.com's avatar
kent@mysql.com committed
1620
  /* report stats */
1621 1622
  report_stats();

kent@mysql.com's avatar
kent@mysql.com committed
1623
  /* close log */
1624 1625
  if (log_fd) fclose(log_fd);

kent@mysql.com's avatar
kent@mysql.com committed
1626 1627
  /* keep results up */
#ifdef __NETWARE__
1628 1629 1630 1631 1632 1633 1634 1635 1636 1637
  pressanykey();
#endif
  return 0;
}


/*
 Synopsis:
  This function breaks the string into a sequence of tokens. The difference
  between this function and strtok is that it respects the quoted string i.e.
kent@mysql.com's avatar
kent@mysql.com committed
1638
  it skips  any delimiter character within the quoted part of the string.
1639 1640 1641 1642
  It return tokens by eliminating quote character. It modifies the input string
  passed. It will work with whitespace delimeter but may not work properly with
  other delimeter. If the delimeter will contain any quote character, then
  function will not tokenize and will return null string.
kent@mysql.com's avatar
kent@mysql.com committed
1643
  e.g. if input string is
1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666
     --init-slave="set global max_connections=500" --skip-external-locking
  then the output will two string i.e.
     --init-slave=set global max_connections=500
     --skip-external-locking

Arguments:
  string:  input string
  delim:   set of delimiter character
Output:
  return the null terminated token of NULL.
*/

char *str_tok(char *string, const char *delim)
{
  char *token;            /* current token received from strtok */
  char *qt_token;         /* token delimeted by the matching pair of quote */
  /*
    if there are any quote chars found in the token then this variable
    will hold the concatenated string to return to the caller
  */
  char *ptr_token=NULL;
  /* pointer to the quote character in the token from strtok */
  char *ptr_quote=NULL;
kent@mysql.com's avatar
kent@mysql.com committed
1667

1668 1669 1670 1671 1672
  /* See if the delimeter contains any quote character */
  if (strchr(delim,'\'') || strchr(delim,'\"'))
    return NULL;

  /* repeate till we are getting some token from strtok */
kent@mysql.com's avatar
kent@mysql.com committed
1673
  while ((token= (char*)strtok(string, delim) ) != NULL)
1674 1675 1676 1677 1678
  {
    /*
      make the input string NULL so that next time onward strtok can
      be called with NULL input string.
    */
kent@mysql.com's avatar
kent@mysql.com committed
1679 1680
    string= NULL;
    /* We don't need to remove any quote character for Windows version */
1681 1682
#ifndef __WIN__
    /* check if the current token contain double quote character*/
kent@mysql.com's avatar
kent@mysql.com committed
1683
    if ((ptr_quote= (char*)strchr(token,'\"')) != NULL)
1684 1685 1686 1687 1688
    {
      /*
        get the matching the matching double quote in the remaining
        input string
      */
kent@mysql.com's avatar
kent@mysql.com committed
1689
      qt_token= (char*)strtok(NULL,"\"");
1690 1691
    }
    /* check if the current token contain single quote character*/
kent@mysql.com's avatar
kent@mysql.com committed
1692
    else if ((ptr_quote= (char*)strchr(token,'\'')) != NULL)
1693 1694 1695 1696 1697
    {
      /*
        get the matching the matching single quote in the remaining
        input string
      */
kent@mysql.com's avatar
kent@mysql.com committed
1698
      qt_token= (char*)strtok(NULL,"\'");
1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720
    }
#endif
    /*
      if the current token does not contains any quote character then
      return to the caller.
    */
    if (ptr_quote == NULL)
    {
      /*
        if there is any earlier token i.e. ptr_token then append the
        current token in it and return it else return the current
        token directly
      */
      return ptr_token ? strcat(ptr_token,token) : token;
    }

    /*
      remove the quote character i.e. make NULL so that the token will
      be devided in two part and later both part can be concatenated
      and hence quote will be removed
    */
    *ptr_quote= 0;
kent@mysql.com's avatar
kent@mysql.com committed
1721

1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735
    /* check if ptr_token has been initialized or not */
    if (ptr_token == NULL)
    {
      /* initialize the ptr_token with current token */
      ptr_token= token;
      /* copy entire string between matching pair of quote*/
      sprintf(ptr_token+strlen(ptr_token),"%s %s", ptr_quote+1, qt_token);
    }
    else
    {
      /*
        copy the current token and entire string between matching pair
        of quote
      */
kent@mysql.com's avatar
kent@mysql.com committed
1736 1737 1738 1739 1740 1741 1742 1743 1744
      if (qt_token == NULL)
      {
        sprintf(ptr_token+strlen(ptr_token),"%s%s", token, ptr_quote+1);
      }
      else
      {
        sprintf(ptr_token+strlen(ptr_token),"%s%s %s", token, ptr_quote+1,
                qt_token );
      }
1745 1746
    }
  }
kent@mysql.com's avatar
kent@mysql.com committed
1747

1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763
  /* return the concatenated token */
  return ptr_token;
}

#ifndef __WIN__

/*
 Synopsis:
  This function run scripts files on Linux and Netware

Arguments:
  script_name:  name of script file

Output:
  nothing
*/
kent@mysql.com's avatar
kent@mysql.com committed
1764

1765 1766 1767 1768
void run_init_script(const char *script_name)
{
  arg_list_t al;
  int err;
kent@mysql.com's avatar
kent@mysql.com committed
1769 1770

  /* args */
1771 1772 1773
  init_args(&al);
  add_arg(&al, sh_file);
  add_arg(&al, script_name);
kent@mysql.com's avatar
kent@mysql.com committed
1774 1775 1776

  /* spawn */
  if ((err= spawn(sh_file, &al, TRUE, NULL, NULL, NULL, NULL)) != 0)
1777 1778 1779
  {
    die("Unable to run script.");
  }
kent@mysql.com's avatar
kent@mysql.com committed
1780 1781

  /* free args */
1782 1783 1784
  free_args(&al);
}
#endif