1 /*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (c) 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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.
18 *
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
29 * SUCH DAMAGE.
30 */
31
32 #ifndef lint
33 static const char copyright[] =
34 "@(#) Copyright (c) 1993\n\
35 The Regents of the University of California. All rights reserved.\n";
36 #endif /* not lint */
37
38 #ifndef lint
39 static const char sccsid[] = "@(#)rs.c 8.1 (Berkeley) 6/6/93";
40 #endif /* not lint */
41
42 /*
43 * rs - reshape a data array
44 * Author: John Kunze, Office of Comp. Affairs, UCB
45 * BEWARE: lots of unfinished edges
46 */
47
48 #include <sys/cdefs.h>
49 __FBSDID("$FreeBSD$");
50
51 #include <err.h>
52 #include <ctype.h>
53 #include <limits.h>
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <string.h>
57
58 static long flags;
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
76
77 static short *colwidths;
78 static short *cord;
79 static short *icbd;
80 static short *ocbd;
81 static int nelem;
82 static char **elem;
83 static char **endelem;
84 static char *curline;
85 static int allocsize = BUFSIZ;
86 static int curlen;
87 static int irows, icols;
88 static int orows = 0, ocols = 0;
89 static int maxlen;
90 static int skip;
91 static int propgutter;
92 static char isep = ' ', osep = ' ';
93 static char blank[] = "";
94 static int owidth = 80, gutter = 2;
95
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);
106
107 #define INCR(ep) do { \
108 if (++ep >= endelem) \
109 ep = getptrs(ep); \
110 } while(0)
111
112 int
main(int argc,char * argv[])113 main(int argc, char *argv[])
114 {
115 getargs(argc, argv);
116 getfile();
117 if (flags & SHAPEONLY) {
118 printf("%d %d\n", irows, icols);
119 exit(0);
120 }
121 prepfile();
122 putfile();
123 exit(0);
124 }
125
126 static void
getfile(void)127 getfile(void)
128 {
129 char *p;
130 char *endp;
131 char **ep;
132 int c;
133 int multisep = (flags & ONEISEPONLY ? 0 : 1);
134 int nullpad = flags & NULLPAD;
135 char **padto;
136
137 while (skip--) {
138 c = get_line();
139 if (flags & SKIPPRINT)
140 puts(curline);
141 if (c == EOF)
142 return;
143 }
144 get_line();
145 if (flags & NOARGS && curlen < owidth)
146 flags |= ONEPERLINE;
147 if (flags & ONEPERLINE)
148 icols = 1;
149 else /* count cols on first line */
150 for (p = curline, endp = curline + curlen; p < endp; p++) {
151 if (*p == isep && multisep)
152 continue;
153 icols++;
154 while (*p && *p != isep)
155 p++;
156 }
157 ep = getptrs(elem);
158 do {
159 if (flags & ONEPERLINE) {
160 *ep = curline;
161 INCR(ep); /* prepare for next entry */
162 if (maxlen < curlen)
163 maxlen = curlen;
164 irows++;
165 continue;
166 }
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 */
171 *ep = blank;
172 else /* store column entry */
173 *ep = p;
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 */
178 maxlen = p - *ep;
179 INCR(ep); /* prepare for next entry */
180 }
181 irows++; /* update row count */
182 if (nullpad) { /* pad missing entries */
183 padto = elem + irows * icols;
184 while (ep < padto) {
185 *ep = blank;
186 INCR(ep);
187 }
188 }
189 } while (get_line() != EOF);
190 *ep = 0; /* mark end of pointers */
191 nelem = ep - elem;
192 }
193
194 static void
putfile(void)195 putfile(void)
196 {
197 char **ep;
198 int i, j, k;
199
200 ep = elem;
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);
205 putchar('\n');
206 }
207 else
208 for (i = k = 0; i < orows; i++) {
209 for (j = 0; j < ocols; j++, k++)
210 if (k < nelem)
211 prints(ep[k], j);
212 putchar('\n');
213 }
214 }
215
216 static void
prints(char * s,int col)217 prints(char *s, int col)
218 {
219 int n;
220 char *p = s;
221
222 while (*p)
223 p++;
224 n = (flags & ONEOSEPONLY ? 1 : colwidths[col] - (p - s));
225 if (flags & RIGHTADJUST)
226 while (n-- > 0)
227 putchar(osep);
228 for (p = s; *p; p++)
229 putchar(*p);
230 while (n-- > 0)
231 putchar(osep);
232 }
233
234 static void
usage(void)235 usage(void)
236 {
237 fprintf(stderr,
238 "usage: rs [-[csCS][x][kKgGw][N]tTeEnyjhHmz] [rows [cols]]\n");
239 exit(1);
240 }
241
242 static void
prepfile(void)243 prepfile(void)
244 {
245 char **ep;
246 int i;
247 int j;
248 char **lp;
249 int colw;
250 int max;
251 int n;
252
253 if (!nelem)
254 exit(0);
255 gutter += maxlen * propgutter / 100.0;
256 colw = maxlen + gutter;
257 if (flags & MTRANSPOSE) {
258 orows = icols;
259 ocols = irows;
260 }
261 else if (orows == 0 && ocols == 0) { /* decide rows and cols */
262 ocols = owidth / colw;
263 if (ocols == 0) {
264 warnx("display width %d is less than column width %d",
265 owidth, colw);
266 ocols = 1;
267 }
268 if (ocols > nelem)
269 ocols = nelem;
270 orows = nelem / ocols + (nelem % ocols ? 1 : 0);
271 }
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;
280 }
281 if (flags & RECYCLE) {
282 for (ep = elem + nelem; ep < lp; ep++)
283 *ep = *(ep - nelem);
284 nelem = lp - elem;
285 }
286 if (!(colwidths = (short *) malloc(ocols * sizeof(short))))
287 errx(1, "malloc");
288 if (flags & SQUEEZE) {
289 ep = elem;
290 if (flags & TRANSPOSE)
291 for (i = 0; i < ocols; i++) {
292 max = 0;
293 for (j = 0; *ep != NULL && j < orows; j++)
294 if ((n = strlen(*ep++)) > max)
295 max = n;
296 colwidths[i] = max + gutter;
297 }
298 else
299 for (i = 0; i < ocols; i++) {
300 max = 0;
301 for (j = i; j < nelem; j += ocols)
302 if ((n = strlen(ep[j])) > max)
303 max = n;
304 colwidths[i] = max + gutter;
305 }
306 }
307 /* for (i = 0; i < orows; i++) {
308 for (j = i; j < nelem; j += orows)
309 prints(ep[j], (j - i) / orows);
310 putchar('\n');
311 }
312 else
313 for (i = 0; i < orows; i++) {
314 for (j = 0; j < ocols; j++)
315 prints(*ep++, j);
316 putchar('\n');
317 }*/
318 else
319 for (i = 0; i < ocols; i++)
320 colwidths[i] = colw;
321 if (!(flags & NOTRIMENDCOL)) {
322 if (flags & RIGHTADJUST)
323 colwidths[0] -= gutter;
324 else
325 colwidths[ocols - 1] = 0;
326 }
327 n = orows * ocols;
328 if (n > nelem && (flags & RECYCLE))
329 nelem = n;
330 /*for (i = 0; i < ocols; i++)
331 warnx("%d is colwidths, nelem %d", colwidths[i], nelem);*/
332 }
333
334 #define BSIZE (LINE_MAX * 2)
335 static char ibuf[BSIZE];
336
337 static int
get_line(void)338 get_line(void) /* get line; maintain curline, curlen; manage storage */
339 {
340 static int putlength;
341 static char *endblock = ibuf + BSIZE;
342 char *p;
343 int c, i;
344
345 if (!irows) {
346 curline = ibuf;
347 putlength = flags & DETAILSHAPE;
348 }
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);
353 curline = ibuf;
354 }
355 }
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);*/
363 }
364 for (p = curline, i = 0;; *p++ = c, i++) {
365 if ((c = getchar()) == EOF)
366 break;
367 if (i >= LINE_MAX)
368 errx(1, "maximum line length (%d) exceeded", LINE_MAX);
369 if (c == '\n')
370 break;
371 }
372 *p = '\0';
373 curlen = i;
374 return(c);
375 }
376
377 static char **
getptrs(char ** sp)378 getptrs(char **sp)
379 {
380 char **p;
381
382 allocsize += allocsize;
383 p = (char **)realloc(elem, allocsize * sizeof(char *));
384 if (p == NULL)
385 err(1, "no memory");
386
387 sp += (p - elem);
388 endelem = (elem = p) + allocsize;
389 return(sp);
390 }
391
392 static void
getargs(int ac,char * av[])393 getargs(int ac, char *av[])
394 {
395 char *p;
396
397 if (ac == 1) {
398 flags |= NOARGS | TRANSPOSE;
399 }
400 while (--ac && **++av == '-')
401 for (p = *av+1; *p; p++)
402 switch (*p) {
403 case 'T':
404 flags |= MTRANSPOSE;
405 case 't':
406 flags |= TRANSPOSE;
407 break;
408 case 'c': /* input col. separator */
409 flags |= ONEISEPONLY;
410 case 's': /* one or more allowed */
411 if (p[1])
412 isep = *++p;
413 else
414 isep = '\t'; /* default is ^I */
415 break;
416 case 'C':
417 flags |= ONEOSEPONLY;
418 case 'S':
419 if (p[1])
420 osep = *++p;
421 else
422 osep = '\t'; /* default is ^I */
423 break;
424 case 'w': /* window width, default 80 */
425 p = getnum(&owidth, p, 0);
426 if (owidth <= 0)
427 errx(1, "width must be a positive integer");
428 break;
429 case 'K': /* skip N lines */
430 flags |= SKIPPRINT;
431 case 'k': /* skip, do not print */
432 p = getnum(&skip, p, 0);
433 if (!skip)
434 skip = 1;
435 break;
436 case 'm':
437 flags |= NOTRIMENDCOL;
438 break;
439 case 'g': /* gutter space */
440 p = getnum(&gutter, p, 0);
441 break;
442 case 'G':
443 p = getnum(&propgutter, p, 0);
444 break;
445 case 'e': /* each line is an entry */
446 flags |= ONEPERLINE;
447 break;
448 case 'E':
449 flags |= ONEPERCHAR;
450 break;
451 case 'j': /* right adjust */
452 flags |= RIGHTADJUST;
453 break;
454 case 'n': /* null padding for missing values */
455 flags |= NULLPAD;
456 break;
457 case 'y':
458 flags |= RECYCLE;
459 break;
460 case 'H': /* print shape only */
461 flags |= DETAILSHAPE;
462 case 'h':
463 flags |= SHAPEONLY;
464 break;
465 case 'z': /* squeeze col width */
466 flags |= SQUEEZE;
467 break;
468 /*case 'p':
469 ipagespace = atoi(++p); (default is 1)
470 break;*/
471 case 'o': /* col order */
472 p = getlist(&cord, p);
473 break;
474 case 'b':
475 flags |= ICOLBOUNDS;
476 p = getlist(&icbd, p);
477 break;
478 case 'B':
479 flags |= OCOLBOUNDS;
480 p = getlist(&ocbd, p);
481 break;
482 default:
483 usage();
484 }
485 /*if (!osep)
486 osep = isep;*/
487 switch (ac) {
488 /*case 3:
489 opages = atoi(av[2]);*/
490 case 2:
491 if ((ocols = atoi(av[1])) < 0)
492 ocols = 0;
493 case 1:
494 if ((orows = atoi(av[0])) < 0)
495 orows = 0;
496 case 0:
497 break;
498 default:
499 errx(1, "too many arguments");
500 }
501 }
502
503 static char *
getlist(short ** list,char * p)504 getlist(short **list, char *p)
505 {
506 int count = 1;
507 char *t;
508
509 for (t = p + 1; *t; t++) {
510 if (!isdigit((unsigned char)*t))
511 errx(1,
512 "option %.1s requires a list of unsigned numbers separated by commas", t);
513 count++;
514 while (*t && isdigit((unsigned char)*t))
515 t++;
516 if (*t != ',')
517 break;
518 }
519 if (!(*list = (short *) malloc(count * sizeof(short))))
520 errx(1, "no list space");
521 count = 0;
522 for (t = p + 1; *t; t++) {
523 (*list)[count++] = atoi(t);
524 printf("++ %d ", (*list)[count-1]);
525 fflush(stdout);
526 while (*t && isdigit((unsigned char)*t))
527 t++;
528 if (*t != ',')
529 break;
530 }
531 (*list)[count] = 0;
532 return(t - 1);
533 }
534
535 /*
536 * num = number p points to; if (strict) complain
537 * returns pointer to end of num
538 */
539 static char *
getnum(int * num,char * p,int strict)540 getnum(int *num, char *p, int strict)
541 {
542 char *t = p;
543
544 if (!isdigit((unsigned char)*++t)) {
545 if (strict || *t == '-' || *t == '+')
546 errx(1, "option %.1s requires an unsigned integer", p);
547 *num = 0;
548 return(p);
549 }
550 *num = atoi(t);
551 while (*++t)
552 if (!isdigit((unsigned char)*t))
553 break;
554 return(--t);
555 }
556