shell_xml.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242
  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. /* reference the next view */
  452. clish_command__set_viewname(cmd, view);
  453. }
  454. /* define the view id which this command changes to */
  455. if (viewid)
  456. clish_command__set_viewid(cmd, viewid);
  457. /* lock field */
  458. if (lock && lub_string_nocasecmp(lock, "false") == 0)
  459. clish_command__set_lock(cmd, BOOL_FALSE);
  460. else
  461. clish_command__set_lock(cmd, BOOL_TRUE);
  462. /* interrupt field */
  463. if (interrupt && lub_string_nocasecmp(interrupt, "true") == 0)
  464. clish_command__set_interrupt(cmd, BOOL_TRUE);
  465. else
  466. clish_command__set_interrupt(cmd, BOOL_FALSE);
  467. /* Set alias */
  468. if (alias_name) {
  469. /* Check syntax */
  470. if (!alias_view) {
  471. fprintf(stderr, CLISH_XML_ERROR_STR"Can't find reference VIEW.\n");
  472. lub_string_free(alias_name);
  473. goto error;
  474. }
  475. if ((alias_view == v) && (!strcmp(alias_name, name))) {
  476. fprintf(stderr, CLISH_XML_ERROR_STR"The COMMAND with reference to itself.\n");
  477. lub_string_free(alias_name);
  478. goto error;
  479. }
  480. clish_command__set_alias(cmd, alias_name);
  481. clish_command__set_alias_view(cmd, alias_view);
  482. lub_string_free(alias_name);
  483. }
  484. //process_command_end:
  485. res = process_children(shell, element, cmd);
  486. error:
  487. clish_xml_release(access);
  488. clish_xml_release(name);
  489. clish_xml_release(help);
  490. clish_xml_release(view);
  491. clish_xml_release(viewid);
  492. clish_xml_release(escape_chars);
  493. clish_xml_release(args_name);
  494. clish_xml_release(args_help);
  495. clish_xml_release(lock);
  496. clish_xml_release(interrupt);
  497. clish_xml_release(ref);
  498. return res;
  499. }
  500. /* ------------------------------------------------------ */
  501. static int
  502. process_startup(clish_shell_t * shell, clish_xmlnode_t * element, void *parent)
  503. {
  504. clish_view_t *v = (clish_view_t *) parent;
  505. clish_command_t *cmd = NULL;
  506. int res = -1;
  507. char *view = clish_xmlnode_fetch_attr(element, "view");
  508. char *viewid = clish_xmlnode_fetch_attr(element, "viewid");
  509. char *default_shebang =
  510. clish_xmlnode_fetch_attr(element, "default_shebang");
  511. char *default_builtin =
  512. clish_xmlnode_fetch_attr(element, "default_builtin");
  513. char *timeout = clish_xmlnode_fetch_attr(element, "timeout");
  514. char *lock = clish_xmlnode_fetch_attr(element, "lock");
  515. char *interrupt = clish_xmlnode_fetch_attr(element, "interrupt");
  516. /* Check syntax */
  517. if (!view) {
  518. fprintf(stderr, CLISH_XML_ERROR_ATTR("view"));
  519. goto error;
  520. }
  521. if (shell->startup) {
  522. fprintf(stderr, CLISH_XML_ERROR_STR"STARTUP tag duplication.\n");
  523. goto error;
  524. }
  525. /* create a command with NULL help */
  526. cmd = clish_view_new_command(v, "startup", NULL);
  527. clish_command__set_lock(cmd, BOOL_FALSE);
  528. /* reference the next view */
  529. clish_command__set_viewname(cmd, view);
  530. /* define the view id which this command changes to */
  531. if (viewid)
  532. clish_command__set_viewid(cmd, viewid);
  533. if (default_shebang)
  534. clish_shell__set_default_shebang(shell, default_shebang);
  535. if (default_builtin)
  536. clish_sym__set_name(shell->hooks[CLISH_SYM_TYPE_ACTION],
  537. default_builtin);
  538. if (timeout)
  539. clish_shell__set_timeout(shell, atoi(timeout));
  540. /* lock field */
  541. if (lock && lub_string_nocasecmp(lock, "false") == 0)
  542. clish_command__set_lock(cmd, BOOL_FALSE);
  543. else
  544. clish_command__set_lock(cmd, BOOL_TRUE);
  545. /* interrupt field */
  546. if (interrupt && lub_string_nocasecmp(interrupt, "true") == 0)
  547. clish_command__set_interrupt(cmd, BOOL_TRUE);
  548. else
  549. clish_command__set_interrupt(cmd, BOOL_FALSE);
  550. /* remember this command */
  551. shell->startup = cmd;
  552. res = process_children(shell, element, cmd);
  553. error:
  554. clish_xml_release(view);
  555. clish_xml_release(viewid);
  556. clish_xml_release(default_shebang);
  557. clish_xml_release(default_builtin);
  558. clish_xml_release(timeout);
  559. clish_xml_release(lock);
  560. clish_xml_release(interrupt);
  561. return res;
  562. }
  563. /* ------------------------------------------------------ */
  564. static int
  565. process_param(clish_shell_t * shell, clish_xmlnode_t * element, void *parent)
  566. {
  567. clish_command_t *cmd = NULL;
  568. clish_param_t *p_param = NULL;
  569. clish_xmlnode_t *pelement;
  570. char *pname;
  571. int res = -1;
  572. pelement = clish_xmlnode_parent(element);
  573. pname = clish_xmlnode_get_all_name(pelement);
  574. if (pname && lub_string_nocasecmp(pname, "PARAM") == 0)
  575. p_param = (clish_param_t *)parent;
  576. else
  577. cmd = (clish_command_t *)parent;
  578. if (pname)
  579. free(pname);
  580. if (cmd || p_param) {
  581. char *name = clish_xmlnode_fetch_attr(element, "name");
  582. char *help = clish_xmlnode_fetch_attr(element, "help");
  583. char *ptype = clish_xmlnode_fetch_attr(element, "ptype");
  584. char *prefix = clish_xmlnode_fetch_attr(element, "prefix");
  585. char *defval = clish_xmlnode_fetch_attr(element, "default");
  586. char *mode = clish_xmlnode_fetch_attr(element, "mode");
  587. char *optional = clish_xmlnode_fetch_attr(element, "optional");
  588. char *order = clish_xmlnode_fetch_attr(element, "order");
  589. char *value = clish_xmlnode_fetch_attr(element, "value");
  590. char *hidden = clish_xmlnode_fetch_attr(element, "hidden");
  591. char *test = clish_xmlnode_fetch_attr(element, "test");
  592. char *completion = clish_xmlnode_fetch_attr(element, "completion");
  593. clish_param_t *param;
  594. clish_ptype_t *tmp = NULL;
  595. /* Check syntax */
  596. if (cmd && (cmd == shell->startup)) {
  597. fprintf(stderr, CLISH_XML_ERROR_STR"STARTUP can't contain PARAMs.\n");
  598. goto error;
  599. }
  600. if (!name) {
  601. fprintf(stderr, CLISH_XML_ERROR_ATTR("name"));
  602. goto error;
  603. }
  604. if (!help) {
  605. fprintf(stderr, CLISH_XML_ERROR_ATTR("help"));
  606. goto error;
  607. }
  608. if (!ptype) {
  609. fprintf(stderr, CLISH_XML_ERROR_ATTR("ptype"));
  610. goto error;
  611. }
  612. if (*ptype) {
  613. tmp = clish_shell_find_create_ptype(shell, ptype,
  614. NULL, NULL,
  615. CLISH_PTYPE_REGEXP,
  616. CLISH_PTYPE_NONE);
  617. }
  618. param = clish_param_new(name, help, tmp);
  619. /* If prefix is set clish will emulate old optional
  620. * command syntax over newer optional command mechanism.
  621. * It will create nested PARAM.
  622. */
  623. if (prefix) {
  624. const char *ptype_name = "__SUBCOMMAND";
  625. clish_param_t *opt_param = NULL;
  626. /* Create a ptype for prefix-named subcommand that
  627. * will contain the nested optional parameter. The
  628. * name of ptype is hardcoded. It's not good but
  629. * it's only the service ptype.
  630. */
  631. tmp = (clish_ptype_t *)lub_bintree_find(
  632. &shell->ptype_tree, ptype_name);
  633. if (!tmp)
  634. tmp = clish_shell_find_create_ptype(shell,
  635. ptype_name, "Option", "[^\\\\]+",
  636. CLISH_PTYPE_REGEXP, CLISH_PTYPE_NONE);
  637. opt_param = clish_param_new(prefix, help, tmp);
  638. clish_param__set_mode(opt_param,
  639. CLISH_PARAM_SUBCOMMAND);
  640. clish_param__set_optional(opt_param, BOOL_TRUE);
  641. if (test)
  642. clish_param__set_test(opt_param, test);
  643. /* add the parameter to the command */
  644. if (cmd)
  645. clish_command_insert_param(cmd, opt_param);
  646. /* add the parameter to the param */
  647. if (p_param)
  648. clish_param_insert_param(p_param, opt_param);
  649. /* Unset cmd and set parent param to opt_param */
  650. cmd = NULL;
  651. p_param = opt_param;
  652. }
  653. if (defval)
  654. clish_param__set_default(param, defval);
  655. if (hidden && lub_string_nocasecmp(hidden, "true") == 0)
  656. clish_param__set_hidden(param, BOOL_TRUE);
  657. else
  658. clish_param__set_hidden(param, BOOL_FALSE);
  659. if (mode) {
  660. if (lub_string_nocasecmp(mode, "switch") == 0) {
  661. clish_param__set_mode(param,
  662. CLISH_PARAM_SWITCH);
  663. /* Force hidden attribute */
  664. clish_param__set_hidden(param, BOOL_TRUE);
  665. } else if (lub_string_nocasecmp(mode, "subcommand") == 0)
  666. clish_param__set_mode(param,
  667. CLISH_PARAM_SUBCOMMAND);
  668. else
  669. clish_param__set_mode(param,
  670. CLISH_PARAM_COMMON);
  671. }
  672. if (optional && lub_string_nocasecmp(optional, "true") == 0)
  673. clish_param__set_optional(param, BOOL_TRUE);
  674. else
  675. clish_param__set_optional(param, BOOL_FALSE);
  676. if (order && lub_string_nocasecmp(order, "true") == 0)
  677. clish_param__set_order(param, BOOL_TRUE);
  678. else
  679. clish_param__set_order(param, BOOL_FALSE);
  680. if (value) {
  681. clish_param__set_value(param, value);
  682. /* Force mode to subcommand */
  683. clish_param__set_mode(param,
  684. CLISH_PARAM_SUBCOMMAND);
  685. }
  686. if (test && !prefix)
  687. clish_param__set_test(param, test);
  688. if (completion)
  689. clish_param__set_completion(param, completion);
  690. /* add the parameter to the command */
  691. if (cmd)
  692. clish_command_insert_param(cmd, param);
  693. /* add the parameter to the param */
  694. if (p_param)
  695. clish_param_insert_param(p_param, param);
  696. res = process_children(shell, element, param);
  697. error:
  698. clish_xml_release(name);
  699. clish_xml_release(help);
  700. clish_xml_release(ptype);
  701. clish_xml_release(prefix);
  702. clish_xml_release(defval);
  703. clish_xml_release(mode);
  704. clish_xml_release(optional);
  705. clish_xml_release(order);
  706. clish_xml_release(value);
  707. clish_xml_release(hidden);
  708. clish_xml_release(test);
  709. clish_xml_release(completion);
  710. }
  711. return res;
  712. }
  713. /* ------------------------------------------------------ */
  714. static int
  715. process_action(clish_shell_t *shell, clish_xmlnode_t *element, void *parent)
  716. {
  717. clish_action_t *action = NULL;
  718. char *builtin = clish_xmlnode_fetch_attr(element, "builtin");
  719. char *shebang = clish_xmlnode_fetch_attr(element, "shebang");
  720. clish_xmlnode_t *pelement = clish_xmlnode_parent(element);
  721. char *pname = clish_xmlnode_get_all_name(pelement);
  722. char *text;
  723. clish_sym_t *sym = NULL;
  724. if (pname && lub_string_nocasecmp(pname, "VAR") == 0)
  725. action = clish_var__get_action((clish_var_t *)parent);
  726. else
  727. action = clish_command__get_action((clish_command_t *)parent);
  728. if (pname)
  729. free(pname);
  730. text = clish_xmlnode_get_all_content(element);
  731. if (text && *text) {
  732. /* store the action */
  733. clish_action__set_script(action, text);
  734. }
  735. if (text)
  736. free(text);
  737. if (builtin)
  738. sym = clish_shell_add_unresolved_sym(shell, builtin,
  739. CLISH_SYM_TYPE_ACTION);
  740. else
  741. sym = shell->hooks[CLISH_SYM_TYPE_ACTION];
  742. clish_action__set_builtin(action, sym);
  743. if (shebang)
  744. clish_action__set_shebang(action, shebang);
  745. clish_xml_release(builtin);
  746. clish_xml_release(shebang);
  747. return 0;
  748. }
  749. /* ------------------------------------------------------ */
  750. static int
  751. process_detail(clish_shell_t * shell, clish_xmlnode_t * element, void *parent)
  752. {
  753. clish_command_t *cmd = (clish_command_t *) parent;
  754. /* read the following text element */
  755. char *text = clish_xmlnode_get_all_content(element);
  756. if (text && *text) {
  757. /* store the action */
  758. clish_command__set_detail(cmd, text);
  759. }
  760. if (text)
  761. free(text);
  762. return 0;
  763. }
  764. /* ------------------------------------------------------ */
  765. static int
  766. process_namespace(clish_shell_t * shell, clish_xmlnode_t * element, void *parent)
  767. {
  768. clish_view_t *v = (clish_view_t *) parent;
  769. clish_nspace_t *nspace = NULL;
  770. int res = -1;
  771. char *view = clish_xmlnode_fetch_attr(element, "ref");
  772. char *prefix = clish_xmlnode_fetch_attr(element, "prefix");
  773. char *prefix_help = clish_xmlnode_fetch_attr(element, "prefix_help");
  774. char *help = clish_xmlnode_fetch_attr(element, "help");
  775. char *completion = clish_xmlnode_fetch_attr(element, "completion");
  776. char *context_help = clish_xmlnode_fetch_attr(element, "context_help");
  777. char *inherit = clish_xmlnode_fetch_attr(element, "inherit");
  778. char *access = clish_xmlnode_fetch_attr(element, "access");
  779. // int allowed = 1;
  780. // if (access) {
  781. // allowed = 0;
  782. // if (shell->hooks->access_fn)
  783. // allowed = shell->hooks->access_fn(shell, access);
  784. // }
  785. // if (!allowed)
  786. // goto process_namespace_end;
  787. /* Check syntax */
  788. if (!view) {
  789. fprintf(stderr, CLISH_XML_ERROR_ATTR("ref"));
  790. goto error;
  791. }
  792. clish_view_t *ref_view = clish_shell_find_create_view(shell,
  793. view, NULL);
  794. /* Don't include itself without prefix */
  795. if ((ref_view == v) && !prefix)
  796. goto process_namespace_end;
  797. nspace = clish_nspace_new(ref_view);
  798. clish_view_insert_nspace(v, nspace);
  799. if (prefix) {
  800. clish_nspace__set_prefix(nspace, prefix);
  801. if (prefix_help)
  802. clish_nspace_create_prefix_cmd(nspace,
  803. "prefix",
  804. prefix_help);
  805. else
  806. clish_nspace_create_prefix_cmd(nspace,
  807. "prefix",
  808. "Prefix for the imported commands.");
  809. }
  810. if (help && lub_string_nocasecmp(help, "true") == 0)
  811. clish_nspace__set_help(nspace, BOOL_TRUE);
  812. else
  813. clish_nspace__set_help(nspace, BOOL_FALSE);
  814. if (completion && lub_string_nocasecmp(completion, "false") == 0)
  815. clish_nspace__set_completion(nspace, BOOL_FALSE);
  816. else
  817. clish_nspace__set_completion(nspace, BOOL_TRUE);
  818. if (context_help && lub_string_nocasecmp(context_help, "true") == 0)
  819. clish_nspace__set_context_help(nspace, BOOL_TRUE);
  820. else
  821. clish_nspace__set_context_help(nspace, BOOL_FALSE);
  822. if (inherit && lub_string_nocasecmp(inherit, "false") == 0)
  823. clish_nspace__set_inherit(nspace, BOOL_FALSE);
  824. else
  825. clish_nspace__set_inherit(nspace, BOOL_TRUE);
  826. process_namespace_end:
  827. res = 0;
  828. error:
  829. clish_xml_release(view);
  830. clish_xml_release(prefix);
  831. clish_xml_release(prefix_help);
  832. clish_xml_release(help);
  833. clish_xml_release(completion);
  834. clish_xml_release(context_help);
  835. clish_xml_release(inherit);
  836. clish_xml_release(access);
  837. return res;
  838. }
  839. /* ------------------------------------------------------ */
  840. static int
  841. process_config(clish_shell_t * shell, clish_xmlnode_t * element, void *parent)
  842. {
  843. clish_command_t *cmd = (clish_command_t *)parent;
  844. clish_config_t *config;
  845. if (!cmd)
  846. return 0;
  847. config = clish_command__get_config(cmd);
  848. /* read the following text element */
  849. char *operation = clish_xmlnode_fetch_attr(element, "operation");
  850. char *priority = clish_xmlnode_fetch_attr(element, "priority");
  851. char *pattern = clish_xmlnode_fetch_attr(element, "pattern");
  852. char *file = clish_xmlnode_fetch_attr(element, "file");
  853. char *splitter = clish_xmlnode_fetch_attr(element, "splitter");
  854. char *seq = clish_xmlnode_fetch_attr(element, "sequence");
  855. char *unique = clish_xmlnode_fetch_attr(element, "unique");
  856. char *depth = clish_xmlnode_fetch_attr(element, "depth");
  857. if (operation && !lub_string_nocasecmp(operation, "unset"))
  858. clish_config__set_op(config, CLISH_CONFIG_UNSET);
  859. else if (operation && !lub_string_nocasecmp(operation, "none"))
  860. clish_config__set_op(config, CLISH_CONFIG_NONE);
  861. else if (operation && !lub_string_nocasecmp(operation, "dump"))
  862. clish_config__set_op(config, CLISH_CONFIG_DUMP);
  863. else {
  864. clish_config__set_op(config, CLISH_CONFIG_SET);
  865. /* The priority if no clearly specified */
  866. clish_config__set_priority(config, 0x7f00);
  867. }
  868. if (priority) {
  869. long val = 0;
  870. char *endptr;
  871. unsigned short pri;
  872. val = strtol(priority, &endptr, 0);
  873. if (endptr == priority)
  874. pri = 0;
  875. else if (val > 0xffff)
  876. pri = 0xffff;
  877. else if (val < 0)
  878. pri = 0;
  879. else
  880. pri = (unsigned short)val;
  881. clish_config__set_priority(config, pri);
  882. }
  883. if (pattern)
  884. clish_config__set_pattern(config, pattern);
  885. else
  886. clish_config__set_pattern(config, "^${__cmd}");
  887. if (file)
  888. clish_config__set_file(config, file);
  889. if (splitter && lub_string_nocasecmp(splitter, "false") == 0)
  890. clish_config__set_splitter(config, BOOL_FALSE);
  891. else
  892. clish_config__set_splitter(config, BOOL_TRUE);
  893. if (unique && lub_string_nocasecmp(unique, "false") == 0)
  894. clish_config__set_unique(config, BOOL_FALSE);
  895. else
  896. clish_config__set_unique(config, BOOL_TRUE);
  897. if (seq)
  898. clish_config__set_seq(config, seq);
  899. else
  900. /* The entries without sequence cannot be non-unique */
  901. clish_config__set_unique(config, BOOL_TRUE);
  902. if (depth)
  903. clish_config__set_depth(config, depth);
  904. clish_xml_release(operation);
  905. clish_xml_release(priority);
  906. clish_xml_release(pattern);
  907. clish_xml_release(file);
  908. clish_xml_release(splitter);
  909. clish_xml_release(seq);
  910. clish_xml_release(unique);
  911. clish_xml_release(depth);
  912. return 0;
  913. }
  914. /* ------------------------------------------------------ */
  915. static int
  916. process_var(clish_shell_t * shell, clish_xmlnode_t * element, void *parent)
  917. {
  918. clish_var_t *var = NULL;
  919. int res = -1;
  920. char *name = clish_xmlnode_fetch_attr(element, "name");
  921. char *dynamic = clish_xmlnode_fetch_attr(element, "dynamic");
  922. char *value = clish_xmlnode_fetch_attr(element, "value");
  923. /* Check syntax */
  924. if (!name) {
  925. fprintf(stderr, CLISH_XML_ERROR_ATTR("name"));
  926. goto error;
  927. }
  928. /* Check if this var doesn't already exist */
  929. var = (clish_var_t *)lub_bintree_find(&shell->var_tree, name);
  930. if (var) {
  931. fprintf(stderr, CLISH_XML_ERROR_STR"Duplicate VAR name=\"%s\".\n", name);
  932. goto error;
  933. }
  934. /* Create var instance */
  935. var = clish_var_new(name);
  936. lub_bintree_insert(&shell->var_tree, var);
  937. if (dynamic && lub_string_nocasecmp(dynamic, "true") == 0)
  938. clish_var__set_dynamic(var, BOOL_TRUE);
  939. if (value)
  940. clish_var__set_value(var, value);
  941. res = process_children(shell, element, var);
  942. error:
  943. clish_xml_release(name);
  944. clish_xml_release(dynamic);
  945. clish_xml_release(value);
  946. return res;
  947. }
  948. /* ------------------------------------------------------ */
  949. static int
  950. process_wdog(clish_shell_t *shell,
  951. clish_xmlnode_t *element, void *parent)
  952. {
  953. clish_view_t *v = (clish_view_t *)parent;
  954. clish_command_t *cmd = NULL;
  955. int res = -1;
  956. /* Check syntax */
  957. if (shell->wdog) {
  958. fprintf(stderr, CLISH_XML_ERROR_STR"WATCHDOG tag duplication.\n");
  959. goto error;
  960. }
  961. /* Create a command with NULL help */
  962. cmd = clish_view_new_command(v, "watchdog", NULL);
  963. clish_command__set_lock(cmd, BOOL_FALSE);
  964. /* Remember this command */
  965. shell->wdog = cmd;
  966. res = process_children(shell, element, cmd);
  967. error:
  968. return res;
  969. }
  970. /* ------------------------------------------------------ */
  971. static int
  972. process_hotkey(clish_shell_t *shell, clish_xmlnode_t* element, void *parent)
  973. {
  974. clish_view_t *v = (clish_view_t *)parent;
  975. int res = -1;
  976. char *key = clish_xmlnode_fetch_attr(element, "key");
  977. char *cmd = clish_xmlnode_fetch_attr(element, "cmd");
  978. /* Check syntax */
  979. if (!key) {
  980. fprintf(stderr, CLISH_XML_ERROR_ATTR("key"));
  981. goto error;
  982. }
  983. if (!cmd) {
  984. fprintf(stderr, CLISH_XML_ERROR_ATTR("cmd"));
  985. goto error;
  986. }
  987. clish_view_insert_hotkey(v, key, cmd);
  988. res = 0;
  989. error:
  990. clish_xml_release(key);
  991. clish_xml_release(cmd);
  992. return res;
  993. }
  994. /* ------------------------------------------------------ */
  995. static int
  996. process_plugin(clish_shell_t *shell, clish_xmlnode_t* element, void *parent)
  997. {
  998. clish_plugin_t *plugin;
  999. char *file = clish_xmlnode_fetch_attr(element, "file");
  1000. char *name = clish_xmlnode_fetch_attr(element, "name");
  1001. int res = -1;
  1002. char *text;
  1003. /* Check syntax */
  1004. if (!file) {
  1005. fprintf(stderr, CLISH_XML_ERROR_ATTR("file"));
  1006. goto error;
  1007. }
  1008. plugin = clish_plugin_new(file, name); /* Really - the name is alias */
  1009. lub_list_add(shell->plugins, plugin);
  1010. /* Get PLUGIN body content */
  1011. text = clish_xmlnode_get_all_content(element);
  1012. if (text && *text)
  1013. clish_plugin__set_conf(plugin, text);
  1014. if (text)
  1015. free(text);
  1016. res = 0;
  1017. error:
  1018. clish_xml_release(file);
  1019. clish_xml_release(name);
  1020. return res;
  1021. }
  1022. /* ------------------------------------------------------ */
  1023. static int
  1024. process_hook(clish_shell_t *shell, clish_xmlnode_t* element, void *parent)
  1025. {
  1026. char *name = clish_xmlnode_fetch_attr(element, "name");
  1027. char *builtin = clish_xmlnode_fetch_attr(element, "builtin");
  1028. int res = -1;
  1029. int type = CLISH_SYM_TYPE_NONE;
  1030. /* Check syntax */
  1031. if (!name) {
  1032. fprintf(stderr, CLISH_XML_ERROR_ATTR("name"));
  1033. goto error;
  1034. }
  1035. /* Find out HOOK type */
  1036. if (!strcmp(name, "init"))
  1037. type = CLISH_SYM_TYPE_INIT;
  1038. else if (!strcmp(name, "fini"))
  1039. type = CLISH_SYM_TYPE_FINI;
  1040. else if (!strcmp(name, "access"))
  1041. type = CLISH_SYM_TYPE_ACCESS;
  1042. else if (!strcmp(name, "config"))
  1043. type = CLISH_SYM_TYPE_CONFIG;
  1044. else if (!strcmp(name, "log"))
  1045. type = CLISH_SYM_TYPE_LOG;
  1046. if (CLISH_SYM_TYPE_NONE == type) {
  1047. fprintf(stderr, CLISH_XML_ERROR_STR"Unknown HOOK name %s.\n", name);
  1048. goto error;
  1049. }
  1050. if (builtin) {
  1051. if (shell->hooks[type])
  1052. clish_sym__set_name(shell->hooks[type], builtin);
  1053. else
  1054. shell->hooks[type] = clish_sym_new(builtin, NULL, type);
  1055. } else {
  1056. if (shell->hooks[type])
  1057. clish_sym_free(shell->hooks[type]);
  1058. shell->hooks[type] = NULL;
  1059. }
  1060. res = 0;
  1061. error:
  1062. clish_xml_release(name);
  1063. clish_xml_release(builtin);
  1064. return res;
  1065. }
  1066. /* ------------------------------------------------------ */