Rename curret_global_list to current_global_list; reformat. No code changes
[yaz-moved-to-github.git] / client / client.c
index b478ce2..991bc06 100644 (file)
@@ -1,8 +1,8 @@
 /* 
- * Copyright (C) 1995-2005, Index Data ApS
+ * Copyright (C) 1995-2006, Index Data ApS
  * See the file LICENSE for details.
  *
- * $Id: client.c,v 1.302 2005-12-20 23:07:27 adam Exp $
+ * $Id: client.c,v 1.308 2006-05-07 19:43:00 adam Exp $
  */
 
 #include <stdio.h>
@@ -169,9 +169,11 @@ struct {
         
 
 void process_cmd_line(char* line);
-char ** readline_completer(char *text, int start, int end);
-char *command_generator(const char *text, int state);
-char** curret_global_list=NULL;
+#if HAVE_READLINE_READLINE_H
+char **readline_completer(char *text, int start, int end);
+#endif
+static char *command_generator(const char *text, int state);
+char** current_global_list = NULL;
 int cmd_register_tab(const char* arg);
 
 static void close_session (void);
@@ -341,12 +343,10 @@ static void send_initRequest(const char* type_and_host)
             
             p0->which = Z_OtherInfo_externallyDefinedInfo;
             p0->information.externallyDefinedInfo =
-                yaz_set_proposal_charneg(
-                    out,
-                    (const char**)&negotiationCharset, 
-                    negotiationCharset ? 1 : 0,
-                    (const char**)&yazLang, yazLang ? 1 : 0, 
-                    negotiationCharsetRecords);
+                yaz_set_proposal_charneg_list(out, ",", 
+                                              negotiationCharset, 
+                                              yazLang,
+                                              negotiationCharsetRecords);
         }
     }
     
