Subject: Re: common lisp as scripting language
From: (Rob Warnock)
Date: Mon, 19 Feb 2007 04:11:19 -0600
Newsgroups: comp.lang.lisp
Message-ID: <>
<> wrote:
| - *standard-input* and *standard-output* may not be used as binary
| streams. Since I like using a lot of small reusable scripts piped
| together and feed them with any kind of data (thais is even whith
| binary data), the standard IOs can't be used

Several CL implementations support bivalent (or multivalent) streams.

In CMUCL, for example, there is a SYSTEM:BINARY-TEXT-STREAM extension
type which allows both text (e.g., READ-CHAR and things that use it)
and binary (e.g., READ-BYTE and things that use it) I/O. If the
initial *STANDARD-{IN,OUT}PUT* streams are instances of a subclass
of SYSTEM:FD-STREAM [which they almost always are], then you can
extract the Unix file descriptors and re-open them [in the Lisp
stream sense] as bivalent streams.

It's actually a bit messier than it might otherwise be in CMUCL,
since *STANDARD-OUTPUT* is actually a synonym streams to the
internal SYSTEM:*STDOUT*, and *STANDARD-INPUT* is even worse --
its a TWO-WAY-STREAM whose input & output streams are each synonym
streams to SYSTEM:*STDIN* & SYSTEM:*STDOUT*, resp., so if you
wanted to do it "cleanly" you'd have to work through all those
levels of indirection.

However, for standard Unix/Linux scripting, you already know
that stdin is fd 0 and stdout is fd 1, so what you're asking
for can be done with the following quick & dirty hack:

    (setf *standard-input*
	  (system:make-fd-stream 0 :input t :binary-stream-p t
				   :element-type :default))

    (setf *standard-output*
	  (system:make-fd-stream 1 :output t :binary-stream-p t
				   :element-type :default))

By the way, when opening a file from scratch, you can also use
the CMUCL OPEN extension options ":CLASS 'BINARY-TEXT-STREAM" to
get the same result. (You also need to set ":ELEMENT-TYPE :DEFAULT"
in this case too.)

| - Modern scripting languages all have builtin regex functions while
| Lisp has not

See <> (and <>
generally). You can load CL-PPCRE (and whatever else you want to be
"always there") into your CL, save the image, and make that one the
system default. From then on, it will "have builtin regex functions"...


Rob Warnock			<>
627 26th Avenue			<URL:>
San Mateo, CA 94403		(650)572-2607