Home | History | Annotate | Line # | Download | only in column
column.c revision 1.10
      1 /*	$NetBSD: column.c,v 1.10 2003/08/07 11:13:26 agc Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1989, 1993, 1994
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. Neither the name of the University nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 #ifndef lint
     34 __COPYRIGHT("@(#) Copyright (c) 1989, 1993, 1994\n\
     35 	The Regents of the University of California.  All rights reserved.\n");
     36 #endif /* not lint */
     37 
     38 #ifndef lint
     39 #if 0
     40 static char sccsid[] = "@(#)column.c	8.4 (Berkeley) 5/4/95";
     41 #endif
     42 __RCSID("$NetBSD: column.c,v 1.10 2003/08/07 11:13:26 agc Exp $");
     43 #endif /* not lint */
     44 
     45 #include <sys/types.h>
     46 #include <sys/ioctl.h>
     47 
     48 #include <ctype.h>
     49 #include <err.h>
     50 #include <termios.h>
     51 #include <limits.h>
     52 #include <stdio.h>
     53 #include <stdlib.h>
     54 #include <string.h>
     55 #include <unistd.h>
     56 
     57 void  c_columnate __P((void));
     58 void *emalloc __P((int));
     59 void  input __P((FILE *));
     60 void  maketbl __P((void));
     61 int   main __P((int, char **));
     62 void  print __P((void));
     63 void  r_columnate __P((void));
     64 void  usage __P((void));
     65 
     66 int termwidth = 80;		/* default terminal width */
     67 
     68 int entries;			/* number of records */
     69 int eval;			/* exit value */
     70 int maxlength;			/* longest record */
     71 char **list;			/* array of pointers to records */
     72 char *separator = "\t ";	/* field separator for table option */
     73 
     74 int
     75 main(argc, argv)
     76 	int argc;
     77 	char **argv;
     78 {
     79 	struct winsize win;
     80 	FILE *fp;
     81 	int ch, tflag, xflag;
     82 	const char *p;
     83 
     84 	if (ioctl(1, TIOCGWINSZ, &win) == -1 || !win.ws_col) {
     85 		if ((p = getenv("COLUMNS")) != NULL)
     86 			termwidth = atoi(p);
     87 	} else
     88 		termwidth = win.ws_col;
     89 
     90 	tflag = xflag = 0;
     91 	while ((ch = getopt(argc, argv, "c:s:tx")) != -1)
     92 		switch(ch) {
     93 		case 'c':
     94 			termwidth = atoi(optarg);
     95 			break;
     96 		case 's':
     97 			separator = optarg;
     98 			break;
     99 		case 't':
    100 			tflag = 1;
    101 			break;
    102 		case 'x':
    103 			xflag = 1;
    104 			break;
    105 		case '?':
    106 		default:
    107 			usage();
    108 		}
    109 	argc -= optind;
    110 	argv += optind;
    111 
    112 	if (!*argv)
    113 		input(stdin);
    114 	else for (; *argv; ++argv)
    115 		if ((fp = fopen(*argv, "r")) != NULL) {
    116 			input(fp);
    117 			(void)fclose(fp);
    118 		} else {
    119 			warn("%s", *argv);
    120 			eval = 1;
    121 		}
    122 
    123 	if (!entries)
    124 		exit(eval);
    125 
    126 	if (tflag)
    127 		maketbl();
    128 	else if (maxlength >= termwidth)
    129 		print();
    130 	else if (xflag)
    131 		c_columnate();
    132 	else
    133 		r_columnate();
    134 	exit(eval);
    135 }
    136 
    137 #define	TAB	8
    138 void
    139 c_columnate()
    140 {
    141 	int chcnt, col, cnt, endcol, numcols;
    142 	char **lp;
    143 
    144 	maxlength = (maxlength + TAB) & ~(TAB - 1);
    145 	numcols = termwidth / maxlength;
    146 	endcol = maxlength;
    147 	for (chcnt = col = 0, lp = list;; ++lp) {
    148 		chcnt += printf("%s", *lp);
    149 		if (!--entries)
    150 			break;
    151 		if (++col == numcols) {
    152 			chcnt = col = 0;
    153 			endcol = maxlength;
    154 			putchar('\n');
    155 		} else {
    156 			while ((cnt = ((chcnt + TAB) & ~(TAB - 1))) <= endcol) {
    157 				(void)putchar('\t');
    158 				chcnt = cnt;
    159 			}
    160 			endcol += maxlength;
    161 		}
    162 	}
    163 	if (chcnt)
    164 		putchar('\n');
    165 }
    166 
    167 void
    168 r_columnate()
    169 {
    170 	int base, chcnt, cnt, col, endcol, numcols, numrows, row;
    171 
    172 	maxlength = (maxlength + TAB) & ~(TAB - 1);
    173 	numcols = termwidth / maxlength;
    174 	numrows = entries / numcols;
    175 	if (entries % numcols)
    176 		++numrows;
    177 
    178 	for (row = 0; row < numrows; ++row) {
    179 		endcol = maxlength;
    180 		for (base = row, chcnt = col = 0; col < numcols; ++col) {
    181 			chcnt += printf("%s", list[base]);
    182 			if ((base += numrows) >= entries)
    183 				break;
    184 			while ((cnt = ((chcnt + TAB) & ~(TAB - 1))) <= endcol) {
    185 				(void)putchar('\t');
    186 				chcnt = cnt;
    187 			}
    188 			endcol += maxlength;
    189 		}
    190 		putchar('\n');
    191 	}
    192 }
    193 
    194 void
    195 print()
    196 {
    197 	int cnt;
    198 	char **lp;
    199 
    200 	for (cnt = entries, lp = list; cnt--; ++lp)
    201 		(void)printf("%s\n", *lp);
    202 }
    203 
    204 typedef struct _tbl {
    205 	char **list;
    206 	int cols, *len;
    207 } TBL;
    208 #define	DEFCOLS	25
    209 
    210 void
    211 maketbl()
    212 {
    213 	TBL *t;
    214 	int coloff, cnt;
    215 	char *p, **lp;
    216 	int *lens, maxcols;
    217 	TBL *tbl;
    218 	char **cols;
    219 
    220 	t = tbl = emalloc(entries * sizeof(TBL));
    221 	cols = emalloc((maxcols = DEFCOLS) * sizeof(char *));
    222 	lens = emalloc(maxcols * sizeof(int));
    223 	for (cnt = 0, lp = list; cnt < entries; ++cnt, ++lp, ++t) {
    224 		for (coloff = 0, p = *lp;
    225 		    (cols[coloff] = strtok(p, separator)) != NULL;
    226 		    p = NULL)
    227 			if (++coloff == maxcols) {
    228 				if (!(cols = realloc(cols, (u_int)maxcols +
    229 				    DEFCOLS * sizeof(char *))) ||
    230 				    !(lens = realloc(lens,
    231 				    (u_int)maxcols + DEFCOLS * sizeof(int))))
    232 					err(1, "realloc");
    233 				memset((char *)lens + maxcols * sizeof(int),
    234 				    0, DEFCOLS * sizeof(int));
    235 				maxcols += DEFCOLS;
    236 			}
    237 		t->list = emalloc(coloff * sizeof(char *));
    238 		t->len = emalloc(coloff * sizeof(int));
    239 		for (t->cols = coloff; --coloff >= 0;) {
    240 			t->list[coloff] = cols[coloff];
    241 			t->len[coloff] = strlen(cols[coloff]);
    242 			if (t->len[coloff] > lens[coloff])
    243 				lens[coloff] = t->len[coloff];
    244 		}
    245 	}
    246 	for (cnt = 0, t = tbl; cnt < entries; ++cnt, ++t) {
    247 		for (coloff = 0; coloff < t->cols  - 1; ++coloff)
    248 			(void)printf("%s%*s", t->list[coloff],
    249 			    lens[coloff] - t->len[coloff] + 2, " ");
    250 		(void)printf("%s\n", t->list[coloff]);
    251 	}
    252 }
    253 
    254 #define	DEFNUM		1000
    255 #define	MAXLINELEN	(LINE_MAX + 1)
    256 
    257 void
    258 input(fp)
    259 	FILE *fp;
    260 {
    261 	static int maxentry;
    262 	int len;
    263 	char *p, buf[MAXLINELEN];
    264 
    265 	if (!list)
    266 		list = emalloc((maxentry = DEFNUM) * sizeof(char *));
    267 	while (fgets(buf, MAXLINELEN, fp)) {
    268 		for (p = buf; *p && isspace((unsigned char)*p); ++p);
    269 		if (!*p)
    270 			continue;
    271 		if (!(p = strchr(p, '\n'))) {
    272 			warnx("line too long");
    273 			eval = 1;
    274 			continue;
    275 		}
    276 		*p = '\0';
    277 		len = p - buf;
    278 		if (maxlength < len)
    279 			maxlength = len;
    280 		if (entries == maxentry) {
    281 			maxentry += DEFNUM;
    282 			if (!(list = realloc(list,
    283 			    (u_int)maxentry * sizeof(char *))))
    284 				err(1, "realloc");
    285 		}
    286 		list[entries++] = strdup(buf);
    287 	}
    288 }
    289 
    290 void *
    291 emalloc(size)
    292 	int size;
    293 {
    294 	char *p;
    295 
    296 	if (!(p = malloc(size)))
    297 		err(1, "malloc");
    298 	memset(p, 0, size);
    299 	return (p);
    300 }
    301 
    302 void
    303 usage()
    304 {
    305 
    306 	(void)fprintf(stderr,
    307 	    "usage: column [-tx] [-c columns] [-s sep] [file ...]\n");
    308 	exit(1);
    309 }
    310