2 * SPDX-License-Identifier: BSD-3-Clause
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 static const char copyright[] =
34 "@(#) Copyright (c) 1993\n\
35 The Regents of the University of California. All rights reserved.\n";
39 static const char sccsid[] = "@(#)rs.c 8.1 (Berkeley) 6/6/93";
43 * rs - reshape a data array
44 * Author: John Kunze, Office of Comp. Affairs, UCB
45 * BEWARE: lots of unfinished edges
48 #include <sys/cdefs.h>
49 __FBSDID("$FreeBSD$");
59 #define TRANSPOSE 000001
60 #define MTRANSPOSE 000002
61 #define ONEPERLINE 000004
62 #define ONEISEPONLY 000010
63 #define ONEOSEPONLY 000020
64 #define NOTRIMENDCOL 000040
65 #define SQUEEZE 000100
66 #define SHAPEONLY 000200
67 #define DETAILSHAPE 000400
68 #define RIGHTADJUST 001000
69 #define NULLPAD 002000
70 #define RECYCLE 004000
71 #define SKIPPRINT 010000
72 #define ICOLBOUNDS 020000
73 #define OCOLBOUNDS 040000
74 #define ONEPERCHAR 0100000
75 #define NOARGS 0200000
77 static short *colwidths;
83 static char **endelem;
85 static int allocsize = BUFSIZ;
87 static int irows, icols;
88 static int orows = 0, ocols = 0;
91 static int propgutter;
92 static char isep = ' ', osep = ' ';
93 static char blank[] = "";
94 static int owidth = 80, gutter = 2;
96 static void getargs(int, char *[]);
97 static void getfile(void);
98 static int get_line(void);
99 static char *getlist(short **, char *);
100 static char *getnum(int *, char *, int);
101 static char **getptrs(char **);
102 static void prepfile(void);
103 static void prints(char *, int);
104 static void putfile(void);
105 static void usage(void);
107 #define INCR(ep) do { \
108 if (++ep >= endelem) \
113 main(int argc, char *argv[])
117 if (flags & SHAPEONLY) {
118 printf("%d %d\n", irows, icols);
133 int multisep = (flags & ONEISEPONLY ? 0 : 1);
134 int nullpad = flags & NULLPAD;
139 if (flags & SKIPPRINT)
145 if (flags & NOARGS && curlen < owidth)
147 if (flags & ONEPERLINE)
149 else /* count cols on first line */
150 for (p = curline, endp = curline + curlen; p < endp; p++) {
151 if (*p == isep && multisep)
154 while (*p && *p != isep)
159 if (flags & ONEPERLINE) {
161 INCR(ep); /* prepare for next entry */
167 for (p = curline, endp = curline + curlen; p < endp; p++) {
168 if (*p == isep && multisep)
169 continue; /* eat up column separators */
170 if (*p == isep) /* must be an empty column */
172 else /* store column entry */
174 while (p < endp && *p != isep)
175 p++; /* find end of entry */
176 *p = '\0'; /* mark end of entry */
177 if (maxlen < p - *ep) /* update maxlen */
179 INCR(ep); /* prepare for next entry */
181 irows++; /* update row count */
182 if (nullpad) { /* pad missing entries */
183 padto = elem + irows * icols;
189 } while (get_line() != EOF);
190 *ep = 0; /* mark end of pointers */
201 if (flags & TRANSPOSE)
202 for (i = 0; i < orows; i++) {
203 for (j = i; j < nelem; j += orows)
204 prints(ep[j], (j - i) / orows);
208 for (i = k = 0; i < orows; i++) {
209 for (j = 0; j < ocols; j++, k++)
217 prints(char *s, int col)
224 n = (flags & ONEOSEPONLY ? 1 : colwidths[col] - (p - s));
225 if (flags & RIGHTADJUST)
238 "usage: rs [-[csCS][x][kKgGw][N]tTeEnyjhHmz] [rows [cols]]\n");
255 gutter += maxlen * propgutter / 100.0;
256 colw = maxlen + gutter;
257 if (flags & MTRANSPOSE) {
261 else if (orows == 0 && ocols == 0) { /* decide rows and cols */
262 ocols = owidth / colw;
264 warnx("display width %d is less than column width %d",
270 orows = nelem / ocols + (nelem % ocols ? 1 : 0);
272 else if (orows == 0) /* decide on rows */
273 orows = nelem / ocols + (nelem % ocols ? 1 : 0);
274 else if (ocols == 0) /* decide on cols */
275 ocols = nelem / orows + (nelem % orows ? 1 : 0);
276 lp = elem + orows * ocols;
277 while (lp > endelem) {
278 getptrs(elem + nelem);
279 lp = elem + orows * ocols;
281 if (flags & RECYCLE) {
282 for (ep = elem + nelem; ep < lp; ep++)
286 if (!(colwidths = (short *) malloc(ocols * sizeof(short))))
288 if (flags & SQUEEZE) {
290 if (flags & TRANSPOSE)
291 for (i = 0; i < ocols; i++) {
293 for (j = 0; *ep != NULL && j < orows; j++)
294 if ((n = strlen(*ep++)) > max)
296 colwidths[i] = max + gutter;
299 for (i = 0; i < ocols; i++) {
301 for (j = i; j < nelem; j += ocols)
302 if ((n = strlen(ep[j])) > max)
304 colwidths[i] = max + gutter;
307 /* for (i = 0; i < orows; i++) {
308 for (j = i; j < nelem; j += orows)
309 prints(ep[j], (j - i) / orows);
313 for (i = 0; i < orows; i++) {
314 for (j = 0; j < ocols; j++)
319 for (i = 0; i < ocols; i++)
321 if (!(flags & NOTRIMENDCOL)) {
322 if (flags & RIGHTADJUST)
323 colwidths[0] -= gutter;
325 colwidths[ocols - 1] = 0;
328 if (n > nelem && (flags & RECYCLE))
330 /*for (i = 0; i < ocols; i++)
331 warnx("%d is colwidths, nelem %d", colwidths[i], nelem);*/
334 #define BSIZE (LINE_MAX * 2)
335 static char ibuf[BSIZE];
338 get_line(void) /* get line; maintain curline, curlen; manage storage */
340 static int putlength;
341 static char *endblock = ibuf + BSIZE;
347 putlength = flags & DETAILSHAPE;
349 else if (skip <= 0) { /* don't waste storage */
350 curline += curlen + 1;
351 if (putlength) { /* print length, recycle storage */
352 printf(" %d line %d\n", curlen, irows);
356 if (!putlength && endblock - curline < LINE_MAX + 1) { /* need storage */
357 /*ww = endblock-curline; tt += ww;*/
358 /*printf("#wasted %d total %d\n",ww,tt);*/
359 if (!(curline = (char *) malloc(BSIZE)))
360 errx(1, "file too large");
361 endblock = curline + BSIZE;
362 /*printf("#endb %d curline %d\n",endblock,curline);*/
364 for (p = curline, i = 0;; *p++ = c, i++) {
365 if ((c = getchar()) == EOF)
368 errx(1, "maximum line length (%d) exceeded", LINE_MAX);
382 allocsize += allocsize;
383 p = (char **)realloc(elem, allocsize * sizeof(char *));
388 endelem = (elem = p) + allocsize;
393 getargs(int ac, char *av[])
398 flags |= NOARGS | TRANSPOSE;
400 while (--ac && **++av == '-')
401 for (p = *av+1; *p; p++)
408 case 'c': /* input col. separator */
409 flags |= ONEISEPONLY;
410 case 's': /* one or more allowed */
414 isep = '\t'; /* default is ^I */
417 flags |= ONEOSEPONLY;
422 osep = '\t'; /* default is ^I */
424 case 'w': /* window width, default 80 */
425 p = getnum(&owidth, p, 0);
427 errx(1, "width must be a positive integer");
429 case 'K': /* skip N lines */
431 case 'k': /* skip, do not print */
432 p = getnum(&skip, p, 0);
437 flags |= NOTRIMENDCOL;
439 case 'g': /* gutter space */
440 p = getnum(&gutter, p, 0);
443 p = getnum(&propgutter, p, 0);
445 case 'e': /* each line is an entry */
451 case 'j': /* right adjust */
452 flags |= RIGHTADJUST;
454 case 'n': /* null padding for missing values */
460 case 'H': /* print shape only */
461 flags |= DETAILSHAPE;
465 case 'z': /* squeeze col width */
469 ipagespace = atoi(++p); (default is 1)
471 case 'o': /* col order */
472 p = getlist(&cord, p);
476 p = getlist(&icbd, p);
480 p = getlist(&ocbd, p);
489 opages = atoi(av[2]);*/
491 if ((ocols = atoi(av[1])) < 0)
494 if ((orows = atoi(av[0])) < 0)
499 errx(1, "too many arguments");
504 getlist(short **list, char *p)
509 for (t = p + 1; *t; t++) {
510 if (!isdigit((unsigned char)*t))
512 "option %.1s requires a list of unsigned numbers separated by commas", t);
514 while (*t && isdigit((unsigned char)*t))
519 if (!(*list = (short *) malloc(count * sizeof(short))))
520 errx(1, "no list space");
522 for (t = p + 1; *t; t++) {
523 (*list)[count++] = atoi(t);
524 printf("++ %d ", (*list)[count-1]);
526 while (*t && isdigit((unsigned char)*t))
536 * num = number p points to; if (strict) complain
537 * returns pointer to end of num
540 getnum(int *num, char *p, int strict)
544 if (!isdigit((unsigned char)*++t)) {
545 if (strict || *t == '-' || *t == '+')
546 errx(1, "option %.1s requires an unsigned integer", p);
552 if (!isdigit((unsigned char)*t))