exgettext 6.84 KB
Newer Older
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 6 7

# Written by Paul Eggert <eggert@twinsun.com>.
# Revised by Zack Weinberg <zackw@stanford.edu> for no-POTFILES operation.

8 9
# 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 3, or (at your option)
13 14
# any later version.

15
# GCC is distributed in the hope that it will be useful,
16 17 18 19 20
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
21 22
# along with GCC; see the file COPYING3.  If not see
# <http://www.gnu.org/licenses/>.
23

24 25 26 27 28 29 30 31
# Always operate in the C locale.
LANG=C
LANGUAGE=C
LC_ALL=C
export LANG LANGUAGE LC_ALL

# Set AWK if environment has not already set it.
AWK=${AWK-awk}
32 33 34 35 36 37 38 39 40 41 42 43 44

# The arguments to this wrapper are: the program to execute, the
# name of the "package", and the path to the source directory.

if [ $# -ne 3 ]
then	echo "usage: $0 <xgettext> <package> <srcdir>"
	exit 1
fi

xgettext=$1
package=$2
srcdir=$3

jakub's avatar
gcc/po/  
jakub committed
45 46 47 48 49 50
case `$xgettext --version | sed -e 1q | sed -e 's/^\([^0-9]*\)//'` in
  0.14.[5-9]* | 0.14.[1-9][0-9]* | 0.1[5-9]* | 0.[2-9][0-9]* | [1-9].*) : ;;
  *) echo "$xgettext is too old.  GNU xgettext 0.14.5 is required"
     exit 1 ;;
esac

51 52 53 54 55 56 57 58 59 60
nl='
'

set -e

# Create temporary directory for scratch files.
T=exg$$.d
mkdir $T
trap "rm -r $T" 0

61
pwd=`${PWDCMD-pwd}`
62
kopt=$pwd/$T/keyword-options
jakub's avatar
gcc/po/  
jakub committed
63
kopt2=$pwd/$T/keyword2-options
64 65
emsg=$pwd/$T/emsgids.c
posr=$pwd/$T/po-sources
jakub's avatar
gcc/po/  
jakub committed
66 67 68
pottmp1=$pwd/$T/tmp1.pot
pottmp2=$pwd/$T/tmp2.pot
pottmp=$pwd/$T/tmp.pot
69

70 71
# Locate files to scan, and generate the list.  All .c, .h, and .def files
# in $srcdir are examined, likewise $srcdir/config and $srcdir/config/*
72 73 74 75 76 77
# (directories).  Also, all subdirectories of $srcdir that contain a
# config-lang.in.  Exclusions come from $srcdir/po/EXCLUDE.
#
# Then generate keyword options for xgettext, by scanning for declarations
# of functions whose parameter names end in "msgid".
#
pzhao's avatar
po/  
pzhao committed
78
# Finally, generate a source file containing all %e and %n strings from
79 80 81 82
# driver specs, so those can be translated too.
#
# All in one huge awk script.

pzhao's avatar
po/  
pzhao committed
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 88 89 90 91 92
  { for dir in "" config/ config/*/ $lang_subdirs
    do  for glob in '*.c' '*.h' '*.def'
        do  eval echo $dir$glob
        done
    done;
  } | tr ' ' "$nl" | grep -v '\*' |
jakub's avatar
gcc/po/  
jakub committed
93
  $AWK -v excl=po/EXCLUDES -v posr=$posr -v kopt=$kopt -v kopt2=$kopt2 -v emsg=$emsg '
94 95 96 97 98 99 100 101 102 103 104 105 106
function keyword_option(line) {
    paren_index = index(line, "(")
    name = substr(line, 1, paren_index - 1)
    sub(/[^0-9A-Z_a-z]*$/, "", name)
    sub(/[	 ]+PARAMS/, "", name)
    sub(/[	 ]+VPARAMS/, "", name)
    sub(/.*[^0-9A-Z_a-z]/, "", name)

    args = substr(line, paren_index)
    sub(/msgid[,\)].*/, "", args)
    for (n = 1; sub(/^[^,]*,/, "", args); n++) {
	continue
    }
jakub's avatar
gcc/po/  
jakub committed
107 108 109
    format=""
    if (args ~ /g$/)
    	format="gcc-internal-format"
fxcoudert's avatar
fxcoudert committed
110 111
    else if (args ~ /noc$/)
        format="no-c-format"
jakub's avatar
gcc/po/  
jakub committed
112 113 114 115 116 117 118 119
    else if (args ~ /c$/)
    	format="c-format"

    if (n == 1) { keyword = "--keyword=" name }
    else { keyword = "--keyword=" name ":" n }
    if (format) {
        keyword=keyword "\n--flag=" name ":" n ":" format
    }
120

jakub's avatar
gcc/po/  
jakub committed
121 122 123 124 125 126 127 128 129 130
    if (! keyword_seen[name]) {
	if (format == "gcc-internal-format")
		print keyword > kopt2
	else
		print keyword > kopt
    	keyword_seen[name] = keyword
    } else if (keyword_seen[name] != keyword) {
	printf("%s used incompatibly as both %s and %s\n",
	       name, keyword_seen[name], keyword)
	exit (1)
131 132 133 134
    }
}

