[comp.lang.lisp] Communicating with foreign hosts from MACL

arie@dip.eecs.umich.edu (Arie Covrigaru) (01/31/90)

The system I am using is Macintosh Allegro Common Lisp 1.3.1.

I am working on a project that communicates with a robot through the
serial port.  The communication is fairy simple, sending commands of
one line each (in BASIC, believe it or not) to the robot and receiving
one or more lines of response from it.  My question is a follows:

The function read-line, as defined in the CL syntax, reads from a stream
of characters until a #\Newline character is encountered, and returns the
read in string without the #\Newline.  The robot, on the other hand, sends
lines ending with a #\Newline followed by a #\Linefeed character.  I 
understand that the Mac's behaviour is different from the common way to
communicate, but what are other systems doing with that #\Linefeed character?

The TI Explorer I am using for the same project doesn't have this problem,
yet it must follow the same definition of read-line.  On the Mac, I had to
add a form to take care of the #\Linefeed character after a read-line, but
this makes the code incompatible with the TI version.

Can people tell me what is really happening on other systems?

Thanks.

=============================================================================
Arie Covrigaru                     |    Internet: arie@eecs.umich.edu
University of Michigan AI Lab      |    1101 Beal Ave., Ann Arbor, MI 48109
=============================================================================

jwz@teak.berkeley.edu (Jamie Zawinski) (02/16/90)

In article <1331@zipeecs.umich.edu> arie@dip.eecs.umich.edu (Arie Covrigaru) writes:
>
> The TI Explorer I am using for the same project doesn't have this problem,
> yet it must follow the same definition of read-line.  On the Mac, I had to
> add a form to take care of the #\Linefeed character after a read-line, but
> this makes the code incompatible with the TI version.

The issue of "what read-line should do" aside, there is no reason that such
an implementation difference should make your code non-portable.  The
read-macros #+ and #- let you conditionally compile things into your code,
much like C's #ifdef form.

	(let ((line (read-line stream)))
	  #-EXPLORER (read-char stream)  ; if not a TI, discard a newline.
	  line)

Or, a more general solution:

	(let ((line (read-line stream)))
	  ;; If the next character is LF, ditch it.
	  (when (char= #\Linefeed (peek-char nil stream nil #\Null))
	    (read-char stream))
	  line)

		-- Jamie