| 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 |
#include "asterisk.h" |
| 31 |
|
| 32 |
#include "asterisk/_private.h" |
| 33 |
#include "asterisk/paths.h" |
| 34 |
#include "asterisk/linkedlists.h" |
| 35 |
#include "asterisk/config.h" |
| 36 |
#include "asterisk/term.h" |
| 37 |
#include "asterisk/astobj2.h" |
| 38 |
#include "asterisk/xmldoc.h" |
| 39 |
#include "asterisk/cli.h" |
| 40 |
|
| 41 |
#ifdef AST_XML_DOCS |
| 42 |
|
| 43 |
|
| 44 |
static const char default_documentation_language[] = "en_US"; |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
static const int xmldoc_text_columns = 79; |
| 49 |
|
| 50 |
|
| 51 |
static char documentation_language[6]; |
| 52 |
|
| 53 |
|
| 54 |
struct documentation_tree { |
| 55 |
char *filename; |
| 56 |
struct ast_xml_doc *doc; |
| 57 |
AST_RWLIST_ENTRY(documentation_tree) entry; |
| 58 |
}; |
| 59 |
|
| 60 |
static char *xmldoc_get_syntax_cmd(struct ast_xml_node *fixnode, const char *name, int printname); |
| 61 |
static int xmldoc_parse_enumlist(struct ast_xml_node *fixnode, const char *tabs, struct ast_str **buffer); |
| 62 |
static void xmldoc_parse_parameter(struct ast_xml_node *fixnode, const char *tabs, struct ast_str **buffer); |
| 63 |
static int xmldoc_parse_info(struct ast_xml_node *node, const char *tabs, const char *posttabs, struct ast_str **buffer); |
| 64 |
static int xmldoc_parse_para(struct ast_xml_node *node, const char *tabs, const char *posttabs, struct ast_str **buffer); |
| 65 |
static int xmldoc_parse_specialtags(struct ast_xml_node *fixnode, const char *tabs, const char *posttabs, struct ast_str **buffer); |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
static AST_RWLIST_HEAD_STATIC(xmldoc_tree, documentation_tree); |
| 75 |
|
| 76 |
static const struct strcolorized_tags { |
| 77 |
const char *init; |
| 78 |
const char *end; |
| 79 |
const int colorfg; |
| 80 |
const char *inittag; |
| 81 |
const char *endtag; |
| 82 |
} colorized_tags[] = { |
| 83 |
{ "<", ">", COLOR_GREEN, "<replaceable>", "</replaceable>" }, |
| 84 |
{ "\'", "\'", COLOR_BLUE, "<literal>", "</literal>" }, |
| 85 |
{ "*", "*", COLOR_RED, "<emphasis>", "</emphasis>" }, |
| 86 |
{ "\"", "\"", COLOR_YELLOW, "<filename>", "</filename>" }, |
| 87 |
{ "\"", "\"", COLOR_CYAN, "<directory>", "</directory>" }, |
| 88 |
{ "${", "}", COLOR_GREEN, "<variable>", "</variable>" }, |
| 89 |
{ "", "", COLOR_BLUE, "<value>", "</value>" }, |
| 90 |
{ "", "", COLOR_BLUE, "<enum>", "</enum>" }, |
| 91 |
{ "\'", "\'", COLOR_GRAY, "<astcli>", "</astcli>" }, |
| 92 |
|
| 93 |
|
| 94 |
{ "", "", COLOR_YELLOW, "<note>", "</note>" }, |
| 95 |
{ "", "", COLOR_RED, "<warning>", "</warning>" }, |
| 96 |
{ "", "", COLOR_WHITE, "<example>", "</example>" }, |
| 97 |
{ "", "", COLOR_WHITE, "<exampletext>", "</exampletext>"}, |
| 98 |
}; |
| 99 |
|
| 100 |
static const struct strspecial_tags { |
| 101 |
const char *tagname; |
| 102 |
const char *init; |
| 103 |
const char *end; |
| 104 |
} special_tags[] = { |
| 105 |
{ "note", "<note>NOTE:</note> ", "" }, |
| 106 |
{ "warning", "<warning>WARNING!!!:</warning> ", "" }, |
| 107 |
{ "example", "<example>Example:</example> ", "" }, |
| 108 |
}; |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
static int xmldoc_postbrlen(const char *postbr) |
| 120 |
{ |
| 121 |
int postbrreallen = 0, i; |
| 122 |
size_t postbrlen; |
| 123 |
|
| 124 |
if (!postbr) { |
| 125 |
return 0; |
| 126 |
} |
| 127 |
postbrlen = strlen(postbr); |
| 128 |
for (i = 0; i < postbrlen; i++) { |
| 129 |
if (postbr[i] == '\t') { |
| 130 |
postbrreallen += 8 - (postbrreallen % 8); |
| 131 |
} else { |
| 132 |
postbrreallen++; |
| 133 |
} |
| 134 |
} |
| 135 |
return postbrreallen; |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
static void xmldoc_setpostbr(char *postbr, size_t len, const char *text) |
| 148 |
{ |
| 149 |
int c, postbrlen = 0; |
| 150 |
|
| 151 |
if (!text) { |
| 152 |
return; |
| 153 |
} |
| 154 |
|
| 155 |
for (c = 0; c < len; c++) { |
| 156 |
if (text[c] == '\t' || text[c] == ' ') { |
| 157 |
postbr[postbrlen++] = text[c]; |
| 158 |
} else { |
| 159 |
break; |
| 160 |
} |
| 161 |
} |
| 162 |
postbr[postbrlen] = '\0'; |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
static char *xmldoc_string_wrap(const char *text, int columns) |
| 176 |
{ |
| 177 |
struct ast_str *tmp; |
| 178 |
char *ret, postbr[160]; |
| 179 |
int count, i, textlen, postbrlen, lastbreak; |
| 180 |
|
| 181 |
|
| 182 |
if (!text || columns <= 0) { |
| 183 |
ast_log(LOG_WARNING, "Passing wrong arguments while trying to wrap the text\n"); |
| 184 |
return NULL; |
| 185 |
} |
| 186 |
|
| 187 |
tmp = ast_str_create(strlen(text) * 3); |
| 188 |
|
| 189 |
if (!tmp) { |
| 190 |
return NULL; |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
xmldoc_setpostbr(postbr, sizeof(postbr), text); |
| 195 |
postbrlen = xmldoc_postbrlen(postbr); |
| 196 |
|
| 197 |
count = 0; |
| 198 |
lastbreak = 0; |
| 199 |
|
| 200 |
textlen = strlen(text); |
| 201 |
for (i = 0; i < textlen; i++) { |
| 202 |
if (text[i] == '\n') { |
| 203 |
xmldoc_setpostbr(postbr, sizeof(postbr), &text[i] + 1); |
| 204 |
postbrlen = xmldoc_postbrlen(postbr); |
| 205 |
count = 0; |
| 206 |
lastbreak = 0; |
| 207 |
} else if (text[i] == ESC) { |
| 208 |
|
| 209 |
do { |
| 210 |
ast_str_append(&tmp, 0, "%c", text[i]); |
| 211 |
i++; |
| 212 |
} while (i < textlen && text[i] != 'm'); |
| 213 |
} else { |
| 214 |
if (text[i] == ' ') { |
| 215 |
lastbreak = i; |
| 216 |
} |
| 217 |
count++; |
| 218 |
} |
| 219 |
|
| 220 |
if (count > columns) { |
| 221 |
|
| 222 |
int back = i - lastbreak; |
| 223 |
if (lastbreak && back > 0 && back < 30) { |
| 224 |
ast_str_truncate(tmp, -back); |
| 225 |
i = lastbreak; |
| 226 |
} |
| 227 |
ast_str_append(&tmp, 0, "\n%s", postbr); |
| 228 |
count = postbrlen; |
| 229 |
lastbreak = 0; |
| 230 |
} else { |
| 231 |
ast_str_append(&tmp, 0, "%c", text[i]); |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
ret = ast_strdup(ast_str_buffer(tmp)); |
| 236 |
ast_free(tmp); |
| 237 |
|
| 238 |
return ret; |
| 239 |
} |
| 240 |
|
| 241 |
char *ast_xmldoc_printable(const char *bwinput, int withcolors) |
| 242 |
{ |
| 243 |
struct ast_str *colorized; |
| 244 |
char *wrapped = NULL; |
| 245 |
int i, c, len, colorsection; |
| 246 |
char *tmp; |
| 247 |
size_t bwinputlen; |
| 248 |
static const int base_fg = COLOR_CYAN; |
| 249 |
|
| 250 |
if (!bwinput) { |
| 251 |
return NULL; |
| 252 |
} |
| 253 |
|
| 254 |
bwinputlen = strlen(bwinput); |
| 255 |
|
| 256 |
if (!(colorized = ast_str_create(256))) { |
| 257 |
return NULL; |
| 258 |
} |
| 259 |
|
| 260 |
if (withcolors) { |
| 261 |
ast_term_color_code(&colorized, base_fg, 0); |
| 262 |
if (!colorized) { |
| 263 |
return NULL; |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
for (i = 0; i < bwinputlen; i++) { |
| 268 |
colorsection = 0; |
| 269 |
|
| 270 |
for (c = 0; c < ARRAY_LEN(colorized_tags); c++) { |
| 271 |
if (strncasecmp(bwinput + i, colorized_tags[c].inittag, strlen(colorized_tags[c].inittag))) { |
| 272 |
continue; |
| 273 |
} |
| 274 |
|
| 275 |
if (!(tmp = strcasestr(bwinput + i + strlen(colorized_tags[c].inittag), colorized_tags[c].endtag))) { |
| 276 |
continue; |
| 277 |
} |
| 278 |
|
| 279 |
len = tmp - (bwinput + i + strlen(colorized_tags[c].inittag)); |
| 280 |
|
| 281 |
|
| 282 |
if (withcolors) { |
| 283 |
if (ast_opt_light_background) { |
| 284 |
|
| 285 |
ast_term_color_code(&colorized, colorized_tags[c].colorfg & 0x7f, 0); |
| 286 |
} else { |
| 287 |
|
| 288 |
ast_term_color_code(&colorized, colorized_tags[c].colorfg | 0x80, 0); |
| 289 |
} |
| 290 |
if (!colorized) { |
| 291 |
return NULL; |
| 292 |
} |
| 293 |
} |
| 294 |
|
| 295 |
|
| 296 |
ast_str_append(&colorized, 0, "%s", colorized_tags[c].init); |
| 297 |
if (!colorized) { |
| 298 |
return NULL; |
| 299 |
} |
| 300 |
{ |
| 301 |
char buf[len + 1]; |
| 302 |
ast_copy_string(buf, bwinput + i + strlen(colorized_tags[c].inittag), sizeof(buf)); |
| 303 |
ast_str_append(&colorized, 0, "%s", buf); |
| 304 |
} |
| 305 |
if (!colorized) { |
| 306 |
return NULL; |
| 307 |
} |
| 308 |
|
| 309 |
|
| 310 |
ast_str_append(&colorized, 0, "%s", colorized_tags[c].end); |
| 311 |
if (!colorized) { |
| 312 |
return NULL; |
| 313 |
} |
| 314 |
|
| 315 |
|
| 316 |
if (withcolors) { |
| 317 |
ast_term_color_code(&colorized, base_fg, 0); |
| 318 |
if (!colorized) { |
| 319 |
return NULL; |
| 320 |
} |
| 321 |
} |
| 322 |
|
| 323 |
i += len + strlen(colorized_tags[c].endtag) + strlen(colorized_tags[c].inittag) - 1; |
| 324 |
colorsection = 1; |
| 325 |
break; |
| 326 |
} |
| 327 |
|
| 328 |
if (!colorsection) { |
| 329 |
ast_str_append(&colorized, 0, "%c", bwinput[i]); |
| 330 |
if (!colorized) { |
| 331 |
return NULL; |
| 332 |
} |
| 333 |
} |
| 334 |
} |
| 335 |
|
| 336 |
if (withcolors) { |
| 337 |
ast_str_append(&colorized, 0, "%s", ast_term_reset()); |
| 338 |
if (!colorized) { |
| 339 |
return NULL; |
| 340 |
} |
| 341 |
} |
| 342 |
|
| 343 |
|
| 344 |
wrapped = xmldoc_string_wrap(ast_str_buffer(colorized), xmldoc_text_columns); |
| 345 |
|
| 346 |
ast_free(colorized); |
| 347 |
|
| 348 |
return wrapped; |
| 349 |
} |
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
static void xmldoc_string_cleanup(const char *text, struct ast_str **output, int lastspaces, int maintain_newlines) |
| 361 |
{ |
| 362 |
int i; |
| 363 |
size_t textlen; |
| 364 |
|
| 365 |
if (!text) { |
| 366 |
*output = NULL; |
| 367 |
return; |
| 368 |
} |
| 369 |
|
| 370 |
textlen = strlen(text); |
| 371 |
|
| 372 |
*output = ast_str_create(textlen); |
| 373 |
if (!(*output)) { |
| 374 |
ast_log(LOG_ERROR, "Problem allocating output buffer\n"); |
| 375 |
return; |
| 376 |
} |
| 377 |
|
| 378 |
for (i = 0; i < textlen; i++) { |
| 379 |
if (text[i] == '\n' || text[i] == '\r') { |
| 380 |
if (maintain_newlines) { |
| 381 |
ast_str_append(output, 0, "%c", text[i]); |
| 382 |
} |
| 383 |
|
| 384 |
while (text[i + 1] == '\t' || text[i + 1] == '\r' || text[i + 1] == '\n') { |
| 385 |
i++; |
| 386 |
} |
| 387 |
ast_str_append(output, 0, " "); |
| 388 |
continue; |
| 389 |
} else { |
| 390 |
ast_str_append(output, 0, "%c", text[i]); |
| 391 |
} |
| 392 |
} |
| 393 |
|
| 394 |
|
| 395 |
if (lastspaces) { |
| 396 |
ast_str_trim_blanks(*output); |
| 397 |
} |
| 398 |
} |
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
static int xmldoc_attribute_match(struct ast_xml_node *node, const char *attr, const char *value) |
| 412 |
{ |
| 413 |
const char *attr_value = ast_xml_get_attribute(node, attr); |
| 414 |
int match = attr_value && !strcmp(attr_value, value); |
| 415 |
ast_xml_free_attr(attr_value); |
| 416 |
return match; |
| 417 |
} |
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
static struct ast_xml_node *xmldoc_get_node(const char *type, const char *name, const char *module, const char *language) |
| 436 |
{ |
| 437 |
struct ast_xml_node *node = NULL; |
| 438 |
struct ast_xml_node *first_match = NULL; |
| 439 |
struct ast_xml_node *lang_match = NULL; |
| 440 |
struct documentation_tree *doctree; |
| 441 |
|
| 442 |
AST_LIST_TRAVERSE(&xmldoc_tree, doctree, entry) { |
| 443 |
|
| 444 |
node = ast_xml_get_root(doctree->doc); |
| 445 |
if (!node) { |
| 446 |
break; |
| 447 |
} |
| 448 |
|
| 449 |
node = ast_xml_node_get_children(node); |
| 450 |
while ((node = ast_xml_find_element(node, type, "name", name))) { |
| 451 |
if (!ast_xml_node_get_children(node)) { |
| 452 |
|
| 453 |
node = ast_xml_node_get_next(node); |
| 454 |
continue; |
| 455 |
} |
| 456 |
|
| 457 |
if (!first_match) { |
| 458 |
first_match = node; |
| 459 |
} |
| 460 |
|
| 461 |
|
| 462 |
if (xmldoc_attribute_match(node, "language", language)) { |
| 463 |
if (!lang_match) { |
| 464 |
lang_match = node; |
| 465 |
} |
| 466 |
|
| 467 |
|
| 468 |
if (ast_strlen_zero(module)) { |
| 469 |
break; |
| 470 |
} |
| 471 |
|
| 472 |
|
| 473 |
if (xmldoc_attribute_match(node, "module", module)) { |
| 474 |
break; |
| 475 |
} |
| 476 |
} |
| 477 |
|
| 478 |
node = ast_xml_node_get_next(node); |
| 479 |
} |
| 480 |
|
| 481 |
|
| 482 |
if (node) { |
| 483 |
break; |
| 484 |
} |
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
if (lang_match) { |
| 489 |
node = lang_match; |
| 490 |
break; |
| 491 |
} |
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
if (first_match) { |
| 496 |
node = first_match; |
| 497 |
break; |
| 498 |
} |
| 499 |
} |
| 500 |
|
| 501 |
return node; |
| 502 |
} |
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
static void __attribute__((format(printf, 4, 5))) xmldoc_reverse_helper(int reverse, int *len, char **syntax, const char *fmt, ...) |
| 516 |
{ |
| 517 |
int totlen; |
| 518 |
int tmpfmtlen; |
| 519 |
char *tmpfmt; |
| 520 |
char *new_syntax; |
| 521 |
char tmp; |
| 522 |
va_list ap; |
| 523 |
|
| 524 |
va_start(ap, fmt); |
| 525 |
if (ast_vasprintf(&tmpfmt, fmt, ap) < 0) { |
| 526 |
va_end(ap); |
| 527 |
return; |
| 528 |
} |
| 529 |
va_end(ap); |
| 530 |
|
| 531 |
tmpfmtlen = strlen(tmpfmt); |
| 532 |
totlen = *len + tmpfmtlen + 1; |
| 533 |
|
| 534 |
new_syntax = ast_realloc(*syntax, totlen); |
| 535 |
if (!new_syntax) { |
| 536 |
ast_free(tmpfmt); |
| 537 |
return; |
| 538 |
} |
| 539 |
*syntax = new_syntax; |
| 540 |
|
| 541 |
if (reverse) { |
| 542 |
memmove(*syntax + tmpfmtlen, *syntax, *len); |
| 543 |
|
| 544 |
tmp = (*syntax)[0]; |
| 545 |
strcpy(*syntax, tmpfmt); |
| 546 |
|
| 547 |
(*syntax)[tmpfmtlen] = tmp; |
| 548 |
(*syntax)[totlen - 1] = '\0'; |
| 549 |
} else { |
| 550 |
strcpy(*syntax + *len, tmpfmt); |
| 551 |
} |
| 552 |
|
| 553 |
*len = totlen - 1; |
| 554 |
ast_free(tmpfmt); |
| 555 |
} |
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
static int xmldoc_has_inside(struct ast_xml_node *fixnode, const char *what) |
| 568 |
{ |
| 569 |
struct ast_xml_node *node = fixnode; |
| 570 |
|
| 571 |
for (node = ast_xml_node_get_children(fixnode); node; node = ast_xml_node_get_next(node)) { |
| 572 |
if (!strcasecmp(ast_xml_node_get_name(node), what)) { |
| 573 |
return 1; |
| 574 |
} |
| 575 |
} |
| 576 |
return 0; |
| 577 |
} |
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
static int xmldoc_has_nodes(struct ast_xml_node *fixnode) |
| 589 |
{ |
| 590 |
struct ast_xml_node *node = fixnode; |
| 591 |
|
| 592 |
for (node = ast_xml_node_get_children(fixnode); node; node = ast_xml_node_get_next(node)) { |
| 593 |
if (strcasecmp(ast_xml_node_get_name(node), "text")) { |
| 594 |
return 1; |
| 595 |
} |
| 596 |
} |
| 597 |
return 0; |
| 598 |
} |
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
static int xmldoc_has_specialtags(struct ast_xml_node *fixnode) |
| 610 |
{ |
| 611 |
struct ast_xml_node *node = fixnode; |
| 612 |
int i; |
| 613 |
|
| 614 |
for (node = ast_xml_node_get_children(fixnode); node; node = ast_xml_node_get_next(node)) { |
| 615 |
for (i = 0; i < ARRAY_LEN(special_tags); i++) { |
| 616 |
if (!strcasecmp(ast_xml_node_get_name(node), special_tags[i].tagname)) { |
| 617 |
return 1; |
| 618 |
} |
| 619 |
} |
| 620 |
} |
| 621 |
return 0; |
| 622 |
} |
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
static char *xmldoc_get_syntax_fun(struct ast_xml_node *rootnode, const char *rootname, const char *childname, int printparenthesis, int printrootname) |
| 638 |
{ |
| 639 |
#define GOTONEXT(__rev, __a) (__rev ? ast_xml_node_get_prev(__a) : ast_xml_node_get_next(__a)) |
| 640 |
#define ISLAST(__rev, __a) (__rev == 1 ? (ast_xml_node_get_prev(__a) ? 0 : 1) : (ast_xml_node_get_next(__a) ? 0 : 1)) |
| 641 |
#define MP(__a) ((multiple ? __a : "")) |
| 642 |
struct ast_xml_node *node = NULL, *firstparam = NULL, *lastparam = NULL; |
| 643 |
const char *paramtype, *multipletype, *paramnameattr, *attrargsep, *parenthesis, *argname; |
| 644 |
int reverse, required, paramcount = 0, openbrackets = 0, len = 0, hasparams=0; |
| 645 |
int reqfinode = 0, reqlanode = 0, optmidnode = 0, prnparenthesis, multiple; |
| 646 |
char *syntax = NULL, *argsep, *paramname; |
| 647 |
|
| 648 |
if (ast_strlen_zero(rootname) || ast_strlen_zero(childname)) { |
| 649 |
ast_log(LOG_WARNING, "Tried to look in XML tree with faulty rootname or childname while creating a syntax.\n"); |
| 650 |
return NULL; |
| 651 |
} |
| 652 |
|
| 653 |
if (!rootnode || !ast_xml_node_get_children(rootnode)) { |
| 654 |
|
| 655 |
if (ast_asprintf(&syntax, "%s%s", (printrootname ? rootname : ""), (printparenthesis ? "()" : "")) < 0) { |
| 656 |
syntax = NULL; |
| 657 |
} |
| 658 |
return syntax; |
| 659 |
} |
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
attrargsep = ast_xml_get_attribute(rootnode, "argsep"); |
| 664 |
if (attrargsep) { |
| 665 |
argsep = ast_strdupa(attrargsep); |
| 666 |
ast_xml_free_attr(attrargsep); |
| 667 |
} else { |
| 668 |
argsep = ast_strdupa(","); |
| 669 |
} |
| 670 |
|
| 671 |
|
| 672 |
for (node = ast_xml_node_get_children(rootnode); node; node = ast_xml_node_get_next(node)) { |
| 673 |
if (strcasecmp(ast_xml_node_get_name(node), childname)) { |
| 674 |
continue; |
| 675 |
} |
| 676 |
required = 0; |
| 677 |
hasparams = 1; |
| 678 |
if ((paramtype = ast_xml_get_attribute(node, "required"))) { |
| 679 |
if (ast_true(paramtype)) { |
| 680 |
required = 1; |
| 681 |
} |
| 682 |
ast_xml_free_attr(paramtype); |
| 683 |
} |
| 684 |
|
| 685 |
lastparam = node; |
| 686 |
reqlanode = required; |
| 687 |
|
| 688 |
if (!firstparam) { |
| 689 |
|
| 690 |
firstparam = node; |
| 691 |
reqfinode = required; |
| 692 |
} |
| 693 |
} |
| 694 |
|
| 695 |
if (!hasparams) { |
| 696 |
|
| 697 |
if (ast_asprintf(&syntax, "%s%s", (printrootname ? rootname : ""), (printparenthesis ? "()" : "")) < 0) { |
| 698 |
syntax = NULL; |
| 699 |
} |
| 700 |
return syntax; |
| 701 |
} |
| 702 |
|
| 703 |
if (reqfinode && reqlanode) { |
| 704 |
|
| 705 |
for (node = ast_xml_node_get_children(rootnode); node; node = ast_xml_node_get_next(node)) { |
| 706 |
if (strcasecmp(ast_xml_node_get_name(node), childname)) { |
| 707 |
continue; |
| 708 |
} |
| 709 |
if (node != firstparam && node != lastparam) { |
| 710 |
if ((paramtype = ast_xml_get_attribute(node, "required"))) { |
| 711 |
if (!ast_true(paramtype)) { |
| 712 |
optmidnode = 1; |
| 713 |
ast_xml_free_attr(paramtype); |
| 714 |
break; |
| 715 |
} |
| 716 |
ast_xml_free_attr(paramtype); |
| 717 |
} |
| 718 |
} |
| 719 |
} |
| 720 |
} |
| 721 |
|
| 722 |
if ((!reqfinode && reqlanode) || (reqfinode && reqlanode && optmidnode)) { |
| 723 |
reverse = 1; |
| 724 |
node = lastparam; |
| 725 |
} else { |
| 726 |
reverse = 0; |
| 727 |
node = firstparam; |
| 728 |
} |
| 729 |
|
| 730 |
|
| 731 |
if (reverse) { |
| 732 |
xmldoc_reverse_helper(reverse, &len, &syntax, |
| 733 |
(printrootname ? (printrootname == 2 ? ")]" : ")"): "")); |
| 734 |
} else { |
| 735 |
xmldoc_reverse_helper(reverse, &len, &syntax, "%s%s", (printrootname ? rootname : ""), |
| 736 |
(printrootname ? (printrootname == 2 ? "[(" : "(") : "")); |
| 737 |
} |
| 738 |
|
| 739 |
for (; node; node = GOTONEXT(reverse, node)) { |
| 740 |
if (strcasecmp(ast_xml_node_get_name(node), childname)) { |
| 741 |
continue; |
| 742 |
} |
| 743 |
|
| 744 |
|
| 745 |
if (xmldoc_has_inside(node, "argument")) { |
| 746 |
parenthesis = ast_xml_get_attribute(node, "hasparams"); |
| 747 |
prnparenthesis = 0; |
| 748 |
if (parenthesis) { |
| 749 |
prnparenthesis = ast_true(parenthesis); |
| 750 |
if (!strcasecmp(parenthesis, "optional")) { |
| 751 |
prnparenthesis = 2; |
| 752 |
} |
| 753 |
ast_xml_free_attr(parenthesis); |
| 754 |
} |
| 755 |
argname = ast_xml_get_attribute(node, "name"); |
| 756 |
if (argname) { |
| 757 |
paramname = xmldoc_get_syntax_fun(node, argname, "argument", prnparenthesis, prnparenthesis); |
| 758 |
ast_xml_free_attr(argname); |
| 759 |
} else { |
| 760 |
|
| 761 |
paramname = ast_strdup("**unknown**"); |
| 762 |
} |
| 763 |
} else { |
| 764 |
paramnameattr = ast_xml_get_attribute(node, "name"); |
| 765 |
if (!paramnameattr) { |
| 766 |
ast_log(LOG_WARNING, "Malformed XML %s: no %s name\n", rootname, childname); |
| 767 |
if (syntax) { |
| 768 |
|
| 769 |
ast_free(syntax); |
| 770 |
} |
| 771 |
|
| 772 |
if (ast_asprintf(&syntax, "%s%s", (printrootname ? rootname : ""), (printparenthesis ? "()" : "")) < 0) { |
| 773 |
syntax = NULL; |
| 774 |
} |
| 775 |
return syntax; |
| 776 |
} |
| 777 |
paramname = ast_strdup(paramnameattr); |
| 778 |
ast_xml_free_attr(paramnameattr); |
| 779 |
} |
| 780 |
|
| 781 |
if (!paramname) { |
| 782 |
return NULL; |
| 783 |
} |
| 784 |
|
| 785 |
|
| 786 |
multiple = 0; |
| 787 |
if ((multipletype = ast_xml_get_attribute(node, "multiple"))) { |
| 788 |
if (ast_true(multipletype)) { |
| 789 |
multiple = 1; |
| 790 |
} |
| 791 |
ast_xml_free_attr(multipletype); |
| 792 |
} |
| 793 |
|
| 794 |
required = 0; |
| 795 |
if ((paramtype = ast_xml_get_attribute(node, "required"))) { |
| 796 |
if (ast_true(paramtype)) { |
| 797 |
required = 1; |
| 798 |
} |
| 799 |
ast_xml_free_attr(paramtype); |
| 800 |
} |
| 801 |
|
| 802 |
|
| 803 |
|
| 804 |
if (required) { |
| 805 |
|
| 806 |
if (!paramcount) { |
| 807 |
xmldoc_reverse_helper(reverse, &len, &syntax, "%s%s%s%s", paramname, MP("["), MP(argsep), MP("...]")); |
| 808 |
} else { |
| 809 |
|
| 810 |
while (openbrackets > 0) { |
| 811 |
xmldoc_reverse_helper(reverse, &len, &syntax, (reverse ? "[" : "]")); |
| 812 |
openbrackets--; |
| 813 |
} |
| 814 |
if (reverse) { |
| 815 |
xmldoc_reverse_helper(reverse, &len, &syntax, "%s%s", paramname, argsep); |
| 816 |
} else { |
| 817 |
xmldoc_reverse_helper(reverse, &len, &syntax, "%s%s", argsep, paramname); |
| 818 |
} |
| 819 |
xmldoc_reverse_helper(reverse, &len, &syntax, "%s%s%s", MP("["), MP(argsep), MP("...]")); |
| 820 |
} |
| 821 |
} else { |
| 822 |
|
| 823 |
if (!paramcount) { |
| 824 |
xmldoc_reverse_helper(reverse, &len, &syntax, "[%s%s%s%s]", paramname, MP("["), MP(argsep), MP("...]")); |
| 825 |
} else { |
| 826 |
if (ISLAST(reverse, node)) { |
| 827 |
|
| 828 |
if (reverse) { |
| 829 |
xmldoc_reverse_helper(reverse, &len, &syntax, "[%s%s%s%s]%s", paramname, |
| 830 |
MP("["), MP(argsep), MP("...]"), argsep); |
| 831 |
} else { |
| 832 |
xmldoc_reverse_helper(reverse, &len, &syntax, "%s[%s%s%s%s]", argsep, paramname, |
| 833 |
MP("["), MP(argsep), MP("...]")); |
| 834 |
} |
| 835 |
} else { |
| 836 |
if (reverse) { |
| 837 |
xmldoc_reverse_helper(reverse, &len, &syntax, "%s%s%s%s%s]", paramname, argsep, |
| 838 |
MP("["), MP(argsep), MP("...]")); |
| 839 |
} else { |
| 840 |
xmldoc_reverse_helper(reverse, &len, &syntax, "[%s%s%s%s%s", argsep, paramname, |
| 841 |
MP("["), MP(argsep), MP("...]")); |
| 842 |
} |
| 843 |
openbrackets++; |
| 844 |
} |
| 845 |
} |
| 846 |
} |
| 847 |
ast_free(paramname); |
| 848 |
|
| 849 |
paramcount++; |
| 850 |
} |
| 851 |
|
| 852 |
|
| 853 |
while (openbrackets > 0) { |
| 854 |
xmldoc_reverse_helper(reverse, &len, &syntax, (reverse ? "[" : "]")); |
| 855 |
openbrackets--; |
| 856 |
} |
| 857 |
|
| 858 |
|
| 859 |
if (reverse) { |
| 860 |
xmldoc_reverse_helper(reverse, &len, &syntax, "%s%s", (printrootname ? rootname : ""), |
| 861 |
(printrootname ? (printrootname == 2 ? "[(" : "(") : "")); |
| 862 |
} else { |
| 863 |
xmldoc_reverse_helper(reverse, &len, &syntax, (printrootname ? (printrootname == 2 ? ")]" : ")") : "")); |
| 864 |
} |
| 865 |
|
| 866 |
return syntax; |
| 867 |
#undef ISLAST |
| 868 |
#undef GOTONEXT |
| 869 |
#undef MP |
| 870 |
} |
| 871 |
|
| 872 |
|
| 873 |
|
| 874 |
|
| 875 |
|
| 876 |
|
| 877 |
|
| 878 |
|
| 879 |
|
| 880 |
|
| 881 |
static char *xmldoc_parse_cmd_enumlist(struct ast_xml_node *fixnode) |
| 882 |
{ |
| 883 |
struct ast_xml_node *node = fixnode; |
| 884 |
struct ast_str *paramname; |
| 885 |
char *enumname, *ret; |
| 886 |
int first = 1; |
| 887 |
|
| 888 |
paramname = ast_str_create(128); |
| 889 |
if (!paramname) { |
| 890 |
return ast_strdup("{<unkown>}"); |
| 891 |
} |
| 892 |
|
| 893 |
ast_str_append(¶mname, 0, "{"); |
| 894 |
|
| 895 |
for (node = ast_xml_node_get_children(node); node; node = ast_xml_node_get_next(node)) { |
| 896 |
if (strcasecmp(ast_xml_node_get_name(node), "enum")) { |
| 897 |
continue; |
| 898 |
} |
| 899 |
|
| 900 |
enumname = xmldoc_get_syntax_cmd(node, "", 0); |
| 901 |
if (!enumname) { |
| 902 |
continue; |
| 903 |
} |
| 904 |
if (!first) { |
| 905 |
ast_str_append(¶mname, 0, "|"); |
| 906 |
} |
| 907 |
ast_str_append(¶mname, 0, "%s", enumname); |
| 908 |
first = 0; |
| 909 |
ast_free(enumname); |
| 910 |
} |
| 911 |
|
| 912 |
ast_str_append(¶mname, 0, "}"); |
| 913 |
|
| 914 |
ret = ast_strdup(ast_str_buffer(paramname)); |
| 915 |
ast_free(paramname); |
| 916 |
|
| 917 |
return ret; |
| 918 |
} |
| 919 |
|
| 920 |
|
| 921 |
|
| 922 |
|
| 923 |
|
| 924 |
|
| 925 |
|
| 926 |
|
| 927 |
|
| 928 |
|
| 929 |
|
| 930 |
|
| 931 |
static char *xmldoc_get_syntax_cmd(struct ast_xml_node *fixnode, const char *name, int printname) |
| 932 |
{ |
| 933 |
struct ast_str *syntax; |
| 934 |
struct ast_xml_node *tmpnode, *node = fixnode; |
| 935 |
char *ret, *paramname; |
| 936 |
const char *paramtype, *attrname, *literal; |
| 937 |
int required, isenum, first = 1, isliteral; |
| 938 |
|
| 939 |
if (!fixnode) { |
| 940 |
return NULL; |
| 941 |
} |
| 942 |
|
| 943 |
syntax = ast_str_create(128); |
| 944 |
if (!syntax) { |
| 945 |
|
| 946 |
return ast_strdup(name); |
| 947 |
} |
| 948 |
|
| 949 |
|
| 950 |
if (printname) { |
| 951 |
ast_str_append(&syntax, 0, "%s", name); |
| 952 |
first = 0; |
| 953 |
} |
| 954 |
|
| 955 |
for (node = ast_xml_node_get_children(node); node; node = ast_xml_node_get_next(node)) { |
| 956 |
if (strcasecmp(ast_xml_node_get_name(node), "parameter")) { |
| 957 |
continue; |
| 958 |
} |
| 959 |
|
| 960 |
if (xmldoc_has_inside(node, "parameter")) { |
| 961 |
|
| 962 |
paramname = xmldoc_get_syntax_cmd(node, "", 0); |
| 963 |
isenum = 1; |
| 964 |
} else { |
| 965 |
for (tmpnode = ast_xml_node_get_children(node); tmpnode; tmpnode = ast_xml_node_get_next(tmpnode)) { |
| 966 |
if (!strcasecmp(ast_xml_node_get_name(tmpnode), "enumlist")) { |
| 967 |
break; |
| 968 |
} |
| 969 |
} |
| 970 |
if (tmpnode) { |
| 971 |
|
| 972 |
|
| 973 |
paramname = xmldoc_parse_cmd_enumlist(tmpnode); |
| 974 |
isenum = 1; |
| 975 |
} else { |
| 976 |
|
| 977 |
attrname = ast_xml_get_attribute(node, "name"); |
| 978 |
if (!attrname) { |
| 979 |
|
| 980 |
continue; |
| 981 |
} |
| 982 |
paramname = ast_strdup(attrname); |
| 983 |
ast_xml_free_attr(attrname); |
| 984 |
isenum = 0; |
| 985 |
} |
| 986 |
} |
| 987 |
|
| 988 |
|
| 989 |
required = 0; |
| 990 |
paramtype = ast_xml_get_attribute(node, "required"); |
| 991 |
if (paramtype) { |
| 992 |
required = ast_true(paramtype); |
| 993 |
ast_xml_free_attr(paramtype); |
| 994 |
} |
| 995 |
|
| 996 |
|
| 997 |
isliteral = 0; |
| 998 |
literal = ast_xml_get_attribute(node, "literal"); |
| 999 |
if (literal) { |
| 1000 |
isliteral = ast_true(literal); |
| 1001 |
ast_xml_free_attr(literal); |
| 1002 |
} |
| 1003 |
|
| 1004 |
|
| 1005 |
|
| 1006 |
|
| 1007 |
|
| 1008 |
ast_str_append(&syntax, 0, "%s%s%s%s%s%s", |
| 1009 |
(first ? "" : " "), |
| 1010 |
(required ? "" : "["), |
| 1011 |
(isenum || isliteral ? "" : "<"), |
| 1012 |
paramname, |
| 1013 |
(isenum || isliteral ? "" : ">"), |
| 1014 |
(required ? "" : "]")); |
| 1015 |
first = 0; |
| 1016 |
ast_free(paramname); |
| 1017 |
} |
| 1018 |
|
| 1019 |
|
| 1020 |
ret = ast_strdup(ast_str_buffer(syntax)); |
| 1021 |
ast_free(syntax); |
| 1022 |
|
| 1023 |
return ret; |
| 1024 |
} |
| 1025 |
|
| 1026 |
|
| 1027 |
|
| 1028 |
|
| 1029 |
|
| 1030 |
|
| 1031 |
|
| 1032 |
|
| 1033 |
|
| 1034 |
|
| 1035 |
|
| 1036 |
|
| 1037 |
static char *xmldoc_get_syntax_manager(struct ast_xml_node *fixnode, const char *name, const char *manager_type) |
| 1038 |
{ |
| 1039 |
struct ast_str *syntax; |
| 1040 |
struct ast_xml_node *node = fixnode; |
| 1041 |
const char *paramtype, *attrname; |
| 1042 |
int required; |
| 1043 |
char *ret; |
| 1044 |
|
| 1045 |
if (!fixnode) { |
| 1046 |
return NULL; |
| 1047 |
} |
| 1048 |
|
| 1049 |
syntax = ast_str_create(128); |
| 1050 |
if (!syntax) { |
| 1051 |
return ast_strdup(name); |
| 1052 |
} |
| 1053 |
|
| 1054 |
ast_str_append(&syntax, 0, "%s: %s", manager_type, name); |
| 1055 |
|
| 1056 |
for (node = ast_xml_node_get_children(node); node; node = ast_xml_node_get_next(node)) { |
| 1057 |
if (strcasecmp(ast_xml_node_get_name(node), "parameter")) { |
| 1058 |
continue; |
| 1059 |
} |
| 1060 |
|
| 1061 |
|
| 1062 |
required = !strcasecmp(manager_type, "event") ? 1 : 0; |
| 1063 |
paramtype = ast_xml_get_attribute(node, "required"); |
| 1064 |
if (paramtype) { |
| 1065 |
required = ast_true(paramtype); |
| 1066 |
ast_xml_free_attr(paramtype); |
| 1067 |
} |
| 1068 |
|
| 1069 |
attrname = ast_xml_get_attribute(node, "name"); |
| 1070 |
if (!attrname) { |
| 1071 |
|
| 1072 |
continue; |
| 1073 |
} |
| 1074 |
|
| 1075 |
ast_str_append(&syntax, 0, "\n%s%s:%s <value>", |
| 1076 |
(required ? "" : "["), |
| 1077 |
attrname, |
| 1078 |
(required ? "" : "]")); |
| 1079 |
ast_xml_free_attr(attrname); |
| 1080 |
} |
| 1081 |
|
| 1082 |
|
| 1083 |
ret = ast_strdup(ast_str_buffer(syntax)); |
| 1084 |
ast_free(syntax); |
| 1085 |
|
| 1086 |
return ret; |
| 1087 |
} |
| 1088 |
|
| 1089 |
static char *xmldoc_get_syntax_config_object(struct ast_xml_node *fixnode, const char *name) |
| 1090 |
{ |
| 1091 |
struct ast_xml_node *matchinfo, *tmp; |
| 1092 |
int match; |
| 1093 |
const char *attr_value; |
| 1094 |
const char *text; |
| 1095 |
RAII_VAR(struct ast_str *, syntax, ast_str_create(128), ast_free); |
| 1096 |
|
| 1097 |
if (!syntax || !fixnode) { |
| 1098 |
return NULL; |
| 1099 |
} |
| 1100 |
if (!(matchinfo = ast_xml_find_element(ast_xml_node_get_children(fixnode), "matchInfo", NULL, NULL))) { |
| 1101 |
return NULL; |
| 1102 |
} |
| 1103 |
if (!(tmp = ast_xml_find_element(ast_xml_node_get_children(matchinfo), "category", NULL, NULL))) { |
| 1104 |
return NULL; |
| 1105 |
} |
| 1106 |
attr_value = ast_xml_get_attribute(tmp, "match"); |
| 1107 |
if (attr_value) { |
| 1108 |
match = ast_true(attr_value); |
| 1109 |
text = ast_xml_get_text(tmp); |
| 1110 |
ast_str_set(&syntax, 0, "category %s /%s/", match ? "=~" : "!~", text); |
| 1111 |
ast_xml_free_attr(attr_value); |
| 1112 |
ast_xml_free_text(text); |
| 1113 |
} |
| 1114 |
|
| 1115 |
if ((tmp = ast_xml_find_element(ast_xml_node_get_children(matchinfo), "field", NULL, NULL))) { |
| 1116 |
text = ast_xml_get_text(tmp); |
| 1117 |
attr_value = ast_xml_get_attribute(tmp, "name"); |
| 1118 |
ast_str_append(&syntax, 0, " matchfield: %s = %s", S_OR(attr_value, "Unknown"), text); |
| 1119 |
ast_xml_free_attr(attr_value); |
| 1120 |
ast_xml_free_text(text); |
| 1121 |
} |
| 1122 |
return ast_strdup(ast_str_buffer(syntax)); |
| 1123 |
} |
| 1124 |
|
| 1125 |
static char *xmldoc_get_syntax_config_option(struct ast_xml_node *fixnode, const char *name) |
| 1126 |
{ |
| 1127 |
const char *type; |
| 1128 |
const char *default_value; |
| 1129 |
const char *regex; |
| 1130 |
RAII_VAR(struct ast_str *, syntax, ast_str_create(128), ast_free); |
| 1131 |
|
| 1132 |
if (!syntax || !fixnode) { |
| 1133 |
return NULL; |
| 1134 |
} |
| 1135 |
type = ast_xml_get_attribute(fixnode, "type"); |
| 1136 |
default_value = ast_xml_get_attribute(fixnode, "default"); |
| 1137 |
|
| 1138 |
regex = ast_xml_get_attribute(fixnode, "regex"); |
| 1139 |
ast_str_set(&syntax, 0, "%s = [%s] (Default: %s) (Regex: %s)\n", |
| 1140 |
name, |
| 1141 |
type ?: "", |
| 1142 |
default_value ?: "n/a", |
| 1143 |
regex ?: "False"); |
| 1144 |
|
| 1145 |
ast_xml_free_attr(type); |
| 1146 |
ast_xml_free_attr(default_value); |
| 1147 |
ast_xml_free_attr(regex); |
| 1148 |
|
| 1149 |
return ast_strdup(ast_str_buffer(syntax)); |
| 1150 |
} |
| 1151 |
|
| 1152 |
|
| 1153 |
enum syntaxtype { |
| 1154 |
FUNCTION_SYNTAX, |
| 1155 |
MANAGER_SYNTAX, |
| 1156 |
MANAGER_EVENT_SYNTAX, |
| 1157 |
CONFIG_INFO_SYNTAX, |
| 1158 |
CONFIG_FILE_SYNTAX, |
| 1159 |
CONFIG_OPTION_SYNTAX, |
| 1160 |
CONFIG_OBJECT_SYNTAX, |
| 1161 |
COMMAND_SYNTAX |
| 1162 |
}; |
| 1163 |
|
| 1164 |
|
| 1165 |
static struct strsyntaxtype { |
| 1166 |
const char *type; |
| 1167 |
enum syntaxtype stxtype; |
| 1168 |
} stxtype[] = { |
| 1169 |
{ "function", FUNCTION_SYNTAX }, |
| 1170 |
{ "application", FUNCTION_SYNTAX }, |
| 1171 |
{ "manager", MANAGER_SYNTAX }, |
| 1172 |
{ "managerEvent", MANAGER_EVENT_SYNTAX }, |
| 1173 |
{ "configInfo", CONFIG_INFO_SYNTAX }, |
| 1174 |
{ "configFile", CONFIG_FILE_SYNTAX }, |
| 1175 |
{ "configOption", CONFIG_OPTION_SYNTAX }, |
| 1176 |
{ "configObject", CONFIG_OBJECT_SYNTAX }, |
| 1177 |
{ "agi", COMMAND_SYNTAX }, |
| 1178 |
}; |
| 1179 |
|
| 1180 |
|
| 1181 |
|
| 1182 |
|
| 1183 |
|
| 1184 |
|
| 1185 |
|
| 1186 |
|
| 1187 |
|
| 1188 |
static enum syntaxtype xmldoc_get_syntax_type(const char *type) |
| 1189 |
{ |
| 1190 |
int i; |
| 1191 |
for (i=0; i < ARRAY_LEN(stxtype); i++) { |
| 1192 |
if (!strcasecmp(stxtype[i].type, type)) { |
| 1193 |
return stxtype[i].stxtype; |
| 1194 |
} |
| 1195 |
} |
| 1196 |
|
| 1197 |
return FUNCTION_SYNTAX; |
| 1198 |
} |
| 1199 |
|
| 1200 |
|
| 1201 |
|
| 1202 |
|
| 1203 |
|
| 1204 |
|
| 1205 |
|
| 1206 |
|
| 1207 |
|
| 1208 |
|
| 1209 |
|
| 1210 |
|
| 1211 |
|
| 1212 |
|
| 1213 |
|
| 1214 |
|
| 1215 |
static char *_ast_xmldoc_build_syntax(struct ast_xml_node *root_node, const char *type, const char *name) |
| 1216 |
{ |
| 1217 |
char *syntax = NULL; |
| 1218 |
struct ast_xml_node *node = root_node; |
| 1219 |
|
| 1220 |
for (node = ast_xml_node_get_children(node); node; node = ast_xml_node_get_next(node)) { |
| 1221 |
if (!strcasecmp(ast_xml_node_get_name(node), "syntax")) { |
| 1222 |
break; |
| 1223 |
} |
| 1224 |
} |
| 1225 |
|
| 1226 |
switch (xmldoc_get_syntax_type(type)) { |
| 1227 |
case FUNCTION_SYNTAX: |
| 1228 |
syntax = xmldoc_get_syntax_fun(node, name, "parameter", 1, 1); |
| 1229 |
break; |
| 1230 |
case COMMAND_SYNTAX: |
| 1231 |
syntax = xmldoc_get_syntax_cmd(node, name, 1); |
| 1232 |
break; |
| 1233 |
case MANAGER_SYNTAX: |
| 1234 |
syntax = xmldoc_get_syntax_manager(node, name, "Action"); |
| 1235 |
break; |
| 1236 |
case MANAGER_EVENT_SYNTAX: |
| 1237 |
syntax = xmldoc_get_syntax_manager(node, name, "Event"); |
| 1238 |
break; |
| 1239 |
case CONFIG_OPTION_SYNTAX: |
| 1240 |
syntax = xmldoc_get_syntax_config_option(root_node, name); |
| 1241 |
break; |
| 1242 |
case CONFIG_OBJECT_SYNTAX: |
| 1243 |
syntax = xmldoc_get_syntax_config_object(node, name); |
| 1244 |
break; |
| 1245 |
default: |
| 1246 |
syntax = xmldoc_get_syntax_fun(node, name, "parameter", 1, 1); |
| 1247 |
} |
| 1248 |
|
| 1249 |
return syntax; |
| 1250 |
} |
| 1251 |
|
| 1252 |
char *ast_xmldoc_build_syntax(const char *type, const char *name, const char *module) |
| 1253 |
{ |
| 1254 |
struct ast_xml_node *node; |
| 1255 |
char *syntax; |
| 1256 |
|
| 1257 |
AST_RWLIST_RDLOCK(&xmldoc_tree); |
| 1258 |
node = xmldoc_get_node(type, name, module, documentation_language); |
| 1259 |
if (!node) { |
| 1260 |
AST_RWLIST_UNLOCK(&xmldoc_tree); |
| 1261 |
return NULL; |
| 1262 |
} |
| 1263 |
|
| 1264 |
syntax = _ast_xmldoc_build_syntax(node, type, name); |
| 1265 |
AST_RWLIST_UNLOCK(&xmldoc_tree); |
| 1266 |
return syntax; |
| 1267 |
} |
| 1268 |
|
| 1269 |
|
| 1270 |
|
| 1271 |
|
| 1272 |
|
| 1273 |
|
| 1274 |
|
| 1275 |
|
| 1276 |
|
| 1277 |
|
| 1278 |
|
| 1279 |
|
| 1280 |
|
| 1281 |
|
| 1282 |
|
| 1283 |
|
| 1284 |
|
| 1285 |
static int xmldoc_parse_common_elements(struct ast_xml_node *node, const char *tabs, const char *posttabs, struct ast_str **buffer) |
| 1286 |
{ |
| 1287 |
return (xmldoc_parse_para(node, tabs, posttabs, buffer) |
| 1288 |
|| xmldoc_parse_specialtags(node, tabs, posttabs, buffer) |
| 1289 |
|| xmldoc_parse_info(node, tabs, posttabs, buffer)); |
| 1290 |
} |
| 1291 |
|
| 1292 |
|
| 1293 |
|
| 1294 |
|
| 1295 |
|
| 1296 |
|
| 1297 |
|
| 1298 |
|
| 1299 |
|
| 1300 |
|
| 1301 |
|
| 1302 |
|
| 1303 |
|
| 1304 |
|
| 1305 |
|
| 1306 |
|
| 1307 |
static int xmldoc_parse_para(struct ast_xml_node *node, const char *tabs, const char *posttabs, struct ast_str **buffer) |
| 1308 |
{ |
| 1309 |
const char *tmptext; |
| 1310 |
struct ast_xml_node *tmp; |
| 1311 |
int ret = 0; |
| 1312 |
struct ast_str *tmpstr; |
| 1313 |
|
| 1314 |
if (!node || !ast_xml_node_get_children(node)) { |
| 1315 |
return ret; |
| 1316 |
} |
| 1317 |
|
| 1318 |
if (strcasecmp(ast_xml_node_get_name(node), "para")) { |
| 1319 |
return ret; |
| 1320 |
} |
| 1321 |
|
| 1322 |
ast_str_append(buffer, 0, "%s", tabs); |
| 1323 |
|
| 1324 |
ret = 1; |
| 1325 |
|
| 1326 |
for (tmp = ast_xml_node_get_children(node); tmp; tmp = ast_xml_node_get_next(tmp)) { |
| 1327 |
|
| 1328 |
tmptext = ast_xml_get_text(tmp); |
| 1329 |
if (tmptext) { |
| 1330 |
|
| 1331 |
xmldoc_string_cleanup(tmptext, &tmpstr, 0, 0); |
| 1332 |
ast_xml_free_text(tmptext); |
| 1333 |
if (tmpstr) { |
| 1334 |
if (strcasecmp(ast_xml_node_get_name(tmp), "text")) { |
| 1335 |
ast_str_append(buffer, 0, "<%s>%s</%s>", ast_xml_node_get_name(tmp), |
| 1336 |
ast_str_buffer(tmpstr), ast_xml_node_get_name(tmp)); |
| 1337 |
} else { |
| 1338 |
ast_str_append(buffer, 0, "%s", ast_str_buffer(tmpstr)); |
| 1339 |
} |
| 1340 |
ast_free(tmpstr); |
| 1341 |
ret = 2; |
| 1342 |
} |
| 1343 |
} |
| 1344 |
} |
| 1345 |
|
| 1346 |
ast_str_append(buffer, 0, "%s", posttabs); |
| 1347 |
|
| 1348 |
return ret; |
| 1349 |
} |
| 1350 |
|
| 1351 |
|
| 1352 |
|
| 1353 |
|
| 1354 |
|
| 1355 |
|
| 1356 |
|
| 1357 |
|
| 1358 |
|
| 1359 |
|
| 1360 |
|
| 1361 |
|
| 1362 |
static int xmldoc_parse_example(struct ast_xml_node *fixnode, const char *tabs, |
| 1363 |
struct ast_str **buffer) |
| 1364 |
{ |
| 1365 |
struct ast_xml_node *node = fixnode; |
| 1366 |
const char *tmptext; |
| 1367 |
const char *title; |
| 1368 |
struct ast_str *stripped_text; |
| 1369 |
int ret = 0; |
| 1370 |
|
| 1371 |
if (!node || !ast_xml_node_get_children(node)) { |
| 1372 |
return ret; |
| 1373 |
} |
| 1374 |
|
| 1375 |
if (strcasecmp(ast_xml_node_get_name(node), "example")) { |
| 1376 |
return ret; |
| 1377 |
} |
| 1378 |
|
| 1379 |
ret = 1; |
| 1380 |
|
| 1381 |
title = ast_xml_get_attribute(node, "title"); |
| 1382 |
if (title) { |
| 1383 |
ast_str_append(buffer, 0, "%s", title); |
| 1384 |
ast_xml_free_attr(title); |
| 1385 |
} |
| 1386 |
ast_str_append(buffer, 0, "\n"); |
| 1387 |
|
| 1388 |
for (node = ast_xml_node_get_children(node); node; node = ast_xml_node_get_next(node)) { |
| 1389 |
tmptext = ast_xml_get_text(node); |
| 1390 |
if (tmptext) { |
| 1391 |
const char *skipped = ast_skip_blanks(tmptext); |
| 1392 |
xmldoc_string_cleanup(skipped, &stripped_text, 0, 1); |
| 1393 |
if (stripped_text) { |
| 1394 |
ast_str_append(buffer, 0, "\n %s<exampletext>%s</exampletext>\n", |
| 1395 |
tabs, ast_str_buffer(stripped_text)); |
| 1396 |
ast_xml_free_text(tmptext); |
| 1397 |
ast_free(stripped_text); |
| 1398 |
} |
| 1399 |
} |
| 1400 |
} |
| 1401 |
|
| 1402 |
return ret; |
| 1403 |
} |
| 1404 |
|
| 1405 |
|
| 1406 |
|
| 1407 |
|
| 1408 |
|
| 1409 |
|
| 1410 |
|
| 1411 |
|
| 1412 |
|
| 1413 |
|
| 1414 |
|
| 1415 |
|
| 1416 |
|
| 1417 |
|
| 1418 |
static int xmldoc_parse_specialtags(struct ast_xml_node *fixnode, const char *tabs, const char *posttabs, struct ast_str **buffer) |
| 1419 |
{ |
| 1420 |
struct ast_xml_node *node = fixnode; |
| 1421 |
int ret = 0, i; |
| 1422 |
|
| 1423 |
if (!node || !ast_xml_node_get_children(node)) { |
| 1424 |
return ret; |
| 1425 |
} |
| 1426 |
|
| 1427 |
for (i = 0; i < ARRAY_LEN(special_tags); i++) { |
| 1428 |
if (strcasecmp(ast_xml_node_get_name(node), special_tags[i].tagname)) { |
| 1429 |
continue; |
| 1430 |
} |
| 1431 |
|
| 1432 |
ret = 1; |
| 1433 |
|
| 1434 |
|
| 1435 |
|
| 1436 |
if (!ast_strlen_zero(special_tags[i].init)) { |
| 1437 |
ast_str_append(buffer, 0, "%s%s", tabs, special_tags[i].init); |
| 1438 |
} |
| 1439 |
|
| 1440 |
if (xmldoc_parse_example(node, tabs, buffer)) { |
| 1441 |
ret = 1; |
| 1442 |
break; |
| 1443 |
} |
| 1444 |
|
| 1445 |
|
| 1446 |
for (node = ast_xml_node_get_children(node); node; node = ast_xml_node_get_next(node)) { |
| 1447 |
|
| 1448 |
if ((xmldoc_parse_para(node, "", posttabs, buffer) == 2) |
| 1449 |
|| (xmldoc_parse_info(node, "", posttabs, buffer) == 2)) { |
| 1450 |
ret = 2; |
| 1451 |
} |
| 1452 |
} |
| 1453 |
|
| 1454 |
if (!ast_strlen_zero(special_tags[i].end)) { |
| 1455 |
ast_str_append(buffer, 0, "%s%s", special_tags[i].end, posttabs); |
| 1456 |
} |
| 1457 |
|
| 1458 |
break; |
| 1459 |
} |
| 1460 |
|
| 1461 |
return ret; |
| 1462 |
} |
| 1463 |
|
| 1464 |
|
| 1465 |
|
| 1466 |
|
| 1467 |
|
| 1468 |
|
| 1469 |
|
| 1470 |
|
| 1471 |
|
| 1472 |
|
| 1473 |
|
| 1474 |
|
| 1475 |
|
| 1476 |
|
| 1477 |
static int xmldoc_parse_argument(struct ast_xml_node *fixnode, int insideparameter, const char *paramtabs, const char *tabs, struct ast_str **buffer) |
| 1478 |
{ |
| 1479 |
struct ast_xml_node *node = fixnode; |
| 1480 |
const char *argname; |
| 1481 |
int count = 0, ret = 0; |
| 1482 |
|
| 1483 |
if (!node || !ast_xml_node_get_children(node)) { |
| 1484 |
return ret; |
| 1485 |
} |
| 1486 |
|
| 1487 |
|
| 1488 |
argname = ast_xml_get_attribute(node, "name"); |
| 1489 |
if (!argname) { |
| 1490 |
return 0; |
| 1491 |
} |
| 1492 |
if (xmldoc_has_inside(node, "para") || xmldoc_has_inside(node, "info") || xmldoc_has_specialtags(node)) { |
| 1493 |
ast_str_append(buffer, 0, "%s%s%s", tabs, argname, (insideparameter ? "\n" : "")); |
| 1494 |
ast_xml_free_attr(argname); |
| 1495 |
} else { |
| 1496 |
ast_xml_free_attr(argname); |
| 1497 |
return 0; |
| 1498 |
} |
| 1499 |
|
| 1500 |
for (node = ast_xml_node_get_children(node); node; node = ast_xml_node_get_next(node)) { |
| 1501 |
int rc = 0; |
| 1502 |
rc = xmldoc_parse_common_elements(node, (insideparameter ? paramtabs : (!count ? " - " : tabs)), "\n", buffer); |
| 1503 |
if (rc >= 1) { |
| 1504 |
count++; |
| 1505 |
ret = 1; |
| 1506 |
} |
| 1507 |
} |
| 1508 |
|
| 1509 |
return ret; |
| 1510 |
} |
| 1511 |
|
| 1512 |
|
| 1513 |
|
| 1514 |
|
| 1515 |
|
| 1516 |
|
| 1517 |
|
| 1518 |
|
| 1519 |
|
| 1520 |
|
| 1521 |
|
| 1522 |
|
| 1523 |
|
| 1524 |
|
| 1525 |
|
| 1526 |
static int xmldoc_parse_variable(struct ast_xml_node *node, const char *tabs, struct ast_str **buffer) |
| 1527 |
{ |
| 1528 |
struct ast_xml_node *tmp; |
| 1529 |
const char *valname; |
| 1530 |
const char *tmptext; |
| 1531 |
struct ast_str *cleanstr; |
| 1532 |
int ret = 0, printedpara=0; |
| 1533 |
|
| 1534 |
for (tmp = ast_xml_node_get_children(node); tmp; tmp = ast_xml_node_get_next(tmp)) { |
| 1535 |
if (xmldoc_parse_common_elements(tmp, (ret ? tabs : ""), "\n", buffer)) { |
| 1536 |
printedpara = 1; |
| 1537 |
continue; |
| 1538 |
} |
| 1539 |
|
| 1540 |
if (strcasecmp(ast_xml_node_get_name(tmp), "value")) { |
| 1541 |
continue; |
| 1542 |
} |
| 1543 |
|
| 1544 |
|
| 1545 |
if (!printedpara) { |
| 1546 |
ast_str_append(buffer, 0, "\n"); |
| 1547 |
printedpara = 1; |
| 1548 |
} |
| 1549 |
|
| 1550 |
valname = ast_xml_get_attribute(tmp, "name"); |
| 1551 |
if (valname) { |
| 1552 |
ret = 1; |
| 1553 |
ast_str_append(buffer, 0, "%s<value>%s</value>", tabs, valname); |
| 1554 |
ast_xml_free_attr(valname); |
| 1555 |
} |
| 1556 |
tmptext = ast_xml_get_text(tmp); |
| 1557 |
|
| 1558 |
if (tmptext) { |
| 1559 |
|
| 1560 |
xmldoc_string_cleanup(tmptext, &cleanstr, 1, 0); |
| 1561 |
ast_xml_free_text(tmptext); |
| 1562 |
if (cleanstr && ast_str_strlen(cleanstr) > 0) { |
| 1563 |
ast_str_append(buffer, 0, ":%s", ast_str_buffer(cleanstr)); |
| 1564 |
} |
| 1565 |
ast_free(cleanstr); |
| 1566 |
} |
| 1567 |
ast_str_append(buffer, 0, "\n"); |
| 1568 |
} |
| 1569 |
|
| 1570 |
return ret; |
| 1571 |
} |
| 1572 |
|
| 1573 |
|
| 1574 |
|
| 1575 |
|
| 1576 |
|
| 1577 |
|
| 1578 |
|
| 1579 |
|
| 1580 |
|
| 1581 |
|
| 1582 |
|
| 1583 |
|
| 1584 |
|
| 1585 |
|
| 1586 |
|
| 1587 |
static int xmldoc_parse_variablelist(struct ast_xml_node *node, const char *tabs, struct ast_str **buffer) |
| 1588 |
{ |
| 1589 |
struct ast_xml_node *tmp; |
| 1590 |
const char *varname; |
| 1591 |
char *vartabs; |
| 1592 |
int ret = 0; |
| 1593 |
|
| 1594 |
if (!node || !ast_xml_node_get_children(node)) { |
| 1595 |
return ret; |
| 1596 |
} |
| 1597 |
|
| 1598 |
if (strcasecmp(ast_xml_node_get_name(node), "variablelist")) { |
| 1599 |
return ret; |
| 1600 |
} |
| 1601 |
|
| 1602 |
|
| 1603 |
if (ast_asprintf(&vartabs, "%s ", tabs) < 0) { |
| 1604 |
return ret; |
| 1605 |
} |
| 1606 |
for (tmp = ast_xml_node_get_children(node); tmp; tmp = ast_xml_node_get_next(tmp)) { |
| 1607 |
|
| 1608 |
if (xmldoc_parse_common_elements(tmp, (ret ? tabs : ""), "\n", buffer)) { |
| 1609 |
ret = 1; |
| 1610 |
continue; |
| 1611 |
} |
| 1612 |
|
| 1613 |
if (!strcasecmp(ast_xml_node_get_name(tmp), "variable")) { |
| 1614 |
|
| 1615 |
varname = ast_xml_get_attribute(tmp, "name"); |
| 1616 |
if (varname) { |
| 1617 |
ast_str_append(buffer, 0, "%s<variable>%s</variable>: ", tabs, varname); |
| 1618 |
ast_xml_free_attr(varname); |
| 1619 |
|
| 1620 |
xmldoc_parse_variable(tmp, vartabs, buffer); |
| 1621 |
ret = 1; |
| 1622 |
} |
| 1623 |
} |
| 1624 |
} |
| 1625 |
|
| 1626 |
ast_free(vartabs); |
| 1627 |
|
| 1628 |
return ret; |
| 1629 |
} |
| 1630 |
|
| 1631 |
|
| 1632 |
|
| 1633 |
|
| 1634 |
|
| 1635 |
|
| 1636 |
|
| 1637 |
|
| 1638 |
|
| 1639 |
|
| 1640 |
|
| 1641 |
|
| 1642 |
|
| 1643 |
|
| 1644 |
|
| 1645 |
static char *_ast_xmldoc_build_seealso(struct ast_xml_node *node) |
| 1646 |
{ |
| 1647 |
char *output; |
| 1648 |
struct ast_str *outputstr; |
| 1649 |
const char *typename; |
| 1650 |
const char *content; |
| 1651 |
int first = 1; |
| 1652 |
|
| 1653 |
|
| 1654 |
for (node = ast_xml_node_get_children(node); node; node = ast_xml_node_get_next(node)) { |
| 1655 |
if (!strcasecmp(ast_xml_node_get_name(node), "see-also")) { |
| 1656 |
break; |
| 1657 |
} |
| 1658 |
} |
| 1659 |
|
| 1660 |
if (!node || !ast_xml_node_get_children(node)) { |
| 1661 |
|
| 1662 |
return NULL; |
| 1663 |
} |
| 1664 |
|
| 1665 |
|
| 1666 |
outputstr = ast_str_create(128); |
| 1667 |
if (!outputstr) { |
| 1668 |
return NULL; |
| 1669 |
} |
| 1670 |
|
| 1671 |
|
| 1672 |
for (node = ast_xml_node_get_children(node); node; node = ast_xml_node_get_next(node)) { |
| 1673 |
if (strcasecmp(ast_xml_node_get_name(node), "ref")) { |
| 1674 |
continue; |
| 1675 |
} |
| 1676 |
|
| 1677 |
|
| 1678 |
typename = ast_xml_get_attribute(node, "type"); |
| 1679 |
if (!typename) { |
| 1680 |
continue; |
| 1681 |
} |
| 1682 |
content = ast_xml_get_text(node); |
| 1683 |
if (!content) { |
| 1684 |
ast_xml_free_attr(typename); |
| 1685 |
continue; |
| 1686 |
} |
| 1687 |
if (!strcasecmp(typename, "application")) { |
| 1688 |
ast_str_append(&outputstr, 0, "%s%s()", (first ? "" : ", "), content); |
| 1689 |
} else if (!strcasecmp(typename, "function")) { |
| 1690 |
ast_str_append(&outputstr, 0, "%s%s", (first ? "" : ", "), content); |
| 1691 |
} else if (!strcasecmp(typename, "astcli")) { |
| 1692 |
ast_str_append(&outputstr, 0, "%s<astcli>%s</astcli>", (first ? "" : ", "), content); |
| 1693 |
} else { |
| 1694 |
ast_str_append(&outputstr, 0, "%s%s", (first ? "" : ", "), content); |
| 1695 |
} |
| 1696 |
first = 0; |
| 1697 |
ast_xml_free_text(content); |
| 1698 |
ast_xml_free_attr(typename); |
| 1699 |
} |
| 1700 |
|
| 1701 |
output = ast_strdup(ast_str_buffer(outputstr)); |
| 1702 |
ast_free(outputstr); |
| 1703 |
|
| 1704 |
return output; |
| 1705 |
} |
| 1706 |
|
| 1707 |
char *ast_xmldoc_build_seealso(const char *type, const char *name, const char *module) |
| 1708 |
{ |
| 1709 |
char *output; |
| 1710 |
struct ast_xml_node *node; |
| 1711 |
|
| 1712 |
if (ast_strlen_zero(type) || ast_strlen_zero(name)) { |
| 1713 |
return NULL; |
| 1714 |
} |
| 1715 |
|
| 1716 |
|
| 1717 |
AST_RWLIST_RDLOCK(&xmldoc_tree); |
| 1718 |
node = xmldoc_get_node(type, name, module, documentation_language); |
| 1719 |
if (!node || !ast_xml_node_get_children(node)) { |
| 1720 |
AST_RWLIST_UNLOCK(&xmldoc_tree); |
| 1721 |
return NULL; |
| 1722 |
} |
| 1723 |
|
| 1724 |
output = _ast_xmldoc_build_seealso(node); |
| 1725 |
AST_RWLIST_UNLOCK(&xmldoc_tree); |
| 1726 |
|
| 1727 |
return output; |
| 1728 |
} |
| 1729 |
|
| 1730 |
|
| 1731 |
|
| 1732 |
|
| 1733 |
|
| 1734 |
|
| 1735 |
|
| 1736 |
|
| 1737 |
|
| 1738 |
|
| 1739 |
|
| 1740 |
|
| 1741 |
|
| 1742 |
|
| 1743 |
|
| 1744 |
static char *_ast_xmldoc_build_since(struct ast_xml_node *node) |
| 1745 |
{ |
| 1746 |
char *output; |
| 1747 |
struct ast_str *outputstr; |
| 1748 |
const char *content; |
| 1749 |
int first = 1; |
| 1750 |
|
| 1751 |
|
| 1752 |
for (node = ast_xml_node_get_children(node); node; node = ast_xml_node_get_next(node)) { |
| 1753 |
if (!strcasecmp(ast_xml_node_get_name(node), "since")) { |
| 1754 |
break; |
| 1755 |
} |
| 1756 |
} |
| 1757 |
|
| 1758 |
if (!node || !ast_xml_node_get_children(node)) { |
| 1759 |
|
| 1760 |
return NULL; |
| 1761 |
} |
| 1762 |
|
| 1763 |
|
| 1764 |
outputstr = ast_str_create(128); |
| 1765 |
if (!outputstr) { |
| 1766 |
return NULL; |
| 1767 |
} |
| 1768 |
|
| 1769 |
|
| 1770 |
for (node = ast_xml_node_get_children(node); node; node = ast_xml_node_get_next(node)) { |
| 1771 |
if (strcasecmp(ast_xml_node_get_name(node), "version")) { |
| 1772 |
continue; |
| 1773 |
} |
| 1774 |
|
| 1775 |
content = ast_xml_get_text(node); |
| 1776 |
if (!content) { |
| 1777 |
continue; |
| 1778 |
} |
| 1779 |
|
| 1780 |
ast_str_append(&outputstr, 0, "%s%s", (first ? "" : ", "), content); |
| 1781 |
|
| 1782 |
first = 0; |
| 1783 |
ast_xml_free_text(content); |
| 1784 |
} |
| 1785 |
|
| 1786 |
output = ast_strdup(ast_str_buffer(outputstr)); |
| 1787 |
ast_free(outputstr); |
| 1788 |
|
| 1789 |
return output; |
| 1790 |
} |
| 1791 |
|
| 1792 |
char *ast_xmldoc_build_since(const char *type, const char *name, const char *module) |
| 1793 |
{ |
| 1794 |
char *output; |
| 1795 |
struct ast_xml_node *node; |
| 1796 |
|
| 1797 |
if (ast_strlen_zero(type) || ast_strlen_zero(name)) { |
| 1798 |
return NULL; |
| 1799 |
} |
| 1800 |
|
| 1801 |
|
| 1802 |
AST_RWLIST_RDLOCK(&xmldoc_tree); |
| 1803 |
node = xmldoc_get_node(type, name, module, documentation_language); |
| 1804 |
if (!node || !ast_xml_node_get_children(node)) { |
| 1805 |
AST_RWLIST_UNLOCK(&xmldoc_tree); |
| 1806 |
return NULL; |
| 1807 |
} |
| 1808 |
|
| 1809 |
output = _ast_xmldoc_build_since(node); |
| 1810 |
AST_RWLIST_UNLOCK(&xmldoc_tree); |
| 1811 |
|
| 1812 |
return output; |
| 1813 |
} |
| 1814 |
|
| 1815 |
|
| 1816 |
|
| 1817 |
|
| 1818 |
|
| 1819 |
|
| 1820 |
|
| 1821 |
|
| 1822 |
|
| 1823 |
|
| 1824 |
|
| 1825 |
|
| 1826 |
|
| 1827 |
|
| 1828 |
|
| 1829 |
|
| 1830 |
static char *_ast_xmldoc_build_provided_by(struct ast_xml_node *node) |
| 1831 |
{ |
| 1832 |
const char *attr; |
| 1833 |
char *output; |
| 1834 |
|
| 1835 |
attr = ast_xml_get_attribute(node, "module"); |
| 1836 |
if (ast_strlen_zero(attr)) { |
| 1837 |
return NULL; |
| 1838 |
} |
| 1839 |
output = ast_strdup(attr); |
| 1840 |
ast_xml_free_attr(attr); |
| 1841 |
return output; |
| 1842 |
} |
| 1843 |
|
| 1844 |
char *ast_xmldoc_build_provided_by(const char *type, const char *name, const char *module) |
| 1845 |
{ |
| 1846 |
char *output; |
| 1847 |
struct ast_xml_node *node; |
| 1848 |
|
| 1849 |
if (ast_strlen_zero(type) || ast_strlen_zero(name)) { |
| 1850 |
return NULL; |
| 1851 |
} |
| 1852 |
|
| 1853 |
|
| 1854 |
AST_RWLIST_RDLOCK(&xmldoc_tree); |
| 1855 |
node = xmldoc_get_node(type, name, module, documentation_language); |
| 1856 |
if (!node || !ast_xml_node_get_children(node)) { |
| 1857 |
AST_RWLIST_UNLOCK(&xmldoc_tree); |
| 1858 |
return NULL; |
| 1859 |
} |
| 1860 |
|
| 1861 |
output = _ast_xmldoc_build_provided_by(node); |
| 1862 |
AST_RWLIST_UNLOCK(&xmldoc_tree); |
| 1863 |
|
| 1864 |
return output; |
| 1865 |
} |
| 1866 |
|
| 1867 |
|
| 1868 |
|
| 1869 |
|
| 1870 |
|
| 1871 |
|
| 1872 |
|
| 1873 |
|
| 1874 |
|
| 1875 |
|
| 1876 |
|
| 1877 |
|
| 1878 |
static int xmldoc_parse_enum(struct ast_xml_node *fixnode, const char *tabs, struct ast_str **buffer) |
| 1879 |
{ |
| 1880 |
struct ast_xml_node *node = fixnode; |
| 1881 |
int ret = 0; |
| 1882 |
char *optiontabs; |
| 1883 |
|
| 1884 |
if (ast_asprintf(&optiontabs, "%s ", tabs) < 0) { |
| 1885 |
return ret; |
| 1886 |
} |
| 1887 |
|
| 1888 |
for (node = ast_xml_node_get_children(node); node; node = ast_xml_node_get_next(node)) { |
| 1889 |
if (xmldoc_parse_common_elements(node, (ret ? tabs : " - "), "\n", buffer)) { |
| 1890 |
ret = 1; |
| 1891 |
} |
| 1892 |
|
| 1893 |
xmldoc_parse_enumlist(node, optiontabs, buffer); |
| 1894 |
xmldoc_parse_parameter(node, optiontabs, buffer); |
| 1895 |
} |
| 1896 |
|
| 1897 |
ast_free(optiontabs); |
| 1898 |
|
| 1899 |
return ret; |
| 1900 |
} |
| 1901 |
|
| 1902 |
|
| 1903 |
|
| 1904 |
|
| 1905 |
|
| 1906 |
|
| 1907 |
|
| 1908 |
|
| 1909 |
|
| 1910 |
|
| 1911 |
|
| 1912 |
|
| 1913 |
static int xmldoc_parse_enumlist(struct ast_xml_node *fixnode, const char *tabs, struct ast_str **buffer) |
| 1914 |
{ |
| 1915 |
struct ast_xml_node *node = fixnode; |
| 1916 |
const char *enumname; |
| 1917 |
int ret = 0; |
| 1918 |
|
| 1919 |
for (node = ast_xml_node_get_children(node); node; node = ast_xml_node_get_next(node)) { |
| 1920 |
if (strcasecmp(ast_xml_node_get_name(node), "enum")) { |
| 1921 |
continue; |
| 1922 |
} |
| 1923 |
|
| 1924 |
enumname = ast_xml_get_attribute(node, "name"); |
| 1925 |
if (enumname) { |
| 1926 |
ast_str_append(buffer, 0, "%s<enum>%s</enum>", tabs, enumname); |
| 1927 |
ast_xml_free_attr(enumname); |
| 1928 |
|
| 1929 |
|
| 1930 |
if ((xmldoc_parse_enum(node, tabs, buffer))) { |
| 1931 |
ret = 1; |
| 1932 |
} else { |
| 1933 |
ast_str_append(buffer, 0, "\n"); |
| 1934 |
} |
| 1935 |
} |
| 1936 |
} |
| 1937 |
return ret; |
| 1938 |
} |
| 1939 |
|
| 1940 |
|
| 1941 |
|
| 1942 |
|
| 1943 |
|
| 1944 |
|
| 1945 |
|
| 1946 |
|
| 1947 |
|
| 1948 |
|
| 1949 |
|
| 1950 |
|
| 1951 |
|
| 1952 |
static int xmldoc_parse_option(struct ast_xml_node *fixnode, const char *tabs, struct ast_str **buffer) |
| 1953 |
{ |
| 1954 |
struct ast_xml_node *node; |
| 1955 |
int ret = 0; |
| 1956 |
char *optiontabs; |
| 1957 |
|
| 1958 |
if (ast_asprintf(&optiontabs, "%s ", tabs) < 0) { |
| 1959 |
return ret; |
| 1960 |
} |
| 1961 |
for (node = ast_xml_node_get_children(fixnode); node; node = ast_xml_node_get_next(node)) { |
| 1962 |
|
| 1963 |
if (!ret && ast_xml_node_get_children(node)) { |
| 1964 |
|
| 1965 |
ast_str_append(buffer, 0, "\n"); |
| 1966 |
} |
| 1967 |
if (!strcasecmp(ast_xml_node_get_name(node), "argument")) { |
| 1968 |
if (xmldoc_parse_argument(node, 0, NULL, optiontabs, buffer)) { |
| 1969 |
ret = 1; |
| 1970 |
} |
| 1971 |
continue; |
| 1972 |
} |
| 1973 |
|
| 1974 |
if (xmldoc_parse_common_elements(node, optiontabs, "\n", buffer)) { |
| 1975 |
ret = 1; |
| 1976 |
} |
| 1977 |
|
| 1978 |
xmldoc_parse_variablelist(node, optiontabs, buffer); |
| 1979 |
|
| 1980 |
xmldoc_parse_enumlist(node, optiontabs, buffer); |
| 1981 |
} |
| 1982 |
ast_free(optiontabs); |
| 1983 |
|
| 1984 |
return ret; |
| 1985 |
} |
| 1986 |
|
| 1987 |
|
| 1988 |
|
| 1989 |
|
| 1990 |
|
| 1991 |
|
| 1992 |
|
| 1993 |
|
| 1994 |
|
| 1995 |
|
| 1996 |
static void xmldoc_parse_optionlist(struct ast_xml_node *fixnode, const char *tabs, struct ast_str **buffer) |
| 1997 |
{ |
| 1998 |
struct ast_xml_node *node; |
| 1999 |
const char *optname, *hasparams; |
| 2000 |
char *optionsyntax; |
| 2001 |
int optparams; |
| 2002 |
|
| 2003 |
for (node = ast_xml_node_get_children(fixnode); node; node = ast_xml_node_get_next(node)) { |
| 2004 |
|
| 2005 |
if (strcasecmp(ast_xml_node_get_name(node), "option")) { |
| 2006 |
continue; |
| 2007 |
} |
| 2008 |
|
| 2009 |
|
| 2010 |
optname = ast_xml_get_attribute(node, "name"); |
| 2011 |
if (!optname) { |
| 2012 |
continue; |
| 2013 |
} |
| 2014 |
|
| 2015 |
optparams = 1; |
| 2016 |
hasparams = ast_xml_get_attribute(node, "hasparams"); |
| 2017 |
if (hasparams && !strcasecmp(hasparams, "optional")) { |
| 2018 |
optparams = 2; |
| 2019 |
} |
| 2020 |
|
| 2021 |
optionsyntax = xmldoc_get_syntax_fun(node, optname, "argument", 0, optparams); |
| 2022 |
if (!optionsyntax) { |
| 2023 |
ast_xml_free_attr(optname); |
| 2024 |
ast_xml_free_attr(hasparams); |
| 2025 |
continue; |
| 2026 |
} |
| 2027 |
|
| 2028 |
ast_str_append(buffer, 0, "%s%s: ", tabs, optionsyntax); |
| 2029 |
|
| 2030 |
if (!xmldoc_parse_option(node, tabs, buffer)) { |
| 2031 |
ast_str_append(buffer, 0, "\n"); |
| 2032 |
} |
| 2033 |
ast_str_append(buffer, 0, "\n"); |
| 2034 |
ast_xml_free_attr(optname); |
| 2035 |
ast_xml_free_attr(hasparams); |
| 2036 |
ast_free(optionsyntax); |
| 2037 |
} |
| 2038 |
} |
| 2039 |
|
| 2040 |
|
| 2041 |
|
| 2042 |
|
| 2043 |
|
| 2044 |
|
| 2045 |
|
| 2046 |
|
| 2047 |
|
| 2048 |
|
| 2049 |
static void xmldoc_parse_parameter(struct ast_xml_node *fixnode, const char *tabs, struct ast_str **buffer) |
| 2050 |
{ |
| 2051 |
const char *paramname; |
| 2052 |
struct ast_xml_node *node = fixnode; |
| 2053 |
int hasarguments, printed = 0; |
| 2054 |
char *internaltabs; |
| 2055 |
|
| 2056 |
if (strcasecmp(ast_xml_node_get_name(node), "parameter")) { |
| 2057 |
return; |
| 2058 |
} |
| 2059 |
|
| 2060 |
hasarguments = xmldoc_has_inside(node, "argument"); |
| 2061 |
if (!(paramname = ast_xml_get_attribute(node, "name"))) { |
| 2062 |
|
| 2063 |
return; |
| 2064 |
} |
| 2065 |
|
| 2066 |
if (ast_asprintf(&internaltabs, "%s ", tabs) < 0) { |
| 2067 |
ast_xml_free_attr(paramname); |
| 2068 |
return; |
| 2069 |
} |
| 2070 |
|
| 2071 |
if (!hasarguments && xmldoc_has_nodes(node)) { |
| 2072 |
ast_str_append(buffer, 0, "\n%s\n", paramname); |
| 2073 |
ast_xml_free_attr(paramname); |
| 2074 |
printed = 1; |
| 2075 |
} |
| 2076 |
|
| 2077 |
for (node = ast_xml_node_get_children(node); node; node = ast_xml_node_get_next(node)) { |
| 2078 |
if (!strcasecmp(ast_xml_node_get_name(node), "optionlist")) { |
| 2079 |
xmldoc_parse_optionlist(node, internaltabs, buffer); |
| 2080 |
} else if (!strcasecmp(ast_xml_node_get_name(node), "enumlist")) { |
| 2081 |
xmldoc_parse_enumlist(node, internaltabs, buffer); |
| 2082 |
} else if (!strcasecmp(ast_xml_node_get_name(node), "argument")) { |
| 2083 |
xmldoc_parse_argument(node, 1, internaltabs, (!hasarguments ? " " : ""), buffer); |
| 2084 |
} else if (!strcasecmp(ast_xml_node_get_name(node), "para")) { |
| 2085 |
if (!printed) { |
| 2086 |
ast_str_append(buffer, 0, "%s\n", paramname); |
| 2087 |
ast_xml_free_attr(paramname); |
| 2088 |
printed = 1; |
| 2089 |
} |
| 2090 |
if (xmldoc_parse_para(node, internaltabs, "\n", buffer)) { |
| 2091 |
|
| 2092 |
|
| 2093 |
continue; |
| 2094 |
} |
| 2095 |
continue; |
| 2096 |
} else if (!strcasecmp(ast_xml_node_get_name(node), "info")) { |
| 2097 |
if (!printed) { |
| 2098 |
ast_str_append(buffer, 0, "%s\n", paramname); |
| 2099 |
ast_xml_free_attr(paramname); |
| 2100 |
printed = 1; |
| 2101 |
} |
| 2102 |
if (xmldoc_parse_info(node, internaltabs, "\n", buffer)) { |
| 2103 |
|
| 2104 |
|
| 2105 |
continue; |
| 2106 |
} |
| 2107 |
continue; |
| 2108 |
} else if ((xmldoc_parse_specialtags(node, internaltabs, "\n", buffer))) { |
| 2109 |
continue; |
| 2110 |
} |
| 2111 |
} |
| 2112 |
if (!printed) { |
| 2113 |
ast_xml_free_attr(paramname); |
| 2114 |
} |
| 2115 |
ast_free(internaltabs); |
| 2116 |
} |
| 2117 |
|
| 2118 |
|
| 2119 |
|
| 2120 |
|
| 2121 |
|
| 2122 |
|
| 2123 |
|
| 2124 |
|
| 2125 |
|
| 2126 |
|
| 2127 |
|
| 2128 |
|
| 2129 |
|
| 2130 |
|
| 2131 |
|
| 2132 |
static int xmldoc_parse_info(struct ast_xml_node *node, const char *tabs, const char *posttabs, struct ast_str **buffer) |
| 2133 |
{ |
| 2134 |
const char *tech; |
| 2135 |
const char *provided_by; |
| 2136 |
char *internaltabs; |
| 2137 |
int internal_ret; |
| 2138 |
int ret = 0; |
| 2139 |
|
| 2140 |
if (strcasecmp(ast_xml_node_get_name(node), "info")) { |
| 2141 |
return ret; |
| 2142 |
} |
| 2143 |
|
| 2144 |
ast_asprintf(&internaltabs, "%s ", tabs); |
| 2145 |
if (!internaltabs) { |
| 2146 |
return ret; |
| 2147 |
} |
| 2148 |
|
| 2149 |
tech = ast_xml_get_attribute(node, "tech"); |
| 2150 |
provided_by = ast_xml_get_attribute(node, "module"); |
| 2151 |
|
| 2152 |
if (tech || provided_by) { |
| 2153 |
ast_str_append(buffer, 0, "%s<note>Technology: %s Provided by: %s</note>\n", internaltabs, |
| 2154 |
S_OR(tech, "unknown"), S_OR(provided_by, "unknown")); |
| 2155 |
ast_xml_free_attr(tech); |
| 2156 |
ast_xml_free_attr(provided_by); |
| 2157 |
} |
| 2158 |
|
| 2159 |
ret = 1; |
| 2160 |
|
| 2161 |
for (node = ast_xml_node_get_children(node); node; node = ast_xml_node_get_next(node)) { |
| 2162 |
if (!strcasecmp(ast_xml_node_get_name(node), "enumlist")) { |
| 2163 |
xmldoc_parse_enumlist(node, internaltabs, buffer); |
| 2164 |
} else if (!strcasecmp(ast_xml_node_get_name(node), "parameter")) { |
| 2165 |
xmldoc_parse_parameter(node, internaltabs, buffer); |
| 2166 |
} else if ((internal_ret = xmldoc_parse_common_elements(node, internaltabs, posttabs, buffer))) { |
| 2167 |
if (internal_ret > ret) { |
| 2168 |
ret = internal_ret; |
| 2169 |
} |
| 2170 |
} |
| 2171 |
} |
| 2172 |
ast_free(internaltabs); |
| 2173 |
|
| 2174 |
return ret; |
| 2175 |
} |
| 2176 |
|
| 2177 |
|
| 2178 |
|
| 2179 |
|
| 2180 |
|
| 2181 |
|
| 2182 |
|
| 2183 |
|
| 2184 |
|
| 2185 |
|
| 2186 |
|
| 2187 |
|
| 2188 |
|
| 2189 |
|
| 2190 |
|
| 2191 |
static char *_ast_xmldoc_build_arguments(struct ast_xml_node *node) |
| 2192 |
{ |
| 2193 |
char *retstr = NULL; |
| 2194 |
struct ast_str *ret; |
| 2195 |
|
| 2196 |
ret = ast_str_create(128); |
| 2197 |
if (!ret) { |
| 2198 |
return NULL; |
| 2199 |
} |
| 2200 |
|
| 2201 |
|
| 2202 |
for (node = ast_xml_node_get_children(node); node; node = ast_xml_node_get_next(node)) { |
| 2203 |
if (!strcasecmp(ast_xml_node_get_name(node), "syntax")) { |
| 2204 |
break; |
| 2205 |
} |
| 2206 |
} |
| 2207 |
|
| 2208 |
if (!node || !ast_xml_node_get_children(node)) { |
| 2209 |
|
| 2210 |
ast_free(ret); |
| 2211 |
return NULL; |
| 2212 |
} |
| 2213 |
|
| 2214 |
for (node = ast_xml_node_get_children(node); node; node = ast_xml_node_get_next(node)) { |
| 2215 |
xmldoc_parse_parameter(node, "", &ret); |
| 2216 |
} |
| 2217 |
|
| 2218 |
if (ast_str_strlen(ret) > 0) { |
| 2219 |
|
| 2220 |
char *buf = ast_str_buffer(ret); |
| 2221 |
if (buf[ast_str_strlen(ret) - 1] == '\n') { |
| 2222 |
ast_str_truncate(ret, -1); |
| 2223 |
} |
| 2224 |
retstr = ast_strdup(ast_str_buffer(ret)); |
| 2225 |
} |
| 2226 |
ast_free(ret); |
| 2227 |
|
| 2228 |
return retstr; |
| 2229 |
} |
| 2230 |
|
| 2231 |
char *ast_xmldoc_build_arguments(const char *type, const char *name, const char *module) |
| 2232 |
{ |
| 2233 |
struct ast_xml_node *node; |
| 2234 |
char *arguments; |
| 2235 |
|
| 2236 |
if (ast_strlen_zero(type) || ast_strlen_zero(name)) { |
| 2237 |
return NULL; |
| 2238 |
} |
| 2239 |
|
| 2240 |
AST_RWLIST_RDLOCK(&xmldoc_tree); |
| 2241 |
node = xmldoc_get_node(type, name, module, documentation_language); |
| 2242 |
|
| 2243 |
if (!node || !ast_xml_node_get_children(node)) { |
| 2244 |
AST_RWLIST_UNLOCK(&xmldoc_tree); |
| 2245 |
return NULL; |
| 2246 |
} |
| 2247 |
|
| 2248 |
arguments = _ast_xmldoc_build_arguments(node); |
| 2249 |
AST_RWLIST_UNLOCK(&xmldoc_tree); |
| 2250 |
return arguments; |
| 2251 |
} |
| 2252 |
|
| 2253 |
|
| 2254 |
|
| 2255 |
|
| 2256 |
|
| 2257 |
|
| 2258 |
|
| 2259 |
|
| 2260 |
|
| 2261 |
|
| 2262 |
|
| 2263 |
|
| 2264 |
static struct ast_str *xmldoc_get_formatted(struct ast_xml_node *node, int raw_output, int raw_wrap) |
| 2265 |
{ |
| 2266 |
struct ast_xml_node *tmp; |
| 2267 |
const char *notcleanret, *tmpstr; |
| 2268 |
struct ast_str *ret; |
| 2269 |
|
| 2270 |
if (raw_output) { |
| 2271 |
|
| 2272 |
notcleanret = ast_xml_get_text(node); |
| 2273 |
tmpstr = notcleanret; |
| 2274 |
xmldoc_string_cleanup(ast_skip_blanks(notcleanret), &ret, 0, 0); |
| 2275 |
ast_xml_free_text(tmpstr); |
| 2276 |
} else { |
| 2277 |
ret = ast_str_create(128); |
| 2278 |
if (!ret) { |
| 2279 |
return NULL; |
| 2280 |
} |
| 2281 |
for (tmp = ast_xml_node_get_children(node); tmp; tmp = ast_xml_node_get_next(tmp)) { |
| 2282 |
|
| 2283 |
if (xmldoc_parse_common_elements(tmp, "", "\n", &ret)) { |
| 2284 |
continue; |
| 2285 |
} |
| 2286 |
if (xmldoc_parse_variablelist(tmp, "", &ret)) { |
| 2287 |
continue; |
| 2288 |
} |
| 2289 |
if (xmldoc_parse_enumlist(tmp, " ", &ret)) { |
| 2290 |
continue; |
| 2291 |
} |
| 2292 |
if (xmldoc_parse_specialtags(tmp, "", "", &ret)) { |
| 2293 |
continue; |
| 2294 |
} |
| 2295 |
} |
| 2296 |
|
| 2297 |
|
| 2298 |
tmpstr = ast_str_buffer(ret); |
| 2299 |
if (tmpstr[ast_str_strlen(ret) - 1] == '\n') { |
| 2300 |
ast_str_truncate(ret, -1); |
| 2301 |
} |
| 2302 |
} |
| 2303 |
return ret; |
| 2304 |
} |
| 2305 |
|
| 2306 |
|
| 2307 |
|
| 2308 |
|
| 2309 |
|
| 2310 |
|
| 2311 |
|
| 2312 |
|
| 2313 |
|
| 2314 |
|
| 2315 |
|
| 2316 |
|
| 2317 |
|
| 2318 |
static char *_xmldoc_build_field(struct ast_xml_node *node, const char *var, int raw) |
| 2319 |
{ |
| 2320 |
char *ret = NULL; |
| 2321 |
struct ast_str *formatted; |
| 2322 |
|
| 2323 |
node = ast_xml_find_element(ast_xml_node_get_children(node), var, NULL, NULL); |
| 2324 |
|
| 2325 |
if (!node || !ast_xml_node_get_children(node)) { |
| 2326 |
return ret; |
| 2327 |
} |
| 2328 |
|
| 2329 |
formatted = xmldoc_get_formatted(node, raw, raw); |
| 2330 |
if (formatted && ast_str_strlen(formatted) > 0) { |
| 2331 |
ret = ast_strdup(ast_str_buffer(formatted)); |
| 2332 |
} |
| 2333 |
ast_free(formatted); |
| 2334 |
|
| 2335 |
return ret; |
| 2336 |
} |
| 2337 |
|
| 2338 |
|
| 2339 |
|
| 2340 |
|
| 2341 |
|
| 2342 |
|
| 2343 |
|
| 2344 |
|
| 2345 |
|
| 2346 |
|
| 2347 |
|
| 2348 |
|
| 2349 |
|
| 2350 |
|
| 2351 |
static char *xmldoc_build_field(const char *type, const char *name, const char *module, const char *var, int raw) |
| 2352 |
{ |
| 2353 |
struct ast_xml_node *node; |
| 2354 |
char *field; |
| 2355 |
|
| 2356 |
if (ast_strlen_zero(type) || ast_strlen_zero(name)) { |
| 2357 |
ast_log(LOG_ERROR, "Tried to look in XML tree with faulty values.\n"); |
| 2358 |
return NULL; |
| 2359 |
} |
| 2360 |
|
| 2361 |
AST_RWLIST_RDLOCK(&xmldoc_tree); |
| 2362 |
node = xmldoc_get_node(type, name, module, documentation_language); |
| 2363 |
|
| 2364 |
if (!node) { |
| 2365 |
AST_RWLIST_UNLOCK(&xmldoc_tree); |
| 2366 |
ast_log(LOG_WARNING, "Couldn't find %s %s in XML documentation" |
| 2367 |
" If this module was recently built, run 'xmldoc reload' to refresh documentation\n", |
| 2368 |
type, name); |
| 2369 |
return NULL; |
| 2370 |
} |
| 2371 |
|
| 2372 |
field = _xmldoc_build_field(node, var, raw); |
| 2373 |
AST_RWLIST_UNLOCK(&xmldoc_tree); |
| 2374 |
return field; |
| 2375 |
} |
| 2376 |
|
| 2377 |
|
| 2378 |
|
| 2379 |
|
| 2380 |
|
| 2381 |
|
| 2382 |
|
| 2383 |
|
| 2384 |
|
| 2385 |
|
| 2386 |
|
| 2387 |
|
| 2388 |
|
| 2389 |
|
| 2390 |
static char *_ast_xmldoc_build_synopsis(struct ast_xml_node *node) |
| 2391 |
{ |
| 2392 |
return _xmldoc_build_field(node, "synopsis", 1); |
| 2393 |
} |
| 2394 |
|
| 2395 |
char *ast_xmldoc_build_synopsis(const char *type, const char *name, const char *module) |
| 2396 |
{ |
| 2397 |
return xmldoc_build_field(type, name, module, "synopsis", 1); |
| 2398 |
} |
| 2399 |
|
| 2400 |
|
| 2401 |
|
| 2402 |
|
| 2403 |
|
| 2404 |
|
| 2405 |
|
| 2406 |
|
| 2407 |
|
| 2408 |
|
| 2409 |
|
| 2410 |
|
| 2411 |
|
| 2412 |
|
| 2413 |
static char *_ast_xmldoc_build_description(struct ast_xml_node *node) |
| 2414 |
{ |
| 2415 |
return _xmldoc_build_field(node, "description", 0); |
| 2416 |
} |
| 2417 |
|
| 2418 |
char *ast_xmldoc_build_description(const char *type, const char *name, const char *module) |
| 2419 |
{ |
| 2420 |
return xmldoc_build_field(type, name, module, "description", 0); |
| 2421 |
} |
| 2422 |
|
| 2423 |
|
| 2424 |
|
| 2425 |
|
| 2426 |
|
| 2427 |
|
| 2428 |
static void ast_xml_doc_item_destructor(void *obj) |
| 2429 |
{ |
| 2430 |
struct ast_xml_doc_item *doc = obj; |
| 2431 |
|
| 2432 |
if (!doc) { |
| 2433 |
return; |
| 2434 |
} |
| 2435 |
|
| 2436 |
ast_free(doc->synopsis); |
| 2437 |
ast_free(doc->provided_by); |
| 2438 |
ast_free(doc->since); |
| 2439 |
ast_free(doc->description); |
| 2440 |
ast_free(doc->syntax); |
| 2441 |
ast_free(doc->arguments); |
| 2442 |
ast_free(doc->seealso); |
| 2443 |
ast_string_field_free_memory(doc); |
| 2444 |
|
| 2445 |
if (AST_LIST_NEXT(doc, next)) { |
| 2446 |
ao2_ref(AST_LIST_NEXT(doc, next), -1); |
| 2447 |
AST_LIST_NEXT(doc, next) = NULL; |
| 2448 |
} |
| 2449 |
} |
| 2450 |
|
| 2451 |
|
| 2452 |
|
| 2453 |
|
| 2454 |
|
| 2455 |
|
| 2456 |
|
| 2457 |
|
| 2458 |
|
| 2459 |
static struct ast_xml_doc_item *ast_xml_doc_item_alloc(const char *name, const char *type) |
| 2460 |
{ |
| 2461 |
struct ast_xml_doc_item *item; |
| 2462 |
|
| 2463 |
item = ao2_alloc_options(sizeof(*item), ast_xml_doc_item_destructor, |
| 2464 |
AO2_ALLOC_OPT_LOCK_NOLOCK); |
| 2465 |
if (!item) { |
| 2466 |
ast_log(AST_LOG_ERROR, "Failed to allocate memory for ast_xml_doc_item instance\n"); |
| 2467 |
return NULL; |
| 2468 |
} |
| 2469 |
|
| 2470 |
if ( !(item->synopsis = ast_str_create(128)) |
| 2471 |
|| !(item->provided_by = ast_str_create(128)) |
| 2472 |
|| !(item->since = ast_str_create(128)) |
| 2473 |
|| !(item->description = ast_str_create(128)) |
| 2474 |
|| !(item->syntax = ast_str_create(128)) |
| 2475 |
|| !(item->arguments = ast_str_create(128)) |
| 2476 |
|| !(item->seealso = ast_str_create(128)) |
| 2477 |
) { |
| 2478 |
ast_log(AST_LOG_ERROR, "Failed to allocate strings for ast_xml_doc_item instance\n"); |
| 2479 |
goto ast_xml_doc_item_failure; |
| 2480 |
} |
| 2481 |
|
| 2482 |
if (ast_string_field_init(item, 64)) { |
| 2483 |
ast_log(AST_LOG_ERROR, "Failed to initialize string field for ast_xml_doc_item instance\n"); |
| 2484 |
goto ast_xml_doc_item_failure; |
| 2485 |
} |
| 2486 |
ast_string_field_set(item, name, name); |
| 2487 |
ast_string_field_set(item, type, type); |
| 2488 |
|
| 2489 |
return item; |
| 2490 |
|
| 2491 |
ast_xml_doc_item_failure: |
| 2492 |
ao2_ref(item, -1); |
| 2493 |
return NULL; |
| 2494 |
} |
| 2495 |
|
| 2496 |
|
| 2497 |
|
| 2498 |
|
| 2499 |
|
| 2500 |
|
| 2501 |
static int ast_xml_doc_item_hash(const void *obj, const int flags) |
| 2502 |
{ |
| 2503 |
const struct ast_xml_doc_item *item = obj; |
| 2504 |
const char *name = (flags & OBJ_KEY) ? obj : item->name; |
| 2505 |
return ast_str_case_hash(name); |
| 2506 |
} |
| 2507 |
|
| 2508 |
|
| 2509 |
|
| 2510 |
|
| 2511 |
|
| 2512 |
|
| 2513 |
static int ast_xml_doc_item_cmp(void *obj, void *arg, int flags) |
| 2514 |
{ |
| 2515 |
struct ast_xml_doc_item *left = obj; |
| 2516 |
struct ast_xml_doc_item *right = arg; |
| 2517 |
const char *match = (flags & OBJ_KEY) ? arg : right->name; |
| 2518 |
return strcasecmp(left->name, match) ? 0 : (CMP_MATCH | CMP_STOP); |
| 2519 |
} |
| 2520 |
|
| 2521 |
|
| 2522 |
|
| 2523 |
|
| 2524 |
|
| 2525 |
|
| 2526 |
|
| 2527 |
|
| 2528 |
|
| 2529 |
|
| 2530 |
|
| 2531 |
|
| 2532 |
|
| 2533 |
static struct ast_xml_doc_item *xmldoc_build_documentation_item(struct ast_xml_node *node, const char *name, const char *type) |
| 2534 |
{ |
| 2535 |
struct ast_xml_doc_item *item; |
| 2536 |
char *synopsis; |
| 2537 |
char *provided_by; |
| 2538 |
char *since; |
| 2539 |
char *description; |
| 2540 |
char *syntax; |
| 2541 |
char *arguments; |
| 2542 |
char *seealso; |
| 2543 |
|
| 2544 |
if (!(item = ast_xml_doc_item_alloc(name, type))) { |
| 2545 |
return NULL; |
| 2546 |
} |
| 2547 |
item->node = node; |
| 2548 |
|
| 2549 |
synopsis = _ast_xmldoc_build_synopsis(node); |
| 2550 |
provided_by = _ast_xmldoc_build_provided_by(node); |
| 2551 |
since = _ast_xmldoc_build_since(node); |
| 2552 |
description = _ast_xmldoc_build_description(node); |
| 2553 |
syntax = _ast_xmldoc_build_syntax(node, type, name); |
| 2554 |
arguments = _ast_xmldoc_build_arguments(node); |
| 2555 |
seealso = _ast_xmldoc_build_seealso(node); |
| 2556 |
|
| 2557 |
if (synopsis) { |
| 2558 |
ast_str_set(&item->synopsis, 0, "%s", synopsis); |
| 2559 |
} |
| 2560 |
if (provided_by) { |
| 2561 |
ast_str_set(&item->provided_by, 0, "%s", provided_by); |
| 2562 |
} |
| 2563 |
if (since) { |
| 2564 |
ast_str_set(&item->since, 0, "%s", since); |
| 2565 |
} |
| 2566 |
if (description) { |
| 2567 |
ast_str_set(&item->description, 0, "%s", description); |
| 2568 |
} |
| 2569 |
if (syntax) { |
| 2570 |
ast_str_set(&item->syntax, 0, "%s", syntax); |
| 2571 |
} |
| 2572 |
if (arguments) { |
| 2573 |
ast_str_set(&item->arguments, 0, "%s", arguments); |
| 2574 |
} |
| 2575 |
if (seealso) { |
| 2576 |
ast_str_set(&item->seealso, 0, "%s", seealso); |
| 2577 |
} |
| 2578 |
|
| 2579 |
ast_free(synopsis); |
| 2580 |
ast_free(provided_by); |
| 2581 |
ast_free(since); |
| 2582 |
ast_free(description); |
| 2583 |
ast_free(syntax); |
| 2584 |
ast_free(arguments); |
| 2585 |
ast_free(seealso); |
| 2586 |
|
| 2587 |
return item; |
| 2588 |
} |
| 2589 |
|
| 2590 |
|
| 2591 |
|
| 2592 |
|
| 2593 |
|
| 2594 |
|
| 2595 |
|
| 2596 |
|
| 2597 |
|
| 2598 |
|
| 2599 |
|
| 2600 |
|
| 2601 |
|
| 2602 |
|
| 2603 |
|
| 2604 |
static struct ast_xml_doc_item *xmldoc_build_list_responses(struct ast_xml_node *manager_action) |
| 2605 |
{ |
| 2606 |
struct ast_xml_node *event; |
| 2607 |
struct ast_xml_node *responses; |
| 2608 |
struct ast_xml_node *list_elements; |
| 2609 |
struct ast_xml_doc_item_list root; |
| 2610 |
|
| 2611 |
AST_LIST_HEAD_INIT(&root); |
| 2612 |
|
| 2613 |
responses = ast_xml_find_element(ast_xml_node_get_children(manager_action), "responses", NULL, NULL); |
| 2614 |
if (!responses) { |
| 2615 |
return NULL; |
| 2616 |
} |
| 2617 |
|
| 2618 |
list_elements = ast_xml_find_element(ast_xml_node_get_children(responses), "list-elements", NULL, NULL); |
| 2619 |
if (!list_elements) { |
| 2620 |
return NULL; |
| 2621 |
} |
| 2622 |
|
| 2623 |
|
| 2624 |
for (event = ast_xml_node_get_children(list_elements); event; event = ast_xml_node_get_next(event)) { |
| 2625 |
struct ast_xml_node *event_instance; |
| 2626 |
RAII_VAR(const char *, name, ast_xml_get_attribute(event, "name"), |
| 2627 |
ast_xml_free_attr); |
| 2628 |
struct ast_xml_doc_item *new_item; |
| 2629 |
|
| 2630 |
if (!name || strcmp(ast_xml_node_get_name(event), "managerEvent")) { |
| 2631 |
continue; |
| 2632 |
} |
| 2633 |
|
| 2634 |
event_instance = ast_xml_find_element(ast_xml_node_get_children(event), |
| 2635 |
"managerEventInstance", NULL, NULL); |
| 2636 |
new_item = xmldoc_build_documentation_item(event_instance, name, "managerEvent"); |
| 2637 |
if (!new_item) { |
| 2638 |
ao2_cleanup(AST_LIST_FIRST(&root)); |
| 2639 |
return NULL; |
| 2640 |
} |
| 2641 |
|
| 2642 |
AST_LIST_INSERT_TAIL(&root, new_item, next); |
| 2643 |
} |
| 2644 |
|
| 2645 |
return AST_LIST_FIRST(&root); |
| 2646 |
} |
| 2647 |
|
| 2648 |
struct ast_xml_doc_item *ast_xmldoc_build_list_responses(const char *type, const char *name, const char *module) |
| 2649 |
{ |
| 2650 |
struct ast_xml_node *node; |
| 2651 |
struct ast_xml_doc_item *responses; |
| 2652 |
|
| 2653 |
if (ast_strlen_zero(type) || ast_strlen_zero(name)) { |
| 2654 |
return NULL; |
| 2655 |
} |
| 2656 |
|
| 2657 |
AST_RWLIST_RDLOCK(&xmldoc_tree); |
| 2658 |
node = xmldoc_get_node(type, name, module, documentation_language); |
| 2659 |
|
| 2660 |
if (!node || !ast_xml_node_get_children(node)) { |
| 2661 |
AST_RWLIST_UNLOCK(&xmldoc_tree); |
| 2662 |
return NULL; |
| 2663 |
} |
| 2664 |
|
| 2665 |
responses = xmldoc_build_list_responses(node); |
| 2666 |
AST_RWLIST_UNLOCK(&xmldoc_tree); |
| 2667 |
return responses; |
| 2668 |
} |
| 2669 |
|
| 2670 |
|
| 2671 |
|
| 2672 |
|
| 2673 |
|
| 2674 |
|
| 2675 |
|
| 2676 |
|
| 2677 |
|
| 2678 |
|
| 2679 |
|
| 2680 |
|
| 2681 |
|
| 2682 |
|
| 2683 |
|
| 2684 |
static struct ast_xml_doc_item *xmldoc_build_final_response(struct ast_xml_node *manager_action) |
| 2685 |
{ |
| 2686 |
struct ast_xml_node *responses; |
| 2687 |
struct ast_xml_node *final_response_event; |
| 2688 |
struct ast_xml_node *event_instance; |
| 2689 |
|
| 2690 |
responses = ast_xml_find_element(ast_xml_node_get_children(manager_action), |
| 2691 |
"responses", NULL, NULL); |
| 2692 |
if (!responses) { |
| 2693 |
return NULL; |
| 2694 |
} |
| 2695 |
|
| 2696 |
final_response_event = ast_xml_find_element(ast_xml_node_get_children(responses), |
| 2697 |
"managerEvent", NULL, NULL); |
| 2698 |
if (!final_response_event) { |
| 2699 |
return NULL; |
| 2700 |
} |
| 2701 |
|
| 2702 |
event_instance = ast_xml_find_element(ast_xml_node_get_children(final_response_event), |
| 2703 |
"managerEventInstance", NULL, NULL); |
| 2704 |
if (!event_instance) { |
| 2705 |
return NULL; |
| 2706 |
} else { |
| 2707 |
const char *name; |
| 2708 |
struct ast_xml_doc_item *res; |
| 2709 |
|
| 2710 |
name = ast_xml_get_attribute(final_response_event, "name"); |
| 2711 |
res = xmldoc_build_documentation_item(event_instance, name, "managerEvent"); |
| 2712 |
ast_xml_free_attr(name); |
| 2713 |
return res; |
| 2714 |
} |
| 2715 |
|
| 2716 |
} |
| 2717 |
|
| 2718 |
struct ast_xml_doc_item *ast_xmldoc_build_final_response(const char *type, const char *name, const char *module) |
| 2719 |
{ |
| 2720 |
struct ast_xml_node *node; |
| 2721 |
static struct ast_xml_doc_item *response; |
| 2722 |
|
| 2723 |
if (ast_strlen_zero(type) || ast_strlen_zero(name)) { |
| 2724 |
return NULL; |
| 2725 |
} |
| 2726 |
|
| 2727 |
AST_RWLIST_RDLOCK(&xmldoc_tree); |
| 2728 |
node = xmldoc_get_node(type, name, module, documentation_language); |
| 2729 |
|
| 2730 |
if (!node || !ast_xml_node_get_children(node)) { |
| 2731 |
AST_RWLIST_UNLOCK(&xmldoc_tree); |
| 2732 |
return NULL; |
| 2733 |
} |
| 2734 |
|
| 2735 |
response = xmldoc_build_final_response(node); |
| 2736 |
AST_RWLIST_UNLOCK(&xmldoc_tree); |
| 2737 |
return response; |
| 2738 |
} |
| 2739 |
|
| 2740 |
struct ast_xml_xpath_results *__attribute__((format(printf, 1, 2))) ast_xmldoc_query(const char *fmt, ...) |
| 2741 |
{ |
| 2742 |
struct ast_xml_xpath_results *results = NULL; |
| 2743 |
struct documentation_tree *doctree; |
| 2744 |
RAII_VAR(struct ast_str *, xpath_str, ast_str_create(128), ast_free); |
| 2745 |
va_list ap; |
| 2746 |
int res; |
| 2747 |
|
| 2748 |
if (!xpath_str) { |
| 2749 |
return NULL; |
| 2750 |
} |
| 2751 |
|
| 2752 |
va_start(ap, fmt); |
| 2753 |
res = ast_str_set_va(&xpath_str, 0, fmt, ap); |
| 2754 |
va_end(ap); |
| 2755 |
if (res == AST_DYNSTR_BUILD_FAILED) { |
| 2756 |
return NULL; |
| 2757 |
} |
| 2758 |
|
| 2759 |
AST_RWLIST_RDLOCK(&xmldoc_tree); |
| 2760 |
AST_LIST_TRAVERSE(&xmldoc_tree, doctree, entry) { |
| 2761 |
if (!(results = ast_xml_query(doctree->doc, ast_str_buffer(xpath_str)))) { |
| 2762 |
continue; |
| 2763 |
} |
| 2764 |
break; |
| 2765 |
} |
| 2766 |
AST_RWLIST_UNLOCK(&xmldoc_tree); |
| 2767 |
|
| 2768 |
return results; |
| 2769 |
} |
| 2770 |
|
| 2771 |
static void build_config_docs(struct ast_xml_node *cur, struct ast_xml_doc_item_list *root) |
| 2772 |
{ |
| 2773 |
struct ast_xml_node *iter; |
| 2774 |
struct ast_xml_doc_item *item; |
| 2775 |
|
| 2776 |
for (iter = ast_xml_node_get_children(cur); iter; iter = ast_xml_node_get_next(iter)) { |
| 2777 |
const char *iter_name; |
| 2778 |
if (strncasecmp(ast_xml_node_get_name(iter), "config", 6)) { |
| 2779 |
continue; |
| 2780 |
} |
| 2781 |
iter_name = ast_xml_get_attribute(iter, "name"); |
| 2782 |
|
| 2783 |
if (!(item = xmldoc_build_documentation_item(iter, iter_name, ast_xml_node_get_name(iter)))) { |
| 2784 |
ast_log(LOG_ERROR, "Could not build documentation for '%s:%s'\n", ast_xml_node_get_name(iter), iter_name); |
| 2785 |
ast_xml_free_attr(iter_name); |
| 2786 |
break; |
| 2787 |
} |
| 2788 |
ast_xml_free_attr(iter_name); |
| 2789 |
if (!strcasecmp(ast_xml_node_get_name(iter), "configOption")) { |
| 2790 |
const char *name = ast_xml_get_attribute(cur, "name"); |
| 2791 |
ast_string_field_set(item, ref, name); |
| 2792 |
ast_xml_free_attr(name); |
| 2793 |
} |
| 2794 |
AST_LIST_INSERT_TAIL(root, item, next); |
| 2795 |
build_config_docs(iter, root); |
| 2796 |
} |
| 2797 |
} |
| 2798 |
|
| 2799 |
int ast_xmldoc_regenerate_doc_item(struct ast_xml_doc_item *item) |
| 2800 |
{ |
| 2801 |
const char *name; |
| 2802 |
char *syntax; |
| 2803 |
char *seealso; |
| 2804 |
char *arguments; |
| 2805 |
char *synopsis; |
| 2806 |
char *provided_by; |
| 2807 |
char *description; |
| 2808 |
|
| 2809 |
if (!item || !item->node) { |
| 2810 |
return -1; |
| 2811 |
} |
| 2812 |
|
| 2813 |
name = ast_xml_get_attribute(item->node, "name"); |
| 2814 |
if (!name) { |
| 2815 |
return -1; |
| 2816 |
} |
| 2817 |
|
| 2818 |
syntax = _ast_xmldoc_build_syntax(item->node, item->type, name); |
| 2819 |
seealso = _ast_xmldoc_build_seealso(item->node); |
| 2820 |
arguments = _ast_xmldoc_build_arguments(item->node); |
| 2821 |
synopsis = _ast_xmldoc_build_synopsis(item->node); |
| 2822 |
provided_by = _ast_xmldoc_build_provided_by(item->node); |
| 2823 |
description = _ast_xmldoc_build_description(item->node); |
| 2824 |
|
| 2825 |
if (syntax) { |
| 2826 |
ast_str_set(&item->syntax, 0, "%s", syntax); |
| 2827 |
} |
| 2828 |
if (seealso) { |
| 2829 |
ast_str_set(&item->seealso, 0, "%s", seealso); |
| 2830 |
} |
| 2831 |
if (arguments) { |
| 2832 |
ast_str_set(&item->arguments, 0, "%s", arguments); |
| 2833 |
} |
| 2834 |
if (synopsis) { |
| 2835 |
ast_str_set(&item->synopsis, 0, "%s", synopsis); |
| 2836 |
} |
| 2837 |
if (description) { |
| 2838 |
ast_str_set(&item->description, 0, "%s", description); |
| 2839 |
} |
| 2840 |
|
| 2841 |
ast_free(syntax); |
| 2842 |
ast_free(seealso); |
| 2843 |
ast_free(arguments); |
| 2844 |
ast_free(synopsis); |
| 2845 |
ast_free(provided_by); |
| 2846 |
ast_free(description); |
| 2847 |
ast_xml_free_attr(name); |
| 2848 |
return 0; |
| 2849 |
} |
| 2850 |
|
| 2851 |
struct ao2_container *ast_xmldoc_build_documentation(const char *type) |
| 2852 |
{ |
| 2853 |
struct ao2_container *docs; |
| 2854 |
struct ast_xml_node *node = NULL, *instance = NULL; |
| 2855 |
struct documentation_tree *doctree; |
| 2856 |
const char *name; |
| 2857 |
|
| 2858 |
docs = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0, 127, |
| 2859 |
ast_xml_doc_item_hash, NULL, ast_xml_doc_item_cmp); |
| 2860 |
if (!docs) { |
| 2861 |
ast_log(AST_LOG_ERROR, "Failed to create container for xml document item instances\n"); |
| 2862 |
return NULL; |
| 2863 |
} |
| 2864 |
|
| 2865 |
AST_RWLIST_RDLOCK(&xmldoc_tree); |
| 2866 |
AST_LIST_TRAVERSE(&xmldoc_tree, doctree, entry) { |
| 2867 |
|
| 2868 |
node = ast_xml_get_root(doctree->doc); |
| 2869 |
if (!node) { |
| 2870 |
break; |
| 2871 |
} |
| 2872 |
|
| 2873 |
for (node = ast_xml_node_get_children(node); node; node = ast_xml_node_get_next(node)) { |
| 2874 |
struct ast_xml_doc_item *item = NULL; |
| 2875 |
|
| 2876 |
|
| 2877 |
if (!ast_xml_node_get_children(node) || strcasecmp(ast_xml_node_get_name(node), type)) { |
| 2878 |
continue; |
| 2879 |
} |
| 2880 |
name = ast_xml_get_attribute(node, "name"); |
| 2881 |
if (!name) { |
| 2882 |
continue; |
| 2883 |
} |
| 2884 |
|
| 2885 |
switch (xmldoc_get_syntax_type(type)) { |
| 2886 |
case MANAGER_EVENT_SYNTAX: |
| 2887 |
{ |
| 2888 |
struct ast_xml_doc_item_list root; |
| 2889 |
|
| 2890 |
AST_LIST_HEAD_INIT(&root); |
| 2891 |
for (instance = ast_xml_node_get_children(node); instance; instance = ast_xml_node_get_next(instance)) { |
| 2892 |
struct ast_xml_doc_item *temp; |
| 2893 |
if (!ast_xml_node_get_children(instance) || strcasecmp(ast_xml_node_get_name(instance), "managerEventInstance")) { |
| 2894 |
continue; |
| 2895 |
} |
| 2896 |
temp = xmldoc_build_documentation_item(instance, name, type); |
| 2897 |
if (!temp) { |
| 2898 |
break; |
| 2899 |
} |
| 2900 |
AST_LIST_INSERT_TAIL(&root, temp, next); |
| 2901 |
} |
| 2902 |
item = AST_LIST_FIRST(&root); |
| 2903 |
break; |
| 2904 |
} |
| 2905 |
case CONFIG_INFO_SYNTAX: |
| 2906 |
{ |
| 2907 |
RAII_VAR(const char *, name, ast_xml_get_attribute(node, "name"), ast_xml_free_attr); |
| 2908 |
|
| 2909 |
if (!ast_xml_node_get_children(node) || strcasecmp(ast_xml_node_get_name(node), "configInfo")) { |
| 2910 |
break; |
| 2911 |
} |
| 2912 |
|
| 2913 |
item = xmldoc_build_documentation_item(node, name, "configInfo"); |
| 2914 |
if (item) { |
| 2915 |
struct ast_xml_doc_item_list root; |
| 2916 |
|
| 2917 |
AST_LIST_HEAD_INIT(&root); |
| 2918 |
AST_LIST_INSERT_TAIL(&root, item, next); |
| 2919 |
build_config_docs(node, &root); |
| 2920 |
} |
| 2921 |
break; |
| 2922 |
} |
| 2923 |
default: |
| 2924 |
item = xmldoc_build_documentation_item(node, name, type); |
| 2925 |
} |
| 2926 |
ast_xml_free_attr(name); |
| 2927 |
|
| 2928 |
if (item) { |
| 2929 |
ao2_link(docs, item); |
| 2930 |
ao2_t_ref(item, -1, "Dispose of creation ref"); |
| 2931 |
} |
| 2932 |
} |
| 2933 |
} |
| 2934 |
AST_RWLIST_UNLOCK(&xmldoc_tree); |
| 2935 |
|
| 2936 |
return docs; |
| 2937 |
} |
| 2938 |
|
| 2939 |
int ast_xmldoc_regenerate_doc_item(struct ast_xml_doc_item *item); |
| 2940 |
|
| 2941 |
|
| 2942 |
#if !defined(HAVE_GLOB_NOMAGIC) || !defined(HAVE_GLOB_BRACE) || defined(DEBUG_NONGNU) |
| 2943 |
static int xml_pathmatch(char *xmlpattern, int xmlpattern_maxlen, glob_t *globbuf) |
| 2944 |
{ |
| 2945 |
int globret; |
| 2946 |
|
| 2947 |
snprintf(xmlpattern, xmlpattern_maxlen, "%s/documentation/thirdparty/*-%s.xml", |
| 2948 |
ast_config_AST_DATA_DIR, documentation_language); |
| 2949 |
if((globret = glob(xmlpattern, GLOB_NOCHECK, NULL, globbuf))) { |
| 2950 |
return globret; |
| 2951 |
} |
| 2952 |
|
| 2953 |
snprintf(xmlpattern, xmlpattern_maxlen, "%s/documentation/thirdparty/*-%.2s_??.xml", |
| 2954 |
ast_config_AST_DATA_DIR, documentation_language); |
| 2955 |
if((globret = glob(xmlpattern, GLOB_APPEND | GLOB_NOCHECK, NULL, globbuf))) { |
| 2956 |
return globret; |
| 2957 |
} |
| 2958 |
|
| 2959 |
snprintf(xmlpattern, xmlpattern_maxlen, "%s/documentation/thirdparty/*-%s.xml", |
| 2960 |
ast_config_AST_DATA_DIR, default_documentation_language); |
| 2961 |
if((globret = glob(xmlpattern, GLOB_APPEND | GLOB_NOCHECK, NULL, globbuf))) { |
| 2962 |
return globret; |
| 2963 |
} |
| 2964 |
|
| 2965 |
snprintf(xmlpattern, xmlpattern_maxlen, "%s/documentation/*-%s.xml", |
| 2966 |
ast_config_AST_DATA_DIR, documentation_language); |
| 2967 |
if((globret = glob(xmlpattern, GLOB_APPEND | GLOB_NOCHECK, NULL, globbuf))) { |
| 2968 |
return globret; |
| 2969 |
} |
| 2970 |
|
| 2971 |
snprintf(xmlpattern, xmlpattern_maxlen, "%s/documentation/*-%.2s_??.xml", |
| 2972 |
ast_config_AST_DATA_DIR, documentation_language); |
| 2973 |
if((globret = glob(xmlpattern, GLOB_APPEND | GLOB_NOCHECK, NULL, globbuf))) { |
| 2974 |
return globret; |
| 2975 |
} |
| 2976 |
|
| 2977 |
snprintf(xmlpattern, xmlpattern_maxlen, "%s/documentation/*-%s.xml", |
| 2978 |
ast_config_AST_DATA_DIR, default_documentation_language); |
| 2979 |
globret = glob(xmlpattern, GLOB_APPEND | GLOB_NOCHECK, NULL, globbuf); |
| 2980 |
|
| 2981 |
return globret; |
| 2982 |
} |
| 2983 |
#endif |
| 2984 |
|
| 2985 |
static char *handle_dump_docs(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
| 2986 |
{ |
| 2987 |
struct documentation_tree *doctree; |
| 2988 |
struct ast_xml_doc *dumpdoc; |
| 2989 |
struct ast_xml_node *dumproot; |
| 2990 |
FILE *f; |
| 2991 |
|
| 2992 |
switch (cmd) { |
| 2993 |
case CLI_INIT: |
| 2994 |
e->command = "xmldoc dump"; |
| 2995 |
e->usage = |
| 2996 |
"Usage: xmldoc dump <filename>\n" |
| 2997 |
" Dump XML documentation to a file\n"; |
| 2998 |
return NULL; |
| 2999 |
case CLI_GENERATE: |
| 3000 |
return NULL; |
| 3001 |
} |
| 3002 |
|
| 3003 |
if (a->argc != 3) { |
| 3004 |
return CLI_SHOWUSAGE; |
| 3005 |
} |
| 3006 |
|
| 3007 |
dumpdoc = ast_xml_new(); |
| 3008 |
if (!dumpdoc) { |
| 3009 |
ast_log(LOG_ERROR, "Could not create new XML document\n"); |
| 3010 |
return CLI_FAILURE; |
| 3011 |
} |
| 3012 |
|
| 3013 |
dumproot = ast_xml_new_node("docs"); |
| 3014 |
if (!dumproot) { |
| 3015 |
ast_xml_close(dumpdoc); |
| 3016 |
ast_log(LOG_ERROR, "Could not create new XML root node\n"); |
| 3017 |
return CLI_FAILURE; |
| 3018 |
} |
| 3019 |
|
| 3020 |
ast_xml_set_root(dumpdoc, dumproot); |
| 3021 |
|
| 3022 |
AST_RWLIST_RDLOCK(&xmldoc_tree); |
| 3023 |
AST_LIST_TRAVERSE(&xmldoc_tree, doctree, entry) { |
| 3024 |
struct ast_xml_node *root_node = ast_xml_get_root(doctree->doc); |
| 3025 |
struct ast_xml_node *kids = ast_xml_node_get_children(root_node); |
| 3026 |
struct ast_xml_node *kids_copy; |
| 3027 |
|
| 3028 |
|
| 3029 |
if (!kids) { |
| 3030 |
continue; |
| 3031 |
} |
| 3032 |
|
| 3033 |
kids_copy = ast_xml_copy_node_list(kids); |
| 3034 |
if (!kids_copy) { |
| 3035 |
ast_xml_close(dumpdoc); |
| 3036 |
ast_log(LOG_ERROR, "Could not create copy of XML node list\n"); |
| 3037 |
return CLI_FAILURE; |
| 3038 |
} |
| 3039 |
|
| 3040 |
ast_xml_add_child_list(dumproot, kids_copy); |
| 3041 |
} |
| 3042 |
AST_RWLIST_UNLOCK(&xmldoc_tree); |
| 3043 |
|
| 3044 |
if (!(f = fopen(a->argv[2], "w"))) { |
| 3045 |
ast_xml_close(dumpdoc); |
| 3046 |
ast_log(LOG_ERROR, "Could not open file '%s': %s\n", a->argv[2], strerror(errno)); |
| 3047 |
return CLI_FAILURE; |
| 3048 |
} |
| 3049 |
|
| 3050 |
ast_xml_doc_dump_file(f, dumpdoc); |
| 3051 |
ast_xml_close(dumpdoc); |
| 3052 |
|
| 3053 |
fclose(f); |
| 3054 |
return CLI_SUCCESS; |
| 3055 |
} |
| 3056 |
|
| 3057 |
static struct ast_cli_entry cli_dump_xmldocs = AST_CLI_DEFINE(handle_dump_docs, "Dump the XML docs to the specified file"); |
| 3058 |
|
| 3059 |
static char *handle_reload_docs(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a); |
| 3060 |
static struct ast_cli_entry cli_reload_xmldocs = AST_CLI_DEFINE(handle_reload_docs, "Reload the XML docs"); |
| 3061 |
|
| 3062 |
|
| 3063 |
static void xmldoc_purge_documentation(void) |
| 3064 |
{ |
| 3065 |
struct documentation_tree *doctree; |
| 3066 |
|
| 3067 |
while ((doctree = AST_RWLIST_REMOVE_HEAD(&xmldoc_tree, entry))) { |
| 3068 |
ast_free(doctree->filename); |
| 3069 |
ast_xml_close(doctree->doc); |
| 3070 |
ast_free(doctree); |
| 3071 |
} |
| 3072 |
} |
| 3073 |
|
| 3074 |
|
| 3075 |
static void xmldoc_unload_documentation(void) |
| 3076 |
{ |
| 3077 |
ast_cli_unregister(&cli_reload_xmldocs); |
| 3078 |
ast_cli_unregister(&cli_dump_xmldocs); |
| 3079 |
|
| 3080 |
AST_RWLIST_WRLOCK(&xmldoc_tree); |
| 3081 |
xmldoc_purge_documentation(); |
| 3082 |
AST_RWLIST_UNLOCK(&xmldoc_tree); |
| 3083 |
|
| 3084 |
ast_xml_finish(); |
| 3085 |
} |
| 3086 |
|
| 3087 |
static int xmldoc_load_documentation(int first_time) |
| 3088 |
{ |
| 3089 |
struct ast_xml_node *root_node; |
| 3090 |
struct ast_xml_doc *tmpdoc; |
| 3091 |
struct documentation_tree *doc_tree; |
| 3092 |
char *xmlpattern; |
| 3093 |
struct ast_config *cfg = NULL; |
| 3094 |
struct ast_variable *var = NULL; |
| 3095 |
struct ast_flags cnfflags = { 0 }; |
| 3096 |
int globret, i, dup, duplicate; |
| 3097 |
glob_t globbuf; |
| 3098 |
#if !defined(HAVE_GLOB_NOMAGIC) || !defined(HAVE_GLOB_BRACE) || defined(DEBUG_NONGNU) |
| 3099 |
int xmlpattern_maxlen; |
| 3100 |
#endif |
| 3101 |
|
| 3102 |
|
| 3103 |
snprintf(documentation_language, sizeof(documentation_language), default_documentation_language); |
| 3104 |
|
| 3105 |
if ((cfg = ast_config_load2("asterisk.conf", "" , cnfflags)) && cfg != CONFIG_STATUS_FILEINVALID) { |
| 3106 |
for (var = ast_variable_browse(cfg, "options"); var; var = var->next) { |
| 3107 |
if (!strcasecmp(var->name, "documentation_language")) { |
| 3108 |
if (!ast_strlen_zero(var->value)) { |
| 3109 |
snprintf(documentation_language, sizeof(documentation_language), "%s", var->value); |
| 3110 |
} |
| 3111 |
} |
| 3112 |
} |
| 3113 |
ast_config_destroy(cfg); |
| 3114 |
} |
| 3115 |
|
| 3116 |
if (first_time) { |
| 3117 |
|
| 3118 |
ast_xml_init(); |
| 3119 |
ast_cli_register(&cli_dump_xmldocs); |
| 3120 |
ast_cli_register(&cli_reload_xmldocs); |
| 3121 |
|
| 3122 |
ast_register_cleanup(xmldoc_unload_documentation); |
| 3123 |
} |
| 3124 |
|
| 3125 |
globbuf.gl_offs = 0; |
| 3126 |
|
| 3127 |
#if !defined(HAVE_GLOB_NOMAGIC) || !defined(HAVE_GLOB_BRACE) || defined(DEBUG_NONGNU) |
| 3128 |
xmlpattern_maxlen = strlen(ast_config_AST_DATA_DIR) + strlen("/documentation/thirdparty") + strlen("/*-??_??.xml") + 1; |
| 3129 |
xmlpattern = ast_malloc(xmlpattern_maxlen); |
| 3130 |
globret = xml_pathmatch(xmlpattern, xmlpattern_maxlen, &globbuf); |
| 3131 |
#else |
| 3132 |
|
| 3133 |
if (ast_asprintf(&xmlpattern, "%s/documentation{/thirdparty/,/}*-{%s,%.2s_??,%s}.xml", ast_config_AST_DATA_DIR, |
| 3134 |
documentation_language, documentation_language, default_documentation_language) < 0) { |
| 3135 |
return 1; |
| 3136 |
} |
| 3137 |
globret = glob(xmlpattern, MY_GLOB_FLAGS, NULL, &globbuf); |
| 3138 |
#endif |
| 3139 |
|
| 3140 |
ast_debug(3, "gl_pathc %zu\n", (size_t)globbuf.gl_pathc); |
| 3141 |
if (globret == GLOB_NOSPACE) { |
| 3142 |
ast_log(LOG_WARNING, "XML load failure, glob expansion of pattern '%s' failed: Not enough memory\n", xmlpattern); |
| 3143 |
ast_free(xmlpattern); |
| 3144 |
return 1; |
| 3145 |
} else if (globret == GLOB_ABORTED) { |
| 3146 |
ast_log(LOG_WARNING, "XML load failure, glob expansion of pattern '%s' failed: Read error\n", xmlpattern); |
| 3147 |
ast_free(xmlpattern); |
| 3148 |
return 1; |
| 3149 |
} |
| 3150 |
ast_free(xmlpattern); |
| 3151 |
|
| 3152 |
AST_RWLIST_WRLOCK(&xmldoc_tree); |
| 3153 |
|
| 3154 |
if (!first_time) { |
| 3155 |
|
| 3156 |
|
| 3157 |
|
| 3158 |
|
| 3159 |
|
| 3160 |
xmldoc_purge_documentation(); |
| 3161 |
} |
| 3162 |
|
| 3163 |
|
| 3164 |
for (i = 0; i < globbuf.gl_pathc; i++) { |
| 3165 |
|
| 3166 |
duplicate = 0; |
| 3167 |
for (dup = 0; dup < i; dup++) { |
| 3168 |
if (!strcmp(globbuf.gl_pathv[i], globbuf.gl_pathv[dup])) { |
| 3169 |
duplicate = 1; |
| 3170 |
break; |
| 3171 |
} |
| 3172 |
} |
| 3173 |
if (duplicate || strchr(globbuf.gl_pathv[i], '*')) { |
| 3174 |
|
| 3175 |
|
| 3176 |
continue; |
| 3177 |
} |
| 3178 |
tmpdoc = NULL; |
| 3179 |
tmpdoc = ast_xml_open(globbuf.gl_pathv[i]); |
| 3180 |
if (!tmpdoc) { |
| 3181 |
ast_log(LOG_ERROR, "Could not open XML documentation at '%s'\n", globbuf.gl_pathv[i]); |
| 3182 |
continue; |
| 3183 |
} |
| 3184 |
|
| 3185 |
root_node = ast_xml_get_root(tmpdoc); |
| 3186 |
if (!root_node) { |
| 3187 |
ast_log(LOG_ERROR, "Error getting documentation root node\n"); |
| 3188 |
ast_xml_close(tmpdoc); |
| 3189 |
continue; |
| 3190 |
} |
| 3191 |
|
| 3192 |
if (strcmp(ast_xml_node_get_name(root_node), "docs")) { |
| 3193 |
ast_log(LOG_ERROR, "Documentation file is not well formed!\n"); |
| 3194 |
ast_xml_close(tmpdoc); |
| 3195 |
continue; |
| 3196 |
} |
| 3197 |
doc_tree = ast_calloc(1, sizeof(*doc_tree)); |
| 3198 |
if (!doc_tree) { |
| 3199 |
ast_log(LOG_ERROR, "Unable to allocate documentation_tree structure!\n"); |
| 3200 |
ast_xml_close(tmpdoc); |
| 3201 |
continue; |
| 3202 |
} |
| 3203 |
doc_tree->doc = tmpdoc; |
| 3204 |
doc_tree->filename = ast_strdup(globbuf.gl_pathv[i]); |
| 3205 |
AST_RWLIST_INSERT_TAIL(&xmldoc_tree, doc_tree, entry); |
| 3206 |
} |
| 3207 |
AST_RWLIST_UNLOCK(&xmldoc_tree); |
| 3208 |
|
| 3209 |
globfree(&globbuf); |
| 3210 |
|
| 3211 |
return 0; |
| 3212 |
} |
| 3213 |
|
| 3214 |
int ast_xmldoc_load_documentation(void) |
| 3215 |
{ |
| 3216 |
return xmldoc_load_documentation(1); |
| 3217 |
} |
| 3218 |
|
| 3219 |
static int xmldoc_reload_documentation(void) |
| 3220 |
{ |
| 3221 |
return xmldoc_load_documentation(0); |
| 3222 |
} |
| 3223 |
|
| 3224 |
static char *handle_reload_docs(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
| 3225 |
{ |
| 3226 |
switch (cmd) { |
| 3227 |
case CLI_INIT: |
| 3228 |
e->command = "xmldoc reload"; |
| 3229 |
e->usage = |
| 3230 |
"Usage: xmldoc reload\n" |
| 3231 |
" Reload XML documentation\n"; |
| 3232 |
return NULL; |
| 3233 |
case CLI_GENERATE: |
| 3234 |
return NULL; |
| 3235 |
} |
| 3236 |
|
| 3237 |
xmldoc_reload_documentation(); |
| 3238 |
return CLI_SUCCESS; |
| 3239 |
} |
| 3240 |
|
| 3241 |
#endif |