zsh-users
 help / color / mirror / code / Atom feed
From: Peter Stephenson <p.stephenson@samsung.com>
To: zsh-users@zsh.org
Subject: Re: print to the terminal in zle
Date: Fri, 26 Jul 2013 18:05:04 +0100	[thread overview]
Message-ID: <20130726180504.3243e220@pwslap01u.europe.root.pri> (raw)
In-Reply-To: <20130726134811.GA14136@chaz.gmail.com>

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


  reply	other threads:[~2013-07-26 17:15 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-07-26 13:48 Stephane Chazelas
2013-07-26 17:05 ` Peter Stephenson [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20130726180504.3243e220@pwslap01u.europe.root.pri \
    --to=p.stephenson@samsung.com \
    --cc=zsh-users@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).