Update to the HEAD 2002.10.19 snapshot. This fixes some irritating bugs.

PR:		42596
Submitted by:	Jasper Jongmans <j.jongmans@aprogas.net>
This commit is contained in:
Maxim Sobolev 2002-10-19 10:32:36 +00:00
parent 1de4a63cc5
commit aed1ab7c12
Notes: svn2git 2021-03-31 03:12:20 +00:00
svn path=/head/; revision=68328
6 changed files with 84 additions and 70 deletions

View file

@ -6,7 +6,7 @@
# #
PORTNAME= sed_inplace PORTNAME= sed_inplace
PORTVERSION= 2002.06.28 PORTVERSION= 2002.10.19
CATEGORIES= textproc CATEGORIES= textproc
MASTER_SITES= # MASTER_SITES= #
DISTFILES= # DISTFILES= #

View file

@ -36,6 +36,7 @@
*/ */
#include <sys/cdefs.h> #include <sys/cdefs.h>
__FBSDID("$FreeBSD: /tmp/pcvs/ports/textproc/sed_inplace/src/Attic/compile.c,v 1.3 2002-10-19 10:32:36 sobomax Exp $");
#ifndef lint #ifndef lint
static const char sccsid[] = "@(#)compile.c 8.1 (Berkeley) 6/6/93"; static const char sccsid[] = "@(#)compile.c 8.1 (Berkeley) 6/6/93";
@ -171,8 +172,14 @@ compile_stream(link)
} }
semicolon: EATSPACE(); semicolon: EATSPACE();
if (p && (*p == '#' || *p == '\0')) if (p) {
continue; if (*p == '#' || *p == '\0')
continue;
else if (*p == ';') {
p++;
goto semicolon;
}
}
if ((*link = cmd = malloc(sizeof(struct s_command))) == NULL) if ((*link = cmd = malloc(sizeof(struct s_command))) == NULL)
err(1, "malloc"); err(1, "malloc");
link = &cmd->next; link = &cmd->next;

View file

@ -44,15 +44,15 @@ extern regmatch_t *match;
extern size_t maxnsub; extern size_t maxnsub;
extern u_long linenum; extern u_long linenum;
extern int appendnum; extern int appendnum;
extern int lastline;
extern int aflag, eflag, nflag; extern int aflag, eflag, nflag;
extern const char *fname; extern const char *fname;
extern int rflags; /* regex flags to use */ extern int rflags; /* regex flags to use */
void cfclose(struct s_command *, struct s_command *); void cfclose(struct s_command *, struct s_command *);
void compile(void); void compile(void);
void cspace(SPACE *, char *, size_t, enum e_spflag); void cspace(SPACE *, const char *, size_t, enum e_spflag);
char *cu_fgets(char *, int, int *); char *cu_fgets(char *, int, int *);
int mf_fgets(SPACE *, enum e_spflag); int mf_fgets(SPACE *, enum e_spflag);
int lastline(void);
void process(void); void process(void);
char *strregerror(int, regex_t *); char *strregerror(int, regex_t *);

View file

