home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 19 Printer / 19-Printer.zip / lj2up2.zip / LJ2UP.C < prev    next >
Text File  |  1988-07-15  |  12KB  |  444 lines

  1. /* This change was made to test slick options
  2. **
  3. **  LJ2UP -- A printing utility for the HP LaserJet
  4. **
  5. **  This program prints a series of files on the LaserJet printer.  The
  6. **  files are printed in a "landscape" font at 17 characters to the inch.
  7. **  To take advantage of this density, two "pages" of information from
  8. **  the file are printed on each piece of paper (left and right halves).
  9. **
  10. **  Usage is:   LJ2UP  [-psvtdo] file1 file2 file3 ...
  11. **
  12. **          p   Page length
  13. **          s   Set Forth "screenfile" mode
  14. **          v   Turn off vertical line on page
  15. **          t   Set tabs
  16. **          d   Supress date and file stamp
  17. **          o   Re-direct output to device or file
  18. **          file    Any MS-DOS file spec, including wildcards
  19. **              and directories
  20. **
  21. **  Joe Barnhart    original version for Lattice C      May 5, 1985
  22. **  Ray Duncan  date and time stamping          May 22, 1985
  23. **  Joe Barnhart    revised date stamping           June 6, 1985
  24. **  Ray Duncan  modified for Microsoft C        Oct. 18, 1985
  25. **  Joe Barnhart    added wildcards, split long lines,
  26. **          command line switches, screenfiles  July 24, 1986
  27. **  Joe Barnhart    added pathname support, prt redirect    Oct 26, 1986
  28. **  Joe Barnhart    added vertical line to page     Apr 3, 1987
  29. **  Steve Coles  Changed to run under OS/2 as Protect App Oct 19 1987
  30. **  Steve Coles  Changed to run under MSC 1.1 with W3 error checking
  31. **
  32. */
  33.  
  34. /* Protoytpes for functions in this module */
  35. void main(int argc,char * *argv);
  36. static  void printfile(char *filename);
  37. static  void printforth(char *filename);
  38. static  int printscr(int fd,int screen);
  39. static  void println(char *text,int ln);
  40. static  void blanks(char *textptr);
  41. static  void newline(void );
  42. static  void newpage(void );
  43. static  void vline(void );
  44. static  void kickpage(void );
  45. static  void stamp(void );
  46. static  void datestamp(char *datestr);
  47. static  void timestamp(char *timestr);
  48.  
  49. /* defines and includes for kernel support */
  50. #define INCL_NOCOMMON
  51. #define INCL_DOSDATETIME
  52. #include <os2.h>
  53.  
  54. /* std C LIB includes */
  55. #include <fcntl.h>
  56. #include <stdio.h>
  57. #include <stdlib.h>
  58. #include <io.h>
  59. #include <string.h>
  60.  
  61. #include "getargs.h"        /* needed by getargs */
  62.  
  63. /* Prototypes For Functions in other LJ2UP Modules */
  64. #include "stoi.h"
  65. #include "fsearch.h"
  66. #include "getargs.pro"
  67.  
  68.  
  69. #define FALSE   0
  70. #define TRUE    1
  71. #define ENV "LJ"        /* name of environment string */
  72. #define ENVLEN  20      /* max characters in environment str */
  73. #define DEFPRT  "PRN"       /* default printer device */
  74. #define DEFLINE 58      /* default lines per page */
  75. #define DEFCOL  80      /* default number of columns */
  76. #define MAXLINE 68      /* height of page in lines */
  77. #define PAGE    '\014'          /* form feed */
  78. #define TAB 8       /* width of one tab stop */
  79. #define LEFT    0       /* printing on left side of page */
  80.  
  81.  
  82. /*
  83. **  The following defines provide control over the Hewlett-Packard
  84. **  LaserJet and LaserJet PLUS printers.
  85. */
  86.  
  87. #define CLEAR       fprintf(prn, "\033E")
  88. #define INITLJ      fprintf(prn, "\033&l1o8D\033(s17H\033&l64F\033&l0L")
  89. #define TOPMAR(x)   fprintf(prn, "\033&l%dE", x)
  90. #define MARGIN(l,r) fprintf(prn, "\033&a0r%dm%dL\015",r,l )
  91. #define ROW(r)      fprintf(prn, "\033&a%dR",r )
  92. #define RHALF       MARGIN( 92,172 )
  93. #define LHALF       MARGIN( 5,85 )
  94. #define MIDDLE      MARGIN( 88,90 )
  95.  
  96. typedef struct {
  97.         int ax, bx, cx, dx, si, di, cflag;
  98.     } REGSET;
  99.  
  100.  
  101. char    *ljenv[2];      /* array of pointers to strings */
  102. int line;           /* global line number */
  103. int pagenum;        /* global page number (physical) */
  104. int nostamp = FALSE;    /* date and time stamping on/off */
  105. int forthscr = FALSE;   /* Forth screen mode on/off */
  106. int nograf = FALSE;     /* graphics (boxes) on/off */
  107. int ibm = FALSE;        /* IBM print mode, 66 lines/page */
  108. int side = LEFT;        /* which side of page now printing */
  109. int lastline = DEFLINE; /* last printed line */
  110. int maxline = DEFLINE;  /* number of lines per page */
  111. int maxcol = DEFCOL;    /* length of one line */
  112. int tab = TAB;      /* value of tab stops */
  113. char    *prtdev = DEFPRT;   /* print device name */
  114. char    *printname;     /* name of file being printed */
  115. FILE    *prn;           /* printer device stream */
  116.  
  117. ARG argtab[] = {
  118.     { 'd', BOOLEAN, &nostamp,   "turn off date and time stamp"  },
  119.     { 's', BOOLEAN, &forthscr,  "enable Forth screenfile mode"  },
  120.     { 'v', BOOLEAN, &nograf,    "disable vertical line"     },
  121.     { 't', INTEGER, &tab,       "spacing of tab stops"      },
  122.     { 'p', INTEGER, &maxline,   "print length in lines"     },
  123.     { 'i', BOOLEAN, &ibm,       "IBM mode, 66 lines, no FF's"   },
  124.     { 'o', STRING,  (int *)&prtdev, "output device or file"     }
  125. };
  126. #define TABSIZE ( sizeof(argtab) / sizeof(ARG) )
  127.  
  128.  
  129. void main(argc, argv)
  130. int   argc;
  131. char  *argv[];
  132. {
  133.     int   filenum, top;
  134.     int   dummy,fhPRN;
  135.     char  *fspec, *path, *filename, *p;
  136.     char  fullname[64];
  137.     FILE  *fopen();
  138.  
  139.  
  140.     printf( "LaserJet landscape lister, version 2.0\n" );
  141.     printf( "Copyright (c) 1987 by Joe Barnhart\n\n" );
  142.  
  143.     /* First check for default preferences with LJ=-xxx enviromnent str */
  144.  
  145.     if( ( p = getenv( ENV ) ) != NULL )
  146.     {
  147.     ljenv[0] = "";
  148.     ljenv[1] = p;
  149.     dummy = 2;
  150.     getargs( dummy, ljenv, argtab, TABSIZE );
  151.     }
  152.  
  153.     /* Next, scan the command line for switches that override defaults */
  154.  
  155.     argc = getargs( argc, argv, argtab, TABSIZE );
  156.     if( !ibm )
  157.     {
  158.     if( forthscr )
  159.         top = 5;
  160.     else
  161.         top = (MAXLINE - maxline) / 2;
  162.     if( nostamp )
  163.         lastline = maxline;
  164.     else
  165.         lastline = maxline-2;
  166.     }
  167.     else
  168.     {
  169.     top = 0;
  170.     lastline = maxline = 65;
  171.     nostamp = TRUE;
  172.     }
  173.  
  174.     /* Open printer and begin */
  175.     /* The following obtuse method of opening the file and then fdopen
  176.     is required because of a bug in api.lib; necessary for bound apps. */
  177.  
  178.     fhPRN = open(prtdev,O_WRONLY);
  179.     if( fhPRN == -1 )
  180.     {
  181.     printf( "Error opening print device: %s - Error number = %d\n", prtdev,errno );
  182.     exit( 1 );
  183.     }
  184.     prn = fdopen( fhPRN, "w" );
  185.     if( prn == NULL )
  186.     {
  187.     printf( "Error steam opening print device: %s - Error number = %d\n", prtdev,errno );
  188.     exit( 1 );
  189.     }
  190.  
  191.     CLEAR;
  192.     INITLJ;
  193.     TOPMAR( top );
  194.     for( filenum = 1; filenum < argc; filenum++ ) {
  195.     fspec = argv[filenum];
  196.     path = fpath( fspec );
  197.     filename = fsearch( fspec );
  198.     if( filename == NULL )
  199.         printf("File %s not found.\n", fspec );
  200.     else
  201.         do {
  202.         strcpy( fullname, path );
  203.         strcat( fullname, filename );
  204.         printname = filename;
  205.         if (forthscr)
  206.             printforth( fullname );
  207.         else
  208.             printfile( fullname );
  209.         filename = fnext();
  210.         } while( filename != NULL );
  211.     }
  212.     CLEAR;
  213.     fclose( prn );
  214. }
  215.  
  216.  
  217. static  void printfile( filename )
  218. char *filename;
  219. {
  220.     FILE *fp;
  221.     register char  c;
  222.     register col;
  223.  
  224.     fp = fopen( filename ,"r" );
  225.     if( fp == NULL ) {
  226.     printf( "Error opening file: %s.\n", filename );
  227.     return;
  228.     }
  229.     else
  230.     printf( "Now printing file: %s\n", filename );
  231.     line = col = 0;
  232.     pagenum = 1;
  233.     LHALF;
  234.     side = LEFT;
  235.     while( !feof( fp ) )
  236.     switch( c = (char)fgetc(fp) ) {
  237.         case '\012':                        /* newline found */
  238.         newline();
  239.         col = 0;
  240.         break;
  241.         case '\015':            /* CR found */
  242.         fputc('\015',prn);
  243.         break;
  244.         case  '\011':                       /* TAB found */
  245.         do
  246.             fputc('\040',prn);
  247.         while ( (++col % tab) != 0 );
  248.         break;
  249.         case PAGE:              /* Page break */
  250.         newpage();
  251.         col = 0;
  252.         break;
  253.         default:                /* no special case */
  254.         fputc(c,prn);           /* print character */
  255.         col++;
  256.         if ( col > maxcol ) {       /* break long lines */
  257.             newline();
  258.             col = 0;
  259.         }
  260.         break;
  261.     }
  262.     stamp();
  263.     kickpage();
  264.     fclose( fp );
  265. }
  266.  
  267.  
  268. static void printforth( filename )
  269. char *filename;
  270. {
  271.     int fd;
  272.     int screen;
  273.     int endfile;
  274.  
  275.     fd = open( filename ,0 );
  276.     if( fd == -1 ) {
  277.     printf( "Error opening file: %s.\n", filename );
  278.     return;
  279.     }
  280.     else
  281.     printf( "Now printing file: %s\n", filename );
  282.     screen = line = 0;
  283.     pagenum = 1;
  284.     LHALF;
  285.     side = LEFT;
  286.     endfile = FALSE;
  287.     while( !endfile ) {
  288.     endfile = printscr( fd, screen );
  289.     if( (screen+1 % 3) == 0 )       /* three screens per page */
  290.         newpage();
  291.     screen++;
  292.     }
  293.     stamp();
  294.     kickpage();
  295.     close( fd );
  296. }
  297.  
  298.  
  299. static int printscr( fd, screen )
  300. int fd;
  301. int screen;
  302. {
  303.     char text[65];
  304.     int endfile;
  305.     int ln;
  306.  
  307.     text[64] = '\0';
  308.     endfile = read( fd, text, 64 );
  309.     if( endfile <= 0 )
  310.     return( TRUE );             /* and quit if end of file */
  311.     fprintf( prn, "       Screen %d", screen );
  312.     newline();
  313.     println( text, 0 );
  314.     for( ln=1; ln<16; ln++ ) {          /* print remaining 15 lines */
  315.     endfile = read( fd, text, 64 );
  316.     if( endfile > 0 )
  317.         println( text, ln );
  318.     }
  319.     newline();                  /* two blank lines to */
  320.     newline();                  /* separate screens */
  321.     return( FALSE );
  322. }
  323.  
  324.  
  325. static void  println( text, ln )
  326. char *text;
  327. int ln;
  328. {
  329.     blanks( text );
  330.     fprintf( prn, "   %2d: ", ln );
  331.     if( *text != '\0' )
  332.     fprintf( prn, "%s", text );
  333.     newline();
  334. }
  335.  
  336.  
  337. static void  blanks( textptr )
  338. char *textptr;
  339. {
  340.     register char *endptr;
  341.  
  342.     endptr = textptr + 64;
  343.     while( (*endptr=='\b') && (endptr>=textptr) )
  344.     endptr--;               /* scan off trailing blanks */
  345.     *( ++endptr ) = '\0';           /* and force end of line */
  346. }
  347.  
  348.  
  349. static void newline()
  350. {
  351.     line++;
  352.     if ( line > lastline )
  353.     newpage();
  354.     else {
  355.     fputc('\015',prn);          /* send CR - LF sequence */
  356.     fputc('\012',prn);
  357.     }
  358. }
  359.  
  360.  
  361. static void newpage()
  362. {
  363.     stamp();                    /* title stamp */
  364.     line = 0;
  365.     if ( side == LEFT )
  366.     RHALF;                  /* LaserJet to right half */
  367.     else {
  368.     kickpage();
  369.     LHALF;                  /* LaserJet to left half */
  370.     }
  371.     pagenum++;
  372.     side = !side;
  373. }
  374.  
  375.  
  376. static void vline()
  377. {
  378.     int i;
  379.     MIDDLE;
  380.     for( i=0; i<=2*maxline; i++ )
  381.     {
  382.     fputc( '|', prn );
  383.     fputc( '\015', prn );           /* overlap vert bars with */
  384.     fputc( '\033', prn );           /* half line feed */
  385.     fputc( '=', prn );
  386.     }
  387. }
  388.  
  389.  
  390. static void kickpage()
  391. {
  392.     if( !nograf )
  393.     vline();                /* draw line down middle */
  394.     fputc(PAGE, prn);               /* kick out paper */
  395. }
  396.  
  397.  
  398. static void stamp()
  399. {
  400.     char datestr[15], timestr[10];
  401.     if( nostamp )
  402.     return;                 /* no date stamping */
  403.     timestamp( timestr );
  404.     datestamp( datestr );
  405.     if( !nograf )               /* no vertical line */
  406.     {
  407.     fputc( '\015', prn );
  408.     ROW( maxline );
  409.     fprintf( prn, "File: %-48s", printname );
  410.     fprintf( prn, "%s   %s   ", datestr, timestr );
  411.     fprintf( prn, "Page %2d", pagenum );
  412.     }
  413.     else if( !(pagenum & 0x01) )        /* stamp only on even pages */
  414.     {
  415.     MARGIN( 5,172 );            /* widen margins */
  416.     ROW( maxline );             /* move to bottom row */
  417.     fprintf( prn, "File: %-133s", printname );
  418.     fprintf( prn, "Page: %-3d", (pagenum+1) / 2 );
  419.     fprintf( prn, "   %s   %s", datestr, timestr);
  420.     }
  421. }
  422.  
  423.  
  424. static void datestamp( datestr )
  425. char  *datestr;
  426. {
  427.     DATETIME dat;
  428.     DosGetDateTime(&dat);
  429.     sprintf(datestr,"%02d/%02d/%02d",(int)dat.month,
  430.                      (int)dat.day,
  431.                      (int)dat.year % 100);
  432. }
  433.  
  434. static void timestamp( timestr )
  435. char    *timestr;
  436. {
  437.     DATETIME dat;
  438.     DosGetDateTime(&dat);
  439.     sprintf(timestr,"%02d:%02d",(int)dat.hours,(int)dat.minutes);
  440. }
  441. 
  442. 
  443. 
  444.