Question about process

Started by cavva, November 20, 2007, 01:19:13 PM

Previous topic - Next topic

cavva

I'm trying to run the example found in the documentation about process



(map set '(myin bcout) (pipe))
(map set '(bcin myout) (pipe))  

(process "bc" bcin bcout)

(write-buffer myout "3 + 4n")  ; bc expects a linefeed

(while (read-line myin) (write-line))

(exit)


I've added a write-line and an exit call.

When i launch the script i get a "new line" waiting for some input, the only think i can do is type ^C to terminate the script.



Instead i'm expecting to see the result of "3+4" printed than the script terminated ...



PS: I'm on linux, newlisp v 9.2.4

cormullion

#1
I think it works. But perhaps you need to specify the full path of bc... My example

http://newlisp.org/introduction-to-newlisp.html#communicatingwithotherprocesses">//http://newlisp.org/introduction-to-newlisp.html#communicatingwithotherprocesses doesn't work either unless I make that change.




newLISP v.9.2.5 on OSX UTF-8, execute 'newlisp -h' for more info.

> (map set '(bcin myout) (pipe))           ; set up the plumbing
(3 4)
> (map set '(myin bcout) (pipe))
(5 6)
>
> (process "/usr/bin/bc" bcin bcout)
416
> (set 'sum "123456789012345 * 123456789012345")
"123456789012345 * 123456789012345"
> (write-buffer myout (string sum "n"))
34
> (set 'answer (read-line myin))
"15241578753238669120562399025"
> (println (string sum " = " answer))
123456789012345 * 123456789012345 = 15241578753238669120562399025
"123456789012345 * 123456789012345 = 15241578753238669120562399025"
> (write-buffer myout "quitn")
5
>

cavva

#2
thanks cormullion, you're right.



I need also to change this line in my script

(while (read-line myin) (write-line))

with this

(println (read-line myin))

because with the "while" launching the script ends with the same "new line waiting input"



Is "bc" that doesn't end the "communication" with a line-feed or am i doing something wrong?

cormullion

#3
Hmm - a bit beyond me I afraid. I think that read-line waits until a newline is received, but if bc gets no input there won't be any output. Checking for all that could be interesting...



This sort of thing might work:


(map set '(bcin myout) (pipe))
(map set '(myin bcout) (pipe))
(process "/usr/bin/bc" bcin bcout)

(set 'question "?")

(until (= (set 'question (read-line))  {quit})
    (write-buffer myout (string question "n"))
    (set 'answer (read-line myin))
    (println "answer " answer))

(write-buffer myout "quitn")

(11 12)
(13 14)
554
"?"
89786756 * 12123123 * 123124123123
answer 134020101580192703938879524
quit
"134020101580192703938879524"
5
>



or perhaps that should be do-until. Or a while. I'm too tired to think about it ... :-)