1 /* $Id: rsbool.c,v 1.23 2004-01-16 15:27:35 heikki 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 =
58 static const struct rset_control control_or =
72 static const struct rset_control control_not =
87 const struct rset_control *rset_kind_and = &control_and;
88 const struct rset_control *rset_kind_or = &control_or;
89 const struct rset_control *rset_kind_not = &control_not;
91 struct rset_bool_info {
96 int (*cmp)(const void *p1, const void *p2);
97 struct rset_bool_rfd *rfd_list;
100 struct rset_bool_rfd {
110 struct rset_bool_rfd *next;
111 struct rset_bool_info *info;
114 static void *r_create (RSET ct, const struct rset_control *sel, void *parms)
116 rset_bool_parms *bool_parms = (rset_bool_parms *) parms;
117 struct rset_bool_info *info;
119 info = (struct rset_bool_info *) xmalloc (sizeof(*info));
120 info->key_size = bool_parms->key_size;
121 info->rset_l = bool_parms->rset_l;
122 info->rset_r = bool_parms->rset_r;
123 if (rset_is_volatile(info->rset_l) || rset_is_volatile(info->rset_r))
124 ct->flags |= RSET_FLAG_VOLATILE;
125 info->cmp = bool_parms->cmp;
126 info->rfd_list = NULL;
128 info->term_index_s = info->rset_l->no_rset_terms;
130 info->rset_l->no_rset_terms + info->rset_r->no_rset_terms;
131 ct->rset_terms = (RSET_TERM *)
132 xmalloc (sizeof (*ct->rset_terms) * ct->no_rset_terms);
134 memcpy (ct->rset_terms, info->rset_l->rset_terms,
135 info->rset_l->no_rset_terms * sizeof(*ct->rset_terms));
136 memcpy (ct->rset_terms + info->rset_l->no_rset_terms,
137 info->rset_r->rset_terms,
138 info->rset_r->no_rset_terms * sizeof(*ct->rset_terms));
142 static RSFD r_open (RSET ct, int flag)
144 struct rset_bool_info *info = (struct rset_bool_info *) ct->buf;
145 struct rset_bool_rfd *rfd;
147 if (flag & RSETF_WRITE)
149 logf (LOG_FATAL, "bool set type is read-only");
152 rfd = (struct rset_bool_rfd *) xmalloc (sizeof(*rfd));
153 rfd->next = info->rfd_list;
154 info->rfd_list = rfd;
157 rfd->buf_l = xmalloc (info->key_size);
158 rfd->buf_r = xmalloc (info->key_size);
159 rfd->rfd_l = rset_open (info->rset_l, RSETF_READ);
160 rfd->rfd_r = rset_open (info->rset_r, RSETF_READ);
161 rfd->more_l = rset_read (info->rset_l, rfd->rfd_l, rfd->buf_l,
163 rfd->more_r = rset_read (info->rset_r, rfd->rfd_r, rfd->buf_r,
169 static void r_close (RSFD rfd)
171 struct rset_bool_info *info = ((struct rset_bool_rfd*)rfd)->info;
172 struct rset_bool_rfd **rfdp;
174 for (rfdp = &info->rfd_list; *rfdp; rfdp = &(*rfdp)->next)
177 xfree ((*rfdp)->buf_l);
178 xfree ((*rfdp)->buf_r);
179 rset_close (info->rset_l, (*rfdp)->rfd_l);
180 rset_close (info->rset_r, (*rfdp)->rfd_r);
181 *rfdp = (*rfdp)->next;
185 logf (LOG_FATAL, "r_close but no rfd match!");
189 static void r_delete (RSET ct)
191 struct rset_bool_info *info = (struct rset_bool_info *) ct->buf;
193 assert (info->rfd_list == NULL);
194 xfree (ct->rset_terms);
195 rset_delete (info->rset_l);
196 rset_delete (info->rset_r);
200 static void r_rewind (RSFD rfd)
202 struct rset_bool_info *info = ((struct rset_bool_rfd*)rfd)->info;
203 struct rset_bool_rfd *p = (struct rset_bool_rfd *) rfd;
205 logf (LOG_DEBUG, "rsbool_rewind");
206 rset_rewind (info->rset_l, p->rfd_l);
207 rset_rewind (info->rset_r, p->rfd_r);
208 p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l, &p->term_index_l);
209 p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r, &p->term_index_r);
212 static int r_count (RSET ct)
233 static int r_read_and (RSFD rfd, void *buf, int *term_index)
235 struct rset_bool_rfd *p = (struct rset_bool_rfd *) rfd;
236 struct rset_bool_info *info = p->info;
238 while (p->more_l || p->more_r)
242 if (p->more_l && p->more_r)
243 cmp = (*info->cmp)(p->buf_l, p->buf_r);
250 memcpy (buf, p->buf_l, info->key_size);
251 *term_index = p->term_index_l;
252 p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l,
258 memcpy (buf, p->buf_r, info->key_size);
259 *term_index = p->term_index_r + info->term_index_s;
260 p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r,
267 memcpy (buf, p->buf_l, info->key_size);
268 *term_index = p->term_index_l;
269 p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l,
276 memcpy (buf, p->buf_r, info->key_size);
277 *term_index = p->term_index_r + info->term_index_s;
279 p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r,
283 if (!p->more_r || (*info->cmp)(p->buf_r, buf) > 1)
290 memcpy (buf, p->buf_l, info->key_size);
291 *term_index = p->term_index_l;
292 p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l,
296 if (!p->more_l || (*info->cmp)(p->buf_l, buf) > 1)
305 static int r_read_or (RSFD rfd, void *buf, int *term_index)
307 struct rset_bool_rfd *p = (struct rset_bool_rfd *) rfd;
308 struct rset_bool_info *info = p->info;
310 while (p->more_l || p->more_r)
314 if (p->more_l && p->more_r)
315 cmp = (*info->cmp)(p->buf_l, p->buf_r);
322 memcpy (buf, p->buf_l, info->key_size);
323 *term_index = p->term_index_l;
324 p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l,
326 p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r,
332 memcpy (buf, p->buf_r, info->key_size);
333 *term_index = p->term_index_r + info->term_index_s;
334 p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r,
340 memcpy (buf, p->buf_l, info->key_size);
341 *term_index = p->term_index_l;
342 p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l,
350 static int r_read_not (RSFD rfd, void *buf, int *term_index)
352 struct rset_bool_rfd *p = (struct rset_bool_rfd *) rfd;
353 struct rset_bool_info *info = p->info;
355 while (p->more_l || p->more_r)
359 if (p->more_l && p->more_r)
360 cmp = (*info->cmp)(p->buf_l, p->buf_r);
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,
374 p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r,
378 memcpy (buf, p->buf_l, info->key_size);
381 p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l,
385 cmp = (*info->cmp)(p->buf_l, buf);
386 } while (cmp >= -1 && cmp <= 1);
389 p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r,
393 cmp = (*info->cmp)(p->buf_r, buf);
394 } while (cmp >= -1 && cmp <= 1);
401 static int r_write (RSFD rfd, const void *buf)
403 logf (LOG_FATAL, "bool set type is read-only");