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 * 4. 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: src/usr.bin/col/col.c,v 1.6.6.4 2001/08/02 01:27:12 obrien Exp $
35 * $DragonFly: src/usr.bin/col/col.c,v 1.5 2006/08/03 16:40:48 swildner Exp $
46 #define BS '\b' /* backspace */
47 #define TAB '\t' /* tab */
48 #define SPACE ' ' /* space */
49 #define NL '\n' /* newline */
50 #define CR '\r' /* carriage return */
51 #define ESC '\033' /* escape */
52 #define SI '\017' /* shift in to normal character set */
53 #define SO '\016' /* shift out to alternate character set */
54 #define VT '\013' /* vertical tab (aka reverse line feed) */
55 #define RLF '\007' /* ESC-07 reverse line feed */
56 #define RHLF '\010' /* ESC-010 reverse half-line feed */
57 #define FHLF '\011' /* ESC-011 forward half-line feed */
59 /* build up at least this many lines before flushing them out */
60 #define BUFFER_MARGIN 32
64 typedef struct char_str
{
66 #define CS_ALTERNATE 2
67 short c_column
; /* column character is in */
68 CSET c_set
; /* character set (currently only 2) */
69 char c_char
; /* character in question */
72 typedef struct line_str LINE
;
74 CHAR
*l_line
; /* characters on the line */
75 LINE
*l_prev
; /* previous line */
76 LINE
*l_next
; /* next line */
77 int l_lsize
; /* allocated sizeof l_line */
78 int l_line_len
; /* strlen(l_line) */
79 int l_needs_sort
; /* set if chars went in out of order */
80 int l_max_col
; /* max column in the line */
83 LINE
*alloc_line(void);
85 void flush_line(LINE
*);
86 void flush_lines(int);
87 void flush_blanks(void);
88 void free_line(LINE
*);
89 int main(int, char **);
92 CSET last_set
; /* char_set of last char printed */
94 int compress_spaces
; /* if doing space -> tab conversion */
95 int fine
; /* if `fine' resolution (half lines) */
96 int max_bufd_lines
; /* max # lines to keep in memory */
97 int nblank_lines
; /* # blanks after last flushed line */
98 int no_backspaces
; /* if not to output any backspaces */
99 int pass_unknown_seqs
; /* pass unknown control sequences */
103 if (putchar(ch) == EOF) \
104 errx(1, "write error"); \
108 main(int argc
, char **argv
)
112 CSET cur_set
; /* current character set */
113 LINE
*l
; /* current line */
114 int extra_lines
; /* # of lines above first line */
115 int cur_col
; /* current column */
116 int cur_line
; /* line number of current position */
117 int max_line
; /* max value of cur_line */
118 int this_line
; /* line l points to */
119 int nflushd_lines
; /* number of lines that were flushed */
120 int adjust
, opt
, warned
;
122 (void)setlocale(LC_CTYPE
, "");
124 max_bufd_lines
= 128;
125 compress_spaces
= 1; /* compress spaces into tabs */
126 while ((opt
= getopt(argc
, argv
, "bfhl:px")) != -1)
128 case 'b': /* do not output backspaces */
131 case 'f': /* allow half forward line feeds */
134 case 'h': /* compress spaces into tabs */
137 case 'l': /* buffered line count */
138 if ((max_bufd_lines
= atoi(optarg
)) <= 0)
139 errx(1, "bad -l argument %s", optarg
);
141 case 'p': /* pass unknown control sequences */
142 pass_unknown_seqs
= 1;
144 case 'x': /* do not compress spaces into tabs */
155 /* this value is in half lines */
158 adjust
= cur_col
= extra_lines
= warned
= 0;
159 cur_line
= max_line
= nflushd_lines
= this_line
= 0;
160 cur_set
= last_set
= CS_NORMAL
;
161 lines
= l
= alloc_line();
163 while ((ch
= getchar()) != EOF
) {
166 case BS
: /* can't go back further */
174 case ESC
: /* just ignore EOF */
184 if (cur_line
> max_line
)
190 if (cur_line
> max_line
)
201 cur_set
= CS_ALTERNATE
;
203 case TAB
: /* adjust column */
211 if (!pass_unknown_seqs
)
215 /* Must stuff ch in a line - are we at the right one? */
216 if (cur_line
!= this_line
- adjust
) {
221 nmove
= cur_line
- this_line
;
223 /* round up to next line */
230 for (; nmove
< 0 && l
->l_prev
; nmove
++)
233 if (nflushd_lines
== 0) {
235 * Allow backup past first
236 * line if nothing has been
239 for (; nmove
< 0; nmove
++) {
253 /* may need to allocate here */
254 for (; nmove
> 0 && l
->l_next
; nmove
--)
256 for (; nmove
> 0; nmove
--) {
263 this_line
= cur_line
+ adjust
;
264 nmove
= this_line
- nflushd_lines
;
265 if (nmove
>= max_bufd_lines
+ BUFFER_MARGIN
) {
266 nflushd_lines
+= nmove
- max_bufd_lines
;
267 flush_lines(nmove
- max_bufd_lines
);
270 /* grow line's buffer? */
271 if (l
->l_line_len
+ 1 >= l
->l_lsize
) {
274 need
= l
->l_lsize
? l
->l_lsize
* 2 : 90;
275 if ((l
->l_line
= realloc(l
->l_line
,
276 (unsigned)need
* sizeof(CHAR
))) == NULL
)
280 c
= &l
->l_line
[l
->l_line_len
++];
283 c
->c_column
= cur_col
;
285 * If things are put in out of order, they will need sorting
286 * when it is flushed.
288 if (cur_col
< l
->l_max_col
)
291 l
->l_max_col
= cur_col
;
295 exit(0); /* no lines, so just exit */
297 /* goto the last line that had a character on it */
298 for (; l
->l_next
; l
= l
->l_next
)
300 flush_lines(this_line
- nflushd_lines
+ extra_lines
+ 1);
302 /* make sure we leave things in a sane state */
303 if (last_set
!= CS_NORMAL
)
306 /* flush out the last few blank lines */
307 nblank_lines
= max_line
- this_line
;
310 else if (!nblank_lines
)
311 /* missing a \n on the last line? */
318 flush_lines(int nflush
)
322 while (--nflush
>= 0) {
331 (void)free(l
->l_line
);
335 lines
->l_prev
= NULL
;
339 * Print a number of newline/half newlines. If fine flag is set, nblank_lines
340 * is the number of half line feeds, otherwise it is the number of whole line
357 for (i
= nb
; --i
>= 0;)
369 * Write a line to stdout taking care of space to tab conversion (-h flag)
370 * and character set shifts.
376 int nchars
, last_col
, this_col
;
379 nchars
= l
->l_line_len
;
381 if (l
->l_needs_sort
) {
383 static int count_size
, *count
, i
, save
, sorted_size
, tot
;
386 * Do an O(n) sort on l->l_line by column being careful to
387 * preserve the order of characters in the same column.
389 if (l
->l_lsize
> sorted_size
) {
390 sorted_size
= l
->l_lsize
;
391 if ((sorted
= realloc(sorted
,
392 (unsigned)sizeof(CHAR
) * sorted_size
)) == NULL
)
395 if (l
->l_max_col
>= count_size
) {
396 count_size
= l
->l_max_col
+ 1;
397 if ((count
= realloc(count
,
398 (unsigned)sizeof(int) * count_size
)) == NULL
)
401 memset(count
, 0, sizeof(int) * l
->l_max_col
+ 1);
402 for (i
= nchars
, c
= l
->l_line
; --i
>= 0; c
++)
403 count
[c
->c_column
]++;
406 * calculate running total (shifted down by 1) to use as
407 * indices into new line.
409 for (tot
= 0, i
= 0; i
<= l
->l_max_col
; i
++) {
415 for (i
= nchars
, c
= l
->l_line
; --i
>= 0; c
++)
416 sorted
[count
[c
->c_column
]++] = *c
;
421 this_col
= c
->c_column
;
425 } while (--nchars
> 0 && this_col
== endc
->c_column
);
427 /* if -b only print last character */
431 if (this_col
> last_col
) {
432 int nspace
= this_col
- last_col
;
434 if (compress_spaces
&& nspace
> 1) {
436 int tab_col
, tab_size
;
438 tab_col
= (last_col
+ 8) & ~7;
439 if (tab_col
> this_col
)
441 tab_size
= tab_col
- last_col
;
450 while (--nspace
>= 0)
457 if (c
->c_set
!= last_set
) {
477 static LINE
*line_freelist
;
485 if (!line_freelist
) {
486 if ((l
= realloc(NULL
, sizeof(LINE
) * NALLOC
)) == NULL
)
489 for (i
= 1; i
< NALLOC
; i
++, l
++)
494 line_freelist
= l
->l_next
;
496 memset(l
, 0, sizeof(LINE
));
504 l
->l_next
= line_freelist
;
512 (void)fprintf(stderr
, "usage: col [-bfhpx] [-l nline]\n");
520 warnx("warning: can't back up %s",
521 line
< 0 ? "past first line" : "-- line already flushed");