Imported Upstream version 3.6
[platform/upstream/diffutils.git] / exgettext
1 #! /bin/sh
2 # Wrapper around gettext for programs using the msgid convention.
3
4 # Copyright (C) 1998, 2001, 2004, 2009-2013, 2015-2017 Free Software
5 # Foundation, Inc.
6
7 # Written by Paul Eggert <eggert@twinsun.com>.
8
9 # This program is free software: you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation, either version 3 of the License, or
12 # (at your option) any later version.
13
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 # GNU General Public License for more details.
18
19 # You should have received a copy of the GNU General Public License
20 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
22 # Always operate in the C locale.
23 LANG=C
24 LANGUAGE=C
25 LC_ALL=C
26 export LANG LANGUAGE LC_ALL
27
28 # Set AWK if environment has not already set it.
29 AWK=${AWK-awk}
30
31 # The argument to this wrapper is the xgettext command to be executed.
32 # Extract the xgettext program name from the rest of the command.
33 xgettext=${1?}
34 shift
35
36 # Save work if we're just wrapping a no-op.
37 case $xgettext in
38 :) exit;;
39 esac
40
41 # Find the files to be scanned, and the directory to scan them from.
42 directory=.
43 files=
44 for i
45 do
46   case $i in
47   --directory=*)
48     directory=`expr " $i" : ' --directory=\(.*\)'`;;
49   --files-from=*)
50     files_from=`expr " $i" : ' --files-from=\(.*\)'`
51     files=`$AWK '/^[^#]/ { print }' $files_from`;;
52   esac
53 done
54
55 # Generate keyword options for xgettext,
56 # by scanning for declarations of functions
57 # whose parameter names end in "msgid".
58 generate_keyword_options='
59   /^[A-Z_a-z].*\(.*msgid[,)]/ {
60
61     paren_index = index($0, "(")
62
63     name = substr($0, 1, paren_index - 1)
64     sub(/[^0-9A-Z_a-z]*$/, "", name)
65     sub(/[       ]+PARAMS/, "", name)
66     sub(/[       ]+VPARAMS/, "", name)
67     sub(/.*[^0-9A-Z_a-z]/, "", name)
68
69     args = substr($0, paren_index)
70     sub(/msgid[,)].*/, "", args)
71     for (n = 1; sub(/^[^,]*,/, "", args); n++) {
72       continue;
73     }
74
75     if (n == 1) {
76       keyword = name
77     } else {
78       keyword = name ":" n
79     }
80
81     if (! keyword_seen[keyword]++) {
82       print "--keyword=" keyword
83     }
84   }
85 '
86 keyword_options=`(
87   cd $directory &&
88   $AWK "$generate_keyword_options" $files < /dev/null
89 )` || exit
90
91 # Run the xgettext command, with extra input containing the extra
92 # msgids that it wouldn't normally find.
93 generate_emsgids='
94   /%e.*}/ {
95     line = $0
96     while ((percent_index = index(line, "%e")) != 0) {
97       line = substr(line, percent_index + 2)
98       bracket_index = index(line, "}")
99       if (bracket_index == 0) {
100         continue
101       }
102       msgid = substr(line, 1, bracket_index - 1)
103       if (index(msgid, "%") != 0) {
104         continue
105       }
106       printf "#line %d \"%s\"\n", FNR, FILENAME
107       printf "_(\"%s\")\n", msgid
108       line = substr(line, bracket_index + 1)
109     }
110   }
111 '
112 (cd $directory &&
113  $AWK "$generate_emsgids" $files < /dev/null
114 ) | "$xgettext" $keyword_options ${1+"$@"} -