xref: /openbsd/gnu/gcc/libcpp/makeucnid.c (revision 404b540a)
1*404b540aSrobert /* Make ucnid.h from various sources.
2*404b540aSrobert    Copyright (C) 2005 Free Software Foundation, Inc.
3*404b540aSrobert 
4*404b540aSrobert This program is free software; you can redistribute it and/or modify it
5*404b540aSrobert under the terms of the GNU General Public License as published by the
6*404b540aSrobert Free Software Foundation; either version 2, or (at your option) any
7*404b540aSrobert later version.
8*404b540aSrobert 
9*404b540aSrobert This program is distributed in the hope that it will be useful,
10*404b540aSrobert but WITHOUT ANY WARRANTY; without even the implied warranty of
11*404b540aSrobert MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12*404b540aSrobert GNU General Public License for more details.
13*404b540aSrobert 
14*404b540aSrobert You should have received a copy of the GNU General Public License
15*404b540aSrobert along with this program; if not, write to the Free Software
16*404b540aSrobert Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
17*404b540aSrobert 
18*404b540aSrobert /* Run this program as
19*404b540aSrobert    ./makeucnid ucnid.tab UnicodeData.txt DerivedNormalizationProps.txt \
20*404b540aSrobert        > ucnid.h
21*404b540aSrobert */
22*404b540aSrobert 
23*404b540aSrobert #include <stdio.h>
24*404b540aSrobert #include <string.h>
25*404b540aSrobert #include <ctype.h>
26*404b540aSrobert #include <stdbool.h>
27*404b540aSrobert #include <stdlib.h>
28*404b540aSrobert 
29*404b540aSrobert enum {
30*404b540aSrobert   C99 = 1,
31*404b540aSrobert   CXX = 2,
32*404b540aSrobert   digit = 4,
33*404b540aSrobert   not_NFC = 8,
34*404b540aSrobert   not_NFKC = 16,
35*404b540aSrobert   maybe_not_NFC = 32
36*404b540aSrobert };
37*404b540aSrobert 
38*404b540aSrobert static unsigned flags[65536];
39*404b540aSrobert static unsigned short decomp[65536][2];
40*404b540aSrobert static unsigned char combining_value[65536];
41*404b540aSrobert 
42*404b540aSrobert /* Die!  */
43*404b540aSrobert 
44*404b540aSrobert static void
fail(const char * s)45*404b540aSrobert fail (const char *s)
46*404b540aSrobert {
47*404b540aSrobert   fprintf (stderr, "%s\n", s);
48*404b540aSrobert   exit (1);
49*404b540aSrobert }
50*404b540aSrobert 
51*404b540aSrobert /* Read ucnid.tab and set the C99 and CXX flags in header[].  */
52*404b540aSrobert 
53*404b540aSrobert static void
read_ucnid(const char * fname)54*404b540aSrobert read_ucnid (const char *fname)
55*404b540aSrobert {
56*404b540aSrobert   FILE *f = fopen (fname, "r");
57*404b540aSrobert   unsigned fl = 0;
58*404b540aSrobert 
59*404b540aSrobert   if (!f)
60*404b540aSrobert     fail ("opening ucnid.tab");
61*404b540aSrobert   for (;;)
62*404b540aSrobert     {
63*404b540aSrobert       char line[256];
64*404b540aSrobert 
65*404b540aSrobert       if (!fgets (line, sizeof (line), f))
66*404b540aSrobert 	break;
67*404b540aSrobert       if (strcmp (line, "[C99]\n") == 0)
68*404b540aSrobert 	fl = C99;
69*404b540aSrobert       else if (strcmp (line, "[CXX]\n") == 0)
70*404b540aSrobert 	fl = CXX;
71*404b540aSrobert       else if (isxdigit (line[0]))
72*404b540aSrobert 	{
73*404b540aSrobert 	  char *l = line;
74*404b540aSrobert 	  while (*l)
75*404b540aSrobert 	    {
76*404b540aSrobert 	      unsigned long start, end;
77*404b540aSrobert 	      char *endptr;
78*404b540aSrobert 	      start = strtoul (l, &endptr, 16);
79*404b540aSrobert 	      if (endptr == l || (*endptr != '-' && ! isspace (*endptr)))
80*404b540aSrobert 		fail ("parsing ucnid.tab [1]");
81*404b540aSrobert 	      l = endptr;
82*404b540aSrobert 	      if (*l != '-')
83*404b540aSrobert 		end = start;
84*404b540aSrobert 	      else
85*404b540aSrobert 		{
86*404b540aSrobert 		  end = strtoul (l + 1, &endptr, 16);
87*404b540aSrobert 		  if (end < start)
88*404b540aSrobert 		    fail ("parsing ucnid.tab, end before start");
89*404b540aSrobert 		  l = endptr;
90*404b540aSrobert 		  if (! isspace (*l))
91*404b540aSrobert 		    fail ("parsing ucnid.tab, junk after range");
92*404b540aSrobert 		}
93*404b540aSrobert 	      while (isspace (*l))
94*404b540aSrobert 		l++;
95*404b540aSrobert 	      if (end > 0xFFFF)
96*404b540aSrobert 		fail ("parsing ucnid.tab, end too large");
97*404b540aSrobert 	      while (start <= end)
98*404b540aSrobert 		flags[start++] |= fl;
99*404b540aSrobert 	    }
100*404b540aSrobert 	}
101*404b540aSrobert     }
102*404b540aSrobert   if (ferror (f))
103*404b540aSrobert     fail ("reading ucnid.tab");
104*404b540aSrobert   fclose (f);
105*404b540aSrobert }
106*404b540aSrobert 
107*404b540aSrobert /* Read UnicodeData.txt and set the 'digit' flag, and
108*404b540aSrobert    also fill in the 'decomp' table to be the decompositions of
109*404b540aSrobert    characters for which both the character decomposed and all the code
110*404b540aSrobert    points in the decomposition are either C99 or CXX.  */
111*404b540aSrobert 
112*404b540aSrobert static void
read_table(char * fname)113*404b540aSrobert read_table (char *fname)
114*404b540aSrobert {
115*404b540aSrobert   FILE * f = fopen (fname, "r");
116*404b540aSrobert 
117*404b540aSrobert   if (!f)
118*404b540aSrobert     fail ("opening UnicodeData.txt");
119*404b540aSrobert   for (;;)
120*404b540aSrobert     {
121*404b540aSrobert       char line[256];
122*404b540aSrobert       unsigned long codepoint, this_decomp[4];
123*404b540aSrobert       char *l;
124*404b540aSrobert       int i;
125*404b540aSrobert       int decomp_useful;
126*404b540aSrobert 
127*404b540aSrobert       if (!fgets (line, sizeof (line), f))
128*404b540aSrobert 	break;
129*404b540aSrobert       codepoint = strtoul (line, &l, 16);
130*404b540aSrobert       if (l == line || *l != ';')
131*404b540aSrobert 	fail ("parsing UnicodeData.txt, reading code point");
132*404b540aSrobert       if (codepoint > 0xffff || ! (flags[codepoint] & (C99 | CXX)))
133*404b540aSrobert 	continue;
134*404b540aSrobert 
135*404b540aSrobert       do {
136*404b540aSrobert 	l++;
137*404b540aSrobert       } while (*l != ';');
138*404b540aSrobert       /* Category value; things starting with 'N' are numbers of some
139*404b540aSrobert 	 kind.  */
140*404b540aSrobert       if (*++l == 'N')
141*404b540aSrobert 	flags[codepoint] |= digit;
142*404b540aSrobert 
143*404b540aSrobert       do {
144*404b540aSrobert 	l++;
145*404b540aSrobert       } while (*l != ';');
146*404b540aSrobert       /* Canonical combining class; in NFC/NFKC, they must be increasing
147*404b540aSrobert 	 (or zero).  */
148*404b540aSrobert       if (! isdigit (*++l))
149*404b540aSrobert 	fail ("parsing UnicodeData.txt, combining class not number");
150*404b540aSrobert       combining_value[codepoint] = strtoul (l, &l, 10);
151*404b540aSrobert       if (*l++ != ';')
152*404b540aSrobert 	fail ("parsing UnicodeData.txt, junk after combining class");
153*404b540aSrobert 
154*404b540aSrobert       /* Skip over bidi value.  */
155*404b540aSrobert       do {
156*404b540aSrobert 	l++;
157*404b540aSrobert       } while (*l != ';');
158*404b540aSrobert 
159*404b540aSrobert       /* Decomposition mapping.  */
160*404b540aSrobert       decomp_useful = flags[codepoint];
161*404b540aSrobert       if (*++l == '<')  /* Compatibility mapping. */
162*404b540aSrobert 	continue;
163*404b540aSrobert       for (i = 0; i < 4; i++)
164*404b540aSrobert 	{
165*404b540aSrobert 	  if (*l == ';')
166*404b540aSrobert 	    break;
167*404b540aSrobert 	  if (!isxdigit (*l))
168*404b540aSrobert 	    fail ("parsing UnicodeData.txt, decomposition format");
169*404b540aSrobert 	  this_decomp[i] = strtoul (l, &l, 16);
170*404b540aSrobert 	  decomp_useful &= flags[this_decomp[i]];
171*404b540aSrobert 	  while (isspace (*l))
172*404b540aSrobert 	    l++;
173*404b540aSrobert 	}
174*404b540aSrobert       if (i > 2)  /* Decomposition too long.  */
175*404b540aSrobert 	fail ("parsing UnicodeData.txt, decomposition too long");
176*404b540aSrobert       if (decomp_useful)
177*404b540aSrobert 	while (--i >= 0)
178*404b540aSrobert 	  decomp[codepoint][i] = this_decomp[i];
179*404b540aSrobert     }
180*404b540aSrobert   if (ferror (f))
181*404b540aSrobert     fail ("reading UnicodeData.txt");
182*404b540aSrobert   fclose (f);
183*404b540aSrobert }
184*404b540aSrobert 
185*404b540aSrobert /* Read DerivedNormalizationProps.txt and set the flags that say whether
186*404b540aSrobert    a character is in NFC, NFKC, or is context-dependent.  */
187*404b540aSrobert 
188*404b540aSrobert static void
read_derived(const char * fname)189*404b540aSrobert read_derived (const char *fname)
190*404b540aSrobert {
191*404b540aSrobert   FILE * f = fopen (fname, "r");
192*404b540aSrobert 
193*404b540aSrobert   if (!f)
194*404b540aSrobert     fail ("opening DerivedNormalizationProps.txt");
195*404b540aSrobert   for (;;)
196*404b540aSrobert     {
197*404b540aSrobert       char line[256];
198*404b540aSrobert       unsigned long start, end;
199*404b540aSrobert       char *l;
200*404b540aSrobert       bool not_NFC_p, not_NFKC_p, maybe_not_NFC_p;
201*404b540aSrobert 
202*404b540aSrobert       if (!fgets (line, sizeof (line), f))
203*404b540aSrobert 	break;
204*404b540aSrobert       not_NFC_p = (strstr (line, "; NFC_QC; N") != NULL);
205*404b540aSrobert       not_NFKC_p = (strstr (line, "; NFKC_QC; N") != NULL);
206*404b540aSrobert       maybe_not_NFC_p = (strstr (line, "; NFC_QC; M") != NULL);
207*404b540aSrobert       if (! not_NFC_p && ! not_NFKC_p && ! maybe_not_NFC_p)
208*404b540aSrobert 	continue;
209*404b540aSrobert 
210*404b540aSrobert       start = strtoul (line, &l, 16);
211*404b540aSrobert       if (l == line)
212*404b540aSrobert 	fail ("parsing DerivedNormalizationProps.txt, reading start");
213*404b540aSrobert       if (start > 0xffff)
214*404b540aSrobert 	continue;
215*404b540aSrobert       if (*l == '.' && l[1] == '.')
216*404b540aSrobert 	end = strtoul (l + 2, &l, 16);
217*404b540aSrobert       else
218*404b540aSrobert 	end = start;
219*404b540aSrobert 
220*404b540aSrobert       while (start <= end)
221*404b540aSrobert 	flags[start++] |= ((not_NFC_p ? not_NFC : 0)
222*404b540aSrobert 			   | (not_NFKC_p ? not_NFKC : 0)
223*404b540aSrobert 			   | (maybe_not_NFC_p ? maybe_not_NFC : 0)
224*404b540aSrobert 			   );
225*404b540aSrobert     }
226*404b540aSrobert   if (ferror (f))
227*404b540aSrobert     fail ("reading DerivedNormalizationProps.txt");
228*404b540aSrobert   fclose (f);
229*404b540aSrobert }
230*404b540aSrobert 
231*404b540aSrobert /* Write out the table.
232*404b540aSrobert    The table consists of two words per entry.  The first word is the flags
233*404b540aSrobert    for the unicode code points up to and including the second word.  */
234*404b540aSrobert 
235*404b540aSrobert static void
write_table(void)236*404b540aSrobert write_table (void)
237*404b540aSrobert {
238*404b540aSrobert   unsigned i;
239*404b540aSrobert   unsigned last_flag = flags[0];
240*404b540aSrobert   bool really_safe = decomp[0][0] == 0;
241*404b540aSrobert   unsigned char last_combine = combining_value[0];
242*404b540aSrobert 
243*404b540aSrobert   for (i = 1; i <= 65536; i++)
244*404b540aSrobert     if (i == 65536
245*404b540aSrobert 	|| (flags[i] != last_flag && ((flags[i] | last_flag) & (C99 | CXX)))
246*404b540aSrobert 	|| really_safe != (decomp[i][0] == 0)
247*404b540aSrobert 	|| combining_value[i] != last_combine)
248*404b540aSrobert       {
249*404b540aSrobert 	printf ("{ %s|%s|%s|%s|%s|%s|%s, %3d, %#06x },\n",
250*404b540aSrobert 		last_flag & C99 ? "C99" : "  0",
251*404b540aSrobert 		last_flag & digit ? "DIG" : "  0",
252*404b540aSrobert 		last_flag & CXX ? "CXX" : "  0",
253*404b540aSrobert 		really_safe ? "CID" : "  0",
254*404b540aSrobert 		last_flag & not_NFC ? "  0" : "NFC",
255*404b540aSrobert 		last_flag & not_NFKC ? "  0" : "NKC",
256*404b540aSrobert 		last_flag & maybe_not_NFC ? "CTX" : "  0",
257*404b540aSrobert 		combining_value[i - 1],
258*404b540aSrobert 		i - 1);
259*404b540aSrobert 	last_flag = flags[i];
260*404b540aSrobert 	last_combine = combining_value[0];
261*404b540aSrobert 	really_safe = decomp[i][0] == 0;
262*404b540aSrobert       }
263*404b540aSrobert }
264*404b540aSrobert 
265*404b540aSrobert /* Print out the huge copyright notice.  */
266*404b540aSrobert 
267*404b540aSrobert static void
write_copyright(void)268*404b540aSrobert write_copyright (void)
269*404b540aSrobert {
270*404b540aSrobert   static const char copyright[] = "\
271*404b540aSrobert /* Unicode characters and various properties.\n\
272*404b540aSrobert    Copyright (C) 2003, 2005 Free Software Foundation, Inc.\n\
273*404b540aSrobert \n\
274*404b540aSrobert    This program is free software; you can redistribute it and/or modify it\n\
275*404b540aSrobert    under the terms of the GNU General Public License as published by the\n\
276*404b540aSrobert    Free Software Foundation; either version 2, or (at your option) any\n\
277*404b540aSrobert    later version.\n\
278*404b540aSrobert \n\
279*404b540aSrobert    This program is distributed in the hope that it will be useful,\n\
280*404b540aSrobert    but WITHOUT ANY WARRANTY; without even the implied warranty of\n\
281*404b540aSrobert    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n\
282*404b540aSrobert    GNU General Public License for more details.\n\
283*404b540aSrobert \n\
284*404b540aSrobert    You should have received a copy of the GNU General Public License\n\
285*404b540aSrobert    along with this program; if not, write to the Free Software\n\
286*404b540aSrobert    Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.\n\
287*404b540aSrobert \n\
288*404b540aSrobert \n\
289*404b540aSrobert    Copyright (C) 1991-2005 Unicode, Inc.  All rights reserved.\n\
290*404b540aSrobert    Distributed under the Terms of Use in\n\
291*404b540aSrobert    http://www.unicode.org/copyright.html.\n\
292*404b540aSrobert \n\
293*404b540aSrobert    Permission is hereby granted, free of charge, to any person\n\
294*404b540aSrobert    obtaining a copy of the Unicode data files and any associated\n\
295*404b540aSrobert    documentation (the \"Data Files\") or Unicode software and any\n\
296*404b540aSrobert    associated documentation (the \"Software\") to deal in the Data Files\n\
297*404b540aSrobert    or Software without restriction, including without limitation the\n\
298*404b540aSrobert    rights to use, copy, modify, merge, publish, distribute, and/or\n\
299*404b540aSrobert    sell copies of the Data Files or Software, and to permit persons to\n\
300*404b540aSrobert    whom the Data Files or Software are furnished to do so, provided\n\
301*404b540aSrobert    that (a) the above copyright notice(s) and this permission notice\n\
302*404b540aSrobert    appear with all copies of the Data Files or Software, (b) both the\n\
303*404b540aSrobert    above copyright notice(s) and this permission notice appear in\n\
304*404b540aSrobert    associated documentation, and (c) there is clear notice in each\n\
305*404b540aSrobert    modified Data File or in the Software as well as in the\n\
306*404b540aSrobert    documentation associated with the Data File(s) or Software that the\n\
307*404b540aSrobert    data or software has been modified.\n\
308*404b540aSrobert \n\
309*404b540aSrobert    THE DATA FILES AND SOFTWARE ARE PROVIDED \"AS IS\", WITHOUT WARRANTY\n\
310*404b540aSrobert    OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE\n\
311*404b540aSrobert    WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\n\
312*404b540aSrobert    NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE\n\
313*404b540aSrobert    COPYRIGHT HOLDER OR HOLDERS INCLUDED IN THIS NOTICE BE LIABLE FOR\n\
314*404b540aSrobert    ANY CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY\n\
315*404b540aSrobert    DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,\n\
316*404b540aSrobert    WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS\n\
317*404b540aSrobert    ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE\n\
318*404b540aSrobert    OF THE DATA FILES OR SOFTWARE.\n\
319*404b540aSrobert \n\
320*404b540aSrobert    Except as contained in this notice, the name of a copyright holder\n\
321*404b540aSrobert    shall not be used in advertising or otherwise to promote the sale,\n\
322*404b540aSrobert    use or other dealings in these Data Files or Software without prior\n\
323*404b540aSrobert    written authorization of the copyright holder.  */\n";
324*404b540aSrobert 
325*404b540aSrobert    puts (copyright);
326*404b540aSrobert }
327*404b540aSrobert 
328*404b540aSrobert /* Main program.  */
329*404b540aSrobert 
330*404b540aSrobert int
main(int argc,char ** argv)331*404b540aSrobert main(int argc, char ** argv)
332*404b540aSrobert {
333*404b540aSrobert   if (argc != 4)
334*404b540aSrobert     fail ("too few arguments to makeucn");
335*404b540aSrobert   read_ucnid (argv[1]);
336*404b540aSrobert   read_table (argv[2]);
337*404b540aSrobert   read_derived (argv[3]);
338*404b540aSrobert 
339*404b540aSrobert   write_copyright ();
340*404b540aSrobert   write_table ();
341*404b540aSrobert   return 0;
342*404b540aSrobert }
343