-
Notifications
You must be signed in to change notification settings - Fork 932
/
xmldoc.c
2996 lines (2619 loc) · 82.2 KB
/
xmldoc.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
/*
* Asterisk -- An open source telephony toolkit.
*
* Copyright (C) 2008, Eliel C. Sardanons (LU1ALY) <eliels@gmail.com>
*
* See http://www.asterisk.org for more information about
* the Asterisk project. Please do not directly contact
* any of the maintainers of this project for assistance;
* the project provides a web site, mailing lists and IRC
* channels for your use.
*
* This program is free software, distributed under the terms of
* the GNU General Public License Version 2. See the LICENSE file
* at the top of the source tree.
*/
/*! \file
*
* \brief XML Documentation API
*
* \author Eliel C. Sardanons (LU1ALY) <eliels@gmail.com>
*
* libxml2 http://www.xmlsoft.org/
*/
/*** MODULEINFO
<support_level>core</support_level>
***/
#include "asterisk.h"
#include "asterisk/_private.h"
#include "asterisk/paths.h"
#include "asterisk/linkedlists.h"
#include "asterisk/config.h"
#include "asterisk/term.h"
#include "asterisk/astobj2.h"
#include "asterisk/xmldoc.h"
#include "asterisk/cli.h"
#ifdef AST_XML_DOCS
/*! \brief Default documentation language. */
static const char default_documentation_language[] = "en_US";
/*! \brief Number of columns to print when showing the XML documentation with a
* 'core show application/function *' CLI command. Used in text wrapping.*/
static const int xmldoc_text_columns = 79;
/*! \brief XML documentation language. */
static char documentation_language[6];
/*! \brief XML documentation tree */
struct documentation_tree {
char *filename; /*!< XML document filename. */
struct ast_xml_doc *doc; /*!< Open document pointer. */
AST_RWLIST_ENTRY(documentation_tree) entry;
};
static char *xmldoc_get_syntax_cmd(struct ast_xml_node *fixnode, const char *name, int printname);
static int xmldoc_parse_enumlist(struct ast_xml_node *fixnode, const char *tabs, struct ast_str **buffer);
static void xmldoc_parse_parameter(struct ast_xml_node *fixnode, const char *tabs, struct ast_str **buffer);
static int xmldoc_parse_info(struct ast_xml_node *node, const char *tabs, const char *posttabs, struct ast_str **buffer);
static int xmldoc_parse_para(struct ast_xml_node *node, const char *tabs, const char *posttabs, struct ast_str **buffer);
static int xmldoc_parse_specialtags(struct ast_xml_node *fixnode, const char *tabs, const char *posttabs, struct ast_str **buffer);
/*!
* \brief Container of documentation trees
*
* \note A RWLIST is a sufficient container type to use here for now.
* However, some changes will need to be made to implement ref counting
* if reload support is added in the future.
*/
static AST_RWLIST_HEAD_STATIC(xmldoc_tree, documentation_tree);
static const struct strcolorized_tags {
const char *init; /*!< Replace initial tag with this string. */
const char *end; /*!< Replace end tag with this string. */
const int colorfg; /*!< Foreground color. */
const char *inittag; /*!< Initial tag description. */
const char *endtag; /*!< Ending tag description. */
} colorized_tags[] = {
{ "<", ">", COLOR_GREEN, "<replaceable>", "</replaceable>" },
{ "\'", "\'", COLOR_BLUE, "<literal>", "</literal>" },
{ "*", "*", COLOR_RED, "<emphasis>", "</emphasis>" },
{ "\"", "\"", COLOR_YELLOW, "<filename>", "</filename>" },
{ "\"", "\"", COLOR_CYAN, "<directory>", "</directory>" },
{ "${", "}", COLOR_GREEN, "<variable>", "</variable>" },
{ "", "", COLOR_BLUE, "<value>", "</value>" },
{ "", "", COLOR_BLUE, "<enum>", "</enum>" },
{ "\'", "\'", COLOR_GRAY, "<astcli>", "</astcli>" },
/* Special tags */
{ "", "", COLOR_YELLOW, "<note>", "</note>" },
{ "", "", COLOR_RED, "<warning>", "</warning>" },
{ "", "", COLOR_WHITE, "<example>", "</example>" },
{ "", "", COLOR_GRAY, "<exampletext>", "</exampletext>"},
};
static const struct strspecial_tags {
const char *tagname; /*!< Special tag name. */
const char *init; /*!< Print this at the beginning. */
const char *end; /*!< Print this at the end. */
} special_tags[] = {
{ "note", "<note>NOTE:</note> ", "" },
{ "warning", "<warning>WARNING!!!:</warning> ", "" },
{ "example", "<example>Example:</example> ", "" },
};
/*!
* \internal
* \brief Calculate the space in bytes used by a format string
* that will be passed to a sprintf function.
*
* \param postbr The format string to use to calculate the length.
*
* \retval The postbr length.
*/
static int xmldoc_postbrlen(const char *postbr)
{
int postbrreallen = 0, i;
size_t postbrlen;
if (!postbr) {
return 0;
}
postbrlen = strlen(postbr);
for (i = 0; i < postbrlen; i++) {
if (postbr[i] == '\t') {
postbrreallen += 8 - (postbrreallen % 8);
} else {
postbrreallen++;
}
}
return postbrreallen;
}
/*!
* \internal
* \brief Setup postbr to be used while wrapping the text.
* Add to postbr array all the spaces and tabs at the beginning of text.
*
* \param postbr output array.
* \param len text array length.
* \param text Text with format string before the actual string.
*/
static void xmldoc_setpostbr(char *postbr, size_t len, const char *text)
{
int c, postbrlen = 0;
if (!text) {
return;
}
for (c = 0; c < len; c++) {
if (text[c] == '\t' || text[c] == ' ') {
postbr[postbrlen++] = text[c];
} else {
break;
}
}
postbr[postbrlen] = '\0';
}
/*!
* \internal
* \brief Justify a text to a number of columns.
*
* \param text Input text to be justified.
* \param columns Number of columns to preserve in the text.
*
* \retval NULL on error.
* \retval The wrapped text.
*/
static char *xmldoc_string_wrap(const char *text, int columns)
{
struct ast_str *tmp;
char *ret, postbr[160];
int count, i, textlen, postbrlen, lastbreak;
/* sanity check */
if (!text || columns <= 0) {
ast_log(LOG_WARNING, "Passing wrong arguments while trying to wrap the text\n");
return NULL;
}
tmp = ast_str_create(strlen(text) * 3);
if (!tmp) {
return NULL;
}
/* Check for blanks and tabs and put them in postbr. */
xmldoc_setpostbr(postbr, sizeof(postbr), text);
postbrlen = xmldoc_postbrlen(postbr);
count = 0;
lastbreak = 0;
textlen = strlen(text);
for (i = 0; i < textlen; i++) {
if (text[i] == '\n') {
xmldoc_setpostbr(postbr, sizeof(postbr), &text[i] + 1);
postbrlen = xmldoc_postbrlen(postbr);
count = 0;
lastbreak = 0;
} else if (text[i] == ESC) {
/* Walk over escape sequences without counting them. */
do {
ast_str_append(&tmp, 0, "%c", text[i]);
i++;
} while (i < textlen && text[i] != 'm');
} else {
if (text[i] == ' ') {
lastbreak = i;
}
count++;
}
if (count > columns) {
/* Seek backwards if it was at most 30 characters ago. */
int back = i - lastbreak;
if (lastbreak && back > 0 && back < 30) {
ast_str_truncate(tmp, -back);
i = lastbreak; /* go back a bit */
}
ast_str_append(&tmp, 0, "\n%s", postbr);
count = postbrlen;
lastbreak = 0;
} else {
ast_str_append(&tmp, 0, "%c", text[i]);
}
}
ret = ast_strdup(ast_str_buffer(tmp));
ast_free(tmp);
return ret;
}
char *ast_xmldoc_printable(const char *bwinput, int withcolors)
{
struct ast_str *colorized;
char *wrapped = NULL;
int i, c, len, colorsection;
char *tmp;
size_t bwinputlen;
static const int base_fg = COLOR_CYAN;
if (!bwinput) {
return NULL;
}
bwinputlen = strlen(bwinput);
if (!(colorized = ast_str_create(256))) {
return NULL;
}
if (withcolors) {
ast_term_color_code(&colorized, base_fg, 0);
if (!colorized) {
return NULL;
}
}
for (i = 0; i < bwinputlen; i++) {
colorsection = 0;
/* Check if we are at the beginning of a tag to be colorized. */
for (c = 0; c < ARRAY_LEN(colorized_tags); c++) {
if (strncasecmp(bwinput + i, colorized_tags[c].inittag, strlen(colorized_tags[c].inittag))) {
continue;
}
if (!(tmp = strcasestr(bwinput + i + strlen(colorized_tags[c].inittag), colorized_tags[c].endtag))) {
continue;
}
len = tmp - (bwinput + i + strlen(colorized_tags[c].inittag));
/* Setup color */
if (withcolors) {
if (ast_opt_light_background) {
/* Turn off *bright* colors */
ast_term_color_code(&colorized, colorized_tags[c].colorfg & 0x7f, 0);
} else {
/* Turn on *bright* colors */
ast_term_color_code(&colorized, colorized_tags[c].colorfg | 0x80, 0);
}
if (!colorized) {
return NULL;
}
}
/* copy initial string replace */
ast_str_append(&colorized, 0, "%s", colorized_tags[c].init);
if (!colorized) {
return NULL;
}
{
char buf[len + 1];
ast_copy_string(buf, bwinput + i + strlen(colorized_tags[c].inittag), sizeof(buf));
ast_str_append(&colorized, 0, "%s", buf);
}
if (!colorized) {
return NULL;
}
/* copy the ending string replace */
ast_str_append(&colorized, 0, "%s", colorized_tags[c].end);
if (!colorized) {
return NULL;
}
/* Continue with the last color. */
if (withcolors) {
ast_term_color_code(&colorized, base_fg, 0);
if (!colorized) {
return NULL;
}
}
i += len + strlen(colorized_tags[c].endtag) + strlen(colorized_tags[c].inittag) - 1;
colorsection = 1;
break;
}
if (!colorsection) {
ast_str_append(&colorized, 0, "%c", bwinput[i]);
if (!colorized) {
return NULL;
}
}
}
if (withcolors) {
ast_str_append(&colorized, 0, "%s", ast_term_reset());
if (!colorized) {
return NULL;
}
}
/* Wrap the text, notice that string wrap will avoid cutting an ESC sequence. */
wrapped = xmldoc_string_wrap(ast_str_buffer(colorized), xmldoc_text_columns);
ast_free(colorized);
return wrapped;
}
/*!
* \internal
* \brief Cleanup spaces and tabs after a \\n
*
* \param text String to be cleaned up.
* \param output buffer (not already allocated).
* \param lastspaces Remove last spaces in the string.
* \param maintain_newlines Preserve new line characters (\\n \\r) discovered in the string
*/
static void xmldoc_string_cleanup(const char *text, struct ast_str **output, int lastspaces, int maintain_newlines)
{
int i;
size_t textlen;
if (!text) {
*output = NULL;
return;
}
textlen = strlen(text);
*output = ast_str_create(textlen);
if (!(*output)) {
ast_log(LOG_ERROR, "Problem allocating output buffer\n");
return;
}
for (i = 0; i < textlen; i++) {
if (text[i] == '\n' || text[i] == '\r') {
if (maintain_newlines) {
ast_str_append(output, 0, "%c", text[i]);
}
/* remove spaces/tabs/\n after a \n. */
while (text[i + 1] == '\t' || text[i + 1] == '\r' || text[i + 1] == '\n') {
i++;
}
ast_str_append(output, 0, " ");
continue;
} else {
ast_str_append(output, 0, "%c", text[i]);
}
}
/* remove last spaces (we don't want always to remove the trailing spaces). */
if (lastspaces) {
ast_str_trim_blanks(*output);
}
}
/*!
* \internal
* \brief Check if the given attribute on the given node matches the given value.
*
* \param node the node to match
* \param attr the name of the attribute
* \param value the expected value of the attribute
*
* \retval true if the given attribute contains the given value
* \retval false if the given attribute does not exist or does not contain the given value
*/
static int xmldoc_attribute_match(struct ast_xml_node *node, const char *attr, const char *value)
{
const char *attr_value = ast_xml_get_attribute(node, attr);
int match = attr_value && !strcmp(attr_value, value);
ast_xml_free_attr(attr_value);
return match;
}
/*!
* \internal
* \brief Get the application/function node for 'name' application/function with language 'language'
* and module 'module' if we don't find any, get the first application
* with 'name' no matter which language or module.
*
* \param type 'application', 'function', ...
* \param name Application or Function name.
* \param module Module item is in.
* \param language Try to get this language (if not found try with en_US)
*
* \retval NULL on error.
* \retval A node of type ast_xml_node.
*/
static struct ast_xml_node *xmldoc_get_node(const char *type, const char *name, const char *module, const char *language)
{
struct ast_xml_node *node = NULL;
struct ast_xml_node *first_match = NULL;
struct ast_xml_node *lang_match = NULL;
struct documentation_tree *doctree;
AST_RWLIST_RDLOCK(&xmldoc_tree);
AST_LIST_TRAVERSE(&xmldoc_tree, doctree, entry) {
/* the core xml documents have priority over thirdparty document. */
node = ast_xml_get_root(doctree->doc);
if (!node) {
break;
}
node = ast_xml_node_get_children(node);
while ((node = ast_xml_find_element(node, type, "name", name))) {
if (!ast_xml_node_get_children(node)) {
/* ignore empty nodes */
node = ast_xml_node_get_next(node);
continue;
}
if (!first_match) {
first_match = node;
}
/* Check language */
if (xmldoc_attribute_match(node, "language", language)) {
if (!lang_match) {
lang_match = node;
}
/* if module is empty we have a match */
if (ast_strlen_zero(module)) {
break;
}
/* Check module */
if (xmldoc_attribute_match(node, "module", module)) {
break;
}
}
node = ast_xml_node_get_next(node);
}
/* if we matched lang and module return this match */
if (node) {
break;
}
/* we didn't match lang and module, just return the first
* result with a matching language if we have one */
if (lang_match) {
node = lang_match;
break;
}
/* we didn't match with only the language, just return the
* first match */
if (first_match) {
node = first_match;
break;
}
}
AST_RWLIST_UNLOCK(&xmldoc_tree);
return node;
}
/*!
* \internal
* \brief Helper function used to build the syntax, it allocates the needed buffer (or reallocates it),
* and based on the reverse value it makes use of fmt to print the parameter list inside the
* realloced buffer (syntax).
*
* \param reverse We are going backwards while generating the syntax?
* \param len Current length of 'syntax' buffer.
* \param syntax Output buffer for the concatenated values.
* \param fmt A format string that will be used in a sprintf call.
*/
static void __attribute__((format(printf, 4, 5))) xmldoc_reverse_helper(int reverse, int *len, char **syntax, const char *fmt, ...)
{
int totlen;
int tmpfmtlen;
char *tmpfmt;
char *new_syntax;
char tmp;
va_list ap;
va_start(ap, fmt);
if (ast_vasprintf(&tmpfmt, fmt, ap) < 0) {
va_end(ap);
return;
}
va_end(ap);
tmpfmtlen = strlen(tmpfmt);
totlen = *len + tmpfmtlen + 1;
new_syntax = ast_realloc(*syntax, totlen);
if (!new_syntax) {
ast_free(tmpfmt);
return;
}
*syntax = new_syntax;
if (reverse) {
memmove(*syntax + tmpfmtlen, *syntax, *len);
/* Save this char, it will be overwritten by the \0 of strcpy. */
tmp = (*syntax)[0];
strcpy(*syntax, tmpfmt);
/* Restore the already saved char. */
(*syntax)[tmpfmtlen] = tmp;
(*syntax)[totlen - 1] = '\0';
} else {
strcpy(*syntax + *len, tmpfmt);
}
*len = totlen - 1;
ast_free(tmpfmt);
}
/*!
* \internal
* \brief Check if the passed node has 'what' tags inside it.
*
* \param fixnode Root node to search 'what' elements.
* \param what Node name to search inside node.
*
* \retval 1 If a 'what' element is found inside 'node'.
* \retval 0 If no 'what' is found inside 'node'.
*/
static int xmldoc_has_inside(struct ast_xml_node *fixnode, const char *what)
{
struct ast_xml_node *node = fixnode;
for (node = ast_xml_node_get_children(fixnode); node; node = ast_xml_node_get_next(node)) {
if (!strcasecmp(ast_xml_node_get_name(node), what)) {
return 1;
}
}
return 0;
}
/*!
* \internal
* \brief Check if the passed node has at least one node inside it.
*
* \param fixnode Root node to search node elements.
*
* \retval 1 If a node element is found inside 'node'.
* \retval 0 If no node is found inside 'node'.
*/
static int xmldoc_has_nodes(struct ast_xml_node *fixnode)
{
struct ast_xml_node *node = fixnode;
for (node = ast_xml_node_get_children(fixnode); node; node = ast_xml_node_get_next(node)) {
if (strcasecmp(ast_xml_node_get_name(node), "text")) {
return 1;
}
}
return 0;
}
/*!
* \internal
* \brief Check if the passed node has at least one specialtag.
*
* \param fixnode Root node to search "specialtags" elements.
*
* \retval 1 If a "specialtag" element is found inside 'node'.
* \retval 0 If no "specialtag" is found inside 'node'.
*/
static int xmldoc_has_specialtags(struct ast_xml_node *fixnode)
{
struct ast_xml_node *node = fixnode;
int i;
for (node = ast_xml_node_get_children(fixnode); node; node = ast_xml_node_get_next(node)) {
for (i = 0; i < ARRAY_LEN(special_tags); i++) {
if (!strcasecmp(ast_xml_node_get_name(node), special_tags[i].tagname)) {
return 1;
}
}
}
return 0;
}
/*!
* \internal
* \brief Build the syntax for a specified starting node.
*
* \param rootnode A pointer to the ast_xml root node.
* \param rootname Name of the application, function, option, etc. to build the syntax.
* \param childname The name of each parameter node.
* \param printparenthesis Boolean if we must print parenthesis if not parameters are found in the rootnode.
* \param printrootname Boolean if we must print the rootname before the syntax and parenthesis at the begining/end.
*
* \retval NULL on error.
* \retval An ast_malloc'ed string with the syntax generated.
*/
static char *xmldoc_get_syntax_fun(struct ast_xml_node *rootnode, const char *rootname, const char *childname, int printparenthesis, int printrootname)
{
#define GOTONEXT(__rev, __a) (__rev ? ast_xml_node_get_prev(__a) : ast_xml_node_get_next(__a))
#define ISLAST(__rev, __a) (__rev == 1 ? (ast_xml_node_get_prev(__a) ? 0 : 1) : (ast_xml_node_get_next(__a) ? 0 : 1))
#define MP(__a) ((multiple ? __a : ""))
struct ast_xml_node *node = NULL, *firstparam = NULL, *lastparam = NULL;
const char *paramtype, *multipletype, *paramnameattr, *attrargsep, *parenthesis, *argname;
int reverse, required, paramcount = 0, openbrackets = 0, len = 0, hasparams=0;
int reqfinode = 0, reqlanode = 0, optmidnode = 0, prnparenthesis, multiple;
char *syntax = NULL, *argsep, *paramname;
if (ast_strlen_zero(rootname) || ast_strlen_zero(childname)) {
ast_log(LOG_WARNING, "Tried to look in XML tree with faulty rootname or childname while creating a syntax.\n");
return NULL;
}
if (!rootnode || !ast_xml_node_get_children(rootnode)) {
/* If the rootnode field is not found, at least print name. */
if (ast_asprintf(&syntax, "%s%s", (printrootname ? rootname : ""), (printparenthesis ? "()" : "")) < 0) {
syntax = NULL;
}
return syntax;
}
/* Get the argument separator from the root node attribute name 'argsep', if not found
defaults to ','. */
attrargsep = ast_xml_get_attribute(rootnode, "argsep");
if (attrargsep) {
argsep = ast_strdupa(attrargsep);
ast_xml_free_attr(attrargsep);
} else {
argsep = ast_strdupa(",");
}
/* Get order of evaluation. */
for (node = ast_xml_node_get_children(rootnode); node; node = ast_xml_node_get_next(node)) {
if (strcasecmp(ast_xml_node_get_name(node), childname)) {
continue;
}
required = 0;
hasparams = 1;
if ((paramtype = ast_xml_get_attribute(node, "required"))) {
if (ast_true(paramtype)) {
required = 1;
}
ast_xml_free_attr(paramtype);
}
lastparam = node;
reqlanode = required;
if (!firstparam) {
/* first parameter node */
firstparam = node;
reqfinode = required;
}
}
if (!hasparams) {
/* This application, function, option, etc, doesn't have any params. */
if (ast_asprintf(&syntax, "%s%s", (printrootname ? rootname : ""), (printparenthesis ? "()" : "")) < 0) {
syntax = NULL;
}
return syntax;
}
if (reqfinode && reqlanode) {
/* check midnode */
for (node = ast_xml_node_get_children(rootnode); node; node = ast_xml_node_get_next(node)) {
if (strcasecmp(ast_xml_node_get_name(node), childname)) {
continue;
}
if (node != firstparam && node != lastparam) {
if ((paramtype = ast_xml_get_attribute(node, "required"))) {
if (!ast_true(paramtype)) {
optmidnode = 1;
ast_xml_free_attr(paramtype);
break;
}
ast_xml_free_attr(paramtype);
}
}
}
}
if ((!reqfinode && reqlanode) || (reqfinode && reqlanode && optmidnode)) {
reverse = 1;
node = lastparam;
} else {
reverse = 0;
node = firstparam;
}
/* init syntax string. */
if (reverse) {
xmldoc_reverse_helper(reverse, &len, &syntax,
(printrootname ? (printrootname == 2 ? ")]" : ")"): ""));
} else {
xmldoc_reverse_helper(reverse, &len, &syntax, "%s%s", (printrootname ? rootname : ""),
(printrootname ? (printrootname == 2 ? "[(" : "(") : ""));
}
for (; node; node = GOTONEXT(reverse, node)) {
if (strcasecmp(ast_xml_node_get_name(node), childname)) {
continue;
}
/* Get the argument name, if it is not the leaf, go inside that parameter. */
if (xmldoc_has_inside(node, "argument")) {
parenthesis = ast_xml_get_attribute(node, "hasparams");
prnparenthesis = 0;
if (parenthesis) {
prnparenthesis = ast_true(parenthesis);
if (!strcasecmp(parenthesis, "optional")) {
prnparenthesis = 2;
}
ast_xml_free_attr(parenthesis);
}
argname = ast_xml_get_attribute(node, "name");
if (argname) {
paramname = xmldoc_get_syntax_fun(node, argname, "argument", prnparenthesis, prnparenthesis);
ast_xml_free_attr(argname);
} else {
/* Malformed XML, print **UNKOWN** */
paramname = ast_strdup("**unknown**");
}
} else {
paramnameattr = ast_xml_get_attribute(node, "name");
if (!paramnameattr) {
ast_log(LOG_WARNING, "Malformed XML %s: no %s name\n", rootname, childname);
if (syntax) {
/* Free already allocated syntax */
ast_free(syntax);
}
/* to give up is ok? */
if (ast_asprintf(&syntax, "%s%s", (printrootname ? rootname : ""), (printparenthesis ? "()" : "")) < 0) {
syntax = NULL;
}
return syntax;
}
paramname = ast_strdup(paramnameattr);
ast_xml_free_attr(paramnameattr);
}
if (!paramname) {
return NULL;
}
/* Defaults to 'false'. */
multiple = 0;
if ((multipletype = ast_xml_get_attribute(node, "multiple"))) {
if (ast_true(multipletype)) {
multiple = 1;
}
ast_xml_free_attr(multipletype);
}
required = 0; /* Defaults to 'false'. */
if ((paramtype = ast_xml_get_attribute(node, "required"))) {
if (ast_true(paramtype)) {
required = 1;
}
ast_xml_free_attr(paramtype);
}
/* build syntax core. */
if (required) {
/* First parameter */
if (!paramcount) {
xmldoc_reverse_helper(reverse, &len, &syntax, "%s%s%s%s", paramname, MP("["), MP(argsep), MP("...]"));
} else {
/* Time to close open brackets. */
while (openbrackets > 0) {
xmldoc_reverse_helper(reverse, &len, &syntax, (reverse ? "[" : "]"));
openbrackets--;
}
if (reverse) {
xmldoc_reverse_helper(reverse, &len, &syntax, "%s%s", paramname, argsep);
} else {
xmldoc_reverse_helper(reverse, &len, &syntax, "%s%s", argsep, paramname);
}
xmldoc_reverse_helper(reverse, &len, &syntax, "%s%s%s", MP("["), MP(argsep), MP("...]"));
}
} else {
/* First parameter */
if (!paramcount) {
xmldoc_reverse_helper(reverse, &len, &syntax, "[%s%s%s%s]", paramname, MP("["), MP(argsep), MP("...]"));
} else {
if (ISLAST(reverse, node)) {
/* This is the last parameter. */
if (reverse) {
xmldoc_reverse_helper(reverse, &len, &syntax, "[%s%s%s%s]%s", paramname,
MP("["), MP(argsep), MP("...]"), argsep);
} else {
xmldoc_reverse_helper(reverse, &len, &syntax, "%s[%s%s%s%s]", argsep, paramname,
MP("["), MP(argsep), MP("...]"));
}
} else {
if (reverse) {
xmldoc_reverse_helper(reverse, &len, &syntax, "%s%s%s%s%s]", paramname, argsep,
MP("["), MP(argsep), MP("...]"));
} else {
xmldoc_reverse_helper(reverse, &len, &syntax, "[%s%s%s%s%s", argsep, paramname,
MP("["), MP(argsep), MP("...]"));
}
openbrackets++;
}
}
}
ast_free(paramname);
paramcount++;
}
/* Time to close open brackets. */
while (openbrackets > 0) {
xmldoc_reverse_helper(reverse, &len, &syntax, (reverse ? "[" : "]"));
openbrackets--;
}
/* close syntax string. */
if (reverse) {
xmldoc_reverse_helper(reverse, &len, &syntax, "%s%s", (printrootname ? rootname : ""),
(printrootname ? (printrootname == 2 ? "[(" : "(") : ""));
} else {
xmldoc_reverse_helper(reverse, &len, &syntax, (printrootname ? (printrootname == 2 ? ")]" : ")") : ""));
}
return syntax;
#undef ISLAST
#undef GOTONEXT
#undef MP
}
/*!
* \internal
* \brief Parse an enumlist inside a \<parameter\> to generate a COMMAND syntax.
*
* \param fixnode A pointer to the \<enumlist\> node.
*
* \retval {<unknown>} on error.
* \retval A string inside brackets {} with the enum's separated by pipes |.
*/
static char *xmldoc_parse_cmd_enumlist(struct ast_xml_node *fixnode)
{
struct ast_xml_node *node = fixnode;
struct ast_str *paramname;
char *enumname, *ret;
int first = 1;
paramname = ast_str_create(128);
if (!paramname) {
return ast_strdup("{<unkown>}");
}
ast_str_append(¶mname, 0, "{");
for (node = ast_xml_node_get_children(node); node; node = ast_xml_node_get_next(node)) {
if (strcasecmp(ast_xml_node_get_name(node), "enum")) {
continue;
}
enumname = xmldoc_get_syntax_cmd(node, "", 0);
if (!enumname) {
continue;
}
if (!first) {
ast_str_append(¶mname, 0, "|");
}
ast_str_append(¶mname, 0, "%s", enumname);
first = 0;
ast_free(enumname);
}
ast_str_append(¶mname, 0, "}");
ret = ast_strdup(ast_str_buffer(paramname));
ast_free(paramname);
return ret;
}
/*!
* \internal
* \brief Generate a syntax of COMMAND type.
*
* \param fixnode The \<syntax\> node pointer.
* \param name The name of the 'command'.
* \param printname Print the name of the command before the paramters?
*
* \return On error, return just 'name'.
* \return On success return the generated syntax.
*/
static char *xmldoc_get_syntax_cmd(struct ast_xml_node *fixnode, const char *name, int printname)
{
struct ast_str *syntax;
struct ast_xml_node *tmpnode, *node = fixnode;
char *ret, *paramname;
const char *paramtype, *attrname, *literal;
int required, isenum, first = 1, isliteral;
if (!fixnode) {
return NULL;
}
syntax = ast_str_create(128);
if (!syntax) {
/* at least try to return something... */
return ast_strdup(name);
}
/* append name to output string. */
if (printname) {
ast_str_append(&syntax, 0, "%s", name);
first = 0;
}
for (node = ast_xml_node_get_children(node); node; node = ast_xml_node_get_next(node)) {
if (strcasecmp(ast_xml_node_get_name(node), "parameter")) {
continue;
}
if (xmldoc_has_inside(node, "parameter")) {
/* is this a recursive parameter. */
paramname = xmldoc_get_syntax_cmd(node, "", 0);
isenum = 1;
} else {
for (tmpnode = ast_xml_node_get_children(node); tmpnode; tmpnode = ast_xml_node_get_next(tmpnode)) {
if (!strcasecmp(ast_xml_node_get_name(tmpnode), "enumlist")) {
break;
}
}
if (tmpnode) {
/* parse enumlist (note that this is a special enumlist
that is used to describe a syntax like {<param1>|<param2>|...} */
paramname = xmldoc_parse_cmd_enumlist(tmpnode);
isenum = 1;
} else {
/* this is a simple parameter. */
attrname = ast_xml_get_attribute(node, "name");
if (!attrname) {
/* ignore this bogus parameter and continue. */
continue;
}
paramname = ast_strdup(attrname);
ast_xml_free_attr(attrname);
isenum = 0;
}
}
/* Is this parameter required? */
required = 0;
paramtype = ast_xml_get_attribute(node, "required");
if (paramtype) {
required = ast_true(paramtype);
ast_xml_free_attr(paramtype);
}
/* Is this a replaceable value or a fixed parameter value? */
isliteral = 0;
literal = ast_xml_get_attribute(node, "literal");
if (literal) {