Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.unix.programmer > #5175

Re: Which content should be escaped?

From Kaz Kylheku <kaz@kylheku.com>
Newsgroups comp.unix.programmer
Subject Re: Which content should be escaped?
Date 2014-03-04 17:32 +0000
Organization Aioe.org NNTP Server
Message-ID <20140304090937.345@kylheku.com> (permalink)
References (1 earlier) <20140302095342.219@kylheku.com> <531381A4.1020704@web.de> <20140302122038.693@kylheku.com> <bnj0e3Fft17U1@mid.individual.net> <c43huaxm12.ln2@perseus.wenlock-data.co.uk>

Show all headers | View raw


On 2014-03-04, Dave Sines <dave.gma+news002@googlemail.com.invalid> wrote:
> Markus Elfring <Markus.Elfring@web.de> wrote:
>>> We have to trade one form escaping instead of another, but the
>>> escaping we have to do now is in the body of the static template, to
>>> ensure it is a valid here-doc, not in the variable being substituted,
>>> which has "wild and wooly" run-time contents.
>>>
>>> The template is more "static" relative to the contents of the wild
>>> and wooly variable being substituted. We can just write the template
>>> correctly by hand, and that's it.
>> 
>> I would agree to your advice in the general situation. The simple test
>> data I started the clarification with fits to your view.
>>
>> But I find that the real template files have got such constraints that
>> the here-document rules are not useful there.
>
> Have you considered using cpp?

You have to be careful, because the C preprocessor decomposes the input
into C preprocessor tokens. (I can't think of a good example of how that
will screw up, off the top of my head.) It also assembles line continuations,
strips comments, and reacts to lines that begin with #.  You are severely
limited in what constitutes a macro name.

> template1.txt:
>
>   +++++++++++++
>   #include REPL
>   *************
>
> replacement1.txt:
>
>   ABC
>   XYZ
>
>   cpp -P -nostdinc -xc -DREPL="\"replacement1.txt\"" template1.txt

I once had a Makefile for generating text documents from templates, where
three suffixes were supported: .ct (C preprocessor template), .st (shell template) and .mt 
(m4 template).

For each one, the build rule would generate a command line with all
the variable definitions for the given language (cpp, shell here document
or m4) and any necessary wrapping (for the here doc).

In that project, I used the appopriate processor based on the situation.

I managed to find this. The relevant parts, which use GNU Make syntax,
are:

%: %.ct
        cpp -P $(shell env | sed -e "s/.*/-D_ENV_'&'/") $< > $@

%: %.st
        ( export __FILE__=$< ; echo "cat <<!" ; cat $< ; echo "!" ) | bash > $@

%: %.mt
        rm -f $@.err
        m4 -D__FILE__=$< $(shell env | sed -e "s/\([^=]*\)=\(.*\)/-D'_ENV_\\1=\\2'/") $< > $@ 2> $@.err
        if [ -s $@.err ] ; then cat $@.err ; exit 1 ; fi
        rm -f $@.err


In .ct and .mt files you can refer to environment variables as _ENV_<name>.

So for instance if we have a foo.st file containing

  the TERM variable is $TERM

and a bar.ct file containing 

  the TERM variable is __ENV_TERM

we can do "make foo bar" to create a foo and bar which contain, perhaps:

  the TERM variable is xterm

or whatever the value.

