-
Notifications
You must be signed in to change notification settings - Fork 0
/
lloconv.cc
405 lines (365 loc) · 8.88 KB
/
lloconv.cc
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
/* lloconv.cc - Convert a document using LibreOfficeKit
*
* Copyright (C) 2014,2015,2016,2018 Olly Betts
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#include <config.h>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <sys/socket.h>
#include <sys/un.h>
#include <errno.h>
#include <poll.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <sysexits.h>
#include <unistd.h>
#include "convert.h"
using namespace std;
static void
usage(ostream& os)
{
os << "Usage: " << program << " [-u|-s SOCKET_PATH] [-f OUTPUT_FORMAT] [-o OPTIONS] INPUT_FILE OUTPUT_FILE\n";
os << " " << program << " -s SOCKET_PATH -l\n\n";
os << " -u INPUT_FILE is a URL\n\n";
os << "Known values for OUTPUT_FORMAT include:\n";
os << " For text documents: doc docx fodt html odt ott pdf txt xhtml\n\n";
os << "Known OPTIONS include:\n";
os << " EmbedImages - embed image data in HTML using <img src=\"data:...\">\n";
os << " SkipImages - don't include images\n";
os << " SkipHeaderFooter - don't include the header and footer\n";
os << " XHTML - use XHTML doctype (different to -f xhtml)\n";
os << " xhtmlns=foo - XHTML with HTML tags prefixed by foo:\n";
os << flush;
}
static const int LISTEN_BACKLOG = 64;
// Automatically start a listener if -s is used there isn't one.
static bool auto_listener = true;
static bool
read_string(int fd, char ** s)
{
unsigned char ch;
if (read(fd, &ch, 1) != 1) return false;
size_t len = ch;
if (len >= 253) {
unsigned i = len - 251;
len = 0;
while (i-- > 0) {
if (read(fd, &ch, 1) != 1) return false;
len = (len << 8) | ch;
}
}
if (*s) free(*s);
*s = (char *)malloc(len + 1);
if (!*s) return false;
char * p = *s;
while (len) {
ssize_t n = read(fd, p, len);
if (n <= 0) {
// Error or EOF!
return false;
}
p += n;
len -= n;
}
*p = '\0';
return true;
}
static ssize_t
write_all(int fd, const char * buf, size_t count)
{
while (count) {
ssize_t r = write(fd, buf, count);
if (r < 0) {
if (errno == EINTR) continue;
return r;
}
buf += r;
count -= r;
}
return 0;
}
bool
write_string(int fd, const string & s)
{
size_t len = s.size();
char buf[5];
size_t buf_len = 0;
if (len < 253) {
buf[buf_len++] = static_cast<unsigned char>(len);
} else if (len < 0x10000) {
buf[buf_len++] = 253;
buf[buf_len++] = static_cast<unsigned char>(len >> 8);
buf[buf_len++] = static_cast<unsigned char>(len);
abort();
} else if (len < 0x1000000) {
buf[buf_len++] = 254;
buf[buf_len++] = static_cast<unsigned char>(len >> 16);
buf[buf_len++] = static_cast<unsigned char>(len >> 8);
buf[buf_len++] = static_cast<unsigned char>(len);
abort();
} else {
buf[buf_len++] = 255;
buf[buf_len++] = static_cast<unsigned char>(len >> 24);
buf[buf_len++] = static_cast<unsigned char>(len >> 16);
buf[buf_len++] = static_cast<unsigned char>(len >> 8);
buf[buf_len++] = static_cast<unsigned char>(len);
abort();
}
if (buf_len != 1) abort();
return write_all(fd, buf, buf_len) == 0 && write_all(fd, s.data(), len) == 0;
}
static void
read_params(int fd, char ** format, char ** input,
char ** output, char ** options)
{
read_string(fd, format);
read_string(fd, input);
read_string(fd, output);
read_string(fd, options);
}
static void
write_params(int fd, const char * format, const char * input,
const char * output, const char * options)
{
if (!format) format = "";
if (!options) options = "";
write_string(fd, format);
write_string(fd, input);
write_string(fd, output);
write_string(fd, options);
}
static int
read_result(int fd)
{
char * v = NULL;
if (!read_string(fd, &v)) return -1;
int r = atoi(v);
free(v);
return r;
}
static void
write_result(int fd, int v)
{
char buf[32];
sprintf(buf, "%d", v);
write_string(fd, buf);
}
static int
llo_daemon(const char * socket_path)
try {
int sock = socket(AF_UNIX, SOCK_STREAM, 0);
if (sock < 0) {
perror("socket");
return 1;
}
struct sockaddr_un my_addr;
memset(&my_addr, 0, sizeof(struct sockaddr_un));
my_addr.sun_family = AF_UNIX;
if (strlen(socket_path) >= sizeof(my_addr.sun_path)) {
fprintf(stderr, "socket path too long\n");
return 1;
}
strcpy(my_addr.sun_path, socket_path);
if (bind(sock, (struct sockaddr *)&my_addr, sizeof(my_addr)) < 0) {
perror("bind");
return 1;
}
void * handle = convert_init();
if (!handle) {
return EX_UNAVAILABLE;
}
char * format = NULL;
char * input = NULL;
char * output = NULL;
char * options = NULL;
while (true) {
if (listen(sock, LISTEN_BACKLOG) < 0) {
perror("listen");
return 1;
}
struct sockaddr_un peer_addr;
socklen_t peer_addr_size = sizeof(struct sockaddr_un);
int fd = accept(sock, (struct sockaddr *) &peer_addr, &peer_addr_size);
if (fd < 0) {
perror("accept");
return 1;
}
read_params(fd, &format, &input, &output, &options);
if (!format[0]) {
free(format);
format = NULL;
}
if (!options[0]) {
free(options);
options = NULL;
}
// Hard-code that the path is a file not a URL when using a server, at
// least for now.
int res = convert(handle, false, input, output, format, options);
write_result(fd, res);
close(fd);
}
convert_cleanup(handle);
} catch (const exception & e) {
cerr << program << ": LibreOffice threw exception (" << e.what() << ")\n";
return 1;
}
static int
llo_daemon_convert(int fd, const char * format, const char * input,
const char * output, const char * options)
{
write_params(fd, format, input, output, options);
return read_result(fd);
}
int
main(int argc, char **argv)
{
program = argv[0];
const char * format = NULL;
const char * options = NULL;
bool url = false;
bool listener = false;
const char * socket_path = NULL;
// FIXME: Use getopt() or something.
++argv;
--argc;
while (argv[0] && argv[0][0] == '-') {
switch (argv[0][1]) {
case '-':
if (argv[0][2] == '\0') {
// End of options.
++argv;
--argc;
goto last_option;
}
if (strcmp(argv[0] + 2, "help") == 0) {
usage(cout);
exit(0);
}
if (strcmp(argv[0] + 2, "version") == 0) {
cout << "lloconv - " PACKAGE_STRING "\n";
exit(0);
}
break;
case 'f':
if (argv[0][2]) {
format = argv[0] + 2;
++argv;
--argc;
} else {
format = argv[1];
argv += 2;
argc -= 2;
}
continue;
case 'o':
if (argv[0][2]) {
options = argv[0] + 2;
++argv;
--argc;
} else {
options = argv[1];
argv += 2;
argc -= 2;
}
continue;
case 'u':
if (argv[0][2] != '\0') {
break;
}
url = true;
++argv;
--argc;
continue;
case 'l':
if (argv[0][2] != '\0') {
break;
}
listener = true;
++argv;
--argc;
continue;
case 's':
if (argv[0][2]) {
socket_path = argv[0] + 2;
++argv;
--argc;
} else {
socket_path = argv[1];
argv += 2;
argc -= 2;
}
continue;
}
cerr << "Option '" << argv[0] << "' unknown\n\n";
argc = -1;
break;
}
last_option:
if (listener) {
if (argc != 0 || format || options || url || !socket_path) {
usage(cerr);
_Exit(EX_USAGE);
}
_Exit(llo_daemon(socket_path));
}
if ((url && socket_path) || argc != 2) {
usage(cerr);
_Exit(EX_USAGE);
}
const char * input = argv[0];
const char * output = argv[1];
if (socket_path) {
int fd = socket(AF_UNIX, SOCK_STREAM, 0);
if (fd < 0) {
perror("socket");
_Exit(1);
}
struct sockaddr_un my_addr;
memset(&my_addr, 0, sizeof(struct sockaddr_un));
my_addr.sun_family = AF_UNIX;
if (strlen(socket_path) >= sizeof(my_addr.sun_path)) {
fprintf(stderr, "socket path too long\n");
_Exit(1);
}
strcpy(my_addr.sun_path, socket_path);
if (connect(fd, (struct sockaddr *)&my_addr, sizeof(my_addr)) < 0) {
if ((errno != ECONNREFUSED && errno != ENOENT) || !auto_listener) {
perror("connect");
_Exit(1);
}
if (errno == ECONNREFUSED)
unlink(socket_path);
pid_t child = fork();
if (child == -1) {
perror("fork");
_Exit(1);
}
if (child == 0) {
_Exit(llo_daemon(socket_path));
}
// FIXME: Actually wait for daemon to start.
sleep(1);
if (connect(fd, (struct sockaddr *)&my_addr, sizeof(my_addr)) < 0) {
perror("connect");
_Exit(1);
}
}
int rc = llo_daemon_convert(fd, format, input, output, options);
_Exit(rc);
}
void * handle = convert_init();
if (!handle) {
_Exit(EX_UNAVAILABLE);
}
int rc = convert(handle, url, input, output, format, options);
convert_cleanup(handle);
// Avoid segfault from LibreOffice by terminating swiftly.
_Exit(rc);
}