| 22 | */ |
| 23 | |
| 24 | void * /* O - Search context */ |
| 25 | cgiCompileSearch(const char *query) /* I - Query string */ |
| 26 | { |
| 27 | regex_t *re; /* Regular expression */ |
| 28 | char *s, /* Regular expression string */ |
| 29 | *sptr, /* Pointer into RE string */ |
| 30 | *sword; /* Pointer to start of word */ |
| 31 | size_t slen; /* Allocated size of RE string */ |
| 32 | const char *qptr, /* Pointer into query string */ |
| 33 | *qend; /* End of current word */ |
| 34 | const char *prefix; /* Prefix to add to next word */ |
| 35 | int quoted; /* Word is quoted */ |
| 36 | size_t wlen; /* Word length */ |
| 37 | char *lword; /* Last word in query */ |
| 38 | |
| 39 | |
| 40 | /* |
| 41 | * Range check input... |
| 42 | */ |
| 43 | |
| 44 | if (!query) |
| 45 | return (NULL); |
| 46 | |
| 47 | /* |
| 48 | * Allocate a regular expression storage structure... |
| 49 | */ |
| 50 | |
| 51 | if ((re = (regex_t *)calloc(1, sizeof(regex_t))) == NULL) |
| 52 | return (NULL); |
| 53 | |
| 54 | /* |
| 55 | * Allocate a buffer to hold the regular expression string, starting |
| 56 | * at 1024 bytes or 3 times the length of the query string, whichever |
| 57 | * is greater. We'll expand the string as needed... |
| 58 | */ |
| 59 | |
| 60 | slen = strlen(query) * 3; |
| 61 | if (slen < 1024) |
| 62 | slen = 1024; |
| 63 | |
| 64 | if ((s = (char *)malloc(slen)) == NULL) |
| 65 | { |
| 66 | free(re); |
| 67 | return (NULL); |
| 68 | } |
| 69 | |
| 70 | /* |
| 71 | * Copy the query string to the regular expression, handling basic |
| 72 | * AND and OR logic... |
| 73 | */ |
| 74 | |
| 75 | prefix = ".*"; |
| 76 | qptr = query; |
| 77 | sptr = s; |
| 78 | lword = NULL; |
| 79 | |
| 80 | while (*qptr) |
| 81 | { |
no test coverage detected