summaryrefslogtreecommitdiff
path: root/src/uci.c
blob: 9e3c8c79a58a0d8649787af952dada05d155d47f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <assert.h>

#include "uci/state.h"
#include "uci/command.h"
#include "uci/response.h"
#include "uci.h"
#include "ipc.h"

void load_from_message(ucicmd* cmd, const char* message) {
  char token[MAX_TOKEN_SIZE];
  int i = 0, k = 0;

  while (message[i] != 0 && message[i] != '\n' && message[i] != '\r') {
    if (message[i] != ' ') { 
      token[k++] = message[i++];
      continue;
    }
    if (k == 0) {
      i++;
      continue;
    }
    i++;
    token[k] = 0;
    ucicmd_add(cmd, token);
    k = 0;
  }
  token[k] = 0;
  ucicmd_add(cmd, token);
}

void *uci(void *_com) {
  comms *com = (comms*)_com;
  // https://stackoverflow.com/a/41559081
  fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK);

  com->state = (uci_state) {0};
  strncpy(com->state.name, "Gacrux", 32);
  strncpy(com->state.author, "Aargh Rai <aarghrai.com>", 32);
  int k = 0;
  com->state.option_settings[k++] = option_setting_spin(
    "Threads",
    1, 5, 1,
    &com->state.threads
  );
  com->state.option_settings[k++] = option_setting_spin(
    "Hash",
    0, 512, 256,
    &com->state.hash
  );
  com->state.option_settings[k++] = option_setting_button(
    "Clear Hash",
    &com->state.clear_hash
  );
  com->state.option_settings[k++] = option_setting_check(
    "UCI_ShowCurrLine",
    false,
    &com->state.uci_showcurrline
  );
  com->state.option_settings[k++] = option_setting_check(
    "UCI_ShowRefutations",
    false,
    &com->state.uci_showrefutations
  );
  com->state.option_settings[k++] = option_setting_check(
    "UCI_LimitStrength",
    false,
    &com->state.uci_limitstrength
  );
  com->state.option_settings[k++] = option_setting_spin(
    "UCI_Elo",
    100, 3500, 800,
    &com->state.uci_elo
  );
  com->state.option_settings[k++] = option_setting_check(
    "UCI_AnalyseMode",
    false,
    &com->state.uci_analysemode
  );
  com->state.option_settings[k++] = option_setting_string(
    "UCI_Opponent",
    "<empty>",
    &com->state.uci_opponent
  );
  com->state.option_settings[k++] = option_setting_string(
    "UCI_EngineAbout",
    "Gacrux by Aargh Rai, Checkout https://git.aarghrai.com/gacrux",
    &com->state.uci_engineabout
  );
  com->state.option_settings[k++] = option_setting_string(
    "UCI_SetPositionValue",
    "<empty>",
    &com->state.uci_setpositionvalue
  );

  ucicmd cmd = ucicmd_init();
  size_t n;
  char buf[1024];

  while (true) {
    cmd.empty = true;
    cmd.args_count = 0;
    ssize_t n = read(STDIN_FILENO, buf, sizeof(buf));
    if (n > 0) {
      load_from_message(&cmd, buf);
    }
    handle_uci(
      &com->state,
      com->engine_messages,
      cmd
    );
  }
}

#include "uci/command.c"
#include "uci/response.c"
#include "uci/state.c"