5 * Revision 1.21 1995/03/27 08:24:04 adam
6 * First use of gip interface and gw-db.
7 * First work on eti program.
9 * Revision 1.20 1995/03/03 17:19:17 adam
10 * Smarter presentation. Bug fix in email header interpretation.
12 * Revision 1.19 1995/03/02 09:32:11 adam
13 * New presentation formats. f0=full, f1=brief, f2=mid
15 * Revision 1.18 1995/03/01 14:32:26 adam
16 * Better diagnostics. Default is, that only one database selected when
19 * Revision 1.17 1995/02/28 13:16:26 adam
20 * Configurable From: added.
22 * Revision 1.16 1995/02/23 10:08:20 adam
23 * Added logging of all user commands.
25 * Revision 1.15 1995/02/23 08:32:17 adam
28 * Revision 1.13 1995/02/22 16:54:42 adam
29 * Qualifiers of LOC target updated. More logging messages.
31 * Revision 1.12 1995/02/22 15:51:51 adam
32 * Bug fix: swap of parameter number and offset in function present.
34 * Revision 1.11 1995/02/22 15:22:33 adam
35 * Much more checking of run-time state. Show command never retrieves
36 * more records than indicated by the previous search request. Help
37 * command available. The maximum number of records retrieved can be
40 * Revision 1.10 1995/02/22 08:51:35 adam
41 * Output function can be customized in fml, which is used to print
42 * the reply to reply_fd.
44 * Revision 1.9 1995/02/21 17:46:21 adam
47 * Revision 1.8 1995/02/21 12:12:00 adam
48 * Diagnostic record with error info. observed.
50 * Revision 1.7 1995/02/20 21:16:20 adam
51 * FML support. Bug fixes. Profile for drewdb.
53 * Revision 1.6 1995/02/17 14:41:14 quinn
54 * Added simple display of records.
56 * Revision 1.5 1995/02/17 14:22:13 adam
57 * First steps of CCL show command. Not finished yet.
59 * Revision 1.4 1995/02/17 09:08:36 adam
60 * Reply with subject. CCL base command implemented.
62 * Revision 1.3 1995/02/16 18:35:09 adam
63 * First use of Zdist library. Search requests are supported.
64 * Present requests are not supported yet.
66 * Revision 1.2 1995/02/16 13:21:00 adam
67 * Organization of resource files for targets and conversion
68 * language implemented.
70 * Revision 1.1 1995/02/15 17:45:30 adam
71 * First version of email gateway kernel. Email requests are read
72 * from stdin. The output is transferred to an MTA if 'From' is
73 * found in the header - or stdout if absent. No Z39.50 client is used.
89 static void put_esc_str (const char *s)
95 int lgets (char *buf, int max, int fd)
102 if ((r=read (fd, buf+no, 1)) != 1)
105 gw_log (GW_LOG_WARN, KERNEL_LOG, "read fail");
111 if (buf[no++] == '\n')
118 static int reopen_target (void)
122 gw_log (GW_LOG_WARN, KERNEL_LOG, "Zass free...");
123 info.zass = zass_open (info.hostname, info.port);
126 fprintf (reply_fd, "%s %s:%d\n",
127 gw_res_get (info.kernel_res, "gw.err.connect",
128 "Cannot connect to target"),
129 info.hostname, info.port);
132 v = gw_res_get (info.kernel_res, "gw.description", NULL);
136 fprintf (reply_fd, "\n");
138 fprintf (reply_fd, "%s %s:%d\n",
139 gw_res_get (info.kernel_res, "gw.msg.connect",
140 "Connection established to"),
141 info.hostname, info.port);
143 fprintf (reply_fd, "%s:\n%s\n",
144 gw_res_get (info.kernel_res, "gw.msg.databases",
145 "Available databases"),
148 fprintf (reply_fd, "%s:\n%s\n",
149 gw_res_get (info.kernel_res, "gw.msg.database",
150 "Selected databases"),
155 static char line_buf[LINE_MAX+1];
157 static struct command_word {
159 char *resource_suffix;
167 { "continue", "continue" },
168 { "status", "status" },
169 { "cancel", "cancel" },
170 { "target", "target" },
174 static int command_search (struct command_word *tab, struct ccl_token *cmd,
175 const char *resource_prefix)
179 assert (resource_prefix);
182 while (tab->default_value)
184 char *cp, command_names[60];
185 char resource_name[60];
188 sprintf (resource_name, "%s%s", resource_prefix,
189 tab->resource_suffix);
190 v = gw_res_get (info.kernel_res, resource_name, tab->default_value);
192 strcpy (command_names, v);
198 if ((split = strchr (cp, ' ')))
200 if (cmd->len == strlen(cp) &&
201 !memcmp (cmd->name, cp, cmd->len))
213 static struct error_no_struct {
216 } error_ccl_tab[] = {
218 { CCL_ERR_TERM_EXPECTED, "term.expected" },
219 { CCL_ERR_RP_EXPECTED, "rp.expected" },
220 { CCL_ERR_SETNAME_EXPECTED, "setname.expected" },
221 { CCL_ERR_OP_EXPECTED, "op.expected" },
222 { CCL_ERR_BAD_RP, "bad.rp" },
223 { CCL_ERR_UNKNOWN_QUAL, "unknown.qual" },
224 { CCL_ERR_DOUBLE_QUAL, "double.qual" },
225 { CCL_ERR_EQ_EXPECTED, "eq.expected" },
226 { CCL_ERR_BAD_RELATION, "bad.relation" },
227 { CCL_ERR_TRUNC_NOT_LEFT, "trunc.not.left" },
228 { CCL_ERR_TRUNC_NOT_BOTH, "trunc.not.both" },
229 { CCL_ERR_TRUNC_NOT_RIGHT, "trunc.not.right" },
233 static char *error_no_search (struct error_no_struct *tab, int no)
235 struct error_no_struct *p = tab;
236 while (p->resource_name)
239 return p->resource_name;
245 static int email_header (int fd, char *from_str, char *subject_str)
249 while (lgets (line_buf, LINE_MAX, fd))
251 if (line_buf[0] == '\n')
253 if (strncmp (line_buf, "From ", 5) == 0)
254 sscanf (line_buf+4, "%s", from_str);
255 if (strncmp (line_buf, "Subject: ", 9) == 0 &&
256 sscanf (line_buf+9, "%s", subject_str+1) == 1)
257 strcpy (subject_str, line_buf+9);
262 static void help_general (void)
264 put_esc_str (gw_res_get (info.kernel_res, "gw.help.general",
265 "Commands available in this service:\n"));
268 static int exec_help (struct ccl_token *list)
270 static char *sep = "-------------------------------\\n";
275 put_esc_str (gw_res_get (info.kernel_res, "gw.help.target",
276 "target <name> - selects a given target\n"));
279 put_esc_str (gw_res_get (info.kernel_res, "gw.help.base",
280 "base <base>.. - selects databases\n"));
283 put_esc_str (gw_res_get (info.kernel_res, "gw.help.find",
284 "find <query> - performs a search request\n"));
287 put_esc_str (gw_res_get (info.kernel_res, "gw.help.show",
288 "show <spec> - retrieves and displays "
294 static void display_diag_error (int code, const char *addinfo)
298 sprintf (str, "gw.bib1.diag.%d", code);
299 fprintf (reply_fd, "%s %d:\n %s: '%s'\n",
300 gw_res_get (info.kernel_res, "gw.msg.z39errcode",
303 gw_res_get (info.kernel_res, str, ""), addinfo);
306 static int exec_find (struct ccl_token *list)
308 const struct zass_searchent *p;
309 struct gw_user_set *us;
311 struct ccl_rpn_node *rpn;
315 us = user_set_add ("Default", -1);
316 rpn = ccl_find (info.bibset, list, &error, &pos);
319 const char *v = NULL, *n;
322 fprintf (reply_fd, " %*s^ - ", pos - line_buf, " ");
324 n = error_no_search (error_ccl_tab, error);
327 sprintf (name, "gw.err.%s", n);
328 v = gw_res_get (info.kernel_res, name, NULL);
331 v = ccl_err_msg (error);
332 fprintf (reply_fd, "%s\n", v);
335 ccl_pr_tree (rpn, reply_fd);
336 fprintf (reply_fd, "\n");
338 if (!*info.database )
340 fprintf (reply_fd, "%s\n",
341 gw_res_get (info.kernel_res, "gw.err.no.database",
342 "You must select database"));
345 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Searching in database %s",
348 p = zass_search (info.zass, rpn, "Default", info.database );
351 if (p->errcode != -1)
353 display_diag_error (p->errcode, p->errstring);
356 fprintf (reply_fd, "%d %s\n", p->num,
357 gw_res_get (info.kernel_res, "gw.msg.hits", "hit(s)"));
362 static int exec_target (struct ccl_token *list)
365 if (list->kind == CCL_TOK_EOL)
368 memcpy (info.target, list->name, len);
369 info.target [len] = '\0';
372 return reopen_target ();
375 static int exec_base (struct ccl_token *list)
377 struct ccl_token *li = list;
381 if (list->kind == CCL_TOK_EOL)
383 free (info.database);
384 while (li->kind != CCL_TOK_EOL)
388 if (li->kind == CCL_TOK_COMMA)
391 info.database = malloc (len);
392 assert (info.database );
395 while (li->kind != CCL_TOK_EOL)
397 memcpy (info.database+len, li->name, li->len);
399 info.database[len++] = ',';
401 if (li->kind == CCL_TOK_COMMA)
404 info.database[len-1] = '\0';
408 struct command_word show_tab [] =
415 static void present (const char *set, int offset, int number,
416 struct ccl_token *format_token)
418 const struct zass_presentent *zp;
423 max_number = atoi (gw_res_get (info.kernel_res, "gw.max.show",
425 if (number > max_number)
427 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "present in set %s", set);
428 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "present of %d records from offset %d",
430 zp = zass_present(info.zass, (char *) set, offset, number);
436 int record_log_fd = -1;
437 const char *record_log_name;
439 record_log_name = gw_res_get (info.kernel_res, "gw.marc.log",
443 sprintf (path, "%s/%s", gw_res_get (info.kernel_res,
446 record_log_fd = open (path, O_WRONLY|O_CREAT|O_APPEND, 0666);
447 if (record_log_fd == -1)
448 gw_log (GW_LOG_WARN, "Cannot open %s", path);
450 fprintf (reply_fd, gw_res_get (info.kernel_res,
454 fprintf (reply_fd, "\n");
455 for (i = 0, pp = zp->records; pp; pp = pp->next, i++)
459 const char *arg_ar[5];
461 fprintf (reply_fd, "--- %d/%d ---\n",
462 i+offset, offset+zp->num-1);
463 if (!gw_res_get (info.kernel_res, "gw.ignore.which", NULL))
465 if (pp->which == ZASS_REC_DIAG)
467 display_diag_error (pp->errcode, pp->errstring);
470 else if (pp->which != ZASS_REC_USMARC)
472 fprintf (reply_fd, "Unknown record kind %d\n",
477 if (record_log_fd != -1)
478 write (record_log_fd, pp->record, strlen(pp->record));
479 rec = iso2709_cvt (pp->record);
485 len = format_token->len;
486 if (len >= sizeof(format_str))
487 len = sizeof(format_str)-1;
488 memcpy (format_str, format_token->name, len);
489 format_str[len] = '\0';
491 if (info.fml && format_token &&
492 (!strcmp (format_str, "0") || !strcmp (format_str, "1")
493 || !strcmp(format_str, "2")))
496 arg_ar[1] = format_str;
497 arg_ar[2] = " \\list";
498 arg_ar[3] = marc_to_str (info.fml, rec);
500 fml_exec_call_argv (info.fml, arg_ar);
503 iso2709_display (rec, reply_fd);
505 iso2709_display (rec, reply_fd);
510 fprintf (reply_fd, "Not a MARC record\n");
512 if (record_log_fd != -1)
513 close (record_log_fd);
517 static int exec_show (struct ccl_token *list)
520 struct ccl_token *set_token = NULL;
521 struct ccl_token *format_token = NULL;
522 struct ccl_token *li = list;
523 int no_of_present = 0;
526 while (li->kind != CCL_TOK_EOL)
529 if (li->next->kind == CCL_TOK_EQ)
531 if (li->kind == CCL_TOK_SET) /* set = <name> ? */
538 modifier_no = command_search (show_tab, li, "ccl.token.");
541 fprintf (reply_fd, "Unknown modifier in show\n");
545 if (modifier_no == 1) /* f = <name> ? */
547 else if (modifier_no == 2) /* p = <name> ? */
549 if (li->kind != CCL_TOK_EOL /* p = <name> - <name> ? */
550 && li->next->kind == CCL_TOK_MINUS
551 && li->next->next != CCL_TOK_EOL)
557 fprintf (reply_fd, "%s\n", "Missing token after '='");
566 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Got set=%.*s", set_token->len,
569 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Got format=%.*s", format_token->len,
573 while (li->kind != CCL_TOK_EOL)
579 if (li->next->kind == CCL_TOK_EQ && li->kind != CCL_TOK_SET)
581 modifier_no = command_search (show_tab, li, "ccl.token.");
583 if (modifier_no == 2) /* p = <name> ? */
585 if (li->kind != CCL_TOK_EOL /* p = <name> - <name> ? */
586 && li->next->kind == CCL_TOK_MINUS
587 && li->next->next != CCL_TOK_EOL)
590 memcpy (tmp_str, li->name, len);
591 tmp_str [len] = '\0';
592 offset = atoi (tmp_str);
596 memcpy (tmp_str, li->name, len);
597 tmp_str [len] = '\0';
598 number = atoi (tmp_str) - offset + 1;
603 memcpy (tmp_str, li->name, len);
604 tmp_str [len] = '\0';
605 offset = atoi (tmp_str);
614 memcpy (tmp_str, li->name, len);
616 number = atoi (tmp_str);
620 if (offset > 0 && number > 0)
622 struct gw_user_set *us;
626 len = set_token->len;
627 memcpy (tmp_str, set_token->name, len);
629 us = user_set_search (tmp_str);
632 us = user_set_search (NULL);
633 if (us && us->hits != -1) /* proper result-set? */
635 if (offset <= us->hits)
637 if (offset+number-1 > us->hits)
638 number = us->hits - offset+1;
639 present (us->name, offset, number, format_token);
642 else if (!no_of_present) /* display error message once! */
644 fprintf (reply_fd, "%s\n",
645 gw_res_get (info.kernel_res, "gw.err.no.set",
646 "No result-set generated"));
651 if (!no_of_present) /* no records shown so far? */
653 struct gw_user_set *us;
656 us = user_set_search (NULL);
657 if (us && us->hits != -1) /* proper result-set? */
659 default_show = atoi (gw_res_get (info.kernel_res,
660 "gw.default.show", "20"));
661 if (us->hits > default_show)
662 present (us->name, 1, default_show, format_token);
663 else if (us->hits > 0)
664 present (us->name, 1, us->hits, format_token);
666 else /* display error message */
668 fprintf (reply_fd, "%s\n",
669 gw_res_get (info.kernel_res, "gw.err.no.set",
670 "No result-set generated"));
677 static int exec_command (const char *str)
679 struct ccl_token *cmd = ccl_tokenize (str);
682 if (cmd->kind != CCL_TOK_EOL &&
683 (no = command_search (command_tab, cmd, "ccl.command.")))
685 if (!info.zass && no != 9 && no != 4)
687 fprintf (reply_fd, "\n> %s\n", str);
688 if (!info.zass && (no == 1 || no == 2 || no == 3))
690 fprintf (reply_fd, "%s\n",
691 gw_res_get (info.kernel_res, "gw.err.no.target",
692 "No connection established - "
699 return exec_find (cmd->next);
701 return exec_show (cmd->next);
703 return exec_base (cmd->next);
705 return exec_help (cmd->next);
707 return exec_target (cmd->next);
709 fprintf (reply_fd, "%s\n",
710 gw_res_get (info.kernel_res, "gw.err.unimplemented",
711 "Not implemented yet"));
716 fprintf (reply_fd, "\n> %s\n", str);
717 fprintf (reply_fd, " ^ %s\n",
718 gw_res_get (info.kernel_res, "gw.err.unknown.command",
720 "Use help to see list of commands"));
728 char subject_str[128];
730 char *reply_fname = NULL;
732 if (email_header (fd, from_str, subject_str))
734 gw_log (GW_LOG_WARN, KERNEL_LOG, "No message body");
737 tty_init (stdout, 40, 70);
740 reply_fname = tempnam (gw_res_get (info.kernel_res,
741 "gw.reply.tmp.dir", NULL),
742 gw_res_get (info.kernel_res,
743 "gw.reply.tmp.prefix", "gwr"));
745 reply_fd = fopen (reply_fname, "w");
748 gw_log (GW_LOG_FATAL, KERNEL_LOG, "Cannot create %s",
752 tty_init (reply_fd, 0, 0);
753 fprintf (reply_fd, "From: %s\n",
754 gw_res_get (info.kernel_res, "gw.msg.from","Email-gateway"));
755 fprintf (reply_fd, "Subject: ");
757 fprintf (reply_fd, "Z39.50 Re: %s", subject_str);
759 fprintf (reply_fd, "%s\n", gw_res_get (info.kernel_res,
762 fprintf (reply_fd, "\n");
763 gw_log (GW_LOG_ACCT, KERNEL_LOG, "User start %s", from_str);
766 gw_log (GW_LOG_WARN, KERNEL_LOG, "No From in email header");
767 fprintf (reply_fd, "%s\n", gw_res_get (info.kernel_res, "gw.msg.greeting",
768 "Email->Z39.50 gateway"));
769 while (lgets (line_buf, LINE_MAX, fd))
773 if (line_buf[0] == '\n')
776 while (lgets (line_buf, LINE_MAX, fd))
782 if ((cp = strchr (line_buf, '\n')))
784 gw_log (GW_LOG_ACCT, KERNEL_LOG, "cmd: %s", line_buf);
785 ccl_token_and = gw_res_get (info.kernel_res, "ccl.token.and", "and");
786 ccl_token_or = gw_res_get (info.kernel_res, "ccl.token.or", "or");
787 ccl_token_not = gw_res_get (info.kernel_res, "ccl.token.not", "not");
788 ccl_token_set = gw_res_get (info.kernel_res, "ccl.token.set", "set");
789 if (isalpha (line_buf[0]))
790 exec_command (line_buf);
795 fprintf (reply_fd, "%s\n", gw_res_get (info.kernel_res,
806 assert (reply_fname);
810 mta = gw_res_get (info.kernel_res, "gw.reply.mta",
811 "/usr/lib/sendmail");
812 sprintf (cmd, "%s %s < %s", mta, from_str, reply_fname);
814 mta_code = system (cmd);
816 gw_log (GW_LOG_FATAL, KERNEL_LOG, "Reply '%s' got exit code %d",
818 unlink (reply_fname);
819 gw_log (GW_LOG_ACCT, KERNEL_LOG, "User end %s", from_str);