kubo/kubo_command.c
2025-07-26 01:51:24 +02:00

72 lines
2.2 KiB
C

/*
* Copyright Luka Jankovic 2025
*
* This file is part of Kubo.
*
* Kubo is free software: you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later
* version.
*
* Kubo is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
* A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* Kubo. If not, see <https://www.gnu.org/licenses/>.
*/
#include "kubo_command.h"
static inline void kubo_command_exit(struct kubo_context *context, char *rest);
static inline void kubo_command_write(struct kubo_context *context, char *rest);
static const struct kubo_command_data kubo_commands[] = {
{":q", kubo_command_exit}, {":w", kubo_command_write}};
static const size_t kubo_commands_size =
sizeof(kubo_commands) / sizeof(kubo_commands[0]);
static struct kubo_char_arr command;
void kubo_command_init() { kubo_char_arr_init(&command); }
void kubo_command_cleanup() { kubo_char_arr_free(&command); }
void kubo_command_append_char(int c) { kubo_char_arr_add(&command, c); }
void kubo_command_pop() {
if (command.count > 1) {
kubo_char_arr_pop(&command);
}
}
void kubo_command_accept_cmd(struct kubo_context *context) {
char *cmd = kubo_command_get_str();
cmd = strtok(cmd, " ");
char *rest = strtok(NULL, " ");
for (size_t i = 0; i < kubo_commands_size; i++) {
if (strcmp(cmd, kubo_commands[i].input) == 0) {
kubo_commands[i].function(context, rest);
return;
}
}
free(cmd);
}
char *kubo_command_get_str() { return kubo_char_arr_build_str(&command); }
void kubo_command_clear() { kubo_char_arr_clear(&command); }
static inline void kubo_command_exit(struct kubo_context *context, char *rest) {
(void)rest;
context->exit_pending = true;
}
static inline void kubo_command_write(struct kubo_context *context, char *rest) {
if (rest) {
context->file_name = rest;
}
kubo_file_write(context);
}