summaryrefslogtreecommitdiff
path: root/textproc/sed_inplace
diff options
context:
space:
mode:
authorMaxim Sobolev <sobomax@FreeBSD.org>2002-10-19 10:32:36 +0000
committerMaxim Sobolev <sobomax@FreeBSD.org>2002-10-19 10:32:36 +0000
commitaed1ab7c12dc57bc6b9f2e4b86bb6985c4e865c6 (patch)
tree96dc182c2de53213a6479d7f7d68dba5a930e62d /textproc/sed_inplace
parentDistfile was repacked (no content changes, except they now have a (diff)
Update to the HEAD 2002.10.19 snapshot. This fixes some irritating bugs.
PR: 42596 Submitted by: Jasper Jongmans <j.jongmans@aprogas.net>
Notes
Notes: svn path=/head/; revision=68328
Diffstat (limited to 'textproc/sed_inplace')
-rw-r--r--textproc/sed_inplace/Makefile2
-rw-r--r--textproc/sed_inplace/src/compile.c11
-rw-r--r--textproc/sed_inplace/src/extern.h4
-rw-r--r--textproc/sed_inplace/src/main.c105
-rw-r--r--textproc/sed_inplace/src/misc.c3
-rw-r--r--textproc/sed_inplace/src/process.c29
6 files changed, 84 insertions, 70 deletions
diff --git a/textproc/sed_inplace/Makefile b/textproc/sed_inplace/Makefile
index df905fc56dba..0ef2d57013c2 100644
--- a/textproc/sed_inplace/Makefile
+++ b/textproc/sed_inplace/Makefile
@@ -6,7 +6,7 @@
#
PORTNAME= sed_inplace
-PORTVERSION= 2002.06.28
+PORTVERSION= 2002.10.19
CATEGORIES= textproc
MASTER_SITES= #
DISTFILES= #
diff --git a/textproc/sed_inplace/src/compile.c b/textproc/sed_inplace/src/compile.c
index a092380c186c..1b0e524a496c 100644
--- a/textproc/sed_inplace/src/compile.c
+++ b/textproc/sed_inplace/src/compile.c
@@ -36,6 +36,7 @@
*/
#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
static const char sccsid[] = "@(#)compile.c 8.1 (Berkeley) 6/6/93";
@@ -171,8 +172,14 @@ compile_stream(link)
}
semicolon: EATSPACE();
- if (p && (*p == '#' || *p == '\0'))
- continue;
+ if (p) {
+ if (*p == '#' || *p == '\0')
+ continue;
+ else if (*p == ';') {
+ p++;
+ goto semicolon;
+ }
+ }
if ((*link = cmd = malloc(sizeof(struct s_command))) == NULL)
err(1, "malloc");
link = &cmd->next;
diff --git a/textproc/sed_inplace/src/extern.h b/textproc/sed_inplace/src/extern.h
index a0ad39b0ccff..df49f7e36ee0 100644
--- a/textproc/sed_inplace/src/extern.h
+++ b/textproc/sed_inplace/src/extern.h
@@ -44,15 +44,15 @@ extern regmatch_t *match;
extern size_t maxnsub;
extern u_long linenum;
extern int appendnum;
-extern int lastline;
extern int aflag, eflag, nflag;
extern const char *fname;
extern int rflags; /* regex flags to use */
void cfclose(struct s_command *, struct s_command *);
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 *);
int mf_fgets(SPACE *, enum e_spflag);
+int lastline(void);
void process(void);
char *strregerror(int, regex_t *);
diff --git a/textproc/sed_inplace/src/main.c b/textproc/sed_inplace/src/main.c
index 3646a1435f7e..31508e80ca3a 100644
--- a/textproc/sed_inplace/src/main.c
+++ b/textproc/sed_inplace/src/main.c
@@ -36,6 +36,7 @@
*/
#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
static const char copyright[] =
@@ -95,6 +96,8 @@ struct s_flist {
*/
static struct s_flist *files, **fl_nextp = &files;
+static FILE *curfile; /* Current open file */
+
int aflag, eflag, nflag;
int rflags = 0;
static int rval; /* Exit status */
@@ -106,7 +109,6 @@ static int rval; /* Exit status */
const char *fname; /* File name. */
const char *inplace; /* Inplace edit file extension. */
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_file(char *);
@@ -297,36 +299,34 @@ mf_fgets(sp, spflag)
SPACE *sp;
enum e_spflag spflag;
{
- static FILE *f; /* Current open file */
size_t len;
char *p;
int c;
static int firstfile;
- if (f == NULL) {
+ if (curfile == NULL) {
/* stdin? */
if (files->fname == NULL) {
if (inplace != NULL)
errx(1, "-i may not be used with stdin");
- f = stdin;
+ curfile = stdin;
fname = "stdin";
}
firstfile = 1;
}
for (;;) {
- if (f != NULL && (c = getc(f)) != EOF) {
- (void)ungetc(c, f);
+ if (curfile != NULL && (c = getc(curfile)) != EOF) {
+ (void)ungetc(c, curfile);
break;
}
/* If we are here then either eof or no files are open yet */
- if (f == stdin) {
+ if (curfile == stdin) {
sp->len = 0;
- lastline = 1;
return (0);
}
- if (f != NULL) {
- fclose(f);
+ if (curfile != NULL) {
+ fclose(curfile);
}
if (firstfile == 0) {
files = files->next;
@@ -334,7 +334,6 @@ mf_fgets(sp, spflag)
firstfile = 0;
if (files == NULL) {
sp->len = 0;
- lastline = 1;
return (0);
}
if (inplace != NULL) {
@@ -342,7 +341,7 @@ mf_fgets(sp, spflag)
continue;
}
fname = files->fname;
- if ((f = fopen(fname, "r")) == NULL) {
+ if ((curfile = fopen(fname, "r")) == NULL) {
warn("%s", fname);
rval = 1;
continue;
@@ -351,28 +350,21 @@ mf_fgets(sp, spflag)
unlink(fname);
}
/*
- * We are here only when f is open and we still have something to
- * read from it.
+ * We are here only when curfile is open and we still have something
+ * to read from it.
*
* Use fgetln so that we can handle essentially infinite input data.
* Can't use the pointer into the stdio buffer as the process space
* because the ungetc() can cause it to move.
*/
- p = fgetln(f, &len);
- if (ferror(f))
+ p = fgetln(curfile, &len);
+ if (ferror(curfile))
errx(1, "%s: %s", fname, strerror(errno ? errno : EIO));
if (len != 0 && p[len - 1] == '\n')
len--;
cspace(sp, p, len, spflag);
linenum++;
- if (files->next == NULL) {
- if ((c = getc(f)) != EOF) {
- (void)ungetc(c, f);
- } else {
- lastline = 1;
- }
- }
return (1);
}
@@ -421,9 +413,7 @@ inplace_edit(filename)
char **filename;
{
struct stat orig;
- int input, output;
char backup[MAXPATHLEN];
- char *buffer;
if (lstat(*filename, &orig) == -1)
err(1, "lstat");
@@ -433,35 +423,48 @@ inplace_edit(filename)
}
if (*inplace == '\0') {
- char template[] = "/tmp/sed.XXXXXXXXXX";
-
- output = mkstemp(template);
- if (output == -1)
- err(1, "mkstemp");
- strlcpy(backup, template, MAXPATHLEN);
+ /*
+ * This is a bit of a hack: we use mkstemp() to avoid the
+ * mktemp() link-time warning, although mktemp() would fit in
+ * this context much better. We're only interested in getting
+ * a name for use in the rename(); there aren't any security
+ * 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 {
- strlcpy(backup, *filename, MAXPATHLEN);
- strlcat(backup, inplace, MAXPATHLEN);
- output = open(backup, O_WRONLY | O_CREAT | O_TRUNC);
- if (output == -1)
- err(1, "open(%s)", backup);
+ strlcpy(backup, *filename, sizeof(backup));
+ strlcat(backup, inplace, sizeof(backup));
}
- input = open(*filename, O_RDONLY);
- if (input == -1)
- err(1, "open(%s)", *filename);
- if (fchmod(output, orig.st_mode & ~S_IFMT) == -1)
- err(1, "chmod");
- buffer = (char *)mmap(0, orig.st_size, PROT_READ, MAP_SHARED, input, 0);
- 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);
+ if (rename(*filename, backup) == -1)
+ err(1, "rename(\"%s\", \"%s\")", *filename, backup);
+ if (freopen(*filename, "w", stdout) == NULL)
+ err(1, "open(\"%s\")", *filename);
+ if (fchmod(fileno(stdout), orig.st_mode) == -1)
+ err(1, "chmod(\"%s\")", *filename);
*filename = strdup(backup);
+ if (*filename == NULL)
+ err(1, "malloc");
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);
+}
diff --git a/textproc/sed_inplace/src/misc.c b/textproc/sed_inplace/src/misc.c
index 1119addd7332..47cd01518093 100644
--- a/textproc/sed_inplace/src/misc.c
+++ b/textproc/sed_inplace/src/misc.c
@@ -36,6 +36,7 @@
*/
#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
static const char sccsid[] = "@(#)misc.c 8.1 (Berkeley) 6/6/93";
@@ -67,7 +68,7 @@ strregerror(errcode, preg)
if (oe != NULL)
free(oe);
- s = regerror(errcode, preg, "", 0);
+ s = regerror(errcode, preg, NULL, 0);
if ((oe = malloc(s)) == NULL)
err(1, "malloc");
(void)regerror(errcode, preg, oe, s);
diff --git a/textproc/sed_inplace/src/process.c b/textproc/sed_inplace/src/process.c
index 2e166a451204..4bbe62635ecc 100644
--- a/textproc/sed_inplace/src/process.c
+++ b/textproc/sed_inplace/src/process.c
@@ -36,6 +36,7 @@
*/
#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
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 hsl HS.len
-static inline int applies(struct s_command *);
+static __inline int applies(struct s_command *);
static void flush_appends(void);
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 int substitute(struct s_command *);
@@ -95,6 +96,8 @@ process()
size_t len, oldpsl = 0;
char *p;
+ p = NULL;
+
for (linenum = 0; mf_fgets(&PS, REPLACE);) {
pd = 0;
top:
@@ -136,7 +139,7 @@ redirect:
if (pd)
goto new;
if (psl == 0 ||
- (p = memchr(ps, '\n', psl - 1)) == NULL) {
+ (p = memchr(ps, '\n', psl)) == NULL) {
pd = 1;
goto new;
} else {
@@ -175,11 +178,8 @@ redirect:
case 'N':
flush_appends();
cspace(&PS, "\n", 1, 0);
- if (!mf_fgets(&PS, 0)) {
- if (!nflag && !pd)
- OUT(ps)
+ if (!mf_fgets(&PS, 0))
exit(0);
- }
break;
case 'p':
if (pd)
@@ -190,7 +190,7 @@ redirect:
if (pd)
break;
if (psl != 0 &&
- (p = memchr(ps, '\n', psl - 1)) != NULL) {
+ (p = memchr(ps, '\n', psl)) != NULL) {
oldpsl = psl;
psl = p - ps;
}
@@ -236,6 +236,8 @@ redirect:
err(1, "%s", cp->t);
break;
case 'x':
+ if (hs == NULL)
+ cspace(&HS, "", 0, REPLACE);
tspace = PS;
PS = HS;
HS = tspace;
@@ -267,13 +269,13 @@ new: if (!nflag && !pd)
*/
#define MATCH(a) \
(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
* flag to process ranges. Interprets the non-select (``!'') flag.
*/
-static inline int
+static __inline int
applies(cp)
struct s_command *cp;
{
@@ -468,7 +470,8 @@ lputs(s)
char *s;
{
int count;
- char *escapes, *p;
+ const char *escapes;
+ char *p;
struct winsize win;
static int termwidth = -1;
@@ -512,7 +515,7 @@ lputs(s)
errx(1, "stdout: %s", strerror(errno ? errno : EIO));
}
-static inline int
+static __inline int
regexec_e(preg, string, eflags, nomatch, slen)
regex_t *preg;
const char *string;
@@ -598,7 +601,7 @@ regsub(sp, string, src)
void
cspace(sp, p, len, spflag)
SPACE *sp;
- char *p;
+ const char *p;
size_t len;
enum e_spflag spflag;
{