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