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