Need help using /usr/lib/cpp for generic text

Maarten Litmaath maart at cs.vu.nl
Fri Sep 22 07:46:16 AEST 1989


verber at pacific.mps.ohio-state.edu (Mark A. Verber) writes:
\...
\(1)  Bloody "# line-number file-name" lines
\
\I had thought that the -P switch suppressed such output, but that doesn't
\seem to be the case (SunOS 4.x /usr/lib/cpp).  I don't want these lines.

Huh?  `-P' works for me on SunOS 4.0.1!

\(2)  Leaving <cr> in the text
\
\When I run text like:	I get the output like:		I would like:
\
\	#define foo	before				before
\	before						test
\	#ifdef foo	test				after
\	test
\	#endif		after
\	after

You could use the following script instead of cpp:
----------8<----------8<----------8<----------8<----------8<----------
#!/bin/sh

tab="	"

for i in define undef ifdef ifndef if elif else endif
do
	SED1="
		$SED1
		/^[ $tab]*#[ $tab]*$i/{
			s//\\\\&/
			p
			s/.//
			b
		}
	"
			# due to a bug in some sed versions, the `p' mustn't
			# be appended to the previous substitute command
	SED2="
		$SED2
		/^\\\\[ $tab]*#[ $tab]*$i/{
			N
			d
		}
	"
done

sed "$SED1" ${1+"$@"} | /lib/cpp -P | sed "$SED2"
----------8<----------8<----------8<----------8<----------8<----------

The idea is to remember that a line must be removed:

	#define foo bar

becomes

	\#define foo bar
	#define foo bar

cpp will leave the first line intact and change the second to an empty line.
The second invocation of sed will delete both lines.
One limitation: you shouldn't use `/*' and `*/' to comment out text; instead
use:
	#if 0
	...
	#endif 0
-- 
   creat(2) shouldn't have been create(2): |Maarten Litmaath @ VU Amsterdam:
      it shouldn't have existed at all.    |maart at cs.vu.nl, mcvax!botter!maart



More information about the Comp.unix.wizards mailing list