*** empty log message ***
[yaz-moved-to-github.git] / client / client.c
index 36843ed..70455bb 100644 (file)
@@ -4,7 +4,40 @@
  * Sebastian Hammer, Adam Dickmeiss
  *
  * $Log: client.c,v $
- * Revision 1.2  1995-05-22 14:56:40  quinn
+ * Revision 1.13  1995-06-16 10:29:11  quinn
+ * *** empty log message ***
+ *
+ * Revision 1.12  1995/06/15  07:44:57  quinn
+ * Moving to v3.
+ *
+ * Revision 1.11  1995/06/14  15:26:40  quinn
+ * *** empty log message ***
+ *
+ * Revision 1.10  1995/06/06  14:56:58  quinn
+ * Better diagnostics.
+ *
+ * Revision 1.9  1995/06/06  08:15:19  quinn
+ * Cosmetic.
+ *
+ * Revision 1.8  1995/06/05  10:52:22  quinn
+ * Added SCAN.
+ *
+ * Revision 1.7  1995/06/02  09:50:09  quinn
+ * Smallish.
+ *
+ * Revision 1.6  1995/05/31  08:29:21  quinn
+ * Nothing significant.
+ *
+ * Revision 1.5  1995/05/29  08:10:47  quinn
+ * Moved oid.c to util.
+ *
+ * Revision 1.4  1995/05/22  15:30:13  adam
+ * Client uses prefix query notation.
+ *
+ * Revision 1.3  1995/05/22  15:06:53  quinn
+ * *** empty log message ***
+ *
+ * Revision 1.2  1995/05/22  14:56:40  quinn
  * *** empty log message ***
  *
  * Revision 1.1  1995/05/22  11:30:31  quinn
@@ -22,6 +55,9 @@
 #include <stdlib.h>
 #include <sys/time.h>
 #include <assert.h>
+#ifdef _AIX
+#include <sys/select.h>
+#endif
 
 #include <comstack.h>
 #include <tcpip.h>
 #endif
 
 #include <proto.h>
-#include <oid.h>
-
 #include <marcdisp.h>
+
+#ifdef RPN_QUERY
+#ifdef PREFIX_QUERY
+#include <pquery.h>
+#else
 #include <yaz-ccl.h>
-#include "../version.h"
+#endif
+#endif
 
 #define C_PROMPT "Z> "
 
@@ -48,7 +88,15 @@ static int largeSetLowerBound = 1;
 static int mediumSetPresentNumber = 0;
 static int setno = 1;                   /* current set offset */
 static int protocol = PROTO_Z3950;      /* current app protocol */
+static ODR_MEM session_mem;                /* memory handle for init-response */
+static Z_InitResponse *session = 0;        /* session parameters */
+static char last_scan[512] = "0";
+static char last_cmd[100] = "?";
+#ifdef RPN_QUERY
+#ifndef PREFIX_QUERY
 static CCL_bibset bibset;               /* CCL bibset handle */
+#endif
+#endif
 
 static void send_apdu(Z_APDU *a)
 {
@@ -78,10 +126,12 @@ static void send_initRequest()
 
     ODR_MASK_SET(req->options, Z_Options_search);
     ODR_MASK_SET(req->options, Z_Options_present);
+    ODR_MASK_SET(req->options, Z_Options_namedResultSets);
+    ODR_MASK_SET(req->options, Z_Options_triggerResourceCtrl);
+    ODR_MASK_SET(req->options, Z_Options_scan);
 
-    ODR_MASK_SET(req->protocolVersion, 0);
-    ODR_MASK_SET(req->protocolVersion, 1);
-    ODR_MASK_SET(req->protocolVersion, 2);
+    ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_1);
+    ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_2);
 
     req->idAuthentication = auth;
 
