Importing usr.bin/sdiff

No Minix-specific changes needed.

Change-Id: Idbe7dcab6024d616022951f7942287fddecc3719
This commit is contained in:
Thomas Cort 2013-10-30 10:11:02 -04:00
parent 152a15652d
commit ce982eb757
11 changed files with 1521 additions and 1 deletions

View file

@ -470,6 +470,7 @@
./usr/bin/rsh minix-sys
./usr/bin/rz minix-sys
./usr/bin/screendump minix-sys
./usr/bin/sdiff minix-sys
./usr/bin/sed minix-sys
./usr/bin/seq minix-sys
./usr/bin/sha1 minix-sys
@ -2047,6 +2048,7 @@
./usr/man/man1/rmdir.1 minix-sys
./usr/man/man1/rsh.1 minix-sys
./usr/man/man1/rz.1 minix-sys
./usr/man/man1/sdiff.1 minix-sys
./usr/man/man1/sed.1 minix-sys
./usr/man/man1/seq.1 minix-sys
./usr/man/man1/set.1 minix-sys

View file

@ -206,6 +206,7 @@
2013/10/24 12:00:00,usr.bin/pwhash
2012/10/17 12:00:00,usr.bin/renice
2013/09/28 12:00:00,usr.bin/rev
2012/10/17 12:00:00,usr.bin/sdiff
2010/02/19 16:35:27,usr.bin/sed
2010/05/27 08:40:19,usr.bin/seq
2012/10/17 12:00:00,usr.bin/shlock

View file

@ -23,7 +23,7 @@ SUBDIR= asa \
printenv printf pwhash \
renice rev \
\
sed seq shlock \
sdiff sed seq shlock \
shuffle sort split stat su \
tee tic tput \
tr tsort tty unexpand unifdef \

11
usr.bin/sdiff/Makefile Normal file
View file

@ -0,0 +1,11 @@
# $NetBSD: Makefile,v 1.2 2009/04/14 22:15:26 lukem Exp $
WARNS?= 2 # XXX -Wcast-qual issues
PROG= sdiff
SRCS= common.c edit.c sdiff.c strtonum.c
LDADD+= -lutil
DPADD+= ${LIBUTIL}
.include <bsd.prog.mk>

21
usr.bin/sdiff/common.c Normal file
View file

@ -0,0 +1,21 @@
/* $NetBSD: common.c,v 1.1 2007/02/18 22:13:42 rmind Exp $ */
/* $OpenBSD: common.c,v 1.4 2006/05/25 03:20:32 ray Exp $ */
/*
* Written by Raymond Lai <ray@cyth.net>.
* Public domain.
*/
#include <err.h>
#include <stdlib.h>
#include <unistd.h>
#include "common.h"
void
cleanup(const char *filename)
{
if (unlink(filename))
err(2, "could not delete: %s", filename);
exit(2);
}

13
usr.bin/sdiff/common.h Normal file
View file

@ -0,0 +1,13 @@
/* $NetBSD: common.h,v 1.1 2007/02/18 22:13:42 rmind Exp $ */
/* $OpenBSD: common.h,v 1.2 2006/05/25 03:20:32 ray Exp $ */
/*
* Written by Raymond Lai <ray@cyth.net>.
* Public domain.
*/
__dead void cleanup(const char *);
long long
strtonum(const char *numstr, long long minval, long long maxval,
const char **errstrp);

187
usr.bin/sdiff/edit.c Normal file
View file

