9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
* [9fans] late to the party - Here docs
@ 2007-11-06 20:31 maht-9fans
  2007-11-06 21:07 ` erik quanstrom
  2007-11-06 21:54 ` arisawa
  0 siblings, 2 replies; 12+ messages in thread
From: maht-9fans @ 2007-11-06 20:31 UTC (permalink / raw)
  To: 9fans

hi,

I've not been able to post for a while (verifying sender didn't like 
me), so I've got a few things stored up

I think it was eric that said the only place here docs are used in plan9 
default is in /sys/lib/newuser and there whoever wrote it went through 
some shenanigans to get $ in the output  (though this is snipped):

x='$'
cat > lib/profile <<!
bind -a $x^home/bin/rc /bin
!

However, what that person seems not to know is that they could have done $$ to escape the $ :

cat > lib/profile <<!

bind -a $$home/bin/rc /bin
!

I like here documents, I think they look better than multi-line echoes though it is annoying they can't be used in fn definitions.

Matt











^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [9fans] late to the party - Here docs
  2007-11-06 20:31 [9fans] late to the party - Here docs maht-9fans
@ 2007-11-06 21:07 ` erik quanstrom
  2007-11-07 16:41   ` dave.l
  2007-11-06 21:54 ` arisawa
  1 sibling, 1 reply; 12+ messages in thread
From: erik quanstrom @ 2007-11-06 21:07 UTC (permalink / raw)
  To: 9fans

> I like here documents, I think they look better than multi-line echoes though it is annoying they can't be used in fn definitions.

i was thinking about this topic again the other day during an
idle moment.  here documents are part of the parsing routine.
but they don't end up in rc's machine language.  this is different
than any other construct in rc, and the reason they don't work
in functions.

they really are anomolous critters.  they need to be taken out
back and shot.  ☺

- erik


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [9fans] late to the party - Here docs
  2007-11-06 20:31 [9fans] late to the party - Here docs maht-9fans
  2007-11-06 21:07 ` erik quanstrom
@ 2007-11-06 21:54 ` arisawa
  1 sibling, 0 replies; 12+ messages in thread
From: arisawa @ 2007-11-06 21:54 UTC (permalink / raw)
  To: Fans of the OS Plan 9 from Bell Labs

Hello,

Try:

cat > lib/profile <<'!'

bind -a $$home/bin/rc /bin
!

Kenji Arisawa

On 2007/11/07, at 5:31, maht-9fans@mail.maht0x0r.net wrote:

> hi,
>
> I've not been able to post for a while (verifying sender didn't  
> like me), so I've got a few things stored up
>
> I think it was eric that said the only place here docs are used in  
> plan9 default is in /sys/lib/newuser and there whoever wrote it  
> went through some shenanigans to get $ in the output  (though this  
> is snipped):
>
> x='$'
> cat > lib/profile <<!
> bind -a $x^home/bin/rc /bin
> !
>
> However, what that person seems not to know is that they could have  
> done $$ to escape the $ :
>
> cat > lib/profile <<!
>
> bind -a $$home/bin/rc /bin
> !
>
> I like here documents, I think they look better than multi-line  
> echoes though it is annoying they can't be used in fn definitions.
>
> Matt
>
>
>
>
>
>
>
>
>


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [9fans] late to the party - Here docs
  2007-11-06 21:07 ` erik quanstrom
@ 2007-11-07 16:41   ` dave.l
  2007-11-07 20:19     ` erik quanstrom
  0 siblings, 1 reply; 12+ messages in thread
From: dave.l @ 2007-11-07 16:41 UTC (permalink / raw)
  To: Fans of the OS Plan 9 from Bell Labs

>> I like here documents,

Metoo, although there may be a bias according to people's typical  
application domains:
I often use them (on (l)unices) to spout the obscene gibberish that  
some external system requires:

	mysql --blah-blah <<!
	select $blah blah;
	$imbibe_rat_urine
	...
	!

and it's a hell of a lot saner and simpler if it's all bottled up in  
in one place,
rather than spread all over the script.

I may not use them frequently but when I do they are a brain-saver.


>> I think they look better than multi-line echoes

Exactly: they are an aesthetic convenience compared with multiple  
echo invocations
but so are quotes compared with multiple backslashes.

>> though it is annoying they can't be used in fn definitions.

That's a bug which could be fixed ...


> i was thinking about this topic again the other day during an
> idle moment.  here documents are part of the parsing routine.
> but they don't end up in rc's machine language.

Does this mean they don't work in loops too?

Sadly, I have to nerdishly play sh's advocate:
I just tested a recent ksh and bash and, as expected, heredocs in  
functions work,
as do things like:
	$ for n in 1 2 3 4 5;do cat<<EOF;done
	> $n
	> EOF
see below for why.

A quick scan of the rc source code suggests that rc won't DTRT with  
an analogous construct:
can someone with a running plan9 or p9p please confirm this
as a matter of academic interest?

