[comp.lang.c] FIXED Perl line-scruncher solution

merlyn@intelob.intel.com (Randal L. Schwartz @ Stonehenge) (03/31/89)

In article <4257@omepd.UUCP>, a rather dingy merlyn@intelob writes:
| Here's my solution, just 5 lines of (mostly readable :-) Perl...
| 
| #!/usr/bin/perl
| for ($file = "stdin", $line = 1; $_ = <stdin>;) {
| 	($line = $1, $file = $2, $sync = 0, next) if /^#\W*(\d+)\W+"(.*)"\W*$/;
| 	($sync = 0, $line++, next) if /^\W*$/;
| 	printf "#line %d \"%s\"\n", $line, $file unless $sync++;
| 	print; $line++;
| }

Arrrgh.  Someone should beat into my head that non-word != whitespace.
All the '\W' up there should be '\s'. Anyway, for those of you that
want the "new and improved" version, try this (I call it "seepp"):

---------------------------------------- cut here --------------------
#!/usr/bin/perl
open(CPP,$a = "/lib/cpp " . join(" ",@ARGV) . "|") ||
	die "Cannot exec '$a' ($!)\n";
for ($file = "/dev/null", $line = 1; $_ = <CPP>;) {
	($file = $1, $line = $2, $sync = 0, next) if /^#\s*"(.*)"\s+(\d+)\s*$/;
	($file = $1, $line++, $sync = 0, next) if /^#\s*"(.*)"\s*$/;
	($line = $1, $file = $2, $sync = 0, next) if /^#\s*(\d+)\s+"(.*)"\s*$/;
	($line = $1, $sync = 0, next) if /^#\s*(\d+)\s*$/;
	($sync = 0, $line++, next) if /^\s*$/;
	printf "#line %d \"%s\"\n", $line, $file unless $sync++;
	print; $line++;
}
close(CPP) || die "Cannot close '$a' ??? ($!)\n";
exit(0);
---------------------------------------- cut here --------------------
(side note to Larry: if you wanna stick this in 'eg', go ahead...)

Probably should have had lunch first,
-- 
/     Randal L. Schwartz, Stonehenge Consulting Services (503)777-0095        \
|        on contract to BiiN (for now :-) Hillsboro, Oregon, USA.             |
|<@intel-iwarp.arpa:merlyn@intelob.intel.com> ...!uunet!tektronix!biin!merlyn |
\     Cute quote: "Welcome to Oregon... home of the California Raisins!"      /