1 /* Gateway kernel - User Request Processor
5 * Revision 1.25 1995/04/17 09:34:33 adam
6 * Timeout (idletime) adjustable. Minor changes in kernel.
8 * Revision 1.24 1995/03/28 11:42:35 adam
9 * First use of string-queue utility.
11 * Revision 1.23 1995/03/28 08:01:28 adam
12 * FIFO existence is used to test for a running kernel.
14 * Revision 1.22 1995/03/27 12:51:05 adam
15 * New log level in use: GW_LOG_ERRNO.
17 * Revision 1.21 1995/03/27 08:24:04 adam
18 * First use of gip interface and gw-db.
19 * First work on eti program.
21 * Revision 1.20 1995/03/03 17:19:17 adam
22 * Smarter presentation. Bug fix in email header interpretation.
24 * Revision 1.19 1995/03/02 09:32:11 adam
25 * New presentation formats. f0=full, f1=brief, f2=mid
27 * Revision 1.18 1995/03/01 14:32:26 adam
28 * Better diagnostics. Default is, that only one database selected when
31 * Revision 1.17 1995/02/28 13:16:26 adam
32 * Configurable From: added.
34 * Revision 1.16 1995/02/23 10:08:20 adam
35 * Added logging of all user commands.
37 * Revision 1.15 1995/02/23 08:32:17 adam
40 * Revision 1.13 1995/02/22 16:54:42 adam
41 * Qualifiers of LOC target updated. More logging messages.
43 * Revision 1.12 1995/02/22 15:51:51 adam
44 * Bug fix: swap of parameter number and offset in function present.
46 * Revision 1.11 1995/02/22 15:22:33 adam
47 * Much more checking of run-time state. Show command never retrieves
48 * more records than indicated by the previous search request. Help
49 * command available. The maximum number of records retrieved can be
52 * Revision 1.10 1995/02/22 08:51:35 adam
53 * Output function can be customized in fml, which is used to print
54 * the reply to reply_fd.
56 * Revision 1.9 1995/02/21 17:46:21 adam
59 * Revision 1.8 1995/02/21 12:12:00 adam
60 * Diagnostic record with error info. observed.
62 * Revision 1.7 1995/02/20 21:16:20 adam
63 * FML support. Bug fixes. Profile for drewdb.
65 * Revision 1.6 1995/02/17 14:41:14 quinn
66 * Added simple display of records.
68 * Revision 1.5 1995/02/17 14:22:13 adam
69 * First steps of CCL show command. Not finished yet.
71 * Revision 1.4 1995/02/17 09:08:36 adam
72 * Reply with subject. CCL base command implemented.
74 * Revision 1.3 1995/02/16 18:35:09 adam
75 * First use of Zdist library. Search requests are supported.
76 * Present requests are not supported yet.
78 * Revision 1.2 1995/02/16 13:21:00 adam
79 * Organization of resource files for targets and conversion
80 * language implemented.
82 * Revision 1.1 1995/02/15 17:45:30 adam
83 * First version of email gateway kernel. Email requests are read
84 * from stdin. The output is transferred to an MTA if 'From' is
85 * found in the header - or stdout if absent. No Z39.50 client is used.
101 static char line_buf[LINE_MAX+1];
103 static void put_esc_str (const char *s)
109 int lgets (char *buf, int max, int fd)
116 if ((r=read (fd, buf+no, 1)) != 1)
119 gw_log (GW_LOG_WARN|GW_LOG_ERRNO, KERNEL_LOG, "read fail");
125 if (buf[no++] == '\n')
132 static int reopen_target (void)
136 gw_log (GW_LOG_WARN, KERNEL_LOG, "Zass free...");
137 info.zass = zass_open (info.hostname, info.port);
140 fprintf (reply_fd, "%s %s:%d\n",
141 gw_res_get (info.kernel_res, "gw.err.connect",
142 "Cannot connect to target"),
143 info.hostname, info.port);
146 v = gw_res_get (info.kernel_res, "gw.description", NULL);
150 fprintf (reply_fd, "\n");
152 fprintf (reply_fd, "%s %s:%d\n",
153 gw_res_get (info.kernel_res, "gw.msg.connect",
154 "Connection established to"),
155 info.hostname, info.port);
157 fprintf (reply_fd, "%s:\n%s\n",
158 gw_res_get (info.kernel_res, "gw.msg.databases",
159 "Available databases"),
162 fprintf (reply_fd, "%s:\n%s\n",
163 gw_res_get (info.kernel_res, "gw.msg.database",
164 "Selected databases"),
170 static struct command_word {
172 char *resource_suffix;
180 { "continue", "continue" },
181 { "status", "status" },
182 { "cancel", "cancel" },
183 { "target", "target" },
188 static int command_search (struct command_word *tab, struct ccl_token *cmd,
189 const char *resource_prefix)
193 assert (resource_prefix);
196 while (tab->default_value)
198 char *cp, command_names[60];
199 char resource_name[60];
202 sprintf (resource_name, "%s%s", resource_prefix,
203 tab->resource_suffix);
204 v = gw_res_get (info.kernel_res, resource_name, tab->default_value);
206 strcpy (command_names, v);
212 if ((split = strchr (cp, ' ')))
214 if (cmd->len == strlen(cp) &&
215 !memcmp (cmd->name, cp, cmd->len))
227 static struct error_no_struct {
230 } error_ccl_tab[] = {
232 { CCL_ERR_TERM_EXPECTED, "term.expected" },
233 { CCL_ERR_RP_EXPECTED, "rp.expected" },
234 { CCL_ERR_SETNAME_EXPECTED, "setname.expected" },
235 { CCL_ERR_OP_EXPECTED, "op.expected" },
236 { CCL_ERR_BAD_RP, "bad.rp" },
237 { CCL_ERR_UNKNOWN_QUAL, "unknown.qual" },
238 { CCL_ERR_DOUBLE_QUAL, "double.qual" },
239 { CCL_ERR_EQ_EXPECTED, "eq.expected" },
240 { CCL_ERR_BAD_RELATION, "bad.relation" },
241 { CCL_ERR_TRUNC_NOT_LEFT, "trunc.not.left" },
242 { CCL_ERR_TRUNC_NOT_BOTH, "trunc.not.both" },
243 { CCL_ERR_TRUNC_NOT_RIGHT, "trunc.not.right" },
247 static char *error_no_search (struct error_no_struct *tab, int no)
249 struct error_no_struct *p = tab;
250 while (p->resource_name)
253 return p->resource_name;
259 static int email_header (struct str_queue *sq,
260 char *from_str, char *subject_str)
264 while (str_queue_deq (sq, line_buf, LINE_MAX))
266 if (line_buf[0] == '\n')
268 if (memcmp (line_buf, "From ", 5) == 0)
269 sscanf (line_buf+4, "%s", from_str);
270 if (memcmp (line_buf, "Subject: ", 9) == 0 &&
271 sscanf (line_buf+9, "%s", subject_str+1) == 1)
272 strcpy (subject_str, line_buf+9);
277 static void help_general (void)
279 put_esc_str (gw_res_get (info.kernel_res, "gw.help.general",
280 "Commands available in this service:\n"));
283 static int exec_help (struct ccl_token *list)
285 static char *sep = "-------------------------------\\n";
290 put_esc_str (gw_res_get (info.kernel_res, "gw.help.target",
291 "target <name> - selects a given target\n"));
294 put_esc_str (gw_res_get (info.kernel_res, "gw.help.base",
295 "base <base>.. - selects databases\n"));
298 put_esc_str (gw_res_get (info.kernel_res, "gw.help.find",
299 "find <query> - performs a search request\n"));
302 put_esc_str (gw_res_get (info.kernel_res, "gw.help.show",
303 "show <spec> - retrieves and displays "
309 static void display_diag_error (int code, const char *addinfo)
313 sprintf (str, "gw.bib1.diag.%d", code);
314 fprintf (reply_fd, "%s %d:\n %s: '%s'\n",
315 gw_res_get (info.kernel_res, "gw.msg.z39errcode",
318 gw_res_get (info.kernel_res, str, ""), addinfo);
321 static int exec_find (struct ccl_token *list)
323 const struct zass_searchent *p;
324 struct gw_user_set *us;
326 struct ccl_rpn_node *rpn;
330 us = user_set_add ("Default", -1);
331 rpn = ccl_find (info.bibset, list, &error, &pos);
334 const char *v = NULL, *n;
337 fprintf (reply_fd, " %*s^ - ", pos - line_buf, " ");
339 n = error_no_search (error_ccl_tab, error);
342 sprintf (name, "gw.err.%s", n);
343 v = gw_res_get (info.kernel_res, name, NULL);
346 v = ccl_err_msg (error);
347 fprintf (reply_fd, "%s\n", v);
350 ccl_pr_tree (rpn, reply_fd);
351 fprintf (reply_fd, "\n");
353 if (!*info.database )
355 fprintf (reply_fd, "%s\n",
356 gw_res_get (info.kernel_res, "gw.err.no.database",
357 "You must select database"));
360 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Searching in database %s",
363 p = zass_search (info.zass, rpn, "Default", info.database );
366 if (p->errcode != -1)
368 display_diag_error (p->errcode, p->errstring);
371 fprintf (reply_fd, "%d %s\n", p->num,
372 gw_res_get (info.kernel_res, "gw.msg.hits", "hit(s)"));
377 static int exec_target (struct ccl_token *list)
380 if (list->kind == CCL_TOK_EOL)
383 memcpy (info.target, list->name, len);
384 info.target [len] = '\0';
387 return reopen_target ();
390 static int exec_base (struct ccl_token *list)
392 struct ccl_token *li = list;
396 if (list->kind == CCL_TOK_EOL)
398 free (info.database);
399 while (li->kind != CCL_TOK_EOL)
403 if (li->kind == CCL_TOK_COMMA)
406 info.database = malloc (len);
407 assert (info.database );
410 while (li->kind != CCL_TOK_EOL)
412 memcpy (info.database+len, li->name, li->len);
414 info.database[len++] = ',';
416 if (li->kind == CCL_TOK_COMMA)
419 info.database[len-1] = '\0';
423 struct command_word show_tab [] =
430 static void present (const char *set, int offset, int number,
431 struct ccl_token *format_token)
433 const struct zass_presentent *zp;
438 max_number = atoi (gw_res_get (info.kernel_res, "gw.max.show",
440 if (number > max_number)
442 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "present in set %s", set);
443 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "present of %d records from offset %d",
445 zp = zass_present(info.zass, (char *) set, offset, number);
451 int record_log_fd = -1;
452 const char *record_log_name;
454 record_log_name = gw_res_get (info.kernel_res, "gw.marc.log",
458 sprintf (path, "%s/%s", gw_res_get (info.kernel_res,
461 record_log_fd = open (path, O_WRONLY|O_CREAT|O_APPEND, 0666);
462 if (record_log_fd == -1)
463 gw_log (GW_LOG_WARN|GW_LOG_ERRNO, "Cannot open %s", path);
465 fprintf (reply_fd, gw_res_get (info.kernel_res,
469 fprintf (reply_fd, "\n");
470 for (i = 0, pp = zp->records; pp; pp = pp->next, i++)
474 const char *arg_ar[5];
476 fprintf (reply_fd, "--- %d/%d ---\n",
477 i+offset, offset+zp->num-1);
478 if (!gw_res_get (info.kernel_res, "gw.ignore.which", NULL))
480 if (pp->which == ZASS_REC_DIAG)
482 display_diag_error (pp->errcode, pp->errstring);
485 else if (pp->which != ZASS_REC_USMARC)
487 fprintf (reply_fd, "Unknown record kind %d\n",
492 if (record_log_fd != -1)
493 write (record_log_fd, pp->record, strlen(pp->record));
494 rec = iso2709_cvt (pp->record);
500 len = format_token->len;
501 if (len >= sizeof(format_str))
502 len = sizeof(format_str)-1;
503 memcpy (format_str, format_token->name, len);
504 format_str[len] = '\0';
506 if (info.fml && format_token &&
507 (!strcmp (format_str, "0") || !strcmp (format_str, "1")
508 || !strcmp(format_str, "2")))
511 arg_ar[1] = format_str;
512 arg_ar[2] = " \\list";
513 arg_ar[3] = marc_to_str (info.fml, rec);
515 fml_exec_call_argv (info.fml, arg_ar);
518 iso2709_display (rec, reply_fd);
520 iso2709_display (rec, reply_fd);
525 fprintf (reply_fd, "Not a MARC record\n");
527 if (record_log_fd != -1)
528 close (record_log_fd);
532 static int exec_show (struct ccl_token *list)
535 struct ccl_token *set_token = NULL;
536 struct ccl_token *format_token = NULL;
537 struct ccl_token *li = list;
538 int no_of_present = 0;
541 while (li->kind != CCL_TOK_EOL)
544 if (li->next->kind == CCL_TOK_EQ)
546 if (li->kind == CCL_TOK_SET) /* set = <name> ? */
553 modifier_no = command_search (show_tab, li, "ccl.token.");
556 fprintf (reply_fd, "Unknown modifier in show\n");
560 if (modifier_no == 1) /* f = <name> ? */
562 else if (modifier_no == 2) /* p = <name> ? */
564 if (li->kind != CCL_TOK_EOL /* p = <name> - <name> ? */
565 && li->next->kind == CCL_TOK_MINUS
566 && li->next->next != CCL_TOK_EOL)
572 fprintf (reply_fd, "%s\n", "Missing token after '='");
581 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Got set=%.*s", set_token->len,
584 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Got format=%.*s", format_token->len,
588 while (li->kind != CCL_TOK_EOL)
594 if (li->next->kind == CCL_TOK_EQ && li->kind != CCL_TOK_SET)
596 modifier_no = command_search (show_tab, li, "ccl.token.");
598 if (modifier_no == 2) /* p = <name> ? */
600 if (li->kind != CCL_TOK_EOL /* p = <name> - <name> ? */
601 && li->next->kind == CCL_TOK_MINUS
602 && li->next->next != CCL_TOK_EOL)
605 memcpy (tmp_str, li->name, len);
606 tmp_str [len] = '\0';
607 offset = atoi (tmp_str);
611 memcpy (tmp_str, li->name, len);
612 tmp_str [len] = '\0';
613 number = atoi (tmp_str) - offset + 1;
618 memcpy (tmp_str, li->name, len);
619 tmp_str [len] = '\0';
620 offset = atoi (tmp_str);
629 memcpy (tmp_str, li->name, len);
631 number = atoi (tmp_str);
635 if (offset > 0 && number > 0)
637 struct gw_user_set *us;
641 len = set_token->len;
642 memcpy (tmp_str, set_token->name, len);
644 us = user_set_search (tmp_str);
647 us = user_set_search (NULL);
648 if (us && us->hits != -1) /* proper result-set? */
650 if (offset <= us->hits)
652 if (offset+number-1 > us->hits)
653 number = us->hits - offset+1;
654 present (us->name, offset, number, format_token);
657 else if (!no_of_present) /* display error message once! */
659 fprintf (reply_fd, "%s\n",
660 gw_res_get (info.kernel_res, "gw.err.no.set",
661 "No result-set generated"));
666 if (!no_of_present) /* no records shown so far? */
668 struct gw_user_set *us;
671 us = user_set_search (NULL);
672 if (us && us->hits != -1) /* proper result-set? */
674 default_show = atoi (gw_res_get (info.kernel_res,
675 "gw.default.show", "20"));
676 if (us->hits > default_show)
677 present (us->name, 1, default_show, format_token);
678 else if (us->hits > 0)
679 present (us->name, 1, us->hits, format_token);
681 else /* display error message */
683 fprintf (reply_fd, "%s\n",
684 gw_res_get (info.kernel_res, "gw.err.no.set",
685 "No result-set generated"));
692 static int exec_command (const char *str)
694 struct ccl_token *cmd = ccl_tokenize (str);
697 if (cmd->kind != CCL_TOK_EOL &&
698 (no = command_search (command_tab, cmd, "ccl.command.")))
700 if (!info.zass && no != 9 && no != 4)
702 fprintf (reply_fd, "\n> %s\n", str);
703 if (!info.zass && (no == 1 || no == 2 || no == 3))
705 fprintf (reply_fd, "%s\n",
706 gw_res_get (info.kernel_res, "gw.err.no.target",
707 "No connection established - "
714 return exec_find (cmd->next);
716 return exec_show (cmd->next);
718 return exec_base (cmd->next);
720 return exec_help (cmd->next);
722 return exec_target (cmd->next);
724 fprintf (reply_fd, "%s\n",
725 gw_res_get (info.kernel_res, "gw.err.unimplemented",
726 "Not implemented yet"));
731 fprintf (reply_fd, "\n> %s\n", str);
732 fprintf (reply_fd, " ^ %s\n",
733 gw_res_get (info.kernel_res, "gw.err.unknown.command",
735 "Use help to see list of commands"));
740 int urp_start (int continuation, struct str_queue *queue)
742 char subject_str[128];
745 info.reply_fname = NULL;
747 if (email_header (queue, info.from_str, subject_str))
749 gw_log (GW_LOG_WARN, KERNEL_LOG, "No message body");
752 tty_init (stdout, 40, 70);
755 info.reply_fname = tempnam (gw_res_get (info.kernel_res,
756 "gw.reply.tmp.dir", NULL),
757 gw_res_get (info.kernel_res,
758 "gw.reply.tmp.prefix", "gwr"));
760 reply_fd = fopen (info.reply_fname, "w");
763 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, KERNEL_LOG, "Cannot create %s",
767 tty_init (reply_fd, 0, 0);
768 fprintf (reply_fd, "From: %s\n",
769 gw_res_get (info.kernel_res, "gw.msg.from","Email-gateway"));
770 fprintf (reply_fd, "Subject: ");
772 fprintf (reply_fd, "Z39.50 Re: %s", subject_str);
774 fprintf (reply_fd, "%s\n", gw_res_get (info.kernel_res,
777 fprintf (reply_fd, "\n");
778 gw_log (GW_LOG_ACCT, KERNEL_LOG, "User start %s", info.from_str);
781 gw_log (GW_LOG_WARN, KERNEL_LOG, "No From in email header");
782 fprintf (reply_fd, "%s\n", gw_res_get (info.kernel_res, "gw.msg.greeting",
783 "Email->Z39.50 gateway"));
785 fprintf (reply_fd, "%s\n", gw_res_get (info.kernel_res,
788 ccl_token_and = gw_res_get (info.kernel_res, "ccl.token.and", "and");
789 ccl_token_or = gw_res_get (info.kernel_res, "ccl.token.or", "or");
790 ccl_token_not = gw_res_get (info.kernel_res, "ccl.token.not", "not");
791 ccl_token_set = gw_res_get (info.kernel_res, "ccl.token.set", "set");
795 int urp_command (struct str_queue *queue)
799 while (str_queue_deq (queue, line_buf, LINE_MAX))
801 if (line_buf[0] == '\n')
804 while (str_queue_deq (queue, 0, 0))
810 if ((cp = strchr (line_buf, '\n')))
812 gw_log (GW_LOG_ACCT, KERNEL_LOG, "cmd: %s", line_buf);
813 if (isalpha (line_buf[0]))
814 exec_command (line_buf);
822 if (!info.command_no)
824 fprintf (reply_fd, "%s\n",
825 gw_res_get (info.kernel_res, "gw.err.nullbody", "No body"));
834 assert (info.reply_fname);
838 mta = gw_res_get (info.kernel_res, "gw.reply.mta",
839 "/usr/lib/sendmail");
840 sprintf (cmd, "%s %s < %s", mta, info.from_str, info.reply_fname);
842 mta_code = system (cmd);
844 gw_log (GW_LOG_FATAL, KERNEL_LOG,
845 "Reply '%s' got exit code %d", cmd, mta_code);
847 unlink (info.reply_fname);
848 gw_log (GW_LOG_ACCT, KERNEL_LOG, "User end %s", info.from_str);