Home | History | Annotate | Line # | Download | only in fpr
fpr.c revision 1.9
      1  1.9    joerg /*	$NetBSD: fpr.c,v 1.9 2011/09/04 20:26:17 joerg 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.9    joerg __RCSID("$NetBSD: fpr.c,v 1.9 2011/09/04 20:26:17 joerg 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.9    joerg static char    cc;
     77  1.9    joerg static char    saved;
     78  1.9    joerg static int     length;
     79  1.9    joerg static char   *text;
     80  1.9    joerg static int     highcol;
     81  1.9    joerg static COLUMN *line;
     82  1.9    joerg static int     maxpos;
     83  1.9    joerg static int     maxcol;
     84  1.9    joerg 
     85  1.9    joerg static void	flush(void);
     86  1.9    joerg static void	get_text(void);
     87  1.9    joerg static void	init(void);
     88  1.9    joerg __dead static void	nospace(void);
     89  1.9    joerg static void	savech(int);
     90  1.4    lukem 
     91  1.4    lukem int
     92  1.9    joerg main(int argc, char **argv)
     93  1.4    lukem {
     94  1.4    lukem 	int ch;
     95  1.4    lukem 	char ateof;
     96  1.4    lukem 	int i;
     97  1.4    lukem 	int errorcount;
     98  1.4    lukem 
     99  1.4    lukem 	init();
    100  1.4    lukem 	errorcount = 0;
    101  1.4    lukem 	ateof = FALSE;
    102  1.4    lukem 
    103  1.9    joerg 	switch (ch = getchar()) {
    104  1.9    joerg 	case EOF:
    105  1.4    lukem 		exit(0);
    106  1.9    joerg 	case EOL:
    107  1.4    lukem 		cc = NUL;
    108  1.4    lukem 		ungetc((int) EOL, stdin);
    109  1.9    joerg 		break;
    110  1.9    joerg 	case BLANK:
    111  1.9    joerg 		cc = NUL;
    112  1.9    joerg 		break;
    113  1.9    joerg 	case '1':
    114  1.9    joerg 		cc = FF;
    115  1.9    joerg 		break;
    116  1.9    joerg 	case '0':
    117  1.9    joerg 		cc = EOL;
    118  1.9    joerg 		break;
    119  1.9    joerg 	case '+':
    120  1.9    joerg 		cc = CR;
    121  1.9    joerg 		break;
    122  1.9    joerg 	default:
    123  1.9    joerg 		errorcount = 1;
    124  1.9    joerg 		cc = NUL;
    125  1.9    joerg 		ungetc(ch, stdin);
    126  1.9    joerg 		break;
    127  1.9    joerg 	}
    128  1.4    lukem 
    129  1.4    lukem 	while (!ateof) {
    130  1.5  thorpej 		get_text();
    131  1.9    joerg 		switch (ch = getchar()) {
    132  1.9    joerg 		case EOF:
    133  1.4    lukem 			flush();
    134  1.4    lukem 			ateof = TRUE;
    135  1.9    joerg 			break;
    136  1.9    joerg 		case EOL:
    137  1.9    joerg 			flush();
    138  1.9    joerg 			cc = NUL;
    139  1.9    joerg 			ungetc((int) EOL, stdin);
    140  1.9    joerg 			break;
    141  1.9    joerg 		case BLANK:
    142  1.9    joerg 			flush();
    143  1.9    joerg 			cc = NUL;
    144  1.9    joerg 			break;
    145  1.9    joerg 		case '1':
    146  1.9    joerg 			flush();
    147  1.9    joerg 			cc = FF;
    148  1.9    joerg 			break;
    149  1.9    joerg 		case '0':
    150  1.9    joerg 			flush();
    151  1.9    joerg 			cc = EOL;
    152  1.9    joerg 			break;
    153  1.9    joerg 		case '+':
    154  1.9    joerg 			for (i = 0; i < length; i++)
    155  1.9    joerg 				savech(i);
    156  1.9    joerg 			break;
    157  1.9    joerg 		default:
    158  1.9    joerg 			errorcount++;
    159  1.9    joerg 			flush();
    160  1.9    joerg 			cc = NUL;
    161  1.9    joerg 			ungetc(ch, stdin);
    162  1.9    joerg 			break;
    163  1.9    joerg 		}
    164  1.4    lukem 	}
    165  1.4    lukem 
    166  1.9    joerg 	if (errorcount)
    167  1.9    joerg 		fprintf(stderr, "Illegal carriage control - %d line%s.\n",
    168  1.9    joerg 		    errorcount, errorcount == 1 ? "" : "s");
    169  1.1      cgd 
    170  1.4    lukem 	exit(0);
    171  1.4    lukem }
    172  1.1      cgd 
    173  1.9    joerg static void
    174  1.9    joerg init(void)
    175  1.1      cgd {
    176  1.4    lukem 	COLUMN *cp;
    177  1.4    lukem 	COLUMN *cend;
    178  1.4    lukem 	char *sp;
    179  1.1      cgd 
    180  1.4    lukem 	length = 0;
    181  1.4    lukem 	maxpos = MAXCOL;
    182  1.4    lukem 	sp = malloc((unsigned) maxpos);
    183  1.4    lukem 	if (sp == NULL)
    184  1.4    lukem 		nospace();
    185  1.4    lukem 	text = sp;
    186  1.1      cgd 
    187  1.4    lukem 	highcol = -1;
    188  1.4    lukem 	maxcol = MAXCOL;
    189  1.9    joerg 	line = calloc(maxcol, sizeof(COLUMN));
    190  1.4    lukem 	if (line == NULL)
    191  1.4    lukem 		nospace();
    192  1.4    lukem 	cp = line;
    193  1.4    lukem 	cend = line + (maxcol - 1);
    194  1.4    lukem 	while (cp <= cend) {
    195  1.4    lukem 		cp->width = INITWIDTH;
    196  1.9    joerg 		sp = calloc(INITWIDTH, sizeof(char));
    197  1.4    lukem 		if (sp == NULL)
    198  1.4    lukem 			nospace();
    199  1.4    lukem 		cp->str = sp;
    200  1.4    lukem 		cp++;
    201  1.4    lukem 	}
    202  1.1      cgd }
    203  1.1      cgd 
    204  1.9    joerg static void
    205  1.9    joerg get_text(void)
    206  1.1      cgd {
    207  1.4    lukem 	int i;
    208  1.4    lukem 	char ateol;
    209  1.4    lukem 	int ch;
    210  1.4    lukem 	int pos;
    211  1.7   itojun 	char *n;
    212  1.4    lukem 
    213  1.4    lukem 	i = 0;
    214  1.4    lukem 	ateol = FALSE;
    215  1.4    lukem 
    216  1.4    lukem 	while (!ateol) {
    217  1.9    joerg 		switch (ch = getchar()) {
    218  1.9    joerg 		case EOL:
    219  1.9    joerg 		case EOF:
    220  1.4    lukem 			ateol = TRUE;
    221  1.9    joerg 			break;
    222  1.9    joerg 		case TAB:
    223  1.9    joerg 			pos = (1 + i / TABSIZE) * TABSIZE;
    224  1.9    joerg 			if (pos > maxpos) {
    225  1.9    joerg 				n = realloc(text, (unsigned)(pos + 10));
    226  1.9    joerg 				if (n == NULL)
    227  1.9    joerg 					nospace();
    228  1.9    joerg 				text = n;
    229  1.9    joerg 				maxpos = pos + 10;
    230  1.9    joerg 			}
    231  1.9    joerg 			while (i < pos) {
    232  1.9    joerg 				text[i] = BLANK;
    233  1.9    joerg 				i++;
    234  1.9    joerg 			}
    235  1.9    joerg 			break;
    236  1.9    joerg 		case BS:
    237  1.9    joerg 			if (i > 0) {
    238  1.9    joerg 				i--;
    239  1.9    joerg 				savech(i);
    240  1.9    joerg 			}
    241  1.9    joerg 			break;
    242  1.9    joerg 		case CR:
    243  1.9    joerg 			while (i > 0) {
    244  1.9    joerg 				i--;
    245  1.9    joerg 				savech(i);
    246  1.9    joerg 			}
    247  1.9    joerg 			break;
    248  1.9    joerg 		case FF:
    249  1.9    joerg 		case VTAB:
    250  1.9    joerg 			flush();
    251  1.9    joerg 			cc = ch;
    252  1.9    joerg 			i = 0;
    253  1.9    joerg 			break;
    254  1.9    joerg 		default:
    255  1.9    joerg 			if (i >= maxpos) {
    256  1.9    joerg 				n = realloc(text, (unsigned)(i + 10));
    257  1.9    joerg 				if (n == NULL)
    258  1.9    joerg 					nospace();
    259  1.9    joerg 				maxpos = i + 10;
    260  1.9    joerg 			}
    261  1.9    joerg 			text[i] = ch;
    262  1.9    joerg 			i++;
    263  1.9    joerg 			break;
    264  1.9    joerg 		}
    265  1.1      cgd 	}
    266  1.1      cgd 
    267  1.4    lukem 	length = i;
    268  1.1      cgd }
    269  1.1      cgd 
    270  1.9    joerg static void
    271  1.9    joerg savech(int col)
    272  1.1      cgd {
    273  1.4    lukem 	char ch;
    274  1.4    lukem 	int oldmax;
    275  1.4    lukem 	COLUMN *cp;
    276  1.4    lukem 	COLUMN *cend;
    277  1.4    lukem 	char *sp;
    278  1.4    lukem 	int newcount;
    279  1.7   itojun 	COLUMN *newline;
    280  1.4    lukem 
    281  1.4    lukem 	ch = text[col];
    282  1.4    lukem 	if (ch == BLANK)
    283  1.4    lukem 		return;
    284  1.4    lukem 
    285  1.4    lukem 	saved = TRUE;
    286  1.4    lukem 
    287  1.4    lukem 	if (col >= highcol)
    288  1.4    lukem 		highcol = col;
    289  1.4    lukem 
    290  1.4    lukem 	if (col >= maxcol) {
    291  1.9    joerg 		newline = realloc(line, (unsigned) (col + 10) * sizeof(COLUMN));
    292  1.7   itojun 		if (newline == NULL)
    293  1.7   itojun 			nospace();
    294  1.7   itojun 		line = newline;
    295  1.4    lukem 		oldmax = maxcol;
    296  1.4    lukem 		maxcol = col + 10;
    297  1.4    lukem 		cp = line + oldmax;
    298  1.4    lukem 		cend = line + (maxcol - 1);
    299  1.4    lukem 		while (cp <= cend) {
    300  1.4    lukem 			cp->width = INITWIDTH;
    301  1.4    lukem 			cp->count = 0;
    302  1.9    joerg 			sp = calloc(INITWIDTH, sizeof(char));
    303  1.4    lukem 			if (sp == NULL)
    304  1.4    lukem 				nospace();
    305  1.4    lukem 			cp->str = sp;
    306  1.4    lukem 			cp++;
    307  1.4    lukem 		}
    308  1.4    lukem 	}
    309  1.4    lukem 	cp = line + col;
    310  1.4    lukem 	newcount = cp->count + 1;
    311  1.4    lukem 	if (newcount > cp->width) {
    312  1.4    lukem 		cp->width = newcount;
    313  1.4    lukem 		sp = realloc(cp->str, (unsigned) newcount * sizeof(char));
    314  1.4    lukem 		if (sp == NULL)
    315  1.4    lukem 			nospace();
    316  1.4    lukem 		cp->str = sp;
    317  1.1      cgd 	}
    318  1.4    lukem 	cp->count = newcount;
    319  1.4    lukem 	cp->str[newcount - 1] = ch;
    320  1.1      cgd }
    321  1.1      cgd 
    322  1.9    joerg static void
    323  1.9    joerg flush(void)
    324  1.1      cgd {
    325  1.4    lukem 	int i;
    326  1.4    lukem 	int anchor;
    327  1.4    lukem 	int height;
    328  1.4    lukem 	int j;
    329  1.4    lukem 
    330  1.4    lukem 	if (cc != NUL)
    331  1.4    lukem 		putchar(cc);
    332  1.4    lukem 
    333  1.4    lukem 	if (!saved) {
    334  1.4    lukem 		i = length;
    335  1.4    lukem 		while (i > 0 && text[i - 1] == BLANK)
    336  1.4    lukem 			i--;
    337  1.4    lukem 		length = i;
    338  1.4    lukem 		for (i = 0; i < length; i++)
    339  1.4    lukem 			putchar(text[i]);
    340  1.4    lukem 		putchar(EOL);
    341  1.4    lukem 		return;
    342  1.4    lukem 	}
    343  1.4    lukem 	for (i = 0; i < length; i++)
    344  1.4    lukem 		savech(i);
    345  1.4    lukem 
    346  1.4    lukem 	anchor = 0;
    347  1.4    lukem 	while (anchor <= highcol) {
    348  1.4    lukem 		height = line[anchor].count;
    349  1.4    lukem 		if (height == 0) {
    350  1.4    lukem 			putchar(BLANK);
    351  1.4    lukem 			anchor++;
    352  1.9    joerg 		} else if (height == 1) {
    353  1.9    joerg 			putchar(*(line[anchor].str));
    354  1.9    joerg 			line[anchor].count = 0;
    355  1.9    joerg 			anchor++;
    356  1.9    joerg 		} else {
    357  1.9    joerg 			i = anchor;
    358  1.9    joerg 			while (i < highcol && line[i + 1].count > 1)
    359  1.9    joerg 				i++;
    360  1.9    joerg 			for (j = anchor; j <= i; j++) {
    361  1.9    joerg 				height = line[j].count - 1;
    362  1.9    joerg 				putchar(line[j].str[height]);
    363  1.9    joerg 				line[j].count = height;
    364  1.4    lukem 			}
    365  1.9    joerg 			for (j = anchor; j <= i; j++)
    366  1.9    joerg 				putchar(BS);
    367  1.9    joerg 		}
    368  1.1      cgd 	}
    369  1.1      cgd 
    370  1.4    lukem 	putchar(EOL);
    371  1.4    lukem 	highcol = -1;
    372  1.1      cgd }
    373  1.1      cgd 
    374  1.9    joerg static void
    375  1.9    joerg nospace(void)
    376  1.1      cgd {
    377  1.4    lukem 	errx(1, "Storage limit exceeded.");
    378  1.1      cgd }
    379