12153Seric # include	"../hdr/defines.h"
22153Seric # include	"../hdr/had.h"
32153Seric 
4*32773Sbostic SCCSID(@(#)stree.c	4.3);
52153Seric USXALLOC();
62153Seric 
72153Seric struct	tree {
82153Seric 	int	t_dsucc;		/* first successor (trunk) */
92153Seric 	struct	list	*t_osucc;	/* other successors */
102153Seric 	int	t_trunk;		/* != 0 is a trunk delta */
112153Seric };
122153Seric 
132153Seric struct	list {
142153Seric 	struct	list	*l_next;
152153Seric 	int	l_val;
162153Seric };
172153Seric 
182153Seric struct	position {
192153Seric 	int	p_depth;
202153Seric 	int	p_width;
212153Seric 	int	p_node;
222153Seric };
232153Seric 
242153Seric 
252153Seric struct	tree	*tree;
262153Seric struct	position	*pos;
272153Seric int	dval;
282153Seric 
292153Seric struct packet gpkt;
302153Seric struct sid sid;
312153Seric int	num_files;
322153Seric char	had[26];
332153Seric 
342153Seric main(argc,argv)
352153Seric int argc;
362153Seric register char *argv[];
372153Seric {
382153Seric 	register int i;
392153Seric 	register char *p;
402153Seric 	char c;
412153Seric 	int testmore;
422153Seric 	extern prttree();
432153Seric 	extern int Fcnt;
442153Seric 
452153Seric 	Fflags = FTLEXIT | FTLMSG | FTLCLN;
462153Seric 	for(i = 1; i < argc; i++)
472153Seric 		if(argv[i][0] == '-' && (c=argv[i][1])) {
482153Seric 			p = &argv[i][2];
492153Seric 			testmore = 0;
502153Seric 			switch (c) {
512153Seric 
522153Seric 			case 'p':
532153Seric 				testmore++;
542153Seric 				break;
552153Seric 
562153Seric 			default:
572153Seric 				fatal("unknown key letter (cm1)");
582153Seric 			}
592153Seric 
602153Seric 			if (testmore) {
612153Seric 				testmore = 0;
622153Seric 				if (*p)
632153Seric 					fatal(sprintf(Error,
642153Seric 					  "value after %c arg (cm7)",c));
652153Seric 			}
662153Seric 			if (had[c - 'a']++)
672153Seric 				fatal("key letter twice (cm2)");
682153Seric 			argv[i] = 0;
692153Seric 		}
702153Seric 		else num_files++;
712153Seric 
722153Seric 	if(num_files == 0)
732153Seric 		fatal("missing file arg (cm3)");
742153Seric 	setsig();
75*32773Sbostic 	Fflags &= ~FTLEXIT;
76*32773Sbostic 	Fflags |= FTLJMP;
772153Seric 	for (i = 1; i < argc; i++)
782153Seric 		if (p=argv[i])
792153Seric 			do_file(p,prttree);
802153Seric 	exit(Fcnt ? 1 : 0);
812153Seric }
822153Seric 
832153Seric 
842153Seric prttree(file)
852153Seric {
862153Seric 	register struct idel *rdp;
872153Seric 	register int n, i;
882153Seric 	char str[32];
892153Seric 	extern char had_dir, had_standinp;
902153Seric 	struct stats stats;
912153Seric 	extern poscomp();
922153Seric 
932153Seric 	if (setjmp(Fjmp))
942153Seric 		return;
952153Seric 	sinit(&gpkt, file, 1);
962153Seric 	gpkt.p_verbose = -1;
972153Seric 	gpkt.p_stdout = stderr;
982153Seric 	if (gpkt.p_verbose && (num_files > 1 || had_dir || had_standinp))
992153Seric 		fprintf(gpkt.p_stdout,"\n%s:\n",gpkt.p_file);
1002153Seric 
1012153Seric 	if (dodelt(&gpkt,&stats,0,0) == 0)
1022153Seric 		fmterr(&gpkt);
1032153Seric 	fclose(gpkt.p_iop);
1042153Seric 	gpkt.p_iop = 0;
1052153Seric 
1062153Seric 	tree = alloc(n = ((maxser(&gpkt) + 1) * sizeof(struct tree)));
10719944Ssam 	bzero(tree, n);
1082153Seric 	pos = alloc(n = ((maxser(&gpkt) + 1) * sizeof(struct position)));
10919944Ssam 	bzero(pos, n);
1102153Seric 	for (i = 1; i <= maxser(&gpkt); i++)
1112153Seric 		pos[i].p_node = i;
1122153Seric 	rdp = gpkt.p_idel;
1132153Seric 	for (i = 1; i <= maxser(&gpkt); i++) {
1142153Seric 		if (rdp[i].i_sid.s_br == 0)
1152153Seric 			tree[i].t_trunk = 1;
1162153Seric 		else
1172153Seric 			pos[i].p_width = pos[rdp[i].i_pred].p_width + 1;
1182153Seric 		for (n = i + 1; n <= maxser(&gpkt); n++)
1192153Seric 			if (rdp[n].i_pred == i)
1202153Seric 				addsucc(i, n, rdp[n].i_sid.s_br);
1212153Seric 	}
1222153Seric 	dval = 0;
1232153Seric 	traverse(1);
1242153Seric 	if (!HADP) {
1252153Seric 		qsort(&pos[1], maxser(&gpkt), sizeof(pos[1]), poscomp);
1262153Seric 		for (n = 1; n <= maxser(&gpkt); n++) {
1272153Seric 			sid_ba(&rdp[pos[n].p_node].i_sid, str);
1282153Seric 			printf("Node %d\tSid %s\tDepth %d\tWidth %d\n",
1292153Seric 				pos[n].p_node, str, pos[n].p_depth, pos[n].p_width);
1302153Seric 		}
1312153Seric 	}
1322153Seric 	else
1332153Seric 		plot(rdp, maxser(&gpkt));
1342153Seric 	xfreeall();
1352153Seric }
1362153Seric 
1372153Seric 
1382153Seric addsucc(par, child, childbr)
1392153Seric {
1402153Seric 	struct tree *tp;
1412153Seric 
1422153Seric 	tp = &tree[par];
1432153Seric 	if (tp->t_trunk && tp->t_dsucc == 0 && childbr == 0)
1442153Seric 		tp->t_dsucc = child;
1452153Seric 	else
1462153Seric 		addlist(&tp->t_osucc, child);
1472153Seric }
1482153Seric 
1492153Seric 
1502153Seric addlist(headp, val)
1512153Seric struct list *headp;
1522153Seric {
1532153Seric 	struct list *prev, *p;
1542153Seric 
1552153Seric 	for (p = headp; p = (prev = p)->l_next; )
1562153Seric 		;
1572153Seric 	prev->l_next = p = alloc(sizeof(struct list));
1582153Seric 	p->l_next = 0;
1592153Seric 	p->l_val = val;
1602153Seric }
1612153Seric 
1622153Seric 
1632153Seric traverse(node)
1642153Seric {
1652153Seric 	register struct list *lp;
1662153Seric 
1672153Seric 	pos[node].p_depth = dval;
1682153Seric 	if (lp = tree[node].t_osucc) {
1692153Seric 		traverse(lp->l_val);
1702153Seric 		while (lp = lp->l_next) {
1712153Seric 			++dval;
1722153Seric 			traverse(lp->l_val);
1732153Seric 		}
1742153Seric 	}
1752153Seric 	if (tree[node].t_dsucc) {
1762153Seric 		++dval;
1772153Seric 		traverse(tree[node].t_dsucc);
1782153Seric 	}
1792153Seric }
1802153Seric 
1812153Seric 
1822153Seric poscomp(p1, p2)
1832153Seric register struct position *p1, *p2;
1842153Seric {
1852153Seric 	register int diff;
1862153Seric 
1872153Seric 	if (diff = p1->p_depth - p2->p_depth)
1882153Seric 		return(diff);
1892153Seric 	else
1902153Seric 		return(p1->p_width - p2->p_width);
1912153Seric }
1922153Seric 
1932153Seric 
1942153Seric dmptree()
1952153Seric {
1962153Seric 	register int n;
1972153Seric 	register struct tree *tp;
1982153Seric 	register struct list *lp;
1992153Seric 
2002153Seric 	for (n = maxser(&gpkt); n; n--) {
2012153Seric 		printf("Node %d", n);
2022153Seric 		tp = &tree[n];
2032153Seric 		if (tp->t_dsucc)
2042153Seric 			printf("\t%d", tp->t_dsucc);
2052153Seric 		for (lp = tp->t_osucc; lp; lp = lp->l_next)
2062153Seric 			printf("\t%d", lp->l_val);
2072153Seric 		printf("\n");
2082153Seric 	}
2092153Seric }
2102153Seric 
2112153Seric 
2122153Seric plot(rdp, n)
2132153Seric register struct idel *rdp;
2142153Seric {
2152153Seric 	char str[32];
2162153Seric 	int i, j, x, y, node;
2172153Seric 	struct tree *tp;
2182153Seric 	struct list *lp;
2192153Seric 
2202153Seric 	for (i = 1; i <= n; i++) {
2212153Seric 		node = pos[i].p_node;
2222153Seric 		x = pos[i].p_width;
2232153Seric 		y = pos[i].p_depth;
2242153Seric 		sid_ba(&rdp[node].i_sid, str);
2252153Seric 		pllabel(str, x, y);
2262153Seric 		tp = &tree[node];
2272153Seric 		if (j = tp->t_dsucc)
2282153Seric 			plline(x, y, pos[j].p_width, pos[j].p_depth);
2292153Seric 		for (lp = tp->t_osucc; lp; lp = lp->l_next) {
2302153Seric 			j = lp->l_val;
2312153Seric 			plline(x, y, pos[j].p_width, pos[j].p_depth);
2322153Seric 		}
2332153Seric 	}
2342153Seric 	pllabel("", 0, 15);
2352153Seric }
2362153Seric 
2372153Seric 
2382153Seric pllabel(s, x, y)
2392153Seric {
2402153Seric 	x = scx(x) + 64;
2412153Seric 	y = scy(y) + 64;
2422153Seric 	putchar('m');
2432153Seric 	putwd(x);
2442153Seric 	putwd(y);
2452153Seric 	printf("t%s\n", s);
2462153Seric }
2472153Seric 
2482153Seric 
2492153Seric plline(x0, y0, x1, y1)
2502153Seric {
2512153Seric 	x0 = scx(x0);
2522153Seric 	x1 = scx(x1);
2532153Seric 	y0 = scy(y0);
2542153Seric 	y1 = scy(y1);
2552153Seric 	putchar('l');
2562153Seric 	putwd(x0);
2572153Seric 	putwd(y0);
2582153Seric 	putwd(x1);
2592153Seric 	putwd(y1);
2602153Seric }
2612153Seric 
2622153Seric 
2632153Seric putwd(w)
2642153Seric {
2652153Seric 	register char *p;
2662153Seric 
2672153Seric 	p = &w;
2682153Seric 	putchar(*p++);
2692153Seric 	putchar(*p);
2702153Seric }
2712153Seric 
2722153Seric 
2732153Seric scx(xi)
2742153Seric {
2752153Seric 	return(xi * 1024 - 2047);
2762153Seric }
2772153Seric 
2782153Seric 
2792153Seric scy(yi)
2802153Seric {
2812153Seric 	return(2047 - (yi * 256));
2822153Seric }
2832153Seric 
2842153Seric 
2852153Seric clean_up(n)
2862153Seric {
2872153Seric 	xfreeall();
2882153Seric }
2892153Seric 
2902153Seric 
2912153Seric escdodelt()	/* dummy for dodelt() */
2922153Seric {
2932153Seric }
294