OSDN Git Service

PR c/13519
[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, 59 Temple Place - Suite 330,
23 # Boston, MA 02111-1307, 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 nl='
47 '
48
49 set -e
50
51 # Create temporary directory for scratch files.
52 T=exg$$.d
53 mkdir $T
54 trap "rm -r $T" 0
55
56 pwd=`${PWDCMD-pwd}`
57 kopt=$pwd/$T/keyword-options
58 emsg=$pwd/$T/emsgids.c
59 posr=$pwd/$T/po-sources
60
61 # Extra files to scan
62 extra_files=$pwd/options.c
63
64 # Locate files to scan, and generate the list.  All .c, .h, and .def files
65 # in $srcdir are examined, likewise $srcdir/config and $srcdir/config/*
66 # (directories).  Also, all subdirectories of $srcdir that contain a
67 # config-lang.in.  Exclusions come from $srcdir/po/EXCLUDE.
68 #
69 # Then generate keyword options for xgettext, by scanning for declarations
70 # of functions whose parameter names end in "msgid".
71 #
72 # Finally, generate a source file containing all %e strings from
73 # driver specs, so those can be translated too.
74 #
75 # All in one huge awk script.
76
77 echo "scanning for keywords and %e strings..." >&2
78
79 ( cd $srcdir
80   lang_subdirs=`echo */config-lang.in | sed -e 's|config-lang\.in||g'`
81   { for dir in "" config/ config/*/ $lang_subdirs
82     do  for glob in '*.c' '*.h' '*.def'
83         do  eval echo $dir$glob
84         done
85     done;
86     echo $extra_files;
87   } | tr ' ' "$nl" | grep -v '\*' |
88   $AWK -v excl=po/EXCLUDES -v posr=$posr -v kopt=$kopt -v emsg=$emsg '
89 function keyword_option(line) {
90     paren_index = index(line, "(")
91     name = substr(line, 1, paren_index - 1)
92     sub(/[^0-9A-Z_a-z]*$/, "", name)
93     sub(/[       ]+PARAMS/, "", name)
94     sub(/[       ]+VPARAMS/, "", name)
95     sub(/.*[^0-9A-Z_a-z]/, "", name)
96
97     args = substr(line, paren_index)
98     sub(/msgid[,\)].*/, "", args)
99     for (n = 1; sub(/^[^,]*,/, "", args); n++) {
100         continue
101     }
102
103     if (n == 1) { keyword = name }
104     else        { keyword = name ":" n }
105
106     if (! keyword_seen[keyword]++) {
107         print "--keyword=" keyword > kopt
108     }
109 }
110
111 function spec_error_string (line) {
112     while ((percent_index = index(line, "%e")) != 0) {
113         escape = substr(line, percent_index - 1, 1)
114         line = substr(line, percent_index + 2)
115         if (escape == "%") continue
116
117         bracket_index = index(line, "}")
118         quote_index = index(line, "\"")
119         if (bracket_index == 0) return
120         if (quote_index != 0 && bracket_index > quote_index) return
121
122         msgid = substr(line, 1, bracket_index - 1)
123         line = substr(line, bracket_index + 1)
124
125         if (index(msgid, "%") != 0) continue
126
127         printf("#line %d \"%s\"\n", lineno, file) > emsg
128         printf("_(\"%s\")\n", msgid) > emsg
129
130     }
131 }
132
133 BEGIN {
134   while ((getline < excl) > 0) {
135     if ($0 ~ /^#/ || $0 ~ /^[   ]*$/)
136       continue
137     excludes[$1] = 1
138   }
139 }
140
141 { if (!($0 in excludes)) {
142     print > posr
143     files[NR] = $0
144   }
145 }
146
147 END {
148     for (f in files) {
149         file = files[f]
150         lineno = 1
151         while (getline < file) {
152             if (/^(#[   ]*define[       ]*)?[A-Za-z_].*\(.*msgid[,\)]/) {
153                 keyword_option($0)
154             } else if (/%e/) {
155                 spec_error_string($0)
156             }
157             lineno++
158         }
159     }
160     print emsg > posr
161 }'
162 )
163
164 # Run the xgettext command, with temporary added as a file to scan.
165 echo "running xgettext..." >&2
166 $xgettext --default-domain=$package --directory=$srcdir \
167           --add-comments `cat $kopt` --files-from=$posr \
168           --copyright-holder="Free Software Foundation, Inc." \
169           --msgid-bugs-address="http://gcc.gnu.org/bugs.html" \
170           --language=c -o po/$package.pot