tags:

views:

260

answers:

1

Hey, hopefully this should be my last PTY-related question and I can move onto more exciting issues. (c;

Here's a set of small functions I have written for creating and reading/writing to a pty: http://pastebin.com/m4fcee34d The only problem is that they don't work! After I run the initializer and writeToPty( "ls -l" ) , 'output' from readFromPty is still empty.

Ubuntu, QT C++

EDITED: Ok, I can confirm all this stuff works except for the read loop. In the debuggers' locals/watchers tab it shows that the QString 'output' actually does get the right data put in it, but after it ( the read() ) runs out of characters from the output it runs and then hangs. What is going on and how can I fix it? Thanks! (c:

#include <iostream>
#include <unistd.h>
#include <utmp.h>
#include <pty.h>

#include <QString>
#include <QThread>
// You also need libutil in your .pro file for this to compile.

class CMkPty
{
public:
    CMkPty( int *writeChannel, int *readChannel );
    ~CMkPty();
    int runInPty( char *command );
    int writeToPty( char *input );
    int readFromPty( QString output );
    int m_nPid;

private:
    int m_nMaster, m_nSlave, m_nPosition, m_nBytes;
    char *m_chName;
    void safe_print( char *s );
    char m_output;

};

CMkPty::CMkPty( int *masterFD, int *slaveFD )
{
    openpty( &m_nMaster, &m_nSlave, (char*)0, __null, __null );
    m_nPid = fork();
    *masterFD = m_nMaster;
    *slaveFD = m_nSlave;
    if( m_nPid == 0 )
    {
        login_tty( m_nSlave );
        execl( "/bin/bash", "-l", (char*)0 );
        return;
    }
    else if( m_nPid > 0 )
    {
        return;
    }
    else if( m_nPid < 0 )
    {
        std::cout << "Failed to fork." ;
        return;
    }
}
CMkPty::~CMkPty()
{
    close( m_nMaster );
    close( m_nSlave );
}
int CMkPty::writeToPty( char *szInput )
{
    int nWriteTest;
    write( m_nMaster, szInput, sizeof( szInput ) );
    nWriteTest = write( m_nMaster, "\n", 1 );
    if( nWriteTest < 0 )
    {
        std::cout << "Write to PTY failed" ;
        return -1;
    }
    return 0;
}
int CMkPty::readFromPty( QString output )
{
        char buffer[ 160 ];
        m_nBytes = sizeof( buffer );
        while ( ( m_nPosition = read( m_nMaster, buffer, m_nBytes ) ) > 0 )
        {
            buffer[ m_nPosition ] = 0;
            output += buffer;
        }
       return 0;
}

EDIT: Here's a link to the question with the code that finally worked for me.

+1  A: 

I'm note entirely familiar with posix, but after reading this page http://pwet.fr/man/linux/fonctions_bibliotheques/posix/read I had some insight. What's more, I don't see you adjusting your M_nBytes value if you haven't read as much as you were expecting on the first pass of the loop.

edit: from that link, perhaps this will be of some help:

If some process has the pipe open for writing and O_NONBLOCK is clear, read() shall block the calling thread until some data is written or the pipe is closed by all processes that had the pipe open for writing.

When attempting to read a file (other than a pipe or FIFO) that supports non-blocking reads and has no data currently available:

* If O_NONBLOCK is clear, read() shall block the calling thread until some data becomes available.

so essentially, if you're not in an error state, and you tell it to keep reading, it will block until it finds something to read.

San Jacinto