xref: /freebsd/usr.bin/col/col.c (revision 0b8224d1cc9dc6c9778ba04a75b2c8d47e5d7481)
19b50d902SRodney W. Grimes /*-
28a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni  *
49b50d902SRodney W. Grimes  * Copyright (c) 1990, 1993, 1994
59b50d902SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
69b50d902SRodney W. Grimes  *
79b50d902SRodney W. Grimes  * This code is derived from software contributed to Berkeley by
89b50d902SRodney W. Grimes  * Michael Rendell of the Memorial University of Newfoundland.
99b50d902SRodney W. Grimes  *
109b50d902SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
119b50d902SRodney W. Grimes  * modification, are permitted provided that the following conditions
129b50d902SRodney W. Grimes  * are met:
139b50d902SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
149b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
159b50d902SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
169b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
179b50d902SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
18fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
199b50d902SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
209b50d902SRodney W. Grimes  *    without specific prior written permission.
219b50d902SRodney W. Grimes  *
229b50d902SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
239b50d902SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
249b50d902SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
259b50d902SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
269b50d902SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
279b50d902SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
289b50d902SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
299b50d902SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
309b50d902SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
319b50d902SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
329b50d902SRodney W. Grimes  * SUCH DAMAGE.
339b50d902SRodney W. Grimes  */
349b50d902SRodney W. Grimes 
35e5ea68e7SBaptiste Daroussin #include <sys/capsicum.h>
36e5ea68e7SBaptiste Daroussin 
37a4e3fc54SMariusz Zaborski #include <capsicum_helpers.h>
389b50d902SRodney W. Grimes #include <err.h>
39e5ea68e7SBaptiste Daroussin #include <errno.h>
40c3fae744STim J. Robbins #include <locale.h>
419b50d902SRodney W. Grimes #include <stdio.h>
429b50d902SRodney W. Grimes #include <stdlib.h>
43919480b7STim J. Robbins #include <string.h>
44e5ea68e7SBaptiste Daroussin #include <termios.h>
45df3f5d9dSPeter Wemm #include <unistd.h>
46c3fae744STim J. Robbins #include <wchar.h>
47c3fae744STim J. Robbins #include <wctype.h>
489b50d902SRodney W. Grimes 
499b50d902SRodney W. Grimes #define	BS	'\b'		/* backspace */
509b50d902SRodney W. Grimes #define	TAB	'\t'		/* tab */
519b50d902SRodney W. Grimes #define	SPACE	' '		/* space */
529b50d902SRodney W. Grimes #define	NL	'\n'		/* newline */
539b50d902SRodney W. Grimes #define	CR	'\r'		/* carriage return */
549b50d902SRodney W. Grimes #define	ESC	'\033'		/* escape */
559b50d902SRodney W. Grimes #define	SI	'\017'		/* shift in to normal character set */
569b50d902SRodney W. Grimes #define	SO	'\016'		/* shift out to alternate character set */
579b50d902SRodney W. Grimes #define	VT	'\013'		/* vertical tab (aka reverse line feed) */
58f6240da9SBaptiste Daroussin #define	RLF	'7'		/* ESC-7 reverse line feed */
59f6240da9SBaptiste Daroussin #define	RHLF	'8'		/* ESC-8 reverse half-line feed */
60f6240da9SBaptiste Daroussin #define	FHLF	'9'		/* ESC-9 forward half-line feed */
619b50d902SRodney W. Grimes 
629b50d902SRodney W. Grimes /* build up at least this many lines before flushing them out */
639b50d902SRodney W. Grimes #define	BUFFER_MARGIN		32
649b50d902SRodney W. Grimes 
659b50d902SRodney W. Grimes typedef char CSET;
669b50d902SRodney W. Grimes 
679b50d902SRodney W. Grimes typedef struct char_str {
689b50d902SRodney W. Grimes #define	CS_NORMAL	1
699b50d902SRodney W. Grimes #define	CS_ALTERNATE	2
709b50d902SRodney W. Grimes 	short		c_column;	/* column character is in */
719b50d902SRodney W. Grimes 	CSET		c_set;		/* character set (currently only 2) */
72c3fae744STim J. Robbins 	wchar_t		c_char;		/* character in question */
73c3fae744STim J. Robbins 	int		c_width;	/* character width */
749b50d902SRodney W. Grimes } CHAR;
759b50d902SRodney W. Grimes 
769b50d902SRodney W. Grimes typedef struct line_str LINE;
779b50d902SRodney W. Grimes struct line_str {
789b50d902SRodney W. Grimes 	CHAR	*l_line;		/* characters on the line */
799b50d902SRodney W. Grimes 	LINE	*l_prev;		/* previous line */
809b50d902SRodney W. Grimes 	LINE	*l_next;		/* next line */
819b50d902SRodney W. Grimes 	int	l_lsize;		/* allocated sizeof l_line */
829b50d902SRodney W. Grimes 	int	l_line_len;		/* strlen(l_line) */
839b50d902SRodney W. Grimes 	int	l_needs_sort;		/* set if chars went in out of order */
849b50d902SRodney W. Grimes 	int	l_max_col;		/* max column in the line */
859b50d902SRodney W. Grimes };
869b50d902SRodney W. Grimes 
87f23ed79bSBaptiste Daroussin static void	addto_lineno(int *, int);
88ab8d971cSEd Schouten static LINE	*alloc_line(void);
89ab8d971cSEd Schouten static void	dowarn(int);
90ab8d971cSEd Schouten static void	flush_line(LINE *);
91ab8d971cSEd Schouten static void	flush_lines(int);
92ab8d971cSEd Schouten static void	flush_blanks(void);
93ab8d971cSEd Schouten static void	free_line(LINE *);
94ab8d971cSEd Schouten static void	usage(void);
959b50d902SRodney W. Grimes 
96ab8d971cSEd Schouten static CSET	last_set;		/* char_set of last char printed */
97ab8d971cSEd Schouten static LINE	*lines;
98ab8d971cSEd Schouten static int	compress_spaces;	/* if doing space -> tab conversion */
99ab8d971cSEd Schouten static int	fine;			/* if `fine' resolution (half lines) */
100f23ed79bSBaptiste Daroussin static int	max_bufd_lines;		/* max # of half lines to keep in memory */
101ab8d971cSEd Schouten static int	nblank_lines;		/* # blanks after last flushed line */
102ab8d971cSEd Schouten static int	no_backspaces;		/* if not to output any backspaces */
103ab8d971cSEd Schouten static int	pass_unknown_seqs;	/* pass unknown control sequences */
1049b50d902SRodney W. Grimes 
1059b50d902SRodney W. Grimes #define	PUTC(ch) \
106f746e67cSBruce Evans 	do {					\
107c3fae744STim J. Robbins 		if (putwchar(ch) == WEOF)	\
108f34d0e74SMike Heffner 			errx(1, "write error");	\
109f746e67cSBruce Evans 	} while (0)
1109b50d902SRodney W. Grimes 
1119b50d902SRodney W. Grimes int
main(int argc,char ** argv)112f4ac32deSDavid Malone main(int argc, char **argv)
1139b50d902SRodney W. Grimes {
114c3fae744STim J. Robbins 	wint_t ch;
1159b50d902SRodney W. Grimes 	CHAR *c;
1169b50d902SRodney W. Grimes 	CSET cur_set;			/* current character set */
1179b50d902SRodney W. Grimes 	LINE *l;			/* current line */
1189b50d902SRodney W. Grimes 	int extra_lines;		/* # of lines above first line */
1199b50d902SRodney W. Grimes 	int cur_col;			/* current column */
1209b50d902SRodney W. Grimes 	int cur_line;			/* line number of current position */
1219b50d902SRodney W. Grimes 	int max_line;			/* max value of cur_line */
1229b50d902SRodney W. Grimes 	int this_line;			/* line l points to */
1239b50d902SRodney W. Grimes 	int nflushd_lines;		/* number of lines that were flushed */
124c3fae744STim J. Robbins 	int adjust, opt, warned, width;
125f23ed79bSBaptiste Daroussin 	const char *errstr;
1269b50d902SRodney W. Grimes 
1278bbd9072SAndrey A. Chernov 	(void)setlocale(LC_CTYPE, "");
1288bbd9072SAndrey A. Chernov 
129a4e3fc54SMariusz Zaborski 	if (caph_limit_stdio() == -1)
130a4e3fc54SMariusz Zaborski 		err(1, "unable to limit stdio");
131e5ea68e7SBaptiste Daroussin 
1327672a014SMariusz Zaborski 	if (caph_enter() < 0)
133e5ea68e7SBaptiste Daroussin 		err(1, "unable to enter capability mode");
134e5ea68e7SBaptiste Daroussin 
135f23ed79bSBaptiste Daroussin 	max_bufd_lines = 256;
1369b50d902SRodney W. Grimes 	compress_spaces = 1;		/* compress spaces into tabs */
137844518ffSMike Heffner 	while ((opt = getopt(argc, argv, "bfhl:px")) != -1)
1389b50d902SRodney W. Grimes 		switch (opt) {
1399b50d902SRodney W. Grimes 		case 'b':		/* do not output backspaces */
1409b50d902SRodney W. Grimes 			no_backspaces = 1;
1419b50d902SRodney W. Grimes 			break;
1429b50d902SRodney W. Grimes 		case 'f':		/* allow half forward line feeds */
1439b50d902SRodney W. Grimes 			fine = 1;
1449b50d902SRodney W. Grimes 			break;
1459b50d902SRodney W. Grimes 		case 'h':		/* compress spaces into tabs */
1469b50d902SRodney W. Grimes 			compress_spaces = 1;
1479b50d902SRodney W. Grimes 			break;
1489b50d902SRodney W. Grimes 		case 'l':		/* buffered line count */
149f23ed79bSBaptiste Daroussin 			max_bufd_lines = strtonum(optarg, 1,
150f23ed79bSBaptiste Daroussin 			    (INT_MAX - BUFFER_MARGIN) / 2, &errstr) * 2;
151f23ed79bSBaptiste Daroussin 			if (errstr != NULL)
152f23ed79bSBaptiste Daroussin 				errx(1, "bad -l argument, %s: %s", errstr,
153f23ed79bSBaptiste Daroussin 					optarg);
1549b50d902SRodney W. Grimes 			break;
155844518ffSMike Heffner 		case 'p':		/* pass unknown control sequences */
156844518ffSMike Heffner 			pass_unknown_seqs = 1;
157844518ffSMike Heffner 			break;
1589b50d902SRodney W. Grimes 		case 'x':		/* do not compress spaces into tabs */
1599b50d902SRodney W. Grimes 			compress_spaces = 0;
1609b50d902SRodney W. Grimes 			break;
1619b50d902SRodney W. Grimes 		case '?':
1629b50d902SRodney W. Grimes 		default:
1639b50d902SRodney W. Grimes 			usage();
1649b50d902SRodney W. Grimes 		}
1659b50d902SRodney W. Grimes 
1669b50d902SRodney W. Grimes 	if (optind != argc)
1679b50d902SRodney W. Grimes 		usage();
1689b50d902SRodney W. Grimes 
1699b50d902SRodney W. Grimes 	adjust = cur_col = extra_lines = warned = 0;
1709b50d902SRodney W. Grimes 	cur_line = max_line = nflushd_lines = this_line = 0;
1719b50d902SRodney W. Grimes 	cur_set = last_set = CS_NORMAL;
1729b50d902SRodney W. Grimes 	lines = l = alloc_line();
1739b50d902SRodney W. Grimes 
174c3fae744STim J. Robbins 	while ((ch = getwchar()) != WEOF) {
175c3fae744STim J. Robbins 		if (!iswgraph(ch)) {
1769b50d902SRodney W. Grimes 			switch (ch) {
1779b50d902SRodney W. Grimes 			case BS:		/* can't go back further */
1789b50d902SRodney W. Grimes 				if (cur_col == 0)
1799b50d902SRodney W. Grimes 					continue;
1809b50d902SRodney W. Grimes 				--cur_col;
1819b50d902SRodney W. Grimes 				continue;
1829b50d902SRodney W. Grimes 			case CR:
1839b50d902SRodney W. Grimes 				cur_col = 0;
1849b50d902SRodney W. Grimes 				continue;
1859b50d902SRodney W. Grimes 			case ESC:		/* just ignore EOF */
186c3fae744STim J. Robbins 				switch(getwchar()) {
1877bc60a16SBaptiste Daroussin 				/*
1887bc60a16SBaptiste Daroussin 				 * In the input stream, accept both the
1897bc60a16SBaptiste Daroussin 				 * XPG5 sequences ESC-digit and the
1907bc60a16SBaptiste Daroussin 				 * traditional BSD sequences ESC-ctrl.
1917bc60a16SBaptiste Daroussin 				 */
1927bc60a16SBaptiste Daroussin 				case '\007':
1937bc60a16SBaptiste Daroussin 					/* FALLTHROUGH */
1949b50d902SRodney W. Grimes 				case RLF:
195f23ed79bSBaptiste Daroussin 					addto_lineno(&cur_line, -2);
1969b50d902SRodney W. Grimes 					break;
1977bc60a16SBaptiste Daroussin 				case '\010':
1987bc60a16SBaptiste Daroussin 					/* FALLTHROUGH */
1999b50d902SRodney W. Grimes 				case RHLF:
200f23ed79bSBaptiste Daroussin 					addto_lineno(&cur_line, -1);
2019b50d902SRodney W. Grimes 					break;
2027bc60a16SBaptiste Daroussin 				case '\011':
2037bc60a16SBaptiste Daroussin 					/* FALLTHROUGH */
2049b50d902SRodney W. Grimes 				case FHLF:
205f23ed79bSBaptiste Daroussin 					addto_lineno(&cur_line, 1);
2069b50d902SRodney W. Grimes 					if (cur_line > max_line)
2079b50d902SRodney W. Grimes 						max_line = cur_line;
2089b50d902SRodney W. Grimes 				}
2099b50d902SRodney W. Grimes 				continue;
2109b50d902SRodney W. Grimes 			case NL:
211f23ed79bSBaptiste Daroussin 				addto_lineno(&cur_line, 2);
2129b50d902SRodney W. Grimes 				if (cur_line > max_line)
2139b50d902SRodney W. Grimes 					max_line = cur_line;
2149b50d902SRodney W. Grimes 				cur_col = 0;
2159b50d902SRodney W. Grimes 				continue;
2169b50d902SRodney W. Grimes 			case SPACE:
2179b50d902SRodney W. Grimes 				++cur_col;
2189b50d902SRodney W. Grimes 				continue;
2199b50d902SRodney W. Grimes 			case SI:
2209b50d902SRodney W. Grimes 				cur_set = CS_NORMAL;
2219b50d902SRodney W. Grimes 				continue;
2229b50d902SRodney W. Grimes 			case SO:
2239b50d902SRodney W. Grimes 				cur_set = CS_ALTERNATE;
2249b50d902SRodney W. Grimes 				continue;
2259b50d902SRodney W. Grimes 			case TAB:		/* adjust column */
2269b50d902SRodney W. Grimes 				cur_col |= 7;
2279b50d902SRodney W. Grimes 				++cur_col;
2289b50d902SRodney W. Grimes 				continue;
2299b50d902SRodney W. Grimes 			case VT:
230f23ed79bSBaptiste Daroussin 				addto_lineno(&cur_line, -2);
2319b50d902SRodney W. Grimes 				continue;
2329b50d902SRodney W. Grimes 			}
233c3fae744STim J. Robbins 			if (iswspace(ch)) {
234c3fae744STim J. Robbins 				if ((width = wcwidth(ch)) > 0)
235c3fae744STim J. Robbins 					cur_col += width;
236c3fae744STim J. Robbins 				continue;
237c3fae744STim J. Robbins 			}
238844518ffSMike Heffner 			if (!pass_unknown_seqs)
2399b50d902SRodney W. Grimes 				continue;
2409b50d902SRodney W. Grimes 		}
2419b50d902SRodney W. Grimes 
2429b50d902SRodney W. Grimes 		/* Must stuff ch in a line - are we at the right one? */
243f23ed79bSBaptiste Daroussin 		if (cur_line + adjust != this_line) {
2449b50d902SRodney W. Grimes 			LINE *lnew;
2459b50d902SRodney W. Grimes 
2469b50d902SRodney W. Grimes 			/* round up to next line */
247f23ed79bSBaptiste Daroussin 			adjust = !fine && (cur_line & 1);
248f23ed79bSBaptiste Daroussin 
249f23ed79bSBaptiste Daroussin 			if (cur_line + adjust < this_line) {
250f23ed79bSBaptiste Daroussin 				while (cur_line + adjust < this_line &&
251f23ed79bSBaptiste Daroussin 				    l->l_prev != NULL) {
2529b50d902SRodney W. Grimes 					l = l->l_prev;
253f23ed79bSBaptiste Daroussin 					this_line--;
254f23ed79bSBaptiste Daroussin 				}
255f23ed79bSBaptiste Daroussin 				if (cur_line + adjust < this_line) {
2569b50d902SRodney W. Grimes 					if (nflushd_lines == 0) {
2579b50d902SRodney W. Grimes 						/*
2589b50d902SRodney W. Grimes 						 * Allow backup past first
2599b50d902SRodney W. Grimes 						 * line if nothing has been
2609b50d902SRodney W. Grimes 						 * flushed yet.
2619b50d902SRodney W. Grimes 						 */
262f23ed79bSBaptiste Daroussin 						while (cur_line + adjust
263f23ed79bSBaptiste Daroussin 						    < this_line) {
2649b50d902SRodney W. Grimes 							lnew = alloc_line();
2659b50d902SRodney W. Grimes 							l->l_prev = lnew;
2669b50d902SRodney W. Grimes 							lnew->l_next = l;
2679b50d902SRodney W. Grimes 							l = lines = lnew;
2689b50d902SRodney W. Grimes 							extra_lines++;
269f23ed79bSBaptiste Daroussin 							this_line--;
2709b50d902SRodney W. Grimes 						}
2719b50d902SRodney W. Grimes 					} else {
2729b50d902SRodney W. Grimes 						if (!warned++)
2739b50d902SRodney W. Grimes 							dowarn(cur_line);
274f23ed79bSBaptiste Daroussin 						cur_line = this_line - adjust;
2759b50d902SRodney W. Grimes 					}
2769b50d902SRodney W. Grimes 				}
2779b50d902SRodney W. Grimes 			} else {
2789b50d902SRodney W. Grimes 				/* may need to allocate here */
279f23ed79bSBaptiste Daroussin 				while (cur_line + adjust > this_line) {
280f23ed79bSBaptiste Daroussin 					if (l->l_next == NULL) {
281f23ed79bSBaptiste Daroussin 						l->l_next = alloc_line();
282f23ed79bSBaptiste Daroussin 						l->l_next->l_prev = l;
283f23ed79bSBaptiste Daroussin 					}
2849b50d902SRodney W. Grimes 					l = l->l_next;
285f23ed79bSBaptiste Daroussin 					this_line++;
2869b50d902SRodney W. Grimes 				}
2879b50d902SRodney W. Grimes 			}
288f23ed79bSBaptiste Daroussin 			if (this_line > nflushd_lines &&
289f23ed79bSBaptiste Daroussin 			    this_line - nflushd_lines >=
290f23ed79bSBaptiste Daroussin 			    max_bufd_lines + BUFFER_MARGIN) {
291f23ed79bSBaptiste Daroussin 				if (extra_lines) {
292f23ed79bSBaptiste Daroussin 					flush_lines(extra_lines);
293f23ed79bSBaptiste Daroussin 					extra_lines = 0;
294f23ed79bSBaptiste Daroussin 				}
295f23ed79bSBaptiste Daroussin 				flush_lines(this_line - nflushd_lines -
296f23ed79bSBaptiste Daroussin 				    max_bufd_lines);
297f23ed79bSBaptiste Daroussin 				nflushd_lines = this_line - max_bufd_lines;
2989b50d902SRodney W. Grimes 			}
2999b50d902SRodney W. Grimes 		}
3009b50d902SRodney W. Grimes 		/* grow line's buffer? */
3019b50d902SRodney W. Grimes 		if (l->l_line_len + 1 >= l->l_lsize) {
3029b50d902SRodney W. Grimes 			int need;
3039b50d902SRodney W. Grimes 
3049b50d902SRodney W. Grimes 			need = l->l_lsize ? l->l_lsize * 2 : 90;
30544974a7fSDavid E. O'Brien 			if ((l->l_line = realloc(l->l_line,
30644974a7fSDavid E. O'Brien 			    (unsigned)need * sizeof(CHAR))) == NULL)
30718463c77SKevin Lo 				err(1, NULL);
3089b50d902SRodney W. Grimes 			l->l_lsize = need;
3099b50d902SRodney W. Grimes 		}
3109b50d902SRodney W. Grimes 		c = &l->l_line[l->l_line_len++];
3119b50d902SRodney W. Grimes 		c->c_char = ch;
3129b50d902SRodney W. Grimes 		c->c_set = cur_set;
3139b50d902SRodney W. Grimes 		c->c_column = cur_col;
314c3fae744STim J. Robbins 		c->c_width = wcwidth(ch);
3159b50d902SRodney W. Grimes 		/*
3169b50d902SRodney W. Grimes 		 * If things are put in out of order, they will need sorting
3179b50d902SRodney W. Grimes 		 * when it is flushed.
3189b50d902SRodney W. Grimes 		 */
3199b50d902SRodney W. Grimes 		if (cur_col < l->l_max_col)
3209b50d902SRodney W. Grimes 			l->l_needs_sort = 1;
3219b50d902SRodney W. Grimes 		else
3229b50d902SRodney W. Grimes 			l->l_max_col = cur_col;
323c3fae744STim J. Robbins 		if (c->c_width > 0)
324c3fae744STim J. Robbins 			cur_col += c->c_width;
3259b50d902SRodney W. Grimes 	}
326c3fae744STim J. Robbins 	if (ferror(stdin))
327c3fae744STim J. Robbins 		err(1, NULL);
328*6b43126fSMark Johnston 	if (extra_lines) {
329*6b43126fSMark Johnston 		/*
330*6b43126fSMark Johnston 		 * Extra lines only exist if no lines have been flushed
331*6b43126fSMark Johnston 		 * yet. This means that 'lines' must point to line zero
332*6b43126fSMark Johnston 		 * after we flush the extra lines.
333*6b43126fSMark Johnston 		 */
334f23ed79bSBaptiste Daroussin 		flush_lines(extra_lines);
335*6b43126fSMark Johnston 		l = lines;
336*6b43126fSMark Johnston 		this_line = 0;
337*6b43126fSMark Johnston 	}
338df3f5d9dSPeter Wemm 
3399b50d902SRodney W. Grimes 	/* goto the last line that had a character on it */
3409b50d902SRodney W. Grimes 	for (; l->l_next; l = l->l_next)
3419b50d902SRodney W. Grimes 		this_line++;
342f23ed79bSBaptiste Daroussin 	flush_lines(this_line - nflushd_lines + 1);
3439b50d902SRodney W. Grimes 
3449b50d902SRodney W. Grimes 	/* make sure we leave things in a sane state */
3459b50d902SRodney W. Grimes 	if (last_set != CS_NORMAL)
346a9da03efSBaptiste Daroussin 		PUTC(SI);
3479b50d902SRodney W. Grimes 
3489b50d902SRodney W. Grimes 	/* flush out the last few blank lines */
349*6b43126fSMark Johnston 	if (max_line >= this_line)
350*6b43126fSMark Johnston 		nblank_lines = max_line - this_line + (max_line & 1);
351*6b43126fSMark Johnston 	if (nblank_lines == 0)
352*6b43126fSMark Johnston 		/* end with a newline even if the source doesn't */
353*6b43126fSMark Johnston 		nblank_lines = 2;
3549b50d902SRodney W. Grimes 	flush_blanks();
3559b50d902SRodney W. Grimes 	exit(0);
3569b50d902SRodney W. Grimes }
3579b50d902SRodney W. Grimes 
358*6b43126fSMark Johnston /*
359*6b43126fSMark Johnston  * Prints the first 'nflush' lines. Printed lines are freed.
360*6b43126fSMark Johnston  * After this function returns, 'lines' points to the first
361*6b43126fSMark Johnston  * of the remaining lines, and 'nblank_lines' will have the
362*6b43126fSMark Johnston  * number of half line feeds between the final flushed line
363*6b43126fSMark Johnston  * and the first remaining line.
364*6b43126fSMark Johnston  */
365ab8d971cSEd Schouten static void
flush_lines(int nflush)366f4ac32deSDavid Malone flush_lines(int nflush)
3679b50d902SRodney W. Grimes {
3689b50d902SRodney W. Grimes 	LINE *l;
3699b50d902SRodney W. Grimes 
3709b50d902SRodney W. Grimes 	while (--nflush >= 0) {
3719b50d902SRodney W. Grimes 		l = lines;
3729b50d902SRodney W. Grimes 		lines = l->l_next;
3739b50d902SRodney W. Grimes 		if (l->l_line) {
3749b50d902SRodney W. Grimes 			flush_blanks();
3759b50d902SRodney W. Grimes 			flush_line(l);
376*6b43126fSMark Johnston 			free(l->l_line);
3779b50d902SRodney W. Grimes 		}
378*6b43126fSMark Johnston 		if (l->l_next)
3799b50d902SRodney W. Grimes 			nblank_lines++;
3809b50d902SRodney W. Grimes 		free_line(l);
3819b50d902SRodney W. Grimes 	}
3829b50d902SRodney W. Grimes 	if (lines)
3839b50d902SRodney W. Grimes 		lines->l_prev = NULL;
3849b50d902SRodney W. Grimes }
3859b50d902SRodney W. Grimes 
3869b50d902SRodney W. Grimes /*
387*6b43126fSMark Johnston  * Print a number of newline/half newlines.
388*6b43126fSMark Johnston  * nblank_lines is the number of half line feeds.
3899b50d902SRodney W. Grimes  */
390ab8d971cSEd Schouten static void
flush_blanks(void)391f4ac32deSDavid Malone flush_blanks(void)
3929b50d902SRodney W. Grimes {
3939b50d902SRodney W. Grimes 	int half, i, nb;
3949b50d902SRodney W. Grimes 
3959b50d902SRodney W. Grimes 	half = 0;
3969b50d902SRodney W. Grimes 	nb = nblank_lines;
3979b50d902SRodney W. Grimes 	if (nb & 1) {
3989b50d902SRodney W. Grimes 		if (fine)
3999b50d902SRodney W. Grimes 			half = 1;
4009b50d902SRodney W. Grimes 		else
4019b50d902SRodney W. Grimes 			nb++;
4029b50d902SRodney W. Grimes 	}
4039b50d902SRodney W. Grimes 	nb /= 2;
4049b50d902SRodney W. Grimes 	for (i = nb; --i >= 0;)
4059b50d902SRodney W. Grimes 		PUTC('\n');
4069b50d902SRodney W. Grimes 	if (half) {
407a9da03efSBaptiste Daroussin 		PUTC(ESC);
408a9da03efSBaptiste Daroussin 		PUTC(FHLF);
4099b50d902SRodney W. Grimes 		if (!nb)
4109b50d902SRodney W. Grimes 			PUTC('\r');
4119b50d902SRodney W. Grimes 	}
4129b50d902SRodney W. Grimes 	nblank_lines = 0;
4139b50d902SRodney W. Grimes }
4149b50d902SRodney W. Grimes 
4159b50d902SRodney W. Grimes /*
4169b50d902SRodney W. Grimes  * Write a line to stdout taking care of space to tab conversion (-h flag)
4179b50d902SRodney W. Grimes  * and character set shifts.
4189b50d902SRodney W. Grimes  */
419ab8d971cSEd Schouten static void
flush_line(LINE * l)420f4ac32deSDavid Malone flush_line(LINE *l)
4219b50d902SRodney W. Grimes {
4229b50d902SRodney W. Grimes 	CHAR *c, *endc;
423ae66acacSStefan Farfeleder 	int i, j, nchars, last_col, save, this_col, tot;
4249b50d902SRodney W. Grimes 
4259b50d902SRodney W. Grimes 	last_col = 0;
4269b50d902SRodney W. Grimes 	nchars = l->l_line_len;
4279b50d902SRodney W. Grimes 
4289b50d902SRodney W. Grimes 	if (l->l_needs_sort) {
4299b50d902SRodney W. Grimes 		static CHAR *sorted;
430ae66acacSStefan Farfeleder 		static int count_size, *count, sorted_size;
4319b50d902SRodney W. Grimes 
4329b50d902SRodney W. Grimes 		/*
4339b50d902SRodney W. Grimes 		 * Do an O(n) sort on l->l_line by column being careful to
4349b50d902SRodney W. Grimes 		 * preserve the order of characters in the same column.
4359b50d902SRodney W. Grimes 		 */
4369b50d902SRodney W. Grimes 		if (l->l_lsize > sorted_size) {
4379b50d902SRodney W. Grimes 			sorted_size = l->l_lsize;
43844974a7fSDavid E. O'Brien 			if ((sorted = realloc(sorted,
43944974a7fSDavid E. O'Brien 			    (unsigned)sizeof(CHAR) * sorted_size)) == NULL)
44018463c77SKevin Lo 				err(1, NULL);
4419b50d902SRodney W. Grimes 		}
4429b50d902SRodney W. Grimes 		if (l->l_max_col >= count_size) {
4439b50d902SRodney W. Grimes 			count_size = l->l_max_col + 1;
44444974a7fSDavid E. O'Brien 			if ((count = realloc(count,
44544974a7fSDavid E. O'Brien 			    (unsigned)sizeof(int) * count_size)) == NULL)
44618463c77SKevin Lo 				err(1, NULL);
4479b50d902SRodney W. Grimes 		}
448f34d0e74SMike Heffner 		memset(count, 0, sizeof(int) * l->l_max_col + 1);
4499b50d902SRodney W. Grimes 		for (i = nchars, c = l->l_line; --i >= 0; c++)
4509b50d902SRodney W. Grimes 			count[c->c_column]++;
4519b50d902SRodney W. Grimes 
4529b50d902SRodney W. Grimes 		/*
4539b50d902SRodney W. Grimes 		 * calculate running total (shifted down by 1) to use as
4549b50d902SRodney W. Grimes 		 * indices into new line.
4559b50d902SRodney W. Grimes 		 */
4569b50d902SRodney W. Grimes 		for (tot = 0, i = 0; i <= l->l_max_col; i++) {
4579b50d902SRodney W. Grimes 			save = count[i];
4589b50d902SRodney W. Grimes 			count[i] = tot;
4599b50d902SRodney W. Grimes 			tot += save;
4609b50d902SRodney W. Grimes 		}
4619b50d902SRodney W. Grimes 
4629b50d902SRodney W. Grimes 		for (i = nchars, c = l->l_line; --i >= 0; c++)
4639b50d902SRodney W. Grimes 			sorted[count[c->c_column]++] = *c;
4649b50d902SRodney W. Grimes 		c = sorted;
4659b50d902SRodney W. Grimes 	} else
4669b50d902SRodney W. Grimes 		c = l->l_line;
4679b50d902SRodney W. Grimes 	while (nchars > 0) {
4689b50d902SRodney W. Grimes 		this_col = c->c_column;
4699b50d902SRodney W. Grimes 		endc = c;
4709b50d902SRodney W. Grimes 		do {
4719b50d902SRodney W. Grimes 			++endc;
4729b50d902SRodney W. Grimes 		} while (--nchars > 0 && this_col == endc->c_column);
4739b50d902SRodney W. Grimes 
4749b50d902SRodney W. Grimes 		/* if -b only print last character */
475c3fae744STim J. Robbins 		if (no_backspaces) {
4769b50d902SRodney W. Grimes 			c = endc - 1;
477c3fae744STim J. Robbins 			if (nchars > 0 &&
478c3fae744STim J. Robbins 			    this_col + c->c_width > endc->c_column)
479c3fae744STim J. Robbins 				continue;
480c3fae744STim J. Robbins 		}
4819b50d902SRodney W. Grimes 
4829b50d902SRodney W. Grimes 		if (this_col > last_col) {
4839b50d902SRodney W. Grimes 			int nspace = this_col - last_col;
4849b50d902SRodney W. Grimes 
4859b50d902SRodney W. Grimes 			if (compress_spaces && nspace > 1) {
486f746e67cSBruce Evans 				while (1) {
4875eb2aa45SEd Maste 					int tab_col, tab_size;
4889b50d902SRodney W. Grimes 
489f746e67cSBruce Evans 					tab_col = (last_col + 8) & ~7;
490f746e67cSBruce Evans 					if (tab_col > this_col)
491f746e67cSBruce Evans 						break;
492f746e67cSBruce Evans 					tab_size = tab_col - last_col;
493f746e67cSBruce Evans 					if (tab_size == 1)
494f746e67cSBruce Evans 						PUTC(' ');
495f746e67cSBruce Evans 					else
4969b50d902SRodney W. Grimes 						PUTC('\t');
497f746e67cSBruce Evans 					nspace -= tab_size;
498f746e67cSBruce Evans 					last_col = tab_col;
499f746e67cSBruce Evans 				}
5009b50d902SRodney W. Grimes 			}
5019b50d902SRodney W. Grimes 			while (--nspace >= 0)
5029b50d902SRodney W. Grimes 				PUTC(' ');
5039b50d902SRodney W. Grimes 			last_col = this_col;
5049b50d902SRodney W. Grimes 		}
5059b50d902SRodney W. Grimes 
5069b50d902SRodney W. Grimes 		for (;;) {
5079b50d902SRodney W. Grimes 			if (c->c_set != last_set) {
5089b50d902SRodney W. Grimes 				switch (c->c_set) {
5099b50d902SRodney W. Grimes 				case CS_NORMAL:
510a9da03efSBaptiste Daroussin 					PUTC(SI);
5119b50d902SRodney W. Grimes 					break;
5129b50d902SRodney W. Grimes 				case CS_ALTERNATE:
513a9da03efSBaptiste Daroussin 					PUTC(SO);
5149b50d902SRodney W. Grimes 				}
5159b50d902SRodney W. Grimes 				last_set = c->c_set;
5169b50d902SRodney W. Grimes 			}
5179b50d902SRodney W. Grimes 			PUTC(c->c_char);
518c3fae744STim J. Robbins 			if ((c + 1) < endc)
51970708375SDavid Malone 				for (j = 0; j < c->c_width; j++)
520c3fae744STim J. Robbins 					PUTC('\b');
5219b50d902SRodney W. Grimes 			if (++c >= endc)
5229b50d902SRodney W. Grimes 				break;
5239b50d902SRodney W. Grimes 		}
524c3fae744STim J. Robbins 		last_col += (c - 1)->c_width;
5259b50d902SRodney W. Grimes 	}
5269b50d902SRodney W. Grimes }
5279b50d902SRodney W. Grimes 
528f23ed79bSBaptiste Daroussin /*
529f23ed79bSBaptiste Daroussin  * Increment or decrement a line number, checking for overflow.
530f23ed79bSBaptiste Daroussin  * Stop one below INT_MAX such that the adjust variable is safe.
531f23ed79bSBaptiste Daroussin  */
532f23ed79bSBaptiste Daroussin void
addto_lineno(int * lno,int offset)533f23ed79bSBaptiste Daroussin addto_lineno(int *lno, int offset)
534f23ed79bSBaptiste Daroussin {
535f23ed79bSBaptiste Daroussin 	if (offset > 0) {
536f23ed79bSBaptiste Daroussin 		if (*lno >= INT_MAX - offset)
537f23ed79bSBaptiste Daroussin 			errx(1, "too many lines");
538f23ed79bSBaptiste Daroussin 	} else {
539f23ed79bSBaptiste Daroussin 		if (*lno < INT_MIN - offset)
540f23ed79bSBaptiste Daroussin 			errx(1, "too many reverse line feeds");
541f23ed79bSBaptiste Daroussin 	}
542f23ed79bSBaptiste Daroussin 	*lno += offset;
543f23ed79bSBaptiste Daroussin }
544f23ed79bSBaptiste Daroussin 
5459b50d902SRodney W. Grimes #define	NALLOC 64
5469b50d902SRodney W. Grimes 
5479b50d902SRodney W. Grimes static LINE *line_freelist;
5489b50d902SRodney W. Grimes 
549ab8d971cSEd Schouten static LINE *
alloc_line(void)550f4ac32deSDavid Malone alloc_line(void)
5519b50d902SRodney W. Grimes {
5529b50d902SRodney W. Grimes 	LINE *l;
5539b50d902SRodney W. Grimes 	int i;
5549b50d902SRodney W. Grimes 
5559b50d902SRodney W. Grimes 	if (!line_freelist) {
55644974a7fSDavid E. O'Brien 		if ((l = realloc(NULL, sizeof(LINE) * NALLOC)) == NULL)
55718463c77SKevin Lo 			err(1, NULL);
5589b50d902SRodney W. Grimes 		line_freelist = l;
5599b50d902SRodney W. Grimes 		for (i = 1; i < NALLOC; i++, l++)
5609b50d902SRodney W. Grimes 			l->l_next = l + 1;
5619b50d902SRodney W. Grimes 		l->l_next = NULL;
5629b50d902SRodney W. Grimes 	}
5639b50d902SRodney W. Grimes 	l = line_freelist;
5649b50d902SRodney W. Grimes 	line_freelist = l->l_next;
5659b50d902SRodney W. Grimes 
5669b50d902SRodney W. Grimes 	memset(l, 0, sizeof(LINE));
5679b50d902SRodney W. Grimes 	return (l);
5689b50d902SRodney W. Grimes }
5699b50d902SRodney W. Grimes 
570ab8d971cSEd Schouten static void
free_line(LINE * l)571f4ac32deSDavid Malone free_line(LINE *l)
5729b50d902SRodney W. Grimes {
5739b50d902SRodney W. Grimes 
5749b50d902SRodney W. Grimes 	l->l_next = line_freelist;
5759b50d902SRodney W. Grimes 	line_freelist = l;
5769b50d902SRodney W. Grimes }
5779b50d902SRodney W. Grimes 
578ab8d971cSEd Schouten static void
usage(void)579f4ac32deSDavid Malone usage(void)
5809b50d902SRodney W. Grimes {
5819b50d902SRodney W. Grimes 
582844518ffSMike Heffner 	(void)fprintf(stderr, "usage: col [-bfhpx] [-l nline]\n");
5839b50d902SRodney W. Grimes 	exit(1);
5849b50d902SRodney W. Grimes }
5859b50d902SRodney W. Grimes 
586ab8d971cSEd Schouten static void
dowarn(int line)587f4ac32deSDavid Malone dowarn(int line)
5889b50d902SRodney W. Grimes {
5899b50d902SRodney W. Grimes 
5909b50d902SRodney W. Grimes 	warnx("warning: can't back up %s",
5919b50d902SRodney W. Grimes 		line < 0 ? "past first line" : "-- line already flushed");
5929b50d902SRodney W. Grimes }
593