diff --git a/src/chessdefs.h b/src/chessdefs.h index ecd4805..39b6822 100644 --- a/src/chessdefs.h +++ b/src/chessdefs.h @@ -142,42 +142,6 @@ typedef u8 dir_t; #define sq_upwest(up) ((up) - 1) #define sq_upeast(up) ((up) + 1) -#include - -typedef struct mclock { - clockid_t clocktype; - ulong elapsed_l; - double elapsed_f; - struct timespec start; -} mclock_t; - -#define CLOCK_WALL CLOCK_REALTIME -#define CLOCK_SYSTEM CLOCK_MONOTONIC_RAW -#define CLOCK_PROCESS CLOCK_PROCESS_CPUTIME_ID -#define CLOCK_THREAD CLOCK_THREAD_CPUTIME_ID - -/** - * CLOCK_DEFINE - define a clock type. - * @name: clock name - * @type: clock type - * - * This macro is equivalent to: - * mclock_t name; - * clock_init(&name, type); - */ -#define CLOCK_DEFINE(name, type) struct mclock name = { .clocktype = type } - -void clock_init(mclock_t *clock, clockid_t type); -void clock_start(mclock_t *clock); -s64 clock_elapsed_μs(mclock_t *clock); -s64 clock_elapsed_ms(mclock_t *clock); -double clock_elapsed_sec(mclock_t *clock); - -#define RAND_SEED_DEFAULT U64(0xb0d1ccea) - -void rand_init(u64 seed); -u64 rand64(void); - void init_all(void); #endif /* _CHESSDEFS_H */ diff --git a/src/hash.c b/src/hash.c index b33172b..989005a 100644 --- a/src/hash.c +++ b/src/hash.c @@ -19,6 +19,7 @@ #include #include "chessdefs.h" +#include "util.h" #include "alloc.h" #include "position.h" #include "piece.h" diff --git a/src/init.c b/src/init.c index cc49abe..3aa598d 100644 --- a/src/init.c +++ b/src/init.c @@ -16,7 +16,7 @@ #include #include "chessdefs.h" - +#include "util.h" #include "bitboard.h" #include "hq.h" #include "eval-defs.h" diff --git a/src/misc.h b/src/misc.h deleted file mode 100644 index 20ced35..0000000 --- a/src/misc.h +++ /dev/null @@ -1,18 +0,0 @@ -/* util.h - various util functions. - * - * Copyright (C) 2024 Bruno Raoult ("br") - * Licensed under the GNU General Public License v3.0 or later. - * Some rights reserved. See COPYING. - * - * You should have received a copy of the GNU General Public License along with this - * program. If not, see . - * - * SPDX-License-Identifier: GPL-3.0-or-later - * - */ - -#ifndef _UTIL_H -#define _UTIL_H - - -#endif /* UTIL_H */ diff --git a/src/uci.c b/src/uci.c index 13a0087..bef697d 100644 --- a/src/uci.c +++ b/src/uci.c @@ -13,13 +13,14 @@ #include #include -#include -#include +//#include +//#include #include #include #include "chessdefs.h" +#include "util.h" #include "position.h" #include "uci.h" #include "hist.h" @@ -47,6 +48,7 @@ int do_ucinewgame(pos_t *, char *); int do_uci(pos_t *, char *); int do_isready(pos_t *, char *); int do_quit(pos_t *, char *); +int do_setoption(pos_t *, char *); int do_position(pos_t *, char *); @@ -59,20 +61,19 @@ int do_hist(pos_t *, char *); int do_help(pos_t *, char *); struct command commands[] = { - { "help", do_help, "(not UCI) This help" }, - { "?", do_help, "(not UCI) This help" }, { "quit", do_quit, "Quit" }, - { "uci", do_uci, "" }, { "ucinewgame", do_ucinewgame, "" }, { "isready", do_isready, "" }, - + { "setoption name", do_setoption, ""}, { "position", do_position, "position startpos|fen [moves ...]" }, { "perft", do_perft, "(not UCI) perft [divide] [alt] depth" }, { "moves", do_moves, "(not UCI) moves ..." }, { "diagram", do_diagram, "(not UCI) print current position diagram" }, { "hist", do_hist, "(not UCI) print history states" }, + { "help", do_help, "(not UCI) This help" }, + { "?", do_help, "(not UCI) This help" }, { NULL, (int(*)()) NULL, NULL } }; @@ -245,6 +246,20 @@ int do_isready(__unused pos_t *pos, __unused char *arg) return 1; } +int do_setoption(__unused pos_t *pos, __unused char *arg) +{ + __unused char *name, *value, *saveptr; + + /* Note: space in var name are unsupported */ + saveptr = NULL; + name = strtok_r(arg, " ", &saveptr); + + value = strstr(arg, "value"); + + return 1; +} + + int do_position(pos_t *pos, char *arg) { char *saveptr, *token, *fen, *moves; @@ -423,48 +438,6 @@ int do_help(__unused pos_t *pos, __unused char *arg) * } */ -/** - * string_trim - cleanup (trim) blank characters in string. - * @str: &string to clean - * - * str is cleaned and packed with the following rules: - * - Leading and trailing blank characters are removed. - * - consecutive blank characters are replaced by one space. - * - non printable characters are removed. - * - * "blank" means characters as understood by isspace(3): space, form-feed ('\f'), - * newline ('\n'), carriage return ('\r'), horizontal tab ('\t'), and vertical - * tab ('\v'). - * - * @return: new @str len. - */ -int string_trim(char *str) -{ - char *to = str, *from = str; - int state = 1; - - while (*from) { - switch (state) { - case 1: /* blanks */ - while (*from && isspace(*from)) - from++; - state = 0; - break; - case 0: /* token */ - while (*from && !isspace(*from)) { - if (isprint(*from)) - *to++ = *from; - from++; - } - *to++ = ' '; - state = 1; - } - } - if (to > str) - to--; - *to = 0; - return to - str; -} static int done = 0; @@ -476,12 +449,12 @@ int do_quit(__unused pos_t *pos, __unused char *arg) int uci(pos_t *pos) { char *str = NULL, *saveptr, *token, *args; - int len; size_t lenstr = 0; struct command *command; while (!done && getline(&str, &lenstr, stdin) >= 0) { - if (!(len = string_trim(str))) + str = str_trim(str); + if (! *str) continue; token = strtok_r(str, " ", &saveptr); if (! (command= find_command(token))) { diff --git a/src/misc.c b/src/util.c similarity index 64% rename from src/misc.c rename to src/util.c index 00b0044..d3ce4ab 100644 --- a/src/misc.c +++ b/src/util.c @@ -1,4 +1,4 @@ -/* misc.c - generic/catchall functions. +/* util.c - generic/catchall functions. * * Copyright (C) 2024 Bruno Raoult ("br") * Licensed under the GNU General Public License v3.0 or later. @@ -11,12 +11,15 @@ * */ -#include -#include +//#include +//#include +#include +#include #include #include "chessdefs.h" +#include "util.h" /* * 1 sec = 1000 millisec @@ -146,3 +149,75 @@ u64 rand64(void) rand_seed ^= rand_seed >> 27; return rand_seed * 0x2545f4914f6cdd1dull; } + +/** + * str_trim - cleanup (trim) blank characters in string. + * @str: &string to clean + * + * str is cleaned and packed with the following rules: + * - Leading and trailing blank characters are removed. + * - consecutive blank characters are replaced by one space. + * - non printable characters are removed. + * + * "blank" means characters as understood by isspace(3): space, form-feed ('\f'), + * newline ('\n'), carriage return ('\r'), horizontal tab ('\t'), and vertical + * tab ('\v'). + * + * @return: new @str len. + */ +char *str_trim(char *str) +{ + char *to = str, *from = str; + int state = 1; + + while (*from) { + switch (state) { + case 1: /* blanks */ + while (*from && isspace(*from)) + from++; + state = 0; + break; + case 0: /* token */ + while (*from && !isspace(*from)) { + if (isprint(*from)) + *to++ = *from; + from++; + } + *to++ = ' '; + state = 1; + } + } + if (to > str) + to--; + *to = 0; + return to; +} + +/** + * str_token - find a token in string. + * @str: string to search + * @token: token to look for + * + * Look for token @token in string @str. + * A token is a string delimited by space characters. However, it may contain + * space characters itself. + * + * @return: @token address if found, NULL otherwise. + */ +char *str_token(const char *str, const char *token) +{ + int len = strlen(token); + const char *found = str; + + while (found && *found) { + printf("trying pos=%ld\n", found - str); + found = strstr(found, token); + if (found) { + if (!found[len] || isspace(found[len])) + break; + found = strchr(found, ' ') + 1; + printf("new pos=%ld\n", found - str); + } + } + return (char *) found; +} diff --git a/src/util.h b/src/util.h new file mode 100644 index 0000000..7249498 --- /dev/null +++ b/src/util.h @@ -0,0 +1,60 @@ +/* util.h - various util functions. + * + * Copyright (C) 2024 Bruno Raoult ("br") + * Licensed under the GNU General Public License v3.0 or later. + * Some rights reserved. See COPYING. + * + * You should have received a copy of the GNU General Public License along with this + * program. If not, see . + * + * SPDX-License-Identifier: GPL-3.0-or-later + * + */ + +#ifndef _UTIL_H +#define _UTIL_H + +#include + +#include + +typedef struct mclock { + clockid_t clocktype; + ulong elapsed_l; + double elapsed_f; + struct timespec start; +} mclock_t; + +#define CLOCK_WALL CLOCK_REALTIME +#define CLOCK_SYSTEM CLOCK_MONOTONIC_RAW +#define CLOCK_PROCESS CLOCK_PROCESS_CPUTIME_ID +#define CLOCK_THREAD CLOCK_THREAD_CPUTIME_ID + +/** + * CLOCK_DEFINE - define a clock type. + * @name: clock name + * @type: clock type + * + * This macro is equivalent to: + * mclock_t name; + * clock_init(&name, type); + */ +#define CLOCK_DEFINE(name, type) struct mclock name = { .clocktype = type } + +void clock_init(mclock_t *clock, clockid_t type); +void clock_start(mclock_t *clock); +s64 clock_elapsed_μs(mclock_t *clock); +s64 clock_elapsed_ms(mclock_t *clock); +double clock_elapsed_sec(mclock_t *clock); + +#define RAND_SEED_DEFAULT U64(0xb0d1ccea) + +void rand_init(u64 seed); +u64 rand64(void); + + +char *str_trim(char *str); +char *str_token(const char *str, const char *token); + + +#endif /* UTIL_H */ diff --git a/test/perft-test.c b/test/perft-test.c index 0fccb5a..a2034a1 100644 --- a/test/perft-test.c +++ b/test/perft-test.c @@ -19,6 +19,7 @@ #include #include "chessdefs.h" +#include "util.h" #include "fen.h" #include "position.h" #include "move.h"