Home | History | Annotate | Line # | Download | only in col
col.c revision 1.2
      1  1.1      cgd /*-
      2  1.1      cgd  * Copyright (c) 1990 The Regents of the University of California.
      3  1.1      cgd  * All rights reserved.
      4  1.1      cgd  *
      5  1.1      cgd  * This code is derived from software contributed to Berkeley by
      6  1.1      cgd  * Michael Rendell of the Memorial University of Newfoundland.
      7  1.1      cgd  *
      8  1.1      cgd  * Redistribution and use in source and binary forms, with or without
      9  1.1      cgd  * modification, are permitted provided that the following conditions
     10  1.1      cgd  * are met:
     11  1.1      cgd  * 1. Redistributions of source code must retain the above copyright
     12  1.1      cgd  *    notice, this list of conditions and the following disclaimer.
     13  1.1      cgd  * 2. Redistributions in binary form must reproduce the above copyright
     14  1.1      cgd  *    notice, this list of conditions and the following disclaimer in the
     15  1.1      cgd  *    documentation and/or other materials provided with the distribution.
     16  1.1      cgd  * 3. All advertising materials mentioning features or use of this software
     17  1.1      cgd  *    must display the following acknowledgement:
     18  1.1      cgd  *	This product includes software developed by the University of
     19  1.1      cgd  *	California, Berkeley and its contributors.
     20  1.1      cgd  * 4. Neither the name of the University nor the names of its contributors
     21  1.1      cgd  *    may be used to endorse or promote products derived from this software
     22  1.1      cgd  *    without specific prior written permission.
     23  1.1      cgd  *
     24  1.1      cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25  1.1      cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  1.1      cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  1.1      cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28  1.1      cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  1.1      cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  1.1      cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  1.1      cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  1.1      cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  1.1      cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  1.1      cgd  * SUCH DAMAGE.
     35  1.1      cgd  */
     36  1.1      cgd 
     37  1.1      cgd #ifndef lint
     38  1.1      cgd char copyright[] =
     39  1.1      cgd "@(#) Copyright (c) 1990 The Regents of the University of California.\n\
     40  1.1      cgd  All rights reserved.\n";
     41  1.1      cgd #endif /* not lint */
     42  1.1      cgd 
     43  1.1      cgd #ifndef lint
     44  1.2  mycroft /*static char sccsid[] = "from: @(#)col.c	5.3 (Berkeley) 2/2/91";*/
     45  1.2  mycroft static char rcsid[] = "$Id: col.c,v 1.2 1993/08/01 18:17:35 mycroft Exp $";
     46  1.1      cgd #endif /* not lint */
     47  1.1      cgd 
     48  1.1      cgd #include <errno.h>
     49  1.1      cgd #include <ctype.h>
     50  1.1      cgd #include <string.h>
     51  1.1      cgd #include <stdio.h>
     52  1.1      cgd 
     53  1.1      cgd #define	BS	'\b'		/* backspace */
     54  1.1      cgd #define	TAB	'\t'		/* tab */
     55  1.1      cgd #define	SPACE	' '		/* space */
     56  1.1      cgd #define	NL	'\n'		/* newline */
     57  1.1      cgd #define	CR	'\r'		/* carriage return */
     58  1.1      cgd #define	ESC	'\033'		/* escape */
     59  1.1      cgd #define	SI	'\017'		/* shift in to normal character set */
     60  1.1      cgd #define	SO	'\016'		/* shift out to alternate character set */
     61  1.1      cgd #define	VT	'\013'		/* vertical tab (aka reverse line feed) */
     62  1.1      cgd #define	RLF	'\007'		/* ESC-07 reverse line feed */
     63  1.1      cgd #define	RHLF	'\010'		/* ESC-010 reverse half-line feed */
     64  1.1      cgd #define	FHLF	'\011'		/* ESC-011 forward half-line feed */
     65  1.1      cgd 
     66  1.1      cgd /* build up at least this many lines before flushing them out */
     67  1.1      cgd #define	BUFFER_MARGIN		32
     68  1.1      cgd 
     69  1.1      cgd typedef char CSET;
     70  1.1      cgd 
     71  1.1      cgd typedef struct char_str {
     72  1.1      cgd #define	CS_NORMAL	1
     73  1.1      cgd #define	CS_ALTERNATE	2
     74  1.1      cgd 	short		c_column;	/* column character is in */
     75  1.1      cgd 	CSET		c_set;		/* character set (currently only 2) */
     76  1.1      cgd 	char		c_char;		/* character in question */
     77  1.1      cgd } CHAR;
     78  1.1      cgd 
     79  1.1      cgd typedef struct line_str LINE;
     80  1.1      cgd struct line_str {
     81  1.1      cgd 	CHAR	*l_line;		/* characters on the line */
     82  1.1      cgd 	LINE	*l_prev;		/* previous line */
     83  1.1      cgd 	LINE	*l_next;		/* next line */
     84  1.1      cgd 	int	l_lsize;		/* allocated sizeof l_line */
     85  1.1      cgd 	int	l_line_len;		/* strlen(l_line) */
     86  1.1      cgd 	int	l_needs_sort;		/* set if chars went in out of order */
     87  1.1      cgd 	int	l_max_col;		/* max column in the line */
     88  1.1      cgd };
     89  1.1      cgd 
     90  1.1      cgd LINE *alloc_line();
     91  1.1      cgd void *xmalloc();
     92  1.1      cgd 
     93  1.1      cgd CSET last_set;			/* char_set of last char printed */
     94  1.1      cgd LINE *lines;
     95  1.1      cgd int compress_spaces;		/* if doing space -> tab conversion */
     96  1.1      cgd int fine;			/* if `fine' resolution (half lines) */
     97  1.1      cgd int max_bufd_lines;		/* max # lines to keep in memory */
     98  1.1      cgd int nblank_lines;		/* # blanks after last flushed line */
     99  1.1      cgd int no_backspaces;		/* if not to output any backspaces */
    100  1.1      cgd 
    101  1.1      cgd #define	PUTC(ch) \
    102  1.1      cgd 	if (putchar(ch) == EOF) \
    103  1.1      cgd 		wrerr();
    104  1.1      cgd 
    105  1.1      cgd main(argc, argv)
    106  1.1      cgd 	int argc;
    107  1.1      cgd 	char **argv;
    108  1.1      cgd {
    109  1.1      cgd 	extern int optind;
    110  1.1      cgd 	extern char *optarg;
    111  1.1      cgd 	register int ch;
    112  1.1      cgd 	CHAR *c;
    113  1.1      cgd 	CSET cur_set;			/* current character set */
    114  1.1      cgd 	LINE *l;			/* current line */
    115  1.1      cgd 	int extra_lines;		/* # of lines above first line */
    116  1.1      cgd 	int cur_col;			/* current column */
    117  1.1      cgd 	int cur_line;			/* line number of current position */
    118  1.1      cgd 	int max_line;			/* max value of cur_line */
    119  1.1      cgd 	int this_line;			/* line l points to */
    120  1.1      cgd 	int nflushd_lines;		/* number of lines that were flushed */
    121  1.1      cgd 	int adjust, opt, warned;
    122  1.1      cgd 
    123  1.1      cgd 	max_bufd_lines = 128;
    124  1.1      cgd 	compress_spaces = 1;		/* compress spaces into tabs */
    125  1.1      cgd 	while ((opt = getopt(argc, argv, "bfhl:x")) != EOF)
    126  1.1      cgd 		switch (opt) {
    127  1.1      cgd 		case 'b':		/* do not output backspaces */
    128  1.1      cgd 			no_backspaces = 1;
    129  1.1      cgd 			break;
    130  1.1      cgd 		case 'f':		/* allow half forward line feeds */
    131  1.1      cgd 			fine = 1;
    132  1.1      cgd 			break;
    133  1.1      cgd 		case 'h':		/* compress spaces into tabs */
    134  1.1      cgd 			compress_spaces = 1;
    135  1.1      cgd 			break;
    136  1.1      cgd 		case 'l':		/* buffered line count */
    137  1.1      cgd 			if ((max_bufd_lines = atoi(optarg)) <= 0) {
    138  1.1      cgd 				(void)fprintf(stderr,
    139  1.1      cgd 				    "col: bad -l argument %s.\n", optarg);
    140  1.1      cgd 				exit(1);
    141  1.1      cgd 			}
    142  1.1      cgd 			break;
    143  1.1      cgd 		case 'x':		/* do not compress spaces into tabs */
    144  1.1      cgd 			compress_spaces = 0;
    145  1.1      cgd 			break;
    146  1.1      cgd 		case '?':
    147  1.1      cgd 		default:
    148  1.1      cgd 			usage();
    149  1.1      cgd 		}
    150  1.1      cgd 
    151  1.1      cgd 	if (optind != argc)
    152  1.1      cgd 		usage();
    153  1.1      cgd 
    154  1.1      cgd 	/* this value is in half lines */
    155  1.1      cgd 	max_bufd_lines *= 2;
    156  1.1      cgd 
    157  1.1      cgd 	adjust = cur_col = extra_lines = warned = 0;
    158  1.1      cgd 	cur_line = max_line = nflushd_lines = this_line = 0;
    159  1.1      cgd 	cur_set = last_set = CS_NORMAL;
    160  1.1      cgd 	lines = l = alloc_line();
    161  1.1      cgd 
    162  1.1      cgd 	while ((ch = getchar()) != EOF) {
    163  1.1      cgd 		if (!isgraph(ch)) {
    164  1.1      cgd 			switch (ch) {
    165  1.1      cgd 			case BS:		/* can't go back further */
    166  1.1      cgd 				if (cur_col == 0)
    167  1.1      cgd 					continue;
    168  1.1      cgd 				--cur_col;
    169  1.1      cgd 				continue;
    170  1.1      cgd 			case CR:
    171  1.1      cgd 				cur_col = 0;
    172  1.1      cgd 				continue;
    173  1.1      cgd 			case ESC:		/* just ignore EOF */
    174  1.1      cgd 				switch(getchar()) {
    175  1.1      cgd 				case RLF:
    176  1.1      cgd 					cur_line -= 2;
    177  1.1      cgd 					break;
    178  1.1      cgd 				case RHLF:
    179  1.1      cgd 					cur_line--;
    180  1.1      cgd 					break;
    181  1.1      cgd 				case FHLF:
    182  1.1      cgd 					cur_line++;
    183  1.1      cgd 					if (cur_line > max_line)
    184  1.1      cgd 						max_line = cur_line;
    185  1.1      cgd 				}
    186  1.1      cgd 				continue;
    187  1.1      cgd 			case NL:
    188  1.1      cgd 				cur_line += 2;
    189  1.1      cgd 				if (cur_line > max_line)
    190  1.1      cgd 					max_line = cur_line;
    191  1.1      cgd 				cur_col = 0;
    192  1.1      cgd 				continue;
    193  1.1      cgd 			case SPACE:
    194  1.1      cgd 				++cur_col;
    195  1.1      cgd 				continue;
    196  1.1      cgd 			case SI:
    197  1.1      cgd 				cur_set = CS_NORMAL;
    198  1.1      cgd 				continue;
    199  1.1      cgd 			case SO:
    200  1.1      cgd 				cur_set = CS_ALTERNATE;
    201  1.1      cgd 				continue;
    202  1.1      cgd 			case TAB:		/* adjust column */
    203  1.1      cgd 				cur_col |= 7;
    204  1.1      cgd 				++cur_col;
    205  1.1      cgd 				continue;
    206  1.1      cgd 			case VT:
    207  1.1      cgd 				cur_line -= 2;
    208  1.1      cgd 				continue;
    209  1.1      cgd 			}
    210  1.1      cgd 			continue;
    211  1.1      cgd 		}
    212  1.1      cgd 
    213  1.1      cgd 		/* Must stuff ch in a line - are we at the right one? */
    214  1.1      cgd 		if (cur_line != this_line - adjust) {
    215  1.1      cgd 			LINE *lnew;
    216  1.1      cgd 			int nmove;
    217  1.1      cgd 
    218  1.1      cgd 			adjust = 0;
    219  1.1      cgd 			nmove = cur_line - this_line;
    220  1.1      cgd 			if (!fine) {
    221  1.1      cgd 				/* round up to next line */
    222  1.1      cgd 				if (cur_line & 1) {
    223  1.1      cgd 					adjust = 1;
    224  1.1      cgd 					nmove++;
    225  1.1      cgd 				}
    226  1.1      cgd 			}
    227  1.1      cgd 			if (nmove < 0) {
    228  1.1      cgd 				for (; nmove < 0 && l->l_prev; nmove++)
    229  1.1      cgd 					l = l->l_prev;
    230  1.1      cgd 				if (nmove) {
    231  1.1      cgd 					if (nflushd_lines == 0) {
    232  1.1      cgd 						/*
    233  1.1      cgd 						 * Allow backup past first
    234  1.1      cgd 						 * line if nothing has been
    235  1.1      cgd 						 * flushed yet.
    236  1.1      cgd 						 */
    237  1.1      cgd 						for (; nmove < 0; nmove++) {
    238  1.1      cgd 							lnew = alloc_line();
    239  1.1      cgd 							l->l_prev = lnew;
    240  1.1      cgd 							lnew->l_next = l;
    241  1.1      cgd 							l = lines = lnew;
    242  1.1      cgd 							extra_lines++;
    243  1.1      cgd 						}
    244  1.1      cgd 					} else {
    245  1.1      cgd 						if (!warned++)
    246  1.1      cgd 							warn(cur_line);
    247  1.1      cgd 						cur_line -= nmove;
    248  1.1      cgd 					}
    249  1.1      cgd 				}
    250  1.1      cgd 			} else {
    251  1.1      cgd 				/* may need to allocate here */
    252  1.1      cgd 				for (; nmove > 0 && l->l_next; nmove--)
    253  1.1      cgd 					l = l->l_next;
    254  1.1      cgd 				for (; nmove > 0; nmove--) {
    255  1.1      cgd 					lnew = alloc_line();
    256  1.1      cgd 					lnew->l_prev = l;
    257  1.1      cgd 					l->l_next = lnew;
    258  1.1      cgd 					l = lnew;
    259  1.1      cgd 				}
    260  1.1      cgd 			}
    261  1.1      cgd 			this_line = cur_line + adjust;
    262  1.1      cgd 			nmove = this_line - nflushd_lines;
    263  1.1      cgd 			if (nmove >= max_bufd_lines + BUFFER_MARGIN) {
    264  1.1      cgd 				nflushd_lines += nmove - max_bufd_lines;
    265  1.1      cgd 				flush_lines(nmove - max_bufd_lines);
    266  1.1      cgd 			}
    267  1.1      cgd 		}
    268  1.1      cgd 		/* grow line's buffer? */
    269  1.1      cgd 		if (l->l_line_len + 1 >= l->l_lsize) {
    270  1.1      cgd 			int need;
    271  1.1      cgd 
    272  1.1      cgd 			need = l->l_lsize ? l->l_lsize * 2 : 90;
    273  1.1      cgd 			l->l_line = (CHAR *)xmalloc((void *) l->l_line,
    274  1.1      cgd 			    (unsigned) need * sizeof(CHAR));
    275  1.1      cgd 			l->l_lsize = need;
    276  1.1      cgd 		}
    277  1.1      cgd 		c = &l->l_line[l->l_line_len++];
    278  1.1      cgd 		c->c_char = ch;
    279  1.1      cgd 		c->c_set = cur_set;
    280  1.1      cgd 		c->c_column = cur_col;
    281  1.1      cgd 		/*
    282  1.1      cgd 		 * If things are put in out of order, they will need sorting
    283  1.1      cgd 		 * when it is flushed.
    284  1.1      cgd 		 */
    285  1.1      cgd 		if (cur_col < l->l_max_col)
    286  1.1      cgd 			l->l_needs_sort = 1;
    287  1.1      cgd 		else
    288  1.1      cgd 			l->l_max_col = cur_col;
    289  1.1      cgd 		cur_col++;
    290  1.1      cgd 	}
    291  1.1      cgd 	/* goto the last line that had a character on it */
    292  1.1      cgd 	for (; l->l_next; l = l->l_next)
    293  1.1      cgd 		this_line++;
    294  1.1      cgd 	flush_lines(this_line - nflushd_lines + extra_lines + 1);
    295  1.1      cgd 
    296  1.1      cgd 	/* make sure we leave things in a sane state */
    297  1.1      cgd 	if (last_set != CS_NORMAL)
    298  1.1      cgd 		PUTC('\017');
    299  1.1      cgd 
    300  1.1      cgd 	/* flush out the last few blank lines */
    301  1.1      cgd 	nblank_lines = max_line - this_line;
    302  1.1      cgd 	if (max_line & 1)
    303  1.1      cgd 		nblank_lines++;
    304  1.1      cgd 	else if (!nblank_lines)
    305  1.1      cgd 		/* missing a \n on the last line? */
    306  1.1      cgd 		nblank_lines = 2;
    307  1.1      cgd 	flush_blanks();
    308  1.1      cgd 	exit(0);
    309  1.1      cgd }
    310  1.1      cgd 
    311  1.1      cgd flush_lines(nflush)
    312  1.1      cgd 	int nflush;
    313  1.1      cgd {
    314  1.1      cgd 	LINE *l;
    315  1.1      cgd 
    316  1.1      cgd 	while (--nflush >= 0) {
    317  1.1      cgd 		l = lines;
    318  1.1      cgd 		lines = l->l_next;
    319  1.1      cgd 		if (l->l_line) {
    320  1.1      cgd 			flush_blanks();
    321  1.1      cgd 			flush_line(l);
    322  1.1      cgd 		}
    323  1.1      cgd 		nblank_lines++;
    324  1.1      cgd 		if (l->l_line)
    325  1.1      cgd 			(void)free((void *)l->l_line);
    326  1.1      cgd 		free_line(l);
    327  1.1      cgd 	}
    328  1.1      cgd 	if (lines)
    329  1.1      cgd 		lines->l_prev = NULL;
    330  1.1      cgd }
    331  1.1      cgd 
    332  1.1      cgd /*
    333  1.1      cgd  * Print a number of newline/half newlines.  If fine flag is set, nblank_lines
    334  1.1      cgd  * is the number of half line feeds, otherwise it is the number of whole line
    335  1.1      cgd  * feeds.
    336  1.1      cgd  */
    337  1.1      cgd flush_blanks()
    338  1.1      cgd {
    339  1.1      cgd 	int half, i, nb;
    340  1.1      cgd 
    341  1.1      cgd 	half = 0;
    342  1.1      cgd 	nb = nblank_lines;
    343  1.1      cgd 	if (nb & 1) {
    344  1.1      cgd 		if (fine)
    345  1.1      cgd 			half = 1;
    346  1.1      cgd 		else
    347  1.1      cgd 			nb++;
    348  1.1      cgd 	}
    349  1.1      cgd 	nb /= 2;
    350  1.1      cgd 	for (i = nb; --i >= 0;)
    351  1.1      cgd 		PUTC('\n');
    352  1.1      cgd 	if (half) {
    353  1.1      cgd 		PUTC('\033');
    354  1.1      cgd 		PUTC('9');
    355  1.1      cgd 		if (!nb)
    356  1.1      cgd 			PUTC('\r');
    357  1.1      cgd 	}
    358  1.1      cgd 	nblank_lines = 0;
    359  1.1      cgd }
    360  1.1      cgd 
    361  1.1      cgd /*
    362  1.1      cgd  * Write a line to stdout taking care of space to tab conversion (-h flag)
    363  1.1      cgd  * and character set shifts.
    364  1.1      cgd  */
    365  1.1      cgd flush_line(l)
    366  1.1      cgd 	LINE *l;
    367  1.1      cgd {
    368  1.1      cgd 	CHAR *c, *endc;
    369  1.1      cgd 	int nchars, last_col, this_col;
    370  1.1      cgd 
    371  1.1      cgd 	last_col = 0;
    372  1.1      cgd 	nchars = l->l_line_len;
    373  1.1      cgd 
    374  1.1      cgd 	if (l->l_needs_sort) {
    375  1.1      cgd 		static CHAR *sorted;
    376  1.1      cgd 		static int count_size, *count, i, save, sorted_size, tot;
    377  1.1      cgd 
    378  1.1      cgd 		/*
    379  1.1      cgd 		 * Do an O(n) sort on l->l_line by column being careful to
    380  1.1      cgd 		 * preserve the order of characters in the same column.
    381  1.1      cgd 		 */
    382  1.1      cgd 		if (l->l_lsize > sorted_size) {
    383  1.1      cgd 			sorted_size = l->l_lsize;
    384  1.1      cgd 			sorted = (CHAR *)xmalloc((void *)sorted,
    385  1.1      cgd 			    (unsigned)sizeof(CHAR) * sorted_size);
    386  1.1      cgd 		}
    387  1.1      cgd 		if (l->l_max_col >= count_size) {
    388  1.1      cgd 			count_size = l->l_max_col + 1;
    389  1.1      cgd 			count = (int *)xmalloc((void *)count,
    390  1.1      cgd 			    (unsigned)sizeof(int) * count_size);
    391  1.1      cgd 		}
    392  1.1      cgd 		bzero((char *)count, sizeof(int) * l->l_max_col + 1);
    393  1.1      cgd 		for (i = nchars, c = l->l_line; --i >= 0; c++)
    394  1.1      cgd 			count[c->c_column]++;
    395  1.1      cgd 
    396  1.1      cgd 		/*
    397  1.1      cgd 		 * calculate running total (shifted down by 1) to use as
    398  1.1      cgd 		 * indices into new line.
    399  1.1      cgd 		 */
    400  1.1      cgd 		for (tot = 0, i = 0; i <= l->l_max_col; i++) {
    401  1.1      cgd 			save = count[i];
    402  1.1      cgd 			count[i] = tot;
    403  1.1      cgd 			tot += save;
    404  1.1      cgd 		}
    405  1.1      cgd 
    406  1.1      cgd 		for (i = nchars, c = l->l_line; --i >= 0; c++)
    407  1.1      cgd 			sorted[count[c->c_column]++] = *c;
    408  1.1      cgd 		c = sorted;
    409  1.1      cgd 	} else
    410  1.1      cgd 		c = l->l_line;
    411  1.1      cgd 	while (nchars > 0) {
    412  1.1      cgd 		this_col = c->c_column;
    413  1.1      cgd 		endc = c;
    414  1.1      cgd 		do {
    415  1.1      cgd 			++endc;
    416  1.1      cgd 		} while (--nchars > 0 && this_col == endc->c_column);
    417  1.1      cgd 
    418  1.1      cgd 		/* if -b only print last character */
    419  1.1      cgd 		if (no_backspaces)
    420  1.1      cgd 			c = endc - 1;
    421  1.1      cgd 
    422  1.1      cgd 		if (this_col > last_col) {
    423  1.1      cgd 			int nspace = this_col - last_col;
    424  1.1      cgd 
    425  1.1      cgd 			if (compress_spaces && nspace > 1) {
    426  1.1      cgd 				int ntabs;
    427  1.1      cgd 
    428  1.1      cgd 				ntabs = this_col / 8 - last_col / 8;
    429  1.1      cgd 				nspace -= ntabs * 8;
    430  1.1      cgd 				while (--ntabs >= 0)
    431  1.1      cgd 					PUTC('\t');
    432  1.1      cgd 			}
    433  1.1      cgd 			while (--nspace >= 0)
    434  1.1      cgd 				PUTC(' ');
    435  1.1      cgd 			last_col = this_col;
    436  1.1      cgd 		}
    437  1.1      cgd 		last_col++;
    438  1.1      cgd 
    439  1.1      cgd 		for (;;) {
    440  1.1      cgd 			if (c->c_set != last_set) {
    441  1.1      cgd 				switch (c->c_set) {
    442  1.1      cgd 				case CS_NORMAL:
    443  1.1      cgd 					PUTC('\017');
    444  1.1      cgd 					break;
    445  1.1      cgd 				case CS_ALTERNATE:
    446  1.1      cgd 					PUTC('\016');
    447  1.1      cgd 				}
    448  1.1      cgd 				last_set = c->c_set;
    449  1.1      cgd 			}
    450  1.1      cgd 			PUTC(c->c_char);
    451  1.1      cgd 			if (++c >= endc)
    452  1.1      cgd 				break;
    453  1.1      cgd 			PUTC('\b');
    454  1.1      cgd 		}
    455  1.1      cgd 	}
    456  1.1      cgd }
    457  1.1      cgd 
    458  1.1      cgd #define	NALLOC 64
    459  1.1      cgd 
    460  1.1      cgd static LINE *line_freelist;
    461  1.1      cgd 
    462  1.1      cgd LINE *
    463  1.1      cgd alloc_line()
    464  1.1      cgd {
    465  1.1      cgd 	LINE *l;
    466  1.1      cgd 	int i;
    467  1.1      cgd 
    468  1.1      cgd 	if (!line_freelist) {
    469  1.1      cgd 		l = (LINE *)xmalloc((void *)NULL, sizeof(LINE) * NALLOC);
    470  1.1      cgd 		line_freelist = l;
    471  1.1      cgd 		for (i = 1; i < NALLOC; i++, l++)
    472  1.1      cgd 			l->l_next = l + 1;
    473  1.1      cgd 		l->l_next = NULL;
    474  1.1      cgd 	}
    475  1.1      cgd 	l = line_freelist;
    476  1.1      cgd 	line_freelist = l->l_next;
    477  1.1      cgd 
    478  1.1      cgd 	bzero(l, sizeof(LINE));
    479  1.1      cgd 	return(l);
    480  1.1      cgd }
    481  1.1      cgd 
    482  1.1      cgd free_line(l)
    483  1.1      cgd 	LINE *l;
    484  1.1      cgd {
    485  1.1      cgd 	l->l_next = line_freelist;
    486  1.1      cgd 	line_freelist = l;
    487  1.1      cgd }
    488  1.1      cgd 
    489  1.1      cgd void *
    490  1.1      cgd xmalloc(p, size)
    491  1.1      cgd 	void *p;
    492  1.1      cgd 	size_t size;
    493  1.1      cgd {
    494  1.1      cgd 	if (!(p = (void *)realloc(p, size))) {
    495  1.1      cgd 		(void)fprintf(stderr, "col: %s.\n", strerror(ENOMEM));
    496  1.1      cgd 		exit(1);
    497  1.1      cgd 	}
    498  1.1      cgd 	return(p);
    499  1.1      cgd }
    500  1.1      cgd 
    501  1.1      cgd usage()
    502  1.1      cgd {
    503  1.1      cgd 	(void)fprintf(stderr, "usage: col [-bfx] [-l nline]\n");
    504  1.1      cgd 	exit(1);
    505  1.1      cgd }
    506  1.1      cgd 
    507  1.1      cgd wrerr()
    508  1.1      cgd {
    509  1.1      cgd 	(void)fprintf(stderr, "col: write error.\n");
    510  1.1      cgd 	exit(1);
    511  1.1      cgd }
    512  1.1      cgd 
    513  1.1      cgd warn(line)
    514  1.1      cgd 	int line;
    515  1.1      cgd {
    516  1.1      cgd 	(void)fprintf(stderr,
    517  1.1      cgd 	    "col: warning: can't back up %s.\n", line < 0 ?
    518  1.1      cgd 	    "past first line" : "-- line already flushed");
    519  1.1      cgd }
    520