5 * Revision 1.18 1995/03/01 14:32:26 adam
6 * Better diagnostics. Default is, that only one database selected when
9 * Revision 1.17 1995/02/28 13:16:26 adam
10 * Configurable From: added.
12 * Revision 1.16 1995/02/23 10:08:20 adam
13 * Added logging of all user commands.
15 * Revision 1.15 1995/02/23 08:32:17 adam
18 * Revision 1.13 1995/02/22 16:54:42 adam
19 * Qualifiers of LOC target updated. More logging messages.
21 * Revision 1.12 1995/02/22 15:51:51 adam
22 * Bug fix: swap of parameter number and offset in function present.
24 * Revision 1.11 1995/02/22 15:22:33 adam
25 * Much more checking of run-time state. Show command never retrieves
26 * more records than indicated by the previous search request. Help
27 * command available. The maximum number of records retrieved can be
30 * Revision 1.10 1995/02/22 08:51:35 adam
31 * Output function can be customized in fml, which is used to print
32 * the reply to reply_fd.
34 * Revision 1.9 1995/02/21 17:46:21 adam
37 * Revision 1.8 1995/02/21 12:12:00 adam
38 * Diagnostic record with error info. observed.
40 * Revision 1.7 1995/02/20 21:16:20 adam
41 * FML support. Bug fixes. Profile for drewdb.
43 * Revision 1.6 1995/02/17 14:41:14 quinn
44 * Added simple display of records.
46 * Revision 1.5 1995/02/17 14:22:13 adam
47 * First steps of CCL show command. Not finished yet.
49 * Revision 1.4 1995/02/17 09:08:36 adam
50 * Reply with subject. CCL base command implemented.
52 * Revision 1.3 1995/02/16 18:35:09 adam
53 * First use of Zdist library. Search requests are supported.
54 * Present requests are not supported yet.
56 * Revision 1.2 1995/02/16 13:21:00 adam
57 * Organization of resource files for targets and conversion
58 * language implemented.
60 * Revision 1.1 1995/02/15 17:45:30 adam
61 * First version of email gateway kernel. Email requests are read
62 * from stdin. The output is transferred to an MTA if 'From' is
63 * found in the header - or stdout if absent. No Z39.50 client is used.
79 static void put_esc_str (const char *s)
84 if (*s == '\\' && s[1])
89 fputc ('\n', reply_fd);
92 fputc ('\t', reply_fd);
102 if (*s != ' ' || !escape_flag)
103 fputc (*s, reply_fd);
110 static int reopen_target (void)
114 gw_log (GW_LOG_WARN, KERNEL_LOG, "Zass free...");
115 info.zass = zass_open (info.hostname, info.port);
118 fprintf (reply_fd, "%s %s:%d\n",
119 gw_res_get (info.kernel_res, "gw.err.connect",
120 "Cannot connect to target"),
121 info.hostname, info.port);
124 v = gw_res_get (info.kernel_res, "gw.description", NULL);
128 fprintf (reply_fd, "\n");
130 fprintf (reply_fd, "%s %s:%d\n",
131 gw_res_get (info.kernel_res, "gw.msg.connect",
132 "Connection established to"),
133 info.hostname, info.port);
135 fprintf (reply_fd, "%s:\n%s\n",
136 gw_res_get (info.kernel_res, "gw.msg.databases",
137 "Available databases"),
140 fprintf (reply_fd, "%s:\n%s\n",
141 gw_res_get (info.kernel_res, "gw.msg.database",
142 "Selected databases"),
147 static char line_buf[LINE_MAX+1];
149 static struct command_word {
151 char *resource_suffix;
159 { "continue", "continue" },
160 { "status", "status" },
161 { "cancel", "cancel" },
162 { "target", "target" },
166 static int command_search (struct command_word *tab, struct ccl_token *cmd,
167 const char *resource_prefix)
171 assert (resource_prefix);
174 while (tab->default_value)
176 char *cp, command_names[60];
177 char resource_name[60];
180 sprintf (resource_name, "%s%s", resource_prefix,
181 tab->resource_suffix);
182 v = gw_res_get (info.kernel_res, resource_name, tab->default_value);
184 strcpy (command_names, v);
190 if ((split = strchr (cp, ' ')))
192 if (cmd->len == strlen(cp) &&
193 !memcmp (cmd->name, cp, cmd->len))
205 static struct error_no_struct {
208 } error_ccl_tab[] = {
210 { CCL_ERR_TERM_EXPECTED, "term.expected" },
211 { CCL_ERR_RP_EXPECTED, "rp.expected" },
212 { CCL_ERR_SETNAME_EXPECTED, "setname.expected" },
213 { CCL_ERR_OP_EXPECTED, "op.expected" },
214 { CCL_ERR_BAD_RP, "bad.rp" },
215 { CCL_ERR_UNKNOWN_QUAL, "unknown.qual" },
216 { CCL_ERR_DOUBLE_QUAL, "double.qual" },
217 { CCL_ERR_EQ_EXPECTED, "eq.expected" },
218 { CCL_ERR_BAD_RELATION, "bad.relation" },
219 { CCL_ERR_TRUNC_NOT_LEFT, "trunc.not.left" },
220 { CCL_ERR_TRUNC_NOT_BOTH, "trunc.not.both" },
221 { CCL_ERR_TRUNC_NOT_RIGHT, "trunc.not.right" },
225 static char *error_no_search (struct error_no_struct *tab, int no)
227 struct error_no_struct *p = tab;
228 while (p->resource_name)
231 return p->resource_name;
237 static int email_header (FILE *inf, char *from_str, char *subject_str)
241 while (fgets (line_buf, LINE_MAX, inf))
243 if (line_buf[0] == '\n')
245 if (strncmp (line_buf, "From ", 5) == 0)
246 sscanf (line_buf+4, "%s", from_str);
247 if (strncmp (line_buf, "Subject: ", 9) == 0 &&
248 sscanf (line_buf+9, "%s", subject_str+1) == 1)
249 strcpy (subject_str, line_buf+9);
254 static void help_general (void)
256 put_esc_str (gw_res_get (info.kernel_res, "gw.help.general",
257 "Commands available in this service:\n"));
260 static int exec_help (struct ccl_token *list)
265 put_esc_str (gw_res_get (info.kernel_res, "gw.help.target",
266 "target <name> - selects a given target\n"));
268 put_esc_str (gw_res_get (info.kernel_res, "gw.help.base",
269 "base <base>.. - selects databases\n"));
271 put_esc_str (gw_res_get (info.kernel_res, "gw.help.find",
272 "find <query> - performs a search request\n"));
274 put_esc_str (gw_res_get (info.kernel_res, "gw.help.show",
275 "show <spec> - retrieves and displays "
277 put_esc_str (gw_res_get (info.kernel_res, "gw.help.help",
278 "help - displays help\n"));
283 static void display_diag_error (int code, const char *addinfo)
287 sprintf (str, "gw.bib1.diag.%d", code);
288 fprintf (reply_fd, "%s %d:\n %s: '%s'\n",
289 gw_res_get (info.kernel_res, "gw.msg.z39errcode",
292 gw_res_get (info.kernel_res, str, ""), addinfo);
295 static int exec_find (struct ccl_token *list)
297 const struct zass_searchent *p;
298 struct gw_user_set *us;
300 struct ccl_rpn_node *rpn;
304 us = user_set_add ("Default", -1);
305 rpn = ccl_find (info.bibset, list, &error, &pos);
308 const char *v = NULL, *n;
311 fprintf (reply_fd, " %*s^ - ", pos - line_buf, " ");
313 n = error_no_search (error_ccl_tab, error);
316 sprintf (name, "gw.err.%s", n);
317 v = gw_res_get (info.kernel_res, name, NULL);
320 v = ccl_err_msg (error);
321 fprintf (reply_fd, "%s\n", v);
324 ccl_pr_tree (rpn, reply_fd);
325 fprintf (reply_fd, "\n");
327 if (!*info.database )
329 fprintf (reply_fd, "%s\n",
330 gw_res_get (info.kernel_res, "gw.err.no.database",
331 "You must select database"));
334 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Searching in database %s",
337 p = zass_search (info.zass, rpn, "Default", info.database );
340 if (p->errcode != -1)
342 display_diag_error (p->errcode, p->errstring);
345 fprintf (reply_fd, "%d %s\n", p->num,
346 gw_res_get (info.kernel_res, "gw.msg.hits", "hit(s)"));
351 static int exec_target (struct ccl_token *list)
354 if (list->kind == CCL_TOK_EOL)
357 memcpy (info.target, list->name, len);
358 info.target [len] = '\0';
361 return reopen_target ();
364 static int exec_base (struct ccl_token *list)
366 struct ccl_token *li = list;
370 if (list->kind == CCL_TOK_EOL)
372 free (info.database);
373 while (li->kind != CCL_TOK_EOL)
377 if (li->kind == CCL_TOK_COMMA)
380 info.database = malloc (len);
381 assert (info.database );
384 while (li->kind != CCL_TOK_EOL)
386 memcpy (info.database+len, li->name, li->len);
388 info.database[len++] = ',';
390 if (li->kind == CCL_TOK_COMMA)
393 info.database[len-1] = '\0';
397 struct command_word show_tab [] =
404 static void present (const char *set, int offset, int number,
405 struct ccl_token *format_token)
407 const struct zass_presentent *zp;
412 max_number = atoi (gw_res_get (info.kernel_res, "gw.max.show",
414 if (number > max_number)
416 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "present in set %s", set);
417 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "present of %d records from offset %d",
419 zp = zass_present(info.zass, (char *) set, offset, number);
425 int record_log_fd = -1;
426 const char *record_log_name;
428 record_log_name = gw_res_get (info.kernel_res, "gw.marc.log",
432 sprintf (path, "%s/%s", gw_res_get (info.kernel_res,
435 record_log_fd = open (path, O_WRONLY|O_CREAT|O_APPEND, 0666);
436 if (record_log_fd == -1)
437 gw_log (GW_LOG_WARN, "Cannot open %s", path);
439 fprintf (reply_fd, gw_res_get (info.kernel_res,
443 fprintf (reply_fd, "\n");
444 for (i = 0, pp = zp->records; pp; pp = pp->next, i++)
448 const char *arg_ar[5];
450 fprintf (reply_fd, "--- %d/%d ---\n",
451 i+offset, offset+zp->num-1);
452 if (!gw_res_get (info.kernel_res, "gw.ignore.which", NULL))
454 if (pp->which == ZASS_REC_DIAG)
456 display_diag_error (pp->errcode, pp->errstring);
459 else if (pp->which != ZASS_REC_USMARC)
461 fprintf (reply_fd, "Unknown record kind %d\n",
466 if (record_log_fd != -1)
467 write (record_log_fd, pp->record, strlen(pp->record));
468 rec = iso2709_cvt (pp->record);
474 len = format_token->len;
475 memcpy (format_str, format_token->name, len);
476 format_str[len] = '\0';
478 if (info.fml && format_token &&
479 (!strcmp (format_str, "0") || !strcmp (format_str, "1")))
482 arg_ar[1] = format_str;
483 arg_ar[2] = " \\list";
484 arg_ar[3] = marc_to_str (info.fml, rec);
486 fml_exec_call_argv (info.fml, arg_ar);
489 iso2709_display (rec, reply_fd);
491 iso2709_display (rec, reply_fd);
496 fprintf (reply_fd, "Not a MARC record\n");
498 if (record_log_fd != -1)
499 close (record_log_fd);
503 static int exec_show (struct ccl_token *list)
506 struct ccl_token *set_token = NULL;
507 struct ccl_token *format_token = NULL;
508 struct ccl_token *li = list;
509 int no_of_present = 0;
512 while (li->kind != CCL_TOK_EOL)
515 if (li->next->kind == CCL_TOK_EQ)
517 if (li->kind == CCL_TOK_SET) /* set = <name> ? */
524 modifier_no = command_search (show_tab, li, "ccl.token.");
527 fprintf (reply_fd, "Unknown modifier in show\n");
531 if (modifier_no == 1) /* f = <name> ? */
533 else if (modifier_no == 2) /* p = <name> ? */
535 if (li->kind != CCL_TOK_EOL /* p = <name> - <name> ? */
536 && li->next->kind == CCL_TOK_MINUS
537 && li->next->next != CCL_TOK_EOL)
543 fprintf (reply_fd, "%s\n", "Missing token after '='");
552 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Got set=%.*s", set_token->len,
555 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Got format=%.*s", format_token->len,
559 while (li->kind != CCL_TOK_EOL)
565 if (li->next->kind == CCL_TOK_EQ && li->kind != CCL_TOK_SET)
567 modifier_no = command_search (show_tab, li, "ccl.token.");
569 if (modifier_no == 2) /* p = <name> ? */
571 if (li->kind != CCL_TOK_EOL /* p = <name> - <name> ? */
572 && li->next->kind == CCL_TOK_MINUS
573 && li->next->next != CCL_TOK_EOL)
576 memcpy (tmp_str, li->name, len);
577 tmp_str [len] = '\0';
578 offset = atoi (tmp_str);
582 memcpy (tmp_str, li->name, len);
583 tmp_str [len] = '\0';
584 number = atoi (tmp_str) - offset + 1;
589 memcpy (tmp_str, li->name, len);
590 tmp_str [len] = '\0';
591 offset = atoi (tmp_str);
600 memcpy (tmp_str, li->name, len);
602 number = atoi (tmp_str);
606 if (offset > 0 && number > 0)
608 struct gw_user_set *us;
612 len = set_token->len;
613 memcpy (tmp_str, set_token->name, len);
615 us = user_set_search (tmp_str);
618 us = user_set_search (NULL);
619 if (us && us->hits != -1) /* proper result-set? */
621 if (offset <= us->hits)
623 if (offset+number-1 > us->hits)
624 number = us->hits - offset+1;
625 present (us->name, offset, number, format_token);
628 else if (!no_of_present) /* display error message once! */
630 fprintf (reply_fd, "%s\n",
631 gw_res_get (info.kernel_res, "gw.err.no.set",
632 "No result-set generated"));
637 if (!no_of_present) /* no records shown so far? */
639 struct gw_user_set *us;
642 us = user_set_search (NULL);
643 if (us && us->hits != -1) /* proper result-set? */
645 default_show = atoi (gw_res_get (info.kernel_res,
646 "gw.default.show", "20"));
647 if (us->hits > default_show)
648 present (us->name, 1, default_show, format_token);
649 else if (us->hits > 0)
650 present (us->name, 1, us->hits, format_token);
652 else /* display error message */
654 fprintf (reply_fd, "%s\n",
655 gw_res_get (info.kernel_res, "gw.err.no.set",
656 "No result-set generated"));
663 static int exec_command (const char *str)
665 struct ccl_token *cmd = ccl_tokenize (str);
668 if (cmd->kind != CCL_TOK_EOL &&
669 (no = command_search (command_tab, cmd, "ccl.command.")))
671 if (!info.zass && no != 9 && no != 4)
673 fprintf (reply_fd, "\n> %s\n", str);
674 if (!info.zass && (no == 1 || no == 2 || no == 3))
676 fprintf (reply_fd, "%s\n",
677 gw_res_get (info.kernel_res, "gw.err.no.target",
678 "No connection established - "
685 return exec_find (cmd->next);
687 return exec_show (cmd->next);
689 return exec_base (cmd->next);
691 return exec_help (cmd->next);
693 return exec_target (cmd->next);
695 fprintf (reply_fd, "%s\n",
696 gw_res_get (info.kernel_res, "gw.err.unimplemented",
697 "Not implemented yet"));
702 fprintf (reply_fd, "\n> %s\n", str);
703 fprintf (reply_fd, " ^ %s\n",
704 gw_res_get (info.kernel_res, "gw.err.unknown.command",
706 "Use help to see list of commands"));
714 char subject_str[128];
716 char *reply_fname = NULL;
718 if (email_header (inf, from_str, subject_str))
720 gw_log (GW_LOG_WARN, KERNEL_LOG, "No message body");
725 reply_fname = tempnam (gw_res_get (info.kernel_res,
726 "gw.reply.tmp.dir", NULL),
727 gw_res_get (info.kernel_res,
728 "gw.reply.tmp.prefix", "gwr"));
730 reply_fd = fopen (reply_fname, "w");
733 gw_log (GW_LOG_FATAL, KERNEL_LOG, "Cannot create %s",
737 fprintf (reply_fd, "From: %s\n",
738 gw_res_get (info.kernel_res, "gw.msg.from","Email-gateway"));
739 fprintf (reply_fd, "Subject: ");
741 fprintf (reply_fd, "Z39.50 Re: %s", subject_str);
743 fprintf (reply_fd, "%s\n", gw_res_get (info.kernel_res,
746 fprintf (reply_fd, "\n");
747 gw_log (GW_LOG_ACCT, KERNEL_LOG, "User start %s", from_str);
750 gw_log (GW_LOG_WARN, KERNEL_LOG, "No From in email header");
751 fprintf (reply_fd, "%s\n", gw_res_get (info.kernel_res, "gw.msg.greeting",
752 "Email->Z39.50 gateway"));
753 while (fgets (line_buf, LINE_MAX, inf))
757 if (line_buf[0] == '\n')
759 if ((cp = strchr (line_buf, '\n')))
761 gw_log (GW_LOG_ACCT, KERNEL_LOG, "cmd: %s", line_buf);
762 ccl_token_and = gw_res_get (info.kernel_res, "ccl.token.and", "and");
763 ccl_token_or = gw_res_get (info.kernel_res, "ccl.token.or", "or");
764 ccl_token_not = gw_res_get (info.kernel_res, "ccl.token.not", "not");
765 ccl_token_set = gw_res_get (info.kernel_res, "ccl.token.set", "set");
766 if (isalpha (line_buf[0]))
767 exec_command (line_buf);
772 fprintf (reply_fd, "%s\n", gw_res_get (info.kernel_res,
783 assert (reply_fname);
787 mta = gw_res_get (info.kernel_res, "gw.reply.mta",
788 "/usr/lib/sendmail");
789 sprintf (cmd, "%s %s < %s", mta, from_str, reply_fname);
791 mta_code = system (cmd);
793 gw_log (GW_LOG_FATAL, KERNEL_LOG, "Reply '%s' got exit code %d",
795 unlink (reply_fname);
796 gw_log (GW_LOG_ACCT, KERNEL_LOG, "User end %s", from_str);