+ return 0;
+}
+
+int cmd_cancel(char *arg)
+{
+ Z_APDU *apdu = zget_APDU(out, Z_APDU_triggerResourceControlRequest);
+ Z_TriggerResourceControlRequest *req =
+ apdu->u.triggerResourceControlRequest;
+ bool_t rfalse = 0;
+
+ if (!conn)
+ {
+ printf("Session not initialized yet\n");
+ return 0;
+ }
+ if (!ODR_MASK_GET(session->options, Z_Options_triggerResourceCtrl))
+ {
+ printf("Target doesn't support cancel (trigger resource ctrl)\n");
+ return 0;
+ }
+ *req->requestedAction = Z_TriggerResourceCtrl_cancel;
+ req->resultSetWanted = &rfalse;
+
+ send_apdu(apdu);
+ printf("Sent cancel request\n");
+ return 2;
+}
+
+int send_scanrequest(char *string, int pp, int num)
+{
+ Z_APDU *apdu = zget_APDU(out, Z_APDU_scanRequest);
+ Z_ScanRequest *req = apdu->u.scanRequest;
+
+ if (!(req->termListAndStartPoint =
+ p_query_scan(out, protocol, &req->attributeSet, string)))
+ {
+ printf("Prefix query error\n");
+ return -1;
+ }
+ req->referenceId = set_refid (out);
+ req->num_databaseNames = num_databaseNames;
+ req->databaseNames = databaseNames;
+ req->numberOfTermsRequested = #
+ req->preferredPositionInResponse = &pp;
+ send_apdu(apdu);
+ return 2;
+}
+
+int send_sortrequest(char *arg, int newset)
+{
+ Z_APDU *apdu = zget_APDU(out, Z_APDU_sortRequest);
+ Z_SortRequest *req = apdu->u.sortRequest;
+ Z_SortKeySpecList *sksl = (Z_SortKeySpecList *)odr_malloc (out, sizeof(*sksl));
+ char setstring[32];
+ char sort_string[32], sort_flags[32];
+ int off;
+ int oid[OID_SIZE];
+ oident bib1;
+
+ if (setnumber >= 0)
+ sprintf (setstring, "%d", setnumber);
+ else
+ sprintf (setstring, "default");
+
+ req->referenceId = set_refid (out);
+
+#ifdef ASN_COMPILED
+ req->num_inputResultSetNames = 1;
+ req->inputResultSetNames = (Z_InternationalString **)
+ odr_malloc (out, sizeof(*req->inputResultSetNames));
+ req->inputResultSetNames[0] = (char *)
+ odr_malloc (out, strlen(setstring)+1);
+ strcpy (req->inputResultSetNames[0], setstring);
+#else
+ req->inputResultSetNames =
+ (Z_StringList *)odr_malloc (out, sizeof(*req->inputResultSetNames));
+ req->inputResultSetNames->num_strings = 1;
+ req->inputResultSetNames->strings =
+ (char **)odr_malloc (out, sizeof(*req->inputResultSetNames->strings));
+ req->inputResultSetNames->strings[0] =
+ (char *)odr_malloc (out, strlen(setstring)+1);
+ strcpy (req->inputResultSetNames->strings[0], setstring);
+#endif
+
+ if (newset && setnumber >= 0)
+ sprintf (setstring, "%d", ++setnumber);
+
+ req->sortedResultSetName = (char *)odr_malloc (out, strlen(setstring)+1);
+ strcpy (req->sortedResultSetName, setstring);
+
+ req->sortSequence = sksl;
+ sksl->num_specs = 0;
+ sksl->specs = (Z_SortKeySpec **)odr_malloc (out, sizeof(sksl->specs) * 20);
+
+ bib1.proto = protocol;
+ bib1.oclass = CLASS_ATTSET;
+ bib1.value = VAL_BIB1;
+ while ((sscanf (arg, "%31s %31s%n", sort_string, sort_flags, &off)) == 2
+ && off > 1)
+ {
+ int i;
+ char *sort_string_sep;
+ Z_SortKeySpec *sks = (Z_SortKeySpec *)odr_malloc (out, sizeof(*sks));
+ Z_SortKey *sk = (Z_SortKey *)odr_malloc (out, sizeof(*sk));
+
+ arg += off;
+ sksl->specs[sksl->num_specs++] = sks;
+ sks->sortElement = (Z_SortElement *)odr_malloc (out, sizeof(*sks->sortElement));
+ sks->sortElement->which = Z_SortElement_generic;
+ sks->sortElement->u.generic = sk;
+
+ if ((sort_string_sep = strchr (sort_string, '=')))
+ {
+ Z_AttributeElement *el = (Z_AttributeElement *)odr_malloc (out, sizeof(*el));
+ sk->which = Z_SortKey_sortAttributes;
+ sk->u.sortAttributes =
+ (Z_SortAttributes *)odr_malloc (out, sizeof(*sk->u.sortAttributes));
+ sk->u.sortAttributes->id = oid_ent_to_oid(&bib1, oid);
+ sk->u.sortAttributes->list =
+ (Z_AttributeList *)odr_malloc (out, sizeof(*sk->u.sortAttributes->list));
+ sk->u.sortAttributes->list->num_attributes = 1;
+ sk->u.sortAttributes->list->attributes =
+ (Z_AttributeElement **)odr_malloc (out,
+ sizeof(*sk->u.sortAttributes->list->attributes));
+ sk->u.sortAttributes->list->attributes[0] = el;
+ el->attributeSet = 0;
+ el->attributeType = (int *)odr_malloc (out, sizeof(*el->attributeType));
+ *el->attributeType = atoi (sort_string);
+ el->which = Z_AttributeValue_numeric;
+ el->value.numeric = (int *)odr_malloc (out, sizeof(*el->value.numeric));
+ *el->value.numeric = atoi (sort_string_sep + 1);
+ }
+ else
+ {
+ sk->which = Z_SortKey_sortField;
+ sk->u.sortField = (char *)odr_malloc (out, strlen(sort_string)+1);
+ strcpy (sk->u.sortField, sort_string);
+ }
+ sks->sortRelation = (int *)odr_malloc (out, sizeof(*sks->sortRelation));
+ *sks->sortRelation = Z_SortRelation_ascending;
+ sks->caseSensitivity = (int *)odr_malloc (out, sizeof(*sks->caseSensitivity));
+ *sks->caseSensitivity = Z_SortCase_caseSensitive;
+
+#ifdef ASN_COMPILED
+ sks->which = Z_SortKeySpec_null;
+ sks->u.null = odr_nullval ();
+#else
+ sks->missingValueAction = NULL;
+#endif
+
+ for (i = 0; sort_flags[i]; i++)
+ {
+ switch (sort_flags[i])
+ {
+ case 'a':
+ case 'A':
+ case '>':
+ *sks->sortRelation = Z_SortRelation_descending;
+ break;
+ case 'd':
+ case 'D':
+ case '<':
+ *sks->sortRelation = Z_SortRelation_ascending;
+ break;
+ case 'i':
+ case 'I':
+ *sks->caseSensitivity = Z_SortCase_caseInsensitive;
+ break;
+ case 'S':
+ case 's':
+ *sks->caseSensitivity = Z_SortCase_caseSensitive;
+ break;
+ }
+ }
+ }
+ if (!sksl->num_specs)
+ {
+ printf ("Missing sort specifications\n");
+ return -1;
+ }
+ send_apdu(apdu);
+ return 2;
+}
+
+void display_term(Z_TermInfo *t)
+{
+ if (t->term->which == Z_Term_general)
+ {
+ printf("%.*s (%d)\n", t->term->u.general->len, t->term->u.general->buf,
+ t->globalOccurrences ? *t->globalOccurrences : -1);
+ sprintf(last_scan, "%.*s", t->term->u.general->len,
+ t->term->u.general->buf);
+ }
+ else
+ printf("Term type not general.\n");
+}
+
+void process_scanResponse(Z_ScanResponse *res)
+{
+ int i;
+ Z_Entry **entries = NULL;
+ int num_entries = 0;
+
+ printf("Received ScanResponse\n");
+ print_refid (res->referenceId);
+ printf("%d entries", *res->numberOfEntriesReturned);
+ if (res->positionOfTerm)
+ printf (", position=%d", *res->positionOfTerm);
+ printf ("\n");
+ if (*res->scanStatus != Z_Scan_success)
+ printf("Scan returned code %d\n", *res->scanStatus);
+ if (!res->entries)
+ return;
+#ifdef ASN_COMPILED
+ if ((entries = res->entries->entries))
+ num_entries = res->entries->num_entries;
+#else
+ if (res->entries->which == Z_ListEntries_entries)
+ {
+ entries = res->entries->u.entries->entries;
+ num_entries = res->entries->u.entries->num_entries;
+ }
+#endif
+ for (i = 0; i < num_entries; i++)
+ if (entries[i]->which == Z_Entry_termInfo)
+ {
+ printf("%c ", i + 1 == *res->positionOfTerm ? '*' : ' ');
+ display_term(entries[i]->u.termInfo);
+ }
+ else
+ display_diagrecs(&entries[i]->u.surrogateDiagnostic, 1);
+#ifdef ASN_COMPILED
+ if (res->entries->nonsurrogateDiagnostics)
+ display_diagrecs (res->entries->nonsurrogateDiagnostics,
+ res->entries->num_nonsurrogateDiagnostics);
+#else
+ if (res->entries->which == Z_ListEntries_nonSurrogateDiagnostics)
+ display_diagrecs(&res->entries->
+ u.nonSurrogateDiagnostics->diagRecs[0], 1);
+#endif
+}
+
+void process_sortResponse(Z_SortResponse *res)
+{
+ printf("Received SortResponse: status=");
+ switch (*res->sortStatus)
+ {
+ case Z_SortStatus_success:
+ printf ("success"); break;
+ case Z_SortStatus_partial_1:
+ printf ("partial"); break;
+ case Z_SortStatus_failure:
+ printf ("failure"); break;
+ default:
+ printf ("unknown (%d)", *res->sortStatus);
+ }
+ printf ("\n");
+ print_refid (res->referenceId);
+#ifdef ASN_COMPILED
+ if (res->diagnostics)
+ display_diagrecs(res->diagnostics,
+ res->num_diagnostics);
+#else
+ if (res->diagnostics)
+ display_diagrecs(res->diagnostics->diagRecs,
+ res->diagnostics->num_diagRecs);
+#endif
+}
+
+int cmd_sort_generic(char *arg, int newset)
+{
+ if (!conn)
+ {
+ printf("Session not initialized yet\n");
+ return 0;
+ }
+ if (!ODR_MASK_GET(session->options, Z_Options_sort))
+ {
+ printf("Target doesn't support sort\n");
+ return 0;
+ }
+ if (*arg)
+ {
+ if (send_sortrequest(arg, newset) < 0)
+ return 0;
+ return 2;
+ }
+ return 0;
+}
+
+int cmd_sort(char *arg)
+{
+ return cmd_sort_generic (arg, 0);
+}
+
+int cmd_sort_newset (char *arg)
+{
+ return cmd_sort_generic (arg, 1);
+}
+
+int cmd_scan(char *arg)
+{
+ if (!conn)
+ {
+ printf("Session not initialized yet\n");
+ return 0;
+ }
+ if (!ODR_MASK_GET(session->options, Z_Options_scan))
+ {
+ printf("Target doesn't support scan\n");
+ return 0;
+ }
+ if (*arg)
+ {
+ if (send_scanrequest(arg, 5, 20) < 0)
+ return 0;
+ }
+ else
+ if (send_scanrequest(last_scan, 1, 20) < 0)
+ return 0;
+ return 2;
+}
+
+int cmd_format(char *arg)
+{
+ if (!arg || !*arg)
+ {
+ printf("Usage: format <recordsyntax>\n");
+ return 0;
+ }
+ recordsyntax = oid_getvalbyname (arg);
+ if (recordsyntax == VAL_NONE)
+ {
+ printf ("unknown record syntax\n");
+ return 0;
+ }
+ return 1;
+}
+
+int cmd_elements(char *arg)
+{
+ static Z_ElementSetNames esn;
+ static char what[100];
+
+ if (!arg || !*arg)
+ {
+ printf("Usage: elements <esn>\n");
+ return 0;
+ }
+ strcpy(what, arg);
+ esn.which = Z_ElementSetNames_generic;
+ esn.u.generic = what;
+ elementSetNames = &esn;
+ return 1;
+}
+
+int cmd_attributeset(char *arg)
+{
+ char what[100];
+
+ if (!arg || !*arg)
+ {
+ printf("Usage: attributeset <setname>\n");
+ return 0;
+ }
+ sscanf(arg, "%s", what);
+ if (p_query_attset (what))
+ {
+ printf("Unknown attribute set name\n");
+ return 0;
+ }
+ return 1;
+}
+
+int cmd_querytype (char *arg)
+{
+ if (!strcmp (arg, "ccl"))
+ queryType = QueryType_CCL;
+ else if (!strcmp (arg, "prefix"))
+ queryType = QueryType_Prefix;
+#if CCL2RPN
+ else if (!strcmp (arg, "ccl2rpn") || !strcmp (arg, "cclrpn"))
+ queryType = QueryType_CCL2RPN;
+#endif
+ else
+ {
+ printf ("Querytype must be one of:\n");
+ printf (" prefix - Prefix query\n");
+ printf (" ccl - CCL query\n");
+#if CCL2RPN
+ printf (" ccl2rpn - CCL query converted to RPN\n");
+#endif
+ return 0;
+ }
+ return 1;
+}
+
+int cmd_refid (char *arg)
+{
+ xfree (refid);
+ refid = NULL;
+ if (*arg)
+ {
+ refid = (char *) xmalloc (strlen(arg)+1);
+ strcpy (refid, arg);
+ }
+ return 1;
+}
+
+int cmd_close(char *arg)
+{
+ Z_APDU *apdu;
+ Z_Close *req;
+ if (!conn)
+ return 0;
+
+ apdu = zget_APDU(out, Z_APDU_close);
+ req = apdu->u.close;
+ *req->closeReason = Z_Close_finished;
+ send_apdu(apdu);
+ printf("Sent close request.\n");
+ sent_close = 1;
+ return 2;