5 * Revision 1.8 1995/02/21 12:12:00 adam
6 * Diagnostic record with error info. observed.
8 * Revision 1.7 1995/02/20 21:16:20 adam
9 * FML support. Bug fixes. Profile for drewdb.
11 * Revision 1.6 1995/02/17 14:41:14 quinn
12 * Added simple display of records.
14 * Revision 1.5 1995/02/17 14:22:13 adam
15 * First steps of CCL show command. Not finished yet.
17 * Revision 1.4 1995/02/17 09:08:36 adam
18 * Reply with subject. CCL base command implemented.
20 * Revision 1.3 1995/02/16 18:35:09 adam
21 * First use of Zdist library. Search requests are supported.
22 * Present requests are not supported yet.
24 * Revision 1.2 1995/02/16 13:21:00 adam
25 * Organization of resource files for targets and conversion
26 * language implemented.
28 * Revision 1.1 1995/02/15 17:45:30 adam
29 * First version of email gateway kernel. Email requests are read
30 * from stdin. The output is transferred to an MTA if 'From' is
31 * found in the header - or stdout if absent. No Z39.50 client is used.
46 static int reopen_target (void)
50 gw_log (GW_LOG_WARN, "urp", "Zass free...");
51 info.zass = zass_open (info.hostname, info.port);
54 fprintf (reply_fd, "%s %s:%d\n",
55 gw_res_get (info.kernel_res, "gw.err.connect",
56 "Cannot connect to target"),
57 info.hostname, info.port);
60 v = gw_res_get (info.kernel_res, "gw.description", NULL);
62 fprintf (reply_fd, "%s\n", v);
63 fprintf (reply_fd, "%s %s:%d\n",
64 gw_res_get (info.kernel_res, "gw.msg.connect",
65 "Connection established to"),
66 info.hostname, info.port);
68 fprintf (reply_fd, "%s:\n%s\n",
69 gw_res_get (info.kernel_res, "gw.msg.databases",
70 "Available databases"),
75 static char line_buf[LINE_MAX+1];
77 static struct command_word {
79 char *resource_suffix;
87 { "continue", "continue" },
88 { "status", "status" },
89 { "cancel", "cancel" },
90 { "target", "target" },
94 static int command_search (struct command_word *tab, struct ccl_token *cmd,
95 const char *resource_prefix)
99 assert (resource_prefix);
102 while (tab->default_value)
104 char *cp, command_names[60];
105 char resource_name[60];
108 sprintf (resource_name, "%s%s", resource_prefix,
109 tab->resource_suffix);
110 v = gw_res_get (info.kernel_res, resource_name, tab->default_value);
112 strcpy (command_names, v);
118 if ((split = strchr (cp, ' ')))
120 if (cmd->len == strlen(cp) &&
121 !memcmp (cmd->name, cp, cmd->len))
133 static struct error_no_struct {
136 } error_ccl_tab[] = {
138 { CCL_ERR_TERM_EXPECTED, "term.expected" },
139 { CCL_ERR_RP_EXPECTED, "rp.expected" },
140 { CCL_ERR_SETNAME_EXPECTED, "setname.expected" },
141 { CCL_ERR_OP_EXPECTED, "op.expected" },
142 { CCL_ERR_BAD_RP, "bad.rp" },
143 { CCL_ERR_UNKNOWN_QUAL, "unknown.qual" },
144 { CCL_ERR_DOUBLE_QUAL, "double.qual" },
145 { CCL_ERR_EQ_EXPECTED, "eq.expected" },
146 { CCL_ERR_BAD_RELATION, "bad.relation" },
147 { CCL_ERR_TRUNC_NOT_LEFT, "trunc.not.left" },
148 { CCL_ERR_TRUNC_NOT_BOTH, "trunc.not.both" },
149 { CCL_ERR_TRUNC_NOT_RIGHT, "trunc.not.right" },
153 static char *error_no_search (struct error_no_struct *tab, int no)
155 struct error_no_struct *p = tab;
156 while (p->resource_name)
159 return p->resource_name;
165 static int email_header (FILE *inf, char *from_str, char *subject_str)
169 while (fgets (line_buf, LINE_MAX, inf))
171 if (line_buf[0] == '\n')
173 if (strncmp (line_buf, "From ", 5) == 0)
174 sscanf (line_buf+4, "%s", from_str);
175 if (strncmp (line_buf, "Subject: ", 9) == 0 &&
176 sscanf (line_buf+9, "%s", subject_str+1) == 1)
177 strcpy (subject_str, line_buf+9);
182 static int exec_find (struct ccl_token *list)
184 const struct zass_searchent *p;
186 struct ccl_rpn_node *rpn;
190 rpn = ccl_find (info.bibset, list, &error, &pos);
193 const char *v = NULL, *n;
196 fprintf (reply_fd, " %*s^ - ", pos - line_buf, " ");
198 n = error_no_search (error_ccl_tab, error);
201 sprintf (name, "gw.err.%s", n);
202 v = gw_res_get (info.kernel_res, name, NULL);
205 v = ccl_err_msg (error);
206 fprintf (reply_fd, "%s\n", v);
209 ccl_pr_tree (rpn, reply_fd);
210 fprintf (reply_fd, "\n");
214 if (!*info.databases)
216 fprintf (reply_fd, "%s\n",
217 gw_res_get (info.kernel_res, "gw.err.no.database",
218 "You must select database"));
221 gw_log (GW_LOG_DEBUG, "urp", "Searching in database %s",
223 p = zass_search (info.zass, rpn, "Default", info.databases);
226 if (p->errcode != -1)
227 fprintf (reply_fd, "%s %d: %s\n",
228 gw_res_get (info.kernel_res, "gw.msg.z39errcode",
229 "Z39.50 error code"),
230 p->errcode, p->errstring);
232 fprintf (reply_fd, "%d %s\n", p->num,
233 gw_res_get (info.kernel_res, "gw.msg.hits", "hit(s)"));
237 static int exec_target (struct ccl_token *list)
240 if (list->kind == CCL_TOK_EOL)
243 memcpy (info.target, list->name, len);
244 info.target [len] = '\0';
247 return reopen_target ();
250 static int exec_base (struct ccl_token *list)
252 struct ccl_token *li = list;
255 if (list->kind == CCL_TOK_EOL)
257 free (info.databases);
258 while (li->kind != CCL_TOK_EOL)
262 if (li->kind == CCL_TOK_COMMA)
265 info.databases = malloc (len);
266 assert (info.databases);
269 while (li->kind != CCL_TOK_EOL)
271 memcpy (info.databases+len, li->name, li->len);
273 info.databases[len++] = ',';
275 if (li->kind == CCL_TOK_COMMA)
278 info.databases[len-1] = '\0';
282 struct command_word show_tab [] =
289 static int exec_show (struct ccl_token *list)
291 const struct zass_presentent *zp;
293 struct ccl_token *set_token = NULL;
294 struct ccl_token *format_token = NULL;
295 struct ccl_token *li = list;
297 if (list->kind == CCL_TOK_EOL)
301 while (li->kind != CCL_TOK_EOL)
304 if (li->next->kind == CCL_TOK_EQ)
306 if (li->kind == CCL_TOK_SET) /* set = <name> ? */
313 modifier_no = command_search (show_tab, li, "ccl.token.");
316 fprintf (reply_fd, "Unknown modifier in show\n");
320 if (modifier_no == 1) /* f = <name> ? */
322 else if (modifier_no == 2) /* p = <name> ? */
324 if (li->kind != CCL_TOK_EOL /* p = <name> - <name> ? */
325 && li->next->kind == CCL_TOK_MINUS
326 && li->next->next != CCL_TOK_EOL)
336 gw_log (GW_LOG_DEBUG, "urp", "Got set=%.*s", set_token->len,
339 gw_log (GW_LOG_DEBUG, "urp", "Got format=%.*s", format_token->len,
343 while (li->kind != CCL_TOK_EOL)
349 if (li->next->kind == CCL_TOK_EQ && li->kind != CCL_TOK_SET)
351 modifier_no = command_search (show_tab, li, "ccl.token.");
353 if (modifier_no == 2) /* p = <name> ? */
355 if (li->kind != CCL_TOK_EOL /* p = <name> - <name> ? */
356 && li->next->kind == CCL_TOK_MINUS
357 && li->next->next != CCL_TOK_EOL)
360 memcpy (num_str, li->name, len);
361 num_str [len] = '\0';
362 offset = atoi (num_str);
366 memcpy (num_str, li->name, len);
367 num_str [len] = '\0';
368 number = atoi (num_str) - offset + 1;
373 memcpy (num_str, li->name, len);
374 num_str [len] = '\0';
375 offset = atoi (num_str);
384 memcpy (num_str, li->name, len);
386 number = atoi (num_str);
390 if (offset > 0 && number > 0)
394 len = set_token->len;
395 memcpy (num_str, set_token->name, len);
399 strcpy (num_str, "Default");
400 gw_log (GW_LOG_DEBUG, "urp", "zass_present of %d records from"
401 " offset %d in set %s", number, offset, num_str);
402 zp = zass_present(info.zass, num_str, offset, number);
408 fprintf (reply_fd, gw_res_get (info.kernel_res,
412 fprintf (reply_fd, "\n");
413 for (i = 0, pp = zp->records; pp; pp = pp->next, i++)
417 const char *arg_ar[5];
419 fprintf (reply_fd, "--- %d/%d ---\n",
420 i+offset, offset+zp->num);
421 if (pp->which == ZASS_REC_DIAG)
423 fprintf (reply_fd, "Record error %d: %s\n",
424 pp->errcode, pp->errstring);
427 else if (pp->which != ZASS_REC_USMARC)
429 fprintf (reply_fd, "Unknown record kind %d\n",
433 rec = iso2709_cvt (pp->record);
437 len = format_token->len;
438 memcpy (num_str, format_token->name, len);
442 (!strcmp (num_str, "0") || !strcmp (num_str, "1")))
446 arg_ar[2] = " \\list";
447 arg_ar[3] = marc_to_str (info.fml, rec);
449 fml_exec_call_argv (info.fml, arg_ar);
452 iso2709_display (rec, reply_fd);
454 iso2709_display (rec, reply_fd);
463 memcpy (num_str, list->name, len);
466 num = atoi (num_str);
469 gw_log (GW_LOG_DEBUG, "urp", "zass_present of %d records", num);
470 zp = zass_present(info.zass, "Default", 1, num);
476 fprintf (reply_fd, gw_res_get (info.kernel_res,
477 "gw.msg.records", "Got %d records"),
479 fprintf (reply_fd, "\n");
480 for (i = 1, pp = zp->records; pp; pp = pp->next, i++)
483 const char *arg_ar[3];
485 Iso2709Rec rec = iso2709_cvt (pp->record);
487 fprintf (reply_fd, "--- %d/%d ---\n", i, zp->num);
489 arg_ar[0] = "\\f0 \\list";
490 arg_ar[1] = marc_to_str (info.fml, rec);
492 fml_exec_call_argv (info.fml, arg_ar);
494 iso2709_display (rec, reply_fd);
503 static int exec_command (const char *str)
505 struct ccl_token *cmd = ccl_tokenize (str);
508 if (cmd->kind != CCL_TOK_EOL &&
509 (no = command_search (command_tab, cmd, "ccl.command.")))
511 if (!info.zass && no != 9)
513 fprintf (reply_fd, "\n> %s", str);
517 return exec_find (cmd->next);
519 return exec_show (cmd->next);
521 return exec_base (cmd->next);
523 return exec_target (cmd->next);
525 fprintf (reply_fd, "%s\n",
526 gw_res_get (info.kernel_res, "gw.err.unimplemented",
527 "Not implemented yet"));
532 fprintf (reply_fd, "\n> %s", str);
533 fprintf (reply_fd, " ^ %s\n",
534 gw_res_get (info.kernel_res, "gw.err.unknown.command",
543 char subject_str[128];
545 char *reply_fname = NULL;
547 if (email_header (inf, from_str, subject_str))
549 gw_log (GW_LOG_WARN, "urp", "No message body");
554 reply_fname = tempnam (gw_res_get (info.kernel_res,
555 "gw.reply.tmp.dir", NULL),
556 gw_res_get (info.kernel_res,
557 "gw.reply.tmp.prefix", "gwr"));
559 reply_fd = fopen (reply_fname, "w");
562 gw_log (GW_LOG_FATAL, "urp", "Cannot create %s",
566 fprintf (reply_fd, "Subject: ");
568 fprintf (reply_fd, "Z39.50 Re: %s", subject_str);
570 fprintf (reply_fd, "%s\n", gw_res_get (info.kernel_res,
573 fprintf (reply_fd, "\n");
576 gw_log (GW_LOG_WARN, "urp", "No From in email header");
577 fprintf (reply_fd, "%s\n", gw_res_get (info.kernel_res, "gw.msg.greeting",
578 "Email->Z39.50 gateway"));
579 while (fgets (line_buf, LINE_MAX, inf))
581 if (line_buf[0] == '\n')
583 ccl_token_and = gw_res_get (info.kernel_res, "ccl.token.and", "and");
584 ccl_token_or = gw_res_get (info.kernel_res, "ccl.token.or", "or");
585 ccl_token_not = gw_res_get (info.kernel_res, "ccl.token.not", "not");
586 ccl_token_set = gw_res_get (info.kernel_res, "ccl.token.set", "set");
587 if (isalpha (line_buf[0]))
588 exec_command (line_buf);
592 fprintf (reply_fd, "%s\n", gw_res_get (info.kernel_res,
601 assert (reply_fname);
605 mta = gw_res_get (info.kernel_res, "gw.reply.mta",
606 "/usr/lib/sendmail");
607 sprintf (cmd, "%s %s < %s", mta, from_str, reply_fname);
609 mta_code = system (cmd);
611 gw_log (GW_LOG_FATAL, "urp", "Reply '%s' got exit code %d",
613 unlink (reply_fname);