postreverse.c revision 7c478bd95313f5f23a4c958a745db2134aa03244
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2004 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "postreverse.h"
/*
* This version of postreverse should parse any Adobe DSC conforming
* PostScript file and most that are not conforming, but minimally have the
* page (%%Page:) and trailer (%%Trailer) comments in them at the begining of
* the line.
*
* If a document cannot be parsed (no page and trailer comments), it is passed
* through untouched. If you look through the code you will find that it
* doesn't ever look for the PostScript magic (%!). This is because it
* assumes that PostScript is sent in. If PostScript is in sent in, it will
* still attempt to parse it based on DSC page and trailer comments as if it
* were postscript.
*
* flow goes as follows:
* 1) get command line options (including parsing a page
* list if supplied)
* 2) if no filename is supplied in command line, copy
* stdin to temp file.
* 3) parse the document:
* start from begining looking for a DSC page comment
* (that is the header) start from the end looking for
* a DSC trailer comment (that is the trailer) start from
* the header until the trailer looking for DSC page
* comments. Each one signifies a new page.
* start from the header until the trailer looking for BSD
* global comments. Each one violates page independence and
* will be stored so it can be printed after the header and
* before any pages.
* 4) print the document: if there is no header, trailer, or
* pages, print it from start to end unaltered if they all
* exist, print the header, pages, and trailer the pages
* are compared against a page list before being printed,
* and are reversed if the reverse flag has been set.
* If global definitions were found in the pages of a
* document, they are printed after the header and before
* the pages.
*/
static void *
{
if (!ret) {
"postreverse : malloc() failed : Out of memory\n");
exit(2);
}
return (ret);
}
static void *
{
if (!ret) {
"postreverse : realloc() failed - Out of memory\n");
exit(2);
}
return (ret);
}
/*
* nstrlen() provides the same functionality as strlen() while also checking
* that the pointer does not cross the end of file.
*
* Returns the number of non-NULL bytes in string argument.
*/
static size_t
{
const char *s0 = s;
while (s < bptr && *s != '\0')
s++;
return (s - s0);
}
/*
* nstrstr() provides the same functionality as strstr() while also checking
* that the pointers do not cross the end of the file.
*
* nstrstr() locates the first occurrence in the string as1 of the sequence of
* characters (excluding the terminating null character) in the string as2.
* nstrstr() returns a pointer to the located string, or a null pointer if
* the string is not found. If as2 is "", the function returns as1.
*/
static char *
{
const char *tptr;
char c;
return ((char *)s1);
c = *s2;
if (*s1++ == c) {
if (c == 0)
return ((char *)tptr - 1);
c = *s2;
}
return (NULL);
}
/*
* caddr_t strrstr(caddr_t as1, caddr_t as2 char *bptr1)
* return the address of the beginning of the last occruence of as2
* in as1 or NULL if not found
*/
{
char c;
return ((char *)t1);
c = *t2;
if (*t1-- == c) {
return ((char *)t1 + 1);
c = *t2;
}
return (NULL);
}
/*
* Copy stdin to a temp file and return the name
*/
char *
{
int fd;
int count;
return (NULL);
}
return (NULL);
}
return (fileName);
}
/*
* Usage(char *name) - program usage
*/
void
{
exit(1);
}
/*
* int **ParsePageList(char *list)
* This will parse as string #,#,#-#,#... into an array of pointers
* to integers. This array will contain all numbers in the list including
* those int the range #-#. The list returned is NULL terminated.
* It uses 2 passes to build the list. pass 1 counts the # of ints and
* allocates the space, and pass 2 fills in the list.
*/
int **
ParsePageList(char *list)
{
int pass = 0;
return (NULL);
while (pass++ < 2) {
char *page;
char *tmplist;
int size = 0;
do {
}
} else
if (pass == 1)
/* count the pages for allocation */
else { /* fill in the page list */
}
if (pass == 1)
}
return (pageList);
}
/*
* int PageIsListed(int page, int **pageList)
* returns 1 if the pagelist is empty or if the page is in the
* NULL terminated pageList. returns 0 if the page is not listed
*/
int
{
int count = 0;
if (!pageList)
return (1);
return (1);
return (0);
}
/*
* Writes the document Header to the fd
*/
int
{
if (d) {
if (h)
}
return (-1);
}
/*
* Writes the document global block to the fd
*/
int
{
if (g)
return (-1);
}
/*
* Writes the document Trailer to the fd
*/
int
{
if (d) {
if (t)
}
return (-1);
}
/*
* Writes the document page to the fd
*/
int
{
if (p) {
(global != 0)) {
/* BeginGlobal/EndGlobal in the page... */
} else
}
return (-1);
}
/*
* Writes out the document pages in pageList (or all if NULL) and reverse
* the output if reverse == 1
*/
void
{
int count = 0;
int prnindex;
if (global)
}
}
}
} else {
}
}
/*
* get a document header from document and return a pointer to a HEADER
* structure.
*/
HEADER *
{
} else {
}
return (header);
}
/*
* get a document trailer from document and return a pointer to a trailer
* structure.
*/
TRAILER *
{
} else {
}
return (trailer);
}
GLOBAL **
{
int count = 0;
long allocated_slots = 0;
count++;
== NULL) {
"DSC violation: %%%%BeginGlobal "
"with no %%%%EndGlobal\n");
exit(-1);
}
if (count > allocated_slots) {
(allocated_slots + BLOCKSIZE) *
sizeof (GLOBAL *));
allocated_slots * sizeof (GLOBAL *), 0,
sizeof (GLOBAL *));
}
}
}
return (globals);
}
/*
* get the pages from a document and return a pointer a list of PAGE
* structures.
*/
PAGE **
{
long allocated_slots = 0;
long no_pages = 0;
long number;
no_pages++;
if (no_pages > allocated_slots) {
}
number = -1;
/* page start & end */
else
/* page label & number */
}
}
}
return (pages);
}
/*
* parse a document and return a pointer to a DOCUMENT structure
*/
DOCUMENT *
DocumentParse(char *name)
{
int fd;
return (NULL);
}
return (NULL);
}
return (NULL);
}
" MAP_SHARED, %d, 0): %s\n",
} else {
/* order in important */
}
return (document);
}
#if defined(DEBUG)
/*
* Print out the contents of the document structure
*/
void
{
if (d) {
printf("Document:\n\tname: %s\n\tstart: 0x%x\n\tsize: %ld\n",
if (d->header) {
printf("\tHeader: %s (0x%x, %ld)\n",
}
if (d->global) {
int count = 0;
}
if (d->page) {
PAGE *p;
int count = 0;
printf("\t\t %4d (%s) - (0x%x, %ld)\n",
p->number,
}
if (d->trailer) {
printf("\tTrailer: %s (0x%x, %ld)\n",
}
}
}
#endif /* DEBUG */
{
char *programName = NULL;
char *unlinkFile = NULL;
int reversePages = 1;
int option;
programName++;
else
programName = av[0];
switch (option) {
case 'o':
break;
case 'r':
reversePages = 0;
break;
case '?':
break;
default:
break;
}
switch (ac) {
case 0:
break;
case 1:
break;
default:
}
exit(0);
}
#endif /* DEBUG */
if (unlinkFile)
exit(0);
}