File: charset.pl

package info (click to toggle)
recode 3.4.1-11
  • links: PTS
  • area: main
  • in suites: slink
  • size: 1,560 kB
  • ctags: 622
  • sloc: ansic: 10,572; perl: 339; makefile: 317; lisp: 243; sh: 173; lex: 165; awk: 127; sed: 10
file content (314 lines) | stat: -rw-r--r-- 6,888 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
# Automatically derive charset.c from rfc1345.txt.
# Copyright (C) 1993, 1994 Free Software Foundation, Inc.
# Francois Pinard <pinard@iro.umontreal.ca>, 1993.

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.

# This program is distributed in the hope that it will be useful,
# 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
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

$overall_header = <<END_OF_TEXT;
/* DO NOT MODIFY THIS FILE!  It was generated by "charset.pl".  */

/* Conversion of files between different charsets and usages.
   Copyright (C) 1990, 1993 Free Software Foundation, Inc.
   Francois Pinard <pinard\@iro.umontreal.ca>, 1993.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful, 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
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "recode.h"
END_OF_TEXT

$split_point = 80;

$charset_ordinal = 0;
$discard_charset = 0;
$alias_count = 0;

$header = "";

while (<>)
{

    # Recognize `&charset'.

    if (/^&charset (.*)/)
    {

	# Before beginning a new charset, process the previous one.

	$next_charset = $1;

	&complete_charset;

	$charset = $next_charset;

	# Save the charset name for further declaration.  Announce
 	# this charset in the array initialization section, and
 	# initialize its processing.

	print STDERR $charset, "...";

	$header = "\n/* $charset\n";

	$hashname = $charset;
	$hashname =~ tr/A-Z/a-z/;
	$hashname =~ s/[^a-z0-9]//g;
	if ($used{$hashname})
	{
	    printf STDERR " duplicate of %s...", $used{$hashname};
	    $discard_charset = 1;
	    next;
	}
	$used{$hashname} = $charset;

	$alias_count = 0;
	@table = ();
	$codedim = 0;
	$code = 0;
	$list = "";
	$body = "";
	next;
    }

    # Ignore discarded charsets, and character set escapes.

    next if $discard_charset;
    next if /^&g[0-4]esc/;

    # Recognize other `&' directives.

    if (/^&rem (.*)/)
    {

	# Save C comments for Texinfo.

	$body .= "$1\n";
	next;
    }
    if (/^&alias (.*)/)
    {

	# Save synonymous charset names for later declarations.

	$alias = $1;
	$header .= "   $alias\n";

	$hashname = $alias;
	$hashname =~ tr/A-Z/a-z/;
	$hashname =~ s/[^a-z0-9]//g;
	if ($used{$hashname} && $used{$hashname} ne $charset)
	{
	    printf STDERR " duplicate of %s...", $used{$hashname};
	    next;
	}
	$used{$hashname} = $charset;

	$list .= "," if $list;
	$list .= $alias;
	push (@declare_alias, "$alias,$charset");
	$alias_count++;
	next;
    }
    if (/^&code (.*)/)
    {

	# Save the code position.

	$code = $1;
	next;
    }
    if (/^&duplicate/)
    {

	# Ignore duplicates for now.

	next;
    }
    if (/^&([^ ]+)/)
    {

	# This is an unrecognized & line, discard the charset.

	print STDERR " &$1...";
	$discard_charset = 1;
	next;
    }

    # Save all other tokens into the double table.

    foreach $token (split)
    {
	if ($token ne "??" && $token ne "__")
	{
	    $table[$code] = $token;
	    if (length ($token) > $codedim)
	    {
		$codedim = length ($token);
	    }
	}
	$code++;
    }
}

# Process the last accumulated charset.

&complete_charset;
&complete_file (2);

# Print the documentation.

open (TEXI, ">charset.texi");
for $charset (sort keys %body)
{
    print TEXI "\n\@item $charset\n";
    @list = sort (split (/,/, $list{$charset}));
    if (@list == 1)
    {
	print TEXI "\@code{", $list[0], "} is an alias for this charset.\n";
    }
    elsif (@list > 0)
    {
	$string = "\@code{" . join ("}, \@code{", @list) . "}";
	$string =~ s/,([^,]+)$/ and\1/;
	print TEXI $string, " are aliases for this charset. \n";
    }
    print TEXI $body{$charset};
}
close TEXI;

exit 0;

# Routine for printing all accumulated initialization information, and
# then closing the file.  Argument is 1 or 2 depending on output file.

sub complete_file
{

    # Print the collectable initialization function.

    print SRC "\n";
    print SRC "void\n";
    printf SRC "module_charset%d (void)\n", $_[0];
    print SRC "{\n";
    $counter = $_[0] == 1 ? 0 : $split_point;
    while ($string = shift @declare_charset)
    {
	$string =~ s/(.*),/"\1", /;
	print SRC "  declare_double_step (&table_$counter, $string);\n";
	$counter++;
    }
    print SRC "\n";
    while ($string = shift @declare_alias)
    {
	$string =~ s/,/", "/;
	print SRC "  declare_alias (\"$string\");\n";
    }
    print SRC "}\n";
    close SRC;
}

# Routine for printing all accumulated information for the charset.
# If the charset should be discarded, adjust tables.

sub complete_charset
{
    if ($discard_charset)
    {
	while ($alias_count-- > 0)
	{
	    pop @declare_alias;
	}
	$discard_charset = 0;
	print STDERR " DISCARDED\n";
    }
    elsif ($header)
    {

	# Save the documentation.

	$list{$charset} = $list;
	$body{$charset} = $body;

	# Open a new source file if needed.

	if ($charset_ordinal == 0)
	{
	    print STDERR " writing on charset1.c...";
	    open (SRC, ">charset1.c");
	    print SRC $overall_header;
	}
	elsif ($charset_ordinal == $split_point)
	{
	    &complete_file (1);
	    
	    print STDERR " writing on charset2.c...";
	    open (SRC, ">charset2.c");
	    print SRC $overall_header;
	}

	# Make introductory C comments.

	print SRC $header;
	print SRC "*/\n";

	# Make the table for this charset.

	print SRC "\n";
	print SRC "static DOUBLE_TABLE table_$charset_ordinal = \n";
	print SRC "  {\n";
	$code = 0;
	for ($index1 = 0; $index1 < 8; $index1++)
	{
	    $header = "";
	    $flag = 0;
	    for ($index2 = 0; $index2 < 32; $index2++)
	    {
		$token = $table[$code++];
		$flag = 1 if $token;
		$token .= " " while length ($token) < $codedim;
		$header .= $token;
	    }
	    if ($flag)
	    {
		$header =~ s/\"/\\\"/g;
		print SRC "    \"", $header, "\",\n";
	    }
	    else
	    {
		print SRC "    NULL,\n";
	    }
	}
	print SRC "  };\n";

	# Register the table.

	push (@declare_charset, "$charset,$codedim");

	print STDERR " done\n";
	$charset_ordinal++;
    }
    $header = "";
}