ientry.c 7.8 KB

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