postdaisy.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 (c) 1984, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
#ident "%Z%%M% %I% %E% SMI" /* SVr4.0 1.1 */
/*
*
* postdaisy - PostScript translator for Diablo 1640 files.
*
* A program that translates Diablo 1640 files into PostScript. Absolutely nothing
* is guaranteed. Quite a few things haven't been implemented, and what's been
* done isn't well tested. Most of the documentation used to write this program
* was taken from the 'Diablo Emulator' section of a recent Imagen manual.
*
* Some of document comments that are generated may not be right. Most of the test
* files I used produced a trailing blank page. I've put a check in formfeed() that
* won't print the last page if it doesn't contain any text, but PAGES comments may
* not be right. The DOCUMENTFONTS comment will also be wrong if auto underline or
* bold printing have been turned on by escape commands.
*
* The brute force approach used to implement horizontal and vertical tabs leaves
* much to be desired, and may not work for very small initial hmi and vmi values.
* At the very least I should have used malloc() to get space for the two tabstop
* arrays after hmi and vmi are known!
*
* Reverse printing mode hasn't been tested at all, but what's here should be
* close even though it's not efficient.
*
* The PostScript prologue is copied from *prologue before any of the input files
* are translated. The program expects that the following PostScript procedures
* are defined in that file:
*
* setup
*
* mark ... setup -
*
* Handles special initialization stuff that depends on how this 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.
*
* t
*
* mark str1 x1 str2 x2 ... strn xn y hmi t mark
*
* Handles all the text on the stack. Characters in the strings are
* printed using hmi as the character advance, and all strings are at
* vertical position y. Each string is begins at the horizontal position
* that preceeds it.
*
* f
*
* font f -
*
* Use font f, where f is the full PostScript font name. Only used when
* we switch to auto underline (Courier-Italic) or bold (Courier-Bold)
* printing.
*
* done
*
* done
*
* Makes sure the last page is printed. Only needed when we're printing
* more than one page on each sheet of paper.
*
* 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 "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 "postdaisy.h" /* a few special definitions */
char *optnames = "a:c:f:h:l:m:n:o:p:r:s:v:x:y:A:C:J:L:P:DI";
int hpos = 0; /* current horizontal */
int vpos = 0; /* and vertical position */
int rightmargin = RIGHTMARGIN;
int bottommargin = BOTTOMMARGIN;
int stringcount = 0; /* number of strings on the stack */
int linespp = 0; /* lines per page if it's positive */
int page = 0; /* page we're working on */
int printed = 0; /* printed this many pages */
/*****************************************************************************/
int agc;
char *agv[];
{
/*
*
* A simple program that translates Diablo 1640 files into PostScript. Nothing is
* guaranteed - the program not well tested and doesn't implement everything.
*
*/
init_signals(); /* sets up interrupt handling */
header(); /* PostScript header comments */
options(); /* handle the command line options */
setup(); /* for PostScript */
arguments(); /* followed by each input file */
done(); /* print the last page etc. */
account(); /* job accounting data */
} /* End of main */
/*****************************************************************************/
{
void interrupt(); /* signal handler */
/*
*
* Makes sure we handle interrupts.
*
*/
} else {
} /* End else */
} /* End of init_signals */
/*****************************************************************************/
header()
{
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 == '?' )
} /* End of header */
/*****************************************************************************/
options()
{
int ch; /* return value from getopt() */
int n; /* for CR and LF modes */
/*
*
* 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 for constant
* width fonts.
*
*/
switch ( ch ) {
case 'a': /* aspect ratio */
break;
case 'c': /* copies */
break;
case 'f': /* use this PostScript font */
break;
case 'h': /* default character spacing */
break;
case 'l': /* lines per page */
break;
case 'm': /* magnification */
break;
case 'n': /* forms per page */
break;
case 'o': /* output page list */
break;
case 'p': /* landscape or portrait mode */
if ( *optarg == 'l' )
break;
case 'r': /* set CR and LF modes */
if ( n & 01 )
if ( n & 02 )
break;
case 's': /* point size */
break;
case 'v': /* default line spacing */
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 */
} /* 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 */
/*****************************************************************************/
setup()
{
/*
*
* Handles things that must be done after the options are read but before the
* input files are processed.
*
*/
if ( formsperpage > 1 ) {
} /* End if */
} /* End of setup */
/*****************************************************************************/
{
/*
*
* 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 process 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 */
/*****************************************************************************/
done()
{
/*
*
* Finished with all the input files, so mark the end of the pages, make sure the
* last page is printed, and restore the initial environment.
*
*/
} /* End of done */
/*****************************************************************************/
account()
{
/*
*
* Writes an accounting record to *fp_acct provided it's not NULL. Accounting
* is requested using the -A or -J options.
*
*/
} /* End of account */
/*****************************************************************************/
text()
{
int ch; /* next input character */
/*
*
* Translates the next input file into PostScript. The redirect(-1) call forces
* end of each page doesn't go to stdout.
*
*/
formfeed(); /* force PAGE comment etc. */
inittabs();
switch ( ch ) {
case '\010': /* backspace */
backspace();
break;
case '\011': /* horizontal tab */
htab();
break;
case '\012': /* new line */
linefeed();
break;
case '\013': /* vertical tab */
vtab();
break;
case '\014': /* form feed */
formfeed();
break;
case '\015': /* carriage return */
carriage();
break;
case '\016': /* extended character set - SO */
break;
case '\017': /* extended character set - SI */
break;
case '\031': /* next char from supplementary set */
break;
case '\033': /* 2 or 3 byte escape sequence */
escape();
break;
default:
break;
} /* End switch */
formfeed(); /* next file starts on a new page? */
} /* End of text */
/*****************************************************************************/
inittabs()
{
int i; /* loop index */
/*
*
* Initializes the horizontal and vertical tab arrays. The way tabs are handled is
* quite inefficient and may not work for all initial hmi or vmi values.
*
*/
for ( i = 0; i < ROWS; i++ )
for ( i = 0; i < COLUMNS; i++ )
} /* End of inittabs */
/*****************************************************************************/
{
int i; /* loop index */
/*
*
* Clears all horizontal and vertical tab stops.
*
*/
for ( i = 0; i < ROWS; i++ )
for ( i = 0; i < COLUMNS; i++ )
} /* End of cleartabs */
/*****************************************************************************/
formfeed()
{
/*
*
* 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. I've added a simple test before the showpage that
* should eliminate the extra blank page that was put out at the end of many jobs,
* but the PAGES comments may be wrong.
*
*/
printed++;
endline(); /* print the last line */
redirect(-1);
markedpage = FALSE;
} /* End of formfeed */
/*****************************************************************************/
linefeed()
{
int line = 0; /* current line - based on ovmi */
/*
*
* Adjust our current vertical position. If we've passed the bottom of the page
* or exceeded the number of lines per page, print it and go to the upper left
* corner of the next page. This routine is also called from carriage() if crislf
* is ON.
*
*/
if ( linespp > 0 ) /* it means something so see where we are */
formfeed();
} /* End of linefeed */
/*****************************************************************************/
carriage()
{
/*
*
* Handles carriage return character. If crislf is ON we'll generate a line feed
* every time we get a carriage return character.
*
*/
advance = 1;
shadowprint = OFF;
linefeed();
} /* End of carriage */
/*****************************************************************************/
htab()
{
int col; /* 'column' we'll be at next */
int i; /* loop index */
/*
*
* Tries to figure out where the next tab stop is. Wasn't positive about this
* one, since hmi can change. I'll assume columns are determined by the original
* value of hmi. That fixes them on the page, which seems to make more sense than
* letting them float all over the place.
*
*/
endline();
col = i;
break;
} /* End if */
} /* End of htab */
/*****************************************************************************/
vtab()
{
int line; /* line we'll be at next */
int i; /* loop index */
/*
*
* Looks for the next vertical tab stop in the vtabstops[] array and moves to that
* line. If we don't find a tab we'll just move down one line - shouldn't happen.
*
*/
endline();
line = i;
break;
} /* End if */
} /* End of vtab */
/*****************************************************************************/
{
/*
*
* Moves backwards a distance equal to the current value of hmi provided we don't
* go past the left margin.
*
*/
endline();
} /* End of backspace */
/*****************************************************************************/
escape()
{
int ch; /* control character */
/*
*
* Handles special codes that are expected to follow an escape character. The
* initial escape character is followed by one or two bytes.
*
*/
case 'T': /* top margin */
break;
case 'L': /* bottom margin */
bottommargin = vpos;
break;
case 'C': /* clear top and bottom margins */
break;
case '9': /* left margin */
leftmargin = hpos;
break;
case '0': /* right margin */
rightmargin = hpos;
break;
case '1': /* set horizontal tab */
break;
case '8': /* clear horizontal tab at hpos */
break;
case '-': /* set vertical tab */
break;
case '2': /* clear all tabs */
cleartabs();
break;
case '\014': /* set lines per page */
break;
case '\037': /* set hmi to next byte minus 1 */
break;
case 'S': /* reset hmi to default */
break;
case '\011': /* move to column given by next byte */
break;
case '?': /* do carriage return after line feed */
break;
case '!': /* don't generate carriage return */
break;
case '5': /* forward print mode */
advance = 1;
break;
case '6': /* backward print mode */
advance = -1;
break;
case '\036': /* set vmi to next byte minus 1 */
break;
case '\013': /* move to line given by next byte */
break;
case 'U': /* positive half line feed */
break;
case 'D': /* negative half line feed */
break;
case '\012': /* negative line feed */
break;
case '\015': /* clear all margins */
break;
case 'E': /* auto underscore - use italic font */
changefont("/Courier-Oblique");
break;
case 'R': /* disable auto underscore */
break;
case 'W':
changefont("/Courier-Bold");
shadowprint = ON;
break;
case '&': /* disable bold printing */
shadowprint = OFF;
break;
case '/': /* ignored 2 byte escapes */
case '\\':
case '<':
case '>':
case '%':
case '=':
case '.':
case '4':
case 'A':
case 'B':
case 'M':
case 'N':
case 'P':
case 'Q':
case 'X':
case '\010':
break;
case ',': /* ignored 3 byte escapes */
case '\016':
case '\021':
break;
case '3': /* graphics mode - should quit! */
case '7':
case 'G':
case 'V':
case 'Y':
case 'Z':
break;
default:
break;
} /* End switch */
} /* End of escape */
/*****************************************************************************/
vmot(n)
int n; /* move this far vertically */
{
/*
*
* Move vertically n units from where we are.
*
*/
vpos += n;
} /* End of vmot */
/*****************************************************************************/
vgoto(n)
int n; /* new vertical position */
{
/*
*
* Moves to absolute vertical position n.
*
*/
vpos = n;
} /* End of vgoto */
/*****************************************************************************/
hmot(n)
int n; /* move this horizontally */
{
/*
*
* Moves horizontally n units from our current position.
*
*/
if ( hpos < leftmargin )
hpos = leftmargin;
} /* End of hmot */
/*****************************************************************************/
hgoto(n)
int n; /* go to this horizontal position */
{
/*
*
* Moves to absolute horizontal position n.
*
*/
hpos = n;
} /* End of hgoto */
/*****************************************************************************/
char *name;
{
/*
*
* Changes the current font. Used to get in and out of auto underscore and bold
* printing.
*
*/
endline();
} /* End of changefont */
/*****************************************************************************/
{
/*
*
* 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 ) {
lastc = -1;
prevx = -1;
stringcount = 1;
} /* End if */
} /* End of startline */
/*****************************************************************************/
endline()
{
/*
*
* Generates a call to the PostScript procedure that processes the text on the
* the stack - provided stringcount is positive.
*
*/
if ( stringcount > 0 )
stringcount = 0;
} /* End of endline */
/*****************************************************************************/
{
/*
*
* Takes the string we've been working on and adds it to the output file. Called
* when we need to adjust our horizontal position before starting a new string.
* Also called from endline() when we're done with the current line.
*
*/
if ( stringcount > 0 ) {
stringcount++;
} /* End if */
} /* End of endstring */
/*****************************************************************************/
int ch; /* 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. Reverse printing
* mode hasn't been tested but it should be close. hpos and lastx should disagree
* each time (except after startline() does something), and that should force a
* call to endstring() for every character.
*
*/
endline();
endline();
startline();
endstring();
} /* End if */
if ( advance != -1 )
markedpage = TRUE;
} /* End of oput */
/*****************************************************************************/
int pg; /* next page we're printing */
{
/*
*
* otherwise output goes to stdout.
*
*/
} /* End of redirect */
/*****************************************************************************/