home *** CD-ROM | disk | FTP | other *** search
/ Amiga Elysian Archive / AmigaElysianArchive.iso / printer / prt104.lha / prt.c < prev    next >
C/C++ Source or Header  |  1989-08-13  |  13KB  |  289 lines

  1. /*
  2.                          PRT - A Print File Utility
  3.  
  4.      This program allows text files to be printed to the printer device with 
  5.      file headers on each page.  Page headers includes the name of the file,
  6.      the current date and time, and a page number.  Imbedded <form feed> 
  7.      characters (^L) force a page break, printing a new page header.  Many 
  8.      command line options are available (see format below).
  9.  
  10.  
  11.      format:    prt [-h -f -pnn -snn -enn -cnn -o] file [file [file]]
  12.  
  13.          where: -h   - toggles the default value of the page header flag
  14.                 -f   - toggles the default value of the form feed flag
  15.                 -pnn - changes the page size from 57 to `nn'
  16.                 -snn - changes the starting page number (default = 1)    
  17.                 -enn - stop (end) printing when page 'nn' encountered
  18.                 -cnn - changes characters/line count (default = 80)
  19.                 -o   - send output to a file instead of the printer
  20.  
  21.      author:    alfredo jahn v
  22.      date:      15-nov-87 (release 1.00)
  23.  
  24.      release:   version 1.04
  25.  
  26.      history:
  27.              15-nov-87 - initial release
  28.              16-jul-89 - added command-line flags -h -f -p
  29.              30-jul-89 - fixed 3 bugs... 
  30.                   1. problem where first 2 pages were numbered page 1
  31.                   2. problem when printing multiple files
  32.                   3. turning off initial FF (-f) turned off FFs completely
  33.              31-jul-89 - started adding the -s option [ jason purcell ]
  34.              01-aug-89 - finished adding -s option [ alfredo jahn ]
  35.              06-aug-89 - pre-release cleanup ver 1.03.
  36.              07-aug-89 - added new option and general cleanup [ v1.04 ]
  37.                   1. added -c option (characters/line).
  38.                   2. fixed problem when printing lines longer than 80 cols.
  39.                      program now wraps and updates line counter.
  40.                   3. changed so the page size is increased by 3 lines when
  41.                      the -h (no-headers) is specified to compensate for the
  42.                      header line and the extra blank lines. [ alfredo jahn ]
  43.              13-aug-89 - added two new options -o and -e. [ still v1.04 ]
  44.                   1. -o allows output to be sent to a text file instead of 
  45.                      the printer.
  46.                   2. -e allows you to specify the 'ending page'. The ending
  47.                      page is not actually printed; when the ending page is
  48.                      encountered, printing stops.  To print only the 3rd page
  49.                      of the file "README", type: prt -s3 -e4 README.
  50.                      [ alfredo jahn ]
  51.  
  52. */
  53.  
  54. #include <stdio.h>
  55. #include <time.h>
  56.  
  57. #define  PROGRAM   "\033[1mPrint File Utility\033[0m"
  58. #define  VERSION   "1.04"
  59. #define  AUTHOR    "Alfredo Jahn V"
  60.  
  61. #define  PAGESIZE  60             /* lines per page default value */
  62. #define  COLUMNS   80             /* characters per line default value */
  63. #define  TRUE      1
  64. #define  FALSE     0
  65.  
  66. char *days[] = { 
  67.  "Sun","Mon","Tue","Wed","Thu","Fri","Sat","Sun" 
  68. };
  69.  
  70. char *months[] = {
  71.  "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec" 
  72. };
  73.             
  74. FILE *fp, *printer;
  75. struct tm *localtime(), *t;       /* time-of-day structures */
  76. int print_headers=TRUE;           /* default to printing headers */
  77. int initial_FF=TRUE;              /* default to FormFeed on 1st page */
  78. int first_file=TRUE;              /* set flag to indicate 1st file */
  79. int start_page=1;                 /* starting page number to print */
  80. int end_page;                     /* used to hold "ending page" number */
  81. int lines_per_page=PAGESIZE;      /* default page size */
  82. int chars_per_line=COLUMNS;       /* default column size */
  83. int end_page_option=FALSE;        /* default to NO ending page specified */
  84.  
  85.  
  86. main(argc,argv)                   /* program start */
  87.   int argc;
  88.   char *argv[];
  89. {
  90.   int i=1, x;
  91.   long time(), time_loc;
  92.   char output_name[80];           /* holds filename out output file */
  93.   int file_output=FALSE;          /* indicate output goes to printer */
  94.  
  95.   if ( argc == 1 ) {              /* check for no arguments passed */
  96.     print_usage();                /* display "usage" information */
  97.   } 
  98.  
  99.   else {
  100.  
  101.     strcpy( output_name,"prt:" );      /* default output goes to printer */
  102.  
  103.     /* check for "user defined" command-line options
  104.     */
  105.     for ( i=1; i<argc; i++ ) {
  106.       if ( argv[i][0] == '-' ) {       /* check if arg is a "flag" */
  107.         switch ( toupper (argv[i][1]) ) {
  108.           case 'H':                    /* page header control flag */
  109.              print_headers = !print_headers;
  110.              if (print_headers==FALSE)
  111.                lines_per_page += 3;    /* compensate for no headers */
  112.              break;
  113.           case 'F':                    /* form feed on 1st page control flag */
  114.              initial_FF = !initial_FF;
  115.              break;
  116.           case 'P':                    /* change page size specified */
  117.              lines_per_page = atoi( &argv[i][2] );
  118.              break;
  119.           case 'S':                    /* change starting page number */
  120.              start_page = atoi( &argv[i][2] );
  121.              break;         
  122.           case 'E':                    /* specify an ending page number */
  123.              end_page = atoi( &argv[i][2] );
  124.              end_page_option = TRUE;
  125.              break;         
  126.           case 'C':                    /* change column size value */
  127.              chars_per_line = atoi( &argv[i][2] );
  128.              break;         
  129.           case 'O':                    /* send output to an ascii file */
  130.              strcpy( output_name,&argv[i][2] );
  131.              file_output = TRUE;
  132.              break;         
  133.           case '?':
  134.              print_usage();            /* also calls exit() */
  135.           default:
  136.              printf("ERROR: Invalid flag `%s'\n",argv[i]);
  137.              print_usage();
  138.         }
  139.       }
  140.     }
  141.  
  142.     if ( (printer = fopen(output_name,"w")) == NULL ) {
  143.       if (file_output)
  144.         printf("ERROR: Can't open output file '%s'\n",output_name);
  145.       else
  146.         printf("ERROR: Can't open printer device\n");
  147.     } 
  148.     else {
  149.  
  150.       time(&time_loc);              /* get the system timestamp */
  151.       t = localtime(&time_loc);     /* fill in with local time */
  152.  
  153.       for ( i=1; i<argc; i++ ) {
  154.         if ( argv[i][0] != '-' ) {  /* skip over "flag" arguments */
  155.           print_file(argv[i]);      /* print the file */
  156.           first_file=FALSE;         /* indicate no longer the 1st file */
  157.         }
  158.       }
  159.     }                               /* else (if printer...) */
  160.   }                                 /* else (argc > 1) */ 
  161. }                                   /* main() */
  162.  
  163.  
  164. print_usage()                       /* print help/usage info */
  165. {
  166.   printf("\n%s (v%s) written by %s\n\n",PROGRAM,VERSION,AUTHOR);
  167.   printf("Usage: prt [-h -f -pN -sN -eN -cN -o] file1 [file2 [...]]\n");
  168.   printf("  -h turns %s the 'page header' option\n",
  169.         print_headers ? "OFF" : "ON");
  170.   printf("  -f turns %s the 'form feed on 1st page' option\n",
  171.         print_headers ? "OFF" : "ON");
  172.   printf("  -p changes the 'page size' from %d to 'N' lines per page\n",
  173.         PAGESIZE);
  174.   printf("  -s starts printing a file on the 'N'th page\n");
  175.   printf("  -e stops printing when this page number is encountered\n");
  176.   printf("     (-s and -e affects all files specified on the command line)\n");
  177.   printf("  -c changes 'characters/line' from %d to 'N'\n",COLUMNS);
  178.   printf("  -o sends output to an ascii file instead of the printer\n\n");
  179.   exit();
  180. }
  181.  
  182.  
  183. print_file(filename)              /* print a single file */
  184.  char *filename;
  185. {
  186.   int c, line=1, char_cnt=1, page=start_page, tmp_page=1;
  187.  
  188.   if ( (fp = fopen(filename,"r")) == NULL ) {
  189.     printf("ERROR: Can't open file `%s'\n",filename);
  190.  
  191.   } else {
  192.  
  193.     /*
  194.      *  Read the file until we find the page we want...
  195.      */
  196.     while ( (c = getc(fp)) != EOF && tmp_page < page ) {
  197.       if (line == lines_per_page) {                 /* End-Of-Page?          */
  198.         line=1;                                     /* reset line counter    */
  199.         char_cnt=1;                                 /* reset char counter    */
  200.         tmp_page++;                                 /* bump the page counter */
  201.         ungetc(c,fp);                               /* push the char back    */
  202.       } else
  203.         if (c=='\n') {                              /* physical end-of-line? */
  204.           line++;                                   /* bump line count       */
  205.           char_cnt=1;                               /* reset char counter    */
  206.         } else
  207.           if (char_cnt == chars_per_line) {         /* line limit?           */
  208.             line++;                                 /* bump line count       */
  209.             char_cnt=1;                             /* reset char counter    */
  210.           } else 
  211.             if (c=='\f') {                          /* is char a FormFeed?   */
  212.               line=1;                               /* reset line counter    */
  213.               char_cnt=1;                           /* reset char counter    */
  214.               tmp_page++;                           /* bump the page counter */
  215.             }
  216.     }                                               /* while ! EOF           */
  217.  
  218.     if ( c != EOF ) {      
  219.  
  220.       header( printer,filename,t,page++ );          /* print 1st header      */
  221.       char_cnt = line = 1;                          /* reset counters        */
  222.       ungetc(c,fp);                                 /* push the char back    */
  223.  
  224.       while ( (c = getc(fp)) != EOF ) {             /* read a character      */
  225.         if (line == lines_per_page) {               /* End-Of-Page?          */
  226.           if ( end_page_option && page == end_page )
  227.             return();
  228.           header( printer,filename,t,page++ );      /* print page header     */
  229.           line=1;                                   /* reset line counter    */
  230.           char_cnt=1;                               /* reset char counter    */
  231.           putc(c,printer);                          /* 1st char on new page  */
  232.         } else
  233.           if (c=='\n') {                            /* physical end-of-line? */
  234.             line++;                                 /* bump line count       */
  235.             char_cnt=1;                             /* reset char counter    */
  236.             putc(c,printer);                        /* print the LF char     */
  237.           } else 
  238.             if (char_cnt == chars_per_line) {       /* line limit?           */
  239.               line++;                               /* bump line count       */
  240.               char_cnt=1;                           /* reset char counter    */
  241.               putc(c,printer);                      /* print last character  */
  242.               putc('\n',printer);                   /* then print a LF       */
  243.           } else
  244.               if (c=='\f') {                        /* is char a FormFeed?   */
  245.                 if ( end_page_option && page == end_page )
  246.                   return();
  247.                 header( printer,filename,t,page++ );  /* print page header   */
  248.                 line=1;                             /* reset line counter    */
  249.                 char_cnt=1;                         /* reset char counter    */
  250.               } else {
  251.                   putc(c,printer);                  /* print the character   */
  252.                   char_cnt++;                       /* bump the char counter */
  253.                 }
  254.  
  255.       }                                             /* while                 */
  256.     } else                                          /* if c!=EOF             */
  257.         printf(
  258.          "ERROR: There are only %d pages in file '%s' \n",tmp_page,filename);
  259.  
  260.   }                                                 /* else                  */
  261. }                                                   /* end print_file()      */
  262.  
  263.  
  264. int header(fd,name,t,page)      /* print the page header */
  265.   FILE *fd;
  266.   char *name;
  267.   struct tm *t;
  268.   int page;
  269. {
  270.   if ( initial_FF == FALSE && first_file )
  271.      initial_FF=TRUE;           /* don't FF, but reset flag for next time */
  272.   else
  273.      putc('\f',fd);             /* write a FF to the printer */
  274.  
  275.   if ( print_headers ) {        /* print the header if flag it true */
  276.     fprintf(fd,
  277.         "File: %-35s  %s %02d-%s-%02d %02d:%02d:%02d       page %d\n\n\n",
  278.         name,                   /* filename */
  279.         days[t->tm_wday],       /* day of the week */
  280.         t->tm_mday,             /* day */
  281.         months[t->tm_mon],      /* month */
  282.         t->tm_year,             /* year */
  283.         t->tm_hour,             /* hour */
  284.         t->tm_min,              /* minutes */
  285.         t->tm_sec,              /* seconds */
  286.         page);                  /* page number */
  287.   }
  288. }
  289.