2 * fribidi-main.c - command line program for libfribidi
5 * Behdad Esfahbod, 2001, 2002, 2004
6 * Dov Grobgeld, 1999, 2000
8 * Copyright (C) 2004 Sharif FarsiWeb, Inc
9 * Copyright (C) 2001,2002 Behdad Esfahbod
10 * Copyright (C) 1999,2000 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>.
33 #include <fribidi-deprecated.h>
50 # if STDC_HEADERS && HAVE_MEMORY_H
62 #define appname "fribidi"
64 #define MAX_STR_LEN 65000
67 #define ALLOCATE(tp,ln) ((tp *) fribidi_malloc (sizeof (tp) * (ln)))
75 fprintf (stderr, "%s: ", appname);
77 fprintf (stderr, fmt, arg);
78 fprintf (stderr, "Try `%s --help' for more information.\n", appname);
82 #define die1(msg) die2("%s", msg)
84 fribidi_boolean do_break, do_pad, do_mirror, do_reorder_nsm, do_clean;
85 fribidi_boolean show_input, show_visual, show_basedir;
86 fribidi_boolean show_ltov, show_vtol, show_levels;
87 const int default_text_width = 80;
90 const char *bol_text, *eol_text;
91 FriBidiParType input_base_direction;
99 /* Break help string into little ones, to assure ISO C89 conformance */
100 printf ("Usage: " appname " [OPTION]... [FILE]...\n"
101 "A command line interface for the " FRIBIDI_NAME " library.\n"
102 "Convert a logical string to visual.\n"
104 " -h, --help Display this information and exit\n"
105 " -V, --version Display version information and exit\n"
106 " -v, --verbose Verbose mode, same as --basedir --ltov --vtol\n"
107 " --levels --changes\n");
108 printf (" -d, --debug Output debug information\n"
109 " -t, --test Test " FRIBIDI_NAME
110 ", same as --clean --nobreak\n"
111 " --showinput --reordernsm --width %d\n",
113 printf (" -c, --charset CS Specify character set, default is %s\n"
114 " --charsetdesc CS Show descriptions for character set CS and exit\n"
115 " --caprtl Old style: set character set to CapRTL\n",
117 printf (" --showinput Output the input string too\n"
118 " --nopad Do not right justify RTL lines\n"
119 " --nobreak Do not break long lines\n"
120 " -w, --width W Screen width for padding, default is %d, but if\n"
121 " environment variable COLUMNS is defined, its value\n"
122 " will be used, --width overrides both of them.\n",
125 (" -B, --bol BOL Output string BOL before the visual string\n"
126 " -E, --eol EOL Output string EOL after the visual string\n"
127 " --rtl Force base direction to RTL\n"
128 " --ltr Force base direction to LTR\n"
129 " --wrtl Set base direction to RTL if no strong character found\n");
131 (" --wltr Set base direction to LTR if no strong character found\n"
133 " --nomirror Turn mirroring off, to do it later\n"
134 " --reordernsm Reorder NSM sequences to follow their base character\n"
135 " --clean Remove explicit format codes in visual string\n"
136 " output, currently does not affect other outputs\n"
137 " --basedir Output Base Direction\n");
138 printf (" --ltov Output Logical to Visual position map\n"
139 " --vtol Output Visual to Logical position map\n"
140 " --levels Output Embedding Levels\n"
141 " --novisual Do not output the visual string, to be used with\n"
142 " --basedir, --ltov, --vtol, --levels, --changes\n");
143 printf (" All string indexes are zero based\n" "\n" "Output:\n"
144 " For each line of input, output something like this:\n"
145 " [input-str` => '][BOL][[padding space]visual-str][EOL]\n"
146 " [\\n base-dir][\\n ltov-map][\\n vtol-map][\\n levels][\\n changes]\n");
150 printf ("\n" "Available character sets:\n");
151 for (i = 1; i <= FRIBIDI_CHAR_SETS_NUM; i++)
152 printf (" * %-10s: %-25s%1s\n",
153 fribidi_char_set_name (i), fribidi_char_set_title (i),
154 (fribidi_char_set_desc (i) ? "X" : ""));
156 (" X: Character set has descriptions, use --charsetdesc to see\n");
159 printf ("\nReport bugs online at\n<" FRIBIDI_BUGREPORT ">.\n");
168 printf (appname " %s", fribidi_version_info);
179 m = fribidi_malloc (strlen (s) + 1);
195 fribidi_boolean file_found;
199 text_width = default_text_width;
204 do_reorder_nsm = false;
207 show_basedir = false;
214 input_base_direction = FRIBIDI_PAR_ON;
216 if ((s = (char *) getenv ("COLUMNS")))
225 #define CHARSETDESC 257
228 /* Parse the command line with getopt library */
229 /* Must set argv[0], getopt uses it to generate error messages */
233 int option_index = 0, c;
234 static struct option long_options[] = {
236 {"version", 0, 0, 'V'},
237 {"verbose", 0, 0, 'v'},
238 {"debug", 0, 0, 'd'},
240 {"charset", 1, 0, 'c'},
241 {"charsetdesc", 1, 0, CHARSETDESC},
242 {"caprtl", 0, 0, CAPRTL},
243 {"showinput", 0, (int *) (void *) &show_input, true},
244 {"nopad", 0, (int *) (void *) &do_pad, false},
245 {"nobreak", 0, (int *) (void *) &do_break, false},
246 {"width", 1, 0, 'w'},
249 {"nomirror", 0, (int *) (void *) &do_mirror, false},
250 {"reordernsm", 0, (int *) (void *) &do_reorder_nsm, true},
251 {"clean", 0, (int *) (void *) &do_clean, true},
252 {"ltr", 0, (int *) (void *) &input_base_direction, FRIBIDI_PAR_LTR},
253 {"rtl", 0, (int *) (void *) &input_base_direction, FRIBIDI_PAR_RTL},
254 {"wltr", 0, (int *) (void *) &input_base_direction,
256 {"wrtl", 0, (int *) (void *) &input_base_direction,
258 {"basedir", 0, (int *) (void *) &show_basedir, true},
259 {"ltov", 0, (int *) (void *) &show_ltov, true},
260 {"vtol", 0, (int *) (void *) &show_vtol, true},
261 {"levels", 0, (int *) (void *) &show_levels, true},
262 {"novisual", 0, (int *) (void *) &show_visual, false},
267 getopt_long (argc, argv, "hVvdtc:w:B:E:", long_options,
283 show_basedir = show_ltov = show_vtol = show_levels = true;
286 text_width = atoi (optarg);
288 die2 ("invalid screen width `%s'\n", optarg);
297 if (!fribidi_set_debug (true))
300 " must be compiled with DEBUG option to enable\nturn debug info on.\n");
303 do_clean = show_input = do_reorder_nsm = true;
305 text_width = default_text_width;
308 char_set = my_fribidi_strdup (optarg);
310 die1 ("memory allocation failed for char_set!");
317 char_set_num = fribidi_parse_charset (char_set);
319 die2 ("unrecognized character set `%s'\n", char_set);
320 if (!fribidi_char_set_desc (char_set_num))
321 die2 ("no description available for character set `%s'\n",
322 fribidi_char_set_name (char_set_num));
324 printf ("Descriptions for character set %s:\n"
325 "\n" "%s", fribidi_char_set_title (char_set_num),
326 fribidi_char_set_desc (char_set_num));
338 char_set_num = fribidi_parse_charset (char_set);
341 die2 ("unrecognized character set `%s'\n", char_set);
343 FRIBIDI_BEGIN_IGNORE_DEPRECATIONS
344 fribidi_set_mirroring (do_mirror);
345 fribidi_set_reorder_nsm (do_reorder_nsm);
346 FRIBIDI_END_IGNORE_DEPRECATIONS
349 while (optind < argc || !file_found)
351 const char *filename;
353 filename = optind < argc ? argv[optind++] : "-";
356 /* Open the infile for reading */
357 if (filename[0] == '-' && !filename[1])
363 IN = fopen (filename, "r");
366 fprintf (stderr, "%s: %s: no such file or directory\n",
373 /* Read and process input one line at a time */
375 char S_[MAX_STR_LEN];
376 int padding_width, break_width;
378 padding_width = show_input ? (text_width - 10) / 2 : text_width;
379 break_width = do_break ? padding_width : 3 * MAX_STR_LEN;
381 while (fgets (S_, sizeof (S_) - 1, IN))
383 const char *new_line, *nl_found;
384 FriBidiChar logical[MAX_STR_LEN];
385 char outstring[MAX_STR_LEN];
390 S_[sizeof (S_) - 1] = 0;
393 if (S_[len - 1] == '\n')
401 /* TODO: handle \r */
403 len = fribidi_charset_to_unicode (char_set_num, S_, len, logical);
407 FriBidiStrIndex *ltov, *vtol;
408 FriBidiLevel *levels;
409 fribidi_boolean log2vis;
411 visual = show_visual ? ALLOCATE (FriBidiChar,
414 ltov = show_ltov ? ALLOCATE (FriBidiStrIndex,
417 vtol = show_vtol ? ALLOCATE (FriBidiStrIndex,
420 levels = show_levels ? ALLOCATE (FriBidiLevel,
424 /* Create a bidi string. */
425 base = input_base_direction;
427 log2vis = fribidi_log2vis (logical, len, &base,
429 visual, ltov, vtol, levels);
435 printf ("%-*s => ", padding_width, S_);
437 /* Remove explicit marks, if asked for. */
441 fribidi_remove_bidi_marks (visual, len, ltov, vtol,
446 printf ("%s", nl_found);
449 printf ("%s", bol_text);
451 /* Convert it to input charset and print. */
453 FriBidiStrIndex idx, st;
454 for (idx = 0; idx < len;)
456 FriBidiStrIndex wid, inlen;
460 if (char_set_num != FRIBIDI_CHAR_SET_CAP_RTL)
461 while (wid > 0 && idx < len)
464 FRIBIDI_IS_EXPLICIT_OR_ISOLATE_OR_BN_OR_NSM
465 (fribidi_get_bidi_type (visual[idx])) ? 0
470 while (wid > 0 && idx < len)
475 if (wid < 0 && idx - st > 1)
479 fribidi_unicode_to_charset (char_set_num,
482 if (FRIBIDI_IS_RTL (base))
484 (int) (do_pad ? (padding_width +
490 printf ("%s", outstring);
496 printf ("%s", eol_text);
502 printf ("%s", nl_found);
503 printf ("Base direction: %s",
504 (FRIBIDI_DIR_TO_LEVEL (base) ? "R" : "L"));
511 printf ("%s", nl_found);
512 for (i = 0; i < len; i++)
513 printf ("%ld ", (long) ltov[i]);
520 printf ("%s", nl_found);
521 for (i = 0; i < len; i++)
522 printf ("%ld ", (long) vtol[i]);
529 printf ("%s", nl_found);
530 for (i = 0; i < len; i++)
531 printf ("%d ", (int) levels[i]);
551 printf ("%s", new_line);
559 /* Editor directions:
560 * vim:textwidth=78:tabstop=8:shiftwidth=2:autoindent:cindent