Home | History | Annotate | Line # | Download | only in column
column.c revision 1.15.2.1
      1  1.15.2.1      tron /*	$NetBSD: column.c,v 1.15.2.1 2006/12/18 14:46:35 tron 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.15.2.1      tron __RCSID("$NetBSD: column.c,v 1.15.2.1 2006/12/18 14:46:35 tron 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.15  christos #include <util.h>
     57       1.3     glass 
     58      1.13  christos #define	TAB	8
     59  1.15.2.1      tron #define TABROUND(l) 	(((l) + TAB) & ~(TAB - 1))
     60      1.13  christos 
     61      1.13  christos static void  c_columnate(void);
     62      1.13  christos static void  input(FILE *);
     63      1.13  christos static void  maketbl(void);
     64      1.13  christos static void  print(void);
     65      1.13  christos static void  r_columnate(void);
     66      1.13  christos static void  usage(void) __attribute__((__noreturn__));
     67      1.13  christos 
     68      1.13  christos static int termwidth = 80;		/* default terminal width */
     69      1.13  christos 
     70      1.13  christos static int entries;			/* number of records */
     71      1.13  christos static int eval;			/* exit value */
     72      1.13  christos static int maxlength;			/* longest record */
     73      1.13  christos static char **list;			/* array of pointers to records */
     74      1.13  christos static const char *separator = "\t ";	/* field separator for table option */
     75       1.1       cgd 
     76       1.3     glass int
     77      1.12   xtraeme main(int argc, char **argv)
     78       1.1       cgd {
     79       1.1       cgd 	struct winsize win;
     80       1.1       cgd 	FILE *fp;
     81       1.1       cgd 	int ch, tflag, xflag;
     82       1.8   mycroft 	const char *p;
     83       1.1       cgd 
     84      1.13  christos 	setprogname(*argv);
     85      1.13  christos 
     86      1.13  christos 	if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &win) == -1 || !win.ws_col) {
     87       1.7     lukem 		if ((p = getenv("COLUMNS")) != NULL)
     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.6     lukem 	while ((ch = getopt(argc, argv, "c:s:tx")) != -1)
     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.7     lukem 		if ((fp = fopen(*argv, "r")) != NULL) {
    118       1.1       cgd 			input(fp);
    119       1.1       cgd 			(void)fclose(fp);
    120       1.1       cgd 		} else {
    121      1.13  christos 			warn("Cannot open `%s'", *argv);
    122       1.1       cgd 			eval = 1;
    123       1.1       cgd 		}
    124       1.1       cgd 
    125       1.1       cgd 	if (!entries)
    126      1.13  christos 		return eval;
    127       1.1       cgd 
    128  1.15.2.1      tron 	maxlength = TABROUND(maxlength);
    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.13  christos 	return eval;
    138       1.1       cgd }
    139       1.1       cgd 
    140      1.13  christos static void
    141      1.12   xtraeme c_columnate(void)
    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 	numcols = termwidth / maxlength;
    147       1.1       cgd 	endcol = maxlength;
    148       1.1       cgd 	for (chcnt = col = 0, lp = list;; ++lp) {
    149       1.1       cgd 		chcnt += printf("%s", *lp);
    150       1.1       cgd 		if (!--entries)
    151       1.1       cgd 			break;
    152       1.1       cgd 		if (++col == numcols) {
    153       1.1       cgd 			chcnt = col = 0;
    154       1.1       cgd 			endcol = maxlength;
    155      1.13  christos 			(void)putchar('\n');
    156       1.1       cgd 		} else {
    157  1.15.2.1      tron 			while ((cnt = TABROUND(chcnt)) <= endcol) {
    158       1.1       cgd 				(void)putchar('\t');
    159       1.1       cgd 				chcnt = cnt;
    160       1.1       cgd 			}
    161       1.1       cgd 			endcol += maxlength;
    162       1.1       cgd 		}
    163       1.1       cgd 	}
    164       1.1       cgd 	if (chcnt)
    165      1.13  christos 		(void)putchar('\n');
    166       1.1       cgd }
    167       1.1       cgd 
    168      1.13  christos static void
    169      1.12   xtraeme r_columnate(void)
    170       1.1       cgd {
    171       1.3     glass 	int base, chcnt, cnt, col, endcol, numcols, numrows, row;
    172       1.1       cgd 
    173       1.1       cgd 	numcols = termwidth / maxlength;
    174       1.1       cgd 	numrows = entries / numcols;
    175       1.1       cgd 	if (entries % numcols)
    176       1.1       cgd 		++numrows;
    177       1.1       cgd 
    178       1.1       cgd 	for (row = 0; row < numrows; ++row) {
    179       1.1       cgd 		endcol = maxlength;
    180       1.1       cgd 		for (base = row, chcnt = col = 0; col < numcols; ++col) {
    181       1.1       cgd 			chcnt += printf("%s", list[base]);
    182       1.1       cgd 			if ((base += numrows) >= entries)
    183       1.1       cgd 				break;
    184  1.15.2.1      tron 			while ((cnt = TABROUND(chcnt)) <= endcol) {
    185       1.1       cgd 				(void)putchar('\t');
    186       1.1       cgd 				chcnt = cnt;
    187       1.1       cgd 			}
    188       1.1       cgd 			endcol += maxlength;
    189       1.1       cgd 		}
    190      1.13  christos 		(void)putchar('\n');
    191       1.1       cgd 	}
    192       1.1       cgd }
    193       1.1       cgd 
    194      1.13  christos static void
    195      1.12   xtraeme print(void)
    196       1.1       cgd {
    197       1.3     glass 	int cnt;
    198       1.3     glass 	char **lp;
    199       1.1       cgd 
    200       1.1       cgd 	for (cnt = entries, lp = list; cnt--; ++lp)
    201       1.1       cgd 		(void)printf("%s\n", *lp);
    202       1.1       cgd }
    203       1.1       cgd 
    204       1.1       cgd typedef struct _tbl {
    205       1.1       cgd 	char **list;
    206       1.1       cgd 	int cols, *len;
    207       1.1       cgd } TBL;
    208       1.1       cgd #define	DEFCOLS	25
    209       1.1       cgd 
    210      1.13  christos static void
    211      1.12   xtraeme maketbl(void)
    212       1.1       cgd {
    213       1.3     glass 	TBL *t;
    214       1.3     glass 	int coloff, cnt;
    215       1.3     glass 	char *p, **lp;
    216      1.11    itojun 	int *lens, *nlens, maxcols;
    217       1.1       cgd 	TBL *tbl;
    218      1.11    itojun 	char **cols, **ncols;
    219       1.1       cgd 
    220  1.15.2.1      tron 	t = tbl = ecalloc(entries, sizeof(TBL));
    221  1.15.2.1      tron 	cols = ecalloc((maxcols = DEFCOLS), sizeof(char *));
    222  1.15.2.1      tron 	lens = ecalloc(maxcols, sizeof(int));
    223       1.1       cgd 	for (cnt = 0, lp = list; cnt < entries; ++cnt, ++lp, ++t) {
    224       1.6     lukem 		for (coloff = 0, p = *lp;
    225       1.6     lukem 		    (cols[coloff] = strtok(p, separator)) != NULL;
    226       1.1       cgd 		    p = NULL)
    227       1.1       cgd 			if (++coloff == maxcols) {
    228      1.13  christos 				ncols = erealloc(cols, maxcols +
    229      1.13  christos 				    DEFCOLS * sizeof(char *));
    230      1.13  christos 				nlens = erealloc(lens, maxcols +
    231      1.13  christos 				    DEFCOLS * sizeof(int));
    232      1.11    itojun 				cols = ncols;
    233      1.11    itojun 				lens = nlens;
    234  1.15.2.1      tron 				(void)memset(lens + maxcols, 0,
    235  1.15.2.1      tron 				    DEFCOLS * sizeof(int));
    236       1.1       cgd 				maxcols += DEFCOLS;
    237       1.1       cgd 			}
    238  1.15.2.1      tron 		t->list = ecalloc(coloff, sizeof(char *));
    239  1.15.2.1      tron 		t->len = ecalloc(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.14  christos 	free(tbl);
    254      1.14  christos 	free(cols);
    255      1.14  christos 	free(lens);
    256       1.1       cgd }
    257       1.1       cgd 
    258       1.1       cgd #define	DEFNUM		1000
    259       1.3     glass #define	MAXLINELEN	(LINE_MAX + 1)
    260       1.1       cgd 
    261      1.13  christos static void
    262      1.12   xtraeme input(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.11    itojun 	char **n;
    268       1.1       cgd 
    269       1.1       cgd 	if (!list)
    270  1.15.2.1      tron 		list = ecalloc((maxentry = DEFNUM), sizeof(char *));
    271       1.1       cgd 	while (fgets(buf, MAXLINELEN, fp)) {
    272       1.9  christos 		for (p = buf; *p && isspace((unsigned char)*p); ++p);
    273       1.1       cgd 		if (!*p)
    274       1.1       cgd 			continue;
    275       1.3     glass 		if (!(p = strchr(p, '\n'))) {
    276       1.3     glass 			warnx("line too long");
    277       1.1       cgd 			eval = 1;
    278       1.1       cgd 			continue;
    279       1.1       cgd 		}
    280       1.1       cgd 		*p = '\0';
    281       1.1       cgd 		len = p - buf;
    282       1.1       cgd 		if (maxlength < len)
    283       1.1       cgd 			maxlength = len;
    284       1.1       cgd 		if (entries == maxentry) {
    285       1.1       cgd 			maxentry += DEFNUM;
    286      1.13  christos 			n = erealloc(list, maxentry * sizeof(char *));
    287      1.11    itojun 			list = n;
    288       1.1       cgd 		}
    289      1.13  christos 		list[entries++] = estrdup(buf);
    290       1.1       cgd 	}
    291       1.1       cgd }
    292       1.1       cgd 
    293      1.13  christos static void
    294      1.12   xtraeme usage(void)
    295       1.1       cgd {
    296       1.1       cgd 
    297       1.1       cgd 	(void)fprintf(stderr,
    298      1.13  christos 	    "Usage: %s [-tx] [-c columns] [-s sep] [file ...]\n",
    299      1.13  christos 	    getprogname());
    300       1.1       cgd 	exit(1);
    301       1.1       cgd }
    302