@ -0,0 +1,187 @@
/* $NetBSD: edit.c,v 1.4 2011/09/01 07:18:51 plunky Exp $ */
/* $OpenBSD: edit.c,v 1.14 2006/05/25 03:20:32 ray Exp $ */
/*
* Written by Raymond Lai <ray@cyth.net>.
* Public domain.
*/
#include <sys/types.h>
#include <sys/wait.h>
#include <ctype.h>
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "common.h"
#include "extern.h"
static void edit(const char *);
/*
* Takes the name of a file and opens it with an editor.
*/
static void
edit(const char *filename)
{
int status;
pid_t pid;
const char *editor;
editor = getenv("VISUAL");
if (editor == NULL)
editor = getenv("EDITOR");
if (editor == NULL)
editor = "vi";
/* Start editor on temporary file. */
switch (pid = fork()) {
case 0:
/* child */
execlp(editor, editor, filename, (void *)NULL);
warn("could not execute editor: %s", editor);
cleanup(filename);
case -1:
warn("could not fork");
cleanup(filename);
}
/* parent */
/* Wait for editor to exit. */
if (waitpid(pid, &status, 0) == -1) {
warn("waitpid");
cleanup(filename);
}
/* Check that editor terminated normally. */
if (!WIFEXITED(status)) {
warn("%s terminated abnormally", editor);
cleanup(filename);
}
}
/*
* Parse edit command. Returns 0 on success, -1 on error.
*/
int
eparse(const char *cmd, const char *left, const char *right)
{
FILE *file;
size_t nread, nwritten;
int fd;
char *filename;
char buf[BUFSIZ], *text;
/* Skip whitespace. */
while (isspace((int)(*cmd)))
++cmd;
text = NULL;
switch (*cmd) {
case '\0':
/* Edit empty file. */
break;
case 'b':
/* Both strings. */
if (left == NULL)
goto RIGHT;
if (right == NULL)
goto LEFT;
/* Neither column is blank, so print both. */
if (asprintf(&text, "%s\n%s\n", left, right) == -1)
err(2, "could not allocate memory");
break;
case 'l':
LEFT:
/* Skip if there is no left column. */
if (left == NULL)
break;
if (asprintf(&text, "%s\n", left) == -1)
err(2, "could not allocate memory");
break;
case 'r':
RIGHT:
/* Skip if there is no right column. */
if (right == NULL)
break;
if (asprintf(&text, "%s\n", right) == -1)
err(2, "could not allocate memory");
break;
default:
return (-1);
}
/* Create temp file. */
if (asprintf(&filename, "%s/sdiff.XXXXXXXXXX", tmpdir) == -1)
err(2, "asprintf");
if ((fd = mkstemp(filename)) == -1)
err(2, "mkstemp");
if (text != NULL) {
size_t len;
len = strlen(text);
if ((size_t)write(fd, text, len) != len) {
warn("error writing to temp file");
cleanup(filename);
}
}
close(fd);
/* text is no longer used. */
free(text);
/* Edit temp file. */
edit(filename);
/* Open temporary file. */
if (!(file = fopen(filename, "r"))) {
warn("could not open edited file: %s", filename);
cleanup(filename);
}
/* Copy temporary file contents to output file. */
for (nread = sizeof(buf); nread == sizeof(buf);) {
nread = fread(buf, sizeof(*buf), sizeof(buf), file);
/* Test for error or end of file. */
if (nread != sizeof(buf) &&
(ferror(file) || !feof(file))) {
warnx("error reading edited file: %s", filename);
cleanup(filename);
}
/*
* If we have nothing to read, break out of loop
* instead of writing nothing.
*/
if (!nread)
break;
/* Write data we just read. */
nwritten = fwrite(buf, sizeof(*buf), nread, outfile);
if (nwritten != nread) {
warnx("error writing to output file");
cleanup(filename);
}
}
/* We've reached the end of the temporary file, so remove it. */
if (unlink(filename))
warn("could not delete: %s", filename);
fclose(file);
free(filename);
return (0);
}

12
usr.bin/sdiff/extern.h Normal file
View file

@ -0,0 +1,12 @@
/* $NetBSD: extern.h,v 1.1 2007/02/18 22:13:42 rmind Exp $ */
/* $OpenBSD: extern.h,v 1.4 2006/05/25 03:20:32 ray Exp $ */
/*
* Written by Raymond Lai <ray@cyth.net>.
* Public domain.
*/
extern FILE *outfile; /* file to save changes to */
extern const char *tmpdir;
int eparse(const char *, const char *, const char *);

167
usr.bin/sdiff/sdiff.1 Normal file
View file

