Line break in preprocessor output

J

John Speth

Hi Group-

I want to use the C preprocessor to generate expanded text as a text
processor for software test script generation. The preprocessor output will
never be compiled. I need to insert newlines in the preprocessed output
file but I can't figure out how to do it.

Here's a stab at what I'm trying to do but failing:

#define CMD(s) text s \
enter \
sleep 500

CMD(mmem:mdir "a1")
CMD(mmem:mdir "a2")

I'd like to output a literal newline for every macro line continuation
character ('\'). I know my example won't work but it's the closest I've
been able to get to actually get the output that I'd like to get, which is:

text mmem:mdir "a1"
enter
sleep 500
text mmem:mdir "a2"
enter
sleep 500

What I get instead is:

text mmem:mdir "a1" enter sleep 500
text mmem:mdir "a2" enter sleep 500

The desired newlines are missing.

Does anyone know how to get the C proeprocessor to output a literal newline
using macros?

Thanks, John.
 
B

Ben Pfaff

John Speth said:
Does anyone know how to get the C proeprocessor to output a literal newline
using macros?

I don't think that there is a way to do it. You might consider
designating a character sequence to represent a new-line
character and then postprocess the C preprocessor's output with
another program that can replace that sequence with a new-line.
 
A

ais523

Hi Group-

I want to use the C preprocessor to generate expanded text as a text
processor for software test script generation. The preprocessor output will
never be compiled. I need to insert newlines in the preprocessed output
file but I can't figure out how to do it.

Here's a stab at what I'm trying to do but failing:

#define CMD(s) text s \
enter \
sleep 500

CMD(mmem:mdir "a1")
CMD(mmem:mdir "a2")

I'd like to output a literal newline for every macro line continuation
character ('\'). I know my example won't work but it's the closest I've
been able to get to actually get the output that I'd like to get, which is:

text mmem:mdir "a1"
enter
sleep 500
text mmem:mdir "a2"
enter
sleep 500

What I get instead is:

text mmem:mdir "a1" enter sleep 500
text mmem:mdir "a2" enter sleep 500

The desired newlines are missing.

Does anyone know how to get the C proeprocessor to output a literal newline
using macros?

Thanks, John.

With some preprocessors, it's possible that the sequence of characters
\u000A will expand into a newline during preprocessing (this is almost
legal C99 syntax, but the standard specifically says that this sort of
thing isn't allowed (6.4.3:2 in N1124.pdf)). (I haven't tested, but it
seems reasonable that at least one preprocessor will get confused into
emitting a newline in this situation; the #defines would be
substituted in stage 4, before the universal character names are even
considered by the compiler.) One point of interest is that universal
character names are one C99 feature that I've actually used in
practice.
 
M

Martin Ambuhl

John said:
Hi Group-

I want to use the C preprocessor to generate expanded text as a text
processor for software test script generation. The preprocessor output will
never be compiled. I need to insert newlines in the preprocessed output
file but I can't figure out how to do it.

The C preprocessor is probably the wrong tool. You might look into a
program designed as a stand-alone macro language, like the old, I know,
but trusty M4.
 
D

David Tiktin

I want to use the C preprocessor to generate expanded text as a
text processor for software test script generation. The
preprocessor output will never be compiled. I need to insert
newlines in the preprocessed output file but I can't figure out
how to do it.

Like others who have responded, I don't think there's a way to do this
portably with the C preprocessor. But could you explain why you chose
that particular tool? Why not use a "real" macro processor like m4?

http://en.wikipedia.org/wiki/M4_(computer_language)

Since it's usually written in C, you should be able to build it for
just about any platform.

Dave
 
J

John Speth

David Tiktin said:
Like others who have responded, I don't think there's a way to do this
portably with the C preprocessor. But could you explain why you chose
that particular tool? Why not use a "real" macro processor like m4?

http://en.wikipedia.org/wiki/M4_(computer_language)

Since it's usually written in C, you should be able to build it for
just about any platform.

(I'm the OP) I chose the C preprocessor because it seemed like a capable
tool for the job AND it was at my fingertips. I'm looking for instant
gratification. M4 looks like it'll work but now I have to spend an hour or
two learning it.

It appears simply outputting a newline is just plain not possible using the
C proeprocessor. It's such a simple thing too :(

Thanks to everyone for your help and suggestions.

JJS
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top