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