zsh-users
 help / color / mirror / code / Atom feed
* print to the terminal in zle
@ 2013-07-26 13:48 Stephane Chazelas
  2013-07-26 17:05 ` Peter Stephenson
                   ` (2 more replies)
  0 siblings, 3 replies; 9+ messages in thread
From: Stephane Chazelas @ 2013-07-26 13:48 UTC (permalink / raw)
  To: zsh-users

Hiya,

recently, Debian broke some of my scripts using ZLE in `zsh -i` by adding:

        function zle-line-init () {
            emulate -L zsh
            printf '%s' ${terminfo[smkx]}
        }

To /etc/zsh/zshrc

That smkx  escape sequence is printed to stdout instead of the terminal.

What would be the correct way to do it?

Doing `printf > /dev/tty` would probably do it but it would be
better I think to be able to write to the fd that zsh currently
has opened to the terminal (usually 10 if it  was free upon zsh
startup).

is there a way to do that?

Or an alternative way to set smkx on ZLE init and rmks  on
zle-finish?

-- 
Stephane


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

* Re: print to the terminal in zle
  2013-07-26 13:48 print to the terminal in zle Stephane Chazelas
@ 2013-07-26 17:05 ` Peter Stephenson
  2013-07-26 17:36 ` Frank Terbeck
  2013-07-26 19:39 ` Peter Stephenson
  2 siblings, 0 replies; 9+ messages in thread
From: Peter Stephenson @ 2013-07-26 17:05 UTC (permalink / raw)
  To: zsh-users

On Fri, 26 Jul 2013 14:48:11 +0100
Stephane Chazelas <stephane.chazelas@gmail.com> wrote:
> recently, Debian broke some of my scripts using ZLE in `zsh -i` by adding:
> 
>         function zle-line-init () {
>             emulate -L zsh
>             printf '%s' ${terminfo[smkx]}
>         }
> 
> To /etc/zsh/zshrc
> 
> That smkx  escape sequence is printed to stdout instead of the terminal.
> 
> What would be the correct way to do it?
> 
> Doing `printf > /dev/tty` would probably do it but it would be
> better I think to be able to write to the fd that zsh currently
> has opened to the terminal (usually 10 if it  was free upon zsh
> startup).
> 
> is there a way to do that?

I don't think there's a way to get the terminal file descriptor or use
it for arbitrary output at the moment.

I'm a bit surprised nobody ever thought of adding a mechanism for
outputting terminal start and end codes before and after the line
editor, but it doesn't look like anyone did, and we now have the hooks
as a more general purpose mechanism.

It would be possible to add an option to zle to query it and assign it
to a parameter.  Then you could use "print -nr -u$fd --" ('printf "%s"'
is presumably an alternative to print -nr --).  (You can't use >&$fd
because that's reserved for file descriptors opened by the user.)  Doing
it this way might be enough to make it clear it's for people in white
coats, i.e. this is not the normal method of terminal output.

By the way, the following patch fixes a bad error message:

% print -u11 blah
print: bad file number: -1

True but not really the point.

diff --git a/Src/builtin.c b/Src/builtin.c
index 8516acd..ae2e9f6 100644
--- a/Src/builtin.c
+++ b/Src/builtin.c
@@ -3792,11 +3792,11 @@ bin_print(char *name, char **args, Options ops, int func)
 
     /* -u and -p -- output to other than standard output */
     if (OPT_HASARG(ops,'u') || OPT_ISSET(ops,'p')) {
-	int fd;
+	int fdarg, fd;
 
 	if (OPT_ISSET(ops, 'p')) {
-	    fd = coprocout;
-	    if (fd < 0) {
+	    fdarg = coprocout;
+	    if (fdarg < 0) {
 		zwarnnam(name, "-p: no coprocess");
 		return 1;
 	    }
@@ -3804,13 +3804,13 @@ bin_print(char *name, char **args, Options ops, int func)
 	    char *argptr = OPT_ARG(ops,'u'), *eptr;
 	    /* Handle undocumented feature that -up worked */
 	    if (!strcmp(argptr, "p")) {
-		fd = coprocout;
-		if (fd < 0) {
+		fdarg= coprocout;
+		if (fdarg < 0) {
 		    zwarnnam(name, "-p: no coprocess");
 		    return 1;
 		}
 	    } else {
-		fd = (int)zstrtol(argptr, &eptr, 10);
+		fdarg = (int)zstrtol(argptr, &eptr, 10);
 		if (*eptr) {
 		    zwarnnam(name, "number expected after -%c: %s", 'u',
 			     argptr);
@@ -3819,8 +3819,8 @@ bin_print(char *name, char **args, Options ops, int func)
 	    }
 	}
 
-	if ((fd = dup(fd)) < 0) {
-	    zwarnnam(name, "bad file number: %d", fd);
+	if ((fd = dup(fdarg)) < 0) {
+	    zwarnnam(name, "bad file number: %d", fdarg);
 	    return 1;
 	}
 	if ((fout = fdopen(fd, "w")) == 0) {

pws


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

* Re: print to the terminal in zle
  2013-07-26 13:48 print to the terminal in zle Stephane Chazelas
  2013-07-26 17:05 ` Peter Stephenson
