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
32 * @(#) Copyright (c) 1990, 1993, 1994 The Regents of the University of California. All rights reserved.
33 * @(#)col.c 8.5 (Berkeley) 5/4/95
34 * $FreeBSD: head/usr.bin/col/col.c 282722 2015-05-10 11:41:38Z bapt $
48 #define BS '\b' /* backspace */
49 #define TAB '\t' /* tab */
50 #define SPACE ' ' /* space */
51 #define NL '\n' /* newline */
52 #define CR '\r' /* carriage return */
53 #define ESC '\033' /* escape */
54 #define SI '\017' /* shift in to normal character set */
55 #define SO '\016' /* shift out to alternate character set */
56 #define VT '\013' /* vertical tab (aka reverse line feed) */
57 #define RLF '7' /* ESC-7 reverse line feed */
58 #define RHLF '8' /* ESC-8 reverse half-line feed */
59 #define FHLF '9' /* ESC-9 forward half-line feed */
61 /* build up at least this many lines before flushing them out */
62 #define BUFFER_MARGIN 32
66 typedef struct char_str {
68 #define CS_ALTERNATE 2
69 short c_column; /* column character is in */
70 CSET c_set; /* character set (currently only 2) */
71 wchar_t c_char; /* character in question */
72 int c_width; /* character width */
75 typedef struct line_str LINE;
77 CHAR *l_line; /* characters on the line */
78 LINE *l_prev; /* previous line */
79 LINE *l_next; /* next line */
80 int l_lsize; /* allocated sizeof l_line */
81 int l_line_len; /* strlen(l_line) */
82 int l_needs_sort; /* set if chars went in out of order */
83 int l_max_col; /* max column in the line */
86 static void addto_lineno(int *, int);
87 static LINE *alloc_line(void);
88 static void dowarn(int);
89 static void flush_line(LINE *);
90 static void flush_lines(int);
91 static void flush_blanks(void);
92 static void free_line(LINE *);
93 static void usage(void);
95 static CSET last_set; /* char_set of last char printed */
97 static int compress_spaces; /* if doing space -> tab conversion */
98 static int fine; /* if `fine' resolution (half lines) */
99 static int max_bufd_lines; /* max # of half lines to keep in memory */
100 static int nblank_lines; /* # blanks after last flushed line */
101 static int no_backspaces; /* if not to output any backspaces */
102 static int pass_unknown_seqs; /* pass unknown control sequences */
106 if (putwchar(ch) == WEOF) \
107 errx(1, "write error"); \
111 main(int argc, char **argv)
115 CSET cur_set; /* current character set */
116 LINE *l; /* current line */
117 int extra_lines; /* # of lines above first line */
118 int cur_col; /* current column */
119 int cur_line; /* line number of current position */
120 int max_line; /* max value of cur_line */
121 int this_line; /* line l points to */
122 int nflushd_lines; /* number of lines that were flushed */
123 int adjust, opt, warned, width;
126 setlocale(LC_CTYPE, "");
128 max_bufd_lines = 256;
129 compress_spaces = 1; /* compress spaces into tabs */
130 while ((opt = getopt(argc, argv, "bfhl:px")) != -1)
132 case 'b': /* do not output backspaces */
135 case 'f': /* allow half forward line feeds */
138 case 'h': /* compress spaces into tabs */
141 case 'l': /* buffered line count */
142 max_bufd_lines = strtonum(optarg, 1,
143 (INT_MAX - BUFFER_MARGIN) / 2, &errstr) * 2;
145 errx(1, "bad -l argument, %s: %s", errstr,
148 case 'p': /* pass unknown control sequences */
149 pass_unknown_seqs = 1;
151 case 'x': /* do not compress spaces into tabs */
162 adjust = cur_col = extra_lines = warned = 0;
163 cur_line = max_line = nflushd_lines = this_line = 0;
164 cur_set = last_set = CS_NORMAL;
165 lines = l = alloc_line();
167 while ((ch = getwchar()) != WEOF) {
170 case BS: /* can't go back further */
178 case ESC: /* just ignore EOF */
179 switch (getwchar()) {
181 * In the input stream, accept both the
182 * XPG5 sequences ESC-digit and the
183 * traditional BSD sequences ESC-ctrl.
188 addto_lineno(&cur_line, -2);
193 addto_lineno(&cur_line, -1);
198 addto_lineno(&cur_line, 1);
199 if (cur_line > max_line)
204 addto_lineno(&cur_line, 2);
205 if (cur_line > max_line)
216 cur_set = CS_ALTERNATE;
218 case TAB: /* adjust column */
223 addto_lineno(&cur_line, -2);
227 if ((width = wcwidth(ch)) > 0)
231 if (!pass_unknown_seqs)
235 /* Must stuff ch in a line - are we at the right one? */
236 if (cur_line + adjust != this_line) {
239 /* round up to next line */
240 adjust = !fine && (cur_line & 1);
242 if (cur_line + adjust < this_line) {
243 while (cur_line + adjust < this_line &&
248 if (cur_line + adjust < this_line) {
249 if (nflushd_lines == 0) {
251 * Allow backup past first
252 * line if nothing has been
255 while (cur_line + adjust
267 cur_line = this_line - adjust;
271 /* may need to allocate here */
272 while (cur_line + adjust > this_line) {
273 if (l->l_next == NULL) {
274 l->l_next = alloc_line();
275 l->l_next->l_prev = l;
281 if (this_line > nflushd_lines &&
282 this_line - nflushd_lines >=
283 max_bufd_lines + BUFFER_MARGIN) {
285 flush_lines(extra_lines);
288 flush_lines(this_line - nflushd_lines -
290 nflushd_lines = this_line - max_bufd_lines;
293 /* grow line's buffer? */
294 if (l->l_line_len + 1 >= l->l_lsize) {
297 need = l->l_lsize ? l->l_lsize * 2 : 90;
298 if ((l->l_line = realloc(l->l_line,
299 (unsigned)need * sizeof(CHAR))) == NULL)
303 c = &l->l_line[l->l_line_len++];
306 c->c_column = cur_col;
307 c->c_width = wcwidth(ch);
309 * If things are put in out of order, they will need sorting
310 * when it is flushed.
312 if (cur_col < l->l_max_col)
315 l->l_max_col = cur_col;
317 cur_col += c->c_width;
322 flush_lines(extra_lines);
324 /* goto the last line that had a character on it */
325 for (; l->l_next; l = l->l_next)
327 flush_lines(this_line - nflushd_lines + 1);
329 /* make sure we leave things in a sane state */
330 if (last_set != CS_NORMAL)
333 /* flush out the last few blank lines */
334 if (max_line > this_line)
335 nblank_lines = max_line - this_line;
343 flush_lines(int nflush)
347 while (--nflush >= 0) {
354 if (l->l_line || l->l_next)
361 lines->l_prev = NULL;
365 * Print a number of newline/half newlines. If fine flag is set, nblank_lines
366 * is the number of half line feeds, otherwise it is the number of whole line
383 for (i = nb; --i >= 0;)
395 * Write a line to stdout taking care of space to tab conversion (-h flag)
396 * and character set shifts.
402 int i, j, nchars, last_col, save, this_col, tot;
405 nchars = l->l_line_len;
407 if (l->l_needs_sort) {
409 static int count_size, *count, sorted_size;
412 * Do an O(n) sort on l->l_line by column being careful to
413 * preserve the order of characters in the same column.
415 if (l->l_lsize > sorted_size) {
416 sorted_size = l->l_lsize;
417 if ((sorted = realloc(sorted,
418 (unsigned)sizeof(CHAR) * sorted_size)) == NULL)
421 if (l->l_max_col >= count_size) {
422 count_size = l->l_max_col + 1;
423 if ((count = realloc(count,
424 (unsigned)sizeof(int) * count_size)) == NULL)
427 memset(count, 0, sizeof(int) * l->l_max_col + 1);
428 for (i = nchars, c = l->l_line; --i >= 0; c++)
429 count[c->c_column]++;
432 * calculate running total (shifted down by 1) to use as
433 * indices into new line.
435 for (tot = 0, i = 0; i <= l->l_max_col; i++) {
441 for (i = nchars, c = l->l_line; --i >= 0; c++)
442 sorted[count[c->c_column]++] = *c;
447 this_col = c->c_column;
451 } while (--nchars > 0 && this_col == endc->c_column);
453 /* if -b only print last character */
457 this_col + c->c_width > endc->c_column)
461 if (this_col > last_col) {
462 int nspace = this_col - last_col;
464 if (compress_spaces && nspace > 1) {
466 int tab_col, tab_size;
468 tab_col = (last_col + 8) & ~7;
469 if (tab_col > this_col)
471 tab_size = tab_col - last_col;
480 while (--nspace >= 0)
486 if (c->c_set != last_set) {
498 for (j = 0; j < c->c_width; j++)
503 last_col += (c - 1)->c_width;
508 * Increment or decrement a line number, checking for overflow.
509 * Stop one below INT_MAX such that the adjust variable is safe.
512 addto_lineno(int *lno, int offset)
515 if (*lno >= INT_MAX - offset)
516 errx(1, "too many lines");
518 if (*lno < INT_MIN - offset)
519 errx(1, "too many reverse line feeds");
526 static LINE *line_freelist;
534 if (!line_freelist) {
535 if ((l = realloc(NULL, sizeof(LINE) * NALLOC)) == NULL)
538 for (i = 1; i < NALLOC; i++, l++)
543 line_freelist = l->l_next;
545 memset(l, 0, sizeof(LINE));
553 l->l_next = line_freelist;
561 fprintf(stderr, "usage: col [-bfhpx] [-l nline]\n");
569 warnx("warning: can't back up %s",
570 line < 0 ? "past first line" : "-- line already flushed");