Debian Codegraph @

lib/slib.scm

raw
1 ;;; "slib.scm" configuration template of slib:features for Scheme -*-scheme-*-
2 ;;; Author: Shiro Kawai
3 ;;; based on the "Template.scm" by Aubrey Jaffer
4 ;;;
5 ;;; This code is in the public domain.
6
7 ;; SLIB module exports all symbols for compatibility.
8 (define-module slib
9 (use srfi.13)
10 (extend util.record) ;; SLIB-compatible make-record-type
11 (use file.util)
12 (use gauche.uvector) ;; Used to implement 'byte' API
13 (use gauche.threads) ;; For make-exchanger
14 (export-all))
15 (select-module slib)
16
17 ;;@ (software-type) should be set to the generic operating system type.
18 ;;; unix, vms, macos, amiga and ms-dos are supported.
19
20 (define (software-type)
21 (cond-expand
22 [gauche.windows 'ms-dos]
23 [else 'unix]))
24
25 ;;@ (scheme-implementation-type) should return the name of the scheme
26 ;;; implementation loading this file.
27 (define (scheme-implementation-type) 'gauche)
28
29 ;;@ (scheme-implementation-home-page) should return a (string) URI
30 ;;; (Uniform Resource Identifier) for this scheme implementation's home
31 ;;; page; or false if there isn't one.
32 (define (scheme-implementation-home-page)
33 "http://practical-scheme.net/gauche/")
34
35 ;;@ (scheme-implementation-version) should return a string describing
36 ;;; the version the scheme implementation loading this file.
37 (define (scheme-implementation-version) (gauche-version))
38
39 ;;@ (implementation-vicinity) should be defined to be the pathname of
40 ;;; the directory where any auxillary files to your Scheme
41 ;;; implementation reside.
42 (define (implementation-vicinity)
43 (string-append (gauche-library-directory) "/"))
44
45 ;;@ (library-vicinity) should be defined to be the pathname of the
46 ;;; directory where files of Scheme library functions reside.
47 (define library-vicinity
48 (let ((library-path
49 (or
50 ;; Use this getenv if your implementation supports it.
51 (and-let1 p (sys-getenv "SCHEME_LIBRARY_PATH")
52 (string-append p "/"))
53 ;; Use this path if your scheme does not support GETENV
54 ;; or if SCHEME_LIBRARY_PATH is not set.
55 (case (software-type)
56 ((unix) (regexp-replace "[^\/]$"
57 (with-module gauche.internal SLIB_DIR)
58 (^m #"~(m 0)/")))
59 ((vms) "lib$scheme:")
60 ((ms-dos) "C:\\SLIB\\")
61 (else "")))))
62 (lambda () library-path)))
63
64 ;;@ (home-vicinity) should return the vicinity of the user's HOME
65 ;;; directory, the directory which typically contains files which
66 ;;; customize a computer environment for a user.
67 (define (home-vicinity)
68 (let ((home (or (sys-getenv "HOME")
69 (home-directory))))
70 (and home
71 (case (software-type)
72 ((unix coherent ms-dos) ;V7 unix has a / on HOME
73 (if (eqv? #\/ (string-ref home (+ -1 (string-length home))))
74 home
75 (string-append home "/")))
76 (else home)))))
77
78 ;@
79 (define in-vicinity string-append)
80 ;@
81 (define (user-vicinity)
82 (case (software-type)
83 ((vms) "[.]")
84 (else "")))
85
86 (define *load-pathname* #f)
87 ;@
88 (define vicinity:suffix?
89 (let ((suffi
90 (case (software-type)
91 ((amiga) '(#\: #\/))
92 ((macos thinkc) '(#\:))
93 ((ms-dos windows atarist os/2) '(#\\ #\/))
94 ((nosve) '(#\: #\.))
95 ((unix coherent plan9) '(#\/))
96 ((vms) '(#\: #\]))
97 (else
98 (slib:warn "require.scm" 'unknown 'software-type (software-type))
99 "/"))))
100 (lambda (chr) (and (memv chr suffi) #t))))
101 ;@
102 (define (pathname->vicinity pathname)
103 (let loop ((i (- (string-length pathname) 1)))
104 (cond ((negative? i) "")
105 ((vicinity:suffix? (string-ref pathname i))
106 (substring pathname 0 (+ i 1)))
107 (else (loop (- i 1))))))
108 (define (program-vicinity)
109 (if *load-pathname*
110 (pathname->vicinity *load-pathname*)
111 (slib:error 'program-vicinity " called; use slib:load to load")))
112 ;@
113 (define sub-vicinity
114 (case (software-type)
115 ((vms) (lambda
116 (vic name)
117 (let ((l (string-length vic)))
118 (if (or (zero? (string-length vic))
119 (not (char=? #\] (string-ref vic (- l 1)))))
120 (string-append vic "[" name "]")
121 (string-append (substring vic 0 (- l 1))
122 "." name "]")))))
123 (else (let ((*vicinity-suffix*
124 (case (software-type)
125 ((nosve) ".")
126 ((macos thinkc) ":")
127 ((ms-dos windows atarist os/2) "\\")
128 ((unix coherent plan9 amiga) "/"))))
129 (lambda (vic name)
130 (string-append vic name *vicinity-suffix*))))))
131 ;@
132 (define (make-vicinity <pathname>) <pathname>)
133 ;@
134 (define with-load-pathname
135 (let ((exchange
136 (lambda (new)
137 (let ((old *load-pathname*))
138 (set! *load-pathname* new)
139 old))))
140 (lambda (path thunk)
141 (let ((old #f))
142 (dynamic-wind
143 (lambda () (set! old (exchange path)))
144 thunk
145 (lambda () (exchange old)))))))
146
147 ;;@ SLIB:FEATURES is a list of symbols naming the (SLIB) features
148 ;;; initially supported by this implementation.
149 (define slib:features
150 '(
151 source ;can load scheme source files
152 ;(SLIB:LOAD-SOURCE "filename")
153 ;;; compiled ;can load compiled files
154 ;(SLIB:LOAD-COMPILED "filename")
155 vicinity
156 srfi-59
157 srfi-96
158
159 ;; Scheme report features
160 ;; R5RS-compliant implementations should provide all 9 features.
161
162 r5rs ;conforms to
163 eval ;R5RS two-argument eval
164 values ;R5RS multiple values
165 dynamic-wind ;R5RS dynamic-wind
166 macro ;R5RS high level macros
167 delay ;has DELAY and FORCE
168 multiarg-apply ;APPLY can take more than 2 args.
169 char-ready?
170 rev4-optional-procedures ;LIST-TAIL, STRING-COPY,
171 ;STRING-FILL!, and VECTOR-FILL!
172
173 ;; These four features are optional in both R4RS and R5RS
174
175 multiarg/and- ;/ and - can take more than 2 args.
176 rationalize
177 ;;; transcript ;TRANSCRIPT-ON and TRANSCRIPT-OFF
178 with-file ;has WITH-INPUT-FROM-FILE and
179 ;WITH-OUTPUT-TO-FILE
180
181 r4rs ;conforms to
182
183 ieee-p1178 ;conforms to
184
185 ;;; r3rs ;conforms to
186
187 ;;; rev2-procedures ;SUBSTRING-MOVE-LEFT!,
188 ;SUBSTRING-MOVE-RIGHT!,
189 ;SUBSTRING-FILL!,
190 ;STRING-NULL?, APPEND!, 1+,
191 ;-1+, <?, <=?, =?, >?, >=?
192 ;;; object-hash ;has OBJECT-HASH
193 ;; NB: Gauche's object-hash is different from SLIB wants, which
194 ;; is Gauche's eqv-hash. Providing it in the name of object-hash
195 ;; would be confusing, so we don't provide this.
196
197 full-continuation ;can return multiple times
198 ieee-floating-point ;conforms to IEEE Standard 754-1985
199 ;IEEE Standard for Binary
200 ;Floating-Point Arithmetic.
201
202 ;; Other common features
203
204 ;; NB: we turned off srfi-0 here, since if this is on, slib tries
205 ;; to import all available srfis, including srfi-29, which defines
206 ;; incompatible 'format'.
207 ;;; srfi-0 ;srfi-0, COND-EXPAND finds all srfi-*
208 ;;; sicp ;runs code from Structure and
209 ;Interpretation of Computer
210 ;Programs by Abelson and Sussman.
211 defmacro ;has Common Lisp DEFMACRO
212 ;;; syntax-case ;has syncase:eval and syncase:load
213 record ;has user defined data structures
214 string-port ;has CALL-WITH-INPUT-STRING and
215 ;CALL-WITH-OUTPUT-STRING
216 sort
217 ;;; pretty-print
218 object->string
219 ;;; format ;Common-lisp output formatting
220 ;;; trace ;has macros: TRACE and UNTRACE
221 ;;; compiler ;has (COMPILER)
222 ;;; ed ;(ED) is editor
223 system ;posix (system <string>)
224 getenv ;posix (getenv <string>)
225 program-arguments ;returns list of strings (argv)
226 current-time ;returns time in seconds since 1/1/1970
227
228 ;; Implementation Specific features
229 byte ;byte string manipulation
230 ))
231
232 ;;@ (FILE-POSITION <port> . <k>)
233 (define (file-position . args) #f)
234
235 ;;@ (OUTPUT-PORT-WIDTH <port>)
236 (define (output-port-width . arg) 79)
237
238 ;;@ (OUTPUT-PORT-HEIGHT <port>)
239 (define (output-port-height . arg) 24)
240
241 ;;@ (CURRENT-ERROR-PORT) - Gauche has it
242 ;(define current-error-port
243 ; (let ((port (current-output-port)))
244 ; (lambda () port)))
245
246 ;;@ (TMPNAM) makes a temporary file name.
247 (define tmpnam sys-tmpnam)
248
249 ;;@ SYSTEM
250 (define system sys-system)
251
252 ;;@ GETENV
253 (define getenv sys-getenv)
254
255 ;;@ (FILE-EXISTS? <string>)
256 ; Gauche has this
257
258 ;;@ (DELETE-FILE <string>)
259 (define delete-file (with-module file.util delete-file))
260
261 ;;@ FORCE-OUTPUT flushes any pending output on optional arg output port
262 ;;; use this definition if your system doesn't have such a procedure.
263 (define force-output flush)
264
265 ;;@ CURRENT-TIME
266 (define current-time sys-time)
267
268 ;;@ PROGRAM-ARGUMENTS
269 (define (program-arguments) (with-module user *argv*))
270
271 ;;; CALL-WITH-INPUT-STRING and CALL-WITH-OUTPUT-STRING are the string
272 ;;; port versions of CALL-WITH-*PUT-FILE.
273
274 ;;@ "rationalize" adjunct procedures.
275 (define (find-ratio x e)
276 (let ((rat (rationalize x e)))
277 (list (numerator rat) (denominator rat))))
278 (define (find-ratio-between x y)
279 (find-ratio (/ (+ x y) 2) (/ (- x y) 2)))
280
281 ;;@ CHAR-CODE-LIMIT is one greater than the largest integer which can
282 ;;; be returned by CHAR->INTEGER.
283 (define char-code-limit (+ *char-code-max* 1))
284
285 ;;@ MOST-POSITIVE-FIXNUM is used in modular.scm
286 (define most-positive-fixnum (greatest-fixnum))
287
288 ;;@ Return argument
289 ;(define (identity x) x) ; Gauche has this.
290
291 ;;@ SLIB:EVAL is single argument eval using the top-level (user) environment.
292 (define (slib:eval expr) (eval expr (interaction-environment)))
293
294 ;; If your implementation provides R4RS macros:
295 (define macro:eval slib:eval)
296 ;;@ %SLIB-LOAD loads file in slib module.
297 (define (%slib-load file)
298 (load file :environment (current-module)))
299 (define macro:load %slib-load)
300
301 (define-syntax defmacro
302 (syntax-rules ()
303 ((_ name params . body) (define-macro (name . params) . body))))
304
305 ;; Gauche has these
306 ; macroexpand-1
307 ; macroexpand
308 ;@
309 (define (gentemp) (gensym "slib:G"))
310
311 (define base:eval slib:eval)
312 ;@
313 (define (defmacro:eval x) (eval x (current-module)))
314 (define (macro:expand x) (macroexpand x))
315 ;(define (defmacro:expand* x)
316 ; (require 'defmacroexpand) (apply defmacro:expand* x '()))
317 ;@
318 (define defmacro:load %slib-load)
319 ;; slib:eval-load definition moved to "require.scm"
320 ;@
321 (define slib:warn
322 (lambda args
323 (let ((cep (current-error-port)))
324 ;;(if (provided? 'trace) (print-call-stack cep))
325 (display "Warn: " cep)
326 (for-each (lambda (x) (display #\space cep) (write x cep)) args)
327 (newline cep))))
328
329 ;;@ define an error procedure for the library
330 (define slib:error error)
331 ;@
332 (define (make-exchanger obj)
333 (let1 a (atom (box obj))
334 (lambda (new) (atomic a (^[b] (begin0 (unbox b) (set-box! b new)))))))
335 (define (open-file filename modes)
336 (case modes
337 ((r rb) (open-input-file filename))
338 ((w wb) (open-output-file filename))
339 (else (slib:error 'open-file 'mode? modes))))
340 (define (port? obj) (or (input-port? obj) (output-port? obj)))
341 (define (call-with-open-ports . ports)
342 (define proc (car ports))
343 (cond ((procedure? proc) (set! ports (cdr ports)))
344 (else (set! ports (reverse ports))
345 (set! proc (car ports))
346 (set! ports (reverse (cdr ports)))))
347 (let ((ans (apply proc ports)))
348 (for-each close-port ports)
349 ans))
350 (define (close-port port)
351 (cond ((input-port? port)
352 (close-input-port port)
353 (if (output-port? port) (close-output-port port)))
354 ((output-port? port) (close-output-port port))
355 (else (slib:error 'close-port 'port? port))))
356 ;@
357 (define (browse-url url)
358 (define (try cmd end) (zero? (system (string-append cmd url end))))
359 (or (try "xdg-open '" "' &")
360 (try "firefox '" "' &")))
361
362 ;@
363 (define object->string write-to-string)
364
365 ;;@ define these as appropriate for your system.
366 (define slib:tab (integer->char 9))
367 (define slib:form-feed (integer->char 12))
368
369 ;;@ Support for older versions of Scheme. Not enough code for its own file.
370 ;(define (last-pair l) (if (pair? (cdr l)) (last-pair (cdr l)) l)) ; Gauche has this.
371 (define t #t)
372 (define nil #f)
373
374 ;;; byte string operators. SLIB's byte module uses slib array.
375 ;;; It's more efficient to use u8vector in Gauche.
376
377 (define (byte-ref s k) (u8vector-ref s k))
378 (define (byte-set! s k b) (u8vector-set! s k b))
379 (define make-bytes make-u8vector)
380 (define (bytes-length s) (u8vector-length s))
381 (define bytes u8vector)
382 (define bytes->list u8vector->list)
383 (define list->bytes list->u8vector)
384 (define bytes->string u8vector->string)
385 (define string->bytes string->u8vector)
386 (define bytes-copy u8vector-copy)
387 ;; write-byte - Gauche has this
388 ;; read-byte - Gauche has this
389
390 ;;@ Define these if your implementation's syntax can support it and if
391 ;;; they are not already defined.
392 (define (1+ n) (+ n 1))
393 (define (-1+ n) (+ n -1))
394 (define 1- -1+)
395
396 ;;@ Define SLIB:EXIT to be the implementation procedure to exit or
397 ;;; return if exiting not supported.
398 (define slib:exit exit)
399
400 ;;@ Here for backward compatability
401 (define scheme-file-suffix
402 (let ((suffix (case (software-type)
403 ((nosve) "_scm")
404 (else ".scm"))))
405 (lambda () suffix)))
406
407 ;;@ (SLIB:LOAD-SOURCE "foo") should load "foo.scm" or with whatever
408 ;;; suffix all the module files in SLIB have. See feature 'SOURCE.
409 (define (slib:load-source f) (load (string-append f ".scm")))
410
411 ;;@ (SLIB:LOAD-COMPILED "foo") should load the file that was produced
412 ;;; by compiling "foo.scm" if this implementation can compile files.
413 ;;; See feature 'COMPILED.
414 (define slib:load-compiled load)
415
416 ;;@ At this point SLIB:LOAD must be able to load SLIB files.
417 (define slib:load load)
418
419 ;; We have to provide slib-related srfis now, since slib's require.scm
420 ;; tries to load all available srfis.
421 (provide "srfi-59")
422 (define-module srfi-59)
423 (provide "srfi-96")
424 (define-module srfi-96)
425
426 ;; [SK] Emit comprehensive message in case we can't find SLIB
427 ;(slib:load (in-vicinity (library-vicinity) "require"))
428 (unless (load (in-vicinity (library-vicinity) "require")
429 :error-if-not-found #f)
430 (error #"Couldn't load SLIB's require.scm in `~(library-vicinity)'. \
431 Either SLIB is not installed, or it is installed in a different \
432 location. Try setting the environment variable \
433 SCHEME_LIBRARY_PATH to point to the SLIB directory; or you can \
434 reconfigure Gauche with --with-slib option and reinstall." ))
435
436 ;;; A trick to make require work both on Gauche files and slib files.
437 ;;; The hint is taken from STk.
438
439 (define-macro (require feature) ;redefine
440 (if (string? feature)
441 `',(%require feature) ;gauche version
442 `(slib:require ,feature))) ;slib version
443
444 (define (provide feature)
445 (if (string? feature)
446 (with-module gauche (provide feature)) ;gauche version
447 (slib:provide feature))) ;slib version
448
449 (define (provided? feature)
450 (if (string? feature)
451 (with-module gauche (provided? feature)) ;gauche version
452 (slib:provided? feature))) ;slib version