Commit | Line | Data |
---|---|---|
04c9591a FF |
1 | /* From https://svnweb.freebsd.org/base/head/usr.bin/col/, r282722 */ |
2 | ||
3 | /*- | |
4 | * Copyright (c) 1990, 1993, 1994 | |
5 | * The Regents of the University of California. All rights reserved. | |
6 | * | |
7 | * This code is derived from software contributed to Berkeley by | |
8 | * Michael Rendell of the Memorial University of Newfoundland. | |
9 | * | |
10 | * Redistribution and use in source and binary forms, with or without | |
11 | * modification, are permitted provided that the following conditions | |
12 | * are met: | |
13 | * 1. Redistributions of source code must retain the above copyright | |
14 | * notice, this list of conditions and the following disclaimer. | |
15 | * 2. Redistributions in binary form must reproduce the above copyright | |
16 | * notice, this list of conditions and the following disclaimer in the | |
17 | * documentation and/or other materials provided with the distribution. | |
18 | * 4. Neither the name of the University nor the names of its contributors | |
19 | * may be used to endorse or promote products derived from this software | |
20 | * without specific prior written permission. | |
21 | * | |
22 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
23 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
24 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
25 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
26 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
27 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
28 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
30 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
31 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
32 | * SUCH DAMAGE. | |
33 | */ | |
34 | ||
35 | #ifndef lint | |
36 | static const char copyright[] = | |
37 | "@(#) Copyright (c) 1990, 1993, 1994\n\ | |
38 | The Regents of the University of California. All rights reserved.\n"; | |
39 | #endif | |
40 | ||
41 | #if 0 | |
42 | #ifndef lint | |
43 | static char sccsid[] = "@(#)col.c 8.5 (Berkeley) 5/4/95"; | |
44 | #endif | |
45 | #endif | |
46 | ||
47 | #include <sys/cdefs.h> | |
48 | ||
49 | #include <err.h> | |
50 | #include <errno.h> | |
51 | #include <limits.h> | |
52 | #include <locale.h> | |
53 | #include <stdio.h> | |
54 | #include <stdlib.h> | |
55 | #include <string.h> | |
56 | #include <termios.h> | |
57 | #include <unistd.h> | |
58 | #include <wchar.h> | |
59 | #include <wctype.h> | |
60 | ||
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 '7' /* ESC-7 reverse line feed */ | |
71 | #define RHLF '8' /* ESC-8 reverse half-line feed */ | |
72 | #define FHLF '9' /* ESC-9 forward half-line feed */ | |
73 | ||
74 | /* build up at least this many lines before flushing them out */ | |
75 | #define BUFFER_MARGIN 32 | |
76 | ||
77 | typedef char CSET; | |
78 | ||
79 | typedef struct char_str { | |
80 | #define CS_NORMAL 1 | |
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 */ | |
86 | } CHAR; | |
87 | ||
88 | typedef struct line_str LINE; | |
89 | struct line_str { | |
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 */ | |
97 | }; | |
98 | ||
99 | static void addto_lineno(int *, int); | |
100 | static LINE *alloc_line(void); | |
101 | static void dowarn(int); | |
102 | static void flush_line(LINE *); | |
103 | static void flush_lines(int); | |
104 | static void flush_blanks(void); | |
105 | static void free_line(LINE *); | |
106 | static void usage(void); | |
107 | ||
108 | static CSET last_set; /* char_set of last char printed */ | |
109 | static LINE *lines; | |
110 | static int compress_spaces; /* if doing space -> tab conversion */ | |
111 | static int fine; /* if `fine' resolution (half lines) */ | |
112 | static int max_bufd_lines; /* max # of half lines to keep in memory */ | |
113 | static int nblank_lines; /* # blanks after last flushed line */ | |
114 | static int no_backspaces; /* if not to output any backspaces */ | |
115 | static int pass_unknown_seqs; /* pass unknown control sequences */ | |
116 | ||
117 | #define PUTC(ch) \ | |
118 | do { \ | |
119 | if (putwchar(ch) == WEOF) \ | |
120 | errx(1, "write error"); \ | |
121 | } while (0) | |
122 | ||
123 | int | |
124 | main(int argc, char **argv) | |
125 | { | |
126 | wint_t ch; | |
127 | CHAR *c; | |
128 | CSET cur_set; /* current character set */ | |
129 | LINE *l; /* current line */ | |
130 | int extra_lines; /* # of lines above first line */ | |
131 | int cur_col; /* current column */ | |
132 | int cur_line; /* line number of current position */ | |
133 | int max_line; /* max value of cur_line */ | |
134 | int this_line; /* line l points to */ | |
135 | int nflushd_lines; /* number of lines that were flushed */ | |
136 | int adjust, opt, warned, width; | |
137 | const char *errstr; | |
138 | unsigned long cmd; | |
139 | ||
140 | (void)setlocale(LC_CTYPE, ""); | |
141 | ||
142 | max_bufd_lines = 256; | |
143 | compress_spaces = 1; /* compress spaces into tabs */ | |
144 | while ((opt = getopt(argc, argv, "bfhl:px")) != -1) | |
145 | switch (opt) { | |
146 | case 'b': /* do not output backspaces */ | |
147 | no_backspaces = 1; | |
148 | break; | |
149 | case 'f': /* allow half forward line feeds */ | |
150 | fine = 1; | |
151 | break; | |
152 | case 'h': /* compress spaces into tabs */ | |
153 | compress_spaces = 1; | |
154 | break; | |
155 | case 'l': /* buffered line count */ | |
156 | max_bufd_lines = atoi(optarg); | |
157 | break; | |
158 | case 'p': /* pass unknown control sequences */ | |
159 | pass_unknown_seqs = 1; | |
160 | break; | |
161 | case 'x': /* do not compress spaces into tabs */ | |
162 | compress_spaces = 0; | |
163 | break; | |
164 | case '?': | |
165 | default: | |
166 | usage(); | |
167 | } | |
168 | ||
169 | if (optind != argc) | |
170 | usage(); | |
171 | ||
172 | adjust = cur_col = extra_lines = warned = 0; | |
173 | cur_line = max_line = nflushd_lines = this_line = 0; | |
174 | cur_set = last_set = CS_NORMAL; | |
175 | lines = l = alloc_line(); | |
176 | ||
177 | while ((ch = getwchar()) != WEOF) { | |
178 | if (!iswgraph(ch)) { | |
179 | switch (ch) { | |
180 | case BS: /* can't go back further */ | |
181 | if (cur_col == 0) | |
182 | continue; | |
183 | --cur_col; | |
184 | continue; | |
185 | case CR: | |
186 | cur_col = 0; | |
187 | continue; | |
188 | case ESC: /* just ignore EOF */ | |
189 | switch(getwchar()) { | |
190 | /* | |
191 | * In the input stream, accept both the | |
192 | * XPG5 sequences ESC-digit and the | |
193 | * traditional BSD sequences ESC-ctrl. | |
194 | */ | |
195 | case '\007': | |
196 | /* FALLTHROUGH */ | |
197 | case RLF: | |
198 | addto_lineno(&cur_line, -2); | |
199 | break; | |
200 | case '\010': | |
201 | /* FALLTHROUGH */ | |
202 | case RHLF: | |
203 | addto_lineno(&cur_line, -1); | |
204 | break; | |
205 | case '\011': | |
206 | /* FALLTHROUGH */ | |
207 | case FHLF: | |
208 | addto_lineno(&cur_line, 1); | |
209 | if (cur_line > max_line) | |
210 | max_line = cur_line; | |
211 | } | |
212 | continue; | |
213 | case NL: | |
214 | addto_lineno(&cur_line, 2); | |
215 | if (cur_line > max_line) | |
216 | max_line = cur_line; | |
217 | cur_col = 0; | |
218 | continue; | |
219 | case SPACE: | |
220 | ++cur_col; | |
221 | continue; | |
222 | case SI: | |
223 | cur_set = CS_NORMAL; | |
224 | continue; | |
225 | case SO: | |
226 | cur_set = CS_ALTERNATE; | |
227 | continue; | |
228 | case TAB: /* adjust column */ | |
229 | cur_col |= 7; | |
230 | ++cur_col; | |
231 | continue; | |
232 | case VT: | |
233 | addto_lineno(&cur_line, -2); | |
234 | continue; | |
235 | } | |
236 | if (iswspace(ch)) { | |
237 | if ((width = wcwidth(ch)) > 0) | |
238 | cur_col += width; | |
239 | continue; | |
240 | } | |
241 | if (!pass_unknown_seqs) | |
242 | continue; | |
243 | } | |
244 | ||
245 | /* Must stuff ch in a line - are we at the right one? */ | |
246 | if (cur_line + adjust != this_line) { | |
247 | LINE *lnew; | |
248 | ||
249 | /* round up to next line */ | |
250 | adjust = !fine && (cur_line & 1); | |
251 | ||
252 | if (cur_line + adjust < this_line) { | |
253 | while (cur_line + adjust < this_line && | |
254 | l->l_prev != NULL) { | |
255 | l = l->l_prev; | |
256 | this_line--; | |
257 | } | |
258 | if (cur_line + adjust < this_line) { | |
259 | if (nflushd_lines == 0) { | |
260 | /* | |
261 | * Allow backup past first | |
262 | * line if nothing has been | |
263 | * flushed yet. | |
264 | */ | |
265 | while (cur_line + adjust | |
266 | < this_line) { | |
267 | lnew = alloc_line(); | |
268 | l->l_prev = lnew; | |
269 | lnew->l_next = l; | |
270 | l = lines = lnew; | |
271 | extra_lines++; | |
272 | this_line--; | |
273 | } | |
274 | } else { | |
275 | if (!warned++) | |
276 | dowarn(cur_line); | |
277 | cur_line = this_line - adjust; | |
278 | } | |
279 | } | |
280 | } else { | |
281 | /* may need to allocate here */ | |
282 | while (cur_line + adjust > this_line) { | |
283 | if (l->l_next == NULL) { | |
284 | l->l_next = alloc_line(); | |
285 | l->l_next->l_prev = l; | |
286 | } | |
287 | l = l->l_next; | |
288 | this_line++; | |
289 | } | |
290 | } | |
291 | if (this_line > nflushd_lines && | |
292 | this_line - nflushd_lines >= | |
293 | max_bufd_lines + BUFFER_MARGIN) { | |
294 | if (extra_lines) { | |
295 | flush_lines(extra_lines); | |
296 | extra_lines = 0; | |
297 | } | |
298 | flush_lines(this_line - nflushd_lines - | |
299 | max_bufd_lines); | |
300 | nflushd_lines = this_line - max_bufd_lines; | |
301 | } | |
302 | } | |
303 | /* grow line's buffer? */ | |
304 | if (l->l_line_len + 1 >= l->l_lsize) { | |
305 | int need; | |
306 | ||
307 | need = l->l_lsize ? l->l_lsize * 2 : 90; | |
308 | if ((l->l_line = realloc(l->l_line, | |
309 | (unsigned)need * sizeof(CHAR))) == NULL) | |
310 | err(1, NULL); | |
311 | l->l_lsize = need; | |
312 | } | |
313 | c = &l->l_line[l->l_line_len++]; | |
314 | c->c_char = ch; | |
315 | c->c_set = cur_set; | |
316 | c->c_column = cur_col; | |
317 | c->c_width = wcwidth(ch); | |
318 | /* | |
319 | * If things are put in out of order, they will need sorting | |
320 | * when it is flushed. | |
321 | */ | |
322 | if (cur_col < l->l_max_col) | |
323 | l->l_needs_sort = 1; | |
324 | else | |
325 | l->l_max_col = cur_col; | |
326 | if (c->c_width > 0) | |
327 | cur_col += c->c_width; | |
328 | } | |
329 | if (ferror(stdin)) | |
330 | err(1, NULL); | |
331 | if (extra_lines) | |
332 | flush_lines(extra_lines); | |
333 | ||
334 | /* goto the last line that had a character on it */ | |
335 | for (; l->l_next; l = l->l_next) | |
336 | this_line++; | |
337 | flush_lines(this_line - nflushd_lines + 1); | |
338 | ||
339 | /* make sure we leave things in a sane state */ | |
340 | if (last_set != CS_NORMAL) | |
341 | PUTC(SI); | |
342 | ||
343 | /* flush out the last few blank lines */ | |
344 | if (max_line > this_line) | |
345 | nblank_lines = max_line - this_line; | |
346 | if (max_line & 1) | |
347 | nblank_lines++; | |
348 | flush_blanks(); | |
349 | exit(0); | |
350 | } | |
351 | ||
352 | static void | |
353 | flush_lines(int nflush) | |
354 | { | |
355 | LINE *l; | |
356 | ||
357 | while (--nflush >= 0) { | |
358 | l = lines; | |
359 | lines = l->l_next; | |
360 | if (l->l_line) { | |
361 | flush_blanks(); | |
362 | flush_line(l); | |
363 | } | |
364 | if (l->l_line || l->l_next) | |
365 | nblank_lines++; | |
366 | if (l->l_line) | |
367 | (void)free(l->l_line); | |
368 | free_line(l); | |
369 | } | |
370 | if (lines) | |
371 | lines->l_prev = NULL; | |
372 | } | |
373 | ||
374 | /* | |
375 | * Print a number of newline/half newlines. If fine flag is set, nblank_lines | |
376 | * is the number of half line feeds, otherwise it is the number of whole line | |
377 | * feeds. | |
378 | */ | |
379 | static void | |
380 | flush_blanks(void) | |
381 | { | |
382 | int half, i, nb; | |
383 | ||
384 | half = 0; | |
385 | nb = nblank_lines; | |
386 | if (nb & 1) { | |
387 | if (fine) | |
388 | half = 1; | |
389 | else | |
390 | nb++; | |
391 | } | |
392 | nb /= 2; | |
393 | for (i = nb; --i >= 0;) | |
394 | PUTC('\n'); | |
395 | if (half) { | |
396 | PUTC(ESC); | |
397 | PUTC(FHLF); | |
398 | if (!nb) | |
399 | PUTC('\r'); | |
400 | } | |
401 | nblank_lines = 0; | |
402 | } | |
403 | ||
404 | /* | |
405 | * Write a line to stdout taking care of space to tab conversion (-h flag) | |
406 | * and character set shifts. | |
407 | */ | |
408 | static void | |
409 | flush_line(LINE *l) | |
410 | { | |
411 | CHAR *c, *endc; | |
412 | int i, j, nchars, last_col, save, this_col, tot; | |
413 | ||
414 | last_col = 0; | |
415 | nchars = l->l_line_len; | |
416 | ||
417 | if (l->l_needs_sort) { | |
418 | static CHAR *sorted; | |
419 | static int count_size, *count, sorted_size; | |
420 | ||
421 | /* | |
422 | * Do an O(n) sort on l->l_line by column being careful to | |
423 | * preserve the order of characters in the same column. | |
424 | */ | |
425 | if (l->l_lsize > sorted_size) { | |
426 | sorted_size = l->l_lsize; | |
427 | if ((sorted = realloc(sorted, | |
428 | (unsigned)sizeof(CHAR) * sorted_size)) == NULL) | |
429 | err(1, NULL); | |
430 | } | |
431 | if (l->l_max_col >= count_size) { | |
432 | count_size = l->l_max_col + 1; | |
433 | if ((count = realloc(count, | |
434 | (unsigned)sizeof(int) * count_size)) == NULL) | |
435 | err(1, NULL); | |
436 | } | |
437 | memset(count, 0, sizeof(int) * l->l_max_col + 1); | |
438 | for (i = nchars, c = l->l_line; --i >= 0; c++) | |
439 | count[c->c_column]++; | |
440 | ||
441 | /* | |
442 | * calculate running total (shifted down by 1) to use as | |
443 | * indices into new line. | |
444 | */ | |
445 | for (tot = 0, i = 0; i <= l->l_max_col; i++) { | |
446 | save = count[i]; | |
447 | count[i] = tot; | |
448 | tot += save; | |
449 | } | |
450 | ||
451 | for (i = nchars, c = l->l_line; --i >= 0; c++) | |
452 | sorted[count[c->c_column]++] = *c; | |
453 | c = sorted; | |
454 | } else | |
455 | c = l->l_line; | |
456 | while (nchars > 0) { | |
457 | this_col = c->c_column; | |
458 | endc = c; | |
459 | do { | |
460 | ++endc; | |
461 | } while (--nchars > 0 && this_col == endc->c_column); | |
462 | ||
463 | /* if -b only print last character */ | |
464 | if (no_backspaces) { | |
465 | c = endc - 1; | |
466 | if (nchars > 0 && | |
467 | this_col + c->c_width > endc->c_column) | |
468 | continue; | |
469 | } | |
470 | ||
471 | if (this_col > last_col) { | |
472 | int nspace = this_col - last_col; | |
473 | ||
474 | if (compress_spaces && nspace > 1) { | |
475 | while (1) { | |
476 | int tab_col, tab_size; | |
477 | ||
478 | tab_col = (last_col + 8) & ~7; | |
479 | if (tab_col > this_col) | |
480 | break; | |
481 | tab_size = tab_col - last_col; | |
482 | if (tab_size == 1) | |
483 | PUTC(' '); | |
484 | else | |
485 | PUTC('\t'); | |
486 | nspace -= tab_size; | |
487 | last_col = tab_col; | |
488 | } | |
489 | } | |
490 | while (--nspace >= 0) | |
491 | PUTC(' '); | |
492 | last_col = this_col; | |
493 | } | |
494 | ||
495 | for (;;) { | |
496 | if (c->c_set != last_set) { | |
497 | switch (c->c_set) { | |
498 | case CS_NORMAL: | |
499 | PUTC(SI); | |
500 | break; | |
501 | case CS_ALTERNATE: | |
502 | PUTC(SO); | |
503 | } | |
504 | last_set = c->c_set; | |
505 | } | |
506 | PUTC(c->c_char); | |
507 | if ((c + 1) < endc) | |
508 | for (j = 0; j < c->c_width; j++) | |
509 | PUTC('\b'); | |
510 | if (++c >= endc) | |
511 | break; | |
512 | } | |
513 | last_col += (c - 1)->c_width; | |
514 | } | |
515 | } | |
516 | ||
517 | /* | |
518 | * Increment or decrement a line number, checking for overflow. | |
519 | * Stop one below INT_MAX such that the adjust variable is safe. | |
520 | */ | |
521 | void | |
522 | addto_lineno(int *lno, int offset) | |
523 | { | |
524 | if (offset > 0) { | |
525 | if (*lno >= INT_MAX - offset) | |
526 | errx(1, "too many lines"); | |
527 | } else { | |
528 | if (*lno < INT_MIN - offset) | |
529 | errx(1, "too many reverse line feeds"); | |
530 | } | |
531 | *lno += offset; | |
532 | } | |
533 | ||
534 | #define NALLOC 64 | |
535 | ||
536 | static LINE *line_freelist; | |
537 | ||
538 | static LINE * | |
539 | alloc_line(void) | |
540 | { | |
541 | LINE *l; | |
542 | int i; | |
543 | ||
544 | if (!line_freelist) { | |
545 | if ((l = realloc(NULL, sizeof(LINE) * NALLOC)) == NULL) | |
546 | err(1, NULL); | |
547 | line_freelist = l; | |
548 | for (i = 1; i < NALLOC; i++, l++) | |
549 | l->l_next = l + 1; | |
550 | l->l_next = NULL; | |
551 | } | |
552 | l = line_freelist; | |
553 | line_freelist = l->l_next; | |
554 | ||
555 | memset(l, 0, sizeof(LINE)); | |
556 | return (l); | |
557 | } | |
558 | ||
559 | static void | |
560 | free_line(LINE *l) | |
561 | { | |
562 | ||
563 | l->l_next = line_freelist; | |
564 | line_freelist = l; | |
565 | } | |
566 | ||
567 | static void | |
568 | usage(void) | |
569 | { | |
570 | ||
571 | (void)fprintf(stderr, "usage: col [-bfhpx] [-l nline]\n"); | |
572 | exit(1); | |
573 | } | |
574 | ||
575 | static void | |
576 | dowarn(int line) | |
577 | { | |
578 | ||
579 | warnx("warning: can't back up %s", | |
580 | line < 0 ? "past first line" : "-- line already flushed"); | |
581 | } |