summaryrefslogtreecommitdiffabout
path: root/ckaliases.c
blob: da0a5c187ec089f6254db49240870c5f0c0a19f7 (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
/* 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 "ckaliases.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);
	}
}


void
slist_add(SLIST **plist, char *str)
{
	struct string_list *p = xmalloc(sizeof(*p));
	p->str = str;
	p->next = NULL;

	if (!*plist) {
		*plist = xmalloc(sizeof(**plist));
		(*plist)->head = NULL;
	}

	if ((*plist)->head == NULL) {
		(*plist)->head = p;
		(*plist)->count = 0;
	} else {
		(*plist)->tail->next = p;
		(*plist)->count++;
	}
	(*plist)->tail = p;
}

void
slist_append(SLIST **pdst, SLIST *src)
{
	struct string_list *tail;

	if (!*pdst) {
		*pdst = xmalloc(sizeof(**pdst));
		(*pdst)->head = NULL;
		(*pdst)->count = 0;
	}

	if ((*pdst)->head = NULL)
		(*pdst)->head = src->head;

	for (tail = src->tail; tail->next; tail = tail->next)
		;

	(*pdst)->tail = tail;
	(*pdst)->count += src->count;
}

char *
slist_member(SLIST *plist, char *name)
{
	struct string_list *p;
	
	if (plist)
		for (p = plist->head; p; p = p->next)
			if (p->str && strcmp(p->str, name) == 0)
				return p->str;
	return NULL;
}

void
slist_destroy(SLIST **plist)
{
	struct string_list *p;
	if (!plist || !*plist)
		return;
	p = (*plist)->head;
	while (p) {
		struct string_list *next = p->next;
		free(p);
		p = next;
	}
	free(*plist);
	*plist = NULL;
}


typedef struct {
	char *name;
	SLIST *exp;
} ALIAS;

struct obstack alias_stk;
unsigned alias_count;
ALIAS *aliases;

void
regalias(char *name, SLIST *exp)
{
	ALIAS a;
	a.name = name;
	a.exp = exp;
	obstack_grow(&alias_stk, &a, sizeof a);
	alias_count++;
}

void
begin_aliases()
{
	obstack_init(&alias_stk);
}

static int
alias_cmp(const void *a, const void *b)
{
	return strcmp(((ALIAS *) a)->name, ((ALIAS *) b)->name);
}

static int
alias_cmp2(const void *a, const void *b)
{
	char *aname = ((ALIAS *) a)->name;
	char *bname = ((ALIAS *) 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
end_aliases()
{
	int i;
	aliases = obstack_finish(&alias_stk);
	qsort(aliases, alias_count, sizeof aliases[0], alias_cmp);
	for (i = 1; i < alias_count; i++)
		if (alias_cmp(aliases + i - 1, aliases + i) == 0) {
			error(0, 0, "alias `%s' multiply defined", aliases[i].name);
			error_count++;
		}
}


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

	if (!name)
		return -1;
	a.name = name;
	p = bsearch(&a, aliases, alias_count, sizeof aliases[0], alias_cmp2);
	return p ? p - aliases : -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)
{
	int i;

	for (i = 0; i < alias_count; i++) {
		if (aliases[i].exp) {
			struct string_list *p;
			for (p = aliases[i].exp->head; p; p = p->next) {
				int n = find_alias(p->str);
				if (n >= 0)
					alias_setbit(r, size, i, n);
			}
		}
	}
}

void
check_circular_deps(unsigned *r, unsigned size)
{
	int i;

	for (i = 0; i < alias_count; i++) {
		if (alias_bitisset(r, size, i, i)) {
			error(0, 0, "%s: circular dependency", aliases[i].name);
			error_count++;
		}
	}
}

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

	/* Allocate matrix */
	size = (alias_count + BITS_PER_WORD - 1) / BITS_PER_WORD;
	r = xmalloc(alias_count * size * sizeof(*r));
	memset(r, 0, alias_count * size * sizeof(*r));

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

	/* Compute transitive closure of the matrix r */
	TC(r, alias_count);

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

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

Return to:

Send suggestions and report system problems to the System administrator.