#include <sys/types.h>
#include <ctype.h>
#include <db.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define NF_MARK 0x1
#define NF_ACYCLIC 0x2
#define NF_NODEST 0x4
typedef struct node_str NODE;
struct node_str {
NODE **n_prevp;
NODE *n_next;
NODE **n_arcs;
int n_narcs;
int n_arcsize;
int n_refcnt;
int n_flags;
char n_name[1];
};
typedef struct _buf {
char *b_buf;
int b_bsize;
} BUF;
static DB *db;
static NODE *graph, **cycle_buf, **longest_cycle;
static int debug, longest, quiet;
static void add_arc(char *, char *);
static int find_cycle(NODE *, NODE *, int, int);
static NODE *get_node(char *);
static void *grow_buf(void *, size_t);
static void remove_node(NODE *);
static void clear_cycle(void);
static void tsort(void);
static void usage(void);
int
main(int argc, char *argv[])
{
BUF *b;
int c, n;
FILE *fp;
int bsize, ch, nused;
BUF bufs[2];
fp = NULL;
while ((ch = getopt(argc, argv, "dlq")) != -1)
switch (ch) {
case 'd':
debug = 1;
break;
case 'l':
longest = 1;
break;
case 'q':
quiet = 1;
break;
case '?':
default:
usage();
}
argc -= optind;
argv += optind;
switch (argc) {
case 0:
fp = stdin;
break;
case 1:
if ((fp = fopen(*argv, "r")) == NULL)
err(1, "%s", *argv);
break;
default:
usage();
}
for (b = bufs, n = 2; --n >= 0; b++)
b->b_buf = grow_buf(NULL, b->b_bsize = 1024);
for (n = 0, c = getc(fp);;) {
while (c != EOF && isspace(c))
c = getc(fp);
if (c == EOF)
break;
nused = 0;
b = &bufs[n];
bsize = b->b_bsize;
do {
b->b_buf[nused++] = c;
if (nused == bsize)
b->b_buf = grow_buf(b->b_buf, bsize *= 2);
c = getc(fp);
} while (c != EOF && !isspace(c));
b->b_buf[nused] = '\0';
b->b_bsize = bsize;
if (n)
add_arc(bufs[0].b_buf, bufs[1].b_buf);
n = !n;
}
(void)fclose(fp);
if (n)
errx(1, "odd data count");
tsort();
if (ferror(stdout) != 0 || fflush(stdout) != 0)
err(1, "stdout");
exit(0);
}
static void *
grow_buf(void *bp, size_t size)
{
if ((bp = realloc(bp, size)) == NULL)
err(1, NULL);
return (bp);
}
static void
add_arc(char *s1, char *s2)
{
NODE *n1;
NODE *n2;
int bsize, i;
n1 = get_node(s1);
if (!strcmp(s1, s2))
return;
n2 = get_node(s2);
for (i = 0; i < n1->n_narcs; i++)
if (n1->n_arcs[i] == n2)
return;
if (n1->n_narcs == n1->n_arcsize) {
if (!n1->n_arcsize)
n1->n_arcsize = 10;
bsize = n1->n_arcsize * sizeof(*n1->n_arcs) * 2;
n1->n_arcs = grow_buf(n1->n_arcs, bsize);
n1->n_arcsize = bsize / sizeof(*n1->n_arcs);
}
n1->n_arcs[n1->n_narcs++] = n2;
++n2->n_refcnt;
}
static NODE *
get_node(char *name)
{
DBT data, key;
NODE *n;
if (db == NULL &&
(db = dbopen(NULL, O_RDWR, 0, DB_HASH, NULL)) == NULL)
err(1, "db: %s", name);
key.data = name;
key.size = strlen(name) + 1;
switch ((*db->get)(db, &key, &data, 0)) {
case 0:
memcpy(&n, data.data, sizeof(n));
return (n);
case 1:
break;
default:
case -1:
err(1, "db: %s", name);
}
if ((n = malloc(sizeof(NODE) + key.size)) == NULL)
err(1, NULL);
n->n_narcs = 0;
n->n_arcsize = 0;
n->n_arcs = NULL;
n->n_refcnt = 0;
n->n_flags = 0;
memcpy(n->n_name, name, key.size);
if ((n->n_next = graph) != NULL)
graph->n_prevp = &n->n_next;
n->n_prevp = &graph;
graph = n;
data.data = &n;
data.size = sizeof(n);
if ((*db->put)(db, &key, &data, 0))
err(1, "db: %s", name);
return (n);
}
static void
clear_cycle(void)
{
NODE *n;
for (n = graph; n != NULL; n = n->n_next)
n->n_flags &= ~NF_NODEST;
}
static void
tsort(void)
{
NODE *n, *next;
int cnt, i;
while (graph != NULL) {
do {
for (cnt = 0, n = graph; n != NULL; n = next) {
next = n->n_next;
if (n->n_refcnt == 0) {
remove_node(n);
++cnt;
}
}
} while (graph != NULL && cnt);
if (graph == NULL)
break;
if (!cycle_buf) {
for (cnt = 0, n = graph; n != NULL; n = n->n_next)
++cnt;
cycle_buf = malloc(sizeof(NODE *) * cnt);
longest_cycle = malloc(sizeof(NODE *) * cnt);
if (cycle_buf == NULL || longest_cycle == NULL)
err(1, NULL);
}
for (n = graph; n != NULL; n = n->n_next)
if (!(n->n_flags & NF_ACYCLIC)) {
if ((cnt = find_cycle(n, n, 0, 0))) {
if (!quiet) {
warnx("cycle in data");
for (i = 0; i < cnt; i++)
warnx("%s",
longest_cycle[i]->n_name);
}
remove_node(n);
clear_cycle();
break;
} else {
n->n_flags |= NF_ACYCLIC;
clear_cycle();
}
}
if (n == NULL)
errx(1, "internal error -- could not find cycle");
}
}
static void
remove_node(NODE *n)
{
NODE **np;
int i;
(void)printf("%s\n", n->n_name);
for (np = n->n_arcs, i = n->n_narcs; --i >= 0; np++)
--(*np)->n_refcnt;
n->n_narcs = 0;
*n->n_prevp = n->n_next;
if (n->n_next)
n->n_next->n_prevp = n->n_prevp;
}
static int
find_cycle(NODE *from, NODE *to, int longest_len, int depth)
{
NODE **np;
int i, len;
if (from->n_flags & (NF_NODEST|NF_MARK|NF_ACYCLIC))
return (0);
from->n_flags |= NF_MARK;
for (np = from->n_arcs, i = from->n_narcs; --i >= 0; np++) {
cycle_buf[depth] = *np;
if (*np == to) {
if (depth + 1 > longest_len) {
longest_len = depth + 1;
(void)memcpy((char *)longest_cycle,
(char *)cycle_buf,
longest_len * sizeof(NODE *));
}
} else {
if ((*np)->n_flags & (NF_MARK|NF_ACYCLIC|NF_NODEST))
continue;
len = find_cycle(*np, to, longest_len, depth + 1);
if (debug)
(void)printf("%*s %s->%s %d\n", depth, "",
from->n_name, to->n_name, len);
if (len == 0)
(*np)->n_flags |= NF_NODEST;
if (len > longest_len)
longest_len = len;
if (len > 0 && !longest)
break;
}
}
from->n_flags &= ~NF_MARK;
return (longest_len);
}
static void
usage(void)
{
(void)fprintf(stderr, "usage: tsort [-dlq] [file]\n");
exit(1);
}