994
|
1 /* matchs.c */
|
|
2 #include <stdio.h>
|
|
3 #include "tools.h"
|
|
4
|
|
5 /* Compares line and pattern. Line is a character string while pat
|
|
6 * is a pattern template made by getpat().
|
|
7 * Returns:
|
|
8 * 1. A zero if no match was found.
|
|
9 *
|
|
10 * 2. A pointer to the last character satisfing the match
|
|
11 * if ret_endp is non-zero.
|
|
12 *
|
|
13 * 3. A pointer to the beginning of the matched string if
|
|
14 * ret_endp is zero.
|
|
15 *
|
|
16 * e.g.:
|
|
17 *
|
|
18 * matchs ("1234567890", getpat("4[0-9]*7), 0);
|
|
19 * will return a pointer to the '4', while:
|
|
20 *
|
|
21 * matchs ("1234567890", getpat("4[0-9]*7), 1);
|
|
22 * will return a pointer to the '7'.
|
|
23 */
|
|
24 char *
|
|
25 matchs(line, pat, ret_endp)
|
|
26 char *line;
|
|
27 TOKEN *pat;
|
|
28 int ret_endp;
|
|
29 {
|
|
30
|
|
31 char *rval, *bptr;
|
|
32 char *line2;
|
|
33 TOKEN *pat2;
|
|
34 char c;
|
|
35 short ok;
|
|
36
|
|
37 bptr = line;
|
|
38
|
|
39 while (*line) {
|
|
40
|
|
41 if (pat && pat->tok == LITCHAR) {
|
|
42 while (*line) {
|
|
43 pat2 = pat;
|
|
44 line2 = line;
|
|
45 if (*line2 != pat2->lchar) {
|
|
46 c = pat2->lchar;
|
|
47 while (*line2 && *line2 != c) ++line2;
|
|
48 line = line2;
|
|
49 if (*line2 == '\0') break;
|
|
50 }
|
|
51 ok = 1;
|
|
52 ++line2;
|
|
53 pat2 = pat2->next;
|
|
54 while (pat2 && pat2->tok == LITCHAR) {
|
|
55 if (*line2 != pat2->lchar) {
|
|
56 ok = 0;
|
|
57 break;
|
|
58 }
|
|
59 ++line2;
|
|
60 pat2 = pat2->next;
|
|
61 }
|
|
62 if (!pat2) {
|
|
63 if (ret_endp)
|
|
64 return(--line2);
|
|
65 else
|
|
66 return(line);
|
|
67 } else if (ok)
|
|
68 break;
|
|
69 ++line;
|
|
70 }
|
|
71 if (*line == '\0') return(0);
|
|
72 } else {
|
|
73 line2 = line;
|
|
74 pat2 = pat;
|
|
75 }
|
|
76 if ((rval = amatch(line2, pat2, bptr)) == 0) {
|
|
77 if (pat && pat->tok == BOL) break;
|
|
78 line++;
|
|
79 } else {
|
|
80 if (rval > bptr && rval > line)
|
|
81 rval--; /* point to last char matched */
|
|
82 rval = ret_endp ? rval : line;
|
|
83 break;
|
|
84 }
|
|
85 }
|
|
86 return(rval);
|
|
87 }
|
|
88
|