X-Git-Url: http://winboard.nl/cgi-bin?a=blobdiff_plain;f=main.c;h=8bd627673f0821168edf1df9cb29bde8def12810;hb=ac968003c13bde5c86ffa19f8819e436b5bc03e6;hp=6ffac64a4c8935196772b7a9e922babb2aa4dac6;hpb=a8209ad25e331b9938321744c3cf0e03867eac1c;p=polyglot.git diff --git a/main.c b/main.c index 6ffac64..8bd6276 100644 --- a/main.c +++ b/main.c @@ -36,7 +36,7 @@ // constants -static const char * const Version = "1.4.41b"; +static const char * const Version = "1.4.44b"; 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\ @@ -65,23 +65,24 @@ static void stop_search (); // arg_shift_left() -void arg_shift_left(char **argv, int index){ +static void arg_shift_left(char **argv, int index){ int i; for(i=index; argv[i]!=NULL; i++){ argv[i]=argv[i+1]; } } +// write_ini() -void write_ini(const char *filename, - option_list_t *pg_options, - option_list_t *uci_options){ +static void write_ini(const char *filename, + option_list_t *pg_options, + option_list_t *uci_options){ option_t *opt; char tmp[StringSize]; FILE *f; f=fopen(filename,"w"); if(!f){ - my_fatal("ini_create_pg(): Cannot open %s for writing.\n",filename); + my_fatal("write_ini(): 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" @@ -91,7 +92,9 @@ void write_ini(const char *filename, fprintf(f,"[PolyGlot]\n"); option_start_iter(pg_options); while((opt=option_next(pg_options))){ - if(!my_string_case_equal(opt->type,"button") && (opt->mode & XBOARD)){ + if(!my_string_equal(opt->value,opt->default_)&& + !IS_BUTTON(opt) && + (opt->mode & XBOARD)){ snprintf(tmp,sizeof(tmp),"%s=%s\n",opt->name,opt->value); tmp[sizeof(tmp)-1]='\0'; fprintf(f,"%s",tmp); @@ -100,7 +103,8 @@ void write_ini(const char *filename, fprintf(f,"[Engine]\n"); option_start_iter(uci_options); while((opt=option_next(uci_options))){ - if(!my_string_case_equal(opt->type,"button")){ + if(!my_string_equal(opt->value,opt->default_)&& + !IS_BUTTON(opt)){ snprintf(tmp,sizeof(tmp),"%s=%s\n",opt->name,opt->value); tmp[sizeof(tmp)-1]='\0'; fprintf(f,"%s",tmp); @@ -109,15 +113,56 @@ 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; + f=fopen(filename,"w"); + if(!f){ + my_fatal("write_ini_ex(): 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")); + fprintf(f,"[PolyGlot]\n"); + 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() int main(int argc, char * argv[]) { - ini_t ini[1]; + ini_t ini[1],ini_save[1]; ini_entry_t *entry; char *arg; int arg_index; - bool NoIni; + bool NoIni, Persist; if(!DEBUG){ printf("PolyGlot %s by Fabien Letouzey.\n",Version); @@ -145,6 +190,9 @@ int main(int argc, char * argv[]) { my_random_init(); + ini_init(ini); + ini_init(ini_save); + // book utilities if (argc >= 2 && my_string_equal(argv[1],"make-book")) { @@ -174,7 +222,10 @@ int main(int argc, char * argv[]) { return EXIT_SUCCESS; } - // what is the config file? This is very hacky right now? + // TODO: If logging is enabled on the command line turn it on NOW + // and do not allow it to be overridden later. + + // What is the config file? This is very hacky right now. // Do we want a config file at all? @@ -203,10 +254,9 @@ int main(int argc, char * argv[]) { arg_shift_left(argv,1); }else{ // Config file is the default. - // This has already been set above or in "option_init_pg" + // This has already been set above or in "option_init_pg()" } - ini_init(ini); // if we use a config file: load it! @@ -217,56 +267,151 @@ int main(int argc, char * argv[]) { strerror(errno)); } } + // remind the reader of what options are in effect - // parse the command line and merge options + my_log("POLYGLOG Options from ini file\n"); + ini_disp(ini); + // extract PG options + + ini_start_iter(ini); + while((entry=ini_next(ini))){ + if(my_string_case_equal(entry->section,"polyglot")){ + option_set(Option,entry->name,entry->value); + option_set_default(Option,entry->name,entry->value); + } + } + + // start logging if required + + if (option_get_bool(Option,"Log")) { + my_log_open(option_get_string(Option,"LogFile")); + } + + // log welcome stuff + + if(!DEBUG){ + my_log("PolyGlot %s by Fabien Letouzey\n",Version); + }else{ + my_log("PolyGlot %s by Fabien Letouzey (debug build)\n",Version); + } + my_log("POLYGLOT *** START ***\n"); + my_log("POLYGLOT INI file \"%s\"\n",option_get_string(Option,"OptionFile")); + + // open book (presumably this should go else where) + + init_book(); + + // scavenge command line for options necessary to start the engine + arg_index=1; while((arg=argv[arg_index])){ -// int i=1; -// char *arg1; -// printf("arg_index=%d\n",arg_index); -// while((arg1=argv[i++])){ -// printf("arg=%s ",arg1); -// } -// printf("\n"); if(my_string_equal(arg,"-ec") && argv[arg_index+1]){ - ini_insert_ex(ini,"PolyGlot","EngineCommand",argv[arg_index+1]); + option_set(Option,"EngineCommand",argv[arg_index+1]); arg_shift_left(argv,arg_index); arg_shift_left(argv,arg_index); continue; } if(my_string_equal(arg,"-ed") && argv[arg_index+1]){ - ini_insert_ex(ini,"PolyGlot","EngineDir",argv[arg_index+1]); + option_set(Option,"EngineDir",argv[arg_index+1]); arg_shift_left(argv,arg_index); arg_shift_left(argv,arg_index); continue; } if(my_string_equal(arg,"-en") && argv[arg_index+1]){ - ini_insert_ex(ini,"PolyGlot","EngineName",argv[arg_index+1]); + option_set(Option,"EngineName",argv[arg_index+1]); arg_shift_left(argv,arg_index); arg_shift_left(argv,arg_index); continue; } + arg_index++; + } + + // start engine + + engine_open(Engine); + if(!engine_active(Engine)){ + my_fatal("Could not start \"%s\"\n",option_get(Option,"EngineCommand")); + } + + // switch to UCI mode if necessary + + if (option_get_bool(Option,"UCI")) { + my_log("POLYGLOT *** Switching to UCI mode ***\n"); + } + + // initialize uci parsing and send uci command. + // Parse options and wait for uciok + + uci_open(Uci,Engine); + + // get engine name from engine if not supplied in config file + + if (my_string_equal(option_get_string(Option,"EngineName"),"")) { + option_set(Option,"EngineName",Uci->name); + } + + // what is the name of the persist file? + + if(my_string_equal(option_get_string(Option,"PersistFile"),"")){ + char tmp[StringSize]; + snprintf(tmp,sizeof(tmp),"PG_%s.ini", + option_get_string(Option,"EngineName")); + tmp[sizeof(tmp)-1]='\0'; + option_set(Option,"PersistFile",tmp); + } + + // Load the persist file + + 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"); + } + + // 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; + } + + // 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])){ if(my_string_equal(arg,"-log")){ - ini_insert_ex(ini,"PolyGlot","Log","true"); + ini_insert_ex(ini_save,"PolyGlot","Log","true"); arg_shift_left(argv,arg_index); continue; } if(my_string_equal(arg,"-lf") && argv[arg_index+1]){ - ini_insert_ex(ini,"PolyGlot","LogFile",argv[arg_index+1]); + ini_insert_ex(ini_save,"PolyGlot","LogFile",argv[arg_index+1]); arg_shift_left(argv,arg_index); arg_shift_left(argv,arg_index); continue; } if(my_string_equal(arg,"-hash") && argv[arg_index+1]){ - ini_insert_ex(ini,"Engine","Hash",argv[arg_index+1]); + ini_insert_ex(ini_save,"Engine","Hash",argv[arg_index+1]); arg_shift_left(argv,arg_index); arg_shift_left(argv,arg_index); continue; } if(my_string_equal(arg,"-bk") && argv[arg_index+1]){ - ini_insert_ex(ini,"PolyGlot","Book","true"); - ini_insert_ex(ini,"PolyGlot","BookFile",argv[arg_index+1]); + ini_insert_ex(ini_save,"PolyGlot","Book","true"); + ini_insert_ex(ini_save,"PolyGlot","BookFile",argv[arg_index+1]); arg_shift_left(argv,arg_index); arg_shift_left(argv,arg_index); continue; @@ -280,9 +425,9 @@ int main(int argc, char * argv[]) { ret=ini_line_parse(argv[arg_index++],section,name,value); if(ret==NAME_VALUE){ if(my_string_equal(arg,"-pg")){ - ini_insert_ex(ini,"PolyGlot",name,value); + ini_insert_ex(ini_save,"PolyGlot",name,value); }else{ - ini_insert_ex(ini,"Engine",name,value); + ini_insert_ex(ini_save,"Engine",name,value); } } arg_shift_left(argv,arg_index); @@ -292,77 +437,21 @@ int main(int argc, char * argv[]) { arg_index++; } - // extract PG options - // this sets both the default and the value - - option_from_ini(Option,ini,"polyglot"); - - // start logging if required - - if (option_get_bool(Option,"Log")) { - my_log_open(option_get_string(Option,"LogFile")); - } - - // log welcome stuff - - if(!DEBUG){ - my_log("PolyGlot %s by Fabien Letouzey\n",Version); - }else{ - my_log("PolyGlot %s by Fabien Letouzey (debug build)\n",Version); - } - my_log("POLYGLOT *** START ***\n"); - my_log("POLYGLOT INI file \"%s\"\n",option_get_string(Option,"OptionFile")); - - // start engine - - engine_open(Engine); - if(!engine_active(Engine)){ - my_fatal("Could not start \"%s\"\n",option_get(Option,"EngineCommand")); - } - - // switch to UCI mode if necessary - - if (option_get_bool(Option,"UCI")) { - my_log("POLYGLOT *** Switching to UCI mode ***\n"); - } + // remind the reader once again about options - // initialize uci parsing and send uci command. Parse options and wait - // for uciok - - uci_open(Uci,Engine); + my_log("POLYGLOG Options from PersistFile and command line\n"); + ini_disp(ini_save); - // get engine name from engine if not supplied in config file + // 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. - if (my_string_equal(option_get_string(Option,"EngineName"),"")) { - option_set(Option,"EngineName",Uci->name); - } - - // if there is a save file: load it! - - if(my_string_equal(option_get_string(Option,"SaveFile"),"")){ - char tmp[StringSize]; - snprintf(tmp,sizeof(tmp),"PG_%s.ini", - option_get_string(Option,"EngineName")); - tmp[sizeof(tmp)-1]='\0'; - option_set(Option,"SaveFile",tmp); - } - if(option_get_bool(Option,"SaveSettingsOnExit")){ - my_log("POLYGLOT SaveFile=%s\n",option_get_string(Option,"SaveFile")); - if(ini_parse(ini,option_get_string(Option,"SaveFile"))){ - my_log("POLYGLOT Unable to open SaveFile\n"); + ini_start_iter(ini_save); + while((entry=ini_next(ini_save))){ + if(my_string_case_equal(entry->section,"polyglot")){ + polyglot_set_option(entry->name,entry->value); } } - // start if it was enabled in the SaveFile - - my_log_close(); - if (option_get_bool(Option,"Log")) { - my_log_open(option_get_string(Option,"LogFile")); - } - // remind the user of the options that are now in effect - - ini_disp(ini); - - // done initializing @@ -377,15 +466,25 @@ int main(int argc, char * argv[]) { uci_send_option(Uci,entry->name,"%s",entry->value); // since this comes from the ini file, also update default option_set_default(Uci->option,entry->name,entry->value); - //to get a decent display in winboard_x we need to now if an engine really is doing multipv analysis - // "multipv 1" in the pv is meaningless,f.i. toga sends that all the time - //therefore check if MultiPV is set to a decent value in the polyglot ini file + // this is inherited, it probably does not work correctly if(my_string_case_equal(entry->name,"MultiPV") && atoi(entry->value)>1){ Uci->multipv_mode=TRUE; } } } + ini_start_iter(ini_save); + while((entry=ini_next(ini_save))){ + if(my_string_case_equal(entry->section,"engine")){ + // also updates value in Uci->option + uci_send_option(Uci,entry->name,"%s",entry->value); + // this is inherited, it probably does not work correctly + if(my_string_case_equal(entry->name,"MultiPV") && + atoi(entry->value)>1){ + Uci->multipv_mode=TRUE; + } + } + } // EPD test @@ -404,15 +503,32 @@ int main(int argc, char * argv[]) { } - init_book(); gui_init(GUI); mainloop(); return EXIT_SUCCESS; } -// polyglot_set_option +// polyglot_set_option() -void polyglot_set_option(char *name, char *value){ // this must be cleaned up! +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"); @@ -424,7 +540,7 @@ void polyglot_set_option(char *name, char *value){ // this must be cleaned up! 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")); @@ -464,6 +580,10 @@ static void init_book(){ void quit() { + ini_t empty[1]; + + ini_init(empty); + my_log("POLYGLOT *** QUIT ***\n"); if (Init) { @@ -474,8 +594,17 @@ void quit() { engine_close(Engine); } - if(option_get_bool(Option,"SaveSettingsOnExit")){ - write_ini(option_get_string(Option,"SaveFile"),Option,Uci->option); + // printf("def=%s val=%s\n",option_get_default(Option,"Persist"),option_get_string(Option,"Persist")); + if(option_get_bool(Option,"Persist")){ + write_ini(option_get_string(Option,"PersistFile"), + 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(option_get_string(Option,"PersistFile"),empty); + }else{ + write_ini_ex(option_get_string(Option,"PersistFile"),empty); } my_log("POLYGLOT Calling exit\n"); exit(EXIT_SUCCESS);