ientry.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <assert.h>
  5. #include <faux/str.h>
  6. #include <faux/list.h>
  7. #include <faux/conv.h>
  8. #include <klish/khelper.h>
  9. #include <klish/ientry.h>
  10. #include <klish/kentry.h>
  11. #define TAG "ENTRY"
  12. bool_t ientry_parse(const ientry_t *info, kentry_t *entry, faux_error_t *error)
  13. {
  14. bool_t retcode = BOOL_TRUE;
  15. if (!info)
  16. return BOOL_FALSE;
  17. if (!entry)
  18. return BOOL_FALSE;
  19. // Help
  20. if (!faux_str_is_empty(info->help)) {
  21. if (!kentry_set_help(entry, info->help)) {
  22. faux_error_add(error, TAG": Illegal 'help' attribute");
  23. retcode = BOOL_FALSE;
  24. }
  25. }
  26. // Container
  27. if (!faux_str_is_empty(info->container)) {
  28. bool_t b = BOOL_FALSE;
  29. if (!faux_conv_str2bool(info->container, &b) ||
  30. !kentry_set_container(entry, b)) {
  31. faux_error_add(error, TAG": Illegal 'container' attribute");
  32. retcode = BOOL_FALSE;
  33. }
  34. }
  35. // Mode
  36. if (!faux_str_is_empty(info->mode)) {
  37. kentry_mode_e mode = KENTRY_MODE_NONE;
  38. if (!faux_str_casecmp(info->mode, "sequence"))
  39. mode = KENTRY_MODE_SEQUENCE;
  40. else if (!faux_str_casecmp(info->mode, "switch"))
  41. mode = KENTRY_MODE_SWITCH;
  42. else if (!faux_str_casecmp(info->mode, "empty"))
  43. mode = KENTRY_MODE_EMPTY;
  44. if ((KENTRY_MODE_NONE == mode) || !kentry_set_mode(entry, mode)) {
  45. faux_error_add(error, TAG": Illegal 'mode' attribute");
  46. retcode = BOOL_FALSE;
  47. }
  48. }
  49. // Min occurs
  50. if (!faux_str_is_empty(info->min)) {
  51. unsigned int i = 0;
  52. if (!faux_conv_atoui(info->min, &i, 0) ||
  53. !kentry_set_min(entry, (size_t)i)) {
  54. faux_error_add(error, TAG": Illegal 'min' attribute");
  55. retcode = BOOL_FALSE;
  56. }
  57. }
  58. // Max occurs
  59. if (!faux_str_is_empty(info->max)) {
  60. unsigned int i = 0;
  61. if (!faux_conv_atoui(info->max, &i, 0) ||
  62. !kentry_set_max(entry, (size_t)i)) {
  63. faux_error_add(error, TAG": Illegal 'max' attribute");
  64. retcode = BOOL_FALSE;
  65. }
  66. }
  67. // Ptype string
  68. if (!faux_str_is_empty(info->ptype)) {
  69. if (!kentry_set_ptype_str(entry, info->ptype)) {
  70. faux_error_add(error, TAG": Illegal 'ptype' attribute");
  71. retcode = BOOL_FALSE;
  72. }
  73. }
  74. // Ref string
  75. if (!faux_str_is_empty(info->ref)) {
  76. if (!kentry_set_ref_str(entry, info->ref)) {
  77. faux_error_add(error, TAG": Illegal 'ref' attribute");
  78. retcode = BOOL_FALSE;
  79. }
  80. }
  81. // Value
  82. if (!faux_str_is_empty(info->value)) {
  83. if (!kentry_set_value(entry, info->value)) {
  84. faux_error_add(error, TAG": Illegal 'value' attribute");
  85. retcode = BOOL_FALSE;
  86. }
  87. }
  88. // Restore
  89. if (!faux_str_is_empty(info->restore)) {
  90. bool_t b = BOOL_FALSE;
  91. if (!faux_conv_str2bool(info->restore, &b) ||
  92. !kentry_set_restore(entry, b)) {
  93. faux_error_add(error, TAG": Illegal 'restore' attribute");
  94. retcode = BOOL_FALSE;
  95. }
  96. }
  97. // Order
  98. if (!faux_str_is_empty(info->order)) {
  99. bool_t b = BOOL_FALSE;
  100. if (!faux_conv_str2bool(info->order, &b) ||
  101. !kentry_set_order(entry, b)) {
  102. faux_error_add(error, TAG": Illegal 'order' attribute");
  103. retcode = BOOL_FALSE;
  104. }
  105. }
  106. // Filter
  107. if (!faux_str_is_empty(info->filter)) {
  108. bool_t b = BOOL_FALSE;
  109. if (!faux_conv_str2bool(info->filter, &b) ||
  110. !kentry_set_filter(entry, b)) {
  111. faux_error_add(error, TAG": Illegal 'filter' attribute");
  112. retcode = BOOL_FALSE;
  113. }
  114. }
  115. return retcode;
  116. }
  117. bool_t ientry_parse_nested(const ientry_t *ientry, kentry_t *kentry,
  118. faux_error_t *error)
  119. {
  120. bool_t retval = BOOL_TRUE;
  121. if (!kentry || !ientry) {
  122. faux_error_add(error, TAG": Internal error");
  123. return BOOL_FALSE;
  124. }
  125. // ENTRY list
  126. // ENTRYs can be duplicate. Duplicated ENTRY will add nested
  127. // elements to existent ENTRY. Also it can overwrite ENTRY attributes.
  128. // So there is no special rule which attribute value will be "on top".
  129. // It's a random. Technically later ENTRYs will rewrite previous
  130. // values.
  131. if (ientry->entrys) {
  132. ientry_t **p_ientry = NULL;
  133. for (p_ientry = *ientry->entrys; *p_ientry; p_ientry++) {
  134. kentry_t *nkentry = NULL;
  135. ientry_t *nientry = *p_ientry;
  136. const char *entry_name = nientry->name;
  137. if (entry_name)
  138. nkentry = kentry_find_entry(kentry, entry_name);
  139. // ENTRY already exists
  140. if (nkentry) {
  141. if (!ientry_parse(nientry, nkentry, error)) {
  142. retval = BOOL_FALSE;
  143. continue;
  144. }
  145. if (!ientry_parse_nested(nientry, nkentry,
  146. error)) {
  147. retval = BOOL_FALSE;
  148. continue;
  149. }
  150. continue;
  151. }
  152. // New ENTRY
  153. nkentry = ientry_load(nientry, error);
  154. if (!nkentry) {
  155. retval = BOOL_FALSE;
  156. continue;
  157. }
  158. kentry_set_parent(nkentry, kentry); // Set parent entry
  159. if (!kentry_add_entrys(kentry, nkentry)) {
  160. faux_error_sprintf(error,
  161. TAG": Can't add ENTRY \"%s\"",
  162. kentry_name(nkentry));
  163. kentry_free(nkentry);
  164. retval = BOOL_FALSE;
  165. continue;
  166. }
  167. }
  168. }
  169. // ACTION list
  170. if (ientry->actions) {
  171. iaction_t **p_iaction = NULL;
  172. for (p_iaction = *ientry->actions; *p_iaction; p_iaction++) {
  173. kaction_t *kaction = NULL;
  174. iaction_t *iaction = *p_iaction;
  175. kaction = iaction_load(iaction, error);
  176. if (!kaction) {
  177. retval = BOOL_FALSE;
  178. continue;
  179. }
  180. if (!kentry_add_actions(kentry, kaction)) {
  181. faux_error_sprintf(error,
  182. TAG": Can't add ACTION #%d",
  183. kentry_actions_len(kentry) + 1);
  184. kaction_free(kaction);
  185. retval = BOOL_FALSE;
  186. continue;
  187. }
  188. }
  189. }
  190. if (!retval)
  191. faux_error_sprintf(error, TAG" \"%s\": Illegal nested elements",
  192. kentry_name(kentry));
  193. return retval;
  194. }
  195. kentry_t *ientry_load(const ientry_t *ientry, faux_error_t *error)
  196. {
  197. kentry_t *kentry = NULL;
  198. if (!ientry)
  199. return NULL;
  200. // Name [mandatory]
  201. if (faux_str_is_empty(ientry->name)) {
  202. faux_error_add(error, TAG": Empty 'name' attribute");
  203. return NULL;
  204. }
  205. kentry = kentry_new(ientry->name);
  206. if (!kentry) {
  207. faux_error_sprintf(error, TAG" \"%s\": Can't create object",
  208. ientry->name);
  209. return NULL;
  210. }
  211. if (!ientry_parse(ientry, kentry, error)) {
  212. kentry_free(kentry);
  213. return NULL;
  214. }
  215. // Parse nested elements
  216. if (!ientry_parse_nested(ientry, kentry, error)) {
  217. kentry_free(kentry);
  218. return NULL;
  219. }
  220. return kentry;
  221. }
  222. char *ientry_deploy(const kentry_t *kentry, int level)
  223. {
  224. char *str = NULL;
  225. char *tmp = NULL;
  226. char *mode = NULL;
  227. kentry_entrys_node_t *entrys_iter = NULL;
  228. kentry_actions_node_t *actions_iter = NULL;
  229. char *num = NULL;
  230. tmp = faux_str_sprintf("%*cENTRY {\n", level, ' ');
  231. faux_str_cat(&str, tmp);
  232. faux_str_free(tmp);
  233. attr2ctext(&str, "name", kentry_name(kentry), level + 1);
  234. attr2ctext(&str, "help", kentry_help(kentry), level + 1);
  235. attr2ctext(&str, "ref", kentry_ref_str(kentry), level + 1);
  236. // Links (ENTRY with 'ref' attribute) doesn't need the following filds
  237. // that will be replaced by content of referenced ENTRY
  238. if (faux_str_is_empty(kentry_ref_str(kentry))) {
  239. attr2ctext(&str, "container", faux_conv_bool2str(kentry_container(kentry)), level + 1);
  240. // Mode
  241. switch (kentry_mode(kentry)) {
  242. case KENTRY_MODE_SEQUENCE:
  243. mode = "sequence";
  244. break;
  245. case KENTRY_MODE_SWITCH:
  246. mode = "switch";
  247. break;
  248. case KENTRY_MODE_EMPTY:
  249. mode = "empty";
  250. break;
  251. default:
  252. mode = NULL;
  253. }
  254. attr2ctext(&str, "mode", mode, level + 1);
  255. // Min occurs
  256. num = faux_str_sprintf("%u", kentry_min(kentry));
  257. attr2ctext(&str, "min", num, level + 1);
  258. faux_str_free(num);
  259. num = NULL;
  260. // Max occurs
  261. num = faux_str_sprintf("%u", kentry_max(kentry));
  262. attr2ctext(&str, "max", num, level + 1);
  263. faux_str_free(num);
  264. num = NULL;
  265. attr2ctext(&str, "ptype", kentry_ptype_str(kentry), level + 1);
  266. attr2ctext(&str, "value", kentry_value(kentry), level + 1);
  267. attr2ctext(&str, "restore", faux_conv_bool2str(kentry_restore(kentry)), level + 1);
  268. attr2ctext(&str, "order", faux_conv_bool2str(kentry_order(kentry)), level + 1);
  269. // ENTRY list
  270. entrys_iter = kentry_entrys_iter(kentry);
  271. if (entrys_iter) {
  272. kentry_t *nentry = NULL;
  273. tmp = faux_str_sprintf("\n%*cENTRY_LIST\n\n", level + 1, ' ');
  274. faux_str_cat(&str, tmp);
  275. faux_str_free(tmp);
  276. while ((nentry = kentry_entrys_each(&entrys_iter))) {
  277. tmp = ientry_deploy(nentry, level + 2);
  278. faux_str_cat(&str, tmp);
  279. faux_str_free(tmp);
  280. }
  281. tmp = faux_str_sprintf("%*cEND_ENTRY_LIST,\n", level + 1, ' ');
  282. faux_str_cat(&str, tmp);
  283. faux_str_free(tmp);
  284. }
  285. // ACTION list
  286. actions_iter = kentry_actions_iter(kentry);
  287. if (actions_iter) {
  288. kaction_t *action = NULL;
  289. tmp = faux_str_sprintf("\n%*cACTION_LIST\n\n", level + 1, ' ');
  290. faux_str_cat(&str, tmp);
  291. faux_str_free(tmp);
  292. while ((action = kentry_actions_each(&actions_iter))) {
  293. tmp = iaction_deploy(action, level + 2);
  294. faux_str_cat(&str, tmp);
  295. faux_str_free(tmp);
  296. }
  297. tmp = faux_str_sprintf("%*cEND_ACTION_LIST,\n", level + 1, ' ');
  298. faux_str_cat(&str, tmp);
  299. faux_str_free(tmp);
  300. }
  301. } // ref_str
  302. tmp = faux_str_sprintf("%*c},\n\n", level, ' ');
  303. faux_str_cat(&str, tmp);
  304. faux_str_free(tmp);
  305. return str;
  306. }