Remove more duplicate variable declarations.
[gnushogi.git] / gnushogi / rawdsp.c
index e8e7b98..ba700db 100644 (file)
@@ -4,6 +4,7 @@
  * ----------------------------------------------------------------------
  * Copyright (c) 1993, 1994, 1995 Matthias Mutz
  * Copyright (c) 1999 Michael Vanier and the Free Software Foundation
+ * Copyright (c) 2008, 2013, 2014 Yann Dirson and the Free Software Foundation
  *
  * GNU SHOGI is based on GNU CHESS
  *
 #include <ctype.h>
 #include <signal.h>
 #include <stdarg.h>
+#include <stdio.h>
 #include <sys/param.h>
 #include <sys/types.h>
 #include <sys/file.h>
+#ifndef WIN32
 #include <poll.h>
 #include <unistd.h>
+#endif
 
 #include "gnushogi.h"
-#include "rawdsp.h"
-
-unsigned short MV[MAXDEPTH];
-int MSCORE;
 
-int mycnt1, mycnt2;
-char *DRAW;
-extern char *InPtr;
-extern short pscore[];
+/****************************************
+ * forward declarations
+ ****************************************/
 
+void Raw_UpdateDisplay(short f, short t, short redraw, short isspec);
 
 /****************************************
  * Trivial output functions.
@@ -57,7 +57,7 @@ extern short pscore[];
 void
 Raw_ClearScreen(void)
 {
-    if (!barebones && !XSHOGI)
+    if (!XSHOGI)
         printf("\n");
 }
 
@@ -65,10 +65,8 @@ Raw_ClearScreen(void)
 void
 Raw_ShowPrompt(void)
 {
-    if (!barebones && !XSHOGI)
-    {
+    if (!XSHOGI)
         fputs("\nYour move is? ", stdout);
-    }
 }
 
 
@@ -81,11 +79,8 @@ Raw_ShowCurrentMove(short pnt, short f, short t)
 void
 Raw_ShowDepth(char ch)
 {
-    if (!barebones && !XSHOGI)
-    {
-        printf("Depth= %d%c ", Sdepth, ch);
-        printf("\n");
-    }
+    if (!XSHOGI)
+        printf("Depth= %d%c \n", Sdepth, ch);
 }
 
 
@@ -124,17 +119,23 @@ Raw_ShowMessage(char *s)
 
 
 void
-Raw_AlwaysShowMessage(const char *format, va_list ap)
+Raw_AlwaysShowMessage(const char *format, ...)
 {
+    va_list ap;
+    va_start(ap, format);
     vprintf(format, ap);
     printf("\n");
+    va_end(ap);
 }
 
 
 void
-Raw_Printf(const char *format, va_list ap)
+Raw_Printf(const char *format, ...)
 {
+    va_list ap;
+    va_start(ap, format);
     vprintf(format, ap);
+    va_end(ap);
 }
 
 
@@ -149,10 +150,17 @@ int
 Raw_GetString(char* sx)
 {
     int eof = 0;
+    char *nl;
     sx[0] = '\0';
 
     while(!eof && !sx[0])
         eof = (fgets(sx, 80, stdin) == NULL);
+
+    /* remove any trailing newline */
+    nl = strchr(sx, '\n');
+    if (nl)
+        nl[0] = '\0';
+
     return eof;
 }
 
@@ -217,15 +225,20 @@ Raw_Initialize(void)
 
     if (XSHOGI)
     {
-#ifdef HAVE_SETLINEBUF
-        setlinebuf(stdout);
+#ifdef WIN32
+        /* needed because of inconsistency between MSVC run-time system and gcc includes */
+        setbuf(stdout, NULL);
 #else
-#  ifdef HAVE_SETVBUF
+#ifdef HAVE_SETVBUF
         setvbuf(stdout, NULL, _IOLBF, BUFSIZ);
+#else
+#  ifdef HAVE_SETLINEBUF
+        setlinebuf(stdout);
 #  else
-#    error "Need setlinebuf() or setvbuf() to compile gnushogi!"
+#    error "Need setvbuf() or setlinebuf() to compile gnushogi!"
 #  endif
 #endif
+#endif
         printf("GNU Shogi %s\n", PACKAGE_VERSION);
     }
 
@@ -251,19 +264,6 @@ Raw_ExitShogi(void)
 
 
 void
