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 * 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.
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
35 static const char copyright[] =
36 "@(#) Copyright (c) 1993\n\
37 The Regents of the University of California. All rights reserved.\n";
41 static const char sccsid[] = "@(#)rs.c 8.1 (Berkeley) 6/6/93";
45 * rs - reshape a data array
46 * Author: John Kunze, Office of Comp. Affairs, UCB
47 * BEWARE: lots of unfinished edges
50 #include <sys/cdefs.h>
51 __FBSDID("$FreeBSD$");
60 #define TRANSPOSE 000001
61 #define MTRANSPOSE 000002
62 #define ONEPERLINE 000004
63 #define ONEISEPONLY 000010
64 #define ONEOSEPONLY 000020
65 #define NOTRIMENDCOL 000040
66 #define SQUEEZE 000100
67 #define SHAPEONLY 000200
68 #define DETAILSHAPE 000400
69 #define RIGHTADJUST 001000
70 #define NULLPAD 002000
71 #define RECYCLE 004000
72 #define SKIPPRINT 010000
73 #define ICOLBOUNDS 020000
74 #define OCOLBOUNDS 040000
75 #define ONEPERCHAR 0100000
76 #define NOARGS 0200000
86 int allocsize = BUFSIZ;
89 int orows = 0, ocols = 0;
93 char isep = ' ', osep = ' ';
95 int owidth = 80, gutter = 2;
97 void getargs(int, char *[]);
100 char *getlist(short **, char *);
101 char *getnum(int *, char *, int);
102 char **getptrs(char **);
104 void prints(char *, int);
106 static void usage(void);
108 #define INCR(ep) do { \
109 if (++ep >= endelem) \
114 main(int argc, char *argv[])
118 if (flags & SHAPEONLY) {
119 printf("%d %d\n", irows, icols);
133 int multisep = (flags & ONEISEPONLY ? 0 : 1);
134 int nullpad = flags & NULLPAD;
139 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)
158 if (flags & ONEPERLINE) {
160 INCR(ep); /* prepare for next entry */
166 for (p = curline, endp = curline + curlen; p < endp; p++) {
167 if (*p == isep && multisep)
168 continue; /* eat up column separators */
169 if (*p == isep) /* must be an empty column */
171 else /* store column entry */
173 while (p < endp && *p != isep)
174 p++; /* find end of entry */
175 *p = '\0'; /* mark end of entry */
176 if (maxlen < p - *ep) /* update maxlen */
178 INCR(ep); /* prepare for next entry */
180 irows++; /* update row count */
181 if (nullpad) { /* pad missing entries */
182 padto = elem + irows * icols;
188 } while (getline() != EOF);
189 *ep = 0; /* mark end of pointers */
200 if (flags & TRANSPOSE)
201 for (i = 0; i < orows; i++) {
202 for (j = i; j < nelem; j += orows)
203 prints(ep[j], (j - i) / orows);
207 for (i = k = 0; i < orows; i++) {
208 for (j = 0; j < ocols; j++, k++)
216 prints(char *s, int col)
223 n = (flags & ONEOSEPONLY ? 1 : colwidths[col] - (p - s));
224 if (flags & RIGHTADJUST)
237 "usage: rs [-[csCS][x][kKgGw][N]tTeEnyjhHmz] [rows [cols]]\n");
254 gutter += maxlen * propgutter / 100.0;
255 colw = maxlen + gutter;
256 if (flags & MTRANSPOSE) {
260 else if (orows == 0 && ocols == 0) { /* decide rows and cols */
261 ocols = owidth / colw;
263 warnx("display width %d is less than column width %d",
269 orows = nelem / ocols + (nelem % ocols ? 1 : 0);
271 else if (orows == 0) /* decide on rows */
272 orows = nelem / ocols + (nelem % ocols ? 1 : 0);
273 else if (ocols == 0) /* decide on cols */
274 ocols = nelem / orows + (nelem % orows ? 1 : 0);
275 lp = elem + orows * ocols;
276 while (lp > endelem) {
277 getptrs(elem + nelem);
278 lp = elem + orows * ocols;
280 if (flags & RECYCLE) {
281 for (ep = elem + nelem; ep < lp; ep++)
285 if (!(colwidths = (short *) malloc(ocols * sizeof(short))))
287 if (flags & SQUEEZE) {
289 if (flags & TRANSPOSE)
290 for (i = 0; i < ocols; i++) {
292 for (j = 0; *ep != NULL && j < orows; j++)
293 if ((n = strlen(*ep++)) > max)
295 colwidths[i] = max + gutter;
298 for (i = 0; i < ocols; i++) {
300 for (j = i; j < nelem; j += ocols)
301 if ((n = strlen(ep[j])) > max)
303 colwidths[i] = max + gutter;
306 /* for (i = 0; i < orows; i++) {
307 for (j = i; j < nelem; j += orows)
308 prints(ep[j], (j - i) / orows);
312 for (i = 0; i < orows; i++) {
313 for (j = 0; j < ocols; j++)
318 for (i = 0; i < ocols; i++)
320 if (!(flags & NOTRIMENDCOL)) {
321 if (flags & RIGHTADJUST)
322 colwidths[0] -= gutter;
324 colwidths[ocols - 1] = 0;
327 if (n > nelem && (flags & RECYCLE))
329 /*for (i = 0; i < ocols; i++)
330 warnx("%d is colwidths, nelem %d", colwidths[i], nelem);*/
334 char ibuf[BSIZE]; /* two screenfuls should do */
337 getline(void) /* get line; maintain curline, curlen; manage storage */
339 static int putlength;
340 static char *endblock = ibuf + BSIZE;
346 putlength = flags & DETAILSHAPE;
348 else if (skip <= 0) { /* don't waste storage */
349 curline += curlen + 1;
350 if (putlength) { /* print length, recycle storage */
351 printf(" %d line %d\n", curlen, irows);
355 if (!putlength && endblock - curline < BUFSIZ) { /* need storage */
356 /*ww = endblock-curline; tt += ww;*/
357 /*printf("#wasted %d total %d\n",ww,tt);*/
358 if (!(curline = (char *) malloc(BSIZE)))
359 errx(1, "file too large");
360 endblock = curline + BSIZE;
361 /*printf("#endb %d curline %d\n",endblock,curline);*/
363 for (p = curline, i = 1; i < BUFSIZ; *p++ = c, i++)
364 if ((c = getchar()) == EOF || c == '\n')
376 allocsize += allocsize;
377 p = (char **)realloc(elem, allocsize * sizeof(char *));
382 endelem = (elem = p) + allocsize;
387 getargs(int ac, char *av[])
392 flags |= NOARGS | TRANSPOSE;
394 while (--ac && **++av == '-')
395 for (p = *av+1; *p; p++)
402 case 'c': /* input col. separator */
403 flags |= ONEISEPONLY;
404 case 's': /* one or more allowed */
408 isep = '\t'; /* default is ^I */
411 flags |= ONEOSEPONLY;
416 osep = '\t'; /* default is ^I */
418 case 'w': /* window width, default 80 */
419 p = getnum(&owidth, p, 0);
421 errx(1, "width must be a positive integer");
423 case 'K': /* skip N lines */
425 case 'k': /* skip, do not print */
426 p = getnum(&skip, p, 0);
431 flags |= NOTRIMENDCOL;
433 case 'g': /* gutter space */
434 p = getnum(&gutter, p, 0);
437 p = getnum(&propgutter, p, 0);
439 case 'e': /* each line is an entry */
445 case 'j': /* right adjust */
446 flags |= RIGHTADJUST;
448 case 'n': /* null padding for missing values */
454 case 'H': /* print shape only */
455 flags |= DETAILSHAPE;
459 case 'z': /* squeeze col width */
463 ipagespace = atoi(++p); (default is 1)
465 case 'o': /* col order */
466 p = getlist(&cord, p);
470 p = getlist(&icbd, p);
474 p = getlist(&ocbd, p);
483 opages = atoi(av[2]);*/
485 if ((ocols = atoi(av[1])) < 0)
488 if ((orows = atoi(av[0])) < 0)
493 errx(1, "too many arguments");
498 getlist(short **list, char *p)
503 for (t = p + 1; *t; t++) {
504 if (!isdigit((unsigned char)*t))
506 "option %.1s requires a list of unsigned numbers separated by commas", t);
508 while (*t && isdigit((unsigned char)*t))
513 if (!(*list = (short *) malloc(count * sizeof(short))))
514 errx(1, "no list space");
516 for (t = p + 1; *t; t++) {
517 (*list)[count++] = atoi(t);
518 printf("++ %d ", (*list)[count-1]);
520 while (*t && isdigit((unsigned char)*t))
530 * num = number p points to; if (strict) complain
531 * returns pointer to end of num
534 getnum(int *num, char *p, int strict)
538 if (!isdigit((unsigned char)*++t)) {
539 if (strict || *t == '-' || *t == '+')
540 errx(1, "option %.1s requires an unsigned integer", p);
546 if (!isdigit((unsigned char)*t))