]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
c07d5b2d | 2 | * "$Id: search.c 177 2006-06-21 00:20:03Z jlovell $" |
ef416fc2 | 3 | * |
4 | * Search routines for the Common UNIX Printing System (CUPS). | |
5 | * | |
6 | * Copyright 1997-2005 by Easy Software Products. | |
7 | * | |
8 | * These coded instructions, statements, and computer programs are the | |
9 | * property of Easy Software Products and are protected by Federal | |
10 | * copyright law. Distribution and use rights are outlined in the file | |
11 | * "LICENSE.txt" which should have been included with this file. If this | |
12 | * file is missing or damaged please contact Easy Software Products | |
13 | * at: | |
14 | * | |
15 | * Attn: CUPS Licensing Information | |
16 | * Easy Software Products | |
17 | * 44141 Airport View Drive, Suite 204 | |
18 | * Hollywood, Maryland 20636 USA | |
19 | * | |
20 | * Voice: (301) 373-9600 | |
21 | * EMail: cups-info@cups.org | |
22 | * WWW: http://www.cups.org | |
23 | * | |
24 | * Contents: | |
25 | * | |
26 | * cgiCompileSearch() - Compile a search string. | |
27 | * cgiDoSearch() - Do a search of some text. | |
28 | * cgiFreeSearch() - Free a compiled search context. | |
29 | */ | |
30 | ||
31 | /* | |
32 | * Include necessary headers... | |
33 | */ | |
34 | ||
35 | #include "cgi-private.h" | |
36 | #include <regex.h> | |
37 | ||
38 | ||
39 | /* | |
40 | * 'cgiCompileSearch()' - Compile a search string. | |
41 | */ | |
42 | ||
43 | void * /* O - Search context */ | |
44 | cgiCompileSearch(const char *query) /* I - Query string */ | |
45 | { | |
46 | regex_t *re; /* Regular expression */ | |
47 | char *s, /* Regular expression string */ | |
48 | *sptr, /* Pointer into RE string */ | |
49 | *sword; /* Pointer to start of word */ | |
50 | int slen; /* Allocated size of RE string */ | |
51 | const char *qptr, /* Pointer into query string */ | |
52 | *qend; /* End of current word */ | |
53 | const char *prefix; /* Prefix to add to next word */ | |
54 | int quoted; /* Word is quoted */ | |
55 | int wlen; /* Word length */ | |
56 | char *lword; /* Last word in query */ | |
57 | ||
58 | ||
59 | DEBUG_printf(("help_compile_search(query=\"%s\")\n", query ? query : "(nil)")); | |
60 | ||
61 | /* | |
62 | * Allocate a regular expression storage structure... | |
63 | */ | |
64 | ||
65 | re = (regex_t *)calloc(1, sizeof(regex_t)); | |
66 | ||
67 | /* | |
68 | * Allocate a buffer to hold the regular expression string, starting | |
69 | * at 1024 bytes or 3 times the length of the query string, whichever | |
70 | * is greater. We'll expand the string as needed... | |
71 | */ | |
72 | ||
73 | slen = strlen(query) * 3; | |
74 | if (slen < 1024) | |
75 | slen = 1024; | |
76 | ||
77 | s = (char *)malloc(slen); | |
78 | ||
79 | /* | |
80 | * Copy the query string to the regular expression, handling basic | |
81 | * AND and OR logic... | |
82 | */ | |
83 | ||
84 | prefix = ".*"; | |
85 | qptr = query; | |
86 | sptr = s; | |
87 | lword = NULL; | |
88 | ||
89 | while (*qptr) | |
90 | { | |
91 | /* | |
92 | * Skip leading whitespace... | |
93 | */ | |
94 | ||
95 | while (isspace(*qptr & 255)) | |
96 | qptr ++; | |
97 | ||
98 | if (!*qptr) | |
99 | break; | |
100 | ||
101 | /* | |
102 | * Find the end of the current word... | |
103 | */ | |
104 | ||
105 | if (*qptr == '\"' || *qptr == '\'') | |
106 | { | |
107 | /* | |
108 | * Scan quoted string... | |
109 | */ | |
110 | ||
111 | quoted = *qptr ++; | |
112 | for (qend = qptr; *qend && *qend != quoted; qend ++); | |
113 | ||
114 | if (!*qend) | |
115 | { | |
116 | /* | |
117 | * No closing quote, error out! | |
118 | */ | |
119 | ||
120 | free(s); | |
121 | free(re); | |
122 | ||
123 | if (lword) | |
124 | free(lword); | |
125 | ||
126 | return (NULL); | |
127 | } | |
128 | } | |
129 | else | |
130 | { | |
131 | /* | |
132 | * Scan whitespace-delimited string... | |
133 | */ | |
134 | ||
135 | quoted = 0; | |
136 | for (qend = qptr + 1; *qend && !isspace(*qend); qend ++); | |
137 | } | |
138 | ||
139 | wlen = qend - qptr; | |
140 | ||
141 | /* | |
142 | * Look for logic words: AND, OR | |
143 | */ | |
144 | ||
145 | if (wlen == 3 && !strncasecmp(qptr, "AND", 3)) | |
146 | { | |
147 | /* | |
148 | * Logical AND with the following text... | |
149 | */ | |
150 | ||
151 | if (sptr > s) | |
152 | prefix = ".*"; | |
153 | ||
154 | qptr = qend; | |
155 | } | |
156 | else if (wlen == 2 && !strncasecmp(qptr, "OR", 2)) | |
157 | { | |
158 | /* | |
159 | * Logical OR with the following text... | |
160 | */ | |
161 | ||
162 | if (sptr > s) | |
163 | prefix = ".*|.*"; | |
164 | ||
165 | qptr = qend; | |
166 | } | |
167 | else | |
168 | { | |
169 | /* | |
170 | * Add a search word, making sure we have enough room for the | |
171 | * string + RE overhead... | |
172 | */ | |
173 | ||
174 | wlen = (sptr - s) + 4 * wlen + 2 * strlen(prefix) + 4; | |
175 | ||
176 | if (wlen > slen) | |
177 | { | |
178 | /* | |
179 | * Expand the RE string buffer... | |
180 | */ | |
181 | ||
182 | char *temp; /* Temporary string pointer */ | |
183 | ||
184 | ||
185 | slen = wlen + 128; | |
186 | temp = (char *)realloc(s, slen); | |
187 | if (!temp) | |
188 | { | |
189 | free(s); | |
190 | free(re); | |
191 | ||
192 | if (lword) | |
193 | free(lword); | |
194 | ||
195 | return (NULL); | |
196 | } | |
197 | ||
198 | sptr = temp + (sptr - s); | |
199 | s = temp; | |
200 | } | |
201 | ||
202 | /* | |
203 | * Add the prefix string... | |
204 | */ | |
205 | ||
206 | strcpy(sptr, prefix); | |
207 | sptr += strlen(sptr); | |
208 | ||
209 | /* | |
210 | * Then quote the remaining word characters as needed for the | |
211 | * RE... | |
212 | */ | |
213 | ||
214 | sword = sptr; | |
215 | ||
216 | while (qptr < qend) | |
217 | { | |
218 | /* | |
219 | * Quote: ^ . [ $ ( ) | * + ? { \ | |
220 | */ | |
221 | ||
222 | if (strchr("^.[$()|*+?{\\", *qptr)) | |
223 | *sptr++ = '\\'; | |
224 | ||
225 | *sptr++ = *qptr++; | |
226 | } | |
227 | ||
228 | /* | |
229 | * For "word1 AND word2", add reciprocal "word2 AND word1"... | |
230 | */ | |
231 | ||
232 | if (!strcmp(prefix, ".*") && lword) | |
233 | { | |
234 | char *lword2; /* New "last word" */ | |
235 | ||
236 | ||
237 | lword2 = strdup(sword); | |
238 | ||
239 | strcpy(sptr, ".*|.*"); | |
240 | sptr += 5; | |
241 | ||
242 | strcpy(sptr, lword2); | |
243 | sptr += strlen(sptr); | |
244 | ||
245 | strcpy(sptr, ".*"); | |
246 | sptr += 2; | |
247 | ||
248 | strcpy(sptr, lword); | |
249 | sptr += strlen(sptr); | |
250 | ||
251 | free(lword); | |
252 | lword = lword2; | |
253 | } | |
254 | else | |
255 | { | |
256 | if (lword) | |
257 | free(lword); | |
258 | ||
259 | lword = strdup(sword); | |
260 | } | |
261 | ||
262 | prefix = ".*|.*"; | |
263 | } | |
264 | ||
265 | /* | |
266 | * Advance to the next string... | |
267 | */ | |
268 | ||
269 | if (quoted) | |
270 | qptr ++; | |
271 | } | |
272 | ||
273 | if (lword) | |
274 | free(lword); | |
275 | ||
276 | if (sptr > s) | |
277 | strcpy(sptr, ".*"); | |
278 | else | |
279 | { | |
280 | /* | |
281 | * No query data, return NULL... | |
282 | */ | |
283 | ||
284 | free(s); | |
285 | free(re); | |
286 | ||
287 | return (NULL); | |
288 | } | |
289 | ||
290 | /* | |
291 | * Compile the regular expression... | |
292 | */ | |
293 | ||
294 | DEBUG_printf((" s=\"%s\"\n", s)); | |
295 | ||
296 | if (regcomp(re, s, REG_EXTENDED | REG_ICASE)) | |
297 | { | |
298 | free(re); | |
299 | free(s); | |
300 | ||
301 | return (NULL); | |
302 | } | |
303 | ||
304 | /* | |
305 | * Free the RE string and return the new regular expression we compiled... | |
306 | */ | |
307 | ||
308 | free(s); | |
309 | ||
310 | return ((void *)re); | |
311 | } | |
312 | ||
313 | ||
314 | /* | |
315 | * 'cgiDoSearch()' - Do a search of some text. | |
316 | */ | |
317 | ||
318 | int /* O - Number of matches */ | |
319 | cgiDoSearch(void *search, /* I - Search context */ | |
320 | const char *text) /* I - Text to search */ | |
321 | { | |
322 | int i; /* Looping var */ | |
323 | regmatch_t matches[100]; /* RE matches */ | |
324 | ||
325 | ||
326 | /* | |
327 | * Range check... | |
328 | */ | |
329 | ||
330 | if (!search || !text) | |
331 | return (0); | |
332 | ||
333 | /* | |
334 | * Do a lookup... | |
335 | */ | |
336 | ||
337 | if (!regexec((regex_t *)search, text, sizeof(matches) / sizeof(matches[0]), | |
338 | matches, 0)) | |
339 | { | |
340 | /* | |
341 | * Figure out the number of matches in the string... | |
342 | */ | |
343 | ||
344 | for (i = 0; i < (int)(sizeof(matches) / sizeof(matches[0])); i ++) | |
345 | if (matches[i].rm_so < 0) | |
346 | break; | |
347 | ||
348 | return (i); | |
349 | } | |
350 | else | |
351 | return (0); | |
352 | } | |
353 | ||
354 | ||
355 | /* | |
356 | * 'cgiFreeSearch()' - Free a compiled search context. | |
357 | */ | |
358 | ||
359 | void | |
360 | cgiFreeSearch(void *search) /* I - Search context */ | |
361 | { | |
362 | regfree((regex_t *)search); | |
363 | } | |
364 | ||
365 | ||
366 | /* | |
c07d5b2d | 367 | * End of "$Id: search.c 177 2006-06-21 00:20:03Z jlovell $". |
ef416fc2 | 368 | */ |