Created
August 26, 2011 17:02
-
-
Save eatnumber1/1173863 to your computer and use it in GitHub Desktop.
cmdsh: A pile of hacks.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/zsh | |
emulate -L zsh | |
setopt err_exit | |
#setopt xtrace | |
typeset -r NONLINK_PROGNAME="cmdsh" | |
typeset -r PROGNAME="${0##*/}" | |
typeset -r CONIN="/usr/local/bin/conin" | |
typeset -r WINPWD="$(cygpath -w "$PWD")" | |
typeset -a winargs arguments | |
typeset -a inetroot corextbranch | |
corextbranch=( -c "nonship" ) | |
inetroot=( -i "$HOME/Sources/appv" ) | |
if [[ "$PROGNAME" == "$NONLINK_PROGNAME" ]]; then | |
zmodload -F zsh/zutil +b:zparseopts | |
zparseopts -D -K -E i:=inetroot -inetroot:=inetroot c:=corextbranch -corextbranch:=corextbranch | |
fi | |
inetroot="$(cygpath -w "${inetroot[-1]}")" | |
corextbranch="${corextbranch[-1]}" | |
winargs=( | |
"set inetroot=$inetroot" | |
"set corextbranch=$corextbranch" | |
"$inetroot"'\tools\path1st\myenv.cmd' | |
'set SDUDIFF=' | |
'set SDDIFF=' | |
"cd $WINPWD" | |
) | |
typeset escaped_args="${${${*//</<^}//>/^>}//|/^|}" | |
if [[ "$PROGNAME" != "$NONLINK_PROGNAME" ]]; then | |
winargs+=( "$PROGNAME $escaped_args" ) | |
arguments+=( "/C" ) | |
elif [[ $# -ge 1 ]]; then | |
winargs+=( "$escaped_args" ) | |
arguments+=( "/C" ) | |
else | |
typeset interactive | |
arguments+=( "/K" ) | |
fi | |
typeset -r LAST_HIDDEN_REGEX="^OACR:.*" | |
if (( ${+interactive} )); then | |
typeset -ri NEWLINES_AFTER_LAST_HIDDEN=2 | |
else | |
typeset -ri NEWLINES_AFTER_LAST_HIDDEN=1 | |
fi | |
typeset -a cmd | |
if [[ -t 0 && -t 1 && -t 2 ]]; then | |
cmd+=( "$CONIN" "$COMSPEC" ) | |
else | |
cmd+=( "$(cygpath -u "$COMSPEC")" ) | |
fi | |
cmd+=( "${arguments[@]}" "${(j:&:)winargs[*]}" ) | |
{ | |
zmodload -F zsh/system +p:sysparams | |
printf "%d\n" "${sysparams[pid]}" | |
exec "${cmd[@]}" | |
} | { | |
read -r pipepid | |
function TRAPINT() { | |
kill -INT "$pipepid" | |
} | |
function TRAPTERM() { kill -TERM "$pipepid" } | |
function TRAPQUIT() { kill -QUIT "$pipepid" } | |
function TRAPABRT() { kill -ABRT "$pipepid" } | |
function TRAPUSR1() { kill -USR1 "$pipepid" } | |
function TRAPUSR2() { kill -USR2 "$pipepid" } | |
zmodload -F zsh/system +b:sysread +b:syserror | |
function sysread() { | |
typeset -i rc=0 | |
builtin sysread "$@" || rc=$? | |
case $rc in | |
0) | |
return 0 | |
;; | |
1) | |
exit $rc | |
;; | |
2|3) | |
syserror -p "sysread: " "$ERRNO" >&2 | |
exit $rc | |
;; | |
4) | |
printf "sysread: Timed out waiting for data\n" >&2 | |
exit $rc | |
;; | |
5) | |
return 1 | |
;; | |
*) | |
printf "sysread: Unknown error code (%d)\n" $rc >&2 | |
exit $rc | |
esac | |
} | |
typeset -i newlines_after_last=0 | |
while true; do | |
if [[ $newlines_after_last -eq 0 ]]; then | |
read -r | |
[[ "$REPLY" =~ $LAST_HIDDEN_REGEX ]] && let newlines_after_last+=1 | |
elif [[ $newlines_after_last -ne $NEWLINES_AFTER_LAST_HIDDEN ]]; then | |
sysread -s 1 || exit | |
[[ "$REPLY" == $'\n' ]] && let newlines_after_last+=1 | |
else | |
break | |
fi | |
done | |
while true; do | |
sysread -o 1 || exit | |
done | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment