Debian Codegraph @

unicode/convmap.pl

raw
1 #!/usr/bin/env perl
2 # $XTermId: convmap.pl,v 1.21 2025/03/30 15:50:57 tom Exp $
3 # -----------------------------------------------------------------------------
4 # this file is part of xterm
5 #
6 # Copyright 2007-2018,2025 by Thomas E. Dickey
7 #
8 # All Rights Reserved
9 #
10 # Permission is hereby granted, free of charge, to any person obtaining a
11 # copy of this software and associated documentation files (the
12 # "Software"), to deal in the Software without restriction, including
13 # without limitation the rights to use, copy, modify, merge, publish,
14 # distribute, sublicense, and/or sell copies of the Software, and to
15 # permit persons to whom the Software is furnished to do so, subject to
16 # the following conditions:
17 #
18 # The above copyright notice and this permission notice shall be included
19 # in all copies or substantial portions of the Software.
20 #
21 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22 # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
24 # IN NO EVENT SHALL THE ABOVE LISTED COPYRIGHT HOLDER(S) BE LIABLE FOR ANY
25 # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
26 # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
27 # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 #
29 # Except as contained in this notice, the name(s) of the above copyright
30 # holders shall not be used in advertising or otherwise to promote the
31 # sale, use or other dealings in this Software without prior written
32 # authorization.
33 # -----------------------------------------------------------------------------
34 # Generate keysym2ucs.c file
35 #
36 # See also:
37 # http://mail.nl.linux.org/linux-utf8/2001-04/msg00248.html
38 #
39 # $XFree86: xc/programs/xterm/unicode/convmap.pl,v 1.5 2000/01/24 22:22:05 dawes Exp $
40
41 use strict;
42 use warnings;
43
44 our $keysym;
45 our %name;
46 our %keysym_custom;
47 our %keysym_to_ucs;
48 our %keysym_to_keysymname;
49
50 our ($opt_v);
51
52 sub utf8 ($);
53
54 sub utf8 ($) {
55 my $c = shift(@_);
56
57 if ( $c < 0x80 ) {
58 return sprintf( "%c", $c );
59 }
60 elsif ( $c < 0x800 ) {
61 return sprintf( "%c%c", 0xc0 | ( $c >> 6 ), 0x80 | ( $c & 0x3f ) );
62 }
63 elsif ( $c < 0x10000 ) {
64 return sprintf( "%c%c%c",
65 0xe0 | ( $c >> 12 ),
66 0x80 | ( ( $c >> 6 ) & 0x3f ),
67 0x80 | ( $c & 0x3f ) );
68 }
69 elsif ( $c < 0x200000 ) {
70 return sprintf( "%c%c%c%c",
71 0xf0 | ( $c >> 18 ),
72 0x80 | ( ( $c >> 12 ) & 0x3f ),
73 0x80 | ( ( $c >> 6 ) & 0x3f ),
74 0x80 | ( $c & 0x3f ) );
75 }
76 elsif ( $c < 0x4000000 ) {
77 return sprintf( "%c%c%c%c%c",
78 0xf8 | ( $c >> 24 ),
79 0x80 | ( ( $c >> 18 ) & 0x3f ),
80 0x80 | ( ( $c >> 12 ) & 0x3f ),
81 0x80 | ( ( $c >> 6 ) & 0x3f ),
82 0x80 | ( $c & 0x3f ) );
83
84 }
85 elsif ( $c < 0x80000000 ) {
86 return sprintf( "%c%c%c%c%c%c",
87 0xfe | ( $c >> 30 ),
88 0x80 | ( ( $c >> 24 ) & 0x3f ),
89 0x80 | ( ( $c >> 18 ) & 0x3f ),
90 0x80 | ( ( $c >> 12 ) & 0x3f ),
91 0x80 | ( ( $c >> 6 ) & 0x3f ),
92 0x80 | ( $c & 0x3f ) );
93 }
94 else {
95 return utf8(0xfffd);
96 }
97 }
98
99 sub is_technical($) {
100 my $keysym = shift;
101 return ( $keysym >= 0x8b0 and $keysym < 0x8bf );
102 }
103
104 sub non_unicode ($) {
105 my $keysym = shift;
106 my $rc = 0;
107 if ( $keysym ne "" ) {
108 if ( defined( $name{$keysym} ) and index( $name{$keysym}, "PUA" ) == 0 )
109 {
110 $rc = 1;
111 }
112 elsif ( defined( $keysym_to_ucs{$keysym} ) or $keysym >= 0x10000000 ) {
113 $rc = private_use($keysym);
114 }
115 elsif ( is_technical($keysym)
116 or ( $keysym >= 0xfd00 and $keysym <= 0xffff ) )
117 {
118 $rc = 1;
119 }
120 }
121 return $rc;
122 }
123
124 sub private_use($) {
125 my $keysym = shift;
126 my $result = $keysym;
127 if ( is_technical($keysym) ) {
128 $result = $result - 0x8b1 + 0xeeee;
129 }
130 elsif ( $keysym > 0xfd00 and $keysym <= 0xffff ) {
131 $result = $result - 0x1d00;
132 }
133 elsif ( $keysym > 0x10000000 and $keysym <= 0x100000ff ) {
134 $result = $result - 0x10000000;
135 }
136 elsif ( $keysym > 0x1000f000 and $keysym <= 0x1000ffff ) {
137 $result = $result - 0x1000f000 + 0xf0000;
138 }
139 elsif ( $keysym > 0x1004f000 and $keysym <= 0x1004ffff ) {
140 $result = $result - 0x1004f000 + 0xf1000;
141 }
142 elsif ( $keysym > 0x1005f000 and $keysym <= 0x1005ffff ) {
143 $result = $result - 0x1005f000 + 0xf2000;
144 }
145 elsif ( $keysym > 0x10081000 and $keysym <= 0x10081fff ) {
146 $result = $result - 0x10081000 + 0xf4000;
147 }
148 elsif ( $keysym > 0x1008fe00 and $keysym <= 0x1008ffff ) {
149 $result = $result - 0x1008fe00 + 0xf6000;
150 }
151 return $result;
152 }
153
154 my $unicodedata = "UnicodeData.txt";
155
156 # read list of all Unicode names
157 if ( !open( UDATA, $unicodedata ) && !open( UDATA, "$unicodedata" ) ) {
158 die( "Can't open Unicode database '$unicodedata':\n$!\n\n"
159 . "Please make sure that you have downloaded the file\n"
160 . "ftp://ftp.unicode.org/Public/UNIDATA/UnicodeData.txt\n" );
161 }
162 while (<UDATA>) {
163 if (
164 /^([0-9,A-F]{4,6});
165 ([^;]*);([^;]*);
166 ([^;]*);([^;]*);
167 ([^;]*);([^;]*);
168 ([^;]*);([^;]*);
169 ([^;]*);([^;]*);
170 ([^;]*);([^;]*);
171 ([^;]*);([^;]*)$/x
172 )
173 {
174 $name{ hex($1) } = $2;
175 }
176 else {
177 die("Syntax error in line '$_' in file '$unicodedata'");
178 }
179 }
180 close(UDATA);
181
182 # read mapping (from http://wsinwp07.win.tue.nl:1234/unicode/keysym.map)
183 our $keysym_custom = "keysym.map";
184 our $lineno = 0;
185 open( LIST, "<$keysym_custom" ) || die("Can't open map file:\n$!\n");
186 while (<LIST>) {
187 ++$lineno;
188 if (/^0x([0-9a-f]{4})\s+U([0-9a-f]{4})\s*(\#.*)?$/) {
189 my $keysym = hex($1);
190 my $ucs = hex($2);
191 my $comment = $3;
192 $comment =~ s/^#\s*//;
193 $keysym_custom{$keysym} = "$keysym_custom:$lineno"
194 unless defined $keysym_custom{$keysym};
195 $keysym_to_ucs{$keysym} = $ucs;
196 $keysym_to_keysymname{$keysym} = $comment;
197 }
198 elsif ( /^\s*\#/ || /^\s*$/ ) {
199 }
200 else {
201 die("Syntax error in 'list' in line\n$_\n");
202 }
203 }
204 close(LIST);
205
206 # read entries in keysymdef.h
207 sub open_keysyms($) {
208 return open( LIST, "</usr/include/X11/" . shift );
209 }
210
211 die("Can't open keysymdef.h:\n$!\n") unless open_keysyms("keysymdef.h");
212
213 sub add_keysyms($) {
214 my $extra = shift;
215 while (<LIST>) {
216 next if (/^.*\b(Alias\s+for|Same\s+as)\b.*$/i);
217
218 # translate Linux evdev's
219 if (/^\#define.*\b_EVDEVK\(0x[0-9a-fA-Z]+\)/) {
220 my $param = substr( $_, index( $_, "_EVDEVK" ) );
221 chomp $param;
222 $param =~ s/\).*$/)/;
223 my $value = $param;
224 $value =~ s/^.*\((\w+)\)/$1/;
225 my $l = substr( $_, 0, index( $_, $param ) );
226 my $r = substr( $_, index( $_, $param ) + length($param) );
227 my $m = sprintf( "%#x", hex($value) + 268963840 );
228 $_ = sprintf( "%s%s%s", $l, $m, $r );
229 }
230 if (
231 /^\#define\s+
232 ${extra}XK_([A-Za-z_0-9]+)\s+
233 0x([0-9a-fA-F]+)\s*(\/.*)?$/x
234 )
235 {
236 next if /\/\* deprecated \*\//;
237 printf STDERR "LIST %s", $_ if ($opt_v);
238 my $keysymname = $1;
239 my $keysym = hex($2);
240 $keysym_to_keysymname{$keysym} = $keysymname;
241
242 my $comment = $_;
243 chomp $comment;
244 my $check = $comment;
245 $comment =~ s%^.*/\*\s*(.*)\s*\*/.*$%$1%;
246 $comment =~ s%\(([^)]*)\)%$1%g;
247 $comment = "" if ( $comment eq $check );
248
249 next if $keysym_to_ucs{$keysym};
250 if ( $comment ne "" ) {
251 printf STDERR "->%s\n", $comment if ($opt_v);
252 my $expect = "";
253 $expect = $keysym_to_ucs{$keysym}
254 if defined $keysym_to_ucs{$keysym};
255 my $actual = "";
256
257 my $explain = $comment;
258 if ( $comment =~ /^U\+[[:xdigit:]]{4,}.*/ ) {
259 $explain =~ s/^[^\s]+\s+//;
260 $explain =~ s/\s+$//;
261 $comment =~ s/\s.*//;
262 $actual = hex( substr( $comment, 2 ) );
263 $expect = $keysym - 0x1000000 if ( $keysym >= 0x1000000 );
264 if ( defined( $keysym_custom{$keysym} )
265 and $actual eq $keysym_to_ucs{$keysym} )
266 {
267 printf STDERR "%s: duplicates $_",
268 $keysym_custom{$keysym};
269 }
270
271 }
272 if ( $actual eq "" and $expect eq "" ) {
273 my $ucs = private_use($keysym);
274 $name{$ucs} = "PUA $comment";
275 $keysym_to_ucs{$keysym} = $ucs;
276 }
277 elsif ( $actual eq $expect ) {
278 printf STDERR "OK %#06x -> U+%04X\n", $keysym, $actual
279 if ( $opt_v and $actual ne "" );
280 }
281 elsif ( $actual eq "" ) {
282 printf STDERR "ERR $comment -> expect:U+%04X\n", $expect
283 unless non_unicode($keysym);
284 }
285 elsif ( $expect eq "" ) {
286 $name{$actual} = "$explain";
287 $keysym_to_ucs{$keysym} = $actual;
288 }
289 else {
290 printf STDERR "ERR $comment -> actual:%#x, expect:U+%04X\n",
291 $actual, $expect;
292 }
293 }
294 elsif (
295 non_unicode($keysym)
296 and
297 ( not defined $keysym_to_ucs{$keysym} or is_technical($keysym) )
298 )
299 {
300 my $ucs = private_use($keysym);
301 $name{$ucs} = "PUA" unless defined $name{$ucs};
302 $keysym_to_ucs{$keysym} = $ucs;
303 }
304 }
305 }
306 close(LIST);
307 }
308
309 add_keysyms("");
310 add_keysyms("D") if open_keysyms("DECkeysym.h");
311 add_keysyms("") if open_keysyms("HPkeysym.h");
312 add_keysyms("ap") if open_keysyms("ap_keysym.h");
313 add_keysyms("hp") if open_keysyms("HPkeysym.h");
314 add_keysyms("osf") if open_keysyms("HPkeysym.h");
315 add_keysyms("Sun") if open_keysyms("Sunkeysym.h");
316 add_keysyms("XF86") if open_keysyms("XF86keysym.h");
317
318 print <<EOT;
319 /* \$XTermId\$
320 * ----------------------------------------------------------------------------
321 * this file is part of xterm
322 *
323 * Copyright 2007-2018,2025 by Thomas E. Dickey
324 *
325 * All Rights Reserved
326 *
327 * Permission is hereby granted, free of charge, to any person obtaining a
328 * copy of this software and associated documentation files (the
329 * "Software"), to deal in the Software without restriction, including
330 * without limitation the rights to use, copy, modify, merge, publish,
331 * distribute, sublicense, and/or sell copies of the Software, and to
332 * permit persons to whom the Software is furnished to do so, subject to
333 * the following conditions:
334 *
335 * The above copyright notice and this permission notice shall be included
336 * in all copies or substantial portions of the Software.
337 *
338 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
339 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
340 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
341 * IN NO EVENT SHALL THE ABOVE LISTED COPYRIGHT HOLDER(S) BE LIABLE FOR ANY
342 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
343 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
344 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
345 *
346 * Except as contained in this notice, the name(s) of the above copyright
347 * holders shall not be used in advertising or otherwise to promote the
348 * sale, use or other dealings in this Software without prior written
349 * authorization.
350 * ----------------------------------------------------------------------------
351 * Note:
352 * ----
353 * This file has been updated and revised to provide for mapping all keysyms
354 * to UCS. The BMP private use area is used for keysyms which do not map to
355 * characters.
356 *
357 * Original header:
358 * ---------------
359 * This module converts keysym values into the corresponding ISO 10646
360 * (UCS, Unicode) values.
361 *
362 * The array keysymtab[] contains pairs of X11 keysym values for graphical
363 * characters and the corresponding Unicode value. The function
364 * keysym2ucs() maps a keysym onto a Unicode value using a binary search,
365 * therefore keysymtab[] must remain SORTED by keysym value.
366 *
367 * The keysym -> UTF-8 conversion will hopefully one day be provided
368 * by Xlib via XmbLookupString() and should ideally not have to be
369 * done in X applications. But we are not there yet.
370 *
371 * We allow to represent any UCS character in the range U-00000000 to
372 * U-00FFFFFF by a keysym value in the range 0x01000000 to 0x01ffffff.
373 * This admittedly does not cover the entire 31-bit space of UCS, but
374 * it does cover all of the characters up to U-10FFFF, which can be
375 * represented by UTF-16, and more, and it is very unlikely that higher
376 * UCS codes will ever be assigned by ISO. So to get Unicode character
377 * U+ABCD you can directly use keysym 0x0100abcd.
378 *
379 * NOTE: The comments in the table below contain the actual character
380 * encoded in UTF-8, so for viewing and editing best use an editor in
381 * UTF-8 mode.
382 *
383 * Author: Markus G. Kuhn <mkuhn\@acm.org>, University of Cambridge, April 2001
384 *
385 * Special thanks to Richard Verhoeven <river\@win.tue.nl> for preparing
386 * an initial draft of the mapping table.
387 *
388 * This software is in the public domain. Share and enjoy!
389 *
390 * AUTOMATICALLY GENERATED FILE, DO NOT EDIT !!! (unicode/convmap.pl)
391 */
392
393 #include <keysym2ucs.h>
394
395 static struct codepair {
396 unsigned keysym;
397 unsigned ucs;
398 } keysymtab[] = {
399 /* *INDENT-OFF* */
400 EOT
401
402 my %pua2;
403 for $keysym ( sort { $a <=> $b } keys(%keysym_to_keysymname) ) {
404 next
405 if ( $keysym > 0xffff and $keysym < 0x10000000 )
406 or ( $keysym < 0x100 );
407 my $ucs = $keysym_to_ucs{$keysym};
408 if ($ucs) {
409 if ( $keysym >= 0x10000000 ) {
410 if ( defined $pua2{$ucs} ) {
411 printf "/* duplicate %#x vs %#x */\n", $keysym, $pua2{$ucs};
412 }
413 else {
414 $pua2{$ucs} = $keysym;
415 }
416 }
417 my $pua = 0;
418 $pua = 1 if index( $name{$ucs}, "PUA" ) == 0;
419 printf(
420 "%s{ 0x%04x, 0x%04x }, %s*%*s %s %s */\n",
421 $pua ? "/*" : " ",
422 $keysym,
423 $ucs,
424 $pua ? "*" : "/",
425 $keysym > 0xffff ? 24 : 29,
426 $keysym_to_keysymname{$keysym},
427 $pua ? "?"
428 : $ucs < 32 ? ( "^" . chr( 64 + $ucs ) )
429 : utf8($ucs),
430 defined( $name{$ucs} ) ? $name{$ucs} : "???"
431 );
432 }
433 else {
434 printf(
435 "/* 0x%04x %*s ? ??? */\n",
436 $keysym,
437 $keysym > 0xffff ? 35 : 39,
438 $keysym_to_keysymname{$keysym}
439 );
440 }
441 }
442
443 print <<EOT;
444 /* *INDENT-ON* */
445 };
446
447 long
448 keysym2ucs(KeySym keysym)
449 {
450 long result = -1; /* no matching Unicode value found */
451 int min = 0;
452 int max = sizeof(keysymtab) / sizeof(struct codepair) - 1;
453
454 if ((keysym >= 0x0020 && keysym <= 0x007e) ||
455 (keysym >= 0x00a0 && keysym <= 0x00ff)) {
456 /* found Latin-1 characters (1:1 mapping) */
457 result = (long) keysym;
458 } else if ((keysym & 0xff000000) == 0x01000000) {
459 /* found directly encoded 24-bit UCS characters */
460 result = (long) (keysym & 0x00ffffff);
461 } else if (keysym >= 0x08b0 && keysym <= 0x08b7) {
462 result = (long) keysym - 0x8b1 + 0xeeee;
463 } else {
464 /* binary search in table */
465 while (max >= min) {
466 int mid = (min + max) / 2;
467 if (keysymtab[mid].keysym < keysym) {
468 min = mid + 1;
469 } else if (keysymtab[mid].keysym > keysym) {
470 max = mid - 1;
471 } else {
472 /* found it in table */
473 result = keysymtab[mid].ucs;
474 break;
475 }
476 }
477 if (result == -1) {
478 long value = (long) keysym;
479 if (keysym >= 0xfd00 && keysym <= 0xffff) {
480 result = value - 0x1d00;
481 } else if (keysym >= 0x10000000 && keysym <= 0x100000ff) {
482 result = value - 0x10000000;
483 } else if (keysym >= 0x1000f000 && keysym <= 0x1000ffff) {
484 result = value - 0x1000f000 + 0xf0000;
485 } else if (keysym >= 0x1004f000 && keysym <= 0x1004ffff) {
486 result = value - 0x1004f000 + 0xf1000;
487 } else if (keysym >= 0x1005f000 && keysym <= 0x1005ffff) {
488 result = value - 0x1005f000 + 0xf2000;
489 } else if (keysym >= 0x10081000 && keysym <= 0x10081fff) {
490 result = value - 0x10081000 + 0xf4000;
491 } else if (keysym >= 0x1008fe00 && keysym <= 0x1008ffff) {
492 result = value - 0x1008fe00 + 0xf6000;
493 }
494 }
495 }
496
497 return result;
498 }
499 EOT