-Raw_Die(int sig)
-{
-    char s[80];
-
-    Raw_ShowMessage("Abort? ");
-    scanf("%s", s);
-
-    if (strcmp(s, "yes") == 0)
-        Raw_ExitShogi();
-}
-
-
-void
 Raw_TerminateSearch(int sig)
 {
 #ifdef INTERRUPT_TEST
@@ -282,30 +282,48 @@ Raw_help(void)
 {
     Raw_ClearScreen();
     printf("GNU Shogi %s command summary\n", PACKAGE_VERSION);
-    printf("----------------------------------"
-           "------------------------------\n");
-    fputs ("7g7f      move from 7g to 7f      quit      Exit Shogi\n", stdout);
-    printf("S6h       move silver to 6h       beep      turn %s\n", (flag.beep) ? "OFF" : "ON");
-    printf("2d2c+     move to 2c and promote  material  turn %s\n", (flag.material) ? "OFF" : "ON");
-    printf("P*5e      drop pawn to 5e         easy      turn %s\n", (flag.easy) ? "OFF" : "ON");
-    printf("tsume     toggle tsume mode       hash      turn %s\n", (flag.hash) ? "OFF" : "ON");
-    fputs ("bd        redraw board            reverse   board display\n", stdout);
-    printf("list      game to shogi.lst       book      turn %s used %d of %d\n", (Book) ? "OFF" : "ON", bookcount, booksize);
-    fputs ("undo      undo last ply           remove    take back a move\n", stdout);
-    fputs ("edit      edit board              force     toggle manual move mode\n", stdout);
-    fputs ("switch    sides with computer     both      computer match\n", stdout);
-    fputs ("black     computer plays black    white     computer plays white\n", stdout);
-    fputs ("depth     set search depth        clock     set time control\n", stdout);
-    fputs ("post      principle variation     hint      suggest a move\n", stdout);
-    fputs ("save      game to file            get       game from file\n", stdout);
-    printf("xsave     pos. to xshogi file     xget"
-           "      pos. from xshogi file\n");
-    fputs("random    randomize play          new       start new game\n", stdout);
-    printf("--------------------------------"
-           "--------------------------------\n");
+    printf("----------------------------------------------------------------\n");
+    printf("7g7f      move from 7g to 7f      quit      Exit Shogi\n");
+    printf("S6h       move silver to 6h       beep      turn %s\n",
+           (flag.beep) ? "OFF" : "ON");
+    printf("2d2c+     move to 2c and promote  material  turn %s\n",
+           (flag.material) ? "OFF" : "ON");
+    printf("P*5e      drop pawn to 5e         easy      turn %s\n",
+           (flag.easy) ? "OFF" : "ON");
+    printf("tsume     toggle tsume mode       hash      turn %s\n",
+           (flag.hash) ? "OFF" : "ON");
+    printf("bd        redraw board            reverse   board display\n");
+    printf("list      game to shogi.lst       book      turn %s used %d of %d\n",
+           (Book) ? "OFF" : "ON", bookcount, booksize);
+    printf("undo      undo last ply           remove    take back a move\n");
+    printf("edit      edit board              force     toggle manual move mode\n");
+    printf("switch    sides with computer     both      computer match\n");
+    printf("black     computer plays black    white     computer plays white\n");
+    printf("sd        set search depth        clock     set time control\n");
+    printf("post      principle variation     hint      suggest a move\n");
+    printf("save      game to file            get       game from file\n");
+    printf("xsave     pos. to xshogi file     xget      pos. from xshogi file\n");
+    printf("random    randomize play          new       start new game\n");
+    printf("setup                             first     \n");
+    printf("go        computer plays now      material  turn %s\n",
+           flag.material ? "OFF" : "ON");
+    printf("level     time control            gamein    \n");
+    printf("time      set engine clock        otime     set opponent clock\n");
+    printf("Awindow                           Bwindow     \n");
+    printf("rcptr     turn %3s                bsave     book save\n",
+           flag.rcptr ? "OFF" : "ON ");
+    printf("hashdepth                         hard      turn easy OFF\n");
+    printf("contempt                          xwndw                  \n");
+    printf("rv        turn %3s                coords    turn %s\n",
+           flag.rv ? "OFF" : "ON ", flag.coords ? "OFF" : "ON");
+    printf("stars     turn %3s                moves                  \n",
+           flag.stars ? "OFF" : "ON ");
+    printf("test                              p                      \n");
+    printf("debug                             depth     alias for 'sd'\n");
+    printf("----------------------------------------------------------------\n");
     printf("Computer: %-12s Opponent:            %s\n",
            ColorStr[computer], ColorStr[opponent]);
-    printf("Depth:    %-12d Response time:       %d sec\n",
+    printf("Depth:    %-12d Response time:       %ld sec\n",
            MaxSearchDepth, MaxResponseTime/100);
     printf("Random:   %-12s Easy mode:           %s\n",
            (dither) ? "ON" : "OFF", (flag.easy) ? "ON" : "OFF");
@@ -313,7 +331,7 @@ Raw_help(void)
            (flag.beep) ? "ON" : "OFF", (flag.hash) ? "ON" : "OFF");
     printf("Tsume:    %-12s Force:               %s\n",
            (flag.tsume) ? "ON" : "OFF", (flag.force) ? "ON" : "OFF");
-    printf("Time Control %s %d moves %d sec %d add %d depth\n",
+    printf("Time Control %s %d moves %ld sec %d add %d depth\n",
            (TCflag) ? "ON" : "OFF",
            TimeControl.moves[black], TimeControl.clock[black] / 100,
            TCadd/100, MaxSearchDepth);
@@ -500,15 +518,11 @@ Raw_OutputMove(void)
         goto nomove;
 
     if (XSHOGI)
-    {
         /* add remaining time in milliseconds to xshogi */
         printf("%d. ... %s %ld\n", ++mycnt1, mvstr[0],
                (TimeControl.clock[player] - et) * 10);
-    }
     else
-    {
         printf("%d. ... %s\n", ++mycnt1, mvstr[0]);
-    }
 
  nomove:
     if ((root->flags & draw) || (root->score == -(SCORE_LIMIT + 999))
@@ -553,28 +567,18 @@ Raw_OutputMove(void)
 
  summary:
     if (root->flags & draw)
-    {
         fputs("Drawn game!\n", stdout);
-    }
     else if (root->score == -(SCORE_LIMIT + 999))
-    {
         printf("%s mates!\n", ColorStr[opponent]);
-    }
     else if (root->score == (SCORE_LIMIT + 998))
-    {
         printf("%s mates!\n", ColorStr[computer]);
-    }
 #ifdef VERYBUGGY
-    else if (!barebones && (root->score < -SCORE_LIMIT))
-    {
+    else if (!XSHOGI && (root->score < -SCORE_LIMIT))
         printf("%s has a forced mate in %d moves!\n",
                ColorStr[opponent], SCORE_LIMIT + 999 + root->score - 1);
-    }
-    else if (!barebones && (root->score > SCORE_LIMIT))
-    {
+    else if (!XSHOGI && (root->score > SCORE_LIMIT))
         printf("%s has a forced mate in %d moves!\n",
                ColorStr[computer], SCORE_LIMIT + 998 - root->score - 1);
-    }
 #endif /* VERYBUGGY */
 }
 
@@ -680,28 +684,17 @@ Raw_GiveHint(void)
 void
 Raw_SelectLevel(char *sx)
 {
+    /* FIXME: NO_SQUARES is nonsense here */
+    char T[NO_SQUARES + 1], *p;
 
-    char T[NO_SQUARES + 1], *p, *q;
-
-    if ((p = strstr(sx, "level")) != NULL)
-        p += strlen("level");
-    else if ((p = strstr(sx, "clock")) != NULL)
-        p += strlen("clock");
-
-    strcat(sx, "XX");
-    q = T;
-    *q = '\0';
-
-    for (; *p != 'X'; *q++ = *p++);
+    strncpy(T, sx, NO_SQUARES);
+    T[NO_SQUARES] = '\0';
 
-    *q = '\0';
-
-    /* line empty ask for input */
+    /* if line empty, ask for input */
     if (!T[0])
     {
         fputs("Enter #moves #minutes: ", stdout);
         fgets(T, NO_SQUARES + 1, stdin);
-        strcat(T, "XX");
     }
 
     /* skip blackspace */
@@ -711,6 +704,7 @@ Raw_SelectLevel(char *sx)
     if (*p == 'f')
     {
         /* its a fischer clock game */
+        char *q;
         p++;
         TCminutes = (short)strtol(p, &q, 10);
         TCadd = (short)strtol(q, NULL, 10) *100;
@@ -720,6 +714,7 @@ Raw_SelectLevel(char *sx)
     else
     {
         /* regular game */
+        char *q;
         TCadd = 0;
         TCmoves = (short)strtol(p, &q, 10);
         TCminutes = (short)strtol(q, &q, 10);
@@ -760,10 +755,16 @@ Raw_SelectLevel(char *sx)
 
 
 void
-Raw_ChangeSearchDepth(void)
+Raw_ChangeSearchDepth(char *sx)
 {
-    printf("depth = ");
-    scanf("%hd", &MaxSearchDepth);
+    char buf[80+1];
+    strncpy(buf, sx, 80); buf[80] = '\0';
+    /* if line empty, ask for input */
+    if (!buf[0]) {
+        printf("depth = ");
+        fgets(buf, 80+1, stdin);
+    }
+    sscanf(buf, "%hd", &MaxSearchDepth);
     TCflag = !(MaxSearchDepth > 0);
 }
 
@@ -801,8 +802,7 @@ Raw_ChangeXwindow(void)
 void
 Raw_ShowPostnValue(short sq)
 {
-    short score;
-    score = ScorePosition(color[sq]);
+    (void) ScorePosition(color[sq]);
 
     if (color[sq] != neutral)
     {
@@ -944,16 +944,66 @@ Raw_ShowPostnValues(void)
 void
 Raw_PollForInput(void)
 {
-    static struct pollfd pollfds[1] = { /* [0] = */ { /* .fd = */ STDIN_FILENO,
-                                                      /* .events = */ POLLIN } };
+#ifdef WIN32
+    DWORD cnt;
+    if (!PeekNamedPipe(GetStdHandle(STD_INPUT_HANDLE), NULL, 0, NULL, &cnt, NULL))
+        cnt = 1;
+#else
+    static struct pollfd pollfds[1] = { [0] = { .fd = STDIN_FILENO,
+                                                .events = POLLIN } };
     int cnt = poll(pollfds, sizeof(pollfds)/sizeof(pollfds[0]), 0);
     if (cnt < 0) {
         perror("polling standard input");
-        ExitShogi();
+        Raw_ExitShogi();
     }
+#endif
     if (cnt) { /* if anything to read, or error occured */
         if (!flag.timeout)
             flag.back = true; /* previous: flag.timeout = true; */
         flag.bothsides = false;
     }
 }
+
+struct display raw_display =
+{
+    .ChangeAlphaWindow    = Raw_ChangeAlphaWindow,
+    .ChangeBetaWindow     = Raw_ChangeBetaWindow,
+    .ChangeHashDepth      = Raw_ChangeHashDepth,
+    .ChangeSearchDepth    = Raw_ChangeSearchDepth,
+    .ChangeXwindow        = Raw_ChangeXwindow,
+    .ClearScreen          = Raw_ClearScreen,
+    .DoDebug              = Raw_DoDebug,
+    .DoTable              = Raw_DoTable,
+    .EditBoard            = Raw_EditBoard,
+    .ExitShogi            = Raw_ExitShogi,
+    .GiveHint             = Raw_GiveHint,
+    .Initialize           = Raw_Initialize,
+    .ShowNodeCnt          = Raw_ShowNodeCnt,
+    .OutputMove           = Raw_OutputMove,
+    .PollForInput         = Raw_PollForInput,
+    .SetContempt          = Raw_SetContempt,
+    .SearchStartStuff     = Raw_SearchStartStuff,
+    .SelectLevel          = Raw_SelectLevel,
+    .ShowCurrentMove      = Raw_ShowCurrentMove,
+    .ShowDepth            = Raw_ShowDepth,
+    .ShowGameType         = Raw_ShowGameType,
+    .ShowLine             = Raw_ShowLine,
+    .ShowMessage          = Raw_ShowMessage,
+    .AlwaysShowMessage    = Raw_AlwaysShowMessage,
+    .Printf               = Raw_Printf,
+    .doRequestInputString = Raw_doRequestInputString,
+    .GetString            = Raw_GetString,
+    .SetupBoard           = Raw_SetupBoard,
+    .ShowPatternCount     = Raw_ShowPatternCount,
+    .ShowPostnValue       = Raw_ShowPostnValue,
+    .ShowPostnValues      = Raw_ShowPostnValues,
+    .ShowPrompt           = Raw_ShowPrompt,
+    .ShowResponseTime     = Raw_ShowResponseTime,
+    .ShowResults          = Raw_ShowResults,
+    .ShowSidetoMove       = Raw_ShowSidetoMove,
+    .ShowStage            = Raw_ShowStage,
+    .TerminateSearch      = Raw_TerminateSearch,
+    .UpdateClocks         = Raw_UpdateClocks,
+    .UpdateDisplay        = Raw_UpdateDisplay,
+    .help                 = Raw_help,
+};