Bug fix: account from previous session had effect when new target command
[egate.git] / kernel / urp.c
1 /*
2  * Copyright (c) 1995, the EUROPAGATE consortium (see below).
3  *
4  * The EUROPAGATE consortium members are:
5  *
6  *    University College Dublin
7  *    Danmarks Teknologiske Videnscenter
8  *    An Chomhairle Leabharlanna
9  *    Consejo Superior de Investigaciones Cientificas
10  *
11  * Permission to use, copy, modify, distribute, and sell this software and
12  * its documentation, in whole or in part, for any purpose, is hereby granted,
13  * provided that:
14  *
15  * 1. This copyright and permission notice appear in all copies of the
16  * software and its documentation. Notices of copyright or attribution
17  * which appear at the beginning of any file must remain unchanged.
18  *
19  * 2. The names of EUROPAGATE or the project partners may not be used to
20  * endorse or promote products derived from this software without specific
21  * prior written permission.
22  *
23  * 3. Users of this software (implementors and gateway operators) agree to
24  * inform the EUROPAGATE consortium of their use of the software. This
25  * information will be used to evaluate the EUROPAGATE project and the
26  * software, and to plan further developments. The consortium may use
27  * the information in later publications.
28  * 
29  * 4. Users of this software agree to make their best efforts, when
30  * documenting their use of the software, to acknowledge the EUROPAGATE
31  * consortium, and the role played by the software in their work.
32  *
33  * THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT WARRANTY OF ANY KIND,
34  * EXPRESS, IMPLIED, OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
35  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
36  * IN NO EVENT SHALL THE EUROPAGATE CONSORTIUM OR ITS MEMBERS BE LIABLE
37  * FOR ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF
38  * ANY KIND, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA
39  * OR PROFITS, WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND
40  * ON ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
41  * USE OR PERFORMANCE OF THIS SOFTWARE.
42  *
43  */
44 /* Gateway kernel - User Request Processor
45  * Europagate, 1995
46  *
47  * $Log: urp.c,v $
48  * Revision 1.46  1995/07/28 10:51:09  adam
49  * Bug fix: account from previous session had effect when new target command
50  * was executed.
51  *
52  * Revision 1.45  1995/07/20  08:20:24  adam
53  * Minor changes.
54  *
55  * Revision 1.44  1995/07/11  12:28:53  adam
56  * New functions ccl_token_simple and ccl_token_del used.
57  *
58  * Revision 1.43  1995/07/11  11:49:13  adam
59  * LINE_MAX renamed to STR_LINE_MAX.
60  *
61  * Revision 1.42  1995/07/03  12:59:29  adam
62  * New option for eti: -c dir to chdir before start.
63  * Setting change: gw.max.show defaults to 100.
64  *
65  * Revision 1.41  1995/07/03  08:20:18  adam
66  * More help information and better diagnostics.
67  *
68  * Revision 1.40  1995/05/23  08:13:00  adam
69  * Minor changes.
70  *
71  * Revision 1.39  1995/05/22  14:57:30  adam
72  * Next default record position also set by last p=... in show.
73  *
74  * Revision 1.38  1995/05/19  13:26:00  adam
75  * Bug fixes. Better command line options.
76  *
77  * Revision 1.37  1995/05/18  12:03:09  adam
78  * Bug fixes and minor improvements.
79  *
80  * Revision 1.36  1995/05/17  10:51:33  adam
81  * Added a few more error checks to the show command.
82  *
83  * Revision 1.35  1995/05/16  09:40:43  adam
84  * LICENSE. Setting of CCL token names (and/or/not/set) in read_kernel_res.
85  *
86  * Revision 1.34  1995/05/04  10:40:08  adam
87  * More work on Def-settings.
88  *
89  * Revision 1.33  1995/05/03  16:34:19  adam
90  * CCL def command, i.e. user definitions - saved as resource files.
91  *
92  * Revision 1.32  1995/05/03  07:37:46  adam
93  * CCL commands stop/continue implemented. New functions gw_res_{int,bool}
94  * are used when possible.
95  *
96  * Revision 1.31  1995/05/01  12:43:38  adam
97  * First work on resource monitor program.
98  *
99  * Revision 1.30  1995/04/20  16:10:47  adam
100  * Modified to work with non-blocking zass-api. Not using non-blocking
101  * facility yet.
102  *
103  * Revision 1.29  1995/04/19  16:01:58  adam
104  * Some hacks to get the FIFO communication work!! Isn't reliable.
105  * Resource gw.account added - default account info.
106  *
107  * Revision 1.28  1995/04/19  13:19:09  adam
108  * New command: account - for authentication.
109  *
110  * Revision 1.27  1995/04/19  10:46:19  adam
111  * Persistency works much better now. New command: status - history-like
112  *
113  * Revision 1.26  1995/04/19  07:31:12  adam
114  * First work on Z39.50 persistence.
115  *
116  * Revision 1.25  1995/04/17  09:34:33  adam
117  * Timeout (idletime) adjustable. Minor changes in kernel.
118  *
119  * Revision 1.24  1995/03/28  11:42:35  adam
120  * First use of string-queue utility.
121  *
122  * Revision 1.23  1995/03/28  08:01:28  adam
123  * FIFO existence is used to test for a running kernel.
124  *
125  * Revision 1.22  1995/03/27  12:51:05  adam
126  * New log level in use: GW_LOG_ERRNO.
127  *
128  * Revision 1.21  1995/03/27  08:24:04  adam
129  * First use of gip interface and gw-db.
130  * First work on eti program.
131  *
132  * Revision 1.20  1995/03/03  17:19:17  adam
133  * Smarter presentation. Bug fix in email header interpretation.
134  *
135  * Revision 1.19  1995/03/02  09:32:11  adam
136  * New presentation formats. f0=full, f1=brief, f2=mid
137  *
138  * Revision 1.18  1995/03/01  14:32:26  adam
139  * Better diagnostics. Default is, that only one database selected when
140  * several are known.
141  *
142  * Revision 1.17  1995/02/28  13:16:26  adam
143  * Configurable From: added.
144  *
145  * Revision 1.16  1995/02/23  10:08:20  adam
146  * Added logging of all user commands.
147  *
148  * Revision 1.15  1995/02/23  08:32:17  adam
149  * Changed header.
150  *
151  * Revision 1.13  1995/02/22  16:54:42  adam
152  * Qualifiers of LOC target updated. More logging messages.
153  *
154  * Revision 1.12  1995/02/22  15:51:51  adam
155  * Bug fix: swap of parameter number and offset in function present.
156  *
157  * Revision 1.11  1995/02/22  15:22:33  adam
158  * Much more checking of run-time state. Show command never retrieves
159  * more records than indicated by the previous search request. Help
160  * command available. The maximum number of records retrieved can be
161  * controlled now.
162  *
163  * Revision 1.10  1995/02/22  08:51:35  adam
164  * Output function can be customized in fml, which is used to print
165  * the reply to reply_fd.
166  *
167  * Revision 1.9  1995/02/21  17:46:21  adam
168  * Minor changes.
169  *
170  * Revision 1.8  1995/02/21  12:12:00  adam
171  * Diagnostic record with error info. observed.
172  *
173  * Revision 1.7  1995/02/20  21:16:20  adam
174  * FML support. Bug fixes. Profile for drewdb.
175  *
176  * Revision 1.6  1995/02/17  14:41:14  quinn
177  * Added simple display of records.
178  *
179  * Revision 1.5  1995/02/17  14:22:13  adam
180  * First steps of CCL show command. Not finished yet.
181  *
182  * Revision 1.4  1995/02/17  09:08:36  adam
183  * Reply with subject. CCL base command implemented.
184  *
185  * Revision 1.3  1995/02/16  18:35:09  adam
186  * First use of Zdist library. Search requests are supported.
187  * Present requests are not supported yet.
188  *
189  * Revision 1.2  1995/02/16  13:21:00  adam
190  * Organization of resource files for targets and conversion
191  * language implemented.
192  *
193  * Revision 1.1  1995/02/15  17:45:30  adam
194  * First version of email gateway kernel. Email requests are read
195  * from stdin. The output is transferred to an MTA if 'From' is
196  * found in the header - or stdout if absent. No Z39.50 client is used.
197  *
198  */
199
200 /*
201    Todo:
202      info/status (other name?)
203      better persistence diagnostics
204
205    Optional:
206      automatic information about target-aliases: name, query-support, etc.
207  */
208
209 #include <stdio.h>
210 #include <stdlib.h>
211 #include <assert.h>
212 #include <ctype.h>
213 #include <string.h>
214 #include <unistd.h>
215 #include <fcntl.h>
216
217 #include <lgets.h>
218 #include <ttyemit.h>
219 #include <strqueue.h>
220 #include "kernel.h"
221
222 static void present (const char *set, int offset, int number,
223                      struct ccl_token *format_token);
224
225 static char line_buf[STR_LINE_MAX+1];
226
227 static void put_esc_str (const char *s)
228 {
229     while (*s)
230         tty_emit (*s++);
231 }
232
233 int reopen_target (void)
234 {
235     const char *v;
236     if (info.zass)
237         gw_log (GW_LOG_WARN, KERNEL_LOG, "Zass free...");
238     gw_log (GW_LOG_DEBUG, KERNEL_LOG, "reopen_target");
239     info.zass = zass_open (info.hostname, info.port, NULL, /* complete */
240                            *info.account ? info.account : NULL);
241     if (!info.zass)
242     {
243         gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Cannot connect to target %s:%d",
244                 info.hostname, info.port);
245         fprintf (reply_fd, "%s %s:%d\n", 
246                  gw_res_get (info.kernel_res, "gw.err.connect",
247                              "Cannot connect to target"),
248                  info.hostname, info.port);
249         return -1;
250     }
251     v = gw_res_get (info.kernel_res, "gw.description", NULL);
252     if (v)
253     {
254         put_esc_str (v);
255         fprintf (reply_fd, "\n");
256     }
257     fprintf (reply_fd, "%s %s:%d\n",
258              gw_res_get (info.kernel_res, "gw.msg.connect",
259                          "Connection established to"),
260              info.hostname, info.port);
261     if (*info.databases)
262         fprintf (reply_fd, "%s: %s\n",
263                  gw_res_get (info.kernel_res, "gw.msg.databases",
264                              "Available databases"),
265                  info.databases);
266     if (*info.database)
267         fprintf (reply_fd, "%s: %s\n",
268                  gw_res_get (info.kernel_res, "gw.msg.database",
269                              "Selected databases"),
270                  info.database);
271     if (info.setno >= 0)
272         fprintf (reply_fd, "set=%d\n", info.setno);
273     else
274         fprintf (reply_fd, "set=Default\n");
275     return 0;
276 }
277
278 struct command_word {
279     char *default_value;
280     char *resource_suffix;
281 };
282
283 static int command_search_str (struct command_word *tab, const char *cmd_name,
284                                int cmd_len, const char *resource_prefix)
285 {
286     int no = 1;
287
288     assert (resource_prefix);
289     assert (tab);
290     while (tab->default_value)
291     {
292         char *cp, command_names[60];
293         char resource_name[60];
294         const char *v;
295
296         sprintf (resource_name, "%s%s", resource_prefix,
297                  tab->resource_suffix);
298         v = gw_res_get (info.kernel_res, resource_name, tab->default_value);
299         assert (v);
300         strcpy (command_names, v);
301         cp = command_names;
302         while (1)
303         {
304             char *split;
305
306             if ((split = strchr (cp, ' ')))
307                 *split = '\0';
308             if (cmd_len == strlen(cp) &&
309                 !memcmp (cmd_name, cp, cmd_len))
310                 return no;
311             if (!split)
312                 break;
313             cp = split+1;
314         }        
315         no++;
316         tab++;
317     }
318     return 0;
319 }
320
321 static int command_search (struct command_word *tab, struct ccl_token *cmd,
322                            const char *resource_prefix)
323 {
324     return command_search_str (tab, cmd->name, cmd->len, resource_prefix);
325 }
326
327 static struct error_no_struct {
328     int no;
329     char *resource_name;
330 } error_ccl_tab[] = {
331 {  CCL_ERR_OK, "ok"},
332 {  CCL_ERR_TERM_EXPECTED, "term.expected" },
333 {  CCL_ERR_RP_EXPECTED, "rp.expected" },
334 {  CCL_ERR_SETNAME_EXPECTED, "setname.expected" },
335 {  CCL_ERR_OP_EXPECTED, "op.expected" },
336 {  CCL_ERR_BAD_RP, "bad.rp" },
337 {  CCL_ERR_UNKNOWN_QUAL, "unknown.qual" },
338 {  CCL_ERR_DOUBLE_QUAL, "double.qual" },
339 {  CCL_ERR_EQ_EXPECTED, "eq.expected" },
340 {  CCL_ERR_BAD_RELATION, "bad.relation" },
341 {  CCL_ERR_TRUNC_NOT_LEFT, "trunc.not.left" },
342 {  CCL_ERR_TRUNC_NOT_BOTH, "trunc.not.both" },
343 {  CCL_ERR_TRUNC_NOT_RIGHT, "trunc.not.right" },
344 {  0, NULL }
345 };
346
347 static char *error_no_search (struct error_no_struct *tab, int no)
348 {
349     struct error_no_struct *p = tab;
350     while (p->resource_name)
351     {
352         if (no == p->no)
353             return p->resource_name;
354         p++;
355     }
356     return NULL;
357 }
358
359 static int email_header (struct str_queue *sq,
360                          char *from_str, char *subject_str)
361 {
362     *from_str = '\0';
363     *subject_str = '\0';    
364     while (str_queue_deq (sq, line_buf, STR_LINE_MAX))
365     {
366         if (line_buf[0] == '\n')
367             return 0;
368         if (memcmp (line_buf, "From ", 5) == 0)
369             sscanf (line_buf+4, "%s", from_str);
370         if (memcmp (line_buf, "Subject: ", 9) == 0 &&
371             sscanf (line_buf+9, "%s", subject_str+1) == 1)
372             strcpy (subject_str, line_buf+9);
373     }
374     return 1;
375 }
376
377 static void help_general (void)
378 {
379     put_esc_str (gw_res_get (info.kernel_res, "gw.help.general",
380                              "Commands available in this service:\n"));
381 }
382
383 static int exec_help (struct ccl_token *list)
384 {
385     static char *sep = "-------------------------------\\n";
386     help_general ();
387
388     put_esc_str (sep);
389     put_esc_str (gw_res_get (info.kernel_res, "gw.help.target",
390                              "target <name> - selects a given target\n"));
391
392     put_esc_str (sep);
393     put_esc_str (gw_res_get (info.kernel_res, "gw.help.base",
394                              "base <base>..  - selects databases\n"));
395
396     put_esc_str (sep);
397     put_esc_str (gw_res_get (info.kernel_res, "gw.help.find",
398                              "find <query>   - performs a search request\n"));
399
400     put_esc_str (sep);
401     put_esc_str (gw_res_get (info.kernel_res, "gw.help.show",
402                              "show <spec>    - retrieves and displays "
403                              "records\n"));
404     put_esc_str (sep);
405     put_esc_str (gw_res_get (info.kernel_res, "gw.help.def",
406                              "def <setting> <value> sets/gets definitons\n"));
407     put_esc_str (sep);
408     put_esc_str (gw_res_get (info.kernel_res, "gw.help.account",
409                              "account <str>    sets authentication string\n"));
410     return 0;
411 }
412
413 static void display_diag_error (int code, const char *addinfo)
414 {
415     static char str[20];
416
417     sprintf (str, "gw.bib1.diag.%d", code);
418     fprintf (reply_fd, "%s %d:\n %s: '%s'\n",
419              gw_res_get (info.kernel_res, "gw.msg.z39errcode", 
420                          "Z39.50 Error"),
421              code,
422              gw_res_get (info.kernel_res, str, ""), addinfo);
423 }
424
425 static int exec_find (struct ccl_token *list, const char *search_str)
426 {
427     const struct zass_searchent *p;
428     struct gw_user_set *us;
429     char setname[32];
430     int autoshow;
431
432     struct ccl_rpn_node *rpn;
433     int error;
434     const char *pos;
435
436     if (info.setno == -1)
437         strcpy (setname, "Default");
438     else
439         sprintf (setname, "%d", info.setno);
440     info.next_position = 1;
441     if (info.setno >= 0)
442         info.setno++;
443     rpn = ccl_find (info.bibset, list, &error, &pos);
444     if (!rpn)
445     {
446         const char *v = NULL, *n;
447         char name[128];
448
449         fprintf (reply_fd, "  %*s^ - ", pos - line_buf, " ");
450
451         n = error_no_search (error_ccl_tab, error);
452         if (n)
453         {
454             sprintf (name, "gw.err.%s", n);
455             v = gw_res_get (info.kernel_res, name, NULL);
456         }
457         if (!v)
458             v = ccl_err_msg (error);
459         fprintf (reply_fd, "%s\n", v);
460         return -1;
461     }
462     ccl_pr_tree (rpn, reply_fd);
463     fprintf (reply_fd, "\n");
464
465     if (!*info.database )
466     {
467         fprintf (reply_fd, "%s\n",
468                  gw_res_get (info.kernel_res, "gw.err.no.database",
469                              "You must select database"));
470         return -3;
471     }
472     gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Searching in database %s",
473             info.database );
474     assert (info.zass);
475     p = zass_p_search (info.zass, rpn, setname, info.database, info.sets);
476     if (!p)
477     {
478         fprintf (reply_fd, "%s\n",
479                  gw_res_get (info.kernel_res, "gw.err.search.fail",
480                              "Search fail."));
481         return -1;
482     }
483     if (p->errcode != -1)
484     {
485         display_diag_error (p->errcode, p->errstring);
486         return -2;
487     }
488     fprintf (reply_fd, "%d %s %s\n", p->num,
489              gw_res_get (info.kernel_res, "gw.msg.hits", "hit(s) in set "),
490              setname);
491     us = user_set_add (setname, p->num, info.database, rpn, 1, search_str);
492     autoshow = gw_res_int (info.kernel_res, "gw.auto.show", 0);
493     if (autoshow && p->num > 0)
494     {
495         if (autoshow > p->num)
496         {
497             present (setname, 1, p->num, NULL);
498             info.next_position = 1+p->num;
499         }
500         else
501         {
502             present (setname, 1, autoshow, NULL);
503             info.next_position = 1+autoshow;
504         }
505     }
506     return 0;
507 }
508
509 static int exec_account (struct ccl_token *list)
510 {
511     if (list->kind != CCL_TOK_EOL)
512     {
513         int len = list->len;
514         memcpy (info.account, list->name, len);
515         info.target[len] = '\0';
516     }
517     else
518         *info.account = '\0';
519     info.account_in_session = 1;
520     return 0;
521 }
522
523 static int exec_target (struct ccl_token *list)
524 {
525     int len;
526     if (list->kind == CCL_TOK_EOL)
527         return -1;
528     len = list->len;
529     memcpy (info.target, list->name, len);
530     info.target [len] = '\0';
531     
532     if (!info.account_in_session)
533         *info.account = '\0';
534     info.account_in_session = 0;
535     read_kernel_res ();
536     return reopen_target ();
537 }
538
539 static void exec_status_r (struct gw_user_set *sp)
540 {
541     if (!sp)
542         return;
543     exec_status_r (sp->prev);
544     fprintf (reply_fd, "%6s %7d %12.12s   %.50s\n", sp->name, sp->hits,
545              sp->database, sp->search_str);
546 }
547
548 static int exec_status (struct ccl_token *list)
549 {
550     fprintf (reply_fd, "%s\n",
551              gw_res_get (info.kernel_res,
552                          "gw.msg.statusline",
553                          "  Name    Hits     Database   Find"));
554     exec_status_r (info.sets);
555     return 0;
556 }
557
558 static int exec_base (struct ccl_token *list)
559 {
560     struct ccl_token *li = list;
561     int len = 0;
562
563     assert (info.zass);
564     if (list->kind == CCL_TOK_EOL)
565         return -1;
566     free (info.database);
567     while (li->kind != CCL_TOK_EOL)
568     {
569         len += li->len + 1;
570         li = li->next;
571         if (li->kind == CCL_TOK_COMMA)
572             li = li->next;
573     }
574     info.database  = malloc (len);
575     assert (info.database );
576     len = 0;
577     li = list;
578     while (li->kind != CCL_TOK_EOL)
579     {
580         memcpy (info.database+len, li->name, li->len);
581         len += li->len;
582         info.database[len++] = ',';
583         li = li->next;
584         if (li->kind == CCL_TOK_COMMA)
585             li = li->next;
586     }
587     info.database[len-1] = '\0';
588     return 0;
589 }
590
591 static void present (const char *set, int offset, int number,
592                      struct ccl_token *format_token)
593 {
594     const struct zass_presentent *zp;
595     int len;
596     int max_number;
597     char format_str[16];
598     
599     max_number = gw_res_int (info.kernel_res, "gw.max.show", 100);
600     if (number > max_number)
601         number = max_number;
602     gw_log (GW_LOG_DEBUG, KERNEL_LOG, "present in set %s", set);
603     gw_log (GW_LOG_DEBUG, KERNEL_LOG, "present of %d records from offset %d",
604             number, offset);
605     zp = zass_p_present(info.zass, (char *) set, offset, number);
606     if (zp)
607     {
608         int i;
609         zass_record *pp;
610         int  record_log_fd = -1;
611         const char *record_log_name;
612
613         record_log_name = gw_res_get (info.kernel_res, "gw.marc.log",
614                                       NULL);
615         if (record_log_name)
616         {
617             record_log_fd = open (record_log_name, 
618                                   O_WRONLY|O_CREAT|O_APPEND, 0666);
619             if (record_log_fd == -1)
620                 gw_log (GW_LOG_WARN|GW_LOG_ERRNO, "Cannot open %s", 
621                         record_log_name);
622         }
623         fprintf (reply_fd, gw_res_get (info.kernel_res,
624                                        "gw.msg.records",
625                                        "Got %d records from set %s"),
626                  zp->num, set);
627         fprintf (reply_fd, "\n");
628         for (i = 0, pp = zp->records; pp; pp = pp->next, i++)
629         {
630             Iso2709Rec rec;
631 #if USE_FML
632             const char *arg_ar[5];
633 #endif
634             fprintf (reply_fd, "--- %d/%d ---\n",
635                      i+offset, offset+zp->num-1);
636             if (!gw_res_get (info.kernel_res, "gw.ignore.which", NULL))
637             {
638                 if (pp->which == ZASS_REC_DIAG)
639                 {
640                     display_diag_error (pp->errcode, pp->errstring);
641                     continue;
642                 }
643                 else if (pp->which != ZASS_REC_USMARC)
644                 {
645                     fprintf (reply_fd, "Unknown record kind %d\n",
646                              pp->which);
647                     continue;
648                 }
649             }
650             if (record_log_fd != -1)
651                 write (record_log_fd, pp->record, strlen(pp->record));
652             rec = iso2709_cvt (pp->record);
653             if (rec)
654             {
655 #if USE_FML
656                 strcpy (format_str,
657                         gw_res_get (info.kernel_res,
658                                     "gw.display.format", ""));
659                 if (format_token)
660                 {
661                     len = format_token->len;
662                     if (len >= sizeof(format_str))
663                         len = sizeof(format_str)-1;
664                     memcpy (format_str, format_token->name, len);
665                     format_str[len] = '\0';
666                 }
667                 if (info.fml && *format_str && 
668                     (!strcmp (format_str, "0") || !strcmp (format_str, "1")
669                      || !strcmp(format_str, "2")))
670                 {
671                     arg_ar[0] = "\\f";
672                     arg_ar[1] = format_str;
673                     arg_ar[2] = " \\list";
674                     arg_ar[3] = marc_to_str (info.fml, rec);
675                     arg_ar[4] = NULL;
676                     fml_exec_call_argv (info.fml, arg_ar);
677                 }
678                 else
679                     iso2709_display (rec, reply_fd);
680 #else
681                 iso2709_display (rec, reply_fd);
682 #endif
683                 iso2709_rm (rec);
684             }
685             else
686                 fprintf (reply_fd, "Not a MARC record\n");
687         }
688         if (record_log_fd != -1)
689             close (record_log_fd);
690     }
691 }
692
693 static int exec_show (struct ccl_token *list)
694 {
695     static struct command_word show_tab [] = 
696     {
697     {   "f", "format"},
698     {   "p", "position"},
699     {   NULL, NULL }
700     };
701     char tmp_str[20];
702     struct ccl_token *set_token = NULL;
703     struct ccl_token *format_token = NULL;
704     struct ccl_token *li = list;
705     int no_of_present = 0;
706
707     assert (info.zass);
708     while (li->kind != CCL_TOK_EOL)
709     {
710         int modifier_no = 0;
711         if (li->next->kind == CCL_TOK_EQ)
712         {
713             if (li->kind == CCL_TOK_SET)    /* set = <name> ? */
714             {
715                 li = li->next->next;
716                 set_token = li;
717             }
718             else 
719             {
720                 modifier_no = command_search (show_tab, li, "ccl.token.");
721                 if (!modifier_no)
722                 {
723                     fprintf (reply_fd, "Unknown modifier in show\n");
724                     return -1;
725                 }
726                 li = li->next->next;
727                 if (modifier_no == 1)       /* f = <name> ? */
728                     format_token = li;
729                 else if (modifier_no == 2)  /* p = <name> ? */
730                 {
731                     if (li->kind != CCL_TOK_EOL   /* p = <name> - <name> ? */
732                         && li->next->kind == CCL_TOK_MINUS
733                         && li->next->next != CCL_TOK_EOL)
734                         li = li->next->next;
735                 }
736             }
737             if (!li->next)
738             {
739                 fprintf (reply_fd, "%s\n", "Missing token after '='");
740                 return -2;
741             }
742             li = li->next;
743         }
744         else
745         {
746             int len = li->len;
747             memcpy (tmp_str, li->name, len);
748             tmp_str[len] = '\0';
749             if (atoi(tmp_str) <= 0)
750             {
751                 fprintf (reply_fd, "%s\n",
752                          gw_res_get (info.kernel_res, "gw.err.bad.show",
753                                      "Syntax error"));
754                 return -3;
755             }
756             li = li->next;
757         }
758     }
759     if (set_token)
760         gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Got set=%.*s", set_token->len,
761                 set_token->name);
762     if (format_token)
763         gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Got format=%.*s", format_token->len,
764                 format_token->name);
765
766     li = list;
767     while (li->kind != CCL_TOK_EOL)
768     {
769         int modifier_no = 0;
770         int offset = 0;
771         int number = 0;
772         int len;
773         if (li->next->kind == CCL_TOK_EQ && li->kind != CCL_TOK_SET)
774         {
775             modifier_no = command_search (show_tab, li, "ccl.token.");
776             li = li->next->next;
777             if (modifier_no == 2)  /* p = <name> ? */
778             {
779                 if (li->kind != CCL_TOK_EOL   /* p = <name> - <name> ? */
780                     && li->next->kind == CCL_TOK_MINUS
781                     && li->next->next != CCL_TOK_EOL)
782                 {
783                     len = li->len;
784                     memcpy (tmp_str, li->name, len);
785                     tmp_str [len] = '\0';
786                     offset = atoi (tmp_str);
787                     li = li->next->next;
788
789                     len = li->len;
790                     memcpy (tmp_str, li->name, len);
791                     tmp_str [len] = '\0';
792                     number = atoi (tmp_str) - offset + 1;
793                     if (number <= 0)
794                     {
795                         fprintf (reply_fd, "%s\n",
796                                  gw_res_get (info.kernel_res,
797                                              "gw.err.pos.show",
798                                              "Starting position "
799                                              "greater than ending position"));
800                         return -3;
801                     }
802                 }
803                 else
804                 {
805                     len = li->len;
806                     memcpy (tmp_str, li->name, len);
807                     tmp_str [len] = '\0';
808                     offset = atoi (tmp_str);
809                     number = 1;
810                 }
811             }
812             li = li->next;
813         }
814         else if (li->kind == CCL_TOK_TERM)
815         {
816             len = li->len;
817             memcpy (tmp_str, li->name, len);
818             tmp_str[len] = '\0';
819             number = atoi (tmp_str);
820             offset = 1;
821             li = li->next;
822         }
823         if (offset > 0 && number > 0)
824         {
825             struct gw_user_set *us;
826
827             if (set_token)
828             {
829                 len = set_token->len;
830                 memcpy (tmp_str, set_token->name, len);
831                 tmp_str[len] = '\0';
832                 us = user_set_search (tmp_str);
833             }
834             else
835                 us = user_set_search (NULL);
836             if (us && us->hits != -1) /* proper result-set? */
837             {
838                 if (offset <= us->hits)
839                 {
840                     if (offset+number-1 > us->hits)
841                         number = us->hits - offset+1;
842                     present (us->name, offset, number, format_token);
843                     info.next_position = offset+number;
844                 }
845             }
846             else if (!no_of_present) /* display error message once! */
847             {
848                 fprintf (reply_fd, "%s\n",
849                          gw_res_get (info.kernel_res, "gw.err.no.set",
850                                      "No result-set generated"));
851             }
852             no_of_present++;
853         }
854     }
855     if (!no_of_present) /* no records shown so far? */
856     {
857         struct gw_user_set *us;
858         int default_show;
859         
860         us = user_set_search (NULL);
861         if (us && us->hits != -1)    /* proper result-set? */
862         {
863             default_show = gw_res_int (info.kernel_res, "gw.default.show", 20);
864             if (us->hits >= info.next_position + default_show)
865             {
866                 present (us->name, info.next_position, default_show, 
867                          format_token);
868                 info.next_position += default_show;
869             }
870             else if (us->hits >= info.next_position)
871             {
872                 present (us->name, info.next_position, 
873                          us->hits - info.next_position + 1, 
874                          format_token);
875                 info.next_position = us->hits + 1;
876             }
877         }
878         else                         /* display error message */
879         {
880             fprintf (reply_fd, "%s\n",
881                      gw_res_get (info.kernel_res, "gw.err.no.set",
882                                  "No result-set generated"));
883             return -3;
884         }        
885     }
886     return 0;
887 }
888
889 static void show_def (void)
890 {
891     fprintf (reply_fd, "format       %s\n", gw_res_get
892              (info.kernel_res, "gw.display.format", ""));
893     fprintf (reply_fd, "autoshow     %s\n", gw_res_get
894              (info.kernel_res, "gw.auto.show", ""));
895     fprintf (reply_fd, "defaultshow  %s\n", gw_res_get
896              (info.kernel_res, "gw.default.show", ""));
897     fprintf (reply_fd, "language     %s\n", gw_res_get
898              (info.kernel_res, "gw.language", ""));
899 }
900
901 static int exec_def (struct ccl_token *list)
902 {
903     const char *name = NULL;
904     char value[128];
905     char fname[256];
906     static struct command_word def_tab [] =
907     {
908     {   "reset",       "reset" },
909     {   "show",        "show" },
910     {   "f",           "format"},
911     {   "autoshow",    "autoshow" },
912     {   "defaultshow", "defaultshow" },
913     {   "lang",        "language" },
914     {   NULL, NULL }
915     };
916
917     if (info.userid < 0)
918         return -1;
919     sprintf (fname, "user.%d.r", info.userid);
920
921     if (list->kind == CCL_TOK_EOL) 
922         show_def ();
923     else
924     {
925         int setting_no = command_search (def_tab, list, "ccl.token.");
926
927         if (!setting_no)
928         {
929             fprintf (reply_fd, "Unknown setting in def\n");
930             return -1;
931         }
932         if (setting_no == 1)
933         {
934             unlink (fname);
935             read_kernel_res ();
936             return 0;
937         }
938         else if (setting_no == 2)
939         {
940             show_def ();
941             return 0;
942         }
943         list = list->next;
944         if (list->kind == CCL_TOK_EOL)
945             return 0;
946         strncpy (value, list->name, 127);
947         value[(size_t) list->len] = '\0';
948         switch (setting_no)
949         {
950         case 3:
951             name = "gw.display.format";
952             break;
953         case 4:
954             name = "gw.auto.show";
955             break;
956         case 5:
957             name = "gw.default.show";
958             break;
959         case 6:
960             name = "gw.language";
961             break;
962         default:
963             return 0;
964         }
965         gw_log (GW_LOG_DEBUG, KERNEL_LOG, "update file %s with %s=%s",
966                 fname, name, value);
967         gw_res_put (info.kernel_res, "gw.username", info.from_str, fname);
968         gw_res_put (info.kernel_res, name, value, fname);
969         gw_res_commit (info.kernel_res, fname);
970         if (setting_no == 6)
971             read_kernel_res ();
972     }
973     return 0;
974 }
975
976
977 /*
978  * exec_command: parse and execute ccl command in str.
979  * str:      ccl command string
980  * stop_flag pointer to integer. On completion the integer
981  *           is 1 (stop) or 2 (continue); 0 (other command)
982  * return: 0 success; non-zero otherwise
983  */
984 static int exec_command (const char *str, int *stop_flag)
985 {
986     static struct command_word command_tab [] = 
987     {
988     {   "find",     "find"},
989     {   "show",     "show"},
990     {   "base",     "base" },
991     {   "help",     "help" },
992     {   "info",     "info" },
993     {   "continue", "continue" },
994     {   "status",   "status" },
995     {   "stop",     "stop" },
996     {   "target",   "target" },
997     {   "def",      "def" },
998     {   "account",  "account" },
999     {   NULL, NULL }
1000     };
1001
1002     struct ccl_token *cmd;
1003     const char *cp = str;
1004     int no;
1005
1006     while (*cp && *cp != ' ' && *cp != '\n' && *cp != '\t' && *cp != '\r')
1007         cp++;
1008
1009     *stop_flag = 0;
1010     no = command_search_str (command_tab, str, cp-str, "ccl.command.");
1011     if (no)
1012     {
1013         if (no == 1 || no == 2 || no == 3) /* find/show/base? */
1014         {
1015             if (!info.zass)                /* open if not already opened */
1016                 reopen_target ();
1017             fprintf (reply_fd, "\n> %s\n", str);
1018             if (!info.zass)                /* fail? */
1019             {
1020                 fprintf (reply_fd, "\n%s\n",
1021                          gw_res_get (info.kernel_res, "gw.err.no.target",
1022                                      "No connection established - "
1023                                      "command ignored"));
1024                 return 0;
1025             }
1026         }
1027         else
1028             fprintf (reply_fd, "\n> %s\n", str);
1029         switch (no)
1030         {
1031         case 1: /* find */
1032             cmd = ccl_tokenize (cp);
1033             exec_find (cmd, str);
1034             break;
1035         case 2: /* show */
1036             cmd = ccl_tokenize (cp);
1037             exec_show (cmd);
1038             break;
1039         case 3: /* base */
1040             cmd = ccl_token_simple (cp);
1041             exec_base (cmd);
1042             break;
1043         case 4: /* help */
1044             cmd = ccl_token_simple (cp);
1045             exec_help (cmd);
1046             break;
1047         case 6:  /* continue */
1048             *stop_flag = 2;
1049             return 0;
1050         case 7:  /* status */
1051             exec_status (cmd);
1052             return 0;
1053         case 8:  /* stop */
1054             *stop_flag = 1;
1055             return 0;
1056         case 9:  /* target */
1057             cmd = ccl_token_simple (cp);
1058             exec_target (cmd);
1059             break;
1060         case 10: /* def */
1061             cmd = ccl_token_simple (cp);
1062             exec_def (cmd);
1063             break;
1064         case 11: /* account */
1065             cmd = ccl_token_simple (cp);
1066             exec_account (cmd);
1067             break;
1068         default:
1069             cmd = NULL;
1070             fprintf (reply_fd, "%s\n",
1071                      gw_res_get (info.kernel_res, "gw.err.unimplemented",
1072                                  "Not implemented yet"));
1073         }
1074         ccl_token_del (cmd);
1075     }
1076     else
1077     {
1078         fprintf (reply_fd, "\n> %s\n", str);
1079         fprintf (reply_fd, "  ^ %s\n", 
1080                  gw_res_get (info.kernel_res, "gw.err.unknown.command",
1081                              "unknown command. "
1082                              "Use help to see list of commands"));
1083     }
1084     return 0;
1085 }
1086
1087 int urp_start (int continuation, struct str_queue *queue)
1088 {
1089     char subject_str[128];
1090
1091     info.command_no = 0;
1092     info.reply_fname = NULL;
1093
1094     if (email_header (queue, info.from_str, subject_str))
1095     {
1096         gw_log (GW_LOG_WARN, KERNEL_LOG, "No message body");
1097         return -1;
1098     }
1099     tty_init (stdout, 40, 70);
1100     if (*info.from_str)
1101     {
1102         info.reply_fname = (char *)
1103             tempnam (gw_res_get (info.kernel_res, "gw.reply.tmp.dir", NULL),
1104                      gw_res_get (info.kernel_res,
1105                                  "gw.reply.tmp.prefix", "gwr"));
1106
1107         reply_fd = fopen (info.reply_fname, "w");
1108         if (!reply_fd)
1109         {
1110             gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, KERNEL_LOG, "Cannot create %s",
1111                     info.reply_fname);
1112             return -1;
1113         }
1114         tty_init (reply_fd, 0, 0);
1115         fprintf (reply_fd, "From: %s\n",
1116                  gw_res_get (info.kernel_res, "gw.msg.from","Email-gateway"));
1117         fprintf (reply_fd, "Subject: ");
1118         if (*subject_str)
1119             fprintf (reply_fd, "Z39.50 Re: %s", subject_str);
1120         else
1121             fprintf (reply_fd, "%s\n", gw_res_get (info.kernel_res,
1122                                                    "gw.msg.subject",
1123                                                    "Your Query"));
1124         fprintf (reply_fd, "\n");
1125         gw_log (GW_LOG_ACCT, KERNEL_LOG, "User start %s", info.from_str);
1126     }
1127     else
1128         gw_log (GW_LOG_WARN, KERNEL_LOG, "No From in email header");
1129     fprintf (reply_fd, "%s\n", gw_res_get (info.kernel_res, "gw.msg.greeting",
1130                                            "Email->Z39.50 gateway"));
1131     if (continuation)
1132         fprintf (reply_fd, "%s\n", gw_res_get (info.kernel_res,
1133                                                "gw.msg.cont",
1134                                                "Continued..."));
1135     return 0;
1136 }
1137
1138 int urp_command (struct str_queue *queue)
1139 {
1140     char *cp;
1141     int stop_flag;
1142
1143     while (str_queue_deq (queue, line_buf, STR_LINE_MAX))
1144     {
1145         if (line_buf[0] == '\n')
1146             if (info.command_no)
1147             {
1148                 while (str_queue_deq (queue, 0, 0))
1149                     ;
1150                 break;
1151             }
1152             else 
1153                 continue;
1154         if ((cp = strchr (line_buf, '\n')))
1155             *cp = '\0';
1156         gw_log (GW_LOG_ACCT, KERNEL_LOG, "cmd: %s", line_buf);
1157         if (isalpha (line_buf[0]))
1158         {
1159             exec_command (line_buf, &stop_flag);
1160             if (stop_flag)
1161             {
1162                 info.command_no++;     /* prevent help! */
1163                 while (str_queue_deq (queue, line_buf, STR_LINE_MAX))
1164                      ;
1165                 return stop_flag;
1166             }
1167         }
1168         info.command_no++;
1169     }
1170     return 0;
1171 }
1172
1173 void urp_end (void)
1174 {
1175     if (!info.command_no)
1176     {
1177         fprintf (reply_fd, "%s\n", 
1178                  gw_res_get (info.kernel_res, "gw.err.nullbody", "No body"));
1179         help_general ();
1180     }
1181     if (*info.from_str)
1182     {
1183         const char *mta;
1184         char cmd[256];
1185         int mta_code;
1186
1187         assert (info.reply_fname);
1188         fclose (reply_fd);
1189         reply_fd = stdout;
1190
1191         mta = gw_res_get (info.kernel_res, "gw.reply.mta",
1192                           "/usr/lib/sendmail");
1193         sprintf (cmd, "%s %s < %s", mta, info.from_str, info.reply_fname);
1194         mta_code = system (cmd);
1195         if (mta_code)
1196             gw_log (GW_LOG_FATAL, KERNEL_LOG,
1197                     "Reply '%s' got exit code %d", cmd, mta_code);
1198         else
1199             unlink (info.reply_fname);        
1200         gw_log (GW_LOG_ACCT, KERNEL_LOG, "User end %s", info.from_str);
1201     }
1202 }
1203