NativeEnumerated.c vars NULL init and check
[com/asn1c.git] / examples / crfc2asn1.pl
1 #!/usr/bin/env perl
2 #
3 # Extract the ASN.1 module text from the given set of RFC files.
4 #
5
6
7 my $inasn = 0;  # Are we inside ASN.1 grammar?
8 my $found = 0;
9 my $currentFname = '';
10
11 if(-t STDIN && $#ARGV == -1) {
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";
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 /^\f$/;
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
29                 # definition. That is, to ask a user.
30                 #
31                 if(/^[ \t]*END[ \t]*(--.*)?$/) {
32                         print STDERR
33                         "Missed an ASN.1 grammar before line ". $. ."?\n";
34                         exit(1);
35                 }
36
37                 my $modName = '';       # ASN.1 module name
38                 my $rfcid = '';
39                 $rfcid = $1 . '-' if($ARGV =~ /([a-z0-9]+)\.[^.]+$/i);
40
41                 if(/^[ \t]*([A-Z][A-Za-z0-9-]*).*DEFINITIONS.*::=/) {
42                         $modName = $1;
43                         $currentFname = $rfcid . $modName . ".asn1";
44                         $inasn = 1;
45                 } elsif(/^[ \t]*([A-Z][A-Za-z0-9-]*).*{[ \t]*(?:joint-)?iso/
46                 || /^[ \t]*{[ \t]*(?:joint-)?iso/) {
47                         my @a = ($_);
48                         $modName = $1;
49                         unless(length($modName)) {
50                                 next unless $prevLine =~
51                                         /^[ \t]*([A-Z][A-Za-z0-9-]*)[ \t]*$/;
52                                 $modName = $1;
53                                 unshift(@a, $prevLine);
54                         }
55                         $currentFname = $rfcid . $modName . ".asn1";
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;
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;
73                 } else {
74                         if(/^[ \t]*--/) {
75                                 $prevComments .= $_;
76                         } else {
77                                 $prevComments = '';
78                                 $prevLine = $_;
79                         }
80                         next;
81                 }
82
83                 print STDERR "Found $modName at line $.\n=> Saving as $currentFname\n";
84                 open(O, "> $currentFname") or die "Can't open $currentFname";
85                 select(O);
86
87                 $found++;
88                 print "\n";
89                 print "-- \n";
90                 print "-- ASN.1 module found by $0 in $ARGV at line " . $. . "\n";
91                 print "-- \n";
92                 print "\n";
93         }
94
95         if(/^[ \t]*END[ \t]*(--.*)?$/) {
96                 print;
97                 select(STDOUT);
98                 close(O);
99                 $inasn = 0;
100                 next;
101         }
102
103         #
104         # The following clauses are primarily designed to simplify
105         # asn1c tool behavior.
106         # You may want to get rid of them if you're doing generic
107         # ASN.1 extraction and do not want to alter the ASN.1 specs at all.
108         #
109         if(
110 /^([ \t]*)((UniversalString|BMPString|UTF8String)\s+::=\s+\[[A-Z]+\s\d+\]\sIMPLICIT\sOCTET\sSTRING)(.*)$/ms
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;
117                 print "/* Legacy constructs deleted by $0:\n";
118                 $_ = <>;
119                 print;
120                 print " */\n";
121                 next;
122         }
123  
124
125         print;  # Dump the ASN.1 module line out.
126 }
127
128 die "No ASN.1 modules found\n" unless $found;