It's non-trivial (but not hard) to implement correctly:
the old Bourne shell, and thus, presumably, it's successors, DidTRT:
store the text of the here documents verbatim when they are parsed
then spit the substituted text into a (new) tempfile every time it's  
needed.
IIRC it ate temporary files and there were also a few perverse  
boundary cases where it would break or
lose track of it's temp files or leave them lying around, but so what?.

Trivial aside: sh also did `...` in here docs, as do it's successors.


> they really are anomolous critters.  they need to be taken out
> back and shot.  ☺

Anomalous in use or in implementation?
Personally I think they are fine,
but their current implementation may need care and feeding.
If punishment is desired, may I suggest that the implementor be  
castigated for anomalousness.

DaveL





^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [9fans] late to the party - Here docs
  2007-11-07 16:41   ` dave.l
@ 2007-11-07 20:19     ` erik quanstrom
  2007-11-07 21:20       ` geoff
                         ` (2 more replies)
  0 siblings, 3 replies; 12+ messages in thread
From: erik quanstrom @ 2007-11-07 20:19 UTC (permalink / raw)
  To: 9fans

> Exactly: they are an aesthetic convenience compared with multiple  
> echo invocations
> but so are quotes compared with multiple backslashes.
> 
>>> though it is annoying they can't be used in fn definitions.
> 
> That's a bug which could be fixed ...
> 
> 
>> i was thinking about this topic again the other day during an
>> idle moment.  here documents are part of the parsing routine.
>> but they don't end up in rc's machine language.
> 
> Does this mean they don't work in loops too?

it could be fixed.  there must be a reason duff never made here
documents work.  i think they just don't fit the pattern.  one
justification i've seen for them is that they don't require the whole
here doc to be in the shell's memory.  the other is that "quoting
is easier".  unfortunately, to have here documents work within
functions, the whole here doc needs to be in the shell's memory.
so much for reason #1.  and one of the reasons for developing
rc was to cut down on the different quoting rules.  the bourne shell
has \-quoting, "-quoting, '-quoting and here document quoting
rules.  rc has ' quoting, \ quoting (only at the end of lines) and
here document quoting.

from your example, i'm not so sure why this

	{
		echo select $blah blah';'
		echo $imbibe_rat_urine
	}|mysql --blah-blah

is so terrible.

it turns out that you can use a here document in a loop

	; for(i in 1 2 3)
		cat <<!
		fu
		!
	fu
	fu
	fu

but it doesn't work if you use curly braces

	; for(i in 1 2 3){
		cat <<!
		fu
		!
		}
<hangs>
		!
	}

	fu: '/bin/fu' directory entry not found
	}
	fu: '/bin/fu' directory entry not found
	}
	fu: '/bin/fu' directory entry not found

since nobody's bothered to fix this, it's likely that it's just not used
very much.

- erik


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [9fans] late to the party - Here docs
  2007-11-07 20:19     ` erik quanstrom
@ 2007-11-07 21:20       ` geoff
  2007-11-07 21:23         ` erik quanstrom
  2007-11-07 22:58       ` Martin Neubauer
  2007-11-08  0:37       ` dave.l
  2 siblings, 1 reply; 12+ messages in thread
From: geoff @ 2007-11-07 21:20 UTC (permalink / raw)
  To: 9fans

You forgot `-quotes, which required \-quoting to nest in sh.


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [9fans] late to the party - Here docs
  2007-11-07 21:20       ` geoff
@ 2007-11-07 21:23         ` erik quanstrom
  0 siblings, 0 replies; 12+ messages in thread
From: erik quanstrom @ 2007-11-07 21:23 UTC (permalink / raw)
  To: 9fans

> You forgot `-quotes, which required \-quoting to nest in sh.

i knew i was forgetting something.  that whole reparsing thing
always bothered me.

- erik


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [9fans] late to the party - Here docs
  2007-11-07 20:19     ` erik quanstrom
  2007-11-07 21:20       ` geoff
@ 2007-11-07 22:58       ` Martin Neubauer
  2007-11-07 23:04         ` erik quanstrom
  2007-11-08  0:37       ` dave.l
  2 siblings, 1 reply; 12+ messages in thread
From: Martin Neubauer @ 2007-11-07 22:58 UTC (permalink / raw)
  To: Fans of the OS Plan 9 from Bell Labs

* erik quanstrom (quanstro@quanstro.net) wrote:
> but it doesn't work if you use curly braces

