]> git.ipfire.org Git - thirdparty/util-linux.git/blame - text-utils/col.c
textual: use manual tail usage() macro
[thirdparty/util-linux.git] / text-utils / col.c
CommitLineData
6dbe3af9
KZ
1/*-
2 * Copyright (c) 1990 The Regents of the University of California.
3 * All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Michael Rendell of the Memorial University of Newfoundland.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
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.
23 *
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
34 * SUCH DAMAGE.
35 *
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)
b50945d4 40 * 1999-02-22 Arkadiusz Miƛkiewicz <misiek@pld.ORG.PL>
eb63b9b8
KZ
41 * added Native Language Support
42 * 1999-09-19 Bruno Haible <haible@clisp.cons.org>
43 * modified to work correctly in multi-byte locales
7eda085c 44 *
6dbe3af9
KZ
45 */
46
7d2600e2
SK
47/*
48 * This command is deprecated. The utility is in maintenance mode,
49 * meaning we keep them in source tree for backward compatibility
50 * only. Do not waste time making this command better, unless the
51 * fix is about security or other very critical issue.
52 *
53 * See Documentation/deprecated.txt for more information.
54 */
55
fd6b7a7f 56#include <stdlib.h>
6dbe3af9
KZ
57#include <errno.h>
58#include <ctype.h>
59#include <string.h>
60#include <stdio.h>
fd6b7a7f 61#include <unistd.h>
f688c298 62#include <getopt.h>
eb63b9b8 63
04faca4a
DB
64#include "nls.h"
65#include "xalloc.h"
eb63b9b8 66#include "widechar.h"
b07e72d9 67#include "strutils.h"
b87cbe84 68#include "closestream.h"
6dbe3af9
KZ
69
70#define BS '\b' /* backspace */
71#define TAB '\t' /* tab */
72#define SPACE ' ' /* space */
73#define NL '\n' /* newline */
74#define CR '\r' /* carriage return */
75#define ESC '\033' /* escape */
76#define SI '\017' /* shift in to normal character set */
77#define SO '\016' /* shift out to alternate character set */
78#define VT '\013' /* vertical tab (aka reverse line feed) */
79#define RLF '\007' /* ESC-07 reverse line feed */
80#define RHLF '\010' /* ESC-010 reverse half-line feed */
81#define FHLF '\011' /* ESC-011 forward half-line feed */
82
83/* build up at least this many lines before flushing them out */
84#define BUFFER_MARGIN 32
85
86typedef char CSET;
87
88typedef struct char_str {
89#define CS_NORMAL 1
90#define CS_ALTERNATE 2
91 short c_column; /* column character is in */
92 CSET c_set; /* character set (currently only 2) */
eb63b9b8 93 wchar_t c_char; /* character in question */
66ee8158 94 int c_width; /* character width */
6dbe3af9
KZ
95} CHAR;
96
97typedef struct line_str LINE;
98struct line_str {
99 CHAR *l_line; /* characters on the line */
100 LINE *l_prev; /* previous line */
101 LINE *l_next; /* next line */
102 int l_lsize; /* allocated sizeof l_line */
103 int l_line_len; /* strlen(l_line) */
104 int l_needs_sort; /* set if chars went in out of order */
105 int l_max_col; /* max column in the line */
106};
107
fd6b7a7f
KZ
108void free_line(LINE *l);
109void flush_line(LINE *l);
110void flush_lines(int);
111void flush_blanks(void);
fd6b7a7f 112LINE *alloc_line(void);
6dbe3af9
KZ
113
114CSET last_set; /* char_set of last char printed */
115LINE *lines;
116int compress_spaces; /* if doing space -> tab conversion */
117int fine; /* if `fine' resolution (half lines) */
3b56eea7 118unsigned max_bufd_lines; /* max # lines to keep in memory */
6dbe3af9
KZ
119int nblank_lines; /* # blanks after last flushed line */
120int no_backspaces; /* if not to output any backspaces */
22853e4a 121int pass_unknown_seqs; /* whether to pass unknown control sequences */
6dbe3af9
KZ
122
123#define PUTC(ch) \
eb63b9b8 124 if (putwchar(ch) == WEOF) \
6dbe3af9
KZ
125 wrerr();
126
f688c298 127static void __attribute__((__noreturn__)) usage(FILE *out)
2ebef870 128{
f688c298
SK
129 fprintf(out, _(
130 "\nUsage:\n"
131 " %s [options]\n"), program_invocation_short_name);
132
133 fprintf(out, _(
134 "\nOptions:\n"
135 " -b, --no-backspaces do not output backspaces\n"
136 " -f, --fine permit forward half line feeds\n"
137 " -p, --pass pass unknown control sequences\n"
138 " -h, --tabs convert spaces to tabs\n"
139 " -x, --spaces convert tabs to spaces\n"
140 " -l, --lines NUM buffer at least NUM lines\n"
141 " -V, --version output version information and exit\n"
142 " -H, --help display this help and exit\n\n"));
143
144 fprintf(out, _(
145 "%s reads from standard input and writes to standard output\n\n"),
146 program_invocation_short_name);
147
a587cc55 148 fprintf(out, USAGE_MAN_TAIL("col(1)"));
f688c298 149 exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
2ebef870
KZ
150}
151
0825179f 152static void __attribute__((__noreturn__)) wrerr(void)
2ebef870 153{
48899b9e 154 errx(EXIT_FAILURE, _("write error"));
2ebef870
KZ
155}
156
fd6b7a7f 157int main(int argc, char **argv)
6dbe3af9 158{
eb63b9b8 159 register wint_t ch;
6dbe3af9
KZ
160 CHAR *c;
161 CSET cur_set; /* current character set */
162 LINE *l; /* current line */
163 int extra_lines; /* # of lines above first line */
164 int cur_col; /* current column */
165 int cur_line; /* line number of current position */
166 int max_line; /* max value of cur_line */
167 int this_line; /* line l points to */
168 int nflushd_lines; /* number of lines that were flushed */
169 int adjust, opt, warned;
2ebef870 170 int ret = EXIT_SUCCESS;
6dbe3af9 171
f688c298
SK
172 static const struct option longopts[] = {
173 { "no-backspaces", no_argument, 0, 'b' },
174 { "fine", no_argument, 0, 'f' },
175 { "pass", no_argument, 0, 'p' },
176 { "tabs", no_argument, 0, 'h' },
177 { "spaces", no_argument, 0, 'x' },
178 { "lines", required_argument, 0, 'l' },
179 { "version", no_argument, 0, 'V' },
180 { "help", no_argument, 0, 'H' },
181 { NULL, 0, 0, 0 }
182 };
183
7eda085c
KZ
184 setlocale(LC_ALL, "");
185 bindtextdomain(PACKAGE, LOCALEDIR);
186 textdomain(PACKAGE);
b87cbe84 187 atexit(close_stdout);
2ebef870 188
b07e72d9 189 max_bufd_lines = 128 * 2;
6dbe3af9 190 compress_spaces = 1; /* compress spaces into tabs */
22853e4a 191 pass_unknown_seqs = 0; /* remove unknown escape sequences */
f688c298
SK
192
193 while ((opt = getopt_long(argc, argv, "bfhl:pxVH", longopts, NULL)) != -1)
6dbe3af9
KZ
194 switch (opt) {
195 case 'b': /* do not output backspaces */
196 no_backspaces = 1;
197 break;
198 case 'f': /* allow half forward line feeds */
199 fine = 1;
200 break;
201 case 'h': /* compress spaces into tabs */
202 compress_spaces = 1;
203 break;
b07e72d9
SK
204 case 'l':
205 /*
206 * Buffered line count, which is a value in half
207 * lines e.g. twice the amount specified.
208 */
3b56eea7 209 max_bufd_lines = strtou32_or_err(optarg, _("bad -l argument")) * 2;
6dbe3af9 210 break;
22853e4a
KZ
211 case 'p':
212 pass_unknown_seqs = 1;
213 break;
6dbe3af9
KZ
214 case 'x': /* do not compress spaces into tabs */
215 compress_spaces = 0;
216 break;
f688c298 217 case 'V':
f6277500 218 printf(UTIL_LINUX_VERSION);
f688c298
SK
219 return EXIT_SUCCESS;
220 case 'H':
221 usage(stdout);
6dbe3af9 222 default:
f688c298 223 usage(stderr);
6dbe3af9
KZ
224 }
225
226 if (optind != argc)
f688c298 227 usage(stderr);
6dbe3af9 228
6dbe3af9
KZ
229 adjust = cur_col = extra_lines = warned = 0;
230 cur_line = max_line = nflushd_lines = this_line = 0;
231 cur_set = last_set = CS_NORMAL;
232 lines = l = alloc_line();
2ebef870 233
8e0ec3b4 234 while (feof(stdin) == 0) {
e9ce5ccc
KZ
235 errno = 0;
236 if ((ch = getwchar()) == WEOF) {
8e0ec3b4 237 if (errno == EILSEQ) {
2ebef870
KZ
238 warn(NULL);
239 ret = EXIT_FAILURE;
e9ce5ccc
KZ
240 }
241 break;
2ebef870 242 }
eb63b9b8 243 if (!iswgraph(ch)) {
6dbe3af9
KZ
244 switch (ch) {
245 case BS: /* can't go back further */
246 if (cur_col == 0)
247 continue;
248 --cur_col;
249 continue;
250 case CR:
251 cur_col = 0;
252 continue;
253 case ESC: /* just ignore EOF */
eb63b9b8 254 switch(getwchar()) {
6dbe3af9
KZ
255 case RLF:
256 cur_line -= 2;
257 break;
258 case RHLF:
259 cur_line--;
260 break;
261 case FHLF:
262 cur_line++;
263 if (cur_line > max_line)
264 max_line = cur_line;
265 }
266 continue;
267 case NL:
268 cur_line += 2;
269 if (cur_line > max_line)
270 max_line = cur_line;
271 cur_col = 0;
272 continue;
273 case SPACE:
274 ++cur_col;
275 continue;
276 case SI:
277 cur_set = CS_NORMAL;
278 continue;
279 case SO:
280 cur_set = CS_ALTERNATE;
281 continue;
282 case TAB: /* adjust column */
283 cur_col |= 7;
284 ++cur_col;
285 continue;
286 case VT:
287 cur_line -= 2;
288 continue;
289 }
66ee8158
KZ
290 if (iswspace(ch)) {
291 if (wcwidth(ch) > 0)
292 cur_col += wcwidth(ch);
293 continue;
294 }
22853e4a
KZ
295 if (!pass_unknown_seqs)
296 continue;
6dbe3af9
KZ
297 }
298
299 /* Must stuff ch in a line - are we at the right one? */
300 if (cur_line != this_line - adjust) {
301 LINE *lnew;
302 int nmove;
303
304 adjust = 0;
305 nmove = cur_line - this_line;
306 if (!fine) {
307 /* round up to next line */
308 if (cur_line & 1) {
309 adjust = 1;
310 nmove++;
311 }
312 }
313 if (nmove < 0) {
314 for (; nmove < 0 && l->l_prev; nmove++)
315 l = l->l_prev;
316 if (nmove) {
317 if (nflushd_lines == 0) {
318 /*
319 * Allow backup past first
320 * line if nothing has been
321 * flushed yet.
322 */
323 for (; nmove < 0; nmove++) {
324 lnew = alloc_line();
325 l->l_prev = lnew;
326 lnew->l_next = l;
327 l = lines = lnew;
328 extra_lines++;
329 }
330 } else {
331 if (!warned++)
2ebef870
KZ
332 warnx(
333 _("warning: can't back up %s."), cur_line < 0 ?
334 _("past first line") : _("-- line already flushed"));
6dbe3af9
KZ
335 cur_line -= nmove;
336 }
337 }
338 } else {
339 /* may need to allocate here */
340 for (; nmove > 0 && l->l_next; nmove--)
341 l = l->l_next;
342 for (; nmove > 0; nmove--) {
343 lnew = alloc_line();
344 lnew->l_prev = l;
345 l->l_next = lnew;
346 l = lnew;
347 }
348 }
349 this_line = cur_line + adjust;
350 nmove = this_line - nflushd_lines;
3b56eea7
KZ
351 if (nmove > 0
352 && (unsigned) nmove >= max_bufd_lines + BUFFER_MARGIN) {
6dbe3af9
KZ
353 nflushd_lines += nmove - max_bufd_lines;
354 flush_lines(nmove - max_bufd_lines);
355 }
356 }
357 /* grow line's buffer? */
358 if (l->l_line_len + 1 >= l->l_lsize) {
359 int need;
360
361 need = l->l_lsize ? l->l_lsize * 2 : 90;
04faca4a
DB
362 l->l_line = (CHAR *)xrealloc((void *) l->l_line,
363 (unsigned) need * sizeof(CHAR));
6dbe3af9
KZ
364 l->l_lsize = need;
365 }
366 c = &l->l_line[l->l_line_len++];
367 c->c_char = ch;
368 c->c_set = cur_set;
369 c->c_column = cur_col;
66ee8158 370 c->c_width = wcwidth(ch);
6dbe3af9
KZ
371 /*
372 * If things are put in out of order, they will need sorting
373 * when it is flushed.
374 */
375 if (cur_col < l->l_max_col)
376 l->l_needs_sort = 1;
377 else
378 l->l_max_col = cur_col;
66ee8158
KZ
379 if (c->c_width > 0)
380 cur_col += c->c_width;
6dbe3af9
KZ
381 }
382 /* goto the last line that had a character on it */
383 for (; l->l_next; l = l->l_next)
384 this_line++;
385 flush_lines(this_line - nflushd_lines + extra_lines + 1);
386
387 /* make sure we leave things in a sane state */
388 if (last_set != CS_NORMAL)
389 PUTC('\017');
390
391 /* flush out the last few blank lines */
392 nblank_lines = max_line - this_line;
393 if (max_line & 1)
394 nblank_lines++;
395 else if (!nblank_lines)
396 /* missing a \n on the last line? */
397 nblank_lines = 2;
398 flush_blanks();
e9ce5ccc 399 return ret;
6dbe3af9
KZ
400}
401
fd6b7a7f 402void flush_lines(int nflush)
6dbe3af9
KZ
403{
404 LINE *l;
405
406 while (--nflush >= 0) {
407 l = lines;
408 lines = l->l_next;
409 if (l->l_line) {
410 flush_blanks();
411 flush_line(l);
412 }
413 nblank_lines++;
9e01635e 414 free((void *)l->l_line);
6dbe3af9
KZ
415 free_line(l);
416 }
417 if (lines)
418 lines->l_prev = NULL;
419}
420
421/*
422 * Print a number of newline/half newlines. If fine flag is set, nblank_lines
423 * is the number of half line feeds, otherwise it is the number of whole line
424 * feeds.
425 */
3acc206d 426void flush_blanks(void)
6dbe3af9
KZ
427{
428 int half, i, nb;
429
430 half = 0;
431 nb = nblank_lines;
432 if (nb & 1) {
433 if (fine)
434 half = 1;
435 else
436 nb++;
437 }
438 nb /= 2;
439 for (i = nb; --i >= 0;)
440 PUTC('\n');
441 if (half) {
442 PUTC('\033');
443 PUTC('9');
444 if (!nb)
445 PUTC('\r');
446 }
447 nblank_lines = 0;
448}
449
450/*
451 * Write a line to stdout taking care of space to tab conversion (-h flag)
452 * and character set shifts.
453 */
fd6b7a7f 454void flush_line(LINE *l)
6dbe3af9
KZ
455{
456 CHAR *c, *endc;
457 int nchars, last_col, this_col;
458
459 last_col = 0;
460 nchars = l->l_line_len;
461
462 if (l->l_needs_sort) {
463 static CHAR *sorted;
464 static int count_size, *count, i, save, sorted_size, tot;
465
466 /*
467 * Do an O(n) sort on l->l_line by column being careful to
468 * preserve the order of characters in the same column.
469 */
470 if (l->l_lsize > sorted_size) {
471 sorted_size = l->l_lsize;
04faca4a
DB
472 sorted = (CHAR *)xrealloc((void *)sorted,
473 (unsigned)sizeof(CHAR) * sorted_size);
6dbe3af9
KZ
474 }
475 if (l->l_max_col >= count_size) {
476 count_size = l->l_max_col + 1;
04faca4a 477 count = (int *)xrealloc((void *)count,
6dbe3af9
KZ
478 (unsigned)sizeof(int) * count_size);
479 }
22853e4a 480 memset(count, 0, sizeof(int) * l->l_max_col + 1);
6dbe3af9
KZ
481 for (i = nchars, c = l->l_line; --i >= 0; c++)
482 count[c->c_column]++;
483
484 /*
485 * calculate running total (shifted down by 1) to use as
486 * indices into new line.
487 */
488 for (tot = 0, i = 0; i <= l->l_max_col; i++) {
489 save = count[i];
490 count[i] = tot;
491 tot += save;
492 }
493
494 for (i = nchars, c = l->l_line; --i >= 0; c++)
495 sorted[count[c->c_column]++] = *c;
496 c = sorted;
497 } else
498 c = l->l_line;
499 while (nchars > 0) {
500 this_col = c->c_column;
501 endc = c;
502 do {
503 ++endc;
504 } while (--nchars > 0 && this_col == endc->c_column);
505
506 /* if -b only print last character */
66ee8158 507 if (no_backspaces) {
6dbe3af9 508 c = endc - 1;
66ee8158
KZ
509 if (nchars > 0 &&
510 this_col + c->c_width > endc->c_column)
511 continue;
512 }
6dbe3af9
KZ
513
514 if (this_col > last_col) {
515 int nspace = this_col - last_col;
516
517 if (compress_spaces && nspace > 1) {
518 int ntabs;
519
520 ntabs = this_col / 8 - last_col / 8;
8e0ec3b4
SK
521 if (ntabs > 0) {
522 nspace = this_col & 7;
523 while (--ntabs >= 0)
524 PUTC('\t');
525 }
6dbe3af9
KZ
526 }
527 while (--nspace >= 0)
528 PUTC(' ');
529 last_col = this_col;
530 }
6dbe3af9
KZ
531
532 for (;;) {
533 if (c->c_set != last_set) {
534 switch (c->c_set) {
535 case CS_NORMAL:
536 PUTC('\017');
537 break;
538 case CS_ALTERNATE:
539 PUTC('\016');
540 }
541 last_set = c->c_set;
542 }
543 PUTC(c->c_char);
8e0ec3b4 544 if ((c + 1) < endc) {
66ee8158
KZ
545 int i;
546 for (i=0; i < c->c_width; i++)
547 PUTC('\b');
548 }
6dbe3af9
KZ
549 if (++c >= endc)
550 break;
6dbe3af9 551 }
8e0ec3b4 552 last_col += (c - 1)->c_width;
6dbe3af9
KZ
553 }
554}
555
556#define NALLOC 64
557
558static LINE *line_freelist;
559
560LINE *
3acc206d 561alloc_line(void)
6dbe3af9
KZ
562{
563 LINE *l;
564 int i;
565
566 if (!line_freelist) {
04faca4a 567 l = xmalloc(sizeof(LINE) * NALLOC);
6dbe3af9
KZ
568 line_freelist = l;
569 for (i = 1; i < NALLOC; i++, l++)
570 l->l_next = l + 1;
571 l->l_next = NULL;
572 }
573 l = line_freelist;
574 line_freelist = l->l_next;
575
22853e4a 576 memset(l, 0, sizeof(LINE));
8e0ec3b4 577 return l;
6dbe3af9
KZ
578}
579
fd6b7a7f 580void free_line(LINE *l)
6dbe3af9
KZ
581{
582 l->l_next = line_freelist;
583 line_freelist = l;
584}