DEAR PEOPLE FROM THE FUTURE: Here's what we've figured out so far...

Welcome! This is a Q&A website for computer programmers and users alike, focused on helping fellow programmers and users. Read more

What are you stuck on? Ask a question and hopefully somebody will be able to help you out!
+2 votes

I am using gpp as a preprocessor in HTML mode. I have a simple input file:

This is the input file.
<#welcome Hello>
End of file

and a set of macros defined in macros.gpp

<#define welcome|Hello, you!>

<#ifdef FOO>
	<#define welcome|Hello, Fool!>
<#endif>
<#ifdef BAR>
	<#define welcome|Hello, Bart!>
<#endif>

I call the preprocessor like this:

$ gpp -H --include macros.gpp -o output.txt -DFOO input.txt

The output file looks like this and has serveral leading empty lines:

(empty line)
(empty line)
(empty line)
(empty line)	
(empty line)
This is the input file.
Hello, Fool!
End of file

I assume those come from the macro definitions. How can I prevent those new lines?
Or can I prevent the macro definitions pre-pended to the input data?

by

1 Answer

+1 vote

The --include option opens the given file, evaluates it, and then adds the resulting text to the output.
In the man page, the HTML-like mode is defined like this

-U "<#" ">" "\B" "|" ">" "<" ">" "#" "\\"

The -U parameter takes 9 arguments, also described by the man page

-U arg1 ... arg9
        User-defined mode. The nine following command-line arguments are taken to be respectively the macro start
        sequence,  the macro end sequence for a call without arguments, the argument start sequence, the argument
        separator, the argument end sequence, the list of characters to stack for argument balancing, the list of
        characters  to  unstack,  the  string  to be used for referring to an argument by number, and finally the
        quote character (if there is none an empty string should be provided).  These settings apply both to user
        macros and to meta-macros, unless the -M option is used to define other settings for meta-macros. See the
        section on syntax specification for more details.

What I understand from the manual is that no, you cannot avoid the empty lines using the HTML-like mode. I also couldn't find any way to exclude the included file from the output. What you could do however is define your own "user mode", copying HTML mode (note the added \n):

-U "<#" ">\n" "\B" "|" ">\n" "<" ">" "#" "\\"

This mode will consume an extra \n symbol when parsing, which will not be in the final output. But I think this introduces some limitations compared to the default HTML mode because of the extra \n symbol.

Alternatively you could define a custom delimited in the macros file, like this:

START
<#ifdef FOO>
<#define welcome|Hello, Fool!>
<#endif>
END

and after gpp has created the final output, you remove only the output from the macros file:

gpp -H --include macros.gpp -DFOO input.txt | perl -p0e "s/START.*END\n//s"
by
0

The macros can appear within the text (I did not provided an example for that) so using \n as part of the delimiter is not a option. I guess using sed or awk to remove the leading white space is the only option then.

0

Yeah, that looks like your only option. However I think START...END is better than removing all the leading white spaces because when you sed/awk/perl the whole output, you're also going to remove the spaces from input.txt (which you probably don't want to).

Contributions licensed under CC0
...