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