Allow access to gzipped man files
[xboard.git] / dialogs.c
index ca9f71c..4dce7f4 100644 (file)
--- a/dialogs.c
+++ b/dialogs.c
@@ -66,6 +66,7 @@ extern char *getenv();
 
 int values[MAX_OPTIONS];
 ChessProgramState *currentCps;
+char dataDir[MSG_SIZ] = DATADIR, manDir[MSG_SIZ] = MANDIR;
 
 //----------------------------Generic dialog --------------------------------------------
 
@@ -1259,7 +1260,7 @@ NewTagsPopup (char *text, char *msg, char *ttl)
     if(tagsText) free(tagsText); tagsText = strdup(text);
     tagsOptions[0].name = msg;
     MarkMenu("View.Tags", TagsDlg);
-    GenericPopUp(tagsOptions, title, TagsDlg, BoardWindow, NONMODAL, appData.topLevel);
+    GenericPopUp(tagsOptions + (msg == NULL), title, TagsDlg, BoardWindow, NONMODAL, appData.topLevel);
 }
 
 void
@@ -1310,10 +1311,12 @@ char *icsText;
 #define HISTORY_SIZE 64
 static char *history[HISTORY_SIZE];
 static int histIn = 0, histP = 0;
+static Boolean noEcho;
 
 static void
 SaveInHistory (char *cmd)
 {
+  if(noEcho) return; // do not save password!
   if (history[histIn] != NULL) {
     free(history[histIn]);
     history[histIn] = NULL;
@@ -2156,8 +2159,6 @@ ConsoleAutoPopUp (char *buf)
        } else PopUpMoveDialog(*buf);
 }
 
-static Boolean noEcho;
-
 void
 EchoOn ()
 {
@@ -2296,7 +2297,7 @@ ErrorPopDown ()
     if (errorExitStatus != -1) ExitEvent(errorExitStatus);
 }
 
-static int
+int
 ErrorOK (int n)
 {
     dialogError = errorUp = False;
@@ -2445,12 +2446,21 @@ DisplayTitle (char *text)
     SetWindowTitle(text, title, icon);
 }
 
+char *textPtr;
+
+int
+GetNext(FILE *f)
+{
+    if(textPtr) return *textPtr ? *textPtr++ : EOF;
+    return fgetc(f);
+}
+
 static char *
 ReadLine (FILE *f)
 {
     static char buf[MSG_SIZ];
     int i = 0, c;
-    while((c = fgetc(f)) != '\n') { if(c == EOF) return NULL; buf[i++] = c; }
+    while((c = GetNext(f)) != '\n') { if(c == EOF) return NULL; buf[i++] = c; }
     buf[i] = NULLCHAR;
     return buf;
 }
@@ -2458,16 +2468,17 @@ ReadLine (FILE *f)
 void
 GetHelpText (FILE *f, char *name)
 {
-    char *line, buf[MSG_SIZ], text[10000], *p = text, *q = text;
+    char *line, buf[MSG_SIZ], title[MSG_SIZ], text[10000], *p = text, *q = text;
     int len, cnt = 0;
     snprintf(buf, MSG_SIZ, ".B %s", name);
     len = strlen(buf);
-    for(len=1; buf[len] == ' ' || isalpha(buf[len]) || isdigit(buf[len]); len++);
+    for(len=1; buf[len] == ' ' || buf[len] == '-' || isalpha(buf[len]) || isdigit(buf[len]); len++);
     buf[len] = NULLCHAR;
     while(buf[--len] == ' ') buf[len] = NULLCHAR;
+    snprintf(title, MSG_SIZ, "Help on '%s'", buf+3);
     while((line = ReadLine(f))) {
-       if(!strncmp(line, buf, len) && (strncmp(line, ".SS ", 4) || strncmp(line+4, buf+3, len-3))) {
-           while((line = ReadLine(f)) && (cnt == 0 || strncmp(line, ".B ", 3))) {
+       if(!strncmp(line, buf, len) || !strncmp(line, ".SS ", 4) && !strncmp(line+4, buf+3, len-3)) {
+           while((line = ReadLine(f)) && (cnt == 0 || strncmp(line, ".B ", 3) && strncmp(line, ".SS ", 4))) {
                if(!*line) { *p++ = '\n'; *p++ = '\n'; q = p; continue; }
                if(*line == '.') continue;
                *p++ = ' '; cnt++;
@@ -2479,7 +2490,7 @@ GetHelpText (FILE *f, char *name)
                if(p - text > 9900) break;
            }
            *p = NULLCHAR;
-           DisplayNote(text);
+           ErrorPopUp(title, text, FALSE);
            return;
        }
     }
@@ -2490,11 +2501,34 @@ GetHelpText (FILE *f, char *name)
 void
 DisplayHelp (char *name)
 {
-    char *manFile = DATADIR "/../../man/man6/xboard.6";
-    FILE *f = fopen(manFile, "r");
+    static char *xboardMan, *manText[2];
+    char buf[MSG_SIZ], tidy[MSG_SIZ], *eng;
+    int n = 0;
+    FILE *f;
+    if(!xboardMan) {
+       xboardMan = BufferCommandOutput("man -w xboard", MSG_SIZ); // obtain path to XBoard's man file
+       if(xboardMan) xboardMan[strlen(xboardMan)-1] = NULLCHAR;   // strip off traling linefeed
+    }
+    if(currentCps) { // for engine options we have to look in engine manual
+       snprintf(tidy, MSG_SIZ, "man -w ");
+       TidyProgramName(currentCps == &first ? appData.firstChessProgram : appData.secondChessProgram, "localhost", tidy+7);
+       eng = BufferCommandOutput(tidy, MSG_SIZ);
+       safeStrCpy(buf, eng, strlen(eng));
+       FREE(eng);
+       n = 1;
+    } else snprintf(buf, MSG_SIZ, "%s", xboardMan);
+    f = fopen(buf, "r");
     if(f) {
+       if(strstr(buf, ".gz")) { // man file is gzipped
+           if(!manText[n]) {    // unzipped text not buffered yet
+               snprintf(tidy, MSG_SIZ, "gunzip -c %s", buf);
+               manText[n] = BufferCommandOutput(tidy, 250000); // store unzipped in buffer
+           }
+           textPtr = manText[n];
+       } else textPtr = NULL;
        GetHelpText(f, name);
        fclose(f);
+       if(currentCps) free(manText[1]), manText[1] = NULL; // never reuse engine text 
     }
 }
 
@@ -2892,6 +2926,7 @@ static char *Extensions[] = {
 ".trn",
 ".bin",
 ".wav",
+".png",
 ".ini",
 ".log",
 "",