tree.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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. this->depth = -1;
  73. /* Be a good binary tree citizen */
  74. lub_bintree_node_init(&this->bt_node);
  75. /* initialise the tree of commands for this conf */
  76. lub_bintree_init(&this->tree,
  77. konf_tree_bt_offset(),
  78. konf_tree_bt_compare, konf_tree_bt_getkey);
  79. }
  80. /*--------------------------------------------------------- */
  81. static void konf_tree_fini(konf_tree_t * this)
  82. {
  83. konf_tree_t *conf;
  84. /* delete each conf held by this conf */
  85. while ((conf = lub_bintree_findfirst(&this->tree))) {
  86. /* remove the conf from the tree */
  87. lub_bintree_remove(&this->tree, conf);
  88. /* release the instance */
  89. konf_tree_delete(conf);
  90. }
  91. /* free our memory */
  92. lub_string_free(this->line);
  93. this->line = NULL;
  94. }
  95. /*---------------------------------------------------------
  96. * PUBLIC META FUNCTIONS
  97. *--------------------------------------------------------- */
  98. size_t konf_tree_bt_offset(void)
  99. {
  100. return offsetof(konf_tree_t, bt_node);
  101. }
  102. /*--------------------------------------------------------- */
  103. konf_tree_t *konf_tree_new(const char *line, unsigned short priority)
  104. {
  105. konf_tree_t *this = malloc(sizeof(konf_tree_t));
  106. if (this) {
  107. konf_tree_init(this, line, priority);
  108. }
  109. return this;
  110. }
  111. /*---------------------------------------------------------
  112. * PUBLIC METHODS
  113. *--------------------------------------------------------- */
  114. void konf_tree_delete(konf_tree_t * this)
  115. {
  116. konf_tree_fini(this);
  117. free(this);
  118. }
  119. /*--------------------------------------------------------- */
  120. void konf_tree_fprintf(konf_tree_t * this, FILE * stream,
  121. const char *pattern, int top_depth,
  122. bool_t seq, unsigned char prev_pri_hi)
  123. {
  124. konf_tree_t *conf;
  125. lub_bintree_iterator_t iter;
  126. unsigned char pri = 0;
  127. regex_t regexp;
  128. if (this->line && (*(this->line) != '\0') &&
  129. (this->depth > top_depth)) {
  130. char *space = NULL;
  131. unsigned space_num = this->depth - top_depth - 1;
  132. if (space_num > 0) {
  133. space = malloc(space_num + 1);
  134. memset(space, ' ', space_num);
  135. space[space_num] = '\0';
  136. }
  137. if ((0 == this->depth) &&
  138. (this->splitter ||
  139. (konf_tree__get_priority_hi(this) != prev_pri_hi)))
  140. fprintf(stream, "!\n");
  141. fprintf(stream, "%s", space ? space : "");
  142. if (seq && (konf_tree__get_seq_num(this) != 0))
  143. fprintf(stream, "%02u ", konf_tree__get_seq_num(this));
  144. fprintf(stream, "%s\n", this->line);
  145. free(space);
  146. }
  147. /* regexp compilation */
  148. if (pattern)
  149. regcomp(&regexp, pattern, REG_EXTENDED | REG_ICASE);
  150. /* iterate child elements */
  151. if (!(conf = lub_bintree_findfirst(&this->tree)))
  152. return;
  153. for(lub_bintree_iterator_init(&iter, &this->tree, conf);
  154. conf; conf = lub_bintree_iterator_next(&iter)) {
  155. if (pattern && (0 != regexec(&regexp, conf->line, 0, NULL, 0)))
  156. continue;
  157. konf_tree_fprintf(conf, stream, NULL, top_depth, seq, pri);
  158. pri = konf_tree__get_priority_hi(conf);
  159. }
  160. if (pattern)
  161. regfree(&regexp);
  162. }
  163. static int normalize_seq(konf_tree_t * this, unsigned short priority)
  164. {
  165. unsigned short cnt = 1;
  166. konf_tree_t *conf = NULL;
  167. lub_bintree_iterator_t iter;
  168. /* If tree is empty */
  169. if (!(conf = lub_bintree_findfirst(&this->tree)))
  170. return 0;
  171. /* Iterate and set dirty */
  172. lub_bintree_iterator_init(&iter, &this->tree, conf);
  173. do {
  174. unsigned short cur_pri = konf_tree__get_priority(conf);
  175. if (cur_pri < priority)
  176. continue;
  177. if (konf_tree__get_seq_num(conf) == 0)
  178. continue;
  179. if (cur_pri > priority)
  180. break;
  181. if (konf_tree__get_sub_num(conf) == KONF_ENTRY_OK)
  182. konf_tree__set_sub_num(conf, KONF_ENTRY_DIRTY);
  183. } while ((conf = lub_bintree_iterator_next(&iter)));
  184. /* Iterate and renum */
  185. conf = lub_bintree_findfirst(&this->tree);
  186. lub_bintree_iterator_init(&iter, &this->tree, conf);
  187. do {
  188. unsigned short cur_pri = konf_tree__get_priority(conf);
  189. if (cur_pri < priority)
  190. continue;
  191. if (konf_tree__get_seq_num(conf) == 0)
  192. continue;
  193. if (cur_pri > priority)
  194. break;
  195. if (konf_tree__get_sub_num(conf) == KONF_ENTRY_OK)
  196. continue;
  197. lub_bintree_remove(&this->tree, conf);
  198. konf_tree__set_sub_num(conf, KONF_ENTRY_OK);
  199. konf_tree__set_seq_num(conf, cnt++);
  200. lub_bintree_insert(&this->tree, conf);
  201. } while ((conf = lub_bintree_iterator_next(&iter)));
  202. return 0;
  203. }
  204. /*--------------------------------------------------------- */
  205. konf_tree_t *konf_tree_new_conf(konf_tree_t * this,
  206. const char *line, unsigned short priority,
  207. bool_t seq, unsigned short seq_num)
  208. {
  209. /* Allocate the memory for a new child element */
  210. konf_tree_t *newconf = konf_tree_new(line, priority);
  211. assert(newconf);
  212. /* Sequence */
  213. if (seq) {
  214. konf_tree__set_seq_num(newconf,
  215. seq_num ? seq_num : 0xffff);
  216. konf_tree__set_sub_num(newconf, KONF_ENTRY_NEW);
  217. }
  218. /* Insert it into the binary tree for this conf */
  219. if (-1 == lub_bintree_insert(&this->tree, newconf)) {
  220. /* inserting a duplicate command is bad */
  221. konf_tree_delete(newconf);
  222. newconf = NULL;
  223. }
  224. if (seq)
  225. normalize_seq(this, priority);
  226. return newconf;
  227. }
  228. /*--------------------------------------------------------- */
  229. konf_tree_t *konf_tree_find_conf(konf_tree_t * this,
  230. const char *line, unsigned short priority, unsigned short seq_num)
  231. {
  232. konf_tree_t *conf;
  233. lub_bintree_key_t key;
  234. lub_bintree_iterator_t iter;
  235. if ((0 != priority) && (0 != seq_num)) {
  236. konf_tree_key(&key, priority, seq_num,
  237. KONF_ENTRY_OK, line);
  238. return lub_bintree_find(&this->tree, &key);
  239. }
  240. /* If tree is empty */
  241. if (!(conf = lub_bintree_findfirst(&this->tree)))
  242. return NULL;
  243. /* Iterate non-empty tree */
  244. lub_bintree_iterator_init(&iter, &this->tree, conf);
  245. do {
  246. if (0 == lub_string_nocasecmp(conf->line, line))
  247. return conf;
  248. } while ((conf = lub_bintree_iterator_next(&iter)));
  249. return NULL;
  250. }
  251. /*--------------------------------------------------------- */
  252. int konf_tree_del_pattern(konf_tree_t *this,
  253. const char *pattern, unsigned short priority,
  254. bool_t seq, unsigned short seq_num)
  255. {
  256. konf_tree_t *conf;
  257. lub_bintree_iterator_t iter;
  258. regex_t regexp;
  259. int del_cnt = 0; /* how many strings were deleted */
  260. if (seq && (0 == priority))
  261. return -1;
  262. /* Is tree empty? */
  263. if (!(conf = lub_bintree_findfirst(&this->tree)))
  264. return 0;
  265. /* Compile regular expression */
  266. regcomp(&regexp, pattern, REG_EXTENDED | REG_ICASE);
  267. /* Iterate configuration tree */
  268. lub_bintree_iterator_init(&iter, &this->tree, conf);
  269. do {
  270. if (0 != regexec(&regexp, conf->line, 0, NULL, 0))
  271. continue;
  272. if ((0 != priority) &&
  273. (priority != conf->priority))
  274. continue;
  275. if (seq && (seq_num != 0) &&
  276. (seq_num != conf->seq_num))
  277. continue;
  278. if (seq && (0 == seq_num) && (0 == conf->seq_num))
  279. continue;
  280. lub_bintree_remove(&this->tree, conf);
  281. konf_tree_delete(conf);
  282. del_cnt++;
  283. } while ((conf = lub_bintree_iterator_next(&iter)));
  284. regfree(&regexp);
  285. if (seq && (del_cnt != 0))
  286. normalize_seq(this, priority);
  287. return 0;
  288. }
  289. /*--------------------------------------------------------- */
  290. unsigned short konf_tree__get_priority(const konf_tree_t * this)
  291. {
  292. return this->priority;
  293. }
  294. /*--------------------------------------------------------- */
  295. unsigned char konf_tree__get_priority_hi(const konf_tree_t * this)
  296. {
  297. return (unsigned char)(this->priority >> 8);
  298. }
  299. /*--------------------------------------------------------- */
  300. unsigned char konf_tree__get_priority_lo(const konf_tree_t * this)
  301. {
  302. return (unsigned char)(this->priority & 0xff);
  303. }
  304. /*--------------------------------------------------------- */
  305. bool_t konf_tree__get_splitter(const konf_tree_t * this)
  306. {
  307. return this->splitter;
  308. }
  309. /*--------------------------------------------------------- */
  310. void konf_tree__set_splitter(konf_tree_t *this, bool_t splitter)
  311. {
  312. this->splitter = splitter;
  313. }
  314. /*--------------------------------------------------------- */
  315. unsigned short konf_tree__get_seq_num(const konf_tree_t * this)
  316. {
  317. return this->seq_num;
  318. }
  319. /*--------------------------------------------------------- */
  320. void konf_tree__set_seq_num(konf_tree_t * this, unsigned short seq_num)
  321. {
  322. this->seq_num = seq_num;
  323. }
  324. /*--------------------------------------------------------- */
  325. unsigned short konf_tree__get_sub_num(const konf_tree_t * this)
  326. {
  327. return this->sub_num;
  328. }
  329. /*--------------------------------------------------------- */
  330. void konf_tree__set_sub_num(konf_tree_t * this, unsigned short sub_num)
  331. {
  332. this->sub_num = sub_num;
  333. }
  334. /*--------------------------------------------------------- */
  335. const char * konf_tree__get_line(const konf_tree_t * this)
  336. {
  337. return this->line;
  338. }
  339. /*--------------------------------------------------------- */
  340. void konf_tree__set_depth(konf_tree_t * this, int depth)
  341. {
  342. this->depth = depth;
  343. }
  344. /*--------------------------------------------------------- */
  345. int konf_tree__get_depth(const konf_tree_t * this)
  346. {
  347. return this->depth;
  348. }