1 /* $Id: rsbool.c,v 1.22 2003-03-31 21:53:40 adam Exp $
2 Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003
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
33 static void *r_create(RSET ct, const struct rset_control *sel, void *parms);
34 static RSFD r_open (RSET ct, int flag);
35 static void r_close (RSFD rfd);
36 static void r_delete (RSET ct);
37 static void r_rewind (RSFD rfd);
38 static int r_count (RSET ct);
39 static int r_read_and (RSFD rfd, void *buf, int *term_index);
40 static int r_read_or (RSFD rfd, void *buf, int *term_index);
41 static int r_read_not (RSFD rfd, void *buf, int *term_index);
42 static int r_write (RSFD rfd, const void *buf);
44 static const struct rset_control control_and =
57 static const struct rset_control control_or =
70 static const struct rset_control control_not =
84 const struct rset_control *rset_kind_and = &control_and;
85 const struct rset_control *rset_kind_or = &control_or;
86 const struct rset_control *rset_kind_not = &control_not;
88 struct rset_bool_info {
93 int (*cmp)(const void *p1, const void *p2);
94 struct rset_bool_rfd *rfd_list;
97 struct rset_bool_rfd {
107 struct rset_bool_rfd *next;
108 struct rset_bool_info *info;
111 static void *r_create (RSET ct, const struct rset_control *sel, void *parms)
113 rset_bool_parms *bool_parms = (rset_bool_parms *) parms;
114 struct rset_bool_info *info;
116 info = (struct rset_bool_info *) xmalloc (sizeof(*info));
117 info->key_size = bool_parms->key_size;
118 info->rset_l = bool_parms->rset_l;
119 info->rset_r = bool_parms->rset_r;
120 if (rset_is_volatile(info->rset_l) || rset_is_volatile(info->rset_r))
121 ct->flags |= RSET_FLAG_VOLATILE;
122 info->cmp = bool_parms->cmp;
123 info->rfd_list = NULL;
125 info->term_index_s = info->rset_l->no_rset_terms;
127 info->rset_l->no_rset_terms + info->rset_r->no_rset_terms;
128 ct->rset_terms = (RSET_TERM *)
129 xmalloc (sizeof (*ct->rset_terms) * ct->no_rset_terms);
131 memcpy (ct->rset_terms, info->rset_l->rset_terms,
132 info->rset_l->no_rset_terms * sizeof(*ct->rset_terms));
133 memcpy (ct->rset_terms + info->rset_l->no_rset_terms,
134 info->rset_r->rset_terms,
135 info->rset_r->no_rset_terms * sizeof(*ct->rset_terms));
139 static RSFD r_open (RSET ct, int flag)
141 struct rset_bool_info *info = (struct rset_bool_info *) ct->buf;
142 struct rset_bool_rfd *rfd;
144 if (flag & RSETF_WRITE)
146 logf (LOG_FATAL, "bool set type is read-only");
149 rfd = (struct rset_bool_rfd *) xmalloc (sizeof(*rfd));
150 rfd->next = info->rfd_list;
151 info->rfd_list = rfd;
154 rfd->buf_l = xmalloc (info->key_size);
155 rfd->buf_r = xmalloc (info->key_size);
156 rfd->rfd_l = rset_open (info->rset_l, RSETF_READ);
157 rfd->rfd_r = rset_open (info->rset_r, RSETF_READ);
158 rfd->more_l = rset_read (info->rset_l, rfd->rfd_l, rfd->buf_l,
160 rfd->more_r = rset_read (info->rset_r, rfd->rfd_r, rfd->buf_r,
166 static void r_close (RSFD rfd)
168 struct rset_bool_info *info = ((struct rset_bool_rfd*)rfd)->info;
169 struct rset_bool_rfd **rfdp;
171 for (rfdp = &info->rfd_list; *rfdp; rfdp = &(*rfdp)->next)
174 xfree ((*rfdp)->buf_l);
175 xfree ((*rfdp)->buf_r);
176 rset_close (info->rset_l, (*rfdp)->rfd_l);
177 rset_close (info->rset_r, (*rfdp)->rfd_r);
178 *rfdp = (*rfdp)->next;
182 logf (LOG_FATAL, "r_close but no rfd match!");
186 static void r_delete (RSET ct)
188 struct rset_bool_info *info = (struct rset_bool_info *) ct->buf;
190 assert (info->rfd_list == NULL);
191 xfree (ct->rset_terms);
192 rset_delete (info->rset_l);
193 rset_delete (info->rset_r);
197 static void r_rewind (RSFD rfd)
199 struct rset_bool_info *info = ((struct rset_bool_rfd*)rfd)->info;
200 struct rset_bool_rfd *p = (struct rset_bool_rfd *) rfd;
202 logf (LOG_DEBUG, "rsbool_rewind");
203 rset_rewind (info->rset_l, p->rfd_l);
204 rset_rewind (info->rset_r, p->rfd_r);
205 p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l, &p->term_index_l);
206 p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r, &p->term_index_r);
209 static int r_count (RSET ct)
230 static int r_read_and (RSFD rfd, void *buf, int *term_index)
232 struct rset_bool_rfd *p = (struct rset_bool_rfd *) rfd;
233 struct rset_bool_info *info = p->info;
235 while (p->more_l || p->more_r)
239 if (p->more_l && p->more_r)
240 cmp = (*info->cmp)(p->buf_l, p->buf_r);
247 memcpy (buf, p->buf_l, info->key_size);
248 *term_index = p->term_index_l;
249 p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l,
255 memcpy (buf, p->buf_r, info->key_size);
256 *term_index = p->term_index_r + info->term_index_s;
257 p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r,
264 memcpy (buf, p->buf_l, info->key_size);
265 *term_index = p->term_index_l;
266 p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l,
273 memcpy (buf, p->buf_r, info->key_size);
274 *term_index = p->term_index_r + info->term_index_s;
276 p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r,
280 if (!p->more_r || (*info->cmp)(p->buf_r, buf) > 1)
287 memcpy (buf, p->buf_l, info->key_size);
288 *term_index = p->term_index_l;
289 p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l,
293 if (!p->more_l || (*info->cmp)(p->buf_l, buf) > 1)
302 static int r_read_or (RSFD rfd, void *buf, int *term_index)
304 struct rset_bool_rfd *p = (struct rset_bool_rfd *) rfd;
305 struct rset_bool_info *info = p->info;
307 while (p->more_l || p->more_r)
311 if (p->more_l && p->more_r)
312 cmp = (*info->cmp)(p->buf_l, p->buf_r);
319 memcpy (buf, p->buf_l, info->key_size);
320 *term_index = p->term_index_l;
321 p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l,
323 p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r,
329 memcpy (buf, p->buf_r, info->key_size);
330 *term_index = p->term_index_r + info->term_index_s;
331 p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r,
337 memcpy (buf, p->buf_l, info->key_size);
338 *term_index = p->term_index_l;
339 p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l,
347 static int r_read_not (RSFD rfd, void *buf, int *term_index)
349 struct rset_bool_rfd *p = (struct rset_bool_rfd *) rfd;
350 struct rset_bool_info *info = p->info;
352 while (p->more_l || p->more_r)
356 if (p->more_l && p->more_r)
357 cmp = (*info->cmp)(p->buf_l, p->buf_r);
364 memcpy (buf, p->buf_l, info->key_size);
365 *term_index = p->term_index_l;
366 p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l,
371 p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r,
375 memcpy (buf, p->buf_l, info->key_size);
378 p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l,
382 cmp = (*info->cmp)(p->buf_l, buf);
383 } while (cmp >= -1 && cmp <= 1);
386 p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r,
390 cmp = (*info->cmp)(p->buf_r, buf);
391 } while (cmp >= -1 && cmp <= 1);
398 static int r_write (RSFD rfd, const void *buf)
400 logf (LOG_FATAL, "bool set type is read-only");