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