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