aboutsummaryrefslogtreecommitdiffstats
path: root/src/globset.c
blob: 124ff246b36767625347792f7a85c3e49a252cea (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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
/*
 * Copyright (C) 2015-2020 "IoT.bzh"
 * Author: José Bollo <jose.bollo@iot.bzh>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#define _GNU_SOURCE

#include <stdlib.h>
#include <string.h>
#include <errno.h>

#include "globset.h"

#define GLOB '*'

/*************************************************************************
 * internal types
 ************************************************************************/

/**
 * internal structure for handling patterns
 */
struct pathndl
{
	/* link to the next handler of the list */
	struct pathndl *next;

	/* the hash value for the pattern */
	size_t hash;

	/* the handler */
	struct globset_handler handler;
};

/**
 * Structure that handles a set of global pattern handlers
 */
struct globset
{
	/** linked list of global patterns */
	struct pathndl *globs;

	/** hash dictionary of exact matches */
	struct pathndl **exacts;

	/** mask used for accessing dictionary of exact matches */
	unsigned gmask;

	/** count of handlers stored in the dictionary of exact matches */
	unsigned count;
};

/**
 * Matches whether the string 'str' matches the pattern 'pat'
 * and returns its matching score.
 *
 * @param pat the glob pattern
 * @param str the string to match
 * @return 0 if no match or number representing the matching score
 */
static unsigned match(const char *pat, const char *str)
{
	unsigned r, rs, rr;
	char c;

	/* scan the prefix without glob */
	r = 1;
	while ((c = *pat++) != GLOB) {
		if (c != *str++)
			return 0; /* no match */
		if (!c)
			return r; /* match up to end */
		r++;
	}

	/* glob found */
	c = *pat++;
	if (!c)
		return r; /* not followed by pattern */

	/* evaluate the best score for following pattern */
	rs = 0;
	while (*str) {
		if (*str++ == c) {
			/* first char matches, check remaining string */
			rr = match(pat, str);
			if (rr > rs)
				rs = rr;
		}
	}

	/* best score or not match if rs == 0 */
	return rs ? rs + r : 0;
}

/**
 * Normalize the string 'from' to the string 'to' and computes the hash code.
 * The normalization translates upper characters to lower characters.
 * The returned hash code is greater than zero for exact patterns or zero
 * for global patterns.
 *
 * @param from string to normalize
 * @param to where to store the normalization
 * @return 0 if 'from' is a glob pattern or the hash code for exacts patterns
 */
static unsigned normhash(const char *from, char *to)
{
	unsigned i, hash;
	int isglob;
	char c;

	isglob = 0; /* is glob? */
	i = hash = 0; /* index and hash code */

	/* copy the normalized string and compute isglob and hash */
	while ((c = from[i])) {
		if (c == GLOB) {
			/* it is a glob pattern */
			isglob = 1;
			hash = 0;
		} else {
			/* normalize to lower */
			if (c >= 'A' && c <= 'Z')
				c = (char)(c + 'a' - 'A');
			/* compute hash if not glob */
			if (!isglob) {
				hash += (unsigned)(unsigned char)c;
				hash += hash << 10;
				hash ^= hash >> 6;
			}
		}
		to[i++] = c;
	}
	to[i] = c;
	if (!isglob) {
		/* finalize hash if not glob */
		hash += i;
		hash += hash << 3;
		hash ^= hash >> 11;
		hash += hash << 15;
		if (hash == 0)
			hash = 1 + i;
	}
	return hash;
}

/**
 * Ensure that there is enough place to store one more exact pattern
 * in the dictionary.
 *
 * @param set the set that has to grow if needed
 * @return 0 in case of success or -1 on case of error
 */
static int grow(struct globset *set)
{
	unsigned old_gmask, new_gmask, i;
	struct pathndl **new_exacts, **old_exacts, *ph, *next_ph;

	/* test if grow is needed */
	old_gmask = set->gmask;
	if (old_gmask - (old_gmask >> 2) > set->count + 1)
		return 0;

	/* compute the new mask */
	new_gmask = (old_gmask << 1) | 15;
	if (new_gmask + 1 <= new_gmask) {
		errno = EOVERFLOW;
		return -1;
	}

	/* allocates the new array */
	new_exacts = calloc(1 + (size_t)new_gmask, sizeof *new_exacts);
	if (!new_exacts)
		return -1;

	/* copy values */
	old_exacts = set->exacts;
	set->exacts = new_exacts;
	set->gmask = new_gmask;
	if (old_gmask) {
		for(i = 0 ; i <= old_gmask ; i++) {
			ph = old_exacts[i];
			while (ph) {
				next_ph = ph->next;
				ph->next = new_exacts[ph->hash & new_gmask];
				new_exacts[ph->hash & new_gmask] = ph;
				ph = next_ph;
			}
		}
		free(old_exacts);
	}

	/* release the previous values */
	return 0;
}

/**
 * Search in set the handler for the normalized pattern 'normal' of 'has' code.
 *
 * @param set the set to search
 * @param normal the normalized pattern
 * @param hash hash code of the normalized pattern
 * @param pprev pointer where to store the pointer pointing to the returned result
 * @return the handler found, can be NULL
 */
static struct pathndl *search(
		struct globset *set,
		const char *normal,
		unsigned hash,
		struct pathndl ***pprev)
{
	struct pathndl *ph, **pph;

	if (!hash)
		pph = &set->globs;
	else if (set->exacts)
		pph = &set->exacts[hash & set->gmask];
	else
		return NULL;
	while ((ph = *pph) && strcmp(normal, ph->handler.pattern))
		pph = &ph->next;
	*pprev = pph;
	return ph;
}








/**
 * Allocates a new set of handlers
 *
 * @return the new allocated global pattern set of handlers or NULL on failure
 */
struct globset *globset_create()
{
	return calloc(1, sizeof(struct globset));
}

/**
 * Destroy the set
 * @param set the set to destroy
 */
void globset_destroy(struct globset *set)
{
	unsigned i;
	struct pathndl *ph, *next_ph;

	/* free exact pattern handlers */
	if (set->gmask) {
		for(i = 0 ; i <= set->gmask ; i++) {
			ph = set->exacts[i];
			while (ph) {
				next_ph = ph->next;
				free(ph);
				ph = next_ph;
			}
		}
		free(set->exacts);
	}

	/* free global pattern handlers */
	ph = set->globs;
	while (ph) {
		next_ph = ph->next;
		free(ph);
		ph = next_ph;
	}

	/* free the set */
	free(set);
}

/**
 * Add a handler for the given pattern
 * @param set the set
 * @param pattern the pattern of the handler
 * @param callback the handler's callback
 * @param closure the handler's closure
 * @return 0 in case of success or -1 in case of error (ENOMEM, EEXIST)
 */
int globset_add(
		struct globset *set,
		const char *pattern,
		void *callback,
		void *closure)
{
	struct pathndl *ph, **pph;
	unsigned hash;
	size_t len;
	char *pat;

	/* normalize */
	len = strlen(pattern);
	pat = alloca(1 + len);
	hash = normhash(pattern, pat);

	/* grows if needed */
	if (hash && grow(set))
		return -1;

	/* search */
	ph = search(set, pat, hash, &pph);
	if (ph) {
		/* found */
		errno = EEXIST;
		return -1;
	}

	/* not found, create it */
	ph = malloc(1 + len + sizeof *ph);
	if (!ph)
		return -1;

	/* initialize it */
	ph->next = NULL;
	ph->hash = hash;
	ph->handler.callback = callback;
	ph->handler.closure = closure;
	strcpy(ph->handler.pattern, pat);
	*pph = ph;
	if (hash)
		set->count++;
	return 0;
}

/**
 * Delete the handler for the pattern
 * @param set the set
 * @param pattern the pattern to delete
 * @param closure where to put the closure if not NULL
 * @return 0 in case of success or -1 in case of error (ENOENT)
 */
int globset_del(
			struct globset *set,
			const char *pattern,
			void **closure)
{
	struct pathndl *ph, **pph;
	unsigned hash;
	char *pat;

	/* normalize */
	pat = alloca(1 + strlen(pattern));
	hash = normhash(pattern, pat);

	/* search */
	ph = search(set, pat, hash, &pph);
	if (!ph) {
		/* not found */
		errno = ENOENT;
		return -1;
	}

	/* found, remove it */
	if (hash)
		set->count--;
	*pph = ph->next;

	/* store the closure back */
	if (closure)
		*closure = ph->handler.closure;

	/* free the memory */
	free(ph);
	return 0;
}

/**
 * Search the handler of 'pattern'
 * @param set the set
 * @param pattern the pattern to search
 * @return the handler found or NULL
 */
struct globset_handler *globset_search(
			struct globset *set,
			const char *pattern)
{
	struct pathndl *ph, **pph;
	unsigned hash;
	char *pat;

	/* local normalization */
	pat = alloca(1 + strlen(pattern));
	hash = normhash(pattern, pat);

	/* search */
	ph = search(set, pat, hash, &pph);
	return ph ? &ph->handler : NULL;
}

/**
 * Search a handler for the string 'text'
 * @param set the set
 * @param text the text to match
 * @return the handler found or NULL
 */
const struct globset_handler *globset_match(
			struct globset *set,
			const char *text)
{
	struct pathndl *ph, *iph;
	unsigned hash, g, s;
	char *txt;

	/* local normalization */
	txt = alloca(1 + strlen(text));
	hash = normhash(text, txt);

	/* first, look in dictionary of exact matches */
	ph = NULL;
	if (hash && set->exacts) {
		ph = set->exacts[hash & set->gmask];
		while(ph && (ph->hash != hash || strcmp(txt, ph->handler.pattern)))
			ph = ph->next;
	}

	/* then if not found */
	if (ph == NULL) {
		/* look in glob patterns for the best match */
		s = 0;
		iph = set->globs;
		while(iph) {
			g = match(iph->handler.pattern, txt);
			if (g > s) {
				s = g;
				ph = iph;
			}
			iph = iph->next;
		}
	}
	return ph ? &ph->handler : NULL;
}