kentry.c 4.3 KB

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