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