5 * Revision 1.12 1995/03/27 12:51:05 adam
6 * New log level in use: GW_LOG_ERRNO.
8 * Revision 1.11 1995/03/27 08:24:02 adam
9 * First use of gip interface and gw-db.
10 * First work on eti program.
12 * Revision 1.10 1995/03/01 14:32:25 adam
13 * Better diagnostics. Default is, that only one database selected when
16 * Revision 1.9 1995/02/23 08:32:17 adam
19 * Revision 1.7 1995/02/22 15:22:33 adam
20 * Much more checking of run-time state. Show command never retrieves
21 * more records than indicated by the previous search request. Help
22 * command available. The maximum number of records retrieved can be
25 * Revision 1.6 1995/02/22 08:51:34 adam
26 * Output function can be customized in fml, which is used to print
27 * the reply to reply_fd.
29 * Revision 1.5 1995/02/20 21:16:20 adam
30 * FML support. Bug fixes. Profile for drewdb.
32 * Revision 1.4 1995/02/17 17:06:16 adam
35 * Revision 1.3 1995/02/16 18:35:09 adam
36 * First use of Zdist library. Search requests are supported.
37 * Present requests are not supported yet.
39 * Revision 1.2 1995/02/16 13:21:00 adam
40 * Organization of resource files for targets and conversion
41 * language implemented.
43 * Revision 1.1 1995/02/15 17:45:29 adam
44 * First version of email gateway kernel. Email requests are read
45 * from stdin. The output is transferred to an MTA if 'From' is
46 * found in the header - or stdout if absent. No Z39.50 client is used.
55 #include <sys/types.h>
62 FILE *reply_fd = stdout;
64 struct gw_kernel_info info;
66 static void mk_active (int userid)
68 char active_name[1024];
72 sprintf (active_name, "kernel.pid.%d", userid);
73 fd = open (active_name, O_WRONLY|O_CREAT, 0666);
76 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, "Cannot create %s", active_name);
79 sprintf (pid_buf, "%ld", (long) getpid());
80 write (fd, pid_buf, strlen(pid_buf));
84 static void rm_active (int userid)
86 char active_name[1024];
88 sprintf (active_name, "kernel.pid.%d", userid);
92 static void kernel_events (int userid)
94 char fifo_client_name[1024];
95 char fifo_server_name[1024];
101 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "kernel event loop");
103 sprintf (fifo_client_name, "fifo.c.%d", userid);
104 sprintf (fifo_server_name, "fifo.s.%d", userid);
106 gip = gips_initialize (fifo_server_name);
107 gips_open (gip, fifo_client_name);
108 gip_fd = gip_infileno (gip);
113 FD_SET (gip_fd, &set_r);
117 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "IPC select");
118 r = select (gip_fd+1, &set_r, NULL, NULL, &tv);
121 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, KERNEL_LOG, "select");
126 gw_log (GW_LOG_STAT, KERNEL_LOG, "Timeout");
129 if (FD_ISSET (gip_fd, &set_r))
131 char command[128], *cp;
133 if (!(lgets (command, 127, gip_fd)))
135 if ((cp = strchr (command, '\n')))
137 gw_log (GW_LOG_STAT, KERNEL_LOG, "IPC: %s", command);
138 if (!strcmp (command, "mail"))
140 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Incoming mail...\n");
143 else if (!strcmp (command, "stop"))
145 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "stop");
150 gw_log (GW_LOG_WARN, KERNEL_LOG, "Unknown IPC: %s", command);
158 int main (int argc, char **argv)
162 info.kernel_res = NULL;
163 info.default_res = "default.res";
164 info.override_res = NULL;
169 info.override_portno = NULL;
170 info.override_hostname = NULL;
171 info.databases = NULL;
172 info.database = NULL;
179 info.kernel_res = gw_res_init ();
187 fprintf (stderr, "kernel [option..] [resource]\n");
188 fprintf (stderr, "If no resource file is given");
189 fprintf (stderr, " default.res is used\n");
190 fprintf (stderr, "Options:\n");
191 fprintf (stderr, " -d Enable debugging log\n");
192 fprintf (stderr, " -t target Open target immediately\n");
193 fprintf (stderr, " -l lang Set language\n");
194 fprintf (stderr, " -o resource Override with resource\n");
195 fprintf (stderr, " -h host Override host\n");
196 fprintf (stderr, " -p port Override port\n");
197 fprintf (stderr, " -g log Set Log file\n");
198 fprintf (stderr, " -i id Set IPC userid\n");
201 gw_log_level (GW_LOG_ALL);
205 strcpy (info.target, argv[0]+2);
209 strcpy (info.target, *++argv);
213 gw_log (GW_LOG_FATAL, KERNEL_LOG, "missing target name");
219 info.lang = argv[0]+2;
227 gw_log (GW_LOG_FATAL, KERNEL_LOG, "missing language name");
233 info.override_res = argv[0]+2;
237 info.override_res = *++argv;
241 gw_log (GW_LOG_FATAL, KERNEL_LOG, "missing override name");
247 info.override_portno = argv[0]+2;
251 info.override_portno = *++argv;
255 gw_log (GW_LOG_FATAL, KERNEL_LOG, "missing portno");
261 info.override_hostname = argv[0]+2;
265 info.override_hostname = *++argv;
269 gw_log (GW_LOG_FATAL, KERNEL_LOG, "missing hostname");
275 gw_log_file (GW_LOG_ALL, argv[0]+2);
279 gw_log_file (GW_LOG_ALL, *++argv);
283 gw_log (GW_LOG_FATAL, KERNEL_LOG, "missing log filename");
289 userid = atoi (argv[0]+2);
293 userid = atoi (*++argv);
297 gw_log (GW_LOG_FATAL, KERNEL_LOG, "missing user id");
302 gw_log (GW_LOG_FATAL, KERNEL_LOG, "unknown option %s", *argv);
307 info.default_res = *argv;
313 kernel_events (userid);
321 struct gw_user_set *user_set_add (const char *name, int hits)
323 struct gw_user_set *s;
325 s = malloc (sizeof (*s));
328 s->name = gw_strdup (name);
335 void user_set_init (void)
337 struct gw_user_set *s, *s1;
339 for (s = info.sets; s; s = s1)
348 struct gw_user_set *user_set_search (const char *name)
350 struct gw_user_set *s;
354 for (s = info.sets; s; s = s->prev)
355 if (!strcmp (s->name, name))
361 static void fml_inf_write (int ch)
365 static FILE *fml_inf;
367 static int fml_inf_read (void)
369 return getc (fml_inf);
373 void read_kernel_res (void)
375 char path_prefix[128];
379 char resource_name[256];
384 ccl_qual_rm (&info.bibset);
385 info.bibset = ccl_qual_mk ();
388 gw_res_close (info.kernel_res);
389 info.kernel_res = gw_res_init ();
391 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "reading kernel resource, default %s",
394 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "reading kernel resource, target %s",
397 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "reading kernel resource, lang %s",
400 if (gw_res_merge (info.kernel_res, info.default_res))
402 gw_log (GW_LOG_WARN, KERNEL_LOG, "Couldn't read resource file %s",
406 strcpy (path_prefix, gw_res_get (info.kernel_res, "gw.path", "."));
410 sprintf (resource_name, "gw.target.%s", info.target);
411 v = gw_res_get (info.kernel_res, resource_name, NULL);
414 sprintf (fname, "%s/%s", path_prefix, v);
415 gw_res_merge (info.kernel_res, fname);
420 sprintf (resource_name, "gw.lang.%s", info.lang);
421 v = gw_res_get (info.kernel_res, resource_name, NULL);
424 sprintf (fname, "%s/%s", path_prefix, v);
425 gw_res_merge (info.kernel_res, fname);
428 if (info.override_res)
430 sprintf (fname, "%s/%s", path_prefix, info.override_res);
431 gw_res_merge (info.kernel_res, fname);
433 v = gw_res_get (info.kernel_res, "gw.bibset", NULL);
438 sprintf (fname, "%s/%s", path_prefix, v);
439 bib_inf = fopen (fname, "r");
441 gw_log (GW_LOG_WARN, KERNEL_LOG, "cannot open %s", fname);
444 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "reading bib file %s", fname);
445 ccl_qual_file (info.bibset, bib_inf);
449 sprintf (resource_name, "gw.target.%s", info.target);
450 if (*info.target && ! gw_res_get (info.kernel_res, resource_name, NULL))
452 /* target is there, and there is no sub-resource for it... */
455 if ((split = strchr (info.target, ':')))
457 strncpy (info.hostname, info.target, sizeof(info.hostname)-1);
459 info.port = atoi (split);
461 info.port = atoi (gw_res_get
462 (info.kernel_res, "gw.portno", "210"));
466 strncpy (info.hostname, gw_res_get (info.kernel_res,
467 "gw.hostname", "localhost"),
468 sizeof(info.hostname)-1);
469 info.port = atoi (gw_res_get (info.kernel_res,
470 "gw.portno", "210"));
473 free (info.databases);
475 free (info.database);
476 v = gw_res_get (info.kernel_res, "gw.databases", "");
477 info.databases = gw_strdup (v);
478 for (cp = info.databases; (cp = strchr (cp, ' ')); cp++)
480 v = gw_res_get (info.kernel_res, "gw.database", "");
481 if (*v == '\0' && *info.databases)
484 cp = strchr (info.databases, ',');
486 len = cp ? (cp-info.databases) : strlen (info.databases);
487 info.database = malloc (len+1);
488 assert (info.database);
489 memcpy (info.database, info.databases, len);
490 info.database[len] = '\0';
494 info.database = gw_strdup (v);
495 for (cp = info.database; (cp = strchr (cp, ' ')); cp++)
498 if (info.override_portno)
499 info.port = atoi (info.override_portno);
500 if (info.override_hostname)
501 strncpy (info.hostname, info.override_hostname,
502 sizeof(info.hostname)-1);
506 v = gw_res_get (info.kernel_res, "gw.fml", "default.fml");
507 sprintf (fname, "%s/%s", path_prefix, v);
508 fml_inf = fopen (fname, "r");
510 gw_log (GW_LOG_WARN, KERNEL_LOG,
511 "cannot open fml script %s", fname);
514 info.fml = fml_open ();
515 info.fml->read_func = fml_inf_read;
516 info.fml->write_func = fml_inf_write;
517 fml_preprocess (info.fml);