OSDN Git Service

2011-05-04 Richard Guenther <rguenther@suse.de>
[pf3gnuchains/gcc-fork.git] / gcc / po / exgettext
1 #! /bin/sh
2 # Wrapper around gettext for programs using the msgid convention.
3 # Copyright 1998, 2001, 2002, 2003, 2009, 2010 Free Software Foundation, Inc.
4
5 # Written by Paul Eggert <eggert@twinsun.com>.
6 # Revised by Zack Weinberg <zackw@stanford.edu> for no-POTFILES operation.
7
8 # This file is part of GCC.
9
10 # GCC is free software; you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 3, or (at your option)
13 # any later version.
14
15 # GCC is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 # GNU General Public License for more details.
19
20 # You should have received a copy of the GNU General Public License
21 # along with GCC; see the file COPYING3.  If not see
22 # <http://www.gnu.org/licenses/>.
23
24 # Always operate in the C locale.
25 LANG=C
26 LANGUAGE=C
27 LC_ALL=C
28 export LANG LANGUAGE LC_ALL
29
30 # Set AWK if environment has not already set it.
31 AWK=${AWK-awk}
32
33 # The arguments to this wrapper are: the program to execute, the
34 # name of the "package", and the path to the source directory.
35
36 if [ $# -ne 3 ]
37 then    echo "usage: $0 <xgettext> <package> <srcdir>"
38         exit 1
39 fi
40
41 xgettext=$1
42 package=$2
43 srcdir=$3
44
45 case `$xgettext --version | sed -e 1q | sed -e 's/^\([^0-9]*\)//'` in
46   0.14.[5-9]* | 0.14.[1-9][0-9]* | 0.1[5-9]* | 0.[2-9][0-9]* | [1-9].*) : ;;
47   *) echo "$xgettext is too old.  GNU xgettext 0.14.5 is required"
48      exit 1 ;;
49 esac
50
51 nl='
52 '
53
54 set -e
55
56 # Create temporary directory for scratch files.
57 T=exg$$.d
58 mkdir $T
59 trap "rm -r $T" 0
60
61 pwd=`${PWDCMD-pwd}`
62 kopt=$pwd/$T/keyword-options
63 kopt2=$pwd/$T/keyword2-options
64 emsg=$pwd/$T/emsgids.c
65 posr=$pwd/$T/po-sources
66 posrcxx=$pwd/$T/po-cxx-sources
67 pottmp1=$pwd/$T/tmp1.pot
68 pottmp2=$pwd/$T/tmp2.pot
69 pottmp3=$pwd/$T/tmp3.pot
70 pottmp=$pwd/$T/tmp.pot
71
72 # Locate files to scan.  We scan the following directories:
73 #  $srcdir
74 #  $srcdir/c-family
75 #  $srcdir/config
76 #  $srcdir/config/*
77 #  all subdirectories of $srcdir containing a config-lang.in file, and
78 #    all subdirectories of those directories.
79 # Within those directories, we examine all .c, .cc, .h, and .def files.
80 # However, any files listed in $srcdir/po/EXCLUDE are not examined.
81 #
82 # Then generate keyword options for xgettext, by scanning for declarations
83 # of functions whose parameter names end in "msgid".
84 #
85 # Finally, generate a source file containing all %e and %n strings from
86 # driver specs, so those can be translated too.
87 #
88 # All in one huge awk script.
89
90 echo "scanning for keywords, %e and %n strings..." >&2
91
92 ( cd $srcdir
93   lang_subdirs=`echo */config-lang.in */*/config-lang.in | sed -e 's|/config-lang\.in||g'`
94   { for dir in "" c-family/ config/ config/*/ \
95       `find $lang_subdirs -type d -print | fgrep -v .svn | sort | sed -e 's|$|/|'`
96     do  for glob in '*.c' '*.cc' '*.h' '*.def'
97         do  eval echo $dir$glob
98         done
99     done;
100   } | tr ' ' "$nl" | grep -v '\*' |
101   $AWK -v excl=po/EXCLUDES -v posr=$posr -v posrcxx=$posrcxx -v kopt=$kopt -v kopt2=$kopt2 -v emsg=$emsg '
102 function keyword_option(line) {
103     paren_index = index(line, "(")
104     name = substr(line, 1, paren_index - 1)
105     sub(/[^0-9A-Z_a-z]*$/, "", name)
106     sub(/[       ]+PARAMS/, "", name)
107     sub(/[       ]+VPARAMS/, "", name)
108     sub(/.*[^0-9A-Z_a-z]/, "", name)
109
110     args = substr(line, paren_index)
111     sub(/msgid[,\)].*/, "", args)
112     for (n = 1; sub(/^[^,]*,/, "", args); n++) {
113         continue
114     }
115     format=""
116     if (args ~ /g$/)
117         format="gcc-internal-format"
118     else if (args ~ /noc$/)
119         format="no-c-format"
120     else if (args ~ /c$/)
121         format="c-format"
122
123     if (n == 1) { keyword = "--keyword=" name }
124     else {
125        keyword = "--keyword=" name ":" n
126        if (name ~ /_n$/)
127          keyword = keyword "," (n + 1)
128     }
129     if (format) {
130         keyword=keyword "\n--flag=" name ":" n ":" format
131         if (name ~ /_n$/)
132           keyword = keyword "\n--flag=" name ":" (n + 1) ":" format
133     }
134
135     if (! keyword_seen[name]) {
136         if (format == "gcc-internal-format")
137                 print keyword > kopt2
138         else
139                 print keyword > kopt
140         keyword_seen[name] = keyword
141     } else if (keyword_seen[name] != keyword) {
142         printf("%s used incompatibly as both %s and %s\n",
143                name, keyword_seen[name], keyword)
144         exit (1)
145     }
146 }
147
148 function spec_error_string (line) {
149     if (index(line, "%e") != 0 && index(line, "%n") != 0) return
150     while ((percent_index = index(line, "%e")) != 0 || 
151            (percent_index = index(line, "%n")) != 0) {
152         line = substr(line, percent_index + 2)
153
154         bracket_index = index(line, "}")
155         newline_index = index(line, "\\n")
156                 
157         quote_index = index(line, "\"")
158         if (bracket_index == 0 && newline_index == 0) return
159
160         if (bracket_index != 0) {
161           if (quote_index != 0 && bracket_index > quote_index) return
162           msgid = substr(line, 1, bracket_index - 1)
163           line = substr(line, bracket_index + 1)
164         }
165         else if (newline_index != 0) {
166           if (quote_index != 0 && quote_index > newline_index) return
167           msgid = substr(line, 1, newline_index - 1)
168           line = substr(line, newline_index + 1)
169         }
170
171         if (index(msgid, "%") != 0) continue
172
173         if ((newline_index = index(msgid, "\\n")) != 0)
174           msgid = substr(msgid, 1, newline_index - 1)
175         printf("#line %d \"%s\"\n", lineno, file) > emsg
176         printf("_(\"%s\")\n", msgid) > emsg
177     }
178 }
179
180 BEGIN {
181   while ((getline < excl) > 0) {
182     if ($0 ~ /^#/ || $0 ~ /^[   ]*$/)
183       continue
184     excludes[$1] = 1
185   }
186 }
187
188 { if (!($0 in excludes)) {
189     if ($0 ~ /.cc$/) {
190       print > posrcxx
191     } else {
192       print > posr
193     }
194     files[NR] = $0
195   }
196 }
197
198 END {
199     for (f in files) {
200         file = files[f]
201         lineno = 1
202         while (getline < file) {
203             if (/^(#[   ]*define[       ]*)?[A-Za-z_].*\(.*msgid[,\)]/) {
204                 keyword_option($0)
205             } else if (/^(#[   ]*define[       ]*)?[A-Za-z_].*(\(|\(.*,)$/) {
206                 name_line = $0
207                 while (getline < file) {
208                   lineno++
209                   if (/msgid[,\)]/){
210                     keyword_option(name_line $0)
211                     break
212                   } else if (/,$/) {
213                       name_line = name_line $0
214                       continue
215                   } else break
216                 }
217             } else if (/%e/ || /%n/) {
218                 spec_error_string($0)
219             }
220             lineno++
221         }
222     }
223     print emsg > posr
224 }'
225 ) || exit
226
227 echo "scanning option files..." >&2
228
229 ( cd $srcdir; find . -name '*.opt' -print |
230   $AWK '{
231     file = $1
232     lineno = 1
233     field = 0
234     while (getline < file) {
235         if (/^[ \t]*(;|$)/ || !/^[^ \t]/) {
236             field = 0
237         } else {
238             if ((field == 1) && /MissingArgError/) {
239                 line = $0
240                 sub(".*MissingArgError\\(", "", line)
241                 if (line ~ "^{") {
242                         sub("^{", "", line)
243                         sub("}\\).*", "", line)
244                 } else
245                         sub("\\).*", "", line)
246                 printf("#line %d \"%s\"\n", lineno, file)
247                 printf("_(\"%s\")\n", line)
248             }
249             if ((field == 1) && /UnknownError/) {
250                 line = $0
251                 sub(".*UnknownError\\(", "", line)
252                 if (line ~ "^{") {
253                         sub("^{", "", line)
254                         sub("}\\).*", "", line)
255                 } else
256                         sub("\\).*", "", line)
257                 printf("#line %d \"%s\"\n", lineno, file)
258                 printf("_(\"%s\")\n", line)
259             }
260             if ((field == 1) && /Warn\(/) {
261                 line = $0
262                 sub(".*Warn\\(", "", line)
263                 if (line ~ "^{") {
264                         sub("^{", "", line)
265                         sub("}\\).*", "", line)
266                 } else
267                         sub("\\).*", "", line)
268                 printf("#line %d \"%s\"\n", lineno, file)
269                 printf("_(\"%s\")\n", line)
270             }
271             if (field == 2) {
272                 line = $0
273                 printf("#line %d \"%s\"\n", lineno, file)
274                 printf("_(\"%s\")\n", line)
275             }
276             field++;
277         }
278         lineno++;
279     }
280   }') >> $emsg
281
282 # Run the xgettext commands, with temporary added as a file to scan.
283 echo "running xgettext..." >&2
284 $xgettext --default-domain=$package --directory=$srcdir \
285           --add-comments `cat $kopt` --files-from=$posr \
286           --copyright-holder="Free Software Foundation, Inc." \
287           --msgid-bugs-address="http://gcc.gnu.org/bugs.html" \
288           --language=c -o $pottmp1
289 if test -s $posrcxx; then
290   $xgettext --default-domain=$package --directory=$srcdir \
291             --add-comments `cat $kopt` --files-from=$posrcxx \
292             --copyright-holder="Free Software Foundation, Inc." \
293             --msgid-bugs-address="http://gcc.gnu.org/bugs.html" \
294             --language=c++ -o $pottmp2
295 else
296   echo > $pottmp2
297 fi
298 $xgettext --default-domain=$package --directory=$srcdir \
299           --add-comments --keyword= `cat $kopt2` --files-from=$posr \
300           --copyright-holder="Free Software Foundation, Inc." \
301           --msgid-bugs-address="http://gcc.gnu.org/bugs.html" \
302           --language=GCC-source -o $pottmp3
303 $xgettext --default-domain=$package \
304           --add-comments $pottmp1 $pottmp2 $pottmp3 \
305           --copyright-holder="Free Software Foundation, Inc." \
306           --msgid-bugs-address="http://gcc.gnu.org/bugs.html" \
307           --language=PO -o $pottmp
308 # Remove local paths from .pot file.
309 sed "s:$srcdir/::g;s:$pwd/::g;" <$pottmp >po/$package.pot