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.43 1995/07/11 11:49:13 adam
49 * LINE_MAX renamed to STR_LINE_MAX.
51 * Revision 1.42 1995/07/03 12:59:29 adam
52 * New option for eti: -c dir to chdir before start.
53 * Setting change: gw.max.show defaults to 100.
55 * Revision 1.41 1995/07/03 08:20:18 adam
56 * More help information and better diagnostics.
58 * Revision 1.40 1995/05/23 08:13:00 adam
61 * Revision 1.39 1995/05/22 14:57:30 adam
62 * Next default record position also set by last p=... in show.
64 * Revision 1.38 1995/05/19 13:26:00 adam
65 * Bug fixes. Better command line options.
67 * Revision 1.37 1995/05/18 12:03:09 adam
68 * Bug fixes and minor improvements.
70 * Revision 1.36 1995/05/17 10:51:33 adam
71 * Added a few more error checks to the show command.
73 * Revision 1.35 1995/05/16 09:40:43 adam
74 * LICENSE. Setting of CCL token names (and/or/not/set) in read_kernel_res.
76 * Revision 1.34 1995/05/04 10:40:08 adam
77 * More work on Def-settings.
79 * Revision 1.33 1995/05/03 16:34:19 adam
80 * CCL def command, i.e. user definitions - saved as resource files.
82 * Revision 1.32 1995/05/03 07:37:46 adam
83 * CCL commands stop/continue implemented. New functions gw_res_{int,bool}
84 * are used when possible.
86 * Revision 1.31 1995/05/01 12:43:38 adam
87 * First work on resource monitor program.
89 * Revision 1.30 1995/04/20 16:10:47 adam
90 * Modified to work with non-blocking zass-api. Not using non-blocking
93 * Revision 1.29 1995/04/19 16:01:58 adam
94 * Some hacks to get the FIFO communication work!! Isn't reliable.
95 * Resource gw.account added - default account info.
97 * Revision 1.28 1995/04/19 13:19:09 adam
98 * New command: account - for authentication.
100 * Revision 1.27 1995/04/19 10:46:19 adam
101 * Persistency works much better now. New command: status - history-like
103 * Revision 1.26 1995/04/19 07:31:12 adam
104 * First work on Z39.50 persistence.
106 * Revision 1.25 1995/04/17 09:34:33 adam
107 * Timeout (idletime) adjustable. Minor changes in kernel.
109 * Revision 1.24 1995/03/28 11:42:35 adam
110 * First use of string-queue utility.
112 * Revision 1.23 1995/03/28 08:01:28 adam
113 * FIFO existence is used to test for a running kernel.
115 * Revision 1.22 1995/03/27 12:51:05 adam
116 * New log level in use: GW_LOG_ERRNO.
118 * Revision 1.21 1995/03/27 08:24:04 adam
119 * First use of gip interface and gw-db.
120 * First work on eti program.
122 * Revision 1.20 1995/03/03 17:19:17 adam
123 * Smarter presentation. Bug fix in email header interpretation.
125 * Revision 1.19 1995/03/02 09:32:11 adam
126 * New presentation formats. f0=full, f1=brief, f2=mid
128 * Revision 1.18 1995/03/01 14:32:26 adam
129 * Better diagnostics. Default is, that only one database selected when
132 * Revision 1.17 1995/02/28 13:16:26 adam
133 * Configurable From: added.
135 * Revision 1.16 1995/02/23 10:08:20 adam
136 * Added logging of all user commands.
138 * Revision 1.15 1995/02/23 08:32:17 adam
141 * Revision 1.13 1995/02/22 16:54:42 adam
142 * Qualifiers of LOC target updated. More logging messages.
144 * Revision 1.12 1995/02/22 15:51:51 adam
145 * Bug fix: swap of parameter number and offset in function present.
147 * Revision 1.11 1995/02/22 15:22:33 adam
148 * Much more checking of run-time state. Show command never retrieves
149 * more records than indicated by the previous search request. Help
150 * command available. The maximum number of records retrieved can be
153 * Revision 1.10 1995/02/22 08:51:35 adam
154 * Output function can be customized in fml, which is used to print
155 * the reply to reply_fd.
157 * Revision 1.9 1995/02/21 17:46:21 adam
160 * Revision 1.8 1995/02/21 12:12:00 adam
161 * Diagnostic record with error info. observed.
163 * Revision 1.7 1995/02/20 21:16:20 adam
164 * FML support. Bug fixes. Profile for drewdb.
166 * Revision 1.6 1995/02/17 14:41:14 quinn
167 * Added simple display of records.
169 * Revision 1.5 1995/02/17 14:22:13 adam
170 * First steps of CCL show command. Not finished yet.
172 * Revision 1.4 1995/02/17 09:08:36 adam
173 * Reply with subject. CCL base command implemented.
175 * Revision 1.3 1995/02/16 18:35:09 adam
176 * First use of Zdist library. Search requests are supported.
177 * Present requests are not supported yet.
179 * Revision 1.2 1995/02/16 13:21:00 adam
180 * Organization of resource files for targets and conversion
181 * language implemented.
183 * Revision 1.1 1995/02/15 17:45:30 adam
184 * First version of email gateway kernel. Email requests are read
185 * from stdin. The output is transferred to an MTA if 'From' is
186 * found in the header - or stdout if absent. No Z39.50 client is used.
192 info/status (other name?)
193 better persistence diagnostics
196 automatic information about target-aliases: name, query-support, etc.
209 #include <strqueue.h>
212 static void present (const char *set, int offset, int number,
213 struct ccl_token *format_token);
215 static char line_buf[STR_LINE_MAX+1];
217 static void put_esc_str (const char *s)
223 int reopen_target (void)
227 gw_log (GW_LOG_WARN, KERNEL_LOG, "Zass free...");
228 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "reopen_target");
229 info.zass = zass_open (info.hostname, info.port, NULL, /* complete */
230 *info.account ? info.account : NULL);
233 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Cannot connect to target %s:%d",
234 info.hostname, info.port);
235 fprintf (reply_fd, "%s %s:%d\n",
236 gw_res_get (info.kernel_res, "gw.err.connect",
237 "Cannot connect to target"),
238 info.hostname, info.port);
241 v = gw_res_get (info.kernel_res, "gw.description", NULL);
245 fprintf (reply_fd, "\n");
247 fprintf (reply_fd, "%s %s:%d\n",
248 gw_res_get (info.kernel_res, "gw.msg.connect",
249 "Connection established to"),
250 info.hostname, info.port);
252 fprintf (reply_fd, "%s: %s\n",
253 gw_res_get (info.kernel_res, "gw.msg.databases",
254 "Available databases"),
257 fprintf (reply_fd, "%s: %s\n",
258 gw_res_get (info.kernel_res, "gw.msg.database",
259 "Selected databases"),
262 fprintf (reply_fd, "set=%d\n", info.setno);
264 fprintf (reply_fd, "set=Default\n");
268 struct command_word {
270 char *resource_suffix;
273 static int command_search (struct command_word *tab, struct ccl_token *cmd,
274 const char *resource_prefix)
278 assert (resource_prefix);
281 while (tab->default_value)
283 char *cp, command_names[60];
284 char resource_name[60];
287 sprintf (resource_name, "%s%s", resource_prefix,
288 tab->resource_suffix);
289 v = gw_res_get (info.kernel_res, resource_name, tab->default_value);
291 strcpy (command_names, v);
297 if ((split = strchr (cp, ' ')))
299 if (cmd->len == strlen(cp) &&
300 !memcmp (cmd->name, cp, cmd->len))
312 static struct error_no_struct {
315 } error_ccl_tab[] = {
317 { CCL_ERR_TERM_EXPECTED, "term.expected" },
318 { CCL_ERR_RP_EXPECTED, "rp.expected" },
319 { CCL_ERR_SETNAME_EXPECTED, "setname.expected" },
320 { CCL_ERR_OP_EXPECTED, "op.expected" },
321 { CCL_ERR_BAD_RP, "bad.rp" },
322 { CCL_ERR_UNKNOWN_QUAL, "unknown.qual" },
323 { CCL_ERR_DOUBLE_QUAL, "double.qual" },
324 { CCL_ERR_EQ_EXPECTED, "eq.expected" },
325 { CCL_ERR_BAD_RELATION, "bad.relation" },
326 { CCL_ERR_TRUNC_NOT_LEFT, "trunc.not.left" },
327 { CCL_ERR_TRUNC_NOT_BOTH, "trunc.not.both" },
328 { CCL_ERR_TRUNC_NOT_RIGHT, "trunc.not.right" },
332 static char *error_no_search (struct error_no_struct *tab, int no)
334 struct error_no_struct *p = tab;
335 while (p->resource_name)
338 return p->resource_name;
344 static int email_header (struct str_queue *sq,
345 char *from_str, char *subject_str)
349 while (str_queue_deq (sq, line_buf, STR_LINE_MAX))
351 if (line_buf[0] == '\n')
353 if (memcmp (line_buf, "From ", 5) == 0)
354 sscanf (line_buf+4, "%s", from_str);
355 if (memcmp (line_buf, "Subject: ", 9) == 0 &&
356 sscanf (line_buf+9, "%s", subject_str+1) == 1)
357 strcpy (subject_str, line_buf+9);
362 static void help_general (void)
364 put_esc_str (gw_res_get (info.kernel_res, "gw.help.general",
365 "Commands available in this service:\n"));
368 static int exec_help (struct ccl_token *list)
370 static char *sep = "-------------------------------\\n";
374 put_esc_str (gw_res_get (info.kernel_res, "gw.help.target",
375 "target <name> - selects a given target\n"));
378 put_esc_str (gw_res_get (info.kernel_res, "gw.help.base",
379 "base <base>.. - selects databases\n"));
382 put_esc_str (gw_res_get (info.kernel_res, "gw.help.find",
383 "find <query> - performs a search request\n"));
386 put_esc_str (gw_res_get (info.kernel_res, "gw.help.show",
387 "show <spec> - retrieves and displays "
390 put_esc_str (gw_res_get (info.kernel_res, "gw.help.def",
391 "def <setting> <value> sets/gets definitons\n"));
393 put_esc_str (gw_res_get (info.kernel_res, "gw.help.account",
394 "account <str> sets authentication string\n"));
398 static void display_diag_error (int code, const char *addinfo)
402 sprintf (str, "gw.bib1.diag.%d", code);
403 fprintf (reply_fd, "%s %d:\n %s: '%s'\n",
404 gw_res_get (info.kernel_res, "gw.msg.z39errcode",
407 gw_res_get (info.kernel_res, str, ""), addinfo);
410 static int exec_find (struct ccl_token *list, const char *search_str)
412 const struct zass_searchent *p;
413 struct gw_user_set *us;
417 struct ccl_rpn_node *rpn;
421 if (info.setno == -1)
422 strcpy (setname, "Default");
424 sprintf (setname, "%d", info.setno);
425 info.next_position = 1;
428 rpn = ccl_find (info.bibset, list, &error, &pos);
431 const char *v = NULL, *n;
434 fprintf (reply_fd, " %*s^ - ", pos - line_buf, " ");
436 n = error_no_search (error_ccl_tab, error);
439 sprintf (name, "gw.err.%s", n);
440 v = gw_res_get (info.kernel_res, name, NULL);
443 v = ccl_err_msg (error);
444 fprintf (reply_fd, "%s\n", v);
447 ccl_pr_tree (rpn, reply_fd);
448 fprintf (reply_fd, "\n");
450 if (!*info.database )
452 fprintf (reply_fd, "%s\n",
453 gw_res_get (info.kernel_res, "gw.err.no.database",
454 "You must select database"));
457 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Searching in database %s",
460 p = zass_p_search (info.zass, rpn, setname, info.database, info.sets);
463 fprintf (reply_fd, "%s\n",
464 gw_res_get (info.kernel_res, "gw.err.search.fail",
468 if (p->errcode != -1)
470 display_diag_error (p->errcode, p->errstring);
473 fprintf (reply_fd, "%d %s %s\n", p->num,
474 gw_res_get (info.kernel_res, "gw.msg.hits", "hit(s) in set "),
476 us = user_set_add (setname, p->num, info.database, rpn, 1, search_str);
477 autoshow = gw_res_int (info.kernel_res, "gw.auto.show", 0);
478 if (autoshow && p->num > 0)
480 if (autoshow > p->num)
482 present (setname, 1, p->num, NULL);
483 info.next_position = 1+p->num;
487 present (setname, 1, autoshow, NULL);
488 info.next_position = 1+autoshow;
494 static int exec_account (struct ccl_token *list)
496 if (list->kind != CCL_TOK_EOL)
499 memcpy (info.account, list->name, len);
500 info.target[len] = '\0';
503 *info.account = '\0';
507 static int exec_target (struct ccl_token *list)
510 if (list->kind == CCL_TOK_EOL)
513 memcpy (info.target, list->name, len);
514 info.target [len] = '\0';
517 return reopen_target ();
520 static void exec_status_r (struct gw_user_set *sp)
524 exec_status_r (sp->prev);
525 fprintf (reply_fd, "%6s %7d %12.12s %.50s\n", sp->name, sp->hits,
526 sp->database, sp->search_str);
529 static int exec_status (struct ccl_token *list)
531 fprintf (reply_fd, "%s\n",
532 gw_res_get (info.kernel_res,
534 " Name Hits Database Find"));
535 exec_status_r (info.sets);
539 static int exec_base (struct ccl_token *list)
541 struct ccl_token *li = list;
545 if (list->kind == CCL_TOK_EOL)
547 free (info.database);
548 while (li->kind != CCL_TOK_EOL)
552 if (li->kind == CCL_TOK_COMMA)
555 info.database = malloc (len);
556 assert (info.database );
559 while (li->kind != CCL_TOK_EOL)
561 memcpy (info.database+len, li->name, li->len);
563 info.database[len++] = ',';
565 if (li->kind == CCL_TOK_COMMA)
568 info.database[len-1] = '\0';
572 static void present (const char *set, int offset, int number,
573 struct ccl_token *format_token)
575 const struct zass_presentent *zp;
580 max_number = gw_res_int (info.kernel_res, "gw.max.show", 100);
581 if (number > max_number)
583 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "present in set %s", set);
584 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "present of %d records from offset %d",
586 zp = zass_p_present(info.zass, (char *) set, offset, number);
591 int record_log_fd = -1;
592 const char *record_log_name;
594 record_log_name = gw_res_get (info.kernel_res, "gw.marc.log",
598 record_log_fd = open (record_log_name,
599 O_WRONLY|O_CREAT|O_APPEND, 0666);
600 if (record_log_fd == -1)
601 gw_log (GW_LOG_WARN|GW_LOG_ERRNO, "Cannot open %s",
604 fprintf (reply_fd, gw_res_get (info.kernel_res,
606 "Got %d records from set %s"),
608 fprintf (reply_fd, "\n");
609 for (i = 0, pp = zp->records; pp; pp = pp->next, i++)
613 const char *arg_ar[5];
615 fprintf (reply_fd, "--- %d/%d ---\n",
616 i+offset, offset+zp->num-1);
617 if (!gw_res_get (info.kernel_res, "gw.ignore.which", NULL))
619 if (pp->which == ZASS_REC_DIAG)
621 display_diag_error (pp->errcode, pp->errstring);
624 else if (pp->which != ZASS_REC_USMARC)
626 fprintf (reply_fd, "Unknown record kind %d\n",
631 if (record_log_fd != -1)
632 write (record_log_fd, pp->record, strlen(pp->record));
633 rec = iso2709_cvt (pp->record);
638 gw_res_get (info.kernel_res,
639 "gw.display.format", ""));
642 len = format_token->len;
643 if (len >= sizeof(format_str))
644 len = sizeof(format_str)-1;
645 memcpy (format_str, format_token->name, len);
646 format_str[len] = '\0';
648 if (info.fml && *format_str &&
649 (!strcmp (format_str, "0") || !strcmp (format_str, "1")
650 || !strcmp(format_str, "2")))
653 arg_ar[1] = format_str;
654 arg_ar[2] = " \\list";
655 arg_ar[3] = marc_to_str (info.fml, rec);
657 fml_exec_call_argv (info.fml, arg_ar);
660 iso2709_display (rec, reply_fd);
662 iso2709_display (rec, reply_fd);
667 fprintf (reply_fd, "Not a MARC record\n");
669 if (record_log_fd != -1)
670 close (record_log_fd);
674 static int exec_show (struct ccl_token *list)
676 static struct command_word show_tab [] =
683 struct ccl_token *set_token = NULL;
684 struct ccl_token *format_token = NULL;
685 struct ccl_token *li = list;
686 int no_of_present = 0;
689 while (li->kind != CCL_TOK_EOL)
692 if (li->next->kind == CCL_TOK_EQ)
694 if (li->kind == CCL_TOK_SET) /* set = <name> ? */
701 modifier_no = command_search (show_tab, li, "ccl.token.");
704 fprintf (reply_fd, "Unknown modifier in show\n");
708 if (modifier_no == 1) /* f = <name> ? */
710 else if (modifier_no == 2) /* p = <name> ? */
712 if (li->kind != CCL_TOK_EOL /* p = <name> - <name> ? */
713 && li->next->kind == CCL_TOK_MINUS
714 && li->next->next != CCL_TOK_EOL)
720 fprintf (reply_fd, "%s\n", "Missing token after '='");
728 memcpy (tmp_str, li->name, len);
730 if (atoi(tmp_str) <= 0)
732 fprintf (reply_fd, "%s\n",
733 gw_res_get (info.kernel_res, "gw.err.bad.show",
741 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Got set=%.*s", set_token->len,
744 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Got format=%.*s", format_token->len,
748 while (li->kind != CCL_TOK_EOL)
754 if (li->next->kind == CCL_TOK_EQ && li->kind != CCL_TOK_SET)
756 modifier_no = command_search (show_tab, li, "ccl.token.");
758 if (modifier_no == 2) /* p = <name> ? */
760 if (li->kind != CCL_TOK_EOL /* p = <name> - <name> ? */
761 && li->next->kind == CCL_TOK_MINUS
762 && li->next->next != CCL_TOK_EOL)
765 memcpy (tmp_str, li->name, len);
766 tmp_str [len] = '\0';
767 offset = atoi (tmp_str);
771 memcpy (tmp_str, li->name, len);
772 tmp_str [len] = '\0';
773 number = atoi (tmp_str) - offset + 1;
776 fprintf (reply_fd, "%s\n",
777 gw_res_get (info.kernel_res,
780 "greater than ending position"));
787 memcpy (tmp_str, li->name, len);
788 tmp_str [len] = '\0';
789 offset = atoi (tmp_str);
795 else if (li->kind == CCL_TOK_TERM)
798 memcpy (tmp_str, li->name, len);
800 number = atoi (tmp_str);
804 if (offset > 0 && number > 0)
806 struct gw_user_set *us;
810 len = set_token->len;
811 memcpy (tmp_str, set_token->name, len);
813 us = user_set_search (tmp_str);
816 us = user_set_search (NULL);
817 if (us && us->hits != -1) /* proper result-set? */
819 if (offset <= us->hits)
821 if (offset+number-1 > us->hits)
822 number = us->hits - offset+1;
823 present (us->name, offset, number, format_token);
824 info.next_position = offset+number;
827 else if (!no_of_present) /* display error message once! */
829 fprintf (reply_fd, "%s\n",
830 gw_res_get (info.kernel_res, "gw.err.no.set",
831 "No result-set generated"));
836 if (!no_of_present) /* no records shown so far? */
838 struct gw_user_set *us;
841 us = user_set_search (NULL);
842 if (us && us->hits != -1) /* proper result-set? */
844 default_show = gw_res_int (info.kernel_res, "gw.default.show", 20);
845 if (us->hits >= info.next_position + default_show)
847 present (us->name, info.next_position, default_show,
849 info.next_position += default_show;
851 else if (us->hits >= info.next_position)
853 present (us->name, info.next_position,
854 us->hits - info.next_position + 1,
856 info.next_position = us->hits + 1;
859 else /* display error message */
861 fprintf (reply_fd, "%s\n",
862 gw_res_get (info.kernel_res, "gw.err.no.set",
863 "No result-set generated"));
870 static void show_def (void)
872 fprintf (reply_fd, "format %s\n", gw_res_get
873 (info.kernel_res, "gw.display.format", ""));
874 fprintf (reply_fd, "autoshow %s\n", gw_res_get
875 (info.kernel_res, "gw.auto.show", ""));
876 fprintf (reply_fd, "defaultshow %s\n", gw_res_get
877 (info.kernel_res, "gw.default.show", ""));
878 fprintf (reply_fd, "language %s\n", gw_res_get
879 (info.kernel_res, "gw.language", ""));
882 static int exec_def (struct ccl_token *list)
884 const char *name = NULL;
887 static struct command_word def_tab [] =
889 { "reset", "reset" },
892 { "autoshow", "autoshow" },
893 { "defaultshow", "defaultshow" },
894 { "lang", "language" },
900 sprintf (fname, "user.%d.r", info.userid);
902 if (list->kind == CCL_TOK_EOL)
906 int setting_no = command_search (def_tab, list, "ccl.token.");
910 fprintf (reply_fd, "Unknown setting in def\n");
919 else if (setting_no == 2)
925 if (list->kind == CCL_TOK_EOL)
927 strncpy (value, list->name, 127);
928 value[(size_t) list->len] = '\0';
932 name = "gw.display.format";
935 name = "gw.auto.show";
938 name = "gw.default.show";
941 name = "gw.language";
946 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "update file %s with %s=%s",
948 gw_res_put (info.kernel_res, "gw.username", info.from_str, fname);
949 gw_res_put (info.kernel_res, name, value, fname);
950 gw_res_commit (info.kernel_res, fname);
959 * exec_command: parse and execute ccl command in str.
960 * str: ccl command string
961 * stop_flag pointer to integer. On completion the integer
962 * is 1 (stop) or 2 (continue); 0 (other command)
963 * return: 0 success; non-zero otherwise
965 static int exec_command (const char *str, int *stop_flag)
967 static struct command_word command_tab [] =
974 { "continue", "continue" },
975 { "status", "status" },
977 { "target", "target" },
979 { "account", "account" },
983 struct ccl_token *cmd = ccl_tokenize (str);
987 if (cmd->kind != CCL_TOK_EOL &&
988 (no = command_search (command_tab, cmd, "ccl.command.")))
990 if (no == 1 || no == 2 || no == 3) /* find/show/base? */
992 if (!info.zass) /* open if not already opened */
994 fprintf (reply_fd, "\n> %s\n", str);
995 if (!info.zass) /* fail? */
997 fprintf (reply_fd, "\n%s\n",
998 gw_res_get (info.kernel_res, "gw.err.no.target",
999 "No connection established - "
1000 "command ignored"));
1005 fprintf (reply_fd, "\n> %s\n", str);
1009 return exec_find (cmd->next, str);
1011 return exec_show (cmd->next);
1013 return exec_base (cmd->next);
1015 return exec_help (cmd->next);
1016 case 6: /* continue */
1019 case 7: /* status */
1020 return exec_status (cmd->next);
1024 case 9: /* target */
1025 return exec_target (cmd->next);
1027 return exec_def (cmd->next);
1029 return exec_account (cmd->next);
1031 fprintf (reply_fd, "%s\n",
1032 gw_res_get (info.kernel_res, "gw.err.unimplemented",
1033 "Not implemented yet"));
1038 fprintf (reply_fd, "\n> %s\n", str);
1039 fprintf (reply_fd, " ^ %s\n",
1040 gw_res_get (info.kernel_res, "gw.err.unknown.command",
1042 "Use help to see list of commands"));
1047 int urp_start (int continuation, struct str_queue *queue)
1049 char subject_str[128];
1051 info.command_no = 0;
1052 info.reply_fname = NULL;
1054 if (email_header (queue, info.from_str, subject_str))
1056 gw_log (GW_LOG_WARN, KERNEL_LOG, "No message body");
1059 tty_init (stdout, 40, 70);
1062 info.reply_fname = (char *)
1063 tempnam (gw_res_get (info.kernel_res, "gw.reply.tmp.dir", NULL),
1064 gw_res_get (info.kernel_res,
1065 "gw.reply.tmp.prefix", "gwr"));
1067 reply_fd = fopen (info.reply_fname, "w");
1070 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, KERNEL_LOG, "Cannot create %s",
1074 tty_init (reply_fd, 0, 0);
1075 fprintf (reply_fd, "From: %s\n",
1076 gw_res_get (info.kernel_res, "gw.msg.from","Email-gateway"));
1077 fprintf (reply_fd, "Subject: ");
1079 fprintf (reply_fd, "Z39.50 Re: %s", subject_str);
1081 fprintf (reply_fd, "%s\n", gw_res_get (info.kernel_res,
1084 fprintf (reply_fd, "\n");
1085 gw_log (GW_LOG_ACCT, KERNEL_LOG, "User start %s", info.from_str);
1088 gw_log (GW_LOG_WARN, KERNEL_LOG, "No From in email header");
1089 fprintf (reply_fd, "%s\n", gw_res_get (info.kernel_res, "gw.msg.greeting",
1090 "Email->Z39.50 gateway"));
1092 fprintf (reply_fd, "%s\n", gw_res_get (info.kernel_res,
1098 int urp_command (struct str_queue *queue)
1103 while (str_queue_deq (queue, line_buf, STR_LINE_MAX))
1105 if (line_buf[0] == '\n')
1106 if (info.command_no)
1108 while (str_queue_deq (queue, 0, 0))
1114 if ((cp = strchr (line_buf, '\n')))
1116 gw_log (GW_LOG_ACCT, KERNEL_LOG, "cmd: %s", line_buf);
1117 if (isalpha (line_buf[0]))
1119 exec_command (line_buf, &stop_flag);
1122 info.command_no++; /* prevent help! */
1123 while (str_queue_deq (queue, line_buf, STR_LINE_MAX))
1135 if (!info.command_no)
1137 fprintf (reply_fd, "%s\n",
1138 gw_res_get (info.kernel_res, "gw.err.nullbody", "No body"));
1147 assert (info.reply_fname);
1151 mta = gw_res_get (info.kernel_res, "gw.reply.mta",
1152 "/usr/lib/sendmail");
1153 sprintf (cmd, "%s %s < %s", mta, info.from_str, info.reply_fname);
1154 mta_code = system (cmd);
1156 gw_log (GW_LOG_FATAL, KERNEL_LOG,
1157 "Reply '%s' got exit code %d", cmd, mta_code);
1159 unlink (info.reply_fname);
1160 gw_log (GW_LOG_ACCT, KERNEL_LOG, "User end %s", info.from_str);