Skip to content
This repository has been archived by the owner on Nov 3, 2022. It is now read-only.

No input received from terminal #11

Closed
xerz-one opened this issue Jan 12, 2015 · 15 comments
Closed

No input received from terminal #11

xerz-one opened this issue Jan 12, 2015 · 15 comments

Comments

@xerz-one
Copy link

I'm developing a simple shell. Because I wanted the execution of code to be synced, input is managed through readline-sync. However, I've found that trying to do anything (including ^C or ^D) is useless. For example, a very basic code like:

rl = require('readline-sync');
input = rl.prompt();

keeps reading without ever responding. The same happens with other commands like rl.question. I can only guess that V8 is unable to receive any input, for some reason.

I'm running a manually compiled node.js 0.10.35 on Ubuntu 14.04 LTS, using Bash and the GNOME Terminal.

@anseki
Copy link
Owner

anseki commented Jan 13, 2015

Hi, Gomez
Does "keeps reading" mean that a method doesn't return? It's not that receiving a empty text from a method?
That is, The "NEXT LINE" isn't shown by this code?:

var rl = require('readline-sync');
input = rl.prompt();
console.log('NEXT LINE');

@anseki
Copy link
Owner

anseki commented Jan 13, 2015

And, the prompt text (default: >) is shown?

@xerz-one
Copy link
Author

It stops executing at that point, trying to read input and keeping stuck. And the prompt is displayed correctly, so I am a 99'9% sure it has a problem with receiving input.

@anseki
Copy link
Owner

anseki commented Jan 14, 2015

Ummm... readlineSync works completely on my machine, there seems to be no problem.
(lubuntu-14.04.1, nodejs-v0.10.35, readline-sync-0.4.10)

The setup nodejs was no problem?
Because, I think that it's unusual that the ^C doesn't work.

Try this:

  1. Input below, and push enter key.
/bin/sh `node -e "console.log(require('path').dirname(require.resolve('readline-sync')))"`/read.sh
  1. Type abc, and push enter key.
  2. The abc is shown, and code is finished.

@xerz-one
Copy link
Author

That worked! Forgot to mention before - I had the problem while using the Node interpreter. So guess that's what happened. I was executing my code by invoking require() manually, and when I tried to debug, I never ran directly a script (take a look, there was no var when defining a variable)

Now - could this bug be fixed? It seems that the problem is that node.js may be trying to read the input twice, in two different ways simultaneously. Or something like that.

@anseki
Copy link
Owner

anseki commented Jan 14, 2015

Ah, you tried in REPL. I understood.
The any codes that handles stdin don't work in REPL because REPL handles stdin (TTY). That is, it's like emulating the shell.
This is not bug. This is design of nodejs. And, nobody want input on REPL.

Try this code in REPL:

process.stdin.pause()

@xerz-one
Copy link
Author

Ugh, I apologize. How simple. Guess I'll have to study better the APIs!

(and yup, here's someone who wants to read input from REPL. Working on what I consider a cool project. Hi!)

@xerz-one
Copy link
Author

Actually -- not working. It still keeps stuck. Here's the source code of the "real" thing:

var os = require('os');
var rl = require('readline-sync');
var child_process = require('child_process');
var sh = require('execSync');

var username = sh.exec("id -un").stdout.replace(os.EOL, '');
var hostname = os.hostname();

module.exports = {
    shell: function (){
        console.log( 'Kuen Shell v0.0.1' );
        console.log( '2015 Francisco Gómez <espectalll123@outlook.com>' + os.EOL );

        process.stdin.pause();
        var loop = true;

        while(loop) {
            rl.setPrompt(username + '@' + hostname + ': ');
            input = rl.prompt();
            try {
                var shExec = sh.exec(input);
                console.log(shExec.stdout);
            } catch(e) {};
        };
    }
}

@xerz-one xerz-one reopened this Jan 14, 2015
@anseki
Copy link
Owner

anseki commented Jan 15, 2015

If you want to read a line from another stream that is not REPL, I think that you must assign another channel (e.g. remote connection).
But, that idea is not good... Because the REPL is used for debug, that is a easy interactive environment. That will spoil REPL's good interactivity.
If you want to make the code run step by step for debug, the node debug might help you. Or, other debug tools like node-inspector.

So, your code seems to run with no problem in my machine. (some syntax errors are shown. but it's running.)
I tried input shell command (e.g. ls), then the response was shown at screen.
Doesn't sh.exec() method fail? because that depends on system environment.

Try this:

var os = require('os');
var rl = require('readline-sync');
var child_process = require('child_process');
//var sh = require('execSync');
// Dummy Object
var sh = {exec: function(cmd) {
    return {stdout: 'Res of: ' + cmd};
}};

var username = sh.exec("id -un").stdout.replace(os.EOL, '');
var hostname = os.hostname();

module.exports = {
    shell: function (){
        console.log( 'Kuen Shell v0.0.1' );
        console.log( '2015 Francisco Gómez <espectalll123@outlook.com>' + os.EOL );

        process.stdin.pause();
        var loop = true;

        while(loop) {
            rl.setPrompt(username + '@' + hostname + ': ');
            input = rl.prompt();
            try {
                var shExec = sh.exec(input);
                console.log(shExec.stdout);
            } catch(e) {};
        };
    }
}

@anseki
Copy link
Owner

anseki commented Jan 15, 2015

I didn't try running readlineSync on node debug. That result might be same as REPL.

@anseki
Copy link
Owner

anseki commented Jan 24, 2015

10 days have passed. I think that that problem was already solved, and I close the issue.
If not solved, please reopen it.

@anseki anseki closed this as completed Jan 24, 2015
@xerz-one
Copy link
Author

The bug itself still happens. However, I'm going to try out a different workaround for my idea. Thanks!

@anseki
Copy link
Owner

anseki commented Jan 29, 2015

Ok. 😊
If that bug you said is the bug of readlineSync, tell me please.

@CreeperDrop
Copy link

I don't think that's a bug in readline-sync because it happens also if you try a different loop such as a for loop.

@anseki
Copy link
Owner

anseki commented Jul 31, 2018

Hi @CreeperDrop, thank you for the comment.

Yes, that is not a bug. It was clearly indicated that there was the problem in @espectalll's environment. (e.g. REPL)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants