xmldoc.c 84 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074
  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. * libxml2 http://www.xmlsoft.org/
  23. */
  24. /*** MODULEINFO
  25. <support_level>core</support_level>
  26. ***/
  27. #include "asterisk.h"
  28. #include "asterisk/_private.h"
  29. #include "asterisk/paths.h"
  30. #include "asterisk/linkedlists.h"
  31. #include "asterisk/config.h"
  32. #include "asterisk/term.h"
  33. #include "asterisk/astobj2.h"
  34. #include "asterisk/xmldoc.h"
  35. #include "asterisk/cli.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 = 79;
  42. /*! \brief XML documentation language. */
  43. static char documentation_language[6];
  44. /*! \brief XML documentation tree */
  45. struct documentation_tree {
  46. char *filename; /*!< XML document filename. */
  47. struct ast_xml_doc *doc; /*!< Open document pointer. */
  48. AST_RWLIST_ENTRY(documentation_tree) entry;
  49. };
  50. static char *xmldoc_get_syntax_cmd(struct ast_xml_node *fixnode, const char *name, int printname);
  51. static int xmldoc_parse_enumlist(struct ast_xml_node *fixnode, const char *tabs, struct ast_str **buffer);
  52. static void xmldoc_parse_parameter(struct ast_xml_node *fixnode, const char *tabs, struct ast_str **buffer);
  53. static int xmldoc_parse_info(struct ast_xml_node *node, const char *tabs, const char *posttabs, struct ast_str **buffer);
  54. static int xmldoc_parse_para(struct ast_xml_node *node, const char *tabs, const char *posttabs, struct ast_str **buffer);
  55. static int xmldoc_parse_specialtags(struct ast_xml_node *fixnode, const char *tabs, const char *posttabs, struct ast_str **buffer);
  56. /*!
  57. * \brief Container of documentation trees
  58. *
  59. * \note A RWLIST is a sufficient container type to use, provided
  60. * the list lock is always held while there are references to the list.
  61. */
  62. static AST_RWLIST_HEAD_STATIC(xmldoc_tree, documentation_tree);
  63. static const struct strcolorized_tags {
  64. const char *init; /*!< Replace initial tag with this string. */
  65. const char *end; /*!< Replace end tag with this string. */
  66. const int colorfg; /*!< Foreground color. */
  67. const char *inittag; /*!< Initial tag description. */
  68. const char *endtag; /*!< Ending tag description. */
  69. } colorized_tags[] = {
  70. { "<", ">", COLOR_GREEN, "<replaceable>", "</replaceable>" },
  71. { "\'", "\'", COLOR_BLUE, "<literal>", "</literal>" },
  72. { "*", "*", COLOR_RED, "<emphasis>", "</emphasis>" },
  73. { "\"", "\"", COLOR_YELLOW, "<filename>", "</filename>" },
  74. { "\"", "\"", COLOR_CYAN, "<directory>", "</directory>" },
  75. { "${", "}", COLOR_GREEN, "<variable>", "</variable>" },
  76. { "", "", COLOR_BLUE, "<value>", "</value>" },
  77. { "", "", COLOR_BLUE, "<enum>", "</enum>" },
  78. { "\'", "\'", COLOR_GRAY, "<astcli>", "</astcli>" },
  79. /* Special tags */
  80. { "", "", COLOR_YELLOW, "<note>", "</note>" },
  81. { "", "", COLOR_RED, "<warning>", "</warning>" },
  82. { "", "", COLOR_WHITE, "<example>", "</example>" },
  83. { "", "", COLOR_GRAY, "<exampletext>", "</exampletext>"},
  84. };
  85. static const struct strspecial_tags {
  86. const char *tagname; /*!< Special tag name. */
  87. const char *init; /*!< Print this at the beginning. */
  88. const char *end; /*!< Print this at the end. */
  89. } special_tags[] = {
  90. { "note", "<note>NOTE:</note> ", "" },
  91. { "warning", "<warning>WARNING!!!:</warning> ", "" },
  92. { "example", "<example>Example:</example> ", "" },
  93. };
  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. *
  99. * \param postbr The format string to use to calculate the length.
  100. *
  101. * \retval The postbr length.
  102. */
  103. static int xmldoc_postbrlen(const char *postbr)
  104. {
  105. int postbrreallen = 0, i;
  106. size_t postbrlen;
  107. if (!postbr) {
  108. return 0;
  109. }
  110. postbrlen = strlen(postbr);
  111. for (i = 0; i < postbrlen; i++) {
  112. if (postbr[i] == '\t') {
  113. postbrreallen += 8 - (postbrreallen % 8);
  114. } else {
  115. postbrreallen++;
  116. }
  117. }
  118. return postbrreallen;
  119. }
  120. /*!
  121. * \internal
  122. * \brief Setup postbr to be used while wrapping the text.
  123. * Add to postbr array all the spaces and tabs at the beginning of text.
  124. *
  125. * \param postbr output array.
  126. * \param len text array length.
  127. * \param text Text with format string before the actual string.
  128. */
  129. static void xmldoc_setpostbr(char *postbr, size_t len, const char *text)
  130. {
  131. int c, postbrlen = 0;
  132. if (!text) {
  133. return;
  134. }
  135. for (c = 0; c < len; c++) {
  136. if (text[c] == '\t' || text[c] == ' ') {
  137. postbr[postbrlen++] = text[c];
  138. } else {
  139. break;
  140. }
  141. }
  142. postbr[postbrlen] = '\0';
  143. }
  144. /*!
  145. * \internal
  146. * \brief Justify a text to a number of columns.
  147. *
  148. * \param text Input text to be justified.
  149. * \param columns Number of columns to preserve in the text.
  150. *
  151. * \retval NULL on error.
  152. * \retval The wrapped text.
  153. */
  154. static char *xmldoc_string_wrap(const char *text, int columns)
  155. {
  156. struct ast_str *tmp;
  157. char *ret, postbr[160];
  158. int count, i, textlen, postbrlen, lastbreak;
  159. /* sanity check */
  160. if (!text || columns <= 0) {
  161. ast_log(LOG_WARNING, "Passing wrong arguments while trying to wrap the text\n");
  162. return NULL;
  163. }
  164. tmp = ast_str_create(strlen(text) * 3);
  165. if (!tmp) {
  166. return NULL;
  167. }
  168. /* Check for blanks and tabs and put them in postbr. */
  169. xmldoc_setpostbr(postbr, sizeof(postbr), text);
  170. postbrlen = xmldoc_postbrlen(postbr);
  171. count = 0;
  172. lastbreak = 0;
  173. textlen = strlen(text);
  174. for (i = 0; i < textlen; i++) {
  175. if (text[i] == '\n') {
  176. xmldoc_setpostbr(postbr, sizeof(postbr), &text[i] + 1);
  177. postbrlen = xmldoc_postbrlen(postbr);
  178. count = 0;
  179. lastbreak = 0;
  180. } else if (text[i] == ESC) {
  181. /* Walk over escape sequences without counting them. */
  182. do {
  183. ast_str_append(&tmp, 0, "%c", text[i]);
  184. i++;
  185. } while (i < textlen && text[i] != 'm');
  186. } else {
  187. if (text[i] == ' ') {
  188. lastbreak = i;
  189. }
  190. count++;
  191. }
  192. if (count > columns) {
  193. /* Seek backwards if it was at most 30 characters ago. */
  194. int back = i - lastbreak;
  195. if (lastbreak && back > 0 && back < 30) {
  196. ast_str_truncate(tmp, -back);
  197. i = lastbreak; /* go back a bit */
  198. }
  199. ast_str_append(&tmp, 0, "\n%s", postbr);
  200. count = postbrlen;
  201. lastbreak = 0;
  202. } else {
  203. ast_str_append(&tmp, 0, "%c", text[i]);
  204. }
  205. }
  206. ret = ast_strdup(ast_str_buffer(tmp));
  207. ast_free(tmp);
  208. return ret;
  209. }
  210. char *ast_xmldoc_printable(const char *bwinput, int withcolors)
  211. {
  212. struct ast_str *colorized;
  213. char *wrapped = NULL;
  214. int i, c, len, colorsection;
  215. char *tmp;
  216. size_t bwinputlen;
  217. static const int base_fg = COLOR_CYAN;
  218. if (!bwinput) {
  219. return NULL;
  220. }
  221. bwinputlen = strlen(bwinput);
  222. if (!(colorized = ast_str_create(256))) {
  223. return NULL;
  224. }
  225. if (withcolors) {
  226. ast_term_color_code(&colorized, base_fg, 0);
  227. if (!colorized) {
  228. return NULL;
  229. }
  230. }
  231. for (i = 0; i < bwinputlen; i++) {
  232. colorsection = 0;
  233. /* Check if we are at the beginning of a tag to be colorized. */
  234. for (c = 0; c < ARRAY_LEN(colorized_tags); c++) {
  235. if (strncasecmp(bwinput + i, colorized_tags[c].inittag, strlen(colorized_tags[c].inittag))) {
  236. continue;
  237. }
  238. if (!(tmp = strcasestr(bwinput + i + strlen(colorized_tags[c].inittag), colorized_tags[c].endtag))) {
  239. continue;
  240. }
  241. len = tmp - (bwinput + i + strlen(colorized_tags[c].inittag));
  242. /* Setup color */
  243. if (withcolors) {
  244. if (ast_opt_light_background) {
  245. /* Turn off *bright* colors */
  246. ast_term_color_code(&colorized, colorized_tags[c].colorfg & 0x7f, 0);
  247. } else {
  248. /* Turn on *bright* colors */
  249. ast_term_color_code(&colorized, colorized_tags[c].colorfg | 0x80, 0);
  250. }
  251. if (!colorized) {
  252. return NULL;
  253. }
  254. }
  255. /* copy initial string replace */
  256. ast_str_append(&colorized, 0, "%s", colorized_tags[c].init);
  257. if (!colorized) {
  258. return NULL;
  259. }
  260. {
  261. char buf[len + 1];
  262. ast_copy_string(buf, bwinput + i + strlen(colorized_tags[c].inittag), sizeof(buf));
  263. ast_str_append(&colorized, 0, "%s", buf);
  264. }
  265. if (!colorized) {
  266. return NULL;
  267. }
  268. /* copy the ending string replace */
  269. ast_str_append(&colorized, 0, "%s", colorized_tags[c].end);
  270. if (!colorized) {
  271. return NULL;
  272. }
  273. /* Continue with the last color. */
  274. if (withcolors) {
  275. ast_term_color_code(&colorized, base_fg, 0);
  276. if (!colorized) {
  277. return NULL;
  278. }
  279. }
  280. i += len + strlen(colorized_tags[c].endtag) + strlen(colorized_tags[c].inittag) - 1;
  281. colorsection = 1;
  282. break;
  283. }
  284. if (!colorsection) {
  285. ast_str_append(&colorized, 0, "%c", bwinput[i]);
  286. if (!colorized) {
  287. return NULL;
  288. }
  289. }
  290. }
  291. if (withcolors) {
  292. ast_str_append(&colorized, 0, "%s", ast_term_reset());
  293. if (!colorized) {
  294. return NULL;
  295. }
  296. }
  297. /* Wrap the text, notice that string wrap will avoid cutting an ESC sequence. */
  298. wrapped = xmldoc_string_wrap(ast_str_buffer(colorized), xmldoc_text_columns);
  299. ast_free(colorized);
  300. return wrapped;
  301. }
  302. /*!
  303. * \internal
  304. * \brief Cleanup spaces and tabs after a \\n
  305. *
  306. * \param text String to be cleaned up.
  307. * \param output buffer (not already allocated).
  308. * \param lastspaces Remove last spaces in the string.
  309. * \param maintain_newlines Preserve new line characters (\\n \\r) discovered in the string
  310. */
  311. static void xmldoc_string_cleanup(const char *text, struct ast_str **output, int lastspaces, int maintain_newlines)
  312. {
  313. int i;
  314. size_t textlen;
  315. if (!text) {
  316. *output = NULL;
  317. return;
  318. }
  319. textlen = strlen(text);
  320. *output = ast_str_create(textlen);
  321. if (!(*output)) {
  322. ast_log(LOG_ERROR, "Problem allocating output buffer\n");
  323. return;
  324. }
  325. for (i = 0; i < textlen; i++) {
  326. if (text[i] == '\n' || text[i] == '\r') {
  327. if (maintain_newlines) {
  328. ast_str_append(output, 0, "%c", text[i]);
  329. }
  330. /* remove spaces/tabs/\n after a \n. */
  331. while (text[i + 1] == '\t' || text[i + 1] == '\r' || text[i + 1] == '\n') {
  332. i++;
  333. }
  334. ast_str_append(output, 0, " ");
  335. continue;
  336. } else {
  337. ast_str_append(output, 0, "%c", text[i]);
  338. }
  339. }
  340. /* remove last spaces (we don't want always to remove the trailing spaces). */
  341. if (lastspaces) {
  342. ast_str_trim_blanks(*output);
  343. }
  344. }
  345. /*!
  346. * \internal
  347. * \brief Check if the given attribute on the given node matches the given value.
  348. *
  349. * \param node the node to match
  350. * \param attr the name of the attribute
  351. * \param value the expected value of the attribute
  352. *
  353. * \retval true if the given attribute contains the given value
  354. * \retval false if the given attribute does not exist or does not contain the given value
  355. */
  356. static int xmldoc_attribute_match(struct ast_xml_node *node, const char *attr, const char *value)
  357. {
  358. const char *attr_value = ast_xml_get_attribute(node, attr);
  359. int match = attr_value && !strcmp(attr_value, value);
  360. ast_xml_free_attr(attr_value);
  361. return match;
  362. }
  363. /*!
  364. * \internal
  365. * \brief Get the application/function node for 'name' application/function with language 'language'
  366. * and module 'module' if we don't find any, get the first application
  367. * with 'name' no matter which language or module.
  368. *
  369. * \param type 'application', 'function', ...
  370. * \param name Application or Function name.
  371. * \param module Module item is in.
  372. * \param language Try to get this language (if not found try with en_US)
  373. *
  374. * \retval NULL on error.
  375. * \retval A node of type ast_xml_node.
  376. *
  377. * \note Must be called with a RDLOCK held on xmldoc_tree
  378. */
  379. static struct ast_xml_node *xmldoc_get_node(const char *type, const char *name, const char *module, const char *language)
  380. {
  381. struct ast_xml_node *node = NULL;
  382. struct ast_xml_node *first_match = NULL;
  383. struct ast_xml_node *lang_match = NULL;
  384. struct documentation_tree *doctree;
  385. AST_LIST_TRAVERSE(&xmldoc_tree, doctree, entry) {
  386. /* the core xml documents have priority over thirdparty document. */
  387. node = ast_xml_get_root(doctree->doc);
  388. if (!node) {
  389. break;
  390. }
  391. node = ast_xml_node_get_children(node);
  392. while ((node = ast_xml_find_element(node, type, "name", name))) {
  393. if (!ast_xml_node_get_children(node)) {
  394. /* ignore empty nodes */
  395. node = ast_xml_node_get_next(node);
  396. continue;
  397. }
  398. if (!first_match) {
  399. first_match = node;
  400. }
  401. /* Check language */
  402. if (xmldoc_attribute_match(node, "language", language)) {
  403. if (!lang_match) {
  404. lang_match = node;
  405. }
  406. /* if module is empty we have a match */
  407. if (ast_strlen_zero(module)) {
  408. break;
  409. }
  410. /* Check module */
  411. if (xmldoc_attribute_match(node, "module", module)) {
  412. break;
  413. }
  414. }
  415. node = ast_xml_node_get_next(node);
  416. }
  417. /* if we matched lang and module return this match */
  418. if (node) {
  419. break;
  420. }
  421. /* we didn't match lang and module, just return the first
  422. * result with a matching language if we have one */
  423. if (lang_match) {
  424. node = lang_match;
  425. break;
  426. }
  427. /* we didn't match with only the language, just return the
  428. * first match */
  429. if (first_match) {
  430. node = first_match;
  431. break;
  432. }
  433. }
  434. return node;
  435. }
  436. /*!
  437. * \internal
  438. * \brief Helper function used to build the syntax, it allocates the needed buffer (or reallocates it),
  439. * and based on the reverse value it makes use of fmt to print the parameter list inside the
  440. * realloced buffer (syntax).
  441. *
  442. * \param reverse We are going backwards while generating the syntax?
  443. * \param len Current length of 'syntax' buffer.
  444. * \param syntax Output buffer for the concatenated values.
  445. * \param fmt A format string that will be used in a sprintf call.
  446. */
  447. static void __attribute__((format(printf, 4, 5))) xmldoc_reverse_helper(int reverse, int *len, char **syntax, const char *fmt, ...)
  448. {
  449. int totlen;
  450. int tmpfmtlen;
  451. char *tmpfmt;
  452. char *new_syntax;
  453. char tmp;
  454. va_list ap;
  455. va_start(ap, fmt);
  456. if (ast_vasprintf(&tmpfmt, fmt, ap) < 0) {
  457. va_end(ap);
  458. return;
  459. }
  460. va_end(ap);
  461. tmpfmtlen = strlen(tmpfmt);
  462. totlen = *len + tmpfmtlen + 1;
  463. new_syntax = ast_realloc(*syntax, totlen);
  464. if (!new_syntax) {
  465. ast_free(tmpfmt);
  466. return;
  467. }
  468. *syntax = new_syntax;
  469. if (reverse) {
  470. memmove(*syntax + tmpfmtlen, *syntax, *len);
  471. /* Save this char, it will be overwritten by the \0 of strcpy. */
  472. tmp = (*syntax)[0];
  473. strcpy(*syntax, tmpfmt);
  474. /* Restore the already saved char. */
  475. (*syntax)[tmpfmtlen] = tmp;
  476. (*syntax)[totlen - 1] = '\0';
  477. } else {
  478. strcpy(*syntax + *len, tmpfmt);
  479. }
  480. *len = totlen - 1;
  481. ast_free(tmpfmt);
  482. }
  483. /*!
  484. * \internal
  485. * \brief Check if the passed node has 'what' tags inside it.
  486. *
  487. * \param fixnode Root node to search 'what' elements.
  488. * \param what Node name to search inside node.
  489. *
  490. * \retval 1 If a 'what' element is found inside 'node'.
  491. * \retval 0 If no 'what' is found inside 'node'.
  492. */
  493. static int xmldoc_has_inside(struct ast_xml_node *fixnode, const char *what)
  494. {
  495. struct ast_xml_node *node = fixnode;
  496. for (node = ast_xml_node_get_children(fixnode); node; node = ast_xml_node_get_next(node)) {
  497. if (!strcasecmp(ast_xml_node_get_name(node), what)) {
  498. return 1;
  499. }
  500. }
  501. return 0;
  502. }
  503. /*!
  504. * \internal
  505. * \brief Check if the passed node has at least one node inside it.
  506. *
  507. * \param fixnode Root node to search node elements.
  508. *
  509. * \retval 1 If a node element is found inside 'node'.
  510. * \retval 0 If no node is found inside 'node'.
  511. */
  512. static int xmldoc_has_nodes(struct ast_xml_node *fixnode)
  513. {
  514. struct ast_xml_node *node = fixnode;
  515. for (node = ast_xml_node_get_children(fixnode); node; node = ast_xml_node_get_next(node)) {
  516. if (strcasecmp(ast_xml_node_get_name(node), "text")) {
  517. return 1;
  518. }
  519. }
  520. return 0;
  521. }
  522. /*!
  523. * \internal
  524. * \brief Check if the passed node has at least one specialtag.
  525. *
  526. * \param fixnode Root node to search "specialtags" elements.
  527. *
  528. * \retval 1 If a "specialtag" element is found inside 'node'.
  529. * \retval 0 If no "specialtag" is found inside 'node'.
  530. */
  531. static int xmldoc_has_specialtags(struct ast_xml_node *fixnode)
  532. {
  533. struct ast_xml_node *node = fixnode;
  534. int i;
  535. for (node = ast_xml_node_get_children(fixnode); node; node = ast_xml_node_get_next(node)) {
  536. for (i = 0; i < ARRAY_LEN(special_tags); i++) {
  537. if (!strcasecmp(ast_xml_node_get_name(node), special_tags[i].tagname)) {
  538. return 1;
  539. }
  540. }
  541. }
  542. return 0;
  543. }
  544. /*!
  545. * \internal
  546. * \brief Build the syntax for a specified starting node.
  547. *
  548. * \param rootnode A pointer to the ast_xml root node.
  549. * \param rootname Name of the application, function, option, etc. to build the syntax.
  550. * \param childname The name of each parameter node.
  551. * \param printparenthesis Boolean if we must print parenthesis if not parameters are found in the rootnode.
  552. * \param printrootname Boolean if we must print the rootname before the syntax and parenthesis at the begining/end.
  553. *
  554. * \retval NULL on error.
  555. * \retval An ast_malloc'ed string with the syntax generated.
  556. */
  557. static char *xmldoc_get_syntax_fun(struct ast_xml_node *rootnode, const char *rootname, const char *childname, int printparenthesis, int printrootname)
  558. {
  559. #define GOTONEXT(__rev, __a) (__rev ? ast_xml_node_get_prev(__a) : ast_xml_node_get_next(__a))
  560. #define ISLAST(__rev, __a) (__rev == 1 ? (ast_xml_node_get_prev(__a) ? 0 : 1) : (ast_xml_node_get_next(__a) ? 0 : 1))
  561. #define MP(__a) ((multiple ? __a : ""))
  562. struct ast_xml_node *node = NULL, *firstparam = NULL, *lastparam = NULL;
  563. const char *paramtype, *multipletype, *paramnameattr, *attrargsep, *parenthesis, *argname;
  564. int reverse, required, paramcount = 0, openbrackets = 0, len = 0, hasparams=0;
  565. int reqfinode = 0, reqlanode = 0, optmidnode = 0, prnparenthesis, multiple;
  566. char *syntax = NULL, *argsep, *paramname;
  567. if (ast_strlen_zero(rootname) || ast_strlen_zero(childname)) {
  568. ast_log(LOG_WARNING, "Tried to look in XML tree with faulty rootname or childname while creating a syntax.\n");
  569. return NULL;
  570. }
  571. if (!rootnode || !ast_xml_node_get_children(rootnode)) {
  572. /* If the rootnode field is not found, at least print name. */
  573. if (ast_asprintf(&syntax, "%s%s", (printrootname ? rootname : ""), (printparenthesis ? "()" : "")) < 0) {
  574. syntax = NULL;
  575. }
  576. return syntax;
  577. }
  578. /* Get the argument separator from the root node attribute name 'argsep', if not found
  579. defaults to ','. */
  580. attrargsep = ast_xml_get_attribute(rootnode, "argsep");
  581. if (attrargsep) {
  582. argsep = ast_strdupa(attrargsep);
  583. ast_xml_free_attr(attrargsep);
  584. } else {
  585. argsep = ast_strdupa(",");
  586. }
  587. /* Get order of evaluation. */
  588. for (node = ast_xml_node_get_children(rootnode); node; node = ast_xml_node_get_next(node)) {
  589. if (strcasecmp(ast_xml_node_get_name(node), childname)) {
  590. continue;
  591. }
  592. required = 0;
  593. hasparams = 1;
  594. if ((paramtype = ast_xml_get_attribute(node, "required"))) {
  595. if (ast_true(paramtype)) {
  596. required = 1;
  597. }
  598. ast_xml_free_attr(paramtype);
  599. }
  600. lastparam = node;
  601. reqlanode = required;
  602. if (!firstparam) {
  603. /* first parameter node */
  604. firstparam = node;
  605. reqfinode = required;
  606. }
  607. }
  608. if (!hasparams) {
  609. /* This application, function, option, etc, doesn't have any params. */
  610. if (ast_asprintf(&syntax, "%s%s", (printrootname ? rootname : ""), (printparenthesis ? "()" : "")) < 0) {
  611. syntax = NULL;
  612. }
  613. return syntax;
  614. }
  615. if (reqfinode && reqlanode) {
  616. /* check midnode */
  617. for (node = ast_xml_node_get_children(rootnode); node; node = ast_xml_node_get_next(node)) {
  618. if (strcasecmp(ast_xml_node_get_name(node), childname)) {
  619. continue;
  620. }
  621. if (node != firstparam && node != lastparam) {
  622. if ((paramtype = ast_xml_get_attribute(node, "required"))) {
  623. if (!ast_true(paramtype)) {
  624. optmidnode = 1;
  625. ast_xml_free_attr(paramtype);
  626. break;
  627. }
  628. ast_xml_free_attr(paramtype);
  629. }
  630. }
  631. }
  632. }
  633. if ((!reqfinode && reqlanode) || (reqfinode && reqlanode && optmidnode)) {
  634. reverse = 1;
  635. node = lastparam;
  636. } else {
  637. reverse = 0;
  638. node = firstparam;
  639. }
  640. /* init syntax string. */
  641. if (reverse) {
  642. xmldoc_reverse_helper(reverse, &len, &syntax,
  643. (printrootname ? (printrootname == 2 ? ")]" : ")"): ""));
  644. } else {
  645. xmldoc_reverse_helper(reverse, &len, &syntax, "%s%s", (printrootname ? rootname : ""),
  646. (printrootname ? (printrootname == 2 ? "[(" : "(") : ""));
  647. }
  648. for (; node; node = GOTONEXT(reverse, node)) {
  649. if (strcasecmp(ast_xml_node_get_name(node), childname)) {
  650. continue;
  651. }
  652. /* Get the argument name, if it is not the leaf, go inside that parameter. */
  653. if (xmldoc_has_inside(node, "argument")) {
  654. parenthesis = ast_xml_get_attribute(node, "hasparams");
  655. prnparenthesis = 0;
  656. if (parenthesis) {
  657. prnparenthesis = ast_true(parenthesis);
  658. if (!strcasecmp(parenthesis, "optional")) {
  659. prnparenthesis = 2;
  660. }
  661. ast_xml_free_attr(parenthesis);
  662. }
  663. argname = ast_xml_get_attribute(node, "name");
  664. if (argname) {
  665. paramname = xmldoc_get_syntax_fun(node, argname, "argument", prnparenthesis, prnparenthesis);
  666. ast_xml_free_attr(argname);
  667. } else {
  668. /* Malformed XML, print **UNKOWN** */
  669. paramname = ast_strdup("**unknown**");
  670. }
  671. } else {
  672. paramnameattr = ast_xml_get_attribute(node, "name");
  673. if (!paramnameattr) {
  674. ast_log(LOG_WARNING, "Malformed XML %s: no %s name\n", rootname, childname);
  675. if (syntax) {
  676. /* Free already allocated syntax */
  677. ast_free(syntax);
  678. }
  679. /* to give up is ok? */
  680. if (ast_asprintf(&syntax, "%s%s", (printrootname ? rootname : ""), (printparenthesis ? "()" : "")) < 0) {
  681. syntax = NULL;
  682. }
  683. return syntax;
  684. }
  685. paramname = ast_strdup(paramnameattr);
  686. ast_xml_free_attr(paramnameattr);
  687. }
  688. if (!paramname) {
  689. return NULL;
  690. }
  691. /* Defaults to 'false'. */
  692. multiple = 0;
  693. if ((multipletype = ast_xml_get_attribute(node, "multiple"))) {
  694. if (ast_true(multipletype)) {
  695. multiple = 1;
  696. }
  697. ast_xml_free_attr(multipletype);
  698. }
  699. required = 0; /* Defaults to 'false'. */
  700. if ((paramtype = ast_xml_get_attribute(node, "required"))) {
  701. if (ast_true(paramtype)) {
  702. required = 1;
  703. }
  704. ast_xml_free_attr(paramtype);
  705. }
  706. /* build syntax core. */
  707. if (required) {
  708. /* First parameter */
  709. if (!paramcount) {
  710. xmldoc_reverse_helper(reverse, &len, &syntax, "%s%s%s%s", paramname, MP("["), MP(argsep), MP("...]"));
  711. } else {
  712. /* Time to close open brackets. */
  713. while (openbrackets > 0) {
  714. xmldoc_reverse_helper(reverse, &len, &syntax, (reverse ? "[" : "]"));
  715. openbrackets--;
  716. }
  717. if (reverse) {
  718. xmldoc_reverse_helper(reverse, &len, &syntax, "%s%s", paramname, argsep);
  719. } else {
  720. xmldoc_reverse_helper(reverse, &len, &syntax, "%s%s", argsep, paramname);
  721. }
  722. xmldoc_reverse_helper(reverse, &len, &syntax, "%s%s%s", MP("["), MP(argsep), MP("...]"));
  723. }
  724. } else {
  725. /* First parameter */
  726. if (!paramcount) {
  727. xmldoc_reverse_helper(reverse, &len, &syntax, "[%s%s%s%s]", paramname, MP("["), MP(argsep), MP("...]"));
  728. } else {
  729. if (ISLAST(reverse, node)) {
  730. /* This is the last parameter. */
  731. if (reverse) {
  732. xmldoc_reverse_helper(reverse, &len, &syntax, "[%s%s%s%s]%s", paramname,
  733. MP("["), MP(argsep), MP("...]"), argsep);
  734. } else {
  735. xmldoc_reverse_helper(reverse, &len, &syntax, "%s[%s%s%s%s]", argsep, paramname,
  736. MP("["), MP(argsep), MP("...]"));
  737. }
  738. } else {
  739. if (reverse) {
  740. xmldoc_reverse_helper(reverse, &len, &syntax, "%s%s%s%s%s]", paramname, argsep,
  741. MP("["), MP(argsep), MP("...]"));
  742. } else {
  743. xmldoc_reverse_helper(reverse, &len, &syntax, "[%s%s%s%s%s", argsep, paramname,
  744. MP("["), MP(argsep), MP("...]"));
  745. }
  746. openbrackets++;
  747. }
  748. }
  749. }
  750. ast_free(paramname);
  751. paramcount++;
  752. }
  753. /* Time to close open brackets. */
  754. while (openbrackets > 0) {
  755. xmldoc_reverse_helper(reverse, &len, &syntax, (reverse ? "[" : "]"));
  756. openbrackets--;
  757. }
  758. /* close syntax string. */
  759. if (reverse) {
  760. xmldoc_reverse_helper(reverse, &len, &syntax, "%s%s", (printrootname ? rootname : ""),
  761. (printrootname ? (printrootname == 2 ? "[(" : "(") : ""));
  762. } else {
  763. xmldoc_reverse_helper(reverse, &len, &syntax, (printrootname ? (printrootname == 2 ? ")]" : ")") : ""));
  764. }
  765. return syntax;
  766. #undef ISLAST
  767. #undef GOTONEXT
  768. #undef MP
  769. }
  770. /*!
  771. * \internal
  772. * \brief Parse an enumlist inside a \<parameter\> to generate a COMMAND syntax.
  773. *
  774. * \param fixnode A pointer to the \<enumlist\> node.
  775. *
  776. * \retval {<unknown>} on error.
  777. * \retval A string inside brackets {} with the enum's separated by pipes |.
  778. */
  779. static char *xmldoc_parse_cmd_enumlist(struct ast_xml_node *fixnode)
  780. {
  781. struct ast_xml_node *node = fixnode;
  782. struct ast_str *paramname;
  783. char *enumname, *ret;
  784. int first = 1;
  785. paramname = ast_str_create(128);
  786. if (!paramname) {
  787. return ast_strdup("{<unkown>}");
  788. }
  789. ast_str_append(&paramname, 0, "{");
  790. for (node = ast_xml_node_get_children(node); node; node = ast_xml_node_get_next(node)) {
  791. if (strcasecmp(ast_xml_node_get_name(node), "enum")) {
  792. continue;
  793. }
  794. enumname = xmldoc_get_syntax_cmd(node, "", 0);
  795. if (!enumname) {
  796. continue;
  797. }
  798. if (!first) {
  799. ast_str_append(&paramname, 0, "|");
  800. }
  801. ast_str_append(&paramname, 0, "%s", enumname);
  802. first = 0;
  803. ast_free(enumname);
  804. }
  805. ast_str_append(&paramname, 0, "}");
  806. ret = ast_strdup(ast_str_buffer(paramname));
  807. ast_free(paramname);
  808. return ret;
  809. }
  810. /*!
  811. * \internal
  812. * \brief Generate a syntax of COMMAND type.
  813. *
  814. * \param fixnode The \<syntax\> node pointer.
  815. * \param name The name of the 'command'.
  816. * \param printname Print the name of the command before the paramters?
  817. *
  818. * \return On error, return just 'name'.
  819. * \return On success return the generated syntax.
  820. */
  821. static char *xmldoc_get_syntax_cmd(struct ast_xml_node *fixnode, const char *name, int printname)
  822. {
  823. struct ast_str *syntax;
  824. struct ast_xml_node *tmpnode, *node = fixnode;
  825. char *ret, *paramname;
  826. const char *paramtype, *attrname, *literal;
  827. int required, isenum, first = 1, isliteral;
  828. if (!fixnode) {
  829. return NULL;
  830. }
  831. syntax = ast_str_create(128);
  832. if (!syntax) {
  833. /* at least try to return something... */
  834. return ast_strdup(name);
  835. }
  836. /* append name to output string. */
  837. if (printname) {
  838. ast_str_append(&syntax, 0, "%s", name);
  839. first = 0;
  840. }
  841. for (node = ast_xml_node_get_children(node); node; node = ast_xml_node_get_next(node)) {
  842. if (strcasecmp(ast_xml_node_get_name(node), "parameter")) {
  843. continue;
  844. }
  845. if (xmldoc_has_inside(node, "parameter")) {
  846. /* is this a recursive parameter. */
  847. paramname = xmldoc_get_syntax_cmd(node, "", 0);
  848. isenum = 1;
  849. } else {
  850. for (tmpnode = ast_xml_node_get_children(node); tmpnode; tmpnode = ast_xml_node_get_next(tmpnode)) {
  851. if (!strcasecmp(ast_xml_node_get_name(tmpnode), "enumlist")) {
  852. break;
  853. }
  854. }
  855. if (tmpnode) {
  856. /* parse enumlist (note that this is a special enumlist
  857. that is used to describe a syntax like {<param1>|<param2>|...} */
  858. paramname = xmldoc_parse_cmd_enumlist(tmpnode);
  859. isenum = 1;
  860. } else {
  861. /* this is a simple parameter. */
  862. attrname = ast_xml_get_attribute(node, "name");
  863. if (!attrname) {
  864. /* ignore this bogus parameter and continue. */
  865. continue;
  866. }
  867. paramname = ast_strdup(attrname);
  868. ast_xml_free_attr(attrname);
  869. isenum = 0;
  870. }
  871. }
  872. /* Is this parameter required? */
  873. required = 0;
  874. paramtype = ast_xml_get_attribute(node, "required");
  875. if (paramtype) {
  876. required = ast_true(paramtype);
  877. ast_xml_free_attr(paramtype);
  878. }
  879. /* Is this a replaceable value or a fixed parameter value? */
  880. isliteral = 0;
  881. literal = ast_xml_get_attribute(node, "literal");
  882. if (literal) {
  883. isliteral = ast_true(literal);
  884. ast_xml_free_attr(literal);
  885. }
  886. /* if required="false" print with [...].
  887. * if literal="true" or is enum print without <..>.
  888. * if not first print a space at the beginning.
  889. */
  890. ast_str_append(&syntax, 0, "%s%s%s%s%s%s",
  891. (first ? "" : " "),
  892. (required ? "" : "["),
  893. (isenum || isliteral ? "" : "<"),
  894. paramname,
  895. (isenum || isliteral ? "" : ">"),
  896. (required ? "" : "]"));
  897. first = 0;
  898. ast_free(paramname);
  899. }
  900. /* return a common string. */
  901. ret = ast_strdup(ast_str_buffer(syntax));
  902. ast_free(syntax);
  903. return ret;
  904. }
  905. /*!
  906. * \internal
  907. * \brief Generate an AMI action/event syntax.
  908. *
  909. * \param fixnode The manager action/event node pointer.
  910. * \param name The name of the manager action/event.
  911. * \param manager_type "Action" or "Event"
  912. *
  913. * \retval The generated syntax.
  914. * \retval NULL on error.
  915. */
  916. static char *xmldoc_get_syntax_manager(struct ast_xml_node *fixnode, const char *name, const char *manager_type)
  917. {
  918. struct ast_str *syntax;
  919. struct ast_xml_node *node = fixnode;
  920. const char *paramtype, *attrname;
  921. int required;
  922. char *ret;
  923. if (!fixnode) {
  924. return NULL;
  925. }
  926. syntax = ast_str_create(128);
  927. if (!syntax) {
  928. return ast_strdup(name);
  929. }
  930. ast_str_append(&syntax, 0, "%s: %s", manager_type, name);
  931. for (node = ast_xml_node_get_children(node); node; node = ast_xml_node_get_next(node)) {
  932. if (strcasecmp(ast_xml_node_get_name(node), "parameter")) {
  933. continue;
  934. }
  935. /* Is this parameter required? */
  936. required = !strcasecmp(manager_type, "event") ? 1 : 0;
  937. paramtype = ast_xml_get_attribute(node, "required");
  938. if (paramtype) {
  939. required = ast_true(paramtype);
  940. ast_xml_free_attr(paramtype);
  941. }
  942. attrname = ast_xml_get_attribute(node, "name");
  943. if (!attrname) {
  944. /* ignore this bogus parameter and continue. */
  945. continue;
  946. }
  947. ast_str_append(&syntax, 0, "\n%s%s:%s <value>",
  948. (required ? "" : "["),
  949. attrname,
  950. (required ? "" : "]"));
  951. ast_xml_free_attr(attrname);
  952. }
  953. /* return a common string. */
  954. ret = ast_strdup(ast_str_buffer(syntax));
  955. ast_free(syntax);
  956. return ret;
  957. }
  958. static char *xmldoc_get_syntax_config_object(struct ast_xml_node *fixnode, const char *name)
  959. {
  960. struct ast_xml_node *matchinfo, *tmp;
  961. int match;
  962. const char *attr_value;
  963. const char *text;
  964. RAII_VAR(struct ast_str *, syntax, ast_str_create(128), ast_free);
  965. if (!syntax || !fixnode) {
  966. return NULL;
  967. }
  968. if (!(matchinfo = ast_xml_find_element(ast_xml_node_get_children(fixnode), "matchInfo", NULL, NULL))) {
  969. return NULL;
  970. }
  971. if (!(tmp = ast_xml_find_element(ast_xml_node_get_children(matchinfo), "category", NULL, NULL))) {
  972. return NULL;
  973. }
  974. attr_value = ast_xml_get_attribute(tmp, "match");
  975. if (attr_value) {
  976. match = ast_true(attr_value);
  977. text = ast_xml_get_text(tmp);
  978. ast_str_set(&syntax, 0, "category %s /%s/", match ? "=~" : "!~", text);
  979. ast_xml_free_attr(attr_value);
  980. ast_xml_free_text(text);
  981. }
  982. if ((tmp = ast_xml_find_element(ast_xml_node_get_children(matchinfo), "field", NULL, NULL))) {
  983. text = ast_xml_get_text(tmp);
  984. attr_value = ast_xml_get_attribute(tmp, "name");
  985. ast_str_append(&syntax, 0, " matchfield: %s = %s", S_OR(attr_value, "Unknown"), text);
  986. ast_xml_free_attr(attr_value);
  987. ast_xml_free_text(text);
  988. }
  989. return ast_strdup(ast_str_buffer(syntax));
  990. }
  991. static char *xmldoc_get_syntax_config_option(struct ast_xml_node *fixnode, const char *name)
  992. {
  993. const char *type;
  994. const char *default_value;
  995. const char *regex;
  996. RAII_VAR(struct ast_str *, syntax, ast_str_create(128), ast_free);
  997. if (!syntax || !fixnode) {
  998. return NULL;
  999. }
  1000. type = ast_xml_get_attribute(fixnode, "type");
  1001. default_value = ast_xml_get_attribute(fixnode, "default");
  1002. regex = ast_xml_get_attribute(fixnode, "regex");
  1003. ast_str_set(&syntax, 0, "%s = [%s] (Default: %s) (Regex: %s)\n",
  1004. name,
  1005. type ?: "",
  1006. default_value ?: "n/a",
  1007. regex ?: "False");
  1008. ast_xml_free_attr(type);
  1009. ast_xml_free_attr(default_value);
  1010. ast_xml_free_attr(regex);
  1011. return ast_strdup(ast_str_buffer(syntax));
  1012. }
  1013. /*! \brief Types of syntax that we are able to generate. */
  1014. enum syntaxtype {
  1015. FUNCTION_SYNTAX,
  1016. MANAGER_SYNTAX,
  1017. MANAGER_EVENT_SYNTAX,
  1018. CONFIG_INFO_SYNTAX,
  1019. CONFIG_FILE_SYNTAX,
  1020. CONFIG_OPTION_SYNTAX,
  1021. CONFIG_OBJECT_SYNTAX,
  1022. COMMAND_SYNTAX
  1023. };
  1024. /*! \brief Mapping between type of node and type of syntax to generate. */
  1025. static struct strsyntaxtype {
  1026. const char *type;
  1027. enum syntaxtype stxtype;
  1028. } stxtype[] = {
  1029. { "function", FUNCTION_SYNTAX },
  1030. { "application", FUNCTION_SYNTAX },
  1031. { "manager", MANAGER_SYNTAX },
  1032. { "managerEvent", MANAGER_EVENT_SYNTAX },
  1033. { "configInfo", CONFIG_INFO_SYNTAX },
  1034. { "configFile", CONFIG_FILE_SYNTAX },
  1035. { "configOption", CONFIG_OPTION_SYNTAX },
  1036. { "configObject", CONFIG_OBJECT_SYNTAX },
  1037. { "agi", COMMAND_SYNTAX },
  1038. };
  1039. /*!
  1040. * \internal
  1041. * \brief Get syntax type based on type of node.
  1042. *
  1043. * \param type Type of node.
  1044. *
  1045. * \retval The type of syntax to generate based on the type of node.
  1046. */
  1047. static enum syntaxtype xmldoc_get_syntax_type(const char *type)
  1048. {
  1049. int i;
  1050. for (i=0; i < ARRAY_LEN(stxtype); i++) {
  1051. if (!strcasecmp(stxtype[i].type, type)) {
  1052. return stxtype[i].stxtype;
  1053. }
  1054. }
  1055. return FUNCTION_SYNTAX;
  1056. }
  1057. /*!
  1058. * \internal
  1059. * \brief Build syntax information for an item
  1060. * \param root_node The syntax node to parse
  1061. * \param type The source type
  1062. * \param name The name of the item that the syntax describes
  1063. *
  1064. * \note This method exists for when you already have the node. This
  1065. * prevents having to lock the documentation tree twice
  1066. *
  1067. * \retval A malloc'd character pointer to the syntax of the item
  1068. * \retval NULL on failure
  1069. *
  1070. * \since 11
  1071. */
  1072. static char *_ast_xmldoc_build_syntax(struct ast_xml_node *root_node, const char *type, const char *name)
  1073. {
  1074. char *syntax = NULL;
  1075. struct ast_xml_node *node = root_node;
  1076. for (node = ast_xml_node_get_children(node); node; node = ast_xml_node_get_next(node)) {
  1077. if (!strcasecmp(ast_xml_node_get_name(node), "syntax")) {
  1078. break;
  1079. }
  1080. }
  1081. switch (xmldoc_get_syntax_type(type)) {
  1082. case FUNCTION_SYNTAX:
  1083. syntax = xmldoc_get_syntax_fun(node, name, "parameter", 1, 1);
  1084. break;
  1085. case COMMAND_SYNTAX:
  1086. syntax = xmldoc_get_syntax_cmd(node, name, 1);
  1087. break;
  1088. case MANAGER_SYNTAX:
  1089. syntax = xmldoc_get_syntax_manager(node, name, "Action");
  1090. break;
  1091. case MANAGER_EVENT_SYNTAX:
  1092. syntax = xmldoc_get_syntax_manager(node, name, "Event");
  1093. break;
  1094. case CONFIG_OPTION_SYNTAX:
  1095. syntax = xmldoc_get_syntax_config_option(root_node, name);
  1096. break;
  1097. case CONFIG_OBJECT_SYNTAX:
  1098. syntax = xmldoc_get_syntax_config_object(node, name);
  1099. break;
  1100. default:
  1101. syntax = xmldoc_get_syntax_fun(node, name, "parameter", 1, 1);
  1102. }
  1103. return syntax;
  1104. }
  1105. char *ast_xmldoc_build_syntax(const char *type, const char *name, const char *module)
  1106. {
  1107. struct ast_xml_node *node;
  1108. char *syntax;
  1109. AST_RWLIST_RDLOCK(&xmldoc_tree);
  1110. node = xmldoc_get_node(type, name, module, documentation_language);
  1111. if (!node) {
  1112. AST_RWLIST_UNLOCK(&xmldoc_tree);
  1113. return NULL;
  1114. }
  1115. syntax = _ast_xmldoc_build_syntax(node, type, name);
  1116. AST_RWLIST_UNLOCK(&xmldoc_tree);
  1117. return syntax;
  1118. }
  1119. /*!
  1120. * \internal
  1121. * \brief Parse common internal elements. This includes paragraphs, special
  1122. * tags, and information nodes.
  1123. *
  1124. * \param node The element to parse
  1125. * \param tabs Add this string before the content of the parsed element.
  1126. * \param posttabs Add this string after the content of the parsed element.
  1127. * \param buffer This must be an already allocated ast_str. It will be used to
  1128. * store the result (if something has already been placed in the
  1129. * buffer, the parsed elements will be appended)
  1130. *
  1131. * \retval 1 if any data was appended to the buffer
  1132. * \retval 2 if the data appended to the buffer contained a text paragraph
  1133. * \retval 0 if no data was appended to the buffer
  1134. */
  1135. static int xmldoc_parse_common_elements(struct ast_xml_node *node, const char *tabs, const char *posttabs, struct ast_str **buffer)
  1136. {
  1137. return (xmldoc_parse_para(node, tabs, posttabs, buffer)
  1138. || xmldoc_parse_specialtags(node, tabs, posttabs, buffer)
  1139. || xmldoc_parse_info(node, tabs, posttabs, buffer));
  1140. }
  1141. /*!
  1142. * \internal
  1143. * \brief Parse a \<para\> element.
  1144. *
  1145. * \param node The \<para\> element pointer.
  1146. * \param tabs Add this string before the content of the \<para\> element.
  1147. * \param posttabs Added this string after the content of the \<para\> element.
  1148. * \param buffer This must be an already allocated ast_str. It will be used
  1149. * to store the result (if already has something it will be appended to the current
  1150. * string).
  1151. *
  1152. * \retval 1 If 'node' is a named 'para'.
  1153. * \retval 2 If data is appended in buffer.
  1154. * \retval 0 on error.
  1155. */
  1156. static int xmldoc_parse_para(struct ast_xml_node *node, const char *tabs, const char *posttabs, struct ast_str **buffer)
  1157. {
  1158. const char *tmptext;
  1159. struct ast_xml_node *tmp;
  1160. int ret = 0;
  1161. struct ast_str *tmpstr;
  1162. if (!node || !ast_xml_node_get_children(node)) {
  1163. return ret;
  1164. }
  1165. if (strcasecmp(ast_xml_node_get_name(node), "para")) {
  1166. return ret;
  1167. }
  1168. ast_str_append(buffer, 0, "%s", tabs);
  1169. ret = 1;
  1170. for (tmp = ast_xml_node_get_children(node); tmp; tmp = ast_xml_node_get_next(tmp)) {
  1171. /* Get the text inside the <para> element and append it to buffer. */
  1172. tmptext = ast_xml_get_text(tmp);
  1173. if (tmptext) {
  1174. /* Strip \n etc. */
  1175. xmldoc_string_cleanup(tmptext, &tmpstr, 0, 0);
  1176. ast_xml_free_text(tmptext);
  1177. if (tmpstr) {
  1178. if (strcasecmp(ast_xml_node_get_name(tmp), "text")) {
  1179. ast_str_append(buffer, 0, "<%s>%s</%s>", ast_xml_node_get_name(tmp),
  1180. ast_str_buffer(tmpstr), ast_xml_node_get_name(tmp));
  1181. } else {
  1182. ast_str_append(buffer, 0, "%s", ast_str_buffer(tmpstr));
  1183. }
  1184. ast_free(tmpstr);
  1185. ret = 2;
  1186. }
  1187. }
  1188. }
  1189. ast_str_append(buffer, 0, "%s", posttabs);
  1190. return ret;
  1191. }
  1192. /*!
  1193. * \internal
  1194. * \brief Parse an \<example\> node.
  1195. * \since 13.0.0
  1196. *
  1197. * \param fixnode An ast xml pointer to the \<example\> node.
  1198. * \param buffer The output buffer.
  1199. *
  1200. * \retval 0 if no example node is parsed.
  1201. * \retval 1 if an example node is parsed.
  1202. */
  1203. static int xmldoc_parse_example(struct ast_xml_node *fixnode, struct ast_str **buffer)
  1204. {
  1205. struct ast_xml_node *node = fixnode;
  1206. const char *tmptext;
  1207. const char *title;
  1208. struct ast_str *stripped_text;
  1209. int ret = 0;
  1210. if (!node || !ast_xml_node_get_children(node)) {
  1211. return ret;
  1212. }
  1213. if (strcasecmp(ast_xml_node_get_name(node), "example")) {
  1214. return ret;
  1215. }
  1216. ret = 1;
  1217. title = ast_xml_get_attribute(node, "title");
  1218. if (title) {
  1219. ast_str_append(buffer, 0, "%s", title);
  1220. ast_xml_free_attr(title);
  1221. }
  1222. ast_str_append(buffer, 0, "\n");
  1223. for (node = ast_xml_node_get_children(node); node; node = ast_xml_node_get_next(node)) {
  1224. tmptext = ast_xml_get_text(node);
  1225. if (tmptext) {
  1226. xmldoc_string_cleanup(tmptext, &stripped_text, 0, 1);
  1227. if (stripped_text) {
  1228. ast_str_append(buffer, 0, "<exampletext>%s</exampletext>\n", ast_str_buffer(stripped_text));
  1229. ast_xml_free_text(tmptext);
  1230. ast_free(stripped_text);
  1231. }
  1232. }
  1233. }
  1234. return ret;
  1235. }
  1236. /*!
  1237. * \internal
  1238. * \brief Parse special elements defined in 'struct special_tags' special elements must have a \<para\> element inside them.
  1239. *
  1240. * \param fixnode special tag node pointer.
  1241. * \param tabs put tabs before printing the node content.
  1242. * \param posttabs put posttabs after printing node content.
  1243. * \param buffer Output buffer, the special tags will be appended here.
  1244. *
  1245. * \retval 0 if no special element is parsed.
  1246. * \retval 1 if a special element is parsed (data is appended to buffer).
  1247. * \retval 2 if a special element is parsed and also a \<para\> element is parsed inside the specialtag.
  1248. */
  1249. static int xmldoc_parse_specialtags(struct ast_xml_node *fixnode, const char *tabs, const char *posttabs, struct ast_str **buffer)
  1250. {
  1251. struct ast_xml_node *node = fixnode;
  1252. int ret = 0, i;
  1253. if (!node || !ast_xml_node_get_children(node)) {
  1254. return ret;
  1255. }
  1256. for (i = 0; i < ARRAY_LEN(special_tags); i++) {
  1257. if (strcasecmp(ast_xml_node_get_name(node), special_tags[i].tagname)) {
  1258. continue;
  1259. }
  1260. ret = 1;
  1261. /* This is a special tag. */
  1262. /* concat data */
  1263. if (!ast_strlen_zero(special_tags[i].init)) {
  1264. ast_str_append(buffer, 0, "%s%s", tabs, special_tags[i].init);
  1265. }
  1266. if (xmldoc_parse_example(node, buffer)) {
  1267. ret = 1;
  1268. break;
  1269. }
  1270. /* parse <para> elements inside special tags. */
  1271. for (node = ast_xml_node_get_children(node); node; node = ast_xml_node_get_next(node)) {
  1272. /* first <para> just print it without tabs at the begining. */
  1273. if ((xmldoc_parse_para(node, "", posttabs, buffer) == 2)
  1274. || (xmldoc_parse_info(node, "", posttabs, buffer) == 2)) {
  1275. ret = 2;
  1276. }
  1277. }
  1278. if (!ast_strlen_zero(special_tags[i].end)) {
  1279. ast_str_append(buffer, 0, "%s%s", special_tags[i].end, posttabs);
  1280. }
  1281. break;
  1282. }
  1283. return ret;
  1284. }
  1285. /*!
  1286. * \internal
  1287. * \brief Parse an \<argument\> element from the xml documentation.
  1288. *
  1289. * \param fixnode Pointer to the 'argument' xml node.
  1290. * \param insideparameter If we are parsing an \<argument\> inside a \<parameter\>.
  1291. * \param paramtabs pre tabs if we are inside a parameter element.
  1292. * \param tabs What to be printed before the argument name.
  1293. * \param buffer Output buffer to put values found inside the \<argument\> element.
  1294. *
  1295. * \retval 1 If there is content inside the argument.
  1296. * \retval 0 If the argument element is not parsed, or there is no content inside it.
  1297. */
  1298. static int xmldoc_parse_argument(struct ast_xml_node *fixnode, int insideparameter, const char *paramtabs, const char *tabs, struct ast_str **buffer)
  1299. {
  1300. struct ast_xml_node *node = fixnode;
  1301. const char *argname;
  1302. int count = 0, ret = 0;
  1303. if (!node || !ast_xml_node_get_children(node)) {
  1304. return ret;
  1305. }
  1306. /* Print the argument names */
  1307. argname = ast_xml_get_attribute(node, "name");
  1308. if (!argname) {
  1309. return 0;
  1310. }
  1311. if (xmldoc_has_inside(node, "para") || xmldoc_has_inside(node, "info") || xmldoc_has_specialtags(node)) {
  1312. ast_str_append(buffer, 0, "%s%s%s", tabs, argname, (insideparameter ? "\n" : ""));
  1313. ast_xml_free_attr(argname);
  1314. } else {
  1315. ast_xml_free_attr(argname);
  1316. return 0;
  1317. }
  1318. for (node = ast_xml_node_get_children(node); node; node = ast_xml_node_get_next(node)) {
  1319. if (xmldoc_parse_common_elements(node, (insideparameter ? paramtabs : (!count ? " - " : tabs)), "\n", buffer) == 2) {
  1320. count++;
  1321. ret = 1;
  1322. }
  1323. }
  1324. return ret;
  1325. }
  1326. /*!
  1327. * \internal
  1328. * \brief Parse a \<variable\> node inside a \<variablelist\> node.
  1329. *
  1330. * \param node The variable node to parse.
  1331. * \param tabs A string to be appended at the begining of the output that will be stored
  1332. * in buffer.
  1333. * \param buffer This must be an already created ast_str. It will be used
  1334. * to store the result (if already has something it will be appended to the current
  1335. * string).
  1336. *
  1337. * \retval 0 if no data is appended.
  1338. * \retval 1 if data is appended.
  1339. */
  1340. static int xmldoc_parse_variable(struct ast_xml_node *node, const char *tabs, struct ast_str **buffer)
  1341. {
  1342. struct ast_xml_node *tmp;
  1343. const char *valname;
  1344. const char *tmptext;
  1345. struct ast_str *cleanstr;
  1346. int ret = 0, printedpara=0;
  1347. for (tmp = ast_xml_node_get_children(node); tmp; tmp = ast_xml_node_get_next(tmp)) {
  1348. if (xmldoc_parse_common_elements(tmp, (ret ? tabs : ""), "\n", buffer)) {
  1349. printedpara = 1;
  1350. continue;
  1351. }
  1352. if (strcasecmp(ast_xml_node_get_name(tmp), "value")) {
  1353. continue;
  1354. }
  1355. /* Parse a <value> tag only. */
  1356. if (!printedpara) {
  1357. ast_str_append(buffer, 0, "\n");
  1358. printedpara = 1;
  1359. }
  1360. /* Parse each <value name='valuename'>desciption</value> */
  1361. valname = ast_xml_get_attribute(tmp, "name");
  1362. if (valname) {
  1363. ret = 1;
  1364. ast_str_append(buffer, 0, "%s<value>%s</value>", tabs, valname);
  1365. ast_xml_free_attr(valname);
  1366. }
  1367. tmptext = ast_xml_get_text(tmp);
  1368. /* Check inside this node for any explanation about its meaning. */
  1369. if (tmptext) {
  1370. /* Cleanup text. */
  1371. xmldoc_string_cleanup(tmptext, &cleanstr, 1, 0);
  1372. ast_xml_free_text(tmptext);
  1373. if (cleanstr && ast_str_strlen(cleanstr) > 0) {
  1374. ast_str_append(buffer, 0, ":%s", ast_str_buffer(cleanstr));
  1375. }
  1376. ast_free(cleanstr);
  1377. }
  1378. ast_str_append(buffer, 0, "\n");
  1379. }
  1380. return ret;
  1381. }
  1382. /*!
  1383. * \internal
  1384. * \brief Parse a \<variablelist\> node and put all the output inside 'buffer'.
  1385. *
  1386. * \param node The variablelist node pointer.
  1387. * \param tabs A string to be appended at the begining of the output that will be stored
  1388. * in buffer.
  1389. * \param buffer This must be an already created ast_str. It will be used
  1390. * to store the result (if already has something it will be appended to the current
  1391. * string).
  1392. *
  1393. * \retval 1 If a \<variablelist\> element is parsed.
  1394. * \retval 0 On error.
  1395. */
  1396. static int xmldoc_parse_variablelist(struct ast_xml_node *node, const char *tabs, struct ast_str **buffer)
  1397. {
  1398. struct ast_xml_node *tmp;
  1399. const char *varname;
  1400. char *vartabs;
  1401. int ret = 0;
  1402. if (!node || !ast_xml_node_get_children(node)) {
  1403. return ret;
  1404. }
  1405. if (strcasecmp(ast_xml_node_get_name(node), "variablelist")) {
  1406. return ret;
  1407. }
  1408. /* use this spacing (add 4 spaces) inside a variablelist node. */
  1409. if (ast_asprintf(&vartabs, "%s ", tabs) < 0) {
  1410. return ret;
  1411. }
  1412. for (tmp = ast_xml_node_get_children(node); tmp; tmp = ast_xml_node_get_next(tmp)) {
  1413. /* We can have a <para> element inside the variable list */
  1414. if (xmldoc_parse_common_elements(tmp, (ret ? tabs : ""), "\n", buffer)) {
  1415. ret = 1;
  1416. continue;
  1417. }
  1418. if (!strcasecmp(ast_xml_node_get_name(tmp), "variable")) {
  1419. /* Store the variable name in buffer. */
  1420. varname = ast_xml_get_attribute(tmp, "name");
  1421. if (varname) {
  1422. ast_str_append(buffer, 0, "%s<variable>%s</variable>: ", tabs, varname);
  1423. ast_xml_free_attr(varname);
  1424. /* Parse the <variable> possible values. */
  1425. xmldoc_parse_variable(tmp, vartabs, buffer);
  1426. ret = 1;
  1427. }
  1428. }
  1429. }
  1430. ast_free(vartabs);
  1431. return ret;
  1432. }
  1433. /*!
  1434. * \internal
  1435. * \brief Build seealso information for an item
  1436. *
  1437. * \param node The seealso node to parse
  1438. *
  1439. * \note This method exists for when you already have the node. This
  1440. * prevents having to lock the documentation tree twice
  1441. *
  1442. * \retval A malloc'd character pointer to the seealso information of the item
  1443. * \retval NULL on failure
  1444. *
  1445. * \since 11
  1446. */
  1447. static char *_ast_xmldoc_build_seealso(struct ast_xml_node *node)
  1448. {
  1449. char *output;
  1450. struct ast_str *outputstr;
  1451. const char *typename;
  1452. const char *content;
  1453. int first = 1;
  1454. /* Find the <see-also> node. */
  1455. for (node = ast_xml_node_get_children(node); node; node = ast_xml_node_get_next(node)) {
  1456. if (!strcasecmp(ast_xml_node_get_name(node), "see-also")) {
  1457. break;
  1458. }
  1459. }
  1460. if (!node || !ast_xml_node_get_children(node)) {
  1461. /* we couldnt find a <see-also> node. */
  1462. return NULL;
  1463. }
  1464. /* prepare the output string. */
  1465. outputstr = ast_str_create(128);
  1466. if (!outputstr) {
  1467. return NULL;
  1468. }
  1469. /* get into the <see-also> node. */
  1470. for (node = ast_xml_node_get_children(node); node; node = ast_xml_node_get_next(node)) {
  1471. if (strcasecmp(ast_xml_node_get_name(node), "ref")) {
  1472. continue;
  1473. }
  1474. /* parse the <ref> node. 'type' attribute is required. */
  1475. typename = ast_xml_get_attribute(node, "type");
  1476. if (!typename) {
  1477. continue;
  1478. }
  1479. content = ast_xml_get_text(node);
  1480. if (!content) {
  1481. ast_xml_free_attr(typename);
  1482. continue;
  1483. }
  1484. if (!strcasecmp(typename, "application")) {
  1485. ast_str_append(&outputstr, 0, "%s%s()", (first ? "" : ", "), content);
  1486. } else if (!strcasecmp(typename, "function")) {
  1487. ast_str_append(&outputstr, 0, "%s%s", (first ? "" : ", "), content);
  1488. } else if (!strcasecmp(typename, "astcli")) {
  1489. ast_str_append(&outputstr, 0, "%s<astcli>%s</astcli>", (first ? "" : ", "), content);
  1490. } else {
  1491. ast_str_append(&outputstr, 0, "%s%s", (first ? "" : ", "), content);
  1492. }
  1493. first = 0;
  1494. ast_xml_free_text(content);
  1495. ast_xml_free_attr(typename);
  1496. }
  1497. output = ast_strdup(ast_str_buffer(outputstr));
  1498. ast_free(outputstr);
  1499. return output;
  1500. }
  1501. char *ast_xmldoc_build_seealso(const char *type, const char *name, const char *module)
  1502. {
  1503. char *output;
  1504. struct ast_xml_node *node;
  1505. if (ast_strlen_zero(type) || ast_strlen_zero(name)) {
  1506. return NULL;
  1507. }
  1508. /* get the application/function root node. */
  1509. AST_RWLIST_RDLOCK(&xmldoc_tree);
  1510. node = xmldoc_get_node(type, name, module, documentation_language);
  1511. if (!node || !ast_xml_node_get_children(node)) {
  1512. AST_RWLIST_UNLOCK(&xmldoc_tree);
  1513. return NULL;
  1514. }
  1515. output = _ast_xmldoc_build_seealso(node);
  1516. AST_RWLIST_UNLOCK(&xmldoc_tree);
  1517. return output;
  1518. }
  1519. /*!
  1520. * \internal
  1521. * \brief Parse a \<enum\> node.
  1522. *
  1523. * \param fixnode An ast_xml_node pointer to the \<enum\> node.
  1524. * \param tabs Add this string before the content of the \<enum\> node.
  1525. * \param buffer The output buffer.
  1526. *
  1527. * \retval 0 if content is not found inside the enum element (data is not appended to buffer).
  1528. * \retval 1 if content is found and data is appended to buffer.
  1529. */
  1530. static int xmldoc_parse_enum(struct ast_xml_node *fixnode, const char *tabs, struct ast_str **buffer)
  1531. {
  1532. struct ast_xml_node *node = fixnode;
  1533. int ret = 0;
  1534. char *optiontabs;
  1535. if (ast_asprintf(&optiontabs, "%s ", tabs) < 0) {
  1536. return ret;
  1537. }
  1538. for (node = ast_xml_node_get_children(node); node; node = ast_xml_node_get_next(node)) {
  1539. if (xmldoc_parse_common_elements(node, (ret ? tabs : " - "), "\n", buffer)) {
  1540. ret = 1;
  1541. }
  1542. xmldoc_parse_enumlist(node, optiontabs, buffer);
  1543. xmldoc_parse_parameter(node, optiontabs, buffer);
  1544. }
  1545. ast_free(optiontabs);
  1546. return ret;
  1547. }
  1548. /*!
  1549. * \internal
  1550. * \brief Parse a \<enumlist\> node.
  1551. *
  1552. * \param fixnode As ast_xml pointer to the \<enumlist\> node.
  1553. * \param tabs Add this string before the content of the \<enumlist\> node.
  1554. * \param buffer The ast_str output buffer.
  1555. *
  1556. * \retval 0 if no \<enumlist\> node was parsed.
  1557. * \retval 1 if a \<enumlist\> node was parsed.
  1558. */
  1559. static int xmldoc_parse_enumlist(struct ast_xml_node *fixnode, const char *tabs, struct ast_str **buffer)
  1560. {
  1561. struct ast_xml_node *node = fixnode;
  1562. const char *enumname;
  1563. int ret = 0;
  1564. for (node = ast_xml_node_get_children(node); node; node = ast_xml_node_get_next(node)) {
  1565. if (strcasecmp(ast_xml_node_get_name(node), "enum")) {
  1566. continue;
  1567. }
  1568. enumname = ast_xml_get_attribute(node, "name");
  1569. if (enumname) {
  1570. ast_str_append(buffer, 0, "%s<enum>%s</enum>", tabs, enumname);
  1571. ast_xml_free_attr(enumname);
  1572. /* parse only enum elements inside a enumlist node. */
  1573. if ((xmldoc_parse_enum(node, tabs, buffer))) {
  1574. ret = 1;
  1575. } else {
  1576. ast_str_append(buffer, 0, "\n");
  1577. }
  1578. }
  1579. }
  1580. return ret;
  1581. }
  1582. /*!
  1583. * \internal
  1584. * \brief Parse an \<option\> node.
  1585. *
  1586. * \param fixnode An ast_xml pointer to the \<option\> node.
  1587. * \param tabs A string to be appended at the begining of each line being added to the
  1588. * buffer string.
  1589. * \param buffer The output buffer.
  1590. *
  1591. * \retval 0 if no option node is parsed.
  1592. * \retval 1 if an option node is parsed.
  1593. */
  1594. static int xmldoc_parse_option(struct ast_xml_node *fixnode, const char *tabs, struct ast_str **buffer)
  1595. {
  1596. struct ast_xml_node *node;
  1597. int ret = 0;
  1598. char *optiontabs;
  1599. if (ast_asprintf(&optiontabs, "%s ", tabs) < 0) {
  1600. return ret;
  1601. }
  1602. for (node = ast_xml_node_get_children(fixnode); node; node = ast_xml_node_get_next(node)) {
  1603. if (!strcasecmp(ast_xml_node_get_name(node), "argument")) {
  1604. /* if this is the first data appended to buffer, print a \n*/
  1605. if (!ret && ast_xml_node_get_children(node)) {
  1606. /* print \n */
  1607. ast_str_append(buffer, 0, "\n");
  1608. }
  1609. if (xmldoc_parse_argument(node, 0, NULL, optiontabs, buffer)) {
  1610. ret = 1;
  1611. }
  1612. continue;
  1613. }
  1614. if (xmldoc_parse_common_elements(node, (ret ? tabs : ""), "\n", buffer)) {
  1615. ret = 1;
  1616. }
  1617. xmldoc_parse_variablelist(node, optiontabs, buffer);
  1618. xmldoc_parse_enumlist(node, optiontabs, buffer);
  1619. }
  1620. ast_free(optiontabs);
  1621. return ret;
  1622. }
  1623. /*!
  1624. * \internal
  1625. * \brief Parse an \<optionlist\> element from the xml documentation.
  1626. *
  1627. * \param fixnode Pointer to the optionlist xml node.
  1628. * \param tabs A string to be appended at the begining of each line being added to the
  1629. * buffer string.
  1630. * \param buffer Output buffer to put what is inside the optionlist tag.
  1631. */
  1632. static void xmldoc_parse_optionlist(struct ast_xml_node *fixnode, const char *tabs, struct ast_str **buffer)
  1633. {
  1634. struct ast_xml_node *node;
  1635. const char *optname, *hasparams;
  1636. char *optionsyntax;
  1637. int optparams;
  1638. for (node = ast_xml_node_get_children(fixnode); node; node = ast_xml_node_get_next(node)) {
  1639. /* Start appending every option tag. */
  1640. if (strcasecmp(ast_xml_node_get_name(node), "option")) {
  1641. continue;
  1642. }
  1643. /* Get the option name. */
  1644. optname = ast_xml_get_attribute(node, "name");
  1645. if (!optname) {
  1646. continue;
  1647. }
  1648. optparams = 1;
  1649. hasparams = ast_xml_get_attribute(node, "hasparams");
  1650. if (hasparams && !strcasecmp(hasparams, "optional")) {
  1651. optparams = 2;
  1652. }
  1653. optionsyntax = xmldoc_get_syntax_fun(node, optname, "argument", 0, optparams);
  1654. if (!optionsyntax) {
  1655. ast_xml_free_attr(optname);
  1656. ast_xml_free_attr(hasparams);
  1657. continue;
  1658. }
  1659. ast_str_append(buffer, 0, "%s%s: ", tabs, optionsyntax);
  1660. if (!xmldoc_parse_option(node, tabs, buffer)) {
  1661. ast_str_append(buffer, 0, "\n");
  1662. }
  1663. ast_str_append(buffer, 0, "\n");
  1664. ast_xml_free_attr(optname);
  1665. ast_xml_free_attr(hasparams);
  1666. ast_free(optionsyntax);
  1667. }
  1668. }
  1669. /*!
  1670. * \internal
  1671. * \brief Parse a 'parameter' tag inside a syntax element.
  1672. *
  1673. * \param fixnode A pointer to the 'parameter' xml node.
  1674. * \param tabs A string to be appended at the beginning of each line being printed inside
  1675. * 'buffer'.
  1676. * \param buffer String buffer to put values found inside the parameter element.
  1677. */
  1678. static void xmldoc_parse_parameter(struct ast_xml_node *fixnode, const char *tabs, struct ast_str **buffer)
  1679. {
  1680. const char *paramname;
  1681. struct ast_xml_node *node = fixnode;
  1682. int hasarguments, printed = 0;
  1683. char *internaltabs;
  1684. if (strcasecmp(ast_xml_node_get_name(node), "parameter")) {
  1685. return;
  1686. }
  1687. hasarguments = xmldoc_has_inside(node, "argument");
  1688. if (!(paramname = ast_xml_get_attribute(node, "name"))) {
  1689. /* parameter MUST have an attribute name. */
  1690. return;
  1691. }
  1692. if (ast_asprintf(&internaltabs, "%s ", tabs) < 0) {
  1693. ast_xml_free_attr(paramname);
  1694. return;
  1695. }
  1696. if (!hasarguments && xmldoc_has_nodes(node)) {
  1697. ast_str_append(buffer, 0, "%s\n", paramname);
  1698. ast_xml_free_attr(paramname);
  1699. printed = 1;
  1700. }
  1701. for (node = ast_xml_node_get_children(node); node; node = ast_xml_node_get_next(node)) {
  1702. if (!strcasecmp(ast_xml_node_get_name(node), "optionlist")) {
  1703. xmldoc_parse_optionlist(node, internaltabs, buffer);
  1704. } else if (!strcasecmp(ast_xml_node_get_name(node), "enumlist")) {
  1705. xmldoc_parse_enumlist(node, internaltabs, buffer);
  1706. } else if (!strcasecmp(ast_xml_node_get_name(node), "argument")) {
  1707. xmldoc_parse_argument(node, 1, internaltabs, (!hasarguments ? " " : ""), buffer);
  1708. } else if (!strcasecmp(ast_xml_node_get_name(node), "para")) {
  1709. if (!printed) {
  1710. ast_str_append(buffer, 0, "%s\n", paramname);
  1711. ast_xml_free_attr(paramname);
  1712. printed = 1;
  1713. }
  1714. if (xmldoc_parse_para(node, internaltabs, "\n", buffer)) {
  1715. /* If anything ever goes in below this condition before the continue below,
  1716. * we should probably continue immediately. */
  1717. continue;
  1718. }
  1719. continue;
  1720. } else if (!strcasecmp(ast_xml_node_get_name(node), "info")) {
  1721. if (!printed) {
  1722. ast_str_append(buffer, 0, "%s\n", paramname);
  1723. ast_xml_free_attr(paramname);
  1724. printed = 1;
  1725. }
  1726. if (xmldoc_parse_info(node, internaltabs, "\n", buffer)) {
  1727. /* If anything ever goes in below this condition before the continue below,
  1728. * we should probably continue immediately. */
  1729. continue;
  1730. }
  1731. continue;
  1732. } else if ((xmldoc_parse_specialtags(node, internaltabs, "\n", buffer))) {
  1733. continue;
  1734. }
  1735. }
  1736. if (!printed) {
  1737. ast_xml_free_attr(paramname);
  1738. }
  1739. ast_free(internaltabs);
  1740. }
  1741. /*!
  1742. * \internal
  1743. * \brief Parse an 'info' tag inside an element.
  1744. *
  1745. * \param node A pointer to the 'info' xml node.
  1746. * \param tabs A string to be appended at the beginning of each line being printed
  1747. * inside 'buffer'.
  1748. * \param posttabs Add this string after the content of the \<para\> element, if one exists
  1749. * \param buffer String buffer to put values found inide the info element.
  1750. *
  1751. * \retval 2 if the information contained a para element, and it returned a value of 2
  1752. * \retval 1 if information was put into the buffer
  1753. * \retval 0 if no information was put into the buffer or error
  1754. */
  1755. static int xmldoc_parse_info(struct ast_xml_node *node, const char *tabs, const char *posttabs, struct ast_str **buffer)
  1756. {
  1757. const char *tech;
  1758. char *internaltabs;
  1759. int internal_ret;
  1760. int ret = 0;
  1761. if (strcasecmp(ast_xml_node_get_name(node), "info")) {
  1762. return ret;
  1763. }
  1764. ast_asprintf(&internaltabs, "%s ", tabs);
  1765. if (!internaltabs) {
  1766. return ret;
  1767. }
  1768. tech = ast_xml_get_attribute(node, "tech");
  1769. if (tech) {
  1770. ast_str_append(buffer, 0, "%s<note>Technology: %s</note>\n", internaltabs, tech);
  1771. ast_xml_free_attr(tech);
  1772. }
  1773. ret = 1;
  1774. for (node = ast_xml_node_get_children(node); node; node = ast_xml_node_get_next(node)) {
  1775. if (!strcasecmp(ast_xml_node_get_name(node), "enumlist")) {
  1776. xmldoc_parse_enumlist(node, internaltabs, buffer);
  1777. } else if (!strcasecmp(ast_xml_node_get_name(node), "parameter")) {
  1778. xmldoc_parse_parameter(node, internaltabs, buffer);
  1779. } else if ((internal_ret = xmldoc_parse_common_elements(node, internaltabs, posttabs, buffer))) {
  1780. if (internal_ret > ret) {
  1781. ret = internal_ret;
  1782. }
  1783. }
  1784. }
  1785. ast_free(internaltabs);
  1786. return ret;
  1787. }
  1788. /*!
  1789. * \internal
  1790. * \brief Build the arguments for an item
  1791. *
  1792. * \param node The arguments node to parse
  1793. *
  1794. * \note This method exists for when you already have the node. This
  1795. * prevents having to lock the documentation tree twice
  1796. *
  1797. * \retval A malloc'd character pointer to the arguments for the item
  1798. * \retval NULL on failure
  1799. *
  1800. * \since 11
  1801. */
  1802. static char *_ast_xmldoc_build_arguments(struct ast_xml_node *node)
  1803. {
  1804. char *retstr = NULL;
  1805. struct ast_str *ret;
  1806. ret = ast_str_create(128);
  1807. if (!ret) {
  1808. return NULL;
  1809. }
  1810. /* Find the syntax field. */
  1811. for (node = ast_xml_node_get_children(node); node; node = ast_xml_node_get_next(node)) {
  1812. if (!strcasecmp(ast_xml_node_get_name(node), "syntax")) {
  1813. break;
  1814. }
  1815. }
  1816. if (!node || !ast_xml_node_get_children(node)) {
  1817. /* We couldn't find the syntax node. */
  1818. ast_free(ret);
  1819. return NULL;
  1820. }
  1821. for (node = ast_xml_node_get_children(node); node; node = ast_xml_node_get_next(node)) {
  1822. xmldoc_parse_parameter(node, "", &ret);
  1823. }
  1824. if (ast_str_strlen(ret) > 0) {
  1825. /* remove last '\n' */
  1826. char *buf = ast_str_buffer(ret);
  1827. if (buf[ast_str_strlen(ret) - 1] == '\n') {
  1828. ast_str_truncate(ret, -1);
  1829. }
  1830. retstr = ast_strdup(ast_str_buffer(ret));
  1831. }
  1832. ast_free(ret);
  1833. return retstr;
  1834. }
  1835. char *ast_xmldoc_build_arguments(const char *type, const char *name, const char *module)
  1836. {
  1837. struct ast_xml_node *node;
  1838. char *arguments;
  1839. if (ast_strlen_zero(type) || ast_strlen_zero(name)) {
  1840. return NULL;
  1841. }
  1842. AST_RWLIST_RDLOCK(&xmldoc_tree);
  1843. node = xmldoc_get_node(type, name, module, documentation_language);
  1844. if (!node || !ast_xml_node_get_children(node)) {
  1845. AST_RWLIST_UNLOCK(&xmldoc_tree);
  1846. return NULL;
  1847. }
  1848. arguments = _ast_xmldoc_build_arguments(node);
  1849. AST_RWLIST_UNLOCK(&xmldoc_tree);
  1850. return arguments;
  1851. }
  1852. /*!
  1853. * \internal
  1854. * \brief Return the string within a node formatted with \<para\> and \<variablelist\> elements.
  1855. *
  1856. * \param node Parent node where content resides.
  1857. * \param raw_output If set, return the node's content without further processing.
  1858. * \param raw_wrap Wrap raw text.
  1859. *
  1860. * \retval NULL on error
  1861. * \retval Node content on success.
  1862. */
  1863. static struct ast_str *xmldoc_get_formatted(struct ast_xml_node *node, int raw_output, int raw_wrap)
  1864. {
  1865. struct ast_xml_node *tmp;
  1866. const char *notcleanret, *tmpstr;
  1867. struct ast_str *ret;
  1868. if (raw_output) {
  1869. /* xmldoc_string_cleanup will allocate the ret object */
  1870. notcleanret = ast_xml_get_text(node);
  1871. tmpstr = notcleanret;
  1872. xmldoc_string_cleanup(ast_skip_blanks(notcleanret), &ret, 0, 0);
  1873. ast_xml_free_text(tmpstr);
  1874. } else {
  1875. ret = ast_str_create(128);
  1876. if (!ret) {
  1877. return NULL;
  1878. }
  1879. for (tmp = ast_xml_node_get_children(node); tmp; tmp = ast_xml_node_get_next(tmp)) {
  1880. /* if found, parse children elements. */
  1881. if (xmldoc_parse_common_elements(tmp, "", "\n", &ret)) {
  1882. continue;
  1883. }
  1884. if (xmldoc_parse_variablelist(tmp, "", &ret)) {
  1885. continue;
  1886. }
  1887. if (xmldoc_parse_enumlist(tmp, " ", &ret)) {
  1888. continue;
  1889. }
  1890. if (xmldoc_parse_specialtags(tmp, "", "", &ret)) {
  1891. continue;
  1892. }
  1893. }
  1894. /* remove last '\n' */
  1895. /* XXX Don't modify ast_str internals manually */
  1896. tmpstr = ast_str_buffer(ret);
  1897. if (tmpstr[ast_str_strlen(ret) - 1] == '\n') {
  1898. ast_str_truncate(ret, -1);
  1899. }
  1900. }
  1901. return ret;
  1902. }
  1903. /*!
  1904. * \internal
  1905. * \brief Get the content of a field (synopsis, description, etc) from an asterisk document tree node
  1906. *
  1907. * \param node The node to obtain the information from
  1908. * \param var Name of field to return (synopsis, description, etc).
  1909. * \param raw Field only contains text, no other elements inside it.
  1910. *
  1911. * \retval NULL On error.
  1912. * \retval Field text content on success.
  1913. * \since 11
  1914. */
  1915. static char *_xmldoc_build_field(struct ast_xml_node *node, const char *var, int raw)
  1916. {
  1917. char *ret = NULL;
  1918. struct ast_str *formatted;
  1919. node = ast_xml_find_element(ast_xml_node_get_children(node), var, NULL, NULL);
  1920. if (!node || !ast_xml_node_get_children(node)) {
  1921. return ret;
  1922. }
  1923. formatted = xmldoc_get_formatted(node, raw, raw);
  1924. if (formatted && ast_str_strlen(formatted) > 0) {
  1925. ret = ast_strdup(ast_str_buffer(formatted));
  1926. }
  1927. ast_free(formatted);
  1928. return ret;
  1929. }
  1930. /*!
  1931. * \internal
  1932. * \brief Get the content of a field (synopsis, description, etc) from an asterisk document tree
  1933. *
  1934. * \param type Type of element (application, function, ...).
  1935. * \param name Name of element (Dial, Echo, Playback, ...).
  1936. * \param var Name of field to return (synopsis, description, etc).
  1937. * \param module
  1938. * \param raw Field only contains text, no other elements inside it.
  1939. *
  1940. * \retval NULL On error.
  1941. * \retval Field text content on success.
  1942. */
  1943. static char *xmldoc_build_field(const char *type, const char *name, const char *module, const char *var, int raw)
  1944. {
  1945. struct ast_xml_node *node;
  1946. char *field;
  1947. if (ast_strlen_zero(type) || ast_strlen_zero(name)) {
  1948. ast_log(LOG_ERROR, "Tried to look in XML tree with faulty values.\n");
  1949. return NULL;
  1950. }
  1951. AST_RWLIST_RDLOCK(&xmldoc_tree);
  1952. node = xmldoc_get_node(type, name, module, documentation_language);
  1953. if (!node) {
  1954. AST_RWLIST_UNLOCK(&xmldoc_tree);
  1955. ast_log(LOG_WARNING, "Couldn't find %s %s in XML documentation"
  1956. " If this module was recently built, run 'xmldoc reload' to refresh documentation\n",
  1957. type, name);
  1958. return NULL;
  1959. }
  1960. field = _xmldoc_build_field(node, var, raw);
  1961. AST_RWLIST_UNLOCK(&xmldoc_tree);
  1962. return field;
  1963. }
  1964. /*!
  1965. * \internal
  1966. * \brief Build the synopsis for an item
  1967. *
  1968. * \param node The synopsis node
  1969. *
  1970. * \note This method exists for when you already have the node. This
  1971. * prevents having to lock the documentation tree twice
  1972. *
  1973. * \retval A malloc'd character pointer to the synopsis information
  1974. * \retval NULL on failure
  1975. * \since 11
  1976. */
  1977. static char *_ast_xmldoc_build_synopsis(struct ast_xml_node *node)
  1978. {
  1979. return _xmldoc_build_field(node, "synopsis", 1);
  1980. }
  1981. char *ast_xmldoc_build_synopsis(const char *type, const char *name, const char *module)
  1982. {
  1983. return xmldoc_build_field(type, name, module, "synopsis", 1);
  1984. }
  1985. /*!
  1986. * \internal
  1987. * \brief Build the descripton for an item
  1988. *
  1989. * \param node The description node to parse
  1990. *
  1991. * \note This method exists for when you already have the node. This
  1992. * prevents having to lock the documentation tree twice
  1993. *
  1994. * \retval A malloc'd character pointer to the arguments for the item
  1995. * \retval NULL on failure
  1996. * \since 11
  1997. */
  1998. static char *_ast_xmldoc_build_description(struct ast_xml_node *node)
  1999. {
  2000. return _xmldoc_build_field(node, "description", 0);
  2001. }
  2002. char *ast_xmldoc_build_description(const char *type, const char *name, const char *module)
  2003. {
  2004. return xmldoc_build_field(type, name, module, "description", 0);
  2005. }
  2006. /*!
  2007. * \internal
  2008. * \brief ast_xml_doc_item ao2 destructor
  2009. * \since 11
  2010. */
  2011. static void ast_xml_doc_item_destructor(void *obj)
  2012. {
  2013. struct ast_xml_doc_item *doc = obj;
  2014. if (!doc) {
  2015. return;
  2016. }
  2017. ast_free(doc->syntax);
  2018. ast_free(doc->seealso);
  2019. ast_free(doc->arguments);
  2020. ast_free(doc->synopsis);
  2021. ast_free(doc->description);
  2022. ast_string_field_free_memory(doc);
  2023. if (AST_LIST_NEXT(doc, next)) {
  2024. ao2_ref(AST_LIST_NEXT(doc, next), -1);
  2025. AST_LIST_NEXT(doc, next) = NULL;
  2026. }
  2027. }
  2028. /*!
  2029. * \internal
  2030. * \brief Create an ao2 ref counted ast_xml_doc_item
  2031. *
  2032. * \param name The name of the item
  2033. * \param type The item's source type
  2034. * \since 11
  2035. */
  2036. static struct ast_xml_doc_item *ast_xml_doc_item_alloc(const char *name, const char *type)
  2037. {
  2038. struct ast_xml_doc_item *item;
  2039. item = ao2_alloc_options(sizeof(*item), ast_xml_doc_item_destructor,
  2040. AO2_ALLOC_OPT_LOCK_NOLOCK);
  2041. if (!item) {
  2042. ast_log(AST_LOG_ERROR, "Failed to allocate memory for ast_xml_doc_item instance\n");
  2043. return NULL;
  2044. }
  2045. if ( !(item->syntax = ast_str_create(128))
  2046. || !(item->seealso = ast_str_create(128))
  2047. || !(item->arguments = ast_str_create(128))
  2048. || !(item->synopsis = ast_str_create(128))
  2049. || !(item->description = ast_str_create(128))) {
  2050. ast_log(AST_LOG_ERROR, "Failed to allocate strings for ast_xml_doc_item instance\n");
  2051. goto ast_xml_doc_item_failure;
  2052. }
  2053. if (ast_string_field_init(item, 64)) {
  2054. ast_log(AST_LOG_ERROR, "Failed to initialize string field for ast_xml_doc_item instance\n");
  2055. goto ast_xml_doc_item_failure;
  2056. }
  2057. ast_string_field_set(item, name, name);
  2058. ast_string_field_set(item, type, type);
  2059. return item;
  2060. ast_xml_doc_item_failure:
  2061. ao2_ref(item, -1);
  2062. return NULL;
  2063. }
  2064. /*!
  2065. * \internal
  2066. * \brief ao2 item hash function for ast_xml_doc_item
  2067. * \since 11
  2068. */
  2069. static int ast_xml_doc_item_hash(const void *obj, const int flags)
  2070. {
  2071. const struct ast_xml_doc_item *item = obj;
  2072. const char *name = (flags & OBJ_KEY) ? obj : item->name;
  2073. return ast_str_case_hash(name);
  2074. }
  2075. /*!
  2076. * \internal
  2077. * \brief ao2 item comparison function for ast_xml_doc_item
  2078. * \since 11
  2079. */
  2080. static int ast_xml_doc_item_cmp(void *obj, void *arg, int flags)
  2081. {
  2082. struct ast_xml_doc_item *left = obj;
  2083. struct ast_xml_doc_item *right = arg;
  2084. const char *match = (flags & OBJ_KEY) ? arg : right->name;
  2085. return strcasecmp(left->name, match) ? 0 : (CMP_MATCH | CMP_STOP);
  2086. }
  2087. /*!
  2088. * \internal
  2089. * \brief Build an XML documentation item
  2090. *
  2091. * \param node The root node for the item
  2092. * \param name The name of the item
  2093. * \param type The item's source type
  2094. *
  2095. * \retval NULL on failure
  2096. * \retval An ao2 ref counted object
  2097. * \since 11
  2098. */
  2099. static struct ast_xml_doc_item *xmldoc_build_documentation_item(struct ast_xml_node *node, const char *name, const char *type)
  2100. {
  2101. struct ast_xml_doc_item *item;
  2102. char *syntax;
  2103. char *seealso;
  2104. char *arguments;
  2105. char *synopsis;
  2106. char *description;
  2107. if (!(item = ast_xml_doc_item_alloc(name, type))) {
  2108. return NULL;
  2109. }
  2110. item->node = node;
  2111. syntax = _ast_xmldoc_build_syntax(node, type, name);
  2112. seealso = _ast_xmldoc_build_seealso(node);
  2113. arguments = _ast_xmldoc_build_arguments(node);
  2114. synopsis = _ast_xmldoc_build_synopsis(node);
  2115. description = _ast_xmldoc_build_description(node);
  2116. if (syntax) {
  2117. ast_str_set(&item->syntax, 0, "%s", syntax);
  2118. }
  2119. if (seealso) {
  2120. ast_str_set(&item->seealso, 0, "%s", seealso);
  2121. }
  2122. if (arguments) {
  2123. ast_str_set(&item->arguments, 0, "%s", arguments);
  2124. }
  2125. if (synopsis) {
  2126. ast_str_set(&item->synopsis, 0, "%s", synopsis);
  2127. }
  2128. if (description) {
  2129. ast_str_set(&item->description, 0, "%s", description);
  2130. }
  2131. ast_free(syntax);
  2132. ast_free(seealso);
  2133. ast_free(arguments);
  2134. ast_free(synopsis);
  2135. ast_free(description);
  2136. return item;
  2137. }
  2138. /*!
  2139. * \internal
  2140. * \brief Build the list responses for an item
  2141. *
  2142. * \param manager_action The action node to parse
  2143. *
  2144. * \note This method exists for when you already have the node. This
  2145. * prevents having to lock the documentation tree twice
  2146. *
  2147. * \retval A list of ast_xml_doc_items
  2148. * \retval NULL on failure
  2149. *
  2150. * \since 13.0.0
  2151. */
  2152. static struct ast_xml_doc_item *xmldoc_build_list_responses(struct ast_xml_node *manager_action)
  2153. {
  2154. struct ast_xml_node *event;
  2155. struct ast_xml_node *responses;
  2156. struct ast_xml_node *list_elements;
  2157. struct ast_xml_doc_item_list root;
  2158. AST_LIST_HEAD_INIT(&root);
  2159. responses = ast_xml_find_element(ast_xml_node_get_children(manager_action), "responses", NULL, NULL);
  2160. if (!responses) {
  2161. return NULL;
  2162. }
  2163. list_elements = ast_xml_find_element(ast_xml_node_get_children(responses), "list-elements", NULL, NULL);
  2164. if (!list_elements) {
  2165. return NULL;
  2166. }
  2167. /* Iterate over managerEvent nodes */
  2168. for (event = ast_xml_node_get_children(list_elements); event; event = ast_xml_node_get_next(event)) {
  2169. struct ast_xml_node *event_instance;
  2170. RAII_VAR(const char *, name, ast_xml_get_attribute(event, "name"),
  2171. ast_xml_free_attr);
  2172. struct ast_xml_doc_item *new_item;
  2173. if (!name || strcmp(ast_xml_node_get_name(event), "managerEvent")) {
  2174. continue;
  2175. }
  2176. event_instance = ast_xml_find_element(ast_xml_node_get_children(event),
  2177. "managerEventInstance", NULL, NULL);
  2178. new_item = xmldoc_build_documentation_item(event_instance, name, "managerEvent");
  2179. if (!new_item) {
  2180. ao2_cleanup(AST_LIST_FIRST(&root));
  2181. return NULL;
  2182. }
  2183. AST_LIST_INSERT_TAIL(&root, new_item, next);
  2184. }
  2185. return AST_LIST_FIRST(&root);
  2186. }
  2187. struct ast_xml_doc_item *ast_xmldoc_build_list_responses(const char *type, const char *name, const char *module)
  2188. {
  2189. struct ast_xml_node *node;
  2190. struct ast_xml_doc_item *responses;
  2191. if (ast_strlen_zero(type) || ast_strlen_zero(name)) {
  2192. return NULL;
  2193. }
  2194. AST_RWLIST_RDLOCK(&xmldoc_tree);
  2195. node = xmldoc_get_node(type, name, module, documentation_language);
  2196. if (!node || !ast_xml_node_get_children(node)) {
  2197. AST_RWLIST_UNLOCK(&xmldoc_tree);
  2198. return NULL;
  2199. }
  2200. responses = xmldoc_build_list_responses(node);
  2201. AST_RWLIST_UNLOCK(&xmldoc_tree);
  2202. return responses;
  2203. }
  2204. /*!
  2205. * \internal
  2206. * \brief Build the final response for an item
  2207. *
  2208. * \param manager_action The action node to parse
  2209. *
  2210. * \note This method exists for when you already have the node. This
  2211. * prevents having to lock the documentation tree twice
  2212. *
  2213. * \retval An ast_xml_doc_item
  2214. * \retval NULL on failure
  2215. *
  2216. * \since 13.0.0
  2217. */
  2218. static struct ast_xml_doc_item *xmldoc_build_final_response(struct ast_xml_node *manager_action)
  2219. {
  2220. struct ast_xml_node *responses;
  2221. struct ast_xml_node *final_response_event;
  2222. struct ast_xml_node *event_instance;
  2223. responses = ast_xml_find_element(ast_xml_node_get_children(manager_action),
  2224. "responses", NULL, NULL);
  2225. if (!responses) {
  2226. return NULL;
  2227. }
  2228. final_response_event = ast_xml_find_element(ast_xml_node_get_children(responses),
  2229. "managerEvent", NULL, NULL);
  2230. if (!final_response_event) {
  2231. return NULL;
  2232. }
  2233. event_instance = ast_xml_find_element(ast_xml_node_get_children(final_response_event),
  2234. "managerEventInstance", NULL, NULL);
  2235. if (!event_instance) {
  2236. return NULL;
  2237. } else {
  2238. const char *name;
  2239. struct ast_xml_doc_item *res;
  2240. name = ast_xml_get_attribute(final_response_event, "name");
  2241. res = xmldoc_build_documentation_item(event_instance, name, "managerEvent");
  2242. ast_xml_free_attr(name);
  2243. return res;
  2244. }
  2245. }
  2246. struct ast_xml_doc_item *ast_xmldoc_build_final_response(const char *type, const char *name, const char *module)
  2247. {
  2248. struct ast_xml_node *node;
  2249. static struct ast_xml_doc_item *response;
  2250. if (ast_strlen_zero(type) || ast_strlen_zero(name)) {
  2251. return NULL;
  2252. }
  2253. AST_RWLIST_RDLOCK(&xmldoc_tree);
  2254. node = xmldoc_get_node(type, name, module, documentation_language);
  2255. if (!node || !ast_xml_node_get_children(node)) {
  2256. AST_RWLIST_UNLOCK(&xmldoc_tree);
  2257. return NULL;
  2258. }
  2259. response = xmldoc_build_final_response(node);
  2260. AST_RWLIST_UNLOCK(&xmldoc_tree);
  2261. return response;
  2262. }
  2263. struct ast_xml_xpath_results *__attribute__((format(printf, 1, 2))) ast_xmldoc_query(const char *fmt, ...)
  2264. {
  2265. struct ast_xml_xpath_results *results = NULL;
  2266. struct documentation_tree *doctree;
  2267. RAII_VAR(struct ast_str *, xpath_str, ast_str_create(128), ast_free);
  2268. va_list ap;
  2269. int res;
  2270. if (!xpath_str) {
  2271. return NULL;
  2272. }
  2273. va_start(ap, fmt);
  2274. res = ast_str_set_va(&xpath_str, 0, fmt, ap);
  2275. va_end(ap);
  2276. if (res == AST_DYNSTR_BUILD_FAILED) {
  2277. return NULL;
  2278. }
  2279. AST_RWLIST_RDLOCK(&xmldoc_tree);
  2280. AST_LIST_TRAVERSE(&xmldoc_tree, doctree, entry) {
  2281. if (!(results = ast_xml_query(doctree->doc, ast_str_buffer(xpath_str)))) {
  2282. continue;
  2283. }
  2284. break;
  2285. }
  2286. AST_RWLIST_UNLOCK(&xmldoc_tree);
  2287. return results;
  2288. }
  2289. static void build_config_docs(struct ast_xml_node *cur, struct ast_xml_doc_item_list *root)
  2290. {
  2291. struct ast_xml_node *iter;
  2292. struct ast_xml_doc_item *item;
  2293. for (iter = ast_xml_node_get_children(cur); iter; iter = ast_xml_node_get_next(iter)) {
  2294. const char *iter_name;
  2295. if (strncasecmp(ast_xml_node_get_name(iter), "config", 6)) {
  2296. continue;
  2297. }
  2298. iter_name = ast_xml_get_attribute(iter, "name");
  2299. /* Now add all of the child config-related items to the list */
  2300. if (!(item = xmldoc_build_documentation_item(iter, iter_name, ast_xml_node_get_name(iter)))) {
  2301. ast_log(LOG_ERROR, "Could not build documentation for '%s:%s'\n", ast_xml_node_get_name(iter), iter_name);
  2302. ast_xml_free_attr(iter_name);
  2303. break;
  2304. }
  2305. ast_xml_free_attr(iter_name);
  2306. if (!strcasecmp(ast_xml_node_get_name(iter), "configOption")) {
  2307. const char *name = ast_xml_get_attribute(cur, "name");
  2308. ast_string_field_set(item, ref, name);
  2309. ast_xml_free_attr(name);
  2310. }
  2311. AST_LIST_INSERT_TAIL(root, item, next);
  2312. build_config_docs(iter, root);
  2313. }
  2314. }
  2315. int ast_xmldoc_regenerate_doc_item(struct ast_xml_doc_item *item)
  2316. {
  2317. const char *name;
  2318. char *syntax;
  2319. char *seealso;
  2320. char *arguments;
  2321. char *synopsis;
  2322. char *description;
  2323. if (!item || !item->node) {
  2324. return -1;
  2325. }
  2326. name = ast_xml_get_attribute(item->node, "name");
  2327. if (!name) {
  2328. return -1;
  2329. }
  2330. syntax = _ast_xmldoc_build_syntax(item->node, item->type, name);
  2331. seealso = _ast_xmldoc_build_seealso(item->node);
  2332. arguments = _ast_xmldoc_build_arguments(item->node);
  2333. synopsis = _ast_xmldoc_build_synopsis(item->node);
  2334. description = _ast_xmldoc_build_description(item->node);
  2335. if (syntax) {
  2336. ast_str_set(&item->syntax, 0, "%s", syntax);
  2337. }
  2338. if (seealso) {
  2339. ast_str_set(&item->seealso, 0, "%s", seealso);
  2340. }
  2341. if (arguments) {
  2342. ast_str_set(&item->arguments, 0, "%s", arguments);
  2343. }
  2344. if (synopsis) {
  2345. ast_str_set(&item->synopsis, 0, "%s", synopsis);
  2346. }
  2347. if (description) {
  2348. ast_str_set(&item->description, 0, "%s", description);
  2349. }
  2350. ast_free(syntax);
  2351. ast_free(seealso);
  2352. ast_free(arguments);
  2353. ast_free(synopsis);
  2354. ast_free(description);
  2355. ast_xml_free_attr(name);
  2356. return 0;
  2357. }
  2358. struct ao2_container *ast_xmldoc_build_documentation(const char *type)
  2359. {
  2360. struct ao2_container *docs;
  2361. struct ast_xml_node *node = NULL, *instance = NULL;
  2362. struct documentation_tree *doctree;
  2363. const char *name;
  2364. docs = ao2_container_alloc_hash(AO2_ALLOC_OPT_LOCK_MUTEX, 0, 127,
  2365. ast_xml_doc_item_hash, NULL, ast_xml_doc_item_cmp);
  2366. if (!docs) {
  2367. ast_log(AST_LOG_ERROR, "Failed to create container for xml document item instances\n");
  2368. return NULL;
  2369. }
  2370. AST_RWLIST_RDLOCK(&xmldoc_tree);
  2371. AST_LIST_TRAVERSE(&xmldoc_tree, doctree, entry) {
  2372. /* the core xml documents have priority over thirdparty document. */
  2373. node = ast_xml_get_root(doctree->doc);
  2374. if (!node) {
  2375. break;
  2376. }
  2377. for (node = ast_xml_node_get_children(node); node; node = ast_xml_node_get_next(node)) {
  2378. struct ast_xml_doc_item *item = NULL;
  2379. /* Ignore empty nodes or nodes that aren't of the type requested */
  2380. if (!ast_xml_node_get_children(node) || strcasecmp(ast_xml_node_get_name(node), type)) {
  2381. continue;
  2382. }
  2383. name = ast_xml_get_attribute(node, "name");
  2384. if (!name) {
  2385. continue;
  2386. }
  2387. switch (xmldoc_get_syntax_type(type)) {
  2388. case MANAGER_EVENT_SYNTAX:
  2389. {
  2390. struct ast_xml_doc_item_list root;
  2391. AST_LIST_HEAD_INIT(&root);
  2392. for (instance = ast_xml_node_get_children(node); instance; instance = ast_xml_node_get_next(instance)) {
  2393. struct ast_xml_doc_item *temp;
  2394. if (!ast_xml_node_get_children(instance) || strcasecmp(ast_xml_node_get_name(instance), "managerEventInstance")) {
  2395. continue;
  2396. }
  2397. temp = xmldoc_build_documentation_item(instance, name, type);
  2398. if (!temp) {
  2399. break;
  2400. }
  2401. AST_LIST_INSERT_TAIL(&root, temp, next);
  2402. }
  2403. item = AST_LIST_FIRST(&root);
  2404. break;
  2405. }
  2406. case CONFIG_INFO_SYNTAX:
  2407. {
  2408. RAII_VAR(const char *, name, ast_xml_get_attribute(node, "name"), ast_xml_free_attr);
  2409. if (!ast_xml_node_get_children(node) || strcasecmp(ast_xml_node_get_name(node), "configInfo")) {
  2410. break;
  2411. }
  2412. item = xmldoc_build_documentation_item(node, name, "configInfo");
  2413. if (item) {
  2414. struct ast_xml_doc_item_list root;
  2415. AST_LIST_HEAD_INIT(&root);
  2416. AST_LIST_INSERT_TAIL(&root, item, next);
  2417. build_config_docs(node, &root);
  2418. }
  2419. break;
  2420. }
  2421. default:
  2422. item = xmldoc_build_documentation_item(node, name, type);
  2423. }
  2424. ast_xml_free_attr(name);
  2425. if (item) {
  2426. ao2_link(docs, item);
  2427. ao2_t_ref(item, -1, "Dispose of creation ref");
  2428. }
  2429. }
  2430. }
  2431. AST_RWLIST_UNLOCK(&xmldoc_tree);
  2432. return docs;
  2433. }
  2434. int ast_xmldoc_regenerate_doc_item(struct ast_xml_doc_item *item);
  2435. #if !defined(HAVE_GLOB_NOMAGIC) || !defined(HAVE_GLOB_BRACE) || defined(DEBUG_NONGNU)
  2436. static int xml_pathmatch(char *xmlpattern, int xmlpattern_maxlen, glob_t *globbuf)
  2437. {
  2438. int globret;
  2439. snprintf(xmlpattern, xmlpattern_maxlen, "%s/documentation/thirdparty/*-%s.xml",
  2440. ast_config_AST_DATA_DIR, documentation_language);
  2441. if((globret = glob(xmlpattern, GLOB_NOCHECK, NULL, globbuf))) {
  2442. return globret;
  2443. }
  2444. snprintf(xmlpattern, xmlpattern_maxlen, "%s/documentation/thirdparty/*-%.2s_??.xml",
  2445. ast_config_AST_DATA_DIR, documentation_language);
  2446. if((globret = glob(xmlpattern, GLOB_APPEND | GLOB_NOCHECK, NULL, globbuf))) {
  2447. return globret;
  2448. }
  2449. snprintf(xmlpattern, xmlpattern_maxlen, "%s/documentation/thirdparty/*-%s.xml",
  2450. ast_config_AST_DATA_DIR, default_documentation_language);
  2451. if((globret = glob(xmlpattern, GLOB_APPEND | GLOB_NOCHECK, NULL, globbuf))) {
  2452. return globret;
  2453. }
  2454. snprintf(xmlpattern, xmlpattern_maxlen, "%s/documentation/*-%s.xml",
  2455. ast_config_AST_DATA_DIR, documentation_language);
  2456. if((globret = glob(xmlpattern, GLOB_APPEND | GLOB_NOCHECK, NULL, globbuf))) {
  2457. return globret;
  2458. }
  2459. snprintf(xmlpattern, xmlpattern_maxlen, "%s/documentation/*-%.2s_??.xml",
  2460. ast_config_AST_DATA_DIR, documentation_language);
  2461. if((globret = glob(xmlpattern, GLOB_APPEND | GLOB_NOCHECK, NULL, globbuf))) {
  2462. return globret;
  2463. }
  2464. snprintf(xmlpattern, xmlpattern_maxlen, "%s/documentation/*-%s.xml",
  2465. ast_config_AST_DATA_DIR, default_documentation_language);
  2466. globret = glob(xmlpattern, GLOB_APPEND | GLOB_NOCHECK, NULL, globbuf);
  2467. return globret;
  2468. }
  2469. #endif
  2470. static char *handle_dump_docs(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  2471. {
  2472. struct documentation_tree *doctree;
  2473. struct ast_xml_doc *dumpdoc;
  2474. struct ast_xml_node *dumproot;
  2475. FILE *f;
  2476. switch (cmd) {
  2477. case CLI_INIT:
  2478. e->command = "xmldoc dump";
  2479. e->usage =
  2480. "Usage: xmldoc dump <filename>\n"
  2481. " Dump XML documentation to a file\n";
  2482. return NULL;
  2483. case CLI_GENERATE:
  2484. return NULL;
  2485. }
  2486. if (a->argc != 3) {
  2487. return CLI_SHOWUSAGE;
  2488. }
  2489. dumpdoc = ast_xml_new();
  2490. if (!dumpdoc) {
  2491. ast_log(LOG_ERROR, "Could not create new XML document\n");
  2492. return CLI_FAILURE;
  2493. }
  2494. dumproot = ast_xml_new_node("docs");
  2495. if (!dumproot) {
  2496. ast_xml_close(dumpdoc);
  2497. ast_log(LOG_ERROR, "Could not create new XML root node\n");
  2498. return CLI_FAILURE;
  2499. }
  2500. ast_xml_set_root(dumpdoc, dumproot);
  2501. AST_RWLIST_RDLOCK(&xmldoc_tree);
  2502. AST_LIST_TRAVERSE(&xmldoc_tree, doctree, entry) {
  2503. struct ast_xml_node *root_node = ast_xml_get_root(doctree->doc);
  2504. struct ast_xml_node *kids = ast_xml_node_get_children(root_node);
  2505. struct ast_xml_node *kids_copy;
  2506. /* If there are no kids someone screwed up, but we check anyway. */
  2507. if (!kids) {
  2508. continue;
  2509. }
  2510. kids_copy = ast_xml_copy_node_list(kids);
  2511. if (!kids_copy) {
  2512. ast_xml_close(dumpdoc);
  2513. ast_log(LOG_ERROR, "Could not create copy of XML node list\n");
  2514. return CLI_FAILURE;
  2515. }
  2516. ast_xml_add_child_list(dumproot, kids_copy);
  2517. }
  2518. AST_RWLIST_UNLOCK(&xmldoc_tree);
  2519. if (!(f = fopen(a->argv[2], "w"))) {
  2520. ast_xml_close(dumpdoc);
  2521. ast_log(LOG_ERROR, "Could not open file '%s': %s\n", a->argv[2], strerror(errno));
  2522. return CLI_FAILURE;
  2523. }
  2524. ast_xml_doc_dump_file(f, dumpdoc);
  2525. ast_xml_close(dumpdoc);
  2526. fclose(f);
  2527. return CLI_SUCCESS;
  2528. }
  2529. static struct ast_cli_entry cli_dump_xmldocs = AST_CLI_DEFINE(handle_dump_docs, "Dump the XML docs to the specified file");
  2530. static char *handle_reload_docs(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
  2531. static struct ast_cli_entry cli_reload_xmldocs = AST_CLI_DEFINE(handle_reload_docs, "Reload the XML docs");
  2532. /*! \note Must be called with xmldoc_tree locked */
  2533. static void xmldoc_purge_documentation(void)
  2534. {
  2535. struct documentation_tree *doctree;
  2536. while ((doctree = AST_RWLIST_REMOVE_HEAD(&xmldoc_tree, entry))) {
  2537. ast_free(doctree->filename);
  2538. ast_xml_close(doctree->doc);
  2539. ast_free(doctree);
  2540. }
  2541. }
  2542. /*! \brief Close and unload XML documentation. */
  2543. static void xmldoc_unload_documentation(void)
  2544. {
  2545. ast_cli_unregister(&cli_reload_xmldocs);
  2546. ast_cli_unregister(&cli_dump_xmldocs);
  2547. AST_RWLIST_WRLOCK(&xmldoc_tree);
  2548. xmldoc_purge_documentation();
  2549. AST_RWLIST_UNLOCK(&xmldoc_tree);
  2550. ast_xml_finish();
  2551. }
  2552. static int xmldoc_load_documentation(int first_time)
  2553. {
  2554. struct ast_xml_node *root_node;
  2555. struct ast_xml_doc *tmpdoc;
  2556. struct documentation_tree *doc_tree;
  2557. char *xmlpattern;
  2558. struct ast_config *cfg = NULL;
  2559. struct ast_variable *var = NULL;
  2560. struct ast_flags cnfflags = { 0 };
  2561. int globret, i, dup, duplicate;
  2562. glob_t globbuf;
  2563. #if !defined(HAVE_GLOB_NOMAGIC) || !defined(HAVE_GLOB_BRACE) || defined(DEBUG_NONGNU)
  2564. int xmlpattern_maxlen;
  2565. #endif
  2566. /* setup default XML documentation language */
  2567. snprintf(documentation_language, sizeof(documentation_language), default_documentation_language);
  2568. if ((cfg = ast_config_load2("asterisk.conf", "" /* core can't reload */, cnfflags)) && cfg != CONFIG_STATUS_FILEINVALID) {
  2569. for (var = ast_variable_browse(cfg, "options"); var; var = var->next) {
  2570. if (!strcasecmp(var->name, "documentation_language")) {
  2571. if (!ast_strlen_zero(var->value)) {
  2572. snprintf(documentation_language, sizeof(documentation_language), "%s", var->value);
  2573. }
  2574. }
  2575. }
  2576. ast_config_destroy(cfg);
  2577. }
  2578. if (first_time) {
  2579. /* initialize the XML library. */
  2580. ast_xml_init();
  2581. ast_cli_register(&cli_dump_xmldocs);
  2582. ast_cli_register(&cli_reload_xmldocs);
  2583. /* register function to be run when asterisk finish. */
  2584. ast_register_cleanup(xmldoc_unload_documentation);
  2585. }
  2586. globbuf.gl_offs = 0; /* slots to reserve in gl_pathv */
  2587. #if !defined(HAVE_GLOB_NOMAGIC) || !defined(HAVE_GLOB_BRACE) || defined(DEBUG_NONGNU)
  2588. xmlpattern_maxlen = strlen(ast_config_AST_DATA_DIR) + strlen("/documentation/thirdparty") + strlen("/*-??_??.xml") + 1;
  2589. xmlpattern = ast_malloc(xmlpattern_maxlen);
  2590. globret = xml_pathmatch(xmlpattern, xmlpattern_maxlen, &globbuf);
  2591. #else
  2592. /* Get every *-LANG.xml file inside $(ASTDATADIR)/documentation */
  2593. if (ast_asprintf(&xmlpattern, "%s/documentation{/thirdparty/,/}*-{%s,%.2s_??,%s}.xml", ast_config_AST_DATA_DIR,
  2594. documentation_language, documentation_language, default_documentation_language) < 0) {
  2595. return 1;
  2596. }
  2597. globret = glob(xmlpattern, MY_GLOB_FLAGS, NULL, &globbuf);
  2598. #endif
  2599. ast_debug(3, "gl_pathc %zu\n", (size_t)globbuf.gl_pathc);
  2600. if (globret == GLOB_NOSPACE) {
  2601. ast_log(LOG_WARNING, "XML load failure, glob expansion of pattern '%s' failed: Not enough memory\n", xmlpattern);
  2602. ast_free(xmlpattern);
  2603. return 1;
  2604. } else if (globret == GLOB_ABORTED) {
  2605. ast_log(LOG_WARNING, "XML load failure, glob expansion of pattern '%s' failed: Read error\n", xmlpattern);
  2606. ast_free(xmlpattern);
  2607. return 1;
  2608. }
  2609. ast_free(xmlpattern);
  2610. AST_RWLIST_WRLOCK(&xmldoc_tree);
  2611. if (!first_time) {
  2612. /* If we're reloading, purge the existing documentation.
  2613. * We do this with the lock held so that if somebody
  2614. * else tries to get documentation, there's no chance
  2615. * of retrieiving it after we purged the old docs
  2616. * but before we loaded the new ones. */
  2617. xmldoc_purge_documentation();
  2618. }
  2619. /* loop over expanded files */
  2620. for (i = 0; i < globbuf.gl_pathc; i++) {
  2621. /* check for duplicates (if we already [try to] open the same file. */
  2622. duplicate = 0;
  2623. for (dup = 0; dup < i; dup++) {
  2624. if (!strcmp(globbuf.gl_pathv[i], globbuf.gl_pathv[dup])) {
  2625. duplicate = 1;
  2626. break;
  2627. }
  2628. }
  2629. if (duplicate || strchr(globbuf.gl_pathv[i], '*')) {
  2630. /* skip duplicates as well as pathnames not found
  2631. * (due to use of GLOB_NOCHECK in xml_pathmatch) */
  2632. continue;
  2633. }
  2634. tmpdoc = NULL;
  2635. tmpdoc = ast_xml_open(globbuf.gl_pathv[i]);
  2636. if (!tmpdoc) {
  2637. ast_log(LOG_ERROR, "Could not open XML documentation at '%s'\n", globbuf.gl_pathv[i]);
  2638. continue;
  2639. }
  2640. /* Get doc root node and check if it starts with '<docs>' */
  2641. root_node = ast_xml_get_root(tmpdoc);
  2642. if (!root_node) {
  2643. ast_log(LOG_ERROR, "Error getting documentation root node\n");
  2644. ast_xml_close(tmpdoc);
  2645. continue;
  2646. }
  2647. /* Check root node name for malformed xmls. */
  2648. if (strcmp(ast_xml_node_get_name(root_node), "docs")) {
  2649. ast_log(LOG_ERROR, "Documentation file is not well formed!\n");
  2650. ast_xml_close(tmpdoc);
  2651. continue;
  2652. }
  2653. doc_tree = ast_calloc(1, sizeof(*doc_tree));
  2654. if (!doc_tree) {
  2655. ast_log(LOG_ERROR, "Unable to allocate documentation_tree structure!\n");
  2656. ast_xml_close(tmpdoc);
  2657. continue;
  2658. }
  2659. doc_tree->doc = tmpdoc;
  2660. doc_tree->filename = ast_strdup(globbuf.gl_pathv[i]);
  2661. AST_RWLIST_INSERT_TAIL(&xmldoc_tree, doc_tree, entry);
  2662. }
  2663. AST_RWLIST_UNLOCK(&xmldoc_tree);
  2664. globfree(&globbuf);
  2665. return 0;
  2666. }
  2667. int ast_xmldoc_load_documentation(void)
  2668. {
  2669. return xmldoc_load_documentation(1);
  2670. }
  2671. static int xmldoc_reload_documentation(void)
  2672. {
  2673. return xmldoc_load_documentation(0);
  2674. }
  2675. static char *handle_reload_docs(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  2676. {
  2677. switch (cmd) {
  2678. case CLI_INIT:
  2679. e->command = "xmldoc reload";
  2680. e->usage =
  2681. "Usage: xmldoc reload\n"
  2682. " Reload XML documentation\n";
  2683. return NULL;
  2684. case CLI_GENERATE:
  2685. return NULL;
  2686. }
  2687. xmldoc_reload_documentation();
  2688. return CLI_SUCCESS;
  2689. }
  2690. #endif /* AST_XML_DOCS */