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>
44 # if !STDC_HEADERS && HAVE_MEMORY_H
55 #define appname "gen-brackets-tab"
56 #define outputname "brackets.tab.i"
63 fprintf (stderr, appname ": %s\n", msg);
73 fprintf (stderr, appname ": ");
74 fprintf (stderr, fmt, p);
75 fprintf (stderr, "\n");
86 fprintf (stderr, appname ": ");
87 fprintf (stderr, fmt, p, q);
88 fprintf (stderr, "\n");
100 fprintf (stderr, appname ": ");
101 fprintf (stderr, fmt, l, p, q);
102 fprintf (stderr, "\n");
106 #define table_name "Brk"
107 #define macro_name "FRIBIDI_GET_BRACKETS"
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;
127 register FriBidiChar c;
129 for (c = 0; c < FRIBIDI_UNICODE_CHARS; c++)
136 static signed int table[FRIBIDI_UNICODE_CHARS];
137 static char buf[4000];
139 /* Read the canonical mapping of unicode characters and store them in the
140 equiv_table array. */
142 read_unicode_data_txt_equivalence (
149 while (fgets (buf, sizeof buf, f))
153 char ce_string[256]; /* For parsing the equivalence */
162 if (s[0] == '#' || s[0] == '\0' || s[0] == '\n')
164 /* Field: 0 ; 1 ; 2 ; 3 ; 4 ; 5 */
165 i = sscanf (s, "%lx;%*[^;];%*[^;];%*[^;];%*[^;];%[^;]", &c, ce_string);
166 if (c >= FRIBIDI_UNICODE_CHARS)
168 fprintf (stderr, "invalid input at line %ld: %s", l, s);
174 /* split and parse ce */
186 else if (!in_tag && isalnum(*p))
188 /* Assume we got a hexa decimal */
189 ce = strtol(p,NULL,16);
195 /* FIXME: We don't handle First..Last parts of UnicodeData.txt,
196 * but it works, since all those are LTR. */
202 read_bidi_brackets_txt (
209 while (fgets (buf, sizeof buf, f))
222 if (s[0] == '#' || s[0] == '\0' || s[0] == '\n')
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);
229 /* Open braces map to themself */
233 /* Turn j into the unicode equivalence if it exists */
236 /* printf("Found match for %04x->%04x\n", j, equiv_table[j]); */
240 dist = ((signed long) j - (signed long) i);
244 else if (-dist > max_dist)
251 const char *bracket_datafile_type,
252 const char *bracket_datafile_name,
253 const char *uni_datafile_type,
254 const char *uni_datafile_name
261 if (!(f = fopen (uni_datafile_name, "rt")))
262 die2 ("error: cannot open `%s' for reading", bracket_datafile_name);
264 if (!strcmp (uni_datafile_type, "UnicodeData.txt"))
265 read_unicode_data_txt_equivalence (f);
267 die2 ("error: unknown data-file-type %s", uni_datafile_type);
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);
273 if (!strcmp (bracket_datafile_type, "BidiBrackets.txt"))
274 read_bidi_brackets_txt (f);
276 die2 ("error: unknown data-file-type %s", bracket_datafile_type);
284 const char *data_file_type
288 const char *key_type;
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);
294 printf ("#define PACKTAB_UINT8 uint8_t\n"
295 "#define PACKTAB_UINT16 uint16_t\n"
296 "#define PACKTAB_UINT32 uint32_t\n\n");
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";
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");
307 printf ("#undef PACKTAB_UINT8\n"
308 "#undef PACKTAB_UINT16\n" "#undef PACKTAB_UINT32\n\n");
310 printf ("#define " macro_name "(x) ((x) + " macro_name "_DELTA(x))\n\n");
312 printf ("/* End of generated " outputname " */\n");
321 const char *bracket_datafile_type = "BidiBrackets.txt";
322 const char *uni_datafile_type = "UnicodeData.txt";
325 die3 ("usage:\n " appname " max-depth /path/to/%s /path/to/%s [junk...]",
326 bracket_datafile_type,
330 int max_depth = atoi (argv[1]);
331 const char *bracket_datafile_name = argv[2];
332 const char *uni_datafile_name = argv[3];
335 die ("invalid depth");
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);