Professional Web Applications Themes

Piping a terminal file descriptor - UNIX Programming

Is there a mechanism similar to pipe() which can transparently pipe terminal file descriptors? Since any file descriptors established by pipe() are stream file descriptors, they will not support term-relevant ioctl commands such as TCGETA. I would like to create a pipe in which I can catch termios commands such as tcgetattr() and pass them to the other end of the pipe where the controlling terminal is connected. Basically I am interested in a pipe mechanism which allows me to filter the data going to the output end of the terminal, while still allowing for control commands to pass transparently. ...

  1. #1

    Default Piping a terminal file descriptor


    Is there a mechanism similar to pipe() which can transparently
    pipe terminal file descriptors? Since any file descriptors
    established by pipe() are stream file descriptors, they will
    not support term-relevant ioctl commands such as TCGETA. I
    would like to create a pipe in which I can catch termios
    commands such as tcgetattr() and pass them to the other end
    of the pipe where the controlling terminal is connected.

    Basically I am interested in a pipe mechanism which allows
    me to filter the data going to the output end of the
    terminal, while still allowing for control commands to
    pass transparently. Shown below is a small code which
    illustrates the problem. It's easy to filter the data,
    but I have no idea how to catch certain control commands
    and pass them along the pipe. For instance, if the child
    process were to exec() to ``top'' instead of simply dumping
    a string, then top would assume that the new STDOUT (now
    one end of a pipe) is a not a terminal because its
    checks for terminal smartness (via requests through
    tcgetattr or ioctl) would fail.

    Thanks.

    % g++ main.C
    % ./a.out
    tcgettattr() on real STDOUT returns 0
    tcgettattr() on pipe returns -1
    THIS WAS LOWERCASE

    //////// main.C ////////
    #include <sys/types.h>
    #include <unistd.h>
    #include <termios.h>
    #include <iostream.h>

    int main()
    {
    int outPipe[2];
    pipe(outPipe);

    pid_t child = fork();

    if (child != 0) {
    struct termios settings;
    cerr << "tcgettattr() on real STDOUT returns "
    << tcgetattr(STDOUT_FILENO, &settings) << endl;

    close(STDIN_FILENO);
    close(outPipe[1]);

    //
    // Trivial filter which converts data arriving through pipe
    // from lower case to upper
    //
    while (1) {
    static char buf[1024];
    int inCount = read(outPipe[0], buf, 1024);

    if (inCount < 0)
    break;
    else if (inCount > 0) {
    for (int i=0; i<inCount; i++) {
    if (buf[i] >= 'a' && buf[i] <= 'z')
    buf[i] -= 32;
    }
    write(STDOUT_FILENO, buf, inCount);
    break;
    }
    }
    }
    else {
    sleep(1);

    //
    // Replace stdout with one end of the pipe
    //
    dup2(outPipe[1], STDOUT_FILENO);
    close(outPipe[0]);
    close(outPipe[1]);

    char *msg = "this was lowercase\n";
    write(STDOUT_FILENO, msg, strlen(msg));

    //
    // Even though the data are connected through the pipe in the
    // parent process, terminal related control commands cannot
    // be handled and return an error value.
    //
    struct termios settings;
    cerr << "tcgettattr() on pipe returns "
    << tcgetattr(outPipe[1], &settings) << endl;
    }

    return 0;
    }
    Ar Guest

  2. #2

    Default Re: Piping a terminal file descriptor

    in comp.unix.programmer i read:
     

    you need a pty. libexcept provides a nice api.

    --
    a signature
    those Guest

  3. #3

    Default Re: Piping a terminal file descriptor

    <net> wrote: 

    Thanks. I will look into that. But won't the pty
    absorb the control calls itself?
    Ar Guest

Similar Threads

  1. #40214 [NEW]: Bad File Descriptor, Please help me
    By support at vikoshosting dot com in forum PHP Bugs
    Replies: 1
    Last Post: January 24th, 09:28 AM
  2. file descriptor problem with tcpclient
    By Bill in forum PHP Development
    Replies: 2
    Last Post: November 18th, 03:17 PM
  3. Piping to windows command line .rb file doesn't work
    By Mills Thomas (app1tam) in forum Ruby
    Replies: 9
    Last Post: October 17th, 02:04 AM
  4. Dup 2: Bad File Descriptor...Whuzzat mean?
    By Michael in forum Linux Setup, Configuration & Administration
    Replies: 4
    Last Post: September 25th, 03:25 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139