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