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