home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / PROGRAMS / UTILS / LASER / LJ22.ZIP / LJ2.C next >
Encoding:
C/C++ Source or Header  |  1991-01-11  |  14.5 KB  |  540 lines

  1.  /******************************************************************************
  2.  **
  3.  **   LJ -- A printing utility for the HP LaserJet
  4.  **
  5.  **
  6.  **   This program prints a series of files on the LaserJet printer.  The
  7.  **   files are printed in a "landscape" font at 17 characters to the inch.
  8.  **   To take advantage of this density, two "pages" of information from
  9.  **   the file are printed on each piece of paper (left and right halves).
  10.  **
  11.  **   Usage is:       LJ  file1 file2 file3 ...
  12.  **
  13.  **   Where file# is a valid MS-DOS filename, included on the command line.
  14.  **
  15.  **   Originally written by Joe Barnhart and subsequently modifed by:
  16.  **     Ray Duncan and Chip Rabinowitz.  This program has been placed in
  17.  **     the public domain for use without profit.
  18.  **
  19.  **   Revised 9/86 for the Mark Williams C Programming System,
  20.  **     Steven Stern, JMB Realty Corp.
  21.  **
  22.  **   Revised 11/86 for DOS wild card characters in the file name.
  23.  ****************************************************************************
  24.  **
  25.  **   Revised 4/88 for the DATALIGHT C Compiler by Serge Stepanoff,
  26.  **   STS Enterprises, 5469 Arlene Way, Livermore, CA.
  27.  **
  28.  **    The revised code uses the built in file wildcard expansion feature
  29.  **    of DATALIGHT C, and makes use of some additional flags to
  30.  **    turn off the Header line, optionally use the 7 point
  31.  **    Prestige Elite font, and ssome oher features.  Typing "lj2" with
  32.  **    no parameters will produce a short users' explanaion.
  33.  **
  34.  **    Program has been checked out on an OKIDATA
  35.  **    LASERLINE printer (HP Laserjet compatible).
  36.  *****************************************************************************
  37.  ** Revised 10/16/89 by Jim Derr for the Turbo C 2.0 Compiler.
  38.  ** Uses the wildargs obj module for file wild card expansion.
  39.  ** Corrected some anomalies in the code that was causing invalid pages breaks.
  40.  ** Added the -s command line option that will shade every other line on the
  41.  ** printout for readability.
  42.  *****************************************************************************
  43.  ** Revised 1/11/91 by Jim Derr.
  44.  ** added -l option to specify number of lines per page to print.
  45.  ** added -i options to ignore formfeed characters.
  46.  ** added -d option to allow for duplex printing.
  47.  **          (Note: when printing duplex only one file at a time may be
  48.  **           specified on the command line)
  49.  *****************************************************************************
  50.  **/
  51.  
  52. #include "stdio.h"
  53. #include "time.h"
  54. #include "ctype.h"
  55. #include <fcntl.h>
  56.  
  57. int     MAXLINE=66;        /* maximum lines per page on LaserJet */
  58. int     MAXLINE_RESET=66;  /* maximum lines per page on LaserJet */
  59. #define MAXVERT 69         /* maximum lines per page on LaserJet */
  60.  
  61.  
  62. #define lp   stdprn
  63.  
  64. /* FILE *lp; */
  65.  
  66. int line_no = 1;
  67. int pagenum;
  68.  
  69. FILE *fp;               /* FILE pointer for the file to be printed */
  70.  
  71. int  hdrflag;        /* Header print suppress flag */
  72. int  pgflag;        /* Page separator flag */
  73. int  tab = 4;        /* default value of one tab stop */
  74. int  pgside;        /* left/right half of page indicator  */
  75. int  psprint = 0;   /* proportional using Helv 6 pt. */
  76. int  shade_it = 0;  /* shade every other line */
  77. int  book = 0;      /* indent left 6 additional spaces for punching*/
  78. int  maxcol=85;
  79. int  numbers = 0;
  80. int  duplex=0;
  81. int  formfeed=0;
  82.  
  83. int do_print=1;     /*flags for doing duplex printing*/
  84.  
  85. /*************************************************************************
  86.  
  87. main
  88.  
  89. *************************************************************************/
  90.  
  91. main(argc, argv)
  92. int argc;
  93. char *argv[];
  94. {
  95. int filenum;
  96. char fname[70];
  97. int first;
  98. int jim;
  99. FILE *mine;
  100.  
  101.    if (argc <= 1)
  102.       {
  103.       usage();
  104.       exit(1);
  105.       }
  106.  
  107.  
  108.    _fmode = O_TEXT;
  109.     mine = freopen("PRN","w",stdprn);
  110.  
  111. /*  lp = fopen("lj2.out","wt");  */
  112.  
  113. /* initialize the LaserJet for landscape printing */
  114.  
  115.    fprintf( lp,"\033E\033&l1O\033(s17H\033&l5.14C\033&l70F\033&l5E" );
  116.  
  117.    for (first = 1; first < argc; first++)  {
  118.       if (*argv[first] != '/' && *argv[first] != '-') /* check for flags */
  119.          break;                    /* and exit if none */
  120.       argv[first]++;
  121.       switch (tolower(*argv[first]))  {
  122.          case 'e':        /* Prestige Elite 7 point font */
  123.             psprint = 1;
  124.             maxcol=104;
  125.             fprintf(lp,"\033&l1O\033(0U\033(s1p6vsb4T");
  126.             break;
  127.          case 'h':        /* suppress headers flag */
  128.             hdrflag++;
  129.             MAXLINE=69;
  130.             MAXLINE_RESET=69;
  131.             break;
  132.          case 't':        /* Horizontal tab value */
  133.             tab = atoi(++argv[first]);
  134.             if (tab < 1 || tab > 8)  {
  135.                 printf("Invalid tab value specified -- defaulting to 8\n");
  136.                 tab = 8;
  137.                 }
  138.             break;
  139.          case 'l':
  140.             MAXLINE = atoi(++argv[first]);
  141.             if (MAXLINE < 1 || MAXLINE > MAXLINE_RESET)  {
  142.                 printf("Invalid lines/page value specified -- defaulting to MAX\n");
  143.                 MAXLINE = MAXLINE_RESET;
  144.                 }
  145.             break;
  146.          case 'p':      /* suppress page eject between files */
  147.             pgflag++;
  148.             break;
  149.          case 's':
  150.             shade_it++;
  151.             break;
  152.          case 'n':
  153.             numbers++;
  154.             break;
  155.          case 'b':
  156.             book++;
  157.             maxcol=79;
  158.             break;
  159.          case 'd':
  160.             duplex=1;
  161.             break;
  162.          case 'i':
  163.             formfeed++;
  164.             break;
  165.          default:
  166.             printf("Invalid Flag Specified ('%s') -- ignored.....\n",
  167.                     --argv[first]);
  168.             break;
  169.          }
  170.       }
  171.  
  172.    printf("Lines per page set to %d\n",MAXLINE);
  173.  
  174.    if(duplex) {
  175.      if((argc - 1) > first) {
  176.         printf("Only one file at a time may be printed when using DUPLEX mode.\n");
  177.         exit(1);
  178.      }
  179.    }
  180.  
  181.    for(filenum = first; filenum < argc; filenum++ )
  182.       {
  183.          copyname(fname, argv[filenum]);
  184.          fp = fopen(fname ,"r");
  185.          if( fp == NULL )
  186.             {
  187.             printf( "File %s doesn't exist.\n", fname );
  188.             }
  189.          else
  190.             {
  191.             printf( "Now printing %s\n", fname );
  192.             printfile( fname );
  193.             fclose( fp );
  194.             }
  195.       }    /* of loop through run-time args */
  196.  
  197. /*
  198.    if (pgflag & pgside)
  199.     fputc('\f', lp);
  200. */
  201.    fprintf( lp, "\r\033E" );      /* clear LaserJet */
  202.    fclose(lp);
  203.  
  204.  
  205. }
  206.  
  207. /*************************************************************************
  208.  
  209. printfile (filename)
  210.  
  211. *************************************************************************/
  212.  
  213. printfile(filename)
  214. char *filename;
  215. {
  216.    int retval = 0;
  217.    int printed = 0;
  218.    int pass=0;
  219.    char c2;
  220.    int f_feed=0;
  221.    int phys_pages=0;
  222.    int plus=1;
  223.  
  224.    pagenum=1;
  225.  
  226. do_it_again:
  227.    while( (feof(fp)==0) && (retval==0) )
  228.       {
  229.       f_feed=0;
  230.       if (pgside == 0)  {
  231.          printed = 0;
  232.          c2 = fgetc(fp);
  233.          if(feof(fp)) break;
  234.          switch (c2) {
  235.             case EOF:
  236.             case '\377':
  237.             case '\032':
  238.                 retval = -1;
  239.                 break;
  240.             default:
  241.                 printed=1;
  242.                 if(do_print) dovert();
  243.                 if(psprint && do_print)
  244.                    fprintf(lp,"\033&a0r0c120m0L\r");
  245.                 else if(book && do_print)
  246.                    fprintf(lp, "\033&a0r0c85m7L\r"); /* set LaserJet to left half */
  247.                 else if(do_print)
  248.                    fprintf(lp, "\033&a0r0c85m0L\r"); /* set LaserJet to left half */
  249.                 if(shade_it && do_print) shade();
  250.                 ungetc(c2,fp);
  251.                 header(filename, pagenum++);       /* title top of page */
  252.                 retval = printpage();                 /* print one page */
  253.                 pgside ^= 1;
  254.          }
  255.       }
  256.       if(feof(fp)==0 && retval==0)
  257.          {                                  /* if more to print... */
  258.          if(psprint && do_print)
  259.             fprintf(lp,"\033&a0r0c243m123L\r");
  260.          else if(book && do_print)
  261.             fprintf(lp, "\033&a0r0c175m97L\r");    /* LaserJet to right half */
  262.          else if(do_print)
  263.             fprintf(lp, "\033&a0r0c175m90L\r");    /* LaserJet to right half */
  264.          c2 = fgetc(fp);
  265.          if(feof(fp)) break;
  266.          switch (c2) {
  267.             case EOF:
  268.             case '\377':
  269.             case '\032':
  270.                 retval = -1;
  271.                 break;
  272.             default:
  273.                 ungetc(c2,fp);
  274.                 header(filename, pagenum++);          /* title top of page */
  275.                 retval = printpage();                 /* print one page */
  276.                 pgside ^= 1;
  277.          }
  278.       }
  279.       if (pgside == 0 && printed && do_print) {
  280.          fputc('\f', lp);
  281.          f_feed=1;
  282.       }
  283.       else if (pgflag == 0 && printed && do_print)  {
  284.          fputc('\f', lp);
  285.          pgside = 0;
  286.          f_feed=1;
  287.          }
  288.       if(f_feed) {
  289.         if(plus)
  290.             ++phys_pages;
  291.         else
  292.             --phys_pages;
  293.       }
  294.       if(duplex) do_print ^= 1;
  295.       }
  296.  
  297.  
  298.       if(duplex) {
  299.         if(pass) {
  300.             while(phys_pages) {
  301.                 fputc('\f',lp);
  302.                 --phys_pages;
  303.             }
  304.             return(0);
  305.         }
  306.         plus=0;
  307.         if(!f_feed && retval == 0) fputc('\f',lp);
  308.         fflush(lp);
  309.         ++pass;
  310.         rewind(fp);
  311.         retval=0;
  312.         pagenum=1;
  313.         do_print = 0;
  314.         printf("\nFlip the paper and press any key when ready\n");
  315.         bioskey(0);
  316.         goto do_it_again;
  317.       }
  318.    return(0);
  319. }
  320.  
  321. /*******************************************************************************
  322.  
  323. printpage
  324.    print a logical page
  325.  
  326. *************************************************************************/
  327.  
  328. printpage()
  329. {
  330.    char c;
  331.    int line,col;
  332.    static int cont = 0;
  333.    static char *cont1 = "---->";
  334.  
  335.    line = col = 0;
  336.    if(cont)
  337.       {
  338.       if(do_print) fprintf(lp,cont1);
  339.       col = strlen(cont1);
  340.       cont = 0;
  341.       }
  342.  
  343.    while( line < MAXLINE )
  344.       {
  345.       c = fgetc(fp);
  346.       if(feof(fp)) return(-1);
  347.  
  348.       if(col>maxcol)
  349.          {
  350.          line++;
  351.          switch(c)
  352.             {
  353.             case '\n':
  354.             case '\r':
  355.             case '\f':
  356.             case EOF:
  357.             case '\377':
  358.             case '\032':
  359.                break;
  360.             default:
  361.                if(line >= MAXLINE)
  362.                   {
  363.                   cont = 1;
  364.                   ungetc(c,fp);
  365.                   return(0);
  366.                   }
  367.                if(do_print) fprintf(lp,"\n%s",cont1);
  368.                col = strlen(cont1);
  369.                break;
  370.             }
  371.          }
  372.  
  373.       if(col == 0) {
  374.           if(do_print) {
  375.               printf("Printing page %4.4d line %4.4d\r",pagenum-1,line);
  376.           }
  377.           else {
  378.               printf("Skipping page %4.4d line %4.4d\r",pagenum-1,line);
  379.           }
  380.  
  381.           if(numbers) {
  382.               if(do_print) fprintf(lp,"%4.4d:",line_no);
  383.               col=5;
  384.           }
  385.           ++line_no;
  386.       }
  387.  
  388.       switch(c)
  389.          {
  390.          case '\n':           /* newline found */
  391.             col = 0;          /* zero column and */
  392.             line++;           /* advance line count */
  393.             if( line < MAXLINE )
  394.                if(do_print) fprintf(lp,"\n");
  395.             break;
  396.          case '\r':           /* CR found */
  397.             break;            /* discard it */
  398.          case '\t':           /* TAB found */
  399.             do
  400.                if(do_print) fputc(' ',lp);
  401.             while ( (++col % tab) != 0 );
  402.             break;
  403.          case '\f':                      /* Page break or */
  404.             if(formfeed) break;
  405.             if(line != 0)
  406.                 line = MAXLINE;      /* force termination of loop */
  407.             break;
  408.          case EOF:            /* EOF mark */
  409.          case '\377':         /* EOF mark */
  410.          case '\032':         /* EOF mark */
  411.             return(-1);
  412.          default:              /* no special case */
  413.             if(do_print) fputc(c,lp);       /* print character */
  414.             col++;
  415.             break;
  416.       }
  417.    }
  418.    return(0);
  419. }
  420.  
  421. /*************************************************************************
  422.  
  423. header
  424.    print a page header
  425.  
  426. *************************************************************************/
  427.  
  428. header( filename, pagenum )
  429. char *filename;
  430. int pagenum;
  431. {
  432.    char datestr[11], timestr[11];
  433.    if (hdrflag)  {
  434.       return;        /* skip if flag set */
  435.       }
  436.    timestamp(timestr);
  437.    datestamp(datestr);
  438.    if(do_print) fprintf(lp,"\033&d0D");
  439.    if(book && do_print)
  440.        fprintf(lp, "File: %-40s%s   %s  --  Page: %03d \n\n",
  441.           filename,datestr,timestr,pagenum);
  442.     else if(do_print)
  443.        fprintf(lp, "File: %-40s%s   %s  --  Page: %03d       \n\n",
  444.           filename,datestr,timestr,pagenum);
  445.    if(do_print) fprintf(lp,"\033&d@");
  446. }
  447.  
  448. timestamp( timestr )
  449. char   *timestr;
  450. {
  451.    struct tm *tod;
  452.    time_t tt;
  453.    tt = time(NULL);
  454.    tod = localtime(&tt);
  455.    sprintf(timestr,"%02d:%02d", tod->tm_hour, tod->tm_min);
  456.    return;
  457. }
  458.  
  459. datestamp( datestr )
  460. char  *datestr;
  461. {
  462.    struct tm *tod;
  463.    time_t tt;
  464.    tt = time(NULL);
  465.    tod = localtime(&tt);
  466.    sprintf(datestr,"%02d/%02d/%02d", tod->tm_mon + 1, tod->tm_mday,
  467.     tod->tm_year + 1900);
  468.    return;
  469. }
  470.  
  471. /*************************************************************************
  472.  
  473. dovert()
  474.    draw a vertical line down the center of the physical page
  475.  
  476. *************************************************************************/
  477.  
  478. dovert()
  479. {
  480.    int line = 1;
  481.  
  482.    if(psprint)
  483.         fprintf(lp,"\033&a0r0c123m121L\r|");
  484.    else
  485.         fprintf(lp,"\033&a0r0c90m88L\r|");
  486.  
  487.    while(line++ < MAXVERT) fprintf(lp,"\n|");
  488. }
  489.  
  490. /*************************************************************************
  491.  
  492. copyname
  493.     copy a file name converting to upper case.
  494.  
  495. *************************************************************************/
  496.  
  497. copyname(to, from)
  498. char *to, *from;
  499. {
  500.     while (*from)
  501.         *to++ = toupper(*from++);
  502.     *to = 0;
  503. }
  504.  
  505. /************************************************************************/
  506.  
  507. usage()
  508. {
  509.     printf("\nUSAGE:\n\n");
  510.     printf("C>LJ2 [flags] filename1 [filename2 ..........]\n");
  511.     printf("\tItems shown in brackets are optional.");
  512.     printf("\n\n\tFilename(s) may include wildcards");
  513.     printf("\n\n\tFlags are:\n");
  514.     printf("\t\t-e (or /e) select Helv proportional 6 PT font\n");
  515.     printf("\t\t-h (or /h) suppress page headers\n");
  516.     printf("\t\t-tx (or /tx) set tab stop value (where x is 1 to 8)\n");
  517.     printf("\t\t-p (or /p) do not leave blank half pages between files\n");
  518.     printf("\t\t-s (or /s) shade every other line\n");
  519.     printf("\t\t-n (or /n) print line numbers\n");
  520.     printf("\t\t-b (or /b) indent for punching\n");
  521.     printf("\t\t-d (or /d) for duplex printing\n");
  522.     printf("\t\t-lxx (or /lxx) set lines/page to x(where x is 1 to 66)\n");
  523.     printf("\t\t-i (or /l) ignore formfeed characters\n");
  524. }
  525.  
  526.  
  527. shade()
  528. {
  529.  
  530.  
  531.     int i;
  532.     fprintf (lp, "\033&f0S\033*p0x0Y\033*c3300a32b10G");
  533.     for (i=1; i <= 35; i++)
  534.         fprintf (lp, "\033*c2P\033&a+2R");
  535.     fprintf (lp, "\033&f1S");
  536.  
  537. }
  538.  
  539.  
  540.