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