1 /* $NetBSD: col.c,v 1.14 2005/02/17 17:12:42 xtraeme Exp $ */
4 * Copyright (c) 1990, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Michael Rendell of the Memorial University of Newfoundland.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include <sys/cdefs.h>
37 __COPYRIGHT("@(#) Copyright (c) 1990, 1993, 1994\
38 The Regents of the University of California. All rights reserved.");
43 static char sccsid
[] = "@(#)col.c 8.5 (Berkeley) 5/4/95";
45 __RCSID("$NetBSD: col.c,v 1.14 2005/02/17 17:12:42 xtraeme Exp $");
55 #define BS '\b' /* backspace */
56 #define TAB '\t' /* tab */
57 #define SPACE ' ' /* space */
58 #define NL '\n' /* newline */
59 #define CR '\r' /* carriage return */
60 #define ESC '\033' /* escape */
61 #define SI '\017' /* shift in to normal character set */
62 #define SO '\016' /* shift out to alternate character set */
63 #define VT '\013' /* vertical tab (aka reverse line feed) */
64 #define RLF '\007' /* ESC-07 reverse line feed */
65 #define RHLF '\010' /* ESC-010 reverse half-line feed */
66 #define FHLF '\011' /* ESC-011 forward half-line feed */
68 /* build up at least this many lines before flushing them out */
69 #define BUFFER_MARGIN 32
73 typedef struct char_str
{
75 #define CS_ALTERNATE 2
76 short c_column
; /* column character is in */
77 CSET c_set
; /* character set (currently only 2) */
78 char c_char
; /* character in question */
81 typedef struct line_str LINE
;
83 CHAR
*l_line
; /* characters on the line */
84 LINE
*l_prev
; /* previous line */
85 LINE
*l_next
; /* next line */
86 int l_lsize
; /* allocated sizeof l_line */
87 int l_line_len
; /* strlen(l_line) */
88 int l_needs_sort
; /* set if chars went in out of order */
89 int l_max_col
; /* max column in the line */
92 LINE
*alloc_line(void);
94 void flush_line(LINE
*);
95 void flush_lines(int);
96 void flush_blanks(void);
97 void free_line(LINE
*);
100 void *xmalloc(void *, size_t);
102 CSET last_set
; /* char_set of last char printed */
104 int compress_spaces
; /* if doing space -> tab conversion */
105 int fine
; /* if `fine' resolution (half lines) */
106 int max_bufd_lines
; /* max # lines to keep in memory */
107 int nblank_lines
; /* # blanks after last flushed line */
108 int no_backspaces
; /* if not to output any backspaces */
109 int pass_unknown_seqs
; /* whether to pass unknown control sequences */
112 if (putchar(ch) == EOF) \
116 main(int argc
, char **argv
)
120 CSET cur_set
; /* current character set */
121 LINE
*l
; /* current line */
122 int extra_lines
; /* # of lines above first line */
123 int cur_col
; /* current column */
124 int cur_line
; /* line number of current position */
125 int max_line
; /* max value of cur_line */
126 int this_line
; /* line l points to */
127 int nflushd_lines
; /* number of lines that were flushed */
128 int adjust
, opt
, warned
;
130 max_bufd_lines
= 128;
131 compress_spaces
= 1; /* compress spaces into tabs */
132 pass_unknown_seqs
= 0; /* remove unknown escape sequences */
133 while ((opt
= getopt(argc
, argv
, "bfhl:px")) != -1)
135 case 'b': /* do not output backspaces */
138 case 'f': /* allow half forward line feeds */
141 case 'h': /* compress spaces into tabs */
144 case 'l': /* buffered line count */
145 if ((max_bufd_lines
= atoi(optarg
)) <= 0) {
146 (void)fprintf(stderr
,
147 "col: bad -l argument %s.\n", optarg
);
151 case 'p': /* pass unknown control sequences */
152 pass_unknown_seqs
= 1;
154 case 'x': /* do not compress spaces into tabs */
165 /* this value is in half lines */
168 adjust
= cur_col
= extra_lines
= warned
= 0;
169 cur_line
= max_line
= nflushd_lines
= this_line
= 0;
170 cur_set
= last_set
= CS_NORMAL
;
171 lines
= l
= alloc_line();
173 while ((ch
= getchar()) != EOF
) {
176 case BS
: /* can't go back further */
184 case ESC
: /* just ignore EOF */
194 if (cur_line
> max_line
)
200 if (cur_line
> max_line
)
211 cur_set
= CS_ALTERNATE
;
213 case TAB
: /* adjust column */
221 if (!pass_unknown_seqs
)
225 /* Must stuff ch in a line - are we at the right one? */
226 if (cur_line
!= this_line
- adjust
) {
231 nmove
= cur_line
- this_line
;
233 /* round up to next line */
240 for (; nmove
< 0 && l
->l_prev
; nmove
++)
243 if (nflushd_lines
== 0) {
245 * Allow backup past first
246 * line if nothing has been
249 for (; nmove
< 0; nmove
++) {
263 /* may need to allocate here */
264 for (; nmove
> 0 && l
->l_next
; nmove
--)
266 for (; nmove
> 0; nmove
--) {
273 this_line
= cur_line
+ adjust
;
274 nmove
= this_line
- nflushd_lines
;
275 if (nmove
>= max_bufd_lines
+ BUFFER_MARGIN
) {
276 nflushd_lines
+= nmove
- max_bufd_lines
;
277 flush_lines(nmove
- max_bufd_lines
);
280 /* grow line's buffer? */
281 if (l
->l_line_len
+ 1 >= l
->l_lsize
) {
284 need
= l
->l_lsize
? l
->l_lsize
* 2 : 90;
285 l
->l_line
= (CHAR
*)xmalloc((void *) l
->l_line
,
286 (unsigned) need
* sizeof(CHAR
));
289 c
= &l
->l_line
[l
->l_line_len
++];
292 c
->c_column
= cur_col
;
294 * If things are put in out of order, they will need sorting
295 * when it is flushed.
297 if (cur_col
< l
->l_max_col
)
300 l
->l_max_col
= cur_col
;
304 exit(EXIT_SUCCESS
); /* no lines, so just exit */
306 /* goto the last line that had a character on it */
307 for (; l
->l_next
; l
= l
->l_next
)
309 flush_lines(this_line
- nflushd_lines
+ extra_lines
+ 1);
311 /* make sure we leave things in a sane state */
312 if (last_set
!= CS_NORMAL
)
315 /* flush out the last few blank lines */
316 nblank_lines
= max_line
- this_line
;
319 else if (!nblank_lines
)
320 /* missing a \n on the last line? */
328 flush_lines(int nflush
)
332 while (--nflush
>= 0) {
341 (void)free((void *)l
->l_line
);
345 lines
->l_prev
= NULL
;
349 * Print a number of newline/half newlines. If fine flag is set, nblank_lines
350 * is the number of half line feeds, otherwise it is the number of whole line
367 for (i
= nb
; --i
>= 0;)
379 * Write a line to stdout taking care of space to tab conversion (-h flag)
380 * and character set shifts.
386 int nchars
, last_col
, this_col
;
389 nchars
= l
->l_line_len
;
391 if (l
->l_needs_sort
) {
393 static int count_size
, *count
, i
, save
, sorted_size
, tot
;
396 * Do an O(n) sort on l->l_line by column being careful to
397 * preserve the order of characters in the same column.
399 if (l
->l_lsize
> sorted_size
) {
400 sorted_size
= l
->l_lsize
;
401 sorted
= (CHAR
*)xmalloc((void *)sorted
,
402 (unsigned)sizeof(CHAR
) * sorted_size
);
404 if (l
->l_max_col
>= count_size
) {
405 count_size
= l
->l_max_col
+ 1;
406 count
= (int *)xmalloc((void *)count
,
407 (unsigned)sizeof(int) * count_size
);
409 (void)memset(count
, 0, sizeof(int) * l
->l_max_col
+ 1);
410 for (i
= nchars
, c
= l
->l_line
; --i
>= 0; c
++)
411 count
[c
->c_column
]++;
414 * calculate running total (shifted down by 1) to use as
415 * indices into new line.
417 for (tot
= 0, i
= 0; i
<= l
->l_max_col
; i
++) {
423 for (i
= nchars
, c
= l
->l_line
; --i
>= 0; c
++)
424 sorted
[count
[c
->c_column
]++] = *c
;
429 this_col
= c
->c_column
;
433 } while (--nchars
> 0 && this_col
== endc
->c_column
);
435 /* if -b only print last character */
439 if (this_col
> last_col
) {
440 int nspace
= this_col
- last_col
;
442 if (compress_spaces
&& nspace
> 1) {
445 ntabs
= ((last_col
% 8) + nspace
) / 8;
447 nspace
-= (ntabs
* 8) - (last_col
% 8);
452 while (--nspace
>= 0)
459 if (c
->c_set
!= last_set
) {
479 static LINE
*line_freelist
;
487 if (!line_freelist
) {
488 l
= (LINE
*)xmalloc((void *)NULL
, sizeof(LINE
) * NALLOC
);
490 for (i
= 1; i
< NALLOC
; i
++, l
++)
495 line_freelist
= l
->l_next
;
497 (void)memset(l
, 0, sizeof(LINE
));
505 l
->l_next
= line_freelist
;
510 xmalloc(void *p
, size_t size
)
514 if (!(q
= (void *)realloc(p
, size
)))
515 err(EXIT_FAILURE
, "realloc");
524 (void)fprintf(stderr
, "usage: col [-bfpx] [-l nline]\n");
532 (void)fprintf(stderr
, "col: write error.\n");
540 warnx("warning: can't back up %s",
541 line
< 0 ? "past first line" : "-- line already flushed");