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
33 * @(#) Copyright (c) 1993 The Regents of the University of California. All rights reserved.
34 * @(#)rs.c 8.1 (Berkeley) 6/6/93
35 * $FreeBSD: src/usr.bin/rs/rs.c,v 1.5.2.2 2002/08/03 00:48:43 tjr Exp $
36 * $DragonFly: src/usr.bin/rs/rs.c,v 1.3 2005/01/05 00:32:50 cpressey Exp $
40 * rs - reshape a data array
41 * Author: John Kunze, Office of Comp. Affairs, UCB
42 * BEWARE: lots of unfinished edges
52 #define TRANSPOSE 000001
53 #define MTRANSPOSE 000002
54 #define ONEPERLINE 000004
55 #define ONEISEPONLY 000010
56 #define ONEOSEPONLY 000020
57 #define NOTRIMENDCOL 000040
58 #define SQUEEZE 000100
59 #define SHAPEONLY 000200
60 #define DETAILSHAPE 000400
61 #define RIGHTADJUST 001000
62 #define NULLPAD 002000
63 #define RECYCLE 004000
64 #define SKIPPRINT 010000
65 #define ICOLBOUNDS 020000
66 #define OCOLBOUNDS 040000
67 #define ONEPERCHAR 0100000
68 #define NOARGS 0200000
78 int allocsize = BUFSIZ;
85 char isep = ' ', osep = ' ';
87 int owidth = 80, gutter = 2;
89 void getargs(int, char *[]);
92 char *getlist(short **, char *);
93 char *getnum(int *, char *, int);
94 char **getptrs(char **);
96 void prints(char *, int);
98 static void usage(void);
100 #define INCR(ep) do { \
101 if (++ep >= endelem) \
106 main(int argc, char *argv[])
110 if (flags & SHAPEONLY) {
111 printf("%d %d\n", irows, icols);
125 int multisep = (flags & ONEISEPONLY ? 0 : 1);
126 int nullpad = flags & NULLPAD;
131 if (flags & SKIPPRINT)
135 if (flags & NOARGS && curlen < owidth)
137 if (flags & ONEPERLINE)
139 else /* count cols on first line */
140 for (p = curline, endp = curline + curlen; p < endp; p++) {
141 if (*p == isep && multisep)
144 while (*p && *p != isep)
150 if (flags & ONEPERLINE) {
152 INCR(ep); /* prepare for next entry */
158 for (p = curline, endp = curline + curlen; p < endp; p++) {
159 if (*p == isep && multisep)
160 continue; /* eat up column separators */
161 if (*p == isep) /* must be an empty column */
163 else /* store column entry */
165 while (p < endp && *p != isep)
166 p++; /* find end of entry */
167 *p = '\0'; /* mark end of entry */
168 if (maxlen < p - *ep) /* update maxlen */
170 INCR(ep); /* prepare for next entry */
172 irows++; /* update row count */
173 if (nullpad) { /* pad missing entries */
174 padto = elem + irows * icols;
180 } while (getline() != EOF);
181 *ep = 0; /* mark end of pointers */
192 if (flags & TRANSPOSE)
193 for (i = 0; i < orows; i++) {
194 for (j = i; j < nelem; j += orows)
195 prints(ep[j], (j - i) / orows);
199 for (i = k = 0; i < orows; i++) {
200 for (j = 0; j < ocols; j++, k++)
208 prints(char *s, int col)
215 n = (flags & ONEOSEPONLY ? 1 : colwidths[col] - (p - s));
216 if (flags & RIGHTADJUST)
229 "usage: rs [-[csCS][x][kKgGw][N]tTeEnyjhHmz] [rows [cols]]\n");
246 gutter += maxlen * propgutter / 100.0;
247 colw = maxlen + gutter;
248 if (flags & MTRANSPOSE) {
252 else if (orows == 0 && ocols == 0) { /* decide rows and cols */
253 ocols = owidth / colw;
255 warnx("display width %d is less than column width %d",
261 orows = nelem / ocols + (nelem % ocols ? 1 : 0);
263 else if (orows == 0) /* decide on rows */
264 orows = nelem / ocols + (nelem % ocols ? 1 : 0);
265 else if (ocols == 0) /* decide on cols */
266 ocols = nelem / orows + (nelem % orows ? 1 : 0);
267 lp = elem + orows * ocols;
268 while (lp > endelem) {
269 getptrs(elem + nelem);
270 lp = elem + orows * ocols;
272 if (flags & RECYCLE) {
273 for (ep = elem + nelem; ep < lp; ep++)
277 if (!(colwidths = (short *) malloc(ocols * sizeof(short))))
279 if (flags & SQUEEZE) {
281 if (flags & TRANSPOSE)
282 for (i = 0; i < ocols; i++) {
284 for (j = 0; *ep != NULL && j < orows; j++)
285 if ((n = strlen(*ep++)) > max)
287 colwidths[i] = max + gutter;
290 for (i = 0; i < ocols; i++) {
292 for (j = i; j < nelem; j += ocols)
293 if ((n = strlen(ep[j])) > max)
295 colwidths[i] = max + gutter;
298 /* for (i = 0; i < orows; i++) {
299 for (j = i; j < nelem; j += orows)
300 prints(ep[j], (j - i) / orows);
304 for (i = 0; i < orows; i++) {
305 for (j = 0; j < ocols; j++)
310 for (i = 0; i < ocols; i++)
312 if (!(flags & NOTRIMENDCOL)) {
313 if (flags & RIGHTADJUST)
314 colwidths[0] -= gutter;
316 colwidths[ocols - 1] = 0;
319 if (n > nelem && (flags & RECYCLE))
321 /*for (i = 0; i < ocols; i++)
322 warnx("%d is colwidths, nelem %d", colwidths[i], nelem);*/
326 char ibuf[BSIZE]; /* two screenfuls should do */
329 getline(void) /* get line; maintain curline, curlen; manage storage */
331 static int putlength;
332 static char *endblock = ibuf + BSIZE;
338 putlength = flags & DETAILSHAPE;
340 else if (skip <= 0) { /* don't waste storage */
341 curline += curlen + 1;
342 if (putlength) { /* print length, recycle storage */
343 printf(" %d line %d\n", curlen, irows);
347 if (!putlength && endblock - curline < BUFSIZ) { /* need storage */
348 /*ww = endblock-curline; tt += ww;*/
349 /*printf("#wasted %d total %d\n",ww,tt);*/
350 if (!(curline = (char *) malloc(BSIZE)))
351 errx(1, "file too large");
352 endblock = curline + BSIZE;
353 /*printf("#endb %d curline %d\n",endblock,curline);*/
356 for (p = curline, i = 1; i < BUFSIZ; *p++ = c, i++)
357 if ((c = getchar()) == EOF || c == '\n')
369 allocsize += allocsize;
370 p = (char **)realloc(elem, allocsize * sizeof(char *));
375 endelem = (elem = p) + allocsize;
380 getargs(int ac, char *av[])
385 flags |= NOARGS | TRANSPOSE;
387 while (--ac && **++av == '-')
388 for (p = *av+1; *p; p++)
395 case 'c': /* input col. separator */
396 flags |= ONEISEPONLY;
397 case 's': /* one or more allowed */
401 isep = '\t'; /* default is ^I */
404 flags |= ONEOSEPONLY;
409 osep = '\t'; /* default is ^I */
411 case 'w': /* window width, default 80 */
412 p = getnum(&owidth, p, 0);
414 errx(1, "width must be a positive integer");
416 case 'K': /* skip N lines */
418 case 'k': /* skip, do not print */
419 p = getnum(&skip, p, 0);
424 flags |= NOTRIMENDCOL;
426 case 'g': /* gutter space */
427 p = getnum(&gutter, p, 0);
430 p = getnum(&propgutter, p, 0);
432 case 'e': /* each line is an entry */
438 case 'j': /* right adjust */
439 flags |= RIGHTADJUST;
441 case 'n': /* null padding for missing values */
447 case 'H': /* print shape only */
448 flags |= DETAILSHAPE;
452 case 'z': /* squeeze col width */
456 ipagespace = atoi(++p); (default is 1)
458 case 'o': /* col order */
459 p = getlist(&cord, p);
463 p = getlist(&icbd, p);
467 p = getlist(&ocbd, p);
476 opages = atoi(av[2]);*/
484 errx(1, "too many arguments");
489 getlist(short **list, char *p)
494 for (t = p + 1; *t; t++) {
497 "option %.1s requires a list of unsigned numbers separated by commas", t);
499 while (*t && isdigit(*t))
504 if (!(*list = (short *) malloc(count * sizeof(short))))
505 errx(1, "no list space");
507 for (t = p + 1; *t; t++) {
508 (*list)[count++] = atoi(t);
509 printf("++ %d ", (*list)[count-1]);
511 while (*t && isdigit(*t))
521 * num = number p points to; if (strict) complain
522 * returns pointer to end of num
525 getnum(int *num, char *p, int strict)
529 if (!isdigit(*++t)) {
530 if (strict || *t == '-' || *t == '+')
531 errx(1, "option %.1s requires an unsigned integer", p);