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