Added second half of close-handshake
[yaz-moved-to-github.git] / client / client.c
index 5dbe876..e06ad37 100644 (file)
@@ -4,7 +4,46 @@
  * Sebastian Hammer, Adam Dickmeiss
  *
  * $Log: client.c,v $
- * Revision 1.7  1995-06-02 09:50:09  quinn
+ * Revision 1.20  1995-08-29 14:24:13  quinn
+ * Added second half of close-handshake
+ *
+ * Revision 1.19  1995/08/29  11:17:28  quinn
+ * Added code to receive close
+ *
+ * Revision 1.18  1995/08/28  12:21:27  quinn
+ * Client can now ask for simple element set names.
+ *
+ * Revision 1.17  1995/08/17  12:45:02  quinn
+ * Fixed minor problems with GRS-1. Added support in c&s.
+ *
+ * Revision 1.16  1995/08/15  12:00:04  quinn
+ * Updated External
+ *
+ * Revision 1.15  1995/06/22  09:28:03  quinn
+ * Fixed bug in SUTRS processing.
+ *
+ * Revision 1.14  1995/06/19  12:37:41  quinn
+ * Added BER dumper.
+ *
+ * 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
@@ -37,6 +76,9 @@
 #include <stdlib.h>
 #include <sys/time.h>
 #include <assert.h>
+#ifdef _AIX
+#include <sys/select.h>
+#endif
 
 #include <comstack.h>
 #include <tcpip.h>
@@ -45,8 +87,8 @@
 #endif
 
 #include <proto.h>
-
 #include <marcdisp.h>
+#include <diagbib1.h>
 
 #ifdef RPN_QUERY
 #ifdef PREFIX_QUERY
@@ -56,8 +98,6 @@
 #endif
 #endif
 
-#include "../version.h"
-
 #define C_PROMPT "Z> "
 
 static ODR out, in, print;              /* encoding and decoding streams */
@@ -68,8 +108,15 @@ static int setnumber = 0;               /* current result set number */
 static int smallSetUpperBound = 0;
 static int largeSetLowerBound = 1;
 static int mediumSetPresentNumber = 0;
+static Z_ElementSetNames *elementSetNames = 0; 
 static int setno = 1;                   /* current set offset */
 static int protocol = PROTO_Z3950;      /* current app protocol */
+static int recordsyntax = VAL_USMARC;
+static int sent_close = 0;
+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 */
@@ -104,9 +151,13 @@ 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, Z_ProtocolVersion_1);
     ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_2);
+    ODR_MASK_SET(req->protocolVersion, Z_ProtocolVersion_3);
 
     req->idAuthentication = auth;
 
