shell_xml.c 34 KB

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