3 # Extract the ASN.1 module text from the given set of RFC files.
7 my $inasn = 0; # Are we inside ASN.1 grammar?
11 if(-t STDIN && $#ARGV == -1) {
12 print STDERR "Extract the ASN.1 specification from the RFC file(s).\n";
13 print STDERR "Usage 1: $0 <rfc-file.txt> ...\n";
14 print STDERR "Usage 2: <someprog> | $0\n";
20 # Strip RFC page delimiters.
22 next if /^[A-Z].*\[Page [0-9]+\]$/;
24 next if /^RFC [0-9].*[0-9]+$/;
28 # The least correct way to find the start of ASN
29 # definition. That is, to ask a user.
31 if(/^[ \t]*END[ \t]*(--.*)?$/) {
33 "Missed an ASN.1 grammar before line ". $. ."?\n";
37 my $modName = ''; # ASN.1 module name
39 $rfcid = $1 . '-' if($ARGV =~ /([a-z0-9]+)\.[^.]+$/i);
41 if(/^[ \t]*([A-Z][A-Za-z0-9-]*).*DEFINITIONS.*::=/) {
43 $currentFname = $rfcid . $modName . ".asn1";
45 } elsif(/^[ \t]*([A-Z][A-Za-z0-9-]*).*{[ \t]*(?:joint-)?iso/
46 || /^[ \t]*{[ \t]*(?:joint-)?iso/) {
49 unless(length($modName)) {
50 next unless $prevLine =~
51 /^[ \t]*([A-Z][A-Za-z0-9-]*)[ \t]*$/;
53 unshift(@a, $prevLine);
55 $currentFname = $rfcid . $modName . ".asn1";
57 for($i = 0; $i < 8; $i++) {
67 } elsif(/^\s*DEFINITIONS\s*$/
68 && $prevLine =~ /^\s*([A-Z][A-Za-z0-9-]*)\s*{[0-9a-z)( -]+}\s*$/) {
69 $_ = $prevLine . $prevComments . $_;
71 $currentFname = $rfcid . $modName. ".asn1";
83 print STDERR "Found $modName at line $.\n=> Saving as $currentFname\n";
84 open(O, "> $currentFname") or die "Can't open $currentFname";
90 print "-- ASN.1 module found by $0 in $ARGV at line " . $. . "\n";
95 if(/^[ \t]*END[ \t]*(--.*)?$/) {
104 # The following clauses are primarily designed to simplify
105 # asn1c tool behavior.
106 # You may want to get rid of them if you're doing generic
107 # ASN.1 extraction and do not want to alter the ASN.1 specs at all.
110 /^([ \t]*)((UniversalString|BMPString|UTF8String)\s+::=\s+\[[A-Z]+\s\d+\]\sIMPLICIT\sOCTET\sSTRING)(.*)$/ms
112 print "\n-- Legacy redefinition of $3 removed by $0:\n";
113 print "$1-- $2 -- $4";
115 } elsif(/delete following line if \"new\" types are supported/) {
117 print "/* Legacy constructs deleted by $0:\n";
125 print; # Dump the ASN.1 module line out.
128 die "No ASN.1 modules found\n" unless $found;