File: search.c

package info (click to toggle)
alevt 1:1.6.2-4
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 956 kB
  • ctags: 662
  • sloc: ansic: 6,284; makefile: 119; perl: 104; sh: 15
file content (128 lines) | stat: -rw-r--r-- 2,008 bytes parent folder | download | duplicates (4)
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include <sys/types.h>	// for freebsd
#include <stdlib.h>
#include "vt.h"
#include "misc.h"
#include "cache.h"
#include "search.h"


static void
convert(u8 *p, u8 *buf, int *line)
{
    int x, y, c, ch, gfx, hid = 0;

    for (y = 1, p += 40; y < 25; ++y)
    {
	if (not hid)
	{
	    gfx = 0;
	    for (x = 0; x < 40; ++x)
	    {
		c = ' ';
		switch (ch = *p++)
		{
		    case 0x00 ... 0x07:
			gfx = 0;
			break;
		    case 0x10 ... 0x17:
			gfx = 1;
			break;
		    case 0x0c:
			hid = 1;
			break;
		    case 0x7f:
			c = '*';
			break;
		    case 0x20 ... 0x7e:
			if (gfx && ch != ' ' && (ch & 0xa0) == 0x20)
			    ch = '#';
		    case 0xa0 ... 0xff:
			c= ch;
		}
		*buf++ = c;
	    }
	    *buf++ = '\n';
	    *line++ = y;
	}
	else
	{
	    p += 40;
	    hid = 0;
	}
    }
    *line = y;
    *buf = 0;
}

static int
search_pg(struct search *s, struct vt_page *vtp)
{
    regmatch_t m[1];
    u8 buf[H *(W+1) + 1];
    int line[H];

    convert(PTR vtp->data, buf, line);
    if (regexec(s->pattern, buf, 1, m, 0) == 0)
    {
	s->len = 0;
	if (m->rm_so >= 0)
	{
	    s->y = line[m->rm_so / (W+1)];
	    s->x = m->rm_so % (W+1);
	    s->len = m->rm_eo - m->rm_so;
	    if (s->x + s->len > 40)
		s->len = 40 - s->x;
	}
	return 1;
    }
    return 0;
}

struct search *
search_start(struct cache *ca, u8 *pattern)
{
    struct search *s;
    int f = 0;

    if (not(s = malloc(sizeof(*s))))
	goto fail1;

    if (pattern[0] == '!')
	pattern++;
    else
	f = REG_ICASE;

    if (regcomp(s->pattern, pattern, f | REG_NEWLINE) != 0)
	goto fail2;

    s->cache = ca;
    return s;

fail2:
    free(s);
fail1:
    return 0;
}

void
search_end(struct search *s)
{
    regfree(s->pattern);
    free(s);
}

int
search_next(struct search *s, int *pgno, int *subno, int dir)
{
    struct vt_page *vtp = 0;

    if (s->cache)
	vtp = s->cache->op->foreach_pg(s->cache, *pgno, *subno, dir,
								search_pg, s);
    if (vtp == 0)
	return -1;

    *pgno = vtp->pgno;
    *subno = vtp->subno ?: ANY_SUB;
    return 0;
}