5 * Revision 1.12 1995/02/22 15:51:51 adam
6 * Bug fix: swap of parameter number and offset in function present.
8 * Revision 1.11 1995/02/22 15:22:33 adam
9 * Much more checking of run-time state. Show command never retrieves
10 * more records than indicated by the previous search request. Help
11 * command available. The maximum number of records retrieved can be
14 * Revision 1.10 1995/02/22 08:51:35 adam
15 * Output function can be customized in fml, which is used to print
16 * the reply to reply_fd.
18 * Revision 1.9 1995/02/21 17:46:21 adam
21 * Revision 1.8 1995/02/21 12:12:00 adam
22 * Diagnostic record with error info. observed.
24 * Revision 1.7 1995/02/20 21:16:20 adam
25 * FML support. Bug fixes. Profile for drewdb.
27 * Revision 1.6 1995/02/17 14:41:14 quinn
28 * Added simple display of records.
30 * Revision 1.5 1995/02/17 14:22:13 adam
31 * First steps of CCL show command. Not finished yet.
33 * Revision 1.4 1995/02/17 09:08:36 adam
34 * Reply with subject. CCL base command implemented.
36 * Revision 1.3 1995/02/16 18:35:09 adam
37 * First use of Zdist library. Search requests are supported.
38 * Present requests are not supported yet.
40 * Revision 1.2 1995/02/16 13:21:00 adam
41 * Organization of resource files for targets and conversion
42 * language implemented.
44 * Revision 1.1 1995/02/15 17:45:30 adam
45 * First version of email gateway kernel. Email requests are read
46 * from stdin. The output is transferred to an MTA if 'From' is
47 * found in the header - or stdout if absent. No Z39.50 client is used.
62 static void put_esc_str (const char *s)
67 if (*s == '\\' && s[1])
72 fputc ('\n', reply_fd);
75 fputc ('\t', reply_fd);
85 if (*s != ' ' || !escape_flag)
93 static int reopen_target (void)
97 gw_log (GW_LOG_WARN, "urp", "Zass free...");
98 info.zass = zass_open (info.hostname, info.port);
101 fprintf (reply_fd, "%s %s:%d\n",
102 gw_res_get (info.kernel_res, "gw.err.connect",
103 "Cannot connect to target"),
104 info.hostname, info.port);
107 v = gw_res_get (info.kernel_res, "gw.description", NULL);
109 fprintf (reply_fd, "%s\n", v);
110 fprintf (reply_fd, "%s %s:%d\n",
111 gw_res_get (info.kernel_res, "gw.msg.connect",
112 "Connection established to"),
113 info.hostname, info.port);
115 fprintf (reply_fd, "%s:\n%s\n",
116 gw_res_get (info.kernel_res, "gw.msg.databases",
117 "Available databases"),
122 static char line_buf[LINE_MAX+1];
124 static struct command_word {
126 char *resource_suffix;
134 { "continue", "continue" },
135 { "status", "status" },
136 { "cancel", "cancel" },
137 { "target", "target" },
141 static int command_search (struct command_word *tab, struct ccl_token *cmd,
142 const char *resource_prefix)
146 assert (resource_prefix);
149 while (tab->default_value)
151 char *cp, command_names[60];
152 char resource_name[60];
155 sprintf (resource_name, "%s%s", resource_prefix,
156 tab->resource_suffix);
157 v = gw_res_get (info.kernel_res, resource_name, tab->default_value);
159 strcpy (command_names, v);
165 if ((split = strchr (cp, ' ')))
167 if (cmd->len == strlen(cp) &&
168 !memcmp (cmd->name, cp, cmd->len))
180 static struct error_no_struct {
183 } error_ccl_tab[] = {
185 { CCL_ERR_TERM_EXPECTED, "term.expected" },
186 { CCL_ERR_RP_EXPECTED, "rp.expected" },
187 { CCL_ERR_SETNAME_EXPECTED, "setname.expected" },
188 { CCL_ERR_OP_EXPECTED, "op.expected" },
189 { CCL_ERR_BAD_RP, "bad.rp" },
190 { CCL_ERR_UNKNOWN_QUAL, "unknown.qual" },
191 { CCL_ERR_DOUBLE_QUAL, "double.qual" },
192 { CCL_ERR_EQ_EXPECTED, "eq.expected" },
193 { CCL_ERR_BAD_RELATION, "bad.relation" },
194 { CCL_ERR_TRUNC_NOT_LEFT, "trunc.not.left" },
195 { CCL_ERR_TRUNC_NOT_BOTH, "trunc.not.both" },
196 { CCL_ERR_TRUNC_NOT_RIGHT, "trunc.not.right" },
200 static char *error_no_search (struct error_no_struct *tab, int no)
202 struct error_no_struct *p = tab;
203 while (p->resource_name)
206 return p->resource_name;
212 static int email_header (FILE *inf, char *from_str, char *subject_str)
216 while (fgets (line_buf, LINE_MAX, inf))
218 if (line_buf[0] == '\n')
220 if (strncmp (line_buf, "From ", 5) == 0)
221 sscanf (line_buf+4, "%s", from_str);
222 if (strncmp (line_buf, "Subject: ", 9) == 0 &&
223 sscanf (line_buf+9, "%s", subject_str+1) == 1)
224 strcpy (subject_str, line_buf+9);
229 static void help_general (void)
231 put_esc_str (gw_res_get (info.kernel_res, "gw.help.general",
232 "Commands available in this service:\n"));
235 static int exec_help (struct ccl_token *list)
240 put_esc_str (gw_res_get (info.kernel_res, "gw.help.target",
241 "target <name> - selects a given target\n"));
243 put_esc_str (gw_res_get (info.kernel_res, "gw.help.base",
244 "base <base>.. - selects databases\n"));
246 put_esc_str (gw_res_get (info.kernel_res, "gw.help.find",
247 "find <query> - performs a search request\n"));
249 put_esc_str (gw_res_get (info.kernel_res, "gw.help.show",
250 "show <spec> - retrieves and displays "
252 put_esc_str (gw_res_get (info.kernel_res, "gw.help.help",
253 "help - displays help\n"));
258 static int exec_find (struct ccl_token *list)
260 const struct zass_searchent *p;
261 struct gw_user_set *us;
263 struct ccl_rpn_node *rpn;
267 us = user_set_add ("Default", -1);
268 rpn = ccl_find (info.bibset, list, &error, &pos);
271 const char *v = NULL, *n;
274 fprintf (reply_fd, " %*s^ - ", pos - line_buf, " ");
276 n = error_no_search (error_ccl_tab, error);
279 sprintf (name, "gw.err.%s", n);
280 v = gw_res_get (info.kernel_res, name, NULL);
283 v = ccl_err_msg (error);
284 fprintf (reply_fd, "%s\n", v);
287 ccl_pr_tree (rpn, reply_fd);
288 fprintf (reply_fd, "\n");
290 if (!*info.databases)
292 fprintf (reply_fd, "%s\n",
293 gw_res_get (info.kernel_res, "gw.err.no.database",
294 "You must select database"));
297 gw_log (GW_LOG_DEBUG, "urp", "Searching in database %s",
300 p = zass_search (info.zass, rpn, "Default", info.databases);
303 if (p->errcode != -1)
305 fprintf (reply_fd, "%s %d: %s\n",
306 gw_res_get (info.kernel_res, "gw.msg.z39errcode",
307 "Z39.50 error code"),
308 p->errcode, p->errstring);
311 fprintf (reply_fd, "%d %s\n", p->num,
312 gw_res_get (info.kernel_res, "gw.msg.hits", "hit(s)"));
317 static int exec_target (struct ccl_token *list)
320 if (list->kind == CCL_TOK_EOL)
323 memcpy (info.target, list->name, len);
324 info.target [len] = '\0';
327 return reopen_target ();
330 static int exec_base (struct ccl_token *list)
332 struct ccl_token *li = list;
336 if (list->kind == CCL_TOK_EOL)
338 free (info.databases);
339 while (li->kind != CCL_TOK_EOL)
343 if (li->kind == CCL_TOK_COMMA)
346 info.databases = malloc (len);
347 assert (info.databases);
350 while (li->kind != CCL_TOK_EOL)
352 memcpy (info.databases+len, li->name, li->len);
354 info.databases[len++] = ',';
356 if (li->kind == CCL_TOK_COMMA)
359 info.databases[len-1] = '\0';
363 struct command_word show_tab [] =
370 static void present (const char *set, int offset, int number,
371 struct ccl_token *format_token)
373 const struct zass_presentent *zp;
378 max_number = atoi (gw_res_get (info.kernel_res, "gw.max.show",
380 if (number > max_number)
382 gw_log (GW_LOG_DEBUG, "urp", "present in set %s", set);
383 gw_log (GW_LOG_DEBUG, "urp", "present of %d records from offset %d",
385 zp = zass_present(info.zass, (char *) set, offset, number);
391 fprintf (reply_fd, gw_res_get (info.kernel_res,
395 fprintf (reply_fd, "\n");
396 for (i = 0, pp = zp->records; pp; pp = pp->next, i++)
400 const char *arg_ar[5];
402 fprintf (reply_fd, "--- %d/%d ---\n",
403 i+offset, offset+zp->num-1);
404 if (!gw_res_get (info.kernel_res, "gw.ignore.which", NULL))
406 if (pp->which == ZASS_REC_DIAG)
408 fprintf (reply_fd, "Record error %d: %s\n",
409 pp->errcode, pp->errstring);
412 else if (pp->which != ZASS_REC_USMARC)
414 fprintf (reply_fd, "Unknown record kind %d\n",
419 rec = iso2709_cvt (pp->record);
425 len = format_token->len;
426 memcpy (format_str, format_token->name, len);
427 format_str[len] = '\0';
429 if (info.fml && format_token &&
430 (!strcmp (format_str, "0") || !strcmp (format_str, "1")))
433 arg_ar[1] = format_str;
434 arg_ar[2] = " \\list";
435 arg_ar[3] = marc_to_str (info.fml, rec);
437 fml_exec_call_argv (info.fml, arg_ar);
440 iso2709_display (rec, reply_fd);
442 iso2709_display (rec, reply_fd);
447 fprintf (reply_fd, "Not a MARC record\n");
452 static int exec_show (struct ccl_token *list)
455 struct ccl_token *set_token = NULL;
456 struct ccl_token *format_token = NULL;
457 struct ccl_token *li = list;
458 int no_of_present = 0;
461 while (li->kind != CCL_TOK_EOL)
464 if (li->next->kind == CCL_TOK_EQ)
466 if (li->kind == CCL_TOK_SET) /* set = <name> ? */
473 modifier_no = command_search (show_tab, li, "ccl.token.");
476 fprintf (reply_fd, "Unknown modifier in show\n");
480 if (modifier_no == 1) /* f = <name> ? */
482 else if (modifier_no == 2) /* p = <name> ? */
484 if (li->kind != CCL_TOK_EOL /* p = <name> - <name> ? */
485 && li->next->kind == CCL_TOK_MINUS
486 && li->next->next != CCL_TOK_EOL)
492 fprintf (reply_fd, "%s\n", "Missing token after '='");
501 gw_log (GW_LOG_DEBUG, "urp", "Got set=%.*s", set_token->len,
504 gw_log (GW_LOG_DEBUG, "urp", "Got format=%.*s", format_token->len,
508 while (li->kind != CCL_TOK_EOL)
514 if (li->next->kind == CCL_TOK_EQ && li->kind != CCL_TOK_SET)
516 modifier_no = command_search (show_tab, li, "ccl.token.");
518 if (modifier_no == 2) /* p = <name> ? */
520 if (li->kind != CCL_TOK_EOL /* p = <name> - <name> ? */
521 && li->next->kind == CCL_TOK_MINUS
522 && li->next->next != CCL_TOK_EOL)
525 memcpy (tmp_str, li->name, len);
526 tmp_str [len] = '\0';
527 offset = atoi (tmp_str);
531 memcpy (tmp_str, li->name, len);
532 tmp_str [len] = '\0';
533 number = atoi (tmp_str) - offset + 1;
538 memcpy (tmp_str, li->name, len);
539 tmp_str [len] = '\0';
540 offset = atoi (tmp_str);
549 memcpy (tmp_str, li->name, len);
551 number = atoi (tmp_str);
555 if (offset > 0 && number > 0)
557 struct gw_user_set *us;
561 len = set_token->len;
562 memcpy (tmp_str, set_token->name, len);
564 us = user_set_search (tmp_str);
567 us = user_set_search (NULL);
568 if (us && us->hits != -1) /* proper result-set? */
570 if (offset <= us->hits)
572 if (offset+number-1 > us->hits)
573 number = us->hits - offset+1;
574 present (us->name, offset, number, format_token);
577 else if (!no_of_present) /* display error message once! */
579 fprintf (reply_fd, "%s\n",
580 gw_res_get (info.kernel_res, "gw.err.no.set",
581 "No result-set generated"));
586 if (!no_of_present) /* no records shown so far? */
588 struct gw_user_set *us;
591 us = user_set_search (NULL);
592 if (us && us->hits != -1) /* proper result-set? */
594 default_show = atoi (gw_res_get (info.kernel_res,
595 "gw.default.show", "20"));
596 if (us->hits > default_show)
597 present (us->name, 1, default_show, format_token);
598 else if (us->hits > 0)
599 present (us->name, 1, us->hits, format_token);
601 else /* display error message */
603 fprintf (reply_fd, "%s\n",
604 gw_res_get (info.kernel_res, "gw.err.no.set",
605 "No result-set generated"));
612 static int exec_command (const char *str)
614 struct ccl_token *cmd = ccl_tokenize (str);
617 if (cmd->kind != CCL_TOK_EOL &&
618 (no = command_search (command_tab, cmd, "ccl.command.")))
620 if (!info.zass && no != 9 && no != 4)
622 fprintf (reply_fd, "\n> %s", str);
623 if (!info.zass && (no == 1 || no == 2 || no == 3))
625 fprintf (reply_fd, "%s\n",
626 gw_res_get (info.kernel_res, "gw.err.no.target",
627 "No connection established - "
634 return exec_find (cmd->next);
636 return exec_show (cmd->next);
638 return exec_base (cmd->next);
640 return exec_help (cmd->next);
642 return exec_target (cmd->next);
644 fprintf (reply_fd, "%s\n",
645 gw_res_get (info.kernel_res, "gw.err.unimplemented",
646 "Not implemented yet"));
651 fprintf (reply_fd, "\n> %s", str);
652 fprintf (reply_fd, " ^ %s\n",
653 gw_res_get (info.kernel_res, "gw.err.unknown.command",
655 "Use help to see list of commands"));
663 char subject_str[128];
665 char *reply_fname = NULL;
667 if (email_header (inf, from_str, subject_str))
669 gw_log (GW_LOG_WARN, "urp", "No message body");
674 reply_fname = tempnam (gw_res_get (info.kernel_res,
675 "gw.reply.tmp.dir", NULL),
676 gw_res_get (info.kernel_res,
677 "gw.reply.tmp.prefix", "gwr"));
679 reply_fd = fopen (reply_fname, "w");
682 gw_log (GW_LOG_FATAL, "urp", "Cannot create %s",
686 fprintf (reply_fd, "Subject: ");
688 fprintf (reply_fd, "Z39.50 Re: %s", subject_str);
690 fprintf (reply_fd, "%s\n", gw_res_get (info.kernel_res,
693 fprintf (reply_fd, "\n");
696 gw_log (GW_LOG_WARN, "urp", "No From in email header");
697 fprintf (reply_fd, "%s\n", gw_res_get (info.kernel_res, "gw.msg.greeting",
698 "Email->Z39.50 gateway"));
699 while (fgets (line_buf, LINE_MAX, inf))
701 if (line_buf[0] == '\n')
703 ccl_token_and = gw_res_get (info.kernel_res, "ccl.token.and", "and");
704 ccl_token_or = gw_res_get (info.kernel_res, "ccl.token.or", "or");
705 ccl_token_not = gw_res_get (info.kernel_res, "ccl.token.not", "not");
706 ccl_token_set = gw_res_get (info.kernel_res, "ccl.token.set", "set");
707 if (isalpha (line_buf[0]))
708 exec_command (line_buf);
713 fprintf (reply_fd, "%s\n", gw_res_get (info.kernel_res,
724 assert (reply_fname);
728 mta = gw_res_get (info.kernel_res, "gw.reply.mta",
729 "/usr/lib/sendmail");
730 sprintf (cmd, "%s %s < %s", mta, from_str, reply_fname);
732 mta_code = system (cmd);
734 gw_log (GW_LOG_FATAL, "urp", "Reply '%s' got exit code %d",
736 unlink (reply_fname);