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$");
61 #define TRANSPOSE 000001
62 #define MTRANSPOSE 000002
63 #define ONEPERLINE 000004
64 #define ONEISEPONLY 000010
65 #define ONEOSEPONLY 000020
66 #define NOTRIMENDCOL 000040
67 #define SQUEEZE 000100
68 #define SHAPEONLY 000200
69 #define DETAILSHAPE 000400
70 #define RIGHTADJUST 001000
71 #define NULLPAD 002000
72 #define RECYCLE 004000
73 #define SKIPPRINT 010000
74 #define ICOLBOUNDS 020000
75 #define OCOLBOUNDS 040000
76 #define ONEPERCHAR 0100000
77 #define NOARGS 0200000
87 int allocsize = BUFSIZ;
90 int orows = 0, ocols = 0;
94 char isep = ' ', osep = ' ';
96 int owidth = 80, gutter = 2;
98 void getargs(int, char *[]);
101 char *getlist(short **, char *);
102 char *getnum(int *, char *, int);
103 char **getptrs(char **);
105 void prints(char *, int);
107 static void usage(void);
109 #define INCR(ep) do { \
110 if (++ep >= endelem) \
115 main(int argc, char *argv[])
119 if (flags & SHAPEONLY) {
120 printf("%d %d\n", irows, icols);
135 int multisep = (flags & ONEISEPONLY ? 0 : 1);
136 int nullpad = flags & NULLPAD;
141 if (flags & SKIPPRINT)
147 if (flags & NOARGS && curlen < owidth)
149 if (flags & ONEPERLINE)
151 else /* count cols on first line */
152 for (p = curline, endp = curline + curlen; p < endp; p++) {
153 if (*p == isep && multisep)
156 while (*p && *p != isep)
161 if (flags & ONEPERLINE) {
163 INCR(ep); /* prepare for next entry */
169 for (p = curline, endp = curline + curlen; p < endp; p++) {
170 if (*p == isep && multisep)
171 continue; /* eat up column separators */
172 if (*p == isep) /* must be an empty column */
174 else /* store column entry */
176 while (p < endp && *p != isep)
177 p++; /* find end of entry */
178 *p = '\0'; /* mark end of entry */
179 if (maxlen < p - *ep) /* update maxlen */
181 INCR(ep); /* prepare for next entry */
183 irows++; /* update row count */
184 if (nullpad) { /* pad missing entries */
185 padto = elem + irows * icols;
191 } while (getline() != EOF);
192 *ep = 0; /* mark end of pointers */
203 if (flags & TRANSPOSE)
204 for (i = 0; i < orows; i++) {
205 for (j = i; j < nelem; j += orows)
206 prints(ep[j], (j - i) / orows);
210 for (i = k = 0; i < orows; i++) {
211 for (j = 0; j < ocols; j++, k++)
219 prints(char *s, int col)
226 n = (flags & ONEOSEPONLY ? 1 : colwidths[col] - (p - s));
227 if (flags & RIGHTADJUST)
240 "usage: rs [-[csCS][x][kKgGw][N]tTeEnyjhHmz] [rows [cols]]\n");
257 gutter += maxlen * propgutter / 100.0;
258 colw = maxlen + gutter;
259 if (flags & MTRANSPOSE) {
263 else if (orows == 0 && ocols == 0) { /* decide rows and cols */
264 ocols = owidth / colw;
266 warnx("display width %d is less than column width %d",
272 orows = nelem / ocols + (nelem % ocols ? 1 : 0);
274 else if (orows == 0) /* decide on rows */
275 orows = nelem / ocols + (nelem % ocols ? 1 : 0);
276 else if (ocols == 0) /* decide on cols */
277 ocols = nelem / orows + (nelem % orows ? 1 : 0);
278 lp = elem + orows * ocols;
279 while (lp > endelem) {
280 getptrs(elem + nelem);
281 lp = elem + orows * ocols;
283 if (flags & RECYCLE) {
284 for (ep = elem + nelem; ep < lp; ep++)
288 if (!(colwidths = (short *) malloc(ocols * sizeof(short))))
290 if (flags & SQUEEZE) {
292 if (flags & TRANSPOSE)
293 for (i = 0; i < ocols; i++) {
295 for (j = 0; *ep != NULL && j < orows; j++)
296 if ((n = strlen(*ep++)) > max)
298 colwidths[i] = max + gutter;
301 for (i = 0; i < ocols; i++) {
303 for (j = i; j < nelem; j += ocols)
304 if ((n = strlen(ep[j])) > max)
306 colwidths[i] = max + gutter;
309 /* for (i = 0; i < orows; i++) {
310 for (j = i; j < nelem; j += orows)
311 prints(ep[j], (j - i) / orows);
315 for (i = 0; i < orows; i++) {
316 for (j = 0; j < ocols; j++)
321 for (i = 0; i < ocols; i++)
323 if (!(flags & NOTRIMENDCOL)) {
324 if (flags & RIGHTADJUST)
325 colwidths[0] -= gutter;
327 colwidths[ocols - 1] = 0;
330 if (n > nelem && (flags & RECYCLE))
332 /*for (i = 0; i < ocols; i++)
333 warnx("%d is colwidths, nelem %d", colwidths[i], nelem);*/
336 #define BSIZE (LINE_MAX * 2)
340 getline(void) /* get line; maintain curline, curlen; manage storage */
342 static int putlength;
343 static char *endblock = ibuf + BSIZE;
349 putlength = flags & DETAILSHAPE;
351 else if (skip <= 0) { /* don't waste storage */
352 curline += curlen + 1;
353 if (putlength) { /* print length, recycle storage */
354 printf(" %d line %d\n", curlen, irows);
358 if (!putlength && endblock - curline < LINE_MAX + 1) { /* need storage */
359 /*ww = endblock-curline; tt += ww;*/
360 /*printf("#wasted %d total %d\n",ww,tt);*/
361 if (!(curline = (char *) malloc(BSIZE)))
362 errx(1, "file too large");
363 endblock = curline + BSIZE;
364 /*printf("#endb %d curline %d\n",endblock,curline);*/
366 for (p = curline, i = 0;; *p++ = c, i++) {
367 if ((c = getchar()) == EOF)
370 errx(1, "maximum line length (%d) exceeded", LINE_MAX);
384 allocsize += allocsize;
385 p = (char **)realloc(elem, allocsize * sizeof(char *));
390 endelem = (elem = p) + allocsize;
395 getargs(int ac, char *av[])
400 flags |= NOARGS | TRANSPOSE;
402 while (--ac && **++av == '-')
403 for (p = *av+1; *p; p++)
410 case 'c': /* input col. separator */
411 flags |= ONEISEPONLY;
412 case 's': /* one or more allowed */
416 isep = '\t'; /* default is ^I */
419 flags |= ONEOSEPONLY;
424 osep = '\t'; /* default is ^I */
426 case 'w': /* window width, default 80 */
427 p = getnum(&owidth, p, 0);
429 errx(1, "width must be a positive integer");
431 case 'K': /* skip N lines */
433 case 'k': /* skip, do not print */
434 p = getnum(&skip, p, 0);
439 flags |= NOTRIMENDCOL;
441 case 'g': /* gutter space */
442 p = getnum(&gutter, p, 0);
445 p = getnum(&propgutter, p, 0);
447 case 'e': /* each line is an entry */
453 case 'j': /* right adjust */
454 flags |= RIGHTADJUST;
456 case 'n': /* null padding for missing values */
462 case 'H': /* print shape only */
463 flags |= DETAILSHAPE;
467 case 'z': /* squeeze col width */
471 ipagespace = atoi(++p); (default is 1)
473 case 'o': /* col order */
474 p = getlist(&cord, p);
478 p = getlist(&icbd, p);
482 p = getlist(&ocbd, p);
491 opages = atoi(av[2]);*/
493 if ((ocols = atoi(av[1])) < 0)
496 if ((orows = atoi(av[0])) < 0)
501 errx(1, "too many arguments");
506 getlist(short **list, char *p)
511 for (t = p + 1; *t; t++) {
512 if (!isdigit((unsigned char)*t))
514 "option %.1s requires a list of unsigned numbers separated by commas", t);
516 while (*t && isdigit((unsigned char)*t))
521 if (!(*list = (short *) malloc(count * sizeof(short))))
522 errx(1, "no list space");
524 for (t = p + 1; *t; t++) {
525 (*list)[count++] = atoi(t);
526 printf("++ %d ", (*list)[count-1]);
528 while (*t && isdigit((unsigned char)*t))
538 * num = number p points to; if (strict) complain
539 * returns pointer to end of num
542 getnum(int *num, char *p, int strict)
546 if (!isdigit((unsigned char)*++t)) {
547 if (strict || *t == '-' || *t == '+')
548 errx(1, "option %.1s requires an unsigned integer", p);
554 if (!isdigit((unsigned char)*t))