9front - general discussion about 9front
 help / color / mirror / Atom feed
* [9front] /bin/"" use of rc -c instead of eval
@ 2024-07-05 22:23 Romano
  2024-07-05 23:14 ` Stanley Lieber
  2024-07-05 23:49 ` [9front] " Anthony Martin
  0 siblings, 2 replies; 9+ messages in thread
From: Romano @ 2024-07-05 22:23 UTC (permalink / raw)
  To: 9front

I use " and "" to review previous commands and execute them, and
I was puzzled when a previous 'cd' command didn't work. I
suspected it might be perhaps to 'cd' being a built-in, and
looking at the source code for "" my suspicion was confirmed. The
last line that executes the command is: rc -c $"_x

Any reason why this isn't using eval instead so that rc built-ins
work (eval $"_x)

P.S. There doesn't appear to be any man page for "" or ", and lookman
doesn't support "" either:

cpu% lookman ""
usage: lookman key ...
cpu% lookman '""'
usage: lookman key ...
cpu%

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

* Re: [9front] /bin/"" use of rc -c instead of eval
  2024-07-05 22:23 [9front] /bin/"" use of rc -c instead of eval Romano
@ 2024-07-05 23:14 ` Stanley Lieber
  2024-07-05 23:49 ` [9front] " Anthony Martin
  1 sibling, 0 replies; 9+ messages in thread
From: Stanley Lieber @ 2024-07-05 23:14 UTC (permalink / raw)
  To: 9front

On July 5, 2024 6:23:03 PM EDT, Romano <me+unobe@fallglow.com> wrote:
>I use " and "" to review previous commands and execute them, and
>I was puzzled when a previous 'cd' command didn't work. I
>suspected it might be perhaps to 'cd' being a built-in, and
>looking at the source code for "" my suspicion was confirmed. The
>last line that executes the command is: rc -c $"_x
>
>Any reason why this isn't using eval instead so that rc built-ins
>work (eval $"_x)
>
>P.S. There doesn't appear to be any man page for "" or ", and lookman
>doesn't support "" either:
>
>cpu% lookman ""
>usage: lookman key ...
>cpu% lookman '""'
>usage: lookman key ...
>cpu%
>

these came straight from plan9port. i don't know the rationale for rc -c vs eval.

sl

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

* [9front] Re: /bin/"" use of rc -c instead of eval
  2024-07-05 22:23 [9front] /bin/"" use of rc -c instead of eval Romano
  2024-07-05 23:14 ` Stanley Lieber
@ 2024-07-05 23:49 ` Anthony Martin
  2024-07-06  1:48   ` Romano
  1 sibling, 1 reply; 9+ messages in thread
From: Anthony Martin @ 2024-07-05 23:49 UTC (permalink / raw)
  To: 9front

Romano <me+unobe@fallglow.com> once said:
> I use " and "" to review previous commands and execute them, and
> I was puzzled when a previous 'cd' command didn't work. I
> suspected it might be perhaps to 'cd' being a built-in, and
> looking at the source code for "" my suspicion was confirmed. The
> last line that executes the command is: rc -c $"_x
>
> Any reason why this isn't using eval instead so that rc built-ins
> work (eval $"_x)

Did you try it? What happened?

Hint: what you seem to want is not

	"" cd.*foo

but
	. <{" cd.*foo}

Cheers,
  Anthony

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

* Re: [9front] Re: /bin/"" use of rc -c instead of eval
  2024-07-05 23:49 ` [9front] " Anthony Martin
@ 2024-07-06  1:48   ` Romano
  2024-07-06  8:00     ` sirjofri
  0 siblings, 1 reply; 9+ messages in thread
From: Romano @ 2024-07-06  1:48 UTC (permalink / raw)
  To: 9front

On Fri Jul  5 16:52:25 -0700 2024, ality@pbrane.org wrote:
> Romano <me+unobe@fallglow.com> once said:
> > I use " and "" to review previous commands and execute them, and
> > I was puzzled when a previous 'cd' command didn't work. I
> > suspected it might be perhaps to 'cd' being a built-in, and
> > looking at the source code for "" my suspicion was confirmed. The
> > last line that executes the command is: rc -c $"_x
> >
> > Any reason why this isn't using eval instead so that rc built-ins
> > work (eval $"_x)
> 
> Did you try it? What happened?
> 
> Hint: what you seem to want is not
> 
> 	"" cd.*foo
> 
> but
> 	. <{" cd.*foo}
> 

Thanks, Anthony, even eval won't work for what I want. I had lazily
just tested:
  rc -c 'cd /sys/src'
against:
  eval 'cd /sys/src'

The above eval works if run directly, but not when within a shell
script like "", so I'd have to think of some other way. 

One way that looks promising is defining "" as a function with
the source of "" and changing 'rc -c' to 'eval':

cpu% whatis ""
fn "" {
	PROMPT='[^ 	]*(%|;)+[ 	]+'
	_x=`{
		" $*|tail -1
	}
	if(~ $#_x 0){
		>[1=2]echo no such command found
		exit notfound
	}
	>[1=2]echo '	' $_x
	_x=`{
		echo -n 'eval '''; echo $_x|sed 's/^'^$PROMPT^'//; s/''/''''/g; s/$/''/'
	}
	eval $"_x
}
cpu% pwd
/
cpu% "" cd /sys
	 cpu% cd /sys/src
cpu% pwd
/sys/src
cpu%


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

* Re: [9front] Re: /bin/"" use of rc -c instead of eval
  2024-07-06  1:48   ` Romano
@ 2024-07-06  8:00     ` sirjofri
  2024-07-06  9:41       ` Romano
  0 siblings, 1 reply; 9+ messages in thread
From: sirjofri @ 2024-07-06  8:00 UTC (permalink / raw)
  To: 9front

06.07.2024 03:49:46 Romano <me+unobe@fallglow.com>:
> cpu% pwd
> /
> cpu% "" cd /sys
>      cpu% cd /sys/src
> cpu% pwd
> /sys/src
> cpu%

That's not how it's supposed to work though. Let me illustrate:

> cpu% pwd
> /sys/src
> cpu% "
>     cpu% pwd
> cpu% ""
>     cpu% pwd
> /sys/src
> cpu%

" only displays the last command while "" executes it. An optional argument can be used for filtering:

> cpu% pwd
> /sys/src
> cpu% cd /lib
> cpu% "" p
>     cpu% pwd
> /lib
> cpu%

Note that I never tested cd'ing with "", which means a lot. It's probably not as useful in most cases, which makes sense if you think about it: you use cd to switch to another directory. When you're there, running it again is a no-op at best, or it fails.

sirjofri

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

* Re: [9front] Re: /bin/"" use of rc -c instead of eval
  2024-07-06  8:00     ` sirjofri
@ 2024-07-06  9:41       ` Romano
  2024-07-06 14:05         ` sirjofri
  2024-07-06 14:08         ` sirjofri
  0 siblings, 2 replies; 9+ messages in thread
From: Romano @ 2024-07-06  9:41 UTC (permalink / raw)
  To: 9front

On Sat Jul  6 01:04:30 -0700 2024, sirjofri+ml-9front@sirjofri.de wrote:
> 06.07.2024 03:49:46 Romano <me+unobe@fallglow.com>:
> > cpu% pwd
> > /
> > cpu% "" cd /sys
> >      cpu% cd /sys/src
> > cpu% pwd
> > /sys/src
> > cpu%
> 
> That's not how it's supposed to work though. Let me illustrate:
> 
> > cpu% pwd
> > /sys/src
> > cpu% "
> >     cpu% pwd
> > cpu% ""
> >     cpu% pwd
> > /sys/src
> > cpu%
> 
> " only displays the last command while "" executes it. An optional argument can be used for filtering:
> 
> > cpu% pwd
> > /sys/src
> > cpu% cd /lib
> > cpu% "" p
> >     cpu% pwd
> > /lib
> > cpu%
> 

Maybe I did not give enough context in my reply: I know
that " lists *previous* commands and "" executes them. So
'cd /sys/src' was something I had previously run in that
window. Here's a comparison of the current "" functionality:

|cpu% cd /sys/src
|cpu% cd /sys
|cpu% pwd
|/sys
|cpu% " cd
|	cpu% cd /sys/src
|	cpu% cd /sys
|cpu% "" cd.*src
|	 cpu% cd /sys/src
|cpu% pwd
|/sys
|cpu% 

And here it is using "" as a fn with eval:

|cpu% fn "" {
|		PROMPT='[^ 	]*(%|;)+[ 	]+'
|		_x=`{
|			" $*|tail -1
|		}
|		if(~ $#_x 0){
|			>[1=2]echo no such command found
|			exit notfound
|		}
|		>[1=2]echo '	' $_x
|		_x=`{
|			echo -n 'eval '''; echo $_x|sed 's/^'^$PROMPT^'//; s/''/''''/g; s/$/''/'
|		}
|		eval $"_x
|	}
|cpu% cd /sys/src
|cpu% cd /sys
|cpu% pwd
|/sys
|cpu% " cd
|	cpu% cd /sys/src
|	cpu% cd /sys
|cpu% "" cd.*src
|	 cpu% cd /sys/src
|cpu% pwd
|/sys/src
|cpu% 

Did you think I was saying it worked differently?

My main point is that built-in rc commands don't
like other commands with "". That is, even tho'
it'll happily print out the cd command, it has
no effect in the shell since it's run in a
subshell. The most I could do is then copy-and-
paste if I wanted to use it. AFAICT a handful
would really make a difference with the fn-form
of "": cd, eval, flag, fn, and '.'.

> Note that I never tested cd'ing with "", which means a lot. It's probably not as useful in most cases, which makes sense if you think about it: you use cd to switch to another directory. When you're there, running it again is a no-op at best, or it fails.

Switching back and forth between different directories
is a common occurrence during development for me, so
I wanted something akin to bash-like 'cd -' or '^R cd'
without having to use the mouse.

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

* Re: [9front] Re: /bin/"" use of rc -c instead of eval
  2024-07-06  9:41       ` Romano
@ 2024-07-06 14:05         ` sirjofri
  2024-07-06 14:08         ` sirjofri
  1 sibling, 0 replies; 9+ messages in thread
From: sirjofri @ 2024-07-06 14:05 UTC (permalink / raw)
  To: 9front

06.07.2024 11:43:32 Romano <me+unobe@fallglow.com>:
> Maybe I did not give enough context in my reply: I know
> that " lists *previous* commands and "" executes them. So
> 'cd /sys/src' was something I had previously run in that
> window. Here's a comparison of the current "" functionality:
>
> |cpu% cd /sys/src
> |cpu% cd /sys
> |cpu% pwd
> |/sys
> |cpu% " cd
> |   cpu% cd /sys/src
> |   cpu% cd /sys
> |cpu% "" cd.*src
> |    cpu% cd /sys/src
> |cpu% pwd
> |/sys
> |cpu%
>
> And here it is using "" as a fn with eval:
>
> |cpu% fn "" {
> |       PROMPT='[^  ]*(%|;)+[   ]+'
> |       _x=`{
> |           " $*|tail -1
> |       }
> |       if(~ $#_x 0){
> |           >[1=2]echo no such command found
> |           exit notfound
> |       }
> |       >[1=2]echo '    ' $_x
> |       _x=`{
> |           echo -n 'eval '''; echo $_x|sed 's/^'^$PROMPT^'//; s/''/''''/g; s/$/''/'
> |       }
> |       eval $"_x
> |   }
> |cpu% cd /sys/src
> |cpu% cd /sys
> |cpu% pwd
> |/sys
> |cpu% " cd
> |   cpu% cd /sys/src
> |   cpu% cd /sys
> |cpu% "" cd.*src
> |    cpu% cd /sys/src
> |cpu% pwd
> |/sys/src
> |cpu%
>
> Did you think I was saying it worked differently?

Initially, it sounded like that, but with that context you're right.

> My main point is that built-in rc commands don't
> like other commands with "". That is, even tho'
> it'll happily print out the cd command, it has
> no effect in the shell since it's run in a
> subshell. The most I could do is then copy-and-
> paste if I wanted to use it. AFAICT a handful
> would really make a difference with the fn-form
> of "": cd, eval, flag, fn, and '.'.

I don't think this has much to do with builtins or not, but with the subshell. Cd is changing "the namespace". I wonder how "" works with mounts/binds: do they also only change the namespace of the subshell? I'd guess they do.

>> Note that I never tested cd'ing with "", which means a lot. It's probably not as useful in most cases, which makes sense if you think about it: you use cd to switch to another directory. When you're there, running it again is a no-op at best, or it fails.
>
> Switching back and forth between different directories
> is a common occurrence during development for me, so
> I wanted something akin to bash-like 'cd -' or '^R cd'
> without having to use the mouse.

I usually have a single rc command at the end of my test scripts (or I run it manually). That way I can investigate/analyze the results, and just use ^D to fall back to my main shell. Sure that's not ideal for everyone, but when I call a program I wouldn't expect it to change my directory at all.

I often have a ramfs behind an rfork n, so I build my test scripts like this:

> #!/bin/rc
> rfork en
> ramfs
> # test...
> cd /tmp
> rc

sirjofri


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

* Re: [9front] Re: /bin/"" use of rc -c instead of eval
  2024-07-06  9:41       ` Romano
  2024-07-06 14:05         ` sirjofri
@ 2024-07-06 14:08         ` sirjofri
  2024-07-11  6:02           ` Romano
  1 sibling, 1 reply; 9+ messages in thread
From: sirjofri @ 2024-07-06 14:08 UTC (permalink / raw)
  To: 9front

To add on that, I would vote for changing everything to make cd work. It's about consistency.

The question is if it's better to have it in /bin/"" or as /env/fn#"". I think that /bin/"" makes more sense for various reasons.

Also a good question would be: do we create an extra case for cd commands, or do we use eval? An extra case for cd commands would imply that the cd part of custom programs (including shell scripts) wouldn't work, so eval would be the most consistent, as OP suggested. I don't know about the downsides of eval though.

sirjofri

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

* Re: [9front] Re: /bin/"" use of rc -c instead of eval
  2024-07-06 14:08         ` sirjofri
@ 2024-07-11  6:02           ` Romano
  0 siblings, 0 replies; 9+ messages in thread
From: Romano @ 2024-07-11  6:02 UTC (permalink / raw)
  To: 9front

On Sat Jul  6 07:11:12 -0700 2024, sirjofri+ml-9front@sirjofri.de wrote:
> To add on that, I would vote for changing everything to make cd work. It's about consistency.
> 
> The question is if it's better to have it in /bin/"" or as /env/fn#"". I think that /bin/"" makes more sense for various reasons.
> 
> Also a good question would be: do we create an extra case for cd commands, or do we use eval? An extra case for cd commands would imply that the cd part of custom programs (including shell scripts) wouldn't work, so eval would be the most consistent, as OP suggested. I don't know about the downsides of eval though.
 
I noticed there was a bigger issue with rc, and so sent the mailing list
a suggested patch to address. With that in place, the below patch makes
"" works for built-ins by defining itself as a function, which then
updates the parent since it's called with a shared environment.

From: Romano <me+unobe@fallglow.com>
Date: Thu, 11 Jul 2024 05:56:24 +0000
Subject: [PATCH] make "" use a fn that will then work with built-ins

	currently "" does not work with built-ins, so re-running
	built-ins might not have the desired effect (e.g., setting
	environment variables, cd). This updates "" to be defined
	internally as a function, which then updates the parent
	environment after being called.
---
diff 23b40c5aa93bb205423c8c40d02eef825011b215 7637f8f5fd6490d07ba64bcf404321028e698ed9
--- a/rc/bin/""
+++ b/rc/bin/""
@@ -1,5 +1,5 @@
-#!/bin/rc
-
+#!/bin/rc -b
+fn "" {
 PROMPT='[^ 	]*(%|;)+[ 	]+'
 
 _x = `{" $* | tail -1}
@@ -10,4 +10,6 @@
 
 echo '	' $_x >[1=2]
 _x=`{ echo -n 'eval '''; echo $_x | sed 's/^'$PROMPT'//; s/''/''''/g; s/$/''/'}
-rc -c $"_x
+eval $"_x
+}
+"" $*

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

end of thread, other threads:[~2024-07-11  6:03 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-07-05 22:23 [9front] /bin/"" use of rc -c instead of eval Romano
2024-07-05 23:14 ` Stanley Lieber
2024-07-05 23:49 ` [9front] " Anthony Martin
2024-07-06  1:48   ` Romano
2024-07-06  8:00     ` sirjofri
2024-07-06  9:41       ` Romano
2024-07-06 14:05         ` sirjofri
2024-07-06 14:08         ` sirjofri
2024-07-11  6:02           ` Romano

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