gnu: xterm: Update to 350.
[jackhill/guix/guix.git] / gnu / installer / utils.scm
CommitLineData
d0f3a672
MO
1;;; GNU Guix --- Functional package management for GNU
2;;; Copyright © 2018 Mathieu Othacehe <m.othacehe@gmail.com>
9529f785 3;;; Copyright © 2019 Ludovic Courtès <ludo@gnu.org>
d0f3a672
MO
4;;;
5;;; This file is part of GNU Guix.
6;;;
7;;; GNU Guix is free software; you can redistribute it and/or modify it
8;;; under the terms of the GNU General Public License as published by
9;;; the Free Software Foundation; either version 3 of the License, or (at
10;;; your option) any later version.
11;;;
12;;; GNU Guix is distributed in the hope that it will be useful, but
13;;; WITHOUT ANY WARRANTY; without even the implied warranty of
14;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15;;; GNU General Public License for more details.
16;;;
17;;; You should have received a copy of the GNU General Public License
18;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
19
20(define-module (gnu installer utils)
3ad8f775
MO
21 #:use-module (guix utils)
22 #:use-module (guix build utils)
9529f785
LC
23 #:use-module (guix i18n)
24 #:use-module (srfi srfi-34)
d0f3a672 25 #:use-module (ice-9 rdelim)
3ad8f775 26 #:use-module (ice-9 regex)
d0f3a672
MO
27 #:use-module (ice-9 textual-ports)
28 #:export (read-lines
3ad8f775
MO
29 read-all
30 nearest-exact-integer
31 read-percentage
32 run-shell-command))
d0f3a672
MO
33
34(define* (read-lines #:optional (port (current-input-port)))
35 "Read lines from PORT and return them as a list."
36 (let loop ((line (read-line port))
37 (lines '()))
38 (if (eof-object? line)
39 (reverse lines)
40 (loop (read-line port)
41 (cons line lines)))))
42
43(define (read-all file)
44 "Return the content of the given FILE as a string."
45 (call-with-input-file file
46 get-string-all))
3ad8f775
MO
47
48(define (nearest-exact-integer x)
49 "Given a real number X, return the nearest exact integer, with ties going to
50the nearest exact even integer."
51 (inexact->exact (round x)))
52
53(define (read-percentage percentage)
54 "Read PERCENTAGE string and return the corresponding percentage as a
55number. If no percentage is found, return #f"
56 (let ((result (string-match "^([0-9]+)%$" percentage)))
57 (and result
58 (string->number (match:substring result 1)))))
59
7611074f 60(define* (run-shell-command command #:key locale)
9529f785
LC
61 "Run COMMAND, a string, with Bash, and in the given LOCALE. Return true if
62COMMAND exited successfully, #f otherwise."
63 (define (pause)
64 (format #t (G_ "Press Enter to continue.~%"))
65 (read-line (current-input-port)))
66
3ad8f775
MO
67 (call-with-temporary-output-file
68 (lambda (file port)
7611074f
LC
69 (when locale
70 (let ((supported? (false-if-exception
71 (setlocale LC_ALL locale))))
72 ;; If LOCALE is not supported, then set LANGUAGE, which might at
73 ;; least give us translated messages.
74 (if supported?
75 (format port "export LC_ALL=\"~a\"~%" locale)
76 (format port "export LANGUAGE=\"~a\"~%"
77 (string-take locale
78 (string-index locale #\_))))))
79
9529f785 80 (format port "exec ~a~%" command)
3ad8f775 81 (close port)
9529f785
LC
82
83 (guard (c ((invoke-error? c)
84 (newline)
85 (format (current-error-port)
86 (G_ "Command failed with exit code ~a.~%")
87 (invoke-error-exit-status c))
88 (pause)
89 #f))
90 (invoke "bash" "--init-file" file)
91 (newline)
92 (pause)
93 #t))))