summaryrefslogtreecommitdiff
path: root/sys/src/cmd/ip/glob.c
blob: 346a1e480375305f86c37a34fb63f27ab4f411df (plain)
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#include <u.h>
#include <libc.h>
#include <regexp.h>
#include <String.h>
#include "glob.h"

/*
 *  I wrote this glob so that there would be no limit
 *  on element or path size.  The one in rc is probably
 *  better, certainly faster. - presotto
 */

static Glob*
globnew(void)
{
	Glob *g;

	g = mallocz(sizeof(*g), 1);
	if(g == nil)
		sysfatal("globnew: %r");
	return g;
}

static void
globfree1(Glob *g)
{
	s_free(g->glob);
	free(g);
}

static void
globfree(Glob *g)
{
	Glob *next;

	for(; g != nil; g = next){
		next = g->next;
		globfree1(g);
	}
}

static Globlist*
globlistnew(char *x)
{
	Globlist *gl;

	gl = mallocz(sizeof *gl, 1);
	if(gl == nil)
		sysfatal("globlistnew: %r");
	gl->first = globnew();
	gl->first->glob = s_copy(x);
	gl->l = &gl->first->next;
	return gl;
}

void
globlistfree(Globlist *gl)
{
	if(gl == nil)
		return;
	globfree(gl->first);
	free(gl);
}

void
globadd(Globlist *gl, char *dir, char *file)
{
	Glob *g;

	g = globnew();
	g->glob = s_copy(dir);
	if(strcmp(dir, "/") != 0 && *dir != 0)
		s_append(g->glob, "/");
	s_append(g->glob, file);
	*(gl->l) = g;
	gl->l = &(g->next); 
}

static void
globdir(Globlist *gl, char *dir, Reprog *re)
{
	Dir *d;
	int i, n, fd;

	if(*dir == 0)
		fd = open(".", OREAD);
	else
		fd = open(dir, OREAD);
	if(fd < 0)
		return;
	n = dirreadall(fd, &d);
	if(n == 0)
		return;
	close(fd);
	for(i = 0; i < n; i++)
		if(regexec(re, d[i].name, nil, 0))
			globadd(gl, dir, d[i].name);
	free(d);
}

static void
globdot(Globlist *gl, char *dir)
{
	Dir *d;

	if(*dir == 0){
		globadd(gl, "", ".");
		return;
	}
	d = dirstat(dir);
	if(d == nil)
		return;
	if(d->qid.type & QTDIR)
		globadd(gl, dir, ".");
	free(d);
}

static void
globnext(Globlist *gl, char *pattern)
{
	String *np;
	Glob *g, *inlist;
	Reprog *re;
	int c;

	/* nothing left */
	if(*pattern == 0)
		return;

	inlist = gl->first;
	gl->first = nil;
	gl->l = &gl->first;

	/* pick off next pattern and turn into a reg exp */
	np = s_new();
	s_putc(np, '^');
	for(; c = *pattern; pattern++){
		if(c == '/'){
			pattern++;
			break;
		}
		switch(c){
		case '|':
		case '+':
		case '.':
		case '^':
		case '$':
		case '(':
		case ')':
			s_putc(np, '\\');
			s_putc(np, c);
			break;
		case '?':
			s_putc(np, '.');
			break;
		case '*':
			s_putc(np, '.');
			s_putc(np, '*');
			break;
		default:
			s_putc(np, c);
			break;
		}
	}
	s_putc(np, '$');
	s_terminate(np);
	if(strcmp(s_to_c(np), "^\\.$") == 0){
		/* anything that's a directory works */
		for(g = inlist; g != nil; g = g->next)
			globdot(gl, s_to_c(g->glob));
	} else {
		re = regcomp(s_to_c(np));

		/* run input list as directories */
		for(g = inlist; g != nil; g = g->next)
			globdir(gl, s_to_c(g->glob), re);
		free(re);
	}
	s_free(np);
	globfree(inlist);

	if(gl->first != nil)
		globnext(gl, pattern);
}

char *
globiter(Globlist *gl)
{
	Glob *g;
	char *s;

	if(gl->first == nil)
		return nil;
	g = gl->first;
	gl->first = g->next;
	if(gl->first == nil)
		gl->l = &gl->first;
	s = strdup(s_to_c(g->glob));
	if(s == nil)
		sysfatal("globiter: %r");
	globfree1(g);
	return s;
}

Globlist*
glob(char *pattern)
{
	Globlist *gl;

	if(pattern == nil || *pattern == 0)
		return nil;
	if(*pattern == '/'){
		pattern++;
		gl = globlistnew("/");
	} else
		gl = globlistnew("");
	globnext(gl, pattern);
	return gl;
}