Commit c9be3c69911663e203375649b0dd024845c146d9

Thomas de Grivel 2020-03-10T18:34:59

fix newlines

diff --git a/core/host.lisp b/core/host.lisp
index 5deef57..46f47fa 100644
--- a/core/host.lisp
+++ b/core/host.lisp
@@ -22,6 +22,18 @@
   "Run a command at the current host. COMMAND is assembled using STR."
   (apply #'host-run (current-host) command))
 
+(defun strip-last-newline (string)
+  (when (stringp string)
+    (let* ((len (length string))
+           (len-1 (1- len)))
+      (if (< len 1)
+          string
+          (when (char= #\Newline (char string len-1))
+            (subseq string 0 len-1))))))
+
+(defun run-1 (&rest command)
+  (strip-last-newline (first (apply #'run command))))
+
 ;;  localhost
 
 (defun local-hostname ()
@@ -164,7 +176,7 @@
             (list :os os)))))))
 
 (defmethod probe-hostname ((host host) (os os-unix))
-  (list :hostname (first (run "hostname"))))
+  (list :hostname (run-1 "hostname")))
 
 (defmethod probe-boot-time ((host host) (os os-unix))
   (with-uptime<1> (time uptime users load1 load5 load15) (run "uptime")
@@ -172,7 +184,7 @@
                               (str uptime " seconds ago"))))))
 
 (defmethod probe-host-user ((host host) (os os-unix))
-  (list :user (first (run "whoami"))))
+  (list :user (run-1 "whoami")))
 
 (defmethod compare-property-values ((host host)
                                     (property (eql :os))
diff --git a/test.lisp b/test.lisp
index bdeaade..7da15f0 100644
--- a/test.lisp
+++ b/test.lisp
@@ -27,7 +27,7 @@
 (setf (debug-p :shell) t)
 (setf (debug-p :sb-shell) nil)
 
-(assert (string= (machine-instance) (first (run "hostname"))))
+(assert (string= (machine-instance) (run-1 "hostname")))
 
 (adams:clear-resources)
 (adams:clear-probed)
diff --git a/unix/commands.lisp b/unix/commands.lisp
index 0a7c506..b5018a9 100644
--- a/unix/commands.lisp
+++ b/unix/commands.lisp
@@ -23,7 +23,7 @@
 (defun uname ()
   (re-bind #~"^(\S+) (\S+) (\S+) (.+) (\S+)$"
       (os-name node-name os-release os-version machine)
-    (first (run "uname -a"))))
+    (run-1 "uname -a")))
 
 (defun grep_ (pattern &rest files)
   (join-str " " "grep" (sh-quote pattern) (mapcar #'sh-quote files)))
diff --git a/unix/freebsd.lisp b/unix/freebsd.lisp
index e279a38..7fb3042 100644
--- a/unix/freebsd.lisp
+++ b/unix/freebsd.lisp
@@ -35,7 +35,7 @@
           file)))
     
 (defmethod probe-hostname ((host host) (os os-freebsd))
-  (let ((hostname (first (run "hostname")))
+  (let ((hostname (run-1 "hostname"))
         (rc-conf (get-sh-var "hostname" "/etc/rc.conf")))
     (list :hostname (if (equal hostname rc-conf)
                         hostname
diff --git a/unix/openbsd.lisp b/unix/openbsd.lisp
index 7abf3cb..acc4cef 100644
--- a/unix/openbsd.lisp
+++ b/unix/openbsd.lisp
@@ -21,7 +21,7 @@
 (in-re-readtable)
 
 (defmethod probe-hostname ((host host) (os os-openbsd))
-  (list :hostname (first (run "hostname -s"))))
+  (list :hostname (run-1 "hostname -s")))
 
 (define-resource-class openbsd-pkg (pkg)
   ()
diff --git a/unix/syntaxes.lisp b/unix/syntaxes.lisp
index 78cc79d..d1ff204 100644
--- a/unix/syntaxes.lisp
+++ b/unix/syntaxes.lisp
@@ -30,7 +30,8 @@
 
 (define-syntax passwd<5> (name pass
 			  (#'parse-integer uid gid)
-			  realname home shell)
+			  realname home
+                          (#'strip-last-newline shell))
   #~|^([^:]*):([^:]*):([^:]*):([^:]*):([^:]*):([^:]*):([^:]*)$|
   "Syntax for the password file /etc/passwd. See passwd(5).")