OSDN Git Service

2010-01-22 Shujing Zhao <pearly.zhao@oracle.com>
[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 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 pottmp1=$pwd/$T/tmp1.pot
67 pottmp2=$pwd/$T/tmp2.pot
68 pottmp=$pwd/$T/tmp.pot
69
70 # Locate files to scan, and generate the list.  All .c, .h, and .def files
71 # in $srcdir are examined, likewise $srcdir/config and $srcdir/config/*
72 # (directories).  Also, all subdirectories of $srcdir that contain a
73 # config-lang.in.  Exclusions come from $srcdir/po/EXCLUDE.
74 #
75 # Then generate keyword options for xgettext, by scanning for declarations
76 # of functions whose parameter names end in "msgid".
77 #
78 # Finally, generate a source file containing all %e and %n strings from
79 # driver specs, so those can be translated too.
80 #
81 # All in one huge awk script.
82
83 echo "scanning for keywords, %e and %n strings..." >&2
84
85 ( cd $srcdir
86   lang_subdirs=`echo */config-lang.in */*/config-lang.in | sed -e 's|config-lang\.in||g'`
87   { for dir in "" config/ config/*/ $lang_subdirs
88     do  for glob in '*.c' '*.h' '*.def'
89         do  eval echo $dir$glob
90         done
91     done;
92   } | tr ' ' "$nl" | grep -v '\*' |
93   $AWK -v excl=po/EXCLUDES -v posr=$posr -v kopt=$kopt -v kopt2=$kopt2 -v emsg=$emsg '
94 function keyword_option(line) {
95     paren_index = index(line, "(")
96     name = substr(line, 1, paren_index - 1)
97     sub(/[^0-9A-Z_a-z]*$/, "", name)
98     sub(/[       ]+PARAMS/, "", name)
99     sub(/[       ]+VPARAMS/, "", name)
100     sub(/.*[^0-9A-Z_a-z]/, "", name)
101
102     args = substr(line, paren_index)
103     sub(/msgid[,\)].*/, "", args)
104     for (n = 1; sub(/^[^,]*,/, "", args); n++) {
105         continue
106     }
107     format=""
108     if (args ~ /g$/)
109         format="gcc-internal-format"
110     else if (args ~ /noc$/)
111         format="no-c-format"
112     else if (args ~ /c$/)
113         format="c-format"
114
115     if (n == 1) { keyword = "--keyword=" name }
116     else { keyword = "--keyword=" name ":" n }
117     if (format) {
118         keyword=keyword "\n--flag=" name ":" n ":" format
119     }
120
121     if (! keyword_seen[name]) {
122         if (format == "gcc-internal-format")
123                 print keyword > kopt2
124         else
125                 print keyword > kopt
126         keyword_seen[name] = keyword
127     } else if (keyword_seen[name] != keyword) {
128         printf("%s used incompatibly as both %s and %s\n",
129                name, keyword_seen[name], keyword)
130         exit (1)
131     }
132 }
133
134 function spec_error_string (line) {
135     if (index(line, "%e") != 0 && index(line, "%n") != 0) return
136     while ((percent_index = index(line, "%e")) != 0 || 
137            (percent_index = index(line, "%n")) != 0) {
138         line = substr(line, percent_index + 2)
139
140         bracket_index = index(line, "}")
141         newline_index = index(line, "\\n")
142                 
143         quote_index = index(line, "\"")
144         if (bracket_index == 0 && newline_index == 0) return
145
146         if (bracket_index != 0) {
147           if (quote_index != 0 && bracket_index > quote_index) return
148           msgid = substr(line, 1, bracket_index - 1)
149           line = substr(line, bracket_index + 1)
150         }
151         else if (newline_index != 0) {
152           if (quote_index != 0 && quote_index > newline_index) return
153           msgid = substr(line, 1, newline_index - 1)
154           line = substr(line, newline_index + 1)
155         }
156
157         if (index(msgid, "%") != 0) continue
158
159         if ((newline_index = index(msgid, "\\n")) != 0)
160           msgid = substr(msgid, 1, newline_index - 1)
161         printf("#line %d \"%s\"\n", lineno, file) > emsg
162         printf("_(\"%s\")\n", msgid) > emsg
163     }
164 }
165
166 BEGIN {
167   while ((getline < excl) > 0) {
168     if ($0 ~ /^#/ || $0 ~ /^[   ]*$/)
169       continue
170     excludes[$1] = 1
171   }
172 }
173
174 { if (!($0 in excludes)) {
175     print > posr
176     files[NR] = $0
177   }
178 }
179
180 END {
181     for (f in files) {
182         file = files[f]
183         lineno = 1
184         while (getline < file) {
185             if (/^(#[   ]*define[       ]*)?[A-Za-z_].*\(.*msgid[,\)]/) {
186                 keyword_option($0)
187             } else if (/^(#[   ]*define[       ]*)?[A-Za-z_].*(\(|\(.*,)$/) {
188                 name_line = $0
189                 while (getline < file) {
190                   lineno++
191                   if (/msgid[,\)]/){
192                     keyword_option(name_line $0)
193                     break
194                   } else if (/,$/) {
195                       name_line = name_line $0
196                       continue
197                   } else break
198                 }
199             } else if (/%e/ || /%n/) {
200                 spec_error_string($0)
201             }
202             lineno++
203         }
204     }
205     print emsg > posr
206 }'
207 ) || exit
208
209 echo "scanning option files..." >&2
210
211 ( cd $srcdir; find . -name '*.opt' -print |
212   $AWK '{
213     file = $1
214     lineno = 1
215     field = 0
216     while (getline < file) {
217         if (/^[ \t]*(;|$)/ || !/^[^ \t]/) {
218             field = 0
219         } else {
220             if (field == 2) {
221                 line = $0
222                 printf("#line %d \"%s\"\n", lineno, file)
223                 printf("_(\"%s\")\n", line)
224             }
225             field++;
226         }
227         lineno++;
228     }
229   }') >> $emsg
230
231 # Run the xgettext command, with temporary added as a file to scan.
232 echo "running xgettext..." >&2
233 $xgettext --default-domain=$package --directory=$srcdir \
234           --add-comments `cat $kopt` --files-from=$posr \
235           --copyright-holder="Free Software Foundation, Inc." \
236           --msgid-bugs-address="http://gcc.gnu.org/bugs.html" \
237           --language=c -o $pottmp1
238 $xgettext --default-domain=$package --directory=$srcdir \
239           --add-comments --keyword= `cat $kopt2` --files-from=$posr \
240           --copyright-holder="Free Software Foundation, Inc." \
241           --msgid-bugs-address="http://gcc.gnu.org/bugs.html" \
242           --language=GCC-source -o $pottmp2
243 $xgettext --default-domain=$package \
244           --add-comments $pottmp1 $pottmp2 \
245           --copyright-holder="Free Software Foundation, Inc." \
246           --msgid-bugs-address="http://gcc.gnu.org/bugs.html" \
247           --language=PO -o $pottmp
248 # Remove local paths from .pot file.
249 sed "s:$srcdir/::g;s:$pwd/::g;" <$pottmp >po/$package.pot