5 * Revision 1.14 1995/03/28 11:42:34 adam
6 * First use of string-queue utility.
8 * Revision 1.13 1995/03/28 08:01:25 adam
9 * FIFO existence is used to test for a running kernel.
11 * Revision 1.12 1995/03/27 12:51:05 adam
12 * New log level in use: GW_LOG_ERRNO.
14 * Revision 1.11 1995/03/27 08:24:02 adam
15 * First use of gip interface and gw-db.
16 * First work on eti program.
18 * Revision 1.10 1995/03/01 14:32:25 adam
19 * Better diagnostics. Default is, that only one database selected when
22 * Revision 1.9 1995/02/23 08:32:17 adam
25 * Revision 1.7 1995/02/22 15:22:33 adam
26 * Much more checking of run-time state. Show command never retrieves
27 * more records than indicated by the previous search request. Help
28 * command available. The maximum number of records retrieved can be
31 * Revision 1.6 1995/02/22 08:51:34 adam
32 * Output function can be customized in fml, which is used to print
33 * the reply to reply_fd.
35 * Revision 1.5 1995/02/20 21:16:20 adam
36 * FML support. Bug fixes. Profile for drewdb.
38 * Revision 1.4 1995/02/17 17:06:16 adam
41 * Revision 1.3 1995/02/16 18:35:09 adam
42 * First use of Zdist library. Search requests are supported.
43 * Present requests are not supported yet.
45 * Revision 1.2 1995/02/16 13:21:00 adam
46 * Organization of resource files for targets and conversion
47 * language implemented.
49 * Revision 1.1 1995/02/15 17:45:29 adam
50 * First version of email gateway kernel. Email requests are read
51 * from stdin. The output is transferred to an MTA if 'From' is
52 * found in the header - or stdout if absent. No Z39.50 client is used.
61 #include <sys/types.h>
69 FILE *reply_fd = stdout;
71 struct gw_kernel_info info;
73 static void kernel_events (struct str_queue *queue, int userid)
75 char fifo_client_name[1024];
76 char fifo_server_name[1024];
83 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "kernel event loop");
85 sprintf (fifo_client_name, "fifo.c.%d", userid);
86 sprintf (fifo_server_name, "fifo.s.%d", userid);
88 gip = gips_initialize (fifo_server_name);
89 gips_open (gip, fifo_client_name);
90 gip_fd = gip_infileno (gip);
95 FD_SET (gip_fd, &set_r);
99 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "IPC select");
100 r = select (gip_fd+1, &set_r, NULL, NULL, &tv);
103 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, KERNEL_LOG, "select");
108 gw_log (GW_LOG_STAT, KERNEL_LOG, "Timeout");
111 if (FD_ISSET (gip_fd, &set_r))
113 char command[128], *cp;
115 if (!(lgets (command, 127, gip_fd)))
117 gw_log (GW_LOG_WARN, KERNEL_LOG, "Unexpected close");
120 if ((cp = strchr (command, '\n')))
122 gw_log (GW_LOG_STAT, KERNEL_LOG, "IPC: %s", command);
123 if (!strcmp (command, "mail"))
125 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Incoming mail...\n");
126 while (lgets (line_buf, sizeof(line_buf)-1, gip_fd))
127 str_queue_enq (queue, line_buf);
129 while (str_queue_deq (queue, 0, 0))
132 else if (!strcmp (command, "stop"))
134 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "stop");
139 gw_log (GW_LOG_WARN, KERNEL_LOG, "Unknown IPC: %s", command);
145 unlink (fifo_client_name);
146 unlink (fifo_server_name);
149 int main (int argc, char **argv)
152 struct str_queue *queue;
154 info.kernel_res = NULL;
155 info.default_res = "default.res";
156 info.override_res = NULL;
161 info.override_portno = NULL;
162 info.override_hostname = NULL;
163 info.databases = NULL;
164 info.database = NULL;
171 info.kernel_res = gw_res_init ();
179 fprintf (stderr, "kernel [option..] [resource]\n");
180 fprintf (stderr, "If no resource file is given");
181 fprintf (stderr, " default.res is used\n");
182 fprintf (stderr, "Options:\n");
183 fprintf (stderr, " -d Enable debugging log\n");
184 fprintf (stderr, " -t target Open target immediately\n");
185 fprintf (stderr, " -l lang Set language\n");
186 fprintf (stderr, " -o resource Override with resource\n");
187 fprintf (stderr, " -h host Override host\n");
188 fprintf (stderr, " -p port Override port\n");
189 fprintf (stderr, " -g log Set Log file\n");
190 fprintf (stderr, " -i id Set IPC userid\n");
193 gw_log_level (GW_LOG_ALL);
197 strcpy (info.target, argv[0]+2);
201 strcpy (info.target, *++argv);
205 gw_log (GW_LOG_FATAL, KERNEL_LOG, "missing target name");
211 info.lang = argv[0]+2;
219 gw_log (GW_LOG_FATAL, KERNEL_LOG, "missing language name");
225 info.override_res = argv[0]+2;
229 info.override_res = *++argv;
233 gw_log (GW_LOG_FATAL, KERNEL_LOG, "missing override name");
239 info.override_portno = argv[0]+2;
243 info.override_portno = *++argv;
247 gw_log (GW_LOG_FATAL, KERNEL_LOG, "missing portno");
253 info.override_hostname = argv[0]+2;
257 info.override_hostname = *++argv;
261 gw_log (GW_LOG_FATAL, KERNEL_LOG, "missing hostname");
267 gw_log_file (GW_LOG_ALL, argv[0]+2);
271 gw_log_file (GW_LOG_ALL, *++argv);
275 gw_log (GW_LOG_FATAL, KERNEL_LOG, "missing log filename");
281 userid = atoi (argv[0]+2);
285 userid = atoi (*++argv);
289 gw_log (GW_LOG_FATAL, KERNEL_LOG, "missing user id");
294 gw_log (GW_LOG_FATAL, KERNEL_LOG, "unknown option %s", *argv);
299 info.default_res = *argv;
302 if (!(queue = str_queue_mk ()))
304 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, KERNEL_LOG, "str_queue_mk");
308 kernel_events (queue, userid);
312 while (lgets (line_buf, sizeof(line_buf)-1, 0))
313 str_queue_enq (queue, line_buf);
319 struct gw_user_set *user_set_add (const char *name, int hits)
321 struct gw_user_set *s;
323 s = malloc (sizeof (*s));
326 s->name = gw_strdup (name);
333 void user_set_init (void)
335 struct gw_user_set *s, *s1;
337 for (s = info.sets; s; s = s1)
346 struct gw_user_set *user_set_search (const char *name)
348 struct gw_user_set *s;
352 for (s = info.sets; s; s = s->prev)
353 if (!strcmp (s->name, name))
359 static void fml_inf_write (int ch)
363 static FILE *fml_inf;
365 static int fml_inf_read (void)
367 return getc (fml_inf);
371 void read_kernel_res (void)
373 char path_prefix[128];
377 char resource_name[256];
382 ccl_qual_rm (&info.bibset);
383 info.bibset = ccl_qual_mk ();
386 gw_res_close (info.kernel_res);
387 info.kernel_res = gw_res_init ();
389 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "reading kernel resource, default %s",
392 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "reading kernel resource, target %s",
395 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "reading kernel resource, lang %s",
398 if (gw_res_merge (info.kernel_res, info.default_res))
400 gw_log (GW_LOG_WARN, KERNEL_LOG, "Couldn't read resource file %s",
404 strcpy (path_prefix, gw_res_get (info.kernel_res, "gw.path", "."));
408 sprintf (resource_name, "gw.target.%s", info.target);
409 v = gw_res_get (info.kernel_res, resource_name, NULL);
412 sprintf (fname, "%s/%s", path_prefix, v);
413 gw_res_merge (info.kernel_res, fname);
418 sprintf (resource_name, "gw.lang.%s", info.lang);
419 v = gw_res_get (info.kernel_res, resource_name, NULL);
422 sprintf (fname, "%s/%s", path_prefix, v);
423 gw_res_merge (info.kernel_res, fname);
426 if (info.override_res)
428 sprintf (fname, "%s/%s", path_prefix, info.override_res);
429 gw_res_merge (info.kernel_res, fname);
431 v = gw_res_get (info.kernel_res, "gw.bibset", NULL);
436 sprintf (fname, "%s/%s", path_prefix, v);
437 bib_inf = fopen (fname, "r");
439 gw_log (GW_LOG_WARN, KERNEL_LOG, "cannot open %s", fname);
442 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "reading bib file %s", fname);
443 ccl_qual_file (info.bibset, bib_inf);
447 sprintf (resource_name, "gw.target.%s", info.target);
448 if (*info.target && ! gw_res_get (info.kernel_res, resource_name, NULL))
450 /* target is there, and there is no sub-resource for it... */
453 if ((split = strchr (info.target, ':')))
455 strncpy (info.hostname, info.target, sizeof(info.hostname)-1);
457 info.port = atoi (split);
459 info.port = atoi (gw_res_get
460 (info.kernel_res, "gw.portno", "210"));
464 strncpy (info.hostname, gw_res_get (info.kernel_res,
465 "gw.hostname", "localhost"),
466 sizeof(info.hostname)-1);
467 info.port = atoi (gw_res_get (info.kernel_res,
468 "gw.portno", "210"));
471 free (info.databases);
473 free (info.database);
474 v = gw_res_get (info.kernel_res, "gw.databases", "");
475 info.databases = gw_strdup (v);
476 for (cp = info.databases; (cp = strchr (cp, ' ')); cp++)
478 v = gw_res_get (info.kernel_res, "gw.database", "");
479 if (*v == '\0' && *info.databases)
482 cp = strchr (info.databases, ',');
484 len = cp ? (cp-info.databases) : strlen (info.databases);
485 info.database = malloc (len+1);
486 assert (info.database);
487 memcpy (info.database, info.databases, len);
488 info.database[len] = '\0';
492 info.database = gw_strdup (v);
493 for (cp = info.database; (cp = strchr (cp, ' ')); cp++)
496 if (info.override_portno)
497 info.port = atoi (info.override_portno);
498 if (info.override_hostname)
499 strncpy (info.hostname, info.override_hostname,
500 sizeof(info.hostname)-1);
504 v = gw_res_get (info.kernel_res, "gw.fml", "default.fml");
505 sprintf (fname, "%s/%s", path_prefix, v);
506 fml_inf = fopen (fname, "r");
508 gw_log (GW_LOG_WARN, KERNEL_LOG,
509 "cannot open fml script %s", fname);
512 info.fml = fml_open ();
513 info.fml->read_func = fml_inf_read;
514 info.fml->write_func = fml_inf_write;
515 fml_preprocess (info.fml);