2 * Copyright (c) 1995-2002, Index Data
3 * See the file LICENSE for details.
5 * $Id: client.c,v 1.135 2002-01-21 12:54:06 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 */
88 /* set this one to 1, to avoid decode of unknown MARCs */
89 #define AVOID_MARC_DECODE 1
91 void process_cmd_line(char* line);
93 ODR getODROutputStream()
98 void send_apdu(Z_APDU *a)
105 z_APDU(print, &a, 0, 0);
108 if (!z_APDU(out, &a, 0, 0))
110 odr_perror(out, "Encoding APDU");
113 buf = odr_getbuf(out, &len, 0);
114 /* printf ("sending APDU of size %d\n", len); */
115 if (cs_put(conn, buf, len) < 0)
117 fprintf(stderr, "cs_put: %s", cs_errmsg(cs_errno(conn)));
120 odr_reset(out); /* release the APDU structure */
123 static void print_stringn(const unsigned char *buf, size_t len)
126 for (i = 0; i<len; i++)
127 if ((buf[i] <= 126 && buf[i] >= 32) || strchr ("\n\r\t\f", buf[i]))
128 fputc (buf[i], stdout);
130 printf ("\\X%02X", buf[i]);
133 static void print_refid (Z_ReferenceId *id)
137 printf ("Reference Id: ");
138 print_stringn (id->buf, id->len);
143 static Z_ReferenceId *set_refid (ODR out)
148 id = (Z_ReferenceId *) odr_malloc (out, sizeof(*id));
149 id->size = id->len = strlen(refid);
150 id->buf = (unsigned char *) odr_malloc (out, id->len);
151 memcpy (id->buf, refid, id->len);
155 /* INIT SERVICE ------------------------------- */
157 static void send_initRequest(const char* type_and_host)
159 Z_APDU *apdu = zget_APDU(out, Z_APDU_initRequest);
160 Z_InitRequest *req = apdu->u.initRequest;
162 ODR_MASK_SET(req->options, Z_Options_search);
163 ODR_MASK_SET(req->options, Z_Options_present);
164 ODR_MASK_SET(req->options, Z_Options_namedResultSets);
165 ODR_MASK_SET(req->options, Z_Options_triggerResourceCtrl);
166 ODR_MASK_SET(req->options, Z_Options_scan);
167 ODR_MASK_SET(req->options, Z_Options_sort);
168 ODR_MASK_SET(req->options, Z_Options_extendedServices);
169 ODR_MASK_SET(req->options, Z_Options_delSet);
171 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_1);
172 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_2);
173 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_3);
175 *req->maximumRecordSize = 1024*1024;
176 *req->preferredMessageSize = 1024*1024;
178 req->idAuthentication = auth;
180 req->referenceId = set_refid (out);
183 yaz_oi_set_string_oidval(&req->otherInfo, out, VAL_PROXY,
187 printf("Sent initrequest.\n");
190 static int process_initResponse(Z_InitResponse *res)
192 /* save session parameters for later use */
193 session_mem = odr_extract_mem(in);
197 printf("Connection rejected by target.\n");
199 printf("Connection accepted by target.\n");
200 if (res->implementationId)
201 printf("ID : %s\n", res->implementationId);
202 if (res->implementationName)
203 printf("Name : %s\n", res->implementationName);
204 if (res->implementationVersion)
205 printf("Version: %s\n", res->implementationVersion);
206 if (res->userInformationField)
208 printf("UserInformationfield:\n");
209 if (!z_External(print, (Z_External**)&res-> userInformationField,
212 odr_perror(print, "Printing userinfo\n");
215 if (res->userInformationField->which == Z_External_octet)
217 printf("Guessing visiblestring:\n");
218 printf("'%s'\n", res->userInformationField->u. octet_aligned->buf);
223 if (ODR_MASK_GET(res->options, Z_Options_search))
225 if (ODR_MASK_GET(res->options, Z_Options_present))
227 if (ODR_MASK_GET(res->options, Z_Options_delSet))
229 if (ODR_MASK_GET(res->options, Z_Options_resourceReport))
230 printf (" resourceReport");
231 if (ODR_MASK_GET(res->options, Z_Options_resourceCtrl))
232 printf (" resourceCtrl");
233 if (ODR_MASK_GET(res->options, Z_Options_accessCtrl))
234 printf (" accessCtrl");
235 if (ODR_MASK_GET(res->options, Z_Options_scan))
237 if (ODR_MASK_GET(res->options, Z_Options_sort))
239 if (ODR_MASK_GET(res->options, Z_Options_extendedServices))
240 printf (" extendedServices");
241 if (ODR_MASK_GET(res->options, Z_Options_level_1Segmentation))
242 printf (" level1Segmentation");
243 if (ODR_MASK_GET(res->options, Z_Options_level_2Segmentation))
244 printf (" level2Segmentation");
245 if (ODR_MASK_GET(res->options, Z_Options_concurrentOperations))
246 printf (" concurrentOperations");
247 if (ODR_MASK_GET(res->options, Z_Options_namedResultSets))
249 printf (" namedResultSets");
257 static int cmd_base(char *arg)
264 printf("Usage: base <database> <database> ...\n");
267 for (i = 0; i<num_databaseNames; i++)
268 xfree (databaseNames[i]);
269 num_databaseNames = 0;
272 if (!(cp = strchr(arg, ' ')))
273 cp = arg + strlen(arg);
276 databaseNames[num_databaseNames] = (char *)xmalloc (1 + cp - arg);
277 memcpy (databaseNames[num_databaseNames], arg, cp - arg);
278 databaseNames[num_databaseNames++][cp - arg] = '\0';
287 int cmd_open(char *arg)
290 char type_and_host[101], base[101];
295 printf("Already connected.\n");
301 nmem_destroy (session_mem);
307 if (sscanf (arg, "%100[^/]/%100s", type_and_host, base) < 1)
311 conn = cs_create_host(yazProxy, 1, &add);
313 conn = cs_create_host(type_and_host, 1, &add);
317 printf ("Couldn't create comstack\n");
320 printf("Connecting...");
322 if (cs_connect(conn, add) < 0)
324 printf ("error = %s\n", cs_strerror(conn));
325 if (conn->cerrno == CSYSERR)
333 send_initRequest(type_and_host);
339 int cmd_authentication(char *arg)
341 static Z_IdAuthentication au;
342 static char open[256];
346 printf("Auth field set to null\n");
351 au.which = Z_IdAuthentication_open;
357 /* SEARCH SERVICE ------------------------------ */
359 static void display_variant(Z_Variant *v, int level)
363 for (i = 0; i < v->num_triples; i++)
365 printf("%*sclass=%d,type=%d", level * 4, "", *v->triples[i]->zclass,
366 *v->triples[i]->type);
367 if (v->triples[i]->which == Z_Triple_internationalString)
368 printf(",value=%s\n", v->triples[i]->value.internationalString);
374 static void display_grs1(Z_GenericRecord *r, int level)
380 for (i = 0; i < r->num_elements; i++)
384 printf("%*s", level * 4, "");
388 printf("%d,", *t->tagType);
391 if (t->tagValue->which == Z_StringOrNumeric_numeric)
392 printf("%d) ", *t->tagValue->u.numeric);
394 printf("%s) ", t->tagValue->u.string);
395 if (t->content->which == Z_ElementData_subtree)
398 display_grs1(t->content->u.subtree, level+1);
400 else if (t->content->which == Z_ElementData_string)
401 printf("%s\n", t->content->u.string);
402 else if (t->content->which == Z_ElementData_numeric)
403 printf("%d\n", *t->content->u.numeric);
404 else if (t->content->which == Z_ElementData_oid)
406 int *ip = t->content->u.oid;
409 if ((oent = oid_getentbyoid(t->content->u.oid)))
410 printf("OID: %s\n", oent->desc);
414 while (ip && *ip >= 0)
415 printf(" %d", *(ip++));
419 else if (t->content->which == Z_ElementData_noDataRequested)
420 printf("[No data requested]\n");
421 else if (t->content->which == Z_ElementData_elementEmpty)
422 printf("[Element empty]\n");
423 else if (t->content->which == Z_ElementData_elementNotThere)
424 printf("[Element not there]\n");
427 if (t->appliedVariant)
428 display_variant(t->appliedVariant, level+1);
429 if (t->metaData && t->metaData->supportedVariants)
433 printf("%*s---- variant list\n", (level+1)*4, "");
434 for (c = 0; c < t->metaData->num_supportedVariants; c++)
436 printf("%*svariant #%d\n", (level+1)*4, "", c);
437 display_variant(t->metaData->supportedVariants[c], level + 2);
444 static void print_record(const unsigned char *buf, size_t len)
447 print_stringn (buf, len);
448 /* add newline if not already added ... */
449 if (i <= 0 || buf[i-1] != '\n')
450 fputc ('\n', stdout);
453 static void display_record(Z_External *r)
455 oident *ent = oid_getentbyoid(r->direct_reference);
459 * Tell the user what we got.
461 if (r->direct_reference)
463 printf("Record type: ");
465 printf("%s\n", ent->desc);
466 else if (!odr_oid(print, &r->direct_reference, 0, 0))
468 odr_perror(print, "print oid");
472 /* Check if this is a known, ASN.1 type tucked away in an octet string */
473 if (ent && r->which == Z_External_octet)
475 Z_ext_typeent *type = z_ext_getentbyref(ent->value);
481 * Call the given decoder to process the record.
483 odr_setbuf(in, (char*)r->u.octet_aligned->buf,
484 r->u.octet_aligned->len, 0);
485 if (!(*type->fun)(in, (char **)&rr, 0, 0))
487 odr_perror(in, "Decoding constructed record.");
488 fprintf(stderr, "[Near %d]\n", odr_offset(in));
489 fprintf(stderr, "Packet dump:\n---------\n");
490 odr_dumpBER(stderr, (char*)r->u.octet_aligned->buf,
491 r->u.octet_aligned->len);
492 fprintf(stderr, "---------\n");
496 * Note: we throw away the original, BER-encoded record here.
497 * Do something else with it if you want to keep it.
499 r->u.sutrs = (Z_SUTRS *) rr; /* we don't actually check the type here. */
500 r->which = type->what;
503 if (ent && ent->value == VAL_SOIF)
504 print_record((const unsigned char *) r->u.octet_aligned->buf,
505 r->u.octet_aligned->len);
506 else if (r->which == Z_External_octet && r->u.octet_aligned->len)
508 const char *octet_buf = (char*)r->u.octet_aligned->buf;
509 if (ent->value == VAL_TEXT_XML || ent->value == VAL_APPLICATION_XML ||
510 ent->value == VAL_HTML)
511 print_record((const unsigned char *) octet_buf,
512 r->u.octet_aligned->len);
516 #if AVOID_MARC_DECODE
517 /* primitive check for a marc OID 5.1-29 */
518 ent->oidsuffix[0] == 5 && ent->oidsuffix[1] < 30
524 if (marc_display_exl (octet_buf, NULL, 0 /* debug */,
525 r->u.octet_aligned->len) <= 0)
527 printf ("bad MARC. Dumping as it is:\n");
528 print_record((const unsigned char*) octet_buf,
529 r->u.octet_aligned->len);
534 print_record((const unsigned char*) octet_buf,
535 r->u.octet_aligned->len);
539 fwrite (octet_buf, 1, r->u.octet_aligned->len, marcdump);
541 else if (ent && ent->value == VAL_SUTRS)
543 if (r->which != Z_External_sutrs)
545 printf("Expecting single SUTRS type for SUTRS.\n");
548 print_record(r->u.sutrs->buf, r->u.sutrs->len);
550 else if (ent && ent->value == VAL_GRS1)
552 if (r->which != Z_External_grs1)
554 printf("Expecting single GRS type for GRS.\n");
557 display_grs1(r->u.grs1, 0);
561 printf("Unknown record representation.\n");
562 if (!z_External(print, &r, 0, 0))
564 odr_perror(print, "Printing external");
571 static void display_diagrecs(Z_DiagRec **pp, int num)
575 Z_DefaultDiagFormat *r;
577 printf("Diagnostic message(s) from database:\n");
578 for (i = 0; i<num; i++)
580 Z_DiagRec *p = pp[i];
581 if (p->which != Z_DiagRec_defaultFormat)
583 printf("Diagnostic record not in default format.\n");
587 r = p->u.defaultFormat;
588 if (!(ent = oid_getentbyoid(r->diagnosticSetId)) ||
589 ent->oclass != CLASS_DIAGSET || ent->value != VAL_BIB1)
590 printf("Missing or unknown diagset\n");
591 printf(" [%d] %s", *r->condition, diagbib1_str(*r->condition));
594 case Z_DefaultDiagFormat_v2Addinfo:
595 printf (" -- v2 addinfo '%s'\n", r->u.v2Addinfo);
597 case Z_DefaultDiagFormat_v3Addinfo:
598 printf (" -- v3 addinfo '%s'\n", r->u.v3Addinfo);
605 static void display_nameplusrecord(Z_NamePlusRecord *p)
608 printf("[%s]", p->databaseName);
609 if (p->which == Z_NamePlusRecord_surrogateDiagnostic)
610 display_diagrecs(&p->u.surrogateDiagnostic, 1);
611 else if (p->which == Z_NamePlusRecord_databaseRecord)
612 display_record(p->u.databaseRecord);
615 static void display_records(Z_Records *p)
619 if (p->which == Z_Records_NSD)
621 Z_DiagRec dr, *dr_p = &dr;
622 dr.which = Z_DiagRec_defaultFormat;
623 dr.u.defaultFormat = p->u.nonSurrogateDiagnostic;
624 display_diagrecs (&dr_p, 1);
626 else if (p->which == Z_Records_multipleNSD)
627 display_diagrecs (p->u.multipleNonSurDiagnostics->diagRecs,
628 p->u.multipleNonSurDiagnostics->num_diagRecs);
631 printf("Records: %d\n", p->u.databaseOrSurDiagnostics->num_records);
632 for (i = 0; i < p->u.databaseOrSurDiagnostics->num_records; i++)
633 display_nameplusrecord(p->u.databaseOrSurDiagnostics->records[i]);
637 static int send_deleteResultSetRequest(char *arg)
642 Z_APDU *apdu = zget_APDU(out, Z_APDU_deleteResultSetRequest);
643 Z_DeleteResultSetRequest *req = apdu->u.deleteResultSetRequest;
645 req->referenceId = set_refid (out);
647 req->num_resultSetList =
648 sscanf (arg, "%30s %30s %30s %30s %30s %30s %30s %30s",
649 names[0], names[1], names[2], names[3],
650 names[4], names[5], names[6], names[7]);
652 req->deleteFunction = (int *)
653 odr_malloc (out, sizeof(*req->deleteFunction));
654 if (req->num_resultSetList > 0)
656 *req->deleteFunction = Z_DeleteRequest_list;
657 req->resultSetList = (char **)
658 odr_malloc (out, sizeof(*req->resultSetList)*
659 req->num_resultSetList);
660 for (i = 0; i<req->num_resultSetList; i++)
661 req->resultSetList[i] = names[i];
665 *req->deleteFunction = Z_DeleteRequest_all;
666 req->resultSetList = 0;
670 printf("Sent deleteResultSetRequest.\n");
674 static int send_searchRequest(char *arg)
676 Z_APDU *apdu = zget_APDU(out, Z_APDU_searchRequest);
677 Z_SearchRequest *req = apdu->u.searchRequest;
681 struct ccl_rpn_node *rpn = NULL;
685 Z_RPNQuery *RPNquery;
689 if (queryType == QueryType_CCL2RPN)
691 rpn = ccl_find_str(bibset, arg, &error, &pos);
694 printf("CCL ERROR: %s\n", ccl_err_msg(error));
699 req->referenceId = set_refid (out);
700 if (!strcmp(arg, "@big")) /* strictly for troublemaking */
702 static unsigned char big[2100];
705 /* send a very big referenceid to test transport stack etc. */
706 memset(big, 'A', 2100);
707 bigo.len = bigo.size = 2100;
709 req->referenceId = &bigo;
714 sprintf(setstring, "%d", ++setnumber);
715 req->resultSetName = setstring;
717 *req->smallSetUpperBound = smallSetUpperBound;
718 *req->largeSetLowerBound = largeSetLowerBound;
719 *req->mediumSetPresentNumber = mediumSetPresentNumber;
720 if (smallSetUpperBound > 0 || (largeSetLowerBound > 1 &&
721 mediumSetPresentNumber > 0))
725 prefsyn.proto = protocol;
726 prefsyn.oclass = CLASS_RECSYN;
727 prefsyn.value = recordsyntax;
728 req->preferredRecordSyntax =
729 odr_oiddup(out, oid_ent_to_oid(&prefsyn, oid));
730 req->smallSetElementSetNames =
731 req->mediumSetElementSetNames = elementSetNames;
733 req->num_databaseNames = num_databaseNames;
734 req->databaseNames = databaseNames;
740 case QueryType_Prefix:
741 query.which = Z_Query_type_1;
742 RPNquery = p_query_rpn (out, protocol, arg);
745 printf("Prefix query error\n");
748 query.u.type_1 = RPNquery;
751 query.which = Z_Query_type_2;
752 query.u.type_2 = &ccl_query;
753 ccl_query.buf = (unsigned char*) arg;
754 ccl_query.len = strlen(arg);
757 case QueryType_CCL2RPN:
758 query.which = Z_Query_type_1;
759 RPNquery = ccl_rpn_query(out, rpn);
762 printf ("Couldn't convert from CCL to RPN\n");
765 query.u.type_1 = RPNquery;
766 ccl_rpn_delete (rpn);
770 printf ("Unsupported query type\n");
775 printf("Sent searchRequest.\n");
779 static int process_searchResponse(Z_SearchResponse *res)
781 printf ("Received SearchResponse.\n");
782 print_refid (res->referenceId);
783 if (*res->searchStatus)
784 printf("Search was a success.\n");
786 printf("Search was a bloomin' failure.\n");
787 printf("Number of hits: %d", *res->resultCount);
789 printf (", setno %d", setnumber);
790 printf("\nrecords returned: %d\n",
791 *res->numberOfRecordsReturned);
792 setno += *res->numberOfRecordsReturned;
794 display_records(res->records);
798 static void print_level(int iLevel)
801 for (i = 0; i < iLevel * 4; i++)
805 static void print_int(int iLevel, const char *pTag, int *pInt)
810 printf("%s: %d\n", pTag, *pInt);
814 static void print_string(int iLevel, const char *pTag, const char *pString)
819 printf("%s: %s\n", pTag, pString);
823 static void print_oid(int iLevel, const char *pTag, Odr_oid *pOid)
831 for (; *pInt != -1; pInt++)
832 printf(" %d", *pInt);
837 static void print_referenceId(int iLevel, Z_ReferenceId *referenceId)
839 if (referenceId != NULL)
844 printf("Ref Id (%d, %d): ", referenceId->len, referenceId->size);
845 for (i = 0; i < referenceId->len; i++)
846 printf("%c", referenceId->buf[i]);
851 static void print_string_or_numeric(int iLevel, const char *pTag, Z_StringOrNumeric *pStringNumeric)
853 if (pStringNumeric != NULL)
855 switch (pStringNumeric->which)
857 case Z_StringOrNumeric_string:
858 print_string(iLevel, pTag, pStringNumeric->u.string);
861 case Z_StringOrNumeric_numeric:
862 print_int(iLevel, pTag, pStringNumeric->u.numeric);
867 printf("%s: valid type for Z_StringOrNumeric\n", pTag);
873 static void print_universe_report_duplicate(int iLevel, Z_UniverseReportDuplicate *pUniverseReportDuplicate)
875 if (pUniverseReportDuplicate != NULL)
878 printf("Universe Report Duplicate: \n");
880 print_string_or_numeric(iLevel, "Hit No", pUniverseReportDuplicate->hitno);
884 static void print_universe_report_hits(int iLevel, Z_UniverseReportHits *pUniverseReportHits)
886 if (pUniverseReportHits != NULL)
889 printf("Universe Report Hits: \n");
891 print_string_or_numeric(iLevel, "Database", pUniverseReportHits->database);
892 print_string_or_numeric(iLevel, "Hits", pUniverseReportHits->hits);
896 static void print_universe_report(int iLevel, Z_UniverseReport *pUniverseReport)
898 if (pUniverseReport != NULL)
901 printf("Universe Report: \n");
903 print_int(iLevel, "Total Hits", pUniverseReport->totalHits);
904 switch (pUniverseReport->which)
906 case Z_UniverseReport_databaseHits:
907 print_universe_report_hits(iLevel, pUniverseReport->u.databaseHits);
910 case Z_UniverseReport_duplicate:
911 print_universe_report_duplicate(iLevel, pUniverseReport->u.duplicate);
916 printf("Type: %d\n", pUniverseReport->which);
922 static void print_external(int iLevel, Z_External *pExternal)
924 if (pExternal != NULL)
927 printf("External: \n");
929 print_oid(iLevel, "Direct Reference", pExternal->direct_reference);
930 print_int(iLevel, "InDirect Reference", pExternal->indirect_reference);
931 print_string(iLevel, "Descriptor", pExternal->descriptor);
932 switch (pExternal->which)
934 case Z_External_universeReport:
935 print_universe_report(iLevel, pExternal->u.universeReport);
940 printf("Type: %d\n", pExternal->which);
946 static int process_resourceControlRequest (Z_ResourceControlRequest *req)
948 printf ("Received ResourceControlRequest.\n");
949 print_referenceId(1, req->referenceId);
950 print_int(1, "Suspended Flag", req->suspendedFlag);
951 print_int(1, "Partial Results Available", req->partialResultsAvailable);
952 print_int(1, "Response Required", req->responseRequired);
953 print_int(1, "Triggered Request Flag", req->triggeredRequestFlag);
954 print_external(1, req->resourceReport);
958 void process_ESResponse(Z_ExtendedServicesResponse *res)
961 switch (*res->operationStatus)
963 case Z_ExtendedServicesResponse_done:
966 case Z_ExtendedServicesResponse_accepted:
967 printf ("accepted\n");
969 case Z_ExtendedServicesResponse_failure:
970 printf ("failure\n");
971 display_diagrecs(res->diagnostics, res->num_diagnostics);
974 printf ("unknown\n");
976 if ( (*res->operationStatus != Z_ExtendedServicesResponse_failure) &&
977 (res->num_diagnostics != 0) ) {
978 display_diagrecs(res->diagnostics, res->num_diagnostics);
980 print_refid (res->referenceId);
981 if (res->taskPackage &&
982 res->taskPackage->which == Z_External_extendedService)
984 Z_TaskPackage *taskPackage = res->taskPackage->u.extendedService;
985 Odr_oct *id = taskPackage->targetReference;
986 Z_External *ext = taskPackage->taskSpecificParameters;
990 printf ("Target Reference: ");
991 print_stringn (id->buf, id->len);
994 if (ext->which == Z_External_update)
996 Z_IUUpdateTaskPackage *utp = ext->u.update->u.taskPackage;
997 if (utp && utp->targetPart)
999 Z_IUTargetPart *targetPart = utp->targetPart;
1002 for (i = 0; i<targetPart->num_taskPackageRecords; i++)
1005 Z_IUTaskPackageRecordStructure *tpr =
1006 targetPart->taskPackageRecords[i];
1007 printf ("task package record %d\n", i+1);
1008 if (tpr->which == Z_IUTaskPackageRecordStructure_record)
1010 display_record (tpr->u.record);
1014 printf ("other type\n");
1024 const char *get_ill_element (void *clientData, const char *element)
1029 static Z_External *create_external_itemRequest()
1031 struct ill_get_ctl ctl;
1032 ILL_ItemRequest *req;
1034 int item_request_size = 0;
1035 char *item_request_buf = 0;
1039 ctl.f = get_ill_element;
1041 req = ill_get_ItemRequest(&ctl, "ill", 0);
1043 printf ("ill_get_ItemRequest failed\n");
1045 if (!ill_ItemRequest (out, &req, 0, 0))
1049 ill_ItemRequest(print, &req, 0, 0);
1052 item_request_buf = odr_getbuf (out, &item_request_size, 0);
1053 if (item_request_buf)
1054 odr_setbuf (out, item_request_buf, item_request_size, 1);
1055 printf ("Couldn't encode ItemRequest, size %d\n", item_request_size);
1062 item_request_buf = odr_getbuf (out, &item_request_size, 0);
1063 oid.proto = PROTO_GENERAL;
1064 oid.oclass = CLASS_GENERAL;
1065 oid.value = VAL_ISO_ILL_1;
1067 r = (Z_External *) odr_malloc (out, sizeof(*r));
1068 r->direct_reference = odr_oiddup(out,oid_getoidbyent(&oid));
1069 r->indirect_reference = 0;
1071 r->which = Z_External_single;
1073 r->u.single_ASN1_type = (Odr_oct *)
1074 odr_malloc (out, sizeof(*r->u.single_ASN1_type));
1075 r->u.single_ASN1_type->buf = (unsigned char *)
1076 odr_malloc (out, item_request_size);
1077 r->u.single_ASN1_type->len = item_request_size;
1078 r->u.single_ASN1_type->size = item_request_size;
1079 memcpy (r->u.single_ASN1_type->buf, item_request_buf,
1081 printf ("len = %d\n", item_request_size);
1087 #ifdef YAZ_MODULE_ill
1088 static Z_External *create_external_ILL_APDU(int which)
1090 struct ill_get_ctl ctl;
1093 int ill_request_size = 0;
1094 char *ill_request_buf = 0;
1098 ctl.f = get_ill_element;
1100 ill_apdu = ill_get_APDU(&ctl, "ill", 0);
1102 if (!ill_APDU (out, &ill_apdu, 0, 0))
1106 printf ("-------------------\n");
1107 ill_APDU(print, &ill_apdu, 0, 0);
1109 printf ("-------------------\n");
1111 ill_request_buf = odr_getbuf (out, &ill_request_size, 0);
1112 if (ill_request_buf)
1113 odr_setbuf (out, ill_request_buf, ill_request_size, 1);
1114 printf ("Couldn't encode ILL-Request, size %d\n", ill_request_size);
1120 ill_request_buf = odr_getbuf (out, &ill_request_size, 0);
1122 oid.proto = PROTO_GENERAL;
1123 oid.oclass = CLASS_GENERAL;
1124 oid.value = VAL_ISO_ILL_1;
1126 r = (Z_External *) odr_malloc (out, sizeof(*r));
1127 r->direct_reference = odr_oiddup(out,oid_getoidbyent(&oid));
1128 r->indirect_reference = 0;
1130 r->which = Z_External_single;
1132 r->u.single_ASN1_type = (Odr_oct *)
1133 odr_malloc (out, sizeof(*r->u.single_ASN1_type));
1134 r->u.single_ASN1_type->buf = (unsigned char *)
1135 odr_malloc (out, ill_request_size);
1136 r->u.single_ASN1_type->len = ill_request_size;
1137 r->u.single_ASN1_type->size = ill_request_size;
1138 memcpy (r->u.single_ASN1_type->buf, ill_request_buf, ill_request_size);
1139 printf ("len = %d\n", ill_request_size);
1146 static Z_External *create_ItemOrderExternal(const char *type, int itemno)
1148 Z_External *r = (Z_External *) odr_malloc(out, sizeof(Z_External));
1149 oident ItemOrderRequest;
1151 ItemOrderRequest.proto = PROTO_Z3950;
1152 ItemOrderRequest.oclass = CLASS_EXTSERV;
1153 ItemOrderRequest.value = VAL_ITEMORDER;
1155 r->direct_reference = odr_oiddup(out,oid_getoidbyent(&ItemOrderRequest));
1156 r->indirect_reference = 0;
1159 r->which = Z_External_itemOrder;
1161 r->u.itemOrder = (Z_ItemOrder *) odr_malloc(out,sizeof(Z_ItemOrder));
1162 memset(r->u.itemOrder, 0, sizeof(Z_ItemOrder));
1163 r->u.itemOrder->which=Z_IOItemOrder_esRequest;
1165 r->u.itemOrder->u.esRequest = (Z_IORequest *)
1166 odr_malloc(out,sizeof(Z_IORequest));
1167 memset(r->u.itemOrder->u.esRequest, 0, sizeof(Z_IORequest));
1169 r->u.itemOrder->u.esRequest->toKeep = (Z_IOOriginPartToKeep *)
1170 odr_malloc(out,sizeof(Z_IOOriginPartToKeep));
1171 memset(r->u.itemOrder->u.esRequest->toKeep, 0, sizeof(Z_IOOriginPartToKeep));
1172 r->u.itemOrder->u.esRequest->notToKeep = (Z_IOOriginPartNotToKeep *)
1173 odr_malloc(out,sizeof(Z_IOOriginPartNotToKeep));
1174 memset(r->u.itemOrder->u.esRequest->notToKeep, 0, sizeof(Z_IOOriginPartNotToKeep));
1176 r->u.itemOrder->u.esRequest->toKeep->supplDescription = NULL;
1177 r->u.itemOrder->u.esRequest->toKeep->contact = NULL;
1178 r->u.itemOrder->u.esRequest->toKeep->addlBilling = NULL;
1180 r->u.itemOrder->u.esRequest->notToKeep->resultSetItem =
1181 (Z_IOResultSetItem *) odr_malloc(out, sizeof(Z_IOResultSetItem));
1182 memset(r->u.itemOrder->u.esRequest->notToKeep->resultSetItem, 0, sizeof(Z_IOResultSetItem));
1183 r->u.itemOrder->u.esRequest->notToKeep->resultSetItem->resultSetId = "1";
1185 r->u.itemOrder->u.esRequest->notToKeep->resultSetItem->item =
1186 (int *) odr_malloc(out, sizeof(int));
1187 *r->u.itemOrder->u.esRequest->notToKeep->resultSetItem->item = itemno;
1190 if (!strcmp (type, "item") || !strcmp(type, "2"))
1192 printf ("using item-request\n");
1193 r->u.itemOrder->u.esRequest->notToKeep->itemRequest =
1194 create_external_itemRequest();
1196 else if (!strcmp(type, "ill") || !strcmp(type, "1"))
1198 printf ("using ILL-request\n");
1199 r->u.itemOrder->u.esRequest->notToKeep->itemRequest =
1200 create_external_ILL_APDU(ILL_APDU_ILL_Request);
1202 else if (!strcmp(type, "xml") || !strcmp(type, "3"))
1204 const char *xml_buf =
1206 " <type>request</type>\n"
1207 " <libraryNo>000200</libraryNo>\n"
1208 " <borrowerTicketNo> 1212 </borrowerTicketNo>\n"
1210 r->u.itemOrder->u.esRequest->notToKeep->itemRequest =
1211 z_ext_record (out, VAL_TEXT_XML, xml_buf, strlen(xml_buf));
1214 r->u.itemOrder->u.esRequest->notToKeep->itemRequest = 0;
1217 r->u.itemOrder->u.esRequest->notToKeep->itemRequest = 0;
1222 static int send_itemorder(const char *type, int itemno)
1224 Z_APDU *apdu = zget_APDU(out, Z_APDU_extendedServicesRequest);
1225 Z_ExtendedServicesRequest *req = apdu->u.extendedServicesRequest;
1226 oident ItemOrderRequest;
1228 ItemOrderRequest.proto = PROTO_Z3950;
1229 ItemOrderRequest.oclass = CLASS_EXTSERV;
1230 ItemOrderRequest.value = VAL_ITEMORDER;
1231 req->packageType = odr_oiddup(out,oid_getoidbyent(&ItemOrderRequest));
1232 req->packageName = esPackageName;
1234 req->taskSpecificParameters = create_ItemOrderExternal(type, itemno);
1240 static int cmd_update(char *arg)
1242 Z_APDU *apdu = zget_APDU(out, Z_APDU_extendedServicesRequest );
1243 Z_ExtendedServicesRequest *req = apdu->u.extendedServicesRequest;
1246 Z_IUOriginPartToKeep *toKeep;
1247 Z_IUSuppliedRecords *notToKeep;
1249 printf ("Update request\n");
1254 update_oid.proto = PROTO_Z3950;
1255 update_oid.oclass = CLASS_EXTSERV;
1256 update_oid.value = VAL_DBUPDATE;
1257 oid_ent_to_oid (&update_oid, oid);
1258 req->packageType = odr_oiddup(out,oid);
1259 req->packageName = esPackageName;
1261 req->referenceId = set_refid (out);
1263 r = req->taskSpecificParameters = (Z_External *)
1264 odr_malloc (out, sizeof(*r));
1265 r->direct_reference = odr_oiddup(out,oid);
1266 r->indirect_reference = 0;
1268 r->which = Z_External_update;
1269 r->u.update = (Z_IUUpdate *) odr_malloc(out, sizeof(*r->u.update));
1270 r->u.update->which = Z_IUUpdate_esRequest;
1271 r->u.update->u.esRequest = (Z_IUUpdateEsRequest *)
1272 odr_malloc(out, sizeof(*r->u.update->u.esRequest));
1273 toKeep = r->u.update->u.esRequest->toKeep = (Z_IUOriginPartToKeep *)
1274 odr_malloc(out, sizeof(*r->u.update->u.esRequest->toKeep));
1275 toKeep->databaseName = databaseNames[0];
1277 toKeep->elementSetName = 0;
1278 toKeep->actionQualifier = 0;
1279 toKeep->action = (int *) odr_malloc(out, sizeof(*toKeep->action));
1280 *toKeep->action = Z_IUOriginPartToKeep_recordInsert;
1282 notToKeep = r->u.update->u.esRequest->notToKeep = (Z_IUSuppliedRecords *)
1283 odr_malloc(out, sizeof(*r->u.update->u.esRequest->notToKeep));
1285 notToKeep->elements = (Z_IUSuppliedRecords_elem **)
1286 odr_malloc(out, sizeof(*notToKeep->elements));
1287 notToKeep->elements[0] = (Z_IUSuppliedRecords_elem *)
1288 odr_malloc(out, sizeof(**notToKeep->elements));
1289 notToKeep->elements[0]->u.number = 0;
1290 notToKeep->elements[0]->supplementalId = 0;
1291 notToKeep->elements[0]->correlationInfo = 0;
1292 notToKeep->elements[0]->record = record_last;
1299 /* II : Added to do DALI Item Order Extended services request */
1300 static int cmd_itemorder(char *arg)
1305 if (sscanf (arg, "%10s %d", type, &itemno) != 2)
1308 printf("Item order request\n");
1310 send_itemorder(type, itemno);
1314 static int cmd_find(char *arg)
1318 printf("Find what?\n");
1323 printf("Not connected yet\n");
1326 if (!send_searchRequest(arg))
1331 static int cmd_delete(char *arg)
1335 printf("Not connected yet\n");
1338 if (!send_deleteResultSetRequest(arg))
1343 static int cmd_ssub(char *arg)
1345 if (!(smallSetUpperBound = atoi(arg)))
1350 static int cmd_lslb(char *arg)
1352 if (!(largeSetLowerBound = atoi(arg)))
1357 static int cmd_mspn(char *arg)
1359 if (!(mediumSetPresentNumber = atoi(arg)))
1364 static int cmd_status(char *arg)
1366 printf("smallSetUpperBound: %d\n", smallSetUpperBound);
1367 printf("largeSetLowerBound: %d\n", largeSetLowerBound);
1368 printf("mediumSetPresentNumber: %d\n", mediumSetPresentNumber);
1372 static int cmd_setnames(char *arg)
1374 if (*arg == '1') /* enable ? */
1376 else if (*arg == '0') /* disable ? */
1378 else if (setnumber < 0) /* no args, toggle .. */
1384 printf("Set numbering enabled.\n");
1386 printf("Set numbering disabled.\n");
1390 /* PRESENT SERVICE ----------------------------- */
1392 static int send_presentRequest(char *arg)
1394 Z_APDU *apdu = zget_APDU(out, Z_APDU_presentRequest);
1395 Z_PresentRequest *req = apdu->u.presentRequest;
1396 Z_RecordComposition compo;
1401 char setstring[100];
1403 req->referenceId = set_refid (out);
1404 if ((p = strchr(arg, '+')))
1411 if (p && (p=strchr(p+1, '+')))
1413 strcpy (setstring, p+1);
1414 req->resultSetId = setstring;
1416 else if (setnumber >= 0)
1418 sprintf(setstring, "%d", setnumber);
1419 req->resultSetId = setstring;
1424 static Z_Range range;
1425 static Z_Range *rangep = ⦥
1426 req->num_ranges = 1;
1428 req->resultSetStartPoint = &setno;
1429 req->numberOfRecordsRequested = &nos;
1430 prefsyn.proto = protocol;
1431 prefsyn.oclass = CLASS_RECSYN;
1432 prefsyn.value = recordsyntax;
1433 req->preferredRecordSyntax =
1434 odr_oiddup (out, oid_ent_to_oid(&prefsyn, oid));
1436 if (schema != VAL_NONE)
1440 prefschema.proto = protocol;
1441 prefschema.oclass = CLASS_SCHEMA;
1442 prefschema.value = schema;
1444 req->recordComposition = &compo;
1445 compo.which = Z_RecordComp_complex;
1446 compo.u.complex = (Z_CompSpec *)
1447 odr_malloc(out, sizeof(*compo.u.complex));
1448 compo.u.complex->selectAlternativeSyntax = (bool_t *)
1449 odr_malloc(out, sizeof(bool_t));
1450 *compo.u.complex->selectAlternativeSyntax = 0;
1452 compo.u.complex->generic = (Z_Specification *)
1453 odr_malloc(out, sizeof(*compo.u.complex->generic));
1454 compo.u.complex->generic->schema = (Odr_oid *)
1455 odr_oiddup(out, oid_ent_to_oid(&prefschema, oid));
1456 if (!compo.u.complex->generic->schema)
1458 /* OID wasn't a schema! Try record syntax instead. */
1459 prefschema.oclass = CLASS_RECSYN;
1460 compo.u.complex->generic->schema = (Odr_oid *)
1461 odr_oiddup(out, oid_ent_to_oid(&prefschema, oid));
1463 if (!elementSetNames)
1464 compo.u.complex->generic->elementSpec = 0;
1467 compo.u.complex->generic->elementSpec = (Z_ElementSpec *)
1468 odr_malloc(out, sizeof(Z_ElementSpec));
1469 compo.u.complex->generic->elementSpec->which =
1470 Z_ElementSpec_elementSetName;
1471 compo.u.complex->generic->elementSpec->u.elementSetName =
1472 elementSetNames->u.generic;
1474 compo.u.complex->num_dbSpecific = 0;
1475 compo.u.complex->dbSpecific = 0;
1476 compo.u.complex->num_recordSyntax = 0;
1477 compo.u.complex->recordSyntax = 0;
1479 else if (elementSetNames)
1481 req->recordComposition = &compo;
1482 compo.which = Z_RecordComp_simple;
1483 compo.u.simple = elementSetNames;
1486 printf("Sent presentRequest (%d+%d).\n", setno, nos);
1490 void process_close(Z_Close *req)
1492 Z_APDU *apdu = zget_APDU(out, Z_APDU_close);
1493 Z_Close *res = apdu->u.close;
1495 static char *reasons[] =
1500 "cost limit reached",
1502 "security violation",
1509 printf("Reason: %s, message: %s\n", reasons[*req->closeReason],
1510 req->diagnosticInformation ? req->diagnosticInformation : "NULL");
1517 nmem_destroy (session_mem);
1524 *res->closeReason = Z_Close_finished;
1526 printf("Sent response.\n");
1531 static int cmd_show(char *arg)
1535 printf("Not connected yet\n");
1538 if (!send_presentRequest(arg))
1543 int cmd_quit(char *arg)
1545 printf("See you later, alligator.\n");
1551 int cmd_cancel(char *arg)
1553 Z_APDU *apdu = zget_APDU(out, Z_APDU_triggerResourceControlRequest);
1554 Z_TriggerResourceControlRequest *req =
1555 apdu->u.triggerResourceControlRequest;
1560 printf("Session not initialized yet\n");
1563 if (!ODR_MASK_GET(session->options, Z_Options_triggerResourceCtrl))
1565 printf("Target doesn't support cancel (trigger resource ctrl)\n");
1568 *req->requestedAction = Z_TriggerResourceCtrl_cancel;
1569 req->resultSetWanted = &rfalse;
1572 printf("Sent cancel request\n");
1576 int send_scanrequest(const char *query, int pp, int num, const char *term)
1578 Z_APDU *apdu = zget_APDU(out, Z_APDU_scanRequest);
1579 Z_ScanRequest *req = apdu->u.scanRequest;
1584 if (queryType == QueryType_CCL2RPN)
1588 struct ccl_rpn_node *rpn;
1590 rpn = ccl_find_str (bibset, query, &error, &pos);
1593 printf("CCL ERROR: %s\n", ccl_err_msg(error));
1597 bib1.proto = PROTO_Z3950;
1598 bib1.oclass = CLASS_ATTSET;
1599 bib1.value = VAL_BIB1;
1600 req->attributeSet = oid_ent_to_oid (&bib1, oid);
1601 if (!(req->termListAndStartPoint = ccl_scan_query (out, rpn)))
1603 printf("Couldn't convert CCL to Scan term\n");
1606 ccl_rpn_delete (rpn);
1609 if (use_rpn && !(req->termListAndStartPoint =
1610 p_query_scan(out, protocol, &req->attributeSet, query)))
1612 printf("Prefix query error\n");
1617 if (req->termListAndStartPoint->term &&
1618 req->termListAndStartPoint->term->which == Z_Term_general &&
1619 req->termListAndStartPoint->term->u.general)
1621 req->termListAndStartPoint->term->u.general->buf =
1622 (unsigned char *) odr_strdup(out, term);
1623 req->termListAndStartPoint->term->u.general->len =
1624 req->termListAndStartPoint->term->u.general->size =
1628 req->referenceId = set_refid (out);
1629 req->num_databaseNames = num_databaseNames;
1630 req->databaseNames = databaseNames;
1631 req->numberOfTermsRequested = #
1632 req->preferredPositionInResponse = &pp;
1637 int send_sortrequest(char *arg, int newset)
1639 Z_APDU *apdu = zget_APDU(out, Z_APDU_sortRequest);
1640 Z_SortRequest *req = apdu->u.sortRequest;
1641 Z_SortKeySpecList *sksl = (Z_SortKeySpecList *)
1642 odr_malloc (out, sizeof(*sksl));
1646 sprintf (setstring, "%d", setnumber);
1648 sprintf (setstring, "default");
1650 req->referenceId = set_refid (out);
1652 req->num_inputResultSetNames = 1;
1653 req->inputResultSetNames = (Z_InternationalString **)
1654 odr_malloc (out, sizeof(*req->inputResultSetNames));
1655 req->inputResultSetNames[0] = odr_strdup (out, setstring);
1657 if (newset && setnumber >= 0)
1658 sprintf (setstring, "%d", ++setnumber);
1660 req->sortedResultSetName = odr_strdup (out, setstring);
1662 req->sortSequence = yaz_sort_spec (out, arg);
1663 if (!req->sortSequence)
1665 printf ("Missing sort specifications\n");
1672 void display_term(Z_TermInfo *t)
1674 if (t->term->which == Z_Term_general)
1676 printf("%.*s", t->term->u.general->len, t->term->u.general->buf);
1677 sprintf(last_scan_line, "%.*s", t->term->u.general->len,
1678 t->term->u.general->buf);
1681 printf("Term (not general)");
1682 if (t->globalOccurrences)
1683 printf (" (%d)\n", *t->globalOccurrences);
1688 void process_scanResponse(Z_ScanResponse *res)
1691 Z_Entry **entries = NULL;
1692 int num_entries = 0;
1694 printf("Received ScanResponse\n");
1695 print_refid (res->referenceId);
1696 printf("%d entries", *res->numberOfEntriesReturned);
1697 if (res->positionOfTerm)
1698 printf (", position=%d", *res->positionOfTerm);
1700 if (*res->scanStatus != Z_Scan_success)
1701 printf("Scan returned code %d\n", *res->scanStatus);
1704 if ((entries = res->entries->entries))
1705 num_entries = res->entries->num_entries;
1706 for (i = 0; i < num_entries; i++)
1708 int pos_term = res->positionOfTerm ? *res->positionOfTerm : -1;
1709 if (entries[i]->which == Z_Entry_termInfo)
1711 printf("%c ", i + 1 == pos_term ? '*' : ' ');
1712 display_term(entries[i]->u.termInfo);
1715 display_diagrecs(&entries[i]->u.surrogateDiagnostic, 1);
1717 if (res->entries->nonsurrogateDiagnostics)
1718 display_diagrecs (res->entries->nonsurrogateDiagnostics,
1719 res->entries->num_nonsurrogateDiagnostics);
1722 void process_sortResponse(Z_SortResponse *res)
1724 printf("Received SortResponse: status=");
1725 switch (*res->sortStatus)
1727 case Z_SortStatus_success:
1728 printf ("success"); break;
1729 case Z_SortStatus_partial_1:
1730 printf ("partial"); break;
1731 case Z_SortStatus_failure:
1732 printf ("failure"); break;
1734 printf ("unknown (%d)", *res->sortStatus);
1737 print_refid (res->referenceId);
1738 if (res->diagnostics)
1739 display_diagrecs(res->diagnostics,
1740 res->num_diagnostics);
1743 void process_deleteResultSetResponse (Z_DeleteResultSetResponse *res)
1745 printf("Got deleteResultSetResponse status=%d\n",
1746 *res->deleteOperationStatus);
1747 if (res->deleteListStatuses)
1750 for (i = 0; i < res->deleteListStatuses->num; i++)
1752 printf ("%s status=%d\n", res->deleteListStatuses->elements[i]->id,
1753 *res->deleteListStatuses->elements[i]->status);
1758 int cmd_sort_generic(char *arg, int newset)
1762 printf("Session not initialized yet\n");
1765 if (!ODR_MASK_GET(session->options, Z_Options_sort))
1767 printf("Target doesn't support sort\n");
1772 if (send_sortrequest(arg, newset) < 0)
1779 int cmd_sort(char *arg)
1781 return cmd_sort_generic (arg, 0);
1784 int cmd_sort_newset (char *arg)
1786 return cmd_sort_generic (arg, 1);
1789 int cmd_scan(char *arg)
1793 printf("Session not initialized yet\n");
1796 if (!ODR_MASK_GET(session->options, Z_Options_scan))
1798 printf("Target doesn't support scan\n");
1803 strcpy (last_scan_query, arg);
1804 if (send_scanrequest(arg, 1, 20, 0) < 0)
1809 if (send_scanrequest(last_scan_query, 1, 20, last_scan_line) < 0)
1815 int cmd_schema(char *arg)
1822 schema = oid_getvalbyname (arg);
1823 if (schema == VAL_NONE)
1825 printf ("unknown schema\n");
1831 int cmd_format(char *arg)
1835 printf("Usage: format <recordsyntax>\n");
1838 recordsyntax = oid_getvalbyname (arg);
1839 if (recordsyntax == VAL_NONE)
1841 printf ("unknown record syntax\n");
1847 int cmd_elements(char *arg)
1849 static Z_ElementSetNames esn;
1850 static char what[100];
1854 elementSetNames = 0;
1858 esn.which = Z_ElementSetNames_generic;
1859 esn.u.generic = what;
1860 elementSetNames = &esn;
1864 int cmd_attributeset(char *arg)
1870 printf("Usage: attributeset <setname>\n");
1873 sscanf(arg, "%s", what);
1874 if (p_query_attset (what))
1876 printf("Unknown attribute set name\n");
1882 int cmd_querytype (char *arg)
1884 if (!strcmp (arg, "ccl"))
1885 queryType = QueryType_CCL;
1886 else if (!strcmp (arg, "prefix") || !strcmp(arg, "rpn"))
1887 queryType = QueryType_Prefix;
1889 else if (!strcmp (arg, "ccl2rpn") || !strcmp (arg, "cclrpn"))
1890 queryType = QueryType_CCL2RPN;
1894 printf ("Querytype must be one of:\n");
1895 printf (" prefix - Prefix query\n");
1896 printf (" ccl - CCL query\n");
1898 printf (" ccl2rpn - CCL query converted to RPN\n");
1905 int cmd_refid (char *arg)
1911 refid = (char *) xmalloc (strlen(arg)+1);
1912 strcpy (refid, arg);
1917 int cmd_close(char *arg)
1924 apdu = zget_APDU(out, Z_APDU_close);
1925 req = apdu->u.close;
1926 *req->closeReason = Z_Close_finished;
1928 printf("Sent close request.\n");
1933 int cmd_packagename(char* arg) {
1934 xfree (esPackageName);
1935 esPackageName = NULL;
1938 esPackageName = (char *) xmalloc (strlen(arg)+1);
1939 strcpy (esPackageName, arg);
1944 int cmd_proxy(char* arg) {
1949 yazProxy = (char *) xmalloc (strlen(arg)+1);
1950 strcpy (yazProxy, arg);
1955 int cmd_source(char* arg)
1957 /* first should open the file and read one line at a time.. */
1959 char line[1024], *cp;
1962 fprintf(stderr,"Error in source command use a filename");
1966 includeFile = fopen (arg, "r");
1969 fprintf(stderr,"Unable to open file %s for reading\n",arg);
1974 while(!feof(includeFile)) {
1975 memset(line,0,sizeof(line));
1976 fgets(line,sizeof(line),includeFile);
1978 if(strlen(line) < 2) continue;
1979 if(line[0] == '#') continue;
1981 if ((cp = strrchr (line, '\n')))
1984 process_cmd_line(line);
1988 if(fclose(includeFile)<0) {
1989 perror("unable to close include file");
1995 int cmd_subshell(char* args) {
2000 static void initialize(void)
2005 if (!(out = odr_createmem(ODR_ENCODE)) ||
2006 !(in = odr_createmem(ODR_DECODE)) ||
2007 !(print = odr_createmem(ODR_PRINT)))
2009 fprintf(stderr, "failed to allocate ODR streams\n");
2012 setvbuf(stdout, 0, _IONBF, 0);
2014 odr_setprint(print, apdu_file);
2017 bibset = ccl_qual_mk ();
2018 inf = fopen (ccl_fields, "r");
2021 ccl_qual_file (bibset, inf);
2025 cmd_base("Default");
2029 #if HAVE_GETTIMEOFDAY
2030 struct timeval tv_start, tv_end;
2033 void wait_and_handle_responce()
2038 int netbufferlen = 0;
2044 && FD_ISSET(cs_fileno(conn), &input)
2050 if ((res = cs_get(conn, &netbuffer, &netbufferlen)) < 0)
2057 printf("Target closed connection.\n");
2060 odr_reset(in); /* release APDU from last round */
2062 odr_setbuf(in, netbuffer, res, 0);
2063 if (!z_APDU(in, &apdu, 0, 0))
2065 odr_perror(in, "Decoding incoming APDU");
2066 fprintf(stderr, "[Near %d]\n", odr_offset(in));
2067 fprintf(stderr, "Packet dump:\n---------\n");
2068 odr_dumpBER(stderr, netbuffer, res);
2069 fprintf(stderr, "---------\n");
2071 z_APDU(print, &apdu, 0, 0);
2074 if (apdu_file && !z_APDU(print, &apdu, 0, 0))
2076 odr_perror(print, "Failed to print incoming APDU");
2082 case Z_APDU_initResponse:
2083 process_initResponse(apdu->u.initResponse);
2085 case Z_APDU_searchResponse:
2086 process_searchResponse(apdu->u.searchResponse);
2088 case Z_APDU_scanResponse:
2089 process_scanResponse(apdu->u.scanResponse);
2091 case Z_APDU_presentResponse:
2092 print_refid (apdu->u.presentResponse->referenceId);
2094 *apdu->u.presentResponse->numberOfRecordsReturned;
2095 if (apdu->u.presentResponse->records)
2096 display_records(apdu->u.presentResponse->records);
2098 printf("No records.\n");
2099 printf ("nextResultSetPosition = %d\n",
2100 *apdu->u.presentResponse->nextResultSetPosition);
2102 case Z_APDU_sortResponse:
2103 process_sortResponse(apdu->u.sortResponse);
2105 case Z_APDU_extendedServicesResponse:
2106 printf("Got extended services response\n");
2107 process_ESResponse(apdu->u.extendedServicesResponse);
2110 printf("Target has closed the association.\n");
2111 process_close(apdu->u.close);
2113 case Z_APDU_resourceControlRequest:
2114 process_resourceControlRequest
2115 (apdu->u.resourceControlRequest);
2117 case Z_APDU_deleteResultSetResponse:
2118 process_deleteResultSetResponse(apdu->u.
2119 deleteResultSetResponse);
2122 printf("Received unknown APDU type (%d).\n",
2127 while (conn && cs_more(conn));
2128 #if HAVE_GETTIMEOFDAY
2129 gettimeofday (&tv_end, 0);
2131 printf ("S/U S/U=%ld/%ld %ld/%ld",
2132 (long) tv_start.tv_sec,
2133 (long) tv_start.tv_usec,
2134 (long) tv_end.tv_sec,
2135 (long) tv_end.tv_usec);
2137 printf ("Elapsed: %.6f\n",
2138 (double) tv_end.tv_usec / 1e6 + tv_end.tv_sec -
2139 ((double) tv_start.tv_usec / 1e6 + tv_start.tv_sec));
2144 void process_cmd_line(char* line)
2148 int (*fun)(char *arg);
2151 {"open", cmd_open, "('tcp'|'ssl')':<host>[':'<port>][/<db>]"},
2152 {"quit", cmd_quit, ""},
2153 {"find", cmd_find, "<query>"},
2154 {"delete", cmd_delete, "<setname>"},
2155 {"base", cmd_base, "<base-name>"},
2156 {"show", cmd_show, "<rec#>['+'<#recs>['+'<setname>]]"},
2157 {"scan", cmd_scan, "<term>"},
2158 {"sort", cmd_sort, "<sortkey> <flag> <sortkey> <flag> ..."},
2159 {"sort+", cmd_sort_newset, "<sortkey> <flag> <sortkey> <flag> ..."},
2160 {"authentication", cmd_authentication, "<acctstring>"},
2161 {"lslb", cmd_lslb, "<largeSetLowerBound>"},
2162 {"ssub", cmd_ssub, "<smallSetUpperBound>"},
2163 {"mspn", cmd_mspn, "<mediumSetPresentNumber>"},
2164 {"status", cmd_status, ""},
2165 {"setnames", cmd_setnames, ""},
2166 {"cancel", cmd_cancel, ""},
2167 {"format", cmd_format, "<recordsyntax>"},
2168 {"schema", cmd_schema, "<schema>"},
2169 {"elements", cmd_elements, "<elementSetName>"},
2170 {"close", cmd_close, ""},
2171 {"attributeset", cmd_attributeset, "<attrset>"},
2172 {"querytype", cmd_querytype, "<type>"},
2173 {"refid", cmd_refid, "<id>"},
2174 {"itemorder", cmd_itemorder, "ill|item <itemno>"},
2175 {"update", cmd_update, "<item>"},
2176 {"packagename", cmd_packagename, "<packagename>"},
2177 {"proxy", cmd_proxy, "('tcp'|'osi')':'[<tsel>'/']<host>[':'<port>]"},
2178 {".", cmd_source, "<filename>"},
2179 {"!", cmd_subshell, "Subshell command"},
2180 /* Server Admin Functions */
2181 {"adm-reindex", cmd_adm_reindex, "<database-name>"},
2182 {"adm-truncate", cmd_adm_truncate, "('database'|'index')<object-name>"},
2183 {"adm-create", cmd_adm_create, ""},
2184 {"adm-drop", cmd_adm_drop, "('database'|'index')<object-name>"},
2185 {"adm-import", cmd_adm_import, "<record-type> <dir> <pattern>"},
2186 {"adm-refresh", cmd_adm_refresh, ""},
2187 {"adm-commit", cmd_adm_commit, ""},
2188 {"adm-shutdown", cmd_adm_shutdown, ""},
2189 {"adm-startup", cmd_adm_startup, ""},
2193 char word[32], arg[1024];
2196 #if HAVE_GETTIMEOFDAY
2197 gettimeofday (&tv_start, 0);
2200 if ((res = sscanf(line, "%31s %1023[^;]", word, arg)) <= 0)
2202 strcpy(word, last_cmd);
2207 strcpy(last_cmd, word);
2208 for (i = 0; cmd[i].cmd; i++)
2209 if (!strncmp(cmd[i].cmd, word, strlen(word)))
2211 res = (*cmd[i].fun)(arg);
2214 if (!cmd[i].cmd) /* dump our help-screen */
2216 printf("Unknown command: %s.\n", word);
2217 printf("Currently recognized commands:\n");
2218 for (i = 0; cmd[i].cmd; i++)
2219 printf(" %s %s\n", cmd[i].cmd, cmd[i].ad);
2223 wait_and_handle_responce();
2228 static void client(void)
2231 #if HAVE_GETTIMEOFDAY
2232 gettimeofday (&tv_start, 0);
2243 #if HAVE_READLINE_READLINE_H
2245 line_in=readline(C_PROMPT);
2248 #if HAVE_READLINE_HISTORY_H
2250 add_history(line_in);
2252 strcpy(line,line_in);
2258 if (!fgets(line, 1023, stdin))
2260 if ((end_p = strchr (line, '\n')))
2263 process_cmd_line(line);
2268 int main(int argc, char **argv)
2271 char *open_command = 0;
2272 char *auth_command = 0;
2276 while ((ret = options("c:a:m:v:p:u:", argv, argc, &arg)) != -2)
2283 open_command = xmalloc (strlen(arg)+6);
2284 strcpy (open_command, "open ");
2285 strcat (open_command, arg);
2289 if (!(marcdump = fopen (arg, "a")))
2296 strncpy (ccl_fields, arg, sizeof(ccl_fields)-1);
2297 ccl_fields[sizeof(ccl_fields)-1] = '\0';
2300 if (!strcmp(arg, "-"))
2303 apdu_file=fopen(arg, "a");
2306 yazProxy=strdup(arg);
2311 auth_command = xmalloc (strlen(arg)+6);
2312 strcpy (auth_command, "auth ");
2313 strcat (auth_command, arg);
2317 yaz_log_init (yaz_log_mask_str(arg), "", NULL);
2320 fprintf (stderr, "Usage: %s [-m <marclog>] [ -a <apdulog>] "
2321 "[-c cclfields]\n [-p <proxy-addr>] [-u <auth>] "
2322 "[<server-addr>]\n",
2330 #ifdef HAVE_GETTIMEOFDAY
2331 gettimeofday (&tv_start, 0);
2333 process_cmd_line (auth_command);
2334 #if HAVE_READLINE_HISTORY_H
2335 add_history(auth_command);
2337 xfree(auth_command);
2341 #ifdef HAVE_GETTIMEOFDAY
2342 gettimeofday (&tv_start, 0);
2344 process_cmd_line (open_command);
2345 #if HAVE_READLINE_HISTORY_H
2346 add_history(open_command);
2348 xfree(open_command);