--- /dev/null
+# Makefile for shell configuration files. At the moment only used for running
+# tests.
+
+
+test:
+ cd tests; zsh run.zsh
host=$(echo $(hostname) | sed -e 's/\..*$//')
# Load environmental related settings used by all shells.
-# If the default file exists, load it.
if [ -f ~/.shell/env ]; then
source ~/.shell/env
+else
+ function source_config() {
+ echo "Couldn't load source_config(), can't source files." >&2
+ }
fi
-# If an env file for the current hostname (first part before a dot) exists load
-# it, otherwise load env.local if available.
-if [ -f ~/.shell/env.$host ]; then
- source ~/.shell/env.$host
-elif [ -f ~/.shell/env.local ]; then
- source ~/.shell/env.local
-fi
+
+# Load global env file for current hostname (first part before a dot) or
+# global env.local.
+source_config ~/.shell "" env $host
# Use Vi(m) style in bash.
set -o vi
-# If a rc file for the current hostname (first part before a dot) exists load
-# it, otherwise load rc.local if available.
-if [ -f ~/.bash/rc.$host ]; then
- source ~/.bash/rc.$host
-elif [ -f ~/.bash/rc.local ]; then
- source ~/.bash/rc.local
-fi
+# Load rc file for current hostname (first part before a dot) or rc.local.
+source_config ~/.bash "" rc $host
# See `dircolors --print-database` for possible colors.
LS_COLORS='no=00:fi=00:di=34;01:ln=36:pi=00:so=00:bd=00:cd=00:ex=31;01'
export LS_COLORS
+
+
+# Sources a configuration file if it exists; loads a fallback .local file if
+# it doesn't. If the .local files doesn't exist nothing is sourced.
+#
+# $1: Path to directory where configuration files are located.
+# $2: Directory name in $1 where the non .local files are stored, can be
+# empty. If empty both configuration files (normal and .local) are stored
+# in $1.
+# $3: Base name of file in $2, for example "rc" or "env".
+# $4: Extension for $3, if this file doesn't exist "$1/$3.local" is sourced.
+# $5: Additional options, set to nolocal to prevent loading of "$1/$3.local"
+# if "$1/$2/$3.$4" doesn't exist.
+#
+# Example with the following directory/file structure, $os is Darwin and
+# $hostname is marvin.
+#
+# ~/.zsh
+# ~/.zsh/env
+# ~/.zsh/env.local
+# ~/.zsh/rc
+# ~/.zsh/rc.local
+# ~/.zsh/host/rc.marvin
+# ~/.zsh/os/rc.Darwin
+#
+# To load additional rc files from within ~/.zsh/rc use the following:
+#
+# source_config ~/.zsh os rc $os # loads os/rc.Darwin
+# source_config ~/.zsh host rc $hostname # loads host/rc.marvin
+#
+# To load additional env files from within ~/.zsh/env use the following (note
+# nolocal to prevent loading env.local two times if os and host files don't
+# exist):
+#
+# source_config zsh os env $os nolocal # loads os/rc.Darwin
+# source_config zsh host env $hostname # loads env.local
+#
+# Doesn't fit perfectly in this file, but this is the best place to make it
+# available everywhere.
+function source_config() {
+ # Path to the file to source and its local counterpart.
+ local source_file=$1/$2/$3.$4
+ local source_file_local=$1/$3.local
+
+ # Additional debug output.
+ if [ x$DEBUG != x ]; then
+ echo "source_config(): checking if $source_file exists"
+ echo "source_config(): checking if $source_file_local exists"
+ fi
+
+ # If the file does exist then source it.
+ if [ -f $source_file ]; then
+ if [ x$DEBUG != x ]; then
+ echo "source_config(): sourcing $source_file"
+ fi
+ source $source_file
+
+ # Otherwise load the .local file if it exists and .local files are
+ # allowed.
+ elif [ -f $source_file_local -a x$5 != xnolocal ]; then
+ if [ x$DEBUG != x ]; then
+ echo "source_config(): sourcing $source_file_local"
+ fi
+ source $source_file_local
+ fi
+}
--- /dev/null
+# Runs all tests.
+
+
+# Get all test files.
+for file in *.test; do
+ # Run the test file in zsh and sh and get its output.
+ zsh $file &> $file.out.zsh
+ sh $file &> $file.out.sh
+
+ # Check if the output matches the expected one. If not abort with exit
+ # code 1.
+ diff -u $file.out $file.out.zsh
+ if [[ $? -ne 0 ]]; then
+ exit 1
+ fi
+ diff -u $file.out $file.out.sh
+ if [[ $? -ne 0 ]]; then
+ exit 1
+ fi
+done
+
+# Remove all temporary files.
+rm -rf tmp
+rm *.out.zsh
+rm *.out.sh
--- /dev/null
+# Test file for source_config().
+
+
+# Load source_config().
+source ../shell/env
+
+
+# Make sure the old temporary directory is removed.
+rm -rf tmp
+# Create the test directories/files.
+mkdir tmp
+mkdir tmp/bash
+mkdir tmp/shell
+mkdir tmp/zsh tmp/zsh/host tmp/zsh/os
+echo echo loaded bash/env.zucker > tmp/shell/env.zucker
+echo echo loaded shell/env.zucker > tmp/shell/env.zucker
+echo echo loaded zsh.env.local > tmp/zsh/env.local
+echo echo loaded zsh/rc.local > tmp/zsh/rc.local
+echo echo loaded zsh/host/rc.zucker > tmp/zsh/host/rc.zucker
+echo echo loaded zsh/os/rc.Darwin > tmp/zsh/os/rc.Darwin
+
+
+function tests() {
+ source_config tmp/zsh os rc Darwin nolocal
+ source_config tmp/zsh host rc zucker
+
+ source_config tmp/zsh os env Darwin nolocal
+ source_config tmp/zsh host env zucker
+
+ source_config tmp/bash host rc zucker
+
+ source_config tmp/shell "" env zucker
+}
+
+# Run tests without and with debug output.
+tests
+echo
+DEBUG=1 tests
--- /dev/null
+loaded zsh/os/rc.Darwin
+loaded zsh/host/rc.zucker
+loaded zsh.env.local
+loaded shell/env.zucker
+
+source_config(): checking if tmp/zsh/os/rc.Darwin exists
+source_config(): checking if tmp/zsh/rc.local exists
+source_config(): sourcing tmp/zsh/os/rc.Darwin
+loaded zsh/os/rc.Darwin
+source_config(): checking if tmp/zsh/host/rc.zucker exists
+source_config(): checking if tmp/zsh/rc.local exists
+source_config(): sourcing tmp/zsh/host/rc.zucker
+loaded zsh/host/rc.zucker
+source_config(): checking if tmp/zsh/os/env.Darwin exists
+source_config(): checking if tmp/zsh/env.local exists
+source_config(): checking if tmp/zsh/host/env.zucker exists
+source_config(): checking if tmp/zsh/env.local exists
+source_config(): sourcing tmp/zsh/env.local
+loaded zsh.env.local
+source_config(): checking if tmp/bash/host/rc.zucker exists
+source_config(): checking if tmp/bash/rc.local exists
+source_config(): checking if tmp/shell//env.zucker exists
+source_config(): checking if tmp/shell/env.local exists
+source_config(): sourcing tmp/shell//env.zucker
+loaded shell/env.zucker
host=${$(hostname)//.*/}
# Load environmental related settings used by all shells.
-# If the default file exists, load it.
if [[ -f ~/.shell/env ]]; then
source ~/.shell/env
-fi
-# If an env file for the current hostname (first part before a dot) exists load
-# it, otherwise load env.local if available.
-if [[ -f ~/.shell/env.$host ]]; then
- source ~/.shell/env.$host
-elif [[ -f ~/.shell/env.local ]]; then
- source ~/.shell/env.local
+else
+ function source_config() {
+ echo "Couldn't load source_config(), can't source files." >&2
+ }
fi
+# Load global env file for current hostname (first part before a dot) or
+# global env.local.
+source_config ~/.shell "" env $host
-# If an env file for the current hostname (first part before a dot) exists load
-# it, otherwise load env.local if available.
-if [[ -f ~/.zsh/env.$host ]]; then;
- source ~/.zsh/env.$host
-elif [[ -f ~/.zsh/env.local ]]; then;
- source ~/.zsh/env.local
-fi
+# Load env file for current hostname (first part before a dot) or env.local.
+source_config ~/.zsh "" env $host
MYUNIX=~/Documents/unix
# Add my bin/ directory, macports bin/ and sbin/ and the X11 bin/ directory to
-# the default PATH.
-typeset -U path
+# the default PATH. -g is necessary as the file is loaded by source_config(),
+# otherwise the path wouldn't be changed outside the function.
+typeset -g -U path
path=($MYUNIX/bin /opt/local/bin /opt/local/sbin $path /usr/X11R6/bin)
# Necessary for xterm to find man pages through PATH.
}
-# If a rc file for the current hostname (first part before a dot) exists load
-# it, otherwise load rc.local if available.
-host=${$(hostname)//.*/}
-if [[ -f ~/.zsh/rc.$host ]]; then;
- source ~/.zsh/rc.$host
-elif [[ -f ~/.zsh/rc.local ]]; then;
- source ~/.zsh/rc.local
-fi
+# Load rc file for current hostname (first part before a dot) or rc.local.
+source_config ~/.zsh "" rc ${$(hostname)//.*/}