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)
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);*/
333 char ibuf[BSIZE]; /* two screenfuls should do */
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 < BUFSIZ) { /* 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 = 1; i < BUFSIZ; *p++ = c, i++)
363 if ((c = getchar()) == EOF || c == '\n')
375 allocsize += allocsize;
376 p = (char **)realloc(elem, allocsize * sizeof(char *));
381 endelem = (elem = p) + allocsize;
386 getargs(int ac, char *av[])
391 flags |= NOARGS | TRANSPOSE;
393 while (--ac && **++av == '-')
394 for (p = *av+1; *p; p++)
401 case 'c': /* input col. separator */
402 flags |= ONEISEPONLY;
403 case 's': /* one or more allowed */
407 isep = '\t'; /* default is ^I */
410 flags |= ONEOSEPONLY;
415 osep = '\t'; /* default is ^I */
417 case 'w': /* window width, default 80 */
418 p = getnum(&owidth, p, 0);
420 errx(1, "width must be a positive integer");
422 case 'K': /* skip N lines */
424 case 'k': /* skip, do not print */
425 p = getnum(&skip, p, 0);
430 flags |= NOTRIMENDCOL;
432 case 'g': /* gutter space */
433 p = getnum(&gutter, p, 0);
436 p = getnum(&propgutter, p, 0);
438 case 'e': /* each line is an entry */
444 case 'j': /* right adjust */
445 flags |= RIGHTADJUST;
447 case 'n': /* null padding for missing values */
453 case 'H': /* print shape only */
454 flags |= DETAILSHAPE;
458 case 'z': /* squeeze col width */
462 ipagespace = atoi(++p); (default is 1)
464 case 'o': /* col order */
465 p = getlist(&cord, p);
469 p = getlist(&icbd, p);
473 p = getlist(&ocbd, p);
482 opages = atoi(av[2]);*/
484 if ((ocols = atoi(av[1])) < 0)
487 if ((orows = atoi(av[0])) < 0)
492 errx(1, "too many arguments");
497 getlist(short **list, char *p)
502 for (t = p + 1; *t; t++) {
503 if (!isdigit((unsigned char)*t))
505 "option %.1s requires a list of unsigned numbers separated by commas", t);
507 while (*t && isdigit((unsigned char)*t))
512 if (!(*list = (short *) malloc(count * sizeof(short))))
513 errx(1, "no list space");
515 for (t = p + 1; *t; t++) {
516 (*list)[count++] = atoi(t);
517 printf("++ %d ", (*list)[count-1]);
519 while (*t && isdigit((unsigned char)*t))
529 * num = number p points to; if (strict) complain
530 * returns pointer to end of num
533 getnum(int *num, char *p, int strict)
537 if (!isdigit((unsigned char)*++t)) {
538 if (strict || *t == '-' || *t == '+')
539 errx(1, "option %.1s requires an unsigned integer", p);
545 if (!isdigit((unsigned char)*t))