5 * Revision 1.15 1995/02/23 08:32:17 adam
8 * Revision 1.13 1995/02/22 16:54:42 adam
9 * Qualifiers of LOC target updated. More logging messages.
11 * Revision 1.12 1995/02/22 15:51:51 adam
12 * Bug fix: swap of parameter number and offset in function present.
14 * Revision 1.11 1995/02/22 15:22:33 adam
15 * Much more checking of run-time state. Show command never retrieves
16 * more records than indicated by the previous search request. Help
17 * command available. The maximum number of records retrieved can be
20 * Revision 1.10 1995/02/22 08:51:35 adam
21 * Output function can be customized in fml, which is used to print
22 * the reply to reply_fd.
24 * Revision 1.9 1995/02/21 17:46:21 adam
27 * Revision 1.8 1995/02/21 12:12:00 adam
28 * Diagnostic record with error info. observed.
30 * Revision 1.7 1995/02/20 21:16:20 adam
31 * FML support. Bug fixes. Profile for drewdb.
33 * Revision 1.6 1995/02/17 14:41:14 quinn
34 * Added simple display of records.
36 * Revision 1.5 1995/02/17 14:22:13 adam
37 * First steps of CCL show command. Not finished yet.
39 * Revision 1.4 1995/02/17 09:08:36 adam
40 * Reply with subject. CCL base command implemented.
42 * Revision 1.3 1995/02/16 18:35:09 adam
43 * First use of Zdist library. Search requests are supported.
44 * Present requests are not supported yet.
46 * Revision 1.2 1995/02/16 13:21:00 adam
47 * Organization of resource files for targets and conversion
48 * language implemented.
50 * Revision 1.1 1995/02/15 17:45:30 adam
51 * First version of email gateway kernel. Email requests are read
52 * from stdin. The output is transferred to an MTA if 'From' is
53 * found in the header - or stdout if absent. No Z39.50 client is used.
68 static void put_esc_str (const char *s)
73 if (*s == '\\' && s[1])
78 fputc ('\n', reply_fd);
81 fputc ('\t', reply_fd);
91 if (*s != ' ' || !escape_flag)
99 static int reopen_target (void)
103 gw_log (GW_LOG_WARN, "urp", "Zass free...");
104 info.zass = zass_open (info.hostname, info.port);
107 fprintf (reply_fd, "%s %s:%d\n",
108 gw_res_get (info.kernel_res, "gw.err.connect",
109 "Cannot connect to target"),
110 info.hostname, info.port);
113 v = gw_res_get (info.kernel_res, "gw.description", NULL);
115 fprintf (reply_fd, "%s\n", v);
116 fprintf (reply_fd, "%s %s:%d\n",
117 gw_res_get (info.kernel_res, "gw.msg.connect",
118 "Connection established to"),
119 info.hostname, info.port);
121 fprintf (reply_fd, "%s:\n%s\n",
122 gw_res_get (info.kernel_res, "gw.msg.databases",
123 "Available databases"),
128 static char line_buf[LINE_MAX+1];
130 static struct command_word {
132 char *resource_suffix;
140 { "continue", "continue" },
141 { "status", "status" },
142 { "cancel", "cancel" },
143 { "target", "target" },
147 static int command_search (struct command_word *tab, struct ccl_token *cmd,
148 const char *resource_prefix)
152 assert (resource_prefix);
155 while (tab->default_value)
157 char *cp, command_names[60];
158 char resource_name[60];
161 sprintf (resource_name, "%s%s", resource_prefix,
162 tab->resource_suffix);
163 v = gw_res_get (info.kernel_res, resource_name, tab->default_value);
165 strcpy (command_names, v);
171 if ((split = strchr (cp, ' ')))
173 if (cmd->len == strlen(cp) &&
174 !memcmp (cmd->name, cp, cmd->len))
186 static struct error_no_struct {
189 } error_ccl_tab[] = {
191 { CCL_ERR_TERM_EXPECTED, "term.expected" },
192 { CCL_ERR_RP_EXPECTED, "rp.expected" },
193 { CCL_ERR_SETNAME_EXPECTED, "setname.expected" },
194 { CCL_ERR_OP_EXPECTED, "op.expected" },
195 { CCL_ERR_BAD_RP, "bad.rp" },
196 { CCL_ERR_UNKNOWN_QUAL, "unknown.qual" },
197 { CCL_ERR_DOUBLE_QUAL, "double.qual" },
198 { CCL_ERR_EQ_EXPECTED, "eq.expected" },
199 { CCL_ERR_BAD_RELATION, "bad.relation" },
200 { CCL_ERR_TRUNC_NOT_LEFT, "trunc.not.left" },
201 { CCL_ERR_TRUNC_NOT_BOTH, "trunc.not.both" },
202 { CCL_ERR_TRUNC_NOT_RIGHT, "trunc.not.right" },
206 static char *error_no_search (struct error_no_struct *tab, int no)
208 struct error_no_struct *p = tab;
209 while (p->resource_name)
212 return p->resource_name;
218 static int email_header (FILE *inf, char *from_str, char *subject_str)
222 while (fgets (line_buf, LINE_MAX, inf))
224 if (line_buf[0] == '\n')
226 if (strncmp (line_buf, "From ", 5) == 0)
227 sscanf (line_buf+4, "%s", from_str);
228 if (strncmp (line_buf, "Subject: ", 9) == 0 &&
229 sscanf (line_buf+9, "%s", subject_str+1) == 1)
230 strcpy (subject_str, line_buf+9);
235 static void help_general (void)
237 put_esc_str (gw_res_get (info.kernel_res, "gw.help.general",
238 "Commands available in this service:\n"));
241 static int exec_help (struct ccl_token *list)
246 put_esc_str (gw_res_get (info.kernel_res, "gw.help.target",
247 "target <name> - selects a given target\n"));
249 put_esc_str (gw_res_get (info.kernel_res, "gw.help.base",
250 "base <base>.. - selects databases\n"));
252 put_esc_str (gw_res_get (info.kernel_res, "gw.help.find",
253 "find <query> - performs a search request\n"));
255 put_esc_str (gw_res_get (info.kernel_res, "gw.help.show",
256 "show <spec> - retrieves and displays "
258 put_esc_str (gw_res_get (info.kernel_res, "gw.help.help",
259 "help - displays help\n"));
264 static int exec_find (struct ccl_token *list)
266 const struct zass_searchent *p;
267 struct gw_user_set *us;
269 struct ccl_rpn_node *rpn;
273 us = user_set_add ("Default", -1);
274 rpn = ccl_find (info.bibset, list, &error, &pos);
277 const char *v = NULL, *n;
280 fprintf (reply_fd, " %*s^ - ", pos - line_buf, " ");
282 n = error_no_search (error_ccl_tab, error);
285 sprintf (name, "gw.err.%s", n);
286 v = gw_res_get (info.kernel_res, name, NULL);
289 v = ccl_err_msg (error);
290 fprintf (reply_fd, "%s\n", v);
293 ccl_pr_tree (rpn, reply_fd);
294 fprintf (reply_fd, "\n");
296 if (!*info.databases)
298 fprintf (reply_fd, "%s\n",
299 gw_res_get (info.kernel_res, "gw.err.no.database",
300 "You must select database"));
303 gw_log (GW_LOG_DEBUG, "urp", "Searching in database %s",
306 p = zass_search (info.zass, rpn, "Default", info.databases);
309 if (p->errcode != -1)
311 fprintf (reply_fd, "%s %d: %s\n",
312 gw_res_get (info.kernel_res, "gw.msg.z39errcode",
313 "Z39.50 error code"),
314 p->errcode, p->errstring);
317 fprintf (reply_fd, "%d %s\n", p->num,
318 gw_res_get (info.kernel_res, "gw.msg.hits", "hit(s)"));
323 static int exec_target (struct ccl_token *list)
326 if (list->kind == CCL_TOK_EOL)
329 memcpy (info.target, list->name, len);
330 info.target [len] = '\0';
333 return reopen_target ();
336 static int exec_base (struct ccl_token *list)
338 struct ccl_token *li = list;
342 if (list->kind == CCL_TOK_EOL)
344 free (info.databases);
345 while (li->kind != CCL_TOK_EOL)
349 if (li->kind == CCL_TOK_COMMA)
352 info.databases = malloc (len);
353 assert (info.databases);
356 while (li->kind != CCL_TOK_EOL)
358 memcpy (info.databases+len, li->name, li->len);
360 info.databases[len++] = ',';
362 if (li->kind == CCL_TOK_COMMA)
365 info.databases[len-1] = '\0';
369 struct command_word show_tab [] =
376 static void present (const char *set, int offset, int number,
377 struct ccl_token *format_token)
379 const struct zass_presentent *zp;
384 max_number = atoi (gw_res_get (info.kernel_res, "gw.max.show",
386 if (number > max_number)
388 gw_log (GW_LOG_DEBUG, "urp", "present in set %s", set);
389 gw_log (GW_LOG_DEBUG, "urp", "present of %d records from offset %d",
391 zp = zass_present(info.zass, (char *) set, offset, number);
397 fprintf (reply_fd, gw_res_get (info.kernel_res,
401 fprintf (reply_fd, "\n");
402 for (i = 0, pp = zp->records; pp; pp = pp->next, i++)
406 const char *arg_ar[5];
408 fprintf (reply_fd, "--- %d/%d ---\n",
409 i+offset, offset+zp->num-1);
410 if (!gw_res_get (info.kernel_res, "gw.ignore.which", NULL))
412 if (pp->which == ZASS_REC_DIAG)
414 fprintf (reply_fd, "Record error %d: %s\n",
415 pp->errcode, pp->errstring);
418 else if (pp->which != ZASS_REC_USMARC)
420 fprintf (reply_fd, "Unknown record kind %d\n",
425 rec = iso2709_cvt (pp->record);
431 len = format_token->len;
432 memcpy (format_str, format_token->name, len);
433 format_str[len] = '\0';
435 if (info.fml && format_token &&
436 (!strcmp (format_str, "0") || !strcmp (format_str, "1")))
439 arg_ar[1] = format_str;
440 arg_ar[2] = " \\list";
441 arg_ar[3] = marc_to_str (info.fml, rec);
443 fml_exec_call_argv (info.fml, arg_ar);
446 iso2709_display (rec, reply_fd);
448 iso2709_display (rec, reply_fd);
453 fprintf (reply_fd, "Not a MARC record\n");
458 static int exec_show (struct ccl_token *list)
461 struct ccl_token *set_token = NULL;
462 struct ccl_token *format_token = NULL;
463 struct ccl_token *li = list;
464 int no_of_present = 0;
467 while (li->kind != CCL_TOK_EOL)
470 if (li->next->kind == CCL_TOK_EQ)
472 if (li->kind == CCL_TOK_SET) /* set = <name> ? */
479 modifier_no = command_search (show_tab, li, "ccl.token.");
482 fprintf (reply_fd, "Unknown modifier in show\n");
486 if (modifier_no == 1) /* f = <name> ? */
488 else if (modifier_no == 2) /* p = <name> ? */
490 if (li->kind != CCL_TOK_EOL /* p = <name> - <name> ? */
491 && li->next->kind == CCL_TOK_MINUS
492 && li->next->next != CCL_TOK_EOL)
498 fprintf (reply_fd, "%s\n", "Missing token after '='");
507 gw_log (GW_LOG_DEBUG, "urp", "Got set=%.*s", set_token->len,
510 gw_log (GW_LOG_DEBUG, "urp", "Got format=%.*s", format_token->len,
514 while (li->kind != CCL_TOK_EOL)
520 if (li->next->kind == CCL_TOK_EQ && li->kind != CCL_TOK_SET)
522 modifier_no = command_search (show_tab, li, "ccl.token.");
524 if (modifier_no == 2) /* p = <name> ? */
526 if (li->kind != CCL_TOK_EOL /* p = <name> - <name> ? */
527 && li->next->kind == CCL_TOK_MINUS
528 && li->next->next != CCL_TOK_EOL)
531 memcpy (tmp_str, li->name, len);
532 tmp_str [len] = '\0';
533 offset = atoi (tmp_str);
537 memcpy (tmp_str, li->name, len);
538 tmp_str [len] = '\0';
539 number = atoi (tmp_str) - offset + 1;
544 memcpy (tmp_str, li->name, len);
545 tmp_str [len] = '\0';
546 offset = atoi (tmp_str);
555 memcpy (tmp_str, li->name, len);
557 number = atoi (tmp_str);
561 if (offset > 0 && number > 0)
563 struct gw_user_set *us;
567 len = set_token->len;
568 memcpy (tmp_str, set_token->name, len);
570 us = user_set_search (tmp_str);
573 us = user_set_search (NULL);
574 if (us && us->hits != -1) /* proper result-set? */
576 if (offset <= us->hits)
578 if (offset+number-1 > us->hits)
579 number = us->hits - offset+1;
580 present (us->name, offset, number, format_token);
583 else if (!no_of_present) /* display error message once! */
585 fprintf (reply_fd, "%s\n",
586 gw_res_get (info.kernel_res, "gw.err.no.set",
587 "No result-set generated"));
592 if (!no_of_present) /* no records shown so far? */
594 struct gw_user_set *us;
597 us = user_set_search (NULL);
598 if (us && us->hits != -1) /* proper result-set? */
600 default_show = atoi (gw_res_get (info.kernel_res,
601 "gw.default.show", "20"));
602 if (us->hits > default_show)
603 present (us->name, 1, default_show, format_token);
604 else if (us->hits > 0)
605 present (us->name, 1, us->hits, format_token);
607 else /* display error message */
609 fprintf (reply_fd, "%s\n",
610 gw_res_get (info.kernel_res, "gw.err.no.set",
611 "No result-set generated"));
618 static int exec_command (const char *str)
620 struct ccl_token *cmd = ccl_tokenize (str);
623 if (cmd->kind != CCL_TOK_EOL &&
624 (no = command_search (command_tab, cmd, "ccl.command.")))
626 if (!info.zass && no != 9 && no != 4)
628 fprintf (reply_fd, "\n> %s", str);
629 if (!info.zass && (no == 1 || no == 2 || no == 3))
631 fprintf (reply_fd, "%s\n",
632 gw_res_get (info.kernel_res, "gw.err.no.target",
633 "No connection established - "
640 return exec_find (cmd->next);
642 return exec_show (cmd->next);
644 return exec_base (cmd->next);
646 return exec_help (cmd->next);
648 return exec_target (cmd->next);
650 fprintf (reply_fd, "%s\n",
651 gw_res_get (info.kernel_res, "gw.err.unimplemented",
652 "Not implemented yet"));
657 fprintf (reply_fd, "\n> %s", str);
658 fprintf (reply_fd, " ^ %s\n",
659 gw_res_get (info.kernel_res, "gw.err.unknown.command",
661 "Use help to see list of commands"));
669 char subject_str[128];
671 char *reply_fname = NULL;
673 if (email_header (inf, from_str, subject_str))
675 gw_log (GW_LOG_WARN, "urp", "No message body");
680 reply_fname = tempnam (gw_res_get (info.kernel_res,
681 "gw.reply.tmp.dir", NULL),
682 gw_res_get (info.kernel_res,
683 "gw.reply.tmp.prefix", "gwr"));
685 reply_fd = fopen (reply_fname, "w");
688 gw_log (GW_LOG_FATAL, "urp", "Cannot create %s",
692 fprintf (reply_fd, "Subject: ");
694 fprintf (reply_fd, "Z39.50 Re: %s", subject_str);
696 fprintf (reply_fd, "%s\n", gw_res_get (info.kernel_res,
699 fprintf (reply_fd, "\n");
700 gw_log (GW_LOG_DEBUG, "urp", "Mail from %s", from_str);
703 gw_log (GW_LOG_WARN, "urp", "No From in email header");
704 fprintf (reply_fd, "%s\n", gw_res_get (info.kernel_res, "gw.msg.greeting",
705 "Email->Z39.50 gateway"));
706 while (fgets (line_buf, LINE_MAX, inf))
708 if (line_buf[0] == '\n')
710 ccl_token_and = gw_res_get (info.kernel_res, "ccl.token.and", "and");
711 ccl_token_or = gw_res_get (info.kernel_res, "ccl.token.or", "or");
712 ccl_token_not = gw_res_get (info.kernel_res, "ccl.token.not", "not");
713 ccl_token_set = gw_res_get (info.kernel_res, "ccl.token.set", "set");
714 if (isalpha (line_buf[0]))
715 exec_command (line_buf);
720 fprintf (reply_fd, "%s\n", gw_res_get (info.kernel_res,
731 assert (reply_fname);
735 mta = gw_res_get (info.kernel_res, "gw.reply.mta",
736 "/usr/lib/sendmail");
737 sprintf (cmd, "%s %s < %s", mta, from_str, reply_fname);
739 mta_code = system (cmd);
741 gw_log (GW_LOG_FATAL, "urp", "Reply '%s' got exit code %d",
743 unlink (reply_fname);
744 gw_log (GW_LOG_DEBUG, "urp", "Normal session end of %s", from_str);