void engine_open(engine_t * engine){ int affinity; char *my_dir; if( (my_dir = _getcwd( NULL, 0 )) == NULL ) my_fatal("Can't build path: %s\n",strerror(errno)); if(SetCurrentDirectory(option_get_string("EngineDir"))==0){ printf("tellusererror Polyglot:EngineDir error: %s\n",option_get_string("EngineDir")); fflush(stdout); Sleep(50000); my_fatal("EngineDir path error: %s\n",option_get_string("EngineDir")); } pipeEngine.Open(option_get_string("EngineCommand")); //play with affinity #ifndef NO_AFFINITY affinity=option_get_int("Affinity"); if(affinity!=-1){ my_log("POLYGLOT SetProcess Affinity\n"); SetProcessAffinityMask(child,affinity); // } #endif //lets go back SetCurrentDirectory(my_dir); // set priority if (option_get_bool("UseNice")){ my_log("POLYGLOT Adjust Engine Piority\n"); SetPriorityClass(child, GetWin32Priority(option_get_int("NiceValue"))); } }
void PipeStruct::ReadInput(void) { DWORD dwBytes; int ret; BOOL bSetEvent=FALSE; // ReadData is outside the critical section otherwise everything // would block during the blocking read ret=ReadData(); EnterCriticalSection(&CriticalSection); if(!EOF_input()){ if(ret+nReadEnd>=LINE_INPUT_MAX_CHAR){ my_fatal("PipeStruct::ReadInput(): Internal error: buffer overflow\n"); } memcpy(lpBuffer+nReadEnd,lpReadBuffer,ret+1); nReadEnd += ret; if(!lpFeedEnd){ lpFeedEnd = (char *) memchr(lpBuffer, '\n', nReadEnd); } if(lpFeedEnd){ bSetEvent=TRUE; }else if(nReadEnd>=LINE_INPUT_MAX_CHAR-1){ my_fatal("PipeStruct::ReadInput(): LINE_INPUT_MAX_CHAR is equal to %d which is too small to contain a full line of engine output or GUI input.\n",LINE_INPUT_MAX_CHAR); } } LeaveCriticalSection(&CriticalSection); if(EOF_input() || bSetEvent){ SetEvent(hEvent); } }
static uint64 read_integer(FILE * file, int size) { uint64 n; int i; int b; ASSERT(file!=NULL); ASSERT(size>0&&size<=8); n = 0; for (i = 0; i < size; i++) { b = fgetc(file); if (b == EOF) { if (feof(file)) { my_fatal("read_integer(): fgetc(): EOF reached\n"); } else { // error my_fatal("read_integer(): fgetc(): %s\n",strerror(errno)); } } ASSERT(b>=0&&b<256); n = (n << 8) | b; } return n; }
void egbb_init(void) { // init Egbb->init = false; Egbb->load = false; Egbb->size = CacheSize; Egbb->piece_nb = 0; Egbb->read_hit = 0; Egbb->read_nb = 0; LibHandler = LOAD_LIB(EgbbLib); if (LibHandler != NULL) { send("egbb library loaded"); load_egbb_ptr = (load_egbb_5men) GET_ENTRY(LibHandler,"load_egbb_5men"); probe_egbb_ptr = (probe_egbb_5men) GET_ENTRY(LibHandler,"probe_egbb_5men"); if (load_egbb_ptr == NULL) my_fatal("egbb_init(): load_egbb_5men() not found\n"); if (probe_egbb_ptr == NULL) my_fatal("egbb_init(): probe_egbb_5men() not found\n"); Egbb->init = true; } }
static void parse_option() { const char * file_name; FILE * file; char line[256]; char * name, * value; file_name = option_get_string("OptionFile"); file = fopen(file_name,"r"); if (file == NULL) my_fatal("Can't open file \"%s\": %s\n",file_name,strerror(errno)); // PolyGlot options (assumed first) while (true) { if (!my_file_read_line(file,line,256)) { my_fatal("parse_option(): missing [Engine] section\n"); } if (my_string_case_equal(line,"[engine]")) break; if (parse_line(line,&name,&value)) option_set(name,value); } if (option_get_bool("Log")) { my_log_open(option_get_string("LogFile")); } my_log("POLYGLOT %s *** START ***\n",Version); my_log("POLYGLOT INI file \"%s\"\n",file_name); engine_open(Engine); Init = true; uci_open(Uci,Engine); while (my_file_read_line(file,line,256)) { if (line[0] == '[') my_fatal("parse_option(): unknown section %s\n",line); if (parse_line(line,&name,&value)) { uci_send_option(Uci,name,"%s",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 if(my_string_case_equal(name,"MultiPV") && atoi(value)>1) Uci->multipv_mode=true; } } uci_send_isready(Uci); fclose(file); if (my_string_equal(option_get_string("EngineName"),"<empty>")) { option_set("EngineName",Uci->name); } }
bool io_get_line(io_t * io, char string[], int size) { int src, dst; int c; ASSERT(io_is_ok(io)); ASSERT(string!=NULL); ASSERT(size>=256); src = 0; dst = 0; while (true) { // test for end of buffer if (src >= io->in_size) { if (io->in_eof) { my_log("< %s EOF\n",io->name); return false; } else { my_fatal("io_get_line(): no EOL in buffer\n"); } } // test for end of string if (dst >= size) my_fatal("io_get_line(): buffer overflow\n"); // copy the next character c = io->in_buffer[src++]; if (c == LF) { // LF => line complete string[dst] = '\0'; break; } else if (c != CR) { // skip CRs string[dst++] = c; } } // shift the buffer ASSERT(src>0); io->in_size -= src; ASSERT(io->in_size>=0); if (io->in_size > 0) memmove(&io->in_buffer[0],&io->in_buffer[src],io->in_size); // return my_log("< %s %s\n",io->name,string); return true; }
bool pgn_next_game(pgn_t * pgn) { char name[PGN_STRING_SIZE]; char value[PGN_STRING_SIZE]; ASSERT(pgn!=NULL); // init strcpy(pgn->result,"*"); strcpy(pgn->fen,""); // loop while (true) { pgn_token_read(pgn); if (pgn->token_type != '[') break; // tag pgn_token_read(pgn); if (pgn->token_type != TOKEN_SYMBOL) { my_fatal("pgn_next_game(): malformed tag at line %d, column %d\n",pgn->token_line,pgn->token_column); } strcpy(name,pgn->token_string); pgn_token_read(pgn); if (pgn->token_type != TOKEN_STRING) { my_fatal("pgn_next_game(): malformed tag at line %d, column %d\n",pgn->token_line,pgn->token_column); } strcpy(value,pgn->token_string); pgn_token_read(pgn); if (pgn->token_type != ']') { my_fatal("pgn_next_game(): malformed tag at line %d, column %d\n",pgn->token_line,pgn->token_column); } // special tag? if (false) { } else if (my_string_equal(name,"Result")) { strcpy(pgn->result,value); } else if (my_string_equal(name,"FEN")) { strcpy(pgn->fen,value); } } if (pgn->token_type == TOKEN_EOF) return false; pgn_token_unread(pgn); return true; }
static void comp_move(int move) { board_t board[1]; char string[256]; ASSERT(move_is_ok(move)); ASSERT(State->state==THINK); ASSERT(!XB->analyse); if(option_get_bool(Option,"RepeatPV")) send_pv(); // to update time and nodes // send the move game_get_board(Game,board); if (move_is_castle(move,board) && option_get_bool(Option,"Chess960")) { if (!move_to_san(move,board,string,256)) my_fatal("comp_move(): move_to_san() failed\n"); // O-O/O-O-O } else { if (!move_to_can(move,board,string,256)) my_fatal("comp_move(): move_to_can() failed\n"); } gui_send(GUI,"move %s",string); // resign? if (option_get_bool(Option,"Resign") && Uci->root_move_nb > 1) { if (Uci->best_score <= -abs(option_get_int(Option,"ResignScore"))) { State->resign_nb++; my_log("POLYGLOT %d move%s with resign score\n",State->resign_nb,(State->resign_nb>1)?"s":""); if (State->resign_nb >= option_get_int(Option,"ResignMoves")) { my_log("POLYGLOT *** RESIGN ***\n"); gui_send(GUI,"resign"); } } else { if (State->resign_nb > 0) my_log("POLYGLOT resign reset (State->resign_nb=%d)\n",State->resign_nb); State->resign_nb = 0; } } // play the move move_step(move); no_mess(move); }
void DrawBoardPart(int x, int y, int c) { int p; switch(c) { case ',': p=SPR_SPACE; break; case '.': p=SPR_DOT; break; case '*': p=SPR_POWER; break; case 'n': p=SPR_HORIZ; break; case 'j': p=SPR_VERT; break; case 'b': p=SPR_DOWNRIGHT; break; case 'd': p=SPR_DOWNLEFT; break; case 'k': p=SPR_UPRIGHT; break; case 'm': p=SPR_UPLEFT; break; case 'u': p=SPR_ROUND; break; case 't': p=SPR_UP; break; case 'p': p=SPR_LEFT; break; case 'o': p=SPR_RIGHT; break; case 'c': p=SPR_TDOWN; break; case 'l': p=SPR_TUP; break; case 'h': p=SPR_TRIGHT; break; case 'f': p=SPR_TLEFT; break; case 'e': p=SPR_HGATE; break; case 's': p=SPR_DOWN; break; case 'g': p=SPR_CROSS; break; default: { my_fatal((char *)"Illegal draw char error!\n"); exit(5); } break; } //destw->draw(picts[p],x*BITMAP_X,y*BITMAP_Y); destw->copyBlock(srcs,picts[p]->x,picts[p]->y,x*BITMAP_X,y*BITMAP_Y,BITMAP_X,BITMAP_Y); }
void io_send_queue(io_t * io, const char format[], ...) { va_list arg_list; char string[StringSize]; int len; ASSERT(io_is_ok(io)); ASSERT(format!=NULL); ASSERT(io->out_fd>=0); // format va_start(arg_list,format); vsprintf(string,format,arg_list); va_end(arg_list); // append string to buffer len = strlen(string); if (io->out_size + len > BufferSize-2) my_fatal("io_send_queue(): buffer overflow\n"); memcpy(&io->out_buffer[io->out_size],string,len); io->out_size += len; ASSERT(io->out_size>=0&&io->out_size<=BufferSize-2); }
static void my_write(int fd, const char string[], int size) { int n; ASSERT(fd>=0); ASSERT(string!=NULL); ASSERT(size>0); do { n = write(fd,string,size); // if (n == -1 && errno != EINTR && errno != EPIPE) my_fatal("my_write(): write(): %s\n",strerror(errno)); if (n == -1) { if (false) { } else if (errno == EINTR) { n = 0; // nothing has been written } else if (errno == EPIPE) { n = size; // pretend everything has been written } else { my_fatal("my_write(): write(): %s\n",strerror(errno)); } } ASSERT(n>=0); string += n; size -= n; } while (size > 0); ASSERT(size==0); }
void pgn_open(pgn_t * pgn, const char file_name[]) { ASSERT(pgn!=NULL); ASSERT(file_name!=NULL); pgn->file = fopen(file_name,"r"); if (pgn->file == NULL) my_fatal("pgn_open(): can't open file \"%s\": %s\n",file_name,strerror(errno)); pgn->char_hack = CHAR_EOF; // DEBUG pgn->char_line = 1; pgn->char_column = 0; pgn->char_unread = false; pgn->char_first = true; pgn->token_type = TOKEN_ERROR; // DEBUG strcpy(pgn->token_string,"?"); // DEBUG pgn->token_length = -1; // DEBUG pgn->token_line = -1; // DEBUG pgn->token_column = -1; // DEBUG pgn->token_unread = false; pgn->token_first = true; strcpy(pgn->result,"?"); // DEBUG strcpy(pgn->fen,"?"); // DEBUG pgn->move_line = -1; // DEBUG pgn->move_column = -1; // DEBUG }
static void pgn_token_read(pgn_t * pgn) { ASSERT(pgn!=NULL); // token "stack" if (pgn->token_unread) { pgn->token_unread = false; return; } // consume the current token if (pgn->token_first) { pgn->token_first = false; } else { ASSERT(pgn->token_type!=TOKEN_ERROR); ASSERT(pgn->token_type!=TOKEN_EOF); } // read a new token pgn_read_token(pgn); if (pgn->token_type == TOKEN_ERROR) my_fatal("pgn_token_read(): lexical error at line %d, column %d\n",pgn->char_line,pgn->char_column); if (DispToken) printf("< L%d C%d \"%s\" (%03X)\n",pgn->token_line,pgn->token_column,pgn->token_string,pgn->token_type); }
static void move_step(int move) { board_t board[1]; char move_string[256]; ASSERT(move_is_ok(move)); // log game_get_board(Game,board); if (XB->ics || (move != MoveNone && move_is_legal(move,board))) { move_to_san(move,board,move_string,sizeof(move_string)); my_log("POLYGLOT MOVE %s\n",move_string); } else { move_to_can(move,board,move_string,sizeof(move_string)); my_log("POLYGLOT ILLEGAL MOVE \"%s\"\n",move_string); board_disp(board); //since we have threads my_fatal is not enough,1 thread will wait for xboard to end the game //stuff illegal move in the comment as well,not everybody logs all the time. if(board->turn==White) xboard_send(XBoard,"0-1 {polyglot: %s illegal engine move white}\n",move_string); else xboard_send(XBoard,"1-0 {polyglot: %s illegal engine move black}\n",move_string); my_fatal("move_step(): illegal move \"%s\"\n",move_string); } // play the move game_add_move(Game,move); //board_update(); }
void option_insert(option_list_t *option, option_t *new_option){ int i; option_t *opt; ASSERT(option!=NULL); ASSERT(new_option!=NULL); ASSERT(new_option->name!=NULL); opt=option_find(option,new_option->name); if(!opt){ opt=&option->options[option->option_nb]; option->option_nb++; } if(option->option_nb>=OptionNb){ my_fatal("option_insert(): option list overflow\n"); } if(new_option->name) my_string_set(&opt->name, new_option->name); if(new_option->value) my_string_set(&opt->value, new_option->value); if(new_option->min) my_string_set(&opt->min, new_option->min); if(new_option->max) my_string_set(&opt->max, new_option->max); if(new_option->default_) my_string_set(&opt->default_, new_option->default_); if(new_option->type) my_string_set(&opt->type, new_option->type); opt->var_nb=new_option->var_nb; for(i=0;i<new_option->var_nb;i++){ my_string_set(&opt->var[i], new_option->var[i]); } opt->mode=new_option->mode; }
static void my_dup2(int old_fd, int new_fd) { ASSERT(old_fd>=0); ASSERT(new_fd>=0); if (dup2(old_fd,new_fd) == -1) my_fatal("my_dup2(): dup2(): %s\n",strerror(errno)); }
static void move_step(int move) { board_t board[1]; char move_string[256]; ASSERT(move_is_ok(move)); // log game_get_board(Game,board); if (move != MoveNone && move_is_legal(move,board)) { move_to_san(move,board,move_string,256); my_log("POLYGLOT MOVE %s\n",move_string); } else { move_to_can(move,board,move_string,256); my_log("POLYGLOT ILLEGAL MOVE \"%s\"\n",move_string); board_disp(board); my_fatal("move_step(): illegal move \"%s\"\n",move_string); } // play the move game_add_move(Game,move); board_update(); }
void parent_init(int reqfd, int msgfd, pid_t child) { // events struct event ev_cmd, ev_msg; struct event ev_sigchld, ev_sigint, ev_sigterm, ev_sighup; #ifdef HAVE_LIBCAP // capabilities cap_t caps; #endif /* HAVE_LIBCAP */ // init the queues TAILQ_INIT(&rawfds); // proctitle setproctitle("parent [priv]"); // setup global sockets sock = my_socket(AF_INET, SOCK_DGRAM, 0); mfd = msgfd; // debug if (options & OPT_DEBUG) { dfd = STDOUT_FILENO; my_pcap_init(dfd); #if HAVE_LIBCAP_NG } else { capng_clear(CAPNG_SELECT_BOTH); capng_updatev(CAPNG_ADD, CAPNG_EFFECTIVE|CAPNG_PERMITTED, CAP_KILL, CAP_NET_ADMIN, CAP_NET_RAW, CAP_NET_BROADCAST, -1); if (capng_apply(CAPNG_SELECT_BOTH) == -1) my_fatal("unable to set capabilities"); #elif HAVE_LIBCAP } else {
bool my_file_read_line(FILE * file, char string[], int size) { int src, dst; int c; ASSERT(file!=NULL); ASSERT(string!=NULL); ASSERT(size>0); if (fgets(string,size,file) == NULL) { if (feof(file)) { return false; } else { // error my_fatal("my_file_read_line(): fgets(): %s\n",strerror(errno)); } } // remove CRs and LFs src = 0; dst = 0; while ((c=string[src++]) != '\0') { if (c != '\r' && c != '\n') string[dst++] = c; } string[dst] = '\0'; return true; }
void book_open(const char file_name[]) { ASSERT(file_name!=NULL); BookFile = fopen(file_name,"rb"); if (BookFile != NULL) { if (fseek(BookFile,0,SEEK_END) == -1) { my_fatal("book_open(): fseek(): %s\n",strerror(errno)); } BookSize = ftell(BookFile) / 16; if (BookSize == -1) my_fatal("book_open(): ftell(): %s\n",strerror(errno)); } }
void pipex_exit(pipex_t *pipex, int kill_timeout){ int status; int elapsed_time; bool exited; int ret; my_log("POLYGLOT Waiting for child process to exit.\n"); elapsed_time=0; exited=FALSE; ret=0; while(elapsed_time<kill_timeout){ ret=waitpid(pipex->pid,&status,WNOHANG); if(ret==0){ my_log("POLYGLOT Child has not exited yet. Sleeping %dms.\n", WAIT_GRANULARITY); my_sleep(WAIT_GRANULARITY); elapsed_time+=WAIT_GRANULARITY; }else{ exited=TRUE; break; } } if(!exited){ my_log("POLYGLOT Child wouldn't exit by itself. Terminating it.\n"); kill(pipex->pid,SIGKILL); waitpid(pipex->pid,&status,0); } if(WIFEXITED(status)){ if(pipex->quit_pending){ my_log("POLYGLOT Child exited with status %d.\n",WEXITSTATUS(status)); }else{ // Suppress further messages. pipex->quit_pending=TRUE; my_fatal("pipex_exit(): %s: child exited with status %d.\n",pipex->command,WEXITSTATUS(status)); } }else if(WIFSIGNALED(status)){ if(pipex->quit_pending){ my_log("POLYGLOT pipex_exit(): %s: child terminated with signal %d.\n",pipex->command,WTERMSIG(status)); }else{ // Suppress further messages. pipex->quit_pending=TRUE; my_fatal("pipex_exit(): %s: child terminated with signal %d.\n",pipex->command,WTERMSIG(status)); } } return; }
void ReadIDString(FILE *tfh, char *name, char *id, char *ss) { std::cerr<<"ReadIDString(,"<<name<<","<<id<<",)...\n"; int len; char fid[5],c; fid[4]=0; len=fread((BYTE_POINTER)fid,1,1,tfh); if(fid[0]==13) len=fread((BYTE_POINTER)fid,1,1,tfh); if(fid[0]==10) len=fread((BYTE_POINTER)fid,1,1,tfh); len=fread((BYTE_POINTER)(fid+1),3,1,tfh); std::cerr<<"len is "<<len<<"\n"; if(len!=1) { my_fatal((char *)"Read error in IDString\n"); exit(0); } if(strcmp(fid,id)) { std::cerr<<"ID error in IDString\n"; boardError=true; return; } MyReadString(tfh,ss,STRING_LEN); len=fread((BYTE_POINTER)&c,1,1,tfh); std::cerr<<"len is "<<len<<"\n"; if(len!=1) { my_fatal((char *)"Read error NL\n"); exit(0); } }
// move_step() void engine_move_fail(char *move_string){ board_t board[1]; game_get_board(Game,board); if(XB->ics) return; // if(board->turn==White) xboard_send(XBoard,"0-1 {polyglot: %s engine move format-error white}\n",move_string); else xboard_send(XBoard,"1-0 {polyglot: %s engine move format-error black}\n",move_string); my_fatal("parse_bestmove(): not a move \"%s\"\n",move_string); }
void * my_realloc(void * address, int size) { ASSERT(address!=NULL); ASSERT(size>0); address = realloc(address,size); if (address == NULL) my_fatal("my_realloc(): realloc(): %s\n",strerror(errno)); return address; }
const char * option_get(const char var[]) { option_t * opt; ASSERT(var!=NULL); opt = option_find(var); if (opt == NULL) my_fatal("option_get(): unknown option \"%s\"\n",var); return opt->val; }
char ReadBoard(int x, int y) { #ifdef CHECK_BOARD if((x>=0)&&(y>=0)&&(x<MAXBOARD_X)&&(y<MAXBOARD_Y)) #endif { return Board[x][y]; } #ifdef CHECK_BOARD else { my_fatal((char *)"Illegal Read error!\n"); exit(5); } return 0; #endif }
void * my_malloc(int size) { void * address; ASSERT(size>0); address = malloc(size); if (address == NULL) my_fatal("my_malloc(): malloc(): %s\n",strerror(errno)); return address; }
const char * option_get(option_list_t *option, const char name[]) { option_t * opt; ASSERT(name!=NULL); opt = option_find(option,name); if (opt == NULL) my_fatal("option_get(): unknown option \"%s\"\n",name); if (UseDebug) my_log("POLYGLOT OPTION GET \"%s\" -> \"%s\"\n",opt->name,opt->value); return opt->value; }
void WriteBoard(int x, int y, int c) { #ifdef CHECK_BOARD if((x>=0)&&(y>=0)&&(x<MAXBOARD_X)&&(y<MAXBOARD_Y)) #endif { Board[x][y]=c; } #ifdef CHECK_BOARD else { my_fatal((char *)"Illegal write\n"); exit(0); } #endif }
const char * option_get(const char var[]) { option_t * opt; ASSERT(var!=NULL); opt = option_find(var); if (opt == NULL) my_fatal("option_get(): unknown option \"%s\"\n",var); if (UseDebug) my_log("POLYGLOT OPTION GET \"%s\" -> \"%s\"\n",opt->var,opt->val); return opt->val; }