5 * Revision 1.24 1995/03/28 11:42:35 adam
6 * First use of string-queue utility.
8 * Revision 1.23 1995/03/28 08:01:28 adam
9 * FIFO existence is used to test for a running kernel.
11 * Revision 1.22 1995/03/27 12:51:05 adam
12 * New log level in use: GW_LOG_ERRNO.
14 * Revision 1.21 1995/03/27 08:24:04 adam
15 * First use of gip interface and gw-db.
16 * First work on eti program.
18 * Revision 1.20 1995/03/03 17:19:17 adam
19 * Smarter presentation. Bug fix in email header interpretation.
21 * Revision 1.19 1995/03/02 09:32:11 adam
22 * New presentation formats. f0=full, f1=brief, f2=mid
24 * Revision 1.18 1995/03/01 14:32:26 adam
25 * Better diagnostics. Default is, that only one database selected when
28 * Revision 1.17 1995/02/28 13:16:26 adam
29 * Configurable From: added.
31 * Revision 1.16 1995/02/23 10:08:20 adam
32 * Added logging of all user commands.
34 * Revision 1.15 1995/02/23 08:32:17 adam
37 * Revision 1.13 1995/02/22 16:54:42 adam
38 * Qualifiers of LOC target updated. More logging messages.
40 * Revision 1.12 1995/02/22 15:51:51 adam
41 * Bug fix: swap of parameter number and offset in function present.
43 * Revision 1.11 1995/02/22 15:22:33 adam
44 * Much more checking of run-time state. Show command never retrieves
45 * more records than indicated by the previous search request. Help
46 * command available. The maximum number of records retrieved can be
49 * Revision 1.10 1995/02/22 08:51:35 adam
50 * Output function can be customized in fml, which is used to print
51 * the reply to reply_fd.
53 * Revision 1.9 1995/02/21 17:46:21 adam
56 * Revision 1.8 1995/02/21 12:12:00 adam
57 * Diagnostic record with error info. observed.
59 * Revision 1.7 1995/02/20 21:16:20 adam
60 * FML support. Bug fixes. Profile for drewdb.
62 * Revision 1.6 1995/02/17 14:41:14 quinn
63 * Added simple display of records.
65 * Revision 1.5 1995/02/17 14:22:13 adam
66 * First steps of CCL show command. Not finished yet.
68 * Revision 1.4 1995/02/17 09:08:36 adam
69 * Reply with subject. CCL base command implemented.
71 * Revision 1.3 1995/02/16 18:35:09 adam
72 * First use of Zdist library. Search requests are supported.
73 * Present requests are not supported yet.
75 * Revision 1.2 1995/02/16 13:21:00 adam
76 * Organization of resource files for targets and conversion
77 * language implemented.
79 * Revision 1.1 1995/02/15 17:45:30 adam
80 * First version of email gateway kernel. Email requests are read
81 * from stdin. The output is transferred to an MTA if 'From' is
82 * found in the header - or stdout if absent. No Z39.50 client is used.
98 static char line_buf[LINE_MAX+1];
100 static void put_esc_str (const char *s)
106 int lgets (char *buf, int max, int fd)
113 if ((r=read (fd, buf+no, 1)) != 1)
116 gw_log (GW_LOG_WARN|GW_LOG_ERRNO, KERNEL_LOG, "read fail");
122 if (buf[no++] == '\n')
129 static int reopen_target (void)
133 gw_log (GW_LOG_WARN, KERNEL_LOG, "Zass free...");
134 info.zass = zass_open (info.hostname, info.port);
137 fprintf (reply_fd, "%s %s:%d\n",
138 gw_res_get (info.kernel_res, "gw.err.connect",
139 "Cannot connect to target"),
140 info.hostname, info.port);
143 v = gw_res_get (info.kernel_res, "gw.description", NULL);
147 fprintf (reply_fd, "\n");
149 fprintf (reply_fd, "%s %s:%d\n",
150 gw_res_get (info.kernel_res, "gw.msg.connect",
151 "Connection established to"),
152 info.hostname, info.port);
154 fprintf (reply_fd, "%s:\n%s\n",
155 gw_res_get (info.kernel_res, "gw.msg.databases",
156 "Available databases"),
159 fprintf (reply_fd, "%s:\n%s\n",
160 gw_res_get (info.kernel_res, "gw.msg.database",
161 "Selected databases"),
167 static struct command_word {
169 char *resource_suffix;
177 { "continue", "continue" },
178 { "status", "status" },
179 { "cancel", "cancel" },
180 { "target", "target" },
185 static int command_search (struct command_word *tab, struct ccl_token *cmd,
186 const char *resource_prefix)
190 assert (resource_prefix);
193 while (tab->default_value)
195 char *cp, command_names[60];
196 char resource_name[60];
199 sprintf (resource_name, "%s%s", resource_prefix,
200 tab->resource_suffix);
201 v = gw_res_get (info.kernel_res, resource_name, tab->default_value);
203 strcpy (command_names, v);
209 if ((split = strchr (cp, ' ')))
211 if (cmd->len == strlen(cp) &&
212 !memcmp (cmd->name, cp, cmd->len))
224 static struct error_no_struct {
227 } error_ccl_tab[] = {
229 { CCL_ERR_TERM_EXPECTED, "term.expected" },
230 { CCL_ERR_RP_EXPECTED, "rp.expected" },
231 { CCL_ERR_SETNAME_EXPECTED, "setname.expected" },
232 { CCL_ERR_OP_EXPECTED, "op.expected" },
233 { CCL_ERR_BAD_RP, "bad.rp" },
234 { CCL_ERR_UNKNOWN_QUAL, "unknown.qual" },
235 { CCL_ERR_DOUBLE_QUAL, "double.qual" },
236 { CCL_ERR_EQ_EXPECTED, "eq.expected" },
237 { CCL_ERR_BAD_RELATION, "bad.relation" },
238 { CCL_ERR_TRUNC_NOT_LEFT, "trunc.not.left" },
239 { CCL_ERR_TRUNC_NOT_BOTH, "trunc.not.both" },
240 { CCL_ERR_TRUNC_NOT_RIGHT, "trunc.not.right" },
244 static char *error_no_search (struct error_no_struct *tab, int no)
246 struct error_no_struct *p = tab;
247 while (p->resource_name)
250 return p->resource_name;
256 static int email_header (struct str_queue *sq,
257 char *from_str, char *subject_str)
261 while (str_queue_deq (sq, line_buf, LINE_MAX))
263 if (line_buf[0] == '\n')
265 if (memcmp (line_buf, "From ", 5) == 0)
266 sscanf (line_buf+4, "%s", from_str);
267 if (memcmp (line_buf, "Subject: ", 9) == 0 &&
268 sscanf (line_buf+9, "%s", subject_str+1) == 1)
269 strcpy (subject_str, line_buf+9);
274 static void help_general (void)
276 put_esc_str (gw_res_get (info.kernel_res, "gw.help.general",
277 "Commands available in this service:\n"));
280 static int exec_help (struct ccl_token *list)
282 static char *sep = "-------------------------------\\n";
287 put_esc_str (gw_res_get (info.kernel_res, "gw.help.target",
288 "target <name> - selects a given target\n"));
291 put_esc_str (gw_res_get (info.kernel_res, "gw.help.base",
292 "base <base>.. - selects databases\n"));
295 put_esc_str (gw_res_get (info.kernel_res, "gw.help.find",
296 "find <query> - performs a search request\n"));
299 put_esc_str (gw_res_get (info.kernel_res, "gw.help.show",
300 "show <spec> - retrieves and displays "
306 static void display_diag_error (int code, const char *addinfo)
310 sprintf (str, "gw.bib1.diag.%d", code);
311 fprintf (reply_fd, "%s %d:\n %s: '%s'\n",
312 gw_res_get (info.kernel_res, "gw.msg.z39errcode",
315 gw_res_get (info.kernel_res, str, ""), addinfo);
318 static int exec_find (struct ccl_token *list)
320 const struct zass_searchent *p;
321 struct gw_user_set *us;
323 struct ccl_rpn_node *rpn;
327 us = user_set_add ("Default", -1);
328 rpn = ccl_find (info.bibset, list, &error, &pos);
331 const char *v = NULL, *n;
334 fprintf (reply_fd, " %*s^ - ", pos - line_buf, " ");
336 n = error_no_search (error_ccl_tab, error);
339 sprintf (name, "gw.err.%s", n);
340 v = gw_res_get (info.kernel_res, name, NULL);
343 v = ccl_err_msg (error);
344 fprintf (reply_fd, "%s\n", v);
347 ccl_pr_tree (rpn, reply_fd);
348 fprintf (reply_fd, "\n");
350 if (!*info.database )
352 fprintf (reply_fd, "%s\n",
353 gw_res_get (info.kernel_res, "gw.err.no.database",
354 "You must select database"));
357 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Searching in database %s",
360 p = zass_search (info.zass, rpn, "Default", info.database );
363 if (p->errcode != -1)
365 display_diag_error (p->errcode, p->errstring);
368 fprintf (reply_fd, "%d %s\n", p->num,
369 gw_res_get (info.kernel_res, "gw.msg.hits", "hit(s)"));
374 static int exec_target (struct ccl_token *list)
377 if (list->kind == CCL_TOK_EOL)
380 memcpy (info.target, list->name, len);
381 info.target [len] = '\0';
384 return reopen_target ();
387 static int exec_base (struct ccl_token *list)
389 struct ccl_token *li = list;
393 if (list->kind == CCL_TOK_EOL)
395 free (info.database);
396 while (li->kind != CCL_TOK_EOL)
400 if (li->kind == CCL_TOK_COMMA)
403 info.database = malloc (len);
404 assert (info.database );
407 while (li->kind != CCL_TOK_EOL)
409 memcpy (info.database+len, li->name, li->len);
411 info.database[len++] = ',';
413 if (li->kind == CCL_TOK_COMMA)
416 info.database[len-1] = '\0';
420 struct command_word show_tab [] =
427 static void present (const char *set, int offset, int number,
428 struct ccl_token *format_token)
430 const struct zass_presentent *zp;
435 max_number = atoi (gw_res_get (info.kernel_res, "gw.max.show",
437 if (number > max_number)
439 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "present in set %s", set);
440 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "present of %d records from offset %d",
442 zp = zass_present(info.zass, (char *) set, offset, number);
448 int record_log_fd = -1;
449 const char *record_log_name;
451 record_log_name = gw_res_get (info.kernel_res, "gw.marc.log",
455 sprintf (path, "%s/%s", gw_res_get (info.kernel_res,
458 record_log_fd = open (path, O_WRONLY|O_CREAT|O_APPEND, 0666);
459 if (record_log_fd == -1)
460 gw_log (GW_LOG_WARN|GW_LOG_ERRNO, "Cannot open %s", path);
462 fprintf (reply_fd, gw_res_get (info.kernel_res,
466 fprintf (reply_fd, "\n");
467 for (i = 0, pp = zp->records; pp; pp = pp->next, i++)
471 const char *arg_ar[5];
473 fprintf (reply_fd, "--- %d/%d ---\n",
474 i+offset, offset+zp->num-1);
475 if (!gw_res_get (info.kernel_res, "gw.ignore.which", NULL))
477 if (pp->which == ZASS_REC_DIAG)
479 display_diag_error (pp->errcode, pp->errstring);
482 else if (pp->which != ZASS_REC_USMARC)
484 fprintf (reply_fd, "Unknown record kind %d\n",
489 if (record_log_fd != -1)
490 write (record_log_fd, pp->record, strlen(pp->record));
491 rec = iso2709_cvt (pp->record);
497 len = format_token->len;
498 if (len >= sizeof(format_str))
499 len = sizeof(format_str)-1;
500 memcpy (format_str, format_token->name, len);
501 format_str[len] = '\0';
503 if (info.fml && format_token &&
504 (!strcmp (format_str, "0") || !strcmp (format_str, "1")
505 || !strcmp(format_str, "2")))
508 arg_ar[1] = format_str;
509 arg_ar[2] = " \\list";
510 arg_ar[3] = marc_to_str (info.fml, rec);
512 fml_exec_call_argv (info.fml, arg_ar);
515 iso2709_display (rec, reply_fd);
517 iso2709_display (rec, reply_fd);
522 fprintf (reply_fd, "Not a MARC record\n");
524 if (record_log_fd != -1)
525 close (record_log_fd);
529 static int exec_show (struct ccl_token *list)
532 struct ccl_token *set_token = NULL;
533 struct ccl_token *format_token = NULL;
534 struct ccl_token *li = list;
535 int no_of_present = 0;
538 while (li->kind != CCL_TOK_EOL)
541 if (li->next->kind == CCL_TOK_EQ)
543 if (li->kind == CCL_TOK_SET) /* set = <name> ? */
550 modifier_no = command_search (show_tab, li, "ccl.token.");
553 fprintf (reply_fd, "Unknown modifier in show\n");
557 if (modifier_no == 1) /* f = <name> ? */
559 else if (modifier_no == 2) /* p = <name> ? */
561 if (li->kind != CCL_TOK_EOL /* p = <name> - <name> ? */
562 && li->next->kind == CCL_TOK_MINUS
563 && li->next->next != CCL_TOK_EOL)
569 fprintf (reply_fd, "%s\n", "Missing token after '='");
578 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Got set=%.*s", set_token->len,
581 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Got format=%.*s", format_token->len,
585 while (li->kind != CCL_TOK_EOL)
591 if (li->next->kind == CCL_TOK_EQ && li->kind != CCL_TOK_SET)
593 modifier_no = command_search (show_tab, li, "ccl.token.");
595 if (modifier_no == 2) /* p = <name> ? */
597 if (li->kind != CCL_TOK_EOL /* p = <name> - <name> ? */
598 && li->next->kind == CCL_TOK_MINUS
599 && li->next->next != CCL_TOK_EOL)
602 memcpy (tmp_str, li->name, len);
603 tmp_str [len] = '\0';
604 offset = atoi (tmp_str);
608 memcpy (tmp_str, li->name, len);
609 tmp_str [len] = '\0';
610 number = atoi (tmp_str) - offset + 1;
615 memcpy (tmp_str, li->name, len);
616 tmp_str [len] = '\0';
617 offset = atoi (tmp_str);
626 memcpy (tmp_str, li->name, len);
628 number = atoi (tmp_str);
632 if (offset > 0 && number > 0)
634 struct gw_user_set *us;
638 len = set_token->len;
639 memcpy (tmp_str, set_token->name, len);
641 us = user_set_search (tmp_str);
644 us = user_set_search (NULL);
645 if (us && us->hits != -1) /* proper result-set? */
647 if (offset <= us->hits)
649 if (offset+number-1 > us->hits)
650 number = us->hits - offset+1;
651 present (us->name, offset, number, format_token);
654 else if (!no_of_present) /* display error message once! */
656 fprintf (reply_fd, "%s\n",
657 gw_res_get (info.kernel_res, "gw.err.no.set",
658 "No result-set generated"));
663 if (!no_of_present) /* no records shown so far? */
665 struct gw_user_set *us;
668 us = user_set_search (NULL);
669 if (us && us->hits != -1) /* proper result-set? */
671 default_show = atoi (gw_res_get (info.kernel_res,
672 "gw.default.show", "20"));
673 if (us->hits > default_show)
674 present (us->name, 1, default_show, format_token);
675 else if (us->hits > 0)
676 present (us->name, 1, us->hits, format_token);
678 else /* display error message */
680 fprintf (reply_fd, "%s\n",
681 gw_res_get (info.kernel_res, "gw.err.no.set",
682 "No result-set generated"));
689 static int exec_command (const char *str)
691 struct ccl_token *cmd = ccl_tokenize (str);
694 if (cmd->kind != CCL_TOK_EOL &&
695 (no = command_search (command_tab, cmd, "ccl.command.")))
697 if (!info.zass && no != 9 && no != 4)
699 fprintf (reply_fd, "\n> %s\n", str);
700 if (!info.zass && (no == 1 || no == 2 || no == 3))
702 fprintf (reply_fd, "%s\n",
703 gw_res_get (info.kernel_res, "gw.err.no.target",
704 "No connection established - "
711 return exec_find (cmd->next);
713 return exec_show (cmd->next);
715 return exec_base (cmd->next);
717 return exec_help (cmd->next);
719 return exec_target (cmd->next);
721 fprintf (reply_fd, "%s\n",
722 gw_res_get (info.kernel_res, "gw.err.unimplemented",
723 "Not implemented yet"));
728 fprintf (reply_fd, "\n> %s\n", str);
729 fprintf (reply_fd, " ^ %s\n",
730 gw_res_get (info.kernel_res, "gw.err.unknown.command",
732 "Use help to see list of commands"));
737 int urp (struct str_queue *queue)
740 char subject_str[128];
742 char *reply_fname = NULL;
745 if (email_header (queue, from_str, subject_str))
747 gw_log (GW_LOG_WARN, KERNEL_LOG, "No message body");
750 tty_init (stdout, 40, 70);
753 reply_fname = tempnam (gw_res_get (info.kernel_res,
754 "gw.reply.tmp.dir", NULL),
755 gw_res_get (info.kernel_res,
756 "gw.reply.tmp.prefix", "gwr"));
758 reply_fd = fopen (reply_fname, "w");
761 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, KERNEL_LOG, "Cannot create %s",
765 tty_init (reply_fd, 0, 0);
766 fprintf (reply_fd, "From: %s\n",
767 gw_res_get (info.kernel_res, "gw.msg.from","Email-gateway"));
768 fprintf (reply_fd, "Subject: ");
770 fprintf (reply_fd, "Z39.50 Re: %s", subject_str);
772 fprintf (reply_fd, "%s\n", gw_res_get (info.kernel_res,
775 fprintf (reply_fd, "\n");
776 gw_log (GW_LOG_ACCT, KERNEL_LOG, "User start %s", from_str);
779 gw_log (GW_LOG_WARN, KERNEL_LOG, "No From in email header");
780 fprintf (reply_fd, "%s\n", gw_res_get (info.kernel_res, "gw.msg.greeting",
781 "Email->Z39.50 gateway"));
782 while (str_queue_deq (queue, line_buf, LINE_MAX))
784 if (line_buf[0] == '\n')
787 while (str_queue_deq (queue, 0, 0))
793 if ((cp = strchr (line_buf, '\n')))
795 gw_log (GW_LOG_ACCT, KERNEL_LOG, "cmd: %s", line_buf);
796 ccl_token_and = gw_res_get (info.kernel_res, "ccl.token.and", "and");
797 ccl_token_or = gw_res_get (info.kernel_res, "ccl.token.or", "or");
798 ccl_token_not = gw_res_get (info.kernel_res, "ccl.token.not", "not");
799 ccl_token_set = gw_res_get (info.kernel_res, "ccl.token.set", "set");
800 if (isalpha (line_buf[0]))
801 exec_command (line_buf);
806 fprintf (reply_fd, "%s\n", gw_res_get (info.kernel_res,
817 assert (reply_fname);
821 mta = gw_res_get (info.kernel_res, "gw.reply.mta",
822 "/usr/lib/sendmail");
823 sprintf (cmd, "%s %s < %s", mta, from_str, reply_fname);
825 mta_code = system (cmd);
827 gw_log (GW_LOG_FATAL, KERNEL_LOG,
828 "Reply '%s' got exit code %d", cmd, mta_code);
829 unlink (reply_fname);
830 gw_log (GW_LOG_ACCT, KERNEL_LOG, "User end %s", from_str);