-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathconfig_parse_util.c
More file actions
1303 lines (1052 loc) · 39.3 KB
/
Copy pathconfig_parse_util.c
File metadata and controls
1303 lines (1052 loc) · 39.3 KB
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
/*
* Copyright (C) 2015 Richard Burke
*
* 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <limits.h>
#include <string.h>
#include <assert.h>
#include "session.h"
#include "config.h"
#include "config_parse_util.h"
#include "config_parse.h"
#include "status.h"
#include "util.h"
#include "file_type.h"
#include "wed_syntax.h"
#include "theme.h"
/* Bison v2.5 shipped with Ubuntu 12.04.5 LTS doesn't add a yyparse
* declaration to config_parse.h, so we have to add it here to avoid an
* implicit function declaration warning */
int yyparse (Session *sess, ConfigLevel config_level, const char *file_path);
/* Used to process variable assignments in wed block definitions.
* i.e. A list of expected variable assignments can be used to
* validate a block definition */
typedef struct {
char *var_name; /* Variable name */
ValueType value_type; /* Expected type for this variable */
Value *value; /* This is set to the value found */
int optional; /* True if this variable can optionally appear */
const ParseLocation *location; /* This is set to the location
of the assignment */
int value_set; /* True if this variable was found and its value set */
} VariableAssignment;
static void cp_reset_parser_location(void);
static void cp_process_block(Session *, StatementBlockNode *);
static int cp_basic_block_check(Session *, StatementBlockNode *);
static void cp_process_filetype_block(Session *, StatementBlockNode *);
static void cp_process_syntax_block(Session *, StatementBlockNode *);
static SyntaxPattern *cp_process_syntax_pattern_block(Session *,
StatementBlockNode *);
static void cp_process_theme_block(Session *, StatementBlockNode *);
static void cp_process_theme_group_block(Session *, Theme *,
StatementBlockNode *);
static int cp_process_assignment(Session *, StatementNode *,
VariableAssignment *,
size_t expected_vars_num);
static int cp_validate_block_vars(Session *, VariableAssignment *,
size_t expected_vars_num,
const ParseLocation *,
const char *block_name, int non_null_empty);
static Status cp_do_function_call(Session *, const char *function_name,
List *values);
ValueNode *cp_new_valuenode(const ParseLocation *location, Value value)
{
ValueNode *val_node = malloc(sizeof(ValueNode));
RETURN_IF_NULL(val_node);
val_node->type.node_type = NT_VALUE;
val_node->type.location = *location;
val_node->value = value;
return val_node;
}
ValueListNode *cp_new_valuelistnode(const ParseLocation *location, ASTNode *val)
{
assert(val == NULL || val->node_type == NT_VALUE);
ValueListNode *val_list_node = malloc(sizeof(ValueListNode));
RETURN_IF_NULL(val_list_node);
size_t list_size = (val == NULL ? 0 : MAX_CMD_ARG_NUM);
val_list_node->values = list_new_prealloc(list_size);
if (val_list_node->values == NULL) {
free(val_list_node);
return NULL;
}
val_list_node->type.node_type = NT_VALUE_LIST;
val_list_node->type.location = *location;
if (val != NULL) {
list_add(val_list_node->values, val);
}
return val_list_node;
}
IdentifierNode *cp_new_identifiernode(const ParseLocation *location,
const char *name)
{
IdentifierNode *id_node = malloc(sizeof(IdentifierNode));
RETURN_IF_NULL(id_node);
id_node->type.node_type = NT_IDENTIFIER;
id_node->type.location = *location;
id_node->name = strdup(name);
if (id_node->name == NULL) {
free(id_node);
return NULL;
}
return id_node;
}
ExpressionNode *cp_new_expressionnode(const ParseLocation *location,
ASTNodeType node_type,
ASTNode *left, ASTNode *right)
{
ExpressionNode *exp_node = malloc(sizeof(ExpressionNode));
RETURN_IF_NULL(exp_node);
exp_node->type.node_type = node_type;
exp_node->type.location = *location;
exp_node->left = left;
exp_node->right = right;
return exp_node;
}
StatementNode *cp_new_statementnode(const ParseLocation *location,
ASTNode *node)
{
StatementNode *stm_node = malloc(sizeof(StatementNode));
RETURN_IF_NULL(stm_node);
stm_node->type.node_type = NT_STATEMENT;
stm_node->type.location = *location;
stm_node->node = node;
stm_node->next = NULL;
return stm_node;
}
StatementBlockNode *cp_new_statementblocknode(const ParseLocation *location,
const char *block_name,
ASTNode *node)
{
StatementBlockNode *stmb_node = malloc(sizeof(StatementBlockNode));
RETURN_IF_NULL(stmb_node);
stmb_node->type.node_type = NT_STATEMENT_BLOCK;
stmb_node->type.location = *location;
stmb_node->node = node;
stmb_node->block_name = strdup(block_name);
if (stmb_node->block_name == NULL) {
free(stmb_node);
return NULL;
}
return stmb_node;
}
int cp_convert_to_bool_value(const char *svalue, Value *value)
{
if (svalue == NULL || value == NULL) {
return 0;
}
int bool_val;
if (strncmp(svalue, "true", 5) == 0 ||
strncmp(svalue, "1", 2) == 0) {
bool_val = 1;
} else if (strncmp(svalue, "false", 6) == 0 ||
strncmp(svalue, "0", 2) == 0) {
bool_val = 0;
} else {
return 0;
}
*value = BOOL_VAL(bool_val);
return 1;
}
int cp_convert_to_int_value(const char *svalue, Value *value)
{
if (svalue == NULL || value == NULL) {
return 0;
}
char *end_ptr;
errno = 0;
long val = strtol(svalue, &end_ptr, 10);
/* Check strtol was successful. See man strtol for more details */
if ((errno == ERANGE && (val == LONG_MAX || val == LONG_MIN)) ||
(errno != 0 && val == 0) || end_ptr == svalue) {
return 0;
}
*value = INT_VAL(val);
return 1;
}
int cp_convert_to_string_value(const char *svalue, Value *value)
{
if (svalue == NULL || value == NULL) {
return 0;
}
size_t length = strlen(svalue);
if (length < 2 || svalue[0] != '"' ||
svalue[length - 1] != '"') {
return 0;
}
char *processed;
/* Empty string "" */
if (length == 2) {
processed = malloc(sizeof(char));
if (processed == NULL) {
return 0;
}
*processed = '\0';
*value = STR_VAL(processed);
return 1;
}
const char *iter = svalue;
size_t esc_count = 0;
while (*iter++) {
if (*iter == '\\') {
esc_count++;
if (*(iter + 1) == '\\') {
iter++;
}
}
}
/* We will convert the escaped characters into the
* actual characters they represent */
processed = malloc(sizeof(char) * (length - esc_count) - 1);
if (processed == NULL) {
return 0;
}
iter = svalue;
const char *end = svalue + length - 1;
char *proc = processed;
while (++iter != end) {
if (*iter == '\\') {
switch (*(iter + 1)) {
case '\\':
{
*proc++ = '\\';
break;
}
case '"':
{
*proc++ = '"';
break;
}
case 'n':
{
*proc++ = '\n';
break;
}
case 't':
{
*proc++ = '\t';
break;
}
default:
{
break;
}
}
iter++;
} else {
*proc++ = *iter;
}
}
*proc = '\0';
*value = STR_VAL(processed);
return 1;
}
int cp_convert_to_regex_value(const char *rvalue, Value *value)
{
if (rvalue == NULL || value == NULL) {
return 0;
}
size_t length = strlen(rvalue);
if (length < 2 || rvalue[0] != '/') {
return 0;
}
/* Find terminating / character. It's not the last character
* in the string as modifiers can be specified */
const char *regex_end = memrch(rvalue + 1, '/', length - 1);
if (regex_end == NULL) {
return 0;
}
char *processed;
if (length == 2 || rvalue[1] == '/') {
processed = malloc(sizeof(char));
if (processed == NULL) {
return 0;
}
*processed = '\0';
*value = REGEX_VAL(processed, 0);
return 1;
}
const char *iter = rvalue;
size_t esc_count = 0;
while (++iter != regex_end) {
if (*iter == '\\' && *(iter + 1) == '/') {
esc_count++;
iter++;
}
}
processed = malloc(sizeof(char) * (length - esc_count) - 1);
if (processed == NULL) {
return 0;
}
iter = rvalue;
char *proc = processed;
while (++iter != regex_end) {
if (*iter == '\\' && *(iter + 1) == '/') {
*proc++ = '/';
iter++;
} else {
*proc++ = *iter;
}
}
*proc = '\0';
int modifiers = 0;
iter = regex_end;
while (*++iter != '\0') {
switch (*iter) {
case 'i':
{
modifiers |= PCRE_CASELESS;
break;
}
case 'x':
{
modifiers |= PCRE_EXTENDED;
break;
}
case 's':
{
modifiers |= PCRE_DOTALL;
break;
}
case 'm':
{
modifiers |= PCRE_MULTILINE;
break;
}
default:
{
/* TODO Error on invalid flags */
break;
}
}
}
*value = REGEX_VAL(processed, modifiers);
return 1;
}
int cp_convert_to_shell_command_value(const char *cmd_value, Value *value)
{
if (cmd_value == NULL || value == NULL) {
return 0;
}
size_t length = strlen(cmd_value);
if (length < 2 || cmd_value[0] != '!') {
return 0;
}
char *processed = malloc(length);
if (processed == NULL) {
return 0;
}
memcpy(processed, cmd_value + 1, length - 1);
processed[length - 1] = '\0';
*value = CMD_VAL(processed);
return 1;
}
/* Add statement onto the end of statement linked list */
int cp_add_statement_to_list(ASTNode *statememt_list, ASTNode *statememt)
{
assert(statememt_list != NULL);
if (statememt_list == NULL || statememt == NULL) {
return 0;
}
assert(statememt_list->node_type == NT_STATEMENT);
assert(statememt->node_type == NT_STATEMENT);
if (statememt_list->node_type != NT_STATEMENT ||
statememt->node_type != NT_STATEMENT) {
return 0;
}
StatementNode *list = (StatementNode *)statememt_list;
StatementNode *stm_node = (StatementNode *)statememt;
while (list->next != NULL) {
list = list->next;
}
list->next = stm_node;
return 1;
}
int cp_add_value_to_list(ASTNode *val_list, ASTNode *val)
{
assert(val_list != NULL);
if (val_list == NULL || val == NULL) {
return 0;
}
assert(val_list->node_type == NT_VALUE_LIST);
assert(val->node_type == NT_VALUE);
ValueListNode *val_list_node = (ValueListNode *)val_list;
ValueNode *val_node = (ValueNode *)val;
return list_add(val_list_node->values, val_node);
}
int cp_eval_ast(Session *sess, ConfigLevel config_level, ASTNode *node)
{
if (node == NULL) {
return 0;
}
switch (node->node_type) {
case NT_STATEMENT:
{
StatementNode *stm_node = (StatementNode *)node;
while (stm_node != NULL) {
cp_eval_ast(sess, config_level, stm_node->node);
stm_node = stm_node->next;
}
break;
}
case NT_ASSIGNMENT:
{
ExpressionNode *exp_node = (ExpressionNode *)node;
IdentifierNode *id_node = (IdentifierNode *)exp_node->left;
ValueNode *val_node = (ValueNode *)exp_node->right;
/* When at CL_BUFFER allow setting purely CL_SESSION
* variables as there is no ambiguity. This allows us to
* set session level vars such as theme in wed's command
* prompt which is at the CL_BUFFER level */
ConfigLevel tmp_config_level =
cp_determine_config_level(id_node->name,
config_level);
Status status = cf_set_named_var(
CE_VAL(sess, sess->active_buffer),
tmp_config_level,
id_node->name, val_node->value);
se_add_error(sess, cp_convert_to_config_error(status,
&node->location));
break;
}
case NT_FUNCTION_CALL:
{
ExpressionNode *exp_node = (ExpressionNode *)node;
IdentifierNode *id_node = (IdentifierNode *)exp_node->left;
ValueListNode *val_list_node = (ValueListNode *)exp_node->right;
se_add_error(sess, cp_do_function_call(sess, id_node->name,
val_list_node->values));
break;
}
case NT_STATEMENT_BLOCK:
{
StatementBlockNode *stmb_node = (StatementBlockNode *)node;
cp_process_block(sess, stmb_node);
break;
}
default:
return 0;
}
return 1;
}
void cp_free_ast(ASTNode *node)
{
if (node == NULL) {
return;
}
switch (node->node_type) {
case NT_VALUE:
{
ValueNode *val_node = (ValueNode *)node;
va_free_value(val_node->value);
break;
}
case NT_VALUE_LIST:
{
ValueListNode *val_list_node = (ValueListNode *)node;
List *values = val_list_node->values;
for (size_t k = 0; k < list_size(values); k++) {
cp_free_ast(list_get(values, k));
}
list_free(values);
break;
}
case NT_IDENTIFIER:
{
IdentifierNode *id_node = (IdentifierNode *)node;
free(id_node->name);
break;
}
case NT_ASSIGNMENT:
case NT_FUNCTION_CALL:
{
ExpressionNode *exp_node = (ExpressionNode *)node;
cp_free_ast(exp_node->left);
cp_free_ast(exp_node->right);
break;
}
case NT_STATEMENT:
{
StatementNode *stm_node = (StatementNode *)node;
StatementNode *last;
while (stm_node != NULL) {
cp_free_ast(stm_node->node);
last = stm_node;
stm_node = stm_node->next;
free(last);
}
node = NULL;
break;
}
case NT_STATEMENT_BLOCK:
{
StatementBlockNode *stmb_node = (StatementBlockNode *)node;
free(stmb_node->block_name);
cp_free_ast(stmb_node->node);
break;
}
default:
break;
}
if (node != NULL) {
free(node);
}
}
/* This function is invoked by yylex via the YY_USER_ACTION macro,
* before a matched rules action is executed. Here we use this event
* to update yyloc to the position of the just matched token */
void cp_update_parser_location(const char *yytext, const char *file_name)
{
yylloc.file_name = file_name;
yylloc.first_line = yylloc.last_line;
yylloc.first_column = yylloc.last_column;
for (size_t k = 0; yytext[k] != '\0'; k++) {
if (yytext[k] == '\n') {
yylloc.last_line++;
yylloc.last_column = 1;
} else {
yylloc.last_column++;
}
}
}
/* Reset position before parsing a new file or string */
static void cp_reset_parser_location(void)
{
yylloc.first_line = yylloc.last_line = 1;
yylloc.first_column = yylloc.last_column = 1;
}
Status cp_convert_to_config_error(Status error, const ParseLocation *location)
{
if (STATUS_IS_SUCCESS(error)) {
return error;
}
Status config_error = cp_get_config_error(error.error_code, location,
error.msg);
st_free_status(error);
return config_error;
}
/* Add location information to error message */
Status cp_get_config_error(ErrorCode error_code, const ParseLocation *location,
const char *format, ...)
{
assert(!is_null_or_empty(format));
va_list arg_ptr;
va_start(arg_ptr, format);
Status status;
if (location->file_name != NULL) {
char new_format[MAX_ERROR_MSG_SIZE];
snprintf(new_format, MAX_ERROR_MSG_SIZE, "%s:%d:%d: %s",
location->file_name, location->first_line,
location->first_column, format);
status = st_get_custom_error(error_code, new_format, arg_ptr);
} else {
status = st_get_custom_error(error_code, format, arg_ptr);
}
va_end(arg_ptr);
return status;
}
/* Called when Bison encounters a syntax error */
void yyerror(Session *sess, ConfigLevel config_level, const char *file_name,
char const *error)
{
(void)config_level;
(void)file_name;
se_add_error(sess, cp_get_config_error(ERR_INVALID_CONFIG_SYNTAX,
&yylloc, error));
}
Status cp_parse_config_file(Session *sess, ConfigLevel config_level,
const char *config_file_path)
{
assert(!is_null_or_empty(config_file_path));
FILE *config_file = fopen(config_file_path, "rb");
if (config_file == NULL) {
return st_get_error(ERR_UNABLE_TO_OPEN_FILE,
"Unable to open file %s for reading",
config_file_path);
}
cp_start_scan_file(sess->cfg_buffer_stack, config_file);
cp_reset_parser_location();
/* Invoke Bison parser */
int parse_status = yyparse(sess, config_level, config_file_path);
cp_finish_scan(sess->cfg_buffer_stack);
if (parse_status != 0) {
return st_get_error(ERR_FAILED_TO_PARSE_CONFIG_FILE,
"Failed to fully config parse file %s",
config_file_path);
}
return STATUS_SUCCESS;
}
Status cp_parse_config_string(Session *sess, ConfigLevel config_level,
const char *str)
{
assert(!is_null_or_empty(str));
cp_start_scan_string(sess->cfg_buffer_stack, str);
cp_reset_parser_location();
int parse_status = yyparse(sess, config_level, NULL);
cp_finish_scan(sess->cfg_buffer_stack);
if (parse_status != 0) {
return st_get_error(ERR_FAILED_TO_PARSE_CONFIG_INPUT,
"Failed to fully parse config input");
}
return STATUS_SUCCESS;
}
static void cp_process_block(Session *sess, StatementBlockNode *stmb_node)
{
if (!is_null_or_empty(stmb_node->block_name)) {
if (strncmp(stmb_node->block_name, "filetype", 9) == 0) {
cp_process_filetype_block(sess, stmb_node);
return;
} else if (strncmp(stmb_node->block_name, "syntax", 7) == 0) {
cp_process_syntax_block(sess, stmb_node);
return;
} else if (strncmp(stmb_node->block_name, "theme", 6) == 0) {
cp_process_theme_block(sess, stmb_node);
return;
}
}
se_add_error(sess, cp_get_config_error(ERR_INVALID_BLOCK_IDENTIFIER,
&stmb_node->type.location,
"Invalid block identifier: \"%s\"",
stmb_node->block_name));
}
/* Basic check performed for all block definitions */
static int cp_basic_block_check(Session *sess, StatementBlockNode *stmb_node)
{
if (stmb_node->node == NULL) {
se_add_error(sess, cp_get_config_error(ERR_EMPTY_BLOCK_DEFINITION,
&stmb_node->type.location,
"Empty block definition"));
return 0;
} else if (stmb_node->node->node_type != NT_STATEMENT) {
se_add_error(sess, cp_get_config_error(ERR_INVALID_CONFIG_ENTRY,
&stmb_node->node->location,
"Invalid block entry"));
return 0;
}
return 1;
}
static void cp_process_filetype_block(Session *sess,
StatementBlockNode *stmb_node)
{
if (!cp_basic_block_check(sess, stmb_node)) {
return;
}
Value name;
Value display_name;
Value file_pattern;
Value file_content = REGEX_VAL(NULL, 0);
/* Variable assignements expected in a filetype block */
VariableAssignment expected_vars[] = {
{ "name" , VAL_TYPE_STR , &name , 0, NULL, 0 },
{ "display_name", VAL_TYPE_STR , &display_name, 0, NULL, 0 },
{ "file_pattern", VAL_TYPE_REGEX, &file_pattern, 0, NULL, 0 },
{ "file_content", VAL_TYPE_REGEX, &file_content, 1, NULL, 0 }
};
size_t expected_vars_num =
ARRAY_SIZE(expected_vars, VariableAssignment);
StatementNode *stm_node = (StatementNode *)stmb_node->node;
while (stm_node != NULL) {
if (stm_node->node->node_type == NT_ASSIGNMENT) {
/* Process the assignment we've found and update
* the relevant expected variable value if valid */
cp_process_assignment(sess, stm_node, expected_vars,
expected_vars_num);
stm_node = stm_node->next;
} else {
/* There should only be assignments in a filetype block */
se_add_error(sess, cp_get_config_error(ERR_INVALID_CONFIG_ENTRY,
&stm_node->node->location,
"Invalid statement "
"in filetype block"));
return;
}
}
/* Validate all expected values have been set */
if (!cp_validate_block_vars(sess, expected_vars, expected_vars_num,
&stmb_node->type.location, "filetype", 1)) {
return;
}
FileType *file_type;
Status status = ft_init(&file_type, SVAL(name), SVAL(display_name),
&RVAL(file_pattern), &RVAL(file_content));
if (!STATUS_IS_SUCCESS(status)) {
se_add_error(sess, status);
return;
}
/* Session keeps track of all loaded filetypes */
status = se_add_filetype_def(sess, file_type);
if (!STATUS_IS_SUCCESS(status)) {
ft_free(file_type);
se_add_error(sess, status);
}
}
static void cp_process_syntax_block(Session *sess,
StatementBlockNode *stmb_node)
{
if (!cp_basic_block_check(sess, stmb_node)) {
return;
}
Value name;
VariableAssignment expected_vars[] = {
{ "name", VAL_TYPE_STR, &name, 0, NULL, 0 }
};
const size_t expected_vars_num =
ARRAY_SIZE(expected_vars, VariableAssignment);
WedSyntaxDefinition *wed_def = NULL;
SyntaxPattern *syn_current = NULL;
SyntaxPattern *syn_first = NULL;
StatementNode *stm_node = (StatementNode *)stmb_node->node;
while (stm_node != NULL) {
if (stm_node->node->node_type == NT_ASSIGNMENT) {
cp_process_assignment(sess, stm_node, expected_vars,
expected_vars_num);
stm_node = stm_node->next;
} else if (stm_node->node->node_type == NT_STATEMENT_BLOCK) {
SyntaxPattern *syn_pattern = cp_process_syntax_pattern_block(sess,
(StatementBlockNode *)stm_node->node);
if (syn_pattern != NULL) {
/* Build linked list of SyntaxPattern's */
if (syn_first == NULL) {
syn_first = syn_current = syn_pattern;
} else {
syn_current->next = syn_pattern;
syn_current = syn_pattern;
}
}
stm_node = stm_node->next;
} else {
se_add_error(sess, cp_get_config_error(ERR_INVALID_CONFIG_ENTRY,
&stm_node->node->location,
"Invalid statement in "
"syntax block"));
goto cleanup;
}
}
if (syn_first == NULL) {
se_add_error(sess, cp_get_config_error(ERR_INVALID_CONFIG_ENTRY,
&stmb_node->type.location,
"Synax block contains no "
"valid pattern blocks"));
return;
}
if (!cp_validate_block_vars(sess, expected_vars, expected_vars_num,
&stmb_node->type.location, "syntax", 1)) {
goto cleanup;
}
wed_def = (WedSyntaxDefinition *)ws_new(sess);
if (wed_def == NULL) {
se_add_error(sess, OUT_OF_MEMORY("Unable to allocate SyntaxDefinition"));
goto cleanup;
}
wed_def->patterns = syn_first;
SyntaxManager *sm = &sess->sm;
if (!hashmap_set(sm->syn_defs, SVAL(name), wed_def)) {
se_add_error(sess, OUT_OF_MEMORY("Unable to save SyntaxDefinition"));
goto cleanup;
}
return;
cleanup:
while (syn_first != NULL) {
syn_current = syn_first->next;
ws_free_pattern(syn_first);
syn_first = syn_current;
}
free(wed_def);
}
static SyntaxPattern *cp_process_syntax_pattern_block(Session *sess,
StatementBlockNode *stmb_node)
{
if (!cp_basic_block_check(sess, stmb_node)) {
return NULL;
}
Value regex;
Value type;
VariableAssignment expected_vars[] = {
{ "regex", VAL_TYPE_REGEX, ®ex, 0, NULL, 0 },
{ "type" , VAL_TYPE_STR , &type , 0, NULL, 0 }
};
size_t expected_vars_num =
ARRAY_SIZE(expected_vars, VariableAssignment);
StatementNode *stm_node = (StatementNode *)stmb_node->node;
while (stm_node != NULL) {
if (stm_node->node->node_type == NT_ASSIGNMENT) {
cp_process_assignment(sess, stm_node, expected_vars,
expected_vars_num);
stm_node = stm_node->next;
} else {
se_add_error(sess, cp_get_config_error(ERR_INVALID_CONFIG_ENTRY,
&stm_node->node->location,
"Invalid statement in "
"pattern block"));
return NULL;
}
}
if (!cp_validate_block_vars(sess, expected_vars, expected_vars_num,
&stmb_node->type.location, "pattern", 1)) {
return NULL;
}
SyntaxToken token;
if (!sy_str_to_token(&token, SVAL(type))) {
se_add_error(sess, cp_get_config_error(ERR_INVALID_CONFIG_ENTRY,