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 $
49 #define BS '\b' /* backspace */
50 #define TAB '\t' /* tab */
51 #define SPACE ' ' /* space */
52 #define NL '\n' /* newline */
53 #define CR '\r' /* carriage return */
54 #define ESC '\033' /* escape */
55 #define SI '\017' /* shift in to normal character set */
56 #define SO '\016' /* shift out to alternate character set */
57 #define VT '\013' /* vertical tab (aka reverse line feed) */
58 #define RLF '7' /* ESC-7 reverse line feed */
59 #define RHLF '8' /* ESC-8 reverse half-line feed */
60 #define FHLF '9' /* ESC-9 forward half-line feed */
62 /* build up at least this many lines before flushing them out */
63 #define BUFFER_MARGIN 32
67 typedef struct char_str
{
69 #define CS_ALTERNATE 2
70 short c_column
; /* column character is in */
71 CSET c_set
; /* character set (currently only 2) */
72 wchar_t c_char
; /* character in question */
73 int c_width
; /* character width */
76 typedef struct line_str LINE
;
78 CHAR
*l_line
; /* characters on the line */
79 LINE
*l_prev
; /* previous line */
80 LINE
*l_next
; /* next line */
81 int l_lsize
; /* allocated sizeof l_line */
82 int l_line_len
; /* strlen(l_line) */
83 int l_needs_sort
; /* set if chars went in out of order */
84 int l_max_col
; /* max column in the line */
87 static void addto_lineno(int *, int);
88 static LINE
*alloc_line(void);
89 static void dowarn(int);
90 static void flush_line(LINE
*);
91 static void flush_lines(int);
92 static void flush_blanks(void);
93 static void free_line(LINE
*);
94 static void usage(void);
96 static CSET last_set
; /* char_set of last char printed */
98 static int compress_spaces
; /* if doing space -> tab conversion */
99 static int fine
; /* if `fine' resolution (half lines) */
100 static int max_bufd_lines
; /* max # of half lines to keep in memory */
101 static int nblank_lines
; /* # blanks after last flushed line */
102 static int no_backspaces
; /* if not to output any backspaces */
103 static int pass_unknown_seqs
; /* pass unknown control sequences */
107 if (putwchar(ch) == WEOF) \
108 errx(1, "write error"); \
112 main(int argc
, char **argv
)
116 CSET cur_set
; /* current character set */
117 LINE
*l
; /* current line */
118 int extra_lines
; /* # of lines above first line */
119 int cur_col
; /* current column */
120 int cur_line
; /* line number of current position */
121 int max_line
; /* max value of cur_line */
122 int this_line
; /* line l points to */
123 int nflushd_lines
; /* number of lines that were flushed */
124 int adjust
, opt
, warned
, width
;
127 setlocale(LC_CTYPE
, "");
129 max_bufd_lines
= 256;
130 compress_spaces
= 1; /* compress spaces into tabs */
131 while ((opt
= getopt(argc
, argv
, "bfhl:px")) != -1)
133 case 'b': /* do not output backspaces */
136 case 'f': /* allow half forward line feeds */
139 case 'h': /* compress spaces into tabs */
142 case 'l': /* buffered line count */
143 max_bufd_lines
= strtonum(optarg
, 1,
144 (INT_MAX
- BUFFER_MARGIN
) / 2, &errstr
) * 2;
146 errx(1, "bad -l argument, %s: %s", errstr
,
149 case 'p': /* pass unknown control sequences */
150 pass_unknown_seqs
= 1;
152 case 'x': /* do not compress spaces into tabs */
163 adjust
= cur_col
= extra_lines
= warned
= 0;
164 cur_line
= max_line
= nflushd_lines
= this_line
= 0;
165 cur_set
= last_set
= CS_NORMAL
;
166 lines
= l
= alloc_line();
168 while ((ch
= getwchar()) != WEOF
) {
171 case BS
: /* can't go back further */
179 case ESC
: /* just ignore EOF */
180 switch (getwchar()) {
182 * In the input stream, accept both the
183 * XPG5 sequences ESC-digit and the
184 * traditional BSD sequences ESC-ctrl.
189 addto_lineno(&cur_line
, -2);
194 addto_lineno(&cur_line
, -1);
199 addto_lineno(&cur_line
, 1);
200 if (cur_line
> max_line
)
205 addto_lineno(&cur_line
, 2);
206 if (cur_line
> max_line
)
217 cur_set
= CS_ALTERNATE
;
219 case TAB
: /* adjust column */
224 addto_lineno(&cur_line
, -2);
228 if ((width
= wcwidth(ch
)) > 0)
232 if (!pass_unknown_seqs
)
236 /* Must stuff ch in a line - are we at the right one? */
237 if (cur_line
+ adjust
!= this_line
) {
240 /* round up to next line */
241 adjust
= !fine
&& (cur_line
& 1);
243 if (cur_line
+ adjust
< this_line
) {
244 while (cur_line
+ adjust
< this_line
&&
249 if (cur_line
+ adjust
< this_line
) {
250 if (nflushd_lines
== 0) {
252 * Allow backup past first
253 * line if nothing has been
256 while (cur_line
+ adjust
268 cur_line
= this_line
- adjust
;
272 /* may need to allocate here */
273 while (cur_line
+ adjust
> this_line
) {
274 if (l
->l_next
== NULL
) {
275 l
->l_next
= alloc_line();
276 l
->l_next
->l_prev
= l
;
282 if (this_line
> nflushd_lines
&&
283 this_line
- nflushd_lines
>=
284 max_bufd_lines
+ BUFFER_MARGIN
) {
286 flush_lines(extra_lines
);
289 flush_lines(this_line
- nflushd_lines
-
291 nflushd_lines
= this_line
- max_bufd_lines
;
294 /* grow line's buffer? */
295 if (l
->l_line_len
+ 1 >= l
->l_lsize
) {
298 need
= l
->l_lsize
? l
->l_lsize
* 2 : 90;
299 if ((l
->l_line
= realloc(l
->l_line
,
300 (unsigned)need
* sizeof(CHAR
))) == NULL
)
304 c
= &l
->l_line
[l
->l_line_len
++];
307 c
->c_column
= cur_col
;
308 c
->c_width
= wcwidth(ch
);
310 * If things are put in out of order, they will need sorting
311 * when it is flushed.
313 if (cur_col
< l
->l_max_col
)
316 l
->l_max_col
= cur_col
;
318 cur_col
+= c
->c_width
;
323 flush_lines(extra_lines
);
325 /* goto the last line that had a character on it */
326 for (; l
->l_next
; l
= l
->l_next
)
328 flush_lines(this_line
- nflushd_lines
+ 1);
330 /* make sure we leave things in a sane state */
331 if (last_set
!= CS_NORMAL
)
334 /* flush out the last few blank lines */
335 if (max_line
> this_line
)
336 nblank_lines
= max_line
- this_line
;
344 flush_lines(int nflush
)
348 while (--nflush
>= 0) {
355 if (l
->l_line
|| l
->l_next
)
362 lines
->l_prev
= NULL
;
366 * Print a number of newline/half newlines. If fine flag is set, nblank_lines
367 * is the number of half line feeds, otherwise it is the number of whole line
384 for (i
= nb
; --i
>= 0;)
396 * Write a line to stdout taking care of space to tab conversion (-h flag)
397 * and character set shifts.
403 int i
, j
, nchars
, last_col
, save
, this_col
, tot
;
406 nchars
= l
->l_line_len
;
408 if (l
->l_needs_sort
) {
410 static int count_size
, *count
, sorted_size
;
413 * Do an O(n) sort on l->l_line by column being careful to
414 * preserve the order of characters in the same column.
416 if (l
->l_lsize
> sorted_size
) {
417 sorted_size
= l
->l_lsize
;
418 if ((sorted
= realloc(sorted
,
419 (unsigned)sizeof(CHAR
) * sorted_size
)) == NULL
)
422 if (l
->l_max_col
>= count_size
) {
423 count_size
= l
->l_max_col
+ 1;
424 if ((count
= realloc(count
,
425 (unsigned)sizeof(int) * count_size
)) == NULL
)
428 memset(count
, 0, sizeof(int) * l
->l_max_col
+ 1);
429 for (i
= nchars
, c
= l
->l_line
; --i
>= 0; c
++)
430 count
[c
->c_column
]++;
433 * calculate running total (shifted down by 1) to use as
434 * indices into new line.
436 for (tot
= 0, i
= 0; i
<= l
->l_max_col
; i
++) {
442 for (i
= nchars
, c
= l
->l_line
; --i
>= 0; c
++)
443 sorted
[count
[c
->c_column
]++] = *c
;
448 this_col
= c
->c_column
;
452 } while (--nchars
> 0 && this_col
== endc
->c_column
);
454 /* if -b only print last character */
458 this_col
+ c
->c_width
> endc
->c_column
)
462 if (this_col
> last_col
) {
463 int nspace
= this_col
- last_col
;
465 if (compress_spaces
&& nspace
> 1) {
467 int tab_col
, tab_size
;
469 tab_col
= (last_col
+ 8) & ~7;
470 if (tab_col
> this_col
)
472 tab_size
= tab_col
- last_col
;
481 while (--nspace
>= 0)
487 if (c
->c_set
!= last_set
) {
499 for (j
= 0; j
< c
->c_width
; j
++)
504 last_col
+= (c
- 1)->c_width
;
509 * Increment or decrement a line number, checking for overflow.
510 * Stop one below INT_MAX such that the adjust variable is safe.
513 addto_lineno(int *lno
, int offset
)
516 if (*lno
>= INT_MAX
- offset
)
517 errx(1, "too many lines");
519 if (*lno
< INT_MIN
- offset
)
520 errx(1, "too many reverse line feeds");
527 static LINE
*line_freelist
;
535 if (!line_freelist
) {
536 if ((l
= realloc(NULL
, sizeof(LINE
) * NALLOC
)) == NULL
)
539 for (i
= 1; i
< NALLOC
; i
++, l
++)
544 line_freelist
= l
->l_next
;
546 memset(l
, 0, sizeof(LINE
));
554 l
->l_next
= line_freelist
;
562 fprintf(stderr
, "usage: col [-bfhpx] [-l nline]\n");
570 warnx("warning: can't back up %s",
571 line
< 0 ? "past first line" : "-- line already flushed");