diff options
Diffstat (limited to 'src/uci.c')
| -rw-r--r-- | src/uci.c | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/src/uci.c b/src/uci.c new file mode 100644 index 0000000..3c45765 --- /dev/null +++ b/src/uci.c @@ -0,0 +1,55 @@ +#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" |
