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.45 1995/07/20 08:20:24 adam
51 * Revision 1.44 1995/07/11 12:28:53 adam
52 * New functions ccl_token_simple and ccl_token_del used.
54 * Revision 1.43 1995/07/11 11:49:13 adam
55 * LINE_MAX renamed to STR_LINE_MAX.
57 * Revision 1.42 1995/07/03 12:59:29 adam
58 * New option for eti: -c dir to chdir before start.
59 * Setting change: gw.max.show defaults to 100.
61 * Revision 1.41 1995/07/03 08:20:18 adam
62 * More help information and better diagnostics.
64 * Revision 1.40 1995/05/23 08:13:00 adam
67 * Revision 1.39 1995/05/22 14:57:30 adam
68 * Next default record position also set by last p=... in show.
70 * Revision 1.38 1995/05/19 13:26:00 adam
71 * Bug fixes. Better command line options.
73 * Revision 1.37 1995/05/18 12:03:09 adam
74 * Bug fixes and minor improvements.
76 * Revision 1.36 1995/05/17 10:51:33 adam
77 * Added a few more error checks to the show command.
79 * Revision 1.35 1995/05/16 09:40:43 adam
80 * LICENSE. Setting of CCL token names (and/or/not/set) in read_kernel_res.
82 * Revision 1.34 1995/05/04 10:40:08 adam
83 * More work on Def-settings.
85 * Revision 1.33 1995/05/03 16:34:19 adam
86 * CCL def command, i.e. user definitions - saved as resource files.
88 * Revision 1.32 1995/05/03 07:37:46 adam
89 * CCL commands stop/continue implemented. New functions gw_res_{int,bool}
90 * are used when possible.
92 * Revision 1.31 1995/05/01 12:43:38 adam
93 * First work on resource monitor program.
95 * Revision 1.30 1995/04/20 16:10:47 adam
96 * Modified to work with non-blocking zass-api. Not using non-blocking
99 * Revision 1.29 1995/04/19 16:01:58 adam
100 * Some hacks to get the FIFO communication work!! Isn't reliable.
101 * Resource gw.account added - default account info.
103 * Revision 1.28 1995/04/19 13:19:09 adam
104 * New command: account - for authentication.
106 * Revision 1.27 1995/04/19 10:46:19 adam
107 * Persistency works much better now. New command: status - history-like
109 * Revision 1.26 1995/04/19 07:31:12 adam
110 * First work on Z39.50 persistence.
112 * Revision 1.25 1995/04/17 09:34:33 adam
113 * Timeout (idletime) adjustable. Minor changes in kernel.
115 * Revision 1.24 1995/03/28 11:42:35 adam
116 * First use of string-queue utility.
118 * Revision 1.23 1995/03/28 08:01:28 adam
119 * FIFO existence is used to test for a running kernel.
121 * Revision 1.22 1995/03/27 12:51:05 adam
122 * New log level in use: GW_LOG_ERRNO.
124 * Revision 1.21 1995/03/27 08:24:04 adam
125 * First use of gip interface and gw-db.
126 * First work on eti program.
128 * Revision 1.20 1995/03/03 17:19:17 adam
129 * Smarter presentation. Bug fix in email header interpretation.
131 * Revision 1.19 1995/03/02 09:32:11 adam
132 * New presentation formats. f0=full, f1=brief, f2=mid
134 * Revision 1.18 1995/03/01 14:32:26 adam
135 * Better diagnostics. Default is, that only one database selected when
138 * Revision 1.17 1995/02/28 13:16:26 adam
139 * Configurable From: added.
141 * Revision 1.16 1995/02/23 10:08:20 adam
142 * Added logging of all user commands.
144 * Revision 1.15 1995/02/23 08:32:17 adam
147 * Revision 1.13 1995/02/22 16:54:42 adam
148 * Qualifiers of LOC target updated. More logging messages.
150 * Revision 1.12 1995/02/22 15:51:51 adam
151 * Bug fix: swap of parameter number and offset in function present.
153 * Revision 1.11 1995/02/22 15:22:33 adam
154 * Much more checking of run-time state. Show command never retrieves
155 * more records than indicated by the previous search request. Help
156 * command available. The maximum number of records retrieved can be
159 * Revision 1.10 1995/02/22 08:51:35 adam
160 * Output function can be customized in fml, which is used to print
161 * the reply to reply_fd.
163 * Revision 1.9 1995/02/21 17:46:21 adam
166 * Revision 1.8 1995/02/21 12:12:00 adam
167 * Diagnostic record with error info. observed.
169 * Revision 1.7 1995/02/20 21:16:20 adam
170 * FML support. Bug fixes. Profile for drewdb.
172 * Revision 1.6 1995/02/17 14:41:14 quinn
173 * Added simple display of records.
175 * Revision 1.5 1995/02/17 14:22:13 adam
176 * First steps of CCL show command. Not finished yet.
178 * Revision 1.4 1995/02/17 09:08:36 adam
179 * Reply with subject. CCL base command implemented.
181 * Revision 1.3 1995/02/16 18:35:09 adam
182 * First use of Zdist library. Search requests are supported.
183 * Present requests are not supported yet.
185 * Revision 1.2 1995/02/16 13:21:00 adam
186 * Organization of resource files for targets and conversion
187 * language implemented.
189 * Revision 1.1 1995/02/15 17:45:30 adam
190 * First version of email gateway kernel. Email requests are read
191 * from stdin. The output is transferred to an MTA if 'From' is
192 * found in the header - or stdout if absent. No Z39.50 client is used.
198 info/status (other name?)
199 better persistence diagnostics
202 automatic information about target-aliases: name, query-support, etc.
215 #include <strqueue.h>
218 static void present (const char *set, int offset, int number,
219 struct ccl_token *format_token);
221 static char line_buf[STR_LINE_MAX+1];
223 static void put_esc_str (const char *s)
229 int reopen_target (void)
233 gw_log (GW_LOG_WARN, KERNEL_LOG, "Zass free...");
234 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "reopen_target");
235 info.zass = zass_open (info.hostname, info.port, NULL, /* complete */
236 *info.account ? info.account : NULL);
239 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Cannot connect to target %s:%d",
240 info.hostname, info.port);
241 fprintf (reply_fd, "%s %s:%d\n",
242 gw_res_get (info.kernel_res, "gw.err.connect",
243 "Cannot connect to target"),
244 info.hostname, info.port);
247 v = gw_res_get (info.kernel_res, "gw.description", NULL);
251 fprintf (reply_fd, "\n");
253 fprintf (reply_fd, "%s %s:%d\n",
254 gw_res_get (info.kernel_res, "gw.msg.connect",
255 "Connection established to"),
256 info.hostname, info.port);
258 fprintf (reply_fd, "%s: %s\n",
259 gw_res_get (info.kernel_res, "gw.msg.databases",
260 "Available databases"),
263 fprintf (reply_fd, "%s: %s\n",
264 gw_res_get (info.kernel_res, "gw.msg.database",
265 "Selected databases"),
268 fprintf (reply_fd, "set=%d\n", info.setno);
270 fprintf (reply_fd, "set=Default\n");
274 struct command_word {
276 char *resource_suffix;
279 static int command_search_str (struct command_word *tab, const char *cmd_name,
280 int cmd_len, const char *resource_prefix)
284 assert (resource_prefix);
286 while (tab->default_value)
288 char *cp, command_names[60];
289 char resource_name[60];
292 sprintf (resource_name, "%s%s", resource_prefix,
293 tab->resource_suffix);
294 v = gw_res_get (info.kernel_res, resource_name, tab->default_value);
296 strcpy (command_names, v);
302 if ((split = strchr (cp, ' ')))
304 if (cmd_len == strlen(cp) &&
305 !memcmp (cmd_name, cp, cmd_len))
317 static int command_search (struct command_word *tab, struct ccl_token *cmd,
318 const char *resource_prefix)
320 return command_search_str (tab, cmd->name, cmd->len, resource_prefix);
323 static struct error_no_struct {
326 } error_ccl_tab[] = {
328 { CCL_ERR_TERM_EXPECTED, "term.expected" },
329 { CCL_ERR_RP_EXPECTED, "rp.expected" },
330 { CCL_ERR_SETNAME_EXPECTED, "setname.expected" },
331 { CCL_ERR_OP_EXPECTED, "op.expected" },
332 { CCL_ERR_BAD_RP, "bad.rp" },
333 { CCL_ERR_UNKNOWN_QUAL, "unknown.qual" },
334 { CCL_ERR_DOUBLE_QUAL, "double.qual" },
335 { CCL_ERR_EQ_EXPECTED, "eq.expected" },
336 { CCL_ERR_BAD_RELATION, "bad.relation" },
337 { CCL_ERR_TRUNC_NOT_LEFT, "trunc.not.left" },
338 { CCL_ERR_TRUNC_NOT_BOTH, "trunc.not.both" },
339 { CCL_ERR_TRUNC_NOT_RIGHT, "trunc.not.right" },
343 static char *error_no_search (struct error_no_struct *tab, int no)
345 struct error_no_struct *p = tab;
346 while (p->resource_name)
349 return p->resource_name;
355 static int email_header (struct str_queue *sq,
356 char *from_str, char *subject_str)
360 while (str_queue_deq (sq, line_buf, STR_LINE_MAX))
362 if (line_buf[0] == '\n')
364 if (memcmp (line_buf, "From ", 5) == 0)
365 sscanf (line_buf+4, "%s", from_str);
366 if (memcmp (line_buf, "Subject: ", 9) == 0 &&
367 sscanf (line_buf+9, "%s", subject_str+1) == 1)
368 strcpy (subject_str, line_buf+9);
373 static void help_general (void)
375 put_esc_str (gw_res_get (info.kernel_res, "gw.help.general",
376 "Commands available in this service:\n"));
379 static int exec_help (struct ccl_token *list)
381 static char *sep = "-------------------------------\\n";
385 put_esc_str (gw_res_get (info.kernel_res, "gw.help.target",
386 "target <name> - selects a given target\n"));
389 put_esc_str (gw_res_get (info.kernel_res, "gw.help.base",
390 "base <base>.. - selects databases\n"));
393 put_esc_str (gw_res_get (info.kernel_res, "gw.help.find",
394 "find <query> - performs a search request\n"));
397 put_esc_str (gw_res_get (info.kernel_res, "gw.help.show",
398 "show <spec> - retrieves and displays "
401 put_esc_str (gw_res_get (info.kernel_res, "gw.help.def",
402 "def <setting> <value> sets/gets definitons\n"));
404 put_esc_str (gw_res_get (info.kernel_res, "gw.help.account",
405 "account <str> sets authentication string\n"));
409 static void display_diag_error (int code, const char *addinfo)
413 sprintf (str, "gw.bib1.diag.%d", code);
414 fprintf (reply_fd, "%s %d:\n %s: '%s'\n",
415 gw_res_get (info.kernel_res, "gw.msg.z39errcode",
418 gw_res_get (info.kernel_res, str, ""), addinfo);
421 static int exec_find (struct ccl_token *list, const char *search_str)
423 const struct zass_searchent *p;
424 struct gw_user_set *us;
428 struct ccl_rpn_node *rpn;
432 if (info.setno == -1)
433 strcpy (setname, "Default");
435 sprintf (setname, "%d", info.setno);
436 info.next_position = 1;
439 rpn = ccl_find (info.bibset, list, &error, &pos);
442 const char *v = NULL, *n;
445 fprintf (reply_fd, " %*s^ - ", pos - line_buf, " ");
447 n = error_no_search (error_ccl_tab, error);
450 sprintf (name, "gw.err.%s", n);
451 v = gw_res_get (info.kernel_res, name, NULL);
454 v = ccl_err_msg (error);
455 fprintf (reply_fd, "%s\n", v);
458 ccl_pr_tree (rpn, reply_fd);
459 fprintf (reply_fd, "\n");
461 if (!*info.database )
463 fprintf (reply_fd, "%s\n",
464 gw_res_get (info.kernel_res, "gw.err.no.database",
465 "You must select database"));
468 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Searching in database %s",
471 p = zass_p_search (info.zass, rpn, setname, info.database, info.sets);
474 fprintf (reply_fd, "%s\n",
475 gw_res_get (info.kernel_res, "gw.err.search.fail",
479 if (p->errcode != -1)
481 display_diag_error (p->errcode, p->errstring);
484 fprintf (reply_fd, "%d %s %s\n", p->num,
485 gw_res_get (info.kernel_res, "gw.msg.hits", "hit(s) in set "),
487 us = user_set_add (setname, p->num, info.database, rpn, 1, search_str);
488 autoshow = gw_res_int (info.kernel_res, "gw.auto.show", 0);
489 if (autoshow && p->num > 0)
491 if (autoshow > p->num)
493 present (setname, 1, p->num, NULL);
494 info.next_position = 1+p->num;
498 present (setname, 1, autoshow, NULL);
499 info.next_position = 1+autoshow;
505 static int exec_account (struct ccl_token *list)
507 if (list->kind != CCL_TOK_EOL)
510 memcpy (info.account, list->name, len);
511 info.target[len] = '\0';
514 *info.account = '\0';
518 static int exec_target (struct ccl_token *list)
521 if (list->kind == CCL_TOK_EOL)
524 memcpy (info.target, list->name, len);
525 info.target [len] = '\0';
528 return reopen_target ();
531 static void exec_status_r (struct gw_user_set *sp)
535 exec_status_r (sp->prev);
536 fprintf (reply_fd, "%6s %7d %12.12s %.50s\n", sp->name, sp->hits,
537 sp->database, sp->search_str);
540 static int exec_status (struct ccl_token *list)
542 fprintf (reply_fd, "%s\n",
543 gw_res_get (info.kernel_res,
545 " Name Hits Database Find"));
546 exec_status_r (info.sets);
550 static int exec_base (struct ccl_token *list)
552 struct ccl_token *li = list;
556 if (list->kind == CCL_TOK_EOL)
558 free (info.database);
559 while (li->kind != CCL_TOK_EOL)
563 if (li->kind == CCL_TOK_COMMA)
566 info.database = malloc (len);
567 assert (info.database );
570 while (li->kind != CCL_TOK_EOL)
572 memcpy (info.database+len, li->name, li->len);
574 info.database[len++] = ',';
576 if (li->kind == CCL_TOK_COMMA)
579 info.database[len-1] = '\0';
583 static void present (const char *set, int offset, int number,
584 struct ccl_token *format_token)
586 const struct zass_presentent *zp;
591 max_number = gw_res_int (info.kernel_res, "gw.max.show", 100);
592 if (number > max_number)
594 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "present in set %s", set);
595 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "present of %d records from offset %d",
597 zp = zass_p_present(info.zass, (char *) set, offset, number);
602 int record_log_fd = -1;
603 const char *record_log_name;
605 record_log_name = gw_res_get (info.kernel_res, "gw.marc.log",
609 record_log_fd = open (record_log_name,
610 O_WRONLY|O_CREAT|O_APPEND, 0666);
611 if (record_log_fd == -1)
612 gw_log (GW_LOG_WARN|GW_LOG_ERRNO, "Cannot open %s",
615 fprintf (reply_fd, gw_res_get (info.kernel_res,
617 "Got %d records from set %s"),
619 fprintf (reply_fd, "\n");
620 for (i = 0, pp = zp->records; pp; pp = pp->next, i++)
624 const char *arg_ar[5];
626 fprintf (reply_fd, "--- %d/%d ---\n",
627 i+offset, offset+zp->num-1);
628 if (!gw_res_get (info.kernel_res, "gw.ignore.which", NULL))
630 if (pp->which == ZASS_REC_DIAG)
632 display_diag_error (pp->errcode, pp->errstring);
635 else if (pp->which != ZASS_REC_USMARC)
637 fprintf (reply_fd, "Unknown record kind %d\n",
642 if (record_log_fd != -1)
643 write (record_log_fd, pp->record, strlen(pp->record));
644 rec = iso2709_cvt (pp->record);
649 gw_res_get (info.kernel_res,
650 "gw.display.format", ""));
653 len = format_token->len;
654 if (len >= sizeof(format_str))
655 len = sizeof(format_str)-1;
656 memcpy (format_str, format_token->name, len);
657 format_str[len] = '\0';
659 if (info.fml && *format_str &&
660 (!strcmp (format_str, "0") || !strcmp (format_str, "1")
661 || !strcmp(format_str, "2")))
664 arg_ar[1] = format_str;
665 arg_ar[2] = " \\list";
666 arg_ar[3] = marc_to_str (info.fml, rec);
668 fml_exec_call_argv (info.fml, arg_ar);
671 iso2709_display (rec, reply_fd);
673 iso2709_display (rec, reply_fd);
678 fprintf (reply_fd, "Not a MARC record\n");
680 if (record_log_fd != -1)
681 close (record_log_fd);
685 static int exec_show (struct ccl_token *list)
687 static struct command_word show_tab [] =
694 struct ccl_token *set_token = NULL;
695 struct ccl_token *format_token = NULL;
696 struct ccl_token *li = list;
697 int no_of_present = 0;
700 while (li->kind != CCL_TOK_EOL)
703 if (li->next->kind == CCL_TOK_EQ)
705 if (li->kind == CCL_TOK_SET) /* set = <name> ? */
712 modifier_no = command_search (show_tab, li, "ccl.token.");
715 fprintf (reply_fd, "Unknown modifier in show\n");
719 if (modifier_no == 1) /* f = <name> ? */
721 else if (modifier_no == 2) /* p = <name> ? */
723 if (li->kind != CCL_TOK_EOL /* p = <name> - <name> ? */
724 && li->next->kind == CCL_TOK_MINUS
725 && li->next->next != CCL_TOK_EOL)
731 fprintf (reply_fd, "%s\n", "Missing token after '='");
739 memcpy (tmp_str, li->name, len);
741 if (atoi(tmp_str) <= 0)
743 fprintf (reply_fd, "%s\n",
744 gw_res_get (info.kernel_res, "gw.err.bad.show",
752 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Got set=%.*s", set_token->len,
755 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Got format=%.*s", format_token->len,
759 while (li->kind != CCL_TOK_EOL)
765 if (li->next->kind == CCL_TOK_EQ && li->kind != CCL_TOK_SET)
767 modifier_no = command_search (show_tab, li, "ccl.token.");
769 if (modifier_no == 2) /* p = <name> ? */
771 if (li->kind != CCL_TOK_EOL /* p = <name> - <name> ? */
772 && li->next->kind == CCL_TOK_MINUS
773 && li->next->next != CCL_TOK_EOL)
776 memcpy (tmp_str, li->name, len);
777 tmp_str [len] = '\0';
778 offset = atoi (tmp_str);
782 memcpy (tmp_str, li->name, len);
783 tmp_str [len] = '\0';
784 number = atoi (tmp_str) - offset + 1;
787 fprintf (reply_fd, "%s\n",
788 gw_res_get (info.kernel_res,
791 "greater than ending position"));
798 memcpy (tmp_str, li->name, len);
799 tmp_str [len] = '\0';
800 offset = atoi (tmp_str);
806 else if (li->kind == CCL_TOK_TERM)
809 memcpy (tmp_str, li->name, len);
811 number = atoi (tmp_str);
815 if (offset > 0 && number > 0)
817 struct gw_user_set *us;
821 len = set_token->len;
822 memcpy (tmp_str, set_token->name, len);
824 us = user_set_search (tmp_str);
827 us = user_set_search (NULL);
828 if (us && us->hits != -1) /* proper result-set? */
830 if (offset <= us->hits)
832 if (offset+number-1 > us->hits)
833 number = us->hits - offset+1;
834 present (us->name, offset, number, format_token);
835 info.next_position = offset+number;
838 else if (!no_of_present) /* display error message once! */
840 fprintf (reply_fd, "%s\n",
841 gw_res_get (info.kernel_res, "gw.err.no.set",
842 "No result-set generated"));
847 if (!no_of_present) /* no records shown so far? */
849 struct gw_user_set *us;
852 us = user_set_search (NULL);
853 if (us && us->hits != -1) /* proper result-set? */
855 default_show = gw_res_int (info.kernel_res, "gw.default.show", 20);
856 if (us->hits >= info.next_position + default_show)
858 present (us->name, info.next_position, default_show,
860 info.next_position += default_show;
862 else if (us->hits >= info.next_position)
864 present (us->name, info.next_position,
865 us->hits - info.next_position + 1,
867 info.next_position = us->hits + 1;
870 else /* display error message */
872 fprintf (reply_fd, "%s\n",
873 gw_res_get (info.kernel_res, "gw.err.no.set",
874 "No result-set generated"));
881 static void show_def (void)
883 fprintf (reply_fd, "format %s\n", gw_res_get
884 (info.kernel_res, "gw.display.format", ""));
885 fprintf (reply_fd, "autoshow %s\n", gw_res_get
886 (info.kernel_res, "gw.auto.show", ""));
887 fprintf (reply_fd, "defaultshow %s\n", gw_res_get
888 (info.kernel_res, "gw.default.show", ""));
889 fprintf (reply_fd, "language %s\n", gw_res_get
890 (info.kernel_res, "gw.language", ""));
893 static int exec_def (struct ccl_token *list)
895 const char *name = NULL;
898 static struct command_word def_tab [] =
900 { "reset", "reset" },
903 { "autoshow", "autoshow" },
904 { "defaultshow", "defaultshow" },
905 { "lang", "language" },
911 sprintf (fname, "user.%d.r", info.userid);
913 if (list->kind == CCL_TOK_EOL)
917 int setting_no = command_search (def_tab, list, "ccl.token.");
921 fprintf (reply_fd, "Unknown setting in def\n");
930 else if (setting_no == 2)
936 if (list->kind == CCL_TOK_EOL)
938 strncpy (value, list->name, 127);
939 value[(size_t) list->len] = '\0';
943 name = "gw.display.format";
946 name = "gw.auto.show";
949 name = "gw.default.show";
952 name = "gw.language";
957 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "update file %s with %s=%s",
959 gw_res_put (info.kernel_res, "gw.username", info.from_str, fname);
960 gw_res_put (info.kernel_res, name, value, fname);
961 gw_res_commit (info.kernel_res, fname);
970 * exec_command: parse and execute ccl command in str.
971 * str: ccl command string
972 * stop_flag pointer to integer. On completion the integer
973 * is 1 (stop) or 2 (continue); 0 (other command)
974 * return: 0 success; non-zero otherwise
976 static int exec_command (const char *str, int *stop_flag)
978 static struct command_word command_tab [] =
985 { "continue", "continue" },
986 { "status", "status" },
988 { "target", "target" },
990 { "account", "account" },
994 struct ccl_token *cmd;
995 const char *cp = str;
998 while (*cp && *cp != ' ' && *cp != '\n' && *cp != '\t' && *cp != '\r')
1002 no = command_search_str (command_tab, str, cp-str, "ccl.command.");
1005 if (no == 1 || no == 2 || no == 3) /* find/show/base? */
1007 if (!info.zass) /* open if not already opened */
1009 fprintf (reply_fd, "\n> %s\n", str);
1010 if (!info.zass) /* fail? */
1012 fprintf (reply_fd, "\n%s\n",
1013 gw_res_get (info.kernel_res, "gw.err.no.target",
1014 "No connection established - "
1015 "command ignored"));
1020 fprintf (reply_fd, "\n> %s\n", str);
1024 cmd = ccl_tokenize (cp);
1025 exec_find (cmd, str);
1028 cmd = ccl_tokenize (cp);
1032 cmd = ccl_token_simple (cp);
1036 cmd = ccl_token_simple (cp);
1039 case 6: /* continue */
1042 case 7: /* status */
1048 case 9: /* target */
1049 cmd = ccl_token_simple (cp);
1053 cmd = ccl_token_simple (cp);
1056 case 11: /* account */
1057 cmd = ccl_token_simple (cp);
1062 fprintf (reply_fd, "%s\n",
1063 gw_res_get (info.kernel_res, "gw.err.unimplemented",
1064 "Not implemented yet"));
1066 ccl_token_del (cmd);
1070 fprintf (reply_fd, "\n> %s\n", str);
1071 fprintf (reply_fd, " ^ %s\n",
1072 gw_res_get (info.kernel_res, "gw.err.unknown.command",
1074 "Use help to see list of commands"));
1079 int urp_start (int continuation, struct str_queue *queue)
1081 char subject_str[128];
1083 info.command_no = 0;
1084 info.reply_fname = NULL;
1086 if (email_header (queue, info.from_str, subject_str))
1088 gw_log (GW_LOG_WARN, KERNEL_LOG, "No message body");
1091 tty_init (stdout, 40, 70);
1094 info.reply_fname = (char *)
1095 tempnam (gw_res_get (info.kernel_res, "gw.reply.tmp.dir", NULL),
1096 gw_res_get (info.kernel_res,
1097 "gw.reply.tmp.prefix", "gwr"));
1099 reply_fd = fopen (info.reply_fname, "w");
1102 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, KERNEL_LOG, "Cannot create %s",
1106 tty_init (reply_fd, 0, 0);
1107 fprintf (reply_fd, "From: %s\n",
1108 gw_res_get (info.kernel_res, "gw.msg.from","Email-gateway"));
1109 fprintf (reply_fd, "Subject: ");
1111 fprintf (reply_fd, "Z39.50 Re: %s", subject_str);
1113 fprintf (reply_fd, "%s\n", gw_res_get (info.kernel_res,
1116 fprintf (reply_fd, "\n");
1117 gw_log (GW_LOG_ACCT, KERNEL_LOG, "User start %s", info.from_str);
1120 gw_log (GW_LOG_WARN, KERNEL_LOG, "No From in email header");
1121 fprintf (reply_fd, "%s\n", gw_res_get (info.kernel_res, "gw.msg.greeting",
1122 "Email->Z39.50 gateway"));
1124 fprintf (reply_fd, "%s\n", gw_res_get (info.kernel_res,
1130 int urp_command (struct str_queue *queue)
1135 while (str_queue_deq (queue, line_buf, STR_LINE_MAX))
1137 if (line_buf[0] == '\n')
1138 if (info.command_no)
1140 while (str_queue_deq (queue, 0, 0))
1146 if ((cp = strchr (line_buf, '\n')))
1148 gw_log (GW_LOG_ACCT, KERNEL_LOG, "cmd: %s", line_buf);
1149 if (isalpha (line_buf[0]))
1151 exec_command (line_buf, &stop_flag);
1154 info.command_no++; /* prevent help! */
1155 while (str_queue_deq (queue, line_buf, STR_LINE_MAX))
1167 if (!info.command_no)
1169 fprintf (reply_fd, "%s\n",
1170 gw_res_get (info.kernel_res, "gw.err.nullbody", "No body"));
1179 assert (info.reply_fname);
1183 mta = gw_res_get (info.kernel_res, "gw.reply.mta",
1184 "/usr/lib/sendmail");
1185 sprintf (cmd, "%s %s < %s", mta, info.from_str, info.reply_fname);
1186 mta_code = system (cmd);
1188 gw_log (GW_LOG_FATAL, KERNEL_LOG,
1189 "Reply '%s' got exit code %d", cmd, mta_code);
1191 unlink (info.reply_fname);
1192 gw_log (GW_LOG_ACCT, KERNEL_LOG, "User end %s", info.from_str);