5 * Revision 1.13 1995/02/22 16:54:42 adam
6 * Qualifiers of LOC target updated. More logging messages.
8 * Revision 1.12 1995/02/22 15:51:51 adam
9 * Bug fix: swap of parameter number and offset in function present.
11 * Revision 1.11 1995/02/22 15:22:33 adam
12 * Much more checking of run-time state. Show command never retrieves
13 * more records than indicated by the previous search request. Help
14 * command available. The maximum number of records retrieved can be
17 * Revision 1.10 1995/02/22 08:51:35 adam
18 * Output function can be customized in fml, which is used to print
19 * the reply to reply_fd.
21 * Revision 1.9 1995/02/21 17:46:21 adam
24 * Revision 1.8 1995/02/21 12:12:00 adam
25 * Diagnostic record with error info. observed.
27 * Revision 1.7 1995/02/20 21:16:20 adam
28 * FML support. Bug fixes. Profile for drewdb.
30 * Revision 1.6 1995/02/17 14:41:14 quinn
31 * Added simple display of records.
33 * Revision 1.5 1995/02/17 14:22:13 adam
34 * First steps of CCL show command. Not finished yet.
36 * Revision 1.4 1995/02/17 09:08:36 adam
37 * Reply with subject. CCL base command implemented.
39 * Revision 1.3 1995/02/16 18:35:09 adam
40 * First use of Zdist library. Search requests are supported.
41 * Present requests are not supported yet.
43 * Revision 1.2 1995/02/16 13:21:00 adam
44 * Organization of resource files for targets and conversion
45 * language implemented.
47 * Revision 1.1 1995/02/15 17:45:30 adam
48 * First version of email gateway kernel. Email requests are read
49 * from stdin. The output is transferred to an MTA if 'From' is
50 * found in the header - or stdout if absent. No Z39.50 client is used.
65 static void put_esc_str (const char *s)
70 if (*s == '\\' && s[1])
75 fputc ('\n', reply_fd);
78 fputc ('\t', reply_fd);
88 if (*s != ' ' || !escape_flag)
96 static int reopen_target (void)
100 gw_log (GW_LOG_WARN, "urp", "Zass free...");
101 info.zass = zass_open (info.hostname, info.port);
104 fprintf (reply_fd, "%s %s:%d\n",
105 gw_res_get (info.kernel_res, "gw.err.connect",
106 "Cannot connect to target"),
107 info.hostname, info.port);
110 v = gw_res_get (info.kernel_res, "gw.description", NULL);
112 fprintf (reply_fd, "%s\n", v);
113 fprintf (reply_fd, "%s %s:%d\n",
114 gw_res_get (info.kernel_res, "gw.msg.connect",
115 "Connection established to"),
116 info.hostname, info.port);
118 fprintf (reply_fd, "%s:\n%s\n",
119 gw_res_get (info.kernel_res, "gw.msg.databases",
120 "Available databases"),
125 static char line_buf[LINE_MAX+1];
127 static struct command_word {
129 char *resource_suffix;
137 { "continue", "continue" },
138 { "status", "status" },
139 { "cancel", "cancel" },
140 { "target", "target" },
144 static int command_search (struct command_word *tab, struct ccl_token *cmd,
145 const char *resource_prefix)
149 assert (resource_prefix);
152 while (tab->default_value)
154 char *cp, command_names[60];
155 char resource_name[60];
158 sprintf (resource_name, "%s%s", resource_prefix,
159 tab->resource_suffix);
160 v = gw_res_get (info.kernel_res, resource_name, tab->default_value);
162 strcpy (command_names, v);
168 if ((split = strchr (cp, ' ')))
170 if (cmd->len == strlen(cp) &&
171 !memcmp (cmd->name, cp, cmd->len))
183 static struct error_no_struct {
186 } error_ccl_tab[] = {
188 { CCL_ERR_TERM_EXPECTED, "term.expected" },
189 { CCL_ERR_RP_EXPECTED, "rp.expected" },
190 { CCL_ERR_SETNAME_EXPECTED, "setname.expected" },
191 { CCL_ERR_OP_EXPECTED, "op.expected" },
192 { CCL_ERR_BAD_RP, "bad.rp" },
193 { CCL_ERR_UNKNOWN_QUAL, "unknown.qual" },
194 { CCL_ERR_DOUBLE_QUAL, "double.qual" },
195 { CCL_ERR_EQ_EXPECTED, "eq.expected" },
196 { CCL_ERR_BAD_RELATION, "bad.relation" },
197 { CCL_ERR_TRUNC_NOT_LEFT, "trunc.not.left" },
198 { CCL_ERR_TRUNC_NOT_BOTH, "trunc.not.both" },
199 { CCL_ERR_TRUNC_NOT_RIGHT, "trunc.not.right" },
203 static char *error_no_search (struct error_no_struct *tab, int no)
205 struct error_no_struct *p = tab;
206 while (p->resource_name)
209 return p->resource_name;
215 static int email_header (FILE *inf, char *from_str, char *subject_str)
219 while (fgets (line_buf, LINE_MAX, inf))
221 if (line_buf[0] == '\n')
223 if (strncmp (line_buf, "From ", 5) == 0)
224 sscanf (line_buf+4, "%s", from_str);
225 if (strncmp (line_buf, "Subject: ", 9) == 0 &&
226 sscanf (line_buf+9, "%s", subject_str+1) == 1)
227 strcpy (subject_str, line_buf+9);
232 static void help_general (void)
234 put_esc_str (gw_res_get (info.kernel_res, "gw.help.general",
235 "Commands available in this service:\n"));
238 static int exec_help (struct ccl_token *list)
243 put_esc_str (gw_res_get (info.kernel_res, "gw.help.target",
244 "target <name> - selects a given target\n"));
246 put_esc_str (gw_res_get (info.kernel_res, "gw.help.base",
247 "base <base>.. - selects databases\n"));
249 put_esc_str (gw_res_get (info.kernel_res, "gw.help.find",
250 "find <query> - performs a search request\n"));
252 put_esc_str (gw_res_get (info.kernel_res, "gw.help.show",
253 "show <spec> - retrieves and displays "
255 put_esc_str (gw_res_get (info.kernel_res, "gw.help.help",
256 "help - displays help\n"));
261 static int exec_find (struct ccl_token *list)
263 const struct zass_searchent *p;
264 struct gw_user_set *us;
266 struct ccl_rpn_node *rpn;
270 us = user_set_add ("Default", -1);
271 rpn = ccl_find (info.bibset, list, &error, &pos);
274 const char *v = NULL, *n;
277 fprintf (reply_fd, " %*s^ - ", pos - line_buf, " ");
279 n = error_no_search (error_ccl_tab, error);
282 sprintf (name, "gw.err.%s", n);
283 v = gw_res_get (info.kernel_res, name, NULL);
286 v = ccl_err_msg (error);
287 fprintf (reply_fd, "%s\n", v);
290 ccl_pr_tree (rpn, reply_fd);
291 fprintf (reply_fd, "\n");
293 if (!*info.databases)
295 fprintf (reply_fd, "%s\n",
296 gw_res_get (info.kernel_res, "gw.err.no.database",
297 "You must select database"));
300 gw_log (GW_LOG_DEBUG, "urp", "Searching in database %s",
303 p = zass_search (info.zass, rpn, "Default", info.databases);
306 if (p->errcode != -1)
308 fprintf (reply_fd, "%s %d: %s\n",
309 gw_res_get (info.kernel_res, "gw.msg.z39errcode",
310 "Z39.50 error code"),
311 p->errcode, p->errstring);
314 fprintf (reply_fd, "%d %s\n", p->num,
315 gw_res_get (info.kernel_res, "gw.msg.hits", "hit(s)"));
320 static int exec_target (struct ccl_token *list)
323 if (list->kind == CCL_TOK_EOL)
326 memcpy (info.target, list->name, len);
327 info.target [len] = '\0';
330 return reopen_target ();
333 static int exec_base (struct ccl_token *list)
335 struct ccl_token *li = list;
339 if (list->kind == CCL_TOK_EOL)
341 free (info.databases);
342 while (li->kind != CCL_TOK_EOL)
346 if (li->kind == CCL_TOK_COMMA)
349 info.databases = malloc (len);
350 assert (info.databases);
353 while (li->kind != CCL_TOK_EOL)
355 memcpy (info.databases+len, li->name, li->len);
357 info.databases[len++] = ',';
359 if (li->kind == CCL_TOK_COMMA)
362 info.databases[len-1] = '\0';
366 struct command_word show_tab [] =
373 static void present (const char *set, int offset, int number,
374 struct ccl_token *format_token)
376 const struct zass_presentent *zp;
381 max_number = atoi (gw_res_get (info.kernel_res, "gw.max.show",
383 if (number > max_number)
385 gw_log (GW_LOG_DEBUG, "urp", "present in set %s", set);
386 gw_log (GW_LOG_DEBUG, "urp", "present of %d records from offset %d",
388 zp = zass_present(info.zass, (char *) set, offset, number);
394 fprintf (reply_fd, gw_res_get (info.kernel_res,
398 fprintf (reply_fd, "\n");
399 for (i = 0, pp = zp->records; pp; pp = pp->next, i++)
403 const char *arg_ar[5];
405 fprintf (reply_fd, "--- %d/%d ---\n",
406 i+offset, offset+zp->num-1);
407 if (!gw_res_get (info.kernel_res, "gw.ignore.which", NULL))
409 if (pp->which == ZASS_REC_DIAG)
411 fprintf (reply_fd, "Record error %d: %s\n",
412 pp->errcode, pp->errstring);
415 else if (pp->which != ZASS_REC_USMARC)
417 fprintf (reply_fd, "Unknown record kind %d\n",
422 rec = iso2709_cvt (pp->record);
428 len = format_token->len;
429 memcpy (format_str, format_token->name, len);
430 format_str[len] = '\0';
432 if (info.fml && format_token &&
433 (!strcmp (format_str, "0") || !strcmp (format_str, "1")))
436 arg_ar[1] = format_str;
437 arg_ar[2] = " \\list";
438 arg_ar[3] = marc_to_str (info.fml, rec);
440 fml_exec_call_argv (info.fml, arg_ar);
443 iso2709_display (rec, reply_fd);
445 iso2709_display (rec, reply_fd);
450 fprintf (reply_fd, "Not a MARC record\n");
455 static int exec_show (struct ccl_token *list)
458 struct ccl_token *set_token = NULL;
459 struct ccl_token *format_token = NULL;
460 struct ccl_token *li = list;
461 int no_of_present = 0;
464 while (li->kind != CCL_TOK_EOL)
467 if (li->next->kind == CCL_TOK_EQ)
469 if (li->kind == CCL_TOK_SET) /* set = <name> ? */
476 modifier_no = command_search (show_tab, li, "ccl.token.");
479 fprintf (reply_fd, "Unknown modifier in show\n");
483 if (modifier_no == 1) /* f = <name> ? */
485 else if (modifier_no == 2) /* p = <name> ? */
487 if (li->kind != CCL_TOK_EOL /* p = <name> - <name> ? */
488 && li->next->kind == CCL_TOK_MINUS
489 && li->next->next != CCL_TOK_EOL)
495 fprintf (reply_fd, "%s\n", "Missing token after '='");
504 gw_log (GW_LOG_DEBUG, "urp", "Got set=%.*s", set_token->len,
507 gw_log (GW_LOG_DEBUG, "urp", "Got format=%.*s", format_token->len,
511 while (li->kind != CCL_TOK_EOL)
517 if (li->next->kind == CCL_TOK_EQ && li->kind != CCL_TOK_SET)
519 modifier_no = command_search (show_tab, li, "ccl.token.");
521 if (modifier_no == 2) /* p = <name> ? */
523 if (li->kind != CCL_TOK_EOL /* p = <name> - <name> ? */
524 && li->next->kind == CCL_TOK_MINUS
525 && li->next->next != CCL_TOK_EOL)
528 memcpy (tmp_str, li->name, len);
529 tmp_str [len] = '\0';
530 offset = atoi (tmp_str);
534 memcpy (tmp_str, li->name, len);
535 tmp_str [len] = '\0';
536 number = atoi (tmp_str) - offset + 1;
541 memcpy (tmp_str, li->name, len);
542 tmp_str [len] = '\0';
543 offset = atoi (tmp_str);
552 memcpy (tmp_str, li->name, len);
554 number = atoi (tmp_str);
558 if (offset > 0 && number > 0)
560 struct gw_user_set *us;
564 len = set_token->len;
565 memcpy (tmp_str, set_token->name, len);
567 us = user_set_search (tmp_str);
570 us = user_set_search (NULL);
571 if (us && us->hits != -1) /* proper result-set? */
573 if (offset <= us->hits)
575 if (offset+number-1 > us->hits)
576 number = us->hits - offset+1;
577 present (us->name, offset, number, format_token);
580 else if (!no_of_present) /* display error message once! */
582 fprintf (reply_fd, "%s\n",
583 gw_res_get (info.kernel_res, "gw.err.no.set",
584 "No result-set generated"));
589 if (!no_of_present) /* no records shown so far? */
591 struct gw_user_set *us;
594 us = user_set_search (NULL);
595 if (us && us->hits != -1) /* proper result-set? */
597 default_show = atoi (gw_res_get (info.kernel_res,
598 "gw.default.show", "20"));
599 if (us->hits > default_show)
600 present (us->name, 1, default_show, format_token);
601 else if (us->hits > 0)
602 present (us->name, 1, us->hits, format_token);
604 else /* display error message */
606 fprintf (reply_fd, "%s\n",
607 gw_res_get (info.kernel_res, "gw.err.no.set",
608 "No result-set generated"));
615 static int exec_command (const char *str)
617 struct ccl_token *cmd = ccl_tokenize (str);
620 if (cmd->kind != CCL_TOK_EOL &&
621 (no = command_search (command_tab, cmd, "ccl.command.")))
623 if (!info.zass && no != 9 && no != 4)
625 fprintf (reply_fd, "\n> %s", str);
626 if (!info.zass && (no == 1 || no == 2 || no == 3))
628 fprintf (reply_fd, "%s\n",
629 gw_res_get (info.kernel_res, "gw.err.no.target",
630 "No connection established - "
637 return exec_find (cmd->next);
639 return exec_show (cmd->next);
641 return exec_base (cmd->next);
643 return exec_help (cmd->next);
645 return exec_target (cmd->next);
647 fprintf (reply_fd, "%s\n",
648 gw_res_get (info.kernel_res, "gw.err.unimplemented",
649 "Not implemented yet"));
654 fprintf (reply_fd, "\n> %s", str);
655 fprintf (reply_fd, " ^ %s\n",
656 gw_res_get (info.kernel_res, "gw.err.unknown.command",
658 "Use help to see list of commands"));
666 char subject_str[128];
668 char *reply_fname = NULL;
670 if (email_header (inf, from_str, subject_str))
672 gw_log (GW_LOG_WARN, "urp", "No message body");
677 reply_fname = tempnam (gw_res_get (info.kernel_res,
678 "gw.reply.tmp.dir", NULL),
679 gw_res_get (info.kernel_res,
680 "gw.reply.tmp.prefix", "gwr"));
682 reply_fd = fopen (reply_fname, "w");
685 gw_log (GW_LOG_FATAL, "urp", "Cannot create %s",
689 fprintf (reply_fd, "Subject: ");
691 fprintf (reply_fd, "Z39.50 Re: %s", subject_str);
693 fprintf (reply_fd, "%s\n", gw_res_get (info.kernel_res,
696 fprintf (reply_fd, "\n");
697 gw_log (GW_LOG_DEBUG, "urp", "Mail from %s", from_str);
700 gw_log (GW_LOG_WARN, "urp", "No From in email header");
701 fprintf (reply_fd, "%s\n", gw_res_get (info.kernel_res, "gw.msg.greeting",
702 "Email->Z39.50 gateway"));
703 while (fgets (line_buf, LINE_MAX, inf))
705 if (line_buf[0] == '\n')
707 ccl_token_and = gw_res_get (info.kernel_res, "ccl.token.and", "and");
708 ccl_token_or = gw_res_get (info.kernel_res, "ccl.token.or", "or");
709 ccl_token_not = gw_res_get (info.kernel_res, "ccl.token.not", "not");
710 ccl_token_set = gw_res_get (info.kernel_res, "ccl.token.set", "set");
711 if (isalpha (line_buf[0]))
712 exec_command (line_buf);
717 fprintf (reply_fd, "%s\n", gw_res_get (info.kernel_res,
728 assert (reply_fname);
732 mta = gw_res_get (info.kernel_res, "gw.reply.mta",
733 "/usr/lib/sendmail");
734 sprintf (cmd, "%s %s < %s", mta, from_str, reply_fname);
736 mta_code = system (cmd);
738 gw_log (GW_LOG_FATAL, "urp", "Reply '%s' got exit code %d",
740 unlink (reply_fname);
741 gw_log (GW_LOG_DEBUG, "urp", "Normal session end of %s", from_str);