-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.c
392 lines (355 loc) · 9.54 KB
/
options.c
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
/*
* options.c
* Copyright (C) 1998-2004 A.J. van Os; Released under GNU GPL
*
* Description:
* Read and write the options
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include "antiword.h"
#define LEAFNAME_SIZE (32+1)
/* Current values for options */
static options_type tOptionsCurr;
typedef struct papersize_tag {
char szName[16]; /* Papersize name */
USHORT usWidth; /* In points */
USHORT usHeight; /* In points */
} papersize_type;
static const papersize_type atPaperSizes[] = {
{ "10x14", 720, 1008 },
{ "a3", 842, 1191 },
{ "a4", 595, 842 },
{ "a5", 420, 595 },
{ "b4", 729, 1032 },
{ "b5", 516, 729 },
{ "executive", 540, 720 },
{ "folio", 612, 936 },
{ "legal", 612, 1008 },
{ "letter", 612, 792 },
{ "note", 540, 720 },
{ "quarto", 610, 780 },
{ "statement", 396, 612 },
{ "tabloid", 792, 1224 },
{ "", 0, 0 },
};
/* Default values for options */
static const options_type tOptionsDefault = {
DEFAULT_SCREEN_WIDTH,
conversion_text,
TRUE,
TRUE,
FALSE,
encoding_latin_1,
INT_MAX,
INT_MAX,
};
/*
* bCorrectPapersize - see if the papersize is correct
*
* TRUE if the papersize is correct, otherwise FALSE
*/
static BOOL
bCorrectPapersize(const char *szName, conversion_type eConversionType)
{
const papersize_type *pPaperSize;
for (pPaperSize = atPaperSizes;
pPaperSize->szName[0] != '\0';
pPaperSize++) {
if (!STRCEQ(pPaperSize->szName, szName)) {
continue;
}
DBG_DEC(pPaperSize->usWidth);
DBG_DEC(pPaperSize->usHeight);
tOptionsCurr.eConversionType = eConversionType;
tOptionsCurr.iPageHeight = (int)pPaperSize->usHeight;
tOptionsCurr.iPageWidth = (int)pPaperSize->usWidth;
return TRUE;
}
return FALSE;
} /* end of bCorrectPapersize */
/*
* szCreateSuffix - create a suffix for the file
*
* Returns the suffix
*/
static const char *
szCreateSuffix(const char *szLeafname)
{
const char *pcDot;
pcDot = strrchr(szLeafname, '.');
if (pcDot != NULL && STRCEQ(pcDot, ".txt")) {
/* There is already a .txt suffix, no need for another one */
return "";
}
return ".txt";
} /* end of szCreateSuffix */
/*
* eMappingFile2Encoding - convert the mapping file to an encoding
*/
static encoding_type
eMappingFile2Encoding(const char *szLeafname)
{
char szMappingFile[LEAFNAME_SIZE+4];
fail(szLeafname == NULL);
if (strlen(szLeafname) + 4 >= sizeof(szMappingFile)) {
DBG_MSG(szLeafname);
return encoding_latin_1;
}
sprintf(szMappingFile, "%s%s", szLeafname, szCreateSuffix(szLeafname));
DBG_MSG(szMappingFile);
if (STRCEQ(szMappingFile, MAPPING_FILE_UTF_8)) {
return encoding_utf_8;
}
if (STRCEQ(szMappingFile, MAPPING_FILE_CP852) ||
STRCEQ(szMappingFile, MAPPING_FILE_CP1250) ||
STRCEQ(szMappingFile, MAPPING_FILE_8859_2)) {
return encoding_latin_2;
}
if (STRCEQ(szMappingFile, MAPPING_FILE_KOI8_R) ||
STRCEQ(szMappingFile, MAPPING_FILE_KOI8_U) ||
STRCEQ(szMappingFile, MAPPING_FILE_CP866) ||
STRCEQ(szMappingFile, MAPPING_FILE_CP1251) ||
STRCEQ(szMappingFile, MAPPING_FILE_8859_5)) {
return encoding_cyrillic;
}
return encoding_latin_1;
} /* end of eMappingFile2Encoding */
/*
* pOpenCharacterMappingFile - open the mapping file
*
* Returns the file pointer or NULL
*/
static FILE *
pOpenCharacterMappingFile(const char *szLeafname)
{
FILE *pFile;
const char *szHome, *szAntiword, *szSuffix;
size_t tFilenameLen;
char szMappingFile[PATH_MAX+1];
if (szLeafname == NULL || szLeafname[0] == '\0') {
return NULL;
}
DBG_MSG(szLeafname);
/* Set the suffix */
szSuffix = szCreateSuffix(szLeafname);
/* Set length */
tFilenameLen = strlen(szLeafname) + strlen(szSuffix);
/* Try the environment version of the mapping file */
szAntiword = szGetAntiwordDirectory();
if (szAntiword != NULL && szAntiword[0] != '\0') {
if (strlen(szAntiword) + tFilenameLen <
sizeof(szMappingFile) -
sizeof(FILE_SEPARATOR)) {
sprintf(szMappingFile,
"%s" FILE_SEPARATOR "%s%s",
szAntiword, szLeafname, szSuffix);
DBG_MSG(szMappingFile);
pFile = fopen(szMappingFile, "r");
if (pFile != NULL) {
return pFile;
}
} else {
werr(0, "Environment mappingfilename ignored");
}
}
/* Try the local version of the mapping file */
szHome = szGetHomeDirectory();
if (strlen(szHome) + tFilenameLen <
sizeof(szMappingFile) -
sizeof(ANTIWORD_DIR) -
2 * sizeof(FILE_SEPARATOR)) {
sprintf(szMappingFile,
"%s" FILE_SEPARATOR ANTIWORD_DIR FILE_SEPARATOR "%s%s",
szHome, szLeafname, szSuffix);
DBG_MSG(szMappingFile);
pFile = fopen(szMappingFile, "r");
if (pFile != NULL) {
return pFile;
}
} else {
werr(0, "Local mappingfilename too long, ignored");
}
/* Try the global version of the mapping file */
if (tFilenameLen <
sizeof(szMappingFile) -
sizeof(GLOBAL_ANTIWORD_DIR) -
sizeof(FILE_SEPARATOR)) {
sprintf(szMappingFile,
GLOBAL_ANTIWORD_DIR FILE_SEPARATOR "%s%s",
szLeafname, szSuffix);
DBG_MSG(szMappingFile);
pFile = fopen(szMappingFile, "r");
if (pFile != NULL) {
return pFile;
}
} else {
werr(0, "Global mappingfilename too long, ignored");
}
werr(0, "I can't open your mapping file (%s%s)\n"
"It is not in '%s" FILE_SEPARATOR ANTIWORD_DIR "' nor in '"
GLOBAL_ANTIWORD_DIR "'.", szLeafname, szSuffix, szHome);
return NULL;
} /* end of pOpenCharacterMappingFile */
/*
* vCloseCharacterMappingFile - close the mapping file
*/
static void
vCloseCharacterMappingFile(FILE *pFile)
{
(void)fclose(pFile);
} /* end of pCloseCharacterMappingFile */
/*
* iReadOptions - read options
*
* returns: -1: error
* 0: help
* >0: index first file argument
*/
int
iReadOptions(int argc, char **argv)
{
extern char *optarg;
extern int optind;
char *pcChar, *szTmp;
int iChar;
char szLeafname[LEAFNAME_SIZE];
FILE *pCharacterMappingFile;
int iTmp;
BOOL bSuccess;
DBG_MSG("iReadOptions");
/* Defaults */
tOptionsCurr = tOptionsDefault;
/* Environment */
szTmp = getenv("COLUMNS");
if (szTmp != NULL) {
DBG_MSG(szTmp);
iTmp = (int)strtol(szTmp, &pcChar, 10);
if (*pcChar == '\0') {
iTmp -= 4; /* This is for the edge */
if (iTmp < MIN_SCREEN_WIDTH) {
iTmp = MIN_SCREEN_WIDTH;
} else if (iTmp > MAX_SCREEN_WIDTH) {
iTmp = MAX_SCREEN_WIDTH;
}
tOptionsCurr.iParagraphBreak = iTmp;
DBG_DEC(tOptionsCurr.iParagraphBreak);
}
}
strncpy(szLeafname, szGetDefaultMappingFile(), sizeof(szLeafname) - 1);
szLeafname[sizeof(szLeafname) - 1] = '\0';
/* Command line */
while ((iChar = getopt(argc, argv, "La:fhi:m:p:rstw:x:")) != -1) {
switch (iChar) {
case 'f':
tOptionsCurr.eConversionType = conversion_fmt_text;
break;
case 'h':
return 0;
case 'm':
if (tOptionsCurr.eConversionType == conversion_xml) {
werr(0, "XML doesn't need a mapping file");
break;
}
strncpy(szLeafname, optarg, sizeof(szLeafname) - 1);
szLeafname[sizeof(szLeafname) - 1] = '\0';
DBG_MSG(szLeafname);
break;
case 'p':
if (!bCorrectPapersize(optarg, conversion_ps)) {
werr(0, "-p without a valid papersize");
return -1;
}
break;
case 'r':
tOptionsCurr.bRemoveRemovedText = FALSE;
break;
case 's':
tOptionsCurr.bHideHiddenText = FALSE;
break;
case 't':
tOptionsCurr.eConversionType = conversion_text;
break;
case 'w':
iTmp = (int)strtol(optarg, &pcChar, 10);
if (*pcChar == '\0') {
if (iTmp != 0 && iTmp < MIN_SCREEN_WIDTH) {
iTmp = MIN_SCREEN_WIDTH;
} else if (iTmp > MAX_SCREEN_WIDTH) {
iTmp = MAX_SCREEN_WIDTH;
}
tOptionsCurr.iParagraphBreak = iTmp;
DBG_DEC(tOptionsCurr.iParagraphBreak);
}
break;
case 'x':
if (STREQ(optarg, "db")) {
tOptionsCurr.iParagraphBreak = 0;
tOptionsCurr.eConversionType = conversion_xml;
strcpy(szLeafname, MAPPING_FILE_UTF_8);
} else {
werr(0, "-x %s is not supported", optarg);
return -1;
}
break;
default:
return -1;
}
}
tOptionsCurr.eEncoding = eMappingFile2Encoding(szLeafname);
DBG_DEC(tOptionsCurr.eEncoding);
if (tOptionsCurr.eConversionType == conversion_ps &&
tOptionsCurr.eEncoding == encoding_utf_8) {
werr(0,
"The combination PostScript and UTF-8 is not supported");
return -1;
}
if (tOptionsCurr.eConversionType == conversion_pdf &&
tOptionsCurr.eEncoding == encoding_utf_8) {
werr(0,
"The combination PDF and UTF-8 is not supported");
return -1;
}
if (tOptionsCurr.eConversionType == conversion_pdf &&
tOptionsCurr.eEncoding == encoding_cyrillic) {
werr(0,
"The combination PDF and Cyrillic is not supported");
return -1;
}
if (tOptionsCurr.eConversionType == conversion_ps ||
tOptionsCurr.eConversionType == conversion_pdf) {
/* PostScript or PDF mode */
if (tOptionsCurr.bUseLandscape) {
/* Swap the page height and width */
iTmp = tOptionsCurr.iPageHeight;
tOptionsCurr.iPageHeight = tOptionsCurr.iPageWidth;
tOptionsCurr.iPageWidth = iTmp;
}
/* The paragraph break depends on the width of the paper */
tOptionsCurr.iParagraphBreak = iMilliPoints2Char(
(long)tOptionsCurr.iPageWidth * 1000 -
lDrawUnits2MilliPoints(
PS_LEFT_MARGIN + PS_RIGHT_MARGIN));
DBG_DEC(tOptionsCurr.iParagraphBreak);
}
pCharacterMappingFile = pOpenCharacterMappingFile(szLeafname);
if (pCharacterMappingFile != NULL) {
bSuccess = bReadCharacterMappingTable(pCharacterMappingFile);
vCloseCharacterMappingFile(pCharacterMappingFile);
} else {
bSuccess = FALSE;
}
return bSuccess ? optind : -1;
} /* end of iReadOptions */
/*
* vGetOptions - get a copy of the current option values
*/
void
vGetOptions(options_type *pOptions)
{
fail(pOptions == NULL);
*pOptions = tOptionsCurr;
} /* end of vGetOptions */