Read from process

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
nkrontir
Posts: 15
Joined: Wed Apr 12, 2006 3:24 am

Read from process

Post by nkrontir » Mon May 14, 2007 8:34 pm

Hi.

Is there a way to do a "non-blocking" read from process so to speak? In particular, i am reading a certain process that can end with a number of strings so a 'read from process until "such and such a string"' would not work (i guess if there would be a way to say "read from process until "string_a" or "string_b" or "string_c" that would be a great solution as well).

Unfortunately, read from process until empty, end and EOF don't seem to offer much help as the program is in a more or less infinite loop waiting for them without results. Needless to say that having a loop of "read from process for 100" produces the same result...

Any thoughts on this?

Thanks very much in advance.

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Post by Mark » Tue May 15, 2007 12:17 am

Hi nkrontir,

You need to have the process write the output to a file. While the process is running, Revolution should poll for the name of the process in the openProcesses. Do the polling by sending a message every few (milli)seconds.

on poll
if "Process Name" is among the lines of the openProcesses then
send "poll" to me in 1 sec
else
-- do whatever needs to be done after the process has finished
end if
end poll

The line start with "-- do whatever..." could call a script that opens and reads the file. Using "send in x seconds" makes sure that the script is non-blocking.

Best,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

nkrontir
Posts: 15
Joined: Wed Apr 12, 2006 3:24 am

Post by nkrontir » Tue May 15, 2007 3:47 pm

Hi Mark,

Thank you for your reply. Unfortunately, writing to a file and accessing it after a while is not ideal in this case. What I am trying to do is have my program work interactively with the process, as in an expect script so I need real-time access to the output of the process. For instance: ftp to a server, if you run into the string "username" write to process "someuser" if you run into the string password write to process "somepass" if you run into the string "welcome" write to process "cd /home/somedirectory" etc.etc.

My solution so far was something like this:

Code: Select all


on startproc the_process
put "false" into finished
try
    read from process the_process for 1 line
    put it into line2
catch error
    close process the_process
    put "true" into finished
    put error
end try
if matchtext(line2,".*Please enter password.*") then 
    write passw & return to process the_process
    put passw
  else
    if matchtext(line2,".*Connection error.*") then 
      close process the_process
    end if
  else
    if matchtext(line2,".*Invalid user.*") then 
      close process the_process
    end if
  else
    if matchtext(line2,".*Invalid password.*") then 
      close process the_process
    end if
  else
     if finished="false" then
         send "startproc the_process" to me in 0 seconds
     end if
  end if
end startproc

on mouseup
open process the_process for text read
send "startproc the_process" to me in 2 seconds
end mouseup


Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Post by Mark » Tue May 15, 2007 9:07 pm

Dear nkrontir,

If this is about passwords, the following might help you:

http://article.gmane.org/gmane.comp.ide ... ser/88545/

You should also be aware of the following bug:

http://quality.runrev.com/qacenter/show_bug.cgi?id=3196

For FTP, I really recommend using the built-in FTP commands. I even built a complete FTP client with plain Transcript.

Does your solution actually work, or are you looking for an alternative solution because it doesn't?

Best,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

nkrontir
Posts: 15
Joined: Wed Apr 12, 2006 3:24 am

Post by nkrontir » Tue May 15, 2007 10:52 pm

Hi Mark,

Unfortunately this is not only about passwords.
It appears that the best solution would be for someone to install expect in their system and revolution worked as a launcher of an expect script and its arguments.

In that case, I would like to ask the following: Is there a way to do a non-blocking application launch? I want to have two buttons, one that launches my application and one that cancels it, but unfortunately it appears that revolution waits for the application to finish before becoming fully functional again, so a "Cancel" button only works after the process has already finished, which defeats its purpose.

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Post by Mark » Tue May 15, 2007 11:44 pm

Dear nkrontir,

On windows, you can use the DOS start command to launch a programme without blocking the standalone or IDE. Use the shell function to execute DOS commands. On Mac OS X, you use AppleScript to tell the Finder to open the application. Use the do command to execute AppleScript.

Quite some time ago, I found that Rev's own launch command doesn't work the way I want it. Since then, I use start and open. You can check the docs for info about the launch command and see if you want to use that.

Best,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

nkrontir
Posts: 15
Joined: Wed Apr 12, 2006 3:24 am

Post by nkrontir » Thu May 17, 2007 1:07 pm

Hi Mark,

Thank you for your help this far.

I have tried to experiment with the launch command but I have found out that even though it does not block the entire interface, there is no way way of making it work with a feature similar to the openprocesses/close process functions (except the kill command which is extremely abrupt and of course unavailable in windows), so the whole point of implementing this with the launch command defeats the purpose in the first place (so that there is a "Cancel" button and the subprocess launched is closed).

Any way to get around this?

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Post by Mark » Thu May 31, 2007 6:42 pm

Hi nkrontir,

I have given this a bit more thought. What about the following experimental code? I don't know whether it will work for you, but I beli9eve it is worth a try.

Code: Select all

local lDataFromProcess

on startProcess
  open process "The Process"
  send "readProcess" to me in 10 secs
end startProcess

on readProcess
  read from process "The Process" in 100 millisecs
  if the result is "time out" then
    send "readProcess" to me in 0 millisecs
  else
    close process "The Process"
  end if
  put it after lDataFromProcess
end readProcess
I haven't tested this and you need to figure out yourself where to put the lines that write to the process. I'm sure you can adjust the readProcess handler to write first and read in 0 or 100 millisecs.

Let me know whether, or to which extent, this works for you.

Best,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

nkrontir
Posts: 15
Joined: Wed Apr 12, 2006 3:24 am

Post by nkrontir » Fri Jul 06, 2007 8:17 pm

Hi Mark,

Thanks for your help so far. After a lot of experimenting and tweaking code, I've decided to instead just do "set the hideconsolewindows to false" so the user will have a means to cancel the operation and maybe have info which will be given by the shell. Even though a crude means, I believe that this is the only solution with the tools provided by Revolution.

Unfortunately I have still to find a way for the shell that appear to actually display the data as you would expect. Can you please point me towards the right direction?

Post Reply