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