From c1ea49e6b139bc7e630843550a15670f146289fa Mon Sep 17 00:00:00 2001 From: charnier Date: Thu, 7 Aug 1997 06:42:37 +0000 Subject: Use err(3). Add usage(). Rewrote man page in mdoc format. --- usr.bin/rs/rs.1 | 222 ++++++++++++++++++++++++++++++++------------------------ usr.bin/rs/rs.c | 47 ++++++------ 2 files changed, 153 insertions(+), 116 deletions(-) diff --git a/usr.bin/rs/rs.1 b/usr.bin/rs/rs.1 index 07de48c..baccd3e 100644 --- a/usr.bin/rs/rs.1 +++ b/usr.bin/rs/rs.1 @@ -31,160 +31,196 @@ .\" .\" @(#)rs.1 8.2 (Berkeley) 12/30/93 .\" -.TH RS 1 "December 30, 1993" -.UC 4 -.SH NAME -rs \- reshape a data array -.SH SYNOPSIS -\fBrs [ \-[csCS][\fRx\fB][kKgGw][\fRN\fB]tTeEnyjhHm ] [ \fRrows\fB [ \fRcols\fB ] ]\fR -.SH DESCRIPTION -.I Rs +.Dd December 30, 1993 +.Dt RS 1 +.Os +.Sh NAME +.Nm rs +.Nd reshape a data array +.Sh SYNOPSIS +.Nm rs +.Oo +.Fl Op csCS +.Op Ar x +.Op kKgGw +.Op Ar N +tTeEnyjhHmz +.Oc +.Op Ar rows Op Ar cols +.Sh DESCRIPTION +.Nm Rs reads the standard input, interpreting each line as a row of blank-separated entries in an array, transforms the array according to the options, and writes it on the standard output. With no arguments it transforms stream input into a columnar format convenient for terminal viewing. -.PP +.Pp The shape of the input array is deduced from the number of lines and the number of columns on the first line. If that shape is inconvenient, a more useful one might be -obtained by skipping some of the input with the \fB\-k\fP option. +obtained by skipping some of the input with the +.Fl k +option. Other options control interpretation of the input columns. -.PP +.Pp The shape of the output array is influenced by the -.I rows +.Ar rows and -.I cols +.Ar cols specifications, which should be positive integers. If only one of them is a positive integer, -.I rs +.Nm computes a value for the other which will accommodate all of the data. When necessary, missing data are supplied in a manner specified by the options and surplus data are deleted. There are options to control presentation of the output columns, including transposition of the rows and columns. -.PP -The options are described below. -.IP \fB\-c\fRx -Input columns are delimited by the single character \fIx\fP. -A missing \fIx\fP is taken to be `^I'. -.IP \fB\-s\fRx -Like \fB\-c\fR, but maximal strings of \fIx\fP are delimiters. -.IP \fB\-C\fRx -Output columns are delimited by the single character \fIx\fP. -A missing \fIx\fP is taken to be `^I'. -.IP \fB\-S\fRx -Like \fB\-C\fR, but padded strings of \fIx\fP are delimiters. -.IP \fB\-t\fR +.Pp +The following options are available: +.Bl -tag -width indent +.It Fl c Ns Ar x +Input columns are delimited by the single character +.Ar x . +A missing +.Ar x +is taken to be `^I'. +.It Fl s Ns Ar x +Like +.Fl c , +but maximal strings of +.Ar x +are delimiters. +.It Fl C Ns Ar x +Output columns are delimited by the single character +.Ar x . +A missing +.Ar x +is taken to be `^I'. +.It Fl S Ns Ar x +Like +.Fl C , +but padded strings of +.Ar x +are delimiters. +.It Fl t Fill in the rows of the output array using the columns of the input array, that is, transpose the input while honoring any -.I rows +.Ar rows and -.I cols +.Ar cols specifications. -.IP \fB\-T\fR +.It Fl T Print the pure transpose of the input, ignoring any -.I rows +.Ar rows or -.I cols +.Ar cols specification. -.IP \fB\-k\fRN -Ignore the first \fIN\fR lines of input. -.IP \fB\-K\fRN -Like \fB\-k\fR, but print the ignored lines. -.IP \fB\-g\fRN -The gutter width (inter-column space), normally 2, is taken to be \fIN\fR. -.IP \fB\-G\fRN -The gutter width has \fIN\fR percent of the maximum -column width added to it. -.IP \fB\-e\fR +.It Fl k Ns Ar N +Ignore the first +.Ar N +lines of input. +.It Fl K Ns Ar N +Like +.Fl k , +but print the ignored lines. +.It Fl g Ns Ar N +The gutter width (inter-column space), normally 2, is taken to be +.Ar N . +.It Fl G Ns Ar N +The gutter width has +.Ar N +percent of the maximum column width added to it. +.It Fl e Consider each line of input as an array entry. -.IP \fB\-n\fR +.It Fl n On lines having fewer entries than the first line, use null entries to pad out the line. Normally, missing entries are taken from the next line of input. -.IP \fB\-y\fR +.It Fl y If there are too few entries to make up the output dimensions, pad the output by recycling the input from the beginning. Normally, the output is padded with blanks. -.IP \fB\-h\fR +.It Fl h Print the shape of the input array and do nothing else. The shape is just the number of lines and the number of entries on the first line. -.IP \fB\-H\fR -Like \fB\-h\fR, but also print the length of each line. -.IP \fB\-j\fR +.It Fl H +Like +.Fl h , +but also print the length of each line. +.It Fl j Right adjust entries within columns. -.IP \fB\-w\fRN +.It Fl w Ns Ar N The width of the display, normally 80, is taken to be the positive -integer \fIN\fP. -.IP \fB\-m\fR +integer +.Ar N . +.It Fl m Do not trim excess delimiters from the ends of the output array. -.PP +.It Fl z +Adapt column widths to fit the largest entries appearing in them. +.El +.Pp With no arguments, -.I rs +.Nm transposes its input, and assumes one array entry per input line unless the first non-ignored line is longer than the display width. Option letters which take numerical arguments interpret a missing number as zero unless otherwise indicated. -.SH EXAMPLES -.de IC -.IP -.ss 36 -.ft B -.. -.de NC -.br -.ss 12 -.PP -.. -.I Rs +.Sh EXAMPLES +.Nm Rs can be used as a filter to convert the stream output of certain programs (e.g., -.IR spell , -.IR du , -.IR file , -.IR look , -.IR nm , -.IR who , +.Xr spell , +.Xr du , +.Xr file , +.Xr look , +.Xr nm , +.Xr who , and -.IR wc (1)) +.Xr wc 1 ) into a convenient ``window'' format, as in -.IC -who | rs -.NC +.Bd -literal -offset indent +% who | rs +.Ed +.Pp This function has been incorporated into the -.IR ls (1) +.Xr ls 1 program, though for most programs with similar output -.I rs +.Nm suffices. -.PP +.Pp To convert stream input into vector output and back again, use -.IC -rs 1 0 | rs 0 1 -.NC +.Bd -literal -offset indent +% rs 1 0 | rs 0 1 +.Ed +.Pp A 10 by 10 array of random numbers from 1 to 100 and its transpose can be generated with -.IC -jot \-r 100 | rs 10 10 | tee array | rs \-T > tarray -.NC +.Bd -literal -offset indent +% jot \-r 100 | rs 10 10 | tee array | rs \-T > tarray +.Ed +.Pp In the editor -.IR vi (1), +.Xr vi 1 , a file consisting of a multi-line vector with 9 elements per line can undergo insertions and deletions, and then be neatly reshaped into 9 columns with -.IC +.Bd -literal -offset indent :1,$!rs 0 9 -.NC +.Ed +.Pp Finally, to sort a database by the first line of each 4-line field, try -.IC -rs \-eC 0 4 | sort | rs \-c 0 1 -.NC -.SH SEE ALSO -jot(1), vi(1), sort(1), pr(1) -.SH BUGS +.Bd -literal -offset indent +% rs \-eC 0 4 | sort | rs \-c 0 1 +.Ed +.Sh SEE ALSO +.Xr jot 1 , +.Xr pr 1 , +.Xr sort 1 , +.Xr vi 1 +.Sh BUGS Handles only two dimensional arrays. The algorithm currently reads the whole file into memory, diff --git a/usr.bin/rs/rs.c b/usr.bin/rs/rs.c index 2f5e92b..a023db9 100644 --- a/usr.bin/rs/rs.c +++ b/usr.bin/rs/rs.c @@ -32,13 +32,17 @@ */ #ifndef lint -static char copyright[] = +static const char copyright[] = "@(#) Copyright (c) 1993\n\ The Regents of the University of California. All rights reserved.\n"; #endif /* not lint */ #ifndef lint +#if 0 static char sccsid[] = "@(#)rs.c 8.1 (Berkeley) 6/6/93"; +#endif +static const char rcsid[] = + "$Id$"; #endif /* not lint */ /* @@ -47,6 +51,7 @@ static char sccsid[] = "@(#)rs.c 8.1 (Berkeley) 6/6/93"; * BEWARE: lots of unfinished edges */ +#include #include #include #include @@ -88,7 +93,6 @@ int propgutter; char isep = ' ', osep = ' '; int owidth = 80, gutter = 2; -void error __P((char *, char *)); void getargs __P((int, char *[])); void getfile __P((void)); int getline __P((void)); @@ -98,6 +102,7 @@ char **getptrs __P((char **)); void prepfile __P((void)); void prints __P((char *, int)); void putfile __P((void)); +static void usage __P((void)); int main(argc, argv) @@ -222,14 +227,11 @@ prints(s, col) putchar(osep); } -void -error(msg, s) - char *msg, *s; +static void +usage() { - fprintf(stderr, "rs: "); - fprintf(stderr, msg, s); fprintf(stderr, -"\nUsage: rs [ -[csCS][x][kKgGw][N]tTeEnyjhHm ] [ rows [ cols ] ]\n"); + "usage: rs [-[csCS][x][kKgGw][N]tTeEnyjhHmz] [rows [cols]]\n"); exit(1); } @@ -255,7 +257,8 @@ prepfile() else if (orows == 0 && ocols == 0) { /* decide rows and cols */ ocols = owidth / colw; 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", + owidth, colw); if (ocols > nelem) ocols = nelem; orows = nelem / ocols + (nelem % ocols ? 1 : 0); @@ -275,7 +278,7 @@ prepfile() nelem = lp - elem; } if (!(colwidths = (short *) malloc(ocols * sizeof(short)))) - error("malloc: No gutter space", ""); + errx(1, "malloc"); if (flags & SQUEEZE) { if (flags & TRANSPOSE) for (ep = elem, i = 0; i < ocols; i++) { @@ -316,8 +319,7 @@ prepfile() if (n > nelem && (flags & RECYCLE)) nelem = n; /*for (i = 0; i < ocols; i++) - fprintf(stderr, "%d ",colwidths[i]); - fprintf(stderr, "is colwidths, nelem %d\n", nelem);*/ + warnx("%d is colwidths, nelem %d", colwidths[i], nelem);*/ } #define BSIZE 2048 @@ -344,7 +346,7 @@ getline() /* get line; maintain curline, curlen; manage storage */ /*ww = endblock-curline; tt += ww;*/ /*printf("#wasted %d total %d\n",ww,tt);*/ if (!(curline = (char *) malloc(BSIZE))) - error("File too large", ""); + errx(1, "file too large"); endblock = curline + BSIZE; /*printf("#endb %d curline %d\n",endblock,curline);*/ } @@ -364,10 +366,8 @@ getptrs(sp) for (;;) { allocsize += allocsize; - if (!(p = (char **) malloc(allocsize * sizeof(char *)))) { - perror("rs"); - exit(1); - } + if (!(p = (char **) malloc(allocsize * sizeof(char *)))) + errx(1, "malloc"); if ((endelem = p + allocsize - icols) <= p) { free(p); continue; @@ -419,7 +419,7 @@ getargs(ac, av) case 'w': /* window width, default 80 */ p = getnum(&owidth, p, 0); if (owidth <= 0) - error("Width must be a positive integer", ""); + errx(1, "width must be a positive integer"); break; case 'K': /* skip N lines */ flags |= SKIPPRINT; @@ -475,7 +475,7 @@ getargs(ac, av) p = getlist(&ocbd, p); break; default: - error("Bad flag: %.1s", p); + usage(); } /*if (!osep) osep = isep;*/ @@ -489,7 +489,7 @@ getargs(ac, av) case 0: break; default: - error("Too many arguments. What do you mean by `%s'?", av[3]); + errx(1, "too many arguments"); } } @@ -503,7 +503,8 @@ getlist(list, p) for (t = p + 1; *t; t++) { if (!isdigit(*t)) - error("Option %.1s requires a list of unsigned numbers separated by commas", t); + errx(1, + "option %.1s requires a list of unsigned numbers separated by commas", t); count++; while (*t && isdigit(*t)) t++; @@ -511,7 +512,7 @@ getlist(list, p) break; } if (!(*list = (short *) malloc(count * sizeof(short)))) - error("No list space", ""); + errx(1, "no list space"); count = 0; for (t = p + 1; *t; t++) { (*list)[count++] = atoi(t); @@ -535,7 +536,7 @@ getnum(num, p, strict) /* num = number p points to; if (strict) complain */ if (!isdigit(*++t)) { if (strict || *t == '-' || *t == '+') - error("Option %.1s requires an unsigned integer", p); + errx(1, "option %.1s requires an unsigned integer", p); *num = 0; return(p); } -- cgit v1.1