142270Sbostic /*- 2*66652Spendry * Copyright (c) 1990, 1993, 1994 361942Sbostic * The Regents of the University of California. All rights reserved. 442270Sbostic * 542270Sbostic * This code is derived from software contributed to Berkeley by 642270Sbostic * Michael Rendell of the Memorial University of Newfoundland. 742270Sbostic * 842270Sbostic * %sccs.include.redist.c% 942270Sbostic */ 10982Sbill 1142270Sbostic #ifndef lint 1261942Sbostic static char copyright[] = 13*66652Spendry "@(#) Copyright (c) 1990, 1993, 1994\n\ 1461942Sbostic The Regents of the University of California. All rights reserved.\n"; 1542270Sbostic #endif /* not lint */ 16982Sbill 1742270Sbostic #ifndef lint 18*66652Spendry static char sccsid[] = "@(#)col.c 8.3 (Berkeley) 04/02/94"; 1942270Sbostic #endif /* not lint */ 20982Sbill 2142270Sbostic #include <ctype.h> 2266590Spendry #include <err.h> 2342270Sbostic #include <string.h> 2442270Sbostic #include <stdio.h> 2566590Spendry #include <stdlib.h> 26982Sbill 2742270Sbostic #define BS '\b' /* backspace */ 2842270Sbostic #define TAB '\t' /* tab */ 2942270Sbostic #define SPACE ' ' /* space */ 3042270Sbostic #define NL '\n' /* newline */ 3142270Sbostic #define CR '\r' /* carriage return */ 3242305Sbostic #define ESC '\033' /* escape */ 3342305Sbostic #define SI '\017' /* shift in to normal character set */ 3442305Sbostic #define SO '\016' /* shift out to alternate character set */ 3542305Sbostic #define VT '\013' /* vertical tab (aka reverse line feed) */ 3646233Storek #define RLF '\007' /* ESC-07 reverse line feed */ 3746233Storek #define RHLF '\010' /* ESC-010 reverse half-line feed */ 3846233Storek #define FHLF '\011' /* ESC-011 forward half-line feed */ 39982Sbill 4042270Sbostic /* build up at least this many lines before flushing them out */ 4142270Sbostic #define BUFFER_MARGIN 32 42982Sbill 4342270Sbostic typedef char CSET; 44982Sbill 4542270Sbostic typedef struct char_str { 4642270Sbostic #define CS_NORMAL 1 4742270Sbostic #define CS_ALTERNATE 2 4842270Sbostic short c_column; /* column character is in */ 4942270Sbostic CSET c_set; /* character set (currently only 2) */ 5042270Sbostic char c_char; /* character in question */ 5142270Sbostic } CHAR; 52982Sbill 5342270Sbostic typedef struct line_str LINE; 5442270Sbostic struct line_str { 5542270Sbostic CHAR *l_line; /* characters on the line */ 5642270Sbostic LINE *l_prev; /* previous line */ 5742270Sbostic LINE *l_next; /* next line */ 5842270Sbostic int l_lsize; /* allocated sizeof l_line */ 5942270Sbostic int l_line_len; /* strlen(l_line) */ 6042270Sbostic int l_needs_sort; /* set if chars went in out of order */ 6142270Sbostic int l_max_col; /* max column in the line */ 6242270Sbostic }; 63982Sbill 6466590Spendry LINE *alloc_line __P((void)); 6566590Spendry void dowarn __P((int)); 6666590Spendry void flush_line __P((LINE *)); 6766590Spendry void flush_lines __P((int)); 6866590Spendry void flush_blanks __P((void)); 6966590Spendry void free_line __P((LINE *)); 7066590Spendry void usage __P((void)); 7166590Spendry void wrerr __P((void)); 7266590Spendry void *xmalloc __P((void *, size_t)); 73982Sbill 7466590Spendry CSET last_set; /* char_set of last char printed */ 7566590Spendry LINE *lines; 7666590Spendry int compress_spaces; /* if doing space -> tab conversion */ 7766590Spendry int fine; /* if `fine' resolution (half lines) */ 7866590Spendry int max_bufd_lines; /* max # lines to keep in memory */ 7966590Spendry int nblank_lines; /* # blanks after last flushed line */ 8066590Spendry int no_backspaces; /* if not to output any backspaces */ 81982Sbill 8242270Sbostic #define PUTC(ch) \ 8342270Sbostic if (putchar(ch) == EOF) \ 8442270Sbostic wrerr(); 85982Sbill 8666590Spendry int 8742270Sbostic main(argc, argv) 8842270Sbostic int argc; 8942270Sbostic char **argv; 9042270Sbostic { 9166590Spendry int ch; 9242270Sbostic CHAR *c; 9342270Sbostic CSET cur_set; /* current character set */ 9442270Sbostic LINE *l; /* current line */ 9542270Sbostic int extra_lines; /* # of lines above first line */ 9642270Sbostic int cur_col; /* current column */ 9742270Sbostic int cur_line; /* line number of current position */ 9842270Sbostic int max_line; /* max value of cur_line */ 9942270Sbostic int this_line; /* line l points to */ 10042270Sbostic int nflushd_lines; /* number of lines that were flushed */ 10142270Sbostic int adjust, opt, warned; 102982Sbill 10342270Sbostic max_bufd_lines = 128; 10442270Sbostic compress_spaces = 1; /* compress spaces into tabs */ 10542270Sbostic while ((opt = getopt(argc, argv, "bfhl:x")) != EOF) 10642270Sbostic switch (opt) { 10742270Sbostic case 'b': /* do not output backspaces */ 10842270Sbostic no_backspaces = 1; 10942270Sbostic break; 11042270Sbostic case 'f': /* allow half forward line feeds */ 11142270Sbostic fine = 1; 11242270Sbostic break; 11342270Sbostic case 'h': /* compress spaces into tabs */ 11442270Sbostic compress_spaces = 1; 11542270Sbostic break; 11642270Sbostic case 'l': /* buffered line count */ 11742270Sbostic if ((max_bufd_lines = atoi(optarg)) <= 0) { 11842270Sbostic (void)fprintf(stderr, 11942270Sbostic "col: bad -l argument %s.\n", optarg); 12042270Sbostic exit(1); 121982Sbill } 12242270Sbostic break; 12342270Sbostic case 'x': /* do not compress spaces into tabs */ 12442270Sbostic compress_spaces = 0; 12542270Sbostic break; 12642270Sbostic case '?': 12742270Sbostic default: 12842270Sbostic usage(); 12942270Sbostic } 130982Sbill 13142270Sbostic if (optind != argc) 13242270Sbostic usage(); 133982Sbill 13442270Sbostic /* this value is in half lines */ 13542270Sbostic max_bufd_lines *= 2; 136982Sbill 13742270Sbostic adjust = cur_col = extra_lines = warned = 0; 13842270Sbostic cur_line = max_line = nflushd_lines = this_line = 0; 13942270Sbostic cur_set = last_set = CS_NORMAL; 14042270Sbostic lines = l = alloc_line(); 141982Sbill 14242270Sbostic while ((ch = getchar()) != EOF) { 14342270Sbostic if (!isgraph(ch)) { 14442270Sbostic switch (ch) { 14542270Sbostic case BS: /* can't go back further */ 14642270Sbostic if (cur_col == 0) 14742270Sbostic continue; 14842270Sbostic --cur_col; 14942270Sbostic continue; 15042270Sbostic case CR: 15142270Sbostic cur_col = 0; 15242270Sbostic continue; 15342270Sbostic case ESC: /* just ignore EOF */ 15442270Sbostic switch(getchar()) { 15542270Sbostic case RLF: 15642270Sbostic cur_line -= 2; 15742270Sbostic break; 15842270Sbostic case RHLF: 15942270Sbostic cur_line--; 16042270Sbostic break; 16142270Sbostic case FHLF: 16242270Sbostic cur_line++; 16342270Sbostic if (cur_line > max_line) 16442270Sbostic max_line = cur_line; 16542270Sbostic } 16642270Sbostic continue; 16742270Sbostic case NL: 16842270Sbostic cur_line += 2; 16942270Sbostic if (cur_line > max_line) 17042270Sbostic max_line = cur_line; 17142270Sbostic cur_col = 0; 17242270Sbostic continue; 17342270Sbostic case SPACE: 17442270Sbostic ++cur_col; 17542270Sbostic continue; 17642270Sbostic case SI: 17742270Sbostic cur_set = CS_NORMAL; 17842270Sbostic continue; 17942270Sbostic case SO: 18042270Sbostic cur_set = CS_ALTERNATE; 18142270Sbostic continue; 18242270Sbostic case TAB: /* adjust column */ 18342270Sbostic cur_col |= 7; 18442270Sbostic ++cur_col; 18542270Sbostic continue; 18642270Sbostic case VT: 18742270Sbostic cur_line -= 2; 18842270Sbostic continue; 18942270Sbostic } 190982Sbill continue; 19142270Sbostic } 192982Sbill 19342270Sbostic /* Must stuff ch in a line - are we at the right one? */ 19442270Sbostic if (cur_line != this_line - adjust) { 19542270Sbostic LINE *lnew; 19642270Sbostic int nmove; 197982Sbill 19842270Sbostic adjust = 0; 19942270Sbostic nmove = cur_line - this_line; 20042270Sbostic if (!fine) { 20142270Sbostic /* round up to next line */ 20242270Sbostic if (cur_line & 1) { 20342270Sbostic adjust = 1; 20442270Sbostic nmove++; 20542270Sbostic } 206982Sbill } 20742270Sbostic if (nmove < 0) { 20842270Sbostic for (; nmove < 0 && l->l_prev; nmove++) 20942270Sbostic l = l->l_prev; 21042270Sbostic if (nmove) { 21142270Sbostic if (nflushd_lines == 0) { 21242270Sbostic /* 21342270Sbostic * Allow backup past first 21442270Sbostic * line if nothing has been 21542270Sbostic * flushed yet. 21642270Sbostic */ 21742270Sbostic for (; nmove < 0; nmove++) { 21842270Sbostic lnew = alloc_line(); 21942270Sbostic l->l_prev = lnew; 22042270Sbostic lnew->l_next = l; 22142270Sbostic l = lines = lnew; 22242270Sbostic extra_lines++; 22342270Sbostic } 22442270Sbostic } else { 22542270Sbostic if (!warned++) 22666590Spendry dowarn(cur_line); 22742270Sbostic cur_line -= nmove; 22842270Sbostic } 22942270Sbostic } 23042270Sbostic } else { 23142270Sbostic /* may need to allocate here */ 23242270Sbostic for (; nmove > 0 && l->l_next; nmove--) 23342270Sbostic l = l->l_next; 23442270Sbostic for (; nmove > 0; nmove--) { 23542270Sbostic lnew = alloc_line(); 23642270Sbostic lnew->l_prev = l; 23742270Sbostic l->l_next = lnew; 23842270Sbostic l = lnew; 23942270Sbostic } 24042270Sbostic } 24142270Sbostic this_line = cur_line + adjust; 24242270Sbostic nmove = this_line - nflushd_lines; 24342270Sbostic if (nmove >= max_bufd_lines + BUFFER_MARGIN) { 24442270Sbostic nflushd_lines += nmove - max_bufd_lines; 24542270Sbostic flush_lines(nmove - max_bufd_lines); 24642270Sbostic } 247982Sbill } 24842270Sbostic /* grow line's buffer? */ 24942270Sbostic if (l->l_line_len + 1 >= l->l_lsize) { 25042270Sbostic int need; 25142270Sbostic 25242270Sbostic need = l->l_lsize ? l->l_lsize * 2 : 90; 25342270Sbostic l->l_line = (CHAR *)xmalloc((void *) l->l_line, 25442270Sbostic (unsigned) need * sizeof(CHAR)); 25542270Sbostic l->l_lsize = need; 25642270Sbostic } 25742270Sbostic c = &l->l_line[l->l_line_len++]; 25842270Sbostic c->c_char = ch; 25942270Sbostic c->c_set = cur_set; 26042270Sbostic c->c_column = cur_col; 26142270Sbostic /* 26242270Sbostic * If things are put in out of order, they will need sorting 26342270Sbostic * when it is flushed. 26442270Sbostic */ 26542270Sbostic if (cur_col < l->l_max_col) 26642270Sbostic l->l_needs_sort = 1; 26742270Sbostic else 26842270Sbostic l->l_max_col = cur_col; 26942270Sbostic cur_col++; 270982Sbill } 27142270Sbostic /* goto the last line that had a character on it */ 27242270Sbostic for (; l->l_next; l = l->l_next) 27342270Sbostic this_line++; 27442270Sbostic flush_lines(this_line - nflushd_lines + extra_lines + 1); 275982Sbill 27642270Sbostic /* make sure we leave things in a sane state */ 27742270Sbostic if (last_set != CS_NORMAL) 27842270Sbostic PUTC('\017'); 27942270Sbostic 28042270Sbostic /* flush out the last few blank lines */ 28142270Sbostic nblank_lines = max_line - this_line; 28242270Sbostic if (max_line & 1) 28342270Sbostic nblank_lines++; 28442270Sbostic else if (!nblank_lines) 28542270Sbostic /* missing a \n on the last line? */ 28642270Sbostic nblank_lines = 2; 28742270Sbostic flush_blanks(); 288982Sbill exit(0); 289982Sbill } 290982Sbill 29166590Spendry void 29242270Sbostic flush_lines(nflush) 29342270Sbostic int nflush; 294982Sbill { 29542270Sbostic LINE *l; 296982Sbill 29742270Sbostic while (--nflush >= 0) { 29842270Sbostic l = lines; 29942270Sbostic lines = l->l_next; 30042270Sbostic if (l->l_line) { 30142270Sbostic flush_blanks(); 30242270Sbostic flush_line(l); 303982Sbill } 30442270Sbostic nblank_lines++; 30542270Sbostic if (l->l_line) 30642270Sbostic (void)free((void *)l->l_line); 30742270Sbostic free_line(l); 308982Sbill } 30942270Sbostic if (lines) 31042270Sbostic lines->l_prev = NULL; 311982Sbill } 312982Sbill 31342270Sbostic /* 31442270Sbostic * Print a number of newline/half newlines. If fine flag is set, nblank_lines 31542270Sbostic * is the number of half line feeds, otherwise it is the number of whole line 31642270Sbostic * feeds. 31742270Sbostic */ 31866590Spendry void 31942270Sbostic flush_blanks() 320982Sbill { 32142270Sbostic int half, i, nb; 322982Sbill 32342270Sbostic half = 0; 32442270Sbostic nb = nblank_lines; 32542270Sbostic if (nb & 1) { 32642270Sbostic if (fine) 32742270Sbostic half = 1; 32842270Sbostic else 32942270Sbostic nb++; 330982Sbill } 33142270Sbostic nb /= 2; 33242270Sbostic for (i = nb; --i >= 0;) 33342270Sbostic PUTC('\n'); 33442270Sbostic if (half) { 33542270Sbostic PUTC('\033'); 33642270Sbostic PUTC('9'); 33742270Sbostic if (!nb) 33842270Sbostic PUTC('\r'); 33942270Sbostic } 34042270Sbostic nblank_lines = 0; 341982Sbill } 342982Sbill 34342270Sbostic /* 34442270Sbostic * Write a line to stdout taking care of space to tab conversion (-h flag) 34542270Sbostic * and character set shifts. 34642270Sbostic */ 34766590Spendry void 34842270Sbostic flush_line(l) 34942270Sbostic LINE *l; 350982Sbill { 35142270Sbostic CHAR *c, *endc; 35242270Sbostic int nchars, last_col, this_col; 353982Sbill 35442270Sbostic last_col = 0; 35542270Sbostic nchars = l->l_line_len; 356982Sbill 35742270Sbostic if (l->l_needs_sort) { 35842270Sbostic static CHAR *sorted; 35942270Sbostic static int count_size, *count, i, save, sorted_size, tot; 36042270Sbostic 36142270Sbostic /* 36242270Sbostic * Do an O(n) sort on l->l_line by column being careful to 36342270Sbostic * preserve the order of characters in the same column. 36442270Sbostic */ 36542270Sbostic if (l->l_lsize > sorted_size) { 36642270Sbostic sorted_size = l->l_lsize; 36742270Sbostic sorted = (CHAR *)xmalloc((void *)sorted, 36842270Sbostic (unsigned)sizeof(CHAR) * sorted_size); 369982Sbill } 37042270Sbostic if (l->l_max_col >= count_size) { 37142270Sbostic count_size = l->l_max_col + 1; 37242270Sbostic count = (int *)xmalloc((void *)count, 37342270Sbostic (unsigned)sizeof(int) * count_size); 374982Sbill } 37566590Spendry memset((char *)count, 0, sizeof(int) * l->l_max_col + 1); 37642270Sbostic for (i = nchars, c = l->l_line; --i >= 0; c++) 37742270Sbostic count[c->c_column]++; 37842270Sbostic 37942270Sbostic /* 38042270Sbostic * calculate running total (shifted down by 1) to use as 38142270Sbostic * indices into new line. 38242270Sbostic */ 38342270Sbostic for (tot = 0, i = 0; i <= l->l_max_col; i++) { 38442270Sbostic save = count[i]; 38542270Sbostic count[i] = tot; 38642270Sbostic tot += save; 38742270Sbostic } 38842270Sbostic 38942270Sbostic for (i = nchars, c = l->l_line; --i >= 0; c++) 39042270Sbostic sorted[count[c->c_column]++] = *c; 39142270Sbostic c = sorted; 39242270Sbostic } else 39342270Sbostic c = l->l_line; 39442270Sbostic while (nchars > 0) { 39542270Sbostic this_col = c->c_column; 39642270Sbostic endc = c; 39742270Sbostic do { 39842270Sbostic ++endc; 39942270Sbostic } while (--nchars > 0 && this_col == endc->c_column); 40042270Sbostic 40142270Sbostic /* if -b only print last character */ 40242270Sbostic if (no_backspaces) 40342270Sbostic c = endc - 1; 40442270Sbostic 40542270Sbostic if (this_col > last_col) { 40642270Sbostic int nspace = this_col - last_col; 40742270Sbostic 40842270Sbostic if (compress_spaces && nspace > 1) { 40942270Sbostic int ntabs; 41042270Sbostic 41142270Sbostic ntabs = this_col / 8 - last_col / 8; 41242270Sbostic nspace -= ntabs * 8; 41342270Sbostic while (--ntabs >= 0) 41442270Sbostic PUTC('\t'); 41542270Sbostic } 41642270Sbostic while (--nspace >= 0) 41742270Sbostic PUTC(' '); 41842270Sbostic last_col = this_col; 41942270Sbostic } 42042270Sbostic last_col++; 42142270Sbostic 42242270Sbostic for (;;) { 42342270Sbostic if (c->c_set != last_set) { 42442270Sbostic switch (c->c_set) { 42542270Sbostic case CS_NORMAL: 42642270Sbostic PUTC('\017'); 42742270Sbostic break; 42842270Sbostic case CS_ALTERNATE: 42942270Sbostic PUTC('\016'); 430982Sbill } 43142270Sbostic last_set = c->c_set; 432982Sbill } 43342270Sbostic PUTC(c->c_char); 43442270Sbostic if (++c >= endc) 435982Sbill break; 43642270Sbostic PUTC('\b'); 437982Sbill } 438982Sbill } 439982Sbill } 440982Sbill 44142270Sbostic #define NALLOC 64 44242270Sbostic 44342270Sbostic static LINE *line_freelist; 44442270Sbostic 44542270Sbostic LINE * 44642270Sbostic alloc_line() 447982Sbill { 44842270Sbostic LINE *l; 44942270Sbostic int i; 45042270Sbostic 45142270Sbostic if (!line_freelist) { 45242270Sbostic l = (LINE *)xmalloc((void *)NULL, sizeof(LINE) * NALLOC); 45342270Sbostic line_freelist = l; 45442270Sbostic for (i = 1; i < NALLOC; i++, l++) 45542270Sbostic l->l_next = l + 1; 45642270Sbostic l->l_next = NULL; 457982Sbill } 45842270Sbostic l = line_freelist; 45942270Sbostic line_freelist = l->l_next; 46042270Sbostic 46166590Spendry memset(l, 0, sizeof(LINE)); 46266590Spendry return (l); 463982Sbill } 464982Sbill 46566590Spendry void 46642270Sbostic free_line(l) 46742270Sbostic LINE *l; 468982Sbill { 46966590Spendry 47042270Sbostic l->l_next = line_freelist; 47142270Sbostic line_freelist = l; 47242270Sbostic } 47342270Sbostic 47442270Sbostic void * 47542270Sbostic xmalloc(p, size) 47642270Sbostic void *p; 47742270Sbostic size_t size; 47842270Sbostic { 47966590Spendry 48066590Spendry if (!(p = (void *)realloc(p, size))) 48166590Spendry err(1, NULL); 48266590Spendry return (p); 483982Sbill } 48442270Sbostic 48566590Spendry void 48642270Sbostic usage() 48742270Sbostic { 48866590Spendry 48942270Sbostic (void)fprintf(stderr, "usage: col [-bfx] [-l nline]\n"); 49042270Sbostic exit(1); 49142270Sbostic } 49242270Sbostic 49366590Spendry void 49442270Sbostic wrerr() 49542270Sbostic { 49666590Spendry 49742270Sbostic (void)fprintf(stderr, "col: write error.\n"); 49842270Sbostic exit(1); 49942270Sbostic } 50042270Sbostic 50166590Spendry void 50266590Spendry dowarn(line) 50342270Sbostic int line; 50442270Sbostic { 50566590Spendry 50666590Spendry warnx("warning: can't back up %s", 50766590Spendry line < 0 ? "past first line" : "-- line already flushed"); 50842270Sbostic } 509