summaryrefslogtreecommitdiffstats
path: root/bin/emitpkgtree
blob: ca12c597739c865ac2b9ecac413379c6501f5989 (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
#!/usr/bin/env perl

use warnings;
use strict;

my $PROG = 'flatlntree';

sub parsetree
{
	my($name) = @_;
	my($txt, $ln, @tree) = (q{}, $., $name);
	while(<STDIN>){
		if(/^BEGNODE (\S+)$/){
			push @tree, $txt if($txt);
			push @tree, parsetree($1);
			$txt = q{};
		}elsif(/^ENDNODE (\S+)$/){
			if($1 ne $name){
				print STDERR "$PROG: wrong ENDNODE:"
				  . qq{ '$1' at line $.}
				  . qq{ (in '$name' started at line $ln)\n};
				exit 101;
			}else{
				last;
			}
		}else{
			$txt .= $_;
		}
	}

	push @tree, $txt if($txt);
	return \@tree;
}

sub flatten
{
	my($tree) = @_;
	if(ref $tree){
		# skip name
		return join q{}, map { flatten($tree->[$_]) } 1 .. $#$tree;
	}else{
		return $tree;
	}
}

sub main
{
	my $top = parsetree('TOP');
	@$top = grep { ref } @$top; # only keep sub-nodes
	for my $n (@$top){
		my $name = $n->[0];
		print ">>> $name\n";
		print flatten($n);
	}
	return 0;
}

exit main();