1 /*
2  * Copyright (c) 2011 Tim van der Molen <tim@kariliq.nl>
3  *
4  * Permission to use, copy, modify, and distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16 
17 #ifdef __OpenBSD__
18 #include <sys/queue.h>
19 #else
20 #include "compat/queue.h"
21 #endif
22 
23 #include <stdlib.h>
24 #include <string.h>
25 
26 #include "siren.h"
27 
28 struct history_entry {
29 	char			*line;
30 	TAILQ_ENTRY(history_entry) entries;
31 };
32 
33 TAILQ_HEAD(history_list, history_entry);
34 
35 struct history {
36 	struct history_list	 list;
37 	struct history_entry	*current_entry;
38 };
39 
40 void
history_add(struct history * h,const char * line)41 history_add(struct history *h, const char *line)
42 {
43 	struct history_entry *e;
44 
45 	/* Only add the entry if it is different from the last one. */
46 	if ((e = TAILQ_FIRST(&h->list)) == NULL || strcmp(e->line, line)) {
47 		e = xmalloc(sizeof *e);
48 		e->line = xstrdup(line);
49 		TAILQ_INSERT_HEAD(&h->list, e, entries);
50 	}
51 }
52 
53 void
history_free(struct history * h)54 history_free(struct history *h)
55 {
56 	struct history_entry *e;
57 
58 	while ((e = TAILQ_FIRST(&h->list)) != NULL) {
59 		TAILQ_REMOVE(&h->list, e, entries);
60 		free(e->line);
61 		free(e);
62 	}
63 	free(h);
64 }
65 
66 const char *
history_get_next(struct history * h)67 history_get_next(struct history *h)
68 {
69 	if (h->current_entry == NULL) {
70 		if ((h->current_entry = TAILQ_FIRST(&h->list)) == NULL)
71 			/* History is empty. */
72 			return NULL;
73 	} else {
74 		if (h->current_entry == TAILQ_LAST(&h->list, history_list))
75 			/* End of history reached. */
76 			return NULL;
77 		h->current_entry = TAILQ_NEXT(h->current_entry, entries);
78 	}
79 
80 	return h->current_entry->line;
81 }
82 
83 const char *
history_get_prev(struct history * h)84 history_get_prev(struct history *h)
85 {
86 	if (h->current_entry == NULL)
87 		return NULL;
88 
89 	if ((h->current_entry = TAILQ_PREV(h->current_entry, history_list,
90 	    entries)) == NULL)
91 		return NULL;
92 
93 	return h->current_entry->line;
94 }
95 
96 struct history *
history_init(void)97 history_init(void)
98 {
99 	struct history *h;
100 
101 	h = xmalloc(sizeof *h);
102 	TAILQ_INIT(&h->list);
103 	h->current_entry = NULL;
104 
105 	return h;
106 }
107 
108 void
history_rewind(struct history * h)109 history_rewind(struct history *h)
110 {
111 	h->current_entry = NULL;
112 }
113