473,659 Members | 2,872 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Utility to partially preprocess

I have a requirement to partially preprocess C code. By 'partially' I
mean that I need to define some macros (to the utility) and have it
preprocess only those macros, leaving everything else intact. A
compiler's preprocessor won't do this, since it will also process macros
defined in the code (and in standard header files) and strip out
everything. For instance, given source like:

#include <stdio.h>
#if defined(AAA)
void doSomeCode(void )
{
# if AAA > 1
printf("do something %d\n", AAA);
# else
printf("do something else\n");
# endif
}
#else
#include <math.h>
#define PI (4 * atan(1.0))
void doSomeCode(void )
{
printf("somethi ng completely different - PI = %f\n", PI);
}
#endif

I would want the output to be:

AAA undefined:

#include <stdio.h>
#include <math.h>
#define PI (4 * atan(1.0))
void doSomeCode(void )
{
printf("somethi ng completely different - PI = %f\n", PI);
}

AAA = 0 or 1

#include <stdio.h>
void doSomeCode(void )
{
printf("do something else\n");
}

AAA = 2

#include <stdio.h>
void doSomeCode(void )
{
printf("do something %d\n", 2);
}

OK, it's a silly example, but the idea should show. Note that using the
C preprocessor would have expanded the header files, which is not
wanted...

One purpose is to take some code which has got horribly crufty, with
lots of specific conditional compilation for things which no longer
exist (in particular things which were needed for pre-standard and
non-standard compilers), and reduce it for a simpler case. Yes, this
can be done "by hand" but this is prone to error and very time-consuming
with a large amount of code.

Another purpose is as a general utility for pre-parsing header and code
files for distribution, so that the destination system does not have to
include "config.h" (or equivalent) every time.

My question is:

Does such a utility exist as Free Software? Licence is unimportant as
long as it is compatible with other Free Software -- (L)GPL, BSD and
'Infozip' licences particularly. Source code as Standard C so it will
compile anywhere (so can't depend on yacc or lex, unless the generated
code from them is Standard C).

If not, I need to write my own. If it does exist already, I would
rather not re-invent the wheel...

Chris C
Nov 14 '05 #1
11 3347
In article <sl************ ******@ccserver .keris.net>
Chris Croughton <ch***@keristor .net> wrote:
I have a requirement to partially preprocess C code. By 'partially' I
mean that I need to define some macros (to the utility) and have it
preprocess only those macros, leaving everything else intact. ...


If you only want certain #ifdef's taken out, the "unifdef" utility
(of which there are no doubt multiple versions) will do the trick.
For something more complicated, look for the "scpp" program -- the
"selective C preprocessor", written some time in the 1980s in
pre-ANSI C.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 14 '05 #2
On 1 Nov 2004 19:34:11 GMT, Chris Torek
<no****@torek.n et> wrote:
In article <sl************ ******@ccserver .keris.net>
Chris Croughton <ch***@keristor .net> wrote:
I have a requirement to partially preprocess C code. By 'partially' I
mean that I need to define some macros (to the utility) and have it
preprocess only those macros, leaving everything else intact. ...


If you only want certain #ifdef's taken out, the "unifdef" utility
(of which there are no doubt multiple versions) will do the trick.
For something more complicated, look for the "scpp" program -- the
"selective C preprocessor", written some time in the 1980s in
pre-ANSI C.


Thanks, the man for scpp sounds like about what I want, but the only
copies (3 of them) I could find won't compile (lots of lex and
lex-generated code errors). I gather from searches that I'm not the
only one to find that with 'modern' utilities (I'm using Debian
GNU/Linux 'woody' (stable), gcc 2.95.4, lex 2.5.4, yacc (GNU bison)
1.35).

