]>
git.ipfire.org Git - thirdparty/cups.git/blob - cgi-bin/search.c
ad1f5ed0ead0b4e602a0c107c78ff274ef451040
2 * Search routines for CUPS.
4 * Copyright 2007-2018 by Apple Inc.
5 * Copyright 1997-2006 by Easy Software Products.
7 * Licensed under Apache License v2.0. See the file "LICENSE" for more
12 * Include necessary headers...
15 #include "cgi-private.h"
20 * 'cgiCompileSearch()' - Compile a search string.
23 void * /* O - Search context */
24 cgiCompileSearch(const char *query
) /* I - Query string */
26 regex_t
*re
; /* Regular expression */
27 char *s
, /* Regular expression string */
28 *sptr
, /* Pointer into RE string */
29 *sword
; /* Pointer to start of word */
30 size_t slen
; /* Allocated size of RE string */
31 const char *qptr
, /* Pointer into query string */
32 *qend
; /* End of current word */
33 const char *prefix
; /* Prefix to add to next word */
34 int quoted
; /* Word is quoted */
35 size_t wlen
; /* Word length */
36 char *lword
; /* Last word in query */
39 DEBUG_printf(("cgiCompileSearch(query=\"%s\")\n", query
));
42 * Range check input...
49 * Allocate a regular expression storage structure...
52 if ((re
= (regex_t
*)calloc(1, sizeof(regex_t
))) == NULL
)
56 * Allocate a buffer to hold the regular expression string, starting
57 * at 1024 bytes or 3 times the length of the query string, whichever
58 * is greater. We'll expand the string as needed...
61 slen
= strlen(query
) * 3;
65 if ((s
= (char *)malloc(slen
)) == NULL
)
72 * Copy the query string to the regular expression, handling basic
84 * Skip leading whitespace...
87 while (isspace(*qptr
& 255))
94 * Find the end of the current word...
97 if (*qptr
== '\"' || *qptr
== '\'')
100 * Scan quoted string...
104 for (qend
= qptr
; *qend
&& *qend
!= quoted
; qend
++);
109 * No closing quote, error out!
124 * Scan whitespace-delimited string...
128 for (qend
= qptr
+ 1; *qend
&& !isspace(*qend
); qend
++);
131 wlen
= (size_t)(qend
- qptr
);
134 * Look for logic words: AND, OR
137 if (wlen
== 3 && !_cups_strncasecmp(qptr
, "AND", 3))
140 * Logical AND with the following text...
148 else if (wlen
== 2 && !_cups_strncasecmp(qptr
, "OR", 2))
151 * Logical OR with the following text...
162 * Add a search word, making sure we have enough room for the
163 * string + RE overhead...
166 wlen
= (size_t)(sptr
- s
) + 2 * 4 * wlen
+ 2 * strlen(prefix
) + 11;
168 wlen
+= strlen(lword
);
173 * Expand the RE string buffer...
176 char *temp
; /* Temporary string pointer */
180 temp
= (char *)realloc(s
, slen
);
192 sptr
= temp
+ (sptr
- s
);
197 * Add the prefix string...
200 memcpy(sptr
, prefix
, strlen(prefix
) + 1);
201 sptr
+= strlen(sptr
);
204 * Then quote the remaining word characters as needed for the
213 * Quote: ^ . [ $ ( ) | * + ? { \
216 if (strchr("^.[$()|*+?{\\", *qptr
))
225 * For "word1 AND word2", add reciprocal "word2 AND word1"...
228 if (!strcmp(prefix
, ".*") && lword
)
230 char *lword2
; /* New "last word" */
233 if ((lword2
= strdup(sword
)) == NULL
)
241 memcpy(sptr
, ".*|.*", 6);
244 memcpy(sptr
, lword2
, strlen(lword2
) + 1);
245 sptr
+= strlen(sptr
);
247 memcpy(sptr
, ".*", 3);
250 memcpy(sptr
, lword
, strlen(lword
) + 1);
251 sptr
+= strlen(sptr
);
261 lword
= strdup(sword
);
268 * Advance to the next string...
279 memcpy(sptr
, ".*", 3);
283 * No query data, return NULL...
293 * Compile the regular expression...
296 DEBUG_printf((" s=\"%s\"\n", s
));
298 if (regcomp(re
, s
, REG_EXTENDED
| REG_ICASE
))
307 * Free the RE string and return the new regular expression we compiled...
317 * 'cgiDoSearch()' - Do a search of some text.
320 int /* O - Number of matches */
321 cgiDoSearch(void *search
, /* I - Search context */
322 const char *text
) /* I - Text to search */
324 int i
; /* Looping var */
325 regmatch_t matches
[100]; /* RE matches */
332 if (!search
|| !text
)
339 if (!regexec((regex_t
*)search
, text
, sizeof(matches
) / sizeof(matches
[0]),
343 * Figure out the number of matches in the string...
346 for (i
= 0; i
< (int)(sizeof(matches
) / sizeof(matches
[0])); i
++)
347 if (matches
[i
].rm_so
< 0)
358 * 'cgiFreeSearch()' - Free a compiled search context.
362 cgiFreeSearch(void *search
) /* I - Search context */
364 regfree((regex_t
*)search
);