2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1990, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Michael Rendell of the Memorial University of Newfoundland.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 static const char copyright[] =
37 "@(#) Copyright (c) 1990, 1993, 1994\n\
38 The Regents of the University of California. All rights reserved.\n";
43 static char sccsid[] = "@(#)col.c 8.5 (Berkeley) 5/4/95";
47 #include <sys/cdefs.h>
48 __FBSDID("$FreeBSD$");
50 #include <sys/capsicum.h>
52 #include <capsicum_helpers.h>
64 #define BS '\b' /* backspace */
65 #define TAB '\t' /* tab */
66 #define SPACE ' ' /* space */
67 #define NL '\n' /* newline */
68 #define CR '\r' /* carriage return */
69 #define ESC '\033' /* escape */
70 #define SI '\017' /* shift in to normal character set */
71 #define SO '\016' /* shift out to alternate character set */
72 #define VT '\013' /* vertical tab (aka reverse line feed) */
73 #define RLF '7' /* ESC-7 reverse line feed */
74 #define RHLF '8' /* ESC-8 reverse half-line feed */
75 #define FHLF '9' /* ESC-9 forward half-line feed */
77 /* build up at least this many lines before flushing them out */
78 #define BUFFER_MARGIN 32
82 typedef struct char_str {
84 #define CS_ALTERNATE 2
85 short c_column; /* column character is in */
86 CSET c_set; /* character set (currently only 2) */
87 wchar_t c_char; /* character in question */
88 int c_width; /* character width */
91 typedef struct line_str LINE;
93 CHAR *l_line; /* characters on the line */
94 LINE *l_prev; /* previous line */
95 LINE *l_next; /* next line */
96 int l_lsize; /* allocated sizeof l_line */
97 int l_line_len; /* strlen(l_line) */
98 int l_needs_sort; /* set if chars went in out of order */
99 int l_max_col; /* max column in the line */
102 static void addto_lineno(int *, int);
103 static LINE *alloc_line(void);
104 static void dowarn(int);
105 static void flush_line(LINE *);
106 static void flush_lines(int);
107 static void flush_blanks(void);
108 static void free_line(LINE *);
109 static void usage(void);
111 static CSET last_set; /* char_set of last char printed */
113 static int compress_spaces; /* if doing space -> tab conversion */
114 static int fine; /* if `fine' resolution (half lines) */
115 static int max_bufd_lines; /* max # of half lines to keep in memory */
116 static int nblank_lines; /* # blanks after last flushed line */
117 static int no_backspaces; /* if not to output any backspaces */
118 static int pass_unknown_seqs; /* pass unknown control sequences */
122 if (putwchar(ch) == WEOF) \
123 errx(1, "write error"); \
127 main(int argc, char **argv)
131 CSET cur_set; /* current character set */
132 LINE *l; /* current line */
133 int extra_lines; /* # of lines above first line */
134 int cur_col; /* current column */
135 int cur_line; /* line number of current position */
136 int max_line; /* max value of cur_line */
137 int this_line; /* line l points to */
138 int nflushd_lines; /* number of lines that were flushed */
139 int adjust, opt, warned, width;
142 (void)setlocale(LC_CTYPE, "");
144 if (caph_limit_stdio() == -1)
145 err(1, "unable to limit stdio");
147 if (caph_enter() < 0)
148 err(1, "unable to enter capability mode");
150 max_bufd_lines = 256;
151 compress_spaces = 1; /* compress spaces into tabs */
152 while ((opt = getopt(argc, argv, "bfhl:px")) != -1)
154 case 'b': /* do not output backspaces */
157 case 'f': /* allow half forward line feeds */
160 case 'h': /* compress spaces into tabs */
163 case 'l': /* buffered line count */
164 max_bufd_lines = strtonum(optarg, 1,
165 (INT_MAX - BUFFER_MARGIN) / 2, &errstr) * 2;
167 errx(1, "bad -l argument, %s: %s", errstr,
170 case 'p': /* pass unknown control sequences */
171 pass_unknown_seqs = 1;
173 case 'x': /* do not compress spaces into tabs */
184 adjust = cur_col = extra_lines = warned = 0;
185 cur_line = max_line = nflushd_lines = this_line = 0;
186 cur_set = last_set = CS_NORMAL;
187 lines = l = alloc_line();
189 while ((ch = getwchar()) != WEOF) {
192 case BS: /* can't go back further */
200 case ESC: /* just ignore EOF */
203 * In the input stream, accept both the
204 * XPG5 sequences ESC-digit and the
205 * traditional BSD sequences ESC-ctrl.
210 addto_lineno(&cur_line, -2);
215 addto_lineno(&cur_line, -1);
220 addto_lineno(&cur_line, 1);
221 if (cur_line > max_line)
226 addto_lineno(&cur_line, 2);
227 if (cur_line > max_line)
238 cur_set = CS_ALTERNATE;
240 case TAB: /* adjust column */
245 addto_lineno(&cur_line, -2);
249 if ((width = wcwidth(ch)) > 0)
253 if (!pass_unknown_seqs)
257 /* Must stuff ch in a line - are we at the right one? */
258 if (cur_line + adjust != this_line) {
261 /* round up to next line */
262 adjust = !fine && (cur_line & 1);
264 if (cur_line + adjust < this_line) {
265 while (cur_line + adjust < this_line &&
270 if (cur_line + adjust < this_line) {
271 if (nflushd_lines == 0) {
273 * Allow backup past first
274 * line if nothing has been
277 while (cur_line + adjust
289 cur_line = this_line - adjust;
293 /* may need to allocate here */
294 while (cur_line + adjust > this_line) {
295 if (l->l_next == NULL) {
296 l->l_next = alloc_line();
297 l->l_next->l_prev = l;
303 if (this_line > nflushd_lines &&
304 this_line - nflushd_lines >=
305 max_bufd_lines + BUFFER_MARGIN) {
307 flush_lines(extra_lines);
310 flush_lines(this_line - nflushd_lines -
312 nflushd_lines = this_line - max_bufd_lines;
315 /* grow line's buffer? */
316 if (l->l_line_len + 1 >= l->l_lsize) {
319 need = l->l_lsize ? l->l_lsize * 2 : 90;
320 if ((l->l_line = realloc(l->l_line,
321 (unsigned)need * sizeof(CHAR))) == NULL)
325 c = &l->l_line[l->l_line_len++];
328 c->c_column = cur_col;
329 c->c_width = wcwidth(ch);
331 * If things are put in out of order, they will need sorting
332 * when it is flushed.
334 if (cur_col < l->l_max_col)
337 l->l_max_col = cur_col;
339 cur_col += c->c_width;
344 flush_lines(extra_lines);
346 /* goto the last line that had a character on it */
347 for (; l->l_next; l = l->l_next)
349 flush_lines(this_line - nflushd_lines + 1);
351 /* make sure we leave things in a sane state */
352 if (last_set != CS_NORMAL)
355 /* flush out the last few blank lines */
356 if (max_line > this_line)
357 nblank_lines = max_line - this_line;
365 flush_lines(int nflush)
369 while (--nflush >= 0) {
376 if (l->l_line || l->l_next)
379 (void)free(l->l_line);
383 lines->l_prev = NULL;
387 * Print a number of newline/half newlines. If fine flag is set, nblank_lines
388 * is the number of half line feeds, otherwise it is the number of whole line
405 for (i = nb; --i >= 0;)
417 * Write a line to stdout taking care of space to tab conversion (-h flag)
418 * and character set shifts.
424 int i, j, nchars, last_col, save, this_col, tot;
427 nchars = l->l_line_len;
429 if (l->l_needs_sort) {
431 static int count_size, *count, sorted_size;
434 * Do an O(n) sort on l->l_line by column being careful to
435 * preserve the order of characters in the same column.
437 if (l->l_lsize > sorted_size) {
438 sorted_size = l->l_lsize;
439 if ((sorted = realloc(sorted,
440 (unsigned)sizeof(CHAR) * sorted_size)) == NULL)
443 if (l->l_max_col >= count_size) {
444 count_size = l->l_max_col + 1;
445 if ((count = realloc(count,
446 (unsigned)sizeof(int) * count_size)) == NULL)
449 memset(count, 0, sizeof(int) * l->l_max_col + 1);
450 for (i = nchars, c = l->l_line; --i >= 0; c++)
451 count[c->c_column]++;
454 * calculate running total (shifted down by 1) to use as
455 * indices into new line.
457 for (tot = 0, i = 0; i <= l->l_max_col; i++) {
463 for (i = nchars, c = l->l_line; --i >= 0; c++)
464 sorted[count[c->c_column]++] = *c;
469 this_col = c->c_column;
473 } while (--nchars > 0 && this_col == endc->c_column);
475 /* if -b only print last character */
479 this_col + c->c_width > endc->c_column)
483 if (this_col > last_col) {
484 int nspace = this_col - last_col;
486 if (compress_spaces && nspace > 1) {
488 int tab_col, tab_size;
490 tab_col = (last_col + 8) & ~7;
491 if (tab_col > this_col)
493 tab_size = tab_col - last_col;
502 while (--nspace >= 0)
508 if (c->c_set != last_set) {
520 for (j = 0; j < c->c_width; j++)
525 last_col += (c - 1)->c_width;
530 * Increment or decrement a line number, checking for overflow.
531 * Stop one below INT_MAX such that the adjust variable is safe.
534 addto_lineno(int *lno, int offset)
537 if (*lno >= INT_MAX - offset)
538 errx(1, "too many lines");
540 if (*lno < INT_MIN - offset)
541 errx(1, "too many reverse line feeds");
548 static LINE *line_freelist;
556 if (!line_freelist) {
557 if ((l = realloc(NULL, sizeof(LINE) * NALLOC)) == NULL)
560 for (i = 1; i < NALLOC; i++, l++)
565 line_freelist = l->l_next;
567 memset(l, 0, sizeof(LINE));
575 l->l_next = line_freelist;
583 (void)fprintf(stderr, "usage: col [-bfhpx] [-l nline]\n");
591 warnx("warning: can't back up %s",
592 line < 0 ? "past first line" : "-- line already flushed");