Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle system detection like shell detection. #159

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,14 @@ standalone/vimpager: vimpager vimpager-version.txt ${SRC:=.uu} inc/* Makefile
@echo building $@
@${MKPATH} ${@D}
@sed \
-e '/^ *\. .*inc\/prologue.sh"$$/{' \
-e '/^\. .*inc\/prologue.sh/{' \
-e 'r inc/prologue.sh' \
-e d \
-e '}' \
-e '/^\. .*inc\/platform.sh/{' \
-e 'r inc/platform.sh' \
-e d \
-e '}' \
-e 's/^\( *\)# EXTRACT BUNDLED SCRIPTS HERE$$/\1extract_bundled_scripts/' \
-e 's|^version=.*|version="'"`cat vimpager-version.txt`"' (standalone, shell=\$$(command -v \$$POSIX_SHELL))"|' \
-e 's!^\( *\)runtime=.*$$!\1runtime='\''\$$tmp/runtime'\''!' \
Expand All @@ -75,7 +79,7 @@ standalone/vimcat: vimcat autoload/vimcat.vim vimcat-version.txt inc/prologue.sh
-e '/^ *--cmd "set rtp^=\$$runtime" \\$$/d' \
-e '/call vimcat#Init/i\'"$$nl"'--cmd "$$silent source $$0" \\' \
-e 's/vimcat#\([^ ]*\)(/\1(/g' \
-e '/^ *\. .*inc\/prologue.sh"$$/{' \
-e '/^\. .*inc\/prologue.sh/{' \
-e 'r inc/prologue.sh' \
-e d \
-e '}' \
Expand All @@ -95,7 +99,7 @@ vimcat.uu: vimcat vimcat-version.txt
@printf "\t(cat <<'EOF') | do_uudecode > bin/vimcat\n" >> $@
@sed \
-e 's|^version=.*|version="'"`cat vimcat-version.txt`"' (bundled, shell=\$$(command -v \$$POSIX_SHELL))"|' \
-e '/^ *\. .*inc\/prologue.sh"$$/{' \
-e '/^\. .*inc\/prologue.sh/{' \
-e 'r inc/prologue.sh' \
-e d \
-e '}' \
Expand Down Expand Up @@ -184,6 +188,7 @@ install: docs vimpager.configured vimcat.configured
sed -e '1{ s|.*|#!'"$$POSIX_SHELL"'|; }' \
-e 's|\$$POSIX_SHELL|'"$$POSIX_SHELL|" \
-e '/^ *\. .*inc\/prologue.sh"$$/d' \
-e 's/^ *\. .*inc\/platform.sh"$$/'"`scripts/find_platform`"/ \
-e 's|^version=.*|version="'"`cat $<-version.txt`"' (configured, shell='"$$POSIX_SHELL"')"|' \
-e '/^# FIND REAL PARENT DIRECTORY$$/,/^# END OF FIND REAL PARENT DIRECTORY$$/d' \
-e 's!^\( *\)runtime=.*!\1runtime='\''${PREFIX}/share/vimpager'\''!' \
Expand Down
73 changes: 73 additions & 0 deletions inc/platform.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#!/bin/sh

# This file is used to set some variables acording to the system we are
# running on. It assumes a POSIX compatible shell.

# Detect the operating system.
case $(uname -s) in
Linux) linux=1 ;;
SunOS) solaris=1 ;;
Darwin) osx=1; bsd=1 ;;
CYGWIN*) cygwin=1; win32=1 ;;
MINGW*) msys=1; win32=1 ;;
MSYS*) msys=1; win32=1 ;;
OpenBSD) openbsd=1; bsd=1 ;;
FreeBSD) freebsd=1; bsd=1 ;;
NetBSD) netbsd=1; bsd=1 ;;
*) bsd=1 ;;
esac

# Find a suitble awk executable.
if [ -n "$AWK" ] && command -v "$AWK" >/dev/null; then
:
elif command -v gawk >/dev/null; then
AWK=gawk
elif command -v nawk >/dev/null; then
AWK=nawk
elif command -v mawk >/dev/null; then
AWK=mawk
elif [ -x /usr/xpg4/bin/awk ]; then
AWK=/usr/xpg4/bin/awk
elif command -v awk >/dev/null; then
AWK=awk
else
echo "ERROR: No awk found!" >&2
exit 1
fi

# Find a suitable sed executable.
if [ -n "$SED" ] && command -v "$SED" >/dev/null; then
:
elif command -v gsed >/dev/null; then
SED=gsed
elif [ -x /usr/xpg4/bin/sed ]; then
SED=/usr/xpg4/bin/sed
elif command -v sed >/dev/null; then
SED=sed
else
echo "ERROR: No sed found!" >&2
exit 1
fi

# Find a suitable head executable.
if command -v ghead >/dev/null; then
HEAD=ghead
else
HEAD=head
fi

# Check the syntax for head.
if [ "$HEAD_SYNTAX" = new -o "$HEAD_SYNTAX" = old ]; then
:
else
if [ "$(echo xx | "$HEAD" -n 1 2>/dev/null)" = xx ]; then
HEAD_SYNTAX=new
else
if ! "$HEAD" -1 -- "$0" >/dev/null 2>&1; then
HEAD_NO_DOUBLE_DASH=1
fi
HEAD_SYNTAX=old
fi
fi

# vim: sw=4 et tw=0:
30 changes: 30 additions & 0 deletions scripts/find_platform
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/bin/sh

. inc/platform.sh

# The backslash has to be put here 4 times: foctor two to get past the initial
# shell quoting, another factor two because it is used in the replacement
# string for sed. This yields one backslash that can escape the single quote
# in the configured vimpager script.
AWK=$(echo "$AWK" | sed "s/'/'\\\\''/g")
SED=$(echo "$SED" | sed "s/'/'\\\\''/g")

# The final sed is used to escape special characters for the sed command in
# the makefile, where the output will be used.
echo \
${bsd:+bsd=$bsd} \
${cygwin:+cygwin=$cygwin} \
${freebsd:+freebsd=$freebsd} \
${linux:+linux=$linux} \
${msys:+msys=$msys} \
${netbsd:+netbsd=$netbsd} \
${openbsd:+openbsd=$openbsd} \
${osx:+osx=$osx} \
${solaris:+solaris=$solaris} \
${win32:+win32=$win32} \
"AWK='$AWK'" \
"SED='$SED'" \
${HEAD_SYNTAX:+HEAD_SYNTAX=$HEAD_SYNTAX} \
| sed -e 's/\\/\\\\/g' -e 's/|/\|/g'

# vim: sw=4 et tw=0:
84 changes: 16 additions & 68 deletions vimpager
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ done
project_dir=`dirname "$link"`
# END OF FIND REAL PARENT DIRECTORY

. "$project_dir/inc/prologue.sh"
. "$project_dir/inc/prologue.sh" # Hopefully we're now POSIX.
. "$project_dir/inc/platform.sh"

[ -n "$ZSH_VERSION" ] && emulate -R sh 2>/dev/null # force zsh into full POSIX

Expand All @@ -36,19 +37,6 @@ runtime='$project_dir'
vimcat='$project_dir/vimcat'
system_vimpagerrc='$project_dir/vimpagerrc'

case "$(uname -s)" in
Linux) linux=1 ;;
SunOS) solaris=1 ;;
Darwin) osx=1; bsd=1 ;;
CYGWIN*) cygwin=1; win32=1 ;;
MINGW*) msys=1; win32=1 ;;
MSYS*) msys=1; win32=1 ;;
OpenBSD) openbsd=1; bsd=1 ;;
FreeBSD) freebsd=1; bsd=1 ;;
NetBSD) netbsd=1; bsd=1 ;;
*) bsd=1 ;;
esac

main() {
# if no args and no stdin, display usage
if [ $# -eq 0 -a -t 0 ]; then
Expand Down Expand Up @@ -219,12 +207,12 @@ main() {

# squeeze blank lines if option was specified, Ubuntu man with /usr/bin/pager does this
if [ "${squeeze_blank_lines:-0}" -eq 1 ]; then
sed -e '/^[ ]*$/{
N
/^[ ]*\n[ ]*$/D
}' < "${tempfile:-$file}" > "$tmp/$filename.work"
tempfile=$tmp/$filename
mv -f -- "$tempfile.work" "$tempfile"
sed -e '/^[ ]*$/{
N
/^[ ]*\n[ ]*$/D
}' < "${tempfile:-$file}" > "$tmp/$filename.work"
tempfile=$tmp/$filename
mv -f -- "$tempfile.work" "$tempfile"
fi

# dumb man detection when the pstree heuristic fails
Expand Down Expand Up @@ -639,38 +627,12 @@ Press ',h' for a summary of keystrokes in the program.
EOF
}

if command -v gawk >/dev/null; then
_awk=gawk
elif command -v nawk >/dev/null; then
_awk=nawk
elif command -v mawk >/dev/null; then
_awk=mawk
elif [ -x /usr/xpg4/bin/awk ]; then
_awk=/usr/xpg4/bin/awk
elif command -v awk >/dev/null; then
_awk=awk
else
echo "ERROR: No awk found!" >&2
quit 1
fi

awk() {
command "$_awk" "$@"
command "$AWK" "$@"
}

if command -v gsed >/dev/null; then
_sed=gsed
elif [ -x /usr/xpg4/bin/sed ]; then
_sed=/usr/xpg4/bin/sed
elif command -v sed >/dev/null; then
_sed=sed
else
echo "ERROR: No sed found!" >&2
quit 1
fi

sed() {
command "$_sed" "$@"
command "$SED" "$@"
}


Expand Down Expand Up @@ -724,20 +686,6 @@ awk_grep_E_q() {
' "$@"
}

if command -v ghead >/dev/null; then
_head=ghead
else
_head=head
fi

if [ "$(echo xx | head -n 1 2>/dev/null)" = "xx" ]; then
_head_syntax=new
else
if ! head -1 -- "$0" >/dev/null 2>&1; then
_head_no_double_dash=1
fi
fi

head() {
_lines=
case "$1" in
Expand All @@ -747,13 +695,13 @@ head() {
esac

if [ -z "$_lines" ]; then
command "$_head" "$@"
elif [ "$_head_syntax" = "new" ]; then
command "$_head" -n $_lines -- "$@"
elif [ -z "$_head_no_double_dash" ]; then
command "$_head" -$_lines -- "$@"
command "$HEAD" "$@"
elif [ "$HEAD_SYNTAX" = "new" ]; then
command "$HEAD" -n $_lines -- "$@"
elif [ -z "$HEAD_NO_DOUBLE_DASH" ]; then
command "$HEAD" -$_lines -- "$@"
else
command "$_head" -$_lines "$@"
command "$HEAD" -$_lines "$@"
fi
}

Expand Down