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
|
/*
LibRCC - module responsible for translation service
Copyright (C) 2005-2008 Suren A. Chilingaryan <csa@dside.dyndns.org>
This library is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License version 2.1 or later
as published by the Free Software Foundation.
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 program; if not, write to the Free Software Foundation, Inc.,
59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef HAVE_STRINGS_H
# include <strings.h>
#endif /* HAVE_STRINGS_H */
#include "internal.h"
#include "rccconfig.h"
#include "rccexternal.h"
#include "rccmutex.h"
#include "rcctranslate.h"
#define RCC_TRANSLATE_DEFAULT_TIMEOUT 1000000 /* 1s */
int rccTranslateInit() {
return 0;
}
void rccTranslateFree() {
}
rcc_translate rccTranslateOpen(const char *from, const char *to) {
#ifdef HAVE_LIBTRANSLATE
rcc_translate translate;
if ((!from)||(!to)||(!strcasecmp(from,to))) return NULL;
if ((strlen(from)!=2)||(strlen(to)!=2)) return NULL;
translate = (rcc_translate)malloc(sizeof(rcc_translate_s));
if (!translate) return NULL;
translate->mutex = rccMutexCreate();
translate->wmutex = rccMutexCreate();
if ((!translate->mutex)||(!translate->wmutex)) {
if (translate->mutex) rccMutexFree(translate->mutex);
if (translate->wmutex) rccMutexFree(translate->wmutex);
free(translate);
return NULL;
}
translate->sock = rccExternalConnect(RCC_EXTERNAL_MODULE_LIBRTRANSLATE);
if (translate->sock == -1) {
rccMutexFree(translate->mutex);
rccMutexFree(translate->wmutex);
free(translate);
return NULL;
}
translate->remaining = 0;
translate->werror = 0;
translate->prefix.cmd.cmd = RCC_EXTERNAL_COMMAND_TRANSLATE;
translate->prefix.cmd.size = sizeof(rcc_translate_prefix_s);
memcpy(translate->prefix.from, from, 3*sizeof(char));
memcpy(translate->prefix.to, to, 3*sizeof(char));
translate->wprefix.cmd.cmd = RCC_EXTERNAL_COMMAND_TRANSLATE_QUEUE;
translate->wprefix.cmd.size = sizeof(rcc_translate_prefix_s);
memcpy(translate->wprefix.from, from, 3*sizeof(char));
memcpy(translate->wprefix.to, to, 3*sizeof(char));
rccTranslateSetTimeout(translate, RCC_TRANSLATE_DEFAULT_TIMEOUT);
return translate;
#else
return NULL;
#endif /* HAVE_LIBTRANSLATE */
}
void rccTranslateClose(rcc_translate translate) {
#ifdef HAVE_LIBTRANSLATE
if (!translate) return;
if (translate->sock != -1) rccExternalClose(translate->sock);
rccMutexFree(translate->mutex);
rccMutexFree(translate->wmutex);
free(translate);
#endif /* HAVE_LIBTRANSLATE */
}
int rccTranslateSetTimeout(rcc_translate translate, unsigned long us) {
if (!translate) return -1;
translate->prefix.timeout = us;
return 0;
}
int rccTranslateAllowOfflineMode(rcc_translate translate) {
return rccExternalAllowOfflineMode();
}
#ifdef HAVE_LIBTRANSLATE
# define RCC_UNLOCK_W 1
# define RCC_UNLOCK_R 2
# define RCC_UNLOCK_RW 3
# define RCC_UNLOCK_WR 3
static char *rccTranslateReturn(rcc_translate translate, char *ret, int unlock) {
if (unlock&RCC_UNLOCK_R) rccMutexUnLock(translate->mutex);
if (unlock&RCC_UNLOCK_W) rccMutexUnLock(translate->wmutex);
return ret;
}
# define rccTranslateReturnNULL(translate, unlock) rccTranslateReturn(translate, NULL, unlock)
static int rccTranslateQueue(rcc_translate translate, const char *buf) {
size_t len, err;
len = strlen(buf);
translate->wprefix.cmd.size = sizeof(rcc_translate_prefix_s) + len - sizeof(rcc_external_command_s);
err = rccExternalWrite(translate->sock, (char*)&translate->wprefix, sizeof(rcc_translate_prefix_s) - 1, 0);
if (!err) err = rccExternalWrite(translate->sock, buf, len + 1, 0);
return err?1:0;
}
#endif /* HAVE_LIBTRANSLATE */
char *rccTranslate(rcc_translate translate, const char *buf) {
#ifdef HAVE_LIBTRANSLATE
rcc_external_command_s resp;
size_t err, len;
char *buffer;
size_t i;
if ((!translate)||(!buf)) return NULL;
if (!strcmp(translate->prefix.to, rcc_english_language_sn)) {
for (i=0;buf[i];i++) {
if ((unsigned char)buf[i]>0x7F) break;
if ((buf[i]>='A')&&(buf[i]<='Z')) break;
if ((buf[i]>='a')&&(buf[i]<='z')) break;
}
if (!buf[i]) return NULL;
}
rccMutexLock(translate->wmutex);
if (rccMutexTryLock(translate->mutex)) {
if ((translate->werror)||(translate->sock == -1)) return rccTranslateReturnNULL(translate,RCC_UNLOCK_W);
if (rccTranslateQueue(translate, buf)) translate->werror = 1;
return rccTranslateReturnNULL(translate, RCC_UNLOCK_W);
}
if (translate->werror) {
rccExternalClose(translate->sock);
translate->sock = -1;
translate->werror = 0;
}
if (translate->sock == -1) {
translate->sock = rccExternalConnect(RCC_EXTERNAL_MODULE_LIBRTRANSLATE);
if (translate->sock == -1) {
return rccTranslateReturnNULL(translate,RCC_UNLOCK_RW);
} else {
translate->werror = 0;
translate->remaining = 0;
}
} else if (translate->remaining) {
if (translate->remaining == (size_t)-1) {
err = rccExternalRead(translate->sock, (char*)&resp, sizeof(rcc_external_command_s), 0);
if (err) {
if (err == sizeof(rcc_external_command_s)) {
if (rccTranslateQueue(translate, buf)) {
rccExternalClose(translate->sock);
translate->sock = -1;
}
} else {
rccExternalClose(translate->sock);
translate->sock = -1;
}
return rccTranslateReturnNULL(translate,RCC_UNLOCK_RW);
}
translate->remaining = resp.size;
}
buffer = (char*)malloc(translate->remaining*sizeof(char));
if (!buffer) {
rccExternalClose(translate->sock);
translate->sock = -1;
return rccTranslateReturnNULL(translate,RCC_UNLOCK_RW);
}
err = rccExternalRead(translate->sock, buffer, translate->remaining, 0);
free(buffer);
if (err) {
translate->remaining = err;
if (rccTranslateQueue(translate, buf)) {
rccExternalClose(translate->sock);
translate->sock = -1;
}
return rccTranslateReturnNULL(translate,RCC_UNLOCK_RW);
}
translate->remaining = 0;
}
len = strlen(buf);
translate->prefix.cmd.size = sizeof(rcc_translate_prefix_s) + len - sizeof(rcc_external_command_s);
err = rccExternalWrite(translate->sock, (char*)&translate->prefix, sizeof(rcc_translate_prefix_s) - 1, 0);
if (err) {
rccExternalClose(translate->sock);
translate->sock = -1;
return rccTranslateReturnNULL(translate,RCC_UNLOCK_RW);
}
err = rccExternalWrite(translate->sock, buf, len + 1, 0);
if (err) {
rccExternalClose(translate->sock);
translate->sock = -1;
return rccTranslateReturnNULL(translate,RCC_UNLOCK_RW);
}
rccMutexUnLock(translate->wmutex);
err = rccExternalRead(translate->sock, (char*)&resp, sizeof(rcc_external_command_s), translate->prefix.timeout);
if (err) {
if (err == sizeof(rcc_external_command_s)) {
translate->remaining = (size_t)-1;
} else {
rccMutexLock(translate->wmutex);
rccExternalClose(translate->sock);
translate->sock = -1;
rccMutexUnLock(translate->wmutex);
}
return rccTranslateReturnNULL(translate,RCC_UNLOCK_R);
}
if ((resp.cmd!=RCC_EXTERNAL_COMMAND_TRANSLATE)||(!resp.size))
return rccTranslateReturnNULL(translate,RCC_UNLOCK_R);
buffer = (char*)malloc(resp.size*sizeof(char));
if (!buffer) {
rccMutexLock(translate->wmutex);
rccExternalClose(translate->sock);
translate->sock = -1;
rccMutexUnLock(translate->wmutex);
return rccTranslateReturnNULL(translate,RCC_UNLOCK_R);
}
err = rccExternalRead(translate->sock, buffer, resp.size, 0);
if (err) {
translate->remaining = err;
free(buffer);
return rccTranslateReturnNULL(translate,RCC_UNLOCK_R);
}
return rccTranslateReturn(translate, buffer, RCC_UNLOCK_R);
#else
return NULL;
#endif /* HAVE_LIBTRANSLATE */
}
|