2 * Copyright (c) 1995-1999, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.75 1999-02-01 15:37:32 adam
8 * Fixed minor bug introduced by previous commit.
10 * Revision 1.74 1999/02/01 15:35:21 adam
13 * Revision 1.73 1998/10/20 13:55:43 quinn
14 * Fixed Scan bug in asn and client
16 * Revision 1.72 1998/10/20 13:23:15 quinn
17 * changed preferred pos to 1
19 * Revision 1.71 1998/10/20 13:21:43 adam
20 * Fixed scan response handler.
22 * Revision 1.70 1998/09/22 09:40:37 adam
23 * Minor changes in sort spec.
25 * Revision 1.69 1998/08/19 16:10:06 adam
26 * Changed som member names of DeleteResultSetRequest/Response.
28 * Revision 1.68 1998/07/20 12:37:06 adam
29 * Added 'delete <resultset>' command. Changed open command so that
30 * it reconnects if already connected.
32 * Revision 1.67 1998/06/09 13:55:06 adam
35 * Revision 1.66 1998/05/18 13:06:53 adam
36 * Changed the way attribute sets are handled by the retriaval module.
37 * Extended Explain conversion / schema.
38 * Modified server and client to work with ASN.1 compiled protocol handlers.
40 * Revision 1.65 1998/03/31 15:13:19 adam
41 * Development towards compiled ASN.1.
43 * Revision 1.64 1998/03/31 11:07:44 adam
44 * Furhter work on UNIverse resource report.
45 * Added Extended Services handling in frontend server.
47 * Revision 1.63 1998/03/05 08:05:10 adam
48 * Added a few casts to make C++ happy.
50 * Revision 1.62 1998/02/11 11:53:33 adam
51 * Changed code so that it compiles as C++.
53 * Revision 1.61 1998/02/10 11:03:06 adam
54 * Implemented command refid. Client prints reference-ID's, when present,
57 * Revision 1.60 1998/01/29 14:08:52 adam
58 * Better sort diagnostics.
60 * Revision 1.59 1998/01/29 13:17:56 adam
63 * Revision 1.58 1998/01/07 13:51:45 adam
66 * Revision 1.57 1998/01/07 12:58:22 adam
67 * Using fgets instead of gets.
69 * Revision 1.56 1997/11/05 09:18:31 adam
70 * The client handles records with no associated syntax.
72 * Revision 1.55 1997/10/31 12:20:08 adam
73 * Improved memory debugging for xmalloc/nmem.c. References to NMEM
74 * instead of ODR in n ESPEC-1 handling in source d1_espec.c.
75 * Bug fix: missing fclose in data1_read_espec1.
77 * Revision 1.54 1997/10/27 13:52:46 adam
78 * Header yaz-util includes all YAZ utility header files.
80 * Revision 1.53 1997/09/29 13:18:59 adam
81 * Added function, oid_ent_to_oid, to replace the function
82 * oid_getoidbyent, which is not thread safe.
84 * Revision 1.52 1997/09/29 07:20:31 adam
85 * Client code uses nmem_init.
87 * Revision 1.51 1997/09/26 09:41:55 adam
88 * Updated client to handle multiple diagnostics.
90 * Revision 1.50 1997/09/17 12:10:29 adam
93 * Revision 1.49 1997/09/04 13:45:17 adam
94 * Added UNImarc to list of available syntaxes.
96 * Revision 1.48 1997/09/01 08:48:44 adam
97 * New windows NT/95 port using MSV5.0. Only a few changes made
98 * to avoid warnings. Sub project created: client.dsp.
100 * Revision 1.47 1997/07/01 13:49:56 adam
101 * Take care of case when invalid target is specified on command line.
103 * Revision 1.46 1997/06/23 10:30:18 adam
104 * Added call to ccl_rpn_delete in search. Added ODR stream "out"
105 * as parameter to ccl_rpn_query to release RPN query.
107 * Revision 1.45 1997/05/14 06:53:29 adam
110 * Revision 1.44 1997/05/05 11:20:35 adam
111 * Client uses "options" utility and marc dump filename may be specified
112 * as an option (-m <file>).
114 * Revision 1.43 1996/11/08 11:03:26 adam
115 * Client accepts multiple database names.
117 * Revision 1.42 1996/10/08 10:44:57 quinn
118 * Resolved conflicts.
120 * Revision 1.41 1996/10/07 15:29:03 quinn
123 * Revision 1.40 1996/08/29 14:19:34 quinn
124 * Fixed conflict (CVS)
126 * Revision 1.39 1996/08/27 10:43:22 quinn
127 * Made select() optional
129 * Revision 1.38 1996/08/12 14:09:11 adam
130 * Default prefix query attribute set defined by using p_query_attset.
132 * Revision 1.37 1996/07/06 19:58:29 quinn
133 * System headerfiles gathered in yconfig
135 * Revision 1.36 1996/06/10 08:53:47 quinn
138 * Revision 1.35 1996/06/03 09:45:50 quinn
139 * Added display of OIDs in the GRS routine.
141 * Revision 1.34 1996/05/09 07:26:49 quinn
142 * *** empty log message ***
144 * Revision 1.33 1996/05/09 07:25:22 quinn
147 * Revision 1.32 1996/03/15 11:05:33 adam
148 * The user can set the preferred query type (prefix, ccl, ..) with the
151 * Revision 1.31 1996/02/20 12:51:54 quinn
152 * Fixed problems with EXTERNAL.
154 * Revision 1.30 1996/02/12 18:18:09 quinn
157 * Revision 1.29 1996/01/02 08:57:25 quinn
158 * Changed enums in the ASN.1 .h files to #defines. Changed oident.class to oclass
160 * Revision 1.28 1995/12/14 11:09:31 quinn
161 * Added Explain record syntax to the format command.
163 * Revision 1.27 1995/12/12 16:37:02 quinn
164 * Added destroy element to data1_node.
166 * Revision 1.26 1995/12/12 14:11:00 quinn
169 * Revision 1.25 1995/11/13 09:27:22 quinn
170 * Fiddling with the variant stuff.
172 * Revision 1.24 1995/10/30 12:41:13 quinn
173 * Added hostname lookup for server.
175 * Revision 1.23 1995/10/18 16:12:30 quinn
176 * Better diagnostics.
178 * Revision 1.22 1995/10/11 14:49:12 quinn
181 * Revision 1.21 1995/09/29 17:01:47 quinn
184 * Revision 1.20 1995/08/29 14:24:13 quinn
185 * Added second half of close-handshake
187 * Revision 1.19 1995/08/29 11:17:28 quinn
188 * Added code to receive close
190 * Revision 1.18 1995/08/28 12:21:27 quinn
191 * Client can now ask for simple element set names.
193 * Revision 1.17 1995/08/17 12:45:02 quinn
194 * Fixed minor problems with GRS-1. Added support in c&s.
196 * Revision 1.16 1995/08/15 12:00:04 quinn
199 * Revision 1.15 1995/06/22 09:28:03 quinn
200 * Fixed bug in SUTRS processing.
202 * Revision 1.14 1995/06/19 12:37:41 quinn
205 * Revision 1.13 1995/06/16 10:29:11 quinn
206 * *** empty log message ***
208 * Revision 1.12 1995/06/15 07:44:57 quinn
211 * Revision 1.11 1995/06/14 15:26:40 quinn
212 * *** empty log message ***
214 * Revision 1.10 1995/06/06 14:56:58 quinn
215 * Better diagnostics.
217 * Revision 1.9 1995/06/06 08:15:19 quinn
220 * Revision 1.8 1995/06/05 10:52:22 quinn
223 * Revision 1.7 1995/06/02 09:50:09 quinn
226 * Revision 1.6 1995/05/31 08:29:21 quinn
227 * Nothing significant.
229 * Revision 1.5 1995/05/29 08:10:47 quinn
230 * Moved oid.c to util.
232 * Revision 1.4 1995/05/22 15:30:13 adam
233 * Client uses prefix query notation.
235 * Revision 1.3 1995/05/22 15:06:53 quinn
236 * *** empty log message ***
238 * Revision 1.2 1995/05/22 14:56:40 quinn
239 * *** empty log message ***
241 * Revision 1.1 1995/05/22 11:30:31 quinn
242 * Added prettier client.
248 * This is the obligatory little toy client, whose primary purpose is
249 * to illustrate the use of the YAZ service-level API.
257 #include <yaz-util.h>
259 #include <comstack.h>
266 #include <marcdisp.h>
267 #include <diagbib1.h>
275 #define C_PROMPT "Z> "
277 static ODR out, in, print; /* encoding and decoding streams */
278 static COMSTACK conn = 0; /* our z-association */
279 static Z_IdAuthentication *auth = 0; /* our current auth definition */
280 static char *databaseNames[128];
281 static int num_databaseNames = 0;
282 static int setnumber = 0; /* current result set number */
283 static int smallSetUpperBound = 0;
284 static int largeSetLowerBound = 1;
285 static int mediumSetPresentNumber = 0;
286 static Z_ElementSetNames *elementSetNames = 0;
287 static int setno = 1; /* current set offset */
288 static enum oid_proto protocol = PROTO_Z3950; /* current app protocol */
289 static enum oid_value recordsyntax = VAL_USMARC;
290 static int sent_close = 0;
291 static NMEM session_mem = NULL; /* memory handle for init-response */
292 static Z_InitResponse *session = 0; /* session parameters */
293 static char last_scan[512] = "0";
294 static char last_cmd[100] = "?";
295 static FILE *marcdump = 0;
296 static char *refid = NULL;
304 static QueryType queryType = QueryType_Prefix;
307 static CCL_bibset bibset; /* CCL bibset handle */
310 static void send_apdu(Z_APDU *a)
315 if (!z_APDU(out, &a, 0))
317 odr_perror(out, "Encoding APDU");
320 buf = odr_getbuf(out, &len, 0);
321 if (cs_put(conn, buf, len) < 0)
323 fprintf(stderr, "cs_put: %s", cs_errmsg(cs_errno(conn)));
326 odr_reset(out); /* release the APDU structure */
329 static void print_refid (Z_ReferenceId *id)
333 printf ("ReferenceId: '%.*s'\n", id->len, id->buf);
337 static Z_ReferenceId *set_refid (ODR out)
342 id = (Z_ReferenceId *) odr_malloc (out, sizeof(*id));
343 id->size = id->len = strlen(refid);
344 id->buf = (unsigned char *) odr_malloc (out, id->len);
345 memcpy (id->buf, refid, id->len);
349 /* INIT SERVICE ------------------------------- */
351 static void send_initRequest()
353 Z_APDU *apdu = zget_APDU(out, Z_APDU_initRequest);
354 Z_InitRequest *req = apdu->u.initRequest;
356 ODR_MASK_SET(req->options, Z_Options_search);
357 ODR_MASK_SET(req->options, Z_Options_present);
358 ODR_MASK_SET(req->options, Z_Options_namedResultSets);
359 ODR_MASK_SET(req->options, Z_Options_triggerResourceCtrl);
360 ODR_MASK_SET(req->options, Z_Options_scan);
361 ODR_MASK_SET(req->options, Z_Options_sort);
362 ODR_MASK_SET(req->options, Z_Options_extendedServices);
363 ODR_MASK_SET(req->options, Z_Options_delSet);
365 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_1);
366 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_2);
367 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_3);
369 *req->maximumRecordSize = 1024*1024;
371 req->idAuthentication = auth;
374 printf("Sent initrequest.\n");
377 static int process_initResponse(Z_InitResponse *res)
379 /* save session parameters for later use */
380 session_mem = odr_extract_mem(in);
384 printf("Connection rejected by target.\n");
386 printf("Connection accepted by target.\n");
387 if (res->implementationId)
388 printf("ID : %s\n", res->implementationId);
389 if (res->implementationName)
390 printf("Name : %s\n", res->implementationName);
391 if (res->implementationVersion)
392 printf("Version: %s\n", res->implementationVersion);
393 if (res->userInformationField)
395 printf("UserInformationfield:\n");
396 if (!z_External(print, (Z_External**)&res-> userInformationField,
399 odr_perror(print, "Printing userinfo\n");
402 if (res->userInformationField->which == Z_External_octet)
404 printf("Guessing visiblestring:\n");
405 printf("'%s'\n", res->userInformationField->u. octet_aligned->buf);
412 static int cmd_base(char *arg)
419 printf("Usage: base <database> <database> ...\n");
422 for (i = 0; i<num_databaseNames; i++)
423 xfree (databaseNames[i]);
424 num_databaseNames = 0;
427 if (!(cp = strchr(arg, ' ')))
428 cp = arg + strlen(arg);
431 databaseNames[num_databaseNames] = (char *)xmalloc (1 + cp - arg);
432 memcpy (databaseNames[num_databaseNames], arg, cp - arg);
433 databaseNames[num_databaseNames++][cp - arg] = '\0';
442 int cmd_open(char *arg)
445 char type[100], addr[100], base[100];
450 printf("Already connected.\n");
456 nmem_destroy (session_mem);
461 if (!*arg || sscanf(arg, "%[^:]:%[^/]/%s", type, addr, base) < 2)
463 fprintf(stderr, "Usage: open (osi|tcp) ':' [tsel '/']host[':'port]\n");
468 if (!strcmp(type, "tcp"))
471 protocol = PROTO_Z3950;
475 if (!strcmp(type, "osi"))
483 fprintf(stderr, "Bad type: %s\n", type);
486 if (!(conn = cs_create(t, 1, protocol)))
491 if (!(add = cs_straddr(conn, addr)))
496 printf("Connecting...");
498 if (cs_connect(conn, add) < 0)
510 int cmd_authentication(char *arg)
512 static Z_IdAuthentication au;
513 static char open[256];
517 printf("Auth field set to null\n");
522 au.which = Z_IdAuthentication_open;
528 /* SEARCH SERVICE ------------------------------ */
530 static void display_variant(Z_Variant *v, int level)
534 for (i = 0; i < v->num_triples; i++)
536 printf("%*sclass=%d,type=%d", level * 4, "", *v->triples[i]->zclass,
537 *v->triples[i]->type);
538 if (v->triples[i]->which == Z_Triple_internationalString)
539 printf(",value=%s\n", v->triples[i]->value.internationalString);
545 static void display_grs1(Z_GenericRecord *r, int level)
551 for (i = 0; i < r->num_elements; i++)
555 printf("%*s", level * 4, "");
559 printf("%d,", *t->tagType);
562 if (t->tagValue->which == Z_StringOrNumeric_numeric)
563 printf("%d) ", *t->tagValue->u.numeric);
565 printf("%s) ", t->tagValue->u.string);
566 if (t->content->which == Z_ElementData_subtree)
569 display_grs1(t->content->u.subtree, level+1);
571 else if (t->content->which == Z_ElementData_string)
572 printf("%s\n", t->content->u.string);
573 else if (t->content->which == Z_ElementData_numeric)
574 printf("%d\n", *t->content->u.numeric);
575 else if (t->content->which == Z_ElementData_oid)
577 int *ip = t->content->u.oid;
580 if ((oent = oid_getentbyoid(t->content->u.oid)))
581 printf("OID: %s\n", oent->desc);
585 while (ip && *ip >= 0)
586 printf(" %d", *(ip++));
590 else if (t->content->which == Z_ElementData_noDataRequested)
591 printf("[No data requested]\n");
592 else if (t->content->which == Z_ElementData_elementEmpty)
593 printf("[Element empty]\n");
594 else if (t->content->which == Z_ElementData_elementNotThere)
595 printf("[Element not there]\n");
598 if (t->appliedVariant)
599 display_variant(t->appliedVariant, level+1);
600 if (t->metaData && t->metaData->supportedVariants)
604 printf("%*s---- variant list\n", (level+1)*4, "");
605 for (c = 0; c < t->metaData->num_supportedVariants; c++)
607 printf("%*svariant #%d\n", (level+1)*4, "", c);
608 display_variant(t->metaData->supportedVariants[c], level + 2);
614 static void display_record(Z_DatabaseRecord *p)
616 Z_External *r = (Z_External*) p;
617 oident *ent = oid_getentbyoid(r->direct_reference);
620 * Tell the user what we got.
622 if (r->direct_reference)
624 printf("Record type: ");
626 printf("%s\n", ent->desc);
627 else if (!odr_oid(print, &r->direct_reference, 0))
629 odr_perror(print, "print oid");
633 /* Check if this is a known, ASN.1 type tucked away in an octet string */
634 if (ent && r->which == Z_External_octet)
636 Z_ext_typeent *type = z_ext_getentbyref(ent->value);
642 * Call the given decoder to process the record.
644 odr_setbuf(in, (char*)p->u.octet_aligned->buf,
645 p->u.octet_aligned->len, 0);
646 if (!(*type->fun)(in, (char **)&rr, 0))
648 odr_perror(in, "Decoding constructed record.");
649 fprintf(stderr, "[Near %d]\n", odr_offset(in));
650 fprintf(stderr, "Packet dump:\n---------\n");
651 odr_dumpBER(stderr, (char*)p->u.octet_aligned->buf,
652 p->u.octet_aligned->len);
653 fprintf(stderr, "---------\n");
657 * Note: we throw away the original, BER-encoded record here.
658 * Do something else with it if you want to keep it.
660 r->u.sutrs = (Z_SUTRS *) rr; /* we don't actually check the type here. */
661 r->which = type->what;
664 if (ent && ent->value == VAL_SOIF)
665 printf("%.*s", r->u.octet_aligned->len, r->u.octet_aligned->buf);
666 else if (r->which == Z_External_octet && p->u.octet_aligned->len)
668 const char *octet_buf = (char*)p->u.octet_aligned->buf;
669 if (ent->value == VAL_TEXT_XML || ent->value == VAL_APPLICATION_XML)
671 fwrite (octet_buf, 1, p->u.octet_aligned->len, stdout);
675 marc_display (octet_buf, NULL);
677 fwrite (octet_buf, 1, p->u.octet_aligned->len, marcdump);
679 else if (ent && ent->value == VAL_SUTRS)
681 if (r->which != Z_External_sutrs)
683 printf("Expecting single SUTRS type for SUTRS.\n");
686 printf("%.*s", r->u.sutrs->len, r->u.sutrs->buf);
688 else if (ent && ent->value == VAL_GRS1)
690 if (r->which != Z_External_grs1)
692 printf("Expecting single GRS type for GRS.\n");
695 display_grs1(r->u.grs1, 0);
699 printf("Unknown record representation.\n");
700 if (!z_External(print, &r, 0))
702 odr_perror(print, "Printing external");
709 static void display_diagrecs(Z_DiagRec **pp, int num)
713 Z_DefaultDiagFormat *r;
715 printf("Diagnostic message(s) from database:\n");
716 for (i = 0; i<num; i++)
718 Z_DiagRec *p = pp[i];
719 if (p->which != Z_DiagRec_defaultFormat)
721 printf("Diagnostic record not in default format.\n");
725 r = p->u.defaultFormat;
726 if (!(ent = oid_getentbyoid(r->diagnosticSetId)) ||
727 ent->oclass != CLASS_DIAGSET || ent->value != VAL_BIB1)
728 printf("Missing or unknown diagset\n");
729 printf(" [%d] %s", *r->condition, diagbib1_str(*r->condition));
733 case Z_DefaultDiagFormat_v2Addinfo:
734 printf (" -- v2 addinfo '%s'\n", r->u.v2Addinfo);
736 case Z_DefaultDiagFormat_v3Addinfo:
737 printf (" -- v3 addinfo '%s'\n", r->u.v3Addinfo);
741 if (r->addinfo && *r->addinfo)
742 printf(" -- '%s'\n", r->addinfo);
750 static void display_nameplusrecord(Z_NamePlusRecord *p)
753 printf("[%s]", p->databaseName);
754 if (p->which == Z_NamePlusRecord_surrogateDiagnostic)
755 display_diagrecs(&p->u.surrogateDiagnostic, 1);
757 display_record(p->u.databaseRecord);
760 static void display_records(Z_Records *p)
764 if (p->which == Z_Records_NSD)
767 Z_DiagRec dr, *dr_p = &dr;
768 dr.which = Z_DiagRec_defaultFormat;
769 dr.u.defaultFormat = p->u.nonSurrogateDiagnostic;
770 display_diagrecs (&dr_p, 1);
772 display_diagrecs (&p->u.nonSurrogateDiagnostic, 1);
775 else if (p->which == Z_Records_multipleNSD)
776 display_diagrecs (p->u.multipleNonSurDiagnostics->diagRecs,
777 p->u.multipleNonSurDiagnostics->num_diagRecs);
780 printf("Records: %d\n", p->u.databaseOrSurDiagnostics->num_records);
781 for (i = 0; i < p->u.databaseOrSurDiagnostics->num_records; i++)
782 display_nameplusrecord(p->u.databaseOrSurDiagnostics->records[i]);
786 static int send_deleteResultSetRequest(char *arg)
788 Z_APDU *apdu = zget_APDU(out, Z_APDU_deleteResultSetRequest);
789 Z_DeleteResultSetRequest *req = apdu->u.deleteResultSetRequest;
791 req->referenceId = set_refid (out);
793 req->num_resultSetList = 1;
794 req->resultSetList = (char **)
795 odr_malloc (out, sizeof(*req->resultSetList));
796 *req->resultSetList = arg;
797 req->deleteFunction = (int *)
798 odr_malloc (out, sizeof(*req->deleteFunction));
799 *req->deleteFunction = Z_DeleteRequest_list;
802 printf("Sent deleteResultSetRequest.\n");
806 static int send_searchRequest(char *arg)
808 Z_APDU *apdu = zget_APDU(out, Z_APDU_searchRequest);
809 Z_SearchRequest *req = apdu->u.searchRequest;
813 struct ccl_rpn_node *rpn = NULL;
818 Z_RPNQuery *RPNquery;
822 if (queryType == QueryType_CCL2RPN)
824 rpn = ccl_find_str(bibset, arg, &error, &pos);
827 printf("CCL ERROR: %s\n", ccl_err_msg(error));
832 req->referenceId = set_refid (out);
833 if (!strcmp(arg, "@big")) /* strictly for troublemaking */
835 static unsigned char big[2100];
838 /* send a very big referenceid to test transport stack etc. */
839 memset(big, 'A', 2100);
840 bigo.len = bigo.size = 2100;
842 req->referenceId = &bigo;
847 sprintf(setstring, "%d", ++setnumber);
848 req->resultSetName = setstring;
850 *req->smallSetUpperBound = smallSetUpperBound;
851 *req->largeSetLowerBound = largeSetLowerBound;
852 *req->mediumSetPresentNumber = mediumSetPresentNumber;
853 if (smallSetUpperBound > 0 || (largeSetLowerBound > 1 &&
854 mediumSetPresentNumber > 0))
858 prefsyn.proto = protocol;
859 prefsyn.oclass = CLASS_RECSYN;
860 prefsyn.value = recordsyntax;
861 req->preferredRecordSyntax =
862 odr_oiddup(out, oid_ent_to_oid(&prefsyn, oid));
863 req->smallSetElementSetNames =
864 req->mediumSetElementSetNames = elementSetNames;
866 req->num_databaseNames = num_databaseNames;
867 req->databaseNames = databaseNames;
873 case QueryType_Prefix:
874 query.which = Z_Query_type_1;
875 RPNquery = p_query_rpn (out, protocol, arg);
878 printf("Prefix query error\n");
881 query.u.type_1 = RPNquery;
884 query.which = Z_Query_type_2;
885 query.u.type_2 = &ccl_query;
886 ccl_query.buf = (unsigned char*) arg;
887 ccl_query.len = strlen(arg);
890 case QueryType_CCL2RPN:
891 query.which = Z_Query_type_1;
892 RPNquery = ccl_rpn_query(out, rpn);
894 bib1.proto = protocol;
895 bib1.oclass = CLASS_ATTSET;
896 bib1.value = VAL_BIB1;
897 RPNquery->attributeSetId = oid_ent_to_oid(&bib1, oid);
898 query.u.type_1 = RPNquery;
899 ccl_rpn_delete (rpn);
903 printf ("Unsupported query type\n");
908 printf("Sent searchRequest.\n");
912 static int process_searchResponse(Z_SearchResponse *res)
914 printf ("Received SearchResponse.\n");
915 print_refid (res->referenceId);
916 if (*res->searchStatus)
917 printf("Search was a success.\n");
919 printf("Search was a bloomin' failure.\n");
920 printf("Number of hits: %d, setno %d\n",
921 *res->resultCount, setnumber);
922 printf("records returned: %d\n",
923 *res->numberOfRecordsReturned);
924 setno += *res->numberOfRecordsReturned;
926 display_records(res->records);
930 static void print_level(int iLevel)
933 for (i = 0; i < iLevel * 4; i++)
937 static void print_int(int iLevel, const char *pTag, int *pInt)
942 printf("%s: %d\n", pTag, *pInt);
946 static void print_string(int iLevel, const char *pTag, const char *pString)
951 printf("%s: %s\n", pTag, pString);
955 static void print_oid(int iLevel, const char *pTag, Odr_oid *pOid)
963 for (; *pInt != -1; pInt++)
964 printf(" %d", *pInt);
969 static void print_referenceId(int iLevel, Z_ReferenceId *referenceId)
971 if (referenceId != NULL)
976 printf("Ref Id (%d, %d): ", referenceId->len, referenceId->size);
977 for (i = 0; i < referenceId->len; i++)
978 printf("%c", referenceId->buf[i]);
983 static void print_string_or_numeric(int iLevel, const char *pTag, Z_StringOrNumeric *pStringNumeric)
985 if (pStringNumeric != NULL)
987 switch (pStringNumeric->which)
989 case Z_StringOrNumeric_string:
990 print_string(iLevel, pTag, pStringNumeric->u.string);
993 case Z_StringOrNumeric_numeric:
994 print_int(iLevel, pTag, pStringNumeric->u.numeric);
999 printf("%s: valid type for Z_StringOrNumeric\n", pTag);
1005 static void print_universe_report_duplicate(int iLevel, Z_UniverseReportDuplicate *pUniverseReportDuplicate)
1007 if (pUniverseReportDuplicate != NULL)
1009 print_level(iLevel);
1010 printf("Universe Report Duplicate: \n");
1012 print_string_or_numeric(iLevel, "Hit No", pUniverseReportDuplicate->hitno);
1016 static void print_universe_report_hits(int iLevel, Z_UniverseReportHits *pUniverseReportHits)
1018 if (pUniverseReportHits != NULL)
1020 print_level(iLevel);
1021 printf("Universe Report Hits: \n");
1023 print_string_or_numeric(iLevel, "Database", pUniverseReportHits->database);
1024 print_string_or_numeric(iLevel, "Hits", pUniverseReportHits->hits);
1028 static void print_universe_report(int iLevel, Z_UniverseReport *pUniverseReport)
1030 if (pUniverseReport != NULL)
1032 print_level(iLevel);
1033 printf("Universe Report: \n");
1035 print_int(iLevel, "Total Hits", pUniverseReport->totalHits);
1036 switch (pUniverseReport->which)
1038 case Z_UniverseReport_databaseHits:
1039 print_universe_report_hits(iLevel, pUniverseReport->u.databaseHits);
1042 case Z_UniverseReport_duplicate:
1043 print_universe_report_duplicate(iLevel, pUniverseReport->u.duplicate);
1047 print_level(iLevel);
1048 printf("Type: %d\n", pUniverseReport->which);
1054 static void print_external(int iLevel, Z_External *pExternal)
1056 if (pExternal != NULL)
1058 print_level(iLevel);
1059 printf("External: \n");
1061 print_oid(iLevel, "Direct Reference", pExternal->direct_reference);
1062 print_int(iLevel, "InDirect Reference", pExternal->indirect_reference);
1063 print_string(iLevel, "Descriptor", pExternal->descriptor);
1064 switch (pExternal->which)
1066 case Z_External_universeReport:
1067 print_universe_report(iLevel, pExternal->u.universeReport);
1071 print_level(iLevel);
1072 printf("Type: %d\n", pExternal->which);
1078 static int process_resourceControlRequest (Z_ResourceControlRequest *req)
1080 printf ("Received ResourceControlRequest.\n");
1081 print_referenceId(1, req->referenceId);
1082 print_int(1, "Suspended Flag", req->suspendedFlag);
1083 print_int(1, "Partial Results Available", req->partialResultsAvailable);
1084 print_int(1, "Response Required", req->responseRequired);
1085 print_int(1, "Triggered Request Flag", req->triggeredRequestFlag);
1086 print_external(1, req->resourceReport);
1090 void process_ESResponse(Z_ExtendedServicesResponse *res)
1092 printf("process_ESResponse\n");
1095 static Z_External *CreateItemOrderExternal(int itemno)
1097 Z_External *r = (Z_External *) odr_malloc(out, sizeof(Z_External));
1098 oident ItemOrderRequest;
1100 ItemOrderRequest.proto = PROTO_Z3950;
1101 ItemOrderRequest.oclass = CLASS_EXTSERV;
1102 ItemOrderRequest.value = VAL_ITEMORDER;
1104 r->direct_reference = odr_oiddup(out,oid_getoidbyent(&ItemOrderRequest));
1105 r->indirect_reference = (int *) odr_malloc(out,sizeof(int));
1106 *r->indirect_reference = 0;
1108 r->descriptor = "Extended services item order";
1110 r->which = Z_External_itemOrder;
1112 r->u.itemOrder = (Z_ItemOrder *) odr_malloc(out,sizeof(Z_ItemOrder));
1113 memset(r->u.itemOrder, 0, sizeof(Z_ItemOrder));
1115 r->u.itemOrder->which=Z_IOItemOrder_esRequest;
1117 r->u.itemOrder->which=Z_ItemOrder_esRequest;
1120 r->u.itemOrder->u.esRequest = (Z_IORequest *)
1121 odr_malloc(out,sizeof(Z_IORequest));
1122 memset(r->u.itemOrder->u.esRequest, 0, sizeof(Z_IORequest));
1124 r->u.itemOrder->u.esRequest->toKeep = (Z_IOOriginPartToKeep *)
1125 odr_malloc(out,sizeof(Z_IOOriginPartToKeep));
1126 memset(r->u.itemOrder->u.esRequest->toKeep, 0, sizeof(Z_IOOriginPartToKeep));
1127 r->u.itemOrder->u.esRequest->notToKeep = (Z_IOOriginPartNotToKeep *)
1128 odr_malloc(out,sizeof(Z_IOOriginPartNotToKeep));
1129 memset(r->u.itemOrder->u.esRequest->notToKeep, 0, sizeof(Z_IOOriginPartNotToKeep));
1131 r->u.itemOrder->u.esRequest->toKeep->supplDescription = NULL;
1132 r->u.itemOrder->u.esRequest->toKeep->contact = NULL;
1133 r->u.itemOrder->u.esRequest->toKeep->addlBilling = NULL;
1135 r->u.itemOrder->u.esRequest->notToKeep->resultSetItem =
1136 (Z_IOResultSetItem *) odr_malloc(out, sizeof(Z_IOResultSetItem));
1137 memset(r->u.itemOrder->u.esRequest->notToKeep->resultSetItem, 0, sizeof(Z_IOResultSetItem));
1138 r->u.itemOrder->u.esRequest->notToKeep->resultSetItem->resultSetId = "1";
1140 r->u.itemOrder->u.esRequest->notToKeep->resultSetItem->item =
1141 (int *) odr_malloc(out, sizeof(int));
1142 *r->u.itemOrder->u.esRequest->notToKeep->resultSetItem->item = itemno;
1144 r->u.itemOrder->u.esRequest->notToKeep->itemRequest = NULL;
1148 /* II : Added to do DALI Item Order Extended services request */
1149 static int send_itemorder(char *arg)
1152 Z_APDU *apdu = zget_APDU(out, Z_APDU_extendedServicesRequest );
1153 Z_ExtendedServicesRequest *req = apdu->u.extendedServicesRequest;
1154 oident ItemOrderRequest;
1159 /* Set up item order request */
1161 /* Function being performed by this extended services request */
1162 req->function = (int *) odr_malloc(out, sizeof(int));
1163 *req->function = Z_ExtendedServicesRequest_create;
1165 /* Package type, Using protocol ILL ( But that's not in the oid.h file yet */
1166 /* create an object of class Extended Service, value Item Order */
1167 ItemOrderRequest.proto = PROTO_Z3950;
1168 ItemOrderRequest.oclass = CLASS_EXTSERV;
1169 ItemOrderRequest.value = VAL_ITEMORDER;
1170 req->packageType = odr_oiddup(out,oid_getoidbyent(&ItemOrderRequest));
1171 req->packageName = "1.Extendedserveq";
1173 /* ** taskSpecificParameters ** */
1174 req->taskSpecificParameters = CreateItemOrderExternal(itemno);
1176 /* waitAction - Create the ILL request and that's it */
1177 *req->waitAction = Z_ExtendedServicesRequest_wait;
1183 /* II : Added to do DALI Item Order Extended services request */
1184 static int cmd_itemorder(char *arg)
1186 printf("Item order request\n");
1189 send_itemorder(arg);
1193 static int cmd_find(char *arg)
1197 printf("Find what?\n");
1202 printf("Not connected yet\n");
1205 if (!send_searchRequest(arg))
1210 static int cmd_delete(char *arg)
1214 printf("Delete what?\n");
1219 printf("Not connected yet\n");
1222 if (!send_deleteResultSetRequest(arg))
1227 static int cmd_ssub(char *arg)
1229 if (!(smallSetUpperBound = atoi(arg)))
1234 static int cmd_lslb(char *arg)
1236 if (!(largeSetLowerBound = atoi(arg)))
1241 static int cmd_mspn(char *arg)
1243 if (!(mediumSetPresentNumber = atoi(arg)))
1248 static int cmd_status(char *arg)
1250 printf("smallSetUpperBound: %d\n", smallSetUpperBound);
1251 printf("largeSetLowerBound: %d\n", largeSetLowerBound);
1252 printf("mediumSetPresentNumber: %d\n", mediumSetPresentNumber);
1256 static int cmd_setnames(char *arg)
1260 printf("Set numbering enabled.\n");
1265 printf("Set numbering disabled.\n");
1271 /* PRESENT SERVICE ----------------------------- */
1273 static int send_presentRequest(char *arg)
1275 Z_APDU *apdu = zget_APDU(out, Z_APDU_presentRequest);
1276 Z_PresentRequest *req = apdu->u.presentRequest;
1277 Z_RecordComposition compo;
1282 char setstring[100];
1284 req->referenceId = set_refid (out);
1285 if ((p = strchr(arg, '+')))
1292 if (p && (p=strchr(p+1, '+')))
1294 strcpy (setstring, p+1);
1295 req->resultSetId = setstring;
1297 else if (setnumber >= 0)
1299 sprintf(setstring, "%d", setnumber);
1300 req->resultSetId = setstring;
1305 static Z_Range range;
1306 static Z_Range *rangep = ⦥
1307 req->num_ranges = 1;
1309 req->resultSetStartPoint = &setno;
1310 req->numberOfRecordsRequested = &nos;
1311 prefsyn.proto = protocol;
1312 prefsyn.oclass = CLASS_RECSYN;
1313 prefsyn.value = recordsyntax;
1314 req->preferredRecordSyntax = oid_ent_to_oid(&prefsyn, oid);
1316 if (elementSetNames)
1318 req->recordComposition = &compo;
1319 compo.which = Z_RecordComp_simple;
1320 compo.u.simple = elementSetNames;
1323 printf("Sent presentRequest (%d+%d).\n", setno, nos);
1327 void process_close(Z_Close *req)
1329 Z_APDU *apdu = zget_APDU(out, Z_APDU_close);
1330 Z_Close *res = apdu->u.close;
1332 static char *reasons[] =
1337 "cost limit reached",
1339 "security violation",
1346 printf("Reason: %s, message: %s\n", reasons[*req->closeReason],
1347 req->diagnosticInformation ? req->diagnosticInformation : "NULL");
1354 nmem_destroy (session_mem);
1361 *res->closeReason = Z_Close_finished;
1363 printf("Sent response.\n");
1368 static int cmd_show(char *arg)
1372 printf("Not connected yet\n");
1375 if (!send_presentRequest(arg))
1380 int cmd_quit(char *arg)
1382 printf("See you later, alligator.\n");
1387 int cmd_cancel(char *arg)
1389 Z_APDU *apdu = zget_APDU(out, Z_APDU_triggerResourceControlRequest);
1390 Z_TriggerResourceControlRequest *req =
1391 apdu->u.triggerResourceControlRequest;
1396 printf("Session not initialized yet\n");
1399 if (!ODR_MASK_GET(session->options, Z_Options_triggerResourceCtrl))
1401 printf("Target doesn't support cancel (trigger resource ctrl)\n");
1404 *req->requestedAction = Z_TriggerResourceCtrl_cancel;
1405 req->resultSetWanted = &rfalse;
1408 printf("Sent cancel request\n");
1412 int send_scanrequest(char *string, int pp, int num)
1414 Z_APDU *apdu = zget_APDU(out, Z_APDU_scanRequest);
1415 Z_ScanRequest *req = apdu->u.scanRequest;
1417 if (!(req->termListAndStartPoint =
1418 p_query_scan(out, protocol, &req->attributeSet, string)))
1420 printf("Prefix query error\n");
1423 req->referenceId = set_refid (out);
1424 req->num_databaseNames = num_databaseNames;
1425 req->databaseNames = databaseNames;
1426 req->numberOfTermsRequested = #
1427 req->preferredPositionInResponse = &pp;
1432 int send_sortrequest(char *arg, int newset)
1434 Z_APDU *apdu = zget_APDU(out, Z_APDU_sortRequest);
1435 Z_SortRequest *req = apdu->u.sortRequest;
1436 Z_SortKeySpecList *sksl = (Z_SortKeySpecList *)odr_malloc (out, sizeof(*sksl));
1438 char sort_string[32], sort_flags[32];
1444 sprintf (setstring, "%d", setnumber);
1446 sprintf (setstring, "default");
1448 req->referenceId = set_refid (out);
1451 req->num_inputResultSetNames = 1;
1452 req->inputResultSetNames = (Z_InternationalString **)
1453 odr_malloc (out, sizeof(*req->inputResultSetNames));
1454 req->inputResultSetNames[0] = (char *)
1455 odr_malloc (out, strlen(setstring)+1);
1456 strcpy (req->inputResultSetNames[0], setstring);
1458 req->inputResultSetNames =
1459 (Z_StringList *)odr_malloc (out, sizeof(*req->inputResultSetNames));
1460 req->inputResultSetNames->num_strings = 1;
1461 req->inputResultSetNames->strings =
1462 (char **)odr_malloc (out, sizeof(*req->inputResultSetNames->strings));
1463 req->inputResultSetNames->strings[0] =
1464 (char *)odr_malloc (out, strlen(setstring)+1);
1465 strcpy (req->inputResultSetNames->strings[0], setstring);
1468 if (newset && setnumber >= 0)
1469 sprintf (setstring, "%d", ++setnumber);
1471 req->sortedResultSetName = (char *)odr_malloc (out, strlen(setstring)+1);
1472 strcpy (req->sortedResultSetName, setstring);
1474 req->sortSequence = sksl;
1475 sksl->num_specs = 0;
1476 sksl->specs = (Z_SortKeySpec **)odr_malloc (out, sizeof(sksl->specs) * 20);
1478 bib1.proto = protocol;
1479 bib1.oclass = CLASS_ATTSET;
1480 bib1.value = VAL_BIB1;
1481 while ((sscanf (arg, "%31s %31s%n", sort_string, sort_flags, &off)) == 2
1485 char *sort_string_sep;
1486 Z_SortKeySpec *sks = (Z_SortKeySpec *)odr_malloc (out, sizeof(*sks));
1487 Z_SortKey *sk = (Z_SortKey *)odr_malloc (out, sizeof(*sk));
1490 sksl->specs[sksl->num_specs++] = sks;
1491 sks->sortElement = (Z_SortElement *)odr_malloc (out, sizeof(*sks->sortElement));
1492 sks->sortElement->which = Z_SortElement_generic;
1493 sks->sortElement->u.generic = sk;
1495 if ((sort_string_sep = strchr (sort_string, '=')))
1497 Z_AttributeElement *el = (Z_AttributeElement *)odr_malloc (out, sizeof(*el));
1498 sk->which = Z_SortKey_sortAttributes;
1499 sk->u.sortAttributes =
1500 (Z_SortAttributes *)odr_malloc (out, sizeof(*sk->u.sortAttributes));
1501 sk->u.sortAttributes->id = oid_ent_to_oid(&bib1, oid);
1502 sk->u.sortAttributes->list =
1503 (Z_AttributeList *)odr_malloc (out, sizeof(*sk->u.sortAttributes->list));
1504 sk->u.sortAttributes->list->num_attributes = 1;
1505 sk->u.sortAttributes->list->attributes =
1506 (Z_AttributeElement **)odr_malloc (out,
1507 sizeof(*sk->u.sortAttributes->list->attributes));
1508 sk->u.sortAttributes->list->attributes[0] = el;
1509 el->attributeSet = 0;
1510 el->attributeType = (int *)odr_malloc (out, sizeof(*el->attributeType));
1511 *el->attributeType = atoi (sort_string);
1512 el->which = Z_AttributeValue_numeric;
1513 el->value.numeric = (int *)odr_malloc (out, sizeof(*el->value.numeric));
1514 *el->value.numeric = atoi (sort_string_sep + 1);
1518 sk->which = Z_SortKey_sortField;
1519 sk->u.sortField = (char *)odr_malloc (out, strlen(sort_string)+1);
1520 strcpy (sk->u.sortField, sort_string);
1522 sks->sortRelation = (int *)odr_malloc (out, sizeof(*sks->sortRelation));
1523 *sks->sortRelation = Z_SortRelation_ascending;
1524 sks->caseSensitivity = (int *)odr_malloc (out, sizeof(*sks->caseSensitivity));
1525 *sks->caseSensitivity = Z_SortCase_caseSensitive;
1528 sks->which = Z_SortKeySpec_null;
1529 sks->u.null = odr_nullval ();
1531 sks->missingValueAction = NULL;
1534 for (i = 0; sort_flags[i]; i++)
1536 switch (sort_flags[i])
1541 *sks->sortRelation = Z_SortRelation_descending;
1546 *sks->sortRelation = Z_SortRelation_ascending;
1550 *sks->caseSensitivity = Z_SortCase_caseInsensitive;
1554 *sks->caseSensitivity = Z_SortCase_caseSensitive;
1559 if (!sksl->num_specs)
1561 printf ("Missing sort specifications\n");
1568 void display_term(Z_TermInfo *t)
1570 if (t->term->which == Z_Term_general)
1572 printf("%.*s (%d)\n", t->term->u.general->len, t->term->u.general->buf,
1573 t->globalOccurrences ? *t->globalOccurrences : -1);
1574 sprintf(last_scan, "%.*s", t->term->u.general->len,
1575 t->term->u.general->buf);
1578 printf("Term type not general.\n");
1581 void process_scanResponse(Z_ScanResponse *res)
1584 Z_Entry **entries = NULL;
1585 int num_entries = 0;
1587 printf("Received ScanResponse\n");
1588 print_refid (res->referenceId);
1589 printf("%d entries", *res->numberOfEntriesReturned);
1590 if (res->positionOfTerm)
1591 printf (", position=%d", *res->positionOfTerm);
1593 if (*res->scanStatus != Z_Scan_success)
1594 printf("Scan returned code %d\n", *res->scanStatus);
1597 if ((entries = res->entries->entries))
1598 num_entries = res->entries->num_entries;
1599 for (i = 0; i < num_entries; i++)
1601 int pos_term = res->positionOfTerm ? *res->positionOfTerm : -1;
1602 if (entries[i]->which == Z_Entry_termInfo)
1604 printf("%c ", i + 1 == pos_term ? '*' : ' ');
1605 display_term(entries[i]->u.termInfo);
1608 display_diagrecs(&entries[i]->u.surrogateDiagnostic, 1);
1610 if (res->entries->nonsurrogateDiagnostics)
1611 display_diagrecs (res->entries->nonsurrogateDiagnostics,
1612 res->entries->num_nonsurrogateDiagnostics);
1615 void process_sortResponse(Z_SortResponse *res)
1617 printf("Received SortResponse: status=");
1618 switch (*res->sortStatus)
1620 case Z_SortStatus_success:
1621 printf ("success"); break;
1622 case Z_SortStatus_partial_1:
1623 printf ("partial"); break;
1624 case Z_SortStatus_failure:
1625 printf ("failure"); break;
1627 printf ("unknown (%d)", *res->sortStatus);
1630 print_refid (res->referenceId);
1632 if (res->diagnostics)
1633 display_diagrecs(res->diagnostics,
1634 res->num_diagnostics);
1636 if (res->diagnostics)
1637 display_diagrecs(res->diagnostics->diagRecs,
1638 res->diagnostics->num_diagRecs);
1642 int cmd_sort_generic(char *arg, int newset)
1646 printf("Session not initialized yet\n");
1649 if (!ODR_MASK_GET(session->options, Z_Options_sort))
1651 printf("Target doesn't support sort\n");
1656 if (send_sortrequest(arg, newset) < 0)
1663 int cmd_sort(char *arg)
1665 return cmd_sort_generic (arg, 0);
1668 int cmd_sort_newset (char *arg)
1670 return cmd_sort_generic (arg, 1);
1673 int cmd_scan(char *arg)
1677 printf("Session not initialized yet\n");
1680 if (!ODR_MASK_GET(session->options, Z_Options_scan))
1682 printf("Target doesn't support scan\n");
1687 if (send_scanrequest(arg, 1, 20) < 0)
1691 if (send_scanrequest(last_scan, 1, 20) < 0)
1696 int cmd_format(char *arg)
1700 printf("Usage: format <recordsyntax>\n");
1703 recordsyntax = oid_getvalbyname (arg);
1704 if (recordsyntax == VAL_NONE)
1706 printf ("unknown record syntax\n");
1712 int cmd_elements(char *arg)
1714 static Z_ElementSetNames esn;
1715 static char what[100];
1719 printf("Usage: elements <esn>\n");
1723 esn.which = Z_ElementSetNames_generic;
1724 esn.u.generic = what;
1725 elementSetNames = &esn;
1729 int cmd_attributeset(char *arg)
1735 printf("Usage: attributeset <setname>\n");
1738 sscanf(arg, "%s", what);
1739 if (p_query_attset (what))
1741 printf("Unknown attribute set name\n");
1747 int cmd_querytype (char *arg)
1749 if (!strcmp (arg, "ccl"))
1750 queryType = QueryType_CCL;
1751 else if (!strcmp (arg, "prefix"))
1752 queryType = QueryType_Prefix;
1754 else if (!strcmp (arg, "ccl2rpn") || !strcmp (arg, "cclrpn"))
1755 queryType = QueryType_CCL2RPN;
1759 printf ("Querytype must be one of:\n");
1760 printf (" prefix - Prefix query\n");
1761 printf (" ccl - CCL query\n");
1763 printf (" ccl2rpn - CCL query converted to RPN\n");
1770 int cmd_refid (char *arg)
1776 refid = (char *) xmalloc (strlen(arg)+1);
1777 strcpy (refid, arg);
1782 int cmd_close(char *arg)
1789 apdu = zget_APDU(out, Z_APDU_close);
1790 req = apdu->u.close;
1791 *req->closeReason = Z_Close_finished;
1793 printf("Sent close request.\n");
1798 static void initialize(void)
1804 if (!(out = odr_createmem(ODR_ENCODE)) ||
1805 !(in = odr_createmem(ODR_DECODE)) ||
1806 !(print = odr_createmem(ODR_PRINT)))
1808 fprintf(stderr, "failed to allocate ODR streams\n");
1811 setvbuf(stdout, 0, _IONBF, 0);
1814 bibset = ccl_qual_mk ();
1815 inf = fopen ("default.bib", "r");
1818 ccl_qual_file (bibset, inf);
1822 cmd_base("Default");
1825 static int client(int wait)
1829 int (*fun)(char *arg);
1832 {"open", cmd_open, "('tcp'|'osi')':'[<tsel>'/']<host>[':'<port>]"},
1833 {"quit", cmd_quit, ""},
1834 {"find", cmd_find, "<query>"},
1835 {"delete", cmd_delete, "<setname>"},
1836 {"base", cmd_base, "<base-name>"},
1837 {"show", cmd_show, "<rec#>['+'<#recs>['+'<setname>]]"},
1838 {"scan", cmd_scan, "<term>"},
1839 {"sort", cmd_sort, "<sortkey> <flag> <sortkey> <flag> ..."},
1840 {"sort+", cmd_sort_newset, "<sortkey> <flag> <sortkey> <flag> ..."},
1841 {"authentication", cmd_authentication, "<acctstring>"},
1842 {"lslb", cmd_lslb, "<largeSetLowerBound>"},
1843 {"ssub", cmd_ssub, "<smallSetUpperBound>"},
1844 {"mspn", cmd_mspn, "<mediumSetPresentNumber>"},
1845 {"status", cmd_status, ""},
1846 {"setnames", cmd_setnames, ""},
1847 {"cancel", cmd_cancel, ""},
1848 {"format", cmd_format, "<recordsyntax>"},
1849 {"elements", cmd_elements, "<elementSetName>"},
1850 {"close", cmd_close, ""},
1851 {"attributeset", cmd_attributeset, "<attrset>"},
1852 {"querytype", cmd_querytype, "<type>"},
1853 {"refid", cmd_refid, "<id>"},
1854 {"itemorder", cmd_itemorder, "<item>"},
1858 int netbufferlen = 0;
1868 char line[1024], word[1024], arg[1024];
1874 FD_SET(cs_fileno(conn), &input);
1875 if ((res = select(20, &input, 0, 0, 0)) < 0)
1882 if (!wait && FD_ISSET(0, &input))
1887 /* quick & dirty way to get a command line. */
1889 if (!fgets(line, 1023, stdin))
1891 if ((end_p = strchr (line, '\n')))
1893 if ((res = sscanf(line, "%s %[^;]", word, arg)) <= 0)
1895 strcpy(word, last_cmd);
1900 strcpy(last_cmd, word);
1901 for (i = 0; cmd[i].cmd; i++)
1902 if (!strncmp(cmd[i].cmd, word, strlen(word)))
1904 res = (*cmd[i].fun)(arg);
1907 if (!cmd[i].cmd) /* dump our help-screen */
1909 printf("Unknown command: %s.\n", word);
1910 printf("Currently recognized commands:\n");
1911 for (i = 0; cmd[i].cmd; i++)
1912 printf(" %s %s\n", cmd[i].cmd, cmd[i].ad);
1923 if (conn && FD_ISSET(cs_fileno(conn), &input))
1928 if ((res = cs_get(conn, &netbuffer, &netbufferlen)) < 0)
1935 printf("Target closed connection.\n");
1938 odr_reset(in); /* release APDU from last round */
1939 odr_setbuf(in, netbuffer, res, 0);
1940 if (!z_APDU(in, &apdu, 0))
1942 odr_perror(in, "Decoding incoming APDU");
1943 fprintf(stderr, "[Near %d]\n", odr_offset(in));
1944 fprintf(stderr, "Packet dump:\n---------\n");
1945 odr_dumpBER(stderr, netbuffer, res);
1946 fprintf(stderr, "---------\n");
1950 if (!z_APDU(print, &apdu, 0))
1952 odr_perror(print, "Failed to print incoming APDU");
1959 case Z_APDU_initResponse:
1960 process_initResponse(apdu->u.initResponse);
1962 case Z_APDU_searchResponse:
1963 process_searchResponse(apdu->u.searchResponse);
1965 case Z_APDU_scanResponse:
1966 process_scanResponse(apdu->u.scanResponse);
1968 case Z_APDU_presentResponse:
1969 print_refid (apdu->u.presentResponse->referenceId);
1971 *apdu->u.presentResponse->numberOfRecordsReturned;
1972 if (apdu->u.presentResponse->records)
1973 display_records(apdu->u.presentResponse->records);
1975 printf("No records.\n");
1976 printf ("nextResultSetPosition = %d\n",
1977 *apdu->u.presentResponse->nextResultSetPosition);
1979 case Z_APDU_sortResponse:
1980 process_sortResponse(apdu->u.sortResponse);
1982 case Z_APDU_extendedServicesResponse:
1983 printf("Got extended services response\n");
1984 process_ESResponse(apdu->u.extendedServicesResponse);
1987 printf("Target has closed the association.\n");
1988 process_close(apdu->u.close);
1990 case Z_APDU_resourceControlRequest:
1991 process_resourceControlRequest
1992 (apdu->u.resourceControlRequest);
1994 case Z_APDU_deleteResultSetResponse:
1995 printf("Got deleteResultSetResponse status=%d\n",
1996 *apdu->u.deleteResultSetResponse->
1997 deleteOperationStatus);
2000 printf("Received unknown APDU type (%d).\n",
2005 while (conn && cs_more(conn));
2013 int main(int argc, char **argv)
2020 while ((ret = options("m:v:", argv, argc, &arg)) != -2)
2028 if (cmd_open (arg) == 2)
2033 if (!(marcdump = fopen (arg, "a")))
2040 log_init (log_mask_str(arg), "", NULL);
2043 fprintf (stderr, "Usage: %s [-m <marclog>] [<server-addr>]\n",
2053 return client (opened);