2 * Copyright (c) 1995-2001, Index Data
3 * See the file LICENSE for details.
5 * $Id: client.c,v 1.129 2001-10-29 09:17:19 adam Exp $
13 #include <yaz/yaz-util.h>
15 #include <yaz/tcpip.h>
17 #include <yaz/xmosi.h>
20 #include <yaz/proto.h>
21 #include <yaz/marcdisp.h>
22 #include <yaz/diagbib1.h>
23 #include <yaz/otherinfo.h>
25 #include <yaz/pquery.h>
26 #include <yaz/sortspec.h>
33 #include <yaz/yaz-ccl.h>
36 #if HAVE_READLINE_READLINE_H
37 #include <readline/readline.h>
39 #if HAVE_READLINE_HISTORY_H
40 #include <readline/history.h>
45 #define C_PROMPT "Z> "
47 static ODR out, in, print; /* encoding and decoding streams */
48 static FILE *apdu_file = 0;
49 static COMSTACK conn = 0; /* our z-association */
50 static Z_IdAuthentication *auth = 0; /* our current auth definition */
51 char *databaseNames[128];
52 int num_databaseNames = 0;
53 static Z_External *record_last = 0;
54 static int setnumber = 0; /* current result set number */
55 static int smallSetUpperBound = 0;
56 static int largeSetLowerBound = 1;
57 static int mediumSetPresentNumber = 0;
58 static Z_ElementSetNames *elementSetNames = 0;
59 static int setno = 1; /* current set offset */
60 static enum oid_proto protocol = PROTO_Z3950; /* current app protocol */
61 static enum oid_value recordsyntax = VAL_USMARC;
62 static enum oid_value schema = VAL_NONE;
63 static int sent_close = 0;
64 static NMEM session_mem = NULL; /* memory handle for init-response */
65 static Z_InitResponse *session = 0; /* session parameters */
66 static char last_scan_line[512] = "0";
67 static char last_scan_query[512] = "0";
68 static char ccl_fields[512] = "default.bib";
69 static char* esPackageName = 0;
70 static char* yazProxy = 0;
72 static char last_cmd[32] = "?";
73 static FILE *marcdump = 0;
74 static char *refid = NULL;
82 static QueryType queryType = QueryType_Prefix;
85 static CCL_bibset bibset; /* CCL bibset handle */
88 ODR getODROutputStream()
93 void send_apdu(Z_APDU *a)
100 z_APDU(print, &a, 0, 0);
103 if (!z_APDU(out, &a, 0, 0))
105 odr_perror(out, "Encoding APDU");
108 buf = odr_getbuf(out, &len, 0);
109 /* printf ("sending APDU of size %d\n", len); */
110 if (cs_put(conn, buf, len) < 0)
112 fprintf(stderr, "cs_put: %s", cs_errmsg(cs_errno(conn)));
115 odr_reset(out); /* release the APDU structure */
118 static void print_refid (Z_ReferenceId *id)
122 printf ("ReferenceId: '%.*s'\n", id->len, id->buf);
126 static Z_ReferenceId *set_refid (ODR out)
131 id = (Z_ReferenceId *) odr_malloc (out, sizeof(*id));
132 id->size = id->len = strlen(refid);
133 id->buf = (unsigned char *) odr_malloc (out, id->len);
134 memcpy (id->buf, refid, id->len);
138 /* INIT SERVICE ------------------------------- */
140 static void send_initRequest(const char* type_and_host)
142 Z_APDU *apdu = zget_APDU(out, Z_APDU_initRequest);
143 Z_InitRequest *req = apdu->u.initRequest;
145 ODR_MASK_SET(req->options, Z_Options_search);
146 ODR_MASK_SET(req->options, Z_Options_present);
147 ODR_MASK_SET(req->options, Z_Options_namedResultSets);
148 ODR_MASK_SET(req->options, Z_Options_triggerResourceCtrl);
149 ODR_MASK_SET(req->options, Z_Options_scan);
150 ODR_MASK_SET(req->options, Z_Options_sort);
151 ODR_MASK_SET(req->options, Z_Options_extendedServices);
152 ODR_MASK_SET(req->options, Z_Options_delSet);
154 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_1);
155 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_2);
156 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_3);
158 *req->maximumRecordSize = 1024*1024;
159 *req->preferredMessageSize = 1024*1024;
161 req->idAuthentication = auth;
163 req->referenceId = set_refid (out);
166 yaz_oi_set_string_oidval(&req->otherInfo, out, VAL_PROXY,
170 printf("Sent initrequest.\n");
173 static int process_initResponse(Z_InitResponse *res)
175 /* save session parameters for later use */
176 session_mem = odr_extract_mem(in);
180 printf("Connection rejected by target.\n");
182 printf("Connection accepted by target.\n");
183 if (res->implementationId)
184 printf("ID : %s\n", res->implementationId);
185 if (res->implementationName)
186 printf("Name : %s\n", res->implementationName);
187 if (res->implementationVersion)
188 printf("Version: %s\n", res->implementationVersion);
189 if (res->userInformationField)
191 printf("UserInformationfield:\n");
192 if (!z_External(print, (Z_External**)&res-> userInformationField,
195 odr_perror(print, "Printing userinfo\n");
198 if (res->userInformationField->which == Z_External_octet)
200 printf("Guessing visiblestring:\n");
201 printf("'%s'\n", res->userInformationField->u. octet_aligned->buf);
206 if (ODR_MASK_GET(res->options, Z_Options_search))
208 if (ODR_MASK_GET(res->options, Z_Options_present))
210 if (ODR_MASK_GET(res->options, Z_Options_delSet))
212 if (ODR_MASK_GET(res->options, Z_Options_resourceReport))
213 printf (" resourceReport");
214 if (ODR_MASK_GET(res->options, Z_Options_resourceCtrl))
215 printf (" resourceCtrl");
216 if (ODR_MASK_GET(res->options, Z_Options_accessCtrl))
217 printf (" accessCtrl");
218 if (ODR_MASK_GET(res->options, Z_Options_scan))
220 if (ODR_MASK_GET(res->options, Z_Options_sort))
222 if (ODR_MASK_GET(res->options, Z_Options_extendedServices))
223 printf (" extendedServices");
224 if (ODR_MASK_GET(res->options, Z_Options_level_1Segmentation))
225 printf (" level1Segmentation");
226 if (ODR_MASK_GET(res->options, Z_Options_level_2Segmentation))
227 printf (" level2Segmentation");
228 if (ODR_MASK_GET(res->options, Z_Options_concurrentOperations))
229 printf (" concurrentOperations");
230 if (ODR_MASK_GET(res->options, Z_Options_namedResultSets))
231 printf (" namedResultSets");
237 static int cmd_base(char *arg)
244 printf("Usage: base <database> <database> ...\n");
247 for (i = 0; i<num_databaseNames; i++)
248 xfree (databaseNames[i]);
249 num_databaseNames = 0;
252 if (!(cp = strchr(arg, ' ')))
253 cp = arg + strlen(arg);
256 databaseNames[num_databaseNames] = (char *)xmalloc (1 + cp - arg);
257 memcpy (databaseNames[num_databaseNames], arg, cp - arg);
258 databaseNames[num_databaseNames++][cp - arg] = '\0';
267 int cmd_open(char *arg)
270 char type_and_host[101], base[101];
275 printf("Already connected.\n");
281 nmem_destroy (session_mem);
287 if (sscanf (arg, "%100[^/]/%100s", type_and_host, base) < 1)
292 conn = cs_create_host(yazProxy, 1, &add);
296 conn = cs_create_host(type_and_host, 1, &add);
301 printf ("Couldn't create comstack\n");
304 printf("Connecting...");
306 if (cs_connect(conn, add) < 0)
308 printf ("error = %s\n", cs_strerror(conn));
309 if (conn->cerrno == CSYSERR)
317 send_initRequest(type_and_host);
323 int cmd_authentication(char *arg)
325 static Z_IdAuthentication au;
326 static char open[256];
330 printf("Auth field set to null\n");
335 au.which = Z_IdAuthentication_open;
341 /* SEARCH SERVICE ------------------------------ */
343 static void display_variant(Z_Variant *v, int level)
347 for (i = 0; i < v->num_triples; i++)
349 printf("%*sclass=%d,type=%d", level * 4, "", *v->triples[i]->zclass,
350 *v->triples[i]->type);
351 if (v->triples[i]->which == Z_Triple_internationalString)
352 printf(",value=%s\n", v->triples[i]->value.internationalString);
358 static void display_grs1(Z_GenericRecord *r, int level)
364 for (i = 0; i < r->num_elements; i++)
368 printf("%*s", level * 4, "");
372 printf("%d,", *t->tagType);
375 if (t->tagValue->which == Z_StringOrNumeric_numeric)
376 printf("%d) ", *t->tagValue->u.numeric);
378 printf("%s) ", t->tagValue->u.string);
379 if (t->content->which == Z_ElementData_subtree)
382 display_grs1(t->content->u.subtree, level+1);
384 else if (t->content->which == Z_ElementData_string)
385 printf("%s\n", t->content->u.string);
386 else if (t->content->which == Z_ElementData_numeric)
387 printf("%d\n", *t->content->u.numeric);
388 else if (t->content->which == Z_ElementData_oid)
390 int *ip = t->content->u.oid;
393 if ((oent = oid_getentbyoid(t->content->u.oid)))
394 printf("OID: %s\n", oent->desc);
398 while (ip && *ip >= 0)
399 printf(" %d", *(ip++));
403 else if (t->content->which == Z_ElementData_noDataRequested)
404 printf("[No data requested]\n");
405 else if (t->content->which == Z_ElementData_elementEmpty)
406 printf("[Element empty]\n");
407 else if (t->content->which == Z_ElementData_elementNotThere)
408 printf("[Element not there]\n");
411 if (t->appliedVariant)
412 display_variant(t->appliedVariant, level+1);
413 if (t->metaData && t->metaData->supportedVariants)
417 printf("%*s---- variant list\n", (level+1)*4, "");
418 for (c = 0; c < t->metaData->num_supportedVariants; c++)
420 printf("%*svariant #%d\n", (level+1)*4, "", c);
421 display_variant(t->metaData->supportedVariants[c], level + 2);
427 static void print_record(const unsigned char *buf, size_t len)
430 for (i = 0; i<len; i++)
431 if ((buf[i] <= 126 && buf[i] >= 32) || strchr ("\n\r\t\f", buf[i]))
432 fputc (buf[i], stdout);
434 printf ("\\X%02X", buf[i]);
435 /* add newline if not already added ... */
436 if (i <= 0 || buf[i-1] != '\n')
437 fputc ('\n', stdout);
440 static void display_record(Z_DatabaseRecord *p)
442 Z_External *r = (Z_External*) p;
443 oident *ent = oid_getentbyoid(r->direct_reference);
447 * Tell the user what we got.
449 if (r->direct_reference)
451 printf("Record type: ");
453 printf("%s\n", ent->desc);
454 else if (!odr_oid(print, &r->direct_reference, 0, 0))
456 odr_perror(print, "print oid");
460 /* Check if this is a known, ASN.1 type tucked away in an octet string */
461 if (ent && r->which == Z_External_octet)
463 Z_ext_typeent *type = z_ext_getentbyref(ent->value);
469 * Call the given decoder to process the record.
471 odr_setbuf(in, (char*)p->u.octet_aligned->buf,
472 p->u.octet_aligned->len, 0);
473 if (!(*type->fun)(in, (char **)&rr, 0, 0))
475 odr_perror(in, "Decoding constructed record.");
476 fprintf(stderr, "[Near %d]\n", odr_offset(in));
477 fprintf(stderr, "Packet dump:\n---------\n");
478 odr_dumpBER(stderr, (char*)p->u.octet_aligned->buf,
479 p->u.octet_aligned->len);
480 fprintf(stderr, "---------\n");
484 * Note: we throw away the original, BER-encoded record here.
485 * Do something else with it if you want to keep it.
487 r->u.sutrs = (Z_SUTRS *) rr; /* we don't actually check the type here. */
488 r->which = type->what;
491 if (ent && ent->value == VAL_SOIF)
492 print_record((const unsigned char *) r->u.octet_aligned->buf,
493 r->u.octet_aligned->len);
494 else if (r->which == Z_External_octet && p->u.octet_aligned->len)
496 const char *octet_buf = (char*)p->u.octet_aligned->buf;
497 if (ent->value == VAL_TEXT_XML || ent->value == VAL_APPLICATION_XML ||
498 ent->value == VAL_HTML)
499 print_record((const unsigned char *) octet_buf,
500 p->u.octet_aligned->len);
503 if (marc_display_exl (octet_buf, NULL, 0 /* debug */,
504 p->u.octet_aligned->len)
507 printf ("ISO2709 decoding failed, dumping record as is:\n");
508 print_record((const unsigned char*) octet_buf,
509 p->u.octet_aligned->len);
513 fwrite (octet_buf, 1, p->u.octet_aligned->len, marcdump);
515 else if (ent && ent->value == VAL_SUTRS)
517 if (r->which != Z_External_sutrs)
519 printf("Expecting single SUTRS type for SUTRS.\n");
522 print_record(r->u.sutrs->buf, r->u.sutrs->len);
524 else if (ent && ent->value == VAL_GRS1)
526 if (r->which != Z_External_grs1)
528 printf("Expecting single GRS type for GRS.\n");
531 display_grs1(r->u.grs1, 0);
535 printf("Unknown record representation.\n");
536 if (!z_External(print, &r, 0, 0))
538 odr_perror(print, "Printing external");
545 static void display_diagrecs(Z_DiagRec **pp, int num)
549 Z_DefaultDiagFormat *r;
551 printf("Diagnostic message(s) from database:\n");
552 for (i = 0; i<num; i++)
554 Z_DiagRec *p = pp[i];
555 if (p->which != Z_DiagRec_defaultFormat)
557 printf("Diagnostic record not in default format.\n");
561 r = p->u.defaultFormat;
562 if (!(ent = oid_getentbyoid(r->diagnosticSetId)) ||
563 ent->oclass != CLASS_DIAGSET || ent->value != VAL_BIB1)
564 printf("Missing or unknown diagset\n");
565 printf(" [%d] %s", *r->condition, diagbib1_str(*r->condition));
569 case Z_DefaultDiagFormat_v2Addinfo:
570 printf (" -- v2 addinfo '%s'\n", r->u.v2Addinfo);
572 case Z_DefaultDiagFormat_v3Addinfo:
573 printf (" -- v3 addinfo '%s'\n", r->u.v3Addinfo);
577 if (r->addinfo && *r->addinfo)
578 printf(" -- '%s'\n", r->addinfo);
586 static void display_nameplusrecord(Z_NamePlusRecord *p)
589 printf("[%s]", p->databaseName);
590 if (p->which == Z_NamePlusRecord_surrogateDiagnostic)
591 display_diagrecs(&p->u.surrogateDiagnostic, 1);
592 else if (p->which == Z_NamePlusRecord_databaseRecord)
593 display_record(p->u.databaseRecord);
596 static void display_records(Z_Records *p)
600 if (p->which == Z_Records_NSD)
603 Z_DiagRec dr, *dr_p = &dr;
604 dr.which = Z_DiagRec_defaultFormat;
605 dr.u.defaultFormat = p->u.nonSurrogateDiagnostic;
606 display_diagrecs (&dr_p, 1);
608 display_diagrecs (&p->u.nonSurrogateDiagnostic, 1);
611 else if (p->which == Z_Records_multipleNSD)
612 display_diagrecs (p->u.multipleNonSurDiagnostics->diagRecs,
613 p->u.multipleNonSurDiagnostics->num_diagRecs);
616 printf("Records: %d\n", p->u.databaseOrSurDiagnostics->num_records);
617 for (i = 0; i < p->u.databaseOrSurDiagnostics->num_records; i++)
618 display_nameplusrecord(p->u.databaseOrSurDiagnostics->records[i]);
622 static int send_deleteResultSetRequest(char *arg)
627 Z_APDU *apdu = zget_APDU(out, Z_APDU_deleteResultSetRequest);
628 Z_DeleteResultSetRequest *req = apdu->u.deleteResultSetRequest;
630 req->referenceId = set_refid (out);
632 req->num_resultSetList =
633 sscanf (arg, "%30s %30s %30s %30s %30s %30s %30s %30s",
634 names[0], names[1], names[2], names[3],
635 names[4], names[5], names[6], names[7]);
637 req->deleteFunction = (int *)
638 odr_malloc (out, sizeof(*req->deleteFunction));
639 if (req->num_resultSetList > 0)
641 *req->deleteFunction = Z_DeleteRequest_list;
642 req->resultSetList = (char **)
643 odr_malloc (out, sizeof(*req->resultSetList)*
644 req->num_resultSetList);
645 for (i = 0; i<req->num_resultSetList; i++)
646 req->resultSetList[i] = names[i];
650 *req->deleteFunction = Z_DeleteRequest_all;
651 req->resultSetList = 0;
655 printf("Sent deleteResultSetRequest.\n");
659 static int send_searchRequest(char *arg)
661 Z_APDU *apdu = zget_APDU(out, Z_APDU_searchRequest);
662 Z_SearchRequest *req = apdu->u.searchRequest;
666 struct ccl_rpn_node *rpn = NULL;
670 Z_RPNQuery *RPNquery;
674 if (queryType == QueryType_CCL2RPN)
676 rpn = ccl_find_str(bibset, arg, &error, &pos);
679 printf("CCL ERROR: %s\n", ccl_err_msg(error));
684 req->referenceId = set_refid (out);
685 if (!strcmp(arg, "@big")) /* strictly for troublemaking */
687 static unsigned char big[2100];
690 /* send a very big referenceid to test transport stack etc. */
691 memset(big, 'A', 2100);
692 bigo.len = bigo.size = 2100;
694 req->referenceId = &bigo;
699 sprintf(setstring, "%d", ++setnumber);
700 req->resultSetName = setstring;
702 *req->smallSetUpperBound = smallSetUpperBound;
703 *req->largeSetLowerBound = largeSetLowerBound;
704 *req->mediumSetPresentNumber = mediumSetPresentNumber;
705 if (smallSetUpperBound > 0 || (largeSetLowerBound > 1 &&
706 mediumSetPresentNumber > 0))
710 prefsyn.proto = protocol;
711 prefsyn.oclass = CLASS_RECSYN;
712 prefsyn.value = recordsyntax;
713 req->preferredRecordSyntax =
714 odr_oiddup(out, oid_ent_to_oid(&prefsyn, oid));
715 req->smallSetElementSetNames =
716 req->mediumSetElementSetNames = elementSetNames;
718 req->num_databaseNames = num_databaseNames;
719 req->databaseNames = databaseNames;
725 case QueryType_Prefix:
726 query.which = Z_Query_type_1;
727 RPNquery = p_query_rpn (out, protocol, arg);
730 printf("Prefix query error\n");
733 query.u.type_1 = RPNquery;
736 query.which = Z_Query_type_2;
737 query.u.type_2 = &ccl_query;
738 ccl_query.buf = (unsigned char*) arg;
739 ccl_query.len = strlen(arg);
742 case QueryType_CCL2RPN:
743 query.which = Z_Query_type_1;
744 RPNquery = ccl_rpn_query(out, rpn);
747 printf ("Couldn't convert from CCL to RPN\n");
750 query.u.type_1 = RPNquery;
751 ccl_rpn_delete (rpn);
755 printf ("Unsupported query type\n");
760 printf("Sent searchRequest.\n");
764 static int process_searchResponse(Z_SearchResponse *res)
766 printf ("Received SearchResponse.\n");
767 print_refid (res->referenceId);
768 if (*res->searchStatus)
769 printf("Search was a success.\n");
771 printf("Search was a bloomin' failure.\n");
772 printf("Number of hits: %d, setno %d\n",
773 *res->resultCount, setnumber);
774 printf("records returned: %d\n",
775 *res->numberOfRecordsReturned);
776 setno += *res->numberOfRecordsReturned;
778 display_records(res->records);
782 static void print_level(int iLevel)
785 for (i = 0; i < iLevel * 4; i++)
789 static void print_int(int iLevel, const char *pTag, int *pInt)
794 printf("%s: %d\n", pTag, *pInt);
798 static void print_string(int iLevel, const char *pTag, const char *pString)
803 printf("%s: %s\n", pTag, pString);
807 static void print_oid(int iLevel, const char *pTag, Odr_oid *pOid)
815 for (; *pInt != -1; pInt++)
816 printf(" %d", *pInt);
821 static void print_referenceId(int iLevel, Z_ReferenceId *referenceId)
823 if (referenceId != NULL)
828 printf("Ref Id (%d, %d): ", referenceId->len, referenceId->size);
829 for (i = 0; i < referenceId->len; i++)
830 printf("%c", referenceId->buf[i]);
835 static void print_string_or_numeric(int iLevel, const char *pTag, Z_StringOrNumeric *pStringNumeric)
837 if (pStringNumeric != NULL)
839 switch (pStringNumeric->which)
841 case Z_StringOrNumeric_string:
842 print_string(iLevel, pTag, pStringNumeric->u.string);
845 case Z_StringOrNumeric_numeric:
846 print_int(iLevel, pTag, pStringNumeric->u.numeric);
851 printf("%s: valid type for Z_StringOrNumeric\n", pTag);
857 static void print_universe_report_duplicate(int iLevel, Z_UniverseReportDuplicate *pUniverseReportDuplicate)
859 if (pUniverseReportDuplicate != NULL)
862 printf("Universe Report Duplicate: \n");
864 print_string_or_numeric(iLevel, "Hit No", pUniverseReportDuplicate->hitno);
868 static void print_universe_report_hits(int iLevel, Z_UniverseReportHits *pUniverseReportHits)
870 if (pUniverseReportHits != NULL)
873 printf("Universe Report Hits: \n");
875 print_string_or_numeric(iLevel, "Database", pUniverseReportHits->database);
876 print_string_or_numeric(iLevel, "Hits", pUniverseReportHits->hits);
880 static void print_universe_report(int iLevel, Z_UniverseReport *pUniverseReport)
882 if (pUniverseReport != NULL)
885 printf("Universe Report: \n");
887 print_int(iLevel, "Total Hits", pUniverseReport->totalHits);
888 switch (pUniverseReport->which)
890 case Z_UniverseReport_databaseHits:
891 print_universe_report_hits(iLevel, pUniverseReport->u.databaseHits);
894 case Z_UniverseReport_duplicate:
895 print_universe_report_duplicate(iLevel, pUniverseReport->u.duplicate);
900 printf("Type: %d\n", pUniverseReport->which);
906 static void print_external(int iLevel, Z_External *pExternal)
908 if (pExternal != NULL)
911 printf("External: \n");
913 print_oid(iLevel, "Direct Reference", pExternal->direct_reference);
914 print_int(iLevel, "InDirect Reference", pExternal->indirect_reference);
915 print_string(iLevel, "Descriptor", pExternal->descriptor);
916 switch (pExternal->which)
918 case Z_External_universeReport:
919 print_universe_report(iLevel, pExternal->u.universeReport);
924 printf("Type: %d\n", pExternal->which);
930 static int process_resourceControlRequest (Z_ResourceControlRequest *req)
932 printf ("Received ResourceControlRequest.\n");
933 print_referenceId(1, req->referenceId);
934 print_int(1, "Suspended Flag", req->suspendedFlag);
935 print_int(1, "Partial Results Available", req->partialResultsAvailable);
936 print_int(1, "Response Required", req->responseRequired);
937 print_int(1, "Triggered Request Flag", req->triggeredRequestFlag);
938 print_external(1, req->resourceReport);
942 void process_ESResponse(Z_ExtendedServicesResponse *res)
944 printf("process_ESResponse status=");
945 switch (*res->operationStatus)
947 case Z_ExtendedServicesResponse_done:
950 case Z_ExtendedServicesResponse_accepted:
951 printf ("accepted\n");
953 case Z_ExtendedServicesResponse_failure:
954 printf ("failure\n");
955 display_diagrecs(res->diagnostics, res->num_diagnostics);
958 if ( (*res->operationStatus != Z_ExtendedServicesResponse_failure) &&
959 (res->num_diagnostics != 0) ) {
960 display_diagrecs(res->diagnostics, res->num_diagnostics);
967 const char *get_ill_element (void *clientData, const char *element)
969 printf ("%s\n", element);
973 static Z_External *create_external_itemRequest()
975 struct ill_get_ctl ctl;
976 ILL_ItemRequest *req;
978 int item_request_size = 0;
979 char *item_request_buf = 0;
983 ctl.f = get_ill_element;
985 req = ill_get_ItemRequest(&ctl, "ill", 0);
987 printf ("ill_get_ItemRequest failed\n");
989 if (!ill_ItemRequest (out, &req, 0, 0))
993 ill_ItemRequest(print, &req, 0, 0);
996 item_request_buf = odr_getbuf (out, &item_request_size, 0);
997 if (item_request_buf)
998 odr_setbuf (out, item_request_buf, item_request_size, 1);
999 printf ("Couldn't encode ItemRequest, size %d\n", item_request_size);
1006 item_request_buf = odr_getbuf (out, &item_request_size, 0);
1007 oid.proto = PROTO_GENERAL;
1008 oid.oclass = CLASS_GENERAL;
1009 oid.value = VAL_ISO_ILL_1;
1011 r = (Z_External *) odr_malloc (out, sizeof(*r));
1012 r->direct_reference = odr_oiddup(out,oid_getoidbyent(&oid));
1013 r->indirect_reference = 0;
1015 r->which = Z_External_single;
1017 r->u.single_ASN1_type = (Odr_oct *)
1018 odr_malloc (out, sizeof(*r->u.single_ASN1_type));
1019 r->u.single_ASN1_type->buf = (unsigned char *)
1020 odr_malloc (out, item_request_size);
1021 r->u.single_ASN1_type->len = item_request_size;
1022 r->u.single_ASN1_type->size = item_request_size;
1023 memcpy (r->u.single_ASN1_type->buf, item_request_buf,
1025 printf ("len = %d\n", item_request_size);
1031 #ifdef YAZ_MODULE_ill
1032 static Z_External *create_external_ILL_APDU(int which)
1034 struct ill_get_ctl ctl;
1037 int ill_request_size = 0;
1038 char *ill_request_buf = 0;
1042 ctl.f = get_ill_element;
1044 ill_apdu = ill_get_APDU(&ctl, "ill", 0);
1046 if (!ill_APDU (out, &ill_apdu, 0, 0))
1050 printf ("-------------------\n");
1051 ill_APDU(print, &ill_apdu, 0, 0);
1053 printf ("-------------------\n");
1055 ill_request_buf = odr_getbuf (out, &ill_request_size, 0);
1056 if (ill_request_buf)
1057 odr_setbuf (out, ill_request_buf, ill_request_size, 1);
1058 printf ("Couldn't encode ILL-Request, size %d\n", ill_request_size);
1064 ill_request_buf = odr_getbuf (out, &ill_request_size, 0);
1066 oid.proto = PROTO_GENERAL;
1067 oid.oclass = CLASS_GENERAL;
1068 oid.value = VAL_ISO_ILL_1;
1070 r = (Z_External *) odr_malloc (out, sizeof(*r));
1071 r->direct_reference = odr_oiddup(out,oid_getoidbyent(&oid));
1072 r->indirect_reference = 0;
1074 r->which = Z_External_single;
1076 r->u.single_ASN1_type = (Odr_oct *)
1077 odr_malloc (out, sizeof(*r->u.single_ASN1_type));
1078 r->u.single_ASN1_type->buf = (unsigned char *)
1079 odr_malloc (out, ill_request_size);
1080 r->u.single_ASN1_type->len = ill_request_size;
1081 r->u.single_ASN1_type->size = ill_request_size;
1082 memcpy (r->u.single_ASN1_type->buf, ill_request_buf, ill_request_size);
1083 printf ("len = %d\n", ill_request_size);
1090 static Z_External *create_ItemOrderExternal(const char *type, int itemno)
1092 Z_External *r = (Z_External *) odr_malloc(out, sizeof(Z_External));
1093 oident ItemOrderRequest;
1095 ItemOrderRequest.proto = PROTO_Z3950;
1096 ItemOrderRequest.oclass = CLASS_EXTSERV;
1097 ItemOrderRequest.value = VAL_ITEMORDER;
1099 r->direct_reference = odr_oiddup(out,oid_getoidbyent(&ItemOrderRequest));
1100 r->indirect_reference = 0;
1103 r->which = Z_External_itemOrder;
1105 r->u.itemOrder = (Z_ItemOrder *) odr_malloc(out,sizeof(Z_ItemOrder));
1106 memset(r->u.itemOrder, 0, sizeof(Z_ItemOrder));
1108 r->u.itemOrder->which=Z_IOItemOrder_esRequest;
1110 r->u.itemOrder->which=Z_ItemOrder_esRequest;
1113 r->u.itemOrder->u.esRequest = (Z_IORequest *)
1114 odr_malloc(out,sizeof(Z_IORequest));
1115 memset(r->u.itemOrder->u.esRequest, 0, sizeof(Z_IORequest));
1117 r->u.itemOrder->u.esRequest->toKeep = (Z_IOOriginPartToKeep *)
1118 odr_malloc(out,sizeof(Z_IOOriginPartToKeep));
1119 memset(r->u.itemOrder->u.esRequest->toKeep, 0, sizeof(Z_IOOriginPartToKeep));
1120 r->u.itemOrder->u.esRequest->notToKeep = (Z_IOOriginPartNotToKeep *)
1121 odr_malloc(out,sizeof(Z_IOOriginPartNotToKeep));
1122 memset(r->u.itemOrder->u.esRequest->notToKeep, 0, sizeof(Z_IOOriginPartNotToKeep));
1124 r->u.itemOrder->u.esRequest->toKeep->supplDescription = NULL;
1125 r->u.itemOrder->u.esRequest->toKeep->contact = NULL;
1126 r->u.itemOrder->u.esRequest->toKeep->addlBilling = NULL;
1128 r->u.itemOrder->u.esRequest->notToKeep->resultSetItem =
1129 (Z_IOResultSetItem *) odr_malloc(out, sizeof(Z_IOResultSetItem));
1130 memset(r->u.itemOrder->u.esRequest->notToKeep->resultSetItem, 0, sizeof(Z_IOResultSetItem));
1131 r->u.itemOrder->u.esRequest->notToKeep->resultSetItem->resultSetId = "1";
1133 r->u.itemOrder->u.esRequest->notToKeep->resultSetItem->item =
1134 (int *) odr_malloc(out, sizeof(int));
1135 *r->u.itemOrder->u.esRequest->notToKeep->resultSetItem->item = itemno;
1138 if (!strcmp (type, "item") || !strcmp(type, "2"))
1140 printf ("using item-request\n");
1141 r->u.itemOrder->u.esRequest->notToKeep->itemRequest =
1142 create_external_itemRequest();
1144 else if (!strcmp(type, "ill") || !strcmp(type, "1"))
1146 printf ("using ILL-request\n");
1147 r->u.itemOrder->u.esRequest->notToKeep->itemRequest =
1148 create_external_ILL_APDU(ILL_APDU_ILL_Request);
1150 else if (!strcmp(type, "xml") || !strcmp(type, "3"))
1152 const char *xml_buf =
1154 " <type>request</type>\n"
1155 " <libraryNo>000200</libraryNo>\n"
1156 " <borrowerTicketNo> 1212 </borrowerTicketNo>\n"
1158 r->u.itemOrder->u.esRequest->notToKeep->itemRequest =
1159 z_ext_record (out, VAL_TEXT_XML, xml_buf, strlen(xml_buf));
1162 r->u.itemOrder->u.esRequest->notToKeep->itemRequest = 0;
1165 r->u.itemOrder->u.esRequest->notToKeep->itemRequest = 0;
1170 static int send_itemorder(const char *type, int itemno)
1172 Z_APDU *apdu = zget_APDU(out, Z_APDU_extendedServicesRequest);
1173 Z_ExtendedServicesRequest *req = apdu->u.extendedServicesRequest;
1174 oident ItemOrderRequest;
1176 ItemOrderRequest.proto = PROTO_Z3950;
1177 ItemOrderRequest.oclass = CLASS_EXTSERV;
1178 ItemOrderRequest.value = VAL_ITEMORDER;
1179 req->packageType = odr_oiddup(out,oid_getoidbyent(&ItemOrderRequest));
1180 req->packageName = esPackageName;
1182 req->taskSpecificParameters = create_ItemOrderExternal(type, itemno);
1188 static int cmd_update(char *arg)
1190 Z_APDU *apdu = zget_APDU(out, Z_APDU_extendedServicesRequest );
1191 Z_ExtendedServicesRequest *req = apdu->u.extendedServicesRequest;
1194 Z_IUOriginPartToKeep *toKeep;
1195 Z_IUSuppliedRecords *notToKeep;
1197 printf ("Update request\n");
1202 update_oid.proto = PROTO_Z3950;
1203 update_oid.oclass = CLASS_EXTSERV;
1204 update_oid.value = VAL_DBUPDATE;
1205 oid_ent_to_oid (&update_oid, oid);
1206 req->packageType = odr_oiddup(out,oid);
1207 req->packageName = esPackageName;
1209 req->referenceId = set_refid (out);
1211 r = req->taskSpecificParameters = (Z_External *)
1212 odr_malloc (out, sizeof(*r));
1213 r->direct_reference = odr_oiddup(out,oid);
1214 r->indirect_reference = 0;
1216 r->which = Z_External_update;
1217 r->u.update = (Z_IUUpdate *) odr_malloc(out, sizeof(*r->u.update));
1218 r->u.update->which = Z_IUUpdate_esRequest;
1219 r->u.update->u.esRequest = (Z_IUUpdateEsRequest *)
1220 odr_malloc(out, sizeof(*r->u.update->u.esRequest));
1221 toKeep = r->u.update->u.esRequest->toKeep = (Z_IUOriginPartToKeep *)
1222 odr_malloc(out, sizeof(*r->u.update->u.esRequest->toKeep));
1223 toKeep->databaseName = databaseNames[0];
1225 toKeep->elementSetName = 0;
1226 toKeep->actionQualifier = 0;
1227 toKeep->action = (int *) odr_malloc(out, sizeof(*toKeep->action));
1228 *toKeep->action = Z_IUOriginPartToKeep_recordInsert;
1230 notToKeep = r->u.update->u.esRequest->notToKeep = (Z_IUSuppliedRecords *)
1231 odr_malloc(out, sizeof(*r->u.update->u.esRequest->notToKeep));
1233 notToKeep->elements = (Z_IUSuppliedRecords_elem **)
1234 odr_malloc(out, sizeof(*notToKeep->elements));
1235 notToKeep->elements[0] = (Z_IUSuppliedRecords_elem *)
1236 odr_malloc(out, sizeof(**notToKeep->elements));
1237 notToKeep->elements[0]->u.number = 0;
1238 notToKeep->elements[0]->supplementalId = 0;
1239 notToKeep->elements[0]->correlationInfo = 0;
1240 notToKeep->elements[0]->record = record_last;
1247 /* II : Added to do DALI Item Order Extended services request */
1248 static int cmd_itemorder(char *arg)
1253 if (sscanf (arg, "%10s %d", type, &itemno) != 2)
1256 printf("Item order request\n");
1258 send_itemorder(type, itemno);
1262 static int cmd_find(char *arg)
1266 printf("Find what?\n");
1271 printf("Not connected yet\n");
1274 if (!send_searchRequest(arg))
1279 static int cmd_delete(char *arg)
1283 printf("Not connected yet\n");
1286 if (!send_deleteResultSetRequest(arg))
1291 static int cmd_ssub(char *arg)
1293 if (!(smallSetUpperBound = atoi(arg)))
1298 static int cmd_lslb(char *arg)
1300 if (!(largeSetLowerBound = atoi(arg)))
1305 static int cmd_mspn(char *arg)
1307 if (!(mediumSetPresentNumber = atoi(arg)))
1312 static int cmd_status(char *arg)
1314 printf("smallSetUpperBound: %d\n", smallSetUpperBound);
1315 printf("largeSetLowerBound: %d\n", largeSetLowerBound);
1316 printf("mediumSetPresentNumber: %d\n", mediumSetPresentNumber);
1320 static int cmd_setnames(char *arg)
1324 printf("Set numbering enabled.\n");
1329 printf("Set numbering disabled.\n");
1335 /* PRESENT SERVICE ----------------------------- */
1337 static int send_presentRequest(char *arg)
1339 Z_APDU *apdu = zget_APDU(out, Z_APDU_presentRequest);
1340 Z_PresentRequest *req = apdu->u.presentRequest;
1341 Z_RecordComposition compo;
1346 char setstring[100];
1348 req->referenceId = set_refid (out);
1349 if ((p = strchr(arg, '+')))
1356 if (p && (p=strchr(p+1, '+')))
1358 strcpy (setstring, p+1);
1359 req->resultSetId = setstring;
1361 else if (setnumber >= 0)
1363 sprintf(setstring, "%d", setnumber);
1364 req->resultSetId = setstring;
1369 static Z_Range range;
1370 static Z_Range *rangep = ⦥
1371 req->num_ranges = 1;
1373 req->resultSetStartPoint = &setno;
1374 req->numberOfRecordsRequested = &nos;
1375 prefsyn.proto = protocol;
1376 prefsyn.oclass = CLASS_RECSYN;
1377 prefsyn.value = recordsyntax;
1378 req->preferredRecordSyntax =
1379 odr_oiddup (out, oid_ent_to_oid(&prefsyn, oid));
1381 if (schema != VAL_NONE)
1385 prefschema.proto = protocol;
1386 prefschema.oclass = CLASS_SCHEMA;
1387 prefschema.value = schema;
1389 req->recordComposition = &compo;
1390 compo.which = Z_RecordComp_complex;
1391 compo.u.complex = (Z_CompSpec *)
1392 odr_malloc(out, sizeof(*compo.u.complex));
1393 compo.u.complex->selectAlternativeSyntax = (bool_t *)
1394 odr_malloc(out, sizeof(bool_t));
1395 *compo.u.complex->selectAlternativeSyntax = 0;
1397 compo.u.complex->generic = (Z_Specification *)
1398 odr_malloc(out, sizeof(*compo.u.complex->generic));
1399 compo.u.complex->generic->schema = (Odr_oid *)
1400 odr_oiddup(out, oid_ent_to_oid(&prefschema, oid));
1401 if (!compo.u.complex->generic->schema)
1403 /* OID wasn't a schema! Try record syntax instead. */
1404 prefschema.oclass = CLASS_RECSYN;
1405 compo.u.complex->generic->schema = (Odr_oid *)
1406 odr_oiddup(out, oid_ent_to_oid(&prefschema, oid));
1408 if (!elementSetNames)
1409 compo.u.complex->generic->elementSpec = 0;
1412 compo.u.complex->generic->elementSpec = (Z_ElementSpec *)
1413 odr_malloc(out, sizeof(Z_ElementSpec));
1414 compo.u.complex->generic->elementSpec->which =
1415 Z_ElementSpec_elementSetName;
1416 compo.u.complex->generic->elementSpec->u.elementSetName =
1417 elementSetNames->u.generic;
1419 compo.u.complex->num_dbSpecific = 0;
1420 compo.u.complex->dbSpecific = 0;
1421 compo.u.complex->num_recordSyntax = 0;
1422 compo.u.complex->recordSyntax = 0;
1424 else if (elementSetNames)
1426 req->recordComposition = &compo;
1427 compo.which = Z_RecordComp_simple;
1428 compo.u.simple = elementSetNames;
1431 printf("Sent presentRequest (%d+%d).\n", setno, nos);
1435 void process_close(Z_Close *req)
1437 Z_APDU *apdu = zget_APDU(out, Z_APDU_close);
1438 Z_Close *res = apdu->u.close;
1440 static char *reasons[] =
1445 "cost limit reached",
1447 "security violation",
1454 printf("Reason: %s, message: %s\n", reasons[*req->closeReason],
1455 req->diagnosticInformation ? req->diagnosticInformation : "NULL");
1462 nmem_destroy (session_mem);
1469 *res->closeReason = Z_Close_finished;
1471 printf("Sent response.\n");
1476 static int cmd_show(char *arg)
1480 printf("Not connected yet\n");
1483 if (!send_presentRequest(arg))
1488 int cmd_quit(char *arg)
1490 printf("See you later, alligator.\n");
1495 int cmd_cancel(char *arg)
1497 Z_APDU *apdu = zget_APDU(out, Z_APDU_triggerResourceControlRequest);
1498 Z_TriggerResourceControlRequest *req =
1499 apdu->u.triggerResourceControlRequest;
1504 printf("Session not initialized yet\n");
1507 if (!ODR_MASK_GET(session->options, Z_Options_triggerResourceCtrl))
1509 printf("Target doesn't support cancel (trigger resource ctrl)\n");
1512 *req->requestedAction = Z_TriggerResourceCtrl_cancel;
1513 req->resultSetWanted = &rfalse;
1516 printf("Sent cancel request\n");
1520 int send_scanrequest(const char *query, int pp, int num, const char *term)
1522 Z_APDU *apdu = zget_APDU(out, Z_APDU_scanRequest);
1523 Z_ScanRequest *req = apdu->u.scanRequest;
1528 if (queryType == QueryType_CCL2RPN)
1532 struct ccl_rpn_node *rpn;
1534 rpn = ccl_find_str (bibset, query, &error, &pos);
1537 printf("CCL ERROR: %s\n", ccl_err_msg(error));
1541 bib1.proto = PROTO_Z3950;
1542 bib1.oclass = CLASS_ATTSET;
1543 bib1.value = VAL_BIB1;
1544 req->attributeSet = oid_ent_to_oid (&bib1, oid);
1545 if (!(req->termListAndStartPoint = ccl_scan_query (out, rpn)))
1547 printf("Couldn't convert CCL to Scan term\n");
1550 ccl_rpn_delete (rpn);
1553 if (use_rpn && !(req->termListAndStartPoint =
1554 p_query_scan(out, protocol, &req->attributeSet, query)))
1556 printf("Prefix query error\n");
1561 if (req->termListAndStartPoint->term &&
1562 req->termListAndStartPoint->term->which == Z_Term_general &&
1563 req->termListAndStartPoint->term->u.general)
1565 req->termListAndStartPoint->term->u.general->buf =
1566 (unsigned char *) odr_strdup(out, term);
1567 req->termListAndStartPoint->term->u.general->len =
1568 req->termListAndStartPoint->term->u.general->size =
1572 req->referenceId = set_refid (out);
1573 req->num_databaseNames = num_databaseNames;
1574 req->databaseNames = databaseNames;
1575 req->numberOfTermsRequested = #
1576 req->preferredPositionInResponse = &pp;
1581 int send_sortrequest(char *arg, int newset)
1583 Z_APDU *apdu = zget_APDU(out, Z_APDU_sortRequest);
1584 Z_SortRequest *req = apdu->u.sortRequest;
1585 Z_SortKeySpecList *sksl = (Z_SortKeySpecList *)
1586 odr_malloc (out, sizeof(*sksl));
1590 sprintf (setstring, "%d", setnumber);
1592 sprintf (setstring, "default");
1594 req->referenceId = set_refid (out);
1597 req->num_inputResultSetNames = 1;
1598 req->inputResultSetNames = (Z_InternationalString **)
1599 odr_malloc (out, sizeof(*req->inputResultSetNames));
1600 req->inputResultSetNames[0] = odr_strdup (out, setstring);
1602 req->inputResultSetNames =
1603 (Z_StringList *)odr_malloc (out, sizeof(*req->inputResultSetNames));
1604 req->inputResultSetNames->num_strings = 1;
1605 req->inputResultSetNames->strings =
1606 (char **)odr_malloc (out, sizeof(*req->inputResultSetNames->strings));
1607 req->inputResultSetNames->strings[0] =
1608 odr_strdup (out, setstring);
1611 if (newset && setnumber >= 0)
1612 sprintf (setstring, "%d", ++setnumber);
1614 req->sortedResultSetName = odr_strdup (out, setstring);
1616 req->sortSequence = yaz_sort_spec (out, arg);
1617 if (!req->sortSequence)
1619 printf ("Missing sort specifications\n");
1626 void display_term(Z_TermInfo *t)
1628 if (t->term->which == Z_Term_general)
1630 printf("%.*s", t->term->u.general->len, t->term->u.general->buf);
1631 sprintf(last_scan_line, "%.*s", t->term->u.general->len,
1632 t->term->u.general->buf);
1635 printf("Term (not general)");
1636 if (t->globalOccurrences)
1637 printf (" (%d)\n", *t->globalOccurrences);
1642 void process_scanResponse(Z_ScanResponse *res)
1645 Z_Entry **entries = NULL;
1646 int num_entries = 0;
1648 printf("Received ScanResponse\n");
1649 print_refid (res->referenceId);
1650 printf("%d entries", *res->numberOfEntriesReturned);
1651 if (res->positionOfTerm)
1652 printf (", position=%d", *res->positionOfTerm);
1654 if (*res->scanStatus != Z_Scan_success)
1655 printf("Scan returned code %d\n", *res->scanStatus);
1658 if ((entries = res->entries->entries))
1659 num_entries = res->entries->num_entries;
1660 for (i = 0; i < num_entries; i++)
1662 int pos_term = res->positionOfTerm ? *res->positionOfTerm : -1;
1663 if (entries[i]->which == Z_Entry_termInfo)
1665 printf("%c ", i + 1 == pos_term ? '*' : ' ');
1666 display_term(entries[i]->u.termInfo);
1669 display_diagrecs(&entries[i]->u.surrogateDiagnostic, 1);
1671 if (res->entries->nonsurrogateDiagnostics)
1672 display_diagrecs (res->entries->nonsurrogateDiagnostics,
1673 res->entries->num_nonsurrogateDiagnostics);
1676 void process_sortResponse(Z_SortResponse *res)
1678 printf("Received SortResponse: status=");
1679 switch (*res->sortStatus)
1681 case Z_SortStatus_success:
1682 printf ("success"); break;
1683 case Z_SortStatus_partial_1:
1684 printf ("partial"); break;
1685 case Z_SortStatus_failure:
1686 printf ("failure"); break;
1688 printf ("unknown (%d)", *res->sortStatus);
1691 print_refid (res->referenceId);
1693 if (res->diagnostics)
1694 display_diagrecs(res->diagnostics,
1695 res->num_diagnostics);
1697 if (res->diagnostics)
1698 display_diagrecs(res->diagnostics->diagRecs,
1699 res->diagnostics->num_diagRecs);
1703 void process_deleteResultSetResponse (Z_DeleteResultSetResponse *res)
1705 printf("Got deleteResultSetResponse status=%d\n",
1706 *res->deleteOperationStatus);
1707 if (res->deleteListStatuses)
1710 for (i = 0; i < res->deleteListStatuses->num; i++)
1712 printf ("%s status=%d\n", res->deleteListStatuses->elements[i]->id,
1713 *res->deleteListStatuses->elements[i]->status);
1718 int cmd_sort_generic(char *arg, int newset)
1722 printf("Session not initialized yet\n");
1725 if (!ODR_MASK_GET(session->options, Z_Options_sort))
1727 printf("Target doesn't support sort\n");
1732 if (send_sortrequest(arg, newset) < 0)
1739 int cmd_sort(char *arg)
1741 return cmd_sort_generic (arg, 0);
1744 int cmd_sort_newset (char *arg)
1746 return cmd_sort_generic (arg, 1);
1749 int cmd_scan(char *arg)
1753 printf("Session not initialized yet\n");
1756 if (!ODR_MASK_GET(session->options, Z_Options_scan))
1758 printf("Target doesn't support scan\n");
1763 strcpy (last_scan_query, arg);
1764 if (send_scanrequest(arg, 1, 20, 0) < 0)
1769 if (send_scanrequest(last_scan_query, 1, 20, last_scan_line) < 0)
1775 int cmd_schema(char *arg)
1782 schema = oid_getvalbyname (arg);
1783 if (schema == VAL_NONE)
1785 printf ("unknown schema\n");
1791 int cmd_format(char *arg)
1795 printf("Usage: format <recordsyntax>\n");
1798 recordsyntax = oid_getvalbyname (arg);
1799 if (recordsyntax == VAL_NONE)
1801 printf ("unknown record syntax\n");
1807 int cmd_elements(char *arg)
1809 static Z_ElementSetNames esn;
1810 static char what[100];
1814 elementSetNames = 0;
1818 esn.which = Z_ElementSetNames_generic;
1819 esn.u.generic = what;
1820 elementSetNames = &esn;
1824 int cmd_attributeset(char *arg)
1830 printf("Usage: attributeset <setname>\n");
1833 sscanf(arg, "%s", what);
1834 if (p_query_attset (what))
1836 printf("Unknown attribute set name\n");
1842 int cmd_querytype (char *arg)
1844 if (!strcmp (arg, "ccl"))
1845 queryType = QueryType_CCL;
1846 else if (!strcmp (arg, "prefix") || !strcmp(arg, "rpn"))
1847 queryType = QueryType_Prefix;
1849 else if (!strcmp (arg, "ccl2rpn") || !strcmp (arg, "cclrpn"))
1850 queryType = QueryType_CCL2RPN;
1854 printf ("Querytype must be one of:\n");
1855 printf (" prefix - Prefix query\n");
1856 printf (" ccl - CCL query\n");
1858 printf (" ccl2rpn - CCL query converted to RPN\n");
1865 int cmd_refid (char *arg)
1871 refid = (char *) xmalloc (strlen(arg)+1);
1872 strcpy (refid, arg);
1877 int cmd_close(char *arg)
1884 apdu = zget_APDU(out, Z_APDU_close);
1885 req = apdu->u.close;
1886 *req->closeReason = Z_Close_finished;
1888 printf("Sent close request.\n");
1893 int cmd_packagename(char* arg) {
1894 xfree (esPackageName);
1895 esPackageName = NULL;
1898 esPackageName = (char *) xmalloc (strlen(arg)+1);
1899 strcpy (esPackageName, arg);
1904 int cmd_proxy(char* arg) {
1909 yazProxy = (char *) xmalloc (strlen(arg)+1);
1910 strcpy (yazProxy, arg);
1915 static void initialize(void)
1920 if (!(out = odr_createmem(ODR_ENCODE)) ||
1921 !(in = odr_createmem(ODR_DECODE)) ||
1922 !(print = odr_createmem(ODR_PRINT)))
1924 fprintf(stderr, "failed to allocate ODR streams\n");
1927 setvbuf(stdout, 0, _IONBF, 0);
1929 odr_setprint(print, apdu_file);
1932 bibset = ccl_qual_mk ();
1933 inf = fopen (ccl_fields, "r");
1936 ccl_qual_file (bibset, inf);
1940 cmd_base("Default");
1943 static int client(int wait)
1947 int (*fun)(char *arg);
1950 {"open", cmd_open, "('tcp'|'osi')':'[<tsel>'/']<host>[':'<port>]"},
1951 {"quit", cmd_quit, ""},
1952 {"find", cmd_find, "<query>"},
1953 {"delete", cmd_delete, "<setname>"},
1954 {"base", cmd_base, "<base-name>"},
1955 {"show", cmd_show, "<rec#>['+'<#recs>['+'<setname>]]"},
1956 {"scan", cmd_scan, "<term>"},
1957 {"sort", cmd_sort, "<sortkey> <flag> <sortkey> <flag> ..."},
1958 {"sort+", cmd_sort_newset, "<sortkey> <flag> <sortkey> <flag> ..."},
1959 {"authentication", cmd_authentication, "<acctstring>"},
1960 {"lslb", cmd_lslb, "<largeSetLowerBound>"},
1961 {"ssub", cmd_ssub, "<smallSetUpperBound>"},
1962 {"mspn", cmd_mspn, "<mediumSetPresentNumber>"},
1963 {"status", cmd_status, ""},
1964 {"setnames", cmd_setnames, ""},
1965 {"cancel", cmd_cancel, ""},
1966 {"format", cmd_format, "<recordsyntax>"},
1967 {"schema", cmd_schema, "<schema>"},
1968 {"elements", cmd_elements, "<elementSetName>"},
1969 {"close", cmd_close, ""},
1970 {"attributeset", cmd_attributeset, "<attrset>"},
1971 {"querytype", cmd_querytype, "<type>"},
1972 {"refid", cmd_refid, "<id>"},
1973 {"itemorder", cmd_itemorder, "ill|item <itemno>"},
1974 {"update", cmd_update, "<item>"},
1975 {"packagename", cmd_packagename, "<packagename>"},
1976 {"proxy", cmd_proxy, "('tcp'|'osi')':'[<tsel>'/']<host>[':'<port>]"},
1978 /* Server Admin Functions */
1979 {"adm-reindex", cmd_adm_reindex, "<database-name>"},
1980 {"adm-truncate", cmd_adm_truncate, "('database'|'index')<object-name>"},
1981 {"adm-create", cmd_adm_create, ""},
1982 {"adm-drop", cmd_adm_drop, "('database'|'index')<object-name>"},
1983 {"adm-import", cmd_adm_import, "<record-type> <dir> <pattern>"},
1984 {"adm-refresh", cmd_adm_refresh, ""},
1985 {"adm-commit", cmd_adm_commit, ""},
1986 {"adm-shutdown", cmd_adm_shutdown, ""},
1987 {"adm-startup", cmd_adm_startup, ""},
1992 int netbufferlen = 0;
1995 #if HAVE_GETTIMEOFDAY
1996 struct timeval tv_start, tv_end;
1997 gettimeofday (&tv_start, 0);
2006 char line[1024], word[32], arg[1024];
2012 FD_SET(cs_fileno(conn), &input);
2013 if ((res = select(20, &input, 0, 0, 0)) < 0)
2020 if (!wait && FD_ISSET(0, &input))
2025 #if HAVE_READLINE_READLINE_H
2027 line_in=readline(C_PROMPT);
2030 #if HAVE_READLINE_HISTORY_H
2032 add_history(line_in);
2034 strcpy(line,line_in);
2040 if (!fgets(line, 1023, stdin))
2042 if ((end_p = strchr (line, '\n')))
2045 #if HAVE_GETTIMEOFDAY
2046 gettimeofday (&tv_start, 0);
2049 if ((res = sscanf(line, "%31s %1023[^;]", word, arg)) <= 0)
2051 strcpy(word, last_cmd);
2056 strcpy(last_cmd, word);
2057 for (i = 0; cmd[i].cmd; i++)
2058 if (!strncmp(cmd[i].cmd, word, strlen(word)))
2060 res = (*cmd[i].fun)(arg);
2063 if (!cmd[i].cmd) /* dump our help-screen */
2065 printf("Unknown command: %s.\n", word);
2066 printf("Currently recognized commands:\n");
2067 for (i = 0; cmd[i].cmd; i++)
2068 printf(" %s %s\n", cmd[i].cmd, cmd[i].ad);
2079 && FD_ISSET(cs_fileno(conn), &input)
2085 if ((res = cs_get(conn, &netbuffer, &netbufferlen)) < 0)
2092 printf("Target closed connection.\n");
2095 odr_reset(in); /* release APDU from last round */
2097 odr_setbuf(in, netbuffer, res, 0);
2098 if (!z_APDU(in, &apdu, 0, 0))
2100 odr_perror(in, "Decoding incoming APDU");
2101 fprintf(stderr, "[Near %d]\n", odr_offset(in));
2102 fprintf(stderr, "Packet dump:\n---------\n");
2103 odr_dumpBER(stderr, netbuffer, res);
2104 fprintf(stderr, "---------\n");
2106 z_APDU(print, &apdu, 0, 0);
2109 if (apdu_file && !z_APDU(print, &apdu, 0, 0))
2111 odr_perror(print, "Failed to print incoming APDU");
2117 case Z_APDU_initResponse:
2118 process_initResponse(apdu->u.initResponse);
2120 case Z_APDU_searchResponse:
2121 process_searchResponse(apdu->u.searchResponse);
2123 case Z_APDU_scanResponse:
2124 process_scanResponse(apdu->u.scanResponse);
2126 case Z_APDU_presentResponse:
2127 print_refid (apdu->u.presentResponse->referenceId);
2129 *apdu->u.presentResponse->numberOfRecordsReturned;
2130 if (apdu->u.presentResponse->records)
2131 display_records(apdu->u.presentResponse->records);
2133 printf("No records.\n");
2134 printf ("nextResultSetPosition = %d\n",
2135 *apdu->u.presentResponse->nextResultSetPosition);
2137 case Z_APDU_sortResponse:
2138 process_sortResponse(apdu->u.sortResponse);
2140 case Z_APDU_extendedServicesResponse:
2141 printf("Got extended services response\n");
2142 process_ESResponse(apdu->u.extendedServicesResponse);
2145 printf("Target has closed the association.\n");
2146 process_close(apdu->u.close);
2148 case Z_APDU_resourceControlRequest:
2149 process_resourceControlRequest
2150 (apdu->u.resourceControlRequest);
2152 case Z_APDU_deleteResultSetResponse:
2153 process_deleteResultSetResponse(apdu->u.
2154 deleteResultSetResponse);
2157 printf("Received unknown APDU type (%d).\n",
2162 while (conn && cs_more(conn));
2163 #if HAVE_GETTIMEOFDAY
2164 gettimeofday (&tv_end, 0);
2167 printf ("Elapsed: %.6f\n", (double) tv_end.tv_usec /
2168 1e6 + tv_end.tv_sec -
2169 ((double) tv_start.tv_usec / 1e6 + tv_start.tv_sec));
2177 int main(int argc, char **argv)
2184 while ((ret = options("c:a:m:v:p:", argv, argc, &arg)) != -2)
2192 if (cmd_open (arg) == 2) {
2193 #if HAVE_READLINE_HISTORY_H
2194 char* tmp=(char*)malloc(strlen(arg)+6);
2196 strcat(tmp,"open ");
2206 if (!(marcdump = fopen (arg, "a")))
2213 strncpy (ccl_fields, arg, sizeof(ccl_fields)-1);
2214 ccl_fields[sizeof(ccl_fields)-1] = '\0';
2217 if (!strcmp(arg, "-"))
2220 apdu_file=fopen(arg, "a");
2223 yazProxy=strdup(arg);
2226 yaz_log_init (yaz_log_mask_str(arg), "", NULL);
2229 fprintf (stderr, "Usage: %s [-m <marclog>] [ -a <apdulog>] "
2230 "[-c cclfields] [-p <proxy-addr>] [<server-addr>]\n",
2237 return client (opened);