Add zsh-lookup sub-system
[grml-etc-core.git] / usr_share_grml / zsh / functions / Lookup / LOOKUP_guard
1 ### vim:ft=zsh:foldmethod=marker
2 ## Copyright: 2009, Frank Terbeck <ft@bewatermyfriend.org>
3
4 ### Usage:
5 ##  Check if we're called from the lookup subsystem.
6 #   This way you can guard your functions from being called by hand
7 #   by the user. Every function in lookup should do this as soon as
8 #   possible.
9 #
10 #       LOOKUP_guard || return 1
11 #
12 ##  Check if a function is defined already.
13 #   This is used to avoid nested functions to get defined over and
14 #   over again. Everytime you introduce a function in a backend, you
15 #   should use this idom. No matter if it's a helper function
16 #   (LOOKU_beh_${backend}_my_helper) or the function, that prints
17 #   the backend's documentation (LOOKUP_help_${backend}).
18 #
19 #       LOOKUP_guard -fd LOOKUP_help_${backend} ||
20 #       function LOOKUP_help_${backend}() {
21 #           LOOKUP_guard || return 1
22 #           ...
23 #       }
24
25 local -i fail
26 fail=1
27
28 if [[ $1 == '-fd' ]] ; then
29     # This function cannot use LOOKUP_parseopts() because that function
30     # already uses LOOKUP_guard(). That would be an endless loop.
31     # Since this function does not alter any 'local -x' variables, it
32     # should be the one that doesn't require any other lookup() function.
33     shift
34     (( ${+functions[$1]} )) && return 0
35     return 1
36 fi
37
38 (( lookup_complete > 0 )) && fail=0
39 [[ -n ${backend} ]] && [[ -n ${lookup_ei} ]] && [[ -n ${lookup_help} ]] && fail=0
40
41 if (( fail > 0 )) ; then
42     printf '\nThis function is part of the lookup subsystem.\n'
43     printf 'It is not supposed to be run by you, the user directly.\n'
44     printf 'Use the appropriate backend via: lookup <backend>\n'
45     printf 'See:\n'
46     printf '    lookup -h\n'
47     printf '  and\n'
48     printf '    lookup -h <backend>\n'
49     printf 'for details.\n\n'
50     return 1
51 fi
52 return 0