summaryrefslogtreecommitdiff
path: root/src/uci.c
blob: 351211d132ce1ebaf56aeded6ef404910ac14ec4 (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
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include "uci/command.h"
#include "uci/response.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);
}

int main() {
  // https://stackoverflow.com/a/41559081
  fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK);

  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(cmd);
  }
}

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