Arun
Arun

Reputation: 3731

How to integrate a C program runnable terminal to an online coding environment?

I have developed an online Coding platform for the most used languages like Java, C, etc. We can give the command/console inputs and get the output. But in this case, it will not hold the UI to get the input. But we need to give all the inputs in the input area and the program will give output accordingly.

This website is an example. We can enter the program and give the inputs in the "STDIN" tab. The output will be shown in the window.

But I saw two websites, onlinegdb and programiz. Here the UI is holding for the input if we are giving a scanf in the C program. This is not an actual terminal integration. Some WebSocket calls are happening to hold and accept the inputs.

I am using node.js and PHP to create my coding environment. How can I achieve this terminal like behaviour in my program? Can someone help with some existing examples? I am not getting any clue. Did a lot of searches.

Upvotes: 0

Views: 51

Answers (2)

vaughngx4
vaughngx4

Reputation: 198

To elaborate on the previous answer, creating a text like terminal in the UI and using chunked encoding to send data back and forth between the api and UI works for me.

The console will run behind your api and certain events like a keypress should trigger an update on the api to send the keypress and an update in the UI to receive the output from the api.

Running a script in the api in the same fashion looks something like:

//set header

res.setHeader('Content-Type', 'application/octect-stream');

//use chunked encoding (send data in chunks)

res.useChunkedEncodingByDefault = true;

//start process

let proc = child.spawn(cmd, [cmdArgs, cmdOptions]);

//start stream

proc.stdout.pipe(res);

//end stream when process stdout ends

proc.on('exit', () => {
   res.end();
});

//catch errors

req.on('error', function(err){
   console.error(err);
   res.status(500).json({ status: `error`})
});

Upvotes: 1

D.B.K
D.B.K

Reputation: 429

On the client side, create a terminal like interface and listen to user keypress events.. On the server side, open a pseudoterminal(ptty) and run the required program,.. Make sure you set up the bidirectional flow correctly. i.e, whatever is typed on the UI should be sent and fed to the ptty on the server, and whatever the ptty outputs, it should be displayed on the UI. The program on ptty will naturally halt and wait if there's any scanf. The user will enter the string in UI terminal, which will be fed to the ptty on the server and the program continues.

Source: https://dev.to/saisandeepvaddi/how-to-create-web-based-terminals-38d

Upvotes: 0

Related Questions