Also buffer engine man page
[xboard.git] / dialogs.c
index 191061c..30b2ad5 100644 (file)
--- a/dialogs.c
+++ b/dialogs.c
@@ -2446,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;
 }
@@ -2468,8 +2477,9 @@ GetHelpText (FILE *f, char *name)
     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) && strncmp(line, ".SS ", 4))) {
+       if(!strncmp(line, buf, len) || !strncmp(line, ".SS ", 4) && !strncmp(line+4, buf+3, len-3)
+                           || !strncmp(line, ".IX Item \"", 10) && !strncmp(line+10, buf+3, len-3)) {
+           while((line = ReadLine(f)) && (cnt == 0 || strncmp(line, ".B ", 3) && strncmp(line, ".SS ", 4) && strncmp(line, ".IX ", 4))) {
                if(!*line) { *p++ = '\n'; *p++ = '\n'; q = p; continue; }
                if(*line == '.') continue;
                *p++ = ' '; cnt++;
@@ -2492,23 +2502,35 @@ GetHelpText (FILE *f, char *name)
 void
 DisplayHelp (char *name)
 {
-    static char *xboardMan;
-    char buf[MSG_SIZ], tidy[MSG_SIZ];
+    static char *xboardMan, *manText[2], tidy[MSG_SIZ], engMan[MSG_SIZ];
+    char buf[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) {
-       TidyProgramName(currentCps == &first ? appData.firstChessProgram : appData.secondChessProgram, "localhost", tidy);
-       snprintf(buf, MSG_SIZ, "/usr/local/share/man/man6/%s.6", tidy);
-    } else snprintf(buf, MSG_SIZ, "%s", xboardMan);
+    if(currentCps) { // for engine options we have to look in engine manual
+       snprintf(buf, MSG_SIZ, "man -w ");            // get (tidied) engine name in buf
+       TidyProgramName(currentCps == &first ? appData.firstChessProgram : appData.secondChessProgram, "localhost", buf+7);
+       if(strcmp(buf, tidy)) {                       // is different engine from last time
+           FREE(manText[1]); manText[1] = NULL;      // so any currently held text is worthless
+           safeStrCpy(tidy, buf, MSG_SIZ);           // remember current engine
+           eng = BufferCommandOutput(tidy, MSG_SIZ); // obtain path to  its man file
+           safeStrCpy(engMan, eng, strlen(eng));     // and remember that too
+           FREE(eng);
+       }
+       safeStrCpy(buf, engMan, MSG_SIZ); n = 1;      // use engine man
+    } else snprintf(buf, MSG_SIZ, "%s", xboardMan);   // use xboard man
     f = fopen(buf, "r");
-    if(!f && currentCps) { // engine manual could be in two places
-       snprintf(buf, MSG_SIZ, "/usr/share/man/man6/%s.6", tidy);
-       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];// use buffered unzipped text
+       } else textPtr = NULL;   // use plaintext man file directly
        GetHelpText(f, name);
        fclose(f);
     }