version 1.4w10UCIb17
[polyglot.git] / main.cpp
index ce6c235..b46be4a 100644 (file)
--- a/main.cpp
+++ b/main.cpp
@@ -4,18 +4,10 @@
 // includes\r
 \r
 #include <cerrno>\r
-#include <csignal>\r
 #include <cstdio>\r
 #include <cstdlib>\r
 #include <cstring>\r
 \r
-#ifdef _WIN32\r
-  #include <windows.h>\r
-#else\r
-  #include <unistd.h>\r
-#endif\r
-\r
-#include "adapter.h"\r
 #include "attack.h"\r
 #include "board.h"\r
 #include "book.h"\r
@@ -28,6 +20,7 @@
 #include "hash.h"\r
 #include "list.h"\r
 #include "main.h"\r
+#include "mainloop.h"\r
 #include "move.h"\r
 #include "move_gen.h"\r
 #include "option.h"\r
 #include "square.h"\r
 #include "uci.h"\r
 #include "util.h"\r
+#include "xboard2uci.h"\r
+#include "uci2uci.h"\r
+\r
 // constants\r
 \r
 \r
-static const char * const Version = "1.4W10UCIb16";\r
+static const char * const Version = "1.4W10UCIb17";\r
 static const char * const HelpMessage = "\\r
 SYNTAX\n\\r
 polyglot [configfile]\n\\r
