[27167] in Source-Commits
/svn/athena r25989 - in trunk/third/rs: . debian debian/patches debian/source
daemon@ATHENA.MIT.EDU (Alexander Chernyakhovsky)
Sun May 12 21:19:46 2013
Date: Sun, 12 May 2013 21:19:39 -0400
From: Alexander Chernyakhovsky <achernya@MIT.EDU>
Message-Id: <201305130119.r4D1Jd8V009795@drugstore.mit.edu>
To: source-commits@MIT.EDU
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Author: achernya
Date: 2013-05-12 21:19:39 -0400 (Sun, 12 May 2013)
New Revision: 25989
Added:
trunk/third/rs/debian/control
trunk/third/rs/debian/patches/
trunk/third/rs/debian/patches/linux-compatibility.patch
trunk/third/rs/debian/patches/series
trunk/third/rs/debian/source/
trunk/third/rs/debian/source/format
Removed:
trunk/third/rs/debian/control.in
Modified:
trunk/third/rs/debian/changelog
trunk/third/rs/debian/compat
trunk/third/rs/debian/rules
trunk/third/rs/rs.1
trunk/third/rs/rs.c
Log:
In rs:
* Import FreeBSD rs (Sun Nov 6 08:16:35 2011 +0000)
* Switch to dh7
* Switch to source format 3.0 (quilt)
* Bump debian/compat to 7
* Bump standards version to 3.9.3
Modified: trunk/third/rs/debian/changelog
===================================================================
--- trunk/third/rs/debian/changelog 2013-05-13 01:03:07 UTC (rev 25988)
+++ trunk/third/rs/debian/changelog 2013-05-13 01:19:39 UTC (rev 25989)
@@ -1,9 +1,17 @@
debathena-rs (10.0.4) UNRELEASED; urgency=low
+ [ Jonathan Reed ]
* Bump debian/compat to 6
- -- Jonathan Reed <jdreed@mit.edu> Sun, 01 Jul 2012 12:21:34 -0400
+ [ Alexander Chernyakhovsky ]
+ * Import FreeBSD rs (Sun Nov 6 08:16:35 2011 +0000)
+ * Switch to dh7
+ * Switch to source format 3.0 (quilt)
+ * Bump debian/compat to 7
+ * Bump standards version to 3.9.3
+ -- Jonathan Reed <jdreed@mit.edu> Sun, 12 May 2013 21:17:46 -0400
+
debathena-rs (10.0.3) unstable; urgency=low
* Rename getline to rs_getline to avoid conflicting with the getline in
Modified: trunk/third/rs/debian/compat
===================================================================
--- trunk/third/rs/debian/compat 2013-05-13 01:03:07 UTC (rev 25988)
+++ trunk/third/rs/debian/compat 2013-05-13 01:19:39 UTC (rev 25989)
@@ -1 +1 @@
-6
+7
Copied: trunk/third/rs/debian/control (from rev 25984, trunk/third/rs/debian/control.in)
===================================================================
--- trunk/third/rs/debian/control (rev 0)
+++ trunk/third/rs/debian/control 2013-05-13 01:19:39 UTC (rev 25989)
@@ -0,0 +1,14 @@
+Source: debathena-rs
+Section: debathena/text
+Priority: extra
+Maintainer: Debathena Project <debathena@mit.edu>
+Build-Depends: debhelper (>= 7)
+Standards-Version: 3.9.3
+
+Package: debathena-rs
+Architecture: any
+Provides: rs
+Depends: ${shlibs:Depends}, ${misc:Depends}
+Description: Reshape a data array
+ rs reshapes data arrays according to various transform possibilities.
+
Added: trunk/third/rs/debian/patches/linux-compatibility.patch
===================================================================
--- trunk/third/rs/debian/patches/linux-compatibility.patch (rev 0)
+++ trunk/third/rs/debian/patches/linux-compatibility.patch 2013-05-13 01:19:39 UTC (rev 25989)
@@ -0,0 +1,53 @@
+--- a/rs.c
++++ b/rs.c
+@@ -44,7 +44,6 @@
+ */
+
+ #include <sys/cdefs.h>
+-__FBSDID("$FreeBSD$");
+
+ #include <err.h>
+ #include <ctype.h>
+@@ -93,7 +92,7 @@
+
+ static void getargs(int, char *[]);
+ static void getfile(void);
+-static int getline(void);
++static int rs_getline(void);
+ static char *getlist(short **, char *);
+ static char *getnum(int *, char *, int);
+ static char **getptrs(char **);
+@@ -133,13 +132,13 @@
+ char **padto;
+
+ while (skip--) {
+- c = getline();
++ c = rs_getline();
+ if (flags & SKIPPRINT)
+ puts(curline);
+ if (c == EOF)
+ return;
+ }
+- getline();
++ rs_getline();
+ if (flags & NOARGS && curlen < owidth)
+ flags |= ONEPERLINE;
+ if (flags & ONEPERLINE)
+@@ -184,7 +183,7 @@
+ INCR(ep);
+ }
+ }
+- } while (getline() != EOF);
++ } while (rs_getline() != EOF);
+ *ep = 0; /* mark end of pointers */
+ nelem = ep - elem;
+ }
+@@ -333,7 +332,7 @@
+ static char ibuf[BSIZE];
+
+ static int
+-getline(void) /* get line; maintain curline, curlen; manage storage */
++rs_getline(void) /* get line; maintain curline, curlen; manage storage */
+ {
+ static int putlength;
+ static char *endblock = ibuf + BSIZE;
Added: trunk/third/rs/debian/patches/series
===================================================================
--- trunk/third/rs/debian/patches/series (rev 0)
+++ trunk/third/rs/debian/patches/series 2013-05-13 01:19:39 UTC (rev 25989)
@@ -0,0 +1 @@
+linux-compatibility.patch
Modified: trunk/third/rs/debian/rules
===================================================================
--- trunk/third/rs/debian/rules 2013-05-13 01:03:07 UTC (rev 25988)
+++ trunk/third/rs/debian/rules 2013-05-13 01:19:39 UTC (rev 25989)
@@ -1,6 +1,3 @@
#!/usr/bin/make -f
-
-include /usr/share/cdbs/1/rules/debhelper.mk
-include /usr/share/cdbs/1/class/makefile.mk
-
-DEB_MAKE_INSTALL_TARGET = install DESTDIR=$(DEB_DESTDIR)
+%:
+ dh $@
Added: trunk/third/rs/debian/source/format
===================================================================
--- trunk/third/rs/debian/source/format (rev 0)
+++ trunk/third/rs/debian/source/format 2013-05-13 01:19:39 UTC (rev 25989)
@@ -0,0 +1 @@
+3.0 (quilt)
Modified: trunk/third/rs/rs.1
===================================================================
--- trunk/third/rs/rs.1 2013-05-13 01:03:07 UTC (rev 25988)
+++ trunk/third/rs/rs.1 2013-05-13 01:19:39 UTC (rev 25989)
@@ -9,7 +9,7 @@
.\" 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. Neither the name of the University nor the names of 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.
.\"
@@ -26,170 +26,220 @@
.\" SUCH DAMAGE.
.\"
.\" @(#)rs.1 8.2 (Berkeley) 12/30/93
+.\" $FreeBSD$
.\"
-.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
+.Dd February 25, 2011
+.Dt RS 1
+.Os
+.Sh NAME
+.Nm rs
+.Nd reshape a data array
+.Sh SYNOPSIS
+.Nm
+.Oo
+.Fl Oo Cm csCS Oc Ns Op Ar x
+.Oo Cm kKgGw Oc Ns Op Ar N
+.Cm tTeEnyjhHmz
+.Oc
+.Op Ar rows Op Ar cols
+.Sh DESCRIPTION
+The
+.Nm
+utility 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.
-.IP \fB\-z\fR
+.It Fl z
Adapt column widths to fit the largest entries appearing in them.
-.PP
+.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
-can be used as a filter to convert the stream output
+.Sh EXAMPLES
+The
+.Nm
+utility 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 1 ,
+.Xr du 1 ,
+.Xr file 1 ,
+.Xr look 1 ,
+.Xr nm 1 ,
+.Xr who 1 ,
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 HISTORY
+The
+.Nm
+utility first appeared in
+.Bx 4.2 .
+.Sh BUGS
+.Bl -item
+.It
Handles only two dimensional arrays.
-
+.It
The algorithm currently reads the whole file into memory,
so files that do not fit in memory will not be reshaped.
-
+.It
Fields cannot be defined yet on character positions.
-
+.It
Re-ordering of columns is not yet possible.
-
+.It
There are too many options.
+.It
+Multibyte characters are not recognized.
+.It
+Lines longer than
+.Dv LINE_MAX
+(2048) bytes are not processed and result in immediate termination of
+.Nm .
+.El
Modified: trunk/third/rs/rs.c
===================================================================
--- trunk/third/rs/rs.c 2013-05-13 01:03:07 UTC (rev 25988)
+++ trunk/third/rs/rs.c 2013-05-13 01:19:39 UTC (rev 25989)
@@ -10,7 +10,7 @@
* 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. Neither the name of the University nor the names of 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.
*
@@ -28,13 +28,13 @@
*/
#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
-static char sccsid[] = "@(#)rs.c 8.1 (Berkeley) 6/6/93";
+static const char sccsid[] = "@(#)rs.c 8.1 (Berkeley) 6/6/93";
#endif /* not lint */
/*
@@ -43,12 +43,16 @@
* BEWARE: lots of unfinished edges
*/
+#include <sys/cdefs.h>
+
+#include <err.h>
#include <ctype.h>
+#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
-long flags;
+static long flags;
#define TRANSPOSE 000001
#define MTRANSPOSE 000002
#define ONEPERLINE 000004
@@ -67,44 +71,43 @@
#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;
+static short *colwidths;
+static short *cord;
+static short *icbd;
+static short *ocbd;
+static int nelem;
+static char **elem;
+static char **endelem;
+static char *curline;
+static int allocsize = BUFSIZ;
+static int curlen;
+static int irows, icols;
+static int orows = 0, ocols = 0;
+static int maxlen;
+static int skip;
+static int propgutter;
+static char isep = ' ', osep = ' ';
+static char blank[] = "";
+static int owidth = 80, gutter = 2;
-void usage(char *, char *);
-void getargs(int, char *[]);
-void getfile(void);
-int rs_getline(void);
-char *getlist(short **, char *);
-char *getnum(int *, char *, int);
-char **getptrs(char **);
-void prepfile(void);
-void prints(char *, int);
-void putfile(void);
+static void getargs(int, char *[]);
+static void getfile(void);
+static int rs_getline(void);
+static char *getlist(short **, char *);
+static char *getnum(int *, char *, int);
+static char **getptrs(char **);
+static void prepfile(void);
+static void prints(char *, int);
+static void putfile(void);
+static void usage(void);
-#define INCR(ep) do { \
+#define INCR(ep) do { \
if (++ep >= endelem) \
ep = getptrs(ep); \
} while(0)
int
-main(argc, argv)
- int argc;
- char *argv[];
+main(int argc, char *argv[])
{
getargs(argc, argv);
getfile();
@@ -117,20 +120,23 @@
exit(0);
}
-void
-getfile()
+static void
+getfile(void)
{
- register char *p;
- register char *endp;
- register char **ep = 0;
+ char *p;
+ char *endp;
+ char **ep;
+ int c;
int multisep = (flags & ONEISEPONLY ? 0 : 1);
int nullpad = flags & NULLPAD;
char **padto;
while (skip--) {
- rs_getline();
+ c = rs_getline();
if (flags & SKIPPRINT)
puts(curline);
+ if (c == EOF)
+ return;
}
rs_getline();
if (flags & NOARGS && curlen < owidth)
@@ -146,7 +152,6 @@
p++;
}
ep = getptrs(elem);
- p = curline;
do {
if (flags & ONEPERLINE) {
*ep = curline;
@@ -160,7 +165,7 @@
if (*p == isep && multisep)
continue; /* eat up column separators */
if (*p == isep) /* must be an empty column */
- *ep = "";
+ *ep = blank;
else /* store column entry */
*ep = p;
while (p < endp && *p != isep)
@@ -174,7 +179,7 @@
if (nullpad) { /* pad missing entries */
padto = elem + irows * icols;
while (ep < padto) {
- *ep = "";
+ *ep = blank;
INCR(ep);
}
}
@@ -183,38 +188,33 @@
nelem = ep - elem;
}
-void
-putfile()
+static void
+putfile(void)
{
- register char **ep;
- register int i, j, n;
+ char **ep;
+ int i, j, k;
ep = elem;
- if (flags & TRANSPOSE) {
+ 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 (n = 0, i = 0; i < orows && n < nelem; i++) {
- for (j = 0; j < ocols; j++) {
- if (n++ >= nelem)
- break;
- prints(*ep++, j);
- }
+ else
+ for (i = k = 0; i < orows; i++) {
+ for (j = 0; j < ocols; j++, k++)
+ if (k < nelem)
+ prints(ep[k], j);
putchar('\n');
}
- }
}
-void
-prints(s, col)
- char *s;
- int col;
+static void
+prints(char *s, int col)
{
- register int n;
- register char *p = s;
+ int n;
+ char *p = s;
while (*p)
p++;
@@ -228,26 +228,23 @@
putchar(osep);
}
-void
-usage(msg, s)
- char *msg, *s;
+static void
+usage(void)
{
- fprintf(stderr, "rs: ");
- fprintf(stderr, msg, s);
fprintf(stderr,
-"Usage: rs [ -[csCS][x][kKgGw][N]tTeEnyjhHm ] [ rows [ cols ] ]\n");
+ "usage: rs [-[csCS][x][kKgGw][N]tTeEnyjhHmz] [rows [cols]]\n");
exit(1);
}
-void
-prepfile()
+static void
+prepfile(void)
{
- register char **ep;
- register int i;
- register int j;
+ char **ep;
+ int i;
+ int j;
char **lp;
int colw;
- int max = 0;
+ int max;
int n;
if (!nelem)
@@ -261,7 +258,8 @@
else if (orows == 0 && ocols == 0) { /* decide rows and cols */
ocols = owidth / colw;
if (ocols == 0) {
- fprintf(stderr, "rs: Display width %d is less than column width %d\n", owidth, colw);
+ warnx("display width %d is less than column width %d",
+ owidth, colw);
ocols = 1;
}
if (ocols > nelem)
@@ -282,20 +280,21 @@
*ep = *(ep - nelem);
nelem = lp - elem;
}
- if (!(colwidths = (short *) malloc(ocols * sizeof(short)))) {
- fprintf(stderr, "rs: malloc: No gutter space\n");
- exit(1);
- }
+ if (!(colwidths = (short *) malloc(ocols * sizeof(short))))
+ errx(1, "malloc");
if (flags & SQUEEZE) {
+ ep = elem;
if (flags & TRANSPOSE)
- for (ep = elem, i = 0; i < ocols; i++) {
- for (j = 0; j < orows; j++)
+ for (i = 0; i < ocols; i++) {
+ max = 0;
+ for (j = 0; *ep != NULL && j < orows; j++)
if ((n = strlen(*ep++)) > max)
max = n;
colwidths[i] = max + gutter;
}
else
for (i = 0; i < ocols; i++) {
+ max = 0;
for (j = i; j < nelem; j += ocols)
if ((n = strlen(ep[j])) > max)
max = n;
@@ -326,20 +325,19 @@
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
-char ibuf[BSIZE]; /* two screenfuls should do */
+#define BSIZE (LINE_MAX * 2)
+static char ibuf[BSIZE];
-int
-rs_getline() /* get line; maintain curline, curlen; manage storage */
+static int
+rs_getline(void) /* get line; maintain curline, curlen; manage storage */
{
static int putlength;
static char *endblock = ibuf + BSIZE;
- register char *p;
- register int c, i;
+ char *p;
+ int c, i;
if (!irows) {
curline = ibuf;
@@ -347,51 +345,51 @@
}
else if (skip <= 0) { /* don't waste storage */
curline += curlen + 1;
- if (putlength) /* print length, recycle storage */
+ if (putlength) { /* print length, recycle storage */
printf(" %d line %d\n", curlen, irows);
+ curline = ibuf;
+ }
}
- if (!putlength && endblock - curline < BUFSIZ) { /* need storage */
+ if (!putlength && endblock - curline < LINE_MAX + 1) { /* need storage */
/*ww = endblock-curline; tt += ww;*/
/*printf("#wasted %d total %d\n",ww,tt);*/
- if (!(curline = (char *) malloc(BSIZE))) {
- fprintf(stderr, "rs: File too large\n");
- exit(1);
- }
+ if (!(curline = (char *) malloc(BSIZE)))
+ errx(1, "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')
+ for (p = curline, i = 0;; *p++ = c, i++) {
+ if ((c = getchar()) == EOF)
break;
+ if (i >= LINE_MAX)
+ errx(1, "maximum line length (%d) exceeded", LINE_MAX);
+ if (c == '\n')
+ break;
+ }
*p = '\0';
- curlen = i - 1;
+ curlen = i;
return(c);
}
-char **
-getptrs(sp)
- char **sp;
+static char **
+getptrs(char **sp)
{
- register char **p;
+ char **p;
allocsize += allocsize;
p = (char **)realloc(elem, allocsize * sizeof(char *));
- if (p == (char **)0) {
- fprintf(stderr, "rs: no memory\n");
- exit(1);
- }
+ if (p == NULL)
+ err(1, "no memory");
sp += (p - elem);
endelem = (elem = p) + allocsize;
return(sp);
}
-void
-getargs(ac, av)
- int ac;
- char *av[];
+static void
+getargs(int ac, char *av[])
{
- register char *p;
+ char *p;
if (ac == 1) {
flags |= NOARGS | TRANSPOSE;
@@ -423,7 +421,7 @@
case 'w': /* window width, default 80 */
p = getnum(&owidth, p, 0);
if (owidth <= 0)
- usage("Width must be a positive %s", "integer");
+ errx(1, "width must be a positive integer");
break;
case 'K': /* skip N lines */
flags |= SKIPPRINT;
@@ -479,7 +477,7 @@
p = getlist(&ocbd, p);
break;
default:
- usage("Bad flag: %.1s", p);
+ usage();
}
/*if (!osep)
osep = isep;*/
@@ -487,43 +485,42 @@
/*case 3:
opages = atoi(av[2]);*/
case 2:
- ocols = atoi(av[1]);
+ if ((ocols = atoi(av[1])) < 0)
+ ocols = 0;
case 1:
- orows = atoi(av[0]);
+ if ((orows = atoi(av[0])) < 0)
+ orows = 0;
case 0:
break;
default:
- usage("Too many %s.", "arguments");
+ errx(1, "too many arguments");
}
}
-char *
-getlist(list, p)
- short **list;
- char *p;
+static char *
+getlist(short **list, char *p)
{
- register int count = 1;
- register char *t;
+ int count = 1;
+ char *t;
for (t = p + 1; *t; t++) {
- if (!isdigit(*t))
- usage("Option %.1s requires a list of unsigned numbers separated by commas", t);
+ if (!isdigit((unsigned char)*t))
+ errx(1,
+ "option %.1s requires a list of unsigned numbers separated by commas", t);
count++;
- while (*t && isdigit(*t))
+ while (*t && isdigit((unsigned char)*t))
t++;
if (*t != ',')
break;
}
- if (!(*list = (short *) malloc(count * sizeof(short)))) {
- fprintf(stderr, "rs: No list space\n");
- exit(1);
- }
+ if (!(*list = (short *) malloc(count * sizeof(short))))
+ errx(1, "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))
+ while (*t && isdigit((unsigned char)*t))
t++;
if (*t != ',')
break;
@@ -532,22 +529,24 @@
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;
+/*
+ * num = number p points to; if (strict) complain
+ * returns pointer to end of num
+ */
+static char *
+getnum(int *num, char *p, int strict)
{
- register char *t = p;
+ char *t = p;
- if (!isdigit(*++t)) {
+ if (!isdigit((unsigned char)*++t)) {
if (strict || *t == '-' || *t == '+')
- usage("Option %.1s requires an unsigned integer", p);
+ errx(1, "option %.1s requires an unsigned integer", p);
*num = 0;
return(p);
}
*num = atoi(t);
while (*++t)
- if (!isdigit(*t))
+ if (!isdigit((unsigned char)*t))
break;
return(--t);
}