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

tancen

New Member
Joined
Sep 28, 2020
Messages
3
Reaction score
0
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.
 


66109DF3-B072-43F2-82CC-238BA368785F.png

I don’t understand.....Can you explain a little more about what you’re trying to do?
 
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

  • 1.jpg
    1.jpg
    31.1 KB · Views: 244
  • 2.jpg
    2.jpg
    123.1 KB · Views: 246


Top