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