However, reading about it has given me ideas for writing my own. It's
been quite a long time since I wrote a C parser in pure C, the last time
was pre-ANSI, probably about the same time as scpp, but a limited
pre-processor is not too bad (only half of the operators are valid, and
I don't need to worry about program structure at all).

Any recommendations for (non-gcc) C compilers to test compatibility?

Chris C
Nov 14 '05 #3
Chris Croughton wrote:
I have a requirement to partially preprocess C code. By 'partially' I
mean that I need to define some macros (to the utility) and have it
preprocess only those macros, leaving everything else intact. A
compiler's preprocessor won't do this, since it will also process macros
defined in the code (and in standard header files) and strip out
everything. For instance, given source like:

#include <stdio.h>
#if defined(AAA)
void doSomeCode(void )
{
# if AAA > 1
printf("do something %d\n", AAA);
# else
printf("do something else\n");
# endif
}
#else
#include <math.h>
#define PI (4 * atan(1.0))
void doSomeCode(void )
{
printf("somethi ng completely different - PI = %f\n", PI);
}
#endif

I would want the output to be:

AAA undefined:

#include <stdio.h>
#include <math.h>
#define PI (4 * atan(1.0))
void doSomeCode(void )
{
printf("somethi ng completely different - PI = %f\n", PI);
}

AAA = 0 or 1

#include <stdio.h>
void doSomeCode(void )
{
printf("do something else\n");
}

AAA = 2

#include <stdio.h>
void doSomeCode(void )
{
printf("do something %d\n", 2);
}

OK, it's a silly example, but the idea should show. Note that using the
C preprocessor would have expanded the header files, which is not
wanted...

One purpose is to take some code which has got horribly crufty, with
lots of specific conditional compilation for things which no longer
exist (in particular things which were needed for pre-standard and
non-standard compilers), and reduce it for a simpler case. Yes, this
can be done "by hand" but this is prone to error and very time-consuming
with a large amount of code.

Another purpose is as a general utility for pre-parsing header and code
files for distribution, so that the destination system does not have to
include "config.h" (or equivalent) every time.

My question is:

Does such a utility exist as Free Software? Licence is unimportant as
long as it is compatible with other Free Software -- (L)GPL, BSD and
'Infozip' licences particularly. Source code as Standard C so it will
compile anywhere (so can't depend on yacc or lex, unless the generated
code from them is Standard C).

If not, I need to write my own. If it does exist already, I would
rather not re-invent the wheel...

Chris C


Using the lcc-win32 compiler system you write:

browsegen -showifdeflines foo.c

and it will write in the standard output the line number
of the lines #ifdefed out in the file foo.c.
This allows the IDE to show ifdefed lines in gray color,
but you can use the output as you want, of course.

http://www.cs.virginia.edu/~lcc-win32
Nov 14 '05 #4
On Wed, 03 Nov 2004 00:51:22 +0100, jacob navia
<ja***@jacob.re mcomp.fr> wrote:
Chris Croughton wrote:

My question is:

Does such a utility exist as Free Software? Licence is unimportant as
long as it is compatible with other Free Software -- (L)GPL, BSD and
'Infozip' licences particularly. Source code as Standard C so it will
compile anywhere (so can't depend on yacc or lex, unless the generated
code from them is Standard C).
Using the lcc-win32 compiler system you write:

browsegen -showifdeflines foo.c


And this compiler is:

Free Software?
Standard C source code which will "compile anywhere"?

Er, not as far as I can see from that web page. It seems to be Win32
binary only.
and it will write in the standard output the line number
of the lines #ifdefed out in the file foo.c.
This allows the IDE to show ifdefed lines in gray color,
but you can use the output as you want, of course.
Very useful, indeed. But presumably it also notices macros in header
files and in the code when it decides what is #ifdefed out as well (i.e.
lines which it ignores when compiling), which is not useful in my case
(I need it to only notice the ones I specify).
http://www.cs.virginia.edu/~lcc-win32


I get "530 Sorry, the maximum number of allowed clients (30) are already
connected" on all of the links -- except the one to the mirror site at Q
Software Solutions (http://www.q-software-solutions.com/lccwin32) which
gives 404 "The requested URL /lccwin32 was not found on this server."

Chris C
Nov 14 '05 #5
Chris Croughton wrote:
On 1 Nov 2004 19:34:11 GMT, Chris Torek
Chris Croughton <ch***@keristor .net> wrote:

I have a requirement to partially preprocess C code. By 'partially'
I mean that I need to define some macros (to the utility) and have
it preprocess only those macros, leaving everything else intact. ...


If you only want certain #ifdef's taken out, the "unifdef" utility
(of which there are no doubt multiple versions) will do the trick.
For something more complicated, look for the "scpp" program -- the
"selective C preprocessor", written some time in the 1980s in
pre-ANSI C.


Thanks, the man for scpp sounds like about what I want, but the only
copies (3 of them) I could find won't compile (lots of lex and
lex-generated code errors). I gather from searches that I'm not the
only one to find that with 'modern' utilities (I'm using Debian
GNU/Linux 'woody' (stable), gcc 2.95.4, lex 2.5.4, yacc (GNU bison)
1.35).


I couldn't find any scpp source via google. Where did you locate
them?

