lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hi,

Asko Kauppi wrote:
> It seems 'popen' is only read, or write, but not both.

This is a limitation of the underlying popen() C library API. On POSIX
systems popen() internally uses the pipe(), dup2(), fork() and exec*()
syscalls and adds stdio buffering plus child handling.

Of course you can use these syscalls yourself to open multiple pipes
between the parent and the child process. But you may encounter some
difficulties with buffering and blocking reads/writes. If you really,
really want to go this route then I suggest to drop stdio buffering
and use multiplexed non-blocking native I/O. Getting all of this right
is not trivial.

BTW: Read the perl docs about the 'open' function (man perlfunc) and
     pipes in general (man perlipc) to get a feeling for the issues
     involved. Then reconsider if you really want to go that way. :-)

> What I'm trying to do is like:
> 
> 	echo blahblahblah | osascript | (catch output)
> 
> That is, Lua would be both feeding in _and_ reading the output of the 
> 'osascript' command (which runs AppleScript on OS X).

You could use a temp file to feed the input or catch the output if you do
not need interactivity on either end. You could even direct the output to
a named pipe and read it with a different Lua process or native thread.

Anyway ... I don't know anything about AppleScript, but I have this gut
feeling that this is not the best way to go. While using a pipe to an
existing program sounds easy at first, I would not recommend it for anything
more complicated than (say) zcat.

Doesn't AppleScript provide a C API? Writing a proper Lua wrapper may prove
to be easier than getting the multi-pipe thing right.

Bye,
     Mike