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