1 | #!/usr/bin/perl |
---|
2 | # $Id$ |
---|
3 | # |
---|
4 | # makeheader.pl - generate a header file out of several header file (ver 1.2). |
---|
5 | # |
---|
6 | |
---|
7 | # |
---|
8 | # searchpath - search a file in a path and return full path. |
---|
9 | # |
---|
10 | sub searchpath { |
---|
11 | local ( $file, $path ) = @_; |
---|
12 | for ( split ( /:/, $path ) ) { |
---|
13 | if ( -f "$_/$file" ) { |
---|
14 | return "$_/$file"; |
---|
15 | } |
---|
16 | } |
---|
17 | return ""; |
---|
18 | } |
---|
19 | |
---|
20 | if ( ( $#ARGV == -1 ) || ( $ARGV[0] =~ /-help|-\?/ ) ) { |
---|
21 | die " |
---|
22 | usage: makeheader [-?] {-I<includedir>} <templatefile> <outfile> |
---|
23 | |
---|
24 | This is makeheader, a header file generation tool. |
---|
25 | With makeheader it is possible to generate a single header |
---|
26 | file out of several other header files. |
---|
27 | |
---|
28 | To include a header file into the generated header file |
---|
29 | \(= <outfile>) use the two lines |
---|
30 | |
---|
31 | /*MAKEHEADER*/ |
---|
32 | #include \"<includefile>\" |
---|
33 | |
---|
34 | in <templatefile>. |
---|
35 | |
---|
36 | If you use the alternate form |
---|
37 | |
---|
38 | /*MAKEHEADER PUBLIC ONLY*/ |
---|
39 | #include \"<includefile>\" |
---|
40 | |
---|
41 | only sections marked with /*BEGINPUBLIC*/ and /*ENDPUBLIC*/ are |
---|
42 | pasted from <includefile> into <outfile>. |
---|
43 | |
---|
44 | <includefile> is looked up in all <includedir>s, then in the |
---|
45 | current directory, then in the base directory of |
---|
46 | <templatefile>.\n"; |
---|
47 | } |
---|
48 | |
---|
49 | # get list of include directories |
---|
50 | $includes = ""; |
---|
51 | while ( $ARGV[0] =~ /^-/ ) { |
---|
52 | $_ = shift; |
---|
53 | if ( /^-I(.*)/ ) { |
---|
54 | $includes .= ( $1 ? $1 : shift ) . ":"; |
---|
55 | } |
---|
56 | } |
---|
57 | $includes .= ".:"; |
---|
58 | |
---|
59 | # check for rest of arguments |
---|
60 | if ( $#ARGV < 1 ) { |
---|
61 | die "usage: makeheader [-?] {-I<includedir>} <templatefile> <outfile>\n\n"; |
---|
62 | } |
---|
63 | |
---|
64 | $infile = shift( @ARGV ); |
---|
65 | $outfile = shift( @ARGV ); |
---|
66 | if ( $infile =~ /(.*)\// ) { |
---|
67 | $includes .= "$1:"; |
---|
68 | } |
---|
69 | |
---|
70 | open( INFILE, $infile ) || |
---|
71 | die "makeheader: Can not open template file $infile for reading!\n"; |
---|
72 | open( OUTFILE, ">$outfile" ) || |
---|
73 | die "makeheader: Can not open header file $outfile for writing!\n"; |
---|
74 | |
---|
75 | print OUTFILE "\n/* $outfile automatically generated by $0 " . "*/\n"; |
---|
76 | |
---|
77 | while( <INFILE> ) { |
---|
78 | if ( /^\/\*MAKEHEADER(.*)\*\/$/ ) { |
---|
79 | # check whether we have to read public part only |
---|
80 | if ( $1 eq " PUBLIC ONLY" ) { |
---|
81 | $public_only = 1; |
---|
82 | } else { |
---|
83 | $public_only = 0; |
---|
84 | } |
---|
85 | |
---|
86 | # get name of file to include |
---|
87 | $_ = <INFILE>; chop; |
---|
88 | if ( /^\#include \"(.*)\"$/ ) { |
---|
89 | $inclfile = $1; |
---|
90 | } else { |
---|
91 | die "makeheader: $_ is not a valid include directive!\n"; |
---|
92 | } |
---|
93 | |
---|
94 | $inclfile = &searchpath( $inclfile, $includes ) || |
---|
95 | die "makeheader: Can not open included file $inclfile for reading!\n"; |
---|
96 | open( INCLFILE, $inclfile ) || |
---|
97 | die "makeheader: Can not open included file $inclfile for reading!\n"; |
---|
98 | |
---|
99 | print OUTFILE "\n/* Stuff included from $inclfile */\n"; |
---|
100 | if ( $public_only ) { |
---|
101 | # read public part only |
---|
102 | while ( <INCLFILE> ) { |
---|
103 | if ( /^\/\*BEGINPUBLIC\*\/$/ ) { |
---|
104 | while ( <INCLFILE> ) { |
---|
105 | last if ( /^\/\*ENDPUBLIC\*\/$/ ); |
---|
106 | print OUTFILE $_; |
---|
107 | } |
---|
108 | } |
---|
109 | } |
---|
110 | } else { |
---|
111 | # just paste contents into output |
---|
112 | while ( <INCLFILE> ) { print OUTFILE $_; } |
---|
113 | } |
---|
114 | close INCLFILE; |
---|
115 | } |
---|
116 | else { |
---|
117 | print OUTFILE $_; |
---|
118 | } |
---|
119 | } |
---|
120 | |
---|
121 | close OUTFILE; |
---|
122 | close INFILE; |
---|