Blob


1 /*
2 * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
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 */
17 #include "compat.h"
19 #include <stdlib.h>
21 #include "telescope.h"
23 void
24 hist_clear(struct histhead *head)
25 {
26 struct hist *h, *th;
28 TAILQ_FOREACH_SAFE(h, &head->head, entries, th) {
29 TAILQ_REMOVE(&head->head, h, entries);
30 free(h);
31 }
32 head->len = 0;
33 }
35 void
36 hist_clear_forward(struct histhead *head, struct hist *h)
37 {
38 struct hist *i;
40 if (h == NULL)
41 return;
43 while ((i = TAILQ_NEXT(h, entries)) != NULL) {
44 TAILQ_REMOVE(&head->head, i, entries);
45 free(i);
46 head->len--;
47 }
49 TAILQ_REMOVE(&head->head, h, entries);
50 free(h);
51 head->len--;
52 }
54 void
55 hist_push(struct histhead *head, struct hist *h)
56 {
57 head->len++;
58 TAILQ_INSERT_TAIL(&head->head, h, entries);
59 }
61 void
62 hist_add_before(struct histhead *head, struct hist *curr, struct hist *h)
63 {
64 head->len++;
65 TAILQ_INSERT_BEFORE(curr, h, entries);
66 }
68 struct hist *
69 hist_pop(struct histhead *head)
70 {
71 struct hist *h, *p;
73 if ((h = TAILQ_LAST(&head->head, mhisthead)) == NULL)
74 return NULL;
75 if ((p = TAILQ_PREV(h, mhisthead, entries)) == NULL)
76 return NULL;
78 hist_clear_forward(head, h);
79 return p;
80 }