How to parse Linux(Ubuntu) PTY bash output data?

tancen

New Member
Credits
24
I am making a program to show the Linux(Ubuntu) PTY bash output data on the windows console. But, It doesn't look friendly to show some bytes such as color bytes. Have a protocol document to indicate how to parse it?
Thanks.
 


tancen

New Member
Credits
24
I made two programs that a pty server in Linux and a pty client in windows, They are communicating over TCP/IP.
The server program's code like below:

C++:
server->onBufferReceived = [&mpty](EasyTCP::IConnection* c, EasyTCP::AutoBuffer data)
    {
        int ret = write(mpty, data.data(), data.size());
        ...
    };
   
    ret = openpty(&mpty, &spty, NULL, NULL, NULL);
    if (ret == -1)
    {
        fprintf(stderr, "openpty failed, err[%d]: %s", ret, strerror(errno));
        return 1;
    }

    printf("%s\n", ttyname(spty));

    pid_t child = fork();
    if (child == -1)
    {
        fprintf(stderr, "fork failed, err[%d]: %s", ret, strerror(errno));
        return 1;
    }

    if (child == 0)
    {
        login_tty(spty);
        execl("/bin/bash", "");
        return 0;
    }
    assert(server->open(7777));
    ...
   
    if (FD_ISSET(mpty, &rset))
    {
        int n = read(mpty, buf, sizeof(buf) - 1);
        ...

        buf[n] = 0;
        ...
        assert(connection->send(sendBuf));
    }
The client program's code like below:

C++:
client->onBufferReceived = [](EasyTCP::IConnection* c, EasyTCP::AutoBuffer data)
    {
        printf(data.data());
        fflush(stdout);

        data.resize(0);
        assert (c->recv(data, false));
    };

    client->onDisconnected = [](EasyTCP::IConnection* )
    {
        fprintf(stderr, "disconnected");
    };

    client->connect("xxxxxx", xxxx);
    ....
    int k = getchar();
    if (k < 0 || k > 255)
    {
        fprintf(stderr, "getchar k = %d", k);
        continue;
    }
    char c = (char)k;

    EasyTCP::AutoBuffer sendBuf(&c, 1);
    sendBuf.resize();
    assert (client->send(sendBuf));
But the client program shows some bytes not friendly.
 

Attachments


Members online


Latest posts

Top