Finish the --without-curses support.
[gnushogi.git] / gnushogi / commondsp.c
index 73afb95..55201aa 100644 (file)
@@ -16,8 +16,8 @@
  *
  * GNU Shogi is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License as published by the
- * Free Software Foundation; either version 1, or (at your option) any
- * later version.
+ * Free Software Foundation; either version 3 of the License,
+ * or (at your option) any later version.
  *
  * GNU Shogi is distributed in the hope that it will be useful, but WITHOUT
  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  * for more details.
  *
  * You should have received a copy of the GNU General Public License along
- * with GNU Shogi; see the file COPYING.  If not, write to the Free
- * Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
+ * with GNU Shogi; see the file COPYING. If not, see
+ * <http://www.gnu.org/licenses/>.
  * ----------------------------------------------------------------------
  *
  */
 
-#include "gnushogi.h"
-
 #if defined HAVE_GETTIMEOFDAY
 #include <sys/time.h>
 #endif
 
-char mvstr[4][6];
-char *InPtr;
-int InBackground = false;
-
-
 #include <ctype.h>
 #include <signal.h>
 
@@ -49,7 +42,11 @@ int InBackground = false;
 #include <sys/types.h>
 #include <sys/file.h>
 
-#include <curses.h>
+#include "gnushogi.h"
+
+char mvstr[4][6];
+char *InPtr;
+int InBackground = false;
 
 
 #if defined(BOOKTEST)
@@ -130,6 +127,17 @@ movealgbr(short m, char *s)
 
 /*
  * Generate move strings in different formats.
+ *
+ * INPUT:
+ * - f                                 piece to be moved
+ *   - 0 < f < NO_SQUARES                              source square
+ *   - NO_SQUARES <= f NO_SQUARES + 2*NO_PIECES                dropped piece modulo NO_PIECES
+ * - t & 0x7f                          target square
+ * - t & 0x80                          promotion flag
+ * - flag                              FIXME: must be zero ?
+ *
+ * OUTPUT:
+ * - GLOBAL mvstr
  */
 
 void
