Remove --enable-module option
[yaz-moved-to-github.git] / client / client.c
index bf24830..0c67a87 100644 (file)
@@ -2,7 +2,7 @@
  * Copyright (c) 1995-2002, Index Data
  * See the file LICENSE for details.
  *
- * $Id: client.c,v 1.146 2002-03-06 01:22:41 adam Exp $
+ * $Id: client.c,v 1.156 2002-06-02 21:34:45 adam Exp $
  */
 
 #include <stdio.h>
 #include <yaz/marcdisp.h>
 #include <yaz/diagbib1.h>
 #include <yaz/otherinfo.h>
+#include <yaz/charneg.h>
 
 #include <yaz/pquery.h>
 #include <yaz/sortspec.h>
 
-#if YAZ_MODULE_ill
 #include <yaz/ill.h>
-#endif
 
-#if YAZ_MODULE_ccl
 #include <yaz/yaz-ccl.h>
-#endif
 
 #if HAVE_READLINE_READLINE_H
 #include <readline/readline.h>
@@ -72,6 +69,8 @@ static char ccl_fields[512] = "default.bib";
 static char* esPackageName = 0;
 static char* yazProxy = 0;
 static int kilobytes = 1024;
+static char* yazCharset = 0;
+static char* yazLang = 0;
 
 static char last_cmd[32] = "?";
 static FILE *marcdump = 0;
@@ -85,10 +84,14 @@ typedef enum {
 
 static QueryType queryType = QueryType_Prefix;
 
-#if YAZ_MODULE_ccl
 static CCL_bibset bibset;               /* CCL bibset handle */
-#endif
 
+#if HAVE_READLINE_COMPLETION_OVER
+
+#else
+/* readline doesn't have this var. Define it ourselves. */
+int rl_attempted_completion_over = 0;
+#endif
 
 /* set this one to 1, to avoid decode of unknown MARCs  */
 #define AVOID_MARC_DECODE 1
@@ -198,6 +201,23 @@ static void send_initRequest(const char* type_and_host)
         yaz_oi_set_string_oidval(&req->otherInfo, out, VAL_PROXY,
         1, type_and_host);
     
+    if (yazCharset || yazLang) {
+       Z_OtherInformation **p;
+       Z_OtherInformationUnit *p0;
+       
+       yaz_oi_APDU(apdu, &p);
+       
+       if (p0=yaz_oi_update(p, out, NULL, 0, 0)) {
+               ODR_MASK_SET(req->options, Z_Options_negotiationModel);
+               
+               p0->which = Z_OtherInfo_externallyDefinedInfo;
+               p0->information.externallyDefinedInfo =
+                       yaz_set_proposal_charneg(out,
+                               (const char**)&yazCharset, (yazCharset)?1:0,
+                               (const char**)&yazLang, (yazLang)?1:0, 1);
+       }
+    }
+    
     send_apdu(apdu);
     printf("Sent initrequest.\n");
 }
@@ -275,6 +295,24 @@ static int process_initResponse(Z_InitResponse *res)
     if (ODR_MASK_GET(res->options, Z_Options_queryType104))
         printf (" queryType104");
     printf ("\n");
+    
+    if (ODR_MASK_GET(res->options, Z_Options_negotiationModel)) {
+    
+       Z_CharSetandLanguageNegotiation *p =
+               yaz_get_charneg_record(res->otherInfo);
+       
+       if (p) {
+       
+               char *charset=NULL, *lang=NULL;
+               int selected;
+               
+               yaz_get_response_charneg(session_mem, p, &charset, &lang, &selected);
+               
+               printf("Accepted character set : `%s'\n", charset);
+               printf("Accepted code language : `%s'\n", lang);
+               printf("Accepted records in ...: %d\n", selected );
+       }
+    }
     fflush (stdout);
     return 0;
 }
@@ -398,6 +436,7 @@ int cmd_authentication(char *arg)
 }
 
 /* SEARCH SERVICE ------------------------------ */
