2 * Copyright (c) 1990, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Michael Rendell of the Memorial University of Newfoundland.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 static const char copyright[] =
35 "@(#) Copyright (c) 1990, 1993, 1994\n\
36 The Regents of the University of California. All rights reserved.\n";
41 static char sccsid[] = "@(#)col.c 8.5 (Berkeley) 5/4/95";
45 #include <sys/cdefs.h>
46 __FBSDID("$FreeBSD$");
48 #include <sys/capsicum.h>
50 #include <capsicum_helpers.h>
62 #define BS '\b' /* backspace */
63 #define TAB '\t' /* tab */
64 #define SPACE ' ' /* space */
65 #define NL '\n' /* newline */
66 #define CR '\r' /* carriage return */
67 #define ESC '\033' /* escape */
68 #define SI '\017' /* shift in to normal character set */
69 #define SO '\016' /* shift out to alternate character set */
70 #define VT '\013' /* vertical tab (aka reverse line feed) */
71 #define RLF '7' /* ESC-7 reverse line feed */
72 #define RHLF '8' /* ESC-8 reverse half-line feed */
73 #define FHLF '9' /* ESC-9 forward half-line feed */
75 /* build up at least this many lines before flushing them out */
76 #define BUFFER_MARGIN 32
80 typedef struct char_str {
82 #define CS_ALTERNATE 2
83 short c_column; /* column character is in */
84 CSET c_set; /* character set (currently only 2) */
85 wchar_t c_char; /* character in question */
86 int c_width; /* character width */
89 typedef struct line_str LINE;
91 CHAR *l_line; /* characters on the line */
92 LINE *l_prev; /* previous line */
93 LINE *l_next; /* next line */
94 int l_lsize; /* allocated sizeof l_line */
95 int l_line_len; /* strlen(l_line) */
96 int l_needs_sort; /* set if chars went in out of order */
97 int l_max_col; /* max column in the line */
100 static void addto_lineno(int *, int);
101 static LINE *alloc_line(void);
102 static void dowarn(int);
103 static void flush_line(LINE *);
104 static void flush_lines(int);
105 static void flush_blanks(void);
106 static void free_line(LINE *);
107 static void usage(void);
109 static CSET last_set; /* char_set of last char printed */
111 static int compress_spaces; /* if doing space -> tab conversion */
112 static int fine; /* if `fine' resolution (half lines) */
113 static int max_bufd_lines; /* max # of half lines to keep in memory */
114 static int nblank_lines; /* # blanks after last flushed line */
115 static int no_backspaces; /* if not to output any backspaces */
116 static int pass_unknown_seqs; /* pass unknown control sequences */
120 if (putwchar(ch) == WEOF) \
121 errx(1, "write error"); \
125 main(int argc, char **argv)
129 CSET cur_set; /* current character set */
130 LINE *l; /* current line */
131 int extra_lines; /* # of lines above first line */
132 int cur_col; /* current column */
133 int cur_line; /* line number of current position */
134 int max_line; /* max value of cur_line */
135 int this_line; /* line l points to */
136 int nflushd_lines; /* number of lines that were flushed */
137 int adjust, opt, warned, width;
140 (void)setlocale(LC_CTYPE, "");
142 if (caph_limit_stdio() == -1)
143 err(1, "unable to limit stdio");
145 if (cap_enter() < 0 && errno != ENOSYS)
146 err(1, "unable to enter capability mode");
148 max_bufd_lines = 256;
149 compress_spaces = 1; /* compress spaces into tabs */
150 while ((opt = getopt(argc, argv, "bfhl:px")) != -1)
152 case 'b': /* do not output backspaces */
155 case 'f': /* allow half forward line feeds */
158 case 'h': /* compress spaces into tabs */
161 case 'l': /* buffered line count */
162 max_bufd_lines = strtonum(optarg, 1,
163 (INT_MAX - BUFFER_MARGIN) / 2, &errstr) * 2;
165 errx(1, "bad -l argument, %s: %s", errstr,
168 case 'p': /* pass unknown control sequences */
169 pass_unknown_seqs = 1;
171 case 'x': /* do not compress spaces into tabs */
182 adjust = cur_col = extra_lines = warned = 0;
183 cur_line = max_line = nflushd_lines = this_line = 0;
184 cur_set = last_set = CS_NORMAL;
185 lines = l = alloc_line();
187 while ((ch = getwchar()) != WEOF) {
190 case BS: /* can't go back further */
198 case ESC: /* just ignore EOF */
201 * In the input stream, accept both the
202 * XPG5 sequences ESC-digit and the
203 * traditional BSD sequences ESC-ctrl.
208 addto_lineno(&cur_line, -2);
213 addto_lineno(&cur_line, -1);
218 addto_lineno(&cur_line, 1);
219 if (cur_line > max_line)
224 addto_lineno(&cur_line, 2);
225 if (cur_line > max_line)
236 cur_set = CS_ALTERNATE;
238 case TAB: /* adjust column */
243 addto_lineno(&cur_line, -2);
247 if ((width = wcwidth(ch)) > 0)
251 if (!pass_unknown_seqs)
255 /* Must stuff ch in a line - are we at the right one? */
256 if (cur_line + adjust != this_line) {
259 /* round up to next line */
260 adjust = !fine && (cur_line & 1);
262 if (cur_line + adjust < this_line) {
263 while (cur_line + adjust < this_line &&
268 if (cur_line + adjust < this_line) {
269 if (nflushd_lines == 0) {
271 * Allow backup past first
272 * line if nothing has been
275 while (cur_line + adjust
287 cur_line = this_line - adjust;
291 /* may need to allocate here */
292 while (cur_line + adjust > this_line) {
293 if (l->l_next == NULL) {
294 l->l_next = alloc_line();
295 l->l_next->l_prev = l;
301 if (this_line > nflushd_lines &&
302 this_line - nflushd_lines >=
303 max_bufd_lines + BUFFER_MARGIN) {
305 flush_lines(extra_lines);
308 flush_lines(this_line - nflushd_lines -
310 nflushd_lines = this_line - max_bufd_lines;
313 /* grow line's buffer? */
314 if (l->l_line_len + 1 >= l->l_lsize) {
317 need = l->l_lsize ? l->l_lsize * 2 : 90;
318 if ((l->l_line = realloc(l->l_line,
319 (unsigned)need * sizeof(CHAR))) == NULL)
323 c = &l->l_line[l->l_line_len++];
326 c->c_column = cur_col;
327 c->c_width = wcwidth(ch);
329 * If things are put in out of order, they will need sorting
330 * when it is flushed.
332 if (cur_col < l->l_max_col)
335 l->l_max_col = cur_col;
337 cur_col += c->c_width;
342 flush_lines(extra_lines);
344 /* goto the last line that had a character on it */
345 for (; l->l_next; l = l->l_next)
347 flush_lines(this_line - nflushd_lines + 1);
349 /* make sure we leave things in a sane state */
350 if (last_set != CS_NORMAL)
353 /* flush out the last few blank lines */
354 if (max_line > this_line)
355 nblank_lines = max_line - this_line;
363 flush_lines(int nflush)
367 while (--nflush >= 0) {
374 if (l->l_line || l->l_next)
377 (void)free(l->l_line);
381 lines->l_prev = NULL;
385 * Print a number of newline/half newlines. If fine flag is set, nblank_lines
386 * is the number of half line feeds, otherwise it is the number of whole line
403 for (i = nb; --i >= 0;)
415 * Write a line to stdout taking care of space to tab conversion (-h flag)
416 * and character set shifts.
422 int i, j, nchars, last_col, save, this_col, tot;
425 nchars = l->l_line_len;
427 if (l->l_needs_sort) {
429 static int count_size, *count, sorted_size;
432 * Do an O(n) sort on l->l_line by column being careful to
433 * preserve the order of characters in the same column.
435 if (l->l_lsize > sorted_size) {
436 sorted_size = l->l_lsize;
437 if ((sorted = realloc(sorted,
438 (unsigned)sizeof(CHAR) * sorted_size)) == NULL)
441 if (l->l_max_col >= count_size) {
442 count_size = l->l_max_col + 1;
443 if ((count = realloc(count,
444 (unsigned)sizeof(int) * count_size)) == NULL)
447 memset(count, 0, sizeof(int) * l->l_max_col + 1);
448 for (i = nchars, c = l->l_line; --i >= 0; c++)
449 count[c->c_column]++;
452 * calculate running total (shifted down by 1) to use as
453 * indices into new line.
455 for (tot = 0, i = 0; i <= l->l_max_col; i++) {
461 for (i = nchars, c = l->l_line; --i >= 0; c++)
462 sorted[count[c->c_column]++] = *c;
467 this_col = c->c_column;
471 } while (--nchars > 0 && this_col == endc->c_column);
473 /* if -b only print last character */
477 this_col + c->c_width > endc->c_column)
481 if (this_col > last_col) {
482 int nspace = this_col - last_col;
484 if (compress_spaces && nspace > 1) {
486 int tab_col, tab_size;
488 tab_col = (last_col + 8) & ~7;
489 if (tab_col > this_col)
491 tab_size = tab_col - last_col;
500 while (--nspace >= 0)
506 if (c->c_set != last_set) {
518 for (j = 0; j < c->c_width; j++)
523 last_col += (c - 1)->c_width;
528 * Increment or decrement a line number, checking for overflow.
529 * Stop one below INT_MAX such that the adjust variable is safe.
532 addto_lineno(int *lno, int offset)
535 if (*lno >= INT_MAX - offset)
536 errx(1, "too many lines");
538 if (*lno < INT_MIN - offset)
539 errx(1, "too many reverse line feeds");
546 static LINE *line_freelist;
554 if (!line_freelist) {
555 if ((l = realloc(NULL, sizeof(LINE) * NALLOC)) == NULL)
558 for (i = 1; i < NALLOC; i++, l++)
563 line_freelist = l->l_next;
565 memset(l, 0, sizeof(LINE));
573 l->l_next = line_freelist;
581 (void)fprintf(stderr, "usage: col [-bfhpx] [-l nline]\n");
589 warnx("warning: can't back up %s",
590 line < 0 ? "past first line" : "-- line already flushed");