2 * Copyright (c) 1995-2001, Index Data
3 * See the file LICENSE for details.
5 * $Id: client.c,v 1.130 2001-11-13 23:00:42 adam Exp $
12 #include <yaz/yaz-util.h>
14 #include <yaz/tcpip.h>
16 #include <yaz/xmosi.h>
19 #include <yaz/proto.h>
20 #include <yaz/marcdisp.h>
21 #include <yaz/diagbib1.h>
22 #include <yaz/otherinfo.h>
24 #include <yaz/pquery.h>
25 #include <yaz/sortspec.h>
32 #include <yaz/yaz-ccl.h>
35 #if HAVE_READLINE_READLINE_H
36 #include <readline/readline.h>
38 #if HAVE_READLINE_HISTORY_H
39 #include <readline/history.h>
44 #define C_PROMPT "Z> "
46 static ODR out, in, print; /* encoding and decoding streams */
47 static FILE *apdu_file = 0;
48 static COMSTACK conn = 0; /* our z-association */
49 static Z_IdAuthentication *auth = 0; /* our current auth definition */
50 char *databaseNames[128];
51 int num_databaseNames = 0;
52 static Z_External *record_last = 0;
53 static int setnumber = 0; /* current result set number */
54 static int smallSetUpperBound = 0;
55 static int largeSetLowerBound = 1;
56 static int mediumSetPresentNumber = 0;
57 static Z_ElementSetNames *elementSetNames = 0;
58 static int setno = 1; /* current set offset */
59 static enum oid_proto protocol = PROTO_Z3950; /* current app protocol */
60 static enum oid_value recordsyntax = VAL_USMARC;
61 static enum oid_value schema = VAL_NONE;
62 static int sent_close = 0;
63 static NMEM session_mem = NULL; /* memory handle for init-response */
64 static Z_InitResponse *session = 0; /* session parameters */
65 static char last_scan_line[512] = "0";
66 static char last_scan_query[512] = "0";
67 static char ccl_fields[512] = "default.bib";
68 static char* esPackageName = 0;
69 static char* yazProxy = 0;
71 static char last_cmd[32] = "?";
72 static FILE *marcdump = 0;
73 static char *refid = NULL;
81 static QueryType queryType = QueryType_Prefix;
84 static CCL_bibset bibset; /* CCL bibset handle */
87 ODR getODROutputStream()
92 void send_apdu(Z_APDU *a)
99 z_APDU(print, &a, 0, 0);
102 if (!z_APDU(out, &a, 0, 0))
104 odr_perror(out, "Encoding APDU");
107 buf = odr_getbuf(out, &len, 0);
108 /* printf ("sending APDU of size %d\n", len); */
109 if (cs_put(conn, buf, len) < 0)
111 fprintf(stderr, "cs_put: %s", cs_errmsg(cs_errno(conn)));
114 odr_reset(out); /* release the APDU structure */
117 static void print_refid (Z_ReferenceId *id)
121 printf ("ReferenceId: '%.*s'\n", id->len, id->buf);
125 static Z_ReferenceId *set_refid (ODR out)
130 id = (Z_ReferenceId *) odr_malloc (out, sizeof(*id));
131 id->size = id->len = strlen(refid);
132 id->buf = (unsigned char *) odr_malloc (out, id->len);
133 memcpy (id->buf, refid, id->len);
137 /* INIT SERVICE ------------------------------- */
139 static void send_initRequest(const char* type_and_host)
141 Z_APDU *apdu = zget_APDU(out, Z_APDU_initRequest);
142 Z_InitRequest *req = apdu->u.initRequest;
144 ODR_MASK_SET(req->options, Z_Options_search);
145 ODR_MASK_SET(req->options, Z_Options_present);
146 ODR_MASK_SET(req->options, Z_Options_namedResultSets);
147 ODR_MASK_SET(req->options, Z_Options_triggerResourceCtrl);
148 ODR_MASK_SET(req->options, Z_Options_scan);
149 ODR_MASK_SET(req->options, Z_Options_sort);
150 ODR_MASK_SET(req->options, Z_Options_extendedServices);
151 ODR_MASK_SET(req->options, Z_Options_delSet);
153 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_1);
154 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_2);
155 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_3);
157 *req->maximumRecordSize = 1024*1024;
158 *req->preferredMessageSize = 1024*1024;
160 req->idAuthentication = auth;
162 req->referenceId = set_refid (out);
165 yaz_oi_set_string_oidval(&req->otherInfo, out, VAL_PROXY,
169 printf("Sent initrequest.\n");
172 static int process_initResponse(Z_InitResponse *res)
174 /* save session parameters for later use */
175 session_mem = odr_extract_mem(in);
179 printf("Connection rejected by target.\n");
181 printf("Connection accepted by target.\n");
182 if (res->implementationId)
183 printf("ID : %s\n", res->implementationId);
184 if (res->implementationName)
185 printf("Name : %s\n", res->implementationName);
186 if (res->implementationVersion)
187 printf("Version: %s\n", res->implementationVersion);
188 if (res->userInformationField)
190 printf("UserInformationfield:\n");
191 if (!z_External(print, (Z_External**)&res-> userInformationField,
194 odr_perror(print, "Printing userinfo\n");
197 if (res->userInformationField->which == Z_External_octet)
199 printf("Guessing visiblestring:\n");
200 printf("'%s'\n", res->userInformationField->u. octet_aligned->buf);
205 if (ODR_MASK_GET(res->options, Z_Options_search))
207 if (ODR_MASK_GET(res->options, Z_Options_present))
209 if (ODR_MASK_GET(res->options, Z_Options_delSet))
211 if (ODR_MASK_GET(res->options, Z_Options_resourceReport))
212 printf (" resourceReport");
213 if (ODR_MASK_GET(res->options, Z_Options_resourceCtrl))
214 printf (" resourceCtrl");
215 if (ODR_MASK_GET(res->options, Z_Options_accessCtrl))
216 printf (" accessCtrl");
217 if (ODR_MASK_GET(res->options, Z_Options_scan))
219 if (ODR_MASK_GET(res->options, Z_Options_sort))
221 if (ODR_MASK_GET(res->options, Z_Options_extendedServices))
222 printf (" extendedServices");
223 if (ODR_MASK_GET(res->options, Z_Options_level_1Segmentation))
224 printf (" level1Segmentation");
225 if (ODR_MASK_GET(res->options, Z_Options_level_2Segmentation))
226 printf (" level2Segmentation");
227 if (ODR_MASK_GET(res->options, Z_Options_concurrentOperations))
228 printf (" concurrentOperations");
229 if (ODR_MASK_GET(res->options, Z_Options_namedResultSets))
230 printf (" namedResultSets");
236 static int cmd_base(char *arg)
243 printf("Usage: base <database> <database> ...\n");
246 for (i = 0; i<num_databaseNames; i++)
247 xfree (databaseNames[i]);
248 num_databaseNames = 0;
251 if (!(cp = strchr(arg, ' ')))
252 cp = arg + strlen(arg);
255 databaseNames[num_databaseNames] = (char *)xmalloc (1 + cp - arg);
256 memcpy (databaseNames[num_databaseNames], arg, cp - arg);
257 databaseNames[num_databaseNames++][cp - arg] = '\0';
266 int cmd_open(char *arg)
269 char type_and_host[101], base[101];
274 printf("Already connected.\n");
280 nmem_destroy (session_mem);
286 if (sscanf (arg, "%100[^/]/%100s", type_and_host, base) < 1)
291 conn = cs_create_host(yazProxy, 1, &add);
295 conn = cs_create_host(type_and_host, 1, &add);
300 printf ("Couldn't create comstack\n");
303 printf("Connecting...");
305 if (cs_connect(conn, add) < 0)
307 printf ("error = %s\n", cs_strerror(conn));
308 if (conn->cerrno == CSYSERR)
316 send_initRequest(type_and_host);
322 int cmd_authentication(char *arg)
324 static Z_IdAuthentication au;
325 static char open[256];
329 printf("Auth field set to null\n");
334 au.which = Z_IdAuthentication_open;
340 /* SEARCH SERVICE ------------------------------ */
342 static void display_variant(Z_Variant *v, int level)
346 for (i = 0; i < v->num_triples; i++)
348 printf("%*sclass=%d,type=%d", level * 4, "", *v->triples[i]->zclass,
349 *v->triples[i]->type);
350 if (v->triples[i]->which == Z_Triple_internationalString)
351 printf(",value=%s\n", v->triples[i]->value.internationalString);
357 static void display_grs1(Z_GenericRecord *r, int level)
363 for (i = 0; i < r->num_elements; i++)
367 printf("%*s", level * 4, "");
371 printf("%d,", *t->tagType);
374 if (t->tagValue->which == Z_StringOrNumeric_numeric)
375 printf("%d) ", *t->tagValue->u.numeric);
377 printf("%s) ", t->tagValue->u.string);
378 if (t->content->which == Z_ElementData_subtree)
381 display_grs1(t->content->u.subtree, level+1);
383 else if (t->content->which == Z_ElementData_string)
384 printf("%s\n", t->content->u.string);
385 else if (t->content->which == Z_ElementData_numeric)
386 printf("%d\n", *t->content->u.numeric);
387 else if (t->content->which == Z_ElementData_oid)
389 int *ip = t->content->u.oid;
392 if ((oent = oid_getentbyoid(t->content->u.oid)))
393 printf("OID: %s\n", oent->desc);
397 while (ip && *ip >= 0)
398 printf(" %d", *(ip++));
402 else if (t->content->which == Z_ElementData_noDataRequested)
403 printf("[No data requested]\n");
404 else if (t->content->which == Z_ElementData_elementEmpty)
405 printf("[Element empty]\n");
406 else if (t->content->which == Z_ElementData_elementNotThere)
407 printf("[Element not there]\n");
410 if (t->appliedVariant)
411 display_variant(t->appliedVariant, level+1);
412 if (t->metaData && t->metaData->supportedVariants)
416 printf("%*s---- variant list\n", (level+1)*4, "");
417 for (c = 0; c < t->metaData->num_supportedVariants; c++)
419 printf("%*svariant #%d\n", (level+1)*4, "", c);
420 display_variant(t->metaData->supportedVariants[c], level + 2);
426 static void print_record(const unsigned char *buf, size_t len)
429 for (i = 0; i<len; i++)
430 if ((buf[i] <= 126 && buf[i] >= 32) || strchr ("\n\r\t\f", buf[i]))
431 fputc (buf[i], stdout);
433 printf ("\\X%02X", buf[i]);
434 /* add newline if not already added ... */
435 if (i <= 0 || buf[i-1] != '\n')
436 fputc ('\n', stdout);
439 static void display_record(Z_DatabaseRecord *p)
441 Z_External *r = (Z_External*) p;
442 oident *ent = oid_getentbyoid(r->direct_reference);
446 * Tell the user what we got.
448 if (r->direct_reference)
450 printf("Record type: ");
452 printf("%s\n", ent->desc);
453 else if (!odr_oid(print, &r->direct_reference, 0, 0))
455 odr_perror(print, "print oid");
459 /* Check if this is a known, ASN.1 type tucked away in an octet string */
460 if (ent && r->which == Z_External_octet)
462 Z_ext_typeent *type = z_ext_getentbyref(ent->value);
468 * Call the given decoder to process the record.
470 odr_setbuf(in, (char*)p->u.octet_aligned->buf,
471 p->u.octet_aligned->len, 0);
472 if (!(*type->fun)(in, (char **)&rr, 0, 0))
474 odr_perror(in, "Decoding constructed record.");
475 fprintf(stderr, "[Near %d]\n", odr_offset(in));
476 fprintf(stderr, "Packet dump:\n---------\n");
477 odr_dumpBER(stderr, (char*)p->u.octet_aligned->buf,
478 p->u.octet_aligned->len);
479 fprintf(stderr, "---------\n");
483 * Note: we throw away the original, BER-encoded record here.
484 * Do something else with it if you want to keep it.
486 r->u.sutrs = (Z_SUTRS *) rr; /* we don't actually check the type here. */
487 r->which = type->what;
490 if (ent && ent->value == VAL_SOIF)
491 print_record((const unsigned char *) r->u.octet_aligned->buf,
492 r->u.octet_aligned->len);
493 else if (r->which == Z_External_octet && p->u.octet_aligned->len)
495 const char *octet_buf = (char*)p->u.octet_aligned->buf;
496 if (ent->value == VAL_TEXT_XML || ent->value == VAL_APPLICATION_XML ||
497 ent->value == VAL_HTML)
498 print_record((const unsigned char *) octet_buf,
499 p->u.octet_aligned->len);
502 if (marc_display_exl (octet_buf, NULL, 0 /* debug */,
503 p->u.octet_aligned->len)
506 printf ("ISO2709 decoding failed, dumping record as is:\n");
507 print_record((const unsigned char*) octet_buf,
508 p->u.octet_aligned->len);
512 fwrite (octet_buf, 1, p->u.octet_aligned->len, marcdump);
514 else if (ent && ent->value == VAL_SUTRS)
516 if (r->which != Z_External_sutrs)
518 printf("Expecting single SUTRS type for SUTRS.\n");
521 print_record(r->u.sutrs->buf, r->u.sutrs->len);
523 else if (ent && ent->value == VAL_GRS1)
525 if (r->which != Z_External_grs1)
527 printf("Expecting single GRS type for GRS.\n");
530 display_grs1(r->u.grs1, 0);
534 printf("Unknown record representation.\n");
535 if (!z_External(print, &r, 0, 0))
537 odr_perror(print, "Printing external");
544 static void display_diagrecs(Z_DiagRec **pp, int num)
548 Z_DefaultDiagFormat *r;
550 printf("Diagnostic message(s) from database:\n");
551 for (i = 0; i<num; i++)
553 Z_DiagRec *p = pp[i];
554 if (p->which != Z_DiagRec_defaultFormat)
556 printf("Diagnostic record not in default format.\n");
560 r = p->u.defaultFormat;
561 if (!(ent = oid_getentbyoid(r->diagnosticSetId)) ||
562 ent->oclass != CLASS_DIAGSET || ent->value != VAL_BIB1)
563 printf("Missing or unknown diagset\n");
564 printf(" [%d] %s", *r->condition, diagbib1_str(*r->condition));
567 case Z_DefaultDiagFormat_v2Addinfo:
568 printf (" -- v2 addinfo '%s'\n", r->u.v2Addinfo);
570 case Z_DefaultDiagFormat_v3Addinfo:
571 printf (" -- v3 addinfo '%s'\n", r->u.v3Addinfo);
578 static void display_nameplusrecord(Z_NamePlusRecord *p)
581 printf("[%s]", p->databaseName);
582 if (p->which == Z_NamePlusRecord_surrogateDiagnostic)
583 display_diagrecs(&p->u.surrogateDiagnostic, 1);
584 else if (p->which == Z_NamePlusRecord_databaseRecord)
585 display_record(p->u.databaseRecord);
588 static void display_records(Z_Records *p)
592 if (p->which == Z_Records_NSD)
594 Z_DiagRec dr, *dr_p = &dr;
595 dr.which = Z_DiagRec_defaultFormat;
596 dr.u.defaultFormat = p->u.nonSurrogateDiagnostic;
597 display_diagrecs (&dr_p, 1);
599 else if (p->which == Z_Records_multipleNSD)
600 display_diagrecs (p->u.multipleNonSurDiagnostics->diagRecs,
601 p->u.multipleNonSurDiagnostics->num_diagRecs);
604 printf("Records: %d\n", p->u.databaseOrSurDiagnostics->num_records);
605 for (i = 0; i < p->u.databaseOrSurDiagnostics->num_records; i++)
606 display_nameplusrecord(p->u.databaseOrSurDiagnostics->records[i]);
610 static int send_deleteResultSetRequest(char *arg)
615 Z_APDU *apdu = zget_APDU(out, Z_APDU_deleteResultSetRequest);
616 Z_DeleteResultSetRequest *req = apdu->u.deleteResultSetRequest;
618 req->referenceId = set_refid (out);
620 req->num_resultSetList =
621 sscanf (arg, "%30s %30s %30s %30s %30s %30s %30s %30s",
622 names[0], names[1], names[2], names[3],
623 names[4], names[5], names[6], names[7]);
625 req->deleteFunction = (int *)
626 odr_malloc (out, sizeof(*req->deleteFunction));
627 if (req->num_resultSetList > 0)
629 *req->deleteFunction = Z_DeleteRequest_list;
630 req->resultSetList = (char **)
631 odr_malloc (out, sizeof(*req->resultSetList)*
632 req->num_resultSetList);
633 for (i = 0; i<req->num_resultSetList; i++)
634 req->resultSetList[i] = names[i];
638 *req->deleteFunction = Z_DeleteRequest_all;
639 req->resultSetList = 0;
643 printf("Sent deleteResultSetRequest.\n");
647 static int send_searchRequest(char *arg)
649 Z_APDU *apdu = zget_APDU(out, Z_APDU_searchRequest);
650 Z_SearchRequest *req = apdu->u.searchRequest;
654 struct ccl_rpn_node *rpn = NULL;
658 Z_RPNQuery *RPNquery;
662 if (queryType == QueryType_CCL2RPN)
664 rpn = ccl_find_str(bibset, arg, &error, &pos);
667 printf("CCL ERROR: %s\n", ccl_err_msg(error));
672 req->referenceId = set_refid (out);
673 if (!strcmp(arg, "@big")) /* strictly for troublemaking */
675 static unsigned char big[2100];
678 /* send a very big referenceid to test transport stack etc. */
679 memset(big, 'A', 2100);
680 bigo.len = bigo.size = 2100;
682 req->referenceId = &bigo;
687 sprintf(setstring, "%d", ++setnumber);
688 req->resultSetName = setstring;
690 *req->smallSetUpperBound = smallSetUpperBound;
691 *req->largeSetLowerBound = largeSetLowerBound;
692 *req->mediumSetPresentNumber = mediumSetPresentNumber;
693 if (smallSetUpperBound > 0 || (largeSetLowerBound > 1 &&
694 mediumSetPresentNumber > 0))
698 prefsyn.proto = protocol;
699 prefsyn.oclass = CLASS_RECSYN;
700 prefsyn.value = recordsyntax;
701 req->preferredRecordSyntax =
702 odr_oiddup(out, oid_ent_to_oid(&prefsyn, oid));
703 req->smallSetElementSetNames =
704 req->mediumSetElementSetNames = elementSetNames;
706 req->num_databaseNames = num_databaseNames;
707 req->databaseNames = databaseNames;
713 case QueryType_Prefix:
714 query.which = Z_Query_type_1;
715 RPNquery = p_query_rpn (out, protocol, arg);
718 printf("Prefix query error\n");
721 query.u.type_1 = RPNquery;
724 query.which = Z_Query_type_2;
725 query.u.type_2 = &ccl_query;
726 ccl_query.buf = (unsigned char*) arg;
727 ccl_query.len = strlen(arg);
730 case QueryType_CCL2RPN:
731 query.which = Z_Query_type_1;
732 RPNquery = ccl_rpn_query(out, rpn);
735 printf ("Couldn't convert from CCL to RPN\n");
738 query.u.type_1 = RPNquery;
739 ccl_rpn_delete (rpn);
743 printf ("Unsupported query type\n");
748 printf("Sent searchRequest.\n");
752 static int process_searchResponse(Z_SearchResponse *res)
754 printf ("Received SearchResponse.\n");
755 print_refid (res->referenceId);
756 if (*res->searchStatus)
757 printf("Search was a success.\n");
759 printf("Search was a bloomin' failure.\n");
760 printf("Number of hits: %d, setno %d\n",
761 *res->resultCount, setnumber);
762 printf("records returned: %d\n",
763 *res->numberOfRecordsReturned);
764 setno += *res->numberOfRecordsReturned;
766 display_records(res->records);
770 static void print_level(int iLevel)
773 for (i = 0; i < iLevel * 4; i++)
777 static void print_int(int iLevel, const char *pTag, int *pInt)
782 printf("%s: %d\n", pTag, *pInt);
786 static void print_string(int iLevel, const char *pTag, const char *pString)
791 printf("%s: %s\n", pTag, pString);
795 static void print_oid(int iLevel, const char *pTag, Odr_oid *pOid)
803 for (; *pInt != -1; pInt++)
804 printf(" %d", *pInt);
809 static void print_referenceId(int iLevel, Z_ReferenceId *referenceId)
811 if (referenceId != NULL)
816 printf("Ref Id (%d, %d): ", referenceId->len, referenceId->size);
817 for (i = 0; i < referenceId->len; i++)
818 printf("%c", referenceId->buf[i]);
823 static void print_string_or_numeric(int iLevel, const char *pTag, Z_StringOrNumeric *pStringNumeric)
825 if (pStringNumeric != NULL)
827 switch (pStringNumeric->which)
829 case Z_StringOrNumeric_string:
830 print_string(iLevel, pTag, pStringNumeric->u.string);
833 case Z_StringOrNumeric_numeric:
834 print_int(iLevel, pTag, pStringNumeric->u.numeric);
839 printf("%s: valid type for Z_StringOrNumeric\n", pTag);
845 static void print_universe_report_duplicate(int iLevel, Z_UniverseReportDuplicate *pUniverseReportDuplicate)
847 if (pUniverseReportDuplicate != NULL)
850 printf("Universe Report Duplicate: \n");
852 print_string_or_numeric(iLevel, "Hit No", pUniverseReportDuplicate->hitno);
856 static void print_universe_report_hits(int iLevel, Z_UniverseReportHits *pUniverseReportHits)
858 if (pUniverseReportHits != NULL)
861 printf("Universe Report Hits: \n");
863 print_string_or_numeric(iLevel, "Database", pUniverseReportHits->database);
864 print_string_or_numeric(iLevel, "Hits", pUniverseReportHits->hits);
868 static void print_universe_report(int iLevel, Z_UniverseReport *pUniverseReport)
870 if (pUniverseReport != NULL)
873 printf("Universe Report: \n");
875 print_int(iLevel, "Total Hits", pUniverseReport->totalHits);
876 switch (pUniverseReport->which)
878 case Z_UniverseReport_databaseHits:
879 print_universe_report_hits(iLevel, pUniverseReport->u.databaseHits);
882 case Z_UniverseReport_duplicate:
883 print_universe_report_duplicate(iLevel, pUniverseReport->u.duplicate);
888 printf("Type: %d\n", pUniverseReport->which);
894 static void print_external(int iLevel, Z_External *pExternal)
896 if (pExternal != NULL)
899 printf("External: \n");
901 print_oid(iLevel, "Direct Reference", pExternal->direct_reference);
902 print_int(iLevel, "InDirect Reference", pExternal->indirect_reference);
903 print_string(iLevel, "Descriptor", pExternal->descriptor);
904 switch (pExternal->which)
906 case Z_External_universeReport:
907 print_universe_report(iLevel, pExternal->u.universeReport);
912 printf("Type: %d\n", pExternal->which);
918 static int process_resourceControlRequest (Z_ResourceControlRequest *req)
920 printf ("Received ResourceControlRequest.\n");
921 print_referenceId(1, req->referenceId);
922 print_int(1, "Suspended Flag", req->suspendedFlag);
923 print_int(1, "Partial Results Available", req->partialResultsAvailable);
924 print_int(1, "Response Required", req->responseRequired);
925 print_int(1, "Triggered Request Flag", req->triggeredRequestFlag);
926 print_external(1, req->resourceReport);
930 void process_ESResponse(Z_ExtendedServicesResponse *res)
932 printf("process_ESResponse status=");
933 switch (*res->operationStatus)
935 case Z_ExtendedServicesResponse_done:
938 case Z_ExtendedServicesResponse_accepted:
939 printf ("accepted\n");
941 case Z_ExtendedServicesResponse_failure:
942 printf ("failure\n");
943 display_diagrecs(res->diagnostics, res->num_diagnostics);
946 if ( (*res->operationStatus != Z_ExtendedServicesResponse_failure) &&
947 (res->num_diagnostics != 0) ) {
948 display_diagrecs(res->diagnostics, res->num_diagnostics);
955 const char *get_ill_element (void *clientData, const char *element)
957 printf ("%s\n", element);
961 static Z_External *create_external_itemRequest()
963 struct ill_get_ctl ctl;
964 ILL_ItemRequest *req;
966 int item_request_size = 0;
967 char *item_request_buf = 0;
971 ctl.f = get_ill_element;
973 req = ill_get_ItemRequest(&ctl, "ill", 0);
975 printf ("ill_get_ItemRequest failed\n");
977 if (!ill_ItemRequest (out, &req, 0, 0))
981 ill_ItemRequest(print, &req, 0, 0);
984 item_request_buf = odr_getbuf (out, &item_request_size, 0);
985 if (item_request_buf)
986 odr_setbuf (out, item_request_buf, item_request_size, 1);
987 printf ("Couldn't encode ItemRequest, size %d\n", item_request_size);
994 item_request_buf = odr_getbuf (out, &item_request_size, 0);
995 oid.proto = PROTO_GENERAL;
996 oid.oclass = CLASS_GENERAL;
997 oid.value = VAL_ISO_ILL_1;
999 r = (Z_External *) odr_malloc (out, sizeof(*r));
1000 r->direct_reference = odr_oiddup(out,oid_getoidbyent(&oid));
1001 r->indirect_reference = 0;
1003 r->which = Z_External_single;
1005 r->u.single_ASN1_type = (Odr_oct *)
1006 odr_malloc (out, sizeof(*r->u.single_ASN1_type));
1007 r->u.single_ASN1_type->buf = (unsigned char *)
1008 odr_malloc (out, item_request_size);
1009 r->u.single_ASN1_type->len = item_request_size;
1010 r->u.single_ASN1_type->size = item_request_size;
1011 memcpy (r->u.single_ASN1_type->buf, item_request_buf,
1013 printf ("len = %d\n", item_request_size);
1019 #ifdef YAZ_MODULE_ill
1020 static Z_External *create_external_ILL_APDU(int which)
1022 struct ill_get_ctl ctl;
1025 int ill_request_size = 0;
1026 char *ill_request_buf = 0;
1030 ctl.f = get_ill_element;
1032 ill_apdu = ill_get_APDU(&ctl, "ill", 0);
1034 if (!ill_APDU (out, &ill_apdu, 0, 0))
1038 printf ("-------------------\n");
1039 ill_APDU(print, &ill_apdu, 0, 0);
1041 printf ("-------------------\n");
1043 ill_request_buf = odr_getbuf (out, &ill_request_size, 0);
1044 if (ill_request_buf)
1045 odr_setbuf (out, ill_request_buf, ill_request_size, 1);
1046 printf ("Couldn't encode ILL-Request, size %d\n", ill_request_size);
1052 ill_request_buf = odr_getbuf (out, &ill_request_size, 0);
1054 oid.proto = PROTO_GENERAL;
1055 oid.oclass = CLASS_GENERAL;
1056 oid.value = VAL_ISO_ILL_1;
1058 r = (Z_External *) odr_malloc (out, sizeof(*r));
1059 r->direct_reference = odr_oiddup(out,oid_getoidbyent(&oid));
1060 r->indirect_reference = 0;
1062 r->which = Z_External_single;
1064 r->u.single_ASN1_type = (Odr_oct *)
1065 odr_malloc (out, sizeof(*r->u.single_ASN1_type));
1066 r->u.single_ASN1_type->buf = (unsigned char *)
1067 odr_malloc (out, ill_request_size);
1068 r->u.single_ASN1_type->len = ill_request_size;
1069 r->u.single_ASN1_type->size = ill_request_size;
1070 memcpy (r->u.single_ASN1_type->buf, ill_request_buf, ill_request_size);
1071 printf ("len = %d\n", ill_request_size);
1078 static Z_External *create_ItemOrderExternal(const char *type, int itemno)
1080 Z_External *r = (Z_External *) odr_malloc(out, sizeof(Z_External));
1081 oident ItemOrderRequest;
1083 ItemOrderRequest.proto = PROTO_Z3950;
1084 ItemOrderRequest.oclass = CLASS_EXTSERV;
1085 ItemOrderRequest.value = VAL_ITEMORDER;
1087 r->direct_reference = odr_oiddup(out,oid_getoidbyent(&ItemOrderRequest));
1088 r->indirect_reference = 0;
1091 r->which = Z_External_itemOrder;
1093 r->u.itemOrder = (Z_ItemOrder *) odr_malloc(out,sizeof(Z_ItemOrder));
1094 memset(r->u.itemOrder, 0, sizeof(Z_ItemOrder));
1095 r->u.itemOrder->which=Z_IOItemOrder_esRequest;
1097 r->u.itemOrder->u.esRequest = (Z_IORequest *)
1098 odr_malloc(out,sizeof(Z_IORequest));
1099 memset(r->u.itemOrder->u.esRequest, 0, sizeof(Z_IORequest));
1101 r->u.itemOrder->u.esRequest->toKeep = (Z_IOOriginPartToKeep *)
1102 odr_malloc(out,sizeof(Z_IOOriginPartToKeep));
1103 memset(r->u.itemOrder->u.esRequest->toKeep, 0, sizeof(Z_IOOriginPartToKeep));
1104 r->u.itemOrder->u.esRequest->notToKeep = (Z_IOOriginPartNotToKeep *)
1105 odr_malloc(out,sizeof(Z_IOOriginPartNotToKeep));
1106 memset(r->u.itemOrder->u.esRequest->notToKeep, 0, sizeof(Z_IOOriginPartNotToKeep));
1108 r->u.itemOrder->u.esRequest->toKeep->supplDescription = NULL;
1109 r->u.itemOrder->u.esRequest->toKeep->contact = NULL;
1110 r->u.itemOrder->u.esRequest->toKeep->addlBilling = NULL;
1112 r->u.itemOrder->u.esRequest->notToKeep->resultSetItem =
1113 (Z_IOResultSetItem *) odr_malloc(out, sizeof(Z_IOResultSetItem));
1114 memset(r->u.itemOrder->u.esRequest->notToKeep->resultSetItem, 0, sizeof(Z_IOResultSetItem));
1115 r->u.itemOrder->u.esRequest->notToKeep->resultSetItem->resultSetId = "1";
1117 r->u.itemOrder->u.esRequest->notToKeep->resultSetItem->item =
1118 (int *) odr_malloc(out, sizeof(int));
1119 *r->u.itemOrder->u.esRequest->notToKeep->resultSetItem->item = itemno;
1122 if (!strcmp (type, "item") || !strcmp(type, "2"))
1124 printf ("using item-request\n");
1125 r->u.itemOrder->u.esRequest->notToKeep->itemRequest =
1126 create_external_itemRequest();
1128 else if (!strcmp(type, "ill") || !strcmp(type, "1"))
1130 printf ("using ILL-request\n");
1131 r->u.itemOrder->u.esRequest->notToKeep->itemRequest =
1132 create_external_ILL_APDU(ILL_APDU_ILL_Request);
1134 else if (!strcmp(type, "xml") || !strcmp(type, "3"))
1136 const char *xml_buf =
1138 " <type>request</type>\n"
1139 " <libraryNo>000200</libraryNo>\n"
1140 " <borrowerTicketNo> 1212 </borrowerTicketNo>\n"
1142 r->u.itemOrder->u.esRequest->notToKeep->itemRequest =
1143 z_ext_record (out, VAL_TEXT_XML, xml_buf, strlen(xml_buf));
1146 r->u.itemOrder->u.esRequest->notToKeep->itemRequest = 0;
1149 r->u.itemOrder->u.esRequest->notToKeep->itemRequest = 0;
1154 static int send_itemorder(const char *type, int itemno)
1156 Z_APDU *apdu = zget_APDU(out, Z_APDU_extendedServicesRequest);
1157 Z_ExtendedServicesRequest *req = apdu->u.extendedServicesRequest;
1158 oident ItemOrderRequest;
1160 ItemOrderRequest.proto = PROTO_Z3950;
1161 ItemOrderRequest.oclass = CLASS_EXTSERV;
1162 ItemOrderRequest.value = VAL_ITEMORDER;
1163 req->packageType = odr_oiddup(out,oid_getoidbyent(&ItemOrderRequest));
1164 req->packageName = esPackageName;
1166 req->taskSpecificParameters = create_ItemOrderExternal(type, itemno);
1172 static int cmd_update(char *arg)
1174 Z_APDU *apdu = zget_APDU(out, Z_APDU_extendedServicesRequest );
1175 Z_ExtendedServicesRequest *req = apdu->u.extendedServicesRequest;
1178 Z_IUOriginPartToKeep *toKeep;
1179 Z_IUSuppliedRecords *notToKeep;
1181 printf ("Update request\n");
1186 update_oid.proto = PROTO_Z3950;
1187 update_oid.oclass = CLASS_EXTSERV;
1188 update_oid.value = VAL_DBUPDATE;
1189 oid_ent_to_oid (&update_oid, oid);
1190 req->packageType = odr_oiddup(out,oid);
1191 req->packageName = esPackageName;
1193 req->referenceId = set_refid (out);
1195 r = req->taskSpecificParameters = (Z_External *)
1196 odr_malloc (out, sizeof(*r));
1197 r->direct_reference = odr_oiddup(out,oid);
1198 r->indirect_reference = 0;
1200 r->which = Z_External_update;
1201 r->u.update = (Z_IUUpdate *) odr_malloc(out, sizeof(*r->u.update));
1202 r->u.update->which = Z_IUUpdate_esRequest;
1203 r->u.update->u.esRequest = (Z_IUUpdateEsRequest *)
1204 odr_malloc(out, sizeof(*r->u.update->u.esRequest));
1205 toKeep = r->u.update->u.esRequest->toKeep = (Z_IUOriginPartToKeep *)
1206 odr_malloc(out, sizeof(*r->u.update->u.esRequest->toKeep));
1207 toKeep->databaseName = databaseNames[0];
1209 toKeep->elementSetName = 0;
1210 toKeep->actionQualifier = 0;
1211 toKeep->action = (int *) odr_malloc(out, sizeof(*toKeep->action));
1212 *toKeep->action = Z_IUOriginPartToKeep_recordInsert;
1214 notToKeep = r->u.update->u.esRequest->notToKeep = (Z_IUSuppliedRecords *)
1215 odr_malloc(out, sizeof(*r->u.update->u.esRequest->notToKeep));
1217 notToKeep->elements = (Z_IUSuppliedRecords_elem **)
1218 odr_malloc(out, sizeof(*notToKeep->elements));
1219 notToKeep->elements[0] = (Z_IUSuppliedRecords_elem *)
1220 odr_malloc(out, sizeof(**notToKeep->elements));
1221 notToKeep->elements[0]->u.number = 0;
1222 notToKeep->elements[0]->supplementalId = 0;
1223 notToKeep->elements[0]->correlationInfo = 0;
1224 notToKeep->elements[0]->record = record_last;
1231 /* II : Added to do DALI Item Order Extended services request */
1232 static int cmd_itemorder(char *arg)
1237 if (sscanf (arg, "%10s %d", type, &itemno) != 2)
1240 printf("Item order request\n");
1242 send_itemorder(type, itemno);
1246 static int cmd_find(char *arg)
1250 printf("Find what?\n");
1255 printf("Not connected yet\n");
1258 if (!send_searchRequest(arg))
1263 static int cmd_delete(char *arg)
1267 printf("Not connected yet\n");
1270 if (!send_deleteResultSetRequest(arg))
1275 static int cmd_ssub(char *arg)
1277 if (!(smallSetUpperBound = atoi(arg)))
1282 static int cmd_lslb(char *arg)
1284 if (!(largeSetLowerBound = atoi(arg)))
1289 static int cmd_mspn(char *arg)
1291 if (!(mediumSetPresentNumber = atoi(arg)))
1296 static int cmd_status(char *arg)
1298 printf("smallSetUpperBound: %d\n", smallSetUpperBound);
1299 printf("largeSetLowerBound: %d\n", largeSetLowerBound);
1300 printf("mediumSetPresentNumber: %d\n", mediumSetPresentNumber);
1304 static int cmd_setnames(char *arg)
1308 printf("Set numbering enabled.\n");
1313 printf("Set numbering disabled.\n");
1319 /* PRESENT SERVICE ----------------------------- */
1321 static int send_presentRequest(char *arg)
1323 Z_APDU *apdu = zget_APDU(out, Z_APDU_presentRequest);
1324 Z_PresentRequest *req = apdu->u.presentRequest;
1325 Z_RecordComposition compo;
1330 char setstring[100];
1332 req->referenceId = set_refid (out);
1333 if ((p = strchr(arg, '+')))
1340 if (p && (p=strchr(p+1, '+')))
1342 strcpy (setstring, p+1);
1343 req->resultSetId = setstring;
1345 else if (setnumber >= 0)
1347 sprintf(setstring, "%d", setnumber);
1348 req->resultSetId = setstring;
1353 static Z_Range range;
1354 static Z_Range *rangep = ⦥
1355 req->num_ranges = 1;
1357 req->resultSetStartPoint = &setno;
1358 req->numberOfRecordsRequested = &nos;
1359 prefsyn.proto = protocol;
1360 prefsyn.oclass = CLASS_RECSYN;
1361 prefsyn.value = recordsyntax;
1362 req->preferredRecordSyntax =
1363 odr_oiddup (out, oid_ent_to_oid(&prefsyn, oid));
1365 if (schema != VAL_NONE)
1369 prefschema.proto = protocol;
1370 prefschema.oclass = CLASS_SCHEMA;
1371 prefschema.value = schema;
1373 req->recordComposition = &compo;
1374 compo.which = Z_RecordComp_complex;
1375 compo.u.complex = (Z_CompSpec *)
1376 odr_malloc(out, sizeof(*compo.u.complex));
1377 compo.u.complex->selectAlternativeSyntax = (bool_t *)
1378 odr_malloc(out, sizeof(bool_t));
1379 *compo.u.complex->selectAlternativeSyntax = 0;
1381 compo.u.complex->generic = (Z_Specification *)
1382 odr_malloc(out, sizeof(*compo.u.complex->generic));
1383 compo.u.complex->generic->schema = (Odr_oid *)
1384 odr_oiddup(out, oid_ent_to_oid(&prefschema, oid));
1385 if (!compo.u.complex->generic->schema)
1387 /* OID wasn't a schema! Try record syntax instead. */
1388 prefschema.oclass = CLASS_RECSYN;
1389 compo.u.complex->generic->schema = (Odr_oid *)
1390 odr_oiddup(out, oid_ent_to_oid(&prefschema, oid));
1392 if (!elementSetNames)
1393 compo.u.complex->generic->elementSpec = 0;
1396 compo.u.complex->generic->elementSpec = (Z_ElementSpec *)
1397 odr_malloc(out, sizeof(Z_ElementSpec));
1398 compo.u.complex->generic->elementSpec->which =
1399 Z_ElementSpec_elementSetName;
1400 compo.u.complex->generic->elementSpec->u.elementSetName =
1401 elementSetNames->u.generic;
1403 compo.u.complex->num_dbSpecific = 0;
1404 compo.u.complex->dbSpecific = 0;
1405 compo.u.complex->num_recordSyntax = 0;
1406 compo.u.complex->recordSyntax = 0;
1408 else if (elementSetNames)
1410 req->recordComposition = &compo;
1411 compo.which = Z_RecordComp_simple;
1412 compo.u.simple = elementSetNames;
1415 printf("Sent presentRequest (%d+%d).\n", setno, nos);
1419 void process_close(Z_Close *req)
1421 Z_APDU *apdu = zget_APDU(out, Z_APDU_close);
1422 Z_Close *res = apdu->u.close;
1424 static char *reasons[] =
1429 "cost limit reached",
1431 "security violation",
1438 printf("Reason: %s, message: %s\n", reasons[*req->closeReason],
1439 req->diagnosticInformation ? req->diagnosticInformation : "NULL");
1446 nmem_destroy (session_mem);
1453 *res->closeReason = Z_Close_finished;
1455 printf("Sent response.\n");
1460 static int cmd_show(char *arg)
1464 printf("Not connected yet\n");
1467 if (!send_presentRequest(arg))
1472 int cmd_quit(char *arg)
1474 printf("See you later, alligator.\n");
1480 int cmd_cancel(char *arg)
1482 Z_APDU *apdu = zget_APDU(out, Z_APDU_triggerResourceControlRequest);
1483 Z_TriggerResourceControlRequest *req =
1484 apdu->u.triggerResourceControlRequest;
1489 printf("Session not initialized yet\n");
1492 if (!ODR_MASK_GET(session->options, Z_Options_triggerResourceCtrl))
1494 printf("Target doesn't support cancel (trigger resource ctrl)\n");
1497 *req->requestedAction = Z_TriggerResourceCtrl_cancel;
1498 req->resultSetWanted = &rfalse;
1501 printf("Sent cancel request\n");
1505 int send_scanrequest(const char *query, int pp, int num, const char *term)
1507 Z_APDU *apdu = zget_APDU(out, Z_APDU_scanRequest);
1508 Z_ScanRequest *req = apdu->u.scanRequest;
1513 if (queryType == QueryType_CCL2RPN)
1517 struct ccl_rpn_node *rpn;
1519 rpn = ccl_find_str (bibset, query, &error, &pos);
1522 printf("CCL ERROR: %s\n", ccl_err_msg(error));
1526 bib1.proto = PROTO_Z3950;
1527 bib1.oclass = CLASS_ATTSET;
1528 bib1.value = VAL_BIB1;
1529 req->attributeSet = oid_ent_to_oid (&bib1, oid);
1530 if (!(req->termListAndStartPoint = ccl_scan_query (out, rpn)))
1532 printf("Couldn't convert CCL to Scan term\n");
1535 ccl_rpn_delete (rpn);
1538 if (use_rpn && !(req->termListAndStartPoint =
1539 p_query_scan(out, protocol, &req->attributeSet, query)))
1541 printf("Prefix query error\n");
1546 if (req->termListAndStartPoint->term &&
1547 req->termListAndStartPoint->term->which == Z_Term_general &&
1548 req->termListAndStartPoint->term->u.general)
1550 req->termListAndStartPoint->term->u.general->buf =
1551 (unsigned char *) odr_strdup(out, term);
1552 req->termListAndStartPoint->term->u.general->len =
1553 req->termListAndStartPoint->term->u.general->size =
1557 req->referenceId = set_refid (out);
1558 req->num_databaseNames = num_databaseNames;
1559 req->databaseNames = databaseNames;
1560 req->numberOfTermsRequested = #
1561 req->preferredPositionInResponse = &pp;
1566 int send_sortrequest(char *arg, int newset)
1568 Z_APDU *apdu = zget_APDU(out, Z_APDU_sortRequest);
1569 Z_SortRequest *req = apdu->u.sortRequest;
1570 Z_SortKeySpecList *sksl = (Z_SortKeySpecList *)
1571 odr_malloc (out, sizeof(*sksl));
1575 sprintf (setstring, "%d", setnumber);
1577 sprintf (setstring, "default");
1579 req->referenceId = set_refid (out);
1581 req->num_inputResultSetNames = 1;
1582 req->inputResultSetNames = (Z_InternationalString **)
1583 odr_malloc (out, sizeof(*req->inputResultSetNames));
1584 req->inputResultSetNames[0] = odr_strdup (out, setstring);
1586 if (newset && setnumber >= 0)
1587 sprintf (setstring, "%d", ++setnumber);
1589 req->sortedResultSetName = odr_strdup (out, setstring);
1591 req->sortSequence = yaz_sort_spec (out, arg);
1592 if (!req->sortSequence)
1594 printf ("Missing sort specifications\n");
1601 void display_term(Z_TermInfo *t)
1603 if (t->term->which == Z_Term_general)
1605 printf("%.*s", t->term->u.general->len, t->term->u.general->buf);
1606 sprintf(last_scan_line, "%.*s", t->term->u.general->len,
1607 t->term->u.general->buf);
1610 printf("Term (not general)");
1611 if (t->globalOccurrences)
1612 printf (" (%d)\n", *t->globalOccurrences);
1617 void process_scanResponse(Z_ScanResponse *res)
1620 Z_Entry **entries = NULL;
1621 int num_entries = 0;
1623 printf("Received ScanResponse\n");
1624 print_refid (res->referenceId);
1625 printf("%d entries", *res->numberOfEntriesReturned);
1626 if (res->positionOfTerm)
1627 printf (", position=%d", *res->positionOfTerm);
1629 if (*res->scanStatus != Z_Scan_success)
1630 printf("Scan returned code %d\n", *res->scanStatus);
1633 if ((entries = res->entries->entries))
1634 num_entries = res->entries->num_entries;
1635 for (i = 0; i < num_entries; i++)
1637 int pos_term = res->positionOfTerm ? *res->positionOfTerm : -1;
1638 if (entries[i]->which == Z_Entry_termInfo)
1640 printf("%c ", i + 1 == pos_term ? '*' : ' ');
1641 display_term(entries[i]->u.termInfo);
1644 display_diagrecs(&entries[i]->u.surrogateDiagnostic, 1);
1646 if (res->entries->nonsurrogateDiagnostics)
1647 display_diagrecs (res->entries->nonsurrogateDiagnostics,
1648 res->entries->num_nonsurrogateDiagnostics);
1651 void process_sortResponse(Z_SortResponse *res)
1653 printf("Received SortResponse: status=");
1654 switch (*res->sortStatus)
1656 case Z_SortStatus_success:
1657 printf ("success"); break;
1658 case Z_SortStatus_partial_1:
1659 printf ("partial"); break;
1660 case Z_SortStatus_failure:
1661 printf ("failure"); break;
1663 printf ("unknown (%d)", *res->sortStatus);
1666 print_refid (res->referenceId);
1667 if (res->diagnostics)
1668 display_diagrecs(res->diagnostics,
1669 res->num_diagnostics);
1672 void process_deleteResultSetResponse (Z_DeleteResultSetResponse *res)
1674 printf("Got deleteResultSetResponse status=%d\n",
1675 *res->deleteOperationStatus);
1676 if (res->deleteListStatuses)
1679 for (i = 0; i < res->deleteListStatuses->num; i++)
1681 printf ("%s status=%d\n", res->deleteListStatuses->elements[i]->id,
1682 *res->deleteListStatuses->elements[i]->status);
1687 int cmd_sort_generic(char *arg, int newset)
1691 printf("Session not initialized yet\n");
1694 if (!ODR_MASK_GET(session->options, Z_Options_sort))
1696 printf("Target doesn't support sort\n");
1701 if (send_sortrequest(arg, newset) < 0)
1708 int cmd_sort(char *arg)
1710 return cmd_sort_generic (arg, 0);
1713 int cmd_sort_newset (char *arg)
1715 return cmd_sort_generic (arg, 1);
1718 int cmd_scan(char *arg)
1722 printf("Session not initialized yet\n");
1725 if (!ODR_MASK_GET(session->options, Z_Options_scan))
1727 printf("Target doesn't support scan\n");
1732 strcpy (last_scan_query, arg);
1733 if (send_scanrequest(arg, 1, 20, 0) < 0)
1738 if (send_scanrequest(last_scan_query, 1, 20, last_scan_line) < 0)
1744 int cmd_schema(char *arg)
1751 schema = oid_getvalbyname (arg);
1752 if (schema == VAL_NONE)
1754 printf ("unknown schema\n");
1760 int cmd_format(char *arg)
1764 printf("Usage: format <recordsyntax>\n");
1767 recordsyntax = oid_getvalbyname (arg);
1768 if (recordsyntax == VAL_NONE)
1770 printf ("unknown record syntax\n");
1776 int cmd_elements(char *arg)
1778 static Z_ElementSetNames esn;
1779 static char what[100];
1783 elementSetNames = 0;
1787 esn.which = Z_ElementSetNames_generic;
1788 esn.u.generic = what;
1789 elementSetNames = &esn;
1793 int cmd_attributeset(char *arg)
1799 printf("Usage: attributeset <setname>\n");
1802 sscanf(arg, "%s", what);
1803 if (p_query_attset (what))
1805 printf("Unknown attribute set name\n");
1811 int cmd_querytype (char *arg)
1813 if (!strcmp (arg, "ccl"))
1814 queryType = QueryType_CCL;
1815 else if (!strcmp (arg, "prefix") || !strcmp(arg, "rpn"))
1816 queryType = QueryType_Prefix;
1818 else if (!strcmp (arg, "ccl2rpn") || !strcmp (arg, "cclrpn"))
1819 queryType = QueryType_CCL2RPN;
1823 printf ("Querytype must be one of:\n");
1824 printf (" prefix - Prefix query\n");
1825 printf (" ccl - CCL query\n");
1827 printf (" ccl2rpn - CCL query converted to RPN\n");
1834 int cmd_refid (char *arg)
1840 refid = (char *) xmalloc (strlen(arg)+1);
1841 strcpy (refid, arg);
1846 int cmd_close(char *arg)
1853 apdu = zget_APDU(out, Z_APDU_close);
1854 req = apdu->u.close;
1855 *req->closeReason = Z_Close_finished;
1857 printf("Sent close request.\n");
1862 int cmd_packagename(char* arg) {
1863 xfree (esPackageName);
1864 esPackageName = NULL;
1867 esPackageName = (char *) xmalloc (strlen(arg)+1);
1868 strcpy (esPackageName, arg);
1873 int cmd_proxy(char* arg) {
1878 yazProxy = (char *) xmalloc (strlen(arg)+1);
1879 strcpy (yazProxy, arg);
1884 static void initialize(void)
1889 if (!(out = odr_createmem(ODR_ENCODE)) ||
1890 !(in = odr_createmem(ODR_DECODE)) ||
1891 !(print = odr_createmem(ODR_PRINT)))
1893 fprintf(stderr, "failed to allocate ODR streams\n");
1896 setvbuf(stdout, 0, _IONBF, 0);
1898 odr_setprint(print, apdu_file);
1901 bibset = ccl_qual_mk ();
1902 inf = fopen (ccl_fields, "r");
1905 ccl_qual_file (bibset, inf);
1909 cmd_base("Default");
1912 static int client(int wait)
1916 int (*fun)(char *arg);
1919 {"open", cmd_open, "('tcp'|'osi')':'[<tsel>'/']<host>[':'<port>]"},
1920 {"quit", cmd_quit, ""},
1921 {"find", cmd_find, "<query>"},
1922 {"delete", cmd_delete, "<setname>"},
1923 {"base", cmd_base, "<base-name>"},
1924 {"show", cmd_show, "<rec#>['+'<#recs>['+'<setname>]]"},
1925 {"scan", cmd_scan, "<term>"},
1926 {"sort", cmd_sort, "<sortkey> <flag> <sortkey> <flag> ..."},
1927 {"sort+", cmd_sort_newset, "<sortkey> <flag> <sortkey> <flag> ..."},
1928 {"authentication", cmd_authentication, "<acctstring>"},
1929 {"lslb", cmd_lslb, "<largeSetLowerBound>"},
1930 {"ssub", cmd_ssub, "<smallSetUpperBound>"},
1931 {"mspn", cmd_mspn, "<mediumSetPresentNumber>"},
1932 {"status", cmd_status, ""},
1933 {"setnames", cmd_setnames, ""},
1934 {"cancel", cmd_cancel, ""},
1935 {"format", cmd_format, "<recordsyntax>"},
1936 {"schema", cmd_schema, "<schema>"},
1937 {"elements", cmd_elements, "<elementSetName>"},
1938 {"close", cmd_close, ""},
1939 {"attributeset", cmd_attributeset, "<attrset>"},
1940 {"querytype", cmd_querytype, "<type>"},
1941 {"refid", cmd_refid, "<id>"},
1942 {"itemorder", cmd_itemorder, "ill|item <itemno>"},
1943 {"update", cmd_update, "<item>"},
1944 {"packagename", cmd_packagename, "<packagename>"},
1945 {"proxy", cmd_proxy, "('tcp'|'osi')':'[<tsel>'/']<host>[':'<port>]"},
1946 /* Server Admin Functions */
1947 {"adm-reindex", cmd_adm_reindex, "<database-name>"},
1948 {"adm-truncate", cmd_adm_truncate, "('database'|'index')<object-name>"},
1949 {"adm-create", cmd_adm_create, ""},
1950 {"adm-drop", cmd_adm_drop, "('database'|'index')<object-name>"},
1951 {"adm-import", cmd_adm_import, "<record-type> <dir> <pattern>"},
1952 {"adm-refresh", cmd_adm_refresh, ""},
1953 {"adm-commit", cmd_adm_commit, ""},
1954 {"adm-shutdown", cmd_adm_shutdown, ""},
1955 {"adm-startup", cmd_adm_startup, ""},
1959 int netbufferlen = 0;
1962 #if HAVE_GETTIMEOFDAY
1963 struct timeval tv_start, tv_end;
1964 gettimeofday (&tv_start, 0);
1973 char line[1024], word[32], arg[1024];
1979 FD_SET(cs_fileno(conn), &input);
1980 if ((res = select(20, &input, 0, 0, 0)) < 0)
1987 if (!wait && FD_ISSET(0, &input))
1992 #if HAVE_READLINE_READLINE_H
1994 line_in=readline(C_PROMPT);
1997 #if HAVE_READLINE_HISTORY_H
1999 add_history(line_in);
2001 strcpy(line,line_in);
2007 if (!fgets(line, 1023, stdin))
2009 if ((end_p = strchr (line, '\n')))
2012 #if HAVE_GETTIMEOFDAY
2013 gettimeofday (&tv_start, 0);
2016 if ((res = sscanf(line, "%31s %1023[^;]", word, arg)) <= 0)
2018 strcpy(word, last_cmd);
2023 strcpy(last_cmd, word);
2024 for (i = 0; cmd[i].cmd; i++)
2025 if (!strncmp(cmd[i].cmd, word, strlen(word)))
2027 res = (*cmd[i].fun)(arg);
2030 if (!cmd[i].cmd) /* dump our help-screen */
2032 printf("Unknown command: %s.\n", word);
2033 printf("Currently recognized commands:\n");
2034 for (i = 0; cmd[i].cmd; i++)
2035 printf(" %s %s\n", cmd[i].cmd, cmd[i].ad);
2046 && FD_ISSET(cs_fileno(conn), &input)
2052 if ((res = cs_get(conn, &netbuffer, &netbufferlen)) < 0)
2059 printf("Target closed connection.\n");
2062 odr_reset(in); /* release APDU from last round */
2064 odr_setbuf(in, netbuffer, res, 0);
2065 if (!z_APDU(in, &apdu, 0, 0))
2067 odr_perror(in, "Decoding incoming APDU");
2068 fprintf(stderr, "[Near %d]\n", odr_offset(in));
2069 fprintf(stderr, "Packet dump:\n---------\n");
2070 odr_dumpBER(stderr, netbuffer, res);
2071 fprintf(stderr, "---------\n");
2073 z_APDU(print, &apdu, 0, 0);
2076 if (apdu_file && !z_APDU(print, &apdu, 0, 0))
2078 odr_perror(print, "Failed to print incoming APDU");
2084 case Z_APDU_initResponse:
2085 process_initResponse(apdu->u.initResponse);
2087 case Z_APDU_searchResponse:
2088 process_searchResponse(apdu->u.searchResponse);
2090 case Z_APDU_scanResponse:
2091 process_scanResponse(apdu->u.scanResponse);
2093 case Z_APDU_presentResponse:
2094 print_refid (apdu->u.presentResponse->referenceId);
2096 *apdu->u.presentResponse->numberOfRecordsReturned;
2097 if (apdu->u.presentResponse->records)
2098 display_records(apdu->u.presentResponse->records);
2100 printf("No records.\n");
2101 printf ("nextResultSetPosition = %d\n",
2102 *apdu->u.presentResponse->nextResultSetPosition);
2104 case Z_APDU_sortResponse:
2105 process_sortResponse(apdu->u.sortResponse);
2107 case Z_APDU_extendedServicesResponse:
2108 printf("Got extended services response\n");
2109 process_ESResponse(apdu->u.extendedServicesResponse);
2112 printf("Target has closed the association.\n");
2113 process_close(apdu->u.close);
2115 case Z_APDU_resourceControlRequest:
2116 process_resourceControlRequest
2117 (apdu->u.resourceControlRequest);
2119 case Z_APDU_deleteResultSetResponse:
2120 process_deleteResultSetResponse(apdu->u.
2121 deleteResultSetResponse);
2124 printf("Received unknown APDU type (%d).\n",
2129 while (conn && cs_more(conn));
2130 #if HAVE_GETTIMEOFDAY
2131 gettimeofday (&tv_end, 0);
2134 printf ("Elapsed: %.6f\n", (double) tv_end.tv_usec /
2135 1e6 + tv_end.tv_sec -
2136 ((double) tv_start.tv_usec / 1e6 + tv_start.tv_sec));
2144 int main(int argc, char **argv)
2151 while ((ret = options("c:a:m:v:p:", argv, argc, &arg)) != -2)
2159 if (cmd_open (arg) == 2) {
2160 #if HAVE_READLINE_HISTORY_H
2161 char* tmp=(char*)malloc(strlen(arg)+6);
2163 strcat(tmp,"open ");
2173 if (!(marcdump = fopen (arg, "a")))
2180 strncpy (ccl_fields, arg, sizeof(ccl_fields)-1);
2181 ccl_fields[sizeof(ccl_fields)-1] = '\0';
2184 if (!strcmp(arg, "-"))
2187 apdu_file=fopen(arg, "a");
2190 yazProxy=strdup(arg);
2193 yaz_log_init (yaz_log_mask_str(arg), "", NULL);
2196 fprintf (stderr, "Usage: %s [-m <marclog>] [ -a <apdulog>] "
2197 "[-c cclfields] [-p <proxy-addr>] [<server-addr>]\n",
2204 return client (opened);