home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume22 / pty / part04 / QUESTIONS < prev    next >
Text File  |  1990-10-09  |  4KB  |  103 lines

  1. I selected the questions below from articles posted to the USENET
  2. newsgroup comp.unix.questions. All the questions are reasonably easy to
  3. answer with pty and not too easy to answer with other widely available
  4. tools. So I hope this file is useful, and I hope other software authors
  5. adopt the QUESTIONS idea.
  6.  
  7. Organization: question, one-sentence answer, further details of
  8. answer, why the question normally poses a problem, and why the answer
  9. given solves the problem.
  10.  
  11.  
  12. 1. How to redirect telnet's input?
  13.  
  14. Run pty telnet instead of telnet. Caveat: telnet stupidly flushes its
  15. input at connect time, so be careful not to flood it; instead do
  16. something like % (sleep 5;echo help;echo quit) | pty telnet whatever 25.
  17. (Try it!) The five-second pause should be enough. For a more
  18. sophisticated (and reliable) technique, see #4 below.
  19.  
  20. The problem here is that telnet wants to take its input from a terminal.
  21. Most other interactive applications (e.g., editors) feel the same way
  22. about stdin, stdout, and stderr. pty solves this by setting input,
  23. output, and error all to the pseudo-terminal.
  24.  
  25.  
  26. 2. How to redirect su's input?
  27.  
  28. pty su < input, of course. (I certainly don't recommend putting your
  29. password in plaintext to be fed to su, but it can be done!) Note that
  30. some su's flush their input; solve this as in #1 above or #4 below.
  31.  
  32. The problem is that su opens /dev/tty for its input. Many other programs
  33. use /dev/tty explicitly for input and output; how do you redirect them?
  34. pty solves this because a program under a pseudo-terminal refers to that
  35. terminal when it opens /dev/tty.
  36.  
  37.  
  38. 3. How to make sed 's/foo/bar/g' | more work?
  39.  
  40. pty -0 sed 's/foo/bar/g' | more. You can abbreviate pty -0 as condom.
  41.  
  42. The problem is that sed uses stdio. stdio checks whether its output is a
  43. terminal; if not, it buffers a block of data inside the program. To see
  44. this in action, try the original % sed 's/foo/bar/g' | more; unless you
  45. type a lot of input, sed will keep buffering its output, so you'll never
  46. see anything.
  47.  
  48. pty solves the stdio buffer problem because a program under stdio does
  49. have a terminal (the pseudo-terminal) as output. So stdio buffers only a
  50. line at a time.
  51.  
  52.  
  53. 4. How to start a program, respond to its prompts, give the correct
  54.    replies, and catch the output?
  55.  
  56. On a machine with named pipes created by mknod foo p:
  57.  
  58.   #!/bin/sh
  59.   # Generic reader-writer.
  60.   (umask 077;mknod input p;mknod output p)
  61.   pty -0 program args < input | pty -0 tee record > output &
  62.   exec 4>input 5<output
  63.   # Now read prompts from <&5 and write replies to >&4.
  64.   # A transcript is kept in record.
  65.  
  66. Another solution is to create two (unnamed) pipes, then stick pty
  67. between them as above. This requires C code but is more portable.
  68.  
  69. The problems here are just the problems in #1-#3 above.
  70.  
  71.  
  72. 5. How to fool rn into processing KILL files in the background?
  73.  
  74. pty -T rn &. This does have one deficiency: control characters like ^C,
  75. ^Z, and so on affect pty rather than rn. (That's what -T does.) This
  76. doesn't matter for rn, but pty -T vi verylongfile definitely doesn't
  77. work the right way. To pass control characters through, start with
  78.  
  79.   % pty -s sh -c 'sessname;disconnect;pty vi verylongfile'
  80.  
  81. (pty -s can be abbreviated as sess.) Then watch the vi process with ps
  82. or by typing pty -sT reconnect xx, watching, then pressing ^C, where xx
  83. is the session name (pty extension). When you want to reconnect and pass
  84. control characters to vi, type % pty -s reconnect xx. From that point
  85. it'll feel just like a normal vi.
  86.  
  87. The problem is that rn---like all character-based interactive
  88. applications---wants to change the tty mode to read one character at a
  89. time instead of one line at a time. The tty driver doesn't let it do
  90. this in the background. pty -T rn solves this because rn is really in
  91. the foreground under the pty.
  92.  
  93.  
  94. 6. How to get terminal speed from shell script?
  95.  
  96. "`pty stty speed`", assuming fd 0 or fd 2 is the tty.
  97.  
  98. The problem is that a straight `stty speed` pipes stty's output away
  99. from your terminal and back into your shell. Many versions of stty
  100. assume that their output points to the terminal, so they blow up. pty
  101. solves this because it copies information from the real terminal to the
  102. pseudo-terminal.
  103.