Debian Codegraph @

scripts/gpp

raw
1 #!/usr/bin/perl
2 ############################################################################
3 #
4 # gpp - general pre processor
5 #
6 # This script is a pre-processor that can be used for parsing documents
7 # and get #ifdef/#ifndef/#else/#endif to decide how the output looks.
8 # Defines are set with the -D option. The -nr option is for setting the
9 # name regular expression that is used to convert infile-names to outfile-
10 # names. --help gives a short usage summary.
11 #
12 # Hacked in perl by Lars J. Aas <[email protected]>, 1998.
13 #
14
15 @options = ();
16 @files = ();
17
18 foreach $arg ( @ARGV ) {
19 if ( -f $arg ) {
20 push( @files, $arg );
21 } else {
22 push( @options, $arg ); # hopefully
23 }
24 }
25
26 # parse options
27
28 %defines = ();
29 $nameexp = "";
30
31 foreach $opt ( @options ) {
32 if ( $opt =~ /^-D/ ) { # define
33 $define = substr( $opt, 2 );
34 $defines{$define} = 1;
35 } elsif ( $opt =~ /^-(h|-help)/ || $opt =~ /^--usage/ ) {
36 print "USAGE: gpp -nr<expr> -D<define> <files>\n";
37 exit 0;
38 } elsif ( $opt =~ /^-nr/ ) {
39 $nameexp = substr( $opt, 3 );
40 } else {
41 print STDERR "Error: unknown option \"", $opt,
42 "\" (or no such file).\n";
43 exit 0;
44 }
45 }
46
47 if ( $nameexp =~ /^$/ ) {
48 print STDERR "ERROR: no name-modifying regular expression.\n";
49 exit 0;
50 }
51
52 foreach $file ( @files ) {
53 $oldname = $file;
54 $newname = $file;
55 $string = '$newname =~ ' . $nameexp . ";";
56 eval $string;
57 print "old name: ", $oldname, "\nnew name: ", $newname, "\n";
58 if ( $oldname eq $newname ) {
59 print STDERR "ERROR: can't use same name for both input and output.\n";
60 exit 0;
61 }
62
63 @inlines = ();
64 @outlines = ();
65
66 open( INPUT, $oldname ) || die "open( ", $oldname, " ): $!\n";
67 @inlines = <INPUT>;
68 close( INPUT );
69
70 $copy = 1;
71 for ( $linenum = 0; $linenum < $#inlines; $linenum++ ) {
72 if ( $inlines[$linenum] =~ /^#/ ) {
73 $directive = $inlines[$linenum];
74 chop( $directive );
75 if ( $directive =~ /^#else/ ) {
76 $copy = ($copy + 1) % 2;
77 } elsif ( $directive =~ /^#endif/ ) {
78 $copy = 1;
79 } elsif ( $directive =~ /^#ifdef / ) {
80 $var = substr( $directive, 7 );
81 if ( $defines{$var} == 1 ) {
82 $copy = 1;
83 } else {
84 $copy = 0;
85 }
86 } elsif ( $directive =~ /^#ifndef / ) {
87 $var = substr( $directive, 8 );
88 if ( $defines{$var} == 1 ) {
89 $copy = 0;
90 } else {
91 $copy = 1;
92 }
93 }
94 } elsif ( $copy == 1 ) {
95 push( @outlines, $inlines[$linenum] );
96 }
97 }
98
99 open( OUTPUT, "> $newname" ) || die "open( > ", $newname, " ): $!\n";
100 print OUTPUT @outlines;
101 close( OUTPUT );
102 @outlines = ();
103 }
104