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 * rs - reshape a data array
34 * Author: John Kunze, Office of Comp. Affairs, UCB
35 * BEWARE: lots of unfinished edges
48 #define TRANSPOSE 000001
49 #define MTRANSPOSE 000002
50 #define ONEPERLINE 000004
51 #define ONEISEPONLY 000010
52 #define ONEOSEPONLY 000020
53 #define NOTRIMENDCOL 000040
54 #define SQUEEZE 000100
55 #define SHAPEONLY 000200
56 #define DETAILSHAPE 000400
57 #define RIGHTADJUST 001000
58 #define NULLPAD 002000
59 #define RECYCLE 004000
60 #define SKIPPRINT 010000
61 #define ICOLBOUNDS 020000
62 #define OCOLBOUNDS 040000
63 #define ONEPERCHAR 0100000
64 #define NOARGS 0200000
66 static short *colwidths;
67 static std::vector<char *> elem;
70 static size_t irows, icols;
71 static size_t orows = 0, ocols = 0;
74 static int propgutter;
75 static char isep = ' ', osep = ' ';
76 static char blank[] = "";
77 static size_t owidth = 80, gutter = 2;
79 static void getargs(int, char *[]);
80 static void getfile(void);
81 static int get_line(void);
82 static long getnum(const char *);
83 static void prepfile(void);
84 static void prints(char *, int);
85 static void putfile(void);
86 static void usage(void);
89 main(int argc, char *argv[])
93 if (flags & SHAPEONLY) {
94 printf("%zu %zu\n", irows, icols);
108 int multisep = (flags & ONEISEPONLY ? 0 : 1);
109 int nullpad = flags & NULLPAD;
114 if (flags & SKIPPRINT)
120 if (flags & NOARGS && curlen < owidth)
122 if (flags & ONEPERLINE)
124 else /* count cols on first line */
125 for (p = curline, endp = curline + curlen; p < endp; p++) {
126 if (*p == isep && multisep)
129 while (*p && *p != isep)
133 if (flags & ONEPERLINE) {
134 elem.push_back(curline);
140 for (p = curline, endp = curline + curlen; p < endp; p++) {
141 if (*p == isep && multisep)
142 continue; /* eat up column separators */
143 if (*p == isep) /* must be an empty column */
144 elem.push_back(blank);
145 else /* store column entry */
148 while (p < endp && *p != isep)
149 p++; /* find end of entry */
150 *p = '\0'; /* mark end of entry */
152 if (maxlen < len) /* update maxlen */
155 irows++; /* update row count */
156 if (nullpad) { /* pad missing entries */
157 padto = irows * icols;
158 elem.resize(padto, blank);
160 } while (get_line() != EOF);
168 if (flags & TRANSPOSE)
169 for (i = 0; i < orows; i++) {
170 for (j = i; j < elem.size(); j += orows)
171 prints(elem[j], (j - i) / orows);
175 for (i = k = 0; i < orows; i++) {
176 for (j = 0; j < ocols; j++, k++)
184 prints(char *s, int col)
191 n = (flags & ONEOSEPONLY ? 1 : colwidths[col] - (p - s));
192 if (flags & RIGHTADJUST)
205 "usage: rs [-[csCS][x][kKgGw][N]tTeEnyjhHmz] [rows [cols]]\n");
213 size_t colw, max, n, orig_size, padto;
217 gutter += maxlen * propgutter / 100.0;
218 colw = maxlen + gutter;
219 if (flags & MTRANSPOSE) {
223 else if (orows == 0 && ocols == 0) { /* decide rows and cols */
224 ocols = owidth / colw;
226 warnx("display width %zu is less than column width %zu",
230 if (ocols > elem.size())
232 orows = elem.size() / ocols + (elem.size() % ocols ? 1 : 0);
234 else if (orows == 0) /* decide on rows */
235 orows = elem.size() / ocols + (elem.size() % ocols ? 1 : 0);
236 else if (ocols == 0) /* decide on cols */
237 ocols = elem.size() / orows + (elem.size() % orows ? 1 : 0);
238 padto = orows * ocols;
239 orig_size = elem.size();
240 if (flags & RECYCLE) {
241 for (i = 0; elem.size() < padto; i++)
242 elem.push_back(elem[i % orig_size]);
244 if (!(colwidths = (short *) malloc(ocols * sizeof(short))))
246 if (flags & SQUEEZE) {
247 if (flags & TRANSPOSE) {
248 auto it = elem.begin();
249 for (i = 0; i < ocols; i++) {
251 for (j = 0; it != elem.end() && j < orows; j++)
252 if ((n = strlen(*it++)) > max)
254 colwidths[i] = max + gutter;
257 for (i = 0; i < ocols; i++) {
259 for (j = i; j < elem.size(); j += ocols)
260 if ((n = strlen(elem[j])) > max)
262 colwidths[i] = max + gutter;
266 /* for (i = 0; i < orows; i++) {
267 for (j = i; j < elem.size(); j += orows)
268 prints(elem[j], (j - i) / orows);
272 auto it = elem.begin();
273 for (i = 0; i < orows; i++) {
274 for (j = 0; j < ocols; j++)
279 for (i = 0; i < ocols; i++)
281 if (!(flags & NOTRIMENDCOL)) {
282 if (flags & RIGHTADJUST)
283 colwidths[0] -= gutter;
285 colwidths[ocols - 1] = 0;
287 /*for (i = 0; i < ocols; i++)
288 warnx("%d is colwidths, nelem %zu", colwidths[i], elem.size());*/
291 #define BSIZE (LINE_MAX * 2)
292 static char ibuf[BSIZE];
295 get_line(void) /* get line; maintain curline, curlen; manage storage */
297 static int putlength;
298 static char *endblock = ibuf + BSIZE;
304 putlength = flags & DETAILSHAPE;
306 else if (skip <= 0) { /* don't waste storage */
307 curline += curlen + 1;
308 if (putlength) { /* print length, recycle storage */
309 printf(" %zu line %zu\n", curlen, irows);
313 if (!putlength && endblock - curline < LINE_MAX + 1) { /* need storage */
314 /*ww = endblock-curline; tt += ww;*/
315 /*printf("#wasted %d total %d\n",ww,tt);*/
316 if (!(curline = (char *) malloc(BSIZE)))
317 errx(1, "file too large");
318 endblock = curline + BSIZE;
319 /*printf("#endb %d curline %d\n",endblock,curline);*/
321 for (p = curline, i = 0;; *p++ = c, i++) {
322 if ((c = getchar()) == EOF)
325 errx(1, "maximum line length (%d) exceeded", LINE_MAX);
335 getargs(int ac, char *av[])
341 flags |= NOARGS | TRANSPOSE;
344 while ((ch = getopt(ac, av, "C::EG:HK:S::Tc::eg:hjk:mns::tw:yz")) != -1)
352 case 'c': /* input col. separator */
353 flags |= ONEISEPONLY;
355 case 's': /* one or more allowed */
359 isep = '\t'; /* default is ^I */
362 flags |= ONEOSEPONLY;
368 osep = '\t'; /* default is ^I */
370 case 'w': /* window width, default 80 */
371 val = getnum(optarg);
373 errx(1, "width must be a positive integer");
376 case 'K': /* skip N lines */
379 case 'k': /* skip, do not print */
380 skip = getnum(optarg);
385 flags |= NOTRIMENDCOL;
387 case 'g': /* gutter space */
388 gutter = getnum(optarg);
391 propgutter = getnum(optarg);
393 case 'e': /* each line is an entry */
399 case 'j': /* right adjust */
400 flags |= RIGHTADJUST;
402 case 'n': /* null padding for missing values */
408 case 'H': /* print shape only */
409 flags |= DETAILSHAPE;
414 case 'z': /* squeeze col width */
418 ipagespace = atoi(optarg); (default is 1)
432 opages = atoi(av[2]);
436 val = strtol(av[1], NULL, 10);
441 val = strtol(av[0], NULL, 10);
448 errx(1, "too many arguments");
453 getnum(const char *p)
458 val = strtol(p, &ep, 10);
460 errx(1, "invalid integer %s", p);