summaryrefslogtreecommitdiff
diff options
authorTimothy Sample <[email protected]>2024-12-18 17:23:53 -0600
committerTimothy Sample <[email protected]>2024-12-18 17:23:53 -0600
commitec9f0313190e380687da387b4207469a0a0a8cd8 (patch)
tree73c875ab1bea030b61295c40bb4ac136cd98e5e9
parentd497a40b7ed79a086e3bca08fbdd352bb53f3a08 (diff)
downloadgash-master.tar.gz
Do not rely on HOME variable being set.HEADmaster
Additionally, respect the XDG Base Directory Specification and handle an unset USER variable. See https://lists.gnu.org/archive/html/gash-devel/2024-12/msg00000.html * gash/gash.scm (get-history-file): New procedure. (main): Use it, and only read and write history when the filename can be determined. (prompt): Handle unset HOME and USER variables.
-rw-r--r--gash/gash.scm23
1 files changed, 17 insertions, 6 deletions
diff --git a/gash/gash.scm b/gash/gash.scm
index 68838e1..edf1336 100644
--- a/gash/gash.scm
+++ b/gash/gash.scm
@@ -1,6 +1,6 @@
;;; Gash --- Guile As SHell
;;; Copyright © 2016,2017,2018 R.E.W. van Beusekom <[email protected]>
-;;; Copyright © 2018, 2019 Timothy Sample <[email protected]>
+;;; Copyright © 2018, 2019, 2024 Timothy Sample <[email protected]>
;;; Copyright © 2018 Jan (janneke) Nieuwenhuizen <[email protected]>
;;;
;;; The careful invocation of 'seclocale' was taken from the
@@ -42,6 +42,7 @@
#:use-module (ice-9 receive)
#:use-module (ice-9 regex)
#:use-module (srfi srfi-1)
+ #:use-module (srfi srfi-2)
#:use-module (srfi srfi-26)
#:export (main))
@@ -85,6 +86,14 @@ This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
")))
+(define (get-history-file)
+ "Lookup the location of the history file from the system environment.
+If the location cannot be determined, return @code{#f}."
+ (and-let* ((config-home (or (getenv "XDG_CONFIG_HOME")
+ (and-let* ((home (getenv "HOME")))
+ (string-append home "/.config")))))
+ (string-append config-home "/gash-history")))
+
(define (main args)
(let ((thunk
(lambda ()
@@ -120,7 +129,7 @@ There is NO WARRANTY, to the extent permitted by law.
(lambda (port)
(exit (run-repl port parse?))))))
((isatty? (current-input-port))
- (let* ((HOME (string-append (getenv "HOME") "/.gash_history"))
+ (let* ((history-file (get-history-file))
(thunk (lambda ()
(let loop ((line (readline (prompt))))
(when (not (eof-object? line))
@@ -137,9 +146,11 @@ There is NO WARRANTY, to the extent permitted by law.
(if (eof-object? next) next
(string-append previous next))))))))))
(clear-history)
- (read-history HOME)
+ (when history-file
+ (read-history history-file))
(with-readline-completion-function completion thunk)
- (write-history HOME)
+ (when history-file
+ (write-history history-file))
(newline)))
(else (exit (run-repl (current-input-port) parse?))))))))
(thunk)))
@@ -148,12 +159,12 @@ There is NO WARRANTY, to the extent permitted by law.
(let* ((l (string #\001))
(r (string #\002))
(e (string #\033))
- (user (getenv "USER"))
+ (user (or (getenv "USER") ""))
(host (gethostname))
(home (getenv "HOME")))
(lambda ()
(let* ((cwd (getcwd))
- (cwd (if (string-prefix? home cwd)
+ (cwd (if (and home (string-prefix? home cwd))
(string-replace cwd "~" 0 (string-length home))
cwd)))
(string-append