home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / text / frame / 3412 / split-ps.c
Encoding:
C/C++ Source or Header  |  1992-11-18  |  5.7 KB  |  209 lines

  1. /* 
  2. * split-ps.c 
  3. *
  4. * Author: David Mallis (mallis@cfi.org), CAD Framework Initiative, 11/17/92
  5. *
  6. * Inspired by breakps, written by William Chia-Wei Cheng
  7. *
  8. * Designed for use on FrameMaker PostScript output in a UNIX environment.
  9. * Splits existing FM PostScript files into smaller free-standing ps files.
  10. * To compile: "cc -o split-ps split-ps.c"; should be portable.
  11. *
  12. * FrameMaker and PostScript are trademarks of their respective owners.
  13. * Written for and tested with FrameMaker 3.1X on a Sun, 4.1.1.
  14. *
  15. * This is free software, and may be used by any person for any reason.
  16. * NO WARRANTY is expressed or implied. Use at your own risk. We ask that
  17. * you acknowledge the origin if you incorporate this code elsewhere.
  18. *
  19. * NOTE: code is untested when using non-standard fonts, specifically
  20. * not tested in downloadable font situations. 
  21. *
  22. * Your mileage may vary. Runs with 100% recycled electrons.
  23.  
  24. Notes from the author:
  25.  
  26. This is a quick-and-dirty program to chop up PostScript output files
  27. into subfiles with fewer pages, copying the prolog into each.  A
  28. resulting `subfile' is also choppable by the same program.
  29.  
  30. This was specifically written for FrameMaker files, although the
  31. algorithm will probably work with minimal modification for other,
  32. modern flavors of PS driver output.
  33.  
  34. This has many dependencies and hard-codings, SOME of which are:
  35.  
  36.   * Assumes PS file has NEWLINE characters FREQUENTLY delimiting the code
  37.     (at least every 255 characters).  If not, enlarge the  line[256]  
  38.     buffer.
  39.  
  40.   * If all \n have been squeezed out then the algorithm will have to 
  41.     be modified.
  42.  
  43.   * Assumes the file is structured with these tokens:
  44.  
  45.           ----Prolog Code used once for whole file----
  46.         EOH token
  47.         BOP token
  48.           ---------ONE PAGE OF PS CODE-----
  49.         EOP token
  50.         BOP token
  51.           ---------ONE PAGE OF PS CODE-----
  52.         EOP token
  53.            .
  54.            .
  55.            .
  56.         TRAILER token
  57.  
  58.   * Note the defines for these expected token delimiters.  Change them 
  59.     as needed.
  60.  
  61.   * If the structure of your PS file is more complicated, you will have 
  62.     to get your hands dirty in the algorithm.
  63.  
  64.   * Assumes that the PS driver was smart enough to put all font,
  65.     graphics, and other PS procedure definitions in the prolog.  This
  66.     is not a trivial expectation.  There are drivers that create new
  67.     font definitions on the fly in page 32, for example, and then use
  68.     them later in page 45.  If this is the case with your PS driver
  69.     output, then if you chop out page 32 you will get a PS error on
  70.     page 45.
  71.  
  72.   * Assumes a scratch file named HEAD_FILE (see #define) can be
  73.     overwritten then deleted.
  74.  
  75.   * Probably many other implicit assumptions too gross to mention.
  76.  
  77. Also, this program does very little error checking on input parameters,
  78. file creation, etc.
  79.  
  80.                           USE WITH DUE CAUTION.
  81. end comments */
  82.  
  83. #include <errno.h>
  84. #include <stdio.h>
  85.  
  86. #define EOH "%%EndSetup"
  87. #define EOP "%%EndPage"
  88. #define BOP "%%Page"
  89. #define TRAILER "%%Trailer"
  90. #define HEAD_FILE "QQhead"
  91.  
  92. main(argc,argv)
  93. int argc;
  94. char *argv[];
  95. {
  96. char  line[256];
  97. int   ncEOH, ncEOP, ncBOP, ncTRAILER ;
  98. char  fname[50];
  99. int   fcount=0, maxpages, pcount;
  100. int   notdone=1;
  101. FILE  *Fhead, *Fout;
  102.  
  103. /* USAGE CHECK */
  104. if (argc<3)
  105.   {
  106.   fprintf(stderr,"\n");
  107.   fprintf(stderr,"USAGE:  split-ps  n  fname < bigfile.ps\n");
  108.   fprintf(stderr,"where -   n = #pages max in each file\n");
  109.   fprintf(stderr,"      -   fname = base file name for output files;\n");
  110.   fprintf(stderr,"                  .0, .1, etc will be appended to each one.\n");
  111.   fprintf(stderr,"      -   bigfile.ps is a FrameMaker PostScript file.\n");
  112.   fprintf(stderr,"\n");
  113.   exit(99);
  114.   }
  115.  
  116. ncEOH=strlen(EOH);
  117. ncBOP=strlen(BOP);
  118. ncEOP=strlen(EOP);
  119. ncTRAILER=strlen(TRAILER);
  120. maxpages=atoi(argv[1]);
  121. printf("Creating files %s.xxxx of %d pages each.\n", argv[2], maxpages );
  122.  
  123. errno=0; if ( (Fhead=fopen(HEAD_FILE,"w+")) == NULL )
  124.   {
  125.   printf("Can't create temp output header file, errno=%d!\n", errno );
  126.   exit(1);
  127.   }
  128.  
  129. /* COPY HEADER TO TEMP FILE */
  130. while ( fgets(line,255,stdin) != NULL )
  131.   {
  132.   fprintf(Fhead, "%s", line);
  133.   if ( strncmp(EOH,line,ncEOH) == 0 ) break; /* END OF HEADER SENTINEL */
  134.   }
  135. fclose(Fhead);
  136.  
  137. while (notdone)
  138.   {
  139.   sprintf(fname,"%s.%d", argv[2], fcount );  /* CREATE NEW FILE NAME . EXT */
  140.   CopyHeader(fname,Fout);
  141.   errno=0; if ( (Fout=fopen(fname,"a")) == NULL )
  142.     {
  143.     printf("Can't open append output file %s, errno=%d!\n", fname, errno );
  144.     exit(3);
  145.     }
  146.   pcount=0;
  147.   while (pcount<maxpages)
  148.     {
  149.     if ( fgets(line,255,stdin) == NULL )
  150.       {
  151.       fprintf(stderr,"Expecting BOP marker: %s, got EOF, errno=%d.\n", BOP );
  152.       fclose(Fout);  exit(3);
  153.       }
  154.     if ( strncmp(TRAILER,line,ncTRAILER) == 0 )
  155.       { notdone=0; break; }
  156.     if ( strncmp(BOP,line,ncBOP) != 0 )
  157.       {
  158.       fprintf(stderr,"Expecting BOP marker: %s, got %s, errno=%d.\n", BOP, line );
  159.       fclose(Fout);  exit(4);
  160.       }
  161.     fprintf(Fout,"%s",line);
  162.     while ( fgets(line,255,stdin) != NULL )
  163.       {
  164.       fprintf(Fout,"%s",line);
  165.       if (strncmp(EOP,line,ncEOP)==0) { break; } 
  166.       }
  167.     ++pcount;
  168.     }
  169.   ++fcount;
  170.   fprintf(Fout,"%s\n",TRAILER);
  171.   fclose(Fout);
  172.   if (pcount==0) unlink(fname);
  173.             else fprintf(stderr,"%d pages in file %s\n",pcount,fname);
  174.   }
  175. unlink(HEAD_FILE);
  176. }
  177.  
  178.  
  179. CopyHeader(fname)
  180. char *fname;
  181. {
  182. FILE  *Fhead, *Fout;
  183. char  line[256];
  184.  
  185. errno=0; if ( (Fout=fopen(fname,"w+")) == NULL )
  186.   {
  187.   printf("Can't create output file %s, errno=%d!\n", fname, errno );
  188.   exit(3);
  189.   }
  190.  
  191. errno=0; if ( (Fhead=fopen(HEAD_FILE,"r")) == NULL )
  192.   {
  193.   printf("Can't read output header file, errno=%d!\n", errno );
  194.   exit(2);
  195.   }
  196.  
  197. while ( fgets(line,255,Fhead) != NULL )
  198.   {
  199.   fprintf(Fout, "%s", line);
  200.   }
  201.  
  202. fclose(Fout);
  203. fclose(Fhead);
  204.  
  205. }
  206.  
  207.  
  208.  
  209.