Debian Codegraph @

bin/pattern.c

raw symbols SCIP
1 /*
2 * pattern.c
3 *
4 * Author: Kazu Yamamoto <[email protected]>
5 * Created: Oct 17, 2000
6 *
7 * Code:
8 */
9
10 #include "mew.h"
11
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15
16 /****************************************************************
17 *
18 * Structures
19 *
20 */
21
22 enum ptype {
23 P_NUL, /* terminator */
24 P_EXP, /* abstracted expression */
25 P_STR, /* str, "str", "\"" */
26 P_EQ, /* = case-insensitive */
27 P_CEQ, /* == case-sensitive */
28 P_NEQ, /* != case-insensitive */
29 P_CNE ,/* !== case-sensitive */
30 P_AND, /* &, && */
31 P_OR, /* |, || */
32 P_NOT, /* ! */
33 P_LSQ, /* ( */
34 P_RSQ /* ) */
35 };
36
37 struct pat {
38 enum ptype typ; /* See above */
39 struct pat *prv; /* Doubly linked list */
40 struct pat *nxt; /* Doubly linked list */
41 struct pat *dwn; /* For P_EXP and P_NOT */
42 char *key; /* For P_STR, P_EQ, P_NEQ */
43 char *val; /* P_EQ, P_NEQ */
44 };
45
46 /****************************************************************
47 *
48 * prototype
49 *
50 */
51
52 private void Pinit(char *);
53 private int Getc(void);
54 private void Ungetc(void);
55 private int token(char **);
56 private struct pat *get_pat(void);
57 private struct pat *exp_eq(struct pat *);
58 private struct pat *exp_sq(struct pat *);
59 private struct pat *exp_not(struct pat *);
60 private struct pat *exp_lao(struct pat *, enum ptype);
61 private struct pat *exp_exp(struct pat *);
62 private struct pat *parse_pattern(char *);
63 private int eval_exp(struct pat *);
64
65 /****************************************************************
66 *
67 * Utilities for lexical analyzer
68 *
69 */
70
71 char *Pvec;
72 int Ppos;
73 int Plim;
74
75 private void
76 Pinit(char *p) {
77 Pvec = p;
78 Ppos = 0;
79 Plim = strlen(Pvec);
80 }
81
82
83 private int
84 Getc() {
85 if (Ppos < Plim)
86 return Pvec[Ppos++];
87 else
88 return EOF;
89 /* Getc() is to be called even after Getc() returned EOF. */
90 }
91
92 private void
93 Ungetc() {
94 if (Ppos > 0)
95 Ppos--;
96 else
97 warn_exit("ungetc is impossible.");
98 }
99
100 /****************************************************************
101 *
102 * Lexical analyzer
103 *
104 */
105
106 private char Keyval[BUFSIZ];
107
108 private int
109 token(char **ret) {
110 int c;
111 int type = P_NUL, i = 0, quoted = NO;
112 *ret = NULL;
113
114 while ((c = Getc()) != EOF) {
115 switch (c) {
116 case SP:
117 case TAB:
118 continue;
119 case '&':
120 type = P_AND;
121 break;
122 case '|':
123 type = P_OR;
124 break;
125 case '!':
126 type = P_NOT;
127 break;
128 case EQ:
129 type = P_EQ;
130 break;
131 case '(':
132 return P_LSQ;
133 case ')':
134 return P_RSQ;
135 break;
136 case '"':
137 type = P_STR;
138 quoted = YES;
139 break;
140 case '\\':
141 if ((c = Getc()) == EOF)
142 warn_exit("broken escape.");
143 /* fall through */
144 default:
145 type = P_STR;
146 Keyval[i++] = c;
147 break;
148 }
149 break;
150 }
151
152 if (quoted == YES) {
153 while ((c = Getc()) != EOF) {
154 switch (c) {
155 case '\\':
156 if ((c = Getc()) == EOF)
157 warn_exit("broken escape.");
158 Keyval[i++] = c;
159 break;
160 case '"':
161 goto allocstr;
162 default:
163 Keyval[i++] = c;
164 break;
165 }
166 }
167 warn_exit("broken quoted string.");
168 }
169
170 while ((c = Getc()) != EOF) {
171 switch (c) {
172 case SP:
173 case TAB:
174 break;
175 case '&':
176 if (type != P_AND)
177 Ungetc();
178 break;
179 case '|':
180 if (type != P_OR)
181 Ungetc();
182 break;
183 case '!':
184 Ungetc();
185 break;
186 case EQ:
187 if (type == P_EQ) {
188 type = P_CEQ;
189 break;
190 } else if (type == P_NOT) {
191 if ((c = Getc()) == EQ) {
192 type = P_CNE;
193 } else {
194 type = P_NEQ;
195 Ungetc();
196 }
197 break;
198 } else {
199 Ungetc();
200 break;
201 }
202 case '(':
203 Ungetc();
204 break;
205 case ')':
206 Ungetc();
207 break;
208 case '\\':
209 if (type != P_STR) {
210 Ungetc();
211 break;
212 }
213 if ((c = Getc()) == EOF)
214 warn_exit("broken escape.");
215 /* fall through */
216 default:
217 if (type != P_STR) {
218 Ungetc();
219 break;
220 }
221 Keyval[i++] = c;
222 continue;
223 }
224 break;
225 }
226
227 allocstr:
228 if (type == P_STR) {
229 Keyval[i] = NUL;
230 STRDUP(*ret, Keyval);
231 }
232 return type;
233 }
234
235 /****************************************************************
236 *
237 * Utilities for parser
238 *
239 */
240
241 private struct pat *
242 get_pat() {
243 struct pat *p;
244 MALLOC(p, sizeof(struct pat));
245 memset(p, 0, sizeof(struct pat));
246 return p;
247 }
248
249 /****************************************************************
250 *
251 * Parser
252 *
253 */
254
255 /*
256 * Both key = val and key != val is the most strong.
257 * This function assumes to be called just once as pre-process
258 * before exp_exp().
259 *
260 * P_STR P_EQ P_STR => P_EXP
261 * key val v
262 * P_EQ
263 * key val
264 */
265
266 private struct pat *
267 exp_eq(struct pat *top) {
268 struct pat *n, *p, *q = top;
269
270 while (q != NULL)
271 if (q->typ == P_EQ || q->typ == P_CEQ ||
272 q->typ == P_NEQ || q->typ == P_CNE) {
273 if (q->prv == NULL || q->nxt == NULL)
274 warn_exit("broken eq/neq.");
275 if (q->prv->typ != P_STR || q->nxt->typ != P_STR)
276 warn_exit("broken eq/neq.");
277 n = get_pat();
278 p = q->prv;
279 n->typ = q->typ;
280 n->key = p->key;
281 n->val = q->nxt->key;
282 p->typ = P_EXP;
283 p->dwn = n;
284 p->key = NULL;
285 q = q->nxt->nxt;
286 if (q != NULL) q->prv = p;
287 free(p->nxt->nxt);
288 free(p->nxt);
289 p->nxt = q;
290 } else
291 q = q->nxt;
292 return top;
293 }
294
295 /*
296 * Making square bracket to one P_EXP.
297 *
298 * X P_LSQ Y Z P_RSQ Z => X P_EXP Z
299 * v
300 * Y Z (will be parsed by exp_exp())
301 */
302
303 private struct pat *
304 exp_sq(struct pat *top) {
305 struct pat *p = NULL, *q = top, *n;
306 int level = 0;
307 while (q != NULL) {
308 switch (q->typ) {
309 case P_LSQ:
310 if (level == 0) p = q;
311 level++;
312 break;
313 case P_RSQ:
314 level--;
315 if (level < 0) warn_exit("too many ')'.");
316 if (level == 0) {
317 if (p->nxt == q)
318 warn_exit("empty '()'.");
319 n = p->nxt;
320 n->prv = NULL;
321 q->prv->nxt = NULL;
322 n = exp_exp(n);
323 if (p->prv == NULL)
324 top = n;
325 else {
326 p->prv->nxt = n;
327 n->prv = p->prv;
328 free(p);
329 }
330 if (q->nxt == NULL)
331 ;
332 else {
333 q->nxt->prv = n;
334 n->nxt = q->nxt;
335 free(q);
336 }
337 q = n->nxt;
338 continue;
339 }
340 break;
341 default:
342 break;
343 }
344 q = q->nxt;
345 }
346
347 if (level > 0)
348 warn_exit("too many '('.");
349 return top;
350 }
351
352 /*
353 * Binding logical not to the next one
354 *
355 * P_NOT P_EXP => P_EXP'
356 * v
357 * P_NOT
358 * v
359 * P_EXP
360 * P_EXP is a result of exp_sq() or exp_eq().
361 */
362
363 private struct pat *
364 exp_not(struct pat *top) {
365 struct pat *n, *m, *p = top, *q;
366 while (p != NULL)
367 if (p->typ == P_NOT) {
368 if ((q = p->nxt) == NULL)
369 warn_exit("broken not.");
370 if (q->typ != P_EXP)
371 warn_exit("broken not.");
372 n = get_pat();
373 m = get_pat();
374 p->dwn = n;
375 n->typ = P_NOT;
376 n->dwn = m;
377 m->typ = P_EXP;
378 m->dwn = q->dwn;
379 p->typ = P_EXP;
380 q = q->nxt;
381 free(p->nxt);
382 if (q != NULL) q->prv = p;
383 p->nxt = q;
384 p = q;
385 } else
386 p = p->nxt;
387 return top;
388 }
389
390 /* Binding logical and/or to the previous one and the next one.
391 * This is processed barkward from the tail so that the result
392 * P_EXP can be evaluated naturally.
393 *
394 * P_EXP1 P_ANDa P_EXP2 P_ANDb P_EXP3
395 *
396 * v
397 * v
398 *
399 * P_EXP5
400 * v
401 * P_ANDa P_EXP1 P_EXP4
402 * v
403 * P_ANDb P_EXP2 P_EXP3
404 */
405
406 private struct pat *
407 exp_lao(struct pat *top, enum ptype lao) {
408 struct pat *p = top, *q, *r, *n;
409 int count = 0;
410
411 do {
412 if (p->typ == lao) count++;
413 q = p;
414 p = p->nxt;
415 } while (p != NULL);
416 if (count == 0)
417 return top;
418 while (q != NULL)
419 if (q->typ == lao) {
420 p = q->prv;
421 r = q->nxt;
422 if (p == NULL || r == NULL)
423 warn_exit("broken and/or.");
424 if (p->typ != P_EXP || r->typ != P_EXP)
425 warn_exit("broken and/or.");
426 n = get_pat();
427 n->typ = P_EXP;
428 n->prv = p->prv;
429 n->nxt = r->nxt;
430 n->dwn = p;
431 if (p->prv != NULL) p->prv->nxt = n;
432 if (r->nxt != NULL) r->nxt->prv = n;
433 p->prv = NULL;
434 r->nxt = NULL;
435 /* swap p and q */
436 q->typ = P_EXP;
437 q->dwn = p->dwn;
438 p->typ = lao;
439 p->dwn = NULL;
440 top = n;
441 q = n->prv;
442 } else {
443 top = q;
444 q = q->prv;
445 }
446 return top;
447 }
448
449 /*
450 * Creating an expression tree.
451 *
452 */
453
454 private struct pat *
455 exp_exp(struct pat *top) {
456 if (top == NULL)
457 warn_exit("invalid pattern.");
458 top = exp_sq(top);
459 top = exp_not(top);
460 top = exp_lao(top, P_AND);
461 top = exp_lao(top, P_OR);
462 return top;
463 }
464
465 /****************************************************************
466 *
467 * Starter of the parser
468 *
469 */
470
471 private struct pat *
472 parse_pattern(char *pattern) {
473 enum ptype type;
474 char *str;
475 struct pat *top = NULL, *p = NULL, *q = NULL;
476
477 Pinit(pattern);
478 while ((type = token(&str)) != P_NUL) {
479 p = get_pat();
480 p->typ = type;
481 if (type == P_STR)
482 p->key = str;
483 if (top == NULL)
484 top = p;
485 else {
486 q->nxt = p;
487 p->prv = q;
488 }
489 q = p;
490 }
491
492 top = exp_eq(top);
493 top = exp_exp(top);
494 return top;
495 }
496
497 /****************************************************************
498 *
499 * Evaluator of an expression tree
500 *
501 */
502
503 private int
504 eval_exp(struct pat *p) {
505 switch (p->typ) {
506 case P_NOT:
507 if (eval_exp(p->dwn) == TRUE)
508 return FALSE;
509 else
510 return TRUE;
511 case P_AND:
512 if (eval_exp(p->nxt) == FALSE)
513 return FALSE;
514 if (eval_exp(p->nxt->nxt) == FALSE)
515 return FALSE;
516 return TRUE;
517 case P_OR:
518 if (eval_exp(p->nxt) == TRUE)
519 return TRUE;
520 if (eval_exp(p->nxt->nxt) == TRUE)
521 return TRUE;
522 return FALSE;
523 #ifndef DEBUG
524 case P_EQ:
525 if ((search_string(p->key, p->val, NO)) == TRUE)
526 return TRUE;
527 else
528 return FALSE;
529 case P_CEQ:
530 if ((search_string(p->key, p->val, YES)) == TRUE)
531 return TRUE;
532 else
533 return FALSE;
534 case P_NEQ:
535 if ((search_string(p->key, p->val, NO)) == TRUE)
536 return FALSE;
537 else
538 return TRUE;
539 case P_CNE:
540 if ((search_string(p->key, p->val, YES)) == TRUE)
541 return FALSE;
542 else
543 return TRUE;
544 #endif /* !DEBUG */
545 case P_EXP:
546 return eval_exp(p->dwn);
547 break;
548 default:
549 warn_exit("broken tree.");
550 }
551 return TRUE; /* never reached */
552 }
553
554 /****************************************************************
555 *
556 * External interface
557 *
558 */
559
560 private struct pat *Parsed_pattern;
561
562 public void
563 pattern_init(char *pattern) {
564 Parsed_pattern = parse_pattern(pattern);
565 }
566
567 public int
568 pattern_match() {
569 return eval_exp(Parsed_pattern);
570 }
571
572 #ifdef DEBUG
573 /****************************************************************
574 *
575 * Visualizing parsed pattern
576 *
577 */
578
579 private void print_exp(struct pat *, int);
580
581 #define leveling(n) {int i; for(i = 0; i < (n); i++) putchar(SP);}
582
583 private void
584 print_exp(struct pat *p, int level){
585 switch (p->typ) {
586 case P_NOT:
587 leveling(level);
588 printf("!\n");
589 print_exp(p->dwn, level + 1);
590 break;
591 case P_AND:
592 leveling(level);
593 printf("&\n");
594 print_exp(p->nxt, level + 1);
595 print_exp(p->nxt->nxt, level + 1);
596 break;
597 case P_OR:
598 leveling(level);
599 printf("|\n");
600 print_exp(p->nxt, level + 1);
601 print_exp(p->nxt->nxt, level + 1);
602 break;
603 case P_EQ:
604 leveling(level);
605 printf("'%s' = '%s'\n", p->key, p->val);
606 break;
607 case P_NEQ:
608 leveling(level);
609 printf("'%s' != '%s'\n", p->key, p->val);
610 break;
611 case P_CEQ:
612 leveling(level);
613 printf("'%s' == '%s'\n", p->key, p->val);
614 break;
615 case P_CNE:
616 leveling(level);
617 printf("'%s' !== '%s'\n", p->key, p->val);
618 break;
619 case P_EXP:
620 print_exp(p->dwn, level + 1);
621 break;
622 default:
623 warn_exit("broken tree.");
624 }
625 }
626
627 int
628 main(int argc, char *argv[])
629 {
630 char *progname = getprognm(argv[0]);
631 struct pat *top;
632
633 warn_prog = progname;
634 if (argc != 2)
635 warn_exit("argc != 2.");
636 top = parse_pattern(argv[1]);
637 print_exp(top, 0);
638 exit(EXIT_SUCCESS);
639 }
640 #endif /* DEBUG */
641
642 /*
643 * Copyright (C) 2000-2005 Mew developing team.
644 * All rights reserved.
645 *
646 * Redistribution and use in source and binary forms, with or without
647 * modification, are permitted provided that the following conditions
648 * are met:
649 *
650 * 1. Redistributions of source code must retain the above copyright
651 * notice, this list of conditions and the following disclaimer.
652 * 2. Redistributions in binary form must reproduce the above copyright
653 * notice, this list of conditions and the following disclaimer in the
654 * documentation and/or other materials provided with the distribution.
655 * 3. Neither the name of the team nor the names of its contributors
656 * may be used to endorse or promote products derived from this software
657 * without specific prior written permission.
658 *
659 * THIS SOFTWARE IS PROVIDED BY THE TEAM AND CONTRIBUTORS ``AS IS'' AND
660 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
661 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
662 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE TEAM OR CONTRIBUTORS BE
663 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
664 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
665 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
666 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
667 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
668 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
669 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
670 */
671
672 /*
673 * pattern.c ends here
674 */