shell_xml.c 31 KB

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