5 * Revision 1.22 1995/03/27 12:51:05 adam
6 * New log level in use: GW_LOG_ERRNO.
8 * Revision 1.21 1995/03/27 08:24:04 adam
9 * First use of gip interface and gw-db.
10 * First work on eti program.
12 * Revision 1.20 1995/03/03 17:19:17 adam
13 * Smarter presentation. Bug fix in email header interpretation.
15 * Revision 1.19 1995/03/02 09:32:11 adam
16 * New presentation formats. f0=full, f1=brief, f2=mid
18 * Revision 1.18 1995/03/01 14:32:26 adam
19 * Better diagnostics. Default is, that only one database selected when
22 * Revision 1.17 1995/02/28 13:16:26 adam
23 * Configurable From: added.
25 * Revision 1.16 1995/02/23 10:08:20 adam
26 * Added logging of all user commands.
28 * Revision 1.15 1995/02/23 08:32:17 adam
31 * Revision 1.13 1995/02/22 16:54:42 adam
32 * Qualifiers of LOC target updated. More logging messages.
34 * Revision 1.12 1995/02/22 15:51:51 adam
35 * Bug fix: swap of parameter number and offset in function present.
37 * Revision 1.11 1995/02/22 15:22:33 adam
38 * Much more checking of run-time state. Show command never retrieves
39 * more records than indicated by the previous search request. Help
40 * command available. The maximum number of records retrieved can be
43 * Revision 1.10 1995/02/22 08:51:35 adam
44 * Output function can be customized in fml, which is used to print
45 * the reply to reply_fd.
47 * Revision 1.9 1995/02/21 17:46:21 adam
50 * Revision 1.8 1995/02/21 12:12:00 adam
51 * Diagnostic record with error info. observed.
53 * Revision 1.7 1995/02/20 21:16:20 adam
54 * FML support. Bug fixes. Profile for drewdb.
56 * Revision 1.6 1995/02/17 14:41:14 quinn
57 * Added simple display of records.
59 * Revision 1.5 1995/02/17 14:22:13 adam
60 * First steps of CCL show command. Not finished yet.
62 * Revision 1.4 1995/02/17 09:08:36 adam
63 * Reply with subject. CCL base command implemented.
65 * Revision 1.3 1995/02/16 18:35:09 adam
66 * First use of Zdist library. Search requests are supported.
67 * Present requests are not supported yet.
69 * Revision 1.2 1995/02/16 13:21:00 adam
70 * Organization of resource files for targets and conversion
71 * language implemented.
73 * Revision 1.1 1995/02/15 17:45:30 adam
74 * First version of email gateway kernel. Email requests are read
75 * from stdin. The output is transferred to an MTA if 'From' is
76 * found in the header - or stdout if absent. No Z39.50 client is used.
92 static void put_esc_str (const char *s)
98 int lgets (char *buf, int max, int fd)
105 if ((r=read (fd, buf+no, 1)) != 1)
108 gw_log (GW_LOG_WARN|GW_LOG_ERRNO, KERNEL_LOG, "read fail");
114 if (buf[no++] == '\n')
121 static int reopen_target (void)
125 gw_log (GW_LOG_WARN, KERNEL_LOG, "Zass free...");
126 info.zass = zass_open (info.hostname, info.port);
129 fprintf (reply_fd, "%s %s:%d\n",
130 gw_res_get (info.kernel_res, "gw.err.connect",
131 "Cannot connect to target"),
132 info.hostname, info.port);
135 v = gw_res_get (info.kernel_res, "gw.description", NULL);
139 fprintf (reply_fd, "\n");
141 fprintf (reply_fd, "%s %s:%d\n",
142 gw_res_get (info.kernel_res, "gw.msg.connect",
143 "Connection established to"),
144 info.hostname, info.port);
146 fprintf (reply_fd, "%s:\n%s\n",
147 gw_res_get (info.kernel_res, "gw.msg.databases",
148 "Available databases"),
151 fprintf (reply_fd, "%s:\n%s\n",
152 gw_res_get (info.kernel_res, "gw.msg.database",
153 "Selected databases"),
158 static char line_buf[LINE_MAX+1];
160 static struct command_word {
162 char *resource_suffix;
170 { "continue", "continue" },
171 { "status", "status" },
172 { "cancel", "cancel" },
173 { "target", "target" },
178 static int command_search (struct command_word *tab, struct ccl_token *cmd,
179 const char *resource_prefix)
183 assert (resource_prefix);
186 while (tab->default_value)
188 char *cp, command_names[60];
189 char resource_name[60];
192 sprintf (resource_name, "%s%s", resource_prefix,
193 tab->resource_suffix);
194 v = gw_res_get (info.kernel_res, resource_name, tab->default_value);
196 strcpy (command_names, v);
202 if ((split = strchr (cp, ' ')))
204 if (cmd->len == strlen(cp) &&
205 !memcmp (cmd->name, cp, cmd->len))
217 static struct error_no_struct {
220 } error_ccl_tab[] = {
222 { CCL_ERR_TERM_EXPECTED, "term.expected" },
223 { CCL_ERR_RP_EXPECTED, "rp.expected" },
224 { CCL_ERR_SETNAME_EXPECTED, "setname.expected" },
225 { CCL_ERR_OP_EXPECTED, "op.expected" },
226 { CCL_ERR_BAD_RP, "bad.rp" },
227 { CCL_ERR_UNKNOWN_QUAL, "unknown.qual" },
228 { CCL_ERR_DOUBLE_QUAL, "double.qual" },
229 { CCL_ERR_EQ_EXPECTED, "eq.expected" },
230 { CCL_ERR_BAD_RELATION, "bad.relation" },
231 { CCL_ERR_TRUNC_NOT_LEFT, "trunc.not.left" },
232 { CCL_ERR_TRUNC_NOT_BOTH, "trunc.not.both" },
233 { CCL_ERR_TRUNC_NOT_RIGHT, "trunc.not.right" },
237 static char *error_no_search (struct error_no_struct *tab, int no)
239 struct error_no_struct *p = tab;
240 while (p->resource_name)
243 return p->resource_name;
249 static int email_header (int fd, char *from_str, char *subject_str)
253 while (lgets (line_buf, LINE_MAX, fd))
255 if (line_buf[0] == '\n')
257 if (strncmp (line_buf, "From ", 5) == 0)
258 sscanf (line_buf+4, "%s", from_str);
259 if (strncmp (line_buf, "Subject: ", 9) == 0 &&
260 sscanf (line_buf+9, "%s", subject_str+1) == 1)
261 strcpy (subject_str, line_buf+9);
266 static void help_general (void)
268 put_esc_str (gw_res_get (info.kernel_res, "gw.help.general",
269 "Commands available in this service:\n"));
272 static int exec_help (struct ccl_token *list)
274 static char *sep = "-------------------------------\\n";
279 put_esc_str (gw_res_get (info.kernel_res, "gw.help.target",
280 "target <name> - selects a given target\n"));
283 put_esc_str (gw_res_get (info.kernel_res, "gw.help.base",
284 "base <base>.. - selects databases\n"));
287 put_esc_str (gw_res_get (info.kernel_res, "gw.help.find",
288 "find <query> - performs a search request\n"));
291 put_esc_str (gw_res_get (info.kernel_res, "gw.help.show",
292 "show <spec> - retrieves and displays "
298 static void display_diag_error (int code, const char *addinfo)
302 sprintf (str, "gw.bib1.diag.%d", code);
303 fprintf (reply_fd, "%s %d:\n %s: '%s'\n",
304 gw_res_get (info.kernel_res, "gw.msg.z39errcode",
307 gw_res_get (info.kernel_res, str, ""), addinfo);
310 static int exec_find (struct ccl_token *list)
312 const struct zass_searchent *p;
313 struct gw_user_set *us;
315 struct ccl_rpn_node *rpn;
319 us = user_set_add ("Default", -1);
320 rpn = ccl_find (info.bibset, list, &error, &pos);
323 const char *v = NULL, *n;
326 fprintf (reply_fd, " %*s^ - ", pos - line_buf, " ");
328 n = error_no_search (error_ccl_tab, error);
331 sprintf (name, "gw.err.%s", n);
332 v = gw_res_get (info.kernel_res, name, NULL);
335 v = ccl_err_msg (error);
336 fprintf (reply_fd, "%s\n", v);
339 ccl_pr_tree (rpn, reply_fd);
340 fprintf (reply_fd, "\n");
342 if (!*info.database )
344 fprintf (reply_fd, "%s\n",
345 gw_res_get (info.kernel_res, "gw.err.no.database",
346 "You must select database"));
349 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Searching in database %s",
352 p = zass_search (info.zass, rpn, "Default", info.database );
355 if (p->errcode != -1)
357 display_diag_error (p->errcode, p->errstring);
360 fprintf (reply_fd, "%d %s\n", p->num,
361 gw_res_get (info.kernel_res, "gw.msg.hits", "hit(s)"));
366 static int exec_target (struct ccl_token *list)
369 if (list->kind == CCL_TOK_EOL)
372 memcpy (info.target, list->name, len);
373 info.target [len] = '\0';
376 return reopen_target ();
379 static int exec_base (struct ccl_token *list)
381 struct ccl_token *li = list;
385 if (list->kind == CCL_TOK_EOL)
387 free (info.database);
388 while (li->kind != CCL_TOK_EOL)
392 if (li->kind == CCL_TOK_COMMA)
395 info.database = malloc (len);
396 assert (info.database );
399 while (li->kind != CCL_TOK_EOL)
401 memcpy (info.database+len, li->name, li->len);
403 info.database[len++] = ',';
405 if (li->kind == CCL_TOK_COMMA)
408 info.database[len-1] = '\0';
412 struct command_word show_tab [] =
419 static void present (const char *set, int offset, int number,
420 struct ccl_token *format_token)
422 const struct zass_presentent *zp;
427 max_number = atoi (gw_res_get (info.kernel_res, "gw.max.show",
429 if (number > max_number)
431 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "present in set %s", set);
432 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "present of %d records from offset %d",
434 zp = zass_present(info.zass, (char *) set, offset, number);
440 int record_log_fd = -1;
441 const char *record_log_name;
443 record_log_name = gw_res_get (info.kernel_res, "gw.marc.log",
447 sprintf (path, "%s/%s", gw_res_get (info.kernel_res,
450 record_log_fd = open (path, O_WRONLY|O_CREAT|O_APPEND, 0666);
451 if (record_log_fd == -1)
452 gw_log (GW_LOG_WARN|GW_LOG_ERRNO, "Cannot open %s", path);
454 fprintf (reply_fd, gw_res_get (info.kernel_res,
458 fprintf (reply_fd, "\n");
459 for (i = 0, pp = zp->records; pp; pp = pp->next, i++)
463 const char *arg_ar[5];
465 fprintf (reply_fd, "--- %d/%d ---\n",
466 i+offset, offset+zp->num-1);
467 if (!gw_res_get (info.kernel_res, "gw.ignore.which", NULL))
469 if (pp->which == ZASS_REC_DIAG)
471 display_diag_error (pp->errcode, pp->errstring);
474 else if (pp->which != ZASS_REC_USMARC)
476 fprintf (reply_fd, "Unknown record kind %d\n",
481 if (record_log_fd != -1)
482 write (record_log_fd, pp->record, strlen(pp->record));
483 rec = iso2709_cvt (pp->record);
489 len = format_token->len;
490 if (len >= sizeof(format_str))
491 len = sizeof(format_str)-1;
492 memcpy (format_str, format_token->name, len);
493 format_str[len] = '\0';
495 if (info.fml && format_token &&
496 (!strcmp (format_str, "0") || !strcmp (format_str, "1")
497 || !strcmp(format_str, "2")))
500 arg_ar[1] = format_str;
501 arg_ar[2] = " \\list";
502 arg_ar[3] = marc_to_str (info.fml, rec);
504 fml_exec_call_argv (info.fml, arg_ar);
507 iso2709_display (rec, reply_fd);
509 iso2709_display (rec, reply_fd);
514 fprintf (reply_fd, "Not a MARC record\n");
516 if (record_log_fd != -1)
517 close (record_log_fd);
521 static int exec_show (struct ccl_token *list)
524 struct ccl_token *set_token = NULL;
525 struct ccl_token *format_token = NULL;
526 struct ccl_token *li = list;
527 int no_of_present = 0;
530 while (li->kind != CCL_TOK_EOL)
533 if (li->next->kind == CCL_TOK_EQ)
535 if (li->kind == CCL_TOK_SET) /* set = <name> ? */
542 modifier_no = command_search (show_tab, li, "ccl.token.");
545 fprintf (reply_fd, "Unknown modifier in show\n");
549 if (modifier_no == 1) /* f = <name> ? */
551 else if (modifier_no == 2) /* p = <name> ? */
553 if (li->kind != CCL_TOK_EOL /* p = <name> - <name> ? */
554 && li->next->kind == CCL_TOK_MINUS
555 && li->next->next != CCL_TOK_EOL)
561 fprintf (reply_fd, "%s\n", "Missing token after '='");
570 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Got set=%.*s", set_token->len,
573 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Got format=%.*s", format_token->len,
577 while (li->kind != CCL_TOK_EOL)
583 if (li->next->kind == CCL_TOK_EQ && li->kind != CCL_TOK_SET)
585 modifier_no = command_search (show_tab, li, "ccl.token.");
587 if (modifier_no == 2) /* p = <name> ? */
589 if (li->kind != CCL_TOK_EOL /* p = <name> - <name> ? */
590 && li->next->kind == CCL_TOK_MINUS
591 && li->next->next != CCL_TOK_EOL)
594 memcpy (tmp_str, li->name, len);
595 tmp_str [len] = '\0';
596 offset = atoi (tmp_str);
600 memcpy (tmp_str, li->name, len);
601 tmp_str [len] = '\0';
602 number = atoi (tmp_str) - offset + 1;
607 memcpy (tmp_str, li->name, len);
608 tmp_str [len] = '\0';
609 offset = atoi (tmp_str);
618 memcpy (tmp_str, li->name, len);
620 number = atoi (tmp_str);
624 if (offset > 0 && number > 0)
626 struct gw_user_set *us;
630 len = set_token->len;
631 memcpy (tmp_str, set_token->name, len);
633 us = user_set_search (tmp_str);
636 us = user_set_search (NULL);
637 if (us && us->hits != -1) /* proper result-set? */
639 if (offset <= us->hits)
641 if (offset+number-1 > us->hits)
642 number = us->hits - offset+1;
643 present (us->name, offset, number, format_token);
646 else if (!no_of_present) /* display error message once! */
648 fprintf (reply_fd, "%s\n",
649 gw_res_get (info.kernel_res, "gw.err.no.set",
650 "No result-set generated"));
655 if (!no_of_present) /* no records shown so far? */
657 struct gw_user_set *us;
660 us = user_set_search (NULL);
661 if (us && us->hits != -1) /* proper result-set? */
663 default_show = atoi (gw_res_get (info.kernel_res,
664 "gw.default.show", "20"));
665 if (us->hits > default_show)
666 present (us->name, 1, default_show, format_token);
667 else if (us->hits > 0)
668 present (us->name, 1, us->hits, format_token);
670 else /* display error message */
672 fprintf (reply_fd, "%s\n",
673 gw_res_get (info.kernel_res, "gw.err.no.set",
674 "No result-set generated"));
681 static int exec_command (const char *str)
683 struct ccl_token *cmd = ccl_tokenize (str);
686 if (cmd->kind != CCL_TOK_EOL &&
687 (no = command_search (command_tab, cmd, "ccl.command.")))
689 if (!info.zass && no != 9 && no != 4)
691 fprintf (reply_fd, "\n> %s\n", str);
692 if (!info.zass && (no == 1 || no == 2 || no == 3))
694 fprintf (reply_fd, "%s\n",
695 gw_res_get (info.kernel_res, "gw.err.no.target",
696 "No connection established - "
703 return exec_find (cmd->next);
705 return exec_show (cmd->next);
707 return exec_base (cmd->next);
709 return exec_help (cmd->next);
711 return exec_target (cmd->next);
713 fprintf (reply_fd, "%s\n",
714 gw_res_get (info.kernel_res, "gw.err.unimplemented",
715 "Not implemented yet"));
720 fprintf (reply_fd, "\n> %s\n", str);
721 fprintf (reply_fd, " ^ %s\n",
722 gw_res_get (info.kernel_res, "gw.err.unknown.command",
724 "Use help to see list of commands"));
732 char subject_str[128];
734 char *reply_fname = NULL;
736 if (email_header (fd, from_str, subject_str))
738 gw_log (GW_LOG_WARN, KERNEL_LOG, "No message body");
741 tty_init (stdout, 40, 70);
744 reply_fname = tempnam (gw_res_get (info.kernel_res,
745 "gw.reply.tmp.dir", NULL),
746 gw_res_get (info.kernel_res,
747 "gw.reply.tmp.prefix", "gwr"));
749 reply_fd = fopen (reply_fname, "w");
752 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, KERNEL_LOG, "Cannot create %s",
756 tty_init (reply_fd, 0, 0);
757 fprintf (reply_fd, "From: %s\n",
758 gw_res_get (info.kernel_res, "gw.msg.from","Email-gateway"));
759 fprintf (reply_fd, "Subject: ");
761 fprintf (reply_fd, "Z39.50 Re: %s", subject_str);
763 fprintf (reply_fd, "%s\n", gw_res_get (info.kernel_res,
766 fprintf (reply_fd, "\n");
767 gw_log (GW_LOG_ACCT, KERNEL_LOG, "User start %s", from_str);
770 gw_log (GW_LOG_WARN, KERNEL_LOG, "No From in email header");
771 fprintf (reply_fd, "%s\n", gw_res_get (info.kernel_res, "gw.msg.greeting",
772 "Email->Z39.50 gateway"));
773 while (lgets (line_buf, LINE_MAX, fd))
777 if (line_buf[0] == '\n')
780 while (lgets (line_buf, LINE_MAX, fd))
786 if ((cp = strchr (line_buf, '\n')))
788 gw_log (GW_LOG_ACCT, KERNEL_LOG, "cmd: %s", line_buf);
789 ccl_token_and = gw_res_get (info.kernel_res, "ccl.token.and", "and");
790 ccl_token_or = gw_res_get (info.kernel_res, "ccl.token.or", "or");
791 ccl_token_not = gw_res_get (info.kernel_res, "ccl.token.not", "not");
792 ccl_token_set = gw_res_get (info.kernel_res, "ccl.token.set", "set");
793 if (isalpha (line_buf[0]))
794 exec_command (line_buf);
799 fprintf (reply_fd, "%s\n", gw_res_get (info.kernel_res,
810 assert (reply_fname);
814 mta = gw_res_get (info.kernel_res, "gw.reply.mta",
815 "/usr/lib/sendmail");
816 sprintf (cmd, "%s %s < %s", mta, from_str, reply_fname);
818 mta_code = system (cmd);
820 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, KERNEL_LOG,
821 "Reply '%s' got exit code %d", cmd, mta_code);
822 unlink (reply_fname);
823 gw_log (GW_LOG_ACCT, KERNEL_LOG, "User end %s", from_str);