]> mj.ucw.cz Git - libucw.git/blob - lib/regex.c
4cc75d6c3db6490e6ea642424d3aeff1c7bcb140
[libucw.git] / lib / regex.c
1 /*
2  *      Sherlock Library -- Regular Expressions
3  *
4  *      (c) 1997 Martin Mares <mj@ucw.cz>
5  *      (c) 2001 Robert Spalek <robert@ucw.cz>
6  */
7
8 #include "lib/lib.h"
9 #include "lib/chartype.h"
10
11 #include <ctype.h>
12 #include <stdio.h>
13 #include <string.h>
14 #include <stdlib.h>
15 #include <regex.h>
16
17 #define INITIAL_MEM 1024                /* Initial space allocated for each pattern */
18 #define CHAR_SET_SIZE 256               /* How many characters in the character set.  */
19
20 struct regex {
21   struct re_pattern_buffer buf;
22   struct re_registers regs;             /* Must not change between re_match() calls */
23   int len_cache;
24 };
25
26 regex *
27 rx_compile(byte *p, int icase)
28 {
29   regex *r = xmalloc_zero(sizeof(regex));
30   const char *msg;
31
32   r->buf.buffer = xmalloc(INITIAL_MEM);
33   r->buf.allocated = INITIAL_MEM;
34   if (icase)
35     {
36       unsigned i;
37       r->buf.translate = xmalloc (CHAR_SET_SIZE);
38       /* Map uppercase characters to corresponding lowercase ones.  */
39       for (i = 0; i < CHAR_SET_SIZE; i++)
40         r->buf.translate[i] = Cupper(i) ? tolower (i) : i;
41     }
42   else
43     r->buf.translate = NULL;
44   msg = re_compile_pattern(p, strlen(p), &r->buf);
45   if (!msg)
46     return r;
47   die("Error parsing pattern `%s': %s", p, msg);
48 }
49
50 void
51 rx_free(regex *r)
52 {
53   xfree(r->buf.buffer);
54   xfree(r);
55 }
56
57 int
58 rx_match(regex *r, byte *s)
59 {
60   int len = strlen(s);
61
62   r->len_cache = len;
63   if (re_match(&r->buf, s, len, 0, &r->regs) < 0)
64     return 0;
65   if (r->regs.start[0] || r->regs.end[0] != len) /* XXX: Why regex doesn't enforce implicit "^...$" ? */
66     return 0;
67   return 1;
68 }
69
70 int
71 rx_subst(regex *r, byte *by, byte *src, byte *dest, uns destlen)
72 {
73   byte *end = dest + destlen - 1;
74
75   if (!rx_match(r, src))
76     return 0;
77
78   while (*by)
79     {
80       if (*by == '\\')
81         {
82           by++;
83           if (*by >= '0' && *by <= '9') /* \0 gets replaced by entire pattern */
84             {
85               uns j = *by++ - '0';
86               if (j < r->regs.num_regs)
87                 {
88                   byte *s = src + r->regs.start[j];
89                   uns i = r->regs.end[j] - r->regs.start[j];
90                   if (r->regs.start[j] > r->len_cache || r->regs.end[j] > r->len_cache)
91                     return -1;
92                   if (dest + i >= end)
93                     return -1;
94                   memcpy(dest, s, i);
95                   dest += i;
96                   continue;
97                 }
98             }
99         }
100       if (dest < end)
101         *dest++ = *by++;
102       else
103         return -1;
104     }
105   *dest = 0;
106   return 1;
107 }
108
109 #ifdef TEST
110
111 void main(int argc, char **argv)
112 {
113   regex *r;
114   byte buf1[256], buf2[256];
115
116   r = rx_compile(argv[1]);
117   while (fgets(buf1, sizeof(buf1), stdin))
118     {
119       char *p = strchr(buf1, '\n');
120       if (p)
121         *p = 0;
122       if (argc == 2)
123         {
124           if (rx_match(r, buf1))
125             puts("MATCH");
126           else
127             puts("NO MATCH");
128         }
129       else
130         {
131           int i = rx_subst(r, argv[2], buf1, buf2, sizeof(buf2));
132           if (i < 0)
133             puts("OVERFLOW");
134           else if (!i)
135             puts("NO MATCH");
136           else
137             puts(buf2);
138         }
139     }
140   rx_free(r);
141 }
142
143 #endif