]> ruderich.org/simon Gitweb - config/dotfiles.git/blobdiff - lib.sh
herbstluftwm/autostart: add bindings to run programs and select windows
[config/dotfiles.git] / lib.sh
diff --git a/lib.sh b/lib.sh
index 474f638c42c8fc858418656ca59ad73e9adab8da..05341537363b6e2bf19ed44df8bd50c3d184a175 100644 (file)
--- a/lib.sh
+++ b/lib.sh
@@ -2,7 +2,7 @@
 #
 # Their setup.sh script sources this file.
 
-# Copyright (C) 2009-2013  Simon Ruderich
+# Copyright (C) 2009-2018  Simon Ruderich
 #
 # This program is free software: you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
@@ -41,7 +41,7 @@ installed_path() {
         # Walk PATH.
         for directory in $PATH; do
             if test -x "$directory/$1"; then
-                echo "$directory/$1"
+                printf '%s\n' "$directory/$1"
                 return 0
             fi
         done
@@ -53,7 +53,7 @@ installed_path() {
 # Usage: cmd_i <cmd> ... <file>
 #
 # Run <cmd> with all arguments (including the last file) and write the result
-# to the temporary file <file>.tmp and then renamed that file to <file>. This
+# to the temporary file <file>.tmp and then rename that file to <file>. This
 # can't be done in-place (e.g. cmd <file >file) because it truncates the file.
 cmd_i() {
     # Get last argument.
@@ -68,7 +68,7 @@ cmd_i() {
 
 # Usage: sed_i ... <file>
 #
-# sed -i is not compatible due to different implementations. See cmd_i.
+# sed -i is not portable due to different implementations. See cmd_i.
 sed_i() {
     cmd_i sed "$@"
 }
@@ -101,7 +101,7 @@ simple_cpp() {
             break
         fi
 
-        cmd="$cmd s/\b$x\b/\$ARGV[\$i]/g; \$i++;"
+        cmd="$cmd s/\\b$x\\b/\$ARGV[\$i]/g; \$i++;"
     done
 
     perl_line_filter "$cmd" -- "$@"
@@ -111,7 +111,6 @@ simple_cpp() {
 #
 # - Debian (debian)
 # - Gentoo (gentoo)
-# - Mac OS X (darwin)
 # - Solaris/OpenSolaris (sun)
 # - FreeBSD (freebsd)
 #
@@ -121,14 +120,12 @@ os() {
         echo debian
     elif test -f /etc/gentoo-release; then
         echo gentoo
-    elif test x`uname` = xDarwin; then
-        echo darwin
-    elif test x`uname` = xSunOS; then
+    elif test x"`uname`" = xSunOS; then
         echo sun
-    elif test x`uname` = xFreeBSD; then
+    elif test x"`uname`" = xFreeBSD; then
         echo freebsd
     else
-        echo unsupported OS! >&2
+        echo 'unsupported OS!' >&2
         return 1
     fi
 }
@@ -143,41 +140,41 @@ link() {
 
     # Get all necessary paths.
     pwd=`pwd`
-    base=`echo "$2" | sed "s|\~|$HOME|"` # expand ~, some sh don't do it
+    base=`printf '%s' "$2" | sed "s|~|$HOME|"` # expand ~, some sh don't do it
     base=`dirname "$base"`
-    source=`echo "$pwd/$1" | sed "s|$base/||"`
+    source=`printf '%s' "$pwd/$1" | sed "s|$base/||"`
     target=`basename "$2"`
 
     # Go to the directory where the link is going to be created.
-    cd "$base"
+    cd "$base" || return 1
 
     # Abort if the target file exists and is no symbolic link. Prevents
     # overwriting real files.
-    if ( test -f "$target" && test ! -h "$target" ) || \
-            ( test -s "$target" && test ! -h "$target" ); then
-        echo "link(): target '$target' exists already and is no symbolic link!" >&2
+    if test -e "$target" && test ! -h "$target"; then
+        printf 'link(): target "%s" exists already and is no symbolic link!\n' \
+               "$target" >&2
         exit 1
     fi
 
-    # Make sure the source exists (is file, directory or link).
-    if test ! -f "$source" && test ! -d "$source" && test ! -h "$source"; then
-        echo "link(): source '$source' doesn't exist!" >&2
+    # Make sure the source exists.
+    if test ! -e "$source"; then
+        printf 'link(): source "%s" does not exist!\n' "$source" >&2
         exit 1
     fi
 
     # Create the new symbolic link; remove the old one if necessary.
-    echo "link(): linking '$source' to '$target'"
+    printf 'link(): linking "%s" to "%s"\n' "$source" "$target"
     rm -f "$target"
     ln -s "$source" "$target"
 
     # Go back to the directory where we were before.
-    cd "$pwd"
+    cd "$pwd" || return 1
 }
 
 # Generate a file from a source file using a given command. A warning not to
 # edit it is automatically added to the created file.
 #
-# Usage: generated() <file> <extension> <cmd..>
+# Usage: generate <file> <extension> <cmd..>
 #
 # If an empty extension is provided, the file is modified in-place (through a
 # temporary file).
@@ -196,15 +193,18 @@ generate() {
         file_tmp="$file.tmp"
     else
         # We only need this message if we generate a new file.
-        printf "%s: generating from '%s' (%s)\n" \
+        printf '%s: generating from "%s" (%s)\n' \
             "$file" "$file$extension" "$1"
 
-        echo '###################################'  >"$file"
-        echo '# WARNING! DO NOT EDIT THIS FILE! #' >>"$file"
-        echo '###################################' >>"$file"
-        echo >>"$file"
-        echo "# It was generated from $file$extension on `date`." >>"$file"
-        echo >>"$file"
+        {
+            echo '###################################'
+            echo '# WARNING! DO NOT EDIT THIS FILE! #'
+            echo '###################################'
+            echo
+            printf '# It was generated from "%s" on %s.\n' \
+                "$file$extension" "`date`"
+            echo
+        } > "$file"
 
         file_tmp="$file"
     fi