tree.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. /*
  2. * tree.c
  3. *
  4. * This file provides the implementation of a konf_tree class
  5. */
  6. #include "private.h"
  7. #include "lub/argv.h"
  8. #include "lub/string.h"
  9. #include "lub/ctype.h"
  10. #include <assert.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <stdio.h>
  14. #include <sys/types.h>
  15. #include <regex.h>
  16. /*---------------------------------------------------------
  17. * PRIVATE META FUNCTIONS
  18. *--------------------------------------------------------- */
  19. int konf_tree_bt_compare(const void *clientnode, const void *clientkey)
  20. {
  21. const konf_tree_t *this = clientnode;
  22. unsigned short *pri = (unsigned short *)clientkey;
  23. unsigned short *seq = (unsigned short *)clientkey + 1;
  24. unsigned short *sub = (unsigned short *)clientkey + 2;
  25. char *line = ((char *)clientkey + (3 * sizeof(unsigned short)));
  26. /* Priority check */
  27. if (this->priority != *pri)
  28. return (this->priority - *pri);
  29. /* Sequence check */
  30. if (this->seq_num != *seq)
  31. return (this->seq_num - *seq);
  32. /* Sub-sequence check */
  33. if (this->sub_num != *sub)
  34. return (this->sub_num - *sub);
  35. /* Line check */
  36. return lub_string_nocasecmp(this->line, line);
  37. }
  38. /*-------------------------------------------------------- */
  39. static void konf_tree_key(lub_bintree_key_t * key,
  40. unsigned short priority, unsigned short sequence,
  41. unsigned short subseq, const char *text)
  42. {
  43. unsigned short *pri = (unsigned short *)key;
  44. unsigned short *seq = (unsigned short *)key + 1;
  45. unsigned short *sub = (unsigned short *)key + 2;
  46. char *line = ((char *)key + (3 * sizeof(unsigned short)));
  47. /* fill out the opaque key */
  48. *pri = priority;
  49. *seq = sequence;
  50. *sub = subseq;
  51. strcpy(line, text);
  52. }
  53. /*-------------------------------------------------------- */
  54. void konf_tree_bt_getkey(const void *clientnode, lub_bintree_key_t * key)
  55. {
  56. const konf_tree_t *this = clientnode;
  57. konf_tree_key(key, this->priority, this->seq_num,
  58. this->sub_num, this->line);
  59. }
  60. /*---------------------------------------------------------
  61. * PRIVATE METHODS
  62. *--------------------------------------------------------- */
  63. static void
  64. konf_tree_init(konf_tree_t * this, const char *line, unsigned short priority)
  65. {
  66. /* set up defaults */
  67. this->line = lub_string_dup(line);
  68. this->priority = priority;
  69. this->seq_num = 0;
  70. this->sub_num = KONF_ENTRY_OK;
  71. this->splitter = BOOL_TRUE;
  72. /* Be a good binary tree citizen */
  73. lub_bintree_node_init(&this->bt_node);
  74. /* initialise the tree of commands for this conf */
  75. lub_bintree_init(&this->tree,
  76. konf_tree_bt_offset(),
  77. konf_tree_bt_compare, konf_tree_bt_getkey);
  78. }
  79. /*--------------------------------------------------------- */
  80. static void konf_tree_fini(konf_tree_t * this)
  81. {
  82. konf_tree_t *conf;
  83. /* delete each conf held by this conf */
  84. while ((conf = lub_bintree_findfirst(&this->tree))) {
  85. /* remove the conf from the tree */
  86. lub_bintree_remove(&this->tree, conf);
  87. /* release the instance */
  88. konf_tree_delete(conf);
  89. }
  90. /* free our memory */
  91. lub_string_free(this->line);
  92. this->line = NULL;
  93. }
  94. /*---------------------------------------------------------
  95. * PUBLIC META FUNCTIONS
  96. *--------------------------------------------------------- */
  97. size_t konf_tree_bt_offset(void)
  98. {
  99. return offsetof(konf_tree_t, bt_node);
  100. }
  101. /*--------------------------------------------------------- */
  102. konf_tree_t *konf_tree_new(const char *line, unsigned short priority)
  103. {
  104. konf_tree_t *this = malloc(sizeof(konf_tree_t));
  105. if (this) {
  106. konf_tree_init(this, line, priority);
  107. }
  108. return this;
  109. }
  110. /*---------------------------------------------------------
  111. * PUBLIC METHODS
  112. *--------------------------------------------------------- */
  113. void konf_tree_delete(konf_tree_t * this)
  114. {
  115. konf_tree_fini(this);
  116. free(this);
  117. }
  118. /*--------------------------------------------------------- */
  119. void konf_tree_fprintf(konf_tree_t * this, FILE * stream,
  120. const char *pattern, int depth,
  121. bool_t seq, unsigned char prev_pri_hi)
  122. {
  123. konf_tree_t *conf;
  124. lub_bintree_iterator_t iter;
  125. unsigned char pri = 0;
  126. regex_t regexp;
  127. if (this->line && *(this->line) != '\0') {
  128. char *space = NULL;
  129. if (depth > 0) {
  130. space = malloc(depth + 1);
  131. memset(space, ' ', depth);
  132. space[depth] = '\0';
  133. }
  134. if ((0 == depth) &&
  135. (this->splitter ||
  136. (konf_tree__get_priority_hi(this) != prev_pri_hi)))
  137. fprintf(stream, "!\n");
  138. fprintf(stream, "%s", space ? space : "");
  139. if (seq && (konf_tree__get_seq_num(this) != 0))
  140. fprintf(stream, "%02u ", konf_tree__get_seq_num(this));
  141. fprintf(stream, "%s\n", this->line);
  142. free(space);
  143. }
  144. /* regexp compilation */
  145. if (pattern)
  146. regcomp(&regexp, pattern, REG_EXTENDED | REG_ICASE);
  147. /* iterate child elements */
  148. if (!(conf = lub_bintree_findfirst(&this->tree)))
  149. return;
  150. for(lub_bintree_iterator_init(&iter, &this->tree, conf);
  151. conf; conf = lub_bintree_iterator_next(&iter)) {
  152. if (pattern && (0 != regexec(&regexp, conf->line, 0, NULL, 0)))
  153. continue;
  154. konf_tree_fprintf(conf, stream, NULL, depth + 1, seq, pri);
  155. pri = konf_tree__get_priority_hi(conf);
  156. }
  157. if (pattern)
  158. regfree(&regexp);
  159. }
  160. static int normalize_seq(konf_tree_t * this, unsigned short priority)
  161. {
  162. unsigned short cnt = 1;
  163. konf_tree_t *conf = NULL;
  164. lub_bintree_iterator_t iter;
  165. /* If tree is empty */
  166. if (!(conf = lub_bintree_findfirst(&this->tree)))
  167. return 0;
  168. /* Iterate and set dirty */
  169. lub_bintree_iterator_init(&iter, &this->tree, conf);
  170. do {
  171. unsigned short cur_pri = konf_tree__get_priority(conf);
  172. if (cur_pri < priority)
  173. continue;
  174. if (konf_tree__get_seq_num(conf) == 0)
  175. continue;
  176. if (cur_pri > priority)
  177. break;
  178. if (konf_tree__get_sub_num(conf) == KONF_ENTRY_OK)
  179. konf_tree__set_sub_num(conf, KONF_ENTRY_DIRTY);
  180. } while ((conf = lub_bintree_iterator_next(&iter)));
  181. /* Iterate and renum */
  182. conf = lub_bintree_findfirst(&this->tree);
  183. lub_bintree_iterator_init(&iter, &this->tree, conf);
  184. do {
  185. unsigned short cur_pri = konf_tree__get_priority(conf);
  186. if (cur_pri < priority)
  187. continue;
  188. if (konf_tree__get_seq_num(conf) == 0)
  189. continue;
  190. if (cur_pri > priority)
  191. break;
  192. if (konf_tree__get_sub_num(conf) == KONF_ENTRY_OK)
  193. continue;
  194. lub_bintree_remove(&this->tree, conf);
  195. konf_tree__set_sub_num(conf, KONF_ENTRY_OK);
  196. konf_tree__set_seq_num(conf, cnt++);
  197. lub_bintree_insert(&this->tree, conf);
  198. } while ((conf = lub_bintree_iterator_next(&iter)));
  199. return 0;
  200. }
  201. /*--------------------------------------------------------- */
  202. konf_tree_t *konf_tree_new_conf(konf_tree_t * this,
  203. const char *line, unsigned short priority,
  204. bool_t seq, unsigned short seq_num)
  205. {
  206. /* Allocate the memory for a new child element */
  207. konf_tree_t *newconf = konf_tree_new(line, priority);
  208. assert(newconf);
  209. /* Sequence */
  210. if (seq) {
  211. konf_tree__set_seq_num(newconf,
  212. seq_num ? seq_num : 0xffff);
  213. konf_tree__set_sub_num(newconf, KONF_ENTRY_NEW);
  214. }
  215. /* Insert it into the binary tree for this conf */
  216. if (-1 == lub_bintree_insert(&this->tree, newconf)) {
  217. /* inserting a duplicate command is bad */
  218. konf_tree_delete(newconf);
  219. newconf = NULL;
  220. }
  221. if (seq)
  222. normalize_seq(this, priority);
  223. return newconf;
  224. }
  225. /*--------------------------------------------------------- */
  226. konf_tree_t *konf_tree_find_conf(konf_tree_t * this,
  227. const char *line, unsigned short priority, unsigned short seq_num)
  228. {
  229. konf_tree_t *conf;
  230. lub_bintree_key_t key;
  231. lub_bintree_iterator_t iter;
  232. if ((0 != priority) && (0 != seq_num)) {
  233. konf_tree_key(&key, priority, seq_num,
  234. KONF_ENTRY_OK, line);
  235. return lub_bintree_find(&this->tree, &key);
  236. }
  237. /* If tree is empty */
  238. if (!(conf = lub_bintree_findfirst(&this->tree)))
  239. return NULL;
  240. /* Iterate non-empty tree */
  241. lub_bintree_iterator_init(&iter, &this->tree, conf);
  242. do {
  243. if (0 == lub_string_nocasecmp(conf->line, line))
  244. return conf;
  245. } while ((conf = lub_bintree_iterator_next(&iter)));
  246. return NULL;
  247. }
  248. /*--------------------------------------------------------- */
  249. int konf_tree_del_pattern(konf_tree_t *this,
  250. const char *pattern, unsigned short priority,
  251. bool_t seq, unsigned short seq_num)
  252. {
  253. konf_tree_t *conf;
  254. lub_bintree_iterator_t iter;
  255. regex_t regexp;
  256. int del_cnt = 0; /* how many strings were deleted */
  257. if (seq && (0 == priority))
  258. return -1;
  259. /* Is tree empty? */
  260. if (!(conf = lub_bintree_findfirst(&this->tree)))
  261. return 0;
  262. /* Compile regular expression */
  263. regcomp(&regexp, pattern, REG_EXTENDED | REG_ICASE);
  264. /* Iterate configuration tree */
  265. lub_bintree_iterator_init(&iter, &this->tree, conf);
  266. do {
  267. if (0 != regexec(&regexp, conf->line, 0, NULL, 0))
  268. continue;
  269. if ((0 != priority) &&
  270. (priority != conf->priority))
  271. continue;
  272. if (seq && (seq_num != 0) &&
  273. (seq_num != conf->seq_num))
  274. continue;
  275. if (seq && (0 == seq_num) && (0 == conf->seq_num))
  276. continue;
  277. lub_bintree_remove(&this->tree, conf);
  278. konf_tree_delete(conf);
  279. del_cnt++;
  280. } while ((conf = lub_bintree_iterator_next(&iter)));
  281. regfree(&regexp);
  282. if (seq && (del_cnt != 0))
  283. normalize_seq(this, priority);
  284. return 0;
  285. }
  286. /*--------------------------------------------------------- */
  287. unsigned short konf_tree__get_priority(const konf_tree_t * this)
  288. {
  289. return this->priority;
  290. }
  291. /*--------------------------------------------------------- */
  292. unsigned char konf_tree__get_priority_hi(const konf_tree_t * this)
  293. {
  294. return (unsigned char)(this->priority >> 8);
  295. }
  296. /*--------------------------------------------------------- */
  297. unsigned char konf_tree__get_priority_lo(const konf_tree_t * this)
  298. {
  299. return (unsigned char)(this->priority & 0xff);
  300. }
  301. /*--------------------------------------------------------- */
  302. bool_t konf_tree__get_splitter(const konf_tree_t * this)
  303. {
  304. return this->splitter;
  305. }
  306. /*--------------------------------------------------------- */
  307. void konf_tree__set_splitter(konf_tree_t *this, bool_t splitter)
  308. {
  309. this->splitter = splitter;
  310. }
  311. /*--------------------------------------------------------- */
  312. unsigned short konf_tree__get_seq_num(const konf_tree_t * this)
  313. {
  314. return this->seq_num;
  315. }
  316. /*--------------------------------------------------------- */
  317. void konf_tree__set_seq_num(konf_tree_t * this, unsigned short seq_num)
  318. {
  319. this->seq_num = seq_num;
  320. }
  321. /*--------------------------------------------------------- */
  322. unsigned short konf_tree__get_sub_num(const konf_tree_t * this)
  323. {
  324. return this->sub_num;
  325. }
  326. /*--------------------------------------------------------- */
  327. void konf_tree__set_sub_num(konf_tree_t * this, unsigned short sub_num)
  328. {
  329. this->sub_num = sub_num;
  330. }
  331. /*--------------------------------------------------------- */
  332. const char * konf_tree__get_line(const konf_tree_t * this)
  333. {
  334. return this->line;
  335. }