File: hist.c

package info (click to toggle)
hx 0.7.10-2
  • links: PTS
  • area: main
  • in suites: potato
  • size: 564 kB
  • ctags: 834
  • sloc: ansic: 7,900; sh: 152; makefile: 84
file content (62 lines) | stat: -rw-r--r-- 1,349 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include "hx_types.h"
#include "hxlib.h"
#include "input.h"
#include <string.h>
#include "xmalloc.h"

void
hist_init (struct history_list *list)
{
	struct input *cur;

	cur = xmalloc(sizeof *cur);
	memset(cur, 0, sizeof *cur);
	cur->buf = xmalloc(4);
	list->cur = cur;
	list->cur->next = list->cur->prev = list->next = list->prev = cur;

	list->count = 0;
	list->max = 100;
}

void
hist_add (struct history_list *list)
{
	struct input *new;

	new = xmalloc(sizeof *new);
	memset(new, 0, sizeof *new);
	new->buf = xmalloc(4);
	if (list->count >= list->max) {
		struct input *next = list->next ? list->next->next : new;
		if (list->cur->next == list->next)
			list->cur->next = next;
		xfree(list->next);
		list->next = next;
	} else
		list->count++;
	if (list->cur->next != list->next && list->cur != list->prev) {
		list->cur->prev->next = list->cur->next;
		list->cur->next->prev = list->cur->prev;
		list->cur->prev = list->prev;
		list->prev->next = list->cur;
	}
	if (list->cur == list->next)
		list->next = list->next->next;
	list->cur->next = new;
	new->prev = list->prev = list->cur;
	new->next = list->next;
	list->next->prev = list->cur = new;
}

struct input *
hist_next (struct history_list *list)
{
	return (list->cur = list->cur->next);
}

struct input *
hist_prev (struct history_list *list)
{
	return (list->cur = list->cur->prev);
}