1 /*
2  *
3  * namazu-cgi.c - search client of Namazu
4  *
5  * $Id: namazu-cgi.c,v 1.16.8.15 2008-07-19 17:56:42 opengl2772 Exp $
6  *
7  * Copyright (C) 1997-1999 Satoru Takabayashi All rights reserved.
8  * Copyright (C) 2000-2008 Namazu Project All rights reserved.
9  * This is free software with ABSOLUTELY NO WARRANTY.
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
24  * 02111-1307, USA
25  *
26  *
27  */
28 
29 #ifdef HAVE_CONFIG_H
30 #  include "config.h"
31 #endif
32 
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <sys/types.h>
36 #include <fcntl.h>
37 #include <ctype.h>
38 #include <sys/stat.h>
39 #include <signal.h>
40 #include <stdarg.h>
41 
42 #if defined (_WIN32) && !defined (__CYGWIN__)
43 /*
44  * It's not Unix, really.  See?  Capital letters.
45  */
46 #include <windows.h>
47 #define	SIGALRM	14	/* alarm clock */
48 #endif
49 
50 #ifdef HAVE_UNISTD_H
51 #include <unistd.h>
52 #else
53 # ifdef _WIN32
54 # include <io.h>
55 # endif
56 #endif
57 
58 #ifdef HAVE_STRING_H
59 #  include <string.h>
60 #else
61 #  include <strings.h>
62 #endif
63 
64 #include "namazu.h"
65 #include "libnamazu.h"
66 #include "util.h"
67 #include "codeconv.h"
68 #include "usage.h"
69 #include "rcfile.h"
70 #include "output.h"
71 #include "search.h"
72 #include "cgi.h"
73 #include "hlist.h"
74 #include "idxname.h"
75 #include "i18n.h"
76 #include "message.h"
77 #include "system.h"
78 #include "result.h"
79 #include "charset.h"
80 
81 /*
82  * Extern variables.
83  */
84 
85 NMZ_HANDLE handle_charset = (NMZ_HANDLE)0;
86 
87 /*
88  *
89  * Private functions
90  *
91  */
92 
93 static void suicide ( int signum );
94 static void combine_pathname( char *dest, const char *command, const char *name );
95 
96 extern int suicide_time;
97 
98 static void
suicide(int signum)99 suicide (int signum)
100 {
101     die("processing time exceeds a limit: %d", suicide_time);
102 }
103 
104 /*
105  * Make the pathname `dest' by conjuncting `command' and `name'.
106  */
107 static void
combine_pathname(char * dest,const char * command,const char * name)108 combine_pathname(char *dest, const char *command, const char *name)
109 {
110     int i;
111 
112     int win32 = 0;
113 #if  defined(_WIN32) || defined(__EMX__)
114     win32 = 1;
115 #endif
116 
117     strcpy(dest, command);
118     for (i = (int)strlen(dest) - 1; i >= 0; i--) {
119 	if (dest[i] == '/' || (win32 && dest[i] == '\\')) {
120 	    break;
121 	}
122     }
123     i++;
124     strcpy(dest + i, name);
125     return;
126 }
127 
128 /*
129  *
130  * Public functions
131  *
132  */
133 
134 int
main(int argc,char ** argv)135 main(int argc, char **argv)
136 {
137     char query[BUFSIZE] = "", subquery[BUFSIZE] = "";
138     char *localedir = getenv("NAMAZULOCALEDIR");
139 
140     /*
141      *
142      * create resource.
143      *
144      */
145     handle_charset = create_charset_list();
146 
147     /*
148      * To support a binary package for Windows, we should
149      * allow to change LOCALEDIR with the environment
150      * variable `NAMAZULOCALEDIR' after installation is
151      * done.
152      */
153     if (localedir != NULL) {
154 	bindtextdomain(PACKAGE, localedir);
155     } else {
156 	bindtextdomain(PACKAGE, LOCALEDIR);
157     }
158     textdomain(PACKAGE);
159 
160     nmz_set_lang("");
161 
162     /* Both environment variables are required. */
163     if (!((getenv("QUERY_STRING") || getenv("SERVER_SOFTWARE"))
164      && getenv("SCRIPT_NAME"))) {
165 	show_version();
166 	puts("");
167 	die("environment variable QUERY_STRING and SCRIPT_NAME are required");
168     }
169 
170 #if !defined (_WIN32) || defined (__CYGWIN__)
171     /*
172      * Set a suicide timer for safety.
173      * namazu.cgi will suicide automatically when SUICIDE_TIME reached.
174      */
175     signal(SIGALRM, suicide);
176     alarm(suicide_time);
177 #endif
178 
179     /*
180      * Setting up CGI mode.
181      */
182     set_cgimode(1);
183     set_refprint(1);
184     set_htmlmode(1);
185     set_pageindex(1);	 /* Print page index */
186     set_formprint(1);	 /* Print "<form> ... </form>"  */
187     set_uridecode(0);        /* Do not decode URI in results. */
188 
189     {
190 	/*
191 	 * Load .namazurc located in the directory
192 	 * where namazu.cgi command is if it exists.
193 	 */
194 	char fname[BUFSIZE];
195 	combine_pathname(fname, argv[0], ".namazurc");
196 	if (nmz_is_file_exists(fname)) {
197 	    set_namazurc(fname);
198 	}
199     }
200 
201     if (load_rcfiles() != SUCCESS) {
202 	die(nmz_get_dyingmsg());
203     }
204 
205 #if !defined (_WIN32) || defined (__CYGWIN__)
206     alarm(suicide_time);
207 #endif
208     nmz_set_output_warn_to_file(1);
209 
210     /*
211      * NOTE: This processing must be place after load_rcfiles().
212      *       Because the default index can be set in namazurc.
213      */
214     init_cgi(query, subquery);
215 
216     if (namazu_core(query, subquery) == ERR_FATAL) {
217         die(nmz_get_dyingmsg());
218     }
219 
220     /*
221      *
222      * free resource.
223      *
224      */
225     nmz_free_handle(handle_charset);
226 
227     return EXIT_SUCCESS;
228 }
229