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