+static void display_record(Z_External *r);
 
 static void display_variant(Z_Variant *v, int level)
 {
@@ -419,7 +458,9 @@ static void display_grs1(Z_GenericRecord *r, int level)
     int i;
 
     if (!r)
+    {
         return;
+    }
     for (i = 0; i < r->num_elements; i++)
     {
         Z_TaggedElement *t;
@@ -437,8 +478,13 @@ static void display_grs1(Z_GenericRecord *r, int level)
             printf("%s) ", t->tagValue->u.string);
         if (t->content->which == Z_ElementData_subtree)
         {
-            printf("\n");
-            display_grs1(t->content->u.subtree, level+1);
+            if (!t->content->u.subtree)
+                printf (" (no subtree)\n");
+            else
+            {
+                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);
@@ -465,8 +511,15 @@ static void display_grs1(Z_GenericRecord *r, int level)
             printf("[Element empty]\n");
         else if (t->content->which == Z_ElementData_elementNotThere)
             printf("[Element not there]\n");
+        else if (t->content->which == Z_ElementData_date)
+            printf("Date: %s\n", t->content->u.date);
+        else if (t->content->which == Z_ElementData_ext)
+        {
+            printf ("External\n");
+            display_record (t->content->u.ext);
+        } 
         else
-            printf("??????\n");
+            printf("? type = %d\n",t->content->which);
         if (t->appliedVariant)
             display_variant(t->appliedVariant, level+1);
         if (t->metaData && t->metaData->supportedVariants)
@@ -543,6 +596,8 @@ static void display_record(Z_External *r)
             r->which = type->what;
         }
     }
+    if (ent && ent->oclass != CLASS_RECSYN)
+        return;
     if (ent && ent->value == VAL_SOIF)
         print_record((const unsigned char *) r->u.octet_aligned->buf,
                      r->u.octet_aligned->len);
@@ -729,15 +784,12 @@ static int send_searchRequest(char *arg)
     Z_SearchRequest *req = apdu->u.searchRequest;
     Z_Query query;
     int oid[OID_SIZE];
-#if YAZ_MODULE_ccl
     struct ccl_rpn_node *rpn = NULL;
     int error, pos;
-#endif
     char setstring[100];
     Z_RPNQuery *RPNquery;
     Odr_oct ccl_query;
 
-#if YAZ_MODULE_ccl
     if (queryType == QueryType_CCL2RPN)
     {
         rpn = ccl_find_str(bibset, arg, &error, &pos);
@@ -747,7 +799,6 @@ static int send_searchRequest(char *arg)
             return 0;
         }
     }
-#endif
     req->referenceId = set_refid (out);
     if (!strcmp(arg, "@big")) /* strictly for troublemaking */
     {
@@ -805,7 +856,6 @@ static int send_searchRequest(char *arg)
         ccl_query.buf = (unsigned char*) arg;
         ccl_query.len = strlen(arg);
         break;
-#if YAZ_MODULE_ccl
     case QueryType_CCL2RPN:
         query.which = Z_Query_type_1;
         RPNquery = ccl_rpn_query(out, rpn);
@@ -817,7 +867,6 @@ static int send_searchRequest(char *arg)
         query.u.type_1 = RPNquery;
         ccl_rpn_delete (rpn);
         break;
-#endif
     default:
         printf ("Unsupported query type\n");
         return 0;
@@ -828,6 +877,59 @@ static int send_searchRequest(char *arg)
     return 2;
 }
 
+/* display Query Expression as part of searchResult-1 */
+static void display_queryExpression (Z_QueryExpression *qe)
+{
+    if (!qe)
+        return;
+    if (qe->which == Z_QueryExpression_term)
+    {
+        if (qe->u.term->queryTerm)
+        {
+            Z_Term *term = qe->u.term->queryTerm;
+            if (term->which == Z_Term_general)
+                printf (" %.*s", term->u.general->len, term->u.general->buf);
+        }
+    }
+
+}
+
+/* see if we can find USR:SearchResult-1 */
+static void display_searchResult (Z_OtherInformation *o)
+{
+    int i;
+    if (!o)
+        return ;
+    for (i = 0; i < o->num_elements; i++)
+    {
+        if (o->list[i]->which == Z_OtherInfo_externallyDefinedInfo)
+        {
+            Z_External *ext = o->list[i]->information.externallyDefinedInfo;
+            
+            if (ext->which == Z_External_searchResult1)
+            {
+                int j;
+                Z_SearchInfoReport *sr = ext->u.searchResult1;
+                printf ("SearchResult-1:");
+                for (j = 0; j < sr->num; j++)
+                {
+                    if (!sr->elements[j]->subqueryExpression)
+                        printf (" %d", j);
+                    display_queryExpression (
+                        sr->elements[j]->subqueryExpression);
+                    display_queryExpression (
+                        sr->elements[j]->subqueryInterpretation);
+                    display_queryExpression (
+                        sr->elements[j]->subqueryRecommendation);
+                    if (sr->elements[j]->subqueryCount)
+                        printf ("(%d)", *sr->elements[j]->subqueryCount);
+                }
+                printf ("\n");
+            }
+        }
+    }
+}
+
 static int process_searchResponse(Z_SearchResponse *res)
 {
     printf ("Received SearchResponse.\n");
@@ -839,7 +941,9 @@ static int process_searchResponse(Z_SearchResponse *res)
     printf("Number of hits: %d", *res->resultCount);
     if (setnumber >= 0)
         printf (", setno %d", setnumber);
-    printf("\nrecords returned: %d\n",
+    printf ("\n");
+    display_searchResult (res->additionalSearchInfo);
+    printf("records returned: %d\n",
            *res->numberOfRecordsReturned);
     setno += *res->numberOfRecordsReturned;
     if (res->records)
@@ -1075,8 +1179,6 @@ void process_ESResponse(Z_ExtendedServicesResponse *res)
     }
 }
 