Back to comp.unix.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Escaping of line breaks for text replacement Markus Elfring <Markus.Elfring@web.de> - 2014-03-02 14:00 +0100
  Re: Escaping of line breaks for text replacement jt@toerring.de (Jens Thoms Toerring) - 2014-03-02 15:49 +0000
    Re: Escaping of line breaks for text replacement Markus Elfring <Markus.Elfring@web.de> - 2014-03-03 08:38 +0100
  Re: Escaping of line breaks for text replacement dave.gma+news002@googlemail.com.invalid (Dave Sines) - 2014-03-02 16:52 +0000
    Re: Escaping of line breaks for text replacement Kaz Kylheku <kaz@kylheku.com> - 2014-03-02 17:53 +0000
      Re: Escaping of line breaks for text replacement Kaz Kylheku <kaz@kylheku.com> - 2014-03-02 20:27 +0000
        Re: Escaping of line breaks for text replacement dave.gma+news002@googlemail.com.invalid (Dave Sines) - 2014-03-03 16:17 +0000
          Re: Escaping of line breaks for text replacement Kaz Kylheku <kaz@kylheku.com> - 2014-03-03 17:44 +0000
            Re: Escaping of line breaks for text replacement Geoff Clare <geoff@clare.See-My-Signature.invalid> - 2014-03-04 13:30 +0000
    Re: Escaping of line breaks for text replacement Markus Elfring <Markus.Elfring@web.de> - 2014-03-02 20:50 +0100
      Re: Escaping of line breaks for text replacement dave.gma+news002@googlemail.com.invalid (Dave Sines) - 2014-03-03 16:52 +0000
    Re: Escaping of line breaks for text replacement Markus Elfring <Markus.Elfring@web.de> - 2014-03-03 09:24 +0100
    Re: Escaping of line breaks for text replacement Markus Elfring <Markus.Elfring@web.de> - 2014-03-03 12:55 +0100
      Re: Escaping of line breaks for text replacement dave.gma+news002@googlemail.com.invalid (Dave Sines) - 2014-03-03 17:02 +0000
        Re: Escaping of line breaks for text replacement Markus Elfring <Markus.Elfring@web.de> - 2014-03-06 12:02 +0100
          Re: Escaping of line breaks for text replacement dave.gma+news002@googlemail.com.invalid (Dave Sines) - 2014-03-06 18:51 +0000
            Re: Escaping of line breaks for text replacement Markus Elfring <Markus.Elfring@web.de> - 2014-03-06 20:48 +0100
              Re: Escaping of line breaks for text replacement dave.gma+news002@googlemail.com.invalid (Dave Sines) - 2014-03-06 22:59 +0000
                Re: Escaping of line breaks for text replacement Kaz Kylheku <kaz@kylheku.com> - 2014-03-07 06:38 +0000
                Re: Escaping of line breaks for text replacement Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-03-07 13:33 +0000
                Re: Escaping of line breaks for text replacement dave.gma+news002@googlemail.com.invalid (Dave Sines) - 2014-03-07 15:08 +0000
                Re: Escaping of line breaks for text replacement Markus Elfring <Markus.Elfring@web.de> - 2014-03-07 11:23 +0100
                Re: Escaping of line breaks for text replacement dave.gma+news002@googlemail.com.invalid (Dave Sines) - 2014-03-07 14:43 +0000
                Re: Escaping of line breaks for text replacement Markus Elfring <Markus.Elfring@web.de> - 2014-03-07 17:23 +0100
                Re: Escaping of line breaks for text replacement dave.gma+news002@googlemail.com.invalid (Dave Sines) - 2014-03-07 19:53 +0000
                Bash 4.3.0 escape expansion (was: Re: Escaping of line breaks for text replacement) dave.gma+news002@googlemail.com.invalid (Dave Sines) - 2014-03-08 12:26 +0000
                Re: Bash 4.3.0 escape expansion (was: Re: Escaping of line breaks for text replacement) Kaz Kylheku <kaz@kylheku.com> - 2014-03-08 20:05 +0000
                Re: Bash 4.3.0 escape expansion Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-03-09 03:48 +0000
                Re: Bash 4.3.0 escape expansion dave.gma+news002@googlemail.com.invalid (Dave Sines) - 2014-03-09 13:21 +0000
  Re: Escaping of line breaks for text replacement Kaz Kylheku <kaz@kylheku.com> - 2014-03-02 18:11 +0000
    Re: Escaping of line breaks for text replacement Markus Elfring <Markus.Elfring@web.de> - 2014-03-02 20:08 +0100
      Re: Escaping of line breaks for text replacement Kaz Kylheku <kaz@kylheku.com> - 2014-03-02 20:35 +0000
        Re: Which content should be escaped? Markus Elfring <Markus.Elfring@web.de> - 2014-03-03 10:30 +0100
          Re: Which content should be escaped? dave.gma+news002@googlemail.com.invalid (Dave Sines) - 2014-03-04 14:04 +0000
            Re: Which content should be escaped? Kaz Kylheku <kaz@kylheku.com> - 2014-03-04 17:32 +0000
  Re: Escaping of line breaks for text replacement Siri Cruz <chine.bleu@yahoo.com> - 2014-03-07 02:47 -0800

csiph-web