Lev Walkin | f15320b | 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; |
Lev Walkin | a2bf724 | 2004-09-07 06:36:35 +0000 | [diff] [blame] | 14 | my $currentFname = ''; |
Lev Walkin | f15320b | 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 | # |
Lev Walkin | d7f237b | 2005-03-17 22:43:48 +0000 | [diff] [blame] | 36 | if(/^[ \t]*END[ \t]*(--.*)?$/) { |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 37 | print STDERR |
| 38 | "Missed an ASN.1 grammar before line ". $. ."?\n"; |
| 39 | exit(1); |
| 40 | } |
| 41 | |
Lev Walkin | d7f237b | 2005-03-17 22:43:48 +0000 | [diff] [blame] | 42 | my $modName = ''; # ASN.1 module name |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 43 | my $rfcid = ''; |
| 44 | $rfcid = $1 . '-' if($ARGV =~ /([a-z0-9]+)/i); |
| 45 | |
Lev Walkin | d7f237b | 2005-03-17 22:43:48 +0000 | [diff] [blame] | 46 | if(/^[ \t]+([A-Z][A-Za-z0-9-]*).*DEFINITIONS.*::=/) { |
| 47 | $modName = $1; |
| 48 | $currentFname = $rfcid . $modName . ".asn1"; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 49 | $inasn = 1; |
Lev Walkin | d7f237b | 2005-03-17 22:43:48 +0000 | [diff] [blame] | 50 | } elsif(/^[ \t]*([A-Z][A-Za-z0-9-]*).*{[ \t]*iso/ |
| 51 | || /^[ \t]*{[ \t]*iso/) { |
| 52 | $modName = $1; |
| 53 | unless(length($modName)) { |
| 54 | next unless $prevLine =~ |
| 55 | /^[ \t]*([A-Z][A-Za-z0-9-]*)[ \t]*$/; |
| 56 | $modName = $1; |
| 57 | } |
| 58 | $currentFname = $rfcid . $modName . ".asn1"; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 59 | my @a = ($_); |
| 60 | my $i; |
| 61 | for($i = 0; $i < 8; $i++) { |
| 62 | $_ = <>; |
| 63 | push(@a, $_); |
| 64 | if(/DEFINITIONS/) { |
| 65 | $_ = join('', @a); |
| 66 | $inasn = 1; |
| 67 | last; |
| 68 | } |
| 69 | } |
| 70 | next unless $inasn; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 71 | } else { |
Lev Walkin | d7f237b | 2005-03-17 22:43:48 +0000 | [diff] [blame] | 72 | $prevLine = $_; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 73 | next; |
| 74 | } |
| 75 | |
Lev Walkin | d7f237b | 2005-03-17 22:43:48 +0000 | [diff] [blame] | 76 | print STDERR "Found $modName at line $.\n=> Saving as $currentFname\n"; |
Lev Walkin | a2bf724 | 2004-09-07 06:36:35 +0000 | [diff] [blame] | 77 | open(O, "> $currentFname") or die "Can't open $currentFname"; |
| 78 | select(O); |
| 79 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 80 | $found++; |
| 81 | print "\n"; |
| 82 | print "-- \n"; |
Lev Walkin | a2bf724 | 2004-09-07 06:36:35 +0000 | [diff] [blame] | 83 | print "-- ASN.1 module found by $0 in $ARGV at line " . $. . "\n"; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 84 | print "-- \n"; |
| 85 | print "\n"; |
| 86 | } |
| 87 | |
Lev Walkin | d7f237b | 2005-03-17 22:43:48 +0000 | [diff] [blame] | 88 | if(/^[ \t]*END[ \t]*(--.*)?$/) { |
Lev Walkin | a2bf724 | 2004-09-07 06:36:35 +0000 | [diff] [blame] | 89 | print; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 90 | select(STDOUT); |
| 91 | close(O); |
| 92 | $inasn = 0; |
Lev Walkin | a2bf724 | 2004-09-07 06:36:35 +0000 | [diff] [blame] | 93 | next; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 94 | } |
Lev Walkin | a2bf724 | 2004-09-07 06:36:35 +0000 | [diff] [blame] | 95 | |
| 96 | # |
| 97 | # The following clauses are primarily designed to make |
| 98 | # asn1c command-line easier (i.e., to avoid "-ftypes88"). |
| 99 | # You may want to get rid of them if you're doing generic |
| 100 | # ASN.1 extraction and do not want to alter the ASN.1 specs. |
| 101 | # |
| 102 | if( |
Lev Walkin | d7f237b | 2005-03-17 22:43:48 +0000 | [diff] [blame] | 103 | /^([ \t]*)((UniversalString|BMPString|UTF8String)\s+::=\s+\[[A-Z]+\s\d+\]\sIMPLICIT\sOCTET\sSTRING)(.*)$/ms |
Lev Walkin | a2bf724 | 2004-09-07 06:36:35 +0000 | [diff] [blame] | 104 | ) { |
| 105 | print "\n-- Legacy redefinition of $3 removed by $0:\n"; |
| 106 | print "$1-- $2 -- $4"; |
| 107 | next; |
| 108 | } elsif(/delete following line if \"new\" types are supported/) { |
| 109 | print; |
| 110 | print "/* Legacy stuff deleted by $0:\n"; |
| 111 | $_ = <>; |
| 112 | print; |
| 113 | print " */\n"; |
| 114 | next; |
| 115 | } |
| 116 | |
| 117 | |
| 118 | print; # Dump the ASN.1 module line out. |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 119 | } |
| 120 | |
Lev Walkin | a2bf724 | 2004-09-07 06:36:35 +0000 | [diff] [blame] | 121 | die "No ASN.1 modules found\n" unless $found; |