2 * Copyright (c) 1995-1998, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.70 1998-09-22 09:40:37 adam
8 * Minor changes in sort spec.
10 * Revision 1.69 1998/08/19 16:10:06 adam
11 * Changed som member names of DeleteResultSetRequest/Response.
13 * Revision 1.68 1998/07/20 12:37:06 adam
14 * Added 'delete <resultset>' command. Changed open command so that
15 * it reconnects if already connected.
17 * Revision 1.67 1998/06/09 13:55:06 adam
20 * Revision 1.66 1998/05/18 13:06:53 adam
21 * Changed the way attribute sets are handled by the retriaval module.
22 * Extended Explain conversion / schema.
23 * Modified server and client to work with ASN.1 compiled protocol handlers.
25 * Revision 1.65 1998/03/31 15:13:19 adam
26 * Development towards compiled ASN.1.
28 * Revision 1.64 1998/03/31 11:07:44 adam
29 * Furhter work on UNIverse resource report.
30 * Added Extended Services handling in frontend server.
32 * Revision 1.63 1998/03/05 08:05:10 adam
33 * Added a few casts to make C++ happy.
35 * Revision 1.62 1998/02/11 11:53:33 adam
36 * Changed code so that it compiles as C++.
38 * Revision 1.61 1998/02/10 11:03:06 adam
39 * Implemented command refid. Client prints reference-ID's, when present,
42 * Revision 1.60 1998/01/29 14:08:52 adam
43 * Better sort diagnostics.
45 * Revision 1.59 1998/01/29 13:17:56 adam
48 * Revision 1.58 1998/01/07 13:51:45 adam
51 * Revision 1.57 1998/01/07 12:58:22 adam
52 * Using fgets instead of gets.
54 * Revision 1.56 1997/11/05 09:18:31 adam
55 * The client handles records with no associated syntax.
57 * Revision 1.55 1997/10/31 12:20:08 adam
58 * Improved memory debugging for xmalloc/nmem.c. References to NMEM
59 * instead of ODR in n ESPEC-1 handling in source d1_espec.c.
60 * Bug fix: missing fclose in data1_read_espec1.
62 * Revision 1.54 1997/10/27 13:52:46 adam
63 * Header yaz-util includes all YAZ utility header files.
65 * Revision 1.53 1997/09/29 13:18:59 adam
66 * Added function, oid_ent_to_oid, to replace the function
67 * oid_getoidbyent, which is not thread safe.
69 * Revision 1.52 1997/09/29 07:20:31 adam
70 * Client code uses nmem_init.
72 * Revision 1.51 1997/09/26 09:41:55 adam
73 * Updated client to handle multiple diagnostics.
75 * Revision 1.50 1997/09/17 12:10:29 adam
78 * Revision 1.49 1997/09/04 13:45:17 adam
79 * Added UNImarc to list of available syntaxes.
81 * Revision 1.48 1997/09/01 08:48:44 adam
82 * New windows NT/95 port using MSV5.0. Only a few changes made
83 * to avoid warnings. Sub project created: client.dsp.
85 * Revision 1.47 1997/07/01 13:49:56 adam
86 * Take care of case when invalid target is specified on command line.
88 * Revision 1.46 1997/06/23 10:30:18 adam
89 * Added call to ccl_rpn_delete in search. Added ODR stream "out"
90 * as parameter to ccl_rpn_query to release RPN query.
92 * Revision 1.45 1997/05/14 06:53:29 adam
95 * Revision 1.44 1997/05/05 11:20:35 adam
96 * Client uses "options" utility and marc dump filename may be specified
97 * as an option (-m <file>).
99 * Revision 1.43 1996/11/08 11:03:26 adam
100 * Client accepts multiple database names.
102 * Revision 1.42 1996/10/08 10:44:57 quinn
103 * Resolved conflicts.
105 * Revision 1.41 1996/10/07 15:29:03 quinn
108 * Revision 1.40 1996/08/29 14:19:34 quinn
109 * Fixed conflict (CVS)
111 * Revision 1.39 1996/08/27 10:43:22 quinn
112 * Made select() optional
114 * Revision 1.38 1996/08/12 14:09:11 adam
115 * Default prefix query attribute set defined by using p_query_attset.
117 * Revision 1.37 1996/07/06 19:58:29 quinn
118 * System headerfiles gathered in yconfig
120 * Revision 1.36 1996/06/10 08:53:47 quinn
123 * Revision 1.35 1996/06/03 09:45:50 quinn
124 * Added display of OIDs in the GRS routine.
126 * Revision 1.34 1996/05/09 07:26:49 quinn
127 * *** empty log message ***
129 * Revision 1.33 1996/05/09 07:25:22 quinn
132 * Revision 1.32 1996/03/15 11:05:33 adam
133 * The user can set the preferred query type (prefix, ccl, ..) with the
136 * Revision 1.31 1996/02/20 12:51:54 quinn
137 * Fixed problems with EXTERNAL.
139 * Revision 1.30 1996/02/12 18:18:09 quinn
142 * Revision 1.29 1996/01/02 08:57:25 quinn
143 * Changed enums in the ASN.1 .h files to #defines. Changed oident.class to oclass
145 * Revision 1.28 1995/12/14 11:09:31 quinn
146 * Added Explain record syntax to the format command.
148 * Revision 1.27 1995/12/12 16:37:02 quinn
149 * Added destroy element to data1_node.
151 * Revision 1.26 1995/12/12 14:11:00 quinn
154 * Revision 1.25 1995/11/13 09:27:22 quinn
155 * Fiddling with the variant stuff.
157 * Revision 1.24 1995/10/30 12:41:13 quinn
158 * Added hostname lookup for server.
160 * Revision 1.23 1995/10/18 16:12:30 quinn
161 * Better diagnostics.
163 * Revision 1.22 1995/10/11 14:49:12 quinn
166 * Revision 1.21 1995/09/29 17:01:47 quinn
169 * Revision 1.20 1995/08/29 14:24:13 quinn
170 * Added second half of close-handshake
172 * Revision 1.19 1995/08/29 11:17:28 quinn
173 * Added code to receive close
175 * Revision 1.18 1995/08/28 12:21:27 quinn
176 * Client can now ask for simple element set names.
178 * Revision 1.17 1995/08/17 12:45:02 quinn
179 * Fixed minor problems with GRS-1. Added support in c&s.
181 * Revision 1.16 1995/08/15 12:00:04 quinn
184 * Revision 1.15 1995/06/22 09:28:03 quinn
185 * Fixed bug in SUTRS processing.
187 * Revision 1.14 1995/06/19 12:37:41 quinn
190 * Revision 1.13 1995/06/16 10:29:11 quinn
191 * *** empty log message ***
193 * Revision 1.12 1995/06/15 07:44:57 quinn
196 * Revision 1.11 1995/06/14 15:26:40 quinn
197 * *** empty log message ***
199 * Revision 1.10 1995/06/06 14:56:58 quinn
200 * Better diagnostics.
202 * Revision 1.9 1995/06/06 08:15:19 quinn
205 * Revision 1.8 1995/06/05 10:52:22 quinn
208 * Revision 1.7 1995/06/02 09:50:09 quinn
211 * Revision 1.6 1995/05/31 08:29:21 quinn
212 * Nothing significant.
214 * Revision 1.5 1995/05/29 08:10:47 quinn
215 * Moved oid.c to util.
217 * Revision 1.4 1995/05/22 15:30:13 adam
218 * Client uses prefix query notation.
220 * Revision 1.3 1995/05/22 15:06:53 quinn
221 * *** empty log message ***
223 * Revision 1.2 1995/05/22 14:56:40 quinn
224 * *** empty log message ***
226 * Revision 1.1 1995/05/22 11:30:31 quinn
227 * Added prettier client.
233 * This is the obligatory little toy client, whose primary purpose is
234 * to illustrate the use of the YAZ service-level API.
242 #include <yaz-util.h>
244 #include <comstack.h>
251 #include <marcdisp.h>
252 #include <diagbib1.h>
260 #define C_PROMPT "Z> "
262 static ODR out, in, print; /* encoding and decoding streams */
263 static COMSTACK conn = 0; /* our z-association */
264 static Z_IdAuthentication *auth = 0; /* our current auth definition */
265 static char *databaseNames[128];
266 static int num_databaseNames = 0;
267 static int setnumber = 0; /* current result set number */
268 static int smallSetUpperBound = 0;
269 static int largeSetLowerBound = 1;
270 static int mediumSetPresentNumber = 0;
271 static Z_ElementSetNames *elementSetNames = 0;
272 static int setno = 1; /* current set offset */
273 static enum oid_proto protocol = PROTO_Z3950; /* current app protocol */
274 static enum oid_value recordsyntax = VAL_USMARC;
275 static int sent_close = 0;
276 static NMEM session_mem = NULL; /* memory handle for init-response */
277 static Z_InitResponse *session = 0; /* session parameters */
278 static char last_scan[512] = "0";
279 static char last_cmd[100] = "?";
280 static FILE *marcdump = 0;
281 static char *refid = NULL;
289 static QueryType queryType = QueryType_Prefix;
292 static CCL_bibset bibset; /* CCL bibset handle */
295 static void send_apdu(Z_APDU *a)
300 if (!z_APDU(out, &a, 0))
302 odr_perror(out, "Encoding APDU");
305 buf = odr_getbuf(out, &len, 0);
306 if (cs_put(conn, buf, len) < 0)
308 fprintf(stderr, "cs_put: %s", cs_errmsg(cs_errno(conn)));
311 odr_reset(out); /* release the APDU structure */
314 static void print_refid (Z_ReferenceId *id)
318 printf ("ReferenceId: '%.*s'\n", id->len, id->buf);
322 static Z_ReferenceId *set_refid (ODR out)
327 id = (Z_ReferenceId *) odr_malloc (out, sizeof(*id));
328 id->size = id->len = strlen(refid);
329 id->buf = (unsigned char *) odr_malloc (out, id->len);
330 memcpy (id->buf, refid, id->len);
334 /* INIT SERVICE ------------------------------- */
336 static void send_initRequest()
338 Z_APDU *apdu = zget_APDU(out, Z_APDU_initRequest);
339 Z_InitRequest *req = apdu->u.initRequest;
341 ODR_MASK_SET(req->options, Z_Options_search);
342 ODR_MASK_SET(req->options, Z_Options_present);
343 ODR_MASK_SET(req->options, Z_Options_namedResultSets);
344 ODR_MASK_SET(req->options, Z_Options_triggerResourceCtrl);
345 ODR_MASK_SET(req->options, Z_Options_scan);
346 ODR_MASK_SET(req->options, Z_Options_sort);
347 ODR_MASK_SET(req->options, Z_Options_extendedServices);
348 ODR_MASK_SET(req->options, Z_Options_delSet);
350 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_1);
351 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_2);
352 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_3);
354 *req->maximumRecordSize = 1024*1024;
356 req->idAuthentication = auth;
359 printf("Sent initrequest.\n");
362 static int process_initResponse(Z_InitResponse *res)
364 /* save session parameters for later use */
365 session_mem = odr_extract_mem(in);
369 printf("Connection rejected by target.\n");
371 printf("Connection accepted by target.\n");
372 if (res->implementationId)
373 printf("ID : %s\n", res->implementationId);
374 if (res->implementationName)
375 printf("Name : %s\n", res->implementationName);
376 if (res->implementationVersion)
377 printf("Version: %s\n", res->implementationVersion);
378 if (res->userInformationField)
380 printf("UserInformationfield:\n");
381 if (!z_External(print, (Z_External**)&res-> userInformationField,
384 odr_perror(print, "Printing userinfo\n");
387 if (res->userInformationField->which == Z_External_octet)
389 printf("Guessing visiblestring:\n");
390 printf("'%s'\n", res->userInformationField->u. octet_aligned->buf);
397 static int cmd_base(char *arg)
404 printf("Usage: base <database> <database> ...\n");
407 for (i = 0; i<num_databaseNames; i++)
408 xfree (databaseNames[i]);
409 num_databaseNames = 0;
412 if (!(cp = strchr(arg, ' ')))
413 cp = arg + strlen(arg);
416 databaseNames[num_databaseNames] = (char *)xmalloc (1 + cp - arg);
417 memcpy (databaseNames[num_databaseNames], arg, cp - arg);
418 databaseNames[num_databaseNames++][cp - arg] = '\0';
427 int cmd_open(char *arg)
430 char type[100], addr[100], base[100];
435 printf("Already connected.\n");
441 nmem_destroy (session_mem);
446 if (!*arg || sscanf(arg, "%[^:]:%[^/]/%s", type, addr, base) < 2)
448 fprintf(stderr, "Usage: open (osi|tcp) ':' [tsel '/']host[':'port]\n");
453 if (!strcmp(type, "tcp"))
456 protocol = PROTO_Z3950;
460 if (!strcmp(type, "osi"))
468 fprintf(stderr, "Bad type: %s\n", type);
471 if (!(conn = cs_create(t, 1, protocol)))
476 if (!(add = cs_straddr(conn, addr)))
481 printf("Connecting...");
483 if (cs_connect(conn, add) < 0)
495 int cmd_authentication(char *arg)
497 static Z_IdAuthentication au;
498 static char open[256];
502 printf("Auth field set to null\n");
507 au.which = Z_IdAuthentication_open;
513 /* SEARCH SERVICE ------------------------------ */
515 static void display_variant(Z_Variant *v, int level)
519 for (i = 0; i < v->num_triples; i++)
521 printf("%*sclass=%d,type=%d", level * 4, "", *v->triples[i]->zclass,
522 *v->triples[i]->type);
523 if (v->triples[i]->which == Z_Triple_internationalString)
524 printf(",value=%s\n", v->triples[i]->value.internationalString);
530 static void display_grs1(Z_GenericRecord *r, int level)
536 for (i = 0; i < r->num_elements; i++)
540 printf("%*s", level * 4, "");
544 printf("%d,", *t->tagType);
547 if (t->tagValue->which == Z_StringOrNumeric_numeric)
548 printf("%d) ", *t->tagValue->u.numeric);
550 printf("%s) ", t->tagValue->u.string);
551 if (t->content->which == Z_ElementData_subtree)
554 display_grs1(t->content->u.subtree, level+1);
556 else if (t->content->which == Z_ElementData_string)
557 printf("%s\n", t->content->u.string);
558 else if (t->content->which == Z_ElementData_numeric)
559 printf("%d\n", *t->content->u.numeric);
560 else if (t->content->which == Z_ElementData_oid)
562 int *ip = t->content->u.oid;
565 if ((oent = oid_getentbyoid(t->content->u.oid)))
566 printf("OID: %s\n", oent->desc);
570 while (ip && *ip >= 0)
571 printf(" %d", *(ip++));
575 else if (t->content->which == Z_ElementData_noDataRequested)
576 printf("[No data requested]\n");
577 else if (t->content->which == Z_ElementData_elementEmpty)
578 printf("[Element empty]\n");
579 else if (t->content->which == Z_ElementData_elementNotThere)
580 printf("[Element not there]\n");
583 if (t->appliedVariant)
584 display_variant(t->appliedVariant, level+1);
585 if (t->metaData && t->metaData->supportedVariants)
589 printf("%*s---- variant list\n", (level+1)*4, "");
590 for (c = 0; c < t->metaData->num_supportedVariants; c++)
592 printf("%*svariant #%d\n", (level+1)*4, "", c);
593 display_variant(t->metaData->supportedVariants[c], level + 2);
599 static void display_record(Z_DatabaseRecord *p)
601 Z_External *r = (Z_External*) p;
602 oident *ent = oid_getentbyoid(r->direct_reference);
605 * Tell the user what we got.
607 if (r->direct_reference)
609 printf("Record type: ");
611 printf("%s\n", ent->desc);
612 else if (!odr_oid(print, &r->direct_reference, 0))
614 odr_perror(print, "print oid");
618 /* Check if this is a known, ASN.1 type tucked away in an octet string */
619 if (ent && r->which == Z_External_octet)
621 Z_ext_typeent *type = z_ext_getentbyref(ent->value);
627 * Call the given decoder to process the record.
629 odr_setbuf(in, (char*)p->u.octet_aligned->buf,
630 p->u.octet_aligned->len, 0);
631 if (!(*type->fun)(in, (char **)&rr, 0))
633 odr_perror(in, "Decoding constructed record.");
634 fprintf(stderr, "[Near %d]\n", odr_offset(in));
635 fprintf(stderr, "Packet dump:\n---------\n");
636 odr_dumpBER(stderr, (char*)p->u.octet_aligned->buf,
637 p->u.octet_aligned->len);
638 fprintf(stderr, "---------\n");
642 * Note: we throw away the original, BER-encoded record here.
643 * Do something else with it if you want to keep it.
645 r->u.sutrs = (Z_SUTRS *) rr; /* we don't actually check the type here. */
646 r->which = type->what;
649 if (ent && ent->value == VAL_SOIF)
650 printf("%.*s", r->u.octet_aligned->len, r->u.octet_aligned->buf);
651 else if (r->which == Z_External_octet && p->u.octet_aligned->len)
653 const char *marc_buf = (char*)p->u.octet_aligned->buf;
654 marc_display (marc_buf, NULL);
656 fwrite (marc_buf, strlen (marc_buf), 1, marcdump);
658 else if (ent && ent->value == VAL_SUTRS)
660 if (r->which != Z_External_sutrs)
662 printf("Expecting single SUTRS type for SUTRS.\n");
665 printf("%.*s", r->u.sutrs->len, r->u.sutrs->buf);
667 else if (ent && ent->value == VAL_GRS1)
669 if (r->which != Z_External_grs1)
671 printf("Expecting single GRS type for GRS.\n");
674 display_grs1(r->u.grs1, 0);
678 printf("Unknown record representation.\n");
679 if (!z_External(print, &r, 0))
681 odr_perror(print, "Printing external");
688 static void display_diagrecs(Z_DiagRec **pp, int num)
692 Z_DefaultDiagFormat *r;
694 printf("Diagnostic message(s) from database:\n");
695 for (i = 0; i<num; i++)
697 Z_DiagRec *p = pp[i];
698 if (p->which != Z_DiagRec_defaultFormat)
700 printf("Diagnostic record not in default format.\n");
704 r = p->u.defaultFormat;
705 if (!(ent = oid_getentbyoid(r->diagnosticSetId)) ||
706 ent->oclass != CLASS_DIAGSET || ent->value != VAL_BIB1)
707 printf("Missing or unknown diagset\n");
708 printf(" [%d] %s", *r->condition, diagbib1_str(*r->condition));
712 case Z_DefaultDiagFormat_v2Addinfo:
713 printf (" -- v2 addinfo '%s'\n", r->u.v2Addinfo);
715 case Z_DefaultDiagFormat_v3Addinfo:
716 printf (" -- v3 addinfo '%s'\n", r->u.v3Addinfo);
720 if (r->addinfo && *r->addinfo)
721 printf(" -- '%s'\n", r->addinfo);
729 static void display_nameplusrecord(Z_NamePlusRecord *p)
732 printf("[%s]", p->databaseName);
733 if (p->which == Z_NamePlusRecord_surrogateDiagnostic)
734 display_diagrecs(&p->u.surrogateDiagnostic, 1);
736 display_record(p->u.databaseRecord);
739 static void display_records(Z_Records *p)
743 if (p->which == Z_Records_NSD)
746 Z_DiagRec dr, *dr_p = &dr;
747 dr.which = Z_DiagRec_defaultFormat;
748 dr.u.defaultFormat = p->u.nonSurrogateDiagnostic;
749 display_diagrecs (&dr_p, 1);
751 display_diagrecs (&p->u.nonSurrogateDiagnostic, 1);
754 else if (p->which == Z_Records_multipleNSD)
755 display_diagrecs (p->u.multipleNonSurDiagnostics->diagRecs,
756 p->u.multipleNonSurDiagnostics->num_diagRecs);
759 printf("Records: %d\n", p->u.databaseOrSurDiagnostics->num_records);
760 for (i = 0; i < p->u.databaseOrSurDiagnostics->num_records; i++)
761 display_nameplusrecord(p->u.databaseOrSurDiagnostics->records[i]);
765 static int send_deleteResultSetRequest(char *arg)
767 Z_APDU *apdu = zget_APDU(out, Z_APDU_deleteResultSetRequest);
768 Z_DeleteResultSetRequest *req = apdu->u.deleteResultSetRequest;
770 req->referenceId = set_refid (out);
772 req->num_resultSetList = 1;
773 req->resultSetList = (char **)
774 odr_malloc (out, sizeof(*req->resultSetList));
775 *req->resultSetList = arg;
776 req->deleteFunction = (int *)
777 odr_malloc (out, sizeof(*req->deleteFunction));
778 *req->deleteFunction = Z_DeleteRequest_list;
781 printf("Sent deleteResultSetRequest.\n");
785 static int send_searchRequest(char *arg)
787 Z_APDU *apdu = zget_APDU(out, Z_APDU_searchRequest);
788 Z_SearchRequest *req = apdu->u.searchRequest;
792 struct ccl_rpn_node *rpn = NULL;
797 Z_RPNQuery *RPNquery;
801 if (queryType == QueryType_CCL2RPN)
803 rpn = ccl_find_str(bibset, arg, &error, &pos);
806 printf("CCL ERROR: %s\n", ccl_err_msg(error));
811 req->referenceId = set_refid (out);
812 if (!strcmp(arg, "@big")) /* strictly for troublemaking */
814 static unsigned char big[2100];
817 /* send a very big referenceid to test transport stack etc. */
818 memset(big, 'A', 2100);
819 bigo.len = bigo.size = 2100;
821 req->referenceId = &bigo;
826 sprintf(setstring, "%d", ++setnumber);
827 req->resultSetName = setstring;
829 *req->smallSetUpperBound = smallSetUpperBound;
830 *req->largeSetLowerBound = largeSetLowerBound;
831 *req->mediumSetPresentNumber = mediumSetPresentNumber;
832 if (smallSetUpperBound > 0 || (largeSetLowerBound > 1 &&
833 mediumSetPresentNumber > 0))
837 prefsyn.proto = protocol;
838 prefsyn.oclass = CLASS_RECSYN;
839 prefsyn.value = recordsyntax;
840 req->preferredRecordSyntax =
841 odr_oiddup(out, oid_ent_to_oid(&prefsyn, oid));
842 req->smallSetElementSetNames =
843 req->mediumSetElementSetNames = elementSetNames;
845 req->num_databaseNames = num_databaseNames;
846 req->databaseNames = databaseNames;
852 case QueryType_Prefix:
853 query.which = Z_Query_type_1;
854 RPNquery = p_query_rpn (out, protocol, arg);
857 printf("Prefix query error\n");
860 query.u.type_1 = RPNquery;
863 query.which = Z_Query_type_2;
864 query.u.type_2 = &ccl_query;
865 ccl_query.buf = (unsigned char*) arg;
866 ccl_query.len = strlen(arg);
869 case QueryType_CCL2RPN:
870 query.which = Z_Query_type_1;
871 RPNquery = ccl_rpn_query(out, rpn);
873 bib1.proto = protocol;
874 bib1.oclass = CLASS_ATTSET;
875 bib1.value = VAL_BIB1;
876 RPNquery->attributeSetId = oid_ent_to_oid(&bib1, oid);
877 query.u.type_1 = RPNquery;
878 ccl_rpn_delete (rpn);
882 printf ("Unsupported query type\n");
887 printf("Sent searchRequest.\n");
891 static int process_searchResponse(Z_SearchResponse *res)
893 printf ("Received SearchResponse.\n");
894 print_refid (res->referenceId);
895 if (*res->searchStatus)
896 printf("Search was a success.\n");
898 printf("Search was a bloomin' failure.\n");
899 printf("Number of hits: %d, setno %d\n",
900 *res->resultCount, setnumber);
901 printf("records returned: %d\n",
902 *res->numberOfRecordsReturned);
903 setno += *res->numberOfRecordsReturned;
905 display_records(res->records);
909 static void print_level(int iLevel)
912 for (i = 0; i < iLevel * 4; i++)
916 static void print_int(int iLevel, const char *pTag, int *pInt)
921 printf("%s: %d\n", pTag, *pInt);
925 static void print_string(int iLevel, const char *pTag, const char *pString)
930 printf("%s: %s\n", pTag, pString);
934 static void print_oid(int iLevel, const char *pTag, Odr_oid *pOid)
942 for (; *pInt != -1; pInt++)
943 printf(" %d", *pInt);
948 static void print_referenceId(int iLevel, Z_ReferenceId *referenceId)
950 if (referenceId != NULL)
955 printf("Ref Id (%d, %d): ", referenceId->len, referenceId->size);
956 for (i = 0; i < referenceId->len; i++)
957 printf("%c", referenceId->buf[i]);
962 static void print_string_or_numeric(int iLevel, const char *pTag, Z_StringOrNumeric *pStringNumeric)
964 if (pStringNumeric != NULL)
966 switch (pStringNumeric->which)
968 case Z_StringOrNumeric_string:
969 print_string(iLevel, pTag, pStringNumeric->u.string);
972 case Z_StringOrNumeric_numeric:
973 print_int(iLevel, pTag, pStringNumeric->u.numeric);
978 printf("%s: valid type for Z_StringOrNumeric\n", pTag);
984 static void print_universe_report_duplicate(int iLevel, Z_UniverseReportDuplicate *pUniverseReportDuplicate)
986 if (pUniverseReportDuplicate != NULL)
989 printf("Universe Report Duplicate: \n");
991 print_string_or_numeric(iLevel, "Hit No", pUniverseReportDuplicate->hitno);
995 static void print_universe_report_hits(int iLevel, Z_UniverseReportHits *pUniverseReportHits)
997 if (pUniverseReportHits != NULL)
1000 printf("Universe Report Hits: \n");
1002 print_string_or_numeric(iLevel, "Database", pUniverseReportHits->database);
1003 print_string_or_numeric(iLevel, "Hits", pUniverseReportHits->hits);
1007 static void print_universe_report(int iLevel, Z_UniverseReport *pUniverseReport)
1009 if (pUniverseReport != NULL)
1011 print_level(iLevel);
1012 printf("Universe Report: \n");
1014 print_int(iLevel, "Total Hits", pUniverseReport->totalHits);
1015 switch (pUniverseReport->which)
1017 case Z_UniverseReport_databaseHits:
1018 print_universe_report_hits(iLevel, pUniverseReport->u.databaseHits);
1021 case Z_UniverseReport_duplicate:
1022 print_universe_report_duplicate(iLevel, pUniverseReport->u.duplicate);
1026 print_level(iLevel);
1027 printf("Type: %d\n", pUniverseReport->which);
1033 static void print_external(int iLevel, Z_External *pExternal)
1035 if (pExternal != NULL)
1037 print_level(iLevel);
1038 printf("External: \n");
1040 print_oid(iLevel, "Direct Reference", pExternal->direct_reference);
1041 print_int(iLevel, "InDirect Reference", pExternal->indirect_reference);
1042 print_string(iLevel, "Descriptor", pExternal->descriptor);
1043 switch (pExternal->which)
1045 case Z_External_universeReport:
1046 print_universe_report(iLevel, pExternal->u.universeReport);
1050 print_level(iLevel);
1051 printf("Type: %d\n", pExternal->which);
1057 static int process_resourceControlRequest (Z_ResourceControlRequest *req)
1059 printf ("Received ResourceControlRequest.\n");
1060 print_referenceId(1, req->referenceId);
1061 print_int(1, "Suspended Flag", req->suspendedFlag);
1062 print_int(1, "Partial Results Available", req->partialResultsAvailable);
1063 print_int(1, "Response Required", req->responseRequired);
1064 print_int(1, "Triggered Request Flag", req->triggeredRequestFlag);
1065 print_external(1, req->resourceReport);
1069 void process_ESResponse(Z_ExtendedServicesResponse *res)
1071 printf("process_ESResponse\n");
1074 static Z_External *CreateItemOrderExternal(int itemno)
1076 Z_External *r = (Z_External *) odr_malloc(out, sizeof(Z_External));
1077 oident ItemOrderRequest;
1079 ItemOrderRequest.proto = PROTO_Z3950;
1080 ItemOrderRequest.oclass = CLASS_EXTSERV;
1081 ItemOrderRequest.value = VAL_ITEMORDER;
1083 r->direct_reference = odr_oiddup(out,oid_getoidbyent(&ItemOrderRequest));
1084 r->indirect_reference = (int *) odr_malloc(out,sizeof(int));
1085 *r->indirect_reference = 0;
1087 r->descriptor = "Extended services item order";
1089 r->which = Z_External_itemOrder;
1091 r->u.itemOrder = (Z_ItemOrder *) odr_malloc(out,sizeof(Z_ItemOrder));
1092 memset(r->u.itemOrder, 0, sizeof(Z_ItemOrder));
1094 r->u.itemOrder->which=Z_IOItemOrder_esRequest;
1096 r->u.itemOrder->which=Z_ItemOrder_esRequest;
1099 r->u.itemOrder->u.esRequest = (Z_IORequest *)
1100 odr_malloc(out,sizeof(Z_IORequest));
1101 memset(r->u.itemOrder->u.esRequest, 0, sizeof(Z_IORequest));
1103 r->u.itemOrder->u.esRequest->toKeep = (Z_IOOriginPartToKeep *)
1104 odr_malloc(out,sizeof(Z_IOOriginPartToKeep));
1105 memset(r->u.itemOrder->u.esRequest->toKeep, 0, sizeof(Z_IOOriginPartToKeep));
1106 r->u.itemOrder->u.esRequest->notToKeep = (Z_IOOriginPartNotToKeep *)
1107 odr_malloc(out,sizeof(Z_IOOriginPartNotToKeep));
1108 memset(r->u.itemOrder->u.esRequest->notToKeep, 0, sizeof(Z_IOOriginPartNotToKeep));
1110 r->u.itemOrder->u.esRequest->toKeep->supplDescription = NULL;
1111 r->u.itemOrder->u.esRequest->toKeep->contact = NULL;
1112 r->u.itemOrder->u.esRequest->toKeep->addlBilling = NULL;
1114 r->u.itemOrder->u.esRequest->notToKeep->resultSetItem =
1115 (Z_IOResultSetItem *) odr_malloc(out, sizeof(Z_IOResultSetItem));
1116 memset(r->u.itemOrder->u.esRequest->notToKeep->resultSetItem, 0, sizeof(Z_IOResultSetItem));
1117 r->u.itemOrder->u.esRequest->notToKeep->resultSetItem->resultSetId = "1";
1119 r->u.itemOrder->u.esRequest->notToKeep->resultSetItem->item =
1120 (int *) odr_malloc(out, sizeof(int));
1121 *r->u.itemOrder->u.esRequest->notToKeep->resultSetItem->item = itemno;
1123 r->u.itemOrder->u.esRequest->notToKeep->itemRequest = NULL;
1127 /* II : Added to do DALI Item Order Extended services request */
1128 static int send_itemorder(char *arg)
1131 Z_APDU *apdu = zget_APDU(out, Z_APDU_extendedServicesRequest );
1132 Z_ExtendedServicesRequest *req = apdu->u.extendedServicesRequest;
1133 oident ItemOrderRequest;
1138 /* Set up item order request */
1140 /* Function being performed by this extended services request */
1141 req->function = (int *) odr_malloc(out, sizeof(int));
1142 *req->function = Z_ExtendedServicesRequest_create;
1144 /* Package type, Using protocol ILL ( But that's not in the oid.h file yet */
1145 /* create an object of class Extended Service, value Item Order */
1146 ItemOrderRequest.proto = PROTO_Z3950;
1147 ItemOrderRequest.oclass = CLASS_EXTSERV;
1148 ItemOrderRequest.value = VAL_ITEMORDER;
1149 req->packageType = odr_oiddup(out,oid_getoidbyent(&ItemOrderRequest));
1150 req->packageName = "1.Extendedserveq";
1152 /* ** taskSpecificParameters ** */
1153 req->taskSpecificParameters = CreateItemOrderExternal(itemno);
1155 /* waitAction - Create the ILL request and that's it */
1156 *req->waitAction = Z_ExtendedServicesRequest_wait;
1162 /* II : Added to do DALI Item Order Extended services request */
1163 static int cmd_itemorder(char *arg)
1165 printf("Item order request\n");
1168 send_itemorder(arg);
1172 static int cmd_find(char *arg)
1176 printf("Find what?\n");
1181 printf("Not connected yet\n");
1184 if (!send_searchRequest(arg))
1189 static int cmd_delete(char *arg)
1193 printf("Delete what?\n");
1198 printf("Not connected yet\n");
1201 if (!send_deleteResultSetRequest(arg))
1206 static int cmd_ssub(char *arg)
1208 if (!(smallSetUpperBound = atoi(arg)))
1213 static int cmd_lslb(char *arg)
1215 if (!(largeSetLowerBound = atoi(arg)))
1220 static int cmd_mspn(char *arg)
1222 if (!(mediumSetPresentNumber = atoi(arg)))
1227 static int cmd_status(char *arg)
1229 printf("smallSetUpperBound: %d\n", smallSetUpperBound);
1230 printf("largeSetLowerBound: %d\n", largeSetLowerBound);
1231 printf("mediumSetPresentNumber: %d\n", mediumSetPresentNumber);
1235 static int cmd_setnames(char *arg)
1239 printf("Set numbering enabled.\n");
1244 printf("Set numbering disabled.\n");
1250 /* PRESENT SERVICE ----------------------------- */
1252 static int send_presentRequest(char *arg)
1254 Z_APDU *apdu = zget_APDU(out, Z_APDU_presentRequest);
1255 Z_PresentRequest *req = apdu->u.presentRequest;
1256 Z_RecordComposition compo;
1261 char setstring[100];
1263 req->referenceId = set_refid (out);
1264 if ((p = strchr(arg, '+')))
1271 if (p && (p=strchr(p+1, '+')))
1273 strcpy (setstring, p+1);
1274 req->resultSetId = setstring;
1276 else if (setnumber >= 0)
1278 sprintf(setstring, "%d", setnumber);
1279 req->resultSetId = setstring;
1284 static Z_Range range;
1285 static Z_Range *rangep = ⦥
1286 req->num_ranges = 1;
1288 req->resultSetStartPoint = &setno;
1289 req->numberOfRecordsRequested = &nos;
1290 prefsyn.proto = protocol;
1291 prefsyn.oclass = CLASS_RECSYN;
1292 prefsyn.value = recordsyntax;
1293 req->preferredRecordSyntax = oid_ent_to_oid(&prefsyn, oid);
1295 if (elementSetNames)
1297 req->recordComposition = &compo;
1298 compo.which = Z_RecordComp_simple;
1299 compo.u.simple = elementSetNames;
1302 printf("Sent presentRequest (%d+%d).\n", setno, nos);
1306 void process_close(Z_Close *req)
1308 Z_APDU *apdu = zget_APDU(out, Z_APDU_close);
1309 Z_Close *res = apdu->u.close;
1311 static char *reasons[] =
1316 "cost limit reached",
1318 "security violation",
1325 printf("Reason: %s, message: %s\n", reasons[*req->closeReason],
1326 req->diagnosticInformation ? req->diagnosticInformation : "NULL");
1333 nmem_destroy (session_mem);
1340 *res->closeReason = Z_Close_finished;
1342 printf("Sent response.\n");
1347 static int cmd_show(char *arg)
1351 printf("Not connected yet\n");
1354 if (!send_presentRequest(arg))
1359 int cmd_quit(char *arg)
1361 printf("See you later, alligator.\n");
1366 int cmd_cancel(char *arg)
1368 Z_APDU *apdu = zget_APDU(out, Z_APDU_triggerResourceControlRequest);
1369 Z_TriggerResourceControlRequest *req =
1370 apdu->u.triggerResourceControlRequest;
1375 printf("Session not initialized yet\n");
1378 if (!ODR_MASK_GET(session->options, Z_Options_triggerResourceCtrl))
1380 printf("Target doesn't support cancel (trigger resource ctrl)\n");
1383 *req->requestedAction = Z_TriggerResourceCtrl_cancel;
1384 req->resultSetWanted = &rfalse;
1387 printf("Sent cancel request\n");
1391 int send_scanrequest(char *string, int pp, int num)
1393 Z_APDU *apdu = zget_APDU(out, Z_APDU_scanRequest);
1394 Z_ScanRequest *req = apdu->u.scanRequest;
1396 if (!(req->termListAndStartPoint =
1397 p_query_scan(out, protocol, &req->attributeSet, string)))
1399 printf("Prefix query error\n");
1402 req->referenceId = set_refid (out);
1403 req->num_databaseNames = num_databaseNames;
1404 req->databaseNames = databaseNames;
1405 req->numberOfTermsRequested = #
1406 req->preferredPositionInResponse = &pp;
1411 int send_sortrequest(char *arg, int newset)
1413 Z_APDU *apdu = zget_APDU(out, Z_APDU_sortRequest);
1414 Z_SortRequest *req = apdu->u.sortRequest;
1415 Z_SortKeySpecList *sksl = (Z_SortKeySpecList *)odr_malloc (out, sizeof(*sksl));
1417 char sort_string[32], sort_flags[32];
1423 sprintf (setstring, "%d", setnumber);
1425 sprintf (setstring, "default");
1427 req->referenceId = set_refid (out);
1430 req->num_inputResultSetNames = 1;
1431 req->inputResultSetNames = (Z_InternationalString **)
1432 odr_malloc (out, sizeof(*req->inputResultSetNames));
1433 req->inputResultSetNames[0] = (char *)
1434 odr_malloc (out, strlen(setstring)+1);
1435 strcpy (req->inputResultSetNames[0], setstring);
1437 req->inputResultSetNames =
1438 (Z_StringList *)odr_malloc (out, sizeof(*req->inputResultSetNames));
1439 req->inputResultSetNames->num_strings = 1;
1440 req->inputResultSetNames->strings =
1441 (char **)odr_malloc (out, sizeof(*req->inputResultSetNames->strings));
1442 req->inputResultSetNames->strings[0] =
1443 (char *)odr_malloc (out, strlen(setstring)+1);
1444 strcpy (req->inputResultSetNames->strings[0], setstring);
1447 if (newset && setnumber >= 0)
1448 sprintf (setstring, "%d", ++setnumber);
1450 req->sortedResultSetName = (char *)odr_malloc (out, strlen(setstring)+1);
1451 strcpy (req->sortedResultSetName, setstring);
1453 req->sortSequence = sksl;
1454 sksl->num_specs = 0;
1455 sksl->specs = (Z_SortKeySpec **)odr_malloc (out, sizeof(sksl->specs) * 20);
1457 bib1.proto = protocol;
1458 bib1.oclass = CLASS_ATTSET;
1459 bib1.value = VAL_BIB1;
1460 while ((sscanf (arg, "%31s %31s%n", sort_string, sort_flags, &off)) == 2
1464 char *sort_string_sep;
1465 Z_SortKeySpec *sks = (Z_SortKeySpec *)odr_malloc (out, sizeof(*sks));
1466 Z_SortKey *sk = (Z_SortKey *)odr_malloc (out, sizeof(*sk));
1469 sksl->specs[sksl->num_specs++] = sks;
1470 sks->sortElement = (Z_SortElement *)odr_malloc (out, sizeof(*sks->sortElement));
1471 sks->sortElement->which = Z_SortElement_generic;
1472 sks->sortElement->u.generic = sk;
1474 if ((sort_string_sep = strchr (sort_string, '=')))
1476 Z_AttributeElement *el = (Z_AttributeElement *)odr_malloc (out, sizeof(*el));
1477 sk->which = Z_SortKey_sortAttributes;
1478 sk->u.sortAttributes =
1479 (Z_SortAttributes *)odr_malloc (out, sizeof(*sk->u.sortAttributes));
1480 sk->u.sortAttributes->id = oid_ent_to_oid(&bib1, oid);
1481 sk->u.sortAttributes->list =
1482 (Z_AttributeList *)odr_malloc (out, sizeof(*sk->u.sortAttributes->list));
1483 sk->u.sortAttributes->list->num_attributes = 1;
1484 sk->u.sortAttributes->list->attributes =
1485 (Z_AttributeElement **)odr_malloc (out,
1486 sizeof(*sk->u.sortAttributes->list->attributes));
1487 sk->u.sortAttributes->list->attributes[0] = el;
1488 el->attributeSet = 0;
1489 el->attributeType = (int *)odr_malloc (out, sizeof(*el->attributeType));
1490 *el->attributeType = atoi (sort_string);
1491 el->which = Z_AttributeValue_numeric;
1492 el->value.numeric = (int *)odr_malloc (out, sizeof(*el->value.numeric));
1493 *el->value.numeric = atoi (sort_string_sep + 1);
1497 sk->which = Z_SortKey_sortField;
1498 sk->u.sortField = (char *)odr_malloc (out, strlen(sort_string)+1);
1499 strcpy (sk->u.sortField, sort_string);
1501 sks->sortRelation = (int *)odr_malloc (out, sizeof(*sks->sortRelation));
1502 *sks->sortRelation = Z_SortRelation_ascending;
1503 sks->caseSensitivity = (int *)odr_malloc (out, sizeof(*sks->caseSensitivity));
1504 *sks->caseSensitivity = Z_SortCase_caseSensitive;
1507 sks->which = Z_SortKeySpec_null;
1508 sks->u.null = odr_nullval ();
1510 sks->missingValueAction = NULL;
1513 for (i = 0; sort_flags[i]; i++)
1515 switch (sort_flags[i])
1520 *sks->sortRelation = Z_SortRelation_descending;
1525 *sks->sortRelation = Z_SortRelation_ascending;
1529 *sks->caseSensitivity = Z_SortCase_caseInsensitive;
1533 *sks->caseSensitivity = Z_SortCase_caseSensitive;
1538 if (!sksl->num_specs)
1540 printf ("Missing sort specifications\n");
1547 void display_term(Z_TermInfo *t)
1549 if (t->term->which == Z_Term_general)
1551 printf("%.*s (%d)\n", t->term->u.general->len, t->term->u.general->buf,
1552 t->globalOccurrences ? *t->globalOccurrences : -1);
1553 sprintf(last_scan, "%.*s", t->term->u.general->len,
1554 t->term->u.general->buf);
1557 printf("Term type not general.\n");
1560 void process_scanResponse(Z_ScanResponse *res)
1563 Z_Entry **entries = NULL;
1564 int num_entries = 0;
1566 printf("Received ScanResponse\n");
1567 print_refid (res->referenceId);
1568 printf("%d entries", *res->numberOfEntriesReturned);
1569 if (res->positionOfTerm)
1570 printf (", position=%d", *res->positionOfTerm);
1572 if (*res->scanStatus != Z_Scan_success)
1573 printf("Scan returned code %d\n", *res->scanStatus);
1577 if ((entries = res->entries->entries))
1578 num_entries = res->entries->num_entries;
1580 if (res->entries->which == Z_ListEntries_entries)
1582 entries = res->entries->u.entries->entries;
1583 num_entries = res->entries->u.entries->num_entries;
1586 for (i = 0; i < num_entries; i++)
1587 if (entries[i]->which == Z_Entry_termInfo)
1589 printf("%c ", i + 1 == *res->positionOfTerm ? '*' : ' ');
1590 display_term(entries[i]->u.termInfo);
1593 display_diagrecs(&entries[i]->u.surrogateDiagnostic, 1);
1595 if (res->entries->nonsurrogateDiagnostics)
1596 display_diagrecs (res->entries->nonsurrogateDiagnostics,
1597 res->entries->num_nonsurrogateDiagnostics);
1599 if (res->entries->which == Z_ListEntries_nonSurrogateDiagnostics)
1600 display_diagrecs(&res->entries->
1601 u.nonSurrogateDiagnostics->diagRecs[0], 1);
1605 void process_sortResponse(Z_SortResponse *res)
1607 printf("Received SortResponse: status=");
1608 switch (*res->sortStatus)
1610 case Z_SortStatus_success:
1611 printf ("success"); break;
1612 case Z_SortStatus_partial_1:
1613 printf ("partial"); break;
1614 case Z_SortStatus_failure:
1615 printf ("failure"); break;
1617 printf ("unknown (%d)", *res->sortStatus);
1620 print_refid (res->referenceId);
1622 if (res->diagnostics)
1623 display_diagrecs(res->diagnostics,
1624 res->num_diagnostics);
1626 if (res->diagnostics)
1627 display_diagrecs(res->diagnostics->diagRecs,
1628 res->diagnostics->num_diagRecs);
1632 int cmd_sort_generic(char *arg, int newset)
1636 printf("Session not initialized yet\n");
1639 if (!ODR_MASK_GET(session->options, Z_Options_sort))
1641 printf("Target doesn't support sort\n");
1646 if (send_sortrequest(arg, newset) < 0)
1653 int cmd_sort(char *arg)
1655 return cmd_sort_generic (arg, 0);
1658 int cmd_sort_newset (char *arg)
1660 return cmd_sort_generic (arg, 1);
1663 int cmd_scan(char *arg)
1667 printf("Session not initialized yet\n");
1670 if (!ODR_MASK_GET(session->options, Z_Options_scan))
1672 printf("Target doesn't support scan\n");
1677 if (send_scanrequest(arg, 5, 20) < 0)
1681 if (send_scanrequest(last_scan, 1, 20) < 0)
1686 int cmd_format(char *arg)
1690 printf("Usage: format <recordsyntax>\n");
1693 recordsyntax = oid_getvalbyname (arg);
1694 if (recordsyntax == VAL_NONE)
1696 printf ("unknown record syntax\n");
1702 int cmd_elements(char *arg)
1704 static Z_ElementSetNames esn;
1705 static char what[100];
1709 printf("Usage: elements <esn>\n");
1713 esn.which = Z_ElementSetNames_generic;
1714 esn.u.generic = what;
1715 elementSetNames = &esn;
1719 int cmd_attributeset(char *arg)
1725 printf("Usage: attributeset <setname>\n");
1728 sscanf(arg, "%s", what);
1729 if (p_query_attset (what))
1731 printf("Unknown attribute set name\n");
1737 int cmd_querytype (char *arg)
1739 if (!strcmp (arg, "ccl"))
1740 queryType = QueryType_CCL;
1741 else if (!strcmp (arg, "prefix"))
1742 queryType = QueryType_Prefix;
1744 else if (!strcmp (arg, "ccl2rpn") || !strcmp (arg, "cclrpn"))
1745 queryType = QueryType_CCL2RPN;
1749 printf ("Querytype must be one of:\n");
1750 printf (" prefix - Prefix query\n");
1751 printf (" ccl - CCL query\n");
1753 printf (" ccl2rpn - CCL query converted to RPN\n");
1760 int cmd_refid (char *arg)
1766 refid = (char *) xmalloc (strlen(arg)+1);
1767 strcpy (refid, arg);
1772 int cmd_close(char *arg)
1779 apdu = zget_APDU(out, Z_APDU_close);
1780 req = apdu->u.close;
1781 *req->closeReason = Z_Close_finished;
1783 printf("Sent close request.\n");
1788 static void initialize(void)
1794 if (!(out = odr_createmem(ODR_ENCODE)) ||
1795 !(in = odr_createmem(ODR_DECODE)) ||
1796 !(print = odr_createmem(ODR_PRINT)))
1798 fprintf(stderr, "failed to allocate ODR streams\n");
1801 setvbuf(stdout, 0, _IONBF, 0);
1804 bibset = ccl_qual_mk ();
1805 inf = fopen ("default.bib", "r");
1808 ccl_qual_file (bibset, inf);
1812 cmd_base("Default");
1815 static int client(int wait)
1819 int (*fun)(char *arg);
1822 {"open", cmd_open, "('tcp'|'osi')':'[<tsel>'/']<host>[':'<port>]"},
1823 {"quit", cmd_quit, ""},
1824 {"find", cmd_find, "<query>"},
1825 {"delete", cmd_delete, "<setname>"},
1826 {"base", cmd_base, "<base-name>"},
1827 {"show", cmd_show, "<rec#>['+'<#recs>['+'<setname>]]"},
1828 {"scan", cmd_scan, "<term>"},
1829 {"sort", cmd_sort, "<sortkey> <flag> <sortkey> <flag> ..."},
1830 {"sort+", cmd_sort_newset, "<sortkey> <flag> <sortkey> <flag> ..."},
1831 {"authentication", cmd_authentication, "<acctstring>"},
1832 {"lslb", cmd_lslb, "<largeSetLowerBound>"},
1833 {"ssub", cmd_ssub, "<smallSetUpperBound>"},
1834 {"mspn", cmd_mspn, "<mediumSetPresentNumber>"},
1835 {"status", cmd_status, ""},
1836 {"setnames", cmd_setnames, ""},
1837 {"cancel", cmd_cancel, ""},
1838 {"format", cmd_format, "<recordsyntax>"},
1839 {"elements", cmd_elements, "<elementSetName>"},
1840 {"close", cmd_close, ""},
1841 {"attributeset", cmd_attributeset, "<attrset>"},
1842 {"querytype", cmd_querytype, "<type>"},
1843 {"refid", cmd_refid, "<id>"},
1844 {"itemorder", cmd_itemorder, "<item>"},
1848 int netbufferlen = 0;
1858 char line[1024], word[1024], arg[1024];
1864 FD_SET(cs_fileno(conn), &input);
1865 if ((res = select(20, &input, 0, 0, 0)) < 0)
1872 if (!wait && FD_ISSET(0, &input))
1877 /* quick & dirty way to get a command line. */
1879 if (!fgets(line, 1023, stdin))
1881 if ((end_p = strchr (line, '\n')))
1883 if ((res = sscanf(line, "%s %[^;]", word, arg)) <= 0)
1885 strcpy(word, last_cmd);
1890 strcpy(last_cmd, word);
1891 for (i = 0; cmd[i].cmd; i++)
1892 if (!strncmp(cmd[i].cmd, word, strlen(word)))
1894 res = (*cmd[i].fun)(arg);
1897 if (!cmd[i].cmd) /* dump our help-screen */
1899 printf("Unknown command: %s.\n", word);
1900 printf("Currently recognized commands:\n");
1901 for (i = 0; cmd[i].cmd; i++)
1902 printf(" %s %s\n", cmd[i].cmd, cmd[i].ad);
1913 if (conn && FD_ISSET(cs_fileno(conn), &input))
1918 if ((res = cs_get(conn, &netbuffer, &netbufferlen)) < 0)
1925 printf("Target closed connection.\n");
1928 odr_reset(in); /* release APDU from last round */
1929 odr_setbuf(in, netbuffer, res, 0);
1930 if (!z_APDU(in, &apdu, 0))
1932 odr_perror(in, "Decoding incoming APDU");
1933 fprintf(stderr, "[Near %d]\n", odr_offset(in));
1934 fprintf(stderr, "Packet dump:\n---------\n");
1935 odr_dumpBER(stderr, netbuffer, res);
1936 fprintf(stderr, "---------\n");
1940 if (!z_APDU(print, &apdu, 0))
1942 odr_perror(print, "Failed to print incoming APDU");
1949 case Z_APDU_initResponse:
1950 process_initResponse(apdu->u.initResponse);
1952 case Z_APDU_searchResponse:
1953 process_searchResponse(apdu->u.searchResponse);
1955 case Z_APDU_scanResponse:
1956 process_scanResponse(apdu->u.scanResponse);
1958 case Z_APDU_presentResponse:
1959 print_refid (apdu->u.presentResponse->referenceId);
1961 *apdu->u.presentResponse->numberOfRecordsReturned;
1962 if (apdu->u.presentResponse->records)
1963 display_records(apdu->u.presentResponse->records);
1965 printf("No records.\n");
1966 printf ("nextResultSetPosition = %d\n",
1967 *apdu->u.presentResponse->nextResultSetPosition);
1969 case Z_APDU_sortResponse:
1970 process_sortResponse(apdu->u.sortResponse);
1972 case Z_APDU_extendedServicesResponse:
1973 printf("Got extended services response\n");
1974 process_ESResponse(apdu->u.extendedServicesResponse);
1977 printf("Target has closed the association.\n");
1978 process_close(apdu->u.close);
1980 case Z_APDU_resourceControlRequest:
1981 process_resourceControlRequest
1982 (apdu->u.resourceControlRequest);
1984 case Z_APDU_deleteResultSetResponse:
1985 printf("Got deleteResultSetResponse status=%d\n",
1986 *apdu->u.deleteResultSetResponse->
1987 deleteOperationStatus);
1990 printf("Received unknown APDU type (%d).\n",
1995 while (conn && cs_more(conn));
2003 int main(int argc, char **argv)
2010 while ((ret = options("m:v:", argv, argc, &arg)) != -2)
2018 if (cmd_open (arg) == 2)
2023 if (!(marcdump = fopen (arg, "a")))
2030 log_init (log_mask_str(arg), "", NULL);
2033 fprintf (stderr, "Usage: %s [-m <marclog>] [<server-addr>]\n",
2043 return client (opened);