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
73 int allocsize
= BUFSIZ
;
80 char isep
= ' ', osep
= ' ';
82 int owidth
= 80, gutter
= 2;
84 void getargs(int, char *[]);
87 char *getlist(short **, char *);
88 char *getnum(int *, char *, int);
89 char **getptrs(char **);
91 void prints(char *, int);
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 (getline() != 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 char ibuf
[BSIZE
]; /* two screenfuls should do */
324 getline(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
++)
390 case 'c': /* input col. separator */
391 flags
|= ONEISEPONLY
;
392 case 's': /* one or more allowed */
396 isep
= '\t'; /* default is ^I */
399 flags
|= ONEOSEPONLY
;
404 osep
= '\t'; /* default is ^I */
406 case 'w': /* window width, default 80 */
407 p
= getnum(&owidth
, p
, 0);
409 errx(1, "width must be a positive integer");
411 case 'K': /* skip N lines */
413 case 'k': /* skip, do not print */
414 p
= getnum(&skip
, p
, 0);
419 flags
|= NOTRIMENDCOL
;
421 case 'g': /* gutter space */
422 p
= getnum(&gutter
, p
, 0);
425 p
= getnum(&propgutter
, p
, 0);
427 case 'e': /* each line is an entry */
433 case 'j': /* right adjust */
434 flags
|= RIGHTADJUST
;
436 case 'n': /* null padding for missing values */
442 case 'H': /* print shape only */
443 flags
|= DETAILSHAPE
;
447 case 'z': /* squeeze col width */
451 ipagespace = atoi(++p); (default is 1)
453 case 'o': /* col order */
454 p
= getlist(&cord
, p
);
458 p
= getlist(&icbd
, p
);
462 p
= getlist(&ocbd
, p
);
471 opages = atoi(av[2]);*/
479 errx(1, "too many arguments");
484 getlist(short **list
, char *p
)
489 for (t
= p
+ 1; *t
; t
++) {
492 "option %.1s requires a list of unsigned numbers separated by commas", t
);
494 while (*t
&& isdigit(*t
))
499 if (!(*list
= (short *) malloc(count
* sizeof(short))))
500 errx(1, "no list space");
502 for (t
= p
+ 1; *t
; t
++) {
503 (*list
)[count
++] = atoi(t
);
504 printf("++ %d ", (*list
)[count
-1]);
506 while (*t
&& isdigit(*t
))
516 * num = number p points to; if (strict) complain
517 * returns pointer to end of num
520 getnum(int *num
, char *p
, int strict
)
524 if (!isdigit(*++t
)) {
525 if (strict
|| *t
== '-' || *t
== '+')
526 errx(1, "option %.1s requires an unsigned integer", p
);