3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 static const char copyright[] =
32 "@(#) Copyright (c) 1993\n\
33 The Regents of the University of California. All rights reserved.\n";
37 static const char sccsid[] = "@(#)rs.c 8.1 (Berkeley) 6/6/93";
41 * rs - reshape a data array
42 * Author: John Kunze, Office of Comp. Affairs, UCB
43 * BEWARE: lots of unfinished edges
46 #include <sys/cdefs.h>
47 __FBSDID("$FreeBSD$");
57 #define TRANSPOSE 000001
58 #define MTRANSPOSE 000002
59 #define ONEPERLINE 000004
60 #define ONEISEPONLY 000010
61 #define ONEOSEPONLY 000020
62 #define NOTRIMENDCOL 000040
63 #define SQUEEZE 000100
64 #define SHAPEONLY 000200
65 #define DETAILSHAPE 000400
66 #define RIGHTADJUST 001000
67 #define NULLPAD 002000
68 #define RECYCLE 004000
69 #define SKIPPRINT 010000
70 #define ICOLBOUNDS 020000
71 #define OCOLBOUNDS 040000
72 #define ONEPERCHAR 0100000
73 #define NOARGS 0200000
83 int allocsize = BUFSIZ;
86 int orows = 0, ocols = 0;
90 char isep = ' ', osep = ' ';
92 int owidth = 80, gutter = 2;
94 void getargs(int, char *[]);
97 char *getlist(short **, char *);
98 char *getnum(int *, char *, int);
99 char **getptrs(char **);
101 void prints(char *, int);
103 static void usage(void);
105 #define INCR(ep) do { \
106 if (++ep >= endelem) \
111 main(int argc, char *argv[])
115 if (flags & SHAPEONLY) {
116 printf("%d %d\n", irows, icols);
131 int multisep = (flags & ONEISEPONLY ? 0 : 1);
132 int nullpad = flags & NULLPAD;
137 if (flags & SKIPPRINT)
143 if (flags & NOARGS && curlen < owidth)
145 if (flags & ONEPERLINE)
147 else /* count cols on first line */
148 for (p = curline, endp = curline + curlen; p < endp; p++) {
149 if (*p == isep && multisep)
152 while (*p && *p != isep)
157 if (flags & ONEPERLINE) {
159 INCR(ep); /* prepare for next entry */
165 for (p = curline, endp = curline + curlen; p < endp; p++) {
166 if (*p == isep && multisep)
167 continue; /* eat up column separators */
168 if (*p == isep) /* must be an empty column */
170 else /* store column entry */
172 while (p < endp && *p != isep)
173 p++; /* find end of entry */
174 *p = '\0'; /* mark end of entry */
175 if (maxlen < p - *ep) /* update maxlen */
177 INCR(ep); /* prepare for next entry */
179 irows++; /* update row count */
180 if (nullpad) { /* pad missing entries */
181 padto = elem + irows * icols;
187 } while (getline() != EOF);
188 *ep = 0; /* mark end of pointers */
199 if (flags & TRANSPOSE)
200 for (i = 0; i < orows; i++) {
201 for (j = i; j < nelem; j += orows)
202 prints(ep[j], (j - i) / orows);
206 for (i = k = 0; i < orows; i++) {
207 for (j = 0; j < ocols; j++, k++)
215 prints(char *s, int col)
222 n = (flags & ONEOSEPONLY ? 1 : colwidths[col] - (p - s));
223 if (flags & RIGHTADJUST)
236 "usage: rs [-[csCS][x][kKgGw][N]tTeEnyjhHmz] [rows [cols]]\n");
253 gutter += maxlen * propgutter / 100.0;
254 colw = maxlen + gutter;
255 if (flags & MTRANSPOSE) {
259 else if (orows == 0 && ocols == 0) { /* decide rows and cols */
260 ocols = owidth / colw;
262 warnx("display width %d is less than column width %d",
268 orows = nelem / ocols + (nelem % ocols ? 1 : 0);
270 else if (orows == 0) /* decide on rows */
271 orows = nelem / ocols + (nelem % ocols ? 1 : 0);
272 else if (ocols == 0) /* decide on cols */
273 ocols = nelem / orows + (nelem % orows ? 1 : 0);
274 lp = elem + orows * ocols;
275 while (lp > endelem) {
276 getptrs(elem + nelem);
277 lp = elem + orows * ocols;
279 if (flags & RECYCLE) {
280 for (ep = elem + nelem; ep < lp; ep++)
284 if (!(colwidths = (short *) malloc(ocols * sizeof(short))))
286 if (flags & SQUEEZE) {
288 if (flags & TRANSPOSE)
289 for (i = 0; i < ocols; i++) {
291 for (j = 0; *ep != NULL && j < orows; j++)
292 if ((n = strlen(*ep++)) > max)
294 colwidths[i] = max + gutter;
297 for (i = 0; i < ocols; i++) {
299 for (j = i; j < nelem; j += ocols)
300 if ((n = strlen(ep[j])) > max)
302 colwidths[i] = max + gutter;
305 /* for (i = 0; i < orows; i++) {
306 for (j = i; j < nelem; j += orows)
307 prints(ep[j], (j - i) / orows);
311 for (i = 0; i < orows; i++) {
312 for (j = 0; j < ocols; j++)
317 for (i = 0; i < ocols; i++)
319 if (!(flags & NOTRIMENDCOL)) {
320 if (flags & RIGHTADJUST)
321 colwidths[0] -= gutter;
323 colwidths[ocols - 1] = 0;
326 if (n > nelem && (flags & RECYCLE))
328 /*for (i = 0; i < ocols; i++)
329 warnx("%d is colwidths, nelem %d", colwidths[i], nelem);*/
332 #define BSIZE (LINE_MAX * 2)
336 getline(void) /* get line; maintain curline, curlen; manage storage */
338 static int putlength;
339 static char *endblock = ibuf + BSIZE;
345 putlength = flags & DETAILSHAPE;
347 else if (skip <= 0) { /* don't waste storage */
348 curline += curlen + 1;
349 if (putlength) { /* print length, recycle storage */
350 printf(" %d line %d\n", curlen, irows);
354 if (!putlength && endblock - curline < LINE_MAX + 1) { /* need storage */
355 /*ww = endblock-curline; tt += ww;*/
356 /*printf("#wasted %d total %d\n",ww,tt);*/
357 if (!(curline = (char *) malloc(BSIZE)))
358 errx(1, "file too large");
359 endblock = curline + BSIZE;
360 /*printf("#endb %d curline %d\n",endblock,curline);*/
362 for (p = curline, i = 0;; *p++ = c, i++) {
363 if ((c = getchar()) == EOF)
366 errx(1, "maximum line length (%d) exceeded", LINE_MAX);
380 allocsize += allocsize;
381 p = (char **)realloc(elem, allocsize * sizeof(char *));
386 endelem = (elem = p) + allocsize;
391 getargs(int ac, char *av[])
396 flags |= NOARGS | TRANSPOSE;
398 while (--ac && **++av == '-')
399 for (p = *av+1; *p; p++)
406 case 'c': /* input col. separator */
407 flags |= ONEISEPONLY;
408 case 's': /* one or more allowed */
412 isep = '\t'; /* default is ^I */
415 flags |= ONEOSEPONLY;
420 osep = '\t'; /* default is ^I */
422 case 'w': /* window width, default 80 */
423 p = getnum(&owidth, p, 0);
425 errx(1, "width must be a positive integer");
427 case 'K': /* skip N lines */
429 case 'k': /* skip, do not print */
430 p = getnum(&skip, p, 0);
435 flags |= NOTRIMENDCOL;
437 case 'g': /* gutter space */
438 p = getnum(&gutter, p, 0);
441 p = getnum(&propgutter, p, 0);
443 case 'e': /* each line is an entry */
449 case 'j': /* right adjust */
450 flags |= RIGHTADJUST;
452 case 'n': /* null padding for missing values */
458 case 'H': /* print shape only */
459 flags |= DETAILSHAPE;
463 case 'z': /* squeeze col width */
467 ipagespace = atoi(++p); (default is 1)
469 case 'o': /* col order */
470 p = getlist(&cord, p);
474 p = getlist(&icbd, p);
478 p = getlist(&ocbd, p);
487 opages = atoi(av[2]);*/
489 if ((ocols = atoi(av[1])) < 0)
492 if ((orows = atoi(av[0])) < 0)
497 errx(1, "too many arguments");
502 getlist(short **list, char *p)
507 for (t = p + 1; *t; t++) {
508 if (!isdigit((unsigned char)*t))
510 "option %.1s requires a list of unsigned numbers separated by commas", t);
512 while (*t && isdigit((unsigned char)*t))
517 if (!(*list = (short *) malloc(count * sizeof(short))))
518 errx(1, "no list space");
520 for (t = p + 1; *t; t++) {
521 (*list)[count++] = atoi(t);
522 printf("++ %d ", (*list)[count-1]);
524 while (*t && isdigit((unsigned char)*t))
534 * num = number p points to; if (strict) complain
535 * returns pointer to end of num
538 getnum(int *num, char *p, int strict)
542 if (!isdigit((unsigned char)*++t)) {
543 if (strict || *t == '-' || *t == '+')
544 errx(1, "option %.1s requires an unsigned integer", p);
550 if (!isdigit((unsigned char)*t))