kentry.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. size_t min; // Min occurs of entry
  18. size_t max; // Max occurs of entry
  19. char *ptype_str; // Text reference to PTYPE
  20. kentry_t *ptype; // Resolved entry's PTYPE
  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. };
  29. // Simple methods
  30. // Name
  31. KGET_STR(entry, name);
  32. // Help
  33. KGET_STR(entry, help);
  34. KSET_STR(entry, help);
  35. // Parent
  36. KGET(entry, kentry_t *, parent);
  37. KSET(entry, kentry_t *, parent);
  38. // Container
  39. KGET_BOOL(entry, container);
  40. KSET_BOOL(entry, container);
  41. // Mode
  42. KGET(entry, kentry_mode_e, mode);
  43. KSET(entry, kentry_mode_e, mode);
  44. // Min occurs
  45. KGET(entry, size_t, min);
  46. KSET(entry, size_t, min);
  47. // Max occurs
  48. KGET(entry, size_t, max);
  49. KSET(entry, size_t, max);
  50. // PTYPE string (must be resolved later)
  51. KGET_STR(entry, ptype_str);
  52. KSET_STR(entry, ptype_str);
  53. // PTYPE (resolved)
  54. KGET(entry, kentry_t *, ptype);
  55. KSET(entry, kentry_t *, ptype);
  56. // Ref string (must be resolved later)
  57. KGET_STR(entry, ref_str);
  58. KSET_STR(entry, ref_str);
  59. // Value
  60. KGET_STR(entry, value);
  61. KSET_STR(entry, value);
  62. // Restore
  63. KGET_BOOL(entry, restore);
  64. KSET_BOOL(entry, restore);
  65. // Order
  66. KGET_BOOL(entry, order);
  67. KSET_BOOL(entry, order);
  68. // Filter
  69. KGET_BOOL(entry, filter);
  70. KSET_BOOL(entry, filter);
  71. // Nested ENTRYs list
  72. KGET(entry, faux_list_t *, entrys);
  73. static KCMP_NESTED(entry, entry, name);
  74. static KCMP_NESTED_BY_KEY(entry, entry, name);
  75. KADD_NESTED(entry, kentry_t *, entrys);
  76. KFIND_NESTED(entry, entry);
  77. KNESTED_LEN(entry, entrys);
  78. KNESTED_IS_EMPTY(entry, entrys);
  79. KNESTED_ITER(entry, entrys);
  80. KNESTED_EACH(entry, kentry_t *, entrys);
  81. // ACTION list
  82. KGET(entry, faux_list_t *, actions);
  83. KADD_NESTED(entry, kaction_t *, actions);
  84. KNESTED_LEN(entry, actions);
  85. KNESTED_ITER(entry, actions);
  86. KNESTED_EACH(entry, kaction_t *, actions);
  87. kentry_t *kentry_new(const char *name)
  88. {
  89. kentry_t *entry = NULL;
  90. if (faux_str_is_empty(name))
  91. return NULL;
  92. entry = faux_zmalloc(sizeof(*entry));
  93. assert(entry);
  94. if (!entry)
  95. return NULL;
  96. // Initialize
  97. entry->name = faux_str_dup(name);
  98. entry->help = NULL;
  99. entry->parent = NULL;
  100. entry->container = BOOL_FALSE;
  101. entry->mode = KENTRY_MODE_SEQUENCE;
  102. entry->min = 1;
  103. entry->max = 1;
  104. entry->ptype_str = NULL;
  105. entry->ptype = NULL;
  106. entry->ref_str = NULL;
  107. entry->value = NULL;
  108. entry->restore = BOOL_FALSE;
  109. entry->order = BOOL_FALSE;
  110. entry->filter = BOOL_FALSE;
  111. // ENTRY list
  112. entry->entrys = faux_list_new(FAUX_LIST_UNSORTED, FAUX_LIST_UNIQUE,
  113. kentry_entry_compare, kentry_entry_kcompare,
  114. (void (*)(void *))kentry_free);
  115. assert(entry->entrys);
  116. // ACTION list
  117. entry->actions = faux_list_new(FAUX_LIST_UNSORTED, FAUX_LIST_NONUNIQUE,
  118. NULL, NULL, (void (*)(void *))kaction_free);
  119. assert(entry->actions);
  120. return entry;
  121. }
  122. static void kentry_free_non_link(kentry_t *entry)
  123. {
  124. if (!entry)
  125. return;
  126. faux_str_free(entry->ptype_str);
  127. faux_list_free(entry->entrys);
  128. faux_list_free(entry->actions);
  129. }
  130. static void kentry_free_common(kentry_t *entry)
  131. {
  132. if (!entry)
  133. return;
  134. faux_str_free(entry->name);
  135. faux_str_free(entry->value);
  136. faux_str_free(entry->help);
  137. faux_str_free(entry->ref_str);
  138. }
  139. void kentry_free(kentry_t *entry)
  140. {
  141. if (!entry)
  142. return;
  143. // If ENTRY is not a link
  144. if (!kentry_ref_str(entry))
  145. kentry_free_non_link(entry);
  146. // For links and non-links
  147. kentry_free_common(entry);
  148. faux_free(entry);
  149. }
  150. bool_t kentry_link(kentry_t *dst, const kentry_t *src)
  151. {
  152. assert(dst);
  153. if (!dst)
  154. return BOOL_FALSE;
  155. assert(src);
  156. if (!src)
  157. return BOOL_FALSE;
  158. // Free all fields that will be linker to src later
  159. kentry_free_non_link(dst);
  160. // Copy structure by hand because else some fields must be
  161. // returned back anyway and temp memory must be allocated. I think it
  162. // worse.
  163. // name - orig
  164. // help - orig
  165. // parent - orig
  166. // container - orig
  167. dst->mode = src->mode;
  168. // min - orig
  169. // max - orig
  170. dst->ptype_str = src->ptype_str;
  171. dst->ptype = src->ptype;
  172. // ref_str - orig
  173. // value - orig
  174. // restore - orig
  175. // order - orig
  176. dst->filter = src->filter;
  177. dst->entrys = src->entrys;
  178. dst->actions = src->actions;
  179. return BOOL_TRUE;
  180. }