2 * Copyright (c) 1995-2002, Index Data
3 * See the file LICENSE for details.
5 * $Id: client.c,v 1.176 2002-12-16 13:13:53 adam Exp $
23 #define S_ISREG(x) (x & _S_IFREG)
24 #define S_ISDIR(x) (x & _S_IFDIR)
27 #include <yaz/yaz-util.h>
29 #include <yaz/comstack.h>
31 #include <yaz/proto.h>
32 #include <yaz/marcdisp.h>
33 #include <yaz/diagbib1.h>
34 #include <yaz/otherinfo.h>
35 #include <yaz/charneg.h>
37 #include <yaz/pquery.h>
38 #include <yaz/sortspec.h>
42 #include <yaz/yaz-ccl.h>
44 #if HAVE_READLINE_READLINE_H
45 #include <readline/readline.h>
48 #if HAVE_READLINE_HISTORY_H
49 #include <readline/history.h>
56 #include "tabcomplete.h"
58 #define C_PROMPT "Z> "
60 static char *codeset = 0; /* character set for output */
62 static ODR out, in, print; /* encoding and decoding streams */
63 static FILE *apdu_file = 0;
64 static COMSTACK conn = 0; /* our z-association */
65 static Z_IdAuthentication *auth = 0; /* our current auth definition */
66 char *databaseNames[128];
67 int num_databaseNames = 0;
68 static Z_External *record_last = 0;
69 static int setnumber = -1; /* current result set number */
70 static int smallSetUpperBound = 0;
71 static int largeSetLowerBound = 1;
72 static int mediumSetPresentNumber = 0;
73 static Z_ElementSetNames *elementSetNames = 0;
74 static int setno = 1; /* current set offset */
75 static enum oid_proto protocol = PROTO_Z3950; /* current app protocol */
76 static enum oid_value recordsyntax = VAL_USMARC;
77 static enum oid_value schema = VAL_NONE;
78 static int sent_close = 0;
79 static NMEM session_mem = NULL; /* memory handle for init-response */
80 static Z_InitResponse *session = 0; /* session parameters */
81 static char last_scan_line[512] = "0";
82 static char last_scan_query[512] = "0";
83 static char ccl_fields[512] = "default.bib";
84 static char* esPackageName = 0;
85 static char* yazProxy = 0;
86 static int kilobytes = 1024;
87 static char* yazCharset = 0;
88 static char* yazLang = 0;
91 static char last_cmd[32] = "?";
92 static FILE *marcdump = 0;
93 static char *refid = NULL;
94 static char *last_open_command = NULL;
95 static int auto_reconnect = 0;
103 static QueryType queryType = QueryType_Prefix;
105 static CCL_bibset bibset; /* CCL bibset handle */
107 #if HAVE_READLINE_COMPLETION_OVER
110 /* readline doesn't have this var. Define it ourselves. */
111 int rl_attempted_completion_over = 0;
114 /* set this one to 1, to avoid decode of unknown MARCs */
115 #define AVOID_MARC_DECODE 1
117 /* nice helper macro as extensive tabbing gives spaces at the en of the args lines */
118 #define REMOVE_TAILING_BLANKS(a) {\
119 char* args_end=(a)+strlen(a)-1; \
120 while(isspace(*args_end)) {*args_end=0;--args_end;}; \
123 #define maxOtherInfosSupported 10
127 } extraOtherInfos[maxOtherInfosSupported];
130 void process_cmd_line(char* line);
131 char ** readline_completer(char *text, int start, int end);
132 char *command_generator(const char *text, int state);
133 char** curret_global_list=NULL;
134 int cmd_register_tab(char* arg);
136 ODR getODROutputStream()
141 const char* query_type_as_string(QueryType q)
144 case QueryType_Prefix: return "prefix (RPN sent to server)";
145 case QueryType_CCL: return "CCL (CCL sent to server) ";
146 case QueryType_CCL2RPN: return "CCL -> RPN (RPN sent to server)";
148 return "unknown Query type internal yaz-client error";
153 void do_hex_dump(char* buf,int len)
157 for( i=0; i<len ; i=i+16 )
160 for(x=0 ; i+x<len && x<16; ++x)
162 printf("%2.2X ",(unsigned int)((unsigned char)buf[i+x]));
169 void add_otherInfos(Z_APDU *a)
171 Z_OtherInformation **oi;
175 for(i=0; i<maxOtherInfosSupported; ++i)
177 if(extraOtherInfos[i].oidval != -1)
178 yaz_oi_set_string_oidval(oi, out, extraOtherInfos[i].oidval, 1, extraOtherInfos[i].value);
182 void send_apdu(Z_APDU *a)
191 z_APDU(print, &a, 0, 0);
194 if (!z_APDU(out, &a, 0, 0))
196 odr_perror(out, "Encoding APDU");
199 buf = odr_getbuf(out, &len, 0);
200 /* printf ("sending APDU of size %d\n", len); */
201 if (cs_put(conn, buf, len) < 0)
203 fprintf(stderr, "cs_put: %s", cs_errmsg(cs_errno(conn)));
207 do_hex_dump(buf,len);
208 odr_reset(out); /* release the APDU structure */
211 static void print_stringn(const unsigned char *buf, size_t len)
214 for (i = 0; i<len; i++)
215 if ((buf[i] <= 126 && buf[i] >= 32) || strchr ("\n\r\t\f", buf[i]))
216 printf ("%c", buf[i]);
218 printf ("\\X%02X", buf[i]);
221 static void print_refid (Z_ReferenceId *id)
225 printf ("Reference Id: ");
226 print_stringn (id->buf, id->len);
231 static Z_ReferenceId *set_refid (ODR out)
236 id = (Z_ReferenceId *) odr_malloc (out, sizeof(*id));
237 id->size = id->len = strlen(refid);
238 id->buf = (unsigned char *) odr_malloc (out, id->len);
239 memcpy (id->buf, refid, id->len);
243 /* INIT SERVICE ------------------------------- */
245 static void send_initRequest(const char* type_and_host)
247 Z_APDU *apdu = zget_APDU(out, Z_APDU_initRequest);
248 Z_InitRequest *req = apdu->u.initRequest;
250 ODR_MASK_SET(req->options, Z_Options_search);
251 ODR_MASK_SET(req->options, Z_Options_present);
252 ODR_MASK_SET(req->options, Z_Options_namedResultSets);
253 ODR_MASK_SET(req->options, Z_Options_triggerResourceCtrl);
254 ODR_MASK_SET(req->options, Z_Options_scan);
255 ODR_MASK_SET(req->options, Z_Options_sort);
256 ODR_MASK_SET(req->options, Z_Options_extendedServices);
257 ODR_MASK_SET(req->options, Z_Options_delSet);
259 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_1);
260 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_2);
261 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_3);
263 *req->maximumRecordSize = 1024*kilobytes;
264 *req->preferredMessageSize = 1024*kilobytes;
266 req->idAuthentication = auth;
268 req->referenceId = set_refid (out);
271 yaz_oi_set_string_oidval(&req->otherInfo, out, VAL_PROXY,
274 if (yazCharset || yazLang) {
275 Z_OtherInformation **p;
276 Z_OtherInformationUnit *p0;
278 yaz_oi_APDU(apdu, &p);
280 if ((p0=yaz_oi_update(p, out, NULL, 0, 0))) {
281 ODR_MASK_SET(req->options, Z_Options_negotiationModel);
283 p0->which = Z_OtherInfo_externallyDefinedInfo;
284 p0->information.externallyDefinedInfo =
285 yaz_set_proposal_charneg(out,
286 (const char**)&yazCharset, (yazCharset)?1:0,
287 (const char**)&yazLang, (yazLang)?1:0, 1);
292 printf("Sent initrequest.\n");
295 static int process_initResponse(Z_InitResponse *res)
297 /* save session parameters for later use */
298 session_mem = odr_extract_mem(in);
302 printf("Connection rejected by target.\n");
304 printf("Connection accepted by target.\n");
305 if (res->implementationId)
306 printf("ID : %s\n", res->implementationId);
307 if (res->implementationName)
308 printf("Name : %s\n", res->implementationName);
309 if (res->implementationVersion)
310 printf("Version: %s\n", res->implementationVersion);
311 if (res->userInformationField)
313 printf("UserInformationfield:\n");
314 if (!z_External(print, (Z_External**)&res-> userInformationField,
317 odr_perror(print, "Printing userinfo\n");
320 if (res->userInformationField->which == Z_External_octet)
322 printf("Guessing visiblestring:\n");
323 printf("'%s'\n", res->userInformationField->u. octet_aligned->buf);
328 if (ODR_MASK_GET(res->options, Z_Options_search))
330 if (ODR_MASK_GET(res->options, Z_Options_present))
332 if (ODR_MASK_GET(res->options, Z_Options_delSet))
334 if (ODR_MASK_GET(res->options, Z_Options_resourceReport))
335 printf (" resourceReport");
336 if (ODR_MASK_GET(res->options, Z_Options_resourceCtrl))
337 printf (" resourceCtrl");
338 if (ODR_MASK_GET(res->options, Z_Options_accessCtrl))
339 printf (" accessCtrl");
340 if (ODR_MASK_GET(res->options, Z_Options_scan))
342 if (ODR_MASK_GET(res->options, Z_Options_sort))
344 if (ODR_MASK_GET(res->options, Z_Options_extendedServices))
345 printf (" extendedServices");
346 if (ODR_MASK_GET(res->options, Z_Options_level_1Segmentation))
347 printf (" level1Segmentation");
348 if (ODR_MASK_GET(res->options, Z_Options_level_2Segmentation))
349 printf (" level2Segmentation");
350 if (ODR_MASK_GET(res->options, Z_Options_concurrentOperations))
351 printf (" concurrentOperations");
352 if (ODR_MASK_GET(res->options, Z_Options_namedResultSets))
354 printf (" namedResultSets");
357 if (ODR_MASK_GET(res->options, Z_Options_encapsulation))
358 printf (" encapsulation");
359 if (ODR_MASK_GET(res->options, Z_Options_resultCount))
360 printf (" resultCount");
361 if (ODR_MASK_GET(res->options, Z_Options_negotiationModel))
362 printf (" negotiationModel");
363 if (ODR_MASK_GET(res->options, Z_Options_duplicateDetection))
364 printf (" duplicateDetection");
365 if (ODR_MASK_GET(res->options, Z_Options_queryType104))
366 printf (" queryType104");
369 if (ODR_MASK_GET(res->options, Z_Options_negotiationModel)) {
371 Z_CharSetandLanguageNegotiation *p =
372 yaz_get_charneg_record(res->otherInfo);
376 char *charset=NULL, *lang=NULL;
379 yaz_get_response_charneg(session_mem, p, &charset, &lang,
382 printf("Accepted character set : %s\n", charset);
383 printf("Accepted code language : %s\n", lang ? lang : "none");
384 printf("Accepted records in ...: %d\n", selected );
391 static int cmd_base(char *arg)
398 printf("Usage: base <database> <database> ...\n");
401 for (i = 0; i<num_databaseNames; i++)
402 xfree (databaseNames[i]);
403 num_databaseNames = 0;
407 if (!(cp = strchr(arg, ' ')))
408 cp = arg + strlen(arg);
411 databaseNames[num_databaseNames] = (char *)xmalloc (1 + cp - arg);
412 memcpy (databaseNames[num_databaseNames], arg, cp - arg);
413 databaseNames[num_databaseNames][cp - arg] = '\0';
415 for (cp1 = databaseNames[num_databaseNames]; *cp1 ; cp1++)
427 void cmd_open_remember_last_open_command(char* arg, char* new_open_command)
429 if(last_open_command != arg)
431 if(last_open_command) xfree(last_open_command);
432 last_open_command = xstrdup(new_open_command);
436 int cmd_open(char *arg)
439 char type_and_host[101], base[101];
443 printf("Already connected.\n");
449 nmem_destroy (session_mem);
454 if (strncmp (arg, "unix:", 5) == 0)
457 conn = cs_create_host(arg, 1, &add);
458 cmd_open_remember_last_open_command(arg,arg);
463 if (sscanf (arg, "%100[^/]/%100s", type_and_host, base) < 1)
466 cmd_open_remember_last_open_command(arg,type_and_host);
468 conn = cs_create_host(yazProxy, 1, &add);
470 conn = cs_create_host(type_and_host, 1, &add);
474 printf ("Couldn't create comstack\n");
477 printf("Connecting...");
479 if (cs_connect(conn, add) < 0)
481 printf ("error = %s\n", cs_strerror(conn));
482 if (conn->cerrno == CSYSERR)
485 yaz_strerror(msg, sizeof(msg));
486 printf ("%s\n", msg);
494 send_initRequest(type_and_host);
507 if(!( auto_reconnect && last_open_command) ) return ;
509 open_command = (char *) xmalloc (strlen(last_open_command)+6);
510 strcpy (open_command, "open ");
512 strcat (open_command, last_open_command);
514 process_cmd_line(open_command);
519 int cmd_authentication(char *arg)
521 static Z_IdAuthentication au;
522 static char user[40], group[40], pass[40];
523 static Z_IdPass idPass;
528 printf("Auth field set to null\n");
532 r = sscanf (arg, "%39s %39s %39s", user, group, pass);
535 printf("Auth field set to null\n");
541 au.which = Z_IdAuthentication_open;
547 au.which = Z_IdAuthentication_idPass;
548 au.u.idPass = &idPass;
549 idPass.groupId = NULL;
550 idPass.userId = user;
551 idPass.password = group;
556 au.which = Z_IdAuthentication_idPass;
557 au.u.idPass = &idPass;
558 idPass.groupId = group;
559 idPass.userId = user;
560 idPass.password = pass;
565 /* SEARCH SERVICE ------------------------------ */
566 static void display_record(Z_External *r);
568 static void print_record(const unsigned char *buf, size_t len)
571 print_stringn (buf, len);
572 /* add newline if not already added ... */
573 if (i <= 0 || buf[i-1] != '\n')
577 static void display_record(Z_External *r)
579 oident *ent = oid_getentbyoid(r->direct_reference);
583 * Tell the user what we got.
585 if (r->direct_reference)
587 printf("Record type: ");
589 printf("%s\n", ent->desc);
590 else if (!odr_oid(print, &r->direct_reference, 0, 0))
592 odr_perror(print, "print oid");
596 /* Check if this is a known, ASN.1 type tucked away in an octet string */
597 if (ent && r->which == Z_External_octet)
599 Z_ext_typeent *type = z_ext_getentbyref(ent->value);
605 * Call the given decoder to process the record.
607 odr_setbuf(in, (char*)r->u.octet_aligned->buf,
608 r->u.octet_aligned->len, 0);
609 if (!(*type->fun)(in, (char **)&rr, 0, 0))
611 odr_perror(in, "Decoding constructed record.");
612 fprintf(stderr, "[Near %d]\n", odr_offset(in));
613 fprintf(stderr, "Packet dump:\n---------\n");
614 odr_dumpBER(stderr, (char*)r->u.octet_aligned->buf,
615 r->u.octet_aligned->len);
616 fprintf(stderr, "---------\n");
618 /* note just ignores the error ant print the bytes form the octet_aligned later */
621 * Note: we throw away the original, BER-encoded record here.
622 * Do something else with it if you want to keep it.
624 r->u.sutrs = (Z_SUTRS *) rr; /* we don't actually check the type here. */
625 r->which = type->what;
629 if (ent && ent->oclass != CLASS_RECSYN)
631 if (ent && ent->value == VAL_SOIF)
632 print_record((const unsigned char *) r->u.octet_aligned->buf,
633 r->u.octet_aligned->len);
634 else if (r->which == Z_External_octet)
636 const char *octet_buf = (char*)r->u.octet_aligned->buf;
637 if (ent->value == VAL_TEXT_XML || ent->value == VAL_APPLICATION_XML ||
638 ent->value == VAL_HTML)
640 print_record((const unsigned char *) octet_buf,
641 r->u.octet_aligned->len);
643 else if (ent->value == VAL_POSTSCRIPT)
645 int size = r->u.octet_aligned->len;
648 print_record((const unsigned char *) octet_buf, size);
653 #if AVOID_MARC_DECODE
654 /* primitive check for a marc OID 5.1-29 except 16 */
655 ent->oidsuffix[0] == 5 && ent->oidsuffix[1] < 30 &&
656 ent->oidsuffix[1] != 16
665 yaz_marc_t mt = yaz_marc_create();
667 if (yaz_marc_decode_buf(mt, octet_buf,r->u.octet_aligned->len,
671 if (ent->value == VAL_USMARC)
673 if (octet_buf[9] == 'a')
683 printf ("convert from %s to %s\n", from, codeset);
684 cd = yaz_iconv_open(codeset, from);
687 fwrite (result, 1, rlen, stdout);
691 size_t inbytesleft = rlen;
692 const char *inp = result;
697 size_t outbytesleft = sizeof(outbuf);
699 size_t r = yaz_iconv (cd, (char**) &inp,
701 &outp, &outbytesleft);
702 if (r == (size_t) (-1))
704 int e = yaz_iconv_error(cd);
705 if (e != YAZ_ICONV_E2BIG)
708 fwrite (outbuf, outp - outbuf, 1, stdout);
714 printf ("bad MARC. Dumping as it is:\n");
715 print_record((const unsigned char*) octet_buf,
716 r->u.octet_aligned->len);
718 yaz_marc_destroy(mt);
724 print_record((const unsigned char*) octet_buf,
725 r->u.octet_aligned->len);
729 fwrite (octet_buf, 1, r->u.octet_aligned->len, marcdump);
731 else if (ent && ent->value == VAL_SUTRS)
733 if (r->which != Z_External_sutrs)
735 printf("Expecting single SUTRS type for SUTRS.\n");
738 print_record(r->u.sutrs->buf, r->u.sutrs->len);
740 else if (ent && ent->value == VAL_GRS1)
743 if (r->which != Z_External_grs1)
745 printf("Expecting single GRS type for GRS.\n");
749 yaz_display_grs1(w, r->u.grs1, 0);
755 printf("Unknown record representation.\n");
756 if (!z_External(print, &r, 0, 0))
758 odr_perror(print, "Printing external");
764 static void display_diagrecs(Z_DiagRec **pp, int num)
768 Z_DefaultDiagFormat *r;
770 printf("Diagnostic message(s) from database:\n");
771 for (i = 0; i<num; i++)
773 Z_DiagRec *p = pp[i];
774 if (p->which != Z_DiagRec_defaultFormat)
776 printf("Diagnostic record not in default format.\n");
780 r = p->u.defaultFormat;
781 if (!(ent = oid_getentbyoid(r->diagnosticSetId)) ||
782 ent->oclass != CLASS_DIAGSET || ent->value != VAL_BIB1)
783 printf("Missing or unknown diagset\n");
784 printf(" [%d] %s", *r->condition, diagbib1_str(*r->condition));
787 case Z_DefaultDiagFormat_v2Addinfo:
788 printf (" -- v2 addinfo '%s'\n", r->u.v2Addinfo);
790 case Z_DefaultDiagFormat_v3Addinfo:
791 printf (" -- v3 addinfo '%s'\n", r->u.v3Addinfo);
798 static void display_nameplusrecord(Z_NamePlusRecord *p)
801 printf("[%s]", p->databaseName);
802 if (p->which == Z_NamePlusRecord_surrogateDiagnostic)
803 display_diagrecs(&p->u.surrogateDiagnostic, 1);
804 else if (p->which == Z_NamePlusRecord_databaseRecord)
805 display_record(p->u.databaseRecord);
808 static void display_records(Z_Records *p)
812 if (p->which == Z_Records_NSD)
814 Z_DiagRec dr, *dr_p = &dr;
815 dr.which = Z_DiagRec_defaultFormat;
816 dr.u.defaultFormat = p->u.nonSurrogateDiagnostic;
817 display_diagrecs (&dr_p, 1);
819 else if (p->which == Z_Records_multipleNSD)
820 display_diagrecs (p->u.multipleNonSurDiagnostics->diagRecs,
821 p->u.multipleNonSurDiagnostics->num_diagRecs);
824 printf("Records: %d\n", p->u.databaseOrSurDiagnostics->num_records);
825 for (i = 0; i < p->u.databaseOrSurDiagnostics->num_records; i++)
826 display_nameplusrecord(p->u.databaseOrSurDiagnostics->records[i]);
830 static int send_deleteResultSetRequest(char *arg)
835 Z_APDU *apdu = zget_APDU(out, Z_APDU_deleteResultSetRequest);
836 Z_DeleteResultSetRequest *req = apdu->u.deleteResultSetRequest;
838 req->referenceId = set_refid (out);
840 req->num_resultSetList =
841 sscanf (arg, "%30s %30s %30s %30s %30s %30s %30s %30s",
842 names[0], names[1], names[2], names[3],
843 names[4], names[5], names[6], names[7]);
845 req->deleteFunction = (int *)
846 odr_malloc (out, sizeof(*req->deleteFunction));
847 if (req->num_resultSetList > 0)
849 *req->deleteFunction = Z_DeleteRequest_list;
850 req->resultSetList = (char **)
851 odr_malloc (out, sizeof(*req->resultSetList)*
852 req->num_resultSetList);
853 for (i = 0; i<req->num_resultSetList; i++)
854 req->resultSetList[i] = names[i];
858 *req->deleteFunction = Z_DeleteRequest_all;
859 req->resultSetList = 0;
863 printf("Sent deleteResultSetRequest.\n");
867 static int send_searchRequest(char *arg)
869 Z_APDU *apdu = zget_APDU(out, Z_APDU_searchRequest);
870 Z_SearchRequest *req = apdu->u.searchRequest;
873 struct ccl_rpn_node *rpn = NULL;
876 Z_RPNQuery *RPNquery;
878 YAZ_PQF_Parser pqf_parser;
880 if (queryType == QueryType_CCL2RPN)
882 rpn = ccl_find_str(bibset, arg, &error, &pos);
885 printf("CCL ERROR: %s\n", ccl_err_msg(error));
889 req->referenceId = set_refid (out);
890 if (!strcmp(arg, "@big")) /* strictly for troublemaking */
892 static unsigned char big[2100];
895 /* send a very big referenceid to test transport stack etc. */
896 memset(big, 'A', 2100);
897 bigo.len = bigo.size = 2100;
899 req->referenceId = &bigo;
904 sprintf(setstring, "%d", ++setnumber);
905 req->resultSetName = setstring;
907 *req->smallSetUpperBound = smallSetUpperBound;
908 *req->largeSetLowerBound = largeSetLowerBound;
909 *req->mediumSetPresentNumber = mediumSetPresentNumber;
910 if (smallSetUpperBound > 0 || (largeSetLowerBound > 1 &&
911 mediumSetPresentNumber > 0))
915 prefsyn.proto = protocol;
916 prefsyn.oclass = CLASS_RECSYN;
917 prefsyn.value = recordsyntax;
918 req->preferredRecordSyntax =
919 odr_oiddup(out, oid_ent_to_oid(&prefsyn, oid));
920 req->smallSetElementSetNames =
921 req->mediumSetElementSetNames = elementSetNames;
923 req->num_databaseNames = num_databaseNames;
924 req->databaseNames = databaseNames;
930 case QueryType_Prefix:
931 query.which = Z_Query_type_1;
932 pqf_parser = yaz_pqf_create ();
933 RPNquery = yaz_pqf_parse (pqf_parser, out, arg);
938 int code = yaz_pqf_error (pqf_parser, &pqf_msg, &off);
939 printf("%*s^\n", off+4, "");
940 printf("Prefix query error: %s (code %d)\n", pqf_msg, code);
942 yaz_pqf_destroy (pqf_parser);
945 yaz_pqf_destroy (pqf_parser);
946 query.u.type_1 = RPNquery;
949 query.which = Z_Query_type_2;
950 query.u.type_2 = &ccl_query;
951 ccl_query.buf = (unsigned char*) arg;
952 ccl_query.len = strlen(arg);
954 case QueryType_CCL2RPN:
955 query.which = Z_Query_type_1;
956 RPNquery = ccl_rpn_query(out, rpn);
959 printf ("Couldn't convert from CCL to RPN\n");
962 query.u.type_1 = RPNquery;
963 ccl_rpn_delete (rpn);
966 printf ("Unsupported query type\n");
971 printf("Sent searchRequest.\n");
975 /* display Query Expression as part of searchResult-1 */
976 static void display_queryExpression (Z_QueryExpression *qe)
980 if (qe->which == Z_QueryExpression_term)
982 if (qe->u.term->queryTerm)
984 Z_Term *term = qe->u.term->queryTerm;
988 printf (" %.*s", term->u.general->len, term->u.general->buf);
990 case Z_Term_characterString:
991 printf (" %s", term->u.characterString);
994 printf (" %d", *term->u.numeric);
1004 /* see if we can find USR:SearchResult-1 */
1005 static void display_searchResult (Z_OtherInformation *o)
1010 for (i = 0; i < o->num_elements; i++)
1012 if (o->list[i]->which == Z_OtherInfo_externallyDefinedInfo)
1014 Z_External *ext = o->list[i]->information.externallyDefinedInfo;
1016 if (ext->which == Z_External_searchResult1)
1019 Z_SearchInfoReport *sr = ext->u.searchResult1;
1020 printf ("SearchResult-1:");
1021 for (j = 0; j < sr->num; j++)
1023 if (!sr->elements[j]->subqueryExpression)
1025 display_queryExpression (
1026 sr->elements[j]->subqueryExpression);
1027 display_queryExpression (
1028 sr->elements[j]->subqueryInterpretation);
1029 display_queryExpression (
1030 sr->elements[j]->subqueryRecommendation);
1031 if (sr->elements[j]->subqueryCount)
1032 printf ("(%d)", *sr->elements[j]->subqueryCount);
1040 static int process_searchResponse(Z_SearchResponse *res)
1042 printf ("Received SearchResponse.\n");
1043 print_refid (res->referenceId);
1044 if (*res->searchStatus)
1045 printf("Search was a success.\n");
1047 printf("Search was a bloomin' failure.\n");
1048 printf("Number of hits: %d", *res->resultCount);
1050 printf (", setno %d", setnumber);
1052 display_searchResult (res->additionalSearchInfo);
1053 printf("records returned: %d\n",
1054 *res->numberOfRecordsReturned);
1055 setno += *res->numberOfRecordsReturned;
1057 display_records(res->records);
1061 static void print_level(int iLevel)
1064 for (i = 0; i < iLevel * 4; i++)
1068 static void print_int(int iLevel, const char *pTag, int *pInt)
1072 print_level(iLevel);
1073 printf("%s: %d\n", pTag, *pInt);
1077 static void print_string(int iLevel, const char *pTag, const char *pString)
1079 if (pString != NULL)
1081 print_level(iLevel);
1082 printf("%s: %s\n", pTag, pString);
1086 static void print_oid(int iLevel, const char *pTag, Odr_oid *pOid)
1092 print_level(iLevel);
1093 printf("%s:", pTag);
1094 for (; *pInt != -1; pInt++)
1095 printf(" %d", *pInt);
1100 static void print_referenceId(int iLevel, Z_ReferenceId *referenceId)
1102 if (referenceId != NULL)
1106 print_level(iLevel);
1107 printf("Ref Id (%d, %d): ", referenceId->len, referenceId->size);
1108 for (i = 0; i < referenceId->len; i++)
1109 printf("%c", referenceId->buf[i]);
1114 static void print_string_or_numeric(int iLevel, const char *pTag, Z_StringOrNumeric *pStringNumeric)
1116 if (pStringNumeric != NULL)
1118 switch (pStringNumeric->which)
1120 case Z_StringOrNumeric_string:
1121 print_string(iLevel, pTag, pStringNumeric->u.string);
1124 case Z_StringOrNumeric_numeric:
1125 print_int(iLevel, pTag, pStringNumeric->u.numeric);
1129 print_level(iLevel);
1130 printf("%s: valid type for Z_StringOrNumeric\n", pTag);
1136 static void print_universe_report_duplicate(
1138 Z_UniverseReportDuplicate *pUniverseReportDuplicate)
1140 if (pUniverseReportDuplicate != NULL)
1142 print_level(iLevel);
1143 printf("Universe Report Duplicate: \n");
1145 print_string_or_numeric(iLevel, "Hit No",
1146 pUniverseReportDuplicate->hitno);
1150 static void print_universe_report_hits(
1152 Z_UniverseReportHits *pUniverseReportHits)
1154 if (pUniverseReportHits != NULL)
1156 print_level(iLevel);
1157 printf("Universe Report Hits: \n");
1159 print_string_or_numeric(iLevel, "Database",
1160 pUniverseReportHits->database);
1161 print_string_or_numeric(iLevel, "Hits", pUniverseReportHits->hits);
1165 static void print_universe_report(int iLevel, Z_UniverseReport *pUniverseReport)
1167 if (pUniverseReport != NULL)
1169 print_level(iLevel);
1170 printf("Universe Report: \n");
1172 print_int(iLevel, "Total Hits", pUniverseReport->totalHits);
1173 switch (pUniverseReport->which)
1175 case Z_UniverseReport_databaseHits:
1176 print_universe_report_hits(iLevel,
1177 pUniverseReport->u.databaseHits);
1180 case Z_UniverseReport_duplicate:
1181 print_universe_report_duplicate(iLevel,
1182 pUniverseReport->u.duplicate);
1186 print_level(iLevel);
1187 printf("Type: %d\n", pUniverseReport->which);
1193 static void print_external(int iLevel, Z_External *pExternal)
1195 if (pExternal != NULL)
1197 print_level(iLevel);
1198 printf("External: \n");
1200 print_oid(iLevel, "Direct Reference", pExternal->direct_reference);
1201 print_int(iLevel, "InDirect Reference", pExternal->indirect_reference);
1202 print_string(iLevel, "Descriptor", pExternal->descriptor);
1203 switch (pExternal->which)
1205 case Z_External_universeReport:
1206 print_universe_report(iLevel, pExternal->u.universeReport);
1210 print_level(iLevel);
1211 printf("Type: %d\n", pExternal->which);
1217 static int process_resourceControlRequest (Z_ResourceControlRequest *req)
1219 printf ("Received ResourceControlRequest.\n");
1220 print_referenceId(1, req->referenceId);
1221 print_int(1, "Suspended Flag", req->suspendedFlag);
1222 print_int(1, "Partial Results Available", req->partialResultsAvailable);
1223 print_int(1, "Response Required", req->responseRequired);
1224 print_int(1, "Triggered Request Flag", req->triggeredRequestFlag);
1225 print_external(1, req->resourceReport);
1229 void process_ESResponse(Z_ExtendedServicesResponse *res)
1232 switch (*res->operationStatus)
1234 case Z_ExtendedServicesResponse_done:
1237 case Z_ExtendedServicesResponse_accepted:
1238 printf ("accepted\n");
1240 case Z_ExtendedServicesResponse_failure:
1241 printf ("failure\n");
1242 display_diagrecs(res->diagnostics, res->num_diagnostics);
1245 printf ("unknown\n");
1247 if ( (*res->operationStatus != Z_ExtendedServicesResponse_failure) &&
1248 (res->num_diagnostics != 0) ) {
1249 display_diagrecs(res->diagnostics, res->num_diagnostics);
1251 print_refid (res->referenceId);
1252 if (res->taskPackage &&
1253 res->taskPackage->which == Z_External_extendedService)
1255 Z_TaskPackage *taskPackage = res->taskPackage->u.extendedService;
1256 Odr_oct *id = taskPackage->targetReference;
1257 Z_External *ext = taskPackage->taskSpecificParameters;
1261 printf ("Target Reference: ");
1262 print_stringn (id->buf, id->len);
1265 if (ext->which == Z_External_update)
1267 Z_IUUpdateTaskPackage *utp = ext->u.update->u.taskPackage;
1268 if (utp && utp->targetPart)
1270 Z_IUTargetPart *targetPart = utp->targetPart;
1273 for (i = 0; i<targetPart->num_taskPackageRecords; i++)
1276 Z_IUTaskPackageRecordStructure *tpr =
1277 targetPart->taskPackageRecords[i];
1278 printf ("task package record %d\n", i+1);
1279 if (tpr->which == Z_IUTaskPackageRecordStructure_record)
1281 display_record (tpr->u.record);
1285 printf ("other type\n");
1293 const char *get_ill_element (void *clientData, const char *element)
1298 static Z_External *create_external_itemRequest()
1300 struct ill_get_ctl ctl;
1301 ILL_ItemRequest *req;
1303 int item_request_size = 0;
1304 char *item_request_buf = 0;
1308 ctl.f = get_ill_element;
1310 req = ill_get_ItemRequest(&ctl, "ill", 0);
1312 printf ("ill_get_ItemRequest failed\n");
1314 if (!ill_ItemRequest (out, &req, 0, 0))
1318 ill_ItemRequest(print, &req, 0, 0);
1321 item_request_buf = odr_getbuf (out, &item_request_size, 0);
1322 if (item_request_buf)
1323 odr_setbuf (out, item_request_buf, item_request_size, 1);
1324 printf ("Couldn't encode ItemRequest, size %d\n", item_request_size);
1331 item_request_buf = odr_getbuf (out, &item_request_size, 0);
1332 oid.proto = PROTO_GENERAL;
1333 oid.oclass = CLASS_GENERAL;
1334 oid.value = VAL_ISO_ILL_1;
1336 r = (Z_External *) odr_malloc (out, sizeof(*r));
1337 r->direct_reference = odr_oiddup(out,oid_getoidbyent(&oid));
1338 r->indirect_reference = 0;
1340 r->which = Z_External_single;
1342 r->u.single_ASN1_type = (Odr_oct *)
1343 odr_malloc (out, sizeof(*r->u.single_ASN1_type));
1344 r->u.single_ASN1_type->buf = (unsigned char *)
1345 odr_malloc (out, item_request_size);
1346 r->u.single_ASN1_type->len = item_request_size;
1347 r->u.single_ASN1_type->size = item_request_size;
1348 memcpy (r->u.single_ASN1_type->buf, item_request_buf,
1351 do_hex_dump(item_request_buf,item_request_size);
1356 static Z_External *create_external_ILL_APDU(int which)
1358 struct ill_get_ctl ctl;
1361 int ill_request_size = 0;
1362 char *ill_request_buf = 0;
1366 ctl.f = get_ill_element;
1368 ill_apdu = ill_get_APDU(&ctl, "ill", 0);
1370 if (!ill_APDU (out, &ill_apdu, 0, 0))
1374 printf ("-------------------\n");
1375 ill_APDU(print, &ill_apdu, 0, 0);
1377 printf ("-------------------\n");
1379 ill_request_buf = odr_getbuf (out, &ill_request_size, 0);
1380 if (ill_request_buf)
1381 odr_setbuf (out, ill_request_buf, ill_request_size, 1);
1382 printf ("Couldn't encode ILL-Request, size %d\n", ill_request_size);
1388 ill_request_buf = odr_getbuf (out, &ill_request_size, 0);
1390 oid.proto = PROTO_GENERAL;
1391 oid.oclass = CLASS_GENERAL;
1392 oid.value = VAL_ISO_ILL_1;
1394 r = (Z_External *) odr_malloc (out, sizeof(*r));
1395 r->direct_reference = odr_oiddup(out,oid_getoidbyent(&oid));
1396 r->indirect_reference = 0;
1398 r->which = Z_External_single;
1400 r->u.single_ASN1_type = (Odr_oct *)
1401 odr_malloc (out, sizeof(*r->u.single_ASN1_type));
1402 r->u.single_ASN1_type->buf = (unsigned char *)
1403 odr_malloc (out, ill_request_size);
1404 r->u.single_ASN1_type->len = ill_request_size;
1405 r->u.single_ASN1_type->size = ill_request_size;
1406 memcpy (r->u.single_ASN1_type->buf, ill_request_buf, ill_request_size);
1407 /* printf ("len = %d\n", ill_request_size); */
1408 /* do_hex_dump(ill_request_buf,ill_request_size); */
1409 /* printf("--- end of extenal\n"); */
1416 static Z_External *create_ItemOrderExternal(const char *type, int itemno)
1418 Z_External *r = (Z_External *) odr_malloc(out, sizeof(Z_External));
1419 oident ItemOrderRequest;
1421 ItemOrderRequest.proto = PROTO_Z3950;
1422 ItemOrderRequest.oclass = CLASS_EXTSERV;
1423 ItemOrderRequest.value = VAL_ITEMORDER;
1425 r->direct_reference = odr_oiddup(out,oid_getoidbyent(&ItemOrderRequest));
1426 r->indirect_reference = 0;
1429 r->which = Z_External_itemOrder;
1431 r->u.itemOrder = (Z_ItemOrder *) odr_malloc(out,sizeof(Z_ItemOrder));
1432 memset(r->u.itemOrder, 0, sizeof(Z_ItemOrder));
1433 r->u.itemOrder->which=Z_IOItemOrder_esRequest;
1435 r->u.itemOrder->u.esRequest = (Z_IORequest *)
1436 odr_malloc(out,sizeof(Z_IORequest));
1437 memset(r->u.itemOrder->u.esRequest, 0, sizeof(Z_IORequest));
1439 r->u.itemOrder->u.esRequest->toKeep = (Z_IOOriginPartToKeep *)
1440 odr_malloc(out,sizeof(Z_IOOriginPartToKeep));
1441 memset(r->u.itemOrder->u.esRequest->toKeep, 0, sizeof(Z_IOOriginPartToKeep));
1442 r->u.itemOrder->u.esRequest->notToKeep = (Z_IOOriginPartNotToKeep *)
1443 odr_malloc(out,sizeof(Z_IOOriginPartNotToKeep));
1444 memset(r->u.itemOrder->u.esRequest->notToKeep, 0, sizeof(Z_IOOriginPartNotToKeep));
1446 r->u.itemOrder->u.esRequest->toKeep->supplDescription = NULL;
1447 r->u.itemOrder->u.esRequest->toKeep->contact = NULL;
1448 r->u.itemOrder->u.esRequest->toKeep->addlBilling = NULL;
1450 r->u.itemOrder->u.esRequest->notToKeep->resultSetItem =
1451 (Z_IOResultSetItem *) odr_malloc(out, sizeof(Z_IOResultSetItem));
1452 memset(r->u.itemOrder->u.esRequest->notToKeep->resultSetItem, 0, sizeof(Z_IOResultSetItem));
1453 r->u.itemOrder->u.esRequest->notToKeep->resultSetItem->resultSetId = "1";
1455 r->u.itemOrder->u.esRequest->notToKeep->resultSetItem->item =
1456 (int *) odr_malloc(out, sizeof(int));
1457 *r->u.itemOrder->u.esRequest->notToKeep->resultSetItem->item = itemno;
1459 if (!strcmp (type, "item") || !strcmp(type, "2"))
1461 printf ("using item-request\n");
1462 r->u.itemOrder->u.esRequest->notToKeep->itemRequest =
1463 create_external_itemRequest();
1465 else if (!strcmp(type, "ill") || !strcmp(type, "1"))
1467 printf ("using ILL-request\n");
1468 r->u.itemOrder->u.esRequest->notToKeep->itemRequest =
1469 create_external_ILL_APDU(ILL_APDU_ILL_Request);
1471 else if (!strcmp(type, "xml") || !strcmp(type, "3"))
1473 const char *xml_buf =
1475 " <type>request</type>\n"
1476 " <libraryNo>000200</libraryNo>\n"
1477 " <borrowerTicketNo> 1212 </borrowerTicketNo>\n"
1479 r->u.itemOrder->u.esRequest->notToKeep->itemRequest =
1480 z_ext_record (out, VAL_TEXT_XML, xml_buf, strlen(xml_buf));
1483 r->u.itemOrder->u.esRequest->notToKeep->itemRequest = 0;
1488 static int send_itemorder(const char *type, int itemno)
1490 Z_APDU *apdu = zget_APDU(out, Z_APDU_extendedServicesRequest);
1491 Z_ExtendedServicesRequest *req = apdu->u.extendedServicesRequest;
1492 oident ItemOrderRequest;
1494 ItemOrderRequest.proto = PROTO_Z3950;
1495 ItemOrderRequest.oclass = CLASS_EXTSERV;
1496 ItemOrderRequest.value = VAL_ITEMORDER;
1497 req->packageType = odr_oiddup(out,oid_getoidbyent(&ItemOrderRequest));
1498 req->packageName = esPackageName;
1500 req->taskSpecificParameters = create_ItemOrderExternal(type, itemno);
1506 static int cmd_update(char *arg)
1508 Z_APDU *apdu = zget_APDU(out, Z_APDU_extendedServicesRequest );
1509 Z_ExtendedServicesRequest *req = apdu->u.extendedServicesRequest;
1512 Z_IUOriginPartToKeep *toKeep;
1513 Z_IUSuppliedRecords *notToKeep;
1515 char action[20], recid[20], fname[80];
1517 Z_External *record_this = 0;
1522 sscanf (arg, "%19s %19s %79s", action, recid, fname);
1524 if (!strcmp (action, "insert"))
1525 action_no = Z_IUOriginPartToKeep_recordInsert;
1526 else if (!strcmp (action, "replace"))
1527 action_no = Z_IUOriginPartToKeep_recordReplace;
1528 else if (!strcmp (action, "delete"))
1529 action_no = Z_IUOriginPartToKeep_recordDelete;
1530 else if (!strcmp (action, "update"))
1531 action_no = Z_IUOriginPartToKeep_specialUpdate;
1534 printf ("Bad action: %s\n", action);
1535 printf ("Possible values: insert, replace, delete, update\n");
1543 stat (fname, &status);
1544 if (S_ISREG(status.st_mode) && (inf = fopen(fname, "r")))
1546 size_t len = status.st_size;
1547 char *buf = (char *) xmalloc (len);
1549 fread (buf, 1, len, inf);
1553 record_this = z_ext_record (out, VAL_TEXT_XML, buf, len);
1559 printf ("File %s doesn't exist\n", fname);
1567 printf ("No last record (update ignored)\n");
1570 record_this = record_last;
1573 update_oid.proto = PROTO_Z3950;
1574 update_oid.oclass = CLASS_EXTSERV;
1575 update_oid.value = VAL_DBUPDATE;
1576 oid_ent_to_oid (&update_oid, oid);
1577 req->packageType = odr_oiddup(out,oid);
1578 req->packageName = esPackageName;
1580 req->referenceId = set_refid (out);
1582 r = req->taskSpecificParameters = (Z_External *)
1583 odr_malloc (out, sizeof(*r));
1584 r->direct_reference = odr_oiddup(out,oid);
1585 r->indirect_reference = 0;
1587 r->which = Z_External_update;
1588 r->u.update = (Z_IUUpdate *) odr_malloc(out, sizeof(*r->u.update));
1589 r->u.update->which = Z_IUUpdate_esRequest;
1590 r->u.update->u.esRequest = (Z_IUUpdateEsRequest *)
1591 odr_malloc(out, sizeof(*r->u.update->u.esRequest));
1592 toKeep = r->u.update->u.esRequest->toKeep = (Z_IUOriginPartToKeep *)
1593 odr_malloc(out, sizeof(*r->u.update->u.esRequest->toKeep));
1594 toKeep->databaseName = databaseNames[0];
1596 toKeep->elementSetName = 0;
1597 toKeep->actionQualifier = 0;
1598 toKeep->action = (int *) odr_malloc(out, sizeof(*toKeep->action));
1599 *toKeep->action = action_no;
1601 notToKeep = r->u.update->u.esRequest->notToKeep = (Z_IUSuppliedRecords *)
1602 odr_malloc(out, sizeof(*r->u.update->u.esRequest->notToKeep));
1604 notToKeep->elements = (Z_IUSuppliedRecords_elem **)
1605 odr_malloc(out, sizeof(*notToKeep->elements));
1606 notToKeep->elements[0] = (Z_IUSuppliedRecords_elem *)
1607 odr_malloc(out, sizeof(**notToKeep->elements));
1608 notToKeep->elements[0]->which = Z_IUSuppliedRecords_elem_opaque;
1611 notToKeep->elements[0]->u.opaque = (Odr_oct *)
1612 odr_malloc (out, sizeof(Odr_oct));
1613 notToKeep->elements[0]->u.opaque->buf = (unsigned char *) recid;
1614 notToKeep->elements[0]->u.opaque->size = strlen(recid);
1615 notToKeep->elements[0]->u.opaque->len = strlen(recid);
1618 notToKeep->elements[0]->u.opaque = 0;
1619 notToKeep->elements[0]->supplementalId = 0;
1620 notToKeep->elements[0]->correlationInfo = 0;
1621 notToKeep->elements[0]->record = record_this;
1628 static int cmd_itemorder(char *arg)
1633 if (sscanf (arg, "%10s %d", type, &itemno) != 2)
1636 printf("Item order request\n");
1638 send_itemorder(type, itemno);
1642 static int cmd_find(char *arg)
1646 printf("Find what?\n");
1654 printf("Not connected yet\n");
1658 if (!send_searchRequest(arg))
1663 static int cmd_delete(char *arg)
1667 printf("Not connected yet\n");
1670 if (!send_deleteResultSetRequest(arg))
1675 static int cmd_ssub(char *arg)
1677 if (!(smallSetUpperBound = atoi(arg)))
1682 static int cmd_lslb(char *arg)
1684 if (!(largeSetLowerBound = atoi(arg)))
1689 static int cmd_mspn(char *arg)
1691 if (!(mediumSetPresentNumber = atoi(arg)))
1696 static int cmd_status(char *arg)
1698 printf("smallSetUpperBound: %d\n", smallSetUpperBound);
1699 printf("largeSetLowerBound: %d\n", largeSetLowerBound);
1700 printf("mediumSetPresentNumber: %d\n", mediumSetPresentNumber);
1704 static int cmd_setnames(char *arg)
1706 if (*arg == '1') /* enable ? */
1708 else if (*arg == '0') /* disable ? */
1710 else if (setnumber < 0) /* no args, toggle .. */
1716 printf("Set numbering enabled.\n");
1718 printf("Set numbering disabled.\n");
1722 /* PRESENT SERVICE ----------------------------- */
1724 static int send_presentRequest(char *arg)
1726 Z_APDU *apdu = zget_APDU(out, Z_APDU_presentRequest);
1727 Z_PresentRequest *req = apdu->u.presentRequest;
1728 Z_RecordComposition compo;
1733 char setstring[100];
1735 req->referenceId = set_refid (out);
1736 if ((p = strchr(arg, '+')))
1743 if (p && (p=strchr(p+1, '+')))
1745 strcpy (setstring, p+1);
1746 req->resultSetId = setstring;
1748 else if (setnumber >= 0)
1750 sprintf(setstring, "%d", setnumber);
1751 req->resultSetId = setstring;
1753 req->resultSetStartPoint = &setno;
1754 req->numberOfRecordsRequested = &nos;
1755 prefsyn.proto = protocol;
1756 prefsyn.oclass = CLASS_RECSYN;
1757 prefsyn.value = recordsyntax;
1758 req->preferredRecordSyntax =
1759 odr_oiddup (out, oid_ent_to_oid(&prefsyn, oid));
1761 if (schema != VAL_NONE)
1765 prefschema.proto = protocol;
1766 prefschema.oclass = CLASS_SCHEMA;
1767 prefschema.value = schema;
1769 req->recordComposition = &compo;
1770 compo.which = Z_RecordComp_complex;
1771 compo.u.complex = (Z_CompSpec *)
1772 odr_malloc(out, sizeof(*compo.u.complex));
1773 compo.u.complex->selectAlternativeSyntax = (bool_t *)
1774 odr_malloc(out, sizeof(bool_t));
1775 *compo.u.complex->selectAlternativeSyntax = 0;
1777 compo.u.complex->generic = (Z_Specification *)
1778 odr_malloc(out, sizeof(*compo.u.complex->generic));
1779 compo.u.complex->generic->schema = (Odr_oid *)
1780 odr_oiddup(out, oid_ent_to_oid(&prefschema, oid));
1781 if (!compo.u.complex->generic->schema)
1783 /* OID wasn't a schema! Try record syntax instead. */
1784 prefschema.oclass = CLASS_RECSYN;
1785 compo.u.complex->generic->schema = (Odr_oid *)
1786 odr_oiddup(out, oid_ent_to_oid(&prefschema, oid));
1788 if (!elementSetNames)
1789 compo.u.complex->generic->elementSpec = 0;
1792 compo.u.complex->generic->elementSpec = (Z_ElementSpec *)
1793 odr_malloc(out, sizeof(Z_ElementSpec));
1794 compo.u.complex->generic->elementSpec->which =
1795 Z_ElementSpec_elementSetName;
1796 compo.u.complex->generic->elementSpec->u.elementSetName =
1797 elementSetNames->u.generic;
1799 compo.u.complex->num_dbSpecific = 0;
1800 compo.u.complex->dbSpecific = 0;
1801 compo.u.complex->num_recordSyntax = 0;
1802 compo.u.complex->recordSyntax = 0;
1804 else if (elementSetNames)
1806 req->recordComposition = &compo;
1807 compo.which = Z_RecordComp_simple;
1808 compo.u.simple = elementSetNames;
1811 printf("Sent presentRequest (%d+%d).\n", setno, nos);
1815 static void close_session (void)
1821 nmem_destroy (session_mem);
1827 void process_close(Z_Close *req)
1829 Z_APDU *apdu = zget_APDU(out, Z_APDU_close);
1830 Z_Close *res = apdu->u.close;
1832 static char *reasons[] =
1837 "cost limit reached",
1839 "security violation",
1846 printf("Reason: %s, message: %s\n", reasons[*req->closeReason],
1847 req->diagnosticInformation ? req->diagnosticInformation : "NULL");
1852 *res->closeReason = Z_Close_finished;
1854 printf("Sent response.\n");
1859 static int cmd_show(char *arg)
1863 printf("Not connected yet\n");
1866 if (!send_presentRequest(arg))
1871 int cmd_quit(char *arg)
1873 printf("See you later, alligator.\n");
1879 int cmd_cancel(char *arg)
1881 Z_APDU *apdu = zget_APDU(out, Z_APDU_triggerResourceControlRequest);
1882 Z_TriggerResourceControlRequest *req =
1883 apdu->u.triggerResourceControlRequest;
1888 printf("Session not initialized yet\n");
1891 if (!ODR_MASK_GET(session->options, Z_Options_triggerResourceCtrl))
1893 printf("Target doesn't support cancel (trigger resource ctrl)\n");
1896 *req->requestedAction = Z_TriggerResourceCtrl_cancel;
1897 req->resultSetWanted = &rfalse;
1900 printf("Sent cancel request\n");
1904 int send_scanrequest(const char *query, int pp, int num, const char *term)
1906 Z_APDU *apdu = zget_APDU(out, Z_APDU_scanRequest);
1907 Z_ScanRequest *req = apdu->u.scanRequest;
1910 if (queryType == QueryType_CCL2RPN)
1914 struct ccl_rpn_node *rpn;
1916 rpn = ccl_find_str (bibset, query, &error, &pos);
1919 printf("CCL ERROR: %s\n", ccl_err_msg(error));
1922 bib1.proto = PROTO_Z3950;
1923 bib1.oclass = CLASS_ATTSET;
1924 bib1.value = VAL_BIB1;
1925 req->attributeSet = oid_ent_to_oid (&bib1, oid);
1926 if (!(req->termListAndStartPoint = ccl_scan_query (out, rpn)))
1928 printf("Couldn't convert CCL to Scan term\n");
1931 ccl_rpn_delete (rpn);
1935 YAZ_PQF_Parser pqf_parser = yaz_pqf_create ();
1937 if (!(req->termListAndStartPoint =
1938 yaz_pqf_scan(pqf_parser, out, &req->attributeSet, query)))
1940 const char *pqf_msg;
1942 int code = yaz_pqf_error (pqf_parser, &pqf_msg, &off);
1943 printf("%*s^\n", off+7, "");
1944 printf("Prefix query error: %s (code %d)\n", pqf_msg, code);
1945 yaz_pqf_destroy (pqf_parser);
1948 yaz_pqf_destroy (pqf_parser);
1952 if (req->termListAndStartPoint->term &&
1953 req->termListAndStartPoint->term->which == Z_Term_general &&
1954 req->termListAndStartPoint->term->u.general)
1956 req->termListAndStartPoint->term->u.general->buf =
1957 (unsigned char *) odr_strdup(out, term);
1958 req->termListAndStartPoint->term->u.general->len =
1959 req->termListAndStartPoint->term->u.general->size =
1963 req->referenceId = set_refid (out);
1964 req->num_databaseNames = num_databaseNames;
1965 req->databaseNames = databaseNames;
1966 req->numberOfTermsRequested = #
1967 req->preferredPositionInResponse = &pp;
1972 int send_sortrequest(char *arg, int newset)
1974 Z_APDU *apdu = zget_APDU(out, Z_APDU_sortRequest);
1975 Z_SortRequest *req = apdu->u.sortRequest;
1976 Z_SortKeySpecList *sksl = (Z_SortKeySpecList *)
1977 odr_malloc (out, sizeof(*sksl));
1981 sprintf (setstring, "%d", setnumber);
1983 sprintf (setstring, "default");
1985 req->referenceId = set_refid (out);
1987 req->num_inputResultSetNames = 1;
1988 req->inputResultSetNames = (Z_InternationalString **)
1989 odr_malloc (out, sizeof(*req->inputResultSetNames));
1990 req->inputResultSetNames[0] = odr_strdup (out, setstring);
1992 if (newset && setnumber >= 0)
1993 sprintf (setstring, "%d", ++setnumber);
1995 req->sortedResultSetName = odr_strdup (out, setstring);
1997 req->sortSequence = yaz_sort_spec (out, arg);
1998 if (!req->sortSequence)
2000 printf ("Missing sort specifications\n");
2007 void display_term(Z_TermInfo *t)
2009 if (t->term->which == Z_Term_general)
2011 printf("%.*s", t->term->u.general->len, t->term->u.general->buf);
2012 sprintf(last_scan_line, "%.*s", t->term->u.general->len,
2013 t->term->u.general->buf);
2016 printf("Term (not general)");
2017 if (t->globalOccurrences)
2018 printf (" (%d)\n", *t->globalOccurrences);
2023 void process_scanResponse(Z_ScanResponse *res)
2026 Z_Entry **entries = NULL;
2027 int num_entries = 0;
2029 printf("Received ScanResponse\n");
2030 print_refid (res->referenceId);
2031 printf("%d entries", *res->numberOfEntriesReturned);
2032 if (res->positionOfTerm)
2033 printf (", position=%d", *res->positionOfTerm);
2035 if (*res->scanStatus != Z_Scan_success)
2036 printf("Scan returned code %d\n", *res->scanStatus);
2039 if ((entries = res->entries->entries))
2040 num_entries = res->entries->num_entries;
2041 for (i = 0; i < num_entries; i++)
2043 int pos_term = res->positionOfTerm ? *res->positionOfTerm : -1;
2044 if (entries[i]->which == Z_Entry_termInfo)
2046 printf("%c ", i + 1 == pos_term ? '*' : ' ');
2047 display_term(entries[i]->u.termInfo);
2050 display_diagrecs(&entries[i]->u.surrogateDiagnostic, 1);
2052 if (res->entries->nonsurrogateDiagnostics)
2053 display_diagrecs (res->entries->nonsurrogateDiagnostics,
2054 res->entries->num_nonsurrogateDiagnostics);
2057 void process_sortResponse(Z_SortResponse *res)
2059 printf("Received SortResponse: status=");
2060 switch (*res->sortStatus)
2062 case Z_SortStatus_success:
2063 printf ("success"); break;
2064 case Z_SortStatus_partial_1:
2065 printf ("partial"); break;
2066 case Z_SortStatus_failure:
2067 printf ("failure"); break;
2069 printf ("unknown (%d)", *res->sortStatus);
2072 print_refid (res->referenceId);
2073 if (res->diagnostics)
2074 display_diagrecs(res->diagnostics,
2075 res->num_diagnostics);
2078 void process_deleteResultSetResponse (Z_DeleteResultSetResponse *res)
2080 printf("Got deleteResultSetResponse status=%d\n",
2081 *res->deleteOperationStatus);
2082 if (res->deleteListStatuses)
2085 for (i = 0; i < res->deleteListStatuses->num; i++)
2087 printf ("%s status=%d\n", res->deleteListStatuses->elements[i]->id,
2088 *res->deleteListStatuses->elements[i]->status);
2093 int cmd_sort_generic(char *arg, int newset)
2097 printf("Session not initialized yet\n");
2100 if (!ODR_MASK_GET(session->options, Z_Options_sort))
2102 printf("Target doesn't support sort\n");
2107 if (send_sortrequest(arg, newset) < 0)
2114 int cmd_sort(char *arg)
2116 return cmd_sort_generic (arg, 0);
2119 int cmd_sort_newset (char *arg)
2121 return cmd_sort_generic (arg, 1);
2124 int cmd_scan(char *arg)
2131 printf("Session not initialized yet\n");
2135 if (!ODR_MASK_GET(session->options, Z_Options_scan))
2137 printf("Target doesn't support scan\n");
2142 strcpy (last_scan_query, arg);
2143 if (send_scanrequest(arg, 1, 20, 0) < 0)
2148 if (send_scanrequest(last_scan_query, 1, 20, last_scan_line) < 0)
2154 int cmd_schema(char *arg)
2161 schema = oid_getvalbyname (arg);
2162 if (schema == VAL_NONE)
2164 printf ("unknown schema\n");
2170 int cmd_format(char *arg)
2174 printf("Usage: format <recordsyntax>\n");
2177 recordsyntax = oid_getvalbyname (arg);
2178 if (recordsyntax == VAL_NONE)
2180 printf ("unknown record syntax\n");
2186 int cmd_elements(char *arg)
2188 static Z_ElementSetNames esn;
2189 static char what[100];
2193 elementSetNames = 0;
2197 esn.which = Z_ElementSetNames_generic;
2198 esn.u.generic = what;
2199 elementSetNames = &esn;
2203 int cmd_attributeset(char *arg)
2209 printf("Usage: attributeset <setname>\n");
2212 sscanf(arg, "%s", what);
2213 if (p_query_attset (what))
2215 printf("Unknown attribute set name\n");
2221 int cmd_querytype (char *arg)
2223 if (!strcmp (arg, "ccl"))
2224 queryType = QueryType_CCL;
2225 else if (!strcmp (arg, "prefix") || !strcmp(arg, "rpn"))
2226 queryType = QueryType_Prefix;
2227 else if (!strcmp (arg, "ccl2rpn") || !strcmp (arg, "cclrpn"))
2228 queryType = QueryType_CCL2RPN;
2231 printf ("Querytype must be one of:\n");
2232 printf (" prefix - Prefix query\n");
2233 printf (" ccl - CCL query\n");
2234 printf (" ccl2rpn - CCL query converted to RPN\n");
2240 int cmd_refid (char *arg)
2246 refid = (char *) xmalloc (strlen(arg)+1);
2247 strcpy (refid, arg);
2252 int cmd_close(char *arg)
2259 apdu = zget_APDU(out, Z_APDU_close);
2260 req = apdu->u.close;
2261 *req->closeReason = Z_Close_finished;
2263 printf("Sent close request.\n");
2268 int cmd_packagename(char* arg)
2270 xfree (esPackageName);
2271 esPackageName = NULL;
2274 esPackageName = (char *) xmalloc (strlen(arg)+1);
2275 strcpy (esPackageName, arg);
2280 int cmd_proxy(char* arg)
2291 yazProxy = (char *) xmalloc (strlen(arg)+1);
2292 strcpy (yazProxy, arg);
2297 int cmd_charset(char* arg)
2299 char l1[30], l2[30];
2302 if (sscanf(arg, "%29s %29s", l1, l2) < 1)
2304 printf("Current character set is `%s'\n", (yazCharset) ? yazCharset:NULL);
2310 yazCharset = xstrdup(l1);
2313 odr_set_charset (out, l1, l2);
2314 odr_set_charset (in, l2, l1);
2319 int cmd_lang(char* arg)
2322 printf("Current language is `%s'\n", (yazLang)?yazLang:NULL);
2329 yazLang = (char *) xmalloc (strlen(arg)+1);
2330 strcpy (yazLang, arg);
2335 int cmd_source(char* arg)
2337 /* first should open the file and read one line at a time.. */
2339 char line[1024], *cp;
2342 char* args_end=(arg)+strlen(arg)-1;
2343 while(isspace(*args_end))
2348 REMOVE_TAILING_BLANKS(arg);
2351 fprintf(stderr,"Error in source command use a filename\n");
2355 includeFile = fopen (arg, "r");
2358 fprintf(stderr,"Unable to open file %s for reading\n",arg);
2362 while(!feof(includeFile)) {
2363 memset(line,0,sizeof(line));
2364 fgets(line,sizeof(line),includeFile);
2366 if(strlen(line) < 2) continue;
2367 if(line[0] == '#') continue;
2369 if ((cp = strrchr (line, '\n')))
2372 process_cmd_line(line);
2375 if(fclose(includeFile)<0) {
2376 perror("unable to close include file");
2382 int cmd_subshell(char* args)
2387 system(getenv("SHELL"));
2393 int cmd_set_apdufile(char* arg)
2395 REMOVE_TAILING_BLANKS(arg);
2397 if(apdu_file && apdu_file != stderr) { /* don't close stdout*/
2398 perror("unable to close apdu log file");
2406 if(!strcmp(arg,"-"))
2409 apdu_file=fopen(arg, "a");
2412 perror("unable to open apdu log file no apdu log loaded");
2414 odr_setprint(print, apdu_file);
2420 int cmd_set_cclfile(char* arg)
2424 REMOVE_TAILING_BLANKS(arg);
2426 bibset = ccl_qual_mk ();
2427 inf = fopen (arg, "r");
2430 ccl_qual_file (bibset, inf);
2433 strcpy(ccl_fields,arg);
2438 int cmd_set_auto_reconnect(char* arg)
2440 REMOVE_TAILING_BLANKS(arg);
2442 if(strlen(arg)==0) {
2443 auto_reconnect = ! auto_reconnect;
2444 } else if(strcmp(arg,"on")==0) {
2446 } else if(strcmp(arg,"off")==0) {
2449 printf("Error use on or off\n");
2454 printf("Set auto reconnect enabled.\n");
2456 printf("Set auto reconnect disabled.\n");
2461 int cmd_set_marcdump(char* arg)
2463 if(marcdump && marcdump != stderr) { /* don't close stdout*/
2464 perror("unable to close apdu log file");
2472 if(!strcmp(arg,"-"))
2475 marcdump=fopen(arg, "a");
2478 perror("unable to open apdu marcdump file no marcdump done\n");
2484 int cmd_set_proxy(char* arg)
2486 if(yazProxy) free(yazProxy);
2489 if(strlen(arg) > 1) {
2490 yazProxy=strdup(arg);
2496 this command takes 3 arge {name class oid}
2498 int cmd_register_oid(char* args) {
2503 {"appctx",CLASS_APPCTX},
2504 {"absyn",CLASS_ABSYN},
2505 {"attset",CLASS_ATTSET},
2506 {"transyn",CLASS_TRANSYN},
2507 {"diagset",CLASS_DIAGSET},
2508 {"recsyn",CLASS_RECSYN},
2509 {"resform",CLASS_RESFORM},
2510 {"accform",CLASS_ACCFORM},
2511 {"extserv",CLASS_EXTSERV},
2512 {"userinfo",CLASS_USERINFO},
2513 {"elemspec",CLASS_ELEMSPEC},
2514 {"varset",CLASS_VARSET},
2515 {"schema",CLASS_SCHEMA},
2516 {"tagset",CLASS_TAGSET},
2517 {"general",CLASS_GENERAL},
2518 {0,(enum oid_class) 0}
2520 char oname_str[101], oclass_str[101], oid_str[101];
2523 oid_class oidclass = CLASS_GENERAL;
2524 int val = 0, oid[OID_SIZE];
2525 struct oident * new_oident=NULL;
2527 if (sscanf (args, "%100[^ ] %100[^ ] %100s",
2528 oname_str,oclass_str, oid_str) < 1) {
2529 printf("Error in regristrate command \n");
2533 for (i = 0; oid_classes[i].className; i++) {
2534 if (!strcmp(oid_classes[i].className, oclass_str))
2536 oidclass=oid_classes[i].oclass;
2541 if(!(oid_classes[i].className)) {
2542 printf("Unknonwn oid class %s\n",oclass_str);
2550 while (isdigit (*name))
2552 val = val*10 + (*name - '0');
2565 new_oident=oid_addent (oid,PROTO_GENERAL,oidclass,oname_str,VAL_DYNAMIC);
2566 if(strcmp(new_oident->desc,oname_str)) {
2567 fprintf(stderr,"oid is already named as %s, regristration faild\n",
2573 int cmd_push_command(char* arg)
2575 #if HAVE_READLINE_HISTORY_H
2579 fprintf(stderr,"Not compiled with the readline/history module\n");
2584 void source_rcfile()
2586 /* Look for a $HOME/.yazclientrc and source it if it exists */
2587 struct stat statbuf;
2589 char* homedir=getenv("HOME");
2591 if(!homedir) return;
2593 sprintf(buffer,"%s/.yazclientrc",homedir);
2595 if(stat(buffer,&statbuf)==0) {
2599 if(stat(".yazclientrc",&statbuf)==0) {
2600 cmd_source(".yazclientrc");
2605 static void initialize(void)
2610 if (!(out = odr_createmem(ODR_ENCODE)) ||
2611 !(in = odr_createmem(ODR_DECODE)) ||
2612 !(print = odr_createmem(ODR_PRINT)))
2614 fprintf(stderr, "failed to allocate ODR streams\n");
2619 setvbuf(stdout, 0, _IONBF, 0);
2621 odr_setprint(print, apdu_file);
2623 bibset = ccl_qual_mk ();
2624 inf = fopen (ccl_fields, "r");
2627 ccl_qual_file (bibset, inf);
2630 cmd_base("Default");
2632 #if HAVE_READLINE_READLINE_H
2633 rl_attempted_completion_function = (CPPFunction*)readline_completer;
2637 for(i=0; i<maxOtherInfosSupported; ++i) {
2638 extraOtherInfos[i].oidval = -1;
2645 #if HAVE_GETTIMEOFDAY
2646 struct timeval tv_start, tv_end;
2649 void wait_and_handle_responce()
2654 int netbufferlen = 0;
2662 if ((res = cs_get(conn, &netbuffer, &netbufferlen)) < 0)
2664 printf("Target closed connection\n");
2670 printf("Target closed connection.\n");
2674 odr_reset(in); /* release APDU from last round */
2676 odr_setbuf(in, netbuffer, res, 0);
2677 if (!z_APDU(in, &apdu, 0, 0))
2679 odr_perror(in, "Decoding incoming APDU");
2680 fprintf(stderr, "[Near %d]\n", odr_offset(in));
2681 fprintf(stderr, "Packet dump:\n---------\n");
2682 odr_dumpBER(stderr, netbuffer, res);
2683 fprintf(stderr, "---------\n");
2685 z_APDU(print, &apdu, 0, 0);
2689 if (apdu_file && !z_APDU(print, &apdu, 0, 0))
2691 odr_perror(print, "Failed to print incoming APDU");
2697 case Z_APDU_initResponse:
2698 process_initResponse(apdu->u.initResponse);
2700 case Z_APDU_searchResponse:
2701 process_searchResponse(apdu->u.searchResponse);
2703 case Z_APDU_scanResponse:
2704 process_scanResponse(apdu->u.scanResponse);
2706 case Z_APDU_presentResponse:
2707 print_refid (apdu->u.presentResponse->referenceId);
2709 *apdu->u.presentResponse->numberOfRecordsReturned;
2710 if (apdu->u.presentResponse->records)
2711 display_records(apdu->u.presentResponse->records);
2713 printf("No records.\n");
2714 printf ("nextResultSetPosition = %d\n",
2715 *apdu->u.presentResponse->nextResultSetPosition);
2717 case Z_APDU_sortResponse:
2718 process_sortResponse(apdu->u.sortResponse);
2720 case Z_APDU_extendedServicesResponse:
2721 printf("Got extended services response\n");
2722 process_ESResponse(apdu->u.extendedServicesResponse);
2725 printf("Target has closed the association.\n");
2726 process_close(apdu->u.close);
2728 case Z_APDU_resourceControlRequest:
2729 process_resourceControlRequest
2730 (apdu->u.resourceControlRequest);
2732 case Z_APDU_deleteResultSetResponse:
2733 process_deleteResultSetResponse(apdu->u.
2734 deleteResultSetResponse);
2737 printf("Received unknown APDU type (%d).\n",
2742 while (conn && cs_more(conn));
2743 #if HAVE_GETTIMEOFDAY
2744 gettimeofday (&tv_end, 0);
2746 printf ("S/U S/U=%ld/%ld %ld/%ld",
2747 (long) tv_start.tv_sec,
2748 (long) tv_start.tv_usec,
2749 (long) tv_end.tv_sec,
2750 (long) tv_end.tv_usec);
2752 printf ("Elapsed: %.6f\n",
2753 (double) tv_end.tv_usec / 1e6 + tv_end.tv_sec -
2754 ((double) tv_start.tv_usec / 1e6 + tv_start.tv_sec));
2761 int cmd_cclparse(char* arg)
2764 struct ccl_rpn_node *rpn=NULL;
2767 rpn = ccl_find_str (bibset, arg, &error, &pos);
2770 printf ("%*s^ - ", 3+strlen(last_cmd)+1+pos, " ");
2771 printf ("%s\n", ccl_err_msg (error));
2777 ccl_pr_tree(rpn, stdout);
2781 ccl_rpn_delete(rpn);
2789 int cmd_set_otherinfo(char* args)
2791 char oid[101], otherinfoString[101];
2796 sscan_res = sscanf (args, "%d %100[^ ] %100s", &otherinfoNo, oid, otherinfoString);
2798 /* reset this otherinfo */
2799 if(otherinfoNo>=maxOtherInfosSupported) {
2800 printf("Error otherinfo index to large (%d>%d)\n",otherinfoNo,maxOtherInfosSupported);
2802 extraOtherInfos[otherinfoNo].oidval = -1;
2803 if(extraOtherInfos[otherinfoNo].value) free(extraOtherInfos[otherinfoNo].value);
2807 printf("Error in set_otherinfo command \n");
2811 if(otherinfoNo>=maxOtherInfosSupported) {
2812 printf("Error otherinfo index to large (%d>%d)\n",otherinfoNo,maxOtherInfosSupported);
2816 oidval = oid_getvalbyname (oid);
2818 printf("Error in set_otherinfo command unknown oid %s \n",oid);
2821 extraOtherInfos[otherinfoNo].oidval = oidval;
2822 if(extraOtherInfos[otherinfoNo].value) free(extraOtherInfos[otherinfoNo].value);
2823 extraOtherInfos[otherinfoNo].value = strdup(otherinfoString);
2828 int cmd_list_otherinfo(char* args)
2832 if(strlen(args)>0) {
2834 if( i >= maxOtherInfosSupported ) {
2835 printf("Error otherinfo index to large (%d>%d)\n",i,maxOtherInfosSupported);
2839 if(extraOtherInfos[i].oidval != -1)
2840 printf(" otherinfo %d %s %s\n",
2842 yaz_z3950_oid_value_to_str(
2843 (enum oid_value) extraOtherInfos[i].oidval,
2845 extraOtherInfos[i].value);
2848 for(i=0; i<maxOtherInfosSupported; ++i) {
2849 if(extraOtherInfos[i].oidval != -1)
2850 printf(" otherinfo %d %s %s\n",
2852 yaz_z3950_oid_value_to_str(
2853 (enum oid_value) extraOtherInfos[i].oidval,
2855 extraOtherInfos[i].value);
2863 int cmd_list_all(char* args) {
2866 /* connection options */
2868 printf("Connected to : %s\n",last_open_command);
2870 if(last_open_command)
2871 printf("Not connected to : %s\n",last_open_command);
2873 printf("Not connected : \n");
2876 if(yazProxy) printf("using proxy : %s\n",yazProxy);
2878 printf("auto_reconnect : %s\n",auto_reconnect?"on":"off");
2881 printf("Authentication : none\n");
2883 switch(auth->which) {
2884 case Z_IdAuthentication_idPass:
2885 printf("Authentication : IdPass\n");
2886 printf(" Login User : %s\n",auth->u.idPass->userId?auth->u.idPass->userId:"");
2887 printf(" Login Group : %s\n",auth->u.idPass->groupId?auth->u.idPass->groupId:"");
2888 printf(" Password : %s\n",auth->u.idPass->password?auth->u.idPass->password:"");
2890 case Z_IdAuthentication_open:
2891 printf("Authentication : psOpen\n");
2892 printf(" Open string : %s\n",auth->u.open);
2895 printf("Authentication : Unknown\n");
2898 if ( yazCharset ) printf("Character set : `%s'\n", (yazCharset) ? yazCharset:NULL);
2902 for (i = 0; i<num_databaseNames; i++) printf("%s ",databaseNames[i]);
2906 printf("CCL file : %s\n",ccl_fields);
2907 printf("Query type : %s\n",query_type_as_string(queryType));
2909 printf("Named Result Sets : %s\n",setnumber==-1?"off":"on");
2911 /* piggy back options */
2912 printf("ssub/lslb/mspn : %d/%d/%d\n",smallSetUpperBound,largeSetLowerBound,mediumSetPresentNumber);
2914 /* print present related options */
2915 printf("Format : %s\n",yaz_z3950_oid_value_to_str(recordsyntax,CLASS_RECSYN));
2916 printf("Schema : %s\n",yaz_z3950_oid_value_to_str(schema,CLASS_SCHEMA));
2917 printf("Elements : %s\n",elementSetNames?elementSetNames->u.generic:"");
2919 /* loging options */
2920 printf("APDU log : %s\n",apdu_file?"on":"off");
2921 printf("Record log : %s\n",marcdump?"on":"off");
2924 printf("Other Info: \n");
2925 cmd_list_otherinfo("");
2930 int cmd_clear_otherinfo(char* args)
2932 if(strlen(args)>0) {
2934 otherinfoNo = atoi(args);
2935 if( otherinfoNo >= maxOtherInfosSupported ) {
2936 printf("Error otherinfo index to large (%d>%d)\n",otherinfoNo,maxOtherInfosSupported);
2940 if(extraOtherInfos[otherinfoNo].oidval != -1) {
2941 /* only clear if set. */
2942 extraOtherInfos[otherinfoNo].oidval=-1;
2943 free(extraOtherInfos[otherinfoNo].value);
2948 for(i=0; i<maxOtherInfosSupported; ++i) {
2949 if (extraOtherInfos[i].oidval!=-1 ) {
2950 extraOtherInfos[i].oidval=-1;
2951 free(extraOtherInfos[i].value);
2958 static int cmd_help (char *line);
2960 typedef char *(*completerFunctionType)(const char *text, int state);
2964 int (*fun)(char *arg);
2966 completerFunctionType rl_completerfunction;
2967 int complete_filenames;
2968 char **local_tabcompletes;
2970 {"open", cmd_open, "('tcp'|'ssl')':<host>[':'<port>][/<db>]",NULL,0,NULL},
2971 {"quit", cmd_quit, "",NULL,0,NULL},
2972 {"find", cmd_find, "<query>",NULL,0,NULL},
2973 {"delete", cmd_delete, "<setname>",NULL,0,NULL},
2974 {"base", cmd_base, "<base-name>",NULL,0,NULL},
2975 {"show", cmd_show, "<rec#>['+'<#recs>['+'<setname>]]",NULL,0,NULL},
2976 {"scan", cmd_scan, "<term>",NULL,0,NULL},
2977 {"sort", cmd_sort, "<sortkey> <flag> <sortkey> <flag> ...",NULL,0,NULL},
2978 {"sort+", cmd_sort_newset, "<sortkey> <flag> <sortkey> <flag> ...",NULL,0,NULL},
2979 {"authentication", cmd_authentication, "<acctstring>",NULL,0,NULL},
2980 {"lslb", cmd_lslb, "<largeSetLowerBound>",NULL,0,NULL},
2981 {"ssub", cmd_ssub, "<smallSetUpperBound>",NULL,0,NULL},
2982 {"mspn", cmd_mspn, "<mediumSetPresentNumber>",NULL,0,NULL},
2983 {"status", cmd_status, "",NULL,0,NULL},
2984 {"setnames", cmd_setnames, "",NULL,0,NULL},
2985 {"cancel", cmd_cancel, "",NULL,0,NULL},
2986 {"format", cmd_format, "<recordsyntax>",complete_format,0,NULL},
2987 {"schema", cmd_schema, "<schema>",complete_schema,0,NULL},
2988 {"elements", cmd_elements, "<elementSetName>",NULL,0,NULL},
2989 {"close", cmd_close, "",NULL,0,NULL},
2990 {"attributeset", cmd_attributeset, "<attrset>",complete_attributeset,0,NULL},
2991 {"querytype", cmd_querytype, "<type>",complete_querytype,0,NULL},
2992 {"refid", cmd_refid, "<id>",NULL,0,NULL},
2993 {"itemorder", cmd_itemorder, "ill|item <itemno>",NULL,0,NULL},
2994 {"update", cmd_update, "<action> <recid> [<file>]",NULL,0,NULL},
2995 {"packagename", cmd_packagename, "<packagename>",NULL,0,NULL},
2996 {"proxy", cmd_proxy, "[('tcp'|'ssl')]<host>[':'<port>]",NULL,0,NULL},
2997 {"charset", cmd_charset, "<charset_name>",NULL,0,NULL},
2998 {"lang", cmd_lang, "<language_code>",NULL,0,NULL},
2999 {".", cmd_source, "<filename>",NULL,1,NULL},
3000 {"!", cmd_subshell, "Subshell command",NULL,1,NULL},
3001 {"set_apdufile", cmd_set_apdufile, "<filename>",NULL,1,NULL},
3002 {"set_marcdump", cmd_set_marcdump," <filename>",NULL,1,NULL},
3003 {"set_cclfile", cmd_set_cclfile," <filename>",NULL,1,NULL},
3004 {"set_auto_reconnect", cmd_set_auto_reconnect," on|off",complete_auto_reconnect,1,NULL},
3005 {"set_otherinfo", cmd_set_otherinfo,"<otherinfoinddex> <oid> <string>",NULL,0,NULL},
3006 {"register_oid", cmd_register_oid,"<name> <class> <oid>",NULL,0,NULL},
3007 {"push_command", cmd_push_command,"<command>",command_generator,0,NULL},
3008 {"register_tab", cmd_register_tab,"<commandname> <tab>",command_generator,0,NULL},
3009 {"cclparse", cmd_cclparse,"<ccl find command>",NULL,0,NULL},
3010 {"list_otherinfo",cmd_list_otherinfo,"[otherinfoinddex]",NULL,0,NULL},
3011 {"list_all",cmd_list_all,"",NULL,0,NULL},
3012 {"clear_otherinfo",cmd_clear_otherinfo,"",NULL,0,NULL},
3013 /* Server Admin Functions */
3014 {"adm-reindex", cmd_adm_reindex, "<database-name>",NULL,0,NULL},
3015 {"adm-truncate", cmd_adm_truncate, "('database'|'index')<object-name>",NULL,0,NULL},
3016 {"adm-create", cmd_adm_create, "",NULL,0,NULL},
3017 {"adm-drop", cmd_adm_drop, "('database'|'index')<object-name>",NULL,0,NULL},
3018 {"adm-import", cmd_adm_import, "<record-type> <dir> <pattern>",NULL,0,NULL},
3019 {"adm-refresh", cmd_adm_refresh, "",NULL,0,NULL},
3020 {"adm-commit", cmd_adm_commit, "",NULL,0,NULL},
3021 {"adm-shutdown", cmd_adm_shutdown, "",NULL,0,NULL},
3022 {"adm-startup", cmd_adm_startup, "",NULL,0,NULL},
3023 {"help", cmd_help, "", NULL,0,NULL},
3027 static int cmd_help (char *line)
3033 sscanf (line, "%20s", topic);
3036 printf("Commands:\n");
3037 for (i = 0; cmd[i].cmd; i++)
3038 if (*topic == 0 || strcmp (topic, cmd[i].cmd) == 0)
3039 printf(" %s %s\n", cmd[i].cmd, cmd[i].ad);
3040 if (strcmp (topic, "find") == 0)
3043 printf (" \"term\" Simple Term\n");
3044 printf (" @attr [attset] type=value op Attribute\n");
3045 printf (" @and opl opr And\n");
3046 printf (" @or opl opr Or\n");
3047 printf (" @not opl opr And-Not\n");
3048 printf (" @set set Result set\n");
3050 printf ("Bib-1 attribute types\n");
3052 printf ("4=Title 7=ISBN 8=ISSN 30=Date 62=Abstract 1003=Author 1016=Any\n");
3053 printf ("2=Relation: ");
3054 printf ("1< 2<= 3= 4>= 5> 6!= 102=Relevance\n");
3055 printf ("3=Position: ");
3056 printf ("1=First in Field 2=First in subfield 3=Any position\n");
3057 printf ("4=Structure: ");
3058 printf ("1=Phrase 2=Word 3=Key 4=Year 5=Date 6=WordList\n");
3059 printf ("5=Truncation: ");
3060 printf ("1=Right 2=Left 3=L&R 100=No 101=# 102=Re-1 103=Re-2\n");
3061 printf ("6=Completeness:");
3062 printf ("1=Incomplete subfield 2=Complete subfield 3=Complete field\n");
3067 int cmd_register_tab(char* arg) {
3069 char command[101], tabargument[101];
3074 if (sscanf (arg, "%100s %100s", command, tabargument) < 1) {
3078 /* locate the amdn in the list */
3079 for (i = 0; cmd[i].cmd; i++) {
3080 if (!strncmp(cmd[i].cmd, command, strlen(command))) {
3086 fprintf(stderr,"Unknown command %s\n",command);
3091 if(!cmd[i].local_tabcompletes)
3092 cmd[i].local_tabcompletes = (char **) calloc(1,sizeof(char**));
3096 tabslist = cmd[i].local_tabcompletes;
3097 for(;tabslist && *tabslist;tabslist++) {
3101 cmd[i].local_tabcompletes = (char **)
3102 realloc(cmd[i].local_tabcompletes,(num_of_tabs+2)*sizeof(char**));
3103 tabslist=cmd[i].local_tabcompletes;
3104 tabslist[num_of_tabs]=strdup(tabargument);
3105 tabslist[num_of_tabs+1]=NULL;
3110 void process_cmd_line(char* line)
3113 char word[32], arg[1024];
3115 #if HAVE_GETTIMEOFDAY
3116 gettimeofday (&tv_start, 0);
3119 if ((res = sscanf(line, "%31s %1023[^;]", word, arg)) <= 0)
3121 strcpy(word, last_cmd);
3126 strcpy(last_cmd, word);
3128 /* removed tailing spaces from the arg command */
3131 char* lastnonspace=NULL;
3140 *(++lastnonspace) = 0;
3144 for (i = 0; cmd[i].cmd; i++)
3145 if (!strncmp(cmd[i].cmd, word, strlen(word)))
3147 res = (*cmd[i].fun)(arg);
3151 if (!cmd[i].cmd) /* dump our help-screen */
3153 printf("Unknown command: %s.\n", word);
3154 printf("use help for list of commands\n");
3155 /* cmd_help (""); */
3159 if(apdu_file) fflush(apdu_file);
3162 wait_and_handle_responce();
3171 char *command_generator(const char *text, int state)
3177 for( ; cmd[idx].cmd; ++idx) {
3178 if (!strncmp(cmd[idx].cmd,text,strlen(text))) {
3179 ++idx; /* skip this entry on the next run */
3180 return strdup(cmd[idx-1].cmd);
3188 This function only known how to complete on the first word
3190 char ** readline_completer(char *text, int start, int end) {
3191 #if HAVE_READLINE_READLINE_H
3193 completerFunctionType completerToUse;
3196 #if HAVE_READLINE_RL_COMPLETION_MATCHES
3197 char** res=rl_completion_matches(text,
3200 char** res=completion_matches(text,
3201 (CPFunction*)command_generator);
3203 rl_attempted_completion_over = 1;
3206 char arg[1024],word[32];
3208 if ((res = sscanf(rl_line_buffer, "%31s %1023[^;]", word, arg)) <= 0) {
3209 rl_attempted_completion_over = 1;
3213 for (i = 0; cmd[i].cmd; i++) {
3214 if (!strncmp(cmd[i].cmd, word, strlen(word))) {
3219 if(!cmd[i].cmd) return NULL;
3222 curret_global_list = cmd[i].local_tabcompletes;
3224 completerToUse = cmd[i].rl_completerfunction;
3225 if(completerToUse==NULL) /* if no pr. command completer is defined use the default completer */
3226 completerToUse = default_completer;
3228 if(completerToUse) {
3229 #ifdef HAVE_READLINE_RL_COMPLETION_MATCHES
3231 rl_completion_matches(text,
3235 completion_matches(text,
3236 (CPFunction*)completerToUse);
3238 if(!cmd[i].complete_filenames)
3239 rl_attempted_completion_over = 1;
3242 if(!cmd[i].complete_filenames)
3243 rl_attempted_completion_over = 1;
3253 static void client(void)
3259 #if HAVE_GETTIMEOFDAY
3260 gettimeofday (&tv_start, 0);
3265 char *line_in = NULL;
3266 #if HAVE_READLINE_READLINE_H
3269 line_in=readline(C_PROMPT);
3272 #if HAVE_READLINE_HISTORY_H
3274 add_history(line_in);
3276 strncpy(line, line_in, 1023);
3285 if (!fgets(line, 1023, stdin))
3287 if ((end_p = strchr (line, '\n')))
3290 process_cmd_line(line);
3294 int main(int argc, char **argv)
3297 char *open_command = 0;
3298 char *auth_command = 0;
3303 if (!setlocale(LC_CTYPE, ""))
3304 fprintf (stderr, "setlocale failed\n");
3307 codeset = nl_langinfo(CODESET);
3310 while ((ret = options("k:c:a:m:v:p:u:t:", argv, argc, &arg)) != -2)
3317 open_command = (char *) xmalloc (strlen(arg)+6);
3318 strcpy (open_command, "open ");
3319 strcat (open_command, arg);
3323 kilobytes = atoi(arg);
3326 if (!(marcdump = fopen (arg, "a")))
3336 strncpy (ccl_fields, arg, sizeof(ccl_fields)-1);
3337 ccl_fields[sizeof(ccl_fields)-1] = '\0';
3340 if (!strcmp(arg, "-"))
3343 apdu_file=fopen(arg, "a");
3346 yazProxy=strdup(arg);
3351 auth_command = (char *) xmalloc (strlen(arg)+6);
3352 strcpy (auth_command, "auth ");
3353 strcat (auth_command, arg);
3357 yaz_log_init (yaz_log_mask_str(arg), "", NULL);
3360 fprintf (stderr, "Usage: %s [-m <marclog>] [ -a <apdulog>] "
3361 "[-c cclfields]\n [-p <proxy-addr>] [-u <auth>] "
3362 "[-k size] [<server-addr>]\n",
3370 #ifdef HAVE_GETTIMEOFDAY
3371 gettimeofday (&tv_start, 0);
3373 process_cmd_line (auth_command);
3374 #if HAVE_READLINE_HISTORY_H
3375 add_history(auth_command);
3377 xfree(auth_command);
3381 #ifdef HAVE_GETTIMEOFDAY
3382 gettimeofday (&tv_start, 0);
3384 process_cmd_line (open_command);
3385 #if HAVE_READLINE_HISTORY_H
3386 add_history(open_command);
3388 xfree(open_command);
3399 * vim600: sw=4 ts=8 fdm=marker
3400 * vim<600: sw=4 ts=8