2 * Copyright (C) 1995-2005, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: client.c,v 1.298 2005-09-26 09:15:27 adam Exp $
14 #include <sys/types.h>
32 #if HAVE_OPENSSL_SSL_H
33 #include <openssl/bio.h>
34 #include <openssl/crypto.h>
35 #include <openssl/x509.h>
36 #include <openssl/pem.h>
37 #include <openssl/ssl.h>
38 #include <openssl/err.h>
45 #define S_ISREG(x) (x & _S_IFREG)
46 #define S_ISDIR(x) (x & _S_IFDIR)
49 #include <yaz/yaz-util.h>
51 #include <yaz/comstack.h>
53 #include <yaz/proto.h>
54 #include <yaz/marcdisp.h>
55 #include <yaz/diagbib1.h>
56 #include <yaz/otherinfo.h>
57 #include <yaz/charneg.h>
59 #include <yaz/pquery.h>
60 #include <yaz/sortspec.h>
64 #include <yaz/yaz-ccl.h>
68 #if HAVE_READLINE_READLINE_H
69 #include <readline/readline.h>
71 #if HAVE_READLINE_HISTORY_H
72 #include <readline/history.h>
77 #include "tabcomplete.h"
79 #define C_PROMPT "Z> "
81 static char *codeset = 0; /* character set for output */
82 static int hex_dump = 0;
83 static char *dump_file_prefix = 0;
84 static ODR out, in, print; /* encoding and decoding streams */
86 static ODR srw_sr_odr_out = 0;
87 static Z_SRW_PDU *srw_sr = 0;
89 static FILE *apdu_file = 0;
90 static FILE *ber_file = 0;
91 static COMSTACK conn = 0; /* our z-association */
92 static Z_IdAuthentication *auth = 0; /* our current auth definition */
93 char *databaseNames[128];
94 int num_databaseNames = 0;
95 static Z_External *record_last = 0;
96 static int setnumber = -1; /* current result set number */
97 static int smallSetUpperBound = 0;
98 static int largeSetLowerBound = 1;
99 static int mediumSetPresentNumber = 0;
100 static Z_ElementSetNames *elementSetNames = 0;
101 static int setno = 1; /* current set offset */
102 static enum oid_proto protocol = PROTO_Z3950; /* current app protocol */
103 #define RECORDSYNTAX_MAX 20
104 static enum oid_value recordsyntax_list[RECORDSYNTAX_MAX] = { VAL_USMARC };
105 static int recordsyntax_size = 1;
107 static char *record_schema = 0;
108 static int sent_close = 0;
109 static NMEM session_mem = NULL; /* memory handle for init-response */
110 static Z_InitResponse *session_initResponse = 0; /* session parameters */
111 static char last_scan_line[512] = "0";
112 static char last_scan_query[512] = "0";
113 static char ccl_fields[512] = "default.bib";
114 /* ### How can I set this path to use wherever YAZ is installed? */
115 static char cql_fields[512] = "/usr/local/share/yaz/etc/pqf.properties";
116 static char *esPackageName = 0;
117 static char *yazProxy = 0;
118 static int kilobytes = 1024;
119 static char *negotiationCharset = 0;
120 static int negotiationCharsetRecords = 1;
121 static int negotiationCharsetVersion = 3;
122 static char *outputCharset = 0;
123 static char *marcCharset = 0;
124 static char* yazLang = 0;
125 static char* http_version = "1.1";
127 static char last_cmd[32] = "?";
128 static FILE *marc_file = 0;
129 static char *refid = NULL;
130 static char *last_open_command = NULL;
131 static int auto_reconnect = 0;
132 static int auto_wait = 1;
133 static Odr_bitmask z3950_options;
134 static int z3950_version = 3;
135 static int scan_stepSize = 0;
136 static int scan_position = 1;
137 static int scan_size = 20;
138 static char cur_host[200];
139 static int last_hit_count = 0;
149 static QueryType queryType = QueryType_Prefix;
151 static CCL_bibset bibset; /* CCL bibset handle */
152 static cql_transform_t cqltrans; /* CQL context-set handle */
154 #if HAVE_READLINE_COMPLETION_OVER
157 /* readline doesn't have this var. Define it ourselves. */
158 int rl_attempted_completion_over = 0;
161 /* set this one to 1, to avoid decode of unknown MARCs */
162 #define AVOID_MARC_DECODE 1
164 #define maxOtherInfosSupported 10
168 } extraOtherInfos[maxOtherInfosSupported];
171 void process_cmd_line(char* line);
172 char ** readline_completer(char *text, int start, int end);
173 char *command_generator(const char *text, int state);
174 char** curret_global_list=NULL;
175 int cmd_register_tab(const char* arg);
177 static void close_session (void);
179 ODR getODROutputStream()
184 const char* query_type_as_string(QueryType q)
187 case QueryType_Prefix: return "prefix (RPN sent to server)";
188 case QueryType_CCL: return "CCL (CCL sent to server) ";
189 case QueryType_CCL2RPN: return "CCL -> RPN (RPN sent to server)";
190 case QueryType_CQL: return "CQL (CQL sent to server)";
191 case QueryType_CQL2RPN: return "CQL -> RPN (RPN sent to server)";
193 return "unknown Query type internal yaz-client error";
197 static void do_hex_dump(const char* buf, int len)
202 for( i=0; i<len ; i=i+16 )
205 for(x=0 ; i+x<len && x<16; ++x)
207 printf("%2.2X ",(unsigned int)((unsigned char)buf[i+x]));
212 if (dump_file_prefix)
215 if (++no < 1000 && strlen(dump_file_prefix) < 500)
219 sprintf (fname, "%s.%03d.raw", dump_file_prefix, no);
220 of = fopen(fname, "wb");
222 fwrite (buf, 1, len, of);
229 void add_otherInfos(Z_APDU *a)
231 Z_OtherInformation **oi;
235 for(i=0; i<maxOtherInfosSupported; ++i)
237 if(extraOtherInfos[i].oidval != -1)
238 yaz_oi_set_string_oidval(oi, out, extraOtherInfos[i].oidval,
239 1, extraOtherInfos[i].value);
243 int send_apdu(Z_APDU *a)
252 z_APDU(print, &a, 0, 0);
255 if (!z_APDU(out, &a, 0, 0))
257 odr_perror(out, "Encoding APDU");
261 buf = odr_getbuf(out, &len, 0);
263 odr_dumpBER(ber_file, buf, len);
264 /* printf ("sending APDU of size %d\n", len); */
265 do_hex_dump(buf, len);
266 if (cs_put(conn, buf, len) < 0)
268 fprintf(stderr, "cs_put: %s", cs_errmsg(cs_errno(conn)));
272 odr_reset(out); /* release the APDU structure */
276 static void print_stringn(const unsigned char *buf, size_t len)
279 for (i = 0; i<len; i++)
280 if ((buf[i] <= 126 && buf[i] >= 32) || strchr ("\n\r\t\f", buf[i]))
281 printf ("%c", buf[i]);
283 printf ("\\X%02X", buf[i]);
286 static void print_refid (Z_ReferenceId *id)
290 printf ("Reference Id: ");
291 print_stringn (id->buf, id->len);
296 static Z_ReferenceId *set_refid (ODR out)
301 id = (Z_ReferenceId *) odr_malloc (out, sizeof(*id));
302 id->size = id->len = strlen(refid);
303 id->buf = (unsigned char *) odr_malloc (out, id->len);
304 memcpy (id->buf, refid, id->len);
308 /* INIT SERVICE ------------------------------- */
310 static void send_initRequest(const char* type_and_host)
312 Z_APDU *apdu = zget_APDU(out, Z_APDU_initRequest);
313 Z_InitRequest *req = apdu->u.initRequest;
316 req->options = &z3950_options;
318 ODR_MASK_ZERO(req->protocolVersion);
319 for (i = 0; i<z3950_version; i++)
320 ODR_MASK_SET(req->protocolVersion, i);
322 *req->maximumRecordSize = 1024*kilobytes;
323 *req->preferredMessageSize = 1024*kilobytes;
325 req->idAuthentication = auth;
327 req->referenceId = set_refid (out);
329 if (yazProxy && type_and_host)
330 yaz_oi_set_string_oidval(&req->otherInfo, out, VAL_PROXY,
333 if (negotiationCharset || yazLang) {
334 Z_OtherInformation **p;
335 Z_OtherInformationUnit *p0;
337 yaz_oi_APDU(apdu, &p);
339 if ((p0=yaz_oi_update(p, out, NULL, 0, 0))) {
340 ODR_MASK_SET(req->options, Z_Options_negotiationModel);
342 p0->which = Z_OtherInfo_externallyDefinedInfo;
343 p0->information.externallyDefinedInfo =
344 yaz_set_proposal_charneg(
346 (const char**)&negotiationCharset,
347 negotiationCharset ? 1 : 0,
348 (const char**)&yazLang, yazLang ? 1 : 0,
349 negotiationCharsetRecords);
354 printf("Sent initrequest.\n");
358 /* These two are used only from process_initResponse() */
359 static void render_initUserInfo(Z_OtherInformation *ui1);
360 static void render_diag(Z_DiagnosticFormat *diag);
362 static void pr_opt(const char *opt, void *clientData)
367 static int process_initResponse(Z_InitResponse *res)
370 /* save session parameters for later use */
371 session_mem = odr_extract_mem(in);
372 session_initResponse = res;
374 for (ver = 0; ver < 8; ver++)
375 if (!ODR_MASK_GET(res->protocolVersion, ver))
379 printf("Connection rejected by v%d target.\n", ver);
381 printf("Connection accepted by v%d target.\n", ver);
382 if (res->implementationId)
383 printf("ID : %s\n", res->implementationId);
384 if (res->implementationName)
385 printf("Name : %s\n", res->implementationName);
386 if (res->implementationVersion)
387 printf("Version: %s\n", res->implementationVersion);
388 if (res->userInformationField)
390 Z_External *uif = res->userInformationField;
391 if (uif->which == Z_External_userInfo1) {
392 render_initUserInfo(uif->u.userInfo1);
394 printf("UserInformationfield:\n");
395 if (!z_External(print, (Z_External**)&uif, 0, 0))
397 odr_perror(print, "Printing userinfo\n");
400 if (uif->which == Z_External_octet) {
401 printf("Guessing visiblestring:\n");
402 printf("'%.*s'\n", uif->u.octet_aligned->len,
403 uif->u.octet_aligned->buf);
405 else if (uif->which == Z_External_single)
407 Odr_any *sat = uif->u.single_ASN1_type;
408 oident *oid = oid_getentbyoid(uif->direct_reference);
409 if (oid->value == VAL_OCLCUI) {
410 Z_OCLC_UserInformation *oclc_ui;
411 ODR decode = odr_createmem(ODR_DECODE);
412 odr_setbuf(decode, (char *) sat->buf, sat->len, 0);
413 if (!z_OCLC_UserInformation(decode, &oclc_ui, 0, 0))
414 printf ("Bad OCLC UserInformation:\n");
416 printf ("OCLC UserInformation:\n");
417 if (!z_OCLC_UserInformation(print, &oclc_ui, 0, 0))
418 printf ("Bad OCLC UserInformation spec\n");
423 /* Peek at any private Init-diagnostic APDUs */
424 printf("### NAUGHTY: External is '%.*s'\n",
432 yaz_init_opt_decode(res->options, pr_opt, 0);
435 if (ODR_MASK_GET(res->options, Z_Options_namedResultSets))
438 if (ODR_MASK_GET(res->options, Z_Options_negotiationModel)) {
440 Z_CharSetandLanguageNegotiation *p =
441 yaz_get_charneg_record(res->otherInfo);
445 char *charset=NULL, *lang=NULL;
448 yaz_get_response_charneg(session_mem, p, &charset, &lang,
451 if (outputCharset && negotiationCharset) {
452 odr_set_charset (out, charset, outputCharset);
453 odr_set_charset (in, outputCharset, charset);
456 odr_set_charset (out, 0, 0);
457 odr_set_charset (in, 0, 0);
460 printf("Accepted character set : %s\n", charset);
461 printf("Accepted code language : %s\n", lang ? lang : "none");
462 printf("Accepted records in ...: %d\n", selected );
470 static void render_initUserInfo(Z_OtherInformation *ui1) {
472 printf("Init response contains %d otherInfo unit%s:\n",
473 ui1->num_elements, ui1->num_elements == 1 ? "" : "s");
475 for (i = 0; i < ui1->num_elements; i++) {
476 Z_OtherInformationUnit *unit = ui1->list[i];
477 printf(" %d: otherInfo unit contains ", i+1);
478 if (unit->which == Z_OtherInfo_externallyDefinedInfo &&
479 unit->information.externallyDefinedInfo &&
480 unit->information.externallyDefinedInfo->which ==
482 render_diag(unit->information.externallyDefinedInfo->u.diag1);
484 else if (unit->which != Z_OtherInfo_externallyDefinedInfo)
486 printf("unsupported otherInfo unit->which = %d\n", unit->which);
490 printf("unsupported otherInfo unit external %d\n",
491 unit->information.externallyDefinedInfo ?
492 unit->information.externallyDefinedInfo->which : -2);
498 /* ### should this share code with display_diagrecs()? */
499 static void render_diag(Z_DiagnosticFormat *diag) {
502 printf("%d diagnostic%s:\n", diag->num, diag->num == 1 ? "" : "s");
503 for (i = 0; i < diag->num; i++) {
504 Z_DiagnosticFormat_s *ds = diag->elements[i];
505 printf(" %d: ", i+1);
507 case Z_DiagnosticFormat_s_defaultDiagRec: {
508 Z_DefaultDiagFormat *dd = ds->u.defaultDiagRec;
509 /* ### should check `dd->diagnosticSetId' */
510 printf("code=%d (%s)", *dd->condition,
511 diagbib1_str(*dd->condition));
512 /* Both types of addinfo are the same, so use type-pun */
513 if (dd->u.v2Addinfo != 0)
514 printf(",\n\taddinfo='%s'", dd->u.v2Addinfo);
517 case Z_DiagnosticFormat_s_explicitDiagnostic:
518 printf("Explicit diagnostic (not supported)");
521 printf("Unrecognised diagnostic type %d", ds->which);
525 if (ds->message != 0)
526 printf(", message='%s'", ds->message);
532 static int set_base(const char *arg)
537 for (i = 0; i<num_databaseNames; i++)
538 xfree (databaseNames[i]);
539 num_databaseNames = 0;
543 if (!(cp = strchr(arg, ' ')))
544 cp = arg + strlen(arg);
547 databaseNames[num_databaseNames] = (char *)xmalloc (1 + cp - arg);
548 memcpy (databaseNames[num_databaseNames], arg, cp - arg);
549 databaseNames[num_databaseNames][cp - arg] = '\0';
551 for (cp1 = databaseNames[num_databaseNames]; *cp1 ; cp1++)
560 if (num_databaseNames == 0)
562 num_databaseNames = 1;
563 databaseNames[0] = xstrdup("");
568 static int parse_cmd_doc(const char **arg, ODR out, char **buf,
572 while (**arg && strchr(" \t\n\r\f", **arg))
574 if ((*arg)[0] == '\"' && (sep=strchr(*arg+1, '"')))
578 *buf = odr_strdupn(out, *arg, *len);
582 else if ((*arg)[0] && (*arg)[0] != '\"')
586 const char *fname = *arg;
588 while (**arg != '\0' && **arg != ' ')
591 inf = fopen(fname, "rb");
594 printf("Couldn't open %s\n", fname);
597 if (fseek(inf, 0L, SEEK_END) == -1)
599 printf("Couldn't seek in %s\n", fname);
604 if (fseek(inf, 0L, SEEK_SET) == -1)
606 printf("Couldn't seek in %s\n", fname);
611 *buf = odr_malloc(out, fsize);
612 if (fread(*buf, 1, fsize, inf) != fsize)
614 printf("Unable to read %s\n", fname);
621 else if (**arg == '\0')
629 printf("Missing doc argument\n");
632 printf("Bad doc argument %s\n", *arg);
636 static int cmd_base(const char *arg)
640 printf("Usage: base <database> <database> ...\n");
643 return set_base(arg);
646 void cmd_open_remember_last_open_command(const char* arg, char* new_open_command)
648 if(last_open_command != arg)
650 if(last_open_command) xfree(last_open_command);
651 last_open_command = xstrdup(new_open_command);
655 int session_connect(const char *arg)
658 char type_and_host[101];
659 const char *basep = 0;
660 #if HAVE_OPENSSL_SSL_H
670 nmem_destroy (session_mem);
672 session_initResponse = 0;
674 cs_get_host_args(arg, &basep);
676 strncpy(type_and_host, arg, sizeof(type_and_host)-1);
677 type_and_host[sizeof(type_and_host)-1] = '\0';
679 cmd_open_remember_last_open_command(arg, type_and_host);
682 conn = cs_create_host(yazProxy, 1, &add);
684 conn = cs_create_host(arg, 1, &add);
687 printf ("Couldn't create comstack\n");
691 if (conn->protocol == PROTO_HTTP)
692 queryType = QueryType_CQL;
694 if (conn->protocol == PROTO_HTTP)
696 printf ("SRW/HTTP not enabled in this YAZ\n");
702 protocol = conn->protocol;
703 if (conn->protocol == PROTO_HTTP)
707 printf("Connecting...");
709 if (cs_connect(conn, add) < 0)
711 printf ("error = %s\n", cs_strerror(conn));
712 if (conn->cerrno == CSYSERR)
715 yaz_strerror(msg, sizeof(msg));
716 printf ("%s\n", msg);
723 #if HAVE_OPENSSL_SSL_H
724 if ((ssl = (SSL *) cs_get_ssl(conn)))
726 X509 *server_cert = SSL_get_peer_certificate (ssl);
732 BIO *bio = BIO_new(BIO_s_mem());
734 /* get PEM buffer in memory */
735 PEM_write_bio_X509(bio, server_cert);
736 pem_len = BIO_get_mem_data(bio, &pem_buf);
737 fwrite(pem_buf, pem_len, 1, stdout);
739 /* print all info on screen .. */
740 X509_print_fp(stdout, server_cert);
743 X509_free (server_cert);
749 if (protocol == PROTO_Z3950)
751 send_initRequest(type_and_host);
757 int cmd_open(const char *arg)
761 strncpy (cur_host, arg, sizeof(cur_host)-1);
762 cur_host[sizeof(cur_host)-1] = 0;
764 return session_connect(cur_host);
771 if(!( auto_reconnect && last_open_command) ) return ;
773 open_command = (char *) xmalloc (strlen(last_open_command)+6);
774 strcpy (open_command, "open ");
776 strcat (open_command, last_open_command);
778 process_cmd_line(open_command);
783 int cmd_authentication(const char *arg)
785 static Z_IdAuthentication au;
786 static char user[40], group[40], pass[40];
787 static Z_IdPass idPass;
792 printf("Auth field set to null\n");
796 r = sscanf (arg, "%39s %39s %39s", user, group, pass);
799 printf("Authentication set to null\n");
805 if (!strcmp(user, "-")) {
806 au.which = Z_IdAuthentication_anonymous;
807 printf("Authentication set to Anonymous\n");
809 au.which = Z_IdAuthentication_open;
811 printf("Authentication set to Open (%s)\n", user);
817 au.which = Z_IdAuthentication_idPass;
818 au.u.idPass = &idPass;
819 idPass.groupId = NULL;
820 idPass.userId = user;
821 idPass.password = group;
822 printf("Authentication set to User (%s), Pass (%s)\n", user, group);
827 au.which = Z_IdAuthentication_idPass;
828 au.u.idPass = &idPass;
829 idPass.groupId = group;
830 idPass.userId = user;
831 idPass.password = pass;
832 printf("Authentication set to User (%s), Group (%s), Pass (%s)\n",
838 /* SEARCH SERVICE ------------------------------ */
839 static void display_record(Z_External *r);
841 static void print_record(const unsigned char *buf, size_t len)
844 print_stringn (buf, len);
845 /* add newline if not already added ... */
846 if (i <= 0 || buf[i-1] != '\n')
850 static void display_record(Z_External *r)
852 oident *ent = oid_getentbyoid(r->direct_reference);
856 * Tell the user what we got.
858 if (r->direct_reference)
860 printf("Record type: ");
862 printf("%s\n", ent->desc);
863 else if (!odr_oid(print, &r->direct_reference, 0, 0))
865 odr_perror(print, "print oid");
869 /* Check if this is a known, ASN.1 type tucked away in an octet string */
870 if (ent && r->which == Z_External_octet)
872 Z_ext_typeent *type = z_ext_getentbyref(ent->value);
878 * Call the given decoder to process the record.
880 odr_setbuf(in, (char*)r->u.octet_aligned->buf,
881 r->u.octet_aligned->len, 0);
882 if (!(*type->fun)(in, &rr, 0, 0))
884 odr_perror(in, "Decoding constructed record.");
885 fprintf(stdout, "[Near %d]\n", odr_offset(in));
886 fprintf(stdout, "Packet dump:\n---------\n");
887 odr_dumpBER(stdout, (char*)r->u.octet_aligned->buf,
888 r->u.octet_aligned->len);
889 fprintf(stdout, "---------\n");
891 /* note just ignores the error ant print the bytes form the octet_aligned later */
894 * Note: we throw away the original, BER-encoded record here.
895 * Do something else with it if you want to keep it.
897 r->u.sutrs = (Z_SUTRS *) rr; /* we don't actually check the type here. */
898 r->which = type->what;
902 if (ent && ent->value == VAL_SOIF)
904 print_record((const unsigned char *) r->u.octet_aligned->buf,
905 r->u.octet_aligned->len);
907 fwrite (r->u.octet_aligned->buf, 1, r->u.octet_aligned->len, marc_file);
909 else if (r->which == Z_External_octet)
911 const char *octet_buf = (char*)r->u.octet_aligned->buf;
912 if (ent->oclass == CLASS_RECSYN &&
913 (ent->value == VAL_TEXT_XML ||
914 ent->value == VAL_APPLICATION_XML ||
915 ent->value == VAL_HTML))
917 print_record((const unsigned char *) octet_buf,
918 r->u.octet_aligned->len);
920 else if (ent->value == VAL_POSTSCRIPT)
922 int size = r->u.octet_aligned->len;
925 print_record((const unsigned char *) octet_buf, size);
930 #if AVOID_MARC_DECODE
931 /* primitive check for a marc OID 5.1-29 except 16 */
932 ent->oidsuffix[0] == 5 && ent->oidsuffix[1] < 30 &&
933 ent->oidsuffix[1] != 16
942 yaz_marc_t mt = yaz_marc_create();
944 if (yaz_marc_decode_buf(mt, octet_buf,r->u.octet_aligned->len,
948 if (marcCharset && !strcmp(marcCharset, "auto"))
950 if (ent->value == VAL_USMARC)
952 if (octet_buf[9] == 'a')
960 else if (marcCharset)
962 if (outputCharset && from)
964 cd = yaz_iconv_open(outputCharset, from);
965 printf ("convert from %s to %s", from,
968 printf (" unsupported\n");
973 fwrite (result, 1, rlen, stdout);
977 size_t inbytesleft = rlen;
978 const char *inp = result;
982 size_t outbytesleft = sizeof(outbuf);
986 r = yaz_iconv (cd, (char**) &inp,
988 &outp, &outbytesleft);
989 if (r == (size_t) (-1))
991 int e = yaz_iconv_error(cd);
992 if (e != YAZ_ICONV_E2BIG)
995 fwrite (outbuf, outp - outbuf, 1, stdout);
1001 printf ("bad MARC. Dumping as it is:\n");
1002 print_record((const unsigned char*) octet_buf,
1003 r->u.octet_aligned->len);
1005 yaz_marc_destroy(mt);
1007 yaz_iconv_close(cd);
1011 print_record((const unsigned char*) octet_buf,
1012 r->u.octet_aligned->len);
1016 fwrite (octet_buf, 1, r->u.octet_aligned->len, marc_file);
1018 else if (ent && ent->value == VAL_SUTRS)
1020 if (r->which != Z_External_sutrs)
1022 printf("Expecting single SUTRS type for SUTRS.\n");
1025 print_record(r->u.sutrs->buf, r->u.sutrs->len);
1027 fwrite (r->u.sutrs->buf, 1, r->u.sutrs->len, marc_file);
1029 else if (ent && ent->value == VAL_GRS1)
1032 if (r->which != Z_External_grs1)
1034 printf("Expecting single GRS type for GRS.\n");
1038 yaz_display_grs1(w, r->u.grs1, 0);
1039 puts (wrbuf_buf(w));
1042 else if (ent && ent->value == VAL_OPAC)
1045 if (r->u.opac->bibliographicRecord)
1046 display_record(r->u.opac->bibliographicRecord);
1047 for (i = 0; i<r->u.opac->num_holdingsData; i++)
1049 Z_HoldingsRecord *h = r->u.opac->holdingsData[i];
1050 if (h->which == Z_HoldingsRecord_marcHoldingsRecord)
1052 printf ("MARC holdings %d\n", i);
1053 display_record(h->u.marcHoldingsRecord);
1055 else if (h->which == Z_HoldingsRecord_holdingsAndCirc)
1059 Z_HoldingsAndCircData *data = h->u.holdingsAndCirc;
1061 printf ("Data holdings %d\n", i);
1062 if (data->typeOfRecord)
1063 printf ("typeOfRecord: %s\n", data->typeOfRecord);
1064 if (data->encodingLevel)
1065 printf ("encodingLevel: %s\n", data->encodingLevel);
1066 if (data->receiptAcqStatus)
1067 printf ("receiptAcqStatus: %s\n", data->receiptAcqStatus);
1068 if (data->generalRetention)
1069 printf ("generalRetention: %s\n", data->generalRetention);
1070 if (data->completeness)
1071 printf ("completeness: %s\n", data->completeness);
1072 if (data->dateOfReport)
1073 printf ("dateOfReport: %s\n", data->dateOfReport);
1075 printf ("nucCode: %s\n", data->nucCode);
1076 if (data->localLocation)
1077 printf ("localLocation: %s\n", data->localLocation);
1078 if (data->shelvingLocation)
1079 printf ("shelvingLocation: %s\n", data->shelvingLocation);
1080 if (data->callNumber)
1081 printf ("callNumber: %s\n", data->callNumber);
1082 if (data->shelvingData)
1083 printf ("shelvingData: %s\n", data->shelvingData);
1084 if (data->copyNumber)
1085 printf ("copyNumber: %s\n", data->copyNumber);
1086 if (data->publicNote)
1087 printf ("publicNote: %s\n", data->publicNote);
1088 if (data->reproductionNote)
1089 printf ("reproductionNote: %s\n", data->reproductionNote);
1090 if (data->termsUseRepro)
1091 printf ("termsUseRepro: %s\n", data->termsUseRepro);
1092 if (data->enumAndChron)
1093 printf ("enumAndChron: %s\n", data->enumAndChron);
1094 for (j = 0; j<data->num_volumes; j++)
1096 printf ("volume %d\n", j);
1097 if (data->volumes[j]->enumeration)
1098 printf (" enumeration: %s\n",
1099 data->volumes[j]->enumeration);
1100 if (data->volumes[j]->chronology)
1101 printf (" chronology: %s\n",
1102 data->volumes[j]->chronology);
1103 if (data->volumes[j]->enumAndChron)
1104 printf (" enumAndChron: %s\n",
1105 data->volumes[j]->enumAndChron);
1107 for (j = 0; j<data->num_circulationData; j++)
1109 printf ("circulation %d\n", j);
1110 if (data->circulationData[j]->availableNow)
1111 printf (" availableNow: %d\n",
1112 *data->circulationData[j]->availableNow);
1113 if (data->circulationData[j]->availablityDate)
1114 printf (" availabiltyDate: %s\n",
1115 data->circulationData[j]->availablityDate);
1116 if (data->circulationData[j]->availableThru)
1117 printf (" availableThru: %s\n",
1118 data->circulationData[j]->availableThru);
1119 if (data->circulationData[j]->restrictions)
1120 printf (" restrictions: %s\n",
1121 data->circulationData[j]->restrictions);
1122 if (data->circulationData[j]->itemId)
1123 printf (" itemId: %s\n",
1124 data->circulationData[j]->itemId);
1125 if (data->circulationData[j]->renewable)
1126 printf (" renewable: %d\n",
1127 *data->circulationData[j]->renewable);
1128 if (data->circulationData[j]->onHold)
1129 printf (" onHold: %d\n",
1130 *data->circulationData[j]->onHold);
1131 if (data->circulationData[j]->enumAndChron)
1132 printf (" enumAndChron: %s\n",
1133 data->circulationData[j]->enumAndChron);
1134 if (data->circulationData[j]->midspine)
1135 printf (" midspine: %s\n",
1136 data->circulationData[j]->midspine);
1137 if (data->circulationData[j]->temporaryLocation)
1138 printf (" temporaryLocation: %s\n",
1139 data->circulationData[j]->temporaryLocation);
1146 printf("Unknown record representation.\n");
1147 if (!z_External(print, &r, 0, 0))
1149 odr_perror(print, "Printing external");
1155 static void display_diagrecs(Z_DiagRec **pp, int num)
1159 Z_DefaultDiagFormat *r;
1161 printf("Diagnostic message(s) from database:\n");
1162 for (i = 0; i<num; i++)
1164 Z_DiagRec *p = pp[i];
1165 if (p->which != Z_DiagRec_defaultFormat)
1167 printf("Diagnostic record not in default format.\n");
1171 r = p->u.defaultFormat;
1172 if (!(ent = oid_getentbyoid(r->diagnosticSetId)) ||
1173 ent->oclass != CLASS_DIAGSET || ent->value != VAL_BIB1)
1174 printf("Missing or unknown diagset\n");
1175 printf(" [%d] %s", *r->condition, diagbib1_str(*r->condition));
1178 case Z_DefaultDiagFormat_v2Addinfo:
1179 printf (" -- v2 addinfo '%s'\n", r->u.v2Addinfo);
1181 case Z_DefaultDiagFormat_v3Addinfo:
1182 printf (" -- v3 addinfo '%s'\n", r->u.v3Addinfo);
1189 static void display_nameplusrecord(Z_NamePlusRecord *p)
1191 if (p->databaseName)
1192 printf("[%s]", p->databaseName);
1193 if (p->which == Z_NamePlusRecord_surrogateDiagnostic)
1194 display_diagrecs(&p->u.surrogateDiagnostic, 1);
1195 else if (p->which == Z_NamePlusRecord_databaseRecord)
1196 display_record(p->u.databaseRecord);
1199 static void display_records(Z_Records *p)
1203 if (p->which == Z_Records_NSD)
1205 Z_DiagRec dr, *dr_p = &dr;
1206 dr.which = Z_DiagRec_defaultFormat;
1207 dr.u.defaultFormat = p->u.nonSurrogateDiagnostic;
1208 display_diagrecs (&dr_p, 1);
1210 else if (p->which == Z_Records_multipleNSD)
1211 display_diagrecs (p->u.multipleNonSurDiagnostics->diagRecs,
1212 p->u.multipleNonSurDiagnostics->num_diagRecs);
1215 printf("Records: %d\n", p->u.databaseOrSurDiagnostics->num_records);
1216 for (i = 0; i < p->u.databaseOrSurDiagnostics->num_records; i++)
1217 display_nameplusrecord(p->u.databaseOrSurDiagnostics->records[i]);
1221 static int send_deleteResultSetRequest(const char *arg)
1226 Z_APDU *apdu = zget_APDU(out, Z_APDU_deleteResultSetRequest);
1227 Z_DeleteResultSetRequest *req = apdu->u.deleteResultSetRequest;
1229 req->referenceId = set_refid (out);
1231 req->num_resultSetList =
1232 sscanf (arg, "%30s %30s %30s %30s %30s %30s %30s %30s",
1233 names[0], names[1], names[2], names[3],
1234 names[4], names[5], names[6], names[7]);
1236 req->deleteFunction = (int *)
1237 odr_malloc (out, sizeof(*req->deleteFunction));
1238 if (req->num_resultSetList > 0)
1240 *req->deleteFunction = Z_DeleteResultSetRequest_list;
1241 req->resultSetList = (char **)
1242 odr_malloc (out, sizeof(*req->resultSetList)*
1243 req->num_resultSetList);
1244 for (i = 0; i<req->num_resultSetList; i++)
1245 req->resultSetList[i] = names[i];
1249 *req->deleteFunction = Z_DeleteResultSetRequest_all;
1250 req->resultSetList = 0;
1254 printf("Sent deleteResultSetRequest.\n");
1259 static int send_srw(Z_SRW_PDU *sr)
1261 const char *charset = negotiationCharset;
1262 const char *host_port = cur_host;
1265 Z_SOAP_Handler h[2] = {
1266 {"http://www.loc.gov/zing/srw/", 0, (Z_SOAP_fun) yaz_srw_codec},
1269 ODR o = odr_createmem(ODR_ENCODE);
1271 Z_SOAP *p = odr_malloc(o, sizeof(*p));
1274 path = odr_malloc(out, strlen(databaseNames[0])+2);
1276 strcpy(path+1, databaseNames[0]);
1278 gdu = z_get_HTTP_Request(out);
1279 gdu->u.HTTP_Request->version = http_version;
1280 gdu->u.HTTP_Request->path = odr_strdup(out, path);
1284 const char *cp0 = strstr(host_port, "://");
1285 const char *cp1 = 0;
1291 cp1 = strchr(cp0, '/');
1293 cp1 = cp0+strlen(cp0);
1297 char *h = odr_malloc(out, cp1 - cp0 + 1);
1298 memcpy (h, cp0, cp1 - cp0);
1300 z_HTTP_header_add(out, &gdu->u.HTTP_Request->headers,
1305 strcpy(ctype, "text/xml");
1306 if (charset && strlen(charset) < 20)
1308 strcat(ctype, "; charset=");
1309 strcat(ctype, charset);
1311 z_HTTP_header_add(out, &gdu->u.HTTP_Request->headers,
1312 "Content-Type", ctype);
1313 z_HTTP_header_add(out, &gdu->u.HTTP_Request->headers,
1314 "SOAPAction", "\"\"");
1315 p->which = Z_SOAP_generic;
1316 p->u.generic = odr_malloc(o, sizeof(*p->u.generic));
1317 p->u.generic->no = 0;
1318 p->u.generic->ns = 0;
1319 p->u.generic->p = sr;
1320 p->ns = "http://schemas.xmlsoap.org/soap/envelope/";
1322 ret = z_soap_codec_enc(o, &p,
1323 &gdu->u.HTTP_Request->content_buf,
1324 &gdu->u.HTTP_Request->content_len, h,
1327 if (z_GDU(out, &gdu, 0, 0))
1335 if (!z_GDU(print, &gdu, 0, 0))
1336 printf ("Failed to print outgoing APDU\n");
1339 buf_out = odr_getbuf(out, &len_out, 0);
1341 /* we don't odr_reset(out), since we may need the buffer again */
1343 do_hex_dump(buf_out, len_out);
1345 r = cs_put(conn, buf_out, len_out);
1357 static char *encode_SRW_term(ODR o, const char *q)
1359 const char *in_charset = "ISO-8859-1";
1360 WRBUF w = wrbuf_alloc();
1364 in_charset = outputCharset;
1365 cd = yaz_iconv_open("UTF-8", in_charset);
1369 return odr_strdup(o, q);
1371 wrbuf_iconv_write(w, cd, q, strlen(q));
1374 int len = wrbuf_len(w);
1375 res = odr_strdupn(o, wrbuf_buf(w), len);
1378 res = odr_strdup(o, q);
1379 yaz_iconv_close(cd);
1385 static int send_SRW_scanRequest(const char *arg, int pos, int num)
1389 /* regular requestse .. */
1390 sr = yaz_srw_get(out, Z_SRW_scan_request);
1395 sr->u.scan_request->query_type = Z_SRW_query_type_cql;
1396 sr->u.scan_request->scanClause.cql = encode_SRW_term(out, arg);
1398 case QueryType_Prefix:
1399 sr->u.scan_request->query_type = Z_SRW_query_type_pqf;
1400 sr->u.scan_request->scanClause.pqf = encode_SRW_term(out, arg);
1403 printf ("Only CQL and PQF supported in SRW\n");
1406 sr->u.scan_request->responsePosition = odr_intdup(out, pos);
1407 sr->u.scan_request->maximumTerms = odr_intdup(out, num);
1408 return send_srw(sr);
1411 static int send_SRW_searchRequest(const char *arg)
1417 assert(srw_sr_odr_out == 0);
1418 srw_sr_odr_out = odr_createmem(ODR_ENCODE);
1420 odr_reset(srw_sr_odr_out);
1424 /* save this for later .. when fetching individual records */
1425 srw_sr = yaz_srw_get(srw_sr_odr_out, Z_SRW_searchRetrieve_request);
1427 /* regular request .. */
1428 sr = yaz_srw_get(out, Z_SRW_searchRetrieve_request);
1433 srw_sr->u.request->query_type = Z_SRW_query_type_cql;
1434 srw_sr->u.request->query.cql = encode_SRW_term(srw_sr_odr_out, arg);
1436 sr->u.request->query_type = Z_SRW_query_type_cql;
1437 sr->u.request->query.cql = encode_SRW_term(srw_sr_odr_out, arg);
1439 case QueryType_Prefix:
1440 srw_sr->u.request->query_type = Z_SRW_query_type_pqf;
1441 srw_sr->u.request->query.pqf = encode_SRW_term(srw_sr_odr_out, arg);
1443 sr->u.request->query_type = Z_SRW_query_type_pqf;
1444 sr->u.request->query.pqf = encode_SRW_term(srw_sr_odr_out, arg);
1447 printf ("Only CQL and PQF supported in SRW\n");
1450 sr->u.request->maximumRecords = odr_intdup(out, 0);
1453 sr->u.request->recordSchema = record_schema;
1454 if (recordsyntax_size == 1 && recordsyntax_list[0] == VAL_TEXT_XML)
1455 sr->u.explain_request->recordPacking = "xml";
1456 return send_srw(sr);
1460 static int send_searchRequest(const char *arg)
1462 Z_APDU *apdu = zget_APDU(out, Z_APDU_searchRequest);
1463 Z_SearchRequest *req = apdu->u.searchRequest;
1465 struct ccl_rpn_node *rpn = NULL;
1467 char setstring[100];
1468 Z_RPNQuery *RPNquery;
1470 YAZ_PQF_Parser pqf_parser;
1472 QueryType myQueryType = queryType;
1475 if (myQueryType == QueryType_CCL2RPN)
1477 rpn = ccl_find_str(bibset, arg, &error, &pos);
1480 printf("CCL ERROR: %s\n", ccl_err_msg(error));
1483 } else if (myQueryType == QueryType_CQL2RPN) {
1484 /* ### All this code should be wrapped in a utility function */
1486 struct cql_node *node;
1487 const char *addinfo;
1488 if (cqltrans == 0) {
1489 printf("Can't use CQL: no translation file. Try set_cqlfile\n");
1492 parser = cql_parser_create();
1493 if ((error = cql_parser_string(parser, arg)) != 0) {
1494 printf("Can't parse CQL: must be a syntax error\n");
1497 node = cql_parser_result(parser);
1498 if ((error = cql_transform_buf(cqltrans, node, pqfbuf,
1499 sizeof pqfbuf)) != 0) {
1500 error = cql_transform_error(cqltrans, &addinfo);
1501 printf ("Can't convert CQL to PQF: %s (addinfo=%s)\n",
1502 cql_strerror(error), addinfo);
1506 myQueryType = QueryType_Prefix;
1509 req->referenceId = set_refid (out);
1510 if (!strcmp(arg, "@big")) /* strictly for troublemaking */
1512 static unsigned char big[2100];
1513 static Odr_oct bigo;
1515 /* send a very big referenceid to test transport stack etc. */
1516 memset(big, 'A', 2100);
1517 bigo.len = bigo.size = 2100;
1519 req->referenceId = &bigo;
1524 sprintf(setstring, "%d", ++setnumber);
1525 req->resultSetName = setstring;
1527 *req->smallSetUpperBound = smallSetUpperBound;
1528 *req->largeSetLowerBound = largeSetLowerBound;
1529 *req->mediumSetPresentNumber = mediumSetPresentNumber;
1530 if (smallSetUpperBound > 0 || (largeSetLowerBound > 1 &&
1531 mediumSetPresentNumber > 0))
1533 if (recordsyntax_list > 0)
1534 req->preferredRecordSyntax =
1535 yaz_oidval_to_z3950oid(out, CLASS_RECSYN, recordsyntax_list[0]);
1536 req->smallSetElementSetNames =
1537 req->mediumSetElementSetNames = elementSetNames;
1539 req->num_databaseNames = num_databaseNames;
1540 req->databaseNames = databaseNames;
1542 req->query = &query;
1544 switch (myQueryType)
1546 case QueryType_Prefix:
1547 query.which = Z_Query_type_1;
1548 pqf_parser = yaz_pqf_create ();
1549 RPNquery = yaz_pqf_parse (pqf_parser, out, arg);
1552 const char *pqf_msg;
1554 int code = yaz_pqf_error (pqf_parser, &pqf_msg, &off);
1555 printf("%*s^\n", off+4, "");
1556 printf("Prefix query error: %s (code %d)\n", pqf_msg, code);
1558 yaz_pqf_destroy (pqf_parser);
1561 yaz_pqf_destroy (pqf_parser);
1562 query.u.type_1 = RPNquery;
1565 query.which = Z_Query_type_2;
1566 query.u.type_2 = &ccl_query;
1567 ccl_query.buf = (unsigned char*) arg;
1568 ccl_query.len = strlen(arg);
1570 case QueryType_CCL2RPN:
1571 query.which = Z_Query_type_1;
1572 RPNquery = ccl_rpn_query(out, rpn);
1575 printf ("Couldn't convert from CCL to RPN\n");
1578 query.u.type_1 = RPNquery;
1579 ccl_rpn_delete (rpn);
1582 query.which = Z_Query_type_104;
1583 ext = (Z_External *) odr_malloc(out, sizeof(*ext));
1584 ext->direct_reference = odr_getoidbystr(out, "1.2.840.10003.16.2");
1585 ext->indirect_reference = 0;
1586 ext->descriptor = 0;
1587 ext->which = Z_External_CQL;
1588 ext->u.cql = odr_strdup(out, arg);
1589 query.u.type_104 = ext;
1592 printf ("Unsupported query type\n");
1595 if (send_apdu(apdu))
1596 printf("Sent searchRequest.\n");
1601 /* display Query Expression as part of searchResult-1 */
1602 static void display_queryExpression (Z_QueryExpression *qe)
1606 if (qe->which == Z_QueryExpression_term)
1608 if (qe->u.term->queryTerm)
1610 Z_Term *term = qe->u.term->queryTerm;
1611 switch (term->which)
1613 case Z_Term_general:
1614 printf (" %.*s", term->u.general->len, term->u.general->buf);
1616 case Z_Term_characterString:
1617 printf (" %s", term->u.characterString);
1619 case Z_Term_numeric:
1620 printf (" %d", *term->u.numeric);
1630 /* see if we can find USR:SearchResult-1 */
1631 static void display_searchResult (Z_OtherInformation *o)
1636 for (i = 0; i < o->num_elements; i++)
1638 if (o->list[i]->which == Z_OtherInfo_externallyDefinedInfo)
1640 Z_External *ext = o->list[i]->information.externallyDefinedInfo;
1642 if (ext->which == Z_External_searchResult1)
1645 Z_SearchInfoReport *sr = ext->u.searchResult1;
1646 printf ("SearchResult-1:");
1647 for (j = 0; j < sr->num; j++)
1649 if (!sr->elements[j]->subqueryExpression)
1651 display_queryExpression (
1652 sr->elements[j]->subqueryExpression);
1653 display_queryExpression (
1654 sr->elements[j]->subqueryInterpretation);
1655 display_queryExpression (
1656 sr->elements[j]->subqueryRecommendation);
1657 if (sr->elements[j]->subqueryCount)
1658 printf ("(%d)", *sr->elements[j]->subqueryCount);
1666 static int process_searchResponse(Z_SearchResponse *res)
1668 printf ("Received SearchResponse.\n");
1669 print_refid (res->referenceId);
1670 if (*res->searchStatus)
1671 printf("Search was a success.\n");
1673 printf("Search was a bloomin' failure.\n");
1674 printf("Number of hits: %d", *res->resultCount);
1675 last_hit_count = *res->resultCount;
1677 printf (", setno %d", setnumber);
1679 display_searchResult (res->additionalSearchInfo);
1680 printf("records returned: %d\n",
1681 *res->numberOfRecordsReturned);
1682 setno += *res->numberOfRecordsReturned;
1684 display_records(res->records);
1688 static void print_level(int iLevel)
1691 for (i = 0; i < iLevel * 4; i++)
1695 static void print_int(int iLevel, const char *pTag, int *pInt)
1699 print_level(iLevel);
1700 printf("%s: %d\n", pTag, *pInt);
1704 static void print_string(int iLevel, const char *pTag, const char *pString)
1706 if (pString != NULL)
1708 print_level(iLevel);
1709 printf("%s: %s\n", pTag, pString);
1713 static void print_oid(int iLevel, const char *pTag, Odr_oid *pOid)
1719 print_level(iLevel);
1720 printf("%s:", pTag);
1721 for (; *pInt != -1; pInt++)
1722 printf(" %d", *pInt);
1727 static void print_referenceId(int iLevel, Z_ReferenceId *referenceId)
1729 if (referenceId != NULL)
1733 print_level(iLevel);
1734 printf("Ref Id (%d, %d): ", referenceId->len, referenceId->size);
1735 for (i = 0; i < referenceId->len; i++)
1736 printf("%c", referenceId->buf[i]);
1741 static void print_string_or_numeric(int iLevel, const char *pTag, Z_StringOrNumeric *pStringNumeric)
1743 if (pStringNumeric != NULL)
1745 switch (pStringNumeric->which)
1747 case Z_StringOrNumeric_string:
1748 print_string(iLevel, pTag, pStringNumeric->u.string);
1751 case Z_StringOrNumeric_numeric:
1752 print_int(iLevel, pTag, pStringNumeric->u.numeric);
1756 print_level(iLevel);
1757 printf("%s: valid type for Z_StringOrNumeric\n", pTag);
1763 static void print_universe_report_duplicate(
1765 Z_UniverseReportDuplicate *pUniverseReportDuplicate)
1767 if (pUniverseReportDuplicate != NULL)
1769 print_level(iLevel);
1770 printf("Universe Report Duplicate: \n");
1772 print_string_or_numeric(iLevel, "Hit No",
1773 pUniverseReportDuplicate->hitno);
1777 static void print_universe_report_hits(
1779 Z_UniverseReportHits *pUniverseReportHits)
1781 if (pUniverseReportHits != NULL)
1783 print_level(iLevel);
1784 printf("Universe Report Hits: \n");
1786 print_string_or_numeric(iLevel, "Database",
1787 pUniverseReportHits->database);
1788 print_string_or_numeric(iLevel, "Hits", pUniverseReportHits->hits);
1792 static void print_universe_report(int iLevel, Z_UniverseReport *pUniverseReport)
1794 if (pUniverseReport != NULL)
1796 print_level(iLevel);
1797 printf("Universe Report: \n");
1799 print_int(iLevel, "Total Hits", pUniverseReport->totalHits);
1800 switch (pUniverseReport->which)
1802 case Z_UniverseReport_databaseHits:
1803 print_universe_report_hits(iLevel,
1804 pUniverseReport->u.databaseHits);
1807 case Z_UniverseReport_duplicate:
1808 print_universe_report_duplicate(iLevel,
1809 pUniverseReport->u.duplicate);
1813 print_level(iLevel);
1814 printf("Type: %d\n", pUniverseReport->which);
1820 static void print_external(int iLevel, Z_External *pExternal)
1822 if (pExternal != NULL)
1824 print_level(iLevel);
1825 printf("External: \n");
1827 print_oid(iLevel, "Direct Reference", pExternal->direct_reference);
1828 print_int(iLevel, "InDirect Reference", pExternal->indirect_reference);
1829 print_string(iLevel, "Descriptor", pExternal->descriptor);
1830 switch (pExternal->which)
1832 case Z_External_universeReport:
1833 print_universe_report(iLevel, pExternal->u.universeReport);
1837 print_level(iLevel);
1838 printf("Type: %d\n", pExternal->which);
1844 static int process_resourceControlRequest (Z_ResourceControlRequest *req)
1846 printf ("Received ResourceControlRequest.\n");
1847 print_referenceId(1, req->referenceId);
1848 print_int(1, "Suspended Flag", req->suspendedFlag);
1849 print_int(1, "Partial Results Available", req->partialResultsAvailable);
1850 print_int(1, "Response Required", req->responseRequired);
1851 print_int(1, "Triggered Request Flag", req->triggeredRequestFlag);
1852 print_external(1, req->resourceReport);
1856 void process_ESResponse(Z_ExtendedServicesResponse *res)
1859 switch (*res->operationStatus)
1861 case Z_ExtendedServicesResponse_done:
1864 case Z_ExtendedServicesResponse_accepted:
1865 printf ("accepted\n");
1867 case Z_ExtendedServicesResponse_failure:
1868 printf ("failure\n");
1869 display_diagrecs(res->diagnostics, res->num_diagnostics);
1872 printf ("unknown\n");
1874 if ( (*res->operationStatus != Z_ExtendedServicesResponse_failure) &&
1875 (res->num_diagnostics != 0) ) {
1876 display_diagrecs(res->diagnostics, res->num_diagnostics);
1878 print_refid (res->referenceId);
1879 if (res->taskPackage &&
1880 res->taskPackage->which == Z_External_extendedService)
1882 Z_TaskPackage *taskPackage = res->taskPackage->u.extendedService;
1883 Odr_oct *id = taskPackage->targetReference;
1884 Z_External *ext = taskPackage->taskSpecificParameters;
1888 printf ("Target Reference: ");
1889 print_stringn (id->buf, id->len);
1892 if (ext->which == Z_External_update)
1894 Z_IUUpdateTaskPackage *utp = ext->u.update->u.taskPackage;
1895 if (utp && utp->targetPart)
1897 Z_IUTargetPart *targetPart = utp->targetPart;
1900 for (i = 0; i<targetPart->num_taskPackageRecords; i++)
1903 Z_IUTaskPackageRecordStructure *tpr =
1904 targetPart->taskPackageRecords[i];
1905 printf ("task package record %d\n", i+1);
1906 if (tpr->which == Z_IUTaskPackageRecordStructure_record)
1908 display_record (tpr->u.record);
1912 printf ("other type\n");
1918 if (res->taskPackage && res->taskPackage->which == Z_External_octet)
1920 Odr_oct *doc = res->taskPackage->u.octet_aligned;
1921 printf("%.*s\n", doc->len, doc->buf);
1925 const char *get_ill_element (void *clientData, const char *element)
1930 static Z_External *create_external_itemRequest()
1932 struct ill_get_ctl ctl;
1933 ILL_ItemRequest *req;
1935 int item_request_size = 0;
1936 char *item_request_buf = 0;
1940 ctl.f = get_ill_element;
1942 req = ill_get_ItemRequest(&ctl, "ill", 0);
1944 printf ("ill_get_ItemRequest failed\n");
1946 if (!ill_ItemRequest (out, &req, 0, 0))
1950 ill_ItemRequest(print, &req, 0, 0);
1953 item_request_buf = odr_getbuf (out, &item_request_size, 0);
1954 if (item_request_buf)
1955 odr_setbuf (out, item_request_buf, item_request_size, 1);
1956 printf ("Couldn't encode ItemRequest, size %d\n", item_request_size);
1963 item_request_buf = odr_getbuf (out, &item_request_size, 0);
1964 oid.proto = PROTO_GENERAL;
1965 oid.oclass = CLASS_GENERAL;
1966 oid.value = VAL_ISO_ILL_1;
1968 r = (Z_External *) odr_malloc (out, sizeof(*r));
1969 r->direct_reference = odr_oiddup(out,oid_getoidbyent(&oid));
1970 r->indirect_reference = 0;
1972 r->which = Z_External_single;
1974 r->u.single_ASN1_type = (Odr_oct *)
1975 odr_malloc (out, sizeof(*r->u.single_ASN1_type));
1976 r->u.single_ASN1_type->buf = (unsigned char *)
1977 odr_malloc (out, item_request_size);
1978 r->u.single_ASN1_type->len = item_request_size;
1979 r->u.single_ASN1_type->size = item_request_size;
1980 memcpy (r->u.single_ASN1_type->buf, item_request_buf,
1983 do_hex_dump(item_request_buf,item_request_size);
1988 static Z_External *create_external_ILL_APDU(int which)
1990 struct ill_get_ctl ctl;
1993 int ill_request_size = 0;
1994 char *ill_request_buf = 0;
1998 ctl.f = get_ill_element;
2000 ill_apdu = ill_get_APDU(&ctl, "ill", 0);
2002 if (!ill_APDU (out, &ill_apdu, 0, 0))
2006 printf ("-------------------\n");
2007 ill_APDU(print, &ill_apdu, 0, 0);
2009 printf ("-------------------\n");
2011 ill_request_buf = odr_getbuf (out, &ill_request_size, 0);
2012 if (ill_request_buf)
2013 odr_setbuf (out, ill_request_buf, ill_request_size, 1);
2014 printf ("Couldn't encode ILL-Request, size %d\n", ill_request_size);
2020 ill_request_buf = odr_getbuf (out, &ill_request_size, 0);
2022 oid.proto = PROTO_GENERAL;
2023 oid.oclass = CLASS_GENERAL;
2024 oid.value = VAL_ISO_ILL_1;
2026 r = (Z_External *) odr_malloc (out, sizeof(*r));
2027 r->direct_reference = odr_oiddup(out,oid_getoidbyent(&oid));
2028 r->indirect_reference = 0;
2030 r->which = Z_External_single;
2032 r->u.single_ASN1_type = (Odr_oct *)
2033 odr_malloc (out, sizeof(*r->u.single_ASN1_type));
2034 r->u.single_ASN1_type->buf = (unsigned char *)
2035 odr_malloc (out, ill_request_size);
2036 r->u.single_ASN1_type->len = ill_request_size;
2037 r->u.single_ASN1_type->size = ill_request_size;
2038 memcpy (r->u.single_ASN1_type->buf, ill_request_buf, ill_request_size);
2039 /* printf ("len = %d\n", ill_request_size); */
2040 /* do_hex_dump(ill_request_buf,ill_request_size); */
2041 /* printf("--- end of extenal\n"); */
2048 static Z_External *create_ItemOrderExternal(const char *type, int itemno)
2050 Z_External *r = (Z_External *) odr_malloc(out, sizeof(Z_External));
2051 oident ItemOrderRequest;
2053 ItemOrderRequest.proto = PROTO_Z3950;
2054 ItemOrderRequest.oclass = CLASS_EXTSERV;
2055 ItemOrderRequest.value = VAL_ITEMORDER;
2057 r->direct_reference = odr_oiddup(out,oid_getoidbyent(&ItemOrderRequest));
2058 r->indirect_reference = 0;
2061 r->which = Z_External_itemOrder;
2063 r->u.itemOrder = (Z_ItemOrder *) odr_malloc(out,sizeof(Z_ItemOrder));
2064 memset(r->u.itemOrder, 0, sizeof(Z_ItemOrder));
2065 r->u.itemOrder->which=Z_IOItemOrder_esRequest;
2067 r->u.itemOrder->u.esRequest = (Z_IORequest *)
2068 odr_malloc(out,sizeof(Z_IORequest));
2069 memset(r->u.itemOrder->u.esRequest, 0, sizeof(Z_IORequest));
2071 r->u.itemOrder->u.esRequest->toKeep = (Z_IOOriginPartToKeep *)
2072 odr_malloc(out,sizeof(Z_IOOriginPartToKeep));
2073 memset(r->u.itemOrder->u.esRequest->toKeep, 0, sizeof(Z_IOOriginPartToKeep));
2074 r->u.itemOrder->u.esRequest->notToKeep = (Z_IOOriginPartNotToKeep *)
2075 odr_malloc(out,sizeof(Z_IOOriginPartNotToKeep));
2076 memset(r->u.itemOrder->u.esRequest->notToKeep, 0, sizeof(Z_IOOriginPartNotToKeep));
2078 r->u.itemOrder->u.esRequest->toKeep->supplDescription = NULL;
2079 r->u.itemOrder->u.esRequest->toKeep->contact = NULL;
2080 r->u.itemOrder->u.esRequest->toKeep->addlBilling = NULL;
2082 r->u.itemOrder->u.esRequest->notToKeep->resultSetItem =
2083 (Z_IOResultSetItem *) odr_malloc(out, sizeof(Z_IOResultSetItem));
2084 memset(r->u.itemOrder->u.esRequest->notToKeep->resultSetItem, 0, sizeof(Z_IOResultSetItem));
2085 r->u.itemOrder->u.esRequest->notToKeep->resultSetItem->resultSetId = "1";
2087 r->u.itemOrder->u.esRequest->notToKeep->resultSetItem->item =
2088 (int *) odr_malloc(out, sizeof(int));
2089 *r->u.itemOrder->u.esRequest->notToKeep->resultSetItem->item = itemno;
2091 if (!strcmp (type, "item") || !strcmp(type, "2"))
2093 printf ("using item-request\n");
2094 r->u.itemOrder->u.esRequest->notToKeep->itemRequest =
2095 create_external_itemRequest();
2097 else if (!strcmp(type, "ill") || !strcmp(type, "1"))
2099 printf ("using ILL-request\n");
2100 r->u.itemOrder->u.esRequest->notToKeep->itemRequest =
2101 create_external_ILL_APDU(ILL_APDU_ILL_Request);
2103 else if (!strcmp(type, "xml") || !strcmp(type, "3"))
2105 const char *xml_buf =
2107 " <type>request</type>\n"
2108 " <libraryNo>000200</libraryNo>\n"
2109 " <borrowerTicketNo> 1212 </borrowerTicketNo>\n"
2111 r->u.itemOrder->u.esRequest->notToKeep->itemRequest =
2112 z_ext_record (out, VAL_TEXT_XML, xml_buf, strlen(xml_buf));
2115 r->u.itemOrder->u.esRequest->notToKeep->itemRequest = 0;
2120 static int send_itemorder(const char *type, int itemno)
2122 Z_APDU *apdu = zget_APDU(out, Z_APDU_extendedServicesRequest);
2123 Z_ExtendedServicesRequest *req = apdu->u.extendedServicesRequest;
2124 oident ItemOrderRequest;
2126 ItemOrderRequest.proto = PROTO_Z3950;
2127 ItemOrderRequest.oclass = CLASS_EXTSERV;
2128 ItemOrderRequest.value = VAL_ITEMORDER;
2129 req->packageType = odr_oiddup(out,oid_getoidbyent(&ItemOrderRequest));
2130 req->packageName = esPackageName;
2132 req->taskSpecificParameters = create_ItemOrderExternal(type, itemno);
2138 static int only_z3950()
2142 printf ("Not connected yet\n");
2145 if (protocol == PROTO_HTTP)
2147 printf ("Not supported by SRW\n");
2153 static int cmd_update_common(const char *arg, int version);
2155 static int cmd_update(const char *arg)
2157 return cmd_update_common(arg, 1);
2160 static int cmd_update0(const char *arg)
2162 return cmd_update_common(arg, 0);
2165 static int cmd_update_common(const char *arg, int version)
2167 Z_APDU *apdu = zget_APDU(out, Z_APDU_extendedServicesRequest );
2168 Z_ExtendedServicesRequest *req = apdu->u.extendedServicesRequest;
2170 char action[20], recid[20];
2175 Z_External *record_this = 0;
2181 sscanf (arg, "%19s %19s%n", action, recid, &noread);
2184 printf("Update must be followed by action and recid\n");
2185 printf(" where action is one of insert,replace,delete.update\n");
2186 printf(" recid is some record ID (any string)\n");
2190 if (!strcmp (action, "insert"))
2191 action_no = Z_IUOriginPartToKeep_recordInsert;
2192 else if (!strcmp (action, "replace"))
2193 action_no = Z_IUOriginPartToKeep_recordReplace;
2194 else if (!strcmp (action, "delete"))
2195 action_no = Z_IUOriginPartToKeep_recordDelete;
2196 else if (!strcmp (action, "update"))
2197 action_no = Z_IUOriginPartToKeep_specialUpdate;
2200 printf ("Bad action: %s\n", action);
2201 printf ("Possible values: insert, replace, delete, update\n");
2206 if (parse_cmd_doc(&arg, out, &rec_buf, &rec_len, 1) == 0)
2210 record_this = z_ext_record (out, VAL_TEXT_XML, rec_buf, rec_len);
2215 printf ("No last record (update ignored)\n");
2218 record_this = record_last;
2222 yaz_oidval_to_z3950oid(out, CLASS_EXTSERV,
2223 version == 0 ? VAL_DBUPDATE0 : VAL_DBUPDATE);
2225 req->packageName = esPackageName;
2227 req->referenceId = set_refid (out);
2229 r = req->taskSpecificParameters = (Z_External *)
2230 odr_malloc (out, sizeof(*r));
2231 r->direct_reference = req->packageType;
2232 r->indirect_reference = 0;
2236 Z_IU0OriginPartToKeep *toKeep;
2237 Z_IU0SuppliedRecords *notToKeep;
2239 r->which = Z_External_update0;
2240 r->u.update0 = (Z_IU0Update *) odr_malloc(out, sizeof(*r->u.update0));
2241 r->u.update0->which = Z_IUUpdate_esRequest;
2242 r->u.update0->u.esRequest = (Z_IU0UpdateEsRequest *)
2243 odr_malloc(out, sizeof(*r->u.update0->u.esRequest));
2244 toKeep = r->u.update0->u.esRequest->toKeep = (Z_IU0OriginPartToKeep *)
2245 odr_malloc(out, sizeof(*r->u.update0->u.esRequest->toKeep));
2247 toKeep->databaseName = databaseNames[0];
2249 toKeep->elementSetName = 0;
2251 toKeep->action = (int *) odr_malloc(out, sizeof(*toKeep->action));
2252 *toKeep->action = action_no;
2254 notToKeep = r->u.update0->u.esRequest->notToKeep = (Z_IU0SuppliedRecords *)
2255 odr_malloc(out, sizeof(*r->u.update0->u.esRequest->notToKeep));
2257 notToKeep->elements = (Z_IU0SuppliedRecords_elem **)
2258 odr_malloc(out, sizeof(*notToKeep->elements));
2259 notToKeep->elements[0] = (Z_IU0SuppliedRecords_elem *)
2260 odr_malloc(out, sizeof(**notToKeep->elements));
2261 notToKeep->elements[0]->which = Z_IUSuppliedRecords_elem_opaque;
2264 notToKeep->elements[0]->u.opaque = (Odr_oct *)
2265 odr_malloc (out, sizeof(Odr_oct));
2266 notToKeep->elements[0]->u.opaque->buf = (unsigned char *) recid;
2267 notToKeep->elements[0]->u.opaque->size = strlen(recid);
2268 notToKeep->elements[0]->u.opaque->len = strlen(recid);
2271 notToKeep->elements[0]->u.opaque = 0;
2272 notToKeep->elements[0]->supplementalId = 0;
2273 notToKeep->elements[0]->correlationInfo = 0;
2274 notToKeep->elements[0]->record = record_this;
2278 Z_IUOriginPartToKeep *toKeep;
2279 Z_IUSuppliedRecords *notToKeep;
2281 r->which = Z_External_update;
2282 r->u.update = (Z_IUUpdate *) odr_malloc(out, sizeof(*r->u.update));
2283 r->u.update->which = Z_IUUpdate_esRequest;
2284 r->u.update->u.esRequest = (Z_IUUpdateEsRequest *)
2285 odr_malloc(out, sizeof(*r->u.update->u.esRequest));
2286 toKeep = r->u.update->u.esRequest->toKeep = (Z_IUOriginPartToKeep *)
2287 odr_malloc(out, sizeof(*r->u.update->u.esRequest->toKeep));
2289 toKeep->databaseName = databaseNames[0];
2291 toKeep->elementSetName = 0;
2292 toKeep->actionQualifier = 0;
2293 toKeep->action = (int *) odr_malloc(out, sizeof(*toKeep->action));
2294 *toKeep->action = action_no;
2296 notToKeep = r->u.update->u.esRequest->notToKeep = (Z_IUSuppliedRecords *)
2297 odr_malloc(out, sizeof(*r->u.update->u.esRequest->notToKeep));
2299 notToKeep->elements = (Z_IUSuppliedRecords_elem **)
2300 odr_malloc(out, sizeof(*notToKeep->elements));
2301 notToKeep->elements[0] = (Z_IUSuppliedRecords_elem *)
2302 odr_malloc(out, sizeof(**notToKeep->elements));
2303 notToKeep->elements[0]->which = Z_IUSuppliedRecords_elem_opaque;
2306 notToKeep->elements[0]->u.opaque = (Odr_oct *)
2307 odr_malloc (out, sizeof(Odr_oct));
2308 notToKeep->elements[0]->u.opaque->buf = (unsigned char *) recid;
2309 notToKeep->elements[0]->u.opaque->size = strlen(recid);
2310 notToKeep->elements[0]->u.opaque->len = strlen(recid);
2313 notToKeep->elements[0]->u.opaque = 0;
2314 notToKeep->elements[0]->supplementalId = 0;
2315 notToKeep->elements[0]->correlationInfo = 0;
2316 notToKeep->elements[0]->record = record_this;
2324 static int cmd_xmles(const char *arg)
2332 int oid_value_xmles = VAL_XMLES;
2333 Z_APDU *apdu = zget_APDU(out, Z_APDU_extendedServicesRequest);
2334 Z_ExtendedServicesRequest *req = apdu->u.extendedServicesRequest;
2336 Z_External *ext = (Z_External *) odr_malloc(out, sizeof(*ext));
2338 req->taskSpecificParameters = ext;
2339 ext->indirect_reference = 0;
2340 ext->descriptor = 0;
2341 ext->which = Z_External_octet;
2342 ext->u.single_ASN1_type = (Odr_oct *) odr_malloc (out, sizeof(Odr_oct));
2343 sscanf(arg, "%50s%n", oid_str, &noread);
2346 printf("Missing OID for xmles\n");
2350 oid_value_xmles = oid_getvalbyname(oid_str);
2351 if (oid_value_xmles == VAL_NONE)
2353 printf("Bad OID: %s\n", oid_str);
2357 if (parse_cmd_doc(&arg, out, (char **) &ext->u.single_ASN1_type->buf,
2358 &ext->u.single_ASN1_type->len, 0) == 0)
2360 req->packageType = yaz_oidval_to_z3950oid(out, CLASS_EXTSERV,
2363 ext->direct_reference = yaz_oidval_to_z3950oid(out, CLASS_EXTSERV,
2371 static int cmd_itemorder(const char *arg)
2378 if (sscanf (arg, "%10s %d", type, &itemno) != 2)
2381 printf("Item order request\n");
2383 send_itemorder(type, itemno);
2387 static void show_opt(const char *arg, void *clientData)
2389 printf ("%s ", arg);
2392 static int cmd_zversion(const char *arg)
2395 z3950_version = atoi(arg);
2397 printf ("version is %d\n", z3950_version);
2401 static int cmd_options(const char *arg)
2407 r = yaz_init_opt_encode(&z3950_options, arg, &pos);
2409 printf("Unknown option(s) near %s\n", arg+pos);
2413 yaz_init_opt_decode(&z3950_options, show_opt, 0);
2419 static int cmd_explain(const char *arg)
2421 if (protocol != PROTO_HTTP)
2432 /* save this for later .. when fetching individual records */
2433 sr = yaz_srw_get(out, Z_SRW_explain_request);
2434 if (recordsyntax_size > 0 && recordsyntax_list[0] == VAL_TEXT_XML)
2435 sr->u.explain_request->recordPacking = "xml";
2443 static int cmd_init(const char *arg)
2447 strncpy (cur_host, arg, sizeof(cur_host)-1);
2448 cur_host[sizeof(cur_host)-1] = 0;
2452 send_initRequest(cur_host);
2456 static int cmd_find(const char *arg)
2460 printf("Find what?\n");
2463 if (protocol == PROTO_HTTP)
2470 if (!send_SRW_searchRequest(arg))
2483 printf("Not connected yet\n");
2487 if (!send_searchRequest(arg))
2493 static int cmd_delete(const char *arg)
2497 if (!send_deleteResultSetRequest(arg))
2502 static int cmd_ssub(const char *arg)
2504 if (!(smallSetUpperBound = atoi(arg)))
2509 static int cmd_lslb(const char *arg)
2511 if (!(largeSetLowerBound = atoi(arg)))
2516 static int cmd_mspn(const char *arg)
2518 if (!(mediumSetPresentNumber = atoi(arg)))
2523 static int cmd_status(const char *arg)
2525 printf("smallSetUpperBound: %d\n", smallSetUpperBound);
2526 printf("largeSetLowerBound: %d\n", largeSetLowerBound);
2527 printf("mediumSetPresentNumber: %d\n", mediumSetPresentNumber);
2531 static int cmd_setnames(const char *arg)
2533 if (*arg == '1') /* enable ? */
2535 else if (*arg == '0') /* disable ? */
2537 else if (setnumber < 0) /* no args, toggle .. */
2543 printf("Set numbering enabled.\n");
2545 printf("Set numbering disabled.\n");
2549 /* PRESENT SERVICE ----------------------------- */
2551 static void parse_show_args(const char *arg_c, char *setstring,
2552 int *start, int *number)
2557 strncpy(arg, arg_c, sizeof(arg)-1);
2558 arg[sizeof(arg)-1] = '\0';
2560 if ((p = strchr(arg, '+')))
2562 *number = atoi(p + 1);
2567 if (!strcmp(arg, "all"))
2569 *number = last_hit_count;
2575 if (p && (p=strchr(p+1, '+')))
2576 strcpy (setstring, p+1);
2577 else if (setnumber >= 0)
2578 sprintf(setstring, "%d", setnumber);
2583 static int send_presentRequest(const char *arg)
2585 Z_APDU *apdu = zget_APDU(out, Z_APDU_presentRequest);
2586 Z_PresentRequest *req = apdu->u.presentRequest;
2587 Z_RecordComposition compo;
2589 char setstring[100];
2591 req->referenceId = set_refid (out);
2593 parse_show_args(arg, setstring, &setno, &nos);
2595 req->resultSetId = setstring;
2597 req->resultSetStartPoint = &setno;
2598 req->numberOfRecordsRequested = &nos;
2600 if (recordsyntax_size == 1)
2601 req->preferredRecordSyntax =
2602 yaz_oidval_to_z3950oid(out, CLASS_RECSYN, recordsyntax_list[0]);
2604 if (record_schema || recordsyntax_size >= 2)
2606 req->recordComposition = &compo;
2607 compo.which = Z_RecordComp_complex;
2608 compo.u.complex = (Z_CompSpec *)
2609 odr_malloc(out, sizeof(*compo.u.complex));
2610 compo.u.complex->selectAlternativeSyntax = (bool_t *)
2611 odr_malloc(out, sizeof(bool_t));
2612 *compo.u.complex->selectAlternativeSyntax = 0;
2614 compo.u.complex->generic = (Z_Specification *)
2615 odr_malloc(out, sizeof(*compo.u.complex->generic));
2617 compo.u.complex->generic->which = Z_Schema_oid;
2619 compo.u.complex->generic->schema.oid = 0;
2622 compo.u.complex->generic->schema.oid =
2623 yaz_str_to_z3950oid(out, CLASS_SCHEMA, record_schema);
2625 if (!compo.u.complex->generic->schema.oid)
2627 /* OID wasn't a schema! Try record syntax instead. */
2628 compo.u.complex->generic->schema.oid = (Odr_oid *)
2629 yaz_str_to_z3950oid(out, CLASS_RECSYN, record_schema);
2632 if (!elementSetNames)
2633 compo.u.complex->generic->elementSpec = 0;
2636 compo.u.complex->generic->elementSpec = (Z_ElementSpec *)
2637 odr_malloc(out, sizeof(Z_ElementSpec));
2638 compo.u.complex->generic->elementSpec->which =
2639 Z_ElementSpec_elementSetName;
2640 compo.u.complex->generic->elementSpec->u.elementSetName =
2641 elementSetNames->u.generic;
2643 compo.u.complex->num_dbSpecific = 0;
2644 compo.u.complex->dbSpecific = 0;
2645 if (recordsyntax_size >= 2)
2648 compo.u.complex->num_recordSyntax = recordsyntax_size;
2649 compo.u.complex->recordSyntax = (Odr_oid **)
2650 odr_malloc(out, recordsyntax_size * sizeof(Odr_oid*));
2651 for (i = 0; i < recordsyntax_size; i++)
2652 compo.u.complex->recordSyntax[i] =
2653 yaz_oidval_to_z3950oid(out, CLASS_RECSYN,
2654 recordsyntax_list[i]);
2658 compo.u.complex->num_recordSyntax = 0;
2659 compo.u.complex->recordSyntax = 0;
2662 else if (elementSetNames)
2664 req->recordComposition = &compo;
2665 compo.which = Z_RecordComp_simple;
2666 compo.u.simple = elementSetNames;
2669 printf("Sent presentRequest (%d+%d).\n", setno, nos);
2674 static int send_SRW_presentRequest(const char *arg)
2676 char setstring[100];
2678 Z_SRW_PDU *sr = srw_sr;
2682 parse_show_args(arg, setstring, &setno, &nos);
2683 sr->u.request->startRecord = odr_intdup(out, setno);
2684 sr->u.request->maximumRecords = odr_intdup(out, nos);
2686 sr->u.request->recordSchema = record_schema;
2687 if (recordsyntax_size == 1 && recordsyntax_list[0] == VAL_TEXT_XML)
2688 sr->u.request->recordPacking = "xml";
2689 return send_srw(sr);
2693 static void close_session (void)
2705 void process_close(Z_Close *req)
2707 Z_APDU *apdu = zget_APDU(out, Z_APDU_close);
2708 Z_Close *res = apdu->u.close;
2710 static char *reasons[] =
2715 "cost limit reached",
2717 "security violation",
2724 printf("Reason: %s, message: %s\n", reasons[*req->closeReason],
2725 req->diagnosticInformation ? req->diagnosticInformation : "NULL");
2730 *res->closeReason = Z_Close_finished;
2732 printf("Sent response.\n");
2737 static int cmd_show(const char *arg)
2739 if (protocol == PROTO_HTTP)
2746 if (!send_SRW_presentRequest(arg))
2756 printf("Not connected yet\n");
2759 if (!send_presentRequest(arg))
2765 int cmd_quit(const char *arg)
2767 printf("See you later, alligator.\n");
2773 int cmd_cancel(const char *arg)
2775 Z_APDU *apdu = zget_APDU(out, Z_APDU_triggerResourceControlRequest);
2776 Z_TriggerResourceControlRequest *req =
2777 apdu->u.triggerResourceControlRequest;
2782 sscanf(arg, "%15s", command);
2786 if (session_initResponse &&
2787 !ODR_MASK_GET(session_initResponse->options,
2788 Z_Options_triggerResourceCtrl))
2790 printf("Target doesn't support cancel (trigger resource ctrl)\n");
2793 *req->requestedAction = Z_TriggerResourceControlRequest_cancel;
2794 req->resultSetWanted = &rfalse;
2795 req->referenceId = set_refid (out);
2798 printf("Sent cancel request\n");
2799 if (!strcmp(command, "wait"))
2805 int cmd_cancel_find(const char *arg) {
2809 return cmd_cancel("");
2814 int send_scanrequest(const char *query, int pp, int num, const char *term)
2816 Z_APDU *apdu = zget_APDU(out, Z_APDU_scanRequest);
2817 Z_ScanRequest *req = apdu->u.scanRequest;
2821 if (queryType == QueryType_CCL2RPN)
2824 struct ccl_rpn_node *rpn;
2826 rpn = ccl_find_str (bibset, query, &error, &pos);
2829 printf("CCL ERROR: %s\n", ccl_err_msg(error));
2833 yaz_oidval_to_z3950oid(out, CLASS_ATTSET, VAL_BIB1);
2834 if (!(req->termListAndStartPoint = ccl_scan_query (out, rpn)))
2836 printf("Couldn't convert CCL to Scan term\n");
2839 ccl_rpn_delete (rpn);
2843 YAZ_PQF_Parser pqf_parser = yaz_pqf_create ();
2845 if (!(req->termListAndStartPoint =
2846 yaz_pqf_scan(pqf_parser, out, &req->attributeSet, query)))
2848 const char *pqf_msg;
2850 int code = yaz_pqf_error (pqf_parser, &pqf_msg, &off);
2851 printf("%*s^\n", off+7, "");
2852 printf("Prefix query error: %s (code %d)\n", pqf_msg, code);
2853 yaz_pqf_destroy (pqf_parser);
2856 yaz_pqf_destroy (pqf_parser);
2860 if (req->termListAndStartPoint->term &&
2861 req->termListAndStartPoint->term->which == Z_Term_general &&
2862 req->termListAndStartPoint->term->u.general)
2864 req->termListAndStartPoint->term->u.general->buf =
2865 (unsigned char *) odr_strdup(out, term);
2866 req->termListAndStartPoint->term->u.general->len =
2867 req->termListAndStartPoint->term->u.general->size =
2871 req->referenceId = set_refid (out);
2872 req->num_databaseNames = num_databaseNames;
2873 req->databaseNames = databaseNames;
2874 req->numberOfTermsRequested = #
2875 req->preferredPositionInResponse = &pp;
2876 req->stepSize = odr_intdup(out, scan_stepSize);
2881 int send_sortrequest(const char *arg, int newset)
2883 Z_APDU *apdu = zget_APDU(out, Z_APDU_sortRequest);
2884 Z_SortRequest *req = apdu->u.sortRequest;
2885 Z_SortKeySpecList *sksl = (Z_SortKeySpecList *)
2886 odr_malloc (out, sizeof(*sksl));
2892 sprintf (setstring, "%d", setnumber);
2894 sprintf (setstring, "default");
2896 req->referenceId = set_refid (out);
2898 req->num_inputResultSetNames = 1;
2899 req->inputResultSetNames = (Z_InternationalString **)
2900 odr_malloc (out, sizeof(*req->inputResultSetNames));
2901 req->inputResultSetNames[0] = odr_strdup (out, setstring);
2903 if (newset && setnumber >= 0)
2904 sprintf (setstring, "%d", ++setnumber);
2906 req->sortedResultSetName = odr_strdup (out, setstring);
2908 req->sortSequence = yaz_sort_spec (out, arg);
2909 if (!req->sortSequence)
2911 printf ("Missing sort specifications\n");
2918 void display_term(Z_TermInfo *t)
2921 printf("%s", t->displayTerm);
2922 else if (t->term->which == Z_Term_general)
2924 printf("%.*s", t->term->u.general->len, t->term->u.general->buf);
2925 sprintf(last_scan_line, "%.*s", t->term->u.general->len,
2926 t->term->u.general->buf);
2929 printf("Term (not general)");
2930 if (t->globalOccurrences)
2931 printf (" (%d)\n", *t->globalOccurrences);
2936 void process_scanResponse(Z_ScanResponse *res)
2939 Z_Entry **entries = NULL;
2940 int num_entries = 0;
2942 printf("Received ScanResponse\n");
2943 print_refid (res->referenceId);
2944 printf("%d entries", *res->numberOfEntriesReturned);
2945 if (res->positionOfTerm)
2946 printf (", position=%d", *res->positionOfTerm);
2948 if (*res->scanStatus != Z_Scan_success)
2949 printf("Scan returned code %d\n", *res->scanStatus);
2952 if ((entries = res->entries->entries))
2953 num_entries = res->entries->num_entries;
2954 for (i = 0; i < num_entries; i++)
2956 int pos_term = res->positionOfTerm ? *res->positionOfTerm : -1;
2957 if (entries[i]->which == Z_Entry_termInfo)
2959 printf("%c ", i + 1 == pos_term ? '*' : ' ');
2960 display_term(entries[i]->u.termInfo);
2963 display_diagrecs(&entries[i]->u.surrogateDiagnostic, 1);
2965 if (res->entries->nonsurrogateDiagnostics)
2966 display_diagrecs (res->entries->nonsurrogateDiagnostics,
2967 res->entries->num_nonsurrogateDiagnostics);
2970 void process_sortResponse(Z_SortResponse *res)
2972 printf("Received SortResponse: status=");
2973 switch (*res->sortStatus)
2975 case Z_SortResponse_success:
2976 printf ("success"); break;
2977 case Z_SortResponse_partial_1:
2978 printf ("partial"); break;
2979 case Z_SortResponse_failure:
2980 printf ("failure"); break;
2982 printf ("unknown (%d)", *res->sortStatus);
2985 print_refid (res->referenceId);
2986 if (res->diagnostics)
2987 display_diagrecs(res->diagnostics,
2988 res->num_diagnostics);
2991 void process_deleteResultSetResponse (Z_DeleteResultSetResponse *res)
2993 printf("Got deleteResultSetResponse status=%d\n",
2994 *res->deleteOperationStatus);
2995 if (res->deleteListStatuses)
2998 for (i = 0; i < res->deleteListStatuses->num; i++)
3000 printf ("%s status=%d\n", res->deleteListStatuses->elements[i]->id,
3001 *res->deleteListStatuses->elements[i]->status);
3006 int cmd_sort_generic(const char *arg, int newset)
3010 if (session_initResponse &&
3011 !ODR_MASK_GET(session_initResponse->options, Z_Options_sort))
3013 printf("Target doesn't support sort\n");
3018 if (send_sortrequest(arg, newset) < 0)
3025 int cmd_sort(const char *arg)
3027 return cmd_sort_generic (arg, 0);
3030 int cmd_sort_newset (const char *arg)
3032 return cmd_sort_generic (arg, 1);
3035 int cmd_scanstep(const char *arg)
3037 scan_stepSize = atoi(arg);
3041 int cmd_scanpos(const char *arg)
3043 int r = sscanf(arg, "%d", &scan_position);
3049 int cmd_scansize(const char *arg)
3051 int r = sscanf(arg, "%d", &scan_size);
3057 int cmd_scan(const char *arg)
3059 if (protocol == PROTO_HTTP)
3068 if (send_SRW_scanRequest(arg, scan_position, scan_size) < 0)
3073 if (send_SRW_scanRequest(last_scan_line, 1, scan_size) < 0)
3088 printf("Session not initialized yet\n");
3092 if (session_initResponse &&
3093 !ODR_MASK_GET(session_initResponse->options, Z_Options_scan))
3095 printf("Target doesn't support scan\n");
3100 strcpy (last_scan_query, arg);
3101 if (send_scanrequest(arg, scan_position, scan_size, 0) < 0)
3106 if (send_scanrequest(last_scan_query, 1, scan_size, last_scan_line) < 0)
3113 int cmd_schema(const char *arg)
3115 xfree(record_schema);
3118 record_schema = xstrdup(arg);
3122 int cmd_format(const char *arg)
3124 const char *cp = arg;
3127 oid_value nsyntax[RECORDSYNTAX_MAX];
3131 printf("Usage: format <recordsyntax>\n");
3134 while (sscanf(cp, "%40s%n", form_str, &nor) >= 1 && nor > 0
3135 && idx < RECORDSYNTAX_MAX)
3137 nsyntax[idx] = oid_getvalbyname(form_str);
3138 if (!strcmp(form_str, "none"))
3140 if (nsyntax[idx] == VAL_NONE)
3142 printf ("unknown record syntax: %s\n", form_str);
3148 recordsyntax_size = idx;
3149 memcpy(recordsyntax_list, nsyntax, idx * sizeof(*nsyntax));
3153 int cmd_elements(const char *arg)
3155 static Z_ElementSetNames esn;
3156 static char what[100];
3160 elementSetNames = 0;
3164 esn.which = Z_ElementSetNames_generic;
3165 esn.u.generic = what;
3166 elementSetNames = &esn;
3170 int cmd_attributeset(const char *arg)
3176 printf("Usage: attributeset <setname>\n");
3179 sscanf(arg, "%s", what);
3180 if (p_query_attset (what))
3182 printf("Unknown attribute set name\n");
3188 int cmd_querytype (const char *arg)
3190 if (!strcmp (arg, "ccl"))
3191 queryType = QueryType_CCL;
3192 else if (!strcmp (arg, "prefix") || !strcmp(arg, "rpn"))
3193 queryType = QueryType_Prefix;
3194 else if (!strcmp (arg, "ccl2rpn") || !strcmp (arg, "cclrpn"))
3195 queryType = QueryType_CCL2RPN;
3196 else if (!strcmp(arg, "cql"))
3197 queryType = QueryType_CQL;
3198 else if (!strcmp (arg, "cql2rpn") || !strcmp (arg, "cqlrpn"))
3199 queryType = QueryType_CQL2RPN;
3202 printf ("Querytype must be one of:\n");
3203 printf (" prefix - Prefix query\n");
3204 printf (" ccl - CCL query\n");
3205 printf (" ccl2rpn - CCL query converted to RPN\n");
3206 printf (" cql - CQL\n");
3207 printf (" cql2rpn - CQL query converted to RPN\n");
3213 int cmd_refid (const char *arg)
3218 refid = xstrdup (arg);
3222 int cmd_close(const char *arg)
3228 apdu = zget_APDU(out, Z_APDU_close);
3229 req = apdu->u.close;
3230 *req->closeReason = Z_Close_finished;
3232 printf("Sent close request.\n");
3237 int cmd_packagename(const char* arg)
3239 xfree (esPackageName);
3240 esPackageName = NULL;
3242 esPackageName = xstrdup(arg);
3246 int cmd_proxy(const char* arg)
3251 yazProxy = xstrdup (arg);
3255 int cmd_marccharset(const char *arg)
3260 if (sscanf(arg, "%29s", l1) < 1)
3262 printf("MARC character set is `%s'\n",
3263 marcCharset ? marcCharset: "none");
3266 xfree (marcCharset);
3268 if (strcmp(l1, "-"))
3269 marcCharset = xstrdup(l1);
3273 int cmd_displaycharset(const char *arg)
3278 if (sscanf(arg, "%29s", l1) < 1)
3280 printf("Display character set is `%s'\n",
3281 outputCharset ? outputCharset: "none");
3285 xfree (outputCharset);
3287 if (!strcmp(l1, "auto") && codeset)
3291 printf ("Display character set: %s\n", codeset);
3292 outputCharset = xstrdup(codeset);
3295 printf ("No codeset found on this system\n");
3297 else if (strcmp(l1, "-") && strcmp(l1, "none"))
3298 outputCharset = xstrdup(l1);
3303 int cmd_negcharset(const char *arg)
3308 if (sscanf(arg, "%29s %d %d", l1, &negotiationCharsetRecords,
3309 &negotiationCharsetVersion) < 1)
3311 printf("Current negotiation character set is `%s'\n",
3312 negotiationCharset ? negotiationCharset: "none");
3313 printf("Records in charset %s\n", negotiationCharsetRecords ?
3315 printf("Charneg version %d\n", negotiationCharsetVersion);
3319 xfree (negotiationCharset);
3320 negotiationCharset = NULL;
3321 if (*l1 && strcmp(l1, "-") && strcmp(l1, "none"))
3323 negotiationCharset = xstrdup(l1);
3324 printf ("Character set negotiation : %s\n", negotiationCharset);
3330 int cmd_charset(const char* arg)
3332 char l1[30], l2[30], l3[30];
3334 *l1 = *l2 = *l3 = 0;
3335 if (sscanf(arg, "%29s %29s %29s", l1, l2, l3) < 1)
3338 cmd_displaycharset("");
3339 cmd_marccharset("");
3345 cmd_displaycharset(l2);
3347 cmd_marccharset(l3);
3352 int cmd_lang(const char* arg)
3355 printf("Current language is `%s'\n", (yazLang)?yazLang:NULL);
3361 yazLang = xstrdup(arg);
3365 int cmd_source(const char* arg, int echo )
3367 /* first should open the file and read one line at a time.. */
3369 char line[102400], *cp;
3372 fprintf(stderr,"Error in source command use a filename\n");
3376 includeFile = fopen (arg, "r");
3379 fprintf(stderr,"Unable to open file %s for reading\n",arg);
3383 while(!feof(includeFile)) {
3384 memset(line,0,sizeof(line));
3385 fgets(line,sizeof(line),includeFile);
3387 if(strlen(line) < 2) continue;
3388 if(line[0] == '#') continue;
3390 if ((cp = strrchr (line, '\n')))
3394 printf( "processing line: %s\n",line );
3396 process_cmd_line(line);
3399 if(fclose(includeFile)<0) {
3400 perror("unable to close include file");
3406 int cmd_source_echo(const char* arg)
3412 int cmd_source_noecho(const char* arg)
3419 int cmd_subshell(const char* args)
3424 system(getenv("SHELL"));
3430 int cmd_set_berfile(const char *arg)
3432 if (ber_file && ber_file != stdout && ber_file != stderr)
3434 if (!strcmp(arg, ""))
3436 else if (!strcmp(arg, "-"))
3439 ber_file = fopen(arg, "a");
3443 int cmd_set_apdufile(const char *arg)
3445 if(apdu_file && apdu_file != stderr && apdu_file != stderr)
3447 if (!strcmp(arg, ""))
3449 else if (!strcmp(arg, "-"))
3453 apdu_file = fopen(arg, "a");
3455 perror("unable to open apdu log file");
3458 odr_setprint(print, apdu_file);
3462 int cmd_set_cclfile(const char* arg)
3466 bibset = ccl_qual_mk ();
3467 inf = fopen (arg, "r");
3469 perror("unable to open CCL file");
3472 ccl_qual_file (bibset, inf);
3475 strcpy(ccl_fields,arg);
3479 int cmd_set_cqlfile(const char* arg)
3481 cql_transform_t newcqltrans;
3483 if ((newcqltrans = cql_transform_open_fname(arg)) == 0) {
3484 perror("unable to open CQL file");
3488 cql_transform_close(cqltrans);
3490 cqltrans = newcqltrans;
3491 strcpy(cql_fields, arg);
3495 int cmd_set_auto_reconnect(const char* arg)
3497 if(strlen(arg)==0) {
3498 auto_reconnect = ! auto_reconnect;
3499 } else if(strcmp(arg,"on")==0) {
3501 } else if(strcmp(arg,"off")==0) {
3504 printf("Error use on or off\n");
3509 printf("Set auto reconnect enabled.\n");
3511 printf("Set auto reconnect disabled.\n");
3517 int cmd_set_auto_wait(const char* arg)
3519 if(strlen(arg)==0) {
3520 auto_wait = ! auto_wait;
3521 } else if(strcmp(arg,"on")==0) {
3523 } else if(strcmp(arg,"off")==0) {
3526 printf("Error use on or off\n");
3531 printf("Set auto wait enabled.\n");
3533 printf("Set auto wait disabled.\n");
3538 int cmd_set_marcdump(const char* arg)
3540 if(marc_file && marc_file != stderr) { /* don't close stdout*/
3544 if (!strcmp(arg, ""))
3546 else if (!strcmp(arg, "-"))
3550 marc_file = fopen(arg, "a");
3552 perror("unable to open marc log file");
3558 this command takes 3 arge {name class oid}
3560 int cmd_register_oid(const char* args) {
3565 {"appctx",CLASS_APPCTX},
3566 {"absyn",CLASS_ABSYN},
3567 {"attset",CLASS_ATTSET},
3568 {"transyn",CLASS_TRANSYN},
3569 {"diagset",CLASS_DIAGSET},
3570 {"recsyn",CLASS_RECSYN},
3571 {"resform",CLASS_RESFORM},
3572 {"accform",CLASS_ACCFORM},
3573 {"extserv",CLASS_EXTSERV},
3574 {"userinfo",CLASS_USERINFO},
3575 {"elemspec",CLASS_ELEMSPEC},
3576 {"varset",CLASS_VARSET},
3577 {"schema",CLASS_SCHEMA},
3578 {"tagset",CLASS_TAGSET},
3579 {"general",CLASS_GENERAL},
3580 {0,(enum oid_class) 0}
3582 char oname_str[101], oclass_str[101], oid_str[101];
3585 oid_class oidclass = CLASS_GENERAL;
3586 int val = 0, oid[OID_SIZE];
3587 struct oident * new_oident=NULL;
3589 if (sscanf (args, "%100[^ ] %100[^ ] %100s",
3590 oname_str,oclass_str, oid_str) < 1) {
3591 printf("Error in register command \n");
3595 for (i = 0; oid_classes[i].className; i++) {
3596 if (!strcmp(oid_classes[i].className, oclass_str))
3598 oidclass=oid_classes[i].oclass;
3603 if(!(oid_classes[i].className)) {
3604 printf("Unknown oid class %s\n",oclass_str);
3612 while (isdigit (*(unsigned char *) name))
3614 val = val*10 + (*name - '0');
3627 new_oident = oid_addent (oid, PROTO_GENERAL, oidclass, oname_str,
3629 if(strcmp(new_oident->desc,oname_str))
3631 fprintf(stderr,"oid is already named as %s, registration failed\n",
3637 int cmd_push_command(const char* arg)
3639 #if HAVE_READLINE_HISTORY_H
3643 fprintf(stderr,"Not compiled with the readline/history module\n");
3648 void source_rcfile()
3650 /* Look for a $HOME/.yazclientrc and source it if it exists */
3651 struct stat statbuf;
3653 char* homedir=getenv("HOME");
3657 sprintf(buffer,"%s/.yazclientrc",homedir);
3659 if(stat(buffer,&statbuf)==0) {
3660 cmd_source(buffer, 0 );
3665 if(stat(".yazclientrc",&statbuf)==0) {
3666 cmd_source(".yazclientrc", 0 );
3671 static void initialize(void)
3676 if (!(out = odr_createmem(ODR_ENCODE)) ||
3677 !(in = odr_createmem(ODR_DECODE)) ||
3678 !(print = odr_createmem(ODR_PRINT)))
3680 fprintf(stderr, "failed to allocate ODR streams\n");
3685 setvbuf(stdout, 0, _IONBF, 0);
3687 odr_setprint(print, apdu_file);
3689 bibset = ccl_qual_mk ();
3690 inf = fopen (ccl_fields, "r");
3693 ccl_qual_file (bibset, inf);
3697 cqltrans = cql_transform_open_fname(cql_fields);
3698 /* If this fails, no problem: we detect cqltrans == 0 later */
3700 #if HAVE_READLINE_READLINE_H
3701 rl_attempted_completion_function = (CPPFunction*)readline_completer;
3705 for(i=0; i<maxOtherInfosSupported; ++i) {
3706 extraOtherInfos[i].oidval = -1;
3713 #if HAVE_GETTIMEOFDAY
3714 struct timeval tv_start;
3718 static void handle_srw_record(Z_SRW_record *rec)
3720 if (rec->recordPosition)
3722 printf ("pos=%d", *rec->recordPosition);
3723 setno = *rec->recordPosition + 1;
3725 if (rec->recordSchema)
3726 printf (" schema=%s", rec->recordSchema);
3728 if (rec->recordData_buf && rec->recordData_len)
3730 fwrite(rec->recordData_buf, 1, rec->recordData_len, stdout);
3732 fwrite (rec->recordData_buf, 1, rec->recordData_len, marc_file);
3735 printf ("No data!");
3739 static void handle_srw_explain_response(Z_SRW_explainResponse *res)
3741 handle_srw_record(&res->record);
3744 static void handle_srw_response(Z_SRW_searchRetrieveResponse *res)
3748 printf ("Received SRW SearchRetrieve Response\n");
3750 for (i = 0; i<res->num_diagnostics; i++)
3752 if (res->diagnostics[i].uri)
3753 printf ("SRW diagnostic %s\n",
3754 res->diagnostics[i].uri);
3756 printf ("SRW diagnostic missing or could not be decoded\n");
3757 if (res->diagnostics[i].message)
3758 printf ("Message: %s\n", res->diagnostics[i].message);
3759 if (res->diagnostics[i].details)
3760 printf ("Details: %s\n", res->diagnostics[i].details);
3762 if (res->numberOfRecords)
3763 printf ("Number of hits: %d\n", *res->numberOfRecords);
3764 for (i = 0; i<res->num_records; i++)
3765 handle_srw_record(res->records + i);
3768 static void handle_srw_scan_term(Z_SRW_scanTerm *term)
3770 if (term->displayTerm)
3771 printf("%s:", term->displayTerm);
3772 else if (term->value)
3773 printf("%s:", term->value);
3775 printf("No value:");
3776 if (term->numberOfRecords)
3777 printf(" %d", *term->numberOfRecords);
3778 if (term->whereInList)
3779 printf(" %s", term->whereInList);
3780 if (term->value && term->displayTerm)
3781 printf(" %s", term->value);
3783 strcpy(last_scan_line, term->value);
3787 static void handle_srw_scan_response(Z_SRW_scanResponse *res)
3791 printf ("Received SRW Scan Response\n");
3793 for (i = 0; i<res->num_diagnostics; i++)
3795 if (res->diagnostics[i].uri)
3796 printf ("SRW diagnostic %s\n",
3797 res->diagnostics[i].uri);
3799 printf ("SRW diagnostic missing or could not be decoded\n");
3800 if (res->diagnostics[i].message)
3801 printf ("Message: %s\n", res->diagnostics[i].message);
3802 if (res->diagnostics[i].details)
3803 printf ("Details: %s\n", res->diagnostics[i].details);
3806 for (i = 0; i<res->num_terms; i++)
3807 handle_srw_scan_term(res->terms + i);
3810 static void http_response(Z_HTTP_Response *hres)
3813 const char *content_type = z_HTTP_header_lookup(hres->headers,
3815 const char *connection_head = z_HTTP_header_lookup(hres->headers,
3817 if (content_type && !yaz_strcmp_del("text/xml", content_type, "; "))
3819 Z_SOAP *soap_package = 0;
3820 ODR o = odr_createmem(ODR_DECODE);
3821 Z_SOAP_Handler soap_handlers[2] = {
3822 {"http://www.loc.gov/zing/srw/", 0,
3823 (Z_SOAP_fun) yaz_srw_codec},
3826 ret = z_soap_codec(o, &soap_package,
3827 &hres->content_buf, &hres->content_len,
3829 if (!ret && soap_package->which == Z_SOAP_generic &&
3830 soap_package->u.generic->no == 0)
3832 Z_SRW_PDU *sr = soap_package->u.generic->p;
3833 if (sr->which == Z_SRW_searchRetrieve_response)
3834 handle_srw_response(sr->u.response);
3835 else if (sr->which == Z_SRW_explain_response)
3836 handle_srw_explain_response(sr->u.explain_response);
3837 else if (sr->which == Z_SRW_scan_response)
3838 handle_srw_scan_response(sr->u.scan_response);
3842 else if (soap_package && (soap_package->which == Z_SOAP_fault
3843 || soap_package->which == Z_SOAP_error))
3845 printf ("HTTP Error Status=%d\n", hres->code);
3846 printf ("SOAP Fault code %s\n",
3847 soap_package->u.fault->fault_code);
3848 printf ("SOAP Fault string %s\n",
3849 soap_package->u.fault->fault_string);
3850 if (soap_package->u.fault->details)
3851 printf ("SOAP Details %s\n",
3852 soap_package->u.fault->details);
3860 if (hres->code != 200)
3862 printf ("HTTP Error Status=%d\n", hres->code);
3866 printf ("Decoding of SRW package failed\n");
3872 if (!strcmp(hres->version, "1.0"))
3874 /* HTTP 1.0: only if Keep-Alive we stay alive.. */
3875 if (!connection_head || strcmp(connection_head, "Keep-Alive"))
3880 /* HTTP 1.1: only if no close we stay alive .. */
3881 if (connection_head && !strcmp(connection_head, "close"))
3888 void wait_and_handle_response(int one_response_only)
3890 int reconnect_ok = 1;
3893 int netbufferlen = 0;
3894 #if HAVE_GETTIMEOFDAY
3896 struct timeval tv_end;
3902 res = cs_get(conn, &netbuffer, &netbufferlen);
3903 if (reconnect_ok && res <= 0 && protocol == PROTO_HTTP)
3914 buf_out = odr_getbuf(out, &len_out, 0);
3916 do_hex_dump(buf_out, len_out);
3918 cs_put(conn, buf_out, len_out);
3926 printf("Target closed connection\n");
3930 #if HAVE_GETTIMEOFDAY
3931 if (got_tv_end == 0)
3932 gettimeofday (&tv_end, 0); /* count first one only */
3936 odr_reset(in); /* release APDU from last round */
3938 do_hex_dump(netbuffer, res);
3939 odr_setbuf(in, netbuffer, res, 0);
3941 if (!z_GDU(in, &gdu, 0, 0))
3943 FILE *f = ber_file ? ber_file : stdout;
3944 odr_perror(in, "Decoding incoming APDU");
3945 fprintf(f, "[Near %d]\n", odr_offset(in));
3946 fprintf(f, "Packet dump:\n---------\n");
3947 odr_dumpBER(f, netbuffer, res);
3948 fprintf(f, "---------\n");
3951 z_GDU(print, &gdu, 0, 0);
3954 if (conn && cs_more(conn))
3959 odr_dumpBER(ber_file, netbuffer, res);
3960 if (apdu_file && !z_GDU(print, &gdu, 0, 0))
3962 odr_perror(print, "Failed to print incoming APDU");
3966 if (gdu->which == Z_GDU_Z3950)
3968 Z_APDU *apdu = gdu->u.z3950;
3971 case Z_APDU_initResponse:
3972 process_initResponse(apdu->u.initResponse);
3974 case Z_APDU_searchResponse:
3975 process_searchResponse(apdu->u.searchResponse);
3977 case Z_APDU_scanResponse:
3978 process_scanResponse(apdu->u.scanResponse);
3980 case Z_APDU_presentResponse:
3981 print_refid (apdu->u.presentResponse->referenceId);
3983 *apdu->u.presentResponse->numberOfRecordsReturned;
3984 if (apdu->u.presentResponse->records)
3985 display_records(apdu->u.presentResponse->records);
3987 printf("No records.\n");
3988 printf ("nextResultSetPosition = %d\n",
3989 *apdu->u.presentResponse->nextResultSetPosition);
3991 case Z_APDU_sortResponse:
3992 process_sortResponse(apdu->u.sortResponse);
3994 case Z_APDU_extendedServicesResponse:
3995 printf("Got extended services response\n");
3996 process_ESResponse(apdu->u.extendedServicesResponse);
3999 printf("Target has closed the association.\n");
4000 process_close(apdu->u.close);
4002 case Z_APDU_resourceControlRequest:
4003 process_resourceControlRequest
4004 (apdu->u.resourceControlRequest);
4006 case Z_APDU_deleteResultSetResponse:
4007 process_deleteResultSetResponse(apdu->u.
4008 deleteResultSetResponse);
4011 printf("Received unknown APDU type (%d).\n",
4017 else if (gdu->which == Z_GDU_HTTP_Response)
4019 http_response(gdu->u.HTTP_Response);
4022 if (one_response_only)
4024 if (conn && !cs_more(conn))
4027 #if HAVE_GETTIMEOFDAY
4031 printf ("S/U S/U=%ld/%ld %ld/%ld",
4032 (long) tv_start.tv_sec,
4033 (long) tv_start.tv_usec,
4034 (long) tv_end.tv_sec,
4035 (long) tv_end.tv_usec);
4037 printf ("Elapsed: %.6f\n",
4038 (double) tv_end.tv_usec / 1e6 + tv_end.tv_sec -
4039 ((double) tv_start.tv_usec / 1e6 + tv_start.tv_sec));
4046 int cmd_cclparse(const char* arg)
4049 struct ccl_rpn_node *rpn=NULL;
4052 rpn = ccl_find_str (bibset, arg, &error, &pos);
4055 printf ("%*s^ - ", 3+strlen(last_cmd)+1+pos, " ");
4056 printf ("%s\n", ccl_err_msg (error));
4062 ccl_pr_tree(rpn, stdout);
4066 ccl_rpn_delete(rpn);
4074 int cmd_set_otherinfo(const char* args)
4076 char oidstr[101], otherinfoString[101];
4081 sscan_res = sscanf (args, "%d %100[^ ] %100s", &otherinfoNo, oidstr, otherinfoString);
4083 /* reset this otherinfo */
4084 if(otherinfoNo>=maxOtherInfosSupported) {
4085 printf("Error otherinfo index to large (%d>%d)\n",
4086 otherinfoNo,maxOtherInfosSupported);
4088 extraOtherInfos[otherinfoNo].oidval = -1;
4089 if (extraOtherInfos[otherinfoNo].value)
4090 free(extraOtherInfos[otherinfoNo].value);
4094 printf("Error in set_otherinfo command \n");
4098 if (otherinfoNo>=maxOtherInfosSupported) {
4099 printf("Error otherinfo index to large (%d>%d)\n",
4100 otherinfoNo,maxOtherInfosSupported);
4103 oidval = oid_getvalbyname (oidstr);
4104 if (oidval == -1 ) {
4105 printf("Error in set_otherinfo command unknown oid %s \n",oidstr);
4108 extraOtherInfos[otherinfoNo].oidval = oidval;
4109 if(extraOtherInfos[otherinfoNo].value) free(extraOtherInfos[otherinfoNo].value);
4110 extraOtherInfos[otherinfoNo].value = strdup(otherinfoString);
4115 int cmd_sleep(const char* args )
4124 printf("Done sleeping %d seconds\n", sec);
4129 int cmd_list_otherinfo(const char* args)
4133 if(strlen(args)>0) {
4135 if( i >= maxOtherInfosSupported ) {
4136 printf("Error otherinfo index to large (%d>%d)\n",i,maxOtherInfosSupported);
4140 if(extraOtherInfos[i].oidval != -1)
4141 printf(" otherinfo %d %s %s\n",
4143 yaz_z3950_oid_value_to_str(
4144 (enum oid_value) extraOtherInfos[i].oidval,
4146 extraOtherInfos[i].value);
4149 for(i=0; i<maxOtherInfosSupported; ++i) {
4150 if(extraOtherInfos[i].oidval != -1)
4151 printf(" otherinfo %d %s %s\n",
4153 yaz_z3950_oid_value_to_str(
4154 (enum oid_value) extraOtherInfos[i].oidval,
4156 extraOtherInfos[i].value);
4164 int cmd_list_all(const char* args) {
4167 /* connection options */
4169 printf("Connected to : %s\n",last_open_command);
4171 if(last_open_command)
4172 printf("Not connected to : %s\n",last_open_command);
4174 printf("Not connected : \n");
4177 if(yazProxy) printf("using proxy : %s\n",yazProxy);
4179 printf("auto_reconnect : %s\n",auto_reconnect?"on":"off");
4180 printf("auto_wait : %s\n",auto_wait?"on":"off");
4183 printf("Authentication : none\n");
4185 switch(auth->which) {
4186 case Z_IdAuthentication_idPass:
4187 printf("Authentication : IdPass\n");
4188 printf(" Login User : %s\n",auth->u.idPass->userId?auth->u.idPass->userId:"");
4189 printf(" Login Group : %s\n",auth->u.idPass->groupId?auth->u.idPass->groupId:"");
4190 printf(" Password : %s\n",auth->u.idPass->password?auth->u.idPass->password:"");
4192 case Z_IdAuthentication_open:
4193 printf("Authentication : psOpen\n");
4194 printf(" Open string : %s\n",auth->u.open);
4197 printf("Authentication : Unknown\n");
4200 if (negotiationCharset)
4201 printf("Neg. Character set : `%s'\n", negotiationCharset);
4205 for (i = 0; i<num_databaseNames; i++) printf("%s ",databaseNames[i]);
4209 printf("CCL file : %s\n",ccl_fields);
4210 printf("CQL file : %s\n",cql_fields);
4211 printf("Query type : %s\n",query_type_as_string(queryType));
4213 printf("Named Result Sets : %s\n",setnumber==-1?"off":"on");
4215 /* piggy back options */
4216 printf("ssub/lslb/mspn : %d/%d/%d\n",smallSetUpperBound,largeSetLowerBound,mediumSetPresentNumber);
4218 /* print present related options */
4219 printf("Format : %s\n",
4220 (recordsyntax_size > 0) ?
4221 yaz_z3950_oid_value_to_str(recordsyntax_list[0], CLASS_RECSYN) :
4223 printf("Schema : %s\n",record_schema ? record_schema : "not set");
4224 printf("Elements : %s\n",elementSetNames?elementSetNames->u.generic:"");
4226 /* loging options */
4227 printf("APDU log : %s\n",apdu_file?"on":"off");
4228 printf("Record log : %s\n",marc_file?"on":"off");
4231 printf("Other Info: \n");
4232 cmd_list_otherinfo("");
4237 int cmd_clear_otherinfo(const char* args)
4239 if(strlen(args)>0) {
4241 otherinfoNo = atoi(args);
4242 if( otherinfoNo >= maxOtherInfosSupported ) {
4243 printf("Error otherinfo index to large (%d>%d)\n",otherinfoNo,maxOtherInfosSupported);
4247 if(extraOtherInfos[otherinfoNo].oidval != -1) {
4248 /* only clear if set. */
4249 extraOtherInfos[otherinfoNo].oidval=-1;
4250 free(extraOtherInfos[otherinfoNo].value);
4255 for(i=0; i<maxOtherInfosSupported; ++i) {
4256 if (extraOtherInfos[i].oidval!=-1 ) {
4257 extraOtherInfos[i].oidval=-1;
4258 free(extraOtherInfos[i].value);
4265 int cmd_wait_response(const char *arg)
4267 int wait_for = atoi(arg);
4269 if( wait_for < 1 ) {
4273 for( i=0 ; i < wait_for ; ++i ) {
4274 wait_and_handle_response(1);
4279 static int cmd_help (const char *line);
4281 typedef char *(*completerFunctionType)(const char *text, int state);
4285 int (*fun)(const char *arg);
4287 completerFunctionType rl_completerfunction;
4288 int complete_filenames;
4289 char **local_tabcompletes;
4291 {"open", cmd_open, "('tcp'|'ssl')':<host>[':'<port>][/<db>]",NULL,0,NULL},
4292 {"quit", cmd_quit, "",NULL,0,NULL},
4293 {"exit", cmd_quit, "",NULL,0,NULL},
4294 {"find", cmd_find, "<query>",NULL,0,NULL},
4295 {"delete", cmd_delete, "<setname>",NULL,0,NULL},
4296 {"base", cmd_base, "<base-name>",NULL,0,NULL},
4297 {"show", cmd_show, "<rec#>['+'<#recs>['+'<setname>]]",NULL,0,NULL},
4298 {"scan", cmd_scan, "<term>",NULL,0,NULL},
4299 {"scanstep", cmd_scanstep, "<size>",NULL,0,NULL},
4300 {"scanpos", cmd_scanpos, "<size>",NULL,0,NULL},
4301 {"scansize", cmd_scansize, "<size>",NULL,0,NULL},
4302 {"sort", cmd_sort, "<sortkey> <flag> <sortkey> <flag> ...",NULL,0,NULL},
4303 {"sort+", cmd_sort_newset, "<sortkey> <flag> <sortkey> <flag> ...",NULL,0,NULL},
4304 {"authentication", cmd_authentication, "<acctstring>",NULL,0,NULL},
4305 {"lslb", cmd_lslb, "<largeSetLowerBound>",NULL,0,NULL},
4306 {"ssub", cmd_ssub, "<smallSetUpperBound>",NULL,0,NULL},
4307 {"mspn", cmd_mspn, "<mediumSetPresentNumber>",NULL,0,NULL},
4308 {"status", cmd_status, "",NULL,0,NULL},
4309 {"setnames", cmd_setnames, "",NULL,0,NULL},
4310 {"cancel", cmd_cancel, "",NULL,0,NULL},
4311 {"cancel_find", cmd_cancel_find, "<query>",NULL,0,NULL},
4312 {"format", cmd_format, "<recordsyntax>",complete_format,0,NULL},
4313 {"schema", cmd_schema, "<schema>",complete_schema,0,NULL},
4314 {"elements", cmd_elements, "<elementSetName>",NULL,0,NULL},
4315 {"close", cmd_close, "",NULL,0,NULL},
4316 {"attributeset", cmd_attributeset, "<attrset>",complete_attributeset,0,NULL},
4317 {"querytype", cmd_querytype, "<type>",complete_querytype,0,NULL},
4318 {"refid", cmd_refid, "<id>",NULL,0,NULL},
4319 {"itemorder", cmd_itemorder, "ill|item <itemno>",NULL,0,NULL},
4320 {"update", cmd_update, "<action> <recid> [<doc>]",NULL,0,NULL},
4321 {"update0", cmd_update0, "<action> <recid> [<doc>]",NULL,0,NULL},
4322 {"xmles", cmd_xmles, "<OID> <doc>",NULL,0,NULL},
4323 {"packagename", cmd_packagename, "<packagename>",NULL,0,NULL},
4324 {"proxy", cmd_proxy, "[('tcp'|'ssl')]<host>[':'<port>]",NULL,0,NULL},
4325 {"charset", cmd_charset, "<nego_charset> <output_charset>",NULL,0,NULL},
4326 {"negcharset", cmd_negcharset, "<nego_charset>",NULL,0,NULL},
4327 {"displaycharset", cmd_displaycharset, "<output_charset>",NULL,0,NULL},
4328 {"marccharset", cmd_marccharset, "<charset_name>",NULL,0,NULL},
4329 {"lang", cmd_lang, "<language_code>",NULL,0,NULL},
4330 {".", cmd_source_echo, "<filename>",NULL,1,NULL},
4331 {"!", cmd_subshell, "Subshell command",NULL,1,NULL},
4332 {"set_apdufile", cmd_set_apdufile, "<filename>",NULL,1,NULL},
4333 {"set_berfile", cmd_set_berfile, "<filename>",NULL,1,NULL},
4334 {"set_marcdump", cmd_set_marcdump," <filename>",NULL,1,NULL},
4335 {"set_cclfile", cmd_set_cclfile," <filename>",NULL,1,NULL},
4336 {"set_cqlfile", cmd_set_cqlfile," <filename>",NULL,1,NULL},
4337 {"set_auto_reconnect", cmd_set_auto_reconnect," on|off",complete_auto_reconnect,1,NULL},
4338 {"set_auto_wait", cmd_set_auto_wait," on|off",complete_auto_reconnect,1,NULL},
4339 {"set_otherinfo", cmd_set_otherinfo,"<otherinfoinddex> <oid> <string>",NULL,0,NULL},
4340 {"sleep", cmd_sleep,"<seconds>",NULL,0,NULL},
4341 {"register_oid", cmd_register_oid,"<name> <class> <oid>",NULL,0,NULL},
4342 {"push_command", cmd_push_command,"<command>",command_generator,0,NULL},
4343 {"register_tab", cmd_register_tab,"<commandname> <tab>",command_generator,0,NULL},
4344 {"cclparse", cmd_cclparse,"<ccl find command>",NULL,0,NULL},
4345 {"list_otherinfo",cmd_list_otherinfo,"[otherinfoinddex]",NULL,0,NULL},
4346 {"list_all",cmd_list_all,"",NULL,0,NULL},
4347 {"clear_otherinfo",cmd_clear_otherinfo,"",NULL,0,NULL},
4348 {"wait_response",cmd_wait_response,"<number>",NULL,0,NULL},
4349 /* Server Admin Functions */
4350 {"adm-reindex", cmd_adm_reindex, "<database-name>",NULL,0,NULL},
4351 {"adm-truncate", cmd_adm_truncate, "('database'|'index')<object-name>",NULL,0,NULL},
4352 {"adm-create", cmd_adm_create, "",NULL,0,NULL},
4353 {"adm-drop", cmd_adm_drop, "('database'|'index')<object-name>",NULL,0,NULL},
4354 {"adm-import", cmd_adm_import, "<record-type> <dir> <pattern>",NULL,0,NULL},
4355 {"adm-refresh", cmd_adm_refresh, "",NULL,0,NULL},
4356 {"adm-commit", cmd_adm_commit, "",NULL,0,NULL},
4357 {"adm-shutdown", cmd_adm_shutdown, "",NULL,0,NULL},
4358 {"adm-startup", cmd_adm_startup, "",NULL,0,NULL},
4359 {"explain", cmd_explain, "", NULL, 0, NULL},
4360 {"options", cmd_options, "", NULL, 0, NULL},
4361 {"zversion", cmd_zversion, "", NULL, 0, NULL},
4362 {"help", cmd_help, "", NULL,0,NULL},
4363 {"init", cmd_init, "", NULL,0,NULL},
4367 static int cmd_help (const char *line)
4373 sscanf (line, "%20s", topic);
4376 printf("Commands:\n");
4377 for (i = 0; cmd_array[i].cmd; i++)
4378 if (*topic == 0 || strcmp (topic, cmd_array[i].cmd) == 0)
4379 printf(" %s %s\n", cmd_array[i].cmd, cmd_array[i].ad);
4380 if (!strcmp(topic, "find"))
4383 printf(" \"term\" Simple Term\n");
4384 printf(" @attr [attset] type=value op Attribute\n");
4385 printf(" @and opl opr And\n");
4386 printf(" @or opl opr Or\n");
4387 printf(" @not opl opr And-Not\n");
4388 printf(" @set set Result set\n");
4389 printf(" @prox exl dist ord rel uc ut Proximity. Use help prox\n");
4391 printf("Bib-1 attribute types\n");
4393 printf("4=Title 7=ISBN 8=ISSN 30=Date 62=Abstract 1003=Author 1016=Any\n");
4394 printf("2=Relation: ");
4395 printf("1< 2<= 3= 4>= 5> 6!= 102=Relevance\n");
4396 printf("3=Position: ");
4397 printf("1=First in Field 2=First in subfield 3=Any position\n");
4398 printf("4=Structure: ");
4399 printf("1=Phrase 2=Word 3=Key 4=Year 5=Date 6=WordList\n");
4400 printf("5=Truncation: ");
4401 printf("1=Right 2=Left 3=L&R 100=No 101=# 102=Re-1 103=Re-2\n");
4402 printf("6=Completeness:");
4403 printf("1=Incomplete subfield 2=Complete subfield 3=Complete field\n");
4405 if (!strcmp(topic, "prox"))
4407 printf("Proximity:\n");
4408 printf(" @prox exl dist ord rel uc ut\n");
4409 printf(" exl: exclude flag . 0=include, 1=exclude.\n");
4410 printf(" dist: distance integer.\n");
4411 printf(" ord: order flag. 0=unordered, 1=ordered.\n");
4412 printf(" rel: relation integer. 1< 2<= 3= 4>= 5> 6!= .\n");
4413 printf(" uc: unit class. k=known, p=private.\n");
4414 printf(" ut: unit type. 1=character, 2=word, 3=sentence,\n");
4415 printf(" 4=paragraph, 5=section, 6=chapter, 7=document,\n");
4416 printf(" 8=element, 9=subelement, 10=elementType, 11=byte.\n");
4417 printf("\nExamples:\n");
4418 printf(" Search for a and b in-order at most 3 words apart:\n");
4419 printf(" @prox 0 3 1 2 k 2 a b\n");
4420 printf(" Search for any order of a and b next to each other:\n");
4421 printf(" @prox 0 1 0 3 k 2 a b\n");
4426 int cmd_register_tab(const char* arg) {
4428 char command[101], tabargument[101];
4433 if (sscanf (arg, "%100s %100s", command, tabargument) < 1) {
4437 /* locate the amdn in the list */
4438 for (i = 0; cmd_array[i].cmd; i++) {
4439 if (!strncmp(cmd_array[i].cmd, command, strlen(command))) {
4444 if(!cmd_array[i].cmd) {
4445 fprintf(stderr,"Unknown command %s\n",command);
4450 if(!cmd_array[i].local_tabcompletes)
4451 cmd_array[i].local_tabcompletes = (char **) calloc(1,sizeof(char**));
4455 tabslist = cmd_array[i].local_tabcompletes;
4456 for(;tabslist && *tabslist;tabslist++) {
4460 cmd_array[i].local_tabcompletes = (char **)
4461 realloc(cmd_array[i].local_tabcompletes,(num_of_tabs+2)*sizeof(char**));
4462 tabslist=cmd_array[i].local_tabcompletes;
4463 tabslist[num_of_tabs]=strdup(tabargument);
4464 tabslist[num_of_tabs+1]=NULL;
4469 void process_cmd_line(char* line)
4472 char word[32], arg[10240];
4474 #if HAVE_GETTIMEOFDAY
4475 gettimeofday (&tv_start, 0);
4478 if ((res = sscanf(line, "%31s %10239[^;]", word, arg)) <= 0)
4480 strcpy(word, last_cmd);
4485 strcpy(last_cmd, word);
4487 /* removed tailing spaces from the arg command */
4490 char* lastnonspace=NULL;
4493 if(!isspace(*(unsigned char *) p)) {
4498 *(++lastnonspace) = 0;
4501 for (i = 0; cmd_array[i].cmd; i++)
4502 if (!strncmp(cmd_array[i].cmd, word, strlen(word)))
4504 res = (*cmd_array[i].fun)(arg);
4508 if (!cmd_array[i].cmd) /* dump our help-screen */
4510 printf("Unknown command: %s.\n", word);
4511 printf("Type 'help' for list of commands\n");
4515 if(apdu_file) fflush(apdu_file);
4517 if (res >= 2 && auto_wait)
4518 wait_and_handle_response(0);
4527 char *command_generator(const char *text, int state)
4533 for( ; cmd_array[idx].cmd; ++idx) {
4534 if (!strncmp(cmd_array[idx].cmd,text,strlen(text))) {
4535 ++idx; /* skip this entry on the next run */
4536 return strdup(cmd_array[idx-1].cmd);
4544 This function only known how to complete on the first word
4546 char ** readline_completer(char *text, int start, int end) {
4547 #if HAVE_READLINE_READLINE_H
4549 completerFunctionType completerToUse;
4552 #if HAVE_READLINE_RL_COMPLETION_MATCHES
4553 char** res=rl_completion_matches(text,
4556 char** res=completion_matches(text,
4557 (CPFunction*)command_generator);
4559 rl_attempted_completion_over = 1;
4562 char arg[10240],word[32];
4564 if ((res = sscanf(rl_line_buffer, "%31s %10239[^;]", word, arg)) <= 0) {
4565 rl_attempted_completion_over = 1;
4569 for (i = 0; cmd_array[i].cmd; i++) {
4570 if (!strncmp(cmd_array[i].cmd, word, strlen(word))) {
4575 if(!cmd_array[i].cmd) return NULL;
4577 curret_global_list = cmd_array[i].local_tabcompletes;
4579 completerToUse = cmd_array[i].rl_completerfunction;
4580 if(completerToUse==NULL) /* if no pr. command completer is defined use the default completer */
4581 completerToUse = default_completer;
4583 if(completerToUse) {
4584 #ifdef HAVE_READLINE_RL_COMPLETION_MATCHES
4586 rl_completion_matches(text,
4590 completion_matches(text,
4591 (CPFunction*)completerToUse);
4593 if(!cmd_array[i].complete_filenames)
4594 rl_attempted_completion_over = 1;
4597 if(!cmd_array[i].complete_filenames)
4598 rl_attempted_completion_over = 1;
4608 static void client(void)
4614 #if HAVE_GETTIMEOFDAY
4615 gettimeofday (&tv_start, 0);
4620 char *line_in = NULL;
4621 #if HAVE_READLINE_READLINE_H
4624 line_in=readline(C_PROMPT);
4627 #if HAVE_READLINE_HISTORY_H
4629 add_history(line_in);
4631 strncpy(line, line_in, 10239);
4640 if (!fgets(line, 10239, stdin))
4642 if ((end_p = strchr (line, '\n')))
4645 process_cmd_line(line);
4649 static void show_version(void)
4653 yaz_version(vstr, 0);
4654 printf ("YAZ version: %s\n", YAZ_VERSION);
4655 if (strcmp(vstr, YAZ_VERSION))
4656 printf ("YAZ DLL/SO: %s\n", vstr);
4660 int main(int argc, char **argv)
4663 char *open_command = 0;
4664 char *auth_command = 0;
4669 if (!setlocale(LC_CTYPE, ""))
4670 fprintf (stderr, "setlocale failed\n");
4674 codeset = nl_langinfo(CODESET);
4678 outputCharset = xstrdup(codeset);
4680 ODR_MASK_SET(&z3950_options, Z_Options_search);
4681 ODR_MASK_SET(&z3950_options, Z_Options_present);
4682 ODR_MASK_SET(&z3950_options, Z_Options_namedResultSets);
4683 ODR_MASK_SET(&z3950_options, Z_Options_triggerResourceCtrl);
4684 ODR_MASK_SET(&z3950_options, Z_Options_scan);
4685 ODR_MASK_SET(&z3950_options, Z_Options_sort);
4686 ODR_MASK_SET(&z3950_options, Z_Options_extendedServices);
4687 ODR_MASK_SET(&z3950_options, Z_Options_delSet);
4689 while ((ret = options("k:c:q:a:b:m:v:p:u:t:Vxd:", argv, argc, &arg)) != -2)
4696 open_command = (char *) xmalloc (strlen(arg)+6);
4697 strcpy (open_command, "open ");
4698 strcat (open_command, arg);
4702 fprintf(stderr, "%s: Specify at most one server address\n",
4708 dump_file_prefix = arg;
4711 kilobytes = atoi(arg);
4714 if (!(marc_file = fopen (arg, "a")))
4721 outputCharset = xstrdup(arg);
4724 strncpy (ccl_fields, arg, sizeof(ccl_fields)-1);
4725 ccl_fields[sizeof(ccl_fields)-1] = '\0';
4728 strncpy (cql_fields, arg, sizeof(cql_fields)-1);
4729 cql_fields[sizeof(cql_fields)-1] = '\0';
4732 if (!strcmp(arg, "-"))
4735 ber_file=fopen(arg, "a");
4738 if (!strcmp(arg, "-"))
4741 apdu_file=fopen(arg, "a");
4747 yazProxy=strdup(arg);
4752 auth_command = (char *) xmalloc (strlen(arg)+6);
4753 strcpy (auth_command, "auth ");
4754 strcat (auth_command, arg);
4758 yaz_log_init(yaz_log_mask_str(arg), "", 0);
4764 fprintf (stderr, "Usage: %s "
4771 " [-p <proxy-addr>]"
4775 " [<server-addr>]\n",
4783 #ifdef HAVE_GETTIMEOFDAY
4784 gettimeofday (&tv_start, 0);
4786 process_cmd_line (auth_command);
4787 #if HAVE_READLINE_HISTORY_H
4788 add_history(auth_command);
4790 xfree(auth_command);
4794 #ifdef HAVE_GETTIMEOFDAY
4795 gettimeofday (&tv_start, 0);
4797 process_cmd_line (open_command);
4798 #if HAVE_READLINE_HISTORY_H
4799 add_history(open_command);
4801 xfree(open_command);
4809 * indent-tabs-mode: nil
4811 * vim: shiftwidth=4 tabstop=8 expandtab