X-Git-Url: http://winboard.nl/cgi-bin?a=blobdiff_plain;f=main.c;h=8d2724b8e441adb3473ef098194031d45c41182e;hb=cd81270f2b1723e0798f4d6dcaee134f0b4aca7f;hp=fad2ed8b9a806b21371342d105d0d1191915a39b;hpb=cb9522491af43508c47cb927247e3b5769b9259b;p=polyglot.git diff --git a/main.c b/main.c index fad2ed8..8d2724b 100644 --- a/main.c +++ b/main.c @@ -7,6 +7,7 @@ #include #include #include +#include #include "attack.h" #include "board.h" @@ -32,11 +33,13 @@ #include "xboard2uci.h" #include "uci2uci.h" #include "ini.h" +#include "util.h" + // constants -static const char * const Version = "1.4.42b"; +static const char * const Version = "1.4.45b"; static const char * const HelpMessage = "\ SYNTAX\n\ * polyglot [configfile] [-noini] [-ec engine] [-ed enginedirectory] [-en enginename] [-log] [-lf logfile] [-hash value] [-bk book] [-pg =]* [-uci =]*\n\ @@ -52,6 +55,19 @@ static const int SearchDepth = 63; static const double SearchTime = 3600.0; static const int StringSize = 4096; +static const char * const IniIntro= + "; This ini file is used internally by PolyGlot\n" + "; to remember the settings for the UCI engine\n" + "; whose name is \"%s\".\n" + "\n" + "; The values for these settings would be typicallly\n" + "; obtained from the engine settings dialog\n" + "; in WinBoard/xboard 4.4.0 and higher.\n" + "\n" + "; It is allowed to manually edit this file\n" + "; and you may safely delete it as well.\n" + "\n"; + // variables static bool Init; @@ -80,20 +96,28 @@ static void write_ini(const char *filename, option_t *opt; char tmp[StringSize]; FILE *f; + time_t t=time(NULL); f=fopen(filename,"w"); if(!f){ - my_fatal("ini_create_pg(): Cannot open %s for writing.\n",filename); - } - fprintf(f,"; You may edit this file to set options for the\n" - "; UCI engine whose PolyGlot name is %s.\n" - "; You may also safely delete this file\n" - "; to restore the default options.\n", - option_get_string(Option,"EngineName")); + // alas this does nothing.... + gui_send(GUI,"tellusererror write_ini(): %s: %s.",filename,strerror(errno)); + // but at least we log the error + my_log("POLYGLOT write_ini(): %s: %s.\n",filename,strerror(errno)); + return; + } + fprintf(f,"; %s\n",ctime(&t)); + fprintf(f,IniIntro,option_get_string(Option,"EngineName")); fprintf(f,"[PolyGlot]\n"); + fprintf(f,"EngineName=%s\n", + option_get_string(Option,"EngineName")); + fprintf(f,"EngineCommand=%s\n", + option_get_string(Option,"EngineCommand")); + fprintf(f,"EngineDir=%s\n", + option_get_string(Option,"EngineDir")); option_start_iter(pg_options); while((opt=option_next(pg_options))){ if(!my_string_equal(opt->value,opt->default_)&& - !my_string_case_equal(opt->type,"button") && + !IS_BUTTON(opt) && (opt->mode & XBOARD)){ snprintf(tmp,sizeof(tmp),"%s=%s\n",opt->name,opt->value); tmp[sizeof(tmp)-1]='\0'; @@ -104,7 +128,7 @@ static void write_ini(const char *filename, option_start_iter(uci_options); while((opt=option_next(uci_options))){ if(!my_string_equal(opt->value,opt->default_)&& - !my_string_case_equal(opt->type,"button")){ + !IS_BUTTON(opt)){ snprintf(tmp,sizeof(tmp),"%s=%s\n",opt->name,opt->value); tmp[sizeof(tmp)-1]='\0'; fprintf(f,"%s",tmp); @@ -113,6 +137,55 @@ static void write_ini(const char *filename, fclose(f); } +// write_ini_ex() + +static void write_ini_ex(const char *filename, + ini_t *ini){ + ini_entry_t *entry; + char tmp[StringSize]; + FILE *f; + time_t t=time(NULL); + f=fopen(filename,"w"); + if(!f){ + // alas this does nothing.... + gui_send(GUI,"tellusererror write_ini(): %s: %s.",filename,strerror(errno)); + // but at least we log the error + my_log("POLYGLOT write_ini(): %s: %s.\n",filename,strerror(errno)); + return; + } + fprintf(f,"; %s\n",ctime(&t)); + fprintf(f,IniIntro,option_get_string(Option,"EngineName")); + fprintf(f,"[PolyGlot]\n"); + fprintf(f,"EngineName=%s\n", + option_get_string(Option,"EngineName")); + fprintf(f,"EngineCommand=%s\n", + option_get_string(Option,"EngineCommand")); + fprintf(f,"EngineDir=%s\n", + option_get_string(Option,"EngineDir")); + ini_start_iter(ini); + while((entry=ini_next(ini))){ + if(my_string_case_equal(entry->section,"polyglot")){ + snprintf(tmp,sizeof(tmp),"%s=%s\n", + entry->name, + entry->value); + tmp[sizeof(tmp)-1]='\0'; + fprintf(f,"%s",tmp); + } + } + fprintf(f,"[Engine]\n"); + ini_start_iter(ini); + while((entry=ini_next(ini))){ + if(my_string_case_equal(entry->section,"engine")){ + snprintf(tmp,sizeof(tmp),"%s=%s\n", + entry->name, + entry->value); + tmp[sizeof(tmp)-1]='\0'; + fprintf(f,"%s",tmp); + } + } + fclose(f); +} + // main() @@ -121,7 +194,8 @@ int main(int argc, char * argv[]) { ini_entry_t *entry; char *arg; int arg_index; - bool NoIni; + bool NoIni, Persist; + char persist_path[StringSize]; if(!DEBUG){ printf("PolyGlot %s by Fabien Letouzey.\n",Version); @@ -299,8 +373,8 @@ int main(int argc, char * argv[]) { my_log("POLYGLOT *** Switching to UCI mode ***\n"); } - // initialize uci parsing and send uci command. Parse options and wait - // for uciok + // initialize uci parsing and send uci command. + // Parse options and wait for uciok uci_open(Uci,Engine); @@ -314,22 +388,44 @@ int main(int argc, char * argv[]) { if(my_string_equal(option_get_string(Option,"PersistFile"),"")){ char tmp[StringSize]; - snprintf(tmp,sizeof(tmp),"PG_%s.ini", + snprintf(tmp,sizeof(tmp),"%s.ini", option_get_string(Option,"EngineName")); tmp[sizeof(tmp)-1]='\0'; option_set(Option,"PersistFile",tmp); } - // if "Persist" is true, load the persist file! - - if(option_get_bool(Option,"Persist")){ - my_log("POLYGLOT PersistFile=%s\n",option_get_string(Option,"PersistFile")); - if(ini_parse(ini_save,option_get_string(Option,"PersistFile"))){ - my_log("POLYGLOT Unable to open PersistFile\n"); - } + // Load the persist file + + my_path_join(persist_path, + option_get_string(Option,"PersistDir"), + option_get_string(Option,"PersistFile")); + + my_log("POLYGLOT PersistFile=%s\n",persist_path); + if(ini_parse(ini_save,persist_path)){ + my_log("POLYGLOT Unable to open PersistFile\n"); + } + + // Do we want to use the persist file? + + entry=ini_find(ini_save,"polyglot","Persist"); + if(!entry){ + Persist=option_get_bool(Option,"Persist"); + }else{ + Persist=(my_string_case_equal(entry->value,"false") || + my_string_equal(entry->value,"0"))?FALSE:TRUE; } - - // parse the command line and merge remaining options + + // if "Persist" now happens to be false, forget about the + // persist file + + if(!Persist){ + my_log("POLYGLOT Ignoring PersistFile"); + ini_clear(ini_save); + } + + option_set(Option,"Persist",Persist?"true":"false"); + + // parse the command line and merge remaining options arg_index=1; while((arg=argv[arg_index])){ @@ -380,16 +476,24 @@ int main(int argc, char * argv[]) { // remind the reader once again about options - my_log("POLYGLOG Options from save file and command line\n"); + my_log("POLYGLOG Options from PersistFile and command line\n"); ini_disp(ini_save); - // extract PG options; this time do not set the default + // Extract PG options; this time do not set the default. // polyglot_set_option() performs the necessary actions such // as opening the log file/opening book etcetera. + // Ignore EngineName, EngineCommand and EngineDir + // as these are really meant as comments. ini_start_iter(ini_save); while((entry=ini_next(ini_save))){ if(my_string_case_equal(entry->section,"polyglot")){ + if(my_string_case_equal(entry->value,"EngineName")) + continue; + if(my_string_case_equal(entry->value,"EngineCommand")) + continue; + if(my_string_case_equal(entry->value,"EngineDir")) + continue; polyglot_set_option(entry->name,entry->value); } } @@ -452,7 +556,24 @@ int main(int argc, char * argv[]) { // polyglot_set_option() void polyglot_set_option(const char *name, const char *value){ // this must be cleaned up! + option_t *opt; my_log("POLYGLOT Setting PolyGlot option %s=\"%s\"\n",name,value); + if(my_string_case_equal(name,"Defaults")){ + option_start_iter(Uci->option); + while((opt=option_next(Uci->option))){ + if(!IS_BUTTON(opt)){ + // also sets opt->value + uci_send_option(Uci,opt->name,opt->default_); + } + } + option_start_iter(Option); + while((opt=option_next(Option))){ + if(!IS_BUTTON(opt)){ + polyglot_set_option(opt->name,opt->default_); + } + } + xboard2uci_send_options(); + } option_set(Option,name,value); if(option_get_bool(Option,"Book")&&(my_string_case_equal(name,"BookFile")||my_string_case_equal(name,"Book"))){ my_log("POLYGLOT *** SETTING BOOK ***\n"); @@ -464,7 +585,7 @@ void polyglot_set_option(const char *name, const char *value){ // this must be c my_log("POLYGLOT Unable to open book \"%s\"\n",option_get_string(Option,"BookFile")); } }else if(option_get_bool(Option,"Log")&&(my_string_case_equal(name,"LogFile") ||my_string_case_equal(name,"Log"))){ - my_log("POLYGLOT *** SETTING LOGFILE ***\n"); + my_log("POLYGLOT *** SWITCHING LOGFILE ***\n"); my_log("POLYGLOT LOGFILE \"%s\"\n",option_get_string(Option,"LogFile")); my_log_close(); my_log_open(option_get_string(Option,"LogFile")); @@ -504,6 +625,12 @@ static void init_book(){ void quit() { + ini_t empty[1]; + char persist_path[StringSize]; + int ret; + + ini_init(empty); + my_log("POLYGLOT *** QUIT ***\n"); if (Init) { @@ -514,11 +641,30 @@ void quit() { engine_close(Engine); } - if(option_get_bool(Option,"Persist")){ - write_ini(option_get_string(Option,"PersistFile"), + ret=my_mkdir(option_get(Option,"PersistDir")); + if(ret){ + my_log("POLYGLOT quit(): %s: %s\n",option_get(Option,"PersistDir"),strerror(errno)); + } + // PersistFile can be named "" in case of a crash before the + // engine is started. + if(!my_string_case_equal(option_get(Option,"PersistFile"), + "")){ + my_path_join(persist_path, + option_get(Option,"PersistDir"), + option_get(Option,"PersistFile")); + if(option_get_bool(Option,"Persist")){ + write_ini(persist_path, Option,Uci->option); + }else if(!my_string_case_equal(option_get_default(Option,"Persist"), + option_get_string(Option,"Persist"))){ + // Hack + ini_insert_ex(empty,"PolyGlot","Persist","false"); + write_ini_ex(persist_path,empty); + }else{ + write_ini_ex(persist_path,empty); + } + my_log("POLYGLOT Calling exit\n"); } - my_log("POLYGLOT Calling exit\n"); exit(EXIT_SUCCESS); }