@@ -116,6 +167,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
@@ -129,13 +184,13 @@ static int process_initResponse(Z_InitResponse *res)
     if (res->userInformationField)
     {
        printf("UserInformationfield:\n");
-       if (!odr_external(print, (Odr_external**)&res-> userInformationField,
+       if (!z_External(print, (Z_External**)&res-> userInformationField,
            0))
        {
            odr_perror(print, "Printing userinfo\n");
            odr_reset(print);
        }
-       if (res->userInformationField->which == ODR_EXTERNAL_octet)
+       if (res->userInformationField->which == Z_External_octet)
        {
            printf("Guessing visiblestring:\n");
            printf("'%s'\n", res->userInformationField->u. octet_aligned->buf);
@@ -227,14 +282,46 @@ int cmd_authentication(char *arg)
 
 /* SEARCH SERVICE ------------------------------ */
 
+void display_grs1(Z_GenericRecord *r, int level)
+{
+    int i;
+
+    if (!r)
+       return;
+    for (i = 0; i < r->num_elements; i++)
+    {
+       Z_TaggedElement *t;
+
+       printf("%*s", level * 4, "");
+       t = r->elements[i];
+       printf("(");
+       if (t->tagType)
+           printf("%d,", *t->tagType);
+       else
+           printf("?,");
+       if (t->tagValue->which == Z_StringOrNumeric_numeric)
+           printf("%d) ", *t->tagValue->u.numeric);
+       else
+           printf("%s) ", t->tagValue->u.string);
+       if (t->content->which == Z_ElementData_subtree)
+       {
+           printf("\n");
+           display_grs1(t->content->u.subtree, level+1);
+       }
+       else if (t->content->which == Z_ElementData_string)
+           printf("%s\n", t->content->u.string);
+       else
+           printf("??????\n");
+    }
+}
+
 void display_record(Z_DatabaseRecord *p)
 {
-    Odr_external *r = (Odr_external*) p;
+    Z_External *r = (Z_External*) p;
+    oident *ent = oid_getentbyoid(r->direct_reference);
 
     if (r->direct_reference)
     {
-       oident *ent = oid_getentbyoid(r->direct_reference);
-
        printf("Record type: ");
        if (ent)
            printf("%s\n", ent->desc);
@@ -244,41 +331,64 @@ void display_record(Z_DatabaseRecord *p)
            odr_reset(print);
        }
     }
-#if 1
-    if (r->which == ODR_EXTERNAL_octet && p->u.octet_aligned->len)
-    {
-#if 1
+    if (r->which == Z_External_octet && p->u.octet_aligned->len)
        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 if (ent->value == VAL_SUTRS)
+    {
+       if (r->which != Z_External_sutrs)
+       {
+           printf("Expecting single SUTRS type for SUTRS.\n");
+           return;
+       }
+       printf("%.*s", r->u.sutrs->len, r->u.sutrs->buf);
     }
-    else
+    else if (ent->value == VAL_GRS1)
+    {
+       if (r->which != Z_External_grs1)
+       {
+           printf("Expecting single GRS type for GRS.\n");
+           return;
+       }
+       display_grs1(r->u.grs1, 0);
+    }
+    else 
     {
        printf("Unknown record representation.\n");
-       if (!odr_external(print, &r, 0))
+       if (!z_External(print, &r, 0))
        {
            odr_perror(print, "Printing external");
            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)) ||
+    printf("Diagnostic message from database:\n");
+#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("    [%d] %s", *r->condition, diagbib1_str(*r->condition));
+    if (r->addinfo && *r->addinfo)
+       printf(" -- %s\n", r->addinfo);
+    else
+       printf("\n");
 }
 
 static void display_nameplusrecord(Z_NamePlusRecord *p)
@@ -356,6 +466,18 @@ static int send_searchRequest(char *arg)
     *req->smallSetUpperBound = smallSetUpperBound;
     *req->largeSetLowerBound = largeSetLowerBound;
     *req->mediumSetPresentNumber = mediumSetPresentNumber;
+    if (smallSetUpperBound > 0 || (largeSetLowerBound > 1 &&
+       mediumSetPresentNumber > 0))
+    {
+       oident prefsyn;
+
+       prefsyn.proto = protocol;
+       prefsyn.class = CLASS_RECSYN;
+       prefsyn.value = recordsyntax;
+       req->preferredRecordSyntax = odr_oiddup(out, oid_getoidbyent(&prefsyn));
+       req->smallSetElementSetNames =
+           req->mediumSetElementSetNames = elementSetNames;
+    }
     req->num_databaseNames = 1;
     req->databaseNames = &databaseNames;
 
@@ -394,7 +516,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");
@@ -486,6 +608,8 @@ static int send_presentRequest(char *arg)
 {
     Z_APDU *apdu = zget_APDU(out, Z_APDU_presentRequest);
     Z_PresentRequest *req = apdu->u.presentRequest;
+    Z_RecordComposition compo;
+    oident prefsyn;
     int nos = 1;
     char *p;
     char setstring[100];
@@ -505,11 +629,53 @@ static int send_presentRequest(char *arg)
     }
     req->resultSetStartPoint = &setno;
     req->numberOfRecordsRequested = &nos;
+    prefsyn.proto = protocol;
+    prefsyn.class = CLASS_RECSYN;
+    prefsyn.value = recordsyntax;
+    req->preferredRecordSyntax = oid_getoidbyent(&prefsyn);
+    if (elementSetNames)
+    {
+       req->recordComposition = &compo;
+       compo.which = Z_RecordComp_simple;
+       compo.u.simple = elementSetNames;
+    }
     send_apdu(apdu);
     printf("Sent presentRequest (%d+%d).\n", setno, nos);
     return 2;
 }
 
+void process_close(Z_Close *req)
+{
+    Z_APDU *apdu = zget_APDU(out, Z_APDU_close);
+    Z_Close *res = apdu->u.close;
+
+    static char *reasons[] =
+    {
+       "finished",
+       "shutdown",
+       "systemProblem",
+       "costLimit",
+       "resources",
+       "securityViolation",
+       "protocolError",
+       "lackOfActivity",
+       "peerAbort",
+       "unspecified"
+    };
+
+    printf("Reason: %s, message: %s\n", reasons[*req->closeReason],
+       req->diagnosticInformation ? req->diagnosticInformation : "NULL");
+    if (sent_close)
+    {
+       printf("Goodbye.\n");
+       exit(0);
+    }
+    *res->closeReason = Z_Close_finished;
+    send_apdu(apdu);
+    printf("Sent response.\n");
+    sent_close = 1;
+}
+
 static int cmd_show(char *arg)
 {
     if (!send_presentRequest(arg))
@@ -523,6 +689,181 @@ 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;
+}
+
+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
+    {
+       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_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;
+}
+
 static void initialize(void)
 {
 #ifdef RPN_QUERY
@@ -565,12 +906,17 @@ 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, ""},
+       {"format", cmd_format, "<recordsyntax>"},
+       {"elements", cmd_elements, "<elementSetName>"},
+       {"close", cmd_close, ""},
        {0,0}
     };
     char *netbuffer= 0;
@@ -602,11 +948,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)))
                {
@@ -643,6 +990,9 @@ static int client(void)
                if (!z_APDU(in, &apdu, 0))
                {
                    odr_perror(in, "Decoding incoming APDU");
+                   fprintf(stderr, "Packet dump:\n---------\n");
+                   odr_dumpBER(stderr, netbuffer, res);
+                   fprintf(stderr, "---------\n");
                    exit(1);
                }
 #if 0
@@ -661,6 +1011,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 +=
@@ -670,6 +1023,10 @@ static int client(void)
                        else
                            printf("No records.\n");
                        break;
+                   case Z_APDU_close:
+                       printf("Target has closed the association.\n");
+                       process_close(apdu->u.close);
+                       break;
                    default:
                        printf("Received unknown APDU type (%d).\n", 
                            apdu->which);