zsh-users
 help / color / mirror / code / Atom feed
* piping question
@ 2014-10-02 14:13 Helmut Jarausch
  2014-10-03  2:29 ` Bart Schaefer
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: Helmut Jarausch @ 2014-10-02 14:13 UTC (permalink / raw)
  To: zsh-users

Hi,

I have a command 'most' which is similar to 'less'.
Now I'd like to write a shell script 'xmost' which
should do the same as 'most' but with its output sent to a newly
generated xterm.

If I say

xterm -g 180x30+0+0  -hold -e most

I cannot pipe into it anymore, like

ls -l | xmost

Can this be achieved?

Many thanks for a hint,
Helmt.


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

* Re: piping question
  2014-10-02 14:13 piping question Helmut Jarausch
@ 2014-10-03  2:29 ` Bart Schaefer
       [not found]   ` <1412326536.9490.1@numa-i>
  2014-10-03 13:36 ` Kurtis Rader
  2014-10-03 14:42 ` Mikael Magnusson
  2 siblings, 1 reply; 7+ messages in thread
From: Bart Schaefer @ 2014-10-03  2:29 UTC (permalink / raw)
  To: zsh-users

On Oct 2,  4:13pm, Helmut Jarausch wrote:
}
} Now I'd like to write a shell script 'xmost' which
} should do the same as 'most' but with its output sent to a newly
} generated xterm.

xterm is going to reset the stdin/stdout of whatever command it runs
to be the terminal input/output, so you can't pipe directly into a
command that's running under xterm.

So the trick is that you have to capture the output in a file and
pass the file's name to the command running in the xterm.  Zsh will
automatically create an appropriate file name for you by using the
process substitution syntax.

So try something like this:

    xmost() {
	() { xterm -g 180x30+0+0  -hold -e "most < $1" } <(cat)
    }

If you don't have a recent zsh with anonymous functions, you'll need
a helper function of some kind to capture the file name created by
<(cat) and pass it through to xterm.


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

* Re: piping question
  2014-10-02 14:13 piping question Helmut Jarausch
  2014-10-03  2:29 ` Bart Schaefer
@ 2014-10-03 13:36 ` Kurtis Rader
  2014-10-03 14:42 ` Mikael Magnusson
  2 siblings, 0 replies; 7+ messages in thread
From: Kurtis Rader @ 2014-10-03 13:36 UTC (permalink / raw)
  To: Helmut Jarausch; +Cc: Zsh Users

[-- Attachment #1: Type: text/plain, Size: 1203 bytes --]

As Bart pointed out xterm is going to establish a new pty via the getpty(),
posix_openpt() or equivalent API. It then attaches stdin, stdout, stderr to
that pty before launching the shell or command specified by the -e option.
By design there is no way to coerce xterm to act like a pipeline filter.

In addition to Bart's suggestion you can leverage the /proc filesystem if
you're doing this on Linux, *BSD, or other UNIX implementation that
provides it. Your xmost script can examine /proc/$$/fd/0. If it isn't
associated with a pty it can arrange for it to be the stdin of the command
running in the new xterm. Specifics are left as an exercise for the student.

On Thu, Oct 2, 2014 at 7:13 AM, Helmut Jarausch <
jarausch@igpm.rwth-aachen.de> wrote:

> Hi,
>
> I have a command 'most' which is similar to 'less'.
> Now I'd like to write a shell script 'xmost' which
> should do the same as 'most' but with its output sent to a newly
> generated xterm.
>
> If I say
>
> xterm -g 180x30+0+0  -hold -e most
>
> I cannot pipe into it anymore, like
>
> ls -l | xmost
>
> Can this be achieved?
>
> Many thanks for a hint,
> Helmt.
>



-- 
Kurtis Rader
Caretaker of the exceptional canines Junior and Hank

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

* Re: piping question
  2014-10-02 14:13 piping question Helmut Jarausch
  2014-10-03  2:29 ` Bart Schaefer
  2014-10-03 13:36 ` Kurtis Rader
@ 2014-10-03 14:42 ` Mikael Magnusson
  2014-10-03 15:23   ` Bart Schaefer
  2 siblings, 1 reply; 7+ messages in thread
From: Mikael Magnusson @ 2014-10-03 14:42 UTC (permalink / raw)
  To: Helmut Jarausch; +Cc: Zsh Users

On 2 October 2014 16:13, Helmut Jarausch <jarausch@igpm.rwth-aachen.de> wrote:
> Hi,
>
> I have a command 'most' which is similar to 'less'.
> Now I'd like to write a shell script 'xmost' which
> should do the same as 'most' but with its output sent to a newly
> generated xterm.
>
> If I say
>
> xterm -g 180x30+0+0  -hold -e most
>
> I cannot pipe into it anymore, like
>
> ls -l | xmost
>
> Can this be achieved?
>
> Many thanks for a hint,
> Helmt.

echo hi there | { xterm -e 'most <& 7' 7< <(cat) }

I forget the syntax right now for allocating an appropriate free fd,
but 7 should be free most of the time ;) It might also be possibly to
use some 7<&1 thing to avoid the extra cat, but 7<&1 isn't it.

