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.48 1996/02/12 10:04:57 adam
49 * The gateway doesn't try to reconnect if it is already known that
50 * it will fail (connect_fail flag introduced).
52 * Revision 1.47 1996/01/18 13:54:48 adam
53 * Help <command> gives help information about individual commands.
54 * Status without arguments gives list of available predefined targets.
56 * Revision 1.46 1995/07/28 10:51:09 adam
57 * Bug fix: account from previous session had effect when new target command
60 * Revision 1.45 1995/07/20 08:20:24 adam
63 * Revision 1.44 1995/07/11 12:28:53 adam
64 * New functions ccl_token_simple and ccl_token_del used.
66 * Revision 1.43 1995/07/11 11:49:13 adam
67 * LINE_MAX renamed to STR_LINE_MAX.
69 * Revision 1.42 1995/07/03 12:59:29 adam
70 * New option for eti: -c dir to chdir before start.
71 * Setting change: gw.max.show defaults to 100.
73 * Revision 1.41 1995/07/03 08:20:18 adam
74 * More help information and better diagnostics.
76 * Revision 1.40 1995/05/23 08:13:00 adam
79 * Revision 1.39 1995/05/22 14:57:30 adam
80 * Next default record position also set by last p=... in show.
82 * Revision 1.38 1995/05/19 13:26:00 adam
83 * Bug fixes. Better command line options.
85 * Revision 1.37 1995/05/18 12:03:09 adam
86 * Bug fixes and minor improvements.
88 * Revision 1.36 1995/05/17 10:51:33 adam
89 * Added a few more error checks to the show command.
91 * Revision 1.35 1995/05/16 09:40:43 adam
92 * LICENSE. Setting of CCL token names (and/or/not/set) in read_kernel_res.
94 * Revision 1.34 1995/05/04 10:40:08 adam
95 * More work on Def-settings.
97 * Revision 1.33 1995/05/03 16:34:19 adam
98 * CCL def command, i.e. user definitions - saved as resource files.
100 * Revision 1.32 1995/05/03 07:37:46 adam
101 * CCL commands stop/continue implemented. New functions gw_res_{int,bool}
102 * are used when possible.
104 * Revision 1.31 1995/05/01 12:43:38 adam
105 * First work on resource monitor program.
107 * Revision 1.30 1995/04/20 16:10:47 adam
108 * Modified to work with non-blocking zass-api. Not using non-blocking
111 * Revision 1.29 1995/04/19 16:01:58 adam
112 * Some hacks to get the FIFO communication work!! Isn't reliable.
113 * Resource gw.account added - default account info.
115 * Revision 1.28 1995/04/19 13:19:09 adam
116 * New command: account - for authentication.
118 * Revision 1.27 1995/04/19 10:46:19 adam
119 * Persistency works much better now. New command: status - history-like
121 * Revision 1.26 1995/04/19 07:31:12 adam
122 * First work on Z39.50 persistence.
124 * Revision 1.25 1995/04/17 09:34:33 adam
125 * Timeout (idletime) adjustable. Minor changes in kernel.
127 * Revision 1.24 1995/03/28 11:42:35 adam
128 * First use of string-queue utility.
130 * Revision 1.23 1995/03/28 08:01:28 adam
131 * FIFO existence is used to test for a running kernel.
133 * Revision 1.22 1995/03/27 12:51:05 adam
134 * New log level in use: GW_LOG_ERRNO.
136 * Revision 1.21 1995/03/27 08:24:04 adam
137 * First use of gip interface and gw-db.
138 * First work on eti program.
140 * Revision 1.20 1995/03/03 17:19:17 adam
141 * Smarter presentation. Bug fix in email header interpretation.
143 * Revision 1.19 1995/03/02 09:32:11 adam
144 * New presentation formats. f0=full, f1=brief, f2=mid
146 * Revision 1.18 1995/03/01 14:32:26 adam
147 * Better diagnostics. Default is, that only one database selected when
150 * Revision 1.17 1995/02/28 13:16:26 adam
151 * Configurable From: added.
153 * Revision 1.16 1995/02/23 10:08:20 adam
154 * Added logging of all user commands.
156 * Revision 1.15 1995/02/23 08:32:17 adam
159 * Revision 1.13 1995/02/22 16:54:42 adam
160 * Qualifiers of LOC target updated. More logging messages.
162 * Revision 1.12 1995/02/22 15:51:51 adam
163 * Bug fix: swap of parameter number and offset in function present.
165 * Revision 1.11 1995/02/22 15:22:33 adam
166 * Much more checking of run-time state. Show command never retrieves
167 * more records than indicated by the previous search request. Help
168 * command available. The maximum number of records retrieved can be
171 * Revision 1.10 1995/02/22 08:51:35 adam
172 * Output function can be customized in fml, which is used to print
173 * the reply to reply_fd.
175 * Revision 1.9 1995/02/21 17:46:21 adam
178 * Revision 1.8 1995/02/21 12:12:00 adam
179 * Diagnostic record with error info. observed.
181 * Revision 1.7 1995/02/20 21:16:20 adam
182 * FML support. Bug fixes. Profile for drewdb.
184 * Revision 1.6 1995/02/17 14:41:14 quinn
185 * Added simple display of records.
187 * Revision 1.5 1995/02/17 14:22:13 adam
188 * First steps of CCL show command. Not finished yet.
190 * Revision 1.4 1995/02/17 09:08:36 adam
191 * Reply with subject. CCL base command implemented.
193 * Revision 1.3 1995/02/16 18:35:09 adam
194 * First use of Zdist library. Search requests are supported.
195 * Present requests are not supported yet.
197 * Revision 1.2 1995/02/16 13:21:00 adam
198 * Organization of resource files for targets and conversion
199 * language implemented.
201 * Revision 1.1 1995/02/15 17:45:30 adam
202 * First version of email gateway kernel. Email requests are read
203 * from stdin. The output is transferred to an MTA if 'From' is
204 * found in the header - or stdout if absent. No Z39.50 client is used.
210 info/status (other name?)
211 better persistence diagnostics
214 automatic information about target-aliases: name, query-support, etc.
227 #include <strqueue.h>
230 static void present (const char *set, int offset, int number,
231 struct ccl_token *format_token);
233 static char line_buf[STR_LINE_MAX+1];
235 static void put_esc_str (const char *s)
241 int reopen_target (void)
245 gw_log (GW_LOG_WARN, KERNEL_LOG, "Zass free...");
246 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "reopen_target");
247 info.zass = zass_open (info.hostname, info.port, NULL, /* complete */
248 *info.account ? info.account : NULL);
251 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Cannot connect to target %s:%d",
252 info.hostname, info.port);
253 fprintf (reply_fd, "%s %s:%d\n",
254 gw_res_get (info.kernel_res, "gw.err.connect",
255 "Cannot connect to target"),
256 info.hostname, info.port);
257 info.connect_failed = 1;
260 v = gw_res_get (info.kernel_res, "gw.description", NULL);
264 fprintf (reply_fd, "\n");
266 fprintf (reply_fd, "%s %s:%d\n",
267 gw_res_get (info.kernel_res, "gw.msg.connect",
268 "Connection established to"),
269 info.hostname, info.port);
271 fprintf (reply_fd, "%s: %s\n",
272 gw_res_get (info.kernel_res, "gw.msg.databases",
273 "Available databases"),
276 fprintf (reply_fd, "%s: %s\n",
277 gw_res_get (info.kernel_res, "gw.msg.database",
278 "Selected databases"),
281 fprintf (reply_fd, "set=%d\n", info.setno);
283 fprintf (reply_fd, "set=Default\n");
287 struct command_word {
289 char *resource_suffix;
292 static int command_search_str (struct command_word *tab, const char *cmd_name,
293 int cmd_len, const char *resource_prefix)
297 assert (resource_prefix);
299 while (tab->default_value)
301 char *cp, command_names[60];
302 char resource_name[60];
305 sprintf (resource_name, "%s%s", resource_prefix,
306 tab->resource_suffix);
307 v = gw_res_get (info.kernel_res, resource_name, tab->default_value);
309 strcpy (command_names, v);
315 if ((split = strchr (cp, ' ')))
317 if (cmd_len == strlen(cp) &&
318 !memcmp (cmd_name, cp, cmd_len))
330 static int command_search (struct command_word *tab, struct ccl_token *cmd,
331 const char *resource_prefix)
333 return command_search_str (tab, cmd->name, cmd->len, resource_prefix);
336 static struct error_no_struct {
339 } error_ccl_tab[] = {
341 { CCL_ERR_TERM_EXPECTED, "term.expected" },
342 { CCL_ERR_RP_EXPECTED, "rp.expected" },
343 { CCL_ERR_SETNAME_EXPECTED, "setname.expected" },
344 { CCL_ERR_OP_EXPECTED, "op.expected" },
345 { CCL_ERR_BAD_RP, "bad.rp" },
346 { CCL_ERR_UNKNOWN_QUAL, "unknown.qual" },
347 { CCL_ERR_DOUBLE_QUAL, "double.qual" },
348 { CCL_ERR_EQ_EXPECTED, "eq.expected" },
349 { CCL_ERR_BAD_RELATION, "bad.relation" },
350 { CCL_ERR_TRUNC_NOT_LEFT, "trunc.not.left" },
351 { CCL_ERR_TRUNC_NOT_BOTH, "trunc.not.both" },
352 { CCL_ERR_TRUNC_NOT_RIGHT, "trunc.not.right" },
356 static char *error_no_search (struct error_no_struct *tab, int no)
358 struct error_no_struct *p = tab;
359 while (p->resource_name)
362 return p->resource_name;
368 static int email_header (struct str_queue *sq,
369 char *from_str, char *subject_str)
373 while (str_queue_deq (sq, line_buf, STR_LINE_MAX))
375 if (line_buf[0] == '\n')
377 if (memcmp (line_buf, "From ", 5) == 0)
378 sscanf (line_buf+4, "%s", from_str);
379 if (memcmp (line_buf, "Subject: ", 9) == 0 &&
380 sscanf (line_buf+9, "%s", subject_str+1) == 1)
381 strcpy (subject_str, line_buf+9);
386 static void help_general (void)
388 put_esc_str (gw_res_get (info.kernel_res, "gw.help.general",
389 "Commands available in this service:\n"));
392 static struct command_word command_tab [] =
399 { "continue", "continue" },
400 { "status", "status" },
402 { "target", "target" },
404 { "account", "account" },
409 static int exec_help (struct ccl_token *list)
412 static char *sep = "-------------------------------\\n";
413 if (list->kind != CCL_TOK_EOL)
414 no = command_search_str (command_tab, list->name, list->len,
422 put_esc_str (gw_res_get (info.kernel_res, "gw.help.target",
423 "target <name> - selects a given target\n"));
429 put_esc_str (gw_res_get (info.kernel_res, "gw.help.base",
430 "base <base>.. - selects databases\n"));
436 put_esc_str (gw_res_get (info.kernel_res, "gw.help.find",
437 "find <query> - performs a search\n"));
443 put_esc_str (gw_res_get (info.kernel_res, "gw.help.show",
444 "show <spec> - retrieves and displays "
451 put_esc_str (gw_res_get (info.kernel_res, "gw.help.def",
452 "def <setting> <value> user definitons\n"));
458 put_esc_str (gw_res_get (info.kernel_res, "gw.help.account",
459 "account <str> sets authentication\n"));
465 put_esc_str (gw_res_get (info.kernel_res, "gw.help.status",
466 "status status information\n"));
472 static void display_diag_error (int code, const char *addinfo)
476 sprintf (str, "gw.bib1.diag.%d", code);
477 fprintf (reply_fd, "%s %d:\n %s: '%s'\n",
478 gw_res_get (info.kernel_res, "gw.msg.z39errcode",
481 gw_res_get (info.kernel_res, str, ""), addinfo);
484 static int exec_find (struct ccl_token *list, const char *search_str)
486 const struct zass_searchent *p;
487 struct gw_user_set *us;
491 struct ccl_rpn_node *rpn;
495 if (info.setno == -1)
496 strcpy (setname, "Default");
498 sprintf (setname, "%d", info.setno);
499 info.next_position = 1;
502 rpn = ccl_find (info.bibset, list, &error, &pos);
505 const char *v = NULL, *n;
508 fprintf (reply_fd, " %*s^ - ", pos - line_buf, " ");
510 n = error_no_search (error_ccl_tab, error);
513 sprintf (name, "gw.err.%s", n);
514 v = gw_res_get (info.kernel_res, name, NULL);
517 v = ccl_err_msg (error);
518 fprintf (reply_fd, "%s\n", v);
521 ccl_pr_tree (rpn, reply_fd);
522 fprintf (reply_fd, "\n");
524 if (!*info.database )
526 fprintf (reply_fd, "%s\n",
527 gw_res_get (info.kernel_res, "gw.err.no.database",
528 "You must select database"));
531 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Searching in database %s",
534 p = zass_p_search (info.zass, rpn, setname, info.database, info.sets);
537 fprintf (reply_fd, "%s\n",
538 gw_res_get (info.kernel_res, "gw.err.search.fail",
542 if (p->errcode != -1)
544 display_diag_error (p->errcode, p->errstring);
547 fprintf (reply_fd, "%d %s %s\n", p->num,
548 gw_res_get (info.kernel_res, "gw.msg.hits", "hit(s) in set "),
550 us = user_set_add (setname, p->num, info.database, rpn, 1, search_str);
551 autoshow = gw_res_int (info.kernel_res, "gw.auto.show", 0);
552 if (autoshow && p->num > 0)
554 if (autoshow > p->num)
556 present (setname, 1, p->num, NULL);
557 info.next_position = 1+p->num;
561 present (setname, 1, autoshow, NULL);
562 info.next_position = 1+autoshow;
568 static int exec_account (struct ccl_token *list)
570 if (list->kind != CCL_TOK_EOL)
573 memcpy (info.account, list->name, len);
574 info.target[len] = '\0';
577 *info.account = '\0';
578 info.account_in_session = 1;
582 void handle_target_list (const char *name, const char *value)
587 if (strlen(name) < 10)
589 if (memcmp (name, "gw.target.", 10))
591 fprintf (reply_fd, "%s\n", name+10);
592 res = gw_res_init ();
593 gw_res_merge (res, value);
594 p = gw_res_get (res, "gw.description", NULL);
600 p = gw_res_get (res, "gw.databases", NULL);
602 fprintf (reply_fd, " Databases: %s\n", p);
603 fprintf (reply_fd, "\n");
607 static int exec_target_list (void)
609 gw_res_trav (info.kernel_res, NULL, handle_target_list);
610 fprintf (reply_fd, "\n");
614 static int exec_target (struct ccl_token *list)
617 if (list->kind == CCL_TOK_EOL)
618 return exec_target_list ();
620 memcpy (info.target, list->name, len);
621 info.target [len] = '\0';
623 if (!info.account_in_session)
624 *info.account = '\0';
625 info.account_in_session = 0;
627 info.connect_failed = 0;
628 return reopen_target ();
631 static void exec_status_r (struct gw_user_set *sp)
635 exec_status_r (sp->prev);
636 fprintf (reply_fd, "%6s %7d %12.12s %.50s\n", sp->name, sp->hits,
637 sp->database, sp->search_str);
640 static int exec_status (struct ccl_token *list)
642 fprintf (reply_fd, "%s\n",
643 gw_res_get (info.kernel_res,
645 " Name Hits Database Find"));
646 exec_status_r (info.sets);
650 static int exec_base (struct ccl_token *list)
652 struct ccl_token *li = list;
656 if (list->kind == CCL_TOK_EOL)
658 free (info.database);
659 while (li->kind != CCL_TOK_EOL)
663 if (li->kind == CCL_TOK_COMMA)
666 info.database = malloc (len);
667 assert (info.database );
670 while (li->kind != CCL_TOK_EOL)
672 memcpy (info.database+len, li->name, li->len);
674 info.database[len++] = ',';
676 if (li->kind == CCL_TOK_COMMA)
679 info.database[len-1] = '\0';
683 static void present (const char *set, int offset, int number,
684 struct ccl_token *format_token)
686 const struct zass_presentent *zp;
691 max_number = gw_res_int (info.kernel_res, "gw.max.show", 100);
692 if (number > max_number)
694 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "present in set %s", set);
695 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "present of %d records from offset %d",
697 zp = zass_p_present(info.zass, (char *) set, offset, number);
702 int record_log_fd = -1;
703 const char *record_log_name;
705 record_log_name = gw_res_get (info.kernel_res, "gw.marc.log",
709 record_log_fd = open (record_log_name,
710 O_WRONLY|O_CREAT|O_APPEND, 0666);
711 if (record_log_fd == -1)
712 gw_log (GW_LOG_WARN|GW_LOG_ERRNO, "Cannot open %s",
715 fprintf (reply_fd, gw_res_get (info.kernel_res,
717 "Got %d records from set %s"),
719 fprintf (reply_fd, "\n");
720 for (i = 0, pp = zp->records; pp; pp = pp->next, i++)
724 const char *arg_ar[5];
726 fprintf (reply_fd, "--- %d/%d ---\n",
727 i+offset, offset+zp->num-1);
728 if (!gw_res_get (info.kernel_res, "gw.ignore.which", NULL))
730 if (pp->which == ZASS_REC_DIAG)
732 display_diag_error (pp->errcode, pp->errstring);
735 else if (pp->which != ZASS_REC_USMARC)
737 fprintf (reply_fd, "Unknown record kind %d\n",
742 if (record_log_fd != -1)
743 write (record_log_fd, pp->record, strlen(pp->record));
744 rec = iso2709_cvt (pp->record);
749 gw_res_get (info.kernel_res,
750 "gw.display.format", ""));
753 len = format_token->len;
754 if (len >= sizeof(format_str))
755 len = sizeof(format_str)-1;
756 memcpy (format_str, format_token->name, len);
757 format_str[len] = '\0';
759 if (info.fml && *format_str &&
760 (!strcmp (format_str, "0") || !strcmp (format_str, "1")
761 || !strcmp(format_str, "2")))
764 arg_ar[1] = format_str;
765 arg_ar[2] = " \\list";
766 arg_ar[3] = marc_to_str (info.fml, rec);
768 fml_exec_call_argv (info.fml, arg_ar);
771 iso2709_display (rec, reply_fd);
773 iso2709_display (rec, reply_fd);
778 fprintf (reply_fd, "Not a MARC record\n");
780 if (record_log_fd != -1)
781 close (record_log_fd);
785 static int exec_show (struct ccl_token *list)
787 static struct command_word show_tab [] =
794 struct ccl_token *set_token = NULL;
795 struct ccl_token *format_token = NULL;
796 struct ccl_token *li = list;
797 int no_of_present = 0;
800 while (li->kind != CCL_TOK_EOL)
803 if (li->next->kind == CCL_TOK_EQ)
805 if (li->kind == CCL_TOK_SET) /* set = <name> ? */
812 modifier_no = command_search (show_tab, li, "ccl.token.");
815 fprintf (reply_fd, "Unknown modifier in show\n");
819 if (modifier_no == 1) /* f = <name> ? */
821 else if (modifier_no == 2) /* p = <name> ? */
823 if (li->kind != CCL_TOK_EOL /* p = <name> - <name> ? */
824 && li->next->kind == CCL_TOK_MINUS
825 && li->next->next != CCL_TOK_EOL)
831 fprintf (reply_fd, "%s\n", "Missing token after '='");
839 memcpy (tmp_str, li->name, len);
841 if (atoi(tmp_str) <= 0)
843 fprintf (reply_fd, "%s\n",
844 gw_res_get (info.kernel_res, "gw.err.bad.show",
852 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Got set=%.*s", set_token->len,
855 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Got format=%.*s", format_token->len,
859 while (li->kind != CCL_TOK_EOL)
865 if (li->next->kind == CCL_TOK_EQ && li->kind != CCL_TOK_SET)
867 modifier_no = command_search (show_tab, li, "ccl.token.");
869 if (modifier_no == 2) /* p = <name> ? */
871 if (li->kind != CCL_TOK_EOL /* p = <name> - <name> ? */
872 && li->next->kind == CCL_TOK_MINUS
873 && li->next->next != CCL_TOK_EOL)
876 memcpy (tmp_str, li->name, len);
877 tmp_str [len] = '\0';
878 offset = atoi (tmp_str);
882 memcpy (tmp_str, li->name, len);
883 tmp_str [len] = '\0';
884 number = atoi (tmp_str) - offset + 1;
887 fprintf (reply_fd, "%s\n",
888 gw_res_get (info.kernel_res,
891 "greater than ending position"));
898 memcpy (tmp_str, li->name, len);
899 tmp_str [len] = '\0';
900 offset = atoi (tmp_str);
906 else if (li->kind == CCL_TOK_TERM)
909 memcpy (tmp_str, li->name, len);
911 number = atoi (tmp_str);
915 if (offset > 0 && number > 0)
917 struct gw_user_set *us;
921 len = set_token->len;
922 memcpy (tmp_str, set_token->name, len);
924 us = user_set_search (tmp_str);
927 us = user_set_search (NULL);
928 if (us && us->hits != -1) /* proper result-set? */
930 if (offset <= us->hits)
932 if (offset+number-1 > us->hits)
933 number = us->hits - offset+1;
934 present (us->name, offset, number, format_token);
935 info.next_position = offset+number;
938 else if (!no_of_present) /* display error message once! */
940 fprintf (reply_fd, "%s\n",
941 gw_res_get (info.kernel_res, "gw.err.no.set",
942 "No result-set generated"));
947 if (!no_of_present) /* no records shown so far? */
949 struct gw_user_set *us;
952 us = user_set_search (NULL);
953 if (us && us->hits != -1) /* proper result-set? */
955 default_show = gw_res_int (info.kernel_res, "gw.default.show", 20);
956 if (us->hits >= info.next_position + default_show)
958 present (us->name, info.next_position, default_show,
960 info.next_position += default_show;
962 else if (us->hits >= info.next_position)
964 present (us->name, info.next_position,
965 us->hits - info.next_position + 1,
967 info.next_position = us->hits + 1;
970 else /* display error message */
972 fprintf (reply_fd, "%s\n",
973 gw_res_get (info.kernel_res, "gw.err.no.set",
974 "No result-set generated"));
981 static void show_def (void)
983 fprintf (reply_fd, "format %s\n", gw_res_get
984 (info.kernel_res, "gw.display.format", ""));
985 fprintf (reply_fd, "autoshow %s\n", gw_res_get
986 (info.kernel_res, "gw.auto.show", ""));
987 fprintf (reply_fd, "defaultshow %s\n", gw_res_get
988 (info.kernel_res, "gw.default.show", ""));
989 fprintf (reply_fd, "language %s\n", gw_res_get
990 (info.kernel_res, "gw.language", ""));
993 static int exec_def (struct ccl_token *list)
995 const char *name = NULL;
998 static struct command_word def_tab [] =
1000 { "reset", "reset" },
1003 { "autoshow", "autoshow" },
1004 { "defaultshow", "defaultshow" },
1005 { "lang", "language" },
1009 if (info.userid < 0)
1011 sprintf (fname, "user.%d.r", info.userid);
1013 if (list->kind == CCL_TOK_EOL)
1017 int setting_no = command_search (def_tab, list, "ccl.token.");
1021 fprintf (reply_fd, "Unknown setting in def\n");
1024 if (setting_no == 1)
1030 else if (setting_no == 2)
1036 if (list->kind == CCL_TOK_EOL)
1038 strncpy (value, list->name, 127);
1039 value[(size_t) list->len] = '\0';
1043 name = "gw.display.format";
1046 name = "gw.auto.show";
1049 name = "gw.default.show";
1052 name = "gw.language";
1057 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "update file %s with %s=%s",
1058 fname, name, value);
1059 gw_res_put (info.kernel_res, "gw.username", info.from_str, fname);
1060 gw_res_put (info.kernel_res, name, value, fname);
1061 gw_res_commit (info.kernel_res, fname);
1062 if (setting_no == 6)
1070 * exec_command: parse and execute ccl command in str.
1071 * str: ccl command string
1072 * stop_flag pointer to integer. On completion the integer
1073 * is 1 (stop) or 2 (continue); 0 (other command)
1074 * return: 0 success; non-zero otherwise
1076 static int exec_command (const char *str, int *stop_flag)
1078 struct ccl_token *cmd;
1079 const char *cp = str;
1082 while (*cp && *cp != ' ' && *cp != '\n' && *cp != '\t' && *cp != '\r')
1086 no = command_search_str (command_tab, str, cp-str, "ccl.command.");
1089 if (no == 1 || no == 2 || no == 3) /* find/show/base? */
1091 if (!info.connect_failed && !info.zass)
1092 /* open if not already opened */
1094 fprintf (reply_fd, "\n> %s\n", str);
1095 if (!info.zass) /* fail? */
1097 fprintf (reply_fd, "\n%s\n",
1098 gw_res_get (info.kernel_res, "gw.err.no.target",
1099 "No connection established - "
1100 "command ignored"));
1105 fprintf (reply_fd, "\n> %s\n", str);
1109 cmd = ccl_tokenize (cp);
1110 exec_find (cmd, str);
1113 cmd = ccl_tokenize (cp);
1117 cmd = ccl_token_simple (cp);
1121 cmd = ccl_token_simple (cp);
1124 case 6: /* continue */
1127 case 7: /* status */
1133 case 9: /* target */
1134 cmd = ccl_token_simple (cp);
1138 cmd = ccl_token_simple (cp);
1141 case 11: /* account */
1142 cmd = ccl_token_simple (cp);
1147 fprintf (reply_fd, "%s\n",
1148 gw_res_get (info.kernel_res, "gw.err.unimplemented",
1149 "Not implemented yet"));
1151 ccl_token_del (cmd);
1155 fprintf (reply_fd, "\n> %s\n", str);
1156 fprintf (reply_fd, " ^ %s\n",
1157 gw_res_get (info.kernel_res, "gw.err.unknown.command",
1159 "Use help to see list of commands"));
1164 int urp_start (int continuation, struct str_queue *queue)
1166 char subject_str[128];
1168 info.command_no = 0;
1169 info.reply_fname = NULL;
1171 if (email_header (queue, info.from_str, subject_str))
1173 gw_log (GW_LOG_WARN, KERNEL_LOG, "No message body");
1176 tty_init (stdout, 40, 70);
1179 info.reply_fname = (char *)
1180 tempnam (gw_res_get (info.kernel_res, "gw.reply.tmp.dir", NULL),
1181 gw_res_get (info.kernel_res,
1182 "gw.reply.tmp.prefix", "gwr"));
1184 reply_fd = fopen (info.reply_fname, "w");
1187 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, KERNEL_LOG, "Cannot create %s",
1191 tty_init (reply_fd, 0, 0);
1192 fprintf (reply_fd, "From: %s\n",
1193 gw_res_get (info.kernel_res, "gw.msg.from",
1194 "Email-Z39.50 gateway"));
1195 fprintf (reply_fd, "Subject: ");
1197 fprintf (reply_fd, "Z39.50 Re: %s", subject_str);
1199 fprintf (reply_fd, "%s\n", gw_res_get (info.kernel_res,
1202 fprintf (reply_fd, "\n");
1203 gw_log (GW_LOG_ACCT, KERNEL_LOG, "User start %s", info.from_str);
1206 gw_log (GW_LOG_WARN, KERNEL_LOG, "No From in email header");
1207 fprintf (reply_fd, "%s\n", gw_res_get (info.kernel_res, "gw.msg.greeting",
1208 "Email->Z39.50 gateway"));
1210 fprintf (reply_fd, "%s\n", gw_res_get (info.kernel_res,
1216 int urp_command (struct str_queue *queue)
1221 while (str_queue_deq (queue, line_buf, STR_LINE_MAX))
1223 if (line_buf[0] == '\n')
1224 if (info.command_no)
1226 while (str_queue_deq (queue, 0, 0))
1232 if ((cp = strchr (line_buf, '\n')))
1234 gw_log (GW_LOG_ACCT, KERNEL_LOG, "cmd: %s", line_buf);
1235 if (isalpha (line_buf[0]))
1237 exec_command (line_buf, &stop_flag);
1240 info.command_no++; /* prevent help! */
1241 while (str_queue_deq (queue, line_buf, STR_LINE_MAX))
1253 if (!info.command_no)
1255 fprintf (reply_fd, "%s\n",
1256 gw_res_get (info.kernel_res, "gw.err.nullbody", "No body"));
1265 assert (info.reply_fname);
1269 mta = gw_res_get (info.kernel_res, "gw.reply.mta",
1270 "/usr/lib/sendmail");
1271 sprintf (cmd, "%s %s < %s", mta, info.from_str, info.reply_fname);
1272 mta_code = system (cmd);
1274 gw_log (GW_LOG_FATAL, KERNEL_LOG,
1275 "Reply '%s' got exit code %d", cmd, mta_code);
1277 unlink (info.reply_fname);
1278 gw_log (GW_LOG_ACCT, KERNEL_LOG, "User end %s", info.from_str);