1 /*
2  *
3  * conf.c -
4  *
5  * Copyright (C) 1997-1999 Satoru Takabayashi  All rights reserved.
6  * This is free software with ABSOLUTELY NO WARRANTY.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
21  * 02111-1307, USA
22  *
23  * This file must be encoded in EUC-JP encoding.
24  *
25  */
26 
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include "namazu.h"
31 #include "util.h"
32 
show_configuration()33 void show_configuration()
34 {
35     printf("namazu configurations\n");
36     if (ConfLoaded)
37 	printf("configuration file: %s\n", NAMAZU_CONF);
38 
39     printf("\
40   * DEFAULT_DIR      : %s\n\
41   * BASE_URL         : %s\n\
42   * URL_REPLACE_FROM : %s\n\
43   * URL_REPLACE_TO   : %s\n\
44   * LOGGING          : %s\n\
45   * LANGUAGE         : %s\n\
46   * SCORING          : %s\n\
47 ", DEFAULT_DIR, BASE_URL, URL_REPLACE_FROM[0],
48 	 URL_REPLACE_TO[0], Logging ? "ON" : "OFF",
49            Lang, TfIdf ? "TFIDF" : "SIMPLE");
50     exit(0);
51 }
52 
53 
54 /* change filename in full pathname */
set_pathname(char * to,char * o,char * name)55 void set_pathname(char *to, char *o, char *name)
56 {
57     int i;
58 
59     strcpy(to, o);
60     for (i = strlen(to) - 1; i > 0; i--) {
61 	if (to[i] == '/') {
62 	    i++;
63 	    break;
64 	}
65     }
66     strcpy(to + i, name);
67     return;
68 }
69 
70 /*
71  1. current_executing_binary_dir/.namazurc
72  2. ${HOME}/.namazurc
73  3. lib/namazu.conf
74  */
open_conf_file(char * av0)75 FILE *open_conf_file(char *av0)
76 {
77     FILE *fp;
78     char fname[BUFSIZE], *home;
79 
80     /* be invoked with -f option to specify rcfile */
81     if (NAMAZURC[0]) {
82         if ((fp = fopen(NAMAZURC, "rb"))) {
83             strcpy(NAMAZU_CONF, NAMAZURC);
84             return fp;
85         }
86     }
87 
88     /* check the where program is */
89     set_pathname(fname, av0, ".namazurc");
90     if ((fp = fopen(fname, "rb"))) {
91         strcpy(NAMAZU_CONF, fname);
92         return fp;
93     }
94 
95     /* checke a home directory */
96     if ((home = getenv("HOME"))) {
97         strcpy(fname, home);
98         strcat(fname, "/.namazurc");
99         if ((fp = fopen(fname, "rb"))) {
100             strcpy(NAMAZU_CONF, fname);
101             return fp;
102         }
103     }
104 
105     /* check the defalut */
106     if ((fp = fopen(NAMAZU_CONF, "rb"))) {
107         return fp;
108     }
109     return (FILE *) NULL;
110 }
111 
112 /* loading configuration file of Namazu */
load_namazu_conf(char * av0)113 void load_namazu_conf(char *av0)
114 {
115     FILE *fp;
116     uchar buf[BUFSIZE];
117     int n, i;
118 
119 #ifdef NOCONF
120     return;
121 #endif
122 
123     /* I don't know why but GNU-Win32 require this */
124     BASE_URL[0] = '\0';
125     URL_REPLACE_FROM[url_no][0] = '\0';
126     URL_REPLACE_TO[url_no][0] = '\0';
127 
128     fp = open_conf_file(av0);
129     if (fp == NULL)
130 	return;
131     ConfLoaded = 1;
132     while (fgets(buf, BUFSIZE, fp)) {
133 	chop(buf);
134 	if (!strncmp(buf, "INDEX\t", 6)) {
135 	    for (n = 6; buf[n] == '\t'; n++);
136 	    strcpy(DEFAULT_DIR, buf + n);
137 	} else if (!strncmp(buf, "BASE\t", 5)) {
138 	    for (n = 5; buf[n] == '\t'; n++);
139 	    strcpy(BASE_URL, buf + n);
140 	} else if (!strncmp(buf, "REPLACE\t", 8)) {
141 	    for (n = 8; buf[n] == '\t'; n++);
142 	    for (i = 0; buf[n] != '\t' && buf[n]; n++, i++)
143 		URL_REPLACE_FROM[url_no][i] = buf[n];
144 	    URL_REPLACE_FROM[url_no][i] = '\0';
145 	    for (; buf[n] == '\t'; n++);
146 	    strcpy(URL_REPLACE_TO[url_no], buf + n);
147 	    url_no++;
148 	} else if (!strncmp(buf, "LOGGING\t", 8)) {
149 	    for (n = 8; buf[n] == '\t'; n++);
150 	    if (!strncmp(&buf[n], "OFF", 3))
151 		Logging = 0;
152 	} else if (!strncmp(buf, "SCORING\t", 8)) {
153 	    for (n = 8; buf[n] == '\t'; n++);
154 	    if (!strncmp(&buf[n], "TFIDF", 5))
155 		TfIdf = 1;
156 	    if (!strncmp(&buf[n], "SIMPLE", 6))
157 		TfIdf = 0;
158 	} else if (!strncmp(buf, "LANG\t", 5)) {
159 	    for (n = 5; buf[n] == '\t'; n++);
160 	    strncpy(Lang, &buf[n], 2);
161             initialize_message();
162 	}
163     }
164     fclose(fp);
165 }
166