blob: df0a099f0e16c850d022cf7846c0c71846422e1c [file] [log] [blame]
Lev Walkinf15320b2004-06-03 03:38:44 +00001#!/usr/bin/perl
2
3#
4# $Id$
5# $Author$
6#
7# Simple tool that fetches the ASN.1 specifications from the
8# given set of RFC files.
9#
10
11
12my $inasn = 0; # Are we inside ASN.1 grammar?
13my $found = 0;
Lev Walkina2bf7242004-09-07 06:36:35 +000014my $currentFname = '';
Lev Walkinf15320b2004-06-03 03:38:44 +000015
16if(-t STDIN && $#ARGV == -1) {
17 print STDERR "Rip ASN.1 specification from RFC file\n";
18 print STDERR "Usage: $0 <rfc-file.txt> ...\n";
19 print STDERR "Usage: <someprog> | $0\n";
20 exit(1);
21}
22
23while(<>) {
24 #
25 # Strip RFC page delimiters.
26 #
27 next if /^[A-Z].*\[Page [0-9]+\]$/;
28 next if /^ $/;
29 next if /^RFC [0-9].*[0-9]+$/;
30
31 if($inasn == 0) {
32 #
33 # The least correct way to find the start of ASN
34 # definition.
35 #
36 if(/^[ \t]*END[ \t]*$/) {
37 print STDERR
38 "Missed an ASN.1 grammar before line ". $. ."?\n";
Lev Walkina2bf7242004-09-07 06:36:35 +000039 unlink($currentFile) or die "Can't remove $!";
40 print STDERR "Removed $currentFile";
Lev Walkinf15320b2004-06-03 03:38:44 +000041 exit(1);
42 }
43
44 my $rfcid = '';
45 $rfcid = $1 . '-' if($ARGV =~ /([a-z0-9]+)/i);
46
47 if(/^[ \t]+([A-Za-z0-9-]+).*DEFINITIONS.*::=/) {
Lev Walkina2bf7242004-09-07 06:36:35 +000048 $currentFname = $rfcid . $1 . ".asn1";
Lev Walkinf15320b2004-06-03 03:38:44 +000049 $inasn = 1;
50 } elsif(/^[ \t]*([A-Za-z0-9-]+).*{.*iso/) {
Lev Walkina2bf7242004-09-07 06:36:35 +000051 $currentFname = $rfcid . $1 . ".asn1";
Lev Walkinf15320b2004-06-03 03:38:44 +000052 my @a = ($_);
53 my $i;
54 for($i = 0; $i < 8; $i++) {
55 $_ = <>;
56 push(@a, $_);
57 if(/DEFINITIONS/) {
58 $_ = join('', @a);
59 $inasn = 1;
60 last;
61 }
62 }
63 next unless $inasn;
Lev Walkinf15320b2004-06-03 03:38:44 +000064 } else {
65 next;
66 }
67
Lev Walkina2bf7242004-09-07 06:36:35 +000068 print STDERR "Found $1 at line $. => $currentFname\n";
69 open(O, "> $currentFname") or die "Can't open $currentFname";
70 select(O);
71
Lev Walkinf15320b2004-06-03 03:38:44 +000072 $found++;
73 print "\n";
74 print "-- \n";
Lev Walkina2bf7242004-09-07 06:36:35 +000075 print "-- ASN.1 module found by $0 in $ARGV at line " . $. . "\n";
Lev Walkinf15320b2004-06-03 03:38:44 +000076 print "-- \n";
77 print "\n";
78 }
79
Lev Walkinf15320b2004-06-03 03:38:44 +000080 if(/^[ \t]*END[ \t]*$/) {
Lev Walkina2bf7242004-09-07 06:36:35 +000081 print;
Lev Walkinf15320b2004-06-03 03:38:44 +000082 select(STDOUT);
83 close(O);
84 $inasn = 0;
Lev Walkina2bf7242004-09-07 06:36:35 +000085 next;
Lev Walkinf15320b2004-06-03 03:38:44 +000086 }
Lev Walkina2bf7242004-09-07 06:36:35 +000087
88 #
89 # The following clauses are primarily designed to make
90 # asn1c command-line easier (i.e., to avoid "-ftypes88").
91 # You may want to get rid of them if you're doing generic
92 # ASN.1 extraction and do not want to alter the ASN.1 specs.
93 #
94 if(
95/^(.*)((UniversalString|BMPString|UTF8String)\s+::=\s+\[[A-Z]+\s\d+\]\sIMPLICIT\sOCTET\sSTRING)(.*)$/ms
96 ) {
97 print "\n-- Legacy redefinition of $3 removed by $0:\n";
98 print "$1-- $2 -- $4";
99 next;
100 } elsif(/delete following line if \"new\" types are supported/) {
101 print;
102 print "/* Legacy stuff deleted by $0:\n";
103 $_ = <>;
104 print;
105 print " */\n";
106 next;
107 }
108
109
110 print; # Dump the ASN.1 module line out.
Lev Walkinf15320b2004-06-03 03:38:44 +0000111}
112
Lev Walkina2bf7242004-09-07 06:36:35 +0000113die "No ASN.1 modules found\n" unless $found;