fpr.c revision 1.8 1 1.8 lukem /* $NetBSD: fpr.c,v 1.8 2008/07/21 14:19:22 lukem Exp $ */
2 1.3 jtc
3 1.1 cgd /*
4 1.3 jtc * Copyright (c) 1989, 1993
5 1.3 jtc * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * This code is derived from software contributed to Berkeley by
8 1.1 cgd * Robert Corbett.
9 1.1 cgd *
10 1.1 cgd * Redistribution and use in source and binary forms, with or without
11 1.1 cgd * modification, are permitted provided that the following conditions
12 1.1 cgd * are met:
13 1.1 cgd * 1. Redistributions of source code must retain the above copyright
14 1.1 cgd * notice, this list of conditions and the following disclaimer.
15 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 cgd * notice, this list of conditions and the following disclaimer in the
17 1.1 cgd * documentation and/or other materials provided with the distribution.
18 1.6 agc * 3. Neither the name of the University nor the names of its contributors
19 1.1 cgd * may be used to endorse or promote products derived from this software
20 1.1 cgd * without specific prior written permission.
21 1.1 cgd *
22 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 1.1 cgd * SUCH DAMAGE.
33 1.1 cgd */
34 1.1 cgd
35 1.4 lukem #include <sys/cdefs.h>
36 1.1 cgd #ifndef lint
37 1.8 lukem __COPYRIGHT("@(#) Copyright (c) 1989, 1993\
38 1.8 lukem The Regents of the University of California. All rights reserved.");
39 1.4 lukem #endif /* not lint */
40 1.1 cgd
41 1.1 cgd #ifndef lint
42 1.3 jtc #if 0
43 1.3 jtc static char sccsid[] = "@(#)fpr.c 8.1 (Berkeley) 6/6/93";
44 1.3 jtc #endif
45 1.8 lukem __RCSID("$NetBSD: fpr.c,v 1.8 2008/07/21 14:19:22 lukem Exp $");
46 1.4 lukem #endif /* not lint */
47 1.1 cgd
48 1.4 lukem #include <err.h>
49 1.1 cgd #include <stdio.h>
50 1.4 lukem #include <stdlib.h>
51 1.1 cgd
52 1.1 cgd #define BLANK ' '
53 1.1 cgd #define TAB '\t'
54 1.1 cgd #define NUL '\000'
55 1.1 cgd #define FF '\f'
56 1.1 cgd #define BS '\b'
57 1.1 cgd #define CR '\r'
58 1.1 cgd #define VTAB '\013'
59 1.1 cgd #define EOL '\n'
60 1.1 cgd
61 1.1 cgd #define TRUE 1
62 1.1 cgd #define FALSE 0
63 1.1 cgd
64 1.1 cgd #define MAXCOL 170
65 1.1 cgd #define TABSIZE 8
66 1.1 cgd #define INITWIDTH 8
67 1.1 cgd
68 1.1 cgd typedef
69 1.4 lukem struct column {
70 1.4 lukem int count;
71 1.4 lukem int width;
72 1.4 lukem char *str;
73 1.4 lukem }
74 1.4 lukem COLUMN;
75 1.4 lukem
76 1.4 lukem char cc;
77 1.4 lukem char saved;
78 1.4 lukem int length;
79 1.4 lukem char *text;
80 1.4 lukem int highcol;
81 1.1 cgd COLUMN *line;
82 1.4 lukem int maxpos;
83 1.4 lukem int maxcol;
84 1.1 cgd
85 1.4 lukem void flush __P((void));
86 1.5 thorpej void get_text __P((void));
87 1.4 lukem void init __P((void));
88 1.4 lukem int main __P((int, char **));
89 1.4 lukem void nospace __P((void));
90 1.4 lukem void savech __P((int));
91 1.4 lukem
92 1.4 lukem int
93 1.4 lukem main(argc, argv)
94 1.4 lukem int argc;
95 1.4 lukem char **argv;
96 1.4 lukem {
97 1.4 lukem int ch;
98 1.4 lukem char ateof;
99 1.4 lukem int i;
100 1.4 lukem int errorcount;
101 1.4 lukem
102 1.4 lukem
103 1.4 lukem init();
104 1.4 lukem errorcount = 0;
105 1.4 lukem ateof = FALSE;
106 1.4 lukem
107 1.4 lukem ch = getchar();
108 1.4 lukem if (ch == EOF)
109 1.4 lukem exit(0);
110 1.4 lukem
111 1.4 lukem if (ch == EOL) {
112 1.4 lukem cc = NUL;
113 1.4 lukem ungetc((int) EOL, stdin);
114 1.4 lukem } else
115 1.4 lukem if (ch == BLANK)
116 1.4 lukem cc = NUL;
117 1.4 lukem else
118 1.4 lukem if (ch == '1')
119 1.4 lukem cc = FF;
120 1.4 lukem else
121 1.4 lukem if (ch == '0')
122 1.4 lukem cc = EOL;
123 1.4 lukem else
124 1.4 lukem if (ch == '+')
125 1.4 lukem cc = CR;
126 1.4 lukem else {
127 1.4 lukem errorcount = 1;
128 1.4 lukem cc = NUL;
129 1.4 lukem ungetc(ch, stdin);
130 1.4 lukem }
131 1.4 lukem
132 1.4 lukem while (!ateof) {
133 1.5 thorpej get_text();
134 1.4 lukem ch = getchar();
135 1.4 lukem if (ch == EOF) {
136 1.4 lukem flush();
137 1.4 lukem ateof = TRUE;
138 1.4 lukem } else
139 1.4 lukem if (ch == EOL) {
140 1.4 lukem flush();
141 1.4 lukem cc = NUL;
142 1.4 lukem ungetc((int) EOL, stdin);
143 1.4 lukem } else
144 1.4 lukem if (ch == BLANK) {
145 1.4 lukem flush();
146 1.4 lukem cc = NUL;
147 1.4 lukem } else
148 1.4 lukem if (ch == '1') {
149 1.4 lukem flush();
150 1.4 lukem cc = FF;
151 1.4 lukem } else
152 1.4 lukem if (ch == '0') {
153 1.4 lukem flush();
154 1.4 lukem cc = EOL;
155 1.4 lukem } else
156 1.4 lukem if (ch == '+') {
157 1.4 lukem for (i = 0; i < length; i++)
158 1.4 lukem savech(i);
159 1.4 lukem } else {
160 1.4 lukem errorcount++;
161 1.4 lukem flush();
162 1.4 lukem cc = NUL;
163 1.4 lukem ungetc(ch, stdin);
164 1.4 lukem }
165 1.4 lukem }
166 1.4 lukem
167 1.4 lukem if (errorcount == 1)
168 1.4 lukem fprintf(stderr, "Illegal carriage control - 1 line.\n");
169 1.4 lukem else
170 1.4 lukem if (errorcount > 1)
171 1.4 lukem fprintf(stderr, "Illegal carriage control - %d lines.\n", errorcount);
172 1.1 cgd
173 1.4 lukem exit(0);
174 1.4 lukem }
175 1.1 cgd
176 1.4 lukem void
177 1.4 lukem init()
178 1.1 cgd {
179 1.4 lukem COLUMN *cp;
180 1.4 lukem COLUMN *cend;
181 1.4 lukem char *sp;
182 1.1 cgd
183 1.1 cgd
184 1.4 lukem length = 0;
185 1.4 lukem maxpos = MAXCOL;
186 1.4 lukem sp = malloc((unsigned) maxpos);
187 1.4 lukem if (sp == NULL)
188 1.4 lukem nospace();
189 1.4 lukem text = sp;
190 1.1 cgd
191 1.4 lukem highcol = -1;
192 1.4 lukem maxcol = MAXCOL;
193 1.4 lukem line = (COLUMN *) calloc(maxcol, (unsigned) sizeof(COLUMN));
194 1.4 lukem if (line == NULL)
195 1.4 lukem nospace();
196 1.4 lukem cp = line;
197 1.4 lukem cend = line + (maxcol - 1);
198 1.4 lukem while (cp <= cend) {
199 1.4 lukem cp->width = INITWIDTH;
200 1.4 lukem sp = calloc(INITWIDTH, (unsigned) sizeof(char));
201 1.4 lukem if (sp == NULL)
202 1.4 lukem nospace();
203 1.4 lukem cp->str = sp;
204 1.4 lukem cp++;
205 1.4 lukem }
206 1.1 cgd }
207 1.1 cgd
208 1.4 lukem void
209 1.5 thorpej get_text()
210 1.1 cgd {
211 1.4 lukem int i;
212 1.4 lukem char ateol;
213 1.4 lukem int ch;
214 1.4 lukem int pos;
215 1.7 itojun char *n;
216 1.4 lukem
217 1.4 lukem i = 0;
218 1.4 lukem ateol = FALSE;
219 1.4 lukem
220 1.4 lukem while (!ateol) {
221 1.4 lukem ch = getchar();
222 1.4 lukem if (ch == EOL || ch == EOF)
223 1.4 lukem ateol = TRUE;
224 1.4 lukem else
225 1.4 lukem if (ch == TAB) {
226 1.4 lukem pos = (1 + i / TABSIZE) * TABSIZE;
227 1.4 lukem if (pos > maxpos) {
228 1.7 itojun n = realloc(text, (unsigned)(pos + 10));
229 1.7 itojun if (n == NULL)
230 1.7 itojun nospace();
231 1.7 itojun text = n;
232 1.4 lukem maxpos = pos + 10;
233 1.4 lukem }
234 1.4 lukem while (i < pos) {
235 1.4 lukem text[i] = BLANK;
236 1.4 lukem i++;
237 1.4 lukem }
238 1.4 lukem } else
239 1.4 lukem if (ch == BS) {
240 1.4 lukem if (i > 0) {
241 1.4 lukem i--;
242 1.4 lukem savech(i);
243 1.4 lukem }
244 1.4 lukem } else
245 1.4 lukem if (ch == CR) {
246 1.4 lukem while (i > 0) {
247 1.4 lukem i--;
248 1.4 lukem savech(i);
249 1.4 lukem }
250 1.4 lukem } else
251 1.4 lukem if (ch == FF || ch == VTAB) {
252 1.4 lukem flush();
253 1.4 lukem cc = ch;
254 1.4 lukem i = 0;
255 1.4 lukem } else {
256 1.4 lukem if (i >= maxpos) {
257 1.7 itojun n = realloc(text, (unsigned)(i + 10));
258 1.7 itojun if (n == NULL)
259 1.7 itojun nospace();
260 1.4 lukem maxpos = i + 10;
261 1.4 lukem }
262 1.4 lukem text[i] = ch;
263 1.4 lukem i++;
264 1.4 lukem }
265 1.1 cgd }
266 1.1 cgd
267 1.4 lukem length = i;
268 1.1 cgd }
269 1.1 cgd
270 1.4 lukem void
271 1.1 cgd savech(col)
272 1.4 lukem int col;
273 1.1 cgd {
274 1.4 lukem char ch;
275 1.4 lukem int oldmax;
276 1.4 lukem COLUMN *cp;
277 1.4 lukem COLUMN *cend;
278 1.4 lukem char *sp;
279 1.4 lukem int newcount;
280 1.7 itojun COLUMN *newline;
281 1.4 lukem
282 1.4 lukem ch = text[col];
283 1.4 lukem if (ch == BLANK)
284 1.4 lukem return;
285 1.4 lukem
286 1.4 lukem saved = TRUE;
287 1.4 lukem
288 1.4 lukem if (col >= highcol)
289 1.4 lukem highcol = col;
290 1.4 lukem
291 1.4 lukem if (col >= maxcol) {
292 1.7 itojun newline = (COLUMN *) realloc(line,
293 1.7 itojun (unsigned) (col + 10) * sizeof(COLUMN));
294 1.7 itojun if (newline == NULL)
295 1.7 itojun nospace();
296 1.7 itojun line = newline;
297 1.4 lukem oldmax = maxcol;
298 1.4 lukem maxcol = col + 10;
299 1.4 lukem cp = line + oldmax;
300 1.4 lukem cend = line + (maxcol - 1);
301 1.4 lukem while (cp <= cend) {
302 1.4 lukem cp->width = INITWIDTH;
303 1.4 lukem cp->count = 0;
304 1.4 lukem sp = calloc(INITWIDTH, (unsigned) sizeof(char));
305 1.4 lukem if (sp == NULL)
306 1.4 lukem nospace();
307 1.4 lukem cp->str = sp;
308 1.4 lukem cp++;
309 1.4 lukem }
310 1.4 lukem }
311 1.4 lukem cp = line + col;
312 1.4 lukem newcount = cp->count + 1;
313 1.4 lukem if (newcount > cp->width) {
314 1.4 lukem cp->width = newcount;
315 1.4 lukem sp = realloc(cp->str, (unsigned) newcount * sizeof(char));
316 1.4 lukem if (sp == NULL)
317 1.4 lukem nospace();
318 1.4 lukem cp->str = sp;
319 1.1 cgd }
320 1.4 lukem cp->count = newcount;
321 1.4 lukem cp->str[newcount - 1] = ch;
322 1.1 cgd }
323 1.1 cgd
324 1.4 lukem void
325 1.1 cgd flush()
326 1.1 cgd {
327 1.4 lukem int i;
328 1.4 lukem int anchor;
329 1.4 lukem int height;
330 1.4 lukem int j;
331 1.4 lukem
332 1.4 lukem if (cc != NUL)
333 1.4 lukem putchar(cc);
334 1.4 lukem
335 1.4 lukem if (!saved) {
336 1.4 lukem i = length;
337 1.4 lukem while (i > 0 && text[i - 1] == BLANK)
338 1.4 lukem i--;
339 1.4 lukem length = i;
340 1.4 lukem for (i = 0; i < length; i++)
341 1.4 lukem putchar(text[i]);
342 1.4 lukem putchar(EOL);
343 1.4 lukem return;
344 1.4 lukem }
345 1.4 lukem for (i = 0; i < length; i++)
346 1.4 lukem savech(i);
347 1.4 lukem
348 1.4 lukem anchor = 0;
349 1.4 lukem while (anchor <= highcol) {
350 1.4 lukem height = line[anchor].count;
351 1.4 lukem if (height == 0) {
352 1.4 lukem putchar(BLANK);
353 1.4 lukem anchor++;
354 1.4 lukem } else
355 1.4 lukem if (height == 1) {
356 1.4 lukem putchar(*(line[anchor].str));
357 1.4 lukem line[anchor].count = 0;
358 1.4 lukem anchor++;
359 1.4 lukem } else {
360 1.4 lukem i = anchor;
361 1.4 lukem while (i < highcol && line[i + 1].count > 1)
362 1.4 lukem i++;
363 1.4 lukem for (j = anchor; j <= i; j++) {
364 1.4 lukem height = line[j].count - 1;
365 1.4 lukem putchar(line[j].str[height]);
366 1.4 lukem line[j].count = height;
367 1.4 lukem }
368 1.4 lukem for (j = anchor; j <= i; j++)
369 1.4 lukem putchar(BS);
370 1.4 lukem }
371 1.1 cgd }
372 1.1 cgd
373 1.4 lukem putchar(EOL);
374 1.4 lukem highcol = -1;
375 1.1 cgd }
376 1.1 cgd
377 1.4 lukem void
378 1.1 cgd nospace()
379 1.1 cgd {
380 1.4 lukem errx(1, "Storage limit exceeded.");
381 1.1 cgd }
382