Fix a signal cleanup problem.
[gnupg.git] / common / mkstrtable.awk
blob56e9bb8811441539a5338d1785e238bf4878daac
1 # mkstrtable.awk
2 # Copyright (C) 2003, 2004 g10 Code GmbH
3 #
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License as
6 # published by the Free Software Foundation; either version 2 of
7 # the License, or (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 # General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, see <http://www.gnu.org/licenses/>.
17 # As a special exception, g10 Code GmbH gives unlimited permission to
18 # copy, distribute and modify the C source files that are the output
19 # of mkstrtable.awk. You need not follow the terms of the GNU General
20 # Public License when using or distributing such scripts, even though
21 # portions of the text of mkstrtable.awk appear in them. The GNU
22 # General Public License (GPL) does govern all other use of the material
23 # that constitutes the mkstrtable.awk program.
25 # Certain portions of the mkstrtable.awk source text are designed to be
26 # copied (in certain cases, depending on the input) into the output of
27 # mkstrtable.awk. We call these the "data" portions. The rest of the
28 # mkstrtable.awk source text consists of comments plus executable code
29 # that decides which of the data portions to output in any given case.
30 # We call these comments and executable code the "non-data" portions.
31 # mkstrtable.h never copies any of the non-data portions into its output.
33 # This special exception to the GPL applies to versions of mkstrtable.awk
34 # released by g10 Code GmbH. When you make and distribute a modified version
35 # of mkstrtable.awk, you may extend this special exception to the GPL to
36 # apply to your modified version as well, *unless* your modified version
37 # has the potential to copy into its output some of the text that was the
38 # non-data portion of the version that you started with. (In other words,
39 # unless your change moves or copies text from the non-data portions to the
40 # data portions.) If your modification has such potential, you must delete
41 # any notice of this special exception to the GPL from your modified version.
43 # This script outputs a source file that does define the following
44 # symbols:
46 # static const char msgstr[];
47 # A string containing all messages in the list.
49 # static const int msgidx[];
50 # A list of index numbers, one for each message, that points to the
51 # beginning of the string in msgstr.
53 # msgidxof (code);
54 # A macro that maps code numbers to idx numbers. If a DEFAULT MESSAGE
55 # is provided (see below), its index will be returned for unknown codes.
56 # Otherwise -1 is returned for codes that do not appear in the list.
57 # You can lookup the message with code CODE with:
58 # msgstr + msgidx[msgidxof (code)].
60 # The input file has the following format:
61 # CODE1 ... MESSAGE1 (code nr, <tab>, something, <tab>, msg)
62 # CODE2 ... MESSAGE2 (code nr, <tab>, something, <tab>, msg)
63 # ...
64 # CODEn ... MESSAGEn (code nr, <tab>, something, <tab>, msg)
65 # ... DEFAULT-MESSAGE (<tab>, something, <tab>, fall-back msg)
67 # Comments (starting with # and ending at the end of the line) are removed,
68 # as is trailing whitespace. The last line is optional; if no DEFAULT
69 # MESSAGE is given, msgidxof will return the number -1 for unknown
70 # index numbers.
72 # The field to be used is specified with the variable "textidx" on
73 # the command line. It defaults to 2.
75 # The variable nogettext can be set to 1 to suppress gettext markers.
77 # The variable prefix can be used to prepend a string to each message.
79 # The variable namespace can be used to prepend a string to each
80 # variable and macro name.
82 BEGIN {
83 FS = "[\t]+";
84 # cpos holds the current position in the message string.
85 cpos = 0;
86 # msg holds the number of messages.
87 msg = 0;
88 print "/* Output of mkstrtable.awk. DO NOT EDIT. */";
89 print "";
90 header = 1;
91 if (textidx == 0)
92 textidx = 2;
93 # nogettext can be set to 1 to suppress gettext noop markers.
96 /^#/ { next; }
98 header {
99 if ($1 ~ /^[0123456789]+$/)
101 print "/* The purpose of this complex string table is to produce";
102 print " optimal code with a minimum of relocations. */";
103 print "";
104 print "static const char " namespace "msgstr[] = ";
105 header = 0;
107 else
108 print;
111 !header {
112 sub (/\#.+/, "");
113 sub (/[ ]+$/, ""); # Strip trailing space and tab characters.
115 if (/^$/)
116 next;
118 # Print the string msgstr line by line. We delay output by one line to be able
119 # to treat the last line differently (see END).
120 if (last_msgstr)
122 if (nogettext)
123 print " \"" last_msgstr "\" \"\\0\"";
124 else
125 print " gettext_noop (\"" last_msgstr "\") \"\\0\"";
127 last_msgstr = prefix $textidx;
129 # Remember the error code and msgidx of each error message.
130 code[msg] = $1;
131 pos[msg] = cpos;
132 cpos += length (last_msgstr) + 1;
133 msg++;
135 if ($1 == "")
137 has_default = 1;
138 exit;
141 END {
142 if (has_default)
143 coded_msgs = msg - 1;
144 else
145 coded_msgs = msg;
147 if (nogettext)
148 print " \"" prefix last_msgstr "\";";
149 else
150 print " gettext_noop (\"" prefix last_msgstr "\");";
151 print "";
152 print "static const int " namespace "msgidx[] =";
153 print " {";
154 for (i = 0; i < coded_msgs; i++)
155 print " " pos[i] ",";
156 print " " pos[coded_msgs];
157 print " };";
158 print "";
159 print "#define " namespace "msgidxof(code) (0 ? -1 \\";
161 # Gather the ranges.
162 skip = code[0];
163 start = code[0];
164 stop = code[0];
165 for (i = 1; i < coded_msgs; i++)
167 if (code[i] == stop + 1)
168 stop++;
169 else
171 print " : ((code >= " start ") && (code <= " stop ")) ? (code - " \
172 skip ") \\";
173 skip += code[i] - stop - 1;
174 start = code[i];
175 stop = code[i];
178 print " : ((code >= " start ") && (code <= " stop ")) ? (code - " \
179 skip ") \\";
180 if (has_default)
181 print " : " stop + 1 " - " skip ")";
182 else
183 print " : -1)";