File: tagmatch.c

package info (click to toggle)
wily 0.13.33-3
  • links: PTS
  • area: main
  • in suites: slink
  • size: 1,500 kB
  • ctags: 1,720
  • sloc: ansic: 12,830; sh: 245; makefile: 188
file content (128 lines) | stat: -rw-r--r-- 2,427 bytes parent folder | download | duplicates (2)
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 "wily.h"
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>

typedef struct Pair Pair;
struct Pair {
	char*	regex;
	char*	tools;
};

static Pair* pair;
static int	npairs = 0;
static int maxpairs = 0;

static Bool
match(char *regex, char *s) {
	/* Make a dummy text file, search in that */
	static Text *t=0;
	Range r;
	Bool	retval;
	
	if(!t)
		t  = text_alloc(0, false);
	r = nr;
	text_replaceutf(t, text_all(t), s);
	text_replaceutf(t, range(text_length(t),text_length(t)), "\n");
	retval =  text_utfregexp(t, regex, &r, true);
	
	return retval;
}

static void
addpair(char *regex, char* tools) {
	Pair p;
	char	*nl;
	
	p.regex = regex;
	p.tools = tools;
	if ((nl = strchr(p.tools, '\n'))) {
		*nl = 0;
	}
	if(npairs == maxpairs) {
		maxpairs = maxpairs? maxpairs*2 : maxpairs + 10;
		pair = (Pair*) srealloc(pair, maxpairs * sizeof(Pair));
	}
	pair[npairs++] = p;
}

char*
tag_match(char*label) {
	int	j;
	static Bool inprogress;
	
	if(inprogress) {
		j= npairs;
	} else {
		inprogress = true;
		for(j=0; j<npairs; j++)
			if(match(pair[j].regex, label))
				break;
		inprogress = false;
	}
	return (j<npairs)? pair[j].tools : "";
}

/* Alloc a buffer, read 'filename' into it, return the buffer.
 * Returns 0 if there's some problem.
 */
static char*
readfile(char*filename) {
	char *buf;
	struct stat statbuf;
	int	fd;
	off_t	size;
	int	nread;
	
	fd = open(filename, O_RDONLY);
	if( (fd<0) || fstat(fd, &statbuf)) {
		return 0;
	}
	
	size = statbuf.st_size;
	buf = salloc (size+10);
	while(size>0) {
		nread = read(fd, buf, size);
		if(nread <= 0) {
			perror(filename);
			free(buf);
			return 0;
		}
		size -= nread;
	}
	close(fd);
	return buf;
}

/*
 * Read 'filename', initialize 'pair' and 'npairs'.
 * 'filename' is made up of lines, each of which may
 * be blank, a comment (starts with '#'), or a pattern
 * toolset pair (separated by tabs)
 */
void
tag_init(char *filename) {
	char	*buf, *ptr, *tab;
	
	if(!(buf=readfile(filename)))
		return;
	
	for(ptr = strtok(buf, "\n"); ptr; ptr = strtok(0, "\n")) {
		/* comment or blank line */
		if (ptr[0] == '#' || isspace(ptr[0]))
			continue;
		if((tab = strchr(ptr, '\t'))) {
			*tab++ = 0;
			/* strip leading whitespace */
			tab += strspn(tab, whitespace);
			addpair(ptr, tab);
		} else {
			addpair(ptr, "");
		}
	}
	/* don't free(buf) - keep the memory in the array of strings */
}