shell_xml.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229
  1. /*
  2. * ------------------------------------------------------
  3. * shell_xml.c
  4. *
  5. * This file implements the means to read an XML encoded file and populate the
  6. * CLI tree based on the contents.
  7. * ------------------------------------------------------
  8. */
  9. #include "private.h"
  10. #include "xmlapi.h"
  11. #include "lub/string.h"
  12. #include "lub/ctype.h"
  13. #include "lub/system.h"
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <assert.h>
  17. #include <errno.h>
  18. #include <sys/types.h>
  19. #include <dirent.h>
  20. typedef int (PROCESS_FN) (clish_shell_t *instance,
  21. clish_xmlnode_t *element, void *parent);
  22. /* Define a control block for handling the decode of an XML file */
  23. typedef struct clish_xml_cb_s clish_xml_cb_t;
  24. struct clish_xml_cb_s {
  25. const char *element;
  26. PROCESS_FN *handler;
  27. };
  28. /* forward declare the handler functions */
  29. static PROCESS_FN
  30. process_clish_module,
  31. process_startup,
  32. process_view,
  33. process_command,
  34. process_param,
  35. process_action,
  36. process_ptype,
  37. process_overview,
  38. process_detail,
  39. process_namespace,
  40. process_config,
  41. process_var,
  42. process_wdog,
  43. process_hotkey,
  44. process_plugin,
  45. process_hook;
  46. static clish_xml_cb_t xml_elements[] = {
  47. {"CLISH_MODULE", process_clish_module},
  48. {"STARTUP", process_startup},
  49. {"VIEW", process_view},
  50. {"COMMAND", process_command},
  51. {"PARAM", process_param},
  52. {"ACTION", process_action},
  53. {"PTYPE", process_ptype},
  54. {"OVERVIEW", process_overview},
  55. {"DETAIL", process_detail},
  56. {"NAMESPACE", process_namespace},
  57. {"CONFIG", process_config},
  58. {"VAR", process_var},
  59. {"WATCHDOG", process_wdog},
  60. {"HOTKEY", process_hotkey},
  61. {"PLUGIN", process_plugin},
  62. {"HOOK", process_hook},
  63. {NULL, NULL}
  64. };
  65. /*
  66. * if CLISH_PATH is unset in the environment then this is the value used.
  67. */
  68. const char *default_path = "/etc/clish;~/.clish";
  69. /*-------------------------------------------------------- */
  70. int clish_shell_load_scheme(clish_shell_t *this, const char *xml_path)
  71. {
  72. const char *path = xml_path;
  73. char *buffer;
  74. char *dirname;
  75. char *saveptr = NULL;
  76. int res = 0;
  77. /* Precreate internal PTYPEs */
  78. clish_shell_find_create_ptype(this,
  79. "__ptype_ARGS",
  80. "Arguments", "[^\\\\]+",
  81. CLISH_PTYPE_REGEXP,
  82. CLISH_PTYPE_NONE);
  83. /* use the default path */
  84. if (!path)
  85. path = default_path;
  86. /* take a copy of the path */
  87. buffer = lub_system_tilde_expand(path);
  88. /* now loop though each directory */
  89. for (dirname = strtok_r(buffer, ";", &saveptr);
  90. dirname; dirname = strtok_r(NULL, ";", &saveptr)) {
  91. DIR *dir;
  92. struct dirent *entry;
  93. /* search this directory for any XML files */
  94. dir = opendir(dirname);
  95. if (NULL == dir) {
  96. #ifdef DEBUG
  97. tinyrl_printf(this->tinyrl,
  98. "*** Failed to open '%s' directory\n",
  99. dirname);
  100. #endif
  101. continue;
  102. }
  103. for (entry = readdir(dir); entry; entry = readdir(dir)) {
  104. const char *extension = strrchr(entry->d_name, '.');
  105. /* check the filename */
  106. if ((NULL != extension) &&
  107. (0 == strcmp(".xml", extension))) {
  108. char *filename = NULL;
  109. /* build the filename */
  110. lub_string_cat(&filename, dirname);
  111. lub_string_cat(&filename, "/");
  112. lub_string_cat(&filename, entry->d_name);
  113. #ifdef DEBUG
  114. fprintf(stderr, "Parse XML-file: %s\n", filename);
  115. #endif
  116. /* load this file */
  117. res = clish_shell_xml_read(this, filename);
  118. /* Error message */
  119. if (res)
  120. fprintf(stderr, CLISH_XML_ERROR_STR"File %s\n",
  121. filename);
  122. /* release the resource */
  123. lub_string_free(filename);
  124. }
  125. if (res)
  126. break;
  127. }
  128. /* all done for this directory */
  129. closedir(dir);
  130. if (res)
  131. break;
  132. }
  133. lub_string_free(buffer);
  134. return res;
  135. }
  136. /*
  137. * ------------------------------------------------------
  138. * This function reads an element from the XML stream and processes it.
  139. * ------------------------------------------------------
  140. */
  141. static int process_node(clish_shell_t *shell, clish_xmlnode_t *node,
  142. void *parent)
  143. {
  144. int res = 0;
  145. switch (clish_xmlnode_get_type(node)) {
  146. case CLISH_XMLNODE_ELM: {
  147. clish_xml_cb_t * cb;
  148. char name[128];
  149. unsigned int namelen = sizeof(name);
  150. if (clish_xmlnode_get_name(node, name, &namelen) == 0) {
  151. for (cb = &xml_elements[0]; cb->element; cb++) {
  152. if (0 == strcmp(name, cb->element)) {
  153. #ifdef DEBUG
  154. fprintf(stderr, "NODE:");
  155. clish_xmlnode_print(node, stderr);
  156. fprintf(stderr, "\n");
  157. #endif
  158. /* process the elements at this level */
  159. res = cb->handler(shell, node, parent);
  160. /* Error message */
  161. if (res) {
  162. char *ename = clish_xmlnode_fetch_attr(node, "name");
  163. char *eref = clish_xmlnode_fetch_attr(node, "ref");
  164. char *ekey = clish_xmlnode_fetch_attr(node, "key");
  165. char *efile = clish_xmlnode_fetch_attr(node, "file");
  166. fprintf(stderr, CLISH_XML_ERROR_STR"Node %s", name);
  167. if (ename)
  168. fprintf(stderr, ", name=\"%s\"", ename);
  169. if (eref)
  170. fprintf(stderr, ", ref=\"%s\"", eref);
  171. if (ekey)
  172. fprintf(stderr, ", key=\"%s\"", ekey);
  173. if (efile)
  174. fprintf(stderr, ", file=\"%s\"", efile);
  175. fprintf(stderr, "\n");
  176. clish_xml_release(ename);
  177. clish_xml_release(eref);
  178. clish_xml_release(ekey);
  179. clish_xml_release(efile);
  180. }
  181. break;
  182. }
  183. }
  184. }
  185. break;
  186. }
  187. case CLISH_XMLNODE_DOC:
  188. case CLISH_XMLNODE_TEXT:
  189. case CLISH_XMLNODE_ATTR:
  190. case CLISH_XMLNODE_PI:
  191. case CLISH_XMLNODE_COMMENT:
  192. case CLISH_XMLNODE_DECL:
  193. case CLISH_XMLNODE_UNKNOWN:
  194. default:
  195. break;
  196. }
  197. return res;
  198. }
  199. /* ------------------------------------------------------ */
  200. static int process_children(clish_shell_t *shell,
  201. clish_xmlnode_t *element, void *parent)
  202. {
  203. clish_xmlnode_t *node = NULL;
  204. int res;
  205. while ((node = clish_xmlnode_next_child(element, node)) != NULL) {
  206. /* Now deal with all the contained elements */
  207. res = process_node(shell, node, parent);
  208. if (res)
  209. return res;
  210. }
  211. return 0;
  212. }
  213. /* ------------------------------------------------------ */
  214. int clish_shell_xml_read(clish_shell_t *shell, const char *filename)
  215. {
  216. int ret = -1;
  217. clish_xmldoc_t *doc;
  218. doc = clish_xmldoc_read(filename);
  219. if (clish_xmldoc_is_valid(doc)) {
  220. clish_xmlnode_t *root = clish_xmldoc_get_root(doc);
  221. ret = process_node(shell, root, NULL);
  222. } else {
  223. int errcaps = clish_xmldoc_error_caps(doc);
  224. printf("Unable to open file '%s'", filename);
  225. if ((errcaps & CLISH_XMLERR_LINE) == CLISH_XMLERR_LINE)
  226. printf(", at line %d", clish_xmldoc_get_err_line(doc));
  227. if ((errcaps & CLISH_XMLERR_COL) == CLISH_XMLERR_COL)
  228. printf(", at column %d", clish_xmldoc_get_err_col(doc));
  229. if ((errcaps & CLISH_XMLERR_DESC) == CLISH_XMLERR_DESC)
  230. printf(", message is %s", clish_xmldoc_get_err_msg(doc));
  231. printf("\n");
  232. }
  233. clish_xmldoc_release(doc);
  234. return ret;
  235. }
  236. /* ------------------------------------------------------ */
  237. static int process_clish_module(clish_shell_t *shell, clish_xmlnode_t *element,
  238. void *parent)
  239. {
  240. /* Create the global view */
  241. if (!shell->global)
  242. shell->global = clish_shell_find_create_view(shell,
  243. "__view_global", "");
  244. parent = parent; /* Happy compiler */
  245. return process_children(shell, element, shell->global);
  246. }
  247. /* ------------------------------------------------------ */
  248. static int process_view(clish_shell_t *shell, clish_xmlnode_t *element,
  249. void *parent)
  250. {
  251. clish_view_t *view;
  252. int res = -1;
  253. char *name = clish_xmlnode_fetch_attr(element, "name");
  254. char *prompt = clish_xmlnode_fetch_attr(element, "prompt");
  255. char *depth = clish_xmlnode_fetch_attr(element, "depth");
  256. char *restore = clish_xmlnode_fetch_attr(element, "restore");
  257. char *access = clish_xmlnode_fetch_attr(element, "access");
  258. /* Check syntax */
  259. if (!name) {
  260. fprintf(stderr, CLISH_XML_ERROR_ATTR("name"));
  261. goto error;
  262. }
  263. /* re-use a view if it already exists */
  264. view = clish_shell_find_create_view(shell, name, prompt);
  265. if (depth && (lub_ctype_isdigit(*depth))) {
  266. unsigned res = atoi(depth);
  267. clish_view__set_depth(view, res);
  268. }
  269. if (restore) {
  270. if (!lub_string_nocasecmp(restore, "depth"))
  271. clish_view__set_restore(view, CLISH_RESTORE_DEPTH);
  272. else if (!lub_string_nocasecmp(restore, "view"))
  273. clish_view__set_restore(view, CLISH_RESTORE_VIEW);
  274. else
  275. clish_view__set_restore(view, CLISH_RESTORE_NONE);
  276. }
  277. if (access)
  278. clish_view__set_access(view, access);
  279. //process_view_end:
  280. res = process_children(shell, element, view);
  281. error:
  282. clish_xml_release(name);
  283. clish_xml_release(prompt);
  284. clish_xml_release(depth);
  285. clish_xml_release(restore);
  286. clish_xml_release(access);
  287. parent = parent; /* Happy compiler */
  288. return res;
  289. }
  290. /* ------------------------------------------------------ */
  291. static int process_ptype(clish_shell_t *shell, clish_xmlnode_t *element,
  292. void *parent)
  293. {
  294. clish_ptype_method_e method;
  295. clish_ptype_preprocess_e preprocess;
  296. int res = -1;
  297. char *name = clish_xmlnode_fetch_attr(element, "name");
  298. char *help = clish_xmlnode_fetch_attr(element, "help");
  299. char *pattern = clish_xmlnode_fetch_attr(element, "pattern");
  300. char *method_name = clish_xmlnode_fetch_attr(element, "method");
  301. char *preprocess_name = clish_xmlnode_fetch_attr(element, "preprocess");
  302. /* Check syntax */
  303. if (!name) {
  304. fprintf(stderr, CLISH_XML_ERROR_ATTR("name"));
  305. goto error;
  306. }
  307. if (!pattern) {
  308. fprintf(stderr, CLISH_XML_ERROR_ATTR("pattern"));
  309. goto error;
  310. }
  311. method = clish_ptype_method_resolve(method_name);
  312. preprocess = clish_ptype_preprocess_resolve(preprocess_name);
  313. clish_shell_find_create_ptype(shell,
  314. name, help, pattern, method, preprocess);
  315. res = 0;
  316. error:
  317. clish_xml_release(name);
  318. clish_xml_release(help);
  319. clish_xml_release(pattern);
  320. clish_xml_release(method_name);
  321. clish_xml_release(preprocess_name);
  322. parent = parent; /* Happy compiler */
  323. return res;
  324. }
  325. /* ------------------------------------------------------ */
  326. static int process_overview(clish_shell_t *shell, clish_xmlnode_t *element,
  327. void *parent)
  328. {
  329. char *content = NULL;
  330. unsigned int content_len = 2048;
  331. int result;
  332. /*
  333. * the code below faithfully assume that we'll be able fully store
  334. * the content of the node. If it's really, really big, we may have
  335. * an issue (but then, if it's that big, how the hell does it
  336. * already fits in allocated memory?)
  337. * Ergo, it -should- be safe.
  338. */
  339. do {
  340. char *new = (char*)realloc(content, content_len);
  341. if (!new) {
  342. if (content)
  343. free(content);
  344. return -1;
  345. }
  346. content = new;
  347. result = clish_xmlnode_get_content(element, content,
  348. &content_len);
  349. } while (result == -E2BIG);
  350. if (result == 0 && content) {
  351. /* set the overview text for this view */
  352. assert(NULL == shell->overview);
  353. /* store the overview */
  354. shell->overview = lub_string_dup(content);
  355. }
  356. if (content)
  357. free(content);
  358. parent = parent; /* Happy compiler */
  359. return 0;
  360. }
  361. /* ------------------------------------------------------ */
  362. static int process_command(clish_shell_t *shell, clish_xmlnode_t *element,
  363. void *parent)
  364. {
  365. clish_view_t *v = (clish_view_t *) parent;
  366. clish_command_t *cmd = NULL;
  367. clish_command_t *old;
  368. int res = -1;
  369. char *access = clish_xmlnode_fetch_attr(element, "access");
  370. char *name = clish_xmlnode_fetch_attr(element, "name");
  371. char *help = clish_xmlnode_fetch_attr(element, "help");
  372. char *view = clish_xmlnode_fetch_attr(element, "view");
  373. char *viewid = clish_xmlnode_fetch_attr(element, "viewid");
  374. char *escape_chars = clish_xmlnode_fetch_attr(element, "escape_chars");
  375. char *args_name = clish_xmlnode_fetch_attr(element, "args");
  376. char *args_help = clish_xmlnode_fetch_attr(element, "args_help");
  377. char *lock = clish_xmlnode_fetch_attr(element, "lock");
  378. char *interrupt = clish_xmlnode_fetch_attr(element, "interrupt");
  379. char *ref = clish_xmlnode_fetch_attr(element, "ref");
  380. /* Check syntax */
  381. if (!name) {
  382. fprintf(stderr, CLISH_XML_ERROR_ATTR("name"));
  383. goto error;
  384. }
  385. if (!help) {
  386. fprintf(stderr, CLISH_XML_ERROR_ATTR("help"));
  387. goto error;
  388. }
  389. /* check this command doesn't already exist */
  390. old = clish_view_find_command(v, name, BOOL_FALSE);
  391. if (old) {
  392. fprintf(stderr, CLISH_XML_ERROR_STR"Duplicate COMMAND name=\"%s\".\n", name);
  393. goto error;
  394. }
  395. /* create a command */
  396. cmd = clish_view_new_command(v, name, help);
  397. clish_command__set_pview(cmd, v);
  398. /* Reference 'ref' field */
  399. if (ref) {
  400. char *saveptr = NULL;
  401. const char *delim = "@";
  402. char *view_name = NULL;
  403. char *cmdn = NULL;
  404. char *str = lub_string_dup(ref);
  405. cmdn = strtok_r(str, delim, &saveptr);
  406. if (!cmdn) {
  407. fprintf(stderr, CLISH_XML_ERROR_STR"Invalid \"ref\" attribute value.\n");
  408. lub_string_free(str);
  409. goto error;
  410. }
  411. clish_command__set_alias(cmd, cmdn); /* alias name */
  412. view_name = strtok_r(NULL, delim, &saveptr);
  413. clish_command__set_alias_view(cmd, view_name);
  414. lub_string_free(str);
  415. }
  416. /* define some specialist escape characters */
  417. if (escape_chars)
  418. clish_command__set_escape_chars(cmd, escape_chars);
  419. if (args_name) {
  420. /* define a "rest of line" argument */
  421. clish_param_t *param;
  422. /* Check syntax */
  423. if (!args_help) {
  424. fprintf(stderr, CLISH_XML_ERROR_ATTR("args_help"));
  425. goto error;
  426. }
  427. param = clish_param_new(args_name, args_help, "__ptype_ARGS");
  428. clish_command__set_args(cmd, param);
  429. }
  430. /* define the view which this command changes to */
  431. if (view) {
  432. /* reference the next view */
  433. clish_command__set_viewname(cmd, view);
  434. }
  435. /* define the view id which this command changes to */
  436. if (viewid)
  437. clish_command__set_viewid(cmd, viewid);
  438. /* lock field */
  439. if (lock && lub_string_nocasecmp(lock, "false") == 0)
  440. clish_command__set_lock(cmd, BOOL_FALSE);
  441. else
  442. clish_command__set_lock(cmd, BOOL_TRUE);
  443. /* interrupt field */
  444. if (interrupt && lub_string_nocasecmp(interrupt, "true") == 0)
  445. clish_command__set_interrupt(cmd, BOOL_TRUE);
  446. else
  447. clish_command__set_interrupt(cmd, BOOL_FALSE);
  448. if (access)
  449. clish_command__set_access(cmd, access);
  450. //process_command_end:
  451. res = process_children(shell, element, cmd);
  452. error:
  453. clish_xml_release(access);
  454. clish_xml_release(name);
  455. clish_xml_release(help);
  456. clish_xml_release(view);
  457. clish_xml_release(viewid);
  458. clish_xml_release(escape_chars);
  459. clish_xml_release(args_name);
  460. clish_xml_release(args_help);
  461. clish_xml_release(lock);
  462. clish_xml_release(interrupt);
  463. clish_xml_release(ref);
  464. return res;
  465. }
  466. /* ------------------------------------------------------ */
  467. static int process_startup(clish_shell_t *shell, clish_xmlnode_t *element,
  468. void *parent)
  469. {
  470. clish_view_t *v = (clish_view_t *) parent;
  471. clish_command_t *cmd = NULL;
  472. int res = -1;
  473. char *view = clish_xmlnode_fetch_attr(element, "view");
  474. char *viewid = clish_xmlnode_fetch_attr(element, "viewid");
  475. char *default_shebang =
  476. clish_xmlnode_fetch_attr(element, "default_shebang");
  477. char *timeout = clish_xmlnode_fetch_attr(element, "timeout");
  478. char *lock = clish_xmlnode_fetch_attr(element, "lock");
  479. char *interrupt = clish_xmlnode_fetch_attr(element, "interrupt");
  480. char *default_plugin = clish_xmlnode_fetch_attr(element,
  481. "default_plugin");
  482. /* Check syntax */
  483. if (!view) {
  484. fprintf(stderr, CLISH_XML_ERROR_ATTR("view"));
  485. goto error;
  486. }
  487. if (shell->startup) {
  488. fprintf(stderr, CLISH_XML_ERROR_STR"STARTUP tag duplication.\n");
  489. goto error;
  490. }
  491. /* create a command with NULL help */
  492. cmd = clish_view_new_command(v, "startup", NULL);
  493. clish_command__set_lock(cmd, BOOL_FALSE);
  494. /* reference the next view */
  495. clish_command__set_viewname(cmd, view);
  496. /* define the view id which this command changes to */
  497. if (viewid)
  498. clish_command__set_viewid(cmd, viewid);
  499. if (default_shebang)
  500. clish_shell__set_default_shebang(shell, default_shebang);
  501. if (timeout)
  502. clish_shell__set_timeout(shell, atoi(timeout));
  503. /* lock field */
  504. if (lock && lub_string_nocasecmp(lock, "false") == 0)
  505. clish_command__set_lock(cmd, BOOL_FALSE);
  506. else
  507. clish_command__set_lock(cmd, BOOL_TRUE);
  508. /* interrupt field */
  509. if (interrupt && lub_string_nocasecmp(interrupt, "true") == 0)
  510. clish_command__set_interrupt(cmd, BOOL_TRUE);
  511. else
  512. clish_command__set_interrupt(cmd, BOOL_FALSE);
  513. /* If we need the default plugin */
  514. if (default_plugin && (0 == strcmp(default_plugin, "false")))
  515. shell->default_plugin = BOOL_FALSE;
  516. /* remember this command */
  517. shell->startup = cmd;
  518. res = process_children(shell, element, cmd);
  519. error:
  520. clish_xml_release(view);
  521. clish_xml_release(viewid);
  522. clish_xml_release(default_shebang);
  523. clish_xml_release(timeout);
  524. clish_xml_release(lock);
  525. clish_xml_release(interrupt);
  526. return res;
  527. }
  528. /* ------------------------------------------------------ */
  529. static int process_param(clish_shell_t *shell, clish_xmlnode_t *element,
  530. void *parent)
  531. {
  532. clish_command_t *cmd = NULL;
  533. clish_param_t *p_param = NULL;
  534. clish_xmlnode_t *pelement;
  535. clish_param_t *param;
  536. char *pname;
  537. int res = -1;
  538. char *name = clish_xmlnode_fetch_attr(element, "name");
  539. char *help = clish_xmlnode_fetch_attr(element, "help");
  540. char *ptype = clish_xmlnode_fetch_attr(element, "ptype");
  541. char *prefix = clish_xmlnode_fetch_attr(element, "prefix");
  542. char *defval = clish_xmlnode_fetch_attr(element, "default");
  543. char *mode = clish_xmlnode_fetch_attr(element, "mode");
  544. char *optional = clish_xmlnode_fetch_attr(element, "optional");
  545. char *order = clish_xmlnode_fetch_attr(element, "order");
  546. char *value = clish_xmlnode_fetch_attr(element, "value");
  547. char *hidden = clish_xmlnode_fetch_attr(element, "hidden");
  548. char *test = clish_xmlnode_fetch_attr(element, "test");
  549. char *completion = clish_xmlnode_fetch_attr(element, "completion");
  550. char *access = clish_xmlnode_fetch_attr(element, "access");
  551. /* The PARAM can be child of COMMAND or another PARAM */
  552. pelement = clish_xmlnode_parent(element);
  553. pname = clish_xmlnode_get_all_name(pelement);
  554. if (pname && lub_string_nocasecmp(pname, "PARAM") == 0)
  555. p_param = (clish_param_t *)parent;
  556. else
  557. cmd = (clish_command_t *)parent;
  558. if (pname)
  559. free(pname);
  560. if (!cmd && !p_param)
  561. goto error;
  562. /* Check syntax */
  563. if (cmd && (cmd == shell->startup)) {
  564. fprintf(stderr, CLISH_XML_ERROR_STR"STARTUP can't contain PARAMs.\n");
  565. goto error;
  566. }
  567. if (!name) {
  568. fprintf(stderr, CLISH_XML_ERROR_ATTR("name"));
  569. goto error;
  570. }
  571. if (!help) {
  572. fprintf(stderr, CLISH_XML_ERROR_ATTR("help"));
  573. goto error;
  574. }
  575. if (!ptype) {
  576. fprintf(stderr, CLISH_XML_ERROR_ATTR("ptype"));
  577. goto error;
  578. }
  579. param = clish_param_new(name, help, ptype);
  580. /* If prefix is set clish will emulate old optional
  581. * command syntax over newer optional command mechanism.
  582. * It will create nested PARAM.
  583. */
  584. if (prefix) {
  585. const char *ptype_name = "__ptype_SUBCOMMAND";
  586. clish_param_t *opt_param = NULL;
  587. char *str = NULL;
  588. clish_ptype_t *tmp;
  589. /* Create a ptype for prefix-named subcommand that
  590. * will contain the nested optional parameter. The
  591. * name of ptype is hardcoded. It's not good but
  592. * it's only the service ptype.
  593. */
  594. tmp = (clish_ptype_t *)lub_bintree_find(
  595. &shell->ptype_tree, ptype_name);
  596. if (!tmp)
  597. tmp = clish_shell_find_create_ptype(shell,
  598. ptype_name, "Option", "[^\\\\]+",
  599. CLISH_PTYPE_REGEXP, CLISH_PTYPE_NONE);
  600. assert(tmp);
  601. lub_string_cat(&str, "_prefix_");
  602. lub_string_cat(&str, name);
  603. opt_param = clish_param_new(str, help, ptype_name);
  604. lub_string_free(str);
  605. clish_param__set_mode(opt_param,
  606. CLISH_PARAM_SUBCOMMAND);
  607. clish_param__set_value(opt_param, prefix);
  608. clish_param__set_optional(opt_param, BOOL_TRUE);
  609. if (test)
  610. clish_param__set_test(opt_param, test);
  611. /* Add the parameter to the command */
  612. if (cmd)
  613. clish_command_insert_param(cmd, opt_param);
  614. /* Add the parameter to the param */
  615. if (p_param)
  616. clish_param_insert_param(p_param, opt_param);
  617. /* Unset cmd and set parent param to opt_param */
  618. cmd = NULL;
  619. p_param = opt_param;
  620. }
  621. if (defval)
  622. clish_param__set_default(param, defval);
  623. if (hidden && lub_string_nocasecmp(hidden, "true") == 0)
  624. clish_param__set_hidden(param, BOOL_TRUE);
  625. else
  626. clish_param__set_hidden(param, BOOL_FALSE);
  627. if (mode) {
  628. if (lub_string_nocasecmp(mode, "switch") == 0) {
  629. clish_param__set_mode(param,
  630. CLISH_PARAM_SWITCH);
  631. /* Force hidden attribute */
  632. clish_param__set_hidden(param, BOOL_TRUE);
  633. } else if (lub_string_nocasecmp(mode, "subcommand") == 0)
  634. clish_param__set_mode(param,
  635. CLISH_PARAM_SUBCOMMAND);
  636. else
  637. clish_param__set_mode(param,
  638. CLISH_PARAM_COMMON);
  639. }
  640. if (optional && lub_string_nocasecmp(optional, "true") == 0)
  641. clish_param__set_optional(param, BOOL_TRUE);
  642. else
  643. clish_param__set_optional(param, BOOL_FALSE);
  644. if (order && lub_string_nocasecmp(order, "true") == 0)
  645. clish_param__set_order(param, BOOL_TRUE);
  646. else
  647. clish_param__set_order(param, BOOL_FALSE);
  648. if (value) {
  649. clish_param__set_value(param, value);
  650. /* Force mode to subcommand */
  651. clish_param__set_mode(param,
  652. CLISH_PARAM_SUBCOMMAND);
  653. }
  654. if (test && !prefix)
  655. clish_param__set_test(param, test);
  656. if (completion)
  657. clish_param__set_completion(param, completion);
  658. if (access)
  659. clish_param__set_access(param, access);
  660. /* Add the parameter to the command */
  661. if (cmd)
  662. clish_command_insert_param(cmd, param);
  663. /* Add the parameter to the param */
  664. if (p_param)
  665. clish_param_insert_param(p_param, param);
  666. res = process_children(shell, element, param);
  667. error:
  668. clish_xml_release(name);
  669. clish_xml_release(help);
  670. clish_xml_release(ptype);
  671. clish_xml_release(prefix);
  672. clish_xml_release(defval);
  673. clish_xml_release(mode);
  674. clish_xml_release(optional);
  675. clish_xml_release(order);
  676. clish_xml_release(value);
  677. clish_xml_release(hidden);
  678. clish_xml_release(test);
  679. clish_xml_release(completion);
  680. clish_xml_release(access);
  681. return res;
  682. }
  683. /* ------------------------------------------------------ */
  684. static int process_action(clish_shell_t *shell, clish_xmlnode_t *element,
  685. void *parent)
  686. {
  687. clish_action_t *action = NULL;
  688. char *builtin = clish_xmlnode_fetch_attr(element, "builtin");
  689. char *shebang = clish_xmlnode_fetch_attr(element, "shebang");
  690. clish_xmlnode_t *pelement = clish_xmlnode_parent(element);
  691. char *pname = clish_xmlnode_get_all_name(pelement);
  692. char *text;
  693. clish_sym_t *sym = NULL;
  694. if (pname && lub_string_nocasecmp(pname, "VAR") == 0)
  695. action = clish_var__get_action((clish_var_t *)parent);
  696. else
  697. action = clish_command__get_action((clish_command_t *)parent);
  698. if (pname)
  699. free(pname);
  700. text = clish_xmlnode_get_all_content(element);
  701. if (text && *text) {
  702. /* store the action */
  703. clish_action__set_script(action, text);
  704. }
  705. if (text)
  706. free(text);
  707. if (builtin)
  708. sym = clish_shell_add_unresolved_sym(shell, builtin,
  709. CLISH_SYM_TYPE_ACTION);
  710. else
  711. sym = shell->hooks[CLISH_SYM_TYPE_ACTION];
  712. clish_action__set_builtin(action, sym);
  713. if (shebang)
  714. clish_action__set_shebang(action, shebang);
  715. clish_xml_release(builtin);
  716. clish_xml_release(shebang);
  717. return 0;
  718. }
  719. /* ------------------------------------------------------ */
  720. static int process_detail(clish_shell_t *shell, clish_xmlnode_t *element,
  721. void *parent)
  722. {
  723. clish_command_t *cmd = (clish_command_t *) parent;
  724. /* read the following text element */
  725. char *text = clish_xmlnode_get_all_content(element);
  726. if (text && *text) {
  727. /* store the action */
  728. clish_command__set_detail(cmd, text);
  729. }
  730. if (text)
  731. free(text);
  732. shell = shell; /* Happy compiler */
  733. return 0;
  734. }
  735. /* ------------------------------------------------------ */
  736. static int process_namespace(clish_shell_t *shell, clish_xmlnode_t *element,
  737. void *parent)
  738. {
  739. clish_view_t *v = (clish_view_t *)parent;
  740. clish_nspace_t *nspace = NULL;
  741. int res = -1;
  742. char *view = clish_xmlnode_fetch_attr(element, "ref");
  743. char *prefix = clish_xmlnode_fetch_attr(element, "prefix");
  744. char *prefix_help = clish_xmlnode_fetch_attr(element, "prefix_help");
  745. char *help = clish_xmlnode_fetch_attr(element, "help");
  746. char *completion = clish_xmlnode_fetch_attr(element, "completion");
  747. char *context_help = clish_xmlnode_fetch_attr(element, "context_help");
  748. char *inherit = clish_xmlnode_fetch_attr(element, "inherit");
  749. char *access = clish_xmlnode_fetch_attr(element, "access");
  750. /* Check syntax */
  751. if (!view) {
  752. fprintf(stderr, CLISH_XML_ERROR_ATTR("ref"));
  753. goto error;
  754. }
  755. clish_view_t *ref_view = clish_shell_find_view(shell, view);
  756. /* Don't include itself without prefix */
  757. if ((ref_view == v) && !prefix)
  758. goto process_namespace_end;
  759. nspace = clish_nspace_new(view);
  760. clish_view_insert_nspace(v, nspace);
  761. if (prefix) {
  762. clish_nspace__set_prefix(nspace, prefix);
  763. if (prefix_help)
  764. clish_nspace_create_prefix_cmd(nspace,
  765. "prefix",
  766. prefix_help);
  767. else
  768. clish_nspace_create_prefix_cmd(nspace,
  769. "prefix",
  770. "Prefix for the imported commands.");
  771. }
  772. if (help && lub_string_nocasecmp(help, "true") == 0)
  773. clish_nspace__set_help(nspace, BOOL_TRUE);
  774. else
  775. clish_nspace__set_help(nspace, BOOL_FALSE);
  776. if (completion && lub_string_nocasecmp(completion, "false") == 0)
  777. clish_nspace__set_completion(nspace, BOOL_FALSE);
  778. else
  779. clish_nspace__set_completion(nspace, BOOL_TRUE);
  780. if (context_help && lub_string_nocasecmp(context_help, "true") == 0)
  781. clish_nspace__set_context_help(nspace, BOOL_TRUE);
  782. else
  783. clish_nspace__set_context_help(nspace, BOOL_FALSE);
  784. if (inherit && lub_string_nocasecmp(inherit, "false") == 0)
  785. clish_nspace__set_inherit(nspace, BOOL_FALSE);
  786. else
  787. clish_nspace__set_inherit(nspace, BOOL_TRUE);
  788. if (access)
  789. clish_nspace__set_access(nspace, access);
  790. process_namespace_end:
  791. res = 0;
  792. error:
  793. clish_xml_release(view);
  794. clish_xml_release(prefix);
  795. clish_xml_release(prefix_help);
  796. clish_xml_release(help);
  797. clish_xml_release(completion);
  798. clish_xml_release(context_help);
  799. clish_xml_release(inherit);
  800. clish_xml_release(access);
  801. return res;
  802. }
  803. /* ------------------------------------------------------ */
  804. static int process_config(clish_shell_t *shell, clish_xmlnode_t *element,
  805. void *parent)
  806. {
  807. clish_command_t *cmd = (clish_command_t *)parent;
  808. clish_config_t *config;
  809. if (!cmd)
  810. return 0;
  811. config = clish_command__get_config(cmd);
  812. /* read the following text element */
  813. char *operation = clish_xmlnode_fetch_attr(element, "operation");
  814. char *priority = clish_xmlnode_fetch_attr(element, "priority");
  815. char *pattern = clish_xmlnode_fetch_attr(element, "pattern");
  816. char *file = clish_xmlnode_fetch_attr(element, "file");
  817. char *splitter = clish_xmlnode_fetch_attr(element, "splitter");
  818. char *seq = clish_xmlnode_fetch_attr(element, "sequence");
  819. char *unique = clish_xmlnode_fetch_attr(element, "unique");
  820. char *depth = clish_xmlnode_fetch_attr(element, "depth");
  821. if (operation && !lub_string_nocasecmp(operation, "unset"))
  822. clish_config__set_op(config, CLISH_CONFIG_UNSET);
  823. else if (operation && !lub_string_nocasecmp(operation, "none"))
  824. clish_config__set_op(config, CLISH_CONFIG_NONE);
  825. else if (operation && !lub_string_nocasecmp(operation, "dump"))
  826. clish_config__set_op(config, CLISH_CONFIG_DUMP);
  827. else {
  828. clish_config__set_op(config, CLISH_CONFIG_SET);
  829. /* The priority if no clearly specified */
  830. clish_config__set_priority(config, 0x7f00);
  831. }
  832. if (priority) {
  833. long val = 0;
  834. char *endptr;
  835. unsigned short pri;
  836. val = strtol(priority, &endptr, 0);
  837. if (endptr == priority)
  838. pri = 0;
  839. else if (val > 0xffff)
  840. pri = 0xffff;
  841. else if (val < 0)
  842. pri = 0;
  843. else
  844. pri = (unsigned short)val;
  845. clish_config__set_priority(config, pri);
  846. }
  847. if (pattern)
  848. clish_config__set_pattern(config, pattern);
  849. else
  850. clish_config__set_pattern(config, "^${__cmd}");
  851. if (file)
  852. clish_config__set_file(config, file);
  853. if (splitter && lub_string_nocasecmp(splitter, "false") == 0)
  854. clish_config__set_splitter(config, BOOL_FALSE);
  855. else
  856. clish_config__set_splitter(config, BOOL_TRUE);
  857. if (unique && lub_string_nocasecmp(unique, "false") == 0)
  858. clish_config__set_unique(config, BOOL_FALSE);
  859. else
  860. clish_config__set_unique(config, BOOL_TRUE);
  861. if (seq)
  862. clish_config__set_seq(config, seq);
  863. else
  864. /* The entries without sequence cannot be non-unique */
  865. clish_config__set_unique(config, BOOL_TRUE);
  866. if (depth)
  867. clish_config__set_depth(config, depth);
  868. clish_xml_release(operation);
  869. clish_xml_release(priority);
  870. clish_xml_release(pattern);
  871. clish_xml_release(file);
  872. clish_xml_release(splitter);
  873. clish_xml_release(seq);
  874. clish_xml_release(unique);
  875. clish_xml_release(depth);
  876. shell = shell; /* Happy compiler */
  877. return 0;
  878. }
  879. /* ------------------------------------------------------ */
  880. static int process_var(clish_shell_t *shell, clish_xmlnode_t *element,
  881. void *parent)
  882. {
  883. clish_var_t *var = NULL;
  884. int res = -1;
  885. char *name = clish_xmlnode_fetch_attr(element, "name");
  886. char *dynamic = clish_xmlnode_fetch_attr(element, "dynamic");
  887. char *value = clish_xmlnode_fetch_attr(element, "value");
  888. /* Check syntax */
  889. if (!name) {
  890. fprintf(stderr, CLISH_XML_ERROR_ATTR("name"));
  891. goto error;
  892. }
  893. /* Check if this var doesn't already exist */
  894. var = (clish_var_t *)lub_bintree_find(&shell->var_tree, name);
  895. if (var) {
  896. fprintf(stderr, CLISH_XML_ERROR_STR"Duplicate VAR name=\"%s\".\n", name);
  897. goto error;
  898. }
  899. /* Create var instance */
  900. var = clish_var_new(name);
  901. lub_bintree_insert(&shell->var_tree, var);
  902. if (dynamic && lub_string_nocasecmp(dynamic, "true") == 0)
  903. clish_var__set_dynamic(var, BOOL_TRUE);
  904. if (value)
  905. clish_var__set_value(var, value);
  906. res = process_children(shell, element, var);
  907. error:
  908. clish_xml_release(name);
  909. clish_xml_release(dynamic);
  910. clish_xml_release(value);
  911. parent = parent; /* Happy compiler */
  912. return res;
  913. }
  914. /* ------------------------------------------------------ */
  915. static int process_wdog(clish_shell_t *shell,
  916. clish_xmlnode_t *element, void *parent)
  917. {
  918. clish_view_t *v = (clish_view_t *)parent;
  919. clish_command_t *cmd = NULL;
  920. int res = -1;
  921. /* Check syntax */
  922. if (shell->wdog) {
  923. fprintf(stderr, CLISH_XML_ERROR_STR"WATCHDOG tag duplication.\n");
  924. goto error;
  925. }
  926. /* Create a command with NULL help */
  927. cmd = clish_view_new_command(v, "watchdog", NULL);
  928. clish_command__set_lock(cmd, BOOL_FALSE);
  929. /* Remember this command */
  930. shell->wdog = cmd;
  931. res = process_children(shell, element, cmd);
  932. error:
  933. return res;
  934. }
  935. /* ------------------------------------------------------ */
  936. static int process_hotkey(clish_shell_t *shell, clish_xmlnode_t *element,
  937. void *parent)
  938. {
  939. clish_view_t *v = (clish_view_t *)parent;
  940. int res = -1;
  941. char *key = clish_xmlnode_fetch_attr(element, "key");
  942. char *cmd = clish_xmlnode_fetch_attr(element, "cmd");
  943. /* Check syntax */
  944. if (!key) {
  945. fprintf(stderr, CLISH_XML_ERROR_ATTR("key"));
  946. goto error;
  947. }
  948. if (!cmd) {
  949. fprintf(stderr, CLISH_XML_ERROR_ATTR("cmd"));
  950. goto error;
  951. }
  952. clish_view_insert_hotkey(v, key, cmd);
  953. res = 0;
  954. error:
  955. clish_xml_release(key);
  956. clish_xml_release(cmd);
  957. shell = shell; /* Happy compiler */
  958. return res;
  959. }
  960. /* ------------------------------------------------------ */
  961. static int process_plugin(clish_shell_t *shell, clish_xmlnode_t *element,
  962. void *parent)
  963. {
  964. clish_plugin_t *plugin;
  965. char *file = clish_xmlnode_fetch_attr(element, "file");
  966. char *name = clish_xmlnode_fetch_attr(element, "name");
  967. char *alias = clish_xmlnode_fetch_attr(element, "alias");
  968. int res = -1;
  969. char *text;
  970. /* Check syntax */
  971. if (!name) {
  972. fprintf(stderr, CLISH_XML_ERROR_ATTR("name"));
  973. goto error;
  974. }
  975. plugin = clish_shell_find_plugin(shell, name);
  976. if (plugin) {
  977. fprintf(stderr,
  978. CLISH_XML_ERROR_STR"PLUGIN %s duplication.\n", name);
  979. goto error;
  980. }
  981. plugin = clish_plugin_new(name);
  982. lub_list_add(shell->plugins, plugin);
  983. if (alias && *alias)
  984. clish_plugin__set_alias(plugin, alias);
  985. if (file && *file)
  986. clish_plugin__set_file(plugin, file);
  987. /* Get PLUGIN body content */
  988. text = clish_xmlnode_get_all_content(element);
  989. if (text && *text)
  990. clish_plugin__set_conf(plugin, text);
  991. if (text)
  992. free(text);
  993. res = 0;
  994. error:
  995. clish_xml_release(file);
  996. clish_xml_release(name);
  997. clish_xml_release(alias);
  998. parent = parent; /* Happy compiler */
  999. return res;
  1000. }
  1001. /* ------------------------------------------------------ */
  1002. static int process_hook(clish_shell_t *shell, clish_xmlnode_t *element,
  1003. void *parent)
  1004. {
  1005. char *name = clish_xmlnode_fetch_attr(element, "name");
  1006. char *builtin = clish_xmlnode_fetch_attr(element, "builtin");
  1007. int res = -1;
  1008. int type = CLISH_SYM_TYPE_NONE;
  1009. /* Check syntax */
  1010. if (!name) {
  1011. fprintf(stderr, CLISH_XML_ERROR_ATTR("name"));
  1012. goto error;
  1013. }
  1014. /* Find out HOOK type */
  1015. if (!strcmp(name, "action"))
  1016. type = CLISH_SYM_TYPE_ACTION;
  1017. else if (!strcmp(name, "access"))
  1018. type = CLISH_SYM_TYPE_ACCESS;
  1019. else if (!strcmp(name, "config"))
  1020. type = CLISH_SYM_TYPE_CONFIG;
  1021. else if (!strcmp(name, "log"))
  1022. type = CLISH_SYM_TYPE_LOG;
  1023. if (CLISH_SYM_TYPE_NONE == type) {
  1024. fprintf(stderr, CLISH_XML_ERROR_STR"Unknown HOOK name %s.\n", name);
  1025. goto error;
  1026. }
  1027. /* Check duplicate HOOK tag */
  1028. if (shell->hooks_use[type]) {
  1029. fprintf(stderr,
  1030. CLISH_XML_ERROR_STR"HOOK %s duplication.\n", name);
  1031. goto error;
  1032. }
  1033. shell->hooks_use[type] = BOOL_TRUE;
  1034. clish_sym__set_name(shell->hooks[type], builtin);
  1035. res = 0;
  1036. error:
  1037. clish_xml_release(name);
  1038. clish_xml_release(builtin);
  1039. parent = parent; /* Happy compiler */
  1040. return res;
  1041. }
  1042. /* ------------------------------------------------------ */