X-Git-Url: http://winboard.nl/cgi-bin?a=blobdiff_plain;f=main.c;h=aaadc822f16b9512f1f976589831e722cd008c4f;hb=bb6c47f77f59067c358579a71cefa1ae65180a30;hp=fad2ed8b9a806b21371342d105d0d1191915a39b;hpb=cb9522491af43508c47cb927247e3b5769b9259b;p=polyglot.git diff --git a/main.c b/main.c index fad2ed8..aaadc82 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.46b"; 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,22 @@ 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" + "; If the value of the option \"Persist\" is false\n" + "; then the content of this file is ignored.\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; @@ -72,43 +91,78 @@ static void arg_shift_left(char **argv, int index){ } } + +// make_ini() + +static void make_ini(ini_t *ini){ + option_t *opt; + char tmp[StringSize]; + ini_insert_ex(ini,"polyglot", + "EngineName", + option_get_string(Option,"EngineName")); + ini_insert_ex(ini,"polyglot", + "EngineCommand", + option_get_string(Option,"EngineCommand")); + ini_insert_ex(ini,"polyglot", + "EngineDir", + option_get_string(Option,"EngineDir")); + option_start_iter(Option); + while((opt=option_next(Option))){ + if(!my_string_equal(opt->value,opt->default_)&& + !IS_BUTTON(opt->type) && + (opt->mode & XBOARD)){ + ini_insert_ex(ini,"polyglot",opt->name,opt->value); + } + } + option_start_iter(Uci->option); + while((opt=option_next(Uci->option))){ + if(!my_string_equal(opt->value,opt->default_)&& + !IS_BUTTON(opt->type)){ + ini_insert_ex(ini,"engine",opt->name,opt->value); + } + } +} + + // write_ini() static void write_ini(const char *filename, - option_list_t *pg_options, - option_list_t *uci_options){ - option_t *opt; + ini_t *ini){ + ini_entry_t *entry; 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"); - 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") && - (opt->mode & XBOARD)){ - snprintf(tmp,sizeof(tmp),"%s=%s\n",opt->name,opt->value); - tmp[sizeof(tmp)-1]='\0'; - fprintf(f,"%s",tmp); - } + 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"); - 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")){ - snprintf(tmp,sizeof(tmp),"%s=%s\n",opt->name,opt->value); - tmp[sizeof(tmp)-1]='\0'; - fprintf(f,"%s",tmp); - } + 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); } @@ -121,7 +175,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 +354,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 +369,50 @@ 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", + int i; + snprintf(tmp,sizeof(tmp),"%s.ini", option_get_string(Option,"EngineName")); tmp[sizeof(tmp)-1]='\0'; + for(i=0;ivalue,"false") || + my_string_equal(entry->value,"0"))?FALSE:TRUE; + } + + // 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 +463,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 +543,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->type)){ + // 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->type)){ + 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 +572,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 +612,12 @@ static void init_book(){ void quit() { + ini_t ini[1]; + char persist_path[StringSize]; + int ret; + + ini_init(ini); + my_log("POLYGLOT *** QUIT ***\n"); if (Init) { @@ -514,11 +628,34 @@ void quit() { engine_close(Engine); } - if(option_get_bool(Option,"Persist")){ - write_ini(option_get_string(Option,"PersistFile"), - Option,Uci->option); + 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"), + "")){ + // Persistence should only work in XBOARD mode. + // In UCI mode the GUI is responsible for remembering options. + if(!option_get_bool(Option,"UCI")){ + my_path_join(persist_path, + option_get(Option,"PersistDir"), + option_get(Option,"PersistFile")); + make_ini(ini); + if(option_get_bool(Option,"Persist")){ + write_ini(persist_path,ini); + }else if(!my_string_case_equal(option_get_default(Option,"Persist"), + option_get_string(Option,"Persist"))){ + // Hack + ini_insert_ex(ini,"polyglot","Persist","false"); + write_ini(persist_path,ini); + }else{ + write_ini(persist_path,ini); + } + my_log("POLYGLOT Calling exit\n"); + } } - my_log("POLYGLOT Calling exit\n"); exit(EXIT_SUCCESS); }