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