@@ -91,6 +141,10 @@ static void send_initRequest()
 
 static int process_initResponse(Z_InitResponse *res)
 {
+    /* save session parameters for later use */
+    session_mem = odr_extract_mem(in);
+    session = res;
+
     if (!*res->result)
        printf("Connection rejected by target.\n");
     else
@@ -163,7 +217,7 @@ int cmd_open(char *arg)
        fprintf(stderr, "Bad type: %s\n", type);
        return 0;
     }
-    if (!(conn = cs_create(t, 1, CS_Z3950)))
+    if (!(conn = cs_create(t, 1, protocol)))
     {
        perror("cs_create");
        return 0;
@@ -177,7 +231,7 @@ int cmd_open(char *arg)
        conn = 0;
        return 0;
     }
-    printf("Ok!\n");
+    printf("Ok.\n");
     send_initRequest();
     return 2;
 }
@@ -219,18 +273,9 @@ void display_record(Z_DatabaseRecord *p)
            odr_reset(print);
        }
     }
-#if 1
     if (r->which == ODR_EXTERNAL_octet && p->u.octet_aligned->len)
     {
-#if 1
        marc_display ((char*)p->u.octet_aligned->buf, stdout);
-#else
-       FILE *ofi = fopen("dump", "a");
-       assert(ofi);
-       fwrite(p->u.octet_aligned->buf, 1, p->u.octet_aligned->len, ofi);
-       fclose(ofi);
-       printf("dumped record\n");
-#endif
     }
     else
     {
@@ -241,19 +286,32 @@ void display_record(Z_DatabaseRecord *p)
            odr_reset(print);
        }
     }
-#endif
 }
 
 static void display_diagrec(Z_DiagRec *p)
 {
     oident *ent;
+#ifdef Z_95
+    Z_DefaultDiagFormat *r;
+#else
+    Z_DiagRec *r = p;
+#endif
 
     printf("Diagnostic message from database.\n");
-    if (!(ent = oid_getentbyoid(p->diagnosticSetId)) ||
+#ifdef Z_95
+    if (p->which != Z_DiagRec_defaultFormat)
+    {
+       printf("Diagnostic record not in default format.\n");
+       return;
+    }
+    else
+       r = p->u.defaultFormat;
+#endif
+    if (!(ent = oid_getentbyoid(r->diagnosticSetId)) ||
        ent->class != CLASS_DIAGSET || ent->value != VAL_BIB1)
        printf("Missing or unknown diagset\n");
-    printf("Error condition: %d", *p->condition);
-    printf(" -- %s\n", p->addinfo ? p->addinfo : "");
+    printf("Error condition: %d", *r->condition);
+    printf(" -- %s\n", r->addinfo ? r->addinfo : "");
 }
 
 static void display_nameplusrecord(Z_NamePlusRecord *p)
@@ -286,16 +344,22 @@ static int send_searchRequest(char *arg)
     Z_SearchRequest *req = apdu->u.searchRequest;
     char *databaseNames = database;
     Z_Query query;
+#ifdef RPN_QUERY
+#ifndef PREFIX_QUERY
+    struct ccl_rpn_node *rpn;
     int error, pos;
+#endif
+#endif
     char setstring[100];
+#ifdef RPN_QUERY
     Z_RPNQuery *RPNquery;
     oident bib1;
-    struct ccl_rpn_node *rpn;
-#ifndef RPN_QUERY
+#else
     Odr_oct ccl_query;
 #endif
 
 #ifdef RPN_QUERY
+#ifndef PREFIX_QUERY
     rpn = ccl_find_str(bibset, arg, &error, &pos);
     if (error)
     {
@@ -303,6 +367,7 @@ static int send_searchRequest(char *arg)
        return 0;
     }
 #endif
+#endif
 
     if (!strcmp(arg, "@big")) /* strictly for troublemaking */
     {
@@ -331,7 +396,17 @@ static int send_searchRequest(char *arg)
 
 #ifdef RPN_QUERY
     query.which = Z_Query_type_1;
+
+#ifndef PREFIX_QUERY
     assert((RPNquery = ccl_rpn_query(rpn)));
+#else
+    RPNquery = p_query_rpn (out, arg);
+    if (!RPNquery)
+    {
+        printf("Prefix query error\n");
+        return 0;
+    }
+#endif
     bib1.proto = protocol;
     bib1.class = CLASS_ATTSET;
     bib1.value = VAL_BIB1;
@@ -352,7 +427,7 @@ static int send_searchRequest(char *arg)
 
 static int process_searchResponse(Z_SearchResponse *res)
 {
-    if (res->searchStatus)
+    if (*res->searchStatus)
        printf("Search was a success.\n");
     else
        printf("Search was a bloomin' failure.\n");
@@ -435,7 +510,7 @@ static int cmd_setnames(char *arg)
        printf("Set numbering disabled.\n");
        setnumber = -1;
     }
-    return 2;
+    return 1;
 }
 
 /* PRESENT SERVICE ----------------------------- */
@@ -481,9 +556,121 @@ int cmd_quit(char *arg)
     exit(0);
 }
 
