-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsrgs.c
1683 lines (1593 loc) · 46.8 KB
/
srgs.c
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
/*
* mod_rayo for FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
* Copyright (C) 2013-2015, Grasshopper
*
* Version: MPL 1.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is mod_rayo for FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
*
* The Initial Developer of the Original Code is Grasshopper
* Portions created by the Initial Developer are Copyright (C)
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Chris Rienzo <[email protected]>
*
* srgs.c -- Parses / converts / matches SRGS grammars
*
*/
#include <switch.h>
#include <iksemel.h>
#include <pcre.h>
#include "srgs.h"
#define MAX_RECURSION 100
#define MAX_TAGS 1024
/** function to handle tag attributes */
typedef int (* tag_attribs_fn)(struct srgs_grammar *, char **);
/** function to handle tag CDATA */
typedef int (* tag_cdata_fn)(struct srgs_grammar *, char *, size_t);
/**
* Tag definition
*/
struct tag_def {
tag_attribs_fn attribs_fn;
tag_cdata_fn cdata_fn;
switch_bool_t is_root;
switch_hash_t *children_tags;
};
/**
* library configuration
*/
typedef struct {
/** true if initialized */
switch_bool_t init;
/** Mapping of tag name to definition */
switch_hash_t *tag_defs;
/** library memory pool */
switch_memory_pool_t *pool;
} srgs_globals;
static srgs_globals globals = { 0 };
/**
* SRGS node types
*/
enum srgs_node_type {
/** anything */
SNT_ANY,
/** <grammar> */
SNT_GRAMMAR,
/** <rule> */
SNT_RULE,
/** <one-of> */
SNT_ONE_OF,
/** <item> */
SNT_ITEM,
/** <ruleref> unresolved reference to node */
SNT_UNRESOLVED_REF,
/** <ruleref> resolved reference to node */
SNT_REF,
/** <item> string */
SNT_STRING,
/** <tag> */
SNT_TAG,
/** <lexicon> */
SNT_LEXICON,
/** <example> */
SNT_EXAMPLE,
/** <token> */
SNT_TOKEN,
/** <meta> */
SNT_META,
/** <metadata> */
SNT_METADATA
};
/**
* <rule> value
*/
struct rule_value {
char is_public;
char *id;
char *regex;
};
/**
* <item> value
*/
struct item_value {
int repeat_min;
int repeat_max;
const char *weight;
int tag;
};
/**
* <ruleref> value
*/
union ref_value {
struct srgs_node *node;
char *uri;
};
/**
* A node in the SRGS parse tree
*/
struct srgs_node {
/** Name of node */
const char *name;
/** Type of node */
enum srgs_node_type type;
/** True if node has been inspected for loops */
char visited;
/** Node value */
union {
char *root;
const char *string;
union ref_value ref;
struct rule_value rule;
struct item_value item;
} value;
/** parent node */
struct srgs_node *parent;
/** child node */
struct srgs_node *child;
/** sibling node */
struct srgs_node *next;
/** number of child nodes */
int num_children;
/** tag handling data */
struct tag_def *tag_def;
};
/**
* A parsed grammar
*/
struct srgs_grammar {
/** grammar memory pool */
switch_memory_pool_t *pool;
/** current node being parsed */
struct srgs_node *cur;
/** rule names mapped to node */
switch_hash_t *rules;
/** possible matching tags */
const char *tags[MAX_TAGS + 1];
/** number of tags */
int tag_count;
/** grammar encoding */
char *encoding;
/** grammar language */
char *language;
/** true if digit grammar */
int digit_mode;
/** grammar parse tree root */
struct srgs_node *root;
/** root rule */
struct srgs_node *root_rule;
/** compiled grammar regex */
pcre *compiled_regex;
/** grammar in regex format */
char *regex;
/** grammar in JSGF format */
char *jsgf;
/** grammar as JSGF file */
char *jsgf_file_name;
/** synchronizes access to this grammar */
switch_mutex_t *mutex;
/** optional uuid for logging */
const char *uuid;
};
/**
* The SRGS SAX parser
*/
struct srgs_parser {
/** parser memory pool */
switch_memory_pool_t *pool;
/** grammar cache */
switch_hash_t *cache;
/** cache mutex */
switch_mutex_t *mutex;
/** optional uuid for logging */
const char *uuid;
};
/**
* Convert entity name to node type
* @param name of entity
* @return the type or ANY
*/
static enum srgs_node_type string_to_node_type(char *name)
{
if (!strcmp("grammar", name)) {
return SNT_GRAMMAR;
}
if (!strcmp("item", name)) {
return SNT_ITEM;
}
if (!strcmp("one-of", name)) {
return SNT_ONE_OF;
}
if (!strcmp("ruleref", name)) {
return SNT_UNRESOLVED_REF;
}
if (!strcmp("rule", name)) {
return SNT_RULE;
}
if (!strcmp("tag", name)) {
return SNT_TAG;
}
if (!strcmp("lexicon", name)) {
return SNT_LEXICON;
}
if (!strcmp("example", name)) {
return SNT_EXAMPLE;
}
if (!strcmp("token", name)) {
return SNT_TOKEN;
}
if (!strcmp("meta", name)) {
return SNT_META;
}
if (!strcmp("metadata", name)) {
return SNT_METADATA;
}
return SNT_ANY;
}
/**
* Log node
*/
static void sn_log_node_open(struct srgs_node *node)
{
switch (node->type) {
case SNT_ANY:
case SNT_METADATA:
case SNT_META:
case SNT_TOKEN:
case SNT_EXAMPLE:
case SNT_LEXICON:
case SNT_TAG:
case SNT_ONE_OF:
case SNT_GRAMMAR:
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG1, "<%s>\n", node->name);
return;
case SNT_RULE:
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG1, "<rule id='%s' scope='%s'>\n", node->value.rule.id, node->value.rule.is_public ? "public" : "private");
return;
case SNT_ITEM:
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG1, "<item repeat='%i'>\n", node->value.item.repeat_min);
return;
case SNT_UNRESOLVED_REF:
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG1, "<ruleref (unresolved) uri='%s'\n", node->value.ref.uri);
return;
case SNT_REF:
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG1, "<ruleref uri='#%s'>\n", node->value.ref.node->value.rule.id);
return;
case SNT_STRING:
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG1, "%s\n", node->value.string);
return;
}
}
/**
* Log node
*/
static void sn_log_node_close(struct srgs_node *node)
{
switch (node->type) {
case SNT_GRAMMAR:
case SNT_RULE:
case SNT_ONE_OF:
case SNT_ITEM:
case SNT_REF:
case SNT_TAG:
case SNT_LEXICON:
case SNT_EXAMPLE:
case SNT_TOKEN:
case SNT_META:
case SNT_METADATA:
case SNT_ANY:
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG1, "</%s>\n", node->name);
return;
case SNT_UNRESOLVED_REF:
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG1, "</ruleref (unresolved)>\n");
return;
case SNT_STRING:
return;
}
}
/**
* Create a new node
* @param pool to use
* @param name of node
* @param type of node
* @return the node
*/
static struct srgs_node *sn_new(switch_memory_pool_t *pool, const char *name, enum srgs_node_type type)
{
struct srgs_node *node = switch_core_alloc(pool, sizeof(*node));
node->name = switch_core_strdup(pool, name);
node->type = type;
return node;
}
/**
* @param node to search
* @return the last sibling of node
*/
static struct srgs_node *sn_find_last_sibling(struct srgs_node *node)
{
if (node && node->next) {
return sn_find_last_sibling(node->next);
}
return node;
}
/**
* Add child node
* @param pool to use
* @param parent node to add child to
* @param name the child node name
* @param type the child node type
* @return the child node
*/
static struct srgs_node *sn_insert(switch_memory_pool_t *pool, struct srgs_node *parent, const char *name, enum srgs_node_type type)
{
struct srgs_node *sibling = parent ? sn_find_last_sibling(parent->child) : NULL;
struct srgs_node *child = sn_new(pool, name, type);
if (parent) {
parent->num_children++;
child->parent = parent;
}
if (sibling) {
sibling->next = child;
} else if (parent) {
parent->child = child;
}
return child;
}
/**
* Add string child node
* @param pool to use
* @param parent node to add string to
* @param string to add - this function does not copy the string
* @return the string child node
*/
static struct srgs_node *sn_insert_string(switch_memory_pool_t *pool, struct srgs_node *parent, char *string)
{
struct srgs_node *child = sn_insert(pool, parent, string, SNT_STRING);
child->value.string = string;
return child;
}
/**
* Tag def destructor
*/
static void destroy_tag_def(void *ptr)
{
struct tag_def *tag = (struct tag_def *) ptr;
if (tag->children_tags) {
switch_core_hash_destroy(&tag->children_tags);
}
}
/**
* Add a definition for a tag
* @param tag the name
* @param attribs_fn the function to handle the tag attributes
* @param cdata_fn the function to handler the tag CDATA
* @param children_tags comma-separated list of valid child tag names
* @return the definition
*/
static struct tag_def *add_tag_def(const char *tag, tag_attribs_fn attribs_fn, tag_cdata_fn cdata_fn, const char *children_tags)
{
struct tag_def *def = switch_core_alloc(globals.pool, sizeof(*def));
switch_core_hash_init(&def->children_tags);
if (!zstr(children_tags)) {
char *children_tags_dup = switch_core_strdup(globals.pool, children_tags);
char *tags[32] = { 0 };
int tag_count = switch_separate_string(children_tags_dup, ',', tags, sizeof(tags) / sizeof(tags[0]));
if (tag_count) {
int i;
for (i = 0; i < tag_count; i++) {
switch_core_hash_insert(def->children_tags, tags[i], tags[i]);
}
}
}
def->attribs_fn = attribs_fn;
def->cdata_fn = cdata_fn;
def->is_root = SWITCH_FALSE;
switch_core_hash_insert_destructor(globals.tag_defs, tag, def, destroy_tag_def);
return def;
}
/**
* Add a definition for a root tag
* @param tag the name
* @param attribs_fn the function to handle the tag attributes
* @param cdata_fn the function to handler the tag CDATA
* @param children_tags comma-separated list of valid child tag names
* @return the definition
*/
static struct tag_def *add_root_tag_def(const char *tag, tag_attribs_fn attribs_fn, tag_cdata_fn cdata_fn, const char *children_tags)
{
struct tag_def *def = add_tag_def(tag, attribs_fn, cdata_fn, children_tags);
def->is_root = SWITCH_TRUE;
return def;
}
/**
* Handle tag attributes
* @param parser the parser
* @param name the tag name
* @param atts the attributes
* @return IKS_OK if OK IKS_BADXML on parse failure
*/
static int process_tag(struct srgs_grammar *grammar, const char *name, char **atts)
{
struct srgs_node *cur = grammar->cur;
if (cur->tag_def->is_root && cur->parent == NULL) {
/* no parent for ROOT tags */
return cur->tag_def->attribs_fn(grammar, atts);
} else if (!cur->tag_def->is_root && cur->parent) {
/* check if this child is allowed by parent node */
struct tag_def *parent_def = cur->parent->tag_def;
if (switch_core_hash_find(parent_def->children_tags, "ANY") ||
switch_core_hash_find(parent_def->children_tags, name)) {
return cur->tag_def->attribs_fn(grammar, atts);
} else {
switch_log_printf(SWITCH_CHANNEL_UUID_LOG(grammar->uuid), SWITCH_LOG_INFO, "<%s> cannot be a child of <%s>\n", name, cur->parent->name);
}
} else if (cur->tag_def->is_root && cur->parent != NULL) {
switch_log_printf(SWITCH_CHANNEL_UUID_LOG(grammar->uuid), SWITCH_LOG_INFO, "<%s> must be the root element\n", name);
} else {
switch_log_printf(SWITCH_CHANNEL_UUID_LOG(grammar->uuid), SWITCH_LOG_INFO, "<%s> cannot be a root element\n", name);
}
return IKS_BADXML;
}
/**
* Handle tag attributes that are ignored
* @param grammar the grammar
* @param atts the attributes
* @return IKS_OK
*/
static int process_attribs_ignore(struct srgs_grammar *grammar, char **atts)
{
return IKS_OK;
}
/**
* Handle CDATA that is ignored
* @param grammar the grammar
* @param data the CDATA
* @param len the CDATA length
* @return IKS_OK
*/
static int process_cdata_ignore(struct srgs_grammar *grammar, char *data, size_t len)
{
return IKS_OK;
}
/**
* Handle CDATA that is not allowed
* @param grammar the grammar
* @param data the CDATA
* @param len the CDATA length
* @return IKS_BADXML if any printable characters
*/
static int process_cdata_bad(struct srgs_grammar *grammar, char *data, size_t len)
{
int i;
for (i = 0; i < len; i++) {
if (isgraph(data[i])) {
switch_log_printf(SWITCH_CHANNEL_UUID_LOG(grammar->uuid), SWITCH_LOG_INFO, "Unexpected CDATA for <%s>\n", grammar->cur->name);
return IKS_BADXML;
}
}
return IKS_OK;
}
/**
* Process <rule> attributes
* @param grammar the grammar state
* @param atts the attributes
* @return IKS_OK if ok
*/
static int process_rule(struct srgs_grammar *grammar, char **atts)
{
struct srgs_node *rule = grammar->cur;
rule->value.rule.is_public = 0;
rule->value.rule.id = NULL;
if (atts) {
int i = 0;
while (atts[i]) {
if (!strcmp("scope", atts[i])) {
rule->value.rule.is_public = !zstr(atts[i + 1]) && !strcmp("public", atts[i + 1]);
} else if (!strcmp("id", atts[i])) {
if (!zstr(atts[i + 1])) {
rule->value.rule.id = switch_core_strdup(grammar->pool, atts[i + 1]);
}
}
i += 2;
}
}
if (zstr(rule->value.rule.id)) {
switch_log_printf(SWITCH_CHANNEL_UUID_LOG(grammar->uuid), SWITCH_LOG_INFO, "Missing rule ID: %s\n", rule->value.rule.id);
return IKS_BADXML;
}
if (switch_core_hash_find(grammar->rules, rule->value.rule.id)) {
switch_log_printf(SWITCH_CHANNEL_UUID_LOG(grammar->uuid), SWITCH_LOG_INFO, "Duplicate rule ID: %s\n", rule->value.rule.id);
return IKS_BADXML;
}
switch_core_hash_insert(grammar->rules, rule->value.rule.id, rule);
return IKS_OK;
}
/**
* Process <ruleref> attributes
* @param grammar the grammar state
* @param atts the attributes
* @return IKS_OK if ok
*/
static int process_ruleref(struct srgs_grammar *grammar, char **atts)
{
struct srgs_node *ruleref = grammar->cur;
if (atts) {
int i = 0;
while (atts[i]) {
if (!strcmp("uri", atts[i])) {
char *uri = atts[i + 1];
if (zstr(uri)) {
switch_log_printf(SWITCH_CHANNEL_UUID_LOG(grammar->uuid), SWITCH_LOG_INFO, "Empty <ruleref> uri\n");
return IKS_BADXML;
}
/* only allow local reference */
if (uri[0] != '#' || strlen(uri) < 2) {
switch_log_printf(SWITCH_CHANNEL_UUID_LOG(grammar->uuid), SWITCH_LOG_INFO, "Only local rule refs allowed\n");
return IKS_BADXML;
}
ruleref->value.ref.uri = switch_core_strdup(grammar->pool, uri);
return IKS_OK;
}
i += 2;
}
}
return IKS_OK;
}
/**
* Process <item> attributes
* @param grammar the grammar state
* @param atts the attributes
* @return IKS_OK if ok
*/
static int process_item(struct srgs_grammar *grammar, char **atts)
{
struct srgs_node *item = grammar->cur;
item->value.item.repeat_min = 1;
item->value.item.repeat_max = 1;
item->value.item.weight = NULL;
if (atts) {
int i = 0;
while (atts[i]) {
if (!strcmp("repeat", atts[i])) {
/* repeats of 0 are not supported by this code */
char *repeat = atts[i + 1];
if (zstr(repeat)) {
switch_log_printf(SWITCH_CHANNEL_UUID_LOG(grammar->uuid), SWITCH_LOG_INFO, "Empty <item> repeat atribute\n");
return IKS_BADXML;
}
if (switch_is_number(repeat)) {
/* single number */
int repeat_val = atoi(repeat);
if (repeat_val < 1) {
switch_log_printf(SWITCH_CHANNEL_UUID_LOG(grammar->uuid), SWITCH_LOG_INFO, "<item> repeat must be >= 0\n");
return IKS_BADXML;
}
item->value.item.repeat_min = repeat_val;
item->value.item.repeat_max = repeat_val;
} else {
/* range */
char *min = switch_core_strdup(grammar->pool, repeat);
char *max = strchr(min, '-');
if (max) {
*max = '\0';
max++;
} else {
switch_log_printf(SWITCH_CHANNEL_UUID_LOG(grammar->uuid), SWITCH_LOG_INFO, "<item> repeat must be a number or range\n");
return IKS_BADXML;
}
if (switch_is_number(min) && (switch_is_number(max) || zstr(max))) {
int min_val = atoi(min);
int max_val = zstr(max) ? INT_MAX : atoi(max);
/* max must be >= min and > 0
min must be >= 0 */
if ((max_val <= 0) || (max_val < min_val) || (min_val < 0)) {
switch_log_printf(SWITCH_CHANNEL_UUID_LOG(grammar->uuid), SWITCH_LOG_INFO, "<item> repeat range invalid\n");
return IKS_BADXML;
}
item->value.item.repeat_min = min_val;
item->value.item.repeat_max = max_val;
} else {
switch_log_printf(SWITCH_CHANNEL_UUID_LOG(grammar->uuid), SWITCH_LOG_INFO, "<item> repeat range is not a number\n");
return IKS_BADXML;
}
}
} else if (!strcmp("weight", atts[i])) {
const char *weight = atts[i + 1];
if (zstr(weight) || !switch_is_number(weight) || atof(weight) < 0) {
switch_log_printf(SWITCH_CHANNEL_UUID_LOG(grammar->uuid), SWITCH_LOG_INFO, "<item> weight is not a number >= 0\n");
return IKS_BADXML;
}
item->value.item.weight = switch_core_strdup(grammar->pool, weight);
}
i += 2;
}
}
return IKS_OK;
}
/**
* Process <grammar> attributes
* @param grammar the grammar state
* @param atts the attributes
* @return IKS_OK if ok
*/
static int process_grammar(struct srgs_grammar *grammar, char **atts)
{
if (grammar->root) {
switch_log_printf(SWITCH_CHANNEL_UUID_LOG(grammar->uuid), SWITCH_LOG_INFO, "Only one <grammar> tag allowed\n");
return IKS_BADXML;
}
grammar->root = grammar->cur;
if (atts) {
int i = 0;
while (atts[i]) {
if (!strcmp("mode", atts[i])) {
char *mode = atts[i + 1];
if (zstr(mode)) {
switch_log_printf(SWITCH_CHANNEL_UUID_LOG(grammar->uuid), SWITCH_LOG_INFO, "<grammar> mode is missing\n");
return IKS_BADXML;
}
grammar->digit_mode = !strcasecmp(mode, "dtmf");
} else if(!strcmp("encoding", atts[i])) {
char *encoding = atts[i + 1];
if (zstr(encoding)) {
switch_log_printf(SWITCH_CHANNEL_UUID_LOG(grammar->uuid), SWITCH_LOG_INFO, "<grammar> encoding is empty\n");
return IKS_BADXML;
}
grammar->encoding = switch_core_strdup(grammar->pool, encoding);
} else if (!strcmp("language", atts[i])) {
char *language = atts[i + 1];
if (zstr(language)) {
switch_log_printf(SWITCH_CHANNEL_UUID_LOG(grammar->uuid), SWITCH_LOG_INFO, "<grammar> language is empty\n");
return IKS_BADXML;
}
grammar->language = switch_core_strdup(grammar->pool, language);
} else if (!strcmp("root", atts[i])) {
char *root = atts[i + 1];
if (zstr(root)) {
switch_log_printf(SWITCH_CHANNEL_UUID_LOG(grammar->uuid), SWITCH_LOG_INFO, "<grammar> root is empty\n");
return IKS_BADXML;
}
grammar->cur->value.root = switch_core_strdup(grammar->pool, root);
}
i += 2;
}
}
return IKS_OK;
}
/**
* Process a tag
*/
static int tag_hook(void *user_data, char *name, char **atts, int type)
{
int result = IKS_OK;
struct srgs_grammar *grammar = (struct srgs_grammar *)user_data;
if (type == IKS_OPEN || type == IKS_SINGLE) {
enum srgs_node_type ntype = string_to_node_type(name);
grammar->cur = sn_insert(grammar->pool, grammar->cur, name, ntype);
grammar->cur->tag_def = switch_core_hash_find(globals.tag_defs, name);
if (!grammar->cur->tag_def) {
grammar->cur->tag_def = switch_core_hash_find(globals.tag_defs, "ANY");
}
result = process_tag(grammar, name, atts);
sn_log_node_open(grammar->cur);
}
if (type == IKS_CLOSE || type == IKS_SINGLE) {
sn_log_node_close(grammar->cur);
grammar->cur = grammar->cur->parent;
}
return result;
}
/**
* Process <tag> CDATA
* @param grammar the grammar
* @param data the CDATA
* @param len the CDATA length
* @return IKS_OK
*/
static int process_cdata_tag(struct srgs_grammar *grammar, char *data, size_t len)
{
struct srgs_node *item = grammar->cur->parent;
if (item && item->type == SNT_ITEM) {
if (grammar->tag_count < MAX_TAGS) {
/* grammar gets the tag name, item gets the unique tag number */
char *tag = switch_core_alloc(grammar->pool, sizeof(char) * (len + 1));
tag[len] = '\0';
strncpy(tag, data, len);
grammar->tags[++grammar->tag_count] = tag;
item->value.item.tag = grammar->tag_count;
} else {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "too many <tag>s\n");
return IKS_BADXML;
}
}
return IKS_OK;
}
/**
* Process CDATA grammar tokens
* @param grammar the grammar
* @param data the CDATA
* @param len the CDATA length
* @return IKS_OK
*/
static int process_cdata_tokens(struct srgs_grammar *grammar, char *data, size_t len)
{
struct srgs_node *string = grammar->cur;
int i;
if (grammar->digit_mode) {
for (i = 0; i < len; i++) {
if (isdigit(data[i]) || data[i] == '#' || data[i] == '*') {
char *digit = switch_core_alloc(grammar->pool, sizeof(char) * 2);
digit[0] = data[i];
digit[1] = '\0';
string = sn_insert_string(grammar->pool, string, digit);
sn_log_node_open(string);
}
}
} else {
char *data_dup = switch_core_alloc(grammar->pool, sizeof(char) * (len + 1));
char *start = data_dup;
char *end = start + len - 1;
memcpy(data_dup, data, len);
/* remove start whitespace */
for (; start && *start && !isgraph(*start); start++) {
}
if (!zstr(start)) {
/* remove end whitespace */
for (; end != start && *end && !isgraph(*end); end--) {
*end = '\0';
}
if (!zstr(start)) {
sn_insert_string(grammar->pool, string, start);
}
}
}
return IKS_OK;
}
/**
* Process cdata
* @param user_data the grammar
* @param data the CDATA
* @param len the CDATA length
* @return IKS_OK
*/
static int cdata_hook(void *user_data, char *data, size_t len)
{
struct srgs_grammar *grammar = (struct srgs_grammar *)user_data;
if (!grammar) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Missing grammar\n");
return IKS_BADXML;
}
if (grammar->cur) {
if (grammar->cur->tag_def) {
return grammar->cur->tag_def->cdata_fn(grammar, data, len);
}
switch_log_printf(SWITCH_CHANNEL_UUID_LOG(grammar->uuid), SWITCH_LOG_INFO, "Missing definition for <%s>\n", grammar->cur->name);
return IKS_BADXML;
}
return IKS_OK;
}
/**
* Create a new parsed grammar
* @param parser
* @return the grammar
*/
struct srgs_grammar *srgs_grammar_new(struct srgs_parser *parser)
{
switch_memory_pool_t *pool = NULL;
struct srgs_grammar *grammar = NULL;
switch_core_new_memory_pool(&pool);
grammar = switch_core_alloc(pool, sizeof (*grammar));
grammar->pool = pool;
grammar->root = NULL;
grammar->cur = NULL;
grammar->uuid = (parser && !zstr(parser->uuid)) ? switch_core_strdup(pool, parser->uuid) : "";
switch_core_hash_init(&grammar->rules);
switch_mutex_init(&grammar->mutex, SWITCH_MUTEX_NESTED, pool);
return grammar;
}
/**
* Destroy a parsed grammar
* @param grammar the grammar
*/
static void srgs_grammar_destroy(struct srgs_grammar *grammar)
{
switch_memory_pool_t *pool = grammar->pool;
if (grammar->compiled_regex) {
pcre_free(grammar->compiled_regex);
}
if (grammar->jsgf_file_name) {
switch_file_remove(grammar->jsgf_file_name, pool);
}
switch_core_hash_destroy(&grammar->rules);
switch_core_destroy_memory_pool(&pool);
}
/**
* Create a new parser.
* @param uuid optional uuid for logging
* @return the created parser
*/
struct srgs_parser *srgs_parser_new(const char *uuid)
{
switch_memory_pool_t *pool = NULL;
struct srgs_parser *parser = NULL;
switch_core_new_memory_pool(&pool);
if (pool) {
parser = switch_core_alloc(pool, sizeof(*parser));
parser->pool = pool;
parser->uuid = zstr(uuid) ? "" : switch_core_strdup(pool, uuid);
switch_core_hash_init(&parser->cache);
switch_mutex_init(&parser->mutex, SWITCH_MUTEX_NESTED, pool);
}
return parser;
}
/**
* Destroy the parser.
* @param parser to destroy
*/
void srgs_parser_destroy(struct srgs_parser *parser)
{
switch_memory_pool_t *pool = parser->pool;
switch_hash_index_t *hi = NULL;
if (parser->cache) {
/* clean up all cached grammars */
for (hi = switch_core_hash_first(parser->cache); hi; hi = switch_core_hash_next(&hi)) {
struct srgs_grammar *grammar = NULL;
const void *key;
void *val;
switch_core_hash_this(hi, &key, NULL, &val);
grammar = (struct srgs_grammar *)val;
switch_assert(grammar);
srgs_grammar_destroy(grammar);
}
switch_core_hash_destroy(&parser->cache);
}
switch_core_destroy_memory_pool(&pool);
}
/**
* Create regexes
* @param grammar the grammar
* @param node root node
* @param stream set to NULL
* @return 1 if successful
*/
static int create_regexes(struct srgs_grammar *grammar, struct srgs_node *node, switch_stream_handle_t *stream)
{
sn_log_node_open(node);
switch (node->type) {
case SNT_GRAMMAR:
if (node->child) {
int num_rules = 0;
struct srgs_node *child = node->child;
if (grammar->root_rule) {
if (!create_regexes(grammar, grammar->root_rule, NULL)) {
return 0;
}
grammar->regex = switch_core_sprintf(grammar->pool, "^%s$", grammar->root_rule->value.rule.regex);
} else {
switch_stream_handle_t new_stream = { 0 };
SWITCH_STANDARD_STREAM(new_stream);
if (node->num_children > 1) {
new_stream.write_function(&new_stream, "%s", "^(?:");
} else {
new_stream.write_function(&new_stream, "%s", "^");
}
for (; child; child = child->next) {
if (!create_regexes(grammar, child, &new_stream)) {
switch_safe_free(new_stream.data);
return 0;
}
if (child->type == SNT_RULE && child->value.rule.is_public) {
if (num_rules > 0) {
new_stream.write_function(&new_stream, "%s", "|");
}
new_stream.write_function(&new_stream, "%s", child->value.rule.regex);
num_rules++;
}
}
if (node->num_children > 1) {
new_stream.write_function(&new_stream, "%s", ")$");
} else {
new_stream.write_function(&new_stream, "%s", "$");
}
grammar->regex = switch_core_strdup(grammar->pool, new_stream.data);
switch_safe_free(new_stream.data);
}
switch_log_printf(SWITCH_CHANNEL_UUID_LOG(grammar->uuid), SWITCH_LOG_DEBUG, "document regex = %s\n", grammar->regex);
}
break;
case SNT_RULE:
if (node->value.rule.regex) {
return 1;
} else if (node->child) {
struct srgs_node *item = node->child;
switch_stream_handle_t new_stream = { 0 };
SWITCH_STANDARD_STREAM(new_stream);
for (; item; item = item->next) {
if (!create_regexes(grammar, item, &new_stream)) {
switch_log_printf(SWITCH_CHANNEL_UUID_LOG(grammar->uuid), SWITCH_LOG_DEBUG, "%s regex failed = %s\n", node->value.rule.id, node->value.rule.regex);
switch_safe_free(new_stream.data);
return 0;
}
}
node->value.rule.regex = switch_core_strdup(grammar->pool, new_stream.data);
switch_log_printf(SWITCH_CHANNEL_UUID_LOG(grammar->uuid), SWITCH_LOG_DEBUG, "%s regex = %s\n", node->value.rule.id, node->value.rule.regex);
switch_safe_free(new_stream.data);
}
break;
case SNT_STRING: {
int i;
for (i = 0; i < strlen(node->value.string); i++) {
switch (node->value.string[i]) {
case '[':
case '\\':
case '^':
case '$':
case '.':
case '|':
case '?':
case '*':
case '+':
case '(':
case ')':
/* escape special PCRE regex characters */
stream->write_function(stream, "\\%c", node->value.string[i]);
break;
default:
stream->write_function(stream, "%c", node->value.string[i]);
break;
}
}
if (node->child) {
if (!create_regexes(grammar, node->child, stream)) {
return 0;
}