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