2 * Copyright (c) 1995-2002, Index Data
3 * See the file LICENSE for details.
5 * $Id: client.c,v 1.137 2002-01-28 09:25:38 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 user[40], group[40], pass[40];
344 static Z_IdPass idPass;
349 printf("Auth field set to null\n");
353 r = sscanf (arg, "%39s %39s %39s", user, group, pass);
356 printf("Auth field set to null\n");
362 au.which = Z_IdAuthentication_open;
368 au.which = Z_IdAuthentication_idPass;
369 au.u.idPass = &idPass;
370 idPass.groupId = group;
371 idPass.userId = user;
372 idPass.password = pass;
377 /* SEARCH SERVICE ------------------------------ */
379 static void display_variant(Z_Variant *v, int level)
383 for (i = 0; i < v->num_triples; i++)
385 printf("%*sclass=%d,type=%d", level * 4, "", *v->triples[i]->zclass,
386 *v->triples[i]->type);
387 if (v->triples[i]->which == Z_Triple_internationalString)
388 printf(",value=%s\n", v->triples[i]->value.internationalString);
394 static void display_grs1(Z_GenericRecord *r, int level)
400 for (i = 0; i < r->num_elements; i++)
404 printf("%*s", level * 4, "");
408 printf("%d,", *t->tagType);
411 if (t->tagValue->which == Z_StringOrNumeric_numeric)
412 printf("%d) ", *t->tagValue->u.numeric);
414 printf("%s) ", t->tagValue->u.string);
415 if (t->content->which == Z_ElementData_subtree)
418 display_grs1(t->content->u.subtree, level+1);
420 else if (t->content->which == Z_ElementData_string)
421 printf("%s\n", t->content->u.string);
422 else if (t->content->which == Z_ElementData_numeric)
423 printf("%d\n", *t->content->u.numeric);
424 else if (t->content->which == Z_ElementData_oid)
426 int *ip = t->content->u.oid;
429 if ((oent = oid_getentbyoid(t->content->u.oid)))
430 printf("OID: %s\n", oent->desc);
434 while (ip && *ip >= 0)
435 printf(" %d", *(ip++));
439 else if (t->content->which == Z_ElementData_noDataRequested)
440 printf("[No data requested]\n");
441 else if (t->content->which == Z_ElementData_elementEmpty)
442 printf("[Element empty]\n");
443 else if (t->content->which == Z_ElementData_elementNotThere)
444 printf("[Element not there]\n");
447 if (t->appliedVariant)
448 display_variant(t->appliedVariant, level+1);
449 if (t->metaData && t->metaData->supportedVariants)
453 printf("%*s---- variant list\n", (level+1)*4, "");
454 for (c = 0; c < t->metaData->num_supportedVariants; c++)
456 printf("%*svariant #%d\n", (level+1)*4, "", c);
457 display_variant(t->metaData->supportedVariants[c], level + 2);
464 static void print_record(const unsigned char *buf, size_t len)
467 print_stringn (buf, len);
468 /* add newline if not already added ... */
469 if (i <= 0 || buf[i-1] != '\n')
470 fputc ('\n', stdout);
473 static void display_record(Z_External *r)
475 oident *ent = oid_getentbyoid(r->direct_reference);
479 * Tell the user what we got.
481 if (r->direct_reference)
483 printf("Record type: ");
485 printf("%s\n", ent->desc);
486 else if (!odr_oid(print, &r->direct_reference, 0, 0))
488 odr_perror(print, "print oid");
492 /* Check if this is a known, ASN.1 type tucked away in an octet string */
493 if (ent && r->which == Z_External_octet)
495 Z_ext_typeent *type = z_ext_getentbyref(ent->value);
501 * Call the given decoder to process the record.
503 odr_setbuf(in, (char*)r->u.octet_aligned->buf,
504 r->u.octet_aligned->len, 0);
505 if (!(*type->fun)(in, (char **)&rr, 0, 0))
507 odr_perror(in, "Decoding constructed record.");
508 fprintf(stderr, "[Near %d]\n", odr_offset(in));
509 fprintf(stderr, "Packet dump:\n---------\n");
510 odr_dumpBER(stderr, (char*)r->u.octet_aligned->buf,
511 r->u.octet_aligned->len);
512 fprintf(stderr, "---------\n");
516 * Note: we throw away the original, BER-encoded record here.
517 * Do something else with it if you want to keep it.
519 r->u.sutrs = (Z_SUTRS *) rr; /* we don't actually check the type here. */
520 r->which = type->what;
523 if (ent && ent->value == VAL_SOIF)
524 print_record((const unsigned char *) r->u.octet_aligned->buf,
525 r->u.octet_aligned->len);
526 else if (r->which == Z_External_octet && r->u.octet_aligned->len)
528 const char *octet_buf = (char*)r->u.octet_aligned->buf;
529 if (ent->value == VAL_TEXT_XML || ent->value == VAL_APPLICATION_XML ||
530 ent->value == VAL_HTML)
532 print_record((const unsigned char *) octet_buf,
533 r->u.octet_aligned->len);
535 else if (ent->value == VAL_POSTSCRIPT)
537 int size = r->u.octet_aligned->len;
540 print_record((const unsigned char *) octet_buf, size);
545 #if AVOID_MARC_DECODE
546 /* primitive check for a marc OID 5.1-29 */
547 ent->oidsuffix[0] == 5 && ent->oidsuffix[1] < 30
553 if (marc_display_exl (octet_buf, NULL, 0 /* debug */,
554 r->u.octet_aligned->len) <= 0)
556 printf ("bad MARC. Dumping as it is:\n");
557 print_record((const unsigned char*) octet_buf,
558 r->u.octet_aligned->len);
563 print_record((const unsigned char*) octet_buf,
564 r->u.octet_aligned->len);
568 fwrite (octet_buf, 1, r->u.octet_aligned->len, marcdump);
570 else if (ent && ent->value == VAL_SUTRS)
572 if (r->which != Z_External_sutrs)
574 printf("Expecting single SUTRS type for SUTRS.\n");
577 print_record(r->u.sutrs->buf, r->u.sutrs->len);
579 else if (ent && ent->value == VAL_GRS1)
581 if (r->which != Z_External_grs1)
583 printf("Expecting single GRS type for GRS.\n");
586 display_grs1(r->u.grs1, 0);
590 printf("Unknown record representation.\n");
591 if (!z_External(print, &r, 0, 0))
593 odr_perror(print, "Printing external");
600 static void display_diagrecs(Z_DiagRec **pp, int num)
604 Z_DefaultDiagFormat *r;
606 printf("Diagnostic message(s) from database:\n");
607 for (i = 0; i<num; i++)
609 Z_DiagRec *p = pp[i];
610 if (p->which != Z_DiagRec_defaultFormat)
612 printf("Diagnostic record not in default format.\n");
616 r = p->u.defaultFormat;
617 if (!(ent = oid_getentbyoid(r->diagnosticSetId)) ||
618 ent->oclass != CLASS_DIAGSET || ent->value != VAL_BIB1)
619 printf("Missing or unknown diagset\n");
620 printf(" [%d] %s", *r->condition, diagbib1_str(*r->condition));
623 case Z_DefaultDiagFormat_v2Addinfo:
624 printf (" -- v2 addinfo '%s'\n", r->u.v2Addinfo);
626 case Z_DefaultDiagFormat_v3Addinfo:
627 printf (" -- v3 addinfo '%s'\n", r->u.v3Addinfo);
634 static void display_nameplusrecord(Z_NamePlusRecord *p)
637 printf("[%s]", p->databaseName);
638 if (p->which == Z_NamePlusRecord_surrogateDiagnostic)
639 display_diagrecs(&p->u.surrogateDiagnostic, 1);
640 else if (p->which == Z_NamePlusRecord_databaseRecord)
641 display_record(p->u.databaseRecord);
644 static void display_records(Z_Records *p)
648 if (p->which == Z_Records_NSD)
650 Z_DiagRec dr, *dr_p = &dr;
651 dr.which = Z_DiagRec_defaultFormat;
652 dr.u.defaultFormat = p->u.nonSurrogateDiagnostic;
653 display_diagrecs (&dr_p, 1);
655 else if (p->which == Z_Records_multipleNSD)
656 display_diagrecs (p->u.multipleNonSurDiagnostics->diagRecs,
657 p->u.multipleNonSurDiagnostics->num_diagRecs);
660 printf("Records: %d\n", p->u.databaseOrSurDiagnostics->num_records);
661 for (i = 0; i < p->u.databaseOrSurDiagnostics->num_records; i++)
662 display_nameplusrecord(p->u.databaseOrSurDiagnostics->records[i]);
666 static int send_deleteResultSetRequest(char *arg)
671 Z_APDU *apdu = zget_APDU(out, Z_APDU_deleteResultSetRequest);
672 Z_DeleteResultSetRequest *req = apdu->u.deleteResultSetRequest;
674 req->referenceId = set_refid (out);
676 req->num_resultSetList =
677 sscanf (arg, "%30s %30s %30s %30s %30s %30s %30s %30s",
678 names[0], names[1], names[2], names[3],
679 names[4], names[5], names[6], names[7]);
681 req->deleteFunction = (int *)
682 odr_malloc (out, sizeof(*req->deleteFunction));
683 if (req->num_resultSetList > 0)
685 *req->deleteFunction = Z_DeleteRequest_list;
686 req->resultSetList = (char **)
687 odr_malloc (out, sizeof(*req->resultSetList)*
688 req->num_resultSetList);
689 for (i = 0; i<req->num_resultSetList; i++)
690 req->resultSetList[i] = names[i];
694 *req->deleteFunction = Z_DeleteRequest_all;
695 req->resultSetList = 0;
699 printf("Sent deleteResultSetRequest.\n");
703 static int send_searchRequest(char *arg)
705 Z_APDU *apdu = zget_APDU(out, Z_APDU_searchRequest);
706 Z_SearchRequest *req = apdu->u.searchRequest;
710 struct ccl_rpn_node *rpn = NULL;
714 Z_RPNQuery *RPNquery;
718 if (queryType == QueryType_CCL2RPN)
720 rpn = ccl_find_str(bibset, arg, &error, &pos);
723 printf("CCL ERROR: %s\n", ccl_err_msg(error));
728 req->referenceId = set_refid (out);
729 if (!strcmp(arg, "@big")) /* strictly for troublemaking */
731 static unsigned char big[2100];
734 /* send a very big referenceid to test transport stack etc. */
735 memset(big, 'A', 2100);
736 bigo.len = bigo.size = 2100;
738 req->referenceId = &bigo;
743 sprintf(setstring, "%d", ++setnumber);
744 req->resultSetName = setstring;
746 *req->smallSetUpperBound = smallSetUpperBound;
747 *req->largeSetLowerBound = largeSetLowerBound;
748 *req->mediumSetPresentNumber = mediumSetPresentNumber;
749 if (smallSetUpperBound > 0 || (largeSetLowerBound > 1 &&
750 mediumSetPresentNumber > 0))
754 prefsyn.proto = protocol;
755 prefsyn.oclass = CLASS_RECSYN;
756 prefsyn.value = recordsyntax;
757 req->preferredRecordSyntax =
758 odr_oiddup(out, oid_ent_to_oid(&prefsyn, oid));
759 req->smallSetElementSetNames =
760 req->mediumSetElementSetNames = elementSetNames;
762 req->num_databaseNames = num_databaseNames;
763 req->databaseNames = databaseNames;
769 case QueryType_Prefix:
770 query.which = Z_Query_type_1;
771 RPNquery = p_query_rpn (out, protocol, arg);
774 printf("Prefix query error\n");
777 query.u.type_1 = RPNquery;
780 query.which = Z_Query_type_2;
781 query.u.type_2 = &ccl_query;
782 ccl_query.buf = (unsigned char*) arg;
783 ccl_query.len = strlen(arg);
786 case QueryType_CCL2RPN:
787 query.which = Z_Query_type_1;
788 RPNquery = ccl_rpn_query(out, rpn);
791 printf ("Couldn't convert from CCL to RPN\n");
794 query.u.type_1 = RPNquery;
795 ccl_rpn_delete (rpn);
799 printf ("Unsupported query type\n");
804 printf("Sent searchRequest.\n");
808 static int process_searchResponse(Z_SearchResponse *res)
810 printf ("Received SearchResponse.\n");
811 print_refid (res->referenceId);
812 if (*res->searchStatus)
813 printf("Search was a success.\n");
815 printf("Search was a bloomin' failure.\n");
816 printf("Number of hits: %d", *res->resultCount);
818 printf (", setno %d", setnumber);
819 printf("\nrecords returned: %d\n",
820 *res->numberOfRecordsReturned);
821 setno += *res->numberOfRecordsReturned;
823 display_records(res->records);
827 static void print_level(int iLevel)
830 for (i = 0; i < iLevel * 4; i++)
834 static void print_int(int iLevel, const char *pTag, int *pInt)
839 printf("%s: %d\n", pTag, *pInt);
843 static void print_string(int iLevel, const char *pTag, const char *pString)
848 printf("%s: %s\n", pTag, pString);
852 static void print_oid(int iLevel, const char *pTag, Odr_oid *pOid)
860 for (; *pInt != -1; pInt++)
861 printf(" %d", *pInt);
866 static void print_referenceId(int iLevel, Z_ReferenceId *referenceId)
868 if (referenceId != NULL)
873 printf("Ref Id (%d, %d): ", referenceId->len, referenceId->size);
874 for (i = 0; i < referenceId->len; i++)
875 printf("%c", referenceId->buf[i]);
880 static void print_string_or_numeric(int iLevel, const char *pTag, Z_StringOrNumeric *pStringNumeric)
882 if (pStringNumeric != NULL)
884 switch (pStringNumeric->which)
886 case Z_StringOrNumeric_string:
887 print_string(iLevel, pTag, pStringNumeric->u.string);
890 case Z_StringOrNumeric_numeric:
891 print_int(iLevel, pTag, pStringNumeric->u.numeric);
896 printf("%s: valid type for Z_StringOrNumeric\n", pTag);
902 static void print_universe_report_duplicate(int iLevel, Z_UniverseReportDuplicate *pUniverseReportDuplicate)
904 if (pUniverseReportDuplicate != NULL)
907 printf("Universe Report Duplicate: \n");
909 print_string_or_numeric(iLevel, "Hit No", pUniverseReportDuplicate->hitno);
913 static void print_universe_report_hits(int iLevel, Z_UniverseReportHits *pUniverseReportHits)
915 if (pUniverseReportHits != NULL)
918 printf("Universe Report Hits: \n");
920 print_string_or_numeric(iLevel, "Database", pUniverseReportHits->database);
921 print_string_or_numeric(iLevel, "Hits", pUniverseReportHits->hits);
925 static void print_universe_report(int iLevel, Z_UniverseReport *pUniverseReport)
927 if (pUniverseReport != NULL)
930 printf("Universe Report: \n");
932 print_int(iLevel, "Total Hits", pUniverseReport->totalHits);
933 switch (pUniverseReport->which)
935 case Z_UniverseReport_databaseHits:
936 print_universe_report_hits(iLevel, pUniverseReport->u.databaseHits);
939 case Z_UniverseReport_duplicate:
940 print_universe_report_duplicate(iLevel, pUniverseReport->u.duplicate);
945 printf("Type: %d\n", pUniverseReport->which);
951 static void print_external(int iLevel, Z_External *pExternal)
953 if (pExternal != NULL)
956 printf("External: \n");
958 print_oid(iLevel, "Direct Reference", pExternal->direct_reference);
959 print_int(iLevel, "InDirect Reference", pExternal->indirect_reference);
960 print_string(iLevel, "Descriptor", pExternal->descriptor);
961 switch (pExternal->which)
963 case Z_External_universeReport:
964 print_universe_report(iLevel, pExternal->u.universeReport);
969 printf("Type: %d\n", pExternal->which);
975 static int process_resourceControlRequest (Z_ResourceControlRequest *req)
977 printf ("Received ResourceControlRequest.\n");
978 print_referenceId(1, req->referenceId);
979 print_int(1, "Suspended Flag", req->suspendedFlag);
980 print_int(1, "Partial Results Available", req->partialResultsAvailable);
981 print_int(1, "Response Required", req->responseRequired);
982 print_int(1, "Triggered Request Flag", req->triggeredRequestFlag);
983 print_external(1, req->resourceReport);
987 void process_ESResponse(Z_ExtendedServicesResponse *res)
990 switch (*res->operationStatus)
992 case Z_ExtendedServicesResponse_done:
995 case Z_ExtendedServicesResponse_accepted:
996 printf ("accepted\n");
998 case Z_ExtendedServicesResponse_failure:
999 printf ("failure\n");
1000 display_diagrecs(res->diagnostics, res->num_diagnostics);
1003 printf ("unknown\n");
1005 if ( (*res->operationStatus != Z_ExtendedServicesResponse_failure) &&
1006 (res->num_diagnostics != 0) ) {
1007 display_diagrecs(res->diagnostics, res->num_diagnostics);
1009 print_refid (res->referenceId);
1010 if (res->taskPackage &&
1011 res->taskPackage->which == Z_External_extendedService)
1013 Z_TaskPackage *taskPackage = res->taskPackage->u.extendedService;
1014 Odr_oct *id = taskPackage->targetReference;
1015 Z_External *ext = taskPackage->taskSpecificParameters;
1019 printf ("Target Reference: ");
1020 print_stringn (id->buf, id->len);
1023 if (ext->which == Z_External_update)
1025 Z_IUUpdateTaskPackage *utp = ext->u.update->u.taskPackage;
1026 if (utp && utp->targetPart)
1028 Z_IUTargetPart *targetPart = utp->targetPart;
1031 for (i = 0; i<targetPart->num_taskPackageRecords; i++)
1034 Z_IUTaskPackageRecordStructure *tpr =
1035 targetPart->taskPackageRecords[i];
1036 printf ("task package record %d\n", i+1);
1037 if (tpr->which == Z_IUTaskPackageRecordStructure_record)
1039 display_record (tpr->u.record);
1043 printf ("other type\n");
1053 const char *get_ill_element (void *clientData, const char *element)
1058 static Z_External *create_external_itemRequest()
1060 struct ill_get_ctl ctl;
1061 ILL_ItemRequest *req;
1063 int item_request_size = 0;
1064 char *item_request_buf = 0;
1068 ctl.f = get_ill_element;
1070 req = ill_get_ItemRequest(&ctl, "ill", 0);
1072 printf ("ill_get_ItemRequest failed\n");
1074 if (!ill_ItemRequest (out, &req, 0, 0))
1078 ill_ItemRequest(print, &req, 0, 0);
1081 item_request_buf = odr_getbuf (out, &item_request_size, 0);
1082 if (item_request_buf)
1083 odr_setbuf (out, item_request_buf, item_request_size, 1);
1084 printf ("Couldn't encode ItemRequest, size %d\n", item_request_size);
1091 item_request_buf = odr_getbuf (out, &item_request_size, 0);
1092 oid.proto = PROTO_GENERAL;
1093 oid.oclass = CLASS_GENERAL;
1094 oid.value = VAL_ISO_ILL_1;
1096 r = (Z_External *) odr_malloc (out, sizeof(*r));
1097 r->direct_reference = odr_oiddup(out,oid_getoidbyent(&oid));
1098 r->indirect_reference = 0;
1100 r->which = Z_External_single;
1102 r->u.single_ASN1_type = (Odr_oct *)
1103 odr_malloc (out, sizeof(*r->u.single_ASN1_type));
1104 r->u.single_ASN1_type->buf = (unsigned char *)
1105 odr_malloc (out, item_request_size);
1106 r->u.single_ASN1_type->len = item_request_size;
1107 r->u.single_ASN1_type->size = item_request_size;
1108 memcpy (r->u.single_ASN1_type->buf, item_request_buf,
1110 printf ("len = %d\n", item_request_size);
1116 #ifdef YAZ_MODULE_ill
1117 static Z_External *create_external_ILL_APDU(int which)
1119 struct ill_get_ctl ctl;
1122 int ill_request_size = 0;
1123 char *ill_request_buf = 0;
1127 ctl.f = get_ill_element;
1129 ill_apdu = ill_get_APDU(&ctl, "ill", 0);
1131 if (!ill_APDU (out, &ill_apdu, 0, 0))
1135 printf ("-------------------\n");
1136 ill_APDU(print, &ill_apdu, 0, 0);
1138 printf ("-------------------\n");
1140 ill_request_buf = odr_getbuf (out, &ill_request_size, 0);
1141 if (ill_request_buf)
1142 odr_setbuf (out, ill_request_buf, ill_request_size, 1);
1143 printf ("Couldn't encode ILL-Request, size %d\n", ill_request_size);
1149 ill_request_buf = odr_getbuf (out, &ill_request_size, 0);
1151 oid.proto = PROTO_GENERAL;
1152 oid.oclass = CLASS_GENERAL;
1153 oid.value = VAL_ISO_ILL_1;
1155 r = (Z_External *) odr_malloc (out, sizeof(*r));
1156 r->direct_reference = odr_oiddup(out,oid_getoidbyent(&oid));
1157 r->indirect_reference = 0;
1159 r->which = Z_External_single;
1161 r->u.single_ASN1_type = (Odr_oct *)
1162 odr_malloc (out, sizeof(*r->u.single_ASN1_type));
1163 r->u.single_ASN1_type->buf = (unsigned char *)
1164 odr_malloc (out, ill_request_size);
1165 r->u.single_ASN1_type->len = ill_request_size;
1166 r->u.single_ASN1_type->size = ill_request_size;
1167 memcpy (r->u.single_ASN1_type->buf, ill_request_buf, ill_request_size);
1168 printf ("len = %d\n", ill_request_size);
1175 static Z_External *create_ItemOrderExternal(const char *type, int itemno)
1177 Z_External *r = (Z_External *) odr_malloc(out, sizeof(Z_External));
1178 oident ItemOrderRequest;
1180 ItemOrderRequest.proto = PROTO_Z3950;
1181 ItemOrderRequest.oclass = CLASS_EXTSERV;
1182 ItemOrderRequest.value = VAL_ITEMORDER;
1184 r->direct_reference = odr_oiddup(out,oid_getoidbyent(&ItemOrderRequest));
1185 r->indirect_reference = 0;
1188 r->which = Z_External_itemOrder;
1190 r->u.itemOrder = (Z_ItemOrder *) odr_malloc(out,sizeof(Z_ItemOrder));
1191 memset(r->u.itemOrder, 0, sizeof(Z_ItemOrder));
1192 r->u.itemOrder->which=Z_IOItemOrder_esRequest;
1194 r->u.itemOrder->u.esRequest = (Z_IORequest *)
1195 odr_malloc(out,sizeof(Z_IORequest));
1196 memset(r->u.itemOrder->u.esRequest, 0, sizeof(Z_IORequest));
1198 r->u.itemOrder->u.esRequest->toKeep = (Z_IOOriginPartToKeep *)
1199 odr_malloc(out,sizeof(Z_IOOriginPartToKeep));
1200 memset(r->u.itemOrder->u.esRequest->toKeep, 0, sizeof(Z_IOOriginPartToKeep));
1201 r->u.itemOrder->u.esRequest->notToKeep = (Z_IOOriginPartNotToKeep *)
1202 odr_malloc(out,sizeof(Z_IOOriginPartNotToKeep));
1203 memset(r->u.itemOrder->u.esRequest->notToKeep, 0, sizeof(Z_IOOriginPartNotToKeep));
1205 r->u.itemOrder->u.esRequest->toKeep->supplDescription = NULL;
1206 r->u.itemOrder->u.esRequest->toKeep->contact = NULL;
1207 r->u.itemOrder->u.esRequest->toKeep->addlBilling = NULL;
1209 r->u.itemOrder->u.esRequest->notToKeep->resultSetItem =
1210 (Z_IOResultSetItem *) odr_malloc(out, sizeof(Z_IOResultSetItem));
1211 memset(r->u.itemOrder->u.esRequest->notToKeep->resultSetItem, 0, sizeof(Z_IOResultSetItem));
1212 r->u.itemOrder->u.esRequest->notToKeep->resultSetItem->resultSetId = "1";
1214 r->u.itemOrder->u.esRequest->notToKeep->resultSetItem->item =
1215 (int *) odr_malloc(out, sizeof(int));
1216 *r->u.itemOrder->u.esRequest->notToKeep->resultSetItem->item = itemno;
1219 if (!strcmp (type, "item") || !strcmp(type, "2"))
1221 printf ("using item-request\n");
1222 r->u.itemOrder->u.esRequest->notToKeep->itemRequest =
1223 create_external_itemRequest();
1225 else if (!strcmp(type, "ill") || !strcmp(type, "1"))
1227 printf ("using ILL-request\n");
1228 r->u.itemOrder->u.esRequest->notToKeep->itemRequest =
1229 create_external_ILL_APDU(ILL_APDU_ILL_Request);
1231 else if (!strcmp(type, "xml") || !strcmp(type, "3"))
1233 const char *xml_buf =
1235 " <type>request</type>\n"
1236 " <libraryNo>000200</libraryNo>\n"
1237 " <borrowerTicketNo> 1212 </borrowerTicketNo>\n"
1239 r->u.itemOrder->u.esRequest->notToKeep->itemRequest =
1240 z_ext_record (out, VAL_TEXT_XML, xml_buf, strlen(xml_buf));
1243 r->u.itemOrder->u.esRequest->notToKeep->itemRequest = 0;
1246 r->u.itemOrder->u.esRequest->notToKeep->itemRequest = 0;
1251 static int send_itemorder(const char *type, int itemno)
1253 Z_APDU *apdu = zget_APDU(out, Z_APDU_extendedServicesRequest);
1254 Z_ExtendedServicesRequest *req = apdu->u.extendedServicesRequest;
1255 oident ItemOrderRequest;
1257 ItemOrderRequest.proto = PROTO_Z3950;
1258 ItemOrderRequest.oclass = CLASS_EXTSERV;
1259 ItemOrderRequest.value = VAL_ITEMORDER;
1260 req->packageType = odr_oiddup(out,oid_getoidbyent(&ItemOrderRequest));
1261 req->packageName = esPackageName;
1263 req->taskSpecificParameters = create_ItemOrderExternal(type, itemno);
1269 static int cmd_update(char *arg)
1271 Z_APDU *apdu = zget_APDU(out, Z_APDU_extendedServicesRequest );
1272 Z_ExtendedServicesRequest *req = apdu->u.extendedServicesRequest;
1275 Z_IUOriginPartToKeep *toKeep;
1276 Z_IUSuppliedRecords *notToKeep;
1278 printf ("Update request\n");
1283 update_oid.proto = PROTO_Z3950;
1284 update_oid.oclass = CLASS_EXTSERV;
1285 update_oid.value = VAL_DBUPDATE;
1286 oid_ent_to_oid (&update_oid, oid);
1287 req->packageType = odr_oiddup(out,oid);
1288 req->packageName = esPackageName;
1290 req->referenceId = set_refid (out);
1292 r = req->taskSpecificParameters = (Z_External *)
1293 odr_malloc (out, sizeof(*r));
1294 r->direct_reference = odr_oiddup(out,oid);
1295 r->indirect_reference = 0;
1297 r->which = Z_External_update;
1298 r->u.update = (Z_IUUpdate *) odr_malloc(out, sizeof(*r->u.update));
1299 r->u.update->which = Z_IUUpdate_esRequest;
1300 r->u.update->u.esRequest = (Z_IUUpdateEsRequest *)
1301 odr_malloc(out, sizeof(*r->u.update->u.esRequest));
1302 toKeep = r->u.update->u.esRequest->toKeep = (Z_IUOriginPartToKeep *)
1303 odr_malloc(out, sizeof(*r->u.update->u.esRequest->toKeep));
1304 toKeep->databaseName = databaseNames[0];
1306 toKeep->elementSetName = 0;
1307 toKeep->actionQualifier = 0;
1308 toKeep->action = (int *) odr_malloc(out, sizeof(*toKeep->action));
1309 *toKeep->action = Z_IUOriginPartToKeep_recordInsert;
1311 notToKeep = r->u.update->u.esRequest->notToKeep = (Z_IUSuppliedRecords *)
1312 odr_malloc(out, sizeof(*r->u.update->u.esRequest->notToKeep));
1314 notToKeep->elements = (Z_IUSuppliedRecords_elem **)
1315 odr_malloc(out, sizeof(*notToKeep->elements));
1316 notToKeep->elements[0] = (Z_IUSuppliedRecords_elem *)
1317 odr_malloc(out, sizeof(**notToKeep->elements));
1318 notToKeep->elements[0]->u.number = 0;
1319 notToKeep->elements[0]->supplementalId = 0;
1320 notToKeep->elements[0]->correlationInfo = 0;
1321 notToKeep->elements[0]->record = record_last;
1328 /* II : Added to do DALI Item Order Extended services request */
1329 static int cmd_itemorder(char *arg)
1334 if (sscanf (arg, "%10s %d", type, &itemno) != 2)
1337 printf("Item order request\n");
1339 send_itemorder(type, itemno);
1343 static int cmd_find(char *arg)
1347 printf("Find what?\n");
1352 printf("Not connected yet\n");
1355 if (!send_searchRequest(arg))
1360 static int cmd_delete(char *arg)
1364 printf("Not connected yet\n");
1367 if (!send_deleteResultSetRequest(arg))
1372 static int cmd_ssub(char *arg)
1374 if (!(smallSetUpperBound = atoi(arg)))
1379 static int cmd_lslb(char *arg)
1381 if (!(largeSetLowerBound = atoi(arg)))
1386 static int cmd_mspn(char *arg)
1388 if (!(mediumSetPresentNumber = atoi(arg)))
1393 static int cmd_status(char *arg)
1395 printf("smallSetUpperBound: %d\n", smallSetUpperBound);
1396 printf("largeSetLowerBound: %d\n", largeSetLowerBound);
1397 printf("mediumSetPresentNumber: %d\n", mediumSetPresentNumber);
1401 static int cmd_setnames(char *arg)
1403 if (*arg == '1') /* enable ? */
1405 else if (*arg == '0') /* disable ? */
1407 else if (setnumber < 0) /* no args, toggle .. */
1413 printf("Set numbering enabled.\n");
1415 printf("Set numbering disabled.\n");
1419 /* PRESENT SERVICE ----------------------------- */
1421 static int send_presentRequest(char *arg)
1423 Z_APDU *apdu = zget_APDU(out, Z_APDU_presentRequest);
1424 Z_PresentRequest *req = apdu->u.presentRequest;
1425 Z_RecordComposition compo;
1430 char setstring[100];
1432 req->referenceId = set_refid (out);
1433 if ((p = strchr(arg, '+')))
1440 if (p && (p=strchr(p+1, '+')))
1442 strcpy (setstring, p+1);
1443 req->resultSetId = setstring;
1445 else if (setnumber >= 0)
1447 sprintf(setstring, "%d", setnumber);
1448 req->resultSetId = setstring;
1453 static Z_Range range;
1454 static Z_Range *rangep = ⦥
1455 req->num_ranges = 1;
1457 req->resultSetStartPoint = &setno;
1458 req->numberOfRecordsRequested = &nos;
1459 prefsyn.proto = protocol;
1460 prefsyn.oclass = CLASS_RECSYN;
1461 prefsyn.value = recordsyntax;
1462 req->preferredRecordSyntax =
1463 odr_oiddup (out, oid_ent_to_oid(&prefsyn, oid));
1465 if (schema != VAL_NONE)
1469 prefschema.proto = protocol;
1470 prefschema.oclass = CLASS_SCHEMA;
1471 prefschema.value = schema;
1473 req->recordComposition = &compo;
1474 compo.which = Z_RecordComp_complex;
1475 compo.u.complex = (Z_CompSpec *)
1476 odr_malloc(out, sizeof(*compo.u.complex));
1477 compo.u.complex->selectAlternativeSyntax = (bool_t *)
1478 odr_malloc(out, sizeof(bool_t));
1479 *compo.u.complex->selectAlternativeSyntax = 0;
1481 compo.u.complex->generic = (Z_Specification *)
1482 odr_malloc(out, sizeof(*compo.u.complex->generic));
1483 compo.u.complex->generic->schema = (Odr_oid *)
1484 odr_oiddup(out, oid_ent_to_oid(&prefschema, oid));
1485 if (!compo.u.complex->generic->schema)
1487 /* OID wasn't a schema! Try record syntax instead. */
1488 prefschema.oclass = CLASS_RECSYN;
1489 compo.u.complex->generic->schema = (Odr_oid *)
1490 odr_oiddup(out, oid_ent_to_oid(&prefschema, oid));
1492 if (!elementSetNames)
1493 compo.u.complex->generic->elementSpec = 0;
1496 compo.u.complex->generic->elementSpec = (Z_ElementSpec *)
1497 odr_malloc(out, sizeof(Z_ElementSpec));
1498 compo.u.complex->generic->elementSpec->which =
1499 Z_ElementSpec_elementSetName;
1500 compo.u.complex->generic->elementSpec->u.elementSetName =
1501 elementSetNames->u.generic;
1503 compo.u.complex->num_dbSpecific = 0;
1504 compo.u.complex->dbSpecific = 0;
1505 compo.u.complex->num_recordSyntax = 0;
1506 compo.u.complex->recordSyntax = 0;
1508 else if (elementSetNames)
1510 req->recordComposition = &compo;
1511 compo.which = Z_RecordComp_simple;
1512 compo.u.simple = elementSetNames;
1515 printf("Sent presentRequest (%d+%d).\n", setno, nos);
1519 void process_close(Z_Close *req)
1521 Z_APDU *apdu = zget_APDU(out, Z_APDU_close);
1522 Z_Close *res = apdu->u.close;
1524 static char *reasons[] =
1529 "cost limit reached",
1531 "security violation",
1538 printf("Reason: %s, message: %s\n", reasons[*req->closeReason],
1539 req->diagnosticInformation ? req->diagnosticInformation : "NULL");
1546 nmem_destroy (session_mem);
1553 *res->closeReason = Z_Close_finished;
1555 printf("Sent response.\n");
1560 static int cmd_show(char *arg)
1564 printf("Not connected yet\n");
1567 if (!send_presentRequest(arg))
1572 int cmd_quit(char *arg)
1574 printf("See you later, alligator.\n");
1580 int cmd_cancel(char *arg)
1582 Z_APDU *apdu = zget_APDU(out, Z_APDU_triggerResourceControlRequest);
1583 Z_TriggerResourceControlRequest *req =
1584 apdu->u.triggerResourceControlRequest;
1589 printf("Session not initialized yet\n");
1592 if (!ODR_MASK_GET(session->options, Z_Options_triggerResourceCtrl))
1594 printf("Target doesn't support cancel (trigger resource ctrl)\n");
1597 *req->requestedAction = Z_TriggerResourceCtrl_cancel;
1598 req->resultSetWanted = &rfalse;
1601 printf("Sent cancel request\n");
1605 int send_scanrequest(const char *query, int pp, int num, const char *term)
1607 Z_APDU *apdu = zget_APDU(out, Z_APDU_scanRequest);
1608 Z_ScanRequest *req = apdu->u.scanRequest;
1613 if (queryType == QueryType_CCL2RPN)
1617 struct ccl_rpn_node *rpn;
1619 rpn = ccl_find_str (bibset, query, &error, &pos);
1622 printf("CCL ERROR: %s\n", ccl_err_msg(error));
1626 bib1.proto = PROTO_Z3950;
1627 bib1.oclass = CLASS_ATTSET;
1628 bib1.value = VAL_BIB1;
1629 req->attributeSet = oid_ent_to_oid (&bib1, oid);
1630 if (!(req->termListAndStartPoint = ccl_scan_query (out, rpn)))
1632 printf("Couldn't convert CCL to Scan term\n");
1635 ccl_rpn_delete (rpn);
1638 if (use_rpn && !(req->termListAndStartPoint =
1639 p_query_scan(out, protocol, &req->attributeSet, query)))
1641 printf("Prefix query error\n");
1646 if (req->termListAndStartPoint->term &&
1647 req->termListAndStartPoint->term->which == Z_Term_general &&
1648 req->termListAndStartPoint->term->u.general)
1650 req->termListAndStartPoint->term->u.general->buf =
1651 (unsigned char *) odr_strdup(out, term);
1652 req->termListAndStartPoint->term->u.general->len =
1653 req->termListAndStartPoint->term->u.general->size =
1657 req->referenceId = set_refid (out);
1658 req->num_databaseNames = num_databaseNames;
1659 req->databaseNames = databaseNames;
1660 req->numberOfTermsRequested = #
1661 req->preferredPositionInResponse = &pp;
1666 int send_sortrequest(char *arg, int newset)
1668 Z_APDU *apdu = zget_APDU(out, Z_APDU_sortRequest);
1669 Z_SortRequest *req = apdu->u.sortRequest;
1670 Z_SortKeySpecList *sksl = (Z_SortKeySpecList *)
1671 odr_malloc (out, sizeof(*sksl));
1675 sprintf (setstring, "%d", setnumber);
1677 sprintf (setstring, "default");
1679 req->referenceId = set_refid (out);
1681 req->num_inputResultSetNames = 1;
1682 req->inputResultSetNames = (Z_InternationalString **)
1683 odr_malloc (out, sizeof(*req->inputResultSetNames));
1684 req->inputResultSetNames[0] = odr_strdup (out, setstring);
1686 if (newset && setnumber >= 0)
1687 sprintf (setstring, "%d", ++setnumber);
1689 req->sortedResultSetName = odr_strdup (out, setstring);
1691 req->sortSequence = yaz_sort_spec (out, arg);
1692 if (!req->sortSequence)
1694 printf ("Missing sort specifications\n");
1701 void display_term(Z_TermInfo *t)
1703 if (t->term->which == Z_Term_general)
1705 printf("%.*s", t->term->u.general->len, t->term->u.general->buf);
1706 sprintf(last_scan_line, "%.*s", t->term->u.general->len,
1707 t->term->u.general->buf);
1710 printf("Term (not general)");
1711 if (t->globalOccurrences)
1712 printf (" (%d)\n", *t->globalOccurrences);
1717 void process_scanResponse(Z_ScanResponse *res)
1720 Z_Entry **entries = NULL;
1721 int num_entries = 0;
1723 printf("Received ScanResponse\n");
1724 print_refid (res->referenceId);
1725 printf("%d entries", *res->numberOfEntriesReturned);
1726 if (res->positionOfTerm)
1727 printf (", position=%d", *res->positionOfTerm);
1729 if (*res->scanStatus != Z_Scan_success)
1730 printf("Scan returned code %d\n", *res->scanStatus);
1733 if ((entries = res->entries->entries))
1734 num_entries = res->entries->num_entries;
1735 for (i = 0; i < num_entries; i++)
1737 int pos_term = res->positionOfTerm ? *res->positionOfTerm : -1;
1738 if (entries[i]->which == Z_Entry_termInfo)
1740 printf("%c ", i + 1 == pos_term ? '*' : ' ');
1741 display_term(entries[i]->u.termInfo);
1744 display_diagrecs(&entries[i]->u.surrogateDiagnostic, 1);
1746 if (res->entries->nonsurrogateDiagnostics)
1747 display_diagrecs (res->entries->nonsurrogateDiagnostics,
1748 res->entries->num_nonsurrogateDiagnostics);
1751 void process_sortResponse(Z_SortResponse *res)
1753 printf("Received SortResponse: status=");
1754 switch (*res->sortStatus)
1756 case Z_SortStatus_success:
1757 printf ("success"); break;
1758 case Z_SortStatus_partial_1:
1759 printf ("partial"); break;
1760 case Z_SortStatus_failure:
1761 printf ("failure"); break;
1763 printf ("unknown (%d)", *res->sortStatus);
1766 print_refid (res->referenceId);
1767 if (res->diagnostics)
1768 display_diagrecs(res->diagnostics,
1769 res->num_diagnostics);
1772 void process_deleteResultSetResponse (Z_DeleteResultSetResponse *res)
1774 printf("Got deleteResultSetResponse status=%d\n",
1775 *res->deleteOperationStatus);
1776 if (res->deleteListStatuses)
1779 for (i = 0; i < res->deleteListStatuses->num; i++)
1781 printf ("%s status=%d\n", res->deleteListStatuses->elements[i]->id,
1782 *res->deleteListStatuses->elements[i]->status);
1787 int cmd_sort_generic(char *arg, int newset)
1791 printf("Session not initialized yet\n");
1794 if (!ODR_MASK_GET(session->options, Z_Options_sort))
1796 printf("Target doesn't support sort\n");
1801 if (send_sortrequest(arg, newset) < 0)
1808 int cmd_sort(char *arg)
1810 return cmd_sort_generic (arg, 0);
1813 int cmd_sort_newset (char *arg)
1815 return cmd_sort_generic (arg, 1);
1818 int cmd_scan(char *arg)
1822 printf("Session not initialized yet\n");
1825 if (!ODR_MASK_GET(session->options, Z_Options_scan))
1827 printf("Target doesn't support scan\n");
1832 strcpy (last_scan_query, arg);
1833 if (send_scanrequest(arg, 1, 20, 0) < 0)
1838 if (send_scanrequest(last_scan_query, 1, 20, last_scan_line) < 0)
1844 int cmd_schema(char *arg)
1851 schema = oid_getvalbyname (arg);
1852 if (schema == VAL_NONE)
1854 printf ("unknown schema\n");
1860 int cmd_format(char *arg)
1864 printf("Usage: format <recordsyntax>\n");
1867 recordsyntax = oid_getvalbyname (arg);
1868 if (recordsyntax == VAL_NONE)
1870 printf ("unknown record syntax\n");
1876 int cmd_elements(char *arg)
1878 static Z_ElementSetNames esn;
1879 static char what[100];
1883 elementSetNames = 0;
1887 esn.which = Z_ElementSetNames_generic;
1888 esn.u.generic = what;
1889 elementSetNames = &esn;
1893 int cmd_attributeset(char *arg)
1899 printf("Usage: attributeset <setname>\n");
1902 sscanf(arg, "%s", what);
1903 if (p_query_attset (what))
1905 printf("Unknown attribute set name\n");
1911 int cmd_querytype (char *arg)
1913 if (!strcmp (arg, "ccl"))
1914 queryType = QueryType_CCL;
1915 else if (!strcmp (arg, "prefix") || !strcmp(arg, "rpn"))
1916 queryType = QueryType_Prefix;
1918 else if (!strcmp (arg, "ccl2rpn") || !strcmp (arg, "cclrpn"))
1919 queryType = QueryType_CCL2RPN;
1923 printf ("Querytype must be one of:\n");
1924 printf (" prefix - Prefix query\n");
1925 printf (" ccl - CCL query\n");
1927 printf (" ccl2rpn - CCL query converted to RPN\n");
1934 int cmd_refid (char *arg)
1940 refid = (char *) xmalloc (strlen(arg)+1);
1941 strcpy (refid, arg);
1946 int cmd_close(char *arg)
1953 apdu = zget_APDU(out, Z_APDU_close);
1954 req = apdu->u.close;
1955 *req->closeReason = Z_Close_finished;
1957 printf("Sent close request.\n");
1962 int cmd_packagename(char* arg) {
1963 xfree (esPackageName);
1964 esPackageName = NULL;
1967 esPackageName = (char *) xmalloc (strlen(arg)+1);
1968 strcpy (esPackageName, arg);
1973 int cmd_proxy(char* arg) {
1978 yazProxy = (char *) xmalloc (strlen(arg)+1);
1979 strcpy (yazProxy, arg);
1984 int cmd_source(char* arg)
1986 /* first should open the file and read one line at a time.. */
1988 char line[1024], *cp;
1991 fprintf(stderr,"Error in source command use a filename");
1995 includeFile = fopen (arg, "r");
1998 fprintf(stderr,"Unable to open file %s for reading\n",arg);
2003 while(!feof(includeFile)) {
2004 memset(line,0,sizeof(line));
2005 fgets(line,sizeof(line),includeFile);
2007 if(strlen(line) < 2) continue;
2008 if(line[0] == '#') continue;
2010 if ((cp = strrchr (line, '\n')))
2013 process_cmd_line(line);
2017 if(fclose(includeFile)<0) {
2018 perror("unable to close include file");
2024 int cmd_subshell(char* args) {
2029 static void initialize(void)
2034 if (!(out = odr_createmem(ODR_ENCODE)) ||
2035 !(in = odr_createmem(ODR_DECODE)) ||
2036 !(print = odr_createmem(ODR_PRINT)))
2038 fprintf(stderr, "failed to allocate ODR streams\n");
2041 setvbuf(stdout, 0, _IONBF, 0);
2043 odr_setprint(print, apdu_file);
2046 bibset = ccl_qual_mk ();
2047 inf = fopen (ccl_fields, "r");
2050 ccl_qual_file (bibset, inf);
2054 cmd_base("Default");
2058 #if HAVE_GETTIMEOFDAY
2059 struct timeval tv_start, tv_end;
2062 void wait_and_handle_responce()
2067 int netbufferlen = 0;
2073 && FD_ISSET(cs_fileno(conn), &input)
2079 if ((res = cs_get(conn, &netbuffer, &netbufferlen)) < 0)
2086 printf("Target closed connection.\n");
2089 odr_reset(in); /* release APDU from last round */
2091 odr_setbuf(in, netbuffer, res, 0);
2092 if (!z_APDU(in, &apdu, 0, 0))
2094 odr_perror(in, "Decoding incoming APDU");
2095 fprintf(stderr, "[Near %d]\n", odr_offset(in));
2096 fprintf(stderr, "Packet dump:\n---------\n");
2097 odr_dumpBER(stderr, netbuffer, res);
2098 fprintf(stderr, "---------\n");
2100 z_APDU(print, &apdu, 0, 0);
2103 if (apdu_file && !z_APDU(print, &apdu, 0, 0))
2105 odr_perror(print, "Failed to print incoming APDU");
2111 case Z_APDU_initResponse:
2112 process_initResponse(apdu->u.initResponse);
2114 case Z_APDU_searchResponse:
2115 process_searchResponse(apdu->u.searchResponse);
2117 case Z_APDU_scanResponse:
2118 process_scanResponse(apdu->u.scanResponse);
2120 case Z_APDU_presentResponse:
2121 print_refid (apdu->u.presentResponse->referenceId);
2123 *apdu->u.presentResponse->numberOfRecordsReturned;
2124 if (apdu->u.presentResponse->records)
2125 display_records(apdu->u.presentResponse->records);
2127 printf("No records.\n");
2128 printf ("nextResultSetPosition = %d\n",
2129 *apdu->u.presentResponse->nextResultSetPosition);
2131 case Z_APDU_sortResponse:
2132 process_sortResponse(apdu->u.sortResponse);
2134 case Z_APDU_extendedServicesResponse:
2135 printf("Got extended services response\n");
2136 process_ESResponse(apdu->u.extendedServicesResponse);
2139 printf("Target has closed the association.\n");
2140 process_close(apdu->u.close);
2142 case Z_APDU_resourceControlRequest:
2143 process_resourceControlRequest
2144 (apdu->u.resourceControlRequest);
2146 case Z_APDU_deleteResultSetResponse:
2147 process_deleteResultSetResponse(apdu->u.
2148 deleteResultSetResponse);
2151 printf("Received unknown APDU type (%d).\n",
2156 while (conn && cs_more(conn));
2157 #if HAVE_GETTIMEOFDAY
2158 gettimeofday (&tv_end, 0);
2160 printf ("S/U S/U=%ld/%ld %ld/%ld",
2161 (long) tv_start.tv_sec,
2162 (long) tv_start.tv_usec,
2163 (long) tv_end.tv_sec,
2164 (long) tv_end.tv_usec);
2166 printf ("Elapsed: %.6f\n",
2167 (double) tv_end.tv_usec / 1e6 + tv_end.tv_sec -
2168 ((double) tv_start.tv_usec / 1e6 + tv_start.tv_sec));
2173 void process_cmd_line(char* line)
2177 int (*fun)(char *arg);
2180 {"open", cmd_open, "('tcp'|'ssl')':<host>[':'<port>][/<db>]"},
2181 {"quit", cmd_quit, ""},
2182 {"find", cmd_find, "<query>"},
2183 {"delete", cmd_delete, "<setname>"},
2184 {"base", cmd_base, "<base-name>"},
2185 {"show", cmd_show, "<rec#>['+'<#recs>['+'<setname>]]"},
2186 {"scan", cmd_scan, "<term>"},
2187 {"sort", cmd_sort, "<sortkey> <flag> <sortkey> <flag> ..."},
2188 {"sort+", cmd_sort_newset, "<sortkey> <flag> <sortkey> <flag> ..."},
2189 {"authentication", cmd_authentication, "<acctstring>"},
2190 {"lslb", cmd_lslb, "<largeSetLowerBound>"},
2191 {"ssub", cmd_ssub, "<smallSetUpperBound>"},
2192 {"mspn", cmd_mspn, "<mediumSetPresentNumber>"},
2193 {"status", cmd_status, ""},
2194 {"setnames", cmd_setnames, ""},
2195 {"cancel", cmd_cancel, ""},
2196 {"format", cmd_format, "<recordsyntax>"},
2197 {"schema", cmd_schema, "<schema>"},
2198 {"elements", cmd_elements, "<elementSetName>"},
2199 {"close", cmd_close, ""},
2200 {"attributeset", cmd_attributeset, "<attrset>"},
2201 {"querytype", cmd_querytype, "<type>"},
2202 {"refid", cmd_refid, "<id>"},
2203 {"itemorder", cmd_itemorder, "ill|item <itemno>"},
2204 {"update", cmd_update, "<item>"},
2205 {"packagename", cmd_packagename, "<packagename>"},
2206 {"proxy", cmd_proxy, "('tcp'|'osi')':'[<tsel>'/']<host>[':'<port>]"},
2207 {".", cmd_source, "<filename>"},
2208 {"!", cmd_subshell, "Subshell command"},
2209 /* Server Admin Functions */
2210 {"adm-reindex", cmd_adm_reindex, "<database-name>"},
2211 {"adm-truncate", cmd_adm_truncate, "('database'|'index')<object-name>"},
2212 {"adm-create", cmd_adm_create, ""},
2213 {"adm-drop", cmd_adm_drop, "('database'|'index')<object-name>"},
2214 {"adm-import", cmd_adm_import, "<record-type> <dir> <pattern>"},
2215 {"adm-refresh", cmd_adm_refresh, ""},
2216 {"adm-commit", cmd_adm_commit, ""},
2217 {"adm-shutdown", cmd_adm_shutdown, ""},
2218 {"adm-startup", cmd_adm_startup, ""},
2222 char word[32], arg[1024];
2225 #if HAVE_GETTIMEOFDAY
2226 gettimeofday (&tv_start, 0);
2229 if ((res = sscanf(line, "%31s %1023[^;]", word, arg)) <= 0)
2231 strcpy(word, last_cmd);
2236 strcpy(last_cmd, word);
2237 for (i = 0; cmd[i].cmd; i++)
2238 if (!strncmp(cmd[i].cmd, word, strlen(word)))
2240 res = (*cmd[i].fun)(arg);
2243 if (!cmd[i].cmd) /* dump our help-screen */
2245 printf("Unknown command: %s.\n", word);
2246 printf("Currently recognized commands:\n");
2247 for (i = 0; cmd[i].cmd; i++)
2248 printf(" %s %s\n", cmd[i].cmd, cmd[i].ad);
2252 wait_and_handle_responce();
2257 static void client(void)
2260 #if HAVE_GETTIMEOFDAY
2261 gettimeofday (&tv_start, 0);
2272 #if HAVE_READLINE_READLINE_H
2274 line_in=readline(C_PROMPT);
2277 #if HAVE_READLINE_HISTORY_H
2279 add_history(line_in);
2281 strcpy(line,line_in);
2287 if (!fgets(line, 1023, stdin))
2289 if ((end_p = strchr (line, '\n')))
2292 process_cmd_line(line);
2297 int main(int argc, char **argv)
2300 char *open_command = 0;
2301 char *auth_command = 0;
2305 while ((ret = options("k:c:a:m:v:p:u:", argv, argc, &arg)) != -2)
2312 open_command = (char *) xmalloc (strlen(arg)+6);
2313 strcpy (open_command, "open ");
2314 strcat (open_command, arg);
2318 kilobytes = atoi(arg);
2321 if (!(marcdump = fopen (arg, "a")))
2328 strncpy (ccl_fields, arg, sizeof(ccl_fields)-1);
2329 ccl_fields[sizeof(ccl_fields)-1] = '\0';
2332 if (!strcmp(arg, "-"))
2335 apdu_file=fopen(arg, "a");
2338 yazProxy=strdup(arg);
2343 auth_command = (char *) xmalloc (strlen(arg)+6);
2344 strcpy (auth_command, "auth ");
2345 strcat (auth_command, arg);
2349 yaz_log_init (yaz_log_mask_str(arg), "", NULL);
2352 fprintf (stderr, "Usage: %s [-m <marclog>] [ -a <apdulog>] "
2353 "[-c cclfields]\n [-p <proxy-addr>] [-u <auth>] "
2354 "[-k size] [<server-addr>]\n",
2362 #ifdef HAVE_GETTIMEOFDAY
2363 gettimeofday (&tv_start, 0);
2365 process_cmd_line (auth_command);
2366 #if HAVE_READLINE_HISTORY_H
2367 add_history(auth_command);
2369 xfree(auth_command);
2373 #ifdef HAVE_GETTIMEOFDAY
2374 gettimeofday (&tv_start, 0);
2376 process_cmd_line (open_command);
2377 #if HAVE_READLINE_HISTORY_H
2378 add_history(open_command);
2380 xfree(open_command);