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 | 8b3b3d8 | 2014-09-07 07:13:16 -0700 | [diff] [blame] | 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"; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 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 = ''; |
Lev Walkin | e0d321a | 2014-09-11 01:28:57 -0700 | [diff] [blame] | 39 | $rfcid = $1 . '-' if($ARGV =~ /([a-z0-9]+)\.[^.]+$/i); |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 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 | 26303bd | 2016-03-14 02:30:52 -0700 | [diff] [blame] | 45 | } elsif(/^[ \t]*([A-Z][A-Za-z0-9-]*).*{[ \t]*(?:joint-)?iso/ |
| 46 | || /^[ \t]*{[ \t]*(?:joint-)?iso/) { |
Wim Lewis | d9d36f8 | 2014-03-12 13:52:08 -0700 | [diff] [blame] | 47 | my @a = ($_); |
Lev Walkin | d7f237b | 2005-03-17 22:43:48 +0000 | [diff] [blame] | 48 | $modName = $1; |
| 49 | unless(length($modName)) { |
| 50 | next unless $prevLine =~ |
| 51 | /^[ \t]*([A-Z][A-Za-z0-9-]*)[ \t]*$/; |
| 52 | $modName = $1; |
Wim Lewis | d9d36f8 | 2014-03-12 13:52:08 -0700 | [diff] [blame] | 53 | unshift(@a, $prevLine); |
Lev Walkin | d7f237b | 2005-03-17 22:43:48 +0000 | [diff] [blame] | 54 | } |
| 55 | $currentFname = $rfcid . $modName . ".asn1"; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 56 | my $i; |
| 57 | for($i = 0; $i < 8; $i++) { |
| 58 | $_ = <>; |
| 59 | push(@a, $_); |
| 60 | if(/DEFINITIONS/) { |
| 61 | $_ = join('', @a); |
| 62 | $inasn = 1; |
| 63 | last; |
| 64 | } |
| 65 | } |
| 66 | next unless $inasn; |
Lev Walkin | 8c85f2a | 2006-09-09 11:26:09 +0000 | [diff] [blame] | 67 | } elsif(/^\s*DEFINITIONS\s*$/ |
| 68 | && $prevLine =~ /^\s*([A-Z][A-Za-z0-9-]*)\s*{[0-9a-z)( -]+}\s*$/) { |
| 69 | $_ = $prevLine . $prevComments . $_; |
| 70 | $modName = $1; |
| 71 | $currentFname = $rfcid . $modName. ".asn1"; |
| 72 | $inasn = 1; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 73 | } else { |
Lev Walkin | 8c85f2a | 2006-09-09 11:26:09 +0000 | [diff] [blame] | 74 | if(/^[ \t]*--/) { |
| 75 | $prevComments .= $_; |
| 76 | } else { |
| 77 | $prevComments = ''; |
| 78 | $prevLine = $_; |
| 79 | } |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 80 | next; |
| 81 | } |
| 82 | |
Lev Walkin | d7f237b | 2005-03-17 22:43:48 +0000 | [diff] [blame] | 83 | print STDERR "Found $modName at line $.\n=> Saving as $currentFname\n"; |
Lev Walkin | a2bf724 | 2004-09-07 06:36:35 +0000 | [diff] [blame] | 84 | open(O, "> $currentFname") or die "Can't open $currentFname"; |
| 85 | select(O); |
| 86 | |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 87 | $found++; |
| 88 | print "\n"; |
| 89 | print "-- \n"; |
Lev Walkin | a2bf724 | 2004-09-07 06:36:35 +0000 | [diff] [blame] | 90 | print "-- ASN.1 module found by $0 in $ARGV at line " . $. . "\n"; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 91 | print "-- \n"; |
| 92 | print "\n"; |
| 93 | } |
| 94 | |
Lev Walkin | d7f237b | 2005-03-17 22:43:48 +0000 | [diff] [blame] | 95 | if(/^[ \t]*END[ \t]*(--.*)?$/) { |
Lev Walkin | a2bf724 | 2004-09-07 06:36:35 +0000 | [diff] [blame] | 96 | print; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 97 | select(STDOUT); |
| 98 | close(O); |
| 99 | $inasn = 0; |
Lev Walkin | a2bf724 | 2004-09-07 06:36:35 +0000 | [diff] [blame] | 100 | next; |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 101 | } |
Lev Walkin | a2bf724 | 2004-09-07 06:36:35 +0000 | [diff] [blame] | 102 | |
| 103 | # |
Lev Walkin | e0d321a | 2014-09-11 01:28:57 -0700 | [diff] [blame] | 104 | # The following clauses are primarily designed to simplify |
| 105 | # asn1c tool behavior. |
Lev Walkin | a2bf724 | 2004-09-07 06:36:35 +0000 | [diff] [blame] | 106 | # You may want to get rid of them if you're doing generic |
Lev Walkin | e0d321a | 2014-09-11 01:28:57 -0700 | [diff] [blame] | 107 | # ASN.1 extraction and do not want to alter the ASN.1 specs at all. |
Lev Walkin | a2bf724 | 2004-09-07 06:36:35 +0000 | [diff] [blame] | 108 | # |
| 109 | if( |
Lev Walkin | d7f237b | 2005-03-17 22:43:48 +0000 | [diff] [blame] | 110 | /^([ \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] | 111 | ) { |
| 112 | print "\n-- Legacy redefinition of $3 removed by $0:\n"; |
| 113 | print "$1-- $2 -- $4"; |
| 114 | next; |
| 115 | } elsif(/delete following line if \"new\" types are supported/) { |
| 116 | print; |
Lev Walkin | e0d321a | 2014-09-11 01:28:57 -0700 | [diff] [blame] | 117 | print "/* Legacy constructs deleted by $0:\n"; |
Lev Walkin | a2bf724 | 2004-09-07 06:36:35 +0000 | [diff] [blame] | 118 | $_ = <>; |
| 119 | print; |
| 120 | print " */\n"; |
| 121 | next; |
| 122 | } |
| 123 | |
| 124 | |
| 125 | print; # Dump the ASN.1 module line out. |
Lev Walkin | f15320b | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 126 | } |
| 127 | |
Lev Walkin | a2bf724 | 2004-09-07 06:36:35 +0000 | [diff] [blame] | 128 | die "No ASN.1 modules found\n" unless $found; |