(standard-input)

General OpenLisp forum

(standard-input)

Postby dbane » Fri Jul 29, 2005 12:33 pm

I tried to write a UNIX filter (read from stdin, write to stdout) in OpenLisp, but (standard-input) doesn't seem to work the way I expected from reading the ISLISP spec. The following code:
Code: Select all
(defun main ()
   (let ((x (read-line (standard-input))))
        (format (standard-output) "Read ~A~%" x)))

doesn't wait for input and just prints "Read". Is OpenLisp supposed to work this way?
dbane
 
Posts: 8
Joined: Wed Feb 23, 2005 6:42 pm
Location: Limerick, Ireland

Postby jullien » Sun Aug 21, 2005 7:10 am

Your code is perfect and work as expected if you understand how the reader and toplevel loop work.

At toplevel, when the reader encounters an expression, this expression is evaluated immediately.
Code: Select all
100(+ 1 2)

100 is read and evaluated immedialely, the '(' is left alone up to the next read. The same is true for the final CR/LF in a single line.
Code: Select all
? 100CR/LF
= 100


100 is read and evaluated CR/LF itself will be read the next time.
Now, using your code, I assume that you made a test like this:
Code: Select all
? (main)CR/LF

The first call to (read-line) reads the current input buffer up to (and including) the next CR/LF and you get the emtpty line which is in fact
all the characters after ')' up to CR/LF.
jullien
Site Admin
 
Posts: 30
Joined: Fri Nov 12, 2004 12:29 am

Re: (standard-input)

Postby dbane » Fri Nov 01, 2019 10:53 pm

This is really thread necromancy :lol:

Things have moved on, and the below was tested on OpenLisp 10.7.0.

Even though the above makes sense for interactive use, in a UNIX pipe using "uxlisp -shell" it surprises me. Consider the following script:

Code: Select all
#!/usr/bin/env uxlisp -shell
(defun input-h (s)
  (let ((c (read-byte (standard-input) nil nil)))
    (format (error-output) "input-h: c ~S~%" c)
    (cond ((eq c 10) s)
      ((eq c nil) (error "empty stream"))
      (t (input-h (cons c s))))))
(defun input ()
  (input-h (string-append (reverse (input-h '())))))
(defun process-line (l)
  (format (standard-output) "~A~%" l))
(defun main ()
  (for ((line (input) (input)))
    ((null line))
    (process-line line)))
(main)


But this is my test result:

Code: Select all
firef$ cat input.txt | ./echo.lsp
input-h: c 10
input-h: c nil
** Error reading file '"./echo.lsp"' at (or before) line 16.
empty streamFatal error.
firef$


The "10" follows your above explanation. But I would expect the contents of "req.in" to follow, not EOF.
dbane
 
Posts: 8
Joined: Wed Feb 23, 2005 6:42 pm
Location: Limerick, Ireland


Return to General discussion

Who is online

Users browsing this forum: No registered users and 3 guests

cron