@@ -155,12 +163,8 @@ algbr(short f, short t, short flag)
 
     if ((f == t) && ((f != 0) || (t != 0)))
     {
-        if (!barebones)
-        {
-            if (NOT_CURSES)
-                printf("error in algbr: FROM=TO=%d, flag=0x%4x\n", t, flag);
-            else
-                printw("error in algbr: FROM=TO=%d, flag=0x%4x\n", t, flag);
+        if (!barebones) {
+            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';
@@ -293,20 +297,7 @@ VerifyMove(char *s, VerifyMove_mode iop, unsigned short *mv)
         if (SqAttacked(PieceList[opponent][0], computer, &blocked))
         {
             UnmakeMove(opponent, &xnode, &tempb, &tempc, &tempsf, &tempst);
-
-            if (NOT_CURSES)
-            {
-                /* Illegal move in check */
-                printf(CP[77], mvstr[0]);
-                printf("\n");
-            }
-            else
-            {
-                /* Illegal move in check */
-                sprintf(buffer, CP[77], s);
-                ShowMessage(buffer);
-            }
-
+            AlwaysShowMessage(CP[77], s);
             return false;
         }
         else
@@ -348,17 +339,7 @@ VerifyMove(char *s, VerifyMove_mode iop, unsigned short *mv)
         }
     }
 
-    if (NOT_CURSES)
-    {
-        /* Illegal move */
-        printf (CP[75], s);
-    }
-    else /* Curses. */
-    {
-        /* Illegal move */
-        sprintf(buffer, CP[76], s);
-        ShowMessage(buffer);
-    }
+    AlwaysShowMessage(CP[76], s);
 
     if (!barebones && (cnt > 1))
     {
@@ -391,17 +372,17 @@ parser(char *f, int side, short *fpiece)
 
     if (f[1] == '*' || f[1] == '\'')
     {
-        c2 = '9' - f[2];
-        r2 = 'i' - f[3];
+        c2 = COL_NAME(f[2]);
+        r2 = ROW_NAME(f[3]);
 
         return ((NO_SQUARES + *fpiece) << 8) | locn(r2, c2);
     }
     else
     {
-        c1 = '9' - f[1];
-        r1 = 'i' - f[2];
-        c2 = '9' - f[3];
-        r2 = 'i' - f[4];
+        c1 = COL_NAME(f[1]);
+        r1 = ROW_NAME(f[2]);
+        c2 = COL_NAME(f[3]);
+        r2 = ROW_NAME(f[4]);
         p = (f[5] == '+') ? 0x80 : 0;
 
         return (locn(r1, c1) << 8) | locn(r2, c2) | p;
@@ -439,24 +420,12 @@ GetGame(void)
     short sq;
     short side, isp;
 
-    if (savefile[0])
-    {
+    if (savefile[0]) {
         strcpy(fname, savefile);
-    }
-    else
-    {
+    } else {
         /* Enter file name */
         ShowMessage(CP[63]);
-
-        if (NOT_CURSES)
-        {
-            scanf("%s", fname);
-        }
-        else
-        {
-            fflush(stdout);
-            scanw("%s", fname);
-        }
+        RequestInputString(fname, sizeof(fname)-1);
     }
 
     /* shogi.000 */
@@ -676,24 +645,12 @@ SaveGame(void)
     short side, piece;
     char empty[2] = "\n";
 
-    if (savefile[0])
-    {
+    if (savefile[0]) {
         strcpy(fname, savefile);
-    }
-    else
-    {
+    } else {
         /* Enter file name */
         ShowMessage(CP[63]);
-
-        if (NOT_CURSES)
-        {
-            scanf("%s", fname);
-        }
-        else
-        {
-            fflush(stdout);
-            scanw("%s", fname);
-        }
+        RequestInputString(fname, sizeof(fname)-1);
     }
 
     if (fname[0] == '\0')        /* shogi.000 */
@@ -712,16 +669,16 @@ SaveGame(void)
 
         fprintf(fd, CP[37], w, b, Game50,
                 flag.force ? "force" : "");
-        fprintf(fd, empty);
+        fputs(empty, fd);
         fprintf(fd, CP[111], TCflag, OperatorTime);
         fprintf(fd, CP[117],
                 TimeControl.clock[black], TimeControl.moves[black],
                 TimeControl.clock[white], TimeControl.moves[white]);
-        fprintf(fd, empty);
+        fputs(empty, fd);
 
         for (i = NO_ROWS - 1; i > -1; i--)
         {
-            fprintf(fd, "%c ", 'i' - i);
+            fprintf(fd, "%c ", ROW_NAME(i));
 
             for (c = 0; c < NO_COLS; c++)
             {
@@ -755,9 +712,9 @@ SaveGame(void)
             fprintf(fd, "\n");
         }
 
-        fprintf(fd, empty);
-        fprintf(fd, "   9 8 7 6 5 4 3 2 1\n");
-        fprintf(fd, empty);
+        fputs(empty, fd);
+        fprintf(fd, "   9 8 7 6 5 4 3 2 1\n");    /* FIXME */
+        fputs(empty, fd);
         fprintf(fd, "   p  l  n  s  g  b  r  k\n");
 
         for (side = 0; side <= 1; side++)
@@ -774,8 +731,8 @@ SaveGame(void)
             fprintf(fd, "\n");
         }
 
-        fprintf(fd, empty);
-        fprintf(fd, CP[126]);
+        fputs(empty, fd);
+        fputs(CP[126], fd);
 
         for (i = 1; i <= GameCnt; i++)
         {
@@ -837,16 +794,7 @@ GetXGame(void)
 
     /* Enter file name */
     ShowMessage(CP[63]);
-
-    if (NOT_CURSES)
-    {
-        scanf("%s", fname);
-    }
-    else
-    {
-        fflush(stdout);
-        scanw("%s", fname);
-    }
+    RequestInputString(fname, sizeof(fname)-1);
 
     if (fname[0] == '\0') /* XSHOGI.position.read */
         strcpy(fname, CP[205]);
@@ -974,16 +922,7 @@ SaveXGame(void)
 
     /* Enter file name */
     ShowMessage(CP[63]);
-
-    if (NOT_CURSES)
-    {
-        scanf("%s", fname);
-    }
-    else
-    {
-        fflush(stdout);
-        scanw("%s", fname);
-    }
+    RequestInputString(fname, sizeof(fname)-1);
 
     if (fname[0] == '\0') /* XSHOGI.position.read */
         strcpy(fname, CP[205]);
@@ -1056,24 +995,12 @@ BookSave(void)
     char fname[256], sflags[4];
     short i, j, f, t;
 
-    if (savefile[0])
-    {
+    if (savefile[0]) {
         strcpy(fname, savefile);
-    }
-    else
-    {
+    } else {
         /* Enter file name */
         ShowMessage(CP[63]);
-
-        if (NOT_CURSES)
-        {
-            scanf("%s", fname);
-        }
-        else
-        {
-            fflush(stdout);
-            scanw("%s", fname);
-        }
+        RequestInputString(fname, sizeof(fname)-1);
     }
 
     if (fname[0] == '\0')
@@ -1205,8 +1132,8 @@ ListGame(void)
 
     /* fprintf(fd, "gnushogi game %d\n", u); */
     fprintf(fd, CP[161], version, patchlevel);
-    fprintf(fd, CP[10]);
-    fprintf(fd, CP[11]);
+    fputs(CP[10], fd);
+    fputs(CP[11], fd);
 
     for (i = 1; i <= GameCnt; i++)
     {
@@ -1434,7 +1361,7 @@ TestSpeed(void(*f)(short side, short ply,
 #endif
 
     unsigned i;
-    long cnt, rate, t1, t2;
+    long cnt, t1, t2;
 
 #ifdef HAVE_GETTIMEOFDAY
     struct timeval tv;
@@ -1474,17 +1401,7 @@ TestSpeed(void(*f)(short side, short ply,
     else
         et = 1;
 
-    rate = (((et) ? ((cnt * 100) / et) : 0));
-
-#ifdef DYNAMIC_ZNODES
-    if (rate > 0)
-        znodes = rate;
-#endif
-
-    if (NOT_CURSES)
-        printf(CP[91], cnt, rate);
-    else
-        ShowNodeCnt(cnt);
+    ShowNodeCnt(cnt);
 }
 
 
@@ -1493,7 +1410,7 @@ void
 TestPSpeed(short(*f) (short side), unsigned j)
 {
     short i;
-    long cnt, rate, t1, t2;
+    long cnt, t1, t2;
 #ifdef HAVE_GETTIMEOFDAY
     struct timeval tv;
 #endif
@@ -1522,14 +1439,7 @@ TestPSpeed(short(*f) (short side), unsigned j)
     else
         et = 1;
 
-    rate = (et) ? ((cnt * 100) / et) : 0;
-
-    /* printf("Nodes= %ld Nodes/sec= %ld\n", cnt, rate); */
-
-    if (NOT_CURSES)
-        printf(CP[91], cnt, rate);
-    else
-        ShowNodeCnt(cnt);
+    ShowNodeCnt(cnt);
 }
 
 
@@ -1658,16 +1568,7 @@ InputCommand(char *command)
             Sdepth = 0;
 
 #ifdef QUIETBACKGROUND
-            if (NOT_CURSES)
-            {
-                PromptForMove();
-            }
-            else
-            {
-                ShowSidetoMove();
-                ShowPrompt();
-            }
-
+            ShowPrompt();
             have_shown_prompt = true;
 #endif /* QUIETBACKGROUND */
 
@@ -1714,15 +1615,7 @@ InputCommand(char *command)
         {
 #endif /* QUIETBACKGROUND */
 
-            if (NOT_CURSES)
-            {
-                PromptForMove();
-            }
-            else
-            {
-                ShowSidetoMove();
-                ShowPrompt();
-            }
+            ShowPrompt();
 
 #ifdef QUIETBACKGROUND
         }
@@ -1730,23 +1623,12 @@ InputCommand(char *command)
         have_shown_prompt = false;
 #endif /* QUIETBACKGROUND */
 
-        if (command == NULL)
-        {
+        if (command == NULL) {
             if (NOT_CURSES)
-            {
-                s[0] = sx[0] = '\0';
+                s[0] = '\0';
 
-                while(!sx[0])
-                    (void)fgets(sx, 256, stdin);
-            }
-            else
-            {
-                fflush(stdout);
-                eof = (getstr(sx) == ERR);
-            }
-        }
-        else
-        {
+            eof = GetString(sx);
+        } else {
             strcpy(sx, command);
             done = true;
         }
@@ -2152,8 +2034,6 @@ InputCommand(char *command)
         printf("\n");
 #endif
     }
-
-    signal(SIGINT, TerminateSearch);
 }