@@ -49,13 +45,14 @@ polyglot [configfile] epd-test [-epd inputfile] [-min-depth depth] [-max-depth d
 polyglot perft [-fen fen] [-max-depth depth]\\r
 ";\r
 \r
-\r
-\r
 static const int SearchDepth = 63;\r
 static const double SearchTime = 3600.0;\r
 static const int StringSize = 4096;\r
+\r
 // variables\r
+\r
 static bool Init;\r
+\r
 // prototypes\r
 \r
 static void parse_option ();\r
@@ -78,82 +75,77 @@ int main(int argc, char * argv[]) {
     }\r
    // init\r
 \r
-   Init = false;\r
-\r
-   signal(SIGINT,sig_quit);\r
-\r
-#ifdef _WIN32\r
-      signal(SIGTERM,SIG_IGN);\r
-#ifdef SIGPIPE\r
-      signal(SIGPIPE,SIG_IGN);\r
-#endif\r
-#endif\r
-\r
-   util_init();\r
-   printf("PolyGlot %s by Fabien Letouzey\n",Version);\r
-\r
-   option_init();\r
-\r
-   square_init();\r
-   piece_init();\r
-   attack_init();\r
-\r
-   hash_init();\r
-\r
-   my_random_init();\r
-\r
-   // build book\r
-\r
-   if (argc >= 2 && my_string_equal(argv[1],"make-book")) {\r
-      book_make(argc,argv);\r
-      return EXIT_SUCCESS;\r
-   }\r
-\r
-   if (argc >= 2 && my_string_equal(argv[1],"merge-book")) {\r
-      book_merge(argc,argv);\r
-      return EXIT_SUCCESS;\r
-   }\r
-\r
-   if (argc >= 2 && my_string_equal(argv[1],"perft")) {\r
-      do_perft(argc,argv);\r
-      return EXIT_SUCCESS;\r
-   }\r
-\r
-   if (argc >= 3 && my_string_equal(argv[1],"-ec")) {\r
-       option_set("EngineCommand",argv[2]);\r
-       Init=true;\r
-       engine_open(Engine);\r
-       gui_init(GUI);\r
-       uci_open(Uci,Engine);\r
-       if (my_string_equal(option_get_string("EngineName"),"<empty>")) {\r
+    Init = false;\r
+\r
+    util_init();\r
+    printf("PolyGlot %s by Fabien Letouzey\n",Version);\r
+    \r
+    option_init();\r
+    \r
+    square_init();\r
+    piece_init();\r
+    attack_init();\r
+    \r
+    hash_init();\r
+    \r
+    my_random_init();\r
+\r
+        // build book\r
+    \r
+    if (argc >= 2 && my_string_equal(argv[1],"make-book")) {\r
+        book_make(argc,argv);\r
+        return EXIT_SUCCESS;\r
+    }\r
+    \r
+    if (argc >= 2 && my_string_equal(argv[1],"merge-book")) {\r
+        book_merge(argc,argv);\r
+        return EXIT_SUCCESS;\r
+    }\r
+    \r
+    if (argc >= 2 && my_string_equal(argv[1],"perft")) {\r
+        do_perft(argc,argv);\r
+        return EXIT_SUCCESS;\r
+    }\r
+    \r
+    if (argc >= 3 && my_string_equal(argv[1],"-ec")) {\r
+        option_set("EngineCommand",argv[2]);\r
+        engine_open(Engine);\r
+        if(!engine_active(Engine)){\r
+            my_fatal("Could not start \"%s\"\n",option_get("EngineCommand"));\r
+        }\r
+        Init=true;\r
+        gui_init(GUI);\r
+        uci_open(Uci,Engine);\r
+        if (my_string_equal(option_get_string("EngineName"),"<empty>")) {\r
             option_set("EngineName",Uci->name);\r
-       }\r
-       adapter_loop();\r
-       return EXIT_SUCCESS; // we don't get here\r
-   }\r
-\r
-   // read options\r
-\r
-   if (argc == 2) option_set("OptionFile",argv[1]); // HACK for compatibility\r
-\r
-   parse_option(); // HACK: also launches the engine\r
+        }\r
+        mainloop();\r
+        return EXIT_SUCCESS; \r
+    }\r
+    \r
+        // read options\r
+    \r
+    if (argc == 2) option_set("OptionFile",argv[1]); // HACK for compatibility\r
+\r
+    parse_option(); // HACK: also launches the engine\r
+    \r
+        // EPD test\r
+    \r
+    if (argc >= 2 && my_string_equal(argv[1],"epd-test")){\r
+        epd_test(argc,argv);\r
+        return EXIT_SUCCESS;\r
+    }else if(argc >= 3 && my_string_equal(argv[2],"epd-test")){\r
+        epd_test(argc-1,argv+1);\r
+        return EXIT_SUCCESS;\r
+    }\r
+    \r
+    if (argc >= 3) my_fatal("Too many arguments\n");\r
 \r
-   // EPD test\r
 \r
-   if (argc >= 2 && my_string_equal(argv[1],"epd-test")){\r
-       epd_test(argc,argv);\r
-       return EXIT_SUCCESS;\r
-   }else if(argc >= 3 && my_string_equal(argv[2],"epd-test")){\r
-       epd_test(argc-1,argv+1);\r
-       return EXIT_SUCCESS;\r
-   }\r
-   \r
-   init_book();\r
-   // adapter\r
-   \r
-   gui_init(GUI);\r
-   adapter_loop();\r
-   return EXIT_SUCCESS;  // we never get here....\r
+    init_book();\r
+    gui_init(GUI);\r
+    mainloop();\r
+    return EXIT_SUCCESS; \r
 }\r
 \r
 // polyglot_set_option\r
