2 * Copyright (c) 1995-2002, Index Data
3 * See the file LICENSE for details.
5 * $Id: client.c,v 1.136 2002-01-23 22:40:36 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;
70 static int kilobytes = 1024;
72 static char last_cmd[32] = "?";
73 static FILE *marcdump = 0;
74 static char *refid = NULL;
82 static QueryType queryType = QueryType_Prefix;
85 static CCL_bibset bibset; /* CCL bibset handle */
89 /* set this one to 1, to avoid decode of unknown MARCs */
90 #define AVOID_MARC_DECODE 1
92 void process_cmd_line(char* line);
94 ODR getODROutputStream()
99 void send_apdu(Z_APDU *a)
106 z_APDU(print, &a, 0, 0);
109 if (!z_APDU(out, &a, 0, 0))
111 odr_perror(out, "Encoding APDU");
114 buf = odr_getbuf(out, &len, 0);
115 /* printf ("sending APDU of size %d\n", len); */
116 if (cs_put(conn, buf, len) < 0)
118 fprintf(stderr, "cs_put: %s", cs_errmsg(cs_errno(conn)));
121 odr_reset(out); /* release the APDU structure */
124 static void print_stringn(const unsigned char *buf, size_t len)
127 for (i = 0; i<len; i++)
128 if ((buf[i] <= 126 && buf[i] >= 32) || strchr ("\n\r\t\f", buf[i]))
129 fputc (buf[i], stdout);
131 printf ("\\X%02X", buf[i]);
134 static void print_refid (Z_ReferenceId *id)
138 printf ("Reference Id: ");
139 print_stringn (id->buf, id->len);
144 static Z_ReferenceId *set_refid (ODR out)
149 id = (Z_ReferenceId *) odr_malloc (out, sizeof(*id));
150 id->size = id->len = strlen(refid);
151 id->buf = (unsigned char *) odr_malloc (out, id->len);
152 memcpy (id->buf, refid, id->len);
156 /* INIT SERVICE ------------------------------- */
158 static void send_initRequest(const char* type_and_host)
160 Z_APDU *apdu = zget_APDU(out, Z_APDU_initRequest);
161 Z_InitRequest *req = apdu->u.initRequest;
163 ODR_MASK_SET(req->options, Z_Options_search);
164 ODR_MASK_SET(req->options, Z_Options_present);
165 ODR_MASK_SET(req->options, Z_Options_namedResultSets);
166 ODR_MASK_SET(req->options, Z_Options_triggerResourceCtrl);
167 ODR_MASK_SET(req->options, Z_Options_scan);
168 ODR_MASK_SET(req->options, Z_Options_sort);
169 ODR_MASK_SET(req->options, Z_Options_extendedServices);
170 ODR_MASK_SET(req->options, Z_Options_delSet);
172 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_1);
173 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_2);
174 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_3);
176 *req->maximumRecordSize = 1024*kilobytes;
177 *req->preferredMessageSize = 1024*kilobytes;
179 req->idAuthentication = auth;
181 req->referenceId = set_refid (out);
184 yaz_oi_set_string_oidval(&req->otherInfo, out, VAL_PROXY,
188 printf("Sent initrequest.\n");
191 static int process_initResponse(Z_InitResponse *res)
193 /* save session parameters for later use */
194 session_mem = odr_extract_mem(in);
198 printf("Connection rejected by target.\n");
200 printf("Connection accepted by target.\n");
201 if (res->implementationId)
202 printf("ID : %s\n", res->implementationId);
203 if (res->implementationName)
204 printf("Name : %s\n", res->implementationName);
205 if (res->implementationVersion)
206 printf("Version: %s\n", res->implementationVersion);
207 if (res->userInformationField)
209 printf("UserInformationfield:\n");
210 if (!z_External(print, (Z_External**)&res-> userInformationField,
213 odr_perror(print, "Printing userinfo\n");
216 if (res->userInformationField->which == Z_External_octet)
218 printf("Guessing visiblestring:\n");
219 printf("'%s'\n", res->userInformationField->u. octet_aligned->buf);
224 if (ODR_MASK_GET(res->options, Z_Options_search))
226 if (ODR_MASK_GET(res->options, Z_Options_present))
228 if (ODR_MASK_GET(res->options, Z_Options_delSet))
230 if (ODR_MASK_GET(res->options, Z_Options_resourceReport))
231 printf (" resourceReport");
232 if (ODR_MASK_GET(res->options, Z_Options_resourceCtrl))
233 printf (" resourceCtrl");
234 if (ODR_MASK_GET(res->options, Z_Options_accessCtrl))
235 printf (" accessCtrl");
236 if (ODR_MASK_GET(res->options, Z_Options_scan))
238 if (ODR_MASK_GET(res->options, Z_Options_sort))
240 if (ODR_MASK_GET(res->options, Z_Options_extendedServices))
241 printf (" extendedServices");
242 if (ODR_MASK_GET(res->options, Z_Options_level_1Segmentation))
243 printf (" level1Segmentation");
244 if (ODR_MASK_GET(res->options, Z_Options_level_2Segmentation))
245 printf (" level2Segmentation");
246 if (ODR_MASK_GET(res->options, Z_Options_concurrentOperations))
247 printf (" concurrentOperations");
248 if (ODR_MASK_GET(res->options, Z_Options_namedResultSets))
250 printf (" namedResultSets");
258 static int cmd_base(char *arg)
265 printf("Usage: base <database> <database> ...\n");
268 for (i = 0; i<num_databaseNames; i++)
269 xfree (databaseNames[i]);
270 num_databaseNames = 0;
273 if (!(cp = strchr(arg, ' ')))
274 cp = arg + strlen(arg);
277 databaseNames[num_databaseNames] = (char *)xmalloc (1 + cp - arg);
278 memcpy (databaseNames[num_databaseNames], arg, cp - arg);
279 databaseNames[num_databaseNames++][cp - arg] = '\0';
288 int cmd_open(char *arg)
291 char type_and_host[101], base[101];
296 printf("Already connected.\n");
302 nmem_destroy (session_mem);
308 if (sscanf (arg, "%100[^/]/%100s", type_and_host, base) < 1)
312 conn = cs_create_host(yazProxy, 1, &add);
314 conn = cs_create_host(type_and_host, 1, &add);
318 printf ("Couldn't create comstack\n");
321 printf("Connecting...");
323 if (cs_connect(conn, add) < 0)
325 printf ("error = %s\n", cs_strerror(conn));
326 if (conn->cerrno == CSYSERR)
334 send_initRequest(type_and_host);
340 int cmd_authentication(char *arg)
342 static Z_IdAuthentication au;
343 static char open[256];
347 printf("Auth field set to null\n");
352 au.which = Z_IdAuthentication_open;
358 /* SEARCH SERVICE ------------------------------ */
360 static void display_variant(Z_Variant *v, int level)
364 for (i = 0; i < v->num_triples; i++)
366 printf("%*sclass=%d,type=%d", level * 4, "", *v->triples[i]->zclass,
367 *v->triples[i]->type);
368 if (v->triples[i]->which == Z_Triple_internationalString)
369 printf(",value=%s\n", v->triples[i]->value.internationalString);
375 static void display_grs1(Z_GenericRecord *r, int level)
381 for (i = 0; i < r->num_elements; i++)
385 printf("%*s", level * 4, "");
389 printf("%d,", *t->tagType);
392 if (t->tagValue->which == Z_StringOrNumeric_numeric)
393 printf("%d) ", *t->tagValue->u.numeric);
395 printf("%s) ", t->tagValue->u.string);
396 if (t->content->which == Z_ElementData_subtree)
399 display_grs1(t->content->u.subtree, level+1);
401 else if (t->content->which == Z_ElementData_string)
402 printf("%s\n", t->content->u.string);
403 else if (t->content->which == Z_ElementData_numeric)
404 printf("%d\n", *t->content->u.numeric);
405 else if (t->content->which == Z_ElementData_oid)
407 int *ip = t->content->u.oid;
410 if ((oent = oid_getentbyoid(t->content->u.oid)))
411 printf("OID: %s\n", oent->desc);
415 while (ip && *ip >= 0)
416 printf(" %d", *(ip++));
420 else if (t->content->which == Z_ElementData_noDataRequested)
421 printf("[No data requested]\n");
422 else if (t->content->which == Z_ElementData_elementEmpty)
423 printf("[Element empty]\n");
424 else if (t->content->which == Z_ElementData_elementNotThere)
425 printf("[Element not there]\n");
428 if (t->appliedVariant)
429 display_variant(t->appliedVariant, level+1);
430 if (t->metaData && t->metaData->supportedVariants)
434 printf("%*s---- variant list\n", (level+1)*4, "");
435 for (c = 0; c < t->metaData->num_supportedVariants; c++)
437 printf("%*svariant #%d\n", (level+1)*4, "", c);
438 display_variant(t->metaData->supportedVariants[c], level + 2);
445 static void print_record(const unsigned char *buf, size_t len)
448 print_stringn (buf, len);
449 /* add newline if not already added ... */
450 if (i <= 0 || buf[i-1] != '\n')
451 fputc ('\n', stdout);
454 static void display_record(Z_External *r)
456 oident *ent = oid_getentbyoid(r->direct_reference);
460 * Tell the user what we got.
462 if (r->direct_reference)
464 printf("Record type: ");
466 printf("%s\n", ent->desc);
467 else if (!odr_oid(print, &r->direct_reference, 0, 0))
469 odr_perror(print, "print oid");
473 /* Check if this is a known, ASN.1 type tucked away in an octet string */
474 if (ent && r->which == Z_External_octet)
476 Z_ext_typeent *type = z_ext_getentbyref(ent->value);
482 * Call the given decoder to process the record.
484 odr_setbuf(in, (char*)r->u.octet_aligned->buf,
485 r->u.octet_aligned->len, 0);
486 if (!(*type->fun)(in, (char **)&rr, 0, 0))
488 odr_perror(in, "Decoding constructed record.");
489 fprintf(stderr, "[Near %d]\n", odr_offset(in));
490 fprintf(stderr, "Packet dump:\n---------\n");
491 odr_dumpBER(stderr, (char*)r->u.octet_aligned->buf,
492 r->u.octet_aligned->len);
493 fprintf(stderr, "---------\n");
497 * Note: we throw away the original, BER-encoded record here.
498 * Do something else with it if you want to keep it.
500 r->u.sutrs = (Z_SUTRS *) rr; /* we don't actually check the type here. */
501 r->which = type->what;
504 if (ent && ent->value == VAL_SOIF)
505 print_record((const unsigned char *) r->u.octet_aligned->buf,
506 r->u.octet_aligned->len);
507 else if (r->which == Z_External_octet && r->u.octet_aligned->len)
509 const char *octet_buf = (char*)r->u.octet_aligned->buf;
510 if (ent->value == VAL_TEXT_XML || ent->value == VAL_APPLICATION_XML ||
511 ent->value == VAL_HTML)
513 print_record((const unsigned char *) octet_buf,
514 r->u.octet_aligned->len);
516 else if (ent->value == VAL_POSTSCRIPT)
518 int size = r->u.octet_aligned->len;
521 print_record((const unsigned char *) octet_buf, size);
526 #if AVOID_MARC_DECODE
527 /* primitive check for a marc OID 5.1-29 */
528 ent->oidsuffix[0] == 5 && ent->oidsuffix[1] < 30
534 if (marc_display_exl (octet_buf, NULL, 0 /* debug */,
535 r->u.octet_aligned->len) <= 0)
537 printf ("bad MARC. Dumping as it is:\n");
538 print_record((const unsigned char*) octet_buf,
539 r->u.octet_aligned->len);
544 print_record((const unsigned char*) octet_buf,
545 r->u.octet_aligned->len);
549 fwrite (octet_buf, 1, r->u.octet_aligned->len, marcdump);
551 else if (ent && ent->value == VAL_SUTRS)
553 if (r->which != Z_External_sutrs)
555 printf("Expecting single SUTRS type for SUTRS.\n");
558 print_record(r->u.sutrs->buf, r->u.sutrs->len);
560 else if (ent && ent->value == VAL_GRS1)
562 if (r->which != Z_External_grs1)
564 printf("Expecting single GRS type for GRS.\n");
567 display_grs1(r->u.grs1, 0);
571 printf("Unknown record representation.\n");
572 if (!z_External(print, &r, 0, 0))
574 odr_perror(print, "Printing external");
581 static void display_diagrecs(Z_DiagRec **pp, int num)
585 Z_DefaultDiagFormat *r;
587 printf("Diagnostic message(s) from database:\n");
588 for (i = 0; i<num; i++)
590 Z_DiagRec *p = pp[i];
591 if (p->which != Z_DiagRec_defaultFormat)
593 printf("Diagnostic record not in default format.\n");
597 r = p->u.defaultFormat;
598 if (!(ent = oid_getentbyoid(r->diagnosticSetId)) ||
599 ent->oclass != CLASS_DIAGSET || ent->value != VAL_BIB1)
600 printf("Missing or unknown diagset\n");
601 printf(" [%d] %s", *r->condition, diagbib1_str(*r->condition));
604 case Z_DefaultDiagFormat_v2Addinfo:
605 printf (" -- v2 addinfo '%s'\n", r->u.v2Addinfo);
607 case Z_DefaultDiagFormat_v3Addinfo:
608 printf (" -- v3 addinfo '%s'\n", r->u.v3Addinfo);
615 static void display_nameplusrecord(Z_NamePlusRecord *p)
618 printf("[%s]", p->databaseName);
619 if (p->which == Z_NamePlusRecord_surrogateDiagnostic)
620 display_diagrecs(&p->u.surrogateDiagnostic, 1);
621 else if (p->which == Z_NamePlusRecord_databaseRecord)
622 display_record(p->u.databaseRecord);
625 static void display_records(Z_Records *p)
629 if (p->which == Z_Records_NSD)
631 Z_DiagRec dr, *dr_p = &dr;
632 dr.which = Z_DiagRec_defaultFormat;
633 dr.u.defaultFormat = p->u.nonSurrogateDiagnostic;
634 display_diagrecs (&dr_p, 1);
636 else if (p->which == Z_Records_multipleNSD)
637 display_diagrecs (p->u.multipleNonSurDiagnostics->diagRecs,
638 p->u.multipleNonSurDiagnostics->num_diagRecs);
641 printf("Records: %d\n", p->u.databaseOrSurDiagnostics->num_records);
642 for (i = 0; i < p->u.databaseOrSurDiagnostics->num_records; i++)
643 display_nameplusrecord(p->u.databaseOrSurDiagnostics->records[i]);
647 static int send_deleteResultSetRequest(char *arg)
652 Z_APDU *apdu = zget_APDU(out, Z_APDU_deleteResultSetRequest);
653 Z_DeleteResultSetRequest *req = apdu->u.deleteResultSetRequest;
655 req->referenceId = set_refid (out);
657 req->num_resultSetList =
658 sscanf (arg, "%30s %30s %30s %30s %30s %30s %30s %30s",
659 names[0], names[1], names[2], names[3],
660 names[4], names[5], names[6], names[7]);
662 req->deleteFunction = (int *)
663 odr_malloc (out, sizeof(*req->deleteFunction));
664 if (req->num_resultSetList > 0)
666 *req->deleteFunction = Z_DeleteRequest_list;
667 req->resultSetList = (char **)
668 odr_malloc (out, sizeof(*req->resultSetList)*
669 req->num_resultSetList);
670 for (i = 0; i<req->num_resultSetList; i++)
671 req->resultSetList[i] = names[i];
675 *req->deleteFunction = Z_DeleteRequest_all;
676 req->resultSetList = 0;
680 printf("Sent deleteResultSetRequest.\n");
684 static int send_searchRequest(char *arg)
686 Z_APDU *apdu = zget_APDU(out, Z_APDU_searchRequest);
687 Z_SearchRequest *req = apdu->u.searchRequest;
691 struct ccl_rpn_node *rpn = NULL;
695 Z_RPNQuery *RPNquery;
699 if (queryType == QueryType_CCL2RPN)
701 rpn = ccl_find_str(bibset, arg, &error, &pos);
704 printf("CCL ERROR: %s\n", ccl_err_msg(error));
709 req->referenceId = set_refid (out);
710 if (!strcmp(arg, "@big")) /* strictly for troublemaking */
712 static unsigned char big[2100];
715 /* send a very big referenceid to test transport stack etc. */
716 memset(big, 'A', 2100);
717 bigo.len = bigo.size = 2100;
719 req->referenceId = &bigo;
724 sprintf(setstring, "%d", ++setnumber);
725 req->resultSetName = setstring;
727 *req->smallSetUpperBound = smallSetUpperBound;
728 *req->largeSetLowerBound = largeSetLowerBound;
729 *req->mediumSetPresentNumber = mediumSetPresentNumber;
730 if (smallSetUpperBound > 0 || (largeSetLowerBound > 1 &&
731 mediumSetPresentNumber > 0))
735 prefsyn.proto = protocol;
736 prefsyn.oclass = CLASS_RECSYN;
737 prefsyn.value = recordsyntax;
738 req->preferredRecordSyntax =
739 odr_oiddup(out, oid_ent_to_oid(&prefsyn, oid));
740 req->smallSetElementSetNames =
741 req->mediumSetElementSetNames = elementSetNames;
743 req->num_databaseNames = num_databaseNames;
744 req->databaseNames = databaseNames;
750 case QueryType_Prefix:
751 query.which = Z_Query_type_1;
752 RPNquery = p_query_rpn (out, protocol, arg);
755 printf("Prefix query error\n");
758 query.u.type_1 = RPNquery;
761 query.which = Z_Query_type_2;
762 query.u.type_2 = &ccl_query;
763 ccl_query.buf = (unsigned char*) arg;
764 ccl_query.len = strlen(arg);
767 case QueryType_CCL2RPN:
768 query.which = Z_Query_type_1;
769 RPNquery = ccl_rpn_query(out, rpn);
772 printf ("Couldn't convert from CCL to RPN\n");
775 query.u.type_1 = RPNquery;
776 ccl_rpn_delete (rpn);
780 printf ("Unsupported query type\n");
785 printf("Sent searchRequest.\n");
789 static int process_searchResponse(Z_SearchResponse *res)
791 printf ("Received SearchResponse.\n");
792 print_refid (res->referenceId);
793 if (*res->searchStatus)
794 printf("Search was a success.\n");
796 printf("Search was a bloomin' failure.\n");
797 printf("Number of hits: %d", *res->resultCount);
799 printf (", setno %d", setnumber);
800 printf("\nrecords returned: %d\n",
801 *res->numberOfRecordsReturned);
802 setno += *res->numberOfRecordsReturned;
804 display_records(res->records);
808 static void print_level(int iLevel)
811 for (i = 0; i < iLevel * 4; i++)
815 static void print_int(int iLevel, const char *pTag, int *pInt)
820 printf("%s: %d\n", pTag, *pInt);
824 static void print_string(int iLevel, const char *pTag, const char *pString)
829 printf("%s: %s\n", pTag, pString);
833 static void print_oid(int iLevel, const char *pTag, Odr_oid *pOid)
841 for (; *pInt != -1; pInt++)
842 printf(" %d", *pInt);
847 static void print_referenceId(int iLevel, Z_ReferenceId *referenceId)
849 if (referenceId != NULL)
854 printf("Ref Id (%d, %d): ", referenceId->len, referenceId->size);
855 for (i = 0; i < referenceId->len; i++)
856 printf("%c", referenceId->buf[i]);
861 static void print_string_or_numeric(int iLevel, const char *pTag, Z_StringOrNumeric *pStringNumeric)
863 if (pStringNumeric != NULL)
865 switch (pStringNumeric->which)
867 case Z_StringOrNumeric_string:
868 print_string(iLevel, pTag, pStringNumeric->u.string);
871 case Z_StringOrNumeric_numeric:
872 print_int(iLevel, pTag, pStringNumeric->u.numeric);
877 printf("%s: valid type for Z_StringOrNumeric\n", pTag);
883 static void print_universe_report_duplicate(int iLevel, Z_UniverseReportDuplicate *pUniverseReportDuplicate)
885 if (pUniverseReportDuplicate != NULL)
888 printf("Universe Report Duplicate: \n");
890 print_string_or_numeric(iLevel, "Hit No", pUniverseReportDuplicate->hitno);
894 static void print_universe_report_hits(int iLevel, Z_UniverseReportHits *pUniverseReportHits)
896 if (pUniverseReportHits != NULL)
899 printf("Universe Report Hits: \n");
901 print_string_or_numeric(iLevel, "Database", pUniverseReportHits->database);
902 print_string_or_numeric(iLevel, "Hits", pUniverseReportHits->hits);
906 static void print_universe_report(int iLevel, Z_UniverseReport *pUniverseReport)
908 if (pUniverseReport != NULL)
911 printf("Universe Report: \n");
913 print_int(iLevel, "Total Hits", pUniverseReport->totalHits);
914 switch (pUniverseReport->which)
916 case Z_UniverseReport_databaseHits:
917 print_universe_report_hits(iLevel, pUniverseReport->u.databaseHits);
920 case Z_UniverseReport_duplicate:
921 print_universe_report_duplicate(iLevel, pUniverseReport->u.duplicate);
926 printf("Type: %d\n", pUniverseReport->which);
932 static void print_external(int iLevel, Z_External *pExternal)
934 if (pExternal != NULL)
937 printf("External: \n");
939 print_oid(iLevel, "Direct Reference", pExternal->direct_reference);
940 print_int(iLevel, "InDirect Reference", pExternal->indirect_reference);
941 print_string(iLevel, "Descriptor", pExternal->descriptor);
942 switch (pExternal->which)
944 case Z_External_universeReport:
945 print_universe_report(iLevel, pExternal->u.universeReport);
950 printf("Type: %d\n", pExternal->which);
956 static int process_resourceControlRequest (Z_ResourceControlRequest *req)
958 printf ("Received ResourceControlRequest.\n");
959 print_referenceId(1, req->referenceId);
960 print_int(1, "Suspended Flag", req->suspendedFlag);
961 print_int(1, "Partial Results Available", req->partialResultsAvailable);
962 print_int(1, "Response Required", req->responseRequired);
963 print_int(1, "Triggered Request Flag", req->triggeredRequestFlag);
964 print_external(1, req->resourceReport);
968 void process_ESResponse(Z_ExtendedServicesResponse *res)
971 switch (*res->operationStatus)
973 case Z_ExtendedServicesResponse_done:
976 case Z_ExtendedServicesResponse_accepted:
977 printf ("accepted\n");
979 case Z_ExtendedServicesResponse_failure:
980 printf ("failure\n");
981 display_diagrecs(res->diagnostics, res->num_diagnostics);
984 printf ("unknown\n");
986 if ( (*res->operationStatus != Z_ExtendedServicesResponse_failure) &&
987 (res->num_diagnostics != 0) ) {
988 display_diagrecs(res->diagnostics, res->num_diagnostics);
990 print_refid (res->referenceId);
991 if (res->taskPackage &&
992 res->taskPackage->which == Z_External_extendedService)
994 Z_TaskPackage *taskPackage = res->taskPackage->u.extendedService;
995 Odr_oct *id = taskPackage->targetReference;
996 Z_External *ext = taskPackage->taskSpecificParameters;
1000 printf ("Target Reference: ");
1001 print_stringn (id->buf, id->len);
1004 if (ext->which == Z_External_update)
1006 Z_IUUpdateTaskPackage *utp = ext->u.update->u.taskPackage;
1007 if (utp && utp->targetPart)
1009 Z_IUTargetPart *targetPart = utp->targetPart;
1012 for (i = 0; i<targetPart->num_taskPackageRecords; i++)
1015 Z_IUTaskPackageRecordStructure *tpr =
1016 targetPart->taskPackageRecords[i];
1017 printf ("task package record %d\n", i+1);
1018 if (tpr->which == Z_IUTaskPackageRecordStructure_record)
1020 display_record (tpr->u.record);
1024 printf ("other type\n");
1034 const char *get_ill_element (void *clientData, const char *element)
1039 static Z_External *create_external_itemRequest()
1041 struct ill_get_ctl ctl;
1042 ILL_ItemRequest *req;
1044 int item_request_size = 0;
1045 char *item_request_buf = 0;
1049 ctl.f = get_ill_element;
1051 req = ill_get_ItemRequest(&ctl, "ill", 0);
1053 printf ("ill_get_ItemRequest failed\n");
1055 if (!ill_ItemRequest (out, &req, 0, 0))
1059 ill_ItemRequest(print, &req, 0, 0);
1062 item_request_buf = odr_getbuf (out, &item_request_size, 0);
1063 if (item_request_buf)
1064 odr_setbuf (out, item_request_buf, item_request_size, 1);
1065 printf ("Couldn't encode ItemRequest, size %d\n", item_request_size);
1072 item_request_buf = odr_getbuf (out, &item_request_size, 0);
1073 oid.proto = PROTO_GENERAL;
1074 oid.oclass = CLASS_GENERAL;
1075 oid.value = VAL_ISO_ILL_1;
1077 r = (Z_External *) odr_malloc (out, sizeof(*r));
1078 r->direct_reference = odr_oiddup(out,oid_getoidbyent(&oid));
1079 r->indirect_reference = 0;
1081 r->which = Z_External_single;
1083 r->u.single_ASN1_type = (Odr_oct *)
1084 odr_malloc (out, sizeof(*r->u.single_ASN1_type));
1085 r->u.single_ASN1_type->buf = (unsigned char *)
1086 odr_malloc (out, item_request_size);
1087 r->u.single_ASN1_type->len = item_request_size;
1088 r->u.single_ASN1_type->size = item_request_size;
1089 memcpy (r->u.single_ASN1_type->buf, item_request_buf,
1091 printf ("len = %d\n", item_request_size);
1097 #ifdef YAZ_MODULE_ill
1098 static Z_External *create_external_ILL_APDU(int which)
1100 struct ill_get_ctl ctl;
1103 int ill_request_size = 0;
1104 char *ill_request_buf = 0;
1108 ctl.f = get_ill_element;
1110 ill_apdu = ill_get_APDU(&ctl, "ill", 0);
1112 if (!ill_APDU (out, &ill_apdu, 0, 0))
1116 printf ("-------------------\n");
1117 ill_APDU(print, &ill_apdu, 0, 0);
1119 printf ("-------------------\n");
1121 ill_request_buf = odr_getbuf (out, &ill_request_size, 0);
1122 if (ill_request_buf)
1123 odr_setbuf (out, ill_request_buf, ill_request_size, 1);
1124 printf ("Couldn't encode ILL-Request, size %d\n", ill_request_size);
1130 ill_request_buf = odr_getbuf (out, &ill_request_size, 0);
1132 oid.proto = PROTO_GENERAL;
1133 oid.oclass = CLASS_GENERAL;
1134 oid.value = VAL_ISO_ILL_1;
1136 r = (Z_External *) odr_malloc (out, sizeof(*r));
1137 r->direct_reference = odr_oiddup(out,oid_getoidbyent(&oid));
1138 r->indirect_reference = 0;
1140 r->which = Z_External_single;
1142 r->u.single_ASN1_type = (Odr_oct *)
1143 odr_malloc (out, sizeof(*r->u.single_ASN1_type));
1144 r->u.single_ASN1_type->buf = (unsigned char *)
1145 odr_malloc (out, ill_request_size);
1146 r->u.single_ASN1_type->len = ill_request_size;
1147 r->u.single_ASN1_type->size = ill_request_size;
1148 memcpy (r->u.single_ASN1_type->buf, ill_request_buf, ill_request_size);
1149 printf ("len = %d\n", ill_request_size);
1156 static Z_External *create_ItemOrderExternal(const char *type, int itemno)
1158 Z_External *r = (Z_External *) odr_malloc(out, sizeof(Z_External));
1159 oident ItemOrderRequest;
1161 ItemOrderRequest.proto = PROTO_Z3950;
1162 ItemOrderRequest.oclass = CLASS_EXTSERV;
1163 ItemOrderRequest.value = VAL_ITEMORDER;
1165 r->direct_reference = odr_oiddup(out,oid_getoidbyent(&ItemOrderRequest));
1166 r->indirect_reference = 0;
1169 r->which = Z_External_itemOrder;
1171 r->u.itemOrder = (Z_ItemOrder *) odr_malloc(out,sizeof(Z_ItemOrder));
1172 memset(r->u.itemOrder, 0, sizeof(Z_ItemOrder));
1173 r->u.itemOrder->which=Z_IOItemOrder_esRequest;
1175 r->u.itemOrder->u.esRequest = (Z_IORequest *)
1176 odr_malloc(out,sizeof(Z_IORequest));
1177 memset(r->u.itemOrder->u.esRequest, 0, sizeof(Z_IORequest));
1179 r->u.itemOrder->u.esRequest->toKeep = (Z_IOOriginPartToKeep *)
1180 odr_malloc(out,sizeof(Z_IOOriginPartToKeep));
1181 memset(r->u.itemOrder->u.esRequest->toKeep, 0, sizeof(Z_IOOriginPartToKeep));
1182 r->u.itemOrder->u.esRequest->notToKeep = (Z_IOOriginPartNotToKeep *)
1183 odr_malloc(out,sizeof(Z_IOOriginPartNotToKeep));
1184 memset(r->u.itemOrder->u.esRequest->notToKeep, 0, sizeof(Z_IOOriginPartNotToKeep));
1186 r->u.itemOrder->u.esRequest->toKeep->supplDescription = NULL;
1187 r->u.itemOrder->u.esRequest->toKeep->contact = NULL;
1188 r->u.itemOrder->u.esRequest->toKeep->addlBilling = NULL;
1190 r->u.itemOrder->u.esRequest->notToKeep->resultSetItem =
1191 (Z_IOResultSetItem *) odr_malloc(out, sizeof(Z_IOResultSetItem));
1192 memset(r->u.itemOrder->u.esRequest->notToKeep->resultSetItem, 0, sizeof(Z_IOResultSetItem));
1193 r->u.itemOrder->u.esRequest->notToKeep->resultSetItem->resultSetId = "1";
1195 r->u.itemOrder->u.esRequest->notToKeep->resultSetItem->item =
1196 (int *) odr_malloc(out, sizeof(int));
1197 *r->u.itemOrder->u.esRequest->notToKeep->resultSetItem->item = itemno;
1200 if (!strcmp (type, "item") || !strcmp(type, "2"))
1202 printf ("using item-request\n");
1203 r->u.itemOrder->u.esRequest->notToKeep->itemRequest =
1204 create_external_itemRequest();
1206 else if (!strcmp(type, "ill") || !strcmp(type, "1"))
1208 printf ("using ILL-request\n");
1209 r->u.itemOrder->u.esRequest->notToKeep->itemRequest =
1210 create_external_ILL_APDU(ILL_APDU_ILL_Request);
1212 else if (!strcmp(type, "xml") || !strcmp(type, "3"))
1214 const char *xml_buf =
1216 " <type>request</type>\n"
1217 " <libraryNo>000200</libraryNo>\n"
1218 " <borrowerTicketNo> 1212 </borrowerTicketNo>\n"
1220 r->u.itemOrder->u.esRequest->notToKeep->itemRequest =
1221 z_ext_record (out, VAL_TEXT_XML, xml_buf, strlen(xml_buf));
1224 r->u.itemOrder->u.esRequest->notToKeep->itemRequest = 0;
1227 r->u.itemOrder->u.esRequest->notToKeep->itemRequest = 0;
1232 static int send_itemorder(const char *type, int itemno)
1234 Z_APDU *apdu = zget_APDU(out, Z_APDU_extendedServicesRequest);
1235 Z_ExtendedServicesRequest *req = apdu->u.extendedServicesRequest;
1236 oident ItemOrderRequest;
1238 ItemOrderRequest.proto = PROTO_Z3950;
1239 ItemOrderRequest.oclass = CLASS_EXTSERV;
1240 ItemOrderRequest.value = VAL_ITEMORDER;
1241 req->packageType = odr_oiddup(out,oid_getoidbyent(&ItemOrderRequest));
1242 req->packageName = esPackageName;
1244 req->taskSpecificParameters = create_ItemOrderExternal(type, itemno);
1250 static int cmd_update(char *arg)
1252 Z_APDU *apdu = zget_APDU(out, Z_APDU_extendedServicesRequest );
1253 Z_ExtendedServicesRequest *req = apdu->u.extendedServicesRequest;
1256 Z_IUOriginPartToKeep *toKeep;
1257 Z_IUSuppliedRecords *notToKeep;
1259 printf ("Update request\n");
1264 update_oid.proto = PROTO_Z3950;
1265 update_oid.oclass = CLASS_EXTSERV;
1266 update_oid.value = VAL_DBUPDATE;
1267 oid_ent_to_oid (&update_oid, oid);
1268 req->packageType = odr_oiddup(out,oid);
1269 req->packageName = esPackageName;
1271 req->referenceId = set_refid (out);
1273 r = req->taskSpecificParameters = (Z_External *)
1274 odr_malloc (out, sizeof(*r));
1275 r->direct_reference = odr_oiddup(out,oid);
1276 r->indirect_reference = 0;
1278 r->which = Z_External_update;
1279 r->u.update = (Z_IUUpdate *) odr_malloc(out, sizeof(*r->u.update));
1280 r->u.update->which = Z_IUUpdate_esRequest;
1281 r->u.update->u.esRequest = (Z_IUUpdateEsRequest *)
1282 odr_malloc(out, sizeof(*r->u.update->u.esRequest));
1283 toKeep = r->u.update->u.esRequest->toKeep = (Z_IUOriginPartToKeep *)
1284 odr_malloc(out, sizeof(*r->u.update->u.esRequest->toKeep));
1285 toKeep->databaseName = databaseNames[0];
1287 toKeep->elementSetName = 0;
1288 toKeep->actionQualifier = 0;
1289 toKeep->action = (int *) odr_malloc(out, sizeof(*toKeep->action));
1290 *toKeep->action = Z_IUOriginPartToKeep_recordInsert;
1292 notToKeep = r->u.update->u.esRequest->notToKeep = (Z_IUSuppliedRecords *)
1293 odr_malloc(out, sizeof(*r->u.update->u.esRequest->notToKeep));
1295 notToKeep->elements = (Z_IUSuppliedRecords_elem **)
1296 odr_malloc(out, sizeof(*notToKeep->elements));
1297 notToKeep->elements[0] = (Z_IUSuppliedRecords_elem *)
1298 odr_malloc(out, sizeof(**notToKeep->elements));
1299 notToKeep->elements[0]->u.number = 0;
1300 notToKeep->elements[0]->supplementalId = 0;
1301 notToKeep->elements[0]->correlationInfo = 0;
1302 notToKeep->elements[0]->record = record_last;
1309 /* II : Added to do DALI Item Order Extended services request */
1310 static int cmd_itemorder(char *arg)
1315 if (sscanf (arg, "%10s %d", type, &itemno) != 2)
1318 printf("Item order request\n");
1320 send_itemorder(type, itemno);
1324 static int cmd_find(char *arg)
1328 printf("Find what?\n");
1333 printf("Not connected yet\n");
1336 if (!send_searchRequest(arg))
1341 static int cmd_delete(char *arg)
1345 printf("Not connected yet\n");
1348 if (!send_deleteResultSetRequest(arg))
1353 static int cmd_ssub(char *arg)
1355 if (!(smallSetUpperBound = atoi(arg)))
1360 static int cmd_lslb(char *arg)
1362 if (!(largeSetLowerBound = atoi(arg)))
1367 static int cmd_mspn(char *arg)
1369 if (!(mediumSetPresentNumber = atoi(arg)))
1374 static int cmd_status(char *arg)
1376 printf("smallSetUpperBound: %d\n", smallSetUpperBound);
1377 printf("largeSetLowerBound: %d\n", largeSetLowerBound);
1378 printf("mediumSetPresentNumber: %d\n", mediumSetPresentNumber);
1382 static int cmd_setnames(char *arg)
1384 if (*arg == '1') /* enable ? */
1386 else if (*arg == '0') /* disable ? */
1388 else if (setnumber < 0) /* no args, toggle .. */
1394 printf("Set numbering enabled.\n");
1396 printf("Set numbering disabled.\n");
1400 /* PRESENT SERVICE ----------------------------- */
1402 static int send_presentRequest(char *arg)
1404 Z_APDU *apdu = zget_APDU(out, Z_APDU_presentRequest);
1405 Z_PresentRequest *req = apdu->u.presentRequest;
1406 Z_RecordComposition compo;
1411 char setstring[100];
1413 req->referenceId = set_refid (out);
1414 if ((p = strchr(arg, '+')))
1421 if (p && (p=strchr(p+1, '+')))
1423 strcpy (setstring, p+1);
1424 req->resultSetId = setstring;
1426 else if (setnumber >= 0)
1428 sprintf(setstring, "%d", setnumber);
1429 req->resultSetId = setstring;
1434 static Z_Range range;
1435 static Z_Range *rangep = ⦥
1436 req->num_ranges = 1;
1438 req->resultSetStartPoint = &setno;
1439 req->numberOfRecordsRequested = &nos;
1440 prefsyn.proto = protocol;
1441 prefsyn.oclass = CLASS_RECSYN;
1442 prefsyn.value = recordsyntax;
1443 req->preferredRecordSyntax =
1444 odr_oiddup (out, oid_ent_to_oid(&prefsyn, oid));
1446 if (schema != VAL_NONE)
1450 prefschema.proto = protocol;
1451 prefschema.oclass = CLASS_SCHEMA;
1452 prefschema.value = schema;
1454 req->recordComposition = &compo;
1455 compo.which = Z_RecordComp_complex;
1456 compo.u.complex = (Z_CompSpec *)
1457 odr_malloc(out, sizeof(*compo.u.complex));
1458 compo.u.complex->selectAlternativeSyntax = (bool_t *)
1459 odr_malloc(out, sizeof(bool_t));
1460 *compo.u.complex->selectAlternativeSyntax = 0;
1462 compo.u.complex->generic = (Z_Specification *)
1463 odr_malloc(out, sizeof(*compo.u.complex->generic));
1464 compo.u.complex->generic->schema = (Odr_oid *)
1465 odr_oiddup(out, oid_ent_to_oid(&prefschema, oid));
1466 if (!compo.u.complex->generic->schema)
1468 /* OID wasn't a schema! Try record syntax instead. */
1469 prefschema.oclass = CLASS_RECSYN;
1470 compo.u.complex->generic->schema = (Odr_oid *)
1471 odr_oiddup(out, oid_ent_to_oid(&prefschema, oid));
1473 if (!elementSetNames)
1474 compo.u.complex->generic->elementSpec = 0;
1477 compo.u.complex->generic->elementSpec = (Z_ElementSpec *)
1478 odr_malloc(out, sizeof(Z_ElementSpec));
1479 compo.u.complex->generic->elementSpec->which =
1480 Z_ElementSpec_elementSetName;
1481 compo.u.complex->generic->elementSpec->u.elementSetName =
1482 elementSetNames->u.generic;
1484 compo.u.complex->num_dbSpecific = 0;
1485 compo.u.complex->dbSpecific = 0;
1486 compo.u.complex->num_recordSyntax = 0;
1487 compo.u.complex->recordSyntax = 0;
1489 else if (elementSetNames)
1491 req->recordComposition = &compo;
1492 compo.which = Z_RecordComp_simple;
1493 compo.u.simple = elementSetNames;
1496 printf("Sent presentRequest (%d+%d).\n", setno, nos);
1500 void process_close(Z_Close *req)
1502 Z_APDU *apdu = zget_APDU(out, Z_APDU_close);
1503 Z_Close *res = apdu->u.close;
1505 static char *reasons[] =
1510 "cost limit reached",
1512 "security violation",
1519 printf("Reason: %s, message: %s\n", reasons[*req->closeReason],
1520 req->diagnosticInformation ? req->diagnosticInformation : "NULL");
1527 nmem_destroy (session_mem);
1534 *res->closeReason = Z_Close_finished;
1536 printf("Sent response.\n");
1541 static int cmd_show(char *arg)
1545 printf("Not connected yet\n");
1548 if (!send_presentRequest(arg))
1553 int cmd_quit(char *arg)
1555 printf("See you later, alligator.\n");
1561 int cmd_cancel(char *arg)
1563 Z_APDU *apdu = zget_APDU(out, Z_APDU_triggerResourceControlRequest);
1564 Z_TriggerResourceControlRequest *req =
1565 apdu->u.triggerResourceControlRequest;
1570 printf("Session not initialized yet\n");
1573 if (!ODR_MASK_GET(session->options, Z_Options_triggerResourceCtrl))
1575 printf("Target doesn't support cancel (trigger resource ctrl)\n");
1578 *req->requestedAction = Z_TriggerResourceCtrl_cancel;
1579 req->resultSetWanted = &rfalse;
1582 printf("Sent cancel request\n");
1586 int send_scanrequest(const char *query, int pp, int num, const char *term)
1588 Z_APDU *apdu = zget_APDU(out, Z_APDU_scanRequest);
1589 Z_ScanRequest *req = apdu->u.scanRequest;
1594 if (queryType == QueryType_CCL2RPN)
1598 struct ccl_rpn_node *rpn;
1600 rpn = ccl_find_str (bibset, query, &error, &pos);
1603 printf("CCL ERROR: %s\n", ccl_err_msg(error));
1607 bib1.proto = PROTO_Z3950;
1608 bib1.oclass = CLASS_ATTSET;
1609 bib1.value = VAL_BIB1;
1610 req->attributeSet = oid_ent_to_oid (&bib1, oid);
1611 if (!(req->termListAndStartPoint = ccl_scan_query (out, rpn)))
1613 printf("Couldn't convert CCL to Scan term\n");
1616 ccl_rpn_delete (rpn);
1619 if (use_rpn && !(req->termListAndStartPoint =
1620 p_query_scan(out, protocol, &req->attributeSet, query)))
1622 printf("Prefix query error\n");
1627 if (req->termListAndStartPoint->term &&
1628 req->termListAndStartPoint->term->which == Z_Term_general &&
1629 req->termListAndStartPoint->term->u.general)
1631 req->termListAndStartPoint->term->u.general->buf =
1632 (unsigned char *) odr_strdup(out, term);
1633 req->termListAndStartPoint->term->u.general->len =
1634 req->termListAndStartPoint->term->u.general->size =
1638 req->referenceId = set_refid (out);
1639 req->num_databaseNames = num_databaseNames;
1640 req->databaseNames = databaseNames;
1641 req->numberOfTermsRequested = #
1642 req->preferredPositionInResponse = &pp;
1647 int send_sortrequest(char *arg, int newset)
1649 Z_APDU *apdu = zget_APDU(out, Z_APDU_sortRequest);
1650 Z_SortRequest *req = apdu->u.sortRequest;
1651 Z_SortKeySpecList *sksl = (Z_SortKeySpecList *)
1652 odr_malloc (out, sizeof(*sksl));
1656 sprintf (setstring, "%d", setnumber);
1658 sprintf (setstring, "default");
1660 req->referenceId = set_refid (out);
1662 req->num_inputResultSetNames = 1;
1663 req->inputResultSetNames = (Z_InternationalString **)
1664 odr_malloc (out, sizeof(*req->inputResultSetNames));
1665 req->inputResultSetNames[0] = odr_strdup (out, setstring);
1667 if (newset && setnumber >= 0)
1668 sprintf (setstring, "%d", ++setnumber);
1670 req->sortedResultSetName = odr_strdup (out, setstring);
1672 req->sortSequence = yaz_sort_spec (out, arg);
1673 if (!req->sortSequence)
1675 printf ("Missing sort specifications\n");
1682 void display_term(Z_TermInfo *t)
1684 if (t->term->which == Z_Term_general)
1686 printf("%.*s", t->term->u.general->len, t->term->u.general->buf);
1687 sprintf(last_scan_line, "%.*s", t->term->u.general->len,
1688 t->term->u.general->buf);
1691 printf("Term (not general)");
1692 if (t->globalOccurrences)
1693 printf (" (%d)\n", *t->globalOccurrences);
1698 void process_scanResponse(Z_ScanResponse *res)
1701 Z_Entry **entries = NULL;
1702 int num_entries = 0;
1704 printf("Received ScanResponse\n");
1705 print_refid (res->referenceId);
1706 printf("%d entries", *res->numberOfEntriesReturned);
1707 if (res->positionOfTerm)
1708 printf (", position=%d", *res->positionOfTerm);
1710 if (*res->scanStatus != Z_Scan_success)
1711 printf("Scan returned code %d\n", *res->scanStatus);
1714 if ((entries = res->entries->entries))
1715 num_entries = res->entries->num_entries;
1716 for (i = 0; i < num_entries; i++)
1718 int pos_term = res->positionOfTerm ? *res->positionOfTerm : -1;
1719 if (entries[i]->which == Z_Entry_termInfo)
1721 printf("%c ", i + 1 == pos_term ? '*' : ' ');
1722 display_term(entries[i]->u.termInfo);
1725 display_diagrecs(&entries[i]->u.surrogateDiagnostic, 1);
1727 if (res->entries->nonsurrogateDiagnostics)
1728 display_diagrecs (res->entries->nonsurrogateDiagnostics,
1729 res->entries->num_nonsurrogateDiagnostics);
1732 void process_sortResponse(Z_SortResponse *res)
1734 printf("Received SortResponse: status=");
1735 switch (*res->sortStatus)
1737 case Z_SortStatus_success:
1738 printf ("success"); break;
1739 case Z_SortStatus_partial_1:
1740 printf ("partial"); break;
1741 case Z_SortStatus_failure:
1742 printf ("failure"); break;
1744 printf ("unknown (%d)", *res->sortStatus);
1747 print_refid (res->referenceId);
1748 if (res->diagnostics)
1749 display_diagrecs(res->diagnostics,
1750 res->num_diagnostics);
1753 void process_deleteResultSetResponse (Z_DeleteResultSetResponse *res)
1755 printf("Got deleteResultSetResponse status=%d\n",
1756 *res->deleteOperationStatus);
1757 if (res->deleteListStatuses)
1760 for (i = 0; i < res->deleteListStatuses->num; i++)
1762 printf ("%s status=%d\n", res->deleteListStatuses->elements[i]->id,
1763 *res->deleteListStatuses->elements[i]->status);
1768 int cmd_sort_generic(char *arg, int newset)
1772 printf("Session not initialized yet\n");
1775 if (!ODR_MASK_GET(session->options, Z_Options_sort))
1777 printf("Target doesn't support sort\n");
1782 if (send_sortrequest(arg, newset) < 0)
1789 int cmd_sort(char *arg)
1791 return cmd_sort_generic (arg, 0);
1794 int cmd_sort_newset (char *arg)
1796 return cmd_sort_generic (arg, 1);
1799 int cmd_scan(char *arg)
1803 printf("Session not initialized yet\n");
1806 if (!ODR_MASK_GET(session->options, Z_Options_scan))
1808 printf("Target doesn't support scan\n");
1813 strcpy (last_scan_query, arg);
1814 if (send_scanrequest(arg, 1, 20, 0) < 0)
1819 if (send_scanrequest(last_scan_query, 1, 20, last_scan_line) < 0)
1825 int cmd_schema(char *arg)
1832 schema = oid_getvalbyname (arg);
1833 if (schema == VAL_NONE)
1835 printf ("unknown schema\n");
1841 int cmd_format(char *arg)
1845 printf("Usage: format <recordsyntax>\n");
1848 recordsyntax = oid_getvalbyname (arg);
1849 if (recordsyntax == VAL_NONE)
1851 printf ("unknown record syntax\n");
1857 int cmd_elements(char *arg)
1859 static Z_ElementSetNames esn;
1860 static char what[100];
1864 elementSetNames = 0;
1868 esn.which = Z_ElementSetNames_generic;
1869 esn.u.generic = what;
1870 elementSetNames = &esn;
1874 int cmd_attributeset(char *arg)
1880 printf("Usage: attributeset <setname>\n");
1883 sscanf(arg, "%s", what);
1884 if (p_query_attset (what))
1886 printf("Unknown attribute set name\n");
1892 int cmd_querytype (char *arg)
1894 if (!strcmp (arg, "ccl"))
1895 queryType = QueryType_CCL;
1896 else if (!strcmp (arg, "prefix") || !strcmp(arg, "rpn"))
1897 queryType = QueryType_Prefix;
1899 else if (!strcmp (arg, "ccl2rpn") || !strcmp (arg, "cclrpn"))
1900 queryType = QueryType_CCL2RPN;
1904 printf ("Querytype must be one of:\n");
1905 printf (" prefix - Prefix query\n");
1906 printf (" ccl - CCL query\n");
1908 printf (" ccl2rpn - CCL query converted to RPN\n");
1915 int cmd_refid (char *arg)
1921 refid = (char *) xmalloc (strlen(arg)+1);
1922 strcpy (refid, arg);
1927 int cmd_close(char *arg)
1934 apdu = zget_APDU(out, Z_APDU_close);
1935 req = apdu->u.close;
1936 *req->closeReason = Z_Close_finished;
1938 printf("Sent close request.\n");
1943 int cmd_packagename(char* arg) {
1944 xfree (esPackageName);
1945 esPackageName = NULL;
1948 esPackageName = (char *) xmalloc (strlen(arg)+1);
1949 strcpy (esPackageName, arg);
1954 int cmd_proxy(char* arg) {
1959 yazProxy = (char *) xmalloc (strlen(arg)+1);
1960 strcpy (yazProxy, arg);
1965 int cmd_source(char* arg)
1967 /* first should open the file and read one line at a time.. */
1969 char line[1024], *cp;
1972 fprintf(stderr,"Error in source command use a filename");
1976 includeFile = fopen (arg, "r");
1979 fprintf(stderr,"Unable to open file %s for reading\n",arg);
1984 while(!feof(includeFile)) {
1985 memset(line,0,sizeof(line));
1986 fgets(line,sizeof(line),includeFile);
1988 if(strlen(line) < 2) continue;
1989 if(line[0] == '#') continue;
1991 if ((cp = strrchr (line, '\n')))
1994 process_cmd_line(line);
1998 if(fclose(includeFile)<0) {
1999 perror("unable to close include file");
2005 int cmd_subshell(char* args) {
2010 static void initialize(void)
2015 if (!(out = odr_createmem(ODR_ENCODE)) ||
2016 !(in = odr_createmem(ODR_DECODE)) ||
2017 !(print = odr_createmem(ODR_PRINT)))
2019 fprintf(stderr, "failed to allocate ODR streams\n");
2022 setvbuf(stdout, 0, _IONBF, 0);
2024 odr_setprint(print, apdu_file);
2027 bibset = ccl_qual_mk ();
2028 inf = fopen (ccl_fields, "r");
2031 ccl_qual_file (bibset, inf);
2035 cmd_base("Default");
2039 #if HAVE_GETTIMEOFDAY
2040 struct timeval tv_start, tv_end;
2043 void wait_and_handle_responce()
2048 int netbufferlen = 0;
2054 && FD_ISSET(cs_fileno(conn), &input)
2060 if ((res = cs_get(conn, &netbuffer, &netbufferlen)) < 0)
2067 printf("Target closed connection.\n");
2070 odr_reset(in); /* release APDU from last round */
2072 odr_setbuf(in, netbuffer, res, 0);
2073 if (!z_APDU(in, &apdu, 0, 0))
2075 odr_perror(in, "Decoding incoming APDU");
2076 fprintf(stderr, "[Near %d]\n", odr_offset(in));
2077 fprintf(stderr, "Packet dump:\n---------\n");
2078 odr_dumpBER(stderr, netbuffer, res);
2079 fprintf(stderr, "---------\n");
2081 z_APDU(print, &apdu, 0, 0);
2084 if (apdu_file && !z_APDU(print, &apdu, 0, 0))
2086 odr_perror(print, "Failed to print incoming APDU");
2092 case Z_APDU_initResponse:
2093 process_initResponse(apdu->u.initResponse);
2095 case Z_APDU_searchResponse:
2096 process_searchResponse(apdu->u.searchResponse);
2098 case Z_APDU_scanResponse:
2099 process_scanResponse(apdu->u.scanResponse);
2101 case Z_APDU_presentResponse:
2102 print_refid (apdu->u.presentResponse->referenceId);
2104 *apdu->u.presentResponse->numberOfRecordsReturned;
2105 if (apdu->u.presentResponse->records)
2106 display_records(apdu->u.presentResponse->records);
2108 printf("No records.\n");
2109 printf ("nextResultSetPosition = %d\n",
2110 *apdu->u.presentResponse->nextResultSetPosition);
2112 case Z_APDU_sortResponse:
2113 process_sortResponse(apdu->u.sortResponse);
2115 case Z_APDU_extendedServicesResponse:
2116 printf("Got extended services response\n");
2117 process_ESResponse(apdu->u.extendedServicesResponse);
2120 printf("Target has closed the association.\n");
2121 process_close(apdu->u.close);
2123 case Z_APDU_resourceControlRequest:
2124 process_resourceControlRequest
2125 (apdu->u.resourceControlRequest);
2127 case Z_APDU_deleteResultSetResponse:
2128 process_deleteResultSetResponse(apdu->u.
2129 deleteResultSetResponse);
2132 printf("Received unknown APDU type (%d).\n",
2137 while (conn && cs_more(conn));
2138 #if HAVE_GETTIMEOFDAY
2139 gettimeofday (&tv_end, 0);
2141 printf ("S/U S/U=%ld/%ld %ld/%ld",
2142 (long) tv_start.tv_sec,
2143 (long) tv_start.tv_usec,
2144 (long) tv_end.tv_sec,
2145 (long) tv_end.tv_usec);
2147 printf ("Elapsed: %.6f\n",
2148 (double) tv_end.tv_usec / 1e6 + tv_end.tv_sec -
2149 ((double) tv_start.tv_usec / 1e6 + tv_start.tv_sec));
2154 void process_cmd_line(char* line)
2158 int (*fun)(char *arg);
2161 {"open", cmd_open, "('tcp'|'ssl')':<host>[':'<port>][/<db>]"},
2162 {"quit", cmd_quit, ""},
2163 {"find", cmd_find, "<query>"},
2164 {"delete", cmd_delete, "<setname>"},
2165 {"base", cmd_base, "<base-name>"},
2166 {"show", cmd_show, "<rec#>['+'<#recs>['+'<setname>]]"},
2167 {"scan", cmd_scan, "<term>"},
2168 {"sort", cmd_sort, "<sortkey> <flag> <sortkey> <flag> ..."},
2169 {"sort+", cmd_sort_newset, "<sortkey> <flag> <sortkey> <flag> ..."},
2170 {"authentication", cmd_authentication, "<acctstring>"},
2171 {"lslb", cmd_lslb, "<largeSetLowerBound>"},
2172 {"ssub", cmd_ssub, "<smallSetUpperBound>"},
2173 {"mspn", cmd_mspn, "<mediumSetPresentNumber>"},
2174 {"status", cmd_status, ""},
2175 {"setnames", cmd_setnames, ""},
2176 {"cancel", cmd_cancel, ""},
2177 {"format", cmd_format, "<recordsyntax>"},
2178 {"schema", cmd_schema, "<schema>"},
2179 {"elements", cmd_elements, "<elementSetName>"},
2180 {"close", cmd_close, ""},
2181 {"attributeset", cmd_attributeset, "<attrset>"},
2182 {"querytype", cmd_querytype, "<type>"},
2183 {"refid", cmd_refid, "<id>"},
2184 {"itemorder", cmd_itemorder, "ill|item <itemno>"},
2185 {"update", cmd_update, "<item>"},
2186 {"packagename", cmd_packagename, "<packagename>"},
2187 {"proxy", cmd_proxy, "('tcp'|'osi')':'[<tsel>'/']<host>[':'<port>]"},
2188 {".", cmd_source, "<filename>"},
2189 {"!", cmd_subshell, "Subshell command"},
2190 /* Server Admin Functions */
2191 {"adm-reindex", cmd_adm_reindex, "<database-name>"},
2192 {"adm-truncate", cmd_adm_truncate, "('database'|'index')<object-name>"},
2193 {"adm-create", cmd_adm_create, ""},
2194 {"adm-drop", cmd_adm_drop, "('database'|'index')<object-name>"},
2195 {"adm-import", cmd_adm_import, "<record-type> <dir> <pattern>"},
2196 {"adm-refresh", cmd_adm_refresh, ""},
2197 {"adm-commit", cmd_adm_commit, ""},
2198 {"adm-shutdown", cmd_adm_shutdown, ""},
2199 {"adm-startup", cmd_adm_startup, ""},
2203 char word[32], arg[1024];
2206 #if HAVE_GETTIMEOFDAY
2207 gettimeofday (&tv_start, 0);
2210 if ((res = sscanf(line, "%31s %1023[^;]", word, arg)) <= 0)
2212 strcpy(word, last_cmd);
2217 strcpy(last_cmd, word);
2218 for (i = 0; cmd[i].cmd; i++)
2219 if (!strncmp(cmd[i].cmd, word, strlen(word)))
2221 res = (*cmd[i].fun)(arg);
2224 if (!cmd[i].cmd) /* dump our help-screen */
2226 printf("Unknown command: %s.\n", word);
2227 printf("Currently recognized commands:\n");
2228 for (i = 0; cmd[i].cmd; i++)
2229 printf(" %s %s\n", cmd[i].cmd, cmd[i].ad);
2233 wait_and_handle_responce();
2238 static void client(void)
2241 #if HAVE_GETTIMEOFDAY
2242 gettimeofday (&tv_start, 0);
2253 #if HAVE_READLINE_READLINE_H
2255 line_in=readline(C_PROMPT);
2258 #if HAVE_READLINE_HISTORY_H
2260 add_history(line_in);
2262 strcpy(line,line_in);
2268 if (!fgets(line, 1023, stdin))
2270 if ((end_p = strchr (line, '\n')))
2273 process_cmd_line(line);
2278 int main(int argc, char **argv)
2281 char *open_command = 0;
2282 char *auth_command = 0;
2286 while ((ret = options("k:c:a:m:v:p:u:", argv, argc, &arg)) != -2)
2293 open_command = xmalloc (strlen(arg)+6);
2294 strcpy (open_command, "open ");
2295 strcat (open_command, arg);
2299 kilobytes = atoi(arg);
2302 if (!(marcdump = fopen (arg, "a")))
2309 strncpy (ccl_fields, arg, sizeof(ccl_fields)-1);
2310 ccl_fields[sizeof(ccl_fields)-1] = '\0';
2313 if (!strcmp(arg, "-"))
2316 apdu_file=fopen(arg, "a");
2319 yazProxy=strdup(arg);
2324 auth_command = xmalloc (strlen(arg)+6);
2325 strcpy (auth_command, "auth ");
2326 strcat (auth_command, arg);
2330 yaz_log_init (yaz_log_mask_str(arg), "", NULL);
2333 fprintf (stderr, "Usage: %s [-m <marclog>] [ -a <apdulog>] "
2334 "[-c cclfields]\n [-p <proxy-addr>] [-u <auth>] "
2335 "[<server-addr>]\n",
2343 #ifdef HAVE_GETTIMEOFDAY
2344 gettimeofday (&tv_start, 0);
2346 process_cmd_line (auth_command);
2347 #if HAVE_READLINE_HISTORY_H
2348 add_history(auth_command);
2350 xfree(auth_command);
2354 #ifdef HAVE_GETTIMEOFDAY
2355 gettimeofday (&tv_start, 0);
2357 process_cmd_line (open_command);
2358 #if HAVE_READLINE_HISTORY_H
2359 add_history(open_command);
2361 xfree(open_command);