2 * Copyright (c) 1990 The Regents of the University of California.
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. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * Wed Jun 22 22:15:41 1994, faith@cs.unc.edu: Added internationalization
37 * patches from Andries.Brouwer@cwi.nl
38 * Wed Sep 14 22:31:17 1994: patches from Carl Christofferson
39 * (cchris@connected.com)
40 * 1999-02-22 Arkadiusz MiĆkiewicz <misiek@pld.ORG.PL>
41 * added Native Language Support
42 * 1999-09-19 Bruno Haible <haible@clisp.cons.org>
43 * modified to work correctly in multi-byte locales
48 * This command is deprecated. The utility is in maintenance mode,
49 * meaning we keep them in source tree for backward compatibility
50 * only. Do not waste time making this command better, unless the
51 * fix is about security or other very critical issue.
53 * See Documentation/deprecated.txt for more information.
68 #include "closestream.h"
70 #define BS '\b' /* backspace */
71 #define TAB '\t' /* tab */
72 #define SPACE ' ' /* space */
73 #define NL '\n' /* newline */
74 #define CR '\r' /* carriage return */
75 #define ESC '\033' /* escape */
76 #define SI '\017' /* shift in to normal character set */
77 #define SO '\016' /* shift out to alternate character set */
78 #define VT '\013' /* vertical tab (aka reverse line feed) */
79 #define RLF '\007' /* ESC-07 reverse line feed */
80 #define RHLF '\010' /* ESC-010 reverse half-line feed */
81 #define FHLF '\011' /* ESC-011 forward half-line feed */
83 /* build up at least this many lines before flushing them out */
84 #define BUFFER_MARGIN 32
88 typedef struct char_str
{
90 #define CS_ALTERNATE 2
91 short c_column
; /* column character is in */
92 CSET c_set
; /* character set (currently only 2) */
93 wchar_t c_char
; /* character in question */
94 int c_width
; /* character width */
97 typedef struct line_str LINE
;
99 CHAR
*l_line
; /* characters on the line */
100 LINE
*l_prev
; /* previous line */
101 LINE
*l_next
; /* next line */
102 int l_lsize
; /* allocated sizeof l_line */
103 int l_line_len
; /* strlen(l_line) */
104 int l_needs_sort
; /* set if chars went in out of order */
105 int l_max_col
; /* max column in the line */
108 void free_line(LINE
*l
);
109 void flush_line(LINE
*l
);
110 void flush_lines(int);
111 void flush_blanks(void);
112 LINE
*alloc_line(void);
114 CSET last_set
; /* char_set of last char printed */
116 int compress_spaces
; /* if doing space -> tab conversion */
117 int fine
; /* if `fine' resolution (half lines) */
118 unsigned max_bufd_lines
; /* max # lines to keep in memory */
119 int nblank_lines
; /* # blanks after last flushed line */
120 int no_backspaces
; /* if not to output any backspaces */
121 int pass_unknown_seqs
; /* whether to pass unknown control sequences */
124 if (putwchar(ch) == WEOF) \
127 static void __attribute__((__noreturn__
)) usage(FILE *out
)
131 " %s [options]\n"), program_invocation_short_name
);
135 " -b, --no-backspaces do not output backspaces\n"
136 " -f, --fine permit forward half line feeds\n"
137 " -p, --pass pass unknown control sequences\n"
138 " -h, --tabs convert spaces to tabs\n"
139 " -x, --spaces convert tabs to spaces\n"
140 " -l, --lines NUM buffer at least NUM lines\n"
141 " -V, --version output version information and exit\n"
142 " -H, --help display this help and exit\n\n"));
145 "%s reads from standard input and writes to standard output\n\n"),
146 program_invocation_short_name
);
148 fprintf(out
, USAGE_MAN_TAIL("col(1)"));
149 exit(out
== stderr
? EXIT_FAILURE
: EXIT_SUCCESS
);
152 static void __attribute__((__noreturn__
)) wrerr(void)
154 errx(EXIT_FAILURE
, _("write error"));
157 int main(int argc
, char **argv
)
161 CSET cur_set
; /* current character set */
162 LINE
*l
; /* current line */
163 int extra_lines
; /* # of lines above first line */
164 int cur_col
; /* current column */
165 int cur_line
; /* line number of current position */
166 int max_line
; /* max value of cur_line */
167 int this_line
; /* line l points to */
168 int nflushd_lines
; /* number of lines that were flushed */
169 int adjust
, opt
, warned
;
170 int ret
= EXIT_SUCCESS
;
172 static const struct option longopts
[] = {
173 { "no-backspaces", no_argument
, 0, 'b' },
174 { "fine", no_argument
, 0, 'f' },
175 { "pass", no_argument
, 0, 'p' },
176 { "tabs", no_argument
, 0, 'h' },
177 { "spaces", no_argument
, 0, 'x' },
178 { "lines", required_argument
, 0, 'l' },
179 { "version", no_argument
, 0, 'V' },
180 { "help", no_argument
, 0, 'H' },
184 setlocale(LC_ALL
, "");
185 bindtextdomain(PACKAGE
, LOCALEDIR
);
187 atexit(close_stdout
);
189 max_bufd_lines
= 128 * 2;
190 compress_spaces
= 1; /* compress spaces into tabs */
191 pass_unknown_seqs
= 0; /* remove unknown escape sequences */
193 while ((opt
= getopt_long(argc
, argv
, "bfhl:pxVH", longopts
, NULL
)) != -1)
195 case 'b': /* do not output backspaces */
198 case 'f': /* allow half forward line feeds */
201 case 'h': /* compress spaces into tabs */
206 * Buffered line count, which is a value in half
207 * lines e.g. twice the amount specified.
209 max_bufd_lines
= strtou32_or_err(optarg
, _("bad -l argument")) * 2;
212 pass_unknown_seqs
= 1;
214 case 'x': /* do not compress spaces into tabs */
218 printf(UTIL_LINUX_VERSION
);
229 adjust
= cur_col
= extra_lines
= warned
= 0;
230 cur_line
= max_line
= nflushd_lines
= this_line
= 0;
231 cur_set
= last_set
= CS_NORMAL
;
232 lines
= l
= alloc_line();
234 while (feof(stdin
) == 0) {
236 if ((ch
= getwchar()) == WEOF
) {
237 if (errno
== EILSEQ
) {
245 case BS
: /* can't go back further */
253 case ESC
: /* just ignore EOF */
263 if (cur_line
> max_line
)
269 if (cur_line
> max_line
)
280 cur_set
= CS_ALTERNATE
;
282 case TAB
: /* adjust column */
292 cur_col
+= wcwidth(ch
);
295 if (!pass_unknown_seqs
)
299 /* Must stuff ch in a line - are we at the right one? */
300 if (cur_line
!= this_line
- adjust
) {
305 nmove
= cur_line
- this_line
;
307 /* round up to next line */
314 for (; nmove
< 0 && l
->l_prev
; nmove
++)
317 if (nflushd_lines
== 0) {
319 * Allow backup past first
320 * line if nothing has been
323 for (; nmove
< 0; nmove
++) {
333 _("warning: can't back up %s."), cur_line
< 0 ?
334 _("past first line") : _("-- line already flushed"));
339 /* may need to allocate here */
340 for (; nmove
> 0 && l
->l_next
; nmove
--)
342 for (; nmove
> 0; nmove
--) {
349 this_line
= cur_line
+ adjust
;
350 nmove
= this_line
- nflushd_lines
;
352 && (unsigned) nmove
>= max_bufd_lines
+ BUFFER_MARGIN
) {
353 nflushd_lines
+= nmove
- max_bufd_lines
;
354 flush_lines(nmove
- max_bufd_lines
);
357 /* grow line's buffer? */
358 if (l
->l_line_len
+ 1 >= l
->l_lsize
) {
361 need
= l
->l_lsize
? l
->l_lsize
* 2 : 90;
362 l
->l_line
= (CHAR
*)xrealloc((void *) l
->l_line
,
363 (unsigned) need
* sizeof(CHAR
));
366 c
= &l
->l_line
[l
->l_line_len
++];
369 c
->c_column
= cur_col
;
370 c
->c_width
= wcwidth(ch
);
372 * If things are put in out of order, they will need sorting
373 * when it is flushed.
375 if (cur_col
< l
->l_max_col
)
378 l
->l_max_col
= cur_col
;
380 cur_col
+= c
->c_width
;
382 /* goto the last line that had a character on it */
383 for (; l
->l_next
; l
= l
->l_next
)
385 flush_lines(this_line
- nflushd_lines
+ extra_lines
+ 1);
387 /* make sure we leave things in a sane state */
388 if (last_set
!= CS_NORMAL
)
391 /* flush out the last few blank lines */
392 nblank_lines
= max_line
- this_line
;
395 else if (!nblank_lines
)
396 /* missing a \n on the last line? */
402 void flush_lines(int nflush
)
406 while (--nflush
>= 0) {
414 free((void *)l
->l_line
);
418 lines
->l_prev
= NULL
;
422 * Print a number of newline/half newlines. If fine flag is set, nblank_lines
423 * is the number of half line feeds, otherwise it is the number of whole line
426 void flush_blanks(void)
439 for (i
= nb
; --i
>= 0;)
451 * Write a line to stdout taking care of space to tab conversion (-h flag)
452 * and character set shifts.
454 void flush_line(LINE
*l
)
457 int nchars
, last_col
, this_col
;
460 nchars
= l
->l_line_len
;
462 if (l
->l_needs_sort
) {
464 static int count_size
, *count
, i
, save
, sorted_size
, tot
;
467 * Do an O(n) sort on l->l_line by column being careful to
468 * preserve the order of characters in the same column.
470 if (l
->l_lsize
> sorted_size
) {
471 sorted_size
= l
->l_lsize
;
472 sorted
= (CHAR
*)xrealloc((void *)sorted
,
473 (unsigned)sizeof(CHAR
) * sorted_size
);
475 if (l
->l_max_col
>= count_size
) {
476 count_size
= l
->l_max_col
+ 1;
477 count
= (int *)xrealloc((void *)count
,
478 (unsigned)sizeof(int) * count_size
);
480 memset(count
, 0, sizeof(int) * l
->l_max_col
+ 1);
481 for (i
= nchars
, c
= l
->l_line
; --i
>= 0; c
++)
482 count
[c
->c_column
]++;
485 * calculate running total (shifted down by 1) to use as
486 * indices into new line.
488 for (tot
= 0, i
= 0; i
<= l
->l_max_col
; i
++) {
494 for (i
= nchars
, c
= l
->l_line
; --i
>= 0; c
++)
495 sorted
[count
[c
->c_column
]++] = *c
;
500 this_col
= c
->c_column
;
504 } while (--nchars
> 0 && this_col
== endc
->c_column
);
506 /* if -b only print last character */
510 this_col
+ c
->c_width
> endc
->c_column
)
514 if (this_col
> last_col
) {
515 int nspace
= this_col
- last_col
;
517 if (compress_spaces
&& nspace
> 1) {
520 ntabs
= this_col
/ 8 - last_col
/ 8;
522 nspace
= this_col
& 7;
527 while (--nspace
>= 0)
533 if (c
->c_set
!= last_set
) {
544 if ((c
+ 1) < endc
) {
546 for (i
=0; i
< c
->c_width
; i
++)
552 last_col
+= (c
- 1)->c_width
;
558 static LINE
*line_freelist
;
566 if (!line_freelist
) {
567 l
= xmalloc(sizeof(LINE
) * NALLOC
);
569 for (i
= 1; i
< NALLOC
; i
++, l
++)
574 line_freelist
= l
->l_next
;
576 memset(l
, 0, sizeof(LINE
));
580 void free_line(LINE
*l
)
582 l
->l_next
= line_freelist
;