-#if YAZ_MODULE_ill
-
 const char *get_ill_element (void *clientData, const char *element)
 {
     return 0;
@@ -1138,9 +1240,7 @@ static Z_External *create_external_itemRequest()
     }
     return r;
 }
-#endif
 
-#ifdef YAZ_MODULE_ill
 static Z_External *create_external_ILL_APDU(int which)
 {
     struct ill_get_ctl ctl;
@@ -1196,7 +1296,6 @@ static Z_External *create_external_ILL_APDU(int which)
     }
     return r;
 }
-#endif
 
 
 static Z_External *create_ItemOrderExternal(const char *type, int itemno)
@@ -1995,6 +2094,10 @@ int cmd_packagename(char* arg)
 
 int cmd_proxy(char* arg)
 {
+    if (*arg == '\0') {
+       printf("Current proxy is `%s'\n", (yazCharset)?yazProxy:NULL);
+       return 1;
+    }
     xfree (yazProxy);
     yazProxy = NULL;
     if (*arg)
@@ -2005,6 +2108,38 @@ int cmd_proxy(char* arg)
     return 1;
 }
 
+int cmd_charset(char* arg)
+{
+    if (*arg == '\0') {
+       printf("Current character set is `%s'\n", (yazCharset)?yazCharset:NULL);
+       return 1;
+    }
+    xfree (yazCharset);
+    yazCharset = NULL;
+    if (*arg)
+    {
+        yazCharset = (char *) xmalloc (strlen(arg)+1);
+        strcpy (yazCharset, arg);
+    } 
+    return 1;
+}
+
+int cmd_lang(char* arg)
+{
+    if (*arg == '\0') {
+       printf("Current language is `%s'\n", (yazLang)?yazLang:NULL);
+       return 1;
+    }
+    xfree (yazLang);
+    yazLang = NULL;
+    if (*arg)
+    {
+        yazLang = (char *) xmalloc (strlen(arg)+1);
+        strcpy (yazLang, arg);
+    } 
+    return 1;
+}
+
 int cmd_source(char* arg) 
 {
     /* first should open the file and read one line at a time.. */
@@ -2440,6 +2575,8 @@ static struct {
     {"update", cmd_update, "<item>",NULL,0},
     {"packagename", cmd_packagename, "<packagename>",NULL,0},
     {"proxy", cmd_proxy, "[('tcp'|'ssl')]<host>[':'<port>]",NULL,0},
+    {"charset", cmd_charset, "<charset_name>",NULL,0},
+    {"lang", cmd_lang, "<language_code>",NULL,0},
     {".", cmd_source, "<filename>",NULL,1},
     {"!", cmd_subshell, "Subshell command",NULL,0},
     {"set_apdufile", cmd_set_apdufile, "<filename>",NULL,0},
@@ -2546,7 +2683,7 @@ void process_cmd_line(char* line)
     {
         printf("Unknown command: %s.\n", word);
         cmd_help ("");
-
+        res = 1;
     }
 
     if(apdu_file) fflush(apdu_file);
@@ -2584,7 +2721,7 @@ char ** readline_completer(char *text, int start, int end) {
 #if HAVE_READLINE_READLINE_H
 
     if(start == 0) {
-#ifdef RL_READLINE_VERSION
+#if HAVE_READLINE_RL_COMPLETION_MATCHES
         char** res=rl_completion_matches(text,
                                       command_generator); 
 #else
@@ -2615,7 +2752,7 @@ char ** readline_completer(char *text, int start, int end) {
         if(!cmd[i].complete_filenames) 
             rl_attempted_completion_over = 1;    
         if(cmd[i].rl_completerfunction) {
-#ifdef RL_READLINE_VERSION
+#ifdef HAVE_READLINE_RL_COMPLETION_MATCHES
             char** res=
                 rl_completion_matches(text,
                                    cmd[i].rl_completerfunction);