3 #include "system/stacktrace.h"
6 #include "system/str.h"
8 #include "system/nth_alloc.h"
20 History *create_history(size_t capacity)
27 History *history = PUSH_LT(
29 nth_alloc(sizeof(History)),
31 if (history == NULL) {
36 history->capacity = capacity;
40 history->buffer = PUSH_LT(lt, nth_calloc(capacity, sizeof(char*)), free);
41 if (history->buffer == NULL) {
48 void destroy_history(History *history)
50 trace_assert(history);
52 for (size_t i = 0; i < history->capacity; ++i) {
53 if (history->buffer[i] != NULL) {
54 free(history->buffer[i]);
58 RETURN_LT0(history->lt);
61 int history_push(History *history, const char *command)
63 trace_assert(history);
64 trace_assert(command);
66 const size_t next_begin = (history->begin + 1) % history->capacity;
68 if (history->buffer[history->begin] != NULL) {
69 free(history->buffer[history->begin]);
72 history->buffer[history->begin] = string_duplicate(command, NULL);
74 if (history->buffer[history->begin] == NULL) {
78 history->begin = next_begin;
79 history->cursor = next_begin;
84 const char *history_current(History *history)
86 trace_assert(history);
87 return history->buffer[history->cursor];
90 void history_prev(History *history)
92 trace_assert(history);
93 if (history->cursor == 0) {
94 history->cursor = history->capacity - 1;
100 void history_next(History *history)
102 trace_assert(history);
103 history->cursor = (history->cursor + 1) % history->capacity;