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