15 #include "book_make.h"
\r
16 #include "book_merge.h"
\r
24 #include "mainloop.h"
\r
26 #include "move_gen.h"
\r
33 #include "xboard2uci.h"
\r
34 #include "uci2uci.h"
\r
42 static const char * const Version = "1.4.46b";
\r
43 static const char * const HelpMessage = "\
\r
45 * polyglot [configfile] [-noini] [-ec engine] [-ed enginedirectory] [-en enginename] [-log] [-lf logfile] [-hash value] [-bk book] [-pg <name>=<value>]* [-uci <name>=<value>]*\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 info-book [-bin inputfile] [-exact]\n\
\r
49 * polyglot dump-book [-bin inputfile] -color color [-out outputfile]\n\
\r
50 * polyglot [configfile] epd-test [engineoptions] [-epd inputfile] [-min-depth depth] [-max-depth depth] [-min-time time] [-max-time time] [-depth-delta delta]\n\
\r
51 * 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
58 static const char * const IniIntro=
\r
59 "; This ini file is used internally by PolyGlot\n"
\r
60 "; to remember the settings for the UCI engine\n"
\r
61 "; whose name is \"%s\".\n"
\r
63 "; The values for these settings would be typicallly\n"
\r
64 "; obtained from the engine settings dialog\n"
\r
65 "; in WinBoard/xboard 4.4.0 and higher.\n"
\r
67 "; If the value of the option \"Persist\" is false\n"
\r
68 "; then the content of this file is ignored.\n"
\r
70 "; It is allowed to manually edit this file\n"
\r
71 "; and you may safely delete it as well.\n"
\r
80 static void init_book ();
\r
81 static void stop_search ();
\r
87 static void arg_shift_left(char **argv, int index){
\r
89 for(i=index; argv[i]!=NULL; i++){
\r
97 static void make_ini(ini_t *ini){
\r
99 char tmp[StringSize];
\r
100 ini_insert_ex(ini,"polyglot",
\r
102 option_get_string(Option,"EngineName"));
\r
103 ini_insert_ex(ini,"polyglot",
\r
105 option_get_string(Option,"EngineCommand"));
\r
106 ini_insert_ex(ini,"polyglot",
\r
108 option_get_string(Option,"EngineDir"));
\r
109 option_start_iter(Option);
\r
110 while((opt=option_next(Option))){
\r
111 if(!my_string_equal(opt->value,opt->default_)&&
\r
112 !IS_BUTTON(opt->type) &&
\r
113 (opt->mode & XBOARD)){
\r
114 ini_insert_ex(ini,"polyglot",opt->name,opt->value);
\r
117 option_start_iter(Uci->option);
\r
118 while((opt=option_next(Uci->option))){
\r
119 if(!my_string_equal(opt->value,opt->default_)&&
\r
120 !IS_BUTTON(opt->type)){
\r
121 ini_insert_ex(ini,"engine",opt->name,opt->value);
\r
129 static void write_ini(const char *filename,
\r
131 ini_entry_t *entry;
\r
132 char tmp[StringSize];
\r
134 time_t t=time(NULL);
\r
135 f=fopen(filename,"w");
\r
137 // alas this does nothing....
\r
138 gui_send(GUI,"tellusererror write_ini(): %s: %s.",filename,strerror(errno));
\r
139 // but at least we log the error
\r
140 my_log("POLYGLOT write_ini(): %s: %s.\n",filename,strerror(errno));
\r
143 fprintf(f,"; %s\n",ctime(&t));
\r
144 fprintf(f,IniIntro,option_get_string(Option,"EngineName"));
\r
145 fprintf(f,"[PolyGlot]\n");
\r
146 ini_start_iter(ini);
\r
147 while((entry=ini_next(ini))){
\r
148 if(my_string_case_equal(entry->section,"polyglot")){
\r
149 snprintf(tmp,sizeof(tmp),"%s=%s\n",
\r
152 tmp[sizeof(tmp)-1]='\0';
\r
153 fprintf(f,"%s",tmp);
\r
156 fprintf(f,"[Engine]\n");
\r
157 ini_start_iter(ini);
\r
158 while((entry=ini_next(ini))){
\r
159 if(my_string_case_equal(entry->section,"engine")){
\r
160 snprintf(tmp,sizeof(tmp),"%s=%s\n",
\r
163 tmp[sizeof(tmp)-1]='\0';
\r
164 fprintf(f,"%s",tmp);
\r
173 int main(int argc, char * argv[]) {
\r
174 ini_t ini[1],ini_save[1];
\r
175 ini_entry_t *entry;
\r
178 bool NoIni, Persist;
\r
179 char persist_path[StringSize];
\r
182 printf("PolyGlot %s by Fabien Letouzey.\n",Version);
\r
184 printf("PolyGlot %s by Fabien Letouzey (debug build).\n",Version);
\r
187 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
188 printf("%s\n",HelpMessage);
\r
189 return EXIT_SUCCESS;
\r
208 ini_init(ini_save);
\r
212 if (argc >= 2 && my_string_equal(argv[1],"make-book")) {
\r
213 book_make(argc,argv);
\r
214 return EXIT_SUCCESS;
\r
217 if (argc >= 2 && my_string_equal(argv[1],"merge-book")) {
\r
218 book_merge(argc,argv);
\r
219 return EXIT_SUCCESS;
\r
222 if (argc >= 2 && my_string_equal(argv[1],"dump-book")) {
\r
223 book_dump(argc,argv);
\r
224 return EXIT_SUCCESS;
\r
227 if (argc >= 2 && my_string_equal(argv[1],"info-book")) {
\r
228 book_info(argc,argv);
\r
229 return EXIT_SUCCESS;
\r
234 if (argc >= 2 && my_string_equal(argv[1],"perft")) {
\r
235 do_perft(argc,argv);
\r
236 return EXIT_SUCCESS;
\r
239 // TODO: If logging is enabled on the command line turn it on NOW
\r
240 // and do not allow it to be overridden later.
\r
242 // What is the config file? This is very hacky right now.
\r
244 // Do we want a config file at all?
\r
248 while((arg=argv[arg_index++])){
\r
249 if(my_string_equal(arg,"-noini")){
\r
254 arg_shift_left(argv,arg_index-1);
\r
256 option_set(Option,"OptionFile","<empty>");
\r
259 // Ok see if first argument looks like config file
\r
261 if(argv[1] && !my_string_equal(argv[1],"epd-test") && !(argv[1][0]=='-')){
\r
262 // first argument must be config file
\r
264 option_set(Option,"OptionFile",argv[1]);
\r
268 arg_shift_left(argv,1);
\r
270 // Config file is the default.
\r
271 // This has already been set above or in "option_init_pg()"
\r
275 // if we use a config file: load it!
\r
277 if(!my_string_equal(option_get_string(Option,"OptionFile"),"<empty>")){
\r
278 if(ini_parse(ini,option_get_string(Option,"OptionFile"))){
\r
279 my_fatal("main(): Can't open file \"%s\": %s\n",
\r
280 option_get_string(Option,"OptionFile"),
\r
284 // remind the reader of what options are in effect
\r
286 my_log("POLYGLOG Options from ini file\n");
\r
289 // extract PG options
\r
291 ini_start_iter(ini);
\r
292 while((entry=ini_next(ini))){
\r
293 if(my_string_case_equal(entry->section,"polyglot")){
\r
294 option_set(Option,entry->name,entry->value);
\r
295 option_set_default(Option,entry->name,entry->value);
\r
299 // start logging if required
\r
301 if (option_get_bool(Option,"Log")) {
\r
302 my_log_open(option_get_string(Option,"LogFile"));
\r
305 // log welcome stuff
\r
308 my_log("PolyGlot %s by Fabien Letouzey\n",Version);
\r
310 my_log("PolyGlot %s by Fabien Letouzey (debug build)\n",Version);
\r
312 my_log("POLYGLOT *** START ***\n");
\r
313 my_log("POLYGLOT INI file \"%s\"\n",option_get_string(Option,"OptionFile"));
\r
315 // open book (presumably this should go else where)
\r
319 // scavenge command line for options necessary to start the engine
\r
322 while((arg=argv[arg_index])){
\r
323 if(my_string_equal(arg,"-ec") && argv[arg_index+1]){
\r
324 option_set(Option,"EngineCommand",argv[arg_index+1]);
\r
325 arg_shift_left(argv,arg_index);
\r
326 arg_shift_left(argv,arg_index);
\r
329 if(my_string_equal(arg,"-ed") && argv[arg_index+1]){
\r
330 option_set(Option,"EngineDir",argv[arg_index+1]);
\r
331 arg_shift_left(argv,arg_index);
\r
332 arg_shift_left(argv,arg_index);
\r
335 if(my_string_equal(arg,"-en") && argv[arg_index+1]){
\r
336 option_set(Option,"EngineName",argv[arg_index+1]);
\r
337 arg_shift_left(argv,arg_index);
\r
338 arg_shift_left(argv,arg_index);
\r
346 engine_open(Engine);
\r
347 if(!engine_active(Engine)){
\r
348 my_fatal("Could not start \"%s\"\n",option_get(Option,"EngineCommand"));
\r
351 // switch to UCI mode if necessary
\r
353 if (option_get_bool(Option,"UCI")) {
\r
354 my_log("POLYGLOT *** Switching to UCI mode ***\n");
\r
357 // initialize uci parsing and send uci command.
\r
358 // Parse options and wait for uciok
\r
360 uci_open(Uci,Engine);
\r
362 // get engine name from engine if not supplied in config file
\r
364 if (my_string_equal(option_get_string(Option,"EngineName"),"<empty>")) {
\r
365 option_set(Option,"EngineName",Uci->name);
\r
368 // what is the name of the persist file?
\r
370 if(my_string_equal(option_get_string(Option,"PersistFile"),"<empty>")){
\r
371 char tmp[StringSize];
\r
373 snprintf(tmp,sizeof(tmp),"%s.ini",
\r
374 option_get_string(Option,"EngineName"));
\r
375 tmp[sizeof(tmp)-1]='\0';
\r
376 for(i=0;i<strlen(tmp);i++){
\r
381 option_set(Option,"PersistFile",tmp);
\r
384 // Load the persist file
\r
386 my_path_join(persist_path,
\r
387 option_get_string(Option,"PersistDir"),
\r
388 option_get_string(Option,"PersistFile"));
\r
390 my_log("POLYGLOT PersistFile=%s\n",persist_path);
\r
391 if(ini_parse(ini_save,persist_path)){
\r
392 my_log("POLYGLOT Unable to open PersistFile\n");
\r
395 // Do we want to use the persist file?
\r
397 entry=ini_find(ini_save,"polyglot","Persist");
\r
399 Persist=option_get_bool(Option,"Persist");
\r
401 Persist=(my_string_case_equal(entry->value,"false") ||
\r
402 my_string_equal(entry->value,"0"))?FALSE:TRUE;
\r
405 // if "Persist" now happens to be false, forget about the
\r
409 my_log("POLYGLOT Ignoring PersistFile");
\r
410 ini_clear(ini_save);
\r
413 option_set(Option,"Persist",Persist?"true":"false");
\r
415 // parse the command line and merge remaining options
\r
418 while((arg=argv[arg_index])){
\r
419 if(my_string_equal(arg,"-log")){
\r
420 ini_insert_ex(ini_save,"PolyGlot","Log","true");
\r
421 arg_shift_left(argv,arg_index);
\r
424 if(my_string_equal(arg,"-lf") && argv[arg_index+1]){
\r
425 ini_insert_ex(ini_save,"PolyGlot","LogFile",argv[arg_index+1]);
\r
426 arg_shift_left(argv,arg_index);
\r
427 arg_shift_left(argv,arg_index);
\r
430 if(my_string_equal(arg,"-hash") && argv[arg_index+1]){
\r
431 ini_insert_ex(ini_save,"Engine","Hash",argv[arg_index+1]);
\r
432 arg_shift_left(argv,arg_index);
\r
433 arg_shift_left(argv,arg_index);
\r
436 if(my_string_equal(arg,"-bk") && argv[arg_index+1]){
\r
437 ini_insert_ex(ini_save,"PolyGlot","Book","true");
\r
438 ini_insert_ex(ini_save,"PolyGlot","BookFile",argv[arg_index+1]);
\r
439 arg_shift_left(argv,arg_index);
\r
440 arg_shift_left(argv,arg_index);
\r
443 if((my_string_equal(arg,"-pg")||my_string_equal(arg,"-uci")) &&
\r
446 char section[StringSize];
\r
447 char name[StringSize];
\r
448 char value[StringSize];
\r
449 ret=ini_line_parse(argv[arg_index++],section,name,value);
\r
450 if(ret==NAME_VALUE){
\r
451 if(my_string_equal(arg,"-pg")){
\r
452 ini_insert_ex(ini_save,"PolyGlot",name,value);
\r
454 ini_insert_ex(ini_save,"Engine",name,value);
\r
457 arg_shift_left(argv,arg_index);
\r
458 arg_shift_left(argv,arg_index);
\r
464 // remind the reader once again about options
\r
466 my_log("POLYGLOG Options from PersistFile and command line\n");
\r
467 ini_disp(ini_save);
\r
469 // Extract PG options; this time do not set the default.
\r
470 // polyglot_set_option() performs the necessary actions such
\r
471 // as opening the log file/opening book etcetera.
\r
472 // Ignore EngineName, EngineCommand and EngineDir
\r
473 // as these are really meant as comments.
\r
475 ini_start_iter(ini_save);
\r
476 while((entry=ini_next(ini_save))){
\r
477 if(my_string_case_equal(entry->section,"polyglot")){
\r
478 if(my_string_case_equal(entry->value,"EngineName"))
\r
480 if(my_string_case_equal(entry->value,"EngineCommand"))
\r
482 if(my_string_case_equal(entry->value,"EngineDir"))
\r
484 polyglot_set_option(entry->name,entry->value);
\r
488 // done initializing
\r
492 // collect engine options from config file(s) and send to engine
\r
494 ini_start_iter(ini);
\r
495 while((entry=ini_next(ini))){
\r
496 if(my_string_case_equal(entry->section,"engine")){
\r
497 // also updates value in Uci->option
\r
498 uci_send_option(Uci,entry->name,"%s",entry->value);
\r
499 // since this comes from the ini file, also update default
\r
500 option_set_default(Uci->option,entry->name,entry->value);
\r
501 // this is inherited, it probably does not work correctly
\r
502 if(my_string_case_equal(entry->name,"MultiPV") &&
\r
503 atoi(entry->value)>1){
\r
504 Uci->multipv_mode=TRUE;
\r
508 ini_start_iter(ini_save);
\r
509 while((entry=ini_next(ini_save))){
\r
510 if(my_string_case_equal(entry->section,"engine")){
\r
511 // also updates value in Uci->option
\r
512 uci_send_option(Uci,entry->name,"%s",entry->value);
\r
513 // this is inherited, it probably does not work correctly
\r
514 if(my_string_case_equal(entry->name,"MultiPV") &&
\r
515 atoi(entry->value)>1){
\r
516 Uci->multipv_mode=TRUE;
\r
524 if (argv[1] && my_string_equal(argv[1],"epd-test")){
\r
526 while((arg=argv[argc++]));
\r
527 epd_test(argc-1,argv);
\r
528 return EXIT_SUCCESS;
\r
531 // Anything that hasn't been parsed yet is a syntax error
\r
534 my_fatal("main(): Unknown option: %s\n",argv[1]);
\r
540 return EXIT_SUCCESS;
\r
543 // polyglot_set_option()
\r
545 void polyglot_set_option(const char *name, const char *value){ // this must be cleaned up!
\r
547 my_log("POLYGLOT Setting PolyGlot option %s=\"%s\"\n",name,value);
\r
548 if(my_string_case_equal(name,"Defaults")){
\r
549 option_start_iter(Uci->option);
\r
550 while((opt=option_next(Uci->option))){
\r
551 if(!IS_BUTTON(opt->type)){
\r
552 // also sets opt->value
\r
553 uci_send_option(Uci,opt->name,opt->default_);
\r
556 option_start_iter(Option);
\r
557 while((opt=option_next(Option))){
\r
558 if(!IS_BUTTON(opt->type)){
\r
559 polyglot_set_option(opt->name,opt->default_);
\r
562 xboard2uci_send_options();
\r
564 option_set(Option,name,value);
\r
565 if(option_get_bool(Option,"Book")&&(my_string_case_equal(name,"BookFile")||my_string_case_equal(name,"Book"))){
\r
566 my_log("POLYGLOT *** SETTING BOOK ***\n");
\r
567 my_log("POLYGLOT BOOK \"%s\"\n",option_get_string(Option,"BookFile"));
\r
570 book_open(option_get_string(Option,"BookFile"));
\r
571 if(!book_is_open()){
\r
572 my_log("POLYGLOT Unable to open book \"%s\"\n",option_get_string(Option,"BookFile"));
\r
574 }else if(option_get_bool(Option,"Log")&&(my_string_case_equal(name,"LogFile") ||my_string_case_equal(name,"Log"))){
\r
575 my_log("POLYGLOT *** SWITCHING LOGFILE ***\n");
\r
576 my_log("POLYGLOT LOGFILE \"%s\"\n",option_get_string(Option,"LogFile"));
\r
578 my_log_open(option_get_string(Option,"LogFile"));
\r
579 }else if(option_get_bool(Option,"UseNice") &&(my_string_case_equal(name,"NiceValue")||my_string_case_equal(name,"UseNice"))){
\r
580 my_log("POLYGLOT Adjust Engine Piority\n");
\r
581 engine_set_nice_value(Engine,atoi(option_get_string(Option,"NiceValue")));
\r
582 }else if(my_string_case_equal(name,"Book") && !option_get_bool(Option,"Book")){
\r
585 }else if(my_string_case_equal(name,"UseNice") && !option_get_bool(Option,"UseNice")){
\r
586 my_log("POLYGLOT Adjust Engine Piority\n");
\r
587 engine_set_nice_value(Engine,0);
\r
588 }else if(my_string_case_equal(name,"Log") && !option_get_bool(Option,"Log")){
\r
589 my_log("POLYGLOT QUIT LOGGING\n");
\r
597 static void init_book(){
\r
599 if (option_get_bool(Option,"Book")){
\r
600 my_log("POLYGLOT *** SETTING BOOK ***\n");
\r
601 my_log("POLYGLOT BOOK \"%s\"\n",option_get_string(Option,"BookFile"));
\r
602 book_open(option_get_string(Option,"BookFile"));
\r
603 if(!book_is_open()){
\r
604 my_log("POLYGLOT Unable to open book \"%s\"\n",
\r
605 option_get_string(Option,"BookFile"));
\r
616 char persist_path[StringSize];
\r
621 my_log("POLYGLOT *** QUIT ***\n");
\r
626 engine_send(Engine,"quit");
\r
627 my_log("POLYGLOT Closing engine\n");
\r
628 engine_close(Engine);
\r
631 ret=my_mkdir(option_get(Option,"PersistDir"));
\r
633 my_log("POLYGLOT quit(): %s: %s\n",option_get(Option,"PersistDir"),strerror(errno));
\r
635 // PersistFile can be named "<empty>" in case of a crash before the
\r
636 // engine is started.
\r
637 if(!my_string_case_equal(option_get(Option,"PersistFile"),
\r
639 // Persistence should only work in XBOARD mode.
\r
640 // In UCI mode the GUI is responsible for remembering options.
\r
641 if(!option_get_bool(Option,"UCI")){
\r
642 my_path_join(persist_path,
\r
643 option_get(Option,"PersistDir"),
\r
644 option_get(Option,"PersistFile"));
\r
646 if(option_get_bool(Option,"Persist")){
\r
647 write_ini(persist_path,ini);
\r
648 }else if(!my_string_case_equal(option_get_default(Option,"Persist"),
\r
649 option_get_string(Option,"Persist"))){
\r
651 ini_insert_ex(ini,"polyglot","Persist","false");
\r
652 write_ini(persist_path,ini);
\r
654 write_ini(persist_path,ini);
\r
656 my_log("POLYGLOT Calling exit\n");
\r
659 exit(EXIT_SUCCESS);
\r
664 static void stop_search() {
\r
666 if (Init && Uci->searching) {
\r
668 ASSERT(Uci->searching);
\r
669 ASSERT(Uci->pending_nb>=1);
\r
671 my_log("POLYGLOT STOP SEARCH\n");
\r
673 if (option_get_bool(Option,"SyncStop")) {
\r
674 uci_send_stop_sync(Uci);
\r
676 uci_send_stop(Uci);
\r