5 * Revision 1.19 1995/03/02 09:32:11 adam
6 * New presentation formats. f0=full, f1=brief, f2=mid
8 * Revision 1.18 1995/03/01 14:32:26 adam
9 * Better diagnostics. Default is, that only one database selected when
12 * Revision 1.17 1995/02/28 13:16:26 adam
13 * Configurable From: added.
15 * Revision 1.16 1995/02/23 10:08:20 adam
16 * Added logging of all user commands.
18 * Revision 1.15 1995/02/23 08:32:17 adam
21 * Revision 1.13 1995/02/22 16:54:42 adam
22 * Qualifiers of LOC target updated. More logging messages.
24 * Revision 1.12 1995/02/22 15:51:51 adam
25 * Bug fix: swap of parameter number and offset in function present.
27 * Revision 1.11 1995/02/22 15:22:33 adam
28 * Much more checking of run-time state. Show command never retrieves
29 * more records than indicated by the previous search request. Help
30 * command available. The maximum number of records retrieved can be
33 * Revision 1.10 1995/02/22 08:51:35 adam
34 * Output function can be customized in fml, which is used to print
35 * the reply to reply_fd.
37 * Revision 1.9 1995/02/21 17:46:21 adam
40 * Revision 1.8 1995/02/21 12:12:00 adam
41 * Diagnostic record with error info. observed.
43 * Revision 1.7 1995/02/20 21:16:20 adam
44 * FML support. Bug fixes. Profile for drewdb.
46 * Revision 1.6 1995/02/17 14:41:14 quinn
47 * Added simple display of records.
49 * Revision 1.5 1995/02/17 14:22:13 adam
50 * First steps of CCL show command. Not finished yet.
52 * Revision 1.4 1995/02/17 09:08:36 adam
53 * Reply with subject. CCL base command implemented.
55 * Revision 1.3 1995/02/16 18:35:09 adam
56 * First use of Zdist library. Search requests are supported.
57 * Present requests are not supported yet.
59 * Revision 1.2 1995/02/16 13:21:00 adam
60 * Organization of resource files for targets and conversion
61 * language implemented.
63 * Revision 1.1 1995/02/15 17:45:30 adam
64 * First version of email gateway kernel. Email requests are read
65 * from stdin. The output is transferred to an MTA if 'From' is
66 * found in the header - or stdout if absent. No Z39.50 client is used.
82 static void put_esc_str (const char *s)
87 if (*s == '\\' && s[1])
92 fputc ('\n', reply_fd);
95 fputc ('\t', reply_fd);
105 if (*s != ' ' || !escape_flag)
106 fputc (*s, reply_fd);
113 static int reopen_target (void)
117 gw_log (GW_LOG_WARN, KERNEL_LOG, "Zass free...");
118 info.zass = zass_open (info.hostname, info.port);
121 fprintf (reply_fd, "%s %s:%d\n",
122 gw_res_get (info.kernel_res, "gw.err.connect",
123 "Cannot connect to target"),
124 info.hostname, info.port);
127 v = gw_res_get (info.kernel_res, "gw.description", NULL);
131 fprintf (reply_fd, "\n");
133 fprintf (reply_fd, "%s %s:%d\n",
134 gw_res_get (info.kernel_res, "gw.msg.connect",
135 "Connection established to"),
136 info.hostname, info.port);
138 fprintf (reply_fd, "%s:\n%s\n",
139 gw_res_get (info.kernel_res, "gw.msg.databases",
140 "Available databases"),
143 fprintf (reply_fd, "%s:\n%s\n",
144 gw_res_get (info.kernel_res, "gw.msg.database",
145 "Selected databases"),
150 static char line_buf[LINE_MAX+1];
152 static struct command_word {
154 char *resource_suffix;
162 { "continue", "continue" },
163 { "status", "status" },
164 { "cancel", "cancel" },
165 { "target", "target" },
169 static int command_search (struct command_word *tab, struct ccl_token *cmd,
170 const char *resource_prefix)
174 assert (resource_prefix);
177 while (tab->default_value)
179 char *cp, command_names[60];
180 char resource_name[60];
183 sprintf (resource_name, "%s%s", resource_prefix,
184 tab->resource_suffix);
185 v = gw_res_get (info.kernel_res, resource_name, tab->default_value);
187 strcpy (command_names, v);
193 if ((split = strchr (cp, ' ')))
195 if (cmd->len == strlen(cp) &&
196 !memcmp (cmd->name, cp, cmd->len))
208 static struct error_no_struct {
211 } error_ccl_tab[] = {
213 { CCL_ERR_TERM_EXPECTED, "term.expected" },
214 { CCL_ERR_RP_EXPECTED, "rp.expected" },
215 { CCL_ERR_SETNAME_EXPECTED, "setname.expected" },
216 { CCL_ERR_OP_EXPECTED, "op.expected" },
217 { CCL_ERR_BAD_RP, "bad.rp" },
218 { CCL_ERR_UNKNOWN_QUAL, "unknown.qual" },
219 { CCL_ERR_DOUBLE_QUAL, "double.qual" },
220 { CCL_ERR_EQ_EXPECTED, "eq.expected" },
221 { CCL_ERR_BAD_RELATION, "bad.relation" },
222 { CCL_ERR_TRUNC_NOT_LEFT, "trunc.not.left" },
223 { CCL_ERR_TRUNC_NOT_BOTH, "trunc.not.both" },
224 { CCL_ERR_TRUNC_NOT_RIGHT, "trunc.not.right" },
228 static char *error_no_search (struct error_no_struct *tab, int no)
230 struct error_no_struct *p = tab;
231 while (p->resource_name)
234 return p->resource_name;
240 static int email_header (FILE *inf, char *from_str, char *subject_str)
244 while (fgets (line_buf, LINE_MAX, inf))
246 if (line_buf[0] == '\n')
248 if (strncmp (line_buf, "From ", 5) == 0)
249 sscanf (line_buf+4, "%s", from_str);
250 if (strncmp (line_buf, "Subject: ", 9) == 0 &&
251 sscanf (line_buf+9, "%s", subject_str+1) == 1)
252 strcpy (subject_str, line_buf+9);
257 static void help_general (void)
259 put_esc_str (gw_res_get (info.kernel_res, "gw.help.general",
260 "Commands available in this service:\n"));
263 static int exec_help (struct ccl_token *list)
268 put_esc_str (gw_res_get (info.kernel_res, "gw.help.target",
269 "target <name> - selects a given target\n"));
271 put_esc_str (gw_res_get (info.kernel_res, "gw.help.base",
272 "base <base>.. - selects databases\n"));
274 put_esc_str (gw_res_get (info.kernel_res, "gw.help.find",
275 "find <query> - performs a search request\n"));
277 put_esc_str (gw_res_get (info.kernel_res, "gw.help.show",
278 "show <spec> - retrieves and displays "
280 put_esc_str (gw_res_get (info.kernel_res, "gw.help.help",
281 "help - displays help\n"));
286 static void display_diag_error (int code, const char *addinfo)
290 sprintf (str, "gw.bib1.diag.%d", code);
291 fprintf (reply_fd, "%s %d:\n %s: '%s'\n",
292 gw_res_get (info.kernel_res, "gw.msg.z39errcode",
295 gw_res_get (info.kernel_res, str, ""), addinfo);
298 static int exec_find (struct ccl_token *list)
300 const struct zass_searchent *p;
301 struct gw_user_set *us;
303 struct ccl_rpn_node *rpn;
307 us = user_set_add ("Default", -1);
308 rpn = ccl_find (info.bibset, list, &error, &pos);
311 const char *v = NULL, *n;
314 fprintf (reply_fd, " %*s^ - ", pos - line_buf, " ");
316 n = error_no_search (error_ccl_tab, error);
319 sprintf (name, "gw.err.%s", n);
320 v = gw_res_get (info.kernel_res, name, NULL);
323 v = ccl_err_msg (error);
324 fprintf (reply_fd, "%s\n", v);
327 ccl_pr_tree (rpn, reply_fd);
328 fprintf (reply_fd, "\n");
330 if (!*info.database )
332 fprintf (reply_fd, "%s\n",
333 gw_res_get (info.kernel_res, "gw.err.no.database",
334 "You must select database"));
337 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Searching in database %s",
340 p = zass_search (info.zass, rpn, "Default", info.database );
343 if (p->errcode != -1)
345 display_diag_error (p->errcode, p->errstring);
348 fprintf (reply_fd, "%d %s\n", p->num,
349 gw_res_get (info.kernel_res, "gw.msg.hits", "hit(s)"));
354 static int exec_target (struct ccl_token *list)
357 if (list->kind == CCL_TOK_EOL)
360 memcpy (info.target, list->name, len);
361 info.target [len] = '\0';
364 return reopen_target ();
367 static int exec_base (struct ccl_token *list)
369 struct ccl_token *li = list;
373 if (list->kind == CCL_TOK_EOL)
375 free (info.database);
376 while (li->kind != CCL_TOK_EOL)
380 if (li->kind == CCL_TOK_COMMA)
383 info.database = malloc (len);
384 assert (info.database );
387 while (li->kind != CCL_TOK_EOL)
389 memcpy (info.database+len, li->name, li->len);
391 info.database[len++] = ',';
393 if (li->kind == CCL_TOK_COMMA)
396 info.database[len-1] = '\0';
400 struct command_word show_tab [] =
407 static void present (const char *set, int offset, int number,
408 struct ccl_token *format_token)
410 const struct zass_presentent *zp;
415 max_number = atoi (gw_res_get (info.kernel_res, "gw.max.show",
417 if (number > max_number)
419 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "present in set %s", set);
420 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "present of %d records from offset %d",
422 zp = zass_present(info.zass, (char *) set, offset, number);
428 int record_log_fd = -1;
429 const char *record_log_name;
431 record_log_name = gw_res_get (info.kernel_res, "gw.marc.log",
435 sprintf (path, "%s/%s", gw_res_get (info.kernel_res,
438 record_log_fd = open (path, O_WRONLY|O_CREAT|O_APPEND, 0666);
439 if (record_log_fd == -1)
440 gw_log (GW_LOG_WARN, "Cannot open %s", path);
442 fprintf (reply_fd, gw_res_get (info.kernel_res,
446 fprintf (reply_fd, "\n");
447 for (i = 0, pp = zp->records; pp; pp = pp->next, i++)
451 const char *arg_ar[5];
453 fprintf (reply_fd, "--- %d/%d ---\n",
454 i+offset, offset+zp->num-1);
455 if (!gw_res_get (info.kernel_res, "gw.ignore.which", NULL))
457 if (pp->which == ZASS_REC_DIAG)
459 display_diag_error (pp->errcode, pp->errstring);
462 else if (pp->which != ZASS_REC_USMARC)
464 fprintf (reply_fd, "Unknown record kind %d\n",
469 if (record_log_fd != -1)
470 write (record_log_fd, pp->record, strlen(pp->record));
471 rec = iso2709_cvt (pp->record);
477 len = format_token->len;
478 if (len >= sizeof(format_str))
479 len = sizeof(format_str)-1;
480 memcpy (format_str, format_token->name, len);
481 format_str[len] = '\0';
483 if (info.fml && format_token &&
484 (!strcmp (format_str, "0") || !strcmp (format_str, "1")
485 || !strcmp(format_str, "2")))
488 arg_ar[1] = format_str;
489 arg_ar[2] = " \\list";
490 arg_ar[3] = marc_to_str (info.fml, rec);
492 fml_exec_call_argv (info.fml, arg_ar);
495 iso2709_display (rec, reply_fd);
497 iso2709_display (rec, reply_fd);
502 fprintf (reply_fd, "Not a MARC record\n");
504 if (record_log_fd != -1)
505 close (record_log_fd);
509 static int exec_show (struct ccl_token *list)
512 struct ccl_token *set_token = NULL;
513 struct ccl_token *format_token = NULL;
514 struct ccl_token *li = list;
515 int no_of_present = 0;
518 while (li->kind != CCL_TOK_EOL)
521 if (li->next->kind == CCL_TOK_EQ)
523 if (li->kind == CCL_TOK_SET) /* set = <name> ? */
530 modifier_no = command_search (show_tab, li, "ccl.token.");
533 fprintf (reply_fd, "Unknown modifier in show\n");
537 if (modifier_no == 1) /* f = <name> ? */
539 else if (modifier_no == 2) /* p = <name> ? */
541 if (li->kind != CCL_TOK_EOL /* p = <name> - <name> ? */
542 && li->next->kind == CCL_TOK_MINUS
543 && li->next->next != CCL_TOK_EOL)
549 fprintf (reply_fd, "%s\n", "Missing token after '='");
558 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Got set=%.*s", set_token->len,
561 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Got format=%.*s", format_token->len,
565 while (li->kind != CCL_TOK_EOL)
571 if (li->next->kind == CCL_TOK_EQ && li->kind != CCL_TOK_SET)
573 modifier_no = command_search (show_tab, li, "ccl.token.");
575 if (modifier_no == 2) /* p = <name> ? */
577 if (li->kind != CCL_TOK_EOL /* p = <name> - <name> ? */
578 && li->next->kind == CCL_TOK_MINUS
579 && li->next->next != CCL_TOK_EOL)
582 memcpy (tmp_str, li->name, len);
583 tmp_str [len] = '\0';
584 offset = atoi (tmp_str);
588 memcpy (tmp_str, li->name, len);
589 tmp_str [len] = '\0';
590 number = atoi (tmp_str) - offset + 1;
595 memcpy (tmp_str, li->name, len);
596 tmp_str [len] = '\0';
597 offset = atoi (tmp_str);
606 memcpy (tmp_str, li->name, len);
608 number = atoi (tmp_str);
612 if (offset > 0 && number > 0)
614 struct gw_user_set *us;
618 len = set_token->len;
619 memcpy (tmp_str, set_token->name, len);
621 us = user_set_search (tmp_str);
624 us = user_set_search (NULL);
625 if (us && us->hits != -1) /* proper result-set? */
627 if (offset <= us->hits)
629 if (offset+number-1 > us->hits)
630 number = us->hits - offset+1;
631 present (us->name, offset, number, format_token);
634 else if (!no_of_present) /* display error message once! */
636 fprintf (reply_fd, "%s\n",
637 gw_res_get (info.kernel_res, "gw.err.no.set",
638 "No result-set generated"));
643 if (!no_of_present) /* no records shown so far? */
645 struct gw_user_set *us;
648 us = user_set_search (NULL);
649 if (us && us->hits != -1) /* proper result-set? */
651 default_show = atoi (gw_res_get (info.kernel_res,
652 "gw.default.show", "20"));
653 if (us->hits > default_show)
654 present (us->name, 1, default_show, format_token);
655 else if (us->hits > 0)
656 present (us->name, 1, us->hits, format_token);
658 else /* display error message */
660 fprintf (reply_fd, "%s\n",
661 gw_res_get (info.kernel_res, "gw.err.no.set",
662 "No result-set generated"));
669 static int exec_command (const char *str)
671 struct ccl_token *cmd = ccl_tokenize (str);
674 if (cmd->kind != CCL_TOK_EOL &&
675 (no = command_search (command_tab, cmd, "ccl.command.")))
677 if (!info.zass && no != 9 && no != 4)
679 fprintf (reply_fd, "\n> %s\n", str);
680 if (!info.zass && (no == 1 || no == 2 || no == 3))
682 fprintf (reply_fd, "%s\n",
683 gw_res_get (info.kernel_res, "gw.err.no.target",
684 "No connection established - "
691 return exec_find (cmd->next);
693 return exec_show (cmd->next);
695 return exec_base (cmd->next);
697 return exec_help (cmd->next);
699 return exec_target (cmd->next);
701 fprintf (reply_fd, "%s\n",
702 gw_res_get (info.kernel_res, "gw.err.unimplemented",
703 "Not implemented yet"));
708 fprintf (reply_fd, "\n> %s\n", str);
709 fprintf (reply_fd, " ^ %s\n",
710 gw_res_get (info.kernel_res, "gw.err.unknown.command",
712 "Use help to see list of commands"));
720 char subject_str[128];
722 char *reply_fname = NULL;
724 if (email_header (inf, from_str, subject_str))
726 gw_log (GW_LOG_WARN, KERNEL_LOG, "No message body");
731 reply_fname = tempnam (gw_res_get (info.kernel_res,
732 "gw.reply.tmp.dir", NULL),
733 gw_res_get (info.kernel_res,
734 "gw.reply.tmp.prefix", "gwr"));
736 reply_fd = fopen (reply_fname, "w");
739 gw_log (GW_LOG_FATAL, KERNEL_LOG, "Cannot create %s",
743 fprintf (reply_fd, "From: %s\n",
744 gw_res_get (info.kernel_res, "gw.msg.from","Email-gateway"));
745 fprintf (reply_fd, "Subject: ");
747 fprintf (reply_fd, "Z39.50 Re: %s", subject_str);
749 fprintf (reply_fd, "%s\n", gw_res_get (info.kernel_res,
752 fprintf (reply_fd, "\n");
753 gw_log (GW_LOG_ACCT, KERNEL_LOG, "User start %s", from_str);
756 gw_log (GW_LOG_WARN, KERNEL_LOG, "No From in email header");
757 fprintf (reply_fd, "%s\n", gw_res_get (info.kernel_res, "gw.msg.greeting",
758 "Email->Z39.50 gateway"));
759 while (fgets (line_buf, LINE_MAX, inf))
763 if (line_buf[0] == '\n')
765 if ((cp = strchr (line_buf, '\n')))
767 gw_log (GW_LOG_ACCT, KERNEL_LOG, "cmd: %s", line_buf);
768 ccl_token_and = gw_res_get (info.kernel_res, "ccl.token.and", "and");
769 ccl_token_or = gw_res_get (info.kernel_res, "ccl.token.or", "or");
770 ccl_token_not = gw_res_get (info.kernel_res, "ccl.token.not", "not");
771 ccl_token_set = gw_res_get (info.kernel_res, "ccl.token.set", "set");
772 if (isalpha (line_buf[0]))
773 exec_command (line_buf);
778 fprintf (reply_fd, "%s\n", gw_res_get (info.kernel_res,
789 assert (reply_fname);
793 mta = gw_res_get (info.kernel_res, "gw.reply.mta",
794 "/usr/lib/sendmail");
795 sprintf (cmd, "%s %s < %s", mta, from_str, reply_fname);
797 mta_code = system (cmd);
799 gw_log (GW_LOG_FATAL, KERNEL_LOG, "Reply '%s' got exit code %d",
801 unlink (reply_fname);
802 gw_log (GW_LOG_ACCT, KERNEL_LOG, "User end %s", from_str);