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