]> git.ipfire.org Git - thirdparty/util-linux.git/blob - text-utils/column.c
Imported from util-linux-2.10s tarball.
[thirdparty/util-linux.git] / text-utils / column.c
1 /*
2 * Copyright (c) 1989, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34 /*
35 * 1999-02-22 Arkadiusz Mi¶kiewicz <misiek@misiek.eu.org>
36 * added Native Language Support
37 * 1999-09-19 Bruno Haible <haible@clisp.cons.org>
38 * modified to work correctly in multi-byte locales
39 */
40
41 #include <sys/types.h>
42 #include <sys/ioctl.h>
43
44 #include <ctype.h>
45 #include <limits.h>
46 #include <stdio.h>
47 #include <unistd.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include "errs.h"
51 #include "nls.h"
52
53 #include "widechar.h"
54
55 #ifdef ENABLE_WIDECHAR
56 #define wcs_width(s) wcswidth(s,wcslen(s))
57 static wchar_t *mbs_to_wcs(const char *);
58 #else
59 #define wcs_width(s) strlen(s)
60 #define mbs_to_wcs(s) strdup(s)
61 static char *mtsafe_strtok(char *, const char *, char **);
62 #define wcstok mtsafe_strtok
63 #endif
64
65 void c_columnate __P((void));
66 void *emalloc __P((int));
67 void input __P((FILE *));
68 void maketbl __P((void));
69 void print __P((void));
70 void r_columnate __P((void));
71 void usage __P((void));
72
73 int termwidth = 80; /* default terminal width */
74
75 int entries; /* number of records */
76 int eval; /* exit value */
77 int maxlength; /* longest record */
78 wchar_t **list; /* array of pointers to records */
79 wchar_t default_separator[] = { '\t', ' ', 0 };
80 wchar_t *separator = default_separator; /* field separator for table option */
81
82 int
83 main(int argc, char **argv)
84 {
85 struct winsize win;
86 FILE *fp;
87 int ch, tflag, xflag;
88 char *p;
89
90 #ifdef __linux__
91 extern int optind;
92 extern char *optarg;
93 extern char *__progname;
94 __progname = argv[0];
95 #endif
96 setlocale(LC_ALL, "");
97 bindtextdomain(PACKAGE, LOCALEDIR);
98 textdomain(PACKAGE);
99
100 if (ioctl(1, TIOCGWINSZ, &win) == -1 || !win.ws_col) {
101 if ((p = getenv("COLUMNS")) != NULL)
102 termwidth = atoi(p);
103 } else
104 termwidth = win.ws_col;
105
106 tflag = xflag = 0;
107 while ((ch = getopt(argc, argv, "c:s:tx")) != EOF)
108 switch(ch) {
109 case 'c':
110 termwidth = atoi(optarg);
111 break;
112 case 's':
113 separator = mbs_to_wcs(optarg);
114 break;
115 case 't':
116 tflag = 1;
117 break;
118 case 'x':
119 xflag = 1;
120 break;
121 case '?':
122 default:
123 usage();
124 }
125 argc -= optind;
126 argv += optind;
127
128 if (!*argv)
129 input(stdin);
130 else for (; *argv; ++argv)
131 if ((fp = fopen(*argv, "r")) != NULL) {
132 input(fp);
133 (void)fclose(fp);
134 } else {
135 warn("%s", *argv);
136 eval = 1;
137 }
138
139 if (!entries)
140 exit(eval);
141
142 if (tflag)
143 maketbl();
144 else if (maxlength >= termwidth)
145 print();
146 else if (xflag)
147 c_columnate();
148 else
149 r_columnate();
150 if (ferror(stdout) || fclose(stdout))
151 eval = 1;
152 exit(eval);
153 }
154
155 #define TAB 8
156 void
157 c_columnate()
158 {
159 int chcnt, col, cnt, endcol, numcols;
160 wchar_t **lp;
161
162 maxlength = (maxlength + TAB) & ~(TAB - 1);
163 numcols = termwidth / maxlength;
164 endcol = maxlength;
165 for (chcnt = col = 0, lp = list;; ++lp) {
166 fputws(*lp, stdout);
167 chcnt += wcs_width(*lp);
168 if (!--entries)
169 break;
170 if (++col == numcols) {
171 chcnt = col = 0;
172 endcol = maxlength;
173 putwchar('\n');
174 } else {
175 while ((cnt = ((chcnt + TAB) & ~(TAB - 1))) <= endcol) {
176 putwchar('\t');
177 chcnt = cnt;
178 }
179 endcol += maxlength;
180 }
181 }
182 if (chcnt)
183 putwchar('\n');
184 }
185
186 void
187 r_columnate()
188 {
189 int base, chcnt, cnt, col, endcol, numcols, numrows, row;
190
191 maxlength = (maxlength + TAB) & ~(TAB - 1);
192 numcols = termwidth / maxlength;
193 if (!numcols)
194 numcols = 1;
195 numrows = entries / numcols;
196 if (entries % numcols)
197 ++numrows;
198
199 for (row = 0; row < numrows; ++row) {
200 endcol = maxlength;
201 for (base = row, chcnt = col = 0; col < numcols; ++col) {
202 fputws(list[base], stdout);
203 chcnt += wcs_width(list[base]);
204 if ((base += numrows) >= entries)
205 break;
206 while ((cnt = ((chcnt + TAB) & ~(TAB - 1))) <= endcol) {
207 putwchar('\t');
208 chcnt = cnt;
209 }
210 endcol += maxlength;
211 }
212 putwchar('\n');
213 }
214 }
215
216 void
217 print()
218 {
219 int cnt;
220 wchar_t **lp;
221
222 for (cnt = entries, lp = list; cnt--; ++lp) {
223 fputws(*lp, stdout);
224 putwchar('\n');
225 }
226 }
227
228 typedef struct _tbl {
229 wchar_t **list;
230 int cols, *len;
231 } TBL;
232 #define DEFCOLS 25
233
234 void
235 maketbl()
236 {
237 TBL *t;
238 int coloff, cnt, i;
239 wchar_t *p, **lp;
240 int *lens, maxcols;
241 TBL *tbl;
242 wchar_t **cols;
243 wchar_t *wcstok_state;
244
245 t = tbl = emalloc(entries * sizeof(TBL));
246 cols = emalloc((maxcols = DEFCOLS) * sizeof(wchar_t *));
247 lens = emalloc(maxcols * sizeof(int));
248 for (cnt = 0, lp = list; cnt < entries; ++cnt, ++lp, ++t) {
249 for (coloff = 0, p = *lp;
250 (cols[coloff] = wcstok(p, separator, &wcstok_state)) != NULL;
251 p = NULL)
252 if (++coloff == maxcols) {
253 if (!(cols = realloc(cols, ((u_int)maxcols + DEFCOLS)
254 * sizeof(wchar_t *))) ||
255 !(lens = realloc(lens, ((u_int)maxcols + DEFCOLS)
256 * sizeof(int))))
257 err_nomsg(1);
258 memset((char *)lens + maxcols * sizeof(int),
259 0, DEFCOLS * sizeof(int));
260 maxcols += DEFCOLS;
261 }
262 t->list = emalloc(coloff * sizeof(wchar_t *));
263 t->len = emalloc(coloff * sizeof(int));
264 for (t->cols = coloff; --coloff >= 0;) {
265 t->list[coloff] = cols[coloff];
266 t->len[coloff] = wcs_width(cols[coloff]);
267 if (t->len[coloff] > lens[coloff])
268 lens[coloff] = t->len[coloff];
269 }
270 }
271 for (cnt = 0, t = tbl; cnt < entries; ++cnt, ++t) {
272 for (coloff = 0; coloff < t->cols - 1; ++coloff) {
273 fputws(t->list[coloff], stdout);
274 for (i = lens[coloff] - t->len[coloff] + 2; i > 0; i--)
275 putwchar(' ');
276 }
277 fputws(t->list[coloff], stdout);
278 putwchar('\n');
279 }
280 }
281
282 #define DEFNUM 1000
283 #define MAXLINELEN (LINE_MAX + 1)
284
285 void
286 input(fp)
287 FILE *fp;
288 {
289 static int maxentry;
290 int len;
291 wchar_t *p, buf[MAXLINELEN];
292
293 if (!list)
294 list = emalloc((maxentry = DEFNUM) * sizeof(wchar_t *));
295 while (fgetws(buf, MAXLINELEN, fp)) {
296 for (p = buf; *p && iswspace(*p); ++p);
297 if (!*p)
298 continue;
299 if (!(p = wcschr(p, '\n'))) {
300 warnx(_("line too long"));
301 eval = 1;
302 continue;
303 }
304 *p = '\0';
305 len = wcs_width(buf); /* len = p - buf; */
306 if (maxlength < len)
307 maxlength = len;
308 if (entries == maxentry) {
309 maxentry += DEFNUM;
310 if (!(list = realloc(list,
311 (u_int)maxentry * sizeof(wchar_t *))))
312 err_nomsg(1);
313 }
314 list[entries++] = wcsdup(buf);
315 }
316 }
317
318 #ifdef ENABLE_WIDECHAR
319 static wchar_t *mbs_to_wcs(const char *s)
320 {
321 size_t n;
322 wchar_t *wcs;
323
324 n = mbstowcs((wchar_t *)0, s, 0);
325 if (n < 0)
326 return NULL;
327 wcs = malloc((n + 1) * sizeof(wchar_t));
328 if (!wcs)
329 return NULL;
330 if (mbstowcs(wcs, s, n + 1) < 0)
331 return NULL;
332 return wcs;
333 }
334 #endif
335
336 #ifndef ENABLE_WIDECHAR
337 static char *mtsafe_strtok(char *str, const char *delim, char **ptr)
338 {
339 if (str == NULL) {
340 str = *ptr;
341 if (str == NULL)
342 return NULL;
343 }
344 str += strspn(str, delim);
345 if (*str == '\0') {
346 *ptr = NULL;
347 return NULL;
348 } else {
349 char *token_end = strpbrk(str, delim);
350 if (token_end) {
351 *token_end = '\0';
352 *ptr = token_end + 1;
353 } else
354 *ptr = NULL;
355 return str;
356 }
357 }
358 #endif
359
360 void *
361 emalloc(size)
362 int size;
363 {
364 char *p;
365
366 if (!(p = malloc(size)))
367 err_nomsg(1);
368 memset(p, 0, size);
369 return (p);
370 }
371
372 void
373 usage()
374 {
375
376 (void)fprintf(stderr,
377 _("usage: column [-tx] [-c columns] [file ...]\n"));
378 exit(1);
379 }