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 * 4. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33 #ifndef lint 34 static const char copyright[] = 35 "@(#) Copyright (c) 1990, 1993, 1994\n\ 36 The Regents of the University of California. All rights reserved.\n"; 37 #endif 38 39 #if 0 40 #ifndef lint 41 static char sccsid[] = "@(#)col.c 8.5 (Berkeley) 5/4/95"; 42 #endif 43 #endif 44 45 #include <sys/cdefs.h> 46 __FBSDID("$FreeBSD$"); 47 48 #include <sys/capsicum.h> 49 50 #include <err.h> 51 #include <errno.h> 52 #include <locale.h> 53 #include <stdio.h> 54 #include <stdlib.h> 55 #include <string.h> 56 #include <termios.h> 57 #include <unistd.h> 58 #include <wchar.h> 59 #include <wctype.h> 60 61 #define BS '\b' /* backspace */ 62 #define TAB '\t' /* tab */ 63 #define SPACE ' ' /* space */ 64 #define NL '\n' /* newline */ 65 #define CR '\r' /* carriage return */ 66 #define ESC '\033' /* escape */ 67 #define SI '\017' /* shift in to normal character set */ 68 #define SO '\016' /* shift out to alternate character set */ 69 #define VT '\013' /* vertical tab (aka reverse line feed) */ 70 #define RLF '7' /* ESC-7 reverse line feed */ 71 #define RHLF '8' /* ESC-8 reverse half-line feed */ 72 #define FHLF '9' /* ESC-9 forward half-line feed */ 73 74 /* build up at least this many lines before flushing them out */ 75 #define BUFFER_MARGIN 32 76 77 typedef char CSET; 78 79 typedef struct char_str { 80 #define CS_NORMAL 1 81 #define CS_ALTERNATE 2 82 short c_column; /* column character is in */ 83 CSET c_set; /* character set (currently only 2) */ 84 wchar_t c_char; /* character in question */ 85 int c_width; /* character width */ 86 } CHAR; 87 88 typedef struct line_str LINE; 89 struct line_str { 90 CHAR *l_line; /* characters on the line */ 91 LINE *l_prev; /* previous line */ 92 LINE *l_next; /* next line */ 93 int l_lsize; /* allocated sizeof l_line */ 94 int l_line_len; /* strlen(l_line) */ 95 int l_needs_sort; /* set if chars went in out of order */ 96 int l_max_col; /* max column in the line */ 97 }; 98 99 static LINE *alloc_line(void); 100 static void dowarn(int); 101 static void flush_line(LINE *); 102 static void flush_lines(int); 103 static void flush_blanks(void); 104 static void free_line(LINE *); 105 static void usage(void); 106 107 static CSET last_set; /* char_set of last char printed */ 108 static LINE *lines; 109 static int compress_spaces; /* if doing space -> tab conversion */ 110 static int fine; /* if `fine' resolution (half lines) */ 111 static int max_bufd_lines; /* max # lines to keep in memory */ 112 static int nblank_lines; /* # blanks after last flushed line */ 113 static int no_backspaces; /* if not to output any backspaces */ 114 static int pass_unknown_seqs; /* pass unknown control sequences */ 115 116 #define PUTC(ch) \ 117 do { \ 118 if (putwchar(ch) == WEOF) \ 119 errx(1, "write error"); \ 120 } while (0) 121 122 int 123 main(int argc, char **argv) 124 { 125 wint_t ch; 126 CHAR *c; 127 CSET cur_set; /* current character set */ 128 LINE *l; /* current line */ 129 int extra_lines; /* # of lines above first line */ 130 int cur_col; /* current column */ 131 int cur_line; /* line number of current position */ 132 int max_line; /* max value of cur_line */ 133 int this_line; /* line l points to */ 134 int nflushd_lines; /* number of lines that were flushed */ 135 int adjust, opt, warned, width; 136 cap_rights_t rights; 137 unsigned long cmd; 138 139 (void)setlocale(LC_CTYPE, ""); 140 141 cap_rights_init(&rights, CAP_FSTAT, CAP_READ); 142 if (cap_rights_limit(STDIN_FILENO, &rights) < 0 && errno != ENOSYS) 143 err(1, "unable to limit rights for stdin"); 144 cap_rights_init(&rights, CAP_FSTAT, CAP_WRITE, CAP_IOCTL); 145 if (cap_rights_limit(STDOUT_FILENO, &rights) < 0 && errno != ENOSYS) 146 err(1, "unable to limit rights for stdout"); 147 cmd = TIOCGETA; /* required by isatty(3) in printf(3) */ 148 if (cap_ioctls_limit(STDOUT_FILENO, &cmd, 1) < 0 && errno != ENOSYS) 149 err(1, "unable to limit ioctls for stdout"); 150 151 if (cap_enter() < 0 && errno != ENOSYS) 152 err(1, "unable to enter capability mode"); 153 154 max_bufd_lines = 128; 155 compress_spaces = 1; /* compress spaces into tabs */ 156 while ((opt = getopt(argc, argv, "bfhl:px")) != -1) 157 switch (opt) { 158 case 'b': /* do not output backspaces */ 159 no_backspaces = 1; 160 break; 161 case 'f': /* allow half forward line feeds */ 162 fine = 1; 163 break; 164 case 'h': /* compress spaces into tabs */ 165 compress_spaces = 1; 166 break; 167 case 'l': /* buffered line count */ 168 if ((max_bufd_lines = atoi(optarg)) <= 0) 169 errx(1, "bad -l argument %s", optarg); 170 break; 171 case 'p': /* pass unknown control sequences */ 172 pass_unknown_seqs = 1; 173 break; 174 case 'x': /* do not compress spaces into tabs */ 175 compress_spaces = 0; 176 break; 177 case '?': 178 default: 179 usage(); 180 } 181 182 if (optind != argc) 183 usage(); 184 185 /* this value is in half lines */ 186 max_bufd_lines *= 2; 187 188 adjust = cur_col = extra_lines = warned = 0; 189 cur_line = max_line = nflushd_lines = this_line = 0; 190 cur_set = last_set = CS_NORMAL; 191 lines = l = alloc_line(); 192 193 while ((ch = getwchar()) != WEOF) { 194 if (!iswgraph(ch)) { 195 switch (ch) { 196 case BS: /* can't go back further */ 197 if (cur_col == 0) 198 continue; 199 --cur_col; 200 continue; 201 case CR: 202 cur_col = 0; 203 continue; 204 case ESC: /* just ignore EOF */ 205 switch(getwchar()) { 206 case RLF: 207 cur_line -= 2; 208 break; 209 case RHLF: 210 cur_line--; 211 break; 212 case FHLF: 213 cur_line++; 214 if (cur_line > max_line) 215 max_line = cur_line; 216 } 217 continue; 218 case NL: 219 cur_line += 2; 220 if (cur_line > max_line) 221 max_line = cur_line; 222 cur_col = 0; 223 continue; 224 case SPACE: 225 ++cur_col; 226 continue; 227 case SI: 228 cur_set = CS_NORMAL; 229 continue; 230 case SO: 231 cur_set = CS_ALTERNATE; 232 continue; 233 case TAB: /* adjust column */ 234 cur_col |= 7; 235 ++cur_col; 236 continue; 237 case VT: 238 cur_line -= 2; 239 continue; 240 } 241 if (iswspace(ch)) { 242 if ((width = wcwidth(ch)) > 0) 243 cur_col += width; 244 continue; 245 } 246 if (!pass_unknown_seqs) 247 continue; 248 } 249 250 /* Must stuff ch in a line - are we at the right one? */ 251 if (cur_line != this_line - adjust) { 252 LINE *lnew; 253 int nmove; 254 255 adjust = 0; 256 nmove = cur_line - this_line; 257 if (!fine) { 258 /* round up to next line */ 259 if (cur_line & 1) { 260 adjust = 1; 261 nmove++; 262 } 263 } 264 if (nmove < 0) { 265 for (; nmove < 0 && l->l_prev; nmove++) 266 l = l->l_prev; 267 if (nmove) { 268 if (nflushd_lines == 0) { 269 /* 270 * Allow backup past first 271 * line if nothing has been 272 * flushed yet. 273 */ 274 for (; nmove < 0; nmove++) { 275 lnew = alloc_line(); 276 l->l_prev = lnew; 277 lnew->l_next = l; 278 l = lines = lnew; 279 extra_lines++; 280 } 281 } else { 282 if (!warned++) 283 dowarn(cur_line); 284 cur_line -= nmove; 285 } 286 } 287 } else { 288 /* may need to allocate here */ 289 for (; nmove > 0 && l->l_next; nmove--) 290 l = l->l_next; 291 for (; nmove > 0; nmove--) { 292 lnew = alloc_line(); 293 lnew->l_prev = l; 294 l->l_next = lnew; 295 l = lnew; 296 } 297 } 298 this_line = cur_line + adjust; 299 nmove = this_line - nflushd_lines; 300 if (nmove >= max_bufd_lines + BUFFER_MARGIN) { 301 nflushd_lines += nmove - max_bufd_lines; 302 flush_lines(nmove - max_bufd_lines); 303 } 304 } 305 /* grow line's buffer? */ 306 if (l->l_line_len + 1 >= l->l_lsize) { 307 int need; 308 309 need = l->l_lsize ? l->l_lsize * 2 : 90; 310 if ((l->l_line = realloc(l->l_line, 311 (unsigned)need * sizeof(CHAR))) == NULL) 312 err(1, NULL); 313 l->l_lsize = need; 314 } 315 c = &l->l_line[l->l_line_len++]; 316 c->c_char = ch; 317 c->c_set = cur_set; 318 c->c_column = cur_col; 319 c->c_width = wcwidth(ch); 320 /* 321 * If things are put in out of order, they will need sorting 322 * when it is flushed. 323 */ 324 if (cur_col < l->l_max_col) 325 l->l_needs_sort = 1; 326 else 327 l->l_max_col = cur_col; 328 if (c->c_width > 0) 329 cur_col += c->c_width; 330 } 331 if (ferror(stdin)) 332 err(1, NULL); 333 if (max_line == 0) 334 exit(0); /* no lines, so just exit */ 335 336 /* goto the last line that had a character on it */ 337 for (; l->l_next; l = l->l_next) 338 this_line++; 339 flush_lines(this_line - nflushd_lines + extra_lines + 1); 340 341 /* make sure we leave things in a sane state */ 342 if (last_set != CS_NORMAL) 343 PUTC(SI); 344 345 /* flush out the last few blank lines */ 346 nblank_lines = max_line - this_line; 347 if (max_line & 1) 348 nblank_lines++; 349 else if (!nblank_lines) 350 /* missing a \n on the last line? */ 351 nblank_lines = 2; 352 flush_blanks(); 353 exit(0); 354 } 355 356 static void 357 flush_lines(int nflush) 358 { 359 LINE *l; 360 361 while (--nflush >= 0) { 362 l = lines; 363 lines = l->l_next; 364 if (l->l_line) { 365 flush_blanks(); 366 flush_line(l); 367 } 368 nblank_lines++; 369 if (l->l_line) 370 (void)free(l->l_line); 371 free_line(l); 372 } 373 if (lines) 374 lines->l_prev = NULL; 375 } 376 377 /* 378 * Print a number of newline/half newlines. If fine flag is set, nblank_lines 379 * is the number of half line feeds, otherwise it is the number of whole line 380 * feeds. 381 */ 382 static void 383 flush_blanks(void) 384 { 385 int half, i, nb; 386 387 half = 0; 388 nb = nblank_lines; 389 if (nb & 1) { 390 if (fine) 391 half = 1; 392 else 393 nb++; 394 } 395 nb /= 2; 396 for (i = nb; --i >= 0;) 397 PUTC('\n'); 398 if (half) { 399 PUTC(ESC); 400 PUTC(FHLF); 401 if (!nb) 402 PUTC('\r'); 403 } 404 nblank_lines = 0; 405 } 406 407 /* 408 * Write a line to stdout taking care of space to tab conversion (-h flag) 409 * and character set shifts. 410 */ 411 static void 412 flush_line(LINE *l) 413 { 414 CHAR *c, *endc; 415 int i, j, nchars, last_col, save, this_col, tot; 416 417 last_col = 0; 418 nchars = l->l_line_len; 419 420 if (l->l_needs_sort) { 421 static CHAR *sorted; 422 static int count_size, *count, sorted_size; 423 424 /* 425 * Do an O(n) sort on l->l_line by column being careful to 426 * preserve the order of characters in the same column. 427 */ 428 if (l->l_lsize > sorted_size) { 429 sorted_size = l->l_lsize; 430 if ((sorted = realloc(sorted, 431 (unsigned)sizeof(CHAR) * sorted_size)) == NULL) 432 err(1, NULL); 433 } 434 if (l->l_max_col >= count_size) { 435 count_size = l->l_max_col + 1; 436 if ((count = realloc(count, 437 (unsigned)sizeof(int) * count_size)) == NULL) 438 err(1, NULL); 439 } 440 memset(count, 0, sizeof(int) * l->l_max_col + 1); 441 for (i = nchars, c = l->l_line; --i >= 0; c++) 442 count[c->c_column]++; 443 444 /* 445 * calculate running total (shifted down by 1) to use as 446 * indices into new line. 447 */ 448 for (tot = 0, i = 0; i <= l->l_max_col; i++) { 449 save = count[i]; 450 count[i] = tot; 451 tot += save; 452 } 453 454 for (i = nchars, c = l->l_line; --i >= 0; c++) 455 sorted[count[c->c_column]++] = *c; 456 c = sorted; 457 } else 458 c = l->l_line; 459 while (nchars > 0) { 460 this_col = c->c_column; 461 endc = c; 462 do { 463 ++endc; 464 } while (--nchars > 0 && this_col == endc->c_column); 465 466 /* if -b only print last character */ 467 if (no_backspaces) { 468 c = endc - 1; 469 if (nchars > 0 && 470 this_col + c->c_width > endc->c_column) 471 continue; 472 } 473 474 if (this_col > last_col) { 475 int nspace = this_col - last_col; 476 477 if (compress_spaces && nspace > 1) { 478 while (1) { 479 int tab_col, tab_size; 480 481 tab_col = (last_col + 8) & ~7; 482 if (tab_col > this_col) 483 break; 484 tab_size = tab_col - last_col; 485 if (tab_size == 1) 486 PUTC(' '); 487 else 488 PUTC('\t'); 489 nspace -= tab_size; 490 last_col = tab_col; 491 } 492 } 493 while (--nspace >= 0) 494 PUTC(' '); 495 last_col = this_col; 496 } 497 498 for (;;) { 499 if (c->c_set != last_set) { 500 switch (c->c_set) { 501 case CS_NORMAL: 502 PUTC(SI); 503 break; 504 case CS_ALTERNATE: 505 PUTC(SO); 506 } 507 last_set = c->c_set; 508 } 509 PUTC(c->c_char); 510 if ((c + 1) < endc) 511 for (j = 0; j < c->c_width; j++) 512 PUTC('\b'); 513 if (++c >= endc) 514 break; 515 } 516 last_col += (c - 1)->c_width; 517 } 518 } 519 520 #define NALLOC 64 521 522 static LINE *line_freelist; 523 524 static LINE * 525 alloc_line(void) 526 { 527 LINE *l; 528 int i; 529 530 if (!line_freelist) { 531 if ((l = realloc(NULL, sizeof(LINE) * NALLOC)) == NULL) 532 err(1, NULL); 533 line_freelist = l; 534 for (i = 1; i < NALLOC; i++, l++) 535 l->l_next = l + 1; 536 l->l_next = NULL; 537 } 538 l = line_freelist; 539 line_freelist = l->l_next; 540 541 memset(l, 0, sizeof(LINE)); 542 return (l); 543 } 544 545 static void 546 free_line(LINE *l) 547 { 548 549 l->l_next = line_freelist; 550 line_freelist = l; 551 } 552 553 static void 554 usage(void) 555 { 556 557 (void)fprintf(stderr, "usage: col [-bfhpx] [-l nline]\n"); 558 exit(1); 559 } 560 561 static void 562 dowarn(int line) 563 { 564 565 warnx("warning: can't back up %s", 566 line < 0 ? "past first line" : "-- line already flushed"); 567 } 568