Debian Codegraph @

encodings/make-tables

raw
1 #!/usr/bin/perl -w
2 # $XTermId: make-tables,v 1.8 2025/09/12 20:09:37 tom Exp $
3 # -----------------------------------------------------------------------------
4 # Copyright 2013,2025 by Thomas E. Dickey
5 #
6 # All Rights Reserved
7 #
8 # Permission to use, copy, modify, and distribute this software and its
9 # documentation for any purpose and without fee is hereby granted,
10 # provided that the above copyright notice appear in all copies and that
11 # both that copyright notice and this permission notice appear in
12 # supporting documentation, and that the name of the above listed
13 # copyright holder(s) not be used in advertising or publicity pertaining
14 # to distribution of the software without specific, written prior
15 # permission.
16 #
17 # THE ABOVE LISTED COPYRIGHT HOLDER(S) DISCLAIM ALL WARRANTIES WITH REGARD
18 # TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
19 # AND FITNESS, IN NO EVENT SHALL THE ABOVE LISTED COPYRIGHT HOLDER(S) BE
20 # LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
21 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
22 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
23 # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
24 # -----------------------------------------------------------------------------
25 # Convert ".enc" files into C code, using that as a fallback when iconv lacks
26 # information for a given encoding.
27
28 our @table_names;
29 our $indent = " ";
30 our $starts = "/* *INDENT-OFF* */";
31
32 sub trim_enc($) {
33 my $name = $_[0];
34 $name =~ s/\.enc$//;
35 return $name;
36 }
37
38 sub table_name($) {
39 my $name = &trim_enc( $_[0] );
40 $name =~ s/[-.]/_/g;
41 return $name;
42 }
43
44 sub do_file($) {
45 my $name = $_[0];
46 my $n = $#table_names + 1;
47 $table_names[ $n++ ] = $name;
48 my $table = &table_name($name);
49 printf <<EOF;
50 $starts
51 static const BuiltInMapping $table\[\] =
52 {
53 EOF
54 open( FP, $name ) || die "cannot open $name";
55 my (@input) = <FP>;
56 close(FP);
57 $starts = "";
58
59 my $ignore = 1;
60 my $comment;
61 my $source;
62 my $target;
63 my $converted = 0;
64 for $n ( 0 .. $#input ) {
65 chomp $input[$n];
66 if ( $input[$n] =~ /startmapping\s+unicode/i ) {
67 $ignore = 0;
68 }
69 elsif ( $input[$n] =~ /endmapping\s+unicode/i ) {
70 $ignore = 1;
71 }
72 my $comment = $input[$n];
73 if ( $comment =~ /#/ ) {
74 $comment =~ s/^[^#]*#\s*//;
75 }
76 else {
77 $comment = "";
78 }
79 if ($ignore) {
80 printf "%s/* %s */\n", $indent, $comment if ( $comment ne "" );
81 }
82 elsif ( $input[$n] =~ /^\s*0x[[:xdigit:]]+/i ) {
83 $source = $input[$n];
84 $source =~ s/#.*//;
85 $source =~ s/^\s+//;
86 $target = $source;
87 $source =~ s/\s+.*//;
88 $target =~ s/^[^[:blank:]]+\s+//;
89 $target =~ s/\s+.*//;
90 if ( $comment ne "" ) {
91 printf "%s{%s, %s},\t/* %s */\n", $indent, $source, $target,
92 $comment;
93 }
94 else {
95 printf "%s{%s, %s},\n", $indent, $source, $target;
96 }
97 $converted++;
98 }
99 }
100 if ( $converted == 0 ) {
101 printf "%s{0, 0},\t/* empty table is illegal syntax */\n", $indent;
102 }
103
104 printf <<EOF;
105 };
106 EOF
107 }
108
109 sub begin_file() {
110 printf <<EOF;
111 /*
112 * \$XTermId\$
113 * This file was generated by $0
114 */
115 #include <other.h>
116 #include <sys.h>
117 #include <luitconv.h>
118 EOF
119 }
120
121 sub quoted($) {
122 my $value = shift;
123 return sprintf( "\"%s\",", $value );
124 }
125
126 sub end_file() {
127 my $name;
128 printf <<EOF;
129
130 #define DATA(name) name, SizeOf(name)
131 const BuiltInCharsetRec builtin_encodings[] =
132 {
133 EOF
134 for $name ( sort @table_names ) {
135 printf "%s{ %-17s DATA(%s) },\n", $indent, &quoted( &trim_enc($name) ),
136 &table_name($name);
137 }
138
139 printf <<EOF;
140 ${indent}{ NULL, NULL, 0 }
141 };
142 /* *INDENT-ON* */
143 EOF
144 }
145
146 &begin_file;
147 while ( $#ARGV >= 0 ) {
148 &do_file( shift @ARGV );
149 }
150 &end_file;