home *** CD-ROM | disk | FTP | other *** search
/ Carousel Volume 2 #1 / carousel.iso / mactosh / unix / unix_mw2.sha / main.c < prev    next >
C/C++ Source or Header  |  1985-04-26  |  4KB  |  259 lines

  1. #include    <stdio.h>
  2. #include    <signal.h>
  3.  
  4. #include    "site.h"
  5. #include    "gen.h"
  6. #include    "option.h"
  7. #include    "md.h"
  8.  
  9. LOCAL    char    * program;
  10. LOCAL    char    * outfile    = NULL;
  11. LOCAL    char    tmpfile[] = "/tmp/mwXXXXXX";
  12. LOCAL    bool    temp = FALSE;
  13.  
  14. #ifdef    BSD_42
  15. LOCAL    char    pr_opts[BUFSIZ]    = "";
  16. #endif    BSD_42
  17. LOCAL    void    sig();
  18.  
  19.  
  20. /*
  21.  *    global variables
  22.  */
  23. FILE    * infd;
  24. FILE    * outfd = NULL;
  25.  
  26.  
  27.  
  28. /*
  29.  *    options
  30.  */
  31. bool    verbose    = FALSE;
  32. bool    spool = FALSE;
  33. bool    debflg    = FALSE;
  34. char    * font_sub_file    = NULL;
  35.  
  36.  
  37. extern    char    * getenv();
  38.  
  39.  
  40. main( argc, argv )
  41. int    argc;
  42. char    * argv[];
  43.     {
  44.  
  45.     bool    first_time = TRUE;
  46.  
  47.     program = argv[0];
  48.  
  49.     /*
  50.      *    signals, signals, signals!
  51.      */
  52.     signal( SIGINT, sig );
  53.     signal( SIGQUIT, sig );
  54.     signal( SIGHUP, sig );
  55.  
  56.     /*
  57.      *    take a shot at some environment variables
  58.      */
  59.     font_sub_file = getenv( "MACFONTS" );
  60.  
  61.  
  62.     /*
  63.      *    process command line options
  64.      */
  65.     while( (++argv, --argc) && (*argv)[0] == '-' )
  66.     switch( (*argv)[1] )
  67.         {
  68.  
  69.         /*    be noisy    */
  70.         case 'v':
  71.             verbose = TRUE;
  72.             break;
  73.  
  74.         /*    spool resultant output    */
  75.         case 'p':
  76.             spool = TRUE;
  77.             break;
  78.         
  79. #ifdef    BSD_42
  80.         case 'P':
  81.             strcat( pr_opts, " " );
  82.             strcat( pr_opts, *argv );
  83.             spool = TRUE;
  84.             break;
  85. #endif    BSD_42
  86.         
  87.  
  88.         /*    debug output    */
  89.         case 'D':
  90.             debflg = TRUE;
  91.             break;
  92.  
  93.         /*    font substitution file    */
  94.         case 's':
  95.             if( --argc == 0 )
  96.                 fatal( "must specify output file" );
  97.  
  98.             font_sub_file = *(++argv);
  99.             break;
  100.             
  101.  
  102.         /*    put mah output here    */
  103.         case 'o':
  104.             if( --argc == 0 )
  105.                 fatal( "must specify output file" );
  106.  
  107.             outfile = *(++argv);
  108.             break;
  109.         
  110.         default:
  111.             fatal( "unknown option %c", (*argv)[1] );
  112.  
  113.         }
  114.     
  115.  
  116.     /*
  117.      *    do it
  118.      */
  119.     if( argc == 0 )
  120.         fatal( "no input file" );
  121.     
  122.  
  123.     /*    all output goes to a file     */
  124.     if( outfile == NULL )
  125.         {
  126.         if( !spool )
  127.             fatal( "no output file" );
  128.         else
  129.             {
  130.             mktemp( tmpfile );
  131.             temp = TRUE;
  132.             outfile = tmpfile;
  133.             }
  134.         }
  135.     
  136.  
  137.     if( (outfd = fopen( outfile, "w" )) == NULL )
  138.         fatal( "couldn't open %s", outfile );
  139.  
  140.  
  141.     
  142.  
  143.     while( argc-- )
  144.         {
  145.  
  146.         if( (infd = fopen( *(argv++), "r" )) == NULL )
  147.             fatal( "couldn't open macwrite file %s", *argv );
  148.         
  149.         if( verbose && argc > 0 )
  150.             fprintf( stderr, "%s:\n", *argv );
  151.  
  152.         if( first_time )
  153.             {
  154.             init();
  155.             first_time = FALSE;
  156.             }
  157.  
  158.         read_globals();
  159.         body();
  160.  
  161.         fclose( infd );
  162.  
  163.         }
  164.     
  165.     finish();
  166.  
  167.     fclose( outfd );
  168.     if( verbose )
  169.         fprintf( stderr, "%d page(s) produced\n", page_cnt );
  170.  
  171.     if( spool )
  172.         spool_file();
  173.  
  174.     Exit( 0 );
  175.  
  176.     }
  177.  
  178.  
  179. Exit( rc )
  180. int    rc;
  181.     {
  182.  
  183.     if( temp && outfd )
  184.         unlink( tmpfile );
  185.     
  186.     exit( rc );
  187.  
  188.     }
  189.  
  190.  
  191.  
  192. /*
  193.  *    SYSTEM DEPENDENT!
  194.  */
  195.  
  196.  
  197. /*    write dvi output directly to imagen    */
  198. spool_file()
  199.     {
  200.     char    buf[300];
  201.     bool    temp = FALSE;
  202.  
  203.     if( verbose )
  204.         fprintf( stderr, "spooling dvi output\n" );
  205.  
  206.     /*
  207.      *    must use less precise -a setting because of common bug in
  208.      *  dviimp  ->  fixptrsuconv calculated using 2E20 fix/pt, 
  209.      *  not 2^20 fix/pt
  210.      */
  211.  
  212. #ifdef    BSD_41
  213.     sprintf( buf, "dviimp -a %s | ipr -Limpress", outfile );
  214. #endif    BSD_41
  215.  
  216. #ifdef    BSD_42
  217.     sprintf( buf, "TERMCAP=/etc/printcap; export TERMCAP; dviimp -a -p %s %s", pr_opts, outfile );
  218. #endif    BSD_42
  219.  
  220.     if( system( buf ) != 0 )
  221.         fatal( "error in spooling" );
  222.  
  223.  
  224.     }
  225.  
  226.  
  227. LOCAL    void
  228. sig()
  229.     {
  230.     fprintf( stderr, "%s aborted\n", program );
  231.     Exit( 1 );
  232.     }
  233.  
  234.  
  235. fatal( fmt, a1, a2, a3, a4 )
  236. char    * fmt;
  237. int    a1, a2, a3, a4;
  238.     {
  239.  
  240.     fprintf( stderr, "%s: fatal - ", program );
  241.     fprintf( stderr, fmt, a1, a2, a3, a4 );
  242.     putc( '\n', stderr );
  243.  
  244.     Exit( 1 );
  245.  
  246.     }
  247.  
  248. warning( fmt, a1, a2, a3, a4 )
  249. char    * fmt;
  250. int    a1, a2, a3, a4;
  251.     {
  252.  
  253.     fprintf( stderr, "%s: warning - ", program );
  254.     fprintf( stderr, fmt, a1, a2, a3, a4 );
  255.     putc( '\n', stderr );
  256.  
  257.     }
  258.  
  259.