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