Of course it does; the syntax is somewhat unexpected, though:
	for(i in 1 2 3){
		cat <<!
		}
		fu
		!
	fu
	fu
	fu

	Martin


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [9fans] late to the party - Here docs
  2007-11-07 22:58       ` Martin Neubauer
@ 2007-11-07 23:04         ` erik quanstrom
  2007-11-07 23:24           ` Martin Neubauer
  0 siblings, 1 reply; 12+ messages in thread
From: erik quanstrom @ 2007-11-07 23:04 UTC (permalink / raw)
  To: 9fans

> * erik quanstrom (quanstro@quanstro.net) wrote:
>> but it doesn't work if you use curly braces
> 
> Of course it does; the syntax is somewhat unexpected, though:
> 	for(i in 1 2 3){
> 		cat <<!
> 		}
> 		fu
> 		!
> 	fu
> 	fu
> 	fu
> 
> 	Martin

so if i wanted

	for(i in 1 2 3){
		cat <<!
		fu
		!
		echo give me a break
	}

what contortions do i need?

- erik


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [9fans] late to the party - Here docs
  2007-11-07 23:04         ` erik quanstrom
@ 2007-11-07 23:24           ` Martin Neubauer
  0 siblings, 0 replies; 12+ messages in thread
From: Martin Neubauer @ 2007-11-07 23:24 UTC (permalink / raw)
  To: Fans of the OS Plan 9 from Bell Labs

* erik quanstrom (quanstro@quanstro.net) wrote:
> so if i wanted
> 
> 	for(i in 1 2 3){
> 		cat <<!
> 		fu
> 		!
> 		echo give me a break
> 	}
> 
> what contortions do i need?
> 
> - erik

That would be

	for(i in 1 2 3){
		cat <<!
		echo give me a break
		}
		fu
		!

	Martin


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [9fans] late to the party - Here docs
  2007-11-07 20:19     ` erik quanstrom
  2007-11-07 21:20       ` geoff
  2007-11-07 22:58       ` Martin Neubauer
@ 2007-11-08  0:37       ` dave.l
  2007-11-08  1:21         ` Gregory Pavelcak
  2 siblings, 1 reply; 12+ messages in thread
From: dave.l @ 2007-11-08  0:37 UTC (permalink / raw)
  To: Fans of the OS Plan 9 from Bell Labs

> from your example, i'm not so sure why this
>
> 	{
> 		echo select $blah blah';'
> 		echo $imbibe_rat_urine
> 	}|mysql --blah-blah
>
> is so terrible.
>


It's not, but it's still uglier ...


> it turns out that you can use a here document in a loop
>
> 	; for(i in 1 2 3)
> 		cat <<!
> 		fu
> 		!
> 	fu
> 	fu
> 	fu
>

Close, but no coconut.  What does:
	for(i in 1 2 3)
		cat <<!
		$i
		!
do?


> but it doesn't work if you use curly braces
>

Yeesh!  Nice detective work!
I see from later detective work it does work, but not in an obvious way:
IIRC sh slurped all the pending heredocs in orders when the lexer(!)  
saw an unquoted newline,
which makes much more sense, but not in a syntactic way, IYSWIM.


> since nobody's bothered to fix this, it's likely that it's just not  
> used
> very much.
>

You're totally right.
OTOH, looking at rc has given me a bit of a software itch ...
I keep thinking "There's gotta be a neater/cleaner/faster way to do  
here documents, argument parsing, globbing, ...".

My "need" for a plan9 system to scratch that itch is increasing:-) ...

DaveL


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [9fans] late to the party - Here docs
  2007-11-08  0:37       ` dave.l
@ 2007-11-08  1:21         ` Gregory Pavelcak
  0 siblings, 0 replies; 12+ messages in thread
From: Gregory Pavelcak @ 2007-11-08  1:21 UTC (permalink / raw)
  To: 9fans

This may be spectacularly stupid, but . . .

It seems to me that the Plan 9 (or UNIX) way here, given pipes
and good text processing tools, is to think that there is absolutely
no need for me to write rc scripts the way that rc demands that
I write them as long as I have a (perhaps sed or awk based)
preprocessor. So, instead of actually writing

	echo 1
	echo 2
	.
	.
	.
	echo n

one might just write

	echoon
	1
	2
	.
	.
	.
	n
	echooff

and pass it through the preprocessor before it gets run by rc.

Is that nuts? It seems to me that, if you don't write a lot of long
here doc using scripts, you can just write the echos, but if you
do, it might be worthwhile to do something like that.

Just a mildly buzzed thought. Man, the Berkshire Brewing
Company makes some good beer. :-)

Greg


^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2007-11-08  1:21 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-11-06 20:31 [9fans] late to the party - Here docs maht-9fans
2007-11-06 21:07 ` erik quanstrom
2007-11-07 16:41   ` dave.l
2007-11-07 20:19     ` erik quanstrom
2007-11-07 21:20       ` geoff
2007-11-07 21:23         ` erik quanstrom
2007-11-07 22:58       ` Martin Neubauer
2007-11-07 23:04         ` erik quanstrom
2007-11-07 23:24           ` Martin Neubauer
2007-11-08  0:37       ` dave.l
2007-11-08  1:21         ` Gregory Pavelcak
2007-11-06 21:54 ` arisawa

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).