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