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