load.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. /** @file load.c
  2. * @brief Common part for XML parsing.
  3. *
  4. * Different XML parsing engines can provide a functions in a form of
  5. * standardized API. This code uses this API and parses XML to kscheme.
  6. */
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <assert.h>
  10. #include <errno.h>
  11. #include <sys/types.h>
  12. #include <dirent.h>
  13. #include <faux/faux.h>
  14. #include <faux/str.h>
  15. #include <faux/error.h>
  16. #include <klish/kscheme.h>
  17. #include <klish/ischeme.h>
  18. #include <klish/kxml.h>
  19. #define TAG "XML"
  20. typedef bool_t (kxml_process_fn)(const kxml_node_t *element,
  21. void *parent, faux_error_t *error);
  22. static kxml_process_fn
  23. process_action,
  24. process_param,
  25. process_command,
  26. process_view,
  27. process_ptype,
  28. process_plugin,
  29. process_klish;
  30. // Different TAGs types
  31. typedef enum {
  32. KTAG_NONE,
  33. KTAG_ACTION,
  34. KTAG_PARAM,
  35. KTAG_COMMAND,
  36. KTAG_VIEW,
  37. KTAG_PTYPE,
  38. KTAG_PLUGIN,
  39. KTAG_KLISH,
  40. KTAG_MAX
  41. } ktags_e;
  42. static const char *kxml_tags[] = {
  43. NULL,
  44. "ACTION",
  45. "PARAM",
  46. "COMMAND",
  47. "VIEW",
  48. "PTYPE",
  49. "PLUGIN",
  50. "KLISH"
  51. };
  52. static kxml_process_fn *kxml_handlers[] = {
  53. NULL,
  54. process_action,
  55. process_param,
  56. process_command,
  57. process_view,
  58. process_ptype,
  59. process_plugin,
  60. process_klish
  61. };
  62. static ktags_e kxml_node_tag(const kxml_node_t *node)
  63. {
  64. ktags_e tag = KTAG_NONE;
  65. char *name = NULL;
  66. if (!node)
  67. return KTAG_NONE;
  68. if (kxml_node_type(node) != KXML_NODE_ELM)
  69. return KTAG_NONE;
  70. name = kxml_node_name(node);
  71. if (!name)
  72. return KTAG_NONE; // Strange case
  73. for (tag = KTAG_NONE; tag < KTAG_MAX; tag++) {
  74. if (faux_str_casecmp(name, kxml_tags[tag]))
  75. break;
  76. }
  77. kxml_node_name_free(name);
  78. if (tag >= KTAG_MAX)
  79. return KTAG_NONE;
  80. return tag;
  81. }
  82. static kxml_process_fn *kxml_node_handler(const kxml_node_t *node)
  83. {
  84. return kxml_handlers[kxml_node_tag(node)];
  85. }
  86. /** @brief Reads an element from the XML stream and processes it.
  87. */
  88. static bool_t process_node(const kxml_node_t *node, void *parent, faux_error_t *error)
  89. {
  90. kxml_process_fn *handler = kxml_node_handler(node);
  91. if (!handler)
  92. return BOOL_TRUE; // Unknown element
  93. return handler(node, parent, error);
  94. }
  95. static bool_t kxml_load_file(kscheme_t *scheme, const char *filename,
  96. faux_error_t *error)
  97. {
  98. kxml_doc_t *doc = NULL;
  99. kxml_node_t *root = NULL;
  100. bool_t r = BOOL_FALSE;
  101. if (!scheme)
  102. return BOOL_FALSE;
  103. if (!filename)
  104. return BOOL_FALSE;
  105. doc = kxml_doc_read(filename);
  106. if (!kxml_doc_is_valid(doc)) {
  107. /* int errcaps = kxml_doc_error_caps(doc);
  108. printf("Unable to open file '%s'", filename);
  109. if ((errcaps & kxml_ERR_LINE) == kxml_ERR_LINE)
  110. printf(", at line %d", kxml_doc_err_line(doc));
  111. if ((errcaps & kxml_ERR_COL) == kxml_ERR_COL)
  112. printf(", at column %d", kxml_doc_err_col(doc));
  113. if ((errcaps & kxml_ERR_DESC) == kxml_ERR_DESC)
  114. printf(", message is %s", kxml_doc_err_msg(doc));
  115. printf("\n");
  116. */ kxml_doc_release(doc);
  117. return BOOL_FALSE;
  118. }
  119. root = kxml_doc_root(doc);
  120. r = process_node(root, scheme, error);
  121. kxml_doc_release(doc);
  122. if (!r) {
  123. faux_error_sprintf(error, TAG": Illegal file %s", filename);
  124. return BOOL_FALSE;
  125. }
  126. return BOOL_TRUE;
  127. }
  128. /** @brief Default path to get XML files from.
  129. */
  130. const char *default_path = "/etc/klish;~/.klish";
  131. static const char *path_separators = ":;";
  132. kscheme_t *kxml_load_scheme(const char *xml_path, faux_error_t *error)
  133. {
  134. kscheme_t *scheme = NULL;
  135. const char *path = xml_path;
  136. char *realpath = NULL;
  137. char *fn = NULL;
  138. char *saveptr = NULL;
  139. bool_t ret = BOOL_TRUE;
  140. // New kscheme instance
  141. scheme = kscheme_new();
  142. if (!scheme)
  143. return NULL;
  144. // Use the default path if not specified
  145. if (!path)
  146. path = default_path;
  147. realpath = faux_expand_tilde(path);
  148. // Loop through each directory
  149. for (fn = strtok_r(realpath, path_separators, &saveptr);
  150. fn; fn = strtok_r(NULL, path_separators, &saveptr)) {
  151. DIR *dir = NULL;
  152. struct dirent *entry = NULL;
  153. // Regular file
  154. if (faux_isfile(fn)) {
  155. if (!kxml_load_file(scheme, fn, error)) {
  156. ret = BOOL_FALSE;
  157. continue;
  158. }
  159. }
  160. // Search this directory for any XML files
  161. dir = opendir(fn);
  162. if (!dir)
  163. continue;
  164. for (entry = readdir(dir); entry; entry = readdir(dir)) {
  165. const char *extension = strrchr(entry->d_name, '.');
  166. char *filename = NULL;
  167. // Check the filename
  168. if (!extension || strcmp(".xml", extension))
  169. continue;
  170. filename = faux_str_sprintf("%s/%s", fn, entry->d_name);
  171. if (!kxml_load_file(scheme, filename, error)) {
  172. ret = BOOL_FALSE;
  173. continue;
  174. }
  175. faux_str_free(filename);
  176. }
  177. closedir(dir);
  178. }
  179. faux_str_free(realpath);
  180. if (!ret) { // Some errors while XML parsing
  181. kscheme_free(scheme);
  182. return NULL;
  183. }
  184. return scheme;
  185. }
  186. /** @brief Iterate through element's children.
  187. */
  188. static bool_t process_children(const kxml_node_t *element, void *parent,
  189. faux_error_t *error)
  190. {
  191. const kxml_node_t *node = NULL;
  192. while ((node = kxml_node_next_child(element, node)) != NULL) {
  193. bool_t res = BOOL_FALSE;
  194. res = process_node(node, parent, error);
  195. if (!res)
  196. return res;
  197. }
  198. return BOOL_TRUE;
  199. }
  200. static bool_t process_klish(const kxml_node_t *element, void *parent,
  201. faux_error_t *error)
  202. {
  203. return process_children(element, parent, error);
  204. }
  205. static bool_t process_view(const kxml_node_t *element, void *parent,
  206. faux_error_t *error)
  207. {
  208. iview_t iview = {};
  209. kview_t *view = NULL;
  210. bool_t res = BOOL_FALSE;
  211. ktags_e parent_tag = kxml_node_tag(kxml_node_parent(element));
  212. iview.name = kxml_node_attr(element, "name");
  213. view = iview_load(&iview, error);
  214. if (!view)
  215. goto err;
  216. if (parent_tag != KTAG_KLISH) {
  217. faux_error_add(error, TAG": Only KLISH tag can contain VIEW tag");
  218. return BOOL_FALSE;
  219. }
  220. if (!kscheme_add_view((kscheme_t *)parent, view)) {
  221. faux_error_sprintf(error, TAG": Can't add VIEW \"%s\"",
  222. kview_name(view));
  223. kview_free(view);
  224. goto err;
  225. }
  226. if (!process_children(element, view, error))
  227. goto err;
  228. res = BOOL_TRUE;
  229. err:
  230. kxml_node_attr_free(iview.name);
  231. return res;
  232. }
  233. static bool_t process_ptype(const kxml_node_t *element, void *parent,
  234. faux_error_t *error)
  235. {
  236. iptype_t iptype = {};
  237. kptype_t *ptype = NULL;
  238. bool_t res = BOOL_FALSE;
  239. ktags_e parent_tag = kxml_node_tag(kxml_node_parent(element));
  240. iptype.name = kxml_node_attr(element, "name");
  241. iptype.help = kxml_node_attr(element, "help");
  242. ptype = iptype_load(&iptype, error);
  243. if (!ptype)
  244. goto err;
  245. if (parent_tag != KTAG_KLISH) {
  246. faux_error_add(error, TAG": Only KLISH tag can contain PTYPE tag");
  247. return BOOL_FALSE;
  248. }
  249. if (!kscheme_add_ptype((kscheme_t *)parent, ptype)) {
  250. faux_error_sprintf(error, TAG": Can't add PTYPE \"%s\"",
  251. kptype_name(ptype));
  252. kptype_free(ptype);
  253. goto err;
  254. }
  255. if (!process_children(element, ptype, error))
  256. goto err;
  257. res = BOOL_TRUE;
  258. err:
  259. kxml_node_attr_free(iptype.name);
  260. kxml_node_attr_free(iptype.help);
  261. return res;
  262. }
  263. static bool_t process_plugin(const kxml_node_t *element, void *parent,
  264. faux_error_t *error)
  265. {
  266. iplugin_t iplugin = {};
  267. kplugin_t *plugin = NULL;
  268. bool_t res = BOOL_FALSE;
  269. ktags_e parent_tag = kxml_node_tag(kxml_node_parent(element));
  270. iplugin.name = kxml_node_attr(element, "name");
  271. iplugin.id = kxml_node_attr(element, "id");
  272. iplugin.file = kxml_node_attr(element, "file");
  273. iplugin.global = kxml_node_attr(element, "global");
  274. iplugin.conf = kxml_node_content(element);
  275. plugin = iplugin_load(&iplugin, error);
  276. if (!plugin)
  277. goto err;
  278. if (parent_tag != KTAG_KLISH) {
  279. faux_error_add(error, TAG": Only KLISH tag can contain PLUGIN tag");
  280. return BOOL_FALSE;
  281. }
  282. if (!kscheme_add_plugin((kscheme_t *)parent, plugin)) {
  283. faux_error_sprintf(error, TAG": Can't add PLUGIN \"%s\"",
  284. kplugin_name(plugin));
  285. kplugin_free(plugin);
  286. goto err;
  287. }
  288. if (!process_children(element, plugin, error))
  289. goto err;
  290. res = BOOL_TRUE;
  291. err:
  292. kxml_node_attr_free(iplugin.name);
  293. kxml_node_attr_free(iplugin.id);
  294. kxml_node_attr_free(iplugin.file);
  295. kxml_node_attr_free(iplugin.global);
  296. kxml_node_content_free(iplugin.conf);
  297. return res;
  298. }
  299. static bool_t process_param(const kxml_node_t *element, void *parent,
  300. faux_error_t *error)
  301. {
  302. iparam_t iparam = {};
  303. kparam_t *param = NULL;
  304. bool_t res = BOOL_FALSE;
  305. ktags_e parent_tag = kxml_node_tag(kxml_node_parent(element));
  306. iparam.name = kxml_node_attr(element, "name");
  307. iparam.help = kxml_node_attr(element, "help");
  308. iparam.ptype = kxml_node_attr(element, "ptype");
  309. param = iparam_load(&iparam, error);
  310. if (!param)
  311. goto err;
  312. if (parent_tag != KTAG_COMMAND) {
  313. faux_error_add(error, TAG": Only COMMAND tag can contain PARAM tag");
  314. return BOOL_FALSE;
  315. }
  316. if (!kcommand_add_param((kcommand_t *)parent, param)) {
  317. faux_error_sprintf(error, TAG": Can't add PARAM \"%s\"",
  318. kparam_name(param));
  319. kparam_free(param);
  320. goto err;
  321. }
  322. if (!process_children(element, param, error))
  323. goto err;
  324. res = BOOL_TRUE;
  325. err:
  326. kxml_node_attr_free(iparam.name);
  327. kxml_node_attr_free(iparam.help);
  328. kxml_node_attr_free(iparam.ptype);
  329. return res;
  330. }
  331. static bool_t process_command(const kxml_node_t *element, void *parent,
  332. faux_error_t *error)
  333. {
  334. icommand_t icommand = {};
  335. kcommand_t *command = NULL;
  336. bool_t res = BOOL_FALSE;
  337. ktags_e parent_tag = kxml_node_tag(kxml_node_parent(element));
  338. icommand.name = kxml_node_attr(element, "name");
  339. icommand.help = kxml_node_attr(element, "help");
  340. command = icommand_load(&icommand, error);
  341. if (!command)
  342. goto err;
  343. if (parent_tag != KTAG_VIEW) {
  344. faux_error_add(error, TAG": Only VIEW tag can contain COMMAND tag");
  345. return BOOL_FALSE;
  346. }
  347. if (!kview_add_command((kview_t *)parent, command)) {
  348. faux_error_sprintf(error, TAG": Can't add COMMAND \"%s\"",
  349. kcommand_name(command));
  350. kcommand_free(command);
  351. goto err;
  352. }
  353. if (!process_children(element, command, error))
  354. goto err;
  355. res = BOOL_TRUE;
  356. err:
  357. kxml_node_attr_free(icommand.name);
  358. kxml_node_attr_free(icommand.help);
  359. return res;
  360. }
  361. static bool_t process_action(const kxml_node_t *element, void *parent,
  362. faux_error_t *error)
  363. {
  364. iaction_t iaction = {};
  365. kaction_t *action = NULL;
  366. bool_t res = BOOL_FALSE;
  367. ktags_e parent_tag = kxml_node_tag(kxml_node_parent(element));
  368. iaction.sym = kxml_node_attr(element, "sym");
  369. iaction.lock = kxml_node_attr(element, "lock");
  370. iaction.interrupt = kxml_node_attr(element, "interrupt");
  371. iaction.interactive = kxml_node_attr(element, "interactive");
  372. iaction.exec_on = kxml_node_attr(element, "exec_on");
  373. iaction.update_retcode = kxml_node_attr(element, "update_retcode");
  374. iaction.script = kxml_node_content(element);
  375. action = iaction_load(&iaction, error);
  376. if (!action)
  377. goto err;
  378. if (KTAG_COMMAND == parent_tag) {
  379. kcommand_t *command = (kcommand_t *)parent;
  380. if (!kcommand_add_action(command, action)) {
  381. faux_error_sprintf(error,
  382. TAG": Can't add ACTION #%d to COMMAND \"%s\"",
  383. kcommand_actions_len(command) + 1,
  384. kcommand_name(command));
  385. kaction_free(action);
  386. goto err;
  387. }
  388. } else if (KTAG_PTYPE == parent_tag) {
  389. kptype_t *ptype = (kptype_t *)parent;
  390. if (!kptype_add_action(ptype, action)) {
  391. faux_error_sprintf(error,
  392. TAG": Can't add ACTION #%d to PTYPE \"%s\"",
  393. kptype_actions_len(ptype) + 1,
  394. kptype_name(ptype));
  395. kaction_free(action);
  396. goto err;
  397. }
  398. } else {
  399. faux_error_add(error,
  400. TAG": Only COMMAND, PTYPE tags can contain ACTION tag");
  401. return BOOL_FALSE;
  402. }
  403. if (!process_children(element, action, error))
  404. goto err;
  405. res = BOOL_TRUE;
  406. err:
  407. kxml_node_attr_free(iaction.sym);
  408. kxml_node_attr_free(iaction.lock);
  409. kxml_node_attr_free(iaction.interrupt);
  410. kxml_node_attr_free(iaction.interactive);
  411. kxml_node_attr_free(iaction.exec_on);
  412. kxml_node_attr_free(iaction.update_retcode);
  413. kxml_node_content_free(iaction.script);
  414. return res;
  415. }