2 * Copyright (c) 1995-2001, Index Data
3 * See the file LICENSE for details.
5 * $Id: client.c,v 1.133 2001-11-22 11:04:48 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_refid (Z_ReferenceId *id)
127 printf ("ReferenceId: '%.*s'\n", id->len, id->buf);
131 static Z_ReferenceId *set_refid (ODR out)
136 id = (Z_ReferenceId *) odr_malloc (out, sizeof(*id));
137 id->size = id->len = strlen(refid);
138 id->buf = (unsigned char *) odr_malloc (out, id->len);
139 memcpy (id->buf, refid, id->len);
143 /* INIT SERVICE ------------------------------- */
145 static void send_initRequest(const char* type_and_host)
147 Z_APDU *apdu = zget_APDU(out, Z_APDU_initRequest);
148 Z_InitRequest *req = apdu->u.initRequest;
150 ODR_MASK_SET(req->options, Z_Options_search);
151 ODR_MASK_SET(req->options, Z_Options_present);
152 ODR_MASK_SET(req->options, Z_Options_namedResultSets);
153 ODR_MASK_SET(req->options, Z_Options_triggerResourceCtrl);
154 ODR_MASK_SET(req->options, Z_Options_scan);
155 ODR_MASK_SET(req->options, Z_Options_sort);
156 ODR_MASK_SET(req->options, Z_Options_extendedServices);
157 ODR_MASK_SET(req->options, Z_Options_delSet);
159 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_1);
160 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_2);
161 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_3);
163 *req->maximumRecordSize = 1024*1024;
164 *req->preferredMessageSize = 1024*1024;
166 req->idAuthentication = auth;
168 req->referenceId = set_refid (out);
171 yaz_oi_set_string_oidval(&req->otherInfo, out, VAL_PROXY,
175 printf("Sent initrequest.\n");
178 static int process_initResponse(Z_InitResponse *res)
180 /* save session parameters for later use */
181 session_mem = odr_extract_mem(in);
185 printf("Connection rejected by target.\n");
187 printf("Connection accepted by target.\n");
188 if (res->implementationId)
189 printf("ID : %s\n", res->implementationId);
190 if (res->implementationName)
191 printf("Name : %s\n", res->implementationName);
192 if (res->implementationVersion)
193 printf("Version: %s\n", res->implementationVersion);
194 if (res->userInformationField)
196 printf("UserInformationfield:\n");
197 if (!z_External(print, (Z_External**)&res-> userInformationField,
200 odr_perror(print, "Printing userinfo\n");
203 if (res->userInformationField->which == Z_External_octet)
205 printf("Guessing visiblestring:\n");
206 printf("'%s'\n", res->userInformationField->u. octet_aligned->buf);
211 if (ODR_MASK_GET(res->options, Z_Options_search))
213 if (ODR_MASK_GET(res->options, Z_Options_present))
215 if (ODR_MASK_GET(res->options, Z_Options_delSet))
217 if (ODR_MASK_GET(res->options, Z_Options_resourceReport))
218 printf (" resourceReport");
219 if (ODR_MASK_GET(res->options, Z_Options_resourceCtrl))
220 printf (" resourceCtrl");
221 if (ODR_MASK_GET(res->options, Z_Options_accessCtrl))
222 printf (" accessCtrl");
223 if (ODR_MASK_GET(res->options, Z_Options_scan))
225 if (ODR_MASK_GET(res->options, Z_Options_sort))
227 if (ODR_MASK_GET(res->options, Z_Options_extendedServices))
228 printf (" extendedServices");
229 if (ODR_MASK_GET(res->options, Z_Options_level_1Segmentation))
230 printf (" level1Segmentation");
231 if (ODR_MASK_GET(res->options, Z_Options_level_2Segmentation))
232 printf (" level2Segmentation");
233 if (ODR_MASK_GET(res->options, Z_Options_concurrentOperations))
234 printf (" concurrentOperations");
235 if (ODR_MASK_GET(res->options, Z_Options_namedResultSets))
237 printf (" namedResultSets");
245 static int cmd_base(char *arg)
252 printf("Usage: base <database> <database> ...\n");
255 for (i = 0; i<num_databaseNames; i++)
256 xfree (databaseNames[i]);
257 num_databaseNames = 0;
260 if (!(cp = strchr(arg, ' ')))
261 cp = arg + strlen(arg);
264 databaseNames[num_databaseNames] = (char *)xmalloc (1 + cp - arg);
265 memcpy (databaseNames[num_databaseNames], arg, cp - arg);
266 databaseNames[num_databaseNames++][cp - arg] = '\0';
275 int cmd_open(char *arg)
278 char type_and_host[101], base[101];
283 printf("Already connected.\n");
289 nmem_destroy (session_mem);
295 if (sscanf (arg, "%100[^/]/%100s", type_and_host, base) < 1)
299 conn = cs_create_host(yazProxy, 1, &add);
301 conn = cs_create_host(type_and_host, 1, &add);
305 printf ("Couldn't create comstack\n");
308 printf("Connecting...");
310 if (cs_connect(conn, add) < 0)
312 printf ("error = %s\n", cs_strerror(conn));
313 if (conn->cerrno == CSYSERR)
321 send_initRequest(type_and_host);
327 int cmd_authentication(char *arg)
329 static Z_IdAuthentication au;
330 static char open[256];
334 printf("Auth field set to null\n");
339 au.which = Z_IdAuthentication_open;
345 /* SEARCH SERVICE ------------------------------ */
347 static void display_variant(Z_Variant *v, int level)
351 for (i = 0; i < v->num_triples; i++)
353 printf("%*sclass=%d,type=%d", level * 4, "", *v->triples[i]->zclass,
354 *v->triples[i]->type);
355 if (v->triples[i]->which == Z_Triple_internationalString)
356 printf(",value=%s\n", v->triples[i]->value.internationalString);
362 static void display_grs1(Z_GenericRecord *r, int level)
368 for (i = 0; i < r->num_elements; i++)
372 printf("%*s", level * 4, "");
376 printf("%d,", *t->tagType);
379 if (t->tagValue->which == Z_StringOrNumeric_numeric)
380 printf("%d) ", *t->tagValue->u.numeric);
382 printf("%s) ", t->tagValue->u.string);
383 if (t->content->which == Z_ElementData_subtree)
386 display_grs1(t->content->u.subtree, level+1);
388 else if (t->content->which == Z_ElementData_string)
389 printf("%s\n", t->content->u.string);
390 else if (t->content->which == Z_ElementData_numeric)
391 printf("%d\n", *t->content->u.numeric);
392 else if (t->content->which == Z_ElementData_oid)
394 int *ip = t->content->u.oid;
397 if ((oent = oid_getentbyoid(t->content->u.oid)))
398 printf("OID: %s\n", oent->desc);
402 while (ip && *ip >= 0)
403 printf(" %d", *(ip++));
407 else if (t->content->which == Z_ElementData_noDataRequested)
408 printf("[No data requested]\n");
409 else if (t->content->which == Z_ElementData_elementEmpty)
410 printf("[Element empty]\n");
411 else if (t->content->which == Z_ElementData_elementNotThere)
412 printf("[Element not there]\n");
415 if (t->appliedVariant)
416 display_variant(t->appliedVariant, level+1);
417 if (t->metaData && t->metaData->supportedVariants)
421 printf("%*s---- variant list\n", (level+1)*4, "");
422 for (c = 0; c < t->metaData->num_supportedVariants; c++)
424 printf("%*svariant #%d\n", (level+1)*4, "", c);
425 display_variant(t->metaData->supportedVariants[c], level + 2);
431 static void print_record(const unsigned char *buf, size_t len)
434 for (i = 0; i<len; i++)
435 if ((buf[i] <= 126 && buf[i] >= 32) || strchr ("\n\r\t\f", buf[i]))
436 fputc (buf[i], stdout);
438 printf ("\\X%02X", buf[i]);
439 /* add newline if not already added ... */
440 if (i <= 0 || buf[i-1] != '\n')
441 fputc ('\n', stdout);
444 static void display_record(Z_DatabaseRecord *p)
446 Z_External *r = (Z_External*) p;
447 oident *ent = oid_getentbyoid(r->direct_reference);
451 * Tell the user what we got.
453 if (r->direct_reference)
455 printf("Record type: ");
457 printf("%s\n", ent->desc);
458 else if (!odr_oid(print, &r->direct_reference, 0, 0))
460 odr_perror(print, "print oid");
464 /* Check if this is a known, ASN.1 type tucked away in an octet string */
465 if (ent && r->which == Z_External_octet)
467 Z_ext_typeent *type = z_ext_getentbyref(ent->value);
473 * Call the given decoder to process the record.
475 odr_setbuf(in, (char*)p->u.octet_aligned->buf,
476 p->u.octet_aligned->len, 0);
477 if (!(*type->fun)(in, (char **)&rr, 0, 0))
479 odr_perror(in, "Decoding constructed record.");
480 fprintf(stderr, "[Near %d]\n", odr_offset(in));
481 fprintf(stderr, "Packet dump:\n---------\n");
482 odr_dumpBER(stderr, (char*)p->u.octet_aligned->buf,
483 p->u.octet_aligned->len);
484 fprintf(stderr, "---------\n");
488 * Note: we throw away the original, BER-encoded record here.
489 * Do something else with it if you want to keep it.
491 r->u.sutrs = (Z_SUTRS *) rr; /* we don't actually check the type here. */
492 r->which = type->what;
495 if (ent && ent->value == VAL_SOIF)
496 print_record((const unsigned char *) r->u.octet_aligned->buf,
497 r->u.octet_aligned->len);
498 else if (r->which == Z_External_octet && p->u.octet_aligned->len)
500 const char *octet_buf = (char*)p->u.octet_aligned->buf;
501 if (ent->value == VAL_TEXT_XML || ent->value == VAL_APPLICATION_XML ||
502 ent->value == VAL_HTML)
503 print_record((const unsigned char *) octet_buf,
504 p->u.octet_aligned->len);
508 #if AVOID_MARC_DECODE
509 /* primitive check for a marc OID 5.1-29 */
510 ent->oidsuffix[0] == 5 && ent->oidsuffix[1] < 30
516 if (marc_display_exl (octet_buf, NULL, 0 /* debug */,
517 p->u.octet_aligned->len) <= 0)
519 printf ("bad MARC. Dumping as it is:\n");
520 print_record((const unsigned char*) octet_buf,
521 p->u.octet_aligned->len);
526 print_record((const unsigned char*) octet_buf,
527 p->u.octet_aligned->len);
531 fwrite (octet_buf, 1, p->u.octet_aligned->len, marcdump);
533 else if (ent && ent->value == VAL_SUTRS)
535 if (r->which != Z_External_sutrs)
537 printf("Expecting single SUTRS type for SUTRS.\n");
540 print_record(r->u.sutrs->buf, r->u.sutrs->len);
542 else if (ent && ent->value == VAL_GRS1)
544 if (r->which != Z_External_grs1)
546 printf("Expecting single GRS type for GRS.\n");
549 display_grs1(r->u.grs1, 0);
553 printf("Unknown record representation.\n");
554 if (!z_External(print, &r, 0, 0))
556 odr_perror(print, "Printing external");
563 static void display_diagrecs(Z_DiagRec **pp, int num)
567 Z_DefaultDiagFormat *r;
569 printf("Diagnostic message(s) from database:\n");
570 for (i = 0; i<num; i++)
572 Z_DiagRec *p = pp[i];
573 if (p->which != Z_DiagRec_defaultFormat)
575 printf("Diagnostic record not in default format.\n");
579 r = p->u.defaultFormat;
580 if (!(ent = oid_getentbyoid(r->diagnosticSetId)) ||
581 ent->oclass != CLASS_DIAGSET || ent->value != VAL_BIB1)
582 printf("Missing or unknown diagset\n");
583 printf(" [%d] %s", *r->condition, diagbib1_str(*r->condition));
586 case Z_DefaultDiagFormat_v2Addinfo:
587 printf (" -- v2 addinfo '%s'\n", r->u.v2Addinfo);
589 case Z_DefaultDiagFormat_v3Addinfo:
590 printf (" -- v3 addinfo '%s'\n", r->u.v3Addinfo);
597 static void display_nameplusrecord(Z_NamePlusRecord *p)
600 printf("[%s]", p->databaseName);
601 if (p->which == Z_NamePlusRecord_surrogateDiagnostic)
602 display_diagrecs(&p->u.surrogateDiagnostic, 1);
603 else if (p->which == Z_NamePlusRecord_databaseRecord)
604 display_record(p->u.databaseRecord);
607 static void display_records(Z_Records *p)
611 if (p->which == Z_Records_NSD)
613 Z_DiagRec dr, *dr_p = &dr;
614 dr.which = Z_DiagRec_defaultFormat;
615 dr.u.defaultFormat = p->u.nonSurrogateDiagnostic;
616 display_diagrecs (&dr_p, 1);
618 else if (p->which == Z_Records_multipleNSD)
619 display_diagrecs (p->u.multipleNonSurDiagnostics->diagRecs,
620 p->u.multipleNonSurDiagnostics->num_diagRecs);
623 printf("Records: %d\n", p->u.databaseOrSurDiagnostics->num_records);
624 for (i = 0; i < p->u.databaseOrSurDiagnostics->num_records; i++)
625 display_nameplusrecord(p->u.databaseOrSurDiagnostics->records[i]);
629 static int send_deleteResultSetRequest(char *arg)
634 Z_APDU *apdu = zget_APDU(out, Z_APDU_deleteResultSetRequest);
635 Z_DeleteResultSetRequest *req = apdu->u.deleteResultSetRequest;
637 req->referenceId = set_refid (out);
639 req->num_resultSetList =
640 sscanf (arg, "%30s %30s %30s %30s %30s %30s %30s %30s",
641 names[0], names[1], names[2], names[3],
642 names[4], names[5], names[6], names[7]);
644 req->deleteFunction = (int *)
645 odr_malloc (out, sizeof(*req->deleteFunction));
646 if (req->num_resultSetList > 0)
648 *req->deleteFunction = Z_DeleteRequest_list;
649 req->resultSetList = (char **)
650 odr_malloc (out, sizeof(*req->resultSetList)*
651 req->num_resultSetList);
652 for (i = 0; i<req->num_resultSetList; i++)
653 req->resultSetList[i] = names[i];
657 *req->deleteFunction = Z_DeleteRequest_all;
658 req->resultSetList = 0;
662 printf("Sent deleteResultSetRequest.\n");
666 static int send_searchRequest(char *arg)
668 Z_APDU *apdu = zget_APDU(out, Z_APDU_searchRequest);
669 Z_SearchRequest *req = apdu->u.searchRequest;
673 struct ccl_rpn_node *rpn = NULL;
677 Z_RPNQuery *RPNquery;
681 if (queryType == QueryType_CCL2RPN)
683 rpn = ccl_find_str(bibset, arg, &error, &pos);
686 printf("CCL ERROR: %s\n", ccl_err_msg(error));
691 req->referenceId = set_refid (out);
692 if (!strcmp(arg, "@big")) /* strictly for troublemaking */
694 static unsigned char big[2100];
697 /* send a very big referenceid to test transport stack etc. */
698 memset(big, 'A', 2100);
699 bigo.len = bigo.size = 2100;
701 req->referenceId = &bigo;
706 sprintf(setstring, "%d", ++setnumber);
707 req->resultSetName = setstring;
709 *req->smallSetUpperBound = smallSetUpperBound;
710 *req->largeSetLowerBound = largeSetLowerBound;
711 *req->mediumSetPresentNumber = mediumSetPresentNumber;
712 if (smallSetUpperBound > 0 || (largeSetLowerBound > 1 &&
713 mediumSetPresentNumber > 0))
717 prefsyn.proto = protocol;
718 prefsyn.oclass = CLASS_RECSYN;
719 prefsyn.value = recordsyntax;
720 req->preferredRecordSyntax =
721 odr_oiddup(out, oid_ent_to_oid(&prefsyn, oid));
722 req->smallSetElementSetNames =
723 req->mediumSetElementSetNames = elementSetNames;
725 req->num_databaseNames = num_databaseNames;
726 req->databaseNames = databaseNames;
732 case QueryType_Prefix:
733 query.which = Z_Query_type_1;
734 RPNquery = p_query_rpn (out, protocol, arg);
737 printf("Prefix query error\n");
740 query.u.type_1 = RPNquery;
743 query.which = Z_Query_type_2;
744 query.u.type_2 = &ccl_query;
745 ccl_query.buf = (unsigned char*) arg;
746 ccl_query.len = strlen(arg);
749 case QueryType_CCL2RPN:
750 query.which = Z_Query_type_1;
751 RPNquery = ccl_rpn_query(out, rpn);
754 printf ("Couldn't convert from CCL to RPN\n");
757 query.u.type_1 = RPNquery;
758 ccl_rpn_delete (rpn);
762 printf ("Unsupported query type\n");
767 printf("Sent searchRequest.\n");
771 static int process_searchResponse(Z_SearchResponse *res)
773 printf ("Received SearchResponse.\n");
774 print_refid (res->referenceId);
775 if (*res->searchStatus)
776 printf("Search was a success.\n");
778 printf("Search was a bloomin' failure.\n");
779 printf("Number of hits: %d", *res->resultCount);
781 printf (", setno %d", setnumber);
782 printf("\nrecords returned: %d\n",
783 *res->numberOfRecordsReturned);
784 setno += *res->numberOfRecordsReturned;
786 display_records(res->records);
790 static void print_level(int iLevel)
793 for (i = 0; i < iLevel * 4; i++)
797 static void print_int(int iLevel, const char *pTag, int *pInt)
802 printf("%s: %d\n", pTag, *pInt);
806 static void print_string(int iLevel, const char *pTag, const char *pString)
811 printf("%s: %s\n", pTag, pString);
815 static void print_oid(int iLevel, const char *pTag, Odr_oid *pOid)
823 for (; *pInt != -1; pInt++)
824 printf(" %d", *pInt);
829 static void print_referenceId(int iLevel, Z_ReferenceId *referenceId)
831 if (referenceId != NULL)
836 printf("Ref Id (%d, %d): ", referenceId->len, referenceId->size);
837 for (i = 0; i < referenceId->len; i++)
838 printf("%c", referenceId->buf[i]);
843 static void print_string_or_numeric(int iLevel, const char *pTag, Z_StringOrNumeric *pStringNumeric)
845 if (pStringNumeric != NULL)
847 switch (pStringNumeric->which)
849 case Z_StringOrNumeric_string:
850 print_string(iLevel, pTag, pStringNumeric->u.string);
853 case Z_StringOrNumeric_numeric:
854 print_int(iLevel, pTag, pStringNumeric->u.numeric);
859 printf("%s: valid type for Z_StringOrNumeric\n", pTag);
865 static void print_universe_report_duplicate(int iLevel, Z_UniverseReportDuplicate *pUniverseReportDuplicate)
867 if (pUniverseReportDuplicate != NULL)
870 printf("Universe Report Duplicate: \n");
872 print_string_or_numeric(iLevel, "Hit No", pUniverseReportDuplicate->hitno);
876 static void print_universe_report_hits(int iLevel, Z_UniverseReportHits *pUniverseReportHits)
878 if (pUniverseReportHits != NULL)
881 printf("Universe Report Hits: \n");
883 print_string_or_numeric(iLevel, "Database", pUniverseReportHits->database);
884 print_string_or_numeric(iLevel, "Hits", pUniverseReportHits->hits);
888 static void print_universe_report(int iLevel, Z_UniverseReport *pUniverseReport)
890 if (pUniverseReport != NULL)
893 printf("Universe Report: \n");
895 print_int(iLevel, "Total Hits", pUniverseReport->totalHits);
896 switch (pUniverseReport->which)
898 case Z_UniverseReport_databaseHits:
899 print_universe_report_hits(iLevel, pUniverseReport->u.databaseHits);
902 case Z_UniverseReport_duplicate:
903 print_universe_report_duplicate(iLevel, pUniverseReport->u.duplicate);
908 printf("Type: %d\n", pUniverseReport->which);
914 static void print_external(int iLevel, Z_External *pExternal)
916 if (pExternal != NULL)
919 printf("External: \n");
921 print_oid(iLevel, "Direct Reference", pExternal->direct_reference);
922 print_int(iLevel, "InDirect Reference", pExternal->indirect_reference);
923 print_string(iLevel, "Descriptor", pExternal->descriptor);
924 switch (pExternal->which)
926 case Z_External_universeReport:
927 print_universe_report(iLevel, pExternal->u.universeReport);
932 printf("Type: %d\n", pExternal->which);
938 static int process_resourceControlRequest (Z_ResourceControlRequest *req)
940 printf ("Received ResourceControlRequest.\n");
941 print_referenceId(1, req->referenceId);
942 print_int(1, "Suspended Flag", req->suspendedFlag);
943 print_int(1, "Partial Results Available", req->partialResultsAvailable);
944 print_int(1, "Response Required", req->responseRequired);
945 print_int(1, "Triggered Request Flag", req->triggeredRequestFlag);
946 print_external(1, req->resourceReport);
950 void process_ESResponse(Z_ExtendedServicesResponse *res)
952 printf("process_ESResponse status=");
953 switch (*res->operationStatus)
955 case Z_ExtendedServicesResponse_done:
958 case Z_ExtendedServicesResponse_accepted:
959 printf ("accepted\n");
961 case Z_ExtendedServicesResponse_failure:
962 printf ("failure\n");
963 display_diagrecs(res->diagnostics, res->num_diagnostics);
966 if ( (*res->operationStatus != Z_ExtendedServicesResponse_failure) &&
967 (res->num_diagnostics != 0) ) {
968 display_diagrecs(res->diagnostics, res->num_diagnostics);
975 const char *get_ill_element (void *clientData, const char *element)
977 printf ("%s\n", element);
981 static Z_External *create_external_itemRequest()
983 struct ill_get_ctl ctl;
984 ILL_ItemRequest *req;
986 int item_request_size = 0;
987 char *item_request_buf = 0;
991 ctl.f = get_ill_element;
993 req = ill_get_ItemRequest(&ctl, "ill", 0);
995 printf ("ill_get_ItemRequest failed\n");
997 if (!ill_ItemRequest (out, &req, 0, 0))
1001 ill_ItemRequest(print, &req, 0, 0);
1004 item_request_buf = odr_getbuf (out, &item_request_size, 0);
1005 if (item_request_buf)
1006 odr_setbuf (out, item_request_buf, item_request_size, 1);
1007 printf ("Couldn't encode ItemRequest, size %d\n", item_request_size);
1014 item_request_buf = odr_getbuf (out, &item_request_size, 0);
1015 oid.proto = PROTO_GENERAL;
1016 oid.oclass = CLASS_GENERAL;
1017 oid.value = VAL_ISO_ILL_1;
1019 r = (Z_External *) odr_malloc (out, sizeof(*r));
1020 r->direct_reference = odr_oiddup(out,oid_getoidbyent(&oid));
1021 r->indirect_reference = 0;
1023 r->which = Z_External_single;
1025 r->u.single_ASN1_type = (Odr_oct *)
1026 odr_malloc (out, sizeof(*r->u.single_ASN1_type));
1027 r->u.single_ASN1_type->buf = (unsigned char *)
1028 odr_malloc (out, item_request_size);
1029 r->u.single_ASN1_type->len = item_request_size;
1030 r->u.single_ASN1_type->size = item_request_size;
1031 memcpy (r->u.single_ASN1_type->buf, item_request_buf,
1033 printf ("len = %d\n", item_request_size);
1039 #ifdef YAZ_MODULE_ill
1040 static Z_External *create_external_ILL_APDU(int which)
1042 struct ill_get_ctl ctl;
1045 int ill_request_size = 0;
1046 char *ill_request_buf = 0;
1050 ctl.f = get_ill_element;
1052 ill_apdu = ill_get_APDU(&ctl, "ill", 0);
1054 if (!ill_APDU (out, &ill_apdu, 0, 0))
1058 printf ("-------------------\n");
1059 ill_APDU(print, &ill_apdu, 0, 0);
1061 printf ("-------------------\n");
1063 ill_request_buf = odr_getbuf (out, &ill_request_size, 0);
1064 if (ill_request_buf)
1065 odr_setbuf (out, ill_request_buf, ill_request_size, 1);
1066 printf ("Couldn't encode ILL-Request, size %d\n", ill_request_size);
1072 ill_request_buf = odr_getbuf (out, &ill_request_size, 0);
1074 oid.proto = PROTO_GENERAL;
1075 oid.oclass = CLASS_GENERAL;
1076 oid.value = VAL_ISO_ILL_1;
1078 r = (Z_External *) odr_malloc (out, sizeof(*r));
1079 r->direct_reference = odr_oiddup(out,oid_getoidbyent(&oid));
1080 r->indirect_reference = 0;
1082 r->which = Z_External_single;
1084 r->u.single_ASN1_type = (Odr_oct *)
1085 odr_malloc (out, sizeof(*r->u.single_ASN1_type));
1086 r->u.single_ASN1_type->buf = (unsigned char *)
1087 odr_malloc (out, ill_request_size);
1088 r->u.single_ASN1_type->len = ill_request_size;
1089 r->u.single_ASN1_type->size = ill_request_size;
1090 memcpy (r->u.single_ASN1_type->buf, ill_request_buf, ill_request_size);
1091 printf ("len = %d\n", ill_request_size);
1098 static Z_External *create_ItemOrderExternal(const char *type, int itemno)
1100 Z_External *r = (Z_External *) odr_malloc(out, sizeof(Z_External));
1101 oident ItemOrderRequest;
1103 ItemOrderRequest.proto = PROTO_Z3950;
1104 ItemOrderRequest.oclass = CLASS_EXTSERV;
1105 ItemOrderRequest.value = VAL_ITEMORDER;
1107 r->direct_reference = odr_oiddup(out,oid_getoidbyent(&ItemOrderRequest));
1108 r->indirect_reference = 0;
1111 r->which = Z_External_itemOrder;
1113 r->u.itemOrder = (Z_ItemOrder *) odr_malloc(out,sizeof(Z_ItemOrder));
1114 memset(r->u.itemOrder, 0, sizeof(Z_ItemOrder));
1115 r->u.itemOrder->which=Z_IOItemOrder_esRequest;
1117 r->u.itemOrder->u.esRequest = (Z_IORequest *)
1118 odr_malloc(out,sizeof(Z_IORequest));
1119 memset(r->u.itemOrder->u.esRequest, 0, sizeof(Z_IORequest));
1121 r->u.itemOrder->u.esRequest->toKeep = (Z_IOOriginPartToKeep *)
1122 odr_malloc(out,sizeof(Z_IOOriginPartToKeep));
1123 memset(r->u.itemOrder->u.esRequest->toKeep, 0, sizeof(Z_IOOriginPartToKeep));
1124 r->u.itemOrder->u.esRequest->notToKeep = (Z_IOOriginPartNotToKeep *)
1125 odr_malloc(out,sizeof(Z_IOOriginPartNotToKeep));
1126 memset(r->u.itemOrder->u.esRequest->notToKeep, 0, sizeof(Z_IOOriginPartNotToKeep));
1128 r->u.itemOrder->u.esRequest->toKeep->supplDescription = NULL;
1129 r->u.itemOrder->u.esRequest->toKeep->contact = NULL;
1130 r->u.itemOrder->u.esRequest->toKeep->addlBilling = NULL;
1132 r->u.itemOrder->u.esRequest->notToKeep->resultSetItem =
1133 (Z_IOResultSetItem *) odr_malloc(out, sizeof(Z_IOResultSetItem));
1134 memset(r->u.itemOrder->u.esRequest->notToKeep->resultSetItem, 0, sizeof(Z_IOResultSetItem));
1135 r->u.itemOrder->u.esRequest->notToKeep->resultSetItem->resultSetId = "1";
1137 r->u.itemOrder->u.esRequest->notToKeep->resultSetItem->item =
1138 (int *) odr_malloc(out, sizeof(int));
1139 *r->u.itemOrder->u.esRequest->notToKeep->resultSetItem->item = itemno;
1142 if (!strcmp (type, "item") || !strcmp(type, "2"))
1144 printf ("using item-request\n");
1145 r->u.itemOrder->u.esRequest->notToKeep->itemRequest =
1146 create_external_itemRequest();
1148 else if (!strcmp(type, "ill") || !strcmp(type, "1"))
1150 printf ("using ILL-request\n");
1151 r->u.itemOrder->u.esRequest->notToKeep->itemRequest =
1152 create_external_ILL_APDU(ILL_APDU_ILL_Request);
1154 else if (!strcmp(type, "xml") || !strcmp(type, "3"))
1156 const char *xml_buf =
1158 " <type>request</type>\n"
1159 " <libraryNo>000200</libraryNo>\n"
1160 " <borrowerTicketNo> 1212 </borrowerTicketNo>\n"
1162 r->u.itemOrder->u.esRequest->notToKeep->itemRequest =
1163 z_ext_record (out, VAL_TEXT_XML, xml_buf, strlen(xml_buf));
1166 r->u.itemOrder->u.esRequest->notToKeep->itemRequest = 0;
1169 r->u.itemOrder->u.esRequest->notToKeep->itemRequest = 0;
1174 static int send_itemorder(const char *type, int itemno)
1176 Z_APDU *apdu = zget_APDU(out, Z_APDU_extendedServicesRequest);
1177 Z_ExtendedServicesRequest *req = apdu->u.extendedServicesRequest;
1178 oident ItemOrderRequest;
1180 ItemOrderRequest.proto = PROTO_Z3950;
1181 ItemOrderRequest.oclass = CLASS_EXTSERV;
1182 ItemOrderRequest.value = VAL_ITEMORDER;
1183 req->packageType = odr_oiddup(out,oid_getoidbyent(&ItemOrderRequest));
1184 req->packageName = esPackageName;
1186 req->taskSpecificParameters = create_ItemOrderExternal(type, itemno);
1192 static int cmd_update(char *arg)
1194 Z_APDU *apdu = zget_APDU(out, Z_APDU_extendedServicesRequest );
1195 Z_ExtendedServicesRequest *req = apdu->u.extendedServicesRequest;
1198 Z_IUOriginPartToKeep *toKeep;
1199 Z_IUSuppliedRecords *notToKeep;
1201 printf ("Update request\n");
1206 update_oid.proto = PROTO_Z3950;
1207 update_oid.oclass = CLASS_EXTSERV;
1208 update_oid.value = VAL_DBUPDATE;
1209 oid_ent_to_oid (&update_oid, oid);
1210 req->packageType = odr_oiddup(out,oid);
1211 req->packageName = esPackageName;
1213 req->referenceId = set_refid (out);
1215 r = req->taskSpecificParameters = (Z_External *)
1216 odr_malloc (out, sizeof(*r));
1217 r->direct_reference = odr_oiddup(out,oid);
1218 r->indirect_reference = 0;
1220 r->which = Z_External_update;
1221 r->u.update = (Z_IUUpdate *) odr_malloc(out, sizeof(*r->u.update));
1222 r->u.update->which = Z_IUUpdate_esRequest;
1223 r->u.update->u.esRequest = (Z_IUUpdateEsRequest *)
1224 odr_malloc(out, sizeof(*r->u.update->u.esRequest));
1225 toKeep = r->u.update->u.esRequest->toKeep = (Z_IUOriginPartToKeep *)
1226 odr_malloc(out, sizeof(*r->u.update->u.esRequest->toKeep));
1227 toKeep->databaseName = databaseNames[0];
1229 toKeep->elementSetName = 0;
1230 toKeep->actionQualifier = 0;
1231 toKeep->action = (int *) odr_malloc(out, sizeof(*toKeep->action));
1232 *toKeep->action = Z_IUOriginPartToKeep_recordInsert;
1234 notToKeep = r->u.update->u.esRequest->notToKeep = (Z_IUSuppliedRecords *)
1235 odr_malloc(out, sizeof(*r->u.update->u.esRequest->notToKeep));
1237 notToKeep->elements = (Z_IUSuppliedRecords_elem **)
1238 odr_malloc(out, sizeof(*notToKeep->elements));
1239 notToKeep->elements[0] = (Z_IUSuppliedRecords_elem *)
1240 odr_malloc(out, sizeof(**notToKeep->elements));
1241 notToKeep->elements[0]->u.number = 0;
1242 notToKeep->elements[0]->supplementalId = 0;
1243 notToKeep->elements[0]->correlationInfo = 0;
1244 notToKeep->elements[0]->record = record_last;
1251 /* II : Added to do DALI Item Order Extended services request */
1252 static int cmd_itemorder(char *arg)
1257 if (sscanf (arg, "%10s %d", type, &itemno) != 2)
1260 printf("Item order request\n");
1262 send_itemorder(type, itemno);
1266 static int cmd_find(char *arg)
1270 printf("Find what?\n");
1275 printf("Not connected yet\n");
1278 if (!send_searchRequest(arg))
1283 static int cmd_delete(char *arg)
1287 printf("Not connected yet\n");
1290 if (!send_deleteResultSetRequest(arg))
1295 static int cmd_ssub(char *arg)
1297 if (!(smallSetUpperBound = atoi(arg)))
1302 static int cmd_lslb(char *arg)
1304 if (!(largeSetLowerBound = atoi(arg)))
1309 static int cmd_mspn(char *arg)
1311 if (!(mediumSetPresentNumber = atoi(arg)))
1316 static int cmd_status(char *arg)
1318 printf("smallSetUpperBound: %d\n", smallSetUpperBound);
1319 printf("largeSetLowerBound: %d\n", largeSetLowerBound);
1320 printf("mediumSetPresentNumber: %d\n", mediumSetPresentNumber);
1324 static int cmd_setnames(char *arg)
1326 if (*arg == '1') /* enable ? */
1328 else if (*arg == '0') /* disable ? */
1330 else if (setnumber < 0) /* no args, toggle .. */
1336 printf("Set numbering enabled.\n");
1338 printf("Set numbering disabled.\n");
1342 /* PRESENT SERVICE ----------------------------- */
1344 static int send_presentRequest(char *arg)
1346 Z_APDU *apdu = zget_APDU(out, Z_APDU_presentRequest);
1347 Z_PresentRequest *req = apdu->u.presentRequest;
1348 Z_RecordComposition compo;
1353 char setstring[100];
1355 req->referenceId = set_refid (out);
1356 if ((p = strchr(arg, '+')))
1363 if (p && (p=strchr(p+1, '+')))
1365 strcpy (setstring, p+1);
1366 req->resultSetId = setstring;
1368 else if (setnumber >= 0)
1370 sprintf(setstring, "%d", setnumber);
1371 req->resultSetId = setstring;
1376 static Z_Range range;
1377 static Z_Range *rangep = ⦥
1378 req->num_ranges = 1;
1380 req->resultSetStartPoint = &setno;
1381 req->numberOfRecordsRequested = &nos;
1382 prefsyn.proto = protocol;
1383 prefsyn.oclass = CLASS_RECSYN;
1384 prefsyn.value = recordsyntax;
1385 req->preferredRecordSyntax =
1386 odr_oiddup (out, oid_ent_to_oid(&prefsyn, oid));
1388 if (schema != VAL_NONE)
1392 prefschema.proto = protocol;
1393 prefschema.oclass = CLASS_SCHEMA;
1394 prefschema.value = schema;
1396 req->recordComposition = &compo;
1397 compo.which = Z_RecordComp_complex;
1398 compo.u.complex = (Z_CompSpec *)
1399 odr_malloc(out, sizeof(*compo.u.complex));
1400 compo.u.complex->selectAlternativeSyntax = (bool_t *)
1401 odr_malloc(out, sizeof(bool_t));
1402 *compo.u.complex->selectAlternativeSyntax = 0;
1404 compo.u.complex->generic = (Z_Specification *)
1405 odr_malloc(out, sizeof(*compo.u.complex->generic));
1406 compo.u.complex->generic->schema = (Odr_oid *)
1407 odr_oiddup(out, oid_ent_to_oid(&prefschema, oid));
1408 if (!compo.u.complex->generic->schema)
1410 /* OID wasn't a schema! Try record syntax instead. */
1411 prefschema.oclass = CLASS_RECSYN;
1412 compo.u.complex->generic->schema = (Odr_oid *)
1413 odr_oiddup(out, oid_ent_to_oid(&prefschema, oid));
1415 if (!elementSetNames)
1416 compo.u.complex->generic->elementSpec = 0;
1419 compo.u.complex->generic->elementSpec = (Z_ElementSpec *)
1420 odr_malloc(out, sizeof(Z_ElementSpec));
1421 compo.u.complex->generic->elementSpec->which =
1422 Z_ElementSpec_elementSetName;
1423 compo.u.complex->generic->elementSpec->u.elementSetName =
1424 elementSetNames->u.generic;
1426 compo.u.complex->num_dbSpecific = 0;
1427 compo.u.complex->dbSpecific = 0;
1428 compo.u.complex->num_recordSyntax = 0;
1429 compo.u.complex->recordSyntax = 0;
1431 else if (elementSetNames)
1433 req->recordComposition = &compo;
1434 compo.which = Z_RecordComp_simple;
1435 compo.u.simple = elementSetNames;
1438 printf("Sent presentRequest (%d+%d).\n", setno, nos);
1442 void process_close(Z_Close *req)
1444 Z_APDU *apdu = zget_APDU(out, Z_APDU_close);
1445 Z_Close *res = apdu->u.close;
1447 static char *reasons[] =
1452 "cost limit reached",
1454 "security violation",
1461 printf("Reason: %s, message: %s\n", reasons[*req->closeReason],
1462 req->diagnosticInformation ? req->diagnosticInformation : "NULL");
1469 nmem_destroy (session_mem);
1476 *res->closeReason = Z_Close_finished;
1478 printf("Sent response.\n");
1483 static int cmd_show(char *arg)
1487 printf("Not connected yet\n");
1490 if (!send_presentRequest(arg))
1495 int cmd_quit(char *arg)
1497 printf("See you later, alligator.\n");
1503 int cmd_cancel(char *arg)
1505 Z_APDU *apdu = zget_APDU(out, Z_APDU_triggerResourceControlRequest);
1506 Z_TriggerResourceControlRequest *req =
1507 apdu->u.triggerResourceControlRequest;
1512 printf("Session not initialized yet\n");
1515 if (!ODR_MASK_GET(session->options, Z_Options_triggerResourceCtrl))
1517 printf("Target doesn't support cancel (trigger resource ctrl)\n");
1520 *req->requestedAction = Z_TriggerResourceCtrl_cancel;
1521 req->resultSetWanted = &rfalse;
1524 printf("Sent cancel request\n");
1528 int send_scanrequest(const char *query, int pp, int num, const char *term)
1530 Z_APDU *apdu = zget_APDU(out, Z_APDU_scanRequest);
1531 Z_ScanRequest *req = apdu->u.scanRequest;
1536 if (queryType == QueryType_CCL2RPN)
1540 struct ccl_rpn_node *rpn;
1542 rpn = ccl_find_str (bibset, query, &error, &pos);
1545 printf("CCL ERROR: %s\n", ccl_err_msg(error));
1549 bib1.proto = PROTO_Z3950;
1550 bib1.oclass = CLASS_ATTSET;
1551 bib1.value = VAL_BIB1;
1552 req->attributeSet = oid_ent_to_oid (&bib1, oid);
1553 if (!(req->termListAndStartPoint = ccl_scan_query (out, rpn)))
1555 printf("Couldn't convert CCL to Scan term\n");
1558 ccl_rpn_delete (rpn);
1561 if (use_rpn && !(req->termListAndStartPoint =
1562 p_query_scan(out, protocol, &req->attributeSet, query)))
1564 printf("Prefix query error\n");
1569 if (req->termListAndStartPoint->term &&
1570 req->termListAndStartPoint->term->which == Z_Term_general &&
1571 req->termListAndStartPoint->term->u.general)
1573 req->termListAndStartPoint->term->u.general->buf =
1574 (unsigned char *) odr_strdup(out, term);
1575 req->termListAndStartPoint->term->u.general->len =
1576 req->termListAndStartPoint->term->u.general->size =
1580 req->referenceId = set_refid (out);
1581 req->num_databaseNames = num_databaseNames;
1582 req->databaseNames = databaseNames;
1583 req->numberOfTermsRequested = #
1584 req->preferredPositionInResponse = &pp;
1589 int send_sortrequest(char *arg, int newset)
1591 Z_APDU *apdu = zget_APDU(out, Z_APDU_sortRequest);
1592 Z_SortRequest *req = apdu->u.sortRequest;
1593 Z_SortKeySpecList *sksl = (Z_SortKeySpecList *)
1594 odr_malloc (out, sizeof(*sksl));
1598 sprintf (setstring, "%d", setnumber);
1600 sprintf (setstring, "default");
1602 req->referenceId = set_refid (out);
1604 req->num_inputResultSetNames = 1;
1605 req->inputResultSetNames = (Z_InternationalString **)
1606 odr_malloc (out, sizeof(*req->inputResultSetNames));
1607 req->inputResultSetNames[0] = odr_strdup (out, setstring);
1609 if (newset && setnumber >= 0)
1610 sprintf (setstring, "%d", ++setnumber);
1612 req->sortedResultSetName = odr_strdup (out, setstring);
1614 req->sortSequence = yaz_sort_spec (out, arg);
1615 if (!req->sortSequence)
1617 printf ("Missing sort specifications\n");
1624 void display_term(Z_TermInfo *t)
1626 if (t->term->which == Z_Term_general)
1628 printf("%.*s", t->term->u.general->len, t->term->u.general->buf);
1629 sprintf(last_scan_line, "%.*s", t->term->u.general->len,
1630 t->term->u.general->buf);
1633 printf("Term (not general)");
1634 if (t->globalOccurrences)
1635 printf (" (%d)\n", *t->globalOccurrences);
1640 void process_scanResponse(Z_ScanResponse *res)
1643 Z_Entry **entries = NULL;
1644 int num_entries = 0;
1646 printf("Received ScanResponse\n");
1647 print_refid (res->referenceId);
1648 printf("%d entries", *res->numberOfEntriesReturned);
1649 if (res->positionOfTerm)
1650 printf (", position=%d", *res->positionOfTerm);
1652 if (*res->scanStatus != Z_Scan_success)
1653 printf("Scan returned code %d\n", *res->scanStatus);
1656 if ((entries = res->entries->entries))
1657 num_entries = res->entries->num_entries;
1658 for (i = 0; i < num_entries; i++)
1660 int pos_term = res->positionOfTerm ? *res->positionOfTerm : -1;
1661 if (entries[i]->which == Z_Entry_termInfo)
1663 printf("%c ", i + 1 == pos_term ? '*' : ' ');
1664 display_term(entries[i]->u.termInfo);
1667 display_diagrecs(&entries[i]->u.surrogateDiagnostic, 1);
1669 if (res->entries->nonsurrogateDiagnostics)
1670 display_diagrecs (res->entries->nonsurrogateDiagnostics,
1671 res->entries->num_nonsurrogateDiagnostics);
1674 void process_sortResponse(Z_SortResponse *res)
1676 printf("Received SortResponse: status=");
1677 switch (*res->sortStatus)
1679 case Z_SortStatus_success:
1680 printf ("success"); break;
1681 case Z_SortStatus_partial_1:
1682 printf ("partial"); break;
1683 case Z_SortStatus_failure:
1684 printf ("failure"); break;
1686 printf ("unknown (%d)", *res->sortStatus);
1689 print_refid (res->referenceId);
1690 if (res->diagnostics)
1691 display_diagrecs(res->diagnostics,
1692 res->num_diagnostics);
1695 void process_deleteResultSetResponse (Z_DeleteResultSetResponse *res)
1697 printf("Got deleteResultSetResponse status=%d\n",
1698 *res->deleteOperationStatus);
1699 if (res->deleteListStatuses)
1702 for (i = 0; i < res->deleteListStatuses->num; i++)
1704 printf ("%s status=%d\n", res->deleteListStatuses->elements[i]->id,
1705 *res->deleteListStatuses->elements[i]->status);
1710 int cmd_sort_generic(char *arg, int newset)
1714 printf("Session not initialized yet\n");
1717 if (!ODR_MASK_GET(session->options, Z_Options_sort))
1719 printf("Target doesn't support sort\n");
1724 if (send_sortrequest(arg, newset) < 0)
1731 int cmd_sort(char *arg)
1733 return cmd_sort_generic (arg, 0);
1736 int cmd_sort_newset (char *arg)
1738 return cmd_sort_generic (arg, 1);
1741 int cmd_scan(char *arg)
1745 printf("Session not initialized yet\n");
1748 if (!ODR_MASK_GET(session->options, Z_Options_scan))
1750 printf("Target doesn't support scan\n");
1755 strcpy (last_scan_query, arg);
1756 if (send_scanrequest(arg, 1, 20, 0) < 0)
1761 if (send_scanrequest(last_scan_query, 1, 20, last_scan_line) < 0)
1767 int cmd_schema(char *arg)
1774 schema = oid_getvalbyname (arg);
1775 if (schema == VAL_NONE)
1777 printf ("unknown schema\n");
1783 int cmd_format(char *arg)
1787 printf("Usage: format <recordsyntax>\n");
1790 recordsyntax = oid_getvalbyname (arg);
1791 if (recordsyntax == VAL_NONE)
1793 printf ("unknown record syntax\n");
1799 int cmd_elements(char *arg)
1801 static Z_ElementSetNames esn;
1802 static char what[100];
1806 elementSetNames = 0;
1810 esn.which = Z_ElementSetNames_generic;
1811 esn.u.generic = what;
1812 elementSetNames = &esn;
1816 int cmd_attributeset(char *arg)
1822 printf("Usage: attributeset <setname>\n");
1825 sscanf(arg, "%s", what);
1826 if (p_query_attset (what))
1828 printf("Unknown attribute set name\n");
1834 int cmd_querytype (char *arg)
1836 if (!strcmp (arg, "ccl"))
1837 queryType = QueryType_CCL;
1838 else if (!strcmp (arg, "prefix") || !strcmp(arg, "rpn"))
1839 queryType = QueryType_Prefix;
1841 else if (!strcmp (arg, "ccl2rpn") || !strcmp (arg, "cclrpn"))
1842 queryType = QueryType_CCL2RPN;
1846 printf ("Querytype must be one of:\n");
1847 printf (" prefix - Prefix query\n");
1848 printf (" ccl - CCL query\n");
1850 printf (" ccl2rpn - CCL query converted to RPN\n");
1857 int cmd_refid (char *arg)
1863 refid = (char *) xmalloc (strlen(arg)+1);
1864 strcpy (refid, arg);
1869 int cmd_close(char *arg)
1876 apdu = zget_APDU(out, Z_APDU_close);
1877 req = apdu->u.close;
1878 *req->closeReason = Z_Close_finished;
1880 printf("Sent close request.\n");
1885 int cmd_packagename(char* arg) {
1886 xfree (esPackageName);
1887 esPackageName = NULL;
1890 esPackageName = (char *) xmalloc (strlen(arg)+1);
1891 strcpy (esPackageName, arg);
1896 int cmd_proxy(char* arg) {
1901 yazProxy = (char *) xmalloc (strlen(arg)+1);
1902 strcpy (yazProxy, arg);
1907 int cmd_source(char* arg)
1909 /* first should open the file and read one line at a time.. */
1911 char line[1024], *cp;
1914 fprintf(stderr,"Error in source command use a filename");
1918 includeFile = fopen (arg, "r");
1921 fprintf(stderr,"Unable to open file %s for reading\n",arg);
1926 while(!feof(includeFile)) {
1927 memset(line,0,sizeof(line));
1928 fgets(line,sizeof(line),includeFile);
1930 if(strlen(line) < 2) continue;
1931 if(line[0] == '#') continue;
1933 if ((cp = strrchr (line, '\n')))
1936 process_cmd_line(line);
1940 if(fclose(includeFile)<0) {
1941 perror("unable to close include file");
1947 int cmd_subshell(char* args) {
1952 static void initialize(void)
1957 if (!(out = odr_createmem(ODR_ENCODE)) ||
1958 !(in = odr_createmem(ODR_DECODE)) ||
1959 !(print = odr_createmem(ODR_PRINT)))
1961 fprintf(stderr, "failed to allocate ODR streams\n");
1964 setvbuf(stdout, 0, _IONBF, 0);
1966 odr_setprint(print, apdu_file);
1969 bibset = ccl_qual_mk ();
1970 inf = fopen (ccl_fields, "r");
1973 ccl_qual_file (bibset, inf);
1977 cmd_base("Default");
1981 #if HAVE_GETTIMEOFDAY
1982 struct timeval tv_start, tv_end;
1985 void wait_and_handle_responce()
1990 int netbufferlen = 0;
1996 && FD_ISSET(cs_fileno(conn), &input)
2002 if ((res = cs_get(conn, &netbuffer, &netbufferlen)) < 0)
2009 printf("Target closed connection.\n");
2012 odr_reset(in); /* release APDU from last round */
2014 odr_setbuf(in, netbuffer, res, 0);
2015 if (!z_APDU(in, &apdu, 0, 0))
2017 odr_perror(in, "Decoding incoming APDU");
2018 fprintf(stderr, "[Near %d]\n", odr_offset(in));
2019 fprintf(stderr, "Packet dump:\n---------\n");
2020 odr_dumpBER(stderr, netbuffer, res);
2021 fprintf(stderr, "---------\n");
2023 z_APDU(print, &apdu, 0, 0);
2026 if (apdu_file && !z_APDU(print, &apdu, 0, 0))
2028 odr_perror(print, "Failed to print incoming APDU");
2034 case Z_APDU_initResponse:
2035 process_initResponse(apdu->u.initResponse);
2037 case Z_APDU_searchResponse:
2038 process_searchResponse(apdu->u.searchResponse);
2040 case Z_APDU_scanResponse:
2041 process_scanResponse(apdu->u.scanResponse);
2043 case Z_APDU_presentResponse:
2044 print_refid (apdu->u.presentResponse->referenceId);
2046 *apdu->u.presentResponse->numberOfRecordsReturned;
2047 if (apdu->u.presentResponse->records)
2048 display_records(apdu->u.presentResponse->records);
2050 printf("No records.\n");
2051 printf ("nextResultSetPosition = %d\n",
2052 *apdu->u.presentResponse->nextResultSetPosition);
2054 case Z_APDU_sortResponse:
2055 process_sortResponse(apdu->u.sortResponse);
2057 case Z_APDU_extendedServicesResponse:
2058 printf("Got extended services response\n");
2059 process_ESResponse(apdu->u.extendedServicesResponse);
2062 printf("Target has closed the association.\n");
2063 process_close(apdu->u.close);
2065 case Z_APDU_resourceControlRequest:
2066 process_resourceControlRequest
2067 (apdu->u.resourceControlRequest);
2069 case Z_APDU_deleteResultSetResponse:
2070 process_deleteResultSetResponse(apdu->u.
2071 deleteResultSetResponse);
2074 printf("Received unknown APDU type (%d).\n",
2079 while (conn && cs_more(conn));
2080 #if HAVE_GETTIMEOFDAY
2081 gettimeofday (&tv_end, 0);
2083 printf ("S/U S/U=%ld/%ld %ld/%ld",
2084 (long) tv_start.tv_sec,
2085 (long) tv_start.tv_usec,
2086 (long) tv_end.tv_sec,
2087 (long) tv_end.tv_usec);
2089 printf ("Elapsed: %.6f\n",
2090 (double) tv_end.tv_usec / 1e6 + tv_end.tv_sec -
2091 ((double) tv_start.tv_usec / 1e6 + tv_start.tv_sec));
2096 void process_cmd_line(char* line)
2100 int (*fun)(char *arg);
2103 {"open", cmd_open, "('tcp'|'ssl')':<host>[':'<port>][/<db>]"},
2104 {"quit", cmd_quit, ""},
2105 {"find", cmd_find, "<query>"},
2106 {"delete", cmd_delete, "<setname>"},
2107 {"base", cmd_base, "<base-name>"},
2108 {"show", cmd_show, "<rec#>['+'<#recs>['+'<setname>]]"},
2109 {"scan", cmd_scan, "<term>"},
2110 {"sort", cmd_sort, "<sortkey> <flag> <sortkey> <flag> ..."},
2111 {"sort+", cmd_sort_newset, "<sortkey> <flag> <sortkey> <flag> ..."},
2112 {"authentication", cmd_authentication, "<acctstring>"},
2113 {"lslb", cmd_lslb, "<largeSetLowerBound>"},
2114 {"ssub", cmd_ssub, "<smallSetUpperBound>"},
2115 {"mspn", cmd_mspn, "<mediumSetPresentNumber>"},
2116 {"status", cmd_status, ""},
2117 {"setnames", cmd_setnames, ""},
2118 {"cancel", cmd_cancel, ""},
2119 {"format", cmd_format, "<recordsyntax>"},
2120 {"schema", cmd_schema, "<schema>"},
2121 {"elements", cmd_elements, "<elementSetName>"},
2122 {"close", cmd_close, ""},
2123 {"attributeset", cmd_attributeset, "<attrset>"},
2124 {"querytype", cmd_querytype, "<type>"},
2125 {"refid", cmd_refid, "<id>"},
2126 {"itemorder", cmd_itemorder, "ill|item <itemno>"},
2127 {"update", cmd_update, "<item>"},
2128 {"packagename", cmd_packagename, "<packagename>"},
2129 {"proxy", cmd_proxy, "('tcp'|'osi')':'[<tsel>'/']<host>[':'<port>]"},
2130 {".", cmd_source, "<filename>"},
2131 {"!", cmd_subshell, "Subshell command"},
2132 /* Server Admin Functions */
2133 {"adm-reindex", cmd_adm_reindex, "<database-name>"},
2134 {"adm-truncate", cmd_adm_truncate, "('database'|'index')<object-name>"},
2135 {"adm-create", cmd_adm_create, ""},
2136 {"adm-drop", cmd_adm_drop, "('database'|'index')<object-name>"},
2137 {"adm-import", cmd_adm_import, "<record-type> <dir> <pattern>"},
2138 {"adm-refresh", cmd_adm_refresh, ""},
2139 {"adm-commit", cmd_adm_commit, ""},
2140 {"adm-shutdown", cmd_adm_shutdown, ""},
2141 {"adm-startup", cmd_adm_startup, ""},
2145 char word[32], arg[1024];
2148 #if HAVE_GETTIMEOFDAY
2149 gettimeofday (&tv_start, 0);
2152 if ((res = sscanf(line, "%31s %1023[^;]", word, arg)) <= 0)
2154 strcpy(word, last_cmd);
2159 strcpy(last_cmd, word);
2160 for (i = 0; cmd[i].cmd; i++)
2161 if (!strncmp(cmd[i].cmd, word, strlen(word)))
2163 res = (*cmd[i].fun)(arg);
2166 if (!cmd[i].cmd) /* dump our help-screen */
2168 printf("Unknown command: %s.\n", word);
2169 printf("Currently recognized commands:\n");
2170 for (i = 0; cmd[i].cmd; i++)
2171 printf(" %s %s\n", cmd[i].cmd, cmd[i].ad);
2175 wait_and_handle_responce();
2180 static void client(void)
2183 #if HAVE_GETTIMEOFDAY
2184 gettimeofday (&tv_start, 0);
2195 #if HAVE_READLINE_READLINE_H
2197 line_in=readline(C_PROMPT);
2200 #if HAVE_READLINE_HISTORY_H
2202 add_history(line_in);
2204 strcpy(line,line_in);
2210 if (!fgets(line, 1023, stdin))
2212 if ((end_p = strchr (line, '\n')))
2215 process_cmd_line(line);
2220 int main(int argc, char **argv)
2223 char *open_command = 0;
2227 while ((ret = options("c:a:m:v:p:", argv, argc, &arg)) != -2)
2234 open_command = xmalloc (strlen(arg)+6);
2235 strcpy (open_command, "open ");
2236 strcat (open_command, arg);
2240 if (!(marcdump = fopen (arg, "a")))
2247 strncpy (ccl_fields, arg, sizeof(ccl_fields)-1);
2248 ccl_fields[sizeof(ccl_fields)-1] = '\0';
2251 if (!strcmp(arg, "-"))
2254 apdu_file=fopen(arg, "a");
2257 yazProxy=strdup(arg);
2260 yaz_log_init (yaz_log_mask_str(arg), "", NULL);
2263 fprintf (stderr, "Usage: %s [-m <marclog>] [ -a <apdulog>] "
2264 "[-c cclfields] [-p <proxy-addr>] [<server-addr>]\n",
2272 #ifdef HAVE_GETTIMEOFDAY
2273 gettimeofday (&tv_start, 0);
2275 process_cmd_line (open_command);
2276 #if HAVE_READLINE_HISTORY_H
2277 add_history(open_command);
2279 xfree(open_command);