shell_xml.c 34 KB

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