4.2.2 Annotated examples

4.2.2.1 Running a program

This example shows how to run a program and have Lisp communicate with it through a stream. Lisp does not wait for the program to complete. This example assumes you have a program called"banner" on/usr/games.

;;; This function prints the output from a program that has been 
;;; started with RUN-PROGRAM. On a heavily loaded system, pauses
;;; in program output can cause premature termination of this
;;; function. In this case the  function can simply be called
;;; again to get the rest of the output.
> (defun type-until-hung (stream) 
    (do ((char (read-char-no-hang stream)   
               (read-char-no-hang stream))) 
        ((null char)) 
     (write-char char))) 
TYPE-UNTIL-HUNG

;; The shell is set to the stream that is used for standard input ;; and standard output for csh. The terminal remains the standard ;; error output stream. > (setq shell (run-program "csh" :input :stream :output :stream :wait nil)) #<Stream BUFFERED-STREAM 101BF73B>

;; Send a banner command to csh. > (format shell "/usr/games/banner test~%") NIL

> (force-output shell) NIL

;; Print the output from banner. > (type-until-hung shell) ##### ###### #### ##### # # # # # ##### #### # # # # # # # # # # # ###### #### # NIL

;; Send an erroneous command to the shell. > (format shell "unknown~%") NIL

> (force-output shell) NIL

> unknown: Command not found. ; This is standard output; ; it is not trapped. (type-until-hung shell) ; No output was sent to the NIL ; standard output.

;; Make csh exit. > (format shell "exit~%") NIL

;; Close the stream used for standard input and output. > (close shell) NIL

Note: Many UNIX programs exit on reading an end-of-file indicator from the standard input stream. If you run a program that does not, you should always give it an exit command before you close its input stream.


The Advanced User's Guide - 9 SEP 1996

Generated with Harlequin WebMaker