kentry.c 6.3 KB

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