[net.sources] Partial CPP to clean up conditional compilations

bamford@ihuxy.UUCP (Bamford) (07/29/86)

There has been a lot of discussion about the problems of using CPP to
get rid of the conditional compiles in a source file.  Many of the 
comments have been one of two forms:

  1)  You get a file with the symbol substitutions already performed.
      This makes the source file hard to read.
  2)  There are certain cases in which it doesn't work perfectly.

Given that the whole idea was to change a file saturated with conditional
compilations into a readable and (hopefully) understandable file, I submit
that the following script is "good enough."  It retains comments, does not
substitute symbols (unless they are defined on the command line) and 
produces a file that is much easier to read.  I have to work on programs
that are sensitive to things like: Machine architecture, word width,
swapping/paging, Transfer vector or direct function call, software
paging or operating system paging, and half a dozen different features. 
All of these result in conditionally compiled code using CPP directives
to control the compilation.  The result is often overwhelming.  I have
used the following script to extract a usable version of the source on
numerous occasions.  It is not perfect, and can be fooled, but it
works well and it is trivially sized.

Enjoy.

				Harold Bamford
# C U T   H E R E  ----------
######################################################################
# SCPP -- Partial CPP for reducing the number of conditional compile
# lines in a C source file.  Not perfect, but very simple.
#
# Usage:	scpp -Dsymbol1 -USymbol2 ... < input > output
######################################################################

#
# First step is to disable all macro definitions and file inclusions.
# Then run CPP with -C (to save comments) and -P (to eliminate line
# control information) and the set of "-D" and "-U" arguments supplied.
# Then restore the macro definitions and inclusions to original state.
#
sed	-e 's/^#[ 	]*define[ 	]/%%%saveit%%%&/' \
	-e 's/^#[ 	]*include[ 	]/%%%saveit%%%&/'  | \
${CPP:=/lib/cpp} -C -P $* 2>/dev/null | \
sed -e '
/^%%%saveit%%%#[ 	]*define[ 	]/{
	s/^%%%saveit%%%//
}
' -e '
/^%%%saveit%%%#[ 	]*include[ 	]/{
	s/^%%%saveit%%%//
}
'
#
# EOF

-- 

				Harold Bamford
				AT&T Bell Labs
				(cornet) 8-367-5744
				Naperville, Ill
				(312) 979-5744