#include "console.h" #include #include LineStatus console_line(FILE *stream, char *buffer, size_t capacity) { size_t used = 0; int over = 0; int ch; while ((ch = fgetc(stream)) != EOF && ch != '\n') { if (used + 1 < capacity) { buffer[used++] = (char)ch; } else { over = 1; } } if (ferror(stream)) { return LINE_IO; } if (ch == EOF && used == 0 && !over) { return LINE_END; } if (used && buffer[used - 1] == '\r') { --used; } buffer[used] = '\0'; return over ? LINE_LONG : LINE_TEXT; } int console_ignored(const char *line) { while (*line && isspace((unsigned char)*line)) { ++line; } return *line == '\0' || *line == '#'; } static size_t tokenize(char *line, char **tokens, size_t capacity) { size_t count = 0; char *cursor = line; while (*cursor) { while (*cursor && isspace((unsigned char)*cursor)) { ++cursor; } if (!*cursor) { break; } if (count == capacity) { return capacity + 1; } tokens[count++] = cursor; while (*cursor && !isspace((unsigned char)*cursor)) { ++cursor; } if (*cursor) { *cursor++ = '\0'; } } return count; } static int decimal(const char *token, uint64_t *result) { uint64_t value = 0; if (!*token) { return 0; } for (const unsigned char *p = (const unsigned char *)token; *p; ++p) { if (*p < '0' || *p > '9') { return 0; } unsigned digit = *p - '0'; if (value > (UINT64_MAX - digit) / 10u) { return 0; } value = value * 10u + digit; } *result = value; return 1; } static int bounded(const char *token, uint64_t maximum, uint64_t *result) { return decimal(token, result) && *result <= maximum; } static int parse_words(char **tokens, size_t start, Command *command) { for (unsigned i = 0; i < command->count; ++i) { uint64_t value; if (!bounded(tokens[start + i], UINT16_MAX, &value)) { return 0; } command->words[i] = (uint16_t)value; } return 1; } Command console_parse(char *line) { Command command = {0}; char *tokens[CW_TOKEN_LIMIT]; size_t count = tokenize(line, tokens, CW_TOKEN_LIMIT); if (count == 0 || count > CW_TOKEN_LIMIT || strlen(tokens[0]) != 1) { return command; } char kind = tokens[0][0]; if (kind == 'R') { if (count == 1) { command.kind = CMD_RESET; } return command; } if (count < 2) { return command; } uint64_t value; if (!bounded(tokens[1], CW_SLOTS - 1, &value)) { return command; } command.slot = (unsigned)value; if (kind == 'D' && count == 3) { if (decimal(tokens[2], &command.expected)) { command.kind = CMD_DROP; } } else if (kind == 'E' && count == 3) { if (bounded(tokens[2], UINT32_MAX, &value)) { command.input = (uint32_t)value; command.kind = CMD_EXECUTE; } } else if (kind == 'I' && count >= 4) { if (bounded(tokens[2], CW_CAPACITY, &value) && value > 0) { command.count = (unsigned)value; if (count == 3 + value && parse_words(tokens, 3, &command)) { command.kind = CMD_INSTALL; } } } else if (kind == 'P' && count >= 6) { if (!decimal(tokens[2], &command.expected)) { return command; } if (!bounded(tokens[3], CW_CAPACITY - 1, &value)) { return command; } command.first = (unsigned)value; if (bounded(tokens[4], CW_CAPACITY, &value) && value > 0) { command.count = (unsigned)value; if (count == 5 + value && parse_words(tokens, 5, &command)) { command.kind = CMD_PATCH; } } } return command; }