Start development on 386BSD 0.0
[unix-history] / .ref-BSD-4_3_Net_2 / usr / src / contrib / isode / others / quipu / uips / pod / filt.c
CommitLineData
11492ebf
C
1
2#ifndef lint
3static char *rcsid = "$Header: /f/osi/others/quipu/uips/pod/RCS/filt.c,v 7.2 91/02/22 09:31:31 mrose Interim $";
4#endif
5
6/*
7 * $Header: /f/osi/others/quipu/uips/pod/RCS/filt.c,v 7.2 91/02/22 09:31:31 mrose Interim $
8 */
9
10#include <string.h>
11#include <malloc.h>
12#include <ctype.h>
13
14#include "quipu/util.h"
15#include "quipu/common.h"
16#include "quipu/entry.h"
17
18#include "filt.h"
19#include "y.tab.h"
20#include "defs.h"
21
22extern unsigned int curr_filt;
23extern unsigned int filt_num;
24extern unsigned int typeindx;
25extern filt_struct *filt_arr[];
26extern char *filtvalue[];
27extern char *filttype[];
28extern char *default_arr[];
29
30extern char mvalue[];
31
32make_type(name_val, filt)
33 char * name_val;
34 filt_struct * filt;
35{
36 filttype[curr_filt] = (char *) malloc((unsigned int) (strlen(name_val) + 1));
37 (void) strcpy(filttype[curr_filt], name_val);
38
39 filt_arr[curr_filt] = filt;
40}
41
42filt_struct *make_item_filter(oid, match, value)
43 char *oid;
44 int match;
45 char *value;
46{
47 register filt_struct * filt = (filt_struct *) malloc(sizeof(filt_struct));
48
49 filt->flt_type = ITEM;
50 filt->next = 0;
51
52 filt->fu_cont.item.fi_type = match;
53 filt->fu_cont.item.stroid =
54 (char *) malloc((unsigned int) (strlen(oid) + 1));
55 (void) strcpy(filt->fu_cont.item.stroid, oid);
56
57 if (*value == '*') filt->fu_cont.item.name = (char *) 0;
58 else {
59 filt->fu_cont.item.name =
60 (char *) malloc((unsigned int) (strlen(value) + 1));
61 (void) strcpy(filt->fu_cont.item.name, value);
62 }
63 return filt;
64}
65
66filt_struct *link_filters(filt1, filt2)
67 filt_struct *filt1, *filt2;
68{
69 filt1->next = filt2;
70 return filt1;
71}
72
73filt_struct *make_parent_filter(filt_type, filt1, filt2, filt3)
74 int filt_type;
75 filt_struct *filt1, *filt2, *filt3;
76{
77 filt_struct * parent = (filt_struct *) malloc(sizeof(filt_struct));
78
79 switch (filt_type) {
80
81 case NOT:
82 parent->flt_type = NOT;
83 parent->fu_cont.sub_filt = filt1;
84 parent->next = 0;
85 break;
86
87 case AND:
88 parent->flt_type = AND;
89 parent->fu_cont.sub_filt = filt1;
90 filt1->next = filt2;
91 filt2->next = filt3;
92 parent->next = 0;
93 break;
94
95 default:
96 parent->flt_type = OR;
97 parent->fu_cont.sub_filt = filt1;
98 filt1->next = filt2;
99 filt2->next = filt3;
100 parent->next = 0;
101 break;
102 }
103
104 return parent;
105}
106
107free_filt(filt)
108 filt_struct *filt;
109{
110 if (filt) {
111 free_filt(filt->next);
112
113 if (filt->flt_type = ITEM) {
114 free(filt->fu_cont.item.stroid);
115 if (filt->fu_cont.item.name) free(filt->fu_cont.item.name);
116 } else
117 free_filt(filt->fu_cont.sub_filt);
118
119 free((char *) filt);
120 } else
121 return;
122}
123
124Filter make_attr_filter()
125{
126 int match_type;
127 char attr_name[STRINGLEN],
128 attr_val[STRINGLEN], sub_val_initial[STRINGLEN],
129 sub_val_final[STRINGLEN], sub_val_any[STRINGLEN];
130 register char *end, *start, *next;
131 char save;
132 Filter sfilt = filter_alloc();
133
134 sfilt->flt_type = FILTER_ITEM;
135 sfilt->flt_next = NULLFILTER;
136 sfilt->flt_un.flt_un_item.fi_type = FILTERITEM_EQUALITY;
137 sfilt->flt_un.flt_un_item.fi_un.fi_un_ava.ava_value = NULLAttrV;
138 sfilt->flt_un.flt_un_item.fi_un.fi_un_ava.ava_type = NULLAttrT;
139
140 if (end = index(mvalue, '~')) match_type = APPROX;
141 else if (end = index(mvalue, '*')) match_type = SUBSTRING;
142 else match_type = EQUAL;
143
144 start = mvalue;
145 while (isspace(*start) && *start != '\0') start++;
146 end = start;
147 while (!isspace(*end) && *end != '=' && *end != '~'
148 && *end != '*' && *end != '\0') end++;
149 save = *end;
150 *end = '\0';
151 (void) strcpy(attr_name, start);
152 *end = save;
153
154 if (attr_name [0] == '\0') {
155 (void) fprintf (stderr,
156 "Error: Cannot search, invalid syntax on filter '%s'.\n",
157 mvalue);
158 filter_free(sfilt);
159 return NULLFILTER;
160 }
161
162 start = end + 1;
163 if (match_type != SUBSTRING) {
164 while (!isalnum(*start) && *start != '\0') start++;
165
166 end = start;
167
168 while (*end != '\0') end++;
169 while (!isalnum(*end) && end > start) end--;
170 if (*end != '\0') end++;
171
172 save = *end;
173 *end = '\0';
174 (void) strcpy(attr_val, start);
175 *end = save;
176
177 if (attr_val[0] == '\0') {
178 (void) fprintf (stderr,
179 "Error: Cannot search, invalid syntax on filter '%s'.\n",
180 mvalue);
181 filter_free(sfilt);
182 return NULLFILTER;
183 }
184 } else {
185 while (!isalnum(*start) && *start != '*' && *start != '\0') start++;
186
187 if (*start == '\0') {
188 (void) fprintf (stderr,
189 "Error: Cannot search, invalid syntax on filter '%s'.\n",
190 mvalue);
191 filter_free(sfilt);
192 return NULLFILTER;
193 }
194
195 if (*start == '*') {
196 sub_val_initial[0] = '*';
197 ++start;
198 while (isspace(*start) && *start != '\0') start++;
199
200 if (*start == '\0' || !isalnum(*start)) {
201 (void) fprintf (stderr,
202 "Error: Cannot search, invalid syntax on filter '%s'.\n",
203 mvalue);
204 filter_free(sfilt);
205 return NULLFILTER;
206 }
207
208 end = start;
209 while (isalnum(*end) && *end != '\0') end++;
210
211 if (*end == '\0') {
212 (void) strcpy (sub_val_final, start);
213 sub_val_any[0] = '*';
214 } else {
215 next = end;
216
217 while (*next != '*' && *next != '\0') next++;
218
219 if (*next == '*') {
220 sub_val_final[0] = '*';
221
222 save = *end;
223 *end = '\0';
224 (void) strcpy(sub_val_any, start);
225 *end = save;
226 } else {
227 sub_val_any[0] = '*';
228
229 save = *end;
230 *end = '\0';
231 (void) strcpy(sub_val_final, start);
232 *end = save;
233 }
234 }
235 } else if (isalnum(*start)) {
236 end = start;
237 while (!isspace(*end) && *end != '\0') end++;
238
239 if (*end == '\0') {
240 (void) fprintf (stderr,
241 "Error: Cannot search, invalid syntax on filter '%s'.\n",
242 mvalue);
243 filter_free(sfilt);
244 return NULLFILTER;
245 }
246
247 save = *end;
248 *end = '\0';
249
250 if (index ((char *) (end + 1), '*') == NULLCP) {
251 (void) fprintf (stderr,
252 "Error: Cannot search, invalid syntax on filter '%s'.\n",
253 mvalue);
254 filter_free(sfilt);
255 return NULLFILTER;
256 } else {
257 (void) strcpy(sub_val_initial, start);
258 sub_val_any[0] = sub_val_final[0] = '*';
259 }
260 }
261 }
262
263 switch (match_type) {
264 case APPROX:
265 case EQUAL:
266 sfilt->flt_un.flt_un_item.fi_un.fi_un_ava.ava_type = AttrT_new(attr_name);
267
268 if (match_type == EQUAL)
269 sfilt->flt_un.flt_un_item.fi_type = FILTERITEM_EQUALITY;
270 else
271 sfilt->flt_un.flt_un_item.fi_type = FILTERITEM_APPROX;
272
273 if (!sfilt->flt_un.flt_un_item.fi_un.fi_un_ava.ava_type ||
274 sfilt->flt_un.flt_un_item.fi_un.fi_un_ava.ava_type->oa_syntax
275 == 0) {
276 (void) fprintf (stderr,
277 "Error: Cannot search, invalid attribute type '%s'.\n",
278 attr_name);
279 sfilt->flt_next = NULLFILTER;
280 filter_free(sfilt);
281 return NULLFILTER;
282 }
283
284 if ((sfilt->flt_un.flt_un_item.fi_un.fi_un_ava.ava_value =
285 str2AttrV(attr_val,
286 sfilt->flt_un.flt_un_item.fi_un.fi_un_ava.ava_type
287 ->oa_syntax)) == NULL) {
288 (void) fprintf (stderr,
289 "Error: Cannot search, invalid value '%s' for attribute type '%s'.\n",
290 attr_val, attr_name);
291 filter_free(sfilt);
292 return NULLFILTER;
293 }
294 return sfilt;
295
296 case SUBSTRING:
297 sfilt->flt_un.flt_un_item.fi_type = FILTERITEM_SUBSTRINGS;
298
299 sfilt->flt_un.flt_un_item.fi_un.fi_un_substrings.fi_sub_initial = NULLAV;
300 sfilt->flt_un.flt_un_item.fi_un.fi_un_substrings.fi_sub_final = NULLAV;
301 sfilt->flt_un.flt_un_item.fi_un.fi_un_substrings.fi_sub_any = NULLAV;
302
303 sfilt->flt_un.flt_un_item.fi_un.fi_un_substrings.fi_sub_type =
304 AttrT_new(attr_name);
305
306 if (!sfilt->flt_un.flt_un_item.fi_un.fi_un_ava.ava_type ||
307 sfilt->flt_un.flt_un_item.fi_un.fi_un_ava.ava_type->oa_syntax
308 == 0) {
309 (void) fprintf (stderr,
310 "Error: Cannot search, invalid attribute type '%s'.\n",
311 attr_name);
312 filter_free(sfilt);
313 return NULLFILTER;
314 }
315
316 if (sub_val_initial[0] != '*')
317 if ((sfilt->flt_un.flt_un_item.fi_un.fi_un_substrings.fi_sub_initial =
318 avs_comp_new(str2AttrV(sub_val_initial,
319 sfilt->flt_un.flt_un_item.fi_un.
320 fi_un_substrings.fi_sub_type
321 ->oa_syntax)))
322 == NULLAV) {
323 (void) fprintf (stderr,
324 "Error: Cannot search, invalid value '%s' for attribute type '%s'.\n",
325 sub_val_initial, attr_name);
326 filter_free(sfilt);
327 return NULLFILTER;
328 }
329
330 if (sub_val_any[0] != '*')
331 if ((sfilt->flt_un.flt_un_item.fi_un.fi_un_substrings.fi_sub_any =
332 avs_comp_new(str2AttrV(sub_val_any,
333 sfilt->flt_un.flt_un_item.fi_un.
334 fi_un_substrings.fi_sub_type->
335 oa_syntax)))
336 == NULLAV) {
337 (void) fprintf (stderr,
338 "Error: Cannot search, invalid value '%s' for attribute type '%s'.\n",
339 sub_val_any, attr_name);
340 filter_free(sfilt);
341 return NULLFILTER;
342 }
343
344 if (sub_val_final[0] != '*')
345 if ((sfilt->flt_un.flt_un_item.fi_un.fi_un_substrings.fi_sub_final =
346 avs_comp_new(str2AttrV(sub_val_final,
347 sfilt->flt_un.flt_un_item.fi_un.
348 fi_un_substrings.fi_sub_type->
349 oa_syntax)))
350 == NULLAV) {
351 (void) fprintf (stderr,
352 "Error: Cannot search, invalid value '%s' for attribute type '%s'.\n",
353 sub_val_final, attr_name);
354 filter_free(sfilt);
355 return NULLFILTER;
356 }
357 return sfilt;
358 default:
359 return NULLFILTER;
360 }
361}
362
363Filter make_filter(filt)
364 filt_struct *filt;
365{
366 int type;
367 char svalue[STRINGLEN];
368 Filter rfilt, sfilt;
369
370 if (!filt) return NULLFILTER;
371
372 if (index(mvalue, '=')) return make_attr_filter();
373
374 sfilt = filter_alloc();
375
376 switch(filt->flt_type) {
377 case ITEM:
378 sfilt->flt_type = FILTER_ITEM;
379 sfilt->flt_next = make_filter(filt->next);
380
381 (void) strcpy(svalue, (filt->fu_cont.item.name?
382 filt->fu_cont.item.name:
383 mvalue));
384
385 type = filt->fu_cont.item.fi_type;
386
387 switch(type) {
388 case APPROX:
389 case EQUAL:
390 sfilt->flt_un.flt_un_item.fi_un.fi_un_ava.ava_type =
391 AttrT_new(filt->fu_cont.item.stroid);
392
393 if (!sfilt->flt_un.flt_un_item.fi_un.fi_un_ava.ava_type ||
394 sfilt->flt_un.flt_un_item.fi_un.fi_un_ava.ava_type->
395 oa_syntax == 0) {
396 rfilt = sfilt->flt_next;
397 sfilt->flt_un.flt_un_item.fi_un.fi_un_ava.ava_value = NULLAttrV;
398 sfilt->flt_next = NULLFILTER;
399 filter_free(sfilt);
400 return rfilt;
401 }
402
403 if ((sfilt->flt_un.flt_un_item.fi_un.fi_un_ava.ava_value =
404 str2AttrV(svalue,
405 sfilt->flt_un.flt_un_item.fi_un.fi_un_ava.ava_type->
406 oa_syntax)) == NULLAttrV) {
407 rfilt = sfilt->flt_next;
408 sfilt->flt_next = NULLFILTER;
409 filter_free(sfilt);
410 return rfilt;
411 }
412
413 if (type == EQUAL)
414 sfilt->flt_un.flt_un_item.fi_type = FILTERITEM_EQUALITY;
415 else
416 sfilt->flt_un.flt_un_item.fi_type = FILTERITEM_APPROX;
417
418 break;
419
420 case SUBSTRING:
421 sfilt->flt_un.flt_un_item.fi_type = FILTERITEM_SUBSTRINGS;
422 sfilt->flt_un.flt_un_item.fi_un.fi_un_substrings.fi_sub_type =
423 AttrT_new(filt->fu_cont.item.stroid);
424
425 if (!sfilt->flt_un.flt_un_item.fi_un.fi_un_ava.ava_type ||
426 sfilt->flt_un.flt_un_item.fi_un.fi_un_ava.ava_type->
427 oa_syntax == 0) {
428 rfilt = sfilt->flt_next;
429 sfilt->flt_un.flt_un_item.fi_un.fi_un_substrings.fi_sub_initial =
430 NULLAV;
431 sfilt->flt_un.flt_un_item.fi_un.fi_un_substrings.fi_sub_final =
432 NULLAV;
433 sfilt->flt_un.flt_un_item.fi_un.fi_un_substrings.fi_sub_any =
434 NULLAV;
435 sfilt->flt_next = NULLFILTER;
436 filter_free(sfilt);
437 return rfilt;
438 }
439
440 sfilt->flt_un.flt_un_item.fi_un.fi_un_substrings.fi_sub_initial =
441 NULLAV;
442 sfilt->flt_un.flt_un_item.fi_un.fi_un_substrings.fi_sub_final =
443 NULLAV;
444 sfilt->flt_un.flt_un_item.fi_un.fi_un_substrings.fi_sub_any =
445 avs_comp_new(str2AttrV(svalue,
446 sfilt->flt_un.flt_un_item.fi_un.
447 fi_un_substrings.fi_sub_type->
448 oa_syntax));
449 if (sfilt->flt_un.flt_un_item.fi_un.fi_un_substrings.fi_sub_any ==
450 NULLAV) {
451 rfilt = sfilt->flt_next;
452 sfilt->flt_next = NULLFILTER;
453 filter_free(sfilt);
454 return rfilt;
455 }
456
457 break;
458
459 default:
460 sfilt->flt_un.flt_un_item.fi_type = FILTERITEM_APPROX;
461 break;
462 }
463 return sfilt;
464
465 case AND:
466 sfilt->flt_type = FILTER_AND;
467 sfilt->flt_un.flt_un_filter = make_filter(filt->fu_cont.sub_filt);
468 sfilt->flt_next = make_filter(filt->next);
469 return sfilt;
470
471 case OR:
472 sfilt->flt_type = FILTER_OR;
473 sfilt->flt_un.flt_un_filter = make_filter(filt->fu_cont.sub_filt);
474 sfilt->flt_next = make_filter(filt->next);
475 return sfilt;
476
477 case NOT:
478 sfilt->flt_type = FILTER_NOT;
479 sfilt->flt_next = make_filter(filt->next);
480 sfilt->flt_un.flt_un_filter = make_filter(filt->fu_cont.sub_filt);
481 return sfilt;
482
483 default:
484 return NULLFILTER;
485 }
486}
487
488
489
490
491
492
493
494
495
496
497
498
499
500