64
67
#include <boost/unordered_map.hpp>
66
69
/* Added this for string translation. */
67
#include "drizzled/gettext.h"
68
#include "drizzled/drizzle_time.h"
69
#include "drizzled/charset.h"
70
#include <drizzled/gettext.h>
71
#include <drizzled/type/time.h>
72
#include <drizzled/charset.h>
73
#include <drizzled/typelib.h>
70
74
#include <drizzled/configmake.h>
76
#define PTR_BYTE_DIFF(A,B) (ptrdiff_t) (reinterpret_cast<const unsigned char*>(A) - reinterpret_cast<const unsigned char*>(B))
72
78
#ifndef DRIZZLE_RETURN_SERVER_GONE
73
79
#define DRIZZLE_RETURN_HANDSHAKE_FAILED DRIZZLE_RETURN_ERROR_CODE
164
171
uint32_t lineno; /* Current line in file */
167
static struct st_test_file file_stack[16];
168
static struct st_test_file* cur_file;
169
static struct st_test_file* file_stack_end;
174
static boost::array<st_test_file, 16> file_stack;
175
static st_test_file* cur_file;
172
177
static const CHARSET_INFO *charset_info= &my_charset_utf8_general_ci; /* Default charset */
178
183
static char *timer_file = NULL;
179
184
static uint64_t timer_start;
180
static void timer_output(void);
181
static uint64_t timer_now(void);
185
static void timer_output();
186
static uint64_t timer_now();
183
188
static uint64_t progress_start= 0;
389
395
struct st_match_err err[10];
392
static struct st_expected_errors saved_expected_errors;
399
static st_expected_errors saved_expected_errors;
396
404
char *query, *query_buf,*first_argument,*last_argument,*end;
397
405
int first_word_len, query_len;
398
406
bool abort_on_error;
399
407
st_expected_errors expected_errors;
400
408
string require_file;
401
enum enum_commands type;
404
412
: query(NULL), query_buf(NULL), first_argument(NULL), last_argument(NULL),
438
443
VAR* var_from_env(const char *, const char *);
439
444
VAR* var_init(VAR* v, const char *name, int name_len, const char *val,
441
void var_free(pair<string, VAR*> v);
442
446
VAR* var_get(const char *var_name, const char** var_name_end,
443
447
bool raw, bool ignore_not_existing);
444
448
void eval_expr(VAR* v, const char *p, const char** p_end);
445
449
bool match_delimiter(int c, const char *delim, uint32_t length);
446
450
void dump_result_to_reject_file(char *buf, int size);
447
451
void dump_result_to_log_file(const char *buf, int size);
448
void dump_warning_messages(void);
449
void dump_progress(void);
452
void dump_warning_messages();
453
void dump_progress();
451
455
void do_eval(string *query_eval, const char *query,
452
456
const char *query_end, bool pass_through_escape_chars);
457
461
static char *replace_column[MAX_COLUMNS];
458
462
static uint32_t max_replace_column= 0;
459
463
void do_get_replace_column(struct st_command*);
460
void free_replace_column(void);
464
void free_replace_column();
462
466
/* For replace */
463
467
void do_get_replace(struct st_command *command);
464
void free_replace(void);
466
470
/* For replace_regex */
467
471
void do_get_replace_regex(struct st_command *command);
468
void free_replace_regex(void);
471
void free_all_replace(void);
474
void free_all_replace(void){
476
free_replace_regex();
477
free_replace_column();
480
473
void replace_append_mem(string *ds, const char *val,
886
875
free(next_con->name);
892
static void close_files(void)
880
static void close_files()
895
for (; cur_file >= file_stack; cur_file--)
882
for (; cur_file >= file_stack.data(); cur_file--)
897
884
if (cur_file->file && cur_file->file != stdin)
899
885
fclose(cur_file->file);
901
free((unsigned char*) cur_file->file_name);
886
free(const_cast<char*>(cur_file->file_name));
902
887
cur_file->file_name= 0;
908
static void free_used_memory(void)
891
static void free_used_memory()
913
893
close_connections();
915
for_each(var_hash.begin(), var_hash.end(), var_free);
895
BOOST_FOREACH(var_hash_t::reference i, var_hash)
897
free(i.second->str_val);
898
free(i.second->env_s);
899
if (i.second->alloced)
916
902
var_hash.clear();
918
vector<st_command *>::iterator iter;
919
for (iter= q_lines.begin() ; iter < q_lines.end() ; iter++)
921
struct st_command * q_line= *iter;
925
for (i= 0; i < 10; i++)
903
BOOST_FOREACH(vector<st_command*>::reference i, q_lines)
905
for (size_t i= 0; i < var_reg.size(); i++)
927
907
if (var_reg[i].alloced_len)
928
908
free(var_reg[i].str_val);
931
910
free_all_replace();
1038
1014
/* Print include filestack */
1039
1015
fprintf(stderr, "The test '%s' is not supported by this installation\n",
1040
file_stack->file_name);
1016
file_stack[0].file_name);
1041
1017
fprintf(stderr, "Detected in file %s at line %d\n",
1042
1018
err_file->file_name, err_file->lineno);
1043
while (err_file != file_stack)
1019
while (err_file != file_stack.data())
1046
1022
fprintf(stderr, "included from %s at line %d\n",
1629
1603
VAR *var_init(VAR *v, const char *name, int name_len, const char *val,
1634
1606
if (!name_len && name)
1635
1607
name_len = strlen(name);
1636
1608
if (!val_len && val)
1637
1609
val_len = strlen(val) ;
1638
val_alloc_len = val_len + 16; /* room to grow */
1639
if (!(tmp_var=v) && !(tmp_var = (VAR*)malloc(sizeof(*tmp_var)
1641
die("Out of memory");
1610
VAR *tmp_var = v ? v : (VAR*)malloc(sizeof(*tmp_var) + name_len+1);
1643
tmp_var->name = (name) ? (char*) tmp_var + sizeof(*tmp_var) : 0;
1612
tmp_var->name = name ? (char*)&tmp_var[1] : 0;
1644
1613
tmp_var->alloced = (v == 0);
1646
if (!(tmp_var->str_val = (char *)malloc(val_alloc_len+1)))
1647
die("Out of memory");
1615
int val_alloc_len = val_len + 16; /* room to grow */
1616
tmp_var->str_val = (char*)malloc(val_alloc_len+1);
1649
1618
memcpy(tmp_var->name, name, name_len);
1655
1624
tmp_var->name_len = name_len;
1656
1625
tmp_var->str_val_len = val_len;
1657
1626
tmp_var->alloced_len = val_alloc_len;
1658
tmp_var->int_val = (val) ? atoi(val) : 0;
1659
tmp_var->int_dirty = 0;
1627
tmp_var->int_val = val ? atoi(val) : 0;
1628
tmp_var->int_dirty = false;
1660
1629
tmp_var->env_s = 0;
1661
1630
return tmp_var;
1665
void var_free(pair<string, VAR *> v)
1667
free(v.second->str_val);
1668
free(v.second->env_s);
1669
if (v.second->alloced)
1674
1633
VAR* var_from_env(const char *name, const char *def_val)
1678
if (!(tmp = getenv(name)))
1635
const char *tmp= getenv(name);
1681
v = var_init(0, name, strlen(name), tmp, strlen(tmp));
1682
string var_name(name);
1683
var_hash.insert(make_pair(var_name, v));
1638
return var_hash[name] = var_init(0, name, strlen(name), tmp, strlen(tmp));
1688
1641
VAR* var_get(const char *var_name, const char **var_name_end, bool raw,
1689
1642
bool ignore_not_existing)
1694
1646
if (*var_name != '$')
1696
1648
digit = *++var_name - '0';
1712
1664
die("Too long variable name: %s", save_var_name);
1714
1666
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);
1667
var_hash_t::iterator iter= var_hash.find(save_var_name_str);
1717
1668
if (iter == var_hash.end())
1719
1670
char buff[MAX_VAR_NAME_LENGTH+1];
1750
1701
static VAR *var_obtain(const char *name, int len)
1752
1703
string var_name(name, len);
1753
boost::unordered_map<string, VAR*>::iterator iter=
1754
var_hash.find(var_name);
1704
var_hash_t::iterator iter= var_hash.find(var_name);
1755
1705
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));
1706
return iter->second;
1707
return var_hash[var_name] = var_init(0, name, len, "", 0);
2162
2108
internal::fn_format(buff, name, "", "", MY_UNPACK_FILENAME);
2164
if (cur_file == file_stack_end)
2111
if (cur_file == &*file_stack.end())
2165
2112
die("Source directives are nesting too deep");
2167
2113
if (!(cur_file->file= fopen(buff, "r")))
3340
3275
sleep_val= opt_sleep;
3343
usleep((uint32_t) (sleep_val * 1000000L));
3278
usleep(sleep_val * 1000000);
3344
3279
command->last_argument= sleep_end;
3349
static void do_get_file_name(struct st_command *command, string &dest)
3284
static void do_get_file_name(st_command *command, string &dest)
3351
char *p= command->first_argument, *name;
3286
char *p= command->first_argument;
3353
3288
die("Missing file name argument");
3355
3290
while (*p && !my_isspace(charset_info,*p))
3426
3361
and assign that string to the $variable
3428
3363
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]);
3434
global_error_names.insert(ErrorCodes::value_type(error_name,
3435
boost::lexical_cast<uint32_t>(error_code)));
3366
global_error_names[string(row[0], lengths[0])] = boost::lexical_cast<uint32_t>(string(row[1], lengths[1]));
3437
3368
catch (boost::bad_lexical_cast &ex)
3526
3455
die("The error name definition must start with an uppercase E");
3459
/* Error name string */
3461
to->code.errnum= get_errcode_from_name(p, end);
3462
to->type= ERR_ERRNO;
3532
/* Check that the string passed to str2int only contain digits */
3533
while (*p && p != end)
3535
if (!my_isdigit(charset_info, *p))
3536
die("Invalid argument to error: '%s' - " \
3537
"the errno may only consist of digits[0-9]",
3538
command->first_argument);
3542
/* Convert the sting to int */
3543
istringstream buff(start);
3544
if ((buff >> val).fail())
3545
die("Invalid argument to error: '%s'", command->first_argument);
3547
to->code.errnum= (uint32_t) val;
3548
to->type= ERR_ERRNO;
3466
die ("You must either use the SQLSTATE or built in drizzle error label, numbers are not accepted");
5366
5284
save= command->query[command->first_word_len-1];
5367
5285
command->query[command->first_word_len-1]= 0;
5368
if (find_type(command->query, &command_typelib, 1+2) > 0)
5286
if (command_typelib.find_type(command->query, 1+2) > 0)
5369
5287
die("Extra delimiter \";\" found");
5370
5288
command->query[command->first_word_len-1]= save;
5587
5504
next_con= connections + 1;
5589
5506
/* Init file stack */
5590
memset(file_stack, 0, sizeof(file_stack));
5592
file_stack + (sizeof(file_stack)/sizeof(struct st_test_file)) - 1;
5593
cur_file= file_stack;
5507
memset(file_stack.data(), 0, sizeof(file_stack));
5508
cur_file= file_stack.data();
5595
5510
/* Init block stack */
5596
5511
memset(block_stack, 0, sizeof(block_stack));
5631
5546
internal::fn_format(buff, tmp.c_str(), "", "", MY_UNPACK_FILENAME);
5632
assert(cur_file == file_stack && cur_file->file == 0);
5547
assert(cur_file == file_stack.data() && cur_file->file == 0);
5633
5548
if (!(cur_file->file= fopen(buff, "r")))
5635
5550
fprintf(stderr, _("Could not open '%s' for reading: errno = %d"), buff, errno);
6213
6128
start= buff= (char *)malloc(strlen(from)+1);
6217
6131
uint32_t column_number;
6219
to= get_string(&buff, &from, command);
6133
char *to= get_string(&buff, &from, command);
6220
6134
if (!(column_number= atoi(to)) || column_number > MAX_COLUMNS)
6221
6135
die("Wrong column number to replace_column in '%s'", command->query);
6263
6173
} POINTER_ARRAY;
6265
6175
struct st_replace;
6266
struct st_replace *init_replace(char * *from, char * *to, uint32_t count,
6267
char * word_end_chars);
6176
struct st_replace *init_replace(const char **from, const char **to, uint32_t count,
6177
char *word_end_chars);
6268
6178
int insert_pointer_name(POINTER_ARRAY *pa,char * name);
6269
6179
void replace_strings_append(struct st_replace *rep, string* ds,
6270
6180
const char *from, int len);
6271
void free_pointer_array(POINTER_ARRAY *pa);
6273
struct st_replace *glob_replace= NULL;
6182
st_replace *glob_replace= NULL;
6183
// boost::scoped_ptr<st_replace> glob_replace;
6276
6186
Get arguments for replace. The syntax is:
6311
6231
if (my_isspace(charset_info,i))
6313
6233
*pos=0; /* End pointer */
6314
if (!(glob_replace= init_replace((char**) from_array.typelib.type_names,
6315
(char**) to_array.typelib.type_names,
6316
(uint32_t) from_array.typelib.count,
6234
if (!(glob_replace= init_replace(from_array.typelib.type_names,
6235
to_array.typelib.type_names,
6236
from_array.typelib.count,
6317
6237
word_end_chars)))
6318
6238
die("Can't initialize replace from '%s'", command->query);
6319
6239
free_pointer_array(&from_array);
6417
6334
st_regex substition. At the end of substitutions buf points to the
6418
6335
one containing the final result.
6337
typedef vector<st_regex> regex_arr_t;
6421
6340
char* even_buf;
6423
6342
int even_buf_len;
6424
6343
int odd_buf_len;
6344
boost::array<char, 8 << 10> buf0_;
6345
boost::array<char, 8 << 10> buf1_;
6346
regex_arr_t regex_arr;
6427
struct st_replace_regex *glob_replace_regex= 0;
6349
boost::scoped_ptr<st_replace_regex> glob_replace_regex;
6429
6351
int reg_replace(char** buf_p, int* buf_len_p, char *pattern, char *replace,
6430
6352
char *string, int icase, int global);
6466
6388
Returns: st_replace_regex struct with pairs of substitutions
6469
static struct st_replace_regex* init_replace_regex(char* expr)
6391
st_replace_regex::st_replace_regex(char* expr)
6471
struct st_replace_regex* res;
6472
char* buf,*expr_end;
6475
6393
uint32_t expr_len= strlen(expr);
6476
6394
char last_c = 0;
6477
struct st_regex reg;
6479
res=(st_replace_regex*)malloc(sizeof(*res)+expr_len);
6482
my_init_dynamic_array(&res->regex_arr,sizeof(struct st_regex),128,128);
6484
buf= (char*)res + sizeof(*res);
6485
expr_end= expr + expr_len;
6397
char* buf= new char[expr_len];
6398
char* expr_end= expr + expr_len;
6489
6402
/* for each regexp substitution statement */
6490
6403
while (p < expr_end)
6543
/* done parsing the statement, now place it in regex_arr */
6544
if (insert_dynamic(&res->regex_arr,(unsigned char*) ®))
6545
die("Out of memory");
6455
regex_arr.push_back(reg);
6547
res->odd_buf_len= res->even_buf_len= 8192;
6548
res->even_buf= (char*)malloc(res->even_buf_len);
6549
res->odd_buf= (char*)malloc(res->odd_buf_len);
6550
res->buf= res->even_buf;
6457
odd_buf_len= even_buf_len= buf0_.size();
6458
even_buf= buf0_.data();
6459
odd_buf= buf1_.data();
6556
6465
die("Error parsing replace_regex \"%s\"", expr);
6579
static int multi_reg_replace(struct st_replace_regex* r,char* val)
6487
int st_replace_regex::multi_reg_replace(char* val)
6582
char* in_buf, *out_buf;
6586
out_buf= r->even_buf;
6587
buf_len_p= &r->even_buf_len;
6490
char* out_buf= even_buf;
6491
int* buf_len_p= &even_buf_len;
6590
6494
/* For each substitution, do the replace */
6591
for (i= 0; i < r->regex_arr.elements; i++)
6495
BOOST_FOREACH(regex_arr_t::const_reference i, regex_arr)
6594
6497
char* save_out_buf= out_buf;
6596
get_dynamic(&r->regex_arr,(unsigned char*)&re,i);
6598
if (!reg_replace(&out_buf, buf_len_p, re.pattern, re.replace,
6599
in_buf, re.icase, re.global))
6498
if (!reg_replace(&out_buf, buf_len_p, i.pattern, i.replace,
6499
in_buf, i.icase, i.global))
6601
6501
/* if the buffer has been reallocated, make adjustements */
6602
6502
if (save_out_buf != out_buf)
6604
if (save_out_buf == r->even_buf)
6605
r->even_buf= out_buf;
6504
if (save_out_buf == even_buf)
6607
r->odd_buf= out_buf;
6611
6510
if (in_buf == val)
6614
std::swap(in_buf,out_buf);
6616
buf_len_p= (out_buf == r->even_buf) ? &r->even_buf_len :
6512
std::swap(in_buf, out_buf);
6513
buf_len_p= (out_buf == even_buf) ? &even_buf_len : &odd_buf_len;
6621
return (r->buf == 0);
6633
6528
void do_get_replace_regex(struct st_command *command)
6635
6530
char *expr= command->first_argument;
6636
free_replace_regex();
6637
if (!(glob_replace_regex=init_replace_regex(expr)))
6638
die("Could not init replace_regex");
6531
glob_replace_regex.reset(new st_replace_regex(expr));
6639
6532
command->last_argument= command->end;
6642
void free_replace_regex()
6644
if (glob_replace_regex)
6646
delete_dynamic(&glob_replace_regex->regex_arr);
6647
free(glob_replace_regex->even_buf);
6648
free(glob_replace_regex->odd_buf);
6649
free(glob_replace_regex);
6650
glob_replace_regex=0;
6657
6536
Performs a regex substitution
6759
6638
#define SET_MALLOC_HUNC 64
6760
6639
#define LAST_CHAR_CODE 259
6762
typedef struct st_rep_set {
6644
void internal_set_bit(uint32_t bit);
6645
void internal_clear_bit(uint32_t bit);
6646
void or_bits(const REP_SET *from);
6647
void copy_bits(const REP_SET *from);
6648
int cmp_bits(const REP_SET *set2) const;
6649
int get_next_bit(uint32_t lastpos) const;
6763
6651
uint32_t *bits; /* Pointer to used sets */
6764
6652
short next[LAST_CHAR_CODE]; /* Pointer to next sets */
6765
6653
uint32_t found_len; /* Best match to date */
6766
6654
int found_offset;
6767
6655
uint32_t table_offset;
6768
6656
uint32_t size_of_bits; /* For convinience */
6771
typedef struct st_rep_sets {
6662
int find_set(const REP_SET *find);
6663
void free_last_set();
6665
void make_sets_invisible();
6772
6667
uint32_t count; /* Number of sets */
6773
6668
uint32_t extra; /* Extra sets in buffer */
6774
uint32_t invisible; /* Sets not chown */
6669
uint32_t invisible; /* Sets not shown */
6775
6670
uint32_t size_of_bits;
6776
6671
REP_SET *set,*set_buffer;
6777
6672
uint32_t *bit_buffer;
6780
typedef struct st_found_set {
6781
6677
uint32_t table_offset;
6782
6678
int found_offset;
6785
typedef struct st_follow {
6787
6684
uint32_t table_offset;
6792
int init_sets(REP_SETS *sets,uint32_t states);
6688
int init_sets(REP_SETS *sets, uint32_t states);
6793
6689
REP_SET *make_new_set(REP_SETS *sets);
6794
void make_sets_invisible(REP_SETS *sets);
6795
void free_last_set(REP_SETS *sets);
6796
void free_sets(REP_SETS *sets);
6797
void internal_set_bit(REP_SET *set, uint32_t bit);
6798
void internal_clear_bit(REP_SET *set, uint32_t bit);
6799
void or_bits(REP_SET *to,REP_SET *from);
6800
void copy_bits(REP_SET *to,REP_SET *from);
6801
int cmp_bits(REP_SET *set1,REP_SET *set2);
6802
int get_next_bit(REP_SET *set,uint32_t lastpos);
6803
int find_set(REP_SETS *sets,REP_SET *find);
6804
int find_found(FOUND_SET *found_set,uint32_t table_offset,
6806
uint32_t start_at_word(char * pos);
6807
uint32_t end_of_word(char * pos);
6809
static uint32_t found_sets=0;
6812
static uint32_t replace_len(char * str)
6690
int find_found(FOUND_SET *found_set, uint32_t table_offset, int found_offset);
6692
static uint32_t found_sets= 0;
6694
static uint32_t replace_len(const char *str)
6814
6696
uint32_t len=0;
6707
/* Return 1 if regexp starts with \b or ends with \b*/
6709
static bool start_at_word(const char *pos)
6711
return ((!memcmp(pos, "\\b",2) && pos[2]) || !memcmp(pos, "\\^", 2));
6714
static bool end_of_word(const char *pos)
6716
const char *end= strchr(pos, '\0');
6717
return (end > pos+2 && !memcmp(end-2, "\\b", 2)) || (end >= pos+2 && !memcmp(end-2, "\\$",2));
6825
6720
/* Init a replace structure for further calls */
6827
REPLACE *init_replace(char * *from, char * *to,uint32_t count,
6828
char * word_end_chars)
6722
REPLACE *init_replace(const char **from, const char **to, uint32_t count, char *word_end_chars)
6830
static const int SPACE_CHAR= 256;
6831
static const int START_OF_LINE= 257;
6832
static const int END_OF_LINE= 258;
6724
const int SPACE_CHAR= 256;
6725
const int START_OF_LINE= 257;
6726
const int END_OF_LINE= 258;
6834
6728
uint32_t i,j,states,set_nr,len,result_len,max_length,found_end,bits_set,bit_nr;
6835
6729
int used_sets,chr,default_state;
6836
6730
char used_chars[LAST_CHAR_CODE],is_word_end[256];
6837
char * pos, *to_pos, **to_array;
6839
REP_SET *set,*start_states,*word_states,*new_set;
6840
FOLLOWS *follow,*follow_ptr;
6842
FOUND_SET *found_set;
6843
REPLACE_STRING *rep_str;
6731
char *to_pos, **to_array;
6846
6733
/* Count number of states */
6847
6734
for (i=result_len=max_length=0 , states=2 ; i < count ; i++)
6861
6748
for (i=0 ; word_end_chars[i] ; i++)
6862
6749
is_word_end[(unsigned char) word_end_chars[i]]=1;
6864
if (init_sets(&sets,states))
6752
REP_SET *set,*start_states,*word_states,*new_set;
6753
REPLACE_STRING *rep_str;
6754
if (init_sets(&sets, states))
6867
if (!(found_set= (FOUND_SET*) malloc(sizeof(FOUND_SET)*max_length*count)))
6757
vector<FOUND_SET> found_set(max_length * count);
6873
6758
make_new_set(&sets); /* Set starting set */
6874
make_sets_invisible(&sets); /* Hide previus sets */
6759
sets.make_sets_invisible(); /* Hide previus sets */
6876
6761
word_states=make_new_set(&sets); /* Start of new word */
6877
6762
start_states=make_new_set(&sets); /* This is first state */
6878
if (!(follow=(FOLLOWS*) malloc((states+2)*sizeof(FOLLOWS))))
6763
vector<FOLLOWS> follow(states + 2);
6764
FOLLOWS *follow_ptr= &follow[1];
6885
6765
/* Init follow_ptr[] */
6886
for (i=0, states=1, follow_ptr=follow+1 ; i < count ; i++)
6766
for (i=0, states=1; i < count; i++)
6888
6768
if (from[i][0] == '\\' && from[i][1] == '^')
6890
internal_set_bit(start_states,states+1);
6770
start_states->internal_set_bit(states + 1);
6891
6771
if (!from[i][2])
6893
6773
start_states->table_offset=i;
6897
6777
else if (from[i][0] == '\\' && from[i][1] == '$')
6899
internal_set_bit(start_states,states);
6900
internal_set_bit(word_states,states);
6779
start_states->internal_set_bit(states);
6780
word_states->internal_set_bit(states);
6901
6781
if (!from[i][2] && start_states->table_offset == UINT32_MAX)
6903
6783
start_states->table_offset=i;
6909
internal_set_bit(word_states,states);
6789
word_states->internal_set_bit(states);
6910
6790
if (from[i][0] == '\\' && (from[i][1] == 'b' && from[i][2]))
6911
internal_set_bit(start_states,states+1);
6791
start_states->internal_set_bit(states + 1);
6913
internal_set_bit(start_states,states);
6793
start_states->internal_set_bit(states);
6915
for (pos=from[i], len=0; *pos ; pos++)
6796
for (pos= from[i], len=0; *pos ; pos++)
6917
6798
if (*pos == '\\' && *(pos+1))
6958
for (set_nr=0,pos=0 ; set_nr < sets.count ; set_nr++)
6839
for (set_nr=0; set_nr < sets.count ; set_nr++)
6960
6841
set=sets.set+set_nr;
6961
6842
default_state= 0; /* Start from beginning */
6963
6844
/* If end of found-string not found or start-set with current set */
6965
for (i= UINT32_MAX; (i=get_next_bit(set,i)) ;)
6846
for (i= UINT32_MAX; (i= set->get_next_bit(i)) ;)
6969
if (! default_state)
6970
default_state= find_found(found_set,set->table_offset,
6971
set->found_offset+1);
6848
if (!follow[i].chr && !default_state)
6849
default_state= find_found(&found_set.front(), set->table_offset, set->found_offset+1);
6974
copy_bits(sets.set+used_sets,set); /* Save set for changes */
6851
sets.set[used_sets].copy_bits(set); /* Save set for changes */
6975
6852
if (!default_state)
6976
or_bits(sets.set+used_sets,sets.set); /* Can restart from start */
6853
sets.set[used_sets].or_bits(sets.set); /* Can restart from start */
6978
6855
/* Find all chars that follows current sets */
6979
6856
memset(used_chars, 0, sizeof(used_chars));
6980
for (i= UINT32_MAX; (i=get_next_bit(sets.set+used_sets,i)) ;)
6857
for (i= UINT32_MAX; (i= sets.set[used_sets].get_next_bit(i)) ;)
6982
6859
used_chars[follow[i].chr]=1;
6983
6860
if ((follow[i].chr == SPACE_CHAR && !follow[i+1].chr &&
7016
6893
follow[i].len > found_end)
7017
6894
found_end=follow[i].len;
7018
6895
if (chr && follow[i].chr)
7019
internal_set_bit(new_set,i+1); /* To next set */
6896
new_set->internal_set_bit(i + 1); /* To next set */
7021
internal_set_bit(new_set,i);
6898
new_set->internal_set_bit(i);
7026
6903
new_set->found_len=0; /* Set for testing if first */
7028
for (i= UINT32_MAX; (i=get_next_bit(new_set,i)) ;)
6905
for (i= UINT32_MAX; (i= new_set->get_next_bit(i)) ;)
7030
6907
if ((follow[i].chr == SPACE_CHAR ||
7031
6908
follow[i].chr == END_OF_LINE) && ! chr)
7052
6929
if (bits_set == 1)
7054
set->next[chr] = find_found(found_set,
7055
new_set->table_offset,
7056
new_set->found_offset);
7057
free_last_set(&sets);
6931
set->next[chr] = find_found(&found_set.front(), new_set->table_offset, new_set->found_offset);
6932
sets.free_last_set();
7060
set->next[chr] = find_set(&sets,new_set);
6935
set->next[chr] = sets.find_set(new_set);
7063
set->next[chr] = find_set(&sets,new_set);
6938
set->next[chr] = sets.find_set(new_set);
7068
6943
/* Alloc replace structure for the replace-state-machine */
7070
if ((replace=(REPLACE*) malloc(sizeof(REPLACE)*(sets.count)+
7071
sizeof(REPLACE_STRING)*(found_sets+1)+
7072
sizeof(char *)*count+result_len)))
6945
REPLACE *replace= (REPLACE*)malloc(sizeof(REPLACE) * (sets.count)
6946
+ sizeof(REPLACE_STRING) * (found_sets + 1) + sizeof(char*) * count + result_len);
7074
6949
memset(replace, 0, sizeof(REPLACE)*(sets.count)+
7075
6950
sizeof(REPLACE_STRING)*(found_sets+1)+
7086
6961
rep_str[0].replace_string=0;
7087
6962
for (i=1 ; i <= found_sets ; i++)
7089
pos=from[found_set[i-1].table_offset];
6964
const char *pos= from[found_set[i-1].table_offset];
7090
6965
rep_str[i].found= !memcmp(pos, "\\^", 3) ? 2 : 1;
7091
rep_str[i].replace_string=to_array[found_set[i-1].table_offset];
7092
rep_str[i].to_offset=found_set[i-1].found_offset-start_at_word(pos);
7093
rep_str[i].from_offset=found_set[i-1].found_offset-replace_len(pos)+
6966
rep_str[i].replace_string= to_array[found_set[i-1].table_offset];
6967
rep_str[i].to_offset= found_set[i-1].found_offset-start_at_word(pos);
6968
rep_str[i].from_offset= found_set[i-1].found_offset-replace_len(pos) + end_of_word(pos);
7096
6970
for (i=0 ; i < sets.count ; i++)
7168
7040
return make_new_set(sets);
7171
void free_last_set(REP_SETS *sets)
7178
void free_sets(REP_SETS *sets)
7180
free(sets->set_buffer);
7181
free(sets->bit_buffer);
7185
void internal_set_bit(REP_SET *set, uint32_t bit)
7187
set->bits[bit / WORD_BIT] |= 1 << (bit % WORD_BIT);
7191
void internal_clear_bit(REP_SET *set, uint32_t bit)
7193
set->bits[bit / WORD_BIT] &= ~ (1 << (bit % WORD_BIT));
7198
void or_bits(REP_SET *to,REP_SET *from)
7200
register uint32_t i;
7201
for (i=0 ; i < to->size_of_bits ; i++)
7202
to->bits[i]|=from->bits[i];
7206
void copy_bits(REP_SET *to,REP_SET *from)
7208
memcpy(to->bits,from->bits,
7209
(size_t) (sizeof(uint32_t) * to->size_of_bits));
7212
int cmp_bits(REP_SET *set1,REP_SET *set2)
7214
return memcmp(set1->bits,set2->bits, sizeof(uint32_t) * set1->size_of_bits);
7043
void REP_SETS::free_last_set()
7049
void REP_SETS::free_sets()
7055
void REP_SET::internal_set_bit(uint32_t bit)
7057
bits[bit / WORD_BIT] |= 1 << (bit % WORD_BIT);
7060
void REP_SET::internal_clear_bit(uint32_t bit)
7062
bits[bit / WORD_BIT] &= ~ (1 << (bit % WORD_BIT));
7066
void REP_SET::or_bits(const REP_SET *from)
7068
for (uint32_t i= 0 ; i < size_of_bits; i++)
7069
bits[i]|=from->bits[i];
7072
void REP_SET::copy_bits(const REP_SET *from)
7074
memcpy(bits, from->bits, sizeof(uint32_t) * size_of_bits);
7077
int REP_SET::cmp_bits(const REP_SET *set2) const
7079
return memcmp(bits, set2->bits, sizeof(uint32_t) * size_of_bits);
7218
7082
/* Get next set bit from set. */
7220
int get_next_bit(REP_SET *set,uint32_t lastpos)
7084
int REP_SET::get_next_bit(uint32_t lastpos) const
7222
uint32_t pos,*start,*end,bits;
7224
start=set->bits+ ((lastpos+1) / WORD_BIT);
7225
end=set->bits + set->size_of_bits;
7226
bits=start[0] & ~((1 << ((lastpos+1) % WORD_BIT)) -1);
7228
while (! bits && ++start < end)
7086
uint32_t *start= bits + ((lastpos+1) / WORD_BIT);
7087
uint32_t *end= bits + size_of_bits;
7088
uint32_t bits0= start[0] & ~((1 << ((lastpos+1) % WORD_BIT)) -1);
7090
while (!bits0 && ++start < end)
7232
pos=(uint32_t) (start-set->bits)*WORD_BIT;
7233
while (! (bits & 1))
7094
uint32_t pos= (start - bits) * WORD_BIT;
7095
while (!(bits0 & 1))
7242
7104
free given set, else put in given set in sets and return its
7245
int find_set(REP_SETS *sets,REP_SET *find)
7107
int REP_SETS::find_set(const REP_SET *find)
7248
for (i=0 ; i < sets->count-1 ; i++)
7110
for (; i < count - 1; i++)
7250
if (!cmp_bits(sets->set+i,find))
7112
if (!set[i].cmp_bits(find))
7252
free_last_set(sets);
7263
7125
set->next[] == -1 is reserved for end without replaces.
7266
int find_found(FOUND_SET *found_set,uint32_t table_offset, int found_offset)
7128
int find_found(FOUND_SET *found_set, uint32_t table_offset, int found_offset)
7269
for (i=0 ; (uint32_t) i < found_sets ; i++)
7131
for (; i < found_sets; i++)
7270
7133
if (found_set[i].table_offset == table_offset &&
7271
7134
found_set[i].found_offset == found_offset)
7273
found_set[i].table_offset=table_offset;
7274
found_set[i].found_offset=found_offset;
7137
found_set[i].table_offset= table_offset;
7138
found_set[i].found_offset= found_offset;
7276
return -i-2; /* return new postion */
7279
/* Return 1 if regexp starts with \b or ends with \b*/
7281
uint32_t start_at_word(char * pos)
7283
return (((!memcmp(pos, "\\b",2) && pos[2]) ||
7284
!memcmp(pos, "\\^", 2)) ? 1 : 0);
7287
uint32_t end_of_word(char * pos)
7289
char * end= strchr(pos, '\0');
7290
return ((end > pos+2 && !memcmp(end-2, "\\b", 2)) ||
7291
(end >= pos+2 && !memcmp(end-2, "\\$",2))) ? 1 : 0;
7140
return - i - 2; // return new postion
7294
7143
/****************************************************************************
7367
7216
} /* insert_pointer_name */
7370
/* free pointer array */
7372
void free_pointer_array(POINTER_ARRAY *pa)
7374
if (pa->typelib.count)
7376
pa->typelib.count=0;
7377
free((char*) pa->typelib.type_names);
7378
pa->typelib.type_names=0;
7381
} /* free_pointer_array */
7384
7219
/* Functions that uses replace and replace_regex */
7386
7221
/* Append the string to ds, with optional replace */
7387
void replace_append_mem(string *ds,
7388
const char *val, int len)
7222
void replace_append_mem(string *ds, const char *val, int len)
7390
7224
char *v= strdup(val);
7392
if (glob_replace_regex)
7226
if (glob_replace_regex && !glob_replace_regex->multi_reg_replace(v))
7395
if (!multi_reg_replace(glob_replace_regex, v))
7397
v= glob_replace_regex->buf;
7228
v= glob_replace_regex->buf_;
7402
7231
if (glob_replace)
7404
7233
/* Normal replace */
7405
7234
replace_strings_append(glob_replace, ds, v, len);
7409
7237
ds->append(v, len);