@@ -882,7 +882,7 @@ static void display_record(Z_External *r)
             if (!(*type->fun)(in, &rr, 0, 0))
             {
                 odr_perror(in, "Decoding constructed record.");
-                fprintf(stdout, "[Near %d]\n", odr_offset(in));
+                fprintf(stdout, "[Near %ld]\n", (long) odr_offset(in));
                 fprintf(stdout, "Packet dump:\n---------\n");
                 odr_dumpBER(stdout, (char*)r->u.octet_aligned->buf,
                             r->u.octet_aligned->len);
@@ -1552,7 +1552,8 @@ static int send_searchRequest(const char *arg)
             const char *pqf_msg;
             size_t off;
             int code = yaz_pqf_error (pqf_parser, &pqf_msg, &off);
-            printf("%*s^\n", off+4, "");
+            int ioff = off;
+            printf("%*s^\n", ioff+4, "");
             printf("Prefix query error: %s (code %d)\n", pqf_msg, code);
             
             yaz_pqf_destroy (pqf_parser);
@@ -2853,7 +2854,8 @@ int send_scanrequest(const char *query, int pp, int num, const char *term)
             const char *pqf_msg;
             size_t off;
             int code = yaz_pqf_error (pqf_parser, &pqf_msg, &off);
-            printf("%*s^\n", off+7, "");
+            int ioff = off;
+            printf("%*s^\n", ioff+7, "");
             printf("Prefix query error: %s (code %d)\n", pqf_msg, code);
             yaz_pqf_destroy (pqf_parser);
             return -1;
@@ -3705,9 +3707,7 @@ static void initialize(void)
 #if HAVE_READLINE_READLINE_H
     rl_attempted_completion_function = (CPPFunction*)readline_completer;
 #endif
-    
-    
-    for(i=0; i<maxOtherInfosSupported; ++i) {
+    for(i = 0; i < maxOtherInfosSupported; ++i) {
         extraOtherInfos[i].oidval = -1;
     }
     
@@ -3947,7 +3947,7 @@ void wait_and_handle_response(int one_response_only)
         {
             FILE *f = ber_file ? ber_file : stdout;
             odr_perror(in, "Decoding incoming APDU");
-            fprintf(f, "[Near %d]\n", odr_offset(in));
+            fprintf(f, "[Near %ld]\n", (long) odr_offset(in));
             fprintf(f, "Packet dump:\n---------\n");
             odr_dumpBER(f, netbuffer, res);
             fprintf(f, "---------\n");
@@ -4057,7 +4057,8 @@ int cmd_cclparse(const char* arg)
     rpn = ccl_find_str (bibset, arg, &error, &pos);
     
     if (error) {
-        printf ("%*s^ - ", 3+strlen(last_cmd)+1+pos, " ");
+        int ioff = 3+strlen(last_cmd)+1+pos;
+        printf ("%*s^ - ", ioff, " ");
         printf ("%s\n", ccl_err_msg (error));
     }
     else
@@ -4083,7 +4084,8 @@ int cmd_set_otherinfo(const char* args)
     int sscan_res;
     int oidval;
     
-    sscan_res = sscanf (args, "%d %100[^ ] %100s", &otherinfoNo, oidstr, otherinfoString);
+    sscan_res = sscanf (args, "%d %100[^ ] %100s", 
+                        &otherinfoNo, oidstr, otherinfoString);
     if (sscan_res==1) {
         /* reset this otherinfo */
         if(otherinfoNo>=maxOtherInfosSupported) {
@@ -4092,7 +4094,7 @@ int cmd_set_otherinfo(const char* args)
         }
         extraOtherInfos[otherinfoNo].oidval = -1;
         if (extraOtherInfos[otherinfoNo].value)
-            free(extraOtherInfos[otherinfoNo].value);                   
+            xfree(extraOtherInfos[otherinfoNo].value);                   
         extraOtherInfos[otherinfoNo].value = 0;
         return 0;
     }
@@ -4102,18 +4104,20 @@ int cmd_set_otherinfo(const char* args)
     }
     
     if (otherinfoNo>=maxOtherInfosSupported) {
-        printf("Error otherinfo index to large (%d>%d)\n",
+        printf("Error otherinfo index too large (%d>=%d)\n",
                otherinfoNo,maxOtherInfosSupported);
     }
     
     oidval = oid_getvalbyname (oidstr);
-    if (oidval == -1 ) {
+    if (oidval == VAL_NONE)
+    {
         printf("Error in set_otherinfo command unknown oid %s \n",oidstr);
         return 0;
     }
     extraOtherInfos[otherinfoNo].oidval = oidval;
-    if(extraOtherInfos[otherinfoNo].value) free(extraOtherInfos[otherinfoNo].value);
-    extraOtherInfos[otherinfoNo].value = strdup(otherinfoString);
+    if (extraOtherInfos[otherinfoNo].value)
+        xfree(extraOtherInfos[otherinfoNo].value);
+    extraOtherInfos[otherinfoNo].value = xstrdup(otherinfoString);
     
     return 0;
 }
@@ -4243,25 +4247,26 @@ int cmd_list_all(const char* args) {
 int cmd_clear_otherinfo(const char* args) 
 {
     if(strlen(args)>0) {
-        int otherinfoNo;
-        otherinfoNo = atoi(args);
-        if( otherinfoNo >= maxOtherInfosSupported ) {
-            printf("Error otherinfo index to large (%d>%d)\n",otherinfoNo,maxOtherInfosSupported);
+        int otherinfoNo = atoi(args);
+        if (otherinfoNo >= maxOtherInfosSupported) {
+            printf("Error otherinfo index too large (%d>=%d)\n",
+                   otherinfoNo, maxOtherInfosSupported);
             return 0;
         }
-        
-        if(extraOtherInfos[otherinfoNo].oidval != -1) {                 
+        if (extraOtherInfos[otherinfoNo].oidval != -1)
+        {                 
             /* only clear if set. */
-            extraOtherInfos[otherinfoNo].oidval=-1;
-            free(extraOtherInfos[otherinfoNo].value);
+            extraOtherInfos[otherinfoNo].oidval = -1;
+            xfree(extraOtherInfos[otherinfoNo].value);
         }
     } else {
         int i;
-        
-        for(i=0; i<maxOtherInfosSupported; ++i) {
-            if (extraOtherInfos[i].oidval!=-1 ) {                               
-                extraOtherInfos[i].oidval=-1;
-                free(extraOtherInfos[i].value);
+        for(i = 0; i < maxOtherInfosSupported; ++i) 
+        {
+            if (extraOtherInfos[i].oidval != -1)
+            {                               
+                extraOtherInfos[i].oidval = -1;
+                xfree(extraOtherInfos[i].value);
             }
         }
     }
@@ -4429,8 +4434,9 @@ static int cmd_help (const char *line)
     return 1;
 }
 
-int cmd_register_tab(const char* arg) {
-        
+int cmd_register_tab(const char* arg) 
+{
+#if HAVE_READLINE_READLINE_H
     char command[101], tabargument[101];
     int i;
     int num_of_tabs;
@@ -4447,27 +4453,29 @@ int cmd_register_tab(const char* arg) {
         }
     }
     
-    if(!cmd_array[i].cmd) { 
+    if (!cmd_array[i].cmd) { 
         fprintf(stderr,"Unknown command %s\n",command);
         return 1;
     }
     
         
-    if(!cmd_array[i].local_tabcompletes)
+    if (!cmd_array[i].local_tabcompletes)
         cmd_array[i].local_tabcompletes = (char **) calloc(1,sizeof(char**));
     
     num_of_tabs=0;              
     
     tabslist = cmd_array[i].local_tabcompletes;
-    for(;tabslist && *tabslist;tabslist++) {
+    for(; tabslist && *tabslist; tabslist++) {
         num_of_tabs++;
     }
     
-    cmd_array[i].local_tabcompletes =  (char **)
-        realloc(cmd_array[i].local_tabcompletes,(num_of_tabs+2)*sizeof(char**));
-    tabslist=cmd_array[i].local_tabcompletes;
-    tabslist[num_of_tabs]=strdup(tabargument);
-    tabslist[num_of_tabs+1]=NULL;
+    cmd_array[i].local_tabcompletes = (char **)
+        realloc(cmd_array[i].local_tabcompletes,
+                (num_of_tabs+2)*sizeof(char**));
+    tabslist = cmd_array[i].local_tabcompletes;
+    tabslist[num_of_tabs] = strdup(tabargument);
+    tabslist[num_of_tabs+1] = NULL;
+#endif
     return 1;
 }
 
@@ -4529,38 +4537,38 @@ void process_cmd_line(char* line)
         fflush(marc_file);
 }
 
-
-char *command_generator(const char *text, int state) 
+static char *command_generator(const char *text, int state) 
 {
+#if HAVE_READLINE_READLINE_H
     static int idx; 
     if (state==0) {
         idx = 0;
     }
     for( ; cmd_array[idx].cmd; ++idx) {
-        if (!strncmp(cmd_array[idx].cmd,text,strlen(text))) {
+        if (!strncmp(cmd_array[idx].cmd, text, strlen(text))) {
             ++idx;  /* skip this entry on the next run */
             return strdup(cmd_array[idx-1].cmd);
         }
     }
+#endif
     return NULL;
 }
 
+#if HAVE_READLINE_READLINE_H
 
 /* 
    This function only known how to complete on the first word
 */
-char ** readline_completer(char *text, int start, int end) {
-#if HAVE_READLINE_READLINE_H
-
-        completerFunctionType completerToUse;
-        
+char **readline_completer(char *text, int start, int end)
+{
+    completerFunctionType completerToUse;
+    
     if(start == 0) {
 #if HAVE_READLINE_RL_COMPLETION_MATCHES
-        char** res=rl_completion_matches(text,
-                                      command_generator); 
+        char** res = rl_completion_matches(text, command_generator); 
 #else
-        char** res=completion_matches(text,
-                                      (CPFunction*)command_generator); 
+        char** res = completion_matches(text,
+                                        (CPFunction*)command_generator); 
 #endif
         rl_attempted_completion_over = 1;
         return res;
@@ -4580,36 +4588,32 @@ char ** readline_completer(char *text, int start, int end) {
         
         if(!cmd_array[i].cmd) return NULL;
         
-        curret_global_list = cmd_array[i].local_tabcompletes;
+        current_global_list = cmd_array[i].local_tabcompletes;
         
         completerToUse = cmd_array[i].rl_completerfunction;
-        if(completerToUse==NULL)  /* if no pr. command completer is defined use the default completer */
+        if (completerToUse==NULL) 
+        { /* if command completer is not defined use the default completer */
             completerToUse = default_completer;
-        
-        if(completerToUse) {
+        }
+        if (completerToUse) {
 #ifdef HAVE_READLINE_RL_COMPLETION_MATCHES
             char** res=
-                rl_completion_matches(text,
-                                      completerToUse);
+                rl_completion_matches(text, completerToUse);
 #else
             char** res=
-                completion_matches(text,
-                                   (CPFunction*)completerToUse);
+                completion_matches(text, (CPFunction*)completerToUse);
 #endif
-            if(!cmd_array[i].complete_filenames) 
+            if (!cmd_array[i].complete_filenames) 
                 rl_attempted_completion_over = 1;
             return res;
         } else {
-            if(!cmd_array[i].complete_filenames) 
+            if (!cmd_array[i].complete_filenames) 
                 rl_attempted_completion_over = 1;
             return 0;
         }
     }
-#else 
-    return 0;
-#endif 
 }
-
+#endif
 
 static void client(void)
 {
@@ -4635,7 +4639,7 @@ static void client(void)
                 add_history(line_in);
 #endif
             strncpy(line, line_in, 10239);
-            free (line_in);
+            free(line_in);
         }
 #endif 
         if (!line_in)