5 * Revision 1.7 1995/02/20 21:16:20 adam
6 * FML support. Bug fixes. Profile for drewdb.
8 * Revision 1.6 1995/02/17 14:41:14 quinn
9 * Added simple display of records.
11 * Revision 1.5 1995/02/17 14:22:13 adam
12 * First steps of CCL show command. Not finished yet.
14 * Revision 1.4 1995/02/17 09:08:36 adam
15 * Reply with subject. CCL base command implemented.
17 * Revision 1.3 1995/02/16 18:35:09 adam
18 * First use of Zdist library. Search requests are supported.
19 * Present requests are not supported yet.
21 * Revision 1.2 1995/02/16 13:21:00 adam
22 * Organization of resource files for targets and conversion
23 * language implemented.
25 * Revision 1.1 1995/02/15 17:45:30 adam
26 * First version of email gateway kernel. Email requests are read
27 * from stdin. The output is transferred to an MTA if 'From' is
28 * found in the header - or stdout if absent. No Z39.50 client is used.
43 static int reopen_target (void)
47 gw_log (GW_LOG_WARN, "urp", "Zass free...");
48 info.zass = zass_open (info.hostname, info.port);
51 fprintf (reply_fd, "%s %s:%d\n",
52 gw_res_get (info.kernel_res, "gw.err.connect",
53 "Cannot connect to target"),
54 info.hostname, info.port);
57 v = gw_res_get (info.kernel_res, "gw.description", NULL);
59 fprintf (reply_fd, "%s\n", v);
60 fprintf (reply_fd, "%s %s:%d\n",
61 gw_res_get (info.kernel_res, "gw.msg.connect",
62 "Connection established to"),
63 info.hostname, info.port);
65 fprintf (reply_fd, "%s:\n%s\n",
66 gw_res_get (info.kernel_res, "gw.msg.databases",
67 "Available databases"),
72 static char line_buf[LINE_MAX+1];
74 static struct command_word {
76 char *resource_suffix;
84 { "continue", "continue" },
85 { "status", "status" },
86 { "cancel", "cancel" },
87 { "target", "target" },
91 static int command_search (struct command_word *tab, struct ccl_token *cmd,
92 const char *resource_prefix)
96 assert (resource_prefix);
99 while (tab->default_value)
101 char *cp, command_names[60];
102 char resource_name[60];
105 sprintf (resource_name, "%s%s", resource_prefix,
106 tab->resource_suffix);
107 v = gw_res_get (info.kernel_res, resource_name, tab->default_value);
109 strcpy (command_names, v);
115 if ((split = strchr (cp, ' ')))
117 if (cmd->len == strlen(cp) &&
118 !memcmp (cmd->name, cp, cmd->len))
130 static struct error_no_struct {
133 } error_ccl_tab[] = {
135 { CCL_ERR_TERM_EXPECTED, "term.expected" },
136 { CCL_ERR_RP_EXPECTED, "rp.expected" },
137 { CCL_ERR_SETNAME_EXPECTED, "setname.expected" },
138 { CCL_ERR_OP_EXPECTED, "op.expected" },
139 { CCL_ERR_BAD_RP, "bad.rp" },
140 { CCL_ERR_UNKNOWN_QUAL, "unknown.qual" },
141 { CCL_ERR_DOUBLE_QUAL, "double.qual" },
142 { CCL_ERR_EQ_EXPECTED, "eq.expected" },
143 { CCL_ERR_BAD_RELATION, "bad.relation" },
144 { CCL_ERR_TRUNC_NOT_LEFT, "trunc.not.left" },
145 { CCL_ERR_TRUNC_NOT_BOTH, "trunc.not.both" },
146 { CCL_ERR_TRUNC_NOT_RIGHT, "trunc.not.right" },
150 static char *error_no_search (struct error_no_struct *tab, int no)
152 struct error_no_struct *p = tab;
153 while (p->resource_name)
156 return p->resource_name;
162 static int email_header (FILE *inf, char *from_str, char *subject_str)
166 while (fgets (line_buf, LINE_MAX, inf))
168 if (line_buf[0] == '\n')
170 if (strncmp (line_buf, "From ", 5) == 0)
171 sscanf (line_buf+4, "%s", from_str);
172 if (strncmp (line_buf, "Subject: ", 9) == 0 &&
173 sscanf (line_buf+9, "%s", subject_str+1) == 1)
174 strcpy (subject_str, line_buf+9);
179 static int exec_find (struct ccl_token *list)
181 const struct zass_searchent *p;
183 struct ccl_rpn_node *rpn;
187 rpn = ccl_find (info.bibset, list, &error, &pos);
190 const char *v = NULL, *n;
193 fprintf (reply_fd, " %*s^ - ", pos - line_buf, " ");
195 n = error_no_search (error_ccl_tab, error);
198 sprintf (name, "gw.err.%s", n);
199 v = gw_res_get (info.kernel_res, name, NULL);
202 v = ccl_err_msg (error);
203 fprintf (reply_fd, "%s\n", v);
206 ccl_pr_tree (rpn, reply_fd);
207 fprintf (reply_fd, "\n");
211 if (!*info.databases)
213 fprintf (reply_fd, "%s\n",
214 gw_res_get (info.kernel_res, "gw.err.no.database",
215 "You must select database"));
218 gw_log (GW_LOG_DEBUG, "urp", "Searching in database %s",
220 p = zass_search (info.zass, rpn, "Default", info.databases);
223 if (p->errcode != -1)
224 fprintf (reply_fd, "%s %d: %s\n",
225 gw_res_get (info.kernel_res, "gw.msg.z39errcode",
226 "Z39.50 error code"),
227 p->errcode, p->errstring);
229 fprintf (reply_fd, "%d %s\n", p->num,
230 gw_res_get (info.kernel_res, "gw.msg.hits", "hit(s)"));
234 static int exec_target (struct ccl_token *list)
237 if (list->kind == CCL_TOK_EOL)
240 memcpy (info.target, list->name, len);
241 info.target [len] = '\0';
244 return reopen_target ();
247 static int exec_base (struct ccl_token *list)
249 struct ccl_token *li = list;
252 if (list->kind == CCL_TOK_EOL)
254 free (info.databases);
255 while (li->kind != CCL_TOK_EOL)
259 if (li->kind == CCL_TOK_COMMA)
262 info.databases = malloc (len);
263 assert (info.databases);
266 while (li->kind != CCL_TOK_EOL)
268 memcpy (info.databases+len, li->name, li->len);
270 info.databases[len++] = ',';
272 if (li->kind == CCL_TOK_COMMA)
275 info.databases[len-1] = '\0';
279 struct command_word show_tab [] =
286 static int exec_show (struct ccl_token *list)
288 const struct zass_presentent *zp;
290 struct ccl_token *set_token = NULL;
291 struct ccl_token *format_token = NULL;
292 struct ccl_token *li = list;
294 if (list->kind == CCL_TOK_EOL)
298 while (li->kind != CCL_TOK_EOL)
301 if (li->next->kind == CCL_TOK_EQ)
303 if (li->kind == CCL_TOK_SET) /* set = <name> ? */
310 modifier_no = command_search (show_tab, li, "ccl.token.");
313 fprintf (reply_fd, "Unknown modifier in show\n");
317 if (modifier_no == 1) /* f = <name> ? */
319 else if (modifier_no == 2) /* p = <name> ? */
321 if (li->kind != CCL_TOK_EOL /* p = <name> - <name> ? */
322 && li->next->kind == CCL_TOK_MINUS
323 && li->next->next != CCL_TOK_EOL)
333 gw_log (GW_LOG_DEBUG, "urp", "Got set=%.*s", set_token->len,
336 gw_log (GW_LOG_DEBUG, "urp", "Got format=%.*s", format_token->len,
340 while (li->kind != CCL_TOK_EOL)
346 if (li->next->kind == CCL_TOK_EQ && li->kind != CCL_TOK_SET)
348 modifier_no = command_search (show_tab, li, "ccl.token.");
350 if (modifier_no == 2) /* p = <name> ? */
352 if (li->kind != CCL_TOK_EOL /* p = <name> - <name> ? */
353 && li->next->kind == CCL_TOK_MINUS
354 && li->next->next != CCL_TOK_EOL)
357 memcpy (num_str, li->name, len);
358 num_str [len] = '\0';
359 offset = atoi (num_str);
363 memcpy (num_str, li->name, len);
364 num_str [len] = '\0';
365 number = atoi (num_str) - offset + 1;
370 memcpy (num_str, li->name, len);
371 num_str [len] = '\0';
372 offset = atoi (num_str);
381 memcpy (num_str, li->name, len);
383 number = atoi (num_str);
387 if (offset > 0 && number > 0)
391 len = set_token->len;
392 memcpy (num_str, set_token->name, len);
396 strcpy (num_str, "Default");
397 gw_log (GW_LOG_DEBUG, "urp", "zass_present of %d records from"
398 " offset %d in set %s", number, offset, num_str);
399 zp = zass_present(info.zass, num_str, offset, number);
400 if (zp && zp->presentstatus == ZASS_PRES_SUCCESS)
405 fprintf (reply_fd, gw_res_get (info.kernel_res,
409 fprintf (reply_fd, "\n");
410 for (i = 1, pp = zp->records; pp; pp = pp->next, i++)
414 const char *arg_ar[5];
417 rec = iso2709_cvt (pp->record);
418 fprintf (reply_fd, "--- %d/%d ---\n", i, zp->num);
422 len = format_token->len;
423 memcpy (num_str, format_token->name, len);
427 (!strcmp (num_str, "0") || !strcmp (num_str, "1")))
431 arg_ar[2] = " \\list";
432 arg_ar[3] = marc_to_str (info.fml, rec);
434 fml_exec_call_argv (info.fml, arg_ar);
437 iso2709_display (rec, reply_fd);
439 iso2709_display (rec, reply_fd);
448 memcpy (num_str, list->name, len);
451 num = atoi (num_str);
454 gw_log (GW_LOG_DEBUG, "urp", "zass_present of %d records", num);
455 zp = zass_present(info.zass, "Default", 1, num);
461 fprintf (reply_fd, gw_res_get (info.kernel_res,
462 "gw.msg.records", "Got %d records"),
464 fprintf (reply_fd, "\n");
465 for (i = 1, pp = zp->records; pp; pp = pp->next, i++)
468 const char *arg_ar[3];
470 Iso2709Rec rec = iso2709_cvt (pp->record);
472 fprintf (reply_fd, "--- %d/%d ---\n", i, zp->num);
474 arg_ar[0] = "\\f0 \\list";
475 arg_ar[1] = marc_to_str (info.fml, rec);
477 fml_exec_call_argv (info.fml, arg_ar);
479 iso2709_display (rec, reply_fd);
488 static int exec_command (const char *str)
490 struct ccl_token *cmd = ccl_tokenize (str);
493 if (cmd->kind != CCL_TOK_EOL &&
494 (no = command_search (command_tab, cmd, "ccl.command.")))
496 if (!info.zass && no != 9)
498 fprintf (reply_fd, "\n> %s", str);
502 return exec_find (cmd->next);
504 return exec_show (cmd->next);
506 return exec_base (cmd->next);
508 return exec_target (cmd->next);
510 fprintf (reply_fd, "%s\n",
511 gw_res_get (info.kernel_res, "gw.err.unimplemented",
512 "Not implemented yet"));
517 fprintf (reply_fd, "\n> %s", str);
518 fprintf (reply_fd, " ^ %s\n",
519 gw_res_get (info.kernel_res, "gw.err.unknown.command",
528 char subject_str[128];
530 char *reply_fname = NULL;
532 if (email_header (inf, from_str, subject_str))
534 gw_log (GW_LOG_WARN, "urp", "No message body");
539 reply_fname = tempnam (gw_res_get (info.kernel_res,
540 "gw.reply.tmp.dir", NULL),
541 gw_res_get (info.kernel_res,
542 "gw.reply.tmp.prefix", "gwr"));
544 reply_fd = fopen (reply_fname, "w");
547 gw_log (GW_LOG_FATAL, "urp", "Cannot create %s",
551 fprintf (reply_fd, "Subject: ");
553 fprintf (reply_fd, "Z39.50 Re: %s", subject_str);
555 fprintf (reply_fd, "%s\n", gw_res_get (info.kernel_res,
558 fprintf (reply_fd, "\n");
561 gw_log (GW_LOG_WARN, "urp", "No From in email header");
562 fprintf (reply_fd, "%s\n", gw_res_get (info.kernel_res, "gw.msg.greeting",
563 "Email->Z39.50 gateway"));
564 while (fgets (line_buf, LINE_MAX, inf))
566 if (line_buf[0] == '\n')
568 ccl_token_and = gw_res_get (info.kernel_res, "ccl.token.and", "and");
569 ccl_token_or = gw_res_get (info.kernel_res, "ccl.token.or", "or");
570 ccl_token_not = gw_res_get (info.kernel_res, "ccl.token.not", "not");
571 ccl_token_set = gw_res_get (info.kernel_res, "ccl.token.set", "set");
572 if (isalpha (line_buf[0]))
573 exec_command (line_buf);
577 fprintf (reply_fd, "%s\n", gw_res_get (info.kernel_res,
586 assert (reply_fname);
590 mta = gw_res_get (info.kernel_res, "gw.reply.mta",
591 "/usr/lib/sendmail");
592 sprintf (cmd, "%s %s < %s", mta, from_str, reply_fname);
594 mta_code = system (cmd);
596 gw_log (GW_LOG_FATAL, "urp", "Reply '%s' got exit code %d",
598 unlink (reply_fname);