+int cmd_cancel(char *arg)
+{
+ Z_APDU *apdu = zget_APDU(out, Z_APDU_triggerResourceControlRequest);
+ Z_TriggerResourceControlRequest *req =
+ apdu->u.triggerResourceControlRequest;
+ bool_t false = 0;
+
+ if (!session)
+ {
+ 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 = &false;
+
+ 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;
+ char *db = database;
+ oident attset;
+
+ req->num_databaseNames = 1;
+ req->databaseNames = &db;
+ attset.proto = protocol;
+ attset.oclass = CLASS_ATTSET;
+ attset.value = VAL_BIB1;
+ req->attributeSet = oid_getoidbyent(&attset);
+ req->termListAndStartPoint = p_query_scan(out, string);
+ req->numberOfTermsRequested = #
+ req->preferredPositionInResponse = &pp;
+ 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;
+
+ printf("SCAN: %d entries, position=%d\n", *res->numberOfEntriesReturned,
+ *res->positionOfTerm);
+ if (*res->scanStatus != Z_Scan_success)
+ printf("Scan returned code %d\n", *res->scanStatus);
+ if (!res->entries)
+ return;
+ if (res->entries->which == Z_ListEntries_entries)
+ {
+ Z_Entries *ent = res->entries->u.entries;
+
+ for (i = 0; i < ent->num_entries; i++)
+ if (ent->entries[i]->which == Z_Entry_termInfo)
+ {
+ printf("%c ", i + 1 == *res->positionOfTerm ? '*' : ' ');
+ display_term(ent->entries[i]->u.termInfo);
+ }
+ else
+ display_diagrec(ent->entries[i]->u.surrogateDiagnostic);
+ }
+ else
+ display_diagrec(res->entries->u.nonSurrogateDiagnostics->diagRecs[0]);
+}
+
+int cmd_scan(char *arg)
+{
+ if (!session)
+ {
+ 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;
+ }
+ if (!strcmp(arg, "sutrs"))
+ {
+ printf("Preferred format is SUTRS.\n");
+ recordsyntax = VAL_SUTRS;
+ return 1;
+ }
+ else if (!strcmp(arg, "usmarc"))
+ {
+ printf("Preferred format is USMARC\n");
+ recordsyntax = VAL_USMARC;
+ return 1;
+ }
+ else if (!strcmp(arg, "danmarc"))
+ {
+ printf("Preferred format is DANMARC\n");
+ recordsyntax = VAL_DANMARC;
+ return 1;
+ }
+ else if (!strcmp(arg, "grs1"))
+ {
+ printf("Preferred format is GRS1\n");
+ recordsyntax = VAL_GRS1;
+ return 1;
+ }
+ else if (!strcmp(arg, "explain"))
+ {
+ printf("Preferred format is Explain\n");
+ recordsyntax = VAL_EXPLAIN;
+ return 1;
+ }
+ else
+ {
+ printf("Specify one of {sutrs,usmarc,danmarc,grs1}.\n");
+ return 0;
+ }
+}
+
+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];
+ oid_value v;
+
+ if (!arg || !*arg)
+ {
+ printf("Usage: attributeset <setname>\n");
+ return 0;
+ }
+ sscanf(arg, "%s", what);
+ if ((v = oid_getvalbyname(what)) == VAL_NONE)
+ {
+ printf("Unknown attribute set name\n");
+ return 0;
+ }
+ attributeset = v;
+ return 1;
+}
+
+int cmd_close(char *arg)
+{
+ Z_APDU *apdu = zget_APDU(out, Z_APDU_close);
+ Z_Close *req = apdu->u.close;
+
+ *req->closeReason = Z_Close_finished;
+ send_apdu(apdu);
+ printf("Sent close request.\n");
+ sent_close = 1;
+ return 2;
+}
+