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