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