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