Enforce LANG=C for all shell-command-to-string calls

This commit is contained in:
Lucien Cartier-Tilet 2021-11-06 23:47:40 +01:00
parent 0a8a340260
commit b2eff90711
Signed by: phundrak
GPG Key ID: BD7789E705CB8DCA
1 changed files with 23 additions and 13 deletions

View File

@ -184,6 +184,11 @@
"Helper macro for applying face `PROPERTIES' to `STR'." "Helper macro for applying face `PROPERTIES' to `STR'."
`(propertize ,str 'face (list ,@properties))) `(propertize ,str 'face (list ,@properties)))
(defun eshell-info-banner--shell-command-to-string (command)
"Execute shell command COMMAND and return its output as a string.
Ensures the command is ran with LANG=C."
(shell-command-to-string (format "LANG=C %s" command)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Internal functions ; ; Internal functions ;
@ -196,12 +201,12 @@
If the executable `uptime' is not found, return nil." If the executable `uptime' is not found, return nil."
(when (executable-find "uptime") (when (executable-find "uptime")
(let ((uptime-str (shell-command-to-string "uptime -p"))) (let ((uptime-str (eshell-info-banner--shell-command-to-string "uptime -p")))
(if (not (seq-some (lambda (keyword) (if (not (seq-some (lambda (keyword)
(string-match-p keyword uptime-str)) (string-match-p keyword uptime-str))
'("invalid" "illegal"))) '("invalid" "illegal")))
(s-chop-prefix "up " (s-trim uptime-str)) (s-chop-prefix "up " (s-trim uptime-str))
(let ((uptime-str (shell-command-to-string "LANG=C uptime"))) (let ((uptime-str (eshell-info-banner--shell-command-to-string "uptime")))
(save-match-data (save-match-data
(string-match "[^,]+up *\\([^,]+\\)," uptime-str) (string-match "[^,]+up *\\([^,]+\\)," uptime-str)
(s-trim (substring-no-properties uptime-str (s-trim (substring-no-properties uptime-str
@ -290,7 +295,7 @@ chosen. Relies on the `duf' command."
Return detected partitions as a list of structs. See Return detected partitions as a list of structs. See
`eshell-info-banner-partition-prefixes' to see how partitions are `eshell-info-banner-partition-prefixes' to see how partitions are
chosen. Relies on the `df' command." chosen. Relies on the `df' command."
(let ((partitions (split-string (shell-command-to-string "LANG=C df -lH") (regexp-quote "\n") t))) (let ((partitions (split-string (eshell-info-banner--shell-command-to-string "df -lH") (regexp-quote "\n") t)))
(-keep (lambda (partition) (-keep (lambda (partition)
(let* ((partition (split-string partition " " t)) (let* ((partition (split-string partition " " t))
(filesystem (nth 0 partition)) (filesystem (nth 0 partition))
@ -327,7 +332,7 @@ chosen."
Return detected partitions as a list of structs. See Return detected partitions as a list of structs. See
`eshell-info-banner-partition-prefixes' to see how partitions are `eshell-info-banner-partition-prefixes' to see how partitions are
chosen. Relies on the `df' command." chosen. Relies on the `df' command."
(let ((partitions (split-string (shell-command-to-string "LANG=C df -lH") (regexp-quote "\n") t))) (let ((partitions (split-string (eshell-info-banner--shell-command-to-string "df -lH") (regexp-quote "\n") t)))
(-keep (lambda (partition) (-keep (lambda (partition)
(let* ((partition (split-string partition " " t)) (let* ((partition (split-string partition " " t))
(filesystem (nth 0 partition)) (filesystem (nth 0 partition))
@ -355,7 +360,7 @@ Return detected partitions as a list of structs."
(if eshell-info-banner-use-duf (if eshell-info-banner-use-duf
(eshell-info-banner--get-mounted-partitions/duf) (eshell-info-banner--get-mounted-partitions/duf)
(pcase system-type (pcase system-type
((or 'gnu 'gnu/linux 'gnu/kfreebsd) ((or 'gnu 'gnu/linux 'gnu/kfreebsd 'berkeley-unix)
(eshell-info-banner--get-mounted-partitions/gnu)) (eshell-info-banner--get-mounted-partitions/gnu))
((or 'ms-dos 'windows-nt 'cygwin) ((or 'ms-dos 'windows-nt 'cygwin)
(eshell-info-banner--get-mounted-partitions/windows)) (eshell-info-banner--get-mounted-partitions/windows))
@ -405,7 +410,7 @@ For TEXT-PADDING and BAR-LENGTH, see the documentation of
(list (s-chop-suffix ":" (nth 0 line)) ; name (list (s-chop-suffix ":" (nth 0 line)) ; name
(string-to-number (nth 1 line)) ; total (string-to-number (nth 1 line)) ; total
(string-to-number (nth 2 line))))) ; used (string-to-number (nth 2 line))))) ; used
(split-string (shell-command-to-string "LANG=C free -b | tail -2") (split-string (eshell-info-banner--shell-command-to-string "free -b | tail -2")
"\n" "\n"
t))) t)))
@ -413,7 +418,12 @@ For TEXT-PADDING and BAR-LENGTH, see the documentation of
"Get memory usage for UNIX systems. "Get memory usage for UNIX systems.
Compatible with Darwin and FreeBSD at least." Compatible with Darwin and FreeBSD at least."
(let* ((command-to-mem (lambda (command) (let* ((command-to-mem (lambda (command)
(string-to-number (s-trim (cadr (split-string (shell-command-to-string command) " " t))))))) (string-to-number
(s-trim
(cadr
(split-string (eshell-info-banner--shell-command-to-string command)
" "
t)))))))
`(("RAM" `(("RAM"
,(apply command-to-mem '("sysctl hw.physmem")) ,(apply command-to-mem '("sysctl hw.physmem"))
,(apply command-to-mem '("sysctl hw.usermem")))))) ,(apply command-to-mem '("sysctl hw.usermem"))))))
@ -422,7 +432,7 @@ Compatible with Darwin and FreeBSD at least."
(defun eshell-info-banner--get-memory/darwin () (defun eshell-info-banner--get-memory/darwin ()
"Get memory usage for macOS." "Get memory usage for macOS."
(let* ((mem (s-lines (shell-command-to-string "LANG=C vm_stat"))) (let* ((mem (s-lines (eshell-info-banner--shell-command-to-string "vm_stat")))
(mem (cl-remove-if-not (lambda (line) (mem (cl-remove-if-not (lambda (line)
(string-match-p "^Pages \\(free\\|active\\|inactive\\|speculative\\|wired\\)" (string-match-p "^Pages \\(free\\|active\\|inactive\\|speculative\\|wired\\)"
line)) line))
@ -617,7 +627,7 @@ If RELEASE-FILE is nil, use '/etc/os-release'."
(defun eshell-info-banner--get-os-information-from-lsb-release () (defun eshell-info-banner--get-os-information-from-lsb-release ()
"Read the operating system information from lsb_release." "Read the operating system information from lsb_release."
(shell-command-to-string "lsb_release -d -s")) (eshell-info-banner--shell-command-to-string "lsb_release -d -s"))
(defun eshell-info-banner--get-os-information-from-registry () (defun eshell-info-banner--get-os-information-from-registry ()
"Read the operating system information from the Windows registry." "Read the operating system information from the Windows registry."
@ -656,7 +666,7 @@ If RELEASE-FILE is nil, use '/etc/os-release'."
((file-exists-p (concat prefix "/etc/os-release")) ((file-exists-p (concat prefix "/etc/os-release"))
(eshell-info-banner--get-os-information-from-release-file)) (eshell-info-banner--get-os-information-from-release-file))
((executable-find "shepherd") ((executable-find "shepherd")
(let ((distro (car (s-lines (shell-command-to-string "guix -V"))))) (let ((distro (car (s-lines (eshell-info-banner--shell-command-to-string "guix -V")))))
(save-match-data (save-match-data
(string-match "\\([0-9\\.]+\\)" distro) (string-match "\\([0-9\\.]+\\)" distro)
(concat "Guix System " (concat "Guix System "
@ -670,7 +680,7 @@ If RELEASE-FILE is nil, use '/etc/os-release'."
(buffer-string))))) (buffer-string)))))
(t "Unknown")) (t "Unknown"))
. .
,(s-trim (shell-command-to-string "uname -rs"))))) ,(s-trim (eshell-info-banner--shell-command-to-string "uname -rs")))))
(defmacro eshell-info-banner--get-macos-name (version) (defmacro eshell-info-banner--get-macos-name (version)
"Get the name of the current macOS or OSX system based on its VERSION." "Get the name of the current macOS or OSX system based on its VERSION."
@ -684,9 +694,9 @@ If RELEASE-FILE is nil, use '/etc/os-release'."
(defun eshell-info-banner--get-os-information/darwin () (defun eshell-info-banner--get-os-information/darwin ()
"See `eshell-info-banner--get-os-information'." "See `eshell-info-banner--get-os-information'."
`(,(eshell-info-banner--get-macos-name (s-trim (shell-command-to-string "sw_vers -productVersion"))) `(,(eshell-info-banner--get-macos-name (s-trim (eshell-info-banner--shell-command-to-string "sw_vers -productVersion")))
. .
,(s-trim (shell-command-to-string "uname -rs")))) ,(s-trim (eshell-info-banner--shell-command-to-string "uname -rs"))))
(defun eshell-info-banner--get-os-information () (defun eshell-info-banner--get-os-information ()
"Get operating system identifying information. "Get operating system identifying information.