3 #include "system/stacktrace.h"
6 #include "system/str.h"
8 #include "system/nth_alloc.h"
20 History *create_history(size_t capacity)
24 History *history = PUSH_LT(
26 nth_calloc(1, sizeof(History)),
28 if (history == NULL) {
33 history->capacity = capacity;
37 history->buffer = PUSH_LT(lt, nth_calloc(capacity, sizeof(char*)), free);
38 if (history->buffer == NULL) {
45 void destroy_history(History *history)
47 trace_assert(history);
49 for (size_t i = 0; i < history->capacity; ++i) {
50 if (history->buffer[i] != NULL) {
51 free(history->buffer[i]);
55 RETURN_LT0(history->lt);
58 int history_push(History *history, const char *command)
60 trace_assert(history);
61 trace_assert(command);
63 const size_t next_begin = (history->begin + 1) % history->capacity;
65 if (history->buffer[history->begin] != NULL) {
66 free(history->buffer[history->begin]);
69 history->buffer[history->begin] = string_duplicate(command, NULL);
71 if (history->buffer[history->begin] == NULL) {
75 history->begin = next_begin;
76 history->cursor = next_begin;
81 const char *history_current(History *history)
83 trace_assert(history);
84 return history->buffer[history->cursor];
87 void history_prev(History *history)
89 trace_assert(history);
90 if (history->cursor == 0) {
91 history->cursor = history->capacity - 1;
97 void history_next(History *history)
99 trace_assert(history);
100 history->cursor = (history->cursor + 1) % history->capacity;