xref: /original-bsd/local/transcript/src/pstext.c (revision 60c3b96a)
1 /*	@(#)pstext.c	1.2 09/15/87	*/
2 #ifndef lint
3 static char Notice[] = "Copyright (c) 1985 Adobe Systems Incorporated";
4 static char *RCSID="$Header: pstext.c,v 2.1 85/11/24 11:51:14 shore Rel $";
5 #endif
6 /* pstext.c
7  *
8  * Copyright (C) 1985 Adobe Systems Incorporated
9  *
10  * ultra-simple text formatter for PostScript
11  *
12  * pstext gets called when a text file gets spooled to a
13  * PostScript printer.  In this case, a text file is a
14  * file without the PostScript magic number (%!).
15  *
16  * In BSD systems, pstext is fork/execed from the spooler
17  * communications program, and has an argv like the spooler's.
18  * In SYSV systems, pstext is run by the top-level interface
19  * shell script, and has more control(?) and different args.
20  *
21  * If you want nicer listings, use enscript.
22  *
23  * Edit History:
24  * Andrew Shore: Sat Nov 16 12:09:25 1985
25  * End Edit History.
26  *
27  * RCSLOG:
28  * $Log:	pstext.c,v $
29  * Revision 2.1  85/11/24  11:51:14  shore
30  * Product Release 2.0
31  *
32  * Revision 1.4  85/11/20  00:55:32  shore
33  * Support for System V
34  * argv changes to work with both
35  * 4.2bsd and Sys V spooler interface
36  *
37  * Revision 1.3  85/06/16  20:31:19  shore
38  * fixed page-break bug
39  *
40  * Revision 1.2  85/05/14  11:27:46  shore
41  * fixed blank page bug
42  *
43  *
44  */
45 
46 #include <stdio.h>
47 #include <ctype.h>
48 #include "transcript.h"
49 
50 #define MAXWIDTH 132
51 #define MAXLINES 12
52 
53 private char	buf[MAXLINES][MAXWIDTH];/* MAXLINE lines of MAXWIDTH chars */
54 private int	maxcol[MAXLINES] = {-1};/* max col used in each lines */
55 
56 private int	width = 132;
57 private int	length = 66;
58 private int	indent = 0;
59 private int	controls;
60 private char	*prog;
61 
62 main(argc, argv)
63 int argc;
64 char **argv;
65 {
66     register char *cp;
67     register int ch;
68     int	lineno = 0;
69     int npages = 1;
70     int blanklines = 0;
71     int donepage = 0;
72     int done, linedone, maxline, i, col;
73     char tempfile[512];
74     char *l, *libdir;
75 
76     prog = *argv;
77 
78     /* initialize line buffer to blanks */
79     done = 0;
80     for (cp = buf[0], l = buf[MAXLINES]; cp < l; *cp++ = ' ');
81 
82     /* put out header */
83     if ((libdir = envget("PSLIBDIR")) == NULL) libdir = LibDir;
84     if (copyfile(mstrcat(tempfile,libdir,TEXTPRO,sizeof tempfile), stdout)) {
85 	fprintf(stderr,"%s: trouble copying text prolog\n",prog);
86 	exit(2);
87     }
88     while (!done) {
89 	col = indent;
90 	maxline = -1;
91 	linedone = 0;
92 	while (!linedone) {
93 	    switch (ch = getchar()) {
94 		case EOF:
95 			linedone = done = 1;
96 			break;
97 		case '\f':
98 			lineno = length;
99 		case '\n':
100 			linedone = 1;
101 			break;
102 		case '\b':
103 			if (--col < indent) col = indent;
104 			break;
105 		case '\r':
106 			col = indent;
107 			break;
108 		case '\t':
109 			col = ((col - indent) | 07) + indent + 1;
110 			break;
111 
112 		default:
113 			if ((col >= width) ||
114 			    (!controls && (!isascii(ch) || !isprint(ch)))) {
115 			    col++;
116 			    break;
117 			}
118 			for (i = 0; i < MAXLINES; i++) {
119 			    if (i > maxline) maxline = i;
120 			    cp = &buf[i][col];
121 			    if (*cp == ' ') {
122 				*cp = ch;
123 				if (col > maxcol[i])
124 				    maxcol[i] = col;
125 				break;
126 			    }
127 			}
128 			col++;
129 			break;
130 	    }
131 	}
132 	/* print out lines */
133 	if (maxline == -1) {
134 	    blanklines++;
135 	}
136 	else {
137 	    if (blanklines) {
138 		if (!donepage) {
139 		    printf("%%%%Page: %d %d\nStartPage\n",
140 		    	npages, npages);
141 		    donepage = 1;
142 		}
143 		if (blanklines == 1) {
144 		    printf("B\n");
145 		}
146 		else {
147 		    printf("%d L\n", blanklines);
148 		}
149 		blanklines = 0;
150 	    }
151 	    for (i = 0; i <= maxline; i++) {
152 		if (!donepage) {
153 		    printf("%%%%Page: %d %d\nStartPage\n",
154 		    	npages, npages);
155 		    donepage = 1;
156 		}
157 		putchar('(');
158 		for (cp = buf[i], l = cp+maxcol[i]; cp <= l;) {
159 		    switch (*cp) {
160 			case '(': case ')': case '\\':
161 			    putchar('\\');
162 			default:
163 			    putchar(*cp);
164 			    *cp++ = ' ';
165 		    }
166 		}
167 		printf(")%s\n", (i < maxline) ? "" : "S");
168 	        maxcol[i] = -1;
169 	    }
170 	}
171 	if (++lineno >= length) {
172 	    if (!donepage)
173 		printf("%%%%Page: %d %d\nStartPage\n", npages, npages);
174 	    printf("EndPage\n");
175 	    npages++;
176 	    donepage = 0;
177 	    lineno = 0;
178 	    blanklines = 0;
179 	}
180     }
181     if (lineno && donepage) {
182 	printf("EndPage\n");
183 	donepage = 0;
184 	npages++;
185     }
186     printf("%%%%Trailer\n");
187     VOIDC fclose(stdout);
188     exit(0);
189 }
190