+ *l1 = *l2 = *l3 = 0;
+ if (sscanf(arg, "%29s %29s %29s", l1, l2, l3) < 1)
+ {
+ cmd_negcharset("");
+ cmd_displaycharset("");
+ cmd_marccharset("");
+ }
+ else
+ {
+ cmd_negcharset(l1);
+ if (*l2)
+ cmd_displaycharset(l2);
+ if (*l3)
+ cmd_marccharset(l3);
+ }
+ return 1;
+}
+
+int cmd_lang(const char* arg)
+{
+ if (*arg == '\0') {
+ printf("Current language is `%s'\n", (yazLang)?yazLang:NULL);
+ return 1;
+ }
+ xfree (yazLang);
+ yazLang = NULL;
+ if (*arg)
+ yazLang = xstrdup(arg);
+ return 1;
+}
+
+int cmd_source(const char* arg, int echo )
+{
+ /* first should open the file and read one line at a time.. */
+ FILE* includeFile;
+ char line[102400], *cp;
+
+ if(strlen(arg)<1) {
+ fprintf(stderr,"Error in source command use a filename\n");
+ return -1;
+ }
+
+ includeFile = fopen (arg, "r");
+
+ if(!includeFile) {
+ fprintf(stderr,"Unable to open file %s for reading\n",arg);
+ return -1;
+ }
+
+ while(!feof(includeFile)) {
+ memset(line,0,sizeof(line));
+ fgets(line,sizeof(line),includeFile);
+
+ if(strlen(line) < 2) continue;
+ if(line[0] == '#') continue;
+
+ if ((cp = strrchr (line, '\n')))
+ *cp = '\0';
+
+ if( echo ) {
+ printf( "processing line: %s\n",line );
+ };
+ process_cmd_line(line);
+ }
+
+ if(fclose(includeFile)<0) {
+ perror("unable to close include file");
+ exit(1);
+ }
+ return 1;
+}
+
+int cmd_source_echo(const char* arg)
+{
+ cmd_source(arg, 1);
+ return 1;
+}
+
+int cmd_source_noecho(const char* arg)
+{
+ cmd_source(arg, 0);
+ return 1;
+}
+
+
+int cmd_subshell(const char* args)
+{
+ if(strlen(args))
+ system(args);
+ else
+ system(getenv("SHELL"));
+
+ printf("\n");
+ return 1;
+}
+
+int cmd_set_berfile(const char *arg)
+{
+ if (ber_file && ber_file != stdout && ber_file != stderr)
+ fclose(ber_file);
+ if (!strcmp(arg, ""))
+ ber_file = 0;
+ else if (!strcmp(arg, "-"))
+ ber_file = stdout;
+ else
+ ber_file = fopen(arg, "a");
+ return 1;
+}
+
+int cmd_set_apdufile(const char *arg)
+{
+ if(apdu_file && apdu_file != stderr && apdu_file != stderr)
+ fclose(apdu_file);
+ if (!strcmp(arg, ""))
+ apdu_file = 0;
+ else if (!strcmp(arg, "-"))
+ apdu_file = stderr;
+ else
+ {
+ apdu_file = fopen(arg, "a");
+ if (!apdu_file)
+ perror("unable to open apdu log file");
+ }
+ if (apdu_file)
+ odr_setprint(print, apdu_file);
+ return 1;
+}
+
+int cmd_set_cclfile(const char* arg)
+{
+ FILE *inf;
+
+ bibset = ccl_qual_mk ();
+ inf = fopen (arg, "r");
+ if (!inf)
+ perror("unable to open CCL file");
+ else
+ {
+ ccl_qual_file (bibset, inf);
+ fclose (inf);
+ }
+ strcpy(ccl_fields,arg);
+ return 0;
+}
+
+int cmd_set_cqlfile(const char* arg)
+{
+ cql_transform_t newcqltrans;
+
+ if ((newcqltrans = cql_transform_open_fname(arg)) == 0) {
+ perror("unable to open CQL file");
+ return 0;
+ }
+ if (cqltrans != 0)
+ cql_transform_close(cqltrans);
+
+ cqltrans = newcqltrans;
+ strcpy(cql_fields, arg);
+ return 0;
+}
+
+int cmd_set_auto_reconnect(const char* arg)
+{
+ if(strlen(arg)==0) {
+ auto_reconnect = ! auto_reconnect;
+ } else if(strcmp(arg,"on")==0) {
+ auto_reconnect = 1;
+ } else if(strcmp(arg,"off")==0) {
+ auto_reconnect = 0;
+ } else {
+ printf("Error use on or off\n");
+ return 1;
+ }
+
+ if (auto_reconnect)
+ printf("Set auto reconnect enabled.\n");
+ else
+ printf("Set auto reconnect disabled.\n");
+
+ return 0;
+}
+
+int cmd_set_marcdump(const char* arg)
+{
+ if(marc_file && marc_file != stderr) { /* don't close stdout*/
+ fclose(marc_file);
+ }
+
+ if (!strcmp(arg, ""))
+ marc_file = 0;
+ else if (!strcmp(arg, "-"))
+ marc_file = stderr;
+ else
+ {
+ marc_file = fopen(arg, "a");
+ if (!marc_file)
+ perror("unable to open marc log file");
+ }
+ return 1;
+}
+
+/*
+ this command takes 3 arge {name class oid}
+*/
+int cmd_register_oid(const char* args) {
+ static struct {
+ char* className;
+ oid_class oclass;
+ } oid_classes[] = {
+ {"appctx",CLASS_APPCTX},
+ {"absyn",CLASS_ABSYN},
+ {"attset",CLASS_ATTSET},
+ {"transyn",CLASS_TRANSYN},
+ {"diagset",CLASS_DIAGSET},
+ {"recsyn",CLASS_RECSYN},
+ {"resform",CLASS_RESFORM},
+ {"accform",CLASS_ACCFORM},
+ {"extserv",CLASS_EXTSERV},
+ {"userinfo",CLASS_USERINFO},
+ {"elemspec",CLASS_ELEMSPEC},
+ {"varset",CLASS_VARSET},
+ {"schema",CLASS_SCHEMA},
+ {"tagset",CLASS_TAGSET},
+ {"general",CLASS_GENERAL},
+ {0,(enum oid_class) 0}
+ };
+ char oname_str[101], oclass_str[101], oid_str[101];
+ char* name;
+ int i;
+ oid_class oidclass = CLASS_GENERAL;
+ int val = 0, oid[OID_SIZE];
+ struct oident * new_oident=NULL;
+
+ if (sscanf (args, "%100[^ ] %100[^ ] %100s",
+ oname_str,oclass_str, oid_str) < 1) {
+ printf("Error in register command \n");
+ return 0;
+ }
+
+ for (i = 0; oid_classes[i].className; i++) {
+ if (!strcmp(oid_classes[i].className, oclass_str))
+ {
+ oidclass=oid_classes[i].oclass;
+ break;
+ }
+ }
+
+ if(!(oid_classes[i].className)) {
+ printf("Unknown oid class %s\n",oclass_str);
+ return 0;
+ }
+
+ i = 0;
+ name = oid_str;
+ val = 0;
+
+ while (isdigit (*(unsigned char *) name))
+ {
+ val = val*10 + (*name - '0');
+ name++;
+ if (*name == '.')
+ {
+ if (i < OID_SIZE-1)
+ oid[i++] = val;
+ val = 0;
+ name++;
+ }
+ }
+ oid[i] = val;
+ oid[i+1] = -1;
+
+ new_oident = oid_addent (oid, PROTO_GENERAL, oidclass, oname_str,
+ VAL_DYNAMIC);
+ if(strcmp(new_oident->desc,oname_str))
+ {
+ fprintf(stderr,"oid is already named as %s, registration failed\n",
+ new_oident->desc);
+ }
+ return 1;
+}
+
+int cmd_push_command(const char* arg)
+{
+#if HAVE_READLINE_HISTORY_H
+ if(strlen(arg)>1)
+ add_history(arg);
+#else
+ fprintf(stderr,"Not compiled with the readline/history module\n");
+#endif
+ return 1;
+}
+
+void source_rcfile()
+{
+ /* Look for a $HOME/.yazclientrc and source it if it exists */
+ struct stat statbuf;
+ char buffer[1000];
+ char* homedir=getenv("HOME");
+
+ if( homedir ) {
+
+ sprintf(buffer,"%s/.yazclientrc",homedir);
+
+ if(stat(buffer,&statbuf)==0) {
+ cmd_source(buffer, 0 );
+ }
+
+ };
+
+ if(stat(".yazclientrc",&statbuf)==0) {
+ cmd_source(".yazclientrc", 0 );
+ }
+}
+
+
+static void initialize(void)
+{
+ FILE *inf;
+ int i;
+
+ if (!(out = odr_createmem(ODR_ENCODE)) ||
+ !(in = odr_createmem(ODR_DECODE)) ||
+ !(print = odr_createmem(ODR_PRINT)))
+ {
+ fprintf(stderr, "failed to allocate ODR streams\n");
+ exit(1);
+ }
+ oid_init();
+
+ setvbuf(stdout, 0, _IONBF, 0);
+ if (apdu_file)
+ odr_setprint(print, apdu_file);
+
+ bibset = ccl_qual_mk ();
+ inf = fopen (ccl_fields, "r");
+ if (inf)
+ {
+ ccl_qual_file (bibset, inf);
+ fclose (inf);
+ }
+
+ cqltrans = cql_transform_open_fname(cql_fields);
+ /* If this fails, no problem: we detect cqltrans == 0 later */
+
+#if HAVE_READLINE_READLINE_H
+ rl_attempted_completion_function = (CPPFunction*)readline_completer;
+#endif
+
+
+ for(i=0; i<maxOtherInfosSupported; ++i) {
+ extraOtherInfos[i].oidval = -1;
+ }
+
+ source_rcfile();
+}
+
+
+#if HAVE_GETTIMEOFDAY
+struct timeval tv_start, tv_end;
+#endif
+
+#if HAVE_XML2
+static void handle_srw_record(Z_SRW_record *rec)
+{
+ if (rec->recordPosition)
+ {
+ printf ("pos=%d", *rec->recordPosition);
+ setno = *rec->recordPosition + 1;
+ }
+ if (rec->recordSchema)
+ printf (" schema=%s", rec->recordSchema);
+ printf ("\n");
+ if (rec->recordData_buf && rec->recordData_len)
+ {
+ fwrite(rec->recordData_buf, 1, rec->recordData_len, stdout);
+ if (marc_file)
+ fwrite (rec->recordData_buf, 1, rec->recordData_len, marc_file);
+ }
+ else
+ printf ("No data!");
+ printf("\n");
+}
+
+static void handle_srw_explain_response(Z_SRW_explainResponse *res)
+{
+ handle_srw_record(&res->record);
+}
+
+static void handle_srw_response(Z_SRW_searchRetrieveResponse *res)
+{
+ int i;
+
+ printf ("Received SRW SearchRetrieve Response\n");
+
+ for (i = 0; i<res->num_diagnostics; i++)
+ {
+ if (res->diagnostics[i].uri)
+ printf ("SRW diagnostic %s\n",
+ res->diagnostics[i].uri);
+ else
+ printf ("SRW diagnostic missing or could not be decoded\n");
+ if (res->diagnostics[i].message)
+ printf ("Message: %s\n", res->diagnostics[i].message);
+ if (res->diagnostics[i].details)
+ printf ("Details: %s\n", res->diagnostics[i].details);
+ }
+ if (res->numberOfRecords)
+ printf ("Number of hits: %d\n", *res->numberOfRecords);
+ for (i = 0; i<res->num_records; i++)
+ handle_srw_record(res->records + i);
+}
+
+static void handle_srw_scan_term(Z_SRW_scanTerm *term)
+{
+ if (term->displayTerm)
+ printf("%s:", term->displayTerm);
+ else if (term->value)
+ printf("%s:", term->value);
+ else
+ printf("No value:");
+ if (term->numberOfRecords)
+ printf(" %d", *term->numberOfRecords);
+ if (term->whereInList)
+ printf(" %s", term->whereInList);
+ if (term->value && term->displayTerm)
+ printf(" %s", term->value);
+
+ strcpy(last_scan_line, term->value);
+ printf("\n");
+}
+
+static void handle_srw_scan_response(Z_SRW_scanResponse *res)
+{
+ int i;
+
+ printf ("Received SRW Scan Response\n");
+
+ for (i = 0; i<res->num_diagnostics; i++)
+ {
+ if (res->diagnostics[i].uri)
+ printf ("SRW diagnostic %s\n",
+ res->diagnostics[i].uri);
+ else
+ printf ("SRW diagnostic missing or could not be decoded\n");
+ if (res->diagnostics[i].message)
+ printf ("Message: %s\n", res->diagnostics[i].message);
+ if (res->diagnostics[i].details)
+ printf ("Details: %s\n", res->diagnostics[i].details);
+ }
+ if (res->terms)
+ for (i = 0; i<res->num_terms; i++)
+ handle_srw_scan_term(res->terms + i);
+}
+
+static void http_response(Z_HTTP_Response *hres)
+{
+ int ret = -1;
+ const char *content_type = z_HTTP_header_lookup(hres->headers,
+ "Content-Type");
+ const char *connection_head = z_HTTP_header_lookup(hres->headers,
+ "Connection");
+ if (content_type && !yaz_strcmp_del("text/xml", content_type, "; "))
+ {
+ Z_SOAP *soap_package = 0;
+ ODR o = odr_createmem(ODR_DECODE);
+ Z_SOAP_Handler soap_handlers[2] = {
+ {"http://www.loc.gov/zing/srw/", 0,
+ (Z_SOAP_fun) yaz_srw_codec},
+ {0, 0, 0}
+ };
+ ret = z_soap_codec(o, &soap_package,
+ &hres->content_buf, &hres->content_len,
+ soap_handlers);
+ if (!ret && soap_package->which == Z_SOAP_generic &&
+ soap_package->u.generic->no == 0)
+ {
+ Z_SRW_PDU *sr = soap_package->u.generic->p;
+ if (sr->which == Z_SRW_searchRetrieve_response)
+ handle_srw_response(sr->u.response);
+ else if (sr->which == Z_SRW_explain_response)
+ handle_srw_explain_response(sr->u.explain_response);
+ else if (sr->which == Z_SRW_scan_response)
+ handle_srw_scan_response(sr->u.scan_response);
+ else
+ ret = -1;
+ }
+ else if (soap_package && (soap_package->which == Z_SOAP_fault
+ || soap_package->which == Z_SOAP_error))
+ {
+ printf ("HTTP Error Status=%d\n", hres->code);
+ printf ("SOAP Fault code %s\n",
+ soap_package->u.fault->fault_code);
+ printf ("SOAP Fault string %s\n",
+ soap_package->u.fault->fault_string);
+ if (soap_package->u.fault->details)
+ printf ("SOAP Details %s\n",
+ soap_package->u.fault->details);
+ }
+ else
+ ret = -1;
+ odr_destroy(o);
+ }
+ if (ret)
+ {
+ if (hres->code != 200)
+ {
+ printf ("HTTP Error Status=%d\n", hres->code);
+ }
+ else
+ {
+ printf ("Decoding of SRW package failed\n");
+ }
+ close_session();
+ }
+ else
+ {
+ if (!strcmp(hres->version, "1.0"))
+ {
+ /* HTTP 1.0: only if Keep-Alive we stay alive.. */
+ if (!connection_head || strcmp(connection_head, "Keep-Alive"))
+ close_session();
+ }
+ else
+ {
+ /* HTTP 1.1: only if no close we stay alive .. */
+ if (connection_head && !strcmp(connection_head, "close"))
+ close_session();
+ }
+ }
+}
+#endif
+
+void wait_and_handle_response()
+{
+ int reconnect_ok = 1;
+ int res;
+ char *netbuffer= 0;
+ int netbufferlen = 0;
+ Z_GDU *gdu;
+
+ while(conn)
+ {
+ res = cs_get(conn, &netbuffer, &netbufferlen);
+ if (reconnect_ok && res <= 0 && protocol == PROTO_HTTP)
+ {
+ cs_close(conn);
+ conn = 0;
+ cmd_open(0);
+ reconnect_ok = 0;
+ if (conn)
+ {
+ char *buf_out;
+ int len_out;
+
+ buf_out = odr_getbuf(out, &len_out, 0);
+
+ do_hex_dump(buf_out, len_out);
+
+ cs_put(conn, buf_out, len_out);
+
+ odr_reset(out);
+ continue;
+ }
+ }
+ else if (res <= 0)
+ {
+ printf("Target closed connection\n");
+ close_session();
+ break;
+ }
+ odr_reset(out);
+ odr_reset(in); /* release APDU from last round */
+ record_last = 0;
+ do_hex_dump(netbuffer, res);
+ odr_setbuf(in, netbuffer, res, 0);
+
+ if (!z_GDU(in, &gdu, 0, 0))
+ {
+ FILE *f = ber_file ? ber_file : stdout;
+ odr_perror(in, "Decoding incoming APDU");
+ fprintf(f, "[Near %d]\n", odr_offset(in));
+ fprintf(f, "Packet dump:\n---------\n");
+ odr_dumpBER(f, netbuffer, res);
+ fprintf(f, "---------\n");
+ if (apdu_file)
+ {
+ z_GDU(print, &gdu, 0, 0);
+ odr_reset(print);
+ }
+ if (conn && cs_more(conn))
+ continue;
+ break;
+ }
+ if (ber_file)
+ odr_dumpBER(ber_file, netbuffer, res);
+ if (apdu_file && !z_GDU(print, &gdu, 0, 0))
+ {
+ odr_perror(print, "Failed to print incoming APDU");
+ odr_reset(print);
+ continue;
+ }
+ if (gdu->which == Z_GDU_Z3950)
+ {
+ Z_APDU *apdu = gdu->u.z3950;
+ switch(apdu->which)
+ {
+ case Z_APDU_initResponse:
+ process_initResponse(apdu->u.initResponse);
+ break;
+ case Z_APDU_searchResponse:
+ process_searchResponse(apdu->u.searchResponse);
+ break;
+ case Z_APDU_scanResponse:
+ process_scanResponse(apdu->u.scanResponse);
+ break;
+ case Z_APDU_presentResponse:
+ print_refid (apdu->u.presentResponse->referenceId);
+ setno +=
+ *apdu->u.presentResponse->numberOfRecordsReturned;
+ if (apdu->u.presentResponse->records)
+ display_records(apdu->u.presentResponse->records);
+ else
+ printf("No records.\n");
+ printf ("nextResultSetPosition = %d\n",
+ *apdu->u.presentResponse->nextResultSetPosition);
+ break;
+ case Z_APDU_sortResponse:
+ process_sortResponse(apdu->u.sortResponse);
+ break;
+ case Z_APDU_extendedServicesResponse:
+ printf("Got extended services response\n");
+ process_ESResponse(apdu->u.extendedServicesResponse);
+ break;
+ case Z_APDU_close:
+ printf("Target has closed the association.\n");
+ process_close(apdu->u.close);
+ break;
+ case Z_APDU_resourceControlRequest:
+ process_resourceControlRequest
+ (apdu->u.resourceControlRequest);
+ break;
+ case Z_APDU_deleteResultSetResponse:
+ process_deleteResultSetResponse(apdu->u.
+ deleteResultSetResponse);
+ break;
+ default:
+ printf("Received unknown APDU type (%d).\n",
+ apdu->which);
+ close_session ();
+ }
+ }
+#if HAVE_XML2
+ else if (gdu->which == Z_GDU_HTTP_Response)
+ {
+ http_response(gdu->u.HTTP_Response);
+ }
+#endif
+ if (conn && !cs_more(conn))
+ break;
+ }
+ if (conn)
+ {
+#if HAVE_GETTIMEOFDAY
+ gettimeofday (&tv_end, 0);
+#if 0
+ printf ("S/U S/U=%ld/%ld %ld/%ld",
+ (long) tv_start.tv_sec,
+ (long) tv_start.tv_usec,
+ (long) tv_end.tv_sec,
+ (long) tv_end.tv_usec);
+#endif
+ printf ("Elapsed: %.6f\n",
+ (double) tv_end.tv_usec / 1e6 + tv_end.tv_sec -
+ ((double) tv_start.tv_usec / 1e6 + tv_start.tv_sec));
+#endif
+ }
+ xfree (netbuffer);
+}
+
+
+int cmd_cclparse(const char* arg)
+{
+ int error, pos;
+ struct ccl_rpn_node *rpn=NULL;
+
+
+ rpn = ccl_find_str (bibset, arg, &error, &pos);
+
+ if (error) {
+ printf ("%*s^ - ", 3+strlen(last_cmd)+1+pos, " ");
+ printf ("%s\n", ccl_err_msg (error));
+ }
+ else
+ {
+ if (rpn)
+ {
+ ccl_pr_tree(rpn, stdout);
+ }
+ }
+ if (rpn)
+ ccl_rpn_delete(rpn);
+
+ printf ("\n");
+
+ return 0;
+}
+
+
+int cmd_set_otherinfo(const char* args)
+{
+ char oidstr[101], otherinfoString[101];
+ int otherinfoNo;
+ int sscan_res;
+ int oidval;
+
+ sscan_res = sscanf (args, "%d %100[^ ] %100s", &otherinfoNo, oidstr, otherinfoString);
+ if (sscan_res==1) {
+ /* reset this otherinfo */
+ if(otherinfoNo>=maxOtherInfosSupported) {
+ printf("Error otherinfo index to large (%d>%d)\n",
+ otherinfoNo,maxOtherInfosSupported);
+ }
+ extraOtherInfos[otherinfoNo].oidval = -1;
+ if (extraOtherInfos[otherinfoNo].value)
+ free(extraOtherInfos[otherinfoNo].value);
+ return 0;
+ }
+ if (sscan_res<3) {
+ printf("Error in set_otherinfo command \n");
+ return 0;
+ }
+
+ if (otherinfoNo>=maxOtherInfosSupported) {
+ printf("Error otherinfo index to large (%d>%d)\n",
+ otherinfoNo,maxOtherInfosSupported);
+ }
+
+ oidval = oid_getvalbyname (oidstr);
+ if (oidval == -1 ) {
+ printf("Error in set_otherinfo command unknown oid %s \n",oidstr);
+ return 0;
+ }
+ extraOtherInfos[otherinfoNo].oidval = oidval;
+ if(extraOtherInfos[otherinfoNo].value) free(extraOtherInfos[otherinfoNo].value);
+ extraOtherInfos[otherinfoNo].value = strdup(otherinfoString);
+
+ return 0;
+}
+
+int cmd_sleep(const char* args )
+{
+ int sec=atoi(args);
+ if( sec > 0 ) {
+#ifdef WIN32
+ Sleep(sec*1000);
+#else
+ sleep(sec);
+#endif
+ printf("Done sleeping %d seconds\n", sec);
+ }
+ return 1;
+}
+
+int cmd_list_otherinfo(const char* args)
+{
+ int i;
+
+ if(strlen(args)>0) {
+ i = atoi(args);
+ if( i >= maxOtherInfosSupported ) {
+ printf("Error otherinfo index to large (%d>%d)\n",i,maxOtherInfosSupported);
+ return 0;
+ }
+
+ if(extraOtherInfos[i].oidval != -1)
+ printf(" otherinfo %d %s %s\n",
+ i,
+ yaz_z3950_oid_value_to_str(
+ (enum oid_value) extraOtherInfos[i].oidval,
+ CLASS_RECSYN),
+ extraOtherInfos[i].value);
+
+ } else {
+ for(i=0; i<maxOtherInfosSupported; ++i) {
+ if(extraOtherInfos[i].oidval != -1)
+ printf(" otherinfo %d %s %s\n",
+ i,
+ yaz_z3950_oid_value_to_str(
+ (enum oid_value) extraOtherInfos[i].oidval,
+ CLASS_RECSYN),
+ extraOtherInfos[i].value);
+ }
+
+ }
+ return 0;
+}
+
+
+int cmd_list_all(const char* args) {
+ int i;
+
+ /* connection options */
+ if(conn) {
+ printf("Connected to : %s\n",last_open_command);
+ } else {
+ if(last_open_command)
+ printf("Not connected to : %s\n",last_open_command);
+ else
+ printf("Not connected : \n");
+
+ }
+ if(yazProxy) printf("using proxy : %s\n",yazProxy);
+
+ printf("auto_reconnect : %s\n",auto_reconnect?"on":"off");
+
+ if (!auth) {
+ printf("Authentication : none\n");
+ } else {
+ switch(auth->which) {
+ case Z_IdAuthentication_idPass:
+ printf("Authentication : IdPass\n");
+ printf(" Login User : %s\n",auth->u.idPass->userId?auth->u.idPass->userId:"");
+ printf(" Login Group : %s\n",auth->u.idPass->groupId?auth->u.idPass->groupId:"");
+ printf(" Password : %s\n",auth->u.idPass->password?auth->u.idPass->password:"");
+ break;
+ case Z_IdAuthentication_open:
+ printf("Authentication : psOpen\n");
+ printf(" Open string : %s\n",auth->u.open);
+ break;
+ default:
+ printf("Authentication : Unknown\n");
+ }
+ }
+ if (negotiationCharset)
+ printf("Neg. Character set : `%s'\n", negotiationCharset);
+
+ /* bases */
+ printf("Bases : ");
+ for (i = 0; i<num_databaseNames; i++) printf("%s ",databaseNames[i]);
+ printf("\n");
+
+ /* Query options */
+ printf("CCL file : %s\n",ccl_fields);
+ printf("CQL file : %s\n",cql_fields);
+ printf("Query type : %s\n",query_type_as_string(queryType));
+
+ printf("Named Result Sets : %s\n",setnumber==-1?"off":"on");
+
+ /* piggy back options */
+ printf("ssub/lslb/mspn : %d/%d/%d\n",smallSetUpperBound,largeSetLowerBound,mediumSetPresentNumber);
+
+ /* print present related options */
+ printf("Format : %s\n",yaz_z3950_oid_value_to_str(recordsyntax,CLASS_RECSYN));
+ printf("Schema : %s\n",record_schema ? record_schema : "not set");
+ printf("Elements : %s\n",elementSetNames?elementSetNames->u.generic:"");
+
+ /* loging options */
+ printf("APDU log : %s\n",apdu_file?"on":"off");
+ printf("Record log : %s\n",marc_file?"on":"off");
+
+ /* other infos */
+ printf("Other Info: \n");
+ cmd_list_otherinfo("");
+
+ return 0;
+}
+
+int cmd_clear_otherinfo(const char* args)
+{
+ if(strlen(args)>0) {
+ int otherinfoNo;
+ otherinfoNo = atoi(args);
+ if( otherinfoNo >= maxOtherInfosSupported ) {
+ printf("Error otherinfo index to large (%d>%d)\n",otherinfoNo,maxOtherInfosSupported);
+ return 0;
+ }
+
+ if(extraOtherInfos[otherinfoNo].oidval != -1) {
+ /* only clear if set. */
+ extraOtherInfos[otherinfoNo].oidval=-1;
+ free(extraOtherInfos[otherinfoNo].value);
+ }
+ } else {
+ int i;
+
+ for(i=0; i<maxOtherInfosSupported; ++i) {
+ if (extraOtherInfos[i].oidval!=-1 ) {
+ extraOtherInfos[i].oidval=-1;
+ free(extraOtherInfos[i].value);
+ }
+ }
+ }
+ return 0;
+}
+
+static int cmd_help (const char *line);
+
+typedef char *(*completerFunctionType)(const char *text, int state);
+
+static struct {
+ char *cmd;
+ int (*fun)(const char *arg);
+ char *ad;
+ completerFunctionType rl_completerfunction;
+ int complete_filenames;
+ char **local_tabcompletes;
+} cmd_array[] = {
+ {"open", cmd_open, "('tcp'|'ssl')':<host>[':'<port>][/<db>]",NULL,0,NULL},
+ {"quit", cmd_quit, "",NULL,0,NULL},
+ {"find", cmd_find, "<query>",NULL,0,NULL},
+ {"delete", cmd_delete, "<setname>",NULL,0,NULL},
+ {"base", cmd_base, "<base-name>",NULL,0,NULL},
+ {"show", cmd_show, "<rec#>['+'<#recs>['+'<setname>]]",NULL,0,NULL},
+ {"scan", cmd_scan, "<term>",NULL,0,NULL},
+ {"scanstep", cmd_scanstep, "<size>",NULL,0,NULL},
+ {"scanpos", cmd_scanpos, "<size>",NULL,0,NULL},
+ {"sort", cmd_sort, "<sortkey> <flag> <sortkey> <flag> ...",NULL,0,NULL},
+ {"sort+", cmd_sort_newset, "<sortkey> <flag> <sortkey> <flag> ...",NULL,0,NULL},
+ {"authentication", cmd_authentication, "<acctstring>",NULL,0,NULL},
+ {"lslb", cmd_lslb, "<largeSetLowerBound>",NULL,0,NULL},
+ {"ssub", cmd_ssub, "<smallSetUpperBound>",NULL,0,NULL},
+ {"mspn", cmd_mspn, "<mediumSetPresentNumber>",NULL,0,NULL},
+ {"status", cmd_status, "",NULL,0,NULL},
+ {"setnames", cmd_setnames, "",NULL,0,NULL},
+ {"cancel", cmd_cancel, "",NULL,0,NULL},
+ {"cancel_find", cmd_cancel_find, "<query>",NULL,0,NULL},
+ {"format", cmd_format, "<recordsyntax>",complete_format,0,NULL},
+ {"schema", cmd_schema, "<schema>",complete_schema,0,NULL},
+ {"elements", cmd_elements, "<elementSetName>",NULL,0,NULL},
+ {"close", cmd_close, "",NULL,0,NULL},
+ {"attributeset", cmd_attributeset, "<attrset>",complete_attributeset,0,NULL},
+ {"querytype", cmd_querytype, "<type>",complete_querytype,0,NULL},
+ {"refid", cmd_refid, "<id>",NULL,0,NULL},
+ {"itemorder", cmd_itemorder, "ill|item <itemno>",NULL,0,NULL},
+ {"update", cmd_update, "<action> <recid> [<file>]",NULL,0,NULL},
+ {"update0", cmd_update0, "<action> <recid> [<file>]",NULL,0,NULL},
+ {"packagename", cmd_packagename, "<packagename>",NULL,0,NULL},
+ {"proxy", cmd_proxy, "[('tcp'|'ssl')]<host>[':'<port>]",NULL,0,NULL},
+ {"charset", cmd_charset, "<nego_charset> <output_charset>",NULL,0,NULL},
+ {"negcharset", cmd_negcharset, "<nego_charset>",NULL,0,NULL},
+ {"displaycharset", cmd_displaycharset, "<output_charset>",NULL,0,NULL},
+ {"marccharset", cmd_marccharset, "<charset_name>",NULL,0,NULL},
+ {"lang", cmd_lang, "<language_code>",NULL,0,NULL},
+ {".", cmd_source_echo, "<filename>",NULL,1,NULL},
+ {"!", cmd_subshell, "Subshell command",NULL,1,NULL},
+ {"set_apdufile", cmd_set_apdufile, "<filename>",NULL,1,NULL},
+ {"set_berfile", cmd_set_berfile, "<filename>",NULL,1,NULL},
+ {"set_marcdump", cmd_set_marcdump," <filename>",NULL,1,NULL},
+ {"set_cclfile", cmd_set_cclfile," <filename>",NULL,1,NULL},
+ {"set_cqlfile", cmd_set_cqlfile," <filename>",NULL,1,NULL},
+ {"set_auto_reconnect", cmd_set_auto_reconnect," on|off",complete_auto_reconnect,1,NULL},
+ {"set_otherinfo", cmd_set_otherinfo,"<otherinfoinddex> <oid> <string>",NULL,0,NULL},
+ {"sleep", cmd_sleep,"<seconds>",NULL,0,NULL},
+ {"register_oid", cmd_register_oid,"<name> <class> <oid>",NULL,0,NULL},
+ {"push_command", cmd_push_command,"<command>",command_generator,0,NULL},
+ {"register_tab", cmd_register_tab,"<commandname> <tab>",command_generator,0,NULL},
+ {"cclparse", cmd_cclparse,"<ccl find command>",NULL,0,NULL},
+ {"list_otherinfo",cmd_list_otherinfo,"[otherinfoinddex]",NULL,0,NULL},
+ {"list_all",cmd_list_all,"",NULL,0,NULL},
+ {"clear_otherinfo",cmd_clear_otherinfo,"",NULL,0,NULL},
+ /* Server Admin Functions */
+ {"adm-reindex", cmd_adm_reindex, "<database-name>",NULL,0,NULL},
+ {"adm-truncate", cmd_adm_truncate, "('database'|'index')<object-name>",NULL,0,NULL},
+ {"adm-create", cmd_adm_create, "",NULL,0,NULL},
+ {"adm-drop", cmd_adm_drop, "('database'|'index')<object-name>",NULL,0,NULL},
+ {"adm-import", cmd_adm_import, "<record-type> <dir> <pattern>",NULL,0,NULL},
+ {"adm-refresh", cmd_adm_refresh, "",NULL,0,NULL},
+ {"adm-commit", cmd_adm_commit, "",NULL,0,NULL},
+ {"adm-shutdown", cmd_adm_shutdown, "",NULL,0,NULL},
+ {"adm-startup", cmd_adm_startup, "",NULL,0,NULL},
+ {"explain", cmd_explain, "", NULL, 0, NULL},
+ {"options", cmd_options, "", NULL, 0, NULL},
+ {"zversion", cmd_zversion, "", NULL, 0, NULL},
+ {"help", cmd_help, "", NULL,0,NULL},
+ {"init", cmd_init, "", NULL,0,NULL},
+ {0,0,0,0,0,0}
+};
+
+static int cmd_help (const char *line)
+{
+ int i;
+ char topic[21];
+
+ *topic = 0;
+ sscanf (line, "%20s", topic);
+
+ if (*topic == 0)
+ printf("Commands:\n");
+ for (i = 0; cmd_array[i].cmd; i++)
+ if (*topic == 0 || strcmp (topic, cmd_array[i].cmd) == 0)
+ printf(" %s %s\n", cmd_array[i].cmd, cmd_array[i].ad);
+ if (strcmp (topic, "find") == 0)
+ {
+ printf ("RPN:\n");
+ printf (" \"term\" Simple Term\n");
+ printf (" @attr [attset] type=value op Attribute\n");
+ printf (" @and opl opr And\n");
+ printf (" @or opl opr Or\n");
+ printf (" @not opl opr And-Not\n");
+ printf (" @set set Result set\n");
+ printf ("\n");
+ printf ("Bib-1 attribute types\n");
+ printf ("1=Use: ");
+ printf ("4=Title 7=ISBN 8=ISSN 30=Date 62=Abstract 1003=Author 1016=Any\n");
+ printf ("2=Relation: ");
+ printf ("1< 2<= 3= 4>= 5> 6!= 102=Relevance\n");
+ printf ("3=Position: ");
+ printf ("1=First in Field 2=First in subfield 3=Any position\n");
+ printf ("4=Structure: ");
+ printf ("1=Phrase 2=Word 3=Key 4=Year 5=Date 6=WordList\n");
+ printf ("5=Truncation: ");
+ printf ("1=Right 2=Left 3=L&R 100=No 101=# 102=Re-1 103=Re-2\n");
+ printf ("6=Completeness:");
+ printf ("1=Incomplete subfield 2=Complete subfield 3=Complete field\n");
+ }
+ return 1;
+}
+
+int cmd_register_tab(const char* arg) {
+
+ char command[101], tabargument[101];
+ int i;
+ int num_of_tabs;
+ char** tabslist;
+
+ if (sscanf (arg, "%100s %100s", command, tabargument) < 1) {
+ return 0;
+ }
+
+ /* locate the amdn in the list */
+ for (i = 0; cmd_array[i].cmd; i++) {
+ if (!strncmp(cmd_array[i].cmd, command, strlen(command))) {
+ break;
+ }
+ }
+
+ if(!cmd_array[i].cmd) {
+ fprintf(stderr,"Unknown command %s\n",command);
+ return 1;
+ }
+
+
+ if(!cmd_array[i].local_tabcompletes)
+ cmd_array[i].local_tabcompletes = (char **) calloc(1,sizeof(char**));
+
+ num_of_tabs=0;
+
+ tabslist = cmd_array[i].local_tabcompletes;
+ for(;tabslist && *tabslist;tabslist++) {
+ num_of_tabs++;
+ }
+
+ cmd_array[i].local_tabcompletes = (char **)
+ realloc(cmd_array[i].local_tabcompletes,(num_of_tabs+2)*sizeof(char**));
+ tabslist=cmd_array[i].local_tabcompletes;
+ tabslist[num_of_tabs]=strdup(tabargument);
+ tabslist[num_of_tabs+1]=NULL;
+ return 1;
+}
+
+
+void process_cmd_line(char* line)
+{
+ int i,res;
+ char word[32], arg[10240];
+
+#if HAVE_GETTIMEOFDAY
+ gettimeofday (&tv_start, 0);
+#endif
+
+ if ((res = sscanf(line, "%31s %10239[^;]", word, arg)) <= 0)
+ {
+ strcpy(word, last_cmd);
+ *arg = '\0';
+ }
+ else if (res == 1)
+ *arg = 0;
+ strcpy(last_cmd, word);
+
+ /* removed tailing spaces from the arg command */
+ {
+ char* p = arg;
+ char* lastnonspace=NULL;
+
+ for(;*p; ++p) {
+ if(!isspace(*(unsigned char *) p)) {
+ lastnonspace = p;
+ }
+ }
+ if(lastnonspace)
+ *(++lastnonspace) = 0;
+ }
+
+ for (i = 0; cmd_array[i].cmd; i++)
+ if (!strncmp(cmd_array[i].cmd, word, strlen(word)))
+ {
+ res = (*cmd_array[i].fun)(arg);
+ break;
+ }
+
+ if (!cmd_array[i].cmd) /* dump our help-screen */
+ {
+ printf("Unknown command: %s.\n", word);
+ printf("use help for list of commands\n");
+ /* cmd_help (""); */
+ res = 1;
+ }
+
+ if(apdu_file) fflush(apdu_file);
+
+ if (res >= 2)
+ wait_and_handle_response();
+
+ if(apdu_file)
+ fflush(apdu_file);
+ if(marc_file)
+ fflush(marc_file);
+}
+
+
+char *command_generator(const char *text, int state)
+{
+ static int idx;
+ if (state==0) {
+ idx = 0;
+ }
+ for( ; cmd_array[idx].cmd; ++idx) {
+ if (!strncmp(cmd_array[idx].cmd,text,strlen(text))) {
+ ++idx; /* skip this entry on the next run */
+ return strdup(cmd_array[idx-1].cmd);
+ }
+ }
+ return NULL;
+}
+
+
+/*
+ This function only known how to complete on the first word
+*/
+char ** readline_completer(char *text, int start, int end) {
+#if HAVE_READLINE_READLINE_H
+
+ completerFunctionType completerToUse;
+
+ if(start == 0) {
+#if HAVE_READLINE_RL_COMPLETION_MATCHES
+ char** res=rl_completion_matches(text,
+ command_generator);
+#else
+ char** res=completion_matches(text,
+ (CPFunction*)command_generator);
+#endif
+ rl_attempted_completion_over = 1;
+ return res;
+ } else {
+ char arg[10240],word[32];
+ int i=0 ,res;
+ if ((res = sscanf(rl_line_buffer, "%31s %10239[^;]", word, arg)) <= 0) {
+ rl_attempted_completion_over = 1;
+ return NULL;
+ }
+
+ for (i = 0; cmd_array[i].cmd; i++) {
+ if (!strncmp(cmd_array[i].cmd, word, strlen(word))) {
+ break;
+ }
+ }
+
+ if(!cmd_array[i].cmd) return NULL;
+
+ curret_global_list = cmd_array[i].local_tabcompletes;
+
+ completerToUse = cmd_array[i].rl_completerfunction;
+ if(completerToUse==NULL) /* if no pr. command completer is defined use the default completer */
+ completerToUse = default_completer;
+
+ if(completerToUse) {
+#ifdef HAVE_READLINE_RL_COMPLETION_MATCHES
+ char** res=
+ rl_completion_matches(text,
+ completerToUse);
+#else
+ char** res=
+ completion_matches(text,
+ (CPFunction*)completerToUse);
+#endif
+ if(!cmd_array[i].complete_filenames)
+ rl_attempted_completion_over = 1;
+ return res;
+ } else {
+ if(!cmd_array[i].complete_filenames)
+ rl_attempted_completion_over = 1;
+ return 0;
+ }
+ }
+#else
+ return 0;
+#endif
+}
+
+
+static void client(void)
+{
+ char line[10240];
+
+ line[10239] = '\0';
+
+#if HAVE_GETTIMEOFDAY
+ gettimeofday (&tv_start, 0);
+#endif
+
+ while (1)
+ {
+ char *line_in = NULL;
+#if HAVE_READLINE_READLINE_H
+ if (isatty(0))
+ {
+ line_in=readline(C_PROMPT);
+ if (!line_in)
+ break;
+#if HAVE_READLINE_HISTORY_H
+ if (*line_in)
+ add_history(line_in);
+#endif
+ strncpy(line, line_in, 10239);
+ free (line_in);
+ }
+#endif
+ if (!line_in)
+ {
+ char *end_p;
+ printf (C_PROMPT);
+ fflush(stdout);
+ if (!fgets(line, 10239, stdin))
+ break;
+ if ((end_p = strchr (line, '\n')))
+ *end_p = '\0';
+ }
+ process_cmd_line(line);
+ }
+}
+
+static void show_version(void)
+{
+ char vstr[20];
+
+ yaz_version(vstr, 0);
+ printf ("YAZ version: %s\n", YAZ_VERSION);
+ if (strcmp(vstr, YAZ_VERSION))
+ printf ("YAZ DLL/SO: %s\n", vstr);
+ exit(0);
+}
+
+int main(int argc, char **argv)
+{
+ char *prog = *argv;
+ char *open_command = 0;
+ char *auth_command = 0;
+ char *arg;
+ int ret;
+
+#if HAVE_LOCALE_H
+ if (!setlocale(LC_CTYPE, ""))
+ fprintf (stderr, "setlocale failed\n");
+#endif
+#if HAVE_LANGINFO_H
+#ifdef CODESET
+ codeset = nl_langinfo(CODESET);
+#endif
+#endif
+ if (codeset)
+ outputCharset = xstrdup(codeset);
+
+ ODR_MASK_SET(&z3950_options, Z_Options_search);
+ ODR_MASK_SET(&z3950_options, Z_Options_present);
+ ODR_MASK_SET(&z3950_options, Z_Options_namedResultSets);
+ ODR_MASK_SET(&z3950_options, Z_Options_triggerResourceCtrl);
+ ODR_MASK_SET(&z3950_options, Z_Options_scan);
+ ODR_MASK_SET(&z3950_options, Z_Options_sort);
+ ODR_MASK_SET(&z3950_options, Z_Options_extendedServices);
+ ODR_MASK_SET(&z3950_options, Z_Options_delSet);
+
+ while ((ret = options("k:c:q:a:b:m:v:p:u:t:Vxd:", argv, argc, &arg)) != -2)
+ {
+ switch (ret)
+ {
+ case 0:
+ if (!open_command)
+ {
+ open_command = (char *) xmalloc (strlen(arg)+6);
+ strcpy (open_command, "open ");
+ strcat (open_command, arg);
+ }
+ break;
+ case 'd':
+ dump_file_prefix = arg;
+ break;
+ case 'k':
+ kilobytes = atoi(arg);
+ break;
+ case 'm':
+ if (!(marc_file = fopen (arg, "a")))
+ {
+ perror (arg);
+ exit (1);
+ }
+ break;
+ case 't':
+ outputCharset = xstrdup(arg);
+ break;
+ case 'c':
+ strncpy (ccl_fields, arg, sizeof(ccl_fields)-1);
+ ccl_fields[sizeof(ccl_fields)-1] = '\0';
+ break;
+ case 'q':
+ strncpy (cql_fields, arg, sizeof(cql_fields)-1);
+ cql_fields[sizeof(cql_fields)-1] = '\0';
+ break;
+ case 'b':
+ if (!strcmp(arg, "-"))
+ ber_file=stderr;
+ else
+ ber_file=fopen(arg, "a");
+ break;
+ case 'a':
+ if (!strcmp(arg, "-"))
+ apdu_file=stderr;
+ else
+ apdu_file=fopen(arg, "a");
+ break;
+ case 'x':
+ hex_dump = 1;
+ break;
+ case 'p':
+ yazProxy=strdup(arg);
+ break;
+ case 'u':
+ if (!auth_command)
+ {
+ auth_command = (char *) xmalloc (strlen(arg)+6);
+ strcpy (auth_command, "auth ");
+ strcat (auth_command, arg);
+ }
+ break;
+ case 'v':
+ yaz_log_init(yaz_log_mask_str(arg), "", 0);
+ break;
+ case 'V':
+ show_version();
+ break;
+ default:
+ fprintf (stderr, "Usage: %s [-m <marclog>] [ -a <apdulog>] "
+ "[-b berdump] [-c cclfields] \n"
+ "[-q cqlfields] [-p <proxy-addr>] [-u <auth>] "
+ "[-k size] [-d dump] [-V] [<server-addr>]\n",
+ prog);
+ exit (1);
+ }
+ }
+ initialize();
+ if (auth_command)
+ {
+#ifdef HAVE_GETTIMEOFDAY
+ gettimeofday (&tv_start, 0);
+#endif
+ process_cmd_line (auth_command);
+#if HAVE_READLINE_HISTORY_H
+ add_history(auth_command);
+#endif
+ xfree(auth_command);
+ }
+ if (open_command)
+ {
+#ifdef HAVE_GETTIMEOFDAY
+ gettimeofday (&tv_start, 0);
+#endif
+ process_cmd_line (open_command);
+#if HAVE_READLINE_HISTORY_H
+ add_history(open_command);
+#endif
+ xfree(open_command);
+ }
+ client ();
+ exit (0);
+}
+
+/*
+ * Local variables:
+ * tab-width: 8
+ * c-basic-offset: 4
+ * End:
+ * vim600: sw=4 ts=8 fdm=marker
+ * vim<600: sw=4 ts=8
+ */