tree.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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. if (this->line && *(this->line) != '\0') {
  127. char *space = NULL;
  128. if (depth > 0) {
  129. space = malloc(depth + 1);
  130. memset(space, ' ', depth);
  131. space[depth] = '\0';
  132. }
  133. if ((0 == depth) &&
  134. (this->splitter ||
  135. (konf_tree__get_priority_hi(this) != prev_pri_hi)))
  136. fprintf(stream, "!\n");
  137. fprintf(stream, "%s", space ? space : "");
  138. if (seq && (konf_tree__get_seq_num(this) != 0))
  139. fprintf(stream, "%02u ", konf_tree__get_seq_num(this));
  140. fprintf(stream, "%s\n", this->line);
  141. free(space);
  142. }
  143. /* iterate child elements */
  144. if (!(conf = lub_bintree_findfirst(&this->tree)))
  145. return;
  146. for(lub_bintree_iterator_init(&iter, &this->tree, conf);
  147. conf; conf = lub_bintree_iterator_next(&iter)) {
  148. if (pattern &&
  149. (lub_string_nocasestr(conf->line, pattern) != conf->line))
  150. continue;
  151. konf_tree_fprintf(conf, stream, NULL, depth + 1, seq, pri);
  152. pri = konf_tree__get_priority_hi(conf);
  153. }
  154. }
  155. static int normalize_seq(konf_tree_t * this, unsigned short priority)
  156. {
  157. unsigned short cnt = 1;
  158. konf_tree_t *conf = NULL;
  159. lub_bintree_iterator_t iter;
  160. /* If tree is empty */
  161. if (!(conf = lub_bintree_findfirst(&this->tree)))
  162. return 0;
  163. /* Iterate and set dirty */
  164. lub_bintree_iterator_init(&iter, &this->tree, conf);
  165. do {
  166. unsigned short cur_pri = konf_tree__get_priority(conf);
  167. if (cur_pri < priority)
  168. continue;
  169. if (konf_tree__get_seq_num(conf) == 0)
  170. continue;
  171. if (cur_pri > priority)
  172. break;
  173. if (konf_tree__get_sub_num(conf) == KONF_ENTRY_OK)
  174. konf_tree__set_sub_num(conf, KONF_ENTRY_DIRTY);
  175. } while ((conf = lub_bintree_iterator_next(&iter)));
  176. /* Iterate and renum */
  177. conf = lub_bintree_findfirst(&this->tree);
  178. lub_bintree_iterator_init(&iter, &this->tree, conf);
  179. do {
  180. unsigned short cur_pri = konf_tree__get_priority(conf);
  181. if (cur_pri < priority)
  182. continue;
  183. if (konf_tree__get_seq_num(conf) == 0)
  184. continue;
  185. if (cur_pri > priority)
  186. break;
  187. if (konf_tree__get_sub_num(conf) == KONF_ENTRY_OK)
  188. continue;
  189. lub_bintree_remove(&this->tree, conf);
  190. konf_tree__set_sub_num(conf, KONF_ENTRY_OK);
  191. konf_tree__set_seq_num(conf, cnt++);
  192. lub_bintree_insert(&this->tree, conf);
  193. } while ((conf = lub_bintree_iterator_next(&iter)));
  194. return 0;
  195. }
  196. /*--------------------------------------------------------- */
  197. konf_tree_t *konf_tree_new_conf(konf_tree_t * this,
  198. const char *line, unsigned short priority,
  199. bool_t seq, unsigned short seq_num)
  200. {
  201. /* Allocate the memory for a new child element */
  202. konf_tree_t *newconf = konf_tree_new(line, priority);
  203. assert(newconf);
  204. /* Sequence */
  205. if (seq) {
  206. konf_tree__set_seq_num(newconf,
  207. seq_num ? seq_num : 0xffff);
  208. konf_tree__set_sub_num(newconf, KONF_ENTRY_NEW);
  209. }
  210. /* Insert it into the binary tree for this conf */
  211. if (-1 == lub_bintree_insert(&this->tree, newconf)) {
  212. /* inserting a duplicate command is bad */
  213. konf_tree_delete(newconf);
  214. newconf = NULL;
  215. }
  216. if (seq)
  217. normalize_seq(this, priority);
  218. return newconf;
  219. }
  220. /*--------------------------------------------------------- */
  221. konf_tree_t *konf_tree_find_conf(konf_tree_t * this,
  222. const char *line, unsigned short priority, unsigned short seq_num)
  223. {
  224. konf_tree_t *conf;
  225. lub_bintree_key_t key;
  226. lub_bintree_iterator_t iter;
  227. if ((0 != priority) && (0 != seq_num)) {
  228. konf_tree_key(&key, priority, seq_num,
  229. KONF_ENTRY_OK, line);
  230. return lub_bintree_find(&this->tree, &key);
  231. }
  232. /* If tree is empty */
  233. if (!(conf = lub_bintree_findfirst(&this->tree)))
  234. return NULL;
  235. /* Iterate non-empty tree */
  236. lub_bintree_iterator_init(&iter, &this->tree, conf);
  237. do {
  238. if (0 == lub_string_nocasecmp(conf->line, line))
  239. return conf;
  240. } while ((conf = lub_bintree_iterator_next(&iter)));
  241. return NULL;
  242. }
  243. /*--------------------------------------------------------- */
  244. int konf_tree_del_pattern(konf_tree_t *this,
  245. const char *pattern, unsigned short priority,
  246. bool_t seq, unsigned short seq_num)
  247. {
  248. konf_tree_t *conf;
  249. lub_bintree_iterator_t iter;
  250. regex_t regexp;
  251. int del_cnt = 0; /* how many strings were deleted */
  252. if (seq && (0 == priority))
  253. return -1;
  254. /* Is tree empty? */
  255. if (!(conf = lub_bintree_findfirst(&this->tree)))
  256. return 0;
  257. /* Compile regular expression */
  258. regcomp(&regexp, pattern, REG_EXTENDED | REG_ICASE);
  259. /* Iterate configuration tree */
  260. lub_bintree_iterator_init(&iter, &this->tree, conf);
  261. do {
  262. if (0 != regexec(&regexp, conf->line, 0, NULL, 0))
  263. continue;
  264. if ((0 != priority) &&
  265. (priority != conf->priority))
  266. continue;
  267. if (seq && (seq_num != 0) &&
  268. (seq_num != conf->seq_num))
  269. continue;
  270. if (seq && (0 == seq_num) && (0 == conf->seq_num))
  271. continue;
  272. lub_bintree_remove(&this->tree, conf);
  273. konf_tree_delete(conf);
  274. del_cnt++;
  275. } while ((conf = lub_bintree_iterator_next(&iter)));
  276. regfree(&regexp);
  277. if (seq && (del_cnt != 0))
  278. normalize_seq(this, priority);
  279. return 0;
  280. }
  281. /*--------------------------------------------------------- */
  282. unsigned short konf_tree__get_priority(const konf_tree_t * this)
  283. {
  284. return this->priority;
  285. }
  286. /*--------------------------------------------------------- */
  287. unsigned char konf_tree__get_priority_hi(const konf_tree_t * this)
  288. {
  289. return (unsigned char)(this->priority >> 8);
  290. }
  291. /*--------------------------------------------------------- */
  292. unsigned char konf_tree__get_priority_lo(const konf_tree_t * this)
  293. {
  294. return (unsigned char)(this->priority & 0xff);
  295. }
  296. /*--------------------------------------------------------- */
  297. bool_t konf_tree__get_splitter(const konf_tree_t * this)
  298. {
  299. return this->splitter;
  300. }
  301. /*--------------------------------------------------------- */
  302. void konf_tree__set_splitter(konf_tree_t *this, bool_t splitter)
  303. {
  304. this->splitter = splitter;
  305. }
  306. /*--------------------------------------------------------- */
  307. unsigned short konf_tree__get_seq_num(const konf_tree_t * this)
  308. {
  309. return this->seq_num;
  310. }
  311. /*--------------------------------------------------------- */
  312. void konf_tree__set_seq_num(konf_tree_t * this, unsigned short seq_num)
  313. {
  314. this->seq_num = seq_num;
  315. }
  316. /*--------------------------------------------------------- */
  317. unsigned short konf_tree__get_sub_num(const konf_tree_t * this)
  318. {
  319. return this->sub_num;
  320. }
  321. /*--------------------------------------------------------- */
  322. void konf_tree__set_sub_num(konf_tree_t * this, unsigned short sub_num)
  323. {
  324. this->sub_num = sub_num;
  325. }
  326. /*--------------------------------------------------------- */
  327. const char * konf_tree__get_line(const konf_tree_t * this)
  328. {
  329. return this->line;
  330. }