Whitespace and parenthesis cleanup.
[gnushogi.git] / gnushogi / commondsp.c
index 14df54c..36a5f8d 100644 (file)
@@ -47,6 +47,7 @@
 char mvstr[4][6];
 char *InPtr;
 int InBackground = false;
+struct display *dsp = &raw_display;
 
 
 #if defined(BOOKTEST)
@@ -123,8 +124,6 @@ movealgbr(short m, char *s)
 #endif /* BOOKTEST */
 
 
-
-
 /*
  * Generate move strings in different formats.
  *
@@ -164,7 +163,7 @@ algbr(short f, short t, short flag)
     if ((f == t) && ((f != 0) || (t != 0)))
     {
         if (!barebones) {
-            Printf("error in algbr: FROM=TO=%d, flag=0x%4x\n", t, flag);
+            dsp->Printf("error in algbr: FROM=TO=%d, flag=0x%4x\n", t, flag);
         }
 
         mvstr[0][0] = mvstr[1][0] = mvstr[2][0] = mvstr[3][0] = '\0';
@@ -216,7 +215,6 @@ algbr(short f, short t, short flag)
 }
 
 
-
 /*
  * Compare the string 's' to the list of legal moves available for the
  * opponent. If a match is found, make the move on the board.
@@ -297,7 +295,7 @@ VerifyMove(char *s, VerifyMove_mode iop, unsigned short *mv)
         if (SqAttacked(PieceList[opponent][0], computer, &blocked))
         {
             UnmakeMove(opponent, &xnode, &tempb, &tempc, &tempsf, &tempst);
-            AlwaysShowMessage("Illegal move (in check) %s", s);
+            dsp->AlwaysShowMessage("Illegal move (in check) %s", s);
             return false;
         }
         else
@@ -305,7 +303,7 @@ VerifyMove(char *s, VerifyMove_mode iop, unsigned short *mv)
             if (iop == VERIFY_AND_TRY_MODE)
                 return true;
 
-            UpdateDisplay(xnode.f, xnode.t, 0, (short) xnode.flags);
+            dsp->UpdateDisplay(xnode.f, xnode.t, 0, (short) xnode.flags);
             GameList[GameCnt].depth = GameList[GameCnt].score = 0;
             GameList[GameCnt].nodes = 0;
             ElapsedTime(COMPUTE_AND_INIT_MODE);
@@ -330,7 +328,7 @@ VerifyMove(char *s, VerifyMove_mode iop, unsigned short *mv)
                     char buf[20];
 
                     sprintf(buf, "%s mates!\n", ColorStr[opponent]);
-                    ShowMessage(buf);
+                    dsp->ShowMessage(buf);
                     flag.mate = true;
                 }
             }
@@ -339,19 +337,18 @@ VerifyMove(char *s, VerifyMove_mode iop, unsigned short *mv)
         }
     }
 
-    AlwaysShowMessage("Illegal move (no match) %s", s);
+    dsp->AlwaysShowMessage("Illegal move (no match) %s", s);
 
     if (!barebones && (cnt > 1))
     {
         sprintf(buffer, "Ambiguous Move %s!", s);
-        ShowMessage(buffer);
+        dsp->ShowMessage(buffer);
     }
 
     return false;
 }
 
 
-
 static int
 parser(char *f, int side, short *fpiece)
 {
@@ -401,7 +398,6 @@ skip()
 }
 
 
-
 void
 skipb()
 {
@@ -410,6 +406,23 @@ skipb()
 }
 
 
+void RequestInputString(char* buffer, unsigned bufsize)
+{
+    static char fmt[10];
+    int ret = snprintf(fmt, sizeof(fmt), "%%%us", bufsize);
+    if (ret < 0 ) {
+        perror("RequestInputString snprintf");
+        exit(1);
+    }
+    if (ret >= sizeof(fmt)) {
+        fprintf(stderr,
+                "Insufficient format-buffer size in %s for bufsize=%u\n",
+                __FUNCTION__, bufsize);
+        exit(1);
+    }
+    dsp->doRequestInputString(fmt, buffer);
+}
+
 
 void
 GetGame(void)
@@ -423,7 +436,7 @@ GetGame(void)
     if (savefile[0]) {
         strcpy(fname, savefile);
     } else {
-        ShowMessage("Enter file name: ");
+        dsp->ShowMessage("Enter file name: ");
         RequestInputString(fname, sizeof(fname)-1);
     }
 
@@ -542,10 +555,12 @@ GetGame(void)
             skipb();
             Captured[side][pawn] = atoi(InPtr);
             skip();
+#ifndef MINISHOGI
             Captured[side][lance] = atoi(InPtr);
             skip();
             Captured[side][knight] = atoi(InPtr);
             skip();
+#endif
             Captured[side][silver] = atoi(InPtr);
             skip();
             Captured[side][gold] = atoi(InPtr);
@@ -626,13 +641,12 @@ GetGame(void)
 
     ZeroRPT();
     InitializeStats();
-    UpdateDisplay(0, 0, 1, 0);
+    dsp->UpdateDisplay(0, 0, 1, 0);
     Sdepth = 0;
     hint = 0;
 }
 
 
-
 void
 SaveGame(void)
 {
@@ -646,7 +660,7 @@ SaveGame(void)
     if (savefile[0]) {
         strcpy(fname, savefile);
     } else {
-        ShowMessage("Enter file name: ");
+        dsp->ShowMessage("Enter file name: ");
         RequestInputString(fname, sizeof(fname)-1);
     }
 
@@ -710,16 +724,24 @@ SaveGame(void)
         }
 
         fputs(empty, fd);
-        fprintf(fd, "   9 8 7 6 5 4 3 2 1\n");    /* FIXME */
+#ifndef MINISHOGI
+        fprintf(fd, "   9 8 7 6 5 4 3 2 1\n");
         fputs(empty, fd);
         fprintf(fd, "   p  l  n  s  g  b  r  k\n");
