vlm | 83b901d | 2007-08-27 23:57:45 +0000 | [diff] [blame^] | 1 | #!/usr/bin/env perl |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 2 | |
| 3 | # |
| 4 | # $Id$ |
| 5 | # $Author$ |
| 6 | # |
vlm | 83b901d | 2007-08-27 23:57:45 +0000 | [diff] [blame^] | 7 | # Simple tool to fetch the ASN.1 specifications from the given set of RFC files. |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 8 | # |
| 9 | |
| 10 | |
| 11 | my $inasn = 0; # Are we inside ASN.1 grammar? |
| 12 | my $found = 0; |
vlm | 54c5d2e | 2004-09-07 06:36:35 +0000 | [diff] [blame] | 13 | my $currentFname = ''; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 14 | |
| 15 | if(-t STDIN && $#ARGV == -1) { |
| 16 | print STDERR "Rip ASN.1 specification from RFC file\n"; |
| 17 | print STDERR "Usage: $0 <rfc-file.txt> ...\n"; |
| 18 | print STDERR "Usage: <someprog> | $0\n"; |
| 19 | exit(1); |
| 20 | } |
| 21 | |
| 22 | while(<>) { |
| 23 | # |
| 24 | # Strip RFC page delimiters. |
| 25 | # |
| 26 | next if /^[A-Z].*\[Page [0-9]+\]$/; |
| 27 | next if /^$/; |
| 28 | next if /^RFC [0-9].*[0-9]+$/; |
| 29 | |
| 30 | if($inasn == 0) { |
| 31 | # |
| 32 | # The least correct way to find the start of ASN |
vlm | 3ceb326 | 2006-09-09 11:26:09 +0000 | [diff] [blame] | 33 | # definition. That is, to ask a user. |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 34 | # |
vlm | 4620b56 | 2005-03-17 22:43:48 +0000 | [diff] [blame] | 35 | if(/^[ \t]*END[ \t]*(--.*)?$/) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 36 | print STDERR |
| 37 | "Missed an ASN.1 grammar before line ". $. ."?\n"; |
| 38 | exit(1); |
| 39 | } |
| 40 | |
vlm | 4620b56 | 2005-03-17 22:43:48 +0000 | [diff] [blame] | 41 | my $modName = ''; # ASN.1 module name |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 42 | my $rfcid = ''; |
| 43 | $rfcid = $1 . '-' if($ARGV =~ /([a-z0-9]+)/i); |
| 44 | |
vlm | 5566ec4 | 2006-05-04 17:35:24 +0000 | [diff] [blame] | 45 | if(/^[ \t]*([A-Z][A-Za-z0-9-]*).*DEFINITIONS.*::=/) { |
vlm | 4620b56 | 2005-03-17 22:43:48 +0000 | [diff] [blame] | 46 | $modName = $1; |
| 47 | $currentFname = $rfcid . $modName . ".asn1"; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 48 | $inasn = 1; |
vlm | 4620b56 | 2005-03-17 22:43:48 +0000 | [diff] [blame] | 49 | } elsif(/^[ \t]*([A-Z][A-Za-z0-9-]*).*{[ \t]*iso/ |
| 50 | || /^[ \t]*{[ \t]*iso/) { |
| 51 | $modName = $1; |
| 52 | unless(length($modName)) { |
| 53 | next unless $prevLine =~ |
| 54 | /^[ \t]*([A-Z][A-Za-z0-9-]*)[ \t]*$/; |
| 55 | $modName = $1; |
| 56 | } |
| 57 | $currentFname = $rfcid . $modName . ".asn1"; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 58 | my @a = ($_); |
| 59 | my $i; |
| 60 | for($i = 0; $i < 8; $i++) { |
| 61 | $_ = <>; |
| 62 | push(@a, $_); |
| 63 | if(/DEFINITIONS/) { |
| 64 | $_ = join('', @a); |
| 65 | $inasn = 1; |
| 66 | last; |
| 67 | } |
| 68 | } |
| 69 | next unless $inasn; |
vlm | 3ceb326 | 2006-09-09 11:26:09 +0000 | [diff] [blame] | 70 | } elsif(/^\s*DEFINITIONS\s*$/ |
| 71 | && $prevLine =~ /^\s*([A-Z][A-Za-z0-9-]*)\s*{[0-9a-z)( -]+}\s*$/) { |
| 72 | $_ = $prevLine . $prevComments . $_; |
| 73 | $modName = $1; |
| 74 | $currentFname = $rfcid . $modName. ".asn1"; |
| 75 | $inasn = 1; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 76 | } else { |
vlm | 3ceb326 | 2006-09-09 11:26:09 +0000 | [diff] [blame] | 77 | if(/^[ \t]*--/) { |
| 78 | $prevComments .= $_; |
| 79 | } else { |
| 80 | $prevComments = ''; |
| 81 | $prevLine = $_; |
| 82 | } |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 83 | next; |
| 84 | } |
| 85 | |
vlm | 4620b56 | 2005-03-17 22:43:48 +0000 | [diff] [blame] | 86 | print STDERR "Found $modName at line $.\n=> Saving as $currentFname\n"; |
vlm | 54c5d2e | 2004-09-07 06:36:35 +0000 | [diff] [blame] | 87 | open(O, "> $currentFname") or die "Can't open $currentFname"; |
| 88 | select(O); |
| 89 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 90 | $found++; |
| 91 | print "\n"; |
| 92 | print "-- \n"; |
vlm | 54c5d2e | 2004-09-07 06:36:35 +0000 | [diff] [blame] | 93 | print "-- ASN.1 module found by $0 in $ARGV at line " . $. . "\n"; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 94 | print "-- \n"; |
| 95 | print "\n"; |
| 96 | } |
| 97 | |
vlm | 4620b56 | 2005-03-17 22:43:48 +0000 | [diff] [blame] | 98 | if(/^[ \t]*END[ \t]*(--.*)?$/) { |
vlm | 54c5d2e | 2004-09-07 06:36:35 +0000 | [diff] [blame] | 99 | print; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 100 | select(STDOUT); |
| 101 | close(O); |
| 102 | $inasn = 0; |
vlm | 54c5d2e | 2004-09-07 06:36:35 +0000 | [diff] [blame] | 103 | next; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 104 | } |
vlm | 54c5d2e | 2004-09-07 06:36:35 +0000 | [diff] [blame] | 105 | |
| 106 | # |
| 107 | # The following clauses are primarily designed to make |
| 108 | # asn1c command-line easier (i.e., to avoid "-ftypes88"). |
| 109 | # You may want to get rid of them if you're doing generic |
| 110 | # ASN.1 extraction and do not want to alter the ASN.1 specs. |
| 111 | # |
| 112 | if( |
vlm | 4620b56 | 2005-03-17 22:43:48 +0000 | [diff] [blame] | 113 | /^([ \t]*)((UniversalString|BMPString|UTF8String)\s+::=\s+\[[A-Z]+\s\d+\]\sIMPLICIT\sOCTET\sSTRING)(.*)$/ms |
vlm | 54c5d2e | 2004-09-07 06:36:35 +0000 | [diff] [blame] | 114 | ) { |
| 115 | print "\n-- Legacy redefinition of $3 removed by $0:\n"; |
| 116 | print "$1-- $2 -- $4"; |
| 117 | next; |
| 118 | } elsif(/delete following line if \"new\" types are supported/) { |
| 119 | print; |
| 120 | print "/* Legacy stuff deleted by $0:\n"; |
| 121 | $_ = <>; |
| 122 | print; |
| 123 | print " */\n"; |
| 124 | next; |
| 125 | } |
| 126 | |
| 127 | |
| 128 | print; # Dump the ASN.1 module line out. |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 129 | } |
| 130 | |
vlm | 54c5d2e | 2004-09-07 06:36:35 +0000 | [diff] [blame] | 131 | die "No ASN.1 modules found\n" unless $found; |