1#! /bin/sh
2# Wrapper around gettext for programs using the msgid convention.
3
4# Copyright (C) 1998, 2001 Free Software Foundation, Inc.
5
6# Written by Paul Eggert <eggert@twinsun.com>.
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 argument to this wrapper is the xgettext command to be executed.
33# Extract the xgettext program name from the rest of the command.
34xgettext=${1?}
35shift
36
37# Save work if we're just wrapping a no-op.
38case $xgettext in
39:) exit;;
40esac
41
42# Find the files to be scanned, and the directory to scan them from.
43directory=.
44files=
45for i
46do
47  case $i in
48  --directory=*)
49    directory=`expr " $i" : ' --directory=\(.*\)'`;;
50  --files-from=*)
51    files_from=`expr " $i" : ' --files-from=\(.*\)'`
52    files=`$AWK '/^[^#]/ { print }' $files_from`;;
53  esac
54done
55
56# Generate keyword options for xgettext,
57# by scanning for declarations of functions
58# whose parameter names end in "msgid".
59generate_keyword_options='
60  /^[A-Z_a-z].*\(.*msgid[,)]/ {
61
62    paren_index = index($0, "(")
63
64    name = substr($0, 1, paren_index - 1)
65    sub(/[^0-9A-Z_a-z]*$/, "", name)
66    sub(/[	 ]+PARAMS/, "", name)
67    sub(/[	 ]+VPARAMS/, "", name)
68    sub(/.*[^0-9A-Z_a-z]/, "", name)
69
70    args = substr($0, paren_index)
71    sub(/msgid[,)].*/, "", args)
72    for (n = 1; sub(/^[^,]*,/, "", args); n++) {
73      continue;
74    }
75
76    if (n == 1) {
77      keyword = name
78    } else {
79      keyword = name ":" n
80    }
81
82    if (! keyword_seen[keyword]++) {
83      print "--keyword=" keyword
84    }
85  }
86'
87keyword_options=`(
88  cd $directory &&
89  $AWK "$generate_keyword_options" $files < /dev/null
90)` || exit
91
92# Generate temporary file reflecting the %e strings in the scanned files.
93tmp=tmp-emsgids.c
94
95generate_emsgids='
96  /%e.*}/ {
97    line = $0
98    while ((percent_index = index(line, "%e")) != 0) {
99      line = substr(line, percent_index + 2)
100      bracket_index = index(line, "}")
101      if (bracket_index == 0) {
102	continue
103      }
104      msgid = substr(line, 1, bracket_index - 1)
105      if (index(msgid, "%") != 0) {
106        continue
107      }
108      printf "#line %d \"%s\"\n", FNR, FILENAME
109      printf "_(\"%s\")\n", msgid
110      line = substr(line, bracket_index + 1)
111    }
112  }
113'
114(cd $directory &&
115 $AWK "$generate_emsgids" $files < /dev/null
116) > $directory/$tmp || exit
117
118# Run the xgettext command, with temporary added as a file to scan.
119"$xgettext" $keyword_options ${1+"$@"} $tmp || exit
120
121# Clean up.
122# If we don't get here, `make clean' will remove this file later.
123rm -f $directory/$tmp
124