kentry.c 4.6 KB

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