Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. /*=============================================================================
  2.    GNU UnRTF, a command-line program to convert RTF documents to other formats.
  3.    Copyright (C) 2000,2001 Zachary Thayer Smith
  4.  
  5.    This program is free software; you can redistribute it and/or modify
  6.    it under the terms of the GNU General Public License as published by
  7.    the Free Software Foundation; either version 2 of the License, or
  8.    (at your option) any later version.
  9.  
  10.    This program is distributed in the hope that it will be useful,
  11.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.    GNU General Public License for more details.
  14.  
  15.    You should have received a copy of the GNU General Public License
  16.    along with this program; if not, write to the Free Software
  17.    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  18.  
  19.    The author is reachable by electronic mail at tuorfa@yahoo.com.
  20. =============================================================================*/
  21.  
  22.  
  23. /*----------------------------------------------------------------------
  24.  * Module name:    main.c
  25.  * Author name:    Zach Smith
  26.  * Create date:    01 Sep 00
  27.  * Purpose:        main() routine with file open/close.
  28.  *----------------------------------------------------------------------
  29.  * Changes:
  30.  * 14 Oct 00, tuorfa@yahoo.com: added -nopict option
  31.  * 15 Oct 00, tuorfa@yahoo.com: added verify_file_type()
  32.  * 08 Apr 01, tuorfa@yahoo.com: more GNU-like switches implemented
  33.  * 24 Jul 01, tuorfa@yahoo.com: removed verify_file_type()
  34.  * 03 Aug 01, tuorfa@yahoo.com: added --inline switch
  35.  * 08 Sep 01, tuorfa@yahoo.com: added use of PROGRAM_NAME
  36.  * 19 Sep 01, tuorfa@yahoo.com: addition of output personalities
  37.  * 22 Sep 01, tuorfa@yahoo.com: added function-level comment blocks
  38.  * 23 Sep 01, tuorfa@yahoo.com: added wpml switch
  39.  *--------------------------------------------------------------------*/
  40.  
  41.  
  42.  
  43. #include <stdio.h>
  44. #include <string.h>
  45.  
  46. #include "defs.h"
  47. #include "error.h"
  48. #include "word.h"
  49. #include "convert.h"
  50. #include "parse.h"
  51. #include "hash.h"
  52. #include "malloc.h"
  53.  
  54. #include "output.h"
  55. #include "html.h"
  56. #include "text.h"
  57. #include "vt.h"
  58. #include "ps.h"
  59. #include "latex.h"
  60. #include "wpml.h"
  61.  
  62.  
  63.  
  64. int nopict_mode; /* TRUE => do not write \pict's to files */
  65. int dump_mode;   /* TRUE => output a dump of the word tree */
  66. int debug_mode;  /* TRUE => output comments within HTML */
  67. int lineno;      /* used for error reporting */
  68. int simple_mode; /* TRUE => output HTML without SPAN/DIV tags */
  69. int inline_mode; /* TRUE => output HTML without HTML/BODY/HEAD */
  70.  
  71.  
  72. OutputPersonality *op;
  73. enum {
  74.         OP_HTML, OP_TEXT, OP_LATEX, OP_PS, OP_VT, OP_WPML
  75. };
  76.  
  77.  
  78.  
  79. /*========================================================================
  80.  * Name:        main
  81.  * Purpose:     Main control function.
  82.  * Args:        Args.
  83.  * Returns:     Exit code.
  84.  *=======================================================================*/
  85.  
  86. int
  87. main (int argc, char **argv)
  88. {
  89.         FILE *f;
  90.         Word * word;
  91.         char *path=NULL;
  92.         int i;
  93.         int output_format = OP_HTML;
  94.  
  95.         nopict_mode = debug_mode = dump_mode = inline_mode = FALSE;
  96.  
  97.         if (argc<2 || argc>7) usage();
  98.  
  99.         for (i=1; i<argc; i++) {
  100.                 if (!strcmp("--dump",argv[i])) dump_mode=TRUE;
  101.                 else if (!strcmp("-d",argv[i])) dump_mode=TRUE;
  102.                 else if (!strcmp("--debug",argv[i])) debug_mode=TRUE;
  103.                 else if (!strcmp("--simple",argv[i])) simple_mode=TRUE;
  104.                 else if (!strcmp("--html",argv[i])) output_format=OP_HTML;
  105.                 else if (!strcmp("--text",argv[i])) output_format=OP_TEXT;
  106.                 else if (!strcmp("--vt",argv[i])) output_format=OP_VT;
  107.                 else if (!strcmp("--ps",argv[i])) output_format=OP_PS;
  108.                 else if (!strcmp("--latex",argv[i])) output_format=OP_LATEX;
  109.                 else if (!strcmp("--wpml",argv[i])) output_format=OP_WPML;
  110.                 else if (!strcmp("-t",argv[i])) {
  111.                         if ((i+1)<argc && *argv[i+1]!='-') {
  112.                                 i++;
  113.                                 if (!strcmp ("html", argv[i]))
  114.                                         output_format=OP_HTML;
  115.                                 else if (!strcmp ("vt", argv[i]))
  116.                                         output_format=OP_VT;
  117.                                 else if (!strcmp ("text", argv[i]))
  118.                                         output_format=OP_TEXT;
  119.                                 else if (!strcmp ("ps", argv[i]))
  120.                                         output_format=OP_PS;
  121.                                 else if (!strcmp ("latex", argv[i]))
  122.                                         output_format=OP_LATEX;
  123.                                 else if (!strcmp ("wpml", argv[i]))
  124.                                         output_format=OP_WPML;
  125.                         }
  126.                 }
  127.                 else if (!strcmp("--inline",argv[i])) inline_mode=TRUE;
  128.                 else if (!strcmp("--help",argv[i]))  {
  129.                         usage();
  130.                         exit (0);
  131.                 }
  132.                 else if (!strcmp("--version",argv[i]))  {
  133.                         fprintf (stderr, "%s\n", PROGRAM_VERSION);
  134.                         exit (0);
  135.                 }
  136.                 else if (!strcmp("--nopict",argv[i])) nopict_mode=TRUE;
  137.                 else if (!strcmp("-n",argv[i])) nopict_mode=TRUE;
  138.                 else {
  139.                         if (*argv[i]=='-') usage();
  140.  
  141.                         if(path)
  142.                                 usage();
  143.                         else    
  144.                                 path=argv[i];
  145.                 }
  146.         }
  147.        
  148.         if (!path) usage();
  149.  
  150.         switch (output_format) {
  151.         case OP_TEXT:
  152.                 op = text_init();
  153.                 break;
  154.         case OP_VT:
  155.                 op = vt_init();
  156.                 break;
  157.         case OP_HTML:
  158.                 op = html_init();
  159.                 break;
  160.         case OP_PS:
  161.                 op = ps_init();
  162.                 break;
  163.         case OP_LATEX:
  164.                 op = latex_init();
  165.                 break;
  166.         case OP_WPML:
  167.                 op = wpml_init();
  168.                 break;
  169.         default:
  170.                 error_handler ("unknown output format");
  171.         }
  172.  
  173.         hash_init();
  174.  
  175.         fprintf (stderr, "This is %s, ", PROGRAM_NAME);
  176.         fprintf (stderr, "version %s\n", PROGRAM_VERSION);
  177.         fprintf (stderr, "By Zach T. Smith\n");
  178.  
  179.         if (debug_mode) fprintf (stderr, "Debug mode.\n");
  180.         if (dump_mode) fprintf (stderr, "Dump mode.\n");
  181.  
  182.         f = fopen (path, "r");
  183.         if (!f) {
  184.                 char path2[200];
  185.                 strcpy(path2,path); strcat(path2,".rtf");
  186.                 f = fopen(path2, "r");
  187.                 if(!f)
  188.                         error_handler ("cannot open input file");
  189.         }
  190.  
  191.         fprintf(stderr,"Processing %s...\n", path);
  192.  
  193.         lineno=0;
  194.  
  195.         word = word_read (f);
  196.  
  197.         if (dump_mode) {
  198.                 word_dump (word);
  199.                 printf ("\n");
  200.         } else {
  201.                 word_print (word);
  202.         }
  203.  
  204.         fclose(f);
  205.  
  206.         fprintf(stderr,"Done.\n");
  207.  
  208.         hash_stats();
  209.  
  210.         if (debug_mode) {
  211.                 fprintf (stderr, "Total memory allocated %ld bytes.\n",
  212.                         total_malloced());
  213.         }
  214.  
  215.         /* May as well */
  216.         word_free (word);
  217.  
  218.         return 0;
  219. }
  220.  
  221.  
  222.