File: namazu-cgi.c

package info (click to toggle)
namazu2 2.0.21-22
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 7,032 kB
  • sloc: ansic: 14,161; perl: 10,789; sh: 9,355; lisp: 1,147; makefile: 914
file content (228 lines) | stat: -rw-r--r-- 4,951 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*
 * 
 * namazu-cgi.c - search client of Namazu
 *
 * $Id: namazu-cgi.c,v 1.16.8.15 2008-07-19 17:56:42 opengl2772 Exp $
 * 
 * Copyright (C) 1997-1999 Satoru Takabayashi All rights reserved.
 * Copyright (C) 2000-2008 Namazu Project All rights reserved.
 * This is free software with ABSOLUTELY NO WARRANTY.
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA
 * 
 * 
 */

#ifdef HAVE_CONFIG_H
#  include "config.h"
#endif

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <fcntl.h>
#include <ctype.h>
#include <sys/stat.h>
#include <signal.h>
#include <stdarg.h>

#if defined (_WIN32) && !defined (__CYGWIN__)
/*
 * It's not Unix, really.  See?  Capital letters. 
 */
#include <windows.h>
#define	SIGALRM	14	/* alarm clock */
#endif

#ifdef HAVE_UNISTD_H
#include <unistd.h>
#else
# ifdef _WIN32
# include <io.h>
# endif
#endif

#ifdef HAVE_STRING_H
#  include <string.h>
#else
#  include <strings.h>
#endif

#include "namazu.h"
#include "libnamazu.h"
#include "util.h"
#include "codeconv.h"
#include "usage.h"
#include "rcfile.h"
#include "output.h"
#include "search.h"
#include "cgi.h"
#include "hlist.h"
#include "idxname.h"
#include "i18n.h"
#include "message.h"
#include "system.h"
#include "result.h"
#include "charset.h"

/*
 * Extern variables.
 */

NMZ_HANDLE handle_charset = (NMZ_HANDLE)0;

/*
 *
 * Private functions
 *
 */

static void suicide ( int signum );
static void combine_pathname( char *dest, const char *command, const char *name );

extern int suicide_time;

static void 
suicide (int signum)
{
    die("processing time exceeds a limit: %d", suicide_time);
}

/*
 * Make the pathname `dest' by conjuncting `command' and `name'.
 */
static void 
combine_pathname(char *dest, const char *command, const char *name)
{
    int i;

    int win32 = 0;
#if  defined(_WIN32) || defined(__EMX__)
    win32 = 1;
#endif

    strcpy(dest, command);
    for (i = (int)strlen(dest) - 1; i >= 0; i--) {
	if (dest[i] == '/' || (win32 && dest[i] == '\\')) {
	    break;
	}
    }
    i++;
    strcpy(dest + i, name);
    return;
}

/*
 *
 * Public functions
 *
 */

int 
main(int argc, char **argv)
{
    char query[BUFSIZE] = "", subquery[BUFSIZE] = "";
    char *localedir = getenv("NAMAZULOCALEDIR");

    /*
     *
     * create resource.
     *
     */
    handle_charset = create_charset_list();

    /*
     * To support a binary package for Windows, we should
     * allow to change LOCALEDIR with the environment
     * variable `NAMAZULOCALEDIR' after installation is
     * done.
     */
    if (localedir != NULL) {
	bindtextdomain(PACKAGE, localedir);
    } else {
	bindtextdomain(PACKAGE, LOCALEDIR);
    }
    textdomain(PACKAGE);

    nmz_set_lang("");

    /* Both environment variables are required. */
    if (!((getenv("QUERY_STRING") || getenv("SERVER_SOFTWARE"))
     && getenv("SCRIPT_NAME"))) {
	show_version();
	puts("");
	die("environment variable QUERY_STRING and SCRIPT_NAME are required");
    }

#if !defined (_WIN32) || defined (__CYGWIN__)
    /* 
     * Set a suicide timer for safety.
     * namazu.cgi will suicide automatically when SUICIDE_TIME reached.
     */
    signal(SIGALRM, suicide);
    alarm(suicide_time);
#endif

    /*
     * Setting up CGI mode.
     */
    set_cgimode(1);
    set_refprint(1);
    set_htmlmode(1);
    set_pageindex(1);	 /* Print page index */
    set_formprint(1);	 /* Print "<form> ... </form>"  */
    set_uridecode(0);        /* Do not decode URI in results. */

    {
	/*
	 * Load .namazurc located in the directory
	 * where namazu.cgi command is if it exists.
	 */
	char fname[BUFSIZE];
	combine_pathname(fname, argv[0], ".namazurc");
	if (nmz_is_file_exists(fname)) {
	    set_namazurc(fname);
	}
    }

    if (load_rcfiles() != SUCCESS) {
	die(nmz_get_dyingmsg());
    }

#if !defined (_WIN32) || defined (__CYGWIN__)
    alarm(suicide_time);
#endif
    nmz_set_output_warn_to_file(1);

    /*
     * NOTE: This processing must be place after load_rcfiles().
     *       Because the default index can be set in namazurc.
     */
    init_cgi(query, subquery);

    if (namazu_core(query, subquery) == ERR_FATAL) {
        die(nmz_get_dyingmsg());
    }

    /*
     *
     * free resource.
     *
     */
    nmz_free_handle(handle_charset);

    return EXIT_SUCCESS;
}