@ 2013-07-26 17:36 ` Frank Terbeck
  2013-07-27 16:20   ` Stephane Chazelas
  2013-07-26 19:39 ` Peter Stephenson
  2 siblings, 1 reply; 9+ messages in thread
From: Frank Terbeck @ 2013-07-26 17:36 UTC (permalink / raw)
  To: zsh-users

Hey Stephane,

Stephane Chazelas wrote:
> recently, Debian broke some of my scripts using ZLE in `zsh -i` by adding:

An interactive shell for a script? What's the reason for that?

>         function zle-line-init () {
>             emulate -L zsh
>             printf '%s' ${terminfo[smkx]}
>         }
>
> To /etc/zsh/zshrc
>
> That smkx  escape sequence is printed to stdout instead of the terminal.

Which terminal is that? The terminal should definitely react to smkx if
it has that sequence in its terminfo database entry. Are you maybe
changing $TERM to something the terminal cannot handle?

If not, what are you doing that uses zle and is not connected to a
terminal?

> What would be the correct way to do it?

There is also `echoti'. But it barfs if the $TERM for which the terminfo
db query was done doesn't have the key you asked for. But it does
exactly the same as the printf. It outputs the escape sequence to
stdout:

% echoti smkx | cat -v
^[[?1h^[=

> Doing `printf > /dev/tty` would probably do it but it would be
> better I think to be able to write to the fd that zsh currently
> has opened to the terminal (usually 10 if it  was free upon zsh
> startup).
>
> is there a way to do that?

Hm, I don't see how that would be better. Care to elaborate?

> Or an alternative way to set smkx on ZLE init and rmks  on
> zle-finish?

I think smkx/rmkx switching should always be done while the line editor
is active - directly in the zsh/zle module. I think this was discussed,
though, and it was decided to keep it the way it is in order to not
break existing setups.

Regards, Frank

-- 
In protocol design, perfection has been reached not when there is
nothing left to add, but when there is nothing left to take away.
                                                  -- RFC 1925


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

* Re: print to the terminal in zle
  2013-07-26 13:48 print to the terminal in zle Stephane Chazelas
  2013-07-26 17:05 ` Peter Stephenson
  2013-07-26 17:36 ` Frank Terbeck
@ 2013-07-26 19:39 ` Peter Stephenson
  2013-07-27  0:00   ` Bart Schaefer
  2013-07-27 16:30   ` Stephane Chazelas
  2 siblings, 2 replies; 9+ messages in thread
From: Peter Stephenson @ 2013-07-26 19:39 UTC (permalink / raw)
  To: zsh-users

On Fri, 26 Jul 2013 14:48:11 +0100
Stephane Chazelas <stephane.chazelas@gmail.com> wrote:
> recently, Debian broke some of my scripts using ZLE in `zsh -i` by adding:
> 
>         function zle-line-init () {
>             emulate -L zsh
>             printf '%s' ${terminfo[smkx]}
>         }
> 
> To /etc/zsh/zshrc
> 
> That smkx  escape sequence is printed to stdout instead of the terminal.

It's just occurred to me that maybe zle special functions should be run
with stdout and stdin set explicitly to the terminal.  Arguably they
have no business knowing about a redirected stdout or stdin since they
run in a context where that's meaningless.  stderr would be good enough
for debugging.

-- 
Peter Stephenson <p.w.stephenson@ntlworld.com>
Web page now at http://homepage.ntlworld.com/p.w.stephenson/


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

* Re: print to the terminal in zle
  2013-07-26 19:39 ` Peter Stephenson
@ 2013-07-27  0:00   ` Bart Schaefer
  2013-07-27 16:30   ` Stephane Chazelas
  1 sibling, 0 replies; 9+ messages in thread
From: Bart Schaefer @ 2013-07-27  0:00 UTC (permalink / raw)
  To: zsh-users

On Jul 26,  8:39pm, Peter Stephenson wrote:
} Subject: Re: print to the terminal in zle
}
} On Fri, 26 Jul 2013 14:48:11 +0100
} Stephane Chazelas <stephane.chazelas@gmail.com> wrote:
} > recently, Debian broke some of my scripts using ZLE in `zsh -i` by adding:

