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