shell_xml.c 31 KB

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