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.4W10UCIb16";
\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
122 if (argc >= 3 && my_string_equal(argv[1],"-ec")) {
\r
123 option_set("EngineCommand",argv[2]);
\r
125 engine_open(Engine);
\r
127 uci_open(Uci,Engine);
\r
128 if (my_string_equal(option_get_string("EngineName"),"<empty>")) {
\r
129 option_set("EngineName",Uci->name);
\r
132 return EXIT_SUCCESS; // we don't get here
\r
137 if (argc == 2) option_set("OptionFile",argv[1]); // HACK for compatibility
\r
139 parse_option(); // HACK: also launches the engine
\r
143 if (argc >= 2 && my_string_equal(argv[1],"epd-test")){
\r
144 epd_test(argc,argv);
\r
145 return EXIT_SUCCESS;
\r
146 }else if(argc >= 3 && my_string_equal(argv[2],"epd-test")){
\r
147 epd_test(argc-1,argv+1);
\r
148 return EXIT_SUCCESS;
\r
156 return EXIT_SUCCESS; // we never get here....
\r
159 // polyglot_set_option
\r
161 void polyglot_set_option(char *name, char *value){ // this must be cleaned up!
\r
162 option_set(name,value);
\r
163 if(option_get_bool("Book")&&(my_string_case_equal(name,"BookFile")||my_string_case_equal(name,"Book"))){
\r
164 my_log("POLYGLOT *** SETTING BOOK ***\n");
\r
165 my_log("POLYGLOT BOOK \"%s\"\n",option_get_string("BookFile"));
\r
168 book_open(option_get_string("BookFile"));
\r
169 if(!book_is_open()){
\r
170 my_log("POLYGLOT Unable to open book \"%s\"\n",option_get_string("BookFile"));
\r
172 }else if(option_get_bool("Log")&&(my_string_case_equal(name,"LogFile") ||my_string_case_equal(name,"Log"))){
\r
173 my_log("POLYGLOT *** SETTING LOGFILE ***\n");
\r
174 my_log("POLYGLOT LOGFILE \"%s\"\n",option_get_string("LogFile"));
\r
176 my_log_open(option_get_string("LogFile"));
\r
177 }else if(option_get_bool("UseNice") &&(my_string_case_equal(name,"NiceValue")||my_string_case_equal(name,"UseNice"))){
\r
178 my_log("POLYGLOT Adjust Engine Piority\n");
\r
179 engine_set_nice_value(Engine,atoi(option_get_string("NiceValue")));
\r
180 }else if(my_string_case_equal(name,"Book") && !option_get_bool("Book")){
\r
183 }else if(my_string_case_equal(name,"UseNice") && !option_get_bool("UseNice")){
\r
184 my_log("POLYGLOT Adjust Engine Piority\n");
\r
185 engine_set_nice_value(Engine,0);
\r
186 }else if(my_string_case_equal(name,"Log") && !option_get_bool("Log")){
\r
187 my_log("POLYGLOT QUIT LOGGING\n");
\r
195 static void init_book(){
\r
196 const char *empty_var[]={};
\r
198 if (option_get_bool("Book")){
\r
199 my_log("POLYGLOT *** SETTING BOOK ***\n");
\r
200 my_log("POLYGLOT BOOK \"%s\"\n",option_get_string("BookFile"));
\r
201 book_open(option_get_string("BookFile"));
\r
202 if(!book_is_open()){
\r
203 my_log("POLYGLOT Unable to open book \"%s\"\n",
\r
204 option_get_string("BookFile"));
\r
211 static void parse_option() {
\r
213 const char * file_name;
\r
216 char * name, * value;
\r
217 file_name = option_get_string("OptionFile");
\r
219 file = fopen(file_name,"r");
\r
220 if (file == NULL) {
\r
221 my_fatal("Can't open file \"%s\": %s\n",file_name,strerror(errno));
\r
224 // PolyGlot options (assumed first)
\r
228 if (!my_file_read_line(file,line,256)) {
\r
229 my_fatal("parse_option(): missing [Engine] section\n");
\r
232 if(line[0]=='#') continue;
\r
234 if (my_string_case_equal(line,"[engine]")) break;
\r
236 if (parse_line(line,&name,&value)) {
\r
237 option_set(name,value);
\r
238 option_set_default(name,value);
\r
242 if (option_get_bool("Log")) {
\r
243 my_log_open(option_get_string("LogFile"));
\r
246 my_log("POLYGLOT *** START ***\n");
\r
247 my_log("POLYGLOT INI file \"%s\"\n",file_name);
\r
248 engine_open(Engine);
\r
249 if (option_get_bool("UCI")) {
\r
250 my_log("POLYGLOT *** Switching to UCI mode ***\n");
\r
255 uci_open(Uci,Engine);
\r
256 while (my_file_read_line(file,line,256)) {
\r
257 if (line[0] == '[') my_fatal("parse_option(): unknown section %s\n",line);
\r
258 if (line[0]=='#') continue;
\r
260 if (parse_line(line,&name,&value)) {
\r
261 uci_send_option(Uci,name,"%s",value);
\r
262 //to get a decent display in winboard_x we need to now if an engine really is doing multipv analysis
\r
263 // "multipv 1" in the pv is meaningless,f.i. toga sends that all the time
\r
264 //therefore check if MultiPV is set to a decent value in the polyglot ini file
\r
265 if(my_string_case_equal(name,"MultiPV") && atoi(value)>1) Uci->multipv_mode=true;
\r
268 if (my_string_equal(option_get_string("EngineName"),"<empty>")) {
\r
269 option_set("EngineName",Uci->name);
\r
277 static bool parse_line(char line[], char * * name_ptr, char * * value_ptr) {
\r
280 char * name, * value;
\r
282 ASSERT(line!=NULL);
\r
283 ASSERT(name_ptr!=NULL);
\r
284 ASSERT(value_ptr!=NULL);
\r
288 ptr = strchr(line,';');
\r
289 if (ptr != NULL) *ptr = '\0';
\r
291 ptr = strchr(line,'#');
\r
292 if (ptr != NULL) *ptr = '\0';
\r
296 ptr = strchr(line,'=');
\r
297 if (ptr == NULL) return false;
\r
304 while (*name == ' ') name++; // remove leading spaces
\r
306 while (ptr > name && ptr[-1] == ' ') ptr--; // remove trailing spaces
\r
309 if (*name == '\0') return false;
\r
313 ptr = &value[strlen(value)]; // pointer to string terminator
\r
315 while (*value == ' ') value++; // remove leading spaces
\r
317 while (ptr > value && ptr[-1] == ' ') ptr--; // remove trailing spaces
\r
320 if (*value == '\0') return false;
\r
325 *value_ptr = value;
\r
330 static void sig_quit(int dummy){
\r
331 my_log("POLYGLOT *** SIGINT Received ***\n");
\r
340 char string[StringSize];
\r
342 my_log("POLYGLOT *** QUIT ***\n");
\r
347 engine_send(Engine,"quit");
\r
349 // wait for the engine to quit
\r
351 engine_get(Engine,string,StringSize); // HACK: calls exit() on receiving EOF
\r
354 exit(EXIT_SUCCESS);
\r
359 static void stop_search() {
\r
361 if (Init && Uci->searching) {
\r
363 ASSERT(Uci->searching);
\r
364 ASSERT(Uci->pending_nb>=1);
\r
366 my_log("POLYGLOT STOP SEARCH\n");
\r
369 engine_send(Engine,"stop");
\r
370 Uci->searching = false;
\r
373 if (option_get_bool("SyncStop")) {
\r
374 uci_send_stop_sync(Uci);
\r
376 uci_send_stop(Uci);
\r