New URL for NEMO forge!   http://forge.nemo-ocean.eu

Since March 2022 along with NEMO 4.2 release, the code development moved to a self-hosted GitLab.
This present forge is now archived and remained online for history.
bash_compl_makenemo.sh on Ticket #945 – Attachment – NEMO

Ticket #945: bash_compl_makenemo.sh

File bash_compl_makenemo.sh, 6.5 KB (added by rkandilarov, 12 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   if [ "$(echo $1 | grep -c $i )" -eq 0 ];then
39       answ="$answ\n$i"
40   fi
41    done
42    echo  -e "$answ" |tail -n"+2" |sort|sed -e "s/^/$3/" -e "s/\$/$4/"
43    IFS=$lIFS
44}
45
46_makenemo_get_nemo_subdirs(){
47#Version 1   
48    # ls -1 "$(dirname $(which ${COMP_WORDS[0]}))"/../NEMO/ |sed -e "s/^/$1/" -e "s/\$/$2/"
49
50#Version 2
51    # #find the uniq dirs
52    # #get all dirs
53    # ls -1 "$(dirname $(which ${COMP_WORDS[0]}))"/../NEMO/ >/tmp/f.$$.1
54    # #get choosed dirs
55    # echo -e "$(echo "$1"| sed -e 's/"//' -e 's/ /\n/g' )" |sort >/tmp/f.$$.2
56    # #strio the uniq
57    # comm -23 /tmp/f.$$.1 /tmp/f.$$.2 |sed -e "s/^/$1/" -e "s/\$/$2/"
58
59#Version 3
60   
61    local i answ
62    answ=''
63    for i in $(ls -1 "$(dirname $(which ${COMP_WORDS[0]}))"/../NEMO/);do #"
64
65   if [ "$(echo $1 | grep -c $i )" -eq 0 ];then
66       answ="$answ\n$i"
67   fi
68    done
69    echo -e "$answ" |tail -n"+2" |sort|sed -e "s/^/$1/" -e "s/\$/$2/" 
70}
71
72_makenemo_get_institutes(){
73    ls -1p "$( dirname ${COMP_WORDS[0]})"/../ARCH/ |grep "/"|sed -e 's/\/$//'
74}
75_makenemo_add_space(){
76    #We use 'complete -o nospace' because of add_key "bbla bla bla"
77    #So for -v -j -m -r we do the following:
78    #if only one result for completion - add a space to the end                                                                                                       
79    #NOTE this function will delete(put space over it) the current text if $cur is not in $res=$1
80    #in other words this function will delete the written text if it is not in the recodnised list of coplete words/options
81    if [ $(echo "$1"|wc -l) -eq 1 ];then
82   COMPREPLY=( $1\  )
83    else                                                                                                                                                 
84        COMPREPLY=( $1 )                                                                                                                               
85    fi                   
86}
87
88_makenemo_opts_func () 
89{
90
91local prev res saveIFS
92local i res  x_n x_r x_m
93
94cur=${COMP_WORDS[COMP_CWORD]}
95prev=${COMP_WORDS[COMP_CWORD-1]}
96
97
98#First check whether do we need completion or some 'help' option is given
99#Also fill the expected x_n x_r for proper confing name for the complition of key_cpp-s
100for i in n r m;do
101    res=$(echo "$COMP_LINE"|sed -e 's/.* -'"$i"' \+\([^-][^[:space:]]*\).*/\1/g'); #'
102    if [[ $res != $COMP_LINE ]]; then
103   eval "x_$i=\"$res\""
104    else
105   eval "x_$i=''"
106    fi
107done
108
109if [[ $(echo $COMP_LINE|grep ' -h'|wc -l) -eq 1 ]];then
110    #-h  is given
111    if [[ "$cur" == '-h' ]];then
112   COMPREPLY=( '-h ')
113    elif [[ "$prev" == '-h' ]];then
114   COMPREPLY=($(compgen -W "$(_makenemo_get_institutes) all"  -- ${cur} )) ;#"
115    else
116   #Do not coplete if not after -h...
117   COMPREPLY=()
118    fi
119    return 0;
120elif [[ "$x_n" == 'help' ]];then
121    COMPREPLY=()
122    return 0;
123elif [[ "$x_m" == 'help' ]];then
124    COMPREPLY=()
125    return 0;
126fi
127
128# Ok - we really have to complete so - find what
129
130case "$prev" in
131    -n) COMPREPLY=($(compgen -W "$(_makenemo_get_ref_conf) help"  -- ${cur} )) ; #"                                                                         
132   #Do not use _makenemo_add_space(to add space after complition)
133   #allow user to add suffixes and new confs by default
134        return 0;;
135               
136    -m) res=$(compgen -W "$( _makenemo_get_compilers ) help"  -- ${cur} ) ;#"
137   _makenemo_add_space "$res"
138   return 0;;
139
140    -d) saveIFS=$IFS
141        IFS=$'\n'
142        local last_space_pos=$(expr match "$cur" '.* ')
143        local preff=${cur:0:$last_space_pos}
144     
145        ##res=$(compgen -W "$( _makenemo_get_nemo_subdirs "$preff")"  -- ${cur} ); #"
146        ##previous line has problems with spaces so using:
147        res=$(_makenemo_get_nemo_subdirs "${preff:-\"}" ' '|grep "${cur}"|sort -u)
148      COMPREPLY=( $res )
149        IFS=$saveIFS
150   return 0;;
151   
152    -r) res=$(compgen -W "$( _makenemo_get_ref_conf )"  -- ${cur} ) ;#"
153   _makenemo_add_space "$res"
154   return 0;;
155
156    -s) res=$(cd "$(dirname $(which ${COMP_WORDS[0]}))"/../; compgen -d $cur) ;#"
157   COMPREPLY=( $res )
158   return 0;;
159   
160    -j) #Use /proc/cpuinfo only when apropriate, if not - opts= '0 1'
161   res=$( [ -f /proc/cpuinfo ] && seq  0 $(cat /proc/cpuinfo |grep processor|wc -l) || echo '0 1' )
162   res=$(compgen -W "$res" -- ${cur} ) #"
163   _makenemo_add_space "$res"
164        return 0;;                       
165
166    -v)  res=$(compgen -W "$(echo -e '0\n1\n2\n3')"  -- ${cur} ) ;#"
167   _makenemo_add_space "$res"
168        return 0;;
169
170    -t|-e) COMPREPLY=( $(compgen -d $cur ) );
171   return 0;;
172
173       
174    del_key|add_key) saveIFS=$IFS
175        IFS=$'\n'
176        local last_space_pos=$(expr match "$cur" '.* ')
177        local prefics=${cur:0:$last_space_pos}
178     
179      #choose config
180      #This script DOES NOT check whether x_n is really existing
181      #if x_r is given we assume it will be used, if not - we use x_n 
182      #otherwise use the defaut from cfg.txt
183      local config=${x_r:-$x_n}
184      config=${config:-$(_makenemo_get_ref_conf |tail -1)}
185        ##res=$(compgen -W "$( _makenemo_get_nemo_subdirs "$preff")"  -- ${cur} ); #"
186        ##previous line has problems with spaces so using:
187        res=$(_makenemo_get_keys $config "$prev" "${prefics:-\"}" ' '|grep "${cur}"|sort -u)
188      COMPREPLY=( $res )
189        IFS=$saveIFS
190   return 0;;
191
192    *) #Defaults
193    res="-h -n -m -d -r -s -e -j -v -t add_key del_key clean clean_config"
194    res=$(compgen -W "$res" -- ${cur} ) #"
195    _makenemo_add_space "$res" 
196    return 0;;
197esac
198
199}
200complete -o nospace -F _makenemo_opts_func makenemo
201