]>
git.cameronkatri.com Git - apple_cmds.git/blob - text_cmds/col/col.c
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. 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
38 static const char copyright
[] =
39 "@(#) Copyright (c) 1990, 1993, 1994\n\
40 The Regents of the University of California. All rights reserved.\n";
45 static char sccsid
[] = "@(#)col.c 8.5 (Berkeley) 5/4/95";
49 #include <sys/cdefs.h>
50 __FBSDID("$FreeBSD: src/usr.bin/col/col.c,v 1.19 2004/07/29 07:28:26 tjr Exp $");
61 #define BS '\b' /* backspace */
62 #define TAB '\t' /* tab */
63 #define SPACE ' ' /* space */
64 #define NL '\n' /* newline */
65 #define CR '\r' /* carriage return */
66 #define ESC '\033' /* escape */
67 #define SI '\017' /* shift in to normal character set */
68 #define SO '\016' /* shift out to alternate character set */
69 #define VT '\013' /* vertical tab (aka reverse line feed) */
70 #define RLF '\007' /* ESC-07 reverse line feed */
71 #define RHLF '\010' /* ESC-010 reverse half-line feed */
72 #define FHLF '\011' /* ESC-011 forward half-line feed */
74 /* build up at least this many lines before flushing them out */
75 #define BUFFER_MARGIN 32
79 typedef struct char_str
{
81 #define CS_ALTERNATE 2
82 short c_column
; /* column character is in */
83 CSET c_set
; /* character set (currently only 2) */
84 wchar_t c_char
; /* character in question */
85 int c_width
; /* character width */
88 typedef struct line_str LINE
;
90 CHAR
*l_line
; /* characters on the line */
91 LINE
*l_prev
; /* previous line */
92 LINE
*l_next
; /* next line */
93 int l_lsize
; /* allocated sizeof l_line */
94 int l_line_len
; /* strlen(l_line) */
95 int l_needs_sort
; /* set if chars went in out of order */
96 int l_max_col
; /* max column in the line */
99 LINE
*alloc_line(void);
101 void flush_line(LINE
*);
102 void flush_lines(int);
103 void flush_blanks(void);
104 void free_line(LINE
*);
107 CSET last_set
; /* char_set of last char printed */
109 int compress_spaces
; /* if doing space -> tab conversion */
110 int fine
; /* if `fine' resolution (half lines) */
111 int max_bufd_lines
; /* max # lines to keep in memory */
112 int nblank_lines
; /* # blanks after last flushed line */
113 int no_backspaces
; /* if not to output any backspaces */
114 int pass_unknown_seqs
; /* pass unknown control sequences */
118 if (putwchar(ch) == WEOF) \
119 errx(1, "write error"); \
123 main(int argc
, char **argv
)
127 CSET cur_set
; /* current character set */
128 LINE
*l
; /* current line */
129 int extra_lines
; /* # of lines above first line */
130 int cur_col
; /* current column */
131 int cur_line
; /* line number of current position */
132 int max_line
; /* max value of cur_line */
133 int this_line
; /* line l points to */
134 int nflushd_lines
; /* number of lines that were flushed */
135 int adjust
, opt
, warned
, width
;
137 (void)setlocale(LC_CTYPE
, "");
139 max_bufd_lines
= 128;
140 compress_spaces
= 1; /* compress spaces into tabs */
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 errx(1, "bad -l argument %s", optarg
);
156 case 'p': /* pass unknown control sequences */
157 pass_unknown_seqs
= 1;
159 case 'x': /* do not compress spaces into tabs */
170 /* this value is in half lines */
173 adjust
= cur_col
= extra_lines
= warned
= 0;
174 cur_line
= max_line
= nflushd_lines
= this_line
= 0;
175 cur_set
= last_set
= CS_NORMAL
;
176 lines
= l
= alloc_line();
178 while ((ch
= getwchar()) != WEOF
) {
181 case BS
: /* can't go back further */
189 case ESC
: /* just ignore EOF */
199 if (cur_line
> max_line
)
205 if (cur_line
> max_line
)
216 cur_set
= CS_ALTERNATE
;
218 case TAB
: /* adjust column */
227 if ((width
= wcwidth(ch
)) > 0)
231 if (!pass_unknown_seqs
)
235 /* Must stuff ch in a line - are we at the right one? */
236 if (cur_line
!= this_line
- adjust
) {
241 nmove
= cur_line
- this_line
;
243 /* round up to next line */
250 for (; nmove
< 0 && l
->l_prev
; nmove
++)
253 if (nflushd_lines
== 0) {
255 * Allow backup past first
256 * line if nothing has been
259 for (; nmove
< 0; nmove
++) {
273 /* may need to allocate here */
274 for (; nmove
> 0 && l
->l_next
; nmove
--)
276 for (; nmove
> 0; nmove
--) {
283 this_line
= cur_line
+ adjust
;
284 nmove
= this_line
- nflushd_lines
;
285 if (nmove
>= max_bufd_lines
+ BUFFER_MARGIN
) {
286 nflushd_lines
+= nmove
- max_bufd_lines
;
287 flush_lines(nmove
- max_bufd_lines
);
290 /* grow line's buffer? */
291 if (l
->l_line_len
+ 1 >= l
->l_lsize
) {
294 need
= l
->l_lsize
? l
->l_lsize
* 2 : 90;
295 if ((l
->l_line
= realloc(l
->l_line
,
296 (unsigned)need
* sizeof(CHAR
))) == NULL
)
297 err(1, (char *)NULL
);
300 c
= &l
->l_line
[l
->l_line_len
++];
303 c
->c_column
= cur_col
;
304 c
->c_width
= wcwidth(ch
);
306 * If things are put in out of order, they will need sorting
307 * when it is flushed.
309 if (cur_col
< l
->l_max_col
)
312 l
->l_max_col
= cur_col
;
314 cur_col
+= c
->c_width
;
319 exit(0); /* no lines, so just exit */
321 /* goto the last line that had a character on it */
322 for (; l
->l_next
; l
= l
->l_next
)
324 flush_lines(this_line
- nflushd_lines
+ extra_lines
+ 1);
326 /* make sure we leave things in a sane state */
327 if (last_set
!= CS_NORMAL
)
330 /* flush out the last few blank lines */
331 nblank_lines
= max_line
- this_line
;
334 else if (!nblank_lines
)
335 /* missing a \n on the last line? */
342 flush_lines(int nflush
)
346 while (--nflush
>= 0) {
355 (void)free(l
->l_line
);
359 lines
->l_prev
= NULL
;
363 * Print a number of newline/half newlines. If fine flag is set, nblank_lines
364 * is the number of half line feeds, otherwise it is the number of whole line
381 for (i
= nb
; --i
>= 0;)
393 * Write a line to stdout taking care of space to tab conversion (-h flag)
394 * and character set shifts.
400 int i
, nchars
, last_col
, this_col
;
403 nchars
= l
->l_line_len
;
405 if (l
->l_needs_sort
) {
407 static int count_size
, *count
, i
, save
, sorted_size
, tot
;
410 * Do an O(n) sort on l->l_line by column being careful to
411 * preserve the order of characters in the same column.
413 if (l
->l_lsize
> sorted_size
) {
414 sorted_size
= l
->l_lsize
;
415 if ((sorted
= realloc(sorted
,
416 (unsigned)sizeof(CHAR
) * sorted_size
)) == NULL
)
417 err(1, (char *)NULL
);
419 if (l
->l_max_col
>= count_size
) {
420 count_size
= l
->l_max_col
+ 1;
421 if ((count
= realloc(count
,
422 (unsigned)sizeof(int) * count_size
)) == NULL
)
423 err(1, (char *)NULL
);
425 memset(count
, 0, sizeof(int) * l
->l_max_col
+ 1);
426 for (i
= nchars
, c
= l
->l_line
; --i
>= 0; c
++)
427 count
[c
->c_column
]++;
430 * calculate running total (shifted down by 1) to use as
431 * indices into new line.
433 for (tot
= 0, i
= 0; i
<= l
->l_max_col
; i
++) {
439 for (i
= nchars
, c
= l
->l_line
; --i
>= 0; c
++)
440 sorted
[count
[c
->c_column
]++] = *c
;
445 this_col
= c
->c_column
;
449 } while (--nchars
> 0 && this_col
== endc
->c_column
);
451 /* if -b only print last character */
455 this_col
+ c
->c_width
> endc
->c_column
)
459 if (this_col
> last_col
) {
460 int nspace
= this_col
- last_col
;
462 if (compress_spaces
&& nspace
> 1) {
464 int tab_col
, tab_size
;;
466 tab_col
= (last_col
+ 8) & ~7;
467 if (tab_col
> this_col
)
469 tab_size
= tab_col
- last_col
;
478 while (--nspace
>= 0)
484 if (c
->c_set
!= last_set
) {
496 for (i
= 0; i
< c
->c_width
; i
++)
501 last_col
+= (c
- 1)->c_width
;
507 static LINE
*line_freelist
;
515 if (!line_freelist
) {
516 if ((l
= realloc(NULL
, sizeof(LINE
) * NALLOC
)) == NULL
)
517 err(1, (char *)NULL
);
519 for (i
= 1; i
< NALLOC
; i
++, l
++)
524 line_freelist
= l
->l_next
;
526 memset(l
, 0, sizeof(LINE
));
534 l
->l_next
= line_freelist
;
542 (void)fprintf(stderr
, "usage: col [-bfhpx] [-l nline]\n");
550 warnx("warning: can't back up %s",
551 line
< 0 ? "past first line" : "-- line already flushed");