version 1.1, 1995/10/18 08:46:03 |
version 1.2, 1996/05/21 21:37:11 |
|
|
char isep = ' ', osep = ' '; |
char isep = ' ', osep = ' '; |
int owidth = 80, gutter = 2; |
int owidth = 80, gutter = 2; |
|
|
void error __P((char *, char *)); |
void usage __P((char *, char *)); |
void getargs __P((int, char *[])); |
void getargs __P((int, char *[])); |
void getfile __P((void)); |
void getfile __P((void)); |
int getline __P((void)); |
int getline __P((void)); |
|
|
void prints __P((char *, int)); |
void prints __P((char *, int)); |
void putfile __P((void)); |
void putfile __P((void)); |
|
|
|
#define INCR(ep) do { \ |
|
if (++ep >= endelem) \ |
|
ep = getptrs(ep); \ |
|
} while(0) |
|
|
int |
int |
main(argc, argv) |
main(argc, argv) |
int argc; |
int argc; |
|
|
p = curline; |
p = curline; |
do { |
do { |
if (flags & ONEPERLINE) { |
if (flags & ONEPERLINE) { |
*ep++ = curline; |
*ep = curline; |
|
INCR(ep); /* prepare for next entry */ |
if (maxlen < curlen) |
if (maxlen < curlen) |
maxlen = curlen; |
maxlen = curlen; |
irows++; |
irows++; |
|
|
*p = '\0'; /* mark end of entry */ |
*p = '\0'; /* mark end of entry */ |
if (maxlen < p - *ep) /* update maxlen */ |
if (maxlen < p - *ep) /* update maxlen */ |
maxlen = p - *ep; |
maxlen = p - *ep; |
ep++; /* prepare for next entry */ |
INCR(ep); /* prepare for next entry */ |
} |
} |
irows++; /* update row count */ |
irows++; /* update row count */ |
if (nullpad) { /* pad missing entries */ |
if (nullpad) { /* pad missing entries */ |
padto = elem + irows * icols; |
padto = elem + irows * icols; |
while (ep < padto) |
while (ep < padto) { |
*ep++ = ""; |
*ep = ""; |
|
INCR(ep); |
|
} |
} |
} |
if (ep > endelem) /* if low on pointers */ |
|
ep = getptrs(ep); /* get some more */ |
|
} while (getline() != EOF); |
} while (getline() != EOF); |
*ep = 0; /* mark end of pointers */ |
*ep = 0; /* mark end of pointers */ |
nelem = ep - elem; |
nelem = ep - elem; |
|
|
putfile() |
putfile() |
{ |
{ |
register char **ep; |
register char **ep; |
register int i, j; |
register int i, j, n; |
|
|
ep = elem; |
ep = elem; |
if (flags & TRANSPOSE) |
if (flags & TRANSPOSE) { |
for (i = 0; i < orows; i++) { |
for (i = 0; i < orows; i++) { |
for (j = i; j < nelem; j += orows) |
for (j = i; j < nelem; j += orows) |
prints(ep[j], (j - i) / orows); |
prints(ep[j], (j - i) / orows); |
putchar('\n'); |
putchar('\n'); |
} |
} |
else |
} else { |
for (i = 0; i < orows; i++) { |
for (n = 0, i = 0; i < orows && n < nelem; i++) { |
for (j = 0; j < ocols; j++) |
for (j = 0; j < ocols; j++) { |
|
if (n++ >= nelem) |
|
break; |
prints(*ep++, j); |
prints(*ep++, j); |
|
} |
putchar('\n'); |
putchar('\n'); |
} |
} |
|
} |
} |
} |
|
|
void |
void |
|
|
} |
} |
|
|
void |
void |
error(msg, s) |
usage(msg, s) |
char *msg, *s; |
char *msg, *s; |
{ |
{ |
fprintf(stderr, "rs: "); |
warnx(msg, s); |
fprintf(stderr, msg, s); |
|
fprintf(stderr, |
fprintf(stderr, |
"\nUsage: rs [ -[csCS][x][kKgGw][N]tTeEnyjhHm ] [ rows [ cols ] ]\n"); |
"Usage: rs [ -[csCS][x][kKgGw][N]tTeEnyjhHm ] [ rows [ cols ] ]\n"); |
exit(1); |
exit(1); |
} |
} |
|
|
|
|
} |
} |
else if (orows == 0 && ocols == 0) { /* decide rows and cols */ |
else if (orows == 0 && ocols == 0) { /* decide rows and cols */ |
ocols = owidth / colw; |
ocols = owidth / colw; |
if (ocols == 0) |
if (ocols == 0) { |
fprintf(stderr, "Display width %d is less than column width %d\n", owidth, colw); |
warnx("Display width %d is less than column width %d\n", owidth, colw); |
|
ocols = 1; |
|
} |
if (ocols > nelem) |
if (ocols > nelem) |
ocols = nelem; |
ocols = nelem; |
orows = nelem / ocols + (nelem % ocols ? 1 : 0); |
orows = nelem / ocols + (nelem % ocols ? 1 : 0); |
|
|
nelem = lp - elem; |
nelem = lp - elem; |
} |
} |
if (!(colwidths = (short *) malloc(ocols * sizeof(short)))) |
if (!(colwidths = (short *) malloc(ocols * sizeof(short)))) |
error("malloc: No gutter space", ""); |
errx(1, "malloc: No gutter space"); |
if (flags & SQUEEZE) { |
if (flags & SQUEEZE) { |
if (flags & TRANSPOSE) |
if (flags & TRANSPOSE) |
for (ep = elem, i = 0; i < ocols; i++) { |
for (ep = elem, i = 0; i < ocols; i++) { |
|
|
/*ww = endblock-curline; tt += ww;*/ |
/*ww = endblock-curline; tt += ww;*/ |
/*printf("#wasted %d total %d\n",ww,tt);*/ |
/*printf("#wasted %d total %d\n",ww,tt);*/ |
if (!(curline = (char *) malloc(BSIZE))) |
if (!(curline = (char *) malloc(BSIZE))) |
error("File too large", ""); |
errx(1, "File too large"); |
endblock = curline + BSIZE; |
endblock = curline + BSIZE; |
/*printf("#endb %d curline %d\n",endblock,curline);*/ |
/*printf("#endb %d curline %d\n",endblock,curline);*/ |
} |
} |
|
|
getptrs(sp) |
getptrs(sp) |
char **sp; |
char **sp; |
{ |
{ |
register char **p, **ep; |
register char **p; |
|
|
for (;;) { |
allocsize += allocsize; |
allocsize += allocsize; |
p = (char **)realloc(elem, allocsize * sizeof(char *)); |
if (!(p = (char **) malloc(allocsize * sizeof(char *)))) { |
if (p == (char **)0) |
perror("rs"); |
err(1, "no memory"); |
exit(1); |
|
} |
sp += (p - elem); |
if ((endelem = p + allocsize - icols) <= p) { |
endelem = (elem = p) + allocsize; |
free(p); |
return(sp); |
continue; |
|
} |
|
if (elem != 0) |
|
free(elem); |
|
ep = elem; |
|
elem = p; |
|
while (ep < sp) |
|
*p++ = *ep++; |
|
return(p); |
|
} |
|
} |
} |
|
|
void |
void |
|
|
case 'w': /* window width, default 80 */ |
case 'w': /* window width, default 80 */ |
p = getnum(&owidth, p, 0); |
p = getnum(&owidth, p, 0); |
if (owidth <= 0) |
if (owidth <= 0) |
error("Width must be a positive integer", ""); |
usage("Width must be a positive integer", ""); |
break; |
break; |
case 'K': /* skip N lines */ |
case 'K': /* skip N lines */ |
flags |= SKIPPRINT; |
flags |= SKIPPRINT; |
|
|
p = getlist(&ocbd, p); |
p = getlist(&ocbd, p); |
break; |
break; |
default: |
default: |
error("Bad flag: %.1s", p); |
usage("Bad flag: %.1s", p); |
} |
} |
/*if (!osep) |
/*if (!osep) |
osep = isep;*/ |
osep = isep;*/ |
|
|
case 0: |
case 0: |
break; |
break; |
default: |
default: |
error("Too many arguments. What do you mean by `%s'?", av[3]); |
usage("Too many arguments.", ""); |
} |
} |
} |
} |
|
|
|
|
|
|
for (t = p + 1; *t; t++) { |
for (t = p + 1; *t; t++) { |
if (!isdigit(*t)) |
if (!isdigit(*t)) |
error("Option %.1s requires a list of unsigned numbers separated by commas", t); |
usage("Option %.1s requires a list of unsigned numbers separated by commas", t); |
count++; |
count++; |
while (*t && isdigit(*t)) |
while (*t && isdigit(*t)) |
t++; |
t++; |
|
|
break; |
break; |
} |
} |
if (!(*list = (short *) malloc(count * sizeof(short)))) |
if (!(*list = (short *) malloc(count * sizeof(short)))) |
error("No list space", ""); |
errx(1, "No list space"); |
count = 0; |
count = 0; |
for (t = p + 1; *t; t++) { |
for (t = p + 1; *t; t++) { |
(*list)[count++] = atoi(t); |
(*list)[count++] = atoi(t); |
|
|
|
|
if (!isdigit(*++t)) { |
if (!isdigit(*++t)) { |
if (strict || *t == '-' || *t == '+') |
if (strict || *t == '-' || *t == '+') |
error("Option %.1s requires an unsigned integer", p); |
usage("Option %.1s requires an unsigned integer", p); |
*num = 0; |
*num = 0; |
return(p); |
return(p); |
} |
} |