summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/uci.c21
-rw-r--r--src/uci/response.c65
-rw-r--r--src/uci/response.h15
-rw-r--r--src/uci/state.c60
-rw-r--r--src/uci/state.h91
5 files changed, 231 insertions, 21 deletions
diff --git a/src/uci.c b/src/uci.c
index 351211d..027c1c7 100644
--- a/src/uci.c
+++ b/src/uci.c
@@ -1,9 +1,12 @@
#include <stdio.h>
+#include <string.h>
#include <stdbool.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
+
+#include "uci/state.h"
#include "uci/command.h"
#include "uci/response.h"
@@ -33,6 +36,21 @@ int main() {
// https://stackoverflow.com/a/41559081
fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK);
+ uci_state state = {0};
+ strncpy(state.name, "Gacrux", 32);
+ strncpy(state.author, "Aargh Rai <aarghrai.com>", 32);
+ int k = 0;
+ state.option_settings[k++] = option_setting_spin("Threads", 1, 5, 1);
+ state.option_settings[k++] = option_setting_spin("Hash", 0, 512, 256);
+ state.option_settings[k++] = option_setting_button("Clear Hash");
+ state.option_settings[k++] = option_setting_check("UCI_ShowCurrLine", false);
+ state.option_settings[k++] = option_setting_check("UCI_ShowRefutations", false);
+ state.option_settings[k++] = option_setting_check("UCI_LimitStrength", false);
+ state.option_settings[k++] = option_setting_spin("UCI_Elo", 100, 3500, 800);
+ state.option_settings[k++] = option_setting_check("UCI_AnalyseMode", false);
+ state.option_settings[k++] = option_setting_string("UCI_Opponent", "<empty>");
+ state.option_settings[k++] = option_setting_string("UCI_EngineAbout", "Gacrux by Aargh Rai, Checkout https://git.aarghrai.com/gacrux");
+ state.option_settings[k++] = option_setting_string("UCI_SetPositionValue", "<empty>");
ucicmd cmd = ucicmd_init();
size_t n;
char buf[1024];
@@ -43,9 +61,10 @@ int main() {
if (n > 0) {
load_from_message(&cmd, buf);
}
- handle_uci(cmd);
+ handle_uci(&state, cmd);
}
}
#include "uci/command.c"
#include "uci/response.c"
+#include "uci/state.c"
diff --git a/src/uci/response.c b/src/uci/response.c
index b27bf76..1f32f55 100644
--- a/src/uci/response.c
+++ b/src/uci/response.c
@@ -2,6 +2,39 @@
#include <assert.h>
#include "response.h"
+// examples from uci_min.txt
+// "option name Nullmove type check default true\n"
+// "option name Selectivity type spin default 2 min 0 max 4\n"
+// "option name Style type combo default Normal var Solid var Normal var Risky\n"
+// "option name NalimovPath type string default c:\\n"
+// "option name Clear Hash type button\n"
+void print_setting(option_setting_t setting) {
+ printf("option name %s type ", setting.option_name);
+ // accidently did the yoda style if statement, i am keeping it
+ if (OPTION_SPIN == setting.type) {
+ option_spin_setting_t data = setting.data.spin;
+ printf(
+ "spin default %d min %d max %d\n",
+ data.default_value,
+ data.min,
+ data.max
+ );
+ } else if (OPTION_COMBO == setting.type) {
+ option_combo_setting_t data = setting.data.combo;
+ printf("combo default %s", data.combination[data.default_index]);
+ for (int i = 0; i < data.count; i++) {
+ printf(" var %s", data.combination[i]);
+ }
+ printf("\n");
+ } else if (OPTION_CHECK == setting.type) {
+ printf("check default %d\n", setting.data.check_default);
+ } else if (OPTION_STRING == setting.type) {
+ printf("string default %s\n", setting.data.string_default);
+ } else if (OPTION_BUTTON == setting.type) {
+ printf("button\n");
+ } else { assert(0); }
+}
+
enum {
STATE_INITIAL,
STATE_IDLE,
@@ -17,32 +50,38 @@ void todo() {
assert(0);
}
-void handle_uci(ucicmd cmd) {
+void handle_uci(uci_state *state, ucicmd cmd) {
if (current_state == STATE_INITIAL) {
- return handle_initial(cmd);
+ return handle_initial(state, cmd);
}
if (current_state == STATE_IDLE) {
- return handle_idle(cmd);
+ return handle_idle(state, cmd);
}
if (current_state == STATE_SYNC) {
- return handle_sync(cmd);
+ return handle_sync(state, cmd);
}
if (current_state == STATE_PING) {
- return handle_ping(cmd);
+ return handle_ping(state, cmd);
}
if (current_state == STATE_ACTIVE) {
- return handle_active(cmd);
+ return handle_active(state, cmd);
}
- return handle_halt(cmd);
+ return handle_halt(state, cmd);
}
-void handle_initial(ucicmd cmd) {
+void handle_initial(uci_state *state, ucicmd cmd) {
if (cmd.empty || strcmp(cmd.root, "uci") != 0) return;
+ printf("id name %s\n", state->name);
+ printf("id author %s\n", state->author);
+ int settings_count = sizeof(state->option_settings)/sizeof(option_setting_t);
+ for (int i = 0; i < settings_count; i++) {
+ print_setting(state->option_settings[i]);
+ }
printf("uciok\n");
current_state = STATE_IDLE;
}
-void handle_idle(ucicmd cmd) {
+void handle_idle(uci_state *state, ucicmd cmd) {
if (cmd.empty) return;
if (strcmp(cmd.root, "debug") == 0) {
debug = strcmp(ucicmd_get_arg(cmd, 0), "on") == 0;
@@ -62,14 +101,14 @@ void handle_idle(ucicmd cmd) {
}
}
-void handle_sync(ucicmd cmd) {
+void handle_sync(uci_state *state, ucicmd cmd) {
// when i come up with some info to give:
// printf("info\n");
printf("readyok\n");
current_state = STATE_IDLE;
}
-void handle_ping(ucicmd cmd) {
+void handle_ping(uci_state *state, ucicmd cmd) {
// when i come up with some info to give:
// printf("info\n");
//
@@ -81,7 +120,7 @@ void handle_ping(ucicmd cmd) {
current_state = STATE_ACTIVE;
}
-void handle_active(ucicmd cmd) {
+void handle_active(uci_state *state, ucicmd cmd) {
if (cmd.empty) {
return;
}
@@ -95,7 +134,7 @@ void handle_active(ucicmd cmd) {
}
}
-void handle_halt(ucicmd cmd) {
+void handle_halt(uci_state *state, ucicmd cmd) {
// when i come up with some info to give:
// printf("info\n");
printf("bestmove 0000\n"); // TODO: idk get it rn
diff --git a/src/uci/response.h b/src/uci/response.h
index 09a0124..17204f5 100644
--- a/src/uci/response.h
+++ b/src/uci/response.h
@@ -1,9 +1,10 @@
#include "command.h"
+#include "state.h"
-void handle_uci(ucicmd cmd);
-void handle_initial(ucicmd cmd);
-void handle_idle(ucicmd cmd);
-void handle_sync(ucicmd cmd);
-void handle_ping(ucicmd cmd);
-void handle_active(ucicmd cmd);
-void handle_halt(ucicmd cmd);
+void handle_uci(uci_state *state, ucicmd cmd);
+void handle_initial(uci_state *state, ucicmd cmd);
+void handle_idle(uci_state *state, ucicmd cmd);
+void handle_sync(uci_state *state, ucicmd cmd);
+void handle_ping(uci_state *state, ucicmd cmd);
+void handle_active(uci_state *state, ucicmd cmd);
+void handle_halt(uci_state *state, ucicmd cmd);
diff --git a/src/uci/state.c b/src/uci/state.c
new file mode 100644
index 0000000..848c6a9
--- /dev/null
+++ b/src/uci/state.c
@@ -0,0 +1,60 @@
+#include "state.h"
+
+option_setting_t option_setting_combo(
+ char* option_name,
+ char** combinations,
+ int combinations_count,
+ int default_value
+) {
+ option_setting_t output = {0};
+ memcpy(output.option_name, option_name, 32);
+ output.type = OPTION_COMBO;
+ output.data.combo = (option_combo_setting_t) {
+ combinations,
+ combinations_count,
+ default_value
+ };
+ return output;
+}
+
+option_setting_t option_setting_spin(
+ const char* option_name,
+ int min,
+ int max,
+ int default_value
+) {
+ option_setting_t output = {0};
+ memcpy(output.option_name, option_name, 32);
+ output.type = OPTION_SPIN;
+ output.data.spin = (option_spin_setting_t) { min, max, default_value };
+ return output;
+}
+
+option_setting_t option_setting_check(
+ const char* option_name,
+ bool default_value
+) {
+ option_setting_t output = {0};
+ memcpy(output.option_name, option_name, 32);
+ output.type = OPTION_CHECK;
+ output.data.check_default = default_value;
+ return output;
+}
+
+option_setting_t option_setting_string(
+ const char* option_name,
+ const char* default_value
+) {
+ option_setting_t output = {0};
+ memcpy(output.option_name, option_name, 32);
+ output.type = OPTION_STRING;
+ output.data.string_default = default_value;
+ return output;
+}
+
+option_setting_t option_setting_button(const char* option_name) {
+ option_setting_t output = {0};
+ memcpy(output.option_name, option_name, 32);
+ output.type = OPTION_BUTTON;
+ return output;
+}
diff --git a/src/uci/state.h b/src/uci/state.h
new file mode 100644
index 0000000..98b42dd
--- /dev/null
+++ b/src/uci/state.h
@@ -0,0 +1,91 @@
+#ifndef UCI_STATE_H
+#define UCI_STATE_H
+
+typedef struct {
+ char* data;
+ int length;
+} str_t;
+
+typedef bool check_t;
+typedef int spin_t;
+typedef struct {
+ str_t* combinations;
+ int chosen_option;
+} combo_t;
+// the button isn't actually a value thing
+// it's more like an event.
+// so we parse a button type, we set this to true, handle it
+// then set it to false again
+typedef bool button_t;
+
+typedef struct {
+ char** combination;
+ int count;
+ int default_index;
+} option_combo_setting_t;
+typedef struct {
+ int min;
+ int max;
+ int default_value;
+} option_spin_setting_t;
+enum { OPTION_SPIN, OPTION_COMBO, OPTION_CHECK, OPTION_STRING, OPTION_BUTTON };
+typedef struct {
+ char option_name[32];
+ int type;
+ union {
+ option_combo_setting_t combo;
+ option_spin_setting_t spin;
+ bool check_default;
+ const char* string_default;
+ } data;
+} option_setting_t;
+
+option_setting_t option_setting_combo(
+ char* option_name,
+ char** combinations,
+ int combinations_count,
+ int default_index
+);
+option_setting_t option_setting_spin(
+ const char* option_name,
+ int min,
+ int max,
+ int default_value
+);
+option_setting_t option_setting_check(
+ const char* option_name,
+ bool default_value
+);
+option_setting_t option_setting_string(
+ const char* option_name,
+ const char* default_value
+);
+option_setting_t option_setting_button(const char* option_name);
+
+typedef struct {
+ char name[32];
+ char author[32];
+
+ option_setting_t option_settings[10];
+
+ // options
+ spin_t threads;
+ spin_t hash;
+ button_t clear_hash;
+ // str_t nalimovpath;
+ // spin_t nalimovcache;
+ // check_t ponder;
+ // check_t ownbook;
+ // spin_t multipv;
+ check_t uci_showcurrline;
+ check_t uci_showrefutations;
+ check_t uci_limitstrength;
+ spin_t uci_elo;
+ check_t uci_analysemode;
+ str_t uci_opponent;
+ str_t uci_engineabout;
+ // str_t uci_shredderbasespath;
+ str_t uci_setpositionvalue;
+} uci_state;
+
+#endif // UCI_STATE_H