b5f403038190d3b1cccc5df4f4085ab182d65c2c
[egate.git] / kernel / main.c
1 /* Gateway kernel
2  * Europagate, 1995
3  *
4  * $Log: main.c,v $
5  * Revision 1.4  1995/02/17 17:06:16  adam
6  * Minor changes.
7  *
8  * Revision 1.3  1995/02/16  18:35:09  adam
9  * First use of Zdist library. Search requests are supported.
10  * Present requests are not supported yet.
11  *
12  * Revision 1.2  1995/02/16  13:21:00  adam
13  * Organization of resource files for targets and conversion
14  * language implemented.
15  *
16  * Revision 1.1  1995/02/15  17:45:29  adam
17  * First version of email gateway kernel. Email requests are read
18  * from stdin. The output is transferred to an MTA if 'From' is
19  * found in the header - or stdout if absent. No Z39.50 client is used.
20  *
21  */
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <assert.h>
27
28 #include "kernel.h"
29
30 FILE *reply_fd = stdout;
31
32 struct gw_kernel_info info;
33
34 int main (int argc, char **argv)
35 {
36     info.kernel_res = NULL;
37     info.default_res = "default.res";
38     info.override_res = NULL;
39     *info.target = 0;
40     info.lang = NULL;
41     info.bibset = NULL;
42     info.zass = NULL;
43     info.override_portno = NULL;
44     info.override_hostname = NULL;
45     info.databases = NULL;
46
47     gw_log_init (*argv);
48     info.kernel_res = gw_res_init ();
49     while (--argc > 0)
50     {
51         if (**++argv == '-')
52         {
53             switch (argv[0][1])
54             {
55             case 'd':
56                 gw_log_level (GW_LOG_ALL);
57                 break;
58             case 't':
59                 if (argv[0][2])
60                     strcpy (info.target, argv[0]+2);
61                 else if (argc > 0)
62                 {
63                     --argc;
64                     strcpy (info.target, *++argv);
65                 }
66                 else
67                 {
68                     gw_log (GW_LOG_FATAL, "main", "missing target name");
69                     exit (1);
70                 }
71                 break;
72             case 'l':
73                 if (argv[0][2])
74                     info.lang = argv[0]+2;
75                 else if (argc > 0)
76                 {
77                     --argc;
78                     info.lang = *++argv;
79                 }
80                 else
81                 {
82                     gw_log (GW_LOG_FATAL, "main", "missing language name");
83                     exit (1);
84                 }
85                 break;
86             case 'o':
87                 if (argv[0][2])
88                     info.override_res = argv[0]+2;
89                 else if (argc > 0)
90                 {
91                     --argc;
92                     info.override_res = *++argv;
93                 }
94                 else
95                 {
96                     gw_log (GW_LOG_FATAL, "main", "missing language name");
97                     exit (1);
98                 }
99                 break;
100             case 'p':
101                 if (argv[0][2])
102                     info.override_portno = argv[0]+2;
103                 else if (argc > 0)
104                 {
105                     --argc;
106                     info.override_portno = *++argv;
107                 }
108                 else
109                 {
110                     gw_log (GW_LOG_FATAL, "main", "missing portno");
111                     exit (1);
112                 }
113                 break;
114             case 'h':
115                 if (argv[0][2])
116                     info.override_hostname = argv[0]+2;
117                 else if (argc > 0)
118                 {
119                     --argc;
120                     info.override_hostname = *++argv;
121                 }
122                 else
123                 {
124                     gw_log (GW_LOG_FATAL, "main", "missing hostname");
125                     exit (1);
126                 }
127                 break;
128             case 'g':
129                 if (argv[0][2])
130                     gw_log_file (GW_LOG_ALL, argv[0]+2);
131                 else if (argc > 0)
132                 {
133                     --argc;
134                     gw_log_file (GW_LOG_ALL, *++argv);
135                 }
136                 else
137                 {
138                     gw_log (GW_LOG_FATAL, "main", "missing log filename");
139                     exit (1);
140                 }
141                 break;
142             default:
143                 gw_log (GW_LOG_FATAL, "main", "unknown option %s", *argv);
144                 exit (1);
145             }
146         }
147         else
148             info.default_res = *argv;
149     }
150     read_kernel_res ();
151     urp (stdin);
152     return 0;
153 }
154
155 void read_kernel_res (void)
156 {
157     char path_prefix[128];
158     char fname[160];
159     const char *v;
160     char *cp;
161     char resource_name[256];
162
163     if (info.bibset)
164         ccl_qual_rm (&info.bibset);
165     info.bibset = ccl_qual_mk ();
166
167     if (info.kernel_res)
168         gw_res_close (info.kernel_res);
169     info.kernel_res = gw_res_init ();
170
171     gw_log (GW_LOG_DEBUG, "main", "reading kernel resource, default %s",
172             info.default_res);
173     if (*info.target)
174         gw_log (GW_LOG_DEBUG, "main", "reading kernel resource, target %s",
175                 info.target);
176     if (info.lang)
177         gw_log (GW_LOG_DEBUG, "main", "reading kernel resource, lang %s",
178                 info.lang);
179
180     if (gw_res_merge (info.kernel_res, info.default_res))
181     {
182         gw_log (GW_LOG_WARN, "main", "Couldn't read resource file %s",
183                 info.default_res);
184         return;
185     }
186     strcpy (path_prefix, gw_res_get (info.kernel_res, "gw.path", "."));
187     
188     if (*info.target)
189     {
190         sprintf (resource_name, "gw.target.%s", info.target);
191         v = gw_res_get (info.kernel_res, resource_name, NULL);
192         if (v)
193         {
194             sprintf (fname, "%s/%s", path_prefix, v);
195             gw_res_merge (info.kernel_res, fname);
196         }
197     }
198     if (info.lang)
199     {
200         sprintf (resource_name, "gw.lang.%s", info.lang);
201         v = gw_res_get (info.kernel_res, resource_name, NULL);
202         if (v)
203         {
204             sprintf (fname, "%s/%s", path_prefix, v);
205             gw_res_merge (info.kernel_res, fname);
206         }
207     }
208     if (info.override_res)
209     {
210         sprintf (fname, "%s/%s", path_prefix, info.override_res);
211         gw_res_merge (info.kernel_res, fname);        
212     }
213     v = gw_res_get (info.kernel_res, "gw.bibset", NULL);
214     if (v)
215     {
216         FILE *bib_inf;
217
218         sprintf (fname, "%s/%s", path_prefix, v);
219         bib_inf = fopen (fname, "r");
220         if (!bib_inf)
221             gw_log (GW_LOG_WARN, "main", "cannot open %s", fname);
222         else
223         {
224             gw_log (GW_LOG_DEBUG, "main", "reading bib file %s", fname);
225             ccl_qual_file (info.bibset, bib_inf);
226             fclose (bib_inf);
227         }
228     }
229     sprintf (resource_name, "gw.target.%s", info.target);
230     if (*info.target && ! gw_res_get (info.kernel_res, resource_name, NULL))
231     {
232         /* target is there, and there is no sub-resource for it... */
233         char *split;
234
235         if ((split = strchr (info.target, ':')))
236             *split++ = '\0';
237         strncpy (info.hostname, info.target, sizeof(info.hostname)-1);
238         if (split)
239             info.port = atoi (split);
240         else
241             info.port = atoi (gw_res_get
242                               (info.kernel_res, "gw.portno", "210"));
243     }
244     else
245     {
246         strncpy (info.hostname, gw_res_get (info.kernel_res,
247                                             "gw.hostname", "localhost"),
248                  sizeof(info.hostname)-1);
249         info.port = atoi (gw_res_get (info.kernel_res,
250                                       "gw.portno", "210"));
251     }
252     if (info.databases)
253         free (info.databases);
254     v = gw_res_get (info.kernel_res, "gw.databases", "Default");
255     info.databases = malloc (1+strlen(v));
256     assert (info.databases);
257     strcpy (info.databases, v);
258     for (cp = info.databases; (cp = strchr (cp, ' ')); cp++)
259         *cp = ',';
260     if (info.override_portno)
261         info.port = atoi (info.override_portno);
262     if (info.override_hostname)
263         strncpy (info.hostname, info.override_hostname,
264                  sizeof(info.hostname)-1);
265 }