shell_xml.c 33 KB

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