5 * Revision 1.11 1995/03/27 08:24:02 adam
6 * First use of gip interface and gw-db.
7 * First work on eti program.
9 * Revision 1.10 1995/03/01 14:32:25 adam
10 * Better diagnostics. Default is, that only one database selected when
13 * Revision 1.9 1995/02/23 08:32:17 adam
16 * Revision 1.7 1995/02/22 15:22:33 adam
17 * Much more checking of run-time state. Show command never retrieves
18 * more records than indicated by the previous search request. Help
19 * command available. The maximum number of records retrieved can be
22 * Revision 1.6 1995/02/22 08:51:34 adam
23 * Output function can be customized in fml, which is used to print
24 * the reply to reply_fd.
26 * Revision 1.5 1995/02/20 21:16:20 adam
27 * FML support. Bug fixes. Profile for drewdb.
29 * Revision 1.4 1995/02/17 17:06:16 adam
32 * Revision 1.3 1995/02/16 18:35:09 adam
33 * First use of Zdist library. Search requests are supported.
34 * Present requests are not supported yet.
36 * Revision 1.2 1995/02/16 13:21:00 adam
37 * Organization of resource files for targets and conversion
38 * language implemented.
40 * Revision 1.1 1995/02/15 17:45:29 adam
41 * First version of email gateway kernel. Email requests are read
42 * from stdin. The output is transferred to an MTA if 'From' is
43 * found in the header - or stdout if absent. No Z39.50 client is used.
52 #include <sys/types.h>
59 FILE *reply_fd = stdout;
61 struct gw_kernel_info info;
63 static void mk_active (int userid)
65 char active_name[1024];
69 sprintf (active_name, "kernel.pid.%d", userid);
70 fd = open (active_name, O_WRONLY|O_CREAT, 0666);
73 gw_log (GW_LOG_FATAL, "Cannot create %s", active_name);
76 sprintf (pid_buf, "%ld", (long) getpid());
77 write (fd, pid_buf, strlen(pid_buf));
81 static void rm_active (int userid)
83 char active_name[1024];
85 sprintf (active_name, "kernel.pid.%d", userid);
89 static void kernel_events (int userid)
91 char fifo_client_name[1024];
92 char fifo_server_name[1024];
98 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "kernel event loop");
100 sprintf (fifo_client_name, "fifo.c.%d", userid);
101 sprintf (fifo_server_name, "fifo.s.%d", userid);
103 gip = gips_initialize (fifo_server_name);
104 gips_open (gip, fifo_client_name);
105 gip_fd = gip_infileno (gip);
110 FD_SET (gip_fd, &set_r);
114 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "IPC select");
115 r = select (gip_fd+1, &set_r, NULL, NULL, &tv);
118 gw_log (GW_LOG_FATAL, KERNEL_LOG, "select fail");
123 gw_log (GW_LOG_STAT, KERNEL_LOG, "Timeout");
126 if (FD_ISSET (gip_fd, &set_r))
128 char command[128], *cp;
130 if (!(lgets (command, 127, gip_fd)))
132 if ((cp = strchr (command, '\n')))
134 gw_log (GW_LOG_STAT, KERNEL_LOG, "IPC: %s", command);
135 if (!strcmp (command, "mail"))
137 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Incoming mail...\n");
140 else if (!strcmp (command, "stop"))
142 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "stop");
147 gw_log (GW_LOG_WARN, KERNEL_LOG, "Unknown IPC: %s", command);
155 int main (int argc, char **argv)
159 info.kernel_res = NULL;
160 info.default_res = "default.res";
161 info.override_res = NULL;
166 info.override_portno = NULL;
167 info.override_hostname = NULL;
168 info.databases = NULL;
169 info.database = NULL;
176 info.kernel_res = gw_res_init ();
184 gw_log_level (GW_LOG_ALL);
188 strcpy (info.target, argv[0]+2);
192 strcpy (info.target, *++argv);
196 gw_log (GW_LOG_FATAL, KERNEL_LOG, "missing target name");
202 info.lang = argv[0]+2;
210 gw_log (GW_LOG_FATAL, KERNEL_LOG, "missing language name");
216 info.override_res = argv[0]+2;
220 info.override_res = *++argv;
224 gw_log (GW_LOG_FATAL, KERNEL_LOG, "missing language name");
230 info.override_portno = argv[0]+2;
234 info.override_portno = *++argv;
238 gw_log (GW_LOG_FATAL, KERNEL_LOG, "missing portno");
244 info.override_hostname = argv[0]+2;
248 info.override_hostname = *++argv;
252 gw_log (GW_LOG_FATAL, KERNEL_LOG, "missing hostname");
258 gw_log_file (GW_LOG_ALL, argv[0]+2);
262 gw_log_file (GW_LOG_ALL, *++argv);
266 gw_log (GW_LOG_FATAL, KERNEL_LOG, "missing log filename");
272 userid = atoi (argv[0]+2);
276 userid = atoi (*++argv);
280 gw_log (GW_LOG_FATAL, KERNEL_LOG, "missing user id");
285 gw_log (GW_LOG_FATAL, KERNEL_LOG, "unknown option %s", *argv);
290 info.default_res = *argv;
296 kernel_events (userid);
304 struct gw_user_set *user_set_add (const char *name, int hits)
306 struct gw_user_set *s;
308 s = malloc (sizeof (*s));
311 s->name = gw_strdup (name);
318 void user_set_init (void)
320 struct gw_user_set *s, *s1;
322 for (s = info.sets; s; s = s1)
331 struct gw_user_set *user_set_search (const char *name)
333 struct gw_user_set *s;
337 for (s = info.sets; s; s = s->prev)
338 if (!strcmp (s->name, name))
344 static void fml_inf_write (int ch)
348 static FILE *fml_inf;
350 static int fml_inf_read (void)
352 return getc (fml_inf);
356 void read_kernel_res (void)
358 char path_prefix[128];
362 char resource_name[256];
367 ccl_qual_rm (&info.bibset);
368 info.bibset = ccl_qual_mk ();
371 gw_res_close (info.kernel_res);
372 info.kernel_res = gw_res_init ();
374 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "reading kernel resource, default %s",
377 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "reading kernel resource, target %s",
380 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "reading kernel resource, lang %s",
383 if (gw_res_merge (info.kernel_res, info.default_res))
385 gw_log (GW_LOG_WARN, KERNEL_LOG, "Couldn't read resource file %s",
389 strcpy (path_prefix, gw_res_get (info.kernel_res, "gw.path", "."));
393 sprintf (resource_name, "gw.target.%s", info.target);
394 v = gw_res_get (info.kernel_res, resource_name, NULL);
397 sprintf (fname, "%s/%s", path_prefix, v);
398 gw_res_merge (info.kernel_res, fname);
403 sprintf (resource_name, "gw.lang.%s", info.lang);
404 v = gw_res_get (info.kernel_res, resource_name, NULL);
407 sprintf (fname, "%s/%s", path_prefix, v);
408 gw_res_merge (info.kernel_res, fname);
411 if (info.override_res)
413 sprintf (fname, "%s/%s", path_prefix, info.override_res);
414 gw_res_merge (info.kernel_res, fname);
416 v = gw_res_get (info.kernel_res, "gw.bibset", NULL);
421 sprintf (fname, "%s/%s", path_prefix, v);
422 bib_inf = fopen (fname, "r");
424 gw_log (GW_LOG_WARN, KERNEL_LOG, "cannot open %s", fname);
427 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "reading bib file %s", fname);
428 ccl_qual_file (info.bibset, bib_inf);
432 sprintf (resource_name, "gw.target.%s", info.target);
433 if (*info.target && ! gw_res_get (info.kernel_res, resource_name, NULL))
435 /* target is there, and there is no sub-resource for it... */
438 if ((split = strchr (info.target, ':')))
440 strncpy (info.hostname, info.target, sizeof(info.hostname)-1);
442 info.port = atoi (split);
444 info.port = atoi (gw_res_get
445 (info.kernel_res, "gw.portno", "210"));
449 strncpy (info.hostname, gw_res_get (info.kernel_res,
450 "gw.hostname", "localhost"),
451 sizeof(info.hostname)-1);
452 info.port = atoi (gw_res_get (info.kernel_res,
453 "gw.portno", "210"));
456 free (info.databases);
458 free (info.database);
459 v = gw_res_get (info.kernel_res, "gw.databases", "");
460 info.databases = gw_strdup (v);
461 for (cp = info.databases; (cp = strchr (cp, ' ')); cp++)
463 v = gw_res_get (info.kernel_res, "gw.database", "");
464 if (*v == '\0' && *info.databases)
467 cp = strchr (info.databases, ',');
469 len = cp ? (cp-info.databases) : strlen (info.databases);
470 info.database = malloc (len+1);
471 assert (info.database);
472 memcpy (info.database, info.databases, len);
473 info.database[len] = '\0';
477 info.database = gw_strdup (v);
478 for (cp = info.database; (cp = strchr (cp, ' ')); cp++)
481 if (info.override_portno)
482 info.port = atoi (info.override_portno);
483 if (info.override_hostname)
484 strncpy (info.hostname, info.override_hostname,
485 sizeof(info.hostname)-1);
489 v = gw_res_get (info.kernel_res, "gw.fml", "default.fml");
490 sprintf (fname, "%s/%s", path_prefix, v);
491 fml_inf = fopen (fname, "r");
493 gw_log (GW_LOG_WARN, KERNEL_LOG,
494 "cannot open fml script %s", fname);
497 info.fml = fml_open ();
498 info.fml->read_func = fml_inf_read;
499 info.fml->write_func = fml_inf_write;
500 fml_preprocess (info.fml);