-- 
Mikael Magnusson


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

* Re: piping question
       [not found]   ` <1412326536.9490.1@numa-i>
@ 2014-10-03 15:04     ` Bart Schaefer
  0 siblings, 0 replies; 7+ messages in thread
From: Bart Schaefer @ 2014-10-03 15:04 UTC (permalink / raw)
  To: zsh-users

On Oct 3, 10:55am, Helmut Jarausch wrote:
> Subject: Re: piping question
>
> On 10/03/2014 04:29:10 AM, Bart Schaefer wrote:
> > 
> >     xmost() {
> > 	() { xterm -g 180x30+0+0  -hold -e "most < $1" } <(cat)
> >     }
> > 
> 
> Many thanks, that works just fine.

For completeness:

    xmost() {
      set - "${(qq)@}"  # Requote command line arguments
      if [[ -t 0 ]]
      then
        xterm -g 180x30+0+0  -hold -e "most $*"
      else
        () { xterm -g 180x30+0+0  -hold -e "most $* < $1" } <(cat)
      fi
    }

-- 
Barton E. Schaefer


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

* Re: piping question
  2014-10-03 14:42 ` Mikael Magnusson
@ 2014-10-03 15:23   ` Bart Schaefer
  2014-10-03 15:42     ` Bart Schaefer
  0 siblings, 1 reply; 7+ messages in thread
From: Bart Schaefer @ 2014-10-03 15:23 UTC (permalink / raw)
  To: Mikael Magnusson; +Cc: Zsh Users

On Oct 3,  4:42pm, Mikael Magnusson wrote:
}
} echo hi there | { xterm -e 'most <& 7' 7< <(cat) }

I avoided that because I was concerned that some terminal emulators
would close all the descriptors above 2 when launching the command.

If it works, though, you can use the {varname}<& syntax to obtain
a free descriptor, and then you don't need <(cat).

    xmost() {
      set - "${(qq)@}"  # Requote command line arguments
      if [[ -t 0 ]]
      then
        xterm -g 180x30+0+0  -hold -e "most $*"
      else
        local stdin
        exec {stdin}<&0
        xterm -g 180x30+0+0  -hold -e "most $* <&$stdin"
      fi
    }

It's possible the [[ -t 0 ]] branch is extraneous with this trick, but
it seems prudent not to have xmost reading from two different ttys.


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

* Re: piping question
  2014-10-03 15:23   ` Bart Schaefer
@ 2014-10-03 15:42     ` Bart Schaefer
  0 siblings, 0 replies; 7+ messages in thread
From: Bart Schaefer @ 2014-10-03 15:42 UTC (permalink / raw)
  To: Zsh Users

On Oct 3,  8:23am, Bart Schaefer wrote:
}
}         local stdin
}         exec {stdin}<&0
}         xterm -g 180x30+0+0  -hold -e "most $* <&$stdin"

Should probably end that with

          exec {stdin}<&-

to avoid leakage.


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

end of thread, other threads:[~2014-10-03 15:42 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-10-02 14:13 piping question Helmut Jarausch
2014-10-03  2:29 ` Bart Schaefer
     [not found]   ` <1412326536.9490.1@numa-i>
2014-10-03 15:04     ` Bart Schaefer
2014-10-03 13:36 ` Kurtis Rader
2014-10-03 14:42 ` Mikael Magnusson
2014-10-03 15:23   ` Bart Schaefer
2014-10-03 15:42     ` Bart Schaefer

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