r4rs.scm 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. ;;;; r4rs.scm --- definitions needed for libguile to be R4RS compliant
  2. ;;;; Jim Blandy <jimb@cyclic.com> --- October 1996
  3. ;;;; Copyright (C) 1996, 1997, 1998, 2000, 2001, 2006 Free Software Foundation, Inc.
  4. ;;;;
  5. ;;;; This library is free software; you can redistribute it and/or
  6. ;;;; modify it under the terms of the GNU Lesser General Public
  7. ;;;; License as published by the Free Software Foundation; either
  8. ;;;; version 2.1 of the License, or (at your option) any later version.
  9. ;;;;
  10. ;;;; This library is distributed in the hope that it will be useful,
  11. ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. ;;;; Lesser General Public License for more details.
  14. ;;;;
  15. ;;;; You should have received a copy of the GNU Lesser General Public
  16. ;;;; License along with this library; if not, write to the Free Software
  17. ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. ;;;; apply and call-with-current-continuation
  19. ;;; We want these to be tail-recursive, so instead of using primitive
  20. ;;; procedures, we define them as closures in terms of the primitive
  21. ;;; macros @apply and @call-with-current-continuation.
  22. (set! apply (lambda (fun . args) (@apply fun (apply:nconc2last args))))
  23. (set-procedure-property! apply 'name 'apply)
  24. (define (call-with-current-continuation proc)
  25. (@call-with-current-continuation proc))
  26. (define (call-with-values producer consumer)
  27. (@call-with-values producer consumer))
  28. ;;;; Basic Port Code
  29. ;;; Specifically, the parts of the low-level port code that are written in
  30. ;;; Scheme rather than C.
  31. ;;;
  32. ;;; WARNING: the parts of this interface that refer to file ports
  33. ;;; are going away. It would be gone already except that it is used
  34. ;;; "internally" in a few places.
  35. ;;; OPEN_READ, OPEN_WRITE, and OPEN_BOTH are used to request the
  36. ;;; proper mode to open files in.
  37. ;;;
  38. ;;; If we want to support systems that do CRLF->LF translation, like
  39. ;;; Windows, then we should have a symbol in scmconfig.h made visible
  40. ;;; to the Scheme level that we can test here, and autoconf magic to
  41. ;;; #define it when appropriate. Windows will probably just have a
  42. ;;; hand-generated scmconfig.h file.
  43. (define OPEN_READ "r")
  44. (define OPEN_WRITE "w")
  45. (define OPEN_BOTH "r+")
  46. (define *null-device* "/dev/null")
  47. (define (open-input-file str)
  48. "Takes a string naming an existing file and returns an input port
  49. capable of delivering characters from the file. If the file
  50. cannot be opened, an error is signalled."
  51. (open-file str OPEN_READ))
  52. (define (open-output-file str)
  53. "Takes a string naming an output file to be created and returns an
  54. output port capable of writing characters to a new file by that
  55. name. If the file cannot be opened, an error is signalled. If a
  56. file with the given name already exists, the effect is unspecified."
  57. (open-file str OPEN_WRITE))
  58. (define (open-io-file str)
  59. "Open file with name STR for both input and output."
  60. (open-file str OPEN_BOTH))
  61. (define close-io-port close-port)
  62. (define (call-with-input-file str proc)
  63. "PROC should be a procedure of one argument, and STR should be a
  64. string naming a file. The file must
  65. already exist. These procedures call PROC
  66. with one argument: the port obtained by opening the named file for
  67. input or output. If the file cannot be opened, an error is
  68. signalled. If the procedure returns, then the port is closed
  69. automatically and the value yielded by the procedure is returned.
  70. If the procedure does not return, then the port will not be closed
  71. automatically unless it is possible to prove that the port will
  72. never again be used for a read or write operation."
  73. (let* ((file (open-input-file str))
  74. (ans (proc file)))
  75. (close-input-port file)
  76. ans))
  77. (define (call-with-output-file str proc)
  78. "PROC should be a procedure of one argument, and STR should be a
  79. string naming a file. The behaviour is unspecified if the file
  80. already exists. These procedures call PROC
  81. with one argument: the port obtained by opening the named file for
  82. input or output. If the file cannot be opened, an error is
  83. signalled. If the procedure returns, then the port is closed
  84. automatically and the value yielded by the procedure is returned.
  85. If the procedure does not return, then the port will not be closed
  86. automatically unless it is possible to prove that the port will
  87. never again be used for a read or write operation."
  88. (let* ((file (open-output-file str))
  89. (ans (proc file)))
  90. (close-output-port file)
  91. ans))
  92. (define (with-input-from-port port thunk)
  93. (let* ((swaports (lambda () (set! port (set-current-input-port port)))))
  94. (dynamic-wind swaports thunk swaports)))
  95. (define (with-output-to-port port thunk)
  96. (let* ((swaports (lambda () (set! port (set-current-output-port port)))))
  97. (dynamic-wind swaports thunk swaports)))
  98. (define (with-error-to-port port thunk)
  99. (let* ((swaports (lambda () (set! port (set-current-error-port port)))))
  100. (dynamic-wind swaports thunk swaports)))
  101. (define (with-input-from-file file thunk)
  102. "THUNK must be a procedure of no arguments, and FILE must be a
  103. string naming a file. The file must already exist. The file is opened for
  104. input, an input port connected to it is made
  105. the default value returned by `current-input-port',
  106. and the THUNK is called with no arguments.
  107. When the THUNK returns, the port is closed and the previous
  108. default is restored. Returns the value yielded by THUNK. If an
  109. escape procedure is used to escape from the continuation of these
  110. procedures, their behavior is implementation dependent."
  111. (let* ((nport (open-input-file file))
  112. (ans (with-input-from-port nport thunk)))
  113. (close-port nport)
  114. ans))
  115. (define (with-output-to-file file thunk)
  116. "THUNK must be a procedure of no arguments, and FILE must be a
  117. string naming a file. The effect is unspecified if the file already exists.
  118. The file is opened for output, an output port connected to it is made
  119. the default value returned by `current-output-port',
  120. and the THUNK is called with no arguments.
  121. When the THUNK returns, the port is closed and the previous
  122. default is restored. Returns the value yielded by THUNK. If an
  123. escape procedure is used to escape from the continuation of these
  124. procedures, their behavior is implementation dependent."
  125. (let* ((nport (open-output-file file))
  126. (ans (with-output-to-port nport thunk)))
  127. (close-port nport)
  128. ans))
  129. (define (with-error-to-file file thunk)
  130. "THUNK must be a procedure of no arguments, and FILE must be a
  131. string naming a file. The effect is unspecified if the file already exists.
  132. The file is opened for output, an output port connected to it is made
  133. the default value returned by `current-error-port',
  134. and the THUNK is called with no arguments.
  135. When the THUNK returns, the port is closed and the previous
  136. default is restored. Returns the value yielded by THUNK. If an
  137. escape procedure is used to escape from the continuation of these
  138. procedures, their behavior is implementation dependent."
  139. (let* ((nport (open-output-file file))
  140. (ans (with-error-to-port nport thunk)))
  141. (close-port nport)
  142. ans))
  143. (define (with-input-from-string string thunk)
  144. "THUNK must be a procedure of no arguments.
  145. The test of STRING is opened for
  146. input, an input port connected to it is made,
  147. and the THUNK is called with no arguments.
  148. When the THUNK returns, the port is closed.
  149. Returns the value yielded by THUNK. If an
  150. escape procedure is used to escape from the continuation of these
  151. procedures, their behavior is implementation dependent."
  152. (call-with-input-string string
  153. (lambda (p) (with-input-from-port p thunk))))
  154. (define (with-output-to-string thunk)
  155. "Calls THUNK and returns its output as a string."
  156. (call-with-output-string
  157. (lambda (p) (with-output-to-port p thunk))))
  158. (define (with-error-to-string thunk)
  159. "Calls THUNK and returns its error output as a string."
  160. (call-with-output-string
  161. (lambda (p) (with-error-to-port p thunk))))
  162. (define the-eof-object (call-with-input-string "" (lambda (p) (read-char p))))
  163. ;;;; Loading
  164. (if (not (defined? '%load-verbosely))
  165. (define %load-verbosely #f))
  166. (define (assert-load-verbosity v) (set! %load-verbosely v))
  167. (define (%load-announce file)
  168. (if %load-verbosely
  169. (with-output-to-port (current-error-port)
  170. (lambda ()
  171. (display ";;; ")
  172. (display "loading ")
  173. (display file)
  174. (newline)
  175. (force-output)))))
  176. (set! %load-hook %load-announce)
  177. (define (load name . reader)
  178. (with-fluid* current-reader (and (pair? reader) (car reader))
  179. (lambda ()
  180. (start-stack 'load-stack
  181. (primitive-load name)))))