* Europagate, 1995
*
* $Log: main.c,v $
- * Revision 1.1 1995/02/15 17:45:29 adam
+ * Revision 1.6 1995/02/22 08:51:34 adam
+ * Output function can be customized in fml, which is used to print
+ * the reply to reply_fd.
+ *
+ * Revision 1.5 1995/02/20 21:16:20 adam
+ * FML support. Bug fixes. Profile for drewdb.
+ *
+ * Revision 1.4 1995/02/17 17:06:16 adam
+ * Minor changes.
+ *
+ * Revision 1.3 1995/02/16 18:35:09 adam
+ * First use of Zdist library. Search requests are supported.
+ * Present requests are not supported yet.
+ *
+ * Revision 1.2 1995/02/16 13:21:00 adam
+ * Organization of resource files for targets and conversion
+ * language implemented.
+ *
+ * Revision 1.1 1995/02/15 17:45:29 adam
* First version of email gateway kernel. Email requests are read
* from stdin. The output is transferred to an MTA if 'From' is
* found in the header - or stdout if absent. No Z39.50 client is used.
#include <stdio.h>
#include <stdlib.h>
+#include <string.h>
+#include <assert.h>
#include "kernel.h"
-GwRes kernel_res;
-CCL_bibset bibset;
FILE *reply_fd = stdout;
+struct gw_kernel_info info;
+
int main (int argc, char **argv)
{
- char *bib_fname;
- FILE *bib_inf;
+ info.kernel_res = NULL;
+ info.default_res = "default.res";
+ info.override_res = NULL;
+ *info.target = 0;
+ info.lang = NULL;
+ info.bibset = NULL;
+ info.zass = NULL;
+ info.override_portno = NULL;
+ info.override_hostname = NULL;
+ info.databases = NULL;
+#if USE_FML
+ info.fml = NULL;
+#endif
gw_log_init (*argv);
- kernel_res = gw_res_init ();
- bibset = ccl_qual_mk ();
+ info.kernel_res = gw_res_init ();
while (--argc > 0)
{
if (**++argv == '-')
{
switch (argv[0][1])
{
- case 'b':
+ case 'd':
+ gw_log_level (GW_LOG_ALL);
+ break;
+ case 't':
+ if (argv[0][2])
+ strcpy (info.target, argv[0]+2);
+ else if (argc > 0)
+ {
+ --argc;
+ strcpy (info.target, *++argv);
+ }
+ else
+ {
+ gw_log (GW_LOG_FATAL, "main", "missing target name");
+ exit (1);
+ }
+ break;
+ case 'l':
+ if (argv[0][2])
+ info.lang = argv[0]+2;
+ else if (argc > 0)
+ {
+ --argc;
+ info.lang = *++argv;
+ }
+ else
+ {
+ gw_log (GW_LOG_FATAL, "main", "missing language name");
+ exit (1);
+ }
+ break;
+ case 'o':
if (argv[0][2])
- bib_fname = argv[0]+2;
+ info.override_res = argv[0]+2;
else if (argc > 0)
{
--argc;
- bib_fname = *++argv;
+ info.override_res = *++argv;
}
else
{
- gw_log (GW_LOG_FATAL, "main", "missing bib filename");
+ gw_log (GW_LOG_FATAL, "main", "missing language name");
exit (1);
}
- bib_inf = fopen (bib_fname, "r");
- if (!bib_inf)
+ break;
+ case 'p':
+ if (argv[0][2])
+ info.override_portno = argv[0]+2;
+ else if (argc > 0)
+ {
+ --argc;
+ info.override_portno = *++argv;
+ }
+ else
+ {
+ gw_log (GW_LOG_FATAL, "main", "missing portno");
+ exit (1);
+ }
+ break;
+ case 'h':
+ if (argv[0][2])
+ info.override_hostname = argv[0]+2;
+ else if (argc > 0)
+ {
+ --argc;
+ info.override_hostname = *++argv;
+ }
+ else
+ {
+ gw_log (GW_LOG_FATAL, "main", "missing hostname");
+ exit (1);
+ }
+ break;
+ case 'g':
+ if (argv[0][2])
+ gw_log_file (GW_LOG_ALL, argv[0]+2);
+ else if (argc > 0)
+ {
+ --argc;
+ gw_log_file (GW_LOG_ALL, *++argv);
+ }
+ else
{
- gw_log (GW_LOG_FATAL, "main", "cannot open %s", bib_fname);
+ gw_log (GW_LOG_FATAL, "main", "missing log filename");
exit (1);
}
- ccl_qual_file (bibset, bib_inf);
- fclose (bib_inf);
break;
default:
gw_log (GW_LOG_FATAL, "main", "unknown option %s", *argv);
}
}
else
- {
- int r;
- r = gw_res_merge (kernel_res, *argv);
- if (r)
- {
- gw_log (GW_LOG_FATAL, "main", "failed to read resource "
- "file %s", *argv);
- exit (1);
- }
- }
+ info.default_res = *argv;
}
+ read_kernel_res ();
urp (stdin);
return 0;
}
+#if USE_FML
+static void fml_inf_write (int ch)
+{
+ putc (ch, reply_fd);
+}
+static FILE *fml_inf;
+
+static int fml_inf_read (void)
+{
+ return getc (fml_inf);
+}
+#endif
+
+void read_kernel_res (void)
+{
+ char path_prefix[128];
+ char fname[160];
+ const char *v;
+ char *cp;
+ char resource_name[256];
+
+ if (info.bibset)
+ ccl_qual_rm (&info.bibset);
+ info.bibset = ccl_qual_mk ();
+
+ if (info.kernel_res)
+ gw_res_close (info.kernel_res);
+ info.kernel_res = gw_res_init ();
+
+ gw_log (GW_LOG_DEBUG, "main", "reading kernel resource, default %s",
+ info.default_res);
+ if (*info.target)
+ gw_log (GW_LOG_DEBUG, "main", "reading kernel resource, target %s",
+ info.target);
+ if (info.lang)
+ gw_log (GW_LOG_DEBUG, "main", "reading kernel resource, lang %s",
+ info.lang);
+
+ if (gw_res_merge (info.kernel_res, info.default_res))
+ {
+ gw_log (GW_LOG_WARN, "main", "Couldn't read resource file %s",
+ info.default_res);
+ return;
+ }
+ strcpy (path_prefix, gw_res_get (info.kernel_res, "gw.path", "."));
+
+ if (*info.target)
+ {
+ sprintf (resource_name, "gw.target.%s", info.target);
+ v = gw_res_get (info.kernel_res, resource_name, NULL);
+ if (v)
+ {
+ sprintf (fname, "%s/%s", path_prefix, v);
+ gw_res_merge (info.kernel_res, fname);
+ }
+ }
+ if (info.lang)
+ {
+ sprintf (resource_name, "gw.lang.%s", info.lang);
+ v = gw_res_get (info.kernel_res, resource_name, NULL);
+ if (v)
+ {
+ sprintf (fname, "%s/%s", path_prefix, v);
+ gw_res_merge (info.kernel_res, fname);
+ }
+ }
+ if (info.override_res)
+ {
+ sprintf (fname, "%s/%s", path_prefix, info.override_res);
+ gw_res_merge (info.kernel_res, fname);
+ }
+ v = gw_res_get (info.kernel_res, "gw.bibset", NULL);
+ if (v)
+ {
+ FILE *bib_inf;
+
+ sprintf (fname, "%s/%s", path_prefix, v);
+ bib_inf = fopen (fname, "r");
+ if (!bib_inf)
+ gw_log (GW_LOG_WARN, "main", "cannot open %s", fname);
+ else
+ {
+ gw_log (GW_LOG_DEBUG, "main", "reading bib file %s", fname);
+ ccl_qual_file (info.bibset, bib_inf);
+ fclose (bib_inf);
+ }
+ }
+ sprintf (resource_name, "gw.target.%s", info.target);
+ if (*info.target && ! gw_res_get (info.kernel_res, resource_name, NULL))
+ {
+ /* target is there, and there is no sub-resource for it... */
+ char *split;
+
+ if ((split = strchr (info.target, ':')))
+ *split++ = '\0';
+ strncpy (info.hostname, info.target, sizeof(info.hostname)-1);
+ if (split)
+ info.port = atoi (split);
+ else
+ info.port = atoi (gw_res_get
+ (info.kernel_res, "gw.portno", "210"));
+ }
+ else
+ {
+ strncpy (info.hostname, gw_res_get (info.kernel_res,
+ "gw.hostname", "localhost"),
+ sizeof(info.hostname)-1);
+ info.port = atoi (gw_res_get (info.kernel_res,
+ "gw.portno", "210"));
+ }
+ if (info.databases)
+ free (info.databases);
+ v = gw_res_get (info.kernel_res, "gw.databases", "");
+ info.databases = malloc (1+strlen(v));
+ assert (info.databases);
+ strcpy (info.databases, v);
+ for (cp = info.databases; (cp = strchr (cp, ' ')); cp++)
+ *cp = ',';
+ if (info.override_portno)
+ info.port = atoi (info.override_portno);
+ if (info.override_hostname)
+ strncpy (info.hostname, info.override_hostname,
+ sizeof(info.hostname)-1);
+#if USE_FML
+ if (!info.fml)
+ {
+ v = gw_res_get (info.kernel_res, "gw.fml", "default.fml");
+ sprintf (fname, "%s/%s", path_prefix, v);
+ fml_inf = fopen (fname, "r");
+ if (!fml_inf)
+ gw_log (GW_LOG_WARN, "main", "cannot open fml script %s", fname);
+ else
+ {
+ info.fml = fml_open ();
+ info.fml->read_func = fml_inf_read;
+ info.fml->write_func = fml_inf_write;
+ fml_preprocess (info.fml);
+ fml_exec (info.fml);
+ fclose (fml_inf);
+ }
+ }
+#endif
+}