+#else
+        fprintf(fd, "   5 4 3 2 1\n");
+        fputs(empty, fd);
+        fprintf(fd, "   p  s  g  b  r  k\n");
+#endif
 
         for (side = 0; side <= 1; side++)
         {
             fprintf(fd, "%c", (side == black) ? 'B' : 'W');
             fprintf(fd, " %2d", Captured[side][pawn]);
+#ifndef MINISHOGI
             fprintf(fd, " %2d", Captured[side][lance]);
             fprintf(fd, " %2d", Captured[side][knight]);
+#endif
             fprintf(fd, " %2d", Captured[side][silver]);
             fprintf(fd, " %2d", Captured[side][gold]);
             fprintf(fd, " %2d", Captured[side][bishop]);
@@ -763,16 +785,15 @@ SaveGame(void)
 
         fclose(fd);
 
-        ShowMessage("Game saved");
+        dsp->ShowMessage("Game saved");
     }
     else
     {
-        ShowMessage("Could not open file");
+        dsp->ShowMessage("Could not open file");
     }
 }
 
 
-
 /*
  * GetXGame, SaveXGame and BookGame used to only be defined if
  * xshogi wasn't defined -- wonder why?
@@ -787,7 +808,7 @@ GetXGame(void)
     short sq;
     short side, isp;
 
-    ShowMessage("Enter file name: ");
+    dsp->ShowMessage("Enter file name: ");
     RequestInputString(fname, sizeof(fname)-1);
 
     if (fname[0] == '\0')
@@ -871,10 +892,12 @@ GetXGame(void)
             InPtr = fname;
             Captured[side][pawn]   = atoi(InPtr);
             skip();
+#ifndef MINISHOGI
             Captured[side][lance]  = atoi(InPtr);
             skip();
             Captured[side][knight] = atoi(InPtr);
             skip();
+#endif
             Captured[side][silver] = atoi(InPtr);
             skip();
             Captured[side][gold]   = atoi(InPtr);
@@ -899,7 +922,7 @@ GetXGame(void)
     Game50 = 1;
     ZeroRPT();
     InitializeStats();
-    UpdateDisplay(0, 0, 1, 0);
+    dsp->UpdateDisplay(0, 0, 1, 0);
     Sdepth = 0;
     hint = 0;
 }
@@ -914,7 +937,7 @@ SaveXGame(void)
     short sq, piece;
     short side, isp;
 
-    ShowMessage("Enter file name: ");
+    dsp->ShowMessage("Enter file name: ");
     RequestInputString(fname, sizeof(fname)-1);
 
     if (fname[0] == '\0')
@@ -955,10 +978,17 @@ SaveXGame(void)
 
         for (side = 0; side <= 1; side++)
         {
-            sprintf(fname, "%d %d %d %d %d %d %d %d\n",
+            sprintf(fname,
+#ifndef MINISHOGI
+                   "%d %d %d %d %d %d %d %d\n",
+#else
+                   "%d %d %d %d %d %d\n",
+#endif
                     Captured[side][pawn],
+#ifndef MINISHOGI
                     Captured[side][lance],
                     Captured[side][knight],
+#endif
                     Captured[side][silver],
                     Captured[side][gold],
                     Captured[side][bishop],
@@ -989,7 +1019,7 @@ BookSave(void)
         strcpy(fname, savefile);
     } else {
         /* Enter file name */
