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