2 * gen-brackets-tab.c - generate brackets.tab.i
5 * Behdad Esfahbod, 2001, 2002, 2004
8 * Copyright (C) 2004 Sharif FarsiWeb, Inc
9 * Copyright (C) 2001,2002,2004 Behdad Esfahbod
10 * Copyright (C) 2017 Dov Grobgeld
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.
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.
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
27 * For licensing issues, contact <fribidi.license@gmail.com>.
32 #include <fribidi-unicode.h>
48 # if !STDC_HEADERS && HAVE_MEMORY_H
59 #define appname "gen-brackets-tab"
60 #define outputname "brackets.tab.i"
67 fprintf (stderr, appname ": %s\n", msg);
77 fprintf (stderr, appname ": ");
78 fprintf (stderr, fmt, p);
79 fprintf (stderr, "\n");
90 fprintf (stderr, appname ": ");
91 fprintf (stderr, fmt, p, q);
92 fprintf (stderr, "\n");
104 fprintf (stderr, appname ": ");
105 fprintf (stderr, fmt, l, p, q);
106 fprintf (stderr, "\n");
110 #define table_name "Brk"
111 #define macro_name "FRIBIDI_GET_BRACKETS"
113 static signed int table[FRIBIDI_UNICODE_CHARS];
114 static signed int equiv_table[FRIBIDI_UNICODE_CHARS];
115 static char buf[4000];
116 static signed long max_dist;
131 register FriBidiChar c;
133 for (c = 0; c < FRIBIDI_UNICODE_CHARS; c++)
140 static signed int table[FRIBIDI_UNICODE_CHARS];
141 static char buf[4000];
143 /* Read the canonical mapping of unicode characters and store them in the
144 equiv_table array. */
146 read_unicode_data_txt_equivalence (
153 while (fgets (buf, sizeof buf, f))
157 char ce_string[256]; /* For parsing the equivalence */
166 if (s[0] == '#' || s[0] == '\0' || s[0] == '\n')
168 /* Field: 0 ; 1 ; 2 ; 3 ; 4 ; 5 */
169 i = sscanf (s, "%lx;%*[^;];%*[^;];%*[^;];%*[^;];%[^;]", &c, ce_string);
170 if (c >= FRIBIDI_UNICODE_CHARS)
172 fprintf (stderr, "invalid input at line %ld: %s", l, s);
178 /* split and parse ce */
190 else if (!in_tag && isalnum(*p))
192 /* Assume we got a hexa decimal */
193 ce = strtol(p,NULL,16);
199 /* FIXME: We don't handle First..Last parts of UnicodeData.txt,
200 * but it works, since all those are LTR. */
206 read_bidi_brackets_txt (
213 while (fgets (buf, sizeof buf, f))
226 if (s[0] == '#' || s[0] == '\0' || s[0] == '\n')
229 k = sscanf (s, "%lx; %lx; %c", &i, &j, &open_close);
230 if (k != 3 || i >= FRIBIDI_UNICODE_CHARS || j >= FRIBIDI_UNICODE_CHARS)
231 die4 ("invalid pair in input at line %ld: %04lX, %04lX", l, i, j);
233 /* Open braces map to themself */
237 /* Turn j into the unicode equivalence if it exists */
240 /* printf("Found match for %04x->%04x\n", j, equiv_table[j]); */
244 dist = ((signed long) j - (signed long) i);
248 else if (-dist > max_dist)
255 const char *bracket_datafile_type,
256 const char *bracket_datafile_name,
257 const char *uni_datafile_type,
258 const char *uni_datafile_name
265 if (!(f = fopen (uni_datafile_name, "rt")))
266 die2 ("error: cannot open `%s' for reading", bracket_datafile_name);
268 if (!strcmp (uni_datafile_type, "UnicodeData.txt"))
269 read_unicode_data_txt_equivalence (f);
271 die2 ("error: unknown data-file-type %s", uni_datafile_type);
273 fprintf (stderr, "Reading `%s'\n", bracket_datafile_name);
274 if (!(f = fopen (bracket_datafile_name, "rt")))
275 die2 ("error: cannot open `%s' for reading", bracket_datafile_name);
277 if (!strcmp (bracket_datafile_type, "BidiBrackets.txt"))
278 read_bidi_brackets_txt (f);
280 die2 ("error: unknown data-file-type %s", bracket_datafile_type);
288 const char *data_file_type
292 const char *key_type;
294 printf ("/* " outputname "\n * generated by " appname " (" FRIBIDI_NAME " "
295 FRIBIDI_VERSION ")\n" " * from the file %s of Unicode version "
296 FRIBIDI_UNICODE_VERSION ". */\n\n", data_file_type);
298 printf ("#define PACKTAB_UINT8 uint8_t\n"
299 "#define PACKTAB_UINT16 uint16_t\n"
300 "#define PACKTAB_UINT32 uint32_t\n\n");
302 key_bytes = max_dist <= 0x7f ? 1 : max_dist < 0x7fff ? 2 : 4;
303 key_type = key_bytes == 1 ? "int8_t" : key_bytes == 2 ?
304 "int16_t" : "int32_t";
307 (table, FRIBIDI_UNICODE_CHARS, key_bytes, 0, max_depth, 1, NULL,
308 key_type, table_name, macro_name "_DELTA", stdout))
309 die ("error: insufficient memory, decrease max_depth");
311 printf ("#undef PACKTAB_UINT8\n"
312 "#undef PACKTAB_UINT16\n" "#undef PACKTAB_UINT32\n\n");
314 printf ("#define " macro_name "(x) ((x) + " macro_name "_DELTA(x))\n\n");
316 printf ("/* End of generated " outputname " */\n");
325 const char *bracket_datafile_type = "BidiBrackets.txt";
326 const char *uni_datafile_type = "UnicodeData.txt";
329 die3 ("usage:\n " appname " max-depth /path/to/%s /path/to/%s [junk...]",
330 bracket_datafile_type,
334 int max_depth = atoi (argv[1]);
335 const char *bracket_datafile_name = argv[2];
336 const char *uni_datafile_name = argv[3];
339 die ("invalid depth");
342 read_data (bracket_datafile_type, bracket_datafile_name,
343 uni_datafile_type, uni_datafile_name);
344 gen_brackets_tab (max_depth, bracket_datafile_type);