[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Retrieve output from command

From: Roland Orre
Subject: Re: Retrieve output from command
Date: Tue, 31 Jul 2001 19:55:58 +0200 (MET DST)

On 31 Jul 2001, Manuel Giraud wrote:
> I'm trying to get the result of the command "ls" in a list, for
> example :
>         (define l (system->list "ls"))
The code below works but may not be the most efficient way to do it,
but it probably gives you what you want, i.e. e.g. each filename
in a string. The procedures read-delimited-string and read-white-space
I intended to code in C some day... maybe someone has done it already?
With some small additions you can get a matrix instead, for e.g.
top ps and such, which is useful.

    Best regards

(define system-white-space (string #\space #\nl #\ht))

(define (system->list command)
  (let ((in (open-input-pipe command))
        (delims system-white-space))
    (consume-white-space delims in)
    (let loop ((item (read-delimited-string delims in))
               (items '()))
      (cond ((eof-object? item)
             (close-input-port in)
             (reverse items))
             (consume-white-space delims in)
             (loop (read-delimited-string delims in) (cons item items)))))))

(define (read-delimited-string delims port)
  (let loop
      ((ch (read-char port))
       (build '()))
    (cond ((eof-object? ch)
           (if (> (length build) 0)
               (apply string (reverse build))
          ((string-index delims ch)
           (unread-char ch port)
           (apply string (reverse build)))
           (loop (read-char port) (cons ch build))))))

(define (consume-white-space delims port)
  (let loop
      ((ch (read-char port)))
    (cond ((eof-object? ch)
          ((string-index delims ch)
           (loop (read-char port)))
           (unread-char ch port)))))


reply via email to

[Prev in Thread] Current Thread [Next in Thread]