3 * Copyright (c) 1995-1996, Index Data.
4 * See the file LICENSE for details.
5 * Sebastian Hammer, Adam Dickmeiss
8 * Revision 1.48 1997-09-01 08:48:44 adam
9 * New windows NT/95 port using MSV5.0. Only a few changes made
10 * to avoid warnings. Sub project created: client.dsp.
12 * Revision 1.47 1997/07/01 13:49:56 adam
13 * Take care of case when invalid target is specified on command line.
15 * Revision 1.46 1997/06/23 10:30:18 adam
16 * Added call to ccl_rpn_delete in search. Added ODR stream "out"
17 * as parameter to ccl_rpn_query to release RPN query.
19 * Revision 1.45 1997/05/14 06:53:29 adam
22 * Revision 1.44 1997/05/05 11:20:35 adam
23 * Client uses "options" utility and marc dump filename may be specified
24 * as an option (-m <file>).
26 * Revision 1.43 1996/11/08 11:03:26 adam
27 * Client accepts multiple database names.
29 * Revision 1.42 1996/10/08 10:44:57 quinn
32 * Revision 1.41 1996/10/07 15:29:03 quinn
35 * Revision 1.40 1996/08/29 14:19:34 quinn
36 * Fixed conflict (CVS)
38 * Revision 1.39 1996/08/27 10:43:22 quinn
39 * Made select() optional
41 * Revision 1.38 1996/08/12 14:09:11 adam
42 * Default prefix query attribute set defined by using p_query_attset.
44 * Revision 1.37 1996/07/06 19:58:29 quinn
45 * System headerfiles gathered in yconfig
47 * Revision 1.36 1996/06/10 08:53:47 quinn
50 * Revision 1.35 1996/06/03 09:45:50 quinn
51 * Added display of OIDs in the GRS routine.
53 * Revision 1.34 1996/05/09 07:26:49 quinn
54 * *** empty log message ***
56 * Revision 1.33 1996/05/09 07:25:22 quinn
59 * Revision 1.32 1996/03/15 11:05:33 adam
60 * The user can set the preferred query type (prefix, ccl, ..) with the
63 * Revision 1.31 1996/02/20 12:51:54 quinn
64 * Fixed problems with EXTERNAL.
66 * Revision 1.30 1996/02/12 18:18:09 quinn
69 * Revision 1.29 1996/01/02 08:57:25 quinn
70 * Changed enums in the ASN.1 .h files to #defines. Changed oident.class to oclass
72 * Revision 1.28 1995/12/14 11:09:31 quinn
73 * Added Explain record syntax to the format command.
75 * Revision 1.27 1995/12/12 16:37:02 quinn
76 * Added destroy element to data1_node.
78 * Revision 1.26 1995/12/12 14:11:00 quinn
81 * Revision 1.25 1995/11/13 09:27:22 quinn
82 * Fiddling with the variant stuff.
84 * Revision 1.24 1995/10/30 12:41:13 quinn
85 * Added hostname lookup for server.
87 * Revision 1.23 1995/10/18 16:12:30 quinn
90 * Revision 1.22 1995/10/11 14:49:12 quinn
93 * Revision 1.21 1995/09/29 17:01:47 quinn
96 * Revision 1.20 1995/08/29 14:24:13 quinn
97 * Added second half of close-handshake
99 * Revision 1.19 1995/08/29 11:17:28 quinn
100 * Added code to receive close
102 * Revision 1.18 1995/08/28 12:21:27 quinn
103 * Client can now ask for simple element set names.
105 * Revision 1.17 1995/08/17 12:45:02 quinn
106 * Fixed minor problems with GRS-1. Added support in c&s.
108 * Revision 1.16 1995/08/15 12:00:04 quinn
111 * Revision 1.15 1995/06/22 09:28:03 quinn
112 * Fixed bug in SUTRS processing.
114 * Revision 1.14 1995/06/19 12:37:41 quinn
117 * Revision 1.13 1995/06/16 10:29:11 quinn
118 * *** empty log message ***
120 * Revision 1.12 1995/06/15 07:44:57 quinn
123 * Revision 1.11 1995/06/14 15:26:40 quinn
124 * *** empty log message ***
126 * Revision 1.10 1995/06/06 14:56:58 quinn
127 * Better diagnostics.
129 * Revision 1.9 1995/06/06 08:15:19 quinn
132 * Revision 1.8 1995/06/05 10:52:22 quinn
135 * Revision 1.7 1995/06/02 09:50:09 quinn
138 * Revision 1.6 1995/05/31 08:29:21 quinn
139 * Nothing significant.
141 * Revision 1.5 1995/05/29 08:10:47 quinn
142 * Moved oid.c to util.
144 * Revision 1.4 1995/05/22 15:30:13 adam
145 * Client uses prefix query notation.
147 * Revision 1.3 1995/05/22 15:06:53 quinn
148 * *** empty log message ***
150 * Revision 1.2 1995/05/22 14:56:40 quinn
151 * *** empty log message ***
153 * Revision 1.1 1995/05/22 11:30:31 quinn
154 * Added prettier client.
160 * This is the obligatory little toy client, whose primary purpose is
161 * to illustrate the use of the YAZ service-level API.
170 #include <comstack.h>
177 #include <marcdisp.h>
178 #include <diagbib1.h>
187 #define C_PROMPT "Z> "
189 static ODR out, in, print; /* encoding and decoding streams */
190 static COMSTACK conn = 0; /* our z-association */
191 static Z_IdAuthentication *auth = 0; /* our current auth definition */
192 static char *databaseNames[128];
193 static int num_databaseNames = 0;
194 static int setnumber = 0; /* current result set number */
195 static int smallSetUpperBound = 0;
196 static int largeSetLowerBound = 1;
197 static int mediumSetPresentNumber = 0;
198 static Z_ElementSetNames *elementSetNames = 0;
199 static int setno = 1; /* current set offset */
200 static int protocol = PROTO_Z3950; /* current app protocol */
201 static int recordsyntax = VAL_USMARC;
202 static int sent_close = 0;
203 static ODR_MEM session_mem; /* memory handle for init-response */
204 static Z_InitResponse *session = 0; /* session parameters */
205 static char last_scan[512] = "0";
206 static char last_cmd[100] = "?";
207 static FILE *marcdump = 0;
215 static QueryType queryType = QueryType_Prefix;
218 static CCL_bibset bibset; /* CCL bibset handle */
221 static void send_apdu(Z_APDU *a)
226 if (!z_APDU(out, &a, 0))
228 odr_perror(out, "Encoding APDU");
231 buf = odr_getbuf(out, &len, 0);
232 odr_reset(out); /* release the APDU structure */
233 if (cs_put(conn, buf, len) < 0)
235 fprintf(stderr, "cs_put: %s", cs_errmsg(cs_errno(conn)));
240 /* INIT SERVICE ------------------------------- */
242 static void send_initRequest()
244 Z_APDU *apdu = zget_APDU(out, Z_APDU_initRequest);
245 Z_InitRequest *req = apdu->u.initRequest;
247 ODR_MASK_SET(req->options, Z_Options_search);
248 ODR_MASK_SET(req->options, Z_Options_present);
249 ODR_MASK_SET(req->options, Z_Options_namedResultSets);
250 ODR_MASK_SET(req->options, Z_Options_triggerResourceCtrl);
251 ODR_MASK_SET(req->options, Z_Options_scan);
253 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_1);
254 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_2);
255 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_3);
257 *req->maximumRecordSize = 1024*1024;
259 req->idAuthentication = auth;
262 printf("Sent initrequest.\n");
265 static int process_initResponse(Z_InitResponse *res)
267 /* save session parameters for later use */
268 session_mem = odr_extract_mem(in);
272 printf("Connection rejected by target.\n");
274 printf("Connection accepted by target.\n");
275 if (res->implementationId)
276 printf("ID : %s\n", res->implementationId);
277 if (res->implementationName)
278 printf("Name : %s\n", res->implementationName);
279 if (res->implementationVersion)
280 printf("Version: %s\n", res->implementationVersion);
281 if (res->userInformationField)
283 printf("UserInformationfield:\n");
284 if (!z_External(print, (Z_External**)&res-> userInformationField,
287 odr_perror(print, "Printing userinfo\n");
290 if (res->userInformationField->which == Z_External_octet)
292 printf("Guessing visiblestring:\n");
293 printf("'%s'\n", res->userInformationField->u. octet_aligned->buf);
299 int cmd_open(char *arg)
302 char type[100], addr[100];
307 printf("Already connected.\n");
310 if (!*arg || sscanf(arg, "%[^:]:%s", type, addr) < 2)
312 fprintf(stderr, "Usage: open (osi|tcp) ':' [tsel '/']host[':'port]\n");
316 if (!strcmp(type, "osi"))
318 if (!(add = mosi_strtoaddr(addr)))
328 if (!strcmp(type, "tcp"))
330 if (!(add = tcpip_strtoaddr(addr)))
336 protocol = PROTO_Z3950;
340 fprintf(stderr, "Bad type: %s\n", type);
343 if (!(conn = cs_create(t, 1, protocol)))
348 printf("Connecting...");
350 if (cs_connect(conn, add) < 0)
362 int cmd_authentication(char *arg)
364 static Z_IdAuthentication au;
365 static char open[256];
369 printf("Auth field set to null\n");
374 au.which = Z_IdAuthentication_open;
380 /* SEARCH SERVICE ------------------------------ */
382 static void display_variant(Z_Variant *v, int level)
386 for (i = 0; i < v->num_triples; i++)
388 printf("%*sclass=%d,type=%d", level * 4, "", *v->triples[i]->zclass,
389 *v->triples[i]->type);
390 if (v->triples[i]->which == Z_Triple_internationalString)
391 printf(",value=%s\n", v->triples[i]->value.internationalString);
397 static void display_grs1(Z_GenericRecord *r, int level)
403 for (i = 0; i < r->num_elements; i++)
407 printf("%*s", level * 4, "");
411 printf("%d,", *t->tagType);
414 if (t->tagValue->which == Z_StringOrNumeric_numeric)
415 printf("%d) ", *t->tagValue->u.numeric);
417 printf("%s) ", t->tagValue->u.string);
418 if (t->content->which == Z_ElementData_subtree)
421 display_grs1(t->content->u.subtree, level+1);
423 else if (t->content->which == Z_ElementData_string)
424 printf("%s\n", t->content->u.string);
425 else if (t->content->which == Z_ElementData_numeric)
426 printf("%d\n", *t->content->u.numeric);
427 else if (t->content->which == Z_ElementData_oid)
429 int *ip = t->content->u.oid;
432 if ((oent = oid_getentbyoid(t->content->u.oid)))
433 printf("OID: %s\n", oent->desc);
437 while (ip && *ip >= 0)
438 printf(" %d", *(ip++));
442 else if (t->content->which == Z_ElementData_noDataRequested)
443 printf("[No data requested]\n");
444 else if (t->content->which == Z_ElementData_elementEmpty)
445 printf("[Element empty]\n");
446 else if (t->content->which == Z_ElementData_elementNotThere)
447 printf("[Element not there]\n");
450 if (t->appliedVariant)
451 display_variant(t->appliedVariant, level+1);
452 if (t->metaData && t->metaData->supportedVariants)
456 printf("%*s---- variant list\n", (level+1)*4, "");
457 for (c = 0; c < t->metaData->num_supportedVariants; c++)
459 printf("%*svariant #%d\n", (level+1)*4, "", c);
460 display_variant(t->metaData->supportedVariants[c], level + 2);
466 static void display_record(Z_DatabaseRecord *p)
468 Z_External *r = (Z_External*) p;
469 oident *ent = oid_getentbyoid(r->direct_reference);
472 * Tell the user what we got.
474 if (r->direct_reference)
476 printf("Record type: ");
478 printf("%s\n", ent->desc);
479 else if (!odr_oid(print, &r->direct_reference, 0))
481 odr_perror(print, "print oid");
486 /* Check if this is a known, ASN.1 type tucked away in an octet string */
487 if (ent && r->which == Z_External_octet)
489 Z_ext_typeent *type = z_ext_getentbyref(ent->value);
495 * Call the given decoder to process the record.
497 odr_setbuf(in, (char*)p->u.octet_aligned->buf,
498 p->u.octet_aligned->len, 0);
499 if (!(*type->fun)(in, &rr, 0))
501 odr_perror(in, "Decoding constructed record.");
502 fprintf(stderr, "[Near %d]\n", odr_offset(in));
503 fprintf(stderr, "Packet dump:\n---------\n");
504 odr_dumpBER(stderr, (char*)p->u.octet_aligned->buf,
505 p->u.octet_aligned->len);
506 fprintf(stderr, "---------\n");
510 * Note: we throw away the original, BER-encoded record here.
511 * Do something else with it if you want to keep it.
513 r->u.sutrs = rr; /* we don't actually check the type here. */
514 r->which = type->what;
517 if (ent->value == VAL_SOIF)
518 printf("%.*s", r->u.octet_aligned->len, r->u.octet_aligned->buf);
519 else if (r->which == Z_External_octet && p->u.octet_aligned->len)
521 const char *marc_buf = (char*)p->u.octet_aligned->buf;
522 marc_display (marc_buf, NULL);
524 fwrite (marc_buf, strlen (marc_buf), 1, marcdump);
526 else if (ent->value == VAL_SUTRS)
528 if (r->which != Z_External_sutrs)
530 printf("Expecting single SUTRS type for SUTRS.\n");
533 printf("%.*s", r->u.sutrs->len, r->u.sutrs->buf);
535 else if (ent->value == VAL_GRS1)
537 if (r->which != Z_External_grs1)
539 printf("Expecting single GRS type for GRS.\n");
542 display_grs1(r->u.grs1, 0);
546 printf("Unknown record representation.\n");
547 if (!z_External(print, &r, 0))
549 odr_perror(print, "Printing external");
555 static void display_diagrec(Z_DiagRec *p)
559 Z_DefaultDiagFormat *r;
564 printf("Diagnostic message from database:\n");
566 if (p->which != Z_DiagRec_defaultFormat)
568 printf("Diagnostic record not in default format.\n");
572 r = p->u.defaultFormat;
574 if (!(ent = oid_getentbyoid(r->diagnosticSetId)) ||
575 ent->oclass != CLASS_DIAGSET || ent->value != VAL_BIB1)
576 printf("Missing or unknown diagset\n");
577 printf(" [%d] %s", *r->condition, diagbib1_str(*r->condition));
578 if (r->addinfo && *r->addinfo)
579 printf(" -- '%s'\n", r->addinfo);
584 static void display_nameplusrecord(Z_NamePlusRecord *p)
587 printf("[%s]", p->databaseName);
588 if (p->which == Z_NamePlusRecord_surrogateDiagnostic)
589 display_diagrec(p->u.surrogateDiagnostic);
591 display_record(p->u.databaseRecord);
594 static void display_records(Z_Records *p)
598 if (p->which == Z_Records_NSD)
599 display_diagrec(p->u.nonSurrogateDiagnostic);
602 printf("Records: %d\n", p->u.databaseOrSurDiagnostics->num_records);
603 for (i = 0; i < p->u.databaseOrSurDiagnostics->num_records; i++)
604 display_nameplusrecord(p->u.databaseOrSurDiagnostics->records[i]);
608 static int send_searchRequest(char *arg)
610 Z_APDU *apdu = zget_APDU(out, Z_APDU_searchRequest);
611 Z_SearchRequest *req = apdu->u.searchRequest;
614 struct ccl_rpn_node *rpn;
619 Z_RPNQuery *RPNquery;
623 if (queryType == QueryType_CCL2RPN)
625 rpn = ccl_find_str(bibset, arg, &error, &pos);
628 printf("CCL ERROR: %s\n", ccl_err_msg(error));
634 if (!strcmp(arg, "@big")) /* strictly for troublemaking */
636 static unsigned char big[2100];
639 /* send a very big referenceid to test transport stack etc. */
640 memset(big, 'A', 2100);
641 bigo.len = bigo.size = 2100;
643 req->referenceId = &bigo;
648 sprintf(setstring, "%d", ++setnumber);
649 req->resultSetName = setstring;
651 *req->smallSetUpperBound = smallSetUpperBound;
652 *req->largeSetLowerBound = largeSetLowerBound;
653 *req->mediumSetPresentNumber = mediumSetPresentNumber;
654 if (smallSetUpperBound > 0 || (largeSetLowerBound > 1 &&
655 mediumSetPresentNumber > 0))
659 prefsyn.proto = protocol;
660 prefsyn.oclass = CLASS_RECSYN;
661 prefsyn.value = recordsyntax;
662 req->preferredRecordSyntax =
663 odr_oiddup(out, oid_getoidbyent(&prefsyn));
664 req->smallSetElementSetNames =
665 req->mediumSetElementSetNames = elementSetNames;
667 req->num_databaseNames = num_databaseNames;
668 req->databaseNames = databaseNames;
674 case QueryType_Prefix:
675 query.which = Z_Query_type_1;
676 RPNquery = p_query_rpn (out, protocol, arg);
679 printf("Prefix query error\n");
682 query.u.type_1 = RPNquery;
685 query.which = Z_Query_type_2;
686 query.u.type_2 = &ccl_query;
687 ccl_query.buf = (unsigned char*) arg;
688 ccl_query.len = strlen(arg);
691 case QueryType_CCL2RPN:
692 query.which = Z_Query_type_1;
693 assert((RPNquery = ccl_rpn_query(out, rpn)));
694 bib1.proto = protocol;
695 bib1.oclass = CLASS_ATTSET;
696 bib1.value = VAL_BIB1;
697 RPNquery->attributeSetId = oid_getoidbyent(&bib1);
698 query.u.type_1 = RPNquery;
699 ccl_rpn_delete (rpn);
703 printf ("Unsupported query type\n");
708 printf("Sent searchRequest.\n");
712 static int process_searchResponse(Z_SearchResponse *res)
714 if (*res->searchStatus)
715 printf("Search was a success.\n");
717 printf("Search was a bloomin' failure.\n");
718 printf("Number of hits: %d, setno %d\n",
719 *res->resultCount, setnumber);
720 printf("records returned: %d\n",
721 *res->numberOfRecordsReturned);
722 setno += *res->numberOfRecordsReturned;
724 display_records(res->records);
728 static int cmd_find(char *arg)
732 printf("Find what?\n");
737 printf("Not connected yet\n");
740 if (!send_searchRequest(arg))
745 static int cmd_ssub(char *arg)
747 if (!(smallSetUpperBound = atoi(arg)))
752 static int cmd_lslb(char *arg)
754 if (!(largeSetLowerBound = atoi(arg)))
759 static int cmd_mspn(char *arg)
761 if (!(mediumSetPresentNumber = atoi(arg)))
766 static int cmd_status(char *arg)
768 printf("smallSetUpperBound: %d\n", smallSetUpperBound);
769 printf("largeSetLowerBound: %d\n", largeSetLowerBound);
770 printf("mediumSetPresentNumber: %d\n", mediumSetPresentNumber);
774 static int cmd_base(char *arg)
781 printf("Usage: base <database> <database> ...\n");
784 for (i = 0; i<num_databaseNames; i++)
785 xfree (databaseNames[i]);
786 num_databaseNames = 0;
789 if (!(cp = strchr(arg, ' ')))
790 cp = arg + strlen(arg);
793 databaseNames[num_databaseNames] = xmalloc (1 + cp - arg);
794 memcpy (databaseNames[num_databaseNames], arg, cp - arg);
795 databaseNames[num_databaseNames++][cp - arg] = '\0';
803 static int cmd_setnames(char *arg)
807 printf("Set numbering enabled.\n");
812 printf("Set numbering disabled.\n");
818 /* PRESENT SERVICE ----------------------------- */
820 static int send_presentRequest(char *arg)
822 Z_APDU *apdu = zget_APDU(out, Z_APDU_presentRequest);
823 Z_PresentRequest *req = apdu->u.presentRequest;
824 Z_RecordComposition compo;
830 if ((p = strchr(arg, '+')))
840 sprintf(setstring, "%d", setnumber);
841 req->resultSetId = setstring;
848 static Z_Range range;
849 static Z_Range *rangep = ⦥
852 req->resultSetStartPoint = &setno;
853 req->numberOfRecordsRequested = &nos;
854 prefsyn.proto = protocol;
855 prefsyn.oclass = CLASS_RECSYN;
856 prefsyn.value = recordsyntax;
857 req->preferredRecordSyntax = oid_getoidbyent(&prefsyn);
860 req->recordComposition = &compo;
861 compo.which = Z_RecordComp_simple;
862 compo.u.simple = elementSetNames;
865 printf("Sent presentRequest (%d+%d).\n", setno, nos);
869 void process_close(Z_Close *req)
871 Z_APDU *apdu = zget_APDU(out, Z_APDU_close);
872 Z_Close *res = apdu->u.close;
874 static char *reasons[] =
879 "cost limit reached",
881 "security violation",
888 printf("Reason: %s, message: %s\n", reasons[*req->closeReason],
889 req->diagnosticInformation ? req->diagnosticInformation : "NULL");
892 printf("Goodbye.\n");
895 *res->closeReason = Z_Close_finished;
897 printf("Sent response.\n");
901 static int cmd_show(char *arg)
903 if (!send_presentRequest(arg))
908 int cmd_quit(char *arg)
910 printf("See you later, alligator.\n");
915 int cmd_cancel(char *arg)
917 Z_APDU *apdu = zget_APDU(out, Z_APDU_triggerResourceControlRequest);
918 Z_TriggerResourceControlRequest *req =
919 apdu->u.triggerResourceControlRequest;
924 printf("Session not initialized yet\n");
927 if (!ODR_MASK_GET(session->options, Z_Options_triggerResourceCtrl))
929 printf("Target doesn't support cancel (trigger resource ctrl)\n");
932 *req->requestedAction = Z_TriggerResourceCtrl_cancel;
933 req->resultSetWanted = &rfalse;
936 printf("Sent cancel request\n");
940 int send_scanrequest(char *string, int pp, int num)
942 Z_APDU *apdu = zget_APDU(out, Z_APDU_scanRequest);
943 Z_ScanRequest *req = apdu->u.scanRequest;
945 req->num_databaseNames = num_databaseNames;
946 req->databaseNames = databaseNames;
947 req->termListAndStartPoint = p_query_scan(out, protocol,
948 &req->attributeSet, string);
949 req->numberOfTermsRequested = #
950 req->preferredPositionInResponse = &pp;
955 void display_term(Z_TermInfo *t)
957 if (t->term->which == Z_Term_general)
959 printf("%.*s (%d)\n", t->term->u.general->len, t->term->u.general->buf,
960 t->globalOccurrences ? *t->globalOccurrences : -1);
961 sprintf(last_scan, "%.*s", t->term->u.general->len,
962 t->term->u.general->buf);
965 printf("Term type not general.\n");
968 void process_scanResponse(Z_ScanResponse *res)
972 printf("SCAN: %d entries, position=%d\n", *res->numberOfEntriesReturned,
973 *res->positionOfTerm);
974 if (*res->scanStatus != Z_Scan_success)
975 printf("Scan returned code %d\n", *res->scanStatus);
978 if (res->entries->which == Z_ListEntries_entries)
980 Z_Entries *ent = res->entries->u.entries;
982 for (i = 0; i < ent->num_entries; i++)
983 if (ent->entries[i]->which == Z_Entry_termInfo)
985 printf("%c ", i + 1 == *res->positionOfTerm ? '*' : ' ');
986 display_term(ent->entries[i]->u.termInfo);
989 display_diagrec(ent->entries[i]->u.surrogateDiagnostic);
992 display_diagrec(res->entries->u.nonSurrogateDiagnostics->diagRecs[0]);
995 int cmd_scan(char *arg)
999 printf("Session not initialized yet\n");
1002 if (!ODR_MASK_GET(session->options, Z_Options_scan))
1004 printf("Target doesn't support scan\n");
1009 if (send_scanrequest(arg, 5, 20) < 0)
1013 if (send_scanrequest(last_scan, 1, 20) < 0)
1018 int cmd_format(char *arg)
1022 printf("Usage: format <recordsyntax>\n");
1025 if (!strcmp(arg, "sutrs"))
1027 printf("Preferred format is SUTRS.\n");
1028 recordsyntax = VAL_SUTRS;
1031 else if (!strcmp(arg, "usmarc"))
1033 printf("Preferred format is USMARC\n");
1034 recordsyntax = VAL_USMARC;
1037 else if (!strcmp(arg, "danmarc"))
1039 printf("Preferred format is DANMARC\n");
1040 recordsyntax = VAL_DANMARC;
1043 else if (!strcmp(arg, "ukmarc"))
1045 printf("Preferred format is UKMARC\n");
1046 recordsyntax = VAL_UKMARC;
1049 else if (!strcmp(arg, "grs1"))
1051 printf("Preferred format is GRS1\n");
1052 recordsyntax = VAL_GRS1;
1055 else if (!strcmp(arg, "soif"))
1057 printf("Preferred format is SOIF\n");
1058 recordsyntax = VAL_SOIF;
1061 else if (!strcmp(arg, "summary"))
1063 printf("Preferred format is Summary\n");
1064 recordsyntax = VAL_SUMMARY;
1067 else if (!strcmp(arg, "explain"))
1069 printf("Preferred format is Explain\n");
1070 recordsyntax = VAL_EXPLAIN;
1075 printf("Specify one of {sutrs,usmarc,danmarc,ukmarc,grs1,summary,explain}.\n");
1080 int cmd_elements(char *arg)
1082 static Z_ElementSetNames esn;
1083 static char what[100];
1087 printf("Usage: elements <esn>\n");
1091 esn.which = Z_ElementSetNames_generic;
1092 esn.u.generic = what;
1093 elementSetNames = &esn;
1097 int cmd_attributeset(char *arg)
1103 printf("Usage: attributeset <setname>\n");
1106 sscanf(arg, "%s", what);
1107 if (p_query_attset (what))
1109 printf("Unknown attribute set name\n");
1115 int cmd_querytype (char *arg)
1117 if (!strcmp (arg, "ccl"))
1118 queryType = QueryType_CCL;
1119 else if (!strcmp (arg, "prefix"))
1120 queryType = QueryType_Prefix;
1122 else if (!strcmp (arg, "ccl2rpn") || !strcmp (arg, "cclrpn"))
1123 queryType = QueryType_CCL2RPN;
1127 printf ("Querytype must be one of:\n");
1128 printf (" prefix - Prefix query\n");
1129 printf (" ccl - CCL query\n");
1131 printf (" ccl2rpn - CCL query converted to RPN\n");
1138 int cmd_close(char *arg)
1140 Z_APDU *apdu = zget_APDU(out, Z_APDU_close);
1141 Z_Close *req = apdu->u.close;
1143 *req->closeReason = Z_Close_finished;
1145 printf("Sent close request.\n");
1150 static void initialize(void)
1156 if (!(out = odr_createmem(ODR_ENCODE)) ||
1157 !(in = odr_createmem(ODR_DECODE)) ||
1158 !(print = odr_createmem(ODR_PRINT)))
1160 fprintf(stderr, "failed to allocate ODR streams\n");
1163 setvbuf(stdout, 0, _IONBF, 0);
1166 bibset = ccl_qual_mk ();
1167 inf = fopen ("default.bib", "r");
1170 ccl_qual_file (bibset, inf);
1176 static int client(int wait)
1180 int (*fun)(char *arg);
1183 {"open", cmd_open, "('tcp'|'osi')':'[<tsel>'/']<host>[':'<port>]"},
1184 {"quit", cmd_quit, ""},
1185 {"find", cmd_find, "<query>"},
1186 {"base", cmd_base, "<base-name>"},
1187 {"show", cmd_show, "<rec#>['+'<#recs>]"},
1188 {"scan", cmd_scan, "<term>"},
1189 {"authentication", cmd_authentication, "<acctstring>"},
1190 {"lslb", cmd_lslb, "<largeSetLowerBound>"},
1191 {"ssub", cmd_ssub, "<smallSetUpperBound>"},
1192 {"mspn", cmd_mspn, "<mediumSetPresentNumber>"},
1193 {"status", cmd_status, ""},
1194 {"setnames", cmd_setnames, ""},
1195 {"cancel", cmd_cancel, ""},
1196 {"format", cmd_format, "<recordsyntax>"},
1197 {"elements", cmd_elements, "<elementSetName>"},
1198 {"close", cmd_close, ""},
1199 {"attributeset", cmd_attributeset, "<attrset>"},
1200 {"querytype", cmd_querytype, "<type>"},
1204 int netbufferlen = 0;
1214 char line[1024], word[1024], arg[1024];
1220 FD_SET(cs_fileno(conn), &input);
1221 if ((res = select(20, &input, 0, 0, 0)) < 0)
1228 if (!wait && FD_ISSET(0, &input))
1233 /* quick & dirty way to get a command line. */
1236 if ((res = sscanf(line, "%s %[^;]", word, arg)) <= 0)
1238 strcpy(word, last_cmd);
1243 strcpy(last_cmd, word);
1244 for (i = 0; cmd[i].cmd; i++)
1245 if (!strncmp(cmd[i].cmd, word, strlen(word)))
1247 res = (*cmd[i].fun)(arg);
1250 if (!cmd[i].cmd) /* dump our help-screen */
1252 printf("Unknown command: %s.\n", word);
1253 printf("Currently recognized commands:\n");
1254 for (i = 0; cmd[i].cmd; i++)
1255 printf(" %s %s\n", cmd[i].cmd, cmd[i].ad);
1265 if (conn && FD_ISSET(cs_fileno(conn), &input))
1270 if ((res = cs_get(conn, &netbuffer, &netbufferlen)) < 0)
1277 printf("Target closed connection.\n");
1280 odr_reset(in); /* release APDU from last round */
1281 odr_setbuf(in, netbuffer, res, 0);
1282 if (!z_APDU(in, &apdu, 0))
1284 odr_perror(in, "Decoding incoming APDU");
1285 fprintf(stderr, "[Near %d]\n", odr_offset(in));
1286 fprintf(stderr, "Packet dump:\n---------\n");
1287 odr_dumpBER(stderr, netbuffer, res);
1288 fprintf(stderr, "---------\n");
1292 if (!z_APDU(print, &apdu, 0))
1294 odr_perror(print, "Failed to print incoming APDU");
1301 case Z_APDU_initResponse:
1302 process_initResponse(apdu->u.initResponse);
1304 case Z_APDU_searchResponse:
1305 process_searchResponse(apdu->u.searchResponse);
1307 case Z_APDU_scanResponse:
1308 process_scanResponse(apdu->u.scanResponse);
1310 case Z_APDU_presentResponse:
1311 printf("Received presentResponse.\n");
1313 *apdu->u.presentResponse->numberOfRecordsReturned;
1314 if (apdu->u.presentResponse->records)
1315 display_records(apdu->u.presentResponse->records);
1317 printf("No records.\n");
1320 printf("Target has closed the association.\n");
1321 process_close(apdu->u.close);
1324 printf("Received unknown APDU type (%d).\n",
1331 while (cs_more(conn));
1338 int main(int argc, char **argv)
1346 cmd_base("Default");
1348 while ((ret = options("m:", argv, argc, &arg)) != -2)
1353 if (cmd_open (arg) == 2)
1357 if (!(marcdump = fopen (arg, "a")))
1364 fprintf (stderr, "Usage: %s [-m <marclog>] [<server-addr>]\n",
1371 return client (opened);