5 * Revision 1.23 1995/03/28 08:01:28 adam
6 * FIFO existence is used to test for a running kernel.
8 * Revision 1.22 1995/03/27 12:51:05 adam
9 * New log level in use: GW_LOG_ERRNO.
11 * Revision 1.21 1995/03/27 08:24:04 adam
12 * First use of gip interface and gw-db.
13 * First work on eti program.
15 * Revision 1.20 1995/03/03 17:19:17 adam
16 * Smarter presentation. Bug fix in email header interpretation.
18 * Revision 1.19 1995/03/02 09:32:11 adam
19 * New presentation formats. f0=full, f1=brief, f2=mid
21 * Revision 1.18 1995/03/01 14:32:26 adam
22 * Better diagnostics. Default is, that only one database selected when
25 * Revision 1.17 1995/02/28 13:16:26 adam
26 * Configurable From: added.
28 * Revision 1.16 1995/02/23 10:08:20 adam
29 * Added logging of all user commands.
31 * Revision 1.15 1995/02/23 08:32:17 adam
34 * Revision 1.13 1995/02/22 16:54:42 adam
35 * Qualifiers of LOC target updated. More logging messages.
37 * Revision 1.12 1995/02/22 15:51:51 adam
38 * Bug fix: swap of parameter number and offset in function present.
40 * Revision 1.11 1995/02/22 15:22:33 adam
41 * Much more checking of run-time state. Show command never retrieves
42 * more records than indicated by the previous search request. Help
43 * command available. The maximum number of records retrieved can be
46 * Revision 1.10 1995/02/22 08:51:35 adam
47 * Output function can be customized in fml, which is used to print
48 * the reply to reply_fd.
50 * Revision 1.9 1995/02/21 17:46:21 adam
53 * Revision 1.8 1995/02/21 12:12:00 adam
54 * Diagnostic record with error info. observed.
56 * Revision 1.7 1995/02/20 21:16:20 adam
57 * FML support. Bug fixes. Profile for drewdb.
59 * Revision 1.6 1995/02/17 14:41:14 quinn
60 * Added simple display of records.
62 * Revision 1.5 1995/02/17 14:22:13 adam
63 * First steps of CCL show command. Not finished yet.
65 * Revision 1.4 1995/02/17 09:08:36 adam
66 * Reply with subject. CCL base command implemented.
68 * Revision 1.3 1995/02/16 18:35:09 adam
69 * First use of Zdist library. Search requests are supported.
70 * Present requests are not supported yet.
72 * Revision 1.2 1995/02/16 13:21:00 adam
73 * Organization of resource files for targets and conversion
74 * language implemented.
76 * Revision 1.1 1995/02/15 17:45:30 adam
77 * First version of email gateway kernel. Email requests are read
78 * from stdin. The output is transferred to an MTA if 'From' is
79 * found in the header - or stdout if absent. No Z39.50 client is used.
95 static void put_esc_str (const char *s)
101 int lgets (char *buf, int max, int fd)
108 if ((r=read (fd, buf+no, 1)) != 1)
111 gw_log (GW_LOG_WARN|GW_LOG_ERRNO, KERNEL_LOG, "read fail");
117 if (buf[no++] == '\n')
124 static int reopen_target (void)
128 gw_log (GW_LOG_WARN, KERNEL_LOG, "Zass free...");
129 info.zass = zass_open (info.hostname, info.port);
132 fprintf (reply_fd, "%s %s:%d\n",
133 gw_res_get (info.kernel_res, "gw.err.connect",
134 "Cannot connect to target"),
135 info.hostname, info.port);
138 v = gw_res_get (info.kernel_res, "gw.description", NULL);
142 fprintf (reply_fd, "\n");
144 fprintf (reply_fd, "%s %s:%d\n",
145 gw_res_get (info.kernel_res, "gw.msg.connect",
146 "Connection established to"),
147 info.hostname, info.port);
149 fprintf (reply_fd, "%s:\n%s\n",
150 gw_res_get (info.kernel_res, "gw.msg.databases",
151 "Available databases"),
154 fprintf (reply_fd, "%s:\n%s\n",
155 gw_res_get (info.kernel_res, "gw.msg.database",
156 "Selected databases"),
161 static char line_buf[LINE_MAX+1];
163 static struct command_word {
165 char *resource_suffix;
173 { "continue", "continue" },
174 { "status", "status" },
175 { "cancel", "cancel" },
176 { "target", "target" },
181 static int command_search (struct command_word *tab, struct ccl_token *cmd,
182 const char *resource_prefix)
186 assert (resource_prefix);
189 while (tab->default_value)
191 char *cp, command_names[60];
192 char resource_name[60];
195 sprintf (resource_name, "%s%s", resource_prefix,
196 tab->resource_suffix);
197 v = gw_res_get (info.kernel_res, resource_name, tab->default_value);
199 strcpy (command_names, v);
205 if ((split = strchr (cp, ' ')))
207 if (cmd->len == strlen(cp) &&
208 !memcmp (cmd->name, cp, cmd->len))
220 static struct error_no_struct {
223 } error_ccl_tab[] = {
225 { CCL_ERR_TERM_EXPECTED, "term.expected" },
226 { CCL_ERR_RP_EXPECTED, "rp.expected" },
227 { CCL_ERR_SETNAME_EXPECTED, "setname.expected" },
228 { CCL_ERR_OP_EXPECTED, "op.expected" },
229 { CCL_ERR_BAD_RP, "bad.rp" },
230 { CCL_ERR_UNKNOWN_QUAL, "unknown.qual" },
231 { CCL_ERR_DOUBLE_QUAL, "double.qual" },
232 { CCL_ERR_EQ_EXPECTED, "eq.expected" },
233 { CCL_ERR_BAD_RELATION, "bad.relation" },
234 { CCL_ERR_TRUNC_NOT_LEFT, "trunc.not.left" },
235 { CCL_ERR_TRUNC_NOT_BOTH, "trunc.not.both" },
236 { CCL_ERR_TRUNC_NOT_RIGHT, "trunc.not.right" },
240 static char *error_no_search (struct error_no_struct *tab, int no)
242 struct error_no_struct *p = tab;
243 while (p->resource_name)
246 return p->resource_name;
252 static int email_header (int fd, char *from_str, char *subject_str)
256 while (lgets (line_buf, LINE_MAX, fd))
258 if (line_buf[0] == '\n')
260 if (strncmp (line_buf, "From ", 5) == 0)
261 sscanf (line_buf+4, "%s", from_str);
262 if (strncmp (line_buf, "Subject: ", 9) == 0 &&
263 sscanf (line_buf+9, "%s", subject_str+1) == 1)
264 strcpy (subject_str, line_buf+9);
269 static void help_general (void)
271 put_esc_str (gw_res_get (info.kernel_res, "gw.help.general",
272 "Commands available in this service:\n"));
275 static int exec_help (struct ccl_token *list)
277 static char *sep = "-------------------------------\\n";
282 put_esc_str (gw_res_get (info.kernel_res, "gw.help.target",
283 "target <name> - selects a given target\n"));
286 put_esc_str (gw_res_get (info.kernel_res, "gw.help.base",
287 "base <base>.. - selects databases\n"));
290 put_esc_str (gw_res_get (info.kernel_res, "gw.help.find",
291 "find <query> - performs a search request\n"));
294 put_esc_str (gw_res_get (info.kernel_res, "gw.help.show",
295 "show <spec> - retrieves and displays "
301 static void display_diag_error (int code, const char *addinfo)
305 sprintf (str, "gw.bib1.diag.%d", code);
306 fprintf (reply_fd, "%s %d:\n %s: '%s'\n",
307 gw_res_get (info.kernel_res, "gw.msg.z39errcode",
310 gw_res_get (info.kernel_res, str, ""), addinfo);
313 static int exec_find (struct ccl_token *list)
315 const struct zass_searchent *p;
316 struct gw_user_set *us;
318 struct ccl_rpn_node *rpn;
322 us = user_set_add ("Default", -1);
323 rpn = ccl_find (info.bibset, list, &error, &pos);
326 const char *v = NULL, *n;
329 fprintf (reply_fd, " %*s^ - ", pos - line_buf, " ");
331 n = error_no_search (error_ccl_tab, error);
334 sprintf (name, "gw.err.%s", n);
335 v = gw_res_get (info.kernel_res, name, NULL);
338 v = ccl_err_msg (error);
339 fprintf (reply_fd, "%s\n", v);
342 ccl_pr_tree (rpn, reply_fd);
343 fprintf (reply_fd, "\n");
345 if (!*info.database )
347 fprintf (reply_fd, "%s\n",
348 gw_res_get (info.kernel_res, "gw.err.no.database",
349 "You must select database"));
352 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Searching in database %s",
355 p = zass_search (info.zass, rpn, "Default", info.database );
358 if (p->errcode != -1)
360 display_diag_error (p->errcode, p->errstring);
363 fprintf (reply_fd, "%d %s\n", p->num,
364 gw_res_get (info.kernel_res, "gw.msg.hits", "hit(s)"));
369 static int exec_target (struct ccl_token *list)
372 if (list->kind == CCL_TOK_EOL)
375 memcpy (info.target, list->name, len);
376 info.target [len] = '\0';
379 return reopen_target ();
382 static int exec_base (struct ccl_token *list)
384 struct ccl_token *li = list;
388 if (list->kind == CCL_TOK_EOL)
390 free (info.database);
391 while (li->kind != CCL_TOK_EOL)
395 if (li->kind == CCL_TOK_COMMA)
398 info.database = malloc (len);
399 assert (info.database );
402 while (li->kind != CCL_TOK_EOL)
404 memcpy (info.database+len, li->name, li->len);
406 info.database[len++] = ',';
408 if (li->kind == CCL_TOK_COMMA)
411 info.database[len-1] = '\0';
415 struct command_word show_tab [] =
422 static void present (const char *set, int offset, int number,
423 struct ccl_token *format_token)
425 const struct zass_presentent *zp;
430 max_number = atoi (gw_res_get (info.kernel_res, "gw.max.show",
432 if (number > max_number)
434 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "present in set %s", set);
435 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "present of %d records from offset %d",
437 zp = zass_present(info.zass, (char *) set, offset, number);
443 int record_log_fd = -1;
444 const char *record_log_name;
446 record_log_name = gw_res_get (info.kernel_res, "gw.marc.log",
450 sprintf (path, "%s/%s", gw_res_get (info.kernel_res,
453 record_log_fd = open (path, O_WRONLY|O_CREAT|O_APPEND, 0666);
454 if (record_log_fd == -1)
455 gw_log (GW_LOG_WARN|GW_LOG_ERRNO, "Cannot open %s", path);
457 fprintf (reply_fd, gw_res_get (info.kernel_res,
461 fprintf (reply_fd, "\n");
462 for (i = 0, pp = zp->records; pp; pp = pp->next, i++)
466 const char *arg_ar[5];
468 fprintf (reply_fd, "--- %d/%d ---\n",
469 i+offset, offset+zp->num-1);
470 if (!gw_res_get (info.kernel_res, "gw.ignore.which", NULL))
472 if (pp->which == ZASS_REC_DIAG)
474 display_diag_error (pp->errcode, pp->errstring);
477 else if (pp->which != ZASS_REC_USMARC)
479 fprintf (reply_fd, "Unknown record kind %d\n",
484 if (record_log_fd != -1)
485 write (record_log_fd, pp->record, strlen(pp->record));
486 rec = iso2709_cvt (pp->record);
492 len = format_token->len;
493 if (len >= sizeof(format_str))
494 len = sizeof(format_str)-1;
495 memcpy (format_str, format_token->name, len);
496 format_str[len] = '\0';
498 if (info.fml && format_token &&
499 (!strcmp (format_str, "0") || !strcmp (format_str, "1")
500 || !strcmp(format_str, "2")))
503 arg_ar[1] = format_str;
504 arg_ar[2] = " \\list";
505 arg_ar[3] = marc_to_str (info.fml, rec);
507 fml_exec_call_argv (info.fml, arg_ar);
510 iso2709_display (rec, reply_fd);
512 iso2709_display (rec, reply_fd);
517 fprintf (reply_fd, "Not a MARC record\n");
519 if (record_log_fd != -1)
520 close (record_log_fd);
524 static int exec_show (struct ccl_token *list)
527 struct ccl_token *set_token = NULL;
528 struct ccl_token *format_token = NULL;
529 struct ccl_token *li = list;
530 int no_of_present = 0;
533 while (li->kind != CCL_TOK_EOL)
536 if (li->next->kind == CCL_TOK_EQ)
538 if (li->kind == CCL_TOK_SET) /* set = <name> ? */
545 modifier_no = command_search (show_tab, li, "ccl.token.");
548 fprintf (reply_fd, "Unknown modifier in show\n");
552 if (modifier_no == 1) /* f = <name> ? */
554 else if (modifier_no == 2) /* p = <name> ? */
556 if (li->kind != CCL_TOK_EOL /* p = <name> - <name> ? */
557 && li->next->kind == CCL_TOK_MINUS
558 && li->next->next != CCL_TOK_EOL)
564 fprintf (reply_fd, "%s\n", "Missing token after '='");
573 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Got set=%.*s", set_token->len,
576 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Got format=%.*s", format_token->len,
580 while (li->kind != CCL_TOK_EOL)
586 if (li->next->kind == CCL_TOK_EQ && li->kind != CCL_TOK_SET)
588 modifier_no = command_search (show_tab, li, "ccl.token.");
590 if (modifier_no == 2) /* p = <name> ? */
592 if (li->kind != CCL_TOK_EOL /* p = <name> - <name> ? */
593 && li->next->kind == CCL_TOK_MINUS
594 && li->next->next != CCL_TOK_EOL)
597 memcpy (tmp_str, li->name, len);
598 tmp_str [len] = '\0';
599 offset = atoi (tmp_str);
603 memcpy (tmp_str, li->name, len);
604 tmp_str [len] = '\0';
605 number = atoi (tmp_str) - offset + 1;
610 memcpy (tmp_str, li->name, len);
611 tmp_str [len] = '\0';
612 offset = atoi (tmp_str);
621 memcpy (tmp_str, li->name, len);
623 number = atoi (tmp_str);
627 if (offset > 0 && number > 0)
629 struct gw_user_set *us;
633 len = set_token->len;
634 memcpy (tmp_str, set_token->name, len);
636 us = user_set_search (tmp_str);
639 us = user_set_search (NULL);
640 if (us && us->hits != -1) /* proper result-set? */
642 if (offset <= us->hits)
644 if (offset+number-1 > us->hits)
645 number = us->hits - offset+1;
646 present (us->name, offset, number, format_token);
649 else if (!no_of_present) /* display error message once! */
651 fprintf (reply_fd, "%s\n",
652 gw_res_get (info.kernel_res, "gw.err.no.set",
653 "No result-set generated"));
658 if (!no_of_present) /* no records shown so far? */
660 struct gw_user_set *us;
663 us = user_set_search (NULL);
664 if (us && us->hits != -1) /* proper result-set? */
666 default_show = atoi (gw_res_get (info.kernel_res,
667 "gw.default.show", "20"));
668 if (us->hits > default_show)
669 present (us->name, 1, default_show, format_token);
670 else if (us->hits > 0)
671 present (us->name, 1, us->hits, format_token);
673 else /* display error message */
675 fprintf (reply_fd, "%s\n",
676 gw_res_get (info.kernel_res, "gw.err.no.set",
677 "No result-set generated"));
684 static int exec_command (const char *str)
686 struct ccl_token *cmd = ccl_tokenize (str);
689 if (cmd->kind != CCL_TOK_EOL &&
690 (no = command_search (command_tab, cmd, "ccl.command.")))
692 if (!info.zass && no != 9 && no != 4)
694 fprintf (reply_fd, "\n> %s\n", str);
695 if (!info.zass && (no == 1 || no == 2 || no == 3))
697 fprintf (reply_fd, "%s\n",
698 gw_res_get (info.kernel_res, "gw.err.no.target",
699 "No connection established - "
706 return exec_find (cmd->next);
708 return exec_show (cmd->next);
710 return exec_base (cmd->next);
712 return exec_help (cmd->next);
714 return exec_target (cmd->next);
716 fprintf (reply_fd, "%s\n",
717 gw_res_get (info.kernel_res, "gw.err.unimplemented",
718 "Not implemented yet"));
723 fprintf (reply_fd, "\n> %s\n", str);
724 fprintf (reply_fd, " ^ %s\n",
725 gw_res_get (info.kernel_res, "gw.err.unknown.command",
727 "Use help to see list of commands"));
735 char subject_str[128];
737 char *reply_fname = NULL;
739 if (email_header (fd, from_str, subject_str))
741 gw_log (GW_LOG_WARN, KERNEL_LOG, "No message body");
744 tty_init (stdout, 40, 70);
747 reply_fname = tempnam (gw_res_get (info.kernel_res,
748 "gw.reply.tmp.dir", NULL),
749 gw_res_get (info.kernel_res,
750 "gw.reply.tmp.prefix", "gwr"));
752 reply_fd = fopen (reply_fname, "w");
755 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, KERNEL_LOG, "Cannot create %s",
759 tty_init (reply_fd, 0, 0);
760 fprintf (reply_fd, "From: %s\n",
761 gw_res_get (info.kernel_res, "gw.msg.from","Email-gateway"));
762 fprintf (reply_fd, "Subject: ");
764 fprintf (reply_fd, "Z39.50 Re: %s", subject_str);
766 fprintf (reply_fd, "%s\n", gw_res_get (info.kernel_res,
769 fprintf (reply_fd, "\n");
770 gw_log (GW_LOG_ACCT, KERNEL_LOG, "User start %s", from_str);
773 gw_log (GW_LOG_WARN, KERNEL_LOG, "No From in email header");
774 fprintf (reply_fd, "%s\n", gw_res_get (info.kernel_res, "gw.msg.greeting",
775 "Email->Z39.50 gateway"));
776 while (lgets (line_buf, LINE_MAX, fd))
780 if (line_buf[0] == '\n')
783 while (lgets (line_buf, LINE_MAX, fd))
789 if ((cp = strchr (line_buf, '\n')))
791 gw_log (GW_LOG_ACCT, KERNEL_LOG, "cmd: %s", line_buf);
792 ccl_token_and = gw_res_get (info.kernel_res, "ccl.token.and", "and");
793 ccl_token_or = gw_res_get (info.kernel_res, "ccl.token.or", "or");
794 ccl_token_not = gw_res_get (info.kernel_res, "ccl.token.not", "not");
795 ccl_token_set = gw_res_get (info.kernel_res, "ccl.token.set", "set");
796 if (isalpha (line_buf[0]))
797 exec_command (line_buf);
802 fprintf (reply_fd, "%s\n", gw_res_get (info.kernel_res,
813 assert (reply_fname);
817 mta = gw_res_get (info.kernel_res, "gw.reply.mta",
818 "/usr/lib/sendmail");
819 sprintf (cmd, "%s %s < %s", mta, from_str, reply_fname);
821 mta_code = system (cmd);
823 gw_log (GW_LOG_FATAL, KERNEL_LOG,
824 "Reply '%s' got exit code %d", cmd, mta_code);
825 unlink (reply_fname);
826 gw_log (GW_LOG_ACCT, KERNEL_LOG, "User end %s", from_str);