function spec_error_string (line) {
pzhao's avatar
po/  
pzhao committed
135 136 137
    if (index(line, "%e") != 0 && index(line, "%n") != 0) return
    while ((percent_index = index(line, "%e")) != 0 || 
	   (percent_index = index(line, "%n")) != 0) {
138 139 140
	line = substr(line, percent_index + 2)

	bracket_index = index(line, "}")
pzhao's avatar
po/  
pzhao committed
141 142
	newline_index = index(line, "\\n")
		
zack's avatar
top:  
zack committed
143
	quote_index = index(line, "\"")
pzhao's avatar
po/  
pzhao committed
144
	if (bracket_index == 0 && newline_index == 0) return
145

pzhao's avatar
po/  
pzhao committed
146 147 148 149 150 151 152 153 154 155
	if (bracket_index != 0) {
	  if (quote_index != 0 && bracket_index > quote_index) return
	  msgid = substr(line, 1, bracket_index - 1)
	  line = substr(line, bracket_index + 1)
	}
	else if (newline_index != 0) {
	  if (quote_index != 0 && quote_index > newline_index) return
	  msgid = substr(line, 1, newline_index - 1)
	  line = substr(line, newline_index + 1)
	}
zack's avatar
top:  
zack committed
156 157

	if (index(msgid, "%") != 0) continue
158

pzhao's avatar
po/  
pzhao committed
159 160
	if ((newline_index = index(msgid, "\\n")) != 0)
	  msgid = substr(msgid, 1, newline_index - 1)
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
	printf("#line %d \"%s\"\n", lineno, file) > emsg
	printf("_(\"%s\")\n", msgid) > emsg
    }
}

BEGIN {
  while ((getline < excl) > 0) {
    if ($0 ~ /^#/ || $0 ~ /^[ 	]*$/)
      continue
    excludes[$1] = 1
  }
}

{ if (!($0 in excludes)) {
    print > posr
    files[NR] = $0
  }
}

END {
    for (f in files) {
	file = files[f]
	lineno = 1
	while (getline < file) {
	    if (/^(#[ 	]*define[ 	]*)?[A-Za-z_].*\(.*msgid[,\)]/) {
		keyword_option($0)
187 188 189 190 191 192 193 194 195 196 197 198
	    } else if (/^(#[   ]*define[       ]*)?[A-Za-z_].*(\(|\(.*,)$/) {
		name_line = $0
		while (getline < file) {
		  lineno++
		  if (/msgid[,\)]/){
		    keyword_option(name_line $0)
		    break
		  } else if (/,$/) {
		      name_line = name_line $0
		      continue
		  } else break
		}
pzhao's avatar
po/  
pzhao committed
199
	    } else if (/%e/ || /%n/) {
200 201 202 203 204 205 206
		spec_error_string($0)
	    }
	    lineno++
	}
    }
    print emsg > posr
}'
jakub's avatar
gcc/po/  
jakub committed
207
) || exit
208

rsandifo's avatar
gcc/  
rsandifo committed
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
echo "scanning option files..." >&2

( cd $srcdir; find . -name '*.opt' -print |
  $AWK '{
    file = $1
    lineno = 1
    field = 0
    while (getline < file) {
	if (/^[ \t]*(;|$)/ || !/^[^ \t]/) {
	    field = 0
	} else {
	    if (field == 2) {
		line = $0
		printf("#line %d \"%s\"\n", lineno, file)
		printf("_(\"%s\")\n", line)
	    }
	    field++;
	}
	lineno++;
    }
  }') >> $emsg

231 232 233 234
# Run the xgettext command, with temporary added as a file to scan.
echo "running xgettext..." >&2
$xgettext --default-domain=$package --directory=$srcdir \
	  --add-comments `cat $kopt` --files-from=$posr \
235 236
	  --copyright-holder="Free Software Foundation, Inc." \
	  --msgid-bugs-address="http://gcc.gnu.org/bugs.html" \
jakub's avatar
gcc/po/  
jakub committed
237 238 239 240 241 242 243 244 245 246 247
	  --language=c -o $pottmp1
$xgettext --default-domain=$package --directory=$srcdir \
	  --add-comments --keyword= `cat $kopt2` --files-from=$posr \
	  --copyright-holder="Free Software Foundation, Inc." \
	  --msgid-bugs-address="http://gcc.gnu.org/bugs.html" \
	  --language=GCC-source -o $pottmp2
$xgettext --default-domain=$package \
	  --add-comments $pottmp1 $pottmp2 \
	  --copyright-holder="Free Software Foundation, Inc." \
	  --msgid-bugs-address="http://gcc.gnu.org/bugs.html" \
	  --language=PO -o $pottmp
248
# Remove local paths from .pot file.
jakub's avatar
gcc/po/  
jakub committed
249
sed "s:$srcdir/::g;s:$pwd/::g;" <$pottmp >po/$package.pot