5 * Revision 1.13 1995/03/28 08:01:25 adam
6 * FIFO existence is used to test for a running kernel.
8 * Revision 1.12 1995/03/27 12:51:05 adam
9 * New log level in use: GW_LOG_ERRNO.
11 * Revision 1.11 1995/03/27 08:24:02 adam
12 * First use of gip interface and gw-db.
13 * First work on eti program.
15 * Revision 1.10 1995/03/01 14:32:25 adam
16 * Better diagnostics. Default is, that only one database selected when
19 * Revision 1.9 1995/02/23 08:32:17 adam
22 * Revision 1.7 1995/02/22 15:22:33 adam
23 * Much more checking of run-time state. Show command never retrieves
24 * more records than indicated by the previous search request. Help
25 * command available. The maximum number of records retrieved can be
28 * Revision 1.6 1995/02/22 08:51:34 adam
29 * Output function can be customized in fml, which is used to print
30 * the reply to reply_fd.
32 * Revision 1.5 1995/02/20 21:16:20 adam
33 * FML support. Bug fixes. Profile for drewdb.
35 * Revision 1.4 1995/02/17 17:06:16 adam
38 * Revision 1.3 1995/02/16 18:35:09 adam
39 * First use of Zdist library. Search requests are supported.
40 * Present requests are not supported yet.
42 * Revision 1.2 1995/02/16 13:21:00 adam
43 * Organization of resource files for targets and conversion
44 * language implemented.
46 * Revision 1.1 1995/02/15 17:45:29 adam
47 * First version of email gateway kernel. Email requests are read
48 * from stdin. The output is transferred to an MTA if 'From' is
49 * found in the header - or stdout if absent. No Z39.50 client is used.
58 #include <sys/types.h>
65 FILE *reply_fd = stdout;
67 struct gw_kernel_info info;
69 static void kernel_events (int userid)
71 char fifo_client_name[1024];
72 char fifo_server_name[1024];
78 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "kernel event loop");
80 sprintf (fifo_client_name, "fifo.c.%d", userid);
81 sprintf (fifo_server_name, "fifo.s.%d", userid);
83 gip = gips_initialize (fifo_server_name);
84 gips_open (gip, fifo_client_name);
85 gip_fd = gip_infileno (gip);
90 FD_SET (gip_fd, &set_r);
94 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "IPC select");
95 r = select (gip_fd+1, &set_r, NULL, NULL, &tv);
98 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, KERNEL_LOG, "select");
103 gw_log (GW_LOG_STAT, KERNEL_LOG, "Timeout");
106 if (FD_ISSET (gip_fd, &set_r))
108 char command[128], *cp;
110 if (!(lgets (command, 127, gip_fd)))
112 gw_log (GW_LOG_WARN, KERNEL_LOG, "Unexpected close");
115 if ((cp = strchr (command, '\n')))
117 gw_log (GW_LOG_STAT, KERNEL_LOG, "IPC: %s", command);
118 if (!strcmp (command, "mail"))
120 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Incoming mail...\n");
123 else if (!strcmp (command, "stop"))
125 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "stop");
130 gw_log (GW_LOG_WARN, KERNEL_LOG, "Unknown IPC: %s", command);
136 unlink (fifo_client_name);
137 unlink (fifo_server_name);
140 int main (int argc, char **argv)
144 info.kernel_res = NULL;
145 info.default_res = "default.res";
146 info.override_res = NULL;
151 info.override_portno = NULL;
152 info.override_hostname = NULL;
153 info.databases = NULL;
154 info.database = NULL;
161 info.kernel_res = gw_res_init ();
169 fprintf (stderr, "kernel [option..] [resource]\n");
170 fprintf (stderr, "If no resource file is given");
171 fprintf (stderr, " default.res is used\n");
172 fprintf (stderr, "Options:\n");
173 fprintf (stderr, " -d Enable debugging log\n");
174 fprintf (stderr, " -t target Open target immediately\n");
175 fprintf (stderr, " -l lang Set language\n");
176 fprintf (stderr, " -o resource Override with resource\n");
177 fprintf (stderr, " -h host Override host\n");
178 fprintf (stderr, " -p port Override port\n");
179 fprintf (stderr, " -g log Set Log file\n");
180 fprintf (stderr, " -i id Set IPC userid\n");
183 gw_log_level (GW_LOG_ALL);
187 strcpy (info.target, argv[0]+2);
191 strcpy (info.target, *++argv);
195 gw_log (GW_LOG_FATAL, KERNEL_LOG, "missing target name");
201 info.lang = argv[0]+2;
209 gw_log (GW_LOG_FATAL, KERNEL_LOG, "missing language name");
215 info.override_res = argv[0]+2;
219 info.override_res = *++argv;
223 gw_log (GW_LOG_FATAL, KERNEL_LOG, "missing override name");
229 info.override_portno = argv[0]+2;
233 info.override_portno = *++argv;
237 gw_log (GW_LOG_FATAL, KERNEL_LOG, "missing portno");
243 info.override_hostname = argv[0]+2;
247 info.override_hostname = *++argv;
251 gw_log (GW_LOG_FATAL, KERNEL_LOG, "missing hostname");
257 gw_log_file (GW_LOG_ALL, argv[0]+2);
261 gw_log_file (GW_LOG_ALL, *++argv);
265 gw_log (GW_LOG_FATAL, KERNEL_LOG, "missing log filename");
271 userid = atoi (argv[0]+2);
275 userid = atoi (*++argv);
279 gw_log (GW_LOG_FATAL, KERNEL_LOG, "missing user id");
284 gw_log (GW_LOG_FATAL, KERNEL_LOG, "unknown option %s", *argv);
289 info.default_res = *argv;
293 kernel_events (userid);
299 struct gw_user_set *user_set_add (const char *name, int hits)
301 struct gw_user_set *s;
303 s = malloc (sizeof (*s));
306 s->name = gw_strdup (name);
313 void user_set_init (void)
315 struct gw_user_set *s, *s1;
317 for (s = info.sets; s; s = s1)
326 struct gw_user_set *user_set_search (const char *name)
328 struct gw_user_set *s;
332 for (s = info.sets; s; s = s->prev)
333 if (!strcmp (s->name, name))
339 static void fml_inf_write (int ch)
343 static FILE *fml_inf;
345 static int fml_inf_read (void)
347 return getc (fml_inf);
351 void read_kernel_res (void)
353 char path_prefix[128];
357 char resource_name[256];
362 ccl_qual_rm (&info.bibset);
363 info.bibset = ccl_qual_mk ();
366 gw_res_close (info.kernel_res);
367 info.kernel_res = gw_res_init ();
369 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "reading kernel resource, default %s",
372 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "reading kernel resource, target %s",
375 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "reading kernel resource, lang %s",
378 if (gw_res_merge (info.kernel_res, info.default_res))
380 gw_log (GW_LOG_WARN, KERNEL_LOG, "Couldn't read resource file %s",
384 strcpy (path_prefix, gw_res_get (info.kernel_res, "gw.path", "."));
388 sprintf (resource_name, "gw.target.%s", info.target);
389 v = gw_res_get (info.kernel_res, resource_name, NULL);
392 sprintf (fname, "%s/%s", path_prefix, v);
393 gw_res_merge (info.kernel_res, fname);
398 sprintf (resource_name, "gw.lang.%s", info.lang);
399 v = gw_res_get (info.kernel_res, resource_name, NULL);
402 sprintf (fname, "%s/%s", path_prefix, v);
403 gw_res_merge (info.kernel_res, fname);
406 if (info.override_res)
408 sprintf (fname, "%s/%s", path_prefix, info.override_res);
409 gw_res_merge (info.kernel_res, fname);
411 v = gw_res_get (info.kernel_res, "gw.bibset", NULL);
416 sprintf (fname, "%s/%s", path_prefix, v);
417 bib_inf = fopen (fname, "r");
419 gw_log (GW_LOG_WARN, KERNEL_LOG, "cannot open %s", fname);
422 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "reading bib file %s", fname);
423 ccl_qual_file (info.bibset, bib_inf);
427 sprintf (resource_name, "gw.target.%s", info.target);
428 if (*info.target && ! gw_res_get (info.kernel_res, resource_name, NULL))
430 /* target is there, and there is no sub-resource for it... */
433 if ((split = strchr (info.target, ':')))
435 strncpy (info.hostname, info.target, sizeof(info.hostname)-1);
437 info.port = atoi (split);
439 info.port = atoi (gw_res_get
440 (info.kernel_res, "gw.portno", "210"));
444 strncpy (info.hostname, gw_res_get (info.kernel_res,
445 "gw.hostname", "localhost"),
446 sizeof(info.hostname)-1);
447 info.port = atoi (gw_res_get (info.kernel_res,
448 "gw.portno", "210"));
451 free (info.databases);
453 free (info.database);
454 v = gw_res_get (info.kernel_res, "gw.databases", "");
455 info.databases = gw_strdup (v);
456 for (cp = info.databases; (cp = strchr (cp, ' ')); cp++)
458 v = gw_res_get (info.kernel_res, "gw.database", "");
459 if (*v == '\0' && *info.databases)
462 cp = strchr (info.databases, ',');
464 len = cp ? (cp-info.databases) : strlen (info.databases);
465 info.database = malloc (len+1);
466 assert (info.database);
467 memcpy (info.database, info.databases, len);
468 info.database[len] = '\0';
472 info.database = gw_strdup (v);
473 for (cp = info.database; (cp = strchr (cp, ' ')); cp++)
476 if (info.override_portno)
477 info.port = atoi (info.override_portno);
478 if (info.override_hostname)
479 strncpy (info.hostname, info.override_hostname,
480 sizeof(info.hostname)-1);
484 v = gw_res_get (info.kernel_res, "gw.fml", "default.fml");
485 sprintf (fname, "%s/%s", path_prefix, v);
486 fml_inf = fopen (fname, "r");
488 gw_log (GW_LOG_WARN, KERNEL_LOG,
489 "cannot open fml script %s", fname);
492 info.fml = fml_open ();
493 info.fml->read_func = fml_inf_read;
494 info.fml->write_func = fml_inf_write;
495 fml_preprocess (info.fml);