zsh-users
 help / color / mirror / code / Atom feed
* Defining commands to not evaluate certain metacharacters
@ 2003-12-20 18:39 Lloyd Zusman
  2003-12-21  1:32 ` Bart Schaefer
  0 siblings, 1 reply; 5+ messages in thread
From: Lloyd Zusman @ 2003-12-20 18:39 UTC (permalink / raw)
  To: zsh-users

I want to define a set of commands that I can execute from the command
line under zsh.  These commands will be performing certain mathematical
operations on their arguments, and therefore, I would like parentheses
and possibly also square brackets to be passed to the command unchanged
by the shell.  However, I want to be able to use variable expansion.

I would like to do this without quoting these parentheses (or square
brackets, if I use them).  For example, if CMD is one of the commands
that I define and OP1, OP2, and OP3 are operations that CMD understands,
I would like the following to take place:

  a=3
  b=4
  CMD (($a OP1 1) OP2 ($b OP3 2))

  means:  expand $a and $b on the command line, so that the
          intermediate result is this:

            CMD ((3 OP1 1) OP2 (4 OP3 2))

          then, pass the string '((3 OP1 1) OP2 (4 OP3 2))' as the
          command line to CMD, which then interprets it as it pleases.

I know that I can achieve this result by double-quoting the command
line, as follows:

  CMD "(($a OP1 1) OP2 ($b OP3 2))"

However, as I mentioned above, my goal is to avoid using quotes.

Is there any way to set up aliases or something similar under zsh so
that I can define commands that will function like my CMD example?

Thanks in advance.

-- 
 Lloyd Zusman
 ljz@asfast.com


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

* Re: Defining commands to not evaluate certain metacharacters
  2003-12-20 18:39 Defining commands to not evaluate certain metacharacters Lloyd Zusman
@ 2003-12-21  1:32 ` Bart Schaefer
  2003-12-21  1:38   ` Philippe Troin
  0 siblings, 1 reply; 5+ messages in thread
From: Bart Schaefer @ 2003-12-21  1:32 UTC (permalink / raw)
  To: zsh-users

On Dec 20,  1:39pm, Lloyd Zusman wrote:
}
} I want to define a set of commands that I can execute from the command
} line under zsh.

This is very nearly a non-starter.  The shell parses shell syntax, not
the syntax of some other language.

You can get part of the way there with the "noglob" precommand modifier,
but that doesn't change the special meanings of parentheses, braces, or
redirections.

You can get somewhat farther by using ZLE widgets to read the input and
reprocess it before handing it to the shell parser, but that still means
using some additional step to invoke that ZLE widget.  Theoretically you
could rebind accept-line to a wrapper that checks whether the first word
on the line is one of your special commands, but that gets fragile if you
make any attempt to handle multi-line inputs.

    function quoting-accept-line {
        local q='"' words
	words=( $=BUFFER )
	case $words[1] in
	CMD|etc) BUFFER="$words[1] $q${words[2,-1]}$q";;
	esac
	zle .accept-line
    }
    zle -N accept-line quoting-accept-line

However what I'd most recommend is that you write a function that runs a
"while read line; do ...; done" loop, which "eval"s anything that looks
like a shell assignment and otherwise invokes your mathematical processor.


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

* Re: Defining commands to not evaluate certain metacharacters
  2003-12-21  1:32 ` Bart Schaefer
@ 2003-12-21  1:38   ` Philippe Troin
  2003-12-21  6:53     ` Bart Schaefer
  0 siblings, 1 reply; 5+ messages in thread
From: Philippe Troin @ 2003-12-21  1:38 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: zsh-users

Bart Schaefer <schaefer@brasslantern.com> writes:

> On Dec 20,  1:39pm, Lloyd Zusman wrote:
> }
> } I want to define a set of commands that I can execute from the command
> } line under zsh.
> 
> This is very nearly a non-starter.  The shell parses shell syntax, not
> the syntax of some other language.
> 
> You can get part of the way there with the "noglob" precommand modifier,
> but that doesn't change the special meanings of parentheses, braces, or
> redirections.

How hard would it be implementing a "noparse" precommand modifier?

I've also been wanting to use such a construct, for example when using
the zmv contributed function.

Phil.


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

* Re: Defining commands to not evaluate certain metacharacters
  2003-12-21  1:38   ` Philippe Troin
@ 2003-12-21  6:53     ` Bart Schaefer
  2003-12-22  4:29       ` Philippe Troin
  0 siblings, 1 reply; 5+ messages in thread
From: Bart Schaefer @ 2003-12-21  6:53 UTC (permalink / raw)
  To: zsh-users

On Dec 20,  5:38pm, Philippe Troin wrote:
}
} How hard would it be implementing a "noparse" precommand modifier?

What does "noparse" mean?  It's got to begin parsing before it can
recognize a precommand modifier.  Even splitting into words at whitespace
is parsing, of a sort.  

Do backslashes still work?  What about quotes of various flavors?  Care
to predict every possible question of this kind that I might ask?

If there isn't _some_ syntax, you're just using "cat" as your shell; the
input can't be processed in any useful way.

If you want to be able to define your own arbitrary syntax, you need a
language-definition language (like a YACC grammar).  How hard would it
be to implement that?


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

* Re: Defining commands to not evaluate certain metacharacters
  2003-12-21  6:53     ` Bart Schaefer
@ 2003-12-22  4:29       ` Philippe Troin
  0 siblings, 0 replies; 5+ messages in thread
From: Philippe Troin @ 2003-12-22  4:29 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: zsh-users

Bart Schaefer <schaefer@brasslantern.com> writes:

> On Dec 20,  5:38pm, Philippe Troin wrote:
> }
> } How hard would it be implementing a "noparse" precommand modifier?
> 
> What does "noparse" mean?  It's got to begin parsing before it can
> recognize a precommand modifier.  Even splitting into words at whitespace
> is parsing, of a sort.  
> 
> Do backslashes still work?  What about quotes of various flavors?  Care
> to predict every possible question of this kind that I might ask?

I have no clue how it would work. I've just said I felt the need for
such a thing at some point, namely while trying to avoid quoting when
using zmv.
 
> If there isn't _some_ syntax, you're just using "cat" as your shell; the
> input can't be processed in any useful way.
> 
> If you want to be able to define your own arbitrary syntax, you need a
> language-definition language (like a YACC grammar).  How hard would it
> be to implement that?

Let's not put yacc into zsh (yet).

Phil.


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

end of thread, other threads:[~2003-12-22  4:29 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-12-20 18:39 Defining commands to not evaluate certain metacharacters Lloyd Zusman
2003-12-21  1:32 ` Bart Schaefer
2003-12-21  1:38   ` Philippe Troin
2003-12-21  6:53     ` Bart Schaefer
2003-12-22  4:29       ` Philippe Troin

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

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