-        ShowMessage("Enter file name: ");
+        dsp->ShowMessage("Enter file name: ");
         RequestInputString(fname, sizeof(fname)-1);
     }
 
@@ -1061,16 +1091,15 @@ BookSave(void)
 
         fclose(fd);
 
-        ShowMessage("Game saved");
+        dsp->ShowMessage("Game saved");
     }
     else
     {
-        ShowMessage("Could not open file");
+        dsp->ShowMessage("Could not open file");
     }
 }
 
 
-
 void
 ListGame(void)
 {
@@ -1093,13 +1122,13 @@ ListGame(void)
         dbuf[16] = '\0';
         dbuf[19] = '\0';
 
-        /* use format "CLp16.Jan01-020304B" when patchlevel is 16,
+        /* use format "CL.Jan01-020304B" when
            date is Jan 1
            time is 02:03:04
            program played white */
 
-        sprintf(fname, "CLp%s.%s%s-%s%s%s%c",
-                patchlevel, dbuf + 4, dbuf + 8, dbuf + 11, dbuf + 14,
+        sprintf(fname, "CL.%s%s-%s%s%s%c",
+                dbuf + 4, dbuf + 8, dbuf + 11, dbuf + 14,
                 dbuf + 17, ColorStr[computer][0]);
 
         /* replace space padding with 0 */
@@ -1118,7 +1147,7 @@ ListGame(void)
         exit(1);
     }
 
-    fprintf(fd, "gnushogi %sp%s game\n", version, patchlevel);
+    fprintf(fd, "gnushogi %s game\n", PACKAGE_VERSION);
     fputs("         score  depth   nodes  time         ", fd);
     fputs("         score  depth   nodes  time\n", fd);
 
@@ -1198,7 +1227,6 @@ ListGame(void)
 }
 
 
-
 void
 FlagMove(char c)
 {
@@ -1221,8 +1249,6 @@ FlagMove(char c)
 }
 
 
-
-
 /*
  * Undo the most recent half-move.
  */
@@ -1276,15 +1302,14 @@ Undo(void)
     flag.mate = false;
     Sdepth = 0;
     player = player ^ 1;
-    ShowSidetoMove();
-    UpdateDisplay(0, 0, 1, 0);
+    dsp->ShowSidetoMove();
+    dsp->UpdateDisplay(0, 0, 1, 0);
 
     if (flag.regularstart)
         Book = false;
 }
 
 
-
 void
 FlagString(unsigned short flags, char *s)
 {
@@ -1337,7 +1362,6 @@ FlagString(unsigned short flags, char *s)
 }
 
 
