Question about process

Notices and updates
Locked
cavva
Posts: 16
Joined: Sun Nov 11, 2007 2:45 am

Question about process

Post by cavva »

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

Code: Select all

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

(process "bc" bcin bcout)

(write-buffer myout "3 + 4\n")  ; 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
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Post by cormullion »

I think it works. But perhaps you need to specify the full path of bc... My example
http://newlisp.org/introduction-to-newl ... rprocesses doesn't work either unless I make that change.

Code: Select all

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 "quit\n")
5
> 

cavva
Posts: 16
Joined: Sun Nov 11, 2007 2:45 am

Post by cavva »

thanks cormullion, you're right.

I need also to change this line in my script

Code: Select all

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

Code: Select all

(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
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Post by cormullion »

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:

Code: Select all

(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 "quit\n") 

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

Locked