xref: /csrg-svn/usr.bin/col/col.c (revision 69228)
142270Sbostic /*-
266652Spendry  * Copyright (c) 1990, 1993, 1994
361942Sbostic  *	The Regents of the University of California.  All rights reserved.
442270Sbostic  *
542270Sbostic  * This code is derived from software contributed to Berkeley by
642270Sbostic  * Michael Rendell of the Memorial University of Newfoundland.
742270Sbostic  *
842270Sbostic  * %sccs.include.redist.c%
942270Sbostic  */
10982Sbill 
1142270Sbostic #ifndef lint
1261942Sbostic static char copyright[] =
1366652Spendry "@(#) Copyright (c) 1990, 1993, 1994\n\
1461942Sbostic 	The Regents of the University of California.  All rights reserved.\n";
1542270Sbostic #endif /* not lint */
16982Sbill 
1742270Sbostic #ifndef lint
18*69228Sbostic static char sccsid[] = "@(#)col.c	8.5 (Berkeley) 05/04/95";
1942270Sbostic #endif /* not lint */
20982Sbill 
2142270Sbostic #include <ctype.h>
2266590Spendry #include <err.h>
2342270Sbostic #include <string.h>
2442270Sbostic #include <stdio.h>
2566590Spendry #include <stdlib.h>
26*69228Sbostic #include <unistd.h>
27982Sbill 
2842270Sbostic #define	BS	'\b'		/* backspace */
2942270Sbostic #define	TAB	'\t'		/* tab */
3042270Sbostic #define	SPACE	' '		/* space */
3142270Sbostic #define	NL	'\n'		/* newline */
3242270Sbostic #define	CR	'\r'		/* carriage return */
3342305Sbostic #define	ESC	'\033'		/* escape */
3442305Sbostic #define	SI	'\017'		/* shift in to normal character set */
3542305Sbostic #define	SO	'\016'		/* shift out to alternate character set */
3642305Sbostic #define	VT	'\013'		/* vertical tab (aka reverse line feed) */
3746233Storek #define	RLF	'\007'		/* ESC-07 reverse line feed */
3846233Storek #define	RHLF	'\010'		/* ESC-010 reverse half-line feed */
3946233Storek #define	FHLF	'\011'		/* ESC-011 forward half-line feed */
40982Sbill 
4142270Sbostic /* build up at least this many lines before flushing them out */
4242270Sbostic #define	BUFFER_MARGIN		32
43982Sbill 
4442270Sbostic typedef char CSET;
45982Sbill 
4642270Sbostic typedef struct char_str {
4742270Sbostic #define	CS_NORMAL	1
4842270Sbostic #define	CS_ALTERNATE	2
4942270Sbostic 	short		c_column;	/* column character is in */
5042270Sbostic 	CSET		c_set;		/* character set (currently only 2) */
5142270Sbostic 	char		c_char;		/* character in question */
5242270Sbostic } CHAR;
53982Sbill 
5442270Sbostic typedef struct line_str LINE;
5542270Sbostic struct line_str {
5642270Sbostic 	CHAR	*l_line;		/* characters on the line */
5742270Sbostic 	LINE	*l_prev;		/* previous line */
5842270Sbostic 	LINE	*l_next;		/* next line */
5942270Sbostic 	int	l_lsize;		/* allocated sizeof l_line */
6042270Sbostic 	int	l_line_len;		/* strlen(l_line) */
6142270Sbostic 	int	l_needs_sort;		/* set if chars went in out of order */
6242270Sbostic 	int	l_max_col;		/* max column in the line */
6342270Sbostic };
64982Sbill 
6566590Spendry LINE   *alloc_line __P((void));
6666590Spendry void	dowarn __P((int));
6766590Spendry void	flush_line __P((LINE *));
6866590Spendry void	flush_lines __P((int));
6966590Spendry void	flush_blanks __P((void));
7066590Spendry void	free_line __P((LINE *));
7166590Spendry void	usage __P((void));
7266590Spendry void	wrerr __P((void));
7366590Spendry void   *xmalloc __P((void *, size_t));
74982Sbill 
7566590Spendry CSET	last_set;		/* char_set of last char printed */
7666590Spendry LINE   *lines;
7766590Spendry int	compress_spaces;	/* if doing space -> tab conversion */
7866590Spendry int	fine;			/* if `fine' resolution (half lines) */
7966590Spendry int	max_bufd_lines;		/* max # lines to keep in memory */
8066590Spendry int	nblank_lines;		/* # blanks after last flushed line */
8166590Spendry int	no_backspaces;		/* if not to output any backspaces */
82982Sbill 
8342270Sbostic #define	PUTC(ch) \
8442270Sbostic 	if (putchar(ch) == EOF) \
8542270Sbostic 		wrerr();
86982Sbill 
8766590Spendry int
main(argc,argv)8842270Sbostic main(argc, argv)
8942270Sbostic 	int argc;
9042270Sbostic 	char **argv;
9142270Sbostic {
9266590Spendry 	int ch;
9342270Sbostic 	CHAR *c;
9442270Sbostic 	CSET cur_set;			/* current character set */
9542270Sbostic 	LINE *l;			/* current line */
9642270Sbostic 	int extra_lines;		/* # of lines above first line */
9742270Sbostic 	int cur_col;			/* current column */
9842270Sbostic 	int cur_line;			/* line number of current position */
9942270Sbostic 	int max_line;			/* max value of cur_line */
10042270Sbostic 	int this_line;			/* line l points to */
10142270Sbostic 	int nflushd_lines;		/* number of lines that were flushed */
10242270Sbostic 	int adjust, opt, warned;
103982Sbill 
10442270Sbostic 	max_bufd_lines = 128;
10542270Sbostic 	compress_spaces = 1;		/* compress spaces into tabs */
10642270Sbostic 	while ((opt = getopt(argc, argv, "bfhl:x")) != EOF)
10742270Sbostic 		switch (opt) {
10842270Sbostic 		case 'b':		/* do not output backspaces */
10942270Sbostic 			no_backspaces = 1;
11042270Sbostic 			break;
11142270Sbostic 		case 'f':		/* allow half forward line feeds */
11242270Sbostic 			fine = 1;
11342270Sbostic 			break;
11442270Sbostic 		case 'h':		/* compress spaces into tabs */
11542270Sbostic 			compress_spaces = 1;
11642270Sbostic 			break;
11742270Sbostic 		case 'l':		/* buffered line count */
11842270Sbostic 			if ((max_bufd_lines = atoi(optarg)) <= 0) {
11942270Sbostic 				(void)fprintf(stderr,
12042270Sbostic 				    "col: bad -l argument %s.\n", optarg);
12142270Sbostic 				exit(1);
122982Sbill 			}
12342270Sbostic 			break;
12442270Sbostic 		case 'x':		/* do not compress spaces into tabs */
12542270Sbostic 			compress_spaces = 0;
12642270Sbostic 			break;
12742270Sbostic 		case '?':
12842270Sbostic 		default:
12942270Sbostic 			usage();
13042270Sbostic 		}
131982Sbill 
13242270Sbostic 	if (optind != argc)
13342270Sbostic 		usage();
134982Sbill 
13542270Sbostic 	/* this value is in half lines */
13642270Sbostic 	max_bufd_lines *= 2;
137982Sbill 
13842270Sbostic 	adjust = cur_col = extra_lines = warned = 0;
13942270Sbostic 	cur_line = max_line = nflushd_lines = this_line = 0;
14042270Sbostic 	cur_set = last_set = CS_NORMAL;
14142270Sbostic 	lines = l = alloc_line();
142982Sbill 
14342270Sbostic 	while ((ch = getchar()) != EOF) {
14442270Sbostic 		if (!isgraph(ch)) {
14542270Sbostic 			switch (ch) {
14642270Sbostic 			case BS:		/* can't go back further */
14742270Sbostic 				if (cur_col == 0)
14842270Sbostic 					continue;
14942270Sbostic 				--cur_col;
15042270Sbostic 				continue;
15142270Sbostic 			case CR:
15242270Sbostic 				cur_col = 0;
15342270Sbostic 				continue;
15442270Sbostic 			case ESC:		/* just ignore EOF */
15542270Sbostic 				switch(getchar()) {
15642270Sbostic 				case RLF:
15742270Sbostic 					cur_line -= 2;
15842270Sbostic 					break;
15942270Sbostic 				case RHLF:
16042270Sbostic 					cur_line--;
16142270Sbostic 					break;
16242270Sbostic 				case FHLF:
16342270Sbostic 					cur_line++;
16442270Sbostic 					if (cur_line > max_line)
16542270Sbostic 						max_line = cur_line;
16642270Sbostic 				}
16742270Sbostic 				continue;
16842270Sbostic 			case NL:
16942270Sbostic 				cur_line += 2;
17042270Sbostic 				if (cur_line > max_line)
17142270Sbostic 					max_line = cur_line;
17242270Sbostic 				cur_col = 0;
17342270Sbostic 				continue;
17442270Sbostic 			case SPACE:
17542270Sbostic 				++cur_col;
17642270Sbostic 				continue;
17742270Sbostic 			case SI:
17842270Sbostic 				cur_set = CS_NORMAL;
17942270Sbostic 				continue;
18042270Sbostic 			case SO:
18142270Sbostic 				cur_set = CS_ALTERNATE;
18242270Sbostic 				continue;
18342270Sbostic 			case TAB:		/* adjust column */
18442270Sbostic 				cur_col |= 7;
18542270Sbostic 				++cur_col;
18642270Sbostic 				continue;
18742270Sbostic 			case VT:
18842270Sbostic 				cur_line -= 2;
18942270Sbostic 				continue;
19042270Sbostic 			}
191982Sbill 			continue;
19242270Sbostic 		}
193982Sbill 
19442270Sbostic 		/* Must stuff ch in a line - are we at the right one? */
19542270Sbostic 		if (cur_line != this_line - adjust) {
19642270Sbostic 			LINE *lnew;
19742270Sbostic 			int nmove;
198982Sbill 
19942270Sbostic 			adjust = 0;
20042270Sbostic 			nmove = cur_line - this_line;
20142270Sbostic 			if (!fine) {
20242270Sbostic 				/* round up to next line */
20342270Sbostic 				if (cur_line & 1) {
20442270Sbostic 					adjust = 1;
20542270Sbostic 					nmove++;
20642270Sbostic 				}
207982Sbill 			}
20842270Sbostic 			if (nmove < 0) {
20942270Sbostic 				for (; nmove < 0 && l->l_prev; nmove++)
21042270Sbostic 					l = l->l_prev;
21142270Sbostic 				if (nmove) {
21242270Sbostic 					if (nflushd_lines == 0) {
21342270Sbostic 						/*
21442270Sbostic 						 * Allow backup past first
21542270Sbostic 						 * line if nothing has been
21642270Sbostic 						 * flushed yet.
21742270Sbostic 						 */
21842270Sbostic 						for (; nmove < 0; nmove++) {
21942270Sbostic 							lnew = alloc_line();
22042270Sbostic 							l->l_prev = lnew;
22142270Sbostic 							lnew->l_next = l;
22242270Sbostic 							l = lines = lnew;
22342270Sbostic 							extra_lines++;
22442270Sbostic 						}
22542270Sbostic 					} else {
22642270Sbostic 						if (!warned++)
22766590Spendry 							dowarn(cur_line);
22842270Sbostic 						cur_line -= nmove;
22942270Sbostic 					}
23042270Sbostic 				}
23142270Sbostic 			} else {
23242270Sbostic 				/* may need to allocate here */
23342270Sbostic 				for (; nmove > 0 && l->l_next; nmove--)
23442270Sbostic 					l = l->l_next;
23542270Sbostic 				for (; nmove > 0; nmove--) {
23642270Sbostic 					lnew = alloc_line();
23742270Sbostic 					lnew->l_prev = l;
23842270Sbostic 					l->l_next = lnew;
23942270Sbostic 					l = lnew;
24042270Sbostic 				}
24142270Sbostic 			}
24242270Sbostic 			this_line = cur_line + adjust;
24342270Sbostic 			nmove = this_line - nflushd_lines;
24442270Sbostic 			if (nmove >= max_bufd_lines + BUFFER_MARGIN) {
24542270Sbostic 				nflushd_lines += nmove - max_bufd_lines;
24642270Sbostic 				flush_lines(nmove - max_bufd_lines);
24742270Sbostic 			}
248982Sbill 		}
24942270Sbostic 		/* grow line's buffer? */
25042270Sbostic 		if (l->l_line_len + 1 >= l->l_lsize) {
25142270Sbostic 			int need;
25242270Sbostic 
25342270Sbostic 			need = l->l_lsize ? l->l_lsize * 2 : 90;
25442270Sbostic 			l->l_line = (CHAR *)xmalloc((void *) l->l_line,
25542270Sbostic 			    (unsigned) need * sizeof(CHAR));
25642270Sbostic 			l->l_lsize = need;
25742270Sbostic 		}
25842270Sbostic 		c = &l->l_line[l->l_line_len++];
25942270Sbostic 		c->c_char = ch;
26042270Sbostic 		c->c_set = cur_set;
26142270Sbostic 		c->c_column = cur_col;
26242270Sbostic 		/*
26342270Sbostic 		 * If things are put in out of order, they will need sorting
26442270Sbostic 		 * when it is flushed.
26542270Sbostic 		 */
26642270Sbostic 		if (cur_col < l->l_max_col)
26742270Sbostic 			l->l_needs_sort = 1;
26842270Sbostic 		else
26942270Sbostic 			l->l_max_col = cur_col;
27042270Sbostic 		cur_col++;
271982Sbill 	}
27269013Sbostic 	if (max_line == 0)
27369013Sbostic 		exit(0);	/* no lines, so just exit */
27469013Sbostic 
27542270Sbostic 	/* goto the last line that had a character on it */
27642270Sbostic 	for (; l->l_next; l = l->l_next)
27742270Sbostic 		this_line++;
27842270Sbostic 	flush_lines(this_line - nflushd_lines + extra_lines + 1);
279982Sbill 
28042270Sbostic 	/* make sure we leave things in a sane state */
28142270Sbostic 	if (last_set != CS_NORMAL)
28242270Sbostic 		PUTC('\017');
28342270Sbostic 
28442270Sbostic 	/* flush out the last few blank lines */
28542270Sbostic 	nblank_lines = max_line - this_line;
28642270Sbostic 	if (max_line & 1)
28742270Sbostic 		nblank_lines++;
28842270Sbostic 	else if (!nblank_lines)
28942270Sbostic 		/* missing a \n on the last line? */
29042270Sbostic 		nblank_lines = 2;
29142270Sbostic 	flush_blanks();
292982Sbill 	exit(0);
293982Sbill }
294982Sbill 
29566590Spendry void
flush_lines(nflush)29642270Sbostic flush_lines(nflush)
29742270Sbostic 	int nflush;
298982Sbill {
29942270Sbostic 	LINE *l;
300982Sbill 
30142270Sbostic 	while (--nflush >= 0) {
30242270Sbostic 		l = lines;
30342270Sbostic 		lines = l->l_next;
30442270Sbostic 		if (l->l_line) {
30542270Sbostic 			flush_blanks();
30642270Sbostic 			flush_line(l);
307982Sbill 		}
30842270Sbostic 		nblank_lines++;
30942270Sbostic 		if (l->l_line)
31042270Sbostic 			(void)free((void *)l->l_line);
31142270Sbostic 		free_line(l);
312982Sbill 	}
31342270Sbostic 	if (lines)
31442270Sbostic 		lines->l_prev = NULL;
315982Sbill }
316982Sbill 
31742270Sbostic /*
31842270Sbostic  * Print a number of newline/half newlines.  If fine flag is set, nblank_lines
31942270Sbostic  * is the number of half line feeds, otherwise it is the number of whole line
32042270Sbostic  * feeds.
32142270Sbostic  */
32266590Spendry void
flush_blanks()32342270Sbostic flush_blanks()
324982Sbill {
32542270Sbostic 	int half, i, nb;
326982Sbill 
32742270Sbostic 	half = 0;
32842270Sbostic 	nb = nblank_lines;
32942270Sbostic 	if (nb & 1) {
33042270Sbostic 		if (fine)
33142270Sbostic 			half = 1;
33242270Sbostic 		else
33342270Sbostic 			nb++;
334982Sbill 	}
33542270Sbostic 	nb /= 2;
33642270Sbostic 	for (i = nb; --i >= 0;)
33742270Sbostic 		PUTC('\n');
33842270Sbostic 	if (half) {
33942270Sbostic 		PUTC('\033');
34042270Sbostic 		PUTC('9');
34142270Sbostic 		if (!nb)
34242270Sbostic 			PUTC('\r');
34342270Sbostic 	}
34442270Sbostic 	nblank_lines = 0;
345982Sbill }
346982Sbill 
34742270Sbostic /*
34842270Sbostic  * Write a line to stdout taking care of space to tab conversion (-h flag)
34942270Sbostic  * and character set shifts.
35042270Sbostic  */
35166590Spendry void
flush_line(l)35242270Sbostic flush_line(l)
35342270Sbostic 	LINE *l;
354982Sbill {
35542270Sbostic 	CHAR *c, *endc;
35642270Sbostic 	int nchars, last_col, this_col;
357982Sbill 
35842270Sbostic 	last_col = 0;
35942270Sbostic 	nchars = l->l_line_len;
360982Sbill 
36142270Sbostic 	if (l->l_needs_sort) {
36242270Sbostic 		static CHAR *sorted;
36342270Sbostic 		static int count_size, *count, i, save, sorted_size, tot;
36442270Sbostic 
36542270Sbostic 		/*
36642270Sbostic 		 * Do an O(n) sort on l->l_line by column being careful to
36742270Sbostic 		 * preserve the order of characters in the same column.
36842270Sbostic 		 */
36942270Sbostic 		if (l->l_lsize > sorted_size) {
37042270Sbostic 			sorted_size = l->l_lsize;
37142270Sbostic 			sorted = (CHAR *)xmalloc((void *)sorted,
37242270Sbostic 			    (unsigned)sizeof(CHAR) * sorted_size);
373982Sbill 		}
37442270Sbostic 		if (l->l_max_col >= count_size) {
37542270Sbostic 			count_size = l->l_max_col + 1;
37642270Sbostic 			count = (int *)xmalloc((void *)count,
37742270Sbostic 			    (unsigned)sizeof(int) * count_size);
378982Sbill 		}
37966590Spendry 		memset((char *)count, 0, sizeof(int) * l->l_max_col + 1);
38042270Sbostic 		for (i = nchars, c = l->l_line; --i >= 0; c++)
38142270Sbostic 			count[c->c_column]++;
38242270Sbostic 
38342270Sbostic 		/*
38442270Sbostic 		 * calculate running total (shifted down by 1) to use as
38542270Sbostic 		 * indices into new line.
38642270Sbostic 		 */
38742270Sbostic 		for (tot = 0, i = 0; i <= l->l_max_col; i++) {
38842270Sbostic 			save = count[i];
38942270Sbostic 			count[i] = tot;
39042270Sbostic 			tot += save;
39142270Sbostic 		}
39242270Sbostic 
39342270Sbostic 		for (i = nchars, c = l->l_line; --i >= 0; c++)
39442270Sbostic 			sorted[count[c->c_column]++] = *c;
39542270Sbostic 		c = sorted;
39642270Sbostic 	} else
39742270Sbostic 		c = l->l_line;
39842270Sbostic 	while (nchars > 0) {
39942270Sbostic 		this_col = c->c_column;
40042270Sbostic 		endc = c;
40142270Sbostic 		do {
40242270Sbostic 			++endc;
40342270Sbostic 		} while (--nchars > 0 && this_col == endc->c_column);
40442270Sbostic 
40542270Sbostic 		/* if -b only print last character */
40642270Sbostic 		if (no_backspaces)
40742270Sbostic 			c = endc - 1;
40842270Sbostic 
40942270Sbostic 		if (this_col > last_col) {
41042270Sbostic 			int nspace = this_col - last_col;
41142270Sbostic 
41242270Sbostic 			if (compress_spaces && nspace > 1) {
41342270Sbostic 				int ntabs;
41442270Sbostic 
41542270Sbostic 				ntabs = this_col / 8 - last_col / 8;
41642270Sbostic 				nspace -= ntabs * 8;
41742270Sbostic 				while (--ntabs >= 0)
41842270Sbostic 					PUTC('\t');
41942270Sbostic 			}
42042270Sbostic 			while (--nspace >= 0)
42142270Sbostic 				PUTC(' ');
42242270Sbostic 			last_col = this_col;
42342270Sbostic 		}
42442270Sbostic 		last_col++;
42542270Sbostic 
42642270Sbostic 		for (;;) {
42742270Sbostic 			if (c->c_set != last_set) {
42842270Sbostic 				switch (c->c_set) {
42942270Sbostic 				case CS_NORMAL:
43042270Sbostic 					PUTC('\017');
43142270Sbostic 					break;
43242270Sbostic 				case CS_ALTERNATE:
43342270Sbostic 					PUTC('\016');
434982Sbill 				}
43542270Sbostic 				last_set = c->c_set;
436982Sbill 			}
43742270Sbostic 			PUTC(c->c_char);
43842270Sbostic 			if (++c >= endc)
439982Sbill 				break;
44042270Sbostic 			PUTC('\b');
441982Sbill 		}
442982Sbill 	}
443982Sbill }
444982Sbill 
44542270Sbostic #define	NALLOC 64
44642270Sbostic 
44742270Sbostic static LINE *line_freelist;
44842270Sbostic 
44942270Sbostic LINE *
alloc_line()45042270Sbostic alloc_line()
451982Sbill {
45242270Sbostic 	LINE *l;
45342270Sbostic 	int i;
45442270Sbostic 
45542270Sbostic 	if (!line_freelist) {
45642270Sbostic 		l = (LINE *)xmalloc((void *)NULL, sizeof(LINE) * NALLOC);
45742270Sbostic 		line_freelist = l;
45842270Sbostic 		for (i = 1; i < NALLOC; i++, l++)
45942270Sbostic 			l->l_next = l + 1;
46042270Sbostic 		l->l_next = NULL;
461982Sbill 	}
46242270Sbostic 	l = line_freelist;
46342270Sbostic 	line_freelist = l->l_next;
46442270Sbostic 
46566590Spendry 	memset(l, 0, sizeof(LINE));
46666590Spendry 	return (l);
467982Sbill }
468982Sbill 
46966590Spendry void
free_line(l)47042270Sbostic free_line(l)
47142270Sbostic 	LINE *l;
472982Sbill {
47366590Spendry 
47442270Sbostic 	l->l_next = line_freelist;
47542270Sbostic 	line_freelist = l;
47642270Sbostic }
47742270Sbostic 
47842270Sbostic void *
xmalloc(p,size)47942270Sbostic xmalloc(p, size)
48042270Sbostic 	void *p;
48142270Sbostic 	size_t size;
48242270Sbostic {
48366590Spendry 
48466590Spendry 	if (!(p = (void *)realloc(p, size)))
48566590Spendry 		err(1, NULL);
48666590Spendry 	return (p);
487982Sbill }
48842270Sbostic 
48966590Spendry void
usage()49042270Sbostic usage()
49142270Sbostic {
49266590Spendry 
49342270Sbostic 	(void)fprintf(stderr, "usage: col [-bfx] [-l nline]\n");
49442270Sbostic 	exit(1);
49542270Sbostic }
49642270Sbostic 
49766590Spendry void
wrerr()49842270Sbostic wrerr()
49942270Sbostic {
50066590Spendry 
50142270Sbostic 	(void)fprintf(stderr, "col: write error.\n");
50242270Sbostic 	exit(1);
50342270Sbostic }
50442270Sbostic 
50566590Spendry void
dowarn(line)50666590Spendry dowarn(line)
50742270Sbostic 	int line;
50842270Sbostic {
50966590Spendry 
51066590Spendry 	warnx("warning: can't back up %s",
51166590Spendry 		line < 0 ? "past first line" : "-- line already flushed");
51242270Sbostic }
513