xmldoc.c 82 KB

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