]> git.decadent.org.uk Git - ion3.git/blob - utils/ion-completeman.in
[svn-upgrade] Integrating new upstream version, ion3 (20070203)
[ion3.git] / utils / ion-completeman.in
1 #!/bin/sh
2
3 tocompl=""
4 section=""
5 beg=""
6 action=""
7 usercache=""
8 syscache=""
9
10 translate_grepsafe() { 
11     # The regexp below is supposed to be [\[\].*$^\\], but sed sucks
12     # and doesn't support simple and intuitive escaping and we have to
13     # do it the hard way with the collations [.[.] and [.].] standing 
14     # for [ and ], respectively.
15     sed 's:^[ \t]*\(.*\)[ \t]*$:\1:; s:[[.[.][.].].*$^\\]:\\&:g'
16 }
17
18 case "$1" in
19     -complete)
20         read section tocompl << EOF
21 $2
22 EOF
23         if test "$tocompl" = ""; then
24             tocompl="$section"
25             section=""
26         else
27             beg="$section "
28         fi
29         tocompl=`echo "$tocompl" | translate_grepsafe`
30         action="complete"
31         ;;
32     -mkusercache)
33         action="mkusercache"
34         ;;
35     -mksyscache)
36         action="mksyscache"
37         ;;
38 esac
39    
40 if test "x$action" = x; then
41     echo 2>&1 "Usage: $0 (-complete what|-mkusercache|-mksyscache)"
42     exit 1
43 fi
44         
45
46 filterpath() {
47     sed 's:^.*/\([^/]*\.[0-9].*\)$:\1:p; d'
48 }
49
50 filtersect() {
51     sed 's:^\(.*\)\.[0-9].*$:\1:p; d'
52 }
53
54 grepper() {
55     if test "$tocompl" = "" -a "$section" = ""; then
56         cat
57     else
58         if test "$section" = ""; then
59             section="[0-9]"
60         fi
61         grep "^$tocompl.*\.$section"
62     fi
63 }
64
65 scan() {
66     if test "x$ION_MANPATH" != "x"; then
67         mpath="$ION_MANPATH"
68     elif test "x$MANPATH" != "x"; then
69         mpath="$MANPATH"
70     else
71         mpprog=`which manpath`
72         if test "x$mpprog" = x; then
73             echo "Please set MANPATH, ION_MANPATH or put 'manpath' on PATH" > /dev/stderr
74             exit 1
75         fi
76         mpath=`$mpprog`
77     fi
78     
79     for p in `echo "$mpath"|tr : '\n'`; do
80         find  "$p" -type f -o -type l | filterpath
81     done 
82 }
83
84
85 cachefile=""
86
87 if test "x$HOME" != x; then
88     usercache="$HOME/.ion3/mancache"
89 fi
90
91 syscache="@VARDIR@/mancache"
92
93 case "$action" in
94     complete)
95         if test "x$usercache" != x -a -f "$usercache"; then
96             cachefile="$usercache"
97         fi
98         
99         if test -f "$syscache"; then
100             cachefile="$syscache"
101         fi
102         
103         # Empty "common part" of completions.
104         echo "$beg"
105         
106         if test "x$cachefile" != x; then
107             grepper < "$cachefile" | filtersect
108         else
109             scan | grepper | filtersect
110         fi
111         ;;
112     mkusercache)
113         if test "x$usercache" != x; then
114             scan > "$usercache"
115         else
116             echo >&2 "\$HOME not set."
117         fi
118         ;;
119     mksyscache)
120         mkdir -p "@VARDIR@"
121         scan > "$syscache"
122         ;;
123 esac