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