1 /* $Id: rsbool.c,v 1.32.2.1 2005-01-23 15:06:21 adam Exp $
2 Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003,2004
5 This file is part of the Zebra server.
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with Zebra; see the file LICENSE.zebra. If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
35 static void *r_create(RSET ct, const struct rset_control *sel, void *parms);
36 static RSFD r_open (RSET ct, int flag);
37 static void r_close (RSFD rfd);
38 static void r_delete (RSET ct);
39 static void r_rewind (RSFD rfd);
40 static int r_forward(RSET ct, RSFD rfd, void *buf, int *term_index,
41 int (*cmpfunc)(const void *p1, const void *p2),
42 const void *untilbuf);
43 /* static void r_pos (RSFD rfd, int *current, int *total); */
44 static int r_read_and_forward (RSFD rfd, void *buf, int *term_index);
45 static int r_read_and (RSFD rfd, void *buf, int *term_index);
46 static int r_read_or (RSFD rfd, void *buf, int *term_index);
47 static int r_read_not (RSFD rfd, void *buf, int *term_index);
48 static int r_write (RSFD rfd, const void *buf);
50 static const struct rset_control control_and =
64 static const struct rset_control control_and_forward =
78 static const struct rset_control control_or =
92 static const struct rset_control control_not =
107 const struct rset_control *rset_kind_and = &control_and;
108 const struct rset_control *rset_kind_and_forward = &control_and_forward;
109 const struct rset_control *rset_kind_or = &control_or;
110 const struct rset_control *rset_kind_not = &control_not;
112 struct rset_bool_info {
117 int (*cmp)(const void *p1, const void *p2);
118 void (*log_item)(int logmask, const void *p, const char *txt);
119 struct rset_bool_rfd *rfd_list;
122 struct rset_bool_rfd {
132 struct rset_bool_rfd *next;
133 struct rset_bool_info *info;
136 static void *r_create (RSET ct, const struct rset_control *sel, void *parms)
138 rset_bool_parms *bool_parms = (rset_bool_parms *) parms;
139 struct rset_bool_info *info;
141 info = (struct rset_bool_info *) xmalloc (sizeof(*info));
142 info->key_size = bool_parms->key_size;
143 info->rset_l = bool_parms->rset_l;
144 info->rset_r = bool_parms->rset_r;
145 if (rset_is_volatile(info->rset_l) || rset_is_volatile(info->rset_r))
146 ct->flags |= RSET_FLAG_VOLATILE;
147 info->cmp = bool_parms->cmp;
148 info->log_item = bool_parms->log_item;
149 info->rfd_list = NULL;
151 info->term_index_s = info->rset_l->no_rset_terms;
153 info->rset_l->no_rset_terms + info->rset_r->no_rset_terms;
154 ct->rset_terms = (RSET_TERM *)
155 xmalloc (sizeof (*ct->rset_terms) * ct->no_rset_terms);
157 memcpy (ct->rset_terms, info->rset_l->rset_terms,
158 info->rset_l->no_rset_terms * sizeof(*ct->rset_terms));
159 memcpy (ct->rset_terms + info->rset_l->no_rset_terms,
160 info->rset_r->rset_terms,
161 info->rset_r->no_rset_terms * sizeof(*ct->rset_terms));
165 static RSFD r_open (RSET ct, int flag)
167 struct rset_bool_info *info = (struct rset_bool_info *) ct->buf;
168 struct rset_bool_rfd *rfd;
170 if (flag & RSETF_WRITE)
172 logf (LOG_FATAL, "bool set type is read-only");
175 rfd = (struct rset_bool_rfd *) xmalloc (sizeof(*rfd));
176 logf(LOG_DEBUG,"rsbool (%s) open [%p]", ct->control->desc, rfd);
177 rfd->next = info->rfd_list;
178 info->rfd_list = rfd;
181 rfd->buf_l = xmalloc (info->key_size);
182 rfd->buf_r = xmalloc (info->key_size);
183 rfd->rfd_l = rset_open (info->rset_l, RSETF_READ);
184 rfd->rfd_r = rset_open (info->rset_r, RSETF_READ);
185 rfd->more_l = rset_read (info->rset_l, rfd->rfd_l, rfd->buf_l,
187 rfd->more_r = rset_read (info->rset_r, rfd->rfd_r, rfd->buf_r,
193 static void r_close (RSFD rfd)
195 struct rset_bool_info *info = ((struct rset_bool_rfd*)rfd)->info;
196 struct rset_bool_rfd **rfdp;
198 for (rfdp = &info->rfd_list; *rfdp; rfdp = &(*rfdp)->next)
201 xfree ((*rfdp)->buf_l);
202 xfree ((*rfdp)->buf_r);
203 rset_close (info->rset_l, (*rfdp)->rfd_l);
204 rset_close (info->rset_r, (*rfdp)->rfd_r);
205 *rfdp = (*rfdp)->next;
209 logf (LOG_FATAL, "r_close but no rfd match!");
213 static void r_delete (RSET ct)
215 struct rset_bool_info *info = (struct rset_bool_info *) ct->buf;
217 assert (info->rfd_list == NULL);
218 xfree (ct->rset_terms);
219 rset_delete (info->rset_l);
220 rset_delete (info->rset_r);
224 static void r_rewind (RSFD rfd)
226 struct rset_bool_info *info = ((struct rset_bool_rfd*)rfd)->info;
227 struct rset_bool_rfd *p = (struct rset_bool_rfd *) rfd;
229 logf (LOG_DEBUG, "rsbool_rewind");
230 rset_rewind (info->rset_l, p->rfd_l);
231 rset_rewind (info->rset_r, p->rfd_r);
232 p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l, &p->term_index_l);
233 p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r, &p->term_index_r);
236 static int r_forward (RSET ct, RSFD rfd, void *buf, int *term_index,
237 int (*cmpfunc)(const void *p1, const void *p2),
238 const void *untilbuf)
240 struct rset_bool_info *info = ((struct rset_bool_rfd*)rfd)->info;
241 struct rset_bool_rfd *p = (struct rset_bool_rfd *) rfd;
245 logf (LOG_DEBUG, "rsbool_forward (L) [%p] '%s' (ct=%p rfd=%p m=%d,%d)",
246 rfd, ct->control->desc, ct, rfd, p->more_l, p->more_r);
248 if ( p->more_l && ((cmpfunc)(untilbuf,p->buf_l)==2) )
249 p->more_l = rset_forward(info->rset_l, p->rfd_l, p->buf_l,
250 &p->term_index_l, info->cmp, untilbuf);
252 logf (LOG_DEBUG, "rsbool_forward (R) [%p] '%s' (ct=%p rfd=%p m=%d,%d)",
253 rfd, ct->control->desc, ct, rfd, p->more_l, p->more_r);
255 if ( p->more_r && ((cmpfunc)(untilbuf,p->buf_r)==2))
256 p->more_r = rset_forward(info->rset_r, p->rfd_r, p->buf_r,
257 &p->term_index_r, info->cmp, untilbuf);
259 logf (LOG_DEBUG, "rsbool_forward [%p] calling read, m=%d,%d t=%d",
260 rfd, p->more_l, p->more_r, p->tail);
264 rc = rset_read(ct,rfd,buf,term_index);
266 logf (LOG_DEBUG, "rsbool_forward returning [%p] %d m=%d,%d",
267 rfd, rc, p->more_l, p->more_r);
288 static int r_read_and (RSFD rfd, void *buf, int *term_index)
290 struct rset_bool_rfd *p = (struct rset_bool_rfd *) rfd;
291 struct rset_bool_info *info = p->info;
293 while (p->more_l || p->more_r)
297 if (p->more_l && p->more_r)
298 cmp = (*info->cmp)(p->buf_l, p->buf_r);
304 logf (LOG_DEBUG, "r_read_and [%p] looping: m=%d/%d c=%d t=%d",
305 rfd, p->more_l, p->more_r, cmp, p->tail);
306 (*info->log_item)(LOG_DEBUG, p->buf_l, "left ");
307 (*info->log_item)(LOG_DEBUG, p->buf_r, "right ");
311 memcpy (buf, p->buf_l, info->key_size);
312 *term_index = p->term_index_l;
313 p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l,
319 memcpy (buf, p->buf_r, info->key_size);
320 *term_index = p->term_index_r + info->term_index_s;
321 p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r,
325 logf (LOG_DEBUG, "r_read_and [%p] returning R m=%d/%d c=%d",
326 rfd, p->more_l, p->more_r, cmp);
327 key_logdump(LOG_DEBUG,buf);
328 (*info->log_item)(LOG_DEBUG, buf, "");
334 memcpy (buf, p->buf_l, info->key_size);
335 *term_index = p->term_index_l;
336 p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l,
340 logf (LOG_DEBUG, "r_read_and [%p] returning L m=%d/%d c=%d",
341 rfd, p->more_l, p->more_r, cmp);
342 (*info->log_item)(LOG_DEBUG, buf, "");
346 else if (cmp > 1) /* cmp == 2 */
348 memcpy (buf, p->buf_r, info->key_size);
349 *term_index = p->term_index_r + info->term_index_s;
351 p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r,
355 if (!p->more_r || (*info->cmp)(p->buf_r, buf) > 1)
358 logf (LOG_DEBUG, "r_read_and returning C m=%d/%d c=%d",
359 p->more_l, p->more_r, cmp);
360 (*info->log_item)(LOG_DEBUG, buf, "");
367 memcpy (buf, p->buf_l, info->key_size);
368 *term_index = p->term_index_l;
369 p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l,
373 if (!p->more_l || (*info->cmp)(p->buf_l, buf) > 1)
376 logf (LOG_DEBUG, "r_read_and [%p] returning R tail m=%d/%d c=%d",
377 rfd, p->more_l, p->more_r, cmp);
378 (*info->log_item)(LOG_DEBUG, buf, "");
385 logf (LOG_DEBUG, "r_read_and [%p] reached its end",rfd);
390 static int r_read_and_forward (RSFD rfd, void *buf, int *term_index)
392 struct rset_bool_rfd *p = (struct rset_bool_rfd *) rfd;
393 struct rset_bool_info *info = p->info;
395 while (p->more_l || p->more_r)
399 if (p->more_l && p->more_r)
400 cmp = (*info->cmp)(p->buf_l, p->buf_r);
406 logf (LOG_DEBUG, "r_read_and [%p] looping: m=%d/%d c=%d t=%d",
407 rfd, p->more_l, p->more_r, cmp, p->tail);
408 (*info->log_item)(LOG_DEBUG, p->buf_l, "left ");
409 (*info->log_item)(LOG_DEBUG, p->buf_r, "right ");
413 memcpy (buf, p->buf_l, info->key_size);
414 *term_index = p->term_index_l;
415 p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l,
421 memcpy (buf, p->buf_r, info->key_size);
422 *term_index = p->term_index_r + info->term_index_s;
423 p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r,
427 logf (LOG_DEBUG, "r_read_and [%p] returning R m=%d/%d c=%d",
428 rfd, p->more_l, p->more_r, cmp);
429 key_logdump(LOG_DEBUG,buf);
430 (*info->log_item)(LOG_DEBUG, buf, "");
436 memcpy (buf, p->buf_l, info->key_size);
437 *term_index = p->term_index_l;
438 p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l,
442 logf (LOG_DEBUG, "r_read_and [%p] returning L m=%d/%d c=%d",
443 rfd, p->more_l, p->more_r, cmp);
444 (*info->log_item)(LOG_DEBUG, buf, "");
448 else if (cmp > 1) /* cmp == 2 */
452 memcpy (buf, p->buf_r, info->key_size);
453 *term_index = p->term_index_r + info->term_index_s;
454 p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r,
456 if (!p->more_r || (*info->cmp)(p->buf_r, buf) > 1)
459 logf (LOG_DEBUG, "r_read_and [%p] returning R tail m=%d/%d c=%d",
460 rfd, p->more_l, p->more_r, cmp);
461 (*info->log_item)(LOG_DEBUG, buf, "");
468 logf (LOG_DEBUG, "r_read_and [%p] about to forward R m=%d/%d c=%d",
469 rfd, p->more_l, p->more_r, cmp);
471 if (p->more_r && p->more_l)
472 p->more_r = rset_forward(
473 info->rset_r, p->rfd_r,
474 p->buf_r, &p->term_index_r,
475 (info->cmp), p->buf_l);
478 return 0; /* no point in reading further */
486 memcpy (buf, p->buf_l, info->key_size);
487 *term_index = p->term_index_l;
488 p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l,
490 if (!p->more_l || (*info->cmp)(p->buf_l, buf) > 1)
493 logf (LOG_DEBUG, "r_read_and [%p] returning L tail m=%d/%d c=%d",
494 rfd, p->more_l, p->more_r, cmp);
495 (*info->log_item)(LOG_DEBUG, buf, "");
502 logf (LOG_DEBUG, "r_read_and [%p] about to forward L m=%d/%d c=%d",
503 rfd, p->more_l, p->more_r, cmp);
505 if (p->more_r && p->more_l)
506 p->more_l = rset_forward(
507 /* p->more_l = rset_default_forward( */
508 info->rset_l, p->rfd_l,
509 p->buf_l, &p->term_index_l,
510 (info->cmp), p->buf_r);
513 return 0; /* no point in reading further */
519 logf (LOG_DEBUG, "r_read_and [%p] reached its end",rfd);
524 static int r_read_or (RSFD rfd, void *buf, int *term_index)
526 struct rset_bool_rfd *p = (struct rset_bool_rfd *) rfd;
527 struct rset_bool_info *info = p->info;
529 while (p->more_l || p->more_r)
533 if (p->more_l && p->more_r)
534 cmp = (*info->cmp)(p->buf_l, p->buf_r);
541 memcpy (buf, p->buf_l, info->key_size);
542 *term_index = p->term_index_l;
543 p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l,
545 p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r,
548 logf (LOG_DEBUG, "r_read_or returning A m=%d/%d c=%d",
549 p->more_l, p->more_r, cmp);
550 (*info->log_item)(LOG_DEBUG, buf, "");
556 memcpy (buf, p->buf_r, info->key_size);
557 *term_index = p->term_index_r + info->term_index_s;
558 p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r,
561 logf (LOG_DEBUG, "r_read_or returning B m=%d/%d c=%d",
562 p->more_l, p->more_r, cmp);
563 (*info->log_item)(LOG_DEBUG, buf, "");
569 memcpy (buf, p->buf_l, info->key_size);
570 *term_index = p->term_index_l;
571 p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l,
574 logf (LOG_DEBUG, "r_read_or returning C m=%d/%d c=%d",
575 p->more_l, p->more_r, cmp);
576 (*info->log_item)(LOG_DEBUG, buf, "");
584 static int r_read_not (RSFD rfd, void *buf, int *term_index)
586 struct rset_bool_rfd *p = (struct rset_bool_rfd *) rfd;
587 struct rset_bool_info *info = p->info;
589 while (p->more_l || p->more_r)
593 if (p->more_l && p->more_r)
594 cmp = (*info->cmp)(p->buf_l, p->buf_r);
601 memcpy (buf, p->buf_l, info->key_size);
602 *term_index = p->term_index_l;
603 p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l,
610 p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r,
613 p->more_r = rset_forward(
614 info->rset_r, p->rfd_r,
615 p->buf_r, &p->term_index_r,
616 (info->cmp), p->buf_l);
621 memcpy (buf, p->buf_l, info->key_size);
624 p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l,
628 cmp = (*info->cmp)(p->buf_l, buf);
629 } while (cmp >= -1 && cmp <= 1);
632 p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r,
636 cmp = (*info->cmp)(p->buf_r, buf);
637 } while (cmp >= -1 && cmp <= 1);
644 static int r_write (RSFD rfd, const void *buf)
646 logf (LOG_FATAL, "bool set type is read-only");