-
 void
 TestSpeed(void(*f)(short side, short ply,
                    short in_check, short blockable),
@@ -1388,11 +1412,10 @@ TestSpeed(void(*f)(short side, short ply,
     else
         et = 1;
 
-    ShowNodeCnt(cnt);
+    dsp->ShowNodeCnt(cnt);
 }
 
 
-
 void
 TestPSpeed(short(*f) (short side), unsigned j)
 {
@@ -1426,11 +1449,10 @@ TestPSpeed(short(*f) (short side), unsigned j)
     else
         et = 1;
 
-    ShowNodeCnt(cnt);
+    dsp->ShowNodeCnt(cnt);
 }
 
 
-
 void
 SetOppTime(char *s)
 {
@@ -1465,7 +1487,6 @@ SetOppTime(char *s)
 }
 
 
-
 void
 SetMachineTime(char *s)
 {
@@ -1500,9 +1521,6 @@ SetMachineTime(char *s)
 }
 
 
-
-
-
 /* FIXME!  This is truly the function from hell! */
 
 /*
@@ -1546,7 +1564,7 @@ InputCommand(char *command)
 
 #if !defined NOPOST
         if (flag.post)
-            GiveHint();
+            dsp->GiveHint();
 #endif
 
         /* do the hint move */
@@ -1555,7 +1573,7 @@ InputCommand(char *command)
             Sdepth = 0;
 
 #ifdef QUIETBACKGROUND
-            ShowPrompt();
+            dsp->ShowPrompt();
             have_shown_prompt = true;
 #endif /* QUIETBACKGROUND */
 
@@ -1602,7 +1620,7 @@ InputCommand(char *command)
         {
 #endif /* QUIETBACKGROUND */
 
-            ShowPrompt();
+            dsp->ShowPrompt();
 
 #ifdef QUIETBACKGROUND
         }
@@ -1614,7 +1632,7 @@ InputCommand(char *command)
             if (NOT_CURSES)
                 s[0] = '\0';
 
-            eof = GetString(sx);
+            eof = dsp->GetString(sx);
         } else {
             strcpy(sx, command);
             done = true;
@@ -1623,7 +1641,7 @@ InputCommand(char *command)
         sscanf(sx, "%s", s);
 
         if (eof)
-            ExitShogi();
+            dsp->ExitShogi();
 
         if (s[0] == '\0')
             continue;
@@ -1636,8 +1654,8 @@ InputCommand(char *command)
             if (old_xshogi)
                 display_type = DISPLAY_RAW;
 
-            ClearScreen();
-            UpdateDisplay(0, 0, 1, 0);
+            dsp->ClearScreen();
+            dsp->UpdateDisplay(0, 0, 1, 0);
 
             if (old_xshogi)
                 display_type = DISPLAY_X;
@@ -1664,11 +1682,11 @@ InputCommand(char *command)
         else if ((strcmp(s, "set") == 0)
                  || (strcmp(s, "edit") == 0))
         {
-            EditBoard();
+            dsp->EditBoard();
         }
         else if ((strcmp(s, "setup") == 0))
         {
-            SetupBoard();
+            dsp->SetupBoard();
         }
         else if (strcmp(s, "first") == 0)
         {
@@ -1692,7 +1710,7 @@ InputCommand(char *command)
         }
         else if (strcmp(s, "help") == 0)
         {
-            help();
+            dsp->help();
         }
         else if (strcmp(s, "material") == 0)
         {
@@ -1718,7 +1736,7 @@ InputCommand(char *command)
         else if (strcmp(s, "new") == 0)
         {
             NewGame();
-            UpdateDisplay(0, 0, 1, 0);
+            dsp->UpdateDisplay(0, 0, 1, 0);
         }
         else if (strcmp(s, "list") == 0)
         {
@@ -1727,7 +1745,7 @@ InputCommand(char *command)
         else if ((strcmp(s, "level") == 0)
                  || (strcmp(s, "clock") == 0))
         {
-            SelectLevel(sx);
+            dsp->SelectLevel(sx);
         }
         else if (strcmp(s, "hash") == 0)
         {
@@ -1751,11 +1769,11 @@ InputCommand(char *command)
         }
         else if (strcmp(s, "Awindow") == 0)
         {
-            ChangeAlphaWindow();
+            dsp->ChangeAlphaWindow();
         }
         else if (strcmp(s, "Bwindow") == 0)
         {
-            ChangeBetaWindow();
+            dsp->ChangeBetaWindow();
         }
         else if (strcmp(s, "rcptr") == 0)
         {
@@ -1763,7 +1781,7 @@ InputCommand(char *command)
         }
         else if (strcmp(s, "hint") == 0)
         {
-            GiveHint();
+            dsp->GiveHint();
         }
         else if (strcmp(s, "both") == 0)
         {
@@ -1777,8 +1795,8 @@ InputCommand(char *command)
         else if (strcmp(s, "reverse") == 0)
         {
             flag.reverse = !flag.reverse;
-            ClearScreen();
-            UpdateDisplay(0, 0, 1, 0);
+            dsp->ClearScreen();
+            dsp->UpdateDisplay(0, 0, 1, 0);
         }
         else if (strcmp(s, "switch") == 0)
         {
@@ -1856,11 +1874,11 @@ InputCommand(char *command)
         }
         else if (strcmp(s, "depth") == 0)
         {
-            ChangeSearchDepth();
+            dsp->ChangeSearchDepth();
         }
         else if (strcmp(s, "hashdepth") == 0)
         {
-            ChangeHashDepth();
+            dsp->ChangeHashDepth();
         }
         else if (strcmp(s, "random") == 0)
         {
@@ -1880,26 +1898,26 @@ InputCommand(char *command)
         }
         else if (strcmp(s, "contempt") == 0)
         {
-            SetContempt();
+            dsp->SetContempt();
         }
         else if (strcmp(s, "xwndw") == 0)
         {
-            ChangeXwindow();
+            dsp->ChangeXwindow();
         }
         else if (strcmp(s, "rv") == 0)
         {
             flag.rv = !flag.rv;
-            UpdateDisplay(0, 0, 1, 0);
+            dsp->UpdateDisplay(0, 0, 1, 0);
         }
         else if (strcmp(s, "coords") == 0)
         {
             flag.coords = !flag.coords;
-            UpdateDisplay(0, 0, 1, 0);
+            dsp->UpdateDisplay(0, 0, 1, 0);
         }
         else if (strcmp(s, "stars") == 0)
         {
             flag.stars = !flag.stars;
-            UpdateDisplay(0, 0, 1, 0);
+            dsp->UpdateDisplay(0, 0, 1, 0);
         }
         else if (!XSHOGI && strcmp(s, "moves") == 0)
         {
@@ -1917,44 +1935,44 @@ InputCommand(char *command)
 #endif
                 SwagHt = 0;
 
-            ShowMessage("Testing MoveList Speed");
+            dsp->ShowMessage("Testing MoveList Speed");
             temp = generate_move_flags;
             generate_move_flags = true;
             TestSpeed(MoveList, 1);
             generate_move_flags = temp;
-            ShowMessage("Testing CaptureList Speed");
+            dsp->ShowMessage("Testing CaptureList Speed");
             TestSpeed(CaptureList, 1);
-            ShowMessage("Testing Eval Speed");
+            dsp->ShowMessage("Testing Eval Speed");
             ExaminePosition(opponent);
             TestPSpeed(ScorePosition, 1);
         }
         else if (!XSHOGI && strcmp(s, "test") == 0)
         {
 #ifdef SLOW_CPU
-            ShowMessage("Testing MoveList Speed");
+            dsp->ShowMessage("Testing MoveList Speed");
             TestSpeed(MoveList, 2000);
-            ShowMessage("Testing CaptureList Speed");
+            dsp->ShowMessage("Testing CaptureList Speed");
             TestSpeed(CaptureList, 3000);
-            ShowMessage("Testing Eval Speed");
+            dsp->ShowMessage("Testing Eval Speed");
             ExaminePosition(opponent);
             TestPSpeed(ScorePosition, 1500);
 #else
-            ShowMessage("Testing MoveList Speed");
+            dsp->ShowMessage("Testing MoveList Speed");
             TestSpeed(MoveList, 20000);
-            ShowMessage("Testing CaptureList Speed");
+            dsp->ShowMessage("Testing CaptureList Speed");
             TestSpeed(CaptureList, 30000);
-            ShowMessage("Testing Eval Speed");
+            dsp->ShowMessage("Testing Eval Speed");
             ExaminePosition(opponent);
             TestPSpeed(ScorePosition, 15000);
 #endif
         }
         else if (!XSHOGI && strcmp(s, "p") == 0)
         {
-            ShowPostnValues();
+            dsp->ShowPostnValues();
         }
         else if (!XSHOGI && strcmp(s, "debug") == 0)
         {
-            DoDebug();
+            dsp->DoDebug();
         }
         else
         {
@@ -1970,7 +1988,7 @@ InputCommand(char *command)
                 if (rpt >= 3)
                 {
                     DRAW = DRAW_REPETITION;
-                    ShowMessage(DRAW);
+                    dsp->ShowMessage(DRAW);
                     GameList[GameCnt].flags |= draw;
 
                         flag.mate = true;
@@ -2024,8 +2042,6 @@ InputCommand(char *command)
 }
 
 
-
-
 void
 SetTimeControl(void)
 {
@@ -2045,4 +2061,3 @@ SetTimeControl(void)
     et = 0;
     ElapsedTime(COMPUTE_AND_INIT_MODE);
 }
-