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 (read-line . rest)
37 (let* ((port (if (pair? rest) (car rest) (current-input-port)))
38 (handle-delim (if (and (pair? rest) (pair? (cdr rest))) (cadr rest) 'trim))
43 (if (or (eof-object? c) (eq? c #\newline)) (case handle-delim
45 ((concat) '(#\newline))
46 (else (error (format #f "not supported: handle-delim=~a" handle-delim))))
47 (cons c (loop (read-char port)))))))))
49 (define (make-string n . fill)
50 (list->string (apply make-list n fill)))
52 (define (object->string x . rest)
53 (with-output-to-string
54 (lambda () ((if (pair? rest) (car rest) write) x))))
56 (define (port-filename p) "<stdin>")
57 (define (port-line p) 0)
59 (define (with-input-from-string string thunk)
60 (let ((prev (set-current-input-port (open-input-string string)))
62 (set-current-input-port prev)
65 (define (with-input-from-file file thunk)
66 (let ((port (open-input-file file)))
68 (error 'no-such-file file)
69 (let* ((save (current-input-port))
70 (foo (set-current-input-port port))
72 (set-current-input-port save)
75 (define (with-output-to-file file thunk)
76 (let ((port (open-output-file file)))
78 (error 'cannot-open file)
79 (let* ((save (current-output-port))
80 (foo (set-current-output-port port))
82 (set-current-output-port save)
85 (define (with-error-to-file file thunk)
86 (let ((port (open-output-file file)))
88 (error 'cannot-open file)
89 (let* ((save (current-error-port))
90 (foo (set-current-error-port port))
92 (set-current-error-port save)
95 (define (with-output-to-port port thunk)
96 (let* ((save (current-output-port))
97 (foo (set-current-output-port port))
99 (set-current-output-port save)
102 (define core:open-input-file open-input-file)
103 (define (open-input-file file)
104 (let ((port (core:open-input-file file))
105 (debug (and=> (getenv "MES_DEBUG") string->number)))
106 (when (and debug (> debug 1))
107 (core:display-error (string-append "open-input-file: `" file "'"))
109 (core:display-error " port=")
110 (core:display-error port)))
111 (core:display-error "\n")
114 (define (dirname file-name)
115 (let* ((lst (string-split file-name #\/))
116 (lst (filter (negate string-null?) lst)))
117 (if (null? lst) (if (string-prefix? "/" file-name) "/" ".")
118 (let ((dir (string-join (list-head lst (1- (length lst))) "/")))
119 (if (string-prefix? "/" file-name) (string-append "/" dir)
120 (if (string-null? dir) "."
123 ;; FIXME: c&p from display
124 (define (with-output-to-string thunk)
125 (define save-write-byte write-byte)
129 (let ((out? (or (null? rest) (eq? (car rest) (current-output-port)))))
130 (if (not out?) (apply save-write-byte (cons x rest))
132 (set! stdout (append stdout (list (integer->char x))))
135 (let ((r (apply string stdout)))
136 (set! write-byte save-write-byte)
139 ;; FIXME: c&p from display
140 (define (simple-format destination format . rest)
141 (let ((port (if (boolean? destination) (current-output-port) destination))
142 (lst (string->list format)))
143 (define (simple-format lst args)
146 (if (not (eq? c #\~)) (begin (write-char (car lst) port)
147 (simple-format (cdr lst) args))
148 (let ((c (cadr lst)))
150 ((#\a) (display (car args) port))
151 ((#\s) (write (car args) port)))
152 (simple-format (cddr lst) (cdr args)))))))
154 (if destination (simple-format lst rest)
155 (with-output-to-string
156 (lambda () (simple-format lst rest))))))
158 (define format simple-format)
160 (define (file-exists? o)