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[2]: [Haskell-cafe] Execution of external command

2007-12-14 Thread Bulat Ziganshin
Hello Jules,

Friday, December 14, 2007, 12:21:30 PM, you wrote:

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

i have more advanced proposal - we should include in its name whole
paper on its semantics so anyone using it will be clearly warned.
moreover, any suggestion to use this function will automatically
include exact description of its caveats that is again The Right Thing
To Do :)


-- 
Best regards,
 Bulatmailto:[EMAIL PROTECTED]

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


Re: Re[2]: [Haskell-cafe] Execution of external command

2007-12-14 Thread Benja Fallenstein
On Dec 14, 2007 10:38 AM, Bulat Ziganshin [EMAIL PROTECTED] wrote:
  hUnsafeGetContentsDontUseThisUnlessYouHaveSpentThreeMonthsLearningGHCsExecutionSemanticsOrYouWillRegretIt

 i have more advanced proposal - we should include in its name whole
 paper on its semantics so anyone using it will be clearly warned.
 moreover, any suggestion to use this function will automatically
 include exact description of its caveats that is again The Right Thing
 To Do :)

Until, that is, the first library appears on Hackage that aliases the
name to ohBugger :-)

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


[Haskell-cafe] Execution of external command

2007-12-13 Thread Bulat Ziganshin
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

-- 
Best regards,
 Bulat  mailto:[EMAIL PROTECTED]

___
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[2]: [Haskell-cafe] Execution of external command

2007-12-13 Thread Bulat Ziganshin
Hello Duncan,

Thursday, December 13, 2007, 4:10:26 PM, you wrote:

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

 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).

i don't need to interact with program, just get its whole output (one
line) after it was finished. btw, afair, there was problems in windows
with redirection of cmd.exe output (in particular when running
executables from command files)

separate windows and unix versions are ok for me, i just need them
both. and i need only ghc support

taking this all into account, where i should look?


-- 
Best regards,
 Bulatmailto:[EMAIL PROTECTED]

___
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[2]: [Haskell-cafe] Execution of external command

2007-12-13 Thread Bulat Ziganshin
Hello Yitzchak,

Thursday, December 13, 2007, 4:36:48 PM, you wrote:

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

am i correctly understand that i don't need to waitForProcess and,
if script behaves correctly, hGetContents will read its whole output?


-- 
Best regards,
 Bulatmailto:[EMAIL PROTECTED]

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


Re: Re[2]: [Haskell-cafe] Execution of external command

2007-12-13 Thread Duncan Coutts

On Thu, 2007-12-13 at 16:27 +0300, Bulat Ziganshin wrote:
 Hello Duncan,
 
 Thursday, December 13, 2007, 4:10:26 PM, you wrote:
 
  i need to run external command with parameter and get its stdout, smth
 
  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).
 
 i don't need to interact with program, just get its whole output (one
 line) after it was finished. btw, afair, there was problems in windows
 with redirection of cmd.exe output (in particular when running
 executables from command files)
 
 separate windows and unix versions are ok for me, i just need them
 both. and i need only ghc support
 
 taking this all into account, where i should look?

Use just the GHC bit from the code I pointed at:

  bracket (liftM2 (,) (openTempFile tmpDir cmdstdout) (openFile devNull 
WriteMode))
  -- We need to close tmpHandle or the file removal fails on Windows
  (\((tmpName, tmpHandle), nullHandle) - do
 hClose tmpHandle
 removeFile tmpName
 hClose nullHandle)
 $ \((tmpName, tmpHandle), nullHandle) - do
cmdHandle - runProcess path args Nothing Nothing
   Nothing (Just tmpHandle) (Just nullHandle)
exitCode - waitForProcess cmdHandle
output - readFile tmpName
evaluate (length output)
return (output, exitCode)

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


Re[4]: [Haskell-cafe] Execution of external command

2007-12-13 Thread Bulat Ziganshin
Hello Duncan,

Thursday, December 13, 2007, 4:43:17 PM, you wrote:

 Use just the GHC bit from the code I pointed at:

thank you, Duncan. are there any objections against simplest code
proposed by Yitzchak? i.e.

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

taking into account that bad-behaved scripts are not my headache?


-- 
Best regards,
 Bulatmailto:[EMAIL PROTECTED]

___
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


Re[2]: [Haskell-cafe] Execution of external command

2007-12-13 Thread Bulat Ziganshin
Hello gwern0,

Thursday, December 13, 2007, 4:43:02 PM, you wrote:

 to get at stdout. I don't think this would go through cmd/sh, but
 honestly, specifying a particular interpreter breaks your
 portability anyway

these scripts will be written by users so they don't need to be the
same for winunix. i guess that users will prefer to use perl or
something like this for these scripts and afaiu in both windows and
linux this need to go through sh/cmd

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

am i right?


-- 
Best regards,
 Bulatmailto:[EMAIL PROTECTED]

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


Re: Re[2]: [Haskell-cafe] Execution of external command

2007-12-13 Thread Yitzchak Gale
Bulat Ziganshin wrote:
 to Yitzchak: and you've asked what's wrong with runInteractiveCommand? :)))

It was a good question. It prompted Duncan to
post some really cool stuff, and we all learned
something.

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


Re: Re[2]: [Haskell-cafe] Execution of external command

2007-12-13 Thread Duncan Coutts

On Thu, 2007-12-13 at 17:08 +0300, Bulat Ziganshin wrote:
 Hello Duncan,
 
 Thursday, December 13, 2007, 4:51:20 PM, you wrote:
 
  OK, I'll bite. What's wrong with runInteractiveCommand?
 
  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.
 
 may be this will be ok (with -threaded)?

No need for -threaded.

 (_, stdout, stderr, _) - runInteractiveCommand script params
 forkIO (hGetContents stderr = evaluate.length)
 result - hGetLine stdout
 hGetContents stdout = evaluate.length

Yep, that'll work.

 awkward, but still shorter than code from Cabal

The Cabal code has to work with ghc-6.2 - 6.8, hugs, nhc98 and jhc. It
cannot use threads.

Duncan

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


Re[4]: [Haskell-cafe] Execution of external command

2007-12-13 Thread Bulat Ziganshin
Hello Duncan,

Thursday, December 13, 2007, 5:38:24 PM, you wrote:

 Yep, that'll work.

Spencer Janssen added that i should use waitForProcess - otherwise on
unix it will leave a zombie

Yitzchak, Duncan, gwern and Spencer. thank you very much for all your
answers, i'm sure that i'll have much longer learning curve without
your help!

 The Cabal code has to work with ghc-6.2 - 6.8, hugs, nhc98 and jhc. It
 cannot use threads.

you've complained that there is no such functionality in std libs. may
be it's worth making new hackage library, smth like Process?


-- 
Best regards,
 Bulatmailto:[EMAIL PROTECTED]

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


Re: Re[2]: [Haskell-cafe] Execution of external command

2007-12-13 Thread Evan Laforge
 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?  When I am reading
lots of little files I worry about open file limits.  And besides, it
seems like unsafeInterleaveIO can result in IO exceptions coming from
pure code, so why use it if you don't have to?
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: Re[2]: [Haskell-cafe] Execution of external command

2007-12-13 Thread Don Stewart
qdunkan:
  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?  When I am reading
 lots of little files I worry about open file limits.  And besides, it
 seems like unsafeInterleaveIO can result in IO exceptions coming from
 pure code, so why use it if you don't have to?

I'll put a System.IO.Strict up on the weekend.
Until then, perhaps use, as I sometimes do:

import qualified Data.ByteString as S

hGetContentsStrict = S.unpack `fmap` S.hGetContents

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