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