1

How can I acknowledge the reading of an active std:cin call of a running C++ program from within another console?

Please: This IS NO DUPLICATE of e.g. Simulating ENTER keypress in bash script, because I don't want to input text to the program itself but to the std:cin that is running within a loop of that program.

So, the running program waits in the console by saying "Enter option:". Manually I would enter something and then press enter.

Entering text via

echo "some text" > /proc/18553/fd/0

works but I cannot redirect the enter-key to enter/end the text as I can by physically hitting the enter key.

This fellow here has or had the same problem:

Send a string and Enter key to a Node.js script [closed]

marc wellman
  • 5,808
  • 5
  • 32
  • 59
  • I never tried it, but wouldn't `printf 'some text\r' >/proc/18553/fd/0` produce something which should be understood as enter key? – user1934428 Sep 12 '22 at 11:22
  • @user1934428 thank you for your answer, but unfortunately not. I tried it and it behaves in the same way as echo... does. – marc wellman Sep 12 '22 at 11:24
  • Did you try the solution posted in the answer you are linking to in your question, i.e. replacing the `>` by `|`? – user1934428 Sep 12 '22 at 11:29
  • of course I did. I am always ending up with the same result, which is that I can redirect line breaks, which are also displayed correctly, but the program I am sending towards is still reading from std::cin. – marc wellman Sep 12 '22 at 11:32
  • Not really my area of knowledge, and I find your question interesting. I'm not sure that you can inject something into a stdin which is already being processed. Even `expect` requests you that you take over the control of the stdin initially, and at some point you can give up the control. You do the opposite. For instance, what happens with "type ahead"? Someone already blindly types something on the console, but the program is not at the point to have it consumed it yet. Now you are sending something as well. Who gets priority? – user1934428 Sep 12 '22 at 11:49
  • yes it's tricky indeed, but what surprises me is that this seems like a standard use case to me and still it seems so tricky :-) However, since this turned out to be a dead end I needed to start exploring alternative solutions for my problem. But thank you very much for your comments and your time! :-) – marc wellman Sep 12 '22 at 11:54
  • Actually, I have never had such a use case. If you can change the C++ program, you could think of providing such a feature there (kind of "automatic run method" which does not ask back). If you **do** need human interaction from a different terminal (maybe from a remote host), I would consider the following approach: Have your C++ program under complete control of i.e. `expect`. But since **you** write the expect program, you can make it clever and allow various input sources (for instance, via a socket interface or whatever). – user1934428 Sep 12 '22 at 11:58
  • it's not quite clear what your real use case is. if you want to do that manually you can use [tag:gnu-screen] or [tag:tmux]. if you want to do that in a programmable way you can use tools like [tag:expect] (for Tcl), [tag:pexpect] (for Python) or you can try my [sexpect](https://github.com/clarkwang/sexpect/) if you prefer shell. – sexpect - Expect for Shells Sep 12 '22 at 12:34
  • 2
    and note that `echo "some text" > /proc/18553/fd/0` did not actually work as you may think. it's outputting to the tty rather than feeding data to the tty. – sexpect - Expect for Shells Sep 12 '22 at 12:39
  • 1
    FWIW, I ran a web search on `send write to other pid stdin` and came up with several hits of interest, eg: [Writing to stdin of a process](https://unix.stackexchange.com/a/385782) which uses a fifo to feed text into the program's stdin; my test program is simply `while read -r line; do echo "tp: ${line}"; done`; if I send `printf "abc\n" >> mypipe` then program prints `tp: abc`; if I send `printf "abc" >> mypipe` then program does nothing (since it doesn't see a `\n`) ... then if I send `printf "\n" >> mypipe` the program then prints `tp: abc` ... – markp-fuso Sep 12 '22 at 13:46
  • having said that ... it may help if you could provide a working example of your use case; does your program need to receive input from both its own terminal/stdin as well as a 'remote' source? – markp-fuso Sep 12 '22 at 13:47

0 Answers0