--
Chuck F (cb********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!
Nov 14 '05 #6
Chris Croughton wrote:
On Wed, 03 Nov 2004 00:51:22 +0100, jacob navia
<ja***@jacob.re mcomp.fr> wrote:

Chris Croughton wrote:
My question is:

Does such a utility exist as Free Software? Licence is unimportant as
long as it is compatible with other Free Software -- (L)GPL, BSD and
'Infozip' licences particularly. Source code as Standard C so it will
compile anywhere (so can't depend on yacc or lex, unless the generated
code from them is Standard C).
Using the lcc-win32 compiler system you write:

browsegen -showifdeflines foo.c

And this compiler is:

Free Software?
Standard C source code which will "compile anywhere"?

Er, not as far as I can see from that web page. It seems to be Win32
binary only.


It is free software as far as you do not have to pay anything to
use it.

lcc-win32 runs only in the win32 susbsystem of windows.
and it will write in the standard output the line number
of the lines #ifdefed out in the file foo.c.
This allows the IDE to show ifdefed lines in gray color,
but you can use the output as you want, of course.

Very useful, indeed. But presumably it also notices macros in header
files and in the code when it decides what is #ifdefed out as well (i.e.
lines which it ignores when compiling), which is not useful in my case
(I need it to only notice the ones I specify).


It will follow included files of course, and accept command line
parameters like
browsegen -DsomeMacro=SOME _MACRO
or similar

If you do not want it to follow any includes, just put your
source in a directory where none of them will be found...
VERY EASY :-)

http://www.cs.virginia.edu/~lcc-win32

I get "530 Sorry, the maximum number of allowed clients (30) are already
connected" on all of the links -- except the one to the mirror site at Q
Software Solutions (http://www.q-software-solutions.com/lccwin32) which
gives 404 "The requested URL /lccwin32 was not found on this server."

Chris C


Sorry but the compiler is very popular and there are a lot of people
downloading.

jacob
Nov 14 '05 #7
CBFalconer wrote:
Chris Croughton wrote:
On 1 Nov 2004 19:34:11 GMT, Chris Torek
Chris Croughton <ch***@keristor .net> wrote:

I have a requirement to partially preprocess C code. By 'partially'
I mean that I need to define some macros (to the utility) and have
it preprocess only those macros, leaving everything else intact. ...

If you only want certain #ifdef's taken out, the "unifdef" utility
(of which there are no doubt multiple versions) will do the trick.
For something more complicated, look for the "scpp" program -- the
"selective C preprocessor", written some time in the 1980s in
pre-ANSI C.


Thanks, the man for scpp sounds like about what I want, but the only
copies (3 of them) I could find won't compile (lots of lex and
lex-generated code errors). I gather from searches that I'm not the
only one to find that with 'modern' utilities (I'm using Debian
GNU/Linux 'woody' (stable), gcc 2.95.4, lex 2.5.4, yacc (GNU bison)
1.35).


I couldn't find any scpp source via google. Where did you locate
them?


Just try

http://www.ibiblio.org/pub/Linux/dev...c/scpp-0.1.tgz

Nov 14 '05 #8
On Wed, 03 Nov 2004 20:14:21 +0100, Danilo Kempf
<us****@nullpoi nter.de> wrote:
CBFalconer wrote:
Chris Croughton wrote:
On 1 Nov 2004 19:34:11 GMT, Chris Torek
Chris Croughton <ch***@keristor .net> wrote:

> I have a requirement to partially preprocess C code. By 'partially'
> I mean that I need to define some macros (to the utility) and have
> it preprocess only those macros, leaving everything else intact. ...

If you only want certain #ifdef's taken out, the "unifdef" utility
(of which there are no doubt multiple versions) will do the trick.
For something more complicated, look for the "scpp" program -- the
"selective C preprocessor", written some time in the 1980s in
pre-ANSI C.

Thanks, the man for scpp sounds like about what I want, but the only
copies (3 of them) I could find won't compile (lots of lex and
lex-generated code errors). I gather from searches that I'm not the
only one to find that with 'modern' utilities (I'm using Debian
GNU/Linux 'woody' (stable), gcc 2.95.4, lex 2.5.4, yacc (GNU bison)
1.35).


I couldn't find any scpp source via google. Where did you locate
them?


Just try

http://www.ibiblio.org/pub/Linux/dev...c/scpp-0.1.tgz


That was (I think) the first one I tried. Also:

ftp://ftp.uu.net/usenet/comp.sources.unix/volume3/scpp
(two shar files from Usenet, possibly the original version)

