1 /*
2  *
3  * libnamazu.c - Namazu library api
4  *
5  * $Id: libnamazu.c,v 1.35.8.11 2008-03-06 15:38:09 opengl2772 Exp $
6  *
7  * Copyright (C) 1997-1999 Satoru Takabayashi All rights reserved.
8  * Copyright (C) 2000-2008 Namazu Project All rights reserved.
9  * Copyright (C) 1999 NOKUBI Takatsugu All rights reserved.
10  * This is free software with ABSOLUTELY NO WARRANTY.
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
25  * 02111-1307, USA
26  *
27  *
28  */
29 
30 #ifdef HAVE_CONFIG_H
31 #  include "config.h"
32 #endif
33 #ifdef HAVE_SUPPORT_H
34 #  include "support.h"
35 #endif
36 
37 #include <stdio.h>
38 #ifdef HAVE_STDLIB_H
39 #  include <stdlib.h>
40 #endif
41 
42 #include <sys/types.h>
43 #include <fcntl.h>
44 #include <ctype.h>
45 #include <sys/stat.h>
46 #include <signal.h>
47 #include <stdarg.h>
48 
49 #ifdef HAVE_UNISTD_H
50 #include <unistd.h>
51 #else
52 # ifdef _WIN32
53 # include <io.h>
54 # endif
55 #endif
56 
57 #ifdef HAVE_STRING_H
58 #  include <string.h>
59 #else
60 #  include <strings.h>
61 #endif
62 
63 #include "libnamazu.h"
64 #include "util.h"
65 #include "codeconv.h"
66 #include "search.h"
67 #include "hlist.h"
68 #include "field.h"
69 #include "i18n.h"
70 #include "regex.h"
71 #include "var.h"
72 #include "alias.h"
73 #include "replace.h"
74 #include "idxname.h"
75 
76 static enum nmz_sortmethod  sortmethod  = SORT_BY_SCORE;
77 static enum nmz_sortorder   sortorder   = DESCENDING;
78 static int  maxhit      = 10000;  /* Ignore if pages matched more than this. */
79 static int  maxmatch    = 1000;   /* Ignore if words matched more than this. */
80 static int  debugmode   = 0;
81 static int  loggingmode = 1;   /* do logging with NMZ.slog */
82 static int  regex_searchmode = 1; /* enable regex search */
83 static char dyingmsg[BUFSIZE] = "";
84 static int  output_warn_to_file = 0; /* output warning to file or stderr */
85 
86 
87 /*
88  *
89  * Public functions
90  *
91  */
92 
93 
94 /*
95  * Free all internal data.
96  */
97 void
nmz_free_internal(void)98 nmz_free_internal(void)
99 {
100     nmz_free_idxnames();
101     nmz_free_aliases();
102     nmz_free_replaces();
103     nmz_free_field_cache();
104 }
105 
106 void
nmz_set_sortmethod(enum nmz_sortmethod method)107 nmz_set_sortmethod(enum nmz_sortmethod method)
108 {
109     sortmethod = method;
110 }
111 
112 enum nmz_sortmethod
nmz_get_sortmethod(void)113 nmz_get_sortmethod(void)
114 {
115     return sortmethod;
116 }
117 
118 void
nmz_set_sortorder(enum nmz_sortorder order)119 nmz_set_sortorder(enum nmz_sortorder order)
120 {
121     sortorder = order;
122 }
123 
124 enum nmz_sortorder
nmz_get_sortorder(void)125 nmz_get_sortorder(void)
126 {
127     return sortorder;
128 }
129 
130 void
nmz_set_maxhit(int max)131 nmz_set_maxhit(int max)
132 {
133     maxhit = max;
134 }
135 
136 int
nmz_get_maxhit(void)137 nmz_get_maxhit(void)
138 {
139     return maxhit;
140 }
141 
142 void
nmz_set_maxmatch(int max)143 nmz_set_maxmatch(int max)
144 {
145     maxmatch = max;
146 }
147 
148 int
nmz_get_maxmatch(void)149 nmz_get_maxmatch(void)
150 {
151     return maxmatch;
152 }
153 
154 void
nmz_set_debugmode(int mode)155 nmz_set_debugmode(int mode)
156 {
157     debugmode = mode;
158 }
159 
160 int
nmz_is_debugmode(void)161 nmz_is_debugmode(void)
162 {
163     return debugmode;
164 }
165 
166 void
nmz_set_loggingmode(int mode)167 nmz_set_loggingmode(int mode)
168 {
169     loggingmode = mode;
170 }
171 
172 int
nmz_is_loggingmode(void)173 nmz_is_loggingmode(void)
174 {
175     return loggingmode;
176 }
177 
178 void
nmz_set_output_warn_to_file(int mode)179 nmz_set_output_warn_to_file(int mode)
180 {
181     output_warn_to_file = mode;
182 }
183 
184 int
nmz_is_output_warn_to_file(void)185 nmz_is_output_warn_to_file(void)
186 {
187     return output_warn_to_file;
188 }
189 
190 void
nmz_set_regex_searchmode(int mode)191 nmz_set_regex_searchmode(int mode)
192 {
193     regex_searchmode = mode;
194 }
195 
196 int
nmz_is_regex_searchmode(void)197 nmz_is_regex_searchmode(void)
198 {
199     return regex_searchmode;
200 }
201 
202 /*
203  * This function is used for formating a string with printf
204  * notation and store the string in the static variable
205  * `msg'.  and return its pointer. So, thhe string can only
206  * be used until the next call to the function.
207  *
208  * NOTE: Mainly used with nmz_set_dyingmsg() macro.
209  */
210 char *
nmz_msg(const char * fmt,...)211 nmz_msg(const char *fmt, ...)
212 {
213     static char msg[BUFSIZE] = "";
214     va_list args;
215 
216     va_start(args, fmt);
217     vsnprintf(msg, BUFSIZE - 1, fmt, args);
218     va_end(args);
219 
220     return msg;
221 }
222 
223 /*
224  * This function is not used directly but used only through
225  * nmz_set_dyingmsg() macro. That's for getting __FILE__ and
226  * __LINE__ information and including them in the
227  * dyingmsg in debug mode. It makes debug easy.
228  */
229 char *
nmz_set_dyingmsg_sub(const char * fmt,...)230 nmz_set_dyingmsg_sub(const char *fmt, ...)
231 {
232     va_list args;
233 
234     va_start(args, fmt);
235     vsnprintf(dyingmsg, BUFSIZE - 1, fmt, args);
236     va_end(args);
237 
238     return dyingmsg;
239 }
240 
241 
242 char *
nmz_get_dyingmsg(void)243 nmz_get_dyingmsg(void)
244 {
245     return dyingmsg;
246 }
247