[net.sources] ifdef filter

keith@enmasse.UUCP (Keith Crews) (07/17/86)

It often happens that software packages are set up to run on so many
configurations that it is hard to read the code because of all the ifdefs.
I would like a filter for C programs that does something like the
following:

Invoke it like this:

	prog <x.c >x.c.new -DSYSV -D68000

and it would take input of the form:

#ifdef 68000
short i;
#else
int i;
#endif
#ifdef BSD
blah
#else
blech
#endif
...

and produce			or maybe

#ifdef 68000			#ifdef 68000
short i;			short i;
#endif				#else
blech				#endif
				#ifdef BSD
				#else
				blech
				#endif

That is, filter out everything that is not going to be expanded by cpp while
retaining the ifdefs that are going to be expanded, just for comment value.
Ideally the output, when compiled, would produce exactly the same results as the
original.  Does anyone have anything like this?

bc@cyb-eng.UUCP (Bill Crews) (07/18/86)

> It often happens that software packages are set up to run on so many
> configurations that it is hard to read the code because of all the ifdefs.
> I would like a filter for C programs that does something like the
> following:
> [...deleted specification...]
> That is, filter out everything that is not going to be expanded by cpp while
> retaining the ifdefs that are going to be expanded, just for comment value.
> Ideally the output, when compiled, would produce exactly the same results as the
> original.  Does anyone have anything like this?

Me, too.  I keep planning to write it and never seem to find the time.
-- 
bc				Bill Crews @ NetCor Data International

..!{seismo,gatech,ihnp4}!ut-sally!cyb-eng!bc  (512) 835-2266

rbj@icst-cmr (Root Boy Jim) (07/18/86)

	That is, filter out everything that is not going to be expanded
	by cpp while retaining the ifdefs that are going to be
	expanded, just for comment value.  Ideally the output, when
	compiled, would produce exactly the same results as the
	original.  Does anyone have anything like this?

Berkeley 4.x has a program called unifdef. The RAND corparation has
a program like this on their new RAND editor tape. I might be able to
send you a copy of it if I can find a good path to you.

	(Root Boy) Jim Cottrell		<rbj@icst-cmr.arpa>
Vote for ME -- I'm well-tapered, half-cocked, ill-conceived and TAX-DEFERRED!

authorplaceholder@mirror.UUCP (07/19/86)

Here is the manpage from "scpp", the selective C pre-processor,
written by Brad Needham (textronix!tekig4!bradn), available from
your nearest mod.sources archive.
	/rich $alz

