Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: print to the terminal in zle



On Fri, 26 Jul 2013 14:48:11 +0100
Stephane Chazelas <stephane.chazelas@xxxxxxxxx> 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



Messages sorted by: Reverse Date, Date, Thread, Author