1 /*
2  * Copyright (C) 2000-2007 Carsten Haitzler, Geoff Harrison and various contributors
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to
6  * deal in the Software without restriction, including without limitation the
7  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8  * sell copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies of the Software, its documentation and marketing & publicity
13  * materials, and acknowledgment shall be given in the documentation, materials
14  * and software packages that this Software was used.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
20  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22  */
23 #include "E.h"
24 
25 static int
isafter(int p,const char * s1,const char * s2)26 isafter(int p, const char *s1, const char *s2)
27 {
28    int                 i, j;
29    int                 len, len2;
30    int                 match;
31 
32    len = strlen(s1);
33    len2 = strlen(s2);
34 
35    match = 0;
36    for (i = p; i < len; i++)
37      {
38 	if (s1[i] == s2[0])
39 	  {
40 	     match = 1;
41 	     for (j = 0; j < len2; j++)
42 	       {
43 		  if ((i + j) >= len)
44 		     return -1;
45 		  if (s1[i + j] != s2[j])
46 		     match = 0;
47 	       }
48 	  }
49 	if (match)
50 	   return i + len2;
51      }
52    return -1;
53 }
54 
55 int
matchregexp(const char * rx,const char * s)56 matchregexp(const char *rx, const char *s)
57 {
58    int                 i, l, m;
59    int                 len, lenr;
60    int                 match;
61    char                rx2[1024];
62 
63    if (!s)
64       return 0;
65    if (!rx)
66       return 0;
67 
68    len = strlen(s);
69    l = 0;
70    lenr = 0;
71    match = 1;
72    if ((strcmp(rx, "*") || rx[0] == 0) && s[0] == 0)
73       return 0;
74 
75    if (rx[0] != '*')
76      {
77 	m = 0;
78 	while ((rx[l] != '*') && (rx[l]) && (m < 1023))
79 	   rx2[m++] = rx[l++];
80 	rx2[m] = 0;
81 	lenr = strlen(rx2);
82 	if (lenr > len)
83 	   return 0;
84 	for (i = 0; i < lenr; i++)
85 	  {
86 	     if (s[i] != rx[i])
87 		return 0;
88 	  }
89      }
90    if ((!rx[l]) && (s[lenr]))
91       return 0;
92    for (i = lenr; i < len;)
93      {
94 	if (rx[l])
95 	   l++;
96 	if (rx[l])
97 	  {
98 	     m = 0;
99 	     while ((rx[l] != '*') && (rx[l]) && (m < 1023))
100 		rx2[m++] = rx[l++];
101 	     rx2[m] = 0;
102 	     i = isafter(i, s, rx2);
103 	     if (i < 0)
104 		return 0;
105 	  }
106 	else
107 	  {
108 	     // We arrived at the end of the regex BUT if it doesn't
109 	     // end with the wildcard * and there are more chars
110 	     // in s remaining to be matched, we should return 0
111 	     if ((i < len) && (rx[l - 1] != '*'))
112 		return 0;
113 	     else
114 		return match;
115 	  }
116      }
117    return match;
118 }
119