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