9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
* [9fans] rc word splitting trouble
@ 2010-04-03 14:21 Ethan Grammatikidis
  2010-04-03 14:38 ` erik quanstrom
  0 siblings, 1 reply; 3+ messages in thread
From: Ethan Grammatikidis @ 2010-04-03 14:21 UTC (permalink / raw)
  To: Fans of the OS Plan 9 from Bell Labs

I'm writing a little web server in rc. For the most part it's been
less trouble than setting up Apache to serve static files, but I've
run into an odd little problem where rc appears to be behaving
differently depending on whether it's run in a script or a terminal.
(Rc & 9term from p9p.)

I'm testing with url: http://analytical-reengineering.dre.am/one/two?three+four

The script separates out the /one/two?three+four into a variable &
then splits that twice, first to get the 'file' part, which it does
correctly, then the arguments, which it has trouble with. You can see
the result at the url above. "three four" appear on 1 line where they
should be two. If I paste the same lines of code into a terminal, the
lines are split correctly:

% fullloc = '/one/two?three+four'
% params = `{echo $fullloc | sed -e 's;.*\?;;' -e 's;\+; ;g'}
% for(param in $params) echo '	<li>'$"param
	<li>three
	<li>four

The 2nd and 3rd command lines are copied directly from the script, but
the script itself outputs the following:

	<li>three four

The sed expressions appear to be working correctly, to my eyes, so I
don't understand what's going on.

--
Simplicity does not precede complexity, but follows it. -- Alan Perlis




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

* Re: [9fans] rc word splitting trouble
  2010-04-03 14:21 [9fans] rc word splitting trouble Ethan Grammatikidis
@ 2010-04-03 14:38 ` erik quanstrom
  2010-04-03 17:24   ` Ethan Grammatikidis
  0 siblings, 1 reply; 3+ messages in thread
From: erik quanstrom @ 2010-04-03 14:38 UTC (permalink / raw)
  To: 9fans

> % fullloc = '/one/two?three+four'
> % params = `{echo $fullloc | sed -e 's;.*\?;;' -e 's;\+; ;g'}
> % for(param in $params) echo '	<li>'$"param
> 	<li>three
> 	<li>four
>
> The 2nd and 3rd command lines are copied directly from the script, but
> the script itself outputs the following:
>
> 	<li>three four
>
> The sed expressions appear to be working correctly, to my eyes, so I
> don't understand what's going on.

i suspect your ifs is set to something funky.

inserting a "whatis params" in your script would be pretty
interesting.  note that gnu sed will eat your regular expression
since you \? is their meta character.

you can accomplish something that's equivalent in rc without
an explicit loop:

echo '<li>' ^ $params

- erik



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

* Re: [9fans] rc word splitting trouble
  2010-04-03 14:38 ` erik quanstrom
@ 2010-04-03 17:24   ` Ethan Grammatikidis
  0 siblings, 0 replies; 3+ messages in thread
From: Ethan Grammatikidis @ 2010-04-03 17:24 UTC (permalink / raw)
  To: Fans of the OS Plan 9 from Bell Labs

[-- Attachment #1: Type: text/plain, Size: 1475 bytes --]


On 3 Apr 2010, at 15:38, erik quanstrom wrote:

>> % fullloc = '/one/two?three+four'
>> % params = `{echo $fullloc | sed -e 's;.*\?;;' -e 's;\+; ;g'}
>> % for(param in $params) echo '	<li>'$"param
>> 	<li>three
>> 	<li>four
>>
>> The 2nd and 3rd command lines are copied directly from the script,
>> but
>> the script itself outputs the following:
>>
>> 	<li>three four
>>
>> The sed expressions appear to be working correctly, to my eyes, so I
>> don't understand what's going on.
>
> i suspect your ifs is set to something funky.
>
> inserting a "whatis params" in your script would be pretty
> interesting.  note that gnu sed will eat your regular expression
> since you \? is their meta character.

sed is p9p sed, verified with which, but ty.

You're right about ifs, "whatis ifs" showed it up. It is set to
newline-only for one line within the read loop, using the "var=foo
command" syntax, but "command" in this case is another assignment.
When I add a no-op command to the end (making it "assignment
assignment command") rc complains "params: not found" and fails to run
the loop properly. I don't know what kind of state the parser got into
to produce that error.

Anyway, I've fixed it for now by re-setting ifs after the line in
question.

I'm attaching the script; an embedded carriage-return makes a mess of
both pasting the line here and posting it in a pastebin. The problem
line is the first one in the while block.


[-- Attachment #2: web-script.rc --]
[-- Type: application/octet-stream, Size: 1113 bytes --]

# munch on request
reqlines = ''
done = false
while(~ $"done 'false') {
	ifs = '
' line = `{read |sed 's/\r//g'}
	# echo '^'$"line'$' > /dev/stderr
	reqlines = $"reqlines$"line'
'

	ifs=' 	
'
	if(echo $"line | grep '^GET ' > /dev/null) {
		method = `{echo $"line | sed 's; .*;;'}
		fullloc = `{echo $"line | sed -e 's;[^ ]* /;/;' -e 's; .*;;'}
		location = `{echo $"fullloc | sed 's;\?.*;;'}
		params = `{echo $fullloc | sed -e 's;.*\?;;' -e 's;\+; ;g'}
	}

	if(~ $"line '')
		done = true
}

# spew our junk
echo HTTP/1.0 200 OK
echo Content-type text/html
echo

cat << EOF
<html>
<head>
<title>Hello!</title>
</head>
<body>

EOF

echo '<p>Request received:
<pre>'
echo -n $reqlines
echo '</pre>'

echo '<ul><li>Method:' $"method
echo '<li>Location (complete):' $"fullloc
echo '<li>Location:' $"location
echo '<li>Parameters:<ul>'
for(param in $params) echo '	<li>'$"param
echo '	</ul>'
echo '</ul>'

echo '<pre>'
echo '% whatis ifs'
whatis ifs
echo '% whatis params'
whatis params
echo '% which sed'
which sed
echo '</pre>'

echo '
</body>
</html>
'

[-- Attachment #3: Type: text/plain, Size: 345 bytes --]





>
> you can accomplish something that's equivalent in rc without
> an explicit loop:
>
> echo '<li>' ^ $params

Good to know. I haven't used it here because the for loop generates
nicer html from nicer source, if that makes sense.

>
> - erik
>

--
Simplicity does not precede complexity, but follows it. -- Alan Perlis


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

end of thread, other threads:[~2010-04-03 17:24 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-04-03 14:21 [9fans] rc word splitting trouble Ethan Grammatikidis
2010-04-03 14:38 ` erik quanstrom
2010-04-03 17:24   ` Ethan Grammatikidis

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).