1 |
adcroft |
1.1 |
/* |
2 |
|
|
$Id: search_alt.c,v 1.3 2001/05/07 18:10:20 rasc Exp $ |
3 |
|
|
** |
4 |
|
|
** This program and library is free software; you can redistribute it and/or |
5 |
|
|
** modify it under the terms of the GNU (Library) General Public License |
6 |
|
|
** as published by the Free Software Foundation; either version 2 |
7 |
|
|
** of the License, or any later version. |
8 |
|
|
** |
9 |
|
|
** This program is distributed in the hope that it will be useful, |
10 |
|
|
** but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 |
|
|
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 |
|
|
** GNU (Library) General Public License for more details. |
13 |
|
|
** |
14 |
|
|
** You should have received a copy of the GNU (Library) General Public License |
15 |
|
|
** along with this program; if not, write to the Free Software |
16 |
|
|
** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
17 |
|
|
** |
18 |
|
|
** |
19 |
|
|
** This file handles alternat search syntax handling |
20 |
|
|
** e.g. "+word -word word" (basic altavista, lycos, etc mode...) |
21 |
|
|
** word1 UND word2 (language specific boolean operators) |
22 |
|
|
** |
23 |
|
|
** |
24 |
|
|
** $$$ This module currently handles also "renamed" boolean search operators. |
25 |
|
|
** $$$ This part may be moved to the new search module, later |
26 |
|
|
** $$$ This would be more apropriate than this module... (2001-05-04 rasc) |
27 |
|
|
** |
28 |
|
|
** |
29 |
|
|
** |
30 |
|
|
** 2001-03-02 rasc initial coding |
31 |
|
|
** 2001-04-12 rasc Module init rewritten |
32 |
|
|
** |
33 |
|
|
*/ |
34 |
|
|
|
35 |
|
|
|
36 |
|
|
#include <string.h> |
37 |
|
|
#include "swish.h" |
38 |
|
|
#include "string.h" |
39 |
|
|
#include "mem.h" |
40 |
|
|
#include "error.h" |
41 |
|
|
#include "parse_conffile.h" |
42 |
|
|
#include "search.h" |
43 |
|
|
#include "search_alt.h" |
44 |
|
|
|
45 |
|
|
|
46 |
|
|
/* |
47 |
|
|
** ---------------------------------------------- |
48 |
|
|
** |
49 |
|
|
** Module management code starts here |
50 |
|
|
** |
51 |
|
|
** ---------------------------------------------- |
52 |
|
|
*/ |
53 |
|
|
|
54 |
|
|
|
55 |
|
|
/* |
56 |
|
|
-- init structures for this module |
57 |
|
|
*/ |
58 |
|
|
|
59 |
|
|
void initModule_SearchAlt (SWISH *sw) |
60 |
|
|
|
61 |
|
|
{ |
62 |
|
|
struct MOD_SearchAlt *msa; |
63 |
|
|
|
64 |
|
|
msa = (struct MOD_SearchAlt *) emalloc (sizeof (struct MOD_SearchAlt)); |
65 |
|
|
sw->SearchAlt = msa; |
66 |
|
|
|
67 |
|
|
msa->enableAltSearchSyntax = 0; /* default: enable only Swish syntax */ |
68 |
|
|
|
69 |
|
|
/* init default logical operator words (2001-03-12 rasc) */ |
70 |
|
|
msa->srch_op.and = estrdup (_AND_WORD); |
71 |
|
|
msa->srch_op.or = estrdup (_OR_WORD); |
72 |
|
|
msa->srch_op.not = estrdup (_NOT_WORD); |
73 |
|
|
msa->srch_op.defaultrule = AND_RULE; |
74 |
|
|
|
75 |
|
|
return; |
76 |
|
|
} |
77 |
|
|
|
78 |
|
|
|
79 |
|
|
/* |
80 |
|
|
-- release all wired memory for this module |
81 |
|
|
*/ |
82 |
|
|
|
83 |
|
|
void freeModule_SearchAlt (SWISH *sw) |
84 |
|
|
|
85 |
|
|
{ |
86 |
|
|
struct MOD_SearchAlt *msa = sw->SearchAlt; |
87 |
|
|
|
88 |
|
|
/* free logical operator words (2001-03-12 rasc) */ |
89 |
|
|
efree (msa->srch_op.and); |
90 |
|
|
efree (msa->srch_op.or); |
91 |
|
|
efree (msa->srch_op.not); |
92 |
|
|
|
93 |
|
|
efree (sw->SearchAlt); |
94 |
|
|
sw->SearchAlt = NULL; |
95 |
|
|
return; |
96 |
|
|
} |
97 |
|
|
|
98 |
|
|
|
99 |
|
|
|
100 |
|
|
/* |
101 |
|
|
** ---------------------------------------------- |
102 |
|
|
** |
103 |
|
|
** Module config code starts here |
104 |
|
|
** |
105 |
|
|
** ---------------------------------------------- |
106 |
|
|
*/ |
107 |
|
|
|
108 |
|
|
|
109 |
|
|
|
110 |
|
|
/* |
111 |
|
|
-- Config Directives |
112 |
|
|
-- Configuration directives for this Module |
113 |
|
|
-- return: 0/1 = none/config applied |
114 |
|
|
*/ |
115 |
|
|
|
116 |
|
|
int configModule_SearchAlt (SWISH *sw, StringList *sl) |
117 |
|
|
|
118 |
|
|
{ |
119 |
|
|
struct MOD_SearchAlt *msa = sw->SearchAlt; |
120 |
|
|
char *w0 = sl->word[0]; |
121 |
|
|
int retval = 1; |
122 |
|
|
|
123 |
|
|
/* $$$ this will not work unless swish is reading the config file also for search ... */ |
124 |
|
|
|
125 |
|
|
if (strcasecmp(w0, "EnableAltSearchSyntax")==0) { /* rasc 2001-02 */ |
126 |
|
|
msa->enableAltSearchSyntax = getYesNoOrAbort (sl, 1,1); |
127 |
|
|
} else if (strcasecmp(w0, "SwishSearchOperators")==0) { /* rasc 2001-03 */ |
128 |
|
|
if(sl->n == 4) { |
129 |
|
|
msa->srch_op.and = sl->word[1]; |
130 |
|
|
msa->srch_op.or = sl->word[2]; |
131 |
|
|
msa->srch_op.not = sl->word[3]; |
132 |
|
|
} else progerr("%s: requires 3 parameters (and-, or-, not-word)",w0); |
133 |
|
|
} |
134 |
|
|
else if (strcasecmp(w0, "SwishSearchDefaultRule")==0) { /* rasc 2001-03 */ |
135 |
|
|
if(sl->n == 2) { |
136 |
|
|
msa->srch_op.defaultrule = u_SelectDefaultRulenum(sw,sl->word[1]); |
137 |
|
|
if (msa->srch_op.defaultrule == NO_RULE) { |
138 |
|
|
progerr("%s: requires \"%s\" or \"%s\"",w0, msa->srch_op.and, msa->srch_op.or); |
139 |
|
|
} |
140 |
|
|
} else progerr("%s: requires 1 parameter",w0); |
141 |
|
|
} |
142 |
|
|
else { |
143 |
|
|
retval = 0; /* not a module directive */ |
144 |
|
|
} |
145 |
|
|
|
146 |
|
|
|
147 |
|
|
return retval; |
148 |
|
|
} |
149 |
|
|
|
150 |
|
|
|
151 |
|
|
/* |
152 |
|
|
** ---------------------------------------------- |
153 |
|
|
** |
154 |
|
|
** Module code starts here |
155 |
|
|
** |
156 |
|
|
** ---------------------------------------------- |
157 |
|
|
*/ |
158 |
|
|
|
159 |
|
|
|
160 |
|
|
/* |
161 |
|
|
-- a simple routine to convert a alternative (+-) search string |
162 |
|
|
-- "word1 +word2 +word3 word4 -word5" |
163 |
|
|
-- to a swish-e search string... |
164 |
|
|
-- only a basic altavista/lycos/etc. syntax conversion is done |
165 |
|
|
-- Return: (char *) swish-search-string |
166 |
|
|
*/ |
167 |
|
|
|
168 |
|
|
char *convAltSearch2SwishStr (char *str) |
169 |
|
|
{ |
170 |
|
|
StringList *slst; |
171 |
|
|
char *sr_new, *p, *tmp; |
172 |
|
|
char *sr_or,*sr_and,*sr_not; |
173 |
|
|
char **s_or, **s_and, **s_not; |
174 |
|
|
int n_or, n_and, n_not; |
175 |
|
|
int i; |
176 |
|
|
|
177 |
|
|
|
178 |
|
|
sr_new = (char *)emalloc (2 * MAXSTRLEN); |
179 |
|
|
sr_or = (char *)emalloc (2 * MAXSTRLEN); |
180 |
|
|
sr_and = (char *)emalloc (2 * MAXSTRLEN); |
181 |
|
|
sr_not = (char *)emalloc (2 * MAXSTRLEN); |
182 |
|
|
tmp = (char *)emalloc (2 * MAXSTRLEN); |
183 |
|
|
*sr_new = *sr_or = *sr_and = *sr_not = '\0'; |
184 |
|
|
|
185 |
|
|
|
186 |
|
|
slst = parse_line (str); /* parse into words */ |
187 |
|
|
|
188 |
|
|
i = (slst->n + 1) * sizeof (char *); |
189 |
|
|
s_or = (char **)emalloc (i); |
190 |
|
|
s_and = (char **)emalloc (i); |
191 |
|
|
s_not = (char **)emalloc (i); |
192 |
|
|
*s_or = *s_and = *s_not = (char *)NULL; |
193 |
|
|
n_or = n_and = n_not = 0; |
194 |
|
|
|
195 |
|
|
|
196 |
|
|
/* parse string into and, not, or */ |
197 |
|
|
|
198 |
|
|
for (i=0; i < slst->n; i++) { |
199 |
|
|
p = slst->word[i]; |
200 |
|
|
switch (*p) { |
201 |
|
|
case '-': /* Exclude, Not */ |
202 |
|
|
*(s_not + (n_not++)) = p+1; |
203 |
|
|
break; |
204 |
|
|
|
205 |
|
|
case '+': /* AND */ |
206 |
|
|
*(s_and + (n_and++)) = p+1; |
207 |
|
|
break; |
208 |
|
|
|
209 |
|
|
default: /* OR */ |
210 |
|
|
*(s_or + (n_or++)) = p; |
211 |
|
|
break; |
212 |
|
|
} |
213 |
|
|
} |
214 |
|
|
*(s_and +(n_and)) = NULL; |
215 |
|
|
*(s_or +(n_or)) = NULL; |
216 |
|
|
*(s_not +(n_not)) = NULL; |
217 |
|
|
|
218 |
|
|
|
219 |
|
|
/* |
220 |
|
|
-- process or-list (word word) |
221 |
|
|
-- if also "+str" are present include these into "or"-list |
222 |
|
|
-- this is necessary ($$$ to be checked!) |
223 |
|
|
*/ |
224 |
|
|
|
225 |
|
|
if (*s_or) { |
226 |
|
|
for (i=0; i<n_or; i++) { |
227 |
|
|
if (*sr_or) { /* not first word */ |
228 |
|
|
sprintf (tmp," %s ",OR_WORD); |
229 |
|
|
strcat (sr_or,tmp); |
230 |
|
|
} |
231 |
|
|
strcat (sr_or, *(s_or + i)); |
232 |
|
|
} |
233 |
|
|
/* -- include And-words (if any) into OR-list */ |
234 |
|
|
for (i=0; i<n_and; i++) { |
235 |
|
|
sprintf (tmp," %s %s",OR_WORD,*(s_and+i)); |
236 |
|
|
strcat (sr_or,tmp); |
237 |
|
|
} |
238 |
|
|
} |
239 |
|
|
|
240 |
|
|
|
241 |
|
|
/* |
242 |
|
|
-- process and-list (+word +word) |
243 |
|
|
|
244 |
|
|
*/ |
245 |
|
|
|
246 |
|
|
for (i=0; i<n_and; i++) { |
247 |
|
|
if (*sr_and) { /* not first word */ |
248 |
|
|
sprintf (tmp," %s ",AND_WORD); |
249 |
|
|
strcat (sr_and,tmp); |
250 |
|
|
} |
251 |
|
|
strcat (sr_and, *(s_and + i)); |
252 |
|
|
} |
253 |
|
|
|
254 |
|
|
|
255 |
|
|
/* |
256 |
|
|
-- process not-list (-word -word) |
257 |
|
|
|
258 |
|
|
*/ |
259 |
|
|
|
260 |
|
|
for (i=0; i<n_not; i++) { |
261 |
|
|
if (*sr_not) { /* not first word */ |
262 |
|
|
sprintf (tmp," %s ",AND_WORD); |
263 |
|
|
strcat (sr_not,tmp); |
264 |
|
|
} |
265 |
|
|
strcat (sr_not,*(s_not + i)); |
266 |
|
|
} |
267 |
|
|
|
268 |
|
|
|
269 |
|
|
/* |
270 |
|
|
-- now build one new searchstring |
271 |
|
|
-- merge and, or, not strings with AND (if not empty) |
272 |
|
|
-- build something like: |
273 |
|
|
-- (orw <or> ... ) <AND> (andw <and> ...) not (notw <and> ...) |
274 |
|
|
*/ |
275 |
|
|
|
276 |
|
|
if (*sr_or) { |
277 |
|
|
sprintf (sr_new,"(%s)",sr_or); |
278 |
|
|
} |
279 |
|
|
if (*sr_and) { |
280 |
|
|
if (*sr_new) { |
281 |
|
|
sprintf (tmp," %s %s (%s)",sr_new,AND_WORD,sr_and); |
282 |
|
|
} else { |
283 |
|
|
sprintf (tmp,"(%s)",sr_and); |
284 |
|
|
} |
285 |
|
|
strcpy (sr_new,tmp); |
286 |
|
|
} |
287 |
|
|
if (*sr_not) { |
288 |
|
|
if (*sr_new) { |
289 |
|
|
sprintf (tmp," %s %s (%s)",sr_new,NOT_WORD,sr_not); |
290 |
|
|
} else { |
291 |
|
|
sprintf (tmp,"%s (%s)",NOT_WORD,sr_not); |
292 |
|
|
} |
293 |
|
|
strcpy (sr_new,tmp); |
294 |
|
|
} |
295 |
|
|
|
296 |
|
|
|
297 |
|
|
|
298 |
|
|
efree (s_or); |
299 |
|
|
efree (s_and); |
300 |
|
|
efree (s_not); |
301 |
|
|
efree (tmp); |
302 |
|
|
freeStringList (slst); |
303 |
|
|
return sr_new; |
304 |
|
|
} |
305 |
|
|
|