Merge from gnulib
[emacs.git] / admin / charsets / mapconv
blob5a72fbd6c79488ffcede34268f1202637e1e3419
1 #!/bin/sh
3 # Copyright (C) 2015-2016 Free Software Foundation, Inc.
5 # Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
6 # National Institute of Advanced Industrial Science and Technology (AIST)
7 # Registration Number H13PRO009
9 # This file is part of GNU Emacs.
11 # GNU Emacs is free software: you can redistribute it and/or modify
12 # it under the terms of the GNU General Public License as published by
13 # the Free Software Foundation, either version 3 of the License, or
14 # (at your option) any later version.
16 # GNU Emacs is distributed in the hope that it will be useful,
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 # GNU General Public License for more details.
21 # You should have received a copy of the GNU General Public License
22 # along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 # Commentary:
26 # Convert charset map of various format into this:
27 # 0xXX 0xYYYY
28 # where,
29 # XX is a code point of the charset in hexa-decimal,
30 # YYYY is the corresponding Unicode character code in hexa-decimal.
31 # Arguments are:
32 # $1: source map file
33 # $2: address pattern for sed (optionally with substitution command)
34 # $3: format of source map file
35 # GLIBC-1 GLIBC-2 GLIBC-2-7 CZYBORRA IANA UNICODE UNICODE2 YASUOKA
36 # $4: awk script
38 ## So that eg [A-F] as used by KANJI-DATABASE branch below works as expected.
39 ## Otherwise with LANG=en_US.utf8, CNS-6.map was generated with a
40 ## bogus entry. By experiment, LC_COLLATE=C was not enough.
41 export LC_ALL=C
43 BASE=`expr "$1" : '.*/\(.*\)' '|' "$1"` # basename
44 FILE="admin/charsets/mapfiles/$BASE"
45 BASE=`expr "$BASE" : '\(.*\)\.gz$' '|' "$BASE"` # remove any .gz suffix
46 AWK=${AWK:-awk}
48 case "$3" in
49 GLIBC*)
50 FILE="$BASE in localedata/charmaps of glibc";
51 SOURCE="";;
52 CZYBORRA)
53 BASE="$BASE.gz";
54 SOURCE="http://czyborra.com/charsets/${BASE}";;
55 IANA)
56 SOURCE="http://www.iana.org/assignments/charset-reg/${BASE}";;
57 UNICODE)
58 SOURCE="http://www.unicode.org/Public/MAPPINGS/VENDORS/ADOBE/${BASE}";;
59 UNICODE2)
60 SOURCE="http://www.unicode.org/Public/MAPPINGS/VENDORS/MICSFT/WINDOWS/${BASE}";;
61 YASUOKA)
62 BASE="$BASE.Z";
63 SOURCE="http://kanji.zinbun.kyoto-u.ac.jp/~yasuoka/ftp/CJKtable/${BASE}";;
64 KANJI-DATABASE)
65 SOURCE="http://kanji-database.cvs.sourceforge.net/viewvc/*checkout*/kanji-database/kanji-database/data/cns2ucsdkw.txt?revision=1.4";;
67 printf 'Unknown file type: %s\n' "$3"
68 exit 1;;
69 esac
71 if [ -n "$SOURCE" ] ; then
72 echo "# Generated from $FILE which is a copy of";
73 echo "# $SOURCE"
74 else
75 echo "# Generated from $FILE"
79 if [ -n "$4" ] ; then
80 if [ -f "$4" ] ; then
81 AWKPROG="$AWK -f $4"
82 else
83 echo "Awk program does not exist: $4"
84 exit 1
86 else
87 AWKPROG=cat
90 if [ "$3" = "GLIBC-1" ] ; then
91 # Source format is:
92 # <UYYYY> /xXX
93 gunzip -c $1 | sed -n -e "${2}p" \
94 | sed -e 's,<U\([^>]*\)>[ ]*/x\(..\).*,0x\2 0x\1,' \
95 | sort | ${AWKPROG}
96 elif [ "$3" = "GLIBC-2" ] ; then
97 # Source format is:
98 # <UYYYY> /xXX/xZZ
99 gunzip -c $1 | sed -n -e "${2}p" \
100 | sed -e 's,<U\([^>]*\)>[ ]*/x\(..\)/x\(..\).*,0x\2\3 0x\1,' \
101 | sort | ${AWKPROG}
102 elif [ "$3" = "GLIBC-2-7" ] ; then
103 # Source format is:
104 # <UYYYY> /xXX/xZZ
105 # We must drop MSBs of XX and ZZ
106 gunzip -c $1 | sed -n -e "${2}p" \
107 | sed -e 's/xa/x2/g' -e 's/xb/x3/g' -e 's/xc/x4/g' \
108 -e 's/xd/x5/g' -e 's/xe/x6/g' -e 's/xf/x7/g' \
109 -e 's,<U\([^>]*\)>[ ]*/x\(..\)/x\(..\).*,0x\2\3 0x\1,' \
110 | sort | ${AWKPROG}
111 elif [ "$3" = "CZYBORRA" ] ; then
112 # Source format is:
113 # =XX U+YYYY
114 sed -n -e "${2}p" < $1 \
115 | sed -e 's/=\(..\)[^U]*U+\([0-9A-F]*\).*/0x\1 0x\2/' \
116 | sort | ${AWKPROG}
117 elif [ "$3" = "IANA" ] ; then
118 # Source format is:
119 # 0xXX 0xYYYY
120 sed -n -e "${2}p" < $1 \
121 | sed -e 's/\(0x[0-9A-Fa-f]*\)[^0]*\(0x[0-9A-Fa-f]*\).*/\1 \2/' \
122 | sort | ${AWKPROG}
123 elif [ "$3" = "UNICODE" ] ; then
124 # Source format is:
125 # YYYY XX
126 # We perform reverse sort to prefer the first one in the
127 # duplicated mappings (e.g. 0x20->U+0020, 0x20->U+00A0).
128 sed -n -e "${2}p" < $1 \
129 | sed -e 's/\([0-9A-F]*\)[^0-9A-F]*\([0-9A-F]*\).*/0x\2 0x\1/' \
130 | sort -r
131 elif [ "$3" = "UNICODE2" ] ; then
132 # Source format is:
133 # 0xXXXX 0xYYYY # ...
134 sed -n -e "${2}p" < $1 \
135 | sed -e 's/\([0-9A-Fx]*\)[^0]*\([0-9A-Fx]*\).*/\1 \2/' \
136 | ${AWKPROG} | sort -n -k 4,4
137 elif [ "$3" = "YASUOKA" ] ; then
138 # Source format is:
139 # YYYY 0-XXXX (XXXX is a Kuten code)
140 sed -n -e "${2}p" < $1 \
141 | sed -e 's/\([0-9A-F]*\)[^0]*0-\([0-9]*\).*/0x\2 0x\1/' \
142 | sort | ${AWKPROG}
143 elif [ "$3" = "KANJI-DATABASE" ] ; then
144 # Source format is:
145 # C?-XXXX U+YYYYY .....
146 sed -n -e "${2}p" < $1 \
147 | sed -e 's/...\(....\) U+\([0-9A-F]*\).*/0x\1 0x\2/' \
148 | sort | ${AWKPROG}
149 else
150 printf 'Invalid arguments: %s\n' "$3"
151 exit 1