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.49 1996/03/01 14:25:28 adam
49 * Email gateway obeys 'Reply-To:' in header.
51 * Revision 1.48 1996/02/12 10:04:57 adam
52 * The gateway doesn't try to reconnect if it is already known that
53 * it will fail (connect_fail flag introduced).
55 * Revision 1.47 1996/01/18 13:54:48 adam
56 * Help <command> gives help information about individual commands.
57 * Status without arguments gives list of available predefined targets.
59 * Revision 1.46 1995/07/28 10:51:09 adam
60 * Bug fix: account from previous session had effect when new target command
63 * Revision 1.45 1995/07/20 08:20:24 adam
66 * Revision 1.44 1995/07/11 12:28:53 adam
67 * New functions ccl_token_simple and ccl_token_del used.
69 * Revision 1.43 1995/07/11 11:49:13 adam
70 * LINE_MAX renamed to STR_LINE_MAX.
72 * Revision 1.42 1995/07/03 12:59:29 adam
73 * New option for eti: -c dir to chdir before start.
74 * Setting change: gw.max.show defaults to 100.
76 * Revision 1.41 1995/07/03 08:20:18 adam
77 * More help information and better diagnostics.
79 * Revision 1.40 1995/05/23 08:13:00 adam
82 * Revision 1.39 1995/05/22 14:57:30 adam
83 * Next default record position also set by last p=... in show.
85 * Revision 1.38 1995/05/19 13:26:00 adam
86 * Bug fixes. Better command line options.
88 * Revision 1.37 1995/05/18 12:03:09 adam
89 * Bug fixes and minor improvements.
91 * Revision 1.36 1995/05/17 10:51:33 adam
92 * Added a few more error checks to the show command.
94 * Revision 1.35 1995/05/16 09:40:43 adam
95 * LICENSE. Setting of CCL token names (and/or/not/set) in read_kernel_res.
97 * Revision 1.34 1995/05/04 10:40:08 adam
98 * More work on Def-settings.
100 * Revision 1.33 1995/05/03 16:34:19 adam
101 * CCL def command, i.e. user definitions - saved as resource files.
103 * Revision 1.32 1995/05/03 07:37:46 adam
104 * CCL commands stop/continue implemented. New functions gw_res_{int,bool}
105 * are used when possible.
107 * Revision 1.31 1995/05/01 12:43:38 adam
108 * First work on resource monitor program.
110 * Revision 1.30 1995/04/20 16:10:47 adam
111 * Modified to work with non-blocking zass-api. Not using non-blocking
114 * Revision 1.29 1995/04/19 16:01:58 adam
115 * Some hacks to get the FIFO communication work!! Isn't reliable.
116 * Resource gw.account added - default account info.
118 * Revision 1.28 1995/04/19 13:19:09 adam
119 * New command: account - for authentication.
121 * Revision 1.27 1995/04/19 10:46:19 adam
122 * Persistency works much better now. New command: status - history-like
124 * Revision 1.26 1995/04/19 07:31:12 adam
125 * First work on Z39.50 persistence.
127 * Revision 1.25 1995/04/17 09:34:33 adam
128 * Timeout (idletime) adjustable. Minor changes in kernel.
130 * Revision 1.24 1995/03/28 11:42:35 adam
131 * First use of string-queue utility.
133 * Revision 1.23 1995/03/28 08:01:28 adam
134 * FIFO existence is used to test for a running kernel.
136 * Revision 1.22 1995/03/27 12:51:05 adam
137 * New log level in use: GW_LOG_ERRNO.
139 * Revision 1.21 1995/03/27 08:24:04 adam
140 * First use of gip interface and gw-db.
141 * First work on eti program.
143 * Revision 1.20 1995/03/03 17:19:17 adam
144 * Smarter presentation. Bug fix in email header interpretation.
146 * Revision 1.19 1995/03/02 09:32:11 adam
147 * New presentation formats. f0=full, f1=brief, f2=mid
149 * Revision 1.18 1995/03/01 14:32:26 adam
150 * Better diagnostics. Default is, that only one database selected when
153 * Revision 1.17 1995/02/28 13:16:26 adam
154 * Configurable From: added.
156 * Revision 1.16 1995/02/23 10:08:20 adam
157 * Added logging of all user commands.
159 * Revision 1.15 1995/02/23 08:32:17 adam
162 * Revision 1.13 1995/02/22 16:54:42 adam
163 * Qualifiers of LOC target updated. More logging messages.
165 * Revision 1.12 1995/02/22 15:51:51 adam
166 * Bug fix: swap of parameter number and offset in function present.
168 * Revision 1.11 1995/02/22 15:22:33 adam
169 * Much more checking of run-time state. Show command never retrieves
170 * more records than indicated by the previous search request. Help
171 * command available. The maximum number of records retrieved can be
174 * Revision 1.10 1995/02/22 08:51:35 adam
175 * Output function can be customized in fml, which is used to print
176 * the reply to reply_fd.
178 * Revision 1.9 1995/02/21 17:46:21 adam
181 * Revision 1.8 1995/02/21 12:12:00 adam
182 * Diagnostic record with error info. observed.
184 * Revision 1.7 1995/02/20 21:16:20 adam
185 * FML support. Bug fixes. Profile for drewdb.
187 * Revision 1.6 1995/02/17 14:41:14 quinn
188 * Added simple display of records.
190 * Revision 1.5 1995/02/17 14:22:13 adam
191 * First steps of CCL show command. Not finished yet.
193 * Revision 1.4 1995/02/17 09:08:36 adam
194 * Reply with subject. CCL base command implemented.
196 * Revision 1.3 1995/02/16 18:35:09 adam
197 * First use of Zdist library. Search requests are supported.
198 * Present requests are not supported yet.
200 * Revision 1.2 1995/02/16 13:21:00 adam
201 * Organization of resource files for targets and conversion
202 * language implemented.
204 * Revision 1.1 1995/02/15 17:45:30 adam
205 * First version of email gateway kernel. Email requests are read
206 * from stdin. The output is transferred to an MTA if 'From' is
207 * found in the header - or stdout if absent. No Z39.50 client is used.
213 info/status (other name?)
214 better persistence diagnostics
217 automatic information about target-aliases: name, query-support, etc.
230 #include <strqueue.h>
233 static void present (const char *set, int offset, int number,
234 struct ccl_token *format_token);
236 static char line_buf[STR_LINE_MAX+1];
238 static void put_esc_str (const char *s)
244 int reopen_target (void)
248 gw_log (GW_LOG_WARN, KERNEL_LOG, "Zass free...");
249 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "reopen_target");
250 info.zass = zass_open (info.hostname, info.port, NULL, /* complete */
251 *info.account ? info.account : NULL);
254 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Cannot connect to target %s:%d",
255 info.hostname, info.port);
256 fprintf (reply_fd, "%s %s:%d\n",
257 gw_res_get (info.kernel_res, "gw.err.connect",
258 "Cannot connect to target"),
259 info.hostname, info.port);
260 info.connect_failed = 1;
263 v = gw_res_get (info.kernel_res, "gw.description", NULL);
267 fprintf (reply_fd, "\n");
269 fprintf (reply_fd, "%s %s:%d\n",
270 gw_res_get (info.kernel_res, "gw.msg.connect",
271 "Connection established to"),
272 info.hostname, info.port);
274 fprintf (reply_fd, "%s: %s\n",
275 gw_res_get (info.kernel_res, "gw.msg.databases",
276 "Available databases"),
279 fprintf (reply_fd, "%s: %s\n",
280 gw_res_get (info.kernel_res, "gw.msg.database",
281 "Selected databases"),
284 fprintf (reply_fd, "set=%d\n", info.setno);
286 fprintf (reply_fd, "set=Default\n");
290 struct command_word {
292 char *resource_suffix;
295 static int command_search_str (struct command_word *tab, const char *cmd_name,
296 int cmd_len, const char *resource_prefix)
300 assert (resource_prefix);
302 while (tab->default_value)
304 char *cp, command_names[60];
305 char resource_name[60];
308 sprintf (resource_name, "%s%s", resource_prefix,
309 tab->resource_suffix);
310 v = gw_res_get (info.kernel_res, resource_name, tab->default_value);
312 strcpy (command_names, v);
318 if ((split = strchr (cp, ' ')))
320 if (cmd_len == strlen(cp) &&
321 !memcmp (cmd_name, cp, cmd_len))
333 static int command_search (struct command_word *tab, struct ccl_token *cmd,
334 const char *resource_prefix)
336 return command_search_str (tab, cmd->name, cmd->len, resource_prefix);
339 static struct error_no_struct {
342 } error_ccl_tab[] = {
344 { CCL_ERR_TERM_EXPECTED, "term.expected" },
345 { CCL_ERR_RP_EXPECTED, "rp.expected" },
346 { CCL_ERR_SETNAME_EXPECTED, "setname.expected" },
347 { CCL_ERR_OP_EXPECTED, "op.expected" },
348 { CCL_ERR_BAD_RP, "bad.rp" },
349 { CCL_ERR_UNKNOWN_QUAL, "unknown.qual" },
350 { CCL_ERR_DOUBLE_QUAL, "double.qual" },
351 { CCL_ERR_EQ_EXPECTED, "eq.expected" },
352 { CCL_ERR_BAD_RELATION, "bad.relation" },
353 { CCL_ERR_TRUNC_NOT_LEFT, "trunc.not.left" },
354 { CCL_ERR_TRUNC_NOT_BOTH, "trunc.not.both" },
355 { CCL_ERR_TRUNC_NOT_RIGHT, "trunc.not.right" },
359 static char *error_no_search (struct error_no_struct *tab, int no)
361 struct error_no_struct *p = tab;
362 while (p->resource_name)
365 return p->resource_name;
371 static int email_header (struct str_queue *sq,
372 char *from_str, char *subject_str)
376 while (str_queue_deq (sq, line_buf, STR_LINE_MAX))
378 if (line_buf[0] == '\n')
380 if (memcmp (line_buf, "From ", 5) == 0)
381 sscanf (line_buf+4, "%s", from_str);
382 if (memcmp (line_buf, "Reply-To: ", 10) == 0)
383 sscanf (line_buf+10, "%s", from_str);
384 if (memcmp (line_buf, "Subject: ", 9) == 0 &&
385 sscanf (line_buf+9, "%s", subject_str+1) == 1)
386 strcpy (subject_str, line_buf+9);
391 static void help_general (void)
393 put_esc_str (gw_res_get (info.kernel_res, "gw.help.general",
394 "Commands available in this service:\n"));
397 static struct command_word command_tab [] =
404 { "continue", "continue" },
405 { "status", "status" },
407 { "target", "target" },
409 { "account", "account" },
414 static int exec_help (struct ccl_token *list)
417 static char *sep = "-------------------------------\\n";
418 if (list->kind != CCL_TOK_EOL)
419 no = command_search_str (command_tab, list->name, list->len,
427 put_esc_str (gw_res_get (info.kernel_res, "gw.help.target",
428 "target <name> - selects a given target\n"));
434 put_esc_str (gw_res_get (info.kernel_res, "gw.help.base",
435 "base <base>.. - selects databases\n"));
441 put_esc_str (gw_res_get (info.kernel_res, "gw.help.find",
442 "find <query> - performs a search\n"));
448 put_esc_str (gw_res_get (info.kernel_res, "gw.help.show",
449 "show <spec> - retrieves and displays "
456 put_esc_str (gw_res_get (info.kernel_res, "gw.help.def",
457 "def <setting> <value> user definitons\n"));
463 put_esc_str (gw_res_get (info.kernel_res, "gw.help.account",
464 "account <str> sets authentication\n"));
470 put_esc_str (gw_res_get (info.kernel_res, "gw.help.status",
471 "status status information\n"));
477 static void display_diag_error (int code, const char *addinfo)
481 sprintf (str, "gw.bib1.diag.%d", code);
482 fprintf (reply_fd, "%s %d:\n %s: '%s'\n",
483 gw_res_get (info.kernel_res, "gw.msg.z39errcode",
486 gw_res_get (info.kernel_res, str, ""), addinfo);
489 static int exec_find (struct ccl_token *list, const char *search_str)
491 const struct zass_searchent *p;
492 struct gw_user_set *us;
496 struct ccl_rpn_node *rpn;
500 if (info.setno == -1)
501 strcpy (setname, "Default");
503 sprintf (setname, "%d", info.setno);
504 info.next_position = 1;
507 rpn = ccl_find (info.bibset, list, &error, &pos);
510 const char *v = NULL, *n;
513 fprintf (reply_fd, " %*s^ - ", pos - line_buf, " ");
515 n = error_no_search (error_ccl_tab, error);
518 sprintf (name, "gw.err.%s", n);
519 v = gw_res_get (info.kernel_res, name, NULL);
522 v = ccl_err_msg (error);
523 fprintf (reply_fd, "%s\n", v);
526 ccl_pr_tree (rpn, reply_fd);
527 fprintf (reply_fd, "\n");
529 if (!*info.database )
531 fprintf (reply_fd, "%s\n",
532 gw_res_get (info.kernel_res, "gw.err.no.database",
533 "You must select database"));
536 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Searching in database %s",
539 p = zass_p_search (info.zass, rpn, setname, info.database, info.sets);
542 fprintf (reply_fd, "%s\n",
543 gw_res_get (info.kernel_res, "gw.err.search.fail",
547 if (p->errcode != -1)
549 display_diag_error (p->errcode, p->errstring);
552 fprintf (reply_fd, "%d %s %s\n", p->num,
553 gw_res_get (info.kernel_res, "gw.msg.hits", "hit(s) in set "),
555 us = user_set_add (setname, p->num, info.database, rpn, 1, search_str);
556 autoshow = gw_res_int (info.kernel_res, "gw.auto.show", 0);
557 if (autoshow && p->num > 0)
559 if (autoshow > p->num)
561 present (setname, 1, p->num, NULL);
562 info.next_position = 1+p->num;
566 present (setname, 1, autoshow, NULL);
567 info.next_position = 1+autoshow;
573 static int exec_account (struct ccl_token *list)
575 if (list->kind != CCL_TOK_EOL)
578 memcpy (info.account, list->name, len);
579 info.target[len] = '\0';
582 *info.account = '\0';
583 info.account_in_session = 1;
587 void handle_target_list (const char *name, const char *value)
592 if (strlen(name) < 10)
594 if (memcmp (name, "gw.target.", 10))
596 fprintf (reply_fd, "%s\n", name+10);
597 res = gw_res_init ();
598 gw_res_merge (res, value);
599 p = gw_res_get (res, "gw.description", NULL);
605 p = gw_res_get (res, "gw.databases", NULL);
607 fprintf (reply_fd, " Databases: %s\n", p);
608 fprintf (reply_fd, "\n");
612 static int exec_target_list (void)
614 gw_res_trav (info.kernel_res, NULL, handle_target_list);
615 fprintf (reply_fd, "\n");
619 static int exec_target (struct ccl_token *list)
622 if (list->kind == CCL_TOK_EOL)
623 return exec_target_list ();
625 memcpy (info.target, list->name, len);
626 info.target [len] = '\0';
628 if (!info.account_in_session)
629 *info.account = '\0';
630 info.account_in_session = 0;
632 info.connect_failed = 0;
633 return reopen_target ();
636 static void exec_status_r (struct gw_user_set *sp)
640 exec_status_r (sp->prev);
641 fprintf (reply_fd, "%6s %7d %12.12s %.50s\n", sp->name, sp->hits,
642 sp->database, sp->search_str);
645 static int exec_status (struct ccl_token *list)
647 fprintf (reply_fd, "%s\n",
648 gw_res_get (info.kernel_res,
650 " Name Hits Database Find"));
651 exec_status_r (info.sets);
655 static int exec_base (struct ccl_token *list)
657 struct ccl_token *li = list;
661 if (list->kind == CCL_TOK_EOL)
663 free (info.database);
664 while (li->kind != CCL_TOK_EOL)
668 if (li->kind == CCL_TOK_COMMA)
671 info.database = malloc (len);
672 assert (info.database );
675 while (li->kind != CCL_TOK_EOL)
677 memcpy (info.database+len, li->name, li->len);
679 info.database[len++] = ',';
681 if (li->kind == CCL_TOK_COMMA)
684 info.database[len-1] = '\0';
688 static void present (const char *set, int offset, int number,
689 struct ccl_token *format_token)
691 const struct zass_presentent *zp;
696 max_number = gw_res_int (info.kernel_res, "gw.max.show", 100);
697 if (number > max_number)
699 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "present in set %s", set);
700 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "present of %d records from offset %d",
702 zp = zass_p_present(info.zass, (char *) set, offset, number);
707 int record_log_fd = -1;
708 const char *record_log_name;
710 record_log_name = gw_res_get (info.kernel_res, "gw.marc.log",
714 record_log_fd = open (record_log_name,
715 O_WRONLY|O_CREAT|O_APPEND, 0666);
716 if (record_log_fd == -1)
717 gw_log (GW_LOG_WARN|GW_LOG_ERRNO, "Cannot open %s",
720 fprintf (reply_fd, gw_res_get (info.kernel_res,
722 "Got %d records from set %s"),
724 fprintf (reply_fd, "\n");
725 for (i = 0, pp = zp->records; pp; pp = pp->next, i++)
729 const char *arg_ar[5];
731 fprintf (reply_fd, "--- %d/%d ---\n",
732 i+offset, offset+zp->num-1);
733 if (!gw_res_get (info.kernel_res, "gw.ignore.which", NULL))
735 if (pp->which == ZASS_REC_DIAG)
737 display_diag_error (pp->errcode, pp->errstring);
740 else if (pp->which != ZASS_REC_USMARC)
742 fprintf (reply_fd, "Unknown record kind %d\n",
747 if (record_log_fd != -1)
748 write (record_log_fd, pp->record, strlen(pp->record));
749 rec = iso2709_cvt (pp->record);
754 gw_res_get (info.kernel_res,
755 "gw.display.format", ""));
758 len = format_token->len;
759 if (len >= sizeof(format_str))
760 len = sizeof(format_str)-1;
761 memcpy (format_str, format_token->name, len);
762 format_str[len] = '\0';
764 if (info.fml && *format_str &&
765 (!strcmp (format_str, "0") || !strcmp (format_str, "1")
766 || !strcmp(format_str, "2")))
769 arg_ar[1] = format_str;
770 arg_ar[2] = " \\list";
771 arg_ar[3] = marc_to_str (info.fml, rec);
773 fml_exec_call_argv (info.fml, arg_ar);
776 iso2709_display (rec, reply_fd);
778 iso2709_display (rec, reply_fd);
783 fprintf (reply_fd, "Not a MARC record\n");
785 if (record_log_fd != -1)
786 close (record_log_fd);
790 static int exec_show (struct ccl_token *list)
792 static struct command_word show_tab [] =
799 struct ccl_token *set_token = NULL;
800 struct ccl_token *format_token = NULL;
801 struct ccl_token *li = list;
802 int no_of_present = 0;
805 while (li->kind != CCL_TOK_EOL)
808 if (li->next->kind == CCL_TOK_EQ)
810 if (li->kind == CCL_TOK_SET) /* set = <name> ? */
817 modifier_no = command_search (show_tab, li, "ccl.token.");
820 fprintf (reply_fd, "Unknown modifier in show\n");
824 if (modifier_no == 1) /* f = <name> ? */
826 else if (modifier_no == 2) /* p = <name> ? */
828 if (li->kind != CCL_TOK_EOL /* p = <name> - <name> ? */
829 && li->next->kind == CCL_TOK_MINUS
830 && li->next->next != CCL_TOK_EOL)
836 fprintf (reply_fd, "%s\n", "Missing token after '='");
844 memcpy (tmp_str, li->name, len);
846 if (atoi(tmp_str) <= 0)
848 fprintf (reply_fd, "%s\n",
849 gw_res_get (info.kernel_res, "gw.err.bad.show",
857 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Got set=%.*s", set_token->len,
860 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Got format=%.*s", format_token->len,
864 while (li->kind != CCL_TOK_EOL)
870 if (li->next->kind == CCL_TOK_EQ && li->kind != CCL_TOK_SET)
872 modifier_no = command_search (show_tab, li, "ccl.token.");
874 if (modifier_no == 2) /* p = <name> ? */
876 if (li->kind != CCL_TOK_EOL /* p = <name> - <name> ? */
877 && li->next->kind == CCL_TOK_MINUS
878 && li->next->next != CCL_TOK_EOL)
881 memcpy (tmp_str, li->name, len);
882 tmp_str [len] = '\0';
883 offset = atoi (tmp_str);
887 memcpy (tmp_str, li->name, len);
888 tmp_str [len] = '\0';
889 number = atoi (tmp_str) - offset + 1;
892 fprintf (reply_fd, "%s\n",
893 gw_res_get (info.kernel_res,
896 "greater than ending position"));
903 memcpy (tmp_str, li->name, len);
904 tmp_str [len] = '\0';
905 offset = atoi (tmp_str);
911 else if (li->kind == CCL_TOK_TERM)
914 memcpy (tmp_str, li->name, len);
916 number = atoi (tmp_str);
920 if (offset > 0 && number > 0)
922 struct gw_user_set *us;
926 len = set_token->len;
927 memcpy (tmp_str, set_token->name, len);
929 us = user_set_search (tmp_str);
932 us = user_set_search (NULL);
933 if (us && us->hits != -1) /* proper result-set? */
935 if (offset <= us->hits)
937 if (offset+number-1 > us->hits)
938 number = us->hits - offset+1;
939 present (us->name, offset, number, format_token);
940 info.next_position = offset+number;
943 else if (!no_of_present) /* display error message once! */
945 fprintf (reply_fd, "%s\n",
946 gw_res_get (info.kernel_res, "gw.err.no.set",
947 "No result-set generated"));
952 if (!no_of_present) /* no records shown so far? */
954 struct gw_user_set *us;
957 us = user_set_search (NULL);
958 if (us && us->hits != -1) /* proper result-set? */
960 default_show = gw_res_int (info.kernel_res, "gw.default.show", 20);
961 if (us->hits >= info.next_position + default_show)
963 present (us->name, info.next_position, default_show,
965 info.next_position += default_show;
967 else if (us->hits >= info.next_position)
969 present (us->name, info.next_position,
970 us->hits - info.next_position + 1,
972 info.next_position = us->hits + 1;
975 else /* display error message */
977 fprintf (reply_fd, "%s\n",
978 gw_res_get (info.kernel_res, "gw.err.no.set",
979 "No result-set generated"));
986 static void show_def (void)
988 fprintf (reply_fd, "format %s\n", gw_res_get
989 (info.kernel_res, "gw.display.format", ""));
990 fprintf (reply_fd, "autoshow %s\n", gw_res_get
991 (info.kernel_res, "gw.auto.show", ""));
992 fprintf (reply_fd, "defaultshow %s\n", gw_res_get
993 (info.kernel_res, "gw.default.show", ""));
994 fprintf (reply_fd, "language %s\n", gw_res_get
995 (info.kernel_res, "gw.language", ""));
998 static int exec_def (struct ccl_token *list)
1000 const char *name = NULL;
1003 static struct command_word def_tab [] =
1005 { "reset", "reset" },
1008 { "autoshow", "autoshow" },
1009 { "defaultshow", "defaultshow" },
1010 { "lang", "language" },
1014 if (info.userid < 0)
1016 sprintf (fname, "user.%d.r", info.userid);
1018 if (list->kind == CCL_TOK_EOL)
1022 int setting_no = command_search (def_tab, list, "ccl.token.");
1026 fprintf (reply_fd, "Unknown setting in def\n");
1029 if (setting_no == 1)
1035 else if (setting_no == 2)
1041 if (list->kind == CCL_TOK_EOL)
1043 strncpy (value, list->name, 127);
1044 value[(size_t) list->len] = '\0';
1048 name = "gw.display.format";
1051 name = "gw.auto.show";
1054 name = "gw.default.show";
1057 name = "gw.language";
1062 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "update file %s with %s=%s",
1063 fname, name, value);
1064 gw_res_put (info.kernel_res, "gw.username", info.from_str, fname);
1065 gw_res_put (info.kernel_res, name, value, fname);
1066 gw_res_commit (info.kernel_res, fname);
1067 if (setting_no == 6)
1075 * exec_command: parse and execute ccl command in str.
1076 * str: ccl command string
1077 * stop_flag pointer to integer. On completion the integer
1078 * is 1 (stop) or 2 (continue); 0 (other command)
1079 * return: 0 success; non-zero otherwise
1081 static int exec_command (const char *str, int *stop_flag)
1083 struct ccl_token *cmd;
1084 const char *cp = str;
1087 while (*cp && *cp != ' ' && *cp != '\n' && *cp != '\t' && *cp != '\r')
1091 no = command_search_str (command_tab, str, cp-str, "ccl.command.");
1094 if (no == 1 || no == 2 || no == 3) /* find/show/base? */
1096 if (!info.connect_failed && !info.zass)
1097 /* open if not already opened */
1099 fprintf (reply_fd, "\n> %s\n", str);
1100 if (!info.zass) /* fail? */
1102 fprintf (reply_fd, "\n%s\n",
1103 gw_res_get (info.kernel_res, "gw.err.no.target",
1104 "No connection established - "
1105 "command ignored"));
1110 fprintf (reply_fd, "\n> %s\n", str);
1114 cmd = ccl_tokenize (cp);
1115 exec_find (cmd, str);
1118 cmd = ccl_tokenize (cp);
1122 cmd = ccl_token_simple (cp);
1126 cmd = ccl_token_simple (cp);
1129 case 6: /* continue */
1132 case 7: /* status */
1138 case 9: /* target */
1139 cmd = ccl_token_simple (cp);
1143 cmd = ccl_token_simple (cp);
1146 case 11: /* account */
1147 cmd = ccl_token_simple (cp);
1152 fprintf (reply_fd, "%s\n",
1153 gw_res_get (info.kernel_res, "gw.err.unimplemented",
1154 "Not implemented yet"));
1156 ccl_token_del (cmd);
1160 fprintf (reply_fd, "\n> %s\n", str);
1161 fprintf (reply_fd, " ^ %s\n",
1162 gw_res_get (info.kernel_res, "gw.err.unknown.command",
1164 "Use help to see list of commands"));
1169 int urp_start (int continuation, struct str_queue *queue)
1171 char subject_str[128];
1173 info.command_no = 0;
1174 info.reply_fname = NULL;
1176 if (email_header (queue, info.from_str, subject_str))
1178 gw_log (GW_LOG_WARN, KERNEL_LOG, "No message body");
1181 tty_init (stdout, 40, 70);
1184 info.reply_fname = (char *)
1185 tempnam (gw_res_get (info.kernel_res, "gw.reply.tmp.dir", NULL),
1186 gw_res_get (info.kernel_res,
1187 "gw.reply.tmp.prefix", "gwr"));
1189 reply_fd = fopen (info.reply_fname, "w");
1192 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, KERNEL_LOG, "Cannot create %s",
1196 tty_init (reply_fd, 0, 0);
1197 fprintf (reply_fd, "From: %s\n",
1198 gw_res_get (info.kernel_res, "gw.msg.from",
1199 "Email-Z39.50 gateway"));
1200 fprintf (reply_fd, "Subject: ");
1202 fprintf (reply_fd, "Z39.50 Re: %s", subject_str);
1204 fprintf (reply_fd, "%s\n", gw_res_get (info.kernel_res,
1207 fprintf (reply_fd, "\n");
1208 gw_log (GW_LOG_ACCT, KERNEL_LOG, "User start %s", info.from_str);
1211 gw_log (GW_LOG_WARN, KERNEL_LOG, "No From in email header");
1212 fprintf (reply_fd, "%s\n", gw_res_get (info.kernel_res, "gw.msg.greeting",
1213 "Email->Z39.50 gateway"));
1215 fprintf (reply_fd, "%s\n", gw_res_get (info.kernel_res,
1221 int urp_command (struct str_queue *queue)
1226 while (str_queue_deq (queue, line_buf, STR_LINE_MAX))
1228 if (line_buf[0] == '\n')
1229 if (info.command_no)
1231 while (str_queue_deq (queue, 0, 0))
1237 if ((cp = strchr (line_buf, '\n')))
1239 gw_log (GW_LOG_ACCT, KERNEL_LOG, "cmd: %s", line_buf);
1240 if (isalpha (line_buf[0]))
1242 exec_command (line_buf, &stop_flag);
1245 info.command_no++; /* prevent help! */
1246 while (str_queue_deq (queue, line_buf, STR_LINE_MAX))
1258 if (!info.command_no)
1260 fprintf (reply_fd, "%s\n",
1261 gw_res_get (info.kernel_res, "gw.err.nullbody", "No body"));
1270 assert (info.reply_fname);
1274 mta = gw_res_get (info.kernel_res, "gw.reply.mta",
1275 "/usr/lib/sendmail");
1276 sprintf (cmd, "%s %s < %s", mta, info.from_str, info.reply_fname);
1277 mta_code = system (cmd);
1279 gw_log (GW_LOG_FATAL, KERNEL_LOG,
1280 "Reply '%s' got exit code %d", cmd, mta_code);
1282 unlink (info.reply_fname);
1283 gw_log (GW_LOG_ACCT, KERNEL_LOG, "User end %s", info.from_str);