-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsphinxutils.cpp
2455 lines (2087 loc) · 65.4 KB
/
sphinxutils.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// $Id$
//
//
// Copyright (c) 2001-2015, Andrew Aksyonoff
// Copyright (c) 2008-2015, Sphinx Technologies 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. You should have
// received a copy of the GPL license along with this program; if you
// did not, you can find it at http://www.gnu.org/
//
/// @file sphinxutils.cpp
/// Implementations for Sphinx utilities shared classes.
#include "sphinx.h"
#include "sphinxutils.h"
#include "sphinxint.h"
#include "sphinxplugin.h"
#include <ctype.h>
#include <fcntl.h>
#include <errno.h>
#if HAVE_EXECINFO_H
#include <execinfo.h>
#endif
#if USE_WINDOWS
#include <io.h> // for ::open on windows
#include <dbghelp.h>
#pragma comment(linker, "/defaultlib:dbghelp.lib")
#pragma message("Automatically linking with dbghelp.lib")
#else
#include <sys/wait.h>
#include <signal.h>
#include <glob.h>
#ifdef HAVE_DLOPEN
#include <dlfcn.h>
#endif // HAVE_DLOPEN
#ifndef HAVE_DLERROR
#define dlerror() ""
#endif // HAVE_DLERROR
#endif
//////////////////////////////////////////////////////////////////////////
// STRING FUNCTIONS
//////////////////////////////////////////////////////////////////////////
static char * ltrim ( char * sLine )
{
while ( *sLine && isspace(*sLine) )
sLine++;
return sLine;
}
static char * rtrim ( char * sLine )
{
char * p = sLine + strlen(sLine) - 1;
while ( p>=sLine && isspace(*p) )
p--;
p[1] = '\0';
return sLine;
}
static char * trim ( char * sLine )
{
return ltrim ( rtrim ( sLine ) );
}
void sphSplit ( CSphVector<CSphString> & dOut, const char * sIn )
{
if ( !sIn )
return;
const char * p = (char*)sIn;
while ( *p )
{
// skip non-alphas
while ( (*p) && !sphIsAlpha(*p) )
p++;
if ( !(*p) )
break;
// this is my next token
assert ( sphIsAlpha(*p) );
const char * sNext = p;
while ( sphIsAlpha(*p) )
p++;
if ( sNext!=p )
dOut.Add().SetBinary ( sNext, p-sNext );
}
}
void sphSplit ( CSphVector<CSphString> & dOut, const char * sIn, const char * sBounds )
{
if ( !sIn )
return;
const char * p = (char*)sIn;
while ( *p )
{
// skip until the first non-boundary character
const char * sNext = p;
while ( *p && !strchr ( sBounds, *p ) )
p++;
// add the token, skip the char
dOut.Add().SetBinary ( sNext, p-sNext );
p++;
}
}
bool sphWildcardMatch ( const char * sString, const char * sPattern )
{
if ( !sString || !sPattern )
return false;
const char * s = sString;
const char * p = sPattern;
while ( *s )
{
switch ( *p )
{
case '\\':
// escaped char, strict match the next one literally
p++;
if ( *s++!=*p++ )
return false;
break;
case '?':
// match any character
s++;
p++;
break;
case '%':
// gotta match either 0 or 1 characters
// well, lets look ahead and see what we need to match next
p++;
// just a shortcut, %* can be folded to just *
if ( *p=='*' )
break;
// plain char after a hash? check the non-ambiguous cases
if ( !sphIsWild(*p) )
{
if ( s[0]!=*p )
{
// hash does not match 0 chars
// check if we can match 1 char, or it's a no-match
if ( s[1]!=*p )
return false;
s++;
break;
} else
{
// hash matches 0 chars
// check if we could ambiguously match 1 char too, though
if ( s[1]!=*p )
break;
// well, fall through to "scan both options" route
}
}
// could not decide yet
// so just recurse both options
if ( sphWildcardMatch ( s, p ) )
return true;
if ( sphWildcardMatch ( s+1, p ) )
return true;
return false;
case '*':
// skip all the extra stars and question marks
for ( p++; *p=='*' || *p=='?'; p++ )
if ( *p=='?' )
{
s++;
if ( !*s )
return p[1]=='\0';
}
// short-circuit trailing star
if ( !*p )
return true;
// so our wildcard expects a real character
// scan forward for its occurrences and recurse
for ( ;; )
{
if ( !*s )
return false;
if ( *s==*p && sphWildcardMatch ( s+1, p+1 ) )
return true;
s++;
}
break;
default:
// default case, strict match
if ( *s++!=*p++ )
return false;
break;
}
}
// string done
// pattern should be either done too, or a trailing star, or a trailing hash
return p[0]=='\0'
|| ( p[0]=='*' && p[1]=='\0' )
|| ( p[0]=='%' && p[1]=='\0' );
}
//////////////////////////////////////////////////////////////////////////
int64_t CSphConfigSection::GetSize64 ( const char * sKey, int64_t iDefault ) const
{
CSphVariant * pEntry = (*this)( sKey );
if ( !pEntry )
return iDefault;
char sMemLimit[256];
strncpy ( sMemLimit, pEntry->cstr(), sizeof(sMemLimit) );
sMemLimit [ sizeof(sMemLimit)-1 ] = '\0';
int iLen = strlen ( sMemLimit );
if ( !iLen )
return iDefault;
iLen--;
int iScale = 1;
if ( toupper ( sMemLimit[iLen] )=='K' )
{
iScale = 1024;
sMemLimit[iLen] = '\0';
} else if ( toupper ( sMemLimit[iLen] )=='M' )
{
iScale = 1048576;
sMemLimit[iLen] = '\0';
}
char * sErr;
int64_t iRes = strtoll ( sMemLimit, &sErr, 10 );
if ( !*sErr )
{
iRes *= iScale;
} else
{
sphWarning ( "'%s = %s' parse error '%s'", sKey, pEntry->cstr(), sErr );
iRes = iDefault;
}
return iRes;
}
int CSphConfigSection::GetSize ( const char * sKey, int iDefault ) const
{
int64_t iSize = GetSize64 ( sKey, iDefault );
if ( iSize>INT_MAX )
{
iSize = INT_MAX;
sphWarning ( "'%s = " INT64_FMT"' clamped to %d(INT_MAX)", sKey, iSize, INT_MAX );
}
return (int)iSize;
}
//////////////////////////////////////////////////////////////////////////
// CONFIG PARSER
//////////////////////////////////////////////////////////////////////////
/// key flags
enum
{
KEY_DEPRECATED = 1UL<<0,
KEY_LIST = 1UL<<1,
KEY_HIDDEN = 1UL<<2,
KEY_REMOVED = 1UL<<3
};
/// key descriptor for validation purposes
struct KeyDesc_t
{
const char * m_sKey; ///< key name
int m_iFlags; ///< flags
const char * m_sExtra; ///< extra stuff (deprecated name, for now)
};
/// allowed keys for source section
static KeyDesc_t g_dKeysSource[] =
{
{ "type", 0, NULL },
{ "sql_host", 0, NULL },
{ "sql_user", 0, NULL },
{ "sql_pass", 0, NULL },
{ "sql_db", 0, NULL },
{ "sql_port", 0, NULL },
{ "sql_sock", 0, NULL },
{ "mysql_connect_flags", 0, NULL },
{ "mysql_ssl_key", 0, NULL }, // check.pl mysql_ssl
{ "mysql_ssl_cert", 0, NULL }, // check.pl mysql_ssl
{ "mysql_ssl_ca", 0, NULL }, // check.pl mysql_ssl
{ "mssql_winauth", 0, NULL },
{ "mssql_unicode", KEY_REMOVED, NULL },
{ "sql_query_pre", KEY_LIST, NULL },
{ "sql_query", 0, NULL },
{ "sql_query_range", 0, NULL },
{ "sql_range_step", 0, NULL },
{ "sql_query_killlist", 0, NULL },
{ "sql_attr_uint", KEY_LIST, NULL },
{ "sql_attr_bool", KEY_LIST, NULL },
{ "sql_attr_timestamp", KEY_LIST, NULL },
{ "sql_attr_str2ordinal", KEY_REMOVED | KEY_LIST, NULL },
{ "sql_attr_float", KEY_LIST, NULL },
{ "sql_attr_bigint", KEY_LIST, NULL },
{ "sql_attr_multi", KEY_LIST, NULL },
{ "sql_query_post", KEY_LIST, NULL },
{ "sql_query_post_index", KEY_LIST, NULL },
{ "sql_ranged_throttle", 0, NULL },
{ "sql_query_info", KEY_REMOVED, NULL },
{ "xmlpipe_command", 0, NULL },
{ "xmlpipe_field", KEY_LIST, NULL },
{ "xmlpipe_attr_uint", KEY_LIST, NULL },
{ "xmlpipe_attr_timestamp", KEY_LIST, NULL },
{ "xmlpipe_attr_str2ordinal", KEY_REMOVED | KEY_LIST, NULL },
{ "xmlpipe_attr_bool", KEY_LIST, NULL },
{ "xmlpipe_attr_float", KEY_LIST, NULL },
{ "xmlpipe_attr_bigint", KEY_LIST, NULL },
{ "xmlpipe_attr_multi", KEY_LIST, NULL },
{ "xmlpipe_attr_multi_64", KEY_LIST, NULL },
{ "xmlpipe_attr_string", KEY_LIST, NULL },
{ "xmlpipe_attr_wordcount", KEY_REMOVED | KEY_LIST, NULL },
{ "xmlpipe_attr_json", KEY_LIST, NULL },
{ "xmlpipe_field_string", KEY_LIST, NULL },
{ "xmlpipe_field_wordcount", KEY_REMOVED | KEY_LIST, NULL },
{ "xmlpipe_fixup_utf8", 0, NULL },
{ "sql_str2ordinal_column", KEY_LIST | KEY_REMOVED, NULL },
{ "unpack_zlib", KEY_LIST, NULL },
{ "unpack_mysqlcompress", KEY_LIST, NULL },
{ "unpack_mysqlcompress_maxsize", 0, NULL },
{ "odbc_dsn", 0, NULL },
{ "sql_joined_field", KEY_LIST, NULL },
{ "sql_attr_string", KEY_LIST, NULL },
{ "sql_attr_str2wordcount", KEY_REMOVED | KEY_LIST, NULL },
{ "sql_field_string", KEY_LIST, NULL },
{ "sql_field_str2wordcount", KEY_REMOVED | KEY_LIST, NULL },
{ "sql_file_field", KEY_LIST, NULL },
{ "sql_column_buffers", 0, NULL },
{ "sql_attr_json", KEY_LIST, NULL },
{ "hook_connect", KEY_HIDDEN, NULL },
{ "hook_query_range", KEY_HIDDEN, NULL },
{ "hook_post_index", KEY_HIDDEN, NULL },
{ "tsvpipe_command", 0, NULL },
{ "tsvpipe_field", KEY_LIST, NULL },
{ "tsvpipe_attr_uint", KEY_LIST, NULL },
{ "tsvpipe_attr_timestamp", KEY_LIST, NULL },
{ "tsvpipe_attr_bool", KEY_LIST, NULL },
{ "tsvpipe_attr_float", KEY_LIST, NULL },
{ "tsvpipe_attr_bigint", KEY_LIST, NULL },
{ "tsvpipe_attr_multi", KEY_LIST, NULL },
{ "tsvpipe_attr_multi_64", KEY_LIST, NULL },
{ "tsvpipe_attr_string", KEY_LIST, NULL },
{ "tsvpipe_attr_json", KEY_LIST, NULL },
{ "tsvpipe_field_string", KEY_LIST, NULL },
{ "csvpipe_command", 0, NULL },
{ "csvpipe_field", KEY_LIST, NULL },
{ "csvpipe_attr_uint", KEY_LIST, NULL },
{ "csvpipe_attr_timestamp", KEY_LIST, NULL },
{ "csvpipe_attr_bool", KEY_LIST, NULL },
{ "csvpipe_attr_float", KEY_LIST, NULL },
{ "csvpipe_attr_bigint", KEY_LIST, NULL },
{ "csvpipe_attr_multi", KEY_LIST, NULL },
{ "csvpipe_attr_multi_64", KEY_LIST, NULL },
{ "csvpipe_attr_string", KEY_LIST, NULL },
{ "csvpipe_attr_json", KEY_LIST, NULL },
{ "csvpipe_field_string", KEY_LIST, NULL },
{ "csvpipe_delimiter", 0, NULL },
{ NULL, 0, NULL }
};
/// allowed keys for index section
static KeyDesc_t g_dKeysIndex[] =
{
{ "source", KEY_LIST, NULL },
{ "path", 0, NULL },
{ "docinfo", 0, NULL },
{ "mlock", 0, NULL },
{ "morphology", 0, NULL },
{ "stopwords", 0, NULL },
{ "exceptions", 0, NULL },
{ "wordforms", KEY_LIST, NULL },
{ "embedded_limit", 0, NULL },
{ "min_word_len", 0, NULL },
{ "charset_type", KEY_REMOVED, NULL },
{ "charset_table", 0, NULL },
{ "charset_dictpath", 0, NULL }, //coreseek: mmseg's dictionary path
{ "charset_debug", 0, NULL }, //coreseek: debug output tokens
{ "ignore_chars", 0, NULL },
{ "min_prefix_len", 0, NULL },
{ "min_infix_len", 0, NULL },
{ "max_substring_len", 0, NULL },
{ "prefix_fields", 0, NULL },
{ "infix_fields", 0, NULL },
{ "enable_star", KEY_REMOVED, NULL },
{ "ngram_len", 0, NULL },
{ "ngram_chars", 0, NULL },
{ "phrase_boundary", 0, NULL },
{ "phrase_boundary_step", 0, NULL },
{ "ondisk_dict", KEY_REMOVED, NULL },
{ "type", 0, NULL },
{ "local", KEY_LIST, NULL },
{ "agent", KEY_LIST, NULL },
{ "agent_blackhole", KEY_LIST, NULL },
{ "agent_persistent", KEY_LIST, NULL },
{ "agent_connect_timeout", 0, NULL },
{ "ha_strategy", 0, NULL },
{ "agent_query_timeout", 0, NULL },
{ "html_strip", 0, NULL },
{ "html_index_attrs", 0, NULL },
{ "html_remove_elements", 0, NULL },
{ "preopen", 0, NULL },
{ "inplace_enable", 0, NULL },
{ "inplace_hit_gap", 0, NULL },
{ "inplace_docinfo_gap", 0, NULL },
{ "inplace_reloc_factor", 0, NULL },
{ "inplace_write_factor", 0, NULL },
{ "index_exact_words", 0, NULL },
{ "min_stemming_len", 0, NULL },
{ "overshort_step", 0, NULL },
{ "stopword_step", 0, NULL },
{ "blend_chars", 0, NULL },
{ "expand_keywords", 0, NULL },
{ "hitless_words", 0, NULL },
{ "hit_format", KEY_HIDDEN | KEY_DEPRECATED, "default value" },
{ "rt_field", KEY_LIST, NULL },
{ "rt_attr_uint", KEY_LIST, NULL },
{ "rt_attr_bigint", KEY_LIST, NULL },
{ "rt_attr_float", KEY_LIST, NULL },
{ "rt_attr_timestamp", KEY_LIST, NULL },
{ "rt_attr_string", KEY_LIST, NULL },
{ "rt_attr_multi", KEY_LIST, NULL },
{ "rt_attr_multi_64", KEY_LIST, NULL },
{ "rt_attr_json", KEY_LIST, NULL },
{ "rt_attr_bool", KEY_LIST, NULL },
{ "rt_mem_limit", 0, NULL },
{ "dict", 0, NULL },
{ "index_sp", 0, NULL },
{ "index_zones", 0, NULL },
{ "blend_mode", 0, NULL },
{ "regexp_filter", KEY_LIST, NULL },
{ "bigram_freq_words", 0, NULL },
{ "bigram_index", 0, NULL },
{ "index_field_lengths", 0, NULL },
{ "divide_remote_ranges", KEY_HIDDEN, NULL },
{ "stopwords_unstemmed", 0, NULL },
{ "global_idf", 0, NULL },
{ "rlp_context", 0, NULL },
{ "ondisk_attrs", 0, NULL },
{ "index_token_filter", 0, NULL },
{ NULL, 0, NULL }
};
/// allowed keys for indexer section
static KeyDesc_t g_dKeysIndexer[] =
{
{ "mem_limit", 0, NULL },
{ "max_iops", 0, NULL },
{ "max_iosize", 0, NULL },
{ "max_xmlpipe2_field", 0, NULL },
{ "max_file_field_buffer", 0, NULL },
{ "write_buffer", 0, NULL },
{ "on_file_field_error", 0, NULL },
{ "on_json_attr_error", KEY_DEPRECATED, "on_json_attr_error in common{..} section" },
{ "json_autoconv_numbers", KEY_DEPRECATED, "json_autoconv_numbers in common{..} section" },
{ "json_autoconv_keynames", KEY_DEPRECATED, "json_autoconv_keynames in common{..} section" },
{ "lemmatizer_cache", 0, NULL },
{ NULL, 0, NULL }
};
/// allowed keys for searchd section
static KeyDesc_t g_dKeysSearchd[] =
{
{ "address", KEY_REMOVED, NULL },
{ "port", KEY_REMOVED, NULL },
{ "listen", KEY_LIST, NULL },
{ "log", 0, NULL },
{ "query_log", 0, NULL },
{ "read_timeout", 0, NULL },
{ "client_timeout", 0, NULL },
{ "max_children", 0, NULL },
{ "pid_file", 0, NULL },
{ "max_matches", KEY_REMOVED, NULL },
{ "seamless_rotate", 0, NULL },
{ "preopen_indexes", 0, NULL },
{ "unlink_old", 0, NULL },
{ "ondisk_dict_default", KEY_REMOVED, NULL },
{ "attr_flush_period", 0, NULL },
{ "max_packet_size", 0, NULL },
{ "mva_updates_pool", 0, NULL },
{ "max_filters", 0, NULL },
{ "max_filter_values", 0, NULL },
{ "listen_backlog", 0, NULL },
{ "read_buffer", 0, NULL },
{ "read_unhinted", 0, NULL },
{ "max_batch_queries", 0, NULL },
{ "subtree_docs_cache", 0, NULL },
{ "subtree_hits_cache", 0, NULL },
{ "workers", 0, NULL },
{ "prefork", KEY_HIDDEN, NULL },
{ "dist_threads", 0, NULL },
{ "binlog_flush", 0, NULL },
{ "binlog_path", 0, NULL },
{ "binlog_max_log_size", 0, NULL },
{ "thread_stack", 0, NULL },
{ "expansion_limit", 0, NULL },
{ "rt_flush_period", 0, NULL },
{ "query_log_format", 0, NULL },
{ "mysql_version_string", 0, NULL },
{ "plugin_dir", KEY_DEPRECATED, "plugin_dir in common{..} section" },
{ "collation_server", 0, NULL },
{ "collation_libc_locale", 0, NULL },
{ "watchdog", 0, NULL },
{ "prefork_rotation_throttle", 0, NULL },
{ "snippets_file_prefix", 0, NULL },
{ "sphinxql_state", 0, NULL },
{ "rt_merge_iops", 0, NULL },
{ "rt_merge_maxiosize", 0, NULL },
{ "ha_ping_interval", 0, NULL },
{ "ha_period_karma", 0, NULL },
{ "predicted_time_costs", 0, NULL },
{ "persistent_connections_limit", 0, NULL },
{ "ondisk_attrs_default", 0, NULL },
{ "shutdown_timeout", 0, NULL },
{ "query_log_min_msec", 0, NULL },
{ "agent_connect_timeout", 0, NULL },
{ "agent_query_timeout", 0, NULL },
{ "agent_retry_delay", 0, NULL },
{ "agent_retry_count", 0, NULL },
{ NULL, 0, NULL }
};
/// allowed keys for common section
static KeyDesc_t g_dKeysCommon[] =
{
{ "lemmatizer_base", 0, NULL },
{ "on_json_attr_error", 0, NULL },
{ "json_autoconv_numbers", 0, NULL },
{ "json_autoconv_keynames", 0, NULL },
{ "rlp_root", 0, NULL },
{ "rlp_environment", 0, NULL },
{ "rlp_max_batch_size", 0, NULL },
{ "rlp_max_batch_docs", 0, NULL },
{ "plugin_dir", 0, NULL },
{ NULL, 0, NULL }
};
struct KeySection_t
{
const char * m_sKey; ///< key name
KeyDesc_t * m_pSection; ///< section to refer
bool m_bNamed; ///< true if section is named. false if plain
};
static KeySection_t g_dConfigSections[] =
{
{ "source", g_dKeysSource, true },
{ "index", g_dKeysIndex, true },
{ "indexer", g_dKeysIndexer, false },
{ "searchd", g_dKeysSearchd, false },
{ "common", g_dKeysCommon, false },
{ NULL, NULL, false }
};
//////////////////////////////////////////////////////////////////////////
CSphConfigParser::CSphConfigParser ()
: m_sFileName ( "" )
, m_iLine ( -1 )
{
}
bool CSphConfigParser::IsPlainSection ( const char * sKey )
{
assert ( sKey );
const KeySection_t * pSection = g_dConfigSections;
while ( pSection->m_sKey && strcasecmp ( sKey, pSection->m_sKey ) )
++pSection;
return pSection->m_sKey && !pSection->m_bNamed;
}
bool CSphConfigParser::IsNamedSection ( const char * sKey )
{
assert ( sKey );
const KeySection_t * pSection = g_dConfigSections;
while ( pSection->m_sKey && strcasecmp ( sKey, pSection->m_sKey ) )
++pSection;
return pSection->m_sKey && pSection->m_bNamed;
}
bool CSphConfigParser::AddSection ( const char * sType, const char * sName )
{
m_sSectionType = sType;
m_sSectionName = sName;
if ( !m_tConf.Exists ( m_sSectionType ) )
m_tConf.Add ( CSphConfigType(), m_sSectionType ); // FIXME! be paranoid, verify that it returned true
if ( m_tConf[m_sSectionType].Exists ( m_sSectionName ) )
{
snprintf ( m_sError, sizeof(m_sError), "section '%s' (type='%s') already exists", sName, sType );
return false;
}
m_tConf[m_sSectionType].Add ( CSphConfigSection(), m_sSectionName ); // FIXME! be paranoid, verify that it returned true
return true;
}
void CSphConfigParser::AddKey ( const char * sKey, char * sValue )
{
assert ( m_tConf.Exists ( m_sSectionType ) );
assert ( m_tConf[m_sSectionType].Exists ( m_sSectionName ) );
sValue = trim ( sValue );
CSphConfigSection & tSec = m_tConf[m_sSectionType][m_sSectionName];
int iTag = tSec.m_iTag;
tSec.m_iTag++;
if ( tSec(sKey) )
{
if ( tSec[sKey].m_bTag )
{
// override value or list with a new value
SafeDelete ( tSec[sKey].m_pNext ); // only leave the first array element
tSec[sKey] = CSphVariant ( sValue, iTag ); // update its value
tSec[sKey].m_bTag = false; // mark it as overridden
} else
{
// chain to tail, to keep the order
CSphVariant * pTail = &tSec[sKey];
while ( pTail->m_pNext )
pTail = pTail->m_pNext;
pTail->m_pNext = new CSphVariant ( sValue, iTag );
}
} else
{
// just add
tSec.Add ( CSphVariant ( sValue, iTag ), sKey ); // FIXME! be paranoid, verify that it returned true
}
}
bool CSphConfigParser::ValidateKey ( const char * sKey )
{
// get proper descriptor table
// OPTIMIZE! move lookup to AddSection
const KeySection_t * pSection = g_dConfigSections;
const KeyDesc_t * pDesc = NULL;
while ( pSection->m_sKey && m_sSectionType!=pSection->m_sKey )
++pSection;
if ( pSection->m_sKey )
pDesc = pSection->m_pSection;
if ( !pDesc )
{
snprintf ( m_sError, sizeof(m_sError), "unknown section type '%s'", m_sSectionType.cstr() );
return false;
}
// check if the key is known
while ( pDesc->m_sKey && strcasecmp ( pDesc->m_sKey, sKey ) )
pDesc++;
if ( !pDesc->m_sKey )
{
snprintf ( m_sError, sizeof(m_sError), "unknown key name '%s'", sKey );
return false;
}
// warn about deprecate keys
if ( pDesc->m_iFlags & KEY_DEPRECATED )
if ( ++m_iWarnings<=WARNS_THRESH )
fprintf ( stdout, "WARNING: key '%s' is deprecated in %s line %d; use '%s' instead.\n",
sKey, m_sFileName.cstr(), m_iLine, pDesc->m_sExtra );
// warn about list/non-list keys
if (!( pDesc->m_iFlags & KEY_LIST ))
{
CSphConfigSection & tSec = m_tConf[m_sSectionType][m_sSectionName];
if ( tSec(sKey) && !tSec[sKey].m_bTag )
if ( ++m_iWarnings<=WARNS_THRESH )
fprintf ( stdout, "WARNING: key '%s' is not multi-value; value in %s line %d will be ignored.\n", sKey, m_sFileName.cstr(), m_iLine );
}
if ( pDesc->m_iFlags & KEY_REMOVED )
if ( ++m_iWarnings<=WARNS_THRESH )
fprintf ( stdout, "WARNING: key '%s' was permanently removed from Sphinx configuration. Refer to documentation for details.\n", sKey );
return true;
}
#if !USE_WINDOWS
bool TryToExec ( char * pBuffer, const char * szFilename, CSphVector<char> & dResult, char * sError, int iErrorLen )
{
int dPipe[2] = { -1, -1 };
if ( pipe ( dPipe ) )
{
snprintf ( sError, iErrorLen, "pipe() failed (error=%s)", strerror(errno) );
return false;
}
pBuffer = trim ( pBuffer );
int iRead = dPipe[0];
int iWrite = dPipe[1];
int iChild = fork();
if ( iChild==0 )
{
close ( iRead );
close ( STDOUT_FILENO );
dup2 ( iWrite, STDOUT_FILENO );
char * pPtr = pBuffer;
char * pArgs = NULL;
while ( *pPtr )
{
if ( sphIsSpace ( *pPtr ) )
{
*pPtr = '\0';
pArgs = trim ( pPtr+1 );
break;
}
pPtr++;
}
if ( pArgs )
execl ( pBuffer, pBuffer, pArgs, szFilename, (char*)NULL );
else
execl ( pBuffer, pBuffer, szFilename, (char*)NULL );
exit ( 1 );
} else if ( iChild==-1 )
{
snprintf ( sError, iErrorLen, "fork failed: [%d] %s", errno, strerror(errno) );
return false;
}
close ( iWrite );
int iBytesRead, iTotalRead = 0;
const int BUFFER_SIZE = 65536;
dResult.Reset ();
do
{
dResult.Resize ( iTotalRead + BUFFER_SIZE );
for ( ;; )
{
iBytesRead = read ( iRead, (void*)&(dResult [iTotalRead]), BUFFER_SIZE );
if ( iBytesRead==-1 && errno==EINTR ) // we can get SIGCHLD just before eof
continue;
break;
}
iTotalRead += iBytesRead;
}
while ( iBytesRead > 0 );
close ( iRead );
int iStatus, iResult;
do
{
// can be interrupted by pretty much anything (e.g. SIGCHLD from other searchd children)
iResult = waitpid ( iChild, &iStatus, 0 );
// they say this can happen if child exited and SIGCHLD was ignored
// a cleaner one would be to temporary handle it here, but can we be bothered
if ( iResult==-1 && errno==ECHILD )
{
iResult = iChild;
iStatus = 0;
}
if ( iResult==-1 && errno!=EINTR )
{
snprintf ( sError, iErrorLen, "waitpid() failed: [%d] %s", errno, strerror(errno) );
return false;
}
}
while ( iResult!=iChild );
if ( WIFEXITED ( iStatus ) && WEXITSTATUS ( iStatus ) )
{
// FIXME? read stderr and log that too
snprintf ( sError, iErrorLen, "error executing '%s' status = %d", pBuffer, WEXITSTATUS ( iStatus ) );
return false;
}
if ( WIFSIGNALED ( iStatus ) )
{
snprintf ( sError, iErrorLen, "error executing '%s', killed by signal %d", pBuffer, WTERMSIG ( iStatus ) );
return false;
}
if ( iBytesRead < 0 )
{
snprintf ( sError, iErrorLen, "pipe read error: [%d] %s", errno, strerror(errno) );
return false;
}
dResult.Resize ( iTotalRead + 1 );
dResult [iTotalRead] = '\0';
return true;
}
#endif
char * CSphConfigParser::GetBufferString ( char * szDest, int iMax, const char * & szSource )
{
int nCopied = 0;
while ( nCopied < iMax-1 && szSource[nCopied] && ( nCopied==0 || szSource[nCopied-1]!='\n' ) )
{
szDest [nCopied] = szSource [nCopied];
nCopied++;
}
if ( !nCopied )
return NULL;
szSource += nCopied;
szDest [nCopied] = '\0';
return szDest;
}
bool CSphConfigParser::ReParse ( const char * sFileName, const char * pBuffer )
{
CSphConfig tOldConfig = m_tConf;
m_tConf.Reset();
if ( Parse ( sFileName, pBuffer ) )
return true;
m_tConf = tOldConfig;
return false;
}
bool CSphConfigParser::Parse ( const char * sFileName, const char * pBuffer )
{
const int L_STEPBACK = 16;
const int L_TOKEN = 64;
const int L_BUFFER = 8192;
FILE * fp = NULL;
if ( !pBuffer )
{
// open file
fp = fopen ( sFileName, "rb" );
if ( !fp )
return false;
}
// init parser
m_sFileName = sFileName;
m_iLine = 0;
m_iWarnings = 0;
char * p = NULL;
char * pEnd = NULL;
char sBuf [ L_BUFFER ] = { 0 };
char sToken [ L_TOKEN ] = { 0 };
int iToken = 0;
int iCh = -1;
enum { S_TOP, S_SKIP2NL, S_TOK, S_TYPE, S_SEC, S_CHR, S_VALUE, S_SECNAME, S_SECBASE, S_KEY } eState = S_TOP, eStack[8];
int iStack = 0;
int iValue = 0, iValueMax = 65535;
char * sValue = new char [ iValueMax+1 ];
#define LOC_ERROR(_msg) { strncpy ( m_sError, _msg, sizeof(m_sError) ); break; }
#define LOC_ERROR2(_msg,_a) { snprintf ( m_sError, sizeof(m_sError), _msg, _a ); break; }
#define LOC_ERROR3(_msg,_a,_b) { snprintf ( m_sError, sizeof(m_sError), _msg, _a, _b ); break; }
#define LOC_ERROR4(_msg,_a,_b,_c) { snprintf ( m_sError, sizeof(m_sError), _msg, _a, _b, _c ); break; }
#define LOC_PUSH(_new) { assert ( iStack<int(sizeof(eStack)/sizeof(eState)) ); eStack[iStack++] = eState; eState = _new; }
#define LOC_POP() { assert ( iStack>0 ); eState = eStack[--iStack]; }
#define LOC_BACK() { p--; }
m_sError[0] = '\0';
for ( ; ; p++ )
{
// if this line is over, load next line
if ( p>=pEnd )
{
char * szResult = pBuffer ? GetBufferString ( sBuf, L_BUFFER, pBuffer ) : fgets ( sBuf, L_BUFFER, fp );
if ( !szResult )
break; // FIXME! check for read error
m_iLine++;
int iLen = strlen(sBuf);
if ( iLen<=0 )
LOC_ERROR ( "internal error; fgets() returned empty string" );
p = sBuf;
pEnd = sBuf + iLen;
if ( pEnd[-1]!='\n' )
{
if ( iLen==L_BUFFER-1 )
LOC_ERROR ( "line too long" );
}
}
// handle S_TOP state
if ( eState==S_TOP )
{
if ( isspace(*p) ) continue;
if ( *p=='#' )
{
#if !USE_WINDOWS
if ( !pBuffer && m_iLine==1 && p==sBuf && p[1]=='!' )
{
CSphVector<char> dResult;
if ( TryToExec ( p+2, sFileName, dResult, m_sError, sizeof(m_sError) ) )
Parse ( sFileName, &dResult[0] );
break;
} else
#endif
{
LOC_PUSH ( S_SKIP2NL );
continue;
}
}
if ( !sphIsAlpha(*p) ) LOC_ERROR ( "invalid token" );
iToken = 0; LOC_PUSH ( S_TYPE ); LOC_PUSH ( S_TOK ); LOC_BACK(); continue;
}
// handle S_SKIP2NL state
if ( eState==S_SKIP2NL )
{
LOC_POP ();
p = pEnd;
continue;
}
// handle S_TOK state
if ( eState==S_TOK )
{
if ( !iToken && !sphIsAlpha(*p) )LOC_ERROR ( "internal error (non-alpha in S_TOK pos 0)" );
if ( iToken==sizeof(sToken) ) LOC_ERROR ( "token too long" );
if ( !sphIsAlpha(*p) ) { LOC_POP (); sToken [ iToken ] = '\0'; iToken = 0; LOC_BACK(); continue; }
if ( !iToken ) { sToken[0] = '\0'; }
sToken [ iToken++ ] = *p; continue;
}
// handle S_TYPE state
if ( eState==S_TYPE )
{
if ( isspace(*p) ) continue;
if ( *p=='#' ) { LOC_PUSH ( S_SKIP2NL ); continue; }
if ( !sToken[0] ) { LOC_ERROR ( "internal error (empty token in S_TYPE)" ); }
if ( IsPlainSection(sToken) )
{
if ( !AddSection ( sToken, sToken ) )
break;
sToken[0] = '\0';
LOC_POP();
LOC_PUSH ( S_SEC );
LOC_PUSH ( S_CHR );
iCh = '{';
LOC_BACK();
continue;
}
if ( IsNamedSection(sToken) ) { m_sSectionType = sToken; sToken[0] = '\0'; LOC_POP (); LOC_PUSH ( S_SECNAME ); LOC_BACK(); continue; }