postprint.c revision f928ce67ef743c33ea27c573c9c7e2d4a4833cbd
/*
* 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 2005 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
#pragma ident "%Z%%M% %I% %E% SMI"
/*
*
* postprint - PostScript translator for ASCII files.
*
* A simple program that translates ASCII files into PostScript. All it really
* does is expand tabs and backspaces, handle character quoting, print text lines,
* and control when pages are started based on the requested number of lines per
* page.
*
* The PostScript prologue is copied from *prologue before any of the input files
* are translated. The program expects that the following procedures are defined
* in that file:
*
* setup
*
* mark ... setup -
*
* Handles special initialization stuff that depends on how the program
* stack. The def operator is applied to each pair up to the mark, then
* the default state is set up.
*
* pagesetup
*
* page pagesetup -
*
* Does whatever is needed to set things up for the next page. Expects
* to find the current page number on the stack.
*
* l
*
* string l -
*
* Prints string starting in the first column and then goes to the next
* line.
*
* L
*
* mark string column string column ... L mark
*
* Prints each string on the stack starting at the horizontal position
* selected by column. Used when tabs and spaces can be sufficiently well
* compressed to make the printer overhead worthwhile. Always used when
* we have to back up.
*
* done
*
* done
*
* Makes sure the last page is printed. Only needed when we're printing
* more than one page on each sheet of paper.
*
* Almost everything has been changed in this version of postprint. The program
* is more intelligent, especially about tabs, spaces, and backspacing, and as a
* result output files usually print faster. Output files also now conform to
* Adobe's file structuring conventions, which is undoubtedly something I should
* have done in the first version of the program. If the number of lines per page
* is set to 0, which can be done using the -l option, pointsize will be used to
* guess a reasonable value. The estimate is based on the values of LINESPP,
* POINTSIZE, and pointsize, and assumes LINESPP lines would fit on a page if
* we printed in size POINTSIZE. Selecting a point size using the -s option and
* adding -l0 to the command line forces the guess to be made.
*
* Many default values, like the magnification and orientation, are defined in
* the prologue, which is where they belong. If they're changed (by options), an
* appropriate definition is made after the prologue is added to the output file.
* The -P option passes arbitrary PostScript through to the output file. Among
* other things it can be used to set (or change) values that can't be accessed by
* other options.
*
*/
#include <stdio.h>
#include <signal.h>
#include <ctype.h>
#include <fcntl.h>
#include <unistd.h>
#include "comments.h" /* PostScript file structuring comments */
#include "gen.h" /* general purpose definitions */
#include "path.h" /* for the prologue */
#include "ext.h" /* external variable declarations */
#include "postprint.h" /* a few special definitions */
char *optnames = "a:c:e:f:l:m:n:o:p:r:s:t:x:y:A:C:J:L:P:R:DI";
int crmode = 0; /* carriage return mode - 0, 1, or 2 */
int stringcount = 0; /* number of strings on the stack */
int page = 0; /* page we're working on */
int printed = 0; /* printed this many pages */
static void account(void);
static void arguments(void);
static void done(void);
static void endline(void);
static void formfeed(void);
static void header(void);
static void init_signals(void);
static void newline(void);
static void options(void);
static void oput(int);
static void redirect(int);
static void setup(void);
static void spaces(int);
static void startline(void);
static void text(void);
/*****************************************************************************/
int
{
/*
*
* A simple program that translates ASCII files into PostScript. If there's more
* than one input file, each begins on a new page.
*
*/
init_signals(); /* sets up interrupt handling */
header(); /* PostScript header and prologue */
setup(); /* for PostScript */
arguments(); /* followed by each input file */
done(); /* print the last page etc. */
account(); /* job accounting data */
return (x_stat); /* not much could be wrong */
} /* End of main */
/*****************************************************************************/
static void
init_signals(void)
{
void interrupt(); /* signal handler */
/*
*
* Makes sure we handle interrupts.
*
*/
} else {
} /* End else */
} /* End of init_signals */
/*****************************************************************************/
static void
header(void)
{
int ch; /* return value from getopt() */
/*
*
* Scans the option list looking for things, like the prologue file, that we need
* right away but could be changed from the default. Doing things this way is an
* attempt to conform to Adobe's latest file structuring conventions. In particular
* they now say there should be nothing executed in the prologue, and they have
* added two new comments that delimit global initialization calls. Once we know
* where things really are we write out the job header, follow it by the prologue,
* and then add the ENDPROLOG and BEGINSETUP comments.
*
*/
if ( ch == 'L' )
else if ( ch == '?' )
options(); /* handle the command line options */
} /* End of header */
/*****************************************************************************/
static void
options(void)
{
int ch; /* return value from getopt() */
int euro = 0;
extern char *getenv(char *);
/*
*
* Reads and processes the command line options. Added the -P option so arbitrary
* PostScript code can be passed through. Expect it could be useful for changing
* definitions in the prologue for which options have not been defined.
*
* Although any PostScript font can be used, things will only work well for
* constant width fonts.
*
*/
/* if there is a locale specific prologue, use it as the default */
else
}
/* if the locale has 8859-15 or euro in it, add the symbol to font */
euro = 1;
}
#if defined(DEBUG)
#endif
switch ( ch ) {
case 'a': /* aspect ratio */
break;
case 'c': /* copies */
break;
case 'e': /* should we add the euro ? */
break;
case 'f': /* use this PostScript font */
break;
case 'l': /* lines per page */
break;
case 'm': /* magnification */
break;
case 'n': /* forms per page */
if (formsperpage <= 0) {
/* set default value */
formsperpage = 1;
}
break;
case 'o': /* output page list */
break;
case 'p': /* landscape or portrait mode */
if ( *optarg == 'l' )
break;
case 'r': /* carriage return mode */
break;
case 's': /* point size */
break;
case 't': /* tabstops */
if (tabstops <= 0) {
/* set default */
}
break;
case 'x': /* shift things horizontally */
break;
case 'y': /* and vertically on the page */
break;
case 'A': /* force job accounting */
case 'J':
break;
case 'C': /* copy file straight to output */
break;
case 'L': /* PostScript prologue file */
break;
case 'P': /* PostScript pass through */
break;
case 'R': /* special global or page level request */
break;
case 'D': /* debug flag */
break;
case 'I': /* ignore FATAL errors */
break;
case '?': /* don't understand the option */
break;
default: /* don't know what to do for ch */
break;
} /* End switch */
} /* End while */
if (euro != 0)
} /* End of options */
/*****************************************************************************/
char *name; /* name the user asked for */
{
int i; /* for looking through fontmap[] */
/*
*
* Called from options() to map a user's font name into a legal PostScript name.
* If the lookup fails *name is returned to the caller. That should let you choose
* any PostScript font, although things will only work well for constant width
* fonts.
*
*/
return(name);
} /* End of get_font */
/*****************************************************************************/
static void
setup(void)
{
/*
*
* Handles things that must be done after the options are read but before the
* input files are processed. linespp (lines per page) can be set using the -l
* option. If it's not positive we calculate a reasonable value using the
* requested point size - assuming LINESPP lines fit on a page in point size
* POINTSIZE.
*
*/
if ( formsperpage > 1 ) {
} /* End if */
if ( linespp <= 0 )
} /* End of setup */
/*****************************************************************************/
static void
arguments(void)
{
/*
*
* Makes sure all the non-option command line arguments are processed. If we get
* here and there aren't any arguments left, or if '-' is one of the input files
* we'll translate stdin.
*
*/
if ( argc < 1 )
text();
else { /* at least one argument is left */
while ( argc > 0 ) {
text();
argc--;
argv++;
} /* End while */
} /* End else */
} /* End of arguments */
/*****************************************************************************/
static void
done(void)
{
/*
*
* Finished with all the input files, so mark the end of the pages with a TRAILER
* comment, make sure the last page prints, and add things like the PAGES comment
* that can only be determined after all the input files have been read.
*
*/
while (printed % formsperpage) {
printed++;
}
}
} /* End of done */
/*****************************************************************************/
static void
account(void)
{
/*
*
* Writes an accounting record to *fp_acct provided it's not NULL. Accounting is
* requested using the -A or -J options.
*
*/
} /* End of account */
/*****************************************************************************/
static void
text(void)
{
int ch; /* next input character */
/*
*
* Translates *fp_in into PostScript. All we do here is handle newlines, tabs,
* backspaces, and quoting of special characters. All other unprintable characters
* are totally ignored. The redirect(-1) call forces the initial output to go to
*
*/
formfeed(); /* force PAGE comment etc. */
switch ( ch ) {
case '\n':
newline();
break;
case '\t':
case '\b':
case ' ':
break;
case '\014':
formfeed();
break;
case '\r':
if ( crmode == 1 )
else if ( crmode == 2 )
newline();
break;
case '(':
case ')':
case '\\':
startline();
/*
*
* Fall through to the default case.
*
*/
default:
else {
startline();
col++;
}
}
break;
} /* End switch */
formfeed(); /* next file starts on a new page? */
} /* End of text */
/*****************************************************************************/
static void
formfeed(void)
{
/*
*
* Called whenever we've finished with the last page and want to get ready for the
* next one. Also used at the beginning and end of each input file, so we have to
* be careful about what's done. The first time through (up to the redirect() call)
*
* Adobe now recommends that the showpage operator occur after the page level
* restore so it can be easily redefined to have side-effects in the printer's VM.
* Although it seems reasonable I haven't implemented it, because it makes other
* things, like selectively setting manual feed or choosing an alternate paper
* tray, clumsy - at least on a per page basis.
*
*/
printed++;
endline(); /* print the last line */
if (printed % formsperpage == 0)
redirect(-1);
if (printed % formsperpage == 0)
line = 1;
} /* End of formfeed */
/*****************************************************************************/
static void
newline(void)
{
/*
*
* Called when we've read a newline character. The call to startline() ensures
* that at least an empty string is on the stack.
*
*/
startline();
endline(); /* print the current line */
formfeed();
} /* End of newline */
/*****************************************************************************/
static void
/* next input character */
{
int endcol; /* ending column */
int i; /* final distance - in spaces */
/*
*
* Counts consecutive spaces, tabs, and backspaces and figures out where the next
* string should start. Once that's been done we try to choose an efficient way
* to output the required number of spaces. The choice is between using procedure
* l with a single string on the stack and L with several string and column pairs.
* We usually break even, in terms of the size of the output file, if we need four
* consecutive spaces. More means using L decreases the size of the file. For now
* if there are less than 6 consecutive spaces we just add them to the current
* string, otherwise we end that string, follow it by its starting position, and
* begin a new one that starts at endcol. Backspacing is always handled this way.
*
*/
startline(); /* so col makes sense */
do {
if ( ch == ' ' )
endcol++;
else if ( ch == '\t' )
else if ( ch == '\b' )
endcol--;
else if ( ch == '\r' )
endcol = 1;
else break;
endcol = 1;
for ( ; i > 0; i-- )
oput((int)' ');
else {
stringcount++;
} /* End else */
} /* End of spaces */
/*****************************************************************************/
static void
startline(void)
{
/*
*
* Called whenever we want to be certain we're ready to start pushing characters
* into an open string on the stack. If stringcount is positive we've already
* started, so there's nothing to do. The first string starts in column 1.
*
*/
if ( stringcount < 1 ) {
stringcount = 1;
} /* End if */
} /* End of startline */
/*****************************************************************************/
static void
endline(void)
{
/*
*
* Generates a call to the PostScript procedure that processes all the text on
* the stack - provided stringcount is positive. If one string is on the stack
* the fast procedure (ie. l) is used to print the line, otherwise the slower
* one that processes string and column pairs is used.
*
*/
if ( stringcount == 1 )
else if ( stringcount > 1 )
stringcount = 0;
} /* End of endline */
/*****************************************************************************/
static void
/* next output character */
{
/*
*
* Responsible for adding all printing characters from the input file to the
* open string on top of the stack. The only other characters that end up in
* that string are the quotes required for special characters. Some simple
* changes here and in spaces could make line wrapping possible. Doing a good
* job would probably force lots of printer dependent stuff into the program,
* so I haven't bothered with it. Could also change the prologue, or perhaps
* write a different one, that uses kshow instead of show to display strings.
*
*/
startline();
col++;
} /* End of oput */
/*****************************************************************************/
static void
/* next page we're printing */
{
/*
*
* otherwise output goes to stdout.
*
*/
} /* End of redirect */
/*****************************************************************************/