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.47 1996/01/18 13:54:48 adam
49 * Help <command> gives help information about individual commands.
50 * Status without arguments gives list of available predefined targets.
52 * Revision 1.46 1995/07/28 10:51:09 adam
53 * Bug fix: account from previous session had effect when new target command
56 * Revision 1.45 1995/07/20 08:20:24 adam
59 * Revision 1.44 1995/07/11 12:28:53 adam
60 * New functions ccl_token_simple and ccl_token_del used.
62 * Revision 1.43 1995/07/11 11:49:13 adam
63 * LINE_MAX renamed to STR_LINE_MAX.
65 * Revision 1.42 1995/07/03 12:59:29 adam
66 * New option for eti: -c dir to chdir before start.
67 * Setting change: gw.max.show defaults to 100.
69 * Revision 1.41 1995/07/03 08:20:18 adam
70 * More help information and better diagnostics.
72 * Revision 1.40 1995/05/23 08:13:00 adam
75 * Revision 1.39 1995/05/22 14:57:30 adam
76 * Next default record position also set by last p=... in show.
78 * Revision 1.38 1995/05/19 13:26:00 adam
79 * Bug fixes. Better command line options.
81 * Revision 1.37 1995/05/18 12:03:09 adam
82 * Bug fixes and minor improvements.
84 * Revision 1.36 1995/05/17 10:51:33 adam
85 * Added a few more error checks to the show command.
87 * Revision 1.35 1995/05/16 09:40:43 adam
88 * LICENSE. Setting of CCL token names (and/or/not/set) in read_kernel_res.
90 * Revision 1.34 1995/05/04 10:40:08 adam
91 * More work on Def-settings.
93 * Revision 1.33 1995/05/03 16:34:19 adam
94 * CCL def command, i.e. user definitions - saved as resource files.
96 * Revision 1.32 1995/05/03 07:37:46 adam
97 * CCL commands stop/continue implemented. New functions gw_res_{int,bool}
98 * are used when possible.
100 * Revision 1.31 1995/05/01 12:43:38 adam
101 * First work on resource monitor program.
103 * Revision 1.30 1995/04/20 16:10:47 adam
104 * Modified to work with non-blocking zass-api. Not using non-blocking
107 * Revision 1.29 1995/04/19 16:01:58 adam
108 * Some hacks to get the FIFO communication work!! Isn't reliable.
109 * Resource gw.account added - default account info.
111 * Revision 1.28 1995/04/19 13:19:09 adam
112 * New command: account - for authentication.
114 * Revision 1.27 1995/04/19 10:46:19 adam
115 * Persistency works much better now. New command: status - history-like
117 * Revision 1.26 1995/04/19 07:31:12 adam
118 * First work on Z39.50 persistence.
120 * Revision 1.25 1995/04/17 09:34:33 adam
121 * Timeout (idletime) adjustable. Minor changes in kernel.
123 * Revision 1.24 1995/03/28 11:42:35 adam
124 * First use of string-queue utility.
126 * Revision 1.23 1995/03/28 08:01:28 adam
127 * FIFO existence is used to test for a running kernel.
129 * Revision 1.22 1995/03/27 12:51:05 adam
130 * New log level in use: GW_LOG_ERRNO.
132 * Revision 1.21 1995/03/27 08:24:04 adam
133 * First use of gip interface and gw-db.
134 * First work on eti program.
136 * Revision 1.20 1995/03/03 17:19:17 adam
137 * Smarter presentation. Bug fix in email header interpretation.
139 * Revision 1.19 1995/03/02 09:32:11 adam
140 * New presentation formats. f0=full, f1=brief, f2=mid
142 * Revision 1.18 1995/03/01 14:32:26 adam
143 * Better diagnostics. Default is, that only one database selected when
146 * Revision 1.17 1995/02/28 13:16:26 adam
147 * Configurable From: added.
149 * Revision 1.16 1995/02/23 10:08:20 adam
150 * Added logging of all user commands.
152 * Revision 1.15 1995/02/23 08:32:17 adam
155 * Revision 1.13 1995/02/22 16:54:42 adam
156 * Qualifiers of LOC target updated. More logging messages.
158 * Revision 1.12 1995/02/22 15:51:51 adam
159 * Bug fix: swap of parameter number and offset in function present.
161 * Revision 1.11 1995/02/22 15:22:33 adam
162 * Much more checking of run-time state. Show command never retrieves
163 * more records than indicated by the previous search request. Help
164 * command available. The maximum number of records retrieved can be
167 * Revision 1.10 1995/02/22 08:51:35 adam
168 * Output function can be customized in fml, which is used to print
169 * the reply to reply_fd.
171 * Revision 1.9 1995/02/21 17:46:21 adam
174 * Revision 1.8 1995/02/21 12:12:00 adam
175 * Diagnostic record with error info. observed.
177 * Revision 1.7 1995/02/20 21:16:20 adam
178 * FML support. Bug fixes. Profile for drewdb.
180 * Revision 1.6 1995/02/17 14:41:14 quinn
181 * Added simple display of records.
183 * Revision 1.5 1995/02/17 14:22:13 adam
184 * First steps of CCL show command. Not finished yet.
186 * Revision 1.4 1995/02/17 09:08:36 adam
187 * Reply with subject. CCL base command implemented.
189 * Revision 1.3 1995/02/16 18:35:09 adam
190 * First use of Zdist library. Search requests are supported.
191 * Present requests are not supported yet.
193 * Revision 1.2 1995/02/16 13:21:00 adam
194 * Organization of resource files for targets and conversion
195 * language implemented.
197 * Revision 1.1 1995/02/15 17:45:30 adam
198 * First version of email gateway kernel. Email requests are read
199 * from stdin. The output is transferred to an MTA if 'From' is
200 * found in the header - or stdout if absent. No Z39.50 client is used.
206 info/status (other name?)
207 better persistence diagnostics
210 automatic information about target-aliases: name, query-support, etc.
223 #include <strqueue.h>
226 static void present (const char *set, int offset, int number,
227 struct ccl_token *format_token);
229 static char line_buf[STR_LINE_MAX+1];
231 static void put_esc_str (const char *s)
237 int reopen_target (void)
241 gw_log (GW_LOG_WARN, KERNEL_LOG, "Zass free...");
242 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "reopen_target");
243 info.zass = zass_open (info.hostname, info.port, NULL, /* complete */
244 *info.account ? info.account : NULL);
247 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Cannot connect to target %s:%d",
248 info.hostname, info.port);
249 fprintf (reply_fd, "%s %s:%d\n",
250 gw_res_get (info.kernel_res, "gw.err.connect",
251 "Cannot connect to target"),
252 info.hostname, info.port);
255 v = gw_res_get (info.kernel_res, "gw.description", NULL);
259 fprintf (reply_fd, "\n");
261 fprintf (reply_fd, "%s %s:%d\n",
262 gw_res_get (info.kernel_res, "gw.msg.connect",
263 "Connection established to"),
264 info.hostname, info.port);
266 fprintf (reply_fd, "%s: %s\n",
267 gw_res_get (info.kernel_res, "gw.msg.databases",
268 "Available databases"),
271 fprintf (reply_fd, "%s: %s\n",
272 gw_res_get (info.kernel_res, "gw.msg.database",
273 "Selected databases"),
276 fprintf (reply_fd, "set=%d\n", info.setno);
278 fprintf (reply_fd, "set=Default\n");
282 struct command_word {
284 char *resource_suffix;
287 static int command_search_str (struct command_word *tab, const char *cmd_name,
288 int cmd_len, const char *resource_prefix)
292 assert (resource_prefix);
294 while (tab->default_value)
296 char *cp, command_names[60];
297 char resource_name[60];
300 sprintf (resource_name, "%s%s", resource_prefix,
301 tab->resource_suffix);
302 v = gw_res_get (info.kernel_res, resource_name, tab->default_value);
304 strcpy (command_names, v);
310 if ((split = strchr (cp, ' ')))
312 if (cmd_len == strlen(cp) &&
313 !memcmp (cmd_name, cp, cmd_len))
325 static int command_search (struct command_word *tab, struct ccl_token *cmd,
326 const char *resource_prefix)
328 return command_search_str (tab, cmd->name, cmd->len, resource_prefix);
331 static struct error_no_struct {
334 } error_ccl_tab[] = {
336 { CCL_ERR_TERM_EXPECTED, "term.expected" },
337 { CCL_ERR_RP_EXPECTED, "rp.expected" },
338 { CCL_ERR_SETNAME_EXPECTED, "setname.expected" },
339 { CCL_ERR_OP_EXPECTED, "op.expected" },
340 { CCL_ERR_BAD_RP, "bad.rp" },
341 { CCL_ERR_UNKNOWN_QUAL, "unknown.qual" },
342 { CCL_ERR_DOUBLE_QUAL, "double.qual" },
343 { CCL_ERR_EQ_EXPECTED, "eq.expected" },
344 { CCL_ERR_BAD_RELATION, "bad.relation" },
345 { CCL_ERR_TRUNC_NOT_LEFT, "trunc.not.left" },
346 { CCL_ERR_TRUNC_NOT_BOTH, "trunc.not.both" },
347 { CCL_ERR_TRUNC_NOT_RIGHT, "trunc.not.right" },
351 static char *error_no_search (struct error_no_struct *tab, int no)
353 struct error_no_struct *p = tab;
354 while (p->resource_name)
357 return p->resource_name;
363 static int email_header (struct str_queue *sq,
364 char *from_str, char *subject_str)
368 while (str_queue_deq (sq, line_buf, STR_LINE_MAX))
370 if (line_buf[0] == '\n')
372 if (memcmp (line_buf, "From ", 5) == 0)
373 sscanf (line_buf+4, "%s", from_str);
374 if (memcmp (line_buf, "Subject: ", 9) == 0 &&
375 sscanf (line_buf+9, "%s", subject_str+1) == 1)
376 strcpy (subject_str, line_buf+9);
381 static void help_general (void)
383 put_esc_str (gw_res_get (info.kernel_res, "gw.help.general",
384 "Commands available in this service:\n"));
387 static struct command_word command_tab [] =
394 { "continue", "continue" },
395 { "status", "status" },
397 { "target", "target" },
399 { "account", "account" },
404 static int exec_help (struct ccl_token *list)
407 static char *sep = "-------------------------------\\n";
408 if (list->kind != CCL_TOK_EOL)
409 no = command_search_str (command_tab, list->name, list->len,
417 put_esc_str (gw_res_get (info.kernel_res, "gw.help.target",
418 "target <name> - selects a given target\n"));
424 put_esc_str (gw_res_get (info.kernel_res, "gw.help.base",
425 "base <base>.. - selects databases\n"));
431 put_esc_str (gw_res_get (info.kernel_res, "gw.help.find",
432 "find <query> - performs a search\n"));
438 put_esc_str (gw_res_get (info.kernel_res, "gw.help.show",
439 "show <spec> - retrieves and displays "
446 put_esc_str (gw_res_get (info.kernel_res, "gw.help.def",
447 "def <setting> <value> user definitons\n"));
453 put_esc_str (gw_res_get (info.kernel_res, "gw.help.account",
454 "account <str> sets authentication\n"));
460 put_esc_str (gw_res_get (info.kernel_res, "gw.help.status",
461 "status status information\n"));
467 static void display_diag_error (int code, const char *addinfo)
471 sprintf (str, "gw.bib1.diag.%d", code);
472 fprintf (reply_fd, "%s %d:\n %s: '%s'\n",
473 gw_res_get (info.kernel_res, "gw.msg.z39errcode",
476 gw_res_get (info.kernel_res, str, ""), addinfo);
479 static int exec_find (struct ccl_token *list, const char *search_str)
481 const struct zass_searchent *p;
482 struct gw_user_set *us;
486 struct ccl_rpn_node *rpn;
490 if (info.setno == -1)
491 strcpy (setname, "Default");
493 sprintf (setname, "%d", info.setno);
494 info.next_position = 1;
497 rpn = ccl_find (info.bibset, list, &error, &pos);
500 const char *v = NULL, *n;
503 fprintf (reply_fd, " %*s^ - ", pos - line_buf, " ");
505 n = error_no_search (error_ccl_tab, error);
508 sprintf (name, "gw.err.%s", n);
509 v = gw_res_get (info.kernel_res, name, NULL);
512 v = ccl_err_msg (error);
513 fprintf (reply_fd, "%s\n", v);
516 ccl_pr_tree (rpn, reply_fd);
517 fprintf (reply_fd, "\n");
519 if (!*info.database )
521 fprintf (reply_fd, "%s\n",
522 gw_res_get (info.kernel_res, "gw.err.no.database",
523 "You must select database"));
526 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Searching in database %s",
529 p = zass_p_search (info.zass, rpn, setname, info.database, info.sets);
532 fprintf (reply_fd, "%s\n",
533 gw_res_get (info.kernel_res, "gw.err.search.fail",
537 if (p->errcode != -1)
539 display_diag_error (p->errcode, p->errstring);
542 fprintf (reply_fd, "%d %s %s\n", p->num,
543 gw_res_get (info.kernel_res, "gw.msg.hits", "hit(s) in set "),
545 us = user_set_add (setname, p->num, info.database, rpn, 1, search_str);
546 autoshow = gw_res_int (info.kernel_res, "gw.auto.show", 0);
547 if (autoshow && p->num > 0)
549 if (autoshow > p->num)
551 present (setname, 1, p->num, NULL);
552 info.next_position = 1+p->num;
556 present (setname, 1, autoshow, NULL);
557 info.next_position = 1+autoshow;
563 static int exec_account (struct ccl_token *list)
565 if (list->kind != CCL_TOK_EOL)
568 memcpy (info.account, list->name, len);
569 info.target[len] = '\0';
572 *info.account = '\0';
573 info.account_in_session = 1;
577 void handle_target_list (const char *name, const char *value)
582 if (strlen(name) < 10)
584 if (memcmp (name, "gw.target.", 10))
586 fprintf (reply_fd, "%s\n", name+10);
587 res = gw_res_init ();
588 gw_res_merge (res, value);
589 p = gw_res_get (res, "gw.description", NULL);
595 p = gw_res_get (res, "gw.databases", NULL);
597 fprintf (reply_fd, " Databases: %s\n", p);
598 fprintf (reply_fd, "\n");
602 static int exec_target_list (void)
604 gw_res_trav (info.kernel_res, NULL, handle_target_list);
605 fprintf (reply_fd, "\n");
609 static int exec_target (struct ccl_token *list)
612 if (list->kind == CCL_TOK_EOL)
613 return exec_target_list ();
615 memcpy (info.target, list->name, len);
616 info.target [len] = '\0';
618 if (!info.account_in_session)
619 *info.account = '\0';
620 info.account_in_session = 0;
622 return reopen_target ();
625 static void exec_status_r (struct gw_user_set *sp)
629 exec_status_r (sp->prev);
630 fprintf (reply_fd, "%6s %7d %12.12s %.50s\n", sp->name, sp->hits,
631 sp->database, sp->search_str);
634 static int exec_status (struct ccl_token *list)
636 fprintf (reply_fd, "%s\n",
637 gw_res_get (info.kernel_res,
639 " Name Hits Database Find"));
640 exec_status_r (info.sets);
644 static int exec_base (struct ccl_token *list)
646 struct ccl_token *li = list;
650 if (list->kind == CCL_TOK_EOL)
652 free (info.database);
653 while (li->kind != CCL_TOK_EOL)
657 if (li->kind == CCL_TOK_COMMA)
660 info.database = malloc (len);
661 assert (info.database );
664 while (li->kind != CCL_TOK_EOL)
666 memcpy (info.database+len, li->name, li->len);
668 info.database[len++] = ',';
670 if (li->kind == CCL_TOK_COMMA)
673 info.database[len-1] = '\0';
677 static void present (const char *set, int offset, int number,
678 struct ccl_token *format_token)
680 const struct zass_presentent *zp;
685 max_number = gw_res_int (info.kernel_res, "gw.max.show", 100);
686 if (number > max_number)
688 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "present in set %s", set);
689 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "present of %d records from offset %d",
691 zp = zass_p_present(info.zass, (char *) set, offset, number);
696 int record_log_fd = -1;
697 const char *record_log_name;
699 record_log_name = gw_res_get (info.kernel_res, "gw.marc.log",
703 record_log_fd = open (record_log_name,
704 O_WRONLY|O_CREAT|O_APPEND, 0666);
705 if (record_log_fd == -1)
706 gw_log (GW_LOG_WARN|GW_LOG_ERRNO, "Cannot open %s",
709 fprintf (reply_fd, gw_res_get (info.kernel_res,
711 "Got %d records from set %s"),
713 fprintf (reply_fd, "\n");
714 for (i = 0, pp = zp->records; pp; pp = pp->next, i++)
718 const char *arg_ar[5];
720 fprintf (reply_fd, "--- %d/%d ---\n",
721 i+offset, offset+zp->num-1);
722 if (!gw_res_get (info.kernel_res, "gw.ignore.which", NULL))
724 if (pp->which == ZASS_REC_DIAG)
726 display_diag_error (pp->errcode, pp->errstring);
729 else if (pp->which != ZASS_REC_USMARC)
731 fprintf (reply_fd, "Unknown record kind %d\n",
736 if (record_log_fd != -1)
737 write (record_log_fd, pp->record, strlen(pp->record));
738 rec = iso2709_cvt (pp->record);
743 gw_res_get (info.kernel_res,
744 "gw.display.format", ""));
747 len = format_token->len;
748 if (len >= sizeof(format_str))
749 len = sizeof(format_str)-1;
750 memcpy (format_str, format_token->name, len);
751 format_str[len] = '\0';
753 if (info.fml && *format_str &&
754 (!strcmp (format_str, "0") || !strcmp (format_str, "1")
755 || !strcmp(format_str, "2")))
758 arg_ar[1] = format_str;
759 arg_ar[2] = " \\list";
760 arg_ar[3] = marc_to_str (info.fml, rec);
762 fml_exec_call_argv (info.fml, arg_ar);
765 iso2709_display (rec, reply_fd);
767 iso2709_display (rec, reply_fd);
772 fprintf (reply_fd, "Not a MARC record\n");
774 if (record_log_fd != -1)
775 close (record_log_fd);
779 static int exec_show (struct ccl_token *list)
781 static struct command_word show_tab [] =
788 struct ccl_token *set_token = NULL;
789 struct ccl_token *format_token = NULL;
790 struct ccl_token *li = list;
791 int no_of_present = 0;
794 while (li->kind != CCL_TOK_EOL)
797 if (li->next->kind == CCL_TOK_EQ)
799 if (li->kind == CCL_TOK_SET) /* set = <name> ? */
806 modifier_no = command_search (show_tab, li, "ccl.token.");
809 fprintf (reply_fd, "Unknown modifier in show\n");
813 if (modifier_no == 1) /* f = <name> ? */
815 else if (modifier_no == 2) /* p = <name> ? */
817 if (li->kind != CCL_TOK_EOL /* p = <name> - <name> ? */
818 && li->next->kind == CCL_TOK_MINUS
819 && li->next->next != CCL_TOK_EOL)
825 fprintf (reply_fd, "%s\n", "Missing token after '='");
833 memcpy (tmp_str, li->name, len);
835 if (atoi(tmp_str) <= 0)
837 fprintf (reply_fd, "%s\n",
838 gw_res_get (info.kernel_res, "gw.err.bad.show",
846 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Got set=%.*s", set_token->len,
849 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Got format=%.*s", format_token->len,
853 while (li->kind != CCL_TOK_EOL)
859 if (li->next->kind == CCL_TOK_EQ && li->kind != CCL_TOK_SET)
861 modifier_no = command_search (show_tab, li, "ccl.token.");
863 if (modifier_no == 2) /* p = <name> ? */
865 if (li->kind != CCL_TOK_EOL /* p = <name> - <name> ? */
866 && li->next->kind == CCL_TOK_MINUS
867 && li->next->next != CCL_TOK_EOL)
870 memcpy (tmp_str, li->name, len);
871 tmp_str [len] = '\0';
872 offset = atoi (tmp_str);
876 memcpy (tmp_str, li->name, len);
877 tmp_str [len] = '\0';
878 number = atoi (tmp_str) - offset + 1;
881 fprintf (reply_fd, "%s\n",
882 gw_res_get (info.kernel_res,
885 "greater than ending position"));
892 memcpy (tmp_str, li->name, len);
893 tmp_str [len] = '\0';
894 offset = atoi (tmp_str);
900 else if (li->kind == CCL_TOK_TERM)
903 memcpy (tmp_str, li->name, len);
905 number = atoi (tmp_str);
909 if (offset > 0 && number > 0)
911 struct gw_user_set *us;
915 len = set_token->len;
916 memcpy (tmp_str, set_token->name, len);
918 us = user_set_search (tmp_str);
921 us = user_set_search (NULL);
922 if (us && us->hits != -1) /* proper result-set? */
924 if (offset <= us->hits)
926 if (offset+number-1 > us->hits)
927 number = us->hits - offset+1;
928 present (us->name, offset, number, format_token);
929 info.next_position = offset+number;
932 else if (!no_of_present) /* display error message once! */
934 fprintf (reply_fd, "%s\n",
935 gw_res_get (info.kernel_res, "gw.err.no.set",
936 "No result-set generated"));
941 if (!no_of_present) /* no records shown so far? */
943 struct gw_user_set *us;
946 us = user_set_search (NULL);
947 if (us && us->hits != -1) /* proper result-set? */
949 default_show = gw_res_int (info.kernel_res, "gw.default.show", 20);
950 if (us->hits >= info.next_position + default_show)
952 present (us->name, info.next_position, default_show,
954 info.next_position += default_show;
956 else if (us->hits >= info.next_position)
958 present (us->name, info.next_position,
959 us->hits - info.next_position + 1,
961 info.next_position = us->hits + 1;
964 else /* display error message */
966 fprintf (reply_fd, "%s\n",
967 gw_res_get (info.kernel_res, "gw.err.no.set",
968 "No result-set generated"));
975 static void show_def (void)
977 fprintf (reply_fd, "format %s\n", gw_res_get
978 (info.kernel_res, "gw.display.format", ""));
979 fprintf (reply_fd, "autoshow %s\n", gw_res_get
980 (info.kernel_res, "gw.auto.show", ""));
981 fprintf (reply_fd, "defaultshow %s\n", gw_res_get
982 (info.kernel_res, "gw.default.show", ""));
983 fprintf (reply_fd, "language %s\n", gw_res_get
984 (info.kernel_res, "gw.language", ""));
987 static int exec_def (struct ccl_token *list)
989 const char *name = NULL;
992 static struct command_word def_tab [] =
994 { "reset", "reset" },
997 { "autoshow", "autoshow" },
998 { "defaultshow", "defaultshow" },
999 { "lang", "language" },
1003 if (info.userid < 0)
1005 sprintf (fname, "user.%d.r", info.userid);
1007 if (list->kind == CCL_TOK_EOL)
1011 int setting_no = command_search (def_tab, list, "ccl.token.");
1015 fprintf (reply_fd, "Unknown setting in def\n");
1018 if (setting_no == 1)
1024 else if (setting_no == 2)
1030 if (list->kind == CCL_TOK_EOL)
1032 strncpy (value, list->name, 127);
1033 value[(size_t) list->len] = '\0';
1037 name = "gw.display.format";
1040 name = "gw.auto.show";
1043 name = "gw.default.show";
1046 name = "gw.language";
1051 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "update file %s with %s=%s",
1052 fname, name, value);
1053 gw_res_put (info.kernel_res, "gw.username", info.from_str, fname);
1054 gw_res_put (info.kernel_res, name, value, fname);
1055 gw_res_commit (info.kernel_res, fname);
1056 if (setting_no == 6)
1064 * exec_command: parse and execute ccl command in str.
1065 * str: ccl command string
1066 * stop_flag pointer to integer. On completion the integer
1067 * is 1 (stop) or 2 (continue); 0 (other command)
1068 * return: 0 success; non-zero otherwise
1070 static int exec_command (const char *str, int *stop_flag)
1072 struct ccl_token *cmd;
1073 const char *cp = str;
1076 while (*cp && *cp != ' ' && *cp != '\n' && *cp != '\t' && *cp != '\r')
1080 no = command_search_str (command_tab, str, cp-str, "ccl.command.");
1083 if (no == 1 || no == 2 || no == 3) /* find/show/base? */
1085 if (!info.zass) /* open if not already opened */
1087 fprintf (reply_fd, "\n> %s\n", str);
1088 if (!info.zass) /* fail? */
1090 fprintf (reply_fd, "\n%s\n",
1091 gw_res_get (info.kernel_res, "gw.err.no.target",
1092 "No connection established - "
1093 "command ignored"));
1098 fprintf (reply_fd, "\n> %s\n", str);
1102 cmd = ccl_tokenize (cp);
1103 exec_find (cmd, str);
1106 cmd = ccl_tokenize (cp);
1110 cmd = ccl_token_simple (cp);
1114 cmd = ccl_token_simple (cp);
1117 case 6: /* continue */
1120 case 7: /* status */
1126 case 9: /* target */
1127 cmd = ccl_token_simple (cp);
1131 cmd = ccl_token_simple (cp);
1134 case 11: /* account */
1135 cmd = ccl_token_simple (cp);
1140 fprintf (reply_fd, "%s\n",
1141 gw_res_get (info.kernel_res, "gw.err.unimplemented",
1142 "Not implemented yet"));
1144 ccl_token_del (cmd);
1148 fprintf (reply_fd, "\n> %s\n", str);
1149 fprintf (reply_fd, " ^ %s\n",
1150 gw_res_get (info.kernel_res, "gw.err.unknown.command",
1152 "Use help to see list of commands"));
1157 int urp_start (int continuation, struct str_queue *queue)
1159 char subject_str[128];
1161 info.command_no = 0;
1162 info.reply_fname = NULL;
1164 if (email_header (queue, info.from_str, subject_str))
1166 gw_log (GW_LOG_WARN, KERNEL_LOG, "No message body");
1169 tty_init (stdout, 40, 70);
1172 info.reply_fname = (char *)
1173 tempnam (gw_res_get (info.kernel_res, "gw.reply.tmp.dir", NULL),
1174 gw_res_get (info.kernel_res,
1175 "gw.reply.tmp.prefix", "gwr"));
1177 reply_fd = fopen (info.reply_fname, "w");
1180 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, KERNEL_LOG, "Cannot create %s",
1184 tty_init (reply_fd, 0, 0);
1185 fprintf (reply_fd, "From: %s\n",
1186 gw_res_get (info.kernel_res, "gw.msg.from","Email-gateway"));
1187 fprintf (reply_fd, "Subject: ");
1189 fprintf (reply_fd, "Z39.50 Re: %s", subject_str);
1191 fprintf (reply_fd, "%s\n", gw_res_get (info.kernel_res,
1194 fprintf (reply_fd, "\n");
1195 gw_log (GW_LOG_ACCT, KERNEL_LOG, "User start %s", info.from_str);
1198 gw_log (GW_LOG_WARN, KERNEL_LOG, "No From in email header");
1199 fprintf (reply_fd, "%s\n", gw_res_get (info.kernel_res, "gw.msg.greeting",
1200 "Email->Z39.50 gateway"));
1202 fprintf (reply_fd, "%s\n", gw_res_get (info.kernel_res,
1208 int urp_command (struct str_queue *queue)
1213 while (str_queue_deq (queue, line_buf, STR_LINE_MAX))
1215 if (line_buf[0] == '\n')
1216 if (info.command_no)
1218 while (str_queue_deq (queue, 0, 0))
1224 if ((cp = strchr (line_buf, '\n')))
1226 gw_log (GW_LOG_ACCT, KERNEL_LOG, "cmd: %s", line_buf);
1227 if (isalpha (line_buf[0]))
1229 exec_command (line_buf, &stop_flag);
1232 info.command_no++; /* prevent help! */
1233 while (str_queue_deq (queue, line_buf, STR_LINE_MAX))
1245 if (!info.command_no)
1247 fprintf (reply_fd, "%s\n",
1248 gw_res_get (info.kernel_res, "gw.err.nullbody", "No body"));
1257 assert (info.reply_fname);
1261 mta = gw_res_get (info.kernel_res, "gw.reply.mta",
1262 "/usr/lib/sendmail");
1263 sprintf (cmd, "%s %s < %s", mta, info.from_str, info.reply_fname);
1264 mta_code = system (cmd);
1266 gw_log (GW_LOG_FATAL, KERNEL_LOG,
1267 "Reply '%s' got exit code %d", cmd, mta_code);
1269 unlink (info.reply_fname);
1270 gw_log (GW_LOG_ACCT, KERNEL_LOG, "User end %s", info.from_str);