1994-05-27 12:33:43 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 1990, 1993, 1994
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* are met:
|
|
|
|
* 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.
|
|
|
|
* 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
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef lint
|
1998-12-06 22:58:23 +00:00
|
|
|
static const char copyright[] =
|
1994-05-27 12:33:43 +00:00
|
|
|
"@(#) Copyright (c) 1990, 1993, 1994\n\
|
|
|
|
The Regents of the University of California. All rights reserved.\n";
|
2001-12-02 23:34:16 +00:00
|
|
|
#endif
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2001-12-10 21:13:08 +00:00
|
|
|
#if 0
|
1994-05-27 12:33:43 +00:00
|
|
|
#ifndef lint
|
2001-12-10 21:13:08 +00:00
|
|
|
static char sccsid[] = "@(#)col.c 8.5 (Berkeley) 5/4/95";
|
2000-03-26 14:23:23 +00:00
|
|
|
#endif
|
2001-12-10 21:13:08 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2015-05-02 12:22:24 +00:00
|
|
|
#include <sys/capsicum.h>
|
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
#include <err.h>
|
2015-05-02 12:22:24 +00:00
|
|
|
#include <errno.h>
|
2004-07-29 07:23:37 +00:00
|
|
|
#include <locale.h>
|
1994-05-27 12:33:43 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2004-07-29 07:28:26 +00:00
|
|
|
#include <string.h>
|
2015-05-02 12:22:24 +00:00
|
|
|
#include <termios.h>
|
1997-03-11 13:43:33 +00:00
|
|
|
#include <unistd.h>
|
2004-07-29 07:23:37 +00:00
|
|
|
#include <wchar.h>
|
|
|
|
#include <wctype.h>
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
#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) */
|
2015-05-01 17:18:07 +00:00
|
|
|
#define RLF '7' /* ESC-7 reverse line feed */
|
|
|
|
#define RHLF '8' /* ESC-8 reverse half-line feed */
|
|
|
|
#define FHLF '9' /* ESC-9 forward half-line feed */
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
/* build up at least this many lines before flushing them out */
|
|
|
|
#define BUFFER_MARGIN 32
|
|
|
|
|
|
|
|
typedef char CSET;
|
|
|
|
|
|
|
|
typedef struct char_str {
|
|
|
|
#define CS_NORMAL 1
|
|
|
|
#define CS_ALTERNATE 2
|
|
|
|
short c_column; /* column character is in */
|
|
|
|
CSET c_set; /* character set (currently only 2) */
|
2004-07-29 07:23:37 +00:00
|
|
|
wchar_t c_char; /* character in question */
|
|
|
|
int c_width; /* character width */
|
1994-05-27 12:33:43 +00:00
|
|
|
} CHAR;
|
|
|
|
|
|
|
|
typedef struct line_str LINE;
|
|
|
|
struct line_str {
|
|
|
|
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 */
|
|
|
|
};
|
|
|
|
|
2015-05-08 22:11:54 +00:00
|
|
|
static void addto_lineno(int *, int);
|
2011-11-06 08:14:22 +00:00
|
|
|
static LINE *alloc_line(void);
|
|
|
|
static void dowarn(int);
|
|
|
|
static void flush_line(LINE *);
|
|
|
|
static void flush_lines(int);
|
|
|
|
static void flush_blanks(void);
|
|
|
|
static void free_line(LINE *);
|
|
|
|
static void usage(void);
|
|
|
|
|
|
|
|
static CSET last_set; /* char_set of last char printed */
|
|
|
|
static LINE *lines;
|
|
|
|
static int compress_spaces; /* if doing space -> tab conversion */
|
|
|
|
static int fine; /* if `fine' resolution (half lines) */
|
2015-05-08 22:11:54 +00:00
|
|
|
static int max_bufd_lines; /* max # of half lines to keep in memory */
|
2011-11-06 08:14:22 +00:00
|
|
|
static int nblank_lines; /* # blanks after last flushed line */
|
|
|
|
static int no_backspaces; /* if not to output any backspaces */
|
|
|
|
static int pass_unknown_seqs; /* pass unknown control sequences */
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
#define PUTC(ch) \
|
2001-06-17 03:56:00 +00:00
|
|
|
do { \
|
2004-07-29 07:23:37 +00:00
|
|
|
if (putwchar(ch) == WEOF) \
|
2001-06-17 03:56:00 +00:00
|
|
|
errx(1, "write error"); \
|
1995-01-17 07:02:31 +00:00
|
|
|
} while (0)
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
int
|
2002-09-04 23:29:10 +00:00
|
|
|
main(int argc, char **argv)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
2004-07-29 07:23:37 +00:00
|
|
|
wint_t ch;
|
1994-05-27 12:33:43 +00:00
|
|
|
CHAR *c;
|
|
|
|
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 */
|
2004-07-29 07:23:37 +00:00
|
|
|
int adjust, opt, warned, width;
|
2015-05-08 22:11:54 +00:00
|
|
|
const char *errstr;
|
2015-05-02 12:22:24 +00:00
|
|
|
cap_rights_t rights;
|
|
|
|
unsigned long cmd;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2001-06-17 03:56:00 +00:00
|
|
|
(void)setlocale(LC_CTYPE, "");
|
1995-10-24 06:15:05 +00:00
|
|
|
|
2015-05-02 12:22:24 +00:00
|
|
|
cap_rights_init(&rights, CAP_FSTAT, CAP_READ);
|
|
|
|
if (cap_rights_limit(STDIN_FILENO, &rights) < 0 && errno != ENOSYS)
|
|
|
|
err(1, "unable to limit rights for stdin");
|
|
|
|
cap_rights_init(&rights, CAP_FSTAT, CAP_WRITE, CAP_IOCTL);
|
|
|
|
if (cap_rights_limit(STDOUT_FILENO, &rights) < 0 && errno != ENOSYS)
|
|
|
|
err(1, "unable to limit rights for stdout");
|
|
|
|
cmd = TIOCGETA; /* required by isatty(3) in printf(3) */
|
|
|
|
if (cap_ioctls_limit(STDOUT_FILENO, &cmd, 1) < 0 && errno != ENOSYS)
|
|
|
|
err(1, "unable to limit ioctls for stdout");
|
|
|
|
|
|
|
|
if (cap_enter() < 0 && errno != ENOSYS)
|
|
|
|
err(1, "unable to enter capability mode");
|
|
|
|
|
2015-05-08 22:11:54 +00:00
|
|
|
max_bufd_lines = 256;
|
1994-05-27 12:33:43 +00:00
|
|
|
compress_spaces = 1; /* compress spaces into tabs */
|
2001-06-17 04:24:16 +00:00
|
|
|
while ((opt = getopt(argc, argv, "bfhl:px")) != -1)
|
1994-05-27 12:33:43 +00:00
|
|
|
switch (opt) {
|
|
|
|
case 'b': /* do not output backspaces */
|
|
|
|
no_backspaces = 1;
|
|
|
|
break;
|
|
|
|
case 'f': /* allow half forward line feeds */
|
|
|
|
fine = 1;
|
|
|
|
break;
|
|
|
|
case 'h': /* compress spaces into tabs */
|
|
|
|
compress_spaces = 1;
|
|
|
|
break;
|
|
|
|
case 'l': /* buffered line count */
|
2015-05-08 22:11:54 +00:00
|
|
|
max_bufd_lines = strtonum(optarg, 1,
|
|
|
|
(INT_MAX - BUFFER_MARGIN) / 2, &errstr) * 2;
|
|
|
|
if (errstr != NULL)
|
|
|
|
errx(1, "bad -l argument, %s: %s", errstr,
|
|
|
|
optarg);
|
1994-05-27 12:33:43 +00:00
|
|
|
break;
|
2001-06-17 04:24:16 +00:00
|
|
|
case 'p': /* pass unknown control sequences */
|
|
|
|
pass_unknown_seqs = 1;
|
|
|
|
break;
|
1994-05-27 12:33:43 +00:00
|
|
|
case 'x': /* do not compress spaces into tabs */
|
|
|
|
compress_spaces = 0;
|
|
|
|
break;
|
|
|
|
case '?':
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (optind != argc)
|
|
|
|
usage();
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
2004-07-29 07:23:37 +00:00
|
|
|
while ((ch = getwchar()) != WEOF) {
|
|
|
|
if (!iswgraph(ch)) {
|
1994-05-27 12:33:43 +00:00
|
|
|
switch (ch) {
|
|
|
|
case BS: /* can't go back further */
|
|
|
|
if (cur_col == 0)
|
|
|
|
continue;
|
|
|
|
--cur_col;
|
|
|
|
continue;
|
|
|
|
case CR:
|
|
|
|
cur_col = 0;
|
|
|
|
continue;
|
|
|
|
case ESC: /* just ignore EOF */
|
2004-07-29 07:23:37 +00:00
|
|
|
switch(getwchar()) {
|
2015-05-10 11:41:38 +00:00
|
|
|
/*
|
|
|
|
* In the input stream, accept both the
|
|
|
|
* XPG5 sequences ESC-digit and the
|
|
|
|
* traditional BSD sequences ESC-ctrl.
|
|
|
|
*/
|
|
|
|
case '\007':
|
|
|
|
/* FALLTHROUGH */
|
1994-05-27 12:33:43 +00:00
|
|
|
case RLF:
|
2015-05-08 22:11:54 +00:00
|
|
|
addto_lineno(&cur_line, -2);
|
1994-05-27 12:33:43 +00:00
|
|
|
break;
|
2015-05-10 11:41:38 +00:00
|
|
|
case '\010':
|
|
|
|
/* FALLTHROUGH */
|
1994-05-27 12:33:43 +00:00
|
|
|
case RHLF:
|
2015-05-08 22:11:54 +00:00
|
|
|
addto_lineno(&cur_line, -1);
|
1994-05-27 12:33:43 +00:00
|
|
|
break;
|
2015-05-10 11:41:38 +00:00
|
|
|
case '\011':
|
|
|
|
/* FALLTHROUGH */
|
1994-05-27 12:33:43 +00:00
|
|
|
case FHLF:
|
2015-05-08 22:11:54 +00:00
|
|
|
addto_lineno(&cur_line, 1);
|
1994-05-27 12:33:43 +00:00
|
|
|
if (cur_line > max_line)
|
|
|
|
max_line = cur_line;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
case NL:
|
2015-05-08 22:11:54 +00:00
|
|
|
addto_lineno(&cur_line, 2);
|
1994-05-27 12:33:43 +00:00
|
|
|
if (cur_line > max_line)
|
|
|
|
max_line = cur_line;
|
|
|
|
cur_col = 0;
|
|
|
|
continue;
|
|
|
|
case SPACE:
|
|
|
|
++cur_col;
|
|
|
|
continue;
|
|
|
|
case SI:
|
|
|
|
cur_set = CS_NORMAL;
|
|
|
|
continue;
|
|
|
|
case SO:
|
|
|
|
cur_set = CS_ALTERNATE;
|
|
|
|
continue;
|
|
|
|
case TAB: /* adjust column */
|
|
|
|
cur_col |= 7;
|
|
|
|
++cur_col;
|
|
|
|
continue;
|
|
|
|
case VT:
|
2015-05-08 22:11:54 +00:00
|
|
|
addto_lineno(&cur_line, -2);
|
1994-05-27 12:33:43 +00:00
|
|
|
continue;
|
|
|
|
}
|
2004-07-29 07:23:37 +00:00
|
|
|
if (iswspace(ch)) {
|
|
|
|
if ((width = wcwidth(ch)) > 0)
|
|
|
|
cur_col += width;
|
|
|
|
continue;
|
|
|
|
}
|
2001-06-17 04:24:16 +00:00
|
|
|
if (!pass_unknown_seqs)
|
|
|
|
continue;
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Must stuff ch in a line - are we at the right one? */
|
2015-05-08 22:11:54 +00:00
|
|
|
if (cur_line + adjust != this_line) {
|
1994-05-27 12:33:43 +00:00
|
|
|
LINE *lnew;
|
2015-05-08 22:11:54 +00:00
|
|
|
|
|
|
|
/* round up to next line */
|
|
|
|
adjust = !fine && (cur_line & 1);
|
|
|
|
|
|
|
|
if (cur_line + adjust < this_line) {
|
|
|
|
while (cur_line + adjust < this_line &&
|
|
|
|
l->l_prev != NULL) {
|
1994-05-27 12:33:43 +00:00
|
|
|
l = l->l_prev;
|
2015-05-08 22:11:54 +00:00
|
|
|
this_line--;
|
|
|
|
}
|
|
|
|
if (cur_line + adjust < this_line) {
|
1994-05-27 12:33:43 +00:00
|
|
|
if (nflushd_lines == 0) {
|
|
|
|
/*
|
|
|
|
* Allow backup past first
|
|
|
|
* line if nothing has been
|
|
|
|
* flushed yet.
|
|
|
|
*/
|
2015-05-08 22:11:54 +00:00
|
|
|
while (cur_line + adjust
|
|
|
|
< this_line) {
|
1994-05-27 12:33:43 +00:00
|
|
|
lnew = alloc_line();
|
|
|
|
l->l_prev = lnew;
|
|
|
|
lnew->l_next = l;
|
|
|
|
l = lines = lnew;
|
|
|
|
extra_lines++;
|
2015-05-08 22:11:54 +00:00
|
|
|
this_line--;
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!warned++)
|
|
|
|
dowarn(cur_line);
|
2015-05-08 22:11:54 +00:00
|
|
|
cur_line = this_line - adjust;
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* may need to allocate here */
|
2015-05-08 22:11:54 +00:00
|
|
|
while (cur_line + adjust > this_line) {
|
|
|
|
if (l->l_next == NULL) {
|
|
|
|
l->l_next = alloc_line();
|
|
|
|
l->l_next->l_prev = l;
|
|
|
|
}
|
1994-05-27 12:33:43 +00:00
|
|
|
l = l->l_next;
|
2015-05-08 22:11:54 +00:00
|
|
|
this_line++;
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
}
|
2015-05-08 22:11:54 +00:00
|
|
|
if (this_line > nflushd_lines &&
|
|
|
|
this_line - nflushd_lines >=
|
|
|
|
max_bufd_lines + BUFFER_MARGIN) {
|
|
|
|
if (extra_lines) {
|
|
|
|
flush_lines(extra_lines);
|
|
|
|
extra_lines = 0;
|
|
|
|
}
|
|
|
|
flush_lines(this_line - nflushd_lines -
|
|
|
|
max_bufd_lines);
|
|
|
|
nflushd_lines = this_line - max_bufd_lines;
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/* grow line's buffer? */
|
|
|
|
if (l->l_line_len + 1 >= l->l_lsize) {
|
|
|
|
int need;
|
|
|
|
|
|
|
|
need = l->l_lsize ? l->l_lsize * 2 : 90;
|
2001-07-24 14:14:11 +00:00
|
|
|
if ((l->l_line = realloc(l->l_line,
|
|
|
|
(unsigned)need * sizeof(CHAR))) == NULL)
|
2015-03-31 15:03:58 +00:00
|
|
|
err(1, NULL);
|
1994-05-27 12:33:43 +00:00
|
|
|
l->l_lsize = need;
|
|
|
|
}
|
|
|
|
c = &l->l_line[l->l_line_len++];
|
|
|
|
c->c_char = ch;
|
|
|
|
c->c_set = cur_set;
|
|
|
|
c->c_column = cur_col;
|
2004-07-29 07:23:37 +00:00
|
|
|
c->c_width = wcwidth(ch);
|
1994-05-27 12:33:43 +00:00
|
|
|
/*
|
|
|
|
* If things are put in out of order, they will need sorting
|
|
|
|
* when it is flushed.
|
|
|
|
*/
|
|
|
|
if (cur_col < l->l_max_col)
|
|
|
|
l->l_needs_sort = 1;
|
|
|
|
else
|
|
|
|
l->l_max_col = cur_col;
|
2004-07-29 07:23:37 +00:00
|
|
|
if (c->c_width > 0)
|
|
|
|
cur_col += c->c_width;
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
2004-07-29 07:23:37 +00:00
|
|
|
if (ferror(stdin))
|
|
|
|
err(1, NULL);
|
2015-05-08 22:11:54 +00:00
|
|
|
if (extra_lines)
|
|
|
|
flush_lines(extra_lines);
|
1997-03-11 13:43:33 +00:00
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
/* goto the last line that had a character on it */
|
|
|
|
for (; l->l_next; l = l->l_next)
|
|
|
|
this_line++;
|
2015-05-08 22:11:54 +00:00
|
|
|
flush_lines(this_line - nflushd_lines + 1);
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
/* make sure we leave things in a sane state */
|
|
|
|
if (last_set != CS_NORMAL)
|
2015-05-01 17:33:32 +00:00
|
|
|
PUTC(SI);
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
/* flush out the last few blank lines */
|
2015-05-08 22:11:54 +00:00
|
|
|
if (max_line > this_line)
|
|
|
|
nblank_lines = max_line - this_line;
|
1994-05-27 12:33:43 +00:00
|
|
|
if (max_line & 1)
|
|
|
|
nblank_lines++;
|
|
|
|
flush_blanks();
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2011-11-06 08:14:22 +00:00
|
|
|
static void
|
2002-09-04 23:29:10 +00:00
|
|
|
flush_lines(int nflush)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
|
|
|
LINE *l;
|
|
|
|
|
|
|
|
while (--nflush >= 0) {
|
|
|
|
l = lines;
|
|
|
|
lines = l->l_next;
|
|
|
|
if (l->l_line) {
|
|
|
|
flush_blanks();
|
|
|
|
flush_line(l);
|
|
|
|
}
|
2015-05-08 22:11:54 +00:00
|
|
|
if (l->l_line || l->l_next)
|
|
|
|
nblank_lines++;
|
1994-05-27 12:33:43 +00:00
|
|
|
if (l->l_line)
|
2001-06-17 03:56:00 +00:00
|
|
|
(void)free(l->l_line);
|
1994-05-27 12:33:43 +00:00
|
|
|
free_line(l);
|
|
|
|
}
|
|
|
|
if (lines)
|
|
|
|
lines->l_prev = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
* feeds.
|
|
|
|
*/
|
2011-11-06 08:14:22 +00:00
|
|
|
static void
|
2002-09-04 23:29:10 +00:00
|
|
|
flush_blanks(void)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
|
|
|
int half, i, nb;
|
|
|
|
|
|
|
|
half = 0;
|
|
|
|
nb = nblank_lines;
|
|
|
|
if (nb & 1) {
|
|
|
|
if (fine)
|
|
|
|
half = 1;
|
|
|
|
else
|
|
|
|
nb++;
|
|
|
|
}
|
|
|
|
nb /= 2;
|
|
|
|
for (i = nb; --i >= 0;)
|
|
|
|
PUTC('\n');
|
|
|
|
if (half) {
|
2015-05-01 17:33:32 +00:00
|
|
|
PUTC(ESC);
|
|
|
|
PUTC(FHLF);
|
1994-05-27 12:33:43 +00:00
|
|
|
if (!nb)
|
|
|
|
PUTC('\r');
|
|
|
|
}
|
|
|
|
nblank_lines = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Write a line to stdout taking care of space to tab conversion (-h flag)
|
|
|
|
* and character set shifts.
|
|
|
|
*/
|
2011-11-06 08:14:22 +00:00
|
|
|
static void
|
2002-09-04 23:29:10 +00:00
|
|
|
flush_line(LINE *l)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
|
|
|
CHAR *c, *endc;
|
2006-07-18 07:16:25 +00:00
|
|
|
int i, j, nchars, last_col, save, this_col, tot;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
last_col = 0;
|
|
|
|
nchars = l->l_line_len;
|
|
|
|
|
|
|
|
if (l->l_needs_sort) {
|
|
|
|
static CHAR *sorted;
|
2006-07-18 07:16:25 +00:00
|
|
|
static int count_size, *count, sorted_size;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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;
|
2001-07-24 14:14:11 +00:00
|
|
|
if ((sorted = realloc(sorted,
|
|
|
|
(unsigned)sizeof(CHAR) * sorted_size)) == NULL)
|
2015-03-31 15:03:58 +00:00
|
|
|
err(1, NULL);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
if (l->l_max_col >= count_size) {
|
|
|
|
count_size = l->l_max_col + 1;
|
2001-07-24 14:14:11 +00:00
|
|
|
if ((count = realloc(count,
|
|
|
|
(unsigned)sizeof(int) * count_size)) == NULL)
|
2015-03-31 15:03:58 +00:00
|
|
|
err(1, NULL);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
2001-06-17 03:56:00 +00:00
|
|
|
memset(count, 0, sizeof(int) * l->l_max_col + 1);
|
1994-05-27 12:33:43 +00:00
|
|
|
for (i = nchars, c = l->l_line; --i >= 0; c++)
|
|
|
|
count[c->c_column]++;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* calculate running total (shifted down by 1) to use as
|
|
|
|
* indices into new line.
|
|
|
|
*/
|
|
|
|
for (tot = 0, i = 0; i <= l->l_max_col; i++) {
|
|
|
|
save = count[i];
|
|
|
|
count[i] = tot;
|
|
|
|
tot += save;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = nchars, c = l->l_line; --i >= 0; c++)
|
|
|
|
sorted[count[c->c_column]++] = *c;
|
|
|
|
c = sorted;
|
|
|
|
} else
|
|
|
|
c = l->l_line;
|
|
|
|
while (nchars > 0) {
|
|
|
|
this_col = c->c_column;
|
|
|
|
endc = c;
|
|
|
|
do {
|
|
|
|
++endc;
|
|
|
|
} while (--nchars > 0 && this_col == endc->c_column);
|
|
|
|
|
|
|
|
/* if -b only print last character */
|
2004-07-29 07:23:37 +00:00
|
|
|
if (no_backspaces) {
|
1994-05-27 12:33:43 +00:00
|
|
|
c = endc - 1;
|
2004-07-29 07:23:37 +00:00
|
|
|
if (nchars > 0 &&
|
|
|
|
this_col + c->c_width > endc->c_column)
|
|
|
|
continue;
|
|
|
|
}
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
if (this_col > last_col) {
|
|
|
|
int nspace = this_col - last_col;
|
|
|
|
|
|
|
|
if (compress_spaces && nspace > 1) {
|
1995-01-17 07:02:31 +00:00
|
|
|
while (1) {
|
2010-07-15 00:04:14 +00:00
|
|
|
int tab_col, tab_size;
|
1995-01-17 07:02:31 +00:00
|
|
|
|
|
|
|
tab_col = (last_col + 8) & ~7;
|
|
|
|
if (tab_col > this_col)
|
|
|
|
break;
|
|
|
|
tab_size = tab_col - last_col;
|
|
|
|
if (tab_size == 1)
|
|
|
|
PUTC(' ');
|
|
|
|
else
|
|
|
|
PUTC('\t');
|
|
|
|
nspace -= tab_size;
|
|
|
|
last_col = tab_col;
|
|
|
|
}
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
while (--nspace >= 0)
|
|
|
|
PUTC(' ');
|
|
|
|
last_col = this_col;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
if (c->c_set != last_set) {
|
|
|
|
switch (c->c_set) {
|
|
|
|
case CS_NORMAL:
|
2015-05-01 17:33:32 +00:00
|
|
|
PUTC(SI);
|
1994-05-27 12:33:43 +00:00
|
|
|
break;
|
|
|
|
case CS_ALTERNATE:
|
2015-05-01 17:33:32 +00:00
|
|
|
PUTC(SO);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
last_set = c->c_set;
|
|
|
|
}
|
|
|
|
PUTC(c->c_char);
|
2004-07-29 07:23:37 +00:00
|
|
|
if ((c + 1) < endc)
|
2005-11-13 20:30:13 +00:00
|
|
|
for (j = 0; j < c->c_width; j++)
|
2004-07-29 07:23:37 +00:00
|
|
|
PUTC('\b');
|
1994-05-27 12:33:43 +00:00
|
|
|
if (++c >= endc)
|
|
|
|
break;
|
|
|
|
}
|
2004-07-29 07:23:37 +00:00
|
|
|
last_col += (c - 1)->c_width;
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-08 22:11:54 +00:00
|
|
|
/*
|
|
|
|
* Increment or decrement a line number, checking for overflow.
|
|
|
|
* Stop one below INT_MAX such that the adjust variable is safe.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
addto_lineno(int *lno, int offset)
|
|
|
|
{
|
|
|
|
if (offset > 0) {
|
|
|
|
if (*lno >= INT_MAX - offset)
|
|
|
|
errx(1, "too many lines");
|
|
|
|
} else {
|
|
|
|
if (*lno < INT_MIN - offset)
|
|
|
|
errx(1, "too many reverse line feeds");
|
|
|
|
}
|
|
|
|
*lno += offset;
|
|
|
|
}
|
|
|
|
|
1994-05-27 12:33:43 +00:00
|
|
|
#define NALLOC 64
|
|
|
|
|
|
|
|
static LINE *line_freelist;
|
|
|
|
|
2011-11-06 08:14:22 +00:00
|
|
|
static LINE *
|
2002-09-04 23:29:10 +00:00
|
|
|
alloc_line(void)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
|
|
|
LINE *l;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (!line_freelist) {
|
2001-07-24 14:14:11 +00:00
|
|
|
if ((l = realloc(NULL, sizeof(LINE) * NALLOC)) == NULL)
|
2015-03-31 15:03:58 +00:00
|
|
|
err(1, NULL);
|
1994-05-27 12:33:43 +00:00
|
|
|
line_freelist = l;
|
|
|
|
for (i = 1; i < NALLOC; i++, l++)
|
|
|
|
l->l_next = l + 1;
|
|
|
|
l->l_next = NULL;
|
|
|
|
}
|
|
|
|
l = line_freelist;
|
|
|
|
line_freelist = l->l_next;
|
|
|
|
|
|
|
|
memset(l, 0, sizeof(LINE));
|
|
|
|
return (l);
|
|
|
|
}
|
|
|
|
|
2011-11-06 08:14:22 +00:00
|
|
|
static void
|
2002-09-04 23:29:10 +00:00
|
|
|
free_line(LINE *l)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
l->l_next = line_freelist;
|
|
|
|
line_freelist = l;
|
|
|
|
}
|
|
|
|
|
2011-11-06 08:14:22 +00:00
|
|
|
static void
|
2002-09-04 23:29:10 +00:00
|
|
|
usage(void)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
|
|
|
|
2001-06-17 04:24:16 +00:00
|
|
|
(void)fprintf(stderr, "usage: col [-bfhpx] [-l nline]\n");
|
1994-05-27 12:33:43 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2011-11-06 08:14:22 +00:00
|
|
|
static void
|
2002-09-04 23:29:10 +00:00
|
|
|
dowarn(int line)
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
warnx("warning: can't back up %s",
|
|
|
|
line < 0 ? "past first line" : "-- line already flushed");
|
|
|
|
}
|