Sign in to follow this  
Followers 0
skroo

How should I launch child console proccess with unknown prompts?

13 posts in this topic

For example I launch php scripts. They read stdin and execute some actions depending on my input.

For console apps I use RunWait, but it fails when child proccess tries to read stdin.

I've read a lot of threads. Many users suggest launch via Run() with infinite loops reading stdout.

But when I launch php scripts I don't know what messages I have to catch.

I found this thread: '?do=embed' frameborder='0' data-embedContent>>

But answer there is unclear for me.

Summary: I want to interact with child proccesses with prompts the same way if they were launched directly from console w/o (parent) autoit script. Is it possible?

Share this post


Link to post
Share on other sites



Anyone?

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Well.

3rd-party console script.php:

<?php

fwrite(STDOUT, "Enter string: ");
$input = fgets(STDIN, ...);

if ($input === 'yes') {
    do_action();
}

My autoit console app:

RunWait('php script.php', ...)

When I launch my app then php's script writes prompt text "Enter string: ", but automatically skips stdin - i can't type "yes".

Edited by skroo

Share this post


Link to post
Share on other sites

tell me if i am reight

Use Stdinout to get the text

use this to send a text

i am using CLS (the clear screen on DOS)

Run("cmd.exe") ; php progrem
Sleep(1000)
ControlSend("[CLASS:ConsoleWindowClass]","","","cls{ENTER}type code here")

but i hope this will help you

read this also

Window Titles and Text (Advanced) if you know the title and INSTANCE

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

I know it's possible to send something somewhere.

But I'd like to avoid this method. Sending "yes" is just an example. And first of all I need enter answers manually but not send them.

Scripts are 3rd-party and I don't know all possible answers I have to enter.

Again: I want the simplest solution as possible. When I open cmd.exe and there run my php script.php I get the expected result - stdin works, I can type. I want the same behavior when launch this script from autoit app.

Edited by skroo

Share this post


Link to post
Share on other sites

I believe you need to specifically add the specific flag to Run.

Such as $STDIN_CHILD etc...

 

I've tried this. Run is non-blocking, so in this case I need an infinite loop within read stdout and somehow wait for stdin reading, right?

I'm a little surprised that this simple task needs these tricks

Share this post


Link to post
Share on other sites

Have you tried compiling as a cui app?

 

I'm not sure, but I compile app with /console command line option

Share this post


Link to post
Share on other sites

I believe you need to specifically add the specific flag to Run.

Such as $STDIN_CHILD etc...

 

I'm trying STDIN again. As I understand in this case child's stdout/stderr go directly into parent's streams. And I have to get ConsoleRead() input and send it to child's stdin via StdinWrite().

But how can I determine that child stopped outputing and waits for input from keyboard?

Share this post


Link to post
Share on other sites

#13 ·  Posted (edited)

In fact I think you should show all what you have tried.

 

What I have now:

Local $ApplicationPid = Run($appCmdLine, $ApplicationWorkingDir, $showFlag, $STDIN_CHILD + $STDOUT_CHILD)

Local $prompt
Local $answer
Local $bytes
Local $errorCode

While ProcessExists($ApplicationPid)
    While 1
        $prompt    = StdoutRead($ApplicationPid)
        $bytes     = @extended
        $errorCode = @error

        If $errorCode Or $bytes Then ExitLoop

        Sleep(1)
    WEnd

    ConsoleWrite($prompt)

    While 1
        $answer   &= ConsoleRead()
        $bytes     = @extended
        $errorCode = @error

        If $bytes > 0 And StringInStr($answer, @CRLF) Then ExitLoop
        If $errorCode Then ExitLoop

        Sleep(1)
    WEnd

    If $answer Then
        StdinWrite($ApplicationPid, $answer)
    EndIf
WEnd

Almost what I need, but skips StdoutRead() block if child script works but does not output

PHP script:

<?php

fwrite(STDOUT, 'Type "yes" to continue: ');
$answer = fgets(STDIN, 4096);

if (strtolower(trim($answer)) !== 'yes') {
    fwrite(STDOUT, 'Aborted');
    exit;
}

sleep(1);

fwrite(STDOUT, '1');

sleep(1);

fwrite(STDOUT, '2');

fwrite(STDOUT, PHP_EOL . 'Type another one: ');
$answer = fgets(STDIN, 4096);

fwrite(STDOUT, 'You entered: ' . $answer);
Edited by skroo

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0