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.46 1995/07/28 10:51:09 adam
49 * Bug fix: account from previous session had effect when new target command
52 * Revision 1.45 1995/07/20 08:20:24 adam
55 * Revision 1.44 1995/07/11 12:28:53 adam
56 * New functions ccl_token_simple and ccl_token_del used.
58 * Revision 1.43 1995/07/11 11:49:13 adam
59 * LINE_MAX renamed to STR_LINE_MAX.
61 * Revision 1.42 1995/07/03 12:59:29 adam
62 * New option for eti: -c dir to chdir before start.
63 * Setting change: gw.max.show defaults to 100.
65 * Revision 1.41 1995/07/03 08:20:18 adam
66 * More help information and better diagnostics.
68 * Revision 1.40 1995/05/23 08:13:00 adam
71 * Revision 1.39 1995/05/22 14:57:30 adam
72 * Next default record position also set by last p=... in show.
74 * Revision 1.38 1995/05/19 13:26:00 adam
75 * Bug fixes. Better command line options.
77 * Revision 1.37 1995/05/18 12:03:09 adam
78 * Bug fixes and minor improvements.
80 * Revision 1.36 1995/05/17 10:51:33 adam
81 * Added a few more error checks to the show command.
83 * Revision 1.35 1995/05/16 09:40:43 adam
84 * LICENSE. Setting of CCL token names (and/or/not/set) in read_kernel_res.
86 * Revision 1.34 1995/05/04 10:40:08 adam
87 * More work on Def-settings.
89 * Revision 1.33 1995/05/03 16:34:19 adam
90 * CCL def command, i.e. user definitions - saved as resource files.
92 * Revision 1.32 1995/05/03 07:37:46 adam
93 * CCL commands stop/continue implemented. New functions gw_res_{int,bool}
94 * are used when possible.
96 * Revision 1.31 1995/05/01 12:43:38 adam
97 * First work on resource monitor program.
99 * Revision 1.30 1995/04/20 16:10:47 adam
100 * Modified to work with non-blocking zass-api. Not using non-blocking
103 * Revision 1.29 1995/04/19 16:01:58 adam
104 * Some hacks to get the FIFO communication work!! Isn't reliable.
105 * Resource gw.account added - default account info.
107 * Revision 1.28 1995/04/19 13:19:09 adam
108 * New command: account - for authentication.
110 * Revision 1.27 1995/04/19 10:46:19 adam
111 * Persistency works much better now. New command: status - history-like
113 * Revision 1.26 1995/04/19 07:31:12 adam
114 * First work on Z39.50 persistence.
116 * Revision 1.25 1995/04/17 09:34:33 adam
117 * Timeout (idletime) adjustable. Minor changes in kernel.
119 * Revision 1.24 1995/03/28 11:42:35 adam
120 * First use of string-queue utility.
122 * Revision 1.23 1995/03/28 08:01:28 adam
123 * FIFO existence is used to test for a running kernel.
125 * Revision 1.22 1995/03/27 12:51:05 adam
126 * New log level in use: GW_LOG_ERRNO.
128 * Revision 1.21 1995/03/27 08:24:04 adam
129 * First use of gip interface and gw-db.
130 * First work on eti program.
132 * Revision 1.20 1995/03/03 17:19:17 adam
133 * Smarter presentation. Bug fix in email header interpretation.
135 * Revision 1.19 1995/03/02 09:32:11 adam
136 * New presentation formats. f0=full, f1=brief, f2=mid
138 * Revision 1.18 1995/03/01 14:32:26 adam
139 * Better diagnostics. Default is, that only one database selected when
142 * Revision 1.17 1995/02/28 13:16:26 adam
143 * Configurable From: added.
145 * Revision 1.16 1995/02/23 10:08:20 adam
146 * Added logging of all user commands.
148 * Revision 1.15 1995/02/23 08:32:17 adam
151 * Revision 1.13 1995/02/22 16:54:42 adam
152 * Qualifiers of LOC target updated. More logging messages.
154 * Revision 1.12 1995/02/22 15:51:51 adam
155 * Bug fix: swap of parameter number and offset in function present.
157 * Revision 1.11 1995/02/22 15:22:33 adam
158 * Much more checking of run-time state. Show command never retrieves
159 * more records than indicated by the previous search request. Help
160 * command available. The maximum number of records retrieved can be
163 * Revision 1.10 1995/02/22 08:51:35 adam
164 * Output function can be customized in fml, which is used to print
165 * the reply to reply_fd.
167 * Revision 1.9 1995/02/21 17:46:21 adam
170 * Revision 1.8 1995/02/21 12:12:00 adam
171 * Diagnostic record with error info. observed.
173 * Revision 1.7 1995/02/20 21:16:20 adam
174 * FML support. Bug fixes. Profile for drewdb.
176 * Revision 1.6 1995/02/17 14:41:14 quinn
177 * Added simple display of records.
179 * Revision 1.5 1995/02/17 14:22:13 adam
180 * First steps of CCL show command. Not finished yet.
182 * Revision 1.4 1995/02/17 09:08:36 adam
183 * Reply with subject. CCL base command implemented.
185 * Revision 1.3 1995/02/16 18:35:09 adam
186 * First use of Zdist library. Search requests are supported.
187 * Present requests are not supported yet.
189 * Revision 1.2 1995/02/16 13:21:00 adam
190 * Organization of resource files for targets and conversion
191 * language implemented.
193 * Revision 1.1 1995/02/15 17:45:30 adam
194 * First version of email gateway kernel. Email requests are read
195 * from stdin. The output is transferred to an MTA if 'From' is
196 * found in the header - or stdout if absent. No Z39.50 client is used.
202 info/status (other name?)
203 better persistence diagnostics
206 automatic information about target-aliases: name, query-support, etc.
219 #include <strqueue.h>
222 static void present (const char *set, int offset, int number,
223 struct ccl_token *format_token);
225 static char line_buf[STR_LINE_MAX+1];
227 static void put_esc_str (const char *s)
233 int reopen_target (void)
237 gw_log (GW_LOG_WARN, KERNEL_LOG, "Zass free...");
238 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "reopen_target");
239 info.zass = zass_open (info.hostname, info.port, NULL, /* complete */
240 *info.account ? info.account : NULL);
243 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Cannot connect to target %s:%d",
244 info.hostname, info.port);
245 fprintf (reply_fd, "%s %s:%d\n",
246 gw_res_get (info.kernel_res, "gw.err.connect",
247 "Cannot connect to target"),
248 info.hostname, info.port);
251 v = gw_res_get (info.kernel_res, "gw.description", NULL);
255 fprintf (reply_fd, "\n");
257 fprintf (reply_fd, "%s %s:%d\n",
258 gw_res_get (info.kernel_res, "gw.msg.connect",
259 "Connection established to"),
260 info.hostname, info.port);
262 fprintf (reply_fd, "%s: %s\n",
263 gw_res_get (info.kernel_res, "gw.msg.databases",
264 "Available databases"),
267 fprintf (reply_fd, "%s: %s\n",
268 gw_res_get (info.kernel_res, "gw.msg.database",
269 "Selected databases"),
272 fprintf (reply_fd, "set=%d\n", info.setno);
274 fprintf (reply_fd, "set=Default\n");
278 struct command_word {
280 char *resource_suffix;
283 static int command_search_str (struct command_word *tab, const char *cmd_name,
284 int cmd_len, const char *resource_prefix)
288 assert (resource_prefix);
290 while (tab->default_value)
292 char *cp, command_names[60];
293 char resource_name[60];
296 sprintf (resource_name, "%s%s", resource_prefix,
297 tab->resource_suffix);
298 v = gw_res_get (info.kernel_res, resource_name, tab->default_value);
300 strcpy (command_names, v);
306 if ((split = strchr (cp, ' ')))
308 if (cmd_len == strlen(cp) &&
309 !memcmp (cmd_name, cp, cmd_len))
321 static int command_search (struct command_word *tab, struct ccl_token *cmd,
322 const char *resource_prefix)
324 return command_search_str (tab, cmd->name, cmd->len, resource_prefix);
327 static struct error_no_struct {
330 } error_ccl_tab[] = {
332 { CCL_ERR_TERM_EXPECTED, "term.expected" },
333 { CCL_ERR_RP_EXPECTED, "rp.expected" },
334 { CCL_ERR_SETNAME_EXPECTED, "setname.expected" },
335 { CCL_ERR_OP_EXPECTED, "op.expected" },
336 { CCL_ERR_BAD_RP, "bad.rp" },
337 { CCL_ERR_UNKNOWN_QUAL, "unknown.qual" },
338 { CCL_ERR_DOUBLE_QUAL, "double.qual" },
339 { CCL_ERR_EQ_EXPECTED, "eq.expected" },
340 { CCL_ERR_BAD_RELATION, "bad.relation" },
341 { CCL_ERR_TRUNC_NOT_LEFT, "trunc.not.left" },
342 { CCL_ERR_TRUNC_NOT_BOTH, "trunc.not.both" },
343 { CCL_ERR_TRUNC_NOT_RIGHT, "trunc.not.right" },
347 static char *error_no_search (struct error_no_struct *tab, int no)
349 struct error_no_struct *p = tab;
350 while (p->resource_name)
353 return p->resource_name;
359 static int email_header (struct str_queue *sq,
360 char *from_str, char *subject_str)
364 while (str_queue_deq (sq, line_buf, STR_LINE_MAX))
366 if (line_buf[0] == '\n')
368 if (memcmp (line_buf, "From ", 5) == 0)
369 sscanf (line_buf+4, "%s", from_str);
370 if (memcmp (line_buf, "Subject: ", 9) == 0 &&
371 sscanf (line_buf+9, "%s", subject_str+1) == 1)
372 strcpy (subject_str, line_buf+9);
377 static void help_general (void)
379 put_esc_str (gw_res_get (info.kernel_res, "gw.help.general",
380 "Commands available in this service:\n"));
383 static int exec_help (struct ccl_token *list)
385 static char *sep = "-------------------------------\\n";
389 put_esc_str (gw_res_get (info.kernel_res, "gw.help.target",
390 "target <name> - selects a given target\n"));
393 put_esc_str (gw_res_get (info.kernel_res, "gw.help.base",
394 "base <base>.. - selects databases\n"));
397 put_esc_str (gw_res_get (info.kernel_res, "gw.help.find",
398 "find <query> - performs a search request\n"));
401 put_esc_str (gw_res_get (info.kernel_res, "gw.help.show",
402 "show <spec> - retrieves and displays "
405 put_esc_str (gw_res_get (info.kernel_res, "gw.help.def",
406 "def <setting> <value> sets/gets definitons\n"));
408 put_esc_str (gw_res_get (info.kernel_res, "gw.help.account",
409 "account <str> sets authentication string\n"));
413 static void display_diag_error (int code, const char *addinfo)
417 sprintf (str, "gw.bib1.diag.%d", code);
418 fprintf (reply_fd, "%s %d:\n %s: '%s'\n",
419 gw_res_get (info.kernel_res, "gw.msg.z39errcode",
422 gw_res_get (info.kernel_res, str, ""), addinfo);
425 static int exec_find (struct ccl_token *list, const char *search_str)
427 const struct zass_searchent *p;
428 struct gw_user_set *us;
432 struct ccl_rpn_node *rpn;
436 if (info.setno == -1)
437 strcpy (setname, "Default");
439 sprintf (setname, "%d", info.setno);
440 info.next_position = 1;
443 rpn = ccl_find (info.bibset, list, &error, &pos);
446 const char *v = NULL, *n;
449 fprintf (reply_fd, " %*s^ - ", pos - line_buf, " ");
451 n = error_no_search (error_ccl_tab, error);
454 sprintf (name, "gw.err.%s", n);
455 v = gw_res_get (info.kernel_res, name, NULL);
458 v = ccl_err_msg (error);
459 fprintf (reply_fd, "%s\n", v);
462 ccl_pr_tree (rpn, reply_fd);
463 fprintf (reply_fd, "\n");
465 if (!*info.database )
467 fprintf (reply_fd, "%s\n",
468 gw_res_get (info.kernel_res, "gw.err.no.database",
469 "You must select database"));
472 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Searching in database %s",
475 p = zass_p_search (info.zass, rpn, setname, info.database, info.sets);
478 fprintf (reply_fd, "%s\n",
479 gw_res_get (info.kernel_res, "gw.err.search.fail",
483 if (p->errcode != -1)
485 display_diag_error (p->errcode, p->errstring);
488 fprintf (reply_fd, "%d %s %s\n", p->num,
489 gw_res_get (info.kernel_res, "gw.msg.hits", "hit(s) in set "),
491 us = user_set_add (setname, p->num, info.database, rpn, 1, search_str);
492 autoshow = gw_res_int (info.kernel_res, "gw.auto.show", 0);
493 if (autoshow && p->num > 0)
495 if (autoshow > p->num)
497 present (setname, 1, p->num, NULL);
498 info.next_position = 1+p->num;
502 present (setname, 1, autoshow, NULL);
503 info.next_position = 1+autoshow;
509 static int exec_account (struct ccl_token *list)
511 if (list->kind != CCL_TOK_EOL)
514 memcpy (info.account, list->name, len);
515 info.target[len] = '\0';
518 *info.account = '\0';
519 info.account_in_session = 1;
523 static int exec_target (struct ccl_token *list)
526 if (list->kind == CCL_TOK_EOL)
529 memcpy (info.target, list->name, len);
530 info.target [len] = '\0';
532 if (!info.account_in_session)
533 *info.account = '\0';
534 info.account_in_session = 0;
536 return reopen_target ();
539 static void exec_status_r (struct gw_user_set *sp)
543 exec_status_r (sp->prev);
544 fprintf (reply_fd, "%6s %7d %12.12s %.50s\n", sp->name, sp->hits,
545 sp->database, sp->search_str);
548 static int exec_status (struct ccl_token *list)
550 fprintf (reply_fd, "%s\n",
551 gw_res_get (info.kernel_res,
553 " Name Hits Database Find"));
554 exec_status_r (info.sets);
558 static int exec_base (struct ccl_token *list)
560 struct ccl_token *li = list;
564 if (list->kind == CCL_TOK_EOL)
566 free (info.database);
567 while (li->kind != CCL_TOK_EOL)
571 if (li->kind == CCL_TOK_COMMA)
574 info.database = malloc (len);
575 assert (info.database );
578 while (li->kind != CCL_TOK_EOL)
580 memcpy (info.database+len, li->name, li->len);
582 info.database[len++] = ',';
584 if (li->kind == CCL_TOK_COMMA)
587 info.database[len-1] = '\0';
591 static void present (const char *set, int offset, int number,
592 struct ccl_token *format_token)
594 const struct zass_presentent *zp;
599 max_number = gw_res_int (info.kernel_res, "gw.max.show", 100);
600 if (number > max_number)
602 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "present in set %s", set);
603 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "present of %d records from offset %d",
605 zp = zass_p_present(info.zass, (char *) set, offset, number);
610 int record_log_fd = -1;
611 const char *record_log_name;
613 record_log_name = gw_res_get (info.kernel_res, "gw.marc.log",
617 record_log_fd = open (record_log_name,
618 O_WRONLY|O_CREAT|O_APPEND, 0666);
619 if (record_log_fd == -1)
620 gw_log (GW_LOG_WARN|GW_LOG_ERRNO, "Cannot open %s",
623 fprintf (reply_fd, gw_res_get (info.kernel_res,
625 "Got %d records from set %s"),
627 fprintf (reply_fd, "\n");
628 for (i = 0, pp = zp->records; pp; pp = pp->next, i++)
632 const char *arg_ar[5];
634 fprintf (reply_fd, "--- %d/%d ---\n",
635 i+offset, offset+zp->num-1);
636 if (!gw_res_get (info.kernel_res, "gw.ignore.which", NULL))
638 if (pp->which == ZASS_REC_DIAG)
640 display_diag_error (pp->errcode, pp->errstring);
643 else if (pp->which != ZASS_REC_USMARC)
645 fprintf (reply_fd, "Unknown record kind %d\n",
650 if (record_log_fd != -1)
651 write (record_log_fd, pp->record, strlen(pp->record));
652 rec = iso2709_cvt (pp->record);
657 gw_res_get (info.kernel_res,
658 "gw.display.format", ""));
661 len = format_token->len;
662 if (len >= sizeof(format_str))
663 len = sizeof(format_str)-1;
664 memcpy (format_str, format_token->name, len);
665 format_str[len] = '\0';
667 if (info.fml && *format_str &&
668 (!strcmp (format_str, "0") || !strcmp (format_str, "1")
669 || !strcmp(format_str, "2")))
672 arg_ar[1] = format_str;
673 arg_ar[2] = " \\list";
674 arg_ar[3] = marc_to_str (info.fml, rec);
676 fml_exec_call_argv (info.fml, arg_ar);
679 iso2709_display (rec, reply_fd);
681 iso2709_display (rec, reply_fd);
686 fprintf (reply_fd, "Not a MARC record\n");
688 if (record_log_fd != -1)
689 close (record_log_fd);
693 static int exec_show (struct ccl_token *list)
695 static struct command_word show_tab [] =
702 struct ccl_token *set_token = NULL;
703 struct ccl_token *format_token = NULL;
704 struct ccl_token *li = list;
705 int no_of_present = 0;
708 while (li->kind != CCL_TOK_EOL)
711 if (li->next->kind == CCL_TOK_EQ)
713 if (li->kind == CCL_TOK_SET) /* set = <name> ? */
720 modifier_no = command_search (show_tab, li, "ccl.token.");
723 fprintf (reply_fd, "Unknown modifier in show\n");
727 if (modifier_no == 1) /* f = <name> ? */
729 else if (modifier_no == 2) /* p = <name> ? */
731 if (li->kind != CCL_TOK_EOL /* p = <name> - <name> ? */
732 && li->next->kind == CCL_TOK_MINUS
733 && li->next->next != CCL_TOK_EOL)
739 fprintf (reply_fd, "%s\n", "Missing token after '='");
747 memcpy (tmp_str, li->name, len);
749 if (atoi(tmp_str) <= 0)
751 fprintf (reply_fd, "%s\n",
752 gw_res_get (info.kernel_res, "gw.err.bad.show",
760 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Got set=%.*s", set_token->len,
763 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Got format=%.*s", format_token->len,
767 while (li->kind != CCL_TOK_EOL)
773 if (li->next->kind == CCL_TOK_EQ && li->kind != CCL_TOK_SET)
775 modifier_no = command_search (show_tab, li, "ccl.token.");
777 if (modifier_no == 2) /* p = <name> ? */
779 if (li->kind != CCL_TOK_EOL /* p = <name> - <name> ? */
780 && li->next->kind == CCL_TOK_MINUS
781 && li->next->next != CCL_TOK_EOL)
784 memcpy (tmp_str, li->name, len);
785 tmp_str [len] = '\0';
786 offset = atoi (tmp_str);
790 memcpy (tmp_str, li->name, len);
791 tmp_str [len] = '\0';
792 number = atoi (tmp_str) - offset + 1;
795 fprintf (reply_fd, "%s\n",
796 gw_res_get (info.kernel_res,
799 "greater than ending position"));
806 memcpy (tmp_str, li->name, len);
807 tmp_str [len] = '\0';
808 offset = atoi (tmp_str);
814 else if (li->kind == CCL_TOK_TERM)
817 memcpy (tmp_str, li->name, len);
819 number = atoi (tmp_str);
823 if (offset > 0 && number > 0)
825 struct gw_user_set *us;
829 len = set_token->len;
830 memcpy (tmp_str, set_token->name, len);
832 us = user_set_search (tmp_str);
835 us = user_set_search (NULL);
836 if (us && us->hits != -1) /* proper result-set? */
838 if (offset <= us->hits)
840 if (offset+number-1 > us->hits)
841 number = us->hits - offset+1;
842 present (us->name, offset, number, format_token);
843 info.next_position = offset+number;
846 else if (!no_of_present) /* display error message once! */
848 fprintf (reply_fd, "%s\n",
849 gw_res_get (info.kernel_res, "gw.err.no.set",
850 "No result-set generated"));
855 if (!no_of_present) /* no records shown so far? */
857 struct gw_user_set *us;
860 us = user_set_search (NULL);
861 if (us && us->hits != -1) /* proper result-set? */
863 default_show = gw_res_int (info.kernel_res, "gw.default.show", 20);
864 if (us->hits >= info.next_position + default_show)
866 present (us->name, info.next_position, default_show,
868 info.next_position += default_show;
870 else if (us->hits >= info.next_position)
872 present (us->name, info.next_position,
873 us->hits - info.next_position + 1,
875 info.next_position = us->hits + 1;
878 else /* display error message */
880 fprintf (reply_fd, "%s\n",
881 gw_res_get (info.kernel_res, "gw.err.no.set",
882 "No result-set generated"));
889 static void show_def (void)
891 fprintf (reply_fd, "format %s\n", gw_res_get
892 (info.kernel_res, "gw.display.format", ""));
893 fprintf (reply_fd, "autoshow %s\n", gw_res_get
894 (info.kernel_res, "gw.auto.show", ""));
895 fprintf (reply_fd, "defaultshow %s\n", gw_res_get
896 (info.kernel_res, "gw.default.show", ""));
897 fprintf (reply_fd, "language %s\n", gw_res_get
898 (info.kernel_res, "gw.language", ""));
901 static int exec_def (struct ccl_token *list)
903 const char *name = NULL;
906 static struct command_word def_tab [] =
908 { "reset", "reset" },
911 { "autoshow", "autoshow" },
912 { "defaultshow", "defaultshow" },
913 { "lang", "language" },
919 sprintf (fname, "user.%d.r", info.userid);
921 if (list->kind == CCL_TOK_EOL)
925 int setting_no = command_search (def_tab, list, "ccl.token.");
929 fprintf (reply_fd, "Unknown setting in def\n");
938 else if (setting_no == 2)
944 if (list->kind == CCL_TOK_EOL)
946 strncpy (value, list->name, 127);
947 value[(size_t) list->len] = '\0';
951 name = "gw.display.format";
954 name = "gw.auto.show";
957 name = "gw.default.show";
960 name = "gw.language";
965 gw_log (GW_LOG_DEBUG, KERNEL_LOG, "update file %s with %s=%s",
967 gw_res_put (info.kernel_res, "gw.username", info.from_str, fname);
968 gw_res_put (info.kernel_res, name, value, fname);
969 gw_res_commit (info.kernel_res, fname);
978 * exec_command: parse and execute ccl command in str.
979 * str: ccl command string
980 * stop_flag pointer to integer. On completion the integer
981 * is 1 (stop) or 2 (continue); 0 (other command)
982 * return: 0 success; non-zero otherwise
984 static int exec_command (const char *str, int *stop_flag)
986 static struct command_word command_tab [] =
993 { "continue", "continue" },
994 { "status", "status" },
996 { "target", "target" },
998 { "account", "account" },
1002 struct ccl_token *cmd;
1003 const char *cp = str;
1006 while (*cp && *cp != ' ' && *cp != '\n' && *cp != '\t' && *cp != '\r')
1010 no = command_search_str (command_tab, str, cp-str, "ccl.command.");
1013 if (no == 1 || no == 2 || no == 3) /* find/show/base? */
1015 if (!info.zass) /* open if not already opened */
1017 fprintf (reply_fd, "\n> %s\n", str);
1018 if (!info.zass) /* fail? */
1020 fprintf (reply_fd, "\n%s\n",
1021 gw_res_get (info.kernel_res, "gw.err.no.target",
1022 "No connection established - "
1023 "command ignored"));
1028 fprintf (reply_fd, "\n> %s\n", str);
1032 cmd = ccl_tokenize (cp);
1033 exec_find (cmd, str);
1036 cmd = ccl_tokenize (cp);
1040 cmd = ccl_token_simple (cp);
1044 cmd = ccl_token_simple (cp);
1047 case 6: /* continue */
1050 case 7: /* status */
1056 case 9: /* target */
1057 cmd = ccl_token_simple (cp);
1061 cmd = ccl_token_simple (cp);
1064 case 11: /* account */
1065 cmd = ccl_token_simple (cp);
1070 fprintf (reply_fd, "%s\n",
1071 gw_res_get (info.kernel_res, "gw.err.unimplemented",
1072 "Not implemented yet"));
1074 ccl_token_del (cmd);
1078 fprintf (reply_fd, "\n> %s\n", str);
1079 fprintf (reply_fd, " ^ %s\n",
1080 gw_res_get (info.kernel_res, "gw.err.unknown.command",
1082 "Use help to see list of commands"));
1087 int urp_start (int continuation, struct str_queue *queue)
1089 char subject_str[128];
1091 info.command_no = 0;
1092 info.reply_fname = NULL;
1094 if (email_header (queue, info.from_str, subject_str))
1096 gw_log (GW_LOG_WARN, KERNEL_LOG, "No message body");
1099 tty_init (stdout, 40, 70);
1102 info.reply_fname = (char *)
1103 tempnam (gw_res_get (info.kernel_res, "gw.reply.tmp.dir", NULL),
1104 gw_res_get (info.kernel_res,
1105 "gw.reply.tmp.prefix", "gwr"));
1107 reply_fd = fopen (info.reply_fname, "w");
1110 gw_log (GW_LOG_FATAL|GW_LOG_ERRNO, KERNEL_LOG, "Cannot create %s",
1114 tty_init (reply_fd, 0, 0);
1115 fprintf (reply_fd, "From: %s\n",
1116 gw_res_get (info.kernel_res, "gw.msg.from","Email-gateway"));
1117 fprintf (reply_fd, "Subject: ");
1119 fprintf (reply_fd, "Z39.50 Re: %s", subject_str);
1121 fprintf (reply_fd, "%s\n", gw_res_get (info.kernel_res,
1124 fprintf (reply_fd, "\n");
1125 gw_log (GW_LOG_ACCT, KERNEL_LOG, "User start %s", info.from_str);
1128 gw_log (GW_LOG_WARN, KERNEL_LOG, "No From in email header");
1129 fprintf (reply_fd, "%s\n", gw_res_get (info.kernel_res, "gw.msg.greeting",
1130 "Email->Z39.50 gateway"));
1132 fprintf (reply_fd, "%s\n", gw_res_get (info.kernel_res,
1138 int urp_command (struct str_queue *queue)
1143 while (str_queue_deq (queue, line_buf, STR_LINE_MAX))
1145 if (line_buf[0] == '\n')
1146 if (info.command_no)
1148 while (str_queue_deq (queue, 0, 0))
1154 if ((cp = strchr (line_buf, '\n')))
1156 gw_log (GW_LOG_ACCT, KERNEL_LOG, "cmd: %s", line_buf);
1157 if (isalpha (line_buf[0]))
1159 exec_command (line_buf, &stop_flag);
1162 info.command_no++; /* prevent help! */
1163 while (str_queue_deq (queue, line_buf, STR_LINE_MAX))
1175 if (!info.command_no)
1177 fprintf (reply_fd, "%s\n",
1178 gw_res_get (info.kernel_res, "gw.err.nullbody", "No body"));
1187 assert (info.reply_fname);
1191 mta = gw_res_get (info.kernel_res, "gw.reply.mta",
1192 "/usr/lib/sendmail");
1193 sprintf (cmd, "%s %s < %s", mta, info.from_str, info.reply_fname);
1194 mta_code = system (cmd);
1196 gw_log (GW_LOG_FATAL, KERNEL_LOG,
1197 "Reply '%s' got exit code %d", cmd, mta_code);
1199 unlink (info.reply_fname);
1200 gw_log (GW_LOG_ACCT, KERNEL_LOG, "User end %s", info.from_str);