]>
git.ipfire.org Git - thirdparty/util-linux.git/blob - text-utils/col.c
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@misiek.eu.org>
41 * added Native Language Support
42 * 1999-09-19 Bruno Haible <haible@clisp.cons.org>
43 * modified to work correctly in multi-byte locales
57 #define BS '\b' /* backspace */
58 #define TAB '\t' /* tab */
59 #define SPACE ' ' /* space */
60 #define NL '\n' /* newline */
61 #define CR '\r' /* carriage return */
62 #define ESC '\033' /* escape */
63 #define SI '\017' /* shift in to normal character set */
64 #define SO '\016' /* shift out to alternate character set */
65 #define VT '\013' /* vertical tab (aka reverse line feed) */
66 #define RLF '\007' /* ESC-07 reverse line feed */
67 #define RHLF '\010' /* ESC-010 reverse half-line feed */
68 #define FHLF '\011' /* ESC-011 forward half-line feed */
70 /* build up at least this many lines before flushing them out */
71 #define BUFFER_MARGIN 32
75 typedef struct char_str
{
77 #define CS_ALTERNATE 2
78 short c_column
; /* column character is in */
79 CSET c_set
; /* character set (currently only 2) */
80 wchar_t c_char
; /* character in question */
81 int c_width
; /* character width */
84 typedef struct line_str LINE
;
86 CHAR
*l_line
; /* characters on the line */
87 LINE
*l_prev
; /* previous line */
88 LINE
*l_next
; /* next line */
89 int l_lsize
; /* allocated sizeof l_line */
90 int l_line_len
; /* strlen(l_line) */
91 int l_needs_sort
; /* set if chars went in out of order */
92 int l_max_col
; /* max column in the line */
98 void free_line(LINE
*l
);
99 void flush_line(LINE
*l
);
100 void flush_lines(int);
101 void flush_blanks(void);
102 void *xmalloc(void *p
, size_t size
);
103 LINE
*alloc_line(void);
105 CSET last_set
; /* char_set of last char printed */
107 int compress_spaces
; /* if doing space -> tab conversion */
108 int fine
; /* if `fine' resolution (half lines) */
109 int max_bufd_lines
; /* max # lines to keep in memory */
110 int nblank_lines
; /* # blanks after last flushed line */
111 int no_backspaces
; /* if not to output any backspaces */
112 int pass_unknown_seqs
; /* whether to pass unknown control sequences */
115 if (putwchar(ch) == WEOF) \
118 int main(int argc
, char **argv
)
124 CSET cur_set
; /* current character set */
125 LINE
*l
; /* current line */
126 int extra_lines
; /* # of lines above first line */
127 int cur_col
; /* current column */
128 int cur_line
; /* line number of current position */
129 int max_line
; /* max value of cur_line */
130 int this_line
; /* line l points to */
131 int nflushd_lines
; /* number of lines that were flushed */
132 int adjust
, opt
, warned
;
134 setlocale(LC_ALL
, "");
135 bindtextdomain(PACKAGE
, LOCALEDIR
);
138 max_bufd_lines
= 128;
139 compress_spaces
= 1; /* compress spaces into tabs */
140 pass_unknown_seqs
= 0; /* remove unknown escape sequences */
141 while ((opt
= getopt(argc
, argv
, "bfhl:px")) != -1)
143 case 'b': /* do not output backspaces */
146 case 'f': /* allow half forward line feeds */
149 case 'h': /* compress spaces into tabs */
152 case 'l': /* buffered line count */
153 if ((max_bufd_lines
= atoi(optarg
)) <= 0) {
154 (void)fprintf(stderr
,
155 _("col: bad -l argument %s.\n"), optarg
);
160 pass_unknown_seqs
= 1;
162 case 'x': /* do not compress spaces into tabs */
173 /* this value is in half lines */
176 adjust
= cur_col
= extra_lines
= warned
= 0;
177 cur_line
= max_line
= nflushd_lines
= this_line
= 0;
178 cur_set
= last_set
= CS_NORMAL
;
179 lines
= l
= alloc_line();
181 while ((ch
= getwchar()) != WEOF
) {
184 case BS
: /* can't go back further */
192 case ESC
: /* just ignore EOF */
202 if (cur_line
> max_line
)
208 if (cur_line
> max_line
)
219 cur_set
= CS_ALTERNATE
;
221 case TAB
: /* adjust column */
231 cur_col
+= wcwidth(ch
);
234 if (!pass_unknown_seqs
)
238 /* Must stuff ch in a line - are we at the right one? */
239 if (cur_line
!= this_line
- adjust
) {
244 nmove
= cur_line
- this_line
;
246 /* round up to next line */
253 for (; nmove
< 0 && l
->l_prev
; nmove
++)
256 if (nflushd_lines
== 0) {
258 * Allow backup past first
259 * line if nothing has been
262 for (; nmove
< 0; nmove
++) {
276 /* may need to allocate here */
277 for (; nmove
> 0 && l
->l_next
; nmove
--)
279 for (; nmove
> 0; nmove
--) {
286 this_line
= cur_line
+ adjust
;
287 nmove
= this_line
- nflushd_lines
;
288 if (nmove
>= max_bufd_lines
+ BUFFER_MARGIN
) {
289 nflushd_lines
+= nmove
- max_bufd_lines
;
290 flush_lines(nmove
- 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 l
->l_line
= (CHAR
*)xmalloc((void *) l
->l_line
,
299 (unsigned) need
* sizeof(CHAR
));
302 c
= &l
->l_line
[l
->l_line_len
++];
305 c
->c_column
= cur_col
;
306 c
->c_width
= wcwidth(ch
);
308 * If things are put in out of order, they will need sorting
309 * when it is flushed.
311 if (cur_col
< l
->l_max_col
)
314 l
->l_max_col
= cur_col
;
316 cur_col
+= c
->c_width
;
318 /* goto the last line that had a character on it */
319 for (; l
->l_next
; l
= l
->l_next
)
321 flush_lines(this_line
- nflushd_lines
+ extra_lines
+ 1);
323 /* make sure we leave things in a sane state */
324 if (last_set
!= CS_NORMAL
)
327 /* flush out the last few blank lines */
328 nblank_lines
= max_line
- this_line
;
331 else if (!nblank_lines
)
332 /* missing a \n on the last line? */
335 if (ferror(stdout
) || fclose(stdout
))
340 void flush_lines(int nflush
)
344 while (--nflush
>= 0) {
353 (void)free((void *)l
->l_line
);
357 lines
->l_prev
= NULL
;
361 * Print a number of newline/half newlines. If fine flag is set, nblank_lines
362 * is the number of half line feeds, otherwise it is the number of whole line
378 for (i
= nb
; --i
>= 0;)
390 * Write a line to stdout taking care of space to tab conversion (-h flag)
391 * and character set shifts.
393 void flush_line(LINE
*l
)
396 int nchars
, last_col
, this_col
;
399 nchars
= l
->l_line_len
;
401 if (l
->l_needs_sort
) {
403 static int count_size
, *count
, i
, save
, sorted_size
, tot
;
406 * Do an O(n) sort on l->l_line by column being careful to
407 * preserve the order of characters in the same column.
409 if (l
->l_lsize
> sorted_size
) {
410 sorted_size
= l
->l_lsize
;
411 sorted
= (CHAR
*)xmalloc((void *)sorted
,
412 (unsigned)sizeof(CHAR
) * sorted_size
);
414 if (l
->l_max_col
>= count_size
) {
415 count_size
= l
->l_max_col
+ 1;
416 count
= (int *)xmalloc((void *)count
,
417 (unsigned)sizeof(int) * count_size
);
419 memset(count
, 0, sizeof(int) * l
->l_max_col
+ 1);
420 for (i
= nchars
, c
= l
->l_line
; --i
>= 0; c
++)
421 count
[c
->c_column
]++;
424 * calculate running total (shifted down by 1) to use as
425 * indices into new line.
427 for (tot
= 0, i
= 0; i
<= l
->l_max_col
; i
++) {
433 for (i
= nchars
, c
= l
->l_line
; --i
>= 0; c
++)
434 sorted
[count
[c
->c_column
]++] = *c
;
439 this_col
= c
->c_column
;
443 } while (--nchars
> 0 && this_col
== endc
->c_column
);
445 /* if -b only print last character */
449 this_col
+ c
->c_width
> endc
->c_column
)
453 if (this_col
> last_col
) {
454 int nspace
= this_col
- last_col
;
456 if (compress_spaces
&& nspace
> 1) {
459 ntabs
= this_col
/ 8 - last_col
/ 8;
461 nspace
= this_col
& 7;
466 while (--nspace
>= 0)
472 if (c
->c_set
!= last_set
) {
485 for (i
=0; i
< c
->c_width
; i
++)
491 last_col
+= (c
-1)->c_width
;
497 static LINE
*line_freelist
;
505 if (!line_freelist
) {
506 l
= (LINE
*)xmalloc((void *)NULL
, sizeof(LINE
) * NALLOC
);
508 for (i
= 1; i
< NALLOC
; i
++, l
++)
513 line_freelist
= l
->l_next
;
515 memset(l
, 0, sizeof(LINE
));
519 void free_line(LINE
*l
)
521 l
->l_next
= line_freelist
;
526 xmalloc(void *p
, size_t size
)
528 if (!(p
= (void *)realloc(p
, size
))) {
529 (void)fprintf(stderr
, "col: %s.\n", strerror(ENOMEM
));
537 (void)fprintf(stderr
, _("usage: col [-bfpx] [-l nline]\n"));
543 (void)fprintf(stderr
, _("col: write error.\n"));
549 (void)fprintf(stderr
,
550 _("col: warning: can't back up %s.\n"), line
< 0 ?
551 _("past first line") : _("-- line already flushed"));