home *** CD-ROM | disk | FTP | other *** search
- /*
- * split-ps.c
- *
- * Author: David Mallis (mallis@cfi.org), CAD Framework Initiative, 11/17/92
- *
- * Inspired by breakps, written by William Chia-Wei Cheng
- *
- * Designed for use on FrameMaker PostScript output in a UNIX environment.
- * Splits existing FM PostScript files into smaller free-standing ps files.
- * To compile: "cc -o split-ps split-ps.c"; should be portable.
- *
- * FrameMaker and PostScript are trademarks of their respective owners.
- * Written for and tested with FrameMaker 3.1X on a Sun, 4.1.1.
- *
- * This is free software, and may be used by any person for any reason.
- * NO WARRANTY is expressed or implied. Use at your own risk. We ask that
- * you acknowledge the origin if you incorporate this code elsewhere.
- *
- * NOTE: code is untested when using non-standard fonts, specifically
- * not tested in downloadable font situations.
- *
- * Your mileage may vary. Runs with 100% recycled electrons.
-
- Notes from the author:
-
- This is a quick-and-dirty program to chop up PostScript output files
- into subfiles with fewer pages, copying the prolog into each. A
- resulting `subfile' is also choppable by the same program.
-
- This was specifically written for FrameMaker files, although the
- algorithm will probably work with minimal modification for other,
- modern flavors of PS driver output.
-
- This has many dependencies and hard-codings, SOME of which are:
-
- * Assumes PS file has NEWLINE characters FREQUENTLY delimiting the code
- (at least every 255 characters). If not, enlarge the line[256]
- buffer.
-
- * If all \n have been squeezed out then the algorithm will have to
- be modified.
-
- * Assumes the file is structured with these tokens:
-
- ----Prolog Code used once for whole file----
- EOH token
- BOP token
- ---------ONE PAGE OF PS CODE-----
- EOP token
- BOP token
- ---------ONE PAGE OF PS CODE-----
- EOP token
- .
- .
- .
- TRAILER token
-
- * Note the defines for these expected token delimiters. Change them
- as needed.
-
- * If the structure of your PS file is more complicated, you will have
- to get your hands dirty in the algorithm.
-
- * Assumes that the PS driver was smart enough to put all font,
- graphics, and other PS procedure definitions in the prolog. This
- is not a trivial expectation. There are drivers that create new
- font definitions on the fly in page 32, for example, and then use
- them later in page 45. If this is the case with your PS driver
- output, then if you chop out page 32 you will get a PS error on
- page 45.
-
- * Assumes a scratch file named HEAD_FILE (see #define) can be
- overwritten then deleted.
-
- * Probably many other implicit assumptions too gross to mention.
-
- Also, this program does very little error checking on input parameters,
- file creation, etc.
-
- USE WITH DUE CAUTION.
- end comments */
-
- #include <errno.h>
- #include <stdio.h>
-
- #define EOH "%%EndSetup"
- #define EOP "%%EndPage"
- #define BOP "%%Page"
- #define TRAILER "%%Trailer"
- #define HEAD_FILE "QQhead"
-
- main(argc,argv)
- int argc;
- char *argv[];
- {
- char line[256];
- int ncEOH, ncEOP, ncBOP, ncTRAILER ;
- char fname[50];
- int fcount=0, maxpages, pcount;
- int notdone=1;
- FILE *Fhead, *Fout;
-
- /* USAGE CHECK */
- if (argc<3)
- {
- fprintf(stderr,"\n");
- fprintf(stderr,"USAGE: split-ps n fname < bigfile.ps\n");
- fprintf(stderr,"where - n = #pages max in each file\n");
- fprintf(stderr," - fname = base file name for output files;\n");
- fprintf(stderr," .0, .1, etc will be appended to each one.\n");
- fprintf(stderr," - bigfile.ps is a FrameMaker PostScript file.\n");
- fprintf(stderr,"\n");
- exit(99);
- }
-
- ncEOH=strlen(EOH);
- ncBOP=strlen(BOP);
- ncEOP=strlen(EOP);
- ncTRAILER=strlen(TRAILER);
- maxpages=atoi(argv[1]);
- printf("Creating files %s.xxxx of %d pages each.\n", argv[2], maxpages );
-
- errno=0; if ( (Fhead=fopen(HEAD_FILE,"w+")) == NULL )
- {
- printf("Can't create temp output header file, errno=%d!\n", errno );
- exit(1);
- }
-
- /* COPY HEADER TO TEMP FILE */
- while ( fgets(line,255,stdin) != NULL )
- {
- fprintf(Fhead, "%s", line);
- if ( strncmp(EOH,line,ncEOH) == 0 ) break; /* END OF HEADER SENTINEL */
- }
- fclose(Fhead);
-
- while (notdone)
- {
- sprintf(fname,"%s.%d", argv[2], fcount ); /* CREATE NEW FILE NAME . EXT */
- CopyHeader(fname,Fout);
- errno=0; if ( (Fout=fopen(fname,"a")) == NULL )
- {
- printf("Can't open append output file %s, errno=%d!\n", fname, errno );
- exit(3);
- }
- pcount=0;
- while (pcount<maxpages)
- {
- if ( fgets(line,255,stdin) == NULL )
- {
- fprintf(stderr,"Expecting BOP marker: %s, got EOF, errno=%d.\n", BOP );
- fclose(Fout); exit(3);
- }
- if ( strncmp(TRAILER,line,ncTRAILER) == 0 )
- { notdone=0; break; }
- if ( strncmp(BOP,line,ncBOP) != 0 )
- {
- fprintf(stderr,"Expecting BOP marker: %s, got %s, errno=%d.\n", BOP, line );
- fclose(Fout); exit(4);
- }
- fprintf(Fout,"%s",line);
- while ( fgets(line,255,stdin) != NULL )
- {
- fprintf(Fout,"%s",line);
- if (strncmp(EOP,line,ncEOP)==0) { break; }
- }
- ++pcount;
- }
- ++fcount;
- fprintf(Fout,"%s\n",TRAILER);
- fclose(Fout);
- if (pcount==0) unlink(fname);
- else fprintf(stderr,"%d pages in file %s\n",pcount,fname);
- }
- unlink(HEAD_FILE);
- }
-
-
- CopyHeader(fname)
- char *fname;
- {
- FILE *Fhead, *Fout;
- char line[256];
-
- errno=0; if ( (Fout=fopen(fname,"w+")) == NULL )
- {
- printf("Can't create output file %s, errno=%d!\n", fname, errno );
- exit(3);
- }
-
- errno=0; if ( (Fhead=fopen(HEAD_FILE,"r")) == NULL )
- {
- printf("Can't read output header file, errno=%d!\n", errno );
- exit(2);
- }
-
- while ( fgets(line,255,Fhead) != NULL )
- {
- fprintf(Fout, "%s", line);
- }
-
- fclose(Fout);
- fclose(Fhead);
-
- }
-
-
-
-