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