1#! /bin/sh
2# Wrapper around gettext for GCC sources.
3# Copyright 1998 Free Software Foundation, Inc.
4
5# Written by Paul Eggert <eggert@twinsun.com>.
6
7# This file is part of GNU CC.
8
9# GNU CC is free software; you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation; either version 2, or (at your option)
12# any later version.
13
14# GNU CC is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17# GNU General Public License for more details.
18
19# You should have received a copy of the GNU General Public License
20# along with GNU CC; see the file COPYING.  If not, write to
21# the Free Software Foundation, 59 Temple Place - Suite 330,
22# Boston, MA 02111-1307, USA.
23
24# Set environment to default value, if not already set.
25: ${AWK=awk}
26
27# The argument to this wrapper is the xgettext command to be executed.
28# Extract the xgettext program name from the rest of the command.
29xgettext=${1?}
30shift
31
32# Save work if we're just wrapping a no-op.
33case $xgettext in
34:) exit;;
35esac
36
37# Find the files to be scanned, and the directory to scan them from.
38directory=.
39files=
40for i
41do
42  case $i in
43  --directory=*)
44    directory=`expr " $i" : ' --directory=\(.*\)'`;;
45  --files-from=*)
46    files_from=`expr " $i" : ' --files-from=\(.*\)'`
47    files=`$AWK '/^[^#]/ { print }' $files_from`;;
48  esac
49done
50
51# Generate keyword options for xgettext,
52# by scanning for declarations of functions
53# whose parameter names end in "msgid".
54generate_keyword_options='
55  /^[A-Z_a-z].*\(.*msgid[,)]/ {
56
57    paren_index = index($0, "(")
58
59    name = substr($0, 1, paren_index - 1)
60    sub(/[^0-9A-Z_a-z]*$/, "", name)
61    sub(/[	 ]+PARAMS/, "", name)
62    sub(/[	 ]+VPROTO/, "", name)
63    sub(/.*[^0-9A-Z_a-z]/, "", name)
64
65    args = substr($0, paren_index)
66    sub(/msgid[,)].*/, "", args)
67    for (n = 1; sub(/^[^,]*,/, "", args); n++) {
68      continue;
69    }
70
71    if (n == 1) {
72      keyword = name
73    } else {
74      keyword = name ":" n
75    }
76
77    if (! keyword_seen[keyword]++) {
78      print "--keyword=" keyword
79    }
80  }
81'
82keyword_options=`(
83  cd $directory &&
84  $AWK "$generate_keyword_options" $files < /dev/null
85)` || exit
86
87# Generate temporary file reflecting the %e strings in the scanned files.
88tmp=tmp-emsgids.c
89
90generate_emsgids='
91  /%e.*}/ {
92    line = $0
93    while ((percent_index = index(line, "%e")) != 0) {
94      line = substr(line, percent_index + 2)
95      bracket_index = index(line, "}")
96      if (bracket_index == 0) {
97	continue
98      }
99      msgid = substr(line, 1, bracket_index - 1)
100      if (index(msgid, "%") != 0) {
101        continue
102      }
103      printf "#line %d \"%s\"\n", FNR, FILENAME
104      printf "_(\"%s\")\n", msgid
105      line = substr(line, bracket_index + 1)
106    }
107  }
108' 
109(cd $directory &&
110 $AWK "$generate_emsgids" $files < /dev/null
111) > $directory/$tmp || exit
112
113# Run the xgettext command, with temporary added as a file to scan.
114"$xgettext" $keyword_options ${1+"$@"} $tmp || exit
115
116# Clean up.
117# If we don't get here, `make clean' will remove this file later.
118rm -f $directory/$tmp
119