summaryrefslogtreecommitdiffabout
path: root/alck.c
blob: f8a3d115c518bfab830fc593c87a0c7a8535f3a8 (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
/* ckaliases - verify syntax of sendmail-style alias files
   Copyright (C) 2005, 2007 Sergey Poznyakoff

   This program is free software; you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by the
   Free Software Foundation; either version 3 of the License, or (at your
   option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License along
   with this program. If not, see <http://www.gnu.org/licenses/>. */

#include "alck.h"

#ifndef CHAR_BIT
# define CHAR_BIT 8
#endif
#define BITS_PER_WORD   (sizeof(unsigned)*CHAR_BIT)
#define MAXTABLE        32767

#define WORDSIZE(n)     (((n) + BITS_PER_WORD - 1) / BITS_PER_WORD)
#define SETBIT(x, i)    ((x)[(i)/BITS_PER_WORD] |= (1<<((i) % BITS_PER_WORD)))
#define RESETBIT(x, i)  ((x)[(i)/BITS_PER_WORD] &= ~(1<<((i) % BITS_PER_WORD)))
#define BITISSET(x, i)  (((x)[(i)/BITS_PER_WORD] & (1<<((i) % BITS_PER_WORD))) != 0)

/* given n by n matrix of bits R, modify its contents
   to be the transitive closure of what was given.  */

void
TC(unsigned *R, int n)
{
	register int rowsize;
	register unsigned mask;
	register unsigned *rowj;
	register unsigned *rp;
	register unsigned *rend;
	register unsigned *ccol;

	unsigned *relend;
	unsigned *cword;
	unsigned *rowi;

	rowsize = WORDSIZE(n) * sizeof(unsigned);
	relend = (unsigned *) ((char *) R + (n * rowsize));
	
	cword = R;
	mask = 1;
	rowi = R;
	while (rowi < relend) {
		ccol = cword;
		rowj = R;

		while (rowj < relend) {
			if (*ccol & mask) {
				rp = rowi;
				rend = (unsigned *) ((char *) rowj + rowsize);
				
				while (rowj < rend)
					*rowj++ |= *rp++;
			} else {
				rowj = (unsigned *) ((char *) rowj + rowsize);
			}

			ccol = (unsigned *) ((char *) ccol + rowsize);
		}

		mask <<= 1;
		if (mask == 0) {
			mask = 1;
			cword++;
		}
		rowi = (unsigned *) ((char *) rowi + rowsize);
	}
}

struct alias {
	struct alias *prev, *next;
	char *name;
	int num;
	SLIST *exp;
};

struct ali_list {
	struct alias *head, *tail;
	size_t count;
};

struct ali_list aliases;

static int
ali_cmp(struct alias *a, struct alias *b)
{
	return strcmp(a->name, b->name);
}

static int
ali_cmp2(struct alias *a, struct alias *b)
{
	char *aname = a->name;
	char *bname = b->name;
	int rc;
	int alen;
	int blen;
	char *p;
	
	if ((p = strchr(aname, '@')) && slist_member(cw_list, p + 1))
		alen = p - aname;
	else
		alen = strlen(aname);

	if ((p = strchr(bname, '@')) && slist_member(cw_list, p + 1))
		blen = p - bname;
	else
		blen = strlen(bname);

	if (alen == blen)
		return memcmp(aname, bname, alen);

	return strcmp(aname, bname);
}

void
ali_insert(struct ali_list *lp,
	   struct alias *anchor,
	   struct alias *ent, int before)
{
	struct alias *p;

	if (!anchor) {
		ent->prev = NULL;
		ent->next = lp->head;
		if (lp->head)
			lp->head->prev = ent;
		else
			lp->tail = ent;
		lp->head = ent;
		lp->count++;
		return;
	}

	if (before) {
		ali_insert(lp, anchor->prev, ent, 0);
		return;
	}

	ent->prev = anchor;
	if (p = anchor->next)
		p->prev = ent;
	else
		lp->tail = ent;
	ent->next = p;
	anchor->next = ent;
	lp->count++;
}

void
ali_join(struct ali_list *a, struct ali_list *b)
{
	if (!b->head)
		return;
	b->head->prev = a->tail;
	if (a->tail)
		a->tail->next = b->head;
	else
		a->head = b->head;
	a->tail = b->tail;
}

void
ali_sort(struct ali_list *list, int (*cmp)(struct alias *, struct alias *))
{
	struct alias *cur, *middle;
	struct ali_list high_list, low_list;
	int rc;

	if (!list->head)
		return;
	cur = list->head;
	do {
		cur = cur->next;
		if (!cur)
			return;
	} while ((rc = cmp(list->head, cur)) == 0);

	/* Select the lower of the two as the middle value */
	middle = (rc > 0) ? cur : list->head;

	/* Split into two sublists */
	low_list.head = low_list.tail = NULL;
	high_list.head = high_list.tail = NULL;

	for (cur = list->head; cur; ) {
		struct alias *next = cur->next;
		cur->next = NULL;
		if (cmp(middle, cur) < 0)
			ali_insert(&high_list, high_list.tail, cur, 0);
		else
			ali_insert(&low_list, low_list.tail, cur, 0);
		cur = next;
	}

	/* Sort both sublists recursively */
	ali_sort(&low_list, cmp);
	ali_sort(&high_list, cmp);

	/* Join both lists in order */
	ali_join(&low_list, &high_list);

	/* Return the resulting list */
	list->head = low_list.head;
	list->tail = low_list.tail;
}

struct alias *
ali_locate(struct ali_list *lp, int (*cmp)(struct alias *, struct alias *),
	   struct alias *data)
{
	struct alias *ep;

	for (ep = lp->head; ep; ep = ep->next) {
		if (cmp(ep, data) == 0)
			return ep;
	}
	return NULL;
}

void
end_aliases()
{
	int i;
	struct alias *ali;
	
	ali_sort(&aliases, ali_cmp);
	if (!aliases.head)
		return;
	for (i = 0, ali = aliases.head; ali->next; ali = ali->next, i++) {
		if (ali_cmp(ali, ali->next) == 0) {
			error("alias `%s' multiply defined", ali->name);
			error_count++;
		}
		ali->num = i;
	}
}

void
regalias(char *name, SLIST *exp)
{
	struct alias *a = emalloc(sizeof(*a));
	a->name = name;
	a->exp = exp;
	a->next = NULL;
	ali_insert(&aliases, NULL, a, 0);
}

int
find_alias(char *name)
{
	struct alias a, *p;

	if (!name)
		return -1;
	a.name = name;
	p = ali_locate(&aliases, ali_cmp2, &a);
	return p ? p->num : -1;
}


static void
alias_setbit(unsigned *r, unsigned rowsize, unsigned row, unsigned col)
{
	SETBIT(r + rowsize * row, col);
}

static int
alias_bitisset(unsigned *r, unsigned rowsize, unsigned row, unsigned col)
{
	return BITISSET(r + rowsize * row, col);
}

void
mark_connected(unsigned *r, unsigned size)
{
	struct alias *ali;

	for (ali = aliases.head; ali; ali = ali->next) {
		if (ali->exp) {
			struct string_list *p;
			for (p = ali->exp->head; p; p = p->next) {
				int n = find_alias(p->str);
				if (n >= 0)
					alias_setbit(r, size, ali->num, n);
			}
		}
	}
}

void
check_circular_deps(unsigned *r, unsigned size)
{
	struct alias *ali;

	for (ali = aliases.head; ali; ali = ali->next) {
		if (alias_bitisset(r, size, ali->num, ali->num)) {
			error("%s: circular dependency", ali->name);
			error_count++;
		}
	}
}

void
check_aliases()
{
	size_t size;
	unsigned *r;

	/* Allocate matrix */
	size = (aliases.count + BITS_PER_WORD - 1) / BITS_PER_WORD;
	r = emalloc(aliases.count * size * sizeof(*r));
	memset(r, 0, aliases.count * size * sizeof(*r));

	/* First pass: mark directly connected entries */
	mark_connected(r, size);

	/* Compute transitive closure of the matrix r */
	TC(r, aliases.count);

	/* Third pass: check for circular deps */
	check_circular_deps(r, size);

	if (verbose)
		printf("%lu aliases\n", aliases.count);
}

Return to:

Send suggestions and report system problems to the System administrator.