* Copyright (c) 1990 The Regents of the University of California.
* This code is derived from software contributed to Berkeley by
* Michael Rendell of the Memorial University of Newfoundland.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
"@(#) Copyright (c) 1990 The Regents of the University of California.\n\
static char sccsid
[] = "@(#)col.c 5.3 (Berkeley) 2/2/91";
#define BS '\b' /* backspace */
#define TAB '\t' /* tab */
#define SPACE ' ' /* space */
#define NL '\n' /* newline */
#define CR '\r' /* carriage return */
#define ESC '\033' /* escape */
#define SI '\017' /* shift in to normal character set */
#define SO '\016' /* shift out to alternate character set */
#define VT '\013' /* vertical tab (aka reverse line feed) */
#define RLF '\007' /* ESC-07 reverse line feed */
#define RHLF '\010' /* ESC-010 reverse half-line feed */
#define FHLF '\011' /* ESC-011 forward half-line feed */
/* build up at least this many lines before flushing them out */
typedef struct char_str
{
short c_column
; /* column character is in */
CSET c_set
; /* character set (currently only 2) */
char c_char
; /* character in question */
typedef struct line_str LINE
;
CHAR
*l_line
; /* characters on the line */
LINE
*l_prev
; /* previous line */
LINE
*l_next
; /* next line */
int l_lsize
; /* allocated sizeof l_line */
int l_line_len
; /* strlen(l_line) */
int l_needs_sort
; /* set if chars went in out of order */
int l_max_col
; /* max column in the line */
CSET last_set
; /* char_set of last char printed */
int compress_spaces
; /* if doing space -> tab conversion */
int fine
; /* if `fine' resolution (half lines) */
int max_bufd_lines
; /* max # lines to keep in memory */
int nblank_lines
; /* # blanks after last flushed line */
int no_backspaces
; /* if not to output any backspaces */
if (putchar(ch) == EOF) \
/* next tabstop after col */
#define TABSTOP(col,ts) ((col) + (ts) - (col) % (ts))
CSET cur_set
; /* current character set */
LINE
*l
; /* current line */
int extra_lines
; /* # of lines above first line */
int cur_col
; /* current column */
int cur_line
; /* line number of current position */
int max_line
; /* max value of cur_line */
int this_line
; /* line l points to */
int nflushd_lines
; /* number of lines that were flushed */
compress_spaces
= 1; /* compress spaces into tabs */
while ((opt
= getopt(argc
, argv
, "bfhl:x")) != EOF
)
case 'b': /* do not output backspaces */
case 'f': /* allow half forward line feeds */
case 'h': /* compress spaces into tabs */
case 'l': /* buffered line count */
if ((max_bufd_lines
= atoi(optarg
)) <= 0) {
"col: bad -l argument %s.\n", optarg
);
case 'x': /* do not compress spaces into tabs */
/* this value is in half lines */
adjust
= cur_col
= extra_lines
= warned
= 0;
cur_line
= max_line
= nflushd_lines
= this_line
= 0;
cur_set
= last_set
= CS_NORMAL
;
lines
= l
= alloc_line();
while ((ch
= getchar()) != EOF
) {
case BS
: /* can't go back further */
case ESC
: /* just ignore EOF */
case TAB
: /* adjust column */
/* Must stuff ch in a line - are we at the right one? */
if (cur_line
!= this_line
- adjust
) {
nmove
= cur_line
- this_line
;
/* round up to next line */
for (; nmove
< 0 && l
->l_prev
; nmove
++)
if (nflushd_lines
== 0) {
* Allow backup past first
* line if nothing has been
for (; nmove
< 0; nmove
++) {
/* may need to allocate here */
for (; nmove
> 0 && l
->l_next
; nmove
--)
for (; nmove
> 0; nmove
--) {
this_line
= cur_line
+ adjust
;
nmove
= this_line
- nflushd_lines
;
if (nmove
>= max_bufd_lines
+ BUFFER_MARGIN
) {
nflushd_lines
+= nmove
- max_bufd_lines
;
flush_lines(nmove
- max_bufd_lines
);
/* grow line's buffer? */
if (l
->l_line_len
+ 1 >= l
->l_lsize
) {
need
= l
->l_lsize
? l
->l_lsize
* 2 : 90;
l
->l_line
= (CHAR
*)xmalloc((void *) l
->l_line
,
(unsigned) need
* sizeof(CHAR
));
c
= &l
->l_line
[l
->l_line_len
++];
* If things are put in out of order, they will need sorting
if (cur_col
< l
->l_max_col
)
/* goto the last line that had a character on it */
for (; l
->l_next
; l
= l
->l_next
)
flush_lines(this_line
- nflushd_lines
+ extra_lines
+ 1);
/* make sure we leave things in a sane state */
if (last_set
!= CS_NORMAL
)
/* flush out the last few blank lines */
nblank_lines
= max_line
- this_line
;
/* missing a \n on the last line? */
(void)free((void *)l
->l_line
);
* Print a number of newline/half newlines. If fine flag is set, nblank_lines
* is the number of half line feeds, otherwise it is the number of whole line
* Write a line to stdout taking care of space to tab conversion (-h flag)
* and character set shifts.
int nchars
, last_col
, this_col
;
static int count_size
, *count
, i
, save
, sorted_size
, tot
;
* Do an O(n) sort on l->l_line by column being careful to
* preserve the order of characters in the same column.
if (l
->l_lsize
> sorted_size
) {
sorted_size
= l
->l_lsize
;
sorted
= (CHAR
*)xmalloc((void *)sorted
,
(unsigned)sizeof(CHAR
) * sorted_size
);
if (l
->l_max_col
>= count_size
) {
count_size
= l
->l_max_col
+ 1;
count
= (int *)xmalloc((void *)count
,
(unsigned)sizeof(int) * count_size
);
bzero((char *)count
, sizeof(int) * l
->l_max_col
+ 1);
for (i
= nchars
, c
= l
->l_line
; --i
>= 0; c
++)
* calculate running total (shifted down by 1) to use as
for (tot
= 0, i
= 0; i
<= l
->l_max_col
; i
++) {
for (i
= nchars
, c
= l
->l_line
; --i
>= 0; c
++)
sorted
[count
[c
->c_column
]++] = *c
;
} while (--nchars
> 0 && this_col
== endc
->c_column
);
/* if -b only print last character */
if (this_col
> last_col
) {
int nspace
= this_col
- last_col
;
if (compress_spaces
&& nspace
> 1) {
while ((ntabs
= TABSTOP(last_col
, 8)) <= this_col
) {
nspace
= this_col
- last_col
;
ntabs = this_col / 8 - last_col / 8;
if (c
->c_set
!= last_set
) {
static LINE
*line_freelist
;
l
= (LINE
*)xmalloc((void *)NULL
, sizeof(LINE
) * NALLOC
);
for (i
= 1; i
< NALLOC
; i
++, l
++)
line_freelist
= l
->l_next
;
l
->l_next
= line_freelist
;
if (!(p
= (void *)realloc(p
, size
))) {
(void)fprintf(stderr
, "col: %s.\n", strerror(ENOMEM
));
(void)fprintf(stderr
, "usage: col [-bfx] [-l nline]\n");
(void)fprintf(stderr
, "col: write error.\n");
"col: warning: can't back up %s.\n", line
< 0 ?
"past first line" : "-- line already flushed");