Blame


1 2051e653 2021-03-13 op /*
2 2051e653 2021-03-13 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
3 2051e653 2021-03-13 op *
4 2051e653 2021-03-13 op * Permission to use, copy, modify, and distribute this software for any
5 2051e653 2021-03-13 op * purpose with or without fee is hereby granted, provided that the above
6 2051e653 2021-03-13 op * copyright notice and this permission notice appear in all copies.
7 2051e653 2021-03-13 op *
8 2051e653 2021-03-13 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 2051e653 2021-03-13 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 2051e653 2021-03-13 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 2051e653 2021-03-13 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 2051e653 2021-03-13 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 2051e653 2021-03-13 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 2051e653 2021-03-13 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 2051e653 2021-03-13 op */
16 2051e653 2021-03-13 op
17 786e6deb 2021-07-21 op #include "compat.h"
18 2051e653 2021-03-13 op
19 2051e653 2021-03-13 op #include <stdlib.h>
20 2051e653 2021-03-13 op
21 786e6deb 2021-07-21 op #include "telescope.h"
22 786e6deb 2021-07-21 op
23 2051e653 2021-03-13 op void
24 bf935370 2022-01-05 op hist_clear(struct histhead *head)
25 bf935370 2022-01-05 op {
26 bf935370 2022-01-05 op struct hist *h, *th;
27 bf935370 2022-01-05 op
28 bf935370 2022-01-05 op TAILQ_FOREACH_SAFE(h, &head->head, entries, th) {
29 bf935370 2022-01-05 op TAILQ_REMOVE(&head->head, h, entries);
30 bf935370 2022-01-05 op free(h);
31 bf935370 2022-01-05 op }
32 bf935370 2022-01-05 op head->len = 0;
33 bf935370 2022-01-05 op }
34 bf935370 2022-01-05 op
35 bf935370 2022-01-05 op void
36 2051e653 2021-03-13 op hist_clear_forward(struct histhead *head, struct hist *h)
37 2051e653 2021-03-13 op {
38 2051e653 2021-03-13 op if (h == NULL)
39 2051e653 2021-03-13 op return;
40 2051e653 2021-03-13 op hist_clear_forward(head, TAILQ_NEXT(h, entries));
41 2051e653 2021-03-13 op TAILQ_REMOVE(&head->head, h, entries);
42 2051e653 2021-03-13 op free(h);
43 2051e653 2021-03-13 op }
44 2051e653 2021-03-13 op
45 2051e653 2021-03-13 op void
46 2051e653 2021-03-13 op hist_push(struct histhead *head, struct hist *h)
47 2051e653 2021-03-13 op {
48 2051e653 2021-03-13 op head->len++;
49 32ac17a4 2021-08-12 op TAILQ_INSERT_TAIL(&head->head, h, entries);
50 2051e653 2021-03-13 op }
51 fcd99a0d 2021-11-05 op
52 1040cc7f 2021-01-02 op void
53 1040cc7f 2021-01-02 op hist_add_before(struct histhead *head, struct hist *curr, struct hist *h)
54 1040cc7f 2021-01-02 op {
55 1040cc7f 2021-01-02 op head->len++;
56 1040cc7f 2021-01-02 op TAILQ_INSERT_BEFORE(curr, h, entries);
57 1040cc7f 2021-01-02 op }
58 1040cc7f 2021-01-02 op
59 fcd99a0d 2021-11-05 op struct hist *
60 fcd99a0d 2021-11-05 op hist_pop(struct histhead *head)
61 fcd99a0d 2021-11-05 op {
62 fcd99a0d 2021-11-05 op struct hist *h, *p;
63 fcd99a0d 2021-11-05 op
64 fcd99a0d 2021-11-05 op if ((h = TAILQ_LAST(&head->head, mhisthead)) == NULL)
65 fcd99a0d 2021-11-05 op return NULL;
66 fcd99a0d 2021-11-05 op if ((p = TAILQ_PREV(h, mhisthead, entries)) == NULL)
67 fcd99a0d 2021-11-05 op return NULL;
68 fcd99a0d 2021-11-05 op
69 fcd99a0d 2021-11-05 op hist_clear_forward(head, h);
70 fcd99a0d 2021-11-05 op return p;
71 fcd99a0d 2021-11-05 op }