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