+ IrTcl_Obj *p = obj;
+
+ if (argc == 0)
+ {
+ p->targetImplementationVersion = NULL;
+ return TCL_OK;
+ }
+ else if (argc == -1)
+ return ir_tcl_strdel (interp, &p->targetImplementationVersion);
+ Tcl_AppendResult (interp, p->targetImplementationVersion, (char*) NULL);
+ return TCL_OK;
+}
+
+/*
+ * do_idAuthentication: Set/get id Authentication
+ */
+static int do_idAuthentication (void *obj, Tcl_Interp *interp,
+ int argc, char **argv)
+{
+ IrTcl_Obj *p = obj;
+
+ if (argc >= 3 || argc == -1)
+ {
+ free (p->idAuthenticationOpen);
+ free (p->idAuthenticationGroupId);
+ free (p->idAuthenticationUserId);
+ free (p->idAuthenticationPassword);
+ }
+ if (argc >= 3 || argc <= 0)
+ {
+ p->idAuthenticationOpen = NULL;
+ p->idAuthenticationGroupId = NULL;
+ p->idAuthenticationUserId = NULL;
+ p->idAuthenticationPassword = NULL;
+ }
+ if (argc <= 0)
+ return TCL_OK;
+ if (argc >= 3)
+ {
+ if (argc == 3)
+ {
+ if (argv[2][0] &&
+ ir_tcl_strdup (interp, &p->idAuthenticationOpen, argv[2])
+ == TCL_ERROR)
+ return TCL_ERROR;
+ }
+ else if (argc == 5)
+ {
+ if (argv[2][0] &&
+ ir_tcl_strdup (interp, &p->idAuthenticationGroupId, argv[2])
+ == TCL_ERROR)
+ return TCL_ERROR;
+ if (argv[3][0] &&
+ ir_tcl_strdup (interp, &p->idAuthenticationUserId, argv[3])
+ == TCL_ERROR)
+ return TCL_ERROR;
+ if (argv[4][0] &&
+ ir_tcl_strdup (interp, &p->idAuthenticationPassword, argv[4])
+ == TCL_ERROR)
+ return TCL_ERROR;
+ }
+ }
+ if (p->idAuthenticationOpen)
+ Tcl_AppendElement (interp, p->idAuthenticationOpen);
+ else if (p->idAuthenticationGroupId)
+ {
+ Tcl_AppendElement (interp, p->idAuthenticationGroupId);
+ Tcl_AppendElement (interp, p->idAuthenticationUserId);
+ Tcl_AppendElement (interp, p->idAuthenticationPassword);
+ }
+ return TCL_OK;
+}
+
+/*
+ * do_connect: connect method on IR object
+ */
+static int do_connect (void *obj, Tcl_Interp *interp,
+ int argc, char **argv)
+{
+ void *addr;
+ IrTcl_Obj *p = obj;
+ int r;
+
+ if (argc <= 0)
+ return TCL_OK;
+ if (argc == 3)
+ {
+ if (p->hostname)
+ {
+ interp->result = "already connected";
+ return TCL_ERROR;
+ }
+ if (!strcmp (p->comstackType, "tcpip"))
+ {
+ p->cs_link = cs_create (tcpip_type, CS_BLOCK, p->protocol_type);
+ addr = tcpip_strtoaddr (argv[2]);
+ if (!addr)
+ {
+ interp->result = "tcpip_strtoaddr fail";
+ return TCL_ERROR;
+ }
+ logf (LOG_DEBUG, "tcp/ip connect %s", argv[2]);
+ }
+ else if (!strcmp (p->comstackType, "mosi"))
+ {
+#if MOSI
+ p->cs_link = cs_create (mosi_type, CS_BLOCK, p->protocol_type);
+ addr = mosi_strtoaddr (argv[2]);
+ if (!addr)
+ {
+ interp->result = "mosi_strtoaddr fail";
+ return TCL_ERROR;
+ }
+ logf (LOG_DEBUG, "mosi connect %s", argv[2]);
+#else
+ interp->result = "MOSI support not there";
+ return TCL_ERROR;
+#endif
+ }
+ else
+ {
+ Tcl_AppendResult (interp, "Bad comstack type: ",
+ p->comstackType, NULL);
+ return TCL_ERROR;
+ }
+ if (ir_tcl_strdup (interp, &p->hostname, argv[2]) == TCL_ERROR)
+ return TCL_ERROR;
+#if IRTCL_GENERIC_FILES
+#ifdef WINDOWS
+ p->csFile = Tcl_GetFile (cs_fileno(p->cs_link), TCL_WIN_SOCKET);
+#else
+ p->csFile = Tcl_GetFile (cs_fileno(p->cs_link), TCL_UNIX_FD);
+#endif
+#endif
+ if ((r=cs_connect (p->cs_link, addr)) < 0)
+ {
+ interp->result = "connect fail";
+ do_disconnect (p, NULL, 2, NULL);
+ return TCL_ERROR;
+ }
+ logf(LOG_DEBUG, "cs_connect() returned %d fd=%d", r,
+ cs_fileno(p->cs_link));
+ p->eventType = "connect";
+#if IRTCL_GENERIC_FILES
+ ir_select_add (p->csFile, p);
+#else
+ ir_select_add (cs_fileno (p->cs_link), p);
+#endif
+ if (r == 1)
+ {
+#if IRTCL_GENERIC_FILES
+ ir_select_add_write (p->csFile, p);
+#else
+ ir_select_add_write (cs_fileno (p->cs_link), p);
+#endif
+ p->state = IR_TCL_R_Connecting;
+ }
+ else
+ {
+ p->state = IR_TCL_R_Idle;
+ if (p->callback)
+ IrTcl_eval (p->interp, p->callback);
+ }
+ }
+ else
+ Tcl_AppendResult (interp, p->hostname, NULL);
+ return TCL_OK;
+}
+
+/*
+ * do_disconnect: disconnect method on IR object
+ */
+static int do_disconnect (void *obj, Tcl_Interp *interp,
+ int argc, char **argv)
+{
+ IrTcl_Obj *p = obj;
+
+ if (argc == 0)
+ {
+ p->state = IR_TCL_R_Idle;
+ p->eventType = NULL;
+ p->hostname = NULL;
+ p->cs_link = NULL;
+#if IRTCL_GENERIC_FILES
+ p->csFile = 0;
+#endif
+ return TCL_OK;
+ }
+ if (p->hostname)
+ {
+ logf(LOG_DEBUG, "Closing connection to %s", p->hostname);
+ free (p->hostname);
+ p->hostname = NULL;
+#if IRTCL_GENERIC_FILES
+ ir_select_remove_write (p->csFile, p);
+ ir_select_remove (p->csFile, p);
+#else
+ ir_select_remove_write (cs_fileno (p->cs_link), p);
+ ir_select_remove (cs_fileno (p->cs_link), p);
+#endif
+
+ odr_reset (p->odr_in);
+
+ assert (p->cs_link);
+ cs_close (p->cs_link);
+ p->cs_link = NULL;
+#if IRTCL_GENERIC_FILES
+ Tcl_FreeFile (p->csFile);
+ p->csFile = NULL;
+#endif
+
+ ODR_MASK_ZERO (&p->options);
+ ODR_MASK_SET (&p->options, 0);
+ ODR_MASK_SET (&p->options, 1);
+ ODR_MASK_SET (&p->options, 4);
+ ODR_MASK_SET (&p->options, 7);
+ ODR_MASK_SET (&p->options, 14);
+
+ ODR_MASK_ZERO (&p->protocolVersion);
+ ODR_MASK_SET (&p->protocolVersion, 0);
+ ODR_MASK_SET (&p->protocolVersion, 1);
+ ir_tcl_del_q (p);
+ }
+ assert (!p->cs_link);
+ return TCL_OK;
+}
+
+/*
+ * do_comstack: Set/get comstack method on IR object
+ */
+static int do_comstack (void *o, Tcl_Interp *interp,
+ int argc, char **argv)
+{
+ IrTcl_Obj *obj = o;
+
+ if (argc == 0)
+ return ir_tcl_strdup (interp, &obj->comstackType, "tcpip");
+ else if (argc == -1)
+ return ir_tcl_strdel (interp, &obj->comstackType);
+ else if (argc == 3)
+ {
+ free (obj->comstackType);
+ if (ir_tcl_strdup (interp, &obj->comstackType, argv[2]) == TCL_ERROR)
+ return TCL_ERROR;
+ }
+ Tcl_AppendElement (interp, obj->comstackType);
+ return TCL_OK;
+}
+
+/*
+ * do_logLevel: Set log level
+ */
+static int do_logLevel (void *o, Tcl_Interp *interp,
+ int argc, char **argv)
+{
+ if (argc <= 2)
+ return TCL_OK;
+ if (argc == 3)
+ log_init (log_mask_str (argv[2]), "", NULL);
+ else if (argc == 4)
+ log_init (log_mask_str (argv[2]), argv[3], NULL);
+ else if (argc == 5)
+ log_init (log_mask_str (argv[2]), argv[3], argv[4]);
+ return TCL_OK;
+}
+
+
+/*
+ * do_eventType: Return type of last event
+ */
+static int do_eventType (void *obj, Tcl_Interp *interp,
+ int argc, char **argv)
+{
+ IrTcl_Obj *p = obj;
+
+ if (argc <= 0)
+ {
+ p->eventType = NULL;
+ return TCL_OK;
+ }
+ Tcl_AppendElement (interp, p->eventType ? p->eventType : "");
+ return TCL_OK;
+}
+
+
+/*
+ * do_callback: add callback
+ */
+static int do_callback (void *obj, Tcl_Interp *interp,
+ int argc, char **argv)
+{
+ IrTcl_Obj *p = obj;
+
+ if (argc == 0)
+ {
+ p->callback = NULL;
+ return TCL_OK;
+ }
+ else if (argc == -1)
+ return ir_tcl_strdel (interp, &p->callback);
+ if (argc == 3)
+ {
+ free (p->callback);
+ if (argv[2][0])
+ {
+ if (ir_tcl_strdup (interp, &p->callback, argv[2]) == TCL_ERROR)
+ return TCL_ERROR;
+ }
+ else
+ p->callback = NULL;
+ }
+ return TCL_OK;
+}
+
+/*
+ * do_failback: add error handle callback
+ */
+static int do_failback (void *obj, Tcl_Interp *interp,
+ int argc, char **argv)
+{
+ IrTcl_Obj *p = obj;
+
+ if (argc == 0)
+ {
+ p->failback = NULL;
+ return TCL_OK;
+ }
+ else if (argc == -1)
+ return ir_tcl_strdel (interp, &p->failback);
+ else if (argc == 3)
+ {
+ free (p->failback);
+ if (argv[2][0])
+ {
+ if (ir_tcl_strdup (interp, &p->failback, argv[2]) == TCL_ERROR)
+ return TCL_ERROR;
+ }
+ else
+ p->failback = NULL;
+ }
+ return TCL_OK;
+}
+
+/*
+ * do_initResponse: add init response handler
+ */
+static int do_initResponse (void *obj, Tcl_Interp *interp,
+ int argc, char **argv)
+{
+ IrTcl_Obj *p = obj;
+
+ if (argc == 0)
+ {
+ p->initResponse = NULL;
+ return TCL_OK;
+ }
+ else if (argc == -1)
+ return ir_tcl_strdel (interp, &p->initResponse);
+ if (argc == 3)
+ {
+ free (p->initResponse);
+ if (argv[2][0])
+ {
+ if (ir_tcl_strdup (interp, &p->initResponse, argv[2]) == TCL_ERROR)
+ return TCL_ERROR;
+ }
+ else
+ p->initResponse = NULL;
+ }
+ return TCL_OK;
+}
+/*
+ * do_protocol: Set/get protocol method on IR object
+ */
+static int do_protocol (void *o, Tcl_Interp *interp, int argc, char **argv)
+{
+ IrTcl_Obj *p = o;
+
+ if (argc <= 0)
+ {
+ p->protocol_type = PROTO_Z3950;
+ return TCL_OK;
+ }
+ else if (argc == 3)
+ {
+ if (!strcmp (argv[2], "Z39"))
+ p->protocol_type = PROTO_Z3950;
+ else if (!strcmp (argv[2], "SR"))
+ p->protocol_type = PROTO_SR;
+ else
+ {
+ Tcl_AppendResult (interp, "Bad protocol: ", argv[2], NULL);
+ return TCL_ERROR;
+ }
+ return TCL_OK;
+ }
+ switch (p->protocol_type)
+ {
+ case PROTO_Z3950:
+ Tcl_AppendElement (interp, "Z39");
+ break;
+ case PROTO_SR:
+ Tcl_AppendElement (interp, "SR");
+ break;
+ }
+ return TCL_OK;
+}
+
+/*
+ * do_triggerResourceControl:
+ */
+static int do_triggerResourceControl (void *obj, Tcl_Interp *interp,
+ int argc, char **argv)
+{
+ IrTcl_Obj *p = obj;
+ Z_APDU *apdu;
+ Z_TriggerResourceControlRequest *req;
+ bool_t is_false = 0;
+
+ if (argc <= 0)
+ return TCL_OK;
+ if (!p->cs_link)
+ {
+ interp->result = "triggerResourceControl: not connected";
+ return TCL_ERROR;
+ }
+ apdu = zget_APDU (p->odr_out, Z_APDU_triggerResourceControlRequest);
+ req = apdu->u.triggerResourceControlRequest;
+ *req->requestedAction = Z_TriggerResourceCtrl_cancel;
+ req->resultSetWanted = &is_false;
+
+ return ir_tcl_send_APDU (interp, p, apdu, "triggerResourceControl",
+ argv[0]);
+}
+
+/*
+ * do_databaseNames: specify database names
+ */
+static int do_databaseNames (void *obj, Tcl_Interp *interp,
+ int argc, char **argv)
+{
+ int i;
+ IrTcl_SetCObj *p = obj;
+
+ if (argc == -1)
+ {
+ for (i=0; i<p->num_databaseNames; i++)
+ free (p->databaseNames[i]);
+ free (p->databaseNames);
+ }
+ if (argc <= 0)
+ {
+ p->num_databaseNames = 0;
+ p->databaseNames = NULL;
+ return TCL_OK;
+ }
+ if (argc < 3)
+ {
+ for (i=0; i<p->num_databaseNames; i++)
+ Tcl_AppendElement (interp, p->databaseNames[i]);
+ return TCL_OK;
+ }
+ if (p->databaseNames)
+ {
+ for (i=0; i<p->num_databaseNames; i++)
+ free (p->databaseNames[i]);
+ free (p->databaseNames);
+ }
+ p->num_databaseNames = argc - 2;
+ p->databaseNames =
+ ir_tcl_malloc (sizeof(*p->databaseNames) * p->num_databaseNames);
+ for (i=0; i<p->num_databaseNames; i++)
+ {
+ if (ir_tcl_strdup (interp, &p->databaseNames[i], argv[2+i])
+ == TCL_ERROR)
+ return TCL_ERROR;
+ }
+ return TCL_OK;
+}
+
+/*
+ * do_replaceIndicator: Set/get replace Set indicator
+ */
+static int do_replaceIndicator (void *obj, Tcl_Interp *interp,
+ int argc, char **argv)
+{
+ IrTcl_SetCObj *p = obj;
+
+ if (argc <= 0)
+ {
+ p->replaceIndicator = 1;
+ return TCL_OK;
+ }
+ return get_set_int (&p->replaceIndicator, interp, argc, argv);
+}
+
+/*
+ * do_queryType: Set/Get query method
+ */
+static int do_queryType (void *obj, Tcl_Interp *interp,
+ int argc, char **argv)
+{
+ IrTcl_SetCObj *p = obj;
+
+ if (argc == 0)
+ return ir_tcl_strdup (interp, &p->queryType, "rpn");
+ else if (argc == -1)
+ return ir_tcl_strdel (interp, &p->queryType);
+ if (argc == 3)
+ {
+ free (p->queryType);
+ if (ir_tcl_strdup (interp, &p->queryType, argv[2]) == TCL_ERROR)
+ return TCL_ERROR;
+ }
+ Tcl_AppendResult (interp, p->queryType, NULL);
+ return TCL_OK;
+}
+
+/*
+ * do_userInformationField: Get User information field
+ */
+static int do_userInformationField (void *obj, Tcl_Interp *interp,
+ int argc, char **argv)
+{
+ IrTcl_Obj *p = obj;
+
+ if (argc == 0)
+ {
+ p->userInformationField = NULL;
+ return TCL_OK;
+ }
+ else if (argc == -1)
+ return ir_tcl_strdel (interp, &p->userInformationField);
+ Tcl_AppendResult (interp, p->userInformationField, NULL);
+ return TCL_OK;
+}
+
+/*
+ * do_smallSetUpperBound: Set/get small set upper bound
+ */
+static int do_smallSetUpperBound (void *o, Tcl_Interp *interp,
+ int argc, char **argv)
+{
+ IrTcl_SetCObj *p = o;
+
+ if (argc <= 0)
+ {
+ p->smallSetUpperBound = 0;
+ return TCL_OK;
+ }
+ return get_set_int (&p->smallSetUpperBound, interp, argc, argv);
+}
+
+/*
+ * do_largeSetLowerBound: Set/get large set lower bound
+ */
+static int do_largeSetLowerBound (void *o, Tcl_Interp *interp,
+ int argc, char **argv)
+{
+ IrTcl_SetCObj *p = o;
+
+ if (argc <= 0)
+ {
+ p->largeSetLowerBound = 2;
+ return TCL_OK;
+ }
+ return get_set_int (&p->largeSetLowerBound, interp, argc, argv);
+}
+
+/*
+ * do_mediumSetPresentNumber: Set/get large set lower bound
+ */
+static int do_mediumSetPresentNumber (void *o, Tcl_Interp *interp,
+ int argc, char **argv)
+{
+ IrTcl_SetCObj *p = o;
+
+ if (argc <= 0)
+ {
+ p->mediumSetPresentNumber = 0;
+ return TCL_OK;
+ }
+ return get_set_int (&p->mediumSetPresentNumber, interp, argc, argv);
+}
+
+/*
+ * do_referenceId: Set/Get referenceId
+ */
+static int do_referenceId (void *obj, Tcl_Interp *interp,
+ int argc, char **argv)
+{
+ IrTcl_SetCObj *p = obj;
+
+ if (argc == 0)
+ {
+ p->referenceId = NULL;
+ return TCL_OK;
+ }
+ else if (argc == -1)
+ return ir_tcl_strdel (interp, &p->referenceId);
+ if (argc == 3)
+ {
+ free (p->referenceId);
+ if (ir_tcl_strdup (interp, &p->referenceId, argv[2]) == TCL_ERROR)
+ return TCL_ERROR;
+ }
+ Tcl_AppendResult (interp, p->referenceId, NULL);
+ return TCL_OK;
+}
+
+/*
+ * do_preferredRecordSyntax: Set/get preferred record syntax
+ */
+static int do_preferredRecordSyntax (void *obj, Tcl_Interp *interp,
+ int argc, char **argv)
+{
+ IrTcl_SetCObj *p = obj;
+
+ if (argc == 0)
+ {
+ p->preferredRecordSyntax = NULL;
+ return TCL_OK;
+ }
+ else if (argc == -1)
+ {
+ free (p->preferredRecordSyntax);
+ p->preferredRecordSyntax = NULL;
+ return TCL_OK;
+ }
+ if (argc == 3)
+ {
+ free (p->preferredRecordSyntax);
+ p->preferredRecordSyntax = NULL;
+ if (argv[2][0] && (p->preferredRecordSyntax =
+ ir_tcl_malloc (sizeof(*p->preferredRecordSyntax))))
+ *p->preferredRecordSyntax = IrTcl_getRecordSyntaxVal (argv[2]);
+ }
+ return TCL_OK;
+
+}
+
+/*
+ * do_elementSetNames: Set/Get element Set Names
+ */
+static int do_elementSetNames (void *obj, Tcl_Interp *interp,
+ int argc, char **argv)
+{
+ IrTcl_SetCObj *p = obj;
+
+ if (argc == 0)
+ {
+ p->elementSetNames = NULL;
+ return TCL_OK;
+ }
+ else if (argc == -1)
+ return ir_tcl_strdel (interp, &p->elementSetNames);
+ if (argc == 3)
+ {
+ free (p->elementSetNames);
+ if (ir_tcl_strdup (interp, &p->elementSetNames, argv[2]) == TCL_ERROR)
+ return TCL_ERROR;
+ }
+ Tcl_AppendResult (interp, p->elementSetNames, NULL);
+ return TCL_OK;
+}
+
+/*
+ * do_smallSetElementSetNames: Set/Get small Set Element Set Names
+ */
+static int do_smallSetElementSetNames (void *obj, Tcl_Interp *interp,
+ int argc, char **argv)
+{
+ IrTcl_SetCObj *p = obj;
+
+ if (argc == 0)
+ {
+ p->smallSetElementSetNames = NULL;
+ return TCL_OK;
+ }
+ else if (argc == -1)
+ return ir_tcl_strdel (interp, &p->smallSetElementSetNames);
+ if (argc == 3)
+ {
+ free (p->smallSetElementSetNames);
+ if (ir_tcl_strdup (interp, &p->smallSetElementSetNames,
+ argv[2]) == TCL_ERROR)
+ return TCL_ERROR;
+ }
+ Tcl_AppendResult (interp, p->smallSetElementSetNames, NULL);
+ return TCL_OK;
+}
+
+/*
+ * do_mediumSetElementSetNames: Set/Get medium Set Element Set Names
+ */
+static int do_mediumSetElementSetNames (void *obj, Tcl_Interp *interp,
+ int argc, char **argv)
+{
+ IrTcl_SetCObj *p = obj;
+
+ if (argc == 0)
+ {
+ p->mediumSetElementSetNames = NULL;
+ return TCL_OK;
+ }
+ else if (argc == -1)
+ return ir_tcl_strdel (interp, &p->mediumSetElementSetNames);
+ if (argc == 3)
+ {
+ free (p->mediumSetElementSetNames);
+ if (ir_tcl_strdup (interp, &p->mediumSetElementSetNames,
+ argv[2]) == TCL_ERROR)
+ return TCL_ERROR;
+ }
+ Tcl_AppendResult (interp, p->mediumSetElementSetNames, NULL);
+ return TCL_OK;
+}
+
+static IrTcl_Method ir_method_tab[] = {
+{ 1, "comstack", do_comstack },
+{ 1, "protocol", do_protocol },
+{ 0, "failback", do_failback },
+{ 0, "failInfo", do_failInfo },
+{ 0, "apduInfo", do_apduInfo },
+{ 0, "logLevel", do_logLevel },
+
+{ 0, "eventType", do_eventType },
+{ 1, "connect", do_connect },
+{ 0, "protocolVersion", do_protocolVersion },
+{ 1, "preferredMessageSize", do_preferredMessageSize },
+{ 1, "maximumRecordSize", do_maximumRecordSize },
+{ 1, "implementationName", do_implementationName },
+{ 1, "implementationId", do_implementationId },
+{ 1, "implementationVersion", do_implementationVersion },
+{ 0, "targetImplementationName", do_targetImplementationName },
+{ 0, "targetImplementationId", do_targetImplementationId },
+{ 0, "targetImplementationVersion", do_targetImplementationVersion },
+{ 0, "userInformationField", do_userInformationField },
+{ 1, "idAuthentication", do_idAuthentication },
+{ 0, "options", do_options },
+{ 0, "init", do_init_request },
+{ 0, "initResult", do_initResult },
+{ 0, "disconnect", do_disconnect },
+{ 0, "callback", do_callback },
+{ 0, "initResponse", do_initResponse },
+{ 0, "triggerResourceControl", do_triggerResourceControl },
+{ 0, "initResponse", do_initResponse },
+{ 0, NULL, NULL}
+};
+
+static IrTcl_Method ir_set_c_method_tab[] = {
+{ 0, "databaseNames", do_databaseNames},
+{ 0, "replaceIndicator", do_replaceIndicator},
+{ 0, "queryType", do_queryType },
+{ 0, "preferredRecordSyntax", do_preferredRecordSyntax },
+{ 0, "smallSetUpperBound", do_smallSetUpperBound},
+{ 0, "largeSetLowerBound", do_largeSetLowerBound},
+{ 0, "mediumSetPresentNumber", do_mediumSetPresentNumber},
+{ 0, "referenceId", do_referenceId },
+{ 0, "elementSetNames", do_elementSetNames },
+{ 0, "smallSetElementSetNames", do_smallSetElementSetNames },
+{ 0, "mediumSetElementSetNames", do_mediumSetElementSetNames },
+{ 0, NULL, NULL}
+};
+
+/*
+ * ir_obj_method: IR Object methods
+ */
+static int ir_obj_method (ClientData clientData, Tcl_Interp *interp,
+ int argc, char **argv)
+{
+ IrTcl_Methods tab[3];
+ IrTcl_Obj *p = clientData;
+
+ if (argc < 2)
+ return ir_method_r (clientData, interp, argc, argv, ir_method_tab);
+
+ tab[0].tab = ir_method_tab;
+ tab[0].obj = p;
+ tab[1].tab = ir_set_c_method_tab;
+ tab[1].obj = &p->set_inher;
+ tab[2].tab = NULL;
+
+ return ir_method (interp, argc, argv, tab);
+}
+
+/*
+ * ir_obj_delete: IR Object disposal
+ */
+static void ir_obj_delete (ClientData clientData)
+{
+ IrTcl_Obj *obj = clientData;
+ IrTcl_Methods tab[3];
+
+ --(obj->ref_count);
+ if (obj->ref_count > 0)
+ return;
+ assert (obj->ref_count == 0);
+
+ logf (LOG_DEBUG, "ir object delete");
+ tab[0].tab = ir_method_tab;
+ tab[0].obj = obj;
+ tab[1].tab = ir_set_c_method_tab;
+ tab[1].obj = &obj->set_inher;
+ tab[2].tab = NULL;
+
+ ir_method (NULL, -1, NULL, tab);
+
+ ir_tcl_del_q (obj);
+ odr_destroy (obj->odr_in);
+ odr_destroy (obj->odr_out);
+ odr_destroy (obj->odr_pr);
+ free (obj);
+}
+
+/*
+ * ir_obj_mk: IR Object creation
+ */
+static int ir_obj_mk (ClientData clientData, Tcl_Interp *interp,
+ int argc, char **argv)
+{
+ IrTcl_Methods tab[3];
+ IrTcl_Obj *obj;
+#if CCL2RPN
+ FILE *inf;
+#endif
+
+ if (argc != 2)
+ {
+ interp->result = "wrong # args";
+ return TCL_ERROR;
+ }
+ obj = ir_tcl_malloc (sizeof(*obj));
+ obj->ref_count = 1;
+#if CCL2RPN
+ obj->bibset = ccl_qual_mk ();
+ if ((inf = fopen ("default.bib", "r")))
+ {
+ ccl_qual_file (obj->bibset, inf);
+ fclose (inf);
+ }
+#endif
+
+ logf (LOG_DEBUG, "ir object create %s", argv[1]);
+ obj->odr_in = odr_createmem (ODR_DECODE);
+ obj->odr_out = odr_createmem (ODR_ENCODE);
+ obj->odr_pr = odr_createmem (ODR_PRINT);
+ obj->state = IR_TCL_R_Idle;
+ obj->interp = interp;
+
+ obj->len_in = 0;
+ obj->buf_in = NULL;
+ obj->request_queue = NULL;
+
+ tab[0].tab = ir_method_tab;
+ tab[0].obj = obj;
+ tab[1].tab = ir_set_c_method_tab;
+ tab[1].obj = &obj->set_inher;
+ tab[2].tab = NULL;
+
+ if (ir_method (interp, 0, NULL, tab) == TCL_ERROR)
+ return TCL_ERROR;
+ Tcl_CreateCommand (interp, argv[1], ir_obj_method,
+ (ClientData) obj, ir_obj_delete);
+ return TCL_OK;
+}
+
+/* ------------------------------------------------------- */
+/*
+ * do_search: Do search request
+ */
+static int do_search (void *o, Tcl_Interp *interp, int argc, char **argv)
+{
+ Z_SearchRequest *req;
+ Z_Query query;
+ Z_APDU *apdu;
+ Odr_oct ccl_query;
+ IrTcl_SetObj *obj = o;
+ IrTcl_Obj *p;
+ int r;
+ oident bib1;
+
+ if (argc <= 0)
+ return TCL_OK;
+
+ p = obj->parent;
+ if (argc != 3)
+ {
+ interp->result = "wrong # args";
+ return TCL_ERROR;
+ }
+ if (!obj->set_inher.num_databaseNames)
+ {
+ interp->result = "no databaseNames";
+ return TCL_ERROR;
+ }
+ if (!p->cs_link)
+ {
+ interp->result = "search: not connected";
+ return TCL_ERROR;
+ }
+ apdu = zget_APDU (p->odr_out, Z_APDU_searchRequest);
+ req = apdu->u.searchRequest;
+
+ obj->start = 1;
+
+ bib1.proto = p->protocol_type;
+ bib1.oclass = CLASS_ATTSET;
+ bib1.value = VAL_BIB1;
+
+ set_referenceId (p->odr_out, &req->referenceId,
+ obj->set_inher.referenceId);
+
+ req->smallSetUpperBound = &obj->set_inher.smallSetUpperBound;
+ req->largeSetLowerBound = &obj->set_inher.largeSetLowerBound;
+ req->mediumSetPresentNumber = &obj->set_inher.mediumSetPresentNumber;
+ req->replaceIndicator = &obj->set_inher.replaceIndicator;
+ req->resultSetName = obj->setName ? obj->setName : "Default";
+ logf (LOG_DEBUG, "Search, resultSetName %s", req->resultSetName);
+ req->num_databaseNames = obj->set_inher.num_databaseNames;
+ req->databaseNames = obj->set_inher.databaseNames;
+ for (r=0; r < obj->set_inher.num_databaseNames; r++)
+ logf (LOG_DEBUG, " Database %s", obj->set_inher.databaseNames[r]);
+ if (obj->set_inher.preferredRecordSyntax)
+ {
+ struct oident ident;
+
+ ident.proto = p->protocol_type;
+ ident.oclass = CLASS_RECSYN;
+ ident.value = *obj->set_inher.preferredRecordSyntax;
+ logf (LOG_DEBUG, "Preferred record syntax is %d", ident.value);
+ req->preferredRecordSyntax = odr_oiddup (p->odr_out,
+ oid_getoidbyent (&ident));
+ }
+ else
+ req->preferredRecordSyntax = 0;
+
+ if (obj->set_inher.smallSetElementSetNames &&
+ *obj->set_inher.smallSetElementSetNames)
+ {
+ Z_ElementSetNames *esn = odr_malloc (p->odr_out, sizeof(*esn));
+
+ esn->which = Z_ElementSetNames_generic;
+ esn->u.generic = obj->set_inher.smallSetElementSetNames;
+ req->smallSetElementSetNames = esn;
+ }
+ else
+ req->smallSetElementSetNames = NULL;
+
+ if (obj->set_inher.mediumSetElementSetNames &&
+ *obj->set_inher.mediumSetElementSetNames)
+ {
+ Z_ElementSetNames *esn = odr_malloc (p->odr_out, sizeof(*esn));
+
+ esn->which = Z_ElementSetNames_generic;
+ esn->u.generic = obj->set_inher.mediumSetElementSetNames;
+ req->mediumSetElementSetNames = esn;
+ }
+ else
+ req->mediumSetElementSetNames = NULL;
+
+ req->query = &query;
+
+ if (!strcmp (obj->set_inher.queryType, "rpn"))
+ {
+ Z_RPNQuery *RPNquery;
+
+ RPNquery = p_query_rpn (p->odr_out, argv[2]);
+ if (!RPNquery)
+ {
+ Tcl_AppendResult (interp, "Syntax error in query", NULL);
+ return TCL_ERROR;
+ }
+ RPNquery->attributeSetId = oid_getoidbyent (&bib1);
+ query.which = Z_Query_type_1;
+ query.u.type_1 = RPNquery;
+ logf (LOG_DEBUG, "RPN");
+ }
+#if CCL2RPN
+ else if (!strcmp (obj->set_inher.queryType, "cclrpn"))
+ {
+ int error;
+ int pos;
+ struct ccl_rpn_node *rpn;
+ Z_RPNQuery *RPNquery;
+
+ rpn = ccl_find_str(p->bibset, argv[2], &error, &pos);
+ if (error)
+ {
+ Tcl_AppendResult (interp, "CCL error: ",
+ ccl_err_msg(error), NULL);
+ return TCL_ERROR;
+ }
+ ccl_pr_tree (rpn, stderr);
+ fprintf (stderr, "\n");
+ assert((RPNquery = ccl_rpn_query(rpn)));
+ RPNquery->attributeSetId = oid_getoidbyent (&bib1);
+ query.which = Z_Query_type_1;
+ query.u.type_1 = RPNquery;
+ logf (LOG_DEBUG, "CCLRPN");
+ }
+#endif
+ else if (!strcmp (obj->set_inher.queryType, "ccl"))
+ {
+ query.which = Z_Query_type_2;
+ query.u.type_2 = &ccl_query;
+ ccl_query.buf = (unsigned char *) argv[2];
+ ccl_query.len = strlen (argv[2]);
+ logf (LOG_DEBUG, "CCL");
+ }
+ else
+ {
+ interp->result = "unknown query method";
+ return TCL_ERROR;
+ }
+ return ir_tcl_send_APDU (interp, p, apdu, "search", *argv);
+}
+
+/*
+ * do_searchResponse: add search response handler
+ */
+static int do_searchResponse (void *o, Tcl_Interp *interp,
+ int argc, char **argv)
+{
+ IrTcl_SetObj *obj = o;
+
+ if (argc == 0)
+ {
+ obj->searchResponse = NULL;
+ return TCL_OK;
+ }
+ else if (argc == -1)
+ return ir_tcl_strdel (interp, &obj->searchResponse);
+ if (argc == 3)
+ {
+ free (obj->searchResponse);
+ if (argv[2][0])
+ {
+ if (ir_tcl_strdup (interp, &obj->searchResponse, argv[2])
+ == TCL_ERROR)
+ return TCL_ERROR;
+ }
+ else
+ obj->searchResponse = NULL;
+ }
+ return TCL_OK;
+}
+
+/*
+ * do_presentResponse: add present response handler
+ */
+static int do_presentResponse (void *o, Tcl_Interp *interp,
+ int argc, char **argv)
+{
+ IrTcl_SetObj *obj = o;
+
+ if (argc == 0)
+ {
+ obj->presentResponse = NULL;
+ return TCL_OK;
+ }
+ else if (argc == -1)
+ return ir_tcl_strdel (interp, &obj->presentResponse);
+ if (argc == 3)
+ {
+ free (obj->presentResponse);
+ if (argv[2][0])
+ {
+ if (ir_tcl_strdup (interp, &obj->presentResponse, argv[2])
+ == TCL_ERROR)
+ return TCL_ERROR;
+ }
+ else
+ obj->presentResponse = NULL;
+ }
+ return TCL_OK;
+}
+
+/*
+ * do_resultCount: Get number of hits
+ */
+static int do_resultCount (void *o, Tcl_Interp *interp,
+ int argc, char **argv)
+{
+ IrTcl_SetObj *obj = o;
+
+ if (argc <= 0)
+ {
+ obj->resultCount = 0;
+ return TCL_OK;
+ }
+ return get_set_int (&obj->resultCount, interp, argc, argv);
+}
+
+/*
+ * do_searchStatus: Get search status (after search response)
+ */
+static int do_searchStatus (void *o, Tcl_Interp *interp,
+ int argc, char **argv)
+{
+ IrTcl_SetObj *obj = o;
+
+ if (argc <= 0)
+ return TCL_OK;
+ return get_set_int (&obj->searchStatus, interp, argc, argv);
+}
+
+/*
+ * do_presentStatus: Get search status (after search/present response)
+ */
+static int do_presentStatus (void *o, Tcl_Interp *interp,
+ int argc, char **argv)
+{
+ IrTcl_SetObj *obj = o;
+
+ if (argc <= 0)
+ return TCL_OK;
+ return get_set_int (&obj->presentStatus, interp, argc, argv);
+}
+
+/*
+ * do_nextResultSetPosition: Get next result set position
+ * (after search/present response)
+ */
+static int do_nextResultSetPosition (void *o, Tcl_Interp *interp,
+ int argc, char **argv)
+{
+ IrTcl_SetObj *obj = o;
+
+ if (argc <= 0)
+ {
+ obj->nextResultSetPosition = 0;
+ return TCL_OK;
+ }
+ return get_set_int (&obj->nextResultSetPosition, interp, argc, argv);
+}
+
+/*
+ * do_setName: Set result Set name
+ */
+static int do_setName (void *o, Tcl_Interp *interp,
+ int argc, char **argv)
+{
+ IrTcl_SetObj *obj = o;
+
+ if (argc == 0)
+ return ir_tcl_strdup (interp, &obj->setName, "Default");
+ else if (argc == -1)
+ return ir_tcl_strdel (interp, &obj->setName);
+ if (argc == 3)
+ {
+ free (obj->setName);
+ if (ir_tcl_strdup (interp, &obj->setName, argv[2])
+ == TCL_ERROR)
+ return TCL_ERROR;
+ }
+ Tcl_AppendElement (interp, obj->setName);
+ return TCL_OK;
+}
+
+/*
+ * do_numberOfRecordsReturned: Get number of records returned
+ */
+static int do_numberOfRecordsReturned (void *o, Tcl_Interp *interp,
+ int argc, char **argv)
+{
+ IrTcl_SetObj *obj = o;
+
+ if (argc <= 0)
+ {
+ obj->numberOfRecordsReturned = 0;
+ return TCL_OK;
+ }
+ return get_set_int (&obj->numberOfRecordsReturned, interp, argc, argv);
+}
+
+/*
+ * do_type: Return type (if any) at position.
+ */
+static int do_type (void *o, Tcl_Interp *interp, int argc, char **argv)
+{
+ IrTcl_SetObj *obj = o;
+ int offset;
+ IrTcl_RecordList *rl;
+
+ if (argc == 0)
+ {
+ obj->record_list = NULL;
+ return TCL_OK;
+ }
+ else if (argc == -1)
+ {
+ delete_IR_records (obj);
+ return TCL_OK;
+ }
+ if (argc != 3)
+ {
+ sprintf (interp->result, "wrong # args");
+ return TCL_ERROR;
+ }
+ if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
+ return TCL_ERROR;
+ rl = find_IR_record (obj, offset);
+ if (!rl)
+ {
+ logf (LOG_DEBUG, "No record at position %d", offset);
+ return TCL_OK;
+ }
+ switch (rl->which)
+ {
+ case Z_NamePlusRecord_databaseRecord:
+ interp->result = "DB";
+ break;
+ case Z_NamePlusRecord_surrogateDiagnostic:
+ interp->result = "SD";
+ break;
+ }
+ return TCL_OK;
+}
+
+
+/*
+ * do_recordType: Return record type (if any) at position.
+ */
+static int do_recordType (void *o, Tcl_Interp *interp, int argc, char **argv)
+{
+ IrTcl_SetObj *obj = o;
+ int offset;
+ IrTcl_RecordList *rl;
+
+ if (argc == 0)
+ {
+ return TCL_OK;
+ }
+ else if (argc == -1)
+ {
+ return TCL_OK;
+ }
+ if (argc != 3)
+ {
+ sprintf (interp->result, "wrong # args");
+ return TCL_ERROR;
+ }
+ if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
+ return TCL_ERROR;
+ rl = find_IR_record (obj, offset);
+ if (!rl)
+ return TCL_OK;
+ if (rl->which != Z_NamePlusRecord_databaseRecord)
+ {
+ Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
+ return TCL_ERROR;
+ }
+ Tcl_AppendElement (interp, (char*)
+ IrTcl_getRecordSyntaxStr (rl->u.dbrec.type));
+ return TCL_OK;
+}
+
+/*
+ * set record elements (for record extraction)
+ */
+static int do_recordElements (void *o, Tcl_Interp *interp,
+ int argc, char **argv)
+{
+ IrTcl_SetObj *obj = o;
+
+ if (argc == 0)
+ {
+ obj->recordElements = NULL;
+ return TCL_OK;
+ }
+ else if (argc == -1)
+ return ir_tcl_strdel (NULL, &obj->recordElements);
+ if (argc > 3)
+ {
+ sprintf (interp->result, "wrong # args");
+ return TCL_ERROR;
+ }
+ if (argc == 3)
+ {
+ free (obj->recordElements);
+ return ir_tcl_strdup (NULL, &obj->recordElements,
+ (*argv[2] ? argv[2] : NULL));
+ }
+ Tcl_AppendResult (interp, obj->recordElements, NULL);
+ return TCL_OK;
+}
+
+/*
+ * ir_diagResult
+ */
+static int ir_diagResult (Tcl_Interp *interp, IrTcl_Diagnostic *list, int num)
+{
+ char buf[20];
+ int i;
+ const char *cp;
+
+ for (i = 0; i<num; i++)
+ {
+ logf (LOG_DEBUG, "Diagnostic, code %d", list[i].condition);
+ sprintf (buf, "%d", list[i].condition);
+ Tcl_AppendElement (interp, buf);
+ cp = diagbib1_str (list[i].condition);
+ if (cp)
+ Tcl_AppendElement (interp, (char*) cp);
+ else
+ Tcl_AppendElement (interp, "");
+ if (list[i].addinfo)
+ Tcl_AppendElement (interp, (char*) list[i].addinfo);
+ else
+ Tcl_AppendElement (interp, "");
+ }
+ return TCL_OK;
+}
+
+/*
+ * do_diag: Return diagnostic record info
+ */
+static int do_diag (void *o, Tcl_Interp *interp, int argc, char **argv)
+{
+ IrTcl_SetObj *obj = o;
+ int offset;
+ IrTcl_RecordList *rl;
+
+ if (argc <= 0)
+ return TCL_OK;
+ if (argc != 3)
+ {
+ sprintf (interp->result, "wrong # args");
+ return TCL_ERROR;
+ }
+ if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
+ return TCL_ERROR;
+ rl = find_IR_record (obj, offset);
+ if (!rl)
+ {
+ Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
+ return TCL_ERROR;
+ }
+ if (rl->which != Z_NamePlusRecord_surrogateDiagnostic)
+ {
+ Tcl_AppendResult (interp, "No Diagnostic record at #", argv[2], NULL);
+ return TCL_ERROR;
+ }
+ return ir_diagResult (interp, rl->u.surrogateDiagnostics.list,
+ rl->u.surrogateDiagnostics.num);
+}
+
+/*
+ * do_getMarc: Get ISO2709 Record lines/fields
+ */
+static int do_getMarc (void *o, Tcl_Interp *interp, int argc, char **argv)
+{
+ IrTcl_SetObj *obj = o;
+ int offset;
+ IrTcl_RecordList *rl;
+
+ if (argc <= 0)
+ return TCL_OK;
+ if (argc < 7)
+ {
+ sprintf (interp->result, "wrong # args");
+ return TCL_ERROR;
+ }
+ if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
+ return TCL_ERROR;
+ rl = find_IR_record (obj, offset);
+ if (!rl)
+ {
+ Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
+ return TCL_ERROR;
+ }
+ if (rl->which != Z_NamePlusRecord_databaseRecord)
+ {
+ Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
+ return TCL_ERROR;
+ }
+ return ir_tcl_get_marc (interp, rl->u.dbrec.buf, argc, argv);
+}
+
+/*
+ * do_getSutrs: Get SUTRS Record
+ */
+static int do_getSutrs (void *o, Tcl_Interp *interp, int argc, char **argv)
+{
+ IrTcl_SetObj *obj = o;
+ int offset;
+ IrTcl_RecordList *rl;
+
+ if (argc <= 0)
+ return TCL_OK;
+ if (argc != 3)
+ {
+ sprintf (interp->result, "wrong # args");
+ return TCL_ERROR;
+ }
+ if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
+ return TCL_ERROR;
+ rl = find_IR_record (obj, offset);
+ if (!rl)
+ {
+ Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
+ return TCL_ERROR;
+ }
+ if (rl->which != Z_NamePlusRecord_databaseRecord)
+ {
+ Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
+ return TCL_ERROR;
+ }
+ if (rl->u.dbrec.type != VAL_SUTRS)
+ return TCL_OK;
+ Tcl_AppendElement (interp, rl->u.dbrec.buf);
+ return TCL_OK;
+}
+
+
+/*
+ * do_getGrs: Get a GRS1 Record
+ */
+static int do_getGrs (void *o, Tcl_Interp *interp, int argc, char **argv)
+{
+ IrTcl_SetObj *obj = o;
+ int offset;
+ IrTcl_RecordList *rl;
+
+ if (argc <= 0)
+ return TCL_OK;
+ if (argc < 3)
+ {
+ sprintf (interp->result, "wrong # args");
+ return TCL_ERROR;
+ }
+ if (Tcl_GetInt (interp, argv[2], &offset)==TCL_ERROR)
+ return TCL_ERROR;
+ rl = find_IR_record (obj, offset);
+ if (!rl)
+ {
+ Tcl_AppendResult (interp, "No record at #", argv[2], NULL);
+ return TCL_ERROR;
+ }
+ if (rl->which != Z_NamePlusRecord_databaseRecord)
+ {
+ Tcl_AppendResult (interp, "No DB record at #", argv[2], NULL);
+ return TCL_ERROR;
+ }
+ if (rl->u.dbrec.type != VAL_GRS1)
+ return TCL_OK;
+ return ir_tcl_get_grs (interp, rl->u.dbrec.u.grs1, argc, argv);
+}
+
+
+/*
+ * do_responseStatus: Return response status (present or search)
+ */
+static int do_responseStatus (void *o, Tcl_Interp *interp,
+ int argc, char **argv)
+{
+ IrTcl_SetObj *obj = o;
+
+ if (argc == 0)
+ {
+ obj->recordFlag = 0;
+ obj->nonSurrogateDiagnosticNum = 0;
+ obj->nonSurrogateDiagnosticList = NULL;
+ return TCL_OK;
+ }
+ else if (argc == -1)
+ {
+ ir_deleteDiags (&obj->nonSurrogateDiagnosticList,
+ &obj->nonSurrogateDiagnosticNum);
+ return TCL_OK;
+ }
+ if (!obj->recordFlag)
+ {
+ Tcl_AppendElement (interp, "OK");
+ return TCL_OK;
+ }
+ switch (obj->which)
+ {
+ case Z_Records_DBOSD:
+ Tcl_AppendElement (interp, "DBOSD");
+ break;
+ case Z_Records_NSD:
+ Tcl_AppendElement (interp, "NSD");
+ return ir_diagResult (interp, obj->nonSurrogateDiagnosticList,
+ obj->nonSurrogateDiagnosticNum);
+ }
+ return TCL_OK;
+}
+
+/*
+ * do_present: Perform Present Request
+ */
+
+static int do_present (void *o, Tcl_Interp *interp, int argc, char **argv)
+{
+ IrTcl_SetObj *obj = o;
+ IrTcl_Obj *p;
+ Z_APDU *apdu;
+ Z_PresentRequest *req;
+ int start;
+ int number;
+
+ if (argc <= 0)
+ return TCL_OK;
+ if (argc >= 3)
+ {
+ if (Tcl_GetInt (interp, argv[2], &start) == TCL_ERROR)
+ return TCL_ERROR;
+ }
+ else
+ start = 1;
+ if (argc >= 4)
+ {
+ if (Tcl_GetInt (interp, argv[3], &number) == TCL_ERROR)
+ return TCL_ERROR;
+ }
+ else
+ number = 10;
+ p = obj->parent;
+ if (!p->cs_link)
+ {
+ interp->result = "present: not connected";
+ return TCL_ERROR;
+ }
+
+ obj->start = start;
+ obj->number = number;
+
+ apdu = zget_APDU (p->odr_out, Z_APDU_presentRequest);
+ req = apdu->u.presentRequest;
+
+ set_referenceId (p->odr_out, &req->referenceId,
+ obj->set_inher.referenceId);
+
+ req->resultSetId = obj->setName ? obj->setName : "Default";
+
+ req->resultSetStartPoint = &start;
+ req->numberOfRecordsRequested = &number;
+ if (obj->set_inher.preferredRecordSyntax)
+ {
+ struct oident ident;
+
+ ident.proto = p->protocol_type;
+ ident.oclass = CLASS_RECSYN;
+ ident.value = *obj->set_inher.preferredRecordSyntax;
+ logf (LOG_DEBUG, "Preferred record syntax is %d", ident.value);
+ req->preferredRecordSyntax = odr_oiddup (p->odr_out,
+ oid_getoidbyent (&ident));
+ }
+ else
+ req->preferredRecordSyntax = 0;
+
+ if (obj->set_inher.elementSetNames && *obj->set_inher.elementSetNames)
+ {
+ Z_ElementSetNames *esn = odr_malloc (p->odr_out, sizeof(*esn));
+ Z_RecordComposition *compo = odr_malloc (p->odr_out, sizeof(*compo));
+
+ esn->which = Z_ElementSetNames_generic;
+ esn->u.generic = obj->set_inher.elementSetNames;
+
+ req->recordComposition = compo;
+ compo->which = Z_RecordComp_simple;
+ compo->u.simple = esn;
+ }
+ else
+ req->recordComposition = NULL;
+ return ir_tcl_send_APDU (interp, p, apdu, "present", *argv);
+}
+
+/*
+ * do_loadFile: Load result set from file
+ */
+
+static int do_loadFile (void *o, Tcl_Interp *interp,
+ int argc, char **argv)
+{
+ IrTcl_SetObj *setobj = o;
+ FILE *inf;
+ size_t size;
+ int no = 1;
+ char *buf;
+
+ if (argc <= 0)
+ return TCL_OK;
+ if (argc != 3)
+ {
+ interp->result = "wrong # args";
+ return TCL_ERROR;
+ }
+ inf = fopen (argv[2], "r");
+ if (!inf)
+ {
+ Tcl_AppendResult (interp, "Cannot open ", argv[2], NULL);
+ return TCL_ERROR;
+ }
+ while ((buf = ir_tcl_fread_marc (inf, &size)))
+ {
+ IrTcl_RecordList *rl;
+
+ rl = new_IR_record (setobj, no, Z_NamePlusRecord_databaseRecord, "F");
+ rl->u.dbrec.type = VAL_USMARC;
+ rl->u.dbrec.buf = buf;
+ rl->u.dbrec.size = size;
+ no++;
+ }
+ setobj->numberOfRecordsReturned = no-1;
+ fclose (inf);
+ return TCL_OK;
+}
+
+static IrTcl_Method ir_set_method_tab[] = {
+ { 0, "search", do_search },
+ { 0, "searchResponse", do_searchResponse },
+ { 0, "presentResponse", do_presentResponse },
+ { 0, "searchStatus", do_searchStatus },
+ { 0, "presentStatus", do_presentStatus },
+ { 0, "nextResultSetPosition", do_nextResultSetPosition },
+ { 0, "setName", do_setName },
+ { 0, "resultCount", do_resultCount },
+ { 0, "numberOfRecordsReturned", do_numberOfRecordsReturned },
+ { 0, "present", do_present },
+ { 0, "type", do_type },
+ { 0, "getMarc", do_getMarc },
+ { 0, "getSutrs", do_getSutrs },
+ { 0, "getGrs", do_getGrs },
+ { 0, "recordType", do_recordType },
+ { 0, "recordElements", do_recordElements },
+ { 0, "diag", do_diag },
+ { 0, "responseStatus", do_responseStatus },
+ { 0, "loadFile", do_loadFile },
+ { 0, NULL, NULL}
+};
+
+/*
+ * ir_set_obj_method: IR Set Object methods
+ */
+static int ir_set_obj_method (ClientData clientData, Tcl_Interp *interp,
+ int argc, char **argv)
+{
+ IrTcl_Methods tabs[3];
+ IrTcl_SetObj *p = clientData;
+
+ if (argc < 2)
+ {
+ interp->result = "wrong # args";
+ return TCL_ERROR;
+ }
+ tabs[0].tab = ir_set_method_tab;
+ tabs[0].obj = p;
+ tabs[1].tab = ir_set_c_method_tab;
+ tabs[1].obj = &p->set_inher;
+ tabs[2].tab = NULL;
+
+ return ir_method (interp, argc, argv, tabs);
+}
+
+/*
+ * ir_set_obj_delete: IR Set Object disposal
+ */
+static void ir_set_obj_delete (ClientData clientData)
+{
+ IrTcl_Methods tabs[3];
+ IrTcl_SetObj *p = clientData;
+
+ logf (LOG_DEBUG, "ir set delete");
+
+ tabs[0].tab = ir_set_method_tab;
+ tabs[0].obj = p;
+ tabs[1].tab = ir_set_c_method_tab;
+ tabs[1].obj = &p->set_inher;
+ tabs[2].tab = NULL;
+
+ ir_method (NULL, -1, NULL, tabs);
+
+ free (p);
+}
+
+/*
+ * ir_set_obj_mk: IR Set Object creation
+ */
+static int ir_set_obj_mk (ClientData clientData, Tcl_Interp *interp,
+ int argc, char **argv)
+{
+ IrTcl_Methods tabs[3];
+ IrTcl_SetObj *obj;
+
+ if (argc < 2 || argc > 3)
+ {
+ interp->result = "wrong # args";
+ return TCL_ERROR;
+ }
+ obj = ir_tcl_malloc (sizeof(*obj));
+ logf (LOG_DEBUG, "ir set create");
+ if (argc == 3)
+ {
+ Tcl_CmdInfo parent_info;
+ int i;
+ IrTcl_SetCObj *dst;
+ IrTcl_SetCObj *src;
+
+ if (!Tcl_GetCommandInfo (interp, argv[2], &parent_info))
+ {
+ interp->result = "No parent";
+ return TCL_ERROR;
+ }
+ obj->parent = (IrTcl_Obj *) parent_info.clientData;
+
+ dst = &obj->set_inher;
+ src = &obj->parent->set_inher;
+
+ if ((dst->num_databaseNames = src->num_databaseNames))
+ dst->databaseNames =
+ ir_tcl_malloc (sizeof (*dst->databaseNames)
+ * dst->num_databaseNames);
+ else
+ dst->databaseNames = NULL;
+ for (i = 0; i < dst->num_databaseNames; i++)
+ if (ir_tcl_strdup (interp, &dst->databaseNames[i],
+ src->databaseNames[i]) == TCL_ERROR)
+ return TCL_ERROR;
+ if (ir_tcl_strdup (interp, &dst->queryType, src->queryType)
+ == TCL_ERROR)
+ return TCL_ERROR;
+
+ if (ir_tcl_strdup (interp, &dst->referenceId, src->referenceId)
+ == TCL_ERROR)
+ return TCL_ERROR;
+
+ if (ir_tcl_strdup (interp, &dst->elementSetNames, src->elementSetNames)
+ == TCL_ERROR)
+ return TCL_ERROR;
+
+ if (ir_tcl_strdup (interp, &dst->smallSetElementSetNames,
+ src->smallSetElementSetNames)
+ == TCL_ERROR)
+ return TCL_ERROR;
+
+ if (ir_tcl_strdup (interp, &dst->mediumSetElementSetNames,
+ src->mediumSetElementSetNames)
+ == TCL_ERROR)
+ return TCL_ERROR;
+
+ if (src->preferredRecordSyntax &&
+ (dst->preferredRecordSyntax
+ = ir_tcl_malloc (sizeof(*dst->preferredRecordSyntax))))
+ *dst->preferredRecordSyntax = *src->preferredRecordSyntax;
+ else
+ dst->preferredRecordSyntax = NULL;
+ dst->replaceIndicator = src->replaceIndicator;
+ dst->smallSetUpperBound = src->smallSetUpperBound;
+ dst->largeSetLowerBound = src->largeSetLowerBound;
+ dst->mediumSetPresentNumber = src->mediumSetPresentNumber;
+ }
+ else
+ obj->parent = NULL;
+
+ tabs[0].tab = ir_set_method_tab;
+ tabs[0].obj = obj;
+ tabs[1].tab = NULL;
+
+ if (ir_method (interp, 0, NULL, tabs) == TCL_ERROR)
+ return TCL_ERROR;
+
+ Tcl_CreateCommand (interp, argv[1], ir_set_obj_method,
+ (ClientData) obj, ir_set_obj_delete);
+ return TCL_OK;
+}
+
+/* ------------------------------------------------------- */
+
+/*
+ * do_scan: Perform scan
+ */
+static int do_scan (void *o, Tcl_Interp *interp, int argc, char **argv)
+{
+ Z_ScanRequest *req;
+ Z_APDU *apdu;
+ IrTcl_ScanObj *obj = o;
+ IrTcl_Obj *p = obj->parent;
+ oident bib1;
+#if CCL2RPN
+ struct ccl_rpn_node *rpn;
+ int pos;
+#endif
+
+ if (argc <= 0)
+ return TCL_OK;
+ if (argc != 3)
+ {
+ interp->result = "wrong # args";
+ return TCL_ERROR;
+ }
+ if (!p->set_inher.num_databaseNames)
+ {
+ interp->result = "no databaseNames";
+ return TCL_ERROR;
+ }
+ if (!p->cs_link)
+ {
+ interp->result = "scan: not connected";
+ return TCL_ERROR;
+ }
+
+ bib1.proto = p->protocol_type;
+ bib1.oclass = CLASS_ATTSET;
+ bib1.value = VAL_BIB1;
+
+ apdu = zget_APDU (p->odr_out, Z_APDU_scanRequest);
+ req = apdu->u.scanRequest;
+
+ set_referenceId (p->odr_out, &req->referenceId, p->set_inher.referenceId);
+ req->num_databaseNames = p->set_inher.num_databaseNames;
+ req->databaseNames = p->set_inher.databaseNames;
+ req->attributeSet = oid_getoidbyent (&bib1);
+
+#if !CCL2RPN
+ if (!(req->termListAndStartPoint = p_query_scan (p->odr_out, argv[2])))
+ {
+ Tcl_AppendResult (interp, "Syntax error in query", NULL);
+ return TCL_ERROR;
+ }
+#else
+ rpn = ccl_find_str(p->bibset, argv[2], &r, &pos);
+ if (r)
+ {
+ Tcl_AppendResult (interp, "CCL error: ", ccl_err_msg (r), NULL);
+ return TCL_ERROR;
+ }
+ ccl_pr_tree (rpn, stderr);
+ fprintf (stderr, "\n");
+ if (!(req->termListAndStartPoint = ccl_scan_query (rpn)))
+ return TCL_ERROR;
+#endif
+ req->stepSize = &obj->stepSize;
+ req->numberOfTermsRequested = &obj->numberOfTermsRequested;
+ req->preferredPositionInResponse = &obj->preferredPositionInResponse;
+ logf (LOG_DEBUG, "stepSize=%d", *req->stepSize);
+ logf (LOG_DEBUG, "numberOfTermsRequested=%d",
+ *req->numberOfTermsRequested);
+ logf (LOG_DEBUG, "preferredPositionInResponse=%d",
+ *req->preferredPositionInResponse);
+
+ return ir_tcl_send_APDU (interp, p, apdu, "scan", *argv);
+}
+
+/*
+ * do_scanResponse: add scan response handler
+ */
+static int do_scanResponse (void *o, Tcl_Interp *interp,
+ int argc, char **argv)
+{
+ IrTcl_ScanObj *obj = o;
+
+ if (argc == 0)
+ {
+ obj->scanResponse = NULL;
+ return TCL_OK;
+ }
+ else if (argc == -1)
+ return ir_tcl_strdel (interp, &obj->scanResponse);
+ if (argc == 3)