2 # Copyright (C) 2020 Free Software Foundation
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (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
12 # GNU 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/>.
23 $0: generate code page tables from ICU encoding list
24 usage: $0 < convrtrs.txt > sys-file-encoding.c
26 To regenerate the encoding data, get the latest ICU encoding data from:
27 http://source.icu-project.org/repos/icu/icu/trunk/source/data/mappings/convrtrs.txt
28 then convert it with this script using the command above.
30 exit (@ARGV && $ARGV[0] eq '--help' ? 0 : 1);
33 open (CONVERTERS, '<', 'convrtrs.txt')
34 or die "convrtrs.txt: open failed ($!)\n";
36 our $WINDOWS = 3; # Windows code pages.
37 our $IBM = 2; # IBM code pages.
38 our $CP = 1; # Java (?) code pages.
39 our %sources = ($WINDOWS => "windows", $IBM => "ibm", $CP => "cp");
42 while (<CONVERTERS>) {
48 process_converter ($converter);
52 process_converter ($converter);
58 /* -*- mode: c; buffer-read-only: t -*-
60 Generated by sys-file-encoding.pl. Do not modify!
65 #include "data/sys-file-private.h"
67 struct sys_encoding sys_codepage_number_to_name[] = {
69 for my $cpnumber (sort { $a <=> $b } (keys (%codepages))) {
70 my $source = max (keys (%{$codepages{$cpnumber}}));
71 my $name = ${$codepages{$cpnumber}{$source}}[0];
72 print " { $cpnumber, \"$name\" },\n";
74 print " { 0, NULL }\n";
78 for my $cpnumber (sort { $a <=> $b } (keys (%codepages))) {
79 for my $source (keys (%{$codepages{$cpnumber}})) {
80 for my $name (@{$codepages{$cpnumber}{$source}}) {
81 push(@{$names{$name}{$source}}, $cpnumber);
85 print "struct sys_encoding sys_codepage_name_to_number[] = {\n";
86 for my $name (sort (keys (%names))) {
87 for my $source (reverse (sort (keys (%sources)))) {
88 next if !exists ($names{$name}{$source});
89 my (@numbers) = @{$names{$name}{$source}};
91 # The only two encodings that currently print this are KSC_5601
92 # and KS_C_5601-1987, for code pages 949 and 51949. It looks to
93 # me like the correct code page number is 949, which is the one
94 # chosen (because the numbers are in sorted order).
95 print " /* $name has multiple numbers for $sources{$source}: @numbers */\n"
98 print " { $numbers[0], \"$name\" },\n";
102 print " { 0, NULL }\n";
105 sub process_converter {
106 my ($converter) = @_;
107 return if $converter =~ /^\s*$/;
108 return if $converter =~ /^\s*\{/;
114 my @fields = split (' ', $converter);
116 my $name = shift (@fields);
117 if (@fields && $fields[0] eq '{') {
122 my $standard = shift (@fields);
123 last if $standard eq '}';
124 $standards{$standard} = 1;
126 if (exists $standards{'IANA*'}) {
127 unshift (@iana, $name);
128 } elsif (exists $standards{'IANA'}) {
130 } elsif (grep (/\*$/, keys %standards)) {
131 unshift (@other, $name);
133 push (@other, $name);
136 # Untagged names are completely nonstandard.
141 if (($number) = $name =~ /^cp([0-9]+)$/) {
142 $cps{$CP} = int ($number);
143 } elsif (($number) = $name =~ /^windows-([0-9]+)$/) {
144 $cps{$WINDOWS} = int ($number);
145 } elsif (($number) = $name =~ /^ibm-([0-9]+)$/) {
146 $cps{$IBM} = int ($number);
152 # If there are no tagged names then this is completely nonstandard.
153 return if !@iana && !@other;
155 $codepages{$cps{$_}}{$_} = [@iana, @other] for keys (%cps);
161 $best = $x if !defined ($best) || $x > $best;