Home | History | Annotate | Line # | Download | only in column
column.c revision 1.8
      1  1.8  mycroft /*	$NetBSD: column.c,v 1.8 1998/07/28 19:26:09 mycroft 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.6    lukem #include <sys/cdefs.h>
     37  1.1      cgd #ifndef lint
     38  1.6    lukem __COPYRIGHT("@(#) Copyright (c) 1989, 1993, 1994\n\
     39  1.6    lukem 	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.4      jtc static char sccsid[] = "@(#)column.c	8.4 (Berkeley) 5/4/95";
     45  1.3    glass #endif
     46  1.8  mycroft __RCSID("$NetBSD: column.c,v 1.8 1998/07/28 19:26:09 mycroft Exp $");
     47  1.1      cgd #endif /* not lint */
     48  1.1      cgd 
     49  1.1      cgd #include <sys/types.h>
     50  1.1      cgd #include <sys/ioctl.h>
     51  1.3    glass 
     52  1.3    glass #include <ctype.h>
     53  1.3    glass #include <err.h>
     54  1.3    glass #include <limits.h>
     55  1.1      cgd #include <stdio.h>
     56  1.3    glass #include <stdlib.h>
     57  1.1      cgd #include <string.h>
     58  1.4      jtc #include <unistd.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.6    lukem int   main __P((int, char **));
     65  1.3    glass void  print __P((void));
     66  1.3    glass void  r_columnate __P((void));
     67  1.3    glass void  usage __P((void));
     68  1.1      cgd 
     69  1.1      cgd int termwidth = 80;		/* default terminal width */
     70  1.1      cgd 
     71  1.1      cgd int entries;			/* number of records */
     72  1.1      cgd int eval;			/* exit value */
     73  1.1      cgd int maxlength;			/* longest record */
     74  1.1      cgd char **list;			/* array of pointers to records */
     75  1.1      cgd char *separator = "\t ";	/* field separator for table option */
     76  1.1      cgd 
     77  1.3    glass int
     78  1.1      cgd main(argc, argv)
     79  1.1      cgd 	int argc;
     80  1.1      cgd 	char **argv;
     81  1.1      cgd {
     82  1.1      cgd 	struct winsize win;
     83  1.1      cgd 	FILE *fp;
     84  1.1      cgd 	int ch, tflag, xflag;
     85  1.8  mycroft 	const char *p;
     86  1.1      cgd 
     87  1.1      cgd 	if (ioctl(1, TIOCGWINSZ, &win) == -1 || !win.ws_col) {
     88  1.7    lukem 		if ((p = getenv("COLUMNS")) != NULL)
     89  1.1      cgd 			termwidth = atoi(p);
     90  1.1      cgd 	} else
     91  1.1      cgd 		termwidth = win.ws_col;
     92  1.1      cgd 
     93  1.3    glass 	tflag = xflag = 0;
     94  1.6    lukem 	while ((ch = getopt(argc, argv, "c:s:tx")) != -1)
     95  1.1      cgd 		switch(ch) {
     96  1.1      cgd 		case 'c':
     97  1.1      cgd 			termwidth = atoi(optarg);
     98  1.1      cgd 			break;
     99  1.1      cgd 		case 's':
    100  1.1      cgd 			separator = optarg;
    101  1.1      cgd 			break;
    102  1.1      cgd 		case 't':
    103  1.1      cgd 			tflag = 1;
    104  1.1      cgd 			break;
    105  1.1      cgd 		case 'x':
    106  1.1      cgd 			xflag = 1;
    107  1.1      cgd 			break;
    108  1.1      cgd 		case '?':
    109  1.1      cgd 		default:
    110  1.1      cgd 			usage();
    111  1.1      cgd 		}
    112  1.1      cgd 	argc -= optind;
    113  1.1      cgd 	argv += optind;
    114  1.1      cgd 
    115  1.1      cgd 	if (!*argv)
    116  1.1      cgd 		input(stdin);
    117  1.1      cgd 	else for (; *argv; ++argv)
    118  1.7    lukem 		if ((fp = fopen(*argv, "r")) != NULL) {
    119  1.1      cgd 			input(fp);
    120  1.1      cgd 			(void)fclose(fp);
    121  1.1      cgd 		} else {
    122  1.3    glass 			warn("%s", *argv);
    123  1.1      cgd 			eval = 1;
    124  1.1      cgd 		}
    125  1.1      cgd 
    126  1.1      cgd 	if (!entries)
    127  1.1      cgd 		exit(eval);
    128  1.1      cgd 
    129  1.1      cgd 	if (tflag)
    130  1.1      cgd 		maketbl();
    131  1.1      cgd 	else if (maxlength >= termwidth)
    132  1.1      cgd 		print();
    133  1.1      cgd 	else if (xflag)
    134  1.1      cgd 		c_columnate();
    135  1.1      cgd 	else
    136  1.1      cgd 		r_columnate();
    137  1.1      cgd 	exit(eval);
    138  1.1      cgd }
    139  1.1      cgd 
    140  1.1      cgd #define	TAB	8
    141  1.3    glass void
    142  1.1      cgd c_columnate()
    143  1.1      cgd {
    144  1.3    glass 	int chcnt, col, cnt, endcol, numcols;
    145  1.1      cgd 	char **lp;
    146  1.1      cgd 
    147  1.1      cgd 	maxlength = (maxlength + TAB) & ~(TAB - 1);
    148  1.1      cgd 	numcols = termwidth / maxlength;
    149  1.1      cgd 	endcol = maxlength;
    150  1.1      cgd 	for (chcnt = col = 0, lp = list;; ++lp) {
    151  1.1      cgd 		chcnt += printf("%s", *lp);
    152  1.1      cgd 		if (!--entries)
    153  1.1      cgd 			break;
    154  1.1      cgd 		if (++col == numcols) {
    155  1.1      cgd 			chcnt = col = 0;
    156  1.1      cgd 			endcol = maxlength;
    157  1.1      cgd 			putchar('\n');
    158  1.1      cgd 		} else {
    159  1.6    lukem 			while ((cnt = ((chcnt + TAB) & ~(TAB - 1))) <= endcol) {
    160  1.1      cgd 				(void)putchar('\t');
    161  1.1      cgd 				chcnt = cnt;
    162  1.1      cgd 			}
    163  1.1      cgd 			endcol += maxlength;
    164  1.1      cgd 		}
    165  1.1      cgd 	}
    166  1.1      cgd 	if (chcnt)
    167  1.1      cgd 		putchar('\n');
    168  1.1      cgd }
    169  1.1      cgd 
    170  1.3    glass void
    171  1.1      cgd r_columnate()
    172  1.1      cgd {
    173  1.3    glass 	int base, chcnt, cnt, col, endcol, numcols, numrows, row;
    174  1.1      cgd 
    175  1.1      cgd 	maxlength = (maxlength + TAB) & ~(TAB - 1);
    176  1.1      cgd 	numcols = termwidth / maxlength;
    177  1.1      cgd 	numrows = entries / numcols;
    178  1.1      cgd 	if (entries % numcols)
    179  1.1      cgd 		++numrows;
    180  1.1      cgd 
    181  1.1      cgd 	for (row = 0; row < numrows; ++row) {
    182  1.1      cgd 		endcol = maxlength;
    183  1.1      cgd 		for (base = row, chcnt = col = 0; col < numcols; ++col) {
    184  1.1      cgd 			chcnt += printf("%s", list[base]);
    185  1.1      cgd 			if ((base += numrows) >= entries)
    186  1.1      cgd 				break;
    187  1.6    lukem 			while ((cnt = ((chcnt + TAB) & ~(TAB - 1))) <= endcol) {
    188  1.1      cgd 				(void)putchar('\t');
    189  1.1      cgd 				chcnt = cnt;
    190  1.1      cgd 			}
    191  1.1      cgd 			endcol += maxlength;
    192  1.1      cgd 		}
    193  1.1      cgd 		putchar('\n');
    194  1.1      cgd 	}
    195  1.1      cgd }
    196  1.1      cgd 
    197  1.3    glass void
    198  1.1      cgd print()
    199  1.1      cgd {
    200  1.3    glass 	int cnt;
    201  1.3    glass 	char **lp;
    202  1.1      cgd 
    203  1.1      cgd 	for (cnt = entries, lp = list; cnt--; ++lp)
    204  1.1      cgd 		(void)printf("%s\n", *lp);
    205  1.1      cgd }
    206  1.1      cgd 
    207  1.1      cgd typedef struct _tbl {
    208  1.1      cgd 	char **list;
    209  1.1      cgd 	int cols, *len;
    210  1.1      cgd } TBL;
    211  1.1      cgd #define	DEFCOLS	25
    212  1.1      cgd 
    213  1.3    glass void
    214  1.1      cgd maketbl()
    215  1.1      cgd {
    216  1.3    glass 	TBL *t;
    217  1.3    glass 	int coloff, cnt;
    218  1.3    glass 	char *p, **lp;
    219  1.1      cgd 	int *lens, maxcols;
    220  1.1      cgd 	TBL *tbl;
    221  1.3    glass 	char **cols;
    222  1.1      cgd 
    223  1.3    glass 	t = tbl = emalloc(entries * sizeof(TBL));
    224  1.3    glass 	cols = emalloc((maxcols = DEFCOLS) * sizeof(char *));
    225  1.3    glass 	lens = emalloc(maxcols * sizeof(int));
    226  1.1      cgd 	for (cnt = 0, lp = list; cnt < entries; ++cnt, ++lp, ++t) {
    227  1.6    lukem 		for (coloff = 0, p = *lp;
    228  1.6    lukem 		    (cols[coloff] = strtok(p, separator)) != NULL;
    229  1.1      cgd 		    p = NULL)
    230  1.1      cgd 			if (++coloff == maxcols) {
    231  1.3    glass 				if (!(cols = realloc(cols, (u_int)maxcols +
    232  1.3    glass 				    DEFCOLS * sizeof(char *))) ||
    233  1.3    glass 				    !(lens = realloc(lens,
    234  1.1      cgd 				    (u_int)maxcols + DEFCOLS * sizeof(int))))
    235  1.6    lukem 					err(1, "realloc");
    236  1.3    glass 				memset((char *)lens + maxcols * sizeof(int),
    237  1.3    glass 				    0, DEFCOLS * sizeof(int));
    238  1.1      cgd 				maxcols += DEFCOLS;
    239  1.1      cgd 			}
    240  1.3    glass 		t->list = emalloc(coloff * sizeof(char *));
    241  1.3    glass 		t->len = emalloc(coloff * sizeof(int));
    242  1.1      cgd 		for (t->cols = coloff; --coloff >= 0;) {
    243  1.1      cgd 			t->list[coloff] = cols[coloff];
    244  1.1      cgd 			t->len[coloff] = strlen(cols[coloff]);
    245  1.1      cgd 			if (t->len[coloff] > lens[coloff])
    246  1.1      cgd 				lens[coloff] = t->len[coloff];
    247  1.1      cgd 		}
    248  1.1      cgd 	}
    249  1.1      cgd 	for (cnt = 0, t = tbl; cnt < entries; ++cnt, ++t) {
    250  1.1      cgd 		for (coloff = 0; coloff < t->cols  - 1; ++coloff)
    251  1.1      cgd 			(void)printf("%s%*s", t->list[coloff],
    252  1.1      cgd 			    lens[coloff] - t->len[coloff] + 2, " ");
    253  1.1      cgd 		(void)printf("%s\n", t->list[coloff]);
    254  1.1      cgd 	}
    255  1.1      cgd }
    256  1.1      cgd 
    257  1.1      cgd #define	DEFNUM		1000
    258  1.3    glass #define	MAXLINELEN	(LINE_MAX + 1)
    259  1.1      cgd 
    260  1.3    glass void
    261  1.1      cgd input(fp)
    262  1.3    glass 	FILE *fp;
    263  1.1      cgd {
    264  1.1      cgd 	static int maxentry;
    265  1.3    glass 	int len;
    266  1.3    glass 	char *p, buf[MAXLINELEN];
    267  1.1      cgd 
    268  1.1      cgd 	if (!list)
    269  1.3    glass 		list = emalloc((maxentry = DEFNUM) * sizeof(char *));
    270  1.1      cgd 	while (fgets(buf, MAXLINELEN, fp)) {
    271  1.1      cgd 		for (p = buf; *p && isspace(*p); ++p);
    272  1.1      cgd 		if (!*p)
    273  1.1      cgd 			continue;
    274  1.3    glass 		if (!(p = strchr(p, '\n'))) {
    275  1.3    glass 			warnx("line too long");
    276  1.1      cgd 			eval = 1;
    277  1.1      cgd 			continue;
    278  1.1      cgd 		}
    279  1.1      cgd 		*p = '\0';
    280  1.1      cgd 		len = p - buf;
    281  1.1      cgd 		if (maxlength < len)
    282  1.1      cgd 			maxlength = len;
    283  1.1      cgd 		if (entries == maxentry) {
    284  1.1      cgd 			maxentry += DEFNUM;
    285  1.3    glass 			if (!(list = realloc(list,
    286  1.1      cgd 			    (u_int)maxentry * sizeof(char *))))
    287  1.6    lukem 				err(1, "realloc");
    288  1.1      cgd 		}
    289  1.1      cgd 		list[entries++] = strdup(buf);
    290  1.1      cgd 	}
    291  1.1      cgd }
    292  1.1      cgd 
    293  1.3    glass void *
    294  1.1      cgd emalloc(size)
    295  1.1      cgd 	int size;
    296  1.1      cgd {
    297  1.3    glass 	char *p;
    298  1.1      cgd 
    299  1.3    glass 	if (!(p = malloc(size)))
    300  1.6    lukem 		err(1, "malloc");
    301  1.3    glass 	memset(p, 0, size);
    302  1.3    glass 	return (p);
    303  1.1      cgd }
    304  1.1      cgd 
    305  1.3    glass void
    306  1.3    glass usage()
    307  1.1      cgd {
    308  1.1      cgd 
    309  1.1      cgd 	(void)fprintf(stderr,
    310  1.5    mikel 	    "usage: column [-tx] [-c columns] [-s sep] [file ...]\n");
    311  1.1      cgd 	exit(1);
    312  1.1      cgd }
    313