Imported Upstream version 1.22.4
[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-2018 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-2014  Free Software Foundation, Inc.
78      Written by Werner Lemberg <wl@gnu.org>
79
80 This file is part of groff.
81
82 groff is free software; you can redistribute it and/or modify it under
83 the terms of the GNU General Public License as published by the Free
84 Software Foundation, either version 3 of the License, or
85 (at your option) any later version.
86
87 groff is distributed in the hope that it will be useful, but WITHOUT ANY
88 WARRANTY; without even the implied warranty of MERCHANTABILITY or
89 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90 for more details.
91
92 You should have received a copy of the GNU General Public License
93 along with this program.  If not, see <http://www.gnu.org/licenses/>. */
94
95 // This code has been algorithmically derived from the file
96 // UnicodeData.txt, version $version_string, available from unicode.org,
97 // on `date '+%Y-%m-%d'`.
98
99 #include "lib.h"
100 #include "stringclass.h"
101 #include "ptable.h"
102
103 #include "unicode.h"
104
105 struct unicode_decompose {
106   char *value;
107 };
108
109 declare_ptable(unicode_decompose)
110 implement_ptable(unicode_decompose)
111
112 PTABLE(unicode_decompose) unicode_decompose_table;
113
114 // the first digit in the composite string gives the number of composites
115
116 struct S {
117   const char *key;
118   const char *value;
119 } unicode_decompose_list[] = {
120 END
121
122 # Emit Unicode data.
123 cat $$4 \
124 | sed -e 's/ /_/g' \
125       -e 's/\(.*\);\(.*_.*_.*_.*\)$/  { "\1", "4\2" },/' \
126       -e 's/\(.*\);\(.*_.*_.*\)$/  { "\1", "3\2" },/' \
127       -e 's/\(.*\);\(.*_.*\)$/  { "\1", "2\2" },/' \
128       -e 's/\(.*\);\(.*\)$/  { "\1", "1\2" },/'
129
130 # Write postamble.
131 cat <<END
132 };
133
134 // global constructor
135
136 static struct unicode_decompose_init {
137   unicode_decompose_init();
138 } _unicode_decompose_init;
139
140 unicode_decompose_init::unicode_decompose_init()
141 {
142   for (unsigned int i = 0;
143        i < sizeof(unicode_decompose_list)/sizeof(unicode_decompose_list[0]);
144        i++) {
145     unicode_decompose *dec = new unicode_decompose[1];
146     dec->value = (char *)unicode_decompose_list[i].value;
147     unicode_decompose_table.define(unicode_decompose_list[i].key, dec);
148   }
149 }
150
151 const char *decompose_unicode(const char *s)
152 {
153   unicode_decompose *result = unicode_decompose_table.lookup(s);
154   return result ? result->value : 0;
155 }
156 END
157
158
159 # Remove temporary files.
160 rm $$1 $$2 $$3 $$4
161
162 # EOF