4

我正在尝试为 clisp 创建一个像这样工作的“系统”命令

(setq result (system "pwd"))

;;now result is equal to /my/path/here

我有这样的事情:

(defun system (cmd)
 (ext:run-program :output :stream))

但是,我不确定如何将流转换为字符串。我已经多次审查了超规格和谷歌。

编辑:使用 Ranier 的命令并使用 with-output-to-stream,

(defun system (cmd)
  (with-output-to-string (stream)
    (ext:run-program cmd :output stream)))

然后尝试运行grep,这是我的路径......

[11]> (system "grep")

*** - STRING: argument #<OUTPUT STRING-OUTPUT-STREAM> should be a string, a
      symbol or a character
The following restarts are available:
USE-VALUE      :R1      Input a value to be used instead.
ABORT          :R2      Abort main loop
Break 1 [12]> :r2
4

4 回答 4

5

像这样的东西?

版本 2:

(defun copy-stream (in out)
   (loop for line = (read-line in nil nil)
         while line
         do (write-line line out)))

(defun system (cmd)
  (with-open-stream (s1 (ext:run-program cmd :output :stream))
    (with-output-to-string (out)
      (copy-stream s1 out))))


[6]> (system "ls")
"#.emacs#
Applications
..."
于 2010-06-10T23:22:41.750 回答
3

根据关于 的CLISP 文档run-program:output参数应该是以下之一

  • :terminal- 写入终端
  • :stream-创建并返回一个输入流,您可以从中读取
  • 路径名指示符-写入指定文件
  • nil- 忽略输出

如果您希望将输出收集到字符串中,则必须使用读写复制循环将数据从返回的流传输到字符串。根据 Rainer 的建议,您已经开始使用了,但是您需要自己写入with-output-to-string,而不是将输出流提供给,从返回的输入流中复制数据。run-programrun-program

于 2010-06-11T00:13:23.170 回答
2

您是在专门询问 clisp。我将在这里补充一点,如果您使用的是 Clozure CL,那么您也可以轻松地运行 os 子进程。

一些例子:

;;; Capture the output of the "uname" program in a lisp string-stream
;;; and return the generated string (which will contain a trailing
;;; newline.)
? (with-output-to-string (stream)
    (run-program "uname" '("-r") :output stream))
;;; Write a string to *STANDARD-OUTPUT*, the hard way.
? (run-program "cat" () :input (make-string-input-stream "hello") :output t)
;;; Find out that "ls" doesn't expand wildcards.
? (run-program "ls" '("*.lisp") :output t)
;;; Let the shell expand wildcards.
? (run-program "sh" '("-c" "ls *.lisp") :output t)

在位于此处的 CCL 文档中搜索运行程序:http: //ccl.clozure.com/ccl-documentation.html

在这个 stackoverflow 答案中有几个很好的 Lisp 方法可以做到这一点:进行系统调用,将标准输出输出作为字符串返回 再一次,Rainer 来救援。谢谢拉尼尔。

于 2011-05-18T02:18:45.057 回答
0

这是一个较短的

(defun system(cmd)
  (ext:shell (string cmd)))

> (system '"cd ..; ls -lrt; pwd")
于 2014-07-10T05:47:15.803 回答