]> git.mxchange.org Git - flightgear.git/blobdiff - scripts/completion/fg-completion.bash
Fixed some memory leaks. Thanks to Andreas Gaeb for reporting.
[flightgear.git] / scripts / completion / fg-completion.bash
index 9e796d724a5241e1b817c84b3aba4d31733d17af..4f34e2dd1fed252939f928832e016b236fb2ddee 100755 (executable)
@@ -1,24 +1,23 @@
 #!/bin/bash
-# Tab completion for FlightGear command line options.
+# Tab completion for FlightGear command line options. To use it put
+# this in your ~/.bashrc file:
 #
-# 1. define $FG_HOME (or accept the default "$HOME/.fgfs/")
+#   export FG_HOME=$HOME/.fgfs/
+#   [ -e $FG_HOME/fg-completion.bash ] && source $FG_HOME/fg-completion.bash
 #
-# 2. run this script to build the $FG_HOME/aircraft.list file and repeat this
-#    step whenever you added an aircraft (or maintain the aircraft list manually):
+# Defining FG_HOME is only required if you don't like the default
+# "$HOME/.fgfs/". The script doesn't offer all available airports
+# for the --airport option, but only those listed in a file
+# $FG_HOME/airport.list if available, or a short default list otherwise.
 #
-#      $ $FG_HOME/fg-completion.bash
-#
-#
-# 3. source this file from your ~/.bashrc:
-#
-#      [ -e $FG_HOME/fg-completion.bash ] && source $FG_HOME/fg-completion.bash
-#
-__fgfs_scriptname="fg-completion.bash"  # adapt this if you renamed this script!
+# After installing new aircraft you have to rebuild the aircraft list
+# by typing   $ fgfs --aircraft=?<TAB>
 
 __fgfs_root=${FG_ROOT:-/usr/local/share/FlightGear}
 __fgfs_home=${FG_HOME:-$HOME/.fgfs}
 [ -d "$__fgfs_home" ] || mkdir -p "$__fgfs_home"
 
+
 __fgfs_ac_list="$__fgfs_home/aircraft.list"
 __fgfs_apt_list="$__fgfs_home/airport.list"
 
@@ -189,26 +188,16 @@ fi
 shopt -s progcomp
 
 
-__fgfs_make_ac_list() {
-       ls $__fgfs_root/Aircraft/*/*-set.xml|
-               while read i; do
-                       i=${i##*/}
-                       echo "${i%-set.xml} "
-               done >"$__fgfs_ac_list"
+__fgfs_write_ac_list() {
+       rm -f $__fgfs_ac_list
+       for i in $__fgfs_root/Aircraft/*/*-set.xml; do
+               i=${i%-set.xml}
+               echo ${i##*/} >>$__fgfs_ac_list
+       done
 }
 
 
-if [ "${0##*/}" == "$__fgfs_scriptname" ]; then  # run explicitly -- not sourced
-       echo "creating list of available aircraft: $__fgfs_ac_list"
-       __fgfs_make_ac_list
-       exit 0
-fi
-
-
-if [ ! -e "$__fgfs_ac_list" ]; then
-       echo "$0: creating list of available aircraft: $__fgfs_ac_list"
-       __fgfs_make_ac_list
-fi
+[ -e $__fgfs_ac_list ] || __fgfs_write_ac_list
 
 
 __fgfs_ai_scenario() {
@@ -220,6 +209,13 @@ __fgfs_ai_scenario() {
 }
 
 
+__fgfs_aircraft() {
+       while read i; do
+               echo $i
+       done <$__fgfs_ac_list
+}
+
+
 __fgfs_offer() {
        local i
        for i in "$@"; do
@@ -240,8 +236,11 @@ __fgfs() {
        --ai-scenario=*)
                alt=$(__fgfs_offer $(__fgfs_ai_scenario))
                ;;
+       --aircraft=\?)
+               __fgfs_write_ac_list
+               ;;
        --aircraft=*|--vehicle=*)
-               alt=$(cat "$__fgfs_ac_list")
+               alt=$(__fgfs_offer $(__fgfs_aircraft))
                ;;
        --airport=*)
                if [ -e "$__fgfs_apt_list" ]; then