kentry.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <assert.h>
  5. #include <faux/faux.h>
  6. #include <faux/str.h>
  7. #include <faux/list.h>
  8. #include <klish/khelper.h>
  9. #include <klish/kaction.h>
  10. #include <klish/kentry.h>
  11. struct kentry_s {
  12. char *name; // Mandatory name (identifier within entries tree)
  13. char *help; // Help for the entry
  14. kentry_t *parent; // Parent kentry_t element
  15. bool_t container; // Is entry container (element with hidden path)
  16. kentry_mode_e mode; // Mode of nested ENTRYs list
  17. kentry_purpose_e purpose; // Special purpose of ENTRY
  18. size_t min; // Min occurs of entry
  19. size_t max; // Max occurs of entry
  20. char *ptype_str; // Text reference to PTYPE
  21. kentry_t *ptype; // Resolved entry's PTYPE
  22. char *ref_str; // Text reference to aliased ENTRY
  23. char *value; // Additional info
  24. bool_t restore; // Should entry restore its depth while execution
  25. bool_t order; // Is entry ordered
  26. bool_t filter; // Is entry filter. Filter can't have inline actions.
  27. faux_list_t *entrys; // Nested ENTRYs
  28. faux_list_t *actions; // Nested ACTIONs
  29. // Fast links to nested entries with special purposes:
  30. kentry_t* nested_by_purpose[KENTRY_PURPOSE_MAX];
  31. };
  32. // Simple methods
  33. // Name
  34. KGET_STR(entry, name);
  35. // Help
  36. KGET_STR(entry, help);
  37. KSET_STR(entry, help);
  38. // Parent
  39. KGET(entry, kentry_t *, parent);
  40. KSET(entry, kentry_t *, parent);
  41. // Container
  42. KGET_BOOL(entry, container);
  43. KSET_BOOL(entry, container);
  44. // Mode
  45. KGET(entry, kentry_mode_e, mode);
  46. KSET(entry, kentry_mode_e, mode);
  47. // Purpose
  48. KGET(entry, kentry_purpose_e, purpose);
  49. KSET(entry, kentry_purpose_e, purpose);
  50. // Min occurs
  51. KGET(entry, size_t, min);
  52. KSET(entry, size_t, min);
  53. // Max occurs
  54. KGET(entry, size_t, max);
  55. KSET(entry, size_t, max);
  56. // PTYPE string (must be resolved later)
  57. KGET_STR(entry, ptype_str);
  58. KSET_STR(entry, ptype_str);
  59. // PTYPE (resolved)
  60. KGET(entry, kentry_t *, ptype);
  61. KSET(entry, kentry_t *, ptype);
  62. // Ref string (must be resolved later)
  63. KGET_STR(entry, ref_str);
  64. KSET_STR(entry, ref_str);
  65. // Value
  66. KGET_STR(entry, value);
  67. KSET_STR(entry, value);
  68. // Restore
  69. KGET_BOOL(entry, restore);
  70. KSET_BOOL(entry, restore);
  71. // Order
  72. KGET_BOOL(entry, order);
  73. KSET_BOOL(entry, order);
  74. // Filter
  75. KGET_BOOL(entry, filter);
  76. KSET_BOOL(entry, filter);
  77. // Nested ENTRYs list
  78. KGET(entry, faux_list_t *, entrys);
  79. static KCMP_NESTED(entry, entry, name);
  80. static KCMP_NESTED_BY_KEY(entry, entry, name);
  81. KADD_NESTED(entry, kentry_t *, entrys);
  82. KFIND_NESTED(entry, entry);
  83. KNESTED_LEN(entry, entrys);
  84. KNESTED_IS_EMPTY(entry, entrys);
  85. KNESTED_ITER(entry, entrys);
  86. KNESTED_EACH(entry, kentry_t *, entrys);
  87. // ACTION list
  88. KGET(entry, faux_list_t *, actions);
  89. KADD_NESTED(entry, kaction_t *, actions);
  90. KNESTED_LEN(entry, actions);
  91. KNESTED_ITER(entry, actions);
  92. KNESTED_EACH(entry, kaction_t *, actions);
  93. kentry_t *kentry_new(const char *name)
  94. {
  95. kentry_t *entry = NULL;
  96. if (faux_str_is_empty(name))
  97. return NULL;
  98. entry = faux_zmalloc(sizeof(*entry));
  99. assert(entry);
  100. if (!entry)
  101. return NULL;
  102. // Initialize
  103. entry->name = faux_str_dup(name);
  104. entry->help = NULL;
  105. entry->parent = NULL;
  106. entry->container = BOOL_FALSE;
  107. entry->mode = KENTRY_MODE_SEQUENCE;
  108. entry->purpose = KENTRY_PURPOSE_COMMON;
  109. entry->min = 1;
  110. entry->max = 1;
  111. entry->ptype_str = NULL;
  112. entry->ptype = NULL;
  113. entry->ref_str = NULL;
  114. entry->value = NULL;
  115. entry->restore = BOOL_FALSE;
  116. entry->order = BOOL_FALSE;
  117. entry->filter = BOOL_FALSE;
  118. // ENTRY list
  119. entry->entrys = faux_list_new(FAUX_LIST_UNSORTED, FAUX_LIST_UNIQUE,
  120. kentry_entry_compare, kentry_entry_kcompare,
  121. (void (*)(void *))kentry_free);
  122. assert(entry->entrys);
  123. // ACTION list
  124. entry->actions = faux_list_new(FAUX_LIST_UNSORTED, FAUX_LIST_NONUNIQUE,
  125. NULL, NULL, (void (*)(void *))kaction_free);
  126. assert(entry->actions);
  127. faux_bzero(entry->nested_by_purpose, sizeof(entry->nested_by_purpose));
  128. return entry;
  129. }
  130. static void kentry_free_non_link(kentry_t *entry)
  131. {
  132. if (!entry)
  133. return;
  134. faux_str_free(entry->ptype_str);
  135. faux_list_free(entry->entrys);
  136. faux_list_free(entry->actions);
  137. }
  138. static void kentry_free_common(kentry_t *entry)
  139. {
  140. if (!entry)
  141. return;
  142. faux_str_free(entry->name);
  143. faux_str_free(entry->value);
  144. faux_str_free(entry->help);
  145. faux_str_free(entry->ref_str);
  146. }
  147. void kentry_free(kentry_t *entry)
  148. {
  149. if (!entry)
  150. return;
  151. // If ENTRY is not a link
  152. if (!kentry_ref_str(entry))
  153. kentry_free_non_link(entry);
  154. // For links and non-links
  155. kentry_free_common(entry);
  156. faux_free(entry);
  157. }
  158. bool_t kentry_link(kentry_t *dst, const kentry_t *src)
  159. {
  160. assert(dst);
  161. if (!dst)
  162. return BOOL_FALSE;
  163. assert(src);
  164. if (!src)
  165. return BOOL_FALSE;
  166. // Free all fields that will be linker to src later
  167. kentry_free_non_link(dst);
  168. // Copy structure by hand because else some fields must be
  169. // returned back anyway and temp memory must be allocated. I think it
  170. // worse.
  171. // name - orig
  172. // help - orig
  173. // parent - orig
  174. // container - orig
  175. dst->mode = src->mode;
  176. // purpose - orig
  177. // min - orig
  178. // max - orig
  179. dst->ptype_str = src->ptype_str;
  180. dst->ptype = src->ptype;
  181. // ref_str - orig
  182. // value - orig
  183. // restore - orig
  184. // order - orig
  185. dst->filter = src->filter;
  186. dst->entrys = src->entrys;
  187. dst->actions = src->actions;
  188. return BOOL_TRUE;
  189. }
  190. kentry_t *kentry_nested_by_purpose(const kentry_t *entry, kentry_purpose_e purpose)
  191. {
  192. assert(entry);
  193. if (!entry)
  194. return NULL;
  195. return entry->nested_by_purpose[purpose];
  196. }
  197. bool_t kentry_set_nested_by_purpose(kentry_t *entry, kentry_purpose_e purpose,
  198. kentry_t *nested)
  199. {
  200. assert(entry);
  201. if (!entry)
  202. return BOOL_FALSE;
  203. entry->nested_by_purpose[purpose] = nested;
  204. return BOOL_TRUE;
  205. }