/* Copyright (c) 1983 Regents of the University of California */
static char sccsid
[] = "@(#)rs.c 4.2 (Berkeley) %G%";
* rs - reshape a data array
* Author: John Kunze, Office of Comp. Affairs, UCB
* BEWARE: lots of unfinished edges
#define MTRANSPOSE 000002
#define ONEPERLINE 000004
#define ONEISEPONLY 000010
#define ONEOSEPONLY 000020
#define NOTRIMENDCOL 000040
#define DETAILSHAPE 000400
#define RIGHTADJUST 001000
#define ICOLBOUNDS 020000
#define OCOLBOUNDS 040000
#define ONEPERCHAR 0100000
char isep
= ' ', osep
= ' ';
int owidth
= 80, gutter
= 2;
printf("%d %d\n", irows
, icols
);
/*fprintf(stderr, "#irows %d icols %d orows %d ocols %d\n",irows,icols,orows,ocols);*/
int multisep
= (flags
& ONEISEPONLY
? 0 : 1);
int nullpad
= flags
& NULLPAD
;
if (flags
& NOARGS
&& curlen
< owidth
)
else /* count cols on first line */
for (p
= curline
, endp
= curline
+ curlen
; p
< endp
; p
++) {
if (*p
== isep
&& multisep
)
if (flags
& ONEPERLINE
) {
for (p
= curline
, endp
= curline
+ curlen
; p
< endp
; p
++) {
if (*p
== isep
&& multisep
)
continue; /* eat up column separators */
if (*p
== isep
) /* must be an empty column */
else /* store column entry */
while (p
< endp
&& *p
!= isep
)
p
++; /* find end of entry */
*p
= '\0'; /* mark end of entry */
if (maxlen
< p
- *ep
) /* update maxlen */
ep
++; /* prepare for next entry */
irows
++; /* update row count */
if (nullpad
) { /* pad missing entries */
padto
= elem
+ irows
* icols
;
if (ep
> endelem
) /* if low on pointers */
ep
= getptrs(ep
); /* get some more */
} while (getline() != EOF
);
*ep
= 0; /* mark end of pointers */
for (i
= 0; i
< orows
; i
++) {
for (j
= i
; j
< nelem
; j
+= orows
)
prints(ep
[j
], (j
- i
) / orows
);
for (i
= 0; i
< orows
; i
++) {
for (j
= 0; j
< ocols
; j
++)
n
= (flags
& ONEOSEPONLY
? 1 : colwidths
[col
] - (p
- s
));
fprintf(stderr
, "\nUsage: rs [ -[csCS][x][kKgGw][N]tTeEnyjhHm ] [ rows [ cols ] ]\n");
gutter
+= maxlen
* propgutter
/ 100.0;
if (flags
& MTRANSPOSE
) {
else if (orows
== 0 && ocols
== 0) { /* decide rows and cols */
fprintf(stderr
, "Display width %d is less than column width %d\n", owidth
, colw
);
orows
= nelem
/ ocols
+ (nelem
% ocols
? 1 : 0);
else if (orows
== 0) /* decide on rows */
orows
= nelem
/ ocols
+ (nelem
% ocols
? 1 : 0);
else if (ocols
== 0) /* decide on cols */
ocols
= nelem
/ orows
+ (nelem
% orows
? 1 : 0);
lp
= elem
+ orows
* ocols
;
lp
= elem
+ orows
* ocols
;
for (ep
= elem
+ nelem
; ep
< lp
; ep
++)
if (!(colwidths
= (short *) malloc(ocols
* sizeof(short))))
error("malloc: No gutter space", "");
for (ep
= elem
, i
= 0; i
< ocols
; i
++) {
for (j
= 0; j
< orows
; j
++)
if ((n
= strlen(*ep
++)) > max
)
colwidths
[i
] = max
+ gutter
;
for (i
= 0; i
< ocols
; i
++) {
for (j
= i
; j
< nelem
; j
+= ocols
)
if ((n
= strlen(ep
[j
])) > max
)
colwidths
[i
] = max
+ gutter
;
/* for (i = 0; i < orows; i++) {
for (j = i; j < nelem; j += orows)
prints(ep[j], (j - i) / orows);
for (i = 0; i < orows; i++) {
for (j = 0; j < ocols; j++)
for (i
= 0; i
< ocols
; i
++)
if (!(flags
& NOTRIMENDCOL
)) {
colwidths
[ocols
- 1] = 0;
if (n
> nelem
&& (flags
& RECYCLE
))
/*for (i = 0; i < ocols; i++)
fprintf(stderr, "%d ",colwidths[i]);
fprintf(stderr, "is colwidths, nelem %d\n", nelem);*/
char ibuf
[BSIZE
]; /* two screenfuls should do */
getline() /* get line; maintain curline, curlen; manage storage */
static char *endblock
= ibuf
+ BSIZE
;
putlength
= flags
& DETAILSHAPE
;
else if (skip
<= 0) { /* don't waste storage */
if (putlength
) /* print length, recycle storage */
printf(" %d line %d\n", curlen
, irows
);
if (!putlength
&& endblock
- curline
< BUFSIZ
) { /* need storage */
/*ww = endblock-curline; tt += ww;*/
/*printf("#wasted %d total %d\n",ww,tt);*/
if (!(curline
= (char *) malloc(BSIZE
)))
error("File too large", "");
endblock
= curline
+ BSIZE
;
/*printf("#endb %d curline %d\n",endblock,curline);*/
for (p
= curline
, i
= 1; i
< BUFSIZ
; *p
++ = c
, i
++)
if ((c
= getchar()) == EOF
|| c
== '\n')
if (!(p
= (char **) malloc(allocsize
* sizeof(char *)))) {
if ((endelem
= p
+ allocsize
- icols
) <= p
) {
char *getnum(), *getlist();
flags
|= NOARGS
| TRANSPOSE
;
while (--ac
&& **++av
== '-')
case 'c': /* input col. separator */
case 's': /* one or more allowed */
isep
= '\t'; /* default is ^I */
osep
= '\t'; /* default is ^I */
case 'w': /* window width, default 80 */
p
= getnum(&owidth
, p
, 0);
error("Width must be a positive integer", "");
case 'K': /* skip N lines */
case 'k': /* skip, do not print */
case 'g': /* gutter space */
p
= getnum(&gutter
, p
, 0);
p
= getnum(&propgutter
, p
, 0);
case 'e': /* each line is an entry */
case 'j': /* right adjust */
case 'n': /* null padding for missing values */
case 'H': /* print shape only */
case 'z': /* squeeze col width */
ipagespace = atoi(++p); (default is 1)
case 'o': /* col order */
error("Bad flag: %.1s", p
);
error("Too many arguments. What do you mean by `%s'?", av
[3]);
for (t
= p
+ 1; *t
; t
++) {
error("Option %.1s requires a list of unsigned numbers separated by commas", t
);
while (*t
&& isdigit(*t
))
if (!(*list
= (short *) malloc(count
* sizeof(short))))
error("No list space", "");
for (t
= p
+ 1; *t
; t
++) {
(*list
)[count
++] = atoi(t
);
printf("++ %d ", (*list
)[count
-1]);
while (*t
&& isdigit(*t
))
getnum(num
, p
, strict
) /* num = number p points to; if (strict) complain */
int *num
; /* returns pointer to end of num */
if (strict
|| *t
== '-' || *t
== '+')
error("Option %.1s requires an unsigned integer", p
);