a2721826d13bb81c4eb17087435c041dd3200498
[platform/upstream/groff.git] / src / libs / libgroff / make-uniuni
1 #! /bin/sh
2 #
3 # make-uniuni -- script for creating the file uniuni.cpp
4 #
5 # Copyright (C) 2005-2014  Free Software Foundation, Inc.
6 #      Written by Werner Lemberg <wl@gnu.org>
7 #
8 # This file is part of groff.
9 #
10 # groff is free software; you can redistribute it and/or modify it under
11 # the terms of the GNU General Public License as published by the Free
12 # Software Foundation, either version 3 of the License, or
13 # (at your option) any later version.
14 #
15 # groff is distributed in the hope that it will be useful, but WITHOUT ANY
16 # WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 # FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
18 # for more details.
19 #
20 # You should have received a copy of the GNU General Public License
21 # along with this program. If not, see <http://www.gnu.org/licenses/>.
22
23 #
24 # usage:
25 #
26 #   make-uniuni <version-string> < UnicodeData.txt > uniuni.cpp
27 #
28 # `UnicodeData.txt' is the central database file from the Unicode standard.
29 # Unfortunately, it doesn't contain a version number which must be thus
30 # provided manually as a parameter to the filter.
31 #
32 # This program needs a C preprocessor.
33 #
34
35 CPP=cpp
36
37 prog="$0"
38
39 if test $# -ne 1; then
40   echo "usage: $0 <version-string> < UnicodeData.txt > uniuni.cpp"
41   exit 1
42 fi
43
44 version_string="$1"
45
46 # Remove ranges and control characters,
47 # then extract the decomposition field,
48 # then remove lines without decomposition,
49 # then remove all compatibility decompositions.
50 sed -e '/^[^;]*;</d' \
51 | sed -e 's/;[^;]*;[^;]*;[^;]*;[^;]*;\([^;]*\);.*$/;\1/' \
52 | sed -e '/^[^;]*;$/d' \
53 | sed -e '/^[^;]*;</d' > $$1
54
55 # Prepare input for running cpp.
56 cat $$1 \
57 | sed -e 's/^\([^;]*\);/#define \1 /' \
58       -e 's/ / u/g' > $$2
59 cat $$1 \
60 | sed -e 's/^\([^;]*\);.*$/\1 u\1/' >> $$2
61
62 # Run C preprocessor to recursively decompose.
63 $CPP $$2 $$3
64
65 # Convert it back to original format.
66 cat $$3 \
67 | sed -e '/#/d' \
68       -e '/^$/d' \
69       -e 's/ \+/ /g' \
70       -e 's/ *$//' \
71       -e 's/u//g' \
72       -e 's/^\([^ ]*\) /\1;/' > $$4
73
74 # Write preamble.
75 cat <<END
76 // -*- C++ -*-
77 /* Copyright (C) 2002, 2003, 2004, 2005
78    Free Software Foundation, Inc.
79      Written by Werner Lemberg <wl@gnu.org>
80
81 This file is part of groff.
82
83 groff is free software; you can redistribute it and/or modify it under
84 the terms of the GNU General Public License as published by the Free
85 Software Foundation, either version 3 of the License, or
86 (at your option) any later version.
87
88 groff is distributed in the hope that it will be useful, but WITHOUT ANY
89 WARRANTY; without even the implied warranty of MERCHANTABILITY or
90 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
91 for more details.
92
93 You should have received a copy of the GNU General Public License
94 along with this program. If not, see <http://www.gnu.org/licenses/>. */
95
96 // This code has been algorithmically derived from the file
97 // UnicodeData.txt, version $version_string, available from unicode.org,
98 // on `date '+%Y-%m-%d'`.
99
100 #include "lib.h"
101 #include "stringclass.h"
102 #include "ptable.h"
103
104 #include "unicode.h"
105
106 struct unicode_decompose {
107   char *value;
108 };
109
110 declare_ptable(unicode_decompose)
111 implement_ptable(unicode_decompose)
112
113 PTABLE(unicode_decompose) unicode_decompose_table;
114
115 // the first digit in the composite string gives the number of composites
116
117 struct S {
118   const char *key;
119   const char *value;
120 } unicode_decompose_list[] = {
121 END
122
123 # Emit Unicode data.
124 cat $$4 \
125 | sed -e 's/ /_/g' \
126       -e 's/\(.*\);\(.*_.*_.*_.*\)$/  { "\1", "4\2" },/' \
127       -e 's/\(.*\);\(.*_.*_.*\)$/  { "\1", "3\2" },/' \
128       -e 's/\(.*\);\(.*_.*\)$/  { "\1", "2\2" },/' \
129       -e 's/\(.*\);\(.*\)$/  { "\1", "1\2" },/'
130
131 # Write postamble.
132 cat <<END
133 };
134
135 // global constructor
136
137 static struct unicode_decompose_init {
138   unicode_decompose_init();
139 } _unicode_decompose_init;
140
141 unicode_decompose_init::unicode_decompose_init()
142 {
143   for (unsigned int i = 0;
144        i < sizeof(unicode_decompose_list)/sizeof(unicode_decompose_list[0]);
145        i++) {
146     unicode_decompose *dec = new unicode_decompose[1];
147     dec->value = (char *)unicode_decompose_list[i].value;
148     unicode_decompose_table.define(unicode_decompose_list[i].key, dec);
149   }
150 }
151
152 const char *decompose_unicode(const char *s)
153 {
154   unicode_decompose *result = unicode_decompose_table.lookup(s);
155   return result ? result->value : 0;
156 }
157 END
158
159
160 # Remove temporary files.
161 rm $$1 $$2 $$3 $$4
162
163 # EOF