5 * Revision 1.16 1995/02/23 10:08:20 adam
6 * Added logging of all user commands.
8 * Revision 1.15 1995/02/23 08:32:17 adam
11 * Revision 1.13 1995/02/22 16:54:42 adam
12 * Qualifiers of LOC target updated. More logging messages.
14 * Revision 1.12 1995/02/22 15:51:51 adam
15 * Bug fix: swap of parameter number and offset in function present.
17 * Revision 1.11 1995/02/22 15:22:33 adam
18 * Much more checking of run-time state. Show command never retrieves
19 * more records than indicated by the previous search request. Help
20 * command available. The maximum number of records retrieved can be
23 * Revision 1.10 1995/02/22 08:51:35 adam
24 * Output function can be customized in fml, which is used to print
25 * the reply to reply_fd.
27 * Revision 1.9 1995/02/21 17:46:21 adam
30 * Revision 1.8 1995/02/21 12:12:00 adam
31 * Diagnostic record with error info. observed.
33 * Revision 1.7 1995/02/20 21:16:20 adam
34 * FML support. Bug fixes. Profile for drewdb.
36 * Revision 1.6 1995/02/17 14:41:14 quinn
37 * Added simple display of records.
39 * Revision 1.5 1995/02/17 14:22:13 adam
40 * First steps of CCL show command. Not finished yet.
42 * Revision 1.4 1995/02/17 09:08:36 adam
43 * Reply with subject. CCL base command implemented.
45 * Revision 1.3 1995/02/16 18:35:09 adam
46 * First use of Zdist library. Search requests are supported.
47 * Present requests are not supported yet.
49 * Revision 1.2 1995/02/16 13:21:00 adam
50 * Organization of resource files for targets and conversion
51 * language implemented.
53 * Revision 1.1 1995/02/15 17:45:30 adam
54 * First version of email gateway kernel. Email requests are read
55 * from stdin. The output is transferred to an MTA if 'From' is
56 * found in the header - or stdout if absent. No Z39.50 client is used.
71 static void put_esc_str (const char *s)
76 if (*s == '\\' && s[1])
81 fputc ('\n', reply_fd);
84 fputc ('\t', reply_fd);
94 if (*s != ' ' || !escape_flag)
102 static int reopen_target (void)
106 gw_log (GW_LOG_WARN, "urp", "Zass free...");
107 info.zass = zass_open (info.hostname, info.port);
110 fprintf (reply_fd, "%s %s:%d\n",
111 gw_res_get (info.kernel_res, "gw.err.connect",
112 "Cannot connect to target"),
113 info.hostname, info.port);
116 v = gw_res_get (info.kernel_res, "gw.description", NULL);
118 fprintf (reply_fd, "%s\n", v);
119 fprintf (reply_fd, "%s %s:%d\n",
120 gw_res_get (info.kernel_res, "gw.msg.connect",
121 "Connection established to"),
122 info.hostname, info.port);
124 fprintf (reply_fd, "%s:\n%s\n",
125 gw_res_get (info.kernel_res, "gw.msg.databases",
126 "Available databases"),
131 static char line_buf[LINE_MAX+1];
133 static struct command_word {
135 char *resource_suffix;
143 { "continue", "continue" },
144 { "status", "status" },
145 { "cancel", "cancel" },
146 { "target", "target" },
150 static int command_search (struct command_word *tab, struct ccl_token *cmd,
151 const char *resource_prefix)
155 assert (resource_prefix);
158 while (tab->default_value)
160 char *cp, command_names[60];
161 char resource_name[60];
164 sprintf (resource_name, "%s%s", resource_prefix,
165 tab->resource_suffix);
166 v = gw_res_get (info.kernel_res, resource_name, tab->default_value);
168 strcpy (command_names, v);
174 if ((split = strchr (cp, ' ')))
176 if (cmd->len == strlen(cp) &&
177 !memcmp (cmd->name, cp, cmd->len))
189 static struct error_no_struct {
192 } error_ccl_tab[] = {
194 { CCL_ERR_TERM_EXPECTED, "term.expected" },
195 { CCL_ERR_RP_EXPECTED, "rp.expected" },
196 { CCL_ERR_SETNAME_EXPECTED, "setname.expected" },
197 { CCL_ERR_OP_EXPECTED, "op.expected" },
198 { CCL_ERR_BAD_RP, "bad.rp" },
199 { CCL_ERR_UNKNOWN_QUAL, "unknown.qual" },
200 { CCL_ERR_DOUBLE_QUAL, "double.qual" },
201 { CCL_ERR_EQ_EXPECTED, "eq.expected" },
202 { CCL_ERR_BAD_RELATION, "bad.relation" },
203 { CCL_ERR_TRUNC_NOT_LEFT, "trunc.not.left" },
204 { CCL_ERR_TRUNC_NOT_BOTH, "trunc.not.both" },
205 { CCL_ERR_TRUNC_NOT_RIGHT, "trunc.not.right" },
209 static char *error_no_search (struct error_no_struct *tab, int no)
211 struct error_no_struct *p = tab;
212 while (p->resource_name)
215 return p->resource_name;
221 static int email_header (FILE *inf, char *from_str, char *subject_str)
225 while (fgets (line_buf, LINE_MAX, inf))
227 if (line_buf[0] == '\n')
229 if (strncmp (line_buf, "From ", 5) == 0)
230 sscanf (line_buf+4, "%s", from_str);
231 if (strncmp (line_buf, "Subject: ", 9) == 0 &&
232 sscanf (line_buf+9, "%s", subject_str+1) == 1)
233 strcpy (subject_str, line_buf+9);
238 static void help_general (void)
240 put_esc_str (gw_res_get (info.kernel_res, "gw.help.general",
241 "Commands available in this service:\n"));
244 static int exec_help (struct ccl_token *list)
249 put_esc_str (gw_res_get (info.kernel_res, "gw.help.target",
250 "target <name> - selects a given target\n"));
252 put_esc_str (gw_res_get (info.kernel_res, "gw.help.base",
253 "base <base>.. - selects databases\n"));
255 put_esc_str (gw_res_get (info.kernel_res, "gw.help.find",
256 "find <query> - performs a search request\n"));
258 put_esc_str (gw_res_get (info.kernel_res, "gw.help.show",
259 "show <spec> - retrieves and displays "
261 put_esc_str (gw_res_get (info.kernel_res, "gw.help.help",
262 "help - displays help\n"));
267 static int exec_find (struct ccl_token *list)
269 const struct zass_searchent *p;
270 struct gw_user_set *us;
272 struct ccl_rpn_node *rpn;
276 us = user_set_add ("Default", -1);
277 rpn = ccl_find (info.bibset, list, &error, &pos);
280 const char *v = NULL, *n;
283 fprintf (reply_fd, " %*s^ - ", pos - line_buf, " ");
285 n = error_no_search (error_ccl_tab, error);
288 sprintf (name, "gw.err.%s", n);
289 v = gw_res_get (info.kernel_res, name, NULL);
292 v = ccl_err_msg (error);
293 fprintf (reply_fd, "%s\n", v);
296 ccl_pr_tree (rpn, reply_fd);
297 fprintf (reply_fd, "\n");
299 if (!*info.databases)
301 fprintf (reply_fd, "%s\n",
302 gw_res_get (info.kernel_res, "gw.err.no.database",
303 "You must select database"));
306 gw_log (GW_LOG_DEBUG, "urp", "Searching in database %s",
309 p = zass_search (info.zass, rpn, "Default", info.databases);
312 if (p->errcode != -1)
314 fprintf (reply_fd, "%s %d: %s\n",
315 gw_res_get (info.kernel_res, "gw.msg.z39errcode",
316 "Z39.50 error code"),
317 p->errcode, p->errstring);
320 fprintf (reply_fd, "%d %s\n", p->num,
321 gw_res_get (info.kernel_res, "gw.msg.hits", "hit(s)"));
326 static int exec_target (struct ccl_token *list)
329 if (list->kind == CCL_TOK_EOL)
332 memcpy (info.target, list->name, len);
333 info.target [len] = '\0';
336 return reopen_target ();
339 static int exec_base (struct ccl_token *list)
341 struct ccl_token *li = list;
345 if (list->kind == CCL_TOK_EOL)
347 free (info.databases);
348 while (li->kind != CCL_TOK_EOL)
352 if (li->kind == CCL_TOK_COMMA)
355 info.databases = malloc (len);
356 assert (info.databases);
359 while (li->kind != CCL_TOK_EOL)
361 memcpy (info.databases+len, li->name, li->len);
363 info.databases[len++] = ',';
365 if (li->kind == CCL_TOK_COMMA)
368 info.databases[len-1] = '\0';
372 struct command_word show_tab [] =
379 static void present (const char *set, int offset, int number,
380 struct ccl_token *format_token)
382 const struct zass_presentent *zp;
387 max_number = atoi (gw_res_get (info.kernel_res, "gw.max.show",
389 if (number > max_number)
391 gw_log (GW_LOG_DEBUG, "urp", "present in set %s", set);
392 gw_log (GW_LOG_DEBUG, "urp", "present of %d records from offset %d",
394 zp = zass_present(info.zass, (char *) set, offset, number);
400 fprintf (reply_fd, gw_res_get (info.kernel_res,
404 fprintf (reply_fd, "\n");
405 for (i = 0, pp = zp->records; pp; pp = pp->next, i++)
409 const char *arg_ar[5];
411 fprintf (reply_fd, "--- %d/%d ---\n",
412 i+offset, offset+zp->num-1);
413 if (!gw_res_get (info.kernel_res, "gw.ignore.which", NULL))
415 if (pp->which == ZASS_REC_DIAG)
417 fprintf (reply_fd, "Record error %d: %s\n",
418 pp->errcode, pp->errstring);
421 else if (pp->which != ZASS_REC_USMARC)
423 fprintf (reply_fd, "Unknown record kind %d\n",
428 rec = iso2709_cvt (pp->record);
434 len = format_token->len;
435 memcpy (format_str, format_token->name, len);
436 format_str[len] = '\0';
438 if (info.fml && format_token &&
439 (!strcmp (format_str, "0") || !strcmp (format_str, "1")))
442 arg_ar[1] = format_str;
443 arg_ar[2] = " \\list";
444 arg_ar[3] = marc_to_str (info.fml, rec);
446 fml_exec_call_argv (info.fml, arg_ar);
449 iso2709_display (rec, reply_fd);
451 iso2709_display (rec, reply_fd);
456 fprintf (reply_fd, "Not a MARC record\n");
461 static int exec_show (struct ccl_token *list)
464 struct ccl_token *set_token = NULL;
465 struct ccl_token *format_token = NULL;
466 struct ccl_token *li = list;
467 int no_of_present = 0;
470 while (li->kind != CCL_TOK_EOL)
473 if (li->next->kind == CCL_TOK_EQ)
475 if (li->kind == CCL_TOK_SET) /* set = <name> ? */
482 modifier_no = command_search (show_tab, li, "ccl.token.");
485 fprintf (reply_fd, "Unknown modifier in show\n");
489 if (modifier_no == 1) /* f = <name> ? */
491 else if (modifier_no == 2) /* p = <name> ? */
493 if (li->kind != CCL_TOK_EOL /* p = <name> - <name> ? */
494 && li->next->kind == CCL_TOK_MINUS
495 && li->next->next != CCL_TOK_EOL)
501 fprintf (reply_fd, "%s\n", "Missing token after '='");
510 gw_log (GW_LOG_DEBUG, "urp", "Got set=%.*s", set_token->len,
513 gw_log (GW_LOG_DEBUG, "urp", "Got format=%.*s", format_token->len,
517 while (li->kind != CCL_TOK_EOL)
523 if (li->next->kind == CCL_TOK_EQ && li->kind != CCL_TOK_SET)
525 modifier_no = command_search (show_tab, li, "ccl.token.");
527 if (modifier_no == 2) /* p = <name> ? */
529 if (li->kind != CCL_TOK_EOL /* p = <name> - <name> ? */
530 && li->next->kind == CCL_TOK_MINUS
531 && li->next->next != CCL_TOK_EOL)
534 memcpy (tmp_str, li->name, len);
535 tmp_str [len] = '\0';
536 offset = atoi (tmp_str);
540 memcpy (tmp_str, li->name, len);
541 tmp_str [len] = '\0';
542 number = atoi (tmp_str) - offset + 1;
547 memcpy (tmp_str, li->name, len);
548 tmp_str [len] = '\0';
549 offset = atoi (tmp_str);
558 memcpy (tmp_str, li->name, len);
560 number = atoi (tmp_str);
564 if (offset > 0 && number > 0)
566 struct gw_user_set *us;
570 len = set_token->len;
571 memcpy (tmp_str, set_token->name, len);
573 us = user_set_search (tmp_str);
576 us = user_set_search (NULL);
577 if (us && us->hits != -1) /* proper result-set? */
579 if (offset <= us->hits)
581 if (offset+number-1 > us->hits)
582 number = us->hits - offset+1;
583 present (us->name, offset, number, format_token);
586 else if (!no_of_present) /* display error message once! */
588 fprintf (reply_fd, "%s\n",
589 gw_res_get (info.kernel_res, "gw.err.no.set",
590 "No result-set generated"));
595 if (!no_of_present) /* no records shown so far? */
597 struct gw_user_set *us;
600 us = user_set_search (NULL);
601 if (us && us->hits != -1) /* proper result-set? */
603 default_show = atoi (gw_res_get (info.kernel_res,
604 "gw.default.show", "20"));
605 if (us->hits > default_show)
606 present (us->name, 1, default_show, format_token);
607 else if (us->hits > 0)
608 present (us->name, 1, us->hits, format_token);
610 else /* display error message */
612 fprintf (reply_fd, "%s\n",
613 gw_res_get (info.kernel_res, "gw.err.no.set",
614 "No result-set generated"));
621 static int exec_command (const char *str)
623 struct ccl_token *cmd = ccl_tokenize (str);
626 if (cmd->kind != CCL_TOK_EOL &&
627 (no = command_search (command_tab, cmd, "ccl.command.")))
629 if (!info.zass && no != 9 && no != 4)
631 fprintf (reply_fd, "\n> %s\n", str);
632 if (!info.zass && (no == 1 || no == 2 || no == 3))
634 fprintf (reply_fd, "%s\n",
635 gw_res_get (info.kernel_res, "gw.err.no.target",
636 "No connection established - "
643 return exec_find (cmd->next);
645 return exec_show (cmd->next);
647 return exec_base (cmd->next);
649 return exec_help (cmd->next);
651 return exec_target (cmd->next);
653 fprintf (reply_fd, "%s\n",
654 gw_res_get (info.kernel_res, "gw.err.unimplemented",
655 "Not implemented yet"));
660 fprintf (reply_fd, "\n> %s", str);
661 fprintf (reply_fd, " ^ %s\n",
662 gw_res_get (info.kernel_res, "gw.err.unknown.command",
664 "Use help to see list of commands"));
672 char subject_str[128];
674 char *reply_fname = NULL;
676 if (email_header (inf, from_str, subject_str))
678 gw_log (GW_LOG_WARN, "urp", "No message body");
683 reply_fname = tempnam (gw_res_get (info.kernel_res,
684 "gw.reply.tmp.dir", NULL),
685 gw_res_get (info.kernel_res,
686 "gw.reply.tmp.prefix", "gwr"));
688 reply_fd = fopen (reply_fname, "w");
691 gw_log (GW_LOG_FATAL, "urp", "Cannot create %s",
695 fprintf (reply_fd, "Subject: ");
697 fprintf (reply_fd, "Z39.50 Re: %s", subject_str);
699 fprintf (reply_fd, "%s\n", gw_res_get (info.kernel_res,
702 fprintf (reply_fd, "\n");
703 gw_log (GW_LOG_DEBUG, "urp", "Mail from %s", from_str);
706 gw_log (GW_LOG_WARN, "urp", "No From in email header");
707 fprintf (reply_fd, "%s\n", gw_res_get (info.kernel_res, "gw.msg.greeting",
708 "Email->Z39.50 gateway"));
709 while (fgets (line_buf, LINE_MAX, inf))
713 if (line_buf[0] == '\n')
715 if ((cp = strchr (line_buf, '\n')))
717 gw_log (GW_LOG_ACCT, "urp", "cmd: %s", line_buf);
718 ccl_token_and = gw_res_get (info.kernel_res, "ccl.token.and", "and");
719 ccl_token_or = gw_res_get (info.kernel_res, "ccl.token.or", "or");
720 ccl_token_not = gw_res_get (info.kernel_res, "ccl.token.not", "not");
721 ccl_token_set = gw_res_get (info.kernel_res, "ccl.token.set", "set");
722 if (isalpha (line_buf[0]))
723 exec_command (line_buf);
728 fprintf (reply_fd, "%s\n", gw_res_get (info.kernel_res,
739 assert (reply_fname);
743 mta = gw_res_get (info.kernel_res, "gw.reply.mta",
744 "/usr/lib/sendmail");
745 sprintf (cmd, "%s %s < %s", mta, from_str, reply_fname);
747 mta_code = system (cmd);
749 gw_log (GW_LOG_FATAL, "urp", "Reply '%s' got exit code %d",
751 unlink (reply_fname);
752 gw_log (GW_LOG_DEBUG, "urp", "Normal session for user %s", from_str);