2 * Copyright (C) 1995-2005, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: client.c,v 1.284 2005-06-06 10:29:33 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 static enum oid_value recordsyntax = VAL_USMARC;
104 static char *record_schema = 0;
105 static int sent_close = 0;
106 static NMEM session_mem = NULL; /* memory handle for init-response */
107 static Z_InitResponse *session = 0; /* session parameters */
108 static char last_scan_line[512] = "0";
109 static char last_scan_query[512] = "0";
110 static char ccl_fields[512] = "default.bib";
111 /* ### How can I set this path to use wherever YAZ is installed? */
112 static char cql_fields[512] = "/usr/local/share/yaz/etc/pqf.properties";
113 static char *esPackageName = 0;
114 static char *yazProxy = 0;
115 static int kilobytes = 1024;
116 static char *negotiationCharset = 0;
117 static int negotiationCharsetRecords = 1;
118 static int negotiationCharsetVersion = 3;
119 static char *outputCharset = 0;
120 static char *marcCharset = 0;
121 static char* yazLang = 0;
122 static char* http_version = "1.1";
124 static char last_cmd[32] = "?";
125 static FILE *marc_file = 0;
126 static char *refid = NULL;
127 static char *last_open_command = NULL;
128 static int auto_reconnect = 0;
129 static int auto_wait = 1;
130 static Odr_bitmask z3950_options;
131 static int z3950_version = 3;
132 static int scan_stepSize = 0;
133 static int scan_position = 1;
134 static int scan_size = 20;
135 static char cur_host[200];
145 static QueryType queryType = QueryType_Prefix;
147 static CCL_bibset bibset; /* CCL bibset handle */
148 static cql_transform_t cqltrans; /* CQL context-set handle */
150 #if HAVE_READLINE_COMPLETION_OVER
153 /* readline doesn't have this var. Define it ourselves. */
154 int rl_attempted_completion_over = 0;
157 /* set this one to 1, to avoid decode of unknown MARCs */
158 #define AVOID_MARC_DECODE 1
160 #define maxOtherInfosSupported 10
164 } extraOtherInfos[maxOtherInfosSupported];
167 void process_cmd_line(char* line);
168 char ** readline_completer(char *text, int start, int end);
169 char *command_generator(const char *text, int state);
170 char** curret_global_list=NULL;
171 int cmd_register_tab(const char* arg);
173 static void close_session (void);
175 ODR getODROutputStream()
180 const char* query_type_as_string(QueryType q)
183 case QueryType_Prefix: return "prefix (RPN sent to server)";
184 case QueryType_CCL: return "CCL (CCL sent to server) ";
185 case QueryType_CCL2RPN: return "CCL -> RPN (RPN sent to server)";
186 case QueryType_CQL: return "CQL (CQL sent to server)";
187 case QueryType_CQL2RPN: return "CQL -> RPN (RPN sent to server)";
189 return "unknown Query type internal yaz-client error";
193 static void do_hex_dump(const char* buf, int len)
198 for( i=0; i<len ; i=i+16 )
201 for(x=0 ; i+x<len && x<16; ++x)
203 printf("%2.2X ",(unsigned int)((unsigned char)buf[i+x]));
208 if (dump_file_prefix)
211 if (++no < 1000 && strlen(dump_file_prefix) < 500)
215 sprintf (fname, "%s.%03d.raw", dump_file_prefix, no);
216 of = fopen(fname, "wb");
218 fwrite (buf, 1, len, of);
225 void add_otherInfos(Z_APDU *a)
227 Z_OtherInformation **oi;
231 for(i=0; i<maxOtherInfosSupported; ++i)
233 if(extraOtherInfos[i].oidval != -1)
234 yaz_oi_set_string_oidval(oi, out, extraOtherInfos[i].oidval,
235 1, extraOtherInfos[i].value);
239 int send_apdu(Z_APDU *a)
248 z_APDU(print, &a, 0, 0);
251 if (!z_APDU(out, &a, 0, 0))
253 odr_perror(out, "Encoding APDU");
257 buf = odr_getbuf(out, &len, 0);
259 odr_dumpBER(ber_file, buf, len);
260 /* printf ("sending APDU of size %d\n", len); */
261 do_hex_dump(buf, len);
262 if (cs_put(conn, buf, len) < 0)
264 fprintf(stderr, "cs_put: %s", cs_errmsg(cs_errno(conn)));
268 odr_reset(out); /* release the APDU structure */
272 static void print_stringn(const unsigned char *buf, size_t len)
275 for (i = 0; i<len; i++)
276 if ((buf[i] <= 126 && buf[i] >= 32) || strchr ("\n\r\t\f", buf[i]))
277 printf ("%c", buf[i]);
279 printf ("\\X%02X", buf[i]);
282 static void print_refid (Z_ReferenceId *id)
286 printf ("Reference Id: ");
287 print_stringn (id->buf, id->len);
292 static Z_ReferenceId *set_refid (ODR out)
297 id = (Z_ReferenceId *) odr_malloc (out, sizeof(*id));
298 id->size = id->len = strlen(refid);
299 id->buf = (unsigned char *) odr_malloc (out, id->len);
300 memcpy (id->buf, refid, id->len);
304 /* INIT SERVICE ------------------------------- */
306 static void send_initRequest(const char* type_and_host)
308 Z_APDU *apdu = zget_APDU(out, Z_APDU_initRequest);
309 Z_InitRequest *req = apdu->u.initRequest;
312 req->options = &z3950_options;
314 ODR_MASK_ZERO(req->protocolVersion);
315 for (i = 0; i<z3950_version; i++)
316 ODR_MASK_SET(req->protocolVersion, i);
318 *req->maximumRecordSize = 1024*kilobytes;
319 *req->preferredMessageSize = 1024*kilobytes;
321 req->idAuthentication = auth;
323 req->referenceId = set_refid (out);
325 if (yazProxy && type_and_host)
326 yaz_oi_set_string_oidval(&req->otherInfo, out, VAL_PROXY,
329 if (negotiationCharset || yazLang) {
330 Z_OtherInformation **p;
331 Z_OtherInformationUnit *p0;
333 yaz_oi_APDU(apdu, &p);
335 if ((p0=yaz_oi_update(p, out, NULL, 0, 0))) {
336 ODR_MASK_SET(req->options, Z_Options_negotiationModel);
338 p0->which = Z_OtherInfo_externallyDefinedInfo;
339 p0->information.externallyDefinedInfo =
340 yaz_set_proposal_charneg(
342 (const char**)&negotiationCharset,
343 negotiationCharset ? 1 : 0,
344 (const char**)&yazLang, yazLang ? 1 : 0,
345 negotiationCharsetRecords);
350 printf("Sent initrequest.\n");
354 /* These two are used only from process_initResponse() */
355 static void render_initUserInfo(Z_OtherInformation *ui1);
356 static void render_diag(Z_DiagnosticFormat *diag);
358 static void pr_opt(const char *opt, void *clientData)
363 static int process_initResponse(Z_InitResponse *res)
366 /* save session parameters for later use */
367 session_mem = odr_extract_mem(in);
370 for (ver = 0; ver < 8; ver++)
371 if (!ODR_MASK_GET(res->protocolVersion, ver))
375 printf("Connection rejected by v%d target.\n", ver);
377 printf("Connection accepted by v%d target.\n", ver);
378 if (res->implementationId)
379 printf("ID : %s\n", res->implementationId);
380 if (res->implementationName)
381 printf("Name : %s\n", res->implementationName);
382 if (res->implementationVersion)
383 printf("Version: %s\n", res->implementationVersion);
384 if (res->userInformationField)
386 Z_External *uif = res->userInformationField;
387 if (uif->which == Z_External_userInfo1) {
388 render_initUserInfo(uif->u.userInfo1);
390 printf("UserInformationfield:\n");
391 if (!z_External(print, (Z_External**)&uif, 0, 0))
393 odr_perror(print, "Printing userinfo\n");
396 if (uif->which == Z_External_octet) {
397 printf("Guessing visiblestring:\n");
398 printf("'%.*s'\n", uif->u.octet_aligned->len,
399 uif->u.octet_aligned->buf);
401 else if (uif->which == Z_External_single)
403 Odr_any *sat = uif->u.single_ASN1_type;
404 oident *oid = oid_getentbyoid(uif->direct_reference);
405 if (oid->value == VAL_OCLCUI) {
406 Z_OCLC_UserInformation *oclc_ui;
407 ODR decode = odr_createmem(ODR_DECODE);
408 odr_setbuf(decode, sat->buf, sat->len, 0);
409 if (!z_OCLC_UserInformation(decode, &oclc_ui, 0, 0))
410 printf ("Bad OCLC UserInformation:\n");
412 printf ("OCLC UserInformation:\n");
413 if (!z_OCLC_UserInformation(print, &oclc_ui, 0, 0))
414 printf ("Bad OCLC UserInformation spec\n");
419 /* Peek at any private Init-diagnostic APDUs */
420 printf("### NAUGHTY: External is '%.*s'\n",
428 yaz_init_opt_decode(res->options, pr_opt, 0);
431 if (ODR_MASK_GET(res->options, Z_Options_namedResultSets))
434 if (ODR_MASK_GET(res->options, Z_Options_negotiationModel)) {
436 Z_CharSetandLanguageNegotiation *p =
437 yaz_get_charneg_record(res->otherInfo);
441 char *charset=NULL, *lang=NULL;
444 yaz_get_response_charneg(session_mem, p, &charset, &lang,
447 if (outputCharset && negotiationCharset) {
448 odr_set_charset (out, charset, outputCharset);
449 odr_set_charset (in, outputCharset, charset);
452 odr_set_charset (out, 0, 0);
453 odr_set_charset (in, 0, 0);
456 printf("Accepted character set : %s\n", charset);
457 printf("Accepted code language : %s\n", lang ? lang : "none");
458 printf("Accepted records in ...: %d\n", selected );
466 static void render_initUserInfo(Z_OtherInformation *ui1) {
468 printf("Init response contains %d otherInfo unit%s:\n",
469 ui1->num_elements, ui1->num_elements == 1 ? "" : "s");
471 for (i = 0; i < ui1->num_elements; i++) {
472 Z_OtherInformationUnit *unit = ui1->list[i];
473 printf(" %d: otherInfo unit contains ", i+1);
474 if (unit->which == Z_OtherInfo_externallyDefinedInfo &&
475 unit->information.externallyDefinedInfo &&
476 unit->information.externallyDefinedInfo->which ==
478 render_diag(unit->information.externallyDefinedInfo->u.diag1);
480 else if (unit->which != Z_OtherInfo_externallyDefinedInfo)
482 printf("unsupported otherInfo unit->which = %d\n", unit->which);
486 printf("unsupported otherInfo unit external %d\n",
487 unit->information.externallyDefinedInfo ?
488 unit->information.externallyDefinedInfo->which : -2);
494 /* ### should this share code with display_diagrecs()? */
495 static void render_diag(Z_DiagnosticFormat *diag) {
498 printf("%d diagnostic%s:\n", diag->num, diag->num == 1 ? "" : "s");
499 for (i = 0; i < diag->num; i++) {
500 Z_DiagnosticFormat_s *ds = diag->elements[i];
501 printf(" %d: ", i+1);
503 case Z_DiagnosticFormat_s_defaultDiagRec: {
504 Z_DefaultDiagFormat *dd = ds->u.defaultDiagRec;
505 /* ### should check `dd->diagnosticSetId' */
506 printf("code=%d (%s)", *dd->condition,
507 diagbib1_str(*dd->condition));
508 /* Both types of addinfo are the same, so use type-pun */
509 if (dd->u.v2Addinfo != 0)
510 printf(",\n\taddinfo='%s'", dd->u.v2Addinfo);
513 case Z_DiagnosticFormat_s_explicitDiagnostic:
514 printf("Explicit diagnostic (not supported)");
517 printf("Unrecognised diagnostic type %d", ds->which);
521 if (ds->message != 0)
522 printf(", message='%s'", ds->message);
528 static int set_base(const char *arg)
533 for (i = 0; i<num_databaseNames; i++)
534 xfree (databaseNames[i]);
535 num_databaseNames = 0;
539 if (!(cp = strchr(arg, ' ')))
540 cp = arg + strlen(arg);
543 databaseNames[num_databaseNames] = (char *)xmalloc (1 + cp - arg);
544 memcpy (databaseNames[num_databaseNames], arg, cp - arg);
545 databaseNames[num_databaseNames][cp - arg] = '\0';
547 for (cp1 = databaseNames[num_databaseNames]; *cp1 ; cp1++)
556 if (num_databaseNames == 0)
558 num_databaseNames = 1;
559 databaseNames[0] = xstrdup("");
564 static int cmd_base(const char *arg)
568 printf("Usage: base <database> <database> ...\n");
571 return set_base(arg);
574 void cmd_open_remember_last_open_command(const char* arg, char* new_open_command)
576 if(last_open_command != arg)
578 if(last_open_command) xfree(last_open_command);
579 last_open_command = xstrdup(new_open_command);
583 int session_connect(const char *arg)
586 char type_and_host[101];
587 const char *basep = 0;
588 #if HAVE_OPENSSL_SSL_H
597 nmem_destroy (session_mem);
601 cs_get_host_args(arg, &basep);
603 strncpy(type_and_host, arg, sizeof(type_and_host)-1);
604 type_and_host[sizeof(type_and_host)-1] = '\0';
606 cmd_open_remember_last_open_command(arg, type_and_host);
609 conn = cs_create_host(yazProxy, 1, &add);
611 conn = cs_create_host(arg, 1, &add);
614 printf ("Couldn't create comstack\n");
618 if (conn->protocol == PROTO_HTTP)
619 queryType = QueryType_CQL;
621 if (conn->protocol == PROTO_HTTP)
623 printf ("SRW/HTTP not enabled in this YAZ\n");
629 protocol = conn->protocol;
630 if (conn->protocol == PROTO_HTTP)
634 printf("Connecting...");
636 if (cs_connect(conn, add) < 0)
638 printf ("error = %s\n", cs_strerror(conn));
639 if (conn->cerrno == CSYSERR)
642 yaz_strerror(msg, sizeof(msg));
643 printf ("%s\n", msg);
650 #if HAVE_OPENSSL_SSL_H
651 if ((ssl = (SSL *) cs_get_ssl(conn)))
653 X509 *server_cert = SSL_get_peer_certificate (ssl);
659 BIO *bio = BIO_new(BIO_s_mem());
661 /* get PEM buffer in memory */
662 PEM_write_bio_X509(bio, server_cert);
663 pem_len = BIO_get_mem_data(bio, &pem_buf);
664 fwrite(pem_buf, pem_len, 1, stdout);
666 /* print all info on screen .. */
667 X509_print_fp(stdout, server_cert);
670 X509_free (server_cert);
676 if (protocol == PROTO_Z3950)
678 send_initRequest(type_and_host);
684 int cmd_open(const char *arg)
688 strncpy (cur_host, arg, sizeof(cur_host)-1);
689 cur_host[sizeof(cur_host)-1] = 0;
691 return session_connect(cur_host);
698 if(!( auto_reconnect && last_open_command) ) return ;
700 open_command = (char *) xmalloc (strlen(last_open_command)+6);
701 strcpy (open_command, "open ");
703 strcat (open_command, last_open_command);
705 process_cmd_line(open_command);
710 int cmd_authentication(const char *arg)
712 static Z_IdAuthentication au;
713 static char user[40], group[40], pass[40];
714 static Z_IdPass idPass;
719 printf("Auth field set to null\n");
723 r = sscanf (arg, "%39s %39s %39s", user, group, pass);
726 printf("Authentication set to null\n");
732 if (!strcmp(user, "-")) {
733 au.which = Z_IdAuthentication_anonymous;
734 printf("Authentication set to Anonymous\n");
736 au.which = Z_IdAuthentication_open;
738 printf("Authentication set to Open (%s)\n", user);
744 au.which = Z_IdAuthentication_idPass;
745 au.u.idPass = &idPass;
746 idPass.groupId = NULL;
747 idPass.userId = user;
748 idPass.password = group;
749 printf("Authentication set to User (%s), Pass (%s)\n", user, group);
754 au.which = Z_IdAuthentication_idPass;
755 au.u.idPass = &idPass;
756 idPass.groupId = group;
757 idPass.userId = user;
758 idPass.password = pass;
759 printf("Authentication set to User (%s), Group (%s), Pass (%s)\n",
765 /* SEARCH SERVICE ------------------------------ */
766 static void display_record(Z_External *r);
768 static void print_record(const unsigned char *buf, size_t len)
771 print_stringn (buf, len);
772 /* add newline if not already added ... */
773 if (i <= 0 || buf[i-1] != '\n')
777 static void display_record(Z_External *r)
779 oident *ent = oid_getentbyoid(r->direct_reference);
783 * Tell the user what we got.
785 if (r->direct_reference)
787 printf("Record type: ");
789 printf("%s\n", ent->desc);
790 else if (!odr_oid(print, &r->direct_reference, 0, 0))
792 odr_perror(print, "print oid");
796 /* Check if this is a known, ASN.1 type tucked away in an octet string */
797 if (ent && r->which == Z_External_octet)
799 Z_ext_typeent *type = z_ext_getentbyref(ent->value);
805 * Call the given decoder to process the record.
807 odr_setbuf(in, (char*)r->u.octet_aligned->buf,
808 r->u.octet_aligned->len, 0);
809 if (!(*type->fun)(in, &rr, 0, 0))
811 odr_perror(in, "Decoding constructed record.");
812 fprintf(stdout, "[Near %d]\n", odr_offset(in));
813 fprintf(stdout, "Packet dump:\n---------\n");
814 odr_dumpBER(stdout, (char*)r->u.octet_aligned->buf,
815 r->u.octet_aligned->len);
816 fprintf(stdout, "---------\n");
818 /* note just ignores the error ant print the bytes form the octet_aligned later */
821 * Note: we throw away the original, BER-encoded record here.
822 * Do something else with it if you want to keep it.
824 r->u.sutrs = (Z_SUTRS *) rr; /* we don't actually check the type here. */
825 r->which = type->what;
829 if (ent && ent->value == VAL_SOIF)
831 print_record((const unsigned char *) r->u.octet_aligned->buf,
832 r->u.octet_aligned->len);
834 fwrite (r->u.octet_aligned->buf, 1, r->u.octet_aligned->len, marc_file);
836 else if (r->which == Z_External_octet)
838 const char *octet_buf = (char*)r->u.octet_aligned->buf;
839 if (ent->oclass == CLASS_RECSYN &&
840 (ent->value == VAL_TEXT_XML ||
841 ent->value == VAL_APPLICATION_XML ||
842 ent->value == VAL_HTML))
844 print_record((const unsigned char *) octet_buf,
845 r->u.octet_aligned->len);
847 else if (ent->value == VAL_POSTSCRIPT)
849 int size = r->u.octet_aligned->len;
852 print_record((const unsigned char *) octet_buf, size);
857 #if AVOID_MARC_DECODE
858 /* primitive check for a marc OID 5.1-29 except 16 */
859 ent->oidsuffix[0] == 5 && ent->oidsuffix[1] < 30 &&
860 ent->oidsuffix[1] != 16
869 yaz_marc_t mt = yaz_marc_create();
871 if (yaz_marc_decode_buf(mt, octet_buf,r->u.octet_aligned->len,
875 if (marcCharset && !strcmp(marcCharset, "auto"))
877 if (ent->value == VAL_USMARC)
879 if (octet_buf[9] == 'a')
887 else if (marcCharset)
889 if (outputCharset && from)
891 cd = yaz_iconv_open(outputCharset, from);
892 printf ("convert from %s to %s", from,
895 printf (" unsupported\n");
900 fwrite (result, 1, rlen, stdout);
904 size_t inbytesleft = rlen;
905 const char *inp = result;
909 size_t outbytesleft = sizeof(outbuf);
913 r = yaz_iconv (cd, (char**) &inp,
915 &outp, &outbytesleft);
916 if (r == (size_t) (-1))
918 int e = yaz_iconv_error(cd);
919 if (e != YAZ_ICONV_E2BIG)
922 fwrite (outbuf, outp - outbuf, 1, stdout);
928 printf ("bad MARC. Dumping as it is:\n");
929 print_record((const unsigned char*) octet_buf,
930 r->u.octet_aligned->len);
932 yaz_marc_destroy(mt);
938 print_record((const unsigned char*) octet_buf,
939 r->u.octet_aligned->len);
943 fwrite (octet_buf, 1, r->u.octet_aligned->len, marc_file);
945 else if (ent && ent->value == VAL_SUTRS)
947 if (r->which != Z_External_sutrs)
949 printf("Expecting single SUTRS type for SUTRS.\n");
952 print_record(r->u.sutrs->buf, r->u.sutrs->len);
954 fwrite (r->u.sutrs->buf, 1, r->u.sutrs->len, marc_file);
956 else if (ent && ent->value == VAL_GRS1)
959 if (r->which != Z_External_grs1)
961 printf("Expecting single GRS type for GRS.\n");
965 yaz_display_grs1(w, r->u.grs1, 0);
969 else if (ent && ent->value == VAL_OPAC)
972 if (r->u.opac->bibliographicRecord)
973 display_record(r->u.opac->bibliographicRecord);
974 for (i = 0; i<r->u.opac->num_holdingsData; i++)
976 Z_HoldingsRecord *h = r->u.opac->holdingsData[i];
977 if (h->which == Z_HoldingsRecord_marcHoldingsRecord)
979 printf ("MARC holdings %d\n", i);
980 display_record(h->u.marcHoldingsRecord);
982 else if (h->which == Z_HoldingsRecord_holdingsAndCirc)
986 Z_HoldingsAndCircData *data = h->u.holdingsAndCirc;
988 printf ("Data holdings %d\n", i);
989 if (data->typeOfRecord)
990 printf ("typeOfRecord: %s\n", data->typeOfRecord);
991 if (data->encodingLevel)
992 printf ("encodingLevel: %s\n", data->encodingLevel);
993 if (data->receiptAcqStatus)
994 printf ("receiptAcqStatus: %s\n", data->receiptAcqStatus);
995 if (data->generalRetention)
996 printf ("generalRetention: %s\n", data->generalRetention);
997 if (data->completeness)
998 printf ("completeness: %s\n", data->completeness);
999 if (data->dateOfReport)
1000 printf ("dateOfReport: %s\n", data->dateOfReport);
1002 printf ("nucCode: %s\n", data->nucCode);
1003 if (data->localLocation)
1004 printf ("localLocation: %s\n", data->localLocation);
1005 if (data->shelvingLocation)
1006 printf ("shelvingLocation: %s\n", data->shelvingLocation);
1007 if (data->callNumber)
1008 printf ("callNumber: %s\n", data->callNumber);
1009 if (data->shelvingData)
1010 printf ("shelvingData: %s\n", data->shelvingData);
1011 if (data->copyNumber)
1012 printf ("copyNumber: %s\n", data->copyNumber);
1013 if (data->publicNote)
1014 printf ("publicNote: %s\n", data->publicNote);
1015 if (data->reproductionNote)
1016 printf ("reproductionNote: %s\n", data->reproductionNote);
1017 if (data->termsUseRepro)
1018 printf ("termsUseRepro: %s\n", data->termsUseRepro);
1019 if (data->enumAndChron)
1020 printf ("enumAndChron: %s\n", data->enumAndChron);
1021 for (j = 0; j<data->num_volumes; j++)
1023 printf ("volume %d\n", j);
1024 if (data->volumes[j]->enumeration)
1025 printf (" enumeration: %s\n",
1026 data->volumes[j]->enumeration);
1027 if (data->volumes[j]->chronology)
1028 printf (" chronology: %s\n",
1029 data->volumes[j]->chronology);
1030 if (data->volumes[j]->enumAndChron)
1031 printf (" enumAndChron: %s\n",
1032 data->volumes[j]->enumAndChron);
1034 for (j = 0; j<data->num_circulationData; j++)
1036 printf ("circulation %d\n", j);
1037 if (data->circulationData[j]->availableNow)
1038 printf (" availableNow: %d\n",
1039 *data->circulationData[j]->availableNow);
1040 if (data->circulationData[j]->availablityDate)
1041 printf (" availabiltyDate: %s\n",
1042 data->circulationData[j]->availablityDate);
1043 if (data->circulationData[j]->availableThru)
1044 printf (" availableThru: %s\n",
1045 data->circulationData[j]->availableThru);
1046 if (data->circulationData[j]->restrictions)
1047 printf (" restrictions: %s\n",
1048 data->circulationData[j]->restrictions);
1049 if (data->circulationData[j]->itemId)
1050 printf (" itemId: %s\n",
1051 data->circulationData[j]->itemId);
1052 if (data->circulationData[j]->renewable)
1053 printf (" renewable: %d\n",
1054 *data->circulationData[j]->renewable);
1055 if (data->circulationData[j]->onHold)
1056 printf (" onHold: %d\n",
1057 *data->circulationData[j]->onHold);
1058 if (data->circulationData[j]->enumAndChron)
1059 printf (" enumAndChron: %s\n",
1060 data->circulationData[j]->enumAndChron);
1061 if (data->circulationData[j]->midspine)
1062 printf (" midspine: %s\n",
1063 data->circulationData[j]->midspine);
1064 if (data->circulationData[j]->temporaryLocation)
1065 printf (" temporaryLocation: %s\n",
1066 data->circulationData[j]->temporaryLocation);
1073 printf("Unknown record representation.\n");
1074 if (!z_External(print, &r, 0, 0))
1076 odr_perror(print, "Printing external");
1082 static void display_diagrecs(Z_DiagRec **pp, int num)
1086 Z_DefaultDiagFormat *r;
1088 printf("Diagnostic message(s) from database:\n");
1089 for (i = 0; i<num; i++)
1091 Z_DiagRec *p = pp[i];
1092 if (p->which != Z_DiagRec_defaultFormat)
1094 printf("Diagnostic record not in default format.\n");
1098 r = p->u.defaultFormat;
1099 if (!(ent = oid_getentbyoid(r->diagnosticSetId)) ||
1100 ent->oclass != CLASS_DIAGSET || ent->value != VAL_BIB1)
1101 printf("Missing or unknown diagset\n");
1102 printf(" [%d] %s", *r->condition, diagbib1_str(*r->condition));
1105 case Z_DefaultDiagFormat_v2Addinfo:
1106 printf (" -- v2 addinfo '%s'\n", r->u.v2Addinfo);
1108 case Z_DefaultDiagFormat_v3Addinfo:
1109 printf (" -- v3 addinfo '%s'\n", r->u.v3Addinfo);
1116 static void display_nameplusrecord(Z_NamePlusRecord *p)
1118 if (p->databaseName)
1119 printf("[%s]", p->databaseName);
1120 if (p->which == Z_NamePlusRecord_surrogateDiagnostic)
1121 display_diagrecs(&p->u.surrogateDiagnostic, 1);
1122 else if (p->which == Z_NamePlusRecord_databaseRecord)
1123 display_record(p->u.databaseRecord);
1126 static void display_records(Z_Records *p)
1130 if (p->which == Z_Records_NSD)
1132 Z_DiagRec dr, *dr_p = &dr;
1133 dr.which = Z_DiagRec_defaultFormat;
1134 dr.u.defaultFormat = p->u.nonSurrogateDiagnostic;
1135 display_diagrecs (&dr_p, 1);
1137 else if (p->which == Z_Records_multipleNSD)
1138 display_diagrecs (p->u.multipleNonSurDiagnostics->diagRecs,
1139 p->u.multipleNonSurDiagnostics->num_diagRecs);
1142 printf("Records: %d\n", p->u.databaseOrSurDiagnostics->num_records);
1143 for (i = 0; i < p->u.databaseOrSurDiagnostics->num_records; i++)
1144 display_nameplusrecord(p->u.databaseOrSurDiagnostics->records[i]);
1148 static int send_deleteResultSetRequest(const char *arg)
1153 Z_APDU *apdu = zget_APDU(out, Z_APDU_deleteResultSetRequest);
1154 Z_DeleteResultSetRequest *req = apdu->u.deleteResultSetRequest;
1156 req->referenceId = set_refid (out);
1158 req->num_resultSetList =
1159 sscanf (arg, "%30s %30s %30s %30s %30s %30s %30s %30s",
1160 names[0], names[1], names[2], names[3],
1161 names[4], names[5], names[6], names[7]);
1163 req->deleteFunction = (int *)
1164 odr_malloc (out, sizeof(*req->deleteFunction));
1165 if (req->num_resultSetList > 0)
1167 *req->deleteFunction = Z_DeleteResultSetRequest_list;
1168 req->resultSetList = (char **)
1169 odr_malloc (out, sizeof(*req->resultSetList)*
1170 req->num_resultSetList);
1171 for (i = 0; i<req->num_resultSetList; i++)
1172 req->resultSetList[i] = names[i];
1176 *req->deleteFunction = Z_DeleteResultSetRequest_all;
1177 req->resultSetList = 0;
1181 printf("Sent deleteResultSetRequest.\n");
1186 static int send_srw(Z_SRW_PDU *sr)
1188 const char *charset = negotiationCharset;
1189 const char *host_port = cur_host;
1192 Z_SOAP_Handler h[2] = {
1193 {"http://www.loc.gov/zing/srw/", 0, (Z_SOAP_fun) yaz_srw_codec},
1196 ODR o = odr_createmem(ODR_ENCODE);
1198 Z_SOAP *p = odr_malloc(o, sizeof(*p));
1201 path = odr_malloc(out, strlen(databaseNames[0])+2);
1203 strcpy(path+1, databaseNames[0]);
1205 gdu = z_get_HTTP_Request(out);
1206 gdu->u.HTTP_Request->version = http_version;
1207 gdu->u.HTTP_Request->path = odr_strdup(out, path);
1211 const char *cp0 = strstr(host_port, "://");
1212 const char *cp1 = 0;
1218 cp1 = strchr(cp0, '/');
1220 cp1 = cp0+strlen(cp0);
1224 char *h = odr_malloc(out, cp1 - cp0 + 1);
1225 memcpy (h, cp0, cp1 - cp0);
1227 z_HTTP_header_add(out, &gdu->u.HTTP_Request->headers,
1232 strcpy(ctype, "text/xml");
1233 if (charset && strlen(charset) < 20)
1235 strcat(ctype, "; charset=");
1236 strcat(ctype, charset);
1238 z_HTTP_header_add(out, &gdu->u.HTTP_Request->headers,
1239 "Content-Type", ctype);
1240 z_HTTP_header_add(out, &gdu->u.HTTP_Request->headers,
1241 "SOAPAction", "\"\"");
1242 p->which = Z_SOAP_generic;
1243 p->u.generic = odr_malloc(o, sizeof(*p->u.generic));
1244 p->u.generic->no = 0;
1245 p->u.generic->ns = 0;
1246 p->u.generic->p = sr;
1247 p->ns = "http://schemas.xmlsoap.org/soap/envelope/";
1249 ret = z_soap_codec_enc(o, &p,
1250 &gdu->u.HTTP_Request->content_buf,
1251 &gdu->u.HTTP_Request->content_len, h,
1254 if (z_GDU(out, &gdu, 0, 0))
1262 if (!z_GDU(print, &gdu, 0, 0))
1263 printf ("Failed to print outgoing APDU\n");
1266 buf_out = odr_getbuf(out, &len_out, 0);
1268 /* we don't odr_reset(out), since we may need the buffer again */
1270 do_hex_dump(buf_out, len_out);
1272 r = cs_put(conn, buf_out, len_out);
1284 static char *encode_SRW_term(ODR o, const char *q)
1286 const char *in_charset = "ISO-8859-1";
1287 WRBUF w = wrbuf_alloc();
1291 in_charset = outputCharset;
1292 cd = yaz_iconv_open("UTF-8", in_charset);
1296 return odr_strdup(o, q);
1298 wrbuf_iconv_write(w, cd, q, strlen(q));
1301 int len = wrbuf_len(w);
1302 res = odr_strdupn(o, wrbuf_buf(w), len);
1305 res = odr_strdup(o, q);
1306 yaz_iconv_close(cd);
1312 static int send_SRW_scanRequest(const char *arg, int pos, int num)
1316 /* regular requestse .. */
1317 sr = yaz_srw_get(out, Z_SRW_scan_request);
1322 sr->u.scan_request->query_type = Z_SRW_query_type_cql;
1323 sr->u.scan_request->scanClause.cql = encode_SRW_term(out, arg);
1325 case QueryType_Prefix:
1326 sr->u.scan_request->query_type = Z_SRW_query_type_pqf;
1327 sr->u.scan_request->scanClause.pqf = encode_SRW_term(out, arg);
1330 printf ("Only CQL and PQF supported in SRW\n");
1333 sr->u.scan_request->responsePosition = odr_intdup(out, pos);
1334 sr->u.scan_request->maximumTerms = odr_intdup(out, num);
1335 return send_srw(sr);
1338 static int send_SRW_searchRequest(const char *arg)
1344 assert(srw_sr_odr_out == 0);
1345 srw_sr_odr_out = odr_createmem(ODR_ENCODE);
1347 odr_reset(srw_sr_odr_out);
1351 /* save this for later .. when fetching individual records */
1352 srw_sr = yaz_srw_get(srw_sr_odr_out, Z_SRW_searchRetrieve_request);
1354 /* regular request .. */
1355 sr = yaz_srw_get(out, Z_SRW_searchRetrieve_request);
1360 srw_sr->u.request->query_type = Z_SRW_query_type_cql;
1361 srw_sr->u.request->query.cql = encode_SRW_term(srw_sr_odr_out, arg);
1363 sr->u.request->query_type = Z_SRW_query_type_cql;
1364 sr->u.request->query.cql = encode_SRW_term(srw_sr_odr_out, arg);
1366 case QueryType_Prefix:
1367 srw_sr->u.request->query_type = Z_SRW_query_type_pqf;
1368 srw_sr->u.request->query.pqf = encode_SRW_term(srw_sr_odr_out, arg);
1370 sr->u.request->query_type = Z_SRW_query_type_pqf;
1371 sr->u.request->query.pqf = encode_SRW_term(srw_sr_odr_out, arg);
1374 printf ("Only CQL and PQF supported in SRW\n");
1377 sr->u.request->maximumRecords = odr_intdup(out, 0);
1380 sr->u.request->recordSchema = record_schema;
1381 if (recordsyntax == VAL_TEXT_XML)
1382 sr->u.explain_request->recordPacking = "xml";
1383 return send_srw(sr);
1387 static int send_searchRequest(const char *arg)
1389 Z_APDU *apdu = zget_APDU(out, Z_APDU_searchRequest);
1390 Z_SearchRequest *req = apdu->u.searchRequest;
1392 struct ccl_rpn_node *rpn = NULL;
1394 char setstring[100];
1395 Z_RPNQuery *RPNquery;
1397 YAZ_PQF_Parser pqf_parser;
1399 QueryType myQueryType = queryType;
1402 if (myQueryType == QueryType_CCL2RPN)
1404 rpn = ccl_find_str(bibset, arg, &error, &pos);
1407 printf("CCL ERROR: %s\n", ccl_err_msg(error));
1410 } else if (myQueryType == QueryType_CQL2RPN) {
1411 /* ### All this code should be wrapped in a utility function */
1413 struct cql_node *node;
1414 const char *addinfo;
1415 if (cqltrans == 0) {
1416 printf("Can't use CQL: no translation file. Try set_cqlfile\n");
1419 parser = cql_parser_create();
1420 if ((error = cql_parser_string(parser, arg)) != 0) {
1421 printf("Can't parse CQL: must be a syntax error\n");
1424 node = cql_parser_result(parser);
1425 if ((error = cql_transform_buf(cqltrans, node, pqfbuf,
1426 sizeof pqfbuf)) != 0) {
1427 error = cql_transform_error(cqltrans, &addinfo);
1428 printf ("Can't convert CQL to PQF: %s (addinfo=%s)\n",
1429 cql_strerror(error), addinfo);
1433 myQueryType = QueryType_Prefix;
1436 req->referenceId = set_refid (out);
1437 if (!strcmp(arg, "@big")) /* strictly for troublemaking */
1439 static unsigned char big[2100];
1440 static Odr_oct bigo;
1442 /* send a very big referenceid to test transport stack etc. */
1443 memset(big, 'A', 2100);
1444 bigo.len = bigo.size = 2100;
1446 req->referenceId = &bigo;
1451 sprintf(setstring, "%d", ++setnumber);
1452 req->resultSetName = setstring;
1454 *req->smallSetUpperBound = smallSetUpperBound;
1455 *req->largeSetLowerBound = largeSetLowerBound;
1456 *req->mediumSetPresentNumber = mediumSetPresentNumber;
1457 if (smallSetUpperBound > 0 || (largeSetLowerBound > 1 &&
1458 mediumSetPresentNumber > 0))
1460 req->preferredRecordSyntax =
1461 yaz_oidval_to_z3950oid(out, CLASS_RECSYN, recordsyntax);
1463 req->smallSetElementSetNames =
1464 req->mediumSetElementSetNames = elementSetNames;
1466 req->num_databaseNames = num_databaseNames;
1467 req->databaseNames = databaseNames;
1469 req->query = &query;
1471 switch (myQueryType)
1473 case QueryType_Prefix:
1474 query.which = Z_Query_type_1;
1475 pqf_parser = yaz_pqf_create ();
1476 RPNquery = yaz_pqf_parse (pqf_parser, out, arg);
1479 const char *pqf_msg;
1481 int code = yaz_pqf_error (pqf_parser, &pqf_msg, &off);
1482 printf("%*s^\n", off+4, "");
1483 printf("Prefix query error: %s (code %d)\n", pqf_msg, code);
1485 yaz_pqf_destroy (pqf_parser);
1488 yaz_pqf_destroy (pqf_parser);
1489 query.u.type_1 = RPNquery;
1492 query.which = Z_Query_type_2;
1493 query.u.type_2 = &ccl_query;
1494 ccl_query.buf = (unsigned char*) arg;
1495 ccl_query.len = strlen(arg);
1497 case QueryType_CCL2RPN:
1498 query.which = Z_Query_type_1;
1499 RPNquery = ccl_rpn_query(out, rpn);
1502 printf ("Couldn't convert from CCL to RPN\n");
1505 query.u.type_1 = RPNquery;
1506 ccl_rpn_delete (rpn);
1509 query.which = Z_Query_type_104;
1510 ext = (Z_External *) odr_malloc(out, sizeof(*ext));
1511 ext->direct_reference = odr_getoidbystr(out, "1.2.840.10003.16.2");
1512 ext->indirect_reference = 0;
1513 ext->descriptor = 0;
1514 ext->which = Z_External_CQL;
1515 ext->u.cql = odr_strdup(out, arg);
1516 query.u.type_104 = ext;
1519 printf ("Unsupported query type\n");
1522 if (send_apdu(apdu))
1523 printf("Sent searchRequest.\n");
1528 /* display Query Expression as part of searchResult-1 */
1529 static void display_queryExpression (Z_QueryExpression *qe)
1533 if (qe->which == Z_QueryExpression_term)
1535 if (qe->u.term->queryTerm)
1537 Z_Term *term = qe->u.term->queryTerm;
1538 switch (term->which)
1540 case Z_Term_general:
1541 printf (" %.*s", term->u.general->len, term->u.general->buf);
1543 case Z_Term_characterString:
1544 printf (" %s", term->u.characterString);
1546 case Z_Term_numeric:
1547 printf (" %d", *term->u.numeric);
1557 /* see if we can find USR:SearchResult-1 */
1558 static void display_searchResult (Z_OtherInformation *o)
1563 for (i = 0; i < o->num_elements; i++)
1565 if (o->list[i]->which == Z_OtherInfo_externallyDefinedInfo)
1567 Z_External *ext = o->list[i]->information.externallyDefinedInfo;
1569 if (ext->which == Z_External_searchResult1)
1572 Z_SearchInfoReport *sr = ext->u.searchResult1;
1573 printf ("SearchResult-1:");
1574 for (j = 0; j < sr->num; j++)
1576 if (!sr->elements[j]->subqueryExpression)
1578 display_queryExpression (
1579 sr->elements[j]->subqueryExpression);
1580 display_queryExpression (
1581 sr->elements[j]->subqueryInterpretation);
1582 display_queryExpression (
1583 sr->elements[j]->subqueryRecommendation);
1584 if (sr->elements[j]->subqueryCount)
1585 printf ("(%d)", *sr->elements[j]->subqueryCount);
1593 static int process_searchResponse(Z_SearchResponse *res)
1595 printf ("Received SearchResponse.\n");
1596 print_refid (res->referenceId);
1597 if (*res->searchStatus)
1598 printf("Search was a success.\n");
1600 printf("Search was a bloomin' failure.\n");
1601 printf("Number of hits: %d", *res->resultCount);
1603 printf (", setno %d", setnumber);
1605 display_searchResult (res->additionalSearchInfo);
1606 printf("records returned: %d\n",
1607 *res->numberOfRecordsReturned);
1608 setno += *res->numberOfRecordsReturned;
1610 display_records(res->records);
1614 static void print_level(int iLevel)
1617 for (i = 0; i < iLevel * 4; i++)
1621 static void print_int(int iLevel, const char *pTag, int *pInt)
1625 print_level(iLevel);
1626 printf("%s: %d\n", pTag, *pInt);
1630 static void print_string(int iLevel, const char *pTag, const char *pString)
1632 if (pString != NULL)
1634 print_level(iLevel);
1635 printf("%s: %s\n", pTag, pString);
1639 static void print_oid(int iLevel, const char *pTag, Odr_oid *pOid)
1645 print_level(iLevel);
1646 printf("%s:", pTag);
1647 for (; *pInt != -1; pInt++)
1648 printf(" %d", *pInt);
1653 static void print_referenceId(int iLevel, Z_ReferenceId *referenceId)
1655 if (referenceId != NULL)
1659 print_level(iLevel);
1660 printf("Ref Id (%d, %d): ", referenceId->len, referenceId->size);
1661 for (i = 0; i < referenceId->len; i++)
1662 printf("%c", referenceId->buf[i]);
1667 static void print_string_or_numeric(int iLevel, const char *pTag, Z_StringOrNumeric *pStringNumeric)
1669 if (pStringNumeric != NULL)
1671 switch (pStringNumeric->which)
1673 case Z_StringOrNumeric_string:
1674 print_string(iLevel, pTag, pStringNumeric->u.string);
1677 case Z_StringOrNumeric_numeric:
1678 print_int(iLevel, pTag, pStringNumeric->u.numeric);
1682 print_level(iLevel);
1683 printf("%s: valid type for Z_StringOrNumeric\n", pTag);
1689 static void print_universe_report_duplicate(
1691 Z_UniverseReportDuplicate *pUniverseReportDuplicate)
1693 if (pUniverseReportDuplicate != NULL)
1695 print_level(iLevel);
1696 printf("Universe Report Duplicate: \n");
1698 print_string_or_numeric(iLevel, "Hit No",
1699 pUniverseReportDuplicate->hitno);
1703 static void print_universe_report_hits(
1705 Z_UniverseReportHits *pUniverseReportHits)
1707 if (pUniverseReportHits != NULL)
1709 print_level(iLevel);
1710 printf("Universe Report Hits: \n");
1712 print_string_or_numeric(iLevel, "Database",
1713 pUniverseReportHits->database);
1714 print_string_or_numeric(iLevel, "Hits", pUniverseReportHits->hits);
1718 static void print_universe_report(int iLevel, Z_UniverseReport *pUniverseReport)
1720 if (pUniverseReport != NULL)
1722 print_level(iLevel);
1723 printf("Universe Report: \n");
1725 print_int(iLevel, "Total Hits", pUniverseReport->totalHits);
1726 switch (pUniverseReport->which)
1728 case Z_UniverseReport_databaseHits:
1729 print_universe_report_hits(iLevel,
1730 pUniverseReport->u.databaseHits);
1733 case Z_UniverseReport_duplicate:
1734 print_universe_report_duplicate(iLevel,
1735 pUniverseReport->u.duplicate);
1739 print_level(iLevel);
1740 printf("Type: %d\n", pUniverseReport->which);
1746 static void print_external(int iLevel, Z_External *pExternal)
1748 if (pExternal != NULL)
1750 print_level(iLevel);
1751 printf("External: \n");
1753 print_oid(iLevel, "Direct Reference", pExternal->direct_reference);
1754 print_int(iLevel, "InDirect Reference", pExternal->indirect_reference);
1755 print_string(iLevel, "Descriptor", pExternal->descriptor);
1756 switch (pExternal->which)
1758 case Z_External_universeReport:
1759 print_universe_report(iLevel, pExternal->u.universeReport);
1763 print_level(iLevel);
1764 printf("Type: %d\n", pExternal->which);
1770 static int process_resourceControlRequest (Z_ResourceControlRequest *req)
1772 printf ("Received ResourceControlRequest.\n");
1773 print_referenceId(1, req->referenceId);
1774 print_int(1, "Suspended Flag", req->suspendedFlag);
1775 print_int(1, "Partial Results Available", req->partialResultsAvailable);
1776 print_int(1, "Response Required", req->responseRequired);
1777 print_int(1, "Triggered Request Flag", req->triggeredRequestFlag);
1778 print_external(1, req->resourceReport);
1782 void process_ESResponse(Z_ExtendedServicesResponse *res)
1785 switch (*res->operationStatus)
1787 case Z_ExtendedServicesResponse_done:
1790 case Z_ExtendedServicesResponse_accepted:
1791 printf ("accepted\n");
1793 case Z_ExtendedServicesResponse_failure:
1794 printf ("failure\n");
1795 display_diagrecs(res->diagnostics, res->num_diagnostics);
1798 printf ("unknown\n");
1800 if ( (*res->operationStatus != Z_ExtendedServicesResponse_failure) &&
1801 (res->num_diagnostics != 0) ) {
1802 display_diagrecs(res->diagnostics, res->num_diagnostics);
1804 print_refid (res->referenceId);
1805 if (res->taskPackage &&
1806 res->taskPackage->which == Z_External_extendedService)
1808 Z_TaskPackage *taskPackage = res->taskPackage->u.extendedService;
1809 Odr_oct *id = taskPackage->targetReference;
1810 Z_External *ext = taskPackage->taskSpecificParameters;
1814 printf ("Target Reference: ");
1815 print_stringn (id->buf, id->len);
1818 if (ext->which == Z_External_update)
1820 Z_IUUpdateTaskPackage *utp = ext->u.update->u.taskPackage;
1821 if (utp && utp->targetPart)
1823 Z_IUTargetPart *targetPart = utp->targetPart;
1826 for (i = 0; i<targetPart->num_taskPackageRecords; i++)
1829 Z_IUTaskPackageRecordStructure *tpr =
1830 targetPart->taskPackageRecords[i];
1831 printf ("task package record %d\n", i+1);
1832 if (tpr->which == Z_IUTaskPackageRecordStructure_record)
1834 display_record (tpr->u.record);
1838 printf ("other type\n");
1844 if (res->taskPackage && res->taskPackage->which == Z_External_octet)
1846 Odr_oct *doc = res->taskPackage->u.octet_aligned;
1847 printf("%.*s\n", doc->len, doc->buf);
1851 const char *get_ill_element (void *clientData, const char *element)
1856 static Z_External *create_external_itemRequest()
1858 struct ill_get_ctl ctl;
1859 ILL_ItemRequest *req;
1861 int item_request_size = 0;
1862 char *item_request_buf = 0;
1866 ctl.f = get_ill_element;
1868 req = ill_get_ItemRequest(&ctl, "ill", 0);
1870 printf ("ill_get_ItemRequest failed\n");
1872 if (!ill_ItemRequest (out, &req, 0, 0))
1876 ill_ItemRequest(print, &req, 0, 0);
1879 item_request_buf = odr_getbuf (out, &item_request_size, 0);
1880 if (item_request_buf)
1881 odr_setbuf (out, item_request_buf, item_request_size, 1);
1882 printf ("Couldn't encode ItemRequest, size %d\n", item_request_size);
1889 item_request_buf = odr_getbuf (out, &item_request_size, 0);
1890 oid.proto = PROTO_GENERAL;
1891 oid.oclass = CLASS_GENERAL;
1892 oid.value = VAL_ISO_ILL_1;
1894 r = (Z_External *) odr_malloc (out, sizeof(*r));
1895 r->direct_reference = odr_oiddup(out,oid_getoidbyent(&oid));
1896 r->indirect_reference = 0;
1898 r->which = Z_External_single;
1900 r->u.single_ASN1_type = (Odr_oct *)
1901 odr_malloc (out, sizeof(*r->u.single_ASN1_type));
1902 r->u.single_ASN1_type->buf = (unsigned char *)
1903 odr_malloc (out, item_request_size);
1904 r->u.single_ASN1_type->len = item_request_size;
1905 r->u.single_ASN1_type->size = item_request_size;
1906 memcpy (r->u.single_ASN1_type->buf, item_request_buf,
1909 do_hex_dump(item_request_buf,item_request_size);
1914 static Z_External *create_external_ILL_APDU(int which)
1916 struct ill_get_ctl ctl;
1919 int ill_request_size = 0;
1920 char *ill_request_buf = 0;
1924 ctl.f = get_ill_element;
1926 ill_apdu = ill_get_APDU(&ctl, "ill", 0);
1928 if (!ill_APDU (out, &ill_apdu, 0, 0))
1932 printf ("-------------------\n");
1933 ill_APDU(print, &ill_apdu, 0, 0);
1935 printf ("-------------------\n");
1937 ill_request_buf = odr_getbuf (out, &ill_request_size, 0);
1938 if (ill_request_buf)
1939 odr_setbuf (out, ill_request_buf, ill_request_size, 1);
1940 printf ("Couldn't encode ILL-Request, size %d\n", ill_request_size);
1946 ill_request_buf = odr_getbuf (out, &ill_request_size, 0);
1948 oid.proto = PROTO_GENERAL;
1949 oid.oclass = CLASS_GENERAL;
1950 oid.value = VAL_ISO_ILL_1;
1952 r = (Z_External *) odr_malloc (out, sizeof(*r));
1953 r->direct_reference = odr_oiddup(out,oid_getoidbyent(&oid));
1954 r->indirect_reference = 0;
1956 r->which = Z_External_single;
1958 r->u.single_ASN1_type = (Odr_oct *)
1959 odr_malloc (out, sizeof(*r->u.single_ASN1_type));
1960 r->u.single_ASN1_type->buf = (unsigned char *)
1961 odr_malloc (out, ill_request_size);
1962 r->u.single_ASN1_type->len = ill_request_size;
1963 r->u.single_ASN1_type->size = ill_request_size;
1964 memcpy (r->u.single_ASN1_type->buf, ill_request_buf, ill_request_size);
1965 /* printf ("len = %d\n", ill_request_size); */
1966 /* do_hex_dump(ill_request_buf,ill_request_size); */
1967 /* printf("--- end of extenal\n"); */
1974 static Z_External *create_ItemOrderExternal(const char *type, int itemno)
1976 Z_External *r = (Z_External *) odr_malloc(out, sizeof(Z_External));
1977 oident ItemOrderRequest;
1979 ItemOrderRequest.proto = PROTO_Z3950;
1980 ItemOrderRequest.oclass = CLASS_EXTSERV;
1981 ItemOrderRequest.value = VAL_ITEMORDER;
1983 r->direct_reference = odr_oiddup(out,oid_getoidbyent(&ItemOrderRequest));
1984 r->indirect_reference = 0;
1987 r->which = Z_External_itemOrder;
1989 r->u.itemOrder = (Z_ItemOrder *) odr_malloc(out,sizeof(Z_ItemOrder));
1990 memset(r->u.itemOrder, 0, sizeof(Z_ItemOrder));
1991 r->u.itemOrder->which=Z_IOItemOrder_esRequest;
1993 r->u.itemOrder->u.esRequest = (Z_IORequest *)
1994 odr_malloc(out,sizeof(Z_IORequest));
1995 memset(r->u.itemOrder->u.esRequest, 0, sizeof(Z_IORequest));
1997 r->u.itemOrder->u.esRequest->toKeep = (Z_IOOriginPartToKeep *)
1998 odr_malloc(out,sizeof(Z_IOOriginPartToKeep));
1999 memset(r->u.itemOrder->u.esRequest->toKeep, 0, sizeof(Z_IOOriginPartToKeep));
2000 r->u.itemOrder->u.esRequest->notToKeep = (Z_IOOriginPartNotToKeep *)
2001 odr_malloc(out,sizeof(Z_IOOriginPartNotToKeep));
2002 memset(r->u.itemOrder->u.esRequest->notToKeep, 0, sizeof(Z_IOOriginPartNotToKeep));
2004 r->u.itemOrder->u.esRequest->toKeep->supplDescription = NULL;
2005 r->u.itemOrder->u.esRequest->toKeep->contact = NULL;
2006 r->u.itemOrder->u.esRequest->toKeep->addlBilling = NULL;
2008 r->u.itemOrder->u.esRequest->notToKeep->resultSetItem =
2009 (Z_IOResultSetItem *) odr_malloc(out, sizeof(Z_IOResultSetItem));
2010 memset(r->u.itemOrder->u.esRequest->notToKeep->resultSetItem, 0, sizeof(Z_IOResultSetItem));
2011 r->u.itemOrder->u.esRequest->notToKeep->resultSetItem->resultSetId = "1";
2013 r->u.itemOrder->u.esRequest->notToKeep->resultSetItem->item =
2014 (int *) odr_malloc(out, sizeof(int));
2015 *r->u.itemOrder->u.esRequest->notToKeep->resultSetItem->item = itemno;
2017 if (!strcmp (type, "item") || !strcmp(type, "2"))
2019 printf ("using item-request\n");
2020 r->u.itemOrder->u.esRequest->notToKeep->itemRequest =
2021 create_external_itemRequest();
2023 else if (!strcmp(type, "ill") || !strcmp(type, "1"))
2025 printf ("using ILL-request\n");
2026 r->u.itemOrder->u.esRequest->notToKeep->itemRequest =
2027 create_external_ILL_APDU(ILL_APDU_ILL_Request);
2029 else if (!strcmp(type, "xml") || !strcmp(type, "3"))
2031 const char *xml_buf =
2033 " <type>request</type>\n"
2034 " <libraryNo>000200</libraryNo>\n"
2035 " <borrowerTicketNo> 1212 </borrowerTicketNo>\n"
2037 r->u.itemOrder->u.esRequest->notToKeep->itemRequest =
2038 z_ext_record (out, VAL_TEXT_XML, xml_buf, strlen(xml_buf));
2041 r->u.itemOrder->u.esRequest->notToKeep->itemRequest = 0;
2046 static int send_itemorder(const char *type, int itemno)
2048 Z_APDU *apdu = zget_APDU(out, Z_APDU_extendedServicesRequest);
2049 Z_ExtendedServicesRequest *req = apdu->u.extendedServicesRequest;
2050 oident ItemOrderRequest;
2052 ItemOrderRequest.proto = PROTO_Z3950;
2053 ItemOrderRequest.oclass = CLASS_EXTSERV;
2054 ItemOrderRequest.value = VAL_ITEMORDER;
2055 req->packageType = odr_oiddup(out,oid_getoidbyent(&ItemOrderRequest));
2056 req->packageName = esPackageName;
2058 req->taskSpecificParameters = create_ItemOrderExternal(type, itemno);
2064 static int only_z3950()
2066 if (protocol == PROTO_HTTP)
2068 printf ("Not supported by SRW\n");
2074 static int cmd_update_common(const char *arg, int version);
2076 static int cmd_update(const char *arg)
2078 return cmd_update_common(arg, 1);
2081 static int cmd_update0(const char *arg)
2083 return cmd_update_common(arg, 0);
2086 static int cmd_update_common(const char *arg, int version)
2088 Z_APDU *apdu = zget_APDU(out, Z_APDU_extendedServicesRequest );
2089 Z_ExtendedServicesRequest *req = apdu->u.extendedServicesRequest;
2091 char action[20], recid[20], fname[80];
2093 Z_External *record_this = 0;
2100 sscanf (arg, "%19s %19s %79s", action, recid, fname);
2102 if (!strcmp (action, "insert"))
2103 action_no = Z_IUOriginPartToKeep_recordInsert;
2104 else if (!strcmp (action, "replace"))
2105 action_no = Z_IUOriginPartToKeep_recordReplace;
2106 else if (!strcmp (action, "delete"))
2107 action_no = Z_IUOriginPartToKeep_recordDelete;
2108 else if (!strcmp (action, "update"))
2109 action_no = Z_IUOriginPartToKeep_specialUpdate;
2112 printf ("Bad action: %s\n", action);
2113 printf ("Possible values: insert, replace, delete, update\n");
2121 stat (fname, &status);
2122 if (S_ISREG(status.st_mode) && (inf = fopen(fname, "rb")))
2124 size_t len = status.st_size;
2125 char *buf = (char *) xmalloc (len);
2127 fread (buf, 1, len, inf);
2131 record_this = z_ext_record (out, VAL_TEXT_XML, buf, len);
2137 printf ("File %s doesn't exist\n", fname);
2145 printf ("No last record (update ignored)\n");
2148 record_this = record_last;
2152 yaz_oidval_to_z3950oid(out, CLASS_EXTSERV,
2153 version == 0 ? VAL_DBUPDATE0 : VAL_DBUPDATE);
2155 req->packageName = esPackageName;
2157 req->referenceId = set_refid (out);
2159 r = req->taskSpecificParameters = (Z_External *)
2160 odr_malloc (out, sizeof(*r));
2161 r->direct_reference = req->packageType;
2162 r->indirect_reference = 0;
2166 Z_IU0OriginPartToKeep *toKeep;
2167 Z_IU0SuppliedRecords *notToKeep;
2169 r->which = Z_External_update0;
2170 r->u.update0 = (Z_IU0Update *) odr_malloc(out, sizeof(*r->u.update0));
2171 r->u.update0->which = Z_IUUpdate_esRequest;
2172 r->u.update0->u.esRequest = (Z_IU0UpdateEsRequest *)
2173 odr_malloc(out, sizeof(*r->u.update0->u.esRequest));
2174 toKeep = r->u.update0->u.esRequest->toKeep = (Z_IU0OriginPartToKeep *)
2175 odr_malloc(out, sizeof(*r->u.update0->u.esRequest->toKeep));
2177 toKeep->databaseName = databaseNames[0];
2179 toKeep->elementSetName = 0;
2181 toKeep->action = (int *) odr_malloc(out, sizeof(*toKeep->action));
2182 *toKeep->action = action_no;
2184 notToKeep = r->u.update0->u.esRequest->notToKeep = (Z_IU0SuppliedRecords *)
2185 odr_malloc(out, sizeof(*r->u.update0->u.esRequest->notToKeep));
2187 notToKeep->elements = (Z_IU0SuppliedRecords_elem **)
2188 odr_malloc(out, sizeof(*notToKeep->elements));
2189 notToKeep->elements[0] = (Z_IU0SuppliedRecords_elem *)
2190 odr_malloc(out, sizeof(**notToKeep->elements));
2191 notToKeep->elements[0]->which = Z_IUSuppliedRecords_elem_opaque;
2194 notToKeep->elements[0]->u.opaque = (Odr_oct *)
2195 odr_malloc (out, sizeof(Odr_oct));
2196 notToKeep->elements[0]->u.opaque->buf = (unsigned char *) recid;
2197 notToKeep->elements[0]->u.opaque->size = strlen(recid);
2198 notToKeep->elements[0]->u.opaque->len = strlen(recid);
2201 notToKeep->elements[0]->u.opaque = 0;
2202 notToKeep->elements[0]->supplementalId = 0;
2203 notToKeep->elements[0]->correlationInfo = 0;
2204 notToKeep->elements[0]->record = record_this;
2208 Z_IUOriginPartToKeep *toKeep;
2209 Z_IUSuppliedRecords *notToKeep;
2211 r->which = Z_External_update;
2212 r->u.update = (Z_IUUpdate *) odr_malloc(out, sizeof(*r->u.update));
2213 r->u.update->which = Z_IUUpdate_esRequest;
2214 r->u.update->u.esRequest = (Z_IUUpdateEsRequest *)
2215 odr_malloc(out, sizeof(*r->u.update->u.esRequest));
2216 toKeep = r->u.update->u.esRequest->toKeep = (Z_IUOriginPartToKeep *)
2217 odr_malloc(out, sizeof(*r->u.update->u.esRequest->toKeep));
2219 toKeep->databaseName = databaseNames[0];
2221 toKeep->elementSetName = 0;
2222 toKeep->actionQualifier = 0;
2223 toKeep->action = (int *) odr_malloc(out, sizeof(*toKeep->action));
2224 *toKeep->action = action_no;
2226 notToKeep = r->u.update->u.esRequest->notToKeep = (Z_IUSuppliedRecords *)
2227 odr_malloc(out, sizeof(*r->u.update->u.esRequest->notToKeep));
2229 notToKeep->elements = (Z_IUSuppliedRecords_elem **)
2230 odr_malloc(out, sizeof(*notToKeep->elements));
2231 notToKeep->elements[0] = (Z_IUSuppliedRecords_elem *)
2232 odr_malloc(out, sizeof(**notToKeep->elements));
2233 notToKeep->elements[0]->which = Z_IUSuppliedRecords_elem_opaque;
2236 notToKeep->elements[0]->u.opaque = (Odr_oct *)
2237 odr_malloc (out, sizeof(Odr_oct));
2238 notToKeep->elements[0]->u.opaque->buf = (unsigned char *) recid;
2239 notToKeep->elements[0]->u.opaque->size = strlen(recid);
2240 notToKeep->elements[0]->u.opaque->len = strlen(recid);
2243 notToKeep->elements[0]->u.opaque = 0;
2244 notToKeep->elements[0]->supplementalId = 0;
2245 notToKeep->elements[0]->correlationInfo = 0;
2246 notToKeep->elements[0]->record = record_this;
2254 static int cmd_xmlupdate(const char *arg)
2256 Z_APDU *apdu = zget_APDU(out, Z_APDU_extendedServicesRequest);
2257 Z_ExtendedServicesRequest *req = apdu->u.extendedServicesRequest;
2259 req->packageType = yaz_oidval_to_z3950oid(out, CLASS_EXTSERV,
2261 Z_External *ext = (Z_External *) odr_malloc(out, sizeof(*ext));
2262 req->taskSpecificParameters = ext;
2263 ext->direct_reference = req->packageType;
2264 ext->descriptor = 0;
2265 ext->indirect_reference = 0;
2267 ext->which = Z_External_octet;
2268 ext->u.single_ASN1_type = (Odr_oct *) odr_malloc (out, sizeof(Odr_oct));
2270 ext->u.single_ASN1_type->buf = (unsigned char*) odr_strdup(out, arg);
2271 ext->u.single_ASN1_type->size = ext->u.single_ASN1_type->len = strlen(arg);
2277 static int cmd_itemorder(const char *arg)
2284 if (sscanf (arg, "%10s %d", type, &itemno) != 2)
2287 printf("Item order request\n");
2289 send_itemorder(type, itemno);
2293 static void show_opt(const char *arg, void *clientData)
2295 printf ("%s ", arg);
2298 static int cmd_zversion(const char *arg)
2301 z3950_version = atoi(arg);
2303 printf ("version is %d\n", z3950_version);
2307 static int cmd_options(const char *arg)
2313 r = yaz_init_opt_encode(&z3950_options, arg, &pos);
2315 printf("Unknown option(s) near %s\n", arg+pos);
2319 yaz_init_opt_decode(&z3950_options, show_opt, 0);
2325 static int cmd_explain(const char *arg)
2327 if (protocol != PROTO_HTTP)
2338 /* save this for later .. when fetching individual records */
2339 sr = yaz_srw_get(out, Z_SRW_explain_request);
2340 if (recordsyntax == VAL_TEXT_XML)
2341 sr->u.explain_request->recordPacking = "xml";
2349 static int cmd_init(const char *arg)
2353 strncpy (cur_host, arg, sizeof(cur_host)-1);
2354 cur_host[sizeof(cur_host)-1] = 0;
2356 if (!conn || protocol != PROTO_Z3950)
2358 send_initRequest(cur_host);
2362 static int cmd_find(const char *arg)
2366 printf("Find what?\n");
2369 if (protocol == PROTO_HTTP)
2376 if (!send_SRW_searchRequest(arg))
2389 printf("Not connected yet\n");
2393 if (!send_searchRequest(arg))
2399 static int cmd_delete(const char *arg)
2403 printf("Not connected yet\n");
2408 if (!send_deleteResultSetRequest(arg))
2413 static int cmd_ssub(const char *arg)
2415 if (!(smallSetUpperBound = atoi(arg)))
2420 static int cmd_lslb(const char *arg)
2424 if (!(largeSetLowerBound = atoi(arg)))
2429 static int cmd_mspn(const char *arg)
2433 if (!(mediumSetPresentNumber = atoi(arg)))
2438 static int cmd_status(const char *arg)
2440 printf("smallSetUpperBound: %d\n", smallSetUpperBound);
2441 printf("largeSetLowerBound: %d\n", largeSetLowerBound);
2442 printf("mediumSetPresentNumber: %d\n", mediumSetPresentNumber);
2446 static int cmd_setnames(const char *arg)
2448 if (*arg == '1') /* enable ? */
2450 else if (*arg == '0') /* disable ? */
2452 else if (setnumber < 0) /* no args, toggle .. */
2458 printf("Set numbering enabled.\n");
2460 printf("Set numbering disabled.\n");
2464 /* PRESENT SERVICE ----------------------------- */
2466 static void parse_show_args(const char *arg_c, char *setstring,
2467 int *start, int *number)
2472 strncpy(arg, arg_c, sizeof(arg)-1);
2473 arg[sizeof(arg)-1] = '\0';
2475 if ((p = strchr(arg, '+')))
2477 *number = atoi(p + 1);
2482 if (p && (p=strchr(p+1, '+')))
2483 strcpy (setstring, p+1);
2484 else if (setnumber >= 0)
2485 sprintf(setstring, "%d", setnumber);
2490 static int send_presentRequest(const char *arg)
2492 Z_APDU *apdu = zget_APDU(out, Z_APDU_presentRequest);
2493 Z_PresentRequest *req = apdu->u.presentRequest;
2494 Z_RecordComposition compo;
2496 char setstring[100];
2498 req->referenceId = set_refid (out);
2500 parse_show_args(arg, setstring, &setno, &nos);
2502 req->resultSetId = setstring;
2504 req->resultSetStartPoint = &setno;
2505 req->numberOfRecordsRequested = &nos;
2507 req->preferredRecordSyntax =
2508 yaz_oidval_to_z3950oid(out, CLASS_RECSYN, recordsyntax);
2512 req->recordComposition = &compo;
2513 compo.which = Z_RecordComp_complex;
2514 compo.u.complex = (Z_CompSpec *)
2515 odr_malloc(out, sizeof(*compo.u.complex));
2516 compo.u.complex->selectAlternativeSyntax = (bool_t *)
2517 odr_malloc(out, sizeof(bool_t));
2518 *compo.u.complex->selectAlternativeSyntax = 0;
2520 compo.u.complex->generic = (Z_Specification *)
2521 odr_malloc(out, sizeof(*compo.u.complex->generic));
2522 compo.u.complex->generic->which = Z_Schema_oid;
2524 compo.u.complex->generic->schema.oid =
2525 yaz_str_to_z3950oid(out, CLASS_SCHEMA, record_schema);
2527 if (!compo.u.complex->generic->schema.oid)
2529 /* OID wasn't a schema! Try record syntax instead. */
2530 compo.u.complex->generic->schema.oid = (Odr_oid *)
2531 yaz_str_to_z3950oid(out, CLASS_RECSYN, record_schema);
2533 if (!elementSetNames)
2534 compo.u.complex->generic->elementSpec = 0;
2537 compo.u.complex->generic->elementSpec = (Z_ElementSpec *)
2538 odr_malloc(out, sizeof(Z_ElementSpec));
2539 compo.u.complex->generic->elementSpec->which =
2540 Z_ElementSpec_elementSetName;
2541 compo.u.complex->generic->elementSpec->u.elementSetName =
2542 elementSetNames->u.generic;
2544 compo.u.complex->num_dbSpecific = 0;
2545 compo.u.complex->dbSpecific = 0;
2546 compo.u.complex->num_recordSyntax = 0;
2547 compo.u.complex->recordSyntax = 0;
2549 else if (elementSetNames)
2551 req->recordComposition = &compo;
2552 compo.which = Z_RecordComp_simple;
2553 compo.u.simple = elementSetNames;
2556 printf("Sent presentRequest (%d+%d).\n", setno, nos);
2561 static int send_SRW_presentRequest(const char *arg)
2563 char setstring[100];
2565 Z_SRW_PDU *sr = srw_sr;
2569 parse_show_args(arg, setstring, &setno, &nos);
2570 sr->u.request->startRecord = odr_intdup(out, setno);
2571 sr->u.request->maximumRecords = odr_intdup(out, nos);
2573 sr->u.request->recordSchema = record_schema;
2574 if (recordsyntax == VAL_TEXT_XML)
2575 sr->u.request->recordPacking = "xml";
2576 return send_srw(sr);
2580 static void close_session (void)
2587 nmem_destroy (session_mem);
2596 void process_close(Z_Close *req)
2598 Z_APDU *apdu = zget_APDU(out, Z_APDU_close);
2599 Z_Close *res = apdu->u.close;
2601 static char *reasons[] =
2606 "cost limit reached",
2608 "security violation",
2615 printf("Reason: %s, message: %s\n", reasons[*req->closeReason],
2616 req->diagnosticInformation ? req->diagnosticInformation : "NULL");
2621 *res->closeReason = Z_Close_finished;
2623 printf("Sent response.\n");
2628 static int cmd_show(const char *arg)
2630 if (protocol == PROTO_HTTP)
2637 if (!send_SRW_presentRequest(arg))
2647 printf("Not connected yet\n");
2650 if (!send_presentRequest(arg))
2656 int cmd_quit(const char *arg)
2658 printf("See you later, alligator.\n");
2664 int cmd_cancel(const char *arg)
2666 Z_APDU *apdu = zget_APDU(out, Z_APDU_triggerResourceControlRequest);
2667 Z_TriggerResourceControlRequest *req =
2668 apdu->u.triggerResourceControlRequest;
2673 printf("Session not initialized yet\n");
2678 if (!ODR_MASK_GET(session->options, Z_Options_triggerResourceCtrl))
2680 printf("Target doesn't support cancel (trigger resource ctrl)\n");
2683 *req->requestedAction = Z_TriggerResourceControlRequest_cancel;
2684 req->resultSetWanted = &rfalse;
2685 req->referenceId = set_refid (out);
2688 printf("Sent cancel request\n");
2693 int cmd_cancel_find(const char *arg) {
2697 return cmd_cancel("");
2702 int send_scanrequest(const char *query, int pp, int num, const char *term)
2704 Z_APDU *apdu = zget_APDU(out, Z_APDU_scanRequest);
2705 Z_ScanRequest *req = apdu->u.scanRequest;
2709 if (queryType == QueryType_CCL2RPN)
2712 struct ccl_rpn_node *rpn;
2714 rpn = ccl_find_str (bibset, query, &error, &pos);
2717 printf("CCL ERROR: %s\n", ccl_err_msg(error));
2721 yaz_oidval_to_z3950oid(out, CLASS_ATTSET, VAL_BIB1);
2722 if (!(req->termListAndStartPoint = ccl_scan_query (out, rpn)))
2724 printf("Couldn't convert CCL to Scan term\n");
2727 ccl_rpn_delete (rpn);
2731 YAZ_PQF_Parser pqf_parser = yaz_pqf_create ();
2733 if (!(req->termListAndStartPoint =
2734 yaz_pqf_scan(pqf_parser, out, &req->attributeSet, query)))
2736 const char *pqf_msg;
2738 int code = yaz_pqf_error (pqf_parser, &pqf_msg, &off);
2739 printf("%*s^\n", off+7, "");
2740 printf("Prefix query error: %s (code %d)\n", pqf_msg, code);
2741 yaz_pqf_destroy (pqf_parser);
2744 yaz_pqf_destroy (pqf_parser);
2748 if (req->termListAndStartPoint->term &&
2749 req->termListAndStartPoint->term->which == Z_Term_general &&
2750 req->termListAndStartPoint->term->u.general)
2752 req->termListAndStartPoint->term->u.general->buf =
2753 (unsigned char *) odr_strdup(out, term);
2754 req->termListAndStartPoint->term->u.general->len =
2755 req->termListAndStartPoint->term->u.general->size =
2759 req->referenceId = set_refid (out);
2760 req->num_databaseNames = num_databaseNames;
2761 req->databaseNames = databaseNames;
2762 req->numberOfTermsRequested = #
2763 req->preferredPositionInResponse = &pp;
2764 req->stepSize = odr_intdup(out, scan_stepSize);
2769 int send_sortrequest(const char *arg, int newset)
2771 Z_APDU *apdu = zget_APDU(out, Z_APDU_sortRequest);
2772 Z_SortRequest *req = apdu->u.sortRequest;
2773 Z_SortKeySpecList *sksl = (Z_SortKeySpecList *)
2774 odr_malloc (out, sizeof(*sksl));
2780 sprintf (setstring, "%d", setnumber);
2782 sprintf (setstring, "default");
2784 req->referenceId = set_refid (out);
2786 req->num_inputResultSetNames = 1;
2787 req->inputResultSetNames = (Z_InternationalString **)
2788 odr_malloc (out, sizeof(*req->inputResultSetNames));
2789 req->inputResultSetNames[0] = odr_strdup (out, setstring);
2791 if (newset && setnumber >= 0)
2792 sprintf (setstring, "%d", ++setnumber);
2794 req->sortedResultSetName = odr_strdup (out, setstring);
2796 req->sortSequence = yaz_sort_spec (out, arg);
2797 if (!req->sortSequence)
2799 printf ("Missing sort specifications\n");
2806 void display_term(Z_TermInfo *t)
2809 printf("%s", t->displayTerm);
2810 else if (t->term->which == Z_Term_general)
2812 printf("%.*s", t->term->u.general->len, t->term->u.general->buf);
2813 sprintf(last_scan_line, "%.*s", t->term->u.general->len,
2814 t->term->u.general->buf);
2817 printf("Term (not general)");
2818 if (t->globalOccurrences)
2819 printf (" (%d)\n", *t->globalOccurrences);
2824 void process_scanResponse(Z_ScanResponse *res)
2827 Z_Entry **entries = NULL;
2828 int num_entries = 0;
2830 printf("Received ScanResponse\n");
2831 print_refid (res->referenceId);
2832 printf("%d entries", *res->numberOfEntriesReturned);
2833 if (res->positionOfTerm)
2834 printf (", position=%d", *res->positionOfTerm);
2836 if (*res->scanStatus != Z_Scan_success)
2837 printf("Scan returned code %d\n", *res->scanStatus);
2840 if ((entries = res->entries->entries))
2841 num_entries = res->entries->num_entries;
2842 for (i = 0; i < num_entries; i++)
2844 int pos_term = res->positionOfTerm ? *res->positionOfTerm : -1;
2845 if (entries[i]->which == Z_Entry_termInfo)
2847 printf("%c ", i + 1 == pos_term ? '*' : ' ');
2848 display_term(entries[i]->u.termInfo);
2851 display_diagrecs(&entries[i]->u.surrogateDiagnostic, 1);
2853 if (res->entries->nonsurrogateDiagnostics)
2854 display_diagrecs (res->entries->nonsurrogateDiagnostics,
2855 res->entries->num_nonsurrogateDiagnostics);
2858 void process_sortResponse(Z_SortResponse *res)
2860 printf("Received SortResponse: status=");
2861 switch (*res->sortStatus)
2863 case Z_SortResponse_success:
2864 printf ("success"); break;
2865 case Z_SortResponse_partial_1:
2866 printf ("partial"); break;
2867 case Z_SortResponse_failure:
2868 printf ("failure"); break;
2870 printf ("unknown (%d)", *res->sortStatus);
2873 print_refid (res->referenceId);
2874 if (res->diagnostics)
2875 display_diagrecs(res->diagnostics,
2876 res->num_diagnostics);
2879 void process_deleteResultSetResponse (Z_DeleteResultSetResponse *res)
2881 printf("Got deleteResultSetResponse status=%d\n",
2882 *res->deleteOperationStatus);
2883 if (res->deleteListStatuses)
2886 for (i = 0; i < res->deleteListStatuses->num; i++)
2888 printf ("%s status=%d\n", res->deleteListStatuses->elements[i]->id,
2889 *res->deleteListStatuses->elements[i]->status);
2894 int cmd_sort_generic(const char *arg, int newset)
2898 printf("Session not initialized yet\n");
2903 if (!ODR_MASK_GET(session->options, Z_Options_sort))
2905 printf("Target doesn't support sort\n");
2910 if (send_sortrequest(arg, newset) < 0)
2917 int cmd_sort(const char *arg)
2919 return cmd_sort_generic (arg, 0);
2922 int cmd_sort_newset (const char *arg)
2924 return cmd_sort_generic (arg, 1);
2927 int cmd_scanstep(const char *arg)
2929 scan_stepSize = atoi(arg);
2933 int cmd_scanpos(const char *arg)
2935 int r = sscanf(arg, "%d", &scan_position);
2941 int cmd_scansize(const char *arg)
2943 int r = sscanf(arg, "%d", &scan_size);
2949 int cmd_scan(const char *arg)
2951 if (protocol == PROTO_HTTP)
2960 if (send_SRW_scanRequest(arg, scan_position, scan_size) < 0)
2965 if (send_SRW_scanRequest(last_scan_line, 1, scan_size) < 0)
2980 printf("Session not initialized yet\n");
2984 if (!ODR_MASK_GET(session->options, Z_Options_scan))
2986 printf("Target doesn't support scan\n");
2991 strcpy (last_scan_query, arg);
2992 if (send_scanrequest(arg, scan_position, scan_size, 0) < 0)
2997 if (send_scanrequest(last_scan_query, 1, scan_size, last_scan_line) < 0)
3004 int cmd_schema(const char *arg)
3006 xfree(record_schema);
3009 record_schema = xstrdup(arg);
3013 int cmd_format(const char *arg)
3018 printf("Usage: format <recordsyntax>\n");
3021 nsyntax = oid_getvalbyname (arg);
3022 if (strcmp(arg, "none") && nsyntax == VAL_NONE)
3024 printf ("unknown record syntax\n");
3027 recordsyntax = nsyntax;
3031 int cmd_elements(const char *arg)
3033 static Z_ElementSetNames esn;
3034 static char what[100];
3038 elementSetNames = 0;
3042 esn.which = Z_ElementSetNames_generic;
3043 esn.u.generic = what;
3044 elementSetNames = &esn;
3048 int cmd_attributeset(const char *arg)
3054 printf("Usage: attributeset <setname>\n");
3057 sscanf(arg, "%s", what);
3058 if (p_query_attset (what))
3060 printf("Unknown attribute set name\n");
3066 int cmd_querytype (const char *arg)
3068 if (!strcmp (arg, "ccl"))
3069 queryType = QueryType_CCL;
3070 else if (!strcmp (arg, "prefix") || !strcmp(arg, "rpn"))
3071 queryType = QueryType_Prefix;
3072 else if (!strcmp (arg, "ccl2rpn") || !strcmp (arg, "cclrpn"))
3073 queryType = QueryType_CCL2RPN;
3074 else if (!strcmp(arg, "cql"))
3075 queryType = QueryType_CQL;
3076 else if (!strcmp (arg, "cql2rpn") || !strcmp (arg, "cqlrpn"))
3077 queryType = QueryType_CQL2RPN;
3080 printf ("Querytype must be one of:\n");
3081 printf (" prefix - Prefix query\n");
3082 printf (" ccl - CCL query\n");
3083 printf (" ccl2rpn - CCL query converted to RPN\n");
3084 printf (" cql - CQL\n");
3085 printf (" cql2rpn - CQL query converted to RPN\n");
3091 int cmd_refid (const char *arg)
3096 refid = xstrdup (arg);
3100 int cmd_close(const char *arg)
3109 apdu = zget_APDU(out, Z_APDU_close);
3110 req = apdu->u.close;
3111 *req->closeReason = Z_Close_finished;
3113 printf("Sent close request.\n");
3118 int cmd_packagename(const char* arg)
3120 xfree (esPackageName);
3121 esPackageName = NULL;
3123 esPackageName = xstrdup(arg);
3127 int cmd_proxy(const char* arg)
3132 yazProxy = xstrdup (arg);
3136 int cmd_marccharset(const char *arg)
3141 if (sscanf(arg, "%29s", l1) < 1)
3143 printf("MARC character set is `%s'\n",
3144 marcCharset ? marcCharset: "none");
3147 xfree (marcCharset);
3149 if (strcmp(l1, "-"))
3150 marcCharset = xstrdup(l1);
3154 int cmd_displaycharset(const char *arg)
3159 if (sscanf(arg, "%29s", l1) < 1)
3161 printf("Display character set is `%s'\n",
3162 outputCharset ? outputCharset: "none");
3166 xfree (outputCharset);
3168 if (!strcmp(l1, "auto") && codeset)
3172 printf ("Display character set: %s\n", codeset);
3173 outputCharset = xstrdup(codeset);
3176 printf ("No codeset found on this system\n");
3178 else if (strcmp(l1, "-") && strcmp(l1, "none"))
3179 outputCharset = xstrdup(l1);
3184 int cmd_negcharset(const char *arg)
3189 if (sscanf(arg, "%29s %d %d", l1, &negotiationCharsetRecords,
3190 &negotiationCharsetVersion) < 1)
3192 printf("Current negotiation character set is `%s'\n",
3193 negotiationCharset ? negotiationCharset: "none");
3194 printf("Records in charset %s\n", negotiationCharsetRecords ?
3196 printf("Charneg version %d\n", negotiationCharsetVersion);
3200 xfree (negotiationCharset);
3201 negotiationCharset = NULL;
3202 if (*l1 && strcmp(l1, "-") && strcmp(l1, "none"))
3204 negotiationCharset = xstrdup(l1);
3205 printf ("Character set negotiation : %s\n", negotiationCharset);
3211 int cmd_charset(const char* arg)
3213 char l1[30], l2[30], l3[30];
3215 *l1 = *l2 = *l3 = 0;
3216 if (sscanf(arg, "%29s %29s %29s", l1, l2, l3) < 1)
3219 cmd_displaycharset("");
3220 cmd_marccharset("");
3226 cmd_displaycharset(l2);
3228 cmd_marccharset(l3);
3233 int cmd_lang(const char* arg)
3236 printf("Current language is `%s'\n", (yazLang)?yazLang:NULL);
3242 yazLang = xstrdup(arg);
3246 int cmd_source(const char* arg, int echo )
3248 /* first should open the file and read one line at a time.. */
3250 char line[102400], *cp;
3253 fprintf(stderr,"Error in source command use a filename\n");
3257 includeFile = fopen (arg, "r");
3260 fprintf(stderr,"Unable to open file %s for reading\n",arg);
3264 while(!feof(includeFile)) {
3265 memset(line,0,sizeof(line));
3266 fgets(line,sizeof(line),includeFile);
3268 if(strlen(line) < 2) continue;
3269 if(line[0] == '#') continue;
3271 if ((cp = strrchr (line, '\n')))
3275 printf( "processing line: %s\n",line );
3277 process_cmd_line(line);
3280 if(fclose(includeFile)<0) {
3281 perror("unable to close include file");
3287 int cmd_source_echo(const char* arg)
3293 int cmd_source_noecho(const char* arg)
3300 int cmd_subshell(const char* args)
3305 system(getenv("SHELL"));
3311 int cmd_set_berfile(const char *arg)
3313 if (ber_file && ber_file != stdout && ber_file != stderr)
3315 if (!strcmp(arg, ""))
3317 else if (!strcmp(arg, "-"))
3320 ber_file = fopen(arg, "a");
3324 int cmd_set_apdufile(const char *arg)
3326 if(apdu_file && apdu_file != stderr && apdu_file != stderr)
3328 if (!strcmp(arg, ""))
3330 else if (!strcmp(arg, "-"))
3334 apdu_file = fopen(arg, "a");
3336 perror("unable to open apdu log file");
3339 odr_setprint(print, apdu_file);
3343 int cmd_set_cclfile(const char* arg)
3347 bibset = ccl_qual_mk ();
3348 inf = fopen (arg, "r");
3350 perror("unable to open CCL file");
3353 ccl_qual_file (bibset, inf);
3356 strcpy(ccl_fields,arg);
3360 int cmd_set_cqlfile(const char* arg)
3362 cql_transform_t newcqltrans;
3364 if ((newcqltrans = cql_transform_open_fname(arg)) == 0) {
3365 perror("unable to open CQL file");
3369 cql_transform_close(cqltrans);
3371 cqltrans = newcqltrans;
3372 strcpy(cql_fields, arg);
3376 int cmd_set_auto_reconnect(const char* arg)
3378 if(strlen(arg)==0) {
3379 auto_reconnect = ! auto_reconnect;
3380 } else if(strcmp(arg,"on")==0) {
3382 } else if(strcmp(arg,"off")==0) {
3385 printf("Error use on or off\n");
3390 printf("Set auto reconnect enabled.\n");
3392 printf("Set auto reconnect disabled.\n");
3398 int cmd_set_auto_wait(const char* arg)
3400 if(strlen(arg)==0) {
3401 auto_wait = ! auto_wait;
3402 } else if(strcmp(arg,"on")==0) {
3404 } else if(strcmp(arg,"off")==0) {
3407 printf("Error use on or off\n");
3412 printf("Set auto wait enabled.\n");
3414 printf("Set auto wait disabled.\n");
3419 int cmd_set_marcdump(const char* arg)
3421 if(marc_file && marc_file != stderr) { /* don't close stdout*/
3425 if (!strcmp(arg, ""))
3427 else if (!strcmp(arg, "-"))
3431 marc_file = fopen(arg, "a");
3433 perror("unable to open marc log file");
3439 this command takes 3 arge {name class oid}
3441 int cmd_register_oid(const char* args) {
3446 {"appctx",CLASS_APPCTX},
3447 {"absyn",CLASS_ABSYN},
3448 {"attset",CLASS_ATTSET},
3449 {"transyn",CLASS_TRANSYN},
3450 {"diagset",CLASS_DIAGSET},
3451 {"recsyn",CLASS_RECSYN},
3452 {"resform",CLASS_RESFORM},
3453 {"accform",CLASS_ACCFORM},
3454 {"extserv",CLASS_EXTSERV},
3455 {"userinfo",CLASS_USERINFO},
3456 {"elemspec",CLASS_ELEMSPEC},
3457 {"varset",CLASS_VARSET},
3458 {"schema",CLASS_SCHEMA},
3459 {"tagset",CLASS_TAGSET},
3460 {"general",CLASS_GENERAL},
3461 {0,(enum oid_class) 0}
3463 char oname_str[101], oclass_str[101], oid_str[101];
3466 oid_class oidclass = CLASS_GENERAL;
3467 int val = 0, oid[OID_SIZE];
3468 struct oident * new_oident=NULL;
3470 if (sscanf (args, "%100[^ ] %100[^ ] %100s",
3471 oname_str,oclass_str, oid_str) < 1) {
3472 printf("Error in register command \n");
3476 for (i = 0; oid_classes[i].className; i++) {
3477 if (!strcmp(oid_classes[i].className, oclass_str))
3479 oidclass=oid_classes[i].oclass;
3484 if(!(oid_classes[i].className)) {
3485 printf("Unknown oid class %s\n",oclass_str);
3493 while (isdigit (*(unsigned char *) name))
3495 val = val*10 + (*name - '0');
3508 new_oident = oid_addent (oid, PROTO_GENERAL, oidclass, oname_str,
3510 if(strcmp(new_oident->desc,oname_str))
3512 fprintf(stderr,"oid is already named as %s, registration failed\n",
3518 int cmd_push_command(const char* arg)
3520 #if HAVE_READLINE_HISTORY_H
3524 fprintf(stderr,"Not compiled with the readline/history module\n");
3529 void source_rcfile()
3531 /* Look for a $HOME/.yazclientrc and source it if it exists */
3532 struct stat statbuf;
3534 char* homedir=getenv("HOME");
3538 sprintf(buffer,"%s/.yazclientrc",homedir);
3540 if(stat(buffer,&statbuf)==0) {
3541 cmd_source(buffer, 0 );
3546 if(stat(".yazclientrc",&statbuf)==0) {
3547 cmd_source(".yazclientrc", 0 );
3552 static void initialize(void)
3557 if (!(out = odr_createmem(ODR_ENCODE)) ||
3558 !(in = odr_createmem(ODR_DECODE)) ||
3559 !(print = odr_createmem(ODR_PRINT)))
3561 fprintf(stderr, "failed to allocate ODR streams\n");
3566 setvbuf(stdout, 0, _IONBF, 0);
3568 odr_setprint(print, apdu_file);
3570 bibset = ccl_qual_mk ();
3571 inf = fopen (ccl_fields, "r");
3574 ccl_qual_file (bibset, inf);
3578 cqltrans = cql_transform_open_fname(cql_fields);
3579 /* If this fails, no problem: we detect cqltrans == 0 later */
3581 #if HAVE_READLINE_READLINE_H
3582 rl_attempted_completion_function = (CPPFunction*)readline_completer;
3586 for(i=0; i<maxOtherInfosSupported; ++i) {
3587 extraOtherInfos[i].oidval = -1;
3594 #if HAVE_GETTIMEOFDAY
3595 struct timeval tv_start;
3599 static void handle_srw_record(Z_SRW_record *rec)
3601 if (rec->recordPosition)
3603 printf ("pos=%d", *rec->recordPosition);
3604 setno = *rec->recordPosition + 1;
3606 if (rec->recordSchema)
3607 printf (" schema=%s", rec->recordSchema);
3609 if (rec->recordData_buf && rec->recordData_len)
3611 fwrite(rec->recordData_buf, 1, rec->recordData_len, stdout);
3613 fwrite (rec->recordData_buf, 1, rec->recordData_len, marc_file);
3616 printf ("No data!");
3620 static void handle_srw_explain_response(Z_SRW_explainResponse *res)
3622 handle_srw_record(&res->record);
3625 static void handle_srw_response(Z_SRW_searchRetrieveResponse *res)
3629 printf ("Received SRW SearchRetrieve Response\n");
3631 for (i = 0; i<res->num_diagnostics; i++)
3633 if (res->diagnostics[i].uri)
3634 printf ("SRW diagnostic %s\n",
3635 res->diagnostics[i].uri);
3637 printf ("SRW diagnostic missing or could not be decoded\n");
3638 if (res->diagnostics[i].message)
3639 printf ("Message: %s\n", res->diagnostics[i].message);
3640 if (res->diagnostics[i].details)
3641 printf ("Details: %s\n", res->diagnostics[i].details);
3643 if (res->numberOfRecords)
3644 printf ("Number of hits: %d\n", *res->numberOfRecords);
3645 for (i = 0; i<res->num_records; i++)
3646 handle_srw_record(res->records + i);
3649 static void handle_srw_scan_term(Z_SRW_scanTerm *term)
3651 if (term->displayTerm)
3652 printf("%s:", term->displayTerm);
3653 else if (term->value)
3654 printf("%s:", term->value);
3656 printf("No value:");
3657 if (term->numberOfRecords)
3658 printf(" %d", *term->numberOfRecords);
3659 if (term->whereInList)
3660 printf(" %s", term->whereInList);
3661 if (term->value && term->displayTerm)
3662 printf(" %s", term->value);
3664 strcpy(last_scan_line, term->value);
3668 static void handle_srw_scan_response(Z_SRW_scanResponse *res)
3672 printf ("Received SRW Scan Response\n");
3674 for (i = 0; i<res->num_diagnostics; i++)
3676 if (res->diagnostics[i].uri)
3677 printf ("SRW diagnostic %s\n",
3678 res->diagnostics[i].uri);
3680 printf ("SRW diagnostic missing or could not be decoded\n");
3681 if (res->diagnostics[i].message)
3682 printf ("Message: %s\n", res->diagnostics[i].message);
3683 if (res->diagnostics[i].details)
3684 printf ("Details: %s\n", res->diagnostics[i].details);
3687 for (i = 0; i<res->num_terms; i++)
3688 handle_srw_scan_term(res->terms + i);
3691 static void http_response(Z_HTTP_Response *hres)
3694 const char *content_type = z_HTTP_header_lookup(hres->headers,
3696 const char *connection_head = z_HTTP_header_lookup(hres->headers,
3698 if (content_type && !yaz_strcmp_del("text/xml", content_type, "; "))
3700 Z_SOAP *soap_package = 0;
3701 ODR o = odr_createmem(ODR_DECODE);
3702 Z_SOAP_Handler soap_handlers[2] = {
3703 {"http://www.loc.gov/zing/srw/", 0,
3704 (Z_SOAP_fun) yaz_srw_codec},
3707 ret = z_soap_codec(o, &soap_package,
3708 &hres->content_buf, &hres->content_len,
3710 if (!ret && soap_package->which == Z_SOAP_generic &&
3711 soap_package->u.generic->no == 0)
3713 Z_SRW_PDU *sr = soap_package->u.generic->p;
3714 if (sr->which == Z_SRW_searchRetrieve_response)
3715 handle_srw_response(sr->u.response);
3716 else if (sr->which == Z_SRW_explain_response)
3717 handle_srw_explain_response(sr->u.explain_response);
3718 else if (sr->which == Z_SRW_scan_response)
3719 handle_srw_scan_response(sr->u.scan_response);
3723 else if (soap_package && (soap_package->which == Z_SOAP_fault
3724 || soap_package->which == Z_SOAP_error))
3726 printf ("HTTP Error Status=%d\n", hres->code);
3727 printf ("SOAP Fault code %s\n",
3728 soap_package->u.fault->fault_code);
3729 printf ("SOAP Fault string %s\n",
3730 soap_package->u.fault->fault_string);
3731 if (soap_package->u.fault->details)
3732 printf ("SOAP Details %s\n",
3733 soap_package->u.fault->details);
3741 if (hres->code != 200)
3743 printf ("HTTP Error Status=%d\n", hres->code);
3747 printf ("Decoding of SRW package failed\n");
3753 if (!strcmp(hres->version, "1.0"))
3755 /* HTTP 1.0: only if Keep-Alive we stay alive.. */
3756 if (!connection_head || strcmp(connection_head, "Keep-Alive"))
3761 /* HTTP 1.1: only if no close we stay alive .. */
3762 if (connection_head && !strcmp(connection_head, "close"))
3769 void wait_and_handle_response()
3771 int reconnect_ok = 1;
3774 int netbufferlen = 0;
3775 #if HAVE_GETTIMEOFDAY
3777 struct timeval tv_end;
3783 res = cs_get(conn, &netbuffer, &netbufferlen);
3784 if (reconnect_ok && res <= 0 && protocol == PROTO_HTTP)
3795 buf_out = odr_getbuf(out, &len_out, 0);
3797 do_hex_dump(buf_out, len_out);
3799 cs_put(conn, buf_out, len_out);
3807 printf("Target closed connection\n");
3811 #if HAVE_GETTIMEOFDAY
3812 if (got_tv_end == 0)
3813 gettimeofday (&tv_end, 0); /* count first one only */
3817 odr_reset(in); /* release APDU from last round */
3819 do_hex_dump(netbuffer, res);
3820 odr_setbuf(in, netbuffer, res, 0);
3822 if (!z_GDU(in, &gdu, 0, 0))
3824 FILE *f = ber_file ? ber_file : stdout;
3825 odr_perror(in, "Decoding incoming APDU");
3826 fprintf(f, "[Near %d]\n", odr_offset(in));
3827 fprintf(f, "Packet dump:\n---------\n");
3828 odr_dumpBER(f, netbuffer, res);
3829 fprintf(f, "---------\n");
3832 z_GDU(print, &gdu, 0, 0);
3835 if (conn && cs_more(conn))
3840 odr_dumpBER(ber_file, netbuffer, res);
3841 if (apdu_file && !z_GDU(print, &gdu, 0, 0))
3843 odr_perror(print, "Failed to print incoming APDU");
3847 if (gdu->which == Z_GDU_Z3950)
3849 Z_APDU *apdu = gdu->u.z3950;
3852 case Z_APDU_initResponse:
3853 process_initResponse(apdu->u.initResponse);
3855 case Z_APDU_searchResponse:
3856 process_searchResponse(apdu->u.searchResponse);
3858 case Z_APDU_scanResponse:
3859 process_scanResponse(apdu->u.scanResponse);
3861 case Z_APDU_presentResponse:
3862 print_refid (apdu->u.presentResponse->referenceId);
3864 *apdu->u.presentResponse->numberOfRecordsReturned;
3865 if (apdu->u.presentResponse->records)
3866 display_records(apdu->u.presentResponse->records);
3868 printf("No records.\n");
3869 printf ("nextResultSetPosition = %d\n",
3870 *apdu->u.presentResponse->nextResultSetPosition);
3872 case Z_APDU_sortResponse:
3873 process_sortResponse(apdu->u.sortResponse);
3875 case Z_APDU_extendedServicesResponse:
3876 printf("Got extended services response\n");
3877 process_ESResponse(apdu->u.extendedServicesResponse);
3880 printf("Target has closed the association.\n");
3881 process_close(apdu->u.close);
3883 case Z_APDU_resourceControlRequest:
3884 process_resourceControlRequest
3885 (apdu->u.resourceControlRequest);
3887 case Z_APDU_deleteResultSetResponse:
3888 process_deleteResultSetResponse(apdu->u.
3889 deleteResultSetResponse);
3892 printf("Received unknown APDU type (%d).\n",
3898 else if (gdu->which == Z_GDU_HTTP_Response)
3900 http_response(gdu->u.HTTP_Response);
3903 if (conn && !cs_more(conn))
3906 #if HAVE_GETTIMEOFDAY
3910 printf ("S/U S/U=%ld/%ld %ld/%ld",
3911 (long) tv_start.tv_sec,
3912 (long) tv_start.tv_usec,
3913 (long) tv_end.tv_sec,
3914 (long) tv_end.tv_usec);
3916 printf ("Elapsed: %.6f\n",
3917 (double) tv_end.tv_usec / 1e6 + tv_end.tv_sec -
3918 ((double) tv_start.tv_usec / 1e6 + tv_start.tv_sec));
3925 int cmd_cclparse(const char* arg)
3928 struct ccl_rpn_node *rpn=NULL;
3931 rpn = ccl_find_str (bibset, arg, &error, &pos);
3934 printf ("%*s^ - ", 3+strlen(last_cmd)+1+pos, " ");
3935 printf ("%s\n", ccl_err_msg (error));
3941 ccl_pr_tree(rpn, stdout);
3945 ccl_rpn_delete(rpn);
3953 int cmd_set_otherinfo(const char* args)
3955 char oidstr[101], otherinfoString[101];
3960 sscan_res = sscanf (args, "%d %100[^ ] %100s", &otherinfoNo, oidstr, otherinfoString);
3962 /* reset this otherinfo */
3963 if(otherinfoNo>=maxOtherInfosSupported) {
3964 printf("Error otherinfo index to large (%d>%d)\n",
3965 otherinfoNo,maxOtherInfosSupported);
3967 extraOtherInfos[otherinfoNo].oidval = -1;
3968 if (extraOtherInfos[otherinfoNo].value)
3969 free(extraOtherInfos[otherinfoNo].value);
3973 printf("Error in set_otherinfo command \n");
3977 if (otherinfoNo>=maxOtherInfosSupported) {
3978 printf("Error otherinfo index to large (%d>%d)\n",
3979 otherinfoNo,maxOtherInfosSupported);
3982 oidval = oid_getvalbyname (oidstr);
3983 if (oidval == -1 ) {
3984 printf("Error in set_otherinfo command unknown oid %s \n",oidstr);
3987 extraOtherInfos[otherinfoNo].oidval = oidval;
3988 if(extraOtherInfos[otherinfoNo].value) free(extraOtherInfos[otherinfoNo].value);
3989 extraOtherInfos[otherinfoNo].value = strdup(otherinfoString);
3994 int cmd_sleep(const char* args )
4003 printf("Done sleeping %d seconds\n", sec);
4008 int cmd_list_otherinfo(const char* args)
4012 if(strlen(args)>0) {
4014 if( i >= maxOtherInfosSupported ) {
4015 printf("Error otherinfo index to large (%d>%d)\n",i,maxOtherInfosSupported);
4019 if(extraOtherInfos[i].oidval != -1)
4020 printf(" otherinfo %d %s %s\n",
4022 yaz_z3950_oid_value_to_str(
4023 (enum oid_value) extraOtherInfos[i].oidval,
4025 extraOtherInfos[i].value);
4028 for(i=0; i<maxOtherInfosSupported; ++i) {
4029 if(extraOtherInfos[i].oidval != -1)
4030 printf(" otherinfo %d %s %s\n",
4032 yaz_z3950_oid_value_to_str(
4033 (enum oid_value) extraOtherInfos[i].oidval,
4035 extraOtherInfos[i].value);
4043 int cmd_list_all(const char* args) {
4046 /* connection options */
4048 printf("Connected to : %s\n",last_open_command);
4050 if(last_open_command)
4051 printf("Not connected to : %s\n",last_open_command);
4053 printf("Not connected : \n");
4056 if(yazProxy) printf("using proxy : %s\n",yazProxy);
4058 printf("auto_reconnect : %s\n",auto_reconnect?"on":"off");
4059 printf("auto_wait : %s\n",auto_wait?"on":"off");
4062 printf("Authentication : none\n");
4064 switch(auth->which) {
4065 case Z_IdAuthentication_idPass:
4066 printf("Authentication : IdPass\n");
4067 printf(" Login User : %s\n",auth->u.idPass->userId?auth->u.idPass->userId:"");
4068 printf(" Login Group : %s\n",auth->u.idPass->groupId?auth->u.idPass->groupId:"");
4069 printf(" Password : %s\n",auth->u.idPass->password?auth->u.idPass->password:"");
4071 case Z_IdAuthentication_open:
4072 printf("Authentication : psOpen\n");
4073 printf(" Open string : %s\n",auth->u.open);
4076 printf("Authentication : Unknown\n");
4079 if (negotiationCharset)
4080 printf("Neg. Character set : `%s'\n", negotiationCharset);
4084 for (i = 0; i<num_databaseNames; i++) printf("%s ",databaseNames[i]);
4088 printf("CCL file : %s\n",ccl_fields);
4089 printf("CQL file : %s\n",cql_fields);
4090 printf("Query type : %s\n",query_type_as_string(queryType));
4092 printf("Named Result Sets : %s\n",setnumber==-1?"off":"on");
4094 /* piggy back options */
4095 printf("ssub/lslb/mspn : %d/%d/%d\n",smallSetUpperBound,largeSetLowerBound,mediumSetPresentNumber);
4097 /* print present related options */
4098 printf("Format : %s\n",yaz_z3950_oid_value_to_str(recordsyntax,CLASS_RECSYN));
4099 printf("Schema : %s\n",record_schema ? record_schema : "not set");
4100 printf("Elements : %s\n",elementSetNames?elementSetNames->u.generic:"");
4102 /* loging options */
4103 printf("APDU log : %s\n",apdu_file?"on":"off");
4104 printf("Record log : %s\n",marc_file?"on":"off");
4107 printf("Other Info: \n");
4108 cmd_list_otherinfo("");
4113 int cmd_clear_otherinfo(const char* args)
4115 if(strlen(args)>0) {
4117 otherinfoNo = atoi(args);
4118 if( otherinfoNo >= maxOtherInfosSupported ) {
4119 printf("Error otherinfo index to large (%d>%d)\n",otherinfoNo,maxOtherInfosSupported);
4123 if(extraOtherInfos[otherinfoNo].oidval != -1) {
4124 /* only clear if set. */
4125 extraOtherInfos[otherinfoNo].oidval=-1;
4126 free(extraOtherInfos[otherinfoNo].value);
4131 for(i=0; i<maxOtherInfosSupported; ++i) {
4132 if (extraOtherInfos[i].oidval!=-1 ) {
4133 extraOtherInfos[i].oidval=-1;
4134 free(extraOtherInfos[i].value);
4141 int cmd_wait_response(const char *arg)
4143 int wait_for = atoi(arg);
4145 if( wait_for < 1 ) {
4149 for( i=0 ; i < wait_for ; ++i ) {
4150 wait_and_handle_response( );
4155 static int cmd_help (const char *line);
4157 typedef char *(*completerFunctionType)(const char *text, int state);
4161 int (*fun)(const char *arg);
4163 completerFunctionType rl_completerfunction;
4164 int complete_filenames;
4165 char **local_tabcompletes;
4167 {"open", cmd_open, "('tcp'|'ssl')':<host>[':'<port>][/<db>]",NULL,0,NULL},
4168 {"quit", cmd_quit, "",NULL,0,NULL},
4169 {"find", cmd_find, "<query>",NULL,0,NULL},
4170 {"delete", cmd_delete, "<setname>",NULL,0,NULL},
4171 {"base", cmd_base, "<base-name>",NULL,0,NULL},
4172 {"show", cmd_show, "<rec#>['+'<#recs>['+'<setname>]]",NULL,0,NULL},
4173 {"scan", cmd_scan, "<term>",NULL,0,NULL},
4174 {"scanstep", cmd_scanstep, "<size>",NULL,0,NULL},
4175 {"scanpos", cmd_scanpos, "<size>",NULL,0,NULL},
4176 {"scansize", cmd_scansize, "<size>",NULL,0,NULL},
4177 {"sort", cmd_sort, "<sortkey> <flag> <sortkey> <flag> ...",NULL,0,NULL},
4178 {"sort+", cmd_sort_newset, "<sortkey> <flag> <sortkey> <flag> ...",NULL,0,NULL},
4179 {"authentication", cmd_authentication, "<acctstring>",NULL,0,NULL},
4180 {"lslb", cmd_lslb, "<largeSetLowerBound>",NULL,0,NULL},
4181 {"ssub", cmd_ssub, "<smallSetUpperBound>",NULL,0,NULL},
4182 {"mspn", cmd_mspn, "<mediumSetPresentNumber>",NULL,0,NULL},
4183 {"status", cmd_status, "",NULL,0,NULL},
4184 {"setnames", cmd_setnames, "",NULL,0,NULL},
4185 {"cancel", cmd_cancel, "",NULL,0,NULL},
4186 {"cancel_find", cmd_cancel_find, "<query>",NULL,0,NULL},
4187 {"format", cmd_format, "<recordsyntax>",complete_format,0,NULL},
4188 {"schema", cmd_schema, "<schema>",complete_schema,0,NULL},
4189 {"elements", cmd_elements, "<elementSetName>",NULL,0,NULL},
4190 {"close", cmd_close, "",NULL,0,NULL},
4191 {"attributeset", cmd_attributeset, "<attrset>",complete_attributeset,0,NULL},
4192 {"querytype", cmd_querytype, "<type>",complete_querytype,0,NULL},
4193 {"refid", cmd_refid, "<id>",NULL,0,NULL},
4194 {"itemorder", cmd_itemorder, "ill|item <itemno>",NULL,0,NULL},
4195 {"update", cmd_update, "<action> <recid> [<file>]",NULL,0,NULL},
4196 {"update0", cmd_update0, "<action> <recid> [<file>]",NULL,0,NULL},
4197 {"xmlupdate", cmd_xmlupdate, "<action> <doc>",NULL,0,NULL},
4198 {"packagename", cmd_packagename, "<packagename>",NULL,0,NULL},
4199 {"proxy", cmd_proxy, "[('tcp'|'ssl')]<host>[':'<port>]",NULL,0,NULL},
4200 {"charset", cmd_charset, "<nego_charset> <output_charset>",NULL,0,NULL},
4201 {"negcharset", cmd_negcharset, "<nego_charset>",NULL,0,NULL},
4202 {"displaycharset", cmd_displaycharset, "<output_charset>",NULL,0,NULL},
4203 {"marccharset", cmd_marccharset, "<charset_name>",NULL,0,NULL},
4204 {"lang", cmd_lang, "<language_code>",NULL,0,NULL},
4205 {".", cmd_source_echo, "<filename>",NULL,1,NULL},
4206 {"!", cmd_subshell, "Subshell command",NULL,1,NULL},
4207 {"set_apdufile", cmd_set_apdufile, "<filename>",NULL,1,NULL},
4208 {"set_berfile", cmd_set_berfile, "<filename>",NULL,1,NULL},
4209 {"set_marcdump", cmd_set_marcdump," <filename>",NULL,1,NULL},
4210 {"set_cclfile", cmd_set_cclfile," <filename>",NULL,1,NULL},
4211 {"set_cqlfile", cmd_set_cqlfile," <filename>",NULL,1,NULL},
4212 {"set_auto_reconnect", cmd_set_auto_reconnect," on|off",complete_auto_reconnect,1,NULL},
4213 {"set_auto_wait", cmd_set_auto_wait," on|off",complete_auto_reconnect,1,NULL},
4214 {"set_otherinfo", cmd_set_otherinfo,"<otherinfoinddex> <oid> <string>",NULL,0,NULL},
4215 {"sleep", cmd_sleep,"<seconds>",NULL,0,NULL},
4216 {"register_oid", cmd_register_oid,"<name> <class> <oid>",NULL,0,NULL},
4217 {"push_command", cmd_push_command,"<command>",command_generator,0,NULL},
4218 {"register_tab", cmd_register_tab,"<commandname> <tab>",command_generator,0,NULL},
4219 {"cclparse", cmd_cclparse,"<ccl find command>",NULL,0,NULL},
4220 {"list_otherinfo",cmd_list_otherinfo,"[otherinfoinddex]",NULL,0,NULL},
4221 {"list_all",cmd_list_all,"",NULL,0,NULL},
4222 {"clear_otherinfo",cmd_clear_otherinfo,"",NULL,0,NULL},
4223 {"wait_response",cmd_wait_response,"<number>",NULL,0,NULL},
4224 /* Server Admin Functions */
4225 {"adm-reindex", cmd_adm_reindex, "<database-name>",NULL,0,NULL},
4226 {"adm-truncate", cmd_adm_truncate, "('database'|'index')<object-name>",NULL,0,NULL},
4227 {"adm-create", cmd_adm_create, "",NULL,0,NULL},
4228 {"adm-drop", cmd_adm_drop, "('database'|'index')<object-name>",NULL,0,NULL},
4229 {"adm-import", cmd_adm_import, "<record-type> <dir> <pattern>",NULL,0,NULL},
4230 {"adm-refresh", cmd_adm_refresh, "",NULL,0,NULL},
4231 {"adm-commit", cmd_adm_commit, "",NULL,0,NULL},
4232 {"adm-shutdown", cmd_adm_shutdown, "",NULL,0,NULL},
4233 {"adm-startup", cmd_adm_startup, "",NULL,0,NULL},
4234 {"explain", cmd_explain, "", NULL, 0, NULL},
4235 {"options", cmd_options, "", NULL, 0, NULL},
4236 {"zversion", cmd_zversion, "", NULL, 0, NULL},
4237 {"help", cmd_help, "", NULL,0,NULL},
4238 {"init", cmd_init, "", NULL,0,NULL},
4242 static int cmd_help (const char *line)
4248 sscanf (line, "%20s", topic);
4251 printf("Commands:\n");
4252 for (i = 0; cmd_array[i].cmd; i++)
4253 if (*topic == 0 || strcmp (topic, cmd_array[i].cmd) == 0)
4254 printf(" %s %s\n", cmd_array[i].cmd, cmd_array[i].ad);
4255 if (!strcmp(topic, "find"))
4258 printf(" \"term\" Simple Term\n");
4259 printf(" @attr [attset] type=value op Attribute\n");
4260 printf(" @and opl opr And\n");
4261 printf(" @or opl opr Or\n");
4262 printf(" @not opl opr And-Not\n");
4263 printf(" @set set Result set\n");
4264 printf(" @prox exl dist ord rel uc ut Proximity. Use help prox\n");
4266 printf("Bib-1 attribute types\n");
4268 printf("4=Title 7=ISBN 8=ISSN 30=Date 62=Abstract 1003=Author 1016=Any\n");
4269 printf("2=Relation: ");
4270 printf("1< 2<= 3= 4>= 5> 6!= 102=Relevance\n");
4271 printf("3=Position: ");
4272 printf("1=First in Field 2=First in subfield 3=Any position\n");
4273 printf("4=Structure: ");
4274 printf("1=Phrase 2=Word 3=Key 4=Year 5=Date 6=WordList\n");
4275 printf("5=Truncation: ");
4276 printf("1=Right 2=Left 3=L&R 100=No 101=# 102=Re-1 103=Re-2\n");
4277 printf("6=Completeness:");
4278 printf("1=Incomplete subfield 2=Complete subfield 3=Complete field\n");
4280 if (!strcmp(topic, "prox"))
4282 printf("Proximity:\n");
4283 printf(" @prox exl dist ord rel uc ut\n");
4284 printf(" exl: exclude flag . 0=include, 1=exclude.\n");
4285 printf(" dist: distance integer.\n");
4286 printf(" ord: order flag. 0=unordered, 1=ordered.\n");
4287 printf(" rel: relation integer. 1< 2<= 3= 4>= 5> 6!= .\n");
4288 printf(" uc: unit class. k=known, p=private.\n");
4289 printf(" ut: unit type. 1=character, 2=word, 3=sentence,\n");
4290 printf(" 4=paragraph, 5=section, 6=chapter, 7=document,\n");
4291 printf(" 8=element, 9=subelement, 10=elementType, 11=byte.\n");
4292 printf("\nExamples:\n");
4293 printf(" Search for a and b in-order at most 3 words apart:\n");
4294 printf(" @prox 0 3 1 2 k 2 a b\n");
4295 printf(" Search for any order of a and b next to each other:\n");
4296 printf(" @prox 0 1 0 3 k 2 a b\n");
4301 int cmd_register_tab(const char* arg) {
4303 char command[101], tabargument[101];
4308 if (sscanf (arg, "%100s %100s", command, tabargument) < 1) {
4312 /* locate the amdn in the list */
4313 for (i = 0; cmd_array[i].cmd; i++) {
4314 if (!strncmp(cmd_array[i].cmd, command, strlen(command))) {
4319 if(!cmd_array[i].cmd) {
4320 fprintf(stderr,"Unknown command %s\n",command);
4325 if(!cmd_array[i].local_tabcompletes)
4326 cmd_array[i].local_tabcompletes = (char **) calloc(1,sizeof(char**));
4330 tabslist = cmd_array[i].local_tabcompletes;
4331 for(;tabslist && *tabslist;tabslist++) {
4335 cmd_array[i].local_tabcompletes = (char **)
4336 realloc(cmd_array[i].local_tabcompletes,(num_of_tabs+2)*sizeof(char**));
4337 tabslist=cmd_array[i].local_tabcompletes;
4338 tabslist[num_of_tabs]=strdup(tabargument);
4339 tabslist[num_of_tabs+1]=NULL;
4344 void process_cmd_line(char* line)
4347 char word[32], arg[10240];
4349 #if HAVE_GETTIMEOFDAY
4350 gettimeofday (&tv_start, 0);
4353 if ((res = sscanf(line, "%31s %10239[^;]", word, arg)) <= 0)
4355 strcpy(word, last_cmd);
4360 strcpy(last_cmd, word);
4362 /* removed tailing spaces from the arg command */
4365 char* lastnonspace=NULL;
4368 if(!isspace(*(unsigned char *) p)) {
4373 *(++lastnonspace) = 0;
4376 for (i = 0; cmd_array[i].cmd; i++)
4377 if (!strncmp(cmd_array[i].cmd, word, strlen(word)))
4379 res = (*cmd_array[i].fun)(arg);
4383 if (!cmd_array[i].cmd) /* dump our help-screen */
4385 printf("Unknown command: %s.\n", word);
4386 printf("Type 'help' for list of commands\n");
4390 if(apdu_file) fflush(apdu_file);
4392 if (res >= 2 && auto_wait)
4393 wait_and_handle_response();
4402 char *command_generator(const char *text, int state)
4408 for( ; cmd_array[idx].cmd; ++idx) {
4409 if (!strncmp(cmd_array[idx].cmd,text,strlen(text))) {
4410 ++idx; /* skip this entry on the next run */
4411 return strdup(cmd_array[idx-1].cmd);
4419 This function only known how to complete on the first word
4421 char ** readline_completer(char *text, int start, int end) {
4422 #if HAVE_READLINE_READLINE_H
4424 completerFunctionType completerToUse;
4427 #if HAVE_READLINE_RL_COMPLETION_MATCHES
4428 char** res=rl_completion_matches(text,
4431 char** res=completion_matches(text,
4432 (CPFunction*)command_generator);
4434 rl_attempted_completion_over = 1;
4437 char arg[10240],word[32];
4439 if ((res = sscanf(rl_line_buffer, "%31s %10239[^;]", word, arg)) <= 0) {
4440 rl_attempted_completion_over = 1;
4444 for (i = 0; cmd_array[i].cmd; i++) {
4445 if (!strncmp(cmd_array[i].cmd, word, strlen(word))) {
4450 if(!cmd_array[i].cmd) return NULL;
4452 curret_global_list = cmd_array[i].local_tabcompletes;
4454 completerToUse = cmd_array[i].rl_completerfunction;
4455 if(completerToUse==NULL) /* if no pr. command completer is defined use the default completer */
4456 completerToUse = default_completer;
4458 if(completerToUse) {
4459 #ifdef HAVE_READLINE_RL_COMPLETION_MATCHES
4461 rl_completion_matches(text,
4465 completion_matches(text,
4466 (CPFunction*)completerToUse);
4468 if(!cmd_array[i].complete_filenames)
4469 rl_attempted_completion_over = 1;
4472 if(!cmd_array[i].complete_filenames)
4473 rl_attempted_completion_over = 1;
4483 static void client(void)
4489 #if HAVE_GETTIMEOFDAY
4490 gettimeofday (&tv_start, 0);
4495 char *line_in = NULL;
4496 #if HAVE_READLINE_READLINE_H
4499 line_in=readline(C_PROMPT);
4502 #if HAVE_READLINE_HISTORY_H
4504 add_history(line_in);
4506 strncpy(line, line_in, 10239);
4515 if (!fgets(line, 10239, stdin))
4517 if ((end_p = strchr (line, '\n')))
4520 process_cmd_line(line);
4524 static void show_version(void)
4528 yaz_version(vstr, 0);
4529 printf ("YAZ version: %s\n", YAZ_VERSION);
4530 if (strcmp(vstr, YAZ_VERSION))
4531 printf ("YAZ DLL/SO: %s\n", vstr);
4535 int main(int argc, char **argv)
4538 char *open_command = 0;
4539 char *auth_command = 0;
4544 if (!setlocale(LC_CTYPE, ""))
4545 fprintf (stderr, "setlocale failed\n");
4549 codeset = nl_langinfo(CODESET);
4553 outputCharset = xstrdup(codeset);
4555 ODR_MASK_SET(&z3950_options, Z_Options_search);
4556 ODR_MASK_SET(&z3950_options, Z_Options_present);
4557 ODR_MASK_SET(&z3950_options, Z_Options_namedResultSets);
4558 ODR_MASK_SET(&z3950_options, Z_Options_triggerResourceCtrl);
4559 ODR_MASK_SET(&z3950_options, Z_Options_scan);
4560 ODR_MASK_SET(&z3950_options, Z_Options_sort);
4561 ODR_MASK_SET(&z3950_options, Z_Options_extendedServices);
4562 ODR_MASK_SET(&z3950_options, Z_Options_delSet);
4564 while ((ret = options("k:c:q:a:b:m:v:p:u:t:Vxd:", argv, argc, &arg)) != -2)
4571 open_command = (char *) xmalloc (strlen(arg)+6);
4572 strcpy (open_command, "open ");
4573 strcat (open_command, arg);
4577 fprintf(stderr, "%s: Specify at most one server address\n",
4583 dump_file_prefix = arg;
4586 kilobytes = atoi(arg);
4589 if (!(marc_file = fopen (arg, "a")))
4596 outputCharset = xstrdup(arg);
4599 strncpy (ccl_fields, arg, sizeof(ccl_fields)-1);
4600 ccl_fields[sizeof(ccl_fields)-1] = '\0';
4603 strncpy (cql_fields, arg, sizeof(cql_fields)-1);
4604 cql_fields[sizeof(cql_fields)-1] = '\0';
4607 if (!strcmp(arg, "-"))
4610 ber_file=fopen(arg, "a");
4613 if (!strcmp(arg, "-"))
4616 apdu_file=fopen(arg, "a");
4622 yazProxy=strdup(arg);
4627 auth_command = (char *) xmalloc (strlen(arg)+6);
4628 strcpy (auth_command, "auth ");
4629 strcat (auth_command, arg);
4633 yaz_log_init(yaz_log_mask_str(arg), "", 0);
4639 fprintf (stderr, "Usage: %s [-m <marclog>] [ -a <apdulog>] "
4640 "[-b berdump] [-c cclfields] \n"
4641 "[-q cqlfields] [-p <proxy-addr>] [-u <auth>] "
4642 "[-k size] [-d dump] [-V] [<server-addr>]\n",
4650 #ifdef HAVE_GETTIMEOFDAY
4651 gettimeofday (&tv_start, 0);
4653 process_cmd_line (auth_command);
4654 #if HAVE_READLINE_HISTORY_H
4655 add_history(auth_command);
4657 xfree(auth_command);
4661 #ifdef HAVE_GETTIMEOFDAY
4662 gettimeofday (&tv_start, 0);
4664 process_cmd_line (open_command);
4665 #if HAVE_READLINE_HISTORY_H
4666 add_history(open_command);
4668 xfree(open_command);
4679 * vim600: sw=4 ts=8 fdm=marker
4680 * vim<600: sw=4 ts=8