Debian Codegraph @

bidiv.c

raw ◀ ▶
Source reconstructed at patch state 0 of 5 — not the installed (fully patched) source.
5 Debian patches touch this file
1 /* Bidiv: Bidi View
2
3 Converts a text file with bidirectional text (in iso8859-8 or UTF-8
4 encoding) into visual 8bit text (iso8859-8) (or UTF-8, depending on locale)
5 suitable for viewing on a constant-width-font text terminal (or piped to a
6 program like "less").
7
8 Usage: bidiv [options[ [file]...
9 (same semantics as cat(1))
10
11 TODO: This should be a general utility which is useful not only to Hebrew:
12 it should work for Arabic and other RL languages too.
13
14 Copyright (c) 2001-2006 Nadav Har'El
15 License: GNU General Public License version 2
16
17 This is version 1.5. See WHATSNEW file for version history.
18 */
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <unistd.h>
23 #include <fribidi/fribidi.h>
24
25 /* In the future, this should be done with autoconf */
26 #define HAVE_LOCALE
27
28 #ifdef HAVE_LOCALE
29 #include <locale.h>
30 #include <langinfo.h>
31 #include <string.h>
32 #endif
33
34 char *progname;
35
36 int width=80;
37 /* auto_basedir=0 doesn't change the base direction (it remains the base_dir
38 previously defined by a command line option, or automatically), =1 means
39 change the base direction on every newline, and =2 means change it every
40 paragraph (where a paragraph starts after a completely empty line.
41 */
42 FriBidiCharType base_dir=FRIBIDI_TYPE_N, prev_base_dir=FRIBIDI_TYPE_LTR;
43 int auto_basedir=2;
44 int justify=1;
45 int out_utf8=0;
46
47 void
48 bidiv(FILE *fp)
49 {
50 char *in, *out;
51 FriBidiChar *unicode_in, *unicode_out;
52 int len, i, c;
53 int rtl_line;
54
55 /* Note: rather than reading the entire line with gets, as we did
56 in the first version of this program, this program reads only
57 "width" character segments. There's a problem though: if we
58 can't decide the base direction from the first width characters
59 of a line (or paragraph), we don't know what to do. If this
60 because a real problem, we should in that case read in more
61 characters (up to some limit) until we do know what to do
62 */
63 int newline=1, newpara=1;
64
65 in=(char *)malloc(width+1);
66 out=(char *)malloc(width*7+1); /* 7 is the maximum number of
67 bytes in one UTF8 char? */
68 /* We use (width+1) not just width, to leave place for a double-
69 width null character, which we might or might not need to write
70 (we don't add one to unicode_in, but it appears that fribidi_log2vis
71 puts one (a single null byte?) in unicode_out). But it can't hurt
72 to be extra safe and leave 2 extra bytes on both. */
73 unicode_in=(FriBidiChar *)malloc(sizeof(FriBidiChar)*(width+1));
74 unicode_out=(FriBidiChar *)malloc(sizeof(FriBidiChar)*(width+1));
75
76 c=0;
77 while(c!=EOF){
78 if(auto_basedir==1){
79 if(newline){
80 if(base_dir!=FRIBIDI_TYPE_N)
81 prev_base_dir=base_dir;
82 base_dir=FRIBIDI_TYPE_N;
83 }
84 } else if(auto_basedir==2){
85 if(newpara){
86 if(base_dir!=FRIBIDI_TYPE_N)
87 prev_base_dir=base_dir;
88 base_dir=FRIBIDI_TYPE_N;
89 }
90 }
91 /* Get the next segment of a line, up to "width" characters
92 or a newline */
93 newpara=0;
94 for(len=0; len<width; len++){
95 /* Read one character. Currently this can either
96 be one byte either ASCII or ISO8859-8 (224..250)
97 or two bytes of UTF8 (such as Hebrew). Other
98 things (such as 3-byte utf8) are not supported
99 properly. This relative ugliness is because I
100 prefered not to have a seperate "input encoding"
101 option to bidiv. This is enabled when TRY_UTF8
102 is defined
103 */
104 #define TRY_UTF8
105 if((c=getc(fp))==EOF)
106 break;
107 else if(c=='\r'){
108 /* just ignore carriage returns... */
109 len--;
110 continue;
111 } else if(c=='\n'){
112 if(newline){
113 /* a newline after a new line,
114 i.e., an empty line */
115 newpara=1;
116 } else {
117 newline=1;
118 }
119 break;
120 #ifdef TRY_UTF8
121 } else if(c>0177 && c<0340) {
122 /* UTF8 2 byte character */
123 int c1;
124 if((c1=getc(fp))==EOF)
125 break;
126 /* if c1 doesn't make sense as a second utf8
127 character, the whole thing isn't utf8. But
128 c wasn't 8859-8, or we would have noticed
129 that sooner :( So this surely won't work :(
130 Maybe we should assume it's iso8859_1?
131 At least this hack prevents us gobbling two
132 characters as one invalid unicode char.
133 TODO: in this case, maybe turn on a "not
134 hebrew" flag, and stop filtering if both
135 input and output are iso?
136 */
137 if(c1<0x80||c1>0xbf){
138 ungetc(c1, fp);
139 unicode_in[len]=
140 fribidi_iso8859_8_to_unicode_c(c);
141 } else
142 unicode_in[len]=((c & 037) << 6) + (c1 & 077);
143 newline=0;
144 #endif
145 } else {
146 #ifdef TRY_UTF8
147 /* 240-256 (0341-0377) - presumably 8859-8.
148 In the future we will have a language
149 option, which will control this (as well
150 as the output encoding). */
151 unicode_in[len]=
152 fribidi_iso8859_8_to_unicode_c(c);
153 #else
154 in[len]=c;
155 #endif
156 newline=0;
157 }
158 }
159 if(len==0){
160 if(newpara){
161 putchar('\n');
162 continue;
163 } else {
164 /* this is a case where we had "width" characters
165 followed by a newline; In the previous iteration we
166 outputted these characters, and a newline, and now
167 we come to this newline, and we don't need to output
168 it again!
169 */
170 continue;
171 }
172 }
173 #ifndef TRY_UTF8
174 in[len]='\0';
175 fribidi_iso8859_8_to_unicode(in, unicode_in);
176 #endif
177
178 /* output the line */
179 fribidi_log2vis(unicode_in, len, &base_dir,
180 unicode_out, NULL, NULL, NULL);
181 /* if base_dir is still FRIBIDI_TYPE_N (we haven't found
182 any strong-direction character in this paragraph (or
183 something else - depending on option) - we prefer the
184 base direction of the previous paragraph (prev_base_dir)
185 to the arbitrary FRIBIDI_TYPE_N
186 */
187 if(base_dir==FRIBIDI_TYPE_N){
188 #if 0
189 base_dir=prev_base_dir;
190 fribidi_log2vis(unicode_in, len, &base_dir,
191 unicode_out, NULL, NULL, NULL);
192 #else
193 FriBidiCharType tmp_dir=prev_base_dir;
194 fribidi_log2vis(unicode_in, len, &tmp_dir,
195 unicode_out, NULL, NULL, NULL);
196 rtl_line= (tmp_dir==FRIBIDI_TYPE_RTL);
197 #endif
198 } else if(base_dir==FRIBIDI_TYPE_RTL)
199 rtl_line=1;
200 else
201 rtl_line=0;
202
203 if(out_utf8)
204 fribidi_unicode_to_utf8(unicode_out, len,
205 out);
206 else
207 fribidi_unicode_to_iso8859_8(unicode_out, len,
208 out);
209 /* if rtl_line (i.e., base_dir is RL), and we didn't fill the
210 entire width, we need to pad with spaces. Maybe in the
211 future this should be an option.
212 */
213 if(justify && rtl_line && len<width)
214 for(i=len; i<width; i++)
215 putchar(' ');
216 puts(out);
217 }
218 /* Free the memory we have allocated */
219 free(in);
220 free(out);
221 free(unicode_in);
222 free(unicode_out);
223 }
224
225 int
226 main(int argc, char *argv[])
227 {
228 FILE *fp;
229 char *s;
230 int i,c;
231 int status=0;
232
233 /* TODO: get width from tty setup, COLUMNS variable, and/or
234 command line option */
235 progname=argv[0];
236
237 /* The COLUMNS variable (set by zsh and bash, for example)
238 overrides the default 'width'.
239 TODO: also try to read the column number directly from the tty.
240 */
241 if((s=getenv("COLUMNS"))){
242 i=atoi(s);
243 if(i>0)
244 width=i;
245 }
246
247 #ifdef HAVE_LOCALE
248 setlocale(LC_CTYPE, "");
249 out_utf8= !strcmp(nl_langinfo(CODESET),"UTF-8");
250 #endif
251
252 /* parse command line options */
253 while((c=getopt(argc, argv, "w:lpj"))!=-1){
254 switch(c){
255 case 'w':
256 /* set line width*/
257 width=atoi(optarg);
258 if(width<=0){
259 fprintf(stderr,
260 "%s: width argument must be positive: %s.\n",
261 progname, optarg);
262 return 2;
263 }
264 break;
265 case 'l':
266 /* choose direction every line */
267 auto_basedir=1;
268 break;
269 case 'p':
270 /* choose direction every paragraph */
271 auto_basedir=2;
272 break;
273 case 'j':
274 /* do not justify (pad with spaces) RTL lines */
275 justify=0;
276 break;
277 case '?': case ':':
278 fprintf(stderr,
279 "usage: %s [-plj] [-w width] [file]...\n",
280 progname);
281 return 2;
282 break;
283 default:
284 fprintf(stderr,"internal error...\n");
285 return 2;
286 }
287 }
288 argv+=optind;
289 argc-=optind;
290
291
292 if (argc>0){
293 int i;
294 for(i=0;i<argc;i++){
295 if((fp=fopen(argv[i],"r"))==NULL){
296 perror(argv[i]);
297 status=1;
298 } else {
299 bidiv(fp);
300 fclose(fp);
301 }
302 }
303 } else {
304 bidiv(stdin);
305 }
306 return status;
307 }