.TH SCPP 1 "28 September 1983"
.SH NAME
scpp \- selective C preprocessor
.SH SYNOPSIS
.B scpp
[
.BI \-M macro
] [
.BI \-D macro
] [
.BI \-D macro=def
] [
.B \-C
]
.ti +5
[
.BI \-I incdir
] [
.I file...
]
.SH DESCRIPTION
.B Scpp
concatenates the input
.I files
(or reads standard-in, if no
.I file
is given),
interprets all references to given macros,
leaving the rest of the
.IR file "(s)"
unaltered,
then writes the result to standard-out.
It is helpful in removing conditionally compiled code or misleading
macros from a file.
.PP
The
.I file
name "\fB-\fP" refers to standard-in.
.PP
The following options are available.
Each option can appear as frequently as desired.
.RS
.TP
.SM \-M
Interpret all references to the given
.I macro.
.I Macro
can be either a single macro name or a whitespace-separated list of
macro names (e.g. -MMAXINT or -M"MAXINT MININT INTWID").
All occurrences of the macro and all instances of the intrinsic macro
\&"defined()" referring to this macro are expanded.
All preprocessor directives referring to this macro (except
.BR #if )
perform their usual function and do not appear in the output.
.B #if
directives are interpreted only if their value is not dependent on macros
which are not to be interpreted.
.TP
.SM \-D
Define the
.I macro
to have the value
.I def,
or "1" if no
.I def
is given.
Unlike the C preprocessor,
.B scpp
does not implicitly define certain macros that describe the environment in
which the code will be running (e.g. "vax" or "unix").
.B \-D
implies
.B \-M.
.TP
.SM \-C
Preserve comments and whitespace in interpreted macro definitions.
Normally, comments and leading and trailing whitespace are stripped from
interpreted macro definitions.
.TP
.SM \-I
Add
.I incdir
to the list of directories to be searched for include files.
.B Scpp
searches directories in the same order as the C preprocessor:
if the include filename is enclosed in double-quotes ("...")
rather than angle-brackets (<...>),
the directory containing the current file being processed;
the directories given by -I, left-to-right;
the standard directory, /usr/include.
.RE
.SH AUTHOR
Brad Needham, Tektronix, Inc.
.SH "SEE ALSO"
cc(1).
.SH BUGS
Very long identifiers (those over 100 characters long) will crash
.B scpp.
.PP
Because
.B scpp
interprets only the given macros, the meaning of some code will change.
E.g. "scpp -MBOO" of
.br
	#define BOO hello,there
.br
	#define twopar(a,b) a b
.br
	twopar(BOO,folks)
.br
will generate
.br
	#define twopar(a,b) a b
.br
	twopar(hello,there,folks)
.br
causing an argument mismatch when the output is compiled.
.PP
Because uninterpreted "#if"s, "ifdef"s, and "ifndef"s, have no effect
on the output, the following example, when processed via "scpp -MLEFT",
will generate an error message complaining about
multiple definitions of "LEFT".
.br
	#ifdef ZULU
.br
	#define LEFT 20
.br
	#else
.br
	#define LEFT 347
.br
	#endif
.PP
The C preprocessor macros "\fB__FILE__\fP" and "\fB__LINE__\fP" have no
special meaning to
.B scpp.

----
Rich $alz	{mit-eddie, ihnp4, wjh12, cca, cbosgd, seismo}!mirror!rs
Mirror Systems	2067 Massachusetts Avenue  Cambridge, MA  02140
Telephone:	617-661-0777
"Hi, mom!"

bright@dataio.UUCP (07/20/86)

In article <323@enmasse.UUCP> keith@enmasse.UUCP (Keith Crews) writes:
>It often happens that software packages are set up to run on so many
>configurations that it is hard to read the code because of all the ifdefs.
>That is, filter out everything that is not going to be expanded by cpp while
>retaining the ifdefs that are going to be expanded, just for comment value.
>Ideally the output, when compiled, would produce exactly the same results as the
>original.  Does anyone have anything like this?

The Datalight C compiler does this if you use the -e and -l switches
on the command line.

brent@poseidon.UUCP (Brent P. Callaghan) (07/20/86)

> 
> It often happens that software packages are set up to run on so many
> configurations that it is hard to read the code because of all the ifdefs.
> I would like a filter for C programs that does something like the
> following:
> 
> Invoke it like this:
> 
> 	prog <x.c >x.c.new -DSYSV -D68000
> 
> That is, filter out everything that is not going to be expanded by cpp while
> retaining the ifdefs that are going to be expanded, just for comment value.
> Ideally the output, when compiled, would produce exactly the same results as the
> original.  Does anyone have anything like this?

How about something simple like the following shell/awk one-liner:

          awk "/# *ifdef.*$1/ , /#endif/ {print}" $2

Call it "ifdef" and invoke as:

	  ifdef 6800  x.c  >  x.c.new

-- 
				
Made in New Zealand -->		Brent Callaghan
				AT&T Information Systems, Lincroft, NJ
				{ihnp4|mtuxo|pegasus}!poseidon!brent
				(201) 576-3475

thomas@utah-gr.UUCP (07/20/86)

There is such a program in 4.3 (and maybe earlier, I can't say because
we had it from:), it was also part of the Rand editor and MH
distribution (ages ago).  It was written by Dave Yost, I don't know if
it is in the public domain.  It is called "unifdef".  Here is the man
page.  (Please don't bug me for copies, I can't send them.  If it turns
out to be PD, I could post it to mod.sources.)




UNIFDEF(1)          UNIX Programmer's Manual           UNIFDEF(1)



NAME
     unifdef - remove ifdef'ed lines

SYNOPSIS
     unifdef [ -t -l -c -Dsym -Usym -idsym -iusym ] ...  [ file ]

DESCRIPTION
     Unifdef is useful for removing ifdef'ed lines from a file
     while otherwise leaving the file alone.  Unifdef is like a
     stripped-down C preprocessor: it is smart enough to deal
     with the nested ifdefs, comments, single and double quotes
     of C syntax so that it can do its job, but it doesn't do any
     including or interpretation of macros.  Neither does it
     strip out comments, though it recognizes and ignores them.
     You specify which symbols you want defined -Dsym or unde-
     fined -Usym and the lines inside those ifdefs will be copied
     to the output or removed as appropriate.  The ifdef, ifndef,
     else, and endif lines associated with sym will also be
     removed.  Ifdefs involving symbols you don't specify are
     untouched and copied out along with their associated ifdef,
     else, and endif lines.  If an ifdef X occurs nested inside
     another ifdef X, then the inside ifdef is treated as if it
     were an unrecognized symbol.  If the same symbol appears in
     more than one argument, only the first occurrence is signi-
     ficant.

     The -l option causes unifdef to replace removed lines with
     blank lines instead of deleting them.

     If you use ifdefs to delimit non-C lines, such as comments
     or code which is under construction, then you must tell
     unifdef which symbols are used for that purpose so that it
     won't try to parse for quotes and comments in those ifdef'ed
     lines.  You specify that you want the lines inside certain
     ifdefs to be ignored but copied out with -idsym and -iusym
     similar to -Dsym and -Usym above.

     If you want to use unifdef for plain text (not C code), use
     the -t option.  This makes unifdef refrain from attempting
     to recognize comments and single and double quotes.

     Unifdef copies its output to stdout and will take its input
     from stdin if no file argument is given.  If the -c argument
     is specified, then the operation of unifdef is complemented,
     i.e. the lines that would have been removed or blanked are
     retained and vice versa.

SEE ALSO
     diff(1)

DIAGNOSTICS
     Premature EOF, inappropriate else or endif.



Printed 6/8/86           April 29, 1985                         1






UNIFDEF(1)          UNIX Programmer's Manual           UNIFDEF(1)



     Exit status is 0 if output is exact copy of input, 1 if not,
     2 if trouble.

BUGS
     Does not know how to deal with cpp consructs such as

          #if  defined(X) || defined(Y)


AUTHOR
     Dave Yost












































Printed 6/8/86           April 29, 1985                         2



-- 
=Spencer   ({ihnp4,decvax}!utah-cs!thomas, thomas@utah-cs.ARPA)

jimg@paisley.UUCP (07/21/86)

In article <924@cyb-eng.UUCP> bc@cyb-eng.UUCP writes:
>> I would like a filter for C programs that does something like the
>> following:
>> [...deleted specification...]
>> That is, filter out everything that is not going to be expanded by cpp while
>> retaining the ifdefs that are going to be expanded, just for comment value.
>> Ideally the output, when compiled, would produce exactly the same results as the
>> original.  Does anyone have anything like this?
>
>Me, too.  I keep planning to write it and never seem to find the time.

Can't you just run it through the C pre-processor?

narayan@wg3b20.UUCP (The Champ) (07/21/86)

> > It often happens that software packages are set up to run on so many
> > configurations that it is hard to read the code because of all the ifdefs.
> > I would like a filter for C programs that does something like the
> > following:
> > [...deleted specification...]
> > That is, filter out everything that is not going to be expanded by cpp while
> > retaining the ifdefs that are going to be expanded, just for comment value.
> > Ideally the output, when compiled, would produce exactly the same results as the
> > original.  Does anyone have anything like this?
> 
> Me, too.  I keep planning to write it and never seem to find the time.
> -- 
> bc				Bill Crews @ NetCor Data International
> 
> ..!{seismo,gatech,ihnp4}!ut-sally!cyb-eng!bc  (512) 835-2266

*** REPLACE THIS LINE WITH YOUR MESSAGE ***

There is a unifdef program from Rand in Public domain going
about that does this. It may be in 4.3 BSD
-- 

Narayan Mohanram

Phone:		415-962 7170
ARPANET		wg3b20!narayan@lll-tis-b.ARPA
Usenet		ihnp4!{amdahl,pesnta}!wg3b20!narayan
Mail		The Wollongong Group
		1129 San Antonio Road
		Palo Alto, CA 94303. USA



	=========================================================
	||	If you can't lick it, try some whipped cream	||
	=========================================================

dwl10@amdahl.UUCP (07/23/86)

I know of a GREAT way to filter out "un-executed" ifdefs. It's
called the C pre-processor! Just feed it your code, with a few
-Doptions to define which ifdefs you want defined, and presto-changeo,
it will spit out only the wanted source.
-- 
-------------------------------------------------------------------
                               Dave Lowrey

"So it goes, so it goes, so it goes, so it goes. But where it's
 going, nobody knows"   [Nick Lowe]
                                ...!{ihnp4,cbosgd,hplabs}!amdahl!dwl10

[ The opinions expressed <may> be those of the author and not necessarily
  those of his most eminent employer. ]

narayan@wg3b20.UUCP (The Champ) (07/24/86)

> I know of a GREAT way to filter out "un-executed" ifdefs. It's
> called the C pre-processor! Just feed it your code, with a few
> -Doptions to define which ifdefs you want defined, and presto-changeo,
> it will spit out only the wanted source.
> -- 
> -------------------------------------------------------------------
>                                Dave Lowrey
> 
You can't do this if you have sources that you would like to sell.
If you have sources that build stuff for several machines, you don't
want to give them all away. Here is where you unifdef what you
don't want to give away. Also ccp pulls in include files, and 
expands #defines. How would you like AT&T to give you sources
with no include files or only octal and hex numbers for all the
masks and parameters.




-- 

Narayan Mohanram

Phone:		415-962 7170
ARPANET		wg3b20!narayan@lll-tis-b.ARPA
Usenet		ihnp4!{amdahl,pesnta}!wg3b20!narayan
Mail		The Wollongong Group
		1129 San Antonio Road
		Palo Alto, CA 94303. USA



	=========================================================
	||	If you can't lick it, try some whipped cream	||
	=========================================================

jrw@hropus.UUCP (Jim Webb) (07/24/86)

> I know of a GREAT way to filter out "un-executed" ifdefs. It's
> called the C pre-processor! Just feed it your code, with a few
> -Doptions to define which ifdefs you want defined, and presto-changeo,
> it will spit out only the wanted source.

I laugh!  This approach will also expand all the #defines that you use,
making the code quite unreadable, especially if you #include <stdio.h>
-- 
Jim Webb           "Out of phase--get help"         ihnp4!houxm!hropus!jrw