column.c revision 1.9 1 1.9 christos /* $NetBSD: column.c,v 1.9 1998/11/06 22:59:12 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.1 cgd * 3. All advertising materials mentioning features or use of this software
16 1.1 cgd * must display the following acknowledgement:
17 1.1 cgd * This product includes software developed by the University of
18 1.1 cgd * California, Berkeley and its contributors.
19 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
20 1.1 cgd * may be used to endorse or promote products derived from this software
21 1.1 cgd * without specific prior written permission.
22 1.1 cgd *
23 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 cgd * SUCH DAMAGE.
34 1.1 cgd */
35 1.1 cgd
36 1.6 lukem #include <sys/cdefs.h>
37 1.1 cgd #ifndef lint
38 1.6 lukem __COPYRIGHT("@(#) Copyright (c) 1989, 1993, 1994\n\
39 1.6 lukem The Regents of the University of California. All rights reserved.\n");
40 1.1 cgd #endif /* not lint */
41 1.1 cgd
42 1.1 cgd #ifndef lint
43 1.3 glass #if 0
44 1.4 jtc static char sccsid[] = "@(#)column.c 8.4 (Berkeley) 5/4/95";
45 1.3 glass #endif
46 1.9 christos __RCSID("$NetBSD: column.c,v 1.9 1998/11/06 22:59:12 christos Exp $");
47 1.1 cgd #endif /* not lint */
48 1.1 cgd
49 1.1 cgd #include <sys/types.h>
50 1.1 cgd #include <sys/ioctl.h>
51 1.3 glass
52 1.3 glass #include <ctype.h>
53 1.3 glass #include <err.h>
54 1.9 christos #include <termios.h>
55 1.3 glass #include <limits.h>
56 1.1 cgd #include <stdio.h>
57 1.3 glass #include <stdlib.h>
58 1.1 cgd #include <string.h>
59 1.4 jtc #include <unistd.h>
60 1.3 glass
61 1.3 glass void c_columnate __P((void));
62 1.3 glass void *emalloc __P((int));
63 1.3 glass void input __P((FILE *));
64 1.3 glass void maketbl __P((void));
65 1.6 lukem int main __P((int, char **));
66 1.3 glass void print __P((void));
67 1.3 glass void r_columnate __P((void));
68 1.3 glass void usage __P((void));
69 1.1 cgd
70 1.1 cgd int termwidth = 80; /* default terminal width */
71 1.1 cgd
72 1.1 cgd int entries; /* number of records */
73 1.1 cgd int eval; /* exit value */
74 1.1 cgd int maxlength; /* longest record */
75 1.1 cgd char **list; /* array of pointers to records */
76 1.1 cgd char *separator = "\t "; /* field separator for table option */
77 1.1 cgd
78 1.3 glass int
79 1.1 cgd main(argc, argv)
80 1.1 cgd int argc;
81 1.1 cgd char **argv;
82 1.1 cgd {
83 1.1 cgd struct winsize win;
84 1.1 cgd FILE *fp;
85 1.1 cgd int ch, tflag, xflag;
86 1.8 mycroft const char *p;
87 1.1 cgd
88 1.1 cgd if (ioctl(1, TIOCGWINSZ, &win) == -1 || !win.ws_col) {
89 1.7 lukem if ((p = getenv("COLUMNS")) != NULL)
90 1.1 cgd termwidth = atoi(p);
91 1.1 cgd } else
92 1.1 cgd termwidth = win.ws_col;
93 1.1 cgd
94 1.3 glass tflag = xflag = 0;
95 1.6 lukem while ((ch = getopt(argc, argv, "c:s:tx")) != -1)
96 1.1 cgd switch(ch) {
97 1.1 cgd case 'c':
98 1.1 cgd termwidth = atoi(optarg);
99 1.1 cgd break;
100 1.1 cgd case 's':
101 1.1 cgd separator = optarg;
102 1.1 cgd break;
103 1.1 cgd case 't':
104 1.1 cgd tflag = 1;
105 1.1 cgd break;
106 1.1 cgd case 'x':
107 1.1 cgd xflag = 1;
108 1.1 cgd break;
109 1.1 cgd case '?':
110 1.1 cgd default:
111 1.1 cgd usage();
112 1.1 cgd }
113 1.1 cgd argc -= optind;
114 1.1 cgd argv += optind;
115 1.1 cgd
116 1.1 cgd if (!*argv)
117 1.1 cgd input(stdin);
118 1.1 cgd else for (; *argv; ++argv)
119 1.7 lukem if ((fp = fopen(*argv, "r")) != NULL) {
120 1.1 cgd input(fp);
121 1.1 cgd (void)fclose(fp);
122 1.1 cgd } else {
123 1.3 glass warn("%s", *argv);
124 1.1 cgd eval = 1;
125 1.1 cgd }
126 1.1 cgd
127 1.1 cgd if (!entries)
128 1.1 cgd exit(eval);
129 1.1 cgd
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.1 cgd exit(eval);
139 1.1 cgd }
140 1.1 cgd
141 1.1 cgd #define TAB 8
142 1.3 glass void
143 1.1 cgd c_columnate()
144 1.1 cgd {
145 1.3 glass int chcnt, col, cnt, endcol, numcols;
146 1.1 cgd char **lp;
147 1.1 cgd
148 1.1 cgd maxlength = (maxlength + TAB) & ~(TAB - 1);
149 1.1 cgd numcols = termwidth / maxlength;
150 1.1 cgd endcol = maxlength;
151 1.1 cgd for (chcnt = col = 0, lp = list;; ++lp) {
152 1.1 cgd chcnt += printf("%s", *lp);
153 1.1 cgd if (!--entries)
154 1.1 cgd break;
155 1.1 cgd if (++col == numcols) {
156 1.1 cgd chcnt = col = 0;
157 1.1 cgd endcol = maxlength;
158 1.1 cgd putchar('\n');
159 1.1 cgd } else {
160 1.6 lukem while ((cnt = ((chcnt + TAB) & ~(TAB - 1))) <= endcol) {
161 1.1 cgd (void)putchar('\t');
162 1.1 cgd chcnt = cnt;
163 1.1 cgd }
164 1.1 cgd endcol += maxlength;
165 1.1 cgd }
166 1.1 cgd }
167 1.1 cgd if (chcnt)
168 1.1 cgd putchar('\n');
169 1.1 cgd }
170 1.1 cgd
171 1.3 glass void
172 1.1 cgd r_columnate()
173 1.1 cgd {
174 1.3 glass int base, chcnt, cnt, col, endcol, numcols, numrows, row;
175 1.1 cgd
176 1.1 cgd maxlength = (maxlength + TAB) & ~(TAB - 1);
177 1.1 cgd numcols = termwidth / maxlength;
178 1.1 cgd numrows = entries / numcols;
179 1.1 cgd if (entries % numcols)
180 1.1 cgd ++numrows;
181 1.1 cgd
182 1.1 cgd for (row = 0; row < numrows; ++row) {
183 1.1 cgd endcol = maxlength;
184 1.1 cgd for (base = row, chcnt = col = 0; col < numcols; ++col) {
185 1.1 cgd chcnt += printf("%s", list[base]);
186 1.1 cgd if ((base += numrows) >= entries)
187 1.1 cgd break;
188 1.6 lukem while ((cnt = ((chcnt + TAB) & ~(TAB - 1))) <= endcol) {
189 1.1 cgd (void)putchar('\t');
190 1.1 cgd chcnt = cnt;
191 1.1 cgd }
192 1.1 cgd endcol += maxlength;
193 1.1 cgd }
194 1.1 cgd putchar('\n');
195 1.1 cgd }
196 1.1 cgd }
197 1.1 cgd
198 1.3 glass void
199 1.1 cgd print()
200 1.1 cgd {
201 1.3 glass int cnt;
202 1.3 glass char **lp;
203 1.1 cgd
204 1.1 cgd for (cnt = entries, lp = list; cnt--; ++lp)
205 1.1 cgd (void)printf("%s\n", *lp);
206 1.1 cgd }
207 1.1 cgd
208 1.1 cgd typedef struct _tbl {
209 1.1 cgd char **list;
210 1.1 cgd int cols, *len;
211 1.1 cgd } TBL;
212 1.1 cgd #define DEFCOLS 25
213 1.1 cgd
214 1.3 glass void
215 1.1 cgd maketbl()
216 1.1 cgd {
217 1.3 glass TBL *t;
218 1.3 glass int coloff, cnt;
219 1.3 glass char *p, **lp;
220 1.1 cgd int *lens, maxcols;
221 1.1 cgd TBL *tbl;
222 1.3 glass char **cols;
223 1.1 cgd
224 1.3 glass t = tbl = emalloc(entries * sizeof(TBL));
225 1.3 glass cols = emalloc((maxcols = DEFCOLS) * sizeof(char *));
226 1.3 glass lens = emalloc(maxcols * sizeof(int));
227 1.1 cgd for (cnt = 0, lp = list; cnt < entries; ++cnt, ++lp, ++t) {
228 1.6 lukem for (coloff = 0, p = *lp;
229 1.6 lukem (cols[coloff] = strtok(p, separator)) != NULL;
230 1.1 cgd p = NULL)
231 1.1 cgd if (++coloff == maxcols) {
232 1.3 glass if (!(cols = realloc(cols, (u_int)maxcols +
233 1.3 glass DEFCOLS * sizeof(char *))) ||
234 1.3 glass !(lens = realloc(lens,
235 1.1 cgd (u_int)maxcols + DEFCOLS * sizeof(int))))
236 1.6 lukem err(1, "realloc");
237 1.3 glass memset((char *)lens + maxcols * sizeof(int),
238 1.3 glass 0, DEFCOLS * sizeof(int));
239 1.1 cgd maxcols += DEFCOLS;
240 1.1 cgd }
241 1.3 glass t->list = emalloc(coloff * sizeof(char *));
242 1.3 glass t->len = emalloc(coloff * sizeof(int));
243 1.1 cgd for (t->cols = coloff; --coloff >= 0;) {
244 1.1 cgd t->list[coloff] = cols[coloff];
245 1.1 cgd t->len[coloff] = strlen(cols[coloff]);
246 1.1 cgd if (t->len[coloff] > lens[coloff])
247 1.1 cgd lens[coloff] = t->len[coloff];
248 1.1 cgd }
249 1.1 cgd }
250 1.1 cgd for (cnt = 0, t = tbl; cnt < entries; ++cnt, ++t) {
251 1.1 cgd for (coloff = 0; coloff < t->cols - 1; ++coloff)
252 1.1 cgd (void)printf("%s%*s", t->list[coloff],
253 1.1 cgd lens[coloff] - t->len[coloff] + 2, " ");
254 1.1 cgd (void)printf("%s\n", t->list[coloff]);
255 1.1 cgd }
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.3 glass void
262 1.1 cgd input(fp)
263 1.3 glass 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.1 cgd
269 1.1 cgd if (!list)
270 1.3 glass list = emalloc((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.3 glass if (!(list = realloc(list,
287 1.1 cgd (u_int)maxentry * sizeof(char *))))
288 1.6 lukem err(1, "realloc");
289 1.1 cgd }
290 1.1 cgd list[entries++] = strdup(buf);
291 1.1 cgd }
292 1.1 cgd }
293 1.1 cgd
294 1.3 glass void *
295 1.1 cgd emalloc(size)
296 1.1 cgd int size;
297 1.1 cgd {
298 1.3 glass char *p;
299 1.1 cgd
300 1.3 glass if (!(p = malloc(size)))
301 1.6 lukem err(1, "malloc");
302 1.3 glass memset(p, 0, size);
303 1.3 glass return (p);
304 1.1 cgd }
305 1.1 cgd
306 1.3 glass void
307 1.3 glass usage()
308 1.1 cgd {
309 1.1 cgd
310 1.1 cgd (void)fprintf(stderr,
311 1.5 mikel "usage: column [-tx] [-c columns] [-s sep] [file ...]\n");
312 1.1 cgd exit(1);
313 1.1 cgd }
314