support precommands and fix a few bugs

This commit is contained in:
romkatv 2020-01-11 18:07:00 +01:00
parent 9a6eb616d9
commit 347392daab

View file

@ -34,20 +34,6 @@ typeset -gA _p9k_skip_token=(
';;' '\)|esac' ';;' '\)|esac'
';&' '\)|esac' ';&' '\)|esac'
';|' '\)|esac' ';|' '\)|esac'
'&>' '*'
'>' '*'
'>&' '*'
'<' '*'
'<&' '*'
'<>' '*'
'&>|' '*'
'>|' '*'
'&>>' '*'
'>>' '*'
'>>&' '*'
'&>>|' '*'
'>>|' '*'
'<<<' '*'
'foreach' '\(*\)' 'foreach' '\(*\)'
) )
@ -111,52 +97,6 @@ typeset -gA _p9k_skip_arg=(
'()' '' '()' ''
) )
function _p9k_next_token() {
if (( $#tokens )); then
if (( $#tokens == aln[-1] )); then
aln[-1]=()
alp[-1]=()
if (( $#tokens == alf[-1] )); then
alf[-1]=()
(( e = 0 ))
else
(( e = 1 ))
fi
else
(( e = 1 ))
fi
while (( c-- > 0 )); do
token=$tokens[1]
tokens[1]=()
if (( $+galiases[$token] )); then
(( $aln[(eI)p$token] )) && return
n=p$token
s=$galiases[$token]
elif (( e )); then
return
elif (( $+aliases[$token] )); then
(( $aln[(eI)p$token] )) && return
n=p$token
s=$aliases[$token]
elif [[ $token == (#b)?*.(?*) ]] && (( $+saliases[$match[1]] )); then
(( $aln[(eI)s$match[1]] )) && return
n=s$match[1]
s=${saliases[$match[1]]%% #}
else
return 0
fi
aln+=$n
alp+=$#tokens
[[ $s == *' ' ]] && alf+=$#tokens
[[ -o interactive_comments ]] && tokens[1,0]=(${(Z+C+)s}) || tokens[1,0]=(${(z)s})
done
fi
token=
return 1
}
# False positives: # False positives:
# #
# {} always {} # {} always {}
@ -196,11 +136,11 @@ function _p9k_extract_commands() {
typeset -ga _p9k_commands=() typeset -ga _p9k_commands=()
local -r id='$(<->|[[:alpha:]_][[:IDENT:]]#)' local -r id='(<->|[[:alpha:]_][[:IDENT:]]#)'
local -r var="\$$id|\${$id}|\"\$$id\"|\"\${$id}\"" local -r var="\$$id|\${$id}|\"\$$id\"|\"\${$id}\""
local -i e c=32 local -i e c=1024
local skip n s r local skip n s r state
local -a aln alp alf v commands match mbegin mend local -a aln alp alf v commands match mbegin mend
[[ -o interactive_comments ]] && local tokens=(${(Z+C+)1}) || local tokens=(${(z)1}) [[ -o interactive_comments ]] && local tokens=(${(Z+C+)1}) || local tokens=(${(z)1})
@ -214,10 +154,10 @@ function _p9k_extract_commands() {
alf[-1]=() alf[-1]=()
(( e = 0 )) (( e = 0 ))
else else
(( e = $#skip )) (( e = $#state ))
fi fi
else else
(( e = $#skip )) (( e = $#state ))
fi fi
while (( c-- > 0 )) || return; do while (( c-- > 0 )) || return; do
@ -246,61 +186,88 @@ function _p9k_extract_commands() {
[[ -o interactive_comments ]] && tokens[1,0]=(${(Z+C+)s}) || tokens[1,0]=(${(z)s}) [[ -o interactive_comments ]] && tokens[1,0]=(${(Z+C+)s}) || tokens[1,0]=(${(z)s})
done done
if [[ $token == '<<'(|-) ]]; then case $state in
_p9k_next_token || return t)
r=$token
while true; do
while _p9k_next_token && [[ $token != ';' ]]; do done
while _p9k_next_token && [[ $token == ';' ]]; do done
[[ $token == (|$r) ]] && break
done
continue
fi
if [[ -n $skip ]]; then
if [[ $skip == ']' ]]; then
if (( $+_p9k_term[$token] )); then if (( $+_p9k_term[$token] )); then
skip=$_p9k_skip_arg[$token] skip=$_p9k_skip_arg[$token]
state=${skip:+s}
[[ $token == '()' ]] || _p9k_commands+=($commands) [[ $token == '()' ]] || _p9k_commands+=($commands)
commands=() commands=()
fi fi
elif [[ $token == $~skip ]]; then continue;;
skip= s)
if [[ $token == $~skip ]]; then
state=
fi fi
continue;;
*r)
state[1]=
continue;;
h)
skip=${(b)token}
state=s
continue;;
esac
if [[ $token == '<<'(|-) ]]; then
state=h
continue continue
fi fi
r=${token#<0-255>} if (( $+_p9k_redirect[${token#<0-255>}] )); then
if (( $+_p9k_skip_token[$r] )); then state+=r
if (( $+_p9k_skip_token[$token] )); then
skip=$_p9k_skip_token[$token]
continue continue
fi fi
if (( $+_p9k_redirect[$r] )); then
skip='*'
continue
fi
fi
if [[ $token == *=* ]]; then
v=${(S)token/#(<->|([[:alpha:]_][[:IDENT:]]#(|'['*[^\\](\\\\)#']')))(|'+')=}
if (( $#v < $#token )); then
[[ $v == '(' ]] && skip='\)'
continue
fi
fi
if [[ $token == *'$'* ]]; then if [[ $token == *'$'* ]]; then
if [[ $token == $~id ]]; then setopt xtrace
if [[ $token == $~var ]]; then
n=${${token##[^[:IDENT:]]}%%[^[:IDENT:]]} n=${${token##[^[:IDENT:]]}%%[^[:IDENT:]]}
[[ $token == *'"' ]] && v=("${(@P)n}") || v=(${(P)name}) [[ $token == *'"' ]] && v=("${(@P)n}") || v=(${(P)n})
tokens[1,0]=(${(qq)v}) tokens[1,0]=(${(qq)v})
continue continue
fi fi
fi fi
commands+=${:-${(Q)${~token}}} case $state in
skip=']' '')
if (( $+_p9k_skip_token[$token] )); then
skip=$_p9k_skip_token[$token]
state=${skip:+s}
continue
fi
if [[ $token == *=* ]]; then
v=${(S)token/#(<->|([[:alpha:]_][[:IDENT:]]#(|'['*[^\\](\\\\)#']')))(|'+')=}
if (( $#v < $#token )); then
if [[ $v == '(' ]]; then
state=s
skip='\)'
fi
continue
fi
fi
: ${token::=${(Q)${~token}}};;
p)
: ${token::=${(Q)${~token}}}
case $token in
[^-]*) ;;
--) state=p1; continue;;
$~skip) state=p2; continue;;
*) continue;;
esac;;
p1) ;;
p2)
state=p
continue;;
esac
commands+=$token
if (( $+_p9k_precomands[$commands[-1]] )); then
state=p
skip=$_p9k_precomands[$commands[-1]]
else
state=t
fi
done done
} }