1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
|
/* FriBidi - Library of BiDi algorithm
* Copyright (C) 1999 Dov Grobgeld
* Copyright (C) 2001 Behdad Esfahbod
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
/*======================================================================
// A main program for fribidi.
//----------------------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include "fribidi.h"
#define appname "fribidi"
#define MAX_STR_LEN 65000
void
die (gchar * fmt, ...)
{
va_list ap;
va_start (ap, fmt);
fprintf (stderr, "%s: ", appname);
vfprintf (stderr, fmt, ap);
fprintf (stderr, "Try `%s --help' for more information.\n", appname);
exit (-1);
}
int
main (int argc, char *argv[])
{
int argp;
FILE *IN;
gboolean do_pad, do_fill, do_clean, file_found, show_input;
gboolean show_visual, show_ltov, show_vtol, show_levels, show_changes;
gint char_set, text_width;
guchar *bol_text, *eol_text;
FriBidiCharType input_base_direction;
argp = 1;
text_width = 80;
do_pad = TRUE;
do_fill = FALSE;
do_clean = FALSE;
show_input = FALSE;
show_visual = TRUE;
show_ltov = FALSE;
show_vtol = FALSE;
show_levels = FALSE;
show_changes = FALSE;
char_set = FRIBIDI_CHARSET_DEFAULT;
bol_text = NULL;
eol_text = NULL;
input_base_direction = FRIBIDI_TYPE_ON;
file_found = FALSE;
#define CASE(s) if (strcmp (S_, (s)) == 0)
#define CASE2(s1, s2) if (strcmp (S_, (s1)) == 0 || strcmp (S_, (s2)) == 0)
#define NEXTARG (argp++, argp - 1 < argc ? argv[argp - 1] : "")
/* Parse the command line */
while (argp < argc || (argp == argc && !file_found))
{
gchar *S_;
if (argp < argc)
S_ = argv[argp++];
else
S_ = "-";
if (S_[0] == '-' && S_[1])
{
CASE2 ("-h", "--help")
{
gint i;
printf
("Usage: %s [OPTION]... [FILE]...\n"
"A command line interface for the %s library,\n"
"Converts a logical string to visual.\n"
"\n"
" -h, --help Display this information and exit\n"
" -V, --version Display version information and exit\n"
" -d, --debug Output debug info\n"
" -t, --test Set default character set to CapRTL, turn on padding\n"
" --showinput Output the input string too\n"
" -C, --charset CS Specify character set, default is %s\n"
" --charsetdesc CS Show descreptions for character set CS, if any and exit\n"
" -p, --nopad Do not right justify RTL lines\n"
" -f, --fill Fill lines up to margin\n"
" -W, --width W Screem width for padding, default is %d\n"
" -B, --bol BOL Output string BOL before the visual string\n"
" -E, --eol EOL Output string EOL after the visual string\n"
" -R, --rtl Force base direction to RTL\n"
" -L, --ltr Force base direction to LTR\n"
" -c, --clean Remove explicit format codes in visual string\n"
" output, currently does not affect other outputs\n"
" --ltov Output Logical to Visual position map\n"
" --vtol Output Visual to Logical position map\n"
" --levels Output Embedding Levels\n"
" --changes Output information about changes between\n"
" logical and visual string (start, length)\n"
" --novisual Do not output the visual string, to be used\n"
" with --ltov, --vtol, --levels, --changes\n"
" Options affect only subsequent arguments\n"
"\n"
"Output:\n"
" For each line of input, output something like this:\n"
" [BOL][input-str` => '][[padding space]visual-str]\n"
" [\\n ltov-map][\\n vtol-map][\\n levels][\\n changes][EOL]\n"
"\n"
"Available character sets are:\n", appname, appname,
fribidi_char_set_name (char_set), text_width);
for (i = 1; i <= FRIBIDI_CHAR_SETS_NUM; i++)
printf (" * %-23s%-4s%s", fribidi_char_set_title (i),
(fribidi_char_set_desc (i) ? "X" : ""),
(i & 1 ? "" : "\n"));
if (FRIBIDI_CHAR_SETS_NUM & 1)
printf ("\n");
printf
(" X: Character set has descreptions, use --charsetdesc to see\n");
printf
("\nReport bugs online at <http://fribidi.sourceforge.net/bugs.php>.\n");
exit (0);
}
CASE2 ("-V", "--version")
{
printf
("%s %s\n"
"Copyright (C) 2001 FriBidi Project.\n"
"%s comes with NO WARRANTY, to the extent permitted by law.\n"
"You may redistribute copies of %s under the terms of\n"
"the GNU General Public License.\n"
"For more information about these matters, see the files name COPYING.\n",
appname, VERSION, appname, appname);
exit (0);
}
CASE2 ("-p", "--nopad")
{
do_pad = FALSE;
continue;
}
CASE2 ("-W", "--width")
{
text_width = atoi (NEXTARG);
continue;
}
CASE2 ("-E", "--eol")
{
eol_text = NEXTARG;
continue;
}
CASE2 ("-B", "--bol")
{
bol_text = NEXTARG;
continue;
}
CASE2 ("-R", "--rtl")
{
input_base_direction = FRIBIDI_TYPE_RTL;
continue;
}
CASE2 ("-L", "--ltr")
{
input_base_direction = FRIBIDI_TYPE_LTR;
continue;
}
CASE2 ("-f", "--fill")
{
do_fill = TRUE;
continue;
}
CASE2 ("-c", "--clean")
{
do_clean = TRUE;
continue;
}
CASE ("--showinput")
{
show_input = TRUE;
continue;
}
CASE ("--ltov")
{
show_ltov = TRUE;
continue;
}
CASE ("--vtol")
{
show_vtol = TRUE;
continue;
}
CASE ("--levels")
{
show_levels = TRUE;
continue;
}
CASE ("--changes")
{
show_changes = TRUE;
continue;
}
CASE2 ("-K", "--novisual")
{
show_visual = FALSE;
continue;
}
CASE2 ("-d", "--debug")
{
if (!fribidi_set_debug (TRUE))
die
("%s lib must be compiled with DEBUG option to enable\nturn debug info on.\n",
PACKAGE);
continue;
}
CASE2 ("-t", "--test")
{
char_set = FRIBIDI_CHARSET_CAP_RTL;
do_pad = TRUE;
continue;
}
CASE2 ("-C", "--charset")
{
S_ = NEXTARG;
char_set = fribidi_parse_charset (S_);
if (!char_set)
die ("unrecognized charset `%s'\n", S_);
continue;
}
CASE ("--charsetdesc")
{
guchar *comment;
S_ = NEXTARG;
char_set = fribidi_parse_charset (S_);
if (!char_set)
die ("unrecognized character set `%s'\n", S_);
S_ = fribidi_char_set_name (char_set);
comment = fribidi_char_set_desc (char_set);
if (!comment)
die ("no description available for character set `%s'\n", S_);
else
printf ("Descriptions for character set %s:\n"
"\n" "%s", fribidi_char_set_title (char_set), comment);
exit (0);
}
die ("unrecognized option `%s'\n", S_);
}
else
{
file_found = TRUE;
/* Open the infile for reading */
if (S_[0] == '-' && !S_[1])
{
IN = stdin;
}
else
{
IN = fopen (S_, "r");
if (!IN)
{
fprintf (stderr, "%s: %s: No such file or directory\n",
appname, S_);
continue;
}
}
/* Read and process input one line at a time */
{
guchar S_[MAX_STR_LEN];
gint padding_width;
if (show_input)
padding_width = (text_width - 10) / 2;
else
padding_width = text_width;
while (fgets (S_, sizeof (S_) - 1, IN))
{
int len;
char *new_line, *nl_found;
FriBidiChar logical[FRIBIDI_MAX_STRING_LENGTH];
FriBidiChar visual[FRIBIDI_MAX_STRING_LENGTH];
guchar outstring[MAX_STR_LEN];
FriBidiCharType base;
int i, j, k;
nl_found = "";
S_[sizeof (S_) - 1] = 0;
len = strlen (S_);
/* chop */
if (S_[len - 1] == '\n')
{
len--;
S_[len] = '\0';
new_line = "\n";
}
else
{
new_line = "";
}
len = fribidi_charset_to_unicode (char_set, S_, logical);
{
guint16 *ltov, *vtol;
guint8 *levels;
gint new_len;
if (show_ltov)
ltov = g_new (guint16, len + 1);
else
ltov = NULL;
if (show_vtol)
vtol = g_new (guint16, len + 1);
else
vtol = NULL;
if (show_levels)
levels = g_new (guint8, len + 1);
else
levels = NULL;
if (show_input)
{
printf ("%-*s => ", padding_width, S_);
}
/* Create a bidi string. */
base = input_base_direction;
fribidi_log2vis (logical, len, &base,
/* output */
visual, ltov, vtol, levels);
new_len = len;
if (show_visual)
{
printf (nl_found);
if (bol_text)
printf ("%s", bol_text);
/* Remove explicit marks, if asked for. */
if (do_clean)
len = fribidi_remove_explicits (visual, len);
/* Convert it to something to print. */
new_len = fribidi_unicode_to_charset (char_set, visual,
len, outstring);
if (base == FRIBIDI_TYPE_RTL && do_pad && *outstring)
{
j = strlen (outstring);
k = (j - 1) % padding_width + 1;
for (i = (j - 1) / padding_width - 1; i >= 0; i--)
printf ("%.*s", padding_width,
outstring + (i * padding_width + k));
printf ("%*.*s", padding_width, k, outstring);
}
else
{
printf ("%s", outstring);
if (do_fill)
{
j = strlen (outstring);
k = padding_width - j % padding_width;
if (k)
printf ("%*s", k, "");
}
}
if (eol_text)
printf ("%s", eol_text);
nl_found = "\n";
}
if (show_ltov)
{
gint i;
printf (nl_found);
for (i = 0; i < len; i++)
printf ("%d ", ltov[i]);
nl_found = "\n";
}
if (show_vtol)
{
gint i;
printf (nl_found);
for (i = 0; i < len; i++)
printf ("%d ", vtol[i]);
nl_found = "\n";
}
if (show_levels)
{
gint i;
printf (nl_found);
for (i = 0; i < len; i++)
printf ("%d ", levels[i]);
nl_found = "\n";
}
if (show_changes)
{
int change_start, change_len;
fribidi_find_string_changes (logical, len,
visual, new_len,
&change_start,
&change_len);
printf ("%sChange start[length] = %d[%d]",
nl_found, change_start, change_len);
nl_found = "\n";
}
if (show_ltov)
g_free (ltov);
if (show_vtol)
g_free (vtol);
if (show_levels)
g_free (levels);
}
if (*nl_found)
printf (new_line);
}
}
}
}
return 0;
}
|