2 * Copyright (c) 1995-2001, Index Data
3 * See the file LICENSE for details.
5 * $Id: client.c,v 1.128 2001-10-24 12:24:43 adam Exp $
10 * This is the obligatory little toy client, whose primary purpose is
11 * to illustrate the use of the YAZ service-level API.
18 #include <yaz/yaz-util.h>
20 #include <yaz/tcpip.h>
22 #include <yaz/xmosi.h>
25 #include <yaz/proto.h>
26 #include <yaz/marcdisp.h>
27 #include <yaz/diagbib1.h>
28 #include <yaz/otherinfo.h>
30 #include <yaz/pquery.h>
31 #include <yaz/sortspec.h>
38 #include <yaz/yaz-ccl.h>
41 #if HAVE_READLINE_READLINE_H
42 #include <readline/readline.h>
44 #if HAVE_READLINE_HISTORY_H
45 #include <readline/history.h>
50 #define C_PROMPT "Z> "
52 static ODR out, in, print; /* encoding and decoding streams */
53 static FILE *apdu_file = 0;
54 static COMSTACK conn = 0; /* our z-association */
55 static Z_IdAuthentication *auth = 0; /* our current auth definition */
56 char *databaseNames[128];
57 int num_databaseNames = 0;
58 static Z_External *record_last = 0;
59 static int setnumber = 0; /* current result set number */
60 static int smallSetUpperBound = 0;
61 static int largeSetLowerBound = 1;
62 static int mediumSetPresentNumber = 0;
63 static Z_ElementSetNames *elementSetNames = 0;
64 static int setno = 1; /* current set offset */
65 static enum oid_proto protocol = PROTO_Z3950; /* current app protocol */
66 static enum oid_value recordsyntax = VAL_USMARC;
67 static enum oid_value schema = VAL_NONE;
68 static int sent_close = 0;
69 static NMEM session_mem = NULL; /* memory handle for init-response */
70 static Z_InitResponse *session = 0; /* session parameters */
71 static char last_scan_line[512] = "0";
72 static char last_scan_query[512] = "0";
73 static char ccl_fields[512] = "default.bib";
74 static char* esPackageName = 0;
75 static char* yazProxy = 0;
77 static char last_cmd[32] = "?";
78 static FILE *marcdump = 0;
79 static char *refid = NULL;
87 static QueryType queryType = QueryType_Prefix;
90 static CCL_bibset bibset; /* CCL bibset handle */
93 ODR getODROutputStream()
98 void send_apdu(Z_APDU *a)
105 z_APDU(print, &a, 0, 0);
108 if (!z_APDU(out, &a, 0, 0))
110 odr_perror(out, "Encoding APDU");
113 buf = odr_getbuf(out, &len, 0);
114 /* printf ("sending APDU of size %d\n", len); */
115 if (cs_put(conn, buf, len) < 0)
117 fprintf(stderr, "cs_put: %s", cs_errmsg(cs_errno(conn)));
120 odr_reset(out); /* release the APDU structure */
123 static void print_refid (Z_ReferenceId *id)
127 printf ("ReferenceId: '%.*s'\n", id->len, id->buf);
131 static Z_ReferenceId *set_refid (ODR out)
136 id = (Z_ReferenceId *) odr_malloc (out, sizeof(*id));
137 id->size = id->len = strlen(refid);
138 id->buf = (unsigned char *) odr_malloc (out, id->len);
139 memcpy (id->buf, refid, id->len);
143 /* INIT SERVICE ------------------------------- */
145 static void send_initRequest(const char* type_and_host)
147 Z_APDU *apdu = zget_APDU(out, Z_APDU_initRequest);
148 Z_InitRequest *req = apdu->u.initRequest;
150 ODR_MASK_SET(req->options, Z_Options_search);
151 ODR_MASK_SET(req->options, Z_Options_present);
152 ODR_MASK_SET(req->options, Z_Options_namedResultSets);
153 ODR_MASK_SET(req->options, Z_Options_triggerResourceCtrl);
154 ODR_MASK_SET(req->options, Z_Options_scan);
155 ODR_MASK_SET(req->options, Z_Options_sort);
156 ODR_MASK_SET(req->options, Z_Options_extendedServices);
157 ODR_MASK_SET(req->options, Z_Options_delSet);
159 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_1);
160 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_2);
161 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_3);
163 *req->maximumRecordSize = 1024*1024;
164 *req->preferredMessageSize = 1024*1024;
166 req->idAuthentication = auth;
168 req->referenceId = set_refid (out);
171 yaz_oi_set_string_oidval(&req->otherInfo, out, VAL_PROXY,
175 printf("Sent initrequest.\n");
178 static int process_initResponse(Z_InitResponse *res)
180 /* save session parameters for later use */
181 session_mem = odr_extract_mem(in);
185 printf("Connection rejected by target.\n");
187 printf("Connection accepted by target.\n");
188 if (res->implementationId)
189 printf("ID : %s\n", res->implementationId);
190 if (res->implementationName)
191 printf("Name : %s\n", res->implementationName);
192 if (res->implementationVersion)
193 printf("Version: %s\n", res->implementationVersion);
194 if (res->userInformationField)
196 printf("UserInformationfield:\n");
197 if (!z_External(print, (Z_External**)&res-> userInformationField,
200 odr_perror(print, "Printing userinfo\n");
203 if (res->userInformationField->which == Z_External_octet)
205 printf("Guessing visiblestring:\n");
206 printf("'%s'\n", res->userInformationField->u. octet_aligned->buf);
211 if (ODR_MASK_GET(res->options, Z_Options_search))
213 if (ODR_MASK_GET(res->options, Z_Options_present))
215 if (ODR_MASK_GET(res->options, Z_Options_delSet))
217 if (ODR_MASK_GET(res->options, Z_Options_resourceReport))
218 printf (" resourceReport");
219 if (ODR_MASK_GET(res->options, Z_Options_resourceCtrl))
220 printf (" resourceCtrl");
221 if (ODR_MASK_GET(res->options, Z_Options_accessCtrl))
222 printf (" accessCtrl");
223 if (ODR_MASK_GET(res->options, Z_Options_scan))
225 if (ODR_MASK_GET(res->options, Z_Options_sort))
227 if (ODR_MASK_GET(res->options, Z_Options_extendedServices))
228 printf (" extendedServices");
229 if (ODR_MASK_GET(res->options, Z_Options_level_1Segmentation))
230 printf (" level1Segmentation");
231 if (ODR_MASK_GET(res->options, Z_Options_level_2Segmentation))
232 printf (" level2Segmentation");
233 if (ODR_MASK_GET(res->options, Z_Options_concurrentOperations))
234 printf (" concurrentOperations");
235 if (ODR_MASK_GET(res->options, Z_Options_namedResultSets))
236 printf (" namedResultSets");
242 static int cmd_base(char *arg)
249 printf("Usage: base <database> <database> ...\n");
252 for (i = 0; i<num_databaseNames; i++)
253 xfree (databaseNames[i]);
254 num_databaseNames = 0;
257 if (!(cp = strchr(arg, ' ')))
258 cp = arg + strlen(arg);
261 databaseNames[num_databaseNames] = (char *)xmalloc (1 + cp - arg);
262 memcpy (databaseNames[num_databaseNames], arg, cp - arg);
263 databaseNames[num_databaseNames++][cp - arg] = '\0';
272 int cmd_open(char *arg)
275 char type_and_host[101], base[101];
280 printf("Already connected.\n");
286 nmem_destroy (session_mem);
292 if (sscanf (arg, "%100[^/]/%100s", type_and_host, base) < 1)
297 conn = cs_create_host(yazProxy, 1, &add);
301 conn = cs_create_host(type_and_host, 1, &add);
306 printf ("Couldn't create comstack\n");
309 printf("Connecting...");
311 if (cs_connect(conn, add) < 0)
313 printf ("error = %s\n", cs_strerror(conn));
314 if (conn->cerrno == CSYSERR)
322 send_initRequest(type_and_host);
328 int cmd_authentication(char *arg)
330 static Z_IdAuthentication au;
331 static char open[256];
335 printf("Auth field set to null\n");
340 au.which = Z_IdAuthentication_open;
346 /* SEARCH SERVICE ------------------------------ */
348 static void display_variant(Z_Variant *v, int level)
352 for (i = 0; i < v->num_triples; i++)
354 printf("%*sclass=%d,type=%d", level * 4, "", *v->triples[i]->zclass,
355 *v->triples[i]->type);
356 if (v->triples[i]->which == Z_Triple_internationalString)
357 printf(",value=%s\n", v->triples[i]->value.internationalString);
363 static void display_grs1(Z_GenericRecord *r, int level)
369 for (i = 0; i < r->num_elements; i++)
373 printf("%*s", level * 4, "");
377 printf("%d,", *t->tagType);
380 if (t->tagValue->which == Z_StringOrNumeric_numeric)
381 printf("%d) ", *t->tagValue->u.numeric);
383 printf("%s) ", t->tagValue->u.string);
384 if (t->content->which == Z_ElementData_subtree)
387 display_grs1(t->content->u.subtree, level+1);
389 else if (t->content->which == Z_ElementData_string)
390 printf("%s\n", t->content->u.string);
391 else if (t->content->which == Z_ElementData_numeric)
392 printf("%d\n", *t->content->u.numeric);
393 else if (t->content->which == Z_ElementData_oid)
395 int *ip = t->content->u.oid;
398 if ((oent = oid_getentbyoid(t->content->u.oid)))
399 printf("OID: %s\n", oent->desc);
403 while (ip && *ip >= 0)
404 printf(" %d", *(ip++));
408 else if (t->content->which == Z_ElementData_noDataRequested)
409 printf("[No data requested]\n");
410 else if (t->content->which == Z_ElementData_elementEmpty)
411 printf("[Element empty]\n");
412 else if (t->content->which == Z_ElementData_elementNotThere)
413 printf("[Element not there]\n");
416 if (t->appliedVariant)
417 display_variant(t->appliedVariant, level+1);
418 if (t->metaData && t->metaData->supportedVariants)
422 printf("%*s---- variant list\n", (level+1)*4, "");
423 for (c = 0; c < t->metaData->num_supportedVariants; c++)
425 printf("%*svariant #%d\n", (level+1)*4, "", c);
426 display_variant(t->metaData->supportedVariants[c], level + 2);
432 static void print_record(const unsigned char *buf, size_t len)
435 for (i = 0; i<len; i++)
436 if ((buf[i] <= 126 && buf[i] >= 32) || strchr ("\n\r\t\f", buf[i]))
437 fputc (buf[i], stdout);
439 printf ("\\X%02X", buf[i]);
440 /* add newline if not already added ... */
441 if (i <= 0 || buf[i-1] != '\n')
442 fputc ('\n', stdout);
445 static void display_record(Z_DatabaseRecord *p)
447 Z_External *r = (Z_External*) p;
448 oident *ent = oid_getentbyoid(r->direct_reference);
452 * Tell the user what we got.
454 if (r->direct_reference)
456 printf("Record type: ");
458 printf("%s\n", ent->desc);
459 else if (!odr_oid(print, &r->direct_reference, 0, 0))
461 odr_perror(print, "print oid");
465 /* Check if this is a known, ASN.1 type tucked away in an octet string */
466 if (ent && r->which == Z_External_octet)
468 Z_ext_typeent *type = z_ext_getentbyref(ent->value);
474 * Call the given decoder to process the record.
476 odr_setbuf(in, (char*)p->u.octet_aligned->buf,
477 p->u.octet_aligned->len, 0);
478 if (!(*type->fun)(in, (char **)&rr, 0, 0))
480 odr_perror(in, "Decoding constructed record.");
481 fprintf(stderr, "[Near %d]\n", odr_offset(in));
482 fprintf(stderr, "Packet dump:\n---------\n");
483 odr_dumpBER(stderr, (char*)p->u.octet_aligned->buf,
484 p->u.octet_aligned->len);
485 fprintf(stderr, "---------\n");
489 * Note: we throw away the original, BER-encoded record here.
490 * Do something else with it if you want to keep it.
492 r->u.sutrs = (Z_SUTRS *) rr; /* we don't actually check the type here. */
493 r->which = type->what;
496 if (ent && ent->value == VAL_SOIF)
497 print_record((const unsigned char *) r->u.octet_aligned->buf, r->u.octet_aligned->len);
498 else if (r->which == Z_External_octet && p->u.octet_aligned->len)
500 const char *octet_buf = (char*)p->u.octet_aligned->buf;
501 if (ent->value == VAL_TEXT_XML || ent->value == VAL_APPLICATION_XML ||
502 ent->value == VAL_HTML)
503 print_record((const unsigned char *) octet_buf,
504 p->u.octet_aligned->len);
507 if (marc_display (octet_buf, NULL) <= 0)
509 printf ("ISO2709 decoding failed, dumping record as is:\n");
510 print_record((const unsigned char*) octet_buf,
511 p->u.octet_aligned->len);
515 fwrite (octet_buf, 1, p->u.octet_aligned->len, marcdump);
517 else if (ent && ent->value == VAL_SUTRS)
519 if (r->which != Z_External_sutrs)
521 printf("Expecting single SUTRS type for SUTRS.\n");
524 print_record(r->u.sutrs->buf, r->u.sutrs->len);
526 else if (ent && ent->value == VAL_GRS1)
528 if (r->which != Z_External_grs1)
530 printf("Expecting single GRS type for GRS.\n");
533 display_grs1(r->u.grs1, 0);
537 printf("Unknown record representation.\n");
538 if (!z_External(print, &r, 0, 0))
540 odr_perror(print, "Printing external");
547 static void display_diagrecs(Z_DiagRec **pp, int num)
551 Z_DefaultDiagFormat *r;
553 printf("Diagnostic message(s) from database:\n");
554 for (i = 0; i<num; i++)
556 Z_DiagRec *p = pp[i];
557 if (p->which != Z_DiagRec_defaultFormat)
559 printf("Diagnostic record not in default format.\n");
563 r = p->u.defaultFormat;
564 if (!(ent = oid_getentbyoid(r->diagnosticSetId)) ||
565 ent->oclass != CLASS_DIAGSET || ent->value != VAL_BIB1)
566 printf("Missing or unknown diagset\n");
567 printf(" [%d] %s", *r->condition, diagbib1_str(*r->condition));
571 case Z_DefaultDiagFormat_v2Addinfo:
572 printf (" -- v2 addinfo '%s'\n", r->u.v2Addinfo);
574 case Z_DefaultDiagFormat_v3Addinfo:
575 printf (" -- v3 addinfo '%s'\n", r->u.v3Addinfo);
579 if (r->addinfo && *r->addinfo)
580 printf(" -- '%s'\n", r->addinfo);
588 static void display_nameplusrecord(Z_NamePlusRecord *p)
591 printf("[%s]", p->databaseName);
592 if (p->which == Z_NamePlusRecord_surrogateDiagnostic)
593 display_diagrecs(&p->u.surrogateDiagnostic, 1);
594 else if (p->which == Z_NamePlusRecord_databaseRecord)
595 display_record(p->u.databaseRecord);
598 static void display_records(Z_Records *p)
602 if (p->which == Z_Records_NSD)
605 Z_DiagRec dr, *dr_p = &dr;
606 dr.which = Z_DiagRec_defaultFormat;
607 dr.u.defaultFormat = p->u.nonSurrogateDiagnostic;
608 display_diagrecs (&dr_p, 1);
610 display_diagrecs (&p->u.nonSurrogateDiagnostic, 1);
613 else if (p->which == Z_Records_multipleNSD)
614 display_diagrecs (p->u.multipleNonSurDiagnostics->diagRecs,
615 p->u.multipleNonSurDiagnostics->num_diagRecs);
618 printf("Records: %d\n", p->u.databaseOrSurDiagnostics->num_records);
619 for (i = 0; i < p->u.databaseOrSurDiagnostics->num_records; i++)
620 display_nameplusrecord(p->u.databaseOrSurDiagnostics->records[i]);
624 static int send_deleteResultSetRequest(char *arg)
629 Z_APDU *apdu = zget_APDU(out, Z_APDU_deleteResultSetRequest);
630 Z_DeleteResultSetRequest *req = apdu->u.deleteResultSetRequest;
632 req->referenceId = set_refid (out);
634 req->num_resultSetList =
635 sscanf (arg, "%30s %30s %30s %30s %30s %30s %30s %30s",
636 names[0], names[1], names[2], names[3],
637 names[4], names[5], names[6], names[7]);
639 req->deleteFunction = (int *)
640 odr_malloc (out, sizeof(*req->deleteFunction));
641 if (req->num_resultSetList > 0)
643 *req->deleteFunction = Z_DeleteRequest_list;
644 req->resultSetList = (char **)
645 odr_malloc (out, sizeof(*req->resultSetList)*
646 req->num_resultSetList);
647 for (i = 0; i<req->num_resultSetList; i++)
648 req->resultSetList[i] = names[i];
652 *req->deleteFunction = Z_DeleteRequest_all;
653 req->resultSetList = 0;
657 printf("Sent deleteResultSetRequest.\n");
661 static int send_searchRequest(char *arg)
663 Z_APDU *apdu = zget_APDU(out, Z_APDU_searchRequest);
664 Z_SearchRequest *req = apdu->u.searchRequest;
668 struct ccl_rpn_node *rpn = NULL;
672 Z_RPNQuery *RPNquery;
676 if (queryType == QueryType_CCL2RPN)
678 rpn = ccl_find_str(bibset, arg, &error, &pos);
681 printf("CCL ERROR: %s\n", ccl_err_msg(error));
686 req->referenceId = set_refid (out);
687 if (!strcmp(arg, "@big")) /* strictly for troublemaking */
689 static unsigned char big[2100];
692 /* send a very big referenceid to test transport stack etc. */
693 memset(big, 'A', 2100);
694 bigo.len = bigo.size = 2100;
696 req->referenceId = &bigo;
701 sprintf(setstring, "%d", ++setnumber);
702 req->resultSetName = setstring;
704 *req->smallSetUpperBound = smallSetUpperBound;
705 *req->largeSetLowerBound = largeSetLowerBound;
706 *req->mediumSetPresentNumber = mediumSetPresentNumber;
707 if (smallSetUpperBound > 0 || (largeSetLowerBound > 1 &&
708 mediumSetPresentNumber > 0))
712 prefsyn.proto = protocol;
713 prefsyn.oclass = CLASS_RECSYN;
714 prefsyn.value = recordsyntax;
715 req->preferredRecordSyntax =
716 odr_oiddup(out, oid_ent_to_oid(&prefsyn, oid));
717 req->smallSetElementSetNames =
718 req->mediumSetElementSetNames = elementSetNames;
720 req->num_databaseNames = num_databaseNames;
721 req->databaseNames = databaseNames;
727 case QueryType_Prefix:
728 query.which = Z_Query_type_1;
729 RPNquery = p_query_rpn (out, protocol, arg);
732 printf("Prefix query error\n");
735 query.u.type_1 = RPNquery;
738 query.which = Z_Query_type_2;
739 query.u.type_2 = &ccl_query;
740 ccl_query.buf = (unsigned char*) arg;
741 ccl_query.len = strlen(arg);
744 case QueryType_CCL2RPN:
745 query.which = Z_Query_type_1;
746 RPNquery = ccl_rpn_query(out, rpn);
749 printf ("Couldn't convert from CCL to RPN\n");
752 query.u.type_1 = RPNquery;
753 ccl_rpn_delete (rpn);
757 printf ("Unsupported query type\n");
762 printf("Sent searchRequest.\n");
766 static int process_searchResponse(Z_SearchResponse *res)
768 printf ("Received SearchResponse.\n");
769 print_refid (res->referenceId);
770 if (*res->searchStatus)
771 printf("Search was a success.\n");
773 printf("Search was a bloomin' failure.\n");
774 printf("Number of hits: %d, setno %d\n",
775 *res->resultCount, setnumber);
776 printf("records returned: %d\n",
777 *res->numberOfRecordsReturned);
778 setno += *res->numberOfRecordsReturned;
780 display_records(res->records);
784 static void print_level(int iLevel)
787 for (i = 0; i < iLevel * 4; i++)
791 static void print_int(int iLevel, const char *pTag, int *pInt)
796 printf("%s: %d\n", pTag, *pInt);
800 static void print_string(int iLevel, const char *pTag, const char *pString)
805 printf("%s: %s\n", pTag, pString);
809 static void print_oid(int iLevel, const char *pTag, Odr_oid *pOid)
817 for (; *pInt != -1; pInt++)
818 printf(" %d", *pInt);
823 static void print_referenceId(int iLevel, Z_ReferenceId *referenceId)
825 if (referenceId != NULL)
830 printf("Ref Id (%d, %d): ", referenceId->len, referenceId->size);
831 for (i = 0; i < referenceId->len; i++)
832 printf("%c", referenceId->buf[i]);
837 static void print_string_or_numeric(int iLevel, const char *pTag, Z_StringOrNumeric *pStringNumeric)
839 if (pStringNumeric != NULL)
841 switch (pStringNumeric->which)
843 case Z_StringOrNumeric_string:
844 print_string(iLevel, pTag, pStringNumeric->u.string);
847 case Z_StringOrNumeric_numeric:
848 print_int(iLevel, pTag, pStringNumeric->u.numeric);
853 printf("%s: valid type for Z_StringOrNumeric\n", pTag);
859 static void print_universe_report_duplicate(int iLevel, Z_UniverseReportDuplicate *pUniverseReportDuplicate)
861 if (pUniverseReportDuplicate != NULL)
864 printf("Universe Report Duplicate: \n");
866 print_string_or_numeric(iLevel, "Hit No", pUniverseReportDuplicate->hitno);
870 static void print_universe_report_hits(int iLevel, Z_UniverseReportHits *pUniverseReportHits)
872 if (pUniverseReportHits != NULL)
875 printf("Universe Report Hits: \n");
877 print_string_or_numeric(iLevel, "Database", pUniverseReportHits->database);
878 print_string_or_numeric(iLevel, "Hits", pUniverseReportHits->hits);
882 static void print_universe_report(int iLevel, Z_UniverseReport *pUniverseReport)
884 if (pUniverseReport != NULL)
887 printf("Universe Report: \n");
889 print_int(iLevel, "Total Hits", pUniverseReport->totalHits);
890 switch (pUniverseReport->which)
892 case Z_UniverseReport_databaseHits:
893 print_universe_report_hits(iLevel, pUniverseReport->u.databaseHits);
896 case Z_UniverseReport_duplicate:
897 print_universe_report_duplicate(iLevel, pUniverseReport->u.duplicate);
902 printf("Type: %d\n", pUniverseReport->which);
908 static void print_external(int iLevel, Z_External *pExternal)
910 if (pExternal != NULL)
913 printf("External: \n");
915 print_oid(iLevel, "Direct Reference", pExternal->direct_reference);
916 print_int(iLevel, "InDirect Reference", pExternal->indirect_reference);
917 print_string(iLevel, "Descriptor", pExternal->descriptor);
918 switch (pExternal->which)
920 case Z_External_universeReport:
921 print_universe_report(iLevel, pExternal->u.universeReport);
926 printf("Type: %d\n", pExternal->which);
932 static int process_resourceControlRequest (Z_ResourceControlRequest *req)
934 printf ("Received ResourceControlRequest.\n");
935 print_referenceId(1, req->referenceId);
936 print_int(1, "Suspended Flag", req->suspendedFlag);
937 print_int(1, "Partial Results Available", req->partialResultsAvailable);
938 print_int(1, "Response Required", req->responseRequired);
939 print_int(1, "Triggered Request Flag", req->triggeredRequestFlag);
940 print_external(1, req->resourceReport);
944 void process_ESResponse(Z_ExtendedServicesResponse *res)
946 printf("process_ESResponse status=");
947 switch (*res->operationStatus)
949 case Z_ExtendedServicesResponse_done:
952 case Z_ExtendedServicesResponse_accepted:
953 printf ("accepted\n");
955 case Z_ExtendedServicesResponse_failure:
956 printf ("failure\n");
957 display_diagrecs(res->diagnostics, res->num_diagnostics);
960 if ( (*res->operationStatus != Z_ExtendedServicesResponse_failure) &&
961 (res->num_diagnostics != 0) ) {
962 display_diagrecs(res->diagnostics, res->num_diagnostics);
969 const char *get_ill_element (void *clientData, const char *element)
971 printf ("%s\n", element);
975 static Z_External *create_external_itemRequest()
977 struct ill_get_ctl ctl;
978 ILL_ItemRequest *req;
980 int item_request_size = 0;
981 char *item_request_buf = 0;
985 ctl.f = get_ill_element;
987 req = ill_get_ItemRequest(&ctl, "ill", 0);
989 printf ("ill_get_ItemRequest failed\n");
991 if (!ill_ItemRequest (out, &req, 0, 0))
995 ill_ItemRequest(print, &req, 0, 0);
998 item_request_buf = odr_getbuf (out, &item_request_size, 0);
999 if (item_request_buf)
1000 odr_setbuf (out, item_request_buf, item_request_size, 1);
1001 printf ("Couldn't encode ItemRequest, size %d\n", item_request_size);
1008 item_request_buf = odr_getbuf (out, &item_request_size, 0);
1009 oid.proto = PROTO_GENERAL;
1010 oid.oclass = CLASS_GENERAL;
1011 oid.value = VAL_ISO_ILL_1;
1013 r = (Z_External *) odr_malloc (out, sizeof(*r));
1014 r->direct_reference = odr_oiddup(out,oid_getoidbyent(&oid));
1015 r->indirect_reference = 0;
1017 r->which = Z_External_single;
1019 r->u.single_ASN1_type = (Odr_oct *)
1020 odr_malloc (out, sizeof(*r->u.single_ASN1_type));
1021 r->u.single_ASN1_type->buf = (unsigned char *)
1022 odr_malloc (out, item_request_size);
1023 r->u.single_ASN1_type->len = item_request_size;
1024 r->u.single_ASN1_type->size = item_request_size;
1025 memcpy (r->u.single_ASN1_type->buf, item_request_buf,
1027 printf ("len = %d\n", item_request_size);
1033 #ifdef YAZ_MODULE_ill
1034 static Z_External *create_external_ILL_APDU(int which)
1036 struct ill_get_ctl ctl;
1039 int ill_request_size = 0;
1040 char *ill_request_buf = 0;
1044 ctl.f = get_ill_element;
1046 ill_apdu = ill_get_APDU(&ctl, "ill", 0);
1048 if (!ill_APDU (out, &ill_apdu, 0, 0))
1052 printf ("-------------------\n");
1053 ill_APDU(print, &ill_apdu, 0, 0);
1055 printf ("-------------------\n");
1057 ill_request_buf = odr_getbuf (out, &ill_request_size, 0);
1058 if (ill_request_buf)
1059 odr_setbuf (out, ill_request_buf, ill_request_size, 1);
1060 printf ("Couldn't encode ILL-Request, size %d\n", ill_request_size);
1066 ill_request_buf = odr_getbuf (out, &ill_request_size, 0);
1068 oid.proto = PROTO_GENERAL;
1069 oid.oclass = CLASS_GENERAL;
1070 oid.value = VAL_ISO_ILL_1;
1072 r = (Z_External *) odr_malloc (out, sizeof(*r));
1073 r->direct_reference = odr_oiddup(out,oid_getoidbyent(&oid));
1074 r->indirect_reference = 0;
1076 r->which = Z_External_single;
1078 r->u.single_ASN1_type = (Odr_oct *)
1079 odr_malloc (out, sizeof(*r->u.single_ASN1_type));
1080 r->u.single_ASN1_type->buf = (unsigned char *)
1081 odr_malloc (out, ill_request_size);
1082 r->u.single_ASN1_type->len = ill_request_size;
1083 r->u.single_ASN1_type->size = ill_request_size;
1084 memcpy (r->u.single_ASN1_type->buf, ill_request_buf, ill_request_size);
1085 printf ("len = %d\n", ill_request_size);
1092 static Z_External *create_ItemOrderExternal(const char *type, int itemno)
1094 Z_External *r = (Z_External *) odr_malloc(out, sizeof(Z_External));
1095 oident ItemOrderRequest;
1097 ItemOrderRequest.proto = PROTO_Z3950;
1098 ItemOrderRequest.oclass = CLASS_EXTSERV;
1099 ItemOrderRequest.value = VAL_ITEMORDER;
1101 r->direct_reference = odr_oiddup(out,oid_getoidbyent(&ItemOrderRequest));
1102 r->indirect_reference = 0;
1105 r->which = Z_External_itemOrder;
1107 r->u.itemOrder = (Z_ItemOrder *) odr_malloc(out,sizeof(Z_ItemOrder));
1108 memset(r->u.itemOrder, 0, sizeof(Z_ItemOrder));
1110 r->u.itemOrder->which=Z_IOItemOrder_esRequest;
1112 r->u.itemOrder->which=Z_ItemOrder_esRequest;
1115 r->u.itemOrder->u.esRequest = (Z_IORequest *)
1116 odr_malloc(out,sizeof(Z_IORequest));
1117 memset(r->u.itemOrder->u.esRequest, 0, sizeof(Z_IORequest));
1119 r->u.itemOrder->u.esRequest->toKeep = (Z_IOOriginPartToKeep *)
1120 odr_malloc(out,sizeof(Z_IOOriginPartToKeep));
1121 memset(r->u.itemOrder->u.esRequest->toKeep, 0, sizeof(Z_IOOriginPartToKeep));
1122 r->u.itemOrder->u.esRequest->notToKeep = (Z_IOOriginPartNotToKeep *)
1123 odr_malloc(out,sizeof(Z_IOOriginPartNotToKeep));
1124 memset(r->u.itemOrder->u.esRequest->notToKeep, 0, sizeof(Z_IOOriginPartNotToKeep));
1126 r->u.itemOrder->u.esRequest->toKeep->supplDescription = NULL;
1127 r->u.itemOrder->u.esRequest->toKeep->contact = NULL;
1128 r->u.itemOrder->u.esRequest->toKeep->addlBilling = NULL;
1130 r->u.itemOrder->u.esRequest->notToKeep->resultSetItem =
1131 (Z_IOResultSetItem *) odr_malloc(out, sizeof(Z_IOResultSetItem));
1132 memset(r->u.itemOrder->u.esRequest->notToKeep->resultSetItem, 0, sizeof(Z_IOResultSetItem));
1133 r->u.itemOrder->u.esRequest->notToKeep->resultSetItem->resultSetId = "1";
1135 r->u.itemOrder->u.esRequest->notToKeep->resultSetItem->item =
1136 (int *) odr_malloc(out, sizeof(int));
1137 *r->u.itemOrder->u.esRequest->notToKeep->resultSetItem->item = itemno;
1140 if (!strcmp (type, "item") || !strcmp(type, "2"))
1142 printf ("using item-request\n");
1143 r->u.itemOrder->u.esRequest->notToKeep->itemRequest =
1144 create_external_itemRequest();
1146 else if (!strcmp(type, "ill") || !strcmp(type, "1"))
1148 printf ("using ILL-request\n");
1149 r->u.itemOrder->u.esRequest->notToKeep->itemRequest =
1150 create_external_ILL_APDU(ILL_APDU_ILL_Request);
1152 else if (!strcmp(type, "xml") || !strcmp(type, "3"))
1154 const char *xml_buf =
1156 " <type>request</type>\n"
1157 " <libraryNo>000200</libraryNo>\n"
1158 " <borrowerTicketNo> 1212 </borrowerTicketNo>\n"
1160 r->u.itemOrder->u.esRequest->notToKeep->itemRequest =
1161 z_ext_record (out, VAL_TEXT_XML, xml_buf, strlen(xml_buf));
1164 r->u.itemOrder->u.esRequest->notToKeep->itemRequest = 0;
1167 r->u.itemOrder->u.esRequest->notToKeep->itemRequest = 0;
1172 static int send_itemorder(const char *type, int itemno)
1174 Z_APDU *apdu = zget_APDU(out, Z_APDU_extendedServicesRequest);
1175 Z_ExtendedServicesRequest *req = apdu->u.extendedServicesRequest;
1176 oident ItemOrderRequest;
1178 ItemOrderRequest.proto = PROTO_Z3950;
1179 ItemOrderRequest.oclass = CLASS_EXTSERV;
1180 ItemOrderRequest.value = VAL_ITEMORDER;
1181 req->packageType = odr_oiddup(out,oid_getoidbyent(&ItemOrderRequest));
1182 req->packageName = esPackageName;
1184 req->taskSpecificParameters = create_ItemOrderExternal(type, itemno);
1190 static int cmd_update(char *arg)
1192 Z_APDU *apdu = zget_APDU(out, Z_APDU_extendedServicesRequest );
1193 Z_ExtendedServicesRequest *req = apdu->u.extendedServicesRequest;
1196 Z_IUOriginPartToKeep *toKeep;
1197 Z_IUSuppliedRecords *notToKeep;
1199 printf ("Update request\n");
1204 update_oid.proto = PROTO_Z3950;
1205 update_oid.oclass = CLASS_EXTSERV;
1206 update_oid.value = VAL_DBUPDATE;
1207 oid_ent_to_oid (&update_oid, oid);
1208 req->packageType = odr_oiddup(out,oid);
1209 req->packageName = esPackageName;
1211 req->referenceId = set_refid (out);
1213 r = req->taskSpecificParameters = (Z_External *)
1214 odr_malloc (out, sizeof(*r));
1215 r->direct_reference = odr_oiddup(out,oid);
1216 r->indirect_reference = 0;
1218 r->which = Z_External_update;
1219 r->u.update = (Z_IUUpdate *) odr_malloc(out, sizeof(*r->u.update));
1220 r->u.update->which = Z_IUUpdate_esRequest;
1221 r->u.update->u.esRequest = (Z_IUUpdateEsRequest *)
1222 odr_malloc(out, sizeof(*r->u.update->u.esRequest));
1223 toKeep = r->u.update->u.esRequest->toKeep = (Z_IUOriginPartToKeep *)
1224 odr_malloc(out, sizeof(*r->u.update->u.esRequest->toKeep));
1225 toKeep->databaseName = databaseNames[0];
1227 toKeep->elementSetName = 0;
1228 toKeep->actionQualifier = 0;
1229 toKeep->action = (int *) odr_malloc(out, sizeof(*toKeep->action));
1230 *toKeep->action = Z_IUOriginPartToKeep_recordInsert;
1232 notToKeep = r->u.update->u.esRequest->notToKeep = (Z_IUSuppliedRecords *)
1233 odr_malloc(out, sizeof(*r->u.update->u.esRequest->notToKeep));
1235 notToKeep->elements = (Z_IUSuppliedRecords_elem **)
1236 odr_malloc(out, sizeof(*notToKeep->elements));
1237 notToKeep->elements[0] = (Z_IUSuppliedRecords_elem *)
1238 odr_malloc(out, sizeof(**notToKeep->elements));
1239 notToKeep->elements[0]->u.number = 0;
1240 notToKeep->elements[0]->supplementalId = 0;
1241 notToKeep->elements[0]->correlationInfo = 0;
1242 notToKeep->elements[0]->record = record_last;
1249 /* II : Added to do DALI Item Order Extended services request */
1250 static int cmd_itemorder(char *arg)
1255 if (sscanf (arg, "%10s %d", type, &itemno) != 2)
1258 printf("Item order request\n");
1260 send_itemorder(type, itemno);
1264 static int cmd_find(char *arg)
1268 printf("Find what?\n");
1273 printf("Not connected yet\n");
1276 if (!send_searchRequest(arg))
1281 static int cmd_delete(char *arg)
1285 printf("Not connected yet\n");
1288 if (!send_deleteResultSetRequest(arg))
1293 static int cmd_ssub(char *arg)
1295 if (!(smallSetUpperBound = atoi(arg)))
1300 static int cmd_lslb(char *arg)
1302 if (!(largeSetLowerBound = atoi(arg)))
1307 static int cmd_mspn(char *arg)
1309 if (!(mediumSetPresentNumber = atoi(arg)))
1314 static int cmd_status(char *arg)
1316 printf("smallSetUpperBound: %d\n", smallSetUpperBound);
1317 printf("largeSetLowerBound: %d\n", largeSetLowerBound);
1318 printf("mediumSetPresentNumber: %d\n", mediumSetPresentNumber);
1322 static int cmd_setnames(char *arg)
1326 printf("Set numbering enabled.\n");
1331 printf("Set numbering disabled.\n");
1337 /* PRESENT SERVICE ----------------------------- */
1339 static int send_presentRequest(char *arg)
1341 Z_APDU *apdu = zget_APDU(out, Z_APDU_presentRequest);
1342 Z_PresentRequest *req = apdu->u.presentRequest;
1343 Z_RecordComposition compo;
1348 char setstring[100];
1350 req->referenceId = set_refid (out);
1351 if ((p = strchr(arg, '+')))
1358 if (p && (p=strchr(p+1, '+')))
1360 strcpy (setstring, p+1);
1361 req->resultSetId = setstring;
1363 else if (setnumber >= 0)
1365 sprintf(setstring, "%d", setnumber);
1366 req->resultSetId = setstring;
1371 static Z_Range range;
1372 static Z_Range *rangep = ⦥
1373 req->num_ranges = 1;
1375 req->resultSetStartPoint = &setno;
1376 req->numberOfRecordsRequested = &nos;
1377 prefsyn.proto = protocol;
1378 prefsyn.oclass = CLASS_RECSYN;
1379 prefsyn.value = recordsyntax;
1380 req->preferredRecordSyntax =
1381 odr_oiddup (out, oid_ent_to_oid(&prefsyn, oid));
1383 if (schema != VAL_NONE)
1387 prefschema.proto = protocol;
1388 prefschema.oclass = CLASS_SCHEMA;
1389 prefschema.value = schema;
1391 req->recordComposition = &compo;
1392 compo.which = Z_RecordComp_complex;
1393 compo.u.complex = (Z_CompSpec *)
1394 odr_malloc(out, sizeof(*compo.u.complex));
1395 compo.u.complex->selectAlternativeSyntax = (bool_t *)
1396 odr_malloc(out, sizeof(bool_t));
1397 *compo.u.complex->selectAlternativeSyntax = 0;
1399 compo.u.complex->generic = (Z_Specification *)
1400 odr_malloc(out, sizeof(*compo.u.complex->generic));
1401 compo.u.complex->generic->schema = (Odr_oid *)
1402 odr_oiddup(out, oid_ent_to_oid(&prefschema, oid));
1403 if (!compo.u.complex->generic->schema)
1405 /* OID wasn't a schema! Try record syntax instead. */
1406 prefschema.oclass = CLASS_RECSYN;
1407 compo.u.complex->generic->schema = (Odr_oid *)
1408 odr_oiddup(out, oid_ent_to_oid(&prefschema, oid));
1410 if (!elementSetNames)
1411 compo.u.complex->generic->elementSpec = 0;
1414 compo.u.complex->generic->elementSpec = (Z_ElementSpec *)
1415 odr_malloc(out, sizeof(Z_ElementSpec));
1416 compo.u.complex->generic->elementSpec->which =
1417 Z_ElementSpec_elementSetName;
1418 compo.u.complex->generic->elementSpec->u.elementSetName =
1419 elementSetNames->u.generic;
1421 compo.u.complex->num_dbSpecific = 0;
1422 compo.u.complex->dbSpecific = 0;
1423 compo.u.complex->num_recordSyntax = 0;
1424 compo.u.complex->recordSyntax = 0;
1426 else if (elementSetNames)
1428 req->recordComposition = &compo;
1429 compo.which = Z_RecordComp_simple;
1430 compo.u.simple = elementSetNames;
1433 printf("Sent presentRequest (%d+%d).\n", setno, nos);
1437 void process_close(Z_Close *req)
1439 Z_APDU *apdu = zget_APDU(out, Z_APDU_close);
1440 Z_Close *res = apdu->u.close;
1442 static char *reasons[] =
1447 "cost limit reached",
1449 "security violation",
1456 printf("Reason: %s, message: %s\n", reasons[*req->closeReason],
1457 req->diagnosticInformation ? req->diagnosticInformation : "NULL");
1464 nmem_destroy (session_mem);
1471 *res->closeReason = Z_Close_finished;
1473 printf("Sent response.\n");
1478 static int cmd_show(char *arg)
1482 printf("Not connected yet\n");
1485 if (!send_presentRequest(arg))
1490 int cmd_quit(char *arg)
1492 printf("See you later, alligator.\n");
1497 int cmd_cancel(char *arg)
1499 Z_APDU *apdu = zget_APDU(out, Z_APDU_triggerResourceControlRequest);
1500 Z_TriggerResourceControlRequest *req =
1501 apdu->u.triggerResourceControlRequest;
1506 printf("Session not initialized yet\n");
1509 if (!ODR_MASK_GET(session->options, Z_Options_triggerResourceCtrl))
1511 printf("Target doesn't support cancel (trigger resource ctrl)\n");
1514 *req->requestedAction = Z_TriggerResourceCtrl_cancel;
1515 req->resultSetWanted = &rfalse;
1518 printf("Sent cancel request\n");
1522 int send_scanrequest(const char *query, int pp, int num, const char *term)
1524 Z_APDU *apdu = zget_APDU(out, Z_APDU_scanRequest);
1525 Z_ScanRequest *req = apdu->u.scanRequest;
1530 if (queryType == QueryType_CCL2RPN)
1534 struct ccl_rpn_node *rpn;
1536 rpn = ccl_find_str (bibset, query, &error, &pos);
1539 printf("CCL ERROR: %s\n", ccl_err_msg(error));
1543 bib1.proto = PROTO_Z3950;
1544 bib1.oclass = CLASS_ATTSET;
1545 bib1.value = VAL_BIB1;
1546 req->attributeSet = oid_ent_to_oid (&bib1, oid);
1547 if (!(req->termListAndStartPoint = ccl_scan_query (out, rpn)))
1549 printf("Couldn't convert CCL to Scan term\n");
1552 ccl_rpn_delete (rpn);
1555 if (use_rpn && !(req->termListAndStartPoint =
1556 p_query_scan(out, protocol, &req->attributeSet, query)))
1558 printf("Prefix query error\n");
1563 if (req->termListAndStartPoint->term &&
1564 req->termListAndStartPoint->term->which == Z_Term_general &&
1565 req->termListAndStartPoint->term->u.general)
1567 req->termListAndStartPoint->term->u.general->buf =
1568 (unsigned char *) odr_strdup(out, term);
1569 req->termListAndStartPoint->term->u.general->len =
1570 req->termListAndStartPoint->term->u.general->size =
1574 req->referenceId = set_refid (out);
1575 req->num_databaseNames = num_databaseNames;
1576 req->databaseNames = databaseNames;
1577 req->numberOfTermsRequested = #
1578 req->preferredPositionInResponse = &pp;
1583 int send_sortrequest(char *arg, int newset)
1585 Z_APDU *apdu = zget_APDU(out, Z_APDU_sortRequest);
1586 Z_SortRequest *req = apdu->u.sortRequest;
1587 Z_SortKeySpecList *sksl = (Z_SortKeySpecList *)
1588 odr_malloc (out, sizeof(*sksl));
1592 sprintf (setstring, "%d", setnumber);
1594 sprintf (setstring, "default");
1596 req->referenceId = set_refid (out);
1599 req->num_inputResultSetNames = 1;
1600 req->inputResultSetNames = (Z_InternationalString **)
1601 odr_malloc (out, sizeof(*req->inputResultSetNames));
1602 req->inputResultSetNames[0] = odr_strdup (out, setstring);
1604 req->inputResultSetNames =
1605 (Z_StringList *)odr_malloc (out, sizeof(*req->inputResultSetNames));
1606 req->inputResultSetNames->num_strings = 1;
1607 req->inputResultSetNames->strings =
1608 (char **)odr_malloc (out, sizeof(*req->inputResultSetNames->strings));
1609 req->inputResultSetNames->strings[0] =
1610 odr_strdup (out, setstring);
1613 if (newset && setnumber >= 0)
1614 sprintf (setstring, "%d", ++setnumber);
1616 req->sortedResultSetName = odr_strdup (out, setstring);
1618 req->sortSequence = yaz_sort_spec (out, arg);
1619 if (!req->sortSequence)
1621 printf ("Missing sort specifications\n");
1628 void display_term(Z_TermInfo *t)
1630 if (t->term->which == Z_Term_general)
1632 printf("%.*s", t->term->u.general->len, t->term->u.general->buf);
1633 sprintf(last_scan_line, "%.*s", t->term->u.general->len,
1634 t->term->u.general->buf);
1637 printf("Term (not general)");
1638 if (t->globalOccurrences)
1639 printf (" (%d)\n", *t->globalOccurrences);
1644 void process_scanResponse(Z_ScanResponse *res)
1647 Z_Entry **entries = NULL;
1648 int num_entries = 0;
1650 printf("Received ScanResponse\n");
1651 print_refid (res->referenceId);
1652 printf("%d entries", *res->numberOfEntriesReturned);
1653 if (res->positionOfTerm)
1654 printf (", position=%d", *res->positionOfTerm);
1656 if (*res->scanStatus != Z_Scan_success)
1657 printf("Scan returned code %d\n", *res->scanStatus);
1660 if ((entries = res->entries->entries))
1661 num_entries = res->entries->num_entries;
1662 for (i = 0; i < num_entries; i++)
1664 int pos_term = res->positionOfTerm ? *res->positionOfTerm : -1;
1665 if (entries[i]->which == Z_Entry_termInfo)
1667 printf("%c ", i + 1 == pos_term ? '*' : ' ');
1668 display_term(entries[i]->u.termInfo);
1671 display_diagrecs(&entries[i]->u.surrogateDiagnostic, 1);
1673 if (res->entries->nonsurrogateDiagnostics)
1674 display_diagrecs (res->entries->nonsurrogateDiagnostics,
1675 res->entries->num_nonsurrogateDiagnostics);
1678 void process_sortResponse(Z_SortResponse *res)
1680 printf("Received SortResponse: status=");
1681 switch (*res->sortStatus)
1683 case Z_SortStatus_success:
1684 printf ("success"); break;
1685 case Z_SortStatus_partial_1:
1686 printf ("partial"); break;
1687 case Z_SortStatus_failure:
1688 printf ("failure"); break;
1690 printf ("unknown (%d)", *res->sortStatus);
1693 print_refid (res->referenceId);
1695 if (res->diagnostics)
1696 display_diagrecs(res->diagnostics,
1697 res->num_diagnostics);
1699 if (res->diagnostics)
1700 display_diagrecs(res->diagnostics->diagRecs,
1701 res->diagnostics->num_diagRecs);
1705 void process_deleteResultSetResponse (Z_DeleteResultSetResponse *res)
1707 printf("Got deleteResultSetResponse status=%d\n",
1708 *res->deleteOperationStatus);
1709 if (res->deleteListStatuses)
1712 for (i = 0; i < res->deleteListStatuses->num; i++)
1714 printf ("%s status=%d\n", res->deleteListStatuses->elements[i]->id,
1715 *res->deleteListStatuses->elements[i]->status);
1720 int cmd_sort_generic(char *arg, int newset)
1724 printf("Session not initialized yet\n");
1727 if (!ODR_MASK_GET(session->options, Z_Options_sort))
1729 printf("Target doesn't support sort\n");
1734 if (send_sortrequest(arg, newset) < 0)
1741 int cmd_sort(char *arg)
1743 return cmd_sort_generic (arg, 0);
1746 int cmd_sort_newset (char *arg)
1748 return cmd_sort_generic (arg, 1);
1751 int cmd_scan(char *arg)
1755 printf("Session not initialized yet\n");
1758 if (!ODR_MASK_GET(session->options, Z_Options_scan))
1760 printf("Target doesn't support scan\n");
1765 strcpy (last_scan_query, arg);
1766 if (send_scanrequest(arg, 1, 20, 0) < 0)
1771 if (send_scanrequest(last_scan_query, 1, 20, last_scan_line) < 0)
1777 int cmd_schema(char *arg)
1784 schema = oid_getvalbyname (arg);
1785 if (schema == VAL_NONE)
1787 printf ("unknown schema\n");
1793 int cmd_format(char *arg)
1797 printf("Usage: format <recordsyntax>\n");
1800 recordsyntax = oid_getvalbyname (arg);
1801 if (recordsyntax == VAL_NONE)
1803 printf ("unknown record syntax\n");
1809 int cmd_elements(char *arg)
1811 static Z_ElementSetNames esn;
1812 static char what[100];
1816 elementSetNames = 0;
1820 esn.which = Z_ElementSetNames_generic;
1821 esn.u.generic = what;
1822 elementSetNames = &esn;
1826 int cmd_attributeset(char *arg)
1832 printf("Usage: attributeset <setname>\n");
1835 sscanf(arg, "%s", what);
1836 if (p_query_attset (what))
1838 printf("Unknown attribute set name\n");
1844 int cmd_querytype (char *arg)
1846 if (!strcmp (arg, "ccl"))
1847 queryType = QueryType_CCL;
1848 else if (!strcmp (arg, "prefix") || !strcmp(arg, "rpn"))
1849 queryType = QueryType_Prefix;
1851 else if (!strcmp (arg, "ccl2rpn") || !strcmp (arg, "cclrpn"))
1852 queryType = QueryType_CCL2RPN;
1856 printf ("Querytype must be one of:\n");
1857 printf (" prefix - Prefix query\n");
1858 printf (" ccl - CCL query\n");
1860 printf (" ccl2rpn - CCL query converted to RPN\n");
1867 int cmd_refid (char *arg)
1873 refid = (char *) xmalloc (strlen(arg)+1);
1874 strcpy (refid, arg);
1879 int cmd_close(char *arg)
1886 apdu = zget_APDU(out, Z_APDU_close);
1887 req = apdu->u.close;
1888 *req->closeReason = Z_Close_finished;
1890 printf("Sent close request.\n");
1895 int cmd_packagename(char* arg) {
1896 xfree (esPackageName);
1897 esPackageName = NULL;
1900 esPackageName = (char *) xmalloc (strlen(arg)+1);
1901 strcpy (esPackageName, arg);
1906 int cmd_proxy(char* arg) {
1911 yazProxy = (char *) xmalloc (strlen(arg)+1);
1912 strcpy (yazProxy, arg);
1917 static void initialize(void)
1922 if (!(out = odr_createmem(ODR_ENCODE)) ||
1923 !(in = odr_createmem(ODR_DECODE)) ||
1924 !(print = odr_createmem(ODR_PRINT)))
1926 fprintf(stderr, "failed to allocate ODR streams\n");
1929 setvbuf(stdout, 0, _IONBF, 0);
1931 odr_setprint(print, apdu_file);
1934 bibset = ccl_qual_mk ();
1935 inf = fopen (ccl_fields, "r");
1938 ccl_qual_file (bibset, inf);
1942 cmd_base("Default");
1945 static int client(int wait)
1949 int (*fun)(char *arg);
1952 {"open", cmd_open, "('tcp'|'osi')':'[<tsel>'/']<host>[':'<port>]"},
1953 {"quit", cmd_quit, ""},
1954 {"find", cmd_find, "<query>"},
1955 {"delete", cmd_delete, "<setname>"},
1956 {"base", cmd_base, "<base-name>"},
1957 {"show", cmd_show, "<rec#>['+'<#recs>['+'<setname>]]"},
1958 {"scan", cmd_scan, "<term>"},
1959 {"sort", cmd_sort, "<sortkey> <flag> <sortkey> <flag> ..."},
1960 {"sort+", cmd_sort_newset, "<sortkey> <flag> <sortkey> <flag> ..."},
1961 {"authentication", cmd_authentication, "<acctstring>"},
1962 {"lslb", cmd_lslb, "<largeSetLowerBound>"},
1963 {"ssub", cmd_ssub, "<smallSetUpperBound>"},
1964 {"mspn", cmd_mspn, "<mediumSetPresentNumber>"},
1965 {"status", cmd_status, ""},
1966 {"setnames", cmd_setnames, ""},
1967 {"cancel", cmd_cancel, ""},
1968 {"format", cmd_format, "<recordsyntax>"},
1969 {"schema", cmd_schema, "<schema>"},
1970 {"elements", cmd_elements, "<elementSetName>"},
1971 {"close", cmd_close, ""},
1972 {"attributeset", cmd_attributeset, "<attrset>"},
1973 {"querytype", cmd_querytype, "<type>"},
1974 {"refid", cmd_refid, "<id>"},
1975 {"itemorder", cmd_itemorder, "ill|item <itemno>"},
1976 {"update", cmd_update, "<item>"},
1977 {"packagename", cmd_packagename, "<packagename>"},
1978 {"proxy", cmd_proxy, "('tcp'|'osi')':'[<tsel>'/']<host>[':'<port>]"},
1980 /* Server Admin Functions */
1981 {"adm-reindex", cmd_adm_reindex, "<database-name>"},
1982 {"adm-truncate", cmd_adm_truncate, "('database'|'index')<object-name>"},
1983 {"adm-create", cmd_adm_create, ""},
1984 {"adm-drop", cmd_adm_drop, "('database'|'index')<object-name>"},
1985 {"adm-import", cmd_adm_import, "<record-type> <dir> <pattern>"},
1986 {"adm-refresh", cmd_adm_refresh, ""},
1987 {"adm-commit", cmd_adm_commit, ""},
1988 {"adm-shutdown", cmd_adm_shutdown, ""},
1989 {"adm-startup", cmd_adm_startup, ""},
1994 int netbufferlen = 0;
1997 #if HAVE_GETTIMEOFDAY
1998 struct timeval tv_start, tv_end;
1999 gettimeofday (&tv_start, 0);
2008 char line[1024], word[32], arg[1024];
2014 FD_SET(cs_fileno(conn), &input);
2015 if ((res = select(20, &input, 0, 0, 0)) < 0)
2022 if (!wait && FD_ISSET(0, &input))
2027 #if HAVE_READLINE_READLINE_H
2029 line_in=readline(C_PROMPT);
2032 #if HAVE_READLINE_HISTORY_H
2034 add_history(line_in);
2036 strcpy(line,line_in);
2042 if (!fgets(line, 1023, stdin))
2044 if ((end_p = strchr (line, '\n')))
2047 #if HAVE_GETTIMEOFDAY
2048 gettimeofday (&tv_start, 0);
2051 if ((res = sscanf(line, "%31s %1023[^;]", word, arg)) <= 0)
2053 strcpy(word, last_cmd);
2058 strcpy(last_cmd, word);
2059 for (i = 0; cmd[i].cmd; i++)
2060 if (!strncmp(cmd[i].cmd, word, strlen(word)))
2062 res = (*cmd[i].fun)(arg);
2065 if (!cmd[i].cmd) /* dump our help-screen */
2067 printf("Unknown command: %s.\n", word);
2068 printf("Currently recognized commands:\n");
2069 for (i = 0; cmd[i].cmd; i++)
2070 printf(" %s %s\n", cmd[i].cmd, cmd[i].ad);
2081 && FD_ISSET(cs_fileno(conn), &input)
2087 if ((res = cs_get(conn, &netbuffer, &netbufferlen)) < 0)
2094 printf("Target closed connection.\n");
2097 odr_reset(in); /* release APDU from last round */
2099 odr_setbuf(in, netbuffer, res, 0);
2100 if (!z_APDU(in, &apdu, 0, 0))
2102 odr_perror(in, "Decoding incoming APDU");
2103 fprintf(stderr, "[Near %d]\n", odr_offset(in));
2104 fprintf(stderr, "Packet dump:\n---------\n");
2105 odr_dumpBER(stderr, netbuffer, res);
2106 fprintf(stderr, "---------\n");
2108 z_APDU(print, &apdu, 0, 0);
2111 if (apdu_file && !z_APDU(print, &apdu, 0, 0))
2113 odr_perror(print, "Failed to print incoming APDU");
2119 case Z_APDU_initResponse:
2120 process_initResponse(apdu->u.initResponse);
2122 case Z_APDU_searchResponse:
2123 process_searchResponse(apdu->u.searchResponse);
2125 case Z_APDU_scanResponse:
2126 process_scanResponse(apdu->u.scanResponse);
2128 case Z_APDU_presentResponse:
2129 print_refid (apdu->u.presentResponse->referenceId);
2131 *apdu->u.presentResponse->numberOfRecordsReturned;
2132 if (apdu->u.presentResponse->records)
2133 display_records(apdu->u.presentResponse->records);
2135 printf("No records.\n");
2136 printf ("nextResultSetPosition = %d\n",
2137 *apdu->u.presentResponse->nextResultSetPosition);
2139 case Z_APDU_sortResponse:
2140 process_sortResponse(apdu->u.sortResponse);
2142 case Z_APDU_extendedServicesResponse:
2143 printf("Got extended services response\n");
2144 process_ESResponse(apdu->u.extendedServicesResponse);
2147 printf("Target has closed the association.\n");
2148 process_close(apdu->u.close);
2150 case Z_APDU_resourceControlRequest:
2151 process_resourceControlRequest
2152 (apdu->u.resourceControlRequest);
2154 case Z_APDU_deleteResultSetResponse:
2155 process_deleteResultSetResponse(apdu->u.
2156 deleteResultSetResponse);
2159 printf("Received unknown APDU type (%d).\n",
2164 while (conn && cs_more(conn));
2165 #if HAVE_GETTIMEOFDAY
2166 gettimeofday (&tv_end, 0);
2169 printf ("Elapsed: %.6f\n", (double) tv_end.tv_usec /
2170 1e6 + tv_end.tv_sec -
2171 ((double) tv_start.tv_usec / 1e6 + tv_start.tv_sec));
2179 int main(int argc, char **argv)
2186 while ((ret = options("c:a:m:v:p:", argv, argc, &arg)) != -2)
2194 if (cmd_open (arg) == 2) {
2195 #if HAVE_READLINE_HISTORY_H
2196 char* tmp=(char*)malloc(strlen(arg)+6);
2198 strcat(tmp,"open ");
2208 if (!(marcdump = fopen (arg, "a")))
2215 strncpy (ccl_fields, arg, sizeof(ccl_fields)-1);
2216 ccl_fields[sizeof(ccl_fields)-1] = '\0';
2219 if (!strcmp(arg, "-"))
2222 apdu_file=fopen(arg, "a");
2225 yazProxy=strdup(arg);
2228 yaz_log_init (yaz_log_mask_str(arg), "", NULL);
2231 fprintf (stderr, "Usage: %s [-m <marclog>] [ -a <apdulog>] "
2232 "[-c cclfields] [-p <proxy-addr>] [<server-addr>]\n",
2239 return client (opened);