Merge branch 'upstream' into tizen
[platform/upstream/fribidi.git] / gen.tab / gen-brackets-tab.c
1 /* FriBidi
2  * gen-brackets-tab.c - generate brackets.tab.i
3  *
4  * Author:
5  *   Behdad Esfahbod, 2001, 2002, 2004
6  *   Dov Grobgeld 2017
7  *
8  * Copyright (C) 2004 Sharif FarsiWeb, Inc
9  * Copyright (C) 2001,2002,2004 Behdad Esfahbod
10  * Copyright (C) 2017 Dov Grobgeld
11  * 
12  * This library is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU Lesser General Public
14  * License as published by the Free Software Foundation; either
15  * version 2.1 of the License, or (at your option) any later version.
16  * 
17  * This library is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20  * Lesser General Public License for more details.
21  * 
22  * You should have received a copy of the GNU Lesser General Public License
23  * along with this library, in a file named COPYING; if not, write to the
24  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25  * Boston, MA 02110-1301, USA
26  * 
27  * For licensing issues, contact <fribidi.license@gmail.com>.
28  */
29
30 #include <common.h>
31 #include <ctype.h>
32 #include <fribidi-unicode.h>
33
34 #include <stdio.h>
35 #ifdef STDC_HEADERS
36 # include <stdlib.h>
37 # include <stddef.h>
38 #else
39 # if HAVE_STDLIB_H
40 #  include <stdlib.h>
41 # endif
42 #endif
43 #ifdef HAVE_STRING_H
44 # if !STDC_HEADERS && HAVE_MEMORY_H
45 #  include <memory.h>
46 # endif
47 # include <string.h>
48 #endif
49 #ifdef HAVE_STRINGS_H
50 # include <strings.h>
51 #endif
52
53 #include "packtab.h"
54
55 #define appname "gen-brackets-tab"
56 #define outputname "brackets.tab.i"
57
58 static void
59 die (
60   const char *msg
61 )
62 {
63   fprintf (stderr, appname ": %s\n", msg);
64   exit (1);
65 }
66
67 static void
68 die2 (
69   const char *fmt,
70   const char *p
71 )
72 {
73   fprintf (stderr, appname ": ");
74   fprintf (stderr, fmt, p);
75   fprintf (stderr, "\n");
76   exit (1);
77 }
78
79 static void
80 die3 (
81   const char *fmt,
82   const char *p,
83   const char *q
84 )
85 {
86   fprintf (stderr, appname ": ");
87   fprintf (stderr, fmt, p, q);
88   fprintf (stderr, "\n");
89   exit (1);
90 }
91
92 static void
93 die4 (
94   const char *fmt,
95   unsigned long l,
96   unsigned long p,
97   unsigned long q
98 )
99 {
100   fprintf (stderr, appname ": ");
101   fprintf (stderr, fmt, l, p, q);
102   fprintf (stderr, "\n");
103   exit (1);
104 }
105
106 #define table_name "Brk"
107 #define macro_name "FRIBIDI_GET_BRACKETS"
108
109 static signed int table[FRIBIDI_UNICODE_CHARS];
110 static signed int equiv_table[FRIBIDI_UNICODE_CHARS];
111 static char buf[4000];
112 static signed long max_dist;
113
114 static void
115 init (
116   void
117 )
118 {
119   max_dist = 0;
120 }
121
122 static void
123 clear_tabs (
124   void
125 )
126 {
127   register FriBidiChar c;
128
129   for (c = 0; c < FRIBIDI_UNICODE_CHARS; c++)
130     {
131       table[c] = 0;
132       equiv_table[c] = 0;
133     }
134 }
135
136 static signed int table[FRIBIDI_UNICODE_CHARS];
137 static char buf[4000];
138
139 /* Read the canonical mapping of unicode characters and store them in the
140    equiv_table array. */
141 static void
142 read_unicode_data_txt_equivalence (
143   FILE *f
144 )
145 {
146   unsigned long c, l;
147
148   l = 0;
149   while (fgets (buf, sizeof buf, f))
150     {
151       int i;
152       const char *s = buf;
153       char ce_string[256]; /* For parsing the equivalence */
154       char *p = NULL;
155       int ce, in_tag;
156
157       l++;
158
159       while (*s == ' ')
160         s++;
161
162       if (s[0] == '#' || s[0] == '\0' || s[0] == '\n')
163         continue;
164       /*  Field:       0 ; 1    ; 2    ; 3    ; 4    ; 5           */
165       i = sscanf (s, "%lx;%*[^;];%*[^;];%*[^;];%*[^;];%[^;]", &c, ce_string);
166       if (c >= FRIBIDI_UNICODE_CHARS)
167         {
168           fprintf (stderr, "invalid input at line %ld: %s", l, s);
169           exit(1);
170         }
171       if (i==1)
172         continue;
173
174       /* split and parse ce */
175       p = ce_string;
176       ce = -1;
177       in_tag = 0;
178       while(*p)
179         {
180           if (*p==';')
181             break;
182           else if (*p=='<')
183             in_tag = 1;
184           else if (*p=='>')
185             in_tag = 0;
186           else if (!in_tag && isalnum(*p))
187             {
188               /* Assume we got a hexa decimal */
189               ce = strtol(p,NULL,16);
190               break;
191             }
192           p++;
193         }
194
195       /* FIXME: We don't handle First..Last parts of UnicodeData.txt,
196        * but it works, since all those are LTR. */
197       equiv_table[c] = ce;
198     }
199 }
200
201 static void
202 read_bidi_brackets_txt (
203   FILE *f
204 )
205 {
206   unsigned long l;
207
208   l = 0;
209   while (fgets (buf, sizeof buf, f))
210     {
211       unsigned long i, j;
212       signed long dist;
213       int k;
214       const char *s = buf;
215       char open_close;
216
217       l++;
218
219       while (*s == ' ')
220         s++;
221
222       if (s[0] == '#' || s[0] == '\0' || s[0] == '\n')
223         continue;
224
225       k = sscanf (s, "%lx; %lx; %c", &i, &j, &open_close);
226       if (k != 3 || i >= FRIBIDI_UNICODE_CHARS || j >= FRIBIDI_UNICODE_CHARS)
227         die4 ("invalid pair in input at line %ld: %04lX, %04lX", l, i, j);
228
229       /* Open braces map to themself */
230       if (open_close=='o')
231         j = i;
232       
233       /* Turn j into the unicode equivalence if it exists */
234       if (equiv_table[j])
235         {
236           /* printf("Found match for %04x->%04x\n", j, equiv_table[j]); */
237           j = equiv_table[j];
238         }
239
240       dist = ((signed long) j - (signed long) i);
241       table[i] = dist;
242       if (dist > max_dist)
243         max_dist = dist;
244       else if (-dist > max_dist)
245         max_dist = -dist;
246     }
247 }
248
249 static void
250 read_data (
251   const char *bracket_datafile_type,
252   const char *bracket_datafile_name,
253   const char *uni_datafile_type,
254   const char *uni_datafile_name
255 )
256 {
257   FILE *f;
258
259   clear_tabs ();
260
261   if (!(f = fopen (uni_datafile_name, "rt")))
262     die2 ("error: cannot open `%s' for reading", bracket_datafile_name);
263
264   if (!strcmp (uni_datafile_type, "UnicodeData.txt"))
265     read_unicode_data_txt_equivalence (f);
266   else
267     die2 ("error: unknown data-file-type %s", uni_datafile_type);
268
269   fprintf (stderr, "Reading `%s'\n", bracket_datafile_name);
270   if (!(f = fopen (bracket_datafile_name, "rt")))
271     die2 ("error: cannot open `%s' for reading", bracket_datafile_name);
272
273   if (!strcmp (bracket_datafile_type, "BidiBrackets.txt"))
274     read_bidi_brackets_txt (f);
275   else
276     die2 ("error: unknown data-file-type %s", bracket_datafile_type);
277
278   fclose (f);
279 }
280
281 static void
282 gen_brackets_tab (
283   int max_depth,
284   const char *data_file_type
285 )
286 {
287   int key_bytes;
288   const char *key_type;
289
290   printf ("/* " outputname "\n * generated by " appname " (" FRIBIDI_NAME " "
291           FRIBIDI_VERSION ")\n" " * from the file %s of Unicode version "
292           FRIBIDI_UNICODE_VERSION ". */\n\n", data_file_type);
293
294   printf ("#define PACKTAB_UINT8 uint8_t\n"
295           "#define PACKTAB_UINT16 uint16_t\n"
296           "#define PACKTAB_UINT32 uint32_t\n\n");
297
298   key_bytes = max_dist <= 0x7f ? 1 : max_dist < 0x7fff ? 2 : 4;
299   key_type = key_bytes == 1 ? "int8_t" : key_bytes == 2 ?
300     "int16_t" : "int32_t";
301
302   if (!pack_table
303       (table, FRIBIDI_UNICODE_CHARS, key_bytes, 0, max_depth, 1, NULL,
304        key_type, table_name, macro_name "_DELTA", stdout))
305     die ("error: insufficient memory, decrease max_depth");
306
307   printf ("#undef PACKTAB_UINT8\n"
308           "#undef PACKTAB_UINT16\n" "#undef PACKTAB_UINT32\n\n");
309
310   printf ("#define " macro_name "(x) ((x) + " macro_name "_DELTA(x))\n\n");
311
312   printf ("/* End of generated " outputname " */\n");
313 }
314
315 int
316 main (
317   int argc,
318   const char **argv
319 )
320 {
321   const char *bracket_datafile_type = "BidiBrackets.txt";
322   const char *uni_datafile_type = "UnicodeData.txt";
323
324   if (argc < 4)
325     die3 ("usage:\n  " appname " max-depth /path/to/%s /path/to/%s [junk...]",
326           bracket_datafile_type,
327           uni_datafile_type);
328
329   {
330     int max_depth = atoi (argv[1]);
331     const char *bracket_datafile_name = argv[2];
332     const char *uni_datafile_name = argv[3];
333
334     if (max_depth < 2)
335       die ("invalid depth");
336
337     init ();
338     read_data (bracket_datafile_type, bracket_datafile_name,
339                uni_datafile_type, uni_datafile_name);
340     gen_brackets_tab (max_depth, bracket_datafile_type);
341   }
342
343   return 0;
344 }