5 * Revision 1.6 1995/02/17 14:41:14 quinn
6 * Added simple display of records.
8 * Revision 1.5 1995/02/17 14:22:13 adam
9 * First steps of CCL show command. Not finished yet.
11 * Revision 1.4 1995/02/17 09:08:36 adam
12 * Reply with subject. CCL base command implemented.
14 * Revision 1.3 1995/02/16 18:35:09 adam
15 * First use of Zdist library. Search requests are supported.
16 * Present requests are not supported yet.
18 * Revision 1.2 1995/02/16 13:21:00 adam
19 * Organization of resource files for targets and conversion
20 * language implemented.
22 * Revision 1.1 1995/02/15 17:45:30 adam
23 * First version of email gateway kernel. Email requests are read
24 * from stdin. The output is transferred to an MTA if 'From' is
25 * found in the header - or stdout if absent. No Z39.50 client is used.
41 static int reopen_target (void)
45 gw_log (GW_LOG_WARN, "urp", "Zass free...");
46 info.zass = zass_open (info.hostname, info.port);
49 fprintf (reply_fd, "%s %s:%d\n",
50 gw_res_get (info.kernel_res, "gw.err.connect",
51 "Cannot connect to target"),
52 info.hostname, info.port);
55 v = gw_res_get (info.kernel_res, "gw.description", NULL);
57 fprintf (reply_fd, "%s\n", v);
58 fprintf (reply_fd, "%s %s:%d\n %s\n",
59 gw_res_get (info.kernel_res, "gw.msg.databases",
60 "Available databases on"),
61 info.hostname, info.port, info.databases);
65 static char line_buf[LINE_MAX+1];
67 static struct command_word {
69 char *resource_suffix;
77 { "continue", "continue" },
78 { "status", "status" },
79 { "cancel", "cancel" },
80 { "target", "target" },
84 static int command_search (struct command_word *tab, struct ccl_token *cmd,
85 const char *resource_prefix)
89 assert (resource_prefix);
92 while (tab->default_value)
94 char *cp, command_names[60];
95 char resource_name[60];
98 sprintf (resource_name, "%s%s", resource_prefix,
99 tab->resource_suffix);
100 v = gw_res_get (info.kernel_res, resource_name, tab->default_value);
102 strcpy (command_names, v);
108 if ((split = strchr (cp, ' ')))
110 if (cmd->len == strlen(cp) &&
111 !memcmp (cmd->name, cp, cmd->len))
123 static struct error_no_struct {
126 } error_ccl_tab[] = {
128 { CCL_ERR_TERM_EXPECTED, "term.expected" },
129 { CCL_ERR_RP_EXPECTED, "rp.expected" },
130 { CCL_ERR_SETNAME_EXPECTED, "setname.expected" },
131 { CCL_ERR_OP_EXPECTED, "op.expected" },
132 { CCL_ERR_BAD_RP, "bad.rp" },
133 { CCL_ERR_UNKNOWN_QUAL, "unknown.qual" },
134 { CCL_ERR_DOUBLE_QUAL, "double.qual" },
135 { CCL_ERR_EQ_EXPECTED, "eq.expected" },
136 { CCL_ERR_BAD_RELATION, "bad.relation" },
137 { CCL_ERR_TRUNC_NOT_LEFT, "trunc.not.left" },
138 { CCL_ERR_TRUNC_NOT_BOTH, "trunc.not.both" },
139 { CCL_ERR_TRUNC_NOT_RIGHT, "trunc.not.right" },
143 static char *error_no_search (struct error_no_struct *tab, int no)
145 struct error_no_struct *p = tab;
146 while (p->resource_name)
149 return p->resource_name;
155 static int email_header (FILE *inf, char *from_str, char *subject_str)
159 while (fgets (line_buf, LINE_MAX, inf))
161 if (line_buf[0] == '\n')
163 if (strncmp (line_buf, "From ", 5) == 0)
164 sscanf (line_buf+4, "%s", from_str);
165 if (strncmp (line_buf, "Subject: ", 9) == 0 &&
166 sscanf (line_buf+9, "%s", subject_str+1) == 1)
167 strcpy (subject_str, line_buf+9);
172 static int exec_find (struct ccl_token *list)
174 const struct zass_searchent *p;
176 struct ccl_rpn_node *rpn;
180 rpn = ccl_find (info.bibset, list, &error, &pos);
183 const char *v = NULL, *n;
186 fprintf (reply_fd, " %*s^ - ", pos - line_buf, " ");
188 n = error_no_search (error_ccl_tab, error);
191 sprintf (name, "gw.err.%s", n);
192 v = gw_res_get (info.kernel_res, name, NULL);
195 v = ccl_err_msg (error);
196 fprintf (reply_fd, "%s\n", v);
199 ccl_pr_tree (rpn, reply_fd);
200 fprintf (reply_fd, "\n");
204 p = zass_search (info.zass, rpn, "Default", info.databases);
207 fprintf (reply_fd, "%d %s\n", p->num,
208 gw_res_get (info.kernel_res, "gw.msg.hits", "hit(s)"));
209 if (p->errcode != -1)
210 fprintf (reply_fd, "%s %d: %s\n",
211 gw_res_get (info.kernel_res, "gw.msg.z39errcode",
212 "Z39.50 error code"),
213 p->errcode, p->errstring);
217 static int exec_target (struct ccl_token *list)
220 if (list->kind == CCL_TOK_EOL)
223 memcpy (info.target, list->name, len);
224 info.target [len] = '\0';
227 return reopen_target ();
230 static int exec_base (struct ccl_token *list)
232 struct ccl_token *li = list;
235 if (list->kind == CCL_TOK_EOL)
237 free (info.databases);
238 while (li->kind != CCL_TOK_EOL)
242 if (li->kind == CCL_TOK_COMMA)
245 info.databases = malloc (len);
246 assert (info.databases);
249 while (li->kind != CCL_TOK_EOL)
251 memcpy (info.databases+len, li->name, li->len);
253 info.databases[len++] = ',';
255 if (li->kind == CCL_TOK_COMMA)
258 info.databases[len-1] = '\0';
262 static int exec_show (struct ccl_token *list)
264 const struct zass_presentent *zp;
269 if (list->kind == CCL_TOK_EOL)
274 memcpy (num_str, list->name, list->len);
275 num_str[list->len] = '\0';
277 num = atoi (num_str);
280 gw_log (GW_LOG_DEBUG, "urp", "zass_present of %d records", num);
281 zp = zass_present(info.zass, "Default", 1, num);
284 fprintf(reply_fd, "Got %d records\n", zp->num);
285 for (pp = zp->records; pp; pp = pp->next)
287 Iso2709Rec p = iso2709_cvt(pp->record);
288 iso2709_display(p, reply_fd);
295 static int exec_command (const char *str)
297 struct ccl_token *cmd = ccl_tokenize (str);
300 if (cmd->kind != CCL_TOK_EOL &&
301 (no = command_search (command_tab, cmd, "ccl.command.")))
303 if (!info.zass && no != 9)
305 fprintf (reply_fd, "\n> %s", str);
309 return exec_find (cmd->next);
311 return exec_show (cmd->next);
313 return exec_base (cmd->next);
315 return exec_target (cmd->next);
317 fprintf (reply_fd, "%s\n",
318 gw_res_get (info.kernel_res, "gw.err.unimplemented",
319 "Not implemented yet"));
324 fprintf (reply_fd, "\n> %s", str);
325 fprintf (reply_fd, " ^ %s\n",
326 gw_res_get (info.kernel_res, "gw.err.unknown.command",
335 char subject_str[128];
337 char *reply_fname = NULL;
339 if (email_header (inf, from_str, subject_str))
341 gw_log (GW_LOG_WARN, "urp", "No message body");
346 reply_fname = tempnam (gw_res_get (info.kernel_res,
347 "gw.reply.tmp.dir", NULL),
348 gw_res_get (info.kernel_res,
349 "gw.reply.tmp.prefix", "gwr"));
351 reply_fd = fopen (reply_fname, "w");
354 gw_log (GW_LOG_FATAL, "urp", "Cannot create %s",
358 fprintf (reply_fd, "Subject: ");
360 fprintf (reply_fd, "Z39.50 Re: %s", subject_str);
362 fprintf (reply_fd, "%s\n", gw_res_get (info.kernel_res,
364 "Your Z39.50 Query"));
365 fprintf (reply_fd, "\n");
368 gw_log (GW_LOG_WARN, "urp", "No From in email header");
369 fprintf (reply_fd, "%s\n", gw_res_get (info.kernel_res, "gw.msg.greeting",
370 "Email->Z39.50 gateway"));
371 while (fgets (line_buf, LINE_MAX, inf))
373 if (line_buf[0] == '\n')
375 ccl_token_and = gw_res_get (info.kernel_res, "ccl.token.and", "and");
376 ccl_token_or = gw_res_get (info.kernel_res, "ccl.token.or", "or");
377 ccl_token_not = gw_res_get (info.kernel_res, "ccl.token.not", "not");
378 ccl_token_set = gw_res_get (info.kernel_res, "ccl.token.set", "set");
379 if (isalpha (line_buf[0]))
380 exec_command (line_buf);
384 fprintf (reply_fd, "%s\n", gw_res_get (info.kernel_res,
393 assert (reply_fname);
397 mta = gw_res_get (info.kernel_res, "gw.reply.mta",
398 "/usr/lib/sendmail");
399 sprintf (cmd, "%s %s < %s", mta, from_str, reply_fname);
401 mta_code = system (cmd);
403 gw_log (GW_LOG_FATAL, "urp", "Reply '%s' got exit code %d",
405 unlink (reply_fname);