+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.class = CLASS_ATTSET;
+    attset.value = VAL_BIB1;
+    req->attributeSet = oid_getoidbyent(&attset);
+    req->termListAndStartPoint = p_query_scan(out, string);
+    req->numberOfTermsRequested = &num;
+    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;
+}
+
 static void initialize(void)
 {
+#ifdef RPN_QUERY
+#ifndef PREFIX_QUERY
     FILE *inf;
+#endif
+#endif
 
     if (!(out = odr_createmem(ODR_ENCODE)) ||
        !(in = odr_createmem(ODR_DECODE)) ||
@@ -494,6 +681,8 @@ static void initialize(void)
     }
     setvbuf(stdout, 0, _IONBF, 0);
 
+#ifdef RPN_QUERY
+#ifndef PREFIX_QUERY
     bibset = ccl_qual_mk (); 
     inf = fopen ("default.bib", "r");
     if (inf)
@@ -501,6 +690,8 @@ static void initialize(void)
        ccl_qual_file (bibset, inf);
        fclose (inf);
     }
+#endif
+#endif
 }
 
 static int client(void)
@@ -515,12 +706,14 @@ static int client(void)
        {"find", cmd_find, "<CCL-QUERY>"},
        {"base", cmd_base, "<BASE-NAME>"},
        {"show", cmd_show, "<REC#>['+'<#RECS>]"},
+       {"scan", cmd_scan, "<TERM>"},
        {"authentication", cmd_authentication, "<ACCTSTRING>"},
        {"lslb", cmd_lslb, "<largeSetLowerBound>"},
        {"ssub", cmd_ssub, "<smallSetUpperBound>"},
        {"mspn", cmd_mspn, "<mediumSetPresentNumber>"},
        {"status", cmd_status, ""},
        {"setnames", cmd_setnames, ""},
+       {"cancel", cmd_cancel, ""},
        {0,0}
     };
     char *netbuffer= 0;
@@ -552,11 +745,12 @@ static int client(void)
                break;
            if ((res = sscanf(line, "%s %[^;]", word, arg)) <= 0)
            {
-               printf(C_PROMPT);
-               continue;
+               strcpy(word, last_cmd);
+               *arg = '\0';
            }
-           if (res == 1)
+           else if (res == 1)
                *arg = 0;
+           strcpy(last_cmd, word);
            for (i = 0; cmd[i].cmd; i++)
                if (!strncmp(cmd[i].cmd, word, strlen(word)))
                {
@@ -611,6 +805,9 @@ static int client(void)
                    case Z_APDU_searchResponse:
                        process_searchResponse(apdu->u.searchResponse);
                        break;
+                   case Z_APDU_scanResponse:
+                       process_scanResponse(apdu->u.scanResponse);
+                       break;
                    case Z_APDU_presentResponse:
                        printf("Received presentResponse.\n");
                        setno +=