tsort.c revision 1.11 1 1.11 mycroft /* $NetBSD: tsort.c,v 1.11 1996/01/17 20:37:53 mycroft Exp $ */
2 1.9 jtc
3 1.1 cgd /*
4 1.9 jtc * Copyright (c) 1989, 1993, 1994
5 1.7 cgd * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * This code is derived from software contributed to Berkeley by
8 1.1 cgd * Michael Rendell of Memorial University of Newfoundland.
9 1.1 cgd *
10 1.1 cgd * Redistribution and use in source and binary forms, with or without
11 1.1 cgd * modification, are permitted provided that the following conditions
12 1.1 cgd * are met:
13 1.1 cgd * 1. Redistributions of source code must retain the above copyright
14 1.1 cgd * notice, this list of conditions and the following disclaimer.
15 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 cgd * notice, this list of conditions and the following disclaimer in the
17 1.1 cgd * documentation and/or other materials provided with the distribution.
18 1.1 cgd * 3. All advertising materials mentioning features or use of this software
19 1.1 cgd * must display the following acknowledgement:
20 1.1 cgd * This product includes software developed by the University of
21 1.1 cgd * California, Berkeley and its contributors.
22 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
23 1.1 cgd * may be used to endorse or promote products derived from this software
24 1.1 cgd * without specific prior written permission.
25 1.1 cgd *
26 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 1.1 cgd * SUCH DAMAGE.
37 1.1 cgd */
38 1.1 cgd
39 1.1 cgd #ifndef lint
40 1.7 cgd static char copyright[] =
41 1.9 jtc "@(#) Copyright (c) 1989, 1993, 1994\n\
42 1.7 cgd The Regents of the University of California. All rights reserved.\n";
43 1.1 cgd #endif /* not lint */
44 1.1 cgd
45 1.1 cgd #ifndef lint
46 1.9 jtc #if 0
47 1.10 jtc static char sccsid[] = "@(#)tsort.c 8.3 (Berkeley) 5/4/95";
48 1.9 jtc #endif
49 1.11 mycroft static char rcsid[] = "$NetBSD: tsort.c,v 1.11 1996/01/17 20:37:53 mycroft Exp $";
50 1.1 cgd #endif /* not lint */
51 1.1 cgd
52 1.1 cgd #include <sys/types.h>
53 1.9 jtc
54 1.9 jtc #include <ctype.h>
55 1.9 jtc #include <db.h>
56 1.9 jtc #include <err.h>
57 1.1 cgd #include <errno.h>
58 1.7 cgd #include <fcntl.h>
59 1.1 cgd #include <stdio.h>
60 1.7 cgd #include <stdlib.h>
61 1.1 cgd #include <string.h>
62 1.10 jtc #include <unistd.h>
63 1.1 cgd
64 1.1 cgd /*
65 1.7 cgd * Topological sort. Input is a list of pairs of strings separated by
66 1.1 cgd * white space (spaces, tabs, and/or newlines); strings are written to
67 1.1 cgd * standard output in sorted order, one per line.
68 1.1 cgd *
69 1.1 cgd * usage:
70 1.7 cgd * tsort [-l] [inputfile]
71 1.1 cgd * If no input file is specified, standard input is read.
72 1.1 cgd *
73 1.1 cgd * Should be compatable with AT&T tsort HOWEVER the output is not identical
74 1.1 cgd * (i.e. for most graphs there is more than one sorted order, and this tsort
75 1.1 cgd * usually generates a different one then the AT&T tsort). Also, cycle
76 1.1 cgd * reporting seems to be more accurate in this version (the AT&T tsort
77 1.1 cgd * sometimes says a node is in a cycle when it isn't).
78 1.1 cgd *
79 1.1 cgd * Michael Rendell, michael (at) stretch.cs.mun.ca - Feb 26, '90
80 1.1 cgd */
81 1.1 cgd #define HASHSIZE 53 /* doesn't need to be big */
82 1.1 cgd #define NF_MARK 0x1 /* marker for cycle detection */
83 1.1 cgd #define NF_ACYCLIC 0x2 /* this node is cycle free */
84 1.7 cgd #define NF_NODEST 0x4 /* Unreachable */
85 1.7 cgd
86 1.1 cgd typedef struct node_str NODE;
87 1.1 cgd
88 1.1 cgd struct node_str {
89 1.1 cgd NODE **n_prevp; /* pointer to previous node's n_next */
90 1.1 cgd NODE *n_next; /* next node in graph */
91 1.7 cgd NODE **n_arcs; /* array of arcs to other nodes */
92 1.1 cgd int n_narcs; /* number of arcs in n_arcs[] */
93 1.1 cgd int n_arcsize; /* size of n_arcs[] array */
94 1.1 cgd int n_refcnt; /* # of arcs pointing to this node */
95 1.1 cgd int n_flags; /* NF_* */
96 1.7 cgd char n_name[1]; /* name of this node */
97 1.1 cgd };
98 1.1 cgd
99 1.1 cgd typedef struct _buf {
100 1.1 cgd char *b_buf;
101 1.1 cgd int b_bsize;
102 1.1 cgd } BUF;
103 1.1 cgd
104 1.7 cgd DB *db;
105 1.9 jtc NODE *graph, **cycle_buf, **longest_cycle;
106 1.11 mycroft int debug, longest, quiet;
107 1.7 cgd
108 1.7 cgd void add_arc __P((char *, char *));
109 1.7 cgd int find_cycle __P((NODE *, NODE *, int, int));
110 1.7 cgd NODE *get_node __P((char *));
111 1.7 cgd void *grow_buf __P((void *, int));
112 1.7 cgd void remove_node __P((NODE *));
113 1.7 cgd void tsort __P((void));
114 1.7 cgd void usage __P((void));
115 1.1 cgd
116 1.7 cgd int
117 1.1 cgd main(argc, argv)
118 1.1 cgd int argc;
119 1.7 cgd char *argv[];
120 1.1 cgd {
121 1.1 cgd register BUF *b;
122 1.1 cgd register int c, n;
123 1.1 cgd FILE *fp;
124 1.7 cgd int bsize, ch, nused;
125 1.1 cgd BUF bufs[2];
126 1.1 cgd
127 1.11 mycroft while ((ch = getopt(argc, argv, "dlq")) != EOF)
128 1.9 jtc switch (ch) {
129 1.7 cgd case 'd':
130 1.9 jtc debug = 1;
131 1.9 jtc break;
132 1.7 cgd case 'l':
133 1.9 jtc longest = 1;
134 1.9 jtc break;
135 1.11 mycroft case 'q':
136 1.11 mycroft quiet = 1;
137 1.11 mycroft break;
138 1.7 cgd case '?':
139 1.7 cgd default:
140 1.7 cgd usage();
141 1.7 cgd }
142 1.7 cgd argc -= optind;
143 1.7 cgd argv += optind;
144 1.7 cgd
145 1.9 jtc switch (argc) {
146 1.7 cgd case 0:
147 1.1 cgd fp = stdin;
148 1.7 cgd break;
149 1.7 cgd case 1:
150 1.7 cgd if ((fp = fopen(*argv, "r")) == NULL)
151 1.9 jtc err(1, "%s", *argv);
152 1.7 cgd break;
153 1.7 cgd default:
154 1.7 cgd usage();
155 1.1 cgd }
156 1.1 cgd
157 1.1 cgd for (b = bufs, n = 2; --n >= 0; b++)
158 1.7 cgd b->b_buf = grow_buf(NULL, b->b_bsize = 1024);
159 1.1 cgd
160 1.1 cgd /* parse input and build the graph */
161 1.1 cgd for (n = 0, c = getc(fp);;) {
162 1.1 cgd while (c != EOF && isspace(c))
163 1.1 cgd c = getc(fp);
164 1.1 cgd if (c == EOF)
165 1.1 cgd break;
166 1.1 cgd
167 1.1 cgd nused = 0;
168 1.1 cgd b = &bufs[n];
169 1.1 cgd bsize = b->b_bsize;
170 1.1 cgd do {
171 1.1 cgd b->b_buf[nused++] = c;
172 1.7 cgd if (nused == bsize)
173 1.7 cgd b->b_buf = grow_buf(b->b_buf, bsize *= 2);
174 1.1 cgd c = getc(fp);
175 1.1 cgd } while (c != EOF && !isspace(c));
176 1.1 cgd
177 1.1 cgd b->b_buf[nused] = '\0';
178 1.1 cgd b->b_bsize = bsize;
179 1.1 cgd if (n)
180 1.1 cgd add_arc(bufs[0].b_buf, bufs[1].b_buf);
181 1.1 cgd n = !n;
182 1.1 cgd }
183 1.1 cgd (void)fclose(fp);
184 1.7 cgd if (n)
185 1.8 cgd errx(1, "odd data count");
186 1.1 cgd
187 1.1 cgd /* do the sort */
188 1.1 cgd tsort();
189 1.1 cgd exit(0);
190 1.1 cgd }
191 1.1 cgd
192 1.1 cgd /* double the size of oldbuf and return a pointer to the new buffer. */
193 1.7 cgd void *
194 1.1 cgd grow_buf(bp, size)
195 1.7 cgd void *bp;
196 1.1 cgd int size;
197 1.1 cgd {
198 1.7 cgd if ((bp = realloc(bp, (u_int)size)) == NULL)
199 1.8 cgd err(1, NULL);
200 1.7 cgd return (bp);
201 1.1 cgd }
202 1.1 cgd
203 1.1 cgd /*
204 1.1 cgd * add an arc from node s1 to node s2 in the graph. If s1 or s2 are not in
205 1.1 cgd * the graph, then add them.
206 1.1 cgd */
207 1.1 cgd void
208 1.1 cgd add_arc(s1, s2)
209 1.1 cgd char *s1, *s2;
210 1.1 cgd {
211 1.1 cgd register NODE *n1;
212 1.1 cgd NODE *n2;
213 1.4 cgd int bsize, i;
214 1.1 cgd
215 1.7 cgd n1 = get_node(s1);
216 1.1 cgd
217 1.1 cgd if (!strcmp(s1, s2))
218 1.1 cgd return;
219 1.1 cgd
220 1.7 cgd n2 = get_node(s2);
221 1.1 cgd
222 1.1 cgd /*
223 1.3 cgd * Check if this arc is already here.
224 1.3 cgd */
225 1.3 cgd for (i = 0; i < n1->n_narcs; i++)
226 1.3 cgd if (n1->n_arcs[i] == n2)
227 1.3 cgd return;
228 1.3 cgd /*
229 1.3 cgd * Add it.
230 1.1 cgd */
231 1.1 cgd if (n1->n_narcs == n1->n_arcsize) {
232 1.1 cgd if (!n1->n_arcsize)
233 1.1 cgd n1->n_arcsize = 10;
234 1.1 cgd bsize = n1->n_arcsize * sizeof(*n1->n_arcs) * 2;
235 1.7 cgd n1->n_arcs = grow_buf(n1->n_arcs, bsize);
236 1.1 cgd n1->n_arcsize = bsize / sizeof(*n1->n_arcs);
237 1.1 cgd }
238 1.1 cgd n1->n_arcs[n1->n_narcs++] = n2;
239 1.1 cgd ++n2->n_refcnt;
240 1.1 cgd }
241 1.1 cgd
242 1.7 cgd /* Find a node in the graph (insert if not found) and return a pointer to it. */
243 1.1 cgd NODE *
244 1.7 cgd get_node(name)
245 1.1 cgd char *name;
246 1.1 cgd {
247 1.7 cgd DBT data, key;
248 1.7 cgd NODE *n;
249 1.1 cgd
250 1.7 cgd if (db == NULL &&
251 1.7 cgd (db = dbopen(NULL, O_RDWR, 0, DB_HASH, NULL)) == NULL)
252 1.9 jtc err(1, "db: %s", name);
253 1.7 cgd
254 1.7 cgd key.data = name;
255 1.7 cgd key.size = strlen(name) + 1;
256 1.7 cgd
257 1.9 jtc switch ((*db->get)(db, &key, &data, 0)) {
258 1.7 cgd case 0:
259 1.7 cgd bcopy(data.data, &n, sizeof(n));
260 1.7 cgd return (n);
261 1.7 cgd case 1:
262 1.7 cgd break;
263 1.7 cgd default:
264 1.7 cgd case -1:
265 1.9 jtc err(1, "db: %s", name);
266 1.7 cgd }
267 1.1 cgd
268 1.7 cgd if ((n = malloc(sizeof(NODE) + key.size)) == NULL)
269 1.8 cgd err(1, NULL);
270 1.1 cgd
271 1.1 cgd n->n_narcs = 0;
272 1.1 cgd n->n_arcsize = 0;
273 1.7 cgd n->n_arcs = NULL;
274 1.1 cgd n->n_refcnt = 0;
275 1.1 cgd n->n_flags = 0;
276 1.7 cgd bcopy(name, n->n_name, key.size);
277 1.1 cgd
278 1.7 cgd /* Add to linked list. */
279 1.9 jtc if ((n->n_next = graph) != NULL)
280 1.1 cgd graph->n_prevp = &n->n_next;
281 1.1 cgd n->n_prevp = &graph;
282 1.1 cgd graph = n;
283 1.1 cgd
284 1.7 cgd /* Add to hash table. */
285 1.7 cgd data.data = &n;
286 1.7 cgd data.size = sizeof(n);
287 1.7 cgd if ((*db->put)(db, &key, &data, 0))
288 1.9 jtc err(1, "db: %s", name);
289 1.7 cgd return (n);
290 1.7 cgd }
291 1.7 cgd
292 1.7 cgd
293 1.7 cgd /*
294 1.7 cgd * Clear the NODEST flag from all nodes.
295 1.7 cgd */
296 1.7 cgd void
297 1.7 cgd clear_cycle()
298 1.7 cgd {
299 1.7 cgd NODE *n;
300 1.7 cgd
301 1.9 jtc for (n = graph; n != NULL; n = n->n_next)
302 1.7 cgd n->n_flags &= ~NF_NODEST;
303 1.1 cgd }
304 1.1 cgd
305 1.1 cgd /* do topological sort on graph */
306 1.1 cgd void
307 1.1 cgd tsort()
308 1.1 cgd {
309 1.7 cgd register NODE *n, *next;
310 1.9 jtc register int cnt, i;
311 1.1 cgd
312 1.9 jtc while (graph != NULL) {
313 1.1 cgd /*
314 1.7 cgd * Keep getting rid of simple cases until there are none left,
315 1.1 cgd * if there are any nodes still in the graph, then there is
316 1.1 cgd * a cycle in it.
317 1.1 cgd */
318 1.1 cgd do {
319 1.9 jtc for (cnt = 0, n = graph; n != NULL; n = next) {
320 1.1 cgd next = n->n_next;
321 1.1 cgd if (n->n_refcnt == 0) {
322 1.1 cgd remove_node(n);
323 1.1 cgd ++cnt;
324 1.1 cgd }
325 1.1 cgd }
326 1.9 jtc } while (graph != NULL && cnt);
327 1.1 cgd
328 1.9 jtc if (graph == NULL)
329 1.1 cgd break;
330 1.1 cgd
331 1.1 cgd if (!cycle_buf) {
332 1.1 cgd /*
333 1.7 cgd * Allocate space for two cycle logs - one to be used
334 1.1 cgd * as scratch space, the other to save the longest
335 1.1 cgd * cycle.
336 1.1 cgd */
337 1.9 jtc for (cnt = 0, n = graph; n != NULL; n = n->n_next)
338 1.1 cgd ++cnt;
339 1.7 cgd cycle_buf = malloc((u_int)sizeof(NODE *) * cnt);
340 1.7 cgd longest_cycle = malloc((u_int)sizeof(NODE *) * cnt);
341 1.7 cgd if (cycle_buf == NULL || longest_cycle == NULL)
342 1.8 cgd err(1, NULL);
343 1.1 cgd }
344 1.9 jtc for (n = graph; n != NULL; n = n->n_next)
345 1.9 jtc if (!(n->n_flags & NF_ACYCLIC))
346 1.1 cgd if (cnt = find_cycle(n, n, 0, 0)) {
347 1.11 mycroft if (!quiet) {
348 1.11 mycroft warnx("cycle in data");
349 1.11 mycroft for (i = 0; i < cnt; i++)
350 1.11 mycroft warnx("%s",
351 1.11 mycroft longest_cycle[i]->n_name);
352 1.11 mycroft }
353 1.1 cgd remove_node(n);
354 1.7 cgd clear_cycle();
355 1.1 cgd break;
356 1.7 cgd } else {
357 1.1 cgd /* to avoid further checks */
358 1.7 cgd n->n_flags |= NF_ACYCLIC;
359 1.7 cgd clear_cycle();
360 1.7 cgd }
361 1.1 cgd
362 1.9 jtc if (n == NULL)
363 1.9 jtc errx(1, "internal error -- could not find cycle");
364 1.1 cgd }
365 1.1 cgd }
366 1.1 cgd
367 1.1 cgd /* print node and remove from graph (does not actually free node) */
368 1.1 cgd void
369 1.1 cgd remove_node(n)
370 1.1 cgd register NODE *n;
371 1.1 cgd {
372 1.1 cgd register NODE **np;
373 1.1 cgd register int i;
374 1.1 cgd
375 1.1 cgd (void)printf("%s\n", n->n_name);
376 1.1 cgd for (np = n->n_arcs, i = n->n_narcs; --i >= 0; np++)
377 1.1 cgd --(*np)->n_refcnt;
378 1.1 cgd n->n_narcs = 0;
379 1.1 cgd *n->n_prevp = n->n_next;
380 1.1 cgd if (n->n_next)
381 1.1 cgd n->n_next->n_prevp = n->n_prevp;
382 1.1 cgd }
383 1.1 cgd
384 1.7 cgd
385 1.7 cgd /* look for the longest? cycle from node from to node to. */
386 1.7 cgd int
387 1.1 cgd find_cycle(from, to, longest_len, depth)
388 1.1 cgd NODE *from, *to;
389 1.1 cgd int depth, longest_len;
390 1.1 cgd {
391 1.1 cgd register NODE **np;
392 1.1 cgd register int i, len;
393 1.1 cgd
394 1.1 cgd /*
395 1.1 cgd * avoid infinite loops and ignore portions of the graph known
396 1.1 cgd * to be acyclic
397 1.1 cgd */
398 1.7 cgd if (from->n_flags & (NF_NODEST|NF_MARK|NF_ACYCLIC))
399 1.7 cgd return (0);
400 1.7 cgd from->n_flags |= NF_MARK;
401 1.1 cgd
402 1.1 cgd for (np = from->n_arcs, i = from->n_narcs; --i >= 0; np++) {
403 1.1 cgd cycle_buf[depth] = *np;
404 1.1 cgd if (*np == to) {
405 1.1 cgd if (depth + 1 > longest_len) {
406 1.1 cgd longest_len = depth + 1;
407 1.1 cgd (void)memcpy((char *)longest_cycle,
408 1.1 cgd (char *)cycle_buf,
409 1.1 cgd longest_len * sizeof(NODE *));
410 1.1 cgd }
411 1.1 cgd } else {
412 1.7 cgd if ((*np)->n_flags & (NF_MARK|NF_ACYCLIC|NF_NODEST))
413 1.7 cgd continue;
414 1.1 cgd len = find_cycle(*np, to, longest_len, depth + 1);
415 1.7 cgd
416 1.7 cgd if (debug)
417 1.9 jtc (void)printf("%*s %s->%s %d\n", depth, "",
418 1.9 jtc from->n_name, to->n_name, len);
419 1.7 cgd
420 1.7 cgd if (len == 0)
421 1.9 jtc (*np)->n_flags |= NF_NODEST;
422 1.7 cgd
423 1.7 cgd if (len > longest_len)
424 1.1 cgd longest_len = len;
425 1.7 cgd
426 1.7 cgd if (len > 0 && !longest)
427 1.5 cgd break;
428 1.1 cgd }
429 1.1 cgd }
430 1.7 cgd from->n_flags &= ~NF_MARK;
431 1.7 cgd return (longest_len);
432 1.1 cgd }
433 1.1 cgd
434 1.1 cgd void
435 1.7 cgd usage()
436 1.7 cgd {
437 1.11 mycroft (void)fprintf(stderr, "usage: tsort [-lq] [file]\n");
438 1.7 cgd exit(1);
439 1.1 cgd }
440