+ return 0;
+}
+
+static int cmd_sru(const char *arg)
+{
+ if (!*arg)
+ {
+ printf("SRU method is: %s\n", sru_method);
+ printf("SRU version is: %s\n", sru_version);
+ }
+ else
+ {
+ int r = sscanf(arg, "%9s %9s", sru_method, sru_version);
+ if (r >= 1)
+ {
+ if (!yaz_matchstr(sru_method, "post"))
+ ;
+ else if (!yaz_matchstr(sru_method, "get"))
+ ;
+ else if (!yaz_matchstr(sru_method, "soap"))
+ ;
+ else if (!yaz_matchstr(sru_method, "solr"))
+ ;
+ else
+ {
+ strcpy(sru_method, "soap");
+ printf("Unknown SRU method: %s\n", arg);
+ printf("Specify one of POST, GET, SOAP, SOLR\n");
+ }
+ }
+ }
+ return 0;
+}
+
+static int cmd_find(const char *arg)
+{
+ if (!*arg)
+ {
+ printf("Find what?\n");
+ return 0;
+ }
+ if (protocol == PROTO_HTTP)
+ {
+#if YAZ_HAVE_XML2
+ if (!conn)
+ session_connect(cur_host);
+ if (!conn)
+ return 0;
+ if (!send_SRW_searchRequest(arg))
+ return 0;
+#else
+ return 0;
+#endif
+ }
+ else
+ {
+ if (*cur_host && auto_reconnect)
+ {
+ int i = 0;
+ for (;;)
+ {
+ if (conn)
+ {
+ if (!send_Z3950_searchRequest(arg))
+ return 0;
+ wait_and_handle_response(0);
+ if (conn)
+ break;
+ }
+ if (++i == 2)
+ {
+ printf("Unable to reconnect\n");
+ break;
+ }
+ session_connect(cur_host);
+ wait_and_handle_response(0);
+ }
+ return 0;
+ }
+ else if (conn)
+ {
+ if (!send_Z3950_searchRequest(arg))
+ return 0;
+ }
+ else
+ {
+ printf("Not connected yet\n");
+ return 0;
+ }
+ }
+ return 2;
+}
+
+static int cmd_facets(const char *arg)
+{
+ if (!facet_odr)
+ facet_odr = odr_createmem(ODR_ENCODE);
+ odr_reset(facet_odr);
+
+ if (!*arg)
+ {
+ facet_list = 0;
+ printf("Facets cleared.\n");
+ return 0;
+ }
+ facet_list = yaz_pqf_parse_facet_list(facet_odr, arg);
+ if (!facet_list)
+ {
+ printf("Invalid facet list: %s", arg);
+ return 0;
+ }
+ return 1;
+}
+
+static int cmd_delete(const char *arg)
+{
+ if (only_z3950())
+ return 0;
+ if (!send_Z3950_deleteResultSetRequest(arg))
+ return 0;
+ return 2;
+}
+
+static int cmd_ssub(const char *arg)
+{
+ if (!(smallSetUpperBound = atoi(arg)))
+ return 0;
+ return 1;
+}
+
+static int cmd_lslb(const char *arg)
+{
+ if (!(largeSetLowerBound = atoi(arg)))
+ return 0;
+ return 1;
+}
+
+static int cmd_mspn(const char *arg)
+{
+ if (!(mediumSetPresentNumber = atoi(arg)))
+ return 0;
+ return 1;
+}
+
+static int cmd_status(const char *arg)
+{
+ printf("smallSetUpperBound: %d\n", smallSetUpperBound);
+ printf("largeSetLowerBound: %d\n", largeSetLowerBound);
+ printf("mediumSetPresentNumber: %d\n", mediumSetPresentNumber);
+ return 1;
+}
+
+static int cmd_setnames(const char *arg)
+{
+ if (*arg == '1') /* enable ? */
+ setnumber = 0;
+ else if (*arg == '0') /* disable ? */
+ setnumber = -1;
+ else if (setnumber < 0) /* no args, toggle .. */
+ setnumber = 0;
+ else
+ setnumber = -1;
+
+ if (setnumber >= 0)
+ printf("Set numbering enabled.\n");
+ else
+ printf("Set numbering disabled.\n");
+ return 1;
+}
+
+/* PRESENT SERVICE ----------------------------- */
+
+size_t check_token(const char *haystack, const char *token)
+{
+ size_t len = strlen(token);
+ size_t extra;
+ if (strncmp(haystack, token, len))
+ return 0;
+ for (extra = 0; haystack[extra + len] != '\0'; extra++)
+ if (!strchr(" \r\n\t", haystack[extra + len]))
+ {
+ if (extra)
+ break;
+ else
+ return 0; /* no whitespace after token */
+ }
+ return extra + len;
+}
+
+static int parse_show_args(const char *arg_c, char *setstring,
+ Odr_int *start, Odr_int *number)
+{
+ char *end_ptr;
+ Odr_int start_position;
+ size_t token_len;
+
+ if (setnumber >= 0)
+ sprintf(setstring, "%d", setnumber);
+ else
+ *setstring = '\0';
+
+ token_len = check_token(arg_c, "format");
+ if (token_len)
+ {
+ pretty_xml = 1;
+ arg_c += token_len;
+ }
+ else
+ pretty_xml = 0;
+
+ token_len = check_token(arg_c, "all");
+ if (token_len)
+ {
+ *number = last_hit_count;
+ *start = 1;
+ return 1;
+ }
+ start_position = odr_strtol(arg_c, &end_ptr, 10);
+ if (end_ptr == arg_c)
+ return 1;
+ *start = start_position;
+ if (*end_ptr == '\0')
+ return 1;
+ while (yaz_isspace(*end_ptr))
+ end_ptr++;
+ if (*end_ptr != '+')
+ {
+ printf("Bad show arg: expected +. Got %s\n", end_ptr);
+ return 0;
+ }
+ end_ptr++;
+ arg_c = end_ptr;
+ *number = odr_strtol(arg_c, &end_ptr, 10);
+ if (end_ptr == arg_c)
+ {
+ printf("Bad show arg: expected number after +\n");
+ return 0;
+ }
+ if (*end_ptr == '\0')
+ return 1;
+ while (yaz_isspace(*end_ptr))
+ end_ptr++;
+ if (*end_ptr != '+')
+ {
+ printf("Bad show arg: + expected. Got %s\n", end_ptr);
+ return 0;
+ }
+ strcpy(setstring, end_ptr+1);
+ return 1;
+}
+
+static int send_Z3950_presentRequest(const char *arg)
+{
+ Z_APDU *apdu = zget_APDU(out, Z_APDU_presentRequest);
+ Z_PresentRequest *req = apdu->u.presentRequest;
+ Z_RecordComposition compo;
+ Odr_int nos = 1;
+ char setstring[100];
+
+ req->referenceId = set_refid(out);
+
+ if (!parse_show_args(arg, setstring, &setno, &nos))
+ return 0;
+ if (*setstring)
+ req->resultSetId = setstring;
+
+ req->resultSetStartPoint = &setno;
+ req->numberOfRecordsRequested = &nos;
+
+ if (recordsyntax_size)
+ req->preferredRecordSyntax =
+ yaz_string_to_oid_odr(yaz_oid_std(),
+ CLASS_RECSYN, recordsyntax_list[0], out);
+
+ if (record_schema || recordsyntax_size >= 2)
+ {
+ req->recordComposition = &compo;
+ compo.which = Z_RecordComp_complex;
+ compo.u.complex = (Z_CompSpec *)
+ odr_malloc(out, sizeof(*compo.u.complex));
+ compo.u.complex->selectAlternativeSyntax = (bool_t *)
+ odr_malloc(out, sizeof(bool_t));
+ *compo.u.complex->selectAlternativeSyntax = 0;
+
+ compo.u.complex->generic = (Z_Specification *)
+ odr_malloc(out, sizeof(*compo.u.complex->generic));
+
+ compo.u.complex->generic->which = Z_Schema_oid;
+ if (!record_schema)
+ compo.u.complex->generic->schema.oid = 0;
+ else
+ {
+ compo.u.complex->generic->schema.oid =
+ yaz_string_to_oid_odr(yaz_oid_std(),
+ CLASS_SCHEMA, record_schema, out);
+
+ if (!compo.u.complex->generic->schema.oid)
+ {
+ /* OID wasn't a schema! Try record syntax instead. */
+ compo.u.complex->generic->schema.oid = (Odr_oid *)
+ yaz_string_to_oid_odr(yaz_oid_std(),
+ CLASS_RECSYN, record_schema, out);
+ }
+ }
+ if (!elementSetNames)
+ compo.u.complex->generic->elementSpec = 0;
+ else
+ {
+ compo.u.complex->generic->elementSpec = (Z_ElementSpec *)
+ odr_malloc(out, sizeof(Z_ElementSpec));
+ compo.u.complex->generic->elementSpec->which =
+ Z_ElementSpec_elementSetName;
+ compo.u.complex->generic->elementSpec->u.elementSetName =
+ elementSetNames->u.generic;
+ }
+ compo.u.complex->num_dbSpecific = 0;
+ compo.u.complex->dbSpecific = 0;
+
+ compo.u.complex->num_recordSyntax = 0;
+ compo.u.complex->recordSyntax = 0;
+ if (recordsyntax_size >= 2)
+ {
+ int i;
+ compo.u.complex->num_recordSyntax = recordsyntax_size;
+ compo.u.complex->recordSyntax = (Odr_oid **)
+ odr_malloc(out, recordsyntax_size * sizeof(Odr_oid*));
+ for (i = 0; i < recordsyntax_size; i++)
+ compo.u.complex->recordSyntax[i] =
+ yaz_string_to_oid_odr(yaz_oid_std(),
+ CLASS_RECSYN, recordsyntax_list[i], out);
+ }
+ }
+ else if (elementSetNames)
+ {
+ req->recordComposition = &compo;
+ compo.which = Z_RecordComp_simple;
+ compo.u.simple = elementSetNames;
+ }
+ send_apdu(apdu);
+ printf("Sent presentRequest (" ODR_INT_PRINTF "+" ODR_INT_PRINTF ").\n",
+ setno, nos);
+ return 2;
+}
+
+#if YAZ_HAVE_XML2
+static int send_SRW_presentRequest(const char *arg)
+{
+ char setstring[100];
+ Odr_int nos = 1;
+ Z_SRW_PDU *sr = srw_sr;
+
+ if (!sr)
+ return 0;
+ if (!parse_show_args(arg, setstring, &setno, &nos))
+ return 0;
+ sr->u.request->startRecord = odr_intdup(out, setno);
+ sru_maximumRecords = nos;
+ sr->u.request->maximumRecords = odr_intdup(out, nos);
+ if (record_schema)
+ sr->u.request->recordSchema = record_schema;
+ if (recordsyntax_size == 1 && !yaz_matchstr(recordsyntax_list[0], "xml"))
+ sr->u.request->recordPacking = "xml";
+ return send_srw(sr);
+}
+#endif
+
+static void close_session(void)
+{
+ if (conn)
+ cs_close(conn);
+ conn = 0;