2 * Copyright (c) 1995, the EUROPAGATE consortium (see below).
4 * The EUROPAGATE consortium members are:
6 * University College Dublin
7 * Danmarks Teknologiske Videnscenter
8 * An Chomhairle Leabharlanna
9 * Consejo Superior de Investigaciones Cientificas
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,
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.
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.
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.
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.
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.
44 /* Gateway kernel - User Request Processor
48 * Revision 1.51 2001/02/26 14:32:36 adam
49 * Updated for YAZ 1.7. HTML output tidy up. Added LOC target.
51 * Revision 1.50 1996/03/01 16:09:30 adam
52 * New setting: gw.msg.replyto that sets Reply-To in user response.
54 * Revision 1.49 1996/03/01 14:25:28 adam
55 * Email gateway obeys 'Reply-To:' in header.
57 * Revision 1.48 1996/02/12 10:04:57 adam
58 * The gateway doesn't try to reconnect if it is already known that
59 * it will fail (connect_fail flag introduced).
61 * Revision 1.47 1996/01/18 13:54:48 adam
62 * Help <command> gives help information about individual commands.
63 * Status without arguments gives list of available predefined targets.
65 * Revision 1.46 1995/07/28 10:51:09 adam
66 * Bug fix: account from previous session had effect when new target command
69 * Revision 1.45 1995/07/20 08:20:24 adam
72 * Revision 1.44 1995/07/11 12:28:53 adam
73 * New functions ccl_token_simple and ccl_token_del used.
75 * Revision 1.43 1995/07/11 11:49:13 adam
76 * LINE_MAX renamed to STR_LINE_MAX.
78 * Revision 1.42 1995/07/03 12:59:29 adam
79 * New option for eti: -c dir to chdir before start.
80 * Setting change: gw.max.show defaults to 100.
82 * Revision 1.41 1995/07/03 08:20:18 adam
83 * More help information and better diagnostics.
85 * Revision 1.40 1995/05/23 08:13:00 adam
88 * Revision 1.39 1995/05/22 14:57:30 adam
89 * Next default record position also set by last p=... in show.
91 * Revision 1.38 1995/05/19 13:26:00 adam
92 * Bug fixes. Better command line options.
94 * Revision 1.37 1995/05/18 12:03:09 adam
95 * Bug fixes and minor improvements.
97 * Revision 1.36 1995/05/17 10:51:33 adam
98 * Added a few more error checks to the show command.
100 * Revision 1.35 1995/05/16 09:40:43 adam
101 * LICENSE. Setting of CCL token names (and/or/not/set) in read_kernel_res.
103 * Revision 1.34 1995/05/04 10:40:08 adam
104 * More work on Def-settings.
106 * Revision 1.33 1995/05/03 16:34:19 adam
107 * CCL def command, i.e. user definitions - saved as resource files.
109 * Revision 1.32 1995/05/03 07:37:46 adam
110 * CCL commands stop/continue implemented. New functions gw_res_{int,bool}
111 * are used when possible.
113 * Revision 1.31 1995/05/01 12:43:38 adam
114 * First work on resource monitor program.
116 * Revision 1.30 1995/04/20 16:10:47 adam
117 * Modified to work with non-blocking zass-api. Not using non-blocking
120 * Revision 1.29 1995/04/19 16:01:58 adam
121 * Some hacks to get the FIFO communication work!! Isn't reliable.
122 * Resource gw.account added - default account info.
124 * Revision 1.28 1995/04/19 13:19:09 adam
125 * New command: account - for authentication.
127 * Revision 1.27 1995/04/19 10:46:19 adam
128 * Persistency works much better now. New command: status - history-like
130 * Revision 1.26 1995/04/19 07:31:12 adam
131 * First work on Z39.50 persistence.
133 * Revision 1.25 1995/04/17 09:34:33 adam
134 * Timeout (idletime) adjustable. Minor changes in kernel.
136 * Revision 1.24 1995/03/28 11:42:35 adam
137 * First use of string-queue utility.
139 * Revision 1.23 1995/03/28 08:01:28 adam
140 * FIFO existence is used to test for a running kernel.
142 * Revision 1.22 1995/03/27 12:51:05 adam
143 * New log level in use: GW_LOG_ERRNO.
145 * Revision 1.21 1995/03/27 08:24:04 adam
146 * First use of gip interface and gw-db.
147 * First work on eti program.
149 * Revision 1.20 1995/03/03 17:19:17 adam
150 * Smarter presentation. Bug fix in email header interpretation.
152 * Revision 1.19 1995/03/02 09:32:11 adam
153 * New presentation formats. f0=full, f1=brief, f2=mid
155 * Revision 1.18 1995/03/01 14:32:26 adam
156 * Better diagnostics. Default is, that only one database selected when
159 * Revision 1.17 1995/02/28 13:16:26 adam
160 * Configurable From: added.
162 * Revision 1.16 1995/02/23 10:08:20 adam
163 * Added logging of all user commands.
165 * Revision 1.15 1995/02/23 08:32:17 adam
168 * Revision 1.13 1995/02/22 16:54:42 adam
169 * Qualifiers of LOC target updated. More logging messages.
171 * Revision 1.12 1995/02/22 15:51:51 adam
172 * Bug fix: swap of parameter number and offset in function present.
174 * Revision 1.11 1995/02/22 15:22:33 adam
175 * Much more checking of run-time state. Show command never retrieves
176 * more records than indicated by the previous search request. Help
177 * command available. The maximum number of records retrieved can be
180 * Revision 1.10 1995/02/22 08:51:35 adam
181 * Output function can be customized in fml, which is used to print
182 * the reply to reply_fd.
184 * Revision 1.9 1995/02/21 17:46:21 adam
187 * Revision 1.8 1995/02/21 12:12:00 adam
188 * Diagnostic record with error info. observed.
190 * Revision 1.7 1995/02/20 21:16:20 adam
191 * FML support. Bug fixes. Profile for drewdb.
193 * Revision 1.6 1995/02/17 14:41:14 quinn
194 * Added simple display of records.
196 * Revision 1.5 1995/02/17 14:22:13 adam
197 * First steps of CCL show command. Not finished yet.
199 * Revision 1.4 1995/02/17 09:08:36 adam
200 * Reply with subject. CCL base command implemented.
202 * Revision 1.3 1995/02/16 18:35:09 adam
203 * First use of Zdist library. Search requests are supported.
204 * Present requests are not supported yet.
206 * Revision 1.2 1995/02/16 13:21:00 adam
207 * Organization of resource files for targets and conversion
208 * language implemented.
210 * Revision 1.1 1995/02/15 17:45:30 adam
211 * First version of email gateway kernel. Email requests are read
212 * from stdin. The output is transferred to an MTA if 'From' is
213 * found in the header - or stdout if absent. No Z39.50 client is used.
219 info/status (other name?)
220 better persistence diagnostics
223 automatic information about target-aliases: name, query-support, etc.
236 #include <strqueue.h>
239 static void present (const char *set, int offset, int number,
240 struct ccl_token *format_token);
242 static char line_buf[STR_LINE_MAX+1];
244 static void put_esc_str (const char *s)
250 int reopen_target (void)
254 gw_log (GW_LOG_WARN, KERNEL_LOG, "Zass free...");
255 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "reopen_target");
256 info.zass = zass_open (info.hostname, info.port, NULL, /* complete */
257 *info.account ? info.account : NULL);
260 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Cannot connect to target %s:%d",
261 info.hostname, info.port);
262 fprintf (reply_fd, "%s %s:%d\n",
263 gw_res_get (info.kernel_res, "gw.err.connect",
264 "Cannot connect to target"),
265 info.hostname, info.port);
266 info.connect_failed = 1;
269 v = gw_res_get (info.kernel_res, "gw.description", NULL);
273 fprintf (reply_fd, "\n");
275 fprintf (reply_fd, "%s %s:%d\n",
276 gw_res_get (info.kernel_res, "gw.msg.connect",
277 "Connection established to"),
278 info.hostname, info.port);
280 fprintf (reply_fd, "%s: %s\n",
281 gw_res_get (info.kernel_res, "gw.msg.databases",
282 "Available databases"),
285 fprintf (reply_fd, "%s: %s\n",
286 gw_res_get (info.kernel_res, "gw.msg.database",
287 "Selected databases"),
290 fprintf (reply_fd, "set=%d\n", info.setno);
292 fprintf (reply_fd, "set=Default\n");
296 struct command_word {
298 char *resource_suffix;
301 static int command_search_str (struct command_word *tab, const char *cmd_name,
302 int cmd_len, const char *resource_prefix)
306 assert (resource_prefix);
308 while (tab->default_value)
310 char *cp, command_names[60];
311 char resource_name[60];
314 sprintf (resource_name, "%s%s", resource_prefix,
315 tab->resource_suffix);
316 v = gw_res_get (info.kernel_res, resource_name, tab->default_value);
318 strcpy (command_names, v);
324 if ((split = strchr (cp, ' ')))
326 if (cmd_len == strlen(cp) &&
327 !memcmp (cmd_name, cp, cmd_len))
339 static int command_search (struct command_word *tab, struct ccl_token *cmd,
340 const char *resource_prefix)
342 return command_search_str (tab, cmd->name, cmd->len, resource_prefix);
345 static struct error_no_struct {
348 } error_ccl_tab[] = {
350 { CCL_ERR_TERM_EXPECTED, "term.expected" },
351 { CCL_ERR_RP_EXPECTED, "rp.expected" },
352 { CCL_ERR_SETNAME_EXPECTED, "setname.expected" },
353 { CCL_ERR_OP_EXPECTED, "op.expected" },
354 { CCL_ERR_BAD_RP, "bad.rp" },
355 { CCL_ERR_UNKNOWN_QUAL, "unknown.qual" },
356 { CCL_ERR_DOUBLE_QUAL, "double.qual" },
357 { CCL_ERR_EQ_EXPECTED, "eq.expected" },
358 { CCL_ERR_BAD_RELATION, "bad.relation" },
359 { CCL_ERR_TRUNC_NOT_LEFT, "trunc.not.left" },
360 { CCL_ERR_TRUNC_NOT_BOTH, "trunc.not.both" },
361 { CCL_ERR_TRUNC_NOT_RIGHT, "trunc.not.right" },
365 static char *error_no_search (struct error_no_struct *tab, int no)
367 struct error_no_struct *p = tab;
368 while (p->resource_name)
371 return p->resource_name;
377 static int email_header (struct str_queue *sq,
378 char *from_str, char *subject_str)
382 while (str_queue_deq (sq, line_buf, STR_LINE_MAX))
384 if (line_buf[0] == '\n')
386 if (memcmp (line_buf, "From ", 5) == 0)
387 sscanf (line_buf+4, "%s", from_str);
388 if (memcmp (line_buf, "Reply-To: ", 10) == 0)
389 sscanf (line_buf+10, "%s", from_str);
390 if (memcmp (line_buf, "Subject: ", 9) == 0 &&
391 sscanf (line_buf+9, "%s", subject_str+1) == 1)
392 strcpy (subject_str, line_buf+9);
397 static void help_general (void)
399 put_esc_str (gw_res_get (info.kernel_res, "gw.help.general",
400 "Commands available in this service:\n"));
403 static struct command_word command_tab [] =
410 { "continue", "continue" },
411 { "status", "status" },
413 { "target", "target" },
415 { "account", "account" },
420 static int exec_help (struct ccl_token *list)
423 static char *sep = "-------------------------------\\n";
424 if (list->kind != CCL_TOK_EOL)
425 no = command_search_str (command_tab, list->name, list->len,
433 put_esc_str (gw_res_get (info.kernel_res, "gw.help.target",
434 "target <name> - selects a given target\n"));
440 put_esc_str (gw_res_get (info.kernel_res, "gw.help.base",
441 "base <base>.. - selects databases\n"));
447 put_esc_str (gw_res_get (info.kernel_res, "gw.help.find",
448 "find <query> - performs a search\n"));
454 put_esc_str (gw_res_get (info.kernel_res, "gw.help.show",
455 "show <spec> - retrieves and displays "
462 put_esc_str (gw_res_get (info.kernel_res, "gw.help.def",
463 "def <setting> <value> user definitons\n"));
469 put_esc_str (gw_res_get (info.kernel_res, "gw.help.account",
470 "account <str> sets authentication\n"));
476 put_esc_str (gw_res_get (info.kernel_res, "gw.help.status",
477 "status status information\n"));
483 static void display_diag_error (int code, const char *addinfo)
487 sprintf (str, "gw.bib1.diag.%d", code);
488 fprintf (reply_fd, "%s %d:\n %s: '%s'\n",
489 gw_res_get (info.kernel_res, "gw.msg.z39errcode",
492 gw_res_get (info.kernel_res, str, ""), addinfo);
495 static int exec_find (struct ccl_token *list, const char *search_str)
497 const struct zass_searchent *p;
498 struct gw_user_set *us;
502 struct ccl_rpn_node *rpn;
506 if (info.setno == -1)
507 strcpy (setname, "Default");
509 sprintf (setname, "%d", info.setno);
510 info.next_position = 1;
513 rpn = ccl_find (info.bibset, list, &error, &pos);
516 const char *v = NULL, *n;
519 fprintf (reply_fd, " %*s^ - ", pos - line_buf, " ");
521 n = error_no_search (error_ccl_tab, error);
524 sprintf (name, "gw.err.%s", n);
525 v = gw_res_get (info.kernel_res, name, NULL);
528 v = ccl_err_msg (error);
529 fprintf (reply_fd, "%s\n", v);
532 ccl_pr_tree (rpn, reply_fd);
533 fprintf (reply_fd, "\n");
535 if (!*info.database )
537 fprintf (reply_fd, "%s\n",
538 gw_res_get (info.kernel_res, "gw.err.no.database",
539 "You must select database"));
542 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Searching in database %s",
545 p = zass_p_search (info.zass, rpn, setname, info.database, info.sets);
548 fprintf (reply_fd, "%s\n",
549 gw_res_get (info.kernel_res, "gw.err.search.fail",
553 if (p->errcode != -1)
555 display_diag_error (p->errcode, p->errstring);
558 fprintf (reply_fd, "%d %s %s\n", p->num,
559 gw_res_get (info.kernel_res, "gw.msg.hits", "hit(s) in set "),
561 us = user_set_add (setname, p->num, info.database, rpn, 1, search_str);
562 autoshow = gw_res_int (info.kernel_res, "gw.auto.show", 0);
563 if (autoshow && p->num > 0)
565 if (autoshow > p->num)
567 present (setname, 1, p->num, NULL);
568 info.next_position = 1+p->num;
572 present (setname, 1, autoshow, NULL);
573 info.next_position = 1+autoshow;
579 static int exec_account (struct ccl_token *list)
581 if (list->kind != CCL_TOK_EOL)
584 memcpy (info.account, list->name, len);
585 info.target[len] = '\0';
588 *info.account = '\0';
589 info.account_in_session = 1;
593 void handle_target_list (const char *name, const char *value)
598 if (strlen(name) < 10)
600 if (memcmp (name, "gw.target.", 10))
602 fprintf (reply_fd, "%s\n", name+10);
603 res = gw_res_init ();
604 gw_res_merge (res, value);
605 p = gw_res_get (res, "gw.description", NULL);
611 p = gw_res_get (res, "gw.databases", NULL);
613 fprintf (reply_fd, " Databases: %s\n", p);
614 fprintf (reply_fd, "\n");
618 static int exec_target_list (void)
620 gw_res_trav (info.kernel_res, NULL, handle_target_list);
621 fprintf (reply_fd, "\n");
625 static int exec_target (struct ccl_token *list)
628 if (list->kind == CCL_TOK_EOL)
629 return exec_target_list ();
631 memcpy (info.target, list->name, len);
632 info.target [len] = '\0';
634 if (!info.account_in_session)
635 *info.account = '\0';
636 info.account_in_session = 0;
638 info.connect_failed = 0;
639 return reopen_target ();
642 static void exec_status_r (struct gw_user_set *sp)
646 exec_status_r (sp->prev);
647 fprintf (reply_fd, "%6s %7d %12.12s %.50s\n", sp->name, sp->hits,
648 sp->database, sp->search_str);
651 static int exec_status (struct ccl_token *list)
653 fprintf (reply_fd, "%s\n",
654 gw_res_get (info.kernel_res,
656 " Name Hits Database Find"));
657 exec_status_r (info.sets);
661 static int exec_base (struct ccl_token *list)
663 struct ccl_token *li = list;
667 if (list->kind == CCL_TOK_EOL)
669 free (info.database);
670 while (li->kind != CCL_TOK_EOL)
674 if (li->kind == CCL_TOK_COMMA)
677 info.database = malloc (len);
678 assert (info.database );
681 while (li->kind != CCL_TOK_EOL)
683 memcpy (info.database+len, li->name, li->len);
685 info.database[len++] = ',';
687 if (li->kind == CCL_TOK_COMMA)
690 info.database[len-1] = '\0';
694 static void present (const char *set, int offset, int number,
695 struct ccl_token *format_token)
697 const struct zass_presentent *zp;
702 max_number = gw_res_int (info.kernel_res, "gw.max.show", 100);
703 if (number > max_number)
705 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "present in set %s", set);
706 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "present of %d records from offset %d",
708 zp = zass_p_present(info.zass, (char *) set, offset, number);
713 int record_log_fd = -1;
714 const char *record_log_name;
716 record_log_name = gw_res_get (info.kernel_res, "gw.marc.log",
720 record_log_fd = open (record_log_name,
721 O_WRONLY|O_CREAT|O_APPEND, 0666);
722 if (record_log_fd == -1)
723 gw_log (GW_LOG_WARN|GW_LOG_ERRNO, "Cannot open %s",
726 fprintf (reply_fd, gw_res_get (info.kernel_res,
728 "Got %d records from set %s"),
730 fprintf (reply_fd, "\n");
731 for (i = 0, pp = zp->records; pp; pp = pp->next, i++)
735 const char *arg_ar[5];
737 fprintf (reply_fd, "--- %d/%d ---\n",
738 i+offset, offset+zp->num-1);
739 if (!gw_res_get (info.kernel_res, "gw.ignore.which", NULL))
741 if (pp->which == ZASS_REC_DIAG)
743 display_diag_error (pp->errcode, pp->errstring);
746 else if (pp->which != ZASS_REC_USMARC)
748 fprintf (reply_fd, "Unknown record kind %d\n",
755 if (record_log_fd != -1)
756 write (record_log_fd, pp->record, strlen(pp->record));
757 rec = iso2709_cvt (pp->record);
762 gw_res_get (info.kernel_res,
763 "gw.display.format", ""));
766 len = format_token->len;
767 if (len >= sizeof(format_str))
768 len = sizeof(format_str)-1;
769 memcpy (format_str, format_token->name, len);
770 format_str[len] = '\0';
772 if (info.fml && *format_str &&
773 (!strcmp (format_str, "0") || !strcmp (format_str, "1")
774 || !strcmp(format_str, "2")))
777 arg_ar[1] = format_str;
778 arg_ar[2] = " \\list";
779 arg_ar[3] = marc_to_str (info.fml, rec);
781 fml_exec_call_argv (info.fml, arg_ar);
784 iso2709_display (rec, reply_fd);
786 iso2709_display (rec, reply_fd);
791 fprintf (reply_fd, "Record type not supported\n");
794 fprintf (reply_fd, "Not a MARC record\n");
796 if (record_log_fd != -1)
797 close (record_log_fd);
801 static int exec_show (struct ccl_token *list)
803 static struct command_word show_tab [] =
810 struct ccl_token *set_token = NULL;
811 struct ccl_token *format_token = NULL;
812 struct ccl_token *li = list;
813 int no_of_present = 0;
816 while (li->kind != CCL_TOK_EOL)
819 if (li->next->kind == CCL_TOK_EQ)
821 if (li->kind == CCL_TOK_SET) /* set = <name> ? */
828 modifier_no = command_search (show_tab, li, "ccl.token.");
831 fprintf (reply_fd, "Unknown modifier in show\n");
835 if (modifier_no == 1) /* f = <name> ? */
837 else if (modifier_no == 2) /* p = <name> ? */
839 if (li->kind != CCL_TOK_EOL /* p = <name> - <name> ? */
840 && li->next->kind == CCL_TOK_MINUS
841 && li->next->next != CCL_TOK_EOL)
847 fprintf (reply_fd, "%s\n", "Missing token after '='");
855 memcpy (tmp_str, li->name, len);
857 if (atoi(tmp_str) <= 0)
859 fprintf (reply_fd, "%s\n",
860 gw_res_get (info.kernel_res, "gw.err.bad.show",
868 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Got set=%.*s", set_token->len,
871 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Got format=%.*s", format_token->len,
875 while (li->kind != CCL_TOK_EOL)
881 if (li->next->kind == CCL_TOK_EQ && li->kind != CCL_TOK_SET)
883 modifier_no = command_search (show_tab, li, "ccl.token.");
885 if (modifier_no == 2) /* p = <name> ? */
887 if (li->kind != CCL_TOK_EOL /* p = <name> - <name> ? */
888 && li->next->kind == CCL_TOK_MINUS
889 && li->next->next != CCL_TOK_EOL)
892 memcpy (tmp_str, li->name, len);
893 tmp_str [len] = '\0';
894 offset = atoi (tmp_str);
898 memcpy (tmp_str, li->name, len);
899 tmp_str [len] = '\0';
900 number = atoi (tmp_str) - offset + 1;
903 fprintf (reply_fd, "%s\n",
904 gw_res_get (info.kernel_res,
907 "greater than ending position"));
914 memcpy (tmp_str, li->name, len);
915 tmp_str [len] = '\0';
916 offset = atoi (tmp_str);
922 else if (li->kind == CCL_TOK_TERM)
925 memcpy (tmp_str, li->name, len);
927 number = atoi (tmp_str);
931 if (offset > 0 && number > 0)
933 struct gw_user_set *us;
937 len = set_token->len;
938 memcpy (tmp_str, set_token->name, len);
940 us = user_set_search (tmp_str);
943 us = user_set_search (NULL);
944 if (us && us->hits != -1) /* proper result-set? */
946 if (offset <= us->hits)
948 if (offset+number-1 > us->hits)
949 number = us->hits - offset+1;
950 present (us->name, offset, number, format_token);
951 info.next_position = offset+number;
954 else if (!no_of_present) /* display error message once! */
956 fprintf (reply_fd, "%s\n",
957 gw_res_get (info.kernel_res, "gw.err.no.set",
958 "No result-set generated"));
963 if (!no_of_present) /* no records shown so far? */
965 struct gw_user_set *us;
968 us = user_set_search (NULL);
969 if (us && us->hits != -1) /* proper result-set? */
971 default_show = gw_res_int (info.kernel_res, "gw.default.show", 20);
972 if (us->hits >= info.next_position + default_show)
974 present (us->name, info.next_position, default_show,
976 info.next_position += default_show;
978 else if (us->hits >= info.next_position)
980 present (us->name, info.next_position,
981 us->hits - info.next_position + 1,
983 info.next_position = us->hits + 1;
986 else /* display error message */
988 fprintf (reply_fd, "%s\n",
989 gw_res_get (info.kernel_res, "gw.err.no.set",
990 "No result-set generated"));
997 static void show_def (void)
999 fprintf (reply_fd, "format %s\n", gw_res_get
1000 (info.kernel_res, "gw.display.format", ""));
1001 fprintf (reply_fd, "autoshow %s\n", gw_res_get
1002 (info.kernel_res, "gw.auto.show", ""));
1003 fprintf (reply_fd, "defaultshow %s\n", gw_res_get
1004 (info.kernel_res, "gw.default.show", ""));
1005 fprintf (reply_fd, "language %s\n", gw_res_get
1006 (info.kernel_res, "gw.language", ""));
1009 static int exec_def (struct ccl_token *list)
1011 const char *name = NULL;
1014 static struct command_word def_tab [] =
1016 { "reset", "reset" },
1019 { "autoshow", "autoshow" },
1020 { "defaultshow", "defaultshow" },
1021 { "lang", "language" },
1025 if (info.userid < 0)
1027 sprintf (fname, "user.%d.r", info.userid);
1029 if (list->kind == CCL_TOK_EOL)
1033 int setting_no = command_search (def_tab, list, "ccl.token.");
1037 fprintf (reply_fd, "Unknown setting in def\n");
1040 if (setting_no == 1)
1046 else if (setting_no == 2)
1052 if (list->kind == CCL_TOK_EOL)
1054 strncpy (value, list->name, 127);
1055 value[(size_t) list->len] = '\0';
1059 name = "gw.display.format";
1062 name = "gw.auto.show";
1065 name = "gw.default.show";
1068 name = "gw.language";
1073 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "update file %s with %s=%s",
1074 fname, name, value);
1075 gw_res_put (info.kernel_res, "gw.username", info.from_str, fname);
1076 gw_res_put (info.kernel_res, name, value, fname);
1077 gw_res_commit (info.kernel_res, fname);
1078 if (setting_no == 6)
1086 * exec_command: parse and execute ccl command in str.
1087 * str: ccl command string
1088 * stop_flag pointer to integer. On completion the integer
1089 * is 1 (stop) or 2 (continue); 0 (other command)
1090 * return: 0 success; non-zero otherwise
1092 static int exec_command (const char *str, int *stop_flag)
1094 struct ccl_token *cmd;
1095 const char *cp = str;
1098 while (*cp && *cp != ' ' && *cp != '\n' && *cp != '\t' && *cp != '\r')
1102 no = command_search_str (command_tab, str, cp-str, "ccl.command.");
1105 if (no == 1 || no == 2 || no == 3) /* find/show/base? */
1107 if (!info.connect_failed && !info.zass)
1108 /* open if not already opened */
1110 fprintf (reply_fd, "\n> %s\n", str);
1111 if (!info.zass) /* fail? */
1113 fprintf (reply_fd, "\n%s\n",
1114 gw_res_get (info.kernel_res, "gw.err.no.target",
1115 "No connection established - "
1116 "command ignored"));
1121 fprintf (reply_fd, "\n> %s\n", str);
1125 cmd = ccl_tokenize (cp);
1126 exec_find (cmd, str);
1129 cmd = ccl_tokenize (cp);
1133 cmd = ccl_token_simple (cp);
1137 cmd = ccl_token_simple (cp);
1140 case 6: /* continue */
1143 case 7: /* status */
1149 case 9: /* target */
1150 cmd = ccl_token_simple (cp);
1154 cmd = ccl_token_simple (cp);
1157 case 11: /* account */
1158 cmd = ccl_token_simple (cp);
1163 fprintf (reply_fd, "%s\n",
1164 gw_res_get (info.kernel_res, "gw.err.unimplemented",
1165 "Not implemented yet"));
1167 ccl_token_del (cmd);
1171 fprintf (reply_fd, "\n> %s\n", str);
1172 fprintf (reply_fd, " ^ %s\n",
1173 gw_res_get (info.kernel_res, "gw.err.unknown.command",
1175 "Use help to see list of commands"));
1180 int urp_start (int continuation, struct str_queue *queue)
1182 char subject_str[128];
1185 info.command_no = 0;
1186 info.reply_fname = NULL;
1188 if (email_header (queue, info.from_str, subject_str))
1190 gw_log (GW_LOG_WARN, KERNEL_LOG, "No message body");
1193 tty_init (stdout, 40, 70);
1196 info.reply_fname = (char *)
1197 tempnam (gw_res_get (info.kernel_res, "gw.reply.tmp.dir", NULL),
1198 gw_res_get (info.kernel_res,
1199 "gw.reply.tmp.prefix", "gwr"));
1201 reply_fd = fopen (info.reply_fname, "w");
1204 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, KERNEL_LOG, "Cannot create %s",
1208 tty_init (reply_fd, 0, 0);
1209 fprintf (reply_fd, "From: %s\n",
1210 gw_res_get (info.kernel_res, "gw.msg.from",
1211 "Email-Z39.50 gateway"));
1212 cp = gw_res_get (info.kernel_res, "gw.msg.replyto", NULL);
1214 fprintf (reply_fd, "Reply-To: %s\n", cp);
1215 fprintf (reply_fd, "Subject: ");
1217 fprintf (reply_fd, "Z39.50 Re: %s", subject_str);
1219 fprintf (reply_fd, "%s\n", gw_res_get (info.kernel_res,
1222 fprintf (reply_fd, "\n");
1223 gw_log (GW_LOG_ACCT, KERNEL_LOG, "User start %s", info.from_str);
1226 gw_log (GW_LOG_WARN, KERNEL_LOG, "No From in email header");
1227 fprintf (reply_fd, "%s\n", gw_res_get (info.kernel_res, "gw.msg.greeting",
1228 "Email->Z39.50 gateway"));
1230 fprintf (reply_fd, "%s\n", gw_res_get (info.kernel_res,
1236 int urp_command (struct str_queue *queue)
1241 while (str_queue_deq (queue, line_buf, STR_LINE_MAX))
1243 if (line_buf[0] == '\n')
1244 if (info.command_no)
1246 while (str_queue_deq (queue, 0, 0))
1252 if ((cp = strchr (line_buf, '\n')))
1254 gw_log (GW_LOG_ACCT, KERNEL_LOG, "cmd: %s", line_buf);
1255 if (isalpha (line_buf[0]))
1257 exec_command (line_buf, &stop_flag);
1260 info.command_no++; /* prevent help! */
1261 while (str_queue_deq (queue, line_buf, STR_LINE_MAX))
1273 if (!info.command_no)
1275 fprintf (reply_fd, "%s\n",
1276 gw_res_get (info.kernel_res, "gw.err.nullbody", "No body"));
1285 assert (info.reply_fname);
1289 mta = gw_res_get (info.kernel_res, "gw.reply.mta",
1290 "/usr/lib/sendmail");
1291 sprintf (cmd, "%s %s < %s", mta, info.from_str, info.reply_fname);
1292 mta_code = system (cmd);
1294 gw_log (GW_LOG_FATAL, KERNEL_LOG,
1295 "Reply '%s' got exit code %d", cmd, mta_code);
1297 unlink (info.reply_fname);
1298 gw_log (GW_LOG_ACCT, KERNEL_LOG, "User end %s", info.from_str);