@@ -210,49 +202,51 @@ static void init_book(){
 \r
 static void parse_option() {\r
 \r
-   const char * file_name;\r
-   FILE * file;\r
-   char line[256];\r
-   char * name, * value;\r
+    const char * file_name;\r
+    FILE * file;\r
+    char line[256];\r
+    char * name, * value;\r
     file_name = option_get_string("OptionFile");\r
-\r
-   file = fopen(file_name,"r");\r
-   if (file == NULL) {\r
-       my_fatal("Can't open file \"%s\": %s\n",file_name,strerror(errno));\r
-   }\r
-\r
-   // PolyGlot options (assumed first)\r
-\r
+    \r
+    file = fopen(file_name,"r");\r
+    if (file == NULL) {\r
+        my_fatal("Can't open file \"%s\": %s\n",file_name,strerror(errno));\r
+    }\r
+    \r
+        // PolyGlot options (assumed first)\r
+    \r
    while (true) {\r
-\r
-      if (!my_file_read_line(file,line,256)) {\r
-         my_fatal("parse_option(): missing [Engine] section\n");\r
-      }\r
-\r
-      if(line[0]=='#') continue;\r
-\r
-      if (my_string_case_equal(line,"[engine]")) break;\r
-\r
-      if (parse_line(line,&name,&value)) {\r
-          option_set(name,value);\r
-          option_set_default(name,value);\r
-      }\r
+       \r
+       if (!my_file_read_line(file,line,256)) {\r
+           my_fatal("parse_option(): missing [Engine] section\n");\r
+       }\r
+       \r
+       if(line[0]=='#') continue;\r
+       \r
+       if (my_string_case_equal(line,"[engine]")) break;\r
+       \r
+       if (parse_line(line,&name,&value)) {\r
+           option_set(name,value);\r
+           option_set_default(name,value);\r
+       }\r
    }\r
-\r
+   \r
    if (option_get_bool("Log")) {\r
-      my_log_open(option_get_string("LogFile"));\r
+       my_log_open(option_get_string("LogFile"));\r
    }\r
-\r
+   \r
    my_log("POLYGLOT *** START ***\n");\r
    my_log("POLYGLOT INI file \"%s\"\n",file_name);\r
    engine_open(Engine);\r
+   if(!engine_active(Engine)){\r
+       my_fatal("Could not start \"%s\"",option_get("EngineCommand"));\r
+   }\r
+\r
    if (option_get_bool("UCI")) {\r
        my_log("POLYGLOT *** Switching to UCI mode ***\n");\r
    }\r
-\r
\r
-   Init = true;\r
    uci_open(Uci,Engine);\r
+   Init = true;\r
    while (my_file_read_line(file,line,256)) {\r
        if (line[0] == '[') my_fatal("parse_option(): unknown section %s\n",line);\r
        if (line[0]=='#') continue;\r
@@ -266,117 +260,102 @@ static void parse_option() {
        }\r
    }\r
    if (my_string_equal(option_get_string("EngineName"),"<empty>")) {\r
-           option_set("EngineName",Uci->name);\r
+       option_set("EngineName",Uci->name);\r
    }\r
-\r
+   \r
    fclose(file);\r
 }\r
 \r
 // parse_line()\r
 \r
 static bool parse_line(char line[], char * * name_ptr, char * * value_ptr) {\r
-\r
-   char * ptr;\r
-   char * name, * value;\r
-\r
-   ASSERT(line!=NULL);\r
-   ASSERT(name_ptr!=NULL);\r
-   ASSERT(value_ptr!=NULL);\r
-\r
-   // remove comments\r
-\r
-   ptr = strchr(line,';');\r
-   if (ptr != NULL) *ptr = '\0';\r
-\r
-   ptr = strchr(line,'#');\r
-   if (ptr != NULL) *ptr = '\0';\r
-\r
-   // split at '='\r
-\r
-   ptr = strchr(line,'=');\r
-   if (ptr == NULL) return false;\r
-\r
-   name = line;\r
-   value = ptr+1;\r
-\r
-   // cleanup name\r
-\r
-   while (*name == ' ') name++; // remove leading spaces\r
-\r
-   while (ptr > name && ptr[-1] == ' ') ptr--; // remove trailing spaces\r
-   *ptr = '\0';\r
-\r
-   if (*name == '\0') return false;\r
-\r
-   // cleanup value\r
-\r
-   ptr = &value[strlen(value)]; // pointer to string terminator\r
-\r
-   while (*value == ' ') value++; // remove leading spaces\r
-\r
-   while (ptr > value && ptr[-1] == ' ') ptr--; // remove trailing spaces\r
-   *ptr = '\0';\r
-\r
-   if (*value == '\0') return false;\r
-\r
-   // end\r
-\r
-   *name_ptr = name;\r
-   *value_ptr = value;\r
-\r
-   return true;\r
-}\r
-\r
-static void sig_quit(int dummy){\r
-    my_log("POLYGLOT *** SIGINT Received ***\n");\r
-    quit();\r
+    \r
+    char * ptr;\r
+    char * name, * value;\r
+    \r
+    ASSERT(line!=NULL);\r
+    ASSERT(name_ptr!=NULL);\r
+    ASSERT(value_ptr!=NULL);\r
+    \r
+        // remove comments\r
+    \r
+    ptr = strchr(line,';');\r
+    if (ptr != NULL) *ptr = '\0';\r
+    \r
+    ptr = strchr(line,'#');\r
+    if (ptr != NULL) *ptr = '\0';\r
+    \r
+        // split at '='\r
+    \r
+    ptr = strchr(line,'=');\r
+    if (ptr == NULL) return false;\r
+    \r
+    name = line;\r
+    value = ptr+1;\r
+   \r
+        // cleanup name\r
+    \r
+    while (*name == ' ') name++; // remove leading spaces\r
+    \r
+    while (ptr > name && ptr[-1] == ' ') ptr--; // remove trailing spaces\r
+    *ptr = '\0';\r
+    \r
+    if (*name == '\0') return false;\r
+    \r
+        // cleanup value\r
+    \r
+    ptr = &value[strlen(value)]; // pointer to string terminator\r
+    \r
+    while (*value == ' ') value++; // remove leading spaces\r
+    \r
+    while (ptr > value && ptr[-1] == ' ') ptr--; // remove trailing spaces\r
+    *ptr = '\0';\r
+    \r
+    if (*value == '\0') return false;\r
+   \r
+        // end\r
+    \r
+    *name_ptr = name;\r
+    *value_ptr = value;\r
+    \r
+    return true;\r
 }\r
 \r
-\r
 // quit()\r
 \r
 void quit() {\r
 \r
-   char string[StringSize];\r
-\r
-   my_log("POLYGLOT *** QUIT ***\n");\r
-\r
-   if (Init) {\r
-\r
-      stop_search();\r
-      engine_send(Engine,"quit");\r
-\r
-      // wait for the engine to quit\r
-      while (true) {\r
-         engine_get(Engine,string,StringSize); // HACK: calls exit() on receiving EOF\r
-      }\r
-   }\r
-   exit(EXIT_SUCCESS);\r
+    my_log("POLYGLOT *** QUIT ***\n");\r
+    \r
+    if (Init) {\r
+        \r
+        stop_search();\r
+        engine_send(Engine,"quit");\r
+        engine_close(Engine);\r
+        \r
+    }\r
+    exit(EXIT_SUCCESS);\r
 }\r
 \r
 // stop_search()\r
 \r
 static void stop_search() {\r
-\r
-   if (Init && Uci->searching) {\r
-\r
-      ASSERT(Uci->searching);\r
-      ASSERT(Uci->pending_nb>=1);\r
-\r
-      my_log("POLYGLOT STOP SEARCH\n");\r
-\r
-/*\r
-      engine_send(Engine,"stop");\r
-      Uci->searching = false;\r
-*/\r
-\r
-      if (option_get_bool("SyncStop")) {\r
-         uci_send_stop_sync(Uci);\r
-      } else {\r
-         uci_send_stop(Uci);\r
-      }\r
-   }\r
+    \r
+    if (Init && Uci->searching) {\r
+        \r
+        ASSERT(Uci->searching);\r
+        ASSERT(Uci->pending_nb>=1);\r
+        \r
+        my_log("POLYGLOT STOP SEARCH\n");\r
+        \r
+        if (option_get_bool("SyncStop")) {\r
+            uci_send_stop_sync(Uci);\r
+        } else {\r
+            uci_send_stop(Uci);\r
+        }\r
+    }\r
 }\r
 \r
+\r
 // end of main.cpp\r
 \r