+ printf ("Received ResourceControlRequest.\n");
+ print_referenceId(1, req->referenceId);
+ print_int(1, "Suspended Flag", req->suspendedFlag);
+ print_int(1, "Partial Results Available", req->partialResultsAvailable);
+ print_int(1, "Response Required", req->responseRequired);
+ print_int(1, "Triggered Request Flag", req->triggeredRequestFlag);
+ print_external(1, req->resourceReport);
+ return 0;
+}
+
+void process_ESResponse(Z_ExtendedServicesResponse *res)
+{
+ printf("Status: ");
+ switch (*res->operationStatus)
+ {
+ case Z_ExtendedServicesResponse_done:
+ printf ("done\n");
+ break;
+ case Z_ExtendedServicesResponse_accepted:
+ printf ("accepted\n");
+ break;
+ case Z_ExtendedServicesResponse_failure:
+ printf ("failure\n");
+ display_diagrecs(res->diagnostics, res->num_diagnostics);
+ break;
+ default:
+ printf ("unknown\n");
+ }
+ if ( (*res->operationStatus != Z_ExtendedServicesResponse_failure) &&
+ (res->num_diagnostics != 0) ) {
+ display_diagrecs(res->diagnostics, res->num_diagnostics);
+ }
+ print_refid (res->referenceId);
+ if (res->taskPackage &&
+ res->taskPackage->which == Z_External_extendedService)
+ {
+ Z_TaskPackage *taskPackage = res->taskPackage->u.extendedService;
+ Odr_oct *id = taskPackage->targetReference;
+ Z_External *ext = taskPackage->taskSpecificParameters;
+
+ if (id)
+ {
+ printf ("Target Reference: ");
+ print_stringn (id->buf, id->len);
+ printf ("\n");
+ }
+ if (ext->which == Z_External_update)
+ {
+ Z_IUUpdateTaskPackage *utp = ext->u.update->u.taskPackage;
+ if (utp && utp->targetPart)
+ {
+ Z_IUTargetPart *targetPart = utp->targetPart;
+ int i;
+
+ for (i = 0; i<targetPart->num_taskPackageRecords; i++)
+ {
+
+ Z_IUTaskPackageRecordStructure *tpr =
+ targetPart->taskPackageRecords[i];
+ printf ("task package record %d\n", i+1);
+ if (tpr->which == Z_IUTaskPackageRecordStructure_record)
+ {
+ display_record (tpr->u.record);
+ }
+ else
+ {
+ printf ("other type\n");
+ }
+ }
+ }
+ }
+ }
+}
+
+const char *get_ill_element (void *clientData, const char *element)
+{
+ return 0;
+}
+
+static Z_External *create_external_itemRequest()
+{
+ struct ill_get_ctl ctl;
+ ILL_ItemRequest *req;
+ Z_External *r = 0;
+ int item_request_size = 0;
+ char *item_request_buf = 0;
+
+ ctl.odr = out;
+ ctl.clientData = 0;
+ ctl.f = get_ill_element;
+
+ req = ill_get_ItemRequest(&ctl, "ill", 0);
+ if (!req)
+ printf ("ill_get_ItemRequest failed\n");
+
+ if (!ill_ItemRequest (out, &req, 0, 0))
+ {
+ if (apdu_file)
+ {
+ ill_ItemRequest(print, &req, 0, 0);
+ odr_reset(print);
+ }
+ item_request_buf = odr_getbuf (out, &item_request_size, 0);
+ if (item_request_buf)
+ odr_setbuf (out, item_request_buf, item_request_size, 1);
+ printf ("Couldn't encode ItemRequest, size %d\n", item_request_size);
+ return 0;
+ }
+ else
+ {
+ oident oid;
+
+ item_request_buf = odr_getbuf (out, &item_request_size, 0);
+ oid.proto = PROTO_GENERAL;
+ oid.oclass = CLASS_GENERAL;
+ oid.value = VAL_ISO_ILL_1;
+
+ r = (Z_External *) odr_malloc (out, sizeof(*r));
+ r->direct_reference = odr_oiddup(out,oid_getoidbyent(&oid));
+ r->indirect_reference = 0;
+ r->descriptor = 0;
+ r->which = Z_External_single;
+
+ r->u.single_ASN1_type = (Odr_oct *)
+ odr_malloc (out, sizeof(*r->u.single_ASN1_type));
+ r->u.single_ASN1_type->buf = (unsigned char *)
+ odr_malloc (out, item_request_size);
+ r->u.single_ASN1_type->len = item_request_size;
+ r->u.single_ASN1_type->size = item_request_size;
+ memcpy (r->u.single_ASN1_type->buf, item_request_buf,
+ item_request_size);
+
+ do_hex_dump(item_request_buf,item_request_size);
+ }
+ return r;
+}
+
+static Z_External *create_external_ILL_APDU(int which)
+{
+ struct ill_get_ctl ctl;
+ ILL_APDU *ill_apdu;
+ Z_External *r = 0;
+ int ill_request_size = 0;
+ char *ill_request_buf = 0;
+
+ ctl.odr = out;
+ ctl.clientData = 0;
+ ctl.f = get_ill_element;
+
+ ill_apdu = ill_get_APDU(&ctl, "ill", 0);
+
+ if (!ill_APDU (out, &ill_apdu, 0, 0))
+ {
+ if (apdu_file)
+ {
+ printf ("-------------------\n");
+ ill_APDU(print, &ill_apdu, 0, 0);
+ odr_reset(print);
+ printf ("-------------------\n");
+ }
+ ill_request_buf = odr_getbuf (out, &ill_request_size, 0);
+ if (ill_request_buf)
+ odr_setbuf (out, ill_request_buf, ill_request_size, 1);
+ printf ("Couldn't encode ILL-Request, size %d\n", ill_request_size);
+ return 0;
+ }
+ else
+ {
+ oident oid;
+ ill_request_buf = odr_getbuf (out, &ill_request_size, 0);
+
+ oid.proto = PROTO_GENERAL;
+ oid.oclass = CLASS_GENERAL;
+ oid.value = VAL_ISO_ILL_1;
+
+ r = (Z_External *) odr_malloc (out, sizeof(*r));
+ r->direct_reference = odr_oiddup(out,oid_getoidbyent(&oid));
+ r->indirect_reference = 0;
+ r->descriptor = 0;
+ r->which = Z_External_single;
+
+ r->u.single_ASN1_type = (Odr_oct *)
+ odr_malloc (out, sizeof(*r->u.single_ASN1_type));
+ r->u.single_ASN1_type->buf = (unsigned char *)
+ odr_malloc (out, ill_request_size);
+ r->u.single_ASN1_type->len = ill_request_size;
+ r->u.single_ASN1_type->size = ill_request_size;
+ memcpy (r->u.single_ASN1_type->buf, ill_request_buf, ill_request_size);
+/* printf ("len = %d\n", ill_request_size); */
+/* do_hex_dump(ill_request_buf,ill_request_size); */
+/* printf("--- end of extenal\n"); */
+
+ }
+ return r;
+}
+
+
+static Z_External *create_ItemOrderExternal(const char *type, int itemno)
+{
+ Z_External *r = (Z_External *) odr_malloc(out, sizeof(Z_External));
+ oident ItemOrderRequest;
+
+ ItemOrderRequest.proto = PROTO_Z3950;
+ ItemOrderRequest.oclass = CLASS_EXTSERV;
+ ItemOrderRequest.value = VAL_ITEMORDER;
+
+ r->direct_reference = odr_oiddup(out,oid_getoidbyent(&ItemOrderRequest));
+ r->indirect_reference = 0;
+ r->descriptor = 0;
+
+ r->which = Z_External_itemOrder;
+
+ r->u.itemOrder = (Z_ItemOrder *) odr_malloc(out,sizeof(Z_ItemOrder));
+ memset(r->u.itemOrder, 0, sizeof(Z_ItemOrder));
+ r->u.itemOrder->which=Z_IOItemOrder_esRequest;
+
+ r->u.itemOrder->u.esRequest = (Z_IORequest *)
+ odr_malloc(out,sizeof(Z_IORequest));
+ memset(r->u.itemOrder->u.esRequest, 0, sizeof(Z_IORequest));
+
+ r->u.itemOrder->u.esRequest->toKeep = (Z_IOOriginPartToKeep *)
+ odr_malloc(out,sizeof(Z_IOOriginPartToKeep));
+ memset(r->u.itemOrder->u.esRequest->toKeep, 0, sizeof(Z_IOOriginPartToKeep));
+ r->u.itemOrder->u.esRequest->notToKeep = (Z_IOOriginPartNotToKeep *)
+ odr_malloc(out,sizeof(Z_IOOriginPartNotToKeep));
+ memset(r->u.itemOrder->u.esRequest->notToKeep, 0, sizeof(Z_IOOriginPartNotToKeep));
+
+ r->u.itemOrder->u.esRequest->toKeep->supplDescription = NULL;
+ r->u.itemOrder->u.esRequest->toKeep->contact = NULL;
+ r->u.itemOrder->u.esRequest->toKeep->addlBilling = NULL;
+
+ r->u.itemOrder->u.esRequest->notToKeep->resultSetItem =
+ (Z_IOResultSetItem *) odr_malloc(out, sizeof(Z_IOResultSetItem));
+ memset(r->u.itemOrder->u.esRequest->notToKeep->resultSetItem, 0, sizeof(Z_IOResultSetItem));
+ r->u.itemOrder->u.esRequest->notToKeep->resultSetItem->resultSetId = "1";
+
+ r->u.itemOrder->u.esRequest->notToKeep->resultSetItem->item =
+ (int *) odr_malloc(out, sizeof(int));
+ *r->u.itemOrder->u.esRequest->notToKeep->resultSetItem->item = itemno;
+
+ if (!strcmp (type, "item") || !strcmp(type, "2"))
+ {
+ printf ("using item-request\n");
+ r->u.itemOrder->u.esRequest->notToKeep->itemRequest =
+ create_external_itemRequest();
+ }
+ else if (!strcmp(type, "ill") || !strcmp(type, "1"))
+ {
+ printf ("using ILL-request\n");
+ r->u.itemOrder->u.esRequest->notToKeep->itemRequest =
+ create_external_ILL_APDU(ILL_APDU_ILL_Request);
+ }
+ else if (!strcmp(type, "xml") || !strcmp(type, "3"))
+ {
+ const char *xml_buf =
+ "<itemorder>\n"
+ " <type>request</type>\n"
+ " <libraryNo>000200</libraryNo>\n"
+ " <borrowerTicketNo> 1212 </borrowerTicketNo>\n"
+ "</itemorder>";
+ r->u.itemOrder->u.esRequest->notToKeep->itemRequest =
+ z_ext_record (out, VAL_TEXT_XML, xml_buf, strlen(xml_buf));
+ }
+ else
+ r->u.itemOrder->u.esRequest->notToKeep->itemRequest = 0;
+
+ return r;
+}
+
+static int send_itemorder(const char *type, int itemno)
+{
+ Z_APDU *apdu = zget_APDU(out, Z_APDU_extendedServicesRequest);
+ Z_ExtendedServicesRequest *req = apdu->u.extendedServicesRequest;
+ oident ItemOrderRequest;
+
+ ItemOrderRequest.proto = PROTO_Z3950;
+ ItemOrderRequest.oclass = CLASS_EXTSERV;
+ ItemOrderRequest.value = VAL_ITEMORDER;
+ req->packageType = odr_oiddup(out,oid_getoidbyent(&ItemOrderRequest));
+ req->packageName = esPackageName;
+
+ req->taskSpecificParameters = create_ItemOrderExternal(type, itemno);
+
+ send_apdu(apdu);
+ return 0;
+}
+
+static int cmd_update(char *arg)
+{
+ Z_APDU *apdu = zget_APDU(out, Z_APDU_extendedServicesRequest );
+ Z_ExtendedServicesRequest *req = apdu->u.extendedServicesRequest;
+ Z_External *r;
+ int oid[OID_SIZE];
+ Z_IUOriginPartToKeep *toKeep;
+ Z_IUSuppliedRecords *notToKeep;
+ oident update_oid;
+ char action[20], recid[20], fname[80];
+ int action_no;
+ Z_External *record_this = 0;
+
+ *action = 0;
+ *recid = 0;
+ *fname = 0;
+ sscanf (arg, "%19s %19s %79s", action, recid, fname);
+
+ if (!strcmp (action, "insert"))
+ action_no = Z_IUOriginPartToKeep_recordInsert;
+ else if (!strcmp (action, "replace"))
+ action_no = Z_IUOriginPartToKeep_recordReplace;
+ else if (!strcmp (action, "delete"))
+ action_no = Z_IUOriginPartToKeep_recordDelete;
+ else if (!strcmp (action, "update"))
+ action_no = Z_IUOriginPartToKeep_specialUpdate;
+ else
+ {
+ printf ("Bad action: %s\n", action);
+ printf ("Possible values: insert, replace, delete, update\n");
+ return 0;
+ }
+
+ if (*fname)
+ {
+ FILE *inf;
+ struct stat status;
+ stat (fname, &status);
+ if (S_ISREG(status.st_mode) && (inf = fopen(fname, "r")))
+ {
+ size_t len = status.st_size;
+ char *buf = (char *) xmalloc (len);
+
+ fread (buf, 1, len, inf);
+
+ fclose (inf);
+
+ record_this = z_ext_record (out, VAL_TEXT_XML, buf, len);
+
+ xfree (buf);
+ }
+ else
+ {
+ printf ("File %s doesn't exist\n", fname);
+ return 0;
+ }
+ }
+ else
+ {
+ if (!record_last)
+ {
+ printf ("No last record (update ignored)\n");
+ return 0;
+ }
+ record_this = record_last;
+ }
+
+ update_oid.proto = PROTO_Z3950;
+ update_oid.oclass = CLASS_EXTSERV;
+ update_oid.value = VAL_DBUPDATE;
+ oid_ent_to_oid (&update_oid, oid);
+ req->packageType = odr_oiddup(out,oid);
+ req->packageName = esPackageName;
+
+ req->referenceId = set_refid (out);
+
+ r = req->taskSpecificParameters = (Z_External *)
+ odr_malloc (out, sizeof(*r));
+ r->direct_reference = odr_oiddup(out,oid);
+ r->indirect_reference = 0;
+ r->descriptor = 0;
+ r->which = Z_External_update;
+ r->u.update = (Z_IUUpdate *) odr_malloc(out, sizeof(*r->u.update));
+ r->u.update->which = Z_IUUpdate_esRequest;
+ r->u.update->u.esRequest = (Z_IUUpdateEsRequest *)
+ odr_malloc(out, sizeof(*r->u.update->u.esRequest));
+ toKeep = r->u.update->u.esRequest->toKeep = (Z_IUOriginPartToKeep *)
+ odr_malloc(out, sizeof(*r->u.update->u.esRequest->toKeep));
+ toKeep->databaseName = databaseNames[0];
+ toKeep->schema = 0;
+ toKeep->elementSetName = 0;
+ toKeep->actionQualifier = 0;
+ toKeep->action = (int *) odr_malloc(out, sizeof(*toKeep->action));
+ *toKeep->action = action_no;
+
+ notToKeep = r->u.update->u.esRequest->notToKeep = (Z_IUSuppliedRecords *)
+ odr_malloc(out, sizeof(*r->u.update->u.esRequest->notToKeep));
+ notToKeep->num = 1;
+ notToKeep->elements = (Z_IUSuppliedRecords_elem **)
+ odr_malloc(out, sizeof(*notToKeep->elements));
+ notToKeep->elements[0] = (Z_IUSuppliedRecords_elem *)
+ odr_malloc(out, sizeof(**notToKeep->elements));
+ notToKeep->elements[0]->which = Z_IUSuppliedRecords_elem_opaque;
+ if (*recid)
+ {
+ notToKeep->elements[0]->u.opaque = (Odr_oct *)
+ odr_malloc (out, sizeof(Odr_oct));
+ notToKeep->elements[0]->u.opaque->buf = (unsigned char *) recid;
+ notToKeep->elements[0]->u.opaque->size = strlen(recid);
+ notToKeep->elements[0]->u.opaque->len = strlen(recid);
+ }
+ else
+ notToKeep->elements[0]->u.opaque = 0;
+ notToKeep->elements[0]->supplementalId = 0;
+ notToKeep->elements[0]->correlationInfo = 0;
+ notToKeep->elements[0]->record = record_this;
+
+ send_apdu(apdu);
+
+ return 2;
+}
+
+static int cmd_itemorder(char *arg)
+{
+ char type[12];
+ int itemno;
+
+ if (sscanf (arg, "%10s %d", type, &itemno) != 2)
+ return 0;
+
+ printf("Item order request\n");
+ fflush(stdout);
+ send_itemorder(type, itemno);
+ return 2;
+}
+
+static int cmd_find(char *arg)
+{
+ if (!*arg)
+ {
+ printf("Find what?\n");
+ return 0;
+ }
+ if (!conn)
+ {
+ try_reconnect();
+
+ if (!conn) {
+ printf("Not connected yet\n");
+ return 0;
+ };
+ }
+ if (!send_searchRequest(arg))
+ return 0;
+ return 2;
+}
+
+static int cmd_delete(char *arg)
+{
+ if (!conn)
+ {
+ printf("Not connected yet\n");
+ return 0;
+ }
+ if (!send_deleteResultSetRequest(arg))
+ return 0;
+ return 2;
+}
+
+static int cmd_ssub(char *arg)
+{
+ if (!(smallSetUpperBound = atoi(arg)))
+ return 0;
+ return 1;
+}
+
+static int cmd_lslb(char *arg)
+{
+ if (!(largeSetLowerBound = atoi(arg)))
+ return 0;
+ return 1;
+}
+
+static int cmd_mspn(char *arg)
+{
+ if (!(mediumSetPresentNumber = atoi(arg)))
+ return 0;
+ return 1;
+}
+
+static int cmd_status(char *arg)
+{
+ printf("smallSetUpperBound: %d\n", smallSetUpperBound);
+ printf("largeSetLowerBound: %d\n", largeSetLowerBound);
+ printf("mediumSetPresentNumber: %d\n", mediumSetPresentNumber);
+ return 1;
+}
+
+static int cmd_setnames(char *arg)
+{
+ if (*arg == '1') /* enable ? */
+ setnumber = 0;
+ else if (*arg == '0') /* disable ? */
+ setnumber = -1;
+ else if (setnumber < 0) /* no args, toggle .. */
+ setnumber = 0;
+ else
+ setnumber = -1;
+
+ if (setnumber >= 0)
+ printf("Set numbering enabled.\n");
+ else
+ printf("Set numbering disabled.\n");
+ return 1;
+}
+
+/* PRESENT SERVICE ----------------------------- */
+
+static int send_presentRequest(char *arg)
+{
+ Z_APDU *apdu = zget_APDU(out, Z_APDU_presentRequest);
+ Z_PresentRequest *req = apdu->u.presentRequest;
+ Z_RecordComposition compo;
+ oident prefsyn;
+ int nos = 1;
+ int oid[OID_SIZE];
+ char *p;
+ char setstring[100];
+
+ req->referenceId = set_refid (out);
+ if ((p = strchr(arg, '+')))
+ {
+ nos = atoi(p + 1);
+ *p = 0;
+ }
+ if (*arg)
+ setno = atoi(arg);
+ if (p && (p=strchr(p+1, '+')))
+ {
+ strcpy (setstring, p+1);
+ req->resultSetId = setstring;
+ }
+ else if (setnumber >= 0)
+ {
+ sprintf(setstring, "%d", setnumber);
+ req->resultSetId = setstring;
+ }
+ req->resultSetStartPoint = &setno;
+ req->numberOfRecordsRequested = &nos;
+ prefsyn.proto = protocol;
+ prefsyn.oclass = CLASS_RECSYN;
+ prefsyn.value = recordsyntax;
+ req->preferredRecordSyntax =
+ odr_oiddup (out, oid_ent_to_oid(&prefsyn, oid));
+
+ if (schema != VAL_NONE)
+ {
+ oident prefschema;
+
+ prefschema.proto = protocol;
+ prefschema.oclass = CLASS_SCHEMA;
+ prefschema.value = schema;
+
+ req->recordComposition = &compo;
+ compo.which = Z_RecordComp_complex;
+ compo.u.complex = (Z_CompSpec *)
+ odr_malloc(out, sizeof(*compo.u.complex));
+ compo.u.complex->selectAlternativeSyntax = (bool_t *)
+ odr_malloc(out, sizeof(bool_t));
+ *compo.u.complex->selectAlternativeSyntax = 0;
+
+ compo.u.complex->generic = (Z_Specification *)
+ odr_malloc(out, sizeof(*compo.u.complex->generic));
+ compo.u.complex->generic->which = Z_Schema_oid;
+ compo.u.complex->generic->schema.oid = (Odr_oid *)
+ odr_oiddup(out, oid_ent_to_oid(&prefschema, oid));
+ if (!compo.u.complex->generic->schema.oid)
+ {
+ /* OID wasn't a schema! Try record syntax instead. */
+ prefschema.oclass = CLASS_RECSYN;
+ compo.u.complex->generic->schema.oid = (Odr_oid *)
+ odr_oiddup(out, oid_ent_to_oid(&prefschema, oid));
+ }
+ if (!elementSetNames)
+ compo.u.complex->generic->elementSpec = 0;
+ else
+ {
+ compo.u.complex->generic->elementSpec = (Z_ElementSpec *)
+ odr_malloc(out, sizeof(Z_ElementSpec));
+ compo.u.complex->generic->elementSpec->which =
+ Z_ElementSpec_elementSetName;
+ compo.u.complex->generic->elementSpec->u.elementSetName =
+ elementSetNames->u.generic;
+ }
+ compo.u.complex->num_dbSpecific = 0;
+ compo.u.complex->dbSpecific = 0;
+ compo.u.complex->num_recordSyntax = 0;
+ compo.u.complex->recordSyntax = 0;
+ }
+ else if (elementSetNames)
+ {
+ req->recordComposition = &compo;
+ compo.which = Z_RecordComp_simple;
+ compo.u.simple = elementSetNames;
+ }
+ send_apdu(apdu);
+ printf("Sent presentRequest (%d+%d).\n", setno, nos);
+ return 2;
+}
+
+static void close_session (void)
+{
+ cs_close (conn);
+ conn = 0;
+ if (session_mem)
+ {
+ nmem_destroy (session_mem);
+ session_mem = NULL;
+ }
+ sent_close = 0;
+}
+
+void process_close(Z_Close *req)
+{
+ Z_APDU *apdu = zget_APDU(out, Z_APDU_close);
+ Z_Close *res = apdu->u.close;
+
+ static char *reasons[] =
+ {
+ "finished",
+ "shutdown",
+ "system problem",
+ "cost limit reached",
+ "resources",
+ "security violation",
+ "protocolError",
+ "lack of activity",
+ "peer abort",
+ "unspecified"
+ };
+
+ printf("Reason: %s, message: %s\n", reasons[*req->closeReason],
+ req->diagnosticInformation ? req->diagnosticInformation : "NULL");
+ if (sent_close)
+ close_session ();
+ else
+ {
+ *res->closeReason = Z_Close_finished;
+ send_apdu(apdu);
+ printf("Sent response.\n");
+ sent_close = 1;
+ }
+}
+
+static int cmd_show(char *arg)
+{
+ if (!conn)
+ {
+ printf("Not connected yet\n");
+ return 0;
+ }
+ if (!send_presentRequest(arg))
+ return 0;
+ return 2;