This commit is contained in:
2024-07-25 09:26:07 +02:00
parent e5a9303d08
commit c93ed5ba01
2 changed files with 35 additions and 40 deletions

View File

@@ -13,9 +13,6 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h> #include <unistd.h>
#include <brlib.h> #include <brlib.h>

View File

@@ -1,6 +1,6 @@
/* uci.c - uci protocol /* uci.c - uci protocol
* *
* Copyright (C) 2024 Bruno Raoult ("br") * Copyright (C) 2021-2024 Bruno Raoult ("br")
* Licensed under the GNU General Public License v3.0 or later. * Licensed under the GNU General Public License v3.0 or later.
* Some rights reserved. See COPYING. * Some rights reserved. See COPYING.
* *
@@ -11,7 +11,6 @@
* *
*/ */
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@@ -30,7 +29,7 @@
#include "search.h" #include "search.h"
struct command { struct command {
char *name; /* User printable name */ char *name; /* command name */
int (*func)(pos_t *, char *); /* function doing the job */ int (*func)(pos_t *, char *); /* function doing the job */
char *doc; /* function doc */ char *doc; /* function doc */
}; };
@@ -46,6 +45,7 @@ int string_trim (char *str);
int do_ucinewgame(pos_t *, char *); int do_ucinewgame(pos_t *, char *);
int do_uci(pos_t *, char *); int do_uci(pos_t *, char *);
int do_isready(pos_t *, char *); int do_isready(pos_t *, char *);
int do_quit(pos_t *, char *);
int do_position(pos_t *, char *); int do_position(pos_t *, char *);
@@ -56,7 +56,6 @@ int do_perft(pos_t *, char *);
int do_hist(pos_t *, char *); int do_hist(pos_t *, char *);
int do_help(pos_t *, char *); int do_help(pos_t *, char *);
int do_quit(pos_t *, char *);
struct command commands[] = { struct command commands[] = {
{ "help", do_help, "(not UCI) This help" }, { "help", do_help, "(not UCI) This help" },
@@ -77,33 +76,6 @@ struct command commands[] = {
{ NULL, (int(*)()) NULL, NULL } { NULL, (int(*)()) NULL, NULL }
}; };
static int done = 0;
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)))
continue;
token = strtok_r(str, " ", &saveptr);
if (! (command= find_command(token))) {
fprintf(stderr, "Unknown [%s] command. Try 'help'.\n", token);
continue;
}
args = strtok_r(NULL, "", &saveptr);
execute_line(pos, command, args);
}
if (str)
free(str);
return 0;
}
/* Execute a command line. */ /* Execute a command line. */
int execute_line(pos_t *pos, struct command *command, char *args) int execute_line(pos_t *pos, struct command *command, char *args)
{ {
@@ -370,7 +342,6 @@ int do_perft(__unused pos_t *pos, __unused char *arg)
return 1; return 1;
} }
int do_hist(__unused pos_t *pos, __unused char *arg) int do_hist(__unused pos_t *pos, __unused char *arg)
{ {
hist_static_print(); hist_static_print();
@@ -387,11 +358,6 @@ int do_help(__unused pos_t *pos, __unused char *arg)
return 0; return 0;
} }
int do_quit(__unused pos_t *pos, __unused char *arg)
{
return done = 1;
}
/* /*
* int do_depth(__unused pos_t *pos, char *arg) * int do_depth(__unused pos_t *pos, char *arg)
* { * {
@@ -490,3 +456,35 @@ int string_trim(char *str)
*to = 0; *to = 0;
return to - str; return to - str;
} }
static int done = 0;
int do_quit(__unused pos_t *pos, __unused char *arg)
{
return done = 1;
}
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)))
continue;
token = strtok_r(str, " ", &saveptr);
if (! (command= find_command(token))) {
fprintf(stderr, "Unknown [%s] command. Try 'help'.\n", token);
continue;
}
args = strtok_r(NULL, "", &saveptr);
execute_line(pos, command, args);
}
if (str)
free(str);
return 0;
}