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