OSDN Git Service

PR tree-optimization/32772
[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 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 2, 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 COPYING.  If not, write to
22 # the Free Software Foundation, 51 Franklin Street, Fifth Floor,
23 # Boston, MA 02110-1301, USA.
24
25 # Always operate in the C locale.
26 LANG=C
27 LANGUAGE=C
28 LC_ALL=C
29 export LANG LANGUAGE LC_ALL
30
31 # Set AWK if environment has not already set it.
32 AWK=${AWK-awk}
33
34 # The arguments to this wrapper are: the program to execute, the
35 # name of the "package", and the path to the source directory.
36
37 if [ $# -ne 3 ]
38 then    echo "usage: $0 <xgettext> <package> <srcdir>"
39         exit 1
40 fi
41
42 xgettext=$1
43 package=$2
44 srcdir=$3
45
46 case `$xgettext --version | sed -e 1q | sed -e 's/^\([^0-9]*\)//'` in
47   0.14.[5-9]* | 0.14.[1-9][0-9]* | 0.1[5-9]* | 0.[2-9][0-9]* | [1-9].*) : ;;
48   *) echo "$xgettext is too old.  GNU xgettext 0.14.5 is required"
49      exit 1 ;;
50 esac
51
52 nl='
53 '
54
55 set -e
56
57 # Create temporary directory for scratch files.
58 T=exg$$.d
59 mkdir $T
60 trap "rm -r $T" 0
61
62 pwd=`${PWDCMD-pwd}`
63 kopt=$pwd/$T/keyword-options
64 kopt2=$pwd/$T/keyword2-options
65 emsg=$pwd/$T/emsgids.c
66 posr=$pwd/$T/po-sources
67 pottmp1=$pwd/$T/tmp1.pot
68 pottmp2=$pwd/$T/tmp2.pot
69 pottmp=$pwd/$T/tmp.pot
70
71 # Locate files to scan, and generate the list.  All .c, .h, and .def files
72 # in $srcdir are examined, likewise $srcdir/config and $srcdir/config/*
73 # (directories).  Also, all subdirectories of $srcdir that contain a
74 # config-lang.in.  Exclusions come from $srcdir/po/EXCLUDE.
75 #
76 # Then generate keyword options for xgettext, by scanning for declarations
77 # of functions whose parameter names end in "msgid".
78 #
79 # Finally, generate a source file containing all %e strings from
80 # driver specs, so those can be translated too.
81 #
82 # All in one huge awk script.
83
84 echo "scanning for keywords and %e strings..." >&2
85
86 ( cd $srcdir
87   lang_subdirs=`echo */config-lang.in | sed -e 's|config-lang\.in||g'`
88   { for dir in "" config/ config/*/ $lang_subdirs
89     do  for glob in '*.c' '*.h' '*.def'
90         do  eval echo $dir$glob
91         done
92     done;
93   } | tr ' ' "$nl" | grep -v '\*' |
94   $AWK -v excl=po/EXCLUDES -v posr=$posr -v kopt=$kopt -v kopt2=$kopt2 -v emsg=$emsg '
95 function keyword_option(line) {
96     paren_index = index(line, "(")
97     name = substr(line, 1, paren_index - 1)
98     sub(/[^0-9A-Z_a-z]*$/, "", name)
99     sub(/[       ]+PARAMS/, "", name)
100     sub(/[       ]+VPARAMS/, "", name)
101     sub(/.*[^0-9A-Z_a-z]/, "", name)
102
103     args = substr(line, paren_index)
104     sub(/msgid[,\)].*/, "", args)
105     for (n = 1; sub(/^[^,]*,/, "", args); n++) {
106         continue
107     }
108     format=""
109     if (args ~ /g$/)
110         format="gcc-internal-format"
111     else if (args ~ /noc$/)
112         format="no-c-format"
113     else if (args ~ /c$/)
114         format="c-format"
115
116     if (n == 1) { keyword = "--keyword=" name }
117     else { keyword = "--keyword=" name ":" n }
118     if (format) {
119         keyword=keyword "\n--flag=" name ":" n ":" format
120     }
121
122     if (! keyword_seen[name]) {
123         if (format == "gcc-internal-format")
124                 print keyword > kopt2
125         else
126                 print keyword > kopt
127         keyword_seen[name] = keyword
128     } else if (keyword_seen[name] != keyword) {
129         printf("%s used incompatibly as both %s and %s\n",
130                name, keyword_seen[name], keyword)
131         exit (1)
132     }
133 }
134
135 function spec_error_string (line) {
136     while ((percent_index = index(line, "%e")) != 0) {
137         escape = substr(line, percent_index - 1, 1)
138         line = substr(line, percent_index + 2)
139         if (escape == "%") continue
140
141         bracket_index = index(line, "}")
142         quote_index = index(line, "\"")
143         if (bracket_index == 0) return
144         if (quote_index != 0 && bracket_index > quote_index) return
145
146         msgid = substr(line, 1, bracket_index - 1)
147         line = substr(line, bracket_index + 1)
148
149         if (index(msgid, "%") != 0) continue
150
151         printf("#line %d \"%s\"\n", lineno, file) > emsg
152         printf("_(\"%s\")\n", msgid) > emsg
153
154     }
155 }
156
157 BEGIN {
158   while ((getline < excl) > 0) {
159     if ($0 ~ /^#/ || $0 ~ /^[   ]*$/)
160       continue
161     excludes[$1] = 1
162   }
163 }
164
165 { if (!($0 in excludes)) {
166     print > posr
167     files[NR] = $0
168   }
169 }
170
171 END {
172     for (f in files) {
173         file = files[f]
174         lineno = 1
175         while (getline < file) {
176             if (/^(#[   ]*define[       ]*)?[A-Za-z_].*\(.*msgid[,\)]/) {
177                 keyword_option($0)
178             } else if (/%e/) {
179                 spec_error_string($0)
180             }
181             lineno++
182         }
183     }
184     print emsg > posr
185 }'
186 ) || exit
187
188 echo "scanning option files..." >&2
189
190 ( cd $srcdir; find . -name '*.opt' -print |
191   $AWK '{
192     file = $1
193     lineno = 1
194     field = 0
195     while (getline < file) {
196         if (/^[ \t]*(;|$)/ || !/^[^ \t]/) {
197             field = 0
198         } else {
199             if (field == 2) {
200                 line = $0
201                 gsub(".*\t", "", line)
202                 printf("#line %d \"%s\"\n", lineno, file)
203                 printf("_(\"%s\")\n", line)
204             }
205             field++;
206         }
207         lineno++;
208     }
209   }') >> $emsg
210
211 # Run the xgettext command, with temporary added as a file to scan.
212 echo "running xgettext..." >&2
213 $xgettext --default-domain=$package --directory=$srcdir \
214           --add-comments `cat $kopt` --files-from=$posr \
215           --copyright-holder="Free Software Foundation, Inc." \
216           --msgid-bugs-address="http://gcc.gnu.org/bugs.html" \
217           --language=c -o $pottmp1
218 $xgettext --default-domain=$package --directory=$srcdir \
219           --add-comments --keyword= `cat $kopt2` --files-from=$posr \
220           --copyright-holder="Free Software Foundation, Inc." \
221           --msgid-bugs-address="http://gcc.gnu.org/bugs.html" \
222           --language=GCC-source -o $pottmp2
223 $xgettext --default-domain=$package \
224           --add-comments $pottmp1 $pottmp2 \
225           --copyright-holder="Free Software Foundation, Inc." \
226           --msgid-bugs-address="http://gcc.gnu.org/bugs.html" \
227           --language=PO -o $pottmp
228 # Remove local paths from .pot file.
229 sed "s:$srcdir/::g;s:$pwd/::g;" <$pottmp >po/$package.pot