2 * Copyright (c) 1995-2001, Index Data
3 * See the file LICENSE for details.
5 * $Id: client.c,v 1.131 2001-11-18 23:17:45 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 = -1; /* 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))
231 printf (" namedResultSets");
239 static int cmd_base(char *arg)
246 printf("Usage: base <database> <database> ...\n");
249 for (i = 0; i<num_databaseNames; i++)
250 xfree (databaseNames[i]);
251 num_databaseNames = 0;
254 if (!(cp = strchr(arg, ' ')))
255 cp = arg + strlen(arg);
258 databaseNames[num_databaseNames] = (char *)xmalloc (1 + cp - arg);
259 memcpy (databaseNames[num_databaseNames], arg, cp - arg);
260 databaseNames[num_databaseNames++][cp - arg] = '\0';
269 int cmd_open(char *arg)
272 char type_and_host[101], base[101];
277 printf("Already connected.\n");
283 nmem_destroy (session_mem);
289 if (sscanf (arg, "%100[^/]/%100s", type_and_host, base) < 1)
294 conn = cs_create_host(yazProxy, 1, &add);
298 conn = cs_create_host(type_and_host, 1, &add);
303 printf ("Couldn't create comstack\n");
306 printf("Connecting...");
308 if (cs_connect(conn, add) < 0)
310 printf ("error = %s\n", cs_strerror(conn));
311 if (conn->cerrno == CSYSERR)
319 send_initRequest(type_and_host);
325 int cmd_authentication(char *arg)
327 static Z_IdAuthentication au;
328 static char open[256];
332 printf("Auth field set to null\n");
337 au.which = Z_IdAuthentication_open;
343 /* SEARCH SERVICE ------------------------------ */
345 static void display_variant(Z_Variant *v, int level)
349 for (i = 0; i < v->num_triples; i++)
351 printf("%*sclass=%d,type=%d", level * 4, "", *v->triples[i]->zclass,
352 *v->triples[i]->type);
353 if (v->triples[i]->which == Z_Triple_internationalString)
354 printf(",value=%s\n", v->triples[i]->value.internationalString);
360 static void display_grs1(Z_GenericRecord *r, int level)
366 for (i = 0; i < r->num_elements; i++)
370 printf("%*s", level * 4, "");
374 printf("%d,", *t->tagType);
377 if (t->tagValue->which == Z_StringOrNumeric_numeric)
378 printf("%d) ", *t->tagValue->u.numeric);
380 printf("%s) ", t->tagValue->u.string);
381 if (t->content->which == Z_ElementData_subtree)
384 display_grs1(t->content->u.subtree, level+1);
386 else if (t->content->which == Z_ElementData_string)
387 printf("%s\n", t->content->u.string);
388 else if (t->content->which == Z_ElementData_numeric)
389 printf("%d\n", *t->content->u.numeric);
390 else if (t->content->which == Z_ElementData_oid)
392 int *ip = t->content->u.oid;
395 if ((oent = oid_getentbyoid(t->content->u.oid)))
396 printf("OID: %s\n", oent->desc);
400 while (ip && *ip >= 0)
401 printf(" %d", *(ip++));
405 else if (t->content->which == Z_ElementData_noDataRequested)
406 printf("[No data requested]\n");
407 else if (t->content->which == Z_ElementData_elementEmpty)
408 printf("[Element empty]\n");
409 else if (t->content->which == Z_ElementData_elementNotThere)
410 printf("[Element not there]\n");
413 if (t->appliedVariant)
414 display_variant(t->appliedVariant, level+1);
415 if (t->metaData && t->metaData->supportedVariants)
419 printf("%*s---- variant list\n", (level+1)*4, "");
420 for (c = 0; c < t->metaData->num_supportedVariants; c++)
422 printf("%*svariant #%d\n", (level+1)*4, "", c);
423 display_variant(t->metaData->supportedVariants[c], level + 2);
429 static void print_record(const unsigned char *buf, size_t len)
432 for (i = 0; i<len; i++)
433 if ((buf[i] <= 126 && buf[i] >= 32) || strchr ("\n\r\t\f", buf[i]))
434 fputc (buf[i], stdout);
436 printf ("\\X%02X", buf[i]);
437 /* add newline if not already added ... */
438 if (i <= 0 || buf[i-1] != '\n')
439 fputc ('\n', stdout);
442 static void display_record(Z_DatabaseRecord *p)
444 Z_External *r = (Z_External*) p;
445 oident *ent = oid_getentbyoid(r->direct_reference);
449 * Tell the user what we got.
451 if (r->direct_reference)
453 printf("Record type: ");
455 printf("%s\n", ent->desc);
456 else if (!odr_oid(print, &r->direct_reference, 0, 0))
458 odr_perror(print, "print oid");
462 /* Check if this is a known, ASN.1 type tucked away in an octet string */
463 if (ent && r->which == Z_External_octet)
465 Z_ext_typeent *type = z_ext_getentbyref(ent->value);
471 * Call the given decoder to process the record.
473 odr_setbuf(in, (char*)p->u.octet_aligned->buf,
474 p->u.octet_aligned->len, 0);
475 if (!(*type->fun)(in, (char **)&rr, 0, 0))
477 odr_perror(in, "Decoding constructed record.");
478 fprintf(stderr, "[Near %d]\n", odr_offset(in));
479 fprintf(stderr, "Packet dump:\n---------\n");
480 odr_dumpBER(stderr, (char*)p->u.octet_aligned->buf,
481 p->u.octet_aligned->len);
482 fprintf(stderr, "---------\n");
486 * Note: we throw away the original, BER-encoded record here.
487 * Do something else with it if you want to keep it.
489 r->u.sutrs = (Z_SUTRS *) rr; /* we don't actually check the type here. */
490 r->which = type->what;
493 if (ent && ent->value == VAL_SOIF)
494 print_record((const unsigned char *) r->u.octet_aligned->buf,
495 r->u.octet_aligned->len);
496 else if (r->which == Z_External_octet && p->u.octet_aligned->len)
498 const char *octet_buf = (char*)p->u.octet_aligned->buf;
499 if (ent->value == VAL_TEXT_XML || ent->value == VAL_APPLICATION_XML ||
500 ent->value == VAL_HTML)
501 print_record((const unsigned char *) octet_buf,
502 p->u.octet_aligned->len);
505 if (marc_display_exl (octet_buf, NULL, 0 /* debug */,
506 p->u.octet_aligned->len)
509 printf ("ISO2709 decoding failed, dumping record as is:\n");
510 print_record((const unsigned char*) octet_buf,
511 p->u.octet_aligned->len);
515 fwrite (octet_buf, 1, p->u.octet_aligned->len, marcdump);
517 else if (ent && ent->value == VAL_SUTRS)
519 if (r->which != Z_External_sutrs)
521 printf("Expecting single SUTRS type for SUTRS.\n");
524 print_record(r->u.sutrs->buf, r->u.sutrs->len);
526 else if (ent && ent->value == VAL_GRS1)
528 if (r->which != Z_External_grs1)
530 printf("Expecting single GRS type for GRS.\n");
533 display_grs1(r->u.grs1, 0);
537 printf("Unknown record representation.\n");
538 if (!z_External(print, &r, 0, 0))
540 odr_perror(print, "Printing external");
547 static void display_diagrecs(Z_DiagRec **pp, int num)
551 Z_DefaultDiagFormat *r;
553 printf("Diagnostic message(s) from database:\n");
554 for (i = 0; i<num; i++)
556 Z_DiagRec *p = pp[i];
557 if (p->which != Z_DiagRec_defaultFormat)
559 printf("Diagnostic record not in default format.\n");
563 r = p->u.defaultFormat;
564 if (!(ent = oid_getentbyoid(r->diagnosticSetId)) ||
565 ent->oclass != CLASS_DIAGSET || ent->value != VAL_BIB1)
566 printf("Missing or unknown diagset\n");
567 printf(" [%d] %s", *r->condition, diagbib1_str(*r->condition));
570 case Z_DefaultDiagFormat_v2Addinfo:
571 printf (" -- v2 addinfo '%s'\n", r->u.v2Addinfo);
573 case Z_DefaultDiagFormat_v3Addinfo:
574 printf (" -- v3 addinfo '%s'\n", r->u.v3Addinfo);
581 static void display_nameplusrecord(Z_NamePlusRecord *p)
584 printf("[%s]", p->databaseName);
585 if (p->which == Z_NamePlusRecord_surrogateDiagnostic)
586 display_diagrecs(&p->u.surrogateDiagnostic, 1);
587 else if (p->which == Z_NamePlusRecord_databaseRecord)
588 display_record(p->u.databaseRecord);
591 static void display_records(Z_Records *p)
595 if (p->which == Z_Records_NSD)
597 Z_DiagRec dr, *dr_p = &dr;
598 dr.which = Z_DiagRec_defaultFormat;
599 dr.u.defaultFormat = p->u.nonSurrogateDiagnostic;
600 display_diagrecs (&dr_p, 1);
602 else if (p->which == Z_Records_multipleNSD)
603 display_diagrecs (p->u.multipleNonSurDiagnostics->diagRecs,
604 p->u.multipleNonSurDiagnostics->num_diagRecs);
607 printf("Records: %d\n", p->u.databaseOrSurDiagnostics->num_records);
608 for (i = 0; i < p->u.databaseOrSurDiagnostics->num_records; i++)
609 display_nameplusrecord(p->u.databaseOrSurDiagnostics->records[i]);
613 static int send_deleteResultSetRequest(char *arg)
618 Z_APDU *apdu = zget_APDU(out, Z_APDU_deleteResultSetRequest);
619 Z_DeleteResultSetRequest *req = apdu->u.deleteResultSetRequest;
621 req->referenceId = set_refid (out);
623 req->num_resultSetList =
624 sscanf (arg, "%30s %30s %30s %30s %30s %30s %30s %30s",
625 names[0], names[1], names[2], names[3],
626 names[4], names[5], names[6], names[7]);
628 req->deleteFunction = (int *)
629 odr_malloc (out, sizeof(*req->deleteFunction));
630 if (req->num_resultSetList > 0)
632 *req->deleteFunction = Z_DeleteRequest_list;
633 req->resultSetList = (char **)
634 odr_malloc (out, sizeof(*req->resultSetList)*
635 req->num_resultSetList);
636 for (i = 0; i<req->num_resultSetList; i++)
637 req->resultSetList[i] = names[i];
641 *req->deleteFunction = Z_DeleteRequest_all;
642 req->resultSetList = 0;
646 printf("Sent deleteResultSetRequest.\n");
650 static int send_searchRequest(char *arg)
652 Z_APDU *apdu = zget_APDU(out, Z_APDU_searchRequest);
653 Z_SearchRequest *req = apdu->u.searchRequest;
657 struct ccl_rpn_node *rpn = NULL;
661 Z_RPNQuery *RPNquery;
665 if (queryType == QueryType_CCL2RPN)
667 rpn = ccl_find_str(bibset, arg, &error, &pos);
670 printf("CCL ERROR: %s\n", ccl_err_msg(error));
675 req->referenceId = set_refid (out);
676 if (!strcmp(arg, "@big")) /* strictly for troublemaking */
678 static unsigned char big[2100];
681 /* send a very big referenceid to test transport stack etc. */
682 memset(big, 'A', 2100);
683 bigo.len = bigo.size = 2100;
685 req->referenceId = &bigo;
690 sprintf(setstring, "%d", ++setnumber);
691 req->resultSetName = setstring;
693 *req->smallSetUpperBound = smallSetUpperBound;
694 *req->largeSetLowerBound = largeSetLowerBound;
695 *req->mediumSetPresentNumber = mediumSetPresentNumber;
696 if (smallSetUpperBound > 0 || (largeSetLowerBound > 1 &&
697 mediumSetPresentNumber > 0))
701 prefsyn.proto = protocol;
702 prefsyn.oclass = CLASS_RECSYN;
703 prefsyn.value = recordsyntax;
704 req->preferredRecordSyntax =
705 odr_oiddup(out, oid_ent_to_oid(&prefsyn, oid));
706 req->smallSetElementSetNames =
707 req->mediumSetElementSetNames = elementSetNames;
709 req->num_databaseNames = num_databaseNames;
710 req->databaseNames = databaseNames;
716 case QueryType_Prefix:
717 query.which = Z_Query_type_1;
718 RPNquery = p_query_rpn (out, protocol, arg);
721 printf("Prefix query error\n");
724 query.u.type_1 = RPNquery;
727 query.which = Z_Query_type_2;
728 query.u.type_2 = &ccl_query;
729 ccl_query.buf = (unsigned char*) arg;
730 ccl_query.len = strlen(arg);
733 case QueryType_CCL2RPN:
734 query.which = Z_Query_type_1;
735 RPNquery = ccl_rpn_query(out, rpn);
738 printf ("Couldn't convert from CCL to RPN\n");
741 query.u.type_1 = RPNquery;
742 ccl_rpn_delete (rpn);
746 printf ("Unsupported query type\n");
751 printf("Sent searchRequest.\n");
755 static int process_searchResponse(Z_SearchResponse *res)
757 printf ("Received SearchResponse.\n");
758 print_refid (res->referenceId);
759 if (*res->searchStatus)
760 printf("Search was a success.\n");
762 printf("Search was a bloomin' failure.\n");
763 printf("Number of hits: %d", *res->resultCount);
765 printf (", setno %d", setnumber);
766 printf("\nrecords returned: %d\n",
767 *res->numberOfRecordsReturned);
768 setno += *res->numberOfRecordsReturned;
770 display_records(res->records);
774 static void print_level(int iLevel)
777 for (i = 0; i < iLevel * 4; i++)
781 static void print_int(int iLevel, const char *pTag, int *pInt)
786 printf("%s: %d\n", pTag, *pInt);
790 static void print_string(int iLevel, const char *pTag, const char *pString)
795 printf("%s: %s\n", pTag, pString);
799 static void print_oid(int iLevel, const char *pTag, Odr_oid *pOid)
807 for (; *pInt != -1; pInt++)
808 printf(" %d", *pInt);
813 static void print_referenceId(int iLevel, Z_ReferenceId *referenceId)
815 if (referenceId != NULL)
820 printf("Ref Id (%d, %d): ", referenceId->len, referenceId->size);
821 for (i = 0; i < referenceId->len; i++)
822 printf("%c", referenceId->buf[i]);
827 static void print_string_or_numeric(int iLevel, const char *pTag, Z_StringOrNumeric *pStringNumeric)
829 if (pStringNumeric != NULL)
831 switch (pStringNumeric->which)
833 case Z_StringOrNumeric_string:
834 print_string(iLevel, pTag, pStringNumeric->u.string);
837 case Z_StringOrNumeric_numeric:
838 print_int(iLevel, pTag, pStringNumeric->u.numeric);
843 printf("%s: valid type for Z_StringOrNumeric\n", pTag);
849 static void print_universe_report_duplicate(int iLevel, Z_UniverseReportDuplicate *pUniverseReportDuplicate)
851 if (pUniverseReportDuplicate != NULL)
854 printf("Universe Report Duplicate: \n");
856 print_string_or_numeric(iLevel, "Hit No", pUniverseReportDuplicate->hitno);
860 static void print_universe_report_hits(int iLevel, Z_UniverseReportHits *pUniverseReportHits)
862 if (pUniverseReportHits != NULL)
865 printf("Universe Report Hits: \n");
867 print_string_or_numeric(iLevel, "Database", pUniverseReportHits->database);
868 print_string_or_numeric(iLevel, "Hits", pUniverseReportHits->hits);
872 static void print_universe_report(int iLevel, Z_UniverseReport *pUniverseReport)
874 if (pUniverseReport != NULL)
877 printf("Universe Report: \n");
879 print_int(iLevel, "Total Hits", pUniverseReport->totalHits);
880 switch (pUniverseReport->which)
882 case Z_UniverseReport_databaseHits:
883 print_universe_report_hits(iLevel, pUniverseReport->u.databaseHits);
886 case Z_UniverseReport_duplicate:
887 print_universe_report_duplicate(iLevel, pUniverseReport->u.duplicate);
892 printf("Type: %d\n", pUniverseReport->which);
898 static void print_external(int iLevel, Z_External *pExternal)
900 if (pExternal != NULL)
903 printf("External: \n");
905 print_oid(iLevel, "Direct Reference", pExternal->direct_reference);
906 print_int(iLevel, "InDirect Reference", pExternal->indirect_reference);
907 print_string(iLevel, "Descriptor", pExternal->descriptor);
908 switch (pExternal->which)
910 case Z_External_universeReport:
911 print_universe_report(iLevel, pExternal->u.universeReport);
916 printf("Type: %d\n", pExternal->which);
922 static int process_resourceControlRequest (Z_ResourceControlRequest *req)
924 printf ("Received ResourceControlRequest.\n");
925 print_referenceId(1, req->referenceId);
926 print_int(1, "Suspended Flag", req->suspendedFlag);
927 print_int(1, "Partial Results Available", req->partialResultsAvailable);
928 print_int(1, "Response Required", req->responseRequired);
929 print_int(1, "Triggered Request Flag", req->triggeredRequestFlag);
930 print_external(1, req->resourceReport);
934 void process_ESResponse(Z_ExtendedServicesResponse *res)
936 printf("process_ESResponse status=");
937 switch (*res->operationStatus)
939 case Z_ExtendedServicesResponse_done:
942 case Z_ExtendedServicesResponse_accepted:
943 printf ("accepted\n");
945 case Z_ExtendedServicesResponse_failure:
946 printf ("failure\n");
947 display_diagrecs(res->diagnostics, res->num_diagnostics);
950 if ( (*res->operationStatus != Z_ExtendedServicesResponse_failure) &&
951 (res->num_diagnostics != 0) ) {
952 display_diagrecs(res->diagnostics, res->num_diagnostics);
959 const char *get_ill_element (void *clientData, const char *element)
961 printf ("%s\n", element);
965 static Z_External *create_external_itemRequest()
967 struct ill_get_ctl ctl;
968 ILL_ItemRequest *req;
970 int item_request_size = 0;
971 char *item_request_buf = 0;
975 ctl.f = get_ill_element;
977 req = ill_get_ItemRequest(&ctl, "ill", 0);
979 printf ("ill_get_ItemRequest failed\n");
981 if (!ill_ItemRequest (out, &req, 0, 0))
985 ill_ItemRequest(print, &req, 0, 0);
988 item_request_buf = odr_getbuf (out, &item_request_size, 0);
989 if (item_request_buf)
990 odr_setbuf (out, item_request_buf, item_request_size, 1);
991 printf ("Couldn't encode ItemRequest, size %d\n", item_request_size);
998 item_request_buf = odr_getbuf (out, &item_request_size, 0);
999 oid.proto = PROTO_GENERAL;
1000 oid.oclass = CLASS_GENERAL;
1001 oid.value = VAL_ISO_ILL_1;
1003 r = (Z_External *) odr_malloc (out, sizeof(*r));
1004 r->direct_reference = odr_oiddup(out,oid_getoidbyent(&oid));
1005 r->indirect_reference = 0;
1007 r->which = Z_External_single;
1009 r->u.single_ASN1_type = (Odr_oct *)
1010 odr_malloc (out, sizeof(*r->u.single_ASN1_type));
1011 r->u.single_ASN1_type->buf = (unsigned char *)
1012 odr_malloc (out, item_request_size);
1013 r->u.single_ASN1_type->len = item_request_size;
1014 r->u.single_ASN1_type->size = item_request_size;
1015 memcpy (r->u.single_ASN1_type->buf, item_request_buf,
1017 printf ("len = %d\n", item_request_size);
1023 #ifdef YAZ_MODULE_ill
1024 static Z_External *create_external_ILL_APDU(int which)
1026 struct ill_get_ctl ctl;
1029 int ill_request_size = 0;
1030 char *ill_request_buf = 0;
1034 ctl.f = get_ill_element;
1036 ill_apdu = ill_get_APDU(&ctl, "ill", 0);
1038 if (!ill_APDU (out, &ill_apdu, 0, 0))
1042 printf ("-------------------\n");
1043 ill_APDU(print, &ill_apdu, 0, 0);
1045 printf ("-------------------\n");
1047 ill_request_buf = odr_getbuf (out, &ill_request_size, 0);
1048 if (ill_request_buf)
1049 odr_setbuf (out, ill_request_buf, ill_request_size, 1);
1050 printf ("Couldn't encode ILL-Request, size %d\n", ill_request_size);
1056 ill_request_buf = odr_getbuf (out, &ill_request_size, 0);
1058 oid.proto = PROTO_GENERAL;
1059 oid.oclass = CLASS_GENERAL;
1060 oid.value = VAL_ISO_ILL_1;
1062 r = (Z_External *) odr_malloc (out, sizeof(*r));
1063 r->direct_reference = odr_oiddup(out,oid_getoidbyent(&oid));
1064 r->indirect_reference = 0;
1066 r->which = Z_External_single;
1068 r->u.single_ASN1_type = (Odr_oct *)
1069 odr_malloc (out, sizeof(*r->u.single_ASN1_type));
1070 r->u.single_ASN1_type->buf = (unsigned char *)
1071 odr_malloc (out, ill_request_size);
1072 r->u.single_ASN1_type->len = ill_request_size;
1073 r->u.single_ASN1_type->size = ill_request_size;
1074 memcpy (r->u.single_ASN1_type->buf, ill_request_buf, ill_request_size);
1075 printf ("len = %d\n", ill_request_size);
1082 static Z_External *create_ItemOrderExternal(const char *type, int itemno)
1084 Z_External *r = (Z_External *) odr_malloc(out, sizeof(Z_External));
1085 oident ItemOrderRequest;
1087 ItemOrderRequest.proto = PROTO_Z3950;
1088 ItemOrderRequest.oclass = CLASS_EXTSERV;
1089 ItemOrderRequest.value = VAL_ITEMORDER;
1091 r->direct_reference = odr_oiddup(out,oid_getoidbyent(&ItemOrderRequest));
1092 r->indirect_reference = 0;
1095 r->which = Z_External_itemOrder;
1097 r->u.itemOrder = (Z_ItemOrder *) odr_malloc(out,sizeof(Z_ItemOrder));
1098 memset(r->u.itemOrder, 0, sizeof(Z_ItemOrder));
1099 r->u.itemOrder->which=Z_IOItemOrder_esRequest;
1101 r->u.itemOrder->u.esRequest = (Z_IORequest *)
1102 odr_malloc(out,sizeof(Z_IORequest));
1103 memset(r->u.itemOrder->u.esRequest, 0, sizeof(Z_IORequest));
1105 r->u.itemOrder->u.esRequest->toKeep = (Z_IOOriginPartToKeep *)
1106 odr_malloc(out,sizeof(Z_IOOriginPartToKeep));
1107 memset(r->u.itemOrder->u.esRequest->toKeep, 0, sizeof(Z_IOOriginPartToKeep));
1108 r->u.itemOrder->u.esRequest->notToKeep = (Z_IOOriginPartNotToKeep *)
1109 odr_malloc(out,sizeof(Z_IOOriginPartNotToKeep));
1110 memset(r->u.itemOrder->u.esRequest->notToKeep, 0, sizeof(Z_IOOriginPartNotToKeep));
1112 r->u.itemOrder->u.esRequest->toKeep->supplDescription = NULL;
1113 r->u.itemOrder->u.esRequest->toKeep->contact = NULL;
1114 r->u.itemOrder->u.esRequest->toKeep->addlBilling = NULL;
1116 r->u.itemOrder->u.esRequest->notToKeep->resultSetItem =
1117 (Z_IOResultSetItem *) odr_malloc(out, sizeof(Z_IOResultSetItem));
1118 memset(r->u.itemOrder->u.esRequest->notToKeep->resultSetItem, 0, sizeof(Z_IOResultSetItem));
1119 r->u.itemOrder->u.esRequest->notToKeep->resultSetItem->resultSetId = "1";
1121 r->u.itemOrder->u.esRequest->notToKeep->resultSetItem->item =
1122 (int *) odr_malloc(out, sizeof(int));
1123 *r->u.itemOrder->u.esRequest->notToKeep->resultSetItem->item = itemno;
1126 if (!strcmp (type, "item") || !strcmp(type, "2"))
1128 printf ("using item-request\n");
1129 r->u.itemOrder->u.esRequest->notToKeep->itemRequest =
1130 create_external_itemRequest();
1132 else if (!strcmp(type, "ill") || !strcmp(type, "1"))
1134 printf ("using ILL-request\n");
1135 r->u.itemOrder->u.esRequest->notToKeep->itemRequest =
1136 create_external_ILL_APDU(ILL_APDU_ILL_Request);
1138 else if (!strcmp(type, "xml") || !strcmp(type, "3"))
1140 const char *xml_buf =
1142 " <type>request</type>\n"
1143 " <libraryNo>000200</libraryNo>\n"
1144 " <borrowerTicketNo> 1212 </borrowerTicketNo>\n"
1146 r->u.itemOrder->u.esRequest->notToKeep->itemRequest =
1147 z_ext_record (out, VAL_TEXT_XML, xml_buf, strlen(xml_buf));
1150 r->u.itemOrder->u.esRequest->notToKeep->itemRequest = 0;
1153 r->u.itemOrder->u.esRequest->notToKeep->itemRequest = 0;
1158 static int send_itemorder(const char *type, int itemno)
1160 Z_APDU *apdu = zget_APDU(out, Z_APDU_extendedServicesRequest);
1161 Z_ExtendedServicesRequest *req = apdu->u.extendedServicesRequest;
1162 oident ItemOrderRequest;
1164 ItemOrderRequest.proto = PROTO_Z3950;
1165 ItemOrderRequest.oclass = CLASS_EXTSERV;
1166 ItemOrderRequest.value = VAL_ITEMORDER;
1167 req->packageType = odr_oiddup(out,oid_getoidbyent(&ItemOrderRequest));
1168 req->packageName = esPackageName;
1170 req->taskSpecificParameters = create_ItemOrderExternal(type, itemno);
1176 static int cmd_update(char *arg)
1178 Z_APDU *apdu = zget_APDU(out, Z_APDU_extendedServicesRequest );
1179 Z_ExtendedServicesRequest *req = apdu->u.extendedServicesRequest;
1182 Z_IUOriginPartToKeep *toKeep;
1183 Z_IUSuppliedRecords *notToKeep;
1185 printf ("Update request\n");
1190 update_oid.proto = PROTO_Z3950;
1191 update_oid.oclass = CLASS_EXTSERV;
1192 update_oid.value = VAL_DBUPDATE;
1193 oid_ent_to_oid (&update_oid, oid);
1194 req->packageType = odr_oiddup(out,oid);
1195 req->packageName = esPackageName;
1197 req->referenceId = set_refid (out);
1199 r = req->taskSpecificParameters = (Z_External *)
1200 odr_malloc (out, sizeof(*r));
1201 r->direct_reference = odr_oiddup(out,oid);
1202 r->indirect_reference = 0;
1204 r->which = Z_External_update;
1205 r->u.update = (Z_IUUpdate *) odr_malloc(out, sizeof(*r->u.update));
1206 r->u.update->which = Z_IUUpdate_esRequest;
1207 r->u.update->u.esRequest = (Z_IUUpdateEsRequest *)
1208 odr_malloc(out, sizeof(*r->u.update->u.esRequest));
1209 toKeep = r->u.update->u.esRequest->toKeep = (Z_IUOriginPartToKeep *)
1210 odr_malloc(out, sizeof(*r->u.update->u.esRequest->toKeep));
1211 toKeep->databaseName = databaseNames[0];
1213 toKeep->elementSetName = 0;
1214 toKeep->actionQualifier = 0;
1215 toKeep->action = (int *) odr_malloc(out, sizeof(*toKeep->action));
1216 *toKeep->action = Z_IUOriginPartToKeep_recordInsert;
1218 notToKeep = r->u.update->u.esRequest->notToKeep = (Z_IUSuppliedRecords *)
1219 odr_malloc(out, sizeof(*r->u.update->u.esRequest->notToKeep));
1221 notToKeep->elements = (Z_IUSuppliedRecords_elem **)
1222 odr_malloc(out, sizeof(*notToKeep->elements));
1223 notToKeep->elements[0] = (Z_IUSuppliedRecords_elem *)
1224 odr_malloc(out, sizeof(**notToKeep->elements));
1225 notToKeep->elements[0]->u.number = 0;
1226 notToKeep->elements[0]->supplementalId = 0;
1227 notToKeep->elements[0]->correlationInfo = 0;
1228 notToKeep->elements[0]->record = record_last;
1235 /* II : Added to do DALI Item Order Extended services request */
1236 static int cmd_itemorder(char *arg)
1241 if (sscanf (arg, "%10s %d", type, &itemno) != 2)
1244 printf("Item order request\n");
1246 send_itemorder(type, itemno);
1250 static int cmd_find(char *arg)
1254 printf("Find what?\n");
1259 printf("Not connected yet\n");
1262 if (!send_searchRequest(arg))
1267 static int cmd_delete(char *arg)
1271 printf("Not connected yet\n");
1274 if (!send_deleteResultSetRequest(arg))
1279 static int cmd_ssub(char *arg)
1281 if (!(smallSetUpperBound = atoi(arg)))
1286 static int cmd_lslb(char *arg)
1288 if (!(largeSetLowerBound = atoi(arg)))
1293 static int cmd_mspn(char *arg)
1295 if (!(mediumSetPresentNumber = atoi(arg)))
1300 static int cmd_status(char *arg)
1302 printf("smallSetUpperBound: %d\n", smallSetUpperBound);
1303 printf("largeSetLowerBound: %d\n", largeSetLowerBound);
1304 printf("mediumSetPresentNumber: %d\n", mediumSetPresentNumber);
1308 static int cmd_setnames(char *arg)
1310 if (*arg == '1') /* enable ? */
1312 else if (*arg == '0') /* disable ? */
1314 else if (setnumber < 0) /* no args, toggle .. */
1320 printf("Set numbering enabled.\n");
1322 printf("Set numbering disabled.\n");
1326 /* PRESENT SERVICE ----------------------------- */
1328 static int send_presentRequest(char *arg)
1330 Z_APDU *apdu = zget_APDU(out, Z_APDU_presentRequest);
1331 Z_PresentRequest *req = apdu->u.presentRequest;
1332 Z_RecordComposition compo;
1337 char setstring[100];
1339 req->referenceId = set_refid (out);
1340 if ((p = strchr(arg, '+')))
1347 if (p && (p=strchr(p+1, '+')))
1349 strcpy (setstring, p+1);
1350 req->resultSetId = setstring;
1352 else if (setnumber >= 0)
1354 sprintf(setstring, "%d", setnumber);
1355 req->resultSetId = setstring;
1360 static Z_Range range;
1361 static Z_Range *rangep = ⦥
1362 req->num_ranges = 1;
1364 req->resultSetStartPoint = &setno;
1365 req->numberOfRecordsRequested = &nos;
1366 prefsyn.proto = protocol;
1367 prefsyn.oclass = CLASS_RECSYN;
1368 prefsyn.value = recordsyntax;
1369 req->preferredRecordSyntax =
1370 odr_oiddup (out, oid_ent_to_oid(&prefsyn, oid));
1372 if (schema != VAL_NONE)
1376 prefschema.proto = protocol;
1377 prefschema.oclass = CLASS_SCHEMA;
1378 prefschema.value = schema;
1380 req->recordComposition = &compo;
1381 compo.which = Z_RecordComp_complex;
1382 compo.u.complex = (Z_CompSpec *)
1383 odr_malloc(out, sizeof(*compo.u.complex));
1384 compo.u.complex->selectAlternativeSyntax = (bool_t *)
1385 odr_malloc(out, sizeof(bool_t));
1386 *compo.u.complex->selectAlternativeSyntax = 0;
1388 compo.u.complex->generic = (Z_Specification *)
1389 odr_malloc(out, sizeof(*compo.u.complex->generic));
1390 compo.u.complex->generic->schema = (Odr_oid *)
1391 odr_oiddup(out, oid_ent_to_oid(&prefschema, oid));
1392 if (!compo.u.complex->generic->schema)
1394 /* OID wasn't a schema! Try record syntax instead. */
1395 prefschema.oclass = CLASS_RECSYN;
1396 compo.u.complex->generic->schema = (Odr_oid *)
1397 odr_oiddup(out, oid_ent_to_oid(&prefschema, oid));
1399 if (!elementSetNames)
1400 compo.u.complex->generic->elementSpec = 0;
1403 compo.u.complex->generic->elementSpec = (Z_ElementSpec *)
1404 odr_malloc(out, sizeof(Z_ElementSpec));
1405 compo.u.complex->generic->elementSpec->which =
1406 Z_ElementSpec_elementSetName;
1407 compo.u.complex->generic->elementSpec->u.elementSetName =
1408 elementSetNames->u.generic;
1410 compo.u.complex->num_dbSpecific = 0;
1411 compo.u.complex->dbSpecific = 0;
1412 compo.u.complex->num_recordSyntax = 0;
1413 compo.u.complex->recordSyntax = 0;
1415 else if (elementSetNames)
1417 req->recordComposition = &compo;
1418 compo.which = Z_RecordComp_simple;
1419 compo.u.simple = elementSetNames;
1422 printf("Sent presentRequest (%d+%d).\n", setno, nos);
1426 void process_close(Z_Close *req)
1428 Z_APDU *apdu = zget_APDU(out, Z_APDU_close);
1429 Z_Close *res = apdu->u.close;
1431 static char *reasons[] =
1436 "cost limit reached",
1438 "security violation",
1445 printf("Reason: %s, message: %s\n", reasons[*req->closeReason],
1446 req->diagnosticInformation ? req->diagnosticInformation : "NULL");
1453 nmem_destroy (session_mem);
1460 *res->closeReason = Z_Close_finished;
1462 printf("Sent response.\n");
1467 static int cmd_show(char *arg)
1471 printf("Not connected yet\n");
1474 if (!send_presentRequest(arg))
1479 int cmd_quit(char *arg)
1481 printf("See you later, alligator.\n");
1487 int cmd_cancel(char *arg)
1489 Z_APDU *apdu = zget_APDU(out, Z_APDU_triggerResourceControlRequest);
1490 Z_TriggerResourceControlRequest *req =
1491 apdu->u.triggerResourceControlRequest;
1496 printf("Session not initialized yet\n");
1499 if (!ODR_MASK_GET(session->options, Z_Options_triggerResourceCtrl))
1501 printf("Target doesn't support cancel (trigger resource ctrl)\n");
1504 *req->requestedAction = Z_TriggerResourceCtrl_cancel;
1505 req->resultSetWanted = &rfalse;
1508 printf("Sent cancel request\n");
1512 int send_scanrequest(const char *query, int pp, int num, const char *term)
1514 Z_APDU *apdu = zget_APDU(out, Z_APDU_scanRequest);
1515 Z_ScanRequest *req = apdu->u.scanRequest;
1520 if (queryType == QueryType_CCL2RPN)
1524 struct ccl_rpn_node *rpn;
1526 rpn = ccl_find_str (bibset, query, &error, &pos);
1529 printf("CCL ERROR: %s\n", ccl_err_msg(error));
1533 bib1.proto = PROTO_Z3950;
1534 bib1.oclass = CLASS_ATTSET;
1535 bib1.value = VAL_BIB1;
1536 req->attributeSet = oid_ent_to_oid (&bib1, oid);
1537 if (!(req->termListAndStartPoint = ccl_scan_query (out, rpn)))
1539 printf("Couldn't convert CCL to Scan term\n");
1542 ccl_rpn_delete (rpn);
1545 if (use_rpn && !(req->termListAndStartPoint =
1546 p_query_scan(out, protocol, &req->attributeSet, query)))
1548 printf("Prefix query error\n");
1553 if (req->termListAndStartPoint->term &&
1554 req->termListAndStartPoint->term->which == Z_Term_general &&
1555 req->termListAndStartPoint->term->u.general)
1557 req->termListAndStartPoint->term->u.general->buf =
1558 (unsigned char *) odr_strdup(out, term);
1559 req->termListAndStartPoint->term->u.general->len =
1560 req->termListAndStartPoint->term->u.general->size =
1564 req->referenceId = set_refid (out);
1565 req->num_databaseNames = num_databaseNames;
1566 req->databaseNames = databaseNames;
1567 req->numberOfTermsRequested = #
1568 req->preferredPositionInResponse = &pp;
1573 int send_sortrequest(char *arg, int newset)
1575 Z_APDU *apdu = zget_APDU(out, Z_APDU_sortRequest);
1576 Z_SortRequest *req = apdu->u.sortRequest;
1577 Z_SortKeySpecList *sksl = (Z_SortKeySpecList *)
1578 odr_malloc (out, sizeof(*sksl));
1582 sprintf (setstring, "%d", setnumber);
1584 sprintf (setstring, "default");
1586 req->referenceId = set_refid (out);
1588 req->num_inputResultSetNames = 1;
1589 req->inputResultSetNames = (Z_InternationalString **)
1590 odr_malloc (out, sizeof(*req->inputResultSetNames));
1591 req->inputResultSetNames[0] = odr_strdup (out, setstring);
1593 if (newset && setnumber >= 0)
1594 sprintf (setstring, "%d", ++setnumber);
1596 req->sortedResultSetName = odr_strdup (out, setstring);
1598 req->sortSequence = yaz_sort_spec (out, arg);
1599 if (!req->sortSequence)
1601 printf ("Missing sort specifications\n");
1608 void display_term(Z_TermInfo *t)
1610 if (t->term->which == Z_Term_general)
1612 printf("%.*s", t->term->u.general->len, t->term->u.general->buf);
1613 sprintf(last_scan_line, "%.*s", t->term->u.general->len,
1614 t->term->u.general->buf);
1617 printf("Term (not general)");
1618 if (t->globalOccurrences)
1619 printf (" (%d)\n", *t->globalOccurrences);
1624 void process_scanResponse(Z_ScanResponse *res)
1627 Z_Entry **entries = NULL;
1628 int num_entries = 0;
1630 printf("Received ScanResponse\n");
1631 print_refid (res->referenceId);
1632 printf("%d entries", *res->numberOfEntriesReturned);
1633 if (res->positionOfTerm)
1634 printf (", position=%d", *res->positionOfTerm);
1636 if (*res->scanStatus != Z_Scan_success)
1637 printf("Scan returned code %d\n", *res->scanStatus);
1640 if ((entries = res->entries->entries))
1641 num_entries = res->entries->num_entries;
1642 for (i = 0; i < num_entries; i++)
1644 int pos_term = res->positionOfTerm ? *res->positionOfTerm : -1;
1645 if (entries[i]->which == Z_Entry_termInfo)
1647 printf("%c ", i + 1 == pos_term ? '*' : ' ');
1648 display_term(entries[i]->u.termInfo);
1651 display_diagrecs(&entries[i]->u.surrogateDiagnostic, 1);
1653 if (res->entries->nonsurrogateDiagnostics)
1654 display_diagrecs (res->entries->nonsurrogateDiagnostics,
1655 res->entries->num_nonsurrogateDiagnostics);
1658 void process_sortResponse(Z_SortResponse *res)
1660 printf("Received SortResponse: status=");
1661 switch (*res->sortStatus)
1663 case Z_SortStatus_success:
1664 printf ("success"); break;
1665 case Z_SortStatus_partial_1:
1666 printf ("partial"); break;
1667 case Z_SortStatus_failure:
1668 printf ("failure"); break;
1670 printf ("unknown (%d)", *res->sortStatus);
1673 print_refid (res->referenceId);
1674 if (res->diagnostics)
1675 display_diagrecs(res->diagnostics,
1676 res->num_diagnostics);
1679 void process_deleteResultSetResponse (Z_DeleteResultSetResponse *res)
1681 printf("Got deleteResultSetResponse status=%d\n",
1682 *res->deleteOperationStatus);
1683 if (res->deleteListStatuses)
1686 for (i = 0; i < res->deleteListStatuses->num; i++)
1688 printf ("%s status=%d\n", res->deleteListStatuses->elements[i]->id,
1689 *res->deleteListStatuses->elements[i]->status);
1694 int cmd_sort_generic(char *arg, int newset)
1698 printf("Session not initialized yet\n");
1701 if (!ODR_MASK_GET(session->options, Z_Options_sort))
1703 printf("Target doesn't support sort\n");
1708 if (send_sortrequest(arg, newset) < 0)
1715 int cmd_sort(char *arg)
1717 return cmd_sort_generic (arg, 0);
1720 int cmd_sort_newset (char *arg)
1722 return cmd_sort_generic (arg, 1);
1725 int cmd_scan(char *arg)
1729 printf("Session not initialized yet\n");
1732 if (!ODR_MASK_GET(session->options, Z_Options_scan))
1734 printf("Target doesn't support scan\n");
1739 strcpy (last_scan_query, arg);
1740 if (send_scanrequest(arg, 1, 20, 0) < 0)
1745 if (send_scanrequest(last_scan_query, 1, 20, last_scan_line) < 0)
1751 int cmd_schema(char *arg)
1758 schema = oid_getvalbyname (arg);
1759 if (schema == VAL_NONE)
1761 printf ("unknown schema\n");
1767 int cmd_format(char *arg)
1771 printf("Usage: format <recordsyntax>\n");
1774 recordsyntax = oid_getvalbyname (arg);
1775 if (recordsyntax == VAL_NONE)
1777 printf ("unknown record syntax\n");
1783 int cmd_elements(char *arg)
1785 static Z_ElementSetNames esn;
1786 static char what[100];
1790 elementSetNames = 0;
1794 esn.which = Z_ElementSetNames_generic;
1795 esn.u.generic = what;
1796 elementSetNames = &esn;
1800 int cmd_attributeset(char *arg)
1806 printf("Usage: attributeset <setname>\n");
1809 sscanf(arg, "%s", what);
1810 if (p_query_attset (what))
1812 printf("Unknown attribute set name\n");
1818 int cmd_querytype (char *arg)
1820 if (!strcmp (arg, "ccl"))
1821 queryType = QueryType_CCL;
1822 else if (!strcmp (arg, "prefix") || !strcmp(arg, "rpn"))
1823 queryType = QueryType_Prefix;
1825 else if (!strcmp (arg, "ccl2rpn") || !strcmp (arg, "cclrpn"))
1826 queryType = QueryType_CCL2RPN;
1830 printf ("Querytype must be one of:\n");
1831 printf (" prefix - Prefix query\n");
1832 printf (" ccl - CCL query\n");
1834 printf (" ccl2rpn - CCL query converted to RPN\n");
1841 int cmd_refid (char *arg)
1847 refid = (char *) xmalloc (strlen(arg)+1);
1848 strcpy (refid, arg);
1853 int cmd_close(char *arg)
1860 apdu = zget_APDU(out, Z_APDU_close);
1861 req = apdu->u.close;
1862 *req->closeReason = Z_Close_finished;
1864 printf("Sent close request.\n");
1869 int cmd_packagename(char* arg) {
1870 xfree (esPackageName);
1871 esPackageName = NULL;
1874 esPackageName = (char *) xmalloc (strlen(arg)+1);
1875 strcpy (esPackageName, arg);
1880 int cmd_proxy(char* arg) {
1885 yazProxy = (char *) xmalloc (strlen(arg)+1);
1886 strcpy (yazProxy, arg);
1891 static void initialize(void)
1896 if (!(out = odr_createmem(ODR_ENCODE)) ||
1897 !(in = odr_createmem(ODR_DECODE)) ||
1898 !(print = odr_createmem(ODR_PRINT)))
1900 fprintf(stderr, "failed to allocate ODR streams\n");
1903 setvbuf(stdout, 0, _IONBF, 0);
1905 odr_setprint(print, apdu_file);
1908 bibset = ccl_qual_mk ();
1909 inf = fopen (ccl_fields, "r");
1912 ccl_qual_file (bibset, inf);
1916 cmd_base("Default");
1919 static int client(int wait)
1923 int (*fun)(char *arg);
1926 {"open", cmd_open, "('tcp'|'osi')':'[<tsel>'/']<host>[':'<port>]"},
1927 {"quit", cmd_quit, ""},
1928 {"find", cmd_find, "<query>"},
1929 {"delete", cmd_delete, "<setname>"},
1930 {"base", cmd_base, "<base-name>"},
1931 {"show", cmd_show, "<rec#>['+'<#recs>['+'<setname>]]"},
1932 {"scan", cmd_scan, "<term>"},
1933 {"sort", cmd_sort, "<sortkey> <flag> <sortkey> <flag> ..."},
1934 {"sort+", cmd_sort_newset, "<sortkey> <flag> <sortkey> <flag> ..."},
1935 {"authentication", cmd_authentication, "<acctstring>"},
1936 {"lslb", cmd_lslb, "<largeSetLowerBound>"},
1937 {"ssub", cmd_ssub, "<smallSetUpperBound>"},
1938 {"mspn", cmd_mspn, "<mediumSetPresentNumber>"},
1939 {"status", cmd_status, ""},
1940 {"setnames", cmd_setnames, ""},
1941 {"cancel", cmd_cancel, ""},
1942 {"format", cmd_format, "<recordsyntax>"},
1943 {"schema", cmd_schema, "<schema>"},
1944 {"elements", cmd_elements, "<elementSetName>"},
1945 {"close", cmd_close, ""},
1946 {"attributeset", cmd_attributeset, "<attrset>"},
1947 {"querytype", cmd_querytype, "<type>"},
1948 {"refid", cmd_refid, "<id>"},
1949 {"itemorder", cmd_itemorder, "ill|item <itemno>"},
1950 {"update", cmd_update, "<item>"},
1951 {"packagename", cmd_packagename, "<packagename>"},
1952 {"proxy", cmd_proxy, "('tcp'|'osi')':'[<tsel>'/']<host>[':'<port>]"},
1953 /* Server Admin Functions */
1954 {"adm-reindex", cmd_adm_reindex, "<database-name>"},
1955 {"adm-truncate", cmd_adm_truncate, "('database'|'index')<object-name>"},
1956 {"adm-create", cmd_adm_create, ""},
1957 {"adm-drop", cmd_adm_drop, "('database'|'index')<object-name>"},
1958 {"adm-import", cmd_adm_import, "<record-type> <dir> <pattern>"},
1959 {"adm-refresh", cmd_adm_refresh, ""},
1960 {"adm-commit", cmd_adm_commit, ""},
1961 {"adm-shutdown", cmd_adm_shutdown, ""},
1962 {"adm-startup", cmd_adm_startup, ""},
1966 int netbufferlen = 0;
1969 #if HAVE_GETTIMEOFDAY
1970 struct timeval tv_start, tv_end;
1971 gettimeofday (&tv_start, 0);
1980 char line[1024], word[32], arg[1024];
1986 FD_SET(cs_fileno(conn), &input);
1987 if ((res = select(20, &input, 0, 0, 0)) < 0)
1994 if (!wait && FD_ISSET(0, &input))
1999 #if HAVE_READLINE_READLINE_H
2001 line_in=readline(C_PROMPT);
2004 #if HAVE_READLINE_HISTORY_H
2006 add_history(line_in);
2008 strcpy(line,line_in);
2014 if (!fgets(line, 1023, stdin))
2016 if ((end_p = strchr (line, '\n')))
2019 #if HAVE_GETTIMEOFDAY
2020 gettimeofday (&tv_start, 0);
2023 if ((res = sscanf(line, "%31s %1023[^;]", word, arg)) <= 0)
2025 strcpy(word, last_cmd);
2030 strcpy(last_cmd, word);
2031 for (i = 0; cmd[i].cmd; i++)
2032 if (!strncmp(cmd[i].cmd, word, strlen(word)))
2034 res = (*cmd[i].fun)(arg);
2037 if (!cmd[i].cmd) /* dump our help-screen */
2039 printf("Unknown command: %s.\n", word);
2040 printf("Currently recognized commands:\n");
2041 for (i = 0; cmd[i].cmd; i++)
2042 printf(" %s %s\n", cmd[i].cmd, cmd[i].ad);
2053 && FD_ISSET(cs_fileno(conn), &input)
2059 if ((res = cs_get(conn, &netbuffer, &netbufferlen)) < 0)
2066 printf("Target closed connection.\n");
2069 odr_reset(in); /* release APDU from last round */
2071 odr_setbuf(in, netbuffer, res, 0);
2072 if (!z_APDU(in, &apdu, 0, 0))
2074 odr_perror(in, "Decoding incoming APDU");
2075 fprintf(stderr, "[Near %d]\n", odr_offset(in));
2076 fprintf(stderr, "Packet dump:\n---------\n");
2077 odr_dumpBER(stderr, netbuffer, res);
2078 fprintf(stderr, "---------\n");
2080 z_APDU(print, &apdu, 0, 0);
2083 if (apdu_file && !z_APDU(print, &apdu, 0, 0))
2085 odr_perror(print, "Failed to print incoming APDU");
2091 case Z_APDU_initResponse:
2092 process_initResponse(apdu->u.initResponse);
2094 case Z_APDU_searchResponse:
2095 process_searchResponse(apdu->u.searchResponse);
2097 case Z_APDU_scanResponse:
2098 process_scanResponse(apdu->u.scanResponse);
2100 case Z_APDU_presentResponse:
2101 print_refid (apdu->u.presentResponse->referenceId);
2103 *apdu->u.presentResponse->numberOfRecordsReturned;
2104 if (apdu->u.presentResponse->records)
2105 display_records(apdu->u.presentResponse->records);
2107 printf("No records.\n");
2108 printf ("nextResultSetPosition = %d\n",
2109 *apdu->u.presentResponse->nextResultSetPosition);
2111 case Z_APDU_sortResponse:
2112 process_sortResponse(apdu->u.sortResponse);
2114 case Z_APDU_extendedServicesResponse:
2115 printf("Got extended services response\n");
2116 process_ESResponse(apdu->u.extendedServicesResponse);
2119 printf("Target has closed the association.\n");
2120 process_close(apdu->u.close);
2122 case Z_APDU_resourceControlRequest:
2123 process_resourceControlRequest
2124 (apdu->u.resourceControlRequest);
2126 case Z_APDU_deleteResultSetResponse:
2127 process_deleteResultSetResponse(apdu->u.
2128 deleteResultSetResponse);
2131 printf("Received unknown APDU type (%d).\n",
2136 while (conn && cs_more(conn));
2137 #if HAVE_GETTIMEOFDAY
2138 gettimeofday (&tv_end, 0);
2141 printf ("Elapsed: %.6f\n", (double) tv_end.tv_usec /
2142 1e6 + tv_end.tv_sec -
2143 ((double) tv_start.tv_usec / 1e6 + tv_start.tv_sec));
2151 int main(int argc, char **argv)
2158 while ((ret = options("c:a:m:v:p:", argv, argc, &arg)) != -2)
2166 if (cmd_open (arg) == 2) {
2167 #if HAVE_READLINE_HISTORY_H
2168 char* tmp=(char*)malloc(strlen(arg)+6);
2170 strcat(tmp,"open ");
2180 if (!(marcdump = fopen (arg, "a")))
2187 strncpy (ccl_fields, arg, sizeof(ccl_fields)-1);
2188 ccl_fields[sizeof(ccl_fields)-1] = '\0';
2191 if (!strcmp(arg, "-"))
2194 apdu_file=fopen(arg, "a");
2197 yazProxy=strdup(arg);
2200 yaz_log_init (yaz_log_mask_str(arg), "", NULL);
2203 fprintf (stderr, "Usage: %s [-m <marclog>] [ -a <apdulog>] "
2204 "[-c cclfields] [-p <proxy-addr>] [<server-addr>]\n",
2211 return client (opened);