Imported Upstream version 0.18.3.2
[platform/upstream/gettext.git] / gettext-runtime / src / gettext.sh.in
1 #! /bin/sh
2 #
3 # Copyright (C) 2003, 2005-2007, 2011 Free Software Foundation, Inc.
4 #
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU Lesser General Public License as published by
7 # the Free Software Foundation; either version 2.1 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU Lesser General Public License for more details.
14 #
15 # You should have received a copy of the GNU Lesser General Public License
16 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 #
18
19 # Find a way to echo strings without interpreting backslash.
20 if test "X`(echo '\t') 2>/dev/null`" = 'X\t'; then
21   echo='echo'
22 else
23   if test "X`(printf '%s\n' '\t') 2>/dev/null`" = 'X\t'; then
24     echo='printf %s\n'
25   else
26     echo_func () {
27       cat <<EOT
28 $*
29 EOT
30     }
31     echo='echo_func'
32   fi
33 fi
34
35 # This script is primarily a shell function library. In order for
36 # ". gettext.sh" to find it, we install it in $PREFIX/bin (that is usually
37 # contained in $PATH), rather than in some other location such as
38 # $PREFIX/share/sh-scripts or $PREFIX/share/gettext. In order to not violate
39 # the Filesystem Hierarchy Standard when doing so, this script is executable.
40 # Therefore it needs to support the standard --help and --version.
41 if test -z "${ZSH_VERSION+set}"; then
42   # zsh is not POSIX compliant: By default, while ". gettext.sh" is executed,
43   # it sets $0 to "gettext.sh", defeating the purpose of this test. But
44   # fortunately we know that when running under zsh, this script is always
45   # being sourced, not executed, because hardly anyone is crazy enough to
46   # install zsh as /bin/sh.
47   case "$0" in
48     gettext.sh | */gettext.sh | *\\gettext.sh)
49       progname=$0
50       package=@PACKAGE@
51       version=@VERSION@
52       # func_usage
53       # outputs to stdout the --help usage message.
54       func_usage ()
55       {
56         echo "GNU gettext shell script function library version $version"
57         echo "Usage: . gettext.sh"
58       }
59       # func_version
60       # outputs to stdout the --version message.
61       func_version ()
62       {
63         echo "$progname (GNU $package) $version"
64         echo "Copyright (C) 2003-2007 Free Software Foundation, Inc.
65 License GPLv2+: GNU GPL version 2 or later <http://gnu.org/licenses/gpl.html>
66 This is free software: you are free to change and redistribute it.
67 There is NO WARRANTY, to the extent permitted by law."
68         echo "Written by" "Bruno Haible"
69       }
70       if test $# = 1; then
71         case "$1" in
72           --help | --hel | --he | --h )
73             func_usage; exit 0 ;;
74           --version | --versio | --versi | --vers | --ver | --ve | --v )
75             func_version; exit 0 ;;
76         esac
77       fi
78       func_usage 1>&2
79       exit 1
80       ;;
81   esac
82 fi
83
84 # eval_gettext MSGID
85 # looks up the translation of MSGID and substitutes shell variables in the
86 # result.
87 eval_gettext () {
88   gettext "$1" | (export PATH `envsubst --variables "$1"`; envsubst "$1")
89 }
90
91 # eval_ngettext MSGID MSGID-PLURAL COUNT
92 # looks up the translation of MSGID / MSGID-PLURAL for COUNT and substitutes
93 # shell variables in the result.
94 eval_ngettext () {
95   ngettext "$1" "$2" "$3" | (export PATH `envsubst --variables "$1 $2"`; envsubst "$1 $2")
96 }
97
98 # Note: This use of envsubst is much safer than using the shell built-in 'eval'
99 # would be.
100 # 1) The security problem with Chinese translations that happen to use a
101 #    character such as \xe0\x60 is avoided.
102 # 2) The security problem with malevolent translators who put in command lists
103 #    like "$(...)" or "`...`" is avoided.
104 # 3) The translations can only refer to shell variables that are already
105 #    mentioned in MSGID or MSGID-PLURAL.
106 #
107 # Note: "export PATH" above is a dummy; this is for the case when
108 # `envsubst --variables ...` returns nothing.
109 #
110 # Note: In eval_ngettext above, "$1 $2" means a string whose variables set is
111 # the union of the variables set of "$1" and "$2".
112 #
113 # Note: The minimal use of backquote above ensures that trailing newlines are
114 # not dropped, not from the gettext invocation and not from the value of any
115 # shell variable.
116 #
117 # Note: Field splitting on the `envsubst --variables ...` result is desired,
118 # since envsubst outputs the variables, separated by newlines. Pathname
119 # wildcard expansion or tilde expansion has no effect here, since the words
120 # output by "envsubst --variables ..." consist solely of alphanumeric
121 # characters and underscore.