Ticket #945: bash_compl_makenemo_v2.sh

File bash_compl_makenemo_v2.sh, 8.7 KB (added by rkandilarov, 8 years ago)
Line 
1
2_makenemo_get_compilers(){
3    #${COMP_WORDS[0]} -m help|grep -v "Available compilers"|sed -e 's/\([^[:space:]]*\).*/\1/'
4    ls -1R "$(dirname $(which ${COMP_WORDS[0]}))"/../ARCH/|grep ".fcm"|sed -e 's/arch-\(.*\)\.fcm/\1/'
5}
6
7_makenemo_get_ref_conf(){
8    #${COMP_WORDS[0]} -n help|grep -v "Available configurations"|sed -e 's/\([^[:space:]]*\).*/\1/'
9    cat "$(dirname $(which ${COMP_WORDS[0]}))"/cfg.txt |sed -e 's/\([^[:space:]]*\).*/\1/' 
10}
11
12_makenemo_get_keys(){
13    #$1 - config
14    #$2 - 'add' or 'del'
15    #$2 - prefix
16    #$3 - suffix
17   
18    local i answ lIFS cpp_file
19    cpp_file="$(dirname $(which ${COMP_WORDS[0]}))"/$1
20    if [[ "$2" == "del_key" ]];then
21   #del keys
22   cpp_file=$cpp_file/cpp_$1.fcm
23    else
24   #add keys
25   cpp_file=$cpp_file/cpp_all_$1.fcm
26    fi
27
28#Version 1
29#    answ=$(cat "$(dirname $(which ${COMP_WORDS[0]}))"/${config}/cpp_${config}.fcm |\
30#  sed -e 's/^ *bld::tool::fppkeys *//g' -e 's/ *$//g' -e 's/ \+/\n/g');#"
31#    echo  -e "$answ" |sed -e "s/^/$1/" -e "s/\$/$2/"
32
33#Version 2
34    lIFS=$IFS;IFS=' '
35    answ=''
36    for i in $(cat $cpp_file | sed -e 's/^ *bld::tool::fppkeys *//g' );do #"
37   #remove those already written to the command line
38   #add only the new one
39   if [ "$(printf '%s'  $1 | grep -c $i )" -eq 0 ];then
40       answ=$(printf '%s\n%s' "$answ" "$i")
41   fi
42    done
43    printf '%s\n' "$answ" |tail -n"+2" |sort|sed -e "s/^/$3/" -e "s/\$/$4/"
44    IFS=$lIFS
45}
46
47_makenemo_get_nemo_subdirs(){
48#Version 1   
49    # ls -1 "$(dirname $(which ${COMP_WORDS[0]}))"/../NEMO/ |sed -e "s/^/$1/" -e "s/\$/$2/"
50
51#Version 2
52    # #find the uniq dirs
53    # #get all dirs
54    # ls -1 "$(dirname $(which ${COMP_WORDS[0]}))"/../NEMO/ >/tmp/f.$$.1
55    # #get choosed dirs
56    # echo -e "$(echo "$1"| sed -e 's/"//' -e 's/ /\n/g' )" |sort >/tmp/f.$$.2
57    # #strio the uniq
58    # comm -23 /tmp/f.$$.1 /tmp/f.$$.2 |sed -e "s/^/$1/" -e "s/\$/$2/"
59
60#Version 3
61   
62    local i answ
63    answ=''
64    for i in $(ls -1 "$(dirname $(which ${COMP_WORDS[0]}))"/../NEMO/);do #"
65
66   if [ "$(printf '%s'  $1 | grep -c $i )" -eq 0 ];then
67       answ=$(printf '%s\n%s' "$answ" "$i") 
68   fi
69    done
70    printf '%s\n' "$answ" |tail -n"+2" |sort|sed -e "s/^/$1/" -e "s/\$/$2/" 
71}
72
73_makenemo_get_institutes(){
74    #$1 Arch dir
75    ls -1p "$( dirname $(which ${COMP_WORDS[0]}))"/../ARCH/ |grep "/"|sed -e 's/\/$//'
76#    ls -1p $1 |grep "/"|sed -e 's/\/$//'
77
78}
79_makenemo_add_space(){
80    #We use 'complete -o nospace' because of add_key "bbla bla bla"
81    #So for -v -j -m -r we do the following:
82    #if only one result for completion - add a space to the end                                                                                                       
83    #NOTE this function will delete(put space over it) the current text if $cur is not in $res=$1
84    #in other words this function will delete the written text if it is not in the recodnised list of coplete words/options
85    if [ $(printf '%s\n' "$1"|wc -l) -eq 1 ];then
86   COMPREPLY=( $1\  )
87    else                                                                                                                                                 
88        COMPREPLY=( $1 )                                                                                                                               
89    fi                   
90}
91
92_makenemo_opts_func() 
93{
94
95local prev cur res saveIFS
96local i x_n x_r x_m
97
98cur=${COMP_WORDS[COMP_CWORD]}
99prev=${COMP_WORDS[COMP_CWORD-1]}
100
101
102#First check whether do we need completion or some 'help' option is given
103#Also fill the expected x_n x_r for proper confing name for the complition of key_cpp-s
104for i in n r m;do
105    res=$(printf '%s\n' "$COMP_LINE"|sed -e 's/.* -'"$i"' \+\([^-][^[:space:]]*\).*/\1/g'); #'
106    if [[ $res != $COMP_LINE ]]; then
107   eval "x_$i=\"$res\""
108    else
109   eval "x_$i=''"
110    fi
111done
112
113if [[ $(printf '%s\n' $COMP_LINE|grep ' -h'|wc -l) -eq 1 ]];then
114    #-h  is given
115    if [[ "$cur" == '-h' ]];then
116   COMPREPLY=( '-h ')
117    elif [[ "$prev" == '-h' ]];then
118   COMPREPLY=($(compgen -W "$(_makenemo_get_institutes $(dirname ${COMP_WORDS[0]})'/../ARCH/') all"  -- ${cur} )) ;#"
119    else
120   #Do not coplete if not after -h...
121   COMPREPLY=()
122    fi
123    return 0;
124elif [[ "$x_n" == 'help' ]];then
125    COMPREPLY=()
126    return 0;
127elif [[ "$x_m" == 'help' ]];then
128    COMPREPLY=()
129    return 0;
130fi
131
132# Ok - we really have to complete so - find what
133
134case "$prev" in
135    -n) COMPREPLY=($(compgen -W "$(_makenemo_get_ref_conf) help"  -- ${cur} )) ; #"                                                                         
136   #Do not use _makenemo_add_space(to add space after complition)
137   #allow user to add suffixes and new confs by default
138        return 0;;
139               
140    -m) res=$(compgen -W "$( _makenemo_get_compilers ) help"  -- ${cur} ) ;#"
141   _makenemo_add_space "$res"
142   return 0;;
143
144    -d) saveIFS=$IFS
145        IFS=$'\n'
146        local last_space_pos=$(expr match "$cur" '.* ')
147        local preff=${cur:0:$last_space_pos}
148     
149        ##res=$(compgen -W "$( _makenemo_get_nemo_subdirs "$preff")"  -- ${cur} ); #"
150        ##previous line has problems with spaces so using:
151        res=$(_makenemo_get_nemo_subdirs "${preff:-\"}" ' '|grep "${cur}"|sort -u)
152      COMPREPLY=( $res )
153        IFS=$saveIFS
154   return 0;;
155   
156    -r) res=$(compgen -W "$( _makenemo_get_ref_conf )"  -- ${cur} ) ;#"
157   _makenemo_add_space "$res"
158   return 0;;
159
160    -s) res=$(cd "$(dirname $(which ${COMP_WORDS[0]}))"/../; compgen -d $cur) ;#"
161   COMPREPLY=( $res )
162   return 0;;
163   
164    -j) #Use /proc/cpuinfo only when apropriate, if not - opts= '0 1'
165   res=$( [ -f /proc/cpuinfo ] && seq  0 $(cat /proc/cpuinfo |grep processor|wc -l) || echo '0 1' )
166   res=$(compgen -W "$res" -- ${cur} ) #"
167   _makenemo_add_space "$res"
168        return 0;;                       
169
170    -v)  res=$(compgen -W "$(printf '%d\n%d\n%d\n%d\n' 1 2 3 4)"  -- ${cur} ) ;#"
171   _makenemo_add_space "$res"
172        return 0;;
173
174    -t|-e) COMPREPLY=( $(compgen -d $cur ) );
175   return 0;;
176
177       
178    add_key1) COMPREPLY=( '"Please write this function as it does not exist !' '" '  )                                                                                                     
179        return 0;;
180
181       
182    del_key|add_key) saveIFS=$IFS
183        IFS=$'\n'
184        local last_space_pos=$(expr match "$cur" '.* ')
185        local prefics=${cur:0:$last_space_pos}
186     
187      #choose config
188      #This script DOES NOT check whether x_n is really existing
189      #if x_r is given we assume it will be used, if not - we use x_n 
190      local config=${x_r:-$x_n}
191      config=${config:-$(_makenemo_get_ref_conf |tail -1)}
192        ##res=$(compgen -W "$( _makenemo_get_nemo_subdirs "$preff")"  -- ${cur} ); #"
193        ##previous line has problems with spaces so using:
194        res=$(_makenemo_get_keys $config "$prev" "${prefics:-\"}" ' '|grep "${cur}"|sort -u)
195      COMPREPLY=( $res )
196        IFS=$saveIFS
197   return 0;;
198
199    *) #Defaults
200    res="-h -n -m -d -r -s -e -j -v -t add_key del_key clean clean_config"
201    res=$(compgen -W "$res" -- ${cur} ) #"
202    _makenemo_add_space "$res" 
203    return 0;;
204esac
205
206}
207
208_maketools_opts_func() 
209{
210
211local prev cur res
212local i x_n x_r x_m
213
214cur=${COMP_WORDS[COMP_CWORD]}
215prev=${COMP_WORDS[COMP_CWORD-1]}
216
217
218#First check whether do we need completion or some 'help' option is given
219#Also fill the expected x_n x_r for proper confing name for the complition of key_cpp-s
220for i in n m;do
221    res=$(printf '%s\n' "$COMP_LINE"|sed -e 's/.* -'"$i"' \+\([^-][^[:space:]]*\).*/\1/g'); #'
222    if [[ $res != $COMP_LINE ]]; then
223   eval "x_$i=\"$res\""
224    else
225   eval "x_$i=''"
226    fi
227done
228
229if [[ $(printf '%s\n' $COMP_LINE|grep ' -h'|wc -l) -eq 1 ]];then
230    #-h  is given
231    if [[ "$cur" == '-h' ]];then
232   COMPREPLY=( '-h ')
233    elif [[ "$prev" == '-h' ]];then
234   COMPREPLY=($(compgen -W "$(_makenemo_get_institutes) all"  -- ${cur} )) ;#"
235    else
236   #Do not coplete if not after -h...
237   COMPREPLY=()
238    fi
239    return 0;
240elif [[ "$x_n" == 'help' ]];then
241    COMPREPLY=()
242    return 0;
243elif [[ "$x_m" == 'help' ]];then
244    COMPREPLY=()
245    return 0;
246fi
247
248# Ok - we really have to complete so - find what
249
250case "$prev" in
251    -n) COMPREPLY=($(compgen -W "$(ls -1p $( dirname $(which ${COMP_WORDS[0]}))/ |grep '/'|sed -e 's/\/$//') help"  -- ${cur} )) ; #"
252    return 0;;           
253    -m) res=$(compgen -W "$( _makenemo_get_compilers ) help"  -- ${cur} ) ;#"
254   _makenemo_add_space "$res"
255    return 0;;
256    -j) #Use /proc/cpuinfo only when apropriate, if not - opts= '0 1'
257   res=$( [ -f /proc/cpuinfo ] && seq  0 $(cat /proc/cpuinfo |grep processor|wc -l) || echo '0 1' )
258   res=$(compgen -W "$res" -- ${cur} ) #"
259   _makenemo_add_space "$res"
260
261    return 0;;
262    -t) COMPREPLY=( $(compgen -d $cur ) );
263    return 0;;
264    *) #Defaults
265   res="-h -n -m -j -t"
266   res=$(compgen -W "$res" -- ${cur} ) #"
267   _makenemo_add_space "$res"
268    return 0;;
269esac
270}
271
272complete -o nospace -F _makenemo_opts_func makenemo
273complete -o nospace -F _maketools_opts_func maketools
274
275