简体   繁体   中英

How to terminate my script if it asks for user input

I have been trying to implement an ide on my website. Like if I want to run this program:

/* add c headers if necessary*/
#include <stdio.h>
/* Include other headers as needed */
int main()
{

/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int a,b;
scanf("%d%d",&a,&b); // requires user input
return 0;
}

This program will get executed at my Linux server using some script.

As this program requires input from user,I have provided a textbox to user to provide that input. Then, I save that input in some file at my backend and supplying it through that file to my program and corresponding result is shown on my webpage.

But now I want user give input one by one.

For this I will need to catch when it requires input through my script then I will terminate my script and send response to user for that input.... Then will be feeding that input to program and again do the same if again some input require.

If something not clear please ask...

There is very similar question without a satisfying answer.

Maybe you could approach the problem from a different angle. Instead of

  • Send program's output only when the program stops
  • Ask user for input only when the program asks for input

you could send outputs and inputs immediately. In a terminal, the user can always type. Simulate that behavior on your website.

  • Send program's output incrementally, as soon as something was printed
  • User can always enter input. As soon as the user types something, that input is sent to the server.

Sending things immediately should be doable. There are websites that do send inputs and outputs immediately, for instance etherpads like this one (try it with a normal and a private browser window opened at the same time and site).

On your server, you can store the user's input and program's output in two temporary files. For one session, you could run something like the following script

# skeleton of the server script
compile program.c
createEmptyFiles input output
tail -f input | ./program > output

Additionally you need to feed the user's input into input and send the program's output to the user.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM