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. 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
29 * @(#) Copyright (c) 1993 The Regents of the University of California. All rights reserved.
30 * @(#)rs.c 8.1 (Berkeley) 6/6/93
31 * $FreeBSD: src/usr.bin/rs/rs.c,v 1.5.2.2 2002/08/03 00:48:43 tjr Exp $
35 * rs - reshape a data array
36 * Author: John Kunze, Office of Comp. Affairs, UCB
37 * BEWARE: lots of unfinished edges
47 #define TRANSPOSE 000001
48 #define MTRANSPOSE 000002
49 #define ONEPERLINE 000004
50 #define ONEISEPONLY 000010
51 #define ONEOSEPONLY 000020
52 #define NOTRIMENDCOL 000040
53 #define SQUEEZE 000100
54 #define SHAPEONLY 000200
55 #define DETAILSHAPE 000400
56 #define RIGHTADJUST 001000
57 #define NULLPAD 002000
58 #define RECYCLE 004000
59 #define SKIPPRINT 010000
60 #define ICOLBOUNDS 020000
61 #define OCOLBOUNDS 040000
62 #define ONEPERCHAR 0100000
63 #define NOARGS 0200000
65 static short *colwidths;
71 static char **endelem;
73 static int allocsize = BUFSIZ;
75 static int irows, icols;
76 static int orows, ocols;
79 static int propgutter;
80 static char isep = ' ', osep = ' ';
81 static char blank[] = "";
82 static int owidth = 80, gutter = 2;
84 static void getargs(int, char *[]);
85 static void getfile(void);
86 static int get_line(void);
87 static char *getlist(short **, char *);
88 static char *getnum(int *, char *, int);
89 static char **getptrs(char **);
90 static void prepfile(void);
91 static void prints(char *, int);
92 static void putfile(void);
93 static void usage(void);
95 #define INCR(ep) do { \
96 if (++ep >= endelem) \
101 main(int argc, char *argv[])
105 if (flags & SHAPEONLY) {
106 printf("%d %d\n", irows, icols);
120 int multisep = (flags & ONEISEPONLY ? 0 : 1);
121 int nullpad = flags & NULLPAD;
126 if (flags & SKIPPRINT)
130 if (flags & NOARGS && curlen < owidth)
132 if (flags & ONEPERLINE)
134 else /* count cols on first line */
135 for (p = curline, endp = curline + curlen; p < endp; p++) {
136 if (*p == isep && multisep)
139 while (*p && *p != isep)
145 if (flags & ONEPERLINE) {
147 INCR(ep); /* prepare for next entry */
153 for (p = curline, endp = curline + curlen; p < endp; p++) {
154 if (*p == isep && multisep)
155 continue; /* eat up column separators */
156 if (*p == isep) /* must be an empty column */
158 else /* store column entry */
160 while (p < endp && *p != isep)
161 p++; /* find end of entry */
162 *p = '\0'; /* mark end of entry */
163 if (maxlen < p - *ep) /* update maxlen */
165 INCR(ep); /* prepare for next entry */
167 irows++; /* update row count */
168 if (nullpad) { /* pad missing entries */
169 padto = elem + irows * icols;
175 } while (get_line() != EOF);
176 *ep = NULL; /* mark end of pointers */
187 if (flags & TRANSPOSE)
188 for (i = 0; i < orows; i++) {
189 for (j = i; j < nelem; j += orows)
190 prints(ep[j], (j - i) / orows);
194 for (i = k = 0; i < orows; i++) {
195 for (j = 0; j < ocols; j++, k++)
203 prints(char *s, int col)
210 n = (flags & ONEOSEPONLY ? 1 : colwidths[col] - (p - s));
211 if (flags & RIGHTADJUST)
224 "usage: rs [-[csCS][x][kKgGw][N]tTeEnyjhHmz] [rows [cols]]\n");
241 gutter += maxlen * propgutter / 100.0;
242 colw = maxlen + gutter;
243 if (flags & MTRANSPOSE) {
247 else if (orows == 0 && ocols == 0) { /* decide rows and cols */
248 ocols = owidth / colw;
250 warnx("display width %d is less than column width %d",
256 orows = nelem / ocols + (nelem % ocols ? 1 : 0);
258 else if (orows == 0) /* decide on rows */
259 orows = nelem / ocols + (nelem % ocols ? 1 : 0);
260 else if (ocols == 0) /* decide on cols */
261 ocols = nelem / orows + (nelem % orows ? 1 : 0);
262 lp = elem + orows * ocols;
263 while (lp > endelem) {
264 getptrs(elem + nelem);
265 lp = elem + orows * ocols;
267 if (flags & RECYCLE) {
268 for (ep = elem + nelem; ep < lp; ep++)
272 if (!(colwidths = (short *) malloc(ocols * sizeof(short))))
274 if (flags & SQUEEZE) {
276 if (flags & TRANSPOSE)
277 for (i = 0; i < ocols; i++) {
279 for (j = 0; *ep != NULL && j < orows; j++)
280 if ((n = strlen(*ep++)) > max)
282 colwidths[i] = max + gutter;
285 for (i = 0; i < ocols; i++) {
287 for (j = i; j < nelem; j += ocols)
288 if ((n = strlen(ep[j])) > max)
290 colwidths[i] = max + gutter;
293 /* for (i = 0; i < orows; i++) {
294 for (j = i; j < nelem; j += orows)
295 prints(ep[j], (j - i) / orows);
299 for (i = 0; i < orows; i++) {
300 for (j = 0; j < ocols; j++)
305 for (i = 0; i < ocols; i++)
307 if (!(flags & NOTRIMENDCOL)) {
308 if (flags & RIGHTADJUST)
309 colwidths[0] -= gutter;
311 colwidths[ocols - 1] = 0;
314 if (n > nelem && (flags & RECYCLE))
316 /*for (i = 0; i < ocols; i++)
317 warnx("%d is colwidths, nelem %d", colwidths[i], nelem);*/
321 static char ibuf[BSIZE]; /* two screenfuls should do */
324 get_line(void) /* get line; maintain curline, curlen; manage storage */
326 static int putlength;
327 static char *endblock = ibuf + BSIZE;
333 putlength = flags & DETAILSHAPE;
335 else if (skip <= 0) { /* don't waste storage */
336 curline += curlen + 1;
337 if (putlength) { /* print length, recycle storage */
338 printf(" %d line %d\n", curlen, irows);
342 if (!putlength && endblock - curline < BUFSIZ) { /* need storage */
343 /*ww = endblock-curline; tt += ww;*/
344 /*printf("#wasted %d total %d\n",ww,tt);*/
345 if (!(curline = (char *) malloc(BSIZE)))
346 errx(1, "file too large");
347 endblock = curline + BSIZE;
348 /*printf("#endb %d curline %d\n",endblock,curline);*/
351 for (p = curline, i = 1; i < BUFSIZ; *p++ = c, i++)
352 if ((c = getchar()) == EOF || c == '\n')
364 allocsize += allocsize;
365 p = (char **)realloc(elem, allocsize * sizeof(char *));
370 endelem = (elem = p) + allocsize;
375 getargs(int ac, char *av[])
380 flags |= NOARGS | TRANSPOSE;
382 while (--ac && **++av == '-')
383 for (p = *av+1; *p; p++)
391 case 'c': /* input col. separator */
392 flags |= ONEISEPONLY;
394 case 's': /* one or more allowed */
398 isep = '\t'; /* default is ^I */
401 flags |= ONEOSEPONLY;
407 osep = '\t'; /* default is ^I */
409 case 'w': /* window width, default 80 */
410 p = getnum(&owidth, p, 0);
412 errx(1, "width must be a positive integer");
414 case 'K': /* skip N lines */
417 case 'k': /* skip, do not print */
418 p = getnum(&skip, p, 0);
423 flags |= NOTRIMENDCOL;
425 case 'g': /* gutter space */
426 p = getnum(&gutter, p, 0);
429 p = getnum(&propgutter, p, 0);
431 case 'e': /* each line is an entry */
437 case 'j': /* right adjust */
438 flags |= RIGHTADJUST;
440 case 'n': /* null padding for missing values */
446 case 'H': /* print shape only */
447 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]);*/
486 errx(1, "too many arguments");
491 getlist(short **list, char *p)
496 for (t = p + 1; *t; t++) {
499 "option %.1s requires a list of unsigned numbers separated by commas", t);
501 while (*t && isdigit(*t))
506 if (!(*list = (short *) malloc(count * sizeof(short))))
507 errx(1, "no list space");
509 for (t = p + 1; *t; t++) {
510 (*list)[count++] = atoi(t);
511 printf("++ %d ", (*list)[count-1]);
513 while (*t && isdigit(*t))
523 * num = number p points to; if (strict) complain
524 * returns pointer to end of num
527 getnum(int *num, char *p, int strict)
531 if (!isdigit(*++t)) {
532 if (strict || *t == '-' || *t == '+')
533 errx(1, "option %.1s requires an unsigned integer", p);