From e6a0dabe9ab4a7a6bb33a86f586ab6b0dcf1aafa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20K=C3=A4stner?= Date: Sun, 2 May 2021 21:32:23 +0200 Subject: [PATCH] os: Add Windows detection The Windows registry key "CurrentVersion" contains both "ProductName" as well as "BuildLab" to identify the currently running Windows variant. As all returned `reg` lines are either values name REG_ value or the list of subkeys, we can just use a regular expression to filter the values from the input. For a more generic variant, see Ashraz code on the Discord. See also #4 for more progress on all OS detections. --- eshell-info-banner.el | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/eshell-info-banner.el b/eshell-info-banner.el index 347ee10..c6e306d 100644 --- a/eshell-info-banner.el +++ b/eshell-info-banner.el @@ -314,12 +314,26 @@ If RELEASE-FILE is nil, use '/etc/os-release'." "Read the operating system information from lsb_release" (shell-command-to-string "lsb_release -d -s")) +(defun eshell-info-banner--get-os-information-from-registry () + "Read the operating system information from the Windows registry." + (let ((win32-name "Windows") + (win32-build "Unknown")) + (with-temp-buffer + (call-process "reg" nil t nil "query" "HKLM\\Software\\Microsoft\\Windows NT\\CurrentVersion") + (goto-char (point-min)) + (while (re-search-forward "\\([^[:blank:]]+\\) *\\(REG_[^[:blank:]]+\\) *\\(.+\\)" nil t) + (cond + ((string= "ProductName" (match-string 1)) (setq win32-name (match-string 3))) + ((string= "BuildLab" (match-string 1)) (setq win32-build (match-string 3))))) + (format "%s (%s)" win32-name win32-build)))) + (defun eshell-info-banner--get-os-information () "Get operating system identifying information." (cond ((executable-find "hostnamectl") (eshell-info-banner--get-os-information-from-hostnamectl)) ((executable-find "lsb_release") (eshell-info-banner--get-os-information-from-lsb-release)) ((file-exists-p "/etc/os-release") (eshell-info-banner--get-os-information-from-release-file)) + ((executable-find "reg") (eshell-info-banner--get-os-information-from-registry)) (t "Unknown"))) ; Public functions ;;;;;;;;;;;;;;;;;;;;