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 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622
|
/* FriBidi
* fribidi-main.c - command line program for libfribidi
*
* $Id: fribidi-main.c,v 1.15 2006/01/31 03:23:12 behdad Exp $
* $Author: behdad $
* $Date: 2006/01/31 03:23:12 $
* $Revision: 1.15 $
* $Source: /cvs/fribidi/fribidi2/bin/fribidi-main.c,v $
*
* Authors:
* Behdad Esfahbod, 2001, 2002, 2004
* Dov Grobgeld, 1999, 2000
*
* Copyright (C) 2004 Sharif FarsiWeb, Inc
* Copyright (C) 2001,2002 Behdad Esfahbod
* Copyright (C) 1999,2000 Dov Grobgeld
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library, in a file named COPYING; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307, USA
*
* For licensing issues, contact <license@farsiweb.info>.
*/
#include <common.h>
#include <fribidi.h>
#if FRIBIDI_CHARSETS+0
#else
# if FRIBIDI_MAIN_USE_ICONV_H
# include <iconv.h>
# else /* !FRIBIDI_MAIN_USE_ICONV_H */
# include <fribidi-char-sets.h>
# endif /* FRIBIDI_MAIN_USE_ICONV_H */
#endif /* !FRIBIDI_CHARSETS */
#include <stdio.h>
#if STDC_HEADERS+0
# include <stdlib.h>
# include <stddef.h>
#else
# if HAVE_STDLIB_H
# include <stdlib.h>
# endif
#endif
#if HAVE_STRING_H+0
# if STDC_HEADERS && HAVE_MEMORY_H
# else
# include <memory.h>
# endif
# include <string.h>
#endif
#if HAVE_STRINGS_H+0
# include <strings.h>
#endif
#include "getopt.h"
#define appname "fribidi"
#define MAX_STR_LEN 65000
#define ALLOCATE(tp,ln) ((tp *) fribidi_malloc (sizeof (tp) * (ln)))
static void
die2 (
const char *fmt,
const char *arg
)
{
fprintf (stderr, "%s: ", appname);
if (fmt)
fprintf (stderr, fmt, arg);
fprintf (stderr, "Try `%s --help' for more information.\n", appname);
exit (-1);
}
#define die1(msg) die2("%s", msg)
fribidi_boolean do_break, do_pad, do_mirror, do_reorder_nsm, do_clean;
fribidi_boolean show_input, show_visual, show_basedir;
fribidi_boolean show_ltov, show_vtol, show_levels;
int text_width;
const char *char_set;
const char *bol_text, *eol_text;
FriBidiParType input_base_direction;
#if FRIBIDI_MAIN_USE_ICONV_H+0
iconv_t to_ucs4, from_ucs4;
#else /* !FRIBIDI_MAIN_USE_ICONV_H */
int char_set_num;
#endif /* !FRIBIDI_MAIN_USE_ICONV_H */
static void
help (
void
)
{
/* Break help string into little ones, to assure ISO C89 conformance */
printf ("Usage: " appname " [OPTION]... [FILE]...\n"
"A command line interface for the " FRIBIDI_NAME " library.\n"
"Convert a logical string to visual.\n"
"\n"
" -h, --help Display this information and exit\n"
" -V, --version Display version information and exit\n"
" -v, --verbose Verbose mode, same as --basedir --ltov --vtol\n"
" --levels --changes\n");
printf (" -d, --debug Output debug information\n"
" -t, --test Test " FRIBIDI_NAME
", same as --clean --nobreak\n"
" --showinput --reordernsm\n");
#if FRIBIDI_MAIN_USE_ICONV_H+0
printf (" -c, --charset CS Specify character set, default is %s.\n"
" CS should be a valid iconv character set name\n",
char_set);
#else /* !FRIBIDI_MAIN_USE_ICONV_H */
printf (" -c, --charset CS Specify character set, default is %s\n"
" --charsetdesc CS Show descriptions for character set CS and exit\n"
" --caprtl Old style: set character set to CapRTL\n",
char_set);
#endif /* !FRIBIDI_MAIN_USE_ICONV_H */
printf (" --showinput Output the input string too\n"
" --nopad Do not right justify RTL lines\n"
" --nobreak Do not break long lines\n"
" -w, --width W Screen width for padding, default is %d, but if\n"
" environment variable COLUMNS is defined, its value\n"
" will be used, --width overrides both of them.\n",
text_width);
printf
(" -B, --bol BOL Output string BOL before the visual string\n"
" -E, --eol EOL Output string EOL after the visual string\n"
" --rtl Force base direction to RTL\n"
" --ltr Force base direction to LTR\n"
" --wrtl Set base direction to RTL if no strong character found\n");
printf
(" --wltr Set base direction to LTR if no strong character found\n"
" (default)\n"
" --nomirror Turn mirroring off, to do it later\n"
" --reordernsm Reorder NSM sequences to follow their base character\n"
" --clean Remove explicit format codes in visual string\n"
" output, currently does not affect other outputs\n"
" --basedir Output Base Direction\n");
printf (" --ltov Output Logical to Visual position map\n"
" --vtol Output Visual to Logical position map\n"
" --levels Output Embedding Levels\n"
" --novisual Do not output the visual string, to be used with\n"
" --basedir, --ltov, --vtol, --levels, --changes\n");
printf (" All string indexes are zero based\n" "\n" "Output:\n"
" For each line of input, output something like this:\n"
" [input-str` => '][BOL][[padding space]visual-str][EOL]\n"
" [\\n base-dir][\\n ltov-map][\\n vtol-map][\\n levels][\\n changes]\n");
#if FRIBIDI_MAIN_USE_ICONV_H+0
#else
{
int i;
printf ("\n" "Available character sets:\n");
for (i = 1; i <= FRIBIDI_CHAR_SETS_NUM; i++)
printf (" * %-10s: %-25s%1s\n",
fribidi_char_set_name (i), fribidi_char_set_title (i),
(fribidi_char_set_desc (i) ? "X" : ""));
printf
(" X: Character set has descriptions, use --charsetdesc to see\n");
}
#endif /* !FRIBIDI_MAIN_USE_ICONV_H */
printf ("\nReport bugs online at\n<" FRIBIDI_BUGREPORT ">.\n");
exit (0);
}
static void
version (
void
)
{
printf (appname " %s", fribidi_version_info);
exit (0);
}
static char *
my_fribidi_strdup (
char *s
)
{
char *m;
m = fribidi_malloc (strlen (s) + 1);
if (!m)
return NULL;
strcpy (m, s);
return m;
}
int
main (
int argc,
char *argv[]
)
{
int exit_val;
fribidi_boolean file_found;
char *s;
FILE *IN;
text_width = 80;
do_break = true;
do_pad = true;
do_mirror = true;
do_clean = false;
do_reorder_nsm = false;
show_input = false;
show_visual = true;
show_basedir = false;
show_ltov = false;
show_vtol = false;
show_levels = false;
char_set = "UTF-8";
bol_text = NULL;
eol_text = NULL;
input_base_direction = FRIBIDI_PAR_ON;
if ((s = (char *) getenv ("COLUMNS")))
{
int i;
i = atoi (s);
if (i > 0)
text_width = i;
}
#define CHARSETDESC 257
#define CAPRTL 258
/* Parse the command line with getopt library */
/* Must set argv[0], getopt uses it to generate error messages */
argv[0] = appname;
while (1)
{
int option_index = 0, c;
static struct option long_options[] = {
{"help", 0, 0, 'h'},
{"version", 0, 0, 'V'},
{"verbose", 0, 0, 'v'},
{"debug", 0, 0, 'd'},
{"test", 0, 0, 't'},
{"charset", 1, 0, 'c'},
#if FRIBIDI_MAIN_USE_ICONV_H+0
#else
{"charsetdesc", 1, 0, CHARSETDESC},
{"caprtl", 0, 0, CAPRTL},
#endif /* FRIBIDI_MAIN_USE_ICONV_H */
{"showinput", 0, (int *) (void *) &show_input, true},
{"nopad", 0, (int *) (void *) &do_pad, false},
{"nobreak", 0, (int *) (void *) &do_break, false},
{"width", 1, 0, 'w'},
{"bol", 1, 0, 'B'},
{"eol", 1, 0, 'E'},
{"nomirror", 0, (int *) (void *) &do_mirror, false},
{"reordernsm", 0, (int *) (void *) &do_reorder_nsm, true},
{"clean", 0, (int *) (void *) &do_clean, true},
{"ltr", 0, (int *) (void *) &input_base_direction, FRIBIDI_PAR_LTR},
{"rtl", 0, (int *) (void *) &input_base_direction, FRIBIDI_PAR_RTL},
{"wltr", 0, (int *) (void *) &input_base_direction,
FRIBIDI_PAR_WLTR},
{"wrtl", 0, (int *) (void *) &input_base_direction,
FRIBIDI_PAR_WRTL},
{"basedir", 0, (int *) (void *) &show_basedir, true},
{"ltov", 0, (int *) (void *) &show_ltov, true},
{"vtol", 0, (int *) (void *) &show_vtol, true},
{"levels", 0, (int *) (void *) &show_levels, true},
{"novisual", 0, (int *) (void *) &show_visual, false},
{0, 0, 0, 0}
};
c =
getopt_long (argc, argv, "hVvdtc:w:B:E:", long_options,
&option_index);
if (c == -1)
break;
switch (c)
{
case 0:
break;
case 'h':
help ();
break;
case 'V':
version ();
break;
case 'v':
show_basedir = show_ltov = show_vtol = show_levels = true;
break;
case 'w':
text_width = atoi (optarg);
if (text_width <= 0)
die2 ("invalid screen width `%s'\n", optarg);
break;
case 'B':
bol_text = optarg;
break;
case 'E':
eol_text = optarg;
break;
case 'd':
if (!fribidi_set_debug (true))
die1
("lib" FRIBIDI
" must be compiled with DEBUG option to enable\nturn debug info on.\n");
break;
case 't':
do_clean = show_input = do_reorder_nsm = true;
do_break = false;
break;
case 'c':
char_set = my_fribidi_strdup (optarg);
if (!char_set)
die1 ("memory allocation failed for char_set!");
break;
#if FRIBIDI_MAIN_USE_ICONV_H+0
#else
case CAPRTL:
char_set = "CapRTL";
break;
case CHARSETDESC:
char_set = optarg;
char_set_num = fribidi_parse_charset (char_set);
if (!char_set_num)
die2 ("unrecognized character set `%s'\n", char_set);
if (!fribidi_char_set_desc (char_set_num))
die2 ("no description available for character set `%s'\n",
fribidi_char_set_name (char_set_num));
else
printf ("Descriptions for character set %s:\n"
"\n" "%s", fribidi_char_set_title (char_set_num),
fribidi_char_set_desc (char_set_num));
exit (0);
break;
#endif /* !FRIBIDI_MAIN_USE_ICONV_H */
case ':':
case '?':
die2 (NULL, NULL);
break;
default:
break;
}
}
#if FRIBIDI_MAIN_USE_ICONV_H+0
to_ucs4 = iconv_open ("WCHAR_T", char_set);
from_ucs4 = iconv_open (char_set, "WCHAR_T");
#else /* !FRIBIDI_MAIN_USE_ICONV_H */
char_set_num = fribidi_parse_charset (char_set);
#endif /* !FRIBIDI_MAIN_USE_ICONV_H */
#if FRIBIDI_MAIN_USE_ICONV_H+0
if (to_ucs4 == (iconv_t) (-1) || from_ucs4 == (iconv_t) (-1))
#else /* !FRIBIDI_MAIN_USE_ICONV_H */
if (!char_set_num)
#endif /* !FRIBIDI_MAIN_USE_ICONV_H */
die2 ("unrecognized character set `%s'\n", char_set);
fribidi_set_mirroring (do_mirror);
fribidi_set_reorder_nsm (do_reorder_nsm);
exit_val = 0;
file_found = false;
while (optind < argc || !file_found)
{
const char *filename;
filename = optind < argc ? argv[optind++] : "-";
file_found = true;
/* Open the infile for reading */
if (filename[0] == '-' && !filename[1])
{
IN = stdin;
}
else
{
IN = fopen (filename, "r");
if (!IN)
{
fprintf (stderr, "%s: %s: no such file or directory\n",
appname, filename);
exit_val = 1;
continue;
}
}
/* Read and process input one line at a time */
{
char S_[MAX_STR_LEN];
int padding_width, break_width;
padding_width = show_input ? (text_width - 10) / 2 : text_width;
break_width = do_break ? padding_width : 3 * MAX_STR_LEN;
while (fgets (S_, sizeof (S_) - 1, IN))
{
const char *new_line, *nl_found;
FriBidiChar logical[MAX_STR_LEN];
char outstring[MAX_STR_LEN];
FriBidiParType base;
FriBidiStrIndex len;
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 = "";
/* TODO: handle \r */
#if FRIBIDI_MAIN_USE_ICONV_H+0
{
char *st = S_, *ust = (char *) logical;
int in_len = (int) len;
len = sizeof logical;
iconv (to_ucs4, &st, &in_len, &ust, (int *) &len);
len = (FriBidiChar *) ust - logical;
}
#else /* !FRIBIDI_MAIN_USE_ICONV_H */
len = fribidi_charset_to_unicode (char_set_num, S_, len, logical);
#endif /* !FRIBIDI_MAIN_USE_ICONV_H */
{
FriBidiChar *visual;
FriBidiStrIndex *ltov, *vtol;
FriBidiLevel *levels;
FriBidiStrIndex new_len;
fribidi_boolean log2vis;
visual = show_visual ? ALLOCATE (FriBidiChar,
len + 1
) : NULL;
ltov = show_ltov ? ALLOCATE (FriBidiStrIndex,
len + 1
) : NULL;
vtol = show_vtol ? ALLOCATE (FriBidiStrIndex,
len + 1
) : NULL;
levels = show_levels ? ALLOCATE (FriBidiLevel,
len + 1
) : NULL;
/* Create a bidi string. */
base = input_base_direction;
log2vis = fribidi_log2vis (logical, len, &base,
/* output */
visual, ltov, vtol, levels);
if (log2vis)
{
if (show_input)
printf ("%-*s => ", padding_width, S_);
new_len = len;
/* Remove explicit marks, if asked for. */
if (do_clean)
len =
fribidi_remove_bidi_marks (visual, len, ltov, vtol,
levels);
if (show_visual)
{
printf ("%s", nl_found);
if (bol_text)
printf ("%s", bol_text);
/* Convert it to input charset and print. */
{
FriBidiStrIndex idx, st;
for (idx = 0; idx < len;)
{
FriBidiStrIndex wid, inlen;
wid = break_width;
st = idx;
#if FRIBIDI_MAIN_USE_ICONV_H+0
#else
if (char_set_num != FRIBIDI_CHAR_SET_CAP_RTL)
#endif /* !FRIBIDI_MAIN_USE_ICONV_H */
while (wid > 0 && idx < len)
{
wid -=
FRIBIDI_IS_EXPLICIT_OR_BN_OR_NSM
(fribidi_get_bidi_type (visual[idx])) ? 0
: 1;
idx++;
}
#if FRIBIDI_MAIN_USE_ICONV_H+0
#else
else
while (wid > 0 && idx < len)
{
wid--;
idx++;
}
#endif /* !FRIBIDI_MAIN_USE_ICONV_H */
if (wid < 0 && idx > st + 1)
idx--;
inlen = idx - st;
#if FRIBIDI_MAIN_USE_ICONV_H+0
{
char *str = outstring, *ust =
(char *) (visual + st);
int in_len = inlen * sizeof visual[0];
new_len = sizeof outstring;
iconv (from_ucs4, &ust, &in_len, &str,
(int *) &new_len);
*str = '\0';
new_len = str - outstring;
}
#else /* !FRIBIDI_MAIN_USE_ICONV_H */
new_len =
fribidi_unicode_to_charset (char_set_num,
visual + st, inlen,
outstring);
#endif /* !FRIBIDI_MAIN_USE_ICONV_H */
if (FRIBIDI_IS_RTL (base))
printf ("%*s",
(int) (do_pad ? (padding_width +
strlen (outstring) -
(break_width -
wid)) : 0),
outstring);
else
printf ("%s", outstring);
if (idx < len)
printf ("\n");
}
}
if (eol_text)
printf ("%s", eol_text);
nl_found = "\n";
}
if (show_basedir)
{
printf ("%s", nl_found);
printf ("Base direction: %s",
(FRIBIDI_DIR_TO_LEVEL (base) ? "R" : "L"));
nl_found = "\n";
}
if (show_ltov)
{
FriBidiStrIndex i;
printf ("%s", nl_found);
for (i = 0; i < len; i++)
printf ("%ld ", (long) ltov[i]);
nl_found = "\n";
}
if (show_vtol)
{
FriBidiStrIndex i;
printf ("%s", nl_found);
for (i = 0; i < len; i++)
printf ("%ld ", (long) vtol[i]);
nl_found = "\n";
}
if (show_levels)
{
FriBidiStrIndex i;
printf ("%s", nl_found);
for (i = 0; i < len; i++)
printf ("%d ", (int) levels[i]);
nl_found = "\n";
}
}
else
{
exit_val = 2;
}
if (show_visual)
free (visual);
if (show_ltov)
free (ltov);
if (show_vtol)
free (vtol);
if (show_levels)
free (levels);
}
if (*nl_found)
printf (new_line);
}
}
}
return exit_val;
}
/* Editor directions:
* vim:textwidth=78:tabstop=8:shiftwidth=2:autoindent:cindent
*/
|