Hi,
I have written a qt program that is designed to perform the communication with another console program, which I otherwise would have to do myself. It is very simple:
process.start("<external terminal program>");
process.waitForStarted();
// read the program's output and react appropriately
while( process.
state() == QProcess::Running ) {
// the information could be either in the stout or sterr channel,
// so we need to poll these
for(int i=0; i<100; i++)
{
process.
setReadChannel(QProcess::StandardOutput);
process.waitForReadyRead(10);
stout = process.readAllStandardOutput();
if( !stout.isEmpty() )
break;
process.
setReadChannel(QProcess::StandardError);
process.waitForReadyRead(10);
sterr = process.readAllStandardError();
if( !sterr.isEmpty() )
break;
}
processOutput += stout + sterr;
if(processOutput.contains("successfully"))
{
qDebug("success");
process.waitForFinished(1000);
return 0;
}
if( stout.isEmpty() && sterr.isEmpty() )
{
qDebug("process does not answer. terminating.");
process.waitForFinished(1000);
return 1;
}
if(stout.contains("[y/n]"))
{
process.write("y\n");
}
if(stout.contains("Username:"))
{
process.write("<Username>\n");
}
if(sterr.contains("Password:"))
{
process.write("<Password>");
}
}
QProcess process;
process.start("<external terminal program>");
process.waitForStarted();
QString processOutput;
// read the program's output and react appropriately
while( process.state() == QProcess::Running )
{
// the information could be either in the stout or sterr channel,
// so we need to poll these
QString stout;
QString sterr;
for(int i=0; i<100; i++)
{
process.setReadChannel(QProcess::StandardOutput);
process.waitForReadyRead(10);
stout = process.readAllStandardOutput();
if( !stout.isEmpty() )
break;
process.setReadChannel(QProcess::StandardError);
process.waitForReadyRead(10);
sterr = process.readAllStandardError();
if( !sterr.isEmpty() )
break;
}
processOutput += stout + sterr;
if(processOutput.contains("successfully"))
{
qDebug("success");
process.waitForFinished(1000);
return 0;
}
if( stout.isEmpty() && sterr.isEmpty() )
{
qDebug("process does not answer. terminating.");
process.waitForFinished(1000);
return 1;
}
if(stout.contains("[y/n]"))
{
process.write("y\n");
}
if(stout.contains("Username:"))
{
process.write("<Username>\n");
}
if(sterr.contains("Password:"))
{
process.write("<Password>");
}
}
To copy to clipboard, switch view to plain text mode
Before someone starts a discussion about security and that one should not circumvent password prompt: This is not the topic here!
So the topic is, why this software does not run in a terminal. there, it keeps on waiting for a true user input, and ignores the pipe from the program. Is there a way to fix this?
The program runs fine in kdevelop, or when "clicking on the icon", and even when it is executed by a daemon (start-stop-daemon in an init script).
Bookmarks