3 ;;; GNU Mes --- Maxwell Equations of Software
4 ;;; Copyright © 2016,2017,2018 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
6 ;;; This file is part of GNU Mes.
8 ;;; GNU Mes is free software; you can redistribute it and/or modify it
9 ;;; under the terms of the GNU General Public License as published by
10 ;;; the Free Software Foundation; either version 3 of the License, or (at
11 ;;; your option) any later version.
13 ;;; GNU Mes is distributed in the hope that it will be useful, but
14 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;;; GNU General Public License for more details.
18 ;;; You should have received a copy of the GNU General Public License
19 ;;; along with GNU Mes. If not, see <http://www.gnu.org/licenses/>.
25 (mes-use-module (srfi srfi-13))
27 (define-macro (cond-expand-provide . rest) #t)
29 (mes-use-module (mes catch))
30 (mes-use-module (mes posix))
31 (mes-use-module (srfi srfi-16))
32 (mes-use-module (mes display))
34 (define (drain-input port) (read-string))
36 (define (make-string n . fill)
37 (list->string (apply make-list n fill)))
39 (define (object->string x . rest)
40 (with-output-to-string
41 (lambda () ((if (pair? rest) (car rest) write) x))))
43 (define (port-filename p) "<stdin>")
44 (define (port-line p) 0)
46 (define (with-input-from-string string thunk)
47 (let ((prev (set-current-input-port (open-input-string string)))
49 (set-current-input-port prev)
52 (define (with-input-from-file file thunk)
53 (let ((port (open-input-file file)))
55 (error 'no-such-file file)
56 (let* ((save (current-input-port))
57 (foo (set-current-input-port port))
59 (set-current-input-port save)
62 (define (with-output-to-file file thunk)
63 (let ((port (open-output-file file)))
65 (error 'cannot-open file)
66 (let* ((save (current-output-port))
67 (foo (set-current-output-port port))
69 (set-current-output-port save)
72 (define (with-error-to-file file thunk)
73 (let ((port (open-output-file file)))
75 (error 'cannot-open file)
76 (let* ((save (current-error-port))
77 (foo (set-current-error-port port))
79 (set-current-error-port save)
82 (define (with-output-to-port port thunk)
83 (let* ((save (current-output-port))
84 (foo (set-current-output-port port))
86 (set-current-output-port save)
89 (define core:open-input-file open-input-file)
90 (define (open-input-file file)
91 (let ((port (core:open-input-file file))
92 (debug (and=> (getenv "MES_DEBUG") string->number)))
93 (when (and debug (> debug 1))
94 (core:display-error (string-append "open-input-file: `" file "'"))
96 (core:display-error " port=")
97 (core:display-error port)))
98 (core:display-error "\n")
101 (define (dirname file-name)
102 (let* ((lst (string-split file-name #\/))
103 (lst (filter (negate string-null?) lst)))
104 (if (null? lst) (if (string-prefix? "/" file-name) "/" ".")
105 (let ((dir (string-join (list-head lst (1- (length lst))) "/")))
106 (if (string-prefix? "/" file-name) (string-append "/" dir)
107 (if (string-null? dir) "."
110 ;; FIXME: c&p from display
111 (define (with-output-to-string thunk)
112 (define save-write-byte write-byte)
116 (let ((out? (or (null? rest) (eq? (car rest) (current-output-port)))))
117 (if (not out?) (apply save-write-byte (cons x rest))
119 (set! stdout (append stdout (list (integer->char x))))
122 (let ((r (apply string stdout)))
123 (set! write-byte save-write-byte)
126 ;; FIXME: c&p from display
127 (define (simple-format destination format . rest)
128 (let ((port (if (boolean? destination) (current-output-port) destination))
129 (lst (string->list format)))
130 (define (simple-format lst args)
133 (if (not (eq? c #\~)) (begin (write-char (car lst) port)
134 (simple-format (cdr lst) args))
135 (let ((c (cadr lst)))
137 ((#\a) (display (car args) port))
138 ((#\s) (write (car args) port)))
139 (simple-format (cddr lst) (cdr args)))))))
141 (if destination (simple-format lst rest)
142 (with-output-to-string
143 (lambda () (simple-format lst rest))))))
145 (define format simple-format)
147 (define (file-exists? o)