6 * Copyright (C) 1998-2004 A.J. van Os
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 * The main program of 'antiword' (Unix version)
32 #if defined(__CYGWIN__) || defined(__CYGMING__)
34 # include <X11/Xlocale.h>
40 #endif /* __CYGWIN__ || __CYGMING__ */
41 #if defined(N_PLAT_NLM)
42 #if !defined(_VA_LIST)
43 #include "NW-only/nw_os.h"
44 #endif /* !_VA_LIST */
46 #endif /* N_PLAT_NLM */
50 /* The name of this program */
51 static const char *szTask
= NULL
;
57 fprintf(stderr
, "\tName: %s\n", szTask
);
58 fprintf(stderr
, "\tPurpose: "PURPOSESTRING
"\n");
59 fprintf(stderr
, "\tAuthor: "AUTHORSTRING
"\n");
60 fprintf(stderr
, "\tVersion: "VERSIONSTRING
);
62 fprintf(stderr
, VERSIONSTRING2
);
64 fprintf(stderr
, "\n");
65 fprintf(stderr
, "\tStatus: "STATUSSTRING
"\n");
67 "\tUsage: %s [switches] wordfile1 [wordfile2 ...]\n", szTask
);
69 "\tSwitches: [-f|-t|-a papersize|-p papersize|-x dtd]"
70 "[-m mapping][-w #][-i #][-Ls]\n");
71 fprintf(stderr
, "\t\t-f formatted text output\n");
72 fprintf(stderr
, "\t\t-t text output (default)\n");
73 fprintf(stderr
, "\t\t-a <paper size name> Adobe PDF output\n");
74 fprintf(stderr
, "\t\t-p <paper size name> PostScript output\n");
75 fprintf(stderr
, "\t\t paper size like: a4, letter or legal\n");
76 fprintf(stderr
, "\t\t-x <dtd> XML output\n");
77 fprintf(stderr
, "\t\t like: db (DocBook)\n");
78 fprintf(stderr
, "\t\t-m <mapping> character mapping file\n");
79 fprintf(stderr
, "\t\t-w <width> in characters of text output\n");
80 fprintf(stderr
, "\t\t-i <level> image level (PostScript only)\n");
81 fprintf(stderr
, "\t\t-L use landscape mode (PostScript only)\n");
82 fprintf(stderr
, "\t\t-r Show removed text\n");
83 fprintf(stderr
, "\t\t-s Show hidden (by Word) text\n");
87 * pStdin2TmpFile - save stdin in a temporary file
89 * returns: the pointer to the temporary file or NULL
92 pStdin2TmpFile(long *lFilesize
)
97 UCHAR aucBytes
[BUFSIZ
];
99 DBG_MSG("pStdin2TmpFile");
101 fail(lFilesize
== NULL
);
103 /* Open the temporary file */
104 pTmpFile
= tmpfile();
105 if (pTmpFile
== NULL
) {
110 /* Stdin must be read as a binary stream */
111 setmode(fileno(stdin
), O_BINARY
);
114 /* Copy stdin to the temporary file */
118 tSize
= fread(aucBytes
, 1, sizeof(aucBytes
), stdin
);
120 bFailure
= feof(stdin
) == 0;
123 if (fwrite(aucBytes
, 1, tSize
, pTmpFile
) != tSize
) {
127 *lFilesize
+= (long)tSize
;
131 /* Switch stdin back to a text stream */
132 setmode(fileno(stdin
), O_TEXT
);
135 /* Deal with the result of the copy action */
138 (void)fclose(pTmpFile
);
143 } /* end of pStdin2TmpFile */
146 * bProcessFile - process a single file
148 * returns: TRUE when the given file is a supported Word file, otherwise FALSE
151 bProcessFile(const char *szFilename
)
159 fail(szFilename
== NULL
|| szFilename
[0] == '\0');
163 if (szFilename
[0] == '-' && szFilename
[1] == '\0') {
164 pFile
= pStdin2TmpFile(&lFilesize
);
166 werr(0, "I can't save the standard input to a file");
170 pFile
= fopen(szFilename
, "rb");
172 werr(0, "I can't open '%s' for reading", szFilename
);
176 lFilesize
= lGetFilesize(szFilename
);
179 werr(0, "I can't get the size of '%s'", szFilename
);
184 iWordVersion
= iGuessVersionNumber(pFile
, lFilesize
);
185 if (iWordVersion
< 0 || iWordVersion
== 3) {
186 if (bIsRtfFile(pFile
)) {
187 werr(0, "%s is not a Word Document."
188 " It is probably a Rich Text Format file",
190 } if (bIsWordPerfectFile(pFile
)) {
191 werr(0, "%s is not a Word Document."
192 " It is probably a Word Perfect file",
196 werr(0, "%s is not a Word Document or the filename"
197 " is not in the 8+3 format.", szFilename
);
199 werr(0, "%s is not a Word Document.", szFilename
);
205 /* Reset any reading done during file testing */
208 pDiag
= pCreateDiagram(szTask
, szFilename
);
214 bResult
= bWordDecryptor(pFile
, lFilesize
, pDiag
);
215 vDestroyDiagram(pDiag
);
219 } /* end of bProcessFile */
222 main(int argc
, char **argv
)
224 options_type tOptions
;
225 const char *szWordfile
;
226 int iFirst
, iIndex
, iGoodCount
;
227 BOOL bUsage
, bMultiple
, bUseTXT
, bUseXML
;
233 szTask
= szBasename(argv
[0]);
239 iFirst
= iReadOptions(argc
, argv
);
240 bUsage
= iFirst
<= 0;
244 return iFirst
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;
247 #if defined(N_PLAT_NLM) && !defined(_VA_LIST)
249 #endif /* N_PLAT_NLM && !_VA_LIST */
251 vGetOptions(&tOptions
);
254 if (is_locale_utf8()) {
255 #if defined(__STDC_ISO_10646__)
257 * If the user wants UTF-8 and the envirionment variables
258 * support UTF-8, than set the locale accordingly
260 if (tOptions
.eEncoding
== encoding_utf_8
) {
261 if (setlocale(LC_CTYPE
, "") == NULL
) {
262 werr(1, "Can't set the UTF-8 locale! "
263 "Check LANG, LC_CTYPE, LC_ALL.");
265 DBG_MSG("The UTF-8 locale has been set");
267 (void)setlocale(LC_CTYPE
, "C");
269 #endif /* __STDC_ISO_10646__ */
271 if (setlocale(LC_CTYPE
, "") == NULL
) {
272 werr(0, "Can't set the locale! Will use defaults");
273 (void)setlocale(LC_CTYPE
, "C");
275 DBG_MSG("The locale has been set");
279 bMultiple
= argc
- iFirst
> 1;
280 bUseTXT
= tOptions
.eConversionType
== conversion_text
||
281 tOptions
.eConversionType
== conversion_fmt_text
;
282 bUseXML
= tOptions
.eConversionType
== conversion_xml
;
286 if (tOptions
.eConversionType
== conversion_pdf
) {
287 /* PDF must be written as a binary stream */
288 setmode(fileno(stdout
), O_BINARY
);
294 "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
295 "<!DOCTYPE %s PUBLIC \"-//OASIS//DTD DocBook XML V4.1.2//EN\"\n"
296 "\t\"http://www.oasis-open.org/docbook/xml/4.1.2/docbookx.dtd\">\n",
297 bMultiple
? "set" : "book");
299 fprintf(stdout
, "<set>\n");
303 for (iIndex
= iFirst
; iIndex
< argc
; iIndex
++) {
304 if (bMultiple
&& bUseTXT
) {
305 szWordfile
= szBasename(argv
[iIndex
]);
306 fprintf(stdout
, "::::::::::::::\n");
307 fprintf(stdout
, "%s\n", szWordfile
);
308 fprintf(stdout
, "::::::::::::::\n");
310 if (bProcessFile(argv
[iIndex
])) {
315 if (bMultiple
&& bUseXML
) {
316 fprintf(stdout
, "</set>\n");
320 return iGoodCount
<= 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;