2

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...

7
  • I think you are on wrong site. You should ask programming questions on the main site: stackoverflow. Commented Feb 19, 2018 at 14:51
  • You should probably let the user input anything on stdin whether it will be used or not. At least that's what online IDEs I'm familiar with (ideone.com, tio.run) chose to do. Commented Feb 19, 2018 at 15:55
  • Maybe the following approach works: Create an empty file tmp. Call the programm with tail -f tmp | userprogram. Whenever the user enters something, append his/her input to tmp. Commented Feb 19, 2018 at 16:27
  • @Socowi - But problem is how would i know program is asking for sinput so that i can prompt user to enter input and then append it to tmp file. Commented Feb 20, 2018 at 5:15
  • @Aaron - I have already given that functionality but suppose sometime a program runs like that while(1){ // enter 2 to exit }.. it will continuously ask for input Commented Feb 20, 2018 at 5:18

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

6 Comments

@Socowi- Thanks for this, i can somehow use this approach.
I don't think using files will help btw : you might as well interact directly with the spawned process' stdin / stdout & stderr as far as I can tell.
@Aaron Of course a solution without files (if possible) would be better.
@Socowi "tail -f input | ./program > output " , because of this program always wait for some triggering even if program is reading nothing.In this case user will have to enter some input to finally make that program exit which is not a good idea because program was never asking for input.
@user8756809 Did you test what you said? ./program should exit if it does not require input. Try it: program() { echo "no input required"; }; tail -f <(echo file content) | program. The example prints no input required and exits. However, I'm not happy with my answer as well. Un-accept my answer to show that you are still waiting for better answers.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.