-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathamalgc
executable file
·334 lines (285 loc) · 8.42 KB
/
amalgc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#!/usr/bin/perl -w
use strict;
use Pod::Usage;
use Getopt::Long;
use Digest::MD5;
### process commandline options
my @incdirs = ();
my $output = '-';
my $plines = 0;
my $pcomments = 0;
my $help = 0;
GetOptions( "include=s@" => \@incdirs,
"output=s" => \$output,
"lines" => \$plines,
"comments" => \$pcomments,
"help" => \$help );
pod2usage( -verbose => 2,
-exitval => 0,
-noperldoc => 1 ) if $help;
pod2usage( "no C files given" ) unless @ARGV;
### utility functions
sub open_output( $ ) {
my( $out ) = @_;
if( $out eq '-' ) {
return \*STDOUT;
} else {
open( my $fh, ">$out" ) or die( "Can't open $out: $!" );
return $fh;
}
}
sub normalize( $ ) {
my( $dir ) = @_;
$dir .= '/' unless $dir =~ m#/$#;
return $dir;
}
sub find_file( $$ ) {
my( $incname, $searchpath ) = @_;
foreach my $dir ( @$searchpath ) {
my $f = &normalize( $dir ) . $incname;
if( -f $f ) {
$f =~ s/^\.\///;
return $f;
}
}
}
sub file_id( $ ) {
my( $fname ) = @_;
open( my $fh, "<$fname" ) or die( "Can't open $fname: $!" );
my $ctx = Digest::MD5->new();
$ctx->addfile( $fh );
seek( $fh, 0, 0 );
return ( $ctx->hexdigest(), $fh );
}
sub compress_empty_lines( $ ) {
my( $state ) = @_;
if( $state->{current} == \&parse_default and
!$state->{preserve_ln} and
$state->{buffer} =~ m/^\s*$/ ) {
$state->{buffer} = '';
$state->{current} = \&parse_delayed;
}
}
sub go( $$$$$ ) {
my( $fref, $state, $line, $i, $n ) = @_;
$state->{current} = $fref;
return &$fref( $state, $line, $i, $n );
}
sub parse_default( $$$$ ) {
my( $state, $line, $i, $n ) = @_;
my $c = '';
for(; $i < $n; ++$i ) {
$c = substr( $line, $i, 1 );
if( $c eq '"' ) {
$state->{buffer} .= '"';
return &go( \&parse_string_literal, $state, $line, $i+1, $n );
} elsif( $c eq '\'' ) {
$state->{buffer} .= '\'';
return &go( \&parse_char_literal, $state, $line, $i+1, $n );
} elsif( $c eq '/' and '/' eq substr( $line, $i+1, 1 ) ) {
$state->{buffer} .= '//' if $state->{preserve_cmts};
return &go( \&parse_line_comment, $state, $line, $i+2, $n );
} elsif( $c eq '/' and '*' eq substr( $line, $i+1, 1 ) ) {
$state->{is_preprocessor} = ($state->{buffer} =~ m/^\s*#/);
$state->{buffer} .= '/*' if $state->{preserve_cmts};
return &go( \&parse_block_comment, $state, $line, $i+2, $n );
} else {
$state->{buffer} .= $c;
}
}
$state->{continue} = ($c eq '\\');
&compress_empty_lines( $state );
}
# same effects as parse_default, *but* compares unequal to
# parse_default, so avoids special handling in parse_line.
sub parse_delayed( $$$$ ) {
my( $state, $line, $i, $n ) = @_;
return &go( \&parse_default, $state, $line, $i, $n );
}
sub parse_char_literal( $$$$ ) {
my( $state, $line, $i, $n ) = @_;
my $c = '';
my $esc = 0;
for(; $i < $n; ++$i ) {
$c = substr( $line, $i, 1 );
$state->{buffer} .= $c;
if( $c eq '\\' ) {
$esc = !$esc;
} elsif( $c eq '\'' and !$esc ) {
return &go( \&parse_default, $state, $line, $i+1, $n );
}
}
$state->{continue} = ($c eq '\\');
if( !$state->{continue} ) {
$state->{current} = \&parse_default;
} else {
$state->{buffer} .= "\n";
}
}
sub parse_string_literal( $$$$ ) {
my( $state, $line, $i, $n ) = @_;
my $c = '';
my $esc = 0;
for(; $i < $n; ++$i ) {
$c = substr( $line, $i, 1 );
$state->{buffer} .= $c;
if( $c eq '\\' ) {
$esc = !$esc;
} elsif( $c eq '"' and !$esc ) {
return &go( \&parse_default, $state, $line, $i+1, $n );
}
}
$state->{continue} = ($c eq '\\');
if( !$state->{continue} ) {
$state->{current} = \&parse_default;
} else {
$state->{buffer} .= "\n";
}
}
sub parse_line_comment( $$$$ ) {
my( $state, $line, $i, $n ) = @_;
my $c = '';
$state->{buffer} .= substr( $line, $i ) if $state->{preserve_cmts};
$c = substr( $line, $n-1, 1 ) if $n;
$state->{continue} = ($c eq '\\');
if( !$state->{continue} ) {
$state->{current} = \&parse_default;
} else {
$state->{buffer} .= "\n";
}
}
sub parse_block_comment( $$$$ ) {
my( $state, $line, $i, $n ) = @_;
for(; $i < $n; ++$i ) {
my $c = substr( $line, $i, 1 );
if( $c eq '*' and '/' eq substr( $line, $i+1, 1 ) ) {
if( $state->{preserve_cmts} ) {
$state->{buffer} .= '*/';
} else {
$state->{buffer} .= ' ' unless $state->{space_printed};
$state->{space_printed} = 0;
}
return &go( \&parse_default, $state, $line, $i+2, $n );
} else {
if( $state->{preserve_cmts} ) {
$state->{buffer} .= $c;
}
}
}
if( $state->{preserve_cmts} or
(!$state->{is_preprocessor} and $state->{preserve_ln}) ) {
$state->{buffer} .= "\n";
$state->{space_printed} = 1;
}
}
sub make_parse_state( $$ ) {
my( $fname, $preserve_ln, $preserve_cmts ) = @_;
my %state = ( current => \&parse_default,
linenumber => 0,
filename => $fname,
preserve_ln => $preserve_ln,
preserve_cmts => $preserve_cmts,
buffer => '',
continue => 0,
is_preprocessor => 0,
space_printed => 0,
);
return \%state;
}
sub newline( $ ) {
my( $state ) = @_;
$state->{linenumber}++;
if( $state->{current} == \&parse_default and
!$state->{continue} ) {
$state->{is_preprocessor} = 0;
}
}
sub parse_line( $$$$$ ) {
my( $state, $line, $out, $incs, $handled ) = @_;
&newline( $state );
chomp( $line );
$state->{current}( $state, $line, 0, length( $line ) );
if( $state->{current} == \&parse_default ) {
my( $fname ) = ($state->{buffer} =~ m/^\s*#\s*include\s*"(.*)"\s*$/);
if( defined( $fname ) and
($fname = &find_file( $fname, $incs )) ) {
my $res = &handle_file( $fname, $out, $handled, $incs,
$state->{preserve_ln},
$state->{preserve_cmts});
if( $state->{preserve_ln} ) {
if( $res ) {
print( $out "#line ", $state->{linenumber}+1, " \"",
$state->{filename}, "\"\n" );
} else {
print( $out "\n" );
}
}
} else { # no #include we can replace:
print( $out $state->{buffer}, "\n" );
}
$state->{buffer} = '';
}
}
sub handle_file( $$$$$ ) {
my( $fname, $out, $handled, $incs, $plines, $pcomments ) = @_;
my( $id, $in ) = &file_id( $fname );
unless( defined( $handled->{ $id } ) ) {
$handled->{ $id } = 1;
print( $out "#line 1 \"$fname\"\n" ) if $plines;
my $state = &make_parse_state( $fname, $plines, $pcomments );
while( defined( my $line = <$in> ) ) {
&parse_line( $state, $line, $out, $incs, $handled );
}
print( $out $state->{buffer}, "\n" ) if $state->{buffer} ne '';
return 1;
}
return 0;
}
### start processing input files
{
my $out = &open_output( $output );
my %handled = ();
foreach my $input ( @ARGV ) {
&handle_file( $input, $out, \%handled, \@incdirs, $plines, $pcomments );
}
}
__END__
=head1 NAME
amalgc - Create a single C source file for a complete C project
=head1 SYNOPSIS
amalgc [options] file.c [...]
Options:
--include, -i add include directory to search path
--output, -o specify output file
--lines, -l add #line directives for better debugging
--comments, -c keep comments
--help, -h get help
=head1 OPTIONS
=over 4
=item B<--include, -i>
Adds a directory to the search path which is used to map include'd
filenames to real include files. This option may be specified multiple
times.
=item B<--output, -o>
Specifies an output file for the amalgamated source code. Default is
standard output.
=item B<--lines, -l>
Adds #line directives for the preprocessor, so that compiler error
messages (and debug output) point to the correct file and source code
line.
=item B<--comments, -c>
Keeps comments in the amalgamated file.
=item B<--help, -h>
Prints this manual page in text format.
=back
=head1 DESCRIPTION
B<amalgc> reads the files provided on the command line, replaces all
include-directives with the respective file contents when they are
first encountered and prints the result to the output file (or
C<stdout>).
Certain include-directives are B<not> replaced, such as system headers
(or more specifically any header files not found in the specified
include directories).
=head1 AUTHOR
2011, Philipp Janda <siffiejoe@gmx.net>
=cut