aboutsummaryrefslogtreecommitdiffstats
path: root/examples/crfc2asn1.pl
blob: 20e1f9f792533f227bd46fb8dc36d50ec6c381e8 (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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/perl

#
# $Id$
# $Author$
#
# Simple tool that fetches the ASN.1 specifications from the
# given set of RFC files.
#


my $inasn = 0;	# Are we inside ASN.1 grammar?
my $found = 0;

if(-t STDIN && $#ARGV == -1) {
	print STDERR "Rip ASN.1 specification from RFC file\n";
	print STDERR "Usage: $0 <rfc-file.txt> ...\n";
	print STDERR "Usage: <someprog> | $0\n";
	exit(1);
}

while(<>) {
	#
	# Strip RFC page delimiters.
	#
	next if /^[A-Z].*\[Page [0-9]+\]$/;
	next if /^$/;
	next if /^RFC [0-9].*[0-9]+$/;

	if($inasn == 0) {
		#
		# The least correct way to find the start of ASN
		# definition.
		#
		if(/^[ \t]*END[ \t]*$/) {
			print STDERR
			"Missed an ASN.1 grammar before line ". $. ."?\n";
			exit(1);
		}

		my $rfcid = '';
		$rfcid = $1 . '-' if($ARGV =~ /([a-z0-9]+)/i);

		if(/^[ \t]+([A-Za-z0-9-]+).*DEFINITIONS.*::=/) {
			my $fname = $rfcid . $1 . ".asn1";
			open(O, "> $fname") or die "Can't open $fname";
			select(O);
			$inasn = 1;
		} elsif(/^[ \t]*([A-Za-z0-9-]+).*{.*iso/) {
			my $fname = $rfcid . $1 . ".asn1";
			my @a = ($_);
			my $i;
			for($i = 0; $i < 8; $i++) {
				$_ = <>;
				push(@a, $_);
				if(/DEFINITIONS/) {
					$_ = join('', @a);
					$inasn = 1;
					last;
				}
			}
			next unless $inasn;
			open(O, "> $fname") or die "Can't open $fname";
			select(O);
		} else {
			next;
		}

		$found++;
		print "\n";
		print "-- \n";
		print "-- Grammar found in $ARGV by $0 at " . $. . "\n";
		print "-- \n";
		print "\n";
	}

	print;

	if(/^[ \t]*END[ \t]*$/) {
		select(STDOUT);
		close(O);
		$inasn = 0;
	}
}

die "No ASN.1 specifications found\n" unless $found;