Re: [Haskell-cafe] Execution of external command

2007-12-16 Thread Jonathan Cast


On 14 Dec 2007, at 1:21 AM, Jules Bean wrote:


Evan Laforge wrote:
it seems that script may be not terminated if its output isn't  
read, so

better code should be

(_, h, g, _) - runInteractiveCommand script params
result - hGetLine h
hGetContents h = evaluate.length
hGetContents g = evaluate.length

Tangent here, but does anyone else think that something like
hGetContentsEagerly would be handy in System.IO?


YES!

Jules

PS we could give it a nice sensible name like hGetContents. We  
could renaming the existing hGetContents to  
hUnsafeGetContentsDontUseThisUnlessYouHaveSpentThreeMonthsLearningGHCs 
ExecutionSemanticsOrYouWillRegretIt


On the contrary, it's great for writing filters.   OTOH, using the  
result of hGetContents interleaved with other IO actions feels like a  
reversion to the bad old days of dialogs (which it is, of course).


jcc


___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Execution of external command

2007-12-14 Thread Jules Bean

Evan Laforge wrote:

it seems that script may be not terminated if its output isn't read, so
better code should be

(_, h, g, _) - runInteractiveCommand script params
result - hGetLine h
hGetContents h = evaluate.length
hGetContents g = evaluate.length


Tangent here, but does anyone else think that something like
hGetContentsEagerly would be handy in System.IO? 


YES!

Jules

PS we could give it a nice sensible name like hGetContents. We could 
renaming the existing hGetContents to 
hUnsafeGetContentsDontUseThisUnlessYouHaveSpentThreeMonthsLearningGHCsExecutionSemanticsOrYouWillRegretIt


___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Execution of external command

2007-12-13 Thread Duncan Coutts

On Thu, 2007-12-13 at 15:48 +0300, Bulat Ziganshin wrote:
 Hello haskell-cafe,
 
 please help me with selection of proper function to use
 
 i need to run external command with parameter and get its stdout, smth
 like this:
 
 output - system cmd param
 
 the code should be compatible with unix and windows, and it should be
 possible to execute scripts (so afaiu it should execute command via
 cmd/sh). i use ghc 6.6.1 and it will be great if this function will
 not require any non-bundled libs and be compatible with later ghc
 versions

There is rawSystemStdout in Cabal which you might like to copy. It is
portable to windows and several Haskell compilers but does use cpp to
achieve that. It's also rather inefficient as it has to create a
temporary file. It seems it is not possible to use pipes to get the
stdout and have the resulting code be portable between Haskell
implementations (it's a favourite peeve of mine).

As the name suggests, rawSystemStdout uses rawSystem so does not
necessarily do what you want with cmd/sh but you could easily adapt it
to use system rather than rawSystem.

http://darcs.haskell.org/cabal/Distribution/Simple/Utils.hs
http://darcs.haskell.org/cabal/Distribution/Compat/TempFile.hs

Duncan

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Execution of external command

2007-12-13 Thread Yitzchak Gale

Hi Bulat,

You wrote:

please help me with selection of proper function to use

i need to run external command with parameter and get its stdout, smth
like this:

output - system cmd param

the code should be compatible with unix and windows, and it should be
possible to execute scripts (so afaiu it should execute command via
cmd/sh). i use ghc 6.6.1 and it will be great if this function will
not require any non-bundled libs and be compatible with later ghc
versions



OK, I'll bite. What's wrong with runInteractiveCommand?

-Yitz
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Execution of external command

2007-12-13 Thread Yitzchak Gale

so,

do (stdin,stdout,stderr,ph) - runInteractiveCommand script param
   waitForProcess ph

when i should read stdout? before or after waitForProcess?


If you are sure that your script will behave nicely
(or don't care if it doesn't), how about just:

(_, h, _, _) - runInteractiveCommand script params
output - hGetContents h

-Yitz
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Execution of external command

2007-12-13 Thread Duncan Coutts

On Thu, 2007-12-13 at 15:06 +0200, Yitzchak Gale wrote:
 Hi Bulat,
 
 You wrote:
  please help me with selection of proper function to use
 
  i need to run external command with parameter and get its stdout, smth
  like this:
 
  output - system cmd param
 
  the code should be compatible with unix and windows, and it should be
  possible to execute scripts (so afaiu it should execute command via
  cmd/sh). i use ghc 6.6.1 and it will be great if this function will
  not require any non-bundled libs and be compatible with later ghc
  versions
 
 
 OK, I'll bite. What's wrong with runInteractiveCommand?

It requires threads to use correctly and it is only available in GHC,
not in hugs, nhc98 etc.

It requires threads because you have to pull from both the stdout and
stderr to prevent blocking. You could do it with non-blocking reads but
not without busy-waiting.

Duncan

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe