0

I'm clearly not expecting the right results.

$ man less

In another terminal:

$ ps u 
# Find that pid of less is 45783
$ lsof -p 45783
COMMAND   PID USER   FD   TYPE             DEVICE  SIZE/OFF    NODE NAME
less    45783 lust  cwd    DIR                1,4       442      46 /usr/share/man
less    45783 lust  txt    REG                1,4    137712    9698 /usr/bin/less
less    45783 lust  txt    REG                1,4    600576    9397 /usr/lib/dyld
less    45783 lust  txt    REG                1,4 303120384 2911813 /private/var/db/dyld/dyld_shared_cache_x86_64
less    45783 lust    0   PIPE 0xffffff8025bf38c0     16384
less    45783 lust    1u   CHR               16,2   0t82970     763 /dev/ttys002
less    45783 lust    2u   CHR               16,2   0t82970     763 /dev/ttys002
less    45783 lust    3r   REG                1,4      4589  286688 /private/etc/man.conf
less    45783 lust    4r   CHR                2,0       0t0     306 /dev/tty
$ echo "q" > /dev/ttys002

At this point the terminal that man less is running in prints q and a newline, rather than quitting the display of the manpage.

How can I make it send the raw "q"? Piping does not work, I get "zsh: permission denied: /dev/ttys002". Switching echo for cat and typing it in did not work either.

Zombo
  • 1
  • 62
  • 391
  • 407
Steven Lu
  • 41,389
  • 58
  • 210
  • 364
  • It seems like you are not interested in sending output to a tty as your question's title says but rather in injecting input into a tty — a very different thing! – Celada Apr 16 '13 at 14:53
  • @Celada Sending **output from a program** *as input to a terminal session*? – Steven Lu Apr 16 '13 at 15:30
  • Indeed. As I said, that is very different from sending output to a terminal! – Celada Apr 16 '13 at 16:08

1 Answers1

0

MAybe what you want is a coprocesses. zsh supports those.

 coproc bc -l
 print -p 1 + 1
 read -p answer
 echo $answer
 2

This allows you to control stdin of the child process via write -p

Try this: it is just a sample bit of code, modify it as you want. It writes to the input queue of a terminal, /dev/pty3 or whatever. Run as root only. usage: ./puttty /dev/pty3

int main(int argc, char **argv)  
{                                                 
   int tty = open(argv[1], O_WRONLY|O_NONBLOCK);
   char ch[80]={0x0};
   char *p=NULL;
   while( fgets(ch, 80, stdin)!=NULL)  //Read keyboard input e.g., q<return>
   {
      for(p=ch; *p && *p!='\n'; p++)
         ioctl(tty, TIOCSTI, p);    // put each byte into /dev/pty3  input queue.                    
   }                       
   close(tty); // ctrl-Z  ends the program.                                      
   return 0;                                         
}       
jim mcnamara
  • 16,005
  • 2
  • 34
  • 51
  • Interesting. I did not know about coprocs. However I am in need of controlling not just stdin (I am far more comfortable using pipes if all I need is send to stdin) but to actually translate the interactive tty used by the pager program. It appears that the only reasonable way to accomplish this is spawning the pager program over a PTY. – Steven Lu Apr 16 '13 at 18:50
  • You want interposition between/among stdout, stdin, stderr of a process to some other process? In other words do you want to examine everything that "goes to and comes from" another tty? – jim mcnamara Apr 16 '13 at 19:00
  • Yeah, I want to translate what goes in, and display out what it normally does. It essentially requires a pseudoterminal pty, so I'm building one with Perl IO::Pty::Easy now. – Steven Lu Apr 17 '13 at 21:15