ftp://isgate.is/pub/unix/sec1/scpp.tar.Z

http://ftp.unicamp.br/pub/unix-c/lan.../c/scpp.tar.gz

I searched google using: '"selective C preprocessor" scpp' (just using
'scpp' turns up a mass of unrelated things, organisations using those
initials etc.).

All seem to have the same problem, they find a lex rule which doesn't
work (I don't remember enough lex to see why) and the compilation finds
a load of undefined variables.

I haven't tried them using Unix 7 under the PDP-11 emulator, though...

I'm making progress on my own version. It can read files and tokenise
preprocessor lines (and gets the interaction with comments and
continuation lines correct), and has a database for the preprocessor
variables. Next is to replace tokens by the defined macros, then comes
the expression parsing...

Chris C
Nov 14 '05 #9
Chris Croughton <ch***@keristor .net> writes:
All seem to have the same problem, they find a lex rule which doesn't
work (I don't remember enough lex to see why) and the compilation finds
a load of undefined variables.


If you're using flex, use 2.5.4a or older with old software.
Newer versions are somewhat incompatible.
--
Here's a tip: null pointers don't have to be *dull* pointers!
Nov 14 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
5081
by: Sylvain Donnet | last post by:
Hi, My question is probably entirely described in the subject... I need to preprocess a PHP file before it enters the PHP parser. My goal is to change some non-PHP parts of text into PHP orders. I see in PHP doc an output_handler to postprocess the output. And I see that there is no input_handler. Does anybody have a clue, a solution ? I am also looking for a solution outside PHP, in some Apache modules (for instance mod_transform,...
1
2609
by: geradeaus | last post by:
Even if you upload an image partially with ftp software, you can still read the image. Only the image will be partially gray. So, can I determine with a php-function if an image is fully uploaded on the server and without checking the filesize using an interval in time? To be completely clear : this isn't about http-upload, but about uploaded files with external ftp-software. Thanks in advance!
3
1913
by: Wolfgang | last post by:
Is it possible in C# .NET to create a window that is always on top of all other windows, while at the same time being partially transparent? If not in C# .NET, how about in C++ .NET? As an example that this kind of thing is possible, the splash screen of Adobe Reader (formely Acrobat) seems to be partially transparent. If you have a code snippet of how something like this could be done, that would be great.
2
7824
by: Benny Raymond | last post by:
I need to be able to process the message WM_MOVING and WM_MOVE, however I noticed that the following breakpoint in PreProcessMessage isn't hit on move (it does get hit when I press alt for instance though). It does get hit on the WndProc, however I can't return a true here, so it's worthless for me... Any help would be awesome: ==== start code ==== public const int WM_MOVING = 0x0216; public const int WM_MOVE = 0x0003; protected...
1
5940
by: huyuhui | last post by:
The following is a question of LOAD utility. Question: How does the DB2 enforce table check constraints for data added to table with the LOAD utility? A. With the BUILD phase of LOAD B. With the SET INTEGRITY statement C. With the DELETE phase of the LOAD D. With the UPDATE CONSTRAINTS statement Answer is A
22
2482
by: David Mathog | last post by:
One thing that keeps coming up in this forum is that standard C lacks many functions which are required in a workstation or server but not possible in an embedded controller. This results in a plethora of "don't ask here, ask in comp.x.y instead", for queries on functions that from the documentation available to the programmer appear to be part of the C language. I think largely because of this "least common denominator" C language...
1
2197
by: efinzel | last post by:
Good morning- I am brand new to any kind of programming language. I am only attempting it because I want to use the Generic Mapping Tools program (http://gmt.soest.hawaii.edu/) to create maps. I have a lot of Arc data that I want to convert to use in GMT. I found a file, shp2gmt (http://169.237.35.250/~dylan/grass_user_group/map1.html), that I can use to do this. However, the shp2gmt.c file needs to be preprocessed before it can be...
8
2430
by: Rahul | last post by:
Hi, Is there a way to partially specialize only a member function of a template class (not the whole class). e.g. template <typename A, typename B> class Base { public:
1
2294
by: =?iso-8859-1?Q?David_S=E1nchez_Mart=EDn?= | last post by:
Hi! I've seen the message below in this python list, that seems to be unanswered. I'm trying to do the pretty same thing. There's a way to preprocess the request with a mod_python handler and then proxying it with mod_proxy?
0
8427
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8332
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8746
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8627
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7356
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6179
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4175
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
1737
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.