1 /* From https://svnweb.freebsd.org/base/head/usr.bin/col/, r282722 */
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 * 4. 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>
61 #define BS '\b' /* backspace */
62 #define TAB '\t' /* tab */
63 #define SPACE ' ' /* space */
64 #define NL '\n' /* newline */
65 #define CR '\r' /* carriage return */
66 #define ESC '\033' /* escape */
67 #define SI '\017' /* shift in to normal character set */
68 #define SO '\016' /* shift out to alternate character set */
69 #define VT '\013' /* vertical tab (aka reverse line feed) */
70 #define RLF '7' /* ESC-7 reverse line feed */
71 #define RHLF '8' /* ESC-8 reverse half-line feed */
72 #define FHLF '9' /* ESC-9 forward half-line feed */
74 /* build up at least this many lines before flushing them out */
75 #define BUFFER_MARGIN 32
79 typedef struct char_str {
81 #define CS_ALTERNATE 2
82 short c_column; /* column character is in */
83 CSET c_set; /* character set (currently only 2) */
84 wchar_t c_char; /* character in question */
85 int c_width; /* character width */
88 typedef struct line_str LINE;
90 CHAR *l_line; /* characters on the line */
91 LINE *l_prev; /* previous line */
92 LINE *l_next; /* next line */
93 int l_lsize; /* allocated sizeof l_line */
94 int l_line_len; /* strlen(l_line) */
95 int l_needs_sort; /* set if chars went in out of order */
96 int l_max_col; /* max column in the line */
99 static void addto_lineno(int *, int);
100 static LINE *alloc_line(void);
101 static void dowarn(int);
102 static void flush_line(LINE *);
103 static void flush_lines(int);
104 static void flush_blanks(void);
105 static void free_line(LINE *);
106 static void usage(void);
108 static CSET last_set; /* char_set of last char printed */
110 static int compress_spaces; /* if doing space -> tab conversion */
111 static int fine; /* if `fine' resolution (half lines) */
112 static int max_bufd_lines; /* max # of half lines to keep in memory */
113 static int nblank_lines; /* # blanks after last flushed line */
114 static int no_backspaces; /* if not to output any backspaces */
115 static int pass_unknown_seqs; /* pass unknown control sequences */
119 if (putwchar(ch) == WEOF) \
120 errx(1, "write error"); \
124 main(int argc, char **argv)
128 CSET cur_set; /* current character set */
129 LINE *l; /* current line */
130 int extra_lines; /* # of lines above first line */
131 int cur_col; /* current column */
132 int cur_line; /* line number of current position */
133 int max_line; /* max value of cur_line */
134 int this_line; /* line l points to */
135 int nflushd_lines; /* number of lines that were flushed */
136 int adjust, opt, warned, width;
140 (void)setlocale(LC_CTYPE, "");
142 max_bufd_lines = 256;
143 compress_spaces = 1; /* compress spaces into tabs */
144 while ((opt = getopt(argc, argv, "bfhl:px")) != -1)
146 case 'b': /* do not output backspaces */
149 case 'f': /* allow half forward line feeds */
152 case 'h': /* compress spaces into tabs */
155 case 'l': /* buffered line count */
156 max_bufd_lines = atoi(optarg);
158 case 'p': /* pass unknown control sequences */
159 pass_unknown_seqs = 1;
161 case 'x': /* do not compress spaces into tabs */
172 adjust = cur_col = extra_lines = warned = 0;
173 cur_line = max_line = nflushd_lines = this_line = 0;
174 cur_set = last_set = CS_NORMAL;
175 lines = l = alloc_line();
177 while ((ch = getwchar()) != WEOF) {
180 case BS: /* can't go back further */
188 case ESC: /* just ignore EOF */
191 * In the input stream, accept both the
192 * XPG5 sequences ESC-digit and the
193 * traditional BSD sequences ESC-ctrl.
198 addto_lineno(&cur_line, -2);
203 addto_lineno(&cur_line, -1);
208 addto_lineno(&cur_line, 1);
209 if (cur_line > max_line)
214 addto_lineno(&cur_line, 2);
215 if (cur_line > max_line)
226 cur_set = CS_ALTERNATE;
228 case TAB: /* adjust column */
233 addto_lineno(&cur_line, -2);
237 if ((width = wcwidth(ch)) > 0)
241 if (!pass_unknown_seqs)
245 /* Must stuff ch in a line - are we at the right one? */
246 if (cur_line + adjust != this_line) {
249 /* round up to next line */
250 adjust = !fine && (cur_line & 1);
252 if (cur_line + adjust < this_line) {
253 while (cur_line + adjust < this_line &&
258 if (cur_line + adjust < this_line) {
259 if (nflushd_lines == 0) {
261 * Allow backup past first
262 * line if nothing has been
265 while (cur_line + adjust
277 cur_line = this_line - adjust;
281 /* may need to allocate here */
282 while (cur_line + adjust > this_line) {
283 if (l->l_next == NULL) {
284 l->l_next = alloc_line();
285 l->l_next->l_prev = l;
291 if (this_line > nflushd_lines &&
292 this_line - nflushd_lines >=
293 max_bufd_lines + BUFFER_MARGIN) {
295 flush_lines(extra_lines);
298 flush_lines(this_line - nflushd_lines -
300 nflushd_lines = this_line - max_bufd_lines;
303 /* grow line's buffer? */
304 if (l->l_line_len + 1 >= l->l_lsize) {
307 need = l->l_lsize ? l->l_lsize * 2 : 90;
308 if ((l->l_line = realloc(l->l_line,
309 (unsigned)need * sizeof(CHAR))) == NULL)
313 c = &l->l_line[l->l_line_len++];
316 c->c_column = cur_col;
317 c->c_width = wcwidth(ch);
319 * If things are put in out of order, they will need sorting
320 * when it is flushed.
322 if (cur_col < l->l_max_col)
325 l->l_max_col = cur_col;
327 cur_col += c->c_width;
332 flush_lines(extra_lines);
334 /* goto the last line that had a character on it */
335 for (; l->l_next; l = l->l_next)
337 flush_lines(this_line - nflushd_lines + 1);
339 /* make sure we leave things in a sane state */
340 if (last_set != CS_NORMAL)
343 /* flush out the last few blank lines */
344 if (max_line > this_line)
345 nblank_lines = max_line - this_line;
353 flush_lines(int nflush)
357 while (--nflush >= 0) {
364 if (l->l_line || l->l_next)
367 (void)free(l->l_line);
371 lines->l_prev = NULL;
375 * Print a number of newline/half newlines. If fine flag is set, nblank_lines
376 * is the number of half line feeds, otherwise it is the number of whole line
393 for (i = nb; --i >= 0;)
405 * Write a line to stdout taking care of space to tab conversion (-h flag)
406 * and character set shifts.
412 int i, j, nchars, last_col, save, this_col, tot;
415 nchars = l->l_line_len;
417 if (l->l_needs_sort) {
419 static int count_size, *count, sorted_size;
422 * Do an O(n) sort on l->l_line by column being careful to
423 * preserve the order of characters in the same column.
425 if (l->l_lsize > sorted_size) {
426 sorted_size = l->l_lsize;
427 if ((sorted = realloc(sorted,
428 (unsigned)sizeof(CHAR) * sorted_size)) == NULL)
431 if (l->l_max_col >= count_size) {
432 count_size = l->l_max_col + 1;
433 if ((count = realloc(count,
434 (unsigned)sizeof(int) * count_size)) == NULL)
437 memset(count, 0, sizeof(int) * l->l_max_col + 1);
438 for (i = nchars, c = l->l_line; --i >= 0; c++)
439 count[c->c_column]++;
442 * calculate running total (shifted down by 1) to use as
443 * indices into new line.
445 for (tot = 0, i = 0; i <= l->l_max_col; i++) {
451 for (i = nchars, c = l->l_line; --i >= 0; c++)
452 sorted[count[c->c_column]++] = *c;
457 this_col = c->c_column;
461 } while (--nchars > 0 && this_col == endc->c_column);
463 /* if -b only print last character */
467 this_col + c->c_width > endc->c_column)
471 if (this_col > last_col) {
472 int nspace = this_col - last_col;
474 if (compress_spaces && nspace > 1) {
476 int tab_col, tab_size;
478 tab_col = (last_col + 8) & ~7;
479 if (tab_col > this_col)
481 tab_size = tab_col - last_col;
490 while (--nspace >= 0)
496 if (c->c_set != last_set) {
508 for (j = 0; j < c->c_width; j++)
513 last_col += (c - 1)->c_width;
518 * Increment or decrement a line number, checking for overflow.
519 * Stop one below INT_MAX such that the adjust variable is safe.
522 addto_lineno(int *lno, int offset)
525 if (*lno >= INT_MAX - offset)
526 errx(1, "too many lines");
528 if (*lno < INT_MIN - offset)
529 errx(1, "too many reverse line feeds");
536 static LINE *line_freelist;
544 if (!line_freelist) {
545 if ((l = realloc(NULL, sizeof(LINE) * NALLOC)) == NULL)
548 for (i = 1; i < NALLOC; i++, l++)
553 line_freelist = l->l_next;
555 memset(l, 0, sizeof(LINE));
563 l->l_next = line_freelist;
571 (void)fprintf(stderr, "usage: col [-bfhpx] [-l nline]\n");
579 warnx("warning: can't back up %s",
580 line < 0 ? "past first line" : "-- line already flushed");