home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / texipf21.zip / texi2ipf / texi2ipf.c < prev    next >
C/C++ Source or Header  |  1997-02-15  |  5KB  |  174 lines

  1. /*
  2.  * texi2ipf.c - texi2ipf mainline
  3.  *
  4.  * texi2roff history:
  5.  *             Release 1.0a    August 1988
  6.  *             Release 2.0     January 1990
  7.  *
  8.  * Copyright 1988, 1989, 1990  Beverly A.Erlebacher
  9.  * erlebach@cs.toronto.edu    ...uunet!utai!erlebach
  10.  *
  11.  * texi2ipf history:
  12.  *             Release 1.0     February 1993
  13.  *
  14.  * Modified by Marcus Gröber, Fido 2:2402/61.1
  15.  *
  16.  * Modified by Martin "Herbert" Dietze, Email herbert@wiloyee.shnet.org
  17.  *
  18.  */
  19.  
  20. /*
  21.  * History:
  22.  *
  23.  * $Log: texi2ipf.c,v $
  24.  * Revision 1.6  1997/02/06 12:45:17  herbert
  25.  * - Added documentation in Texinfo format.
  26.  * - Minor bug fixes.
  27.  *
  28.  * Revision 1.5  1997/02/04 13:05:43  herbert
  29.  * Menu descriptions of more than one lines are now converted correctly if they
  30.  * are indented by more than 25 whitespaces (1 tab == 8 spaces).
  31.  *
  32.  * Revision 1.4  1997/01/10 14:56:44  herbert
  33.  * Fixed a weird error under OS/2 caused by too few stack.
  34.  *
  35.  * Revision 1.3  1996/12/17 15:14:22  herbert
  36.  * Only some cosmetic changes. The code looks still rather ugly to me :-)
  37.  *
  38.  * Revision 1.2  1996/12/17 14:10:01  herbert
  39.  * Added support for pseudo-Texinfo-commands: @ifhtml (ignored) and @ipfline{}
  40.  * (my invention) for putting IPF code into the Texinfo source.
  41.  * Added @macro command to table.h, will be ignored.
  42.  *
  43.  * Revision 1.1.1.1  1996/12/02 12:10:01  herbert
  44.  * Texi2IPF 1.0
  45.  *
  46.  */
  47.  
  48. #include "texi2ipf.h"
  49. #include <stdlib.h>
  50. #include <stdio.h>
  51. #ifndef __TURBOC__
  52. #include <sys/types.h>
  53. #endif
  54. #include <sys/stat.h>
  55. #include <string.h>
  56. #include <setjmp.h>
  57.  
  58. static char * id =
  59. "@(#)$Id: texi2ipf.c,v 1.6 1997/02/06 12:45:17 herbert Exp $";
  60.  
  61. /*
  62.  * I start by version 2.0 since it is the first version touched by me...
  63.  * (herbert)
  64.  */
  65. static char * version = "2.1";
  66.  
  67. char *progname;
  68. jmp_buf cleanup_point;  /* needed to reach the end-of-file processing
  69.                          * via longjmp() after encountering @bye */    
  70.  
  71. /*
  72.  * main - parse arguments, handle options
  73.  *     - initialize tables and other strings
  74.  *     - open files and pass them to process().
  75.  */
  76. int main( int argc, char * argv[])
  77. {
  78.     int errflg = 0;
  79.     FILE *in;
  80.     char *inname;
  81.     int h_optind = 1;   /* to keep structure without using getopt() */
  82.  
  83.     int process( FILE*, char*);
  84.     void initialize( void);
  85.  
  86.     progname = argv[0];
  87.  
  88.     if ( errflg || argc < 2 ) {
  89.         (void) fprintf( stderr,
  90.                        "\ntexi2ipf -=- Convert GNU texinfo files to "
  91.                        "OS/2 IPFC source code, version %s\n"
  92.                        "Based on texi2roff by Beverly A.Erlebacher (1990)\n"
  93.                        "Modified by Marcus Groeber (1993)\n"
  94.                        "Modified by Martin \"Herbert\" Dietze (1997)\n\n"
  95.                        "Usage: %s texi_file(s) [>ipfc_file]\n", 
  96.                         version, progname);
  97.         exit(1);
  98.     }
  99.  
  100.     initialize();
  101.  
  102.     if ( !setjmp( cleanup_point) ) /* Store address for longjmp() after @bye
  103.                                     * in Texinfo file... */
  104.         if ( h_optind >= argc ) {
  105.             errflg += process( stdin, "stdin");
  106.         } else
  107.             for (; h_optind < argc; h_optind++) {
  108.                 if ( STREQ(argv[h_optind], "-") ) {
  109.                     inname = "stdin";
  110.                     in = stdin;
  111.                 } else {
  112.     #ifdef __IBMC__
  113.     #  define OM_ "r"
  114.     #else
  115.     #  define OM_ "rt"
  116.     #endif
  117.                     if ( ( in = fopen(argv[h_optind], OM_)) == NULL ) {
  118.                         (void) fprintf( stderr, "%s : can't open file %s\n",
  119.                                         progname, argv[h_optind]);
  120.                         continue;
  121.                     }/* if */
  122.                     inname = argv[h_optind];
  123.                 }/* if */
  124.                 errflg += process( in, inname);
  125.                 if ( in != stdin )
  126.                     (void) fclose( in);
  127.                 /* endif */
  128.             }/* for */
  129.         /* endif */
  130.     else
  131.         if( !errflg )     /* we don't close files here as we exit the program anyway... */
  132.             puts( cmds->exit);
  133.         /* endif */
  134.     /* endif */
  135.     exit( errflg);
  136.     return 0; /* keep the compiler happy... */
  137. }
  138.  
  139. /*
  140.  * process -  check opened files and pass them to translate().
  141.  *        -  report on disastrous translation failures
  142.  */
  143. int process( FILE *fp, char *filename)
  144. {
  145.     struct stat statbuf;
  146.     int translate( FILE *, char *);
  147.  
  148.     if ( fstat(fileno(fp), &statbuf) != 0 ){
  149.         (void) fprintf( stderr, "%s : can't fstat file %s\n", progname,
  150.                         filename);
  151.         return 1;
  152.     }
  153. #ifdef __IBMC__
  154.     if (statbuf.st_mode & S_IFDIR) {
  155. #else
  156.     if ((statbuf.st_mode & S_IFMT)==S_IFDIR) {
  157. #endif
  158.         (void) fprintf( stderr, "%s : %s is a directory\n", progname,
  159.                         filename);
  160.         return 1;
  161.     }
  162.     /* translate returns 0 (ok) or -1 (disaster). it isn't worthwhile
  163.      * to try to recover from a disaster.
  164.      */
  165.     if ( translate( fp, filename) < 0 ) {
  166.         (void) fprintf( stderr,
  167.                         "%s: error while processing file %s,"
  168.                         " translation aborted\n",
  169.                         progname, filename);
  170.         exit( 1);
  171.     }
  172.     return 0;
  173. }
  174.