home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / postscri / ps / ps.c next >
Text File  |  1994-05-04  |  9KB  |  297 lines

  1. //:: program: ps.c
  2. //:: compile: tcc -mt -lt ps.c
  3. //:: creator: d.reece
  4. //:: purpose: print simple text files in postscript.
  5. //:: written: 1.00 ... 01-12-94
  6. //:: revised: 1.01 ... 01-14-94 ... ensured fclose()
  7. //::                            ... added duplexing.
  8. //::          1.02 ... 01-15-94 ... avoid languagelevel 2 except for duplex.
  9. //::                            ... added dbf/mdx info.
  10. //::                            ... pitch page number if only one page.
  11. //::          1.03 ... 01-16-93 ... added time stamp.
  12. //::          1.04 ... 01-18-93 ... ability to specify beginning page number.
  13. //::          1.05 ... 05-03-94 ... removed dbf stuff.
  14.  
  15. #include <bios.h>
  16. #include <time.h>
  17. #include <ctype.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <fcntl.h>
  22.  
  23. #define VERSION "ps v1.05"
  24.  
  25. void help( void );
  26. void bail( char *cMessage, int rc );
  27. void padBS( char *s );
  28. int printOK( void );
  29. int writePP( char *filename, int pt, int pd, int pb );
  30. int writePS( char *filename, int pt );
  31. int writePT( int pd );
  32.  
  33. int main( int argc, char *argv[] )
  34. {
  35.     int rc;
  36.     int pd = 0;
  37.     int pb = 1;
  38.     int pt = 10;
  39.  
  40.     //:: if help requested or arguments lacking.
  41.     if( !strcmp( argv[1], "/?" ) || ( argc == 1 ) )
  42.         help();
  43.     //:: if duplex specified, pop space, toggle.
  44.     if( argv[2][0] == '=' )
  45.         pd = argv[2][0] = 32;
  46.     //:: if valid pointsize specified, change typeface size from 10.
  47.     if( ( argc > 2 ) && atoi( argv[2] ) )
  48.         pt = atoi( argv[2] );
  49.     //:: if valid starting page specified, change beginning page from 1.
  50.     if( ( argc > 3 ) && atoi( argv[3] ) )
  51.         pb = atoi( argv[3] );
  52.     //:: check status of LPT1.
  53.     if( ( rc = printOK() ) != 144 )
  54.         bail( "printer not captured?", rc );
  55.     //:: write header. should return nothing.
  56.     if( ( rc = writePP( argv[1], pt, pd, pb ) ) != 0 )
  57.         bail( "ps header failure?", rc );
  58.     //:: write file. should return nothing.
  59.     if( ( rc = writePS( argv[1], pt ) ) != 0 )
  60.         bail( "text print failure?", rc );
  61.     //:: write trailer. should return nothing.
  62.     if( ( rc = writePT( pd ) ) != 0 )
  63.         bail( "print trailer failure?", rc );
  64.     //:: leave stadium.
  65.     return( 0 );
  66. }
  67.  
  68. int printOK( void )
  69. {
  70.     int rc;
  71.     //:: couldn't figure actual decoding of biosprint().
  72.     //:: apparent returns: 144=OK, 104=out of paper, 72=offline, 1=inactive.
  73.     rc = biosprint( 2, '\0', 0 );
  74.     return( rc );
  75. }
  76.  
  77. void help( void )
  78. {
  79.     //:: simple help. duplexing request a secret option.
  80.     printf( "%s\narguments: [filename] [pointsize] [pagestart]\n", VERSION );
  81.     printf( "(c) 1994 reece novelties" );
  82.     exit( 1 );
  83. }
  84.  
  85. void bail( char *cMessage, int rc )
  86. {
  87.     //:: yer error handler.
  88.     printf( "\n%s: %s\nreturn code: %d\n", VERSION, cMessage, rc );
  89.     exit( 1 );
  90. }
  91.  
  92. void padBS( char *s )
  93. {
  94.     char cFile[128];
  95.     int x = 0;
  96.     int i = 0;
  97.     int t = 0;
  98.     x = strlen( s );
  99.     for( i = 0; i < x; i ++ )
  100.     {
  101.         if ( s[i] == 92 )
  102.         {
  103.             cFile[t] = s[i];
  104.             t++;
  105.         }
  106.         cFile[t] = s[i];
  107.         t++;
  108.     }
  109.     cFile[t] = '\0';
  110.     strcpy( s, cFile );
  111. }
  112.  
  113. int writePP( char *filename, int pt, int pd, int pb )
  114. {
  115.     time_t timer;
  116.     struct tm *t;
  117.     char cDate[32];
  118.     char cFile[129];
  119.     FILE *lpt;
  120.  
  121.     //:: copy filename, fix backslashes.
  122.     strcpy( cFile, filename );
  123.     padBS( cFile );
  124.     //:: open printer stream.
  125.     lpt = fopen( "PRN", "wb" );
  126.     //:: thrash around and extract current time.
  127.     timer = time( NULL );
  128.     t = localtime( &timer );
  129.     //:: store date string.
  130.     sprintf( cDate, "%02d-%02d-%02d %02d:%02d:%02d",
  131.         t->tm_mon + 1, t->tm_mday, t->tm_year,
  132.         t->tm_hour, t->tm_min, t->tm_sec );
  133.     //:: begin header:
  134.     //:: end any previous jobs with ctrl-d.
  135.     fprintf( lpt, "%c\n", 4 );
  136.     fprintf( lpt, "%%!PS-Adobe-2.0 EPSF-1.2\n" );
  137.     fprintf( lpt, "%%%%BoundingBox: 0 0 612 792\n" );
  138.     fprintf( lpt, "%%%%Title: %s\n", cFile );
  139.     fprintf( lpt, "%%%%Creator: PS.C\n" );
  140.     fprintf( lpt, "%%%%CreationDate: %s\n", cDate );
  141.     fprintf( lpt, "%%%%EndComments\n" );
  142.     //:: if duplexing requested, presume languagelevel 2 printer ability.
  143.     if( pd )
  144.     {
  145.         fprintf( lpt, "%%%%BeginSetup\n" );
  146.         fprintf( lpt, "%%%%BeginFeature: *Duplex\n" );
  147.         fprintf( lpt, "<< /Duplex true >> setpagedevice\n" );
  148.         fprintf( lpt, "%%%%EndFeature\n" );
  149.         fprintf( lpt, "%%%%EndSetup\n" );
  150.     }
  151.     fprintf( lpt, "%%%%BeginProlog\n" );
  152.     fprintf( lpt, "/str 3 string def\n" );
  153.     fprintf( lpt, "/pagenumber\n" );
  154.     fprintf( lpt, "{\n" );
  155.     //:: change to bold for page number info. if zero, skip
  156.     fprintf( lpt, "    /Courier-Bold findfont 10 scalefont setfont\n" );
  157.     fprintf( lpt, "    72 750 moveto (%s) show\n", cFile );
  158.     fprintf( lpt, "    p 0 ne\n" );
  159.     fprintf( lpt, "    {\n" );
  160.     fprintf( lpt, "        499 750 moveto (page ) show\n" );
  161.     fprintf( lpt, "        p str cvs show\n" );
  162.     fprintf( lpt, "    } if\n" );
  163.     //:: box 612x792 page.
  164.     fprintf( lpt, "    .1 setlinewidth\n" );
  165.     fprintf( lpt, "    72 740 moveto 468 0 rlineto stroke\n" );
  166.     fprintf( lpt, "    48 24 moveto\n" );
  167.     fprintf( lpt, "    516 0 rlineto 0 742 rlineto -516 0 rlineto\n" );
  168.     fprintf( lpt, "    closepath stroke\n" );
  169.     //:: show puny time stamp.
  170.     fprintf( lpt, "    /Courier findfont 4 scalefont setfont\n", pt );
  171.     fprintf( lpt, "    4 setlinewidth\n" );
  172.     fprintf( lpt, "    1 setgray\n" );
  173.     fprintf( lpt, "    284 23 moveto 43 0 rlineto stroke\n" );
  174.     fprintf( lpt, "    285 23 moveto\n" );
  175.     fprintf( lpt, "    0 setgray\n" );
  176.     fprintf( lpt, "    (%s) show\n", cDate );
  177.     //:: restore pointsize.
  178.     fprintf( lpt, "    /Courier findfont %d scalefont setfont\n", pt );
  179.     fprintf( lpt, "} bind def\n" );
  180.     fprintf( lpt, "/printloop\n" );
  181.     fprintf( lpt, "{\n" );
  182.     //:: define beginning page number.
  183.     fprintf( lpt, "    /p %d def\n", pb );
  184.     fprintf( lpt, "    {\n" );
  185.     fprintf( lpt, "        currentfile cvlit =string readline not\n" );
  186.     fprintf( lpt, "        {\n" );
  187.     fprintf( lpt, "            exit\n" );
  188.     fprintf( lpt, "        } if\n" );
  189.     fprintf( lpt, "        show currentpoint exch\n" );
  190.     //:: invoke leading as pt + 1.
  191.     fprintf( lpt, "        pop %d sub dup 36 le\n", pt + 1 );
  192.     fprintf( lpt, "        {\n" );
  193.     fprintf( lpt, "            pop\n" );
  194.     fprintf( lpt, "            p pagenumber\n" );
  195.     fprintf( lpt, "            /p p 1 add def\n" );
  196.     fprintf( lpt, "            pop showpage\n" );
  197.     fprintf( lpt, "           72 %d moveto\n", 730 - pt );
  198.     fprintf( lpt, "        }\n" );
  199.     fprintf( lpt, "        {\n" );
  200.     fprintf( lpt, "            72 exch moveto\n" );
  201.     fprintf( lpt, "        } ifelse\n" );
  202.     fprintf( lpt, "    } loop\n" );
  203.     fprintf( lpt, "    p 1 eq\n" );
  204.     fprintf( lpt, "    {\n" );
  205.     fprintf( lpt, "        /p 0 def\n" );
  206.     fprintf( lpt, "    } if\n" );
  207.     fprintf( lpt, "    p pagenumber\n" );
  208.     fprintf( lpt, "    showpage\n" );
  209.     fprintf( lpt, "} bind def\n" );
  210.     //:: set typeface to pt-point courier.
  211.     fprintf( lpt, "/Courier findfont %d scalefont setfont\n", pt );
  212.     fprintf( lpt, "72 %d moveto\n", 730 - pt );
  213.     //:: end header:
  214.     fprintf( lpt, "%%%%EndProlog\n" );
  215.     fprintf( lpt, "printloop\n" );
  216.     //:: DO NOT close lpt.
  217. //    NO! fclose( lpt );
  218.     //:: return nothing.
  219.     return( 0 );
  220. }
  221.  
  222.  
  223. int writePS( char *filename, int pt )
  224. {
  225.     int cc;
  226.     char c;
  227.     FILE *p;
  228.     FILE *lpt;
  229.  
  230.     //:: open printer stream.
  231.     lpt = fopen( "PRN", "wb" );
  232.     //:: if file can't be opened, complain and leave.
  233.     if( ( p = fopen( filename, "rb" ) ) == NULL )
  234.         bail( "file can't be opened?", -1 );
  235.     //:: open p and feed c to LPT1:
  236.     c = fgetc( p );
  237.     cc = 1;
  238.     while( !feof( p ) )
  239.     {
  240.         //:: if it's a tab, do three spaces.
  241.         if( c == 9 )
  242.         {
  243.             fprintf( lpt, "   " );
  244.             cc = cc + 2;
  245.         }
  246.         //:: if CR or two wide, whack.
  247.         if( ( c == 13 )
  248.         || ( cc > 127 )
  249.         || ( cc * pt > 800 ) )
  250.         {
  251.             fprintf( lpt, "\n" );
  252.             cc = 0;
  253.         }
  254.         //:: blow character out LPT port.
  255.         if( isprint( c ) )
  256.             fputc( c, lpt );
  257.         c = fgetc( p );
  258.         cc++;
  259.     }
  260.     //:: close job with ctrl-d.
  261.     fprintf( lpt, "\n%%%%EOF%c", 4 );
  262.     //:: close source file.
  263.     fclose( p );
  264.     fclose( lpt );
  265.     //:: return nothing.
  266.     return( 0 );
  267. }
  268.  
  269. int writePT( int pd )
  270. {
  271.     FILE *lpt;
  272.  
  273.     //:: if duplexing requested, turn off. presume languagelevel 2.
  274.     //:: submit trailer job to turn duplexing off.
  275.     if( pd )
  276.     {
  277.         //:: open printer stream.
  278.         lpt = fopen( "PRN", "wb" );
  279.         fprintf( lpt, "%c\n", 4 );
  280.         fprintf( lpt, "%%!PS-Adobe-2.0 EPSF-1.2\n" );
  281.         fprintf( lpt, "%%%%BoundingBox: 0 0 612 792\n" );
  282.         fprintf( lpt, "%%%%Title: EndDuplex\n" );
  283.         fprintf( lpt, "%%%%Creator: PS.C\n" );
  284.         fprintf( lpt, "%%%%EndComments\n" );
  285.         fprintf( lpt, "%%%%BeginSetup\n" );
  286.         fprintf( lpt, "%%%%BeginFeature: *Duplex\n" );
  287.         fprintf( lpt, "<< /Duplex false >> setpagedevice\n" );
  288.         fprintf( lpt, "%%%%EndFeature\n" );
  289.         fprintf( lpt, "%%%%EndSetup\n" );
  290.         fprintf( lpt, "\n%%%%EOF%c", 4 );
  291.         fclose( lpt );
  292.     }
  293.     //:: return nothing.
  294.     return( 0 );
  295. }
  296.  
  297.