asn1c/examples/crfc2asn1.pl

129 lines
2.9 KiB
Perl
Raw Normal View History

2007-08-27 23:57:45 +00:00
#!/usr/bin/env perl
2004-06-03 03:38:44 +00:00
#
2013-10-24 08:36:43 +00:00
# Extract the ASN.1 module text from the given set of RFC files.
2004-06-03 03:38:44 +00:00
#
my $inasn = 0; # Are we inside ASN.1 grammar?
my $found = 0;
2004-09-07 06:36:35 +00:00
my $currentFname = '';
2004-06-03 03:38:44 +00:00
if(-t STDIN && $#ARGV == -1) {
2014-09-07 14:13:16 +00:00
print STDERR "Extract the ASN.1 specification from the RFC file(s).\n";
print STDERR "Usage 1: $0 <rfc-file.txt> ...\n";
print STDERR "Usage 2: <someprog> | $0\n";
2004-06-03 03:38:44 +00:00
exit(1);
}
while(<>) {
#
# Strip RFC page delimiters.
#
next if /^[A-Z].*\[Page [0-9]+\]$/;
next if /^ $/;
next if /^RFC [0-9].*[0-9]+$/;
if($inasn == 0) {
#
# The least correct way to find the start of ASN
2006-09-09 11:26:09 +00:00
# definition. That is, to ask a user.
2004-06-03 03:38:44 +00:00
#
2005-03-17 22:43:48 +00:00
if(/^[ \t]*END[ \t]*(--.*)?$/) {
2004-06-03 03:38:44 +00:00
print STDERR
"Missed an ASN.1 grammar before line ". $. ."?\n";
exit(1);
}
2005-03-17 22:43:48 +00:00
my $modName = ''; # ASN.1 module name
2004-06-03 03:38:44 +00:00
my $rfcid = '';
2014-09-11 08:28:57 +00:00
$rfcid = $1 . '-' if($ARGV =~ /([a-z0-9]+)\.[^.]+$/i);
2004-06-03 03:38:44 +00:00
2006-05-04 17:35:24 +00:00
if(/^[ \t]*([A-Z][A-Za-z0-9-]*).*DEFINITIONS.*::=/) {
2005-03-17 22:43:48 +00:00
$modName = $1;
$currentFname = $rfcid . $modName . ".asn1";
2004-06-03 03:38:44 +00:00
$inasn = 1;
} elsif(/^[ \t]*([A-Z][A-Za-z0-9-]*).*{[ \t]*(?:joint-)?iso/
|| /^[ \t]*{[ \t]*(?:joint-)?iso/) {
my @a = ($_);
2005-03-17 22:43:48 +00:00
$modName = $1;
unless(length($modName)) {
next unless $prevLine =~
/^[ \t]*([A-Z][A-Za-z0-9-]*)[ \t]*$/;
$modName = $1;
unshift(@a, $prevLine);
2005-03-17 22:43:48 +00:00
}
$currentFname = $rfcid . $modName . ".asn1";
2004-06-03 03:38:44 +00:00
my $i;
for($i = 0; $i < 8; $i++) {
$_ = <>;
push(@a, $_);
if(/DEFINITIONS/) {
$_ = join('', @a);
$inasn = 1;
last;
}
}
next unless $inasn;
2006-09-09 11:26:09 +00:00
} elsif(/^\s*DEFINITIONS\s*$/
&& $prevLine =~ /^\s*([A-Z][A-Za-z0-9-]*)\s*{[0-9a-z)( -]+}\s*$/) {
$_ = $prevLine . $prevComments . $_;
$modName = $1;
$currentFname = $rfcid . $modName. ".asn1";
$inasn = 1;
2004-06-03 03:38:44 +00:00
} else {
2006-09-09 11:26:09 +00:00
if(/^[ \t]*--/) {
$prevComments .= $_;
} else {
$prevComments = '';
$prevLine = $_;
}
2004-06-03 03:38:44 +00:00
next;
}
2005-03-17 22:43:48 +00:00
print STDERR "Found $modName at line $.\n=> Saving as $currentFname\n";
2004-09-07 06:36:35 +00:00
open(O, "> $currentFname") or die "Can't open $currentFname";
select(O);
2004-06-03 03:38:44 +00:00
$found++;
print "\n";
print "-- \n";
2004-09-07 06:36:35 +00:00
print "-- ASN.1 module found by $0 in $ARGV at line " . $. . "\n";
2004-06-03 03:38:44 +00:00
print "-- \n";
print "\n";
}
2005-03-17 22:43:48 +00:00
if(/^[ \t]*END[ \t]*(--.*)?$/) {
2004-09-07 06:36:35 +00:00
print;
2004-06-03 03:38:44 +00:00
select(STDOUT);
close(O);
$inasn = 0;
2004-09-07 06:36:35 +00:00
next;
2004-06-03 03:38:44 +00:00
}
2004-09-07 06:36:35 +00:00
#
2014-09-11 08:28:57 +00:00
# The following clauses are primarily designed to simplify
# asn1c tool behavior.
2004-09-07 06:36:35 +00:00
# You may want to get rid of them if you're doing generic
2014-09-11 08:28:57 +00:00
# ASN.1 extraction and do not want to alter the ASN.1 specs at all.
2004-09-07 06:36:35 +00:00
#
if(
2005-03-17 22:43:48 +00:00
/^([ \t]*)((UniversalString|BMPString|UTF8String)\s+::=\s+\[[A-Z]+\s\d+\]\sIMPLICIT\sOCTET\sSTRING)(.*)$/ms
2004-09-07 06:36:35 +00:00
) {
print "\n-- Legacy redefinition of $3 removed by $0:\n";
print "$1-- $2 -- $4";
next;
} elsif(/delete following line if \"new\" types are supported/) {
print;
2014-09-11 08:28:57 +00:00
print "/* Legacy constructs deleted by $0:\n";
2004-09-07 06:36:35 +00:00
$_ = <>;
print;
print " */\n";
next;
}
print; # Dump the ASN.1 module line out.
2004-06-03 03:38:44 +00:00
}
2004-09-07 06:36:35 +00:00
die "No ASN.1 modules found\n" unless $found;