2 * Copyright (c) 1995-1998, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.61 1998-02-10 11:03:06 adam
8 * Implemented command refid. Client prints reference-ID's, when present,
11 * Revision 1.60 1998/01/29 14:08:52 adam
12 * Better sort diagnostics.
14 * Revision 1.59 1998/01/29 13:17:56 adam
17 * Revision 1.58 1998/01/07 13:51:45 adam
20 * Revision 1.57 1998/01/07 12:58:22 adam
21 * Using fgets instead of gets.
23 * Revision 1.56 1997/11/05 09:18:31 adam
24 * The client handles records with no associated syntax.
26 * Revision 1.55 1997/10/31 12:20:08 adam
27 * Improved memory debugging for xmalloc/nmem.c. References to NMEM
28 * instead of ODR in n ESPEC-1 handling in source d1_espec.c.
29 * Bug fix: missing fclose in data1_read_espec1.
31 * Revision 1.54 1997/10/27 13:52:46 adam
32 * Header yaz-util includes all YAZ utility header files.
34 * Revision 1.53 1997/09/29 13:18:59 adam
35 * Added function, oid_ent_to_oid, to replace the function
36 * oid_getoidbyent, which is not thread safe.
38 * Revision 1.52 1997/09/29 07:20:31 adam
39 * Client code uses nmem_init.
41 * Revision 1.51 1997/09/26 09:41:55 adam
42 * Updated client to handle multiple diagnostics.
44 * Revision 1.50 1997/09/17 12:10:29 adam
47 * Revision 1.49 1997/09/04 13:45:17 adam
48 * Added UNImarc to list of available syntaxes.
50 * Revision 1.48 1997/09/01 08:48:44 adam
51 * New windows NT/95 port using MSV5.0. Only a few changes made
52 * to avoid warnings. Sub project created: client.dsp.
54 * Revision 1.47 1997/07/01 13:49:56 adam
55 * Take care of case when invalid target is specified on command line.
57 * Revision 1.46 1997/06/23 10:30:18 adam
58 * Added call to ccl_rpn_delete in search. Added ODR stream "out"
59 * as parameter to ccl_rpn_query to release RPN query.
61 * Revision 1.45 1997/05/14 06:53:29 adam
64 * Revision 1.44 1997/05/05 11:20:35 adam
65 * Client uses "options" utility and marc dump filename may be specified
66 * as an option (-m <file>).
68 * Revision 1.43 1996/11/08 11:03:26 adam
69 * Client accepts multiple database names.
71 * Revision 1.42 1996/10/08 10:44:57 quinn
74 * Revision 1.41 1996/10/07 15:29:03 quinn
77 * Revision 1.40 1996/08/29 14:19:34 quinn
78 * Fixed conflict (CVS)
80 * Revision 1.39 1996/08/27 10:43:22 quinn
81 * Made select() optional
83 * Revision 1.38 1996/08/12 14:09:11 adam
84 * Default prefix query attribute set defined by using p_query_attset.
86 * Revision 1.37 1996/07/06 19:58:29 quinn
87 * System headerfiles gathered in yconfig
89 * Revision 1.36 1996/06/10 08:53:47 quinn
92 * Revision 1.35 1996/06/03 09:45:50 quinn
93 * Added display of OIDs in the GRS routine.
95 * Revision 1.34 1996/05/09 07:26:49 quinn
96 * *** empty log message ***
98 * Revision 1.33 1996/05/09 07:25:22 quinn
101 * Revision 1.32 1996/03/15 11:05:33 adam
102 * The user can set the preferred query type (prefix, ccl, ..) with the
105 * Revision 1.31 1996/02/20 12:51:54 quinn
106 * Fixed problems with EXTERNAL.
108 * Revision 1.30 1996/02/12 18:18:09 quinn
111 * Revision 1.29 1996/01/02 08:57:25 quinn
112 * Changed enums in the ASN.1 .h files to #defines. Changed oident.class to oclass
114 * Revision 1.28 1995/12/14 11:09:31 quinn
115 * Added Explain record syntax to the format command.
117 * Revision 1.27 1995/12/12 16:37:02 quinn
118 * Added destroy element to data1_node.
120 * Revision 1.26 1995/12/12 14:11:00 quinn
123 * Revision 1.25 1995/11/13 09:27:22 quinn
124 * Fiddling with the variant stuff.
126 * Revision 1.24 1995/10/30 12:41:13 quinn
127 * Added hostname lookup for server.
129 * Revision 1.23 1995/10/18 16:12:30 quinn
130 * Better diagnostics.
132 * Revision 1.22 1995/10/11 14:49:12 quinn
135 * Revision 1.21 1995/09/29 17:01:47 quinn
138 * Revision 1.20 1995/08/29 14:24:13 quinn
139 * Added second half of close-handshake
141 * Revision 1.19 1995/08/29 11:17:28 quinn
142 * Added code to receive close
144 * Revision 1.18 1995/08/28 12:21:27 quinn
145 * Client can now ask for simple element set names.
147 * Revision 1.17 1995/08/17 12:45:02 quinn
148 * Fixed minor problems with GRS-1. Added support in c&s.
150 * Revision 1.16 1995/08/15 12:00:04 quinn
153 * Revision 1.15 1995/06/22 09:28:03 quinn
154 * Fixed bug in SUTRS processing.
156 * Revision 1.14 1995/06/19 12:37:41 quinn
159 * Revision 1.13 1995/06/16 10:29:11 quinn
160 * *** empty log message ***
162 * Revision 1.12 1995/06/15 07:44:57 quinn
165 * Revision 1.11 1995/06/14 15:26:40 quinn
166 * *** empty log message ***
168 * Revision 1.10 1995/06/06 14:56:58 quinn
169 * Better diagnostics.
171 * Revision 1.9 1995/06/06 08:15:19 quinn
174 * Revision 1.8 1995/06/05 10:52:22 quinn
177 * Revision 1.7 1995/06/02 09:50:09 quinn
180 * Revision 1.6 1995/05/31 08:29:21 quinn
181 * Nothing significant.
183 * Revision 1.5 1995/05/29 08:10:47 quinn
184 * Moved oid.c to util.
186 * Revision 1.4 1995/05/22 15:30:13 adam
187 * Client uses prefix query notation.
189 * Revision 1.3 1995/05/22 15:06:53 quinn
190 * *** empty log message ***
192 * Revision 1.2 1995/05/22 14:56:40 quinn
193 * *** empty log message ***
195 * Revision 1.1 1995/05/22 11:30:31 quinn
196 * Added prettier client.
202 * This is the obligatory little toy client, whose primary purpose is
203 * to illustrate the use of the YAZ service-level API.
211 #include <yaz-util.h>
213 #include <comstack.h>
220 #include <marcdisp.h>
221 #include <diagbib1.h>
229 #define C_PROMPT "Z> "
231 static ODR out, in, print; /* encoding and decoding streams */
232 static COMSTACK conn = 0; /* our z-association */
233 static Z_IdAuthentication *auth = 0; /* our current auth definition */
234 static char *databaseNames[128];
235 static int num_databaseNames = 0;
236 static int setnumber = 0; /* current result set number */
237 static int smallSetUpperBound = 0;
238 static int largeSetLowerBound = 1;
239 static int mediumSetPresentNumber = 0;
240 static Z_ElementSetNames *elementSetNames = 0;
241 static int setno = 1; /* current set offset */
242 static int protocol = PROTO_Z3950; /* current app protocol */
243 static int recordsyntax = VAL_USMARC;
244 static int sent_close = 0;
245 static ODR_MEM session_mem; /* memory handle for init-response */
246 static Z_InitResponse *session = 0; /* session parameters */
247 static char last_scan[512] = "0";
248 static char last_cmd[100] = "?";
249 static FILE *marcdump = 0;
250 static char *refid = NULL;
258 static QueryType queryType = QueryType_Prefix;
261 static CCL_bibset bibset; /* CCL bibset handle */
264 static void send_apdu(Z_APDU *a)
269 if (!z_APDU(out, &a, 0))
271 odr_perror(out, "Encoding APDU");
274 buf = odr_getbuf(out, &len, 0);
275 if (cs_put(conn, buf, len) < 0)
277 fprintf(stderr, "cs_put: %s", cs_errmsg(cs_errno(conn)));
280 odr_reset(out); /* release the APDU structure */
283 static void print_refid (Z_ReferenceId *id)
287 printf ("ReferenceId: '%.*s'\n", id->len, id->buf);
291 static Z_ReferenceId *set_refid (ODR out)
296 id = odr_malloc (out, sizeof(*id));
297 id->size = id->len = strlen(refid);
298 id->buf = odr_malloc (out, id->len);
299 memcpy (id->buf, refid, id->len);
303 /* INIT SERVICE ------------------------------- */
305 static void send_initRequest()
307 Z_APDU *apdu = zget_APDU(out, Z_APDU_initRequest);
308 Z_InitRequest *req = apdu->u.initRequest;
310 ODR_MASK_SET(req->options, Z_Options_search);
311 ODR_MASK_SET(req->options, Z_Options_present);
312 ODR_MASK_SET(req->options, Z_Options_namedResultSets);
313 ODR_MASK_SET(req->options, Z_Options_triggerResourceCtrl);
314 ODR_MASK_SET(req->options, Z_Options_scan);
315 ODR_MASK_SET(req->options, Z_Options_sort);
317 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_1);
318 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_2);
319 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_3);
321 *req->maximumRecordSize = 1024*1024;
323 req->idAuthentication = auth;
326 printf("Sent initrequest.\n");
329 static int process_initResponse(Z_InitResponse *res)
331 /* save session parameters for later use */
332 session_mem = odr_extract_mem(in);
336 printf("Connection rejected by target.\n");
338 printf("Connection accepted by target.\n");
339 if (res->implementationId)
340 printf("ID : %s\n", res->implementationId);
341 if (res->implementationName)
342 printf("Name : %s\n", res->implementationName);
343 if (res->implementationVersion)
344 printf("Version: %s\n", res->implementationVersion);
345 if (res->userInformationField)
347 printf("UserInformationfield:\n");
348 if (!z_External(print, (Z_External**)&res-> userInformationField,
351 odr_perror(print, "Printing userinfo\n");
354 if (res->userInformationField->which == Z_External_octet)
356 printf("Guessing visiblestring:\n");
357 printf("'%s'\n", res->userInformationField->u. octet_aligned->buf);
363 int cmd_open(char *arg)
366 char type[100], addr[100];
371 printf("Already connected.\n");
374 if (!*arg || sscanf(arg, "%[^:]:%s", type, addr) < 2)
376 fprintf(stderr, "Usage: open (osi|tcp) ':' [tsel '/']host[':'port]\n");
379 if (!strcmp(type, "tcp"))
382 protocol = PROTO_Z3950;
386 if (!strcmp(type, "osi"))
394 fprintf(stderr, "Bad type: %s\n", type);
397 if (!(conn = cs_create(t, 1, protocol)))
402 if (!(add = cs_straddr(conn, addr)))
407 printf("Connecting...");
409 if (cs_connect(conn, add) < 0)
421 int cmd_authentication(char *arg)
423 static Z_IdAuthentication au;
424 static char open[256];
428 printf("Auth field set to null\n");
433 au.which = Z_IdAuthentication_open;
439 /* SEARCH SERVICE ------------------------------ */
441 static void display_variant(Z_Variant *v, int level)
445 for (i = 0; i < v->num_triples; i++)
447 printf("%*sclass=%d,type=%d", level * 4, "", *v->triples[i]->zclass,
448 *v->triples[i]->type);
449 if (v->triples[i]->which == Z_Triple_internationalString)
450 printf(",value=%s\n", v->triples[i]->value.internationalString);
456 static void display_grs1(Z_GenericRecord *r, int level)
462 for (i = 0; i < r->num_elements; i++)
466 printf("%*s", level * 4, "");
470 printf("%d,", *t->tagType);
473 if (t->tagValue->which == Z_StringOrNumeric_numeric)
474 printf("%d) ", *t->tagValue->u.numeric);
476 printf("%s) ", t->tagValue->u.string);
477 if (t->content->which == Z_ElementData_subtree)
480 display_grs1(t->content->u.subtree, level+1);
482 else if (t->content->which == Z_ElementData_string)
483 printf("%s\n", t->content->u.string);
484 else if (t->content->which == Z_ElementData_numeric)
485 printf("%d\n", *t->content->u.numeric);
486 else if (t->content->which == Z_ElementData_oid)
488 int *ip = t->content->u.oid;
491 if ((oent = oid_getentbyoid(t->content->u.oid)))
492 printf("OID: %s\n", oent->desc);
496 while (ip && *ip >= 0)
497 printf(" %d", *(ip++));
501 else if (t->content->which == Z_ElementData_noDataRequested)
502 printf("[No data requested]\n");
503 else if (t->content->which == Z_ElementData_elementEmpty)
504 printf("[Element empty]\n");
505 else if (t->content->which == Z_ElementData_elementNotThere)
506 printf("[Element not there]\n");
509 if (t->appliedVariant)
510 display_variant(t->appliedVariant, level+1);
511 if (t->metaData && t->metaData->supportedVariants)
515 printf("%*s---- variant list\n", (level+1)*4, "");
516 for (c = 0; c < t->metaData->num_supportedVariants; c++)
518 printf("%*svariant #%d\n", (level+1)*4, "", c);
519 display_variant(t->metaData->supportedVariants[c], level + 2);
525 static void display_record(Z_DatabaseRecord *p)
527 Z_External *r = (Z_External*) p;
528 oident *ent = oid_getentbyoid(r->direct_reference);
531 * Tell the user what we got.
533 if (r->direct_reference)
535 printf("Record type: ");
537 printf("%s\n", ent->desc);
538 else if (!odr_oid(print, &r->direct_reference, 0))
540 odr_perror(print, "print oid");
544 /* Check if this is a known, ASN.1 type tucked away in an octet string */
545 if (ent && r->which == Z_External_octet)
547 Z_ext_typeent *type = z_ext_getentbyref(ent->value);
553 * Call the given decoder to process the record.
555 odr_setbuf(in, (char*)p->u.octet_aligned->buf,
556 p->u.octet_aligned->len, 0);
557 if (!(*type->fun)(in, &rr, 0))
559 odr_perror(in, "Decoding constructed record.");
560 fprintf(stderr, "[Near %d]\n", odr_offset(in));
561 fprintf(stderr, "Packet dump:\n---------\n");
562 odr_dumpBER(stderr, (char*)p->u.octet_aligned->buf,
563 p->u.octet_aligned->len);
564 fprintf(stderr, "---------\n");
568 * Note: we throw away the original, BER-encoded record here.
569 * Do something else with it if you want to keep it.
571 r->u.sutrs = rr; /* we don't actually check the type here. */
572 r->which = type->what;
575 if (ent && ent->value == VAL_SOIF)
576 printf("%.*s", r->u.octet_aligned->len, r->u.octet_aligned->buf);
577 else if (r->which == Z_External_octet && p->u.octet_aligned->len)
579 const char *marc_buf = (char*)p->u.octet_aligned->buf;
580 marc_display (marc_buf, NULL);
582 fwrite (marc_buf, strlen (marc_buf), 1, marcdump);
584 else if (ent && ent->value == VAL_SUTRS)
586 if (r->which != Z_External_sutrs)
588 printf("Expecting single SUTRS type for SUTRS.\n");
591 printf("%.*s", r->u.sutrs->len, r->u.sutrs->buf);
593 else if (ent && ent->value == VAL_GRS1)
595 if (r->which != Z_External_grs1)
597 printf("Expecting single GRS type for GRS.\n");
600 display_grs1(r->u.grs1, 0);
604 printf("Unknown record representation.\n");
605 if (!z_External(print, &r, 0))
607 odr_perror(print, "Printing external");
614 static void display_diagrecs(Z_DiagRec **pp, int num)
618 Z_DefaultDiagFormat *r;
620 printf("Diagnostic message(s) from database:\n");
621 for (i = 0; i<num; i++)
623 Z_DiagRec *p = pp[i];
624 if (p->which != Z_DiagRec_defaultFormat)
626 printf("Diagnostic record not in default format.\n");
630 r = p->u.defaultFormat;
631 if (!(ent = oid_getentbyoid(r->diagnosticSetId)) ||
632 ent->oclass != CLASS_DIAGSET || ent->value != VAL_BIB1)
633 printf("Missing or unknown diagset\n");
634 printf(" [%d] %s", *r->condition, diagbib1_str(*r->condition));
635 if (r->addinfo && *r->addinfo)
636 printf(" -- '%s'\n", r->addinfo);
643 static void display_nameplusrecord(Z_NamePlusRecord *p)
646 printf("[%s]", p->databaseName);
647 if (p->which == Z_NamePlusRecord_surrogateDiagnostic)
648 display_diagrecs(&p->u.surrogateDiagnostic, 1);
650 display_record(p->u.databaseRecord);
653 static void display_records(Z_Records *p)
657 if (p->which == Z_Records_NSD)
658 display_diagrecs (&p->u.nonSurrogateDiagnostic, 1);
659 else if (p->which == Z_Records_multipleNSD)
660 display_diagrecs (p->u.multipleNonSurDiagnostics->diagRecs,
661 p->u.multipleNonSurDiagnostics->num_diagRecs);
664 printf("Records: %d\n", p->u.databaseOrSurDiagnostics->num_records);
665 for (i = 0; i < p->u.databaseOrSurDiagnostics->num_records; i++)
666 display_nameplusrecord(p->u.databaseOrSurDiagnostics->records[i]);
670 static int send_searchRequest(char *arg)
672 Z_APDU *apdu = zget_APDU(out, Z_APDU_searchRequest);
673 Z_SearchRequest *req = apdu->u.searchRequest;
677 struct ccl_rpn_node *rpn;
682 Z_RPNQuery *RPNquery;
686 if (queryType == QueryType_CCL2RPN)
688 rpn = ccl_find_str(bibset, arg, &error, &pos);
691 printf("CCL ERROR: %s\n", ccl_err_msg(error));
696 req->referenceId = set_refid (out);
697 if (!strcmp(arg, "@big")) /* strictly for troublemaking */
699 static unsigned char big[2100];
702 /* send a very big referenceid to test transport stack etc. */
703 memset(big, 'A', 2100);
704 bigo.len = bigo.size = 2100;
706 req->referenceId = &bigo;
711 sprintf(setstring, "%d", ++setnumber);
712 req->resultSetName = setstring;
714 *req->smallSetUpperBound = smallSetUpperBound;
715 *req->largeSetLowerBound = largeSetLowerBound;
716 *req->mediumSetPresentNumber = mediumSetPresentNumber;
717 if (smallSetUpperBound > 0 || (largeSetLowerBound > 1 &&
718 mediumSetPresentNumber > 0))
722 prefsyn.proto = protocol;
723 prefsyn.oclass = CLASS_RECSYN;
724 prefsyn.value = recordsyntax;
725 req->preferredRecordSyntax =
726 odr_oiddup(out, oid_ent_to_oid(&prefsyn, oid));
727 req->smallSetElementSetNames =
728 req->mediumSetElementSetNames = elementSetNames;
730 req->num_databaseNames = num_databaseNames;
731 req->databaseNames = databaseNames;
737 case QueryType_Prefix:
738 query.which = Z_Query_type_1;
739 RPNquery = p_query_rpn (out, protocol, arg);
742 printf("Prefix query error\n");
745 query.u.type_1 = RPNquery;
748 query.which = Z_Query_type_2;
749 query.u.type_2 = &ccl_query;
750 ccl_query.buf = (unsigned char*) arg;
751 ccl_query.len = strlen(arg);
754 case QueryType_CCL2RPN:
755 query.which = Z_Query_type_1;
756 assert((RPNquery = ccl_rpn_query(out, rpn)));
757 bib1.proto = protocol;
758 bib1.oclass = CLASS_ATTSET;
759 bib1.value = VAL_BIB1;
760 RPNquery->attributeSetId = oid_ent_to_oid(&bib1, oid);
761 query.u.type_1 = RPNquery;
762 ccl_rpn_delete (rpn);
766 printf ("Unsupported query type\n");
771 printf("Sent searchRequest.\n");
775 static int process_searchResponse(Z_SearchResponse *res)
777 printf ("Received SearchResponse.\n");
778 print_refid (res->referenceId);
779 if (*res->searchStatus)
780 printf("Search was a success.\n");
782 printf("Search was a bloomin' failure.\n");
783 printf("Number of hits: %d, setno %d\n",
784 *res->resultCount, setnumber);
785 printf("records returned: %d\n",
786 *res->numberOfRecordsReturned);
787 setno += *res->numberOfRecordsReturned;
789 display_records(res->records);
793 static int process_resourceControlRequest (Z_ResourceControlRequest *req)
795 printf ("Received ResourceControlRequest.\n");
796 print_refid (req->referenceId);
800 static int cmd_find(char *arg)
804 printf("Find what?\n");
809 printf("Not connected yet\n");
812 if (!send_searchRequest(arg))
817 static int cmd_ssub(char *arg)
819 if (!(smallSetUpperBound = atoi(arg)))
824 static int cmd_lslb(char *arg)
826 if (!(largeSetLowerBound = atoi(arg)))
831 static int cmd_mspn(char *arg)
833 if (!(mediumSetPresentNumber = atoi(arg)))
838 static int cmd_status(char *arg)
840 printf("smallSetUpperBound: %d\n", smallSetUpperBound);
841 printf("largeSetLowerBound: %d\n", largeSetLowerBound);
842 printf("mediumSetPresentNumber: %d\n", mediumSetPresentNumber);
846 static int cmd_base(char *arg)
853 printf("Usage: base <database> <database> ...\n");
856 for (i = 0; i<num_databaseNames; i++)
857 xfree (databaseNames[i]);
858 num_databaseNames = 0;
861 if (!(cp = strchr(arg, ' ')))
862 cp = arg + strlen(arg);
865 databaseNames[num_databaseNames] = xmalloc (1 + cp - arg);
866 memcpy (databaseNames[num_databaseNames], arg, cp - arg);
867 databaseNames[num_databaseNames++][cp - arg] = '\0';
875 static int cmd_setnames(char *arg)
879 printf("Set numbering enabled.\n");
884 printf("Set numbering disabled.\n");
890 /* PRESENT SERVICE ----------------------------- */
892 static int send_presentRequest(char *arg)
894 Z_APDU *apdu = zget_APDU(out, Z_APDU_presentRequest);
895 Z_PresentRequest *req = apdu->u.presentRequest;
896 Z_RecordComposition compo;
903 req->referenceId = set_refid (out);
904 if ((p = strchr(arg, '+')))
911 if (p && (p=strchr(p+1, '+')))
913 strcpy (setstring, p+1);
914 req->resultSetId = setstring;
916 else if (setnumber >= 0)
918 sprintf(setstring, "%d", setnumber);
919 req->resultSetId = setstring;
924 static Z_Range range;
925 static Z_Range *rangep = ⦥
928 req->resultSetStartPoint = &setno;
929 req->numberOfRecordsRequested = &nos;
930 prefsyn.proto = protocol;
931 prefsyn.oclass = CLASS_RECSYN;
932 prefsyn.value = recordsyntax;
933 req->preferredRecordSyntax = oid_ent_to_oid(&prefsyn, oid);
937 req->recordComposition = &compo;
938 compo.which = Z_RecordComp_simple;
939 compo.u.simple = elementSetNames;
942 printf("Sent presentRequest (%d+%d).\n", setno, nos);
946 void process_close(Z_Close *req)
948 Z_APDU *apdu = zget_APDU(out, Z_APDU_close);
949 Z_Close *res = apdu->u.close;
951 static char *reasons[] =
956 "cost limit reached",
958 "security violation",
965 printf("Reason: %s, message: %s\n", reasons[*req->closeReason],
966 req->diagnosticInformation ? req->diagnosticInformation : "NULL");
969 printf("Goodbye.\n");
972 *res->closeReason = Z_Close_finished;
974 printf("Sent response.\n");
978 static int cmd_show(char *arg)
980 if (!send_presentRequest(arg))
985 int cmd_quit(char *arg)
987 printf("See you later, alligator.\n");
992 int cmd_cancel(char *arg)
994 Z_APDU *apdu = zget_APDU(out, Z_APDU_triggerResourceControlRequest);
995 Z_TriggerResourceControlRequest *req =
996 apdu->u.triggerResourceControlRequest;
1001 printf("Session not initialized yet\n");
1004 if (!ODR_MASK_GET(session->options, Z_Options_triggerResourceCtrl))
1006 printf("Target doesn't support cancel (trigger resource ctrl)\n");
1009 *req->requestedAction = Z_TriggerResourceCtrl_cancel;
1010 req->resultSetWanted = &rfalse;
1013 printf("Sent cancel request\n");
1017 int send_scanrequest(char *string, int pp, int num)
1019 Z_APDU *apdu = zget_APDU(out, Z_APDU_scanRequest);
1020 Z_ScanRequest *req = apdu->u.scanRequest;
1022 req->referenceId = set_refid (out);
1023 req->num_databaseNames = num_databaseNames;
1024 req->databaseNames = databaseNames;
1025 req->termListAndStartPoint = p_query_scan(out, protocol,
1026 &req->attributeSet, string);
1027 req->numberOfTermsRequested = #
1028 req->preferredPositionInResponse = &pp;
1033 int send_sortrequest(char *arg, int newset)
1035 Z_APDU *apdu = zget_APDU(out, Z_APDU_sortRequest);
1036 Z_SortRequest *req = apdu->u.sortRequest;
1037 Z_SortKeySpecList *sksl = odr_malloc (out, sizeof(*sksl));
1039 char sort_string[32], sort_flags[32];
1045 sprintf (setstring, "%d", setnumber);
1047 sprintf (setstring, "default");
1049 req->referenceId = set_refid (out);
1051 req->inputResultSetNames =
1052 odr_malloc (out, sizeof(*req->inputResultSetNames));
1053 req->inputResultSetNames->num_strings = 1;
1054 req->inputResultSetNames->strings =
1055 odr_malloc (out, sizeof(*req->inputResultSetNames->strings));
1056 req->inputResultSetNames->strings[0] =
1057 odr_malloc (out, strlen(setstring)+1);
1058 strcpy (req->inputResultSetNames->strings[0], setstring);
1060 if (newset && setnumber >= 0)
1061 sprintf (setstring, "%d", ++setnumber);
1063 req->sortedResultSetName = odr_malloc (out, strlen(setstring)+1);
1064 strcpy (req->sortedResultSetName, setstring);
1066 req->sortSequence = sksl;
1067 sksl->num_specs = 0;
1068 sksl->specs = odr_malloc (out, sizeof(sksl->specs) * 20);
1070 bib1.proto = protocol;
1071 bib1.oclass = CLASS_ATTSET;
1072 bib1.value = VAL_BIB1;
1073 while ((sscanf (arg, "%31s %31s%n", sort_string, sort_flags, &off)) == 2
1077 char *sort_string_sep;
1078 Z_SortKeySpec *sks = odr_malloc (out, sizeof(*sks));
1079 Z_SortKey *sk = odr_malloc (out, sizeof(*sk));
1082 sksl->specs[sksl->num_specs++] = sks;
1083 sks->sortElement = odr_malloc (out, sizeof(*sks->sortElement));
1084 sks->sortElement->which = Z_SortElement_generic;
1085 sks->sortElement->u.generic = sk;
1087 if ((sort_string_sep = strchr (sort_string, '=')))
1089 Z_AttributeElement *el = odr_malloc (out, sizeof(*el));
1090 sk->which = Z_SortKey_sortAttributes;
1091 sk->u.sortAttributes =
1092 odr_malloc (out, sizeof(*sk->u.sortAttributes));
1093 sk->u.sortAttributes->id = oid_ent_to_oid(&bib1, oid);
1094 sk->u.sortAttributes->list =
1095 odr_malloc (out, sizeof(*sk->u.sortAttributes->list));
1096 sk->u.sortAttributes->list->num_attributes = 1;
1097 sk->u.sortAttributes->list->attributes =
1099 sizeof(*sk->u.sortAttributes->list->attributes));
1100 sk->u.sortAttributes->list->attributes[0] = el;
1101 el->attributeSet = 0;
1102 el->attributeType = odr_malloc (out, sizeof(*el->attributeType));
1103 *el->attributeType = atoi (sort_string);
1104 el->which = Z_AttributeValue_numeric;
1105 el->value.numeric = odr_malloc (out, sizeof(*el->value.numeric));
1106 *el->value.numeric = atoi (sort_string_sep + 1);
1110 sk->which = Z_SortKey_sortField;
1111 sk->u.sortField = odr_malloc (out, strlen(sort_string)+1);
1112 strcpy (sk->u.sortField, sort_string);
1114 sks->sortRelation = odr_malloc (out, sizeof(*sks->sortRelation));
1115 *sks->sortRelation = Z_SortRelation_ascending;
1116 sks->caseSensitivity = odr_malloc (out, sizeof(*sks->caseSensitivity));
1117 *sks->caseSensitivity = Z_SortCase_caseSensitive;
1119 sks->missingValueAction = NULL;
1121 for (i = 0; sort_flags[i]; i++)
1123 switch (sort_flags[i])
1128 *sks->sortRelation = Z_SortRelation_ascending;
1133 *sks->sortRelation = Z_SortRelation_descending;
1137 *sks->caseSensitivity = Z_SortCase_caseInsensitive;
1141 *sks->caseSensitivity = Z_SortCase_caseSensitive;
1146 if (!sksl->num_specs)
1148 printf ("Missing sort specifications\n");
1155 void display_term(Z_TermInfo *t)
1157 if (t->term->which == Z_Term_general)
1159 printf("%.*s (%d)\n", t->term->u.general->len, t->term->u.general->buf,
1160 t->globalOccurrences ? *t->globalOccurrences : -1);
1161 sprintf(last_scan, "%.*s", t->term->u.general->len,
1162 t->term->u.general->buf);
1165 printf("Term type not general.\n");
1168 void process_scanResponse(Z_ScanResponse *res)
1172 printf("Received ScanResponse\n");
1173 print_refid (res->referenceId);
1174 printf("%d entries", *res->numberOfEntriesReturned);
1175 if (res->positionOfTerm)
1176 printf (", position=%d", *res->positionOfTerm);
1178 if (*res->scanStatus != Z_Scan_success)
1179 printf("Scan returned code %d\n", *res->scanStatus);
1182 if (res->entries->which == Z_ListEntries_entries)
1184 Z_Entries *ent = res->entries->u.entries;
1186 for (i = 0; i < ent->num_entries; i++)
1187 if (ent->entries[i]->which == Z_Entry_termInfo)
1189 printf("%c ", i + 1 == *res->positionOfTerm ? '*' : ' ');
1190 display_term(ent->entries[i]->u.termInfo);
1193 display_diagrecs(&ent->entries[i]->u.surrogateDiagnostic, 1);
1196 display_diagrecs(&res->entries->
1197 u.nonSurrogateDiagnostics->diagRecs[0], 1);
1200 void process_sortResponse(Z_SortResponse *res)
1202 printf("Received SortResponse: status=");
1203 switch (*res->sortStatus)
1205 case Z_SortStatus_success:
1206 printf ("success"); break;
1207 case Z_SortStatus_partial_1:
1208 printf ("partial"); break;
1209 case Z_SortStatus_failure:
1210 printf ("failure"); break;
1212 printf ("unknown (%d)", *res->sortStatus);
1215 print_refid (res->referenceId);
1216 if (res->diagnostics)
1217 display_diagrecs(res->diagnostics->diagRecs,
1218 res->diagnostics->num_diagRecs);
1221 int cmd_sort_generic(char *arg, int newset)
1225 printf("Session not initialized yet\n");
1228 if (!ODR_MASK_GET(session->options, Z_Options_sort))
1230 printf("Target doesn't support sort\n");
1235 if (send_sortrequest(arg, newset) < 0)
1242 int cmd_sort(char *arg)
1244 return cmd_sort_generic (arg, 0);
1247 int cmd_sort_newset (char *arg)
1249 return cmd_sort_generic (arg, 1);
1252 int cmd_scan(char *arg)
1256 printf("Session not initialized yet\n");
1259 if (!ODR_MASK_GET(session->options, Z_Options_scan))
1261 printf("Target doesn't support scan\n");
1266 if (send_scanrequest(arg, 5, 20) < 0)
1270 if (send_scanrequest(last_scan, 1, 20) < 0)
1275 int cmd_format(char *arg)
1279 printf("Usage: format <recordsyntax>\n");
1282 recordsyntax = oid_getvalbyname (arg);
1283 if (recordsyntax == VAL_NONE)
1285 printf ("unknown record syntax\n");
1291 int cmd_elements(char *arg)
1293 static Z_ElementSetNames esn;
1294 static char what[100];
1298 printf("Usage: elements <esn>\n");
1302 esn.which = Z_ElementSetNames_generic;
1303 esn.u.generic = what;
1304 elementSetNames = &esn;
1308 int cmd_attributeset(char *arg)
1314 printf("Usage: attributeset <setname>\n");
1317 sscanf(arg, "%s", what);
1318 if (p_query_attset (what))
1320 printf("Unknown attribute set name\n");
1326 int cmd_querytype (char *arg)
1328 if (!strcmp (arg, "ccl"))
1329 queryType = QueryType_CCL;
1330 else if (!strcmp (arg, "prefix"))
1331 queryType = QueryType_Prefix;
1333 else if (!strcmp (arg, "ccl2rpn") || !strcmp (arg, "cclrpn"))
1334 queryType = QueryType_CCL2RPN;
1338 printf ("Querytype must be one of:\n");
1339 printf (" prefix - Prefix query\n");
1340 printf (" ccl - CCL query\n");
1342 printf (" ccl2rpn - CCL query converted to RPN\n");
1349 int cmd_refid (char *arg)
1355 refid = xmalloc (strlen(arg)+1);
1356 strcpy (refid, arg);
1361 int cmd_close(char *arg)
1363 Z_APDU *apdu = zget_APDU(out, Z_APDU_close);
1364 Z_Close *req = apdu->u.close;
1366 *req->closeReason = Z_Close_finished;
1368 printf("Sent close request.\n");
1373 static void initialize(void)
1379 if (!(out = odr_createmem(ODR_ENCODE)) ||
1380 !(in = odr_createmem(ODR_DECODE)) ||
1381 !(print = odr_createmem(ODR_PRINT)))
1383 fprintf(stderr, "failed to allocate ODR streams\n");
1386 setvbuf(stdout, 0, _IONBF, 0);
1389 bibset = ccl_qual_mk ();
1390 inf = fopen ("default.bib", "r");
1393 ccl_qual_file (bibset, inf);
1399 static int client(int wait)
1403 int (*fun)(char *arg);
1406 {"open", cmd_open, "('tcp'|'osi')':'[<tsel>'/']<host>[':'<port>]"},
1407 {"quit", cmd_quit, ""},
1408 {"find", cmd_find, "<query>"},
1409 {"base", cmd_base, "<base-name>"},
1410 {"show", cmd_show, "<rec#>['+'<#recs>['+'<setname>]]"},
1411 {"scan", cmd_scan, "<term>"},
1412 {"sort", cmd_sort, "<sortkey> <flag> <sortkey> <flag> ..."},
1413 {"sort+", cmd_sort_newset, "<sortkey> <flag> <sortkey> <flag> ..."},
1414 {"authentication", cmd_authentication, "<acctstring>"},
1415 {"lslb", cmd_lslb, "<largeSetLowerBound>"},
1416 {"ssub", cmd_ssub, "<smallSetUpperBound>"},
1417 {"mspn", cmd_mspn, "<mediumSetPresentNumber>"},
1418 {"status", cmd_status, ""},
1419 {"setnames", cmd_setnames, ""},
1420 {"cancel", cmd_cancel, ""},
1421 {"format", cmd_format, "<recordsyntax>"},
1422 {"elements", cmd_elements, "<elementSetName>"},
1423 {"close", cmd_close, ""},
1424 {"attributeset", cmd_attributeset, "<attrset>"},
1425 {"querytype", cmd_querytype, "<type>"},
1426 {"refid", cmd_refid, "<id>"},
1430 int netbufferlen = 0;
1440 char line[1024], word[1024], arg[1024];
1446 FD_SET(cs_fileno(conn), &input);
1447 if ((res = select(20, &input, 0, 0, 0)) < 0)
1454 if (!wait && FD_ISSET(0, &input))
1459 /* quick & dirty way to get a command line. */
1461 if (!fgets(line, 1023, stdin))
1463 if ((end_p = strchr (line, '\n')))
1465 if ((res = sscanf(line, "%s %[^;]", word, arg)) <= 0)
1467 strcpy(word, last_cmd);
1472 strcpy(last_cmd, word);
1473 for (i = 0; cmd[i].cmd; i++)
1474 if (!strncmp(cmd[i].cmd, word, strlen(word)))
1476 res = (*cmd[i].fun)(arg);
1479 if (!cmd[i].cmd) /* dump our help-screen */
1481 printf("Unknown command: %s.\n", word);
1482 printf("Currently recognized commands:\n");
1483 for (i = 0; cmd[i].cmd; i++)
1484 printf(" %s %s\n", cmd[i].cmd, cmd[i].ad);
1495 if (conn && FD_ISSET(cs_fileno(conn), &input))
1500 if ((res = cs_get(conn, &netbuffer, &netbufferlen)) < 0)
1507 printf("Target closed connection.\n");
1510 odr_reset(in); /* release APDU from last round */
1511 odr_setbuf(in, netbuffer, res, 0);
1512 if (!z_APDU(in, &apdu, 0))
1514 odr_perror(in, "Decoding incoming APDU");
1515 fprintf(stderr, "[Near %d]\n", odr_offset(in));
1516 fprintf(stderr, "Packet dump:\n---------\n");
1517 odr_dumpBER(stderr, netbuffer, res);
1518 fprintf(stderr, "---------\n");
1522 if (!z_APDU(print, &apdu, 0))
1524 odr_perror(print, "Failed to print incoming APDU");
1531 case Z_APDU_initResponse:
1532 process_initResponse(apdu->u.initResponse);
1534 case Z_APDU_searchResponse:
1535 process_searchResponse(apdu->u.searchResponse);
1537 case Z_APDU_scanResponse:
1538 process_scanResponse(apdu->u.scanResponse);
1540 case Z_APDU_presentResponse:
1541 print_refid (apdu->u.presentResponse->referenceId);
1543 *apdu->u.presentResponse->numberOfRecordsReturned;
1544 if (apdu->u.presentResponse->records)
1545 display_records(apdu->u.presentResponse->records);
1547 printf("No records.\n");
1549 case Z_APDU_sortResponse:
1550 process_sortResponse(apdu->u.sortResponse);
1553 printf("Target has closed the association.\n");
1554 process_close(apdu->u.close);
1556 case Z_APDU_resourceControlRequest:
1557 process_resourceControlRequest
1558 (apdu->u.resourceControlRequest);
1561 printf("Received unknown APDU type (%d).\n",
1566 while (cs_more(conn));
1574 int main(int argc, char **argv)
1582 cmd_base("Default");
1584 while ((ret = options("m:", argv, argc, &arg)) != -2)
1589 if (cmd_open (arg) == 2)
1593 if (!(marcdump = fopen (arg, "a")))
1600 fprintf (stderr, "Usage: %s [-m <marclog>] [<server-addr>]\n",
1607 return client (opened);