Home | History | Annotate | Line # | Download | only in column
column.c revision 1.14
      1  1.14  christos /*	$NetBSD: column.c,v 1.14 2006/04/09 19:51:23 christos 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.14  christos __RCSID("$NetBSD: column.c,v 1.14 2006/04/09 19:51:23 christos 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.13  christos #define	TAB	8
     58  1.13  christos 
     59  1.13  christos static void  c_columnate(void);
     60  1.13  christos static void *emalloc(size_t);
     61  1.13  christos static void *erealloc(void *, size_t);
     62  1.13  christos static char *estrdup(const char *);
     63  1.13  christos static void  input(FILE *);
     64  1.13  christos static void  maketbl(void);
     65  1.13  christos static void  print(void);
     66  1.13  christos static void  r_columnate(void);
     67  1.13  christos static void  usage(void) __attribute__((__noreturn__));
     68  1.13  christos 
     69  1.13  christos static int termwidth = 80;		/* default terminal width */
     70  1.13  christos 
     71  1.13  christos static int entries;			/* number of records */
     72  1.13  christos static int eval;			/* exit value */
     73  1.13  christos static int maxlength;			/* longest record */
     74  1.13  christos static char **list;			/* array of pointers to records */
     75  1.13  christos static const char *separator = "\t ";	/* field separator for table option */
     76   1.1       cgd 
     77   1.3     glass int
     78  1.12   xtraeme main(int argc, char **argv)
     79   1.1       cgd {
     80   1.1       cgd 	struct winsize win;
     81   1.1       cgd 	FILE *fp;
     82   1.1       cgd 	int ch, tflag, xflag;
     83   1.8   mycroft 	const char *p;
     84   1.1       cgd 
     85  1.13  christos 	setprogname(*argv);
     86  1.13  christos 
     87  1.13  christos 	if (ioctl(STDOUT_FILENO, 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.13  christos 			warn("Cannot open `%s'", *argv);
    123   1.1       cgd 			eval = 1;
    124   1.1       cgd 		}
    125   1.1       cgd 
    126   1.1       cgd 	if (!entries)
    127  1.13  christos 		return eval;
    128   1.1       cgd 
    129  1.13  christos 	maxlength = (maxlength + TAB) & ~(TAB - 1);
    130   1.1       cgd 	if (tflag)
    131   1.1       cgd 		maketbl();
    132   1.1       cgd 	else if (maxlength >= termwidth)
    133   1.1       cgd 		print();
    134   1.1       cgd 	else if (xflag)
    135   1.1       cgd 		c_columnate();
    136   1.1       cgd 	else
    137   1.1       cgd 		r_columnate();
    138  1.13  christos 	return eval;
    139   1.1       cgd }
    140   1.1       cgd 
    141  1.13  christos static void
    142  1.12   xtraeme c_columnate(void)
    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 	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.13  christos 			(void)putchar('\n');
    157   1.1       cgd 		} else {
    158   1.6     lukem 			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.13  christos 		(void)putchar('\n');
    167   1.1       cgd }
    168   1.1       cgd 
    169  1.13  christos static void
    170  1.12   xtraeme r_columnate(void)
    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 	numcols = termwidth / maxlength;
    175   1.1       cgd 	numrows = entries / numcols;
    176   1.1       cgd 	if (entries % numcols)
    177   1.1       cgd 		++numrows;
    178   1.1       cgd 
    179   1.1       cgd 	for (row = 0; row < numrows; ++row) {
    180   1.1       cgd 		endcol = maxlength;
    181   1.1       cgd 		for (base = row, chcnt = col = 0; col < numcols; ++col) {
    182   1.1       cgd 			chcnt += printf("%s", list[base]);
    183   1.1       cgd 			if ((base += numrows) >= entries)
    184   1.1       cgd 				break;
    185   1.6     lukem 			while ((cnt = ((chcnt + TAB) & ~(TAB - 1))) <= endcol) {
    186   1.1       cgd 				(void)putchar('\t');
    187   1.1       cgd 				chcnt = cnt;
    188   1.1       cgd 			}
    189   1.1       cgd 			endcol += maxlength;
    190   1.1       cgd 		}
    191  1.13  christos 		(void)putchar('\n');
    192   1.1       cgd 	}
    193   1.1       cgd }
    194   1.1       cgd 
    195  1.13  christos static void
    196  1.12   xtraeme print(void)
    197   1.1       cgd {
    198   1.3     glass 	int cnt;
    199   1.3     glass 	char **lp;
    200   1.1       cgd 
    201   1.1       cgd 	for (cnt = entries, lp = list; cnt--; ++lp)
    202   1.1       cgd 		(void)printf("%s\n", *lp);
    203   1.1       cgd }
    204   1.1       cgd 
    205   1.1       cgd typedef struct _tbl {
    206   1.1       cgd 	char **list;
    207   1.1       cgd 	int cols, *len;
    208   1.1       cgd } TBL;
    209   1.1       cgd #define	DEFCOLS	25
    210   1.1       cgd 
    211  1.13  christos static void
    212  1.12   xtraeme maketbl(void)
    213   1.1       cgd {
    214   1.3     glass 	TBL *t;
    215   1.3     glass 	int coloff, cnt;
    216   1.3     glass 	char *p, **lp;
    217  1.11    itojun 	int *lens, *nlens, maxcols;
    218   1.1       cgd 	TBL *tbl;
    219  1.11    itojun 	char **cols, **ncols;
    220   1.1       cgd 
    221   1.3     glass 	t = tbl = emalloc(entries * sizeof(TBL));
    222   1.3     glass 	cols = emalloc((maxcols = DEFCOLS) * sizeof(char *));
    223   1.3     glass 	lens = emalloc(maxcols * sizeof(int));
    224   1.1       cgd 	for (cnt = 0, lp = list; cnt < entries; ++cnt, ++lp, ++t) {
    225   1.6     lukem 		for (coloff = 0, p = *lp;
    226   1.6     lukem 		    (cols[coloff] = strtok(p, separator)) != NULL;
    227   1.1       cgd 		    p = NULL)
    228   1.1       cgd 			if (++coloff == maxcols) {
    229  1.13  christos 				ncols = erealloc(cols, maxcols +
    230  1.13  christos 				    DEFCOLS * sizeof(char *));
    231  1.13  christos 				nlens = erealloc(lens, maxcols +
    232  1.13  christos 				    DEFCOLS * sizeof(int));
    233  1.11    itojun 				cols = ncols;
    234  1.11    itojun 				lens = nlens;
    235  1.13  christos 				(void)memset((char *)(void *)lens + maxcols *
    236  1.13  christos 				    sizeof(int), 0, DEFCOLS * sizeof(int));
    237   1.1       cgd 				maxcols += DEFCOLS;
    238   1.1       cgd 			}
    239   1.3     glass 		t->list = emalloc(coloff * sizeof(char *));
    240   1.3     glass 		t->len = emalloc(coloff * sizeof(int));
    241   1.1       cgd 		for (t->cols = coloff; --coloff >= 0;) {
    242   1.1       cgd 			t->list[coloff] = cols[coloff];
    243   1.1       cgd 			t->len[coloff] = strlen(cols[coloff]);
    244   1.1       cgd 			if (t->len[coloff] > lens[coloff])
    245   1.1       cgd 				lens[coloff] = t->len[coloff];
    246   1.1       cgd 		}
    247   1.1       cgd 	}
    248   1.1       cgd 	for (cnt = 0, t = tbl; cnt < entries; ++cnt, ++t) {
    249   1.1       cgd 		for (coloff = 0; coloff < t->cols  - 1; ++coloff)
    250   1.1       cgd 			(void)printf("%s%*s", t->list[coloff],
    251   1.1       cgd 			    lens[coloff] - t->len[coloff] + 2, " ");
    252   1.1       cgd 		(void)printf("%s\n", t->list[coloff]);
    253   1.1       cgd 	}
    254  1.14  christos 	free(tbl);
    255  1.14  christos 	free(cols);
    256  1.14  christos 	free(lens);
    257   1.1       cgd }
    258   1.1       cgd 
    259   1.1       cgd #define	DEFNUM		1000
    260   1.3     glass #define	MAXLINELEN	(LINE_MAX + 1)
    261   1.1       cgd 
    262  1.13  christos static void
    263  1.12   xtraeme input(FILE *fp)
    264   1.1       cgd {
    265   1.1       cgd 	static int maxentry;
    266   1.3     glass 	int len;
    267   1.3     glass 	char *p, buf[MAXLINELEN];
    268  1.11    itojun 	char **n;
    269   1.1       cgd 
    270   1.1       cgd 	if (!list)
    271   1.3     glass 		list = emalloc((maxentry = DEFNUM) * sizeof(char *));
    272   1.1       cgd 	while (fgets(buf, MAXLINELEN, fp)) {
    273   1.9  christos 		for (p = buf; *p && isspace((unsigned char)*p); ++p);
    274   1.1       cgd 		if (!*p)
    275   1.1       cgd 			continue;
    276   1.3     glass 		if (!(p = strchr(p, '\n'))) {
    277   1.3     glass 			warnx("line too long");
    278   1.1       cgd 			eval = 1;
    279   1.1       cgd 			continue;
    280   1.1       cgd 		}
    281   1.1       cgd 		*p = '\0';
    282   1.1       cgd 		len = p - buf;
    283   1.1       cgd 		if (maxlength < len)
    284   1.1       cgd 			maxlength = len;
    285   1.1       cgd 		if (entries == maxentry) {
    286   1.1       cgd 			maxentry += DEFNUM;
    287  1.13  christos 			n = erealloc(list, maxentry * sizeof(char *));
    288  1.11    itojun 			list = n;
    289   1.1       cgd 		}
    290  1.13  christos 		list[entries++] = estrdup(buf);
    291   1.1       cgd 	}
    292   1.1       cgd }
    293   1.1       cgd 
    294  1.13  christos static void *
    295  1.13  christos emalloc(size_t size)
    296   1.1       cgd {
    297  1.13  christos 	void *p;
    298   1.1       cgd 
    299  1.13  christos 	if ((p = malloc(size)) == NULL)
    300   1.6     lukem 		err(1, "malloc");
    301  1.13  christos 	(void)memset(p, 0, size);
    302   1.3     glass 	return (p);
    303   1.1       cgd }
    304   1.1       cgd 
    305  1.13  christos static void *
    306  1.13  christos erealloc(void *op, size_t size)
    307  1.13  christos {
    308  1.13  christos 	void *p;
    309  1.13  christos 
    310  1.13  christos 	if ((p = realloc(op, size)) == NULL)
    311  1.13  christos 		err(1, "realloc");
    312  1.13  christos 	return p;
    313  1.13  christos }
    314  1.13  christos 
    315  1.13  christos static char *
    316  1.13  christos estrdup(const char *str)
    317  1.13  christos {
    318  1.13  christos 	char *p;
    319  1.13  christos 
    320  1.13  christos 	if ((p = strdup(str)) == NULL)
    321  1.13  christos 		err(1, "strdup");
    322  1.13  christos 	return p;
    323  1.13  christos }
    324  1.13  christos 
    325  1.13  christos static void
    326  1.12   xtraeme usage(void)
    327   1.1       cgd {
    328   1.1       cgd 
    329   1.1       cgd 	(void)fprintf(stderr,
    330  1.13  christos 	    "Usage: %s [-tx] [-c columns] [-s sep] [file ...]\n",
    331  1.13  christos 	    getprogname());
    332   1.1       cgd 	exit(1);
    333   1.1       cgd }
    334