1 |
adcroft |
1.1 |
/* |
2 |
|
|
$Id: error.c,v 1.19 2002/08/22 22:58:39 whmoseley Exp $ |
3 |
|
|
** Copyright (C) 1995, 1996, 1997, 1998 Hewlett-Packard Company |
4 |
|
|
** Originally by Kevin Hughes, kev@kevcom.com, 3/11/94 |
5 |
|
|
** |
6 |
|
|
** This program and library is free software; you can redistribute it and/or |
7 |
|
|
** modify it under the terms of the GNU (Library) General Public License |
8 |
|
|
** as published by the Free Software Foundation; either version 2 |
9 |
|
|
** of the License, or any later version. |
10 |
|
|
** |
11 |
|
|
** This program is distributed in the hope that it will be useful, |
12 |
|
|
** but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
|
|
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
|
|
** GNU (Library) General Public License for more details. |
15 |
|
|
** |
16 |
|
|
** You should have received a copy of the GNU (Library) General Public License |
17 |
|
|
** along with this program; if not, write to the Free Software |
18 |
|
|
** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
19 |
|
|
** |
20 |
|
|
** |
21 |
|
|
** 2001-02-12 rasc rewritten (progerr uses vargs, now) |
22 |
|
|
** |
23 |
|
|
*/ |
24 |
|
|
|
25 |
|
|
#include "swish.h" |
26 |
|
|
#include <stdio.h> |
27 |
|
|
#include <stdlib.h> |
28 |
|
|
#include <stdarg.h> |
29 |
|
|
#include <string.h> |
30 |
|
|
#include "error.h" |
31 |
|
|
#include <errno.h> |
32 |
|
|
|
33 |
|
|
|
34 |
|
|
/* |
35 |
|
|
-- print program error message (like printf) |
36 |
|
|
-- exit (1) |
37 |
|
|
*/ |
38 |
|
|
|
39 |
|
|
|
40 |
|
|
/* Allow overriding swish-e's old behavior of errors to stdout */ |
41 |
|
|
|
42 |
|
|
FILE *error_handle; |
43 |
|
|
|
44 |
|
|
void set_error_handle( FILE *where ) |
45 |
|
|
{ |
46 |
|
|
error_handle = where; |
47 |
|
|
} |
48 |
|
|
|
49 |
|
|
void SwishErrorsToStderr( void ) |
50 |
|
|
{ |
51 |
|
|
error_handle = stderr; |
52 |
|
|
} |
53 |
|
|
|
54 |
|
|
|
55 |
|
|
void progerr(char *msgfmt,...) |
56 |
|
|
{ |
57 |
|
|
va_list args; |
58 |
|
|
|
59 |
|
|
va_start (args,msgfmt); |
60 |
|
|
fprintf (error_handle, "err: "); |
61 |
|
|
vfprintf (error_handle, msgfmt, args); |
62 |
|
|
fprintf (error_handle, "\n.\n"); |
63 |
|
|
va_end (args); |
64 |
|
|
|
65 |
|
|
exit(1); |
66 |
|
|
} |
67 |
|
|
|
68 |
|
|
|
69 |
|
|
/* |
70 |
|
|
-- print program error message (like printf) |
71 |
|
|
-- includes text of errno at end of message |
72 |
|
|
-- exit (1) |
73 |
|
|
*/ |
74 |
|
|
|
75 |
|
|
void progerrno(char *msgfmt,...) |
76 |
|
|
{ |
77 |
|
|
va_list args; |
78 |
|
|
|
79 |
|
|
va_start (args,msgfmt); |
80 |
|
|
fprintf (error_handle, "err: "); |
81 |
|
|
vfprintf (error_handle, msgfmt, args); |
82 |
|
|
fprintf (error_handle, "%s", strerror(errno)); |
83 |
|
|
fprintf (error_handle, "\n.\n"); |
84 |
|
|
va_end (args); |
85 |
|
|
exit(1); |
86 |
|
|
} |
87 |
|
|
|
88 |
|
|
/********** These are an attempt to prevent aborting in the library *********/ |
89 |
|
|
|
90 |
|
|
void set_progerr(int errornum, SWISH *sw, char *msgfmt,...) |
91 |
|
|
{ |
92 |
|
|
va_list args; |
93 |
|
|
|
94 |
|
|
sw->lasterror = errornum; |
95 |
|
|
|
96 |
|
|
va_start (args,msgfmt); |
97 |
|
|
vsnprintf (sw->lasterrorstr, MAX_ERROR_STRING_LEN, msgfmt, args); |
98 |
|
|
va_end (args); |
99 |
|
|
} |
100 |
|
|
|
101 |
|
|
|
102 |
|
|
|
103 |
|
|
void set_progerrno(int errornum, SWISH *sw, char *msgfmt,...) |
104 |
|
|
{ |
105 |
|
|
va_list args; |
106 |
|
|
char *errstr = strerror(errno); |
107 |
|
|
|
108 |
|
|
sw->lasterror = errornum; |
109 |
|
|
|
110 |
|
|
va_start (args,msgfmt); |
111 |
|
|
vsnprintf (sw->lasterrorstr, MAX_ERROR_STRING_LEN - strlen( errstr ), msgfmt, args); |
112 |
|
|
strcat( sw->lasterrorstr, errstr ); |
113 |
|
|
va_end (args); |
114 |
|
|
} |
115 |
|
|
|
116 |
|
|
|
117 |
|
|
|
118 |
|
|
/* only print a warning (also to error_handle) and return */ |
119 |
|
|
/* might want to have an enum level WARN_INFO, WARN_ERROR, WARN_CRIT, WARN_DEBUG */ |
120 |
|
|
void progwarn(char *msgfmt,...) |
121 |
|
|
{ |
122 |
|
|
va_list args; |
123 |
|
|
|
124 |
|
|
va_start (args,msgfmt); |
125 |
|
|
fprintf (error_handle, "\nWarning: "); |
126 |
|
|
vfprintf (error_handle, msgfmt, args); |
127 |
|
|
fprintf (error_handle, "\n"); |
128 |
|
|
va_end (args); |
129 |
|
|
} |
130 |
|
|
|
131 |
|
|
/* only print a warning (also to error_handle) and return */ |
132 |
|
|
/* might want to have an enum level WARN_INFO, WARN_ERROR, WARN_CRIT, WARN_DEBUG */ |
133 |
|
|
/* includes text of errno at end of message */ |
134 |
|
|
void progwarnno(char *msgfmt,...) |
135 |
|
|
{ |
136 |
|
|
va_list args; |
137 |
|
|
|
138 |
|
|
va_start (args,msgfmt); |
139 |
|
|
fprintf (error_handle, "\nWarning: "); |
140 |
|
|
vfprintf (error_handle, msgfmt, args); |
141 |
|
|
fprintf (error_handle, "%s", strerror(errno)); |
142 |
|
|
fprintf (error_handle, "\n"); |
143 |
|
|
va_end (args); |
144 |
|
|
} |
145 |
|
|
|
146 |
|
|
typedef struct |
147 |
|
|
{ |
148 |
|
|
int critical; /* If true the calling code needs to call SwishClose */ |
149 |
|
|
int error_num; |
150 |
|
|
char *message_string; |
151 |
|
|
} error_msg_map; |
152 |
|
|
|
153 |
|
|
/* See errors.h to the correspondant numerical value */ |
154 |
|
|
static error_msg_map swishErrors[]={ |
155 |
|
|
{ 0, RC_OK, "" }, |
156 |
|
|
{ 0, NO_WORDS_IN_SEARCH, "No search words specified" }, |
157 |
|
|
{ 0, WORDS_TOO_COMMON, "All search words too common to be useful" }, |
158 |
|
|
{ 0, UNKNOWN_PROPERTY_NAME_IN_SEARCH_DISPLAY, "Unknown property name in display properties" }, |
159 |
|
|
{ 0, UNKNOWN_PROPERTY_NAME_IN_SEARCH_SORT, "Unknown property name to sort by" }, |
160 |
|
|
{ 0, INVALID_PROPERTY_TYPE, "Invalid property type" }, |
161 |
|
|
{ 0, UNKNOWN_METANAME, "Unknown metaname" }, |
162 |
|
|
{ 0, UNIQUE_WILDCARD_NOT_ALLOWED_IN_WORD, "Single wildcard not allowed as word" }, |
163 |
|
|
{ 0, WORD_NOT_FOUND, "Word not found" }, |
164 |
|
|
{ 0, SEARCH_WORD_TOO_BIG, "Search word exceeded maxwordlimit setting" }, |
165 |
|
|
{ 0, QUERY_SYNTAX_ERROR, "Syntax error in query (missing end quote or unbalanced parenthesis?)" }, |
166 |
|
|
{ 0, PROP_LIMIT_ERROR, "Failed to setup limit by property"}, |
167 |
|
|
{ 0, SWISH_LISTRESULTS_EOF, "No more results" }, |
168 |
|
|
{ 1, INDEX_FILE_NOT_FOUND, "Could not open index file" }, |
169 |
|
|
{ 1, UNKNOWN_INDEX_FILE_FORMAT, "Unknown index file format" }, |
170 |
|
|
{ 1, INDEX_FILE_IS_EMPTY, "Index file(s) is empty" }, |
171 |
|
|
{ 1, INDEX_FILE_ERROR, "Index file error" }, |
172 |
|
|
{ 1, INVALID_SWISH_HANDLE, "Invalid swish handle" }, |
173 |
|
|
}; |
174 |
|
|
|
175 |
|
|
|
176 |
|
|
/***************************************************************** |
177 |
|
|
* SwishError |
178 |
|
|
* |
179 |
|
|
* Pass: |
180 |
|
|
* SWISH *sw |
181 |
|
|
* |
182 |
|
|
* Returns: |
183 |
|
|
* value of the last error number, or zero |
184 |
|
|
* |
185 |
|
|
******************************************************************/ |
186 |
|
|
|
187 |
|
|
int SwishError(SWISH * sw) |
188 |
|
|
{ |
189 |
|
|
if (!sw) |
190 |
|
|
return INVALID_SWISH_HANDLE; |
191 |
|
|
return (sw->lasterror); |
192 |
|
|
} |
193 |
|
|
|
194 |
|
|
|
195 |
|
|
|
196 |
|
|
/***************************************************************** |
197 |
|
|
* SwishErrorString |
198 |
|
|
* |
199 |
|
|
* Pass: |
200 |
|
|
* SWISH *sw |
201 |
|
|
* |
202 |
|
|
* Returns: |
203 |
|
|
* pointer to string of generic error message related to |
204 |
|
|
* the last error number |
205 |
|
|
* |
206 |
|
|
******************************************************************/ |
207 |
|
|
|
208 |
|
|
|
209 |
|
|
char *SwishErrorString(SWISH *sw) |
210 |
|
|
{ |
211 |
|
|
return getErrorString(sw ? sw->lasterror : INVALID_SWISH_HANDLE); |
212 |
|
|
} |
213 |
|
|
|
214 |
|
|
|
215 |
|
|
|
216 |
|
|
/***************************************************************** |
217 |
|
|
* SwishLastErrorMsg |
218 |
|
|
* |
219 |
|
|
* Pass: |
220 |
|
|
* SWISH *sw |
221 |
|
|
* |
222 |
|
|
* Returns: |
223 |
|
|
* pointer to the string comment of the last error message, if any |
224 |
|
|
* |
225 |
|
|
******************************************************************/ |
226 |
|
|
|
227 |
|
|
|
228 |
|
|
char *SwishLastErrorMsg(SWISH *sw ) |
229 |
|
|
{ |
230 |
|
|
return sw->lasterrorstr; |
231 |
|
|
} |
232 |
|
|
|
233 |
|
|
|
234 |
|
|
|
235 |
|
|
|
236 |
|
|
|
237 |
|
|
/***************************************************************** |
238 |
|
|
* getErrorString |
239 |
|
|
* |
240 |
|
|
* Pass: |
241 |
|
|
* error number |
242 |
|
|
* |
243 |
|
|
* Returns: |
244 |
|
|
* value of the last error number, or zero |
245 |
|
|
* |
246 |
|
|
******************************************************************/ |
247 |
|
|
|
248 |
|
|
|
249 |
|
|
char *getErrorString(int number) |
250 |
|
|
{ |
251 |
|
|
int i; |
252 |
|
|
static char message[50]; |
253 |
|
|
|
254 |
|
|
for (i = 0; i < sizeof(swishErrors) / sizeof(swishErrors[0]); i++) |
255 |
|
|
if ( number == swishErrors[i].error_num ) |
256 |
|
|
return swishErrors[i].message_string; |
257 |
|
|
|
258 |
|
|
sprintf( message, "Invalid error number '%d'", number ); |
259 |
|
|
return( message ); |
260 |
|
|
} |
261 |
|
|
|
262 |
|
|
/***************************************************************** |
263 |
|
|
* SwishCriticalError |
264 |
|
|
* |
265 |
|
|
* This returns true if the last error was critical and means that |
266 |
|
|
* the swish object should be destroyed |
267 |
|
|
* |
268 |
|
|
* Pass: |
269 |
|
|
* *sw |
270 |
|
|
* |
271 |
|
|
* Returns: |
272 |
|
|
* true if the current sw->lasterror is a critical error |
273 |
|
|
* or if the number is invalid or the sw is null |
274 |
|
|
* |
275 |
|
|
******************************************************************/ |
276 |
|
|
|
277 |
|
|
|
278 |
|
|
int SwishCriticalError(SWISH *sw) |
279 |
|
|
{ |
280 |
|
|
int i; |
281 |
|
|
|
282 |
|
|
if ( !sw ) |
283 |
|
|
return 1; |
284 |
|
|
|
285 |
|
|
for (i = 0; i < sizeof(swishErrors) / sizeof(swishErrors[0]); i++) |
286 |
|
|
if ( sw->lasterror == swishErrors[i].error_num ) |
287 |
|
|
return swishErrors[i].critical; |
288 |
|
|
|
289 |
|
|
return 1; |
290 |
|
|
} |
291 |
|
|
|
292 |
|
|
|
293 |
|
|
|
294 |
|
|
|
295 |
|
|
/***************************************************************** |
296 |
|
|
* SwishAbortLastError |
297 |
|
|
* |
298 |
|
|
* Aborts with the error message type, and the optional comment message |
299 |
|
|
* |
300 |
|
|
* Pass: |
301 |
|
|
* SWISH *sw |
302 |
|
|
* |
303 |
|
|
* Returns: |
304 |
|
|
* nope |
305 |
|
|
* |
306 |
|
|
******************************************************************/ |
307 |
|
|
|
308 |
|
|
void SwishAbortLastError(SWISH *sw) |
309 |
|
|
{ |
310 |
|
|
if ( sw->lasterror < 0 ) |
311 |
|
|
{ |
312 |
|
|
if ( *(SwishLastErrorMsg( sw )) ) |
313 |
|
|
progerr( "%s: %s", SwishErrorString( sw ), SwishLastErrorMsg( sw ) ); |
314 |
|
|
else |
315 |
|
|
progerr( "%s", SwishErrorString( sw ) ); |
316 |
|
|
} |
317 |
|
|
|
318 |
|
|
progerr("Swish aborted with non-negative lasterror"); |
319 |
|
|
} |
320 |
|
|
|
321 |
|
|
typedef struct |
322 |
|
|
{ |
323 |
|
|
FuzzyIndexType fuzzy_mode; |
324 |
|
|
char *name; |
325 |
|
|
} |
326 |
|
|
FUZZY_OPTS; |
327 |
|
|
|
328 |
|
|
static FUZZY_OPTS fuzzy_opts[] = { |
329 |
|
|
|
330 |
|
|
{ FUZZY_NONE, "None" }, |
331 |
|
|
{ FUZZY_STEMMING, "Stemming" }, |
332 |
|
|
{ FUZZY_STEMMING, "Stem" }, |
333 |
|
|
{ FUZZY_SOUNDEX, "Soundex" }, |
334 |
|
|
{ FUZZY_METAPHONE, "Metaphone" }, |
335 |
|
|
{ FUZZY_DOUBLE_METAPHONE, "DoubleMetaphone" } |
336 |
|
|
}; |
337 |
|
|
|
338 |
|
|
FuzzyIndexType set_fuzzy_mode( char *param ) |
339 |
|
|
{ |
340 |
|
|
int i; |
341 |
|
|
|
342 |
|
|
for (i = 0; i < sizeof(fuzzy_opts) / sizeof(fuzzy_opts[0]); i++) |
343 |
|
|
if ( 0 == strcasecmp(fuzzy_opts[i].name, param ) ) |
344 |
|
|
return fuzzy_opts[i].fuzzy_mode; |
345 |
|
|
|
346 |
|
|
|
347 |
|
|
progerr("Invalid FuzzyIndexingMode '%s' in configuation file", param); |
348 |
|
|
return FUZZY_NONE; |
349 |
|
|
} |
350 |
|
|
|
351 |
|
|
|
352 |
|
|
|
353 |
|
|
|
354 |
|
|
|
355 |
|
|
char *fuzzy_mode_to_string( FuzzyIndexType mode ) |
356 |
|
|
{ |
357 |
|
|
int i; |
358 |
|
|
for (i = 0; i < sizeof(fuzzy_opts) / sizeof(fuzzy_opts[0]); i++) |
359 |
|
|
if ( mode == fuzzy_opts[i].fuzzy_mode ) |
360 |
|
|
return fuzzy_opts[i].name; |
361 |
|
|
|
362 |
|
|
return "Unknown FuzzyIndexingMode"; |
363 |
|
|
} |
364 |
|
|
|
365 |
|
|
|