2 * Copyright (C) 1995-2005, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: client.c,v 1.269 2005-01-16 22:12:39 adam Exp $
26 #if HAVE_OPENSSL_SSL_H
27 #include <openssl/bio.h>
28 #include <openssl/crypto.h>
29 #include <openssl/x509.h>
30 #include <openssl/pem.h>
31 #include <openssl/ssl.h>
32 #include <openssl/err.h>
38 #define S_ISREG(x) (x & _S_IFREG)
39 #define S_ISDIR(x) (x & _S_IFDIR)
42 #include <yaz/yaz-util.h>
44 #include <yaz/comstack.h>
46 #include <yaz/proto.h>
47 #include <yaz/marcdisp.h>
48 #include <yaz/diagbib1.h>
49 #include <yaz/otherinfo.h>
50 #include <yaz/charneg.h>
52 #include <yaz/pquery.h>
53 #include <yaz/sortspec.h>
57 #include <yaz/yaz-ccl.h>
61 #if HAVE_READLINE_READLINE_H
62 #include <readline/readline.h>
64 #if HAVE_READLINE_HISTORY_H
65 #include <readline/history.h>
70 #include "tabcomplete.h"
72 #define C_PROMPT "Z> "
74 static char *codeset = 0; /* character set for output */
75 static int hex_dump = 0;
76 static char *dump_file_prefix = 0;
77 static ODR out, in, print; /* encoding and decoding streams */
79 static ODR srw_sr_odr_out = 0;
80 static Z_SRW_PDU *srw_sr = 0;
82 static FILE *apdu_file = 0;
83 static FILE *ber_file = 0;
84 static COMSTACK conn = 0; /* our z-association */
85 static Z_IdAuthentication *auth = 0; /* our current auth definition */
86 char *databaseNames[128];
87 int num_databaseNames = 0;
88 static Z_External *record_last = 0;
89 static int setnumber = -1; /* current result set number */
90 static int smallSetUpperBound = 0;
91 static int largeSetLowerBound = 1;
92 static int mediumSetPresentNumber = 0;
93 static Z_ElementSetNames *elementSetNames = 0;
94 static int setno = 1; /* current set offset */
95 static enum oid_proto protocol = PROTO_Z3950; /* current app protocol */
96 static enum oid_value recordsyntax = VAL_USMARC;
97 static char *record_schema = 0;
98 static int sent_close = 0;
99 static NMEM session_mem = NULL; /* memory handle for init-response */
100 static Z_InitResponse *session = 0; /* session parameters */
101 static char last_scan_line[512] = "0";
102 static char last_scan_query[512] = "0";
103 static char ccl_fields[512] = "default.bib";
104 /* ### How can I set this path to use wherever YAZ is installed? */
105 static char cql_fields[512] = "/usr/local/share/yaz/etc/pqf.properties";
106 static char *esPackageName = 0;
107 static char *yazProxy = 0;
108 static int kilobytes = 1024;
109 static char *negotiationCharset = 0;
110 static int negotiationCharsetRecords = 1;
111 static int negotiationCharsetVersion = 3;
112 static char *outputCharset = 0;
113 static char *marcCharset = 0;
114 static char* yazLang = 0;
115 static char* http_version = "1.1";
117 static char last_cmd[32] = "?";
118 static FILE *marc_file = 0;
119 static char *refid = NULL;
120 static char *last_open_command = NULL;
121 static int auto_reconnect = 0;
122 static Odr_bitmask z3950_options;
123 static int z3950_version = 3;
124 static int scan_stepSize = 0;
125 static int scan_position = 1;
126 static char cur_host[200];
136 static QueryType queryType = QueryType_Prefix;
138 static CCL_bibset bibset; /* CCL bibset handle */
139 static cql_transform_t cqltrans; /* CQL context-set handle */
141 #if HAVE_READLINE_COMPLETION_OVER
144 /* readline doesn't have this var. Define it ourselves. */
145 int rl_attempted_completion_over = 0;
148 /* set this one to 1, to avoid decode of unknown MARCs */
149 #define AVOID_MARC_DECODE 1
151 #define maxOtherInfosSupported 10
155 } extraOtherInfos[maxOtherInfosSupported];
158 void process_cmd_line(char* line);
159 char ** readline_completer(char *text, int start, int end);
160 char *command_generator(const char *text, int state);
161 char** curret_global_list=NULL;
162 int cmd_register_tab(const char* arg);
164 static void close_session (void);
166 ODR getODROutputStream()
171 const char* query_type_as_string(QueryType q)
174 case QueryType_Prefix: return "prefix (RPN sent to server)";
175 case QueryType_CCL: return "CCL (CCL sent to server) ";
176 case QueryType_CCL2RPN: return "CCL -> RPN (RPN sent to server)";
177 case QueryType_CQL: return "CQL (CQL sent to server)";
178 case QueryType_CQL2RPN: return "CQL -> RPN (RPN sent to server)";
180 return "unknown Query type internal yaz-client error";
184 static void do_hex_dump(const char* buf, int len)
189 for( i=0; i<len ; i=i+16 )
192 for(x=0 ; i+x<len && x<16; ++x)
194 printf("%2.2X ",(unsigned int)((unsigned char)buf[i+x]));
199 if (dump_file_prefix)
202 if (++no < 1000 && strlen(dump_file_prefix) < 500)
206 sprintf (fname, "%s.%03d.raw", dump_file_prefix, no);
207 of = fopen(fname, "wb");
209 fwrite (buf, 1, len, of);
216 void add_otherInfos(Z_APDU *a)
218 Z_OtherInformation **oi;
222 for(i=0; i<maxOtherInfosSupported; ++i)
224 if(extraOtherInfos[i].oidval != -1)
225 yaz_oi_set_string_oidval(oi, out, extraOtherInfos[i].oidval,
226 1, extraOtherInfos[i].value);
230 int send_apdu(Z_APDU *a)
239 z_APDU(print, &a, 0, 0);
242 if (!z_APDU(out, &a, 0, 0))
244 odr_perror(out, "Encoding APDU");
248 buf = odr_getbuf(out, &len, 0);
250 odr_dumpBER(ber_file, buf, len);
251 /* printf ("sending APDU of size %d\n", len); */
252 do_hex_dump(buf, len);
253 if (cs_put(conn, buf, len) < 0)
255 fprintf(stderr, "cs_put: %s", cs_errmsg(cs_errno(conn)));
259 odr_reset(out); /* release the APDU structure */
263 static void print_stringn(const unsigned char *buf, size_t len)
266 for (i = 0; i<len; i++)
267 if ((buf[i] <= 126 && buf[i] >= 32) || strchr ("\n\r\t\f", buf[i]))
268 printf ("%c", buf[i]);
270 printf ("\\X%02X", buf[i]);
273 static void print_refid (Z_ReferenceId *id)
277 printf ("Reference Id: ");
278 print_stringn (id->buf, id->len);
283 static Z_ReferenceId *set_refid (ODR out)
288 id = (Z_ReferenceId *) odr_malloc (out, sizeof(*id));
289 id->size = id->len = strlen(refid);
290 id->buf = (unsigned char *) odr_malloc (out, id->len);
291 memcpy (id->buf, refid, id->len);
295 /* INIT SERVICE ------------------------------- */
297 static void send_initRequest(const char* type_and_host)
299 Z_APDU *apdu = zget_APDU(out, Z_APDU_initRequest);
300 Z_InitRequest *req = apdu->u.initRequest;
303 req->options = &z3950_options;
305 ODR_MASK_ZERO(req->protocolVersion);
306 for (i = 0; i<z3950_version; i++)
307 ODR_MASK_SET(req->protocolVersion, i);
309 *req->maximumRecordSize = 1024*kilobytes;
310 *req->preferredMessageSize = 1024*kilobytes;
312 req->idAuthentication = auth;
314 req->referenceId = set_refid (out);
316 if (yazProxy && type_and_host)
317 yaz_oi_set_string_oidval(&req->otherInfo, out, VAL_PROXY,
320 if (negotiationCharset || yazLang) {
321 Z_OtherInformation **p;
322 Z_OtherInformationUnit *p0;
324 yaz_oi_APDU(apdu, &p);
326 if ((p0=yaz_oi_update(p, out, NULL, 0, 0))) {
327 ODR_MASK_SET(req->options, Z_Options_negotiationModel);
329 p0->which = Z_OtherInfo_externallyDefinedInfo;
330 p0->information.externallyDefinedInfo =
331 yaz_set_proposal_charneg(
333 (const char**)&negotiationCharset,
334 negotiationCharset ? 1 : 0,
335 (const char**)&yazLang, yazLang ? 1 : 0,
336 negotiationCharsetRecords);
341 printf("Sent initrequest.\n");
345 /* These two are used only from process_initResponse() */
346 static void render_initUserInfo(Z_OtherInformation *ui1);
347 static void render_diag(Z_DiagnosticFormat *diag);
349 static void pr_opt(const char *opt, void *clientData)
354 static int process_initResponse(Z_InitResponse *res)
357 /* save session parameters for later use */
358 session_mem = odr_extract_mem(in);
361 for (ver = 0; ver < 8; ver++)
362 if (!ODR_MASK_GET(res->protocolVersion, ver))
366 printf("Connection rejected by v%d target.\n", ver);
368 printf("Connection accepted by v%d target.\n", ver);
369 if (res->implementationId)
370 printf("ID : %s\n", res->implementationId);
371 if (res->implementationName)
372 printf("Name : %s\n", res->implementationName);
373 if (res->implementationVersion)
374 printf("Version: %s\n", res->implementationVersion);
375 if (res->userInformationField)
377 Z_External *uif = res->userInformationField;
378 if (uif->which == Z_External_userInfo1) {
379 render_initUserInfo(uif->u.userInfo1);
381 printf("UserInformationfield:\n");
382 if (!z_External(print, (Z_External**)&uif, 0, 0)) {
383 odr_perror(print, "Printing userinfo\n");
386 if (uif->which == Z_External_octet) {
387 printf("Guessing visiblestring:\n");
388 printf("'%s'\n", uif->u. octet_aligned->buf);
389 } else if (uif->which == Z_External_single) {
390 Odr_any *sat = uif->u.single_ASN1_type;
391 oident *oid = oid_getentbyoid(uif->direct_reference);
392 if (oid->value == VAL_OCLCUI) {
393 Z_OCLC_UserInformation *oclc_ui;
394 ODR decode = odr_createmem(ODR_DECODE);
395 odr_setbuf(decode, sat->buf, sat->len, 0);
396 if (!z_OCLC_UserInformation(decode, &oclc_ui, 0, 0))
397 printf ("Bad OCLC UserInformation:\n");
399 printf ("OCLC UserInformation:\n");
400 if (!z_OCLC_UserInformation(print, &oclc_ui, 0, 0))
401 printf ("Bad OCLC UserInformation spec\n");
406 /* Peek at any private Init-diagnostic APDUs */
407 printf("### NAUGHTY: External is '%.*s'\n", sat->len, sat->buf);
414 yaz_init_opt_decode(res->options, pr_opt, 0);
417 if (ODR_MASK_GET(res->options, Z_Options_namedResultSets))
420 if (ODR_MASK_GET(res->options, Z_Options_negotiationModel)) {
422 Z_CharSetandLanguageNegotiation *p =
423 yaz_get_charneg_record(res->otherInfo);
427 char *charset=NULL, *lang=NULL;
430 yaz_get_response_charneg(session_mem, p, &charset, &lang,
433 if (outputCharset && negotiationCharset) {
434 odr_set_charset (out, charset, outputCharset);
435 odr_set_charset (in, outputCharset, charset);
438 odr_set_charset (out, 0, 0);
439 odr_set_charset (in, 0, 0);
442 printf("Accepted character set : %s\n", charset);
443 printf("Accepted code language : %s\n", lang ? lang : "none");
444 printf("Accepted records in ...: %d\n", selected );
452 static void render_initUserInfo(Z_OtherInformation *ui1) {
454 printf("Init response contains %d otherInfo unit%s:\n",
455 ui1->num_elements, ui1->num_elements == 1 ? "" : "s");
457 for (i = 0; i < ui1->num_elements; i++) {
458 Z_OtherInformationUnit *unit = ui1->list[i];
459 printf(" %d: otherInfo unit contains ", i+1);
460 if (unit->which == Z_OtherInfo_externallyDefinedInfo &&
461 unit->information.externallyDefinedInfo->which ==
463 render_diag(unit->information.externallyDefinedInfo->u.diag1);
465 printf("unsupported otherInfo unit type %d\n", unit->which);
471 /* ### should this share code with display_diagrecs()? */
472 static void render_diag(Z_DiagnosticFormat *diag) {
475 printf("%d diagnostic%s:\n", diag->num, diag->num == 1 ? "" : "s");
476 for (i = 0; i < diag->num; i++) {
477 Z_DiagnosticFormat_s *ds = diag->elements[i];
478 printf(" %d: ", i+1);
480 case Z_DiagnosticFormat_s_defaultDiagRec: {
481 Z_DefaultDiagFormat *dd = ds->u.defaultDiagRec;
482 /* ### should check `dd->diagnosticSetId' */
483 printf("code=%d (%s)", *dd->condition,
484 diagbib1_str(*dd->condition));
485 /* Both types of addinfo are the same, so use type-pun */
486 if (dd->u.v2Addinfo != 0)
487 printf(",\n\taddinfo='%s'", dd->u.v2Addinfo);
490 case Z_DiagnosticFormat_s_explicitDiagnostic:
491 printf("Explicit diagnostic (not supported)");
494 printf("Unrecognised diagnostic type %d", ds->which);
498 if (ds->message != 0)
499 printf(", message='%s'", ds->message);
505 static int set_base(const char *arg)
510 for (i = 0; i<num_databaseNames; i++)
511 xfree (databaseNames[i]);
512 num_databaseNames = 0;
516 if (!(cp = strchr(arg, ' ')))
517 cp = arg + strlen(arg);
520 databaseNames[num_databaseNames] = (char *)xmalloc (1 + cp - arg);
521 memcpy (databaseNames[num_databaseNames], arg, cp - arg);
522 databaseNames[num_databaseNames][cp - arg] = '\0';
524 for (cp1 = databaseNames[num_databaseNames]; *cp1 ; cp1++)
533 if (num_databaseNames == 0)
535 num_databaseNames = 1;
536 databaseNames[0] = xstrdup("");
541 static int cmd_base(const char *arg)
545 printf("Usage: base <database> <database> ...\n");
548 return set_base(arg);
551 void cmd_open_remember_last_open_command(const char* arg, char* new_open_command)
553 if(last_open_command != arg)
555 if(last_open_command) xfree(last_open_command);
556 last_open_command = xstrdup(new_open_command);
560 int session_connect(const char *arg)
563 char type_and_host[101];
564 const char *basep = 0;
565 #if HAVE_OPENSSL_SSL_H
574 nmem_destroy (session_mem);
578 cs_get_host_args(arg, &basep);
580 strncpy(type_and_host, arg, sizeof(type_and_host)-1);
581 type_and_host[sizeof(type_and_host)-1] = '\0';
583 cmd_open_remember_last_open_command(arg,type_and_host);
586 conn = cs_create_host(yazProxy, 1, &add);
588 conn = cs_create_host(arg, 1, &add);
591 printf ("Couldn't create comstack\n");
595 if (conn->protocol == PROTO_HTTP)
596 queryType = QueryType_CQL;
598 if (conn->protocol == PROTO_HTTP)
600 printf ("SRW/HTTP not enabled in this YAZ\n");
606 protocol = conn->protocol;
607 if (conn->protocol == PROTO_HTTP)
611 printf("Connecting...");
613 if (cs_connect(conn, add) < 0)
615 printf ("error = %s\n", cs_strerror(conn));
616 if (conn->cerrno == CSYSERR)
619 yaz_strerror(msg, sizeof(msg));
620 printf ("%s\n", msg);
627 #if HAVE_OPENSSL_SSL_H
628 if ((ssl = (SSL *) cs_get_ssl(conn)))
630 X509 *server_cert = SSL_get_peer_certificate (ssl);
636 BIO *bio = BIO_new(BIO_s_mem());
638 /* get PEM buffer in memory */
639 PEM_write_bio_X509(bio, server_cert);
640 pem_len = BIO_get_mem_data(bio, &pem_buf);
641 fwrite(pem_buf, pem_len, 1, stdout);
643 /* print all info on screen .. */
644 X509_print_fp(stdout, server_cert);
647 X509_free (server_cert);
653 if (protocol == PROTO_Z3950)
655 send_initRequest(type_and_host);
661 int cmd_open(const char *arg)
665 strncpy (cur_host, arg, sizeof(cur_host)-1);
666 cur_host[sizeof(cur_host)-1] = 0;
668 return session_connect(cur_host);
675 if(!( auto_reconnect && last_open_command) ) return ;
677 open_command = (char *) xmalloc (strlen(last_open_command)+6);
678 strcpy (open_command, "open ");
680 strcat (open_command, last_open_command);
682 process_cmd_line(open_command);
687 int cmd_authentication(const char *arg)
689 static Z_IdAuthentication au;
690 static char user[40], group[40], pass[40];
691 static Z_IdPass idPass;
696 printf("Auth field set to null\n");
700 r = sscanf (arg, "%39s %39s %39s", user, group, pass);
703 printf("Authentication set to null\n");
709 if (!strcmp(user, "-")) {
710 au.which = Z_IdAuthentication_anonymous;
711 printf("Authentication set to Anonymous\n");
713 au.which = Z_IdAuthentication_open;
715 printf("Authentication set to Open (%s)\n", user);
721 au.which = Z_IdAuthentication_idPass;
722 au.u.idPass = &idPass;
723 idPass.groupId = NULL;
724 idPass.userId = user;
725 idPass.password = group;
726 printf("Authentication set to User (%s), Pass (%s)\n", user, group);
731 au.which = Z_IdAuthentication_idPass;
732 au.u.idPass = &idPass;
733 idPass.groupId = group;
734 idPass.userId = user;
735 idPass.password = pass;
736 printf("Authentication set to User (%s), Group (%s), Pass (%s)\n",
742 /* SEARCH SERVICE ------------------------------ */
743 static void display_record(Z_External *r);
745 static void print_record(const unsigned char *buf, size_t len)
748 print_stringn (buf, len);
749 /* add newline if not already added ... */
750 if (i <= 0 || buf[i-1] != '\n')
753 fwrite (buf, 1, len, marc_file);
756 static void display_record(Z_External *r)
758 oident *ent = oid_getentbyoid(r->direct_reference);
762 * Tell the user what we got.
764 if (r->direct_reference)
766 printf("Record type: ");
768 printf("%s\n", ent->desc);
769 else if (!odr_oid(print, &r->direct_reference, 0, 0))
771 odr_perror(print, "print oid");
775 /* Check if this is a known, ASN.1 type tucked away in an octet string */
776 if (ent && r->which == Z_External_octet)
778 Z_ext_typeent *type = z_ext_getentbyref(ent->value);
784 * Call the given decoder to process the record.
786 odr_setbuf(in, (char*)r->u.octet_aligned->buf,
787 r->u.octet_aligned->len, 0);
788 if (!(*type->fun)(in, &rr, 0, 0))
790 odr_perror(in, "Decoding constructed record.");
791 fprintf(stdout, "[Near %d]\n", odr_offset(in));
792 fprintf(stdout, "Packet dump:\n---------\n");
793 odr_dumpBER(stdout, (char*)r->u.octet_aligned->buf,
794 r->u.octet_aligned->len);
795 fprintf(stdout, "---------\n");
797 /* note just ignores the error ant print the bytes form the octet_aligned later */
800 * Note: we throw away the original, BER-encoded record here.
801 * Do something else with it if you want to keep it.
803 r->u.sutrs = (Z_SUTRS *) rr; /* we don't actually check the type here. */
804 r->which = type->what;
808 if (ent && ent->value == VAL_SOIF)
809 print_record((const unsigned char *) r->u.octet_aligned->buf,
810 r->u.octet_aligned->len);
811 else if (r->which == Z_External_octet)
813 const char *octet_buf = (char*)r->u.octet_aligned->buf;
814 if (ent->oclass == CLASS_RECSYN &&
815 (ent->value == VAL_TEXT_XML ||
816 ent->value == VAL_APPLICATION_XML ||
817 ent->value == VAL_HTML))
819 print_record((const unsigned char *) octet_buf,
820 r->u.octet_aligned->len);
822 else if (ent->value == VAL_POSTSCRIPT)
824 int size = r->u.octet_aligned->len;
827 print_record((const unsigned char *) octet_buf, size);
832 #if AVOID_MARC_DECODE
833 /* primitive check for a marc OID 5.1-29 except 16 */
834 ent->oidsuffix[0] == 5 && ent->oidsuffix[1] < 30 &&
835 ent->oidsuffix[1] != 16
844 yaz_marc_t mt = yaz_marc_create();
846 if (yaz_marc_decode_buf(mt, octet_buf,r->u.octet_aligned->len,
850 if (marcCharset && !strcmp(marcCharset, "auto"))
852 if (ent->value == VAL_USMARC)
854 if (octet_buf[9] == 'a')
862 else if (marcCharset)
864 if (outputCharset && from)
866 cd = yaz_iconv_open(outputCharset, from);
867 printf ("convert from %s to %s", from,
870 printf (" unsupported\n");
875 fwrite (result, 1, rlen, stdout);
879 size_t inbytesleft = rlen;
880 const char *inp = result;
884 size_t outbytesleft = sizeof(outbuf);
888 r = yaz_iconv (cd, (char**) &inp,
890 &outp, &outbytesleft);
891 if (r == (size_t) (-1))
893 int e = yaz_iconv_error(cd);
894 if (e != YAZ_ICONV_E2BIG)
897 fwrite (outbuf, outp - outbuf, 1, stdout);
903 printf ("bad MARC. Dumping as it is:\n");
904 print_record((const unsigned char*) octet_buf,
905 r->u.octet_aligned->len);
907 yaz_marc_destroy(mt);
913 print_record((const unsigned char*) octet_buf,
914 r->u.octet_aligned->len);
918 fwrite (octet_buf, 1, r->u.octet_aligned->len, marc_file);
920 else if (ent && ent->value == VAL_SUTRS)
922 if (r->which != Z_External_sutrs)
924 printf("Expecting single SUTRS type for SUTRS.\n");
927 print_record(r->u.sutrs->buf, r->u.sutrs->len);
929 else if (ent && ent->value == VAL_GRS1)
932 if (r->which != Z_External_grs1)
934 printf("Expecting single GRS type for GRS.\n");
938 yaz_display_grs1(w, r->u.grs1, 0);
942 else if ( /* OPAC display not complete yet .. */
943 ent && ent->value == VAL_OPAC)
946 if (r->u.opac->bibliographicRecord)
947 display_record(r->u.opac->bibliographicRecord);
948 for (i = 0; i<r->u.opac->num_holdingsData; i++)
950 Z_HoldingsRecord *h = r->u.opac->holdingsData[i];
951 if (h->which == Z_HoldingsRecord_marcHoldingsRecord)
953 printf ("MARC holdings %d\n", i);
954 display_record(h->u.marcHoldingsRecord);
956 else if (h->which == Z_HoldingsRecord_holdingsAndCirc)
960 Z_HoldingsAndCircData *data = h->u.holdingsAndCirc;
962 printf ("Data holdings %d\n", i);
963 if (data->typeOfRecord)
964 printf ("typeOfRecord: %s\n", data->typeOfRecord);
965 if (data->encodingLevel)
966 printf ("encodingLevel: %s\n", data->encodingLevel);
967 if (data->receiptAcqStatus)
968 printf ("receiptAcqStatus: %s\n", data->receiptAcqStatus);
969 if (data->generalRetention)
970 printf ("generalRetention: %s\n", data->generalRetention);
971 if (data->completeness)
972 printf ("completeness: %s\n", data->completeness);
973 if (data->dateOfReport)
974 printf ("dateOfReport: %s\n", data->dateOfReport);
976 printf ("nucCode: %s\n", data->nucCode);
977 if (data->localLocation)
978 printf ("localLocation: %s\n", data->localLocation);
979 if (data->shelvingLocation)
980 printf ("shelvingLocation: %s\n", data->shelvingLocation);
981 if (data->callNumber)
982 printf ("callNumber: %s\n", data->callNumber);
983 if (data->shelvingData)
984 printf ("shelvingData: %s\n", data->shelvingData);
985 if (data->copyNumber)
986 printf ("copyNumber: %s\n", data->copyNumber);
987 if (data->publicNote)
988 printf ("publicNote: %s\n", data->publicNote);
989 if (data->reproductionNote)
990 printf ("reproductionNote: %s\n", data->reproductionNote);
991 if (data->termsUseRepro)
992 printf ("termsUseRepro: %s\n", data->termsUseRepro);
993 if (data->enumAndChron)
994 printf ("enumAndChron: %s\n", data->enumAndChron);
995 for (j = 0; j<data->num_volumes; j++)
997 printf ("volume %d\n", j);
998 if (data->volumes[j]->enumeration)
999 printf (" enumeration: %s\n",
1000 data->volumes[j]->enumeration);
1001 if (data->volumes[j]->chronology)
1002 printf (" chronology: %s\n",
1003 data->volumes[j]->chronology);
1004 if (data->volumes[j]->enumAndChron)
1005 printf (" enumAndChron: %s\n",
1006 data->volumes[j]->enumAndChron);
1008 for (j = 0; j<data->num_circulationData; j++)
1010 printf ("circulation %d\n", j);
1011 if (data->circulationData[j]->availableNow)
1012 printf (" availableNow: %d\n",
1013 *data->circulationData[j]->availableNow);
1014 if (data->circulationData[j]->availablityDate)
1015 printf (" availabiltyDate: %s\n",
1016 data->circulationData[j]->availablityDate);
1017 if (data->circulationData[j]->availableThru)
1018 printf (" availableThru: %s\n",
1019 data->circulationData[j]->availableThru);
1020 if (data->circulationData[j]->restrictions)
1021 printf (" restrictions: %s\n",
1022 data->circulationData[j]->restrictions);
1023 if (data->circulationData[j]->itemId)
1024 printf (" itemId: %s\n",
1025 data->circulationData[j]->itemId);
1026 if (data->circulationData[j]->renewable)
1027 printf (" renewable: %d\n",
1028 *data->circulationData[j]->renewable);
1029 if (data->circulationData[j]->onHold)
1030 printf (" onHold: %d\n",
1031 *data->circulationData[j]->onHold);
1032 if (data->circulationData[j]->enumAndChron)
1033 printf (" enumAndChron: %s\n",
1034 data->circulationData[j]->enumAndChron);
1035 if (data->circulationData[j]->midspine)
1036 printf (" midspine: %s\n",
1037 data->circulationData[j]->midspine);
1038 if (data->circulationData[j]->temporaryLocation)
1039 printf (" temporaryLocation: %s\n",
1040 data->circulationData[j]->temporaryLocation);
1047 printf("Unknown record representation.\n");
1048 if (!z_External(print, &r, 0, 0))
1050 odr_perror(print, "Printing external");
1056 static void display_diagrecs(Z_DiagRec **pp, int num)
1060 Z_DefaultDiagFormat *r;
1062 printf("Diagnostic message(s) from database:\n");
1063 for (i = 0; i<num; i++)
1065 Z_DiagRec *p = pp[i];
1066 if (p->which != Z_DiagRec_defaultFormat)
1068 printf("Diagnostic record not in default format.\n");
1072 r = p->u.defaultFormat;
1073 if (!(ent = oid_getentbyoid(r->diagnosticSetId)) ||
1074 ent->oclass != CLASS_DIAGSET || ent->value != VAL_BIB1)
1075 printf("Missing or unknown diagset\n");
1076 printf(" [%d] %s", *r->condition, diagbib1_str(*r->condition));
1079 case Z_DefaultDiagFormat_v2Addinfo:
1080 printf (" -- v2 addinfo '%s'\n", r->u.v2Addinfo);
1082 case Z_DefaultDiagFormat_v3Addinfo:
1083 printf (" -- v3 addinfo '%s'\n", r->u.v3Addinfo);
1090 static void display_nameplusrecord(Z_NamePlusRecord *p)
1092 if (p->databaseName)
1093 printf("[%s]", p->databaseName);
1094 if (p->which == Z_NamePlusRecord_surrogateDiagnostic)
1095 display_diagrecs(&p->u.surrogateDiagnostic, 1);
1096 else if (p->which == Z_NamePlusRecord_databaseRecord)
1097 display_record(p->u.databaseRecord);
1100 static void display_records(Z_Records *p)
1104 if (p->which == Z_Records_NSD)
1106 Z_DiagRec dr, *dr_p = &dr;
1107 dr.which = Z_DiagRec_defaultFormat;
1108 dr.u.defaultFormat = p->u.nonSurrogateDiagnostic;
1109 display_diagrecs (&dr_p, 1);
1111 else if (p->which == Z_Records_multipleNSD)
1112 display_diagrecs (p->u.multipleNonSurDiagnostics->diagRecs,
1113 p->u.multipleNonSurDiagnostics->num_diagRecs);
1116 printf("Records: %d\n", p->u.databaseOrSurDiagnostics->num_records);
1117 for (i = 0; i < p->u.databaseOrSurDiagnostics->num_records; i++)
1118 display_nameplusrecord(p->u.databaseOrSurDiagnostics->records[i]);
1122 static int send_deleteResultSetRequest(const char *arg)
1127 Z_APDU *apdu = zget_APDU(out, Z_APDU_deleteResultSetRequest);
1128 Z_DeleteResultSetRequest *req = apdu->u.deleteResultSetRequest;
1130 req->referenceId = set_refid (out);
1132 req->num_resultSetList =
1133 sscanf (arg, "%30s %30s %30s %30s %30s %30s %30s %30s",
1134 names[0], names[1], names[2], names[3],
1135 names[4], names[5], names[6], names[7]);
1137 req->deleteFunction = (int *)
1138 odr_malloc (out, sizeof(*req->deleteFunction));
1139 if (req->num_resultSetList > 0)
1141 *req->deleteFunction = Z_DeleteResultSetRequest_list;
1142 req->resultSetList = (char **)
1143 odr_malloc (out, sizeof(*req->resultSetList)*
1144 req->num_resultSetList);
1145 for (i = 0; i<req->num_resultSetList; i++)
1146 req->resultSetList[i] = names[i];
1150 *req->deleteFunction = Z_DeleteResultSetRequest_all;
1151 req->resultSetList = 0;
1155 printf("Sent deleteResultSetRequest.\n");
1160 static int send_srw(Z_SRW_PDU *sr)
1162 const char *charset = negotiationCharset;
1163 const char *host_port = cur_host;
1166 Z_SOAP_Handler h[2] = {
1167 {"http://www.loc.gov/zing/srw/", 0, (Z_SOAP_fun) yaz_srw_codec},
1170 ODR o = odr_createmem(ODR_ENCODE);
1172 Z_SOAP *p = odr_malloc(o, sizeof(*p));
1175 path = odr_malloc(out, strlen(databaseNames[0])+2);
1177 strcpy(path+1, databaseNames[0]);
1179 gdu = z_get_HTTP_Request(out);
1180 gdu->u.HTTP_Request->version = http_version;
1181 gdu->u.HTTP_Request->path = odr_strdup(out, path);
1185 const char *cp0 = strstr(host_port, "://");
1186 const char *cp1 = 0;
1192 cp1 = strchr(cp0, '/');
1194 cp1 = cp0+strlen(cp0);
1198 char *h = odr_malloc(out, cp1 - cp0 + 1);
1199 memcpy (h, cp0, cp1 - cp0);
1201 z_HTTP_header_add(out, &gdu->u.HTTP_Request->headers,
1206 strcpy(ctype, "text/xml");
1207 if (charset && strlen(charset) < 20)
1209 strcat(ctype, "; charset=");
1210 strcat(ctype, charset);
1212 z_HTTP_header_add(out, &gdu->u.HTTP_Request->headers,
1213 "Content-Type", ctype);
1214 z_HTTP_header_add(out, &gdu->u.HTTP_Request->headers,
1215 "SOAPAction", "\"\"");
1216 p->which = Z_SOAP_generic;
1217 p->u.generic = odr_malloc(o, sizeof(*p->u.generic));
1218 p->u.generic->no = 0;
1219 p->u.generic->ns = 0;
1220 p->u.generic->p = sr;
1221 p->ns = "http://schemas.xmlsoap.org/soap/envelope/";
1223 ret = z_soap_codec_enc(o, &p,
1224 &gdu->u.HTTP_Request->content_buf,
1225 &gdu->u.HTTP_Request->content_len, h,
1228 if (z_GDU(out, &gdu, 0, 0))
1236 if (!z_GDU(print, &gdu, 0, 0))
1237 printf ("Failed to print outgoing APDU\n");
1240 buf_out = odr_getbuf(out, &len_out, 0);
1242 /* we don't odr_reset(out), since we may need the buffer again */
1244 do_hex_dump(buf_out, len_out);
1246 r = cs_put(conn, buf_out, len_out);
1258 static int send_SRW_scanRequest(const char *arg, int pos, int num)
1262 /* regular requestse .. */
1263 sr = yaz_srw_get(out, Z_SRW_scan_request);
1268 sr->u.scan_request->query_type = Z_SRW_query_type_cql;
1269 sr->u.scan_request->scanClause.cql = odr_strdup(out, arg);
1271 case QueryType_Prefix:
1272 sr->u.scan_request->query_type = Z_SRW_query_type_pqf;
1273 sr->u.scan_request->scanClause.pqf = odr_strdup(out, arg);
1276 printf ("Only CQL and PQF supported in SRW\n");
1279 sr->u.scan_request->responsePosition = odr_intdup(out, pos);
1280 sr->u.scan_request->maximumTerms = odr_intdup(out, num);
1281 return send_srw(sr);
1284 static int send_SRW_searchRequest(const char *arg)
1290 assert(srw_sr_odr_out == 0);
1291 srw_sr_odr_out = odr_createmem(ODR_ENCODE);
1293 odr_reset(srw_sr_odr_out);
1297 /* save this for later .. when fetching individual records */
1298 srw_sr = yaz_srw_get(srw_sr_odr_out, Z_SRW_searchRetrieve_request);
1300 /* regular request .. */
1301 sr = yaz_srw_get(out, Z_SRW_searchRetrieve_request);
1306 srw_sr->u.request->query_type = Z_SRW_query_type_cql;
1307 srw_sr->u.request->query.cql = odr_strdup(srw_sr_odr_out, arg);
1309 sr->u.request->query_type = Z_SRW_query_type_cql;
1310 sr->u.request->query.cql = odr_strdup(out, arg);
1312 case QueryType_Prefix:
1313 srw_sr->u.request->query_type = Z_SRW_query_type_pqf;
1314 srw_sr->u.request->query.pqf = odr_strdup(srw_sr_odr_out, arg);
1316 sr->u.request->query_type = Z_SRW_query_type_pqf;
1317 sr->u.request->query.pqf = odr_strdup(out, arg);
1320 printf ("Only CQL and PQF supported in SRW\n");
1323 sr->u.request->maximumRecords = odr_intdup(out, 0);
1326 sr->u.request->recordSchema = record_schema;
1327 if (recordsyntax == VAL_TEXT_XML)
1328 sr->u.explain_request->recordPacking = "xml";
1329 return send_srw(sr);
1333 static int send_searchRequest(const char *arg)
1335 Z_APDU *apdu = zget_APDU(out, Z_APDU_searchRequest);
1336 Z_SearchRequest *req = apdu->u.searchRequest;
1338 struct ccl_rpn_node *rpn = NULL;
1340 char setstring[100];
1341 Z_RPNQuery *RPNquery;
1343 YAZ_PQF_Parser pqf_parser;
1345 QueryType myQueryType = queryType;
1348 if (myQueryType == QueryType_CCL2RPN)
1350 rpn = ccl_find_str(bibset, arg, &error, &pos);
1353 printf("CCL ERROR: %s\n", ccl_err_msg(error));
1356 } else if (myQueryType == QueryType_CQL2RPN) {
1357 /* ### All this code should be wrapped in a utility function */
1359 struct cql_node *node;
1360 const char *addinfo;
1361 if (cqltrans == 0) {
1362 printf("Can't use CQL: no translation file. Try set_cqlfile\n");
1365 parser = cql_parser_create();
1366 if ((error = cql_parser_string(parser, arg)) != 0) {
1367 printf("Can't parse CQL: must be a syntax error\n");
1370 node = cql_parser_result(parser);
1371 if ((error = cql_transform_buf(cqltrans, node, pqfbuf,
1372 sizeof pqfbuf)) != 0) {
1373 error = cql_transform_error(cqltrans, &addinfo);
1374 printf ("Can't convert CQL to PQF: %s (addinfo=%s)\n",
1375 cql_strerror(error), addinfo);
1379 myQueryType = QueryType_Prefix;
1382 req->referenceId = set_refid (out);
1383 if (!strcmp(arg, "@big")) /* strictly for troublemaking */
1385 static unsigned char big[2100];
1386 static Odr_oct bigo;
1388 /* send a very big referenceid to test transport stack etc. */
1389 memset(big, 'A', 2100);
1390 bigo.len = bigo.size = 2100;
1392 req->referenceId = &bigo;
1397 sprintf(setstring, "%d", ++setnumber);
1398 req->resultSetName = setstring;
1400 *req->smallSetUpperBound = smallSetUpperBound;
1401 *req->largeSetLowerBound = largeSetLowerBound;
1402 *req->mediumSetPresentNumber = mediumSetPresentNumber;
1403 if (smallSetUpperBound > 0 || (largeSetLowerBound > 1 &&
1404 mediumSetPresentNumber > 0))
1406 req->preferredRecordSyntax =
1407 yaz_oidval_to_z3950oid(out, CLASS_RECSYN, recordsyntax);
1409 req->smallSetElementSetNames =
1410 req->mediumSetElementSetNames = elementSetNames;
1412 req->num_databaseNames = num_databaseNames;
1413 req->databaseNames = databaseNames;
1415 req->query = &query;
1417 switch (myQueryType)
1419 case QueryType_Prefix:
1420 query.which = Z_Query_type_1;
1421 pqf_parser = yaz_pqf_create ();
1422 RPNquery = yaz_pqf_parse (pqf_parser, out, arg);
1425 const char *pqf_msg;
1427 int code = yaz_pqf_error (pqf_parser, &pqf_msg, &off);
1428 printf("%*s^\n", off+4, "");
1429 printf("Prefix query error: %s (code %d)\n", pqf_msg, code);
1431 yaz_pqf_destroy (pqf_parser);
1434 yaz_pqf_destroy (pqf_parser);
1435 query.u.type_1 = RPNquery;
1438 query.which = Z_Query_type_2;
1439 query.u.type_2 = &ccl_query;
1440 ccl_query.buf = (unsigned char*) arg;
1441 ccl_query.len = strlen(arg);
1443 case QueryType_CCL2RPN:
1444 query.which = Z_Query_type_1;
1445 RPNquery = ccl_rpn_query(out, rpn);
1448 printf ("Couldn't convert from CCL to RPN\n");
1451 query.u.type_1 = RPNquery;
1452 ccl_rpn_delete (rpn);
1455 query.which = Z_Query_type_104;
1456 ext = (Z_External *) odr_malloc(out, sizeof(*ext));
1457 ext->direct_reference = odr_getoidbystr(out, "1.2.840.10003.16.2");
1458 ext->indirect_reference = 0;
1459 ext->descriptor = 0;
1460 ext->which = Z_External_CQL;
1461 ext->u.cql = odr_strdup(out, arg);
1462 query.u.type_104 = ext;
1465 printf ("Unsupported query type\n");
1468 if (send_apdu(apdu))
1469 printf("Sent searchRequest.\n");
1474 /* display Query Expression as part of searchResult-1 */
1475 static void display_queryExpression (Z_QueryExpression *qe)
1479 if (qe->which == Z_QueryExpression_term)
1481 if (qe->u.term->queryTerm)
1483 Z_Term *term = qe->u.term->queryTerm;
1484 switch (term->which)
1486 case Z_Term_general:
1487 printf (" %.*s", term->u.general->len, term->u.general->buf);
1489 case Z_Term_characterString:
1490 printf (" %s", term->u.characterString);
1492 case Z_Term_numeric:
1493 printf (" %d", *term->u.numeric);
1503 /* see if we can find USR:SearchResult-1 */
1504 static void display_searchResult (Z_OtherInformation *o)
1509 for (i = 0; i < o->num_elements; i++)
1511 if (o->list[i]->which == Z_OtherInfo_externallyDefinedInfo)
1513 Z_External *ext = o->list[i]->information.externallyDefinedInfo;
1515 if (ext->which == Z_External_searchResult1)
1518 Z_SearchInfoReport *sr = ext->u.searchResult1;
1519 printf ("SearchResult-1:");
1520 for (j = 0; j < sr->num; j++)
1522 if (!sr->elements[j]->subqueryExpression)
1524 display_queryExpression (
1525 sr->elements[j]->subqueryExpression);
1526 display_queryExpression (
1527 sr->elements[j]->subqueryInterpretation);
1528 display_queryExpression (
1529 sr->elements[j]->subqueryRecommendation);
1530 if (sr->elements[j]->subqueryCount)
1531 printf ("(%d)", *sr->elements[j]->subqueryCount);
1539 static int process_searchResponse(Z_SearchResponse *res)
1541 printf ("Received SearchResponse.\n");
1542 print_refid (res->referenceId);
1543 if (*res->searchStatus)
1544 printf("Search was a success.\n");
1546 printf("Search was a bloomin' failure.\n");
1547 printf("Number of hits: %d", *res->resultCount);
1549 printf (", setno %d", setnumber);
1551 display_searchResult (res->additionalSearchInfo);
1552 printf("records returned: %d\n",
1553 *res->numberOfRecordsReturned);
1554 setno += *res->numberOfRecordsReturned;
1556 display_records(res->records);
1560 static void print_level(int iLevel)
1563 for (i = 0; i < iLevel * 4; i++)
1567 static void print_int(int iLevel, const char *pTag, int *pInt)
1571 print_level(iLevel);
1572 printf("%s: %d\n", pTag, *pInt);
1576 static void print_string(int iLevel, const char *pTag, const char *pString)
1578 if (pString != NULL)
1580 print_level(iLevel);
1581 printf("%s: %s\n", pTag, pString);
1585 static void print_oid(int iLevel, const char *pTag, Odr_oid *pOid)
1591 print_level(iLevel);
1592 printf("%s:", pTag);
1593 for (; *pInt != -1; pInt++)
1594 printf(" %d", *pInt);
1599 static void print_referenceId(int iLevel, Z_ReferenceId *referenceId)
1601 if (referenceId != NULL)
1605 print_level(iLevel);
1606 printf("Ref Id (%d, %d): ", referenceId->len, referenceId->size);
1607 for (i = 0; i < referenceId->len; i++)
1608 printf("%c", referenceId->buf[i]);
1613 static void print_string_or_numeric(int iLevel, const char *pTag, Z_StringOrNumeric *pStringNumeric)
1615 if (pStringNumeric != NULL)
1617 switch (pStringNumeric->which)
1619 case Z_StringOrNumeric_string:
1620 print_string(iLevel, pTag, pStringNumeric->u.string);
1623 case Z_StringOrNumeric_numeric:
1624 print_int(iLevel, pTag, pStringNumeric->u.numeric);
1628 print_level(iLevel);
1629 printf("%s: valid type for Z_StringOrNumeric\n", pTag);
1635 static void print_universe_report_duplicate(
1637 Z_UniverseReportDuplicate *pUniverseReportDuplicate)
1639 if (pUniverseReportDuplicate != NULL)
1641 print_level(iLevel);
1642 printf("Universe Report Duplicate: \n");
1644 print_string_or_numeric(iLevel, "Hit No",
1645 pUniverseReportDuplicate->hitno);
1649 static void print_universe_report_hits(
1651 Z_UniverseReportHits *pUniverseReportHits)
1653 if (pUniverseReportHits != NULL)
1655 print_level(iLevel);
1656 printf("Universe Report Hits: \n");
1658 print_string_or_numeric(iLevel, "Database",
1659 pUniverseReportHits->database);
1660 print_string_or_numeric(iLevel, "Hits", pUniverseReportHits->hits);
1664 static void print_universe_report(int iLevel, Z_UniverseReport *pUniverseReport)
1666 if (pUniverseReport != NULL)
1668 print_level(iLevel);
1669 printf("Universe Report: \n");
1671 print_int(iLevel, "Total Hits", pUniverseReport->totalHits);
1672 switch (pUniverseReport->which)
1674 case Z_UniverseReport_databaseHits:
1675 print_universe_report_hits(iLevel,
1676 pUniverseReport->u.databaseHits);
1679 case Z_UniverseReport_duplicate:
1680 print_universe_report_duplicate(iLevel,
1681 pUniverseReport->u.duplicate);
1685 print_level(iLevel);
1686 printf("Type: %d\n", pUniverseReport->which);
1692 static void print_external(int iLevel, Z_External *pExternal)
1694 if (pExternal != NULL)
1696 print_level(iLevel);
1697 printf("External: \n");
1699 print_oid(iLevel, "Direct Reference", pExternal->direct_reference);
1700 print_int(iLevel, "InDirect Reference", pExternal->indirect_reference);
1701 print_string(iLevel, "Descriptor", pExternal->descriptor);
1702 switch (pExternal->which)
1704 case Z_External_universeReport:
1705 print_universe_report(iLevel, pExternal->u.universeReport);
1709 print_level(iLevel);
1710 printf("Type: %d\n", pExternal->which);
1716 static int process_resourceControlRequest (Z_ResourceControlRequest *req)
1718 printf ("Received ResourceControlRequest.\n");
1719 print_referenceId(1, req->referenceId);
1720 print_int(1, "Suspended Flag", req->suspendedFlag);
1721 print_int(1, "Partial Results Available", req->partialResultsAvailable);
1722 print_int(1, "Response Required", req->responseRequired);
1723 print_int(1, "Triggered Request Flag", req->triggeredRequestFlag);
1724 print_external(1, req->resourceReport);
1728 void process_ESResponse(Z_ExtendedServicesResponse *res)
1731 switch (*res->operationStatus)
1733 case Z_ExtendedServicesResponse_done:
1736 case Z_ExtendedServicesResponse_accepted:
1737 printf ("accepted\n");
1739 case Z_ExtendedServicesResponse_failure:
1740 printf ("failure\n");
1741 display_diagrecs(res->diagnostics, res->num_diagnostics);
1744 printf ("unknown\n");
1746 if ( (*res->operationStatus != Z_ExtendedServicesResponse_failure) &&
1747 (res->num_diagnostics != 0) ) {
1748 display_diagrecs(res->diagnostics, res->num_diagnostics);
1750 print_refid (res->referenceId);
1751 if (res->taskPackage &&
1752 res->taskPackage->which == Z_External_extendedService)
1754 Z_TaskPackage *taskPackage = res->taskPackage->u.extendedService;
1755 Odr_oct *id = taskPackage->targetReference;
1756 Z_External *ext = taskPackage->taskSpecificParameters;
1760 printf ("Target Reference: ");
1761 print_stringn (id->buf, id->len);
1764 if (ext->which == Z_External_update)
1766 Z_IUUpdateTaskPackage *utp = ext->u.update->u.taskPackage;
1767 if (utp && utp->targetPart)
1769 Z_IUTargetPart *targetPart = utp->targetPart;
1772 for (i = 0; i<targetPart->num_taskPackageRecords; i++)
1775 Z_IUTaskPackageRecordStructure *tpr =
1776 targetPart->taskPackageRecords[i];
1777 printf ("task package record %d\n", i+1);
1778 if (tpr->which == Z_IUTaskPackageRecordStructure_record)
1780 display_record (tpr->u.record);
1784 printf ("other type\n");
1792 const char *get_ill_element (void *clientData, const char *element)
1797 static Z_External *create_external_itemRequest()
1799 struct ill_get_ctl ctl;
1800 ILL_ItemRequest *req;
1802 int item_request_size = 0;
1803 char *item_request_buf = 0;
1807 ctl.f = get_ill_element;
1809 req = ill_get_ItemRequest(&ctl, "ill", 0);
1811 printf ("ill_get_ItemRequest failed\n");
1813 if (!ill_ItemRequest (out, &req, 0, 0))
1817 ill_ItemRequest(print, &req, 0, 0);
1820 item_request_buf = odr_getbuf (out, &item_request_size, 0);
1821 if (item_request_buf)
1822 odr_setbuf (out, item_request_buf, item_request_size, 1);
1823 printf ("Couldn't encode ItemRequest, size %d\n", item_request_size);
1830 item_request_buf = odr_getbuf (out, &item_request_size, 0);
1831 oid.proto = PROTO_GENERAL;
1832 oid.oclass = CLASS_GENERAL;
1833 oid.value = VAL_ISO_ILL_1;
1835 r = (Z_External *) odr_malloc (out, sizeof(*r));
1836 r->direct_reference = odr_oiddup(out,oid_getoidbyent(&oid));
1837 r->indirect_reference = 0;
1839 r->which = Z_External_single;
1841 r->u.single_ASN1_type = (Odr_oct *)
1842 odr_malloc (out, sizeof(*r->u.single_ASN1_type));
1843 r->u.single_ASN1_type->buf = (unsigned char *)
1844 odr_malloc (out, item_request_size);
1845 r->u.single_ASN1_type->len = item_request_size;
1846 r->u.single_ASN1_type->size = item_request_size;
1847 memcpy (r->u.single_ASN1_type->buf, item_request_buf,
1850 do_hex_dump(item_request_buf,item_request_size);
1855 static Z_External *create_external_ILL_APDU(int which)
1857 struct ill_get_ctl ctl;
1860 int ill_request_size = 0;
1861 char *ill_request_buf = 0;
1865 ctl.f = get_ill_element;
1867 ill_apdu = ill_get_APDU(&ctl, "ill", 0);
1869 if (!ill_APDU (out, &ill_apdu, 0, 0))
1873 printf ("-------------------\n");
1874 ill_APDU(print, &ill_apdu, 0, 0);
1876 printf ("-------------------\n");
1878 ill_request_buf = odr_getbuf (out, &ill_request_size, 0);
1879 if (ill_request_buf)
1880 odr_setbuf (out, ill_request_buf, ill_request_size, 1);
1881 printf ("Couldn't encode ILL-Request, size %d\n", ill_request_size);
1887 ill_request_buf = odr_getbuf (out, &ill_request_size, 0);
1889 oid.proto = PROTO_GENERAL;
1890 oid.oclass = CLASS_GENERAL;
1891 oid.value = VAL_ISO_ILL_1;
1893 r = (Z_External *) odr_malloc (out, sizeof(*r));
1894 r->direct_reference = odr_oiddup(out,oid_getoidbyent(&oid));
1895 r->indirect_reference = 0;
1897 r->which = Z_External_single;
1899 r->u.single_ASN1_type = (Odr_oct *)
1900 odr_malloc (out, sizeof(*r->u.single_ASN1_type));
1901 r->u.single_ASN1_type->buf = (unsigned char *)
1902 odr_malloc (out, ill_request_size);
1903 r->u.single_ASN1_type->len = ill_request_size;
1904 r->u.single_ASN1_type->size = ill_request_size;
1905 memcpy (r->u.single_ASN1_type->buf, ill_request_buf, ill_request_size);
1906 /* printf ("len = %d\n", ill_request_size); */
1907 /* do_hex_dump(ill_request_buf,ill_request_size); */
1908 /* printf("--- end of extenal\n"); */
1915 static Z_External *create_ItemOrderExternal(const char *type, int itemno)
1917 Z_External *r = (Z_External *) odr_malloc(out, sizeof(Z_External));
1918 oident ItemOrderRequest;
1920 ItemOrderRequest.proto = PROTO_Z3950;
1921 ItemOrderRequest.oclass = CLASS_EXTSERV;
1922 ItemOrderRequest.value = VAL_ITEMORDER;
1924 r->direct_reference = odr_oiddup(out,oid_getoidbyent(&ItemOrderRequest));
1925 r->indirect_reference = 0;
1928 r->which = Z_External_itemOrder;
1930 r->u.itemOrder = (Z_ItemOrder *) odr_malloc(out,sizeof(Z_ItemOrder));
1931 memset(r->u.itemOrder, 0, sizeof(Z_ItemOrder));
1932 r->u.itemOrder->which=Z_IOItemOrder_esRequest;
1934 r->u.itemOrder->u.esRequest = (Z_IORequest *)
1935 odr_malloc(out,sizeof(Z_IORequest));
1936 memset(r->u.itemOrder->u.esRequest, 0, sizeof(Z_IORequest));
1938 r->u.itemOrder->u.esRequest->toKeep = (Z_IOOriginPartToKeep *)
1939 odr_malloc(out,sizeof(Z_IOOriginPartToKeep));
1940 memset(r->u.itemOrder->u.esRequest->toKeep, 0, sizeof(Z_IOOriginPartToKeep));
1941 r->u.itemOrder->u.esRequest->notToKeep = (Z_IOOriginPartNotToKeep *)
1942 odr_malloc(out,sizeof(Z_IOOriginPartNotToKeep));
1943 memset(r->u.itemOrder->u.esRequest->notToKeep, 0, sizeof(Z_IOOriginPartNotToKeep));
1945 r->u.itemOrder->u.esRequest->toKeep->supplDescription = NULL;
1946 r->u.itemOrder->u.esRequest->toKeep->contact = NULL;
1947 r->u.itemOrder->u.esRequest->toKeep->addlBilling = NULL;
1949 r->u.itemOrder->u.esRequest->notToKeep->resultSetItem =
1950 (Z_IOResultSetItem *) odr_malloc(out, sizeof(Z_IOResultSetItem));
1951 memset(r->u.itemOrder->u.esRequest->notToKeep->resultSetItem, 0, sizeof(Z_IOResultSetItem));
1952 r->u.itemOrder->u.esRequest->notToKeep->resultSetItem->resultSetId = "1";
1954 r->u.itemOrder->u.esRequest->notToKeep->resultSetItem->item =
1955 (int *) odr_malloc(out, sizeof(int));
1956 *r->u.itemOrder->u.esRequest->notToKeep->resultSetItem->item = itemno;
1958 if (!strcmp (type, "item") || !strcmp(type, "2"))
1960 printf ("using item-request\n");
1961 r->u.itemOrder->u.esRequest->notToKeep->itemRequest =
1962 create_external_itemRequest();
1964 else if (!strcmp(type, "ill") || !strcmp(type, "1"))
1966 printf ("using ILL-request\n");
1967 r->u.itemOrder->u.esRequest->notToKeep->itemRequest =
1968 create_external_ILL_APDU(ILL_APDU_ILL_Request);
1970 else if (!strcmp(type, "xml") || !strcmp(type, "3"))
1972 const char *xml_buf =
1974 " <type>request</type>\n"
1975 " <libraryNo>000200</libraryNo>\n"
1976 " <borrowerTicketNo> 1212 </borrowerTicketNo>\n"
1978 r->u.itemOrder->u.esRequest->notToKeep->itemRequest =
1979 z_ext_record (out, VAL_TEXT_XML, xml_buf, strlen(xml_buf));
1982 r->u.itemOrder->u.esRequest->notToKeep->itemRequest = 0;
1987 static int send_itemorder(const char *type, int itemno)
1989 Z_APDU *apdu = zget_APDU(out, Z_APDU_extendedServicesRequest);
1990 Z_ExtendedServicesRequest *req = apdu->u.extendedServicesRequest;
1991 oident ItemOrderRequest;
1993 ItemOrderRequest.proto = PROTO_Z3950;
1994 ItemOrderRequest.oclass = CLASS_EXTSERV;
1995 ItemOrderRequest.value = VAL_ITEMORDER;
1996 req->packageType = odr_oiddup(out,oid_getoidbyent(&ItemOrderRequest));
1997 req->packageName = esPackageName;
1999 req->taskSpecificParameters = create_ItemOrderExternal(type, itemno);
2005 static int only_z3950()
2007 if (protocol == PROTO_HTTP)
2009 printf ("Not supported by SRW\n");
2015 static int cmd_update_common(const char *arg, int version);
2017 static int cmd_update(const char *arg)
2019 return cmd_update_common(arg, 1);
2022 static int cmd_update0(const char *arg)
2024 return cmd_update_common(arg, 0);
2027 static int cmd_update_common(const char *arg, int version)
2029 Z_APDU *apdu = zget_APDU(out, Z_APDU_extendedServicesRequest );
2030 Z_ExtendedServicesRequest *req = apdu->u.extendedServicesRequest;
2032 char action[20], recid[20], fname[80];
2034 Z_External *record_this = 0;
2041 sscanf (arg, "%19s %19s %79s", action, recid, fname);
2043 if (!strcmp (action, "insert"))
2044 action_no = Z_IUOriginPartToKeep_recordInsert;
2045 else if (!strcmp (action, "replace"))
2046 action_no = Z_IUOriginPartToKeep_recordReplace;
2047 else if (!strcmp (action, "delete"))
2048 action_no = Z_IUOriginPartToKeep_recordDelete;
2049 else if (!strcmp (action, "update"))
2050 action_no = Z_IUOriginPartToKeep_specialUpdate;
2053 printf ("Bad action: %s\n", action);
2054 printf ("Possible values: insert, replace, delete, update\n");
2062 stat (fname, &status);
2063 if (S_ISREG(status.st_mode) && (inf = fopen(fname, "r")))
2065 size_t len = status.st_size;
2066 char *buf = (char *) xmalloc (len);
2068 fread (buf, 1, len, inf);
2072 record_this = z_ext_record (out, VAL_TEXT_XML, buf, len);
2078 printf ("File %s doesn't exist\n", fname);
2086 printf ("No last record (update ignored)\n");
2089 record_this = record_last;
2093 yaz_oidval_to_z3950oid(out, CLASS_EXTSERV,
2094 version == 0 ? VAL_DBUPDATE0 : VAL_DBUPDATE);
2096 req->packageName = esPackageName;
2098 req->referenceId = set_refid (out);
2100 r = req->taskSpecificParameters = (Z_External *)
2101 odr_malloc (out, sizeof(*r));
2102 r->direct_reference = req->packageType;
2103 r->indirect_reference = 0;
2107 Z_IU0OriginPartToKeep *toKeep;
2108 Z_IU0SuppliedRecords *notToKeep;
2110 r->which = Z_External_update0;
2111 r->u.update0 = (Z_IU0Update *) odr_malloc(out, sizeof(*r->u.update0));
2112 r->u.update0->which = Z_IUUpdate_esRequest;
2113 r->u.update0->u.esRequest = (Z_IU0UpdateEsRequest *)
2114 odr_malloc(out, sizeof(*r->u.update0->u.esRequest));
2115 toKeep = r->u.update0->u.esRequest->toKeep = (Z_IU0OriginPartToKeep *)
2116 odr_malloc(out, sizeof(*r->u.update0->u.esRequest->toKeep));
2118 toKeep->databaseName = databaseNames[0];
2120 toKeep->elementSetName = 0;
2122 toKeep->action = (int *) odr_malloc(out, sizeof(*toKeep->action));
2123 *toKeep->action = action_no;
2125 notToKeep = r->u.update0->u.esRequest->notToKeep = (Z_IU0SuppliedRecords *)
2126 odr_malloc(out, sizeof(*r->u.update0->u.esRequest->notToKeep));
2128 notToKeep->elements = (Z_IU0SuppliedRecords_elem **)
2129 odr_malloc(out, sizeof(*notToKeep->elements));
2130 notToKeep->elements[0] = (Z_IU0SuppliedRecords_elem *)
2131 odr_malloc(out, sizeof(**notToKeep->elements));
2132 notToKeep->elements[0]->which = Z_IUSuppliedRecords_elem_opaque;
2135 notToKeep->elements[0]->u.opaque = (Odr_oct *)
2136 odr_malloc (out, sizeof(Odr_oct));
2137 notToKeep->elements[0]->u.opaque->buf = (unsigned char *) recid;
2138 notToKeep->elements[0]->u.opaque->size = strlen(recid);
2139 notToKeep->elements[0]->u.opaque->len = strlen(recid);
2142 notToKeep->elements[0]->u.opaque = 0;
2143 notToKeep->elements[0]->supplementalId = 0;
2144 notToKeep->elements[0]->correlationInfo = 0;
2145 notToKeep->elements[0]->record = record_this;
2149 Z_IUOriginPartToKeep *toKeep;
2150 Z_IUSuppliedRecords *notToKeep;
2152 r->which = Z_External_update;
2153 r->u.update = (Z_IUUpdate *) odr_malloc(out, sizeof(*r->u.update));
2154 r->u.update->which = Z_IUUpdate_esRequest;
2155 r->u.update->u.esRequest = (Z_IUUpdateEsRequest *)
2156 odr_malloc(out, sizeof(*r->u.update->u.esRequest));
2157 toKeep = r->u.update->u.esRequest->toKeep = (Z_IUOriginPartToKeep *)
2158 odr_malloc(out, sizeof(*r->u.update->u.esRequest->toKeep));
2160 toKeep->databaseName = databaseNames[0];
2162 toKeep->elementSetName = 0;
2163 toKeep->actionQualifier = 0;
2164 toKeep->action = (int *) odr_malloc(out, sizeof(*toKeep->action));
2165 *toKeep->action = action_no;
2167 notToKeep = r->u.update->u.esRequest->notToKeep = (Z_IUSuppliedRecords *)
2168 odr_malloc(out, sizeof(*r->u.update->u.esRequest->notToKeep));
2170 notToKeep->elements = (Z_IUSuppliedRecords_elem **)
2171 odr_malloc(out, sizeof(*notToKeep->elements));
2172 notToKeep->elements[0] = (Z_IUSuppliedRecords_elem *)
2173 odr_malloc(out, sizeof(**notToKeep->elements));
2174 notToKeep->elements[0]->which = Z_IUSuppliedRecords_elem_opaque;
2177 notToKeep->elements[0]->u.opaque = (Odr_oct *)
2178 odr_malloc (out, sizeof(Odr_oct));
2179 notToKeep->elements[0]->u.opaque->buf = (unsigned char *) recid;
2180 notToKeep->elements[0]->u.opaque->size = strlen(recid);
2181 notToKeep->elements[0]->u.opaque->len = strlen(recid);
2184 notToKeep->elements[0]->u.opaque = 0;
2185 notToKeep->elements[0]->supplementalId = 0;
2186 notToKeep->elements[0]->correlationInfo = 0;
2187 notToKeep->elements[0]->record = record_this;
2195 static int cmd_itemorder(const char *arg)
2202 if (sscanf (arg, "%10s %d", type, &itemno) != 2)
2205 printf("Item order request\n");
2207 send_itemorder(type, itemno);
2211 static void show_opt(const char *arg, void *clientData)
2213 printf ("%s ", arg);
2216 static int cmd_zversion(const char *arg)
2219 z3950_version = atoi(arg);
2221 printf ("version is %d\n", z3950_version);
2225 static int cmd_options(const char *arg)
2231 r = yaz_init_opt_encode(&z3950_options, arg, &pos);
2235 yaz_init_opt_decode(&z3950_options, show_opt, 0);
2241 static int cmd_explain(const char *arg)
2243 if (protocol != PROTO_HTTP)
2254 /* save this for later .. when fetching individual records */
2255 sr = yaz_srw_get(out, Z_SRW_explain_request);
2256 if (recordsyntax == VAL_TEXT_XML)
2257 sr->u.explain_request->recordPacking = "xml";
2265 static int cmd_init(const char *arg)
2267 if (!conn || protocol != PROTO_Z3950)
2269 send_initRequest(0);
2273 static int cmd_find(const char *arg)
2277 printf("Find what?\n");
2280 if (protocol == PROTO_HTTP)
2287 if (!send_SRW_searchRequest(arg))
2300 printf("Not connected yet\n");
2304 if (!send_searchRequest(arg))
2310 static int cmd_delete(const char *arg)
2314 printf("Not connected yet\n");
2319 if (!send_deleteResultSetRequest(arg))
2324 static int cmd_ssub(const char *arg)
2326 if (!(smallSetUpperBound = atoi(arg)))
2331 static int cmd_lslb(const char *arg)
2335 if (!(largeSetLowerBound = atoi(arg)))
2340 static int cmd_mspn(const char *arg)
2344 if (!(mediumSetPresentNumber = atoi(arg)))
2349 static int cmd_status(const char *arg)
2351 printf("smallSetUpperBound: %d\n", smallSetUpperBound);
2352 printf("largeSetLowerBound: %d\n", largeSetLowerBound);
2353 printf("mediumSetPresentNumber: %d\n", mediumSetPresentNumber);
2357 static int cmd_setnames(const char *arg)
2359 if (*arg == '1') /* enable ? */
2361 else if (*arg == '0') /* disable ? */
2363 else if (setnumber < 0) /* no args, toggle .. */
2369 printf("Set numbering enabled.\n");
2371 printf("Set numbering disabled.\n");
2375 /* PRESENT SERVICE ----------------------------- */
2377 static void parse_show_args(const char *arg_c, char *setstring,
2378 int *start, int *number)
2383 strncpy(arg, arg_c, sizeof(arg)-1);
2384 arg[sizeof(arg)-1] = '\0';
2386 if ((p = strchr(arg, '+')))
2388 *number = atoi(p + 1);
2393 if (p && (p=strchr(p+1, '+')))
2394 strcpy (setstring, p+1);
2395 else if (setnumber >= 0)
2396 sprintf(setstring, "%d", setnumber);
2401 static int send_presentRequest(const char *arg)
2403 Z_APDU *apdu = zget_APDU(out, Z_APDU_presentRequest);
2404 Z_PresentRequest *req = apdu->u.presentRequest;
2405 Z_RecordComposition compo;
2407 char setstring[100];
2409 req->referenceId = set_refid (out);
2411 parse_show_args(arg, setstring, &setno, &nos);
2413 req->resultSetId = setstring;
2415 req->resultSetStartPoint = &setno;
2416 req->numberOfRecordsRequested = &nos;
2418 req->preferredRecordSyntax =
2419 yaz_oidval_to_z3950oid(out, CLASS_RECSYN, recordsyntax);
2423 req->recordComposition = &compo;
2424 compo.which = Z_RecordComp_complex;
2425 compo.u.complex = (Z_CompSpec *)
2426 odr_malloc(out, sizeof(*compo.u.complex));
2427 compo.u.complex->selectAlternativeSyntax = (bool_t *)
2428 odr_malloc(out, sizeof(bool_t));
2429 *compo.u.complex->selectAlternativeSyntax = 0;
2431 compo.u.complex->generic = (Z_Specification *)
2432 odr_malloc(out, sizeof(*compo.u.complex->generic));
2433 compo.u.complex->generic->which = Z_Schema_oid;
2435 compo.u.complex->generic->schema.oid =
2436 yaz_str_to_z3950oid(out, CLASS_SCHEMA, record_schema);
2438 if (!compo.u.complex->generic->schema.oid)
2440 /* OID wasn't a schema! Try record syntax instead. */
2441 compo.u.complex->generic->schema.oid = (Odr_oid *)
2442 yaz_str_to_z3950oid(out, CLASS_RECSYN, record_schema);
2444 if (!elementSetNames)
2445 compo.u.complex->generic->elementSpec = 0;
2448 compo.u.complex->generic->elementSpec = (Z_ElementSpec *)
2449 odr_malloc(out, sizeof(Z_ElementSpec));
2450 compo.u.complex->generic->elementSpec->which =
2451 Z_ElementSpec_elementSetName;
2452 compo.u.complex->generic->elementSpec->u.elementSetName =
2453 elementSetNames->u.generic;
2455 compo.u.complex->num_dbSpecific = 0;
2456 compo.u.complex->dbSpecific = 0;
2457 compo.u.complex->num_recordSyntax = 0;
2458 compo.u.complex->recordSyntax = 0;
2460 else if (elementSetNames)
2462 req->recordComposition = &compo;
2463 compo.which = Z_RecordComp_simple;
2464 compo.u.simple = elementSetNames;
2467 printf("Sent presentRequest (%d+%d).\n", setno, nos);
2472 static int send_SRW_presentRequest(const char *arg)
2474 char setstring[100];
2476 Z_SRW_PDU *sr = srw_sr;
2480 parse_show_args(arg, setstring, &setno, &nos);
2481 sr->u.request->startRecord = odr_intdup(out, setno);
2482 sr->u.request->maximumRecords = odr_intdup(out, nos);
2484 sr->u.request->recordSchema = record_schema;
2485 if (recordsyntax == VAL_TEXT_XML)
2486 sr->u.request->recordPacking = "xml";
2487 return send_srw(sr);
2491 static void close_session (void)
2498 nmem_destroy (session_mem);
2507 void process_close(Z_Close *req)
2509 Z_APDU *apdu = zget_APDU(out, Z_APDU_close);
2510 Z_Close *res = apdu->u.close;
2512 static char *reasons[] =
2517 "cost limit reached",
2519 "security violation",
2526 printf("Reason: %s, message: %s\n", reasons[*req->closeReason],
2527 req->diagnosticInformation ? req->diagnosticInformation : "NULL");
2532 *res->closeReason = Z_Close_finished;
2534 printf("Sent response.\n");
2539 static int cmd_show(const char *arg)
2541 if (protocol == PROTO_HTTP)
2548 if (!send_SRW_presentRequest(arg))
2558 printf("Not connected yet\n");
2561 if (!send_presentRequest(arg))
2567 int cmd_quit(const char *arg)
2569 printf("See you later, alligator.\n");
2575 int cmd_cancel(const char *arg)
2577 Z_APDU *apdu = zget_APDU(out, Z_APDU_triggerResourceControlRequest);
2578 Z_TriggerResourceControlRequest *req =
2579 apdu->u.triggerResourceControlRequest;
2584 printf("Session not initialized yet\n");
2589 if (!ODR_MASK_GET(session->options, Z_Options_triggerResourceCtrl))
2591 printf("Target doesn't support cancel (trigger resource ctrl)\n");
2594 *req->requestedAction = Z_TriggerResourceControlRequest_cancel;
2595 req->resultSetWanted = &rfalse;
2596 req->referenceId = set_refid (out);
2599 printf("Sent cancel request\n");
2604 int cmd_cancel_find(const char *arg) {
2608 return cmd_cancel("");
2613 int send_scanrequest(const char *query, int pp, int num, const char *term)
2615 Z_APDU *apdu = zget_APDU(out, Z_APDU_scanRequest);
2616 Z_ScanRequest *req = apdu->u.scanRequest;
2620 if (queryType == QueryType_CCL2RPN)
2623 struct ccl_rpn_node *rpn;
2625 rpn = ccl_find_str (bibset, query, &error, &pos);
2628 printf("CCL ERROR: %s\n", ccl_err_msg(error));
2632 yaz_oidval_to_z3950oid(out, CLASS_ATTSET, VAL_BIB1);
2633 if (!(req->termListAndStartPoint = ccl_scan_query (out, rpn)))
2635 printf("Couldn't convert CCL to Scan term\n");
2638 ccl_rpn_delete (rpn);
2642 YAZ_PQF_Parser pqf_parser = yaz_pqf_create ();
2644 if (!(req->termListAndStartPoint =
2645 yaz_pqf_scan(pqf_parser, out, &req->attributeSet, query)))
2647 const char *pqf_msg;
2649 int code = yaz_pqf_error (pqf_parser, &pqf_msg, &off);
2650 printf("%*s^\n", off+7, "");
2651 printf("Prefix query error: %s (code %d)\n", pqf_msg, code);
2652 yaz_pqf_destroy (pqf_parser);
2655 yaz_pqf_destroy (pqf_parser);
2659 if (req->termListAndStartPoint->term &&
2660 req->termListAndStartPoint->term->which == Z_Term_general &&
2661 req->termListAndStartPoint->term->u.general)
2663 req->termListAndStartPoint->term->u.general->buf =
2664 (unsigned char *) odr_strdup(out, term);
2665 req->termListAndStartPoint->term->u.general->len =
2666 req->termListAndStartPoint->term->u.general->size =
2670 req->referenceId = set_refid (out);
2671 req->num_databaseNames = num_databaseNames;
2672 req->databaseNames = databaseNames;
2673 req->numberOfTermsRequested = #
2674 req->preferredPositionInResponse = &pp;
2675 req->stepSize = odr_intdup(out, scan_stepSize);
2680 int send_sortrequest(const char *arg, int newset)
2682 Z_APDU *apdu = zget_APDU(out, Z_APDU_sortRequest);
2683 Z_SortRequest *req = apdu->u.sortRequest;
2684 Z_SortKeySpecList *sksl = (Z_SortKeySpecList *)
2685 odr_malloc (out, sizeof(*sksl));
2691 sprintf (setstring, "%d", setnumber);
2693 sprintf (setstring, "default");
2695 req->referenceId = set_refid (out);
2697 req->num_inputResultSetNames = 1;
2698 req->inputResultSetNames = (Z_InternationalString **)
2699 odr_malloc (out, sizeof(*req->inputResultSetNames));
2700 req->inputResultSetNames[0] = odr_strdup (out, setstring);
2702 if (newset && setnumber >= 0)
2703 sprintf (setstring, "%d", ++setnumber);
2705 req->sortedResultSetName = odr_strdup (out, setstring);
2707 req->sortSequence = yaz_sort_spec (out, arg);
2708 if (!req->sortSequence)
2710 printf ("Missing sort specifications\n");
2717 void display_term(Z_TermInfo *t)
2720 printf("%s", t->displayTerm);
2721 else if (t->term->which == Z_Term_general)
2723 printf("%.*s", t->term->u.general->len, t->term->u.general->buf);
2724 sprintf(last_scan_line, "%.*s", t->term->u.general->len,
2725 t->term->u.general->buf);
2728 printf("Term (not general)");
2729 if (t->globalOccurrences)
2730 printf (" (%d)\n", *t->globalOccurrences);
2735 void process_scanResponse(Z_ScanResponse *res)
2738 Z_Entry **entries = NULL;
2739 int num_entries = 0;
2741 printf("Received ScanResponse\n");
2742 print_refid (res->referenceId);
2743 printf("%d entries", *res->numberOfEntriesReturned);
2744 if (res->positionOfTerm)
2745 printf (", position=%d", *res->positionOfTerm);
2747 if (*res->scanStatus != Z_Scan_success)
2748 printf("Scan returned code %d\n", *res->scanStatus);
2751 if ((entries = res->entries->entries))
2752 num_entries = res->entries->num_entries;
2753 for (i = 0; i < num_entries; i++)
2755 int pos_term = res->positionOfTerm ? *res->positionOfTerm : -1;
2756 if (entries[i]->which == Z_Entry_termInfo)
2758 printf("%c ", i + 1 == pos_term ? '*' : ' ');
2759 display_term(entries[i]->u.termInfo);
2762 display_diagrecs(&entries[i]->u.surrogateDiagnostic, 1);
2764 if (res->entries->nonsurrogateDiagnostics)
2765 display_diagrecs (res->entries->nonsurrogateDiagnostics,
2766 res->entries->num_nonsurrogateDiagnostics);
2769 void process_sortResponse(Z_SortResponse *res)
2771 printf("Received SortResponse: status=");
2772 switch (*res->sortStatus)
2774 case Z_SortResponse_success:
2775 printf ("success"); break;
2776 case Z_SortResponse_partial_1:
2777 printf ("partial"); break;
2778 case Z_SortResponse_failure:
2779 printf ("failure"); break;
2781 printf ("unknown (%d)", *res->sortStatus);
2784 print_refid (res->referenceId);
2785 if (res->diagnostics)
2786 display_diagrecs(res->diagnostics,
2787 res->num_diagnostics);
2790 void process_deleteResultSetResponse (Z_DeleteResultSetResponse *res)
2792 printf("Got deleteResultSetResponse status=%d\n",
2793 *res->deleteOperationStatus);
2794 if (res->deleteListStatuses)
2797 for (i = 0; i < res->deleteListStatuses->num; i++)
2799 printf ("%s status=%d\n", res->deleteListStatuses->elements[i]->id,
2800 *res->deleteListStatuses->elements[i]->status);
2805 int cmd_sort_generic(const char *arg, int newset)
2809 printf("Session not initialized yet\n");
2814 if (!ODR_MASK_GET(session->options, Z_Options_sort))
2816 printf("Target doesn't support sort\n");
2821 if (send_sortrequest(arg, newset) < 0)
2828 int cmd_sort(const char *arg)
2830 return cmd_sort_generic (arg, 0);
2833 int cmd_sort_newset (const char *arg)
2835 return cmd_sort_generic (arg, 1);
2838 int cmd_scanstep(const char *arg)
2840 scan_stepSize = atoi(arg);
2844 int cmd_scanpos(const char *arg)
2846 int r = sscanf(arg, "%d", &scan_position);
2852 int cmd_scan(const char *arg)
2854 if (protocol == PROTO_HTTP)
2863 if (send_SRW_scanRequest(arg, scan_position, 20) < 0)
2868 if (send_SRW_scanRequest(last_scan_line, 1, 20) < 0)
2883 printf("Session not initialized yet\n");
2887 if (!ODR_MASK_GET(session->options, Z_Options_scan))
2889 printf("Target doesn't support scan\n");
2894 strcpy (last_scan_query, arg);
2895 if (send_scanrequest(arg, scan_position, 20, 0) < 0)
2900 if (send_scanrequest(last_scan_query, 1, 20, last_scan_line) < 0)
2907 int cmd_schema(const char *arg)
2909 xfree(record_schema);
2912 record_schema = xstrdup(arg);
2916 int cmd_format(const char *arg)
2921 printf("Usage: format <recordsyntax>\n");
2924 nsyntax = oid_getvalbyname (arg);
2925 if (strcmp(arg, "none") && nsyntax == VAL_NONE)
2927 printf ("unknown record syntax\n");
2930 recordsyntax = nsyntax;
2934 int cmd_elements(const char *arg)
2936 static Z_ElementSetNames esn;
2937 static char what[100];
2941 elementSetNames = 0;
2945 esn.which = Z_ElementSetNames_generic;
2946 esn.u.generic = what;
2947 elementSetNames = &esn;
2951 int cmd_attributeset(const char *arg)
2957 printf("Usage: attributeset <setname>\n");
2960 sscanf(arg, "%s", what);
2961 if (p_query_attset (what))
2963 printf("Unknown attribute set name\n");
2969 int cmd_querytype (const char *arg)
2971 if (!strcmp (arg, "ccl"))
2972 queryType = QueryType_CCL;
2973 else if (!strcmp (arg, "prefix") || !strcmp(arg, "rpn"))
2974 queryType = QueryType_Prefix;
2975 else if (!strcmp (arg, "ccl2rpn") || !strcmp (arg, "cclrpn"))
2976 queryType = QueryType_CCL2RPN;
2977 else if (!strcmp(arg, "cql"))
2978 queryType = QueryType_CQL;
2979 else if (!strcmp (arg, "cql2rpn") || !strcmp (arg, "cqlrpn"))
2980 queryType = QueryType_CQL2RPN;
2983 printf ("Querytype must be one of:\n");
2984 printf (" prefix - Prefix query\n");
2985 printf (" ccl - CCL query\n");
2986 printf (" ccl2rpn - CCL query converted to RPN\n");
2987 printf (" cql - CQL\n");
2988 printf (" cql2rpn - CQL query converted to RPN\n");
2994 int cmd_refid (const char *arg)
2999 refid = xstrdup (arg);
3003 int cmd_close(const char *arg)
3012 apdu = zget_APDU(out, Z_APDU_close);
3013 req = apdu->u.close;
3014 *req->closeReason = Z_Close_finished;
3016 printf("Sent close request.\n");
3021 int cmd_packagename(const char* arg)
3023 xfree (esPackageName);
3024 esPackageName = NULL;
3026 esPackageName = xstrdup(arg);
3030 int cmd_proxy(const char* arg)
3035 yazProxy = xstrdup (arg);
3039 int cmd_marccharset(const char *arg)
3044 if (sscanf(arg, "%29s", l1) < 1)
3046 printf("MARC character set is `%s'\n",
3047 marcCharset ? marcCharset: "none");
3050 xfree (marcCharset);
3052 if (strcmp(l1, "-"))
3053 marcCharset = xstrdup(l1);
3057 int cmd_displaycharset(const char *arg)
3062 if (sscanf(arg, "%29s", l1) < 1)
3064 printf("Display character set is `%s'\n",
3065 outputCharset ? outputCharset: "none");
3069 xfree (outputCharset);
3071 if (!strcmp(l1, "auto") && codeset)
3075 printf ("Display character set: %s\n", codeset);
3076 outputCharset = xstrdup(codeset);
3079 printf ("No codeset found on this system\n");
3081 else if (strcmp(l1, "-") && strcmp(l1, "none"))
3082 outputCharset = xstrdup(l1);
3087 int cmd_negcharset(const char *arg)
3092 if (sscanf(arg, "%29s %d %d", l1, &negotiationCharsetRecords,
3093 &negotiationCharsetVersion) < 1)
3095 printf("Current negotiation character set is `%s'\n",
3096 negotiationCharset ? negotiationCharset: "none");
3097 printf("Records in charset %s\n", negotiationCharsetRecords ?
3099 printf("Charneg version %d\n", negotiationCharsetVersion);
3103 xfree (negotiationCharset);
3104 negotiationCharset = NULL;
3105 if (*l1 && strcmp(l1, "-") && strcmp(l1, "none"))
3107 negotiationCharset = xstrdup(l1);
3108 printf ("Character set negotiation : %s\n", negotiationCharset);
3114 int cmd_charset(const char* arg)
3116 char l1[30], l2[30], l3[30];
3118 *l1 = *l2 = *l3 = 0;
3119 if (sscanf(arg, "%29s %29s %29s", l1, l2, l3) < 1)
3122 cmd_displaycharset("");
3123 cmd_marccharset("");
3129 cmd_displaycharset(l2);
3131 cmd_marccharset(l3);
3136 int cmd_lang(const char* arg)
3139 printf("Current language is `%s'\n", (yazLang)?yazLang:NULL);
3145 yazLang = xstrdup(arg);
3149 int cmd_source(const char* arg, int echo )
3151 /* first should open the file and read one line at a time.. */
3153 char line[102400], *cp;
3156 fprintf(stderr,"Error in source command use a filename\n");
3160 includeFile = fopen (arg, "r");
3163 fprintf(stderr,"Unable to open file %s for reading\n",arg);
3167 while(!feof(includeFile)) {
3168 memset(line,0,sizeof(line));
3169 fgets(line,sizeof(line),includeFile);
3171 if(strlen(line) < 2) continue;
3172 if(line[0] == '#') continue;
3174 if ((cp = strrchr (line, '\n')))
3178 printf( "processing line: %s\n",line );
3180 process_cmd_line(line);
3183 if(fclose(includeFile)<0) {
3184 perror("unable to close include file");
3190 int cmd_source_echo(const char* arg)
3196 int cmd_source_noecho(const char* arg)
3203 int cmd_subshell(const char* args)
3208 system(getenv("SHELL"));
3214 int cmd_set_berfile(const char *arg)
3216 if (ber_file && ber_file != stdout && ber_file != stderr)
3218 if (!strcmp(arg, ""))
3220 else if (!strcmp(arg, "-"))
3223 ber_file = fopen(arg, "a");
3227 int cmd_set_apdufile(const char *arg)
3229 if(apdu_file && apdu_file != stderr && apdu_file != stderr)
3231 if (!strcmp(arg, ""))
3233 else if (!strcmp(arg, "-"))
3237 apdu_file = fopen(arg, "a");
3239 perror("unable to open apdu log file");
3242 odr_setprint(print, apdu_file);
3246 int cmd_set_cclfile(const char* arg)
3250 bibset = ccl_qual_mk ();
3251 inf = fopen (arg, "r");
3253 perror("unable to open CCL file");
3256 ccl_qual_file (bibset, inf);
3259 strcpy(ccl_fields,arg);
3263 int cmd_set_cqlfile(const char* arg)
3265 cql_transform_t newcqltrans;
3267 if ((newcqltrans = cql_transform_open_fname(arg)) == 0) {
3268 perror("unable to open CQL file");
3272 cql_transform_close(cqltrans);
3274 cqltrans = newcqltrans;
3275 strcpy(cql_fields, arg);
3279 int cmd_set_auto_reconnect(const char* arg)
3281 if(strlen(arg)==0) {
3282 auto_reconnect = ! auto_reconnect;
3283 } else if(strcmp(arg,"on")==0) {
3285 } else if(strcmp(arg,"off")==0) {
3288 printf("Error use on or off\n");
3293 printf("Set auto reconnect enabled.\n");
3295 printf("Set auto reconnect disabled.\n");
3300 int cmd_set_marcdump(const char* arg)
3302 if(marc_file && marc_file != stderr) { /* don't close stdout*/
3306 if (!strcmp(arg, ""))
3308 else if (!strcmp(arg, "-"))
3312 marc_file = fopen(arg, "a");
3314 perror("unable to open marc log file");
3320 this command takes 3 arge {name class oid}
3322 int cmd_register_oid(const char* args) {
3327 {"appctx",CLASS_APPCTX},
3328 {"absyn",CLASS_ABSYN},
3329 {"attset",CLASS_ATTSET},
3330 {"transyn",CLASS_TRANSYN},
3331 {"diagset",CLASS_DIAGSET},
3332 {"recsyn",CLASS_RECSYN},
3333 {"resform",CLASS_RESFORM},
3334 {"accform",CLASS_ACCFORM},
3335 {"extserv",CLASS_EXTSERV},
3336 {"userinfo",CLASS_USERINFO},
3337 {"elemspec",CLASS_ELEMSPEC},
3338 {"varset",CLASS_VARSET},
3339 {"schema",CLASS_SCHEMA},
3340 {"tagset",CLASS_TAGSET},
3341 {"general",CLASS_GENERAL},
3342 {0,(enum oid_class) 0}
3344 char oname_str[101], oclass_str[101], oid_str[101];
3347 oid_class oidclass = CLASS_GENERAL;
3348 int val = 0, oid[OID_SIZE];
3349 struct oident * new_oident=NULL;
3351 if (sscanf (args, "%100[^ ] %100[^ ] %100s",
3352 oname_str,oclass_str, oid_str) < 1) {
3353 printf("Error in register command \n");
3357 for (i = 0; oid_classes[i].className; i++) {
3358 if (!strcmp(oid_classes[i].className, oclass_str))
3360 oidclass=oid_classes[i].oclass;
3365 if(!(oid_classes[i].className)) {
3366 printf("Unknown oid class %s\n",oclass_str);
3374 while (isdigit (*(unsigned char *) name))
3376 val = val*10 + (*name - '0');
3389 new_oident = oid_addent (oid, PROTO_GENERAL, oidclass, oname_str,
3391 if(strcmp(new_oident->desc,oname_str))
3393 fprintf(stderr,"oid is already named as %s, registration failed\n",
3399 int cmd_push_command(const char* arg)
3401 #if HAVE_READLINE_HISTORY_H
3405 fprintf(stderr,"Not compiled with the readline/history module\n");
3410 void source_rcfile()
3412 /* Look for a $HOME/.yazclientrc and source it if it exists */
3413 struct stat statbuf;
3415 char* homedir=getenv("HOME");
3419 sprintf(buffer,"%s/.yazclientrc",homedir);
3421 if(stat(buffer,&statbuf)==0) {
3422 cmd_source(buffer, 0 );
3427 if(stat(".yazclientrc",&statbuf)==0) {
3428 cmd_source(".yazclientrc", 0 );
3433 static void initialize(void)
3438 if (!(out = odr_createmem(ODR_ENCODE)) ||
3439 !(in = odr_createmem(ODR_DECODE)) ||
3440 !(print = odr_createmem(ODR_PRINT)))
3442 fprintf(stderr, "failed to allocate ODR streams\n");
3447 setvbuf(stdout, 0, _IONBF, 0);
3449 odr_setprint(print, apdu_file);
3451 bibset = ccl_qual_mk ();
3452 inf = fopen (ccl_fields, "r");
3455 ccl_qual_file (bibset, inf);
3459 cqltrans = cql_transform_open_fname(cql_fields);
3460 /* If this fails, no problem: we detect cqltrans == 0 later */
3462 #if HAVE_READLINE_READLINE_H
3463 rl_attempted_completion_function = (CPPFunction*)readline_completer;
3467 for(i=0; i<maxOtherInfosSupported; ++i) {
3468 extraOtherInfos[i].oidval = -1;
3475 #if HAVE_GETTIMEOFDAY
3476 struct timeval tv_start, tv_end;
3480 static void handle_srw_record(Z_SRW_record *rec)
3482 if (rec->recordPosition)
3484 printf ("pos=%d", *rec->recordPosition);
3485 setno = *rec->recordPosition + 1;
3487 if (rec->recordSchema)
3488 printf (" schema=%s", rec->recordSchema);
3490 if (rec->recordData_buf && rec->recordData_len)
3492 fwrite(rec->recordData_buf, 1, rec->recordData_len, stdout);
3494 fwrite (rec->recordData_buf, 1, rec->recordData_len, marc_file);
3497 printf ("No data!");
3501 static void handle_srw_explain_response(Z_SRW_explainResponse *res)
3503 handle_srw_record(&res->record);
3506 static void handle_srw_response(Z_SRW_searchRetrieveResponse *res)
3510 printf ("Received SRW SearchRetrieve Response\n");
3512 for (i = 0; i<res->num_diagnostics; i++)
3514 if (res->diagnostics[i].uri)
3515 printf ("SRW diagnostic %s\n",
3516 res->diagnostics[i].uri);
3518 printf ("SRW diagnostic missing or could not be decoded\n");
3519 if (res->diagnostics[i].message)
3520 printf ("Message: %s\n", res->diagnostics[i].message);
3521 if (res->diagnostics[i].details)
3522 printf ("Details: %s\n", res->diagnostics[i].details);
3524 if (res->numberOfRecords)
3525 printf ("Number of hits: %d\n", *res->numberOfRecords);
3526 for (i = 0; i<res->num_records; i++)
3527 handle_srw_record(res->records + i);
3530 static void handle_srw_scan_term(Z_SRW_scanTerm *term)
3532 if (term->displayTerm)
3533 printf("%s:", term->displayTerm);
3534 else if (term->value)
3535 printf("%s:", term->value);
3537 printf("No value:");
3538 if (term->numberOfRecords)
3539 printf(" %d", *term->numberOfRecords);
3540 if (term->whereInList)
3541 printf(" %s", term->whereInList);
3542 if (term->value && term->displayTerm)
3543 printf(" %s", term->value);
3545 strcpy(last_scan_line, term->value);
3549 static void handle_srw_scan_response(Z_SRW_scanResponse *res)
3553 printf ("Received SRW Scan Response\n");
3555 for (i = 0; i<res->num_diagnostics; i++)
3557 if (res->diagnostics[i].uri)
3558 printf ("SRW diagnostic %s\n",
3559 res->diagnostics[i].uri);
3561 printf ("SRW diagnostic missing or could not be decoded\n");
3562 if (res->diagnostics[i].message)
3563 printf ("Message: %s\n", res->diagnostics[i].message);
3564 if (res->diagnostics[i].details)
3565 printf ("Details: %s\n", res->diagnostics[i].details);
3568 for (i = 0; i<res->num_terms; i++)
3569 handle_srw_scan_term(res->terms + i);
3572 static void http_response(Z_HTTP_Response *hres)
3575 const char *content_type = z_HTTP_header_lookup(hres->headers,
3577 const char *connection_head = z_HTTP_header_lookup(hres->headers,
3579 if (content_type && !yaz_strcmp_del("text/xml", content_type, "; "))
3581 Z_SOAP *soap_package = 0;
3582 ODR o = odr_createmem(ODR_DECODE);
3583 Z_SOAP_Handler soap_handlers[2] = {
3584 {"http://www.loc.gov/zing/srw/", 0,
3585 (Z_SOAP_fun) yaz_srw_codec},
3588 ret = z_soap_codec(o, &soap_package,
3589 &hres->content_buf, &hres->content_len,
3591 if (!ret && soap_package->which == Z_SOAP_generic &&
3592 soap_package->u.generic->no == 0)
3594 Z_SRW_PDU *sr = soap_package->u.generic->p;
3595 if (sr->which == Z_SRW_searchRetrieve_response)
3596 handle_srw_response(sr->u.response);
3597 else if (sr->which == Z_SRW_explain_response)
3598 handle_srw_explain_response(sr->u.explain_response);
3599 else if (sr->which == Z_SRW_scan_response)
3600 handle_srw_scan_response(sr->u.scan_response);
3604 else if (soap_package && (soap_package->which == Z_SOAP_fault
3605 || soap_package->which == Z_SOAP_error))
3607 printf ("HTTP Error Status=%d\n", hres->code);
3608 printf ("SOAP Fault code %s\n",
3609 soap_package->u.fault->fault_code);
3610 printf ("SOAP Fault string %s\n",
3611 soap_package->u.fault->fault_string);
3612 if (soap_package->u.fault->details)
3613 printf ("SOAP Details %s\n",
3614 soap_package->u.fault->details);
3622 if (hres->code != 200)
3624 printf ("HTTP Error Status=%d\n", hres->code);
3628 printf ("Decoding of SRW package failed\n");
3634 if (!strcmp(hres->version, "1.0"))
3636 /* HTTP 1.0: only if Keep-Alive we stay alive.. */
3637 if (!connection_head || strcmp(connection_head, "Keep-Alive"))
3642 /* HTTP 1.1: only if no close we stay alive .. */
3643 if (connection_head && !strcmp(connection_head, "close"))
3650 void wait_and_handle_response()
3652 int reconnect_ok = 1;
3655 int netbufferlen = 0;
3660 res = cs_get(conn, &netbuffer, &netbufferlen);
3661 if (reconnect_ok && res <= 0 && protocol == PROTO_HTTP)
3672 buf_out = odr_getbuf(out, &len_out, 0);
3674 do_hex_dump(buf_out, len_out);
3676 cs_put(conn, buf_out, len_out);
3684 printf("Target closed connection\n");
3689 odr_reset(in); /* release APDU from last round */
3691 do_hex_dump(netbuffer, res);
3692 odr_setbuf(in, netbuffer, res, 0);
3694 if (!z_GDU(in, &gdu, 0, 0))
3696 FILE *f = ber_file ? ber_file : stdout;
3697 odr_perror(in, "Decoding incoming APDU");
3698 fprintf(f, "[Near %d]\n", odr_offset(in));
3699 fprintf(f, "Packet dump:\n---------\n");
3700 odr_dumpBER(f, netbuffer, res);
3701 fprintf(f, "---------\n");
3704 z_GDU(print, &gdu, 0, 0);
3707 if (conn && cs_more(conn))
3712 odr_dumpBER(ber_file, netbuffer, res);
3713 if (apdu_file && !z_GDU(print, &gdu, 0, 0))
3715 odr_perror(print, "Failed to print incoming APDU");
3719 if (gdu->which == Z_GDU_Z3950)
3721 Z_APDU *apdu = gdu->u.z3950;
3724 case Z_APDU_initResponse:
3725 process_initResponse(apdu->u.initResponse);
3727 case Z_APDU_searchResponse:
3728 process_searchResponse(apdu->u.searchResponse);
3730 case Z_APDU_scanResponse:
3731 process_scanResponse(apdu->u.scanResponse);
3733 case Z_APDU_presentResponse:
3734 print_refid (apdu->u.presentResponse->referenceId);
3736 *apdu->u.presentResponse->numberOfRecordsReturned;
3737 if (apdu->u.presentResponse->records)
3738 display_records(apdu->u.presentResponse->records);
3740 printf("No records.\n");
3741 printf ("nextResultSetPosition = %d\n",
3742 *apdu->u.presentResponse->nextResultSetPosition);
3744 case Z_APDU_sortResponse:
3745 process_sortResponse(apdu->u.sortResponse);
3747 case Z_APDU_extendedServicesResponse:
3748 printf("Got extended services response\n");
3749 process_ESResponse(apdu->u.extendedServicesResponse);
3752 printf("Target has closed the association.\n");
3753 process_close(apdu->u.close);
3755 case Z_APDU_resourceControlRequest:
3756 process_resourceControlRequest
3757 (apdu->u.resourceControlRequest);
3759 case Z_APDU_deleteResultSetResponse:
3760 process_deleteResultSetResponse(apdu->u.
3761 deleteResultSetResponse);
3764 printf("Received unknown APDU type (%d).\n",
3770 else if (gdu->which == Z_GDU_HTTP_Response)
3772 http_response(gdu->u.HTTP_Response);
3775 if (conn && !cs_more(conn))
3780 #if HAVE_GETTIMEOFDAY
3781 gettimeofday (&tv_end, 0);
3783 printf ("S/U S/U=%ld/%ld %ld/%ld",
3784 (long) tv_start.tv_sec,
3785 (long) tv_start.tv_usec,
3786 (long) tv_end.tv_sec,
3787 (long) tv_end.tv_usec);
3789 printf ("Elapsed: %.6f\n",
3790 (double) tv_end.tv_usec / 1e6 + tv_end.tv_sec -
3791 ((double) tv_start.tv_usec / 1e6 + tv_start.tv_sec));
3798 int cmd_cclparse(const char* arg)
3801 struct ccl_rpn_node *rpn=NULL;
3804 rpn = ccl_find_str (bibset, arg, &error, &pos);
3807 printf ("%*s^ - ", 3+strlen(last_cmd)+1+pos, " ");
3808 printf ("%s\n", ccl_err_msg (error));
3814 ccl_pr_tree(rpn, stdout);
3818 ccl_rpn_delete(rpn);
3826 int cmd_set_otherinfo(const char* args)
3828 char oidstr[101], otherinfoString[101];
3833 sscan_res = sscanf (args, "%d %100[^ ] %100s", &otherinfoNo, oidstr, otherinfoString);
3835 /* reset this otherinfo */
3836 if(otherinfoNo>=maxOtherInfosSupported) {
3837 printf("Error otherinfo index to large (%d>%d)\n",
3838 otherinfoNo,maxOtherInfosSupported);
3840 extraOtherInfos[otherinfoNo].oidval = -1;
3841 if (extraOtherInfos[otherinfoNo].value)
3842 free(extraOtherInfos[otherinfoNo].value);
3846 printf("Error in set_otherinfo command \n");
3850 if (otherinfoNo>=maxOtherInfosSupported) {
3851 printf("Error otherinfo index to large (%d>%d)\n",
3852 otherinfoNo,maxOtherInfosSupported);
3855 oidval = oid_getvalbyname (oidstr);
3856 if (oidval == -1 ) {
3857 printf("Error in set_otherinfo command unknown oid %s \n",oidstr);
3860 extraOtherInfos[otherinfoNo].oidval = oidval;
3861 if(extraOtherInfos[otherinfoNo].value) free(extraOtherInfos[otherinfoNo].value);
3862 extraOtherInfos[otherinfoNo].value = strdup(otherinfoString);
3867 int cmd_sleep(const char* args )
3876 printf("Done sleeping %d seconds\n", sec);
3881 int cmd_list_otherinfo(const char* args)
3885 if(strlen(args)>0) {
3887 if( i >= maxOtherInfosSupported ) {
3888 printf("Error otherinfo index to large (%d>%d)\n",i,maxOtherInfosSupported);
3892 if(extraOtherInfos[i].oidval != -1)
3893 printf(" otherinfo %d %s %s\n",
3895 yaz_z3950_oid_value_to_str(
3896 (enum oid_value) extraOtherInfos[i].oidval,
3898 extraOtherInfos[i].value);
3901 for(i=0; i<maxOtherInfosSupported; ++i) {
3902 if(extraOtherInfos[i].oidval != -1)
3903 printf(" otherinfo %d %s %s\n",
3905 yaz_z3950_oid_value_to_str(
3906 (enum oid_value) extraOtherInfos[i].oidval,
3908 extraOtherInfos[i].value);
3916 int cmd_list_all(const char* args) {
3919 /* connection options */
3921 printf("Connected to : %s\n",last_open_command);
3923 if(last_open_command)
3924 printf("Not connected to : %s\n",last_open_command);
3926 printf("Not connected : \n");
3929 if(yazProxy) printf("using proxy : %s\n",yazProxy);
3931 printf("auto_reconnect : %s\n",auto_reconnect?"on":"off");
3934 printf("Authentication : none\n");
3936 switch(auth->which) {
3937 case Z_IdAuthentication_idPass:
3938 printf("Authentication : IdPass\n");
3939 printf(" Login User : %s\n",auth->u.idPass->userId?auth->u.idPass->userId:"");
3940 printf(" Login Group : %s\n",auth->u.idPass->groupId?auth->u.idPass->groupId:"");
3941 printf(" Password : %s\n",auth->u.idPass->password?auth->u.idPass->password:"");
3943 case Z_IdAuthentication_open:
3944 printf("Authentication : psOpen\n");
3945 printf(" Open string : %s\n",auth->u.open);
3948 printf("Authentication : Unknown\n");
3951 if (negotiationCharset)
3952 printf("Neg. Character set : `%s'\n", negotiationCharset);
3956 for (i = 0; i<num_databaseNames; i++) printf("%s ",databaseNames[i]);
3960 printf("CCL file : %s\n",ccl_fields);
3961 printf("CQL file : %s\n",cql_fields);
3962 printf("Query type : %s\n",query_type_as_string(queryType));
3964 printf("Named Result Sets : %s\n",setnumber==-1?"off":"on");
3966 /* piggy back options */
3967 printf("ssub/lslb/mspn : %d/%d/%d\n",smallSetUpperBound,largeSetLowerBound,mediumSetPresentNumber);
3969 /* print present related options */
3970 printf("Format : %s\n",yaz_z3950_oid_value_to_str(recordsyntax,CLASS_RECSYN));
3971 printf("Schema : %s\n",record_schema ? record_schema : "not set");
3972 printf("Elements : %s\n",elementSetNames?elementSetNames->u.generic:"");
3974 /* loging options */
3975 printf("APDU log : %s\n",apdu_file?"on":"off");
3976 printf("Record log : %s\n",marc_file?"on":"off");
3979 printf("Other Info: \n");
3980 cmd_list_otherinfo("");
3985 int cmd_clear_otherinfo(const char* args)
3987 if(strlen(args)>0) {
3989 otherinfoNo = atoi(args);
3990 if( otherinfoNo >= maxOtherInfosSupported ) {
3991 printf("Error otherinfo index to large (%d>%d)\n",otherinfoNo,maxOtherInfosSupported);
3995 if(extraOtherInfos[otherinfoNo].oidval != -1) {
3996 /* only clear if set. */
3997 extraOtherInfos[otherinfoNo].oidval=-1;
3998 free(extraOtherInfos[otherinfoNo].value);
4003 for(i=0; i<maxOtherInfosSupported; ++i) {
4004 if (extraOtherInfos[i].oidval!=-1 ) {
4005 extraOtherInfos[i].oidval=-1;
4006 free(extraOtherInfos[i].value);
4013 static int cmd_help (const char *line);
4015 typedef char *(*completerFunctionType)(const char *text, int state);
4019 int (*fun)(const char *arg);
4021 completerFunctionType rl_completerfunction;
4022 int complete_filenames;
4023 char **local_tabcompletes;
4025 {"open", cmd_open, "('tcp'|'ssl')':<host>[':'<port>][/<db>]",NULL,0,NULL},
4026 {"quit", cmd_quit, "",NULL,0,NULL},
4027 {"find", cmd_find, "<query>",NULL,0,NULL},
4028 {"delete", cmd_delete, "<setname>",NULL,0,NULL},
4029 {"base", cmd_base, "<base-name>",NULL,0,NULL},
4030 {"show", cmd_show, "<rec#>['+'<#recs>['+'<setname>]]",NULL,0,NULL},
4031 {"scan", cmd_scan, "<term>",NULL,0,NULL},
4032 {"scanstep", cmd_scanstep, "<size>",NULL,0,NULL},
4033 {"scanpos", cmd_scanpos, "<size>",NULL,0,NULL},
4034 {"sort", cmd_sort, "<sortkey> <flag> <sortkey> <flag> ...",NULL,0,NULL},
4035 {"sort+", cmd_sort_newset, "<sortkey> <flag> <sortkey> <flag> ...",NULL,0,NULL},
4036 {"authentication", cmd_authentication, "<acctstring>",NULL,0,NULL},
4037 {"lslb", cmd_lslb, "<largeSetLowerBound>",NULL,0,NULL},
4038 {"ssub", cmd_ssub, "<smallSetUpperBound>",NULL,0,NULL},
4039 {"mspn", cmd_mspn, "<mediumSetPresentNumber>",NULL,0,NULL},
4040 {"status", cmd_status, "",NULL,0,NULL},
4041 {"setnames", cmd_setnames, "",NULL,0,NULL},
4042 {"cancel", cmd_cancel, "",NULL,0,NULL},
4043 {"cancel_find", cmd_cancel_find, "<query>",NULL,0,NULL},
4044 {"format", cmd_format, "<recordsyntax>",complete_format,0,NULL},
4045 {"schema", cmd_schema, "<schema>",complete_schema,0,NULL},
4046 {"elements", cmd_elements, "<elementSetName>",NULL,0,NULL},
4047 {"close", cmd_close, "",NULL,0,NULL},
4048 {"attributeset", cmd_attributeset, "<attrset>",complete_attributeset,0,NULL},
4049 {"querytype", cmd_querytype, "<type>",complete_querytype,0,NULL},
4050 {"refid", cmd_refid, "<id>",NULL,0,NULL},
4051 {"itemorder", cmd_itemorder, "ill|item <itemno>",NULL,0,NULL},
4052 {"update", cmd_update, "<action> <recid> [<file>]",NULL,0,NULL},
4053 {"update0", cmd_update0, "<action> <recid> [<file>]",NULL,0,NULL},
4054 {"packagename", cmd_packagename, "<packagename>",NULL,0,NULL},
4055 {"proxy", cmd_proxy, "[('tcp'|'ssl')]<host>[':'<port>]",NULL,0,NULL},
4056 {"charset", cmd_charset, "<nego_charset> <output_charset>",NULL,0,NULL},
4057 {"negcharset", cmd_negcharset, "<nego_charset>",NULL,0,NULL},
4058 {"displaycharset", cmd_displaycharset, "<output_charset>",NULL,0,NULL},
4059 {"marccharset", cmd_marccharset, "<charset_name>",NULL,0,NULL},
4060 {"lang", cmd_lang, "<language_code>",NULL,0,NULL},
4061 {".", cmd_source_echo, "<filename>",NULL,1,NULL},
4062 {"!", cmd_subshell, "Subshell command",NULL,1,NULL},
4063 {"set_apdufile", cmd_set_apdufile, "<filename>",NULL,1,NULL},
4064 {"set_berfile", cmd_set_berfile, "<filename>",NULL,1,NULL},
4065 {"set_marcdump", cmd_set_marcdump," <filename>",NULL,1,NULL},
4066 {"set_cclfile", cmd_set_cclfile," <filename>",NULL,1,NULL},
4067 {"set_cqlfile", cmd_set_cqlfile," <filename>",NULL,1,NULL},
4068 {"set_auto_reconnect", cmd_set_auto_reconnect," on|off",complete_auto_reconnect,1,NULL},
4069 {"set_otherinfo", cmd_set_otherinfo,"<otherinfoinddex> <oid> <string>",NULL,0,NULL},
4070 {"sleep", cmd_sleep,"<seconds>",NULL,0,NULL},
4071 {"register_oid", cmd_register_oid,"<name> <class> <oid>",NULL,0,NULL},
4072 {"push_command", cmd_push_command,"<command>",command_generator,0,NULL},
4073 {"register_tab", cmd_register_tab,"<commandname> <tab>",command_generator,0,NULL},
4074 {"cclparse", cmd_cclparse,"<ccl find command>",NULL,0,NULL},
4075 {"list_otherinfo",cmd_list_otherinfo,"[otherinfoinddex]",NULL,0,NULL},
4076 {"list_all",cmd_list_all,"",NULL,0,NULL},
4077 {"clear_otherinfo",cmd_clear_otherinfo,"",NULL,0,NULL},
4078 /* Server Admin Functions */
4079 {"adm-reindex", cmd_adm_reindex, "<database-name>",NULL,0,NULL},
4080 {"adm-truncate", cmd_adm_truncate, "('database'|'index')<object-name>",NULL,0,NULL},
4081 {"adm-create", cmd_adm_create, "",NULL,0,NULL},
4082 {"adm-drop", cmd_adm_drop, "('database'|'index')<object-name>",NULL,0,NULL},
4083 {"adm-import", cmd_adm_import, "<record-type> <dir> <pattern>",NULL,0,NULL},
4084 {"adm-refresh", cmd_adm_refresh, "",NULL,0,NULL},
4085 {"adm-commit", cmd_adm_commit, "",NULL,0,NULL},
4086 {"adm-shutdown", cmd_adm_shutdown, "",NULL,0,NULL},
4087 {"adm-startup", cmd_adm_startup, "",NULL,0,NULL},
4088 {"explain", cmd_explain, "", NULL, 0, NULL},
4089 {"options", cmd_options, "", NULL, 0, NULL},
4090 {"zversion", cmd_zversion, "", NULL, 0, NULL},
4091 {"help", cmd_help, "", NULL,0,NULL},
4092 {"init", cmd_init, "", NULL,0,NULL},
4096 static int cmd_help (const char *line)
4102 sscanf (line, "%20s", topic);
4105 printf("Commands:\n");
4106 for (i = 0; cmd_array[i].cmd; i++)
4107 if (*topic == 0 || strcmp (topic, cmd_array[i].cmd) == 0)
4108 printf(" %s %s\n", cmd_array[i].cmd, cmd_array[i].ad);
4109 if (strcmp (topic, "find") == 0)
4112 printf (" \"term\" Simple Term\n");
4113 printf (" @attr [attset] type=value op Attribute\n");
4114 printf (" @and opl opr And\n");
4115 printf (" @or opl opr Or\n");
4116 printf (" @not opl opr And-Not\n");
4117 printf (" @set set Result set\n");
4119 printf ("Bib-1 attribute types\n");
4121 printf ("4=Title 7=ISBN 8=ISSN 30=Date 62=Abstract 1003=Author 1016=Any\n");
4122 printf ("2=Relation: ");
4123 printf ("1< 2<= 3= 4>= 5> 6!= 102=Relevance\n");
4124 printf ("3=Position: ");
4125 printf ("1=First in Field 2=First in subfield 3=Any position\n");
4126 printf ("4=Structure: ");
4127 printf ("1=Phrase 2=Word 3=Key 4=Year 5=Date 6=WordList\n");
4128 printf ("5=Truncation: ");
4129 printf ("1=Right 2=Left 3=L&R 100=No 101=# 102=Re-1 103=Re-2\n");
4130 printf ("6=Completeness:");
4131 printf ("1=Incomplete subfield 2=Complete subfield 3=Complete field\n");
4136 int cmd_register_tab(const char* arg) {
4138 char command[101], tabargument[101];
4143 if (sscanf (arg, "%100s %100s", command, tabargument) < 1) {
4147 /* locate the amdn in the list */
4148 for (i = 0; cmd_array[i].cmd; i++) {
4149 if (!strncmp(cmd_array[i].cmd, command, strlen(command))) {
4154 if(!cmd_array[i].cmd) {
4155 fprintf(stderr,"Unknown command %s\n",command);
4160 if(!cmd_array[i].local_tabcompletes)
4161 cmd_array[i].local_tabcompletes = (char **) calloc(1,sizeof(char**));
4165 tabslist = cmd_array[i].local_tabcompletes;
4166 for(;tabslist && *tabslist;tabslist++) {
4170 cmd_array[i].local_tabcompletes = (char **)
4171 realloc(cmd_array[i].local_tabcompletes,(num_of_tabs+2)*sizeof(char**));
4172 tabslist=cmd_array[i].local_tabcompletes;
4173 tabslist[num_of_tabs]=strdup(tabargument);
4174 tabslist[num_of_tabs+1]=NULL;
4179 void process_cmd_line(char* line)
4182 char word[32], arg[10240];
4184 #if HAVE_GETTIMEOFDAY
4185 gettimeofday (&tv_start, 0);
4188 if ((res = sscanf(line, "%31s %10239[^;]", word, arg)) <= 0)
4190 strcpy(word, last_cmd);
4195 strcpy(last_cmd, word);
4197 /* removed tailing spaces from the arg command */
4200 char* lastnonspace=NULL;
4203 if(!isspace(*(unsigned char *) p)) {
4208 *(++lastnonspace) = 0;
4211 for (i = 0; cmd_array[i].cmd; i++)
4212 if (!strncmp(cmd_array[i].cmd, word, strlen(word)))
4214 res = (*cmd_array[i].fun)(arg);
4218 if (!cmd_array[i].cmd) /* dump our help-screen */
4220 printf("Unknown command: %s.\n", word);
4221 printf("use help for list of commands\n");
4222 /* cmd_help (""); */
4226 if(apdu_file) fflush(apdu_file);
4229 wait_and_handle_response();
4238 char *command_generator(const char *text, int state)
4244 for( ; cmd_array[idx].cmd; ++idx) {
4245 if (!strncmp(cmd_array[idx].cmd,text,strlen(text))) {
4246 ++idx; /* skip this entry on the next run */
4247 return strdup(cmd_array[idx-1].cmd);
4255 This function only known how to complete on the first word
4257 char ** readline_completer(char *text, int start, int end) {
4258 #if HAVE_READLINE_READLINE_H
4260 completerFunctionType completerToUse;
4263 #if HAVE_READLINE_RL_COMPLETION_MATCHES
4264 char** res=rl_completion_matches(text,
4267 char** res=completion_matches(text,
4268 (CPFunction*)command_generator);
4270 rl_attempted_completion_over = 1;
4273 char arg[10240],word[32];
4275 if ((res = sscanf(rl_line_buffer, "%31s %10239[^;]", word, arg)) <= 0) {
4276 rl_attempted_completion_over = 1;
4280 for (i = 0; cmd_array[i].cmd; i++) {
4281 if (!strncmp(cmd_array[i].cmd, word, strlen(word))) {
4286 if(!cmd_array[i].cmd) return NULL;
4288 curret_global_list = cmd_array[i].local_tabcompletes;
4290 completerToUse = cmd_array[i].rl_completerfunction;
4291 if(completerToUse==NULL) /* if no pr. command completer is defined use the default completer */
4292 completerToUse = default_completer;
4294 if(completerToUse) {
4295 #ifdef HAVE_READLINE_RL_COMPLETION_MATCHES
4297 rl_completion_matches(text,
4301 completion_matches(text,
4302 (CPFunction*)completerToUse);
4304 if(!cmd_array[i].complete_filenames)
4305 rl_attempted_completion_over = 1;
4308 if(!cmd_array[i].complete_filenames)
4309 rl_attempted_completion_over = 1;
4319 static void client(void)
4325 #if HAVE_GETTIMEOFDAY
4326 gettimeofday (&tv_start, 0);
4331 char *line_in = NULL;
4332 #if HAVE_READLINE_READLINE_H
4335 line_in=readline(C_PROMPT);
4338 #if HAVE_READLINE_HISTORY_H
4340 add_history(line_in);
4342 strncpy(line, line_in, 10239);
4351 if (!fgets(line, 10239, stdin))
4353 if ((end_p = strchr (line, '\n')))
4356 process_cmd_line(line);
4360 static void show_version(void)
4364 yaz_version(vstr, 0);
4365 printf ("YAZ version: %s\n", YAZ_VERSION);
4366 if (strcmp(vstr, YAZ_VERSION))
4367 printf ("YAZ DLL/SO: %s\n", vstr);
4371 int main(int argc, char **argv)
4374 char *open_command = 0;
4375 char *auth_command = 0;
4380 if (!setlocale(LC_CTYPE, ""))
4381 fprintf (stderr, "setlocale failed\n");
4385 codeset = nl_langinfo(CODESET);
4389 outputCharset = xstrdup(codeset);
4391 ODR_MASK_SET(&z3950_options, Z_Options_search);
4392 ODR_MASK_SET(&z3950_options, Z_Options_present);
4393 ODR_MASK_SET(&z3950_options, Z_Options_namedResultSets);
4394 ODR_MASK_SET(&z3950_options, Z_Options_triggerResourceCtrl);
4395 ODR_MASK_SET(&z3950_options, Z_Options_scan);
4396 ODR_MASK_SET(&z3950_options, Z_Options_sort);
4397 ODR_MASK_SET(&z3950_options, Z_Options_extendedServices);
4398 ODR_MASK_SET(&z3950_options, Z_Options_delSet);
4400 while ((ret = options("k:c:q:a:b:m:v:p:u:t:Vxd:", argv, argc, &arg)) != -2)
4407 open_command = (char *) xmalloc (strlen(arg)+6);
4408 strcpy (open_command, "open ");
4409 strcat (open_command, arg);
4413 dump_file_prefix = arg;
4416 kilobytes = atoi(arg);
4419 if (!(marc_file = fopen (arg, "a")))
4426 outputCharset = xstrdup(arg);
4429 strncpy (ccl_fields, arg, sizeof(ccl_fields)-1);
4430 ccl_fields[sizeof(ccl_fields)-1] = '\0';
4433 strncpy (cql_fields, arg, sizeof(cql_fields)-1);
4434 cql_fields[sizeof(cql_fields)-1] = '\0';
4437 if (!strcmp(arg, "-"))
4440 ber_file=fopen(arg, "a");
4443 if (!strcmp(arg, "-"))
4446 apdu_file=fopen(arg, "a");
4452 yazProxy=strdup(arg);
4457 auth_command = (char *) xmalloc (strlen(arg)+6);
4458 strcpy (auth_command, "auth ");
4459 strcat (auth_command, arg);
4463 yaz_log_init(yaz_log_mask_str(arg), "", 0);
4469 fprintf (stderr, "Usage: %s [-m <marclog>] [ -a <apdulog>] "
4470 "[-b berdump] [-c cclfields] \n"
4471 "[-q cqlfields] [-p <proxy-addr>] [-u <auth>] "
4472 "[-k size] [-d dump] [-V] [<server-addr>]\n",
4480 #ifdef HAVE_GETTIMEOFDAY
4481 gettimeofday (&tv_start, 0);
4483 process_cmd_line (auth_command);
4484 #if HAVE_READLINE_HISTORY_H
4485 add_history(auth_command);
4487 xfree(auth_command);
4491 #ifdef HAVE_GETTIMEOFDAY
4492 gettimeofday (&tv_start, 0);
4494 process_cmd_line (open_command);
4495 #if HAVE_READLINE_HISTORY_H
4496 add_history(open_command);
4498 xfree(open_command);
4509 * vim600: sw=4 ts=8 fdm=marker
4510 * vim<600: sw=4 ts=8