Results 1 to 9 of 9

Thread: software that can't be run from a terminal

  1. #1
    Join Date
    Jul 2010
    Posts
    21
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default software that can't be run from a terminal

    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:

    Qt Code:
    1. QProcess process;
    2. process.start("<external terminal program>");
    3. process.waitForStarted();
    4. QString processOutput;
    5.  
    6. // read the program's output and react appropriately
    7. while( process.state() == QProcess::Running )
    8. {
    9. // the information could be either in the stout or sterr channel,
    10. // so we need to poll these
    11. QString stout;
    12. QString sterr;
    13. for(int i=0; i<100; i++)
    14. {
    15. process.setReadChannel(QProcess::StandardOutput);
    16. process.waitForReadyRead(10);
    17. stout = process.readAllStandardOutput();
    18. if( !stout.isEmpty() )
    19. break;
    20.  
    21. process.setReadChannel(QProcess::StandardError);
    22. process.waitForReadyRead(10);
    23. sterr = process.readAllStandardError();
    24. if( !sterr.isEmpty() )
    25. break;
    26. }
    27.  
    28. processOutput += stout + sterr;
    29.  
    30. if(processOutput.contains("successfully"))
    31. {
    32. qDebug("success");
    33. process.waitForFinished(1000);
    34. return 0;
    35. }
    36.  
    37. if( stout.isEmpty() && sterr.isEmpty() )
    38. {
    39. qDebug("process does not answer. terminating.");
    40. process.waitForFinished(1000);
    41. return 1;
    42. }
    43.  
    44. if(stout.contains("[y/n]"))
    45. {
    46. process.write("y\n");
    47. }
    48.  
    49. if(stout.contains("Username:"))
    50. {
    51. process.write("<Username>\n");
    52. }
    53.  
    54. if(sterr.contains("Password:"))
    55. {
    56. process.write("<Password>");
    57. }
    58. }
    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).

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: software that can't be run from a terminal

    Quote Originally Posted by Gh0str1d3r View Post
    Before someone starts a discussion about security and that one should not circumvent password prompt: This is not the topic here!
    Is it the topic here that you are reinventing the wheel?

    http://expect.sourceforge.net/

    So the topic is, why this software does not run in a terminal.
    What does "does not run in a terminal" mean?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Jul 2010
    Posts
    21
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: software that can't be run from a terminal

    yes, probably I reinvented the wheel. Sometimes it requires more effort to search for an existing solution rather than quickly solve it yourself.

    "does not run in a terminal" means that the automatic answering of the prompts is not performed when I run the software from a shell. That is, it still waits for a true user input, which is not what it should do.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: software that can't be run from a terminal

    Quote Originally Posted by Gh0str1d3r View Post
    yes, probably I reinvented the wheel. Sometimes it requires more effort to search for an existing solution rather than quickly solve it yourself.
    I guess the "quickly" idea backfired then It's not hard to do something "quickly wrong" and when facing a similar problem in future do it "quickly wrong" again but effectively you waste more time than if you did a proper reaseach first looking for a ready solution that would work in both cases.

    "does not run in a terminal" means that the automatic answering of the prompts is not performed when I run the software from a shell.
    So the software runs but it does not work the way you want it, yes?

    That is, it still waits for a true user input, which is not what it should do.
    Maybe the process you spawn detects it has a terminal available and expects the input to come from the terminal and not stdin.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Jul 2010
    Posts
    21
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: software that can't be run from a terminal

    Quote Originally Posted by wysota View Post
    I guess the "quickly" idea backfired then It's not hard to do something "quickly wrong" and when facing a similar problem in future do it "quickly wrong" again but effectively you waste more time than if you did a proper reaseach first looking for a ready solution that would work in both cases.
    Maybe you are write. Still, this way I may as well learn something about Qt. The reason why I am asking is because I don't want to do it wrong.

    Quote Originally Posted by wysota View Post
    So the software runs but it does not work the way you want it, yes?
    yes.

    Quote Originally Posted by wysota View Post
    Maybe the process you spawn detects it has a terminal available and expects the input to come from the terminal and not stdin.
    I guess so. So the original question could equally have been "how do I have to modify my code to force the external process to expect its input from stdin?"

    btw: why does the biggest and most highlighted button (i.e. "Reply to Thread") irreversibly delete the text that one has written using "quick reply"?? very frustrating...

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: software that can't be run from a terminal

    Quote Originally Posted by Gh0str1d3r View Post
    So the original question could equally have been "how do I have to modify my code to force the external process to expect its input from stdin?"
    Detach your program from the terminal (using native api) before spawning the child process.

    btw: why does the biggest and most highlighted button (i.e. "Reply to Thread") irreversibly delete the text that one has written using "quick reply"?? very frustrating...
    True.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Jul 2010
    Posts
    21
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: software that can't be run from a terminal

    thanks, running

    Qt Code:
    1. # ./a.out&
    To copy to clipboard, switch view to plain text mode 

    (i.e. with the &) works!

    EDIT: no, it doesn't, it only looks good at first glance.
    Last edited by Gh0str1d3r; 7th October 2010 at 14:29.

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: software that can't be run from a terminal

    That's not the most straightforward way. Use fork(), daemon(), setsid() or something similar in your program.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    Jul 2010
    Posts
    21
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: software that can't be run from a terminal

    many thanks, these commands were completely new to me. now it really works (with the &, this was not true).

Similar Threads

  1. Opening A Terminal using QProcess
    By sujan.dasmahapatra in forum Qt Programming
    Replies: 1
    Last Post: 3rd February 2011, 15:51
  2. terminal widget
    By kernel_panic in forum Qt Programming
    Replies: 11
    Last Post: 21st November 2009, 15:08
  3. Terminal into Qt application
    By paF4uko in forum Qt Programming
    Replies: 4
    Last Post: 27th February 2009, 12:22
  4. help about terminal window
    By andyyeng in forum Qt Programming
    Replies: 0
    Last Post: 29th October 2008, 16:22
  5. Looking for widget to run terminal app
    By marcell in forum Qt Programming
    Replies: 0
    Last Post: 16th May 2008, 17:24

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.