64 lines
2 KiB
C
64 lines
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);
|
|
static inline void kubo_command_write(struct kubo_context *context);
|
|
|
|
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) {
|
|
for (size_t i = 0; i < kubo_commands_size; i++) {
|
|
if (strcmp(kubo_commands[i].input, kubo_char_arr_build_str(&command)) ==
|
|
0) {
|
|
kubo_commands[i].function(context);
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
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) {
|
|
context->exit_pending = true;
|
|
}
|
|
|
|
static inline void kubo_command_write(struct kubo_context *context) {
|
|
kubo_file_write(context);
|
|
}
|