2 * Copyright (c) 1995-2002, Index Data
3 * See the file LICENSE for details.
5 * $Id: client.c,v 1.171 2002-09-24 08:05:41 adam Exp $
13 #include <yaz/yaz-util.h>
15 #include <yaz/comstack.h>
17 #include <yaz/proto.h>
18 #include <yaz/marcdisp.h>
19 #include <yaz/diagbib1.h>
20 #include <yaz/otherinfo.h>
21 #include <yaz/charneg.h>
23 #include <yaz/pquery.h>
24 #include <yaz/sortspec.h>
28 #include <yaz/yaz-ccl.h>
30 #if HAVE_READLINE_READLINE_H
31 #include <readline/readline.h>
34 #if HAVE_READLINE_HISTORY_H
35 #include <readline/history.h>
42 #include "tabcomplete.h"
44 #define C_PROMPT "Z> "
46 static ODR out, in, print; /* encoding and decoding streams */
47 static FILE *apdu_file = 0;
48 static COMSTACK conn = 0; /* our z-association */
49 static Z_IdAuthentication *auth = 0; /* our current auth definition */
50 char *databaseNames[128];
51 int num_databaseNames = 0;
52 static Z_External *record_last = 0;
53 static int setnumber = -1; /* current result set number */
54 static int smallSetUpperBound = 0;
55 static int largeSetLowerBound = 1;
56 static int mediumSetPresentNumber = 0;
57 static Z_ElementSetNames *elementSetNames = 0;
58 static int setno = 1; /* current set offset */
59 static enum oid_proto protocol = PROTO_Z3950; /* current app protocol */
60 static enum oid_value recordsyntax = VAL_USMARC;
61 static enum oid_value schema = VAL_NONE;
62 static int sent_close = 0;
63 static NMEM session_mem = NULL; /* memory handle for init-response */
64 static Z_InitResponse *session = 0; /* session parameters */
65 static char last_scan_line[512] = "0";
66 static char last_scan_query[512] = "0";
67 static char ccl_fields[512] = "default.bib";
68 static char* esPackageName = 0;
69 static char* yazProxy = 0;
70 static int kilobytes = 1024;
71 static char* yazCharset = 0;
72 static char* yazLang = 0;
75 static char last_cmd[32] = "?";
76 static FILE *marcdump = 0;
77 static char *refid = NULL;
78 static char *last_open_command = NULL;
79 static int auto_reconnect = 0;
87 static QueryType queryType = QueryType_Prefix;
89 static CCL_bibset bibset; /* CCL bibset handle */
91 #if HAVE_READLINE_COMPLETION_OVER
94 /* readline doesn't have this var. Define it ourselves. */
95 int rl_attempted_completion_over = 0;
98 /* set this one to 1, to avoid decode of unknown MARCs */
99 #define AVOID_MARC_DECODE 1
101 /* nice helper macro as extensive tabbing gives spaces at the en of the args lines */
102 #define REMOVE_TAILING_BLANKS(a) {\
103 char* args_end=(a)+strlen(a)-1; \
104 while(isspace(*args_end)) {*args_end=0;--args_end;}; \
107 #define maxOtherInfosSupported 10
111 } extraOtherInfos[maxOtherInfosSupported];
114 void process_cmd_line(char* line);
115 char ** readline_completer(char *text, int start, int end);
116 char *command_generator(const char *text, int state);
117 char** curret_global_list=NULL;
118 int cmd_register_tab(char* arg);
120 ODR getODROutputStream()
125 const char* query_type_as_string(QueryType q)
128 case QueryType_Prefix: return "prefix (RPN sent to server)";
129 case QueryType_CCL: return "CCL (CCL sent to server) ";
130 case QueryType_CCL2RPN: return "CCL -> RPN (RPN sent to server)";
132 return "unknown Query type internal yaz-client error";
137 void do_hex_dump(char* buf,int len)
141 for( i=0; i<len ; i=i+16 )
144 for(x=0 ; i+x<len && x<16; ++x)
146 printf("%2.2X ",(unsigned int)((unsigned char)buf[i+x]));
155 void add_otherInfos(Z_APDU *a)
157 Z_OtherInformation **oi;
161 for(i=0; i<maxOtherInfosSupported; ++i)
163 if(extraOtherInfos[i].oidval != -1)
164 yaz_oi_set_string_oidval(oi, out, extraOtherInfos[i].oidval, 1, extraOtherInfos[i].value);
168 void send_apdu(Z_APDU *a)
177 z_APDU(print, &a, 0, 0);
180 if (!z_APDU(out, &a, 0, 0))
182 odr_perror(out, "Encoding APDU");
185 buf = odr_getbuf(out, &len, 0);
186 /* printf ("sending APDU of size %d\n", len); */
187 if (cs_put(conn, buf, len) < 0)
189 fprintf(stderr, "cs_put: %s", cs_errmsg(cs_errno(conn)));
193 do_hex_dump(buf,len);
194 odr_reset(out); /* release the APDU structure */
197 static void print_stringn(const unsigned char *buf, size_t len)
200 for (i = 0; i<len; i++)
201 if ((buf[i] <= 126 && buf[i] >= 32) || strchr ("\n\r\t\f", buf[i]))
202 printf ("%c", buf[i]);
204 printf ("\\X%02X", buf[i]);
207 static void print_refid (Z_ReferenceId *id)
211 printf ("Reference Id: ");
212 print_stringn (id->buf, id->len);
217 static Z_ReferenceId *set_refid (ODR out)
222 id = (Z_ReferenceId *) odr_malloc (out, sizeof(*id));
223 id->size = id->len = strlen(refid);
224 id->buf = (unsigned char *) odr_malloc (out, id->len);
225 memcpy (id->buf, refid, id->len);
229 /* INIT SERVICE ------------------------------- */
231 static void send_initRequest(const char* type_and_host)
233 Z_APDU *apdu = zget_APDU(out, Z_APDU_initRequest);
234 Z_InitRequest *req = apdu->u.initRequest;
236 ODR_MASK_SET(req->options, Z_Options_search);
237 ODR_MASK_SET(req->options, Z_Options_present);
238 ODR_MASK_SET(req->options, Z_Options_namedResultSets);
239 ODR_MASK_SET(req->options, Z_Options_triggerResourceCtrl);
240 ODR_MASK_SET(req->options, Z_Options_scan);
241 ODR_MASK_SET(req->options, Z_Options_sort);
242 ODR_MASK_SET(req->options, Z_Options_extendedServices);
243 ODR_MASK_SET(req->options, Z_Options_delSet);
245 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_1);
246 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_2);
247 ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_3);
249 *req->maximumRecordSize = 1024*kilobytes;
250 *req->preferredMessageSize = 1024*kilobytes;
252 req->idAuthentication = auth;
254 req->referenceId = set_refid (out);
257 yaz_oi_set_string_oidval(&req->otherInfo, out, VAL_PROXY,
260 if (yazCharset || yazLang) {
261 Z_OtherInformation **p;
262 Z_OtherInformationUnit *p0;
264 yaz_oi_APDU(apdu, &p);
266 if ((p0=yaz_oi_update(p, out, NULL, 0, 0))) {
267 ODR_MASK_SET(req->options, Z_Options_negotiationModel);
269 p0->which = Z_OtherInfo_externallyDefinedInfo;
270 p0->information.externallyDefinedInfo =
271 yaz_set_proposal_charneg(out,
272 (const char**)&yazCharset, (yazCharset)?1:0,
273 (const char**)&yazLang, (yazLang)?1:0, 1);
278 printf("Sent initrequest.\n");
281 static int process_initResponse(Z_InitResponse *res)
283 /* save session parameters for later use */
284 session_mem = odr_extract_mem(in);
288 printf("Connection rejected by target.\n");
290 printf("Connection accepted by target.\n");
291 if (res->implementationId)
292 printf("ID : %s\n", res->implementationId);
293 if (res->implementationName)
294 printf("Name : %s\n", res->implementationName);
295 if (res->implementationVersion)
296 printf("Version: %s\n", res->implementationVersion);
297 if (res->userInformationField)
299 printf("UserInformationfield:\n");
300 if (!z_External(print, (Z_External**)&res-> userInformationField,
303 odr_perror(print, "Printing userinfo\n");
306 if (res->userInformationField->which == Z_External_octet)
308 printf("Guessing visiblestring:\n");
309 printf("'%s'\n", res->userInformationField->u. octet_aligned->buf);
314 if (ODR_MASK_GET(res->options, Z_Options_search))
316 if (ODR_MASK_GET(res->options, Z_Options_present))
318 if (ODR_MASK_GET(res->options, Z_Options_delSet))
320 if (ODR_MASK_GET(res->options, Z_Options_resourceReport))
321 printf (" resourceReport");
322 if (ODR_MASK_GET(res->options, Z_Options_resourceCtrl))
323 printf (" resourceCtrl");
324 if (ODR_MASK_GET(res->options, Z_Options_accessCtrl))
325 printf (" accessCtrl");
326 if (ODR_MASK_GET(res->options, Z_Options_scan))
328 if (ODR_MASK_GET(res->options, Z_Options_sort))
330 if (ODR_MASK_GET(res->options, Z_Options_extendedServices))
331 printf (" extendedServices");
332 if (ODR_MASK_GET(res->options, Z_Options_level_1Segmentation))
333 printf (" level1Segmentation");
334 if (ODR_MASK_GET(res->options, Z_Options_level_2Segmentation))
335 printf (" level2Segmentation");
336 if (ODR_MASK_GET(res->options, Z_Options_concurrentOperations))
337 printf (" concurrentOperations");
338 if (ODR_MASK_GET(res->options, Z_Options_namedResultSets))
340 printf (" namedResultSets");
343 if (ODR_MASK_GET(res->options, Z_Options_encapsulation))
344 printf (" encapsulation");
345 if (ODR_MASK_GET(res->options, Z_Options_resultCount))
346 printf (" resultCount");
347 if (ODR_MASK_GET(res->options, Z_Options_negotiationModel))
348 printf (" negotiationModel");
349 if (ODR_MASK_GET(res->options, Z_Options_duplicateDetection))
350 printf (" duplicateDetection");
351 if (ODR_MASK_GET(res->options, Z_Options_queryType104))
352 printf (" queryType104");
355 if (ODR_MASK_GET(res->options, Z_Options_negotiationModel)) {
357 Z_CharSetandLanguageNegotiation *p =
358 yaz_get_charneg_record(res->otherInfo);
362 char *charset=NULL, *lang=NULL;
365 yaz_get_response_charneg(session_mem, p, &charset, &lang,
368 printf("Accepted character set : %s\n", charset);
369 printf("Accepted code language : %s\n", lang ? lang : "none");
370 printf("Accepted records in ...: %d\n", selected );
377 static int cmd_base(char *arg)
384 printf("Usage: base <database> <database> ...\n");
387 for (i = 0; i<num_databaseNames; i++)
388 xfree (databaseNames[i]);
389 num_databaseNames = 0;
393 if (!(cp = strchr(arg, ' ')))
394 cp = arg + strlen(arg);
397 databaseNames[num_databaseNames] = (char *)xmalloc (1 + cp - arg);
398 memcpy (databaseNames[num_databaseNames], arg, cp - arg);
399 databaseNames[num_databaseNames][cp - arg] = '\0';
401 for (cp1 = databaseNames[num_databaseNames]; *cp1 ; cp1++)
413 void cmd_open_remember_last_open_command(char* arg, char* new_open_command)
415 if(last_open_command != arg)
417 if(last_open_command) xfree(last_open_command);
418 last_open_command = xstrdup(new_open_command);
422 int cmd_open(char *arg)
425 char type_and_host[101], base[101];
429 printf("Already connected.\n");
435 nmem_destroy (session_mem);
440 if (strncmp (arg, "unix:", 5) == 0)
443 conn = cs_create_host(arg, 1, &add);
444 cmd_open_remember_last_open_command(arg,arg);
449 if (sscanf (arg, "%100[^/]/%100s", type_and_host, base) < 1)
452 cmd_open_remember_last_open_command(arg,type_and_host);
454 conn = cs_create_host(yazProxy, 1, &add);
456 conn = cs_create_host(type_and_host, 1, &add);
460 printf ("Couldn't create comstack\n");
463 printf("Connecting...");
465 if (cs_connect(conn, add) < 0)
467 printf ("error = %s\n", cs_strerror(conn));
468 if (conn->cerrno == CSYSERR)
476 send_initRequest(type_and_host);
489 if(!( auto_reconnect && last_open_command) ) return ;
491 open_command = (char *) xmalloc (strlen(last_open_command)+6);
492 strcpy (open_command, "open ");
494 strcat (open_command, last_open_command);
496 process_cmd_line(open_command);
501 int cmd_authentication(char *arg)
503 static Z_IdAuthentication au;
504 static char user[40], group[40], pass[40];
505 static Z_IdPass idPass;
510 printf("Auth field set to null\n");
514 r = sscanf (arg, "%39s %39s %39s", user, group, pass);
517 printf("Auth field set to null\n");
523 au.which = Z_IdAuthentication_open;
529 au.which = Z_IdAuthentication_idPass;
530 au.u.idPass = &idPass;
531 idPass.groupId = NULL;
532 idPass.userId = user;
533 idPass.password = group;
538 au.which = Z_IdAuthentication_idPass;
539 au.u.idPass = &idPass;
540 idPass.groupId = group;
541 idPass.userId = user;
542 idPass.password = pass;
547 /* SEARCH SERVICE ------------------------------ */
548 static void display_record(Z_External *r);
550 static void display_variant(Z_Variant *v, int level)
554 for (i = 0; i < v->num_triples; i++)
556 printf("%*sclass=%d,type=%d", level * 4, "", *v->triples[i]->zclass,
557 *v->triples[i]->type);
558 if (v->triples[i]->which == Z_Triple_internationalString)
559 printf(",value=%s\n", v->triples[i]->value.internationalString);
565 static void display_grs1(Z_GenericRecord *r, int level)
573 for (i = 0; i < r->num_elements; i++)
577 printf("%*s", level * 4, "");
581 printf("%d,", *t->tagType);
584 if (t->tagValue->which == Z_StringOrNumeric_numeric)
585 printf("%d) ", *t->tagValue->u.numeric);
587 printf("%s) ", t->tagValue->u.string);
588 if (t->content->which == Z_ElementData_subtree)
590 if (!t->content->u.subtree)
591 printf (" (no subtree)\n");
595 display_grs1(t->content->u.subtree, level+1);
598 else if (t->content->which == Z_ElementData_string)
599 printf("%s\n", t->content->u.string);
600 else if (t->content->which == Z_ElementData_numeric)
601 printf("%d\n", *t->content->u.numeric);
602 else if (t->content->which == Z_ElementData_oid)
604 int *ip = t->content->u.oid;
607 if ((oent = oid_getentbyoid(t->content->u.oid)))
608 printf("OID: %s\n", oent->desc);
612 while (ip && *ip >= 0)
613 printf(" %d", *(ip++));
617 else if (t->content->which == Z_ElementData_noDataRequested)
618 printf("[No data requested]\n");
619 else if (t->content->which == Z_ElementData_elementEmpty)
620 printf("[Element empty]\n");
621 else if (t->content->which == Z_ElementData_elementNotThere)
622 printf("[Element not there]\n");
623 else if (t->content->which == Z_ElementData_date)
624 printf("Date: %s\n", t->content->u.date);
625 else if (t->content->which == Z_ElementData_ext)
627 printf ("External\n");
628 display_record (t->content->u.ext);
631 printf("? type = %d\n",t->content->which);
632 if (t->appliedVariant)
633 display_variant(t->appliedVariant, level+1);
634 if (t->metaData && t->metaData->supportedVariants)
638 printf("%*s---- variant list\n", (level+1)*4, "");
639 for (c = 0; c < t->metaData->num_supportedVariants; c++)
641 printf("%*svariant #%d\n", (level+1)*4, "", c);
642 display_variant(t->metaData->supportedVariants[c], level + 2);
649 static void print_record(const unsigned char *buf, size_t len)
652 print_stringn (buf, len);
653 /* add newline if not already added ... */
654 if (i <= 0 || buf[i-1] != '\n')
658 static void display_record(Z_External *r)
660 oident *ent = oid_getentbyoid(r->direct_reference);
664 * Tell the user what we got.
666 if (r->direct_reference)
668 printf("Record type: ");
670 printf("%s\n", ent->desc);
671 else if (!odr_oid(print, &r->direct_reference, 0, 0))
673 odr_perror(print, "print oid");
677 /* Check if this is a known, ASN.1 type tucked away in an octet string */
678 if (ent && r->which == Z_External_octet)
680 Z_ext_typeent *type = z_ext_getentbyref(ent->value);
686 * Call the given decoder to process the record.
688 odr_setbuf(in, (char*)r->u.octet_aligned->buf,
689 r->u.octet_aligned->len, 0);
690 if (!(*type->fun)(in, (char **)&rr, 0, 0))
692 odr_perror(in, "Decoding constructed record.");
693 fprintf(stderr, "[Near %d]\n", odr_offset(in));
694 fprintf(stderr, "Packet dump:\n---------\n");
695 odr_dumpBER(stderr, (char*)r->u.octet_aligned->buf,
696 r->u.octet_aligned->len);
697 fprintf(stderr, "---------\n");
699 /* note just ignores the error ant print the bytes form the octet_aligned later */
702 * Note: we throw away the original, BER-encoded record here.
703 * Do something else with it if you want to keep it.
705 r->u.sutrs = (Z_SUTRS *) rr; /* we don't actually check the type here. */
706 r->which = type->what;
710 if (ent && ent->oclass != CLASS_RECSYN)
712 if (ent && ent->value == VAL_SOIF)
713 print_record((const unsigned char *) r->u.octet_aligned->buf,
714 r->u.octet_aligned->len);
715 else if (r->which == Z_External_octet && r->u.octet_aligned->len)
717 const char *octet_buf = (char*)r->u.octet_aligned->buf;
718 if (ent->value == VAL_TEXT_XML || ent->value == VAL_APPLICATION_XML ||
719 ent->value == VAL_HTML)
721 print_record((const unsigned char *) octet_buf,
722 r->u.octet_aligned->len);
724 else if (ent->value == VAL_POSTSCRIPT)
726 int size = r->u.octet_aligned->len;
729 print_record((const unsigned char *) octet_buf, size);
734 #if AVOID_MARC_DECODE
735 /* primitive check for a marc OID 5.1-29 except 16 */
736 ent->oidsuffix[0] == 5 && ent->oidsuffix[1] < 30 &&
737 ent->oidsuffix[1] != 16
743 if (marc_display_exl (octet_buf, NULL, 0 /* debug */,
744 r->u.octet_aligned->len) <= 0)
746 printf ("bad MARC. Dumping as it is:\n");
747 print_record((const unsigned char*) octet_buf,
748 r->u.octet_aligned->len);
753 print_record((const unsigned char*) octet_buf,
754 r->u.octet_aligned->len);
758 fwrite (octet_buf, 1, r->u.octet_aligned->len, marcdump);
760 else if (ent && ent->value == VAL_SUTRS)
762 if (r->which != Z_External_sutrs)
764 printf("Expecting single SUTRS type for SUTRS.\n");
767 print_record(r->u.sutrs->buf, r->u.sutrs->len);
769 else if (ent && ent->value == VAL_GRS1)
771 if (r->which != Z_External_grs1)
773 printf("Expecting single GRS type for GRS.\n");
776 display_grs1(r->u.grs1, 0);
780 printf("Unknown record representation.\n");
781 if (!z_External(print, &r, 0, 0))
783 odr_perror(print, "Printing external");
789 static void display_diagrecs(Z_DiagRec **pp, int num)
793 Z_DefaultDiagFormat *r;
795 printf("Diagnostic message(s) from database:\n");
796 for (i = 0; i<num; i++)
798 Z_DiagRec *p = pp[i];
799 if (p->which != Z_DiagRec_defaultFormat)
801 printf("Diagnostic record not in default format.\n");
805 r = p->u.defaultFormat;
806 if (!(ent = oid_getentbyoid(r->diagnosticSetId)) ||
807 ent->oclass != CLASS_DIAGSET || ent->value != VAL_BIB1)
808 printf("Missing or unknown diagset\n");
809 printf(" [%d] %s", *r->condition, diagbib1_str(*r->condition));
812 case Z_DefaultDiagFormat_v2Addinfo:
813 printf (" -- v2 addinfo '%s'\n", r->u.v2Addinfo);
815 case Z_DefaultDiagFormat_v3Addinfo:
816 printf (" -- v3 addinfo '%s'\n", r->u.v3Addinfo);
823 static void display_nameplusrecord(Z_NamePlusRecord *p)
826 printf("[%s]", p->databaseName);
827 if (p->which == Z_NamePlusRecord_surrogateDiagnostic)
828 display_diagrecs(&p->u.surrogateDiagnostic, 1);
829 else if (p->which == Z_NamePlusRecord_databaseRecord)
830 display_record(p->u.databaseRecord);
833 static void display_records(Z_Records *p)
837 if (p->which == Z_Records_NSD)
839 Z_DiagRec dr, *dr_p = &dr;
840 dr.which = Z_DiagRec_defaultFormat;
841 dr.u.defaultFormat = p->u.nonSurrogateDiagnostic;
842 display_diagrecs (&dr_p, 1);
844 else if (p->which == Z_Records_multipleNSD)
845 display_diagrecs (p->u.multipleNonSurDiagnostics->diagRecs,
846 p->u.multipleNonSurDiagnostics->num_diagRecs);
849 printf("Records: %d\n", p->u.databaseOrSurDiagnostics->num_records);
850 for (i = 0; i < p->u.databaseOrSurDiagnostics->num_records; i++)
851 display_nameplusrecord(p->u.databaseOrSurDiagnostics->records[i]);
855 static int send_deleteResultSetRequest(char *arg)
860 Z_APDU *apdu = zget_APDU(out, Z_APDU_deleteResultSetRequest);
861 Z_DeleteResultSetRequest *req = apdu->u.deleteResultSetRequest;
863 req->referenceId = set_refid (out);
865 req->num_resultSetList =
866 sscanf (arg, "%30s %30s %30s %30s %30s %30s %30s %30s",
867 names[0], names[1], names[2], names[3],
868 names[4], names[5], names[6], names[7]);
870 req->deleteFunction = (int *)
871 odr_malloc (out, sizeof(*req->deleteFunction));
872 if (req->num_resultSetList > 0)
874 *req->deleteFunction = Z_DeleteRequest_list;
875 req->resultSetList = (char **)
876 odr_malloc (out, sizeof(*req->resultSetList)*
877 req->num_resultSetList);
878 for (i = 0; i<req->num_resultSetList; i++)
879 req->resultSetList[i] = names[i];
883 *req->deleteFunction = Z_DeleteRequest_all;
884 req->resultSetList = 0;
888 printf("Sent deleteResultSetRequest.\n");
892 static int send_searchRequest(char *arg)
894 Z_APDU *apdu = zget_APDU(out, Z_APDU_searchRequest);
895 Z_SearchRequest *req = apdu->u.searchRequest;
898 struct ccl_rpn_node *rpn = NULL;
901 Z_RPNQuery *RPNquery;
903 YAZ_PQF_Parser pqf_parser;
905 if (queryType == QueryType_CCL2RPN)
907 rpn = ccl_find_str(bibset, arg, &error, &pos);
910 printf("CCL ERROR: %s\n", ccl_err_msg(error));
914 req->referenceId = set_refid (out);
915 if (!strcmp(arg, "@big")) /* strictly for troublemaking */
917 static unsigned char big[2100];
920 /* send a very big referenceid to test transport stack etc. */
921 memset(big, 'A', 2100);
922 bigo.len = bigo.size = 2100;
924 req->referenceId = &bigo;
929 sprintf(setstring, "%d", ++setnumber);
930 req->resultSetName = setstring;
932 *req->smallSetUpperBound = smallSetUpperBound;
933 *req->largeSetLowerBound = largeSetLowerBound;
934 *req->mediumSetPresentNumber = mediumSetPresentNumber;
935 if (smallSetUpperBound > 0 || (largeSetLowerBound > 1 &&
936 mediumSetPresentNumber > 0))
940 prefsyn.proto = protocol;
941 prefsyn.oclass = CLASS_RECSYN;
942 prefsyn.value = recordsyntax;
943 req->preferredRecordSyntax =
944 odr_oiddup(out, oid_ent_to_oid(&prefsyn, oid));
945 req->smallSetElementSetNames =
946 req->mediumSetElementSetNames = elementSetNames;
948 req->num_databaseNames = num_databaseNames;
949 req->databaseNames = databaseNames;
955 case QueryType_Prefix:
956 query.which = Z_Query_type_1;
957 pqf_parser = yaz_pqf_create ();
958 RPNquery = yaz_pqf_parse (pqf_parser, out, arg);
963 int code = yaz_pqf_error (pqf_parser, &pqf_msg, &off);
964 printf("%*s^\n", off+4, "");
965 printf("Prefix query error: %s (code %d)\n", pqf_msg, code);
967 yaz_pqf_destroy (pqf_parser);
970 yaz_pqf_destroy (pqf_parser);
971 query.u.type_1 = RPNquery;
974 query.which = Z_Query_type_2;
975 query.u.type_2 = &ccl_query;
976 ccl_query.buf = (unsigned char*) arg;
977 ccl_query.len = strlen(arg);
979 case QueryType_CCL2RPN:
980 query.which = Z_Query_type_1;
981 RPNquery = ccl_rpn_query(out, rpn);
984 printf ("Couldn't convert from CCL to RPN\n");
987 query.u.type_1 = RPNquery;
988 ccl_rpn_delete (rpn);
991 printf ("Unsupported query type\n");
996 printf("Sent searchRequest.\n");
1000 /* display Query Expression as part of searchResult-1 */
1001 static void display_queryExpression (Z_QueryExpression *qe)
1005 if (qe->which == Z_QueryExpression_term)
1007 if (qe->u.term->queryTerm)
1009 Z_Term *term = qe->u.term->queryTerm;
1010 switch (term->which)
1012 case Z_Term_general:
1013 printf (" %.*s", term->u.general->len, term->u.general->buf);
1015 case Z_Term_characterString:
1016 printf (" %s", term->u.characterString);
1018 case Z_Term_numeric:
1019 printf (" %d", *term->u.numeric);
1029 /* see if we can find USR:SearchResult-1 */
1030 static void display_searchResult (Z_OtherInformation *o)
1035 for (i = 0; i < o->num_elements; i++)
1037 if (o->list[i]->which == Z_OtherInfo_externallyDefinedInfo)
1039 Z_External *ext = o->list[i]->information.externallyDefinedInfo;
1041 if (ext->which == Z_External_searchResult1)
1044 Z_SearchInfoReport *sr = ext->u.searchResult1;
1045 printf ("SearchResult-1:");
1046 for (j = 0; j < sr->num; j++)
1048 if (!sr->elements[j]->subqueryExpression)
1050 display_queryExpression (
1051 sr->elements[j]->subqueryExpression);
1052 display_queryExpression (
1053 sr->elements[j]->subqueryInterpretation);
1054 display_queryExpression (
1055 sr->elements[j]->subqueryRecommendation);
1056 if (sr->elements[j]->subqueryCount)
1057 printf ("(%d)", *sr->elements[j]->subqueryCount);
1065 static int process_searchResponse(Z_SearchResponse *res)
1067 printf ("Received SearchResponse.\n");
1068 print_refid (res->referenceId);
1069 if (*res->searchStatus)
1070 printf("Search was a success.\n");
1072 printf("Search was a bloomin' failure.\n");
1073 printf("Number of hits: %d", *res->resultCount);
1075 printf (", setno %d", setnumber);
1077 display_searchResult (res->additionalSearchInfo);
1078 printf("records returned: %d\n",
1079 *res->numberOfRecordsReturned);
1080 setno += *res->numberOfRecordsReturned;
1082 display_records(res->records);
1086 static void print_level(int iLevel)
1089 for (i = 0; i < iLevel * 4; i++)
1093 static void print_int(int iLevel, const char *pTag, int *pInt)
1097 print_level(iLevel);
1098 printf("%s: %d\n", pTag, *pInt);
1102 static void print_string(int iLevel, const char *pTag, const char *pString)
1104 if (pString != NULL)
1106 print_level(iLevel);
1107 printf("%s: %s\n", pTag, pString);
1111 static void print_oid(int iLevel, const char *pTag, Odr_oid *pOid)
1117 print_level(iLevel);
1118 printf("%s:", pTag);
1119 for (; *pInt != -1; pInt++)
1120 printf(" %d", *pInt);
1125 static void print_referenceId(int iLevel, Z_ReferenceId *referenceId)
1127 if (referenceId != NULL)
1131 print_level(iLevel);
1132 printf("Ref Id (%d, %d): ", referenceId->len, referenceId->size);
1133 for (i = 0; i < referenceId->len; i++)
1134 printf("%c", referenceId->buf[i]);
1139 static void print_string_or_numeric(int iLevel, const char *pTag, Z_StringOrNumeric *pStringNumeric)
1141 if (pStringNumeric != NULL)
1143 switch (pStringNumeric->which)
1145 case Z_StringOrNumeric_string:
1146 print_string(iLevel, pTag, pStringNumeric->u.string);
1149 case Z_StringOrNumeric_numeric:
1150 print_int(iLevel, pTag, pStringNumeric->u.numeric);
1154 print_level(iLevel);
1155 printf("%s: valid type for Z_StringOrNumeric\n", pTag);
1161 static void print_universe_report_duplicate(
1163 Z_UniverseReportDuplicate *pUniverseReportDuplicate)
1165 if (pUniverseReportDuplicate != NULL)
1167 print_level(iLevel);
1168 printf("Universe Report Duplicate: \n");
1170 print_string_or_numeric(iLevel, "Hit No",
1171 pUniverseReportDuplicate->hitno);
1175 static void print_universe_report_hits(
1177 Z_UniverseReportHits *pUniverseReportHits)
1179 if (pUniverseReportHits != NULL)
1181 print_level(iLevel);
1182 printf("Universe Report Hits: \n");
1184 print_string_or_numeric(iLevel, "Database",
1185 pUniverseReportHits->database);
1186 print_string_or_numeric(iLevel, "Hits", pUniverseReportHits->hits);
1190 static void print_universe_report(int iLevel, Z_UniverseReport *pUniverseReport)
1192 if (pUniverseReport != NULL)
1194 print_level(iLevel);
1195 printf("Universe Report: \n");
1197 print_int(iLevel, "Total Hits", pUniverseReport->totalHits);
1198 switch (pUniverseReport->which)
1200 case Z_UniverseReport_databaseHits:
1201 print_universe_report_hits(iLevel,
1202 pUniverseReport->u.databaseHits);
1205 case Z_UniverseReport_duplicate:
1206 print_universe_report_duplicate(iLevel,
1207 pUniverseReport->u.duplicate);
1211 print_level(iLevel);
1212 printf("Type: %d\n", pUniverseReport->which);
1218 static void print_external(int iLevel, Z_External *pExternal)
1220 if (pExternal != NULL)
1222 print_level(iLevel);
1223 printf("External: \n");
1225 print_oid(iLevel, "Direct Reference", pExternal->direct_reference);
1226 print_int(iLevel, "InDirect Reference", pExternal->indirect_reference);
1227 print_string(iLevel, "Descriptor", pExternal->descriptor);
1228 switch (pExternal->which)
1230 case Z_External_universeReport:
1231 print_universe_report(iLevel, pExternal->u.universeReport);
1235 print_level(iLevel);
1236 printf("Type: %d\n", pExternal->which);
1242 static int process_resourceControlRequest (Z_ResourceControlRequest *req)
1244 printf ("Received ResourceControlRequest.\n");
1245 print_referenceId(1, req->referenceId);
1246 print_int(1, "Suspended Flag", req->suspendedFlag);
1247 print_int(1, "Partial Results Available", req->partialResultsAvailable);
1248 print_int(1, "Response Required", req->responseRequired);
1249 print_int(1, "Triggered Request Flag", req->triggeredRequestFlag);
1250 print_external(1, req->resourceReport);
1254 void process_ESResponse(Z_ExtendedServicesResponse *res)
1257 switch (*res->operationStatus)
1259 case Z_ExtendedServicesResponse_done:
1262 case Z_ExtendedServicesResponse_accepted:
1263 printf ("accepted\n");
1265 case Z_ExtendedServicesResponse_failure:
1266 printf ("failure\n");
1267 display_diagrecs(res->diagnostics, res->num_diagnostics);
1270 printf ("unknown\n");
1272 if ( (*res->operationStatus != Z_ExtendedServicesResponse_failure) &&
1273 (res->num_diagnostics != 0) ) {
1274 display_diagrecs(res->diagnostics, res->num_diagnostics);
1276 print_refid (res->referenceId);
1277 if (res->taskPackage &&
1278 res->taskPackage->which == Z_External_extendedService)
1280 Z_TaskPackage *taskPackage = res->taskPackage->u.extendedService;
1281 Odr_oct *id = taskPackage->targetReference;
1282 Z_External *ext = taskPackage->taskSpecificParameters;
1286 printf ("Target Reference: ");
1287 print_stringn (id->buf, id->len);
1290 if (ext->which == Z_External_update)
1292 Z_IUUpdateTaskPackage *utp = ext->u.update->u.taskPackage;
1293 if (utp && utp->targetPart)
1295 Z_IUTargetPart *targetPart = utp->targetPart;
1298 for (i = 0; i<targetPart->num_taskPackageRecords; i++)
1301 Z_IUTaskPackageRecordStructure *tpr =
1302 targetPart->taskPackageRecords[i];
1303 printf ("task package record %d\n", i+1);
1304 if (tpr->which == Z_IUTaskPackageRecordStructure_record)
1306 display_record (tpr->u.record);
1310 printf ("other type\n");
1318 const char *get_ill_element (void *clientData, const char *element)
1323 static Z_External *create_external_itemRequest()
1325 struct ill_get_ctl ctl;
1326 ILL_ItemRequest *req;
1328 int item_request_size = 0;
1329 char *item_request_buf = 0;
1333 ctl.f = get_ill_element;
1335 req = ill_get_ItemRequest(&ctl, "ill", 0);
1337 printf ("ill_get_ItemRequest failed\n");
1339 if (!ill_ItemRequest (out, &req, 0, 0))
1343 ill_ItemRequest(print, &req, 0, 0);
1346 item_request_buf = odr_getbuf (out, &item_request_size, 0);
1347 if (item_request_buf)
1348 odr_setbuf (out, item_request_buf, item_request_size, 1);
1349 printf ("Couldn't encode ItemRequest, size %d\n", item_request_size);
1356 item_request_buf = odr_getbuf (out, &item_request_size, 0);
1357 oid.proto = PROTO_GENERAL;
1358 oid.oclass = CLASS_GENERAL;
1359 oid.value = VAL_ISO_ILL_1;
1361 r = (Z_External *) odr_malloc (out, sizeof(*r));
1362 r->direct_reference = odr_oiddup(out,oid_getoidbyent(&oid));
1363 r->indirect_reference = 0;
1365 r->which = Z_External_single;
1367 r->u.single_ASN1_type = (Odr_oct *)
1368 odr_malloc (out, sizeof(*r->u.single_ASN1_type));
1369 r->u.single_ASN1_type->buf = (unsigned char *)
1370 odr_malloc (out, item_request_size);
1371 r->u.single_ASN1_type->len = item_request_size;
1372 r->u.single_ASN1_type->size = item_request_size;
1373 memcpy (r->u.single_ASN1_type->buf, item_request_buf,
1376 do_hex_dump(item_request_buf,item_request_size);
1381 static Z_External *create_external_ILL_APDU(int which)
1383 struct ill_get_ctl ctl;
1386 int ill_request_size = 0;
1387 char *ill_request_buf = 0;
1391 ctl.f = get_ill_element;
1393 ill_apdu = ill_get_APDU(&ctl, "ill", 0);
1395 if (!ill_APDU (out, &ill_apdu, 0, 0))
1399 printf ("-------------------\n");
1400 ill_APDU(print, &ill_apdu, 0, 0);
1402 printf ("-------------------\n");
1404 ill_request_buf = odr_getbuf (out, &ill_request_size, 0);
1405 if (ill_request_buf)
1406 odr_setbuf (out, ill_request_buf, ill_request_size, 1);
1407 printf ("Couldn't encode ILL-Request, size %d\n", ill_request_size);
1413 ill_request_buf = odr_getbuf (out, &ill_request_size, 0);
1415 oid.proto = PROTO_GENERAL;
1416 oid.oclass = CLASS_GENERAL;
1417 oid.value = VAL_ISO_ILL_1;
1419 r = (Z_External *) odr_malloc (out, sizeof(*r));
1420 r->direct_reference = odr_oiddup(out,oid_getoidbyent(&oid));
1421 r->indirect_reference = 0;
1423 r->which = Z_External_single;
1425 r->u.single_ASN1_type = (Odr_oct *)
1426 odr_malloc (out, sizeof(*r->u.single_ASN1_type));
1427 r->u.single_ASN1_type->buf = (unsigned char *)
1428 odr_malloc (out, ill_request_size);
1429 r->u.single_ASN1_type->len = ill_request_size;
1430 r->u.single_ASN1_type->size = ill_request_size;
1431 memcpy (r->u.single_ASN1_type->buf, ill_request_buf, ill_request_size);
1432 /* printf ("len = %d\n", ill_request_size); */
1433 /* do_hex_dump(ill_request_buf,ill_request_size); */
1434 /* printf("--- end of extenal\n"); */
1441 static Z_External *create_ItemOrderExternal(const char *type, int itemno)
1443 Z_External *r = (Z_External *) odr_malloc(out, sizeof(Z_External));
1444 oident ItemOrderRequest;
1446 ItemOrderRequest.proto = PROTO_Z3950;
1447 ItemOrderRequest.oclass = CLASS_EXTSERV;
1448 ItemOrderRequest.value = VAL_ITEMORDER;
1450 r->direct_reference = odr_oiddup(out,oid_getoidbyent(&ItemOrderRequest));
1451 r->indirect_reference = 0;
1454 r->which = Z_External_itemOrder;
1456 r->u.itemOrder = (Z_ItemOrder *) odr_malloc(out,sizeof(Z_ItemOrder));
1457 memset(r->u.itemOrder, 0, sizeof(Z_ItemOrder));
1458 r->u.itemOrder->which=Z_IOItemOrder_esRequest;
1460 r->u.itemOrder->u.esRequest = (Z_IORequest *)
1461 odr_malloc(out,sizeof(Z_IORequest));
1462 memset(r->u.itemOrder->u.esRequest, 0, sizeof(Z_IORequest));
1464 r->u.itemOrder->u.esRequest->toKeep = (Z_IOOriginPartToKeep *)
1465 odr_malloc(out,sizeof(Z_IOOriginPartToKeep));
1466 memset(r->u.itemOrder->u.esRequest->toKeep, 0, sizeof(Z_IOOriginPartToKeep));
1467 r->u.itemOrder->u.esRequest->notToKeep = (Z_IOOriginPartNotToKeep *)
1468 odr_malloc(out,sizeof(Z_IOOriginPartNotToKeep));
1469 memset(r->u.itemOrder->u.esRequest->notToKeep, 0, sizeof(Z_IOOriginPartNotToKeep));
1471 r->u.itemOrder->u.esRequest->toKeep->supplDescription = NULL;
1472 r->u.itemOrder->u.esRequest->toKeep->contact = NULL;
1473 r->u.itemOrder->u.esRequest->toKeep->addlBilling = NULL;
1475 r->u.itemOrder->u.esRequest->notToKeep->resultSetItem =
1476 (Z_IOResultSetItem *) odr_malloc(out, sizeof(Z_IOResultSetItem));
1477 memset(r->u.itemOrder->u.esRequest->notToKeep->resultSetItem, 0, sizeof(Z_IOResultSetItem));
1478 r->u.itemOrder->u.esRequest->notToKeep->resultSetItem->resultSetId = "1";
1480 r->u.itemOrder->u.esRequest->notToKeep->resultSetItem->item =
1481 (int *) odr_malloc(out, sizeof(int));
1482 *r->u.itemOrder->u.esRequest->notToKeep->resultSetItem->item = itemno;
1484 if (!strcmp (type, "item") || !strcmp(type, "2"))
1486 printf ("using item-request\n");
1487 r->u.itemOrder->u.esRequest->notToKeep->itemRequest =
1488 create_external_itemRequest();
1490 else if (!strcmp(type, "ill") || !strcmp(type, "1"))
1492 printf ("using ILL-request\n");
1493 r->u.itemOrder->u.esRequest->notToKeep->itemRequest =
1494 create_external_ILL_APDU(ILL_APDU_ILL_Request);
1496 else if (!strcmp(type, "xml") || !strcmp(type, "3"))
1498 const char *xml_buf =
1500 " <type>request</type>\n"
1501 " <libraryNo>000200</libraryNo>\n"
1502 " <borrowerTicketNo> 1212 </borrowerTicketNo>\n"
1504 r->u.itemOrder->u.esRequest->notToKeep->itemRequest =
1505 z_ext_record (out, VAL_TEXT_XML, xml_buf, strlen(xml_buf));
1508 r->u.itemOrder->u.esRequest->notToKeep->itemRequest = 0;
1513 static int send_itemorder(const char *type, int itemno)
1515 Z_APDU *apdu = zget_APDU(out, Z_APDU_extendedServicesRequest);
1516 Z_ExtendedServicesRequest *req = apdu->u.extendedServicesRequest;
1517 oident ItemOrderRequest;
1519 ItemOrderRequest.proto = PROTO_Z3950;
1520 ItemOrderRequest.oclass = CLASS_EXTSERV;
1521 ItemOrderRequest.value = VAL_ITEMORDER;
1522 req->packageType = odr_oiddup(out,oid_getoidbyent(&ItemOrderRequest));
1523 req->packageName = esPackageName;
1525 req->taskSpecificParameters = create_ItemOrderExternal(type, itemno);
1531 static int cmd_update(char *arg)
1533 Z_APDU *apdu = zget_APDU(out, Z_APDU_extendedServicesRequest );
1534 Z_ExtendedServicesRequest *req = apdu->u.extendedServicesRequest;
1537 Z_IUOriginPartToKeep *toKeep;
1538 Z_IUSuppliedRecords *notToKeep;
1540 char action[20], recid[20], fname[80];
1542 Z_External *record_this = 0;
1547 sscanf (arg, "%19s %19s %79s", action, recid, fname);
1549 if (!strcmp (action, "insert"))
1550 action_no = Z_IUOriginPartToKeep_recordInsert;
1551 else if (!strcmp (action, "replace"))
1552 action_no = Z_IUOriginPartToKeep_recordReplace;
1553 else if (!strcmp (action, "delete"))
1554 action_no = Z_IUOriginPartToKeep_recordDelete;
1555 else if (!strcmp (action, "update"))
1556 action_no = Z_IUOriginPartToKeep_specialUpdate;
1559 printf ("Bad action: %s\n", action);
1560 printf ("Possible values: insert, replace, delete, update\n");
1568 stat (fname, &status);
1569 if (S_ISREG(status.st_mode) && (inf = fopen(fname, "r")))
1571 size_t len = status.st_size;
1572 char *buf = (char *) xmalloc (len);
1574 fread (buf, 1, len, inf);
1578 record_this = z_ext_record (out, VAL_TEXT_XML, buf, len);
1584 printf ("File %s doesn't exist\n", fname);
1592 printf ("No last record (update ignored)\n");
1595 record_this = record_last;
1598 update_oid.proto = PROTO_Z3950;
1599 update_oid.oclass = CLASS_EXTSERV;
1600 update_oid.value = VAL_DBUPDATE;
1601 oid_ent_to_oid (&update_oid, oid);
1602 req->packageType = odr_oiddup(out,oid);
1603 req->packageName = esPackageName;
1605 req->referenceId = set_refid (out);
1607 r = req->taskSpecificParameters = (Z_External *)
1608 odr_malloc (out, sizeof(*r));
1609 r->direct_reference = odr_oiddup(out,oid);
1610 r->indirect_reference = 0;
1612 r->which = Z_External_update;
1613 r->u.update = (Z_IUUpdate *) odr_malloc(out, sizeof(*r->u.update));
1614 r->u.update->which = Z_IUUpdate_esRequest;
1615 r->u.update->u.esRequest = (Z_IUUpdateEsRequest *)
1616 odr_malloc(out, sizeof(*r->u.update->u.esRequest));
1617 toKeep = r->u.update->u.esRequest->toKeep = (Z_IUOriginPartToKeep *)
1618 odr_malloc(out, sizeof(*r->u.update->u.esRequest->toKeep));
1619 toKeep->databaseName = databaseNames[0];
1621 toKeep->elementSetName = 0;
1622 toKeep->actionQualifier = 0;
1623 toKeep->action = (int *) odr_malloc(out, sizeof(*toKeep->action));
1624 *toKeep->action = action_no;
1626 notToKeep = r->u.update->u.esRequest->notToKeep = (Z_IUSuppliedRecords *)
1627 odr_malloc(out, sizeof(*r->u.update->u.esRequest->notToKeep));
1629 notToKeep->elements = (Z_IUSuppliedRecords_elem **)
1630 odr_malloc(out, sizeof(*notToKeep->elements));
1631 notToKeep->elements[0] = (Z_IUSuppliedRecords_elem *)
1632 odr_malloc(out, sizeof(**notToKeep->elements));
1633 notToKeep->elements[0]->which = Z_IUSuppliedRecords_elem_opaque;
1636 notToKeep->elements[0]->u.opaque = (Odr_oct *)
1637 odr_malloc (out, sizeof(Odr_oct));
1638 notToKeep->elements[0]->u.opaque->buf = (unsigned char *) recid;
1639 notToKeep->elements[0]->u.opaque->size = strlen(recid);
1640 notToKeep->elements[0]->u.opaque->len = strlen(recid);
1643 notToKeep->elements[0]->u.opaque = 0;
1644 notToKeep->elements[0]->supplementalId = 0;
1645 notToKeep->elements[0]->correlationInfo = 0;
1646 notToKeep->elements[0]->record = record_this;
1653 static int cmd_itemorder(char *arg)
1658 if (sscanf (arg, "%10s %d", type, &itemno) != 2)
1661 printf("Item order request\n");
1663 send_itemorder(type, itemno);
1667 static int cmd_find(char *arg)
1671 printf("Find what?\n");
1679 printf("Not connected yet\n");
1683 if (!send_searchRequest(arg))
1688 static int cmd_delete(char *arg)
1692 printf("Not connected yet\n");
1695 if (!send_deleteResultSetRequest(arg))
1700 static int cmd_ssub(char *arg)
1702 if (!(smallSetUpperBound = atoi(arg)))
1707 static int cmd_lslb(char *arg)
1709 if (!(largeSetLowerBound = atoi(arg)))
1714 static int cmd_mspn(char *arg)
1716 if (!(mediumSetPresentNumber = atoi(arg)))
1721 static int cmd_status(char *arg)
1723 printf("smallSetUpperBound: %d\n", smallSetUpperBound);
1724 printf("largeSetLowerBound: %d\n", largeSetLowerBound);
1725 printf("mediumSetPresentNumber: %d\n", mediumSetPresentNumber);
1729 static int cmd_setnames(char *arg)
1731 if (*arg == '1') /* enable ? */
1733 else if (*arg == '0') /* disable ? */
1735 else if (setnumber < 0) /* no args, toggle .. */
1741 printf("Set numbering enabled.\n");
1743 printf("Set numbering disabled.\n");
1747 /* PRESENT SERVICE ----------------------------- */
1749 static int send_presentRequest(char *arg)
1751 Z_APDU *apdu = zget_APDU(out, Z_APDU_presentRequest);
1752 Z_PresentRequest *req = apdu->u.presentRequest;
1753 Z_RecordComposition compo;
1758 char setstring[100];
1760 req->referenceId = set_refid (out);
1761 if ((p = strchr(arg, '+')))
1768 if (p && (p=strchr(p+1, '+')))
1770 strcpy (setstring, p+1);
1771 req->resultSetId = setstring;
1773 else if (setnumber >= 0)
1775 sprintf(setstring, "%d", setnumber);
1776 req->resultSetId = setstring;
1778 req->resultSetStartPoint = &setno;
1779 req->numberOfRecordsRequested = &nos;
1780 prefsyn.proto = protocol;
1781 prefsyn.oclass = CLASS_RECSYN;
1782 prefsyn.value = recordsyntax;
1783 req->preferredRecordSyntax =
1784 odr_oiddup (out, oid_ent_to_oid(&prefsyn, oid));
1786 if (schema != VAL_NONE)
1790 prefschema.proto = protocol;
1791 prefschema.oclass = CLASS_SCHEMA;
1792 prefschema.value = schema;
1794 req->recordComposition = &compo;
1795 compo.which = Z_RecordComp_complex;
1796 compo.u.complex = (Z_CompSpec *)
1797 odr_malloc(out, sizeof(*compo.u.complex));
1798 compo.u.complex->selectAlternativeSyntax = (bool_t *)
1799 odr_malloc(out, sizeof(bool_t));
1800 *compo.u.complex->selectAlternativeSyntax = 0;
1802 compo.u.complex->generic = (Z_Specification *)
1803 odr_malloc(out, sizeof(*compo.u.complex->generic));
1804 compo.u.complex->generic->schema = (Odr_oid *)
1805 odr_oiddup(out, oid_ent_to_oid(&prefschema, oid));
1806 if (!compo.u.complex->generic->schema)
1808 /* OID wasn't a schema! Try record syntax instead. */
1809 prefschema.oclass = CLASS_RECSYN;
1810 compo.u.complex->generic->schema = (Odr_oid *)
1811 odr_oiddup(out, oid_ent_to_oid(&prefschema, oid));
1813 if (!elementSetNames)
1814 compo.u.complex->generic->elementSpec = 0;
1817 compo.u.complex->generic->elementSpec = (Z_ElementSpec *)
1818 odr_malloc(out, sizeof(Z_ElementSpec));
1819 compo.u.complex->generic->elementSpec->which =
1820 Z_ElementSpec_elementSetName;
1821 compo.u.complex->generic->elementSpec->u.elementSetName =
1822 elementSetNames->u.generic;
1824 compo.u.complex->num_dbSpecific = 0;
1825 compo.u.complex->dbSpecific = 0;
1826 compo.u.complex->num_recordSyntax = 0;
1827 compo.u.complex->recordSyntax = 0;
1829 else if (elementSetNames)
1831 req->recordComposition = &compo;
1832 compo.which = Z_RecordComp_simple;
1833 compo.u.simple = elementSetNames;
1836 printf("Sent presentRequest (%d+%d).\n", setno, nos);
1840 static void close_session (void)
1846 nmem_destroy (session_mem);
1852 void process_close(Z_Close *req)
1854 Z_APDU *apdu = zget_APDU(out, Z_APDU_close);
1855 Z_Close *res = apdu->u.close;
1857 static char *reasons[] =
1862 "cost limit reached",
1864 "security violation",
1871 printf("Reason: %s, message: %s\n", reasons[*req->closeReason],
1872 req->diagnosticInformation ? req->diagnosticInformation : "NULL");
1877 *res->closeReason = Z_Close_finished;
1879 printf("Sent response.\n");
1884 static int cmd_show(char *arg)
1888 printf("Not connected yet\n");
1891 if (!send_presentRequest(arg))
1896 int cmd_quit(char *arg)
1898 printf("See you later, alligator.\n");
1904 int cmd_cancel(char *arg)
1906 Z_APDU *apdu = zget_APDU(out, Z_APDU_triggerResourceControlRequest);
1907 Z_TriggerResourceControlRequest *req =
1908 apdu->u.triggerResourceControlRequest;
1913 printf("Session not initialized yet\n");
1916 if (!ODR_MASK_GET(session->options, Z_Options_triggerResourceCtrl))
1918 printf("Target doesn't support cancel (trigger resource ctrl)\n");
1921 *req->requestedAction = Z_TriggerResourceCtrl_cancel;
1922 req->resultSetWanted = &rfalse;
1925 printf("Sent cancel request\n");
1929 int send_scanrequest(const char *query, int pp, int num, const char *term)
1931 Z_APDU *apdu = zget_APDU(out, Z_APDU_scanRequest);
1932 Z_ScanRequest *req = apdu->u.scanRequest;
1935 if (queryType == QueryType_CCL2RPN)
1939 struct ccl_rpn_node *rpn;
1941 rpn = ccl_find_str (bibset, query, &error, &pos);
1944 printf("CCL ERROR: %s\n", ccl_err_msg(error));
1947 bib1.proto = PROTO_Z3950;
1948 bib1.oclass = CLASS_ATTSET;
1949 bib1.value = VAL_BIB1;
1950 req->attributeSet = oid_ent_to_oid (&bib1, oid);
1951 if (!(req->termListAndStartPoint = ccl_scan_query (out, rpn)))
1953 printf("Couldn't convert CCL to Scan term\n");
1956 ccl_rpn_delete (rpn);
1960 YAZ_PQF_Parser pqf_parser = yaz_pqf_create ();
1962 if (!(req->termListAndStartPoint =
1963 yaz_pqf_scan(pqf_parser, out, &req->attributeSet, query)))
1965 const char *pqf_msg;
1967 int code = yaz_pqf_error (pqf_parser, &pqf_msg, &off);
1968 printf("%*s^\n", off+7, "");
1969 printf("Prefix query error: %s (code %d)\n", pqf_msg, code);
1970 yaz_pqf_destroy (pqf_parser);
1973 yaz_pqf_destroy (pqf_parser);
1977 if (req->termListAndStartPoint->term &&
1978 req->termListAndStartPoint->term->which == Z_Term_general &&
1979 req->termListAndStartPoint->term->u.general)
1981 req->termListAndStartPoint->term->u.general->buf =
1982 (unsigned char *) odr_strdup(out, term);
1983 req->termListAndStartPoint->term->u.general->len =
1984 req->termListAndStartPoint->term->u.general->size =
1988 req->referenceId = set_refid (out);
1989 req->num_databaseNames = num_databaseNames;
1990 req->databaseNames = databaseNames;
1991 req->numberOfTermsRequested = #
1992 req->preferredPositionInResponse = &pp;
1997 int send_sortrequest(char *arg, int newset)
1999 Z_APDU *apdu = zget_APDU(out, Z_APDU_sortRequest);
2000 Z_SortRequest *req = apdu->u.sortRequest;
2001 Z_SortKeySpecList *sksl = (Z_SortKeySpecList *)
2002 odr_malloc (out, sizeof(*sksl));
2006 sprintf (setstring, "%d", setnumber);
2008 sprintf (setstring, "default");
2010 req->referenceId = set_refid (out);
2012 req->num_inputResultSetNames = 1;
2013 req->inputResultSetNames = (Z_InternationalString **)
2014 odr_malloc (out, sizeof(*req->inputResultSetNames));
2015 req->inputResultSetNames[0] = odr_strdup (out, setstring);
2017 if (newset && setnumber >= 0)
2018 sprintf (setstring, "%d", ++setnumber);
2020 req->sortedResultSetName = odr_strdup (out, setstring);
2022 req->sortSequence = yaz_sort_spec (out, arg);
2023 if (!req->sortSequence)
2025 printf ("Missing sort specifications\n");
2032 void display_term(Z_TermInfo *t)
2034 if (t->term->which == Z_Term_general)
2036 printf("%.*s", t->term->u.general->len, t->term->u.general->buf);
2037 sprintf(last_scan_line, "%.*s", t->term->u.general->len,
2038 t->term->u.general->buf);
2041 printf("Term (not general)");
2042 if (t->globalOccurrences)
2043 printf (" (%d)\n", *t->globalOccurrences);
2048 void process_scanResponse(Z_ScanResponse *res)
2051 Z_Entry **entries = NULL;
2052 int num_entries = 0;
2054 printf("Received ScanResponse\n");
2055 print_refid (res->referenceId);
2056 printf("%d entries", *res->numberOfEntriesReturned);
2057 if (res->positionOfTerm)
2058 printf (", position=%d", *res->positionOfTerm);
2060 if (*res->scanStatus != Z_Scan_success)
2061 printf("Scan returned code %d\n", *res->scanStatus);
2064 if ((entries = res->entries->entries))
2065 num_entries = res->entries->num_entries;
2066 for (i = 0; i < num_entries; i++)
2068 int pos_term = res->positionOfTerm ? *res->positionOfTerm : -1;
2069 if (entries[i]->which == Z_Entry_termInfo)
2071 printf("%c ", i + 1 == pos_term ? '*' : ' ');
2072 display_term(entries[i]->u.termInfo);
2075 display_diagrecs(&entries[i]->u.surrogateDiagnostic, 1);
2077 if (res->entries->nonsurrogateDiagnostics)
2078 display_diagrecs (res->entries->nonsurrogateDiagnostics,
2079 res->entries->num_nonsurrogateDiagnostics);
2082 void process_sortResponse(Z_SortResponse *res)
2084 printf("Received SortResponse: status=");
2085 switch (*res->sortStatus)
2087 case Z_SortStatus_success:
2088 printf ("success"); break;
2089 case Z_SortStatus_partial_1:
2090 printf ("partial"); break;
2091 case Z_SortStatus_failure:
2092 printf ("failure"); break;
2094 printf ("unknown (%d)", *res->sortStatus);
2097 print_refid (res->referenceId);
2098 if (res->diagnostics)
2099 display_diagrecs(res->diagnostics,
2100 res->num_diagnostics);
2103 void process_deleteResultSetResponse (Z_DeleteResultSetResponse *res)
2105 printf("Got deleteResultSetResponse status=%d\n",
2106 *res->deleteOperationStatus);
2107 if (res->deleteListStatuses)
2110 for (i = 0; i < res->deleteListStatuses->num; i++)
2112 printf ("%s status=%d\n", res->deleteListStatuses->elements[i]->id,
2113 *res->deleteListStatuses->elements[i]->status);
2118 int cmd_sort_generic(char *arg, int newset)
2122 printf("Session not initialized yet\n");
2125 if (!ODR_MASK_GET(session->options, Z_Options_sort))
2127 printf("Target doesn't support sort\n");
2132 if (send_sortrequest(arg, newset) < 0)
2139 int cmd_sort(char *arg)
2141 return cmd_sort_generic (arg, 0);
2144 int cmd_sort_newset (char *arg)
2146 return cmd_sort_generic (arg, 1);
2149 int cmd_scan(char *arg)
2156 printf("Session not initialized yet\n");
2160 if (!ODR_MASK_GET(session->options, Z_Options_scan))
2162 printf("Target doesn't support scan\n");
2167 strcpy (last_scan_query, arg);
2168 if (send_scanrequest(arg, 1, 20, 0) < 0)
2173 if (send_scanrequest(last_scan_query, 1, 20, last_scan_line) < 0)
2179 int cmd_schema(char *arg)
2186 schema = oid_getvalbyname (arg);
2187 if (schema == VAL_NONE)
2189 printf ("unknown schema\n");
2195 int cmd_format(char *arg)
2199 printf("Usage: format <recordsyntax>\n");
2202 recordsyntax = oid_getvalbyname (arg);
2203 if (recordsyntax == VAL_NONE)
2205 printf ("unknown record syntax\n");
2211 int cmd_elements(char *arg)
2213 static Z_ElementSetNames esn;
2214 static char what[100];
2218 elementSetNames = 0;
2222 esn.which = Z_ElementSetNames_generic;
2223 esn.u.generic = what;
2224 elementSetNames = &esn;
2228 int cmd_attributeset(char *arg)
2234 printf("Usage: attributeset <setname>\n");
2237 sscanf(arg, "%s", what);
2238 if (p_query_attset (what))
2240 printf("Unknown attribute set name\n");
2246 int cmd_querytype (char *arg)
2248 if (!strcmp (arg, "ccl"))
2249 queryType = QueryType_CCL;
2250 else if (!strcmp (arg, "prefix") || !strcmp(arg, "rpn"))
2251 queryType = QueryType_Prefix;
2252 else if (!strcmp (arg, "ccl2rpn") || !strcmp (arg, "cclrpn"))
2253 queryType = QueryType_CCL2RPN;
2256 printf ("Querytype must be one of:\n");
2257 printf (" prefix - Prefix query\n");
2258 printf (" ccl - CCL query\n");
2259 printf (" ccl2rpn - CCL query converted to RPN\n");
2265 int cmd_refid (char *arg)
2271 refid = (char *) xmalloc (strlen(arg)+1);
2272 strcpy (refid, arg);
2277 int cmd_close(char *arg)
2284 apdu = zget_APDU(out, Z_APDU_close);
2285 req = apdu->u.close;
2286 *req->closeReason = Z_Close_finished;
2288 printf("Sent close request.\n");
2293 int cmd_packagename(char* arg)
2295 xfree (esPackageName);
2296 esPackageName = NULL;
2299 esPackageName = (char *) xmalloc (strlen(arg)+1);
2300 strcpy (esPackageName, arg);
2305 int cmd_proxy(char* arg)
2316 yazProxy = (char *) xmalloc (strlen(arg)+1);
2317 strcpy (yazProxy, arg);
2322 int cmd_charset(char* arg)
2324 char l1[30], l2[30];
2327 if (sscanf(arg, "%29s %29s", l1, l2) < 1)
2329 printf("Current character set is `%s'\n", (yazCharset) ? yazCharset:NULL);
2335 yazCharset = xstrdup(l1);
2338 odr_set_charset (out, l1, l2);
2339 odr_set_charset (in, l2, l1);
2344 int cmd_lang(char* arg)
2347 printf("Current language is `%s'\n", (yazLang)?yazLang:NULL);
2354 yazLang = (char *) xmalloc (strlen(arg)+1);
2355 strcpy (yazLang, arg);
2360 int cmd_source(char* arg)
2362 /* first should open the file and read one line at a time.. */
2364 char line[1024], *cp;
2367 char* args_end=(arg)+strlen(arg)-1;
2368 while(isspace(*args_end))
2373 REMOVE_TAILING_BLANKS(arg);
2376 fprintf(stderr,"Error in source command use a filename\n");
2380 includeFile = fopen (arg, "r");
2383 fprintf(stderr,"Unable to open file %s for reading\n",arg);
2387 while(!feof(includeFile)) {
2388 memset(line,0,sizeof(line));
2389 fgets(line,sizeof(line),includeFile);
2391 if(strlen(line) < 2) continue;
2392 if(line[0] == '#') continue;
2394 if ((cp = strrchr (line, '\n')))
2397 process_cmd_line(line);
2400 if(fclose(includeFile)<0) {
2401 perror("unable to close include file");
2407 int cmd_subshell(char* args)
2412 system(getenv("SHELL"));
2418 int cmd_set_apdufile(char* arg)
2420 REMOVE_TAILING_BLANKS(arg);
2422 if(apdu_file && apdu_file != stderr) { /* don't close stdout*/
2423 perror("unable to close apdu log file");
2431 if(!strcmp(arg,"-"))
2434 apdu_file=fopen(arg, "a");
2437 perror("unable to open apdu log file no apdu log loaded");
2439 odr_setprint(print, apdu_file);
2445 int cmd_set_cclfile(char* arg)
2449 REMOVE_TAILING_BLANKS(arg);
2451 bibset = ccl_qual_mk ();
2452 inf = fopen (arg, "r");
2455 ccl_qual_file (bibset, inf);
2458 strcpy(ccl_fields,arg);
2463 int cmd_set_auto_reconnect(char* arg)
2465 REMOVE_TAILING_BLANKS(arg);
2467 if(strlen(arg)==0) {
2468 auto_reconnect = ! auto_reconnect;
2469 } else if(strcmp(arg,"on")==0) {
2471 } else if(strcmp(arg,"off")==0) {
2474 printf("Error use on or off\n");
2479 printf("Set auto reconnect enabled.\n");
2481 printf("Set auto reconnect disabled.\n");
2486 int cmd_set_marcdump(char* arg)
2488 if(marcdump && marcdump != stderr) { /* don't close stdout*/
2489 perror("unable to close apdu log file");
2497 if(!strcmp(arg,"-"))
2500 marcdump=fopen(arg, "a");
2503 perror("unable to open apdu marcdump file no marcdump done\n");
2509 int cmd_set_proxy(char* arg)
2511 if(yazProxy) free(yazProxy);
2514 if(strlen(arg) > 1) {
2515 yazProxy=strdup(arg);
2521 this command takes 3 arge {name class oid}
2523 int cmd_register_oid(char* args) {
2528 {"appctx",CLASS_APPCTX},
2529 {"absyn",CLASS_ABSYN},
2530 {"attset",CLASS_ATTSET},
2531 {"transyn",CLASS_TRANSYN},
2532 {"diagset",CLASS_DIAGSET},
2533 {"recsyn",CLASS_RECSYN},
2534 {"resform",CLASS_RESFORM},
2535 {"accform",CLASS_ACCFORM},
2536 {"extserv",CLASS_EXTSERV},
2537 {"userinfo",CLASS_USERINFO},
2538 {"elemspec",CLASS_ELEMSPEC},
2539 {"varset",CLASS_VARSET},
2540 {"schema",CLASS_SCHEMA},
2541 {"tagset",CLASS_TAGSET},
2542 {"general",CLASS_GENERAL},
2543 {0,(enum oid_class) 0}
2545 char oname_str[101], oclass_str[101], oid_str[101];
2548 oid_class oidclass = CLASS_GENERAL;
2549 int val = 0, oid[OID_SIZE];
2550 struct oident * new_oident=NULL;
2552 if (sscanf (args, "%100[^ ] %100[^ ] %100s",
2553 oname_str,oclass_str, oid_str) < 1) {
2554 printf("Error in regristrate command \n");
2558 for (i = 0; oid_classes[i].className; i++) {
2559 if (!strcmp(oid_classes[i].className, oclass_str))
2561 oidclass=oid_classes[i].oclass;
2566 if(!(oid_classes[i].className)) {
2567 printf("Unknonwn oid class %s\n",oclass_str);
2575 while (isdigit (*name))
2577 val = val*10 + (*name - '0');
2590 new_oident=oid_addent (oid,PROTO_GENERAL,oidclass,oname_str,VAL_DYNAMIC);
2591 if(strcmp(new_oident->desc,oname_str)) {
2592 fprintf(stderr,"oid is already named as %s, regristration faild\n",
2598 int cmd_push_command(char* arg)
2600 #if HAVE_READLINE_HISTORY_H
2604 fprintf(stderr,"Not compiled with the readline/history module\n");
2609 void source_rcfile()
2611 /* Look for a $HOME/.yazclientrc and source it if it exists */
2612 struct stat statbuf;
2614 char* homedir=getenv("HOME");
2616 if(!homedir) return;
2618 sprintf(buffer,"%s/.yazclientrc",homedir);
2620 if(stat(buffer,&statbuf)==0) {
2624 if(stat(".yazclientrc",&statbuf)==0) {
2625 cmd_source(".yazclientrc");
2630 static void initialize(void)
2635 if (!(out = odr_createmem(ODR_ENCODE)) ||
2636 !(in = odr_createmem(ODR_DECODE)) ||
2637 !(print = odr_createmem(ODR_PRINT)))
2639 fprintf(stderr, "failed to allocate ODR streams\n");
2644 setvbuf(stdout, 0, _IONBF, 0);
2646 odr_setprint(print, apdu_file);
2648 bibset = ccl_qual_mk ();
2649 inf = fopen (ccl_fields, "r");
2652 ccl_qual_file (bibset, inf);
2655 cmd_base("Default");
2657 #if HAVE_READLINE_READLINE_H
2658 rl_attempted_completion_function = (CPPFunction*)readline_completer;
2662 for(i=0; i<maxOtherInfosSupported; ++i) {
2663 extraOtherInfos[i].oidval = -1;
2670 #if HAVE_GETTIMEOFDAY
2671 struct timeval tv_start, tv_end;
2674 void wait_and_handle_responce()
2679 int netbufferlen = 0;
2687 if ((res = cs_get(conn, &netbuffer, &netbufferlen)) < 0)
2689 printf("Target closed connection\n");
2695 printf("Target closed connection.\n");
2699 odr_reset(in); /* release APDU from last round */
2701 odr_setbuf(in, netbuffer, res, 0);
2702 if (!z_APDU(in, &apdu, 0, 0))
2704 odr_perror(in, "Decoding incoming APDU");
2705 fprintf(stderr, "[Near %d]\n", odr_offset(in));
2706 fprintf(stderr, "Packet dump:\n---------\n");
2707 odr_dumpBER(stderr, netbuffer, res);
2708 fprintf(stderr, "---------\n");
2710 z_APDU(print, &apdu, 0, 0);
2714 if (apdu_file && !z_APDU(print, &apdu, 0, 0))
2716 odr_perror(print, "Failed to print incoming APDU");
2722 case Z_APDU_initResponse:
2723 process_initResponse(apdu->u.initResponse);
2725 case Z_APDU_searchResponse:
2726 process_searchResponse(apdu->u.searchResponse);
2728 case Z_APDU_scanResponse:
2729 process_scanResponse(apdu->u.scanResponse);
2731 case Z_APDU_presentResponse:
2732 print_refid (apdu->u.presentResponse->referenceId);
2734 *apdu->u.presentResponse->numberOfRecordsReturned;
2735 if (apdu->u.presentResponse->records)
2736 display_records(apdu->u.presentResponse->records);
2738 printf("No records.\n");
2739 printf ("nextResultSetPosition = %d\n",
2740 *apdu->u.presentResponse->nextResultSetPosition);
2742 case Z_APDU_sortResponse:
2743 process_sortResponse(apdu->u.sortResponse);
2745 case Z_APDU_extendedServicesResponse:
2746 printf("Got extended services response\n");
2747 process_ESResponse(apdu->u.extendedServicesResponse);
2750 printf("Target has closed the association.\n");
2751 process_close(apdu->u.close);
2753 case Z_APDU_resourceControlRequest:
2754 process_resourceControlRequest
2755 (apdu->u.resourceControlRequest);
2757 case Z_APDU_deleteResultSetResponse:
2758 process_deleteResultSetResponse(apdu->u.
2759 deleteResultSetResponse);
2762 printf("Received unknown APDU type (%d).\n",
2767 while (conn && cs_more(conn));
2768 #if HAVE_GETTIMEOFDAY
2769 gettimeofday (&tv_end, 0);
2771 printf ("S/U S/U=%ld/%ld %ld/%ld",
2772 (long) tv_start.tv_sec,
2773 (long) tv_start.tv_usec,
2774 (long) tv_end.tv_sec,
2775 (long) tv_end.tv_usec);
2777 printf ("Elapsed: %.6f\n",
2778 (double) tv_end.tv_usec / 1e6 + tv_end.tv_sec -
2779 ((double) tv_start.tv_usec / 1e6 + tv_start.tv_sec));
2786 int cmd_cclparse(char* arg)
2789 struct ccl_rpn_node *rpn=NULL;
2792 rpn = ccl_find_str (bibset, arg, &error, &pos);
2795 printf ("%*s^ - ", 3+strlen(last_cmd)+1+pos, " ");
2796 printf ("%s\n", ccl_err_msg (error));
2802 ccl_pr_tree(rpn, stdout);
2806 ccl_rpn_delete(rpn);
2814 int cmd_set_otherinfo(char* args)
2816 char oid[101], otherinfoString[101];
2821 sscan_res = sscanf (args, "%d %100[^ ] %100s", &otherinfoNo, oid, otherinfoString);
2823 /* reset this otherinfo */
2824 if(otherinfoNo>=maxOtherInfosSupported) {
2825 printf("Error otherinfo index to large (%d>%d)\n",otherinfoNo,maxOtherInfosSupported);
2827 extraOtherInfos[otherinfoNo].oidval = -1;
2828 if(extraOtherInfos[otherinfoNo].value) free(extraOtherInfos[otherinfoNo].value);
2832 printf("Error in set_otherinfo command \n");
2836 if(otherinfoNo>=maxOtherInfosSupported) {
2837 printf("Error otherinfo index to large (%d>%d)\n",otherinfoNo,maxOtherInfosSupported);
2841 oidval = oid_getvalbyname (oid);
2843 printf("Error in set_otherinfo command unknown oid %s \n",oid);
2846 extraOtherInfos[otherinfoNo].oidval = oidval;
2847 if(extraOtherInfos[otherinfoNo].value) free(extraOtherInfos[otherinfoNo].value);
2848 extraOtherInfos[otherinfoNo].value = strdup(otherinfoString);
2853 int cmd_list_otherinfo(char* args)
2857 if(strlen(args)>0) {
2859 if( i >= maxOtherInfosSupported ) {
2860 printf("Error otherinfo index to large (%d>%d)\n",i,maxOtherInfosSupported);
2864 if(extraOtherInfos[i].oidval != -1)
2865 printf(" otherinfo %d %s %s\n",
2867 yaz_z3950_oid_value_to_str(
2868 (enum oid_value) extraOtherInfos[i].oidval,
2870 extraOtherInfos[i].value);
2873 for(i=0; i<maxOtherInfosSupported; ++i) {
2874 if(extraOtherInfos[i].oidval != -1)
2875 printf(" otherinfo %d %s %s\n",
2877 yaz_z3950_oid_value_to_str(
2878 (enum oid_value) extraOtherInfos[i].oidval,
2880 extraOtherInfos[i].value);
2888 int cmd_list_all(char* args) {
2891 /* connection options */
2893 printf("Connected to : %s\n",last_open_command);
2895 if(last_open_command)
2896 printf("Not connected to : %s\n",last_open_command);
2898 printf("Not connected : \n");
2901 if(yazProxy) printf("using proxy : %s\n",yazProxy);
2903 printf("auto_reconnect : %s\n",auto_reconnect?"on":"off");
2906 printf("Authentication : none\n");
2908 switch(auth->which) {
2909 case Z_IdAuthentication_idPass:
2910 printf("Authentication : IdPass\n");
2911 printf(" Login User : %s\n",auth->u.idPass->userId?auth->u.idPass->userId:"");
2912 printf(" Login Group : %s\n",auth->u.idPass->groupId?auth->u.idPass->groupId:"");
2913 printf(" Password : %s\n",auth->u.idPass->password?auth->u.idPass->password:"");
2915 case Z_IdAuthentication_open:
2916 printf("Authentication : psOpen\n");
2917 printf(" Open string : %s\n",auth->u.open);
2920 printf("Authentication : Unknown\n");
2923 if ( yazCharset ) printf("Character set : `%s'\n", (yazCharset) ? yazCharset:NULL);
2927 for (i = 0; i<num_databaseNames; i++) printf("%s ",databaseNames[i]);
2931 printf("CCL file : %s\n",ccl_fields);
2932 printf("Query type : %s\n",query_type_as_string(queryType));
2934 printf("Named Result Sets : %s\n",setnumber==-1?"off":"on");
2936 /* piggy back options */
2937 printf("ssub/lslb/mspn : %d/%d/%d\n",smallSetUpperBound,largeSetLowerBound,mediumSetPresentNumber);
2939 /* print present related options */
2940 printf("Format : %s\n",yaz_z3950_oid_value_to_str(recordsyntax,CLASS_RECSYN));
2941 printf("Schema : %s\n",yaz_z3950_oid_value_to_str(schema,CLASS_SCHEMA));
2942 printf("Elements : %s\n",elementSetNames?elementSetNames->u.generic:"");
2944 /* loging options */
2945 printf("APDU log : %s\n",apdu_file?"on":"off");
2946 printf("Record log : %s\n",marcdump?"on":"off");
2949 printf("Other Info: \n");
2950 cmd_list_otherinfo("");
2955 int cmd_clear_otherinfo(char* args)
2957 if(strlen(args)>0) {
2959 otherinfoNo = atoi(args);
2960 if( otherinfoNo >= maxOtherInfosSupported ) {
2961 printf("Error otherinfo index to large (%d>%d)\n",otherinfoNo,maxOtherInfosSupported);
2965 if(extraOtherInfos[otherinfoNo].oidval != -1) {
2966 /* only clear if set. */
2967 extraOtherInfos[otherinfoNo].oidval=-1;
2968 free(extraOtherInfos[otherinfoNo].value);
2973 for(i=0; i<maxOtherInfosSupported; ++i) {
2974 if (extraOtherInfos[i].oidval!=-1 ) {
2975 extraOtherInfos[i].oidval=-1;
2976 free(extraOtherInfos[i].value);
2983 static int cmd_help (char *line);
2985 typedef char *(*completerFunctionType)(const char *text, int state);
2989 int (*fun)(char *arg);
2991 completerFunctionType rl_completerfunction;
2992 int complete_filenames;
2993 char **local_tabcompletes;
2995 {"open", cmd_open, "('tcp'|'ssl')':<host>[':'<port>][/<db>]",NULL,0,NULL},
2996 {"quit", cmd_quit, "",NULL,0,NULL},
2997 {"find", cmd_find, "<query>",NULL,0,NULL},
2998 {"delete", cmd_delete, "<setname>",NULL,0,NULL},
2999 {"base", cmd_base, "<base-name>",NULL,0,NULL},
3000 {"show", cmd_show, "<rec#>['+'<#recs>['+'<setname>]]",NULL,0,NULL},
3001 {"scan", cmd_scan, "<term>",NULL,0,NULL},
3002 {"sort", cmd_sort, "<sortkey> <flag> <sortkey> <flag> ...",NULL,0,NULL},
3003 {"sort+", cmd_sort_newset, "<sortkey> <flag> <sortkey> <flag> ...",NULL,0,NULL},
3004 {"authentication", cmd_authentication, "<acctstring>",NULL,0,NULL},
3005 {"lslb", cmd_lslb, "<largeSetLowerBound>",NULL,0,NULL},
3006 {"ssub", cmd_ssub, "<smallSetUpperBound>",NULL,0,NULL},
3007 {"mspn", cmd_mspn, "<mediumSetPresentNumber>",NULL,0,NULL},
3008 {"status", cmd_status, "",NULL,0,NULL},
3009 {"setnames", cmd_setnames, "",NULL,0,NULL},
3010 {"cancel", cmd_cancel, "",NULL,0,NULL},
3011 {"format", cmd_format, "<recordsyntax>",complete_format,0,NULL},
3012 {"schema", cmd_schema, "<schema>",complete_schema,0,NULL},
3013 {"elements", cmd_elements, "<elementSetName>",NULL,0,NULL},
3014 {"close", cmd_close, "",NULL,0,NULL},
3015 {"attributeset", cmd_attributeset, "<attrset>",complete_attributeset,0,NULL},
3016 {"querytype", cmd_querytype, "<type>",complete_querytype,0,NULL},
3017 {"refid", cmd_refid, "<id>",NULL,0,NULL},
3018 {"itemorder", cmd_itemorder, "ill|item <itemno>",NULL,0,NULL},
3019 {"update", cmd_update, "<action> <recid> [<file>]",NULL,0,NULL},
3020 {"packagename", cmd_packagename, "<packagename>",NULL,0,NULL},
3021 {"proxy", cmd_proxy, "[('tcp'|'ssl')]<host>[':'<port>]",NULL,0,NULL},
3022 {"charset", cmd_charset, "<charset_name>",NULL,0,NULL},
3023 {"lang", cmd_lang, "<language_code>",NULL,0,NULL},
3024 {".", cmd_source, "<filename>",NULL,1,NULL},
3025 {"!", cmd_subshell, "Subshell command",NULL,1,NULL},
3026 {"set_apdufile", cmd_set_apdufile, "<filename>",NULL,1,NULL},
3027 {"set_marcdump", cmd_set_marcdump," <filename>",NULL,1,NULL},
3028 {"set_cclfile", cmd_set_cclfile," <filename>",NULL,1,NULL},
3029 {"set_auto_reconnect", cmd_set_auto_reconnect," on|off",complete_auto_reconnect,1,NULL},
3030 {"set_otherinfo", cmd_set_otherinfo,"<otherinfoinddex> <oid> <string>",NULL,0,NULL},
3031 {"register_oid", cmd_register_oid,"<name> <class> <oid>",NULL,0,NULL},
3032 {"push_command", cmd_push_command,"<command>",command_generator,0,NULL},
3033 {"register_tab", cmd_register_tab,"<commandname> <tab>",command_generator,0,NULL},
3034 {"cclparse", cmd_cclparse,"<ccl find command>",NULL,0,NULL},
3035 {"list_otherinfo",cmd_list_otherinfo,"[otherinfoinddex]",NULL,0,NULL},
3036 {"list_all",cmd_list_all,"",NULL,0,NULL},
3037 {"clear_otherinfo",cmd_clear_otherinfo,"",NULL,0,NULL},
3038 /* Server Admin Functions */
3039 {"adm-reindex", cmd_adm_reindex, "<database-name>",NULL,0,NULL},
3040 {"adm-truncate", cmd_adm_truncate, "('database'|'index')<object-name>",NULL,0,NULL},
3041 {"adm-create", cmd_adm_create, "",NULL,0,NULL},
3042 {"adm-drop", cmd_adm_drop, "('database'|'index')<object-name>",NULL,0,NULL},
3043 {"adm-import", cmd_adm_import, "<record-type> <dir> <pattern>",NULL,0,NULL},
3044 {"adm-refresh", cmd_adm_refresh, "",NULL,0,NULL},
3045 {"adm-commit", cmd_adm_commit, "",NULL,0,NULL},
3046 {"adm-shutdown", cmd_adm_shutdown, "",NULL,0,NULL},
3047 {"adm-startup", cmd_adm_startup, "",NULL,0,NULL},
3048 {"help", cmd_help, "", NULL,0,NULL},
3052 static int cmd_help (char *line)
3058 sscanf (line, "%20s", topic);
3061 printf("Commands:\n");
3062 for (i = 0; cmd[i].cmd; i++)
3063 if (*topic == 0 || strcmp (topic, cmd[i].cmd) == 0)
3064 printf(" %s %s\n", cmd[i].cmd, cmd[i].ad);
3065 if (strcmp (topic, "find") == 0)
3068 printf (" \"term\" Simple Term\n");
3069 printf (" @attr [attset] type=value op Attribute\n");
3070 printf (" @and opl opr And\n");
3071 printf (" @or opl opr Or\n");
3072 printf (" @not opl opr And-Not\n");
3073 printf (" @set set Result set\n");
3075 printf ("Bib-1 attribute types\n");
3077 printf ("4=Title 7=ISBN 8=ISSN 30=Date 62=Abstract 1003=Author 1016=Any\n");
3078 printf ("2=Relation: ");
3079 printf ("1< 2<= 3= 4>= 5> 6!= 102=Relevance\n");
3080 printf ("3=Position: ");
3081 printf ("1=First in Field 2=First in subfield 3=Any position\n");
3082 printf ("4=Structure: ");
3083 printf ("1=Phrase 2=Word 3=Key 4=Year 5=Date 6=WordList\n");
3084 printf ("5=Truncation: ");
3085 printf ("1=Right 2=Left 3=L&R 100=No 101=# 102=Re-1 103=Re-2\n");
3086 printf ("6=Completeness:");
3087 printf ("1=Incomplete subfield 2=Complete subfield 3=Complete field\n");
3092 int cmd_register_tab(char* arg) {
3094 char command[101], tabargument[101];
3099 if (sscanf (arg, "%100s %100s", command, tabargument) < 1) {
3103 /* locate the amdn in the list */
3104 for (i = 0; cmd[i].cmd; i++) {
3105 if (!strncmp(cmd[i].cmd, command, strlen(command))) {
3111 fprintf(stderr,"Unknown command %s\n",command);
3116 if(!cmd[i].local_tabcompletes)
3117 cmd[i].local_tabcompletes = (char **) calloc(1,sizeof(char**));
3121 tabslist = cmd[i].local_tabcompletes;
3122 for(;tabslist && *tabslist;tabslist++) {
3126 cmd[i].local_tabcompletes = (char **)
3127 realloc(cmd[i].local_tabcompletes,(num_of_tabs+2)*sizeof(char**));
3128 tabslist=cmd[i].local_tabcompletes;
3129 tabslist[num_of_tabs]=strdup(tabargument);
3130 tabslist[num_of_tabs+1]=NULL;
3135 void process_cmd_line(char* line)
3138 char word[32], arg[1024];
3140 #if HAVE_GETTIMEOFDAY
3141 gettimeofday (&tv_start, 0);
3144 if ((res = sscanf(line, "%31s %1023[^;]", word, arg)) <= 0)
3146 strcpy(word, last_cmd);
3151 strcpy(last_cmd, word);
3153 /* removed tailing spaces from the arg command */
3156 char* lastnonspace=NULL;
3165 *(++lastnonspace) = 0;
3169 for (i = 0; cmd[i].cmd; i++)
3170 if (!strncmp(cmd[i].cmd, word, strlen(word)))
3172 res = (*cmd[i].fun)(arg);
3176 if (!cmd[i].cmd) /* dump our help-screen */
3178 printf("Unknown command: %s.\n", word);
3179 printf("use help for list of commands\n");
3180 /* cmd_help (""); */
3184 if(apdu_file) fflush(apdu_file);
3187 wait_and_handle_responce();
3196 char *command_generator(const char *text, int state)
3202 for( ; cmd[idx].cmd; ++idx) {
3203 if (!strncmp(cmd[idx].cmd,text,strlen(text))) {
3204 ++idx; /* skip this entry on the next run */
3205 return strdup(cmd[idx-1].cmd);
3213 This function only known how to complete on the first word
3215 char ** readline_completer(char *text, int start, int end) {
3216 #if HAVE_READLINE_READLINE_H
3218 completerFunctionType completerToUse;
3221 #if HAVE_READLINE_RL_COMPLETION_MATCHES
3222 char** res=rl_completion_matches(text,
3225 char** res=completion_matches(text,
3226 (CPFunction*)command_generator);
3228 rl_attempted_completion_over = 1;
3231 char arg[1024],word[32];
3233 if ((res = sscanf(rl_line_buffer, "%31s %1023[^;]", word, arg)) <= 0) {
3234 rl_attempted_completion_over = 1;
3238 for (i = 0; cmd[i].cmd; i++) {
3239 if (!strncmp(cmd[i].cmd, word, strlen(word))) {
3244 if(!cmd[i].cmd) return NULL;
3247 curret_global_list = cmd[i].local_tabcompletes;
3249 completerToUse = cmd[i].rl_completerfunction;
3250 if(completerToUse==NULL) /* if no pr. command completer is defined use the default completer */
3251 completerToUse = default_completer;
3253 if(completerToUse) {
3254 #ifdef HAVE_READLINE_RL_COMPLETION_MATCHES
3256 rl_completion_matches(text,
3260 completion_matches(text,
3261 (CPFunction*)completerToUse);
3263 if(!cmd[i].complete_filenames)
3264 rl_attempted_completion_over = 1;
3267 if(!cmd[i].complete_filenames)
3268 rl_attempted_completion_over = 1;
3278 static void client(void)
3284 #if HAVE_GETTIMEOFDAY
3285 gettimeofday (&tv_start, 0);
3290 char *line_in = NULL;
3291 #if HAVE_READLINE_READLINE_H
3294 line_in=readline(C_PROMPT);
3297 #if HAVE_READLINE_HISTORY_H
3299 add_history(line_in);
3301 strncpy(line, line_in, 1023);
3310 if (!fgets(line, 1023, stdin))
3312 if ((end_p = strchr (line, '\n')))
3315 process_cmd_line(line);
3319 int main(int argc, char **argv)
3322 char *open_command = 0;
3323 char *auth_command = 0;
3327 while ((ret = options("k:c:a:m:v:p:u:", argv, argc, &arg)) != -2)
3334 open_command = (char *) xmalloc (strlen(arg)+6);
3335 strcpy (open_command, "open ");
3336 strcat (open_command, arg);
3340 kilobytes = atoi(arg);
3343 if (!(marcdump = fopen (arg, "a")))
3350 strncpy (ccl_fields, arg, sizeof(ccl_fields)-1);
3351 ccl_fields[sizeof(ccl_fields)-1] = '\0';
3354 if (!strcmp(arg, "-"))
3357 apdu_file=fopen(arg, "a");
3360 yazProxy=strdup(arg);
3365 auth_command = (char *) xmalloc (strlen(arg)+6);
3366 strcpy (auth_command, "auth ");
3367 strcat (auth_command, arg);
3371 yaz_log_init (yaz_log_mask_str(arg), "", NULL);
3374 fprintf (stderr, "Usage: %s [-m <marclog>] [ -a <apdulog>] "
3375 "[-c cclfields]\n [-p <proxy-addr>] [-u <auth>] "
3376 "[-k size] [<server-addr>]\n",
3384 #ifdef HAVE_GETTIMEOFDAY
3385 gettimeofday (&tv_start, 0);
3387 process_cmd_line (auth_command);
3388 #if HAVE_READLINE_HISTORY_H
3389 add_history(auth_command);
3391 xfree(auth_command);
3395 #ifdef HAVE_GETTIMEOFDAY
3396 gettimeofday (&tv_start, 0);
3398 process_cmd_line (open_command);
3399 #if HAVE_READLINE_HISTORY_H
3400 add_history(open_command);
3402 xfree(open_command);
3413 * vim600: sw=4 ts=8 fdm=marker
3414 * vim<600: sw=4 ts=8