-
Notifications
You must be signed in to change notification settings - Fork 0
/
patagrep.lib.sh
195 lines (188 loc) · 4.9 KB
/
patagrep.lib.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
patagrep_basedir=.
# _filters want argument like 'tag1=pat1' / 'not tag1=pat1' / 'not any tag1=' ; does not support single argument 'no'|'any'|'empty'
_patagrep() {
#echo >&2 "#debug# _patagrep ($#) [1=$1] $*"
local s=' '
local b="${b:-$s}" ;# word begin
local e="${e:-$s}" ;# word end
local pat="$1";shift
if [ $# -eq 0 ]; then
local invertmatch=false wantany=false wantempty=false
while true; do
case "$pat" in # invert match support
('without '?*) invertmatch=true;;
('with '?*) invertmatch=false;;
('not '?*|'no '?*|'non '?*|'without '?*)
${invertmatch:-false} && invertmatch=false || invertmatch=true
;;
('any '?*)
if ${wantempty:-false}; then
echo >&2 "ERROR: you can not 'any' and 'empty' on a single request"
return 1
fi
wantany=true
;;
('empty '?*)
if ${wantany:-false}; then
echo >&2 "ERROR: you can not 'any' and 'empty' on a single request"
return 1
fi
wantempty=true
;;
('cmd '?*)
local cmd="${pat#cmd }"
local modsdirname="mods"
if [ ! -f "${PATAGREP_DIR:-.}/$modsdirname/$cmd.cmd.sh" ]; then
echo >&2 "$self: invalid command $cmd (${PATAGREP_DIR:-.}/$modsdirname/$cmd.cmd.sh)"
return 1
fi
#echo >&2 "command exists $cmd"
# if [ ! -t 0 ]; then #pipe here
#echo >&2 "# there is data on the pipe for cmd $cmd)"
# else
#echo >&2 "# no piped data before cmd $cmd"
# fi
if [ $# -eq 0 ]; then
#echo >&2 "#debug: run FINAL command (no more arg)"
(
set -- "$cmd"
patagrep_basedir="${patagrep_basedir:-.}/$modsdirname" \
. "${patagrep_basedir:-.}/$modsdirname/$cmd.cmd.sh"
)
return $?
fi
#echo >&2 "#debug: run PIPE command (there are $# more args)"
(
set -- "$cmd"
patagrep_basedir="${patagrep_basedir:-.}/$modsdirname" \
. "${patagrep_basedir:-.}/$modsdirname/$cmd.cmd.sh"
) | "$self" "$@"
return $?
;;
(*) break ;;
esac
pat="${pat#* }"
done
local grepopt; ${invertmatch:-false} && grepopt='-v' || grepopt=''
if ${wantempty:-false}; then
"${patagrep_grep:-grep}" $grepopt -- "$b""${pat%%=*}"'='"$e"
return $?
fi
if ${wantany:-false}; then
# match any value of this tag
"${patagrep_grep:-grep}" $grepopt -- "$b""${pat%%=*}"'=.*'"$e"
return $?
fi
case "$pat" in
# (*=*==*);;
(*'=='*) # match the exact pattern (do not include tag=ANY)
"${patagrep_grep:-grep}" $grepopt -- "$b""${pat%==*}=${pat#*==}""$e"
return $?
;;
esac
# match the requested value (implicite include of tag=ANY)
"${patagrep_grep:-grep}" $grepopt -- "$b"'\('"$pat"'\|'"${pat%=*}"'=ANY\)'"$e"
return $?
fi
b="$b" e="$e" "_$self" "$pat" | b="$b" e="$e" "$self" "$@"
}
# this function is use to merge some argument with their prefix
# from 'not' 'empty' 'tag1=' 'any' 'tag2='
# to 'not empty tag1=' 'any tag2='
patagrep() {
local self="patagrep"
local a1='' mods=''
local defaultaction='auto' ;# PATAGREP_DEFAULT_MODE ?
#echo >&2 "$self: ($#) $*"
# if [ $# -ge 2 ]; then
local last=""
while [ $# -gt 0 ]; do
case "$1" in
(cmd)
# in correct case 'cmd' is the only prefix, the next argument is the command name.
if [ -z "$last" ]; then
last="$1 $2"
shift 2
break
#else
# # this warning should be removed... valid case: with cmd foo
# echo >&2 "WARN: ATTENTION cmd est utilisé mais last n'est pas vide (last=$last)"
fi
;;
(filter)
#if [ "$defaultaction" != "filter" ]; then
defaultaction='filter';shift;continue
#fi
;;
esac
# TODO: faire depend les case du $defaultaction
# if $defaultaction = cmd ...
# filter -> defaultaction=filter
# cmd => 'cmd $1'
# if [ "$defaultaction" = "filter" ]; then
# ...
# a 'not'|'any'|'empty' prefix argument is pasted with the next argument
local word="$1"
case "$word" in
(no|non) word=not ;;
(w/o|with-out) word=without;;
(w/) word=with;;
esac
case "$word" in
(not|any|empty|with|without)
last="${last:+$last }$word"
;;
('cmd '?*)
last="$word";shift;break
;;
(*)
local tmpaction="$defaultaction"
case "$defaultaction" in
(auto)
case "$word" in
(*=*) tmpaction='filter' ;;
(*) tmpaction='cmd' ;;
esac
;;
esac
case "$tmpaction" in
(filter)
last="${last:+$last }$word"
shift
break
;;
(cmd)
if [ -n "$last" ]; then
echo >&2 "no prefix suported in command mode, except 'cmd'"
return 1
fi
defaultaction=auto
last="cmd $word"
shift
break
;;
esac
;;
esac
shift
done
if [ -n "$last" ]; then
a1="$last"
elif [ $# -ge 1 ]; then
a1="$1";shift
else
local ret=$?
if [ ! -t 0 ]; then
cat - || return $?
fi
return $ret
#echo >&2 "SOMETHING WRONG with a1"
#return 0
fi
# else
# a1="$1";shift
# fi
#echo >&2 "_$self: a1=$a1 @=$*"
"_$self" "$a1" "$@"
return $?
}