@ -36,6 +36,7 @@
*/ */
#include <sys/cdefs.h> #include <sys/cdefs.h>
__FBSDID("$FreeBSD: /tmp/pcvs/ports/textproc/sed_inplace/src/Attic/main.c,v 1.4 2002-10-19 10:32:36 sobomax Exp $");
#ifndef lint #ifndef lint
static const char copyright[] = static const char copyright[] =
@ -95,6 +96,8 @@ struct s_flist {
*/ */
static struct s_flist *files, **fl_nextp = &files; static struct s_flist *files, **fl_nextp = &files;
static FILE *curfile; /* Current open file */
int aflag, eflag, nflag; int aflag, eflag, nflag;
int rflags = 0; int rflags = 0;
static int rval; /* Exit status */ static int rval; /* Exit status */
@ -106,7 +109,6 @@ static int rval; /* Exit status */
const char *fname; /* File name. */ const char *fname; /* File name. */
const char *inplace; /* Inplace edit file extension. */ const char *inplace; /* Inplace edit file extension. */
u_long linenum; u_long linenum;
int lastline; /* TRUE on the last line of the last file */
static void add_compunit(enum e_cut, char *); static void add_compunit(enum e_cut, char *);
static void add_file(char *); static void add_file(char *);
@ -297,36 +299,34 @@ mf_fgets(sp, spflag)
SPACE *sp; SPACE *sp;
enum e_spflag spflag; enum e_spflag spflag;
{ {
static FILE *f; /* Current open file */
size_t len; size_t len;
char *p; char *p;
int c; int c;
static int firstfile; static int firstfile;
if (f == NULL) { if (curfile == NULL) {
/* stdin? */ /* stdin? */
if (files->fname == NULL) { if (files->fname == NULL) {
if (inplace != NULL) if (inplace != NULL)
errx(1, "-i may not be used with stdin"); errx(1, "-i may not be used with stdin");
f = stdin; curfile = stdin;
fname = "stdin"; fname = "stdin";
} }
firstfile = 1; firstfile = 1;
} }
for (;;) { for (;;) {
if (f != NULL && (c = getc(f)) != EOF) { if (curfile != NULL && (c = getc(curfile)) != EOF) {
(void)ungetc(c, f); (void)ungetc(c, curfile);
break; break;
} }
/* If we are here then either eof or no files are open yet */ /* If we are here then either eof or no files are open yet */
if (f == stdin) { if (curfile == stdin) {
sp->len = 0; sp->len = 0;
lastline = 1;
return (0); return (0);
} }
if (f != NULL) { if (curfile != NULL) {
fclose(f); fclose(curfile);
} }
if (firstfile == 0) { if (firstfile == 0) {
files = files->next; files = files->next;
@ -334,7 +334,6 @@ mf_fgets(sp, spflag)
firstfile = 0; firstfile = 0;
if (files == NULL) { if (files == NULL) {
sp->len = 0; sp->len = 0;
lastline = 1;
return (0); return (0);
} }
if (inplace != NULL) { if (inplace != NULL) {
@ -342,7 +341,7 @@ mf_fgets(sp, spflag)
continue; continue;
} }
fname = files->fname; fname = files->fname;
if ((f = fopen(fname, "r")) == NULL) { if ((curfile = fopen(fname, "r")) == NULL) {
warn("%s", fname); warn("%s", fname);
rval = 1; rval = 1;
continue; continue;
@ -351,28 +350,21 @@ mf_fgets(sp, spflag)
unlink(fname); unlink(fname);
} }
/* /*
* We are here only when f is open and we still have something to * We are here only when curfile is open and we still have something
* read from it. * to read from it.
* *
* Use fgetln so that we can handle essentially infinite input data. * Use fgetln so that we can handle essentially infinite input data.
* Can't use the pointer into the stdio buffer as the process space * Can't use the pointer into the stdio buffer as the process space
* because the ungetc() can cause it to move. * because the ungetc() can cause it to move.
*/ */
p = fgetln(f, &len); p = fgetln(curfile, &len);
if (ferror(f)) if (ferror(curfile))
errx(1, "%s: %s", fname, strerror(errno ? errno : EIO)); errx(1, "%s: %s", fname, strerror(errno ? errno : EIO));
if (len != 0 && p[len - 1] == '\n') if (len != 0 && p[len - 1] == '\n')
len--; len--;
cspace(sp, p, len, spflag); cspace(sp, p, len, spflag);
linenum++; linenum++;
if (files->next == NULL) {
if ((c = getc(f)) != EOF) {
(void)ungetc(c, f);
} else {
lastline = 1;
}
}
return (1); return (1);
} }
@ -421,9 +413,7 @@ inplace_edit(filename)
char **filename; char **filename;
{ {
struct stat orig; struct stat orig;
int input, output;
char backup[MAXPATHLEN]; char backup[MAXPATHLEN];
char *buffer;
if (lstat(*filename, &orig) == -1) if (lstat(*filename, &orig) == -1)
err(1, "lstat"); err(1, "lstat");
@ -433,35 +423,48 @@ inplace_edit(filename)
} }
if (*inplace == '\0') { if (*inplace == '\0') {
char template[] = "/tmp/sed.XXXXXXXXXX"; /*
* This is a bit of a hack: we use mkstemp() to avoid the
output = mkstemp(template); * mktemp() link-time warning, although mktemp() would fit in
if (output == -1) * this context much better. We're only interested in getting
err(1, "mkstemp"); * a name for use in the rename(); there aren't any security
strlcpy(backup, template, MAXPATHLEN); * issues here that don't already exist in relation to the
* original file and its directory.
*/
int fd;
strlcpy(backup, *filename, sizeof(backup));
strlcat(backup, ".XXXXXXXXXX", sizeof(backup));
fd = mkstemp(backup);
if (fd == -1)
errx(1, "could not create backup of %s", *filename);
else
close(fd);
} else { } else {
strlcpy(backup, *filename, MAXPATHLEN); strlcpy(backup, *filename, sizeof(backup));
strlcat(backup, inplace, MAXPATHLEN); strlcat(backup, inplace, sizeof(backup));
output = open(backup, O_WRONLY | O_CREAT | O_TRUNC);
if (output == -1)
err(1, "open(%s)", backup);
} }
input = open(*filename, O_RDONLY); if (rename(*filename, backup) == -1)
if (input == -1) err(1, "rename(\"%s\", \"%s\")", *filename, backup);
err(1, "open(%s)", *filename); if (freopen(*filename, "w", stdout) == NULL)
if (fchmod(output, orig.st_mode & ~S_IFMT) == -1) err(1, "open(\"%s\")", *filename);
err(1, "chmod"); if (fchmod(fileno(stdout), orig.st_mode) == -1)
buffer = (char *)mmap(0, orig.st_size, PROT_READ, MAP_SHARED, input, 0); err(1, "chmod(\"%s\")", *filename);
if (buffer == MAP_FAILED)
err(1, "mmap(%s)", *filename);
if (write(output, buffer, orig.st_size) == -1)
err(1, "write(%s)", backup);
if (munmap(buffer, orig.st_size) == -1)
err(1, "munmap(%s)", *filename);
close(input);
close(output);
freopen(*filename, "w", stdout);
*filename = strdup(backup); *filename = strdup(backup);
if (*filename == NULL)
err(1, "malloc");
return 0; return 0;
} }
int
lastline(void)
{
int ch;
if (files->next != NULL)
return (0);
if ((ch = getc(curfile)) == EOF)
return (1);
ungetc(ch, curfile);
return (0);
}

View file

@ -36,6 +36,7 @@
*/ */
#include <sys/cdefs.h> #include <sys/cdefs.h>
__FBSDID("$FreeBSD: /tmp/pcvs/ports/textproc/sed_inplace/src/Attic/misc.c,v 1.3 2002-10-19 10:32:36 sobomax Exp $");
#ifndef lint #ifndef lint
static const char sccsid[] = "@(#)misc.c 8.1 (Berkeley) 6/6/93"; static const char sccsid[] = "@(#)misc.c 8.1 (Berkeley) 6/6/93";
@ -67,7 +68,7 @@ strregerror(errcode, preg)
if (oe != NULL) if (oe != NULL)
free(oe); free(oe);
s = regerror(errcode, preg, "", 0); s = regerror(errcode, preg, NULL, 0);
if ((oe = malloc(s)) == NULL) if ((oe = malloc(s)) == NULL)
err(1, "malloc"); err(1, "malloc");
(void)regerror(errcode, preg, oe, s); (void)regerror(errcode, preg, oe, s);

View file

@ -36,6 +36,7 @@
*/ */
#include <sys/cdefs.h> #include <sys/cdefs.h>
__FBSDID("$FreeBSD: /tmp/pcvs/ports/textproc/sed_inplace/src/Attic/process.c,v 1.4 2002-10-19 10:32:36 sobomax Exp $");
#ifndef lint #ifndef lint
static const char sccsid[] = "@(#)process.c 8.6 (Berkeley) 4/20/94"; static const char sccsid[] = "@(#)process.c 8.6 (Berkeley) 4/20/94";
@ -67,10 +68,10 @@ static SPACE HS, PS, SS;
#define hs HS.space #define hs HS.space
#define hsl HS.len #define hsl HS.len
static inline int applies(struct s_command *); static __inline int applies(struct s_command *);
static void flush_appends(void); static void flush_appends(void);
static void lputs(char *); static void lputs(char *);
static inline int regexec_e(regex_t *, const char *, int, int, size_t); static __inline int regexec_e(regex_t *, const char *, int, int, size_t);
static void regsub(SPACE *, char *, char *); static void regsub(SPACE *, char *, char *);
static int substitute(struct s_command *); static int substitute(struct s_command *);
@ -95,6 +96,8 @@ process()
size_t len, oldpsl = 0; size_t len, oldpsl = 0;
char *p; char *p;
p = NULL;
for (linenum = 0; mf_fgets(&PS, REPLACE);) { for (linenum = 0; mf_fgets(&PS, REPLACE);) {
pd = 0; pd = 0;
top: top:
@ -136,7 +139,7 @@ redirect:
if (pd) if (pd)
goto new; goto new;
if (psl == 0 || if (psl == 0 ||
(p = memchr(ps, '\n', psl - 1)) == NULL) { (p = memchr(ps, '\n', psl)) == NULL) {
pd = 1; pd = 1;
goto new; goto new;
} else { } else {
@ -175,11 +178,8 @@ redirect:
case 'N': case 'N':
flush_appends(); flush_appends();
cspace(&PS, "\n", 1, 0); cspace(&PS, "\n", 1, 0);
if (!mf_fgets(&PS, 0)) { if (!mf_fgets(&PS, 0))
if (!nflag && !pd)
OUT(ps)
exit(0); exit(0);
}
break; break;
case 'p': case 'p':
if (pd) if (pd)
@ -190,7 +190,7 @@ redirect:
if (pd) if (pd)
break; break;
if (psl != 0 && if (psl != 0 &&
(p = memchr(ps, '\n', psl - 1)) != NULL) { (p = memchr(ps, '\n', psl)) != NULL) {
oldpsl = psl; oldpsl = psl;
psl = p - ps; psl = p - ps;
} }
@ -236,6 +236,8 @@ redirect:
err(1, "%s", cp->t); err(1, "%s", cp->t);
break; break;
case 'x': case 'x':
if (hs == NULL)
cspace(&HS, "", 0, REPLACE);
tspace = PS; tspace = PS;
PS = HS; PS = HS;
HS = tspace; HS = tspace;
@ -267,13 +269,13 @@ new: if (!nflag && !pd)
*/ */
#define MATCH(a) \ #define MATCH(a) \
(a)->type == AT_RE ? regexec_e((a)->u.r, ps, 0, 1, psl) : \ (a)->type == AT_RE ? regexec_e((a)->u.r, ps, 0, 1, psl) : \
(a)->type == AT_LINE ? linenum == (a)->u.l : lastline (a)->type == AT_LINE ? linenum == (a)->u.l : lastline()
/* /*
* Return TRUE if the command applies to the current line. Sets the inrange * Return TRUE if the command applies to the current line. Sets the inrange
* flag to process ranges. Interprets the non-select (``!'') flag. * flag to process ranges. Interprets the non-select (``!'') flag.
*/ */
static inline int static __inline int
applies(cp) applies(cp)
struct s_command *cp; struct s_command *cp;
{ {
@ -468,7 +470,8 @@ lputs(s)
char *s; char *s;
{ {
int count; int count;
char *escapes, *p; const char *escapes;
char *p;
struct winsize win; struct winsize win;
static int termwidth = -1; static int termwidth = -1;
@ -512,7 +515,7 @@ lputs(s)
errx(1, "stdout: %s", strerror(errno ? errno : EIO)); errx(1, "stdout: %s", strerror(errno ? errno : EIO));
} }
static inline int static __inline int
regexec_e(preg, string, eflags, nomatch, slen) regexec_e(preg, string, eflags, nomatch, slen)
regex_t *preg; regex_t *preg;
const char *string; const char *string;
@ -598,7 +601,7 @@ regsub(sp, string, src)
void void
cspace(sp, p, len, spflag) cspace(sp, p, len, spflag)
SPACE *sp; SPACE *sp;
char *p; const char *p;
size_t len; size_t len;
enum e_spflag spflag; enum e_spflag spflag;
{ {