Timeout (idletime) adjustable. Minor changes in kernel.
[egate.git] / kernel / urp.c
1 /* Gateway kernel - User Request Processor
2  * Europagate, 1995
3  *
4  * $Log: urp.c,v $
5  * Revision 1.25  1995/04/17 09:34:33  adam
6  * Timeout (idletime) adjustable. Minor changes in kernel.
7  *
8  * Revision 1.24  1995/03/28  11:42:35  adam
9  * First use of string-queue utility.
10  *
11  * Revision 1.23  1995/03/28  08:01:28  adam
12  * FIFO existence is used to test for a running kernel.
13  *
14  * Revision 1.22  1995/03/27  12:51:05  adam
15  * New log level in use: GW_LOG_ERRNO.
16  *
17  * Revision 1.21  1995/03/27  08:24:04  adam
18  * First use of gip interface and gw-db.
19  * First work on eti program.
20  *
21  * Revision 1.20  1995/03/03  17:19:17  adam
22  * Smarter presentation. Bug fix in email header interpretation.
23  *
24  * Revision 1.19  1995/03/02  09:32:11  adam
25  * New presentation formats. f0=full, f1=brief, f2=mid
26  *
27  * Revision 1.18  1995/03/01  14:32:26  adam
28  * Better diagnostics. Default is, that only one database selected when
29  * several are known.
30  *
31  * Revision 1.17  1995/02/28  13:16:26  adam
32  * Configurable From: added.
33  *
34  * Revision 1.16  1995/02/23  10:08:20  adam
35  * Added logging of all user commands.
36  *
37  * Revision 1.15  1995/02/23  08:32:17  adam
38  * Changed header.
39  *
40  * Revision 1.13  1995/02/22  16:54:42  adam
41  * Qualifiers of LOC target updated. More logging messages.
42  *
43  * Revision 1.12  1995/02/22  15:51:51  adam
44  * Bug fix: swap of parameter number and offset in function present.
45  *
46  * Revision 1.11  1995/02/22  15:22:33  adam
47  * Much more checking of run-time state. Show command never retrieves
48  * more records than indicated by the previous search request. Help
49  * command available. The maximum number of records retrieved can be
50  * controlled now.
51  *
52  * Revision 1.10  1995/02/22  08:51:35  adam
53  * Output function can be customized in fml, which is used to print
54  * the reply to reply_fd.
55  *
56  * Revision 1.9  1995/02/21  17:46:21  adam
57  * Minor changes.
58  *
59  * Revision 1.8  1995/02/21  12:12:00  adam
60  * Diagnostic record with error info. observed.
61  *
62  * Revision 1.7  1995/02/20  21:16:20  adam
63  * FML support. Bug fixes. Profile for drewdb.
64  *
65  * Revision 1.6  1995/02/17  14:41:14  quinn
66  * Added simple display of records.
67  *
68  * Revision 1.5  1995/02/17  14:22:13  adam
69  * First steps of CCL show command. Not finished yet.
70  *
71  * Revision 1.4  1995/02/17  09:08:36  adam
72  * Reply with subject. CCL base command implemented.
73  *
74  * Revision 1.3  1995/02/16  18:35:09  adam
75  * First use of Zdist library. Search requests are supported.
76  * Present requests are not supported yet.
77  *
78  * Revision 1.2  1995/02/16  13:21:00  adam
79  * Organization of resource files for targets and conversion
80  * language implemented.
81  *
82  * Revision 1.1  1995/02/15  17:45:30  adam
83  * First version of email gateway kernel. Email requests are read
84  * from stdin. The output is transferred to an MTA if 'From' is
85  * found in the header - or stdout if absent. No Z39.50 client is used.
86  *
87  */
88
89 #include <stdio.h>
90 #include <stdlib.h>
91 #include <assert.h>
92 #include <ctype.h>
93 #include <string.h>
94 #include <unistd.h>
95 #include <fcntl.h>
96
97 #include <ttyemit.h>
98 #include <strqueue.h>
99 #include "kernel.h"
100
101 static char line_buf[LINE_MAX+1];
102
103 static void put_esc_str (const char *s)
104 {
105     while (*s)
106         tty_emit (*s++);
107 }
108
109 int lgets (char *buf, int max, int fd)
110 {
111     int r, no = 0;
112
113     --max;
114     while (no <= max)
115     {
116         if ((r=read (fd, buf+no, 1)) != 1)
117         {
118             if (r == -1)
119                 gw_log (GW_LOG_WARN|GW_LOG_ERRNO, KERNEL_LOG, "read fail");
120             buf[no] = '\0';
121             return 0;
122         }
123         if (buf[no] == 1)
124             return 0;     
125         if (buf[no++] == '\n')
126             break;
127     }
128     buf[no] = '\0';     
129     return 1;
130 }
131
132 static int reopen_target (void)
133 {
134     const char *v;
135     if (info.zass)
136         gw_log (GW_LOG_WARN, KERNEL_LOG, "Zass free...");
137     info.zass = zass_open (info.hostname, info.port);
138     if (!info.zass)
139     {
140         fprintf (reply_fd, "%s %s:%d\n", 
141                  gw_res_get (info.kernel_res, "gw.err.connect",
142                              "Cannot connect to target"),
143                  info.hostname, info.port);
144         return -1;
145     }
146     v = gw_res_get (info.kernel_res, "gw.description", NULL);
147     if (v)
148     {
149         put_esc_str (v);
150         fprintf (reply_fd, "\n");
151     }
152     fprintf (reply_fd, "%s %s:%d\n",
153              gw_res_get (info.kernel_res, "gw.msg.connect",
154                          "Connection established to"),
155              info.hostname, info.port);
156     if (*info.databases)
157         fprintf (reply_fd, "%s:\n%s\n",
158                  gw_res_get (info.kernel_res, "gw.msg.databases",
159                              "Available databases"),
160                  info.databases);
161     if (*info.database)
162         fprintf (reply_fd, "%s:\n%s\n",
163                  gw_res_get (info.kernel_res, "gw.msg.database",
164                              "Selected databases"),
165                  info.database);
166     return 0;
167 }
168
169
170 static struct command_word {
171     char *default_value;
172     char *resource_suffix;
173 } command_tab [] = 
174 {
175 {   "find", "find"},
176 {   "show", "show"},
177 {   "base", "base" },
178 {   "help", "help" },
179 {   "info", "info" },
180 {   "continue", "continue" },
181 {   "status", "status" },
182 {   "cancel", "cancel" },
183 {   "target", "target" },
184 {   "stop",   "stop" },
185 {   NULL, NULL }
186 };
187
188 static int command_search (struct command_word *tab, struct ccl_token *cmd,
189 const char *resource_prefix)
190 {
191     int no = 1;
192
193     assert (resource_prefix);
194     assert (tab);
195     assert (cmd);
196     while (tab->default_value)
197     {
198         char *cp, command_names[60];
199         char resource_name[60];
200         const char *v;
201
202         sprintf (resource_name, "%s%s", resource_prefix,
203                  tab->resource_suffix);
204         v = gw_res_get (info.kernel_res, resource_name, tab->default_value);
205         assert (v);
206         strcpy (command_names, v);
207         cp = command_names;
208         while (1)
209         {
210             char *split;
211
212             if ((split = strchr (cp, ' ')))
213                 *split = '\0';
214             if (cmd->len == strlen(cp) &&
215                 !memcmp (cmd->name, cp, cmd->len))
216                 return no;
217             if (!split)
218                 break;
219             cp = split+1;
220         }        
221         no++;
222         tab++;
223     }
224     return 0;
225 }
226
227 static struct error_no_struct {
228     int no;
229     char *resource_name;
230 } error_ccl_tab[] = {
231 {  CCL_ERR_OK, "ok"},
232 {  CCL_ERR_TERM_EXPECTED, "term.expected" },
233 {  CCL_ERR_RP_EXPECTED, "rp.expected" },
234 {  CCL_ERR_SETNAME_EXPECTED, "setname.expected" },
235 {  CCL_ERR_OP_EXPECTED, "op.expected" },
236 {  CCL_ERR_BAD_RP, "bad.rp" },
237 {  CCL_ERR_UNKNOWN_QUAL, "unknown.qual" },
238 {  CCL_ERR_DOUBLE_QUAL, "double.qual" },
239 {  CCL_ERR_EQ_EXPECTED, "eq.expected" },
240 {  CCL_ERR_BAD_RELATION, "bad.relation" },
241 {  CCL_ERR_TRUNC_NOT_LEFT, "trunc.not.left" },
242 {  CCL_ERR_TRUNC_NOT_BOTH, "trunc.not.both" },
243 {  CCL_ERR_TRUNC_NOT_RIGHT, "trunc.not.right" },
244 {  0, NULL }
245 };
246
247 static char *error_no_search (struct error_no_struct *tab, int no)
248 {
249     struct error_no_struct *p = tab;
250     while (p->resource_name)
251     {
252         if (no == p->no)
253             return p->resource_name;
254         p++;
255     }
256     return NULL;
257 }
258
259 static int email_header (struct str_queue *sq,
260                          char *from_str, char *subject_str)
261 {
262     *from_str = '\0';
263     *subject_str = '\0';    
264     while (str_queue_deq (sq, line_buf, LINE_MAX))
265     {
266         if (line_buf[0] == '\n')
267             return 0;
268         if (memcmp (line_buf, "From ", 5) == 0)
269             sscanf (line_buf+4, "%s", from_str);
270         if (memcmp (line_buf, "Subject: ", 9) == 0 &&
271             sscanf (line_buf+9, "%s", subject_str+1) == 1)
272             strcpy (subject_str, line_buf+9);
273     }
274     return 1;
275 }
276
277 static void help_general (void)
278 {
279     put_esc_str (gw_res_get (info.kernel_res, "gw.help.general",
280                              "Commands available in this service:\n"));
281 }
282
283 static int exec_help (struct ccl_token *list)
284 {
285     static char *sep = "-------------------------------\\n";
286     help_general ();
287
288 #if 1
289     put_esc_str (sep);
290     put_esc_str (gw_res_get (info.kernel_res, "gw.help.target",
291                              "target <name> - selects a given target\n"));
292
293     put_esc_str (sep);
294     put_esc_str (gw_res_get (info.kernel_res, "gw.help.base",
295                              "base <base>..  - selects databases\n"));
296
297     put_esc_str (sep);
298     put_esc_str (gw_res_get (info.kernel_res, "gw.help.find",
299                              "find <query>   - performs a search request\n"));
300
301     put_esc_str (sep);
302     put_esc_str (gw_res_get (info.kernel_res, "gw.help.show",
303                              "show <spec>    - retrieves and displays "
304                              "records\n"));
305 #endif
306     return 0;
307 }
308
309 static void display_diag_error (int code, const char *addinfo)
310 {
311     static char str[20];
312
313     sprintf (str, "gw.bib1.diag.%d", code);
314     fprintf (reply_fd, "%s %d:\n %s: '%s'\n",
315              gw_res_get (info.kernel_res, "gw.msg.z39errcode", 
316                          "Z39.50 Error"),
317              code,
318              gw_res_get (info.kernel_res, str, ""), addinfo);
319 }
320
321 static int exec_find (struct ccl_token *list)
322 {
323     const struct zass_searchent *p;
324     struct gw_user_set *us;
325
326     struct ccl_rpn_node *rpn;
327     int error;
328     const char *pos;
329
330     us = user_set_add ("Default", -1);
331     rpn = ccl_find (info.bibset, list, &error, &pos);
332     if (!rpn)
333     {
334         const char *v = NULL, *n;
335         char name[128];
336
337         fprintf (reply_fd, "  %*s^ - ", pos - line_buf, " ");
338
339         n = error_no_search (error_ccl_tab, error);
340         if (n)
341         {
342             sprintf (name, "gw.err.%s", n);
343             v = gw_res_get (info.kernel_res, name, NULL);
344         }
345         if (!v)
346             v = ccl_err_msg (error);
347         fprintf (reply_fd, "%s\n", v);
348         return -1;
349     }
350     ccl_pr_tree (rpn, reply_fd);
351     fprintf (reply_fd, "\n");
352
353     if (!*info.database )
354     {
355         fprintf (reply_fd, "%s\n",
356                  gw_res_get (info.kernel_res, "gw.err.no.database",
357                              "You must select database"));
358         return -3;
359     }
360     gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Searching in database %s",
361             info.database );
362     assert (info.zass);
363     p = zass_search (info.zass, rpn, "Default", info.database );
364     if (!p)
365         return -1;
366     if (p->errcode != -1)
367     {
368         display_diag_error (p->errcode, p->errstring);
369         return -2;
370     }
371     fprintf (reply_fd, "%d %s\n", p->num,
372              gw_res_get (info.kernel_res, "gw.msg.hits", "hit(s)"));
373     us->hits = p->num;
374     return 0;
375 }
376
377 static int exec_target (struct ccl_token *list)
378 {
379     int len;
380     if (list->kind == CCL_TOK_EOL)
381         return -1;
382     len = list->len;
383     memcpy (info.target, list->name, len);
384     info.target [len] = '\0';
385
386     read_kernel_res ();
387     return reopen_target ();
388 }
389
390 static int exec_base (struct ccl_token *list)
391 {
392     struct ccl_token *li = list;
393     int len = 0;
394
395     assert (info.zass);
396     if (list->kind == CCL_TOK_EOL)
397         return -1;
398     free (info.database);
399     while (li->kind != CCL_TOK_EOL)
400     {
401         len += li->len + 1;
402         li = li->next;
403         if (li->kind == CCL_TOK_COMMA)
404             li = li->next;
405     }
406     info.database  = malloc (len);
407     assert (info.database );
408     len = 0;
409     li = list;
410     while (li->kind != CCL_TOK_EOL)
411     {
412         memcpy (info.database+len, li->name, li->len);
413         len += li->len;
414         info.database[len++] = ',';
415         li = li->next;
416         if (li->kind == CCL_TOK_COMMA)
417             li = li->next;
418     }
419     info.database[len-1] = '\0';
420     return 0;
421 }
422
423 struct command_word show_tab [] = 
424 {
425 {   "f", "format"},
426 {   "p", "position"},
427 {   NULL, NULL }
428 };
429
430 static void present (const char *set, int offset, int number,
431                      struct ccl_token *format_token)
432 {
433     const struct zass_presentent *zp;
434     int len;
435     int max_number;
436     char format_str[16];
437     
438     max_number = atoi (gw_res_get (info.kernel_res, "gw.max.show", 
439                                    "200"));
440     if (number > max_number)
441         number = max_number;
442     gw_log (GW_LOG_DEBUG, KERNEL_LOG, "present in set %s", set);
443     gw_log (GW_LOG_DEBUG, KERNEL_LOG, "present of %d records from offset %d",
444             number, offset);
445     zp = zass_present(info.zass, (char *) set, offset, number);
446     if (zp)
447     {
448         int i;
449         zass_record *pp;
450         char path[128];
451         int  record_log_fd = -1;
452         const char *record_log_name;
453
454         record_log_name = gw_res_get (info.kernel_res, "gw.marc.log",
455                                       NULL);
456         if (record_log_name)
457         {
458             sprintf (path, "%s/%s", gw_res_get (info.kernel_res,
459                                                 "gw.path", "."),
460                      record_log_name );
461             record_log_fd = open (path, O_WRONLY|O_CREAT|O_APPEND, 0666);
462             if (record_log_fd == -1)
463                 gw_log (GW_LOG_WARN|GW_LOG_ERRNO, "Cannot open %s", path);
464         }
465         fprintf (reply_fd, gw_res_get (info.kernel_res,
466                                        "gw.msg.records",
467                                        "Got %d records"),
468                  zp->num);
469         fprintf (reply_fd, "\n");
470         for (i = 0, pp = zp->records; pp; pp = pp->next, i++)
471         {
472             Iso2709Rec rec;
473 #if USE_FML
474             const char *arg_ar[5];
475 #endif
476             fprintf (reply_fd, "--- %d/%d ---\n",
477                      i+offset, offset+zp->num-1);
478             if (!gw_res_get (info.kernel_res, "gw.ignore.which", NULL))
479             {
480                 if (pp->which == ZASS_REC_DIAG)
481                 {
482                     display_diag_error (pp->errcode, pp->errstring);
483                     continue;
484                 }
485                 else if (pp->which != ZASS_REC_USMARC)
486                 {
487                     fprintf (reply_fd, "Unknown record kind %d\n",
488                              pp->which);
489                     continue;
490                 }
491             }
492             if (record_log_fd != -1)
493                 write (record_log_fd, pp->record, strlen(pp->record));
494             rec = iso2709_cvt (pp->record);
495             if (rec)
496             {
497 #if USE_FML
498                 if (format_token)
499                 {
500                     len = format_token->len;
501                     if (len >= sizeof(format_str))
502                         len = sizeof(format_str)-1;
503                     memcpy (format_str, format_token->name, len);
504                     format_str[len] = '\0';
505                 }
506                 if (info.fml && format_token && 
507                     (!strcmp (format_str, "0") || !strcmp (format_str, "1")
508                      || !strcmp(format_str, "2")))
509                 {
510                     arg_ar[0] = "\\f";
511                     arg_ar[1] = format_str;
512                     arg_ar[2] = " \\list";
513                     arg_ar[3] = marc_to_str (info.fml, rec);
514                     arg_ar[4] = NULL;
515                     fml_exec_call_argv (info.fml, arg_ar);
516                 }
517                 else
518                     iso2709_display (rec, reply_fd);
519 #else
520                 iso2709_display (rec, reply_fd);
521 #endif
522                 iso2709_rm (rec);
523             }
524             else
525                 fprintf (reply_fd, "Not a MARC record\n");
526         }
527         if (record_log_fd != -1)
528             close (record_log_fd);
529     }
530 }
531
532 static int exec_show (struct ccl_token *list)
533 {
534     char tmp_str[20];
535     struct ccl_token *set_token = NULL;
536     struct ccl_token *format_token = NULL;
537     struct ccl_token *li = list;
538     int no_of_present = 0;
539
540     assert (info.zass);
541     while (li->kind != CCL_TOK_EOL)
542     {
543         int modifier_no = 0;
544         if (li->next->kind == CCL_TOK_EQ)
545         {
546             if (li->kind == CCL_TOK_SET)    /* set = <name> ? */
547             {
548                 li = li->next->next;
549                 set_token = li;
550             }
551             else 
552             {
553                 modifier_no = command_search (show_tab, li, "ccl.token.");
554                 if (!modifier_no)
555                 {
556                     fprintf (reply_fd, "Unknown modifier in show\n");
557                     return -1;
558                 }
559                 li = li->next->next;
560                 if (modifier_no == 1)       /* f = <name> ? */
561                     format_token = li;
562                 else if (modifier_no == 2)  /* p = <name> ? */
563                 {
564                     if (li->kind != CCL_TOK_EOL   /* p = <name> - <name> ? */
565                         && li->next->kind == CCL_TOK_MINUS
566                         && li->next->next != CCL_TOK_EOL)
567                         li = li->next->next;
568                 }
569             }
570             if (!li->next)
571             {
572                 fprintf (reply_fd, "%s\n", "Missing token after '='");
573                 return -2;
574             }
575             li = li->next;
576         }
577         else
578             li = li->next;
579     }
580     if (set_token)
581         gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Got set=%.*s", set_token->len,
582                 set_token->name);
583     if (format_token)
584         gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Got format=%.*s", format_token->len,
585                 format_token->name);
586
587     li = list;
588     while (li->kind != CCL_TOK_EOL)
589     {
590         int modifier_no = 0;
591         int offset = 0;
592         int number = 0;
593         int len;
594         if (li->next->kind == CCL_TOK_EQ && li->kind != CCL_TOK_SET)
595         {
596             modifier_no = command_search (show_tab, li, "ccl.token.");
597             li = li->next->next;
598             if (modifier_no == 2)  /* p = <name> ? */
599             {
600                 if (li->kind != CCL_TOK_EOL   /* p = <name> - <name> ? */
601                     && li->next->kind == CCL_TOK_MINUS
602                     && li->next->next != CCL_TOK_EOL)
603                 {
604                     len = li->len;
605                     memcpy (tmp_str, li->name, len);
606                     tmp_str [len] = '\0';
607                     offset = atoi (tmp_str);
608                     li = li->next->next;
609
610                     len = li->len;
611                     memcpy (tmp_str, li->name, len);
612                     tmp_str [len] = '\0';
613                     number = atoi (tmp_str) - offset + 1;
614                 }
615                 else
616                 {
617                     len = li->len;
618                     memcpy (tmp_str, li->name, len);
619                     tmp_str [len] = '\0';
620                     offset = atoi (tmp_str);
621                     number = 1;
622                 }
623             }
624             li = li->next;
625         }
626         else
627         {
628             len = li->len;
629             memcpy (tmp_str, li->name, len);
630             tmp_str[len] = '\0';
631             number = atoi (tmp_str);
632             offset = 1;
633             li = li->next;
634         }
635         if (offset > 0 && number > 0)
636         {
637             struct gw_user_set *us;
638
639             if (set_token)
640             {
641                 len = set_token->len;
642                 memcpy (tmp_str, set_token->name, len);
643                 tmp_str[len] = '\0';
644                 us = user_set_search (tmp_str);
645             }
646             else
647                 us = user_set_search (NULL);
648             if (us && us->hits != -1) /* proper result-set? */
649             {
650                 if (offset <= us->hits)
651                 {
652                     if (offset+number-1 > us->hits)
653                         number = us->hits - offset+1;
654                     present (us->name, offset, number, format_token);
655                 }
656             }
657             else if (!no_of_present) /* display error message once! */
658             {
659                 fprintf (reply_fd, "%s\n",
660                          gw_res_get (info.kernel_res, "gw.err.no.set",
661                                      "No result-set generated"));
662             }
663             no_of_present++;
664         }
665     }
666     if (!no_of_present) /* no records shown so far? */
667     {
668         struct gw_user_set *us;
669         int default_show;
670         
671         us = user_set_search (NULL);
672         if (us && us->hits != -1)    /* proper result-set? */
673         {
674             default_show = atoi (gw_res_get (info.kernel_res,
675                                              "gw.default.show", "20"));
676             if (us->hits > default_show)
677                 present (us->name, 1, default_show, format_token);
678             else if (us->hits > 0)
679                 present (us->name, 1, us->hits, format_token);
680         }
681         else                         /* display error message */
682         {
683             fprintf (reply_fd, "%s\n",
684                      gw_res_get (info.kernel_res, "gw.err.no.set",
685                                  "No result-set generated"));
686             return -3;
687         }        
688     }
689     return 0;
690 }
691
692 static int exec_command (const char *str)
693 {
694     struct ccl_token *cmd = ccl_tokenize (str);
695     int no;
696
697     if (cmd->kind != CCL_TOK_EOL &&
698         (no = command_search (command_tab, cmd, "ccl.command.")))
699     {
700         if (!info.zass && no != 9 && no != 4)
701             reopen_target ();
702         fprintf (reply_fd, "\n> %s\n", str);
703         if (!info.zass && (no == 1 || no == 2 || no == 3))
704         {
705             fprintf (reply_fd, "%s\n",
706                      gw_res_get (info.kernel_res, "gw.err.no.target",
707                                  "No connection established - "
708                                  "command ignored"));
709             return 0;
710         }
711         switch (no)
712         {
713         case 1:
714             return exec_find (cmd->next);
715         case 2:
716             return exec_show (cmd->next);
717         case 3:
718             return exec_base (cmd->next);
719         case 4:
720             return exec_help (cmd->next);
721         case 9:
722             return exec_target (cmd->next);
723         default:
724             fprintf (reply_fd, "%s\n",
725                      gw_res_get (info.kernel_res, "gw.err.unimplemented",
726                                  "Not implemented yet"));
727         }
728     }
729     else
730     {
731         fprintf (reply_fd, "\n> %s\n", str);
732         fprintf (reply_fd, "  ^ %s\n", 
733                  gw_res_get (info.kernel_res, "gw.err.unknown.command",
734                              "unknown command. "
735                              "Use help to see list of commands"));
736     }
737     return 0;
738 }
739
740 int urp_start (int continuation, struct str_queue *queue)
741 {
742     char subject_str[128];
743
744     info.command_no = 0;
745     info.reply_fname = NULL;
746
747     if (email_header (queue, info.from_str, subject_str))
748     {
749         gw_log (GW_LOG_WARN, KERNEL_LOG, "No message body");
750         return -1;
751     }
752     tty_init (stdout, 40, 70);
753     if (*info.from_str)
754     {
755         info.reply_fname = tempnam (gw_res_get (info.kernel_res,
756                                            "gw.reply.tmp.dir", NULL),
757                                gw_res_get (info.kernel_res,
758                                            "gw.reply.tmp.prefix", "gwr"));
759                                                  
760         reply_fd = fopen (info.reply_fname, "w");
761         if (!reply_fd)
762         {
763             gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, KERNEL_LOG, "Cannot create %s",
764                     info.reply_fname);
765             return -1;
766         }
767         tty_init (reply_fd, 0, 0);
768         fprintf (reply_fd, "From: %s\n",
769                  gw_res_get (info.kernel_res, "gw.msg.from","Email-gateway"));
770         fprintf (reply_fd, "Subject: ");
771         if (*subject_str)
772             fprintf (reply_fd, "Z39.50 Re: %s", subject_str);
773         else
774             fprintf (reply_fd, "%s\n", gw_res_get (info.kernel_res,
775                                                    "gw.msg.subject",
776                                                    "Your Query"));
777         fprintf (reply_fd, "\n");
778         gw_log (GW_LOG_ACCT, KERNEL_LOG, "User start %s", info.from_str);
779     }
780     else
781         gw_log (GW_LOG_WARN, KERNEL_LOG, "No From in email header");
782     fprintf (reply_fd, "%s\n", gw_res_get (info.kernel_res, "gw.msg.greeting",
783                                            "Email->Z39.50 gateway"));
784     if (continuation)
785         fprintf (reply_fd, "%s\n", gw_res_get (info.kernel_res,
786                                                "gw.msg.cont",
787                                                "Continued..."));
788     ccl_token_and = gw_res_get (info.kernel_res, "ccl.token.and", "and");
789     ccl_token_or = gw_res_get (info.kernel_res, "ccl.token.or", "or");
790     ccl_token_not = gw_res_get (info.kernel_res, "ccl.token.not", "not");
791     ccl_token_set = gw_res_get (info.kernel_res, "ccl.token.set", "set");
792     return 0;
793 }
794
795 int urp_command (struct str_queue *queue)
796 {
797     char *cp;
798
799     while (str_queue_deq (queue, line_buf, LINE_MAX))
800     {
801         if (line_buf[0] == '\n')
802             if (info.command_no)
803             {
804                 while (str_queue_deq (queue, 0, 0))
805                     ;
806                 break;
807             }
808             else 
809                 continue;
810         if ((cp = strchr (line_buf, '\n')))
811             *cp = '\0';
812         gw_log (GW_LOG_ACCT, KERNEL_LOG, "cmd: %s", line_buf);
813         if (isalpha (line_buf[0]))
814             exec_command (line_buf);
815         info.command_no++;
816     }
817     return 0;
818 }
819
820 void urp_end (void)
821 {
822     if (!info.command_no)
823     {
824         fprintf (reply_fd, "%s\n", 
825                  gw_res_get (info.kernel_res, "gw.err.nullbody", "No body"));
826         help_general ();
827     }
828     if (*info.from_str)
829     {
830         const char *mta;
831         char cmd[256];
832         int mta_code;
833
834         assert (info.reply_fname);
835         fclose (reply_fd);
836         reply_fd = stdout;
837
838         mta = gw_res_get (info.kernel_res, "gw.reply.mta",
839                           "/usr/lib/sendmail");
840         sprintf (cmd, "%s %s < %s", mta, info.from_str, info.reply_fname);
841         
842         mta_code = system (cmd);
843         if (mta_code)
844             gw_log (GW_LOG_FATAL, KERNEL_LOG,
845                     "Reply '%s' got exit code %d", cmd, mta_code);
846         else
847             unlink (info.reply_fname);        
848         gw_log (GW_LOG_ACCT, KERNEL_LOG, "User end %s", info.from_str);
849     }
850 }
851