@ -0,0 +1,167 @@
.\" $NetBSD: sdiff.1,v 1.2 2011/04/25 22:42:11 wiz Exp $
.\" $OpenBSD: sdiff.1,v 1.11 2007/02/22 02:50:56 ray Exp $
.\"
.\" Written by Raymond Lai <ray@cyth.net>.
.\" Public domain.
.\"
.Dd February 21, 2007
.Dt SDIFF 1
.Os
.Sh NAME
.Nm sdiff
.Nd side-by-side diff
.Sh SYNOPSIS
.Nm
.Op Fl abdilstW
.Op Fl I Ar regexp
.Op Fl o Ar outfile
.Op Fl w Ar width
.Ar file1
.Ar file2
.Sh DESCRIPTION
.Nm
displays two files side by side,
with any differences between the two highlighted as follows:
new lines are marked with
.Sq \*[Gt] ;
deleted lines are marked with
.Sq \*[Lt] ;
and changed lines are marked with
.Sq | .
.Pp
.Nm
can also be used to interactively merge two files,
prompting at each set of differences.
See the
.Fl o
option for an explanation.
.Pp
The options are:
.Bl -tag -width Ds
.It Fl l
Only print the left column for identical lines.
.It Fl o Ar outfile
Interactively merge
.Ar file1
and
.Ar file2
into
.Ar outfile .
In this mode, the user is prompted for each set of differences.
See
.Ev EDITOR
and
.Ev VISUAL ,
below,
for details of which editor, if any, is invoked.
.Pp
The commands are as follows:
.Bl -tag -width Ds
.It Cm l
Choose left set of diffs.
.It Cm r
Choose right set of diffs.
.It Cm s
Silent mode \(en identical lines are not printed.
.It Cm v
Verbose mode \(en identical lines are printed.
.It Cm e
Start editing an empty file, which will be merged into
.Ar outfile
upon exiting the editor.
.It Cm e Cm l
Start editing file with left set of diffs.
.It Cm e Cm r
Start editing file with right set of diffs.
.It Cm e Cm b
Start editing file with both sets of diffs.
.It Cm q
Quit
.Nm .
.El
.It Fl s
Skip identical lines.
.It Fl w Ar width
Print a maximum of
.Ar width
characters on each line.
The default is 130 characters.
.El
.Pp
Options passed to
.Xr diff 1
are:
.Bl -tag -width Ds
.It Fl a
Treat
.Ar file1
and
.Ar file2
as text files.
.It Fl b
Ignore trailing blank spaces.
.It Fl d
Minimize diff size.
.It Fl I Ar regexp
Ignore line changes matching
.Ar regexp .
All lines in the change must match
.Ar regexp
for the change to be ignored.
.It Fl i
Do a case-insensitive comparison.
.It Fl t
Expand tabs to spaces.
.It Fl W
Ignore all spaces
(the
.Fl w
flag is passed to
.Xr diff 1 ) .
.El
.Sh ENVIRONMENT
.Bl -tag -width Ds
.It Ev EDITOR , VISUAL
Specifies an editor to use with the
.Fl o
option.
If both
.Ev EDITOR
and
.Ev VISUAL
are set,
.Ev VISUAL
takes precedence.
If neither
.Ev EDITOR
nor
.Ev VISUAL
are set,
the default is
.Xr vi 1 .
.It Ev TMPDIR
Specifies a directory for temporary files to be created.
The default is
.Pa /tmp .
.El
.Sh SEE ALSO
.Xr diff 1 ,
.Xr diff3 1 ,
.Xr vi 1 ,
.Xr re_format 7
.Sh AUTHORS
.Nm
was written from scratch for the public domain by
.An Ray Lai Aq ray@cyth.net .
.Sh CAVEATS
Although undocumented,
.Nm
supports all options supported by GNU sdiff.
Some options require GNU diff.
.Pp
Tabs are treated as anywhere from one to eight characters wide,
depending on the current column.
Terminals that treat tabs as eight characters wide will look best.
.Sh BUGS
.Nm
may not work with binary data.

1038
usr.bin/sdiff/sdiff.c Normal file

File diff suppressed because it is too large Load diff

68
usr.bin/sdiff/strtonum.c Normal file
View file

@ -0,0 +1,68 @@
/* $NetBSD: strtonum.c,v 1.1 2007/02/18 22:13:43 rmind Exp $ */
/* $OpenBSD: strtonum.c,v 1.6 2004/08/03 19:38:01 millert Exp $ */
/*
* Copyright (c) 2004 Ted Unangst and Todd Miller
* All rights reserved.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <errno.h>
#include <limits.h>
#include <stdlib.h>
#include "common.h"
#define INVALID 1
#define TOOSMALL 2
#define TOOLARGE 3
long long
strtonum(const char *numstr, long long minval, long long maxval,
const char **errstrp)
{
long long ll = 0;
char *ep;
int error = 0;
struct errval {
const char *errstr;
int err;
} ev[4] = {
{ NULL, 0 },
{ "invalid", EINVAL },
{ "too small", ERANGE },
{ "too large", ERANGE },
};
ev[0].err = errno;
errno = 0;
if (minval > maxval)
error = INVALID;
else {
ll = strtoll(numstr, &ep, 10);
if (numstr == ep || *ep != '\0')
error = INVALID;
else if ((ll == LLONG_MIN && errno == ERANGE) || ll < minval)
error = TOOSMALL;
else if ((ll == LLONG_MAX && errno == ERANGE) || ll > maxval)
error = TOOLARGE;
}
if (errstrp != NULL)
*errstrp = ev[error].errstr;
errno = ev[error].err;
if (error)
ll = 0;
return (ll);
}