unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
* A better way to run shell cmd?
@ 2012-06-13  6:55 Nala Ginrut
  2012-06-13  7:19 ` Daniel Hartwig
  2012-06-14  1:57 ` gregory benison
  0 siblings, 2 replies; 4+ messages in thread
From: Nala Ginrut @ 2012-06-13  6:55 UTC (permalink / raw)
  To: guile-devel, Guile User

hi folks! I'm on my trip and inconvenient to meet you guys on IRC.
Things gonna be normal next month.

Anyway, there's a problem for you.
I'm trying to write a simple wrapper for "sed" with our popen module:
--------------code------------
(use-modules (ice-9 popen) (rnrs))
(define (sed pattern str)
  (let ((p (open-input-output-pipe (string-append "sed " pattern))))
    (display str p)
    (get-string-all p)))
----------------end-------------

I expect it run like this:
-----------------------
(sed "s:a:b:g" "abcabc")
==> "bbcbbc"
-----------------------

But it halts that I have to interrupt.
Is it accepted?

Unlimited to "sed", I expect any shell cmd could be used in this way.
And I think it's easy to implement it with fork-then-exec. But I think
it's better do it with our (ice-9 popen).

I think the alternative way would be:
------------------code----------------
(define (sed pattern str)
  (let ((p (open-input-output-pipe (string-append "echo -n " str "|sed
" pattern))))
         (get-string-all p)))
-------------------end-----------------
which works but less elegant to the former.

Or anyone provide a better solution for shell cmd?



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: A better way to run shell cmd?
  2012-06-13  6:55 A better way to run shell cmd? Nala Ginrut
@ 2012-06-13  7:19 ` Daniel Hartwig
  2012-06-13 10:02   ` Nala Ginrut
  2012-06-14  1:57 ` gregory benison
  1 sibling, 1 reply; 4+ messages in thread
From: Daniel Hartwig @ 2012-06-13  7:19 UTC (permalink / raw)
  To: guile-devel, Guile User

On 13 June 2012 14:55, Nala Ginrut <nalaginrut@gmail.com> wrote:
> hi folks! I'm on my trip and inconvenient to meet you guys on IRC.
> Things gonna be normal next month.
>
> Anyway, there's a problem for you.
> I'm trying to write a simple wrapper for "sed" with our popen module:
> --------------code------------
> (use-modules (ice-9 popen) (rnrs))
> (define (sed pattern str)
>  (let ((p (open-input-output-pipe (string-append "sed " pattern))))
>    (display str p)
>    (get-string-all p)))
> ----------------end-------------
>
> I expect it run like this:
> -----------------------
> (sed "s:a:b:g" "abcabc")
> ==> "bbcbbc"
> -----------------------
>
> But it halts that I have to interrupt.
> Is it accepted?
>

The sed program may be expecting a newline, or waiting for EOF or a
full buffer before producing it's output.  Some users have had trouble
with this in the past, because you can not send EOF to one end of the
pipe without closing the other.

One way around this to use run-with-pipe from the guile-lib module (os
process).  This returns two separate pipe objects for input and
output, thus you can close/EOF one and continue to use the other.
Many filter programs do not flush their output until their input EOFs,
which is why your solution using 'echo -n' works.

See this thread for a short discussion:

http://lists.gnu.org/archive/html/guile-user/2012-02/msg00066.html

Updating guile to support closing one end of the pipe is long off on my TODO.



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: A better way to run shell cmd?
  2012-06-13  7:19 ` Daniel Hartwig
@ 2012-06-13 10:02   ` Nala Ginrut
  0 siblings, 0 replies; 4+ messages in thread
From: Nala Ginrut @ 2012-06-13 10:02 UTC (permalink / raw)
  To: Daniel Hartwig; +Cc: Guile User, guile-devel

hi Daniel, thanks for reply!
I tried guile-lib just now, it's nice. I'll use it.

Besides, do you think pipe is the proper way to implement "sed" function?
Or it's better to implement a module with pure Guile?

On Wed, Jun 13, 2012 at 3:19 PM, Daniel Hartwig <mandyke@gmail.com> wrote:
> On 13 June 2012 14:55, Nala Ginrut <nalaginrut@gmail.com> wrote:
>> hi folks! I'm on my trip and inconvenient to meet you guys on IRC.
>> Things gonna be normal next month.
>>
>> Anyway, there's a problem for you.
>> I'm trying to write a simple wrapper for "sed" with our popen module:
>> --------------code------------
>> (use-modules (ice-9 popen) (rnrs))
>> (define (sed pattern str)
>>  (let ((p (open-input-output-pipe (string-append "sed " pattern))))
>>    (display str p)
>>    (get-string-all p)))
>> ----------------end-------------
>>
>> I expect it run like this:
>> -----------------------
>> (sed "s:a:b:g" "abcabc")
>> ==> "bbcbbc"
>> -----------------------
>>
>> But it halts that I have to interrupt.
>> Is it accepted?
>>
>
> The sed program may be expecting a newline, or waiting for EOF or a
> full buffer before producing it's output.  Some users have had trouble
> with this in the past, because you can not send EOF to one end of the
> pipe without closing the other.
>
> One way around this to use run-with-pipe from the guile-lib module (os
> process).  This returns two separate pipe objects for input and
> output, thus you can close/EOF one and continue to use the other.
> Many filter programs do not flush their output until their input EOFs,
> which is why your solution using 'echo -n' works.
>
> See this thread for a short discussion:
>
> http://lists.gnu.org/archive/html/guile-user/2012-02/msg00066.html
>
> Updating guile to support closing one end of the pipe is long off on my TODO.
>



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: A better way to run shell cmd?
  2012-06-13  6:55 A better way to run shell cmd? Nala Ginrut
  2012-06-13  7:19 ` Daniel Hartwig
@ 2012-06-14  1:57 ` gregory benison
  1 sibling, 0 replies; 4+ messages in thread
From: gregory benison @ 2012-06-14  1:57 UTC (permalink / raw)
  To: Nala Ginrut; +Cc: Guile User, guile-devel

On Tue, Jun 12, 2012 at 11:55 PM, Nala Ginrut <nalaginrut@gmail.com> wrote:

>
> Anyway, there's a problem for you.
> I'm trying to write a simple wrapper for "sed" with our popen module:

This is exactly the kind of thing SCSH does well:

1> (run/string (sed s:a:b:g)(<< "banana"))
"bbnbnb"

Nevertheless the current state of guile's scsh implementation is,
according to the manual, "bitrotten"


-- 
Greg Benison <gbenison@gmail.com>
[blog] http://gcbenison.wordpress.com
[twitter] @gcbenison



^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2012-06-14  1:57 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-06-13  6:55 A better way to run shell cmd? Nala Ginrut
2012-06-13  7:19 ` Daniel Hartwig
2012-06-13 10:02   ` Nala Ginrut
2012-06-14  1:57 ` gregory benison

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).