1 /* Gateway kernel - User Request Processor
5 * Revision 1.27 1995/04/19 10:46:19 adam
6 * Persistency works much better now. New command: status - history-like
8 * Revision 1.26 1995/04/19 07:31:12 adam
9 * First work on Z39.50 persistence.
11 * Revision 1.25 1995/04/17 09:34:33 adam
12 * Timeout (idletime) adjustable. Minor changes in kernel.
14 * Revision 1.24 1995/03/28 11:42:35 adam
15 * First use of string-queue utility.
17 * Revision 1.23 1995/03/28 08:01:28 adam
18 * FIFO existence is used to test for a running kernel.
20 * Revision 1.22 1995/03/27 12:51:05 adam
21 * New log level in use: GW_LOG_ERRNO.
23 * Revision 1.21 1995/03/27 08:24:04 adam
24 * First use of gip interface and gw-db.
25 * First work on eti program.
27 * Revision 1.20 1995/03/03 17:19:17 adam
28 * Smarter presentation. Bug fix in email header interpretation.
30 * Revision 1.19 1995/03/02 09:32:11 adam
31 * New presentation formats. f0=full, f1=brief, f2=mid
33 * Revision 1.18 1995/03/01 14:32:26 adam
34 * Better diagnostics. Default is, that only one database selected when
37 * Revision 1.17 1995/02/28 13:16:26 adam
38 * Configurable From: added.
40 * Revision 1.16 1995/02/23 10:08:20 adam
41 * Added logging of all user commands.
43 * Revision 1.15 1995/02/23 08:32:17 adam
46 * Revision 1.13 1995/02/22 16:54:42 adam
47 * Qualifiers of LOC target updated. More logging messages.
49 * Revision 1.12 1995/02/22 15:51:51 adam
50 * Bug fix: swap of parameter number and offset in function present.
52 * Revision 1.11 1995/02/22 15:22:33 adam
53 * Much more checking of run-time state. Show command never retrieves
54 * more records than indicated by the previous search request. Help
55 * command available. The maximum number of records retrieved can be
58 * Revision 1.10 1995/02/22 08:51:35 adam
59 * Output function can be customized in fml, which is used to print
60 * the reply to reply_fd.
62 * Revision 1.9 1995/02/21 17:46:21 adam
65 * Revision 1.8 1995/02/21 12:12:00 adam
66 * Diagnostic record with error info. observed.
68 * Revision 1.7 1995/02/20 21:16:20 adam
69 * FML support. Bug fixes. Profile for drewdb.
71 * Revision 1.6 1995/02/17 14:41:14 quinn
72 * Added simple display of records.
74 * Revision 1.5 1995/02/17 14:22:13 adam
75 * First steps of CCL show command. Not finished yet.
77 * Revision 1.4 1995/02/17 09:08:36 adam
78 * Reply with subject. CCL base command implemented.
80 * Revision 1.3 1995/02/16 18:35:09 adam
81 * First use of Zdist library. Search requests are supported.
82 * Present requests are not supported yet.
84 * Revision 1.2 1995/02/16 13:21:00 adam
85 * Organization of resource files for targets and conversion
86 * language implemented.
88 * Revision 1.1 1995/02/15 17:45:30 adam
89 * First version of email gateway kernel. Email requests are read
90 * from stdin. The output is transferred to an MTA if 'From' is
91 * found in the header - or stdout if absent. No Z39.50 client is used.
104 #include <strqueue.h>
107 static char line_buf[LINE_MAX+1];
109 static void put_esc_str (const char *s)
115 int lgets (char *buf, int max, int fd)
122 if ((r=read (fd, buf+no, 1)) != 1)
125 gw_log (GW_LOG_WARN|GW_LOG_ERRNO, KERNEL_LOG, "read fail");
131 if (buf[no++] == '\n')
138 int reopen_target (void)
142 gw_log (GW_LOG_WARN, KERNEL_LOG, "Zass free...");
143 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "reopen_target");
144 info.zass = zass_open (info.hostname, info.port);
147 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Cannot connect to target %s:%d",
148 info.hostname, info.port);
149 fprintf (reply_fd, "%s %s:%d\n",
150 gw_res_get (info.kernel_res, "gw.err.connect",
151 "Cannot connect to target"),
152 info.hostname, info.port);
155 v = gw_res_get (info.kernel_res, "gw.description", NULL);
159 fprintf (reply_fd, "\n");
161 fprintf (reply_fd, "%s %s:%d\n",
162 gw_res_get (info.kernel_res, "gw.msg.connect",
163 "Connection established to"),
164 info.hostname, info.port);
166 fprintf (reply_fd, "%s:\n%s\n",
167 gw_res_get (info.kernel_res, "gw.msg.databases",
168 "Available databases"),
171 fprintf (reply_fd, "%s:\n%s\n",
172 gw_res_get (info.kernel_res, "gw.msg.database",
173 "Selected databases"),
176 fprintf (reply_fd, "set=%d\n", info.setno);
178 fprintf (reply_fd, "set=Default\n");
183 static struct command_word {
185 char *resource_suffix;
193 { "continue", "continue" },
194 { "status", "status" },
195 { "cancel", "cancel" },
196 { "target", "target" },
201 static int command_search (struct command_word *tab, struct ccl_token *cmd,
202 const char *resource_prefix)
206 assert (resource_prefix);
209 while (tab->default_value)
211 char *cp, command_names[60];
212 char resource_name[60];
215 sprintf (resource_name, "%s%s", resource_prefix,
216 tab->resource_suffix);
217 v = gw_res_get (info.kernel_res, resource_name, tab->default_value);
219 strcpy (command_names, v);
225 if ((split = strchr (cp, ' ')))
227 if (cmd->len == strlen(cp) &&
228 !memcmp (cmd->name, cp, cmd->len))
240 static struct error_no_struct {
243 } error_ccl_tab[] = {
245 { CCL_ERR_TERM_EXPECTED, "term.expected" },
246 { CCL_ERR_RP_EXPECTED, "rp.expected" },
247 { CCL_ERR_SETNAME_EXPECTED, "setname.expected" },
248 { CCL_ERR_OP_EXPECTED, "op.expected" },
249 { CCL_ERR_BAD_RP, "bad.rp" },
250 { CCL_ERR_UNKNOWN_QUAL, "unknown.qual" },
251 { CCL_ERR_DOUBLE_QUAL, "double.qual" },
252 { CCL_ERR_EQ_EXPECTED, "eq.expected" },
253 { CCL_ERR_BAD_RELATION, "bad.relation" },
254 { CCL_ERR_TRUNC_NOT_LEFT, "trunc.not.left" },
255 { CCL_ERR_TRUNC_NOT_BOTH, "trunc.not.both" },
256 { CCL_ERR_TRUNC_NOT_RIGHT, "trunc.not.right" },
260 static char *error_no_search (struct error_no_struct *tab, int no)
262 struct error_no_struct *p = tab;
263 while (p->resource_name)
266 return p->resource_name;
272 static int email_header (struct str_queue *sq,
273 char *from_str, char *subject_str)
277 while (str_queue_deq (sq, line_buf, LINE_MAX))
279 if (line_buf[0] == '\n')
281 if (memcmp (line_buf, "From ", 5) == 0)
282 sscanf (line_buf+4, "%s", from_str);
283 if (memcmp (line_buf, "Subject: ", 9) == 0 &&
284 sscanf (line_buf+9, "%s", subject_str+1) == 1)
285 strcpy (subject_str, line_buf+9);
290 static void help_general (void)
292 put_esc_str (gw_res_get (info.kernel_res, "gw.help.general",
293 "Commands available in this service:\n"));
296 static int exec_help (struct ccl_token *list)
298 static char *sep = "-------------------------------\\n";
303 put_esc_str (gw_res_get (info.kernel_res, "gw.help.target",
304 "target <name> - selects a given target\n"));
307 put_esc_str (gw_res_get (info.kernel_res, "gw.help.base",
308 "base <base>.. - selects databases\n"));
311 put_esc_str (gw_res_get (info.kernel_res, "gw.help.find",
312 "find <query> - performs a search request\n"));
315 put_esc_str (gw_res_get (info.kernel_res, "gw.help.show",
316 "show <spec> - retrieves and displays "
322 static void display_diag_error (int code, const char *addinfo)
326 sprintf (str, "gw.bib1.diag.%d", code);
327 fprintf (reply_fd, "%s %d:\n %s: '%s'\n",
328 gw_res_get (info.kernel_res, "gw.msg.z39errcode",
331 gw_res_get (info.kernel_res, str, ""), addinfo);
334 static int exec_find (struct ccl_token *list, const char *search_str)
336 const struct zass_searchent *p;
337 struct gw_user_set *us;
340 struct ccl_rpn_node *rpn;
344 if (info.setno == -1)
345 strcpy (setname, "Default");
347 sprintf (setname, "%d", info.setno);
348 rpn = ccl_find (info.bibset, list, &error, &pos);
351 const char *v = NULL, *n;
354 fprintf (reply_fd, " %*s^ - ", pos - line_buf, " ");
356 n = error_no_search (error_ccl_tab, error);
359 sprintf (name, "gw.err.%s", n);
360 v = gw_res_get (info.kernel_res, name, NULL);
363 v = ccl_err_msg (error);
364 fprintf (reply_fd, "%s\n", v);
367 ccl_pr_tree (rpn, reply_fd);
368 fprintf (reply_fd, "\n");
370 if (!*info.database )
372 fprintf (reply_fd, "%s\n",
373 gw_res_get (info.kernel_res, "gw.err.no.database",
374 "You must select database"));
377 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Searching in database %s",
380 fprintf (reply_fd, "Searching in database %s\n", info.database);
381 p = zass_p_search (info.zass, rpn, setname, info.database, info.sets);
384 fprintf (reply_fd, "Search fail\n");
387 if (p->errcode != -1)
389 display_diag_error (p->errcode, p->errstring);
392 fprintf (reply_fd, "%d %s\n", p->num,
393 gw_res_get (info.kernel_res, "gw.msg.hits", "hit(s)"));
394 us = user_set_add (setname, p->num, info.database, rpn, 1, search_str);
395 fprintf (reply_fd, "Result-set %s created\n", setname);
401 static int exec_target (struct ccl_token *list)
404 if (list->kind == CCL_TOK_EOL)
407 memcpy (info.target, list->name, len);
408 info.target [len] = '\0';
411 return reopen_target ();
414 static void exec_status_r (struct gw_user_set *sp)
418 exec_status_r (sp->prev);
419 fprintf (reply_fd, "%6s %7d %12.12s %.50s\n", sp->name, sp->hits,
420 sp->database, sp->search_str);
423 static int exec_status (struct ccl_token *list)
425 fprintf (reply_fd, " Name Hits Database Find\n");
426 exec_status_r (info.sets);
430 static int exec_base (struct ccl_token *list)
432 struct ccl_token *li = list;
436 if (list->kind == CCL_TOK_EOL)
438 free (info.database);
439 while (li->kind != CCL_TOK_EOL)
443 if (li->kind == CCL_TOK_COMMA)
446 info.database = malloc (len);
447 assert (info.database );
450 while (li->kind != CCL_TOK_EOL)
452 memcpy (info.database+len, li->name, li->len);
454 info.database[len++] = ',';
456 if (li->kind == CCL_TOK_COMMA)
459 info.database[len-1] = '\0';
463 struct command_word show_tab [] =
470 static void present (const char *set, int offset, int number,
471 struct ccl_token *format_token)
473 const struct zass_presentent *zp;
478 max_number = atoi (gw_res_get (info.kernel_res, "gw.max.show",
480 if (number > max_number)
482 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "present in set %s", set);
483 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "present of %d records from offset %d",
485 zp = zass_p_present(info.zass, (char *) set, offset, number);
491 int record_log_fd = -1;
492 const char *record_log_name;
494 record_log_name = gw_res_get (info.kernel_res, "gw.marc.log",
498 sprintf (path, "%s/%s", gw_res_get (info.kernel_res,
501 record_log_fd = open (path, O_WRONLY|O_CREAT|O_APPEND, 0666);
502 if (record_log_fd == -1)
503 gw_log (GW_LOG_WARN|GW_LOG_ERRNO, "Cannot open %s", path);
505 fprintf (reply_fd, gw_res_get (info.kernel_res,
507 "Got %d records from set %s"),
509 fprintf (reply_fd, "\n");
510 for (i = 0, pp = zp->records; pp; pp = pp->next, i++)
514 const char *arg_ar[5];
516 fprintf (reply_fd, "--- %d/%d ---\n",
517 i+offset, offset+zp->num-1);
518 if (!gw_res_get (info.kernel_res, "gw.ignore.which", NULL))
520 if (pp->which == ZASS_REC_DIAG)
522 display_diag_error (pp->errcode, pp->errstring);
525 else if (pp->which != ZASS_REC_USMARC)
527 fprintf (reply_fd, "Unknown record kind %d\n",
532 if (record_log_fd != -1)
533 write (record_log_fd, pp->record, strlen(pp->record));
534 rec = iso2709_cvt (pp->record);
540 len = format_token->len;
541 if (len >= sizeof(format_str))
542 len = sizeof(format_str)-1;
543 memcpy (format_str, format_token->name, len);
544 format_str[len] = '\0';
546 if (info.fml && format_token &&
547 (!strcmp (format_str, "0") || !strcmp (format_str, "1")
548 || !strcmp(format_str, "2")))
551 arg_ar[1] = format_str;
552 arg_ar[2] = " \\list";
553 arg_ar[3] = marc_to_str (info.fml, rec);
555 fml_exec_call_argv (info.fml, arg_ar);
558 iso2709_display (rec, reply_fd);
560 iso2709_display (rec, reply_fd);
565 fprintf (reply_fd, "Not a MARC record\n");
567 if (record_log_fd != -1)
568 close (record_log_fd);
572 static int exec_show (struct ccl_token *list)
575 struct ccl_token *set_token = NULL;
576 struct ccl_token *format_token = NULL;
577 struct ccl_token *li = list;
578 int no_of_present = 0;
581 while (li->kind != CCL_TOK_EOL)
584 if (li->next->kind == CCL_TOK_EQ)
586 if (li->kind == CCL_TOK_SET) /* set = <name> ? */
593 modifier_no = command_search (show_tab, li, "ccl.token.");
596 fprintf (reply_fd, "Unknown modifier in show\n");
600 if (modifier_no == 1) /* f = <name> ? */
602 else if (modifier_no == 2) /* p = <name> ? */
604 if (li->kind != CCL_TOK_EOL /* p = <name> - <name> ? */
605 && li->next->kind == CCL_TOK_MINUS
606 && li->next->next != CCL_TOK_EOL)
612 fprintf (reply_fd, "%s\n", "Missing token after '='");
621 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Got set=%.*s", set_token->len,
624 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Got format=%.*s", format_token->len,
628 while (li->kind != CCL_TOK_EOL)
634 if (li->next->kind == CCL_TOK_EQ && li->kind != CCL_TOK_SET)
636 modifier_no = command_search (show_tab, li, "ccl.token.");
638 if (modifier_no == 2) /* p = <name> ? */
640 if (li->kind != CCL_TOK_EOL /* p = <name> - <name> ? */
641 && li->next->kind == CCL_TOK_MINUS
642 && li->next->next != CCL_TOK_EOL)
645 memcpy (tmp_str, li->name, len);
646 tmp_str [len] = '\0';
647 offset = atoi (tmp_str);
651 memcpy (tmp_str, li->name, len);
652 tmp_str [len] = '\0';
653 number = atoi (tmp_str) - offset + 1;
658 memcpy (tmp_str, li->name, len);
659 tmp_str [len] = '\0';
660 offset = atoi (tmp_str);
669 memcpy (tmp_str, li->name, len);
671 number = atoi (tmp_str);
675 if (offset > 0 && number > 0)
677 struct gw_user_set *us;
681 len = set_token->len;
682 memcpy (tmp_str, set_token->name, len);
684 us = user_set_search (tmp_str);
687 us = user_set_search (NULL);
688 if (us && us->hits != -1) /* proper result-set? */
690 if (offset <= us->hits)
692 if (offset+number-1 > us->hits)
693 number = us->hits - offset+1;
694 present (us->name, offset, number, format_token);
697 else if (!no_of_present) /* display error message once! */
699 fprintf (reply_fd, "%s\n",
700 gw_res_get (info.kernel_res, "gw.err.no.set",
701 "No result-set generated"));
706 if (!no_of_present) /* no records shown so far? */
708 struct gw_user_set *us;
711 us = user_set_search (NULL);
712 if (us && us->hits != -1) /* proper result-set? */
714 default_show = atoi (gw_res_get (info.kernel_res,
715 "gw.default.show", "20"));
716 if (us->hits > default_show)
717 present (us->name, 1, default_show, format_token);
718 else if (us->hits > 0)
719 present (us->name, 1, us->hits, format_token);
721 else /* display error message */
723 fprintf (reply_fd, "%s\n",
724 gw_res_get (info.kernel_res, "gw.err.no.set",
725 "No result-set generated"));
732 static int exec_command (const char *str)
734 struct ccl_token *cmd = ccl_tokenize (str);
737 if (cmd->kind != CCL_TOK_EOL &&
738 (no = command_search (command_tab, cmd, "ccl.command.")))
740 if (!info.zass && no != 9 && no != 4)
742 fprintf (reply_fd, "\n> %s\n", str);
743 if (!info.zass && (no == 1 || no == 2 || no == 3))
745 fprintf (reply_fd, "%s\n",
746 gw_res_get (info.kernel_res, "gw.err.no.target",
747 "No connection established - "
752 ccl_token_and = gw_res_get (info.kernel_res, "ccl.token.and", "and");
753 ccl_token_or = gw_res_get (info.kernel_res, "ccl.token.or", "or");
754 ccl_token_not = gw_res_get (info.kernel_res, "ccl.token.not", "not");
755 ccl_token_set = gw_res_get (info.kernel_res, "ccl.token.set", "set");
760 return exec_find (cmd->next, str);
762 return exec_show (cmd->next);
764 return exec_base (cmd->next);
766 return exec_help (cmd->next);
768 return exec_status (cmd->next);
770 return exec_target (cmd->next);
772 fprintf (reply_fd, "%s\n",
773 gw_res_get (info.kernel_res, "gw.err.unimplemented",
774 "Not implemented yet"));
779 fprintf (reply_fd, "\n> %s\n", str);
780 fprintf (reply_fd, " ^ %s\n",
781 gw_res_get (info.kernel_res, "gw.err.unknown.command",
783 "Use help to see list of commands"));
788 int urp_start (int continuation, struct str_queue *queue)
790 char subject_str[128];
793 info.reply_fname = NULL;
795 if (email_header (queue, info.from_str, subject_str))
797 gw_log (GW_LOG_WARN, KERNEL_LOG, "No message body");
800 tty_init (stdout, 40, 70);
803 info.reply_fname = tempnam (gw_res_get (info.kernel_res,
804 "gw.reply.tmp.dir", NULL),
805 gw_res_get (info.kernel_res,
806 "gw.reply.tmp.prefix", "gwr"));
808 reply_fd = fopen (info.reply_fname, "w");
811 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, KERNEL_LOG, "Cannot create %s",
815 tty_init (reply_fd, 0, 0);
816 fprintf (reply_fd, "From: %s\n",
817 gw_res_get (info.kernel_res, "gw.msg.from","Email-gateway"));
818 fprintf (reply_fd, "Subject: ");
820 fprintf (reply_fd, "Z39.50 Re: %s", subject_str);
822 fprintf (reply_fd, "%s\n", gw_res_get (info.kernel_res,
825 fprintf (reply_fd, "\n");
826 gw_log (GW_LOG_ACCT, KERNEL_LOG, "User start %s", info.from_str);
829 gw_log (GW_LOG_WARN, KERNEL_LOG, "No From in email header");
830 fprintf (reply_fd, "%s\n", gw_res_get (info.kernel_res, "gw.msg.greeting",
831 "Email->Z39.50 gateway"));
833 fprintf (reply_fd, "%s\n", gw_res_get (info.kernel_res,
839 int urp_command (struct str_queue *queue)
843 while (str_queue_deq (queue, line_buf, LINE_MAX))
845 if (line_buf[0] == '\n')
848 while (str_queue_deq (queue, 0, 0))
854 if ((cp = strchr (line_buf, '\n')))
856 gw_log (GW_LOG_ACCT, KERNEL_LOG, "cmd: %s", line_buf);
857 if (isalpha (line_buf[0]))
858 exec_command (line_buf);
866 if (!info.command_no)
868 fprintf (reply_fd, "%s\n",
869 gw_res_get (info.kernel_res, "gw.err.nullbody", "No body"));
878 assert (info.reply_fname);
882 mta = gw_res_get (info.kernel_res, "gw.reply.mta",
883 "/usr/lib/sendmail");
884 sprintf (cmd, "%s %s < %s", mta, info.from_str, info.reply_fname);
886 mta_code = system (cmd);
888 gw_log (GW_LOG_FATAL, KERNEL_LOG,
889 "Reply '%s' got exit code %d", cmd, mta_code);
891 unlink (info.reply_fname);
892 gw_log (GW_LOG_ACCT, KERNEL_LOG, "User end %s", info.from_str);