xmldoc.c 71 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2008, Eliel C. Sardanons (LU1ALY) <eliels@gmail.com>
  5. *
  6. * See http://www.asterisk.org for more information about
  7. * the Asterisk project. Please do not directly contact
  8. * any of the maintainers of this project for assistance;
  9. * the project provides a web site, mailing lists and IRC
  10. * channels for your use.
  11. *
  12. * This program is free software, distributed under the terms of
  13. * the GNU General Public License Version 2. See the LICENSE file
  14. * at the top of the source tree.
  15. */
  16. /*! \file
  17. *
  18. * \brief XML Documentation API
  19. *
  20. * \author Eliel C. Sardanons (LU1ALY) <eliels@gmail.com>
  21. *
  22. * \extref libxml2 http://www.xmlsoft.org/
  23. */
  24. /*** MODULEINFO
  25. <support_level>core</support_level>
  26. ***/
  27. #include "asterisk.h"
  28. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  29. #include "asterisk/_private.h"
  30. #include "asterisk/paths.h"
  31. #include "asterisk/linkedlists.h"
  32. #include "asterisk/config.h"
  33. #include "asterisk/term.h"
  34. #include "asterisk/astobj2.h"
  35. #include "asterisk/xmldoc.h"
  36. #ifdef AST_XML_DOCS
  37. /*! \brief Default documentation language. */
  38. static const char default_documentation_language[] = "en_US";
  39. /*! \brief Number of columns to print when showing the XML documentation with a
  40. * 'core show application/function *' CLI command. Used in text wrapping.*/
  41. static const int xmldoc_text_columns = 74;
  42. /*! \brief This is a value that we will use to let the wrapping mechanism move the cursor
  43. * backward and forward xmldoc_max_diff positions before cutting the middle of a
  44. * word, trying to find a space or a \n. */
  45. static const int xmldoc_max_diff = 5;
  46. /*! \brief XML documentation language. */
  47. static char documentation_language[6];
  48. /*! \brief XML documentation tree */
  49. struct documentation_tree {
  50. char *filename; /*!< XML document filename. */
  51. struct ast_xml_doc *doc; /*!< Open document pointer. */
  52. AST_RWLIST_ENTRY(documentation_tree) entry;
  53. };
  54. static char *xmldoc_get_syntax_cmd(struct ast_xml_node *fixnode, const char *name, int printname);
  55. static int xmldoc_parse_enumlist(struct ast_xml_node *fixnode, const char *tabs, struct ast_str **buffer);
  56. static int xmldoc_parse_info(struct ast_xml_node *node, const char *tabs, const char *posttabs, struct ast_str **buffer);
  57. static int xmldoc_parse_para(struct ast_xml_node *node, const char *tabs, const char *posttabs, struct ast_str **buffer);
  58. static int xmldoc_parse_specialtags(struct ast_xml_node *fixnode, const char *tabs, const char *posttabs, struct ast_str **buffer);
  59. /*!
  60. * \brief Container of documentation trees
  61. *
  62. * \note A RWLIST is a sufficient container type to use here for now.
  63. * However, some changes will need to be made to implement ref counting
  64. * if reload support is added in the future.
  65. */
  66. static AST_RWLIST_HEAD_STATIC(xmldoc_tree, documentation_tree);
  67. static const struct strcolorized_tags {
  68. const char *init; /*!< Replace initial tag with this string. */
  69. const char *end; /*!< Replace end tag with this string. */
  70. const int colorfg; /*!< Foreground color. */
  71. const char *inittag; /*!< Initial tag description. */
  72. const char *endtag; /*!< Ending tag description. */
  73. } colorized_tags[] = {
  74. { "<", ">", COLOR_GREEN, "<replaceable>", "</replaceable>" },
  75. { "\'", "\'", COLOR_BLUE, "<literal>", "</literal>" },
  76. { "*", "*", COLOR_RED, "<emphasis>", "</emphasis>" },
  77. { "\"", "\"", COLOR_YELLOW, "<filename>", "</filename>" },
  78. { "\"", "\"", COLOR_CYAN, "<directory>", "</directory>" },
  79. { "${", "}", COLOR_GREEN, "<variable>", "</variable>" },
  80. { "", "", COLOR_BLUE, "<value>", "</value>" },
  81. { "", "", COLOR_BLUE, "<enum>", "</enum>" },
  82. { "\'", "\'", COLOR_GRAY, "<astcli>", "</astcli>" },
  83. /* Special tags */
  84. { "", "", COLOR_YELLOW, "<note>", "</note>" },
  85. { "", "", COLOR_RED, "<warning>", "</warning>" }
  86. };
  87. static const struct strspecial_tags {
  88. const char *tagname; /*!< Special tag name. */
  89. const char *init; /*!< Print this at the beginning. */
  90. const char *end; /*!< Print this at the end. */
  91. } special_tags[] = {
  92. { "note", "<note>NOTE:</note> ", "" },
  93. { "warning", "<warning>WARNING!!!:</warning> ", "" }
  94. };
  95. /*! \internal
  96. * \brief Calculate the space in bytes used by a format string
  97. * that will be passed to a sprintf function.
  98. * \param postbr The format string to use to calculate the length.
  99. * \retval The postbr length.
  100. */
  101. static int xmldoc_postbrlen(const char *postbr)
  102. {
  103. int postbrreallen = 0, i;
  104. size_t postbrlen;
  105. if (!postbr) {
  106. return 0;
  107. }
  108. postbrlen = strlen(postbr);
  109. for (i = 0; i < postbrlen; i++) {
  110. if (postbr[i] == '\t') {
  111. postbrreallen += 8 - (postbrreallen % 8);
  112. } else {
  113. postbrreallen++;
  114. }
  115. }
  116. return postbrreallen;
  117. }
  118. /*! \internal
  119. * \brief Setup postbr to be used while wrapping the text.
  120. * Add to postbr array all the spaces and tabs at the beginning of text.
  121. * \param postbr output array.
  122. * \param len text array length.
  123. * \param text Text with format string before the actual string.
  124. */
  125. static void xmldoc_setpostbr(char *postbr, size_t len, const char *text)
  126. {
  127. int c, postbrlen = 0;
  128. if (!text) {
  129. return;
  130. }
  131. for (c = 0; c < len; c++) {
  132. if (text[c] == '\t' || text[c] == ' ') {
  133. postbr[postbrlen++] = text[c];
  134. } else {
  135. break;
  136. }
  137. }
  138. postbr[postbrlen] = '\0';
  139. }
  140. /*! \internal
  141. * \brief Try to find a space or a break in text starting at currentpost
  142. * and moving at most maxdiff positions.
  143. * Helper for xmldoc_string_wrap().
  144. * \param text Input string where it will search.
  145. * \param currentpos Current position within text.
  146. * \param maxdiff Not move more than maxdiff inside text.
  147. * \retval 1 if a space or break is found inside text while moving.
  148. * \retval 0 if no space or break is found.
  149. */
  150. static int xmldoc_wait_nextspace(const char *text, int currentpos, int maxdiff)
  151. {
  152. int i, textlen;
  153. if (!text) {
  154. return 0;
  155. }
  156. textlen = strlen(text);
  157. for (i = currentpos; i < textlen; i++) {
  158. if (text[i] == ESC) {
  159. /* Move to the end of the escape sequence */
  160. while (i < textlen && text[i] != 'm') {
  161. i++;
  162. }
  163. } else if (text[i] == ' ' || text[i] == '\n') {
  164. /* Found the next space or linefeed */
  165. return 1;
  166. } else if (i - currentpos > maxdiff) {
  167. /* We have looked the max distance and didn't find it */
  168. return 0;
  169. }
  170. }
  171. /* Reached the end and did not find it */
  172. return 0;
  173. }
  174. /*! \internal
  175. * \brief Helper function for xmldoc_string_wrap().
  176. * Try to found a space or a break inside text moving backward
  177. * not more than maxdiff positions.
  178. * \param text The input string where to search for a space.
  179. * \param currentpos The current cursor position.
  180. * \param maxdiff The max number of positions to move within text.
  181. * \retval 0 If no space is found (Notice that text[currentpos] is not a space or a break)
  182. * \retval > 0 If a space or a break is found, and the result is the position relative to
  183. * currentpos.
  184. */
  185. static int xmldoc_foundspace_backward(const char *text, int currentpos, int maxdiff)
  186. {
  187. int i;
  188. for (i = currentpos; i > 0; i--) {
  189. if (text[i] == ' ' || text[i] == '\n') {
  190. return (currentpos - i);
  191. } else if (text[i] == 'm' && (text[i - 1] >= '0' || text[i - 1] <= '9')) {
  192. /* give up, we found the end of a possible ESC sequence. */
  193. return 0;
  194. } else if (currentpos - i > maxdiff) {
  195. /* give up, we can't move anymore. */
  196. return 0;
  197. }
  198. }
  199. /* we found the beginning of the text */
  200. return 0;
  201. }
  202. /*! \internal
  203. * \brief Justify a text to a number of columns.
  204. * \param text Input text to be justified.
  205. * \param columns Number of columns to preserve in the text.
  206. * \param maxdiff Try to not cut a word when goinf down.
  207. * \retval NULL on error.
  208. * \retval The wrapped text.
  209. */
  210. static char *xmldoc_string_wrap(const char *text, int columns, int maxdiff)
  211. {
  212. struct ast_str *tmp;
  213. char *ret, postbr[160];
  214. int count = 1, i, backspace, needtobreak = 0, colmax, textlen;
  215. /* sanity check */
  216. if (!text || columns <= 0 || maxdiff < 0) {
  217. ast_log(LOG_WARNING, "Passing wrong arguments while trying to wrap the text\n");
  218. return NULL;
  219. }
  220. tmp = ast_str_create(strlen(text) * 3);
  221. if (!tmp) {
  222. return NULL;
  223. }
  224. /* Check for blanks and tabs and put them in postbr. */
  225. xmldoc_setpostbr(postbr, sizeof(postbr), text);
  226. colmax = columns - xmldoc_postbrlen(postbr);
  227. textlen = strlen(text);
  228. for (i = 0; i < textlen; i++) {
  229. if (needtobreak || !(count % colmax)) {
  230. if (text[i] == ' ') {
  231. ast_str_append(&tmp, 0, "\n%s", postbr);
  232. needtobreak = 0;
  233. count = 1;
  234. } else if (text[i] != '\n') {
  235. needtobreak = 1;
  236. if (xmldoc_wait_nextspace(text, i, maxdiff)) {
  237. /* wait for the next space */
  238. ast_str_append(&tmp, 0, "%c", text[i]);
  239. continue;
  240. }
  241. /* Try to look backwards */
  242. backspace = xmldoc_foundspace_backward(text, i, maxdiff);
  243. if (backspace) {
  244. needtobreak = 1;
  245. ast_str_truncate(tmp, -backspace);
  246. i -= backspace + 1;
  247. continue;
  248. }
  249. ast_str_append(&tmp, 0, "\n%s", postbr);
  250. needtobreak = 0;
  251. count = 1;
  252. }
  253. /* skip blanks after a \n */
  254. while (text[i] == ' ') {
  255. i++;
  256. }
  257. }
  258. if (text[i] == '\n') {
  259. xmldoc_setpostbr(postbr, sizeof(postbr), &text[i] + 1);
  260. colmax = columns - xmldoc_postbrlen(postbr);
  261. needtobreak = 0;
  262. count = 1;
  263. }
  264. if (text[i] == ESC) {
  265. /* Ignore Escape sequences. */
  266. do {
  267. ast_str_append(&tmp, 0, "%c", text[i]);
  268. i++;
  269. } while (i < textlen && text[i] != 'm');
  270. } else {
  271. count++;
  272. }
  273. ast_str_append(&tmp, 0, "%c", text[i]);
  274. }
  275. ret = ast_strdup(ast_str_buffer(tmp));
  276. ast_free(tmp);
  277. return ret;
  278. }
  279. char *ast_xmldoc_printable(const char *bwinput, int withcolors)
  280. {
  281. struct ast_str *colorized;
  282. char *wrapped = NULL;
  283. int i, c, len, colorsection;
  284. char *tmp;
  285. size_t bwinputlen;
  286. static const int base_fg = COLOR_CYAN;
  287. if (!bwinput) {
  288. return NULL;
  289. }
  290. bwinputlen = strlen(bwinput);
  291. if (!(colorized = ast_str_create(256))) {
  292. return NULL;
  293. }
  294. if (withcolors) {
  295. ast_term_color_code(&colorized, base_fg, 0);
  296. if (!colorized) {
  297. return NULL;
  298. }
  299. }
  300. for (i = 0; i < bwinputlen; i++) {
  301. colorsection = 0;
  302. /* Check if we are at the beginning of a tag to be colorized. */
  303. for (c = 0; c < ARRAY_LEN(colorized_tags); c++) {
  304. if (strncasecmp(bwinput + i, colorized_tags[c].inittag, strlen(colorized_tags[c].inittag))) {
  305. continue;
  306. }
  307. if (!(tmp = strcasestr(bwinput + i + strlen(colorized_tags[c].inittag), colorized_tags[c].endtag))) {
  308. continue;
  309. }
  310. len = tmp - (bwinput + i + strlen(colorized_tags[c].inittag));
  311. /* Setup color */
  312. if (withcolors) {
  313. if (ast_opt_light_background) {
  314. /* Turn off *bright* colors */
  315. ast_term_color_code(&colorized, colorized_tags[c].colorfg & 0x7f, 0);
  316. } else {
  317. /* Turn on *bright* colors */
  318. ast_term_color_code(&colorized, colorized_tags[c].colorfg | 0x80, 0);
  319. }
  320. if (!colorized) {
  321. return NULL;
  322. }
  323. }
  324. /* copy initial string replace */
  325. ast_str_append(&colorized, 0, "%s", colorized_tags[c].init);
  326. if (!colorized) {
  327. return NULL;
  328. }
  329. {
  330. char buf[len + 1];
  331. ast_copy_string(buf, bwinput + i + strlen(colorized_tags[c].inittag), sizeof(buf));
  332. ast_str_append(&colorized, 0, "%s", buf);
  333. }
  334. if (!colorized) {
  335. return NULL;
  336. }
  337. /* copy the ending string replace */
  338. ast_str_append(&colorized, 0, "%s", colorized_tags[c].end);
  339. if (!colorized) {
  340. return NULL;
  341. }
  342. /* Continue with the last color. */
  343. if (withcolors) {
  344. ast_term_color_code(&colorized, base_fg, 0);
  345. if (!colorized) {
  346. return NULL;
  347. }
  348. }
  349. i += len + strlen(colorized_tags[c].endtag) + strlen(colorized_tags[c].inittag) - 1;
  350. colorsection = 1;
  351. break;
  352. }
  353. if (!colorsection) {
  354. ast_str_append(&colorized, 0, "%c", bwinput[i]);
  355. if (!colorized) {
  356. return NULL;
  357. }
  358. }
  359. }
  360. if (withcolors) {
  361. ast_str_append(&colorized, 0, "%s", term_end());
  362. if (!colorized) {
  363. return NULL;
  364. }
  365. }
  366. /* Wrap the text, notice that string wrap will avoid cutting an ESC sequence. */
  367. wrapped = xmldoc_string_wrap(ast_str_buffer(colorized), xmldoc_text_columns, xmldoc_max_diff);
  368. ast_free(colorized);
  369. return wrapped;
  370. }
  371. /*! \internal
  372. * \brief Cleanup spaces and tabs after a \n
  373. * \param text String to be cleaned up.
  374. * \param output buffer (not already allocated).
  375. * \param lastspaces Remove last spaces in the string.
  376. */
  377. static void xmldoc_string_cleanup(const char *text, struct ast_str **output, int lastspaces)
  378. {
  379. int i;
  380. size_t textlen;
  381. if (!text) {
  382. *output = NULL;
  383. return;
  384. }
  385. textlen = strlen(text);
  386. *output = ast_str_create(textlen);
  387. if (!(*output)) {
  388. ast_log(LOG_ERROR, "Problem allocating output buffer\n");
  389. return;
  390. }
  391. for (i = 0; i < textlen; i++) {
  392. if (text[i] == '\n' || text[i] == '\r') {
  393. /* remove spaces/tabs/\n after a \n. */
  394. while (text[i + 1] == '\t' || text[i + 1] == '\r' || text[i + 1] == '\n') {
  395. i++;
  396. }
  397. ast_str_append(output, 0, " ");
  398. continue;
  399. } else {
  400. ast_str_append(output, 0, "%c", text[i]);
  401. }
  402. }
  403. /* remove last spaces (we don't want always to remove the trailing spaces). */
  404. if (lastspaces) {
  405. ast_str_trim_blanks(*output);
  406. }
  407. }
  408. /*! \internal
  409. * \brief Check if the given attribute on the given node matches the given value.
  410. * \param node the node to match
  411. * \param attr the name of the attribute
  412. * \param value the expected value of the attribute
  413. * \retval true if the given attribute contains the given value
  414. * \retval false if the given attribute does not exist or does not contain the given value
  415. */
  416. static int xmldoc_attribute_match(struct ast_xml_node *node, const char *attr, const char *value)
  417. {
  418. const char *attr_value = ast_xml_get_attribute(node, attr);
  419. int match = attr_value && !strcmp(attr_value, value);
  420. ast_xml_free_attr(attr_value);
  421. return match;
  422. }
  423. /*! \internal
  424. * \brief Get the application/function node for 'name' application/function with language 'language'
  425. * and module 'module' if we don't find any, get the first application
  426. * with 'name' no matter which language or module.
  427. * \param type 'application', 'function', ...
  428. * \param name Application or Function name.
  429. * \param module Module item is in.
  430. * \param language Try to get this language (if not found try with en_US)
  431. * \retval NULL on error.
  432. * \retval A node of type ast_xml_node.
  433. */
  434. static struct ast_xml_node *xmldoc_get_node(const char *type, const char *name, const char *module, const char *language)
  435. {
  436. struct ast_xml_node *node = NULL;
  437. struct ast_xml_node *first_match = NULL;
  438. struct ast_xml_node *lang_match = NULL;
  439. struct documentation_tree *doctree;
  440. AST_RWLIST_RDLOCK(&xmldoc_tree);
  441. AST_LIST_TRAVERSE(&xmldoc_tree, doctree, entry) {
  442. /* the core xml documents have priority over thirdparty document. */
  443. node = ast_xml_get_root(doctree->doc);
  444. if (!node) {
  445. break;
  446. }
  447. node = ast_xml_node_get_children(node);
  448. while ((node = ast_xml_find_element(node, type, "name", name))) {
  449. if (!ast_xml_node_get_children(node)) {
  450. /* ignore empty nodes */
  451. node = ast_xml_node_get_next(node);
  452. continue;
  453. }
  454. if (!first_match) {
  455. first_match = node;
  456. }
  457. /* Check language */
  458. if (xmldoc_attribute_match(node, "language", language)) {
  459. if (!lang_match) {
  460. lang_match = node;
  461. }
  462. /* if module is empty we have a match */
  463. if (ast_strlen_zero(module)) {
  464. break;
  465. }
  466. /* Check module */
  467. if (xmldoc_attribute_match(node, "module", module)) {
  468. break;
  469. }
  470. }
  471. node = ast_xml_node_get_next(node);
  472. }
  473. /* if we matched lang and module return this match */
  474. if (node) {
  475. break;
  476. }
  477. /* we didn't match lang and module, just return the first
  478. * result with a matching language if we have one */
  479. if (lang_match) {
  480. node = lang_match;
  481. break;
  482. }
  483. /* we didn't match with only the language, just return the
  484. * first match */
  485. if (first_match) {
  486. node = first_match;
  487. break;
  488. }
  489. }
  490. AST_RWLIST_UNLOCK(&xmldoc_tree);
  491. return node;
  492. }
  493. /*! \internal
  494. * \brief Helper function used to build the syntax, it allocates the needed buffer (or reallocates it),
  495. * and based on the reverse value it makes use of fmt to print the parameter list inside the
  496. * realloced buffer (syntax).
  497. * \param reverse We are going backwards while generating the syntax?
  498. * \param len Current length of 'syntax' buffer.
  499. * \param syntax Output buffer for the concatenated values.
  500. * \param fmt A format string that will be used in a sprintf call.
  501. */
  502. static void __attribute__((format(printf, 4, 5))) xmldoc_reverse_helper(int reverse, int *len, char **syntax, const char *fmt, ...)
  503. {
  504. int totlen;
  505. int tmpfmtlen;
  506. char *tmpfmt;
  507. char *new_syntax;
  508. char tmp;
  509. va_list ap;
  510. va_start(ap, fmt);
  511. if (ast_vasprintf(&tmpfmt, fmt, ap) < 0) {
  512. va_end(ap);
  513. return;
  514. }
  515. va_end(ap);
  516. tmpfmtlen = strlen(tmpfmt);
  517. totlen = *len + tmpfmtlen + 1;
  518. new_syntax = ast_realloc(*syntax, totlen);
  519. if (!new_syntax) {
  520. ast_free(tmpfmt);
  521. return;
  522. }
  523. *syntax = new_syntax;
  524. if (reverse) {
  525. memmove(*syntax + tmpfmtlen, *syntax, *len);
  526. /* Save this char, it will be overwritten by the \0 of strcpy. */
  527. tmp = (*syntax)[0];
  528. strcpy(*syntax, tmpfmt);
  529. /* Restore the already saved char. */
  530. (*syntax)[tmpfmtlen] = tmp;
  531. (*syntax)[totlen - 1] = '\0';
  532. } else {
  533. strcpy(*syntax + *len, tmpfmt);
  534. }
  535. *len = totlen - 1;
  536. ast_free(tmpfmt);
  537. }
  538. /*! \internal
  539. * \brief Check if the passed node has 'what' tags inside it.
  540. * \param node Root node to search 'what' elements.
  541. * \param what node name to search inside node.
  542. * \retval 1 If a 'what' element is found inside 'node'.
  543. * \retval 0 If no 'what' is found inside 'node'.
  544. */
  545. static int xmldoc_has_inside(struct ast_xml_node *fixnode, const char *what)
  546. {
  547. struct ast_xml_node *node = fixnode;
  548. for (node = ast_xml_node_get_children(fixnode); node; node = ast_xml_node_get_next(node)) {
  549. if (!strcasecmp(ast_xml_node_get_name(node), what)) {
  550. return 1;
  551. }
  552. }
  553. return 0;
  554. }
  555. /*! \internal
  556. * \brief Check if the passed node has at least one node inside it.
  557. * \param node Root node to search node elements.
  558. * \retval 1 If a node element is found inside 'node'.
  559. * \retval 0 If no node is found inside 'node'.
  560. */
  561. static int xmldoc_has_nodes(struct ast_xml_node *fixnode)
  562. {
  563. struct ast_xml_node *node = fixnode;
  564. for (node = ast_xml_node_get_children(fixnode); node; node = ast_xml_node_get_next(node)) {
  565. if (strcasecmp(ast_xml_node_get_name(node), "text")) {
  566. return 1;
  567. }
  568. }
  569. return 0;
  570. }
  571. /*! \internal
  572. * \brief Check if the passed node has at least one specialtag.
  573. * \param node Root node to search "specialtags" elements.
  574. * \retval 1 If a "specialtag" element is found inside 'node'.
  575. * \retval 0 If no "specialtag" is found inside 'node'.
  576. */
  577. static int xmldoc_has_specialtags(struct ast_xml_node *fixnode)
  578. {
  579. struct ast_xml_node *node = fixnode;
  580. int i;
  581. for (node = ast_xml_node_get_children(fixnode); node; node = ast_xml_node_get_next(node)) {
  582. for (i = 0; i < ARRAY_LEN(special_tags); i++) {
  583. if (!strcasecmp(ast_xml_node_get_name(node), special_tags[i].tagname)) {
  584. return 1;
  585. }
  586. }
  587. }
  588. return 0;
  589. }
  590. /*! \internal
  591. * \brief Build the syntax for a specified starting node.
  592. * \param rootnode A pointer to the ast_xml root node.
  593. * \param rootname Name of the application, function, option, etc. to build the syntax.
  594. * \param childname The name of each parameter node.
  595. * \param printparenthesis Boolean if we must print parenthesis if not parameters are found in the rootnode.
  596. * \param printrootname Boolean if we must print the rootname before the syntax and parenthesis at the begining/end.
  597. * \retval NULL on error.
  598. * \retval An ast_malloc'ed string with the syntax generated.
  599. */
  600. static char *xmldoc_get_syntax_fun(struct ast_xml_node *rootnode, const char *rootname, const char *childname, int printparenthesis, int printrootname)
  601. {
  602. #define GOTONEXT(__rev, __a) (__rev ? ast_xml_node_get_prev(__a) : ast_xml_node_get_next(__a))
  603. #define ISLAST(__rev, __a) (__rev == 1 ? (ast_xml_node_get_prev(__a) ? 0 : 1) : (ast_xml_node_get_next(__a) ? 0 : 1))
  604. #define MP(__a) ((multiple ? __a : ""))
  605. struct ast_xml_node *node = NULL, *firstparam = NULL, *lastparam = NULL;
  606. const char *paramtype, *multipletype, *paramnameattr, *attrargsep, *parenthesis, *argname;
  607. int reverse, required, paramcount = 0, openbrackets = 0, len = 0, hasparams=0;
  608. int reqfinode = 0, reqlanode = 0, optmidnode = 0, prnparenthesis, multiple;
  609. char *syntax = NULL, *argsep, *paramname;
  610. if (ast_strlen_zero(rootname) || ast_strlen_zero(childname)) {
  611. ast_log(LOG_WARNING, "Tried to look in XML tree with faulty rootname or childname while creating a syntax.\n");
  612. return NULL;
  613. }
  614. if (!rootnode || !ast_xml_node_get_children(rootnode)) {
  615. /* If the rootnode field is not found, at least print name. */
  616. if (ast_asprintf(&syntax, "%s%s", (printrootname ? rootname : ""), (printparenthesis ? "()" : "")) < 0) {
  617. syntax = NULL;
  618. }
  619. return syntax;
  620. }
  621. /* Get the argument separator from the root node attribute name 'argsep', if not found
  622. defaults to ','. */
  623. attrargsep = ast_xml_get_attribute(rootnode, "argsep");
  624. if (attrargsep) {
  625. argsep = ast_strdupa(attrargsep);
  626. ast_xml_free_attr(attrargsep);
  627. } else {
  628. argsep = ast_strdupa(",");
  629. }
  630. /* Get order of evaluation. */
  631. for (node = ast_xml_node_get_children(rootnode); node; node = ast_xml_node_get_next(node)) {
  632. if (strcasecmp(ast_xml_node_get_name(node), childname)) {
  633. continue;
  634. }
  635. required = 0;
  636. hasparams = 1;
  637. if ((paramtype = ast_xml_get_attribute(node, "required"))) {
  638. if (ast_true(paramtype)) {
  639. required = 1;
  640. }
  641. ast_xml_free_attr(paramtype);
  642. }
  643. lastparam = node;
  644. reqlanode = required;
  645. if (!firstparam) {
  646. /* first parameter node */
  647. firstparam = node;
  648. reqfinode = required;
  649. }
  650. }
  651. if (!hasparams) {
  652. /* This application, function, option, etc, doesn't have any params. */
  653. if (ast_asprintf(&syntax, "%s%s", (printrootname ? rootname : ""), (printparenthesis ? "()" : "")) < 0) {
  654. syntax = NULL;
  655. }
  656. return syntax;
  657. }
  658. if (reqfinode && reqlanode) {
  659. /* check midnode */
  660. for (node = ast_xml_node_get_children(rootnode); node; node = ast_xml_node_get_next(node)) {
  661. if (strcasecmp(ast_xml_node_get_name(node), childname)) {
  662. continue;
  663. }
  664. if (node != firstparam && node != lastparam) {
  665. if ((paramtype = ast_xml_get_attribute(node, "required"))) {
  666. if (!ast_true(paramtype)) {
  667. optmidnode = 1;
  668. ast_xml_free_attr(paramtype);
  669. break;
  670. }
  671. ast_xml_free_attr(paramtype);
  672. }
  673. }
  674. }
  675. }
  676. if ((!reqfinode && reqlanode) || (reqfinode && reqlanode && optmidnode)) {
  677. reverse = 1;
  678. node = lastparam;
  679. } else {
  680. reverse = 0;
  681. node = firstparam;
  682. }
  683. /* init syntax string. */
  684. if (reverse) {
  685. xmldoc_reverse_helper(reverse, &len, &syntax,
  686. (printrootname ? (printrootname == 2 ? ")]" : ")"): ""));
  687. } else {
  688. xmldoc_reverse_helper(reverse, &len, &syntax, "%s%s", (printrootname ? rootname : ""),
  689. (printrootname ? (printrootname == 2 ? "[(" : "(") : ""));
  690. }
  691. for (; node; node = GOTONEXT(reverse, node)) {
  692. if (strcasecmp(ast_xml_node_get_name(node), childname)) {
  693. continue;
  694. }
  695. /* Get the argument name, if it is not the leaf, go inside that parameter. */
  696. if (xmldoc_has_inside(node, "argument")) {
  697. parenthesis = ast_xml_get_attribute(node, "hasparams");
  698. prnparenthesis = 0;
  699. if (parenthesis) {
  700. prnparenthesis = ast_true(parenthesis);
  701. if (!strcasecmp(parenthesis, "optional")) {
  702. prnparenthesis = 2;
  703. }
  704. ast_xml_free_attr(parenthesis);
  705. }
  706. argname = ast_xml_get_attribute(node, "name");
  707. if (argname) {
  708. paramname = xmldoc_get_syntax_fun(node, argname, "argument", prnparenthesis, prnparenthesis);
  709. ast_xml_free_attr(argname);
  710. } else {
  711. /* Malformed XML, print **UNKOWN** */
  712. paramname = ast_strdup("**unknown**");
  713. }
  714. } else {
  715. paramnameattr = ast_xml_get_attribute(node, "name");
  716. if (!paramnameattr) {
  717. ast_log(LOG_WARNING, "Malformed XML %s: no %s name\n", rootname, childname);
  718. if (syntax) {
  719. /* Free already allocated syntax */
  720. ast_free(syntax);
  721. }
  722. /* to give up is ok? */
  723. if (ast_asprintf(&syntax, "%s%s", (printrootname ? rootname : ""), (printparenthesis ? "()" : "")) < 0) {
  724. syntax = NULL;
  725. }
  726. return syntax;
  727. }
  728. paramname = ast_strdup(paramnameattr);
  729. ast_xml_free_attr(paramnameattr);
  730. }
  731. if (!paramname) {
  732. return NULL;
  733. }
  734. /* Defaults to 'false'. */
  735. multiple = 0;
  736. if ((multipletype = ast_xml_get_attribute(node, "multiple"))) {
  737. if (ast_true(multipletype)) {
  738. multiple = 1;
  739. }
  740. ast_xml_free_attr(multipletype);
  741. }
  742. required = 0; /* Defaults to 'false'. */
  743. if ((paramtype = ast_xml_get_attribute(node, "required"))) {
  744. if (ast_true(paramtype)) {
  745. required = 1;
  746. }
  747. ast_xml_free_attr(paramtype);
  748. }
  749. /* build syntax core. */
  750. if (required) {
  751. /* First parameter */
  752. if (!paramcount) {
  753. xmldoc_reverse_helper(reverse, &len, &syntax, "%s%s%s%s", paramname, MP("["), MP(argsep), MP("...]"));
  754. } else {
  755. /* Time to close open brackets. */
  756. while (openbrackets > 0) {
  757. xmldoc_reverse_helper(reverse, &len, &syntax, (reverse ? "[" : "]"));
  758. openbrackets--;
  759. }
  760. if (reverse) {
  761. xmldoc_reverse_helper(reverse, &len, &syntax, "%s%s", paramname, argsep);
  762. } else {
  763. xmldoc_reverse_helper(reverse, &len, &syntax, "%s%s", argsep, paramname);
  764. }
  765. xmldoc_reverse_helper(reverse, &len, &syntax, "%s%s%s", MP("["), MP(argsep), MP("...]"));
  766. }
  767. } else {
  768. /* First parameter */
  769. if (!paramcount) {
  770. xmldoc_reverse_helper(reverse, &len, &syntax, "[%s%s%s%s]", paramname, MP("["), MP(argsep), MP("...]"));
  771. } else {
  772. if (ISLAST(reverse, node)) {
  773. /* This is the last parameter. */
  774. if (reverse) {
  775. xmldoc_reverse_helper(reverse, &len, &syntax, "[%s%s%s%s]%s", paramname,
  776. MP("["), MP(argsep), MP("...]"), argsep);
  777. } else {
  778. xmldoc_reverse_helper(reverse, &len, &syntax, "%s[%s%s%s%s]", argsep, paramname,
  779. MP("["), MP(argsep), MP("...]"));
  780. }
  781. } else {
  782. if (reverse) {
  783. xmldoc_reverse_helper(reverse, &len, &syntax, "%s%s%s%s%s]", paramname, argsep,
  784. MP("["), MP(argsep), MP("...]"));
  785. } else {
  786. xmldoc_reverse_helper(reverse, &len, &syntax, "[%s%s%s%s%s", argsep, paramname,
  787. MP("["), MP(argsep), MP("...]"));
  788. }
  789. openbrackets++;
  790. }
  791. }
  792. }
  793. ast_free(paramname);
  794. paramcount++;
  795. }
  796. /* Time to close open brackets. */
  797. while (openbrackets > 0) {
  798. xmldoc_reverse_helper(reverse, &len, &syntax, (reverse ? "[" : "]"));
  799. openbrackets--;
  800. }
  801. /* close syntax string. */
  802. if (reverse) {
  803. xmldoc_reverse_helper(reverse, &len, &syntax, "%s%s", (printrootname ? rootname : ""),
  804. (printrootname ? (printrootname == 2 ? "[(" : "(") : ""));
  805. } else {
  806. xmldoc_reverse_helper(reverse, &len, &syntax, (printrootname ? (printrootname == 2 ? ")]" : ")") : ""));
  807. }
  808. return syntax;
  809. #undef ISLAST
  810. #undef GOTONEXT
  811. #undef MP
  812. }
  813. /*! \internal
  814. * \brief Parse an enumlist inside a <parameter> to generate a COMMAND
  815. * syntax.
  816. * \param fixnode A pointer to the <enumlist> node.
  817. * \retval {<unknown>} on error.
  818. * \retval A string inside brackets {} with the enum's separated by pipes |.
  819. */
  820. static char *xmldoc_parse_cmd_enumlist(struct ast_xml_node *fixnode)
  821. {
  822. struct ast_xml_node *node = fixnode;
  823. struct ast_str *paramname;
  824. char *enumname, *ret;
  825. int first = 1;
  826. paramname = ast_str_create(128);
  827. if (!paramname) {
  828. return ast_strdup("{<unkown>}");
  829. }
  830. ast_str_append(&paramname, 0, "{");
  831. for (node = ast_xml_node_get_children(node); node; node = ast_xml_node_get_next(node)) {
  832. if (strcasecmp(ast_xml_node_get_name(node), "enum")) {
  833. continue;
  834. }
  835. enumname = xmldoc_get_syntax_cmd(node, "", 0);
  836. if (!enumname) {
  837. continue;
  838. }
  839. if (!first) {
  840. ast_str_append(&paramname, 0, "|");
  841. }
  842. ast_str_append(&paramname, 0, "%s", enumname);
  843. first = 0;
  844. ast_free(enumname);
  845. }
  846. ast_str_append(&paramname, 0, "}");
  847. ret = ast_strdup(ast_str_buffer(paramname));
  848. ast_free(paramname);
  849. return ret;
  850. }
  851. /*! \internal
  852. * \brief Generate a syntax of COMMAND type.
  853. * \param fixnode The <syntax> node pointer.
  854. * \param name The name of the 'command'.
  855. * \param printname Print the name of the command before the paramters?
  856. * \retval On error, return just 'name'.
  857. * \retval On success return the generated syntax.
  858. */
  859. static char *xmldoc_get_syntax_cmd(struct ast_xml_node *fixnode, const char *name, int printname)
  860. {
  861. struct ast_str *syntax;
  862. struct ast_xml_node *tmpnode, *node = fixnode;
  863. char *ret, *paramname;
  864. const char *paramtype, *attrname, *literal;
  865. int required, isenum, first = 1, isliteral;
  866. syntax = ast_str_create(128);
  867. if (!syntax) {
  868. /* at least try to return something... */
  869. return ast_strdup(name);
  870. }
  871. /* append name to output string. */
  872. if (printname) {
  873. ast_str_append(&syntax, 0, "%s", name);
  874. first = 0;
  875. }
  876. for (node = ast_xml_node_get_children(node); node; node = ast_xml_node_get_next(node)) {
  877. if (strcasecmp(ast_xml_node_get_name(node), "parameter")) {
  878. continue;
  879. }
  880. if (xmldoc_has_inside(node, "parameter")) {
  881. /* is this a recursive parameter. */
  882. paramname = xmldoc_get_syntax_cmd(node, "", 0);
  883. isenum = 1;
  884. } else {
  885. for (tmpnode = ast_xml_node_get_children(node); tmpnode; tmpnode = ast_xml_node_get_next(tmpnode)) {
  886. if (!strcasecmp(ast_xml_node_get_name(tmpnode), "enumlist")) {
  887. break;
  888. }
  889. }
  890. if (tmpnode) {
  891. /* parse enumlist (note that this is a special enumlist
  892. that is used to describe a syntax like {<param1>|<param2>|...} */
  893. paramname = xmldoc_parse_cmd_enumlist(tmpnode);
  894. isenum = 1;
  895. } else {
  896. /* this is a simple parameter. */
  897. attrname = ast_xml_get_attribute(node, "name");
  898. if (!attrname) {
  899. /* ignore this bogus parameter and continue. */
  900. continue;
  901. }
  902. paramname = ast_strdup(attrname);
  903. ast_xml_free_attr(attrname);
  904. isenum = 0;
  905. }
  906. }
  907. /* Is this parameter required? */
  908. required = 0;
  909. paramtype = ast_xml_get_attribute(node, "required");
  910. if (paramtype) {
  911. required = ast_true(paramtype);
  912. ast_xml_free_attr(paramtype);
  913. }
  914. /* Is this a replaceable value or a fixed parameter value? */
  915. isliteral = 0;
  916. literal = ast_xml_get_attribute(node, "literal");
  917. if (literal) {
  918. isliteral = ast_true(literal);
  919. ast_xml_free_attr(literal);
  920. }
  921. /* if required="false" print with [...].
  922. * if literal="true" or is enum print without <..>.
  923. * if not first print a space at the beginning.
  924. */
  925. ast_str_append(&syntax, 0, "%s%s%s%s%s%s",
  926. (first ? "" : " "),
  927. (required ? "" : "["),
  928. (isenum || isliteral ? "" : "<"),
  929. paramname,
  930. (isenum || isliteral ? "" : ">"),
  931. (required ? "" : "]"));
  932. first = 0;
  933. ast_free(paramname);
  934. }
  935. /* return a common string. */
  936. ret = ast_strdup(ast_str_buffer(syntax));
  937. ast_free(syntax);
  938. return ret;
  939. }
  940. /*! \internal
  941. * \brief Generate an AMI action/event syntax.
  942. * \param fixnode The manager action/event node pointer.
  943. * \param name The name of the manager action/event.
  944. * \param manager_type "Action" or "Event"
  945. * \retval The generated syntax.
  946. * \retval NULL on error.
  947. */
  948. static char *xmldoc_get_syntax_manager(struct ast_xml_node *fixnode, const char *name, const char *manager_type)
  949. {
  950. struct ast_str *syntax;
  951. struct ast_xml_node *node = fixnode;
  952. const char *paramtype, *attrname;
  953. int required;
  954. char *ret;
  955. syntax = ast_str_create(128);
  956. if (!syntax) {
  957. return ast_strdup(name);
  958. }
  959. ast_str_append(&syntax, 0, "%s: %s", manager_type, name);
  960. for (node = ast_xml_node_get_children(node); node; node = ast_xml_node_get_next(node)) {
  961. if (strcasecmp(ast_xml_node_get_name(node), "parameter")) {
  962. continue;
  963. }
  964. /* Is this parameter required? */
  965. required = !strcasecmp(manager_type, "event") ? 1 : 0;
  966. paramtype = ast_xml_get_attribute(node, "required");
  967. if (paramtype) {
  968. required = ast_true(paramtype);
  969. ast_xml_free_attr(paramtype);
  970. }
  971. attrname = ast_xml_get_attribute(node, "name");
  972. if (!attrname) {
  973. /* ignore this bogus parameter and continue. */
  974. continue;
  975. }
  976. ast_str_append(&syntax, 0, "\n%s%s:%s <value>",
  977. (required ? "" : "["),
  978. attrname,
  979. (required ? "" : "]"));
  980. ast_xml_free_attr(attrname);
  981. }
  982. /* return a common string. */
  983. ret = ast_strdup(ast_str_buffer(syntax));
  984. ast_free(syntax);
  985. return ret;
  986. }
  987. /*! \brief Types of syntax that we are able to generate. */
  988. enum syntaxtype {
  989. FUNCTION_SYNTAX,
  990. MANAGER_SYNTAX,
  991. MANAGER_EVENT_SYNTAX,
  992. COMMAND_SYNTAX
  993. };
  994. /*! \brief Mapping between type of node and type of syntax to generate. */
  995. static struct strsyntaxtype {
  996. const char *type;
  997. enum syntaxtype stxtype;
  998. } stxtype[] = {
  999. { "function", FUNCTION_SYNTAX },
  1000. { "application", FUNCTION_SYNTAX },
  1001. { "manager", MANAGER_SYNTAX },
  1002. { "managerEvent", MANAGER_EVENT_SYNTAX },
  1003. { "agi", COMMAND_SYNTAX }
  1004. };
  1005. /*! \internal
  1006. * \brief Get syntax type based on type of node.
  1007. * \param type Type of node.
  1008. * \retval The type of syntax to generate based on the type of node.
  1009. */
  1010. static enum syntaxtype xmldoc_get_syntax_type(const char *type)
  1011. {
  1012. int i;
  1013. for (i=0; i < ARRAY_LEN(stxtype); i++) {
  1014. if (!strcasecmp(stxtype[i].type, type)) {
  1015. return stxtype[i].stxtype;
  1016. }
  1017. }
  1018. return FUNCTION_SYNTAX;
  1019. }
  1020. /*!
  1021. * \internal
  1022. * \brief Build syntax information for an item
  1023. * \param node The syntax node to parse
  1024. * \param type The source type
  1025. * \param name The name of the item that the syntax describes
  1026. *
  1027. * \note This method exists for when you already have the node. This
  1028. * prevents having to lock the documentation tree twice
  1029. *
  1030. * \returns A malloc'd character pointer to the syntax of the item
  1031. * \returns NULL on failure
  1032. *
  1033. * \since 11
  1034. */
  1035. static char *_ast_xmldoc_build_syntax(struct ast_xml_node *node, const char *type, const char *name)
  1036. {
  1037. char *syntax = NULL;
  1038. for (node = ast_xml_node_get_children(node); node; node = ast_xml_node_get_next(node)) {
  1039. if (!strcasecmp(ast_xml_node_get_name(node), "syntax")) {
  1040. break;
  1041. }
  1042. }
  1043. if (!node) {
  1044. return syntax;
  1045. }
  1046. switch (xmldoc_get_syntax_type(type)) {
  1047. case FUNCTION_SYNTAX:
  1048. syntax = xmldoc_get_syntax_fun(node, name, "parameter", 1, 1);
  1049. break;
  1050. case COMMAND_SYNTAX:
  1051. syntax = xmldoc_get_syntax_cmd(node, name, 1);
  1052. break;
  1053. case MANAGER_SYNTAX:
  1054. syntax = xmldoc_get_syntax_manager(node, name, "Action");
  1055. break;
  1056. case MANAGER_EVENT_SYNTAX:
  1057. syntax = xmldoc_get_syntax_manager(node, name, "Event");
  1058. break;
  1059. default:
  1060. syntax = xmldoc_get_syntax_fun(node, name, "parameter", 1, 1);
  1061. }
  1062. return syntax;
  1063. }
  1064. char *ast_xmldoc_build_syntax(const char *type, const char *name, const char *module)
  1065. {
  1066. struct ast_xml_node *node;
  1067. node = xmldoc_get_node(type, name, module, documentation_language);
  1068. if (!node) {
  1069. return NULL;
  1070. }
  1071. return _ast_xmldoc_build_syntax(node, type, name);
  1072. }
  1073. /*! \internal
  1074. * \brief Parse common internal elements. This includes paragraphs, special
  1075. * tags, and information nodes.
  1076. * \param node The element to parse
  1077. * \param tabs Add this string before the content of the parsed element.
  1078. * \param posttabs Add this string after the content of the parsed element.
  1079. * \param buffer This must be an already allocated ast_str. It will be used to
  1080. * store the result (if something has already been placed in the
  1081. * buffer, the parsed elements will be appended)
  1082. * \retval 1 if any data was appended to the buffer
  1083. * \retval 2 if the data appended to the buffer contained a text paragraph
  1084. * \retval 0 if no data was appended to the buffer
  1085. */
  1086. static int xmldoc_parse_common_elements(struct ast_xml_node *node, const char *tabs, const char *posttabs, struct ast_str **buffer)
  1087. {
  1088. return (xmldoc_parse_para(node, tabs, posttabs, buffer)
  1089. || xmldoc_parse_specialtags(node, tabs, posttabs, buffer)
  1090. || xmldoc_parse_info(node, tabs, posttabs, buffer));
  1091. }
  1092. /*! \internal
  1093. * \brief Parse a <para> element.
  1094. * \param node The <para> element pointer.
  1095. * \param tabs Added this string before the content of the <para> element.
  1096. * \param posttabs Added this string after the content of the <para> element.
  1097. * \param buffer This must be an already allocated ast_str. It will be used
  1098. * to store the result (if already has something it will be appended to the current
  1099. * string).
  1100. * \retval 1 If 'node' is a named 'para'.
  1101. * \retval 2 If data is appended in buffer.
  1102. * \retval 0 on error.
  1103. */
  1104. static int xmldoc_parse_para(struct ast_xml_node *node, const char *tabs, const char *posttabs, struct ast_str **buffer)
  1105. {
  1106. const char *tmptext;
  1107. struct ast_xml_node *tmp;
  1108. int ret = 0;
  1109. struct ast_str *tmpstr;
  1110. if (!node || !ast_xml_node_get_children(node)) {
  1111. return ret;
  1112. }
  1113. if (strcasecmp(ast_xml_node_get_name(node), "para")) {
  1114. return ret;
  1115. }
  1116. ast_str_append(buffer, 0, "%s", tabs);
  1117. ret = 1;
  1118. for (tmp = ast_xml_node_get_children(node); tmp; tmp = ast_xml_node_get_next(tmp)) {
  1119. /* Get the text inside the <para> element and append it to buffer. */
  1120. tmptext = ast_xml_get_text(tmp);
  1121. if (tmptext) {
  1122. /* Strip \n etc. */
  1123. xmldoc_string_cleanup(tmptext, &tmpstr, 0);
  1124. ast_xml_free_text(tmptext);
  1125. if (tmpstr) {
  1126. if (strcasecmp(ast_xml_node_get_name(tmp), "text")) {
  1127. ast_str_append(buffer, 0, "<%s>%s</%s>", ast_xml_node_get_name(tmp),
  1128. ast_str_buffer(tmpstr), ast_xml_node_get_name(tmp));
  1129. } else {
  1130. ast_str_append(buffer, 0, "%s", ast_str_buffer(tmpstr));
  1131. }
  1132. ast_free(tmpstr);
  1133. ret = 2;
  1134. }
  1135. }
  1136. }
  1137. ast_str_append(buffer, 0, "%s", posttabs);
  1138. return ret;
  1139. }
  1140. /*! \internal
  1141. * \brief Parse special elements defined in 'struct special_tags' special elements must have a <para> element inside them.
  1142. * \param fixnode special tag node pointer.
  1143. * \param tabs put tabs before printing the node content.
  1144. * \param posttabs put posttabs after printing node content.
  1145. * \param buffer Output buffer, the special tags will be appended here.
  1146. * \retval 0 if no special element is parsed.
  1147. * \retval 1 if a special element is parsed (data is appended to buffer).
  1148. * \retval 2 if a special element is parsed and also a <para> element is parsed inside the specialtag.
  1149. */
  1150. static int xmldoc_parse_specialtags(struct ast_xml_node *fixnode, const char *tabs, const char *posttabs, struct ast_str **buffer)
  1151. {
  1152. struct ast_xml_node *node = fixnode;
  1153. int ret = 0, i, count = 0;
  1154. if (!node || !ast_xml_node_get_children(node)) {
  1155. return ret;
  1156. }
  1157. for (i = 0; i < ARRAY_LEN(special_tags); i++) {
  1158. if (strcasecmp(ast_xml_node_get_name(node), special_tags[i].tagname)) {
  1159. continue;
  1160. }
  1161. ret = 1;
  1162. /* This is a special tag. */
  1163. /* concat data */
  1164. if (!ast_strlen_zero(special_tags[i].init)) {
  1165. ast_str_append(buffer, 0, "%s%s", tabs, special_tags[i].init);
  1166. }
  1167. /* parse <para> elements inside special tags. */
  1168. for (node = ast_xml_node_get_children(node); node; node = ast_xml_node_get_next(node)) {
  1169. /* first <para> just print it without tabs at the begining. */
  1170. if ((xmldoc_parse_para(node, (!count ? "" : tabs), posttabs, buffer) == 2)
  1171. || (xmldoc_parse_info(node, (!count ? "": tabs), posttabs, buffer) == 2)) {
  1172. ret = 2;
  1173. }
  1174. }
  1175. if (!ast_strlen_zero(special_tags[i].end)) {
  1176. ast_str_append(buffer, 0, "%s%s", special_tags[i].end, posttabs);
  1177. }
  1178. break;
  1179. }
  1180. return ret;
  1181. }
  1182. /*! \internal
  1183. * \brief Parse an 'info' tag inside an element.
  1184. * \param node A pointer to the 'info' xml node.
  1185. * \param tabs A string to be appended at the beginning of each line being printed
  1186. * inside 'buffer'
  1187. * \param posttabs Add this string after the content of the <para> element, if one exists
  1188. * \param String buffer to put values found inide the info element.
  1189. * \ret 2 if the information contained a para element, and it returned a value of 2
  1190. * \ret 1 if information was put into the buffer
  1191. * \ret 0 if no information was put into the buffer or error
  1192. */
  1193. static int xmldoc_parse_info(struct ast_xml_node *node, const char *tabs, const char *posttabs, struct ast_str **buffer)
  1194. {
  1195. const char *tech;
  1196. char *internaltabs;
  1197. int internal_ret;
  1198. int ret = 0;
  1199. if (strcasecmp(ast_xml_node_get_name(node), "info")) {
  1200. return ret;
  1201. }
  1202. ast_asprintf(&internaltabs, "%s ", tabs);
  1203. if (!internaltabs) {
  1204. return ret;
  1205. }
  1206. tech = ast_xml_get_attribute(node, "tech");
  1207. if (tech) {
  1208. ast_str_append(buffer, 0, "%s<note>Technology: %s</note>\n", internaltabs, tech);
  1209. ast_xml_free_attr(tech);
  1210. }
  1211. ret = 1;
  1212. for (node = ast_xml_node_get_children(node); node; node = ast_xml_node_get_next(node)) {
  1213. if (!strcasecmp(ast_xml_node_get_name(node), "enumlist")) {
  1214. xmldoc_parse_enumlist(node, internaltabs, buffer);
  1215. } else if ((internal_ret = xmldoc_parse_common_elements(node, internaltabs, posttabs, buffer))) {
  1216. if (internal_ret > ret) {
  1217. ret = internal_ret;
  1218. }
  1219. }
  1220. }
  1221. ast_free(internaltabs);
  1222. return ret;
  1223. }
  1224. /*! \internal
  1225. * \brief Parse an <argument> element from the xml documentation.
  1226. * \param fixnode Pointer to the 'argument' xml node.
  1227. * \param insideparameter If we are parsing an <argument> inside a <parameter>.
  1228. * \param paramtabs pre tabs if we are inside a parameter element.
  1229. * \param tabs What to be printed before the argument name.
  1230. * \param buffer Output buffer to put values found inside the <argument> element.
  1231. * \retval 1 If there is content inside the argument.
  1232. * \retval 0 If the argument element is not parsed, or there is no content inside it.
  1233. */
  1234. static int xmldoc_parse_argument(struct ast_xml_node *fixnode, int insideparameter, const char *paramtabs, const char *tabs, struct ast_str **buffer)
  1235. {
  1236. struct ast_xml_node *node = fixnode;
  1237. const char *argname;
  1238. int count = 0, ret = 0;
  1239. if (!node || !ast_xml_node_get_children(node)) {
  1240. return ret;
  1241. }
  1242. /* Print the argument names */
  1243. argname = ast_xml_get_attribute(node, "name");
  1244. if (!argname) {
  1245. return 0;
  1246. }
  1247. if (xmldoc_has_inside(node, "para") || xmldoc_has_inside(node, "info") || xmldoc_has_specialtags(node)) {
  1248. ast_str_append(buffer, 0, "%s%s%s", tabs, argname, (insideparameter ? "\n" : ""));
  1249. ast_xml_free_attr(argname);
  1250. } else {
  1251. ast_xml_free_attr(argname);
  1252. return 0;
  1253. }
  1254. for (node = ast_xml_node_get_children(node); node; node = ast_xml_node_get_next(node)) {
  1255. if (xmldoc_parse_common_elements(node, (insideparameter ? paramtabs : (!count ? " - " : tabs)), "\n", buffer) == 2) {
  1256. count++;
  1257. ret = 1;
  1258. }
  1259. }
  1260. return ret;
  1261. }
  1262. /*! \internal
  1263. * \brief Parse a <variable> node inside a <variablelist> node.
  1264. * \param node The variable node to parse.
  1265. * \param tabs A string to be appended at the begining of the output that will be stored
  1266. * in buffer.
  1267. * \param buffer This must be an already created ast_str. It will be used
  1268. * to store the result (if already has something it will be appended to the current
  1269. * string).
  1270. * \retval 0 if no data is appended.
  1271. * \retval 1 if data is appended.
  1272. */
  1273. static int xmldoc_parse_variable(struct ast_xml_node *node, const char *tabs, struct ast_str **buffer)
  1274. {
  1275. struct ast_xml_node *tmp;
  1276. const char *valname;
  1277. const char *tmptext;
  1278. struct ast_str *cleanstr;
  1279. int ret = 0, printedpara=0;
  1280. for (tmp = ast_xml_node_get_children(node); tmp; tmp = ast_xml_node_get_next(tmp)) {
  1281. if (xmldoc_parse_common_elements(tmp, (ret ? tabs : ""), "\n", buffer)) {
  1282. printedpara = 1;
  1283. continue;
  1284. }
  1285. if (strcasecmp(ast_xml_node_get_name(tmp), "value")) {
  1286. continue;
  1287. }
  1288. /* Parse a <value> tag only. */
  1289. if (!printedpara) {
  1290. ast_str_append(buffer, 0, "\n");
  1291. printedpara = 1;
  1292. }
  1293. /* Parse each <value name='valuename'>desciption</value> */
  1294. valname = ast_xml_get_attribute(tmp, "name");
  1295. if (valname) {
  1296. ret = 1;
  1297. ast_str_append(buffer, 0, "%s<value>%s</value>", tabs, valname);
  1298. ast_xml_free_attr(valname);
  1299. }
  1300. tmptext = ast_xml_get_text(tmp);
  1301. /* Check inside this node for any explanation about its meaning. */
  1302. if (tmptext) {
  1303. /* Cleanup text. */
  1304. xmldoc_string_cleanup(tmptext, &cleanstr, 1);
  1305. ast_xml_free_text(tmptext);
  1306. if (cleanstr && ast_str_strlen(cleanstr) > 0) {
  1307. ast_str_append(buffer, 0, ":%s", ast_str_buffer(cleanstr));
  1308. }
  1309. ast_free(cleanstr);
  1310. }
  1311. ast_str_append(buffer, 0, "\n");
  1312. }
  1313. return ret;
  1314. }
  1315. /*! \internal
  1316. * \brief Parse a <variablelist> node and put all the output inside 'buffer'.
  1317. * \param node The variablelist node pointer.
  1318. * \param tabs A string to be appended at the begining of the output that will be stored
  1319. * in buffer.
  1320. * \param buffer This must be an already created ast_str. It will be used
  1321. * to store the result (if already has something it will be appended to the current
  1322. * string).
  1323. * \retval 1 If a <variablelist> element is parsed.
  1324. * \retval 0 On error.
  1325. */
  1326. static int xmldoc_parse_variablelist(struct ast_xml_node *node, const char *tabs, struct ast_str **buffer)
  1327. {
  1328. struct ast_xml_node *tmp;
  1329. const char *varname;
  1330. char *vartabs;
  1331. int ret = 0;
  1332. if (!node || !ast_xml_node_get_children(node)) {
  1333. return ret;
  1334. }
  1335. if (strcasecmp(ast_xml_node_get_name(node), "variablelist")) {
  1336. return ret;
  1337. }
  1338. /* use this spacing (add 4 spaces) inside a variablelist node. */
  1339. if (ast_asprintf(&vartabs, "%s ", tabs) < 0) {
  1340. return ret;
  1341. }
  1342. for (tmp = ast_xml_node_get_children(node); tmp; tmp = ast_xml_node_get_next(tmp)) {
  1343. /* We can have a <para> element inside the variable list */
  1344. if (xmldoc_parse_common_elements(tmp, (ret ? tabs : ""), "\n", buffer)) {
  1345. ret = 1;
  1346. continue;
  1347. }
  1348. if (!strcasecmp(ast_xml_node_get_name(tmp), "variable")) {
  1349. /* Store the variable name in buffer. */
  1350. varname = ast_xml_get_attribute(tmp, "name");
  1351. if (varname) {
  1352. ast_str_append(buffer, 0, "%s<variable>%s</variable>: ", tabs, varname);
  1353. ast_xml_free_attr(varname);
  1354. /* Parse the <variable> possible values. */
  1355. xmldoc_parse_variable(tmp, vartabs, buffer);
  1356. ret = 1;
  1357. }
  1358. }
  1359. }
  1360. ast_free(vartabs);
  1361. return ret;
  1362. }
  1363. /*!
  1364. * \internal
  1365. * \brief Build seealso information for an item
  1366. * \param node The seealso node to parse
  1367. *
  1368. * \note This method exists for when you already have the node. This
  1369. * prevents having to lock the documentation tree twice
  1370. *
  1371. * \returns A malloc'd character pointer to the seealso information of the item
  1372. * \returns NULL on failure
  1373. *
  1374. * \since 11
  1375. */
  1376. static char *_ast_xmldoc_build_seealso(struct ast_xml_node *node)
  1377. {
  1378. char *output;
  1379. struct ast_str *outputstr;
  1380. const char *typename;
  1381. const char *content;
  1382. int first = 1;
  1383. /* Find the <see-also> node. */
  1384. for (node = ast_xml_node_get_children(node); node; node = ast_xml_node_get_next(node)) {
  1385. if (!strcasecmp(ast_xml_node_get_name(node), "see-also")) {
  1386. break;
  1387. }
  1388. }
  1389. if (!node || !ast_xml_node_get_children(node)) {
  1390. /* we couldnt find a <see-also> node. */
  1391. return NULL;
  1392. }
  1393. /* prepare the output string. */
  1394. outputstr = ast_str_create(128);
  1395. if (!outputstr) {
  1396. return NULL;
  1397. }
  1398. /* get into the <see-also> node. */
  1399. for (node = ast_xml_node_get_children(node); node; node = ast_xml_node_get_next(node)) {
  1400. if (strcasecmp(ast_xml_node_get_name(node), "ref")) {
  1401. continue;
  1402. }
  1403. /* parse the <ref> node. 'type' attribute is required. */
  1404. typename = ast_xml_get_attribute(node, "type");
  1405. if (!typename) {
  1406. continue;
  1407. }
  1408. content = ast_xml_get_text(node);
  1409. if (!content) {
  1410. ast_xml_free_attr(typename);
  1411. continue;
  1412. }
  1413. if (!strcasecmp(typename, "application")) {
  1414. ast_str_append(&outputstr, 0, "%s%s()", (first ? "" : ", "), content);
  1415. } else if (!strcasecmp(typename, "function")) {
  1416. ast_str_append(&outputstr, 0, "%s%s", (first ? "" : ", "), content);
  1417. } else if (!strcasecmp(typename, "astcli")) {
  1418. ast_str_append(&outputstr, 0, "%s<astcli>%s</astcli>", (first ? "" : ", "), content);
  1419. } else {
  1420. ast_str_append(&outputstr, 0, "%s%s", (first ? "" : ", "), content);
  1421. }
  1422. first = 0;
  1423. ast_xml_free_text(content);
  1424. ast_xml_free_attr(typename);
  1425. }
  1426. output = ast_strdup(ast_str_buffer(outputstr));
  1427. ast_free(outputstr);
  1428. return output;
  1429. }
  1430. char *ast_xmldoc_build_seealso(const char *type, const char *name, const char *module)
  1431. {
  1432. char *output;
  1433. struct ast_xml_node *node;
  1434. if (ast_strlen_zero(type) || ast_strlen_zero(name)) {
  1435. return NULL;
  1436. }
  1437. /* get the application/function root node. */
  1438. node = xmldoc_get_node(type, name, module, documentation_language);
  1439. if (!node || !ast_xml_node_get_children(node)) {
  1440. return NULL;
  1441. }
  1442. output = _ast_xmldoc_build_seealso(node);
  1443. return output;
  1444. }
  1445. /*! \internal
  1446. * \brief Parse a <enum> node.
  1447. * \brief fixnode An ast_xml_node pointer to the <enum> node.
  1448. * \bried buffer The output buffer.
  1449. * \retval 0 if content is not found inside the enum element (data is not appended to buffer).
  1450. * \retval 1 if content is found and data is appended to buffer.
  1451. */
  1452. static int xmldoc_parse_enum(struct ast_xml_node *fixnode, const char *tabs, struct ast_str **buffer)
  1453. {
  1454. struct ast_xml_node *node = fixnode;
  1455. int ret = 0;
  1456. char *optiontabs;
  1457. if (ast_asprintf(&optiontabs, "%s ", tabs) < 0) {
  1458. return ret;
  1459. }
  1460. for (node = ast_xml_node_get_children(node); node; node = ast_xml_node_get_next(node)) {
  1461. if (xmldoc_parse_common_elements(node, (ret ? tabs : " - "), "\n", buffer)) {
  1462. ret = 1;
  1463. }
  1464. xmldoc_parse_enumlist(node, optiontabs, buffer);
  1465. }
  1466. ast_free(optiontabs);
  1467. return ret;
  1468. }
  1469. /*! \internal
  1470. * \brief Parse a <enumlist> node.
  1471. * \param fixnode As ast_xml pointer to the <enumlist> node.
  1472. * \param buffer The ast_str output buffer.
  1473. * \retval 0 if no <enumlist> node was parsed.
  1474. * \retval 1 if a <enumlist> node was parsed.
  1475. */
  1476. static int xmldoc_parse_enumlist(struct ast_xml_node *fixnode, const char *tabs, struct ast_str **buffer)
  1477. {
  1478. struct ast_xml_node *node = fixnode;
  1479. const char *enumname;
  1480. int ret = 0;
  1481. for (node = ast_xml_node_get_children(node); node; node = ast_xml_node_get_next(node)) {
  1482. if (strcasecmp(ast_xml_node_get_name(node), "enum")) {
  1483. continue;
  1484. }
  1485. enumname = ast_xml_get_attribute(node, "name");
  1486. if (enumname) {
  1487. ast_str_append(buffer, 0, "%s<enum>%s</enum>", tabs, enumname);
  1488. ast_xml_free_attr(enumname);
  1489. /* parse only enum elements inside a enumlist node. */
  1490. if ((xmldoc_parse_enum(node, tabs, buffer))) {
  1491. ret = 1;
  1492. } else {
  1493. ast_str_append(buffer, 0, "\n");
  1494. }
  1495. }
  1496. }
  1497. return ret;
  1498. }
  1499. /*! \internal
  1500. * \brief Parse an <option> node.
  1501. * \param fixnode An ast_xml pointer to the <option> node.
  1502. * \param tabs A string to be appended at the begining of each line being added to the
  1503. * buffer string.
  1504. * \param buffer The output buffer.
  1505. * \retval 0 if no option node is parsed.
  1506. * \retval 1 if an option node is parsed.
  1507. */
  1508. static int xmldoc_parse_option(struct ast_xml_node *fixnode, const char *tabs, struct ast_str **buffer)
  1509. {
  1510. struct ast_xml_node *node;
  1511. int ret = 0;
  1512. char *optiontabs;
  1513. if (ast_asprintf(&optiontabs, "%s ", tabs) < 0) {
  1514. return ret;
  1515. }
  1516. for (node = ast_xml_node_get_children(fixnode); node; node = ast_xml_node_get_next(node)) {
  1517. if (!strcasecmp(ast_xml_node_get_name(node), "argument")) {
  1518. /* if this is the first data appended to buffer, print a \n*/
  1519. if (!ret && ast_xml_node_get_children(node)) {
  1520. /* print \n */
  1521. ast_str_append(buffer, 0, "\n");
  1522. }
  1523. if (xmldoc_parse_argument(node, 0, NULL, optiontabs, buffer)) {
  1524. ret = 1;
  1525. }
  1526. continue;
  1527. }
  1528. if (xmldoc_parse_common_elements(node, (ret ? tabs : ""), "\n", buffer)) {
  1529. ret = 1;
  1530. }
  1531. xmldoc_parse_variablelist(node, optiontabs, buffer);
  1532. xmldoc_parse_enumlist(node, optiontabs, buffer);
  1533. }
  1534. ast_free(optiontabs);
  1535. return ret;
  1536. }
  1537. /*! \internal
  1538. * \brief Parse an <optionlist> element from the xml documentation.
  1539. * \param fixnode Pointer to the optionlist xml node.
  1540. * \param tabs A string to be appended at the begining of each line being added to the
  1541. * buffer string.
  1542. * \param buffer Output buffer to put what is inside the optionlist tag.
  1543. */
  1544. static void xmldoc_parse_optionlist(struct ast_xml_node *fixnode, const char *tabs, struct ast_str **buffer)
  1545. {
  1546. struct ast_xml_node *node;
  1547. const char *optname, *hasparams;
  1548. char *optionsyntax;
  1549. int optparams;
  1550. for (node = ast_xml_node_get_children(fixnode); node; node = ast_xml_node_get_next(node)) {
  1551. /* Start appending every option tag. */
  1552. if (strcasecmp(ast_xml_node_get_name(node), "option")) {
  1553. continue;
  1554. }
  1555. /* Get the option name. */
  1556. optname = ast_xml_get_attribute(node, "name");
  1557. if (!optname) {
  1558. continue;
  1559. }
  1560. optparams = 1;
  1561. hasparams = ast_xml_get_attribute(node, "hasparams");
  1562. if (hasparams && !strcasecmp(hasparams, "optional")) {
  1563. optparams = 2;
  1564. }
  1565. optionsyntax = xmldoc_get_syntax_fun(node, optname, "argument", 0, optparams);
  1566. if (!optionsyntax) {
  1567. ast_xml_free_attr(optname);
  1568. ast_xml_free_attr(hasparams);
  1569. continue;
  1570. }
  1571. ast_str_append(buffer, 0, "%s%s: ", tabs, optionsyntax);
  1572. if (!xmldoc_parse_option(node, tabs, buffer)) {
  1573. ast_str_append(buffer, 0, "\n");
  1574. }
  1575. ast_str_append(buffer, 0, "\n");
  1576. ast_xml_free_attr(optname);
  1577. ast_xml_free_attr(hasparams);
  1578. ast_free(optionsyntax);
  1579. }
  1580. }
  1581. /*! \internal
  1582. * \brief Parse a 'parameter' tag inside a syntax element.
  1583. * \param fixnode A pointer to the 'parameter' xml node.
  1584. * \param tabs A string to be appended at the beginning of each line being printed inside
  1585. * 'buffer'.
  1586. * \param buffer String buffer to put values found inside the parameter element.
  1587. */
  1588. static void xmldoc_parse_parameter(struct ast_xml_node *fixnode, const char *tabs, struct ast_str **buffer)
  1589. {
  1590. const char *paramname;
  1591. struct ast_xml_node *node = fixnode;
  1592. int hasarguments, printed = 0;
  1593. char *internaltabs;
  1594. if (strcasecmp(ast_xml_node_get_name(node), "parameter")) {
  1595. return;
  1596. }
  1597. hasarguments = xmldoc_has_inside(node, "argument");
  1598. if (!(paramname = ast_xml_get_attribute(node, "name"))) {
  1599. /* parameter MUST have an attribute name. */
  1600. return;
  1601. }
  1602. if (ast_asprintf(&internaltabs, "%s ", tabs) < 0) {
  1603. ast_xml_free_attr(paramname);
  1604. return;
  1605. }
  1606. if (!hasarguments && xmldoc_has_nodes(node)) {
  1607. ast_str_append(buffer, 0, "%s\n", paramname);
  1608. ast_xml_free_attr(paramname);
  1609. printed = 1;
  1610. }
  1611. for (node = ast_xml_node_get_children(node); node; node = ast_xml_node_get_next(node)) {
  1612. if (!strcasecmp(ast_xml_node_get_name(node), "optionlist")) {
  1613. xmldoc_parse_optionlist(node, internaltabs, buffer);
  1614. } else if (!strcasecmp(ast_xml_node_get_name(node), "enumlist")) {
  1615. xmldoc_parse_enumlist(node, internaltabs, buffer);
  1616. } else if (!strcasecmp(ast_xml_node_get_name(node), "argument")) {
  1617. xmldoc_parse_argument(node, 1, internaltabs, (!hasarguments ? " " : ""), buffer);
  1618. } else if (!strcasecmp(ast_xml_node_get_name(node), "para")) {
  1619. if (!printed) {
  1620. ast_str_append(buffer, 0, "%s\n", paramname);
  1621. ast_xml_free_attr(paramname);
  1622. printed = 1;
  1623. }
  1624. if (xmldoc_parse_para(node, internaltabs, "\n", buffer)) {
  1625. /* If anything ever goes in below this condition before the continue below,
  1626. * we should probably continue immediately. */
  1627. continue;
  1628. }
  1629. continue;
  1630. } else if (!strcasecmp(ast_xml_node_get_name(node), "info")) {
  1631. if (!printed) {
  1632. ast_str_append(buffer, 0, "%s\n", paramname);
  1633. ast_xml_free_attr(paramname);
  1634. printed = 1;
  1635. }
  1636. if (xmldoc_parse_info(node, internaltabs, "\n", buffer)) {
  1637. /* If anything ever goes in below this condition before the continue below,
  1638. * we should probably continue immediately. */
  1639. continue;
  1640. }
  1641. continue;
  1642. } else if ((xmldoc_parse_specialtags(node, internaltabs, "\n", buffer))) {
  1643. continue;
  1644. }
  1645. }
  1646. if (!printed) {
  1647. ast_xml_free_attr(paramname);
  1648. }
  1649. ast_free(internaltabs);
  1650. }
  1651. /*!
  1652. * \internal
  1653. * \brief Build the arguments for an item
  1654. * \param node The arguments node to parse
  1655. *
  1656. * \note This method exists for when you already have the node. This
  1657. * prevents having to lock the documentation tree twice
  1658. *
  1659. * \returns A malloc'd character pointer to the arguments for the item
  1660. * \returns NULL on failure
  1661. *
  1662. * \since 11
  1663. */
  1664. static char *_ast_xmldoc_build_arguments(struct ast_xml_node *node)
  1665. {
  1666. char *retstr = NULL;
  1667. struct ast_str *ret;
  1668. ret = ast_str_create(128);
  1669. if (!ret) {
  1670. return NULL;
  1671. }
  1672. /* Find the syntax field. */
  1673. for (node = ast_xml_node_get_children(node); node; node = ast_xml_node_get_next(node)) {
  1674. if (!strcasecmp(ast_xml_node_get_name(node), "syntax")) {
  1675. break;
  1676. }
  1677. }
  1678. if (!node || !ast_xml_node_get_children(node)) {
  1679. /* We couldn't find the syntax node. */
  1680. ast_free(ret);
  1681. return NULL;
  1682. }
  1683. for (node = ast_xml_node_get_children(node); node; node = ast_xml_node_get_next(node)) {
  1684. xmldoc_parse_parameter(node, "", &ret);
  1685. }
  1686. if (ast_str_strlen(ret) > 0) {
  1687. /* remove last '\n' */
  1688. char *buf = ast_str_buffer(ret);
  1689. if (buf[ast_str_strlen(ret) - 1] == '\n') {
  1690. ast_str_truncate(ret, -1);
  1691. }
  1692. retstr = ast_strdup(ast_str_buffer(ret));
  1693. }
  1694. ast_free(ret);
  1695. return retstr;
  1696. }
  1697. char *ast_xmldoc_build_arguments(const char *type, const char *name, const char *module)
  1698. {
  1699. struct ast_xml_node *node;
  1700. if (ast_strlen_zero(type) || ast_strlen_zero(name)) {
  1701. return NULL;
  1702. }
  1703. node = xmldoc_get_node(type, name, module, documentation_language);
  1704. if (!node || !ast_xml_node_get_children(node)) {
  1705. return NULL;
  1706. }
  1707. return _ast_xmldoc_build_arguments(node);
  1708. }
  1709. /*! \internal
  1710. * \brief Return the string within a node formatted with <para> and <variablelist> elements.
  1711. * \param node Parent node where content resides.
  1712. * \param raw If set, return the node's content without further processing.
  1713. * \param raw_wrap Wrap raw text.
  1714. * \retval NULL on error
  1715. * \retval Node content on success.
  1716. */
  1717. static struct ast_str *xmldoc_get_formatted(struct ast_xml_node *node, int raw_output, int raw_wrap)
  1718. {
  1719. struct ast_xml_node *tmp;
  1720. const char *notcleanret, *tmpstr;
  1721. struct ast_str *ret;
  1722. if (raw_output) {
  1723. /* xmldoc_string_cleanup will allocate the ret object */
  1724. notcleanret = ast_xml_get_text(node);
  1725. tmpstr = notcleanret;
  1726. xmldoc_string_cleanup(ast_skip_blanks(notcleanret), &ret, 0);
  1727. ast_xml_free_text(tmpstr);
  1728. } else {
  1729. ret = ast_str_create(128);
  1730. if (!ret) {
  1731. return NULL;
  1732. }
  1733. for (tmp = ast_xml_node_get_children(node); tmp; tmp = ast_xml_node_get_next(tmp)) {
  1734. /* if found, parse a <para> element. */
  1735. if (xmldoc_parse_common_elements(tmp, "", "\n", &ret)) {
  1736. continue;
  1737. }
  1738. /* if found, parse a <variablelist> element. */
  1739. xmldoc_parse_variablelist(tmp, "", &ret);
  1740. xmldoc_parse_enumlist(tmp, " ", &ret);
  1741. }
  1742. /* remove last '\n' */
  1743. /* XXX Don't modify ast_str internals manually */
  1744. tmpstr = ast_str_buffer(ret);
  1745. if (tmpstr[ast_str_strlen(ret) - 1] == '\n') {
  1746. ast_str_truncate(ret, -1);
  1747. }
  1748. }
  1749. return ret;
  1750. }
  1751. /*!
  1752. * \brief Get the content of a field (synopsis, description, etc) from an asterisk document tree node
  1753. * \param node The node to obtain the information from
  1754. * \param var Name of field to return (synopsis, description, etc).
  1755. * \param raw Field only contains text, no other elements inside it.
  1756. * \retval NULL On error.
  1757. * \retval Field text content on success.
  1758. * \since 11
  1759. */
  1760. static char *_xmldoc_build_field(struct ast_xml_node *node, const char *var, int raw)
  1761. {
  1762. char *ret = NULL;
  1763. struct ast_str *formatted;
  1764. node = ast_xml_find_element(ast_xml_node_get_children(node), var, NULL, NULL);
  1765. if (!node || !ast_xml_node_get_children(node)) {
  1766. return ret;
  1767. }
  1768. formatted = xmldoc_get_formatted(node, raw, raw);
  1769. if (formatted && ast_str_strlen(formatted) > 0) {
  1770. ret = ast_strdup(ast_str_buffer(formatted));
  1771. }
  1772. ast_free(formatted);
  1773. return ret;
  1774. }
  1775. /*!
  1776. * \brief Get the content of a field (synopsis, description, etc) from an asterisk document tree
  1777. * \param type Type of element (application, function, ...).
  1778. * \param name Name of element (Dial, Echo, Playback, ...).
  1779. * \param var Name of field to return (synopsis, description, etc).
  1780. * \param raw Field only contains text, no other elements inside it.
  1781. * \retval NULL On error.
  1782. * \retval Field text content on success.
  1783. */
  1784. static char *xmldoc_build_field(const char *type, const char *name, const char *module, const char *var, int raw)
  1785. {
  1786. struct ast_xml_node *node;
  1787. if (ast_strlen_zero(type) || ast_strlen_zero(name)) {
  1788. ast_log(LOG_ERROR, "Tried to look in XML tree with faulty values.\n");
  1789. return NULL;
  1790. }
  1791. node = xmldoc_get_node(type, name, module, documentation_language);
  1792. if (!node) {
  1793. ast_log(LOG_WARNING, "Couldn't find %s %s in XML documentation\n", type, name);
  1794. return NULL;
  1795. }
  1796. return _xmldoc_build_field(node, var, raw);
  1797. }
  1798. /* \internal
  1799. * \brief Build the synopsis for an item
  1800. * \param node The synopsis node
  1801. *
  1802. * \note This method exists for when you already have the node. This
  1803. * prevents having to lock the documentation tree twice
  1804. *
  1805. * \returns A malloc'd character pointer to the synopsis information
  1806. * \returns NULL on failure
  1807. * \since 11
  1808. */
  1809. static char *_ast_xmldoc_build_synopsis(struct ast_xml_node *node)
  1810. {
  1811. return _xmldoc_build_field(node, "synopsis", 1);
  1812. }
  1813. char *ast_xmldoc_build_synopsis(const char *type, const char *name, const char *module)
  1814. {
  1815. return xmldoc_build_field(type, name, module, "synopsis", 1);
  1816. }
  1817. /*!
  1818. * \internal
  1819. * \brief Build the descripton for an item
  1820. * \param node The description node to parse
  1821. *
  1822. * \note This method exists for when you already have the node. This
  1823. * prevents having to lock the documentation tree twice
  1824. *
  1825. * \returns A malloc'd character pointer to the arguments for the item
  1826. * \returns NULL on failure
  1827. * \since 11
  1828. */
  1829. static char *_ast_xmldoc_build_description(struct ast_xml_node *node)
  1830. {
  1831. return _xmldoc_build_field(node, "description", 0);
  1832. }
  1833. char *ast_xmldoc_build_description(const char *type, const char *name, const char *module)
  1834. {
  1835. return xmldoc_build_field(type, name, module, "description", 0);
  1836. }
  1837. /*! \internal \brief ast_xml_doc_item ao2 destructor
  1838. * \since 11
  1839. */
  1840. static void ast_xml_doc_item_destructor(void *obj)
  1841. {
  1842. struct ast_xml_doc_item *doc = obj;
  1843. if (!doc) {
  1844. return;
  1845. }
  1846. ast_free(doc->syntax);
  1847. ast_free(doc->seealso);
  1848. ast_free(doc->arguments);
  1849. ast_free(doc->synopsis);
  1850. ast_free(doc->description);
  1851. ast_string_field_free_memory(doc);
  1852. if (doc->next) {
  1853. ao2_ref(doc->next, -1);
  1854. doc->next = NULL;
  1855. }
  1856. }
  1857. /*! \internal
  1858. * \brief Create an ao2 ref counted ast_xml_doc_item
  1859. * \param name The name of the item
  1860. * \param type The item's source type
  1861. * \since 11
  1862. */
  1863. static struct ast_xml_doc_item *ast_xml_doc_item_alloc(const char *name, const char *type)
  1864. {
  1865. struct ast_xml_doc_item *item;
  1866. if (!(item = ao2_alloc(sizeof(*item), ast_xml_doc_item_destructor))) {
  1867. ast_log(AST_LOG_ERROR, "Failed to allocate memory for ast_xml_doc_item instance\n");
  1868. return NULL;
  1869. }
  1870. if ( !(item->syntax = ast_str_create(128))
  1871. || !(item->seealso = ast_str_create(128))
  1872. || !(item->arguments = ast_str_create(128))
  1873. || !(item->synopsis = ast_str_create(128))
  1874. || !(item->description = ast_str_create(128))) {
  1875. ast_log(AST_LOG_ERROR, "Failed to allocate strings for ast_xml_doc_item instance\n");
  1876. goto ast_xml_doc_item_failure;
  1877. }
  1878. if (ast_string_field_init(item, 64)) {
  1879. ast_log(AST_LOG_ERROR, "Failed to initialize string field for ast_xml_doc_item instance\n");
  1880. goto ast_xml_doc_item_failure;
  1881. }
  1882. ast_string_field_set(item, name, name);
  1883. ast_string_field_set(item, type, type);
  1884. return item;
  1885. ast_xml_doc_item_failure:
  1886. ao2_ref(item, -1);
  1887. return NULL;
  1888. }
  1889. /*! \internal
  1890. * \brief ao2 item hash function for ast_xml_doc_item
  1891. * \since 11
  1892. */
  1893. static int ast_xml_doc_item_hash(const void *obj, const int flags)
  1894. {
  1895. const struct ast_xml_doc_item *item = obj;
  1896. const char *name = (flags & OBJ_KEY) ? obj : item->name;
  1897. return ast_str_case_hash(name);
  1898. }
  1899. /*! \internal
  1900. * \brief ao2 item comparison function for ast_xml_doc_item
  1901. * \since 11
  1902. */
  1903. static int ast_xml_doc_item_cmp(void *obj, void *arg, int flags)
  1904. {
  1905. struct ast_xml_doc_item *left = obj;
  1906. struct ast_xml_doc_item *right = arg;
  1907. const char *match = (flags & OBJ_KEY) ? arg : right->name;
  1908. return strcasecmp(left->name, match) ? 0 : (CMP_MATCH | CMP_STOP);
  1909. }
  1910. /* \internal
  1911. * \brief Build an XML documentation item
  1912. * \param node The root node for the item
  1913. * \param name The name of the item
  1914. * \param type The item's source type
  1915. *
  1916. * \returns NULL on failure
  1917. * \returns An ao2 ref counted object
  1918. * \since 11
  1919. */
  1920. static struct ast_xml_doc_item *xmldoc_build_documentation_item(struct ast_xml_node *node, const char *name, const char *type)
  1921. {
  1922. struct ast_xml_doc_item *item;
  1923. char *syntax;
  1924. char *seealso;
  1925. char *arguments;
  1926. char *synopsis;
  1927. char *description;
  1928. if (!(item = ast_xml_doc_item_alloc(name, type))) {
  1929. return NULL;
  1930. }
  1931. syntax = _ast_xmldoc_build_syntax(node, type, name);
  1932. seealso = _ast_xmldoc_build_seealso(node);
  1933. arguments = _ast_xmldoc_build_arguments(node);
  1934. synopsis = _ast_xmldoc_build_synopsis(node);
  1935. description = _ast_xmldoc_build_description(node);
  1936. if (syntax) {
  1937. ast_str_set(&item->syntax, 0, "%s", syntax);
  1938. }
  1939. if (seealso) {
  1940. ast_str_set(&item->seealso, 0, "%s", seealso);
  1941. }
  1942. if (arguments) {
  1943. ast_str_set(&item->arguments, 0, "%s", arguments);
  1944. }
  1945. if (synopsis) {
  1946. ast_str_set(&item->synopsis, 0, "%s", synopsis);
  1947. }
  1948. if (description) {
  1949. ast_str_set(&item->description, 0, "%s", description);
  1950. }
  1951. ast_free(syntax);
  1952. ast_free(seealso);
  1953. ast_free(arguments);
  1954. ast_free(synopsis);
  1955. ast_free(description);
  1956. return item;
  1957. }
  1958. struct ao2_container *ast_xmldoc_build_documentation(const char *type)
  1959. {
  1960. struct ao2_container *docs;
  1961. struct ast_xml_doc_item *item = NULL, *root = NULL;
  1962. struct ast_xml_node *node = NULL, *instance = NULL;
  1963. struct documentation_tree *doctree;
  1964. const char *name;
  1965. if (!(docs = ao2_container_alloc(127, ast_xml_doc_item_hash, ast_xml_doc_item_cmp))) {
  1966. ast_log(AST_LOG_ERROR, "Failed to create container for xml document item instances\n");
  1967. return NULL;
  1968. }
  1969. AST_RWLIST_RDLOCK(&xmldoc_tree);
  1970. AST_LIST_TRAVERSE(&xmldoc_tree, doctree, entry) {
  1971. /* the core xml documents have priority over thirdparty document. */
  1972. node = ast_xml_get_root(doctree->doc);
  1973. if (!node) {
  1974. break;
  1975. }
  1976. for (node = ast_xml_node_get_children(node); node; node = ast_xml_node_get_next(node)) {
  1977. /* Ignore empty nodes or nodes that aren't of the type requested */
  1978. if (!ast_xml_node_get_children(node) || strcasecmp(ast_xml_node_get_name(node), type)) {
  1979. continue;
  1980. }
  1981. name = ast_xml_get_attribute(node, "name");
  1982. if (!name) {
  1983. continue;
  1984. }
  1985. switch (xmldoc_get_syntax_type(type)) {
  1986. case MANAGER_EVENT_SYNTAX:
  1987. for (instance = ast_xml_node_get_children(node); instance; instance = ast_xml_node_get_next(instance)) {
  1988. struct ast_xml_doc_item *temp;
  1989. if (!ast_xml_node_get_children(instance) || strcasecmp(ast_xml_node_get_name(instance), "managerEventInstance")) {
  1990. continue;
  1991. }
  1992. temp = xmldoc_build_documentation_item(instance, name, type);
  1993. if (!temp) {
  1994. break;
  1995. }
  1996. if (!item) {
  1997. item = temp;
  1998. root = item;
  1999. } else {
  2000. item->next = temp;
  2001. item = temp;
  2002. }
  2003. }
  2004. item = root;
  2005. break;
  2006. default:
  2007. item = xmldoc_build_documentation_item(node, name, type);
  2008. }
  2009. ast_xml_free_attr(name);
  2010. if (item) {
  2011. ao2_link(docs, item);
  2012. ao2_t_ref(item, -1, "Dispose of creation ref");
  2013. item = NULL;
  2014. }
  2015. }
  2016. }
  2017. AST_RWLIST_UNLOCK(&xmldoc_tree);
  2018. return docs;
  2019. }
  2020. #if !defined(HAVE_GLOB_NOMAGIC) || !defined(HAVE_GLOB_BRACE) || defined(DEBUG_NONGNU)
  2021. static int xml_pathmatch(char *xmlpattern, int xmlpattern_maxlen, glob_t *globbuf)
  2022. {
  2023. int globret;
  2024. snprintf(xmlpattern, xmlpattern_maxlen, "%s/documentation/thirdparty/*-%s.xml",
  2025. ast_config_AST_DATA_DIR, documentation_language);
  2026. if((globret = glob(xmlpattern, GLOB_NOCHECK, NULL, globbuf))) {
  2027. return globret;
  2028. }
  2029. snprintf(xmlpattern, xmlpattern_maxlen, "%s/documentation/thirdparty/*-%.2s_??.xml",
  2030. ast_config_AST_DATA_DIR, documentation_language);
  2031. if((globret = glob(xmlpattern, GLOB_APPEND | GLOB_NOCHECK, NULL, globbuf))) {
  2032. return globret;
  2033. }
  2034. snprintf(xmlpattern, xmlpattern_maxlen, "%s/documentation/thirdparty/*-%s.xml",
  2035. ast_config_AST_DATA_DIR, default_documentation_language);
  2036. if((globret = glob(xmlpattern, GLOB_APPEND | GLOB_NOCHECK, NULL, globbuf))) {
  2037. return globret;
  2038. }
  2039. snprintf(xmlpattern, xmlpattern_maxlen, "%s/documentation/*-%s.xml",
  2040. ast_config_AST_DATA_DIR, documentation_language);
  2041. if((globret = glob(xmlpattern, GLOB_APPEND | GLOB_NOCHECK, NULL, globbuf))) {
  2042. return globret;
  2043. }
  2044. snprintf(xmlpattern, xmlpattern_maxlen, "%s/documentation/*-%.2s_??.xml",
  2045. ast_config_AST_DATA_DIR, documentation_language);
  2046. if((globret = glob(xmlpattern, GLOB_APPEND | GLOB_NOCHECK, NULL, globbuf))) {
  2047. return globret;
  2048. }
  2049. snprintf(xmlpattern, xmlpattern_maxlen, "%s/documentation/*-%s.xml",
  2050. ast_config_AST_DATA_DIR, default_documentation_language);
  2051. globret = glob(xmlpattern, GLOB_APPEND | GLOB_NOCHECK, NULL, globbuf);
  2052. return globret;
  2053. }
  2054. #endif
  2055. /*! \brief Close and unload XML documentation. */
  2056. static void xmldoc_unload_documentation(void)
  2057. {
  2058. struct documentation_tree *doctree;
  2059. AST_RWLIST_WRLOCK(&xmldoc_tree);
  2060. while ((doctree = AST_RWLIST_REMOVE_HEAD(&xmldoc_tree, entry))) {
  2061. ast_free(doctree->filename);
  2062. ast_xml_close(doctree->doc);
  2063. ast_free(doctree);
  2064. }
  2065. AST_RWLIST_UNLOCK(&xmldoc_tree);
  2066. ast_xml_finish();
  2067. }
  2068. int ast_xmldoc_load_documentation(void)
  2069. {
  2070. struct ast_xml_node *root_node;
  2071. struct ast_xml_doc *tmpdoc;
  2072. struct documentation_tree *doc_tree;
  2073. char *xmlpattern;
  2074. struct ast_config *cfg = NULL;
  2075. struct ast_variable *var = NULL;
  2076. struct ast_flags cnfflags = { 0 };
  2077. int globret, i, dup, duplicate;
  2078. glob_t globbuf;
  2079. #if !defined(HAVE_GLOB_NOMAGIC) || !defined(HAVE_GLOB_BRACE) || defined(DEBUG_NONGNU)
  2080. int xmlpattern_maxlen;
  2081. #endif
  2082. /* setup default XML documentation language */
  2083. snprintf(documentation_language, sizeof(documentation_language), default_documentation_language);
  2084. if ((cfg = ast_config_load2("asterisk.conf", "" /* core can't reload */, cnfflags)) && cfg != CONFIG_STATUS_FILEINVALID) {
  2085. for (var = ast_variable_browse(cfg, "options"); var; var = var->next) {
  2086. if (!strcasecmp(var->name, "documentation_language")) {
  2087. if (!ast_strlen_zero(var->value)) {
  2088. snprintf(documentation_language, sizeof(documentation_language), "%s", var->value);
  2089. }
  2090. }
  2091. }
  2092. ast_config_destroy(cfg);
  2093. }
  2094. /* initialize the XML library. */
  2095. ast_xml_init();
  2096. /* register function to be run when asterisk finish. */
  2097. ast_register_cleanup(xmldoc_unload_documentation);
  2098. globbuf.gl_offs = 0; /* slots to reserve in gl_pathv */
  2099. #if !defined(HAVE_GLOB_NOMAGIC) || !defined(HAVE_GLOB_BRACE) || defined(DEBUG_NONGNU)
  2100. xmlpattern_maxlen = strlen(ast_config_AST_DATA_DIR) + strlen("/documentation/thirdparty") + strlen("/*-??_??.xml") + 1;
  2101. xmlpattern = ast_malloc(xmlpattern_maxlen);
  2102. globret = xml_pathmatch(xmlpattern, xmlpattern_maxlen, &globbuf);
  2103. #else
  2104. /* Get every *-LANG.xml file inside $(ASTDATADIR)/documentation */
  2105. if (ast_asprintf(&xmlpattern, "%s/documentation{/thirdparty/,/}*-{%s,%.2s_??,%s}.xml", ast_config_AST_DATA_DIR,
  2106. documentation_language, documentation_language, default_documentation_language) < 0) {
  2107. return 1;
  2108. }
  2109. globret = glob(xmlpattern, MY_GLOB_FLAGS, NULL, &globbuf);
  2110. #endif
  2111. ast_debug(3, "gl_pathc %zu\n", (size_t)globbuf.gl_pathc);
  2112. if (globret == GLOB_NOSPACE) {
  2113. ast_log(LOG_WARNING, "XML load failure, glob expansion of pattern '%s' failed: Not enough memory\n", xmlpattern);
  2114. ast_free(xmlpattern);
  2115. return 1;
  2116. } else if (globret == GLOB_ABORTED) {
  2117. ast_log(LOG_WARNING, "XML load failure, glob expansion of pattern '%s' failed: Read error\n", xmlpattern);
  2118. ast_free(xmlpattern);
  2119. return 1;
  2120. }
  2121. ast_free(xmlpattern);
  2122. AST_RWLIST_WRLOCK(&xmldoc_tree);
  2123. /* loop over expanded files */
  2124. for (i = 0; i < globbuf.gl_pathc; i++) {
  2125. /* check for duplicates (if we already [try to] open the same file. */
  2126. duplicate = 0;
  2127. for (dup = 0; dup < i; dup++) {
  2128. if (!strcmp(globbuf.gl_pathv[i], globbuf.gl_pathv[dup])) {
  2129. duplicate = 1;
  2130. break;
  2131. }
  2132. }
  2133. if (duplicate || strchr(globbuf.gl_pathv[i], '*')) {
  2134. /* skip duplicates as well as pathnames not found
  2135. * (due to use of GLOB_NOCHECK in xml_pathmatch) */
  2136. continue;
  2137. }
  2138. tmpdoc = NULL;
  2139. tmpdoc = ast_xml_open(globbuf.gl_pathv[i]);
  2140. if (!tmpdoc) {
  2141. ast_log(LOG_ERROR, "Could not open XML documentation at '%s'\n", globbuf.gl_pathv[i]);
  2142. continue;
  2143. }
  2144. /* Get doc root node and check if it starts with '<docs>' */
  2145. root_node = ast_xml_get_root(tmpdoc);
  2146. if (!root_node) {
  2147. ast_log(LOG_ERROR, "Error getting documentation root node\n");
  2148. ast_xml_close(tmpdoc);
  2149. continue;
  2150. }
  2151. /* Check root node name for malformed xmls. */
  2152. if (strcmp(ast_xml_node_get_name(root_node), "docs")) {
  2153. ast_log(LOG_ERROR, "Documentation file is not well formed!\n");
  2154. ast_xml_close(tmpdoc);
  2155. continue;
  2156. }
  2157. doc_tree = ast_calloc(1, sizeof(*doc_tree));
  2158. if (!doc_tree) {
  2159. ast_log(LOG_ERROR, "Unable to allocate documentation_tree structure!\n");
  2160. ast_xml_close(tmpdoc);
  2161. continue;
  2162. }
  2163. doc_tree->doc = tmpdoc;
  2164. doc_tree->filename = ast_strdup(globbuf.gl_pathv[i]);
  2165. AST_RWLIST_INSERT_TAIL(&xmldoc_tree, doc_tree, entry);
  2166. }
  2167. AST_RWLIST_UNLOCK(&xmldoc_tree);
  2168. globfree(&globbuf);
  2169. return 0;
  2170. }
  2171. #endif /* AST_XML_DOCS */