5 * Revision 1.5 1995/02/17 14:22:13 adam
6 * First steps of CCL show command. Not finished yet.
8 * Revision 1.4 1995/02/17 09:08:36 adam
9 * Reply with subject. CCL base command implemented.
11 * Revision 1.3 1995/02/16 18:35:09 adam
12 * First use of Zdist library. Search requests are supported.
13 * Present requests are not supported yet.
15 * Revision 1.2 1995/02/16 13:21:00 adam
16 * Organization of resource files for targets and conversion
17 * language implemented.
19 * Revision 1.1 1995/02/15 17:45:30 adam
20 * First version of email gateway kernel. Email requests are read
21 * from stdin. The output is transferred to an MTA if 'From' is
22 * found in the header - or stdout if absent. No Z39.50 client is used.
37 static int reopen_target (void)
41 gw_log (GW_LOG_WARN, "urp", "Zass free...");
42 info.zass = zass_open (info.hostname, info.port);
45 fprintf (reply_fd, "%s %s:%d\n",
46 gw_res_get (info.kernel_res, "gw.err.connect",
47 "Cannot connect to target"),
48 info.hostname, info.port);
51 v = gw_res_get (info.kernel_res, "gw.description", NULL);
53 fprintf (reply_fd, "%s\n", v);
54 fprintf (reply_fd, "%s %s:%d\n %s\n",
55 gw_res_get (info.kernel_res, "gw.msg.databases",
56 "Available databases on"),
57 info.hostname, info.port, info.databases);
61 static char line_buf[LINE_MAX+1];
63 static struct command_word {
65 char *resource_suffix;
73 { "continue", "continue" },
74 { "status", "status" },
75 { "cancel", "cancel" },
76 { "target", "target" },
80 static int command_search (struct command_word *tab, struct ccl_token *cmd,
81 const char *resource_prefix)
85 assert (resource_prefix);
88 while (tab->default_value)
90 char *cp, command_names[60];
91 char resource_name[60];
94 sprintf (resource_name, "%s%s", resource_prefix,
95 tab->resource_suffix);
96 v = gw_res_get (info.kernel_res, resource_name, tab->default_value);
98 strcpy (command_names, v);
104 if ((split = strchr (cp, ' ')))
106 if (cmd->len == strlen(cp) &&
107 !memcmp (cmd->name, cp, cmd->len))
119 static struct error_no_struct {
122 } error_ccl_tab[] = {
124 { CCL_ERR_TERM_EXPECTED, "term.expected" },
125 { CCL_ERR_RP_EXPECTED, "rp.expected" },
126 { CCL_ERR_SETNAME_EXPECTED, "setname.expected" },
127 { CCL_ERR_OP_EXPECTED, "op.expected" },
128 { CCL_ERR_BAD_RP, "bad.rp" },
129 { CCL_ERR_UNKNOWN_QUAL, "unknown.qual" },
130 { CCL_ERR_DOUBLE_QUAL, "double.qual" },
131 { CCL_ERR_EQ_EXPECTED, "eq.expected" },
132 { CCL_ERR_BAD_RELATION, "bad.relation" },
133 { CCL_ERR_TRUNC_NOT_LEFT, "trunc.not.left" },
134 { CCL_ERR_TRUNC_NOT_BOTH, "trunc.not.both" },
135 { CCL_ERR_TRUNC_NOT_RIGHT, "trunc.not.right" },
139 static char *error_no_search (struct error_no_struct *tab, int no)
141 struct error_no_struct *p = tab;
142 while (p->resource_name)
145 return p->resource_name;
151 static int email_header (FILE *inf, char *from_str, char *subject_str)
155 while (fgets (line_buf, LINE_MAX, inf))
157 if (line_buf[0] == '\n')
159 if (strncmp (line_buf, "From ", 5) == 0)
160 sscanf (line_buf+4, "%s", from_str);
161 if (strncmp (line_buf, "Subject: ", 9) == 0 &&
162 sscanf (line_buf+9, "%s", subject_str+1) == 1)
163 strcpy (subject_str, line_buf+9);
168 static int exec_find (struct ccl_token *list)
170 const struct zass_searchent *p;
172 struct ccl_rpn_node *rpn;
176 rpn = ccl_find (info.bibset, list, &error, &pos);
179 const char *v = NULL, *n;
182 fprintf (reply_fd, " %*s^ - ", pos - line_buf, " ");
184 n = error_no_search (error_ccl_tab, error);
187 sprintf (name, "gw.err.%s", n);
188 v = gw_res_get (info.kernel_res, name, NULL);
191 v = ccl_err_msg (error);
192 fprintf (reply_fd, "%s\n", v);
195 ccl_pr_tree (rpn, reply_fd);
196 fprintf (reply_fd, "\n");
200 p = zass_search (info.zass, rpn, "Default", info.databases);
203 fprintf (reply_fd, "%d %s\n", p->num,
204 gw_res_get (info.kernel_res, "gw.msg.hits", "hit(s)"));
205 if (p->errcode != -1)
206 fprintf (reply_fd, "%s %d: %s\n",
207 gw_res_get (info.kernel_res, "gw.msg.z39errcode",
208 "Z39.50 error code"),
209 p->errcode, p->errstring);
213 static int exec_target (struct ccl_token *list)
216 if (list->kind == CCL_TOK_EOL)
219 memcpy (info.target, list->name, len);
220 info.target [len] = '\0';
223 return reopen_target ();
226 static int exec_base (struct ccl_token *list)
228 struct ccl_token *li = list;
231 if (list->kind == CCL_TOK_EOL)
233 free (info.databases);
234 while (li->kind != CCL_TOK_EOL)
238 if (li->kind == CCL_TOK_COMMA)
241 info.databases = malloc (len);
242 assert (info.databases);
245 while (li->kind != CCL_TOK_EOL)
247 memcpy (info.databases+len, li->name, li->len);
249 info.databases[len++] = ',';
251 if (li->kind == CCL_TOK_COMMA)
254 info.databases[len-1] = '\0';
258 static int exec_show (struct ccl_token *list)
260 const struct zass_presentent *zp;
264 if (list->kind == CCL_TOK_EOL)
269 memcpy (num_str, list->name, list->len);
270 num_str[list->len] = '\0';
272 num = atoi (num_str);
275 gw_log (GW_LOG_DEBUG, "urp", "zass_present of %d records", num);
276 zp = zass_present(info.zass, "Default", 1, num);
280 static int exec_command (const char *str)
282 struct ccl_token *cmd = ccl_tokenize (str);
285 if (cmd->kind != CCL_TOK_EOL &&
286 (no = command_search (command_tab, cmd, "ccl.command.")))
288 if (!info.zass && no != 9)
290 fprintf (reply_fd, "\n> %s", str);
294 return exec_find (cmd->next);
296 return exec_show (cmd->next);
298 return exec_base (cmd->next);
300 return exec_target (cmd->next);
302 fprintf (reply_fd, "%s\n",
303 gw_res_get (info.kernel_res, "gw.err.unimplemented",
304 "Not implemented yet"));
309 fprintf (reply_fd, "\n> %s", str);
310 fprintf (reply_fd, " ^ %s\n",
311 gw_res_get (info.kernel_res, "gw.err.unknown.command",
320 char subject_str[128];
322 char *reply_fname = NULL;
324 if (email_header (inf, from_str, subject_str))
326 gw_log (GW_LOG_WARN, "urp", "No message body");
331 reply_fname = tempnam (gw_res_get (info.kernel_res,
332 "gw.reply.tmp.dir", NULL),
333 gw_res_get (info.kernel_res,
334 "gw.reply.tmp.prefix", "gwr"));
336 reply_fd = fopen (reply_fname, "w");
339 gw_log (GW_LOG_FATAL, "urp", "Cannot create %s",
343 fprintf (reply_fd, "Subject: ");
345 fprintf (reply_fd, "Z39.50 Re: %s", subject_str);
347 fprintf (reply_fd, "%s\n", gw_res_get (info.kernel_res,
349 "Your Z39.50 Query"));
350 fprintf (reply_fd, "\n");
353 gw_log (GW_LOG_WARN, "urp", "No From in email header");
354 fprintf (reply_fd, "%s\n", gw_res_get (info.kernel_res, "gw.msg.greeting",
355 "Email->Z39.50 gateway"));
356 while (fgets (line_buf, LINE_MAX, inf))
358 if (line_buf[0] == '\n')
360 ccl_token_and = gw_res_get (info.kernel_res, "ccl.token.and", "and");
361 ccl_token_or = gw_res_get (info.kernel_res, "ccl.token.or", "or");
362 ccl_token_not = gw_res_get (info.kernel_res, "ccl.token.not", "not");
363 ccl_token_set = gw_res_get (info.kernel_res, "ccl.token.set", "set");
364 if (isalpha (line_buf[0]))
365 exec_command (line_buf);
369 fprintf (reply_fd, "%s\n", gw_res_get (info.kernel_res,
378 assert (reply_fname);
382 mta = gw_res_get (info.kernel_res, "gw.reply.mta",
383 "/usr/lib/sendmail");
384 sprintf (cmd, "%s %s < %s", mta, from_str, reply_fname);
386 mta_code = system (cmd);
388 gw_log (GW_LOG_FATAL, "urp", "Reply '%s' got exit code %d",
390 unlink (reply_fname);