5 * Revision 1.9 1995/02/21 17:46:21 adam
8 * Revision 1.8 1995/02/21 12:12:00 adam
9 * Diagnostic record with error info. observed.
11 * Revision 1.7 1995/02/20 21:16:20 adam
12 * FML support. Bug fixes. Profile for drewdb.
14 * Revision 1.6 1995/02/17 14:41:14 quinn
15 * Added simple display of records.
17 * Revision 1.5 1995/02/17 14:22:13 adam
18 * First steps of CCL show command. Not finished yet.
20 * Revision 1.4 1995/02/17 09:08:36 adam
21 * Reply with subject. CCL base command implemented.
23 * Revision 1.3 1995/02/16 18:35:09 adam
24 * First use of Zdist library. Search requests are supported.
25 * Present requests are not supported yet.
27 * Revision 1.2 1995/02/16 13:21:00 adam
28 * Organization of resource files for targets and conversion
29 * language implemented.
31 * Revision 1.1 1995/02/15 17:45:30 adam
32 * First version of email gateway kernel. Email requests are read
33 * from stdin. The output is transferred to an MTA if 'From' is
34 * found in the header - or stdout if absent. No Z39.50 client is used.
49 static int reopen_target (void)
53 gw_log (GW_LOG_WARN, "urp", "Zass free...");
54 info.zass = zass_open (info.hostname, info.port);
57 fprintf (reply_fd, "%s %s:%d\n",
58 gw_res_get (info.kernel_res, "gw.err.connect",
59 "Cannot connect to target"),
60 info.hostname, info.port);
63 v = gw_res_get (info.kernel_res, "gw.description", NULL);
65 fprintf (reply_fd, "%s\n", v);
66 fprintf (reply_fd, "%s %s:%d\n",
67 gw_res_get (info.kernel_res, "gw.msg.connect",
68 "Connection established to"),
69 info.hostname, info.port);
71 fprintf (reply_fd, "%s:\n%s\n",
72 gw_res_get (info.kernel_res, "gw.msg.databases",
73 "Available databases"),
78 static char line_buf[LINE_MAX+1];
80 static struct command_word {
82 char *resource_suffix;
90 { "continue", "continue" },
91 { "status", "status" },
92 { "cancel", "cancel" },
93 { "target", "target" },
97 static int command_search (struct command_word *tab, struct ccl_token *cmd,
98 const char *resource_prefix)
102 assert (resource_prefix);
105 while (tab->default_value)
107 char *cp, command_names[60];
108 char resource_name[60];
111 sprintf (resource_name, "%s%s", resource_prefix,
112 tab->resource_suffix);
113 v = gw_res_get (info.kernel_res, resource_name, tab->default_value);
115 strcpy (command_names, v);
121 if ((split = strchr (cp, ' ')))
123 if (cmd->len == strlen(cp) &&
124 !memcmp (cmd->name, cp, cmd->len))
136 static struct error_no_struct {
139 } error_ccl_tab[] = {
141 { CCL_ERR_TERM_EXPECTED, "term.expected" },
142 { CCL_ERR_RP_EXPECTED, "rp.expected" },
143 { CCL_ERR_SETNAME_EXPECTED, "setname.expected" },
144 { CCL_ERR_OP_EXPECTED, "op.expected" },
145 { CCL_ERR_BAD_RP, "bad.rp" },
146 { CCL_ERR_UNKNOWN_QUAL, "unknown.qual" },
147 { CCL_ERR_DOUBLE_QUAL, "double.qual" },
148 { CCL_ERR_EQ_EXPECTED, "eq.expected" },
149 { CCL_ERR_BAD_RELATION, "bad.relation" },
150 { CCL_ERR_TRUNC_NOT_LEFT, "trunc.not.left" },
151 { CCL_ERR_TRUNC_NOT_BOTH, "trunc.not.both" },
152 { CCL_ERR_TRUNC_NOT_RIGHT, "trunc.not.right" },
156 static char *error_no_search (struct error_no_struct *tab, int no)
158 struct error_no_struct *p = tab;
159 while (p->resource_name)
162 return p->resource_name;
168 static int email_header (FILE *inf, char *from_str, char *subject_str)
172 while (fgets (line_buf, LINE_MAX, inf))
174 if (line_buf[0] == '\n')
176 if (strncmp (line_buf, "From ", 5) == 0)
177 sscanf (line_buf+4, "%s", from_str);
178 if (strncmp (line_buf, "Subject: ", 9) == 0 &&
179 sscanf (line_buf+9, "%s", subject_str+1) == 1)
180 strcpy (subject_str, line_buf+9);
185 static int exec_find (struct ccl_token *list)
187 const struct zass_searchent *p;
189 struct ccl_rpn_node *rpn;
193 rpn = ccl_find (info.bibset, list, &error, &pos);
196 const char *v = NULL, *n;
199 fprintf (reply_fd, " %*s^ - ", pos - line_buf, " ");
201 n = error_no_search (error_ccl_tab, error);
204 sprintf (name, "gw.err.%s", n);
205 v = gw_res_get (info.kernel_res, name, NULL);
208 v = ccl_err_msg (error);
209 fprintf (reply_fd, "%s\n", v);
212 ccl_pr_tree (rpn, reply_fd);
213 fprintf (reply_fd, "\n");
217 if (!*info.databases)
219 fprintf (reply_fd, "%s\n",
220 gw_res_get (info.kernel_res, "gw.err.no.database",
221 "You must select database"));
224 gw_log (GW_LOG_DEBUG, "urp", "Searching in database %s",
226 p = zass_search (info.zass, rpn, "Default", info.databases);
229 if (p->errcode != -1)
230 fprintf (reply_fd, "%s %d: %s\n",
231 gw_res_get (info.kernel_res, "gw.msg.z39errcode",
232 "Z39.50 error code"),
233 p->errcode, p->errstring);
235 fprintf (reply_fd, "%d %s\n", p->num,
236 gw_res_get (info.kernel_res, "gw.msg.hits", "hit(s)"));
240 static int exec_target (struct ccl_token *list)
243 if (list->kind == CCL_TOK_EOL)
246 memcpy (info.target, list->name, len);
247 info.target [len] = '\0';
250 return reopen_target ();
253 static int exec_base (struct ccl_token *list)
255 struct ccl_token *li = list;
258 if (list->kind == CCL_TOK_EOL)
260 free (info.databases);
261 while (li->kind != CCL_TOK_EOL)
265 if (li->kind == CCL_TOK_COMMA)
268 info.databases = malloc (len);
269 assert (info.databases);
272 while (li->kind != CCL_TOK_EOL)
274 memcpy (info.databases+len, li->name, li->len);
276 info.databases[len++] = ',';
278 if (li->kind == CCL_TOK_COMMA)
281 info.databases[len-1] = '\0';
285 struct command_word show_tab [] =
292 static int exec_show (struct ccl_token *list)
294 const struct zass_presentent *zp;
296 struct ccl_token *set_token = NULL;
297 struct ccl_token *format_token = NULL;
298 struct ccl_token *li = list;
300 if (list->kind == CCL_TOK_EOL)
304 while (li->kind != CCL_TOK_EOL)
307 if (li->next->kind == CCL_TOK_EQ)
309 if (li->kind == CCL_TOK_SET) /* set = <name> ? */
316 modifier_no = command_search (show_tab, li, "ccl.token.");
319 fprintf (reply_fd, "Unknown modifier in show\n");
323 if (modifier_no == 1) /* f = <name> ? */
325 else if (modifier_no == 2) /* p = <name> ? */
327 if (li->kind != CCL_TOK_EOL /* p = <name> - <name> ? */
328 && li->next->kind == CCL_TOK_MINUS
329 && li->next->next != CCL_TOK_EOL)
339 gw_log (GW_LOG_DEBUG, "urp", "Got set=%.*s", set_token->len,
342 gw_log (GW_LOG_DEBUG, "urp", "Got format=%.*s", format_token->len,
346 while (li->kind != CCL_TOK_EOL)
352 if (li->next->kind == CCL_TOK_EQ && li->kind != CCL_TOK_SET)
354 modifier_no = command_search (show_tab, li, "ccl.token.");
356 if (modifier_no == 2) /* p = <name> ? */
358 if (li->kind != CCL_TOK_EOL /* p = <name> - <name> ? */
359 && li->next->kind == CCL_TOK_MINUS
360 && li->next->next != CCL_TOK_EOL)
363 memcpy (num_str, li->name, len);
364 num_str [len] = '\0';
365 offset = atoi (num_str);
369 memcpy (num_str, li->name, len);
370 num_str [len] = '\0';
371 number = atoi (num_str) - offset + 1;
376 memcpy (num_str, li->name, len);
377 num_str [len] = '\0';
378 offset = atoi (num_str);
387 memcpy (num_str, li->name, len);
389 number = atoi (num_str);
393 if (offset > 0 && number > 0)
397 len = set_token->len;
398 memcpy (num_str, set_token->name, len);
402 strcpy (num_str, "Default");
403 gw_log (GW_LOG_DEBUG, "urp", "zass_present of %d records from"
404 " offset %d in set %s", number, offset, num_str);
405 zp = zass_present(info.zass, num_str, offset, number);
411 fprintf (reply_fd, gw_res_get (info.kernel_res,
415 fprintf (reply_fd, "\n");
416 for (i = 0, pp = zp->records; pp; pp = pp->next, i++)
420 const char *arg_ar[5];
422 fprintf (reply_fd, "--- %d/%d ---\n",
423 i+offset, offset+zp->num-1);
424 if (pp->which == ZASS_REC_DIAG)
426 fprintf (reply_fd, "Record error %d: %s\n",
427 pp->errcode, pp->errstring);
430 else if (pp->which != ZASS_REC_USMARC)
432 fprintf (reply_fd, "Unknown record kind %d\n",
436 rec = iso2709_cvt (pp->record);
440 len = format_token->len;
441 memcpy (num_str, format_token->name, len);
445 (!strcmp (num_str, "0") || !strcmp (num_str, "1")))
449 arg_ar[2] = " \\list";
450 arg_ar[3] = marc_to_str (info.fml, rec);
452 fml_exec_call_argv (info.fml, arg_ar);
455 iso2709_display (rec, reply_fd);
457 iso2709_display (rec, reply_fd);
466 memcpy (num_str, list->name, len);
469 num = atoi (num_str);
472 gw_log (GW_LOG_DEBUG, "urp", "zass_present of %d records", num);
473 zp = zass_present(info.zass, "Default", 1, num);
479 fprintf (reply_fd, gw_res_get (info.kernel_res,
480 "gw.msg.records", "Got %d records"),
482 fprintf (reply_fd, "\n");
483 for (i = 1, pp = zp->records; pp; pp = pp->next, i++)
486 const char *arg_ar[3];
488 Iso2709Rec rec = iso2709_cvt (pp->record);
490 fprintf (reply_fd, "--- %d/%d ---\n", i, zp->num);
492 arg_ar[0] = "\\f0 \\list";
493 arg_ar[1] = marc_to_str (info.fml, rec);
495 fml_exec_call_argv (info.fml, arg_ar);
497 iso2709_display (rec, reply_fd);
506 static int exec_command (const char *str)
508 struct ccl_token *cmd = ccl_tokenize (str);
511 if (cmd->kind != CCL_TOK_EOL &&
512 (no = command_search (command_tab, cmd, "ccl.command.")))
514 if (!info.zass && no != 9)
516 fprintf (reply_fd, "\n> %s", str);
520 return exec_find (cmd->next);
522 return exec_show (cmd->next);
524 return exec_base (cmd->next);
526 return exec_target (cmd->next);
528 fprintf (reply_fd, "%s\n",
529 gw_res_get (info.kernel_res, "gw.err.unimplemented",
530 "Not implemented yet"));
535 fprintf (reply_fd, "\n> %s", str);
536 fprintf (reply_fd, " ^ %s\n",
537 gw_res_get (info.kernel_res, "gw.err.unknown.command",
546 char subject_str[128];
548 char *reply_fname = NULL;
550 if (email_header (inf, from_str, subject_str))
552 gw_log (GW_LOG_WARN, "urp", "No message body");
557 reply_fname = tempnam (gw_res_get (info.kernel_res,
558 "gw.reply.tmp.dir", NULL),
559 gw_res_get (info.kernel_res,
560 "gw.reply.tmp.prefix", "gwr"));
562 reply_fd = fopen (reply_fname, "w");
565 gw_log (GW_LOG_FATAL, "urp", "Cannot create %s",
569 fprintf (reply_fd, "Subject: ");
571 fprintf (reply_fd, "Z39.50 Re: %s", subject_str);
573 fprintf (reply_fd, "%s\n", gw_res_get (info.kernel_res,
576 fprintf (reply_fd, "\n");
579 gw_log (GW_LOG_WARN, "urp", "No From in email header");
580 fprintf (reply_fd, "%s\n", gw_res_get (info.kernel_res, "gw.msg.greeting",
581 "Email->Z39.50 gateway"));
582 while (fgets (line_buf, LINE_MAX, inf))
584 if (line_buf[0] == '\n')
586 ccl_token_and = gw_res_get (info.kernel_res, "ccl.token.and", "and");
587 ccl_token_or = gw_res_get (info.kernel_res, "ccl.token.or", "or");
588 ccl_token_not = gw_res_get (info.kernel_res, "ccl.token.not", "not");
589 ccl_token_set = gw_res_get (info.kernel_res, "ccl.token.set", "set");
590 if (isalpha (line_buf[0]))
591 exec_command (line_buf);
595 fprintf (reply_fd, "%s\n", gw_res_get (info.kernel_res,
604 assert (reply_fname);
608 mta = gw_res_get (info.kernel_res, "gw.reply.mta",
609 "/usr/lib/sendmail");
610 sprintf (cmd, "%s %s < %s", mta, from_str, reply_fname);
612 mta_code = system (cmd);
614 gw_log (GW_LOG_FATAL, "urp", "Reply '%s' got exit code %d",
616 unlink (reply_fname);