I want to detect when/if an application issues read(stdin, ...). Is it possible to use unbuffered I/O so that I could wait on write in external process?

The problem:

Some commands require user input when executed, I want to detect those and handle them because I'm targeting headless environment.

I'm also thinking whether the task isn't too broad and that the usage of read(stdin, ..) is simply too generic and not limited to requesting user input, what other set of constraints would also apply? I can think of:

  • Perform these checks only if the stdin hasn't been redirected to allow for | piping

  • Prompts are usually reading entire lines (have to press enter), so maybe try write a single char

Or is the input discarded without blocking if the application is not read(stdin, ...) at the moment the input is sent (that's just my rationale for yes spamming y\n in the terminal)?

  • If the goal is to feed the program appropriate input when it tries to read it, it's difficult to detect which piece of code in the program is doing the read. In general, you'd rely on the program first printing out something that says what sort of input it wants. And that's how the expect program does its job. – Mark Plotnick Mar 9 '15 at 15:16
  • You could try using a named pipe, and redirect standard input from that. Reads will then block until something writes to the pipe. I just don't understand what you mean with yes, the sole purpose of that tool is to send endless repeats of y\n (or whatever else) to its standard output. – wurtel Mar 9 '15 at 15:50
  • @MarkPlotnick thanks for the link for expect. The goal is sole detection that the application is requesting input from stdin (which cannot be satisfied because it's headless). – arul Mar 9 '15 at 16:06
  • @wurtel I'll take a look into named pipes, thanks. About yes - my train of assumptions has crashed, disregard that remark :) – arul Mar 9 '15 at 16:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.