Line break in preprocessor output

Discussion in 'C Programming' started by John Speth, May 29, 2007.

  1. John Speth

    John Speth Guest

    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.
    John Speth, May 29, 2007
    #1
    1. Advertising

  2. John Speth

    Ben Pfaff Guest

    "John Speth" <> writes:

    > 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.
    --
    int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.\
    \n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
    );while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p\
    );}return 0;}
    Ben Pfaff, May 29, 2007
    #2
    1. Advertising

  3. John Speth

    ais523 Guest

    On May 29, 5:23 pm, "John Speth" <> wrote:
    > 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.
    --
    ais523
    ais523, May 29, 2007
    #3
  4. John Speth wrote:
    > 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.
    Martin Ambuhl, May 29, 2007
    #4
  5. John Speth

    David Tiktin Guest

    On 29 May 2007, "John Speth" <> wrote:

    > 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

    --
    D.a.v.i.d T.i.k.t.i.n
    t.i.k.t.i.n [at] a.d.v.a.n.c.e.d.r.e.l.a.y [dot] c.o.m
    David Tiktin, May 29, 2007
    #5
  6. John Speth

    John Speth Guest

    "David Tiktin" <-bogus.com> wrote in message
    news:Xns993F77B3CA75Adtiktinnospambogusco@216.196.97.136...
    > On 29 May 2007, "John Speth" <> wrote:
    >
    >> 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.


    (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
    John Speth, May 29, 2007
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    2,671
  2. Cronus
    Replies:
    1
    Views:
    667
    Paul Mensonides
    Jul 15, 2004
  3. Vittal

    How reliable preprocessor output is?

    Vittal, Jul 19, 2003, in forum: C Programming
    Replies:
    5
    Views:
    333
    Gordon Burditt
    Jul 19, 2003
  4. Replies:
    32
    Views:
    873
    =?utf-8?B?SGFyYWxkIHZhbiBExLNr?=
    Jun 28, 2006
  5. Replies:
    12
    Views:
    959
Loading...

Share This Page