summaryrefslogtreecommitdiff
path: root/support/fribidixetex/src/fribidixetex.c
blob: 0df17b4e033a8a9f216cc5feef235632bff3ba1f (plain)
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
/*********************************/
/* Copyright Vafa Khalighi 2016 */
/* Copyright Khaled Hosny 2015 */
/* Copyright Artyom Tonkikh 2007 */
/* License GPL                   */
/*********************************/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <fribidi/fribidi.h>

#include "fribidixetex-defines.h"
#include "fribidixetex-io.h"
#include "fribidixetex-bidi.h"

void help(void)
{
	fprintf(stderr,
			"usage: fribidixetex [ parameters ] [ inputfilename ]\n"
			"       -o file_name.tex             - output file name\n"
			"       -e utf8 | iso8859-8 | cp1255 - encoding\n"
			"       -t utf8 | iso8859-8 | cp1255 - output encoding\n"
			"       -d       transalte only - not apply bidi\n"
			"                this is usefull for latex2html that has native\n"
			"                bidirectional support\n"
			"       -m       replace '--'  &   '---'\n"
			"                by   '\\fribidixetexLRE{--} & \\fribidixetexLRE{'---'}\n"
			"       -n       no mirroring - do not mirror parethesis\n"
			"                for engines that do that natively (like XeTeX)\n"
	);
	exit(1);
}


/* Read cmd line parameters */
void read_parameters(int argc,char **argv,
					char **fname_in,char **fname_out,
					int *encoding,int *out_encoding,
					int *replace_minus,int *transalte_only,
					int *no_mirroring)
{
	int i;
	int cnt1=0,cnt2=0,cnt3=0,cnt4=0;
	int *ptr;

	for(i=1;i<argc;i++){
		if(strcmp(argv[i],"-h")==0) {
			help();
		}
		else if(strcmp(argv[i],"-d")==0) {
			*transalte_only = 1;
		}
		else if(strcmp(argv[i],"-m")==0) {
			*replace_minus = 1;
		}
		else if(strcmp(argv[i],"-n")==0) {
			*no_mirroring = 1;
		}
		else if(strcmp(argv[i],"-o")==0) {
			i++;
			if(i>=argc){
				help();
			}
			*fname_out = argv[i];
			cnt1++;
		}
		else if(strcmp(argv[i],"-e")==0 || strcmp(argv[i],"-t")==0) {
			if(i+1>=argc){
				help();
			}

			if(argv[i][1]=='e'){
				ptr=encoding;
				cnt2++;
			}
			else{
				ptr=out_encoding;
				cnt4++;
			}
			i++;
			if(strcmp(argv[i],"utf8")==0) {
				*ptr=ENC_UTF_8;
			}
			else if(strcmp(argv[i],"cp1255")==0) {
				*ptr=ENC_CP1255;
			}
			else if(strcmp(argv[i],"iso8859-8")==0) {
				*ptr=ENC_ISO_8859_8;
			}
			else {
				help();
			}
		}
		else {
			*fname_in=argv[i];
			cnt3++;
		}
	}
	if(cnt1>1 || cnt2>1 || cnt3>1 || cnt4>1){
		help();
	}
}

/* Global buffers */

static FriBidiChar text_line_in[MAX_LINE_SIZE];
static FriBidiChar text_line_out[MAX_LINE_SIZE];

/****************************
 ******** M A I N ***********
 ****************************/
int main(int argc,char **argv)
{
	char *fname_in=NULL,*fname_out=NULL;
	int encoding=ENC_DEFAULT,out_encoding = -1;
	int replace_minus = 0;
	int transalte_only = 0;
	int no_mirroring = 0;

	FILE *f_in,*f_out;

	/******************
	 * Inicialization *
	 ******************/

	read_parameters(argc,argv,&fname_in,&fname_out,
			&encoding,&out_encoding,
			&replace_minus,&transalte_only,
			&no_mirroring);
	if(out_encoding == -1) {
		out_encoding = encoding;
	}

	if(!fname_in) {
		f_in = stdin;
	}
	else {
		if(!(f_in=fopen(fname_in,"r"))) {
			fprintf(stderr,"Failed to open %s for reading\n",fname_in);
			exit(1);
		}
	}

	if(!fname_out) {
		f_out = stdout;
	}
	else {
		if(!(f_out=fopen(fname_out,"w"))) {
			fprintf(stderr,"Failed to open %s for writing\n",fname_out);
			exit(1);
		}
	}


	/*************
	 * Main loop *
	 *************/

	io_init();

	bidi_init(f_out);

	while(io_read_line(text_line_in,encoding,f_in)) {

		if(bidi_process(text_line_in,text_line_out,
							replace_minus,transalte_only,no_mirroring))
		{
			/*If there is something to print */
			io_write_line(text_line_out,out_encoding,f_out);
		}

	}

	/**********
	 * Finish *
	 **********/

	if(f_out!=stdout) fclose(f_out);
	if(f_in!=stdin) fclose(f_in);

	bidi_finish();

	return 0;
}