I have to admit that I'm with Frank in being confused about "scripts using
ZLE in `zsh -i`".

} It's just occurred to me that maybe zle special functions should be run
} with stdout and stdin set explicitly to the terminal.

I can't make up my mind about this one.  If anything is to change, they
should have stdin/out set to whatever ZLE is using, not necessarily to
"the terminal" (though perhaps those never differ?).

} Arguably they have no business knowing about a redirected stdout or
} stdin since they run in a context where that's meaningless.

To the extent that "zsh -i" can be used to force interactivity, isn't
the point of doing so to have the shell read standard input "as if" it
were a terminal device even when it is not one?


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

* Re: print to the terminal in zle
  2013-07-26 17:36 ` Frank Terbeck
@ 2013-07-27 16:20   ` Stephane Chazelas
  2013-07-27 18:58     ` Greg Klanderman
  0 siblings, 1 reply; 9+ messages in thread
From: Stephane Chazelas @ 2013-07-27 16:20 UTC (permalink / raw)
  To: Frank Terbeck; +Cc: zsh-users

2013-07-26 19:36:46 +0200, Frank Terbeck:
> Hey Stephane,
> 
> Stephane Chazelas wrote:
> > recently, Debian broke some of my scripts using ZLE in `zsh -i` by adding:
> 
> An interactive shell for a script? What's the reason for that?

See http://www.zsh.org/mla/users/2007/msg01187.html

The idea is just to use the zsh line editor in an application of
mine (there a wrapper around mairix (a mail indexer) to provide
with an intelligent prompt (with completion) for mutt).


> 
> >         function zle-line-init () {
> >             emulate -L zsh
> >             printf '%s' ${terminfo[smkx]}
> >         }
> >
> > To /etc/zsh/zshrc
> >
> > That smkx  escape sequence is printed to stdout instead of the terminal.
> 
> Which terminal is that? The terminal should definitely react to smkx if
> it has that sequence in its terminfo database entry. Are you maybe
> changing $TERM to something the terminal cannot handle?

The problem is not with the terminal, but with that code sending
the escape sequence to stdout (which doesn't have to be the
terminal, in my case, it's a pipe and I don't want to get that
smkx escape there (that's what breaking my script), instead of
the terminal

> If not, what are you doing that uses zle and is not connected to a
> terminal?

zle is connected to a terminal, not via stdout, zle doesn't use
stdout for display and should not which is the whole point of
this thread.

I was just asking if there was a way for a zle function to
output something to the terminal via the file descriptor that
zsh keeps to the terminal (which is not stdout).

Peter clearly answered the question in his first reply.

-- 
Stephane


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

* Re: print to the terminal in zle
  2013-07-26 19:39 ` Peter Stephenson
  2013-07-27  0:00   ` Bart Schaefer
@ 2013-07-27 16:30   ` Stephane Chazelas
  2013-07-27 17:02     ` Stephane Chazelas
  1 sibling, 1 reply; 9+ messages in thread
From: Stephane Chazelas @ 2013-07-27 16:30 UTC (permalink / raw)
  To: Peter Stephenson; +Cc: zsh-users

2013-07-26 20:39:31 +0100, Peter Stephenson:
> On Fri, 26 Jul 2013 14:48:11 +0100
> Stephane Chazelas <stephane.chazelas@gmail.com> wrote:
> > recently, Debian broke some of my scripts using ZLE in `zsh -i` by adding:
> > 
> >         function zle-line-init () {
> >             emulate -L zsh
> >             printf '%s' ${terminfo[smkx]}
> >         }
> > 
> > To /etc/zsh/zshrc
> > 
> > That smkx  escape sequence is printed to stdout instead of the terminal.
> 
> It's just occurred to me that maybe zle special functions should be run
> with stdout and stdin set explicitly to the terminal.  Arguably they
> have no business knowing about a redirected stdout or stdin since they
> run in a context where that's meaningless.  stderr would be good enough
> for debugging.
[...]

In my script (http://www.zsh.org/mla/users/2007/msg01187.html),
I need to be able to access stdout from a zle function, though I
could change it to use another fd (my modified "accept-line"
writes the $BUFFER to stdout that goes to a pipe) (and yes,
there was a reason to write it in such a convoluted way, but
that was years ago)

Given that ZLE can be used from "vared" to implement a line
editor for something else than the shell prompt, where a system
of functions can be defined by the user, it feels wrong to hide
stdout from those functions.

Think of

vared var | {
  some processing that interacts with what widgets write to
  stdout
}

Now, I agree it's a clean way to expose the terminal to the ZLE
widgets would they need to send/recv data to/from it, and one
can dup to another fd if they need to access the original
stdin/out.

vared var 3>&1 | ...

or use a coproc...

-- 
Stephane


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

* Re: print to the terminal in zle
  2013-07-27 16:30   ` Stephane Chazelas
@ 2013-07-27 17:02     ` Stephane Chazelas
  0 siblings, 0 replies; 9+ messages in thread
From: Stephane Chazelas @ 2013-07-27 17:02 UTC (permalink / raw)
  To: Peter Stephenson, zsh-users

2013-07-27 17:30:55 +0100, Stephane Chazelas:
[...]
> (and yes, there was a reason to write it in such a convoluted
> way, but that was years ago)
[...]

For the curious, details as to why (why zsh -i, why writing the
value in a ZLE widget, why the kill -HUP) are at
http://www.zsh.org/mla/users/2007/msg01025.html

Note that that was 6 years ago, there may be cleaner ways to
write it with newer zsh versions.

-- 
Stephane


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

* Re: print to the terminal in zle
  2013-07-27 16:20   ` Stephane Chazelas
@ 2013-07-27 18:58     ` Greg Klanderman
  0 siblings, 0 replies; 9+ messages in thread
From: Greg Klanderman @ 2013-07-27 18:58 UTC (permalink / raw)
  To: zsh-users

>>>>> On July 27, 2013 Stephane Chazelas <stephane.chazelas@gmail.com> wrote:

> The idea is just to use the zsh line editor in an application of
> mine (there a wrapper around mairix (a mail indexer) to provide
> with an intelligent prompt (with completion) for mutt).

I'm with ya Stephane, I have several similar types of scripts.
Very useful for building completion/history interface on top of some
program which has a dumb text interface.

Greg


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

end of thread, other threads:[~2013-07-27 18:58 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-07-26 13:48 print to the terminal in zle Stephane Chazelas
2013-07-26 17:05 ` Peter Stephenson
2013-07-26 17:36 ` Frank Terbeck
2013-07-27 16:20   ` Stephane Chazelas
2013-07-27 18:58     ` Greg Klanderman
2013-07-26 19:39 ` Peter Stephenson
2013-07-27  0:00   ` Bart Schaefer
2013-07-27 16:30   ` Stephane Chazelas
2013-07-27 17:02     ` Stephane Chazelas

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