diff options
author | Theo de Raadt <deraadt@cvs.openbsd.org> | 1995-10-18 08:53:40 +0000 |
---|---|---|
committer | Theo de Raadt <deraadt@cvs.openbsd.org> | 1995-10-18 08:53:40 +0000 |
commit | d6583bb2a13f329cf0332ef2570eb8bb8fc0e39c (patch) | |
tree | ece253b876159b39c620e62b6c9b1174642e070e /usr.bin/rs |
initial import of NetBSD tree
Diffstat (limited to 'usr.bin/rs')
-rw-r--r-- | usr.bin/rs/Makefile | 5 | ||||
-rw-r--r-- | usr.bin/rs/rs.1 | 197 | ||||
-rw-r--r-- | usr.bin/rs/rs.c | 547 |
3 files changed, 749 insertions, 0 deletions
diff --git a/usr.bin/rs/Makefile b/usr.bin/rs/Makefile new file mode 100644 index 00000000000..21da34d0a01 --- /dev/null +++ b/usr.bin/rs/Makefile @@ -0,0 +1,5 @@ +# @(#)Makefile 8.1 (Berkeley) 6/6/93 + +PROG= rs + +.include <bsd.prog.mk> diff --git a/usr.bin/rs/rs.1 b/usr.bin/rs/rs.1 new file mode 100644 index 00000000000..07de48c50d6 --- /dev/null +++ b/usr.bin/rs/rs.1 @@ -0,0 +1,197 @@ +.\" Copyright (c) 1993 +.\" The Regents of the University of California. All rights reserved. +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" 1. Redistributions of source code must retain the above copyright +.\" notice, this list of conditions and the following disclaimer. +.\" 2. Redistributions in binary form must reproduce the above copyright +.\" notice, this list of conditions and the following disclaimer in the +.\" documentation and/or other materials provided with the distribution. +.\" 3. All advertising materials mentioning features or use of this software +.\" must display the following acknowledgement: +.\" This product includes software developed by the University of +.\" California, Berkeley and its contributors. +.\" 4. Neither the name of the University nor the names of its contributors +.\" may be used to endorse or promote products derived from this software +.\" without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND +.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE +.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +.\" SUCH DAMAGE. +.\" +.\" @(#)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 +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 +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. +Other options control interpretation of the input columns. +.PP +The shape of the output array is influenced by the +.I rows +and +.I cols +specifications, which should be positive integers. +If only one of them is a positive integer, +.I rs +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 +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 +and +.I cols +specifications. +.IP \fB\-T\fR +Print the pure transpose of the input, ignoring any +.I rows +or +.I 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 +Consider each line of input as an array entry. +.IP \fB\-n\fR +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 +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 +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 +Right adjust entries within columns. +.IP \fB\-w\fRN +The width of the display, normally 80, is taken to be the positive +integer \fIN\fP. +.IP \fB\-m\fR +Do not trim excess delimiters from the ends of the output array. +.PP +With no arguments, +.I rs +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 +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 , +and +.IR wc (1)) +into a convenient ``window'' format, as in +.IC +who | rs +.NC +This function has been incorporated into the +.IR ls (1) +program, though for most programs with similar output +.I rs +suffices. +.PP +To convert stream input into vector output and back again, use +.IC +rs 1 0 | rs 0 1 +.NC +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 +In the editor +.IR 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 +:1,$!rs 0 9 +.NC +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 +Handles only two dimensional arrays. + +The algorithm currently reads the whole file into memory, +so files that do not fit in memory will not be reshaped. + +Fields cannot be defined yet on character positions. + +Re-ordering of columns is not yet possible. + +There are too many options. diff --git a/usr.bin/rs/rs.c b/usr.bin/rs/rs.c new file mode 100644 index 00000000000..7112a861522 --- /dev/null +++ b/usr.bin/rs/rs.c @@ -0,0 +1,547 @@ +/*- + * Copyright (c) 1993 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#ifndef lint +static char copyright[] = +"@(#) Copyright (c) 1993\n\ + The Regents of the University of California. All rights reserved.\n"; +#endif /* not lint */ + +#ifndef lint +static char sccsid[] = "@(#)rs.c 8.1 (Berkeley) 6/6/93"; +#endif /* not lint */ + +/* + * rs - reshape a data array + * Author: John Kunze, Office of Comp. Affairs, UCB + * BEWARE: lots of unfinished edges + */ + +#include <ctype.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +long flags; +#define TRANSPOSE 000001 +#define MTRANSPOSE 000002 +#define ONEPERLINE 000004 +#define ONEISEPONLY 000010 +#define ONEOSEPONLY 000020 +#define NOTRIMENDCOL 000040 +#define SQUEEZE 000100 +#define SHAPEONLY 000200 +#define DETAILSHAPE 000400 +#define RIGHTADJUST 001000 +#define NULLPAD 002000 +#define RECYCLE 004000 +#define SKIPPRINT 010000 +#define ICOLBOUNDS 020000 +#define OCOLBOUNDS 040000 +#define ONEPERCHAR 0100000 +#define NOARGS 0200000 + +short *colwidths; +short *cord; +short *icbd; +short *ocbd; +int nelem; +char **elem; +char **endelem; +char *curline; +int allocsize = BUFSIZ; +int curlen; +int irows, icols; +int orows, ocols; +int maxlen; +int skip; +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)); +char *getlist __P((short **, char *)); +char *getnum __P((int *, char *, int)); +char **getptrs __P((char **)); +void prepfile __P((void)); +void prints __P((char *, int)); +void putfile __P((void)); + +int +main(argc, argv) + int argc; + char *argv[]; +{ + getargs(argc, argv); + getfile(); + if (flags & SHAPEONLY) { + printf("%d %d\n", irows, icols); + exit(0); + } + prepfile(); + putfile(); + exit(0); +} + +void +getfile() +{ + register char *p; + register char *endp; + register char **ep = 0; + int multisep = (flags & ONEISEPONLY ? 0 : 1); + int nullpad = flags & NULLPAD; + char **padto; + + while (skip--) { + getline(); + if (flags & SKIPPRINT) + puts(curline); + } + getline(); + if (flags & NOARGS && curlen < owidth) + flags |= ONEPERLINE; + if (flags & ONEPERLINE) + icols = 1; + else /* count cols on first line */ + for (p = curline, endp = curline + curlen; p < endp; p++) { + if (*p == isep && multisep) + continue; + icols++; + while (*p && *p != isep) + p++; + } + ep = getptrs(elem); + p = curline; + do { + if (flags & ONEPERLINE) { + *ep++ = curline; + if (maxlen < curlen) + maxlen = curlen; + irows++; + continue; + } + 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 */ + *ep = ""; + else /* store column entry */ + *ep = p; + while (p < endp && *p != isep) + p++; /* find end of entry */ + *p = '\0'; /* mark end of entry */ + if (maxlen < p - *ep) /* update maxlen */ + maxlen = p - *ep; + ep++; /* prepare for next entry */ + } + irows++; /* update row count */ + if (nullpad) { /* pad missing entries */ + padto = elem + irows * icols; + while (ep < padto) + *ep++ = ""; + } + if (ep > endelem) /* if low on pointers */ + ep = getptrs(ep); /* get some more */ + } while (getline() != EOF); + *ep = 0; /* mark end of pointers */ + nelem = ep - elem; +} + +void +putfile() +{ + register char **ep; + register int i, j; + + ep = elem; + if (flags & TRANSPOSE) + for (i = 0; i < orows; i++) { + for (j = i; j < nelem; j += orows) + prints(ep[j], (j - i) / orows); + putchar('\n'); + } + else + for (i = 0; i < orows; i++) { + for (j = 0; j < ocols; j++) + prints(*ep++, j); + putchar('\n'); + } +} + +void +prints(s, col) + char *s; + int col; +{ + register int n; + register char *p = s; + + while (*p) + p++; + n = (flags & ONEOSEPONLY ? 1 : colwidths[col] - (p - s)); + if (flags & RIGHTADJUST) + while (n-- > 0) + putchar(osep); + for (p = s; *p; p++) + putchar(*p); + while (n-- > 0) + putchar(osep); +} + +void +error(msg, s) + char *msg, *s; +{ + fprintf(stderr, "rs: "); + fprintf(stderr, msg, s); + fprintf(stderr, +"\nUsage: rs [ -[csCS][x][kKgGw][N]tTeEnyjhHm ] [ rows [ cols ] ]\n"); + exit(1); +} + +void +prepfile() +{ + register char **ep; + register int i; + register int j; + char **lp; + int colw; + int max = 0; + int n; + + if (!nelem) + exit(0); + gutter += maxlen * propgutter / 100.0; + colw = maxlen + gutter; + if (flags & MTRANSPOSE) { + orows = icols; + ocols = irows; + } + 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); + if (ocols > nelem) + ocols = nelem; + 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; + while (lp > endelem) { + getptrs(elem + nelem); + lp = elem + orows * ocols; + } + if (flags & RECYCLE) { + for (ep = elem + nelem; ep < lp; ep++) + *ep = *(ep - nelem); + nelem = lp - elem; + } + if (!(colwidths = (short *) malloc(ocols * sizeof(short)))) + error("malloc: No gutter space", ""); + if (flags & SQUEEZE) { + if (flags & TRANSPOSE) + for (ep = elem, i = 0; i < ocols; i++) { + for (j = 0; j < orows; j++) + if ((n = strlen(*ep++)) > max) + max = n; + colwidths[i] = max + gutter; + } + else + for (i = 0; i < ocols; i++) { + for (j = i; j < nelem; j += ocols) + if ((n = strlen(ep[j])) > max) + max = n; + colwidths[i] = max + gutter; + } + } + /* for (i = 0; i < orows; i++) { + for (j = i; j < nelem; j += orows) + prints(ep[j], (j - i) / orows); + putchar('\n'); + } + else + for (i = 0; i < orows; i++) { + for (j = 0; j < ocols; j++) + prints(*ep++, j); + putchar('\n'); + }*/ + else + for (i = 0; i < ocols; i++) + colwidths[i] = colw; + if (!(flags & NOTRIMENDCOL)) { + if (flags & RIGHTADJUST) + colwidths[0] -= gutter; + else + colwidths[ocols - 1] = 0; + } + n = orows * ocols; + 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);*/ +} + +#define BSIZE 2048 +char ibuf[BSIZE]; /* two screenfuls should do */ + +int +getline() /* get line; maintain curline, curlen; manage storage */ +{ + static int putlength; + static char *endblock = ibuf + BSIZE; + register char *p; + register int c, i; + + if (!irows) { + curline = ibuf; + putlength = flags & DETAILSHAPE; + } + else if (skip <= 0) { /* don't waste storage */ + curline += curlen + 1; + 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') + break; + *p = '\0'; + curlen = i - 1; + return(c); +} + +char ** +getptrs(sp) + char **sp; +{ + register char **p, **ep; + + for (;;) { + allocsize += allocsize; + if (!(p = (char **) malloc(allocsize * sizeof(char *)))) { + perror("rs"); + exit(1); + } + if ((endelem = p + allocsize - icols) <= p) { + free(p); + continue; + } + if (elem != 0) + free(elem); + ep = elem; + elem = p; + while (ep < sp) + *p++ = *ep++; + return(p); + } +} + +void +getargs(ac, av) + int ac; + char *av[]; +{ + register char *p; + + if (ac == 1) { + flags |= NOARGS | TRANSPOSE; + } + while (--ac && **++av == '-') + for (p = *av+1; *p; p++) + switch (*p) { + case 'T': + flags |= MTRANSPOSE; + case 't': + flags |= TRANSPOSE; + break; + case 'c': /* input col. separator */ + flags |= ONEISEPONLY; + case 's': /* one or more allowed */ + if (p[1]) + isep = *++p; + else + isep = '\t'; /* default is ^I */ + break; + case 'C': + flags |= ONEOSEPONLY; + case 'S': + if (p[1]) + osep = *++p; + else + osep = '\t'; /* default is ^I */ + break; + case 'w': /* window width, default 80 */ + p = getnum(&owidth, p, 0); + if (owidth <= 0) + error("Width must be a positive integer", ""); + break; + case 'K': /* skip N lines */ + flags |= SKIPPRINT; + case 'k': /* skip, do not print */ + p = getnum(&skip, p, 0); + if (!skip) + skip = 1; + break; + case 'm': + flags |= NOTRIMENDCOL; + break; + case 'g': /* gutter space */ + p = getnum(&gutter, p, 0); + break; + case 'G': + p = getnum(&propgutter, p, 0); + break; + case 'e': /* each line is an entry */ + flags |= ONEPERLINE; + break; + case 'E': + flags |= ONEPERCHAR; + break; + case 'j': /* right adjust */ + flags |= RIGHTADJUST; + break; + case 'n': /* null padding for missing values */ + flags |= NULLPAD; + break; + case 'y': + flags |= RECYCLE; + break; + case 'H': /* print shape only */ + flags |= DETAILSHAPE; + case 'h': + flags |= SHAPEONLY; + break; + case 'z': /* squeeze col width */ + flags |= SQUEEZE; + break; + /*case 'p': + ipagespace = atoi(++p); (default is 1) + break;*/ + case 'o': /* col order */ + p = getlist(&cord, p); + break; + case 'b': + flags |= ICOLBOUNDS; + p = getlist(&icbd, p); + break; + case 'B': + flags |= OCOLBOUNDS; + p = getlist(&ocbd, p); + break; + default: + error("Bad flag: %.1s", p); + } + /*if (!osep) + osep = isep;*/ + switch (ac) { + /*case 3: + opages = atoi(av[2]);*/ + case 2: + ocols = atoi(av[1]); + case 1: + orows = atoi(av[0]); + case 0: + break; + default: + error("Too many arguments. What do you mean by `%s'?", av[3]); + } +} + +char * +getlist(list, p) + short **list; + char *p; +{ + register int count = 1; + register char *t; + + for (t = p + 1; *t; t++) { + if (!isdigit(*t)) + error("Option %.1s requires a list of unsigned numbers separated by commas", t); + count++; + while (*t && isdigit(*t)) + t++; + if (*t != ',') + break; + } + if (!(*list = (short *) malloc(count * sizeof(short)))) + error("No list space", ""); + count = 0; + for (t = p + 1; *t; t++) { + (*list)[count++] = atoi(t); + printf("++ %d ", (*list)[count-1]); + fflush(stdout); + while (*t && isdigit(*t)) + t++; + if (*t != ',') + break; + } + (*list)[count] = 0; + return(t - 1); +} + +char * +getnum(num, p, strict) /* num = number p points to; if (strict) complain */ + int *num, strict; /* returns pointer to end of num */ + char *p; +{ + register char *t = p; + + if (!isdigit(*++t)) { + if (strict || *t == '-' || *t == '+') + error("Option %.1s requires an unsigned integer", p); + *num = 0; + return(p); + } + *num = atoi(t); + while (*++t) + if (!isdigit(*t)) + break; + return(--t); +} |