5 * Revision 1.10 1995/02/22 08:51:35 adam
6 * Output function can be customized in fml, which is used to print
7 * the reply to reply_fd.
9 * Revision 1.9 1995/02/21 17:46:21 adam
12 * Revision 1.8 1995/02/21 12:12:00 adam
13 * Diagnostic record with error info. observed.
15 * Revision 1.7 1995/02/20 21:16:20 adam
16 * FML support. Bug fixes. Profile for drewdb.
18 * Revision 1.6 1995/02/17 14:41:14 quinn
19 * Added simple display of records.
21 * Revision 1.5 1995/02/17 14:22:13 adam
22 * First steps of CCL show command. Not finished yet.
24 * Revision 1.4 1995/02/17 09:08:36 adam
25 * Reply with subject. CCL base command implemented.
27 * Revision 1.3 1995/02/16 18:35:09 adam
28 * First use of Zdist library. Search requests are supported.
29 * Present requests are not supported yet.
31 * Revision 1.2 1995/02/16 13:21:00 adam
32 * Organization of resource files for targets and conversion
33 * language implemented.
35 * Revision 1.1 1995/02/15 17:45:30 adam
36 * First version of email gateway kernel. Email requests are read
37 * from stdin. The output is transferred to an MTA if 'From' is
38 * found in the header - or stdout if absent. No Z39.50 client is used.
53 static int reopen_target (void)
57 gw_log (GW_LOG_WARN, "urp", "Zass free...");
58 info.zass = zass_open (info.hostname, info.port);
61 fprintf (reply_fd, "%s %s:%d\n",
62 gw_res_get (info.kernel_res, "gw.err.connect",
63 "Cannot connect to target"),
64 info.hostname, info.port);
67 v = gw_res_get (info.kernel_res, "gw.description", NULL);
69 fprintf (reply_fd, "%s\n", v);
70 fprintf (reply_fd, "%s %s:%d\n",
71 gw_res_get (info.kernel_res, "gw.msg.connect",
72 "Connection established to"),
73 info.hostname, info.port);
75 fprintf (reply_fd, "%s:\n%s\n",
76 gw_res_get (info.kernel_res, "gw.msg.databases",
77 "Available databases"),
82 static char line_buf[LINE_MAX+1];
84 static struct command_word {
86 char *resource_suffix;
94 { "continue", "continue" },
95 { "status", "status" },
96 { "cancel", "cancel" },
97 { "target", "target" },
101 static int command_search (struct command_word *tab, struct ccl_token *cmd,
102 const char *resource_prefix)
106 assert (resource_prefix);
109 while (tab->default_value)
111 char *cp, command_names[60];
112 char resource_name[60];
115 sprintf (resource_name, "%s%s", resource_prefix,
116 tab->resource_suffix);
117 v = gw_res_get (info.kernel_res, resource_name, tab->default_value);
119 strcpy (command_names, v);
125 if ((split = strchr (cp, ' ')))
127 if (cmd->len == strlen(cp) &&
128 !memcmp (cmd->name, cp, cmd->len))
140 static struct error_no_struct {
143 } error_ccl_tab[] = {
145 { CCL_ERR_TERM_EXPECTED, "term.expected" },
146 { CCL_ERR_RP_EXPECTED, "rp.expected" },
147 { CCL_ERR_SETNAME_EXPECTED, "setname.expected" },
148 { CCL_ERR_OP_EXPECTED, "op.expected" },
149 { CCL_ERR_BAD_RP, "bad.rp" },
150 { CCL_ERR_UNKNOWN_QUAL, "unknown.qual" },
151 { CCL_ERR_DOUBLE_QUAL, "double.qual" },
152 { CCL_ERR_EQ_EXPECTED, "eq.expected" },
153 { CCL_ERR_BAD_RELATION, "bad.relation" },
154 { CCL_ERR_TRUNC_NOT_LEFT, "trunc.not.left" },
155 { CCL_ERR_TRUNC_NOT_BOTH, "trunc.not.both" },
156 { CCL_ERR_TRUNC_NOT_RIGHT, "trunc.not.right" },
160 static char *error_no_search (struct error_no_struct *tab, int no)
162 struct error_no_struct *p = tab;
163 while (p->resource_name)
166 return p->resource_name;
172 static int email_header (FILE *inf, char *from_str, char *subject_str)
176 while (fgets (line_buf, LINE_MAX, inf))
178 if (line_buf[0] == '\n')
180 if (strncmp (line_buf, "From ", 5) == 0)
181 sscanf (line_buf+4, "%s", from_str);
182 if (strncmp (line_buf, "Subject: ", 9) == 0 &&
183 sscanf (line_buf+9, "%s", subject_str+1) == 1)
184 strcpy (subject_str, line_buf+9);
189 static int exec_find (struct ccl_token *list)
191 const struct zass_searchent *p;
193 struct ccl_rpn_node *rpn;
197 rpn = ccl_find (info.bibset, list, &error, &pos);
200 const char *v = NULL, *n;
203 fprintf (reply_fd, " %*s^ - ", pos - line_buf, " ");
205 n = error_no_search (error_ccl_tab, error);
208 sprintf (name, "gw.err.%s", n);
209 v = gw_res_get (info.kernel_res, name, NULL);
212 v = ccl_err_msg (error);
213 fprintf (reply_fd, "%s\n", v);
216 ccl_pr_tree (rpn, reply_fd);
217 fprintf (reply_fd, "\n");
221 if (!*info.databases)
223 fprintf (reply_fd, "%s\n",
224 gw_res_get (info.kernel_res, "gw.err.no.database",
225 "You must select database"));
228 gw_log (GW_LOG_DEBUG, "urp", "Searching in database %s",
230 p = zass_search (info.zass, rpn, "Default", info.databases);
233 if (p->errcode != -1)
234 fprintf (reply_fd, "%s %d: %s\n",
235 gw_res_get (info.kernel_res, "gw.msg.z39errcode",
236 "Z39.50 error code"),
237 p->errcode, p->errstring);
239 fprintf (reply_fd, "%d %s\n", p->num,
240 gw_res_get (info.kernel_res, "gw.msg.hits", "hit(s)"));
244 static int exec_target (struct ccl_token *list)
247 if (list->kind == CCL_TOK_EOL)
250 memcpy (info.target, list->name, len);
251 info.target [len] = '\0';
254 return reopen_target ();
257 static int exec_base (struct ccl_token *list)
259 struct ccl_token *li = list;
262 if (list->kind == CCL_TOK_EOL)
264 free (info.databases);
265 while (li->kind != CCL_TOK_EOL)
269 if (li->kind == CCL_TOK_COMMA)
272 info.databases = malloc (len);
273 assert (info.databases);
276 while (li->kind != CCL_TOK_EOL)
278 memcpy (info.databases+len, li->name, li->len);
280 info.databases[len++] = ',';
282 if (li->kind == CCL_TOK_COMMA)
285 info.databases[len-1] = '\0';
289 struct command_word show_tab [] =
296 static int exec_show (struct ccl_token *list)
298 const struct zass_presentent *zp;
300 struct ccl_token *set_token = NULL;
301 struct ccl_token *format_token = NULL;
302 struct ccl_token *li = list;
304 if (list->kind == CCL_TOK_EOL)
308 while (li->kind != CCL_TOK_EOL)
311 if (li->next->kind == CCL_TOK_EQ)
313 if (li->kind == CCL_TOK_SET) /* set = <name> ? */
320 modifier_no = command_search (show_tab, li, "ccl.token.");
323 fprintf (reply_fd, "Unknown modifier in show\n");
327 if (modifier_no == 1) /* f = <name> ? */
329 else if (modifier_no == 2) /* p = <name> ? */
331 if (li->kind != CCL_TOK_EOL /* p = <name> - <name> ? */
332 && li->next->kind == CCL_TOK_MINUS
333 && li->next->next != CCL_TOK_EOL)
339 fprintf (reply_fd, "%s\n", "Missing token after '='");
348 gw_log (GW_LOG_DEBUG, "urp", "Got set=%.*s", set_token->len,
351 gw_log (GW_LOG_DEBUG, "urp", "Got format=%.*s", format_token->len,
355 while (li->kind != CCL_TOK_EOL)
361 if (li->next->kind == CCL_TOK_EQ && li->kind != CCL_TOK_SET)
363 modifier_no = command_search (show_tab, li, "ccl.token.");
365 if (modifier_no == 2) /* p = <name> ? */
367 if (li->kind != CCL_TOK_EOL /* p = <name> - <name> ? */
368 && li->next->kind == CCL_TOK_MINUS
369 && li->next->next != CCL_TOK_EOL)
372 memcpy (num_str, li->name, len);
373 num_str [len] = '\0';
374 offset = atoi (num_str);
378 memcpy (num_str, li->name, len);
379 num_str [len] = '\0';
380 number = atoi (num_str) - offset + 1;
385 memcpy (num_str, li->name, len);
386 num_str [len] = '\0';
387 offset = atoi (num_str);
396 memcpy (num_str, li->name, len);
398 number = atoi (num_str);
402 if (offset > 0 && number > 0)
406 len = set_token->len;
407 memcpy (num_str, set_token->name, len);
411 strcpy (num_str, "Default");
412 gw_log (GW_LOG_DEBUG, "urp", "zass_present of %d records from"
413 " offset %d in set %s", number, offset, num_str);
414 zp = zass_present(info.zass, num_str, offset, number);
420 fprintf (reply_fd, gw_res_get (info.kernel_res,
424 fprintf (reply_fd, "\n");
425 for (i = 0, pp = zp->records; pp; pp = pp->next, i++)
429 const char *arg_ar[5];
431 fprintf (reply_fd, "--- %d/%d ---\n",
432 i+offset, offset+zp->num-1);
434 if (pp->which == ZASS_REC_DIAG)
436 fprintf (reply_fd, "Record error %d: %s\n",
437 pp->errcode, pp->errstring);
440 else if (pp->which != ZASS_REC_USMARC)
442 fprintf (reply_fd, "Unknown record kind %d\n",
447 rec = iso2709_cvt (pp->record);
451 len = format_token->len;
452 memcpy (num_str, format_token->name, len);
456 (!strcmp (num_str, "0") || !strcmp (num_str, "1")))
460 arg_ar[2] = " \\list";
461 arg_ar[3] = marc_to_str (info.fml, rec);
463 fml_exec_call_argv (info.fml, arg_ar);
466 iso2709_display (rec, reply_fd);
468 iso2709_display (rec, reply_fd);
477 memcpy (num_str, list->name, len);
480 num = atoi (num_str);
483 gw_log (GW_LOG_DEBUG, "urp", "zass_present of %d records", num);
484 zp = zass_present(info.zass, "Default", 1, num);
490 fprintf (reply_fd, gw_res_get (info.kernel_res,
491 "gw.msg.records", "Got %d records"),
493 fprintf (reply_fd, "\n");
494 for (i = 1, pp = zp->records; pp; pp = pp->next, i++)
497 const char *arg_ar[3];
499 Iso2709Rec rec = iso2709_cvt (pp->record);
501 fprintf (reply_fd, "--- %d/%d ---\n", i, zp->num);
503 arg_ar[0] = "\\f0 \\list";
504 arg_ar[1] = marc_to_str (info.fml, rec);
506 fml_exec_call_argv (info.fml, arg_ar);
508 iso2709_display (rec, reply_fd);
517 static int exec_command (const char *str)
519 struct ccl_token *cmd = ccl_tokenize (str);
522 if (cmd->kind != CCL_TOK_EOL &&
523 (no = command_search (command_tab, cmd, "ccl.command.")))
525 if (!info.zass && no != 9)
527 fprintf (reply_fd, "\n> %s", str);
531 return exec_find (cmd->next);
533 return exec_show (cmd->next);
535 return exec_base (cmd->next);
537 return exec_target (cmd->next);
539 fprintf (reply_fd, "%s\n",
540 gw_res_get (info.kernel_res, "gw.err.unimplemented",
541 "Not implemented yet"));
546 fprintf (reply_fd, "\n> %s", str);
547 fprintf (reply_fd, " ^ %s\n",
548 gw_res_get (info.kernel_res, "gw.err.unknown.command",
557 char subject_str[128];
559 char *reply_fname = NULL;
561 if (email_header (inf, from_str, subject_str))
563 gw_log (GW_LOG_WARN, "urp", "No message body");
568 reply_fname = tempnam (gw_res_get (info.kernel_res,
569 "gw.reply.tmp.dir", NULL),
570 gw_res_get (info.kernel_res,
571 "gw.reply.tmp.prefix", "gwr"));
573 reply_fd = fopen (reply_fname, "w");
576 gw_log (GW_LOG_FATAL, "urp", "Cannot create %s",
580 fprintf (reply_fd, "Subject: ");
582 fprintf (reply_fd, "Z39.50 Re: %s", subject_str);
584 fprintf (reply_fd, "%s\n", gw_res_get (info.kernel_res,
587 fprintf (reply_fd, "\n");
590 gw_log (GW_LOG_WARN, "urp", "No From in email header");
591 fprintf (reply_fd, "%s\n", gw_res_get (info.kernel_res, "gw.msg.greeting",
592 "Email->Z39.50 gateway"));
593 while (fgets (line_buf, LINE_MAX, inf))
595 if (line_buf[0] == '\n')
597 ccl_token_and = gw_res_get (info.kernel_res, "ccl.token.and", "and");
598 ccl_token_or = gw_res_get (info.kernel_res, "ccl.token.or", "or");
599 ccl_token_not = gw_res_get (info.kernel_res, "ccl.token.not", "not");
600 ccl_token_set = gw_res_get (info.kernel_res, "ccl.token.set", "set");
601 if (isalpha (line_buf[0]))
602 exec_command (line_buf);
606 fprintf (reply_fd, "%s\n", gw_res_get (info.kernel_res,
615 assert (reply_fname);
619 mta = gw_res_get (info.kernel_res, "gw.reply.mta",
620 "/usr/lib/sendmail");
621 sprintf (cmd, "%s %s < %s", mta, from_str, reply_fname);
623 mta_code = system (cmd);
625 gw_log (GW_LOG_FATAL, "urp", "Reply '%s' got exit code %d",
627 unlink (reply_fname);