gcc:
[official-gcc.git] / gcc / po / exgettext
blob93f65c343702aaf288a53a36ba5ae10d66b253b3
1 #! /bin/sh
2 # Wrapper around gettext for programs using the msgid convention.
3 # Copyright 1998, 2001, 2002, 2003 Free Software Foundation, Inc.
5 # Written by Paul Eggert <eggert@twinsun.com>.
6 # Revised by Zack Weinberg <zackw@stanford.edu> for no-POTFILES operation.
8 # This file is part of GCC.
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.
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.
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.
25 # Always operate in the C locale.
26 LANG=C
27 LANGUAGE=C
28 LC_ALL=C
29 export LANG LANGUAGE LC_ALL
31 # Set AWK if environment has not already set it.
32 AWK=${AWK-awk}
34 # The arguments to this wrapper are: the program to execute, the
35 # name of the "package", and the path to the source directory.
37 if [ $# -ne 3 ]
38 then echo "usage: $0 <xgettext> <package> <srcdir>"
39 exit 1
42 xgettext=$1
43 package=$2
44 srcdir=$3
46 nl='
49 set -e
51 # Create temporary directory for scratch files.
52 T=exg$$.d
53 mkdir $T
54 trap "rm -r $T" 0
56 pwd=`${PWDCMD-pwd}`
57 kopt=$pwd/$T/keyword-options
58 emsg=$pwd/$T/emsgids.c
59 posr=$pwd/$T/po-sources
61 # Extra files to scan
62 extra_files=$pwd/options.c
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.
69 # Then generate keyword options for xgettext, by scanning for declarations
70 # of functions whose parameter names end in "msgid".
72 # Finally, generate a source file containing all %e strings from
73 # driver specs, so those can be translated too.
75 # All in one huge awk script.
77 echo "scanning for keywords and %e strings..." >&2
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)
97 args = substr(line, paren_index)
98 sub(/msgid[,\)].*/, "", args)
99 for (n = 1; sub(/^[^,]*,/, "", args); n++) {
100 continue
103 if (n == 1) { keyword = name }
104 else { keyword = name ":" n }
106 if (! keyword_seen[keyword]++) {
107 print "--keyword=" keyword > kopt
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 == "%") return
117 bracket_index = index(line, "}")
118 if (bracket_index == 0) return
120 msgid = substr(line, 1, bracket_index - 1)
121 if (index(msgid, "%") != 0) return
123 printf("#line %d \"%s\"\n", lineno, file) > emsg
124 printf("_(\"%s\")\n", msgid) > emsg
126 line = substr(line, bracket_index + 1)
130 BEGIN {
131 while ((getline < excl) > 0) {
132 if ($0 ~ /^#/ || $0 ~ /^[ ]*$/)
133 continue
134 excludes[$1] = 1
138 { if (!($0 in excludes)) {
139 print > posr
140 files[NR] = $0
144 END {
145 for (f in files) {
146 file = files[f]
147 lineno = 1
148 while (getline < file) {
149 if (/^(#[ ]*define[ ]*)?[A-Za-z_].*\(.*msgid[,\)]/) {
150 keyword_option($0)
151 } else if (/%e/) {
152 spec_error_string($0)
154 lineno++
157 print emsg > posr
161 # Run the xgettext command, with temporary added as a file to scan.
162 echo "running xgettext..." >&2
163 $xgettext --default-domain=$package --directory=$srcdir \
164 --add-comments `cat $kopt` --files-from=$posr \
165 --language=c -o po/$package.pot