13 #include <windows.h>
\r
18 #include "adapter.h"
\r
22 #include "book_make.h"
\r
23 #include "book_merge.h"
\r
32 #include "move_gen.h"
\r
42 static const char * const Version = "1.4W10UCIb15";
\r
43 static const char * const HelpMessage = "\
\r
45 polyglot [configfile]\n\
\r
46 polyglot make-book [-pgn inputfile] [-bin outputfile] [-max-ply ply] [-min-game games] [-min-score score] [-only-white] [-only-black] [-uniform]\n\
\r
47 polyglot merge-book -in1 inputfile1 -in2 inputfile2 [-out outputfile]\n\
\r
48 polyglot [configfile] epd-test [-epd inputfile] [-min-depth depth] [-max-depth depth] [-max-time time] [-depth-delta delta]\n\
\r
49 polyglot perft [-fen fen] [-max-depth depth]\
\r
54 static const int SearchDepth = 63;
\r
55 static const double SearchTime = 3600.0;
\r
56 static const int StringSize = 4096;
\r
61 static void parse_option ();
\r
62 static void init_book ();
\r
63 static bool parse_line (char line[], char * * name_ptr, char * * value_ptr);
\r
64 static void stop_search ();
\r
65 static void sig_quit(int);
\r
71 int main(int argc, char * argv[]) {
\r
73 // board_t board[1];
\r
75 if(argc>=2 && ((my_string_case_equal(argv[1],"help")) || (my_string_case_equal(argv[1],"-help")) || (my_string_case_equal(argv[1],"--help")) || (my_string_case_equal(argv[1],"-h")) || my_string_case_equal(argv[1],"/?"))){
\r
76 printf("%s\n",HelpMessage);
\r
77 return EXIT_SUCCESS;
\r
83 signal(SIGINT,sig_quit);
\r
86 signal(SIGTERM,SIG_IGN);
\r
88 signal(SIGPIPE,SIG_IGN);
\r
93 printf("PolyGlot %s by Fabien Letouzey\n",Version);
\r
107 if (argc >= 2 && my_string_equal(argv[1],"make-book")) {
\r
108 book_make(argc,argv);
\r
109 return EXIT_SUCCESS;
\r
112 if (argc >= 2 && my_string_equal(argv[1],"merge-book")) {
\r
113 book_merge(argc,argv);
\r
114 return EXIT_SUCCESS;
\r
117 if (argc >= 2 && my_string_equal(argv[1],"perft")) {
\r
118 do_perft(argc,argv);
\r
119 return EXIT_SUCCESS;
\r
124 if (argc == 2) option_set("OptionFile",argv[1]); // HACK for compatibility
\r
126 parse_option(); // HACK: also launches the engine
\r
130 if (argc >= 2 && my_string_equal(argv[1],"epd-test")){
\r
131 epd_test(argc,argv);
\r
132 return EXIT_SUCCESS;
\r
133 }else if(argc >= 3 && my_string_equal(argv[2],"epd-test")){
\r
134 epd_test(argc-1,argv+1);
\r
135 return EXIT_SUCCESS;
\r
143 return EXIT_SUCCESS; // we never get here....
\r
148 static void init_book(){
\r
150 if (option_get_bool("Book")){
\r
151 my_log("POLYGLOT *** SETTING BOOK ***\n");
\r
152 my_log("POLYGLOT BOOK \"%s\"\n",option_get_string("BookFile"));
\r
153 uci_set_option(Uci,
\r
154 PolyglotBookFile, // name
\r
155 option_get_string("BookFile"), // value
\r
161 book_open(option_get_string("BookFile"));
\r
162 if(!book_is_open()){
\r
163 my_log("POLYGLOT Unable to open book \"%s\"\n",
\r
164 option_get_string("BookFile"));
\r
167 uci_set_option(Uci,
\r
168 PolyglotBookFile, // name
\r
169 "<empty>", // value
\r
181 static void parse_option() {
\r
183 const char * file_name;
\r
186 char * name, * value;
\r
187 file_name = option_get_string("OptionFile");
\r
189 file = fopen(file_name,"r");
\r
190 if (file == NULL) {
\r
191 my_fatal("Can't open file \"%s\": %s\n",file_name,strerror(errno));
\r
194 // PolyGlot options (assumed first)
\r
198 if (!my_file_read_line(file,line,256)) {
\r
199 my_fatal("parse_option(): missing [Engine] section\n");
\r
202 if(line[0]=='#') continue;
\r
204 if (my_string_case_equal(line,"[engine]")) break;
\r
206 if (parse_line(line,&name,&value)) option_set(name,value);
\r
209 if (option_get_bool("Log")) {
\r
210 my_log_open(option_get_string("LogFile"));
\r
213 my_log("POLYGLOT *** START ***\n");
\r
214 my_log("POLYGLOT INI file \"%s\"\n",file_name);
\r
215 engine_open(Engine);
\r
216 if (option_get_bool("UCI")) {
\r
217 my_log("POLYGLOT *** Switching to UCI mode ***\n");
\r
222 uci_open(Uci,Engine);
\r
223 while (my_file_read_line(file,line,256)) {
\r
225 if (line[0] == '[') my_fatal("parse_option(): unknown section %s\n",line);
\r
227 if (parse_line(line,&name,&value)) {
\r
228 uci_send_option(Uci,name,"%s",value);
\r
229 //to get a decent display in winboard_x we need to now if an engine really is doing multipv analysis
\r
230 // "multipv 1" in the pv is meaningless,f.i. toga sends that all the time
\r
231 //therefore check if MultiPV is set to a decent value in the polyglot ini file
\r
232 if(my_string_case_equal(name,"MultiPV") && atoi(value)>1) Uci->multipv_mode=true;
\r
235 if (my_string_equal(option_get_string("EngineName"),"<empty>")) {
\r
236 option_set("EngineName",Uci->name);
\r
243 static bool parse_line(char line[], char * * name_ptr, char * * value_ptr) {
\r
246 char * name, * value;
\r
248 ASSERT(line!=NULL);
\r
249 ASSERT(name_ptr!=NULL);
\r
250 ASSERT(value_ptr!=NULL);
\r
254 ptr = strchr(line,';');
\r
255 if (ptr != NULL) *ptr = '\0';
\r
257 ptr = strchr(line,'#');
\r
258 if (ptr != NULL) *ptr = '\0';
\r
262 ptr = strchr(line,'=');
\r
263 if (ptr == NULL) return false;
\r
270 while (*name == ' ') name++; // remove leading spaces
\r
272 while (ptr > name && ptr[-1] == ' ') ptr--; // remove trailing spaces
\r
275 if (*name == '\0') return false;
\r
279 ptr = &value[strlen(value)]; // pointer to string terminator
\r
281 while (*value == ' ') value++; // remove leading spaces
\r
283 while (ptr > value && ptr[-1] == ' ') ptr--; // remove trailing spaces
\r
286 if (*value == '\0') return false;
\r
291 *value_ptr = value;
\r
296 static void sig_quit(int dummy){
\r
297 my_log("POLYGLOT *** SIGINT Received ***\n");
\r
306 char string[StringSize];
\r
308 my_log("POLYGLOT *** QUIT ***\n");
\r
313 engine_send(Engine,"quit");
\r
315 // wait for the engine to quit
\r
317 engine_get(Engine,string,StringSize); // HACK: calls exit() on receiving EOF
\r
324 static void stop_search() {
\r
326 if (Init && Uci->searching) {
\r
328 ASSERT(Uci->searching);
\r
329 ASSERT(Uci->pending_nb>=1);
\r
331 my_log("POLYGLOT STOP SEARCH\n");
\r
334 engine_send(Engine,"stop");
\r
335 Uci->searching = false;
\r
338 if (option_get_bool("SyncStop")) {
\r
339 uci_send_stop_sync(Uci);
\r
341 uci_send_stop(Uci);
\r