r4rs.scm 8.2 KB

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