vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1 | #!/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 | |
| 12 | my $inasn = 0; # Are we inside ASN.1 grammar? |
| 13 | my $found = 0; |
vlm | 54c5d2e | 2004-09-07 06:36:35 +0000 | [diff] [blame] | 14 | my $currentFname = ''; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 15 | |
| 16 | if(-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 | |
| 23 | while(<>) { |
| 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"; |
vlm | 54c5d2e | 2004-09-07 06:36:35 +0000 | [diff] [blame] | 39 | unlink($currentFile) or die "Can't remove $!"; |
| 40 | print STDERR "Removed $currentFile"; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 41 | 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.*::=/) { |
vlm | 54c5d2e | 2004-09-07 06:36:35 +0000 | [diff] [blame] | 48 | $currentFname = $rfcid . $1 . ".asn1"; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 49 | $inasn = 1; |
| 50 | } elsif(/^[ \t]*([A-Za-z0-9-]+).*{.*iso/) { |
vlm | 54c5d2e | 2004-09-07 06:36:35 +0000 | [diff] [blame] | 51 | $currentFname = $rfcid . $1 . ".asn1"; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 52 | 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; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 64 | } else { |
| 65 | next; |
| 66 | } |
| 67 | |
vlm | 54c5d2e | 2004-09-07 06:36:35 +0000 | [diff] [blame] | 68 | print STDERR "Found $1 at line $. => $currentFname\n"; |
| 69 | open(O, "> $currentFname") or die "Can't open $currentFname"; |
| 70 | select(O); |
| 71 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 72 | $found++; |
| 73 | print "\n"; |
| 74 | print "-- \n"; |
vlm | 54c5d2e | 2004-09-07 06:36:35 +0000 | [diff] [blame] | 75 | print "-- ASN.1 module found by $0 in $ARGV at line " . $. . "\n"; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 76 | print "-- \n"; |
| 77 | print "\n"; |
| 78 | } |
| 79 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 80 | if(/^[ \t]*END[ \t]*$/) { |
vlm | 54c5d2e | 2004-09-07 06:36:35 +0000 | [diff] [blame] | 81 | print; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 82 | select(STDOUT); |
| 83 | close(O); |
| 84 | $inasn = 0; |
vlm | 54c5d2e | 2004-09-07 06:36:35 +0000 | [diff] [blame] | 85 | next; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 86 | } |
vlm | 54c5d2e | 2004-09-07 06:36:35 +0000 | [diff] [blame] | 87 | |
| 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. |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 111 | } |
| 112 | |
vlm | 54c5d2e | 2004-09-07 06:36:35 +0000 | [diff] [blame] | 113 | die "No ASN.1 modules found\n" unless $found; |