home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Spezial / SPEZIAL2_97.zip / SPEZIAL2_97.iso / ANWEND / TOOLS / MPAGE / MPAGE.ZIP / mp_main.c < prev    next >
Text File  |  1997-09-25  |  5KB  |  185 lines

  1. # include <stdio.h>
  2. # include <sys/types.h>
  3.  
  4. # ifndef lint
  5. static char *rcs_id =
  6.     "@(#) $Header: mp_main.c,v 2.5 89/05/25 10:42:35 mark Exp $";
  7. # endif
  8.  
  9. # include "mp_head.h"
  10.  
  11. /*
  12.  * mpage:    a program to reduce pages of print so that several pages
  13.  *           of output appear on one printed page.
  14.  *
  15.  * Written by:
  16.  *   ...!uunet!\                       Mark Hahn, Sr Systems Engineer
  17.  *              >pyrdc!mark            Pyramid Technology Corporation
  18.  * ...!pyramid!/                       Vienna, Va    (703)848-2050
  19.  *
  20.  *
  21.  * Copyright (c) 1988 Mark P. Hahn, Herndon, Virginia
  22.  *  
  23.  *     Permission is granted to anyone to make or distribute verbatim
  24.  *     copies of this document as received, in any medium, provided
  25.  *     that this copyright notice notice is preserved, and that the
  26.  *     distributor grants the recipient permission for further
  27.  *     redistribution as permitted by this notice.
  28.  *
  29.  */
  30.  
  31. /* $Log:    mp_main.c,v $
  32.  * Revision 2.5  89/05/25  10:42:35  mark
  33.  * changes to print a page count on stderr after the print job is queued.
  34.  * 
  35.  * Revision 2.4  89/05/25  08:58:25  mark
  36.  * rearranged the rcs header keywords for better readability.
  37.  * 
  38.  * Revision 2.3  89/05/22  14:40:28  mark
  39.  * Fixed the type-o in the rcs identification string
  40.  * 
  41.  * Revision 2.2  89/05/22  14:37:30  mark
  42.  * Added rcs identification usable with the "what" program
  43.  * 
  44.  * Revision 2.1  89/05/22  14:31:15  mark
  45.  * New Major Revision
  46.  * 
  47.  * Revision 1.1  89/05/22  14:19:56  mark
  48.  * Initial revision
  49.  *  */
  50.  
  51. main(argc,argv)
  52.  int argc;
  53.  char **argv;
  54. {
  55.     FILE *outfd;
  56.     int currarg;
  57.     struct sheet *thelist;
  58.     struct sheet *thesheet;
  59.  
  60.     /*
  61.      * examine the environment for PRINTER and MPAGE environment variables
  62.      */
  63.     if (do_env() == 0) {
  64.         fprintf(stderr, usage, MPAGE);
  65.         exit(1);
  66.     }
  67.         
  68.     currarg = do_args(argc, argv, 0);
  69.     if (currarg < 0) {
  70.         fprintf(stderr, usage, MPAGE);
  71.         exit(1);
  72.     }
  73.  
  74.     /*
  75.      * if a printer was specified then create a lpr command using
  76.      * the specified printer, else output goes to standard out
  77.      */
  78.     if (*printer) {
  79.         (void)sprintf(outcommand, "ucb /usr/ucb/lpr -P%s", printer);
  80.         if ((outfd = popen(outcommand, "w")) == NULL) {
  81.             fprintf(stderr, "%s: cannot create pipe for '%s'\n",
  82.                 outcommand);
  83.             perror(MPAGE);
  84.         }
  85.     } else {
  86.         outfd = stdout;
  87.     }
  88.  
  89.     /*
  90.      * pick the array of sheet lists based upon the specified option
  91.      */
  92.     if (sheetorder == UPDOWN) {
  93.         sheetlist = up_down;
  94.     } else {
  95.         sheetlist = left_right;
  96.     }
  97.  
  98.     /*
  99.      * from the array of sheet lists pick the proper sheet list for
  100.      * the given sheetaspect, then select the proper sheet format
  101.      * for the given number of redueced pages per output page
  102.      */
  103.     thelist = sheetlist[sheetaspect];
  104.     thesheet = &(thelist[sheetindex]);
  105.  
  106.     /*
  107.      * if either lines or columns were specified as options, over
  108.      * the default sheets idea of the number of line or cloumns
  109.      * per reduced page
  110.      */
  111.     if (opt_lines > 0) {
  112.         thesheet->sh_plength = opt_lines;
  113.     }
  114.     if (opt_width > 0) {
  115.         thesheet->sh_cwidth = opt_width;
  116.     }
  117.  
  118.     /*
  119.      * if there are arguments left over after processing options, then
  120.      * these are names of files to process, else process the standard
  121.      * input
  122.      */
  123.     if (currarg < argc) {
  124.         ps_title(argv[currarg], outfd);
  125.         for ( ;currarg < argc; currarg++) {
  126.             do_file(argv[currarg], thesheet, outfd);
  127.         }
  128.     } else {
  129.         ps_title("<stdin>", outfd);
  130.         do_stdin(thesheet, outfd);
  131.     }
  132.     /*
  133.      * having processed all input, print out a PS trailer
  134.      * (timeing stuff stolen from old adobe troff stuff)
  135.      */
  136.     fprintf(outfd, "%%%%Trailer\n");
  137.     fprintf(outfd, "statusdict begin jobname print flush");
  138.     fprintf(outfd, " (: Job finished:\\n) print\n");
  139.     fprintf(outfd, "(\\tmpage time (s) = ) print flush usertime ");
  140.     fprintf(outfd, "mp_stm sub 1000 div ==\n(\\tmpage pages = ) print ");
  141.     fprintf(outfd, "flush pagecount mp_pgc sub ==\nend flush\n");
  142.     fprintf(outfd, "%%%%Pages: %d\n", ps_pagenum);
  143.     if (opt_verbose) {
  144.         fprintf(stderr, "[%s: %d pages, ", MPAGE, ps_pagenum);
  145.         if (*printer == 0) {
  146.             fprintf(stderr, "on <stdout>]\n");
  147.         } else {
  148.             fprintf(stderr, "printer %s]\n", printer);
  149.         }
  150.     }
  151.     /*
  152.      * proper clean up to make sure the pipe is flushed
  153.      */
  154.     if (*printer) {
  155.         (void)pclose(outfd);
  156.     }
  157.     return 0;
  158. }
  159.  
  160. /*
  161.  * ps_title prints a post script header suitable for PS processors
  162.  */
  163. ps_title(name, outfd)
  164.  char *name;
  165.  FILE *outfd;
  166. {
  167.     time_t curr_time;
  168.     char *time_str;
  169.  
  170.     fprintf(outfd, "%%!PS-Adobe-1.0\n");
  171.     fprintf(outfd, "%%%%DocumentFonts: Courier\n");
  172.     fprintf(outfd, "%%%%Title: %s (mpage)\n", name, MPAGE);
  173.     fprintf(outfd, "%%%%Creator: %s\n", MPAGE);
  174.     (void)time(&curr_time);
  175.     time_str = ctime(&curr_time);
  176.     fprintf(outfd, "%%%%CreationDate: %s",time_str);
  177.     fprintf(outfd, "%%%%Pages: (atend)\n");
  178.     fprintf(outfd, "%%%%BoundingBox: 20 20 596 776\n");
  179.     fprintf(outfd, "%%%%EndComments\n\n");
  180.     fprintf(outfd, "/mp_stm usertime def\n");
  181.     fprintf(outfd, "/mp_pgc statusdict begin pagecount end def\n");
  182.     fprintf(outfd, "statusdict begin /jobname (%s) def end\n", name);
  183. }
  184.  
  185.