shell_xml.c 33 KB

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