1#! /bin/sh 2# Wrapper around gettext for programs using the msgid convention. 3# Copyright 1998, 2001, 2002 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 program is free software; you can redistribute it and/or modify 9# it under the terms of the GNU General Public License as published by 10# the Free Software Foundation; either version 2, or (at your option) 11# any later version. 12 13# This program is distributed in the hope that it will be useful, 14# but WITHOUT ANY WARRANTY; without even the implied warranty of 15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16# GNU General Public License for more details. 17 18# You should have received a copy of the GNU General Public License 19# along with GNU CC; see the file COPYING. If not, write to 20# the Free Software Foundation, 59 Temple Place - Suite 330, 21# Boston, MA 02111-1307, USA. 22 23# Always operate in the C locale. 24LANG=C 25LANGUAGE=C 26LC_ALL=C 27export LANG LANGUAGE LC_ALL 28 29# Set AWK if environment has not already set it. 30AWK=${AWK-awk} 31 32# The arguments to this wrapper are: the program to execute, the 33# name of the "package", and the path to the source directory. 34 35if [ $# -ne 3 ] 36then echo "usage: $0 <xgettext> <package> <srcdir>" 37 exit 1 38fi 39 40xgettext=$1 41package=$2 42srcdir=$3 43 44nl=' 45' 46 47set -e 48 49# Create temporary directory for scratch files. 50T=exg$$.d 51mkdir $T 52trap "rm -r $T" 0 53 54pwd=`${PWDCMD-pwd}` 55kopt=$pwd/$T/keyword-options 56emsg=$pwd/$T/emsgids.c 57posr=$pwd/$T/po-sources 58 59# Locate files to scan, and generate the list. All .c, .h, and .def files 60# in $srcdir are examined, likewise $srcdir/config and $srcdir/config/* 61# (directories). Also, all subdirectories of $srcdir that contain a 62# config-lang.in. Exclusions come from $srcdir/po/EXCLUDE. 63# 64# Then generate keyword options for xgettext, by scanning for declarations 65# of functions whose parameter names end in "msgid". 66# 67# Finally, generate a source file containing all %e strings from 68# driver specs, so those can be translated too. 69# 70# All in one huge awk script. 71 72echo "scanning for keywords and %e strings..." >&2 73 74( cd $srcdir 75 lang_subdirs=`echo */config-lang.in | sed -e 's|config-lang\.in||g'` 76 for dir in "" config/ config/*/ $lang_subdirs 77 do for glob in '*.c' '*.h' '*.def' 78 do eval echo $dir$glob 79 done 80 done | tr ' ' "$nl" | grep -v '\*' | 81 $AWK -v excl=po/EXCLUDES -v posr=$posr -v kopt=$kopt -v emsg=$emsg ' 82function keyword_option(line) { 83 paren_index = index(line, "(") 84 name = substr(line, 1, paren_index - 1) 85 sub(/[^0-9A-Z_a-z]*$/, "", name) 86 sub(/[ ]+PARAMS/, "", name) 87 sub(/[ ]+VPARAMS/, "", name) 88 sub(/.*[^0-9A-Z_a-z]/, "", name) 89 90 args = substr(line, paren_index) 91 sub(/msgid[,\)].*/, "", args) 92 for (n = 1; sub(/^[^,]*,/, "", args); n++) { 93 continue 94 } 95 96 if (n == 1) { keyword = name } 97 else { keyword = name ":" n } 98 99 if (! keyword_seen[keyword]++) { 100 print "--keyword=" keyword > kopt 101 } 102} 103 104function spec_error_string (line) { 105 while ((percent_index = index(line, "%e")) != 0) { 106 escape = substr(line, percent_index - 1, 1) 107 line = substr(line, percent_index + 2) 108 if (escape == "%") return 109 110 bracket_index = index(line, "}") 111 if (bracket_index == 0) return 112 113 msgid = substr(line, 1, bracket_index - 1) 114 if (index(msgid, "%") != 0) return 115 116 printf("#line %d \"%s\"\n", lineno, file) > emsg 117 printf("_(\"%s\")\n", msgid) > emsg 118 119 line = substr(line, bracket_index + 1) 120 } 121} 122 123BEGIN { 124 while ((getline < excl) > 0) { 125 if ($0 ~ /^#/ || $0 ~ /^[ ]*$/) 126 continue 127 excludes[$1] = 1 128 } 129} 130 131{ if (!($0 in excludes)) { 132 print > posr 133 files[NR] = $0 134 } 135} 136 137END { 138 for (f in files) { 139 file = files[f] 140 lineno = 1 141 while (getline < file) { 142 if (/^(#[ ]*define[ ]*)?[A-Za-z_].*\(.*msgid[,\)]/) { 143 keyword_option($0) 144 } else if (/%e/) { 145 spec_error_string($0) 146 } 147 lineno++ 148 } 149 } 150 print emsg > posr 151}' 152) 153 154# Run the xgettext command, with temporary added as a file to scan. 155echo "running xgettext..." >&2 156$xgettext --default-domain=$package --directory=$srcdir \ 157 --add-comments `cat $kopt` --files-from=$posr \ 158 --language=c -o po/$package.pot 159