2 * IR toolkit for tcl/tk
3 * (c) Index Data 1995-2001
4 * See the file LICENSE for details.
7 * Revision 1.117 2001-03-26 11:39:34 adam
8 * Fixed bug in ir_deleteDiags - crash when receiving multiple diags.
10 * Revision 1.116 2001/02/09 11:58:04 adam
11 * Updated for Tcl8.1 and higher where internal encoding is UTF-8.
13 * Revision 1.115 2000/09/13 12:18:49 adam
14 * Logging utility patch (YAZ version 1.7).
16 * Revision 1.114 1999/05/17 20:37:41 adam
17 * Fixed problem with ASN code.
19 * Revision 1.113 1999/04/20 10:01:46 adam
20 * Modified calls to ODR encoders/decoders (name argument).
22 * Revision 1.112 1999/03/22 06:51:34 adam
25 * Revision 1.111 1999/02/11 11:30:09 adam
28 * Revision 1.110 1998/10/20 15:15:31 adam
29 * Changed scan response handler.
31 * Revision 1.109 1998/10/13 21:23:26 adam
32 * Fixed searchStatus method.
34 * Revision 1.108 1998/10/12 11:48:08 adam
35 * Removed printf call.
37 * Revision 1.107 1998/06/10 13:00:46 adam
38 * Added ir-version command.
40 * Revision 1.106 1998/05/20 12:25:35 adam
41 * Fixed bug that occurred in rare cases when encoding of incoming
44 * Revision 1.105 1998/04/02 14:31:08 adam
45 * This version works with compiled ASN.1 code.
47 * Revision 1.104 1998/02/27 14:26:07 adam
48 * Changed client so that it still works if target sets numberOfRecords
49 * in response to an illegal value.
51 * Revision 1.103 1997/11/19 11:22:10 adam
52 * Object identifiers can be accessed in GRS-1 records.
54 * Revision 1.102 1997/09/17 12:22:40 adam
55 * Changed to use YAZ version 1.4. The new comstack utility, cs_straddr,
58 * Revision 1.101 1997/09/09 10:19:53 adam
59 * New MSV5.0 port with fewer warnings.
61 * Revision 1.100 1997/05/01 15:04:05 adam
62 * Added ir-log command.
64 * Revision 1.99 1997/04/30 07:24:47 adam
65 * Spell fix of an error message.
67 * Revision 1.98 1997/04/13 18:57:20 adam
68 * Better error reporting and aligned with Tcl/Tk style.
69 * Rework of notifier code with Tcl_File handles.
71 * Revision 1.97 1996/11/14 17:11:07 adam
72 * Added Explain documentaion.
74 * Revision 1.96 1996/10/08 13:02:50 adam
75 * When dealing with records, odr_choice_enable_bias function is used to
76 * prevent decoding of externals.
78 * Revision 1.95 1996/09/13 10:51:49 adam
79 * Bug fix: ir_tcl_select_set called Tcl_GetFile at disconnect.
81 * Revision 1.94 1996/08/21 13:32:53 adam
82 * Implemented saveFile method and extended loadFile method to work with it.
84 * Revision 1.93 1996/08/16 15:07:45 adam
85 * First work on Explain.
87 * Revision 1.92 1996/08/09 15:33:07 adam
88 * Modified the code to use tk4.1/tcl7.5 patch level 1. The time-driven
89 * polling is no longer activated on Windows since asynchrounous I/O works
92 * Revision 1.91 1996/07/03 13:31:11 adam
93 * The xmalloc/xfree functions from YAZ are used to manage memory.
95 * Revision 1.90 1996/06/27 14:21:00 adam
96 * Yet another Windows port.
98 * Revision 1.89 1996/06/11 15:27:15 adam
99 * Event type set to connect a little earlier in the do_connect function.
101 * Revision 1.88 1996/06/03 09:04:22 adam
102 * Changed a few logf calls.
104 * Revision 1.87 1996/05/29 06:37:51 adam
105 * Function ir_tcl_get_grs_r enhanced so that specific elements can be
108 * Revision 1.86 1996/03/20 13:54:04 adam
109 * The Tcl_File structure is only manipulated in the Tk-event interface
112 * Revision 1.85 1996/03/15 11:15:48 adam
113 * Modified to use new prototypes for p_query_rpn and p_query_scan.
115 * Revision 1.84 1996/03/07 12:42:49 adam
116 * Better logging when callback is invoked.
118 * Revision 1.83 1996/03/05 09:21:09 adam
119 * Bug fix: memory used by GRS records wasn't freed.
120 * Rewrote some of the error handling code - the connection is always
121 * closed before failback is called.
122 * If failback is defined the send APDU methods (init, search, ...) will
123 * return OK but invoke failback (as is the case if the write operation
125 * Bug fix: ref_count in assoc object could grow if fraction of PDU was
128 * Revision 1.82 1996/02/29 15:30:21 adam
129 * Export of IrTcl functionality to extensions.
131 * Revision 1.81 1996/02/26 18:38:32 adam
132 * Work on export of set methods.
134 * Revision 1.80 1996/02/23 17:31:39 adam
135 * More functions made available to the wais tcl extension.
137 * Revision 1.79 1996/02/23 13:41:38 adam
138 * Work on public access to simple ir class system.
140 * Revision 1.78 1996/02/21 10:16:08 adam
141 * Simplified select handling. Only one function ir_tcl_select_set has
142 * to be externally defined.
144 * Revision 1.77 1996/02/20 17:52:58 adam
145 * Uses the YAZ oid system to name record syntax object identifiers.
147 * Revision 1.76 1996/02/20 16:09:51 adam
148 * Bug fix: didn't set element set names stamp correctly on result
149 * set records when element set names were set to the empty string.
151 * Revision 1.75 1996/02/19 15:41:53 adam
152 * Better log messages.
153 * Minor improvement of connect method.
155 * Revision 1.74 1996/02/05 17:58:03 adam
156 * Ported ir-tcl to use the beta releases of tcl7.5/tk4.1.
158 * Revision 1.73 1996/01/29 11:35:19 adam
159 * Bug fix: cs_type member renamed to comstackType to avoid conflict with
160 * cs_type macro defined by YAZ.
162 * Revision 1.72 1996/01/19 17:45:34 quinn
163 * Added debugging output
165 * Revision 1.71 1996/01/19 16:22:38 adam
166 * New method: apduDump - returns information about last incoming APDU.
168 * Revision 1.70 1996/01/10 09:18:34 adam
169 * PDU specific callbacks implemented: initRespnse, searchResponse,
170 * presentResponse and scanResponse.
171 * Bug fix in the command line shell (tclmain.c) - discovered on OSF/1.
173 * Revision 1.69 1996/01/04 16:12:12 adam
174 * Setting PDUType renamed to eventType.
176 * Revision 1.68 1996/01/04 11:05:22 adam
177 * New setting: PDUType - returns type of last PDU returned from the target.
178 * Fixed a bug in configure/Makefile.
180 * Revision 1.67 1996/01/03 09:00:51 adam
181 * Updated to use new version of Yaz (names changed to avoid C++ conflict).
183 * Revision 1.66 1995/11/28 17:26:39 adam
184 * Removed Carriage return from ir-tcl.c!
185 * Removed misc. debug logs.
187 * Revision 1.65 1995/11/28 13:53:00 quinn
190 * Revision 1.64 1995/11/13 15:39:18 adam
191 * Bug fix: {small,medium}SetElementSetNames weren't set correctly.
192 * Bug fix: idAuthentication weren't set correctly.
194 * Revision 1.63 1995/11/13 09:55:39 adam
195 * Multiple records at a position in a result-set with differnt
198 * Revision 1.62 1995/10/18 17:20:33 adam
199 * Work on target setup in client.tcl.
201 * Revision 1.61 1995/10/18 16:42:42 adam
202 * New settings: smallSetElementSetNames and mediumSetElementSetNames.
204 * Revision 1.60 1995/10/18 15:43:31 adam
205 * In search: mediumSetElementSetNames and smallSetElementSetNames are
206 * set to elementSetNames.
208 * Revision 1.59 1995/10/17 12:18:58 adam
209 * Bug fix: when target connection closed, the connection was not
210 * properly reestablished.
212 * Revision 1.58 1995/10/16 17:00:55 adam
213 * New setting: elementSetNames.
214 * Various client improvements. Medium presentation format looks better.
216 * Revision 1.57 1995/09/21 13:11:51 adam
217 * Support of dynamic loading.
218 * Test script uses load command if necessary.
220 * Revision 1.56 1995/08/29 15:30:14 adam
221 * Work on GRS records.
223 * Revision 1.55 1995/08/28 09:43:25 adam
224 * Minor changes. configure only searches for yaz beta 3 and versions after
227 * Revision 1.54 1995/08/24 12:25:16 adam
228 * Modified to work with yaz 1.0b3.
230 * Revision 1.53 1995/08/04 12:49:26 adam
231 * Bug fix: reading uninitialized variable p.
233 * Revision 1.52 1995/08/04 11:32:38 adam
234 * More work on output queue. Memory related routines moved
237 * Revision 1.51 1995/08/03 13:22:54 adam
240 * Revision 1.50 1995/07/20 08:09:49 adam
241 * client.tcl: Targets removed from hotTargets list when targets
242 * are removed/modified.
243 * ir-tcl.c: More work on triggerResourceControl.
245 * Revision 1.49 1995/06/30 12:39:21 adam
246 * Bug fix: loadFile didn't set record type.
247 * The MARC routines are a little less strict in the interpretation.
248 * Script display.tcl replaces the old marc.tcl.
249 * New interactive script: shell.tcl.
251 * Revision 1.48 1995/06/27 19:03:50 adam
252 * Bug fix in do_present in ir-tcl.c: p->set_child member weren't set.
253 * nextResultSetPosition used instead of setOffset.
255 * Revision 1.47 1995/06/25 10:25:04 adam
256 * Working on triggerResourceControl. Description of compile/install
257 * procedure moved to ir-tcl.sgml.
259 * Revision 1.46 1995/06/22 13:15:06 adam
260 * Feature: SUTRS. Setting getSutrs implemented.
261 * Work on display formats.
262 * Preferred record syntax can be set by the user.
264 * Revision 1.45 1995/06/20 08:07:30 adam
265 * New setting: failInfo.
266 * Working on better cancel mechanism.
268 * Revision 1.44 1995/06/19 17:01:20 adam
271 * Revision 1.43 1995/06/19 13:06:08 adam
272 * New define: IR_TCL_VERSION.
274 * Revision 1.42 1995/06/19 08:08:52 adam
275 * client.tcl: hotTargets now contain both database and target name.
276 * ir-tcl.c: setting protocol edited. Errors in callbacks are logged
277 * by logf(LOG_WARN, ...) calls.
279 * Revision 1.41 1995/06/16 12:28:16 adam
280 * Implemented preferredRecordSyntax.
281 * Minor changes in diagnostic handling.
282 * Record list deleted when connection closes.
284 * Revision 1.40 1995/06/14 13:37:18 adam
285 * Setting recordType implemented.
286 * Setting implementationVersion implemented.
287 * Settings implementationId / implementationName edited.
289 * Revision 1.39 1995/06/08 10:26:32 adam
290 * Bug fix in ir_strdup.
292 * Revision 1.38 1995/06/01 16:36:47 adam
293 * About buttons. Minor bug fixes.
295 * Revision 1.37 1995/06/01 07:31:20 adam
296 * Rename of many typedefs -> IrTcl_...
298 * Revision 1.36 1995/05/31 13:09:59 adam
299 * Client searches/presents may be interrupted.
300 * New moving book-logo.
302 * Revision 1.35 1995/05/31 08:36:33 adam
303 * Bug fix in client.tcl: didn't save options on clientrc.tcl.
304 * New method: referenceId. More work on scan.
306 * Revision 1.34 1995/05/29 10:33:42 adam
307 * README and rename of startup script.
309 * Revision 1.33 1995/05/29 09:15:11 quinn
310 * Changed CS_SR to PROTO_SR, etc.
312 * Revision 1.32 1995/05/29 08:44:16 adam
313 * Work on delete of objects.
315 * Revision 1.31 1995/05/26 11:44:10 adam
316 * Bugs fixed. More work on MARC utilities and queries. Test
317 * client is up-to-date again.
319 * Revision 1.30 1995/05/26 08:54:11 adam
320 * New MARC utilities. Uses prefix query.
322 * Revision 1.29 1995/05/24 14:10:22 adam
323 * Work on idAuthentication, protocolVersion and options.
325 * Revision 1.28 1995/05/23 15:34:48 adam
326 * Many new settings, userInformationField, smallSetUpperBound, etc.
327 * A number of settings are inherited when ir-set is executed.
328 * This version is incompatible with the graphical test client (client.tcl).
330 * Revision 1.27 1995/05/11 15:34:47 adam
331 * Scan request changed a bit. This version works with RLG.
333 * Revision 1.26 1995/04/18 16:11:51 adam
334 * First version of graphical Scan. Some work on query-by-form.
336 * Revision 1.25 1995/04/17 09:37:17 adam
337 * Further development of scan.
339 * Revision 1.24 1995/04/11 14:16:42 adam
340 * Further work on scan. Response works. Entries aren't saved yet.
342 * Revision 1.23 1995/04/10 10:50:27 adam
343 * Result-set name defaults to suffix of ir-set name.
344 * Started working on scan. Not finished at this point.
346 * Revision 1.22 1995/03/31 10:43:03 adam
347 * More robust when getting bad MARC records.
349 * Revision 1.21 1995/03/31 08:56:37 adam
350 * New button "Search".
352 * Revision 1.20 1995/03/29 16:07:09 adam
353 * Bug fix: Didn't use setName in present request.
355 * Revision 1.19 1995/03/28 12:45:23 adam
356 * New ir method failback: called on disconnect/protocol error.
357 * New ir set/get method: protocol: SR / Z3950.
358 * Simple popup and disconnect when failback is invoked.
360 * Revision 1.18 1995/03/21 15:50:12 adam
363 * Revision 1.17 1995/03/21 13:41:03 adam
364 * Comstack cs_create not used too often. Non-blocking connect.
366 * Revision 1.16 1995/03/21 08:26:06 adam
367 * New method, setName, to specify the result set name (other than Default).
368 * New method, responseStatus, which returns diagnostic info, if any, after
369 * present response / search response.
371 * Revision 1.15 1995/03/20 15:24:07 adam
372 * Diagnostic records saved on searchResponse.
374 * Revision 1.14 1995/03/20 08:53:22 adam
375 * Event loop in tclmain.c rewritten. New method searchStatus.
377 * Revision 1.13 1995/03/17 18:26:17 adam
378 * Non-blocking i/o used now. Database names popup as cascade items.
380 * Revision 1.12 1995/03/17 15:45:00 adam
381 * Improved target/database setup.
383 * Revision 1.11 1995/03/16 17:54:03 adam
384 * Minor changes really.
386 * Revision 1.10 1995/03/15 16:14:50 adam
387 * Blocking arg in cs_create changed.
389 * Revision 1.9 1995/03/15 13:59:24 adam
392 * Revision 1.8 1995/03/15 08:25:16 adam
393 * New method presentStatus to check for error on present. Misc. cleanup
394 * of IrTcl_RecordList manipulations. Full MARC record presentation in
397 * Revision 1.7 1995/03/14 17:32:29 adam
398 * Presentation of full Marc record in popup window.
400 * Revision 1.6 1995/03/12 19:31:55 adam
401 * Pattern matching implemented when retrieving MARC records. More
402 * diagnostic functions.
404 * Revision 1.5 1995/03/10 18:00:15 adam
405 * Actual presentation in line-by-line format. RPN query support.
407 * Revision 1.4 1995/03/09 16:15:08 adam
408 * First presentRequest attempts. Hot-target list.
426 #if defined(__WIN32__)
427 # define WIN32_LEAN_AND_MEAN
428 # include <windows.h>
429 # undef WIN32_LEAN_AND_MEAN
432 * VC++ has an alternate entry point called DllMain, so we need to rename
436 # if defined(_MSC_VER)
437 # define EXPORT(a,b) __declspec(dllexport) a b
438 # define DllEntryPoint DllMain
440 # if defined(__BORLANDC__)
441 # define EXPORT(a,b) a _export b
443 # define EXPORT(a,b) a b
447 # define EXPORT(a,b) a b
450 static char *wrongArgs = "wrong # args: should be \"";
452 static int ir_tcl_error_exec (Tcl_Interp *interp, int argc, char **argv)
455 Tcl_AppendResult (interp, " while executing ", NULL);
456 for (i = 0; i<argc; i++)
457 Tcl_AppendResult (interp, (i ? " " : "\""), argv[i], NULL);
458 Tcl_AppendResult (interp, "\"", NULL);
463 static void ir_deleteDiags (IrTcl_Diagnostic **dst_list, int *dst_num);
465 static void ir_select_notify (ClientData clientData, int r, int w, int e);
467 void ir_select_add (int fd, void *obj)
469 ir_tcl_select_set (ir_select_notify, fd, obj, 1, 0, 0);
472 void ir_select_add_write (int fd, void *obj)
474 ir_tcl_select_set (ir_select_notify, fd, obj, 1, 1, 0);
477 void ir_select_remove (int fd, void *obj)
479 ir_tcl_select_set (NULL, fd, obj, 0, 0, 0);
482 void ir_select_remove_write (int fd, void *obj)
484 ir_tcl_select_set (ir_select_notify, fd, obj, 1, 0, 0);
487 static void delete_IR_record (IrTcl_RecordList *rl)
491 case Z_NamePlusRecord_databaseRecord:
492 switch (rl->u.dbrec.type)
495 ir_tcl_grs_del (&rl->u.dbrec.u.grs1);
500 xfree (rl->u.dbrec.buf);
501 rl->u.dbrec.buf = NULL;
503 case Z_NamePlusRecord_surrogateDiagnostic:
504 ir_deleteDiags (&rl->u.surrogateDiagnostics.list,
505 &rl->u.surrogateDiagnostics.num);
508 xfree (rl->elements);
511 static void purge_IR_records (IrTcl_SetObj *setobj)
513 IrTcl_RecordList *rl;
514 while ((rl = setobj->record_list))
516 setobj->record_list = rl->next;
517 delete_IR_record (rl);
522 static IrTcl_RecordList *new_IR_record (IrTcl_SetObj *setobj,
524 const char *elements)
526 IrTcl_RecordList *rl;
528 if (elements && !*elements)
530 for (rl = setobj->record_list; rl; rl = rl->next)
532 if (no == rl->no && (!rl->elements || !elements ||
533 !strcmp(elements, rl->elements)))
535 delete_IR_record (rl);
541 rl = ir_tcl_malloc (sizeof(*rl));
542 rl->next = setobj->record_list;
544 setobj->record_list = rl;
547 ir_tcl_strdup (NULL, &rl->elements, elements);
554 int ir_tcl_eval (Tcl_Interp *interp, const char *command)
556 char *tmp = ir_tcl_malloc (strlen(command)+1);
559 logf (LOG_DEBUG, "Invoking %.23s ...", command);
560 strcpy (tmp, command);
561 r = Tcl_Eval (interp, tmp);
564 const char *errorInfo = Tcl_GetVar (interp, "errorInfo", 0);
565 logf (LOG_WARN, "Tcl error in line %d: %s\n%s", interp->errorLine,
566 interp->result, errorInfo ? errorInfo : "<null>");
568 Tcl_FreeResult (interp);
574 * IrTcl_getRecordSyntaxStr: Return record syntax name of object id
576 static char *IrTcl_getRecordSyntaxStr (enum oid_value value)
579 struct oident ent, *entp;
581 ent.proto = PROTO_Z3950;
582 ent.oclass = CLASS_RECSYN;
585 o = oid_getoidbyent (&ent);
586 entp = oid_getentbyoid (o);
594 * IrTcl_getRecordSyntaxVal: Return record syntax value of string
596 static enum oid_value IrTcl_getRecordSyntaxVal (const char *name)
598 return oid_getvalbyname (name);
601 static IrTcl_RecordList *find_IR_record (IrTcl_SetObj *setobj, int no)
603 IrTcl_RecordList *rl;
605 for (rl = setobj->record_list; rl; rl = rl->next)
607 (!setobj->recordElements || !rl->elements ||
608 !strcmp (setobj->recordElements, rl->elements)))
613 static void delete_IR_records (IrTcl_SetObj *setobj)
615 IrTcl_RecordList *rl, *rl1;
617 for (rl = setobj->record_list; rl; rl = rl1)
619 delete_IR_record (rl);
623 setobj->record_list = NULL;
627 * ir_tcl_get_set_int: Set/get integer value
629 int ir_tcl_get_set_int (int *val, Tcl_Interp *interp, int argc, char **argv)
635 if (Tcl_GetInt (interp, argv[2], val)==TCL_ERROR)
638 sprintf (buf, "%d", *val);
639 Tcl_AppendResult (interp, buf, NULL);
645 * ir_tcl_method_error
647 int ir_tcl_method_error (Tcl_Interp *interp, int argc, char **argv,
650 IrTcl_Methods *tab_i = tab;
653 Tcl_AppendResult (interp, "bad method: \"", *argv, " ", argv[1],
654 "\"\nmethod should be of:", NULL);
655 for (tab_i = tab; tab_i->tab; tab_i++)
656 for (t = tab_i->tab; t->name; t++)
657 Tcl_AppendResult (interp, " ", t->name, NULL);
662 * ir_tcl_method: Search for method in table and invoke method handler
664 int ir_tcl_method (Tcl_Interp *interp, int argc, char **argv,
665 IrTcl_Methods *tab, int *ret)
667 IrTcl_Methods *tab_i = tab;
670 for (tab_i = tab; tab_i->tab; tab_i++)
671 for (t = tab_i->tab; t->name; t++)
674 if ((*t->method)(tab_i->obj, interp, argc, argv) == TCL_ERROR)
678 if (!strcmp (t->name, argv[1]))
680 *ret = (*t->method)(tab_i->obj, interp, argc, argv);
691 * ir_tcl_named_bits: get/set named bits
693 int ir_tcl_named_bits (struct ir_named_entry *tab, Odr_bitmask *ob,
694 Tcl_Interp *interp, int argc, char **argv)
696 struct ir_named_entry *ti;
701 for (no = 0; no < argc; no++)
704 for (ti = tab; ti->name; ti++)
705 if (!strcmp(argv[no], "@all") || !strcmp (argv[no], ti->name))
707 ODR_MASK_SET (ob, ti->pos);
712 Tcl_AppendResult (interp, "bad bit mask ", argv[no], NULL);
713 return ir_tcl_error_exec (interp, argc, argv);
718 for (ti = tab; ti->name; ti++)
719 if (ODR_MASK_GET (ob, ti->pos))
720 Tcl_AppendElement (interp, ti->name);
724 static void set_referenceId (ODR o, Z_ReferenceId **dst, const char *src)
730 *dst = odr_malloc (o, sizeof(**dst));
731 (*dst)->size = (*dst)->len = strlen(src);
732 (*dst)->buf = odr_malloc (o, (*dst)->len);
733 memcpy ((*dst)->buf, src, (*dst)->len);
737 static void get_referenceId (char **dst, Z_ReferenceId *src)
745 *dst = ir_tcl_malloc (src->len+1);
746 memcpy (*dst, src->buf, src->len);
747 (*dst)[src->len] = '\0';
750 /* ------------------------------------------------------- */
753 * do_init_request: init method on IR object
755 static int do_init_request (void *obj, Tcl_Interp *interp,
756 int argc, char **argv)
764 logf (LOG_DEBUG, "init %s", *argv);
767 Tcl_AppendResult (interp, "not connected", NULL);
768 return ir_tcl_error_exec (interp, argc, argv);
770 apdu = zget_APDU (p->odr_out, Z_APDU_initRequest);
771 req = apdu->u.initRequest;
773 set_referenceId (p->odr_out, &req->referenceId, p->set_inher.referenceId);
774 req->options = &p->options;
775 req->protocolVersion = &p->protocolVersion;
776 req->preferredMessageSize = &p->preferredMessageSize;
777 req->maximumRecordSize = &p->maximumRecordSize;
779 if (p->idAuthenticationGroupId)
781 Z_IdPass *pass = odr_malloc (p->odr_out, sizeof(*pass));
782 Z_IdAuthentication *auth = odr_malloc (p->odr_out, sizeof(*auth));
784 logf (LOG_DEBUG, "using pass authentication");
786 auth->which = Z_IdAuthentication_idPass;
787 auth->u.idPass = pass;
788 if (p->idAuthenticationGroupId && *p->idAuthenticationGroupId)
789 pass->groupId = p->idAuthenticationGroupId;
791 pass->groupId = NULL;
792 if (p->idAuthenticationUserId && *p->idAuthenticationUserId)
793 pass->userId = p->idAuthenticationUserId;
796 if (p->idAuthenticationPassword && *p->idAuthenticationPassword)
797 pass->password = p->idAuthenticationPassword;
799 pass->password = NULL;
800 req->idAuthentication = auth;
802 else if (!p->idAuthenticationOpen || !*p->idAuthenticationOpen)
803 req->idAuthentication = NULL;
806 Z_IdAuthentication *auth = odr_malloc (p->odr_out, sizeof(*auth));
808 logf (LOG_DEBUG, "using open authentication");
809 auth->which = Z_IdAuthentication_open;
810 auth->u.open = p->idAuthenticationOpen;
811 req->idAuthentication = auth;
813 req->implementationId = p->implementationId;
814 req->implementationName = p->implementationName;
815 req->implementationVersion = p->implementationVersion;
816 req->userInformationField = 0;
818 return ir_tcl_send_APDU (interp, p, apdu, "init", *argv);
822 * do_protocolVersion: Set protocol Version
824 static int do_protocolVersion (void *obj, Tcl_Interp *interp,
825 int argc, char **argv)
833 ODR_MASK_ZERO (&p->protocolVersion);
834 ODR_MASK_SET (&p->protocolVersion, 0);
835 ODR_MASK_SET (&p->protocolVersion, 1);
840 if (Tcl_GetInt (interp, argv[2], &version)==TCL_ERROR)
842 ODR_MASK_ZERO (&p->protocolVersion);
843 for (i = 0; i<version; i++)
844 ODR_MASK_SET (&p->protocolVersion, i);
846 for (i = 4; --i >= 0; )
847 if (ODR_MASK_GET (&p->protocolVersion, i))
849 sprintf (buf, "%d", i+1);
850 interp->result = buf;
855 * do_options: Set options
857 static int do_options (void *obj, Tcl_Interp *interp,
858 int argc, char **argv)
860 static struct ir_named_entry options_tab[] = {
864 { "resourceReport", 3 },
865 { "triggerResourceCtrl", 4},
866 { "resourceCtrl", 5},
870 { "extendedServices", 10},
871 { "level-1Segmentation", 11},
872 { "level-2Segmentation", 12},
873 { "concurrentOperations", 13},
874 { "namedResultSets", 14},
881 ODR_MASK_ZERO (&p->options);
882 ODR_MASK_SET (&p->options, 0);
883 ODR_MASK_SET (&p->options, 1);
884 ODR_MASK_SET (&p->options, 4);
885 ODR_MASK_SET (&p->options, 7);
886 ODR_MASK_SET (&p->options, 14);
889 return ir_tcl_named_bits (options_tab, &p->options, interp, argc-2, argv+2);
893 * do_apduInfo: Get APDU information
895 static int do_apduInfo (void *obj, Tcl_Interp *interp, int argc, char **argv)
903 sprintf (buf, "%d", p->apduLen);
904 Tcl_AppendElement (interp, buf);
905 sprintf (buf, "%d", p->apduOffset);
906 Tcl_AppendElement (interp, buf);
909 Tcl_AppendElement (interp, "");
912 apduf = fopen ("apdu.tmp", "w");
915 Tcl_AppendElement (interp, "");
918 odr_dumpBER (apduf, p->buf_in, p->apduLen);
920 if (!(apduf = fopen ("apdu.tmp", "r")))
921 Tcl_AppendElement (interp, "");
926 Tcl_AppendResult (interp, " {", NULL);
927 while ((c = getc (apduf)) != EOF)
931 Tcl_AppendResult (interp, buf, NULL);
934 Tcl_AppendResult (interp, "}", NULL);
941 * do_failInfo: Get fail information
943 static int do_failInfo (void *obj, Tcl_Interp *interp, int argc, char **argv)
953 sprintf (buf, "%d", p->failInfo);
959 case IR_TCL_FAIL_CONNECT:
960 cp = "connect failed";
962 case IR_TCL_FAIL_READ:
963 cp = "connection closed";
965 case IR_TCL_FAIL_WRITE:
966 cp = "connection closed";
968 case IR_TCL_FAIL_IN_APDU:
969 cp = "failed to decode incoming APDU";
971 case IR_TCL_FAIL_UNKNOWN_APDU:
977 Tcl_AppendElement (interp, buf);
978 Tcl_AppendElement (interp, cp);
983 * do_preferredMessageSize: Set/get preferred message size
985 static int do_preferredMessageSize (void *obj, Tcl_Interp *interp,
986 int argc, char **argv)
992 p->preferredMessageSize = 30000;
995 return ir_tcl_get_set_int (&p->preferredMessageSize, interp, argc, argv);
999 * do_maximumRecordSize: Set/get maximum record size
1001 static int do_maximumRecordSize (void *obj, Tcl_Interp *interp,
1002 int argc, char **argv)
1008 p->maximumRecordSize = 30000;
1011 return ir_tcl_get_set_int (&p->maximumRecordSize, interp, argc, argv);
1015 * do_initResult: Get init result
1017 static int do_initResult (void *obj, Tcl_Interp *interp,
1018 int argc, char **argv)
1024 return ir_tcl_get_set_int (&p->initResult, interp, argc, argv);
1029 * do_implementationName: Set/get Implementation Name.
1031 static int do_implementationName (void *obj, Tcl_Interp *interp,
1032 int argc, char **argv)
1037 return ir_tcl_strdup (interp, &p->implementationName,
1038 "Index Data/IrTcl on YAZ");
1039 else if (argc == -1)
1040 return ir_tcl_strdel (interp, &p->implementationName);
1043 xfree (p->implementationName);
1044 if (ir_tcl_strdup (interp, &p->implementationName, argv[2])
1048 Tcl_AppendResult (interp, p->implementationName, (char*) NULL);
1053 * do_implementationId: Get Implementation Id.
1055 static int do_implementationId (void *obj, Tcl_Interp *interp,
1056 int argc, char **argv)
1061 return ir_tcl_strdup (interp, &p->implementationId, "YAZ (id=81)");
1062 else if (argc == -1)
1063 return ir_tcl_strdel (interp, &p->implementationId);
1064 Tcl_AppendResult (interp, p->implementationId, (char*) NULL);
1069 * do_implementationVersion: get Implementation Version.
1071 static int do_implementationVersion (void *obj, Tcl_Interp *interp,
1072 int argc, char **argv)
1077 return ir_tcl_strdup (interp, &p->implementationVersion,
1079 #ifdef IR_TCL_VERSION
1080 " / Irtcl: " IR_TCL_VERSION
1083 else if (argc == -1)
1084 return ir_tcl_strdel (interp, &p->implementationVersion);
1085 Tcl_AppendResult (interp, p->implementationVersion, (char*) NULL);
1090 * do_targetImplementationName: Get Implementation Name of target.
1092 static int do_targetImplementationName (void *obj, Tcl_Interp *interp,
1093 int argc, char **argv)
1099 p->targetImplementationName = NULL;
1102 else if (argc == -1)
1103 return ir_tcl_strdel (interp, &p->targetImplementationName);
1104 Tcl_AppendResult (interp, p->targetImplementationName, (char*) NULL);
1109 * do_targetImplementationId: Get Implementation Id of target
1111 static int do_targetImplementationId (void *obj, Tcl_Interp *interp,
1112 int argc, char **argv)
1118 p->targetImplementationId = NULL;
1121 else if (argc == -1)
1122 return ir_tcl_strdel (interp, &p->targetImplementationId);
1123 Tcl_AppendResult (interp, p->targetImplementationId, (char*) NULL);
1128 * do_targetImplementationVersion: Get Implementation Version of target
1130 static int do_targetImplementationVersion (void *obj, Tcl_Interp *interp,
1131 int argc, char **argv)
1137 p->targetImplementationVersion = NULL;
1140 else if (argc == -1)
1141 return ir_tcl_strdel (interp, &p->targetImplementationVersion);
1142 Tcl_AppendResult (interp, p->targetImplementationVersion, (char*) NULL);
1147 * do_idAuthentication: Set/get id Authentication
1149 static int do_idAuthentication (void *obj, Tcl_Interp *interp,
1150 int argc, char **argv)
1154 if (argc >= 3 || argc == -1)
1156 xfree (p->idAuthenticationOpen);
1157 xfree (p->idAuthenticationGroupId);
1158 xfree (p->idAuthenticationUserId);
1159 xfree (p->idAuthenticationPassword);
1161 if (argc >= 3 || argc <= 0)
1163 p->idAuthenticationOpen = NULL;
1164 p->idAuthenticationGroupId = NULL;
1165 p->idAuthenticationUserId = NULL;
1166 p->idAuthenticationPassword = NULL;
1175 ir_tcl_strdup (interp, &p->idAuthenticationOpen, argv[2])
1182 ir_tcl_strdup (interp, &p->idAuthenticationGroupId, argv[2])
1186 ir_tcl_strdup (interp, &p->idAuthenticationUserId, argv[3])
1190 ir_tcl_strdup (interp, &p->idAuthenticationPassword, argv[4])
1195 if (p->idAuthenticationOpen)
1196 Tcl_AppendElement (interp, p->idAuthenticationOpen);
1197 else if (p->idAuthenticationGroupId)
1199 Tcl_AppendElement (interp, p->idAuthenticationGroupId);
1200 Tcl_AppendElement (interp, p->idAuthenticationUserId);
1201 Tcl_AppendElement (interp, p->idAuthenticationPassword);
1207 * do_connect: connect method on IR object
1209 static int do_connect (void *obj, Tcl_Interp *interp,
1210 int argc, char **argv)
1220 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
1221 " ?hostname?\"", NULL);
1226 Tcl_AppendResult (interp, p->hostname, NULL);
1230 logf (LOG_DEBUG, "connect %s %s", *argv, argv[2]);
1233 Tcl_AppendResult (interp, "already connected", NULL);
1234 return ir_tcl_error_exec (interp, argc, argv);
1236 if (!strcmp (p->comstackType, "tcpip"))
1238 p->cs_link = cs_create (tcpip_type, CS_BLOCK, p->protocol_type);
1239 logf (LOG_DEBUG, "tcp/ip connect %s", argv[2]);
1241 else if (!strcmp (p->comstackType, "mosi"))
1244 p->cs_link = cs_create (mosi_type, CS_BLOCK, p->protocol_type);
1245 logf (LOG_DEBUG, "mosi connect %s", argv[2]);
1247 Tcl_AppendResult (interp, "mosi not supported", NULL);
1248 return ir_tcl_error_exec (interp, argc, argv);
1253 Tcl_AppendResult (interp, "bad comstack type ",
1254 p->comstackType, NULL);
1255 return ir_tcl_error_exec (interp, argc, argv);
1257 if (ir_tcl_strdup (interp, &p->hostname, argv[2]) == TCL_ERROR)
1259 p->eventType = "connect";
1260 addr = cs_straddr (p->cs_link, argv[2]);
1263 ir_tcl_disconnect (p);
1264 Tcl_AppendResult (interp, "cs_straddr fail", NULL);
1265 return ir_tcl_error_exec (interp, argc, argv);
1267 if ((r=cs_connect (p->cs_link, addr)) < 0)
1269 ir_tcl_disconnect (p);
1270 Tcl_AppendResult (interp, "connect fail", NULL);
1271 return ir_tcl_error_exec (interp, argc, argv);
1273 ir_select_add (cs_fileno (p->cs_link), p);
1276 logf (LOG_DEBUG, "connect pending fd=%d", cs_fileno(p->cs_link));
1277 ir_select_add_write (cs_fileno (p->cs_link), p);
1278 p->state = IR_TCL_R_Connecting;
1282 logf (LOG_DEBUG, "connect ok fd=%d", cs_fileno(p->cs_link));
1283 p->state = IR_TCL_R_Idle;
1285 ir_tcl_eval (p->interp, p->callback);
1292 * ir_tcl_disconnect: close connection
1294 void ir_tcl_disconnect (IrTcl_Obj *p)
1298 logf(LOG_DEBUG, "Closing connection to %s", p->hostname);
1299 xfree (p->hostname);
1301 assert (p->cs_link);
1302 ir_select_remove (cs_fileno (p->cs_link), p);
1304 odr_reset (p->odr_in);
1306 #if TCL_MAJOR_VERSION == 8
1307 cs_fileno(p->cs_link) = -1;
1309 cs_close (p->cs_link);
1312 ODR_MASK_ZERO (&p->options);
1313 ODR_MASK_SET (&p->options, 0);
1314 ODR_MASK_SET (&p->options, 1);
1315 ODR_MASK_SET (&p->options, 4);
1316 ODR_MASK_SET (&p->options, 7);
1317 ODR_MASK_SET (&p->options, 14);
1319 ODR_MASK_ZERO (&p->protocolVersion);
1320 ODR_MASK_SET (&p->protocolVersion, 0);
1321 ODR_MASK_SET (&p->protocolVersion, 1);
1324 assert (!p->cs_link);
1328 * do_disconnect: disconnect method on IR object
1330 static int do_disconnect (void *obj, Tcl_Interp *interp,
1331 int argc, char **argv)
1337 p->state = IR_TCL_R_Idle;
1338 p->eventType = NULL;
1343 ir_tcl_disconnect (p);
1348 * do_comstack: Set/get comstack method on IR object
1350 static int do_comstack (void *o, Tcl_Interp *interp,
1351 int argc, char **argv)
1356 return ir_tcl_strdup (interp, &obj->comstackType, "tcpip");
1357 else if (argc == -1)
1358 return ir_tcl_strdel (interp, &obj->comstackType);
1361 xfree (obj->comstackType);
1362 if (ir_tcl_strdup (interp, &obj->comstackType, argv[2]) == TCL_ERROR)
1365 Tcl_AppendElement (interp, obj->comstackType);
1370 * do_logLevel: Set log level
1372 static int do_logLevel (void *o, Tcl_Interp *interp,
1373 int argc, char **argv)
1378 yaz_log_init (yaz_log_mask_str (argv[2]), "", NULL);
1380 yaz_log_init (yaz_log_mask_str (argv[2]), argv[3], NULL);
1382 yaz_log_init (yaz_log_mask_str (argv[2]), argv[3], argv[4]);
1388 * do_eventType: Return type of last event
1390 static int do_eventType (void *obj, Tcl_Interp *interp,
1391 int argc, char **argv)
1397 p->eventType = NULL;
1400 Tcl_AppendElement (interp, p->eventType ? p->eventType : "");
1406 * do_callback: add callback
1408 static int do_callback (void *obj, Tcl_Interp *interp,
1409 int argc, char **argv)
1418 else if (argc == -1)
1419 return ir_tcl_strdel (interp, &p->callback);
1422 xfree (p->callback);
1425 if (ir_tcl_strdup (interp, &p->callback, argv[2]) == TCL_ERROR)
1435 * do_failback: add error handle callback
1437 static int do_failback (void *obj, Tcl_Interp *interp,
1438 int argc, char **argv)
1447 else if (argc == -1)
1448 return ir_tcl_strdel (interp, &p->failback);
1451 xfree (p->failback);
1454 if (ir_tcl_strdup (interp, &p->failback, argv[2]) == TCL_ERROR)
1464 * do_initResponse: add init response handler
1466 static int do_initResponse (void *obj, Tcl_Interp *interp,
1467 int argc, char **argv)
1473 p->initResponse = NULL;
1476 else if (argc == -1)
1477 return ir_tcl_strdel (interp, &p->initResponse);
1480 xfree (p->initResponse);
1483 if (ir_tcl_strdup (interp, &p->initResponse, argv[2]) == TCL_ERROR)
1487 p->initResponse = NULL;
1492 * do_protocol: Set/get protocol method on IR object
1494 static int do_protocol (void *o, Tcl_Interp *interp, int argc, char **argv)
1500 p->protocol_type = PROTO_Z3950;
1505 if (!strcmp (argv[2], "Z39"))
1506 p->protocol_type = PROTO_Z3950;
1507 else if (!strcmp (argv[2], "SR"))
1508 p->protocol_type = PROTO_SR;
1511 Tcl_AppendResult (interp, "bad protocol ", argv[2], NULL);
1512 return ir_tcl_error_exec (interp, argc, argv);
1516 switch (p->protocol_type)
1519 Tcl_AppendElement (interp, "Z39");
1522 Tcl_AppendElement (interp, "SR");
1529 * do_triggerResourceControl:
1531 static int do_triggerResourceControl (void *obj, Tcl_Interp *interp,
1532 int argc, char **argv)
1536 Z_TriggerResourceControlRequest *req;
1537 bool_t is_false = 0;
1543 Tcl_AppendResult (interp, "not connected", NULL);
1544 return ir_tcl_error_exec (interp, argc, argv);
1546 apdu = zget_APDU (p->odr_out, Z_APDU_triggerResourceControlRequest);
1547 req = apdu->u.triggerResourceControlRequest;
1548 *req->requestedAction = Z_TriggerResourceCtrl_cancel;
1549 req->resultSetWanted = &is_false;
1551 return ir_tcl_send_APDU (interp, p, apdu, "triggerResourceControl",
1556 * do_databaseNames: specify database names
1558 static int do_databaseNames (void *obj, Tcl_Interp *interp,
1559 int argc, char **argv)
1562 IrTcl_SetCObj *p = obj;
1566 for (i=0; i<p->num_databaseNames; i++)
1567 xfree (p->databaseNames[i]);
1568 xfree (p->databaseNames);
1572 p->num_databaseNames = 0;
1573 p->databaseNames = NULL;
1578 for (i=0; i<p->num_databaseNames; i++)
1579 Tcl_AppendElement (interp, p->databaseNames[i]);
1582 if (p->databaseNames)
1584 for (i=0; i<p->num_databaseNames; i++)
1585 xfree (p->databaseNames[i]);
1586 xfree (p->databaseNames);
1588 p->num_databaseNames = argc - 2;
1590 ir_tcl_malloc (sizeof(*p->databaseNames) * (1+p->num_databaseNames));
1591 for (i=0; i<p->num_databaseNames; i++)
1593 if (ir_tcl_strdup (interp, &p->databaseNames[i], argv[2+i])
1597 p->databaseNames[i] = NULL;
1602 * do_replaceIndicator: Set/get replace Set indicator
1604 static int do_replaceIndicator (void *obj, Tcl_Interp *interp,
1605 int argc, char **argv)
1607 IrTcl_SetCObj *p = obj;
1611 p->replaceIndicator = 1;
1614 return ir_tcl_get_set_int (&p->replaceIndicator, interp, argc, argv);
1618 * do_queryType: Set/Get query method
1620 static int do_queryType (void *obj, Tcl_Interp *interp,
1621 int argc, char **argv)
1623 IrTcl_SetCObj *p = obj;
1626 return ir_tcl_strdup (interp, &p->queryType, "rpn");
1627 else if (argc == -1)
1628 return ir_tcl_strdel (interp, &p->queryType);
1631 xfree (p->queryType);
1632 if (ir_tcl_strdup (interp, &p->queryType, argv[2]) == TCL_ERROR)
1635 Tcl_AppendResult (interp, p->queryType, NULL);
1640 * do_userInformationField: Get User information field
1642 static int do_userInformationField (void *obj, Tcl_Interp *interp,
1643 int argc, char **argv)
1649 p->userInformationField = NULL;
1652 else if (argc == -1)
1653 return ir_tcl_strdel (interp, &p->userInformationField);
1654 Tcl_AppendResult (interp, p->userInformationField, NULL);
1659 * do_smallSetUpperBound: Set/get small set upper bound
1661 static int do_smallSetUpperBound (void *o, Tcl_Interp *interp,
1662 int argc, char **argv)
1664 IrTcl_SetCObj *p = o;
1668 p->smallSetUpperBound = 0;
1671 return ir_tcl_get_set_int (&p->smallSetUpperBound, interp, argc, argv);
1675 * do_largeSetLowerBound: Set/get large set lower bound
1677 static int do_largeSetLowerBound (void *o, Tcl_Interp *interp,
1678 int argc, char **argv)
1680 IrTcl_SetCObj *p = o;
1684 p->largeSetLowerBound = 2;
1687 return ir_tcl_get_set_int (&p->largeSetLowerBound, interp, argc, argv);
1691 * do_mediumSetPresentNumber: Set/get large set lower bound
1693 static int do_mediumSetPresentNumber (void *o, Tcl_Interp *interp,
1694 int argc, char **argv)
1696 IrTcl_SetCObj *p = o;
1700 p->mediumSetPresentNumber = 0;
1703 return ir_tcl_get_set_int (&p->mediumSetPresentNumber, interp, argc, argv);
1707 * do_referenceId: Set/Get referenceId
1709 static int do_referenceId (void *obj, Tcl_Interp *interp,
1710 int argc, char **argv)
1712 IrTcl_SetCObj *p = obj;
1716 p->referenceId = NULL;
1719 else if (argc == -1)
1720 return ir_tcl_strdel (interp, &p->referenceId);
1723 xfree (p->referenceId);
1724 if (ir_tcl_strdup (interp, &p->referenceId, argv[2]) == TCL_ERROR)
1727 Tcl_AppendResult (interp, p->referenceId, NULL);
1732 * do_preferredRecordSyntax: Set/get preferred record syntax
1734 static int do_preferredRecordSyntax (void *obj, Tcl_Interp *interp,
1735 int argc, char **argv)
1737 IrTcl_SetCObj *p = obj;
1741 p->preferredRecordSyntax = NULL;
1744 else if (argc == -1)
1746 xfree (p->preferredRecordSyntax);
1747 p->preferredRecordSyntax = NULL;
1752 xfree (p->preferredRecordSyntax);
1753 p->preferredRecordSyntax = NULL;
1754 if (argv[2][0] && (p->preferredRecordSyntax =
1755 ir_tcl_malloc (sizeof(*p->preferredRecordSyntax))))
1756 *p->preferredRecordSyntax = IrTcl_getRecordSyntaxVal (argv[2]);
1761 (interp,!p->preferredRecordSyntax ? "" :
1762 IrTcl_getRecordSyntaxStr(*p->preferredRecordSyntax));
1769 * do_elementSetNames: Set/Get element Set Names
1771 static int do_elementSetNames (void *obj, Tcl_Interp *interp,
1772 int argc, char **argv)
1774 IrTcl_SetCObj *p = obj;
1778 p->elementSetNames = NULL;
1781 else if (argc == -1)
1782 return ir_tcl_strdel (interp, &p->elementSetNames);
1785 xfree (p->elementSetNames);
1786 if (ir_tcl_strdup (interp, &p->elementSetNames, argv[2]) == TCL_ERROR)
1789 Tcl_AppendResult (interp, p->elementSetNames, NULL);
1794 * do_smallSetElementSetNames: Set/Get small Set Element Set Names
1796 static int do_smallSetElementSetNames (void *obj, Tcl_Interp *interp,
1797 int argc, char **argv)
1799 IrTcl_SetCObj *p = obj;
1803 p->smallSetElementSetNames = NULL;
1806 else if (argc == -1)
1807 return ir_tcl_strdel (interp, &p->smallSetElementSetNames);
1810 xfree (p->smallSetElementSetNames);
1811 if (ir_tcl_strdup (interp, &p->smallSetElementSetNames,
1812 argv[2]) == TCL_ERROR)
1815 Tcl_AppendResult (interp, p->smallSetElementSetNames, NULL);
1820 * do_mediumSetElementSetNames: Set/Get medium Set Element Set Names
1822 static int do_mediumSetElementSetNames (void *obj, Tcl_Interp *interp,
1823 int argc, char **argv)
1825 IrTcl_SetCObj *p = obj;
1829 p->mediumSetElementSetNames = NULL;
1832 else if (argc == -1)
1833 return ir_tcl_strdel (interp, &p->mediumSetElementSetNames);
1836 xfree (p->mediumSetElementSetNames);
1837 if (ir_tcl_strdup (interp, &p->mediumSetElementSetNames,
1838 argv[2]) == TCL_ERROR)
1841 Tcl_AppendResult (interp, p->mediumSetElementSetNames, NULL);
1845 static IrTcl_Method ir_method_tab[] = {
1846 { "comstack", do_comstack, NULL },
1847 { "protocol", do_protocol, NULL },
1848 { "failback", do_failback, NULL },
1849 { "failInfo", do_failInfo, NULL },
1850 { "apduInfo", do_apduInfo, NULL },
1851 { "logLevel", do_logLevel, NULL },
1853 { "eventType", do_eventType, NULL },
1854 { "connect", do_connect, NULL },
1855 { "protocolVersion", do_protocolVersion, NULL },
1856 { "preferredMessageSize", do_preferredMessageSize, NULL },
1857 { "maximumRecordSize", do_maximumRecordSize, NULL },
1858 { "implementationName", do_implementationName, NULL },
1859 { "implementationId", do_implementationId, NULL },
1860 { "implementationVersion", do_implementationVersion, NULL },
1861 { "targetImplementationName", do_targetImplementationName, NULL },
1862 { "targetImplementationId", do_targetImplementationId, NULL },
1863 { "targetImplementationVersion", do_targetImplementationVersion, NULL},
1864 { "userInformationField", do_userInformationField, NULL},
1865 { "idAuthentication", do_idAuthentication, NULL},
1866 { "options", do_options, NULL},
1867 { "init", do_init_request, NULL},
1868 { "initResult", do_initResult, NULL},
1869 { "disconnect", do_disconnect, NULL},
1870 { "callback", do_callback, NULL},
1871 { "initResponse", do_initResponse, NULL},
1872 { "triggerResourceControl", do_triggerResourceControl, NULL},
1873 { "initResponse", do_initResponse, NULL},
1877 static IrTcl_Method ir_set_c_method_tab[] = {
1878 { "databaseNames", do_databaseNames, NULL},
1879 { "replaceIndicator", do_replaceIndicator, NULL},
1880 { "queryType", do_queryType, NULL},
1881 { "preferredRecordSyntax", do_preferredRecordSyntax, NULL},
1882 { "smallSetUpperBound", do_smallSetUpperBound, NULL},
1883 { "largeSetLowerBound", do_largeSetLowerBound, NULL},
1884 { "mediumSetPresentNumber", do_mediumSetPresentNumber, NULL},
1885 { "referenceId", do_referenceId, NULL},
1886 { "elementSetNames", do_elementSetNames, NULL},
1887 { "smallSetElementSetNames", do_smallSetElementSetNames, NULL},
1888 { "mediumSetElementSetNames", do_mediumSetElementSetNames, NULL},
1893 * ir_obj_method: IR Object methods
1895 static int ir_obj_method (ClientData clientData, Tcl_Interp *interp,
1896 int argc, char **argv)
1898 IrTcl_Methods tab[3];
1899 IrTcl_Obj *p = (IrTcl_Obj *) clientData;
1904 Tcl_AppendResult (interp, wrongArgs, *argv, "method args...\"", NULL);
1908 tab[0].tab = ir_method_tab;
1910 tab[1].tab = ir_set_c_method_tab;
1911 tab[1].obj = &p->set_inher;
1914 if (ir_tcl_method (interp, argc, argv, tab, &r) == TCL_ERROR)
1915 return ir_tcl_method_error (interp, argc, argv, tab);
1920 * ir_obj_delete: IR Object disposal
1922 static void ir_obj_delete (ClientData clientData)
1924 IrTcl_Obj *obj = (IrTcl_Obj *) clientData;
1925 IrTcl_Methods tab[3];
1928 if (obj->ref_count > 0)
1930 assert (obj->ref_count == 0);
1932 logf (LOG_DEBUG, "ir object delete");
1933 tab[0].tab = ir_method_tab;
1935 tab[1].tab = ir_set_c_method_tab;
1936 tab[1].obj = &obj->set_inher;
1939 ir_tcl_method (NULL, -1, NULL, tab, NULL);
1942 odr_destroy (obj->odr_in);
1943 odr_destroy (obj->odr_out);
1944 odr_destroy (obj->odr_pr);
1949 * ir_obj_init: IR Object initialization
1951 int ir_obj_init (ClientData clientData, Tcl_Interp *interp,
1952 int argc, char **argv, ClientData *subData,
1953 ClientData parentData)
1955 IrTcl_Methods tab[3];
1963 Tcl_AppendResult (interp, wrongArgs, *argv, " objName\"", NULL);
1966 obj = ir_tcl_malloc (sizeof(*obj));
1969 obj->bibset = ccl_qual_mk ();
1970 if ((inf = fopen ("default.bib", "r")))
1972 ccl_qual_file (obj->bibset, inf);
1977 logf (LOG_DEBUG, "ir object create %s", argv[1]);
1978 obj->odr_in = odr_createmem (ODR_DECODE);
1979 odr_choice_enable_bias (obj->odr_in, 0);
1980 obj->odr_out = odr_createmem (ODR_ENCODE);
1981 obj->odr_pr = odr_createmem (ODR_PRINT);
1982 obj->state = IR_TCL_R_Idle;
1983 obj->interp = interp;
1987 obj->request_queue = NULL;
1989 tab[0].tab = ir_method_tab;
1991 tab[1].tab = ir_set_c_method_tab;
1992 tab[1].obj = &obj->set_inher;
1995 if (ir_tcl_method (interp, 0, NULL, tab, NULL) == TCL_ERROR)
1997 Tcl_AppendResult (interp, "Failed to initialize ", argv[1], NULL);
2000 *subData = (ClientData) obj;
2006 * ir_obj_mk: IR Object creation
2008 static int ir_obj_mk (ClientData clientData, Tcl_Interp *interp,
2009 int argc, char **argv)
2012 int r = ir_obj_init (clientData, interp, argc, argv, &subData, 0);
2016 Tcl_CreateCommand (interp, argv[1], ir_obj_method,
2017 subData, ir_obj_delete);
2021 IrTcl_Class ir_obj_class = {
2029 /* ------------------------------------------------------- */
2031 * do_search: Do search request
2033 static int do_search (void *o, Tcl_Interp *interp, int argc, char **argv)
2035 Z_SearchRequest *req;
2039 IrTcl_SetObj *obj = o;
2042 #if TCL_MAJOR_VERSION > 8 || (TCL_MAJOR_VERSION == 8 && TCL_MINOR_VERSION > 0)
2054 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1], "query\"",
2058 #if TCL_MAJOR_VERSION > 8 || (TCL_MAJOR_VERSION == 8 && TCL_MINOR_VERSION > 0)
2059 query_str = Tcl_UtfToExternalDString(0, argv[2], -1, &ds);
2061 query_str = argv[2];
2063 logf (LOG_DEBUG, "search %s %s", *argv, query_str);
2064 if (!obj->set_inher.num_databaseNames)
2066 Tcl_AppendResult (interp, "no databaseNames", NULL);
2067 code = ir_tcl_error_exec (interp, argc, argv);
2072 Tcl_AppendResult (interp, "not connected", NULL);
2073 code = ir_tcl_error_exec (interp, argc, argv);
2076 apdu = zget_APDU (p->odr_out, Z_APDU_searchRequest);
2077 req = apdu->u.searchRequest;
2081 set_referenceId (p->odr_out, &req->referenceId,
2082 obj->set_inher.referenceId);
2084 req->smallSetUpperBound = &obj->set_inher.smallSetUpperBound;
2085 req->largeSetLowerBound = &obj->set_inher.largeSetLowerBound;
2086 req->mediumSetPresentNumber = &obj->set_inher.mediumSetPresentNumber;
2087 req->replaceIndicator = &obj->set_inher.replaceIndicator;
2088 req->resultSetName = obj->setName ? obj->setName : "default";
2089 logf (LOG_DEBUG, "Search, resultSetName %s", req->resultSetName);
2090 req->num_databaseNames = obj->set_inher.num_databaseNames;
2091 req->databaseNames = obj->set_inher.databaseNames;
2092 for (r=0; r < obj->set_inher.num_databaseNames; r++)
2093 logf (LOG_DEBUG, " Database %s", obj->set_inher.databaseNames[r]);
2094 if (obj->set_inher.preferredRecordSyntax)
2096 struct oident ident;
2098 ident.proto = p->protocol_type;
2099 ident.oclass = CLASS_RECSYN;
2100 ident.value = *obj->set_inher.preferredRecordSyntax;
2101 logf (LOG_DEBUG, "Preferred record syntax is %d", ident.value);
2102 req->preferredRecordSyntax = odr_oiddup (p->odr_out,
2103 oid_getoidbyent (&ident));
2106 req->preferredRecordSyntax = 0;
2108 if (obj->set_inher.smallSetElementSetNames &&
2109 *obj->set_inher.smallSetElementSetNames)
2111 Z_ElementSetNames *esn = odr_malloc (p->odr_out, sizeof(*esn));
2113 esn->which = Z_ElementSetNames_generic;
2114 esn->u.generic = obj->set_inher.smallSetElementSetNames;
2115 req->smallSetElementSetNames = esn;
2118 req->smallSetElementSetNames = NULL;
2120 if (obj->set_inher.mediumSetElementSetNames &&
2121 *obj->set_inher.mediumSetElementSetNames)
2123 Z_ElementSetNames *esn = odr_malloc (p->odr_out, sizeof(*esn));
2125 esn->which = Z_ElementSetNames_generic;
2126 esn->u.generic = obj->set_inher.mediumSetElementSetNames;
2127 req->mediumSetElementSetNames = esn;
2130 req->mediumSetElementSetNames = NULL;
2132 req->query = &query;
2134 logf (LOG_DEBUG, "queryType %s", obj->set_inher.queryType);
2135 if (!strcmp (obj->set_inher.queryType, "rpn"))
2137 Z_RPNQuery *RPNquery;
2139 RPNquery = p_query_rpn (p->odr_out, p->protocol_type, query_str);
2142 Tcl_AppendResult (interp, "query syntax error", NULL);
2143 code = ir_tcl_error_exec (interp, argc, argv);
2146 query.which = Z_Query_type_1;
2147 query.u.type_1 = RPNquery;
2150 else if (!strcmp (obj->set_inher.queryType, "cclrpn"))
2154 struct ccl_rpn_node *rpn;
2155 Z_RPNQuery *RPNquery;
2158 bib1.proto = p->protocol_type;
2159 bib1.oclass = CLASS_ATTSET;
2160 bib1.value = VAL_BIB1;
2162 rpn = ccl_find_str(p->bibset, query_str, &error, &pos);
2165 Tcl_AppendResult (interp, "ccl syntax error ", ccl_err_msg(error),
2167 code = ir_tcl_error_exec (interp, argc, argv);
2171 ccl_pr_tree (rpn, stderr);
2172 fprintf (stderr, "\n");
2174 RPNquery = ccl_rpn_query(p->odr_out, rpn);
2175 RPNquery->attributeSetId = oid_getoidbyent (&bib1);
2176 query.which = Z_Query_type_1;
2177 query.u.type_1 = RPNquery;
2180 else if (!strcmp (obj->set_inher.queryType, "ccl"))
2182 query.which = Z_Query_type_2;
2183 query.u.type_2 = &ccl_query;
2184 ccl_query.buf = (unsigned char *) query_str;
2185 ccl_query.len = strlen (query_str);
2189 Tcl_AppendResult (interp, "invalid query method ",
2190 obj->set_inher.queryType, NULL);
2191 code = ir_tcl_error_exec (interp, argc, argv);
2194 code = ir_tcl_send_APDU (interp, p, apdu, "search", *argv);
2196 #if TCL_MAJOR_VERSION > 8 || (TCL_MAJOR_VERSION == 8 && TCL_MINOR_VERSION > 0)
2197 Tcl_DStringFree (&ds);
2203 * do_searchResponse: add search response handler
2205 static int do_searchResponse (void *o, Tcl_Interp *interp,
2206 int argc, char **argv)
2208 IrTcl_SetObj *obj = o;
2212 obj->searchResponse = NULL;
2215 else if (argc == -1)
2216 return ir_tcl_strdel (interp, &obj->searchResponse);
2219 xfree (obj->searchResponse);
2222 if (ir_tcl_strdup (interp, &obj->searchResponse, argv[2])
2227 obj->searchResponse = NULL;
2233 * do_presentResponse: add present response handler
2235 static int do_presentResponse (void *o, Tcl_Interp *interp,
2236 int argc, char **argv)
2238 IrTcl_SetObj *obj = o;
2242 obj->presentResponse = NULL;
2245 else if (argc == -1)
2246 return ir_tcl_strdel (interp, &obj->presentResponse);
2249 xfree (obj->presentResponse);
2252 if (ir_tcl_strdup (interp, &obj->presentResponse, argv[2])
2257 obj->presentResponse = NULL;
2263 * do_sortResponse: add sort response handler
2265 static int do_sortResponse (void *o, Tcl_Interp *interp,
2266 int argc, char **argv)
2268 IrTcl_SetObj *obj = o;
2272 obj->sortResponse = NULL;
2275 else if (argc == -1)
2276 return ir_tcl_strdel (interp, &obj->sortResponse);
2279 xfree (obj->sortResponse);
2282 if (ir_tcl_strdup (interp, &obj->sortResponse, argv[2])
2287 obj->sortResponse = NULL;
2293 * do_resultCount: Get number of hits
2295 static int do_resultCount (void *o, Tcl_Interp *interp,
2296 int argc, char **argv)
2298 IrTcl_SetObj *obj = o;
2302 obj->resultCount = 0;
2305 return ir_tcl_get_set_int (&obj->resultCount, interp, argc, argv);
2309 * do_searchStatus: Get search status (after search response)
2311 static int do_searchStatus (void *o, Tcl_Interp *interp,
2312 int argc, char **argv)
2314 IrTcl_SetObj *obj = o;
2318 return ir_tcl_get_set_int (&obj->searchStatus, interp, argc, argv);
2322 * do_presentStatus: Get search status (after search/present response)
2324 static int do_presentStatus (void *o, Tcl_Interp *interp,
2325 int argc, char **argv)
2327 IrTcl_SetObj *obj = o;
2331 return ir_tcl_get_set_int (&obj->presentStatus, interp, argc, argv);
2335 * do_sortStatus: Get sort status (after sort response)
2337 static int do_sortStatus (void *o, Tcl_Interp *interp,
2338 int argc, char **argv)
2340 IrTcl_SetObj *obj = o;
2345 obj->sortStatus = Z_SortStatus_failure;
2348 switch (obj->sortStatus)
2350 case Z_SortStatus_success:
2351 res = "success"; break;
2352 case Z_SortStatus_partial_1:
2353 res = "partial"; break;
2354 case Z_SortStatus_failure:
2355 res = "failure"; break;
2357 res = "unknown"; break;
2359 Tcl_AppendElement (interp, res);
2364 * do_nextResultSetPosition: Get next result set position
2365 * (after search/present response)
2367 static int do_nextResultSetPosition (void *o, Tcl_Interp *interp,
2368 int argc, char **argv)
2370 IrTcl_SetObj *obj = o;
2374 obj->nextResultSetPosition = 0;
2377 return ir_tcl_get_set_int (&obj->nextResultSetPosition, interp,
2382 * do_setName: Set result Set name
2384 static int do_setName (void *o, Tcl_Interp *interp,
2385 int argc, char **argv)
2387 IrTcl_SetObj *obj = o;
2390 return ir_tcl_strdup (interp, &obj->setName, "Default");
2391 else if (argc == -1)
2392 return ir_tcl_strdel (interp, &obj->setName);
2395 xfree (obj->setName);
2396 if (ir_tcl_strdup (interp, &obj->setName, argv[2])
2400 Tcl_AppendElement (interp, obj->setName);
2405 * do_numberOfRecordsReturned: Get number of records returned
2407 static int do_numberOfRecordsReturned (void *o, Tcl_Interp *interp,
2408 int argc, char **argv)
2410 IrTcl_SetObj *obj = o;
2414 obj->numberOfRecordsReturned = 0;
2417 return ir_tcl_get_set_int (&obj->numberOfRecordsReturned, interp,
2422 * do_type: Return type (if any) at position.
2424 static int do_type (void *o, Tcl_Interp *interp, int argc, char **argv)
2426 IrTcl_SetObj *obj = o;
2428 IrTcl_RecordList *rl;
2432 obj->record_list = NULL;
2435 else if (argc == -1)
2437 delete_IR_records (obj);
2442 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2443 " position\"", NULL);
2446 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2448 rl = find_IR_record (obj, offset);
2451 logf (LOG_DEBUG, "%s %s %s: no record", argv[0], argv[1], argv[2]);
2456 case Z_NamePlusRecord_databaseRecord:
2457 interp->result = "DB";
2459 case Z_NamePlusRecord_surrogateDiagnostic:
2460 interp->result = "SD";
2468 * do_recordType: Return record type (if any) at position.
2470 static int do_recordType (void *o, Tcl_Interp *interp, int argc, char **argv)
2472 IrTcl_SetObj *obj = o;
2474 IrTcl_RecordList *rl;
2480 else if (argc == -1)
2486 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2487 " position\"", NULL);
2490 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2492 rl = find_IR_record (obj, offset);
2495 logf (LOG_DEBUG, "%s %s %s: no record", argv[0], argv[1], argv[2]);
2498 if (rl->which != Z_NamePlusRecord_databaseRecord)
2500 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2503 Tcl_AppendElement (interp, (char*)
2504 IrTcl_getRecordSyntaxStr (rl->u.dbrec.type));
2509 * set record elements (for record extraction)
2511 static int do_recordElements (void *o, Tcl_Interp *interp,
2512 int argc, char **argv)
2514 IrTcl_SetObj *obj = o;
2518 obj->recordElements = NULL;
2521 else if (argc == -1)
2522 return ir_tcl_strdel (NULL, &obj->recordElements);
2525 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2526 " ?position?\"", NULL);
2531 xfree (obj->recordElements);
2532 return ir_tcl_strdup (NULL, &obj->recordElements,
2533 (*argv[2] ? argv[2] : NULL));
2535 Tcl_AppendResult (interp, obj->recordElements, NULL);
2542 static int ir_diagResult (Tcl_Interp *interp, IrTcl_Diagnostic *list, int num)
2548 for (i = 0; i<num; i++)
2550 sprintf (buf, "%d", list[i].condition);
2551 Tcl_AppendElement (interp, buf);
2552 cp = diagbib1_str (list[i].condition);
2554 Tcl_AppendElement (interp, (char*) cp);
2556 Tcl_AppendElement (interp, "");
2557 if (list[i].addinfo)
2558 Tcl_AppendElement (interp, (char*) list[i].addinfo);
2560 Tcl_AppendElement (interp, "");
2566 * do_diag: Return diagnostic record info
2568 static int do_diag (void *o, Tcl_Interp *interp, int argc, char **argv)
2570 IrTcl_SetObj *obj = o;
2572 IrTcl_RecordList *rl;
2578 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2579 " position\"", NULL);
2582 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2584 rl = find_IR_record (obj, offset);
2587 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2590 if (rl->which != Z_NamePlusRecord_surrogateDiagnostic)
2592 Tcl_AppendResult (interp, "No Diagnostic record at #", argv[2], NULL);
2595 return ir_diagResult (interp, rl->u.surrogateDiagnostics.list,
2596 rl->u.surrogateDiagnostics.num);
2600 * do_getMarc: Get ISO2709 Record lines/fields
2602 static int do_getMarc (void *o, Tcl_Interp *interp, int argc, char **argv)
2604 IrTcl_SetObj *obj = o;
2606 IrTcl_RecordList *rl;
2612 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2613 " position line|field tag ind field\"", NULL);
2616 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2618 rl = find_IR_record (obj, offset);
2621 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2624 if (rl->which != Z_NamePlusRecord_databaseRecord)
2626 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2629 return ir_tcl_get_marc (interp, rl->u.dbrec.buf, argc, argv);
2633 * do_getSutrs: Get SUTRS Record
2635 static int do_getSutrs (void *o, Tcl_Interp *interp, int argc, char **argv)
2637 IrTcl_SetObj *obj = o;
2639 IrTcl_RecordList *rl;
2645 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2646 " position\"", NULL);
2649 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2651 rl = find_IR_record (obj, offset);
2654 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2657 if (rl->which != Z_NamePlusRecord_databaseRecord)
2659 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2662 if (!rl->u.dbrec.buf || rl->u.dbrec.type != VAL_SUTRS)
2664 Tcl_AppendElement (interp, rl->u.dbrec.buf);
2670 * do_getGrs: Get a GRS-1 Record
2672 static int do_getGrs (void *o, Tcl_Interp *interp, int argc, char **argv)
2674 IrTcl_SetObj *obj = o;
2676 IrTcl_RecordList *rl;
2682 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2683 " position ?(set,tag) (set,tag) ...?\"", NULL);
2686 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2688 rl = find_IR_record (obj, offset);
2691 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2694 if (rl->which != Z_NamePlusRecord_databaseRecord)
2696 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2699 if (rl->u.dbrec.type != VAL_GRS1)
2701 return ir_tcl_get_grs (interp, rl->u.dbrec.u.grs1, argc, argv);
2706 * do_getExplain: Get an Explain Record
2708 static int do_getExplain (void *o, Tcl_Interp *interp, int argc, char **argv)
2710 IrTcl_SetObj *obj = o;
2711 IrTcl_Obj *p = obj->parent;
2713 Z_ext_typeent *etype;
2715 IrTcl_RecordList *rl;
2721 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2722 " position ?mask? ...\"", NULL);
2725 if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
2727 rl = find_IR_record (obj, offset);
2730 Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
2733 if (rl->which != Z_NamePlusRecord_databaseRecord)
2735 Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
2738 if (!rl->u.dbrec.buf || rl->u.dbrec.type != VAL_EXPLAIN)
2741 if (!(etype = z_ext_getentbyref (VAL_EXPLAIN)))
2743 assert (rl->u.dbrec.buf);
2744 odr_setbuf (p->odr_in, rl->u.dbrec.buf, rl->u.dbrec.size, 0);
2745 if (!(*etype->fun)(p->odr_in, (char **) &rr, 0, 0))
2748 if (etype->what != Z_External_explainRecord)
2751 return ir_tcl_get_explain (interp, rr, argc, argv);
2755 * do_responseStatus: Return response status (present or search)
2757 static int do_responseStatus (void *o, Tcl_Interp *interp,
2758 int argc, char **argv)
2760 IrTcl_SetObj *obj = o;
2764 obj->recordFlag = 0;
2765 obj->nonSurrogateDiagnosticNum = 0;
2766 obj->nonSurrogateDiagnosticList = NULL;
2769 else if (argc == -1)
2771 ir_deleteDiags (&obj->nonSurrogateDiagnosticList,
2772 &obj->nonSurrogateDiagnosticNum);
2775 if (!obj->recordFlag)
2777 Tcl_AppendElement (interp, "OK");
2782 case Z_Records_DBOSD:
2783 Tcl_AppendElement (interp, "DBOSD");
2786 Tcl_AppendElement (interp, "NSD");
2787 return ir_diagResult (interp, obj->nonSurrogateDiagnosticList,
2788 obj->nonSurrogateDiagnosticNum);
2794 * do_present: Perform Present Request
2797 static int do_present (void *o, Tcl_Interp *interp, int argc, char **argv)
2799 IrTcl_SetObj *obj = o;
2802 Z_PresentRequest *req;
2810 if (Tcl_GetInt (interp, argv[2], &start) == TCL_ERROR)
2817 if (Tcl_GetInt (interp, argv[3], &number) == TCL_ERROR)
2822 logf (LOG_DEBUG, "present %s %d %d", *argv, start, number);
2826 Tcl_AppendResult (interp, "not connected", NULL);
2827 return ir_tcl_error_exec (interp, argc, argv);
2830 obj->number = number;
2832 apdu = zget_APDU (p->odr_out, Z_APDU_presentRequest);
2833 req = apdu->u.presentRequest;
2835 set_referenceId (p->odr_out, &req->referenceId,
2836 obj->set_inher.referenceId);
2838 req->resultSetId = obj->setName ? obj->setName : "Default";
2840 req->resultSetStartPoint = &start;
2841 req->numberOfRecordsRequested = &number;
2842 if (obj->set_inher.preferredRecordSyntax)
2844 struct oident ident;
2846 ident.proto = p->protocol_type;
2847 ident.oclass = CLASS_RECSYN;
2848 ident.value = *obj->set_inher.preferredRecordSyntax;
2849 logf (LOG_DEBUG, "Preferred record syntax is %d", ident.value);
2850 req->preferredRecordSyntax = odr_oiddup (p->odr_out,
2851 oid_getoidbyent (&ident));
2854 req->preferredRecordSyntax = 0;
2856 if (obj->set_inher.elementSetNames && *obj->set_inher.elementSetNames)
2858 Z_ElementSetNames *esn = odr_malloc (p->odr_out, sizeof(*esn));
2859 Z_RecordComposition *compo = odr_malloc (p->odr_out, sizeof(*compo));
2861 esn->which = Z_ElementSetNames_generic;
2862 esn->u.generic = obj->set_inher.elementSetNames;
2864 req->recordComposition = compo;
2865 compo->which = Z_RecordComp_simple;
2866 compo->u.simple = esn;
2869 req->recordComposition = NULL;
2870 return ir_tcl_send_APDU (interp, p, apdu, "present", *argv);
2873 #define IR_TCL_RECORD_ENCODING_ISO2709 1
2874 #define IR_TCL_RECORD_ENCODING_RAW 2
2880 } IrTcl_FileRecordHead;
2883 * do_loadFile: Load result set from file
2885 static int do_loadFile (void *o, Tcl_Interp *interp,
2886 int argc, char **argv)
2888 IrTcl_SetObj *setobj = o;
2900 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2901 " filename ?start? ?number?\"", NULL);
2905 start = atoi (argv[3]);
2907 number = atoi (argv[4]);
2910 inf = fopen (argv[2], "r");
2913 Tcl_AppendResult (interp, "Cannot open ", argv[2], NULL);
2914 return ir_tcl_error_exec (interp, argc, argv);
2916 while (offset < (start+number))
2918 IrTcl_FileRecordHead head;
2919 IrTcl_RecordList *rl;
2921 if (fread (&head, sizeof(head), 1, inf) < 1)
2923 rl = new_IR_record (setobj, offset,
2924 Z_NamePlusRecord_databaseRecord,
2925 (argc > 5) ? argv[5] : NULL);
2926 rl->u.dbrec.type = head.syntax;
2927 if (head.encoding == IR_TCL_RECORD_ENCODING_ISO2709)
2929 if (!(buf = ir_tcl_fread_marc (inf, &size)))
2931 rl->u.dbrec.buf = buf;
2932 rl->u.dbrec.size = size;
2933 if (size != head.size)
2936 Tcl_AppendResult (interp, "bad ISO2709 encoding", NULL);
2937 return ir_tcl_error_exec (interp, argc, argv);
2940 else if (head.encoding == IR_TCL_RECORD_ENCODING_RAW)
2942 rl->u.dbrec.size = head.size;
2943 rl->u.dbrec.buf = ir_tcl_malloc (head.size + 1);
2944 if (fread (rl->u.dbrec.buf, rl->u.dbrec.size, 1, inf) < 1)
2947 Tcl_AppendResult (interp, "bad raw encoding", NULL);
2948 return ir_tcl_error_exec (interp, argc, argv);
2950 rl->u.dbrec.buf[rl->u.dbrec.size] = '\0';
2954 rl->u.dbrec.buf = NULL;
2955 rl->u.dbrec.size = 0;
2957 Tcl_AppendResult (interp, "bad encoding", NULL);
2958 return ir_tcl_error_exec (interp, argc, argv);
2962 setobj->numberOfRecordsReturned = offset - start;
2968 * do_saveFile: Save result set on file
2970 static int do_saveFile (void *o, Tcl_Interp *interp,
2971 int argc, char **argv)
2973 IrTcl_SetObj *setobj = o;
2978 IrTcl_RecordList *rl;
2984 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
2985 " filename ?start? ?number?\"", NULL);
2989 start = atoi (argv[3]);
2991 number = atoi (argv[4]);
2994 outf = fopen (argv[2], "w");
2997 Tcl_AppendResult (interp, "cannot open file", NULL);
2998 return ir_tcl_error_exec (interp, argc, argv);
3000 while (offset < (start+number) && (rl = find_IR_record (setobj, offset)))
3002 if (rl->which == Z_NamePlusRecord_databaseRecord &&
3003 rl->u.dbrec.buf && rl->u.dbrec.size)
3005 IrTcl_FileRecordHead head;
3007 head.encoding = IR_TCL_RECORD_ENCODING_RAW;
3008 head.syntax = rl->u.dbrec.type;
3009 head.size = rl->u.dbrec.size;
3010 if (fwrite (&head, sizeof(head), 1, outf) < 1)
3012 Tcl_AppendResult (interp, "cannot write", NULL);
3013 return ir_tcl_error_exec (interp, argc, argv);
3015 if (fwrite (rl->u.dbrec.buf, rl->u.dbrec.size, 1, outf) < 1)
3017 Tcl_AppendResult (interp, "cannot write", NULL);
3018 return ir_tcl_error_exec (interp, argc, argv);
3025 Tcl_AppendResult (interp, "cannot write ", NULL);
3026 return ir_tcl_error_exec (interp, argc, argv);
3032 /* ------------------------------------------------------- */
3034 * do_sort: Do sort request
3036 static int do_sort (void *o, Tcl_Interp *interp, int argc, char **argv)
3040 IrTcl_SetObj *obj = o;
3042 char sort_string[64], sort_flags[64];
3045 Z_SortKeySpecList *sksl;
3056 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1], "query\"",
3060 logf (LOG_DEBUG, "sort %s %s", *argv, argv[2]);
3063 Tcl_AppendResult (interp, "not connected", NULL);
3064 return ir_tcl_error_exec (interp, argc, argv);
3066 apdu = zget_APDU (p->odr_out, Z_APDU_sortRequest);
3067 sksl = (Z_SortKeySpecList *) odr_malloc (p->odr_out, sizeof(*sksl));
3068 req = apdu->u.sortRequest;
3070 set_referenceId (p->odr_out, &req->referenceId,
3071 obj->set_inher.referenceId);
3074 req->num_inputResultSetNames = 1;
3075 req->inputResultSetNames = (Z_InternationalString **)
3076 odr_malloc (p->odr_out, sizeof(*req->inputResultSetNames));
3077 req->inputResultSetNames[0] = obj->setName;
3079 req->inputResultSetNames =
3080 (Z_StringList *)odr_malloc (p->odr_out,
3081 sizeof(*req->inputResultSetNames));
3082 req->inputResultSetNames->num_strings = 1;
3083 req->inputResultSetNames->strings =
3084 (char **)odr_malloc (p->odr_out,
3085 sizeof(*req->inputResultSetNames->strings));
3086 req->inputResultSetNames->strings[0] = obj->setName;
3089 req->sortedResultSetName = (char *) obj->setName;
3092 req->sortSequence = sksl;
3093 sksl->num_specs = 0;
3094 sksl->specs = (Z_SortKeySpec **)
3095 odr_malloc (p->odr_out, sizeof(sksl->specs) * 20);
3097 bib1.proto = PROTO_Z3950;
3098 bib1.oclass = CLASS_ATTSET;
3099 bib1.value = VAL_BIB1;
3101 while ((sscanf (arg, "%63s %63s%n", sort_string, sort_flags, &off)) == 2
3105 char *sort_string_sep;
3106 Z_SortKeySpec *sks = (Z_SortKeySpec *)
3107 odr_malloc (p->odr_out, sizeof(*sks));
3108 Z_SortKey *sk = (Z_SortKey *)
3109 odr_malloc (p->odr_out, sizeof(*sk));
3112 sksl->specs[sksl->num_specs++] = sks;
3113 sks->sortElement = (Z_SortElement *)
3114 odr_malloc (p->odr_out, sizeof(*sks->sortElement));
3115 sks->sortElement->which = Z_SortElement_generic;
3116 sks->sortElement->u.generic = sk;
3118 if ((sort_string_sep = strchr (sort_string, '=')))
3120 Z_AttributeElement *el = (Z_AttributeElement *)
3121 odr_malloc (p->odr_out, sizeof(*el));
3122 sk->which = Z_SortKey_sortAttributes;
3123 sk->u.sortAttributes =
3124 (Z_SortAttributes *)
3125 odr_malloc (p->odr_out, sizeof(*sk->u.sortAttributes));
3126 sk->u.sortAttributes->id = oid_ent_to_oid(&bib1, oid);
3127 sk->u.sortAttributes->list =
3129 odr_malloc (p->odr_out, sizeof(*sk->u.sortAttributes->list));
3130 sk->u.sortAttributes->list->num_attributes = 1;
3131 sk->u.sortAttributes->list->attributes =
3132 (Z_AttributeElement **)odr_malloc (p->odr_out,
3133 sizeof(*sk->u.sortAttributes->list->attributes));
3134 sk->u.sortAttributes->list->attributes[0] = el;
3135 el->attributeSet = 0;
3136 el->attributeType = (int *)
3137 odr_malloc (p->odr_out, sizeof(*el->attributeType));
3138 *el->attributeType = atoi (sort_string);
3139 el->which = Z_AttributeValue_numeric;
3140 el->value.numeric = (int *)
3141 odr_malloc (p->odr_out, sizeof(*el->value.numeric));
3142 *el->value.numeric = atoi (sort_string_sep + 1);
3146 sk->which = Z_SortKey_sortField;
3147 sk->u.sortField = (char *)odr_malloc (p->odr_out, strlen(sort_string)+1);
3148 strcpy (sk->u.sortField, sort_string);
3150 sks->sortRelation = (int *)
3151 odr_malloc (p->odr_out, sizeof(*sks->sortRelation));
3152 *sks->sortRelation = Z_SortRelation_ascending;
3153 sks->caseSensitivity = (int *)
3154 odr_malloc (p->odr_out, sizeof(*sks->caseSensitivity));
3155 *sks->caseSensitivity = Z_SortCase_caseSensitive;
3158 sks->which = Z_SortKeySpec_null;
3159 sks->u.null = odr_nullval ();
3161 sks->missingValueAction = NULL;
3164 for (i = 0; sort_flags[i]; i++)
3166 switch (sort_flags[i])
3171 *sks->sortRelation = Z_SortRelation_descending;
3176 *sks->sortRelation = Z_SortRelation_ascending;
3180 *sks->caseSensitivity = Z_SortCase_caseInsensitive;
3184 *sks->caseSensitivity = Z_SortCase_caseSensitive;
3189 if (!sksl->num_specs)
3191 printf ("Missing sort specifications\n");
3194 return ir_tcl_send_APDU (interp, p, apdu, "sort", *argv);
3197 static IrTcl_Method ir_set_method_tab[] = {
3198 { "search", do_search, NULL},
3199 { "searchResponse", do_searchResponse, NULL},
3200 { "presentResponse", do_presentResponse, NULL},
3201 { "searchStatus", do_searchStatus, NULL},
3202 { "presentStatus", do_presentStatus, NULL},
3203 { "nextResultSetPosition", do_nextResultSetPosition, NULL},
3204 { "setName", do_setName, NULL},
3205 { "resultCount", do_resultCount, NULL},
3206 { "numberOfRecordsReturned", do_numberOfRecordsReturned, NULL},
3207 { "present", do_present, NULL},
3208 { "type", do_type, NULL},
3209 { "getMarc", do_getMarc, NULL},
3210 { "getSutrs", do_getSutrs, NULL},
3211 { "getGrs", do_getGrs, NULL},
3212 { "getExplain", do_getExplain, NULL},
3213 { "recordType", do_recordType, NULL},
3214 { "recordElements", do_recordElements, NULL},
3215 { "diag", do_diag, NULL},
3216 { "responseStatus", do_responseStatus, NULL},
3217 { "loadFile", do_loadFile, NULL},
3218 { "saveFile", do_saveFile, NULL},
3219 { "sort", do_sort, NULL },
3220 { "sortResponse", do_sortResponse, NULL},
3221 { "sortStatus", do_sortStatus, NULL},
3226 * ir_set_obj_method: IR Set Object methods
3228 static int ir_set_obj_method (ClientData clientData, Tcl_Interp *interp,
3229 int argc, char **argv)
3231 IrTcl_Methods tabs[3];
3232 IrTcl_SetObj *p = (IrTcl_SetObj *) clientData;
3237 Tcl_AppendResult (interp, wrongArgs, *argv, " method args...\"", NULL);
3240 tabs[0].tab = ir_set_method_tab;
3242 tabs[1].tab = ir_set_c_method_tab;
3243 tabs[1].obj = &p->set_inher;
3246 if (ir_tcl_method (interp, argc, argv, tabs, &r) == TCL_ERROR)
3247 return ir_tcl_method_error (interp, argc, argv, tabs);
3252 * ir_set_obj_delete: IR Set Object disposal
3254 static void ir_set_obj_delete (ClientData clientData)
3256 IrTcl_Methods tabs[3];
3257 IrTcl_SetObj *p = (IrTcl_SetObj *) clientData;
3259 logf (LOG_DEBUG, "ir set delete");
3261 tabs[0].tab = ir_set_method_tab;
3263 tabs[1].tab = ir_set_c_method_tab;
3264 tabs[1].obj = &p->set_inher;
3267 ir_tcl_method (NULL, -1, NULL, tabs, NULL);
3273 * ir_set_obj_init: IR Set Object initialization
3275 static int ir_set_obj_init (ClientData clientData, Tcl_Interp *interp,
3276 int argc, char **argv, ClientData *subData,
3277 ClientData parentData)
3279 IrTcl_Methods tabs[3];
3282 if (argc < 2 || argc > 3)
3284 Tcl_AppendResult (interp, wrongArgs, *argv,
3285 " objSetName ?objParent?\"", NULL);
3288 obj = ir_tcl_malloc (sizeof(*obj));
3289 logf (LOG_DEBUG, "ir set create %s", argv[1]);
3296 obj->parent = (IrTcl_Obj *) parentData;
3298 dst = &obj->set_inher;
3299 src = &obj->parent->set_inher;
3301 if ((dst->num_databaseNames = src->num_databaseNames))
3303 dst->databaseNames =
3304 ir_tcl_malloc (sizeof (*dst->databaseNames)
3305 * (1+dst->num_databaseNames));
3306 for (i = 0; i < dst->num_databaseNames; i++)
3307 if (ir_tcl_strdup (interp, &dst->databaseNames[i],
3308 src->databaseNames[i]) == TCL_ERROR)
3310 dst->databaseNames[i] = NULL;
3313 dst->databaseNames = NULL;
3314 if (ir_tcl_strdup (interp, &dst->queryType, src->queryType)
3318 if (ir_tcl_strdup (interp, &dst->referenceId, src->referenceId)
3322 if (ir_tcl_strdup (interp, &dst->elementSetNames, src->elementSetNames)
3326 if (ir_tcl_strdup (interp, &dst->smallSetElementSetNames,
3327 src->smallSetElementSetNames)
3331 if (ir_tcl_strdup (interp, &dst->mediumSetElementSetNames,
3332 src->mediumSetElementSetNames)
3336 if (src->preferredRecordSyntax &&
3337 (dst->preferredRecordSyntax
3338 = ir_tcl_malloc (sizeof(*dst->preferredRecordSyntax))))
3339 *dst->preferredRecordSyntax = *src->preferredRecordSyntax;
3341 dst->preferredRecordSyntax = NULL;
3342 dst->replaceIndicator = src->replaceIndicator;
3343 dst->smallSetUpperBound = src->smallSetUpperBound;
3344 dst->largeSetLowerBound = src->largeSetLowerBound;
3345 dst->mediumSetPresentNumber = src->mediumSetPresentNumber;
3350 tabs[0].tab = ir_set_method_tab;
3354 if (ir_tcl_method (interp, 0, NULL, tabs, NULL) == TCL_ERROR)
3357 *subData = (ClientData) obj;
3362 * ir_set_obj_mk: IR Set Object creation
3364 static int ir_set_obj_mk (ClientData clientData, Tcl_Interp *interp,
3365 int argc, char **argv)
3368 ClientData parentData = 0;
3373 Tcl_CmdInfo parent_info;
3374 if (!Tcl_GetCommandInfo (interp, argv[2], &parent_info))
3376 Tcl_AppendResult (interp, "no object parent", NULL);
3377 return ir_tcl_error_exec (interp, argc, argv);
3379 parentData = parent_info.clientData;
3381 r = ir_set_obj_init (clientData, interp, argc, argv, &subData, parentData);
3384 Tcl_CreateCommand (interp, argv[1], ir_set_obj_method,
3385 subData, ir_set_obj_delete);
3389 IrTcl_Class ir_set_obj_class = {
3396 /* ------------------------------------------------------- */
3399 * do_scan: Perform scan
3401 static int do_scan (void *o, Tcl_Interp *interp, int argc, char **argv)
3405 IrTcl_ScanObj *obj = o;
3406 IrTcl_Obj *p = obj->parent;
3411 struct ccl_rpn_node *rpn;
3415 #if TCL_MAJOR_VERSION > 8 || (TCL_MAJOR_VERSION == 8 && TCL_MINOR_VERSION > 0)
3423 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
3424 " scanQuery\"", NULL);
3427 #if TCL_MAJOR_VERSION > 8 || (TCL_MAJOR_VERSION == 8 && TCL_MINOR_VERSION > 0)
3428 start_term = Tcl_UtfToExternalDString(0, argv[2], -1, &ds);
3430 start_term = argv[2];
3432 logf (LOG_DEBUG, "scan %s %s", *argv, start_term);
3433 if (!p->set_inher.num_databaseNames)
3435 Tcl_AppendResult (interp, "no databaseNames", NULL);
3436 code = ir_tcl_error_exec (interp, argc, argv);
3441 Tcl_AppendResult (interp, "not connected", NULL);
3442 code = ir_tcl_error_exec (interp, argc, argv);
3446 apdu = zget_APDU (p->odr_out, Z_APDU_scanRequest);
3447 req = apdu->u.scanRequest;
3449 set_referenceId (p->odr_out, &req->referenceId, p->set_inher.referenceId);
3450 req->num_databaseNames = p->set_inher.num_databaseNames;
3451 req->databaseNames = p->set_inher.databaseNames;
3454 if (!(req->termListAndStartPoint =
3455 p_query_scan (p->odr_out, p->protocol_type,
3456 &req->attributeSet, start_term)))
3458 Tcl_AppendResult (interp, "query syntax error", NULL);
3459 code = ir_tcl_error_exec (interp, argc, argv);
3463 rpn = ccl_find_str(p->bibset, start_term, &r, &pos);
3466 Tcl_AppendResult (interp, "ccl syntax error ", ccl_err_msg(r), NULL);
3467 code = ir_tcl_error_exec (interp, argc, argv);
3470 bib1.proto = p->protocol_type;
3471 bib1.oclass = CLASS_ATTSET;
3472 bib1.value = VAL_BIB1;
3474 req->attributeSet = oid_getoidbyent (&bib1);
3475 if (!(req->termListAndStartPoint = ccl_scan_query (p->odr_out, rpn)))
3481 req->stepSize = &obj->stepSize;
3482 req->numberOfTermsRequested = &obj->numberOfTermsRequested;
3483 req->preferredPositionInResponse = &obj->preferredPositionInResponse;
3484 logf (LOG_DEBUG, "stepSize=%d", *req->stepSize);
3485 logf (LOG_DEBUG, "numberOfTermsRequested=%d",
3486 *req->numberOfTermsRequested);
3487 logf (LOG_DEBUG, "preferredPositionInResponse=%d",
3488 *req->preferredPositionInResponse);
3490 code = ir_tcl_send_APDU (interp, p, apdu, "scan", *argv);
3492 #if TCL_MAJOR_VERSION > 8 || (TCL_MAJOR_VERSION == 8 && TCL_MINOR_VERSION > 0)
3493 Tcl_DStringFree (&ds);
3499 * do_scanResponse: add scan response handler
3501 static int do_scanResponse (void *o, Tcl_Interp *interp,
3502 int argc, char **argv)
3504 IrTcl_ScanObj *obj = o;
3508 obj->scanResponse = NULL;
3511 else if (argc == -1)
3512 return ir_tcl_strdel (interp, &obj->scanResponse);
3515 xfree (obj->scanResponse);
3518 if (ir_tcl_strdup (interp, &obj->scanResponse, argv[2])
3523 obj->scanResponse = NULL;
3529 * do_stepSize: Set/get replace Step Size
3531 static int do_stepSize (void *obj, Tcl_Interp *interp,
3532 int argc, char **argv)
3534 IrTcl_ScanObj *p = obj;
3540 return ir_tcl_get_set_int (&p->stepSize, interp, argc, argv);
3544 * do_numberOfTermsRequested: Set/get Number of Terms requested
3546 static int do_numberOfTermsRequested (void *obj, Tcl_Interp *interp,
3547 int argc, char **argv)
3549 IrTcl_ScanObj *p = obj;
3553 p->numberOfTermsRequested = 20;
3556 return ir_tcl_get_set_int (&p->numberOfTermsRequested, interp, argc, argv);
3561 * do_preferredPositionInResponse: Set/get preferred Position
3563 static int do_preferredPositionInResponse (void *obj, Tcl_Interp *interp,
3564 int argc, char **argv)
3566 IrTcl_ScanObj *p = obj;
3570 p->preferredPositionInResponse = 1;
3573 return ir_tcl_get_set_int (&p->preferredPositionInResponse, interp,
3578 * do_scanStatus: Get scan status
3580 static int do_scanStatus (void *obj, Tcl_Interp *interp,
3581 int argc, char **argv)
3583 IrTcl_ScanObj *p = obj;
3587 return ir_tcl_get_set_int (&p->scanStatus, interp, argc, argv);
3591 * do_numberOfEntriesReturned: Get number of Entries returned
3593 static int do_numberOfEntriesReturned (void *obj, Tcl_Interp *interp,
3594 int argc, char **argv)
3596 IrTcl_ScanObj *p = obj;
3600 return ir_tcl_get_set_int (&p->numberOfEntriesReturned, interp,
3605 * do_positionOfTerm: Get position of Term
3607 static int do_positionOfTerm (void *obj, Tcl_Interp *interp,
3608 int argc, char **argv)
3610 IrTcl_ScanObj *p = obj;
3614 return ir_tcl_get_set_int (&p->positionOfTerm, interp, argc, argv);
3618 * do_scanLine: get Scan Line (surrogate or normal) after response
3620 static int do_scanLine (void *obj, Tcl_Interp *interp, int argc, char **argv)
3622 IrTcl_ScanObj *p = obj;
3628 p->entries_flag = 0;
3630 p->nonSurrogateDiagnosticNum = 0;
3631 p->nonSurrogateDiagnosticList = 0;
3634 else if (argc == -1)
3636 p->entries_flag = 0;
3637 /* release entries */
3640 ir_deleteDiags (&p->nonSurrogateDiagnosticList,
3641 &p->nonSurrogateDiagnosticNum);
3646 Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1],
3647 " position\"", NULL);
3650 if (Tcl_GetInt (interp, argv[2], &i) == TCL_ERROR)
3652 if (!p->entries_flag || !p->entries || i >= p->num_entries || i < 0)
3654 switch (p->entries[i].which)
3656 case Z_Entry_termInfo:
3657 Tcl_AppendElement (interp, "T");
3658 if (p->entries[i].u.term.buf)
3659 Tcl_AppendElement (interp, p->entries[i].u.term.buf);
3661 Tcl_AppendElement (interp, "");
3662 sprintf (numstr, "%d", p->entries[i].u.term.globalOccurrences);
3663 Tcl_AppendElement (interp, numstr);
3665 case Z_Entry_surrogateDiagnostic:
3666 Tcl_AppendElement (interp, "SD");
3667 return ir_diagResult (interp, p->entries[i].u.diag.list,
3668 p->entries[i].u.diag.num);
3674 static IrTcl_Method ir_scan_method_tab[] = {
3675 { "scan", do_scan, NULL},
3676 { "scanResponse", do_scanResponse, NULL},
3677 { "stepSize", do_stepSize, NULL},
3678 { "numberOfTermsRequested", do_numberOfTermsRequested, NULL},
3679 { "preferredPositionInResponse", do_preferredPositionInResponse, NULL},
3680 { "scanStatus", do_scanStatus, NULL},
3681 { "numberOfEntriesReturned", do_numberOfEntriesReturned, NULL},
3682 { "positionOfTerm", do_positionOfTerm, NULL},
3683 { "scanLine", do_scanLine, NULL},
3688 * ir_scan_obj_method: IR Scan Object methods
3690 static int ir_scan_obj_method (ClientData clientData, Tcl_Interp *interp,
3691 int argc, char **argv)
3693 IrTcl_Methods tabs[2];
3698 Tcl_AppendResult (interp, wrongArgs, *argv, " method args...\"", NULL);
3701 tabs[0].tab = ir_scan_method_tab;
3702 tabs[0].obj = clientData;
3705 if (ir_tcl_method (interp, argc, argv, tabs, &r) == TCL_ERROR)
3706 return ir_tcl_method_error (interp, argc, argv, tabs);
3711 * ir_scan_obj_delete: IR Scan Object disposal
3713 static void ir_scan_obj_delete (ClientData clientData)
3715 IrTcl_Methods tabs[2];
3716 IrTcl_ScanObj *obj = (IrTcl_ScanObj *) clientData;
3718 tabs[0].tab = ir_scan_method_tab;
3722 ir_tcl_method (NULL, -1, NULL, tabs, NULL);
3727 * ir_scan_obj_mk: IR Scan Object creation
3729 static int ir_scan_obj_mk (ClientData clientData, Tcl_Interp *interp,
3730 int argc, char **argv)
3732 Tcl_CmdInfo parent_info;
3734 IrTcl_Methods tabs[2];
3738 Tcl_AppendResult (interp, wrongArgs, *argv,
3739 "objScanName objParentName\"", NULL);
3742 logf (LOG_DEBUG, "ir scan create %s", argv[1]);
3743 if (!Tcl_GetCommandInfo (interp, argv[2], &parent_info))
3745 Tcl_AppendResult (interp, "no object parent", NULL);
3746 return ir_tcl_error_exec (interp, argc, argv);
3748 obj = ir_tcl_malloc (sizeof(*obj));
3749 obj->parent = (IrTcl_Obj *) parent_info.clientData;
3751 tabs[0].tab = ir_scan_method_tab;
3755 if (ir_tcl_method (interp, 0, NULL, tabs, NULL) == TCL_ERROR)
3757 Tcl_CreateCommand (interp, argv[1], ir_scan_obj_method,
3758 (ClientData) obj, ir_scan_obj_delete);
3762 /* ------------------------------------------------------- */
3765 * ir_log_init_proc: set yaz log level
3767 static int ir_log_init_proc (ClientData clientData, Tcl_Interp *interp,
3768 int argc, char **argv)
3770 if (argc <= 1 || argc > 4)
3772 Tcl_AppendResult (interp, wrongArgs, *argv,
3773 " ?level ?prefix ?filename\"", NULL);
3777 yaz_log_init (yaz_log_mask_str (argv[1]), "", NULL);
3779 yaz_log_init (yaz_log_mask_str (argv[1]), argv[2], NULL);
3781 yaz_log_init (yaz_log_mask_str (argv[1]), argv[2], argv[3]);
3786 * ir_log_proc: log yaz message
3788 static int ir_log_proc (ClientData clientData, Tcl_Interp *interp,
3789 int argc, char **argv)
3794 Tcl_AppendResult (interp, wrongArgs, *argv,
3795 " level string\"", NULL);
3798 mask = yaz_log_mask_str_x (argv[1], 0);
3799 logf (LOG_DEBUG, "%s", argv[2]);
3805 * ir_version: log ir version
3807 static int ir_version (ClientData clientData, Tcl_Interp *interp,
3808 int argc, char **argv)
3810 Tcl_AppendElement (interp, IR_TCL_VERSION);
3811 Tcl_AppendElement (interp, YAZ_VERSION);
3816 /* ------------------------------------------------------- */
3817 static void ir_initResponse (void *obj, Z_InitResponse *initrs)
3821 p->initResult = *initrs->result ? 1 : 0;
3822 if (!*initrs->result)
3823 logf (LOG_DEBUG, "Connection rejected by target");
3825 logf (LOG_DEBUG, "Connection accepted by target");
3827 get_referenceId (&p->set_inher.referenceId, initrs->referenceId);
3829 xfree (p->targetImplementationId);
3830 ir_tcl_strdup (p->interp, &p->targetImplementationId,
3831 initrs->implementationId);
3832 xfree (p->targetImplementationName);
3833 ir_tcl_strdup (p->interp, &p->targetImplementationName,
3834 initrs->implementationName);
3835 xfree (p->targetImplementationVersion);
3836 ir_tcl_strdup (p->interp, &p->targetImplementationVersion,
3837 initrs->implementationVersion);
3839 p->maximumRecordSize = *initrs->maximumRecordSize;
3840 p->preferredMessageSize = *initrs->preferredMessageSize;
3842 memcpy (&p->options, initrs->options, sizeof(initrs->options));
3843 memcpy (&p->protocolVersion, initrs->protocolVersion,
3844 sizeof(initrs->protocolVersion));
3845 xfree (p->userInformationField);
3846 p->userInformationField = NULL;
3847 if (initrs->userInformationField)
3851 if (initrs->userInformationField->which == ODR_EXTERNAL_octet &&
3852 (p->userInformationField =
3853 ir_tcl_malloc ((len =
3854 initrs->userInformationField->
3855 u.octet_aligned->len) +1)))
3857 memcpy (p->userInformationField,
3858 initrs->userInformationField->u.octet_aligned->buf,
3860 (p->userInformationField)[len] = '\0';
3865 static void ir_deleteDiags (IrTcl_Diagnostic **dst_list, int *dst_num)
3868 for (i = 0; i<*dst_num; i++)
3869 xfree ((*dst_list)[i].addinfo);
3875 static void ir_handleDiags (IrTcl_Diagnostic **dst_list, int *dst_num,
3876 Z_DiagRec **list, int num)
3879 char *addinfo = NULL;
3882 *dst_list = ir_tcl_malloc (sizeof(**dst_list) * num);
3883 for (i = 0; i<num; i++)
3886 switch (list[i]->which)
3888 case Z_DiagRec_defaultFormat:
3889 (*dst_list)[i].condition = *list[i]->u.defaultFormat->condition;
3891 switch (list[i]->u.defaultFormat->which)
3893 case Z_DefaultDiagFormat_v2Addinfo:
3894 addinfo = list[i]->u.defaultFormat->u.v2Addinfo;
3896 case Z_DefaultDiagFormat_v3Addinfo:
3897 addinfo = list[i]->u.defaultFormat->u.v3Addinfo;
3901 addinfo = list[i]->u.defaultFormat->addinfo;
3904 ((*dst_list)[i].addinfo = ir_tcl_malloc (strlen(addinfo)+1)))
3905 strcpy ((*dst_list)[i].addinfo, addinfo);
3906 cp = diagbib1_str ((*dst_list)[i].condition);
3907 logf (LOG_DEBUG, "Diag %d %s %s", (*dst_list)[i].condition,
3908 cp ? cp : "", addinfo ? addinfo : "");
3911 (*dst_list)[i].addinfo = NULL;
3912 (*dst_list)[i].condition = 0;
3917 static void ir_handleDBRecord (IrTcl_Obj *p, IrTcl_RecordList *rl,
3920 struct oident *ident;
3921 Z_ext_typeent *etype;
3923 logf (LOG_DEBUG, "handleDBRecord size=%d", oe->u.octet_aligned->len);
3924 rl->u.dbrec.size = oe->u.octet_aligned->len;
3925 rl->u.dbrec.buf = NULL;
3927 if ((ident = oid_getentbyoid (oe->direct_reference)))
3928 rl->u.dbrec.type = ident->value;
3930 rl->u.dbrec.type = VAL_USMARC;
3932 if (ident && (oe->which == Z_External_single ||
3933 oe->which == Z_External_octet)
3934 && (etype = z_ext_getentbyref (ident->value)))
3938 odr_setbuf (p->odr_in, (char*) oe->u.octet_aligned->buf,
3939 oe->u.octet_aligned->len, 0);
3940 if (!(*etype->fun)(p->odr_in, (char **) &rr, 0, 0))
3942 rl->u.dbrec.type = VAL_NONE;
3945 switch (etype->what)
3947 case Z_External_sutrs:
3948 logf (LOG_DEBUG, "Z_External_sutrs");
3950 if ((rl->u.dbrec.buf = ir_tcl_malloc (oe->u.sutrs->len+1)))
3952 memcpy (rl->u.dbrec.buf, oe->u.sutrs->buf,
3954 rl->u.dbrec.buf[oe->u.sutrs->len] = '\0';
3956 rl->u.dbrec.size = oe->u.sutrs->len;
3958 case Z_External_grs1:
3959 logf (LOG_DEBUG, "Z_External_grs1");
3961 ir_tcl_grs_mk (oe->u.grs1, &rl->u.dbrec.u.grs1);
3963 case Z_External_explainRecord:
3964 logf (LOG_DEBUG, "Z_External_explainRecord");
3965 if ((rl->u.dbrec.buf = ir_tcl_malloc (rl->u.dbrec.size)))
3967 memcpy (rl->u.dbrec.buf, oe->u.octet_aligned->buf,
3975 if (oe->which == Z_External_octet && rl->u.dbrec.size > 0)
3977 char *buf = (char*) oe->u.octet_aligned->buf;
3978 if ((rl->u.dbrec.buf = ir_tcl_malloc (rl->u.dbrec.size)))
3979 memcpy (rl->u.dbrec.buf, buf, rl->u.dbrec.size);
3981 else if (rl->u.dbrec.type == VAL_SUTRS &&
3982 oe->which == Z_External_sutrs)
3984 if ((rl->u.dbrec.buf = ir_tcl_malloc (oe->u.sutrs->len+1)))
3986 memcpy (rl->u.dbrec.buf, oe->u.sutrs->buf,
3988 rl->u.dbrec.buf[oe->u.sutrs->len] = '\0';
3990 rl->u.dbrec.size = oe->u.sutrs->len;
3992 else if (rl->u.dbrec.type == VAL_GRS1 &&
3993 oe->which == Z_External_grs1)
3995 ir_tcl_grs_mk (oe->u.grs1, &rl->u.dbrec.u.grs1);
4000 static void ir_handleZRecords (void *o, Z_Records *zrs, IrTcl_SetObj *setobj,
4001 const char *elements)
4006 IrTcl_RecordList *rl;
4008 setobj->which = zrs->which;
4009 setobj->recordFlag = 1;
4011 ir_deleteDiags (&setobj->nonSurrogateDiagnosticList,
4012 &setobj->nonSurrogateDiagnosticNum);
4013 if (zrs->which == Z_Records_DBOSD)
4015 int num_rec = zrs->u.databaseOrSurDiagnostics->num_records;
4017 if (num_rec != setobj->numberOfRecordsReturned)
4019 logf (LOG_WARN, "numberOfRecordsReturned=%d but num records=%d",
4020 setobj->numberOfRecordsReturned, num_rec);
4021 setobj->numberOfRecordsReturned = num_rec;
4024 for (offset = 0; offset < num_rec; offset++)
4026 Z_NamePlusRecord *znpr = zrs->u.databaseOrSurDiagnostics->
4029 rl = new_IR_record (setobj, setobj->start + offset, znpr->which,
4031 if (rl->which == Z_NamePlusRecord_surrogateDiagnostic)
4032 ir_handleDiags (&rl->u.surrogateDiagnostics.list,
4033 &rl->u.surrogateDiagnostics.num,
4034 &znpr->u.surrogateDiagnostic,
4037 ir_handleDBRecord (p, rl,
4038 (Z_External*) (znpr->u.databaseRecord));
4041 else if (zrs->which == Z_Records_multipleNSD)
4043 logf (LOG_DEBUG, "multipleNonSurrogateDiagnostic %d",
4044 zrs->u.multipleNonSurDiagnostics->num_diagRecs);
4045 setobj->numberOfRecordsReturned = 0;
4046 ir_handleDiags (&setobj->nonSurrogateDiagnosticList,
4047 &setobj->nonSurrogateDiagnosticNum,
4048 zrs->u.multipleNonSurDiagnostics->diagRecs,
4049 zrs->u.multipleNonSurDiagnostics->num_diagRecs);
4054 Z_DiagRec dr, *dr_p = &dr;
4055 dr.which = Z_DiagRec_defaultFormat;
4056 dr.u.defaultFormat = zrs->u.nonSurrogateDiagnostic;
4058 Z_DiagRec *dr_p = zrs->u.nonSurrogateDiagnostic;
4060 logf (LOG_DEBUG, "NonSurrogateDiagnostic");
4062 setobj->numberOfRecordsReturned = 0;
4063 ir_handleDiags (&setobj->nonSurrogateDiagnosticList,
4064 &setobj->nonSurrogateDiagnosticNum,
4069 static void ir_searchResponse (void *o, Z_SearchResponse *searchrs,
4070 IrTcl_SetObj *setobj)
4072 Z_Records *zrs = searchrs->records;
4074 logf (LOG_DEBUG, "Received search response");
4077 logf (LOG_DEBUG, "Search response, no object!");
4080 setobj->searchStatus = *searchrs->searchStatus;
4081 get_referenceId (&setobj->set_inher.referenceId, searchrs->referenceId);
4082 setobj->resultCount = *searchrs->resultCount;
4083 if (searchrs->presentStatus)
4084 setobj->presentStatus = *searchrs->presentStatus;
4086 setobj->presentStatus = Z_RES_NONE;
4087 if (searchrs->nextResultSetPosition)
4088 setobj->nextResultSetPosition = *searchrs->nextResultSetPosition;
4090 logf (LOG_DEBUG, "status %d hits %d",
4091 setobj->searchStatus, setobj->resultCount);
4095 if (setobj->resultCount <= setobj->set_inher.smallSetUpperBound)
4096 es = setobj->set_inher.smallSetElementSetNames;
4098 es = setobj->set_inher.mediumSetElementSetNames;
4099 setobj->numberOfRecordsReturned = *searchrs->numberOfRecordsReturned;
4100 ir_handleZRecords (o, zrs, setobj, es);
4104 setobj->numberOfRecordsReturned = 0;
4105 setobj->recordFlag = 0;
4110 static void ir_presentResponse (void *o, Z_PresentResponse *presrs,
4111 IrTcl_SetObj *setobj)
4113 Z_Records *zrs = presrs->records;
4115 logf (LOG_DEBUG, "Received present response");
4118 logf (LOG_DEBUG, "Present response, no object!");
4121 setobj->presentStatus = *presrs->presentStatus;
4122 get_referenceId (&setobj->set_inher.referenceId, presrs->referenceId);
4123 setobj->nextResultSetPosition = *presrs->nextResultSetPosition;
4126 setobj->numberOfRecordsReturned = *presrs->numberOfRecordsReturned;
4127 ir_handleZRecords (o, zrs, setobj, setobj->set_inher.elementSetNames);
4131 setobj->numberOfRecordsReturned = 0;
4132 setobj->recordFlag = 0;
4133 logf (LOG_DEBUG, "No records!");
4137 static void ir_scanResponse (void *o, Z_ScanResponse *scanrs,
4138 IrTcl_ScanObj *scanobj)
4142 logf (LOG_DEBUG, "Received scanResponse");
4144 get_referenceId (&p->set_inher.referenceId, scanrs->referenceId);
4145 scanobj->scanStatus = *scanrs->scanStatus;
4146 logf (LOG_DEBUG, "scanStatus=%d", scanobj->scanStatus);
4148 if (scanrs->stepSize)
4149 scanobj->stepSize = *scanrs->stepSize;
4150 logf (LOG_DEBUG, "stepSize=%d", scanobj->stepSize);
4152 scanobj->numberOfEntriesReturned = *scanrs->numberOfEntriesReturned;
4153 logf (LOG_DEBUG, "numberOfEntriesReturned=%d",
4154 scanobj->numberOfEntriesReturned);
4156 if (scanrs->positionOfTerm)
4157 scanobj->positionOfTerm = *scanrs->positionOfTerm;
4159 scanobj->positionOfTerm = -1;
4160 logf (LOG_DEBUG, "positionOfTerm=%d", scanobj->positionOfTerm);
4162 xfree (scanobj->entries);
4163 scanobj->entries = NULL;
4164 scanobj->num_entries = 0;
4165 scanobj->entries_flag = 0;
4167 ir_deleteDiags (&scanobj->nonSurrogateDiagnosticList,
4168 &scanobj->nonSurrogateDiagnosticNum);
4169 if (scanrs->entries)
4174 scanobj->entries_flag = 1;
4175 if (scanrs->entries)
4177 scanobj->num_entries = scanrs->entries->num_entries;
4178 scanobj->entries = ir_tcl_malloc (scanobj->num_entries *
4179 sizeof(*scanobj->entries));
4180 ze = scanrs->entries->entries;
4182 for (i=0; i<scanobj->num_entries; i++, ze++)
4184 scanobj->entries[i].which = (*ze)->which;
4185 switch ((*ze)->which)
4187 case Z_Entry_termInfo:
4188 if ((*ze)->u.termInfo->term->which == Z_Term_general)
4190 int l = (*ze)->u.termInfo->term->u.general->len;
4191 scanobj->entries[i].u.term.buf = ir_tcl_malloc (1+l);
4192 memcpy (scanobj->entries[i].u.term.buf,
4193 (*ze)->u.termInfo->term->u.general->buf,
4195 scanobj->entries[i].u.term.buf[l] = '\0';
4198 scanobj->entries[i].u.term.buf = NULL;
4199 if ((*ze)->u.termInfo->globalOccurrences)
4200 scanobj->entries[i].u.term.globalOccurrences =
4201 *(*ze)->u.termInfo->globalOccurrences;
4203 scanobj->entries[i].u.term.globalOccurrences = 0;
4205 case Z_Entry_surrogateDiagnostic:
4206 ir_handleDiags (&scanobj->entries[i].u.diag.list,
4207 &scanobj->entries[i].u.diag.num,
4208 &(*ze)->u.surrogateDiagnostic,
4213 if (scanrs->entries->nonsurrogateDiagnostics)
4214 ir_handleDiags (&scanobj->nonSurrogateDiagnosticList,
4215 &scanobj->nonSurrogateDiagnosticNum,
4216 scanrs->entries->nonsurrogateDiagnostics,
4217 scanrs->entries->num_nonsurrogateDiagnostics);
4222 static void ir_sortResponse (void *o, Z_SortResponse *sortrs,
4223 IrTcl_SetObj *setobj)
4227 logf (LOG_DEBUG, "Received sortResponse");
4232 purge_IR_records (setobj);
4234 get_referenceId (&p->set_inher.referenceId, sortrs->referenceId);
4236 setobj->sortStatus = *sortrs->sortStatus;
4238 ir_deleteDiags (&setobj->nonSurrogateDiagnosticList,
4239 &setobj->nonSurrogateDiagnosticNum);
4241 if (sortrs->diagnostics)
4242 ir_handleDiags (&setobj->nonSurrogateDiagnosticList,
4243 &setobj->nonSurrogateDiagnosticNum,
4244 sortrs->diagnostics,
4245 sortrs->num_diagnostics);
4247 if (sortrs->diagnostics)
4248 ir_handleDiags (&setobj->nonSurrogateDiagnosticList,
4249 &setobj->nonSurrogateDiagnosticNum,
4250 sortrs->diagnostics->diagRecs,
4251 sortrs->diagnostics->num_diagRecs);
4256 * ir_select_read: handle incoming packages
4258 static void ir_select_read (ClientData clientData)
4260 IrTcl_Obj *p = (IrTcl_Obj *) clientData;
4265 Tcl_CmdInfo cmd_info;
4266 const char *apdu_call;
4268 logf(LOG_DEBUG, "Read handler fd=%d", cs_fileno(p->cs_link));
4269 if (p->state == IR_TCL_R_Connecting)
4271 logf(LOG_DEBUG, "read: connect");
4272 r = cs_rcvconnect (p->cs_link);
4275 logf (LOG_WARN, "cs_rcvconnect returned 1");
4278 p->state = IR_TCL_R_Idle;
4280 ir_select_remove_write (cs_fileno (p->cs_link), p);
4283 logf (LOG_DEBUG, "cs_rcvconnect error");
4284 ir_tcl_disconnect (p);
4287 p->failInfo = IR_TCL_FAIL_CONNECT;
4288 ir_tcl_eval (p->interp, p->failback);
4290 ir_obj_delete ((ClientData) p);
4294 ir_tcl_eval (p->interp, p->callback);
4295 if (p->ref_count == 2 && p->cs_link && p->request_queue
4296 && p->state == IR_TCL_R_Idle)
4297 ir_tcl_send_q (p, p->request_queue, "x");
4298 ir_obj_delete ((ClientData) p);
4303 p->state = IR_TCL_R_Reading;
4305 /* read incoming APDU */
4306 if ((r=cs_get (p->cs_link, &p->buf_in, &p->len_in)) == 1)
4308 logf(LOG_DEBUG, "PDU Fraction read");
4311 /* signal one more use of ir object - callbacks must not
4312 release the ir memory (p pointer) */
4316 logf (LOG_DEBUG, "cs_get failed, code %d", r);
4317 ir_tcl_disconnect (p);
4320 p->failInfo = IR_TCL_FAIL_READ;
4321 ir_tcl_eval (p->interp, p->failback);
4323 /* release ir object now if callback deleted it */
4324 ir_obj_delete ((ClientData) p);
4327 /* got complete APDU. Now decode */
4330 odr_setbuf (p->odr_in, p->buf_in, r, 0);
4331 logf (LOG_DEBUG, "cs_get ok, total size %d", r);
4332 if (!z_APDU (p->odr_in, &apdu, 0, 0))
4334 logf (LOG_DEBUG, "cs_get failed: %s",
4335 odr_errmsg (odr_geterror (p->odr_in)));
4336 ir_tcl_disconnect (p);
4339 p->failInfo = IR_TCL_FAIL_IN_APDU;
4340 p->apduOffset = odr_offset (p->odr_in);
4341 ir_tcl_eval (p->interp, p->failback);
4343 /* release ir object now if failback deleted it */
4344 ir_obj_delete ((ClientData) p);
4347 /* handle APDU and invoke callback */
4348 rq = p->request_queue;
4351 logf (LOG_FATAL, "Internal error. No queue entry");
4354 object_name = rq->object_name;
4355 logf (LOG_DEBUG, "Object %s", object_name);
4357 if (Tcl_GetCommandInfo (p->interp, object_name, &cmd_info))
4361 case Z_APDU_initResponse:
4362 p->eventType = "init";
4363 ir_initResponse (p, apdu->u.initResponse);
4364 apdu_call = p->initResponse;
4366 case Z_APDU_searchResponse:
4367 p->eventType = "search";
4368 ir_searchResponse (p, apdu->u.searchResponse,
4369 (IrTcl_SetObj *) cmd_info.clientData);
4370 apdu_call = ((IrTcl_SetObj *)
4371 cmd_info.clientData)->searchResponse;
4373 case Z_APDU_presentResponse:
4374 p->eventType = "present";
4375 ir_presentResponse (p, apdu->u.presentResponse,
4376 (IrTcl_SetObj *) cmd_info.clientData);
4377 apdu_call = ((IrTcl_SetObj *)
4378 cmd_info.clientData)->presentResponse;
4380 case Z_APDU_scanResponse:
4381 p->eventType = "scan";
4382 ir_scanResponse (p, apdu->u.scanResponse,
4383 (IrTcl_ScanObj *) cmd_info.clientData);
4384 apdu_call = ((IrTcl_ScanObj *)
4385 cmd_info.clientData)->scanResponse;
4387 case Z_APDU_sortResponse:
4388 p->eventType = "sort";
4389 ir_sortResponse (p, apdu->u.sortResponse,
4390 (IrTcl_SetObj *) cmd_info.clientData);
4391 apdu_call = ((IrTcl_SetObj *)
4392 cmd_info.clientData)->sortResponse;
4395 logf (LOG_WARN, "Received unknown APDU type (%d)",
4397 ir_tcl_disconnect (p);
4400 p->failInfo = IR_TCL_FAIL_UNKNOWN_APDU;
4401 ir_tcl_eval (p->interp, p->failback);
4406 p->request_queue = rq->next;
4407 p->state = IR_TCL_R_Idle;
4410 ir_tcl_eval (p->interp, apdu_call);
4411 else if (rq->callback)
4412 ir_tcl_eval (p->interp, rq->callback);
4413 xfree (rq->buf_out);
4414 xfree (rq->callback);
4415 xfree (rq->object_name);
4417 odr_reset (p->odr_in);
4418 if (p->ref_count == 1)
4420 ir_obj_delete ((ClientData) p);
4423 ir_obj_delete ((ClientData) p);
4424 } while (p->cs_link && cs_more (p->cs_link));
4425 if (p->cs_link && p->request_queue && p->state == IR_TCL_R_Idle)
4426 ir_tcl_send_q (p, p->request_queue, "x");
4430 * ir_select_write: handle outgoing packages - not yet written.
4432 static int ir_select_write (ClientData clientData)
4434 IrTcl_Obj *p = (IrTcl_Obj *) clientData;
4438 logf (LOG_DEBUG, "Write handler fd=%d", cs_fileno(p->cs_link));
4439 if (p->state == IR_TCL_R_Connecting)
4441 logf(LOG_DEBUG, "write: connect");
4442 r = cs_rcvconnect (p->cs_link);
4445 logf (LOG_DEBUG, "cs_rcvconnect returned 1");
4448 p->state = IR_TCL_R_Idle;
4450 ir_select_remove_write (cs_fileno (p->cs_link), p);
4453 logf (LOG_DEBUG, "cs_rcvconnect error");
4454 ir_tcl_disconnect (p);
4457 p->failInfo = IR_TCL_FAIL_CONNECT;
4458 ir_tcl_eval (p->interp, p->failback);
4460 ir_obj_delete ((ClientData) p);
4464 ir_tcl_eval (p->interp, p->callback);
4465 ir_obj_delete ((ClientData) p);
4468 rq = p->request_queue;
4469 if (!rq || !rq->buf_out)
4472 if ((r=cs_put (p->cs_link, rq->buf_out, rq->len_out)) < 0)
4474 logf (LOG_DEBUG, "cs_put write fail");
4476 xfree (rq->buf_out);
4478 ir_tcl_disconnect (p);
4481 p->failInfo = IR_TCL_FAIL_WRITE;
4482 ir_tcl_eval (p->interp, p->failback);
4484 ir_obj_delete ((ClientData) p);
4486 else if (r == 0) /* remove select bit */
4488 logf (LOG_DEBUG, "Write completed");
4489 p->state = IR_TCL_R_Waiting;
4490 ir_select_remove_write (cs_fileno (p->cs_link), p);
4491 xfree (rq->buf_out);
4497 static void ir_select_notify (ClientData clientData, int r, int w, int e)
4501 if (!ir_select_write (clientData) && r)
4502 ir_select_read (clientData);
4506 ir_select_read (clientData);
4510 /*----------------------------------------------------------- */
4514 * This wrapper function is used by Windows to invoke the
4515 * initialization code for the DLL. If we are compiling
4516 * with Visual C++, this routine will be renamed to DllMain.
4528 DllEntryPoint(hInst, reason, reserved)
4529 HINSTANCE hInst; /* Library instance handle. */
4530 DWORD reason; /* Reason this function is being called. */
4531 LPVOID reserved; /* Not used. */
4537 /* ------------------------------------------------------- */
4539 * Irtcl_init: Registration of TCL commands.
4541 EXPORT (int,Irtcl_Init) (Tcl_Interp *interp)
4543 Tcl_CreateCommand (interp, "ir", ir_obj_mk, (ClientData) NULL,
4544 (Tcl_CmdDeleteProc *) NULL);
4545 Tcl_CreateCommand (interp, "ir-set", ir_set_obj_mk,
4546 (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
4547 Tcl_CreateCommand (interp, "ir-scan", ir_scan_obj_mk,
4548 (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
4549 Tcl_CreateCommand (interp, "ir-log-init", ir_log_init_proc,
4550 (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
4551 Tcl_CreateCommand (interp, "ir-log", ir_log_proc,
4552 (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
4553 Tcl_CreateCommand (interp, "ir-version", ir_version, (ClientData) NULL,
4554 (Tcl_CmdDeleteProc *) NULL);