home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / anwor032.zip / antiword.0.32 / main_u.c < prev    next >
C/C++ Source or Header  |  2001-05-25  |  6KB  |  238 lines

  1. /*
  2.  * main_u.c
  3.  *
  4.  * Released under GPL
  5.  *
  6.  * Copyright (C) 1998-2001 A.J. van Os
  7.  *
  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.
  12.  *
  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.
  17.  *
  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.
  21.  *
  22.  * Description:
  23.  * The main program of 'antiword' (Unix version)
  24.  */
  25.  
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #if defined(__dos)
  29. #include <fcntl.h>
  30. #endif /* __dos */
  31. #include "version.h"
  32. #include "antiword.h"
  33.  
  34. /* The name of this program */
  35. static const char    *szTask = NULL;
  36.  
  37.  
  38. static void
  39. vUsage(void)
  40. {
  41.     fprintf(stderr, "\tName: %s\n", szTask);
  42.     fprintf(stderr, "\tPurpose: "PURPOSESTRING"\n");
  43.     fprintf(stderr, "\tAuthor: "AUTHORSTRING"\n");
  44.     fprintf(stderr, "\tVersion: "VERSIONSTRING"\n");
  45.     fprintf(stderr, "\tStatus: "STATUSSTRING"\n");
  46.     fprintf(stderr,
  47.         "\tUsage: %s [switches] wordfile1 [wordfile2 ...]\n", szTask);
  48.     fprintf(stderr,
  49.         "\tSwitches: [-t|-p papersize][-m mapping][-w #][-i #][-X #]"
  50.         "[-Ls]\n");
  51.     fprintf(stderr, "\t\t-t text output (default)\n");
  52.     fprintf(stderr, "\t\t-p <paper size name> PostScript output\n");
  53.     fprintf(stderr, "\t\t   like: a4, letter or legal\n");
  54.     fprintf(stderr, "\t\t-m <mapping> character mapping file\n");
  55.     fprintf(stderr, "\t\t-w <width> in characters of text output\n");
  56.     fprintf(stderr, "\t\t-i <level> image level (PostScript only)\n");
  57.     fprintf(stderr, "\t\t-X <encoding> character set (Postscript only)\n");
  58.     fprintf(stderr, "\t\t-L use landscape mode (PostScript only)\n");
  59.     fprintf(stderr, "\t\t-s Show hidden (by Word) text\n");
  60. } /* end of vUsage */
  61.  
  62. /*
  63.  * pStdin2TmpFile - save stdin in a temporary file
  64.  *
  65.  * returns: the pointer to the temporary file or NULL
  66.  */
  67. static FILE *
  68. pStdin2TmpFile(long *lFilesize)
  69. {
  70.     FILE    *pTmpFile;
  71.     size_t    tSize;
  72.     BOOL    bFailure;
  73.     unsigned char    aucBytes[BUFSIZ];
  74.  
  75.     DBG_MSG("pStdin2TmpFile");
  76.  
  77.     /* Open the temporary file */
  78.     pTmpFile = tmpfile();
  79.     if (pTmpFile == NULL) {
  80.         return NULL;
  81.     }
  82.  
  83. #if defined(__dos)
  84.     /* Stdin must be read as a binary stream */
  85.     setmode(fileno(stdin), O_BINARY);
  86. #endif /* __dos */
  87.  
  88.     /* Copy stdin to the temporary file */
  89.     *lFilesize = 0;
  90.     bFailure = TRUE;
  91.     for (;;) {
  92.         tSize = fread(aucBytes, 1, sizeof(aucBytes), stdin);
  93.         if (tSize == 0) {
  94.             bFailure = feof(stdin) == 0;
  95.             break;
  96.         }
  97.         if (fwrite(aucBytes, 1, tSize, pTmpFile) != tSize) {
  98.             bFailure = TRUE;
  99.             break;
  100.         }
  101.         *lFilesize += (long)tSize;
  102.     }
  103.  
  104. #if defined(__dos)
  105.     /* Switch stdin back to a text stream */
  106.     setmode(fileno(stdin), O_TEXT);
  107. #endif /* __dos */
  108.  
  109.     /* Deal with the result of the copy action */
  110.     if (bFailure) {
  111.         *lFilesize = 0;
  112.         (void)fclose(pTmpFile);
  113.         return NULL;
  114.     }
  115.     rewind(pTmpFile);
  116.     return pTmpFile;
  117. } /* end of pStdin2TmpFile */
  118.  
  119. /*
  120.  * bProcessFile - process a single file
  121.  *
  122.  * returns: TRUE when the given file is a supported Word file, otherwise FALSE
  123.  */
  124. static BOOL
  125. bProcessFile(const char *szFilename)
  126. {
  127.     FILE        *pFile;
  128.     diagram_type    *pDiag;
  129.     long        lFilesize;
  130.  
  131.     fail(szFilename == NULL || szFilename[0] == '\0');
  132.  
  133.     DBG_MSG(szFilename);
  134.  
  135.     if (szFilename[0] == '-' && szFilename[1] == '\0') {
  136.         pFile = pStdin2TmpFile(&lFilesize);
  137.         if (pFile == NULL) {
  138.             werr(0, "I can't save the standard input to a file");
  139.             return FALSE;
  140.         }
  141.     } else {
  142.         lFilesize = lGetFilesize(szFilename);
  143.         if (lFilesize < 0) {
  144.             werr(0, "I can't get the size of '%s'", szFilename);
  145.             return FALSE;
  146.         }
  147.  
  148.         pFile = fopen(szFilename, "rb");
  149.         if (pFile == NULL) {
  150.             werr(0, "I can't open '%s' for reading", szFilename);
  151.             return FALSE;
  152.         }
  153.     }
  154.  
  155.     if (!bIsSupportedWordFile(pFile, lFilesize)) {
  156.         if (bIsRtfFile(pFile)) {
  157.             werr(0, "%s is not a Word Document."
  158.                 " It is probably a Rich Text Format file",
  159.                 szFilename);
  160.         } else if (bIsWord245File(pFile)) {
  161.             werr(0, "%s is not in a supported Word format."
  162.                 " It is probably from 'Word2, 4 or 5'",
  163.                 szFilename);
  164.         } else {
  165. #if defined(__dos)
  166.             werr(0, "%s is not a Word Document or the filename"
  167.                 " is not in the 8+3 format.", szFilename);
  168. #else
  169.             werr(0, "%s is not a Word Document.", szFilename);
  170. #endif /* __dos */
  171.         }
  172.         (void)fclose(pFile);
  173.         return FALSE;
  174.     }
  175.     /* Reset any reading done during file testing */
  176.     rewind(pFile);
  177.  
  178.     pDiag = pCreateDiagram(szTask, szFilename);
  179.     if (pDiag == NULL) {
  180.         (void)fclose(pFile);
  181.         return FALSE;
  182.     }
  183.  
  184.  
  185.     vWord2Text(pFile, lFilesize, pDiag);
  186.     vDestroyDiagram(pDiag);
  187.  
  188.     (void)fclose(pFile);
  189.     return TRUE;
  190. } /* end of bProcessFile */
  191.  
  192. int
  193. main(int argc, char **argv)
  194. {
  195.     options_type    tOptions;
  196.     const char    *szWordfile;
  197.     int    iFirst, iIndex, iGoodCount;
  198.     BOOL    bUsage, bMultiple, bUsePlainText;
  199.  
  200.     if (argc <= 0) {
  201.         return EXIT_FAILURE;
  202.     }
  203.  
  204.     szTask = szBasename(argv[0]);
  205.  
  206.     if (argc <= 1) {
  207.         iFirst = 1;
  208.         bUsage = TRUE;
  209.     } else {
  210.         iFirst = iReadOptions(argc, argv);
  211.         bUsage = iFirst <= 0;
  212.     }
  213.     if (bUsage) {
  214.         vUsage();
  215.         return iFirst < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
  216.     }
  217.  
  218.     vGetOptions(&tOptions);
  219.  
  220.     bMultiple = argc - iFirst > 1;
  221.     bUsePlainText = !tOptions.bUseOutlineFonts;
  222.     iGoodCount = 0;
  223.  
  224.     for (iIndex = iFirst; iIndex < argc; iIndex++) {
  225.         if (bMultiple && bUsePlainText) {
  226.             szWordfile = szBasename(argv[iIndex]);
  227.             fprintf(stdout, "::::::::::::::\n");
  228.             fprintf(stdout, "%s\n", szWordfile);
  229.             fprintf(stdout, "::::::::::::::\n");
  230.         }
  231.         if (bProcessFile(argv[iIndex])) {
  232.             iGoodCount++;
  233.         }
  234.     }
  235.     DBG_DEC(iGoodCount);
  236.     return iGoodCount <= 0 ? EXIT_FAILURE : EXIT_SUCCESS;
  237. } /* end of main */
  238.