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