examples/PIPS/antiword/src/main_u.c

00001 /*
00002  * main_u.c
00003  *
00004  * Released under GPL
00005  *
00006  * Copyright (C) 1998-2004 A.J. van Os
00007  *
00008  * This program is free software; you can redistribute it and/or
00009  * modify it under the terms of the GNU General Public License
00010  * as published by the Free Software Foundation; either version 2
00011  * of the License, or (at your option) any later version.
00012  *
00013  * This program is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License
00019  * along with this program; if not, write to the Free Software
00020  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00021  *
00022  * Description:
00023  * The main program of 'antiword' (Unix version)
00024  */
00025 #ifdef SYMBIAN
00026 //GCCE header
00027 //#include <staticlibinit_gcce.h>
00028 #include <string.h>
00029 #endif /*SYMBIAN*/
00030 
00031 #include <stdio.h>
00032 #include <stdlib.h>
00033 #if defined(__dos)
00034 #include <fcntl.h>
00035 #include <io.h>
00036 #endif /* __dos */
00037 #if defined(__CYGWIN__) || defined(__CYGMING__)
00038 #  ifdef X_LOCALE
00039 #    include <X11/Xlocale.h>
00040 #  else
00041 #    include <locale.h>
00042 #  endif
00043 #else
00044 #include <locale.h>
00045 #endif /* __CYGWIN__ || __CYGMING__ */
00046 #if defined(N_PLAT_NLM)
00047 #if !defined(_VA_LIST)
00048 #include "NW-only/nw_os.h"
00049 #endif /* !_VA_LIST */
00050 #include "getopt.h"
00051 #endif /* N_PLAT_NLM */
00052 #include "version.h"
00053 #include "antiword.h"
00054 #include <unistd.h> 
00055 
00056 /* The name of this program */
00057 static const char       *szTask = NULL;
00058 
00059 
00060 static void
00061 vUsage(void)
00062 {
00063         fprintf(stderr, "\tName: %s\n", szTask);
00064         fprintf(stderr, "\tPurpose: "PURPOSESTRING"\n");
00065         fprintf(stderr, "\tAuthor: "AUTHORSTRING"\n");
00066         fprintf(stderr, "\tVersion: "VERSIONSTRING);
00067 #if defined(__dos)
00068         fprintf(stderr, VERSIONSTRING2);
00069 #endif /* __dos */
00070         fprintf(stderr, "\n");
00071         fprintf(stderr, "\tStatus: "STATUSSTRING"\n");
00072         fprintf(stderr,
00073                 "\tUsage: %s [switches] wordfile1 [wordfile2 ...]\n", szTask);
00074         fprintf(stderr,
00075                 "\tSwitches: [-f|-t|-a papersize|-p papersize|-x dtd]"
00076                 "[-m mapping][-w #][-i #][-Ls]\n");
00077         fprintf(stderr, "\t-f formatted text output\n");
00078         fprintf(stderr, "\t-t text output (default)\n");
00079         fprintf(stderr, "\t-a <paper size name> Adobe PDF output\n");
00080         fprintf(stderr, "\t-p <paper size name> PostScript output\n");
00081         fprintf(stderr, "\t   paper size like: a4, letter or legal\n");
00082         fprintf(stderr, "\t-x <dtd> XML output\n");
00083         fprintf(stderr, "\t   like: db (DocBook)\n");
00084         fprintf(stderr, "\t-m <mapping> character mapping file\n");
00085         fprintf(stderr, "\t-w <width> in characters of text output\n");
00086         fprintf(stderr, "\t-i <level> image level (PostScript only)\n");
00087         fprintf(stderr, "\t-L use landscape mode (PostScript only)\n");
00088         fprintf(stderr, "\t-r Show removed text\n");
00089         fprintf(stderr, "\t-s Show hidden (by Word) text\n");
00090 } /* end of vUsage */
00091 
00092 /*
00093  * pStdin2TmpFile - save stdin in a temporary file
00094  *
00095  * returns: the pointer to the temporary file or NULL
00096  */
00097 static FILE *
00098 pStdin2TmpFile(long *lFilesize)
00099 {
00100         FILE    *pTmpFile;
00101         size_t  tSize;
00102         BOOL    bFailure;
00103         UCHAR   aucBytes[BUFSIZ];
00104 
00105         DBG_MSG("pStdin2TmpFile");
00106 
00107         fail(lFilesize == NULL);
00108 
00109         /* Open the temporary file */
00110         pTmpFile = tmpfile();
00111         if (pTmpFile == NULL) {
00112                 return NULL;
00113         }
00114 
00115 #if defined(__dos)
00116         /* Stdin must be read as a binary stream */
00117         setmode(fileno(stdin), O_BINARY);
00118 #endif /* __dos */
00119 
00120         /* Copy stdin to the temporary file */
00121         *lFilesize = 0;
00122         bFailure = TRUE;
00123         for (;;) {
00124                 tSize = fread(aucBytes, 1, sizeof(aucBytes), stdin);
00125                 if (tSize == 0) {
00126                         bFailure = feof(stdin) == 0;
00127                         break;
00128                 }
00129                 if (fwrite(aucBytes, 1, tSize, pTmpFile) != tSize) {
00130                         bFailure = TRUE;
00131                         break;
00132                 }
00133                 *lFilesize += (long)tSize;
00134         }
00135 
00136 #if defined(__dos)
00137         /* Switch stdin back to a text stream */
00138         setmode(fileno(stdin), O_TEXT);
00139 #endif /* __dos */
00140 
00141         /* Deal with the result of the copy action */
00142         if (bFailure) {
00143                 *lFilesize = 0;
00144                 (void)fclose(pTmpFile);
00145                 return NULL;
00146         }
00147         rewind(pTmpFile);
00148         return pTmpFile;
00149 } /* end of pStdin2TmpFile */
00150 
00151 /*
00152  * bProcessFile - process a single file
00153  *
00154  * returns: TRUE when the given file is a supported Word file, otherwise FALSE
00155  */
00156 #ifndef SYMBIAN 
00157 static BOOL
00158 bProcessFile(const char *szFilename )
00159 #else
00160 static BOOL
00161 bProcessFile(const char *szFilename, FILE *ofp)
00162 #endif /*SYMBIAN*/
00163 {
00164         FILE            *pFile;
00165         diagram_type    *pDiag;
00166         long            lFilesize;
00167         int             iWordVersion;
00168         BOOL            bResult;
00169 
00170         fail(szFilename == NULL || szFilename[0] == '\0');
00171 
00172         DBG_MSG(szFilename);
00173 
00174         if (szFilename[0] == '-' && szFilename[1] == '\0') {
00175                 pFile = pStdin2TmpFile(&lFilesize);
00176                 if (pFile == NULL) {
00177                         werr(0, "I can't save the standard input to a file");
00178                         return FALSE;
00179                 }
00180         } else {
00181                 pFile = fopen(szFilename, "rb");
00182                 if (pFile == NULL) {
00183                         werr(0, "I can't open '%s' for reading", szFilename);
00184                         return FALSE;
00185                 }
00186 
00187                 lFilesize = lGetFilesize(szFilename);
00188                 if (lFilesize < 0) {
00189                         (void)fclose(pFile);
00190                         werr(0, "I can't get the size of '%s'", szFilename);
00191                         return FALSE;
00192                 }
00193         }
00194 
00195         iWordVersion = iGuessVersionNumber(pFile, lFilesize);
00196         if (iWordVersion < 0 || iWordVersion == 3) {
00197                 if (bIsRtfFile(pFile)) {
00198                         werr(0, "%s is not a Word Document."
00199                                 " It is probably a Rich Text Format file",
00200                                 szFilename);
00201                 } if (bIsWordPerfectFile(pFile)) {
00202                         werr(0, "%s is not a Word Document."
00203                                 " It is probably a Word Perfect file",
00204                                 szFilename);
00205                 } else {
00206 #if defined(__dos)
00207                         werr(0, "%s is not a Word Document or the filename"
00208                                 " is not in the 8+3 format.", szFilename);
00209 #else
00210                         werr(0, "%s is not a Word Document.", szFilename);
00211 #endif /* __dos */
00212                 }
00213                 (void)fclose(pFile);
00214                 return FALSE;
00215         }
00216         /* Reset any reading done during file testing */
00217         rewind(pFile);
00218         
00219         #ifndef SYMBIAN
00220         pDiag = pCreateDiagram(szTask, szFilename);
00221         #else
00222         pDiag = pCreateDiagram(szTask, szFilename, ofp );
00223         #endif /*SYMBIAN*/
00224         if (pDiag == NULL) {
00225                 (void)fclose(pFile);
00226                 return FALSE;
00227         }
00228 
00229         bResult = bWordDecryptor(pFile, lFilesize, pDiag);
00230         vDestroyDiagram(pDiag);
00231 
00232         (void)fclose(pFile);
00233         return bResult;
00234 } /* end of bProcessFile */
00235 
00236 
00237 #ifdef SYMBIAN
00238 int fillArg( char **argv, char *inpStr)
00239 {
00240         int count =0;
00241         const char *search = " ";
00242         
00243         char *token = strtok( inpStr, search);
00244         argv[0] = token;
00245         count = 1;
00246         while( token != NULL )
00247         {
00248                 token = strtok( NULL, search);
00249                 argv[count++] = token;
00250         }
00251         return count-1;
00252 }
00253 #endif /*SYMBIAN*/
00254 int
00255 main(int argc, char **argv)
00256 {
00257         #ifdef SYMBIAN
00258         FILE *ofp = NULL;
00259         char *buf;
00260         #endif /*SYMBIAN*/
00261         
00262         options_type    tOptions;
00263         const char      *szWordfile;
00264         int     iFirst, iIndex, iGoodCount;
00265         BOOL    bUsage, bMultiple, bUseTXT, bUseXML;
00266 
00267         if (argc <= 0) {
00268                 return EXIT_FAILURE;
00269         }
00270 
00271         #ifdef SYMBIAN
00272         //Adding HOME environment Variable explicitly
00273         
00274     buf = (char *)malloc(FILENAME_MAX);
00275     // get path to the private folder
00276     getcwd(buf, FILENAME_MAX);
00277         #ifndef __WINS__
00278         // acquire installation location and get the drive letter 
00279         buf[0] = argv[0][0]; // argv[0] contains full path to exe including drive
00280         #endif
00281 
00282     strcat(buf, "\\Resources\\");
00283         if (setenv("HOME", buf, 1) != 0) {
00284                 return EXIT_FAILURE;
00285         }
00286     free(buf);
00287         #endif /*SYMBIAN*/
00288 
00289         szTask = szBasename(argv[0]);
00290 
00291         if (argc <= 1) {
00292 #ifdef SYMBIAN
00293                 char* inpStr = (char*) malloc(_POSIX_ARG_MAX);
00294                 /*If there is no command-line input, Prompt for it!*/
00295                 vUsage();
00296                 
00297                 fprintf(stdout, "\n\nEnter the input Command String:");
00298                 fgets( inpStr, _POSIX_ARG_MAX, stdin );
00299                 
00300                 argc = fillArg( argv, inpStr);
00301                 /* Check whether the first argument is valid or not */
00302                 if ( strcasecmp ( "antiword", argv[0]))
00303                 {
00304                         fprintf(stdout, "\nInvalid command syntax, Please read the syntax properly\n\n\n");
00305                         bUsage = TRUE;
00306                         getchar();
00307                 }
00308                 else
00309                 {
00310                         iFirst = iReadOptions(argc, argv);
00311                         bUsage = iFirst <= 0;
00312                 }
00313                 #else 
00314                 bUsage = TRUE;
00315                 iFirst = 1;
00316                 free(inputStr);
00317 #endif /*SYMBIAN*/
00318         } else {
00319                 iFirst = iReadOptions(argc, argv);
00320                 bUsage = iFirst <= 0;
00321         }
00322         if (bUsage) {
00323                 vUsage();
00324                 #ifdef SYMBIAN
00325                 getchar();
00326                 #endif /*SYMBIAN*/
00327                 return iFirst < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
00328         }
00329 
00330 #if defined(N_PLAT_NLM) && !defined(_VA_LIST)
00331         nwinit();
00332 #endif /* N_PLAT_NLM && !_VA_LIST */
00333 
00334         vGetOptions(&tOptions);
00335 
00336 #if !defined(__dos)
00337         if (is_locale_utf8()) {
00338 #if defined(__STDC_ISO_10646__)
00339                 /*
00340                  * If the user wants UTF-8 and the envirionment variables
00341                  * support UTF-8, than set the locale accordingly
00342                  */
00343                 if (tOptions.eEncoding == encoding_utf_8) {
00344                         if (setlocale(LC_CTYPE, "") == NULL) {
00345                                 werr(1, "Can't set the UTF-8 locale! "
00346                                         "Check LANG, LC_CTYPE, LC_ALL.");
00347                         }
00348                         DBG_MSG("The UTF-8 locale has been set");
00349                 } else {
00350                         (void)setlocale(LC_CTYPE, "C");
00351                 }
00352 #endif /* __STDC_ISO_10646__ */
00353         } else {
00354                 if (setlocale(LC_CTYPE, "") == NULL) {
00355                         werr(0, "Can't set the locale! Will use defaults");
00356                         (void)setlocale(LC_CTYPE, "C");
00357                 }
00358                 DBG_MSG("The locale has been set");
00359         }
00360 #endif /* !__dos */
00361 
00362         bMultiple = argc - iFirst > 1;
00363         bUseTXT = tOptions.eConversionType == conversion_text ||
00364                 tOptions.eConversionType == conversion_fmt_text;
00365         bUseXML = tOptions.eConversionType == conversion_xml;
00366         iGoodCount = 0;
00367 
00368 #if defined(__dos)
00369         if (tOptions.eConversionType == conversion_pdf) {
00370                 /* PDF must be written as a binary stream */
00371                 setmode(fileno(stdout), O_BINARY);
00372         }
00373 #endif /* __dos */
00374 
00375         if (bUseXML) {
00376                 fprintf(stdout,
00377         "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
00378         "<!DOCTYPE %s PUBLIC \"-//OASIS//DTD DocBook XML V4.1.2//EN\"\n"
00379         "\t\"http://www.oasis-open.org/docbook/xml/4.1.2/docbookx.dtd\">\n",
00380                 bMultiple ? "set" : "book");
00381                 if (bMultiple) {
00382                         fprintf(stdout, "<set>\n");
00383                 }
00384         }
00385 
00386         //Output file
00387         #ifdef SYMBIAN
00388         ofp = fopen( argv[argc-1], "w");
00389         if ( ofp == NULL)
00390                 {
00391                         printf("Unable to open the output file: %s\n",argv[argc-1]);
00392                         exit(0);
00393                 }
00394         #endif /*SYMBIAN*/
00395 
00396         for (iIndex = iFirst; iIndex < argc-1; iIndex++) {
00397                 if (bMultiple && bUseTXT) {
00398                         szWordfile = szBasename(argv[iIndex]);
00399                         fprintf(stdout, "::::::::::::::\n");
00400                         fprintf(stdout, "%s\n", szWordfile);
00401                         fprintf(stdout, "::::::::::::::\n");
00402                 }
00403                 #ifndef SYMBIAN
00404                 if (bProcessFile(argv[iIndex])) {
00405                 #else
00406                 if (bProcessFile(argv[iIndex],ofp )) {
00407                 #endif  /*SYMBIAN*/
00408                 
00409                         iGoodCount++;
00410                 }
00411         }
00412 
00413         if (bMultiple && bUseXML) {
00414                 fprintf(stdout, "</set>\n");
00415         }
00416 
00417         DBG_DEC(iGoodCount);
00418         #ifdef SYMBIAN
00419         fclose(ofp);
00420         fprintf(stdout, "\n\nConversion is successful!!, check out this file : %s\n", argv[argc-1]);
00421         getchar();
00422         #endif /*SYMBIAN*/
00423         
00424         return iGoodCount <= 0 ? EXIT_FAILURE : EXIT_SUCCESS;
